view libavcodec/h264_ps.c @ 3:0b056460c67d

changed code to use VSs
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Mon, 29 Oct 2012 16:44:27 +0100
parents
children
line source
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 */
22 /**
23 * @file
24 * H.264 / AVC / MPEG4 part10 parameter set decoding.
25 * @author Michael Niedermayer <michaelni@gmx.at>
26 */
28 #include "dsputil.h"
29 #include "avcodec.h"
30 #include "h264_types.h"
31 #include "h264_data.h"
32 #include "golomb.h"
35 //#undef NDEBUG
36 #include <assert.h>
38 static const int pixel_aspect[17][2]={
39 {0, 1},
40 {1, 1},
41 {12, 11},
42 {10, 11},
43 {16, 11},
44 {40, 33},
45 {24, 11},
46 {20, 11},
47 {32, 11},
48 {80, 33},
49 {18, 11},
50 {15, 11},
51 {64, 33},
52 {160,99},
53 {4, 3},
54 {3, 2},
55 {2, 1},
56 };
58 const uint8_t ff_h264_chroma_qp[52]={
59 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,
60 12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,
61 28,29,29,30,31,32,32,33,34,34,35,35,36,36,37,37,
62 37,38,38,38,39,39,39,39
63 };
65 static const uint8_t default_scaling4[2][16]={
66 { 6,13,20,28,
67 13,20,28,32,
68 20,28,32,37,
69 28,32,37,42
70 },{
71 10,14,20,24,
72 14,20,24,27,
73 20,24,27,30,
74 24,27,30,34
75 }};
77 static const uint8_t default_scaling8[2][64]={
78 { 6,10,13,16,18,23,25,27,
79 10,11,16,18,23,25,27,29,
80 13,16,18,23,25,27,29,31,
81 16,18,23,25,27,29,31,33,
82 18,23,25,27,29,31,33,36,
83 23,25,27,29,31,33,36,38,
84 25,27,29,31,33,36,38,40,
85 27,29,31,33,36,38,40,42
86 },{
87 9,13,15,17,19,21,22,24,
88 13,13,17,19,21,22,24,25,
89 15,17,19,21,22,24,25,27,
90 17,19,21,22,24,25,27,28,
91 19,21,22,24,25,27,28,30,
92 21,22,24,25,27,28,30,32,
93 22,24,25,27,28,30,32,33,
94 24,25,27,28,30,32,33,35
95 }};
97 static inline int decode_hrd_parameters(GetBitContext *gb, SPS *sps){
98 int cpb_count, i;
99 cpb_count = get_ue_golomb_31(gb) + 1;
101 if(cpb_count > 32){
102 av_log(AV_LOG_ERROR, "cpb_count %d invalid\n", cpb_count);
103 return -1;
104 }
106 get_bits(gb, 4); /* bit_rate_scale */
107 get_bits(gb, 4); /* cpb_size_scale */
108 for(i=0; i<cpb_count; i++){
109 get_ue_golomb(gb); /* bit_rate_value_minus1 */
110 get_ue_golomb(gb); /* cpb_size_value_minus1 */
111 get_bits1(gb); /* cbr_flag */
112 }
113 sps->initial_cpb_removal_delay_length = get_bits(gb, 5) + 1;
114 sps->cpb_removal_delay_length = get_bits(gb, 5) + 1;
115 sps->dpb_output_delay_length = get_bits(gb, 5) + 1;
116 sps->time_offset_length = get_bits(gb, 5);
117 sps->cpb_cnt = cpb_count;
118 return 0;
119 }
121 static inline int decode_vui_parameters(GetBitContext *gb, SPS *sps){
122 int aspect_ratio_info_present_flag;
123 unsigned int aspect_ratio_idc;
125 aspect_ratio_info_present_flag= get_bits1(gb);
127 if( aspect_ratio_info_present_flag ) {
128 aspect_ratio_idc= get_bits(gb, 8);
129 if( aspect_ratio_idc == EXTENDED_SAR ) {
130 sps->num= get_bits(gb, 16);
131 sps->den= get_bits(gb, 16);
132 }else if(aspect_ratio_idc < sizeof(pixel_aspect)/sizeof(int[2])){
133 //sps->sar= pixel_aspect[aspect_ratio_idc];
134 }else{
135 av_log( AV_LOG_ERROR, "illegal aspect ratio idc %d\n", aspect_ratio_idc);
136 // return -1;
137 }
138 }else{
139 sps->num=
140 sps->den= 0;
141 }
143 if(get_bits1(gb)){ /* overscan_info_present_flag */
144 get_bits1(gb); /* overscan_appropriate_flag */
145 }
147 sps->video_signal_type_present_flag = get_bits1(gb);
148 if(sps->video_signal_type_present_flag){
149 get_bits(gb, 3); /* video_format */
150 sps->full_range = get_bits1(gb); /* video_full_range_flag */
152 sps->colour_description_present_flag = get_bits1(gb);
153 if(sps->colour_description_present_flag){
154 sps->color_primaries = get_bits(gb, 8); /* colour_primaries */
155 sps->color_trc = get_bits(gb, 8); /* transfer_characteristics */
156 sps->colorspace = get_bits(gb, 8); /* matrix_coefficients */
157 if (sps->color_primaries >= AVCOL_PRI_NB)
158 sps->color_primaries = AVCOL_PRI_UNSPECIFIED;
159 if (sps->color_trc >= AVCOL_TRC_NB)
160 sps->color_trc = AVCOL_TRC_UNSPECIFIED;
161 if (sps->colorspace >= AVCOL_SPC_NB)
162 sps->colorspace = AVCOL_SPC_UNSPECIFIED;
163 }
164 }
166 if(get_bits1(gb)){ /* chroma_location_info_present_flag */
167 av_log(AV_LOG_ERROR, "chroma_location_info_present_flag found, but not supported\n");
168 (void) (get_ue_golomb(gb)+1); /* chroma_sample_location_type_top_field */
169 (void) get_ue_golomb(gb); /* chroma_sample_location_type_bottom_field */
170 }
172 sps->timing_info_present_flag = get_bits1(gb);
173 if(sps->timing_info_present_flag){
174 sps->num_units_in_tick = get_bits_long(gb, 32);
175 sps->time_scale = get_bits_long(gb, 32);
176 if(!sps->num_units_in_tick || !sps->time_scale){
177 av_log(AV_LOG_ERROR, "time_scale/num_units_in_tick invalid or unsupported (%d/%d)\n", sps->time_scale, sps->num_units_in_tick);
178 return -1;
179 }
180 sps->fixed_frame_rate_flag = get_bits1(gb);
181 }
183 sps->nal_hrd_parameters_present_flag = get_bits1(gb);
184 if(sps->nal_hrd_parameters_present_flag)
185 if(decode_hrd_parameters(gb, sps) < 0)
186 return -1;
187 sps->vcl_hrd_parameters_present_flag = get_bits1(gb);
188 if(sps->vcl_hrd_parameters_present_flag)
189 if(decode_hrd_parameters(gb, sps) < 0)
190 return -1;
191 if(sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag)
192 get_bits1(gb); /* low_delay_hrd_flag */
193 sps->pic_struct_present_flag = get_bits1(gb);
195 sps->bitstream_restriction_flag = get_bits1(gb);
196 if(sps->bitstream_restriction_flag){
197 get_bits1(gb); /* motion_vectors_over_pic_boundaries_flag */
198 get_ue_golomb(gb); /* max_bytes_per_pic_denom */
199 get_ue_golomb(gb); /* max_bits_per_mb_denom */
200 get_ue_golomb(gb); /* log2_max_mv_length_horizontal */
201 get_ue_golomb(gb); /* log2_max_mv_length_vertical */
202 sps->num_reorder_frames= get_ue_golomb(gb);
203 get_ue_golomb(gb); /*max_dec_frame_buffering*/
205 if(sps->num_reorder_frames > 16 /*max_dec_frame_buffering || max_dec_frame_buffering > 16*/){
206 av_log(AV_LOG_ERROR, "illegal num_reorder_frames %d\n", sps->num_reorder_frames);
207 return -1;
208 }
209 }
211 return 0;
212 }
214 static void decode_scaling_list(GetBitContext *gb, uint8_t *factors, int size, const uint8_t *jvt_list, const uint8_t *fallback_list){
215 int i, last = 8, next = 8;
216 const uint8_t *scan = size == 16 ? zigzag_scan : ff_zigzag_direct;
217 if(!get_bits1(gb)) /* matrix not written, we use the predicted one */
218 memcpy(factors, fallback_list, size*sizeof(uint8_t));
219 else
220 for(i=0;i<size;i++){
221 if(next)
222 next = (last + get_se_golomb(gb)) & 0xff;
223 if(!i && !next){ /* matrix not written, we use the preset one */
224 memcpy(factors, jvt_list, size*sizeof(uint8_t));
225 break;
226 }
227 last = factors[scan[i]] = next ? next : last;
228 }
229 }
231 static void decode_scaling_matrices(GetBitContext *gb, SPS *sps, PPS *pps, int is_sps,
232 uint8_t (*scaling_matrix4)[16], uint8_t (*scaling_matrix8)[64]){
233 int fallback_sps = !is_sps && sps->scaling_matrix_present;
234 const uint8_t *fallback[4] = {
235 fallback_sps ? sps->scaling_matrix4[0] : default_scaling4[0],
236 fallback_sps ? sps->scaling_matrix4[3] : default_scaling4[1],
237 fallback_sps ? sps->scaling_matrix8[0] : default_scaling8[0],
238 fallback_sps ? sps->scaling_matrix8[1] : default_scaling8[1]
239 };
240 if(get_bits1(gb)){
241 sps->scaling_matrix_present |= is_sps;
242 decode_scaling_list(gb, scaling_matrix4[0],16,default_scaling4[0],fallback[0]); // Intra, Y
243 decode_scaling_list(gb, scaling_matrix4[1],16,default_scaling4[0],scaling_matrix4[0]); // Intra, Cr
244 decode_scaling_list(gb, scaling_matrix4[2],16,default_scaling4[0],scaling_matrix4[1]); // Intra, Cb
245 decode_scaling_list(gb, scaling_matrix4[3],16,default_scaling4[1],fallback[1]); // Inter, Y
246 decode_scaling_list(gb, scaling_matrix4[4],16,default_scaling4[1],scaling_matrix4[3]); // Inter, Cr
247 decode_scaling_list(gb, scaling_matrix4[5],16,default_scaling4[1],scaling_matrix4[4]); // Inter, Cb
248 if(is_sps || pps->transform_8x8_mode){
249 decode_scaling_list(gb, scaling_matrix8[0],64,default_scaling8[0],fallback[2]); // Intra, Y
250 decode_scaling_list(gb, scaling_matrix8[1],64,default_scaling8[1],fallback[3]); // Inter, Y
251 }
252 }
253 }
255 int ff_h264_decode_seq_parameter_set(NalContext *n, GetBitContext *gb){
256 int profile_idc, level_idc;
257 unsigned int sps_id;
258 int i;
259 SPS *sps;
261 profile_idc= get_bits(gb, 8);
262 get_bits1(gb); //constraint_set0_flag
263 get_bits1(gb); //constraint_set1_flag
264 get_bits1(gb); //constraint_set2_flag
265 get_bits1(gb); //constraint_set3_flag
266 get_bits(gb, 4); // reserved
267 level_idc= get_bits(gb, 8);
268 sps_id= get_ue_golomb_31(gb);
270 if(sps_id >= MAX_SPS_COUNT) {
271 av_log(AV_LOG_ERROR, "sps_id (%d) out of range\n", sps_id);
272 return -1;
273 }
274 if (!n->sps_buffers[sps_id])
275 n->sps_buffers[sps_id]= av_mallocz(sizeof(SPS));
277 sps = n->sps_buffers[sps_id];
278 if(sps == NULL)
279 return -1;
281 sps->profile_idc= profile_idc;
282 sps->level_idc= level_idc;
284 memset(sps->scaling_matrix4, 16, sizeof(sps->scaling_matrix4));
285 memset(sps->scaling_matrix8, 16, sizeof(sps->scaling_matrix8));
286 sps->scaling_matrix_present = 0;
288 if(sps->profile_idc >= 100){ //high profile
289 sps->chroma_format_idc= get_ue_golomb_31(gb);
290 if(sps->chroma_format_idc == 3)
291 sps->residual_color_transform_flag = get_bits1(gb);
292 sps->bit_depth_luma = get_ue_golomb(gb) + 8;
293 sps->bit_depth_chroma = get_ue_golomb(gb) + 8;
294 sps->transform_bypass = get_bits1(gb);
295 decode_scaling_matrices(gb, sps, NULL, 1, sps->scaling_matrix4, sps->scaling_matrix8);
296 }else{
297 sps->chroma_format_idc= 1;
298 sps->bit_depth_luma = 8;
299 sps->bit_depth_chroma = 8;
300 }
302 sps->log2_max_frame_num= get_ue_golomb(gb) + 4;
303 sps->poc_type= get_ue_golomb_31(gb);
305 if(sps->poc_type == 0){ //FIXME #define
306 sps->log2_max_poc_lsb= get_ue_golomb(gb) + 4;
307 } else if(sps->poc_type == 1){//FIXME #define
308 sps->delta_pic_order_always_zero_flag= get_bits1(gb);
309 sps->offset_for_non_ref_pic= get_se_golomb(gb);
310 sps->offset_for_top_to_bottom_field= get_se_golomb(gb);
311 sps->poc_cycle_length = get_ue_golomb(gb);
313 if((unsigned)sps->poc_cycle_length >= FF_ARRAY_ELEMS(sps->offset_for_ref_frame)){
314 av_log(AV_LOG_ERROR, "poc_cycle_length overflow %u\n", sps->poc_cycle_length);
315 goto fail;
316 }
318 for(i=0; i<sps->poc_cycle_length; i++)
319 sps->offset_for_ref_frame[i]= get_se_golomb(gb);
320 }else if(sps->poc_type != 2){
321 av_log(AV_LOG_ERROR, "illegal POC type %d\n", sps->poc_type);
322 goto fail;
323 }
325 sps->ref_frame_count= get_ue_golomb_31(gb);
326 if(sps->ref_frame_count >= 32){
327 av_log(AV_LOG_ERROR, "too many reference frames\n");
328 goto fail;
329 }
330 sps->gaps_in_frame_num_allowed_flag= get_bits1(gb);
331 sps->mb_width = get_ue_golomb(gb) + 1;
332 sps->mb_height= get_ue_golomb(gb) + 1;
335 sps->frame_mbs_only_flag= get_bits1(gb);
336 if(!sps->frame_mbs_only_flag){
337 av_log(AV_LOG_ERROR, "MBAFF support not included\n");
338 get_bits1(gb);
339 }else
340 sps->mb_aff= 0;
342 sps->direct_8x8_inference_flag= get_bits1(gb);
343 if(!sps->frame_mbs_only_flag && !sps->direct_8x8_inference_flag){
344 av_log(AV_LOG_ERROR, "This stream was generated by a broken encoder, invalid 8x8 inference\n");
345 goto fail;
346 }
348 sps->crop= get_bits1(gb);
349 if(sps->crop){
350 sps->crop_left = get_ue_golomb(gb);
351 sps->crop_right = get_ue_golomb(gb);
352 sps->crop_top = get_ue_golomb(gb);
353 sps->crop_bottom= get_ue_golomb(gb);
354 if(sps->crop_left || sps->crop_top){
355 av_log( AV_LOG_ERROR, "insane cropping not completely supported, this could look slightly wrong ...\n");
356 }
357 if(sps->crop_right >= 8 || sps->crop_bottom >= (8>> !sps->frame_mbs_only_flag)){
358 av_log( AV_LOG_ERROR, "brainfart cropping not supported, this could look slightly wrong ...\n");
359 }
360 }else {
362 sps->crop_left =
363 sps->crop_right =
364 sps->crop_top =
365 sps->crop_bottom= 0;
366 }
368 sps->vui_parameters_present_flag= get_bits1(gb);
369 if( sps->vui_parameters_present_flag )
370 if (decode_vui_parameters(gb, sps) < 0)
371 goto fail;
374 n->sps = *sps;
376 if( sps->bitstream_restriction_flag){
377 n->has_b_frames = sps->num_reorder_frames;
378 }
379 else
380 n->has_b_frames= MAX_DELAYED_PIC_COUNT;
382 return 0;
383 fail:
384 av_free(sps);
385 return -1;
386 }
388 static void
389 build_qp_table(PPS *pps, int t, int index)
390 {
391 int i;
392 for(i = 0; i < 52; i++)
393 pps->chroma_qp_table[t][i] = ff_h264_chroma_qp[av_clip(i + index, 0, 51)];
394 }
396 int ff_h264_decode_picture_parameter_set(NalContext *n, GetBitContext *gb, int bit_length){
397 unsigned int pps_id= get_ue_golomb(gb);
398 PPS *pps;
400 if(pps_id >= MAX_PPS_COUNT) {
401 av_log(AV_LOG_ERROR, "pps_id (%d) out of range\n", pps_id);
402 return -1;
403 }
404 if (!n->pps_buffers[pps_id])
405 n->pps_buffers[pps_id]= av_mallocz(sizeof(PPS));
406 pps = n->pps_buffers[pps_id];
407 if(pps == NULL)
408 return -1;
409 pps->sps_id= get_ue_golomb_31(gb);
410 if((unsigned)pps->sps_id>=MAX_SPS_COUNT || n->sps_buffers[pps->sps_id] == NULL){
411 av_log(AV_LOG_ERROR, "sps_id out of range\n");
412 goto fail;
413 }
415 pps->cabac= get_bits1(gb);
416 pps->pic_order_present= get_bits1(gb);
417 if(pps->pic_order_present){
418 av_log(AV_LOG_ERROR, "no interlaces support\n");
419 }
420 pps->slice_group_count= get_ue_golomb(gb) + 1;
421 if(pps->slice_group_count > 1 ){
422 pps->mb_slice_group_map_type= get_ue_golomb(gb);
423 av_log(AV_LOG_ERROR, "multiple slices not supported\n");
424 }
425 pps->ref_count[0]= get_ue_golomb(gb) + 1;
426 pps->ref_count[1]= get_ue_golomb(gb) + 1;
427 if(pps->ref_count[0]> 32 || pps->ref_count[1]> 32){
428 av_log(AV_LOG_ERROR, "reference overflow (pps)\n");
429 goto fail;
430 }
432 pps->weighted_pred= get_bits1(gb);
433 pps->weighted_bipred_idc= get_bits(gb, 2);
434 pps->init_qp= get_se_golomb(gb) + 26;
435 pps->init_qs= get_se_golomb(gb) + 26;
436 pps->chroma_qp_index_offset[0]= get_se_golomb(gb);
437 pps->deblocking_filter_parameters_present= get_bits1(gb);
438 pps->constrained_intra_pred= get_bits1(gb);
439 pps->redundant_pic_cnt_present = get_bits1(gb);
441 pps->transform_8x8_mode= 0;
442 memcpy(pps->scaling_matrix4, n->sps_buffers[pps->sps_id]->scaling_matrix4, sizeof(pps->scaling_matrix4));
443 memcpy(pps->scaling_matrix8, n->sps_buffers[pps->sps_id]->scaling_matrix8, sizeof(pps->scaling_matrix8));
445 if(get_bits_count(gb) < bit_length){
446 pps->transform_8x8_mode= get_bits1(gb);
447 decode_scaling_matrices(gb, n->sps_buffers[pps->sps_id], pps, 0, pps->scaling_matrix4, pps->scaling_matrix8);
448 pps->chroma_qp_index_offset[1]= get_se_golomb(gb); //second_chroma_qp_index_offset
449 } else {
450 pps->chroma_qp_index_offset[1]= pps->chroma_qp_index_offset[0];
451 }
453 build_qp_table(pps, 0, pps->chroma_qp_index_offset[0]);
454 build_qp_table(pps, 1, pps->chroma_qp_index_offset[1]);
455 if(pps->chroma_qp_index_offset[0] != pps->chroma_qp_index_offset[1])
456 pps->chroma_qp_diff= 1;
458 return 0;
459 fail:
460 av_free(pps);
461 return -1;
462 }