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_MONOWHITE:
00136 be = 1;
00137 bitorder = 1;
00138 bpp = 1;
00139 bpad = 8;
00140 vclass = XWD_STATIC_GRAY;
00141 break;
00142 default:
00143 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00144 return AVERROR(EINVAL);
00145 }
00146
00147 lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
00148 header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
00149 out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
00150
00151 if ((ret = ff_alloc_packet2(avctx, pkt, out_size)) < 0)
00152 return ret;
00153 buf = pkt->data;
00154
00155 avctx->coded_frame->key_frame = 1;
00156 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00157
00158 bytestream_put_be32(&buf, header_size);
00159 bytestream_put_be32(&buf, XWD_VERSION);
00160 bytestream_put_be32(&buf, XWD_Z_PIXMAP);
00161 bytestream_put_be32(&buf, pixdepth);
00162 bytestream_put_be32(&buf, avctx->width);
00163 bytestream_put_be32(&buf, avctx->height);
00164 bytestream_put_be32(&buf, 0);
00165 bytestream_put_be32(&buf, be);
00166 bytestream_put_be32(&buf, 32);
00167 bytestream_put_be32(&buf, bitorder);
00168 bytestream_put_be32(&buf, bpad);
00169 bytestream_put_be32(&buf, bpp);
00170 bytestream_put_be32(&buf, lsize);
00171 bytestream_put_be32(&buf, vclass);
00172 bytestream_put_be32(&buf, rgb[0]);
00173 bytestream_put_be32(&buf, rgb[1]);
00174 bytestream_put_be32(&buf, rgb[2]);
00175 bytestream_put_be32(&buf, 8);
00176 bytestream_put_be32(&buf, ncolors);
00177 bytestream_put_be32(&buf, ncolors);
00178 bytestream_put_be32(&buf, avctx->width);
00179 bytestream_put_be32(&buf, avctx->height);
00180 bytestream_put_be32(&buf, 0);
00181 bytestream_put_be32(&buf, 0);
00182 bytestream_put_be32(&buf, 0);
00183 bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
00184
00185 for (i = 0; i < ncolors; i++) {
00186 uint32_t val;
00187 uint8_t red, green, blue;
00188
00189 val = AV_RN32A(p->data[1] + i * 4);
00190 red = (val >> 16) & 0xFF;
00191 green = (val >> 8) & 0xFF;
00192 blue = val & 0xFF;
00193
00194 bytestream_put_be32(&buf, i);
00195 bytestream_put_be16(&buf, red << 8);
00196 bytestream_put_be16(&buf, green << 8);
00197 bytestream_put_be16(&buf, blue << 8);
00198 bytestream_put_byte(&buf, 0x7);
00199 bytestream_put_byte(&buf, 0);
00200 }
00201
00202 ptr = p->data[0];
00203 for (i = 0; i < avctx->height; i++) {
00204 bytestream_put_buffer(&buf, ptr, lsize);
00205 ptr += p->linesize[0];
00206 }
00207
00208 pkt->flags |= AV_PKT_FLAG_KEY;
00209 *got_packet = 1;
00210 return 0;
00211 }
00212
00213 static av_cold int xwd_encode_close(AVCodecContext *avctx)
00214 {
00215 av_freep(&avctx->coded_frame);
00216
00217 return 0;
00218 }
00219
00220 AVCodec ff_xwd_encoder = {
00221 .name = "xwd",
00222 .type = AVMEDIA_TYPE_VIDEO,
00223 .id = CODEC_ID_XWD,
00224 .init = xwd_encode_init,
00225 .encode2 = xwd_encode_frame,
00226 .close = xwd_encode_close,
00227 .pix_fmts = (const enum PixelFormat[]) { PIX_FMT_BGRA,
00228 PIX_FMT_RGBA,
00229 PIX_FMT_ARGB,
00230 PIX_FMT_ABGR,
00231 PIX_FMT_RGB24,
00232 PIX_FMT_BGR24,
00233 PIX_FMT_RGB565BE,
00234 PIX_FMT_RGB565LE,
00235 PIX_FMT_BGR565BE,
00236 PIX_FMT_BGR565LE,
00237 PIX_FMT_RGB555BE,
00238 PIX_FMT_RGB555LE,
00239 PIX_FMT_BGR555BE,
00240 PIX_FMT_BGR555LE,
00241 PIX_FMT_RGB8,
00242 PIX_FMT_BGR8,
00243 PIX_FMT_RGB4_BYTE,
00244 PIX_FMT_BGR4_BYTE,
00245 PIX_FMT_PAL8,
00246 PIX_FMT_MONOWHITE,
00247 PIX_FMT_NONE },
00248 .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
00249 };