2 * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3 * Copyright (C) 2009 David Conrad
5 * This file is part of Libav.
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * @author Marco Gerards <marco@gnu.org>
28 #include "libavutil/imgutils.h"
31 #include "bitstream.h"
35 #include "mpeg12data.h"
37 #if CONFIG_DIRAC_PARSE
39 typedef struct dirac_source_params {
42 uint8_t chroma_format; ///< 0: 444 1: 422 2: 420
45 uint8_t top_field_first;
47 uint8_t frame_rate_index; ///< index into dirac_frame_rate[]
48 uint8_t aspect_ratio_index; ///< index into dirac_aspect_ratio[]
51 uint16_t clean_height;
52 uint16_t clean_left_offset;
53 uint16_t clean_right_offset;
55 uint8_t pixel_range_index; ///< index into dirac_pixel_range_presets[]
56 uint8_t color_spec_index; ///< index into dirac_color_spec_presets[]
57 } dirac_source_params;
59 // defaults for source parameters
60 static const dirac_source_params dirac_source_parameters_defaults[] = {
61 { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
62 { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
63 { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
64 { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
65 { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
66 { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
67 { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
68 { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
69 { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
71 { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
72 { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
73 { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
74 { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
75 { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
76 { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
77 { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
78 { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
80 { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
81 { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
82 { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
83 { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
86 /* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
87 static const AVRational dirac_preset_aspect_ratios[] = {
96 /* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
97 * Table 10.3 Available preset frame rate values
99 static const AVRational dirac_frame_rate[] = {
104 /* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
106 static const struct {
108 enum AVColorRange color_range;
109 } pixel_range_presets[] = {
110 { 8, AVCOL_RANGE_JPEG },
111 { 8, AVCOL_RANGE_MPEG },
112 { 10, AVCOL_RANGE_MPEG },
113 { 12, AVCOL_RANGE_MPEG },
116 static const enum AVColorPrimaries dirac_primaries[] = {
122 static const struct {
123 enum AVColorPrimaries color_primaries;
124 enum AVColorSpace colorspace;
125 enum AVColorTransferCharacteristic color_trc;
126 } dirac_color_presets[] = {
127 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
128 { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
129 { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
130 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
131 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ },
134 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats + luma Offset */
135 static const enum AVPixelFormat dirac_pix_fmt[2][3] = {
136 { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
137 { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P },
140 /* [DIRAC_STD] 10.3 Parse Source Parameters.
141 * source_parameters(base_video_format) */
142 static int parse_source_parameters(AVDiracSeqHeader *dsh, BitstreamContext *bc,
145 AVRational frame_rate = { 0, 0 };
146 unsigned luma_depth = 8, luma_offset = 16;
149 /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
150 /* [DIRAC_STD] custom_dimensions_flag */
151 if (bitstream_read_bit(bc)) {
152 dsh->width = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] FRAME_WIDTH */
153 dsh->height = get_interleaved_ue_golomb(bc); /* [DIRAC_STD] FRAME_HEIGHT */
156 /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
157 * chroma_sampling_format(video_params) */
158 /* [DIRAC_STD] custom_chroma_format_flag */
159 if (bitstream_read_bit(bc))
160 /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
161 dsh->chroma_format = get_interleaved_ue_golomb(bc);
162 if (dsh->chroma_format > 2) {
164 av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
166 return AVERROR_INVALIDDATA;
169 /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
170 /* [DIRAC_STD] custom_scan_format_flag */
171 if (bitstream_read_bit(bc))
172 /* [DIRAC_STD] SOURCE_SAMPLING */
173 dsh->interlaced = get_interleaved_ue_golomb(bc);
174 if (dsh->interlaced > 1)
175 return AVERROR_INVALIDDATA;
177 /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
178 if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_frame_rate_flag */
179 dsh->frame_rate_index = get_interleaved_ue_golomb(bc);
181 if (dsh->frame_rate_index > 10)
182 return AVERROR_INVALIDDATA;
184 if (!dsh->frame_rate_index) {
185 /* [DIRAC_STD] FRAME_RATE_NUMER */
186 frame_rate.num = get_interleaved_ue_golomb(bc);
187 /* [DIRAC_STD] FRAME_RATE_DENOM */
188 frame_rate.den = get_interleaved_ue_golomb(bc);
191 /* [DIRAC_STD] preset_frame_rate(video_params, index) */
192 if (dsh->frame_rate_index > 0) {
193 if (dsh->frame_rate_index <= 8)
194 frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
196 /* [DIRAC_STD] Table 10.3 values 9-10 */
197 frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
199 dsh->framerate = frame_rate;
201 /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
202 * pixel_aspect_ratio(video_params) */
203 if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
204 /* [DIRAC_STD] index */
205 dsh->aspect_ratio_index = get_interleaved_ue_golomb(bc);
207 if (dsh->aspect_ratio_index > 6)
208 return AVERROR_INVALIDDATA;
210 if (!dsh->aspect_ratio_index) {
211 dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(bc);
212 dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(bc);
215 /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
216 * aspect ratio values */
217 if (dsh->aspect_ratio_index > 0)
218 dsh->sample_aspect_ratio =
219 dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
221 /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
222 if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_clean_area_flag */
223 /* [DIRAC_STD] CLEAN_WIDTH */
224 dsh->clean_width = get_interleaved_ue_golomb(bc);
225 /* [DIRAC_STD] CLEAN_HEIGHT */
226 dsh->clean_height = get_interleaved_ue_golomb(bc);
227 /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
228 dsh->clean_left_offset = get_interleaved_ue_golomb(bc);
229 /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
230 dsh->clean_right_offset = get_interleaved_ue_golomb(bc);
233 /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
234 * WARNING: Some adaptation seems to be done using the
235 * AVCOL_RANGE_MPEG/JPEG values */
236 if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_signal_range_flag */
237 /* [DIRAC_STD] index */
238 dsh->pixel_range_index = get_interleaved_ue_golomb(bc);
240 if (dsh->pixel_range_index > 4)
241 return AVERROR_INVALIDDATA;
243 // This assumes either fullrange or MPEG levels only
244 if (!dsh->pixel_range_index) {
245 luma_offset = get_interleaved_ue_golomb(bc);
246 luma_depth = av_log2(get_interleaved_ue_golomb(bc)) + 1;
247 get_interleaved_ue_golomb(bc); /* chroma offset */
248 get_interleaved_ue_golomb(bc); /* chroma excursion */
249 dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
253 /* [DIRAC_STD] Table 10.5
254 * Available signal range presets <--> pixel_range_presets */
255 if (dsh->pixel_range_index > 0) {
256 idx = dsh->pixel_range_index - 1;
257 luma_depth = pixel_range_presets[idx].bitdepth;
258 dsh->color_range = pixel_range_presets[idx].color_range;
261 if (luma_depth > 8 && log_ctx)
262 av_log(log_ctx, AV_LOG_WARNING, "Bitdepth greater than 8");
264 dsh->pix_fmt = dirac_pix_fmt[!luma_offset][dsh->chroma_format];
266 /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
267 if (bitstream_read_bit(bc)) { /* [DIRAC_STD] custom_colour_spec_flag */
268 /* [DIRAC_STD] index */
269 idx = dsh->color_spec_index = get_interleaved_ue_golomb(bc);
271 if (dsh->color_spec_index > 4)
272 return AVERROR_INVALIDDATA;
274 dsh->color_primaries = dirac_color_presets[idx].color_primaries;
275 dsh->colorspace = dirac_color_presets[idx].colorspace;
276 dsh->color_trc = dirac_color_presets[idx].color_trc;
278 if (!dsh->color_spec_index) {
279 /* [DIRAC_STD] 10.3.9.1 Colour primaries */
280 if (bitstream_read_bit(bc)) {
281 idx = get_interleaved_ue_golomb(bc);
283 dsh->color_primaries = dirac_primaries[idx];
285 /* [DIRAC_STD] 10.3.9.2 Colour matrix */
286 if (bitstream_read_bit(bc)) {
287 idx = get_interleaved_ue_golomb(bc);
289 dsh->colorspace = AVCOL_SPC_BT709;
291 dsh->colorspace = AVCOL_SPC_BT470BG;
293 /* [DIRAC_STD] 10.3.9.3 Transfer function */
294 if (bitstream_read_bit(bc) && !get_interleaved_ue_golomb(bc))
295 dsh->color_trc = AVCOL_TRC_BT709;
298 idx = dsh->color_spec_index;
299 dsh->color_primaries = dirac_color_presets[idx].color_primaries;
300 dsh->colorspace = dirac_color_presets[idx].colorspace;
301 dsh->color_trc = dirac_color_presets[idx].color_trc;
307 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
308 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
309 const uint8_t *buf, size_t buf_size,
312 AVDiracSeqHeader *dsh;
314 unsigned version_major;
315 unsigned video_format, picture_coding_mode;
318 dsh = av_mallocz(sizeof(*dsh));
320 return AVERROR(ENOMEM);
322 ret = bitstream_init8(&bc, buf, buf_size);
326 /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
327 version_major = get_interleaved_ue_golomb(&bc);
328 get_interleaved_ue_golomb(&bc); /* version_minor */
329 dsh->profile = get_interleaved_ue_golomb(&bc);
330 dsh->level = get_interleaved_ue_golomb(&bc);
331 /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
332 * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
333 video_format = get_interleaved_ue_golomb(&bc);
336 if (version_major < 2)
337 av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
338 else if (version_major > 2)
339 av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
342 if (video_format > 20) {
343 ret = AVERROR_INVALIDDATA;
347 // Fill in defaults for the source parameters.
348 dsh->width = dirac_source_parameters_defaults[video_format].width;
349 dsh->height = dirac_source_parameters_defaults[video_format].height;
350 dsh->chroma_format = dirac_source_parameters_defaults[video_format].chroma_format;
351 dsh->interlaced = dirac_source_parameters_defaults[video_format].interlaced;
352 dsh->top_field_first = dirac_source_parameters_defaults[video_format].top_field_first;
353 dsh->frame_rate_index = dirac_source_parameters_defaults[video_format].frame_rate_index;
354 dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
355 dsh->clean_width = dirac_source_parameters_defaults[video_format].clean_width;
356 dsh->clean_height = dirac_source_parameters_defaults[video_format].clean_height;
357 dsh->clean_left_offset = dirac_source_parameters_defaults[video_format].clean_left_offset;
358 dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
359 dsh->pixel_range_index = dirac_source_parameters_defaults[video_format].pixel_range_index;
360 dsh->color_spec_index = dirac_source_parameters_defaults[video_format].color_spec_index;
362 /* [DIRAC_STD] 10.3 Source Parameters
363 * Override the defaults. */
364 ret = parse_source_parameters(dsh, &bc, log_ctx);
368 /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
369 * currently only used to signal field coding */
370 picture_coding_mode = get_interleaved_ue_golomb(&bc);
371 if (picture_coding_mode != 0) {
373 av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
374 picture_coding_mode);
376 ret = AVERROR_INVALIDDATA;
388 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
389 const uint8_t *buf, size_t buf_size,
392 return AVERROR(ENOSYS);