]> git.sesse.net Git - ffmpeg/blob - libavcodec/xwddec.c
lcl: return negative error codes on decode_init() errors.
[ffmpeg] / libavcodec / xwddec.c
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"
26 #include "xwd.h"
27
28 static av_cold int xwd_decode_init(AVCodecContext *avctx)
29 {
30     avctx->coded_frame = avcodec_alloc_frame();
31     if (!avctx->coded_frame)
32         return AVERROR(ENOMEM);
33
34     return 0;
35 }
36
37 static int xwd_decode_frame(AVCodecContext *avctx, void *data,
38                             int *data_size, AVPacket *avpkt)
39 {
40     AVFrame *p = avctx->coded_frame;
41     const uint8_t *buf = avpkt->data;
42     int i, ret, buf_size = avpkt->size;
43     uint32_t version, header_size, vclass, ncolors;
44     uint32_t xoffset, be, bpp, lsize, rsize;
45     uint32_t pixformat, pixdepth, bunit, bitorder, bpad;
46     uint32_t rgb[3];
47     uint8_t *ptr;
48
49     if (buf_size < XWD_HEADER_SIZE)
50         return AVERROR_INVALIDDATA;
51
52     header_size = bytestream_get_be32(&buf);
53     if (buf_size < header_size)
54         return AVERROR_INVALIDDATA;
55
56     version = bytestream_get_be32(&buf);
57     if (version != XWD_VERSION) {
58         av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
59         return AVERROR_INVALIDDATA;
60     }
61
62     if (header_size < XWD_HEADER_SIZE) {
63         av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
64         return AVERROR_INVALIDDATA;
65     }
66
67     pixformat     = bytestream_get_be32(&buf);
68     pixdepth      = bytestream_get_be32(&buf);
69     avctx->width  = bytestream_get_be32(&buf);
70     avctx->height = bytestream_get_be32(&buf);
71     xoffset       = bytestream_get_be32(&buf);
72     be            = bytestream_get_be32(&buf);
73     bunit         = bytestream_get_be32(&buf);
74     bitorder      = bytestream_get_be32(&buf);
75     bpad          = bytestream_get_be32(&buf);
76     bpp           = bytestream_get_be32(&buf);
77     lsize         = bytestream_get_be32(&buf);
78     vclass        = bytestream_get_be32(&buf);
79     rgb[0]        = bytestream_get_be32(&buf);
80     rgb[1]        = bytestream_get_be32(&buf);
81     rgb[2]        = bytestream_get_be32(&buf);
82     buf          += 8;
83     ncolors       = bytestream_get_be32(&buf);
84     buf          += header_size - (XWD_HEADER_SIZE - 20);
85
86     av_log(avctx, AV_LOG_DEBUG, "pixformat %d, pixdepth %d, bunit %d, bitorder %d, bpad %d\n",
87            pixformat, pixdepth, bunit, bitorder, bpad);
88     av_log(avctx, AV_LOG_DEBUG, "vclass %d, ncolors %d, bpp %d, be %d, lsize %d, xoffset %d\n",
89            vclass, ncolors, bpp, be, lsize, xoffset);
90     av_log(avctx, AV_LOG_DEBUG, "red %0x, green %0x, blue %0x\n", rgb[0], rgb[1], rgb[2]);
91
92     if (pixformat > XWD_Z_PIXMAP) {
93         av_log(avctx, AV_LOG_ERROR, "invalid pixmap format\n");
94         return AVERROR_INVALIDDATA;
95     }
96
97     if (pixdepth == 0 || pixdepth > 32) {
98         av_log(avctx, AV_LOG_ERROR, "invalid pixmap depth\n");
99         return AVERROR_INVALIDDATA;
100     }
101
102     if (xoffset) {
103         av_log_ask_for_sample(avctx, "unsupported xoffset %d\n", xoffset);
104         return AVERROR_PATCHWELCOME;
105     }
106
107     if (be > 1) {
108         av_log(avctx, AV_LOG_ERROR, "invalid byte order\n");
109         return AVERROR_INVALIDDATA;
110     }
111
112     if (bitorder > 1) {
113         av_log(avctx, AV_LOG_ERROR, "invalid bitmap bit order\n");
114         return AVERROR_INVALIDDATA;
115     }
116
117     if (bunit != 8 && bunit != 16 && bunit != 32) {
118         av_log(avctx, AV_LOG_ERROR, "invalid bitmap unit\n");
119         return AVERROR_INVALIDDATA;
120     }
121
122     if (bpad != 8 && bpad != 16 && bpad != 32) {
123         av_log(avctx, AV_LOG_ERROR, "invalid bitmap scan-line pad\n");
124         return AVERROR_INVALIDDATA;
125     }
126
127     if (bpp == 0 || bpp > 32) {
128         av_log(avctx, AV_LOG_ERROR, "invalid bits per pixel\n");
129         return AVERROR_INVALIDDATA;
130     }
131
132     if (ncolors > 256) {
133         av_log(avctx, AV_LOG_ERROR, "invalid number of entries in colormap\n");
134         return AVERROR_INVALIDDATA;
135     }
136
137     if ((ret = av_image_check_size(avctx->width, avctx->height, 0, NULL)) < 0)
138         return ret;
139
140     rsize = FFALIGN(avctx->width * bpp, bpad) / 8;
141     if (lsize < rsize) {
142         av_log(avctx, AV_LOG_ERROR, "invalid bytes per scan-line\n");
143         return AVERROR_INVALIDDATA;
144     }
145
146     if (buf_size < header_size + ncolors * XWD_CMAP_SIZE + avctx->height * lsize) {
147         av_log(avctx, AV_LOG_ERROR, "input buffer too small\n");
148         return AVERROR_INVALIDDATA;
149     }
150
151     if (pixformat != XWD_Z_PIXMAP) {
152         av_log(avctx, AV_LOG_ERROR, "pixmap format %d unsupported\n", pixformat);
153         return AVERROR_PATCHWELCOME;
154     }
155
156     avctx->pix_fmt = PIX_FMT_NONE;
157     switch (vclass) {
158     case XWD_STATIC_GRAY:
159     case XWD_GRAY_SCALE:
160         if (bpp != 1)
161             return AVERROR_INVALIDDATA;
162         if (pixdepth == 1)
163             avctx->pix_fmt = PIX_FMT_MONOWHITE;
164         break;
165     case XWD_STATIC_COLOR:
166     case XWD_PSEUDO_COLOR:
167         if (bpp == 8)
168             avctx->pix_fmt = PIX_FMT_PAL8;
169         break;
170     case XWD_TRUE_COLOR:
171     case XWD_DIRECT_COLOR:
172         if (bpp != 16 && bpp != 24 && bpp != 32)
173             return AVERROR_INVALIDDATA;
174         if (bpp == 16 && pixdepth == 15) {
175             if (rgb[0] == 0x7C00 && rgb[1] == 0x3E0 && rgb[2] == 0x1F)
176                 avctx->pix_fmt = be ? PIX_FMT_RGB555BE : PIX_FMT_RGB555LE;
177             else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
178                 avctx->pix_fmt = be ? PIX_FMT_BGR555BE : PIX_FMT_BGR555LE;
179         } else if (bpp == 16 && pixdepth == 16) {
180             if (rgb[0] == 0xF800 && rgb[1] == 0x7E0 && rgb[2] == 0x1F)
181                 avctx->pix_fmt = be ? PIX_FMT_RGB565BE : PIX_FMT_RGB565LE;
182             else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
183                 avctx->pix_fmt = be ? PIX_FMT_BGR565BE : PIX_FMT_BGR565LE;
184         } else if (bpp == 24) {
185             if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
186                 avctx->pix_fmt = be ? PIX_FMT_RGB24 : PIX_FMT_BGR24;
187             else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
188                 avctx->pix_fmt = be ? PIX_FMT_BGR24 : PIX_FMT_RGB24;
189         } else if (bpp == 32) {
190             if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
191                 avctx->pix_fmt = be ? PIX_FMT_ARGB : PIX_FMT_BGRA;
192             else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
193                 avctx->pix_fmt = be ? PIX_FMT_ABGR : PIX_FMT_RGBA;
194         }
195         buf += ncolors * XWD_CMAP_SIZE;
196         break;
197     default:
198         av_log(avctx, AV_LOG_ERROR, "invalid visual class\n");
199         return AVERROR_INVALIDDATA;
200     }
201
202     if (avctx->pix_fmt == PIX_FMT_NONE) {
203         av_log_ask_for_sample(avctx, "unknown file: bpp %d, pixdepth %d, vclass %d\n", bpp, pixdepth, vclass);
204         return AVERROR_PATCHWELCOME;
205     }
206
207     if (p->data[0])
208         avctx->release_buffer(avctx, p);
209
210     p->reference = 0;
211     if ((ret = avctx->get_buffer(avctx, p)) < 0) {
212         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
213         return ret;
214     }
215
216     p->key_frame = 1;
217     p->pict_type = AV_PICTURE_TYPE_I;
218
219     if (avctx->pix_fmt == PIX_FMT_PAL8) {
220         uint32_t *dst = (uint32_t *)p->data[1];
221         uint8_t red, green, blue;
222
223         for (i = 0; i < ncolors; i++) {
224
225             buf   += 4;  // skip colormap entry number
226             red    = *buf; buf += 2;
227             green  = *buf; buf += 2;
228             blue   = *buf; buf += 2;
229             buf   += 2;  // skip bitmask flag and padding
230
231             dst[i] = red << 16 | green << 8 | blue;
232         }
233     }
234
235     ptr = p->data[0];
236     for (i = 0; i < avctx->height; i++) {
237         bytestream_get_buffer(&buf, ptr, rsize);
238         buf += lsize - rsize;
239         ptr += p->linesize[0];
240     }
241
242     *data_size = sizeof(AVFrame);
243     *(AVFrame *)data = *p;
244
245     return buf_size;
246 }
247
248 static av_cold int xwd_decode_close(AVCodecContext *avctx)
249 {
250     if (avctx->coded_frame->data[0])
251         avctx->release_buffer(avctx, avctx->coded_frame);
252
253     av_freep(&avctx->coded_frame);
254
255     return 0;
256 }
257
258 AVCodec ff_xwd_decoder = {
259     .name           = "xwd",
260     .type           = AVMEDIA_TYPE_VIDEO,
261     .id             = CODEC_ID_XWD,
262     .init           = xwd_decode_init,
263     .close          = xwd_decode_close,
264     .decode         = xwd_decode_frame,
265     .capabilities   = CODEC_CAP_DR1,
266     .long_name      = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
267 };