]> git.sesse.net Git - ffmpeg/blob - libavcodec/dirac.c
pthread_frame: ensure the threads don't run simultaneously with hwaccel
[ffmpeg] / libavcodec / dirac.c
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
22 /**
23  * @file
24  * Dirac Decoder
25  * @author Marco Gerards <marco@gnu.org>
26  */
27
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;          ///< 0: 444  1: 422  2: 420
42
43     uint8_t interlaced;
44     uint8_t top_field_first;
45
46     uint8_t frame_rate_index;       ///< index into dirac_frame_rate[]
47     uint8_t aspect_ratio_index;     ///< index into dirac_aspect_ratio[]
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;      ///< index into dirac_pixel_range_presets[]
55     uint8_t color_spec_index;       ///< index into dirac_color_spec_presets[]
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[] = {
116     AVCOL_PRI_BT709,
117     AVCOL_PRI_SMPTE170M,
118     AVCOL_PRI_BT470BG,
119 };
120
121 static const struct {
122     enum AVColorPrimaries color_primaries;
123     enum AVColorSpace colorspace;
124     enum AVColorTransferCharacteristic color_trc;
125 } dirac_color_presets[] = {
126     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_BT709 },
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 },
130     { AVCOL_PRI_BT709,     AVCOL_SPC_BT709,   AVCOL_TRC_UNSPECIFIED /* DCinema */ },
131 };
132
133 /* [DIRAC_STD] Table 10.2 Supported chroma sampling formats + luma Offset */
134 static const enum AVPixelFormat dirac_pix_fmt[2][3] = {
135     { AV_PIX_FMT_YUV444P,  AV_PIX_FMT_YUV422P,  AV_PIX_FMT_YUV420P  },
136     { AV_PIX_FMT_YUVJ444P, AV_PIX_FMT_YUVJ422P, AV_PIX_FMT_YUVJ420P },
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 */
160         dsh->chroma_format = get_interleaved_ue_golomb(gb);
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 */
172         dsh->interlaced = get_interleaved_ue_golomb(gb);
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 */
178         dsh->frame_rate_index = get_interleaved_ue_golomb(gb);
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 */
204         dsh->aspect_ratio_index = get_interleaved_ue_golomb(gb);
205
206         if (dsh->aspect_ratio_index > 6)
207             return AVERROR_INVALIDDATA;
208
209         if (!dsh->aspect_ratio_index) {
210             dsh->sample_aspect_ratio.num = get_interleaved_ue_golomb(gb);
211             dsh->sample_aspect_ratio.den = get_interleaved_ue_golomb(gb);
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 */
223         dsh->clean_width = get_interleaved_ue_golomb(gb);
224         /* [DIRAC_STD] CLEAN_HEIGHT */
225         dsh->clean_height = get_interleaved_ue_golomb(gb);
226         /* [DIRAC_STD] CLEAN_LEFT_OFFSET */
227         dsh->clean_left_offset = get_interleaved_ue_golomb(gb);
228         /* [DIRAC_STD] CLEAN_RIGHT_OFFSET */
229         dsh->clean_right_offset = get_interleaved_ue_golomb(gb);
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 */
237         dsh->pixel_range_index = get_interleaved_ue_golomb(gb);
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
249                                            : AVCOL_RANGE_JPEG;
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 */
268         idx = dsh->color_spec_index = get_interleaved_ue_golomb(gb);
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)
288                     dsh->colorspace = AVCOL_SPC_BT709;
289                 else if (idx == 1)
290                     dsh->colorspace = AVCOL_SPC_BT470BG;
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() */
307 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
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 */
328     dsh->profile = get_interleaved_ue_golomb(&gb);
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
387 int av_dirac_parse_sequence_header(AVDiracSeqHeader **pdsh,
388                                    const uint8_t *buf, size_t buf_size,
389                                    void *log_ctx)
390 {
391     return AVERROR(ENOSYS);
392 }
393 #endif