00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/intreadwrite.h"
00024 #include "libavutil/pixdesc.h"
00025 #include "avcodec.h"
00026 #include "bytestream.h"
00027 #include "internal.h"
00028 #include "xwd.h"
00029
00030 #define WINDOW_NAME "lavcxwdenc"
00031 #define WINDOW_NAME_SIZE 11
00032
00033 static av_cold int xwd_encode_init(AVCodecContext *avctx)
00034 {
00035 avctx->coded_frame = avcodec_alloc_frame();
00036 if (!avctx->coded_frame)
00037 return AVERROR(ENOMEM);
00038
00039 return 0;
00040 }
00041
00042 static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
00043 const AVFrame *p, int *got_packet)
00044 {
00045 enum PixelFormat pix_fmt = avctx->pix_fmt;
00046 uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
00047 uint32_t rgb[3] = { 0 }, bitorder = 0;
00048 uint32_t header_size;
00049 int i, out_size, ret;
00050 uint8_t *ptr, *buf;
00051
00052 pixdepth = av_get_bits_per_pixel(&av_pix_fmt_descriptors[pix_fmt]);
00053 if (av_pix_fmt_descriptors[pix_fmt].flags & PIX_FMT_BE)
00054 be = 1;
00055 switch (pix_fmt) {
00056 case PIX_FMT_ARGB:
00057 case PIX_FMT_BGRA:
00058 case PIX_FMT_RGBA:
00059 case PIX_FMT_ABGR:
00060 if (pix_fmt == PIX_FMT_ARGB ||
00061 pix_fmt == PIX_FMT_ABGR)
00062 be = 1;
00063 if (pix_fmt == PIX_FMT_ABGR ||
00064 pix_fmt == PIX_FMT_RGBA) {
00065 rgb[0] = 0xFF;
00066 rgb[1] = 0xFF00;
00067 rgb[2] = 0xFF0000;
00068 } else {
00069 rgb[0] = 0xFF0000;
00070 rgb[1] = 0xFF00;
00071 rgb[2] = 0xFF;
00072 }
00073 bpp = 32;
00074 pixdepth = 24;
00075 vclass = XWD_TRUE_COLOR;
00076 bpad = 32;
00077 break;
00078 case PIX_FMT_BGR24:
00079 case PIX_FMT_RGB24:
00080 if (pix_fmt == PIX_FMT_RGB24)
00081 be = 1;
00082 bpp = 24;
00083 vclass = XWD_TRUE_COLOR;
00084 bpad = 32;
00085 rgb[0] = 0xFF0000;
00086 rgb[1] = 0xFF00;
00087 rgb[2] = 0xFF;
00088 break;
00089 case PIX_FMT_RGB565LE:
00090 case PIX_FMT_RGB565BE:
00091 case PIX_FMT_BGR565LE:
00092 case PIX_FMT_BGR565BE:
00093 if (pix_fmt == PIX_FMT_BGR565LE ||
00094 pix_fmt == PIX_FMT_BGR565BE) {
00095 rgb[0] = 0x1F;
00096 rgb[1] = 0x7E0;
00097 rgb[2] = 0xF800;
00098 } else {
00099 rgb[0] = 0xF800;
00100 rgb[1] = 0x7E0;
00101 rgb[2] = 0x1F;
00102 }
00103 bpp = 16;
00104 vclass = XWD_TRUE_COLOR;
00105 bpad = 16;
00106 break;
00107 case PIX_FMT_RGB555LE:
00108 case PIX_FMT_RGB555BE:
00109 case PIX_FMT_BGR555LE:
00110 case PIX_FMT_BGR555BE:
00111 if (pix_fmt == PIX_FMT_BGR555LE ||
00112 pix_fmt == PIX_FMT_BGR555BE) {
00113 rgb[0] = 0x1F;
00114 rgb[1] = 0x3E0;
00115 rgb[2] = 0x7C00;
00116 } else {
00117 rgb[0] = 0x7C00;
00118 rgb[1] = 0x3E0;
00119 rgb[2] = 0x1F;
00120 }
00121 bpp = 16;
00122 vclass = XWD_TRUE_COLOR;
00123 bpad = 16;
00124 break;
00125 case PIX_FMT_RGB8:
00126 case PIX_FMT_BGR8:
00127 case PIX_FMT_RGB4_BYTE:
00128 case PIX_FMT_BGR4_BYTE:
00129 case PIX_FMT_PAL8:
00130 bpp = 8;
00131 vclass = XWD_PSEUDO_COLOR;
00132 bpad = 8;
00133 ncolors = 256;
00134 break;
00135 case PIX_FMT_GRAY8:
00136 bpp = 8;
00137 bpad = 8;
00138 vclass = XWD_STATIC_GRAY;
00139 break;
00140 case PIX_FMT_MONOWHITE:
00141 be = 1;
00142 bitorder = 1;
00143 bpp = 1;
00144 bpad = 8;
00145 vclass = XWD_STATIC_GRAY;
00146 break;
00147 default:
00148 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00149 return AVERROR(EINVAL);
00150 }
00151
00152 lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
00153 header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
00154 out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
00155
00156 if ((ret = ff_alloc_packet2(avctx, pkt, out_size)) < 0)
00157 return ret;
00158 buf = pkt->data;
00159
00160 avctx->coded_frame->key_frame = 1;
00161 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00162
00163 bytestream_put_be32(&buf, header_size);
00164 bytestream_put_be32(&buf, XWD_VERSION);
00165 bytestream_put_be32(&buf, XWD_Z_PIXMAP);
00166 bytestream_put_be32(&buf, pixdepth);
00167 bytestream_put_be32(&buf, avctx->width);
00168 bytestream_put_be32(&buf, avctx->height);
00169 bytestream_put_be32(&buf, 0);
00170 bytestream_put_be32(&buf, be);
00171 bytestream_put_be32(&buf, 32);
00172 bytestream_put_be32(&buf, bitorder);
00173 bytestream_put_be32(&buf, bpad);
00174 bytestream_put_be32(&buf, bpp);
00175 bytestream_put_be32(&buf, lsize);
00176 bytestream_put_be32(&buf, vclass);
00177 bytestream_put_be32(&buf, rgb[0]);
00178 bytestream_put_be32(&buf, rgb[1]);
00179 bytestream_put_be32(&buf, rgb[2]);
00180 bytestream_put_be32(&buf, 8);
00181 bytestream_put_be32(&buf, ncolors);
00182 bytestream_put_be32(&buf, ncolors);
00183 bytestream_put_be32(&buf, avctx->width);
00184 bytestream_put_be32(&buf, avctx->height);
00185 bytestream_put_be32(&buf, 0);
00186 bytestream_put_be32(&buf, 0);
00187 bytestream_put_be32(&buf, 0);
00188 bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
00189
00190 for (i = 0; i < ncolors; i++) {
00191 uint32_t val;
00192 uint8_t red, green, blue;
00193
00194 val = AV_RN32A(p->data[1] + i * 4);
00195 red = (val >> 16) & 0xFF;
00196 green = (val >> 8) & 0xFF;
00197 blue = val & 0xFF;
00198
00199 bytestream_put_be32(&buf, i);
00200 bytestream_put_be16(&buf, red << 8);
00201 bytestream_put_be16(&buf, green << 8);
00202 bytestream_put_be16(&buf, blue << 8);
00203 bytestream_put_byte(&buf, 0x7);
00204 bytestream_put_byte(&buf, 0);
00205 }
00206
00207 ptr = p->data[0];
00208 for (i = 0; i < avctx->height; i++) {
00209 bytestream_put_buffer(&buf, ptr, lsize);
00210 ptr += p->linesize[0];
00211 }
00212
00213 pkt->flags |= AV_PKT_FLAG_KEY;
00214 *got_packet = 1;
00215 return 0;
00216 }
00217
00218 static av_cold int xwd_encode_close(AVCodecContext *avctx)
00219 {
00220 av_freep(&avctx->coded_frame);
00221
00222 return 0;
00223 }
00224
00225 AVCodec ff_xwd_encoder = {
00226 .name = "xwd",
00227 .type = AVMEDIA_TYPE_VIDEO,
00228 .id = AV_CODEC_ID_XWD,
00229 .init = xwd_encode_init,
00230 .encode2 = xwd_encode_frame,
00231 .close = xwd_encode_close,
00232 .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_BGRA,
00233 PIX_FMT_RGBA,
00234 PIX_FMT_ARGB,
00235 PIX_FMT_ABGR,
00236 PIX_FMT_RGB24,
00237 PIX_FMT_BGR24,
00238 PIX_FMT_RGB565BE,
00239 PIX_FMT_RGB565LE,
00240 PIX_FMT_BGR565BE,
00241 PIX_FMT_BGR565LE,
00242 PIX_FMT_RGB555BE,
00243 PIX_FMT_RGB555LE,
00244 PIX_FMT_BGR555BE,
00245 PIX_FMT_BGR555LE,
00246 PIX_FMT_RGB8,
00247 PIX_FMT_BGR8,
00248 PIX_FMT_RGB4_BYTE,
00249 PIX_FMT_BGR4_BYTE,
00250 PIX_FMT_PAL8,
00251 PIX_FMT_GRAY8,
00252 PIX_FMT_MONOWHITE,
00253 PIX_FMT_NONE },
00254 .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
00255 };