2 * Copyright (C) 2007 Marco Gerards <marco@gnu.org>
3 * Copyright (C) 2009 David Conrad
4 * Copyright (C) 2011 Jordi Ortiz
6 * This file is part of FFmpeg.
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.
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.
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
26 * @author Marco Gerards <marco@gnu.org>, David Conrad, Jordi Ortiz <nenjordi@gmail.com>
29 #include "libavutil/imgutils.h"
35 #include "mpeg12data.h"
37 /* defaults for source parameters */
38 static const dirac_source_params dirac_source_parameters_defaults[] = {
39 { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
40 { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
41 { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
42 { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
43 { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
44 { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
45 { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
46 { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
47 { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
49 { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
50 { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
51 { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
52 { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
53 { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
54 { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
55 { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
56 { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
58 { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
59 { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
60 { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
61 { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
64 /* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
65 static const AVRational dirac_preset_aspect_ratios[] = {
74 /* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
75 * Table 10.3 Available preset frame rate values
77 static const AVRational dirac_frame_rate[] = {
82 /* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
86 enum AVColorRange color_range;
87 } pixel_range_presets[] = {
88 { 8, AVCOL_RANGE_JPEG },
89 { 8, AVCOL_RANGE_MPEG },
90 { 10, AVCOL_RANGE_MPEG },
91 { 12, AVCOL_RANGE_MPEG },
94 static const enum AVColorPrimaries dirac_primaries[] = {
100 static const struct {
101 enum AVColorPrimaries color_primaries;
102 enum AVColorSpace colorspace;
103 enum AVColorTransferCharacteristic color_trc;
104 } dirac_color_presets[] = {
105 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
106 { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
107 { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
108 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
109 { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_UNSPECIFIED /* DCinema */ },
112 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats */
113 static const enum AVPixelFormat dirac_pix_fmt[][3] = {
114 {AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12},
115 {AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12},
116 {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12},
119 /* [DIRAC_STD] 10.3 Parse Source Parameters.
120 * source_parameters(base_video_format) */
121 static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb,
122 dirac_source_params *source, int *bit_depth)
124 AVRational frame_rate = { 0, 0 };
125 unsigned luma_depth = 8, luma_offset = 16;
127 int chroma_x_shift, chroma_y_shift;
129 /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
130 /* [DIRAC_STD] custom_dimensions_flag */
132 source->width = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
133 source->height = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
136 /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
137 * chroma_sampling_format(video_params) */
138 /* [DIRAC_STD] custom_chroma_format_flag */
140 /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
141 source->chroma_format = svq3_get_ue_golomb(gb);
142 if (source->chroma_format > 2U) {
143 av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
144 source->chroma_format);
145 return AVERROR_INVALIDDATA;
148 /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
149 /* [DIRAC_STD] custom_scan_format_flag */
151 /* [DIRAC_STD] SOURCE_SAMPLING */
152 source->interlaced = svq3_get_ue_golomb(gb);
153 if (source->interlaced > 1U)
154 return AVERROR_INVALIDDATA;
156 /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
157 if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
158 source->frame_rate_index = svq3_get_ue_golomb(gb);
160 if (source->frame_rate_index > 10U)
161 return AVERROR_INVALIDDATA;
163 if (!source->frame_rate_index) {
164 /* [DIRAC_STD] FRAME_RATE_NUMER */
165 frame_rate.num = svq3_get_ue_golomb(gb);
166 /* [DIRAC_STD] FRAME_RATE_DENOM */
167 frame_rate.den = svq3_get_ue_golomb(gb);
170 /* [DIRAC_STD] preset_frame_rate(video_params, index) */
171 if (source->frame_rate_index > 0) {
172 if (source->frame_rate_index <= 8)
173 frame_rate = ff_mpeg12_frame_rate_tab[source->frame_rate_index];
175 /* [DIRAC_STD] Table 10.3 values 9-10 */
176 frame_rate = dirac_frame_rate[source->frame_rate_index - 9];
178 avctx->framerate = frame_rate;
180 /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
181 * pixel_aspect_ratio(video_params) */
182 if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
183 /* [DIRAC_STD] index */
184 source->aspect_ratio_index = svq3_get_ue_golomb(gb);
186 if (source->aspect_ratio_index > 6U)
187 return AVERROR_INVALIDDATA;
189 if (!source->aspect_ratio_index) {
190 avctx->sample_aspect_ratio.num = svq3_get_ue_golomb(gb);
191 avctx->sample_aspect_ratio.den = svq3_get_ue_golomb(gb);
194 /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
195 * aspect ratio values */
196 if (source->aspect_ratio_index > 0)
197 avctx->sample_aspect_ratio =
198 dirac_preset_aspect_ratios[source->aspect_ratio_index - 1];
200 /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
201 if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
202 /* [DIRAC_STD] CLEAN_WIDTH */
203 source->clean_width = svq3_get_ue_golomb(gb);
204 /* [DIRAC_STD] CLEAN_HEIGHT */
205 source->clean_height = svq3_get_ue_golomb(gb);
206 /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
207 source->clean_left_offset = svq3_get_ue_golomb(gb);
208 /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
209 source->clean_right_offset = svq3_get_ue_golomb(gb);
212 /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
213 * WARNING: Some adaptation seems to be done using the
214 * AVCOL_RANGE_MPEG/JPEG values */
215 if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
216 /* [DIRAC_STD] index */
217 source->pixel_range_index = svq3_get_ue_golomb(gb);
219 if (source->pixel_range_index > 4U)
220 return AVERROR_INVALIDDATA;
222 /* This assumes either fullrange or MPEG levels only */
223 if (!source->pixel_range_index) {
224 luma_offset = svq3_get_ue_golomb(gb);
225 luma_depth = av_log2(svq3_get_ue_golomb(gb)) + 1;
226 svq3_get_ue_golomb(gb); /* chroma offset */
227 svq3_get_ue_golomb(gb); /* chroma excursion */
228 avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG
232 /* [DIRAC_STD] Table 10.5
233 * Available signal range presets <--> pixel_range_presets */
234 if (source->pixel_range_index > 0) {
235 idx = source->pixel_range_index - 1;
236 luma_depth = pixel_range_presets[idx].bitdepth;
237 avctx->color_range = pixel_range_presets[idx].color_range;
240 *bit_depth = luma_depth;
242 avctx->pix_fmt = dirac_pix_fmt[source->chroma_format][source->pixel_range_index-2];
243 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_x_shift, &chroma_y_shift);
244 if ((source->width % (1<<chroma_x_shift)) || (source->height % (1<<chroma_y_shift))) {
245 av_log(avctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
246 return AVERROR_INVALIDDATA;
250 /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
251 if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
252 /* [DIRAC_STD] index */
253 idx = source->color_spec_index = svq3_get_ue_golomb(gb);
255 if (source->color_spec_index > 4U)
256 return AVERROR_INVALIDDATA;
258 avctx->color_primaries = dirac_color_presets[idx].color_primaries;
259 avctx->colorspace = dirac_color_presets[idx].colorspace;
260 avctx->color_trc = dirac_color_presets[idx].color_trc;
262 if (!source->color_spec_index) {
263 /* [DIRAC_STD] 10.3.9.1 Colour primaries */
265 idx = svq3_get_ue_golomb(gb);
267 avctx->color_primaries = dirac_primaries[idx];
269 /* [DIRAC_STD] 10.3.9.2 Colour matrix */
271 idx = svq3_get_ue_golomb(gb);
273 avctx->colorspace = AVCOL_SPC_BT709;
275 avctx->colorspace = AVCOL_SPC_BT470BG;
277 /* [DIRAC_STD] 10.3.9.3 Transfer function */
278 if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
279 avctx->color_trc = AVCOL_TRC_BT709;
282 idx = source->color_spec_index;
283 avctx->color_primaries = dirac_color_presets[idx].color_primaries;
284 avctx->colorspace = dirac_color_presets[idx].colorspace;
285 avctx->color_trc = dirac_color_presets[idx].color_trc;
291 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
292 int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb,
293 dirac_source_params *source,
294 DiracVersionInfo *version,
297 unsigned video_format, picture_coding_mode;
300 /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
301 version->major = svq3_get_ue_golomb(gb);
302 version->minor = svq3_get_ue_golomb(gb);
303 avctx->profile = svq3_get_ue_golomb(gb);
304 avctx->level = svq3_get_ue_golomb(gb);
305 /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
306 * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
307 video_format = svq3_get_ue_golomb(gb);
309 if (version->major < 2)
310 av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n");
311 else if (version->major > 2)
312 av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
314 if (video_format > 20U)
315 return AVERROR_INVALIDDATA;
317 /* Fill in defaults for the source parameters. */
318 *source = dirac_source_parameters_defaults[video_format];
320 /* [DIRAC_STD] 10.3 Source Parameters
321 * Override the defaults. */
322 if (ret = parse_source_parameters(avctx, gb, source, bit_depth))
325 ret = ff_set_dimensions(avctx, source->width, source->height);
329 ff_set_sar(avctx, avctx->sample_aspect_ratio);
331 /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
332 * currently only used to signal field coding */
333 picture_coding_mode = svq3_get_ue_golomb(gb);
334 if (picture_coding_mode != 0) {
335 av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d\n",
336 picture_coding_mode);
337 return AVERROR_INVALIDDATA;