Coverage Report

Created: 2026-07-25 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/dirac.c
Line
Count
Source
1
/*
2
 * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3
 * Copyright (C) 2009 David Conrad
4
 * Copyright (C) 2011 Jordi Ortiz
5
 *
6
 * This file is part of FFmpeg.
7
 *
8
 * FFmpeg is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * FFmpeg is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with FFmpeg; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 */
22
23
/**
24
 * @file
25
 * Dirac Decoder
26
 * @author Marco Gerards <marco@gnu.org>, David Conrad, Jordi Ortiz <nenjordi@gmail.com>
27
 */
28
29
#include "config.h"
30
31
#include "libavutil/mem.h"
32
#include "libavutil/pixdesc.h"
33
34
#include "dirac.h"
35
#include "get_bits.h"
36
#include "golomb.h"
37
#include "mpeg12data.h"
38
39
#if CONFIG_DIRAC_PARSE
40
41
typedef struct dirac_source_params {
42
    unsigned width;
43
    unsigned height;
44
    uint8_t chroma_format;          ///< 0: 444  1: 422  2: 420
45
46
    uint8_t interlaced;
47
    uint8_t top_field_first;
48
49
    uint8_t frame_rate_index;       ///< index into dirac_frame_rate[]
50
    uint8_t aspect_ratio_index;     ///< index into dirac_aspect_ratio[]
51
52
    uint16_t clean_width;
53
    uint16_t clean_height;
54
    uint16_t clean_left_offset;
55
    uint16_t clean_right_offset;
56
57
    uint8_t pixel_range_index;      ///< index into dirac_pixel_range_presets[]
58
    uint8_t color_spec_index;       ///< index into dirac_color_spec_presets[]
59
} dirac_source_params;
60
61
/* defaults for source parameters */
62
static const dirac_source_params dirac_source_parameters_defaults[] = {
63
    {  640,  480, 2, 0, 0,  1, 1,  640,  480, 0, 0, 1, 0 },
64
    {  176,  120, 2, 0, 0,  9, 2,  176,  120, 0, 0, 1, 1 },
65
    {  176,  144, 2, 0, 1, 10, 3,  176,  144, 0, 0, 1, 2 },
66
    {  352,  240, 2, 0, 0,  9, 2,  352,  240, 0, 0, 1, 1 },
67
    {  352,  288, 2, 0, 1, 10, 3,  352,  288, 0, 0, 1, 2 },
68
    {  704,  480, 2, 0, 0,  9, 2,  704,  480, 0, 0, 1, 1 },
69
    {  704,  576, 2, 0, 1, 10, 3,  704,  576, 0, 0, 1, 2 },
70
    {  720,  480, 1, 1, 0,  4, 2,  704,  480, 8, 0, 3, 1 },
71
    {  720,  576, 1, 1, 1,  3, 3,  704,  576, 8, 0, 3, 2 },
72
73
    { 1280,  720, 1, 0, 1,  7, 1, 1280,  720, 0, 0, 3, 3 },
74
    { 1280,  720, 1, 0, 1,  6, 1, 1280,  720, 0, 0, 3, 3 },
75
    { 1920, 1080, 1, 1, 1,  4, 1, 1920, 1080, 0, 0, 3, 3 },
76
    { 1920, 1080, 1, 1, 1,  3, 1, 1920, 1080, 0, 0, 3, 3 },
77
    { 1920, 1080, 1, 0, 1,  7, 1, 1920, 1080, 0, 0, 3, 3 },
78
    { 1920, 1080, 1, 0, 1,  6, 1, 1920, 1080, 0, 0, 3, 3 },
79
    { 2048, 1080, 0, 0, 1,  2, 1, 2048, 1080, 0, 0, 4, 4 },
80
    { 4096, 2160, 0, 0, 1,  2, 1, 4096, 2160, 0, 0, 4, 4 },
81
82
    { 3840, 2160, 1, 0, 1,  7, 1, 3840, 2160, 0, 0, 3, 3 },
83
    { 3840, 2160, 1, 0, 1,  6, 1, 3840, 2160, 0, 0, 3, 3 },
84
    { 7680, 4320, 1, 0, 1,  7, 1, 3840, 2160, 0, 0, 3, 3 },
85
    { 7680, 4320, 1, 0, 1,  6, 1, 3840, 2160, 0, 0, 3, 3 },
86
};
87
88
/* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
89
static const AVRational dirac_preset_aspect_ratios[] = {
90
    {  1,  1 },
91
    { 10, 11 },
92
    { 12, 11 },
93
    { 40, 33 },
94
    { 16, 11 },
95
    {  4,  3 },
96
};
97
98
/* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
99
 * Table 10.3 Available preset frame rate values
100
 */
101
static const AVRational dirac_frame_rate[] = {
102
    { 15000, 1001 },
103
    {    25,    2 },
104
};
105
106
/* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
107
 * range presets */
108
static const struct {
109
    uint8_t             bitdepth;
110
    enum AVColorRange   color_range;
111
} pixel_range_presets[] = {
112
    {  8, AVCOL_RANGE_JPEG },
113
    {  8, AVCOL_RANGE_MPEG },
114
    { 10, AVCOL_RANGE_MPEG },
115
    { 12, AVCOL_RANGE_MPEG },
116
};
117
118
static const enum AVColorPrimaries dirac_primaries[] = {
119
    AVCOL_PRI_BT709,
120
    AVCOL_PRI_SMPTE170M,
121
    AVCOL_PRI_BT470BG,
122
};
123
124
static const struct {
125
    enum AVColorPrimaries color_primaries;
126
    enum AVColorSpace colorspace;
127
    enum AVColorTransferCharacteristic color_trc;
128
} dirac_color_presets[] = {
129
    { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_BT709 },
130
    { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
131
    { AVCOL_PRI_BT470BG,   AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
132
    { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_BT709 },
133
    { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_UNSPECIFIED /* DCinema */ },
134
};
135
136
/* [DIRAC_STD] Table 10.2 Supported chroma sampling formats */
137
static const enum AVPixelFormat dirac_pix_fmt[][3] = {
138
    {AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12},
139
    {AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12},
140
    {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12},
141
};
142
143
/* [DIRAC_STD] 10.3 Parse Source Parameters.
144
 * source_parameters(base_video_format) */
145
static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb,
146
                                   void *log_ctx)
147
31.3k
{
148
31.3k
    AVRational frame_rate = { 0, 0 };
149
31.3k
    unsigned luma_depth = 8, luma_offset = 16;
150
31.3k
    int idx;
151
31.3k
    int chroma_x_shift, chroma_y_shift;
152
31.3k
    int ret;
153
154
    /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
155
    /* [DIRAC_STD] custom_dimensions_flag */
156
31.3k
    if (get_bits1(gb)) {
157
19.8k
        dsh->width  = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH  */
158
19.8k
        dsh->height = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
159
19.8k
    }
160
161
    /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
162
     *  chroma_sampling_format(video_params) */
163
    /* [DIRAC_STD] custom_chroma_format_flag */
164
31.3k
    if (get_bits1(gb))
165
        /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
166
14.7k
        dsh->chroma_format = get_interleaved_ue_golomb(gb);
167
31.3k
    if (dsh->chroma_format > 2U) {
168
1.24k
        if (log_ctx)
169
1.24k
            av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
170
1.24k
                   dsh->chroma_format);
171
1.24k
        return AVERROR_INVALIDDATA;
172
1.24k
    }
173
174
    /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
175
    /* [DIRAC_STD] custom_scan_format_flag */
176
30.1k
    if (get_bits1(gb))
177
        /* [DIRAC_STD] SOURCE_SAMPLING */
178
11.9k
        dsh->interlaced = get_interleaved_ue_golomb(gb);
179
30.1k
    if (dsh->interlaced > 1U)
180
912
        return AVERROR_INVALIDDATA;
181
182
    /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
183
29.2k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
184
16.0k
        dsh->frame_rate_index = get_interleaved_ue_golomb(gb);
185
186
16.0k
        if (dsh->frame_rate_index > 10U)
187
1.00k
            return AVERROR_INVALIDDATA;
188
189
15.0k
        if (!dsh->frame_rate_index) {
190
            /* [DIRAC_STD] FRAME_RATE_NUMER */
191
7.95k
            frame_rate.num = get_interleaved_ue_golomb(gb);
192
            /* [DIRAC_STD] FRAME_RATE_DENOM */
193
7.95k
            frame_rate.den = get_interleaved_ue_golomb(gb);
194
7.95k
        }
195
15.0k
    }
196
    /* [DIRAC_STD] preset_frame_rate(video_params, index) */
197
28.2k
    if (dsh->frame_rate_index > 0) {
198
20.2k
        if (dsh->frame_rate_index <= 8)
199
15.1k
            frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
200
5.12k
        else
201
            /* [DIRAC_STD] Table 10.3 values 9-10 */
202
5.12k
            frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
203
20.2k
    }
204
28.2k
    dsh->framerate = frame_rate;
205
206
    /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
207
     * pixel_aspect_ratio(video_params) */
208
28.2k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
209
        /* [DIRAC_STD] index */
210
10.1k
        dsh->aspect_ratio_index = get_interleaved_ue_golomb(gb);
211
212
10.1k
        if (dsh->aspect_ratio_index > 6U)
213
884
            return AVERROR_INVALIDDATA;
214
215
9.25k
        if (!dsh->aspect_ratio_index) {
216
5.71k
            dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(gb);
217
5.71k
            dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(gb);
218
5.71k
        }
219
9.25k
    }
220
    /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
221
     *  aspect ratio values */
222
27.3k
    if (dsh->aspect_ratio_index > 0)
223
21.6k
        dsh->sample_aspect_ratio =
224
21.6k
            dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
225
226
    /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
227
27.3k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
228
        /* [DIRAC_STD] CLEAN_WIDTH */
229
7.54k
        dsh->clean_width = get_interleaved_ue_golomb(gb);
230
        /* [DIRAC_STD] CLEAN_HEIGHT */
231
7.54k
        dsh->clean_height = get_interleaved_ue_golomb(gb);
232
        /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
233
7.54k
        dsh->clean_left_offset = get_interleaved_ue_golomb(gb);
234
        /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
235
7.54k
        dsh->clean_right_offset = get_interleaved_ue_golomb(gb);
236
7.54k
    }
237
238
    /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
239
     * WARNING: Some adaptation seems to be done using the
240
     * AVCOL_RANGE_MPEG/JPEG values */
241
27.3k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
242
        /* [DIRAC_STD] index */
243
7.04k
        dsh->pixel_range_index = get_interleaved_ue_golomb(gb);
244
245
7.04k
        if (dsh->pixel_range_index > 4U)
246
904
            return AVERROR_INVALIDDATA;
247
248
        /* This assumes either fullrange or MPEG levels only */
249
6.14k
        if (!dsh->pixel_range_index) {
250
1.21k
            luma_offset = get_interleaved_ue_golomb(gb);
251
1.21k
            luma_depth  = av_log2(get_interleaved_ue_golomb(gb)) + 1;
252
1.21k
            get_interleaved_ue_golomb(gb); /* chroma offset    */
253
1.21k
            get_interleaved_ue_golomb(gb); /* chroma excursion */
254
1.21k
            dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
255
1.21k
                                           : AVCOL_RANGE_JPEG;
256
1.21k
        }
257
6.14k
    }
258
    /* [DIRAC_STD] Table 10.5
259
     * Available signal range presets <--> pixel_range_presets */
260
26.4k
    if (dsh->pixel_range_index > 0) {
261
25.2k
        idx                = dsh->pixel_range_index - 1;
262
25.2k
        luma_depth         = pixel_range_presets[idx].bitdepth;
263
25.2k
        dsh->color_range   = pixel_range_presets[idx].color_range;
264
25.2k
    }
265
266
26.4k
    dsh->bit_depth = luma_depth;
267
268
    /* Full range 8 bts uses the same pix_fmts as limited range 8 bits */
269
26.4k
    dsh->pixel_range_index += dsh->pixel_range_index == 1;
270
271
26.4k
    if (dsh->pixel_range_index < 2U)
272
1.21k
        return AVERROR_INVALIDDATA;
273
274
25.2k
    dsh->pix_fmt = dirac_pix_fmt[dsh->chroma_format][dsh->pixel_range_index-2];
275
25.2k
    ret = av_pix_fmt_get_chroma_sub_sample(dsh->pix_fmt, &chroma_x_shift, &chroma_y_shift);
276
25.2k
    if (ret)
277
0
        return ret;
278
279
25.2k
    if ((dsh->width % (1<<chroma_x_shift)) || (dsh->height % (1<<chroma_y_shift))) {
280
2.25k
        if (log_ctx)
281
2.25k
            av_log(log_ctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
282
2.25k
        return AVERROR_INVALIDDATA;
283
2.25k
    }
284
285
    /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
286
22.9k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
287
        /* [DIRAC_STD] index */
288
9.91k
        idx = dsh->color_spec_index = get_interleaved_ue_golomb(gb);
289
290
9.91k
        if (dsh->color_spec_index > 4U)
291
317
            return AVERROR_INVALIDDATA;
292
293
9.59k
        dsh->color_primaries = dirac_color_presets[idx].color_primaries;
294
9.59k
        dsh->colorspace      = dirac_color_presets[idx].colorspace;
295
9.59k
        dsh->color_trc       = dirac_color_presets[idx].color_trc;
296
297
9.59k
        if (!dsh->color_spec_index) {
298
            /* [DIRAC_STD] 10.3.9.1 Colour primaries */
299
5.25k
            if (get_bits1(gb)) {
300
2.23k
                idx = get_interleaved_ue_golomb(gb);
301
2.23k
                if (idx < 3U)
302
1.90k
                    dsh->color_primaries = dirac_primaries[idx];
303
2.23k
            }
304
            /* [DIRAC_STD] 10.3.9.2 Colour matrix */
305
5.25k
            if (get_bits1(gb)) {
306
2.96k
                idx = get_interleaved_ue_golomb(gb);
307
2.96k
                if (!idx)
308
2.32k
                    dsh->colorspace = AVCOL_SPC_BT709;
309
645
                else if (idx == 1)
310
254
                    dsh->colorspace = AVCOL_SPC_BT470BG;
311
2.96k
            }
312
            /* [DIRAC_STD] 10.3.9.3 Transfer function */
313
5.25k
            if (get_bits1(gb) && !get_interleaved_ue_golomb(gb))
314
1.65k
                dsh->color_trc = AVCOL_TRC_BT709;
315
5.25k
        }
316
13.0k
    } else {
317
13.0k
        idx                    = dsh->color_spec_index;
318
13.0k
        dsh->color_primaries = dirac_color_presets[idx].color_primaries;
319
13.0k
        dsh->colorspace      = dirac_color_presets[idx].colorspace;
320
13.0k
        dsh->color_trc       = dirac_color_presets[idx].color_trc;
321
13.0k
    }
322
323
22.6k
    return 0;
324
22.9k
}
325
326
/* [DIRAC_STD] 10. Sequence Header. sequence_header() */
327
int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
328
                                   const uint8_t *buf, size_t buf_size,
329
                                   void *log_ctx)
330
32.3k
{
331
32.3k
    AVDiracSeqHeader *dsh;
332
32.3k
    GetBitContext gb;
333
32.3k
    unsigned video_format, picture_coding_mode;
334
32.3k
    int ret;
335
336
32.3k
    dsh = av_mallocz(sizeof(*dsh));
337
32.3k
    if (!dsh)
338
0
        return AVERROR(ENOMEM);
339
340
32.3k
    ret = init_get_bits8(&gb, buf, buf_size);
341
32.3k
    if (ret < 0)
342
0
        goto fail;
343
344
    /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
345
32.3k
    dsh->version.major = get_interleaved_ue_golomb(&gb);
346
32.3k
    dsh->version.minor = get_interleaved_ue_golomb(&gb);
347
32.3k
    dsh->profile   = get_interleaved_ue_golomb(&gb);
348
32.3k
    dsh->level     = get_interleaved_ue_golomb(&gb);
349
    /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
350
     * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
351
32.3k
    video_format = get_interleaved_ue_golomb(&gb);
352
353
32.3k
    if (dsh->version.major < 2 && log_ctx)
354
13.1k
        av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
355
19.2k
    else if (dsh->version.major > 2 && log_ctx)
356
13.2k
        av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
357
358
32.3k
    if (video_format > 20U) {
359
964
        ret = AVERROR_INVALIDDATA;
360
964
        goto fail;
361
964
    }
362
363
    /* Fill in defaults for the source parameters. */
364
31.3k
    dsh->width              = dirac_source_parameters_defaults[video_format].width;
365
31.3k
    dsh->height             = dirac_source_parameters_defaults[video_format].height;
366
31.3k
    dsh->chroma_format      = dirac_source_parameters_defaults[video_format].chroma_format;
367
31.3k
    dsh->interlaced         = dirac_source_parameters_defaults[video_format].interlaced;
368
31.3k
    dsh->top_field_first    = dirac_source_parameters_defaults[video_format].top_field_first;
369
31.3k
    dsh->frame_rate_index   = dirac_source_parameters_defaults[video_format].frame_rate_index;
370
31.3k
    dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
371
31.3k
    dsh->clean_width        = dirac_source_parameters_defaults[video_format].clean_width;
372
31.3k
    dsh->clean_height       = dirac_source_parameters_defaults[video_format].clean_height;
373
31.3k
    dsh->clean_left_offset  = dirac_source_parameters_defaults[video_format].clean_left_offset;
374
31.3k
    dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
375
31.3k
    dsh->pixel_range_index  = dirac_source_parameters_defaults[video_format].pixel_range_index;
376
31.3k
    dsh->color_spec_index   = dirac_source_parameters_defaults[video_format].color_spec_index;
377
378
    /* [DIRAC_STD] 10.3 Source Parameters
379
     * Override the defaults. */
380
31.3k
    ret = parse_source_parameters(dsh, &gb, log_ctx);
381
31.3k
    if (ret < 0)
382
8.72k
        goto fail;
383
384
    /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
385
     * currently only used to signal field coding */
386
22.6k
    picture_coding_mode = get_interleaved_ue_golomb(&gb);
387
22.6k
    if (picture_coding_mode != 0) {
388
6.83k
        if (log_ctx) {
389
6.83k
            av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
390
6.83k
                   picture_coding_mode);
391
6.83k
        }
392
6.83k
        ret = AVERROR_INVALIDDATA;
393
6.83k
        goto fail;
394
6.83k
    }
395
396
15.8k
    *pdsh = dsh;
397
15.8k
    return 0;
398
16.5k
fail:
399
16.5k
    av_freep(&dsh);
400
    *pdsh = NULL;
401
16.5k
    return ret;
402
22.6k
}
403
#else
404
int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
405
                                   const uint8_t *buf, size_t buf_size,
406
                                   void *log_ctx)
407
{
408
    return AVERROR(ENOSYS);
409
}
410
#endif