FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/imgutils.h"
30 
31 #include "avcodec.h"
32 #include "dirac.h"
33 #include "golomb.h"
34 #include "internal.h"
35 #include "mpeg12data.h"
36 
37 /* defaults for source parameters */
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 },
48 
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 },
57 
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 },
62 };
63 
64 /* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
66  { 1, 1 },
67  { 10, 11 },
68  { 12, 11 },
69  { 40, 33 },
70  { 16, 11 },
71  { 4, 3 },
72 };
73 
74 /* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
75  * Table 10.3 Available preset frame rate values
76  */
77 static const AVRational dirac_frame_rate[] = {
78  { 15000, 1001 },
79  { 25, 2 },
80 };
81 
82 /* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
83  * range presets */
84 static const struct {
87 } pixel_range_presets[] = {
88  { 8, AVCOL_RANGE_JPEG },
89  { 8, AVCOL_RANGE_MPEG },
90  { 10, AVCOL_RANGE_MPEG },
91  { 12, AVCOL_RANGE_MPEG },
92 };
93 
94 static const enum AVColorPrimaries dirac_primaries[] = {
98 };
99 
100 static const struct {
104 } dirac_color_presets[] = {
110 };
111 
112 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats + luma Offset */
113 static const enum AVPixelFormat dirac_pix_fmt[2][3] = {
116 };
117 
118 /* [DIRAC_STD] 10.3 Parse Source Parameters.
119  * source_parameters(base_video_format) */
121  dirac_source_params *source)
122 {
123  AVRational frame_rate = { 0, 0 };
124  unsigned luma_depth = 8, luma_offset = 16;
125  int idx;
126  int chroma_x_shift, chroma_y_shift;
127 
128  /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
129  /* [DIRAC_STD] custom_dimensions_flag */
130  if (get_bits1(gb)) {
131  source->width = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
132  source->height = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
133  }
134 
135  /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
136  * chroma_sampling_format(video_params) */
137  /* [DIRAC_STD] custom_chroma_format_flag */
138  if (get_bits1(gb))
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;
145  }
146 
147  /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
148  /* [DIRAC_STD] custom_scan_format_flag */
149  if (get_bits1(gb))
150  /* [DIRAC_STD] SOURCE_SAMPLING */
151  source->interlaced = svq3_get_ue_golomb(gb);
152  if (source->interlaced > 1U)
153  return AVERROR_INVALIDDATA;
154 
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);
158 
159  if (source->frame_rate_index > 10U)
160  return AVERROR_INVALIDDATA;
161 
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);
167  }
168  }
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];
173  else
174  /* [DIRAC_STD] Table 10.3 values 9-10 */
175  frame_rate = dirac_frame_rate[source->frame_rate_index - 9];
176  }
177  av_reduce(&avctx->time_base.num, &avctx->time_base.den,
178  frame_rate.den, frame_rate.num, 1 << 30);
179 
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 */
185 
186  if (source->aspect_ratio_index > 6U)
187  return AVERROR_INVALIDDATA;
188 
189  if (!source->aspect_ratio_index) {
192  }
193  }
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];
199 
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 */
208  /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
210  }
211 
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 */
218 
219  if (source->pixel_range_index > 4U)
220  return AVERROR_INVALIDDATA;
221 
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
230  }
231  }
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;
238  }
239 
240  if (luma_depth > 8)
241  av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8\n");
242 
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;
248  }
249 
250 
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);
255 
256  if (source->color_spec_index > 4U)
257  return AVERROR_INVALIDDATA;
258 
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;
262 
263  if (!source->color_spec_index) {
264  /* [DIRAC_STD] 10.3.9.1 Colour primaries */
265  if (get_bits1(gb)) {
266  idx = svq3_get_ue_golomb(gb);
267  if (idx < 3U)
268  avctx->color_primaries = dirac_primaries[idx];
269  }
270  /* [DIRAC_STD] 10.3.9.2 Colour matrix */
271  if (get_bits1(gb)) {
272  idx = svq3_get_ue_golomb(gb);
273  if (!idx)
274  avctx->colorspace = AVCOL_SPC_BT709;
275  else if (idx == 1)
276  avctx->colorspace = AVCOL_SPC_BT470BG;
277  }
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;
281  }
282  } else {
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;
287  }
288 
289  return 0;
290 }
291 
292 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
294  dirac_source_params *source)
295 {
296  unsigned version_major;
297  unsigned video_format, picture_coding_mode;
298  int ret;
299 
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);
308 
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");
313 
314  if (video_format > 20U)
315  return AVERROR_INVALIDDATA;
316 
317  /* Fill in defaults for the source parameters. */
318  *source = dirac_source_parameters_defaults[video_format];
319 
320  /* [DIRAC_STD] 10.3 Source Parameters
321  * Override the defaults. */
322  if (ret = parse_source_parameters(avctx, gb, source))
323  return ret;
324 
325  ret = ff_set_dimensions(avctx, source->width, source->height);
326  if (ret < 0)
327  return ret;
328 
329  ff_set_sar(avctx, avctx->sample_aspect_ratio);
330 
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;
338  }
339  return 0;
340 }