]> git.sesse.net Git - ffmpeg/blob - libavcodec/magicyuv.c
exr: fix out-of-bounds read
[ffmpeg] / libavcodec / magicyuv.c
1 /*
2  * MagicYUV decoder
3  * Copyright (c) 2016 Paul B Mahol
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "libavutil/pixdesc.h"
26 #include "libavutil/qsort.h"
27
28 #include "avcodec.h"
29 #include "bytestream.h"
30 #include "get_bits.h"
31 #include "huffyuvdsp.h"
32 #include "internal.h"
33 #include "thread.h"
34
35 typedef struct Slice {
36     uint32_t start;
37     uint32_t size;
38 } Slice;
39
40 typedef enum Prediction {
41     LEFT = 1,
42     GRADIENT,
43     MEDIAN,
44 } Prediction;
45
46 typedef struct HuffEntry {
47     uint8_t  sym;
48     uint8_t  len;
49     uint32_t code;
50 } HuffEntry;
51
52 typedef struct MagicYUVContext {
53     AVFrame          *p;
54     int               slice_height;
55     int               nb_slices;
56     int               planes;         // number of encoded planes in bitstream
57     int               decorrelate;    // postprocessing work
58     int               interlaced;     // video is interlaced
59     uint8_t          *buf;            // pointer to AVPacket->data
60     int               hshift[4];
61     int               vshift[4];
62     Slice            *slices[4];      // slice bitstream positions for each plane
63     unsigned int      slices_size[4]; // slice sizes for each plane
64     uint8_t           len[4][256];    // table of code lengths for each plane
65     VLC               vlc[4];         // VLC for each plane
66     HuffYUVDSPContext hdsp;
67 } MagicYUVContext;
68
69 static int huff_cmp_len(const void *a, const void *b)
70 {
71     const HuffEntry *aa = a, *bb = b;
72     return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
73 }
74
75 static int huff_build(VLC *vlc, uint8_t *len)
76 {
77     HuffEntry he[256];
78     uint32_t codes[256];
79     uint8_t bits[256];
80     uint8_t syms[256];
81     uint32_t code;
82     int i;
83
84     for (i = 0; i < 256; i++) {
85         he[i].sym = 255 - i;
86         he[i].len = len[i];
87     }
88     AV_QSORT(he, 256, HuffEntry, huff_cmp_len);
89
90     code = 1;
91     for (i = 255; i >= 0; i--) {
92         codes[i] = code >> (32 - he[i].len);
93         bits[i]  = he[i].len;
94         syms[i]  = he[i].sym;
95         code += 0x80000000u >> (he[i].len - 1);
96     }
97
98     ff_free_vlc(vlc);
99     return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
100                               bits,  sizeof(*bits),  sizeof(*bits),
101                               codes, sizeof(*codes), sizeof(*codes),
102                               syms,  sizeof(*syms),  sizeof(*syms), 0);
103 }
104
105 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
106                              int j, int threadnr)
107 {
108     MagicYUVContext *s = avctx->priv_data;
109     int interlaced = s->interlaced;
110     AVFrame *p = s->p;
111     int i, k, x;
112     GetBitContext gb;
113     uint8_t *dst;
114
115     for (i = 0; i < s->planes; i++) {
116         int left, lefttop, top;
117         int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
118         int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
119         int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
120         ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
121         ptrdiff_t stride = p->linesize[i];
122         int flags, pred;
123         int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
124                                  s->slices[i][j].size);
125
126         if (ret < 0)
127             return ret;
128
129         flags = get_bits(&gb, 8);
130         pred  = get_bits(&gb, 8);
131
132         dst = p->data[i] + j * sheight * stride;
133         if (flags & 1) {
134             for (k = 0; k < height; k++) {
135                 for (x = 0; x < width; x++)
136                     dst[x] = get_bits(&gb, 8);
137
138                 dst += stride;
139             }
140         } else {
141             for (k = 0; k < height; k++) {
142                 for (x = 0; x < width; x++) {
143                     int pix;
144                     if (get_bits_left(&gb) <= 0)
145                         return AVERROR_INVALIDDATA;
146
147                     pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
148                     if (pix < 0)
149                         return AVERROR_INVALIDDATA;
150
151                     dst[x] = 255 - pix;
152                 }
153                 dst += stride;
154             }
155         }
156
157         switch (pred) {
158         case LEFT:
159             dst = p->data[i] + j * sheight * stride;
160             s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
161             dst += stride;
162             if (interlaced) {
163                 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
164                 dst += stride;
165             }
166             for (k = 1 + interlaced; k < height; k++) {
167                 s->hdsp.add_hfyu_left_pred(dst, dst, width, dst[-fake_stride]);
168                 dst += stride;
169             }
170             break;
171         case GRADIENT:
172             dst = p->data[i] + j * sheight * stride;
173             s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
174             left = lefttop = 0;
175             dst += stride;
176             if (interlaced) {
177                 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
178                 left = lefttop = 0;
179                 dst += stride;
180             }
181             for (k = 1 + interlaced; k < height; k++) {
182                 top = dst[-fake_stride];
183                 left = top + dst[0];
184                 dst[0] = left;
185                 for (x = 1; x < width; x++) {
186                     top = dst[x - fake_stride];
187                     lefttop = dst[x - (fake_stride + 1)];
188                     left += top - lefttop + dst[x];
189                     dst[x] = left;
190                 }
191                 dst += stride;
192             }
193             break;
194         case MEDIAN:
195             dst = p->data[i] + j * sheight * stride;
196             lefttop = left = dst[0];
197             s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
198             dst += stride;
199             if (interlaced) {
200                 lefttop = left = dst[0];
201                 s->hdsp.add_hfyu_left_pred(dst, dst, width, 0);
202                 dst += stride;
203             }
204             for (k = 1 + interlaced; k < height; k++) {
205                 s->hdsp.add_hfyu_median_pred(dst, dst - fake_stride,
206                                              dst, width, &left, &lefttop);
207                 lefttop = left = dst[0];
208                 dst += stride;
209             }
210             break;
211         default:
212             avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
213         }
214     }
215
216     if (s->decorrelate) {
217         int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
218         int width = avctx->coded_width;
219         uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
220         uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
221         uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
222
223         for (i = 0; i < height; i++) {
224             s->hdsp.add_bytes(b, g, width);
225             s->hdsp.add_bytes(r, g, width);
226             b += p->linesize[0];
227             g += p->linesize[1];
228             r += p->linesize[2];
229         }
230     }
231
232     return 0;
233 }
234
235 static int magy_decode_frame(AVCodecContext *avctx, void *data,
236                              int *got_frame, AVPacket *avpkt)
237 {
238     MagicYUVContext *s = avctx->priv_data;
239     ThreadFrame frame = { .f = data };
240     AVFrame *p = data;
241     GetByteContext gbyte;
242     GetBitContext gbit;
243     uint32_t first_offset, offset, next_offset, header_size, slice_width;
244     int width, height, format, version, table_size;
245     int ret, i, j, k;
246
247     bytestream2_init(&gbyte, avpkt->data, avpkt->size);
248     if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
249         return AVERROR_INVALIDDATA;
250
251     header_size = bytestream2_get_le32(&gbyte);
252     if (header_size < 32 || header_size >= avpkt->size) {
253         av_log(avctx, AV_LOG_ERROR,
254                "header or packet too small %"PRIu32"\n", header_size);
255         return AVERROR_INVALIDDATA;
256     }
257
258     version = bytestream2_get_byte(&gbyte);
259     if (version != 7) {
260         avpriv_request_sample(avctx, "Version %d", version);
261         return AVERROR_PATCHWELCOME;
262     }
263
264     s->hshift[1] =
265     s->vshift[1] =
266     s->hshift[2] =
267     s->vshift[2] = 0;
268     s->decorrelate = 0;
269
270     format = bytestream2_get_byte(&gbyte);
271     switch (format) {
272     case 0x65:
273         avctx->pix_fmt = AV_PIX_FMT_GBRP;
274         s->decorrelate = 1;
275         break;
276     case 0x66:
277         avctx->pix_fmt = AV_PIX_FMT_GBRAP;
278         s->decorrelate = 1;
279         break;
280     case 0x67:
281         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
282         break;
283     case 0x68:
284         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
285         s->hshift[1] =
286         s->hshift[2] = 1;
287         break;
288     case 0x69:
289         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
290         s->hshift[1] =
291         s->vshift[1] =
292         s->hshift[2] =
293         s->vshift[2] = 1;
294         break;
295     case 0x6a:
296         avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
297         break;
298     case 0x6b:
299         avctx->pix_fmt = AV_PIX_FMT_GRAY8;
300         break;
301     default:
302         avpriv_request_sample(avctx, "Format 0x%X", format);
303         return AVERROR_PATCHWELCOME;
304     }
305     s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
306
307     bytestream2_skip(&gbyte, 2);
308     s->interlaced = !!(bytestream2_get_byte(&gbyte) & 2);
309     bytestream2_skip(&gbyte, 3);
310
311     width  = bytestream2_get_le32(&gbyte);
312     height = bytestream2_get_le32(&gbyte);
313     ret = ff_set_dimensions(avctx, width, height);
314     if (ret < 0)
315         return ret;
316
317     slice_width = bytestream2_get_le32(&gbyte);
318     if (slice_width != avctx->coded_width) {
319         avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
320         return AVERROR_PATCHWELCOME;
321     }
322     s->slice_height = bytestream2_get_le32(&gbyte);
323     if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
324         av_log(avctx, AV_LOG_ERROR,
325                "invalid slice height: %d\n", s->slice_height);
326         return AVERROR_INVALIDDATA;
327     }
328
329     bytestream2_skip(&gbyte, 4);
330
331     s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
332     if (s->nb_slices > INT_MAX / sizeof(Slice)) {
333         av_log(avctx, AV_LOG_ERROR,
334                "invalid number of slices: %d\n", s->nb_slices);
335         return AVERROR_INVALIDDATA;
336     }
337
338     for (i = 0; i < s->planes; i++) {
339         av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
340         if (!s->slices[i])
341             return AVERROR(ENOMEM);
342
343         offset = bytestream2_get_le32(&gbyte);
344         if (offset >= avpkt->size - header_size)
345             return AVERROR_INVALIDDATA;
346
347         if (i == 0)
348             first_offset = offset;
349
350         for (j = 0; j < s->nb_slices - 1; j++) {
351             s->slices[i][j].start = offset + header_size;
352
353             next_offset = bytestream2_get_le32(&gbyte);
354             if (next_offset <= offset || next_offset >= avpkt->size - header_size)
355                 return AVERROR_INVALIDDATA;
356
357             s->slices[i][j].size = next_offset - offset;
358             offset = next_offset;
359         }
360
361         s->slices[i][j].start = offset + header_size;
362         s->slices[i][j].size  = avpkt->size - s->slices[i][j].start;
363     }
364
365     if (bytestream2_get_byte(&gbyte) != s->planes)
366         return AVERROR_INVALIDDATA;
367
368     bytestream2_skip(&gbyte, s->nb_slices * s->planes);
369
370     table_size = header_size + first_offset - bytestream2_tell(&gbyte);
371     if (table_size < 2)
372         return AVERROR_INVALIDDATA;
373
374     ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
375     if (ret < 0)
376         return ret;
377
378     memset(s->len, 0, sizeof(s->len));
379     j = i = 0;
380     while (get_bits_left(&gbit) >= 8) {
381         int b = get_bits(&gbit, 4);
382         int x = get_bits(&gbit, 4);
383         int l = get_bitsz(&gbit, b) + 1;
384
385         for (k = 0; k < l; k++)
386             if (j + k < 256)
387                 s->len[i][j + k] = x;
388
389         j += l;
390         if (j == 256) {
391             j = 0;
392             if (huff_build(&s->vlc[i], s->len[i])) {
393                 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
394                 return AVERROR_INVALIDDATA;
395             }
396             i++;
397             if (i == s->planes) {
398                 break;
399             }
400         } else if (j > 256) {
401             return AVERROR_INVALIDDATA;
402         }
403     }
404
405     if (i != s->planes) {
406         av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
407         return AVERROR_INVALIDDATA;
408     }
409
410     p->pict_type = AV_PICTURE_TYPE_I;
411     p->key_frame = 1;
412
413     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
414         return ret;
415
416     s->buf = avpkt->data;
417     s->p = p;
418     avctx->execute2(avctx, magy_decode_slice, NULL, NULL, s->nb_slices);
419
420     if (avctx->pix_fmt == AV_PIX_FMT_GBRP ||
421         avctx->pix_fmt == AV_PIX_FMT_GBRAP) {
422         FFSWAP(uint8_t*, p->data[0], p->data[1]);
423         FFSWAP(int, p->linesize[0], p->linesize[1]);
424     }
425
426     *got_frame = 1;
427
428     return avpkt->size;
429 }
430
431 #if HAVE_THREADS
432 static int magy_init_thread_copy(AVCodecContext *avctx)
433 {
434     MagicYUVContext *s = avctx->priv_data;
435     int i;
436
437     for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
438         s->slices[i] = NULL;
439         s->slices_size[i] = 0;
440     }
441
442     return 0;
443 }
444 #endif
445
446 static av_cold int magy_decode_init(AVCodecContext *avctx)
447 {
448     MagicYUVContext *s = avctx->priv_data;
449     ff_huffyuvdsp_init(&s->hdsp);
450     return 0;
451 }
452
453 static av_cold int magy_decode_end(AVCodecContext *avctx)
454 {
455     MagicYUVContext * const s = avctx->priv_data;
456     int i;
457
458     for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
459         av_freep(&s->slices[i]);
460         s->slices_size[i] = 0;
461         ff_free_vlc(&s->vlc[i]);
462     }
463
464     return 0;
465 }
466
467 AVCodec ff_magicyuv_decoder = {
468     .name             = "magicyuv",
469     .long_name        = NULL_IF_CONFIG_SMALL("MagicYUV video"),
470     .type             = AVMEDIA_TYPE_VIDEO,
471     .id               = AV_CODEC_ID_MAGICYUV,
472     .priv_data_size   = sizeof(MagicYUVContext),
473     .init             = magy_decode_init,
474     .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
475     .close            = magy_decode_end,
476     .decode           = magy_decode_frame,
477     .capabilities     = AV_CODEC_CAP_DR1 |
478                         AV_CODEC_CAP_FRAME_THREADS |
479                         AV_CODEC_CAP_SLICE_THREADS,
480     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE,
481 };