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/imgutils.h"
25 #include "bytestream.h"
29 static av_cold int xwd_decode_init(AVCodecContext *avctx)
31 avctx->coded_frame = avcodec_alloc_frame();
32 if (!avctx->coded_frame)
33 return AVERROR(ENOMEM);
38 static int xwd_decode_frame(AVCodecContext *avctx, void *data,
39 int *got_frame, AVPacket *avpkt)
41 AVFrame *p = avctx->coded_frame;
42 const uint8_t *buf = avpkt->data;
43 int i, ret, buf_size = avpkt->size;
44 uint32_t version, header_size, vclass, ncolors;
45 uint32_t xoffset, be, bpp, lsize, rsize;
46 uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
51 if (buf_size < XWD_HEADER_SIZE)
52 return AVERROR_INVALIDDATA;
54 bytestream2_init(&gb, buf, buf_size);
55 header_size = bytestream2_get_be32u(&gb);
57 version = bytestream2_get_be32u(&gb);
58 if (version != XWD_VERSION) {
59 av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
60 return AVERROR_INVALIDDATA;
63 if (buf_size < header_size || header_size < XWD_HEADER_SIZE) {
64 av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
65 return AVERROR_INVALIDDATA;
68 pixformat = bytestream2_get_be32u(&gb);
69 pixdepth = bytestream2_get_be32u(&gb);
70 avctx->width = bytestream2_get_be32u(&gb);
71 avctx->height = bytestream2_get_be32u(&gb);
72 xoffset = bytestream2_get_be32u(&gb);
73 be = bytestream2_get_be32u(&gb);
74 bunit = bytestream2_get_be32u(&gb);
75 bitorder = bytestream2_get_be32u(&gb);
76 bpad = bytestream2_get_be32u(&gb);
77 bpp = bytestream2_get_be32u(&gb);
78 lsize = bytestream2_get_be32u(&gb);
79 vclass = bytestream2_get_be32u(&gb);
80 rgb[0] = bytestream2_get_be32u(&gb);
81 rgb[1] = bytestream2_get_be32u(&gb);
82 rgb[2] = bytestream2_get_be32u(&gb);
83 bytestream2_skipu(&gb, 8);
84 ncolors = bytestream2_get_be32u(&gb);
85 bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
87 av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n",
88 pixformat, pixdepth, bunit, bitorder, bpad);
89 av_log(avctx, AV_LOG_DEBUG, "vclass %d, ncolors %d, bpp %d, be %d, lsize %d, xoffset %d\n",
90 vclass, ncolors, bpp, be, lsize, xoffset);
91 av_log(avctx, AV_LOG_DEBUG, "red %0x, green %0x, blue %0x\n", rgb[0], rgb[1], rgb[2]);
93 if (pixformat > XWD_Z_PIXMAP) {
94 av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
95 return AVERROR_INVALIDDATA;
98 if (pixdepth == 0 || pixdepth > 32) {
99 av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
100 return AVERROR_INVALIDDATA;
104 av_log_ask_for_sample(avctx, "unsupported xoffset %d\n", xoffset);
105 return AVERROR_PATCHWELCOME;
109 av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
110 return AVERROR_INVALIDDATA;
114 av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
115 return AVERROR_INVALIDDATA;
118 if (bunit != 8 && bunit != 16 && bunit != 32) {
119 av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
120 return AVERROR_INVALIDDATA;
123 if (bpad != 8 && bpad != 16 && bpad != 32) {
124 av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
125 return AVERROR_INVALIDDATA;
128 if (bpp == 0 || bpp > 32) {
129 av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
130 return AVERROR_INVALIDDATA;
134 av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
135 return AVERROR_INVALIDDATA;
138 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
141 rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
143 av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
144 return AVERROR_INVALIDDATA;
147 if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + avctx->height * lsize) {
148 av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
149 return AVERROR_INVALIDDATA;
152 if (pixformat != XWD_Z_PIXMAP) {
153 av_log(avctx, AV_LOG_ERROR, "pixmap format %d unsupported\n", pixformat);
154 return AVERROR_PATCHWELCOME;
157 avctx->pix_fmt = AV_PIX_FMT_NONE;
159 case XWD_STATIC_GRAY:
161 if (bpp != 1 && bpp != 8)
162 return AVERROR_INVALIDDATA;
164 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
165 } else if (pixdepth == 8) {
166 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
169 case XWD_STATIC_COLOR:
170 case XWD_PSEUDO_COLOR:
172 avctx->pix_fmt = AV_PIX_FMT_PAL8;
175 case XWD_DIRECT_COLOR:
176 if (bpp != 16 && bpp != 24 && bpp != 32)
177 return AVERROR_INVALIDDATA;
178 if (bpp == 16 && pixdepth == 15) {
179 if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
180 avctx->pix_fmt = be ? AV_PIX_FMT_RGB555BE : AV_PIX_FMT_RGB555LE;
181 else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
182 avctx->pix_fmt = be ? AV_PIX_FMT_BGR555BE : AV_PIX_FMT_BGR555LE;
183 } else if (bpp == 16 && pixdepth == 16) {
184 if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
185 avctx->pix_fmt = be ? AV_PIX_FMT_RGB565BE : AV_PIX_FMT_RGB565LE;
186 else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
187 avctx->pix_fmt = be ? AV_PIX_FMT_BGR565BE : AV_PIX_FMT_BGR565LE;
188 } else if (bpp == 24) {
189 if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
190 avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
191 else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
192 avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
193 } else if (bpp == 32) {
194 if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
195 avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
196 else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
197 avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
199 bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
202 av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
203 return AVERROR_INVALIDDATA;
206 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
207 av_log_ask_for_sample(avctx, "unknown file: bpp %d, pixdepth %d, vclass %d\n", bpp, pixdepth, vclass);
208 return AVERROR_PATCHWELCOME;
212 avctx->release_buffer(avctx, p);
215 if ((ret = ff_get_buffer(avctx, p)) < 0) {
216 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
221 p->pict_type = AV_PICTURE_TYPE_I;
223 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
224 uint32_t *dst = (uint32_t *)p->data[1];
225 uint8_t red, green, blue;
227 for (i = 0; i < ncolors; i++) {
229 bytestream2_skipu(&gb, 4); // skip colormap entry number
230 red = bytestream2_get_byteu(&gb);
231 bytestream2_skipu(&gb, 1);
232 green = bytestream2_get_byteu(&gb);
233 bytestream2_skipu(&gb, 1);
234 blue = bytestream2_get_byteu(&gb);
235 bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
237 dst[i] = red << 16 | green << 8 | blue;
242 for (i = 0; i < avctx->height; i++) {
243 bytestream2_get_bufferu(&gb, ptr, rsize);
244 bytestream2_skipu(&gb, lsize - rsize);
245 ptr += p->linesize[0];
249 *(AVFrame *)data = *p;
254 static av_cold int xwd_decode_close(AVCodecContext *avctx)
256 if (avctx->coded_frame->data[0])
257 avctx->release_buffer(avctx, avctx->coded_frame);
259 av_freep(&avctx->coded_frame);
264 AVCodec ff_xwd_decoder = {
266 .type = AVMEDIA_TYPE_VIDEO,
267 .id = AV_CODEC_ID_XWD,
268 .init = xwd_decode_init,
269 .close = xwd_decode_close,
270 .decode = xwd_decode_frame,
271 .capabilities = CODEC_CAP_DR1,
272 .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),