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 }
also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
Definition: pixfmt.h:502
AVRational framerate
Definition: avcodec.h:3023
unsigned height
Definition: dirac.h:39
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
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
uint16_t clean_right_offset
Definition: dirac.h:51
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:229
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601 ...
Definition: pixfmt.h:506
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1960
int num
numerator
Definition: rational.h:44
uint8_t frame_rate_index
index into dirac_frame_rate[]
Definition: dirac.h:45
static unsigned svq3_get_ue_golomb(GetBitContext *gb)
Definition: golomb.h:115
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1623
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1444
int profile
profile
Definition: avcodec.h:2835
AVColorTransferCharacteristic
Color Transfer Characteristic.
Definition: pixfmt.h:477
enum AVColorTransferCharacteristic color_trc
Definition: dirac.c:103
static const dirac_source_params dirac_source_parameters_defaults[]
Definition: dirac.c:38
uint8_t
Interface to Dirac Decoder/Encoder.
AVColorSpace
YUV colorspace type.
Definition: pixfmt.h:500
uint8_t color_spec_index
index into dirac_color_spec_presets[]
Definition: dirac.h:54
enum AVColorRange color_range
Definition: dirac.c:86
uint16_t clean_width
Definition: dirac.h:48
planar YUV 4:2:2, 16bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV422P and setting col...
Definition: pixfmt.h:76
int ff_set_sar(AVCodecContext *avctx, AVRational sar)
Check that the provided sample aspect ratio is valid and set it on the codec context.
Definition: utils.c:244
AVColorRange
MPEG vs JPEG YUV range.
Definition: pixfmt.h:520
static enum AVPixelFormat dirac_pix_fmt[2][3]
Definition: dirac.c:113
AVColorPrimaries
Chromaticity coordinates of the source primaries.
Definition: pixfmt.h:459
#define av_log(a,...)
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
enum AVColorPrimaries color_primaries
Definition: dirac.c:101
also ITU-R BT1361 / IEC 61966-2-4 / SMPTE RP177 Annex B
Definition: pixfmt.h:461
uint16_t clean_height
Definition: dirac.h:49
Libavcodec external API header.
uint8_t chroma_format
0: 444 1: 422 2: 420
Definition: dirac.h:40
int avpriv_dirac_parse_sequence_header(AVCodecContext *avctx, GetBitContext *gb, dirac_source_params *source)
Definition: dirac.c:292
uint8_t aspect_ratio_index
index into dirac_aspect_ratio[]
Definition: dirac.h:46
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
Definition: pixfmt.h:67
planar YUV 4:2:0, 12bpp, full scale (JPEG), deprecated in favor of AV_PIX_FMT_YUV420P and setting col...
Definition: pixfmt.h:75
uint8_t interlaced
Definition: dirac.h:42
ret
Definition: avfilter.c:974
also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM
Definition: pixfmt.h:466
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1939
int level
level
Definition: avcodec.h:2925
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:43
the normal 2^n-1 "JPEG" YUV ranges
Definition: pixfmt.h:523
uint8_t pixel_range_index
index into dirac_pixel_range_presets[]
Definition: dirac.h:53
also ITU-R BT1361
Definition: pixfmt.h:479
static const AVRational dirac_frame_rate[]
Definition: dirac.c:77
static const struct @39 pixel_range_presets[]
main external API structure.
Definition: avcodec.h:1241
MPEG1/2 tables.
static enum AVColorPrimaries dirac_primaries[]
Definition: dirac.c:94
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:304
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1953
rational number numerator/denominator
Definition: rational.h:43
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1946
static const AVRational dirac_preset_aspect_ratios[]
Definition: dirac.c:65
static int parse_source_parameters(AVCodecContext *avctx, GetBitContext *gb, dirac_source_params *source)
Definition: dirac.c:120
the normal 219*2^(n-8) "MPEG" YUV ranges
Definition: pixfmt.h:522
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
Definition: pixfmt.h:63
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:77
int den
denominator
Definition: rational.h:45
uint16_t clean_left_offset
Definition: dirac.h:50
#define av_log2
Definition: intmath.h:105
static const struct @40 dirac_color_presets[]
also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC
Definition: pixfmt.h:467
enum AVColorSpace colorspace
Definition: dirac.c:102
uint8_t bitdepth
Definition: dirac.c:85
exp golomb vlc stuff
AVPixelFormat
Pixel format.
Definition: pixfmt.h:61
const AVRational ff_mpeg12_frame_rate_tab[16]
Definition: mpeg12data.c:308
unsigned width
Definition: dirac.h:38