GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/magicyuv.c Lines: 248 444 55.9 %
Date: 2020-11-28 20:53:16 Branches: 108 211 51.2 %

Line Branch Exec Source
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
29
#include "avcodec.h"
30
#include "bytestream.h"
31
#include "get_bits.h"
32
#include "huffyuvdsp.h"
33
#include "internal.h"
34
#include "lossless_videodsp.h"
35
#include "thread.h"
36
37
typedef struct Slice {
38
    uint32_t start;
39
    uint32_t size;
40
} Slice;
41
42
typedef enum Prediction {
43
    LEFT = 1,
44
    GRADIENT,
45
    MEDIAN,
46
} Prediction;
47
48
typedef struct HuffEntry {
49
    uint8_t  len;
50
    uint16_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
    const 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
    VLC               vlc[4];         // VLC for each plane
70
    int (*magy_decode_slice)(AVCodecContext *avctx, void *tdata,
71
                             int j, int threadnr);
72
    LLVidDSPContext   llviddsp;
73
} MagicYUVContext;
74
75
42
static int huff_build(HuffEntry he[], uint16_t codes_count[33],
76
                      VLC *vlc, int nb_elems)
77
{
78
42
    unsigned nb_codes = 0, max = 0;
79
80
1386
    for (int i = 32; i > 0; i--) {
81
1344
        uint16_t curr = codes_count[i];   // # of leafs of length i
82
1344
        codes_count[i] = nb_codes / 2;    // # of non-leaf nodes on level i
83
1344
        nb_codes = codes_count[i] + curr; // # of nodes on level i
84

1344
        if (curr && !max)
85
42
            max = i;
86
    }
87
88
10794
    for (unsigned i = 0; i < nb_elems; i++) {
89
10752
        he[i].code = codes_count[he[i].len];
90
10752
        codes_count[he[i].len]++;
91
    }
92
42
    ff_free_vlc(vlc);
93
42
    return init_vlc(vlc, FFMIN(max, 12), nb_elems,
94
                    &he[0].len,  sizeof(he[0]), sizeof(he[0].len),
95
                    &he[0].code, sizeof(he[0]), sizeof(he[0].code), 0);
96
}
97
98
static void magicyuv_median_pred16(uint16_t *dst, const uint16_t *src1,
99
                                   const uint16_t *diff, intptr_t w,
100
                                   int *left, int *left_top, int max)
101
{
102
    int i;
103
    uint16_t l, lt;
104
105
    l  = *left;
106
    lt = *left_top;
107
108
    for (i = 0; i < w; i++) {
109
        l      = mid_pred(l, src1[i], (l + src1[i] - lt)) + diff[i];
110
        l     &= max;
111
        lt     = src1[i];
112
        dst[i] = l;
113
    }
114
115
    *left     = l;
116
    *left_top = lt;
117
}
118
119
static int magy_decode_slice10(AVCodecContext *avctx, void *tdata,
120
                               int j, int threadnr)
121
{
122
    MagicYUVContext *s = avctx->priv_data;
123
    int interlaced = s->interlaced;
124
    const int bps = s->bps;
125
    const int max = s->max - 1;
126
    AVFrame *p = s->p;
127
    int i, k, x;
128
    GetBitContext gb;
129
    uint16_t *dst;
130
131
    for (i = 0; i < s->planes; i++) {
132
        int left, lefttop, top;
133
        int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
134
        int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
135
        int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
136
        ptrdiff_t fake_stride = (p->linesize[i] / 2) * (1 + interlaced);
137
        ptrdiff_t stride = p->linesize[i] / 2;
138
        int flags, pred;
139
        int ret = init_get_bits8(&gb, s->buf + s->slices[i][j].start,
140
                                 s->slices[i][j].size);
141
142
        if (ret < 0)
143
            return ret;
144
145
        flags = get_bits(&gb, 8);
146
        pred  = get_bits(&gb, 8);
147
148
        dst = (uint16_t *)p->data[i] + j * sheight * stride;
149
        if (flags & 1) {
150
            if (get_bits_left(&gb) < bps * width * height)
151
                return AVERROR_INVALIDDATA;
152
            for (k = 0; k < height; k++) {
153
                for (x = 0; x < width; x++)
154
                    dst[x] = get_bits(&gb, bps);
155
156
                dst += stride;
157
            }
158
        } else {
159
            for (k = 0; k < height; k++) {
160
                for (x = 0; x < width; x++) {
161
                    int pix;
162
                    if (get_bits_left(&gb) <= 0)
163
                        return AVERROR_INVALIDDATA;
164
165
                    pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
166
                    if (pix < 0)
167
                        return AVERROR_INVALIDDATA;
168
169
                    dst[x] = pix;
170
                }
171
                dst += stride;
172
            }
173
        }
174
175
        switch (pred) {
176
        case LEFT:
177
            dst = (uint16_t *)p->data[i] + j * sheight * stride;
178
            s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
179
            dst += stride;
180
            if (interlaced) {
181
                s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
182
                dst += stride;
183
            }
184
            for (k = 1 + interlaced; k < height; k++) {
185
                s->llviddsp.add_left_pred_int16(dst, dst, max, width, dst[-fake_stride]);
186
                dst += stride;
187
            }
188
            break;
189
        case GRADIENT:
190
            dst = (uint16_t *)p->data[i] + j * sheight * stride;
191
            s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
192
            dst += stride;
193
            if (interlaced) {
194
                s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
195
                dst += stride;
196
            }
197
            for (k = 1 + interlaced; k < height; k++) {
198
                top = dst[-fake_stride];
199
                left = top + dst[0];
200
                dst[0] = left & max;
201
                for (x = 1; x < width; x++) {
202
                    top = dst[x - fake_stride];
203
                    lefttop = dst[x - (fake_stride + 1)];
204
                    left += top - lefttop + dst[x];
205
                    dst[x] = left & max;
206
                }
207
                dst += stride;
208
            }
209
            break;
210
        case MEDIAN:
211
            dst = (uint16_t *)p->data[i] + j * sheight * stride;
212
            s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
213
            dst += stride;
214
            if (interlaced) {
215
                s->llviddsp.add_left_pred_int16(dst, dst, max, width, 0);
216
                dst += stride;
217
            }
218
            lefttop = left = dst[0];
219
            for (k = 1 + interlaced; k < height; k++) {
220
                magicyuv_median_pred16(dst, dst - fake_stride, dst, width, &left, &lefttop, max);
221
                lefttop = left = dst[0];
222
                dst += stride;
223
            }
224
            break;
225
        default:
226
            avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
227
        }
228
    }
229
230
    if (s->decorrelate) {
231
        int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
232
        int width = avctx->coded_width;
233
        uint16_t *r = (uint16_t *)p->data[0] + j * s->slice_height * p->linesize[0] / 2;
234
        uint16_t *g = (uint16_t *)p->data[1] + j * s->slice_height * p->linesize[1] / 2;
235
        uint16_t *b = (uint16_t *)p->data[2] + j * s->slice_height * p->linesize[2] / 2;
236
237
        for (i = 0; i < height; i++) {
238
            for (k = 0; k < width; k++) {
239
                b[k] = (b[k] + g[k]) & max;
240
                r[k] = (r[k] + g[k]) & max;
241
            }
242
            b += p->linesize[0] / 2;
243
            g += p->linesize[1] / 2;
244
            r += p->linesize[2] / 2;
245
        }
246
    }
247
248
    return 0;
249
}
250
251
126
static int magy_decode_slice(AVCodecContext *avctx, void *tdata,
252
                             int j, int threadnr)
253
{
254
126
    MagicYUVContext *s = avctx->priv_data;
255
126
    int interlaced = s->interlaced;
256
126
    AVFrame *p = s->p;
257
    int i, k, x, min_width;
258
    GetBitContext gb;
259
    uint8_t *dst;
260
261
504
    for (i = 0; i < s->planes; i++) {
262
        int left, lefttop, top;
263
378
        int height = AV_CEIL_RSHIFT(FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height), s->vshift[i]);
264
378
        int width = AV_CEIL_RSHIFT(avctx->coded_width, s->hshift[i]);
265
378
        int sheight = AV_CEIL_RSHIFT(s->slice_height, s->vshift[i]);
266
378
        ptrdiff_t fake_stride = p->linesize[i] * (1 + interlaced);
267
378
        ptrdiff_t stride = p->linesize[i];
268
378
        const uint8_t *slice = s->buf + s->slices[i][j].start;
269
        int flags, pred;
270
271
378
        flags = bytestream_get_byte(&slice);
272
378
        pred  = bytestream_get_byte(&slice);
273
274
378
        dst = p->data[i] + j * sheight * stride;
275
378
        if (flags & 1) {
276
            if (s->slices[i][j].size - 2 < width * height)
277
                return AVERROR_INVALIDDATA;
278
            for (k = 0; k < height; k++) {
279
                bytestream_get_buffer(&slice, dst, width);
280
                dst += stride;
281
            }
282
        } else {
283
378
            int ret = init_get_bits8(&gb, slice, s->slices[i][j].size - 2);
284
285
378
            if (ret < 0)
286
                return ret;
287
288
9658
            for (k = 0; k < height; k++) {
289
1794752
                for (x = 0; x < width; x++) {
290
                    int pix;
291
1785472
                    if (get_bits_left(&gb) <= 0)
292
                        return AVERROR_INVALIDDATA;
293
294
1785472
                    pix = get_vlc2(&gb, s->vlc[i].table, s->vlc[i].bits, 3);
295
1785472
                    if (pix < 0)
296
                        return AVERROR_INVALIDDATA;
297
298
1785472
                    dst[x] = pix;
299
                }
300
9280
                dst += stride;
301
            }
302
        }
303
304

378
        switch (pred) {
305
126
        case LEFT:
306
126
            dst = p->data[i] + j * sheight * stride;
307
126
            s->llviddsp.add_left_pred(dst, dst, width, 0);
308
126
            dst += stride;
309
126
            if (interlaced) {
310
72
                s->llviddsp.add_left_pred(dst, dst, width, 0);
311
72
                dst += stride;
312
            }
313
3176
            for (k = 1 + interlaced; k < height; k++) {
314
3050
                s->llviddsp.add_left_pred(dst, dst, width, dst[-fake_stride]);
315
3050
                dst += stride;
316
            }
317
126
            break;
318
90
        case GRADIENT:
319
90
            dst = p->data[i] + j * sheight * stride;
320
90
            s->llviddsp.add_left_pred(dst, dst, width, 0);
321
90
            dst += stride;
322
90
            if (interlaced) {
323
18
                s->llviddsp.add_left_pred(dst, dst, width, 0);
324
18
                dst += stride;
325
            }
326
90
            min_width = FFMIN(width, 32);
327
2302
            for (k = 1 + interlaced; k < height; k++) {
328
2212
                top = dst[-fake_stride];
329
2212
                left = top + dst[0];
330
2212
                dst[0] = left;
331
70784
                for (x = 1; x < min_width; x++) { /* dsp need aligned 32 */
332
68572
                    top = dst[x - fake_stride];
333
68572
                    lefttop = dst[x - (fake_stride + 1)];
334
68572
                    left += top - lefttop + dst[x];
335
68572
                    dst[x] = left;
336
                }
337
2212
                if (width > 32)
338
2212
                    s->llviddsp.add_gradient_pred(dst + 32, fake_stride, width - 32);
339
2212
                dst += stride;
340
            }
341
90
            break;
342
162
        case MEDIAN:
343
162
            dst = p->data[i] + j * sheight * stride;
344
162
            s->llviddsp.add_left_pred(dst, dst, width, 0);
345
162
            dst += stride;
346
162
            if (interlaced) {
347
54
                s->llviddsp.add_left_pred(dst, dst, width, 0);
348
54
                dst += stride;
349
            }
350
162
            lefttop = left = dst[0];
351
3658
            for (k = 1 + interlaced; k < height; k++) {
352
3496
                s->llviddsp.add_median_pred(dst, dst - fake_stride,
353
                                             dst, width, &left, &lefttop);
354
3496
                lefttop = left = dst[0];
355
3496
                dst += stride;
356
            }
357
162
            break;
358
        default:
359
            avpriv_request_sample(avctx, "Unknown prediction: %d", pred);
360
        }
361
    }
362
363
126
    if (s->decorrelate) {
364
36
        int height = FFMIN(s->slice_height, avctx->coded_height - j * s->slice_height);
365
36
        int width = avctx->coded_width;
366
36
        uint8_t *b = p->data[0] + j * s->slice_height * p->linesize[0];
367
36
        uint8_t *g = p->data[1] + j * s->slice_height * p->linesize[1];
368
36
        uint8_t *r = p->data[2] + j * s->slice_height * p->linesize[2];
369
370
964
        for (i = 0; i < height; i++) {
371
928
            s->llviddsp.add_bytes(b, g, width);
372
928
            s->llviddsp.add_bytes(r, g, width);
373
928
            b += p->linesize[0];
374
928
            g += p->linesize[1];
375
928
            r += p->linesize[2];
376
        }
377
    }
378
379
126
    return 0;
380
}
381
382
14
static int build_huffman(AVCodecContext *avctx, const uint8_t *table,
383
                         int table_size, int max)
384
{
385
14
    MagicYUVContext *s = avctx->priv_data;
386
    GetByteContext gb;
387
    HuffEntry he[4096];
388
14
    uint16_t length_count[33] = { 0 };
389
14
    int i = 0, j = 0, k;
390
391
14
    bytestream2_init(&gb, table, table_size);
392
393
1036
    while (bytestream2_get_bytes_left(&gb) > 0) {
394
1036
        int b = bytestream2_peek_byteu(&gb) &  0x80;
395
1036
        int x = bytestream2_get_byteu(&gb)  & ~0x80;
396
1036
        int l = 1;
397
398
1036
        if (b) {
399
424
            if (bytestream2_get_bytes_left(&gb) <= 0)
400
                break;
401
424
            l += bytestream2_get_byteu(&gb);
402
        }
403
1036
        k = j + l;
404

1036
        if (k > max || x == 0 || x > 32) {
405
            av_log(avctx, AV_LOG_ERROR, "Invalid Huffman codes\n");
406
            return AVERROR_INVALIDDATA;
407
        }
408
409
1036
        length_count[x] += l;
410
11788
        for (; j < k; j++)
411
10752
            he[j].len = x;
412
413
1036
        if (j == max) {
414
42
            j = 0;
415
42
            if (huff_build(he, length_count, &s->vlc[i], max)) {
416
                av_log(avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
417
                return AVERROR_INVALIDDATA;
418
            }
419
42
            i++;
420
42
            if (i == s->planes) {
421
14
                break;
422
            }
423
28
            memset(length_count, 0, sizeof(length_count));
424
        }
425
    }
426
427
14
    if (i != s->planes) {
428
        av_log(avctx, AV_LOG_ERROR, "Huffman tables too short\n");
429
        return AVERROR_INVALIDDATA;
430
    }
431
432
14
    return 0;
433
}
434
435
14
static int magy_decode_frame(AVCodecContext *avctx, void *data,
436
                             int *got_frame, AVPacket *avpkt)
437
{
438
14
    MagicYUVContext *s = avctx->priv_data;
439
14
    ThreadFrame frame = { .f = data };
440
14
    AVFrame *p = data;
441
    GetByteContext gb;
442
    uint32_t first_offset, offset, next_offset, header_size, slice_width;
443
    int width, height, format, version, table_size;
444
    int ret, i, j;
445
446
14
    if (avpkt->size < 36)
447
        return AVERROR_INVALIDDATA;
448
449
14
    bytestream2_init(&gb, avpkt->data, avpkt->size);
450
14
    if (bytestream2_get_le32u(&gb) != MKTAG('M', 'A', 'G', 'Y'))
451
        return AVERROR_INVALIDDATA;
452
453
14
    header_size = bytestream2_get_le32u(&gb);
454

14
    if (header_size < 32 || header_size >= avpkt->size) {
455
        av_log(avctx, AV_LOG_ERROR,
456
               "header or packet too small %"PRIu32"\n", header_size);
457
        return AVERROR_INVALIDDATA;
458
    }
459
460
14
    version = bytestream2_get_byteu(&gb);
461
14
    if (version != 7) {
462
        avpriv_request_sample(avctx, "Version %d", version);
463
        return AVERROR_PATCHWELCOME;
464
    }
465
466
14
    s->hshift[1] =
467
14
    s->vshift[1] =
468
14
    s->hshift[2] =
469
14
    s->vshift[2] = 0;
470
14
    s->decorrelate = 0;
471
14
    s->bps = 8;
472
473
14
    format = bytestream2_get_byteu(&gb);
474




14
    switch (format) {
475
2
    case 0x65:
476
2
        avctx->pix_fmt = AV_PIX_FMT_GBRP;
477
2
        s->decorrelate = 1;
478
2
        break;
479
2
    case 0x66:
480
2
        avctx->pix_fmt = AV_PIX_FMT_GBRAP;
481
2
        s->decorrelate = 1;
482
2
        break;
483
2
    case 0x67:
484
2
        avctx->pix_fmt = AV_PIX_FMT_YUV444P;
485
2
        break;
486
2
    case 0x68:
487
2
        avctx->pix_fmt = AV_PIX_FMT_YUV422P;
488
2
        s->hshift[1] =
489
2
        s->hshift[2] = 1;
490
2
        break;
491
2
    case 0x69:
492
2
        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
493
2
        s->hshift[1] =
494
2
        s->vshift[1] =
495
2
        s->hshift[2] =
496
2
        s->vshift[2] = 1;
497
2
        break;
498
2
    case 0x6a:
499
2
        avctx->pix_fmt = AV_PIX_FMT_YUVA444P;
500
2
        break;
501
2
    case 0x6b:
502
2
        avctx->pix_fmt = AV_PIX_FMT_GRAY8;
503
2
        break;
504
    case 0x6c:
505
        avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
506
        s->hshift[1] =
507
        s->hshift[2] = 1;
508
        s->bps = 10;
509
        break;
510
    case 0x76:
511
        avctx->pix_fmt = AV_PIX_FMT_YUV444P10;
512
        s->bps = 10;
513
        break;
514
    case 0x6d:
515
        avctx->pix_fmt = AV_PIX_FMT_GBRP10;
516
        s->decorrelate = 1;
517
        s->bps = 10;
518
        break;
519
    case 0x6e:
520
        avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
521
        s->decorrelate = 1;
522
        s->bps = 10;
523
        break;
524
    case 0x6f:
525
        avctx->pix_fmt = AV_PIX_FMT_GBRP12;
526
        s->decorrelate = 1;
527
        s->bps = 12;
528
        break;
529
    case 0x70:
530
        avctx->pix_fmt = AV_PIX_FMT_GBRAP12;
531
        s->decorrelate = 1;
532
        s->bps = 12;
533
        break;
534
    case 0x73:
535
        avctx->pix_fmt = AV_PIX_FMT_GRAY10;
536
        s->bps = 10;
537
        break;
538
    case 0x7b:
539
        avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
540
        s->hshift[1] =
541
        s->vshift[1] =
542
        s->hshift[2] =
543
        s->vshift[2] = 1;
544
        s->bps = 10;
545
        break;
546
    default:
547
        avpriv_request_sample(avctx, "Format 0x%X", format);
548
        return AVERROR_PATCHWELCOME;
549
    }
550
14
    s->max = 1 << s->bps;
551
14
    s->magy_decode_slice = s->bps == 8 ? magy_decode_slice : magy_decode_slice10;
552
14
    s->planes = av_pix_fmt_count_planes(avctx->pix_fmt);
553
554
14
    bytestream2_skipu(&gb, 1);
555
14
    s->color_matrix = bytestream2_get_byteu(&gb);
556
14
    s->flags        = bytestream2_get_byteu(&gb);
557
14
    s->interlaced   = !!(s->flags & 2);
558
14
    bytestream2_skipu(&gb, 3);
559
560
14
    width  = bytestream2_get_le32u(&gb);
561
14
    height = bytestream2_get_le32u(&gb);
562
14
    ret = ff_set_dimensions(avctx, width, height);
563
14
    if (ret < 0)
564
        return ret;
565
566
14
    slice_width = bytestream2_get_le32u(&gb);
567
14
    if (slice_width != avctx->coded_width) {
568
        avpriv_request_sample(avctx, "Slice width %"PRIu32, slice_width);
569
        return AVERROR_PATCHWELCOME;
570
    }
571
14
    s->slice_height = bytestream2_get_le32u(&gb);
572

14
    if (s->slice_height <= 0 || s->slice_height > INT_MAX - avctx->coded_height) {
573
        av_log(avctx, AV_LOG_ERROR,
574
               "invalid slice height: %d\n", s->slice_height);
575
        return AVERROR_INVALIDDATA;
576
    }
577
578
14
    bytestream2_skipu(&gb, 4);
579
580
14
    s->nb_slices = (avctx->coded_height + s->slice_height - 1) / s->slice_height;
581
14
    if (s->nb_slices > INT_MAX / FFMAX(sizeof(Slice), 4 * 5)) {
582
        av_log(avctx, AV_LOG_ERROR,
583
               "invalid number of slices: %d\n", s->nb_slices);
584
        return AVERROR_INVALIDDATA;
585
    }
586
587
14
    if (s->interlaced) {
588
6
        if ((s->slice_height >> s->vshift[1]) < 2) {
589
            av_log(avctx, AV_LOG_ERROR, "impossible slice height\n");
590
            return AVERROR_INVALIDDATA;
591
        }
592

6
        if ((avctx->coded_height % s->slice_height) && ((avctx->coded_height % s->slice_height) >> s->vshift[1]) < 2) {
593
            av_log(avctx, AV_LOG_ERROR, "impossible height\n");
594
            return AVERROR_INVALIDDATA;
595
        }
596
    }
597
598
14
    if (bytestream2_get_bytes_left(&gb) <= s->nb_slices * s->planes * 5)
599
        return AVERROR_INVALIDDATA;
600
56
    for (i = 0; i < s->planes; i++) {
601
42
        av_fast_malloc(&s->slices[i], &s->slices_size[i], s->nb_slices * sizeof(Slice));
602
42
        if (!s->slices[i])
603
            return AVERROR(ENOMEM);
604
605
42
        offset = bytestream2_get_le32u(&gb);
606
42
        if (offset >= avpkt->size - header_size)
607
            return AVERROR_INVALIDDATA;
608
609
42
        if (i == 0)
610
14
            first_offset = offset;
611
612
378
        for (j = 0; j < s->nb_slices - 1; j++) {
613
336
            s->slices[i][j].start = offset + header_size;
614
615
336
            next_offset = bytestream2_get_le32u(&gb);
616

336
            if (next_offset <= offset || next_offset >= avpkt->size - header_size)
617
                return AVERROR_INVALIDDATA;
618
619
336
            s->slices[i][j].size = next_offset - offset;
620
336
            if (s->slices[i][j].size < 2)
621
                return AVERROR_INVALIDDATA;
622
336
            offset = next_offset;
623
        }
624
625
42
        s->slices[i][j].start = offset + header_size;
626
42
        s->slices[i][j].size  = avpkt->size - s->slices[i][j].start;
627
628
42
        if (s->slices[i][j].size < 2)
629
            return AVERROR_INVALIDDATA;
630
    }
631
632
14
    if (bytestream2_get_byteu(&gb) != s->planes)
633
        return AVERROR_INVALIDDATA;
634
635
14
    bytestream2_skipu(&gb, s->nb_slices * s->planes);
636
637
14
    table_size = header_size + first_offset - bytestream2_tell(&gb);
638
14
    if (table_size < 2)
639
        return AVERROR_INVALIDDATA;
640
641
14
    ret = build_huffman(avctx, avpkt->data + bytestream2_tell(&gb),
642
                        table_size, s->max);
643
14
    if (ret < 0)
644
        return ret;
645
646
14
    p->pict_type = AV_PICTURE_TYPE_I;
647
14
    p->key_frame = 1;
648
649
14
    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
650
        return ret;
651
652
14
    s->buf = avpkt->data;
653
14
    s->p = p;
654
14
    avctx->execute2(avctx, s->magy_decode_slice, NULL, NULL, s->nb_slices);
655
656
14
    if (avctx->pix_fmt == AV_PIX_FMT_GBRP   ||
657
12
        avctx->pix_fmt == AV_PIX_FMT_GBRAP  ||
658
10
        avctx->pix_fmt == AV_PIX_FMT_GBRP10 ||
659
10
        avctx->pix_fmt == AV_PIX_FMT_GBRAP10||
660
10
        avctx->pix_fmt == AV_PIX_FMT_GBRAP12||
661
10
        avctx->pix_fmt == AV_PIX_FMT_GBRP12) {
662
4
        FFSWAP(uint8_t*, p->data[0], p->data[1]);
663
4
        FFSWAP(int, p->linesize[0], p->linesize[1]);
664
    } else {
665
10
        switch (s->color_matrix) {
666
        case 1:
667
            p->colorspace = AVCOL_SPC_BT470BG;
668
            break;
669
10
        case 2:
670
10
            p->colorspace = AVCOL_SPC_BT709;
671
10
            break;
672
        }
673
10
        p->color_range = (s->flags & 4) ? AVCOL_RANGE_JPEG : AVCOL_RANGE_MPEG;
674
    }
675
676
14
    *got_frame = 1;
677
678
14
    return avpkt->size;
679
}
680
681
14
static av_cold int magy_decode_init(AVCodecContext *avctx)
682
{
683
14
    MagicYUVContext *s = avctx->priv_data;
684
14
    ff_llviddsp_init(&s->llviddsp);
685
14
    return 0;
686
}
687
688
14
static av_cold int magy_decode_end(AVCodecContext *avctx)
689
{
690
14
    MagicYUVContext * const s = avctx->priv_data;
691
    int i;
692
693
70
    for (i = 0; i < FF_ARRAY_ELEMS(s->slices); i++) {
694
56
        av_freep(&s->slices[i]);
695
56
        s->slices_size[i] = 0;
696
56
        ff_free_vlc(&s->vlc[i]);
697
    }
698
699
14
    return 0;
700
}
701
702
AVCodec ff_magicyuv_decoder = {
703
    .name             = "magicyuv",
704
    .long_name        = NULL_IF_CONFIG_SMALL("MagicYUV video"),
705
    .type             = AVMEDIA_TYPE_VIDEO,
706
    .id               = AV_CODEC_ID_MAGICYUV,
707
    .priv_data_size   = sizeof(MagicYUVContext),
708
    .init             = magy_decode_init,
709
    .close            = magy_decode_end,
710
    .decode           = magy_decode_frame,
711
    .capabilities     = AV_CODEC_CAP_DR1 |
712
                        AV_CODEC_CAP_FRAME_THREADS |
713
                        AV_CODEC_CAP_SLICE_THREADS,
714
    .caps_internal    = FF_CODEC_CAP_INIT_THREADSAFE,
715
};