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  avctx->framerate = frame_rate;
178 
179  /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
180  * pixel_aspect_ratio(video_params) */
181  if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
182  /* [DIRAC_STD] index */
184 
185  if (source->aspect_ratio_index > 6U)
186  return AVERROR_INVALIDDATA;
187 
188  if (!source->aspect_ratio_index) {
191  }
192  }
193  /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
194  * aspect ratio values */
195  if (source->aspect_ratio_index > 0)
196  avctx->sample_aspect_ratio =
197  dirac_preset_aspect_ratios[source->aspect_ratio_index - 1];
198 
199  /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
200  if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
201  /* [DIRAC_STD] CLEAN_WIDTH */
202  source->clean_width = svq3_get_ue_golomb(gb);
203  /* [DIRAC_STD] CLEAN_HEIGHT */
204  source->clean_height = svq3_get_ue_golomb(gb);
205  /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
207  /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
209  }
210 
211  /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
212  * WARNING: Some adaptation seems to be done using the
213  * AVCOL_RANGE_MPEG/JPEG values */
214  if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
215  /* [DIRAC_STD] index */
217 
218  if (source->pixel_range_index > 4U)
219  return AVERROR_INVALIDDATA;
220 
221  /* This assumes either fullrange or MPEG levels only */
222  if (!source->pixel_range_index) {
223  luma_offset = svq3_get_ue_golomb(gb);
224  luma_depth = av_log2(svq3_get_ue_golomb(gb)) + 1;
225  svq3_get_ue_golomb(gb); /* chroma offset */
226  svq3_get_ue_golomb(gb); /* chroma excursion */
227  avctx->color_range = luma_offset ? AVCOL_RANGE_MPEG
229  }
230  }
231  /* [DIRAC_STD] Table 10.5
232  * Available signal range presets <--> pixel_range_presets */
233  if (source->pixel_range_index > 0) {
234  idx = source->pixel_range_index - 1;
235  luma_depth = pixel_range_presets[idx].bitdepth;
236  avctx->color_range = pixel_range_presets[idx].color_range;
237  }
238 
239  if (luma_depth > 8)
240  av_log(avctx, AV_LOG_WARNING, "Bitdepth greater than 8\n");
241 
242  avctx->pix_fmt = dirac_pix_fmt[!luma_offset][source->chroma_format];
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;
247  }
248 
249 
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);
254 
255  if (source->color_spec_index > 4U)
256  return AVERROR_INVALIDDATA;
257 
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;
261 
262  if (!source->color_spec_index) {
263  /* [DIRAC_STD] 10.3.9.1 Colour primaries */
264  if (get_bits1(gb)) {
265  idx = svq3_get_ue_golomb(gb);
266  if (idx < 3U)
267  avctx->color_primaries = dirac_primaries[idx];
268  }
269  /* [DIRAC_STD] 10.3.9.2 Colour matrix */
270  if (get_bits1(gb)) {
271  idx = svq3_get_ue_golomb(gb);
272  if (!idx)
273  avctx->colorspace = AVCOL_SPC_BT709;
274  else if (idx == 1)
275  avctx->colorspace = AVCOL_SPC_BT470BG;
276  }
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;
280  }
281  } else {
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;
286  }
287 
288  return 0;
289 }
290 
291 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
293  dirac_source_params *source)
294 {
295  unsigned version_major;
296  unsigned video_format, picture_coding_mode;
297  int ret;
298 
299  /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
300  version_major = svq3_get_ue_golomb(gb);
301  svq3_get_ue_golomb(gb); /* version_minor */
302  avctx->profile = svq3_get_ue_golomb(gb);
303  avctx->level = svq3_get_ue_golomb(gb);
304  /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
305  * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
306  video_format = svq3_get_ue_golomb(gb);
307 
308  if (version_major < 2)
309  av_log(avctx, AV_LOG_WARNING, "Stream is old and may not work\n");
310  else if (version_major > 2)
311  av_log(avctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
312 
313  if (video_format > 20U)
314  return AVERROR_INVALIDDATA;
315 
316  /* Fill in defaults for the source parameters. */
317  *source = dirac_source_parameters_defaults[video_format];
318 
319  /* [DIRAC_STD] 10.3 Source Parameters
320  * Override the defaults. */
321  if (ret = parse_source_parameters(avctx, gb, source))
322  return ret;
323 
324  ret = ff_set_dimensions(avctx, source->width, source->height);
325  if (ret < 0)
326  return ret;
327 
328  ff_set_sar(avctx, avctx->sample_aspect_ratio);
329 
330  /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
331  * currently only used to signal field coding */
332  picture_coding_mode = svq3_get_ue_golomb(gb);
333  if (picture_coding_mode != 0) {
334  av_log(avctx, AV_LOG_ERROR, "Unsupported picture coding mode %d\n",
335  picture_coding_mode);
336  return AVERROR_INVALIDDATA;
337  }
338  return 0;
339 }