]> git.sesse.net Git - ffmpeg/blob - libavcodec/magicyuv.c
avcodec/magicyuvenc: mark as not more experimental
[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 "lossless_videodsp.h"
34 #include "thread.h"
35
36 typedef struct Slice {
37     uint32_t start;
38     uint32_t size;
39 } Slice;
40
41 typedef enum Prediction {
42     LEFT = 1,
43     GRADIENT,
44     MEDIAN,
45 } Prediction;
46
47 typedef struct HuffEntry {
48     uint16_t sym;
49     uint8_t  len;
50     uint32_t code;
51 } HuffEntry;
52
53 typedef struct MagicYUVContext {
54     AVFrame          *p;
55     int               max;
56     int               bps;
57     int               slice_height;
58     int               nb_slices;
59     int               planes;         // number of encoded planes in bitstream
60     int               decorrelate;    // postprocessing work
61     int               color_matrix;   // video color matrix
62     int               flags;
63     int               interlaced;     // video is interlaced
64     uint8_t          *buf;            // pointer to AVPacket->data
65     int               hshift[4];
66     int               vshift[4];
67     Slice            *slices[4];      // slice bitstream positions for each plane
68     unsigned int      slices_size[4]; // slice sizes for each plane
69     uint8_t           len[4][4096];   // table of code lengths for each plane
70     VLC               vlc[4];         // VLC for each plane
71     int (*huff_build)(VLC *vlc, uint8_t *len);
72     int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
73                              int j, int threadnr);
74     LLVidDSPContext   llviddsp;
75 } MagicYUVContext;
76
77 static int huff_cmp_len(const void *a, const void *b)
78 {
79     const HuffEntry *aa = a, *bb = b;
80     return (aa->len - bb->len) * 256 + aa->sym - bb->sym;
81 }
82
83 static int huff_cmp_len10(const void *a, const void *b)
84 {
85     const HuffEntry *aa = a, *bb = b;
86     return (aa->len - bb->len) * 1024 + aa->sym - bb->sym;
87 }
88
89 static int huff_cmp_len12(const void *a, const void *b)
90 {
91     const HuffEntry *aa = a, *bb = b;
92     return (aa->len - bb->len) * 4096 + aa->sym - bb->sym;
93 }
94
95 static int huff_build10(VLC *vlc, uint8_t *len)
96 {
97     HuffEntry he[1024];
98     uint32_t codes[1024];
99     uint8_t bits[1024];
100     uint16_t syms[1024];
101     uint32_t code;
102     int i;
103
104     for (i = 0; i < 1024; i++) {
105         he[i].sym = 1023 - i;
106         he[i].len = len[i];
107         if (len[i] == 0 || len[i] > 32)
108             return AVERROR_INVALIDDATA;
109     }
110     AV_QSORT(he, 1024, HuffEntry, huff_cmp_len10);
111
112     code = 1;
113     for (i = 1023; i >= 0; i--) {
114         codes[i] = code >> (32 - he[i].len);
115         bits[i]  = he[i].len;
116         syms[i]  = he[i].sym;
117         code += 0x80000000u >> (he[i].len - 1);
118     }
119
120     ff_free_vlc(vlc);
121     return ff_init_vlc_sparse(vlc, FFMIN(he[1023].len, 12), 1024,
122                               bits,  sizeof(*bits),  sizeof(*bits),
123                               codes, sizeof(*codes), sizeof(*codes),
124                               syms,  sizeof(*syms),  sizeof(*syms), 0);
125 }
126
127 static int huff_build12(VLC *vlc, uint8_t *len)
128 {
129     HuffEntry he[4096];
130     uint32_t codes[4096];
131     uint8_t bits[4096];
132     uint16_t syms[4096];
133     uint32_t code;
134     int i;
135
136     for (i = 0; i < 4096; i++) {
137         he[i].sym = 4095 - i;
138         he[i].len = len[i];
139         if (len[i] == 0 || len[i] > 32)
140             return AVERROR_INVALIDDATA;
141     }
142     AV_QSORT(he, 4096, HuffEntry, huff_cmp_len12);
143
144     code = 1;
145     for (i = 4095; i >= 0; i--) {
146         codes[i] = code >> (32 - he[i].len);
147         bits[i]  = he[i].len;
148         syms[i]  = he[i].sym;
149         code += 0x80000000u >> (he[i].len - 1);
150     }
151
152     ff_free_vlc(vlc);
153     return ff_init_vlc_sparse(vlc, FFMIN(he[4095].len, 14), 4096,
154                               bits,  sizeof(*bits),  sizeof(*bits),
155                               codes, sizeof(*codes), sizeof(*codes),
156                               syms,  sizeof(*syms),  sizeof(*syms), 0);
157 }
158
159 static int huff_build(VLC *vlc, uint8_t *len)
160 {
161     HuffEntry he[256];
162     uint32_t codes[256];
163     uint8_t bits[256];
164     uint8_t syms[256];
165     uint32_t code;
166     int i;
167
168     for (i = 0; i < 256; i++) {
169         he[i].sym = 255 - i;
170         he[i].len = len[i];
171         if (len[i] == 0 || len[i] > 32)
172             return AVERROR_INVALIDDATA;
173     }
174     AV_QSORT(he, 256, HuffEntry, huff_cmp_len);
175
176     code = 1;
177     for (i = 255; i >= 0; i--) {
178         codes[i] = code >> (32 - he[i].len);
179         bits[i]  = he[i].len;
180         syms[i]  = he[i].sym;
181         code += 0x80000000u >> (he[i].len - 1);
182     }
183
184     ff_free_vlc(vlc);
185     return ff_init_vlc_sparse(vlc, FFMIN(he[255].len, 12), 256,
186                               bits,  sizeof(*bits),  sizeof(*bits),
187                               codes, sizeof(*codes), sizeof(*codes),
188                               syms,  sizeof(*syms),  sizeof(*syms), 0);
189 }
190
191 static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
192                                    const uint16_t *diff, intptr_t w,
193                                    int *left, int *left_top, int max)
194 {
195     int i;
196     uint16_t l, lt;
197
198     l  = *left;
199     lt = *left_top;
200
201     for (i = 0; i < w; i++) {
202         l      = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
203         l     &= max;
204         lt     = src1[i];
205         dst[i] = l;
206     }
207
208     *left     = l;
209     *left_top = lt;
210 }
211
212 static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
213                                int j, int threadnr)
214 {
215     MagicYUVContext *s = avctx->priv_data;
216     int interlaced = s->interlaced;
217     const int bps = s->bps;
218     const int max = s->max - 1;
219     AVFrame *p = s->p;
220     int i, k, x;
221     GetBitContext gb;
222     uint16_t *dst;
223
224     for (i = 0; i < s->planes; i++) {
225         int left, lefttop, top;
226         int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
227         int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
228         int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
229         ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
230         ptrdiff_t stride = p->linesize[i] / 2;
231         int flags, pred;
232         int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
233                                  s->slices[i][j].size);
234
235         if (ret < 0)
236             return ret;
237
238         flags = get_bits(&gb, 8);
239         pred  = get_bits(&gb, 8);
240
241         dst = (uint16_t *)p->data[i] + j * sheight * stride;
242         if (flags & 1) {
243             for (k = 0; k < height; k++) {
244                 for (x = 0; x < width; x++)
245                     dst[x] = get_bits(&gb, bps);
246
247                 dst += stride;
248             }
249         } else {
250             for (k = 0; k < height; k++) {
251                 for (x = 0; x < width; x++) {
252                     int pix;
253                     if (get_bits_left(&gb) <= 0)
254                         return AVERROR_INVALIDDATA;
255
256                     pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
257                     if (pix < 0)
258                         return AVERROR_INVALIDDATA;
259
260                     dst[x] = max - pix;
261                 }
262                 dst += stride;
263             }
264         }
265
266         switch (pred) {
267         case LEFT:
268             dst = (uint16_t *)p->data[i] + j * sheight * stride;
269             s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
270             dst += stride;
271             if (interlaced) {
272                 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
273                 dst += stride;
274             }
275             for (k = 1 + interlaced; k < height; k++) {
276                 s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
277                 dst += stride;
278             }
279             break;
280         case GRADIENT:
281             dst = (uint16_t *)p->data[i] + j * sheight * stride;
282             s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
283             dst += stride;
284             if (interlaced) {
285                 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
286                 dst += stride;
287             }
288             for (k = 1 + interlaced; k < height; k++) {
289                 top = dst[-fake_stride];
290                 left = top + dst[0];
291                 dst[0] = left & max;
292                 for (x = 1; x < width; x++) {
293                     top = dst[x - fake_stride];
294                     lefttop = dst[x - (fake_stride + 1)];
295                     left += top - lefttop + dst[x];
296                     dst[x] = left & max;
297                 }
298                 dst += stride;
299             }
300             break;
301         case MEDIAN:
302             dst = (uint16_t *)p->data[i] + j * sheight * stride;
303             s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
304             dst += stride;
305             if (interlaced) {
306                 s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
307                 dst += stride;
308             }
309             lefttop = left = dst[0];
310             for (k = 1 + interlaced; k < height; k++) {
311                 magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
312                 lefttop = left = dst[0];
313                 dst += stride;
314             }
315             break;
316         default:
317             avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
318         }
319     }
320
321     if (s->decorrelate) {
322         int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
323         int width = avctx->coded_width;
324         uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
325         uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
326         uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
327
328         for (i = 0; i < height; i++) {
329             for (k = 0; k < width; k++) {
330                 b[k] = (b[k] + g[k]) & max;
331                 r[k] = (r[k] + g[k]) & max;
332             }
333             b += p->linesize[0] / 2;
334             g += p->linesize[1] / 2;
335             r += p->linesize[2] / 2;
336         }
337     }
338
339     return 0;
340 }
341
342 static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
343                              int j, int threadnr)
344 {
345     MagicYUVContext *s = avctx->priv_data;
346     int interlaced = s->interlaced;
347     AVFrame *p = s->p;
348     int i, k, x, min_width;
349     GetBitContext gb;
350     uint8_t *dst;
351
352     for (i = 0; i < s->planes; i++) {
353         int left, lefttop, top;
354         int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
355         int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
356         int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
357         ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
358         ptrdiff_t stride = p->linesize[i];
359         int flags, pred;
360         int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
361                                  s->slices[i][j].size);
362
363         if (ret < 0)
364             return ret;
365
366         flags = get_bits(&gb, 8);
367         pred  = get_bits(&gb, 8);
368
369         dst = p->data[i] + j * sheight * stride;
370         if (flags & 1) {
371             for (k = 0; k < height; k++) {
372                 for (x = 0; x < width; x++)
373                     dst[x] = get_bits(&gb, 8);
374
375                 dst += stride;
376             }
377         } else {
378             for (k = 0; k < height; k++) {
379                 for (x = 0; x < width; x++) {
380                     int pix;
381                     if (get_bits_left(&gb) <= 0)
382                         return AVERROR_INVALIDDATA;
383
384                     pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
385                     if (pix < 0)
386                         return AVERROR_INVALIDDATA;
387
388                     dst[x] = 255 - pix;
389                 }
390                 dst += stride;
391             }
392         }
393
394         switch (pred) {
395         case LEFT:
396             dst = p->data[i] + j * sheight * stride;
397             s->llviddsp.add_left_pred(dst, dst, width, 0);
398             dst += stride;
399             if (interlaced) {
400                 s->llviddsp.add_left_pred(dst, dst, width, 0);
401                 dst += stride;
402             }
403             for (k = 1 + interlaced; k < height; k++) {
404                 s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
405                 dst += stride;
406             }
407             break;
408         case GRADIENT:
409             dst = p->data[i] + j * sheight * stride;
410             s->llviddsp.add_left_pred(dst, dst, width, 0);
411             dst += stride;
412             if (interlaced) {
413                 s->llviddsp.add_left_pred(dst, dst, width, 0);
414                 dst += stride;
415             }
416             min_width = FFMIN(width, 32);
417             for (k = 1 + interlaced; k < height; k++) {
418                 top = dst[-fake_stride];
419                 left = top + dst[0];
420                 dst[0] = left;
421                 for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
422                     top = dst[x - fake_stride];
423                     lefttop = dst[x - (fake_stride + 1)];
424                     left += top - lefttop + dst[x];
425                     dst[x] = left;
426                 }
427                 if (width > 32)
428                     s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
429                 dst += stride;
430             }
431             break;
432         case MEDIAN:
433             dst = p->data[i] + j * sheight * stride;
434             s->llviddsp.add_left_pred(dst, dst, width, 0);
435             dst += stride;
436             if (interlaced) {
437                 s->llviddsp.add_left_pred(dst, dst, width, 0);
438                 dst += stride;
439             }
440             lefttop = left = dst[0];
441             for (k = 1 + interlaced; k < height; k++) {
442                 s->llviddsp.add_median_pred(dst, dst - fake_stride,
443                                              dst, width, &left, &lefttop);
444                 lefttop = left = dst[0];
445                 dst += stride;
446             }
447             break;
448         default:
449             avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
450         }
451     }
452
453     if (s->decorrelate) {
454         int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
455         int width = avctx->coded_width;
456         uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
457         uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
458         uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
459
460         for (i = 0; i < height; i++) {
461             s->llviddsp.add_bytes(b, g, width);
462             s->llviddsp.add_bytes(r, g, width);
463             b += p->linesize[0];
464             g += p->linesize[1];
465             r += p->linesize[2];
466         }
467     }
468
469     return 0;
470 }
471
472 static int build_huffman(AVCodecContext *avctx, GetBitContext *gbit, int max)
473 {
474     MagicYUVContext *s = avctx->priv_data;
475     int i = 0, j = 0, k;
476
477     memset(s->len, 0, sizeof(s->len));
478     while (get_bits_left(gbit) >= 8) {
479         int b = get_bits(gbit, 1);
480         int x = get_bits(gbit, 7);
481         int l = get_bitsz(gbit, b * 8) + 1;
482
483         for (k = 0; k < l; k++)
484             if (j + k < max)
485                 s->len[i][j + k] = x;
486
487         j += l;
488         if (j == max) {
489             j = 0;
490             if (s->huff_build(&s->vlc[i], s->len[i])) {
491                 av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
492                 return AVERROR_INVALIDDATA;
493             }
494             i++;
495             if (i == s->planes) {
496                 break;
497             }
498         } else if (j > max) {
499             av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
500             return AVERROR_INVALIDDATA;
501         }
502     }
503
504     if (i != s->planes) {
505         av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
506         return AVERROR_INVALIDDATA;
507     }
508
509     return 0;
510 }
511
512 static int magy_decode_frame(AVCodecContext *avctx, void *data,
513                              int *got_frame, AVPacket *avpkt)
514 {
515     MagicYUVContext *s = avctx->priv_data;
516     ThreadFrame frame = { .f = data };
517     AVFrame *p = data;
518     GetByteContext gbyte;
519     GetBitContext gbit;
520     uint32_t first_offset, offset, next_offset, header_size, slice_width;
521     int width, height, format, version, table_size;
522     int ret, i, j;
523
524     bytestream2_init(&gbyte, avpkt->data, avpkt->size);
525     if (bytestream2_get_le32(&gbyte) != MKTAG('M', 'A', 'G', 'Y'))
526         return AVERROR_INVALIDDATA;
527
528     header_size = bytestream2_get_le32(&gbyte);
529     if (header_size < 32 || header_size >= avpkt->size) {
530         av_log(avctx, AV_LOG_ERROR,
531                "header or packet too small %"PRIu32"\n", header_size);
532         return AVERROR_INVALIDDATA;
533     }
534
535     version = bytestream2_get_byte(&gbyte);
536     if (version != 7) {
537         avpriv_request_sample(avctx, "Version %d", version);
538         return AVERROR_PATCHWELCOME;
539     }
540
541     s->hshift[1] =
542     s->vshift[1] =
543     s->hshift[2] =
544     s->vshift[2] = 0;
545     s->decorrelate = 0;
546     s->max = 256;
547     s->bps = 8;
548     s->huff_build = huff_build;
549     s->magy_decode_slice = magy_decode_slice;
550
551     format = bytestream2_get_byte(&gbyte);
552     switch (format) {
553     case 0x65:
554         avctx->pix_fmt = AV_PIX_FMT_GBRP;
555         s->decorrelate = 1;
556         break;
557     case 0x66:
558         avctx->pix_fmt = AV_PIX_FMT_GBRAP;
559         s->decorrelate = 1;
560         break;
561     case 0x67:
562         avctx->pix_fmt = AV_PIX_FMT_YUV444P;
563         break;
564     case 0x68:
565         avctx->pix_fmt = AV_PIX_FMT_YUV422P;
566         s->hshift[1] =
567         s->hshift[2] = 1;
568         break;
569     case 0x69:
570         avctx->pix_fmt = AV_PIX_FMT_YUV420P;
571         s->hshift[1] =
572         s->vshift[1] =
573         s->hshift[2] =
574         s->vshift[2] = 1;
575         break;
576     case 0x6a:
577         avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
578         break;
579     case 0x6b:
580         avctx->pix_fmt = AV_PIX_FMT_GRAY8;
581         break;
582     case 0x6c:
583         avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
584         s->hshift[1] =
585         s->hshift[2] = 1;
586         s->max = 1024;
587         s->huff_build = huff_build10;
588         s->magy_decode_slice = magy_decode_slice10;
589         s->bps = 10;
590         break;
591     case 0x6d:
592         avctx->pix_fmt = AV_PIX_FMT_GBRP10;
593         s->decorrelate = 1;
594         s->max = 1024;
595         s->huff_build = huff_build10;
596         s->magy_decode_slice = magy_decode_slice10;
597         s->bps = 10;
598         break;
599     case 0x6e:
600         avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
601         s->decorrelate = 1;
602         s->max = 1024;
603         s->huff_build = huff_build10;
604         s->magy_decode_slice = magy_decode_slice10;
605         s->bps = 10;
606         break;
607     case 0x6f:
608         avctx->pix_fmt = AV_PIX_FMT_GBRP12;
609         s->decorrelate = 1;
610         s->max = 4096;
611         s->huff_build = huff_build12;
612         s->magy_decode_slice = magy_decode_slice10;
613         s->bps = 12;
614         break;
615     case 0x70:
616         avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
617         s->decorrelate = 1;
618         s->max = 4096;
619         s->huff_build = huff_build12;
620         s->magy_decode_slice = magy_decode_slice10;
621         s->bps = 12;
622         break;
623     case 0x73:
624         avctx->pix_fmt = AV_PIX_FMT_GRAY10;
625         s->max = 1024;
626         s->huff_build = huff_build10;
627         s->magy_decode_slice = magy_decode_slice10;
628         s->bps = 10;
629         break;
630     default:
631         avpriv_request_sample(avctx, "Format 0x%X", format);
632         return AVERROR_PATCHWELCOME;
633     }
634     s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
635
636     bytestream2_skip(&gbyte, 1);
637     s->color_matrix = bytestream2_get_byte(&gbyte);
638     s->flags        = bytestream2_get_byte(&gbyte);
639     s->interlaced   = !!(s->flags & 2);
640     bytestream2_skip(&gbyte, 3);
641
642     width  = bytestream2_get_le32(&gbyte);
643     height = bytestream2_get_le32(&gbyte);
644     ret = ff_set_dimensions(avctx, width, height);
645     if (ret < 0)
646         return ret;
647
648     slice_width = bytestream2_get_le32(&gbyte);
649     if (slice_width != avctx->coded_width) {
650         avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
651         return AVERROR_PATCHWELCOME;
652     }
653     s->slice_height = bytestream2_get_le32(&gbyte);
654     if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
655         av_log(avctx, AV_LOG_ERROR,
656                "invalid slice height: %d\n", s->slice_height);
657         return AVERROR_INVALIDDATA;
658     }
659
660     bytestream2_skip(&gbyte, 4);
661
662     s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
663     if (s->nb_slices > INT_MAX / sizeof(Slice)) {
664         av_log(avctx, AV_LOG_ERROR,
665                "invalid number of slices: %d\n", s->nb_slices);
666         return AVERROR_INVALIDDATA;
667     }
668
669     for (i = 0; i < s->planes; i++) {
670         av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
671         if (!s->slices[i])
672             return AVERROR(ENOMEM);
673
674         offset = bytestream2_get_le32(&gbyte);
675         if (offset >= avpkt->size - header_size)
676             return AVERROR_INVALIDDATA;
677
678         if (i == 0)
679             first_offset = offset;
680
681         for (j = 0; j < s->nb_slices - 1; j++) {
682             s->slices[i][j].start = offset + header_size;
683
684             next_offset = bytestream2_get_le32(&gbyte);
685             if (next_offset <= offset || next_offset >= avpkt->size - header_size)
686                 return AVERROR_INVALIDDATA;
687
688             s->slices[i][j].size = next_offset - offset;
689             offset = next_offset;
690         }
691
692         s->slices[i][j].start = offset + header_size;
693         s->slices[i][j].size  = avpkt->size - s->slices[i][j].start;
694     }
695
696     if (bytestream2_get_byte(&gbyte) != s->planes)
697         return AVERROR_INVALIDDATA;
698
699     bytestream2_skip(&gbyte, s->nb_slices * s->planes);
700
701     table_size = header_size + first_offset - bytestream2_tell(&gbyte);
702     if (table_size < 2)
703         return AVERROR_INVALIDDATA;
704
705     ret = init_get_bits8(&gbit, avpkt->data + bytestream2_tell(&gbyte), table_size);
706     if (ret < 0)
707         return ret;
708
709     ret = build_huffman(avctx, &gbit, s->max);
710     if (ret < 0)
711         return ret;
712
713     p->pict_type = AV_PICTURE_TYPE_I;
714     p->key_frame = 1;
715
716     if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
717         return ret;
718
719     s->buf = avpkt->data;
720     s->p = p;
721     avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
722
723     if (avctx->pix_fmt == AV_PIX_FMT_GBRP   ||
724         avctx->pix_fmt == AV_PIX_FMT_GBRAP  ||
725         avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
726         avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
727         avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
728         avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
729         FFSWAP(uint8_t*, p->data[0], p->data[1]);
730         FFSWAP(int, p->linesize[0], p->linesize[1]);
731     } else {
732         switch (s->color_matrix) {
733         case 1:
734             p->colorspace = AVCOL_SPC_BT470BG;
735             break;
736         case 2:
737             p->colorspace = AVCOL_SPC_BT709;
738             break;
739         }
740         p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
741     }
742
743     *got_frame = 1;
744
745     return avpkt->size;
746 }
747
748 #if HAVE_THREADS
749 static int magy_init_thread_copy(AVCodecContext *avctx)
750 {
751     MagicYUVContext *s = avctx->priv_data;
752     int i;
753
754     for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
755         s->slices[i] = NULL;
756         s->slices_size[i] = 0;
757     }
758
759     return 0;
760 }
761 #endif
762
763 static av_cold int magy_decode_init(AVCodecContext *avctx)
764 {
765     MagicYUVContext *s = avctx->priv_data;
766     ff_llviddsp_init(&s->llviddsp);
767     return 0;
768 }
769
770 static av_cold int magy_decode_end(AVCodecContext *avctx)
771 {
772     MagicYUVContext * const s = avctx->priv_data;
773     int i;
774
775     for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
776         av_freep(&s->slices[i]);
777         s->slices_size[i] = 0;
778         ff_free_vlc(&s->vlc[i]);
779     }
780
781     return 0;
782 }
783
784 AVCodec ff_magicyuv_decoder = {
785     .name             = "magicyuv",
786     .long_name        = NULL_IF_CONFIG_SMALL("MagicYUV video"),
787     .type             = AVMEDIA_TYPE_VIDEO,
788     .id               = AV_CODEC_ID_MAGICYUV,
789     .priv_data_size   = sizeof(MagicYUVContext),
790     .init             = magy_decode_init,
791     .init_thread_copy = ONLY_IF_THREADS_ENABLED(magy_init_thread_copy),
792     .close            = magy_decode_end,
793     .decode           = magy_decode_frame,
794     .capabilities     = AV_CODEC_CAP_DR1 |
795                         AV_CODEC_CAP_FRAME_THREADS |
796                         AV_CODEC_CAP_SLICE_THREADS,
797     .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE,
798 };