]> git.sesse.net Git - ffmpeg/blob - libavcodec/qdrw.c
avcodec/golomb: get_ur_golomb_jpegls: Fix reading huge k values
[ffmpeg] / libavcodec / qdrw.c
1 /*
2  * QuickDraw (qdrw) codec
3  * Copyright (c) 2004 Konstantin Shishkov
4  * Copyright (c) 2015 Vittorio Giovara
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /**
24  * @file
25  * Apple QuickDraw codec.
26  * https://developer.apple.com/legacy/library/documentation/mac/QuickDraw/QuickDraw-461.html
27  */
28
29 #include "libavutil/common.h"
30 #include "libavutil/intreadwrite.h"
31 #include "avcodec.h"
32 #include "bytestream.h"
33 #include "internal.h"
34
35 enum QuickdrawOpcodes {
36     PACKBITSRECT = 0x0098,
37     PACKBITSRGN,
38     DIRECTBITSRECT,
39     DIRECTBITSRGN,
40
41     EOP = 0x00FF,
42 };
43
44 static int parse_palette(AVCodecContext *avctx, GetByteContext *gbc,
45                          uint32_t *pal, int colors)
46 {
47     int i;
48
49     for (i = 0; i <= colors; i++) {
50         uint8_t r, g, b;
51         unsigned int idx = bytestream2_get_be16(gbc); /* color index */
52         if (idx > 255) {
53             av_log(avctx, AV_LOG_WARNING,
54                    "Palette index out of range: %u\n", idx);
55             bytestream2_skip(gbc, 6);
56             continue;
57         }
58         r = bytestream2_get_byte(gbc);
59         bytestream2_skip(gbc, 1);
60         g = bytestream2_get_byte(gbc);
61         bytestream2_skip(gbc, 1);
62         b = bytestream2_get_byte(gbc);
63         bytestream2_skip(gbc, 1);
64         pal[idx] = (0xFFU << 24) | (r << 16) | (g << 8) | b;
65     }
66     return 0;
67 }
68
69 static int decode_rle(AVCodecContext *avctx, AVFrame *p, GetByteContext *gbc,
70                       int step)
71 {
72     int i, j;
73     int offset = avctx->width * step;
74     uint8_t *outdata = p->data[0];
75
76     for (i = 0; i < avctx->height; i++) {
77         int size, left, code, pix;
78         uint8_t *out = outdata;
79         int pos = 0;
80
81         /* size of packed line */
82         size = left = bytestream2_get_be16(gbc);
83         if (bytestream2_get_bytes_left(gbc) < size)
84             return AVERROR_INVALIDDATA;
85
86         /* decode line */
87         while (left > 0) {
88             code = bytestream2_get_byte(gbc);
89             if (code & 0x80 ) { /* run */
90                 pix = bytestream2_get_byte(gbc);
91                 for (j = 0; j < 257 - code; j++) {
92                     out[pos] = pix;
93                     pos += step;
94                     if (pos >= offset) {
95                         pos -= offset;
96                         pos++;
97                     }
98                     if (pos >= offset)
99                         return AVERROR_INVALIDDATA;
100                 }
101                 left  -= 2;
102             } else { /* copy */
103                 for (j = 0; j < code + 1; j++) {
104                     out[pos] = bytestream2_get_byte(gbc);
105                     pos += step;
106                     if (pos >= offset) {
107                         pos -= offset;
108                         pos++;
109                     }
110                     if (pos >= offset)
111                         return AVERROR_INVALIDDATA;
112                 }
113                 left  -= 2 + code;
114             }
115         }
116         outdata += p->linesize[0];
117     }
118     return 0;
119 }
120
121 static int check_header(const char *buf, int buf_size)
122 {
123     unsigned w, h, v0, v1;
124
125     if (buf_size < 40)
126         return 0;
127
128     w = AV_RB16(buf+6);
129     h = AV_RB16(buf+8);
130     v0 = AV_RB16(buf+10);
131     v1 = AV_RB16(buf+12);
132
133     if (!w || !h)
134         return 0;
135
136     if (v0 == 0x1101)
137         return 1;
138     if (v0 == 0x0011 && v1 == 0x02FF)
139         return 2;
140     return 0;
141 }
142
143
144 static int decode_frame(AVCodecContext *avctx,
145                         void *data, int *got_frame,
146                         AVPacket *avpkt)
147 {
148     AVFrame * const p      = data;
149     GetByteContext gbc;
150     int colors;
151     int w, h, ret;
152     int ver;
153
154     bytestream2_init(&gbc, avpkt->data, avpkt->size);
155     if (   bytestream2_get_bytes_left(&gbc) >= 552
156            && !check_header(gbc.buffer      , bytestream2_get_bytes_left(&gbc))
157            &&  check_header(gbc.buffer + 512, bytestream2_get_bytes_left(&gbc) - 512)
158        )
159         bytestream2_skip(&gbc, 512);
160
161     ver = check_header(gbc.buffer, bytestream2_get_bytes_left(&gbc));
162
163     /* smallest PICT header */
164     if (bytestream2_get_bytes_left(&gbc) < 40) {
165         av_log(avctx, AV_LOG_ERROR, "Frame is too small %d\n",
166                bytestream2_get_bytes_left(&gbc));
167         return AVERROR_INVALIDDATA;
168     }
169
170     bytestream2_skip(&gbc, 6);
171     h = bytestream2_get_be16(&gbc);
172     w = bytestream2_get_be16(&gbc);
173
174     ret = ff_set_dimensions(avctx, w, h);
175     if (ret < 0)
176         return ret;
177
178     /* version 1 is identified by 0x1101
179      * it uses byte-aligned opcodes rather than word-aligned */
180     if (ver == 1) {
181         avpriv_request_sample(avctx, "QuickDraw version 1");
182         return AVERROR_PATCHWELCOME;
183     } else if (ver != 2) {
184         avpriv_request_sample(avctx, "QuickDraw version unknown (%X)", bytestream2_get_be32(&gbc));
185         return AVERROR_PATCHWELCOME;
186     }
187
188     bytestream2_skip(&gbc, 4+26);
189
190     while (bytestream2_get_bytes_left(&gbc) >= 4) {
191         int bppcnt, bpp;
192         int rowbytes, pack_type;
193         int opcode = bytestream2_get_be16(&gbc);
194
195         switch(opcode) {
196         case PACKBITSRECT:
197         case PACKBITSRGN:
198             av_log(avctx, AV_LOG_DEBUG, "Parsing Packbit opcode\n");
199
200             bytestream2_skip(&gbc, 30);
201             bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
202             bpp    = bytestream2_get_be16(&gbc); /* cmpSize */
203
204             av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
205             if (bppcnt == 1 && bpp == 8) {
206                 avctx->pix_fmt = AV_PIX_FMT_PAL8;
207             } else {
208                 av_log(avctx, AV_LOG_ERROR,
209                        "Invalid pixel format (bppcnt %d bpp %d) in Packbit\n",
210                        bppcnt, bpp);
211                 return AVERROR_INVALIDDATA;
212             }
213
214             /* jump to palette */
215             bytestream2_skip(&gbc, 18);
216             colors = bytestream2_get_be16(&gbc);
217
218             if (colors < 0 || colors > 256) {
219                 av_log(avctx, AV_LOG_ERROR,
220                        "Error color count - %i(0x%X)\n", colors, colors);
221                 return AVERROR_INVALIDDATA;
222             }
223             if (bytestream2_get_bytes_left(&gbc) < (colors + 1) * 8) {
224                 av_log(avctx, AV_LOG_ERROR, "Palette is too small %d\n",
225                        bytestream2_get_bytes_left(&gbc));
226                 return AVERROR_INVALIDDATA;
227             }
228             if ((ret = ff_get_buffer(avctx, p, 0)) < 0)
229                 return ret;
230
231             parse_palette(avctx, &gbc, (uint32_t *)p->data[1], colors);
232             p->palette_has_changed = 1;
233
234             /* jump to image data */
235             bytestream2_skip(&gbc, 18);
236
237             if (opcode == PACKBITSRGN) {
238                 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
239                 avpriv_report_missing_feature(avctx, "Packbit mask region");
240             }
241
242             ret = decode_rle(avctx, p, &gbc, bppcnt);
243             if (ret < 0)
244                 return ret;
245             *got_frame = 1;
246             break;
247         case DIRECTBITSRECT:
248         case DIRECTBITSRGN:
249             av_log(avctx, AV_LOG_DEBUG, "Parsing Directbit opcode\n");
250
251             bytestream2_skip(&gbc, 4);
252             rowbytes = bytestream2_get_be16(&gbc) & 0x3FFF;
253             if (rowbytes <= 250) {
254                 avpriv_report_missing_feature(avctx, "Short rowbytes");
255                 return AVERROR_PATCHWELCOME;
256             }
257
258             bytestream2_skip(&gbc, 10);
259             pack_type = bytestream2_get_be16(&gbc);
260
261             bytestream2_skip(&gbc, 16);
262             bppcnt = bytestream2_get_be16(&gbc); /* cmpCount */
263             bpp    = bytestream2_get_be16(&gbc); /* cmpSize */
264
265             av_log(avctx, AV_LOG_DEBUG, "bppcount %d bpp %d\n", bppcnt, bpp);
266             if (bppcnt == 3 && bpp == 8) {
267                 avctx->pix_fmt = AV_PIX_FMT_RGB24;
268             } else if (bppcnt == 4 && bpp == 8) {
269                 avctx->pix_fmt = AV_PIX_FMT_ARGB;
270             } else {
271                 av_log(avctx, AV_LOG_ERROR,
272                        "Invalid pixel format (bppcnt %d bpp %d) in Directbit\n",
273                        bppcnt, bpp);
274                 return AVERROR_INVALIDDATA;
275             }
276
277             /* set packing when default is selected */
278             if (pack_type == 0)
279                 pack_type = bppcnt;
280
281             if (pack_type != 3 && pack_type != 4) {
282                 avpriv_request_sample(avctx, "Pack type %d", pack_type);
283                 return AVERROR_PATCHWELCOME;
284             }
285             if ((ret = ff_get_buffer(avctx, p, 0)) < 0) {
286                 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
287                 return ret;
288             }
289
290             /* jump to data */
291             bytestream2_skip(&gbc, 30);
292
293             if (opcode == DIRECTBITSRGN) {
294                 bytestream2_skip(&gbc, 2 + 8); /* size + rect */
295                 avpriv_report_missing_feature(avctx, "DirectBit mask region");
296             }
297
298             ret = decode_rle(avctx, p, &gbc, bppcnt);
299             if (ret < 0)
300                 return ret;
301             *got_frame = 1;
302             break;
303         default:
304             av_log(avctx, AV_LOG_TRACE, "Unknown 0x%04X opcode\n", opcode);
305             break;
306         }
307         /* exit the loop when a known pixel block has been found */
308         if (*got_frame) {
309             int eop, trail;
310
311             /* re-align to a word */
312             bytestream2_skip(&gbc, bytestream2_get_bytes_left(&gbc) % 2);
313
314             eop = bytestream2_get_be16(&gbc);
315             trail = bytestream2_get_bytes_left(&gbc);
316             if (eop != EOP)
317                 av_log(avctx, AV_LOG_WARNING,
318                        "Missing end of picture opcode (found 0x%04X)\n", eop);
319             if (trail)
320                 av_log(avctx, AV_LOG_WARNING, "Got %d trailing bytes\n", trail);
321             break;
322         }
323     }
324
325     if (*got_frame) {
326         p->pict_type = AV_PICTURE_TYPE_I;
327         p->key_frame = 1;
328
329         return avpkt->size;
330     } else {
331         av_log(avctx, AV_LOG_ERROR, "Frame contained no usable data\n");
332
333         return AVERROR_INVALIDDATA;
334     }
335 }
336
337 AVCodec ff_qdraw_decoder = {
338     .name           = "qdraw",
339     .long_name      = NULL_IF_CONFIG_SMALL("Apple QuickDraw"),
340     .type           = AVMEDIA_TYPE_VIDEO,
341     .id             = AV_CODEC_ID_QDRAW,
342     .decode         = decode_frame,
343     .capabilities   = CODEC_CAP_DR1,
344 };