]> git.sesse.net Git - ffmpeg/blob - libavcodec/dirac.c
avutil/mem: Also poison new av_realloc-allocated blocks
[ffmpeg] / libavcodec / dirac.c
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
44     uint8_t interlaced;
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;
108     enum AVColorRange   color_range;
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[] = {
117     AVCOL_PRI_BT709,
118     AVCOL_PRI_SMPTE170M,
119     AVCOL_PRI_BT470BG,
120 };
121
122 static const struct {
123     enum AVColorPrimaries color_primaries;
124     enum AVColorSpace colorspace;
125     enum AVColorTransferCharacteristic color_trc;
126 } dirac_color_presets[] = {
127     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_BT709 },
128     { AVCOL_PRI_SMPTE170M, AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
129     { AVCOL_PRI_BT470BG,   AVCOL_SPC_BT470BG, AVCOL_TRC_BT709 },
130     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_BT709 },
131     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_UNSPECIFIED /* DCinema */ },
132 };
133
134 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats */
135 static const enum AVPixelFormat dirac_pix_fmt[][3] = {
136     {AV_PIX_FMT_YUV444P, AV_PIX_FMT_YUV444P10, AV_PIX_FMT_YUV444P12},
137     {AV_PIX_FMT_YUV422P, AV_PIX_FMT_YUV422P10, AV_PIX_FMT_YUV422P12},
138     {AV_PIX_FMT_YUV420P, AV_PIX_FMT_YUV420P10, AV_PIX_FMT_YUV420P12},
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     int ret;
151
152     /* [DIRAC_STD] 10.3.2 Frame size. frame_size(video_params) */
153     /* [DIRAC_STD] custom_dimensions_flag */
154     if (get_bits1(gb)) {
155         dsh->width  = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_WIDTH  */
156         dsh->height = get_interleaved_ue_golomb(gb); /* [DIRAC_STD] FRAME_HEIGHT */
157     }
158
159     /* [DIRAC_STD] 10.3.3 Chroma Sampling Format.
160      *  chroma_sampling_format(video_params) */
161     /* [DIRAC_STD] custom_chroma_format_flag */
162     if (get_bits1(gb))
163         /* [DIRAC_STD] CHROMA_FORMAT_INDEX */
164         dsh->chroma_format = get_interleaved_ue_golomb(gb);
165     if (dsh->chroma_format > 2U) {
166         if (log_ctx)
167             av_log(log_ctx, AV_LOG_ERROR, "Unknown chroma format %d\n",
168                    dsh->chroma_format);
169         return AVERROR_INVALIDDATA;
170     }
171
172     /* [DIRAC_STD] 10.3.4 Scan Format. scan_format(video_params) */
173     /* [DIRAC_STD] custom_scan_format_flag */
174     if (get_bits1(gb))
175         /* [DIRAC_STD] SOURCE_SAMPLING */
176         dsh->interlaced = get_interleaved_ue_golomb(gb);
177     if (dsh->interlaced > 1U)
178         return AVERROR_INVALIDDATA;
179
180     /* [DIRAC_STD] 10.3.5 Frame Rate. frame_rate(video_params) */
181     if (get_bits1(gb)) { /* [DIRAC_STD] custom_frame_rate_flag */
182         dsh->frame_rate_index = get_interleaved_ue_golomb(gb);
183
184         if (dsh->frame_rate_index > 10U)
185             return AVERROR_INVALIDDATA;
186
187         if (!dsh->frame_rate_index) {
188             /* [DIRAC_STD] FRAME_RATE_NUMER */
189             frame_rate.num = get_interleaved_ue_golomb(gb);
190             /* [DIRAC_STD] FRAME_RATE_DENOM */
191             frame_rate.den = get_interleaved_ue_golomb(gb);
192         }
193     }
194     /* [DIRAC_STD] preset_frame_rate(video_params, index) */
195     if (dsh->frame_rate_index > 0) {
196         if (dsh->frame_rate_index <= 8)
197             frame_rate = ff_mpeg12_frame_rate_tab[dsh->frame_rate_index];
198         else
199             /* [DIRAC_STD] Table 10.3 values 9-10 */
200             frame_rate = dirac_frame_rate[dsh->frame_rate_index - 9];
201     }
202     dsh->framerate = frame_rate;
203
204     /* [DIRAC_STD] 10.3.6 Pixel Aspect Ratio.
205      * pixel_aspect_ratio(video_params) */
206     if (get_bits1(gb)) { /* [DIRAC_STD] custom_pixel_aspect_ratio_flag */
207         /* [DIRAC_STD] index */
208         dsh->aspect_ratio_index = get_interleaved_ue_golomb(gb);
209
210         if (dsh->aspect_ratio_index > 6U)
211             return AVERROR_INVALIDDATA;
212
213         if (!dsh->aspect_ratio_index) {
214             dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(gb);
215             dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(gb);
216         }
217     }
218     /* [DIRAC_STD] Take value from Table 10.4 Available preset pixel
219      *  aspect ratio values */
220     if (dsh->aspect_ratio_index > 0)
221         dsh->sample_aspect_ratio =
222             dirac_preset_aspect_ratios[dsh->aspect_ratio_index - 1];
223
224     /* [DIRAC_STD] 10.3.7 Clean area. clean_area(video_params) */
225     if (get_bits1(gb)) { /* [DIRAC_STD] custom_clean_area_flag */
226         /* [DIRAC_STD] CLEAN_WIDTH */
227         dsh->clean_width = get_interleaved_ue_golomb(gb);
228         /* [DIRAC_STD] CLEAN_HEIGHT */
229         dsh->clean_height = get_interleaved_ue_golomb(gb);
230         /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
231         dsh->clean_left_offset = get_interleaved_ue_golomb(gb);
232         /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
233         dsh->clean_right_offset = get_interleaved_ue_golomb(gb);
234     }
235
236     /* [DIRAC_STD] 10.3.8 Signal range. signal_range(video_params)
237      * WARNING: Some adaptation seems to be done using the
238      * AVCOL_RANGE_MPEG/JPEG values */
239     if (get_bits1(gb)) { /* [DIRAC_STD] custom_signal_range_flag */
240         /* [DIRAC_STD] index */
241         dsh->pixel_range_index = get_interleaved_ue_golomb(gb);
242
243         if (dsh->pixel_range_index > 4U)
244             return AVERROR_INVALIDDATA;
245
246         /* This assumes either fullrange or MPEG levels only */
247         if (!dsh->pixel_range_index) {
248             luma_offset = get_interleaved_ue_golomb(gb);
249             luma_depth  = av_log2(get_interleaved_ue_golomb(gb)) + 1;
250             get_interleaved_ue_golomb(gb); /* chroma offset    */
251             get_interleaved_ue_golomb(gb); /* chroma excursion */
252             dsh->color_range = luma_offset ? AVCOL_RANGE_MPEG
253                                            : AVCOL_RANGE_JPEG;
254         }
255     }
256     /* [DIRAC_STD] Table 10.5
257      * Available signal range presets <--> pixel_range_presets */
258     if (dsh->pixel_range_index > 0) {
259         idx                = dsh->pixel_range_index - 1;
260         luma_depth         = pixel_range_presets[idx].bitdepth;
261         dsh->color_range   = pixel_range_presets[idx].color_range;
262     }
263
264     dsh->bit_depth = luma_depth;
265
266     /* Full range 8 bts uses the same pix_fmts as limited range 8 bits */
267     dsh->pixel_range_index += dsh->pixel_range_index == 1;
268
269     if (dsh->pixel_range_index < 2U)
270         return AVERROR_INVALIDDATA;
271
272     dsh->pix_fmt = dirac_pix_fmt[dsh->chroma_format][dsh->pixel_range_index-2];
273     ret = av_pix_fmt_get_chroma_sub_sample(dsh->pix_fmt, &chroma_x_shift, &chroma_y_shift);
274     if (ret)
275         return ret;
276
277     if ((dsh->width % (1<<chroma_x_shift)) || (dsh->height % (1<<chroma_y_shift))) {
278         if (log_ctx)
279             av_log(log_ctx, AV_LOG_ERROR, "Dimensions must be an integer multiple of the chroma subsampling\n");
280         return AVERROR_INVALIDDATA;
281     }
282
283     /* [DIRAC_STD] 10.3.9 Colour specification. colour_spec(video_params) */
284     if (get_bits1(gb)) { /* [DIRAC_STD] custom_colour_spec_flag */
285         /* [DIRAC_STD] index */
286         idx = dsh->color_spec_index = get_interleaved_ue_golomb(gb);
287
288         if (dsh->color_spec_index > 4U)
289             return AVERROR_INVALIDDATA;
290
291         dsh->color_primaries = dirac_color_presets[idx].color_primaries;
292         dsh->colorspace      = dirac_color_presets[idx].colorspace;
293         dsh->color_trc       = dirac_color_presets[idx].color_trc;
294
295         if (!dsh->color_spec_index) {
296             /* [DIRAC_STD] 10.3.9.1 Colour primaries */
297             if (get_bits1(gb)) {
298                 idx = get_interleaved_ue_golomb(gb);
299                 if (idx < 3U)
300                     dsh->color_primaries = dirac_primaries[idx];
301             }
302             /* [DIRAC_STD] 10.3.9.2 Colour matrix */
303             if (get_bits1(gb)) {
304                 idx = get_interleaved_ue_golomb(gb);
305                 if (!idx)
306                     dsh->colorspace = AVCOL_SPC_BT709;
307                 else if (idx == 1)
308                     dsh->colorspace = AVCOL_SPC_BT470BG;
309             }
310             /* [DIRAC_STD] 10.3.9.3 Transfer function */
311             if (get_bits1(gb) && !get_interleaved_ue_golomb(gb))
312                 dsh->color_trc = AVCOL_TRC_BT709;
313         }
314     } else {
315         idx                    = dsh->color_spec_index;
316         dsh->color_primaries = dirac_color_presets[idx].color_primaries;
317         dsh->colorspace      = dirac_color_presets[idx].colorspace;
318         dsh->color_trc       = dirac_color_presets[idx].color_trc;
319     }
320
321     return 0;
322 }
323
324 /* [DIRAC_STD] 10. Sequence Header. sequence_header() */
325 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
326                                    const uint8_t *buf, size_t buf_size,
327                                    void *log_ctx)
328 {
329     AVDiracSeqHeader *dsh;
330     GetBitContext gb;
331     unsigned video_format, picture_coding_mode;
332     int ret;
333
334     dsh = av_mallocz(sizeof(*dsh));
335     if (!dsh)
336         return AVERROR(ENOMEM);
337
338     ret = init_get_bits8(&gb, buf, buf_size);
339     if (ret < 0)
340         goto fail;
341
342     /* [DIRAC_SPEC] 10.1 Parse Parameters. parse_parameters() */
343     dsh->version.major = get_interleaved_ue_golomb(&gb);
344     dsh->version.minor = get_interleaved_ue_golomb(&gb);
345     dsh->profile   = get_interleaved_ue_golomb(&gb);
346     dsh->level     = get_interleaved_ue_golomb(&gb);
347     /* [DIRAC_SPEC] sequence_header() -> base_video_format as defined in
348      * 10.2 Base Video Format, table 10.1 Dirac predefined video formats */
349     video_format = get_interleaved_ue_golomb(&gb);
350
351     if (dsh->version.major < 2 && log_ctx)
352         av_log(log_ctx, AV_LOG_WARNING, "Stream is old and may not work\n");
353     else if (dsh->version.major > 2 && log_ctx)
354         av_log(log_ctx, AV_LOG_WARNING, "Stream may have unhandled features\n");
355
356     if (video_format > 20U) {
357         ret = AVERROR_INVALIDDATA;
358         goto fail;
359     }
360
361     /* Fill in defaults for the source parameters. */
362     dsh->width              = dirac_source_parameters_defaults[video_format].width;
363     dsh->height             = dirac_source_parameters_defaults[video_format].height;
364     dsh->chroma_format      = dirac_source_parameters_defaults[video_format].chroma_format;
365     dsh->interlaced         = dirac_source_parameters_defaults[video_format].interlaced;
366     dsh->top_field_first    = dirac_source_parameters_defaults[video_format].top_field_first;
367     dsh->frame_rate_index   = dirac_source_parameters_defaults[video_format].frame_rate_index;
368     dsh->aspect_ratio_index = dirac_source_parameters_defaults[video_format].aspect_ratio_index;
369     dsh->clean_width        = dirac_source_parameters_defaults[video_format].clean_width;
370     dsh->clean_height       = dirac_source_parameters_defaults[video_format].clean_height;
371     dsh->clean_left_offset  = dirac_source_parameters_defaults[video_format].clean_left_offset;
372     dsh->clean_right_offset = dirac_source_parameters_defaults[video_format].clean_right_offset;
373     dsh->pixel_range_index  = dirac_source_parameters_defaults[video_format].pixel_range_index;
374     dsh->color_spec_index   = dirac_source_parameters_defaults[video_format].color_spec_index;
375
376     /* [DIRAC_STD] 10.3 Source Parameters
377      * Override the defaults. */
378     ret = parse_source_parameters(dsh, &gb, log_ctx);
379     if (ret < 0)
380         goto fail;
381
382     /* [DIRAC_STD] picture_coding_mode shall be 0 for fields and 1 for frames
383      * currently only used to signal field coding */
384     picture_coding_mode = get_interleaved_ue_golomb(&gb);
385     if (picture_coding_mode != 0) {
386         if (log_ctx) {
387             av_log(log_ctx, AV_LOG_ERROR, "Unsupported picture coding mode %d",
388                    picture_coding_mode);
389         }
390         ret = AVERROR_INVALIDDATA;
391         goto fail;
392     }
393
394     *pdsh = dsh;
395     return 0;
396 fail:
397     av_freep(&dsh);
398     *pdsh = NULL;
399     return ret;
400 }
401 #else
402 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
403                                    const uint8_t *buf, size_t buf_size,
404                                    void *log_ctx)
405 {
406     return AVERROR(ENOSYS);
407 }
408 #endif