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"
34 #include "mpeg12data.h"
36 #if CONFIG_DIRAC_PARSE
38 typedef struct dirac_source_params {
41 uint8_t chroma_format; ///< 0: 444 1: 422 2: 420
44 uint8_t top_field_first;
46 uint8_t frame_rate_index; ///< index into dirac_frame_rate[]
47 uint8_t aspect_ratio_index; ///< index into dirac_aspect_ratio[]
50 uint16_t clean_height;
51 uint16_t clean_left_offset;
52 uint16_t clean_right_offset;
54 uint8_t pixel_range_index; ///< index into dirac_pixel_range_presets[]
55 uint8_t color_spec_index; ///< index into dirac_color_spec_presets[]
56 } dirac_source_params;
58 // defaults for source parameters
59 static const dirac_source_params dirac_source_parameters_defaults[] = {
60 { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
61 { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
62 { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
63 { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
64 { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
65 { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
66 { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
67 { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
68 { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
70 { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
71 { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
72 { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
73 { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
74 { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
75 { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
76 { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
77 { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
79 { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
80 { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
81 { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
82 { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
85 /* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
86 static const AVRational dirac_preset_aspect_ratios[] = {
95 /* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
96 * Table 10.3 Available preset frame rate values
98 static const AVRational dirac_frame_rate[] = {
103 /* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
105 static const struct {
107 enum AVColorRange color_range;
108 } pixel_range_presets[] = {
109 { 8, AVCOL_RANGE_JPEG },
110 { 8, AVCOL_RANGE_MPEG },
111 { 10, AVCOL_RANGE_MPEG },
112 { 12, AVCOL_RANGE_MPEG },
115 static const enum AVColorPrimaries dirac_primaries[] = {
121 static const struct {
122 enum AVColorPrimaries color_primaries;
123 enum AVColorSpace colorspace;
124 enum AVColorTransferCharacteristic color_trc;
125 } dirac_color_presets[] = {
126 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
127 { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
128 { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
129 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
130 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ },
133 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats + luma Offset */
134 static const enum AVPixelFormat dirac_pix_fmt[2][3] = {
135 { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
136 { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P },
139 /* [DIRAC_STD] 10.3 Parse Source Parameters.
140 * source_parameters(base_video_format) */
141 static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb,
144 AVRational frame_rate = { 0, 0 };
145 unsigned luma_depth = 8, luma_offset = 16;
148 /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
149 /* [DIRAC_STD] custom_dimensions_flag */
151 dsh->width = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
152 dsh->height = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
155 /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
156 * chroma_sampling_format(video_params) */
157 /* [DIRAC_STD] custom_chroma_format_flag */
159 /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
160 dsh->chroma_format = get_interleaved_ue_golomb(gb);
161 if (dsh->chroma_format > 2) {
163 av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
165 return AVERROR_INVALIDDATA;
168 /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
169 /* [DIRAC_STD] custom_scan_format_flag */
171 /* [DIRAC_STD] SOURCE_SAMPLING */
172 dsh->interlaced = get_interleaved_ue_golomb(gb);
173 if (dsh->interlaced > 1)
174 return AVERROR_INVALIDDATA;
176 /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
177 if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
178 dsh->frame_rate_index = get_interleaved_ue_golomb(gb);
180 if (dsh->frame_rate_index > 10)
181 return AVERROR_INVALIDDATA;
183 if (!dsh->frame_rate_index) {
184 /* [DIRAC_STD] FRAME_RATE_NUMER */
185 frame_rate.num = get_interleaved_ue_golomb(gb);
186 /* [DIRAC_STD] FRAME_RATE_DENOM */
187 frame_rate.den = get_interleaved_ue_golomb(gb);
190 /* [DIRAC_STD] preset_frame_rate(video_params, index) */
191 if (dsh->frame_rate_index > 0) {
192 if (dsh->frame_rate_index <= 8)
193 frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
195 /* [DIRAC_STD] Table 10.3 values 9-10 */
196 frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
198 dsh->framerate = frame_rate;
200 /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
201 * pixel_aspect_ratio(video_params) */
202 if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
203 /* [DIRAC_STD] index */
204 dsh->aspect_ratio_index = get_interleaved_ue_golomb(gb);
206 if (dsh->aspect_ratio_index > 6)
207 return AVERROR_INVALIDDATA;
209 if (!dsh->aspect_ratio_index) {
210 dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(gb);
211 dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(gb);
214 /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
215 * aspect ratio values */
216 if (dsh->aspect_ratio_index > 0)
217 dsh->sample_aspect_ratio =
218 dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
220 /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
221 if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
222 /* [DIRAC_STD] CLEAN_WIDTH */
223 dsh->clean_width = get_interleaved_ue_golomb(gb);
224 /* [DIRAC_STD] CLEAN_HEIGHT */
225 dsh->clean_height = get_interleaved_ue_golomb(gb);
226 /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
227 dsh->clean_left_offset = get_interleaved_ue_golomb(gb);
228 /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
229 dsh->clean_right_offset = get_interleaved_ue_golomb(gb);
232 /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
233 * WARNING: Some adaptation seems to be done using the
234 * AVCOL_RANGE_MPEG/JPEG values */
235 if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
236 /* [DIRAC_STD] index */
237 dsh->pixel_range_index = get_interleaved_ue_golomb(gb);
239 if (dsh->pixel_range_index > 4)
240 return AVERROR_INVALIDDATA;
242 // This assumes either fullrange or MPEG levels only
243 if (!dsh->pixel_range_index) {
244 luma_offset = get_interleaved_ue_golomb(gb);
245 luma_depth = av_log2(get_interleaved_ue_golomb(gb)) + 1;
246 get_interleaved_ue_golomb(gb); /* chroma offset */
247 get_interleaved_ue_golomb(gb); /* chroma excursion */
248 dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
252 /* [DIRAC_STD] Table 10.5
253 * Available signal range presets <--> pixel_range_presets */
254 if (dsh->pixel_range_index > 0) {
255 idx = dsh->pixel_range_index - 1;
256 luma_depth = pixel_range_presets[idx].bitdepth;
257 dsh->color_range = pixel_range_presets[idx].color_range;
260 if (luma_depth > 8 && log_ctx)
261 av_log(log_ctx, AV_LOG_WARNING, "Bitdepth greater than 8");
263 dsh->pix_fmt = dirac_pix_fmt[!luma_offset][dsh->chroma_format];
265 /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
266 if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
267 /* [DIRAC_STD] index */
268 idx = dsh->color_spec_index = get_interleaved_ue_golomb(gb);
270 if (dsh->color_spec_index > 4)
271 return AVERROR_INVALIDDATA;
273 dsh->color_primaries = dirac_color_presets[idx].color_primaries;
274 dsh->colorspace = dirac_color_presets[idx].colorspace;
275 dsh->color_trc = dirac_color_presets[idx].color_trc;
277 if (!dsh->color_spec_index) {
278 /* [DIRAC_STD] 10.3.9.1 Colour primaries */
280 idx = get_interleaved_ue_golomb(gb);
282 dsh->color_primaries = dirac_primaries[idx];
284 /* [DIRAC_STD] 10.3.9.2 Colour matrix */
286 idx = get_interleaved_ue_golomb(gb);
288 dsh->colorspace = AVCOL_SPC_BT709;
290 dsh->colorspace = AVCOL_SPC_BT470BG;
292 /* [DIRAC_STD] 10.3.9.3 Transfer function */
293 if (get_bits1(gb) && !get_interleaved_ue_golomb(gb))
294 dsh->color_trc = AVCOL_TRC_BT709;
297 idx = dsh->color_spec_index;
298 dsh->color_primaries = dirac_color_presets[idx].color_primaries;
299 dsh->colorspace = dirac_color_presets[idx].colorspace;
300 dsh->color_trc = dirac_color_presets[idx].color_trc;
306 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
307 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
308 const uint8_t *buf, size_t buf_size,
311 AVDiracSeqHeader *dsh;
313 unsigned version_major;
314 unsigned video_format, picture_coding_mode;
317 dsh = av_mallocz(sizeof(*dsh));
319 return AVERROR(ENOMEM);
321 ret = init_get_bits8(&gb, buf, buf_size);
325 /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
326 version_major = get_interleaved_ue_golomb(&gb);
327 get_interleaved_ue_golomb(&gb); /* version_minor */
328 dsh->profile = get_interleaved_ue_golomb(&gb);
329 dsh->level = get_interleaved_ue_golomb(&gb);
330 /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
331 * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
332 video_format = get_interleaved_ue_golomb(&gb);
335 if (version_major < 2)
336 av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
337 else if (version_major > 2)
338 av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
341 if (video_format > 20) {
342 ret = AVERROR_INVALIDDATA;
346 // Fill in defaults for the source parameters.
347 dsh->width = dirac_source_parameters_defaults[video_format].width;
348 dsh->height = dirac_source_parameters_defaults[video_format].height;
349 dsh->chroma_format = dirac_source_parameters_defaults[video_format].chroma_format;
350 dsh->interlaced = dirac_source_parameters_defaults[video_format].interlaced;
351 dsh->top_field_first = dirac_source_parameters_defaults[video_format].top_field_first;
352 dsh->frame_rate_index = dirac_source_parameters_defaults[video_format].frame_rate_index;
353 dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
354 dsh->clean_width = dirac_source_parameters_defaults[video_format].clean_width;
355 dsh->clean_height = dirac_source_parameters_defaults[video_format].clean_height;
356 dsh->clean_left_offset = dirac_source_parameters_defaults[video_format].clean_left_offset;
357 dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
358 dsh->pixel_range_index = dirac_source_parameters_defaults[video_format].pixel_range_index;
359 dsh->color_spec_index = dirac_source_parameters_defaults[video_format].color_spec_index;
361 /* [DIRAC_STD] 10.3 Source Parameters
362 * Override the defaults. */
363 ret = parse_source_parameters(dsh, &gb, log_ctx);
367 /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
368 * currently only used to signal field coding */
369 picture_coding_mode = get_interleaved_ue_golomb(&gb);
370 if (picture_coding_mode != 0) {
372 av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
373 picture_coding_mode);
375 ret = AVERROR_INVALIDDATA;
387 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
388 const uint8_t *buf, size_t buf_size,
391 return AVERROR(ENOSYS);