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