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 + luma Offset */
113 static const enum AVPixelFormat dirac_pix_fmt[2][3] = {
114 { AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV420P },
115 { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P },
118 /* [DIRAC_STD] 10.3 Parse Source Parameters.
119 * source_parameters(base_video_format) */
120 static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb,
121 dirac_source_params *source)
123 AVRational frame_rate = { 0, 0 };
124 unsigned luma_depth = 8, luma_offset = 16;
126 int chroma_x_shift, chroma_y_shift;
128 /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
129 /* [DIRAC_STD] custom_dimensions_flag */
131 source->width = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
132 source->height = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
135 /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
136 * chroma_sampling_format(video_params) */
137 /* [DIRAC_STD] custom_chroma_format_flag */
139 /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
140 source->chroma_format = svq3_get_ue_golomb(gb);
141 if (source->chroma_format > 2U) {
142 av_log(avctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
143 source->chroma_format);
144 return AVERROR_INVALIDDATA;
147 /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
148 /* [DIRAC_STD] custom_scan_format_flag */
150 /* [DIRAC_STD] SOURCE_SAMPLING */
151 source->interlaced = svq3_get_ue_golomb(gb);
152 if (source->interlaced > 1U)
153 return AVERROR_INVALIDDATA;
155 /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
156 if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
157 source->frame_rate_index = svq3_get_ue_golomb(gb);
159 if (source->frame_rate_index > 10U)
160 return AVERROR_INVALIDDATA;
162 if (!source->frame_rate_index) {
163 /* [DIRAC_STD] FRAME_RATE_NUMER */
164 frame_rate.num = svq3_get_ue_golomb(gb);
165 /* [DIRAC_STD] FRAME_RATE_DENOM */
166 frame_rate.den = svq3_get_ue_golomb(gb);
169 /* [DIRAC_STD] preset_frame_rate(video_params, index) */
170 if (source->frame_rate_index > 0) {
171 if (source->frame_rate_index <= 8)
172 frame_rate = ff_mpeg12_frame_rate_tab[source->frame_rate_index];
174 /* [DIRAC_STD] Table 10.3 values 9-10 */
175 frame_rate = dirac_frame_rate[source->frame_rate_index - 9];
177 av_reduce(&avctx->time_base.num, &avctx->time_base.den,
178 frame_rate.den, frame_rate.num, 1 << 30);
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;
241 av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8\n");
243 avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format];
244 avcodec_get_chroma_sub_sample(avctx->pix_fmt, &chroma_x_shift, &chroma_y_shift);
245 if ((source->width % (1<<chroma_x_shift)) || (source->height % (1<<chroma_y_shift))) {
246 av_log(avctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
247 return AVERROR_INVALIDDATA;
251 /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
252 if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
253 /* [DIRAC_STD] index */
254 idx = source->color_spec_index = svq3_get_ue_golomb(gb);
256 if (source->color_spec_index > 4U)
257 return AVERROR_INVALIDDATA;
259 avctx->color_primaries = dirac_color_presets[idx].color_primaries;
260 avctx->colorspace = dirac_color_presets[idx].colorspace;
261 avctx->color_trc = dirac_color_presets[idx].color_trc;
263 if (!source->color_spec_index) {
264 /* [DIRAC_STD] 10.3.9.1 Colour primaries */
266 idx = svq3_get_ue_golomb(gb);
268 avctx->color_primaries = dirac_primaries[idx];
270 /* [DIRAC_STD] 10.3.9.2 Colour matrix */
272 idx = svq3_get_ue_golomb(gb);
274 avctx->colorspace = AVCOL_SPC_BT709;
276 avctx->colorspace = AVCOL_SPC_BT470BG;
278 /* [DIRAC_STD] 10.3.9.3 Transfer function */
279 if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
280 avctx->color_trc = AVCOL_TRC_BT709;
283 idx = source->color_spec_index;
284 avctx->color_primaries = dirac_color_presets[idx].color_primaries;
285 avctx->colorspace = dirac_color_presets[idx].colorspace;
286 avctx->color_trc = dirac_color_presets[idx].color_trc;
292 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
293 int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb,
294 dirac_source_params *source)
296 unsigned version_major;
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 svq3_get_ue_golomb(gb); /* version_minor */
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))
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;