]> git.sesse.net Git - ffmpeg/blob - libavcodec/h264_ps.c
avfilter/vf_mcdeint: avoid uninitilaized fields in AVPacket
[ffmpeg] / libavcodec / h264_ps.c
1 /*
2  * H.26L/H.264/AVC/JVT/14496-10/... parameter set decoding
3  * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at>
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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  * H.264 / AVC / MPEG4 part10 parameter set decoding.
25  * @author Michael Niedermayer <michaelni@gmx.at>
26  */
27
28 #include <inttypes.h>
29
30 #include "libavutil/imgutils.h"
31 #include "internal.h"
32 #include "avcodec.h"
33 #include "h264.h"
34 #include "h264data.h"
35 #include "golomb.h"
36
37 #define MAX_LOG2_MAX_FRAME_NUM    (12 + 4)
38 #define MIN_LOG2_MAX_FRAME_NUM    4
39
40 #define QP(qP, depth) ((qP) + 6 * ((depth) - 8))
41
42 #define CHROMA_QP_TABLE_END(d)                                          \
43     QP(0,  d), QP(1,  d), QP(2,  d), QP(3,  d), QP(4,  d), QP(5,  d),   \
44     QP(6,  d), QP(7,  d), QP(8,  d), QP(9,  d), QP(10, d), QP(11, d),   \
45     QP(12, d), QP(13, d), QP(14, d), QP(15, d), QP(16, d), QP(17, d),   \
46     QP(18, d), QP(19, d), QP(20, d), QP(21, d), QP(22, d), QP(23, d),   \
47     QP(24, d), QP(25, d), QP(26, d), QP(27, d), QP(28, d), QP(29, d),   \
48     QP(29, d), QP(30, d), QP(31, d), QP(32, d), QP(32, d), QP(33, d),   \
49     QP(34, d), QP(34, d), QP(35, d), QP(35, d), QP(36, d), QP(36, d),   \
50     QP(37, d), QP(37, d), QP(37, d), QP(38, d), QP(38, d), QP(38, d),   \
51     QP(39, d), QP(39, d), QP(39, d), QP(39, d)
52
53 const uint8_t ff_h264_chroma_qp[7][QP_MAX_NUM + 1] = {
54     { CHROMA_QP_TABLE_END(8) },
55     { 0, 1, 2, 3, 4, 5,
56       CHROMA_QP_TABLE_END(9) },
57     { 0, 1, 2, 3,  4,  5,
58       6, 7, 8, 9, 10, 11,
59       CHROMA_QP_TABLE_END(10) },
60     { 0,  1, 2, 3,  4,  5,
61       6,  7, 8, 9, 10, 11,
62       12,13,14,15, 16, 17,
63       CHROMA_QP_TABLE_END(11) },
64     { 0,  1, 2, 3,  4,  5,
65       6,  7, 8, 9, 10, 11,
66       12,13,14,15, 16, 17,
67       18,19,20,21, 22, 23,
68       CHROMA_QP_TABLE_END(12) },
69     { 0,  1, 2, 3,  4,  5,
70       6,  7, 8, 9, 10, 11,
71       12,13,14,15, 16, 17,
72       18,19,20,21, 22, 23,
73       24,25,26,27, 28, 29,
74       CHROMA_QP_TABLE_END(13) },
75     { 0,  1, 2, 3,  4,  5,
76       6,  7, 8, 9, 10, 11,
77       12,13,14,15, 16, 17,
78       18,19,20,21, 22, 23,
79       24,25,26,27, 28, 29,
80       30,31,32,33, 34, 35,
81       CHROMA_QP_TABLE_END(14) },
82 };
83
84 static const uint8_t default_scaling4[2][16] = {
85     {  6, 13, 20, 28, 13, 20, 28, 32,
86       20, 28, 32, 37, 28, 32, 37, 42 },
87     { 10, 14, 20, 24, 14, 20, 24, 27,
88       20, 24, 27, 30, 24, 27, 30, 34 }
89 };
90
91 static const uint8_t default_scaling8[2][64] = {
92     {  6, 10, 13, 16, 18, 23, 25, 27,
93       10, 11, 16, 18, 23, 25, 27, 29,
94       13, 16, 18, 23, 25, 27, 29, 31,
95       16, 18, 23, 25, 27, 29, 31, 33,
96       18, 23, 25, 27, 29, 31, 33, 36,
97       23, 25, 27, 29, 31, 33, 36, 38,
98       25, 27, 29, 31, 33, 36, 38, 40,
99       27, 29, 31, 33, 36, 38, 40, 42 },
100     {  9, 13, 15, 17, 19, 21, 22, 24,
101       13, 13, 17, 19, 21, 22, 24, 25,
102       15, 17, 19, 21, 22, 24, 25, 27,
103       17, 19, 21, 22, 24, 25, 27, 28,
104       19, 21, 22, 24, 25, 27, 28, 30,
105       21, 22, 24, 25, 27, 28, 30, 32,
106       22, 24, 25, 27, 28, 30, 32, 33,
107       24, 25, 27, 28, 30, 32, 33, 35 }
108 };
109
110 static inline int decode_hrd_parameters(H264Context *h, SPS *sps)
111 {
112     int cpb_count, i;
113     cpb_count = get_ue_golomb_31(&h->gb) + 1;
114
115     if (cpb_count > 32U) {
116         av_log(h->avctx, AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
117         return AVERROR_INVALIDDATA;
118     }
119
120     get_bits(&h->gb, 4); /* bit_rate_scale */
121     get_bits(&h->gb, 4); /* cpb_size_scale */
122     for (i = 0; i < cpb_count; i++) {
123         get_ue_golomb_long(&h->gb); /* bit_rate_value_minus1 */
124         get_ue_golomb_long(&h->gb); /* cpb_size_value_minus1 */
125         get_bits1(&h->gb);          /* cbr_flag */
126     }
127     sps->initial_cpb_removal_delay_length = get_bits(&h->gb, 5) + 1;
128     sps->cpb_removal_delay_length         = get_bits(&h->gb, 5) + 1;
129     sps->dpb_output_delay_length          = get_bits(&h->gb, 5) + 1;
130     sps->time_offset_length               = get_bits(&h->gb, 5);
131     sps->cpb_cnt                          = cpb_count;
132     return 0;
133 }
134
135 static inline int decode_vui_parameters(H264Context *h, SPS *sps)
136 {
137     int aspect_ratio_info_present_flag;
138     unsigned int aspect_ratio_idc;
139
140     aspect_ratio_info_present_flag = get_bits1(&h->gb);
141
142     if (aspect_ratio_info_present_flag) {
143         aspect_ratio_idc = get_bits(&h->gb, 8);
144         if (aspect_ratio_idc == EXTENDED_SAR) {
145             sps->sar.num = get_bits(&h->gb, 16);
146             sps->sar.den = get_bits(&h->gb, 16);
147         } else if (aspect_ratio_idc < FF_ARRAY_ELEMS(ff_h264_pixel_aspect)) {
148             sps->sar = ff_h264_pixel_aspect[aspect_ratio_idc];
149         } else {
150             av_log(h->avctx, AV_LOG_ERROR, "illegal aspect ratio\n");
151             return AVERROR_INVALIDDATA;
152         }
153     } else {
154         sps->sar.num =
155         sps->sar.den = 0;
156     }
157
158     if (get_bits1(&h->gb))      /* overscan_info_present_flag */
159         get_bits1(&h->gb);      /* overscan_appropriate_flag */
160
161     sps->video_signal_type_present_flag = get_bits1(&h->gb);
162     if (sps->video_signal_type_present_flag) {
163         get_bits(&h->gb, 3);                 /* video_format */
164         sps->full_range = get_bits1(&h->gb); /* video_full_range_flag */
165
166         sps->colour_description_present_flag = get_bits1(&h->gb);
167         if (sps->colour_description_present_flag) {
168             sps->color_primaries = get_bits(&h->gb, 8); /* colour_primaries */
169             sps->color_trc       = get_bits(&h->gb, 8); /* transfer_characteristics */
170             sps->colorspace      = get_bits(&h->gb, 8); /* matrix_coefficients */
171             if (sps->color_primaries >= AVCOL_PRI_NB)
172                 sps->color_primaries = AVCOL_PRI_UNSPECIFIED;
173             if (sps->color_trc >= AVCOL_TRC_NB)
174                 sps->color_trc = AVCOL_TRC_UNSPECIFIED;
175             if (sps->colorspace >= AVCOL_SPC_NB)
176                 sps->colorspace = AVCOL_SPC_UNSPECIFIED;
177         }
178     }
179
180     /* chroma_location_info_present_flag */
181     if (get_bits1(&h->gb)) {
182         /* chroma_sample_location_type_top_field */
183         h->avctx->chroma_sample_location = get_ue_golomb(&h->gb) + 1;
184         get_ue_golomb(&h->gb);  /* chroma_sample_location_type_bottom_field */
185     }
186
187     if (show_bits1(&h->gb) && get_bits_left(&h->gb) < 10) {
188         av_log(h->avctx, AV_LOG_WARNING, "Truncated VUI\n");
189         return 0;
190     }
191
192     sps->timing_info_present_flag = get_bits1(&h->gb);
193     if (sps->timing_info_present_flag) {
194         sps->num_units_in_tick = get_bits_long(&h->gb, 32);
195         sps->time_scale        = get_bits_long(&h->gb, 32);
196         if (!sps->num_units_in_tick || !sps->time_scale) {
197             av_log(h->avctx, AV_LOG_ERROR,
198                    "time_scale/num_units_in_tick invalid or unsupported (%"PRIu32"/%"PRIu32")\n",
199                    sps->time_scale, sps->num_units_in_tick);
200             return AVERROR_INVALIDDATA;
201         }
202         sps->fixed_frame_rate_flag = get_bits1(&h->gb);
203     }
204
205     sps->nal_hrd_parameters_present_flag = get_bits1(&h->gb);
206     if (sps->nal_hrd_parameters_present_flag)
207         if (decode_hrd_parameters(h, sps) < 0)
208             return AVERROR_INVALIDDATA;
209     sps->vcl_hrd_parameters_present_flag = get_bits1(&h->gb);
210     if (sps->vcl_hrd_parameters_present_flag)
211         if (decode_hrd_parameters(h, sps) < 0)
212             return AVERROR_INVALIDDATA;
213     if (sps->nal_hrd_parameters_present_flag ||
214         sps->vcl_hrd_parameters_present_flag)
215         get_bits1(&h->gb);     /* low_delay_hrd_flag */
216     sps->pic_struct_present_flag = get_bits1(&h->gb);
217     if (!get_bits_left(&h->gb))
218         return 0;
219     sps->bitstream_restriction_flag = get_bits1(&h->gb);
220     if (sps->bitstream_restriction_flag) {
221         get_bits1(&h->gb);     /* motion_vectors_over_pic_boundaries_flag */
222         get_ue_golomb(&h->gb); /* max_bytes_per_pic_denom */
223         get_ue_golomb(&h->gb); /* max_bits_per_mb_denom */
224         get_ue_golomb(&h->gb); /* log2_max_mv_length_horizontal */
225         get_ue_golomb(&h->gb); /* log2_max_mv_length_vertical */
226         sps->num_reorder_frames = get_ue_golomb(&h->gb);
227         get_ue_golomb(&h->gb); /*max_dec_frame_buffering*/
228
229         if (get_bits_left(&h->gb) < 0) {
230             sps->num_reorder_frames         = 0;
231             sps->bitstream_restriction_flag = 0;
232         }
233
234         if (sps->num_reorder_frames > 16U
235             /* max_dec_frame_buffering || max_dec_frame_buffering > 16 */) {
236             av_log(h->avctx, AV_LOG_ERROR,
237                    "Clipping illegal num_reorder_frames %d\n",
238                    sps->num_reorder_frames);
239             sps->num_reorder_frames = 16;
240             return AVERROR_INVALIDDATA;
241         }
242     }
243
244     if (get_bits_left(&h->gb) < 0) {
245         av_log(h->avctx, AV_LOG_ERROR,
246                "Overread VUI by %d bits\n", -get_bits_left(&h->gb));
247         return AVERROR_INVALIDDATA;
248     }
249
250     return 0;
251 }
252
253 static void decode_scaling_list(H264Context *h, uint8_t *factors, int size,
254                                 const uint8_t *jvt_list,
255                                 const uint8_t *fallback_list)
256 {
257     int i, last = 8, next = 8;
258     const uint8_t *scan = size == 16 ? zigzag_scan : ff_zigzag_direct;
259     if (!get_bits1(&h->gb)) /* matrix not written, we use the predicted one */
260         memcpy(factors, fallback_list, size * sizeof(uint8_t));
261     else
262         for (i = 0; i < size; i++) {
263             if (next)
264                 next = (last + get_se_golomb(&h->gb)) & 0xff;
265             if (!i && !next) { /* matrix not written, we use the preset one */
266                 memcpy(factors, jvt_list, size * sizeof(uint8_t));
267                 break;
268             }
269             last = factors[scan[i]] = next ? next : last;
270         }
271 }
272
273 static void decode_scaling_matrices(H264Context *h, SPS *sps,
274                                     PPS *pps, int is_sps,
275                                     uint8_t(*scaling_matrix4)[16],
276                                     uint8_t(*scaling_matrix8)[64])
277 {
278     int fallback_sps = !is_sps && sps->scaling_matrix_present;
279     const uint8_t *fallback[4] = {
280         fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
281         fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
282         fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
283         fallback_sps ? sps->scaling_matrix8[3] : default_scaling8[1]
284     };
285     if (get_bits1(&h->gb)) {
286         sps->scaling_matrix_present |= is_sps;
287         decode_scaling_list(h, scaling_matrix4[0], 16, default_scaling4[0], fallback[0]);        // Intra, Y
288         decode_scaling_list(h, scaling_matrix4[1], 16, default_scaling4[0], scaling_matrix4[0]); // Intra, Cr
289         decode_scaling_list(h, scaling_matrix4[2], 16, default_scaling4[0], scaling_matrix4[1]); // Intra, Cb
290         decode_scaling_list(h, scaling_matrix4[3], 16, default_scaling4[1], fallback[1]);        // Inter, Y
291         decode_scaling_list(h, scaling_matrix4[4], 16, default_scaling4[1], scaling_matrix4[3]); // Inter, Cr
292         decode_scaling_list(h, scaling_matrix4[5], 16, default_scaling4[1], scaling_matrix4[4]); // Inter, Cb
293         if (is_sps || pps->transform_8x8_mode) {
294             decode_scaling_list(h, scaling_matrix8[0], 64, default_scaling8[0], fallback[2]); // Intra, Y
295             decode_scaling_list(h, scaling_matrix8[3], 64, default_scaling8[1], fallback[3]); // Inter, Y
296             if (sps->chroma_format_idc == 3) {
297                 decode_scaling_list(h, scaling_matrix8[1], 64, default_scaling8[0], scaling_matrix8[0]); // Intra, Cr
298                 decode_scaling_list(h, scaling_matrix8[4], 64, default_scaling8[1], scaling_matrix8[3]); // Inter, Cr
299                 decode_scaling_list(h, scaling_matrix8[2], 64, default_scaling8[0], scaling_matrix8[1]); // Intra, Cb
300                 decode_scaling_list(h, scaling_matrix8[5], 64, default_scaling8[1], scaling_matrix8[4]); // Inter, Cb
301             }
302         }
303     }
304 }
305
306 int ff_h264_decode_seq_parameter_set(H264Context *h)
307 {
308     int profile_idc, level_idc, constraint_set_flags = 0;
309     unsigned int sps_id;
310     int i, log2_max_frame_num_minus4;
311     SPS *sps;
312
313     profile_idc           = get_bits(&h->gb, 8);
314     constraint_set_flags |= get_bits1(&h->gb) << 0;   // constraint_set0_flag
315     constraint_set_flags |= get_bits1(&h->gb) << 1;   // constraint_set1_flag
316     constraint_set_flags |= get_bits1(&h->gb) << 2;   // constraint_set2_flag
317     constraint_set_flags |= get_bits1(&h->gb) << 3;   // constraint_set3_flag
318     constraint_set_flags |= get_bits1(&h->gb) << 4;   // constraint_set4_flag
319     constraint_set_flags |= get_bits1(&h->gb) << 5;   // constraint_set5_flag
320     skip_bits(&h->gb, 2);                             // reserved_zero_2bits
321     level_idc = get_bits(&h->gb, 8);
322     sps_id    = get_ue_golomb_31(&h->gb);
323
324     if (sps_id >= MAX_SPS_COUNT) {
325         av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", sps_id);
326         return AVERROR_INVALIDDATA;
327     }
328     sps = av_mallocz(sizeof(SPS));
329     if (!sps)
330         return AVERROR(ENOMEM);
331
332     sps->sps_id               = sps_id;
333     sps->time_offset_length   = 24;
334     sps->profile_idc          = profile_idc;
335     sps->constraint_set_flags = constraint_set_flags;
336     sps->level_idc            = level_idc;
337     sps->full_range           = -1;
338
339     memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
340     memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
341     sps->scaling_matrix_present = 0;
342     sps->colorspace = 2; //AVCOL_SPC_UNSPECIFIED
343
344     if (sps->profile_idc == 100 ||  // High profile
345         sps->profile_idc == 110 ||  // High10 profile
346         sps->profile_idc == 122 ||  // High422 profile
347         sps->profile_idc == 244 ||  // High444 Predictive profile
348         sps->profile_idc ==  44 ||  // Cavlc444 profile
349         sps->profile_idc ==  83 ||  // Scalable Constrained High profile (SVC)
350         sps->profile_idc ==  86 ||  // Scalable High Intra profile (SVC)
351         sps->profile_idc == 118 ||  // Stereo High profile (MVC)
352         sps->profile_idc == 128 ||  // Multiview High profile (MVC)
353         sps->profile_idc == 138 ||  // Multiview Depth High profile (MVCD)
354         sps->profile_idc == 144) {  // old High444 profile
355         sps->chroma_format_idc = get_ue_golomb_31(&h->gb);
356         if (sps->chroma_format_idc > 3U) {
357             avpriv_request_sample(h->avctx, "chroma_format_idc %u",
358                                   sps->chroma_format_idc);
359             goto fail;
360         } else if (sps->chroma_format_idc == 3) {
361             sps->residual_color_transform_flag = get_bits1(&h->gb);
362             if (sps->residual_color_transform_flag) {
363                 av_log(h->avctx, AV_LOG_ERROR, "separate color planes are not supported\n");
364                 goto fail;
365             }
366         }
367         sps->bit_depth_luma   = get_ue_golomb(&h->gb) + 8;
368         sps->bit_depth_chroma = get_ue_golomb(&h->gb) + 8;
369         if (sps->bit_depth_chroma != sps->bit_depth_luma) {
370             avpriv_request_sample(h->avctx,
371                                   "Different chroma and luma bit depth");
372             goto fail;
373         }
374         if (sps->bit_depth_luma > 14U || sps->bit_depth_chroma > 14U) {
375             av_log(h->avctx, AV_LOG_ERROR, "illegal bit depth value (%d, %d)\n",
376                    sps->bit_depth_luma, sps->bit_depth_chroma);
377             goto fail;
378         }
379         sps->transform_bypass = get_bits1(&h->gb);
380         decode_scaling_matrices(h, sps, NULL, 1,
381                                 sps->scaling_matrix4, sps->scaling_matrix8);
382     } else {
383         sps->chroma_format_idc = 1;
384         sps->bit_depth_luma    = 8;
385         sps->bit_depth_chroma  = 8;
386     }
387
388     log2_max_frame_num_minus4 = get_ue_golomb(&h->gb);
389     if (log2_max_frame_num_minus4 < MIN_LOG2_MAX_FRAME_NUM - 4 ||
390         log2_max_frame_num_minus4 > MAX_LOG2_MAX_FRAME_NUM - 4) {
391         av_log(h->avctx, AV_LOG_ERROR,
392                "log2_max_frame_num_minus4 out of range (0-12): %d\n",
393                log2_max_frame_num_minus4);
394         goto fail;
395     }
396     sps->log2_max_frame_num = log2_max_frame_num_minus4 + 4;
397
398     sps->poc_type = get_ue_golomb_31(&h->gb);
399
400     if (sps->poc_type == 0) { // FIXME #define
401         unsigned t = get_ue_golomb(&h->gb);
402         if (t>12) {
403             av_log(h->avctx, AV_LOG_ERROR, "log2_max_poc_lsb (%d) is out of range\n", t);
404             goto fail;
405         }
406         sps->log2_max_poc_lsb = t + 4;
407     } else if (sps->poc_type == 1) { // FIXME #define
408         sps->delta_pic_order_always_zero_flag = get_bits1(&h->gb);
409         sps->offset_for_non_ref_pic           = get_se_golomb(&h->gb);
410         sps->offset_for_top_to_bottom_field   = get_se_golomb(&h->gb);
411         sps->poc_cycle_length                 = get_ue_golomb(&h->gb);
412
413         if ((unsigned)sps->poc_cycle_length >=
414             FF_ARRAY_ELEMS(sps->offset_for_ref_frame)) {
415             av_log(h->avctx, AV_LOG_ERROR,
416                    "poc_cycle_length overflow %d\n", sps->poc_cycle_length);
417             goto fail;
418         }
419
420         for (i = 0; i < sps->poc_cycle_length; i++)
421             sps->offset_for_ref_frame[i] = get_se_golomb(&h->gb);
422     } else if (sps->poc_type != 2) {
423         av_log(h->avctx, AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
424         goto fail;
425     }
426
427     sps->ref_frame_count = get_ue_golomb_31(&h->gb);
428     if (h->avctx->codec_tag == MKTAG('S', 'M', 'V', '2'))
429         sps->ref_frame_count = FFMAX(2, sps->ref_frame_count);
430     if (sps->ref_frame_count > H264_MAX_PICTURE_COUNT - 2 ||
431         sps->ref_frame_count > 16U) {
432         av_log(h->avctx, AV_LOG_ERROR,
433                "too many reference frames %d\n", sps->ref_frame_count);
434         goto fail;
435     }
436     sps->gaps_in_frame_num_allowed_flag = get_bits1(&h->gb);
437     sps->mb_width                       = get_ue_golomb(&h->gb) + 1;
438     sps->mb_height                      = get_ue_golomb(&h->gb) + 1;
439     if ((unsigned)sps->mb_width  >= INT_MAX / 16 ||
440         (unsigned)sps->mb_height >= INT_MAX / 16 ||
441         av_image_check_size(16 * sps->mb_width,
442                             16 * sps->mb_height, 0, h->avctx)) {
443         av_log(h->avctx, AV_LOG_ERROR, "mb_width/height overflow\n");
444         goto fail;
445     }
446
447     sps->frame_mbs_only_flag = get_bits1(&h->gb);
448     if (!sps->frame_mbs_only_flag)
449         sps->mb_aff = get_bits1(&h->gb);
450     else
451         sps->mb_aff = 0;
452
453     sps->direct_8x8_inference_flag = get_bits1(&h->gb);
454
455 #ifndef ALLOW_INTERLACE
456     if (sps->mb_aff)
457         av_log(h->avctx, AV_LOG_ERROR,
458                "MBAFF support not included; enable it at compile-time.\n");
459 #endif
460     sps->crop = get_bits1(&h->gb);
461     if (sps->crop) {
462         int crop_left   = get_ue_golomb(&h->gb);
463         int crop_right  = get_ue_golomb(&h->gb);
464         int crop_top    = get_ue_golomb(&h->gb);
465         int crop_bottom = get_ue_golomb(&h->gb);
466         int width  = 16 * sps->mb_width;
467         int height = 16 * sps->mb_height * (2 - sps->frame_mbs_only_flag);
468
469         if (h->avctx->flags2 & CODEC_FLAG2_IGNORE_CROP) {
470             av_log(h->avctx, AV_LOG_DEBUG, "discarding sps cropping, original "
471                                            "values are l:%d r:%d t:%d b:%d\n",
472                    crop_left, crop_right, crop_top, crop_bottom);
473
474             sps->crop_left   =
475             sps->crop_right  =
476             sps->crop_top    =
477             sps->crop_bottom = 0;
478         } else {
479             int vsub   = (sps->chroma_format_idc == 1) ? 1 : 0;
480             int hsub   = (sps->chroma_format_idc == 1 ||
481                           sps->chroma_format_idc == 2) ? 1 : 0;
482             int step_x = 1 << hsub;
483             int step_y = (2 - sps->frame_mbs_only_flag) << vsub;
484
485             if (crop_left & (0x1F >> (sps->bit_depth_luma > 8)) &&
486                 !(h->avctx->flags & CODEC_FLAG_UNALIGNED)) {
487                 crop_left &= ~(0x1F >> (sps->bit_depth_luma > 8));
488                 av_log(h->avctx, AV_LOG_WARNING,
489                        "Reducing left cropping to %d "
490                        "chroma samples to preserve alignment.\n",
491                        crop_left);
492             }
493
494             if (crop_left  > (unsigned)INT_MAX / 4 / step_x ||
495                 crop_right > (unsigned)INT_MAX / 4 / step_x ||
496                 crop_top   > (unsigned)INT_MAX / 4 / step_y ||
497                 crop_bottom> (unsigned)INT_MAX / 4 / step_y ||
498                 (crop_left + crop_right ) * step_x >= width ||
499                 (crop_top  + crop_bottom) * step_y >= height
500             ) {
501                 av_log(h->avctx, AV_LOG_ERROR, "crop values invalid %d %d %d %d / %d %d\n", crop_left, crop_right, crop_top, crop_bottom, width, height);
502                 goto fail;
503             }
504
505             sps->crop_left   = crop_left   * step_x;
506             sps->crop_right  = crop_right  * step_x;
507             sps->crop_top    = crop_top    * step_y;
508             sps->crop_bottom = crop_bottom * step_y;
509         }
510     } else {
511         sps->crop_left   =
512         sps->crop_right  =
513         sps->crop_top    =
514         sps->crop_bottom =
515         sps->crop        = 0;
516     }
517
518     sps->vui_parameters_present_flag = get_bits1(&h->gb);
519     if (sps->vui_parameters_present_flag) {
520         int ret = decode_vui_parameters(h, sps);
521         if (ret < 0)
522             goto fail;
523     }
524
525     if (!sps->sar.den)
526         sps->sar.den = 1;
527
528     if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
529         static const char csp[4][5] = { "Gray", "420", "422", "444" };
530         av_log(h->avctx, AV_LOG_DEBUG,
531                "sps:%u profile:%d/%d poc:%d ref:%d %dx%d %s %s crop:%u/%u/%u/%u %s %s %"PRId32"/%"PRId32" b%d reo:%d\n",
532                sps_id, sps->profile_idc, sps->level_idc,
533                sps->poc_type,
534                sps->ref_frame_count,
535                sps->mb_width, sps->mb_height,
536                sps->frame_mbs_only_flag ? "FRM" : (sps->mb_aff ? "MB-AFF" : "PIC-AFF"),
537                sps->direct_8x8_inference_flag ? "8B8" : "",
538                sps->crop_left, sps->crop_right,
539                sps->crop_top, sps->crop_bottom,
540                sps->vui_parameters_present_flag ? "VUI" : "",
541                csp[sps->chroma_format_idc],
542                sps->timing_info_present_flag ? sps->num_units_in_tick : 0,
543                sps->timing_info_present_flag ? sps->time_scale : 0,
544                sps->bit_depth_luma,
545                sps->bitstream_restriction_flag ? sps->num_reorder_frames : -1
546                );
547     }
548     sps->new = 1;
549
550     av_free(h->sps_buffers[sps_id]);
551     h->sps_buffers[sps_id] = sps;
552
553     return 0;
554
555 fail:
556     av_free(sps);
557     return -1;
558 }
559
560 static void build_qp_table(PPS *pps, int t, int index, const int depth)
561 {
562     int i;
563     const int max_qp = 51 + 6 * (depth - 8);
564     for (i = 0; i < max_qp + 1; i++)
565         pps->chroma_qp_table[t][i] =
566             ff_h264_chroma_qp[depth - 8][av_clip(i + index, 0, max_qp)];
567 }
568
569 static int more_rbsp_data_in_pps(H264Context *h, PPS *pps)
570 {
571     const SPS *sps = h->sps_buffers[pps->sps_id];
572     int profile_idc = sps->profile_idc;
573
574     if ((profile_idc == 66 || profile_idc == 77 ||
575          profile_idc == 88) && (sps->constraint_set_flags & 7)) {
576         av_log(h->avctx, AV_LOG_VERBOSE,
577                "Current profile doesn't provide more RBSP data in PPS, skipping\n");
578         return 0;
579     }
580
581     return 1;
582 }
583
584 int ff_h264_decode_picture_parameter_set(H264Context *h, int bit_length)
585 {
586     unsigned int pps_id = get_ue_golomb(&h->gb);
587     PPS *pps;
588     SPS *sps;
589     int qp_bd_offset;
590     int bits_left;
591
592     if (pps_id >= MAX_PPS_COUNT) {
593         av_log(h->avctx, AV_LOG_ERROR, "pps_id %u out of range\n", pps_id);
594         return AVERROR_INVALIDDATA;
595     }
596
597     pps = av_mallocz(sizeof(PPS));
598     if (!pps)
599         return AVERROR(ENOMEM);
600     pps->sps_id = get_ue_golomb_31(&h->gb);
601     if ((unsigned)pps->sps_id >= MAX_SPS_COUNT ||
602         !h->sps_buffers[pps->sps_id]) {
603         av_log(h->avctx, AV_LOG_ERROR, "sps_id %u out of range\n", pps->sps_id);
604         goto fail;
605     }
606     sps = h->sps_buffers[pps->sps_id];
607     qp_bd_offset = 6 * (sps->bit_depth_luma - 8);
608     if (sps->bit_depth_luma > 14) {
609         av_log(h->avctx, AV_LOG_ERROR,
610                "Invalid luma bit depth=%d\n",
611                sps->bit_depth_luma);
612         goto fail;
613     } else if (sps->bit_depth_luma == 11 || sps->bit_depth_luma == 13) {
614         av_log(h->avctx, AV_LOG_ERROR,
615                "Unimplemented luma bit depth=%d\n",
616                sps->bit_depth_luma);
617         goto fail;
618     }
619
620     pps->cabac             = get_bits1(&h->gb);
621     pps->pic_order_present = get_bits1(&h->gb);
622     pps->slice_group_count = get_ue_golomb(&h->gb) + 1;
623     if (pps->slice_group_count > 1) {
624         pps->mb_slice_group_map_type = get_ue_golomb(&h->gb);
625         av_log(h->avctx, AV_LOG_ERROR, "FMO not supported\n");
626         switch (pps->mb_slice_group_map_type) {
627         case 0:
628 #if 0
629     |       for (i = 0; i <= num_slice_groups_minus1; i++)  |   |      |
630     |           run_length[i]                               |1  |ue(v) |
631 #endif
632             break;
633         case 2:
634 #if 0
635     |       for (i = 0; i < num_slice_groups_minus1; i++) { |   |      |
636     |           top_left_mb[i]                              |1  |ue(v) |
637     |           bottom_right_mb[i]                          |1  |ue(v) |
638     |       }                                               |   |      |
639 #endif
640             break;
641         case 3:
642         case 4:
643         case 5:
644 #if 0
645     |       slice_group_change_direction_flag               |1  |u(1)  |
646     |       slice_group_change_rate_minus1                  |1  |ue(v) |
647 #endif
648             break;
649         case 6:
650 #if 0
651     |       slice_group_id_cnt_minus1                       |1  |ue(v) |
652     |       for (i = 0; i <= slice_group_id_cnt_minus1; i++)|   |      |
653     |           slice_group_id[i]                           |1  |u(v)  |
654 #endif
655             break;
656         }
657     }
658     pps->ref_count[0] = get_ue_golomb(&h->gb) + 1;
659     pps->ref_count[1] = get_ue_golomb(&h->gb) + 1;
660     if (pps->ref_count[0] - 1 > 32 - 1 || pps->ref_count[1] - 1 > 32 - 1) {
661         av_log(h->avctx, AV_LOG_ERROR, "reference overflow (pps)\n");
662         goto fail;
663     }
664
665     pps->weighted_pred                        = get_bits1(&h->gb);
666     pps->weighted_bipred_idc                  = get_bits(&h->gb, 2);
667     pps->init_qp                              = get_se_golomb(&h->gb) + 26 + qp_bd_offset;
668     pps->init_qs                              = get_se_golomb(&h->gb) + 26 + qp_bd_offset;
669     pps->chroma_qp_index_offset[0]            = get_se_golomb(&h->gb);
670     pps->deblocking_filter_parameters_present = get_bits1(&h->gb);
671     pps->constrained_intra_pred               = get_bits1(&h->gb);
672     pps->redundant_pic_cnt_present            = get_bits1(&h->gb);
673
674     pps->transform_8x8_mode = 0;
675     // contents of sps/pps can change even if id doesn't, so reinit
676     h->dequant_coeff_pps = -1;
677     memcpy(pps->scaling_matrix4, h->sps_buffers[pps->sps_id]->scaling_matrix4,
678            sizeof(pps->scaling_matrix4));
679     memcpy(pps->scaling_matrix8, h->sps_buffers[pps->sps_id]->scaling_matrix8,
680            sizeof(pps->scaling_matrix8));
681
682     bits_left = bit_length - get_bits_count(&h->gb);
683     if (bits_left > 0 && more_rbsp_data_in_pps(h, pps)) {
684         pps->transform_8x8_mode = get_bits1(&h->gb);
685         decode_scaling_matrices(h, h->sps_buffers[pps->sps_id], pps, 0,
686                                 pps->scaling_matrix4, pps->scaling_matrix8);
687         // second_chroma_qp_index_offset
688         pps->chroma_qp_index_offset[1] = get_se_golomb(&h->gb);
689     } else {
690         pps->chroma_qp_index_offset[1] = pps->chroma_qp_index_offset[0];
691     }
692
693     build_qp_table(pps, 0, pps->chroma_qp_index_offset[0], sps->bit_depth_luma);
694     build_qp_table(pps, 1, pps->chroma_qp_index_offset[1], sps->bit_depth_luma);
695     if (pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
696         pps->chroma_qp_diff = 1;
697
698     if (h->avctx->debug & FF_DEBUG_PICT_INFO) {
699         av_log(h->avctx, AV_LOG_DEBUG,
700                "pps:%u sps:%u %s slice_groups:%d ref:%u/%u %s qp:%d/%d/%d/%d %s %s %s %s\n",
701                pps_id, pps->sps_id,
702                pps->cabac ? "CABAC" : "CAVLC",
703                pps->slice_group_count,
704                pps->ref_count[0], pps->ref_count[1],
705                pps->weighted_pred ? "weighted" : "",
706                pps->init_qp, pps->init_qs, pps->chroma_qp_index_offset[0], pps->chroma_qp_index_offset[1],
707                pps->deblocking_filter_parameters_present ? "LPAR" : "",
708                pps->constrained_intra_pred ? "CONSTR" : "",
709                pps->redundant_pic_cnt_present ? "REDU" : "",
710                pps->transform_8x8_mode ? "8x8DCT" : "");
711     }
712
713     av_free(h->pps_buffers[pps_id]);
714     h->pps_buffers[pps_id] = pps;
715     return 0;
716
717 fail:
718     av_free(pps);
719     return -1;
720 }