]> git.sesse.net Git - ffmpeg/blob - libavcodec/xwddec.c
aacdec: Drop some unused function arguments
[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     GetByteContext gb;
49
50     if (buf_size < XWD_HEADER_SIZE)
51         return AVERROR_INVALIDDATA;
52
53     bytestream2_init(&gb, buf, buf_size);
54     header_size = bytestream2_get_be32u(&gb);
55
56     version = bytestream2_get_be32u(&gb);
57     if (version != XWD_VERSION) {
58         av_log(avctx, AV_LOG_ERROR, "unsupported version\n");
59         return AVERROR_INVALIDDATA;
60     }
61
62     if (buf_size < header_size || header_size < XWD_HEADER_SIZE) {
63         av_log(avctx, AV_LOG_ERROR, "invalid header size\n");
64         return AVERROR_INVALIDDATA;
65     }
66
67     pixformat     = bytestream2_get_be32u(&gb);
68     pixdepth      = bytestream2_get_be32u(&gb);
69     avctx->width  = bytestream2_get_be32u(&gb);
70     avctx->height = bytestream2_get_be32u(&gb);
71     xoffset       = bytestream2_get_be32u(&gb);
72     be            = bytestream2_get_be32u(&gb);
73     bunit         = bytestream2_get_be32u(&gb);
74     bitorder      = bytestream2_get_be32u(&gb);
75     bpad          = bytestream2_get_be32u(&gb);
76     bpp           = bytestream2_get_be32u(&gb);
77     lsize         = bytestream2_get_be32u(&gb);
78     vclass        = bytestream2_get_be32u(&gb);
79     rgb[0]        = bytestream2_get_be32u(&gb);
80     rgb[1]        = bytestream2_get_be32u(&gb);
81     rgb[2]        = bytestream2_get_be32u(&gb);
82     bytestream2_skipu(&gb, 8);
83     ncolors       = bytestream2_get_be32u(&gb);
84     bytestream2_skipu(&gb, 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 (bytestream2_get_bytes_left(&gb) < 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 = AV_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 = AV_PIX_FMT_MONOWHITE;
164         break;
165     case XWD_STATIC_COLOR:
166     case XWD_PSEUDO_COLOR:
167         if (bpp == 8)
168             avctx->pix_fmt = AV_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 ? AV_PIX_FMT_RGB555BE : AV_PIX_FMT_RGB555LE;
177             else if (rgb[0] == 0x1F && rgb[1] == 0x3E0 && rgb[2] == 0x7C00)
178                 avctx->pix_fmt = be ? AV_PIX_FMT_BGR555BE : AV_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 ? AV_PIX_FMT_RGB565BE : AV_PIX_FMT_RGB565LE;
182             else if (rgb[0] == 0x1F && rgb[1] == 0x7E0 && rgb[2] == 0xF800)
183                 avctx->pix_fmt = be ? AV_PIX_FMT_BGR565BE : AV_PIX_FMT_BGR565LE;
184         } else if (bpp == 24) {
185             if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
186                 avctx->pix_fmt = be ? AV_PIX_FMT_RGB24 : AV_PIX_FMT_BGR24;
187             else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
188                 avctx->pix_fmt = be ? AV_PIX_FMT_BGR24 : AV_PIX_FMT_RGB24;
189         } else if (bpp == 32) {
190             if (rgb[0] == 0xFF0000 && rgb[1] == 0xFF00 && rgb[2] == 0xFF)
191                 avctx->pix_fmt = be ? AV_PIX_FMT_ARGB : AV_PIX_FMT_BGRA;
192             else if (rgb[0] == 0xFF && rgb[1] == 0xFF00 && rgb[2] == 0xFF0000)
193                 avctx->pix_fmt = be ? AV_PIX_FMT_ABGR : AV_PIX_FMT_RGBA;
194         }
195         bytestream2_skipu(&gb, 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 == AV_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 == AV_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             bytestream2_skipu(&gb, 4); // skip colormap entry number
226             red    = bytestream2_get_byteu(&gb);
227             bytestream2_skipu(&gb, 1);
228             green  = bytestream2_get_byteu(&gb);
229             bytestream2_skipu(&gb, 1);
230             blue   = bytestream2_get_byteu(&gb);
231             bytestream2_skipu(&gb, 3); // skip bitmask flag and padding
232
233             dst[i] = red << 16 | green << 8 | blue;
234         }
235     }
236
237     ptr = p->data[0];
238     for (i = 0; i < avctx->height; i++) {
239         bytestream2_get_bufferu(&gb, ptr, rsize);
240         bytestream2_skipu(&gb, lsize - rsize);
241         ptr += p->linesize[0];
242     }
243
244     *data_size = sizeof(AVFrame);
245     *(AVFrame *)data = *p;
246
247     return buf_size;
248 }
249
250 static av_cold int xwd_decode_close(AVCodecContext *avctx)
251 {
252     if (avctx->coded_frame->data[0])
253         avctx->release_buffer(avctx, avctx->coded_frame);
254
255     av_freep(&avctx->coded_frame);
256
257     return 0;
258 }
259
260 AVCodec ff_xwd_decoder = {
261     .name           = "xwd",
262     .type           = AVMEDIA_TYPE_VIDEO,
263     .id             = AV_CODEC_ID_XWD,
264     .init           = xwd_decode_init,
265     .close          = xwd_decode_close,
266     .decode         = xwd_decode_frame,
267     .capabilities   = CODEC_CAP_DR1,
268     .long_name      = NULL_IF_CONFIG_SMALL("XWD (X Window Dump) image"),
269 };