GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/utvideodec.c Lines: 373 663 56.3 %
Date: 2020-10-23 17:01:47 Branches: 163 317 51.4 %

Line Branch Exec Source
1
/*
2
 * Ut Video decoder
3
 * Copyright (c) 2011 Konstantin Shishkov
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
/**
23
 * @file
24
 * Ut Video decoder
25
 */
26
27
#include <inttypes.h>
28
#include <stdlib.h>
29
30
#define CACHED_BITSTREAM_READER !ARCH_X86_32
31
#define UNCHECKED_BITSTREAM_READER 1
32
33
#include "libavutil/intreadwrite.h"
34
#include "libavutil/pixdesc.h"
35
#include "avcodec.h"
36
#include "bswapdsp.h"
37
#include "bytestream.h"
38
#include "get_bits.h"
39
#include "internal.h"
40
#include "thread.h"
41
#include "utvideo.h"
42
43
183
static int build_huff(const uint8_t *src, VLC *vlc, int *fsym, unsigned nb_elems)
44
{
45
    int i;
46
    uint32_t codes[1024];
47
    uint8_t bits[1024];
48
183
    uint16_t codes_count[33] = { 0 };
49
50
183
    *fsym = -1;
51
46566
    for (i = 0; i < nb_elems; i++) {
52
46387
        if (src[i] == 0) {
53
4
            *fsym = i;
54
4
            return 0;
55
46383
        } else if (src[i] == 255) {
56
18630
            bits[i] = 0;
57
27753
        } else if (src[i] <= 32) {
58
27753
            bits[i] = src[i];
59
        } else
60
            return AVERROR_INVALIDDATA;
61
62
46383
        codes_count[bits[i]]++;
63
    }
64
179
    if (codes_count[0] == nb_elems)
65
        return AVERROR_INVALIDDATA;
66
67
5907
    for (unsigned i = 32, nb_codes = 0; i > 0; i--) {
68
5728
        uint16_t curr = codes_count[i];   // # of leafs of length i
69
5728
        codes_count[i] = nb_codes / 2;    // # of non-leaf nodes on level i
70
5728
        nb_codes = codes_count[i] + curr; // # of nodes on level i
71
    }
72
73
46003
    for (unsigned i = nb_elems; i-- > 0;) {
74
45824
        if (!bits[i]) {
75
18071
            codes[i] = 0;
76
18071
            continue;
77
        }
78
27753
        codes[i] = codes_count[bits[i]]++;
79
    }
80
#define VLC_BITS 11
81
179
    return init_vlc(vlc, VLC_BITS, nb_elems,
82
                    bits,  sizeof(*bits),  sizeof(*bits),
83
                    codes, sizeof(*codes), sizeof(*codes), 0);
84
}
85
86
static int decode_plane10(UtvideoContext *c, int plane_no,
87
                          uint16_t *dst, ptrdiff_t stride,
88
                          int width, int height,
89
                          const uint8_t *src, const uint8_t *huff,
90
                          int use_pred)
91
{
92
    int i, j, slice, pix, ret;
93
    int sstart, send;
94
    VLC vlc;
95
    GetBitContext gb;
96
    int prev, fsym;
97
98
    if ((ret = build_huff(huff, &vlc, &fsym, 1024)) < 0) {
99
        av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
100
        return ret;
101
    }
102
    if (fsym >= 0) { // build_huff reported a symbol to fill slices with
103
        send = 0;
104
        for (slice = 0; slice < c->slices; slice++) {
105
            uint16_t *dest;
106
107
            sstart = send;
108
            send   = (height * (slice + 1) / c->slices);
109
            dest   = dst + sstart * stride;
110
111
            prev = 0x200;
112
            for (j = sstart; j < send; j++) {
113
                for (i = 0; i < width; i++) {
114
                    pix = fsym;
115
                    if (use_pred) {
116
                        prev += pix;
117
                        prev &= 0x3FF;
118
                        pix   = prev;
119
                    }
120
                    dest[i] = pix;
121
                }
122
                dest += stride;
123
            }
124
        }
125
        return 0;
126
    }
127
128
    send = 0;
129
    for (slice = 0; slice < c->slices; slice++) {
130
        uint16_t *dest;
131
        int slice_data_start, slice_data_end, slice_size;
132
133
        sstart = send;
134
        send   = (height * (slice + 1) / c->slices);
135
        dest   = dst + sstart * stride;
136
137
        // slice offset and size validation was done earlier
138
        slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
139
        slice_data_end   = AV_RL32(src + slice * 4);
140
        slice_size       = slice_data_end - slice_data_start;
141
142
        if (!slice_size) {
143
            av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
144
                   "yet a slice has a length of zero.\n");
145
            goto fail;
146
        }
147
148
        memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
149
        c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
150
                          (uint32_t *)(src + slice_data_start + c->slices * 4),
151
                          (slice_data_end - slice_data_start + 3) >> 2);
152
        init_get_bits(&gb, c->slice_bits, slice_size * 8);
153
154
        prev = 0x200;
155
        for (j = sstart; j < send; j++) {
156
            for (i = 0; i < width; i++) {
157
                pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
158
                if (pix < 0) {
159
                    av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
160
                    goto fail;
161
                }
162
                if (use_pred) {
163
                    prev += pix;
164
                    prev &= 0x3FF;
165
                    pix   = prev;
166
                }
167
                dest[i] = pix;
168
            }
169
            dest += stride;
170
            if (get_bits_left(&gb) < 0) {
171
                av_log(c->avctx, AV_LOG_ERROR,
172
                        "Slice decoding ran out of bits\n");
173
                goto fail;
174
            }
175
        }
176
        if (get_bits_left(&gb) > 32)
177
            av_log(c->avctx, AV_LOG_WARNING,
178
                   "%d bits left after decoding slice\n", get_bits_left(&gb));
179
    }
180
181
    ff_free_vlc(&vlc);
182
183
    return 0;
184
fail:
185
    ff_free_vlc(&vlc);
186
    return AVERROR_INVALIDDATA;
187
}
188
189
183
static int compute_cmask(int plane_no, int interlaced, enum AVPixelFormat pix_fmt)
190
{
191

183
    const int is_luma = (pix_fmt == AV_PIX_FMT_YUV420P) && !plane_no;
192
193
183
    if (interlaced)
194
24
        return ~(1 + 2 * is_luma);
195
196
159
    return ~is_luma;
197
}
198
199
183
static int decode_plane(UtvideoContext *c, int plane_no,
200
                        uint8_t *dst, ptrdiff_t stride,
201
                        int width, int height,
202
                        const uint8_t *src, int use_pred)
203
{
204
    int i, j, slice, pix;
205
    int sstart, send;
206
    VLC vlc;
207
    GetBitContext gb;
208
    int ret, prev, fsym;
209
183
    const int cmask = compute_cmask(plane_no, c->interlaced, c->avctx->pix_fmt);
210
211
183
    if (c->pack) {
212
        send = 0;
213
        for (slice = 0; slice < c->slices; slice++) {
214
            GetBitContext cbit, pbit;
215
            uint8_t *dest, *p;
216
217
            ret = init_get_bits8_le(&cbit, c->control_stream[plane_no][slice], c->control_stream_size[plane_no][slice]);
218
            if (ret < 0)
219
                return ret;
220
221
            ret = init_get_bits8_le(&pbit, c->packed_stream[plane_no][slice], c->packed_stream_size[plane_no][slice]);
222
            if (ret < 0)
223
                return ret;
224
225
            sstart = send;
226
            send   = (height * (slice + 1) / c->slices) & cmask;
227
            dest   = dst + sstart * stride;
228
229
            if (3 * ((dst + send * stride - dest + 7)/8) > get_bits_left(&cbit))
230
                return AVERROR_INVALIDDATA;
231
232
            for (p = dest; p < dst + send * stride; p += 8) {
233
                int bits = get_bits_le(&cbit, 3);
234
235
                if (bits == 0) {
236
                    *(uint64_t *) p = 0;
237
                } else {
238
                    uint32_t sub = 0x80 >> (8 - (bits + 1)), add;
239
                    int k;
240
241
                    if ((bits + 1) * 8 > get_bits_left(&pbit))
242
                        return AVERROR_INVALIDDATA;
243
244
                    for (k = 0; k < 8; k++) {
245
246
                        p[k] = get_bits_le(&pbit, bits + 1);
247
                        add = (~p[k] & sub) << (8 - bits);
248
                        p[k] -= sub;
249
                        p[k] += add;
250
                    }
251
                }
252
            }
253
        }
254
255
        return 0;
256
    }
257
258
183
    if (build_huff(src, &vlc, &fsym, 256)) {
259
        av_log(c->avctx, AV_LOG_ERROR, "Cannot build Huffman codes\n");
260
        return AVERROR_INVALIDDATA;
261
    }
262
183
    if (fsym >= 0) { // build_huff reported a symbol to fill slices with
263
4
        send = 0;
264
20
        for (slice = 0; slice < c->slices; slice++) {
265
            uint8_t *dest;
266
267
16
            sstart = send;
268
16
            send   = (height * (slice + 1) / c->slices) & cmask;
269
16
            dest   = dst + sstart * stride;
270
271
16
            prev = 0x80;
272
3088
            for (j = sstart; j < send; j++) {
273
3148800
                for (i = 0; i < width; i++) {
274
3145728
                    pix = fsym;
275
3145728
                    if (use_pred) {
276
                        prev += (unsigned)pix;
277
                        pix   = prev;
278
                    }
279
3145728
                    dest[i] = pix;
280
                }
281
3072
                dest += stride;
282
            }
283
        }
284
4
        return 0;
285
    }
286
287
179
    src      += 256;
288
289
179
    send = 0;
290
895
    for (slice = 0; slice < c->slices; slice++) {
291
        uint8_t *dest;
292
        int slice_data_start, slice_data_end, slice_size;
293
294
716
        sstart = send;
295
716
        send   = (height * (slice + 1) / c->slices) & cmask;
296
716
        dest   = dst + sstart * stride;
297
298
        // slice offset and size validation was done earlier
299
716
        slice_data_start = slice ? AV_RL32(src + slice * 4 - 4) : 0;
300
716
        slice_data_end   = AV_RL32(src + slice * 4);
301
716
        slice_size       = slice_data_end - slice_data_start;
302
303
716
        if (!slice_size) {
304
            av_log(c->avctx, AV_LOG_ERROR, "Plane has more than one symbol "
305
                   "yet a slice has a length of zero.\n");
306
            goto fail;
307
        }
308
309
716
        memset(c->slice_bits + slice_size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
310
716
        c->bdsp.bswap_buf((uint32_t *) c->slice_bits,
311
716
                          (uint32_t *)(src + slice_data_start + c->slices * 4),
312
716
                          (slice_data_end - slice_data_start + 3) >> 2);
313
716
        init_get_bits(&gb, c->slice_bits, slice_size * 8);
314
315
716
        prev = 0x80;
316
63858
        for (j = sstart; j < send; j++) {
317
35325686
            for (i = 0; i < width; i++) {
318
35262544
                pix = get_vlc2(&gb, vlc.table, VLC_BITS, 3);
319
35262544
                if (pix < 0) {
320
                    av_log(c->avctx, AV_LOG_ERROR, "Decoding error\n");
321
                    goto fail;
322
                }
323
35262544
                if (use_pred) {
324
15513600
                    prev += pix;
325
15513600
                    pix   = prev;
326
                }
327
35262544
                dest[i] = pix;
328
            }
329
63142
            if (get_bits_left(&gb) < 0) {
330
                av_log(c->avctx, AV_LOG_ERROR,
331
                        "Slice decoding ran out of bits\n");
332
                goto fail;
333
            }
334
63142
            dest += stride;
335
        }
336
716
        if (get_bits_left(&gb) > 32)
337
            av_log(c->avctx, AV_LOG_WARNING,
338
                   "%d bits left after decoding slice\n", get_bits_left(&gb));
339
    }
340
341
179
    ff_free_vlc(&vlc);
342
343
179
    return 0;
344
fail:
345
    ff_free_vlc(&vlc);
346
    return AVERROR_INVALIDDATA;
347
}
348
349
#undef A
350
#undef B
351
#undef C
352
353
77
static void restore_median_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
354
                                  int width, int height, int slices, int rmode)
355
{
356
    int i, j, slice;
357
    int A, B, C;
358
    uint8_t *bsrc;
359
    int slice_start, slice_height;
360
77
    const int cmask = ~rmode;
361
362
385
    for (slice = 0; slice < slices; slice++) {
363
308
        slice_start  = ((slice * height) / slices) & cmask;
364
308
        slice_height = ((((slice + 1) * height) / slices) & cmask) -
365
                       slice_start;
366
367
308
        if (!slice_height)
368
            continue;
369
308
        bsrc = src + slice_start * stride;
370
371
        // first line - left neighbour prediction
372
308
        bsrc[0] += 0x80;
373
308
        c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
374
308
        bsrc += stride;
375
308
        if (slice_height <= 1)
376
            continue;
377
        // second line - first element has top prediction, the rest uses median
378
308
        C        = bsrc[-stride];
379
308
        bsrc[0] += C;
380
308
        A        = bsrc[0];
381
4928
        for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
382
4620
            B        = bsrc[i - stride];
383
4620
            bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
384
4620
            C        = B;
385
4620
            A        = bsrc[i];
386
        }
387
308
        if (width > 16)
388
308
            c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride + 16,
389
308
                                        bsrc + 16, width - 16, &A, &B);
390
391
308
        bsrc += stride;
392
        // the rest of lines use continuous median prediction
393
33436
        for (j = 2; j < slice_height; j++) {
394
33128
            c->llviddsp.add_median_pred(bsrc, bsrc - stride,
395
                                            bsrc, width, &A, &B);
396
33128
            bsrc += stride;
397
        }
398
    }
399
77
}
400
401
/* UtVideo interlaced mode treats every two lines as a single one,
402
 * so restoring function should take care of possible padding between
403
 * two parts of the same "line".
404
 */
405
12
static void restore_median_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
406
                                     int width, int height, int slices, int rmode)
407
{
408
    int i, j, slice;
409
    int A, B, C;
410
    uint8_t *bsrc;
411
    int slice_start, slice_height;
412
12
    const int cmask   = ~(rmode ? 3 : 1);
413
12
    const ptrdiff_t stride2 = stride << 1;
414
415
60
    for (slice = 0; slice < slices; slice++) {
416
48
        slice_start    = ((slice * height) / slices) & cmask;
417
48
        slice_height   = ((((slice + 1) * height) / slices) & cmask) -
418
                         slice_start;
419
48
        slice_height >>= 1;
420
48
        if (!slice_height)
421
            continue;
422
423
48
        bsrc = src + slice_start * stride;
424
425
        // first line - left neighbour prediction
426
48
        bsrc[0] += 0x80;
427
48
        A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
428
48
        c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
429
48
        bsrc += stride2;
430
48
        if (slice_height <= 1)
431
            continue;
432
        // second line - first element has top prediction, the rest uses median
433
48
        C        = bsrc[-stride2];
434
48
        bsrc[0] += C;
435
48
        A        = bsrc[0];
436
768
        for (i = 1; i < FFMIN(width, 16); i++) { /* scalar loop (DSP need align 16) */
437
720
            B        = bsrc[i - stride2];
438
720
            bsrc[i] += mid_pred(A, B, (uint8_t)(A + B - C));
439
720
            C        = B;
440
720
            A        = bsrc[i];
441
        }
442
48
        if (width > 16)
443
48
            c->llviddsp.add_median_pred(bsrc + 16, bsrc - stride2 + 16,
444
48
                                        bsrc + 16, width - 16, &A, &B);
445
446
48
        c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
447
48
                                        bsrc + stride, width, &A, &B);
448
48
        bsrc += stride2;
449
        // the rest of lines use continuous median prediction
450
216
        for (j = 2; j < slice_height; j++) {
451
168
            c->llviddsp.add_median_pred(bsrc, bsrc - stride2,
452
                                            bsrc, width, &A, &B);
453
168
            c->llviddsp.add_median_pred(bsrc + stride, bsrc - stride,
454
168
                                            bsrc + stride, width, &A, &B);
455
168
            bsrc += stride2;
456
        }
457
    }
458
12
}
459
460
13
static void restore_gradient_planar(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
461
                                    int width, int height, int slices, int rmode)
462
{
463
    int i, j, slice;
464
    int A, B, C;
465
    uint8_t *bsrc;
466
    int slice_start, slice_height;
467
13
    const int cmask = ~rmode;
468
13
    int min_width = FFMIN(width, 32);
469
470
65
    for (slice = 0; slice < slices; slice++) {
471
52
        slice_start  = ((slice * height) / slices) & cmask;
472
52
        slice_height = ((((slice + 1) * height) / slices) & cmask) -
473
                       slice_start;
474
475
52
        if (!slice_height)
476
            continue;
477
52
        bsrc = src + slice_start * stride;
478
479
        // first line - left neighbour prediction
480
52
        bsrc[0] += 0x80;
481
52
        c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
482
52
        bsrc += stride;
483
52
        if (slice_height <= 1)
484
            continue;
485
502
        for (j = 1; j < slice_height; j++) {
486
            // second line - first element has top prediction, the rest uses gradient
487
450
            bsrc[0] = (bsrc[0] + bsrc[-stride]) & 0xFF;
488
14400
            for (i = 1; i < min_width; i++) { /* dsp need align 32 */
489
13950
                A = bsrc[i - stride];
490
13950
                B = bsrc[i - (stride + 1)];
491
13950
                C = bsrc[i - 1];
492
13950
                bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
493
            }
494
450
            if (width > 32)
495
322
                c->llviddsp.add_gradient_pred(bsrc + 32, stride, width - 32);
496
450
            bsrc += stride;
497
        }
498
    }
499
13
}
500
501
12
static void restore_gradient_planar_il(UtvideoContext *c, uint8_t *src, ptrdiff_t stride,
502
                                      int width, int height, int slices, int rmode)
503
{
504
    int i, j, slice;
505
    int A, B, C;
506
    uint8_t *bsrc;
507
    int slice_start, slice_height;
508
12
    const int cmask   = ~(rmode ? 3 : 1);
509
12
    const ptrdiff_t stride2 = stride << 1;
510
12
    int min_width = FFMIN(width, 32);
511
512
60
    for (slice = 0; slice < slices; slice++) {
513
48
        slice_start    = ((slice * height) / slices) & cmask;
514
48
        slice_height   = ((((slice + 1) * height) / slices) & cmask) -
515
                         slice_start;
516
48
        slice_height >>= 1;
517
48
        if (!slice_height)
518
            continue;
519
520
48
        bsrc = src + slice_start * stride;
521
522
        // first line - left neighbour prediction
523
48
        bsrc[0] += 0x80;
524
48
        A = c->llviddsp.add_left_pred(bsrc, bsrc, width, 0);
525
48
        c->llviddsp.add_left_pred(bsrc + stride, bsrc + stride, width, A);
526
48
        bsrc += stride2;
527
48
        if (slice_height <= 1)
528
            continue;
529
264
        for (j = 1; j < slice_height; j++) {
530
            // second line - first element has top prediction, the rest uses gradient
531
216
            bsrc[0] = (bsrc[0] + bsrc[-stride2]) & 0xFF;
532
6912
            for (i = 1; i < min_width; i++) { /* dsp need align 32 */
533
6696
                A = bsrc[i - stride2];
534
6696
                B = bsrc[i - (stride2 + 1)];
535
6696
                C = bsrc[i - 1];
536
6696
                bsrc[i] = (A - B + C + bsrc[i]) & 0xFF;
537
            }
538
216
            if (width > 32)
539
160
                c->llviddsp.add_gradient_pred(bsrc + 32, stride2, width - 32);
540
541
216
            A = bsrc[-stride];
542
216
            B = bsrc[-(1 + stride + stride - width)];
543
216
            C = bsrc[width - 1];
544
216
            bsrc[stride] = (A - B + C + bsrc[stride]) & 0xFF;
545
12032
            for (i = 1; i < width; i++) {
546
11816
                A = bsrc[i - stride];
547
11816
                B = bsrc[i - (1 + stride)];
548
11816
                C = bsrc[i - 1 + stride];
549
11816
                bsrc[i + stride] = (A - B + C + bsrc[i + stride]) & 0xFF;
550
            }
551
216
            bsrc += stride2;
552
        }
553
    }
554
12
}
555
556
57
static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
557
                        AVPacket *avpkt)
558
{
559
57
    const uint8_t *buf = avpkt->data;
560
57
    int buf_size = avpkt->size;
561
57
    UtvideoContext *c = avctx->priv_data;
562
    int i, j;
563
    const uint8_t *plane_start[5];
564
57
    int plane_size, max_slice_size = 0, slice_start, slice_end, slice_size;
565
    int ret;
566
    GetByteContext gb;
567
57
    ThreadFrame frame = { .f = data };
568
569
57
    if ((ret = ff_thread_get_buffer(avctx, &frame, 0)) < 0)
570
        return ret;
571
572
    /* parse plane structure to get frame flags and validate slice offsets */
573
57
    bytestream2_init(&gb, buf, buf_size);
574
575
57
    if (c->pack) {
576
        const uint8_t *packed_stream;
577
        const uint8_t *control_stream;
578
        GetByteContext pb;
579
        uint32_t nb_cbs;
580
        int left;
581
582
        c->frame_info = PRED_GRADIENT << 8;
583
584
        if (bytestream2_get_byte(&gb) != 1)
585
            return AVERROR_INVALIDDATA;
586
        bytestream2_skip(&gb, 3);
587
        c->offset = bytestream2_get_le32(&gb);
588
589
        if (buf_size <= c->offset + 8LL)
590
            return AVERROR_INVALIDDATA;
591
592
        bytestream2_init(&pb, buf + 8 + c->offset, buf_size - 8 - c->offset);
593
594
        nb_cbs = bytestream2_get_le32(&pb);
595
        if (nb_cbs > c->offset)
596
            return AVERROR_INVALIDDATA;
597
598
        packed_stream = buf + 8;
599
        control_stream = packed_stream + (c->offset - nb_cbs);
600
        left = control_stream - packed_stream;
601
602
        for (i = 0; i < c->planes; i++) {
603
            for (j = 0; j < c->slices; j++) {
604
                c->packed_stream[i][j] = packed_stream;
605
                c->packed_stream_size[i][j] = bytestream2_get_le32(&pb);
606
                if (c->packed_stream_size[i][j] > left)
607
                    return AVERROR_INVALIDDATA;
608
                left -= c->packed_stream_size[i][j];
609
                packed_stream += c->packed_stream_size[i][j];
610
            }
611
        }
612
613
        left = buf + buf_size - control_stream;
614
615
        for (i = 0; i < c->planes; i++) {
616
            for (j = 0; j < c->slices; j++) {
617
                c->control_stream[i][j] = control_stream;
618
                c->control_stream_size[i][j] = bytestream2_get_le32(&pb);
619
                if (c->control_stream_size[i][j] > left)
620
                    return AVERROR_INVALIDDATA;
621
                left -= c->control_stream_size[i][j];
622
                control_stream += c->control_stream_size[i][j];
623
            }
624
        }
625
57
    } else if (c->pro) {
626
        if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
627
            av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
628
            return AVERROR_INVALIDDATA;
629
        }
630
        c->frame_info = bytestream2_get_le32u(&gb);
631
        c->slices = ((c->frame_info >> 16) & 0xff) + 1;
632
        for (i = 0; i < c->planes; i++) {
633
            plane_start[i] = gb.buffer;
634
            if (bytestream2_get_bytes_left(&gb) < 1024 + 4 * c->slices) {
635
                av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
636
                return AVERROR_INVALIDDATA;
637
            }
638
            slice_start = 0;
639
            slice_end   = 0;
640
            for (j = 0; j < c->slices; j++) {
641
                slice_end   = bytestream2_get_le32u(&gb);
642
                if (slice_end < 0 || slice_end < slice_start ||
643
                    bytestream2_get_bytes_left(&gb) < slice_end + 1024LL) {
644
                    av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
645
                    return AVERROR_INVALIDDATA;
646
                }
647
                slice_size  = slice_end - slice_start;
648
                slice_start = slice_end;
649
                max_slice_size = FFMAX(max_slice_size, slice_size);
650
            }
651
            plane_size = slice_end;
652
            bytestream2_skipu(&gb, plane_size);
653
            bytestream2_skipu(&gb, 1024);
654
        }
655
        plane_start[c->planes] = gb.buffer;
656
    } else {
657
240
        for (i = 0; i < c->planes; i++) {
658
183
            plane_start[i] = gb.buffer;
659
183
            if (bytestream2_get_bytes_left(&gb) < 256 + 4 * c->slices) {
660
                av_log(avctx, AV_LOG_ERROR, "Insufficient data for a plane\n");
661
                return AVERROR_INVALIDDATA;
662
            }
663
183
            bytestream2_skipu(&gb, 256);
664
183
            slice_start = 0;
665
183
            slice_end   = 0;
666
915
            for (j = 0; j < c->slices; j++) {
667
732
                slice_end   = bytestream2_get_le32u(&gb);
668

1464
                if (slice_end < 0 || slice_end < slice_start ||
669
732
                    bytestream2_get_bytes_left(&gb) < slice_end) {
670
                    av_log(avctx, AV_LOG_ERROR, "Incorrect slice size\n");
671
                    return AVERROR_INVALIDDATA;
672
                }
673
732
                slice_size  = slice_end - slice_start;
674
732
                slice_start = slice_end;
675
732
                max_slice_size = FFMAX(max_slice_size, slice_size);
676
            }
677
183
            plane_size = slice_end;
678
183
            bytestream2_skipu(&gb, plane_size);
679
        }
680
57
        plane_start[c->planes] = gb.buffer;
681
57
        if (bytestream2_get_bytes_left(&gb) < c->frame_info_size) {
682
            av_log(avctx, AV_LOG_ERROR, "Not enough data for frame information\n");
683
            return AVERROR_INVALIDDATA;
684
        }
685
57
        c->frame_info = bytestream2_get_le32u(&gb);
686
    }
687
57
    av_log(avctx, AV_LOG_DEBUG, "frame information flags %"PRIX32"\n",
688
           c->frame_info);
689
690
57
    c->frame_pred = (c->frame_info >> 8) & 3;
691
692
57
    max_slice_size += 4*avctx->width;
693
694
57
    if (!c->pack) {
695
57
        av_fast_malloc(&c->slice_bits, &c->slice_bits_size,
696
57
                       max_slice_size + AV_INPUT_BUFFER_PADDING_SIZE);
697
698
57
        if (!c->slice_bits) {
699
            av_log(avctx, AV_LOG_ERROR, "Cannot allocate temporary buffer\n");
700
            return AVERROR(ENOMEM);
701
        }
702
    }
703
704


57
    switch (c->avctx->pix_fmt) {
705
28
    case AV_PIX_FMT_GBRP:
706
    case AV_PIX_FMT_GBRAP:
707
124
        for (i = 0; i < c->planes; i++) {
708
96
            ret = decode_plane(c, i, frame.f->data[i],
709
96
                               frame.f->linesize[i], avctx->width,
710
                               avctx->height, plane_start[i],
711
96
                               c->frame_pred == PRED_LEFT);
712
96
            if (ret)
713
                return ret;
714
96
            if (c->frame_pred == PRED_MEDIAN) {
715
53
                if (!c->interlaced) {
716
50
                    restore_median_planar(c, frame.f->data[i],
717
50
                                          frame.f->linesize[i], avctx->width,
718
                                          avctx->height, c->slices, 0);
719
                } else {
720
3
                    restore_median_planar_il(c, frame.f->data[i],
721
3
                                             frame.f->linesize[i],
722
                                             avctx->width, avctx->height, c->slices,
723
                                             0);
724
                }
725
43
            } else if (c->frame_pred == PRED_GRADIENT) {
726
7
                if (!c->interlaced) {
727
4
                    restore_gradient_planar(c, frame.f->data[i],
728
4
                                            frame.f->linesize[i], avctx->width,
729
                                            avctx->height, c->slices, 0);
730
                } else {
731
3
                    restore_gradient_planar_il(c, frame.f->data[i],
732
3
                                               frame.f->linesize[i],
733
                                               avctx->width, avctx->height, c->slices,
734
                                               0);
735
                }
736
            }
737
        }
738
28
        c->utdsp.restore_rgb_planes(frame.f->data[2], frame.f->data[0], frame.f->data[1],
739
28
                                    frame.f->linesize[2], frame.f->linesize[0], frame.f->linesize[1],
740
                                    avctx->width, avctx->height);
741
28
        break;
742
    case AV_PIX_FMT_GBRAP10:
743
    case AV_PIX_FMT_GBRP10:
744
        for (i = 0; i < c->planes; i++) {
745
            ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i],
746
                                 frame.f->linesize[i] / 2, avctx->width,
747
                                 avctx->height, plane_start[i],
748
                                 plane_start[i + 1] - 1024,
749
                                 c->frame_pred == PRED_LEFT);
750
            if (ret)
751
                return ret;
752
        }
753
        c->utdsp.restore_rgb_planes10((uint16_t *)frame.f->data[2], (uint16_t *)frame.f->data[0], (uint16_t *)frame.f->data[1],
754
                                      frame.f->linesize[2] / 2, frame.f->linesize[0] / 2, frame.f->linesize[1] / 2,
755
                                      avctx->width, avctx->height);
756
        break;
757
14
    case AV_PIX_FMT_YUV420P:
758
56
        for (i = 0; i < 3; i++) {
759
42
            ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
760
42
                               avctx->width >> !!i, avctx->height >> !!i,
761
42
                               plane_start[i], c->frame_pred == PRED_LEFT);
762
42
            if (ret)
763
                return ret;
764
42
            if (c->frame_pred == PRED_MEDIAN) {
765
15
                if (!c->interlaced) {
766
12
                    restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
767
12
                                          avctx->width >> !!i, avctx->height >> !!i,
768
                                          c->slices, !i);
769
                } else {
770
3
                    restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
771
3
                                             avctx->width  >> !!i,
772
3
                                             avctx->height >> !!i,
773
                                             c->slices, !i);
774
                }
775
27
            } else if (c->frame_pred == PRED_GRADIENT) {
776
6
                if (!c->interlaced) {
777
3
                    restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
778
3
                                            avctx->width >> !!i, avctx->height >> !!i,
779
                                            c->slices, !i);
780
                } else {
781
3
                    restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
782
3
                                               avctx->width  >> !!i,
783
3
                                               avctx->height >> !!i,
784
                                               c->slices, !i);
785
                }
786
            }
787
        }
788
14
        break;
789
11
    case AV_PIX_FMT_YUV422P:
790
44
        for (i = 0; i < 3; i++) {
791
33
            ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
792
33
                               avctx->width >> !!i, avctx->height,
793
33
                               plane_start[i], c->frame_pred == PRED_LEFT);
794
33
            if (ret)
795
                return ret;
796
33
            if (c->frame_pred == PRED_MEDIAN) {
797
15
                if (!c->interlaced) {
798
12
                    restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
799
12
                                          avctx->width >> !!i, avctx->height,
800
                                          c->slices, 0);
801
                } else {
802
3
                    restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
803
3
                                             avctx->width >> !!i, avctx->height,
804
                                             c->slices, 0);
805
                }
806
18
            } else if (c->frame_pred == PRED_GRADIENT) {
807
6
                if (!c->interlaced) {
808
3
                    restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
809
3
                                            avctx->width >> !!i, avctx->height,
810
                                            c->slices, 0);
811
                } else {
812
3
                    restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
813
3
                                               avctx->width  >> !!i, avctx->height,
814
                                               c->slices, 0);
815
                }
816
            }
817
        }
818
11
        break;
819
4
    case AV_PIX_FMT_YUV444P:
820
16
        for (i = 0; i < 3; i++) {
821
12
            ret = decode_plane(c, i, frame.f->data[i], frame.f->linesize[i],
822
                               avctx->width, avctx->height,
823
12
                               plane_start[i], c->frame_pred == PRED_LEFT);
824
12
            if (ret)
825
                return ret;
826
12
            if (c->frame_pred == PRED_MEDIAN) {
827
6
                if (!c->interlaced) {
828
3
                    restore_median_planar(c, frame.f->data[i], frame.f->linesize[i],
829
                                          avctx->width, avctx->height,
830
                                          c->slices, 0);
831
                } else {
832
3
                    restore_median_planar_il(c, frame.f->data[i], frame.f->linesize[i],
833
                                             avctx->width, avctx->height,
834
                                             c->slices, 0);
835
                }
836
6
            } else if (c->frame_pred == PRED_GRADIENT) {
837
6
                if (!c->interlaced) {
838
3
                    restore_gradient_planar(c, frame.f->data[i], frame.f->linesize[i],
839
                                            avctx->width, avctx->height,
840
                                            c->slices, 0);
841
                } else {
842
3
                    restore_gradient_planar_il(c, frame.f->data[i], frame.f->linesize[i],
843
                                               avctx->width, avctx->height,
844
                                               c->slices, 0);
845
                }
846
            }
847
        }
848
4
        break;
849
    case AV_PIX_FMT_YUV420P10:
850
        for (i = 0; i < 3; i++) {
851
            ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], frame.f->linesize[i] / 2,
852
                                 avctx->width >> !!i, avctx->height >> !!i,
853
                                 plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
854
            if (ret)
855
                return ret;
856
        }
857
        break;
858
    case AV_PIX_FMT_YUV422P10:
859
        for (i = 0; i < 3; i++) {
860
            ret = decode_plane10(c, i, (uint16_t *)frame.f->data[i], frame.f->linesize[i] / 2,
861
                                 avctx->width >> !!i, avctx->height,
862
                                 plane_start[i], plane_start[i + 1] - 1024, c->frame_pred == PRED_LEFT);
863
            if (ret)
864
                return ret;
865
        }
866
        break;
867
    }
868
869
57
    frame.f->key_frame = 1;
870
57
    frame.f->pict_type = AV_PICTURE_TYPE_I;
871
57
    frame.f->interlaced_frame = !!c->interlaced;
872
873
57
    *got_frame = 1;
874
875
    /* always report that the buffer was completely consumed */
876
57
    return buf_size;
877
}
878
879
46
static av_cold int decode_init(AVCodecContext *avctx)
880
{
881
46
    UtvideoContext * const c = avctx->priv_data;
882
    int h_shift, v_shift;
883
884
46
    c->avctx = avctx;
885
886
46
    ff_utvideodsp_init(&c->utdsp);
887
46
    ff_bswapdsp_init(&c->bdsp);
888
46
    ff_llviddsp_init(&c->llviddsp);
889
890
46
    c->slice_bits_size = 0;
891
892




46
    switch (avctx->codec_tag) {
893
10
    case MKTAG('U', 'L', 'R', 'G'):
894
10
        c->planes      = 3;
895
10
        avctx->pix_fmt = AV_PIX_FMT_GBRP;
896
10
        break;
897
8
    case MKTAG('U', 'L', 'R', 'A'):
898
8
        c->planes      = 4;
899
8
        avctx->pix_fmt = AV_PIX_FMT_GBRAP;
900
8
        break;
901
4
    case MKTAG('U', 'L', 'Y', '0'):
902
4
        c->planes      = 3;
903
4
        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
904
4
        avctx->colorspace = AVCOL_SPC_BT470BG;
905
4
        break;
906
4
    case MKTAG('U', 'L', 'Y', '2'):
907
4
        c->planes      = 3;
908
4
        avctx->pix_fmt = AV_PIX_FMT_YUV422P;
909
4
        avctx->colorspace = AVCOL_SPC_BT470BG;
910
4
        break;
911
    case MKTAG('U', 'L', 'Y', '4'):
912
        c->planes      = 3;
913
        avctx->pix_fmt = AV_PIX_FMT_YUV444P;
914
        avctx->colorspace = AVCOL_SPC_BT470BG;
915
        break;
916
    case MKTAG('U', 'Q', 'Y', '0'):
917
        c->planes      = 3;
918
        c->pro         = 1;
919
        avctx->pix_fmt = AV_PIX_FMT_YUV420P10;
920
        break;
921
    case MKTAG('U', 'Q', 'Y', '2'):
922
        c->planes      = 3;
923
        c->pro         = 1;
924
        avctx->pix_fmt = AV_PIX_FMT_YUV422P10;
925
        break;
926
    case MKTAG('U', 'Q', 'R', 'G'):
927
        c->planes      = 3;
928
        c->pro         = 1;
929
        avctx->pix_fmt = AV_PIX_FMT_GBRP10;
930
        break;
931
    case MKTAG('U', 'Q', 'R', 'A'):
932
        c->planes      = 4;
933
        c->pro         = 1;
934
        avctx->pix_fmt = AV_PIX_FMT_GBRAP10;
935
        break;
936
6
    case MKTAG('U', 'L', 'H', '0'):
937
6
        c->planes      = 3;
938
6
        avctx->pix_fmt = AV_PIX_FMT_YUV420P;
939
6
        avctx->colorspace = AVCOL_SPC_BT709;
940
6
        break;
941
6
    case MKTAG('U', 'L', 'H', '2'):
942
6
        c->planes      = 3;
943
6
        avctx->pix_fmt = AV_PIX_FMT_YUV422P;
944
6
        avctx->colorspace = AVCOL_SPC_BT709;
945
6
        break;
946
8
    case MKTAG('U', 'L', 'H', '4'):
947
8
        c->planes      = 3;
948
8
        avctx->pix_fmt = AV_PIX_FMT_YUV444P;
949
8
        avctx->colorspace = AVCOL_SPC_BT709;
950
8
        break;
951
    case MKTAG('U', 'M', 'Y', '2'):
952
        c->planes      = 3;
953
        c->pack        = 1;
954
        avctx->pix_fmt = AV_PIX_FMT_YUV422P;
955
        avctx->colorspace = AVCOL_SPC_BT470BG;
956
        break;
957
    case MKTAG('U', 'M', 'H', '2'):
958
        c->planes      = 3;
959
        c->pack        = 1;
960
        avctx->pix_fmt = AV_PIX_FMT_YUV422P;
961
        avctx->colorspace = AVCOL_SPC_BT709;
962
        break;
963
    case MKTAG('U', 'M', 'Y', '4'):
964
        c->planes      = 3;
965
        c->pack        = 1;
966
        avctx->pix_fmt = AV_PIX_FMT_YUV444P;
967
        avctx->colorspace = AVCOL_SPC_BT470BG;
968
        break;
969
    case MKTAG('U', 'M', 'H', '4'):
970
        c->planes      = 3;
971
        c->pack        = 1;
972
        avctx->pix_fmt = AV_PIX_FMT_YUV444P;
973
        avctx->colorspace = AVCOL_SPC_BT709;
974
        break;
975
    case MKTAG('U', 'M', 'R', 'G'):
976
        c->planes      = 3;
977
        c->pack        = 1;
978
        avctx->pix_fmt = AV_PIX_FMT_GBRP;
979
        break;
980
    case MKTAG('U', 'M', 'R', 'A'):
981
        c->planes      = 4;
982
        c->pack        = 1;
983
        avctx->pix_fmt = AV_PIX_FMT_GBRAP;
984
        break;
985
    default:
986
        av_log(avctx, AV_LOG_ERROR, "Unknown Ut Video FOURCC provided (%08X)\n",
987
               avctx->codec_tag);
988
        return AVERROR_INVALIDDATA;
989
    }
990
991
46
    av_pix_fmt_get_chroma_sub_sample(avctx->pix_fmt, &h_shift, &v_shift);
992
46
    if ((avctx->width  & ((1<<h_shift)-1)) ||
993
46
        (avctx->height & ((1<<v_shift)-1))) {
994
        avpriv_request_sample(avctx, "Odd dimensions");
995
        return AVERROR_PATCHWELCOME;
996
    }
997
998

46
    if (c->pack && avctx->extradata_size >= 16) {
999
        av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1000
               avctx->extradata[3], avctx->extradata[2],
1001
               avctx->extradata[1], avctx->extradata[0]);
1002
        av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1003
               AV_RB32(avctx->extradata + 4));
1004
        c->compression = avctx->extradata[8];
1005
        if (c->compression != 2)
1006
            avpriv_request_sample(avctx, "Unknown compression type");
1007
        c->slices      = avctx->extradata[9] + 1;
1008

46
    } else if (!c->pro && avctx->extradata_size >= 16) {
1009
46
        av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1010
46
               avctx->extradata[3], avctx->extradata[2],
1011
46
               avctx->extradata[1], avctx->extradata[0]);
1012
46
        av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1013
46
               AV_RB32(avctx->extradata + 4));
1014
46
        c->frame_info_size = AV_RL32(avctx->extradata + 8);
1015
46
        c->flags           = AV_RL32(avctx->extradata + 12);
1016
1017
46
        if (c->frame_info_size != 4)
1018
            avpriv_request_sample(avctx, "Frame info not 4 bytes");
1019
46
        av_log(avctx, AV_LOG_DEBUG, "Encoding parameters %08"PRIX32"\n", c->flags);
1020
46
        c->slices      = (c->flags >> 24) + 1;
1021
46
        c->compression = c->flags & 1;
1022
46
        c->interlaced  = c->flags & 0x800;
1023
    } else if (c->pro && avctx->extradata_size == 8) {
1024
        av_log(avctx, AV_LOG_DEBUG, "Encoder version %d.%d.%d.%d\n",
1025
               avctx->extradata[3], avctx->extradata[2],
1026
               avctx->extradata[1], avctx->extradata[0]);
1027
        av_log(avctx, AV_LOG_DEBUG, "Original format %"PRIX32"\n",
1028
               AV_RB32(avctx->extradata + 4));
1029
        c->interlaced  = 0;
1030
        c->frame_info_size = 4;
1031
    } else {
1032
        av_log(avctx, AV_LOG_ERROR,
1033
               "Insufficient extradata size %d, should be at least 16\n",
1034
               avctx->extradata_size);
1035
        return AVERROR_INVALIDDATA;
1036
    }
1037
1038
46
    return 0;
1039
}
1040
1041
46
static av_cold int decode_end(AVCodecContext *avctx)
1042
{
1043
46
    UtvideoContext * const c = avctx->priv_data;
1044
1045
46
    av_freep(&c->slice_bits);
1046
1047
46
    return 0;
1048
}
1049
1050
AVCodec ff_utvideo_decoder = {
1051
    .name           = "utvideo",
1052
    .long_name      = NULL_IF_CONFIG_SMALL("Ut Video"),
1053
    .type           = AVMEDIA_TYPE_VIDEO,
1054
    .id             = AV_CODEC_ID_UTVIDEO,
1055
    .priv_data_size = sizeof(UtvideoContext),
1056
    .init           = decode_init,
1057
    .close          = decode_end,
1058
    .decode         = decode_frame,
1059
    .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
1060
    .caps_internal  = FF_CODEC_CAP_INIT_THREADSAFE,
1061
};