Coverage Report

Created: 2026-01-16 07:48

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
33.0k
{
148
33.0k
    AVRational frame_rate = { 0, 0 };
149
33.0k
    unsigned luma_depth = 8, luma_offset = 16;
150
33.0k
    int idx;
151
33.0k
    int chroma_x_shift, chroma_y_shift;
152
33.0k
    int ret;
153
154
    /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
155
    /* [DIRAC_STD] custom_dimensions_flag */
156
33.0k
    if (get_bits1(gb)) {
157
20.3k
        dsh->width  = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH  */
158
20.3k
        dsh->height = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
159
20.3k
    }
160
161
    /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
162
     *  chroma_sampling_format(video_params) */
163
    /* [DIRAC_STD] custom_chroma_format_flag */
164
33.0k
    if (get_bits1(gb))
165
        /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
166
14.6k
        dsh->chroma_format = get_interleaved_ue_golomb(gb);
167
33.0k
    if (dsh->chroma_format > 2U) {
168
1.36k
        if (log_ctx)
169
1.36k
            av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
170
1.36k
                   dsh->chroma_format);
171
1.36k
        return AVERROR_INVALIDDATA;
172
1.36k
    }
173
174
    /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
175
    /* [DIRAC_STD] custom_scan_format_flag */
176
31.6k
    if (get_bits1(gb))
177
        /* [DIRAC_STD] SOURCE_SAMPLING */
178
11.2k
        dsh->interlaced = get_interleaved_ue_golomb(gb);
179
31.6k
    if (dsh->interlaced > 1U)
180
1.09k
        return AVERROR_INVALIDDATA;
181
182
    /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
183
30.6k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
184
15.7k
        dsh->frame_rate_index = get_interleaved_ue_golomb(gb);
185
186
15.7k
        if (dsh->frame_rate_index > 10U)
187
852
            return AVERROR_INVALIDDATA;
188
189
14.8k
        if (!dsh->frame_rate_index) {
190
            /* [DIRAC_STD] FRAME_RATE_NUMER */
191
7.68k
            frame_rate.num = get_interleaved_ue_golomb(gb);
192
            /* [DIRAC_STD] FRAME_RATE_DENOM */
193
7.68k
            frame_rate.den = get_interleaved_ue_golomb(gb);
194
7.68k
        }
195
14.8k
    }
196
    /* [DIRAC_STD] preset_frame_rate(video_params, index) */
197
29.7k
    if (dsh->frame_rate_index > 0) {
198
22.0k
        if (dsh->frame_rate_index <= 8)
199
17.0k
            frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
200
5.01k
        else
201
            /* [DIRAC_STD] Table 10.3 values 9-10 */
202
5.01k
            frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
203
22.0k
    }
204
29.7k
    dsh->framerate = frame_rate;
205
206
    /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
207
     * pixel_aspect_ratio(video_params) */
208
29.7k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
209
        /* [DIRAC_STD] index */
210
11.3k
        dsh->aspect_ratio_index = get_interleaved_ue_golomb(gb);
211
212
11.3k
        if (dsh->aspect_ratio_index > 6U)
213
976
            return AVERROR_INVALIDDATA;
214
215
10.3k
        if (!dsh->aspect_ratio_index) {
216
5.99k
            dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(gb);
217
5.99k
            dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(gb);
218
5.99k
        }
219
10.3k
    }
220
    /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
221
     *  aspect ratio values */
222
28.7k
    if (dsh->aspect_ratio_index > 0)
223
22.7k
        dsh->sample_aspect_ratio =
224
22.7k
            dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
225
226
    /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
227
28.7k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
228
        /* [DIRAC_STD] CLEAN_WIDTH */
229
8.87k
        dsh->clean_width = get_interleaved_ue_golomb(gb);
230
        /* [DIRAC_STD] CLEAN_HEIGHT */
231
8.87k
        dsh->clean_height = get_interleaved_ue_golomb(gb);
232
        /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
233
8.87k
        dsh->clean_left_offset = get_interleaved_ue_golomb(gb);
234
        /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
235
8.87k
        dsh->clean_right_offset = get_interleaved_ue_golomb(gb);
236
8.87k
    }
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
28.7k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
242
        /* [DIRAC_STD] index */
243
7.95k
        dsh->pixel_range_index = get_interleaved_ue_golomb(gb);
244
245
7.95k
        if (dsh->pixel_range_index > 4U)
246
956
            return AVERROR_INVALIDDATA;
247
248
        /* This assumes either fullrange or MPEG levels only */
249
6.99k
        if (!dsh->pixel_range_index) {
250
2.29k
            luma_offset = get_interleaved_ue_golomb(gb);
251
2.29k
            luma_depth  = av_log2(get_interleaved_ue_golomb(gb)) + 1;
252
2.29k
            get_interleaved_ue_golomb(gb); /* chroma offset    */
253
2.29k
            get_interleaved_ue_golomb(gb); /* chroma excursion */
254
2.29k
            dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
255
2.29k
                                           : AVCOL_RANGE_JPEG;
256
2.29k
        }
257
6.99k
    }
258
    /* [DIRAC_STD] Table 10.5
259
     * Available signal range presets <--> pixel_range_presets */
260
27.8k
    if (dsh->pixel_range_index > 0) {
261
25.5k
        idx                = dsh->pixel_range_index - 1;
262
25.5k
        luma_depth         = pixel_range_presets[idx].bitdepth;
263
25.5k
        dsh->color_range   = pixel_range_presets[idx].color_range;
264
25.5k
    }
265
266
27.8k
    dsh->bit_depth = luma_depth;
267
268
    /* Full range 8 bts uses the same pix_fmts as limited range 8 bits */
269
27.8k
    dsh->pixel_range_index += dsh->pixel_range_index == 1;
270
271
27.8k
    if (dsh->pixel_range_index < 2U)
272
2.29k
        return AVERROR_INVALIDDATA;
273
274
25.5k
    dsh->pix_fmt = dirac_pix_fmt[dsh->chroma_format][dsh->pixel_range_index-2];
275
25.5k
    ret = av_pix_fmt_get_chroma_sub_sample(dsh->pix_fmt, &chroma_x_shift, &chroma_y_shift);
276
25.5k
    if (ret)
277
0
        return ret;
278
279
25.5k
    if ((dsh->width % (1<<chroma_x_shift)) || (dsh->height % (1<<chroma_y_shift))) {
280
2.97k
        if (log_ctx)
281
2.97k
            av_log(log_ctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
282
2.97k
        return AVERROR_INVALIDDATA;
283
2.97k
    }
284
285
    /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
286
22.5k
    if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
287
        /* [DIRAC_STD] index */
288
9.97k
        idx = dsh->color_spec_index = get_interleaved_ue_golomb(gb);
289
290
9.97k
        if (dsh->color_spec_index > 4U)
291
366
            return AVERROR_INVALIDDATA;
292
293
9.60k
        dsh->color_primaries = dirac_color_presets[idx].color_primaries;
294
9.60k
        dsh->colorspace      = dirac_color_presets[idx].colorspace;
295
9.60k
        dsh->color_trc       = dirac_color_presets[idx].color_trc;
296
297
9.60k
        if (!dsh->color_spec_index) {
298
            /* [DIRAC_STD] 10.3.9.1 Colour primaries */
299
5.15k
            if (get_bits1(gb)) {
300
2.12k
                idx = get_interleaved_ue_golomb(gb);
301
2.12k
                if (idx < 3U)
302
1.78k
                    dsh->color_primaries = dirac_primaries[idx];
303
2.12k
            }
304
            /* [DIRAC_STD] 10.3.9.2 Colour matrix */
305
5.15k
            if (get_bits1(gb)) {
306
2.96k
                idx = get_interleaved_ue_golomb(gb);
307
2.96k
                if (!idx)
308
2.29k
                    dsh->colorspace = AVCOL_SPC_BT709;
309
669
                else if (idx == 1)
310
257
                    dsh->colorspace = AVCOL_SPC_BT470BG;
311
2.96k
            }
312
            /* [DIRAC_STD] 10.3.9.3 Transfer function */
313
5.15k
            if (get_bits1(gb) && !get_interleaved_ue_golomb(gb))
314
1.58k
                dsh->color_trc = AVCOL_TRC_BT709;
315
5.15k
        }
316
12.5k
    } else {
317
12.5k
        idx                    = dsh->color_spec_index;
318
12.5k
        dsh->color_primaries = dirac_color_presets[idx].color_primaries;
319
12.5k
        dsh->colorspace      = dirac_color_presets[idx].colorspace;
320
12.5k
        dsh->color_trc       = dirac_color_presets[idx].color_trc;
321
12.5k
    }
322
323
22.1k
    return 0;
324
22.5k
}
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
34.5k
{
331
34.5k
    AVDiracSeqHeader *dsh;
332
34.5k
    GetBitContext gb;
333
34.5k
    unsigned video_format, picture_coding_mode;
334
34.5k
    int ret;
335
336
34.5k
    dsh = av_mallocz(sizeof(*dsh));
337
34.5k
    if (!dsh)
338
0
        return AVERROR(ENOMEM);
339
340
34.5k
    ret = init_get_bits8(&gb, buf, buf_size);
341
34.5k
    if (ret < 0)
342
0
        goto fail;
343
344
    /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
345
34.5k
    dsh->version.major = get_interleaved_ue_golomb(&gb);
346
34.5k
    dsh->version.minor = get_interleaved_ue_golomb(&gb);
347
34.5k
    dsh->profile   = get_interleaved_ue_golomb(&gb);
348
34.5k
    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
34.5k
    video_format = get_interleaved_ue_golomb(&gb);
352
353
34.5k
    if (dsh->version.major < 2 && log_ctx)
354
13.8k
        av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
355
20.6k
    else if (dsh->version.major > 2 && log_ctx)
356
15.1k
        av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
357
358
34.5k
    if (video_format > 20U) {
359
1.46k
        ret = AVERROR_INVALIDDATA;
360
1.46k
        goto fail;
361
1.46k
    }
362
363
    /* Fill in defaults for the source parameters. */
364
33.0k
    dsh->width              = dirac_source_parameters_defaults[video_format].width;
365
33.0k
    dsh->height             = dirac_source_parameters_defaults[video_format].height;
366
33.0k
    dsh->chroma_format      = dirac_source_parameters_defaults[video_format].chroma_format;
367
33.0k
    dsh->interlaced         = dirac_source_parameters_defaults[video_format].interlaced;
368
33.0k
    dsh->top_field_first    = dirac_source_parameters_defaults[video_format].top_field_first;
369
33.0k
    dsh->frame_rate_index   = dirac_source_parameters_defaults[video_format].frame_rate_index;
370
33.0k
    dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
371
33.0k
    dsh->clean_width        = dirac_source_parameters_defaults[video_format].clean_width;
372
33.0k
    dsh->clean_height       = dirac_source_parameters_defaults[video_format].clean_height;
373
33.0k
    dsh->clean_left_offset  = dirac_source_parameters_defaults[video_format].clean_left_offset;
374
33.0k
    dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
375
33.0k
    dsh->pixel_range_index  = dirac_source_parameters_defaults[video_format].pixel_range_index;
376
33.0k
    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
33.0k
    ret = parse_source_parameters(dsh, &gb, log_ctx);
381
33.0k
    if (ret < 0)
382
10.8k
        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.1k
    picture_coding_mode = get_interleaved_ue_golomb(&gb);
387
22.1k
    if (picture_coding_mode != 0) {
388
6.81k
        if (log_ctx) {
389
6.81k
            av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
390
6.81k
                   picture_coding_mode);
391
6.81k
        }
392
6.81k
        ret = AVERROR_INVALIDDATA;
393
6.81k
        goto fail;
394
6.81k
    }
395
396
15.3k
    *pdsh = dsh;
397
15.3k
    return 0;
398
19.1k
fail:
399
19.1k
    av_freep(&dsh);
400
    *pdsh = NULL;
401
19.1k
    return ret;
402
22.1k
}
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