avfilter: more correct printf format specifiers
[libav.git] / libavcodec / xwddec.c
CommitLineData
27ed027b
PM
1/*
2 * XWD image format
3 *
4 * Copyright (c) 2012 Paul B Mahol
5 *
6 * This file is part of Libav.
7 *
8 * Libav 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.
12 *
13 * Libav 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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23#include "libavutil/imgutils.h"
24#include "avcodec.h"
25#include "bytestream.h"
594d4d5d 26#include "internal.h"
27ed027b
PM
27#include "xwd.h"
28
27ed027b 29static int xwd_decode_frame(AVCodecContext *avctx, void *data,
df9b9567 30 int *got_frame, AVPacket *avpkt)
27ed027b 31{
759001c5 32 AVFrame *p = data;
27ed027b
PM
33 const uint8_t *buf = avpkt->data;
34 int i, ret, buf_size = avpkt->size;
35 uint32_t version, header_size, vclass, ncolors;
36 uint32_t xoffset, be, bpp, lsize, rsize;
37 uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
38 uint32_t rgb[3];
39 uint8_t *ptr;
abe3c697 40 GetByteContext gb;
27ed027b
PM
41
42 if (buf_size < XWD_HEADER_SIZE)
43 return AVERROR_INVALIDDATA;
44
abe3c697
RB
45 bytestream2_init(&gb, buf, buf_size);
46 header_size = bytestream2_get_be32u(&gb);
27ed027b 47
abe3c697 48 version = bytestream2_get_be32u(&gb);
27ed027b
PM
49 if (version != XWD_VERSION) {
50 av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
51 return AVERROR_INVALIDDATA;
52 }
53
abe3c697 54 if (buf_size < header_size || header_size < XWD_HEADER_SIZE) {
27ed027b
PM
55 av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
56 return AVERROR_INVALIDDATA;
57 }
58
abe3c697
RB
59 pixformat = bytestream2_get_be32u(&gb);
60 pixdepth = bytestream2_get_be32u(&gb);
61 avctx->width = bytestream2_get_be32u(&gb);
62 avctx->height = bytestream2_get_be32u(&gb);
63 xoffset = bytestream2_get_be32u(&gb);
64 be = bytestream2_get_be32u(&gb);
65 bunit = bytestream2_get_be32u(&gb);
66 bitorder = bytestream2_get_be32u(&gb);
67 bpad = bytestream2_get_be32u(&gb);
68 bpp = bytestream2_get_be32u(&gb);
69 lsize = bytestream2_get_be32u(&gb);
70 vclass = bytestream2_get_be32u(&gb);
71 rgb[0] = bytestream2_get_be32u(&gb);
72 rgb[1] = bytestream2_get_be32u(&gb);
73 rgb[2] = bytestream2_get_be32u(&gb);
74 bytestream2_skipu(&gb, 8);
75 ncolors = bytestream2_get_be32u(&gb);
76 bytestream2_skipu(&gb, header_size - (XWD_HEADER_SIZE - 20));
27ed027b
PM
77
78 av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n",
79 pixformat, pixdepth, bunit, bitorder, bpad);
80 av_log(avctx, AV_LOG_DEBUG, "vclass %d, ncolors %d, bpp %d, be %d, lsize %d, xoffset %d\n",
81 vclass, ncolors, bpp, be, lsize, xoffset);
82 av_log(avctx, AV_LOG_DEBUG, "red %0x, green %0x, blue %0x\n", rgb[0], rgb[1], rgb[2]);
83
84 if (pixformat > XWD_Z_PIXMAP) {
85 av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
86 return AVERROR_INVALIDDATA;
87 }
88
89 if (pixdepth == 0 || pixdepth > 32) {
90 av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
91 return AVERROR_INVALIDDATA;
92 }
93
94 if (xoffset) {
6d97484d 95 avpriv_request_sample(avctx, "xoffset %d", xoffset);
27ed027b
PM
96 return AVERROR_PATCHWELCOME;
97 }
98
99 if (be > 1) {
100 av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
101 return AVERROR_INVALIDDATA;
102 }
103
104 if (bitorder > 1) {
105 av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
106 return AVERROR_INVALIDDATA;
107 }
108
109 if (bunit != 8 && bunit != 16 && bunit != 32) {
110 av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
111 return AVERROR_INVALIDDATA;
112 }
113
114 if (bpad != 8 && bpad != 16 && bpad != 32) {
115 av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
116 return AVERROR_INVALIDDATA;
117 }
118
119 if (bpp == 0 || bpp > 32) {
120 av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
121 return AVERROR_INVALIDDATA;
122 }
123
124 if (ncolors > 256) {
125 av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
126 return AVERROR_INVALIDDATA;
127 }
128
129 if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
130 return ret;
131
132 rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
133 if (lsize < rsize) {
134 av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
135 return AVERROR_INVALIDDATA;
136 }
137
abe3c697 138 if (bytestream2_get_bytes_left(&gb) < ncolors * XWD_CMAP_SIZE + avctx->height * lsize) {
27ed027b
PM
139 av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
140 return AVERROR_INVALIDDATA;
141 }
142
143 if (pixformat != XWD_Z_PIXMAP) {
144 av_log(avctx, AV_LOG_ERROR, "pixmap format %d unsupported\n", pixformat);
145 return AVERROR_PATCHWELCOME;
146 }
147
716d413c 148 avctx->pix_fmt = AV_PIX_FMT_NONE;
27ed027b
PM
149 switch (vclass) {
150 case XWD_STATIC_GRAY:
151 case XWD_GRAY_SCALE:
152 if (bpp != 1)
153 return AVERROR_INVALIDDATA;
154 if (pixdepth == 1)
716d413c 155 avctx->pix_fmt = AV_PIX_FMT_MONOWHITE;
27ed027b
PM
156 break;
157 case XWD_STATIC_COLOR:
158 case XWD_PSEUDO_COLOR:
159 if (bpp == 8)
716d413c 160 avctx->pix_fmt = AV_PIX_FMT_PAL8;
27ed027b
PM
161 break;
162 case XWD_TRUE_COLOR:
163 case XWD_DIRECT_COLOR:
164 if (bpp != 16 && bpp != 24 && bpp != 32)
165 return AVERROR_INVALIDDATA;
166 if (bpp == 16 && pixdepth == 15) {
167 if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
716d413c 168 avctx->pix_fmt = be ? AV_PIX_FMT_RGB555BE : AV_PIX_FMT_RGB555LE;
27ed027b 169 else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
716d413c 170 avctx->pix_fmt = be ? AV_PIX_FMT_BGR555BE : AV_PIX_FMT_BGR555LE;
27ed027b
PM
171 } else if (bpp == 16 && pixdepth == 16) {
172 if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
716d413c 173 avctx->pix_fmt = be ? AV_PIX_FMT_RGB565BE : AV_PIX_FMT_RGB565LE;
27ed027b 174 else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
716d413c 175 avctx->pix_fmt = be ? AV_PIX_FMT_BGR565BE : AV_PIX_FMT_BGR565LE;
27ed027b
PM
176 } else if (bpp == 24) {
177 if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
716d413c 178 avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
27ed027b 179 else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
716d413c 180 avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
27ed027b
PM
181 } else if (bpp == 32) {
182 if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
716d413c 183 avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
27ed027b 184 else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
716d413c 185 avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
27ed027b 186 }
abe3c697 187 bytestream2_skipu(&gb, ncolors * XWD_CMAP_SIZE);
27ed027b
PM
188 break;
189 default:
190 av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
191 return AVERROR_INVALIDDATA;
192 }
193
716d413c 194 if (avctx->pix_fmt == AV_PIX_FMT_NONE) {
6d97484d
DB
195 avpriv_request_sample(avctx,
196 "Unknown file: bpp %d, pixdepth %d, vclass %d",
197 bpp, pixdepth, vclass);
27ed027b
PM
198 return AVERROR_PATCHWELCOME;
199 }
200
759001c5 201 if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
27ed027b
PM
202 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
203 return ret;
204 }
205
206 p->key_frame = 1;
207 p->pict_type = AV_PICTURE_TYPE_I;
208
716d413c 209 if (avctx->pix_fmt == AV_PIX_FMT_PAL8) {
27ed027b
PM
210 uint32_t *dst = (uint32_t *)p->data[1];
211 uint8_t red, green, blue;
212
213 for (i = 0; i < ncolors; i++) {
214
abe3c697
RB
215 bytestream2_skipu(&gb, 4); // skip colormap entry number
216 red = bytestream2_get_byteu(&gb);
217 bytestream2_skipu(&gb, 1);
218 green = bytestream2_get_byteu(&gb);
219 bytestream2_skipu(&gb, 1);
220 blue = bytestream2_get_byteu(&gb);
221 bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
27ed027b
PM
222
223 dst[i] = red << 16 | green << 8 | blue;
224 }
225 }
226
227 ptr = p->data[0];
228 for (i = 0; i < avctx->height; i++) {
abe3c697
RB
229 bytestream2_get_bufferu(&gb, ptr, rsize);
230 bytestream2_skipu(&gb, lsize - rsize);
27ed027b
PM
231 ptr += p->linesize[0];
232 }
233
df9b9567 234 *got_frame = 1;
27ed027b
PM
235
236 return buf_size;
237}
238
27ed027b
PM
239AVCodec ff_xwd_decoder = {
240 .name = "xwd",
b2bed932 241 .long_name = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
27ed027b 242 .type = AVMEDIA_TYPE_VIDEO,
36ef5369 243 .id = AV_CODEC_ID_XWD,
27ed027b
PM
244 .decode = xwd_decode_frame,
245 .capabilities = CODEC_CAP_DR1,
27ed027b 246};