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 AVPixelFormat pix_fmt = avctx->pix_fmt;
00046 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
00047 uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
00048 uint32_t rgb[3] = { 0 }, bitorder = 0;
00049 uint32_t header_size;
00050 int i, out_size, ret;
00051 uint8_t *ptr, *buf;
00052
00053 pixdepth = av_get_bits_per_pixel(desc);
00054 if (desc->flags & PIX_FMT_BE)
00055 be = 1;
00056 switch (pix_fmt) {
00057 case AV_PIX_FMT_ARGB:
00058 case AV_PIX_FMT_BGRA:
00059 case AV_PIX_FMT_RGBA:
00060 case AV_PIX_FMT_ABGR:
00061 if (pix_fmt == AV_PIX_FMT_ARGB ||
00062 pix_fmt == AV_PIX_FMT_ABGR)
00063 be = 1;
00064 if (pix_fmt == AV_PIX_FMT_ABGR ||
00065 pix_fmt == AV_PIX_FMT_RGBA) {
00066 rgb[0] = 0xFF;
00067 rgb[1] = 0xFF00;
00068 rgb[2] = 0xFF0000;
00069 } else {
00070 rgb[0] = 0xFF0000;
00071 rgb[1] = 0xFF00;
00072 rgb[2] = 0xFF;
00073 }
00074 bpp = 32;
00075 pixdepth = 24;
00076 vclass = XWD_TRUE_COLOR;
00077 bpad = 32;
00078 break;
00079 case AV_PIX_FMT_BGR24:
00080 case AV_PIX_FMT_RGB24:
00081 if (pix_fmt == AV_PIX_FMT_RGB24)
00082 be = 1;
00083 bpp = 24;
00084 vclass = XWD_TRUE_COLOR;
00085 bpad = 32;
00086 rgb[0] = 0xFF0000;
00087 rgb[1] = 0xFF00;
00088 rgb[2] = 0xFF;
00089 break;
00090 case AV_PIX_FMT_RGB565LE:
00091 case AV_PIX_FMT_RGB565BE:
00092 case AV_PIX_FMT_BGR565LE:
00093 case AV_PIX_FMT_BGR565BE:
00094 if (pix_fmt == AV_PIX_FMT_BGR565LE ||
00095 pix_fmt == AV_PIX_FMT_BGR565BE) {
00096 rgb[0] = 0x1F;
00097 rgb[1] = 0x7E0;
00098 rgb[2] = 0xF800;
00099 } else {
00100 rgb[0] = 0xF800;
00101 rgb[1] = 0x7E0;
00102 rgb[2] = 0x1F;
00103 }
00104 bpp = 16;
00105 vclass = XWD_TRUE_COLOR;
00106 bpad = 16;
00107 break;
00108 case AV_PIX_FMT_RGB555LE:
00109 case AV_PIX_FMT_RGB555BE:
00110 case AV_PIX_FMT_BGR555LE:
00111 case AV_PIX_FMT_BGR555BE:
00112 if (pix_fmt == AV_PIX_FMT_BGR555LE ||
00113 pix_fmt == AV_PIX_FMT_BGR555BE) {
00114 rgb[0] = 0x1F;
00115 rgb[1] = 0x3E0;
00116 rgb[2] = 0x7C00;
00117 } else {
00118 rgb[0] = 0x7C00;
00119 rgb[1] = 0x3E0;
00120 rgb[2] = 0x1F;
00121 }
00122 bpp = 16;
00123 vclass = XWD_TRUE_COLOR;
00124 bpad = 16;
00125 break;
00126 case AV_PIX_FMT_RGB8:
00127 case AV_PIX_FMT_BGR8:
00128 case AV_PIX_FMT_RGB4_BYTE:
00129 case AV_PIX_FMT_BGR4_BYTE:
00130 case AV_PIX_FMT_PAL8:
00131 bpp = 8;
00132 vclass = XWD_PSEUDO_COLOR;
00133 bpad = 8;
00134 ncolors = 256;
00135 break;
00136 case AV_PIX_FMT_GRAY8:
00137 bpp = 8;
00138 bpad = 8;
00139 vclass = XWD_STATIC_GRAY;
00140 break;
00141 case AV_PIX_FMT_MONOWHITE:
00142 be = 1;
00143 bitorder = 1;
00144 bpp = 1;
00145 bpad = 8;
00146 vclass = XWD_STATIC_GRAY;
00147 break;
00148 default:
00149 av_log(avctx, AV_LOG_INFO, "unsupported pixel format\n");
00150 return AVERROR(EINVAL);
00151 }
00152
00153 lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
00154 header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
00155 out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
00156
00157 if ((ret = ff_alloc_packet2(avctx, pkt, out_size)) < 0)
00158 return ret;
00159 buf = pkt->data;
00160
00161 avctx->coded_frame->key_frame = 1;
00162 avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
00163
00164 bytestream_put_be32(&buf, header_size);
00165 bytestream_put_be32(&buf, XWD_VERSION);
00166 bytestream_put_be32(&buf, XWD_Z_PIXMAP);
00167 bytestream_put_be32(&buf, pixdepth);
00168 bytestream_put_be32(&buf, avctx->width);
00169 bytestream_put_be32(&buf, avctx->height);
00170 bytestream_put_be32(&buf, 0);
00171 bytestream_put_be32(&buf, be);
00172 bytestream_put_be32(&buf, 32);
00173 bytestream_put_be32(&buf, bitorder);
00174 bytestream_put_be32(&buf, bpad);
00175 bytestream_put_be32(&buf, bpp);
00176 bytestream_put_be32(&buf, lsize);
00177 bytestream_put_be32(&buf, vclass);
00178 bytestream_put_be32(&buf, rgb[0]);
00179 bytestream_put_be32(&buf, rgb[1]);
00180 bytestream_put_be32(&buf, rgb[2]);
00181 bytestream_put_be32(&buf, 8);
00182 bytestream_put_be32(&buf, ncolors);
00183 bytestream_put_be32(&buf, ncolors);
00184 bytestream_put_be32(&buf, avctx->width);
00185 bytestream_put_be32(&buf, avctx->height);
00186 bytestream_put_be32(&buf, 0);
00187 bytestream_put_be32(&buf, 0);
00188 bytestream_put_be32(&buf, 0);
00189 bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
00190
00191 for (i = 0; i < ncolors; i++) {
00192 uint32_t val;
00193 uint8_t red, green, blue;
00194
00195 val = AV_RN32A(p->data[1] + i * 4);
00196 red = (val >> 16) & 0xFF;
00197 green = (val >> 8) & 0xFF;
00198 blue = val & 0xFF;
00199
00200 bytestream_put_be32(&buf, i);
00201 bytestream_put_be16(&buf, red << 8);
00202 bytestream_put_be16(&buf, green << 8);
00203 bytestream_put_be16(&buf, blue << 8);
00204 bytestream_put_byte(&buf, 0x7);
00205 bytestream_put_byte(&buf, 0);
00206 }
00207
00208 ptr = p->data[0];
00209 for (i = 0; i < avctx->height; i++) {
00210 bytestream_put_buffer(&buf, ptr, lsize);
00211 ptr += p->linesize[0];
00212 }
00213
00214 pkt->flags |= AV_PKT_FLAG_KEY;
00215 *got_packet = 1;
00216 return 0;
00217 }
00218
00219 static av_cold int xwd_encode_close(AVCodecContext *avctx)
00220 {
00221 av_freep(&avctx->coded_frame);
00222
00223 return 0;
00224 }
00225
00226 AVCodec ff_xwd_encoder = {
00227 .name = "xwd",
00228 .type = AVMEDIA_TYPE_VIDEO,
00229 .id = AV_CODEC_ID_XWD,
00230 .init = xwd_encode_init,
00231 .encode2 = xwd_encode_frame,
00232 .close = xwd_encode_close,
00233 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
00234 AV_PIX_FMT_RGBA,
00235 AV_PIX_FMT_ARGB,
00236 AV_PIX_FMT_ABGR,
00237 AV_PIX_FMT_RGB24,
00238 AV_PIX_FMT_BGR24,
00239 AV_PIX_FMT_RGB565BE,
00240 AV_PIX_FMT_RGB565LE,
00241 AV_PIX_FMT_BGR565BE,
00242 AV_PIX_FMT_BGR565LE,
00243 AV_PIX_FMT_RGB555BE,
00244 AV_PIX_FMT_RGB555LE,
00245 AV_PIX_FMT_BGR555BE,
00246 AV_PIX_FMT_BGR555LE,
00247 AV_PIX_FMT_RGB8,
00248 AV_PIX_FMT_BGR8,
00249 AV_PIX_FMT_RGB4_BYTE,
00250 AV_PIX_FMT_BGR4_BYTE,
00251 AV_PIX_FMT_PAL8,
00252 AV_PIX_FMT_GRAY8,
00253 AV_PIX_FMT_MONOWHITE,
00254 AV_PIX_FMT_NONE },
00255 .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
00256 };