Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/dnxhddec.c
Line
Count
Source
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
9.70k
#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.60k
{
97
3.60k
    DNXHDContext *ctx = avctx->priv_data;
98
99
3.60k
    ctx->avctx = avctx;
100
3.60k
    ctx->cid = -1;
101
3.60k
    if (avctx->colorspace == AVCOL_SPC_UNSPECIFIED) {
102
3.60k
        avctx->colorspace = AVCOL_SPC_BT709;
103
3.60k
    }
104
105
3.60k
    avctx->coded_width  = FFALIGN(avctx->width,  16);
106
3.60k
    avctx->coded_height = FFALIGN(avctx->height, 16);
107
108
3.60k
    ctx->rows = av_calloc(avctx->thread_count, sizeof(*ctx->rows));
109
3.60k
    if (!ctx->rows)
110
0
        return AVERROR(ENOMEM);
111
112
3.60k
    return 0;
113
3.60k
}
114
115
static int dnxhd_init_vlc(DNXHDContext *ctx, uint32_t cid, int bitdepth)
116
41.8k
{
117
41.8k
    int ret;
118
41.8k
    if (cid != ctx->cid) {
119
10.4k
        const CIDEntry *cid_table = ff_dnxhd_get_cid_table(cid);
120
121
10.4k
        if (!cid_table) {
122
506
            av_log(ctx->avctx, AV_LOG_ERROR, "unsupported cid %"PRIu32"\n", cid);
123
506
            return AVERROR(ENOSYS);
124
506
        }
125
9.92k
        if (cid_table->bit_depth != bitdepth &&
126
5.97k
            cid_table->bit_depth != DNXHD_VARIABLE) {
127
223
            av_log(ctx->avctx, AV_LOG_ERROR, "bit depth mismatches %d %d\n",
128
223
                   cid_table->bit_depth, bitdepth);
129
223
            return AVERROR_INVALIDDATA;
130
223
        }
131
9.70k
        ctx->cid_table = cid_table;
132
9.70k
        av_log(ctx->avctx, AV_LOG_VERBOSE, "Profile cid %"PRIu32".\n", cid);
133
134
9.70k
        ff_vlc_free(&ctx->ac_vlc);
135
9.70k
        ff_vlc_free(&ctx->dc_vlc);
136
9.70k
        ff_vlc_free(&ctx->run_vlc);
137
138
9.70k
        if ((ret = vlc_init(&ctx->ac_vlc, DNXHD_VLC_BITS, 257,
139
9.70k
                 ctx->cid_table->ac_bits, 1, 1,
140
9.70k
                 ctx->cid_table->ac_codes, 2, 2, 0)) < 0)
141
0
            goto out;
142
9.70k
        if ((ret = vlc_init(&ctx->dc_vlc, DNXHD_DC_VLC_BITS, bitdepth > 8 ? 14 : 12,
143
9.70k
                 ctx->cid_table->dc_bits, 1, 1,
144
9.70k
                 ctx->cid_table->dc_codes, 1, 1, 0)) < 0)
145
0
            goto out;
146
9.70k
        if ((ret = ff_vlc_init_sparse(&ctx->run_vlc, DNXHD_VLC_BITS, 62,
147
9.70k
                 ctx->cid_table->run_bits, 1, 1,
148
9.70k
                 ctx->cid_table->run_codes, 2, 2,
149
9.70k
                 ctx->cid_table->run, 1, 1, 0)) < 0)
150
0
            goto out;
151
152
9.70k
        ctx->cid = cid;
153
9.70k
    }
154
41.1k
    ret = 0;
155
41.1k
out:
156
41.1k
    if (ret < 0)
157
0
        av_log(ctx->avctx, AV_LOG_ERROR, "vlc_init failed\n");
158
41.1k
    return ret;
159
41.1k
}
160
161
static int dnxhd_get_profile(int cid)
162
41.8k
{
163
41.8k
    switch(cid) {
164
24.7k
    case 1270:
165
24.7k
        return AV_PROFILE_DNXHR_444;
166
3.19k
    case 1271:
167
3.19k
        return AV_PROFILE_DNXHR_HQX;
168
4.70k
    case 1272:
169
4.70k
        return AV_PROFILE_DNXHR_HQ;
170
166
    case 1273:
171
166
        return AV_PROFILE_DNXHR_SQ;
172
455
    case 1274:
173
455
        return AV_PROFILE_DNXHR_LB;
174
41.8k
    }
175
8.63k
    return AV_PROFILE_DNXHD;
176
41.8k
}
177
178
static int dnxhd_decode_header(DNXHDContext *ctx, AVFrame *frame,
179
                               const uint8_t *buf, int buf_size,
180
                               int first_field)
181
256k
{
182
256k
    int i, cid, ret;
183
256k
    int old_bit_depth = ctx->bit_depth, bitdepth;
184
256k
    uint64_t header_prefix;
185
256k
    if (buf_size < 0x280) {
186
211k
        av_log(ctx->avctx, AV_LOG_ERROR,
187
211k
               "buffer too small (%d < 640).\n", buf_size);
188
211k
        return AVERROR_INVALIDDATA;
189
211k
    }
190
191
44.7k
    header_prefix = ff_dnxhd_parse_header_prefix(buf);
192
44.7k
    if (header_prefix == 0) {
193
2.56k
        av_log(ctx->avctx, AV_LOG_ERROR,
194
2.56k
               "unknown header 0x%02X 0x%02X 0x%02X 0x%02X 0x%02X\n",
195
2.56k
               buf[0], buf[1], buf[2], buf[3], buf[4]);
196
2.56k
        return AVERROR_INVALIDDATA;
197
2.56k
    }
198
42.2k
    if (buf[5] & 2) { /* interlaced */
199
28.3k
        ctx->cur_field = first_field ? buf[5] & 1 : !ctx->cur_field;
200
28.3k
        frame->flags |= AV_FRAME_FLAG_INTERLACED;
201
28.3k
        if (first_field ^ ctx->cur_field)
202
6.10k
            frame->flags |= AV_FRAME_FLAG_TOP_FIELD_FIRST;
203
28.3k
        av_log(ctx->avctx, AV_LOG_DEBUG,
204
28.3k
               "interlaced %d, cur field %d\n", buf[5] & 3, ctx->cur_field);
205
28.3k
    } else {
206
13.8k
        ctx->cur_field = 0;
207
13.8k
    }
208
42.2k
    ctx->mbaff = (buf[0x6] >> 5) & 1;
209
42.2k
    ctx->alpha = buf[0x7] & 1;
210
42.2k
    ctx->lla   = (buf[0x7] >> 1) & 1;
211
42.2k
    if (ctx->alpha)
212
5.32k
        avpriv_request_sample(ctx->avctx, "alpha");
213
214
42.2k
    ctx->height = AV_RB16(buf + 0x18);
215
42.2k
    ctx->width  = AV_RB16(buf + 0x1a);
216
217
42.2k
    switch(buf[0x21] >> 5) {
218
29.5k
    case 1: bitdepth = 8; break;
219
7.17k
    case 2: bitdepth = 10; break;
220
5.19k
    case 3: bitdepth = 12; break;
221
324
    default:
222
324
        av_log(ctx->avctx, AV_LOG_ERROR,
223
324
               "Unknown bitdepth indicator (%d)\n", buf[0x21] >> 5);
224
324
        return AVERROR_INVALIDDATA;
225
42.2k
    }
226
227
41.8k
    cid = AV_RB32(buf + 0x28);
228
229
41.8k
    ctx->avctx->profile = dnxhd_get_profile(cid);
230
231
41.8k
    if ((ret = dnxhd_init_vlc(ctx, cid, bitdepth)) < 0)
232
729
        return ret;
233
41.1k
    if (ctx->mbaff && ctx->cid_table->cid != 1260)
234
25.2k
        av_log(ctx->avctx, AV_LOG_WARNING,
235
25.2k
               "Adaptive MB interlace flag in an unsupported profile.\n");
236
237
41.1k
    switch ((buf[0x2C] >> 1) & 3) {
238
33.3k
    case 0: frame->colorspace = AVCOL_SPC_BT709;       break;
239
2.73k
    case 1: frame->colorspace = AVCOL_SPC_BT2020_NCL;  break;
240
1.11k
    case 2: frame->colorspace = AVCOL_SPC_BT2020_CL;   break;
241
3.97k
    case 3: frame->colorspace = AVCOL_SPC_UNSPECIFIED; break;
242
41.1k
    }
243
244
41.1k
    ctx->act = buf[0x2C] & 1;
245
41.1k
    if (ctx->act && ctx->cid_table->cid != 1256 && ctx->cid_table->cid != 1270)
246
5.21k
        av_log(ctx->avctx, AV_LOG_WARNING,
247
5.21k
               "Adaptive color transform in an unsupported profile.\n");
248
249
41.1k
    ctx->is_444 = (buf[0x2C] >> 6) & 1;
250
41.1k
    if (ctx->is_444) {
251
5.82k
        if (bitdepth == 8) {
252
208
            avpriv_request_sample(ctx->avctx, "4:4:4 8 bits");
253
208
            return AVERROR_INVALIDDATA;
254
5.61k
        } else if (bitdepth == 10) {
255
3.18k
            ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
256
3.18k
            ctx->pix_fmt = ctx->act ? AV_PIX_FMT_YUV444P10
257
3.18k
                                    : AV_PIX_FMT_GBRP10;
258
3.18k
        } else {
259
2.42k
            ctx->decode_dct_block = dnxhd_decode_dct_block_12_444;
260
2.42k
            ctx->pix_fmt = ctx->act ? AV_PIX_FMT_YUV444P12
261
2.42k
                                    : AV_PIX_FMT_GBRP12;
262
2.42k
        }
263
35.3k
    } else if (bitdepth == 12) {
264
2.28k
        ctx->decode_dct_block = dnxhd_decode_dct_block_12;
265
2.28k
        ctx->pix_fmt = AV_PIX_FMT_YUV422P12;
266
33.0k
    } else if (bitdepth == 10) {
267
3.86k
        if (ctx->avctx->profile == AV_PROFILE_DNXHR_HQX)
268
627
            ctx->decode_dct_block = dnxhd_decode_dct_block_10_444;
269
3.24k
        else
270
3.24k
            ctx->decode_dct_block = dnxhd_decode_dct_block_10;
271
3.86k
        ctx->pix_fmt = AV_PIX_FMT_YUV422P10;
272
29.1k
    } else {
273
29.1k
        ctx->decode_dct_block = dnxhd_decode_dct_block_8;
274
29.1k
        ctx->pix_fmt = AV_PIX_FMT_YUV422P;
275
29.1k
    }
276
277
40.9k
    ctx->avctx->bits_per_raw_sample = ctx->bit_depth = bitdepth;
278
40.9k
    if (ctx->bit_depth != old_bit_depth) {
279
9.41k
        ff_blockdsp_init(&ctx->bdsp);
280
9.41k
        ff_idctdsp_init(&ctx->idsp, ctx->avctx);
281
9.41k
        ff_permute_scantable(ctx->permutated_scantable, ff_zigzag_direct,
282
9.41k
                             ctx->idsp.idct_permutation);
283
9.41k
    }
284
285
    // make sure profile size constraints are respected
286
    // DNx100 allows 1920->1440 and 1280->960 subsampling
287
40.9k
    if (ctx->width != ctx->cid_table->width &&
288
40.5k
        ctx->cid_table->width != DNXHD_VARIABLE) {
289
7.70k
        av_reduce(&ctx->avctx->sample_aspect_ratio.num,
290
7.70k
                  &ctx->avctx->sample_aspect_ratio.den,
291
7.70k
                  ctx->width, ctx->cid_table->width, 255);
292
7.70k
        ctx->width = ctx->cid_table->width;
293
7.70k
    }
294
295
40.9k
    if (buf_size < ctx->cid_table->coding_unit_size) {
296
7.70k
        av_log(ctx->avctx, AV_LOG_ERROR, "incorrect frame size (%d < %u).\n",
297
7.70k
               buf_size, ctx->cid_table->coding_unit_size);
298
7.70k
        return AVERROR_INVALIDDATA;
299
7.70k
    }
300
301
33.2k
    ctx->mb_width  = (ctx->width + 15)>> 4;
302
33.2k
    ctx->mb_height = AV_RB16(buf + 0x16c);
303
304
33.2k
    if ((ctx->height + 15) >> 4 == ctx->mb_height && (frame->flags & AV_FRAME_FLAG_INTERLACED))
305
3.51k
        ctx->height <<= 1;
306
307
33.2k
    av_log(ctx->avctx, AV_LOG_VERBOSE, "%dx%d, 4:%s %d bits, MBAFF=%d ACT=%d\n",
308
33.2k
           ctx->width, ctx->height, ctx->is_444 ? "4:4" : "2:2",
309
33.2k
           ctx->bit_depth, ctx->mbaff, ctx->act);
310
311
    // Newer format supports variable mb_scan_index sizes
312
33.2k
    if (ctx->mb_height > 68 && ff_dnxhd_check_header_prefix_hr(header_prefix)) {
313
3.67k
        ctx->data_offset = 0x170 + (ctx->mb_height << 2);
314
29.5k
    } else {
315
29.5k
        if (ctx->mb_height > 68) {
316
274
            av_log(ctx->avctx, AV_LOG_ERROR,
317
274
                   "mb height too big: %d\n", ctx->mb_height);
318
274
            return AVERROR_INVALIDDATA;
319
274
        }
320
29.2k
        ctx->data_offset = 0x280;
321
29.2k
    }
322
32.9k
    if ((ctx->mb_height << !!(frame->flags & AV_FRAME_FLAG_INTERLACED)) > (ctx->height + 15) >> 4) {
323
3.25k
        av_log(ctx->avctx, AV_LOG_ERROR,
324
3.25k
                "mb height too big: %d\n", ctx->mb_height);
325
3.25k
        return AVERROR_INVALIDDATA;
326
3.25k
    }
327
328
29.7k
    if (buf_size < ctx->data_offset) {
329
212
        av_log(ctx->avctx, AV_LOG_ERROR,
330
212
               "buffer too small (%d < %d).\n", buf_size, ctx->data_offset);
331
212
        return AVERROR_INVALIDDATA;
332
212
    }
333
334
29.5k
    if (ctx->mb_height > FF_ARRAY_ELEMS(ctx->mb_scan_index)) {
335
207
        av_log(ctx->avctx, AV_LOG_ERROR,
336
207
               "mb_height too big (%d > %zu).\n", ctx->mb_height, FF_ARRAY_ELEMS(ctx->mb_scan_index));
337
207
        return AVERROR_INVALIDDATA;
338
207
    }
339
340
70.0k
    for (i = 0; i < ctx->mb_height; i++) {
341
41.0k
        ctx->mb_scan_index[i] = AV_RB32(buf + 0x170 + (i << 2));
342
41.0k
        ff_dlog(ctx->avctx, "mb scan index %d, pos %d: %"PRIu32"\n",
343
41.0k
                i, 0x170 + (i << 2), ctx->mb_scan_index[i]);
344
41.0k
        if (buf_size - ctx->data_offset < ctx->mb_scan_index[i]) {
345
320
            av_log(ctx->avctx, AV_LOG_ERROR,
346
320
                   "invalid mb scan index (%"PRIu32" vs %u).\n",
347
320
                   ctx->mb_scan_index[i], buf_size - ctx->data_offset);
348
320
            return AVERROR_INVALIDDATA;
349
320
        }
350
41.0k
    }
351
352
28.9k
    return 0;
353
29.2k
}
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
482k
{
363
482k
    int i, j, index1, len, flags;
364
482k
    int level, component, sign;
365
482k
    const int *scale;
366
482k
    const uint8_t *weight_matrix;
367
482k
    const uint8_t *ac_info = ctx->cid_table->ac_info;
368
482k
    int16_t *block = row->blocks[n];
369
482k
    const int eob_index     = ctx->cid_table->eob_index;
370
482k
    int ret = 0;
371
482k
    OPEN_READER(bs, &row->gb);
372
373
482k
    ctx->bdsp.clear_block(block);
374
375
482k
    if (!ctx->is_444) {
376
434k
        if (n & 2) {
377
196k
            component     = 1 + (n & 1);
378
196k
            scale = row->chroma_scale;
379
196k
            weight_matrix = ctx->cid_table->chroma_weight;
380
238k
        } else {
381
238k
            component     = 0;
382
238k
            scale = row->luma_scale;
383
238k
            weight_matrix = ctx->cid_table->luma_weight;
384
238k
        }
385
434k
    } else {
386
47.4k
        component = (n >> 1) % 3;
387
47.4k
        if (component) {
388
26.6k
            scale = row->chroma_scale;
389
26.6k
            weight_matrix = ctx->cid_table->chroma_weight;
390
26.6k
        } else {
391
20.8k
            scale = row->luma_scale;
392
20.8k
            weight_matrix = ctx->cid_table->luma_weight;
393
20.8k
        }
394
47.4k
    }
395
396
482k
    UPDATE_CACHE(bs, &row->gb);
397
482k
    GET_VLC(len, bs, &row->gb, ctx->dc_vlc.table, DNXHD_DC_VLC_BITS, 1);
398
482k
    if (len < 0) {
399
1.39k
        ret = len;
400
1.39k
        goto error;
401
1.39k
    }
402
480k
    if (len) {
403
423k
        level = GET_CACHE(bs, &row->gb);
404
423k
        LAST_SKIP_BITS(bs, &row->gb, len);
405
423k
        sign  = ~level >> 31;
406
423k
        level = (NEG_USR32(sign ^ level, len) ^ sign) - sign;
407
423k
        row->last_dc[component] += level * (1 << dc_shift);
408
423k
    }
409
480k
    block[0] = row->last_dc[component];
410
411
480k
    i = 0;
412
413
480k
    UPDATE_CACHE(bs, &row->gb);
414
480k
    GET_VLC(index1, bs, &row->gb, ctx->ac_vlc.table,
415
480k
            DNXHD_VLC_BITS, 2);
416
417
4.15M
    while (index1 != eob_index) {
418
3.71M
        level = ac_info[2*index1+0];
419
3.71M
        flags = ac_info[2*index1+1];
420
421
3.71M
        sign = SHOW_SBITS(bs, &row->gb, 1);
422
3.71M
        SKIP_BITS(bs, &row->gb, 1);
423
424
3.71M
        if (flags & 1) {
425
12.9k
            level += SHOW_UBITS(bs, &row->gb, index_bits) << 7;
426
12.9k
            SKIP_BITS(bs, &row->gb, index_bits);
427
12.9k
        }
428
429
3.71M
        if (flags & 2) {
430
799k
            int run;
431
799k
            UPDATE_CACHE(bs, &row->gb);
432
799k
            GET_VLC(run, bs, &row->gb, ctx->run_vlc.table,
433
799k
                    DNXHD_VLC_BITS, 2);
434
799k
            i += run;
435
799k
        }
436
437
3.71M
        if (++i > 63) {
438
38.0k
            av_log(ctx->avctx, AV_LOG_ERROR, "ac tex damaged %d, %d\n", n, i);
439
38.0k
            ret = -1;
440
38.0k
            break;
441
38.0k
        }
442
443
3.67M
        j      = ctx->permutated_scantable[i];
444
3.67M
        level *= scale[i];
445
3.67M
        level += scale[i] >> 1;
446
3.67M
        if (level_bias < 32 || weight_matrix[i] != level_bias)
447
3.37M
            level += level_bias; // 1<<(level_shift-1)
448
3.67M
        level >>= level_shift;
449
450
3.67M
        block[j] = (level ^ sign) - sign;
451
452
3.67M
        UPDATE_CACHE(bs, &row->gb);
453
3.67M
        GET_VLC(index1, bs, &row->gb, ctx->ac_vlc.table,
454
3.67M
                DNXHD_VLC_BITS, 2);
455
3.67M
    }
456
482k
error:
457
482k
    CLOSE_READER(bs, &row->gb);
458
482k
    return ret;
459
480k
}
460
461
static int dnxhd_decode_dct_block_8(const DNXHDContext *ctx,
462
                                    RowContext *row, int n)
463
381k
{
464
381k
    return dnxhd_decode_dct_block(ctx, row, n, 4, 32, 6, 0);
465
381k
}
466
467
static int dnxhd_decode_dct_block_10(const DNXHDContext *ctx,
468
                                     RowContext *row, int n)
469
32.7k
{
470
32.7k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 0);
471
32.7k
}
472
473
static int dnxhd_decode_dct_block_10_444(const DNXHDContext *ctx,
474
                                         RowContext *row, int n)
475
24.5k
{
476
24.5k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 6, 0);
477
24.5k
}
478
479
static int dnxhd_decode_dct_block_12(const DNXHDContext *ctx,
480
                                     RowContext *row, int n)
481
19.4k
{
482
19.4k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 8, 4, 2);
483
19.4k
}
484
485
static int dnxhd_decode_dct_block_12_444(const DNXHDContext *ctx,
486
                                         RowContext *row, int n)
487
24.0k
{
488
24.0k
    return dnxhd_decode_dct_block(ctx, row, n, 6, 32, 4, 2);
489
24.0k
}
490
491
static int dnxhd_decode_macroblock(const DNXHDContext *ctx, RowContext *row,
492
                                   AVFrame *frame, int x, int y)
493
88.9k
{
494
88.9k
    int shift1 = ctx->bit_depth >= 10;
495
88.9k
    int dct_linesize_luma   = frame->linesize[0];
496
88.9k
    int dct_linesize_chroma = frame->linesize[1];
497
88.9k
    uint8_t *dest_y, *dest_u, *dest_v;
498
88.9k
    int dct_y_offset, dct_x_offset;
499
88.9k
    int qscale, i, act;
500
88.9k
    int interlaced_mb = 0;
501
502
88.9k
    if (ctx->mbaff) {
503
15.2k
        interlaced_mb = get_bits1(&row->gb);
504
15.2k
        qscale = get_bits(&row->gb, 10);
505
73.6k
    } else {
506
73.6k
        qscale = get_bits(&row->gb, 11);
507
73.6k
    }
508
88.9k
    act = get_bits1(&row->gb);
509
88.9k
    if (act) {
510
28.1k
        if (!ctx->act) {
511
17.4k
            static int act_warned;
512
17.4k
            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
17.4k
        } else if (row->format == -1) {
518
1.51k
            row->format = act;
519
9.22k
        } else if (row->format != act) {
520
0
            row->format = 2; // Variable
521
0
        }
522
28.1k
    }
523
524
88.9k
    if (qscale != row->last_qscale) {
525
3.33M
        for (i = 0; i < 64; i++) {
526
3.28M
            row->luma_scale[i]   = qscale * ctx->cid_table->luma_weight[i];
527
3.28M
            row->chroma_scale[i] = qscale * ctx->cid_table->chroma_weight[i];
528
3.28M
        }
529
51.2k
        row->last_qscale = qscale;
530
51.2k
    }
531
532
531k
    for (i = 0; i < 8 + 4 * ctx->is_444; i++) {
533
482k
        if (ctx->decode_dct_block(ctx, row, i) < 0)
534
39.4k
            return AVERROR_INVALIDDATA;
535
482k
    }
536
537
49.5k
    if (frame->flags & AV_FRAME_FLAG_INTERLACED) {
538
15.3k
        dct_linesize_luma   <<= 1;
539
15.3k
        dct_linesize_chroma <<= 1;
540
15.3k
    }
541
542
49.5k
    dest_y = frame->data[0] + ((y * dct_linesize_luma)   << 4) + (x << (4 + shift1));
543
49.5k
    dest_u = frame->data[1] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
544
49.5k
    dest_v = frame->data[2] + ((y * dct_linesize_chroma) << 4) + (x << (3 + shift1 + ctx->is_444));
545
546
49.5k
    if ((frame->flags & AV_FRAME_FLAG_INTERLACED) && ctx->cur_field) {
547
7.67k
        dest_y += frame->linesize[0];
548
7.67k
        dest_u += frame->linesize[1];
549
7.67k
        dest_v += frame->linesize[2];
550
7.67k
    }
551
49.5k
    if (interlaced_mb) {
552
2.97k
        dct_linesize_luma   <<= 1;
553
2.97k
        dct_linesize_chroma <<= 1;
554
2.97k
    }
555
556
49.5k
    dct_y_offset = interlaced_mb ? frame->linesize[0] : (dct_linesize_luma << 3);
557
49.5k
    dct_x_offset = 8 << shift1;
558
49.5k
    if (!ctx->is_444) {
559
46.6k
        ctx->idsp.idct_put(dest_y,                               dct_linesize_luma, row->blocks[0]);
560
46.6k
        ctx->idsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, row->blocks[1]);
561
46.6k
        ctx->idsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, row->blocks[4]);
562
46.6k
        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[5]);
563
564
46.6k
        if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
565
46.6k
            dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
566
46.6k
            ctx->idsp.idct_put(dest_u,                dct_linesize_chroma, row->blocks[2]);
567
46.6k
            ctx->idsp.idct_put(dest_v,                dct_linesize_chroma, row->blocks[3]);
568
46.6k
            ctx->idsp.idct_put(dest_u + dct_y_offset, dct_linesize_chroma, row->blocks[6]);
569
46.6k
            ctx->idsp.idct_put(dest_v + dct_y_offset, dct_linesize_chroma, row->blocks[7]);
570
46.6k
        }
571
46.6k
    } else {
572
2.85k
        ctx->idsp.idct_put(dest_y,                               dct_linesize_luma, row->blocks[0]);
573
2.85k
        ctx->idsp.idct_put(dest_y + dct_x_offset,                dct_linesize_luma, row->blocks[1]);
574
2.85k
        ctx->idsp.idct_put(dest_y + dct_y_offset,                dct_linesize_luma, row->blocks[6]);
575
2.85k
        ctx->idsp.idct_put(dest_y + dct_y_offset + dct_x_offset, dct_linesize_luma, row->blocks[7]);
576
577
2.85k
        if (!(ctx->avctx->flags & AV_CODEC_FLAG_GRAY)) {
578
2.85k
            dct_y_offset = interlaced_mb ? frame->linesize[1] : (dct_linesize_chroma << 3);
579
2.85k
            ctx->idsp.idct_put(dest_u,                               dct_linesize_chroma, row->blocks[2]);
580
2.85k
            ctx->idsp.idct_put(dest_u + dct_x_offset,                dct_linesize_chroma, row->blocks[3]);
581
2.85k
            ctx->idsp.idct_put(dest_u + dct_y_offset,                dct_linesize_chroma, row->blocks[8]);
582
2.85k
            ctx->idsp.idct_put(dest_u + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[9]);
583
2.85k
            ctx->idsp.idct_put(dest_v,                               dct_linesize_chroma, row->blocks[4]);
584
2.85k
            ctx->idsp.idct_put(dest_v + dct_x_offset,                dct_linesize_chroma, row->blocks[5]);
585
2.85k
            ctx->idsp.idct_put(dest_v + dct_y_offset,                dct_linesize_chroma, row->blocks[10]);
586
2.85k
            ctx->idsp.idct_put(dest_v + dct_y_offset + dct_x_offset, dct_linesize_chroma, row->blocks[11]);
587
2.85k
        }
588
2.85k
    }
589
590
49.5k
    return 0;
591
88.9k
}
592
593
static int dnxhd_decode_row(AVCodecContext *avctx, void *data,
594
                            int rownb, int threadnb)
595
40.5k
{
596
40.5k
    const DNXHDContext *ctx = avctx->priv_data;
597
40.5k
    uint32_t offset = ctx->mb_scan_index[rownb];
598
40.5k
    RowContext *row = ctx->rows + threadnb;
599
40.5k
    int x, ret;
600
601
40.5k
    row->last_dc[0] =
602
40.5k
    row->last_dc[1] =
603
40.5k
    row->last_dc[2] = 1 << (ctx->bit_depth + 2); // for levels +2^(bitdepth-1)
604
40.5k
    ret = init_get_bits8(&row->gb, ctx->buf + offset, ctx->buf_size - offset);
605
40.5k
    if (ret < 0) {
606
0
        row->errors++;
607
0
        return ret;
608
0
    }
609
90.0k
    for (x = 0; x < ctx->mb_width; x++) {
610
88.9k
        int ret = dnxhd_decode_macroblock(ctx, row, data, x, rownb);
611
88.9k
        if (ret < 0) {
612
39.4k
            row->errors++;
613
39.4k
            return ret;
614
39.4k
        }
615
88.9k
    }
616
617
1.07k
    return 0;
618
40.5k
}
619
620
static int dnxhd_decode_frame(AVCodecContext *avctx, AVFrame *picture,
621
                              int *got_frame, AVPacket *avpkt)
622
244k
{
623
244k
    const uint8_t *buf = avpkt->data;
624
244k
    int buf_size = avpkt->size;
625
244k
    DNXHDContext *ctx = avctx->priv_data;
626
244k
    int first_field = 1;
627
244k
    int ret, i;
628
629
244k
    ff_dlog(avctx, "frame size %d\n", buf_size);
630
631
489k
    for (i = 0; i < avctx->thread_count; i++)
632
244k
        ctx->rows[i].format = -1;
633
634
256k
decode_coding_unit:
635
256k
    if ((ret = dnxhd_decode_header(ctx, picture, buf, buf_size, first_field)) < 0)
636
227k
        return ret;
637
638
28.9k
    if ((avctx->width || avctx->height) &&
639
25.4k
        (ctx->width != avctx->width || ctx->height != avctx->height)) {
640
1.48k
        av_log(avctx, AV_LOG_WARNING, "frame size changed: %dx%d -> %ux%u\n",
641
1.48k
               avctx->width, avctx->height, ctx->width, ctx->height);
642
1.48k
        first_field = 1;
643
1.48k
    }
644
28.9k
    if (avctx->pix_fmt != AV_PIX_FMT_NONE && avctx->pix_fmt != ctx->pix_fmt) {
645
1.53k
        av_log(avctx, AV_LOG_WARNING, "pix_fmt changed: %s -> %s\n",
646
1.53k
               av_get_pix_fmt_name(avctx->pix_fmt), av_get_pix_fmt_name(ctx->pix_fmt));
647
1.53k
        first_field = 1;
648
1.53k
    }
649
650
28.9k
    avctx->pix_fmt = ctx->pix_fmt;
651
28.9k
    ret = ff_set_dimensions(avctx, ctx->width, ctx->height);
652
28.9k
    if (ret < 0)
653
1.40k
        return ret;
654
655
27.5k
    if (first_field) {
656
15.7k
        if ((ret = ff_thread_get_buffer(avctx, picture, 0)) < 0)
657
202
            return ret;
658
15.7k
    }
659
660
27.3k
    ctx->buf_size = buf_size - ctx->data_offset;
661
27.3k
    ctx->buf = buf + ctx->data_offset;
662
27.3k
    avctx->execute2(avctx, dnxhd_decode_row, picture, NULL, ctx->mb_height);
663
664
27.3k
    if (first_field && (picture->flags & AV_FRAME_FLAG_INTERLACED)) {
665
11.8k
        buf      += ctx->cid_table->coding_unit_size;
666
11.8k
        buf_size -= ctx->cid_table->coding_unit_size;
667
11.8k
        first_field = 0;
668
11.8k
        goto decode_coding_unit;
669
11.8k
    }
670
671
15.5k
    ret = 0;
672
31.1k
    for (i = 0; i < avctx->thread_count; i++) {
673
15.5k
        ret += ctx->rows[i].errors;
674
15.5k
        ctx->rows[i].errors = 0;
675
15.5k
    }
676
677
15.5k
    if (ctx->act) {
678
4.29k
        static int act_warned;
679
4.29k
        int format = ctx->rows[0].format;
680
4.29k
        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.29k
        switch (format) {
688
2.77k
        case -1:
689
2.77k
        case 2:
690
2.77k
            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
2.77k
            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
1.51k
        case 1:
701
1.51k
            ctx->pix_fmt = ctx->bit_depth==10
702
1.51k
                         ? AV_PIX_FMT_YUV444P10 : AV_PIX_FMT_YUV444P12;
703
1.51k
            break;
704
4.29k
        }
705
4.29k
    }
706
15.5k
    avctx->pix_fmt = ctx->pix_fmt;
707
15.5k
    if (ret) {
708
4.48k
        av_log(ctx->avctx, AV_LOG_ERROR, "%d lines with errors\n", ret);
709
4.48k
        return AVERROR_INVALIDDATA;
710
4.48k
    }
711
712
11.0k
    *got_frame = 1;
713
11.0k
    return avpkt->size;
714
15.5k
}
715
716
static av_cold int dnxhd_decode_close(AVCodecContext *avctx)
717
3.60k
{
718
3.60k
    DNXHDContext *ctx = avctx->priv_data;
719
720
3.60k
    ff_vlc_free(&ctx->ac_vlc);
721
3.60k
    ff_vlc_free(&ctx->dc_vlc);
722
3.60k
    ff_vlc_free(&ctx->run_vlc);
723
724
3.60k
    av_freep(&ctx->rows);
725
726
3.60k
    return 0;
727
3.60k
}
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
};