Libav
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  *
5  * This file is part of Libav.
6  *
7  * Libav is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * Libav is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
28 #include "libavutil/imgutils.h"
29 
30 #include "avcodec.h"
31 #include "dirac.h"
32 #include "golomb.h"
33 #include "internal.h"
34 #include "mpeg12data.h"
35 
36 #if CONFIG_DIRAC_PARSE
37 
38 typedef struct dirac_source_params {
39  unsigned width;
40  unsigned height;
41  uint8_t chroma_format;
42 
43  uint8_t interlaced;
44  uint8_t top_field_first;
45 
46  uint8_t frame_rate_index;
47  uint8_t aspect_ratio_index;
48 
49  uint16_t clean_width;
50  uint16_t clean_height;
51  uint16_t clean_left_offset;
52  uint16_t clean_right_offset;
53 
54  uint8_t pixel_range_index;
55  uint8_t color_spec_index;
56 } dirac_source_params;
57 
58 // defaults for source parameters
59 static const dirac_source_params dirac_source_parameters_defaults[] = {
60  { 640, 480, 2, 0, 0, 1, 1, 640, 480, 0, 0, 1, 0 },
61  { 176, 120, 2, 0, 0, 9, 2, 176, 120, 0, 0, 1, 1 },
62  { 176, 144, 2, 0, 1, 10, 3, 176, 144, 0, 0, 1, 2 },
63  { 352, 240, 2, 0, 0, 9, 2, 352, 240, 0, 0, 1, 1 },
64  { 352, 288, 2, 0, 1, 10, 3, 352, 288, 0, 0, 1, 2 },
65  { 704, 480, 2, 0, 0, 9, 2, 704, 480, 0, 0, 1, 1 },
66  { 704, 576, 2, 0, 1, 10, 3, 704, 576, 0, 0, 1, 2 },
67  { 720, 480, 1, 1, 0, 4, 2, 704, 480, 8, 0, 3, 1 },
68  { 720, 576, 1, 1, 1, 3, 3, 704, 576, 8, 0, 3, 2 },
69 
70  { 1280, 720, 1, 0, 1, 7, 1, 1280, 720, 0, 0, 3, 3 },
71  { 1280, 720, 1, 0, 1, 6, 1, 1280, 720, 0, 0, 3, 3 },
72  { 1920, 1080, 1, 1, 1, 4, 1, 1920, 1080, 0, 0, 3, 3 },
73  { 1920, 1080, 1, 1, 1, 3, 1, 1920, 1080, 0, 0, 3, 3 },
74  { 1920, 1080, 1, 0, 1, 7, 1, 1920, 1080, 0, 0, 3, 3 },
75  { 1920, 1080, 1, 0, 1, 6, 1, 1920, 1080, 0, 0, 3, 3 },
76  { 2048, 1080, 0, 0, 1, 2, 1, 2048, 1080, 0, 0, 4, 4 },
77  { 4096, 2160, 0, 0, 1, 2, 1, 4096, 2160, 0, 0, 4, 4 },
78 
79  { 3840, 2160, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
80  { 3840, 2160, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
81  { 7680, 4320, 1, 0, 1, 7, 1, 3840, 2160, 0, 0, 3, 3 },
82  { 7680, 4320, 1, 0, 1, 6, 1, 3840, 2160, 0, 0, 3, 3 },
83 };
84 
85 /* [DIRAC_STD] Table 10.4 - Available preset pixel aspect ratio values */
86 static const AVRational dirac_preset_aspect_ratios[] = {
87  { 1, 1 },
88  { 10, 11 },
89  { 12, 11 },
90  { 40, 33 },
91  { 16, 11 },
92  { 4, 3 },
93 };
94 
95 /* [DIRAC_STD] Values 9,10 of 10.3.5 Frame Rate.
96  * Table 10.3 Available preset frame rate values
97  */
98 static const AVRational dirac_frame_rate[] = {
99  { 15000, 1001 },
100  { 25, 2 },
101 };
102 
103 /* [DIRAC_STD] This should be equivalent to Table 10.5 Available signal
104  * range presets */
105 static const struct {
106  uint8_t bitdepth;
107  enum AVColorRange color_range;
108 } pixel_range_presets[] = {
109  { 8, AVCOL_RANGE_JPEG },
110  { 8, AVCOL_RANGE_MPEG },
111  { 10, AVCOL_RANGE_MPEG },
112  { 12, AVCOL_RANGE_MPEG },
113 };
114 
115 static const enum AVColorPrimaries dirac_primaries[] = {
119 };
120 
121 static const struct {
122  enum AVColorPrimaries color_primaries;
123  enum AVColorSpace colorspace;
124  enum AVColorTransferCharacteristic color_trc;
125 } dirac_color_presets[] = {
127  { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
128  { AVCOL_PRI_BT470BG, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
129  { AVCOL_PRI_BT709, AVCOL_SPC_BT709, AVCOL_TRC_BT709 },
131 };
132 
133 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats + luma Offset */
134 static const enum AVPixelFormat dirac_pix_fmt[2][3] = {
137 };
138 
139 /* [DIRAC_STD] 10.3 Parse Source Parameters.
140  * source_parameters(base_video_format) */
141 static int parse_source_parameters(AVDiracSeqHeader *dsh, GetBitContext *gb,
142  void *log_ctx)
143 {
144  AVRational frame_rate = { 0, 0 };
145  unsigned luma_depth = 8, luma_offset = 16;
146  int idx;
147 
148  /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
149  /* [DIRAC_STD] custom_dimensions_flag */
150  if (get_bits1(gb)) {
151  dsh->width = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH */
152  dsh->height = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
153  }
154 
155  /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
156  * chroma_sampling_format(video_params) */
157  /* [DIRAC_STD] custom_chroma_format_flag */
158  if (get_bits1(gb))
159  /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
161  if (dsh->chroma_format > 2) {
162  if (log_ctx)
163  av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
164  dsh->chroma_format);
165  return AVERROR_INVALIDDATA;
166  }
167 
168  /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
169  /* [DIRAC_STD] custom_scan_format_flag */
170  if (get_bits1(gb))
171  /* [DIRAC_STD] SOURCE_SAMPLING */
173  if (dsh->interlaced > 1)
174  return AVERROR_INVALIDDATA;
175 
176  /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
177  if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
179 
180  if (dsh->frame_rate_index > 10)
181  return AVERROR_INVALIDDATA;
182 
183  if (!dsh->frame_rate_index) {
184  /* [DIRAC_STD] FRAME_RATE_NUMER */
185  frame_rate.num = get_interleaved_ue_golomb(gb);
186  /* [DIRAC_STD] FRAME_RATE_DENOM */
187  frame_rate.den = get_interleaved_ue_golomb(gb);
188  }
189  }
190  /* [DIRAC_STD] preset_frame_rate(video_params, index) */
191  if (dsh->frame_rate_index > 0) {
192  if (dsh->frame_rate_index <= 8)
193  frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
194  else
195  /* [DIRAC_STD] Table 10.3 values 9-10 */
196  frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
197  }
198  dsh->framerate = frame_rate;
199 
200  /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
201  * pixel_aspect_ratio(video_params) */
202  if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
203  /* [DIRAC_STD] index */
205 
206  if (dsh->aspect_ratio_index > 6)
207  return AVERROR_INVALIDDATA;
208 
209  if (!dsh->aspect_ratio_index) {
212  }
213  }
214  /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
215  * aspect ratio values */
216  if (dsh->aspect_ratio_index > 0)
217  dsh->sample_aspect_ratio =
218  dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
219 
220  /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
221  if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
222  /* [DIRAC_STD] CLEAN_WIDTH */
224  /* [DIRAC_STD] CLEAN_HEIGHT */
226  /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
228  /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
230  }
231 
232  /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
233  * WARNING: Some adaptation seems to be done using the
234  * AVCOL_RANGE_MPEG/JPEG values */
235  if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
236  /* [DIRAC_STD] index */
238 
239  if (dsh->pixel_range_index > 4)
240  return AVERROR_INVALIDDATA;
241 
242  // This assumes either fullrange or MPEG levels only
243  if (!dsh->pixel_range_index) {
244  luma_offset = get_interleaved_ue_golomb(gb);
245  luma_depth = av_log2(get_interleaved_ue_golomb(gb)) + 1;
246  get_interleaved_ue_golomb(gb); /* chroma offset */
247  get_interleaved_ue_golomb(gb); /* chroma excursion */
248  dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
250  }
251  }
252  /* [DIRAC_STD] Table 10.5
253  * Available signal range presets <--> pixel_range_presets */
254  if (dsh->pixel_range_index > 0) {
255  idx = dsh->pixel_range_index - 1;
256  luma_depth = pixel_range_presets[idx].bitdepth;
257  dsh->color_range = pixel_range_presets[idx].color_range;
258  }
259 
260  if (luma_depth > 8 && log_ctx)
261  av_log(log_ctx, AV_LOG_WARNING, "Bitdepth greater than 8");
262 
263  dsh->pix_fmt = dirac_pix_fmt[!luma_offset][dsh->chroma_format];
264 
265  /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
266  if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
267  /* [DIRAC_STD] index */
269 
270  if (dsh->color_spec_index > 4)
271  return AVERROR_INVALIDDATA;
272 
273  dsh->color_primaries = dirac_color_presets[idx].color_primaries;
274  dsh->colorspace = dirac_color_presets[idx].colorspace;
275  dsh->color_trc = dirac_color_presets[idx].color_trc;
276 
277  if (!dsh->color_spec_index) {
278  /* [DIRAC_STD] 10.3.9.1 Colour primaries */
279  if (get_bits1(gb)) {
280  idx = get_interleaved_ue_golomb(gb);
281  if (idx < 3)
282  dsh->color_primaries = dirac_primaries[idx];
283  }
284  /* [DIRAC_STD] 10.3.9.2 Colour matrix */
285  if (get_bits1(gb)) {
286  idx = get_interleaved_ue_golomb(gb);
287  if (!idx)
289  else if (idx == 1)
291  }
292  /* [DIRAC_STD] 10.3.9.3 Transfer function */
293  if (get_bits1(gb) && !get_interleaved_ue_golomb(gb))
294  dsh->color_trc = AVCOL_TRC_BT709;
295  }
296  } else {
297  idx = dsh->color_spec_index;
298  dsh->color_primaries = dirac_color_presets[idx].color_primaries;
299  dsh->colorspace = dirac_color_presets[idx].colorspace;
300  dsh->color_trc = dirac_color_presets[idx].color_trc;
301  }
302 
303  return 0;
304 }
305 
306 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
308  const uint8_t *buf, size_t buf_size,
309  void *log_ctx)
310 {
311  AVDiracSeqHeader *dsh;
312  GetBitContext gb;
313  unsigned version_major;
314  unsigned video_format, picture_coding_mode;
315  int ret;
316 
317  dsh = av_mallocz(sizeof(*dsh));
318  if (!dsh)
319  return AVERROR(ENOMEM);
320 
321  ret = init_get_bits8(&gb, buf, buf_size);
322  if (ret < 0)
323  goto fail;
324 
325  /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
326  version_major = get_interleaved_ue_golomb(&gb);
327  get_interleaved_ue_golomb(&gb); /* version_minor */
329  dsh->level = get_interleaved_ue_golomb(&gb);
330  /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
331  * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
332  video_format = get_interleaved_ue_golomb(&gb);
333 
334  if (log_ctx) {
335  if (version_major < 2)
336  av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
337  else if (version_major > 2)
338  av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
339  }
340 
341  if (video_format > 20) {
342  ret = AVERROR_INVALIDDATA;
343  goto fail;
344  }
345 
346  // Fill in defaults for the source parameters.
347  dsh->width = dirac_source_parameters_defaults[video_format].width;
348  dsh->height = dirac_source_parameters_defaults[video_format].height;
349  dsh->chroma_format = dirac_source_parameters_defaults[video_format].chroma_format;
350  dsh->interlaced = dirac_source_parameters_defaults[video_format].interlaced;
351  dsh->top_field_first = dirac_source_parameters_defaults[video_format].top_field_first;
352  dsh->frame_rate_index = dirac_source_parameters_defaults[video_format].frame_rate_index;
353  dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
354  dsh->clean_width = dirac_source_parameters_defaults[video_format].clean_width;
355  dsh->clean_height = dirac_source_parameters_defaults[video_format].clean_height;
356  dsh->clean_left_offset = dirac_source_parameters_defaults[video_format].clean_left_offset;
357  dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
358  dsh->pixel_range_index = dirac_source_parameters_defaults[video_format].pixel_range_index;
359  dsh->color_spec_index = dirac_source_parameters_defaults[video_format].color_spec_index;
360 
361  /* [DIRAC_STD] 10.3 Source Parameters
362  * Override the defaults. */
363  ret = parse_source_parameters(dsh, &gb, log_ctx);
364  if (ret < 0)
365  goto fail;
366 
367  /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
368  * currently only used to signal field coding */
369  picture_coding_mode = get_interleaved_ue_golomb(&gb);
370  if (picture_coding_mode != 0) {
371  if (log_ctx) {
372  av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
373  picture_coding_mode);
374  }
375  ret = AVERROR_INVALIDDATA;
376  goto fail;
377  }
378 
379  *pdsh = dsh;
380  return 0;
381 fail:
382  av_freep(&dsh);
383  *pdsh = NULL;
384  return ret;
385 }
386 #else
388  const uint8_t *buf, size_t buf_size,
389  void *log_ctx)
390 {
391  return AVERROR(ENOSYS);
392 }
393 #endif
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:345
uint8_t interlaced
Definition: dirac.h:38
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:54
enum AVColorRange color_range
Definition: dirac.h:59
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
Definition: pixfmt.h:64
misc image utilities
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
enum AVColorTransferCharacteristic color_trc
Definition: dirac.h:61
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:349
int num
numerator
Definition: rational.h:44
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:315
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:387
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
uint8_t top_field_first
Definition: dirac.h:39
uint8_t
Interface to Dirac Decoder/Encoder.
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:343
AVRational sample_aspect_ratio
Definition: dirac.h:56
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:72
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:362
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:293
uint8_t pixel_range_index
index into dirac_pixel_range_presets[]
Definition: dirac.h:49
AVRational framerate
Definition: dirac.h:55
uint8_t chroma_format
0: 444 1: 422 2: 420
Definition: dirac.h:36
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
#define AVERROR(e)
Definition: error.h:43
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:295
uint16_t clean_height
Definition: dirac.h:45
#define fail()
Definition: checkasm.h:80
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:63
enum AVColorSpace colorspace
Definition: dirac.h:62
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:71
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:300
uint16_t clean_width
Definition: dirac.h:44
uint8_t color_spec_index
index into dirac_color_spec_presets[]
Definition: dirac.h:50
uint8_t frame_rate_index
index into dirac_frame_rate[]
Definition: dirac.h:41
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:365
also ITU-R BT1361
Definition: pixfmt.h:317
NULL
Definition: eval.c:55
static int width
Definition: utils.c:156
Libavcodec external API header.
enum AVPixelFormat pix_fmt
Definition: dirac.h:58
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:395
uint16_t clean_right_offset
Definition: dirac.h:47
MPEG-1/2 tables.
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:267
rational number numerator/denominator
Definition: rational.h:43
uint16_t clean_left_offset
Definition: dirac.h:46
unsigned height
Definition: dirac.h:35
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:364
int height
Definition: gxfenc.c:72
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:59
common internal api header.
planar YUV 4:4:4, 24bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV444P and setting col...
Definition: pixfmt.h:73
int den
denominator
Definition: rational.h:45
#define av_log2
Definition: intmath.h:85
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:301
uint8_t aspect_ratio_index
index into dirac_aspect_ratio[]
Definition: dirac.h:42
enum AVColorPrimaries color_primaries
Definition: dirac.h:60
unsigned width
Definition: dirac.h:34
static unsigned get_interleaved_ue_golomb(GetBitContext *gb)
Definition: golomb.h:111
exp golomb vlc stuff
AVPixelFormat
Pixel format.
Definition: pixfmt.h:57
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:211
const AVRational ff_mpeg12_frame_rate_tab[]