FFmpeg
Loading...
Searching...
No Matches
dirac.c
Go to the documentation of this file.
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
41typedef 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 */
62static 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 */
89static 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 */
101static 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 */
108static const struct {
109 uint8_t bitdepth;
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
118static const enum AVColorPrimaries dirac_primaries[] = {
122};
123
124static const struct {
126 enum AVColorSpace colorspace;
127 enum AVColorTransferCharacteristic color_trc;
128} dirac_color_presets[] = {
134};
135
136/* [DIRAC_STD] Table 10.2 Supported chroma sampling formats */
137static const enum AVPixelFormat dirac_pix_fmt[][3] = {
141};
142
143/* [DIRAC_STD] 10.3 Parse Source Parameters.
144 * source_parameters(base_video_format) */
145static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb,
146 void *log_ctx)
147{
148 AVRational frame_rate = { 0, 0 };
149 unsigned luma_depth = 8, luma_offset = 16;
150 int idx;
151 int chroma_x_shift, chroma_y_shift;
152 int ret;
153
154 /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
155 /* [DIRAC_STD] custom_dimensions_flag */
156 if (get_bits1(gb)) {
157 dsh->width = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
158 dsh->height = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
159 }
160
161 /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
162 * chroma_sampling_format(video_params) */
163 /* [DIRAC_STD] custom_chroma_format_flag */
164 if (get_bits1(gb))
165 /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
167 if (dsh->chroma_format > 2U) {
168 if (log_ctx)
169 av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
170 dsh->chroma_format);
171 return AVERROR_INVALIDDATA;
172 }
173
174 /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
175 /* [DIRAC_STD] custom_scan_format_flag */
176 if (get_bits1(gb))
177 /* [DIRAC_STD] SOURCE_SAMPLING */
179 if (dsh->interlaced > 1U)
180 return AVERROR_INVALIDDATA;
181
182 /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
183 if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
185
186 if (dsh->frame_rate_index > 10U)
187 return AVERROR_INVALIDDATA;
188
189 if (!dsh->frame_rate_index) {
190 /* [DIRAC_STD] FRAME_RATE_NUMER */
191 frame_rate.num = get_interleaved_ue_golomb(gb);
192 /* [DIRAC_STD] FRAME_RATE_DENOM */
193 frame_rate.den = get_interleaved_ue_golomb(gb);
194 }
195 }
196 /* [DIRAC_STD] preset_frame_rate(video_params, index) */
197 if (dsh->frame_rate_index > 0) {
198 if (dsh->frame_rate_index <= 8)
200 else
201 /* [DIRAC_STD] Table 10.3 values 9-10 */
202 frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
203 }
204 dsh->framerate = frame_rate;
205
206 /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
207 * pixel_aspect_ratio(video_params) */
208 if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
209 /* [DIRAC_STD] index */
211
212 if (dsh->aspect_ratio_index > 6U)
213 return AVERROR_INVALIDDATA;
214
215 if (!dsh->aspect_ratio_index) {
218 }
219 }
220 /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
221 * aspect ratio values */
222 if (dsh->aspect_ratio_index > 0)
224 dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
225
226 /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
227 if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
228 /* [DIRAC_STD] CLEAN_WIDTH */
230 /* [DIRAC_STD] CLEAN_HEIGHT */
232 /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
234 /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
236 }
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 if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
242 /* [DIRAC_STD] index */
244
245 if (dsh->pixel_range_index > 4U)
246 return AVERROR_INVALIDDATA;
247
248 /* This assumes either fullrange or MPEG levels only */
249 if (!dsh->pixel_range_index) {
250 luma_offset = get_interleaved_ue_golomb(gb);
251 luma_depth = av_log2(get_interleaved_ue_golomb(gb)) + 1;
252 get_interleaved_ue_golomb(gb); /* chroma offset */
253 get_interleaved_ue_golomb(gb); /* chroma excursion */
254 dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
256 }
257 }
258 /* [DIRAC_STD] Table 10.5
259 * Available signal range presets <--> pixel_range_presets */
260 if (dsh->pixel_range_index > 0) {
261 idx = dsh->pixel_range_index - 1;
262 luma_depth = pixel_range_presets[idx].bitdepth;
263 dsh->color_range = pixel_range_presets[idx].color_range;
264 }
265
266 dsh->bit_depth = luma_depth;
267
268 /* Full range 8 bts uses the same pix_fmts as limited range 8 bits */
269 dsh->pixel_range_index += dsh->pixel_range_index == 1;
270
271 if (dsh->pixel_range_index < 2U)
272 return AVERROR_INVALIDDATA;
273
274 dsh->pix_fmt = dirac_pix_fmt[dsh->chroma_format][dsh->pixel_range_index-2];
275 ret = av_pix_fmt_get_chroma_sub_sample(dsh->pix_fmt, &chroma_x_shift, &chroma_y_shift);
276 if (ret)
277 return ret;
278
279 if ((dsh->width % (1<<chroma_x_shift)) || (dsh->height % (1<<chroma_y_shift))) {
280 if (log_ctx)
281 av_log(log_ctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
282 return AVERROR_INVALIDDATA;
283 }
284
285 /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
286 if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
287 /* [DIRAC_STD] index */
289
290 if (dsh->color_spec_index > 4U)
291 return AVERROR_INVALIDDATA;
292
293 dsh->color_primaries = dirac_color_presets[idx].color_primaries;
294 dsh->colorspace = dirac_color_presets[idx].colorspace;
295 dsh->color_trc = dirac_color_presets[idx].color_trc;
296
297 if (!dsh->color_spec_index) {
298 /* [DIRAC_STD] 10.3.9.1 Colour primaries */
299 if (get_bits1(gb)) {
301 if (idx < 3U)
302 dsh->color_primaries = dirac_primaries[idx];
303 }
304 /* [DIRAC_STD] 10.3.9.2 Colour matrix */
305 if (get_bits1(gb)) {
307 if (!idx)
309 else if (idx == 1)
311 }
312 /* [DIRAC_STD] 10.3.9.3 Transfer function */
313 if (get_bits1(gb) && !get_interleaved_ue_golomb(gb))
315 }
316 } else {
317 idx = dsh->color_spec_index;
318 dsh->color_primaries = dirac_color_presets[idx].color_primaries;
319 dsh->colorspace = dirac_color_presets[idx].colorspace;
320 dsh->color_trc = dirac_color_presets[idx].color_trc;
321 }
322
323 return 0;
324}
325
326/* [DIRAC_STD] 10. Sequence Header. sequence_header() */
328 const uint8_t *buf, size_t buf_size,
329 void *log_ctx)
330{
331 AVDiracSeqHeader *dsh;
332 GetBitContext gb;
333 unsigned video_format, picture_coding_mode;
334 int ret;
335
336 dsh = av_mallocz(sizeof(*dsh));
337 if (!dsh)
338 return AVERROR(ENOMEM);
339
340 ret = init_get_bits8(&gb, buf, buf_size);
341 if (ret < 0)
342 goto fail;
343
344 /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
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 video_format = get_interleaved_ue_golomb(&gb);
352
353 if (dsh->version.major < 2 && log_ctx)
354 av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
355 else if (dsh->version.major > 2 && log_ctx)
356 av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
357
358 if (video_format > 20U) {
360 goto fail;
361 }
362
363 /* Fill in defaults for the source parameters. */
364 dsh->width = dirac_source_parameters_defaults[video_format].width;
365 dsh->height = dirac_source_parameters_defaults[video_format].height;
366 dsh->chroma_format = dirac_source_parameters_defaults[video_format].chroma_format;
367 dsh->interlaced = dirac_source_parameters_defaults[video_format].interlaced;
368 dsh->top_field_first = dirac_source_parameters_defaults[video_format].top_field_first;
369 dsh->frame_rate_index = dirac_source_parameters_defaults[video_format].frame_rate_index;
370 dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
371 dsh->clean_width = dirac_source_parameters_defaults[video_format].clean_width;
372 dsh->clean_height = dirac_source_parameters_defaults[video_format].clean_height;
373 dsh->clean_left_offset = dirac_source_parameters_defaults[video_format].clean_left_offset;
374 dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
375 dsh->pixel_range_index = dirac_source_parameters_defaults[video_format].pixel_range_index;
376 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 ret = parse_source_parameters(dsh, &gb, log_ctx);
381 if (ret < 0)
382 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 picture_coding_mode = get_interleaved_ue_golomb(&gb);
387 if (picture_coding_mode != 0) {
388 if (log_ctx) {
389 av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
390 picture_coding_mode);
391 }
393 goto fail;
394 }
395
396 *pdsh = dsh;
397 return 0;
398fail:
399 av_freep(&dsh);
400 *pdsh = NULL;
401 return ret;
402}
403#else
405 const uint8_t *buf, size_t buf_size,
406 void *log_ctx)
407{
408 return AVERROR(ENOSYS);
409}
410#endif
#define bitdepth
#define NULL
Definition coverity.c:32
static const AVColorPrimariesDesc color_primaries[AVCOL_PRI_NB]
Definition csp.c:76
int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh, const uint8_t *buf, size_t buf_size, void *log_ctx)
Parse a Dirac sequence header.
Definition dirac.c:404
Interface to Dirac Decoder/Encoder.
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
Definition get_bits.h:391
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition get_bits.h:544
exp golomb vlc stuff
static unsigned get_interleaved_ue_golomb(GetBitContext *gb)
Definition golomb.h:143
#define fail
Definition test.h:479
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition error.h:61
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
#define av_log2
Definition intmath.h:84
Memory handling functions.
MPEG-1/2 tables.
const AVRational ff_mpeg12_frame_rate_tab[]
uint8_t interlaced
Definition mxfenc.c:2336
int av_pix_fmt_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition pixdesc.c:3488
#define AV_PIX_FMT_YUV444P12
Definition pixfmt.h:552
#define AV_PIX_FMT_YUV420P10
Definition pixfmt.h:545
AVColorRange
Visual content value range.
Definition pixfmt.h:748
@ AVCOL_RANGE_MPEG
Narrow or limited range content.
Definition pixfmt.h:766
@ AVCOL_RANGE_JPEG
Full range content.
Definition pixfmt.h:783
#define AV_PIX_FMT_YUV420P12
Definition pixfmt.h:549
#define AV_PIX_FMT_YUV422P12
Definition pixfmt.h:550
#define AV_PIX_FMT_YUV422P10
Definition pixfmt.h:546
AVPixelFormat
Pixel format.
Definition pixfmt.h:71
@ AV_PIX_FMT_YUV420P
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition pixfmt.h:73
@ AV_PIX_FMT_YUV422P
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition pixfmt.h:77
@ AV_PIX_FMT_YUV444P
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition pixfmt.h:78
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition pixfmt.h:642
@ AVCOL_PRI_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition pixfmt.h:649
@ AVCOL_PRI_BT709
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP 177 Annex B
Definition pixfmt.h:644
@ AVCOL_PRI_SMPTE170M
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition pixfmt.h:650
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition pixfmt.h:672
@ AVCOL_TRC_UNSPECIFIED
Definition pixfmt.h:675
@ AVCOL_TRC_BT709
also ITU-R BT1361
Definition pixfmt.h:674
#define AV_PIX_FMT_YUV444P10
Definition pixfmt.h:548
AVColorSpace
YUV colorspace type.
Definition pixfmt.h:706
@ AVCOL_SPC_BT709
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / derived in SMPTE RP 177 Annex B
Definition pixfmt.h:708
@ AVCOL_SPC_BT470BG
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
Definition pixfmt.h:712
DiracVersionInfo version
Definition dirac.h:116
uint16_t clean_right_offset
Definition dirac.h:99
uint8_t top_field_first
Definition dirac.h:91
uint8_t chroma_format
0: 444 1: 422 2: 420
Definition dirac.h:88
enum AVColorPrimaries color_primaries
Definition dirac.h:112
uint8_t pixel_range_index
index into dirac_pixel_range_presets[]
Definition dirac.h:101
unsigned height
Definition dirac.h:87
enum AVColorRange color_range
Definition dirac.h:111
AVRational sample_aspect_ratio
Definition dirac.h:108
uint8_t color_spec_index
index into dirac_color_spec_presets[]
Definition dirac.h:102
unsigned width
Definition dirac.h:86
enum AVColorSpace colorspace
Definition dirac.h:114
uint16_t clean_height
Definition dirac.h:97
enum AVColorTransferCharacteristic color_trc
Definition dirac.h:113
enum AVPixelFormat pix_fmt
Definition dirac.h:110
uint8_t frame_rate_index
index into dirac_frame_rate[]
Definition dirac.h:93
uint8_t aspect_ratio_index
index into dirac_aspect_ratio[]
Definition dirac.h:94
uint8_t interlaced
Definition dirac.h:90
uint16_t clean_left_offset
Definition dirac.h:98
uint16_t clean_width
Definition dirac.h:96
AVRational framerate
Definition dirac.h:107
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
#define height
Definition dsp.h:89
#define width
Definition dsp.h:89
color_range