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 #if CONFIG_DIRAC_PARSE
38 
39 typedef struct dirac_source_params {
40  unsigned width;
41  unsigned height;
42  uint8_t chroma_format; ///< 0: 444 1: 422 2: 420
43 
45  uint8_t top_field_first;
46 
47  uint8_t frame_rate_index; ///< index into dirac_frame_rate[]
48  uint8_t aspect_ratio_index; ///< index into dirac_aspect_ratio[]
49 
50  uint16_t clean_width;
51  uint16_t clean_height;
52  uint16_t clean_left_offset;
53  uint16_t clean_right_offset;
54 
55  uint8_t pixel_range_index; ///< index into dirac_pixel_range_presets[]
56  uint8_t color_spec_index; ///< index into dirac_color_spec_presets[]
57 } dirac_source_params;
58 
59 /* defaults for source parameters */
60 static const dirac_source_params dirac_source_parameters_defaults[] = {
61  { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
62  { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
63  { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
64  { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
65  { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
66  { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
67  { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
68  { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
69  { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
70 
71  { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
72  { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
73  { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
74  { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
75  { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
76  { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
77  { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
78  { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
79 
80  { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
81  { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
82  { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
83  { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
84 };
85 
86 /* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
87 static const AVRational dirac_preset_aspect_ratios[] = {
88  { 1, 1 },
89  { 10, 11 },
90  { 12, 11 },
91  { 40, 33 },
92  { 16, 11 },
93  { 4, 3 },
94 };
95 
96 /* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
97  * Table 10.3 Available preset frame rate values
98  */
99 static const AVRational dirac_frame_rate[] = {
100  { 15000, 1001 },
101  { 25, 2 },
102 };
103 
104 /* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
105  * range presets */
106 static const struct {
107  uint8_t bitdepth;
109 } pixel_range_presets[] = {
110  { 8, AVCOL_RANGE_JPEG },
111  { 8, AVCOL_RANGE_MPEG },
112  { 10, AVCOL_RANGE_MPEG },
113  { 12, AVCOL_RANGE_MPEG },
114 };
115 
116 static const enum AVColorPrimaries dirac_primaries[] = {
120 };
121 
122 static const struct {
123  enum AVColorPrimaries color_primaries;
124  enum AVColorSpace colorspace;
125  enum AVColorTransferCharacteristic color_trc;
126 } dirac_color_presets[] = {
132 };
133 
134 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats */
135 static const enum AVPixelFormat dirac_pix_fmt[][3] = {
139 };
140 
141 /* [DIRAC_STD] 10.3 Parse Source Parameters.
142  * source_parameters(base_video_format) */
143 static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb,
144  void *log_ctx)
145 {
146  AVRational frame_rate = { 0, 0 };
147  unsigned luma_depth = 8, luma_offset = 16;
148  int idx;
149  int chroma_x_shift, chroma_y_shift;
150 
151  /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
152  /* [DIRAC_STD] custom_dimensions_flag */
153  if (get_bits1(gb)) {
154  dsh->width = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
155  dsh->height = svq3_get_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
156  }
157 
158  /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
159  * chroma_sampling_format(video_params) */
160  /* [DIRAC_STD] custom_chroma_format_flag */
161  if (get_bits1(gb))
162  /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
164  if (dsh->chroma_format > 2U) {
165  if (log_ctx)
166  av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
167  dsh->chroma_format);
168  return AVERROR_INVALIDDATA;
169  }
170 
171  /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
172  /* [DIRAC_STD] custom_scan_format_flag */
173  if (get_bits1(gb))
174  /* [DIRAC_STD] SOURCE_SAMPLING */
175  dsh->interlaced = svq3_get_ue_golomb(gb);
176  if (dsh->interlaced > 1U)
177  return AVERROR_INVALIDDATA;
178 
179  /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
180  if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
182 
183  if (dsh->frame_rate_index > 10U)
184  return AVERROR_INVALIDDATA;
185 
186  if (!dsh->frame_rate_index) {
187  /* [DIRAC_STD] FRAME_RATE_NUMER */
188  frame_rate.num = svq3_get_ue_golomb(gb);
189  /* [DIRAC_STD] FRAME_RATE_DENOM */
190  frame_rate.den = svq3_get_ue_golomb(gb);
191  }
192  }
193  /* [DIRAC_STD] preset_frame_rate(video_params, index) */
194  if (dsh->frame_rate_index > 0) {
195  if (dsh->frame_rate_index <= 8)
196  frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
197  else
198  /* [DIRAC_STD] Table 10.3 values 9-10 */
199  frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
200  }
201  dsh->framerate = frame_rate;
202 
203  /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
204  * pixel_aspect_ratio(video_params) */
205  if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
206  /* [DIRAC_STD] index */
208 
209  if (dsh->aspect_ratio_index > 6U)
210  return AVERROR_INVALIDDATA;
211 
212  if (!dsh->aspect_ratio_index) {
215  }
216  }
217  /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
218  * aspect ratio values */
219  if (dsh->aspect_ratio_index > 0)
220  dsh->sample_aspect_ratio =
221  dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
222 
223  /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
224  if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
225  /* [DIRAC_STD] CLEAN_WIDTH */
226  dsh->clean_width = svq3_get_ue_golomb(gb);
227  /* [DIRAC_STD] CLEAN_HEIGHT */
228  dsh->clean_height = svq3_get_ue_golomb(gb);
229  /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
231  /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
233  }
234 
235  /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
236  * WARNING: Some adaptation seems to be done using the
237  * AVCOL_RANGE_MPEG/JPEG values */
238  if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
239  /* [DIRAC_STD] index */
241 
242  if (dsh->pixel_range_index > 4U)
243  return AVERROR_INVALIDDATA;
244 
245  /* This assumes either fullrange or MPEG levels only */
246  if (!dsh->pixel_range_index) {
247  luma_offset = svq3_get_ue_golomb(gb);
248  luma_depth = av_log2(svq3_get_ue_golomb(gb)) + 1;
249  svq3_get_ue_golomb(gb); /* chroma offset */
250  svq3_get_ue_golomb(gb); /* chroma excursion */
251  dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
253  }
254  }
255  /* [DIRAC_STD] Table 10.5
256  * Available signal range presets <--> pixel_range_presets */
257  if (dsh->pixel_range_index > 0) {
258  idx = dsh->pixel_range_index - 1;
259  luma_depth = pixel_range_presets[idx].bitdepth;
260  dsh->color_range = pixel_range_presets[idx].color_range;
261  }
262 
263  dsh->bit_depth = luma_depth;
264 
265  /* Full range 8 bts uses the same pix_fmts as limited range 8 bits */
266  dsh->pixel_range_index += dsh->pixel_range_index == 1;
267 
268  if (dsh->pixel_range_index < 2U)
269  return AVERROR_INVALIDDATA;
270 
271  dsh->pix_fmt = dirac_pix_fmt[dsh->chroma_format][dsh->pixel_range_index-2];
272  avcodec_get_chroma_sub_sample(dsh->pix_fmt, &chroma_x_shift, &chroma_y_shift);
273  if ((dsh->width % (1<<chroma_x_shift)) || (dsh->height % (1<<chroma_y_shift))) {
274  if (log_ctx)
275  av_log(log_ctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
276  return AVERROR_INVALIDDATA;
277  }
278 
279  /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
280  if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
281  /* [DIRAC_STD] index */
282  idx = dsh->color_spec_index = svq3_get_ue_golomb(gb);
283 
284  if (dsh->color_spec_index > 4U)
285  return AVERROR_INVALIDDATA;
286 
287  dsh->color_primaries = dirac_color_presets[idx].color_primaries;
288  dsh->colorspace = dirac_color_presets[idx].colorspace;
289  dsh->color_trc = dirac_color_presets[idx].color_trc;
290 
291  if (!dsh->color_spec_index) {
292  /* [DIRAC_STD] 10.3.9.1 Colour primaries */
293  if (get_bits1(gb)) {
294  idx = svq3_get_ue_golomb(gb);
295  if (idx < 3U)
296  dsh->color_primaries = dirac_primaries[idx];
297  }
298  /* [DIRAC_STD] 10.3.9.2 Colour matrix */
299  if (get_bits1(gb)) {
300  idx = svq3_get_ue_golomb(gb);
301  if (!idx)
303  else if (idx == 1)
305  }
306  /* [DIRAC_STD] 10.3.9.3 Transfer function */
307  if (get_bits1(gb) && !svq3_get_ue_golomb(gb))
308  dsh->color_trc = AVCOL_TRC_BT709;
309  }
310  } else {
311  idx = dsh->color_spec_index;
312  dsh->color_primaries = dirac_color_presets[idx].color_primaries;
313  dsh->colorspace = dirac_color_presets[idx].colorspace;
314  dsh->color_trc = dirac_color_presets[idx].color_trc;
315  }
316 
317  return 0;
318 }
319 
320 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
322  const uint8_t *buf, size_t buf_size,
323  void *log_ctx)
324 {
325  AVDiracSeqHeader *dsh;
326  GetBitContext gb;
327  unsigned video_format, picture_coding_mode;
328  int ret;
329 
330  dsh = av_mallocz(sizeof(*dsh));
331  if (!dsh)
332  return AVERROR(ENOMEM);
333 
334  ret = init_get_bits8(&gb, buf, buf_size);
335  if (ret < 0)
336  goto fail;
337 
338  /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
339  dsh->version.major = svq3_get_ue_golomb(&gb);
340  dsh->version.minor = svq3_get_ue_golomb(&gb);
341  dsh->profile = svq3_get_ue_golomb(&gb);
342  dsh->level = svq3_get_ue_golomb(&gb);
343  /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
344  * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
345  video_format = svq3_get_ue_golomb(&gb);
346 
347  if (dsh->version.major < 2 && log_ctx)
348  av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
349  else if (dsh->version.major > 2 && log_ctx)
350  av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
351 
352  if (video_format > 20U) {
353  ret = AVERROR_INVALIDDATA;
354  goto fail;
355  }
356 
357  /* Fill in defaults for the source parameters. */
358  dsh->width = dirac_source_parameters_defaults[video_format].width;
359  dsh->height = dirac_source_parameters_defaults[video_format].height;
360  dsh->chroma_format = dirac_source_parameters_defaults[video_format].chroma_format;
361  dsh->interlaced = dirac_source_parameters_defaults[video_format].interlaced;
362  dsh->top_field_first = dirac_source_parameters_defaults[video_format].top_field_first;
363  dsh->frame_rate_index = dirac_source_parameters_defaults[video_format].frame_rate_index;
364  dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
365  dsh->clean_width = dirac_source_parameters_defaults[video_format].clean_width;
366  dsh->clean_height = dirac_source_parameters_defaults[video_format].clean_height;
367  dsh->clean_left_offset = dirac_source_parameters_defaults[video_format].clean_left_offset;
368  dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
369  dsh->pixel_range_index = dirac_source_parameters_defaults[video_format].pixel_range_index;
370  dsh->color_spec_index = dirac_source_parameters_defaults[video_format].color_spec_index;
371 
372  /* [DIRAC_STD] 10.3 Source Parameters
373  * Override the defaults. */
374  ret = parse_source_parameters(dsh, &gb, log_ctx);
375  if (ret < 0)
376  goto fail;
377 
378  /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
379  * currently only used to signal field coding */
380  picture_coding_mode = svq3_get_ue_golomb(&gb);
381  if (picture_coding_mode != 0) {
382  if (log_ctx) {
383  av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
384  picture_coding_mode);
385  }
386  ret = AVERROR_INVALIDDATA;
387  goto fail;
388  }
389 
390  *pdsh = dsh;
391  return 0;
392 fail:
393  av_freep(&dsh);
394  *pdsh = NULL;
395  return ret;
396 }
397 #else
399  const uint8_t *buf, size_t buf_size,
400  void *log_ctx)
401 {
402  return AVERROR(ENOSYS);
403 }
404 #endif
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:422
uint8_t interlaced
Definition: dirac.h:86
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
enum AVColorRange color_range
Definition: dirac.h:107
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:68
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
enum AVColorTransferCharacteristic color_trc
Definition: dirac.h:109
DiracVersionInfo version
Definition: dirac.h:112
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:426
int num
numerator
Definition: rational.h:44
int av_log2(unsigned v)
Definition: intmath.c:26
static unsigned svq3_get_ue_golomb(GetBitContext *gb)
Definition: golomb.h:115
color_range
#define AV_PIX_FMT_YUV420P12
Definition: pixfmt.h:334
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:395
int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh, const uint8_t *buf, size_t buf_size, void *log_ctx)
Parse a Dirac sequence header.
Definition: dirac.c:398
uint8_t top_field_first
Definition: dirac.h:87
uint8_t
Interface to Dirac Decoder/Encoder.
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:420
AVRational sample_aspect_ratio
Definition: dirac.h:104
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:440
#define AV_PIX_FMT_YUV422P12
Definition: pixfmt.h:335
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:376
uint8_t pixel_range_index
index into dirac_pixel_range_presets[]
Definition: dirac.h:97
#define av_log(a,...)
AVRational framerate
Definition: dirac.h:103
#define U(x)
Definition: vp56_arith.h:37
uint8_t chroma_format
0: 444 1: 422 2: 420
Definition: dirac.h:84
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:378
#define AV_PIX_FMT_YUV444P10
Definition: pixfmt.h:333
uint16_t clean_height
Definition: dirac.h:93
#define fail()
Definition: checkasm.h:80
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
enum AVColorSpace colorspace
Definition: dirac.h:110
uint8_t interlaced
Definition: mxfenc.c:1823
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:383
uint16_t clean_width
Definition: dirac.h:92
void avcodec_get_chroma_sub_sample(enum AVPixelFormat pix_fmt, int *h_shift, int *v_shift)
Utility function to access log2_chroma_w log2_chroma_h from the pixel format AVPixFmtDescriptor.
Definition: imgconvert.c:38
uint8_t color_spec_index
index into dirac_color_spec_presets[]
Definition: dirac.h:98
uint8_t frame_rate_index
index into dirac_frame_rate[]
Definition: dirac.h:89
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:443
also ITU-R BT1361
Definition: pixfmt.h:397
Libavcodec external API header.
enum AVPixelFormat pix_fmt
Definition: dirac.h:106
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:449
uint16_t clean_right_offset
Definition: dirac.h:95
MPEG1/2 tables.
void * buf
Definition: avisynth_c.h:553
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:312
BYTE int const BYTE int int int height
Definition: avisynth_c.h:676
#define AV_PIX_FMT_YUV420P10
Definition: pixfmt.h:330
rational number numerator/denominator
Definition: rational.h:43
uint16_t clean_left_offset
Definition: dirac.h:94
unsigned height
Definition: dirac.h:83
#define AV_PIX_FMT_YUV422P10
Definition: pixfmt.h:331
#define AV_PIX_FMT_YUV444P12
Definition: pixfmt.h:337
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:442
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
common internal api header.
int den
denominator
Definition: rational.h:45
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:384
uint8_t aspect_ratio_index
index into dirac_aspect_ratio[]
Definition: dirac.h:90
#define av_freep(p)
enum AVColorPrimaries color_primaries
Definition: dirac.h:108
unsigned width
Definition: dirac.h:82
exp golomb vlc stuff
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
const AVRational ff_mpeg12_frame_rate_tab[16]
Definition: mpeg12data.c:308
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:252
static int width