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