Coverage Report

Created: 2025-08-28 07:12

/src/ffmpeg/libavcodec/dnxhddec.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * VC3/DNxHD decoder.
3
 * Copyright (c) 2007 SmartJog S.A., Baptiste Coudurier <baptiste dot coudurier at smartjog dot com>
4
 * Copyright (c) 2011 MirriAd Ltd
5
 * Copyright (c) 2015 Christophe Gisquet
6
 *
7
 * 10 bit support added by MirriAd Ltd, Joseph Artsimovich <joseph@mirriad.com>
8
 * Slice multithreading and MB interlaced support added by Christophe Gisquet
9
 *
10
 * This file is part of FFmpeg.
11
 *
12
 * FFmpeg is free software; you can redistribute it and/or
13
 * modify it under the terms of the GNU Lesser General Public
14
 * License as published by the Free Software Foundation; either
15
 * version 2.1 of the License, or (at your option) any later version.
16
 *
17
 * FFmpeg is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20
 * Lesser General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Lesser General Public
23
 * License along with FFmpeg; if not, write to the Free Software
24
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25
 */
26
27
#include "libavutil/mem.h"
28
#include "libavutil/mem_internal.h"
29
#include "libavutil/pixdesc.h"
30
31
#include "avcodec.h"
32
#include "blockdsp.h"
33
#include "codec_internal.h"
34
#include "decode.h"
35
#define  UNCHECKED_BITSTREAM_READER 1
36
#include "get_bits.h"
37
#include "dnxhddata.h"
38
#include "idctdsp.h"
39
#include "profiles.h"
40
#include "thread.h"
41
42
typedef struct RowContext {
43
    DECLARE_ALIGNED(32, int16_t, blocks)[12][64];
44
    int luma_scale[64];
45
    int chroma_scale[64];
46
    GetBitContext gb;
47
    int last_dc[3];
48
    int last_qscale;
49
    int errors;
50
    /** -1:not set yet  0:off=RGB  1:on=YUV  2:variable */
51
    int format;
52
} RowContext;
53
54
typedef struct DNXHDContext {
55
    AVCodecContext *avctx;
56
    RowContext *rows;
57
    BlockDSPContext bdsp;
58
    const uint8_t* buf;
59
    int buf_size;
60
    int64_t cid;                        ///< compression id
61
    unsigned int width, height;
62
    enum AVPixelFormat pix_fmt;
63
    unsigned int mb_width, mb_height;
64
    uint32_t mb_scan_index[512];
65
    int data_offset;                    // End of mb_scan_index, where macroblocks start
66
    int cur_field;                      ///< current interlaced field
67
    VLC ac_vlc, dc_vlc, run_vlc;
68
    IDCTDSPContext idsp;
69
    uint8_t permutated_scantable[64];
70
    const CIDEntry *cid_table;
71
    int bit_depth; // 8, 10, 12 or 0 if not initialized at all.
72
    int is_444;
73
    int alpha;
74
    int lla;
75
    int mbaff;
76
    int act;
77
    int (*decode_dct_block)(const struct DNXHDContext *ctx,
78
                            RowContext *row, int n);
79
} DNXHDContext;
80
81
7.07k
#define DNXHD_VLC_BITS 9
82
#define DNXHD_DC_VLC_BITS 7
83
84
static int dnxhd_decode_dct_block_8(const DNXHDContext *ctx,
85
                                    RowContext *row, int n);
86
static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx,
87
                                     RowContext *row, int n);
88
static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx,
89
                                         RowContext *row, int n);
90
static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx,
91
                                     RowContext *row, int n);
92
static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx,
93
                                         RowContext *row, int n);
94
95
static av_cold int dnxhd_decode_init(AVCodecContext *avctx)
96
3.41k
{
97
3.41k
    DNXHDContext *ctx = avctx->priv_data;
98
99
3.41k
    ctx->avctx = avctx;
100
3.41k
    ctx->cid = -1;
101
3.41k
    if (avctx->colorspace == AVCOL_SPC_UNSPECIFIED) {
102
3.41k
        avctx->colorspace = AVCOL_SPC_BT709;
103
3.41k
    }
104
105
3.41k
    avctx->coded_width  = FFALIGN(avctx->width,  16);
106
3.41k
    avctx->coded_height = FFALIGN(avctx->height, 16);
107
108
3.41k
    ctx->rows = av_calloc(avctx->thread_count, sizeof(*ctx->rows));
109
3.41k
    if (!ctx->rows)
110
0
        return AVERROR(ENOMEM);
111
112
3.41k
    return 0;
113
3.41k
}
114
115
static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid, int bitdepth)
116
37.7k
{
117
37.7k
    int ret;
118
37.7k
    if (cid != ctx->cid) {
119
8.14k
        const CIDEntry *cid_table = ff_dnxhd_get_cid_table(cid);
120
121
8.14k
        if (!cid_table) {
122
853
            av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %"PRIu32"\n", cid);
123
853
            return AVERROR(ENOSYS);
124
853
        }
125
7.29k
        if (cid_table->bit_depth != bitdepth &&
126
7.29k
            cid_table->bit_depth != DNXHD_VARIABLE) {
127
212
            av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n",
128
212
                   cid_table->bit_depth, bitdepth);
129
212
            return AVERROR_INVALIDDATA;
130
212
        }
131
7.07k
        ctx->cid_table = cid_table;
132
7.07k
        av_log(ctx->avctx, AV_LOG_VERBOSE, "Profile cid %"PRIu32".\n", cid);
133
134
7.07k
        ff_vlc_free(&ctx->ac_vlc);
135
7.07k
        ff_vlc_free(&ctx->dc_vlc);
136
7.07k
        ff_vlc_free(&ctx->run_vlc);
137
138
7.07k
        if ((ret = vlc_init(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
139
7.07k
                 ctx->cid_table->ac_bits, 1, 1,
140
7.07k
                 ctx->cid_table->ac_codes, 2, 2, 0)) < 0)
141
0
            goto out;
142
7.07k
        if ((ret = vlc_init(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, bitdepth > 8 ? 14 : 12,
143
7.07k
                 ctx->cid_table->dc_bits, 1, 1,
144
7.07k
                 ctx->cid_table->dc_codes, 1, 1, 0)) < 0)
145
0
            goto out;
146
7.07k
        if ((ret = ff_vlc_init_sparse(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
147
7.07k
                 ctx->cid_table->run_bits, 1, 1,
148
7.07k
                 ctx->cid_table->run_codes, 2, 2,
149
7.07k
                 ctx->cid_table->run, 1, 1, 0)) < 0)
150
0
            goto out;
151
152
7.07k
        ctx->cid = cid;
153
7.07k
    }
154
36.6k
    ret = 0;
155
36.6k
out:
156
36.6k
    if (ret < 0)
157
0
        av_log(ctx->avctx, AV_LOG_ERROR, "vlc_init failed\n");
158
36.6k
    return ret;
159
36.6k
}
160
161
static int dnxhd_get_profile(int cid)
162
37.7k
{
163
37.7k
    switch(cid) {
164
24.6k
    case 1270:
165
24.6k
        return AV_PROFILE_DNXHR_444;
166
3.07k
    case 1271:
167
3.07k
        return AV_PROFILE_DNXHR_HQX;
168
1.45k
    case 1272:
169
1.45k
        return AV_PROFILE_DNXHR_HQ;
170
431
    case 1273:
171
431
        return AV_PROFILE_DNXHR_SQ;
172
203
    case 1274:
173
203
        return AV_PROFILE_DNXHR_LB;
174
37.7k
    }
175
7.94k
    return AV_PROFILE_DNXHD;
176
37.7k
}
177
178
static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
179
                               const uint8_t *buf, int buf_size,
180
                               int first_field)
181
145k
{
182
145k
    int i, cid, ret;
183
145k
    int old_bit_depth = ctx->bit_depth, bitdepth;
184
145k
    uint64_t header_prefix;
185
145k
    if (buf_size < 0x280) {
186
105k
        av_log(ctx->avctx, AV_LOG_ERROR,
187
105k
               "buffer too small (%d < 640).\n", buf_size);
188
105k
        return AVERROR_INVALIDDATA;
189
105k
    }
190
191
39.9k
    header_prefix = ff_dnxhd_parse_header_prefix(buf);
192
39.9k
    if (header_prefix == 0) {
193
1.89k
        av_log(ctx->avctx, AV_LOG_ERROR,
194
1.89k
               "unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
195
1.89k
               buf[0], buf[1], buf[2], buf[3], buf[4]);
196
1.89k
        return AVERROR_INVALIDDATA;
197
1.89k
    }
198
38.1k
    if (buf[5] & 2) { /* interlaced */
199
22.7k
        ctx->cur_field = first_field ? buf[5] & 1 : !ctx->cur_field;
200
22.7k
        frame->flags |= AV_FRAME_FLAG_INTERLACED;
201
22.7k
        if (first_field ^ ctx->cur_field)
202
2.23k
            frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
203
22.7k
        av_log(ctx->avctx, AV_LOG_DEBUG,
204
22.7k
               "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
205
22.7k
    } else {
206
15.3k
        ctx->cur_field = 0;
207
15.3k
    }
208
38.1k
    ctx->mbaff = (buf[0x6] >> 5) & 1;
209
38.1k
    ctx->alpha = buf[0x7] & 1;
210
38.1k
    ctx->lla   = (buf[0x7] >> 1) & 1;
211
38.1k
    if (ctx->alpha)
212
2.17k
        avpriv_request_sample(ctx->avctx, "alpha");
213
214
38.1k
    ctx->height = AV_RB16(buf + 0x18);
215
38.1k
    ctx->width  = AV_RB16(buf + 0x1a);
216
217
38.1k
    switch(buf[0x21] >> 5) {
218
25.2k
    case 1: bitdepth = 8; break;
219
4.88k
    case 2: bitdepth = 10; break;
220
7.63k
    case 3: bitdepth = 12; break;
221
347
    default:
222
347
        av_log(ctx->avctx, AV_LOG_ERROR,
223
347
               "Unknown bitdepth indicator (%d)\n", buf[0x21] >> 5);
224
347
        return AVERROR_INVALIDDATA;
225
38.1k
    }
226
227
37.7k
    cid = AV_RB32(buf + 0x28);
228
229
37.7k
    ctx->avctx->profile = dnxhd_get_profile(cid);
230
231
37.7k
    if ((ret = dnxhd_init_vlc(ctx, cid, bitdepth)) < 0)
232
1.06k
        return ret;
233
36.6k
    if (ctx->mbaff && ctx->cid_table->cid != 1260)
234
23.8k
        av_log(ctx->avctx, AV_LOG_WARNING,
235
23.8k
               "Adaptive MB interlace flag in an unsupported profile.\n");
236
237
36.6k
    switch ((buf[0x2C] >> 1) & 3) {
238
26.7k
    case 0: frame->colorspace = AVCOL_SPC_BT709;       break;
239
2.20k
    case 1: frame->colorspace = AVCOL_SPC_BT2020_NCL;  break;
240
487
    case 2: frame->colorspace = AVCOL_SPC_BT2020_CL;   break;
241
7.29k
    case 3: frame->colorspace = AVCOL_SPC_UNSPECIFIED; break;
242
36.6k
    }
243
244
36.6k
    ctx->act = buf[0x2C] & 1;
245
36.6k
    if (ctx->act && ctx->cid_table->cid != 1256 && ctx->cid_table->cid != 1270)
246
4.63k
        av_log(ctx->avctx, AV_LOG_WARNING,
247
4.63k
               "Adaptive color transform in an unsupported profile.\n");
248
249
36.6k
    ctx->is_444 = (buf[0x2C] >> 6) & 1;
250
36.6k
    if (ctx->is_444) {
251
8.05k
        if (bitdepth == 8) {
252
235
            avpriv_request_sample(ctx->avctx, "4:4:4 8 bits");
253
235
            return AVERROR_INVALIDDATA;
254
7.82k
        } else if (bitdepth == 10) {
255
2.88k
            ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
256
2.88k
            ctx->pix_fmt = ctx->act ? AV_PIX_FMT_YUV444P10
257
2.88k
                                    : AV_PIX_FMT_GBRP10;
258
4.93k
        } else {
259
4.93k
            ctx->decode_dct_block = dnxhd_decode_dct_block_12_444;
260
4.93k
            ctx->pix_fmt = ctx->act ? AV_PIX_FMT_YUV444P12
261
4.93k
                                    : AV_PIX_FMT_GBRP12;
262
4.93k
        }
263
28.6k
    } else if (bitdepth == 12) {
264
1.90k
        ctx->decode_dct_block = dnxhd_decode_dct_block_12;
265
1.90k
        ctx->pix_fmt = AV_PIX_FMT_YUV422P12;
266
26.7k
    } else if (bitdepth == 10) {
267
1.86k
        if (ctx->avctx->profile == AV_PROFILE_DNXHR_HQX)
268
608
            ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
269
1.25k
        else
270
1.25k
            ctx->decode_dct_block = dnxhd_decode_dct_block_10;
271
1.86k
        ctx->pix_fmt = AV_PIX_FMT_YUV422P10;
272
24.8k
    } else {
273
24.8k
        ctx->decode_dct_block = dnxhd_decode_dct_block_8;
274
24.8k
        ctx->pix_fmt = AV_PIX_FMT_YUV422P;
275
24.8k
    }
276
277
36.4k
    ctx->avctx->bits_per_raw_sample = ctx->bit_depth = bitdepth;
278
36.4k
    if (ctx->bit_depth != old_bit_depth) {
279
10.4k
        ff_blockdsp_init(&ctx->bdsp);
280
10.4k
        ff_idctdsp_init(&ctx->idsp, ctx->avctx);
281
10.4k
        ff_permute_scantable(ctx->permutated_scantable, ff_zigzag_direct,
282
10.4k
                             ctx->idsp.idct_permutation);
283
10.4k
    }
284
285
    // make sure profile size constraints are respected
286
    // DNx100 allows 1920->1440 and 1280->960 subsampling
287
36.4k
    if (ctx->width != ctx->cid_table->width &&
288
36.4k
        ctx->cid_table->width != DNXHD_VARIABLE) {
289
6.65k
        av_reduce(&ctx->avctx->sample_aspect_ratio.num,
290
6.65k
                  &ctx->avctx->sample_aspect_ratio.den,
291
6.65k
                  ctx->width, ctx->cid_table->width, 255);
292
6.65k
        ctx->width = ctx->cid_table->width;
293
6.65k
    }
294
295
36.4k
    if (buf_size < ctx->cid_table->coding_unit_size) {
296
6.65k
        av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size (%d < %u).\n",
297
6.65k
               buf_size, ctx->cid_table->coding_unit_size);
298
6.65k
        return AVERROR_INVALIDDATA;
299
6.65k
    }
300
301
29.7k
    ctx->mb_width  = (ctx->width + 15)>> 4;
302
29.7k
    ctx->mb_height = AV_RB16(buf + 0x16c);
303
304
29.7k
    if ((ctx->height + 15) >> 4 == ctx->mb_height && (frame->flags & AV_FRAME_FLAG_INTERLACED))
305
805
        ctx->height <<= 1;
306
307
29.7k
    av_log(ctx->avctx, AV_LOG_VERBOSE, "%dx%d, 4:%s %d bits, MBAFF=%d ACT=%d\n",
308
29.7k
           ctx->width, ctx->height, ctx->is_444 ? "4:4" : "2:2",
309
29.7k
           ctx->bit_depth, ctx->mbaff, ctx->act);
310
311
    // Newer format supports variable mb_scan_index sizes
312
29.7k
    if (ctx->mb_height > 68 && ff_dnxhd_check_header_prefix_hr(header_prefix)) {
313
4.05k
        ctx->data_offset = 0x170 + (ctx->mb_height << 2);
314
25.7k
    } else {
315
25.7k
        if (ctx->mb_height > 68) {
316
251
            av_log(ctx->avctx, AV_LOG_ERROR,
317
251
                   "mb height too big: %d\n", ctx->mb_height);
318
251
            return AVERROR_INVALIDDATA;
319
251
        }
320
25.4k
        ctx->data_offset = 0x280;
321
25.4k
    }
322
29.5k
    if ((ctx->mb_height << !!(frame->flags & AV_FRAME_FLAG_INTERLACED)) > (ctx->height + 15) >> 4) {
323
3.75k
        av_log(ctx->avctx, AV_LOG_ERROR,
324
3.75k
                "mb height too big: %d\n", ctx->mb_height);
325
3.75k
        return AVERROR_INVALIDDATA;
326
3.75k
    }
327
328
25.7k
    if (buf_size < ctx->data_offset) {
329
247
        av_log(ctx->avctx, AV_LOG_ERROR,
330
247
               "buffer too small (%d < %d).\n", buf_size, ctx->data_offset);
331
247
        return AVERROR_INVALIDDATA;
332
247
    }
333
334
25.5k
    if (ctx->mb_height > FF_ARRAY_ELEMS(ctx->mb_scan_index)) {
335
68
        av_log(ctx->avctx, AV_LOG_ERROR,
336
68
               "mb_height too big (%d > %"SIZE_SPECIFIER").\n", ctx->mb_height, FF_ARRAY_ELEMS(ctx->mb_scan_index));
337
68
        return AVERROR_INVALIDDATA;
338
68
    }
339
340
53.8k
    for (i = 0; i < ctx->mb_height; i++) {
341
29.4k
        ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
342
29.4k
        ff_dlog(ctx->avctx, "mb scan index %d, pos %d: %"PRIu32"\n",
343
29.4k
                i, 0x170 + (i << 2), ctx->mb_scan_index[i]);
344
29.4k
        if (buf_size - ctx->data_offset < ctx->mb_scan_index[i]) {
345
1.07k
            av_log(ctx->avctx, AV_LOG_ERROR,
346
1.07k
                   "invalid mb scan index (%"PRIu32" vs %u).\n",
347
1.07k
                   ctx->mb_scan_index[i], buf_size - ctx->data_offset);
348
1.07k
            return AVERROR_INVALIDDATA;
349
1.07k
        }
350
29.4k
    }
351
352
24.4k
    return 0;
353
25.4k
}
354
355
static av_always_inline int dnxhd_decode_dct_block(const DNXHDContext *ctx,
356
                                                   RowContext *row,
357
                                                   int n,
358
                                                   int index_bits,
359
                                                   int level_bias,
360
                                                   int level_shift,
361
                                                   int dc_shift)
362
409k
{
363
409k
    int i, j, index1, len, flags;
364
409k
    int level, component, sign;
365
409k
    const int *scale;
366
409k
    const uint8_t *weight_matrix;
367
409k
    const uint8_t *ac_info = ctx->cid_table->ac_info;
368
409k
    int16_t *block = row->blocks[n];
369
409k
    const int eob_index     = ctx->cid_table->eob_index;
370
409k
    int ret = 0;
371
409k
    OPEN_READER(bs, &row->gb);
372
373
409k
    ctx->bdsp.clear_block(block);
374
375
409k
    if (!ctx->is_444) {
376
276k
        if (n & 2) {
377
130k
            component     = 1 + (n & 1);
378
130k
            scale = row->chroma_scale;
379
130k
            weight_matrix = ctx->cid_table->chroma_weight;
380
146k
        } else {
381
146k
            component     = 0;
382
146k
            scale = row->luma_scale;
383
146k
            weight_matrix = ctx->cid_table->luma_weight;
384
146k
        }
385
276k
    } else {
386
132k
        component = (n >> 1) % 3;
387
132k
        if (component) {
388
81.4k
            scale = row->chroma_scale;
389
81.4k
            weight_matrix = ctx->cid_table->chroma_weight;
390
81.4k
        } else {
391
51.0k
            scale = row->luma_scale;
392
51.0k
            weight_matrix = ctx->cid_table->luma_weight;
393
51.0k
        }
394
132k
    }
395
396
409k
    UPDATE_CACHE(bs, &row->gb);
397
409k
    GET_VLC(len, bs, &row->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
398
409k
    if (len < 0) {
399
2.70k
        ret = len;
400
2.70k
        goto error;
401
2.70k
    }
402
406k
    if (len) {
403
358k
        level = GET_CACHE(bs, &row->gb);
404
358k
        LAST_SKIP_BITS(bs, &row->gb, len);
405
358k
        sign  = ~level >> 31;
406
358k
        level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
407
358k
        row->last_dc[component] += level * (1 << dc_shift);
408
358k
    }
409
406k
    block[0] = row->last_dc[component];
410
411
406k
    i = 0;
412
413
406k
    UPDATE_CACHE(bs, &row->gb);
414
406k
    GET_VLC(index1, bs, &row->gb, ctx->ac_vlc.table,
415
406k
            DNXHD_VLC_BITS, 2);
416
417
2.67M
    while (index1 != eob_index) {
418
2.29M
        level = ac_info[2*index1+0];
419
2.29M
        flags = ac_info[2*index1+1];
420
421
2.29M
        sign = SHOW_SBITS(bs, &row->gb, 1);
422
2.29M
        SKIP_BITS(bs, &row->gb, 1);
423
424
2.29M
        if (flags & 1) {
425
17.8k
            level += SHOW_UBITS(bs, &row->gb, index_bits) << 7;
426
17.8k
            SKIP_BITS(bs, &row->gb, index_bits);
427
17.8k
        }
428
429
2.29M
        if (flags & 2) {
430
475k
            int run;
431
475k
            UPDATE_CACHE(bs, &row->gb);
432
475k
            GET_VLC(run, bs, &row->gb, ctx->run_vlc.table,
433
475k
                    DNXHD_VLC_BITS, 2);
434
475k
            i += run;
435
475k
        }
436
437
2.29M
        if (++i > 63) {
438
23.9k
            av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
439
23.9k
            ret = -1;
440
23.9k
            break;
441
23.9k
        }
442
443
2.27M
        j      = ctx->permutated_scantable[i];
444
2.27M
        level *= scale[i];
445
2.27M
        level += scale[i] >> 1;
446
2.27M
        if (level_bias < 32 || weight_matrix[i] != level_bias)
447
1.86M
            level += level_bias; // 1<<(level_shift-1)
448
2.27M
        level >>= level_shift;
449
450
2.27M
        block[j] = (level ^ sign) - sign;
451
452
2.27M
        UPDATE_CACHE(bs, &row->gb);
453
2.27M
        GET_VLC(index1, bs, &row->gb, ctx->ac_vlc.table,
454
2.27M
                DNXHD_VLC_BITS, 2);
455
2.27M
    }
456
409k
error:
457
409k
    CLOSE_READER(bs, &row->gb);
458
409k
    return ret;
459
406k
}
460
461
static int dnxhd_decode_dct_block_8(const DNXHDContext *ctx,
462
                                    RowContext *row, int n)
463
244k
{
464
244k
    return dnxhd_decode_dct_block(ctx, row, n, 4, 32, 6, 0);
465
244k
}
466
467
static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx,
468
                                     RowContext *row, int n)
469
14.5k
{
470
14.5k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 0);
471
14.5k
}
472
473
static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx,
474
                                         RowContext *row, int n)
475
51.3k
{
476
51.3k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 6, 0);
477
51.3k
}
478
479
static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx,
480
                                     RowContext *row, int n)
481
16.2k
{
482
16.2k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 2);
483
16.2k
}
484
485
static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx,
486
                                         RowContext *row, int n)
487
82.2k
{
488
82.2k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 4, 2);
489
82.2k
}
490
491
static int dnxhd_decode_macroblock(const DNXHDContext *ctx, RowContext *row,
492
                                   AVFrame *frame, int x, int y)
493
64.7k
{
494
64.7k
    int shift1 = ctx->bit_depth >= 10;
495
64.7k
    int dct_linesize_luma   = frame->linesize[0];
496
64.7k
    int dct_linesize_chroma = frame->linesize[1];
497
64.7k
    uint8_t *dest_y, *dest_u, *dest_v;
498
64.7k
    int dct_y_offset, dct_x_offset;
499
64.7k
    int qscale, i, act;
500
64.7k
    int interlaced_mb = 0;
501
502
64.7k
    if (ctx->mbaff) {
503
16.3k
        interlaced_mb = get_bits1(&row->gb);
504
16.3k
        qscale = get_bits(&row->gb, 10);
505
48.4k
    } else {
506
48.4k
        qscale = get_bits(&row->gb, 11);
507
48.4k
    }
508
64.7k
    act = get_bits1(&row->gb);
509
64.7k
    if (act) {
510
27.2k
        if (!ctx->act) {
511
13.2k
            static int act_warned;
512
13.2k
            if (!act_warned) {
513
1
                act_warned = 1;
514
1
                av_log(ctx->avctx, AV_LOG_ERROR,
515
1
                       "ACT flag set, in violation of frame header.\n");
516
1
            }
517
13.9k
        } else if (row->format == -1) {
518
2.73k
            row->format = act;
519
11.2k
        } else if (row->format != act) {
520
0
            row->format = 2; // Variable
521
0
        }
522
27.2k
    }
523
524
64.7k
    if (qscale != row->last_qscale) {
525
2.65M
        for (i = 0; i < 64; i++) {
526
2.61M
            row->luma_scale[i]   = qscale * ctx->cid_table->luma_weight[i];
527
2.61M
            row->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
528
2.61M
        }
529
40.8k
        row->last_qscale = qscale;
530
40.8k
    }
531
532
447k
    for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
533
409k
        if (ctx->decode_dct_block(ctx, row, i) < 0)
534
26.6k
            return AVERROR_INVALIDDATA;
535
409k
    }
536
537
38.0k
    if (frame->flags & AV_FRAME_FLAG_INTERLACED) {
538
20.6k
        dct_linesize_luma   <<= 1;
539
20.6k
        dct_linesize_chroma <<= 1;
540
20.6k
    }
541
542
38.0k
    dest_y = frame->data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
543
38.0k
    dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
544
38.0k
    dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
545
546
38.0k
    if ((frame->flags & AV_FRAME_FLAG_INTERLACED) && ctx->cur_field) {
547
10.3k
        dest_y += frame->linesize[0];
548
10.3k
        dest_u += frame->linesize[1];
549
10.3k
        dest_v += frame->linesize[2];
550
10.3k
    }
551
38.0k
    if (interlaced_mb) {
552
5.83k
        dct_linesize_luma   <<= 1;
553
5.83k
        dct_linesize_chroma <<= 1;
554
5.83k
    }
555
556
38.0k
    dct_y_offset = interlaced_mb ? frame->linesize[0] : (dct_linesize_luma << 3);
557
38.0k
    dct_x_offset = 8 << shift1;
558
38.0k
    if (!ctx->is_444) {
559
30.3k
        ctx->idsp.idct_put(dest_y,                               dct_linesize_luma, row->blocks[0]);
560
30.3k
        ctx->idsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, row->blocks[1]);
561
30.3k
        ctx->idsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, row->blocks[4]);
562
30.3k
        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[5]);
563
564
30.3k
        if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
565
30.3k
            dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
566
30.3k
            ctx->idsp.idct_put(dest_u,                dct_linesize_chroma, row->blocks[2]);
567
30.3k
            ctx->idsp.idct_put(dest_v,                dct_linesize_chroma, row->blocks[3]);
568
30.3k
            ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, row->blocks[6]);
569
30.3k
            ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, row->blocks[7]);
570
30.3k
        }
571
30.3k
    } else {
572
7.76k
        ctx->idsp.idct_put(dest_y,                               dct_linesize_luma, row->blocks[0]);
573
7.76k
        ctx->idsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, row->blocks[1]);
574
7.76k
        ctx->idsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, row->blocks[6]);
575
7.76k
        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[7]);
576
577
7.76k
        if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
578
7.76k
            dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
579
7.76k
            ctx->idsp.idct_put(dest_u,                               dct_linesize_chroma, row->blocks[2]);
580
7.76k
            ctx->idsp.idct_put(dest_u + dct_x_offset,                dct_linesize_chroma, row->blocks[3]);
581
7.76k
            ctx->idsp.idct_put(dest_u + dct_y_offset,                dct_linesize_chroma, row->blocks[8]);
582
7.76k
            ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[9]);
583
7.76k
            ctx->idsp.idct_put(dest_v,                               dct_linesize_chroma, row->blocks[4]);
584
7.76k
            ctx->idsp.idct_put(dest_v + dct_x_offset,                dct_linesize_chroma, row->blocks[5]);
585
7.76k
            ctx->idsp.idct_put(dest_v + dct_y_offset,                dct_linesize_chroma, row->blocks[10]);
586
7.76k
            ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[11]);
587
7.76k
        }
588
7.76k
    }
589
590
38.0k
    return 0;
591
64.7k
}
592
593
static int dnxhd_decode_row(AVCodecContext *avctx, void *data,
594
                            int rownb, int threadnb)
595
27.9k
{
596
27.9k
    const DNXHDContext *ctx = avctx->priv_data;
597
27.9k
    uint32_t offset = ctx->mb_scan_index[rownb];
598
27.9k
    RowContext *row = ctx->rows + threadnb;
599
27.9k
    int x, ret;
600
601
27.9k
    row->last_dc[0] =
602
27.9k
    row->last_dc[1] =
603
27.9k
    row->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
604
27.9k
    ret = init_get_bits8(&row->gb, ctx->buf + offset, ctx->buf_size - offset);
605
27.9k
    if (ret < 0) {
606
0
        row->errors++;
607
0
        return ret;
608
0
    }
609
65.9k
    for (x = 0; x < ctx->mb_width; x++) {
610
64.7k
        int ret = dnxhd_decode_macroblock(ctx, row, data, x, rownb);
611
64.7k
        if (ret < 0) {
612
26.6k
            row->errors++;
613
26.6k
            return ret;
614
26.6k
        }
615
64.7k
    }
616
617
1.25k
    return 0;
618
27.9k
}
619
620
static int dnxhd_decode_frame(AVCodecContext *avctx, AVFrame *picture,
621
                              int *got_frame, AVPacket *avpkt)
622
136k
{
623
136k
    const uint8_t *buf = avpkt->data;
624
136k
    int buf_size = avpkt->size;
625
136k
    DNXHDContext *ctx = avctx->priv_data;
626
136k
    int first_field = 1;
627
136k
    int ret, i;
628
629
136k
    ff_dlog(avctx, "frame size %d\n", buf_size);
630
631
272k
    for (i = 0; i < avctx->thread_count; i++)
632
136k
        ctx->rows[i].format = -1;
633
634
145k
decode_coding_unit:
635
145k
    if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
636
120k
        return ret;
637
638
24.4k
    if ((avctx->width || avctx->height) &&
639
24.4k
        (ctx->width != avctx->width || ctx->height != avctx->height)) {
640
1.23k
        av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %ux%u\n",
641
1.23k
               avctx->width, avctx->height, ctx->width, ctx->height);
642
1.23k
        first_field = 1;
643
1.23k
    }
644
24.4k
    if (avctx->pix_fmt != AV_PIX_FMT_NONE && avctx->pix_fmt != ctx->pix_fmt) {
645
2.79k
        av_log(avctx, AV_LOG_WARNING, "pix_fmt changed: %s -> %s\n",
646
2.79k
               av_get_pix_fmt_name(avctx->pix_fmt), av_get_pix_fmt_name(ctx->pix_fmt));
647
2.79k
        first_field = 1;
648
2.79k
    }
649
650
24.4k
    avctx->pix_fmt = ctx->pix_fmt;
651
24.4k
    ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
652
24.4k
    if (ret < 0)
653
1.24k
        return ret;
654
655
23.1k
    if (first_field) {
656
14.1k
        if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
657
231
            return ret;
658
14.1k
    }
659
660
22.9k
    ctx->buf_size = buf_size - ctx->data_offset;
661
22.9k
    ctx->buf = buf + ctx->data_offset;
662
22.9k
    avctx->execute2(avctx, dnxhd_decode_row, picture, NULL, ctx->mb_height);
663
664
22.9k
    if (first_field && (picture->flags & AV_FRAME_FLAG_INTERLACED)) {
665
9.01k
        buf      += ctx->cid_table->coding_unit_size;
666
9.01k
        buf_size -= ctx->cid_table->coding_unit_size;
667
9.01k
        first_field = 0;
668
9.01k
        goto decode_coding_unit;
669
9.01k
    }
670
671
13.9k
    ret = 0;
672
27.8k
    for (i = 0; i < avctx->thread_count; i++) {
673
13.9k
        ret += ctx->rows[i].errors;
674
13.9k
        ctx->rows[i].errors = 0;
675
13.9k
    }
676
677
13.9k
    if (ctx->act) {
678
4.36k
        static int act_warned;
679
4.36k
        int format = ctx->rows[0].format;
680
4.36k
        for (i = 1; i < avctx->thread_count; i++) {
681
0
            if (ctx->rows[i].format != format &&
682
0
                ctx->rows[i].format != -1 /* not run */) {
683
0
                format = 2;
684
0
                break;
685
0
            }
686
0
        }
687
4.36k
        switch (format) {
688
1.63k
        case -1:
689
1.63k
        case 2:
690
1.63k
            if (!act_warned) {
691
1
                act_warned = 1;
692
1
                av_log(ctx->avctx, AV_LOG_ERROR,
693
1
                       "Unsupported: variable ACT flag.\n");
694
1
            }
695
1.63k
            break;
696
0
        case 0:
697
0
            ctx->pix_fmt = ctx->bit_depth==10
698
0
                         ? AV_PIX_FMT_GBRP10 : AV_PIX_FMT_GBRP12;
699
0
            break;
700
2.73k
        case 1:
701
2.73k
            ctx->pix_fmt = ctx->bit_depth==10
702
2.73k
                         ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV444P12;
703
2.73k
            break;
704
4.36k
        }
705
4.36k
    }
706
13.9k
    avctx->pix_fmt = ctx->pix_fmt;
707
13.9k
    if (ret) {
708
5.03k
        av_log(ctx->avctx, AV_LOG_ERROR, "%d lines with errors\n", ret);
709
5.03k
        return AVERROR_INVALIDDATA;
710
5.03k
    }
711
712
8.88k
    *got_frame = 1;
713
8.88k
    return avpkt->size;
714
13.9k
}
715
716
static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
717
3.41k
{
718
3.41k
    DNXHDContext *ctx = avctx->priv_data;
719
720
3.41k
    ff_vlc_free(&ctx->ac_vlc);
721
3.41k
    ff_vlc_free(&ctx->dc_vlc);
722
3.41k
    ff_vlc_free(&ctx->run_vlc);
723
724
3.41k
    av_freep(&ctx->rows);
725
726
3.41k
    return 0;
727
3.41k
}
728
729
const FFCodec ff_dnxhd_decoder = {
730
    .p.name         = "dnxhd",
731
    CODEC_LONG_NAME("VC3/DNxHD"),
732
    .p.type         = AVMEDIA_TYPE_VIDEO,
733
    .p.id           = AV_CODEC_ID_DNXHD,
734
    .priv_data_size = sizeof(DNXHDContext),
735
    .init           = dnxhd_decode_init,
736
    .close          = dnxhd_decode_close,
737
    FF_CODEC_DECODE_CB(dnxhd_decode_frame),
738
    .p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS |
739
                      AV_CODEC_CAP_SLICE_THREADS,
740
    .p.profiles     = NULL_IF_CONFIG_SMALL(ff_dnxhd_profiles),
741
};