FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/dirac.c
Date: 2021-09-24 20:55:06
Exec Total Coverage
Lines: 101 143 70.6%
Branches: 45 92 48.9%

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