4 * Copyright (c) 2012 Paul B Mahol
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include "libavutil/intreadwrite.h"
24 #include "libavutil/pixdesc.h"
26 #include "bytestream.h"
30 #define WINDOW_NAME "lavcxwdenc"
31 #define WINDOW_NAME_SIZE 11
33 static int xwd_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
34 const AVFrame *pict, int *got_packet)
36 enum AVPixelFormat pix_fmt = avctx->pix_fmt;
37 const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pix_fmt);
38 uint32_t pixdepth, bpp, bpad, ncolors = 0, lsize, vclass, be = 0;
39 uint32_t rgb[3] = { 0 }, bitorder = 0;
43 AVFrame * const p = (AVFrame *)pict;
45 pixdepth = av_get_bits_per_pixel(desc);
46 if (desc->flags & AV_PIX_FMT_FLAG_BE)
53 if (pix_fmt == AV_PIX_FMT_ARGB ||
54 pix_fmt == AV_PIX_FMT_ABGR)
56 if (pix_fmt == AV_PIX_FMT_ABGR ||
57 pix_fmt == AV_PIX_FMT_RGBA) {
68 vclass = XWD_TRUE_COLOR;
71 case AV_PIX_FMT_BGR24:
72 case AV_PIX_FMT_RGB24:
73 if (pix_fmt == AV_PIX_FMT_RGB24)
76 vclass = XWD_TRUE_COLOR;
82 case AV_PIX_FMT_RGB565LE:
83 case AV_PIX_FMT_RGB565BE:
84 case AV_PIX_FMT_BGR565LE:
85 case AV_PIX_FMT_BGR565BE:
86 if (pix_fmt == AV_PIX_FMT_BGR565LE ||
87 pix_fmt == AV_PIX_FMT_BGR565BE) {
97 vclass = XWD_TRUE_COLOR;
100 case AV_PIX_FMT_RGB555LE:
101 case AV_PIX_FMT_RGB555BE:
102 case AV_PIX_FMT_BGR555LE:
103 case AV_PIX_FMT_BGR555BE:
104 if (pix_fmt == AV_PIX_FMT_BGR555LE ||
105 pix_fmt == AV_PIX_FMT_BGR555BE) {
115 vclass = XWD_TRUE_COLOR;
118 case AV_PIX_FMT_RGB8:
119 case AV_PIX_FMT_BGR8:
120 case AV_PIX_FMT_RGB4_BYTE:
121 case AV_PIX_FMT_BGR4_BYTE:
122 case AV_PIX_FMT_PAL8:
124 vclass = XWD_PSEUDO_COLOR;
128 case AV_PIX_FMT_GRAY8:
131 vclass = XWD_STATIC_GRAY;
133 case AV_PIX_FMT_MONOWHITE:
138 vclass = XWD_STATIC_GRAY;
141 av_log(avctx, AV_LOG_ERROR, "unsupported pixel format\n");
142 return AVERROR(EINVAL);
145 lsize = FFALIGN(bpp * avctx->width, bpad) / 8;
146 header_size = XWD_HEADER_SIZE + WINDOW_NAME_SIZE;
147 out_size = header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize;
149 if ((ret = ff_alloc_packet2(avctx, pkt, out_size)) < 0)
154 p->pict_type = AV_PICTURE_TYPE_I;
156 bytestream_put_be32(&buf, header_size);
157 bytestream_put_be32(&buf, XWD_VERSION); // file version
158 bytestream_put_be32(&buf, XWD_Z_PIXMAP); // pixmap format
159 bytestream_put_be32(&buf, pixdepth); // pixmap depth in pixels
160 bytestream_put_be32(&buf, avctx->width); // pixmap width in pixels
161 bytestream_put_be32(&buf, avctx->height); // pixmap height in pixels
162 bytestream_put_be32(&buf, 0); // bitmap x offset
163 bytestream_put_be32(&buf, be); // byte order
164 bytestream_put_be32(&buf, 32); // bitmap unit
165 bytestream_put_be32(&buf, bitorder); // bit-order of image data
166 bytestream_put_be32(&buf, bpad); // bitmap scan-line pad in bits
167 bytestream_put_be32(&buf, bpp); // bits per pixel
168 bytestream_put_be32(&buf, lsize); // bytes per scan-line
169 bytestream_put_be32(&buf, vclass); // visual class
170 bytestream_put_be32(&buf, rgb[0]); // red mask
171 bytestream_put_be32(&buf, rgb[1]); // green mask
172 bytestream_put_be32(&buf, rgb[2]); // blue mask
173 bytestream_put_be32(&buf, 8); // size of each bitmask in bits
174 bytestream_put_be32(&buf, ncolors); // number of colors
175 bytestream_put_be32(&buf, ncolors); // number of entries in color map
176 bytestream_put_be32(&buf, avctx->width); // window width
177 bytestream_put_be32(&buf, avctx->height); // window height
178 bytestream_put_be32(&buf, 0); // window upper left X coordinate
179 bytestream_put_be32(&buf, 0); // window upper left Y coordinate
180 bytestream_put_be32(&buf, 0); // window border width
181 bytestream_put_buffer(&buf, WINDOW_NAME, WINDOW_NAME_SIZE);
183 for (i = 0; i < ncolors; i++) {
185 uint8_t red, green, blue;
187 val = AV_RN32A(p->data[1] + i * 4);
188 red = (val >> 16) & 0xFF;
189 green = (val >> 8) & 0xFF;
192 bytestream_put_be32(&buf, i); // colormap entry number
193 bytestream_put_be16(&buf, red << 8);
194 bytestream_put_be16(&buf, green << 8);
195 bytestream_put_be16(&buf, blue << 8);
196 bytestream_put_byte(&buf, 0x7); // bitmask flag
197 bytestream_put_byte(&buf, 0); // padding
201 for (i = 0; i < avctx->height; i++) {
202 bytestream_put_buffer(&buf, ptr, lsize);
203 ptr += p->linesize[0];
206 pkt->flags |= AV_PKT_FLAG_KEY;
211 AVCodec ff_xwd_encoder = {
213 .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
214 .type = AVMEDIA_TYPE_VIDEO,
215 .id = AV_CODEC_ID_XWD,
216 .encode2 = xwd_encode_frame,
217 .pix_fmts = (const enum AVPixelFormat[]) { AV_PIX_FMT_BGRA,
233 AV_PIX_FMT_RGB4_BYTE,
234 AV_PIX_FMT_BGR4_BYTE,
237 AV_PIX_FMT_MONOWHITE,