comparison h264.c @ 2560:bfba825ee300 libavcodec

Set keyframe flag only on IDR-frames (needed for reordering across I-frames). Read vui bitstream restrictions (allows B-frame delay in advance, rather than waiting till we're out of order.) Mark duplicate frames due to delay as duplicates.
author lorenm
date Fri, 11 Mar 2005 02:14:44 +0000
parents 48c1b17233e8
children eb72c01df6ed
comparison
equal deleted inserted replaced
2559:7d8ba28e18d9 2560:bfba825ee300
85 int timing_info_present_flag; 85 int timing_info_present_flag;
86 uint32_t num_units_in_tick; 86 uint32_t num_units_in_tick;
87 uint32_t time_scale; 87 uint32_t time_scale;
88 int fixed_frame_rate_flag; 88 int fixed_frame_rate_flag;
89 short offset_for_ref_frame[256]; //FIXME dyn aloc? 89 short offset_for_ref_frame[256]; //FIXME dyn aloc?
90 int bitstream_restriction_flag;
91 int num_reorder_frames;
90 }SPS; 92 }SPS;
91 93
92 /** 94 /**
93 * Picture parameter set 95 * Picture parameter set
94 */ 96 */
287 Picture *long_ref[16]; 289 Picture *long_ref[16];
288 Picture default_ref_list[2][32]; 290 Picture default_ref_list[2][32];
289 Picture ref_list[2][32]; //FIXME size? 291 Picture ref_list[2][32]; //FIXME size?
290 Picture field_ref_list[2][32]; //FIXME size? 292 Picture field_ref_list[2][32]; //FIXME size?
291 Picture *delayed_pic[16]; //FIXME size? 293 Picture *delayed_pic[16]; //FIXME size?
292 int delayed_output_poc; 294 Picture *delayed_output_pic;
293 295
294 /** 296 /**
295 * memory management control operations buffer. 297 * memory management control operations buffer.
296 */ 298 */
297 MMCO mmco[MAX_MMCO_COUNT]; 299 MMCO mmco[MAX_MMCO_COUNT];
3247 h->implicit_weight[ref0][ref1] = 32; 3249 h->implicit_weight[ref0][ref1] = 32;
3248 } 3250 }
3249 } 3251 }
3250 } 3252 }
3251 3253
3254 static inline void unreference_pic(H264Context *h, Picture *pic){
3255 int i;
3256 pic->reference=0;
3257 if(pic == h->delayed_output_pic)
3258 pic->reference=1;
3259 else{
3260 for(i = 0; h->delayed_pic[i]; i++)
3261 if(pic == h->delayed_pic[i]){
3262 pic->reference=1;
3263 break;
3264 }
3265 }
3266 }
3267
3252 /** 3268 /**
3253 * instantaneous decoder refresh. 3269 * instantaneous decoder refresh.
3254 */ 3270 */
3255 static void idr(H264Context *h){ 3271 static void idr(H264Context *h){
3256 int i,j; 3272 int i;
3257
3258 #define CHECK_DELAY(pic) \
3259 for(j = 0; h->delayed_pic[j]; j++) \
3260 if(pic == h->delayed_pic[j]){ \
3261 pic->reference=1; \
3262 break; \
3263 }
3264 3273
3265 for(i=0; i<16; i++){ 3274 for(i=0; i<16; i++){
3266 if (h->long_ref[i] != NULL) { 3275 if (h->long_ref[i] != NULL) {
3267 h->long_ref[i]->reference=0; 3276 unreference_pic(h, h->long_ref[i]);
3268 CHECK_DELAY(h->long_ref[i]);
3269 h->long_ref[i]= NULL; 3277 h->long_ref[i]= NULL;
3270 } 3278 }
3271 } 3279 }
3272 h->long_ref_count=0; 3280 h->long_ref_count=0;
3273 3281
3274 for(i=0; i<h->short_ref_count; i++){ 3282 for(i=0; i<h->short_ref_count; i++){
3275 h->short_ref[i]->reference=0; 3283 unreference_pic(h, h->short_ref[i]);
3276 CHECK_DELAY(h->short_ref[i]);
3277 h->short_ref[i]= NULL; 3284 h->short_ref[i]= NULL;
3278 } 3285 }
3279 h->short_ref_count=0; 3286 h->short_ref_count=0;
3280 } 3287 }
3281 #undef CHECK_DELAY
3282 3288
3283 /** 3289 /**
3284 * 3290 *
3285 * @return the removed picture or NULL if an error occures 3291 * @return the removed picture or NULL if an error occures
3286 */ 3292 */
3367 3373
3368 switch(mmco[i].opcode){ 3374 switch(mmco[i].opcode){
3369 case MMCO_SHORT2UNUSED: 3375 case MMCO_SHORT2UNUSED:
3370 pic= remove_short(h, mmco[i].short_frame_num); 3376 pic= remove_short(h, mmco[i].short_frame_num);
3371 if(pic==NULL) return -1; 3377 if(pic==NULL) return -1;
3372 pic->reference= 0; 3378 unreference_pic(h, pic);
3373 break; 3379 break;
3374 case MMCO_SHORT2LONG: 3380 case MMCO_SHORT2LONG:
3375 pic= remove_long(h, mmco[i].long_index); 3381 pic= remove_long(h, mmco[i].long_index);
3376 if(pic) pic->reference=0; 3382 if(pic) unreference_pic(h, pic);
3377 3383
3378 h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num); 3384 h->long_ref[ mmco[i].long_index ]= remove_short(h, mmco[i].short_frame_num);
3379 h->long_ref[ mmco[i].long_index ]->long_ref=1; 3385 h->long_ref[ mmco[i].long_index ]->long_ref=1;
3380 h->long_ref_count++; 3386 h->long_ref_count++;
3381 break; 3387 break;
3382 case MMCO_LONG2UNUSED: 3388 case MMCO_LONG2UNUSED:
3383 pic= remove_long(h, mmco[i].long_index); 3389 pic= remove_long(h, mmco[i].long_index);
3384 if(pic==NULL) return -1; 3390 if(pic==NULL) return -1;
3385 pic->reference= 0; 3391 unreference_pic(h, pic);
3386 break; 3392 break;
3387 case MMCO_LONG: 3393 case MMCO_LONG:
3388 pic= remove_long(h, mmco[i].long_index); 3394 pic= remove_long(h, mmco[i].long_index);
3389 if(pic) pic->reference=0; 3395 if(pic) unreference_pic(h, pic);
3390 3396
3391 h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr; 3397 h->long_ref[ mmco[i].long_index ]= s->current_picture_ptr;
3392 h->long_ref[ mmco[i].long_index ]->long_ref=1; 3398 h->long_ref[ mmco[i].long_index ]->long_ref=1;
3393 h->long_ref_count++; 3399 h->long_ref_count++;
3394 3400
3397 case MMCO_SET_MAX_LONG: 3403 case MMCO_SET_MAX_LONG:
3398 assert(mmco[i].long_index <= 16); 3404 assert(mmco[i].long_index <= 16);
3399 // just remove the long term which index is greater than new max 3405 // just remove the long term which index is greater than new max
3400 for(j = mmco[i].long_index; j<16; j++){ 3406 for(j = mmco[i].long_index; j<16; j++){
3401 pic = remove_long(h, j); 3407 pic = remove_long(h, j);
3402 if (pic) pic->reference=0; 3408 if (pic) unreference_pic(h, pic);
3403 } 3409 }
3404 break; 3410 break;
3405 case MMCO_RESET: 3411 case MMCO_RESET:
3406 while(h->short_ref_count){ 3412 while(h->short_ref_count){
3407 pic= remove_short(h, h->short_ref[0]->frame_num); 3413 pic= remove_short(h, h->short_ref[0]->frame_num);
3408 pic->reference=0; 3414 unreference_pic(h, pic);
3409 } 3415 }
3410 for(j = 0; j < 16; j++) { 3416 for(j = 0; j < 16; j++) {
3411 pic= remove_long(h, j); 3417 pic= remove_long(h, j);
3412 if(pic) pic->reference=0; 3418 if(pic) unreference_pic(h, pic);
3413 } 3419 }
3414 break; 3420 break;
3415 default: assert(0); 3421 default: assert(0);
3416 } 3422 }
3417 } 3423 }
3418 3424
3419 if(!current_is_long){ 3425 if(!current_is_long){
3420 pic= remove_short(h, s->current_picture_ptr->frame_num); 3426 pic= remove_short(h, s->current_picture_ptr->frame_num);
3421 if(pic){ 3427 if(pic){
3422 pic->reference=0; 3428 unreference_pic(h, pic);
3423 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n"); 3429 av_log(h->s.avctx, AV_LOG_ERROR, "illegal short term buffer state detected\n");
3424 } 3430 }
3425 3431
3426 if(h->short_ref_count) 3432 if(h->short_ref_count)
3427 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*)); 3433 memmove(&h->short_ref[1], &h->short_ref[0], h->short_ref_count*sizeof(Picture*));
3754 else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE) 3760 else if(h->pps.weighted_bipred_idc==2 && h->slice_type==B_TYPE)
3755 implicit_weight_table(h); 3761 implicit_weight_table(h);
3756 else 3762 else
3757 h->use_weight = 0; 3763 h->use_weight = 0;
3758 3764
3759 if(s->current_picture.reference) 3765 if(s->current_picture.reference == 3)
3760 decode_ref_pic_marking(h); 3766 decode_ref_pic_marking(h);
3761 3767
3762 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac ) 3768 if( h->slice_type != I_TYPE && h->slice_type != SI_TYPE && h->pps.cabac )
3763 h->cabac_init_idc = get_ue_golomb(&s->gb); 3769 h->cabac_init_idc = get_ue_golomb(&s->gb);
3764 3770
5972 } 5978 }
5973 #endif 5979 #endif
5974 return -1; //not reached 5980 return -1; //not reached
5975 } 5981 }
5976 5982
5983 static inline void decode_hrd_parameters(H264Context *h, SPS *sps){
5984 MpegEncContext * const s = &h->s;
5985 int cpb_count, i;
5986 cpb_count = get_ue_golomb(&s->gb) + 1;
5987 get_bits(&s->gb, 4); /* bit_rate_scale */
5988 get_bits(&s->gb, 4); /* cpb_size_scale */
5989 for(i=0; i<cpb_count; i++){
5990 get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
5991 get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
5992 get_bits1(&s->gb); /* cbr_flag */
5993 }
5994 get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
5995 get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */
5996 get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */
5997 get_bits(&s->gb, 5); /* time_offset_length */
5998 }
5999
5977 static inline int decode_vui_parameters(H264Context *h, SPS *sps){ 6000 static inline int decode_vui_parameters(H264Context *h, SPS *sps){
5978 MpegEncContext * const s = &h->s; 6001 MpegEncContext * const s = &h->s;
5979 int aspect_ratio_info_present_flag, aspect_ratio_idc; 6002 int aspect_ratio_info_present_flag, aspect_ratio_idc;
6003 int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
5980 6004
5981 aspect_ratio_info_present_flag= get_bits1(&s->gb); 6005 aspect_ratio_info_present_flag= get_bits1(&s->gb);
5982 6006
5983 if( aspect_ratio_info_present_flag ) { 6007 if( aspect_ratio_info_present_flag ) {
5984 aspect_ratio_idc= get_bits(&s->gb, 8); 6008 aspect_ratio_idc= get_bits(&s->gb, 8);
6021 sps->num_units_in_tick = get_bits_long(&s->gb, 32); 6045 sps->num_units_in_tick = get_bits_long(&s->gb, 32);
6022 sps->time_scale = get_bits_long(&s->gb, 32); 6046 sps->time_scale = get_bits_long(&s->gb, 32);
6023 sps->fixed_frame_rate_flag = get_bits1(&s->gb); 6047 sps->fixed_frame_rate_flag = get_bits1(&s->gb);
6024 } 6048 }
6025 6049
6026 #if 0 6050 nal_hrd_parameters_present_flag = get_bits1(&s->gb);
6027 | nal_hrd_parameters_present_flag |0 |u(1) | 6051 if(nal_hrd_parameters_present_flag)
6028 | if( nal_hrd_parameters_present_flag = = 1) | | | 6052 decode_hrd_parameters(h, sps);
6029 | hrd_parameters( ) | | | 6053 vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
6030 | vcl_hrd_parameters_present_flag |0 |u(1) | 6054 if(vcl_hrd_parameters_present_flag)
6031 | if( vcl_hrd_parameters_present_flag = = 1) | | | 6055 decode_hrd_parameters(h, sps);
6032 | hrd_parameters( ) | | | 6056 if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag)
6033 | if( ( nal_hrd_parameters_present_flag = = 1 | || | | 6057 get_bits1(&s->gb); /* low_delay_hrd_flag */
6034 | | | | 6058 get_bits1(&s->gb); /* pic_struct_present_flag */
6035 |( vcl_hrd_parameters_present_flag = = 1 ) ) | | | 6059
6036 | low_delay_hrd_flag |0 |u(1) | 6060 sps->bitstream_restriction_flag = get_bits1(&s->gb);
6037 | bitstream_restriction_flag |0 |u(1) | 6061 if(sps->bitstream_restriction_flag){
6038 | if( bitstream_restriction_flag ) { |0 |u(1) | 6062 get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
6039 | motion_vectors_over_pic_boundaries_flag |0 |u(1) | 6063 get_ue_golomb(&s->gb); /* max_bytes_per_pic_denom */
6040 | max_bytes_per_pic_denom |0 |ue(v) | 6064 get_ue_golomb(&s->gb); /* max_bits_per_mb_denom */
6041 | max_bits_per_mb_denom |0 |ue(v) | 6065 get_ue_golomb(&s->gb); /* log2_max_mv_length_horizontal */
6042 | log2_max_mv_length_horizontal |0 |ue(v) | 6066 get_ue_golomb(&s->gb); /* log2_max_mv_length_vertical */
6043 | log2_max_mv_length_vertical |0 |ue(v) | 6067 sps->num_reorder_frames = get_ue_golomb(&s->gb);
6044 | num_reorder_frames |0 |ue(v) | 6068 get_ue_golomb(&s->gb); /* max_dec_frame_buffering */
6045 | max_dec_frame_buffering |0 |ue(v) | 6069 }
6046 | } | | | 6070
6047 |} | | |
6048 #endif
6049 return 0; 6071 return 0;
6050 } 6072 }
6051 6073
6052 static inline int decode_seq_parameter_set(H264Context *h){ 6074 static inline int decode_seq_parameter_set(H264Context *h){
6053 MpegEncContext * const s = &h->s; 6075 MpegEncContext * const s = &h->s;
6388 case NAL_FILTER_DATA: 6410 case NAL_FILTER_DATA:
6389 break; 6411 break;
6390 default: 6412 default:
6391 av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type); 6413 av_log(avctx, AV_LOG_ERROR, "Unknown NAL code: %d\n", h->nal_unit_type);
6392 } 6414 }
6393
6394 //FIXME move after where irt is set
6395 s->current_picture.pict_type= s->pict_type;
6396 s->current_picture.key_frame= s->pict_type == I_TYPE && h->nal_unit_type == NAL_IDR_SLICE;
6397 } 6415 }
6398 6416
6399 if(!s->current_picture_ptr) return buf_index; //no frame 6417 if(!s->current_picture_ptr) return buf_index; //no frame
6418
6419 s->current_picture_ptr->pict_type= s->pict_type;
6420 s->current_picture_ptr->key_frame= s->pict_type == I_TYPE && h->nal_unit_type == NAL_IDR_SLICE;
6400 6421
6401 h->prev_frame_num_offset= h->frame_num_offset; 6422 h->prev_frame_num_offset= h->frame_num_offset;
6402 h->prev_frame_num= h->frame_num; 6423 h->prev_frame_num= h->frame_num;
6403 if(s->current_picture_ptr->reference){ 6424 if(s->current_picture_ptr->reference){
6404 h->prev_poc_msb= h->poc_msb; 6425 h->prev_poc_msb= h->poc_msb;
6515 } 6536 }
6516 6537
6517 { 6538 {
6518 //#define DECODE_ORDER 6539 //#define DECODE_ORDER
6519 Picture *out = s->current_picture_ptr; 6540 Picture *out = s->current_picture_ptr;
6541 *data_size = sizeof(AVFrame);
6520 #ifndef DECODE_ORDER 6542 #ifndef DECODE_ORDER
6521 /* Sort B-frames into display order */ 6543 /* Sort B-frames into display order */
6522 Picture *cur = s->current_picture_ptr; 6544 Picture *cur = s->current_picture_ptr;
6545 Picture *prev = h->delayed_output_pic;
6523 int out_idx = 0; 6546 int out_idx = 0;
6524 int pics = 0; 6547 int pics = 0;
6548 int out_of_order;
6549 int cross_idr = 0;
6550 int dropped_frame = 0;
6525 int i; 6551 int i;
6526 out = NULL; 6552
6553 if(h->sps.bitstream_restriction_flag
6554 && s->avctx->has_b_frames < h->sps.num_reorder_frames){
6555 s->avctx->has_b_frames = h->sps.num_reorder_frames;
6556 s->low_delay = 0;
6557 }
6527 6558
6528 while(h->delayed_pic[pics]) pics++; 6559 while(h->delayed_pic[pics]) pics++;
6529 h->delayed_pic[pics++] = cur; 6560 h->delayed_pic[pics++] = cur;
6561 if(cur->reference == 0)
6562 cur->reference = 1;
6563
6564 for(i=0; h->delayed_pic[i]; i++)
6565 if(h->delayed_pic[i]->key_frame)
6566 cross_idr = 1;
6567
6530 out = h->delayed_pic[0]; 6568 out = h->delayed_pic[0];
6531 for(i=0; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++) 6569 for(i=1; h->delayed_pic[i] && !h->delayed_pic[i]->key_frame; i++)
6532 if(!out || h->delayed_pic[i]->poc < out->poc){ 6570 if(h->delayed_pic[i]->poc < out->poc){
6533 out = h->delayed_pic[i]; 6571 out = h->delayed_pic[i];
6534 out_idx = i; 6572 out_idx = i;
6535 } 6573 }
6536 if(cur->reference == 0) 6574
6537 cur->reference = 1; 6575 out_of_order = !cross_idr && prev && out->poc < prev->poc;
6538 for(i=0; h->delayed_pic[i]; i++) 6576 if(prev && pics <= s->avctx->has_b_frames)
6539 if(h->delayed_pic[i]->key_frame) 6577 out = prev;
6540 h->delayed_output_poc = -1; 6578 else if((out_of_order && pics-1 == s->avctx->has_b_frames)
6541 if(pics > FFMAX(1, s->avctx->has_b_frames)){ 6579 || (s->low_delay &&
6542 if(out->reference == 1) 6580 ((!cross_idr && prev && out->poc > prev->poc + 2)
6543 out->reference = 0; 6581 || cur->pict_type == B_TYPE)))
6582 {
6583 s->low_delay = 0;
6584 s->avctx->has_b_frames++;
6585 out = prev;
6586 }
6587 else if(out_of_order)
6588 out = prev;
6589
6590 if(out_of_order || pics > s->avctx->has_b_frames){
6591 dropped_frame = (out != h->delayed_pic[out_idx]);
6544 for(i=out_idx; h->delayed_pic[i]; i++) 6592 for(i=out_idx; h->delayed_pic[i]; i++)
6545 h->delayed_pic[i] = h->delayed_pic[i+1]; 6593 h->delayed_pic[i] = h->delayed_pic[i+1];
6546 } 6594 }
6547 6595
6548 if((h->delayed_output_poc >=0 && h->delayed_output_poc > cur->poc) 6596 if(prev == out && !dropped_frame)
6549 || (s->low_delay && (cur->pict_type == B_TYPE 6597 *data_size = 0;
6550 || (!h->sps.gaps_in_frame_num_allowed_flag 6598 if(prev && prev != out && prev->reference == 1)
6551 && cur->poc - out->poc > 2)))){ 6599 prev->reference = 0;
6552 s->low_delay = 0; 6600 h->delayed_output_pic = out;
6553 s->avctx->has_b_frames++;
6554 }
6555
6556 h->delayed_output_poc = out->poc;
6557 #endif 6601 #endif
6558 6602
6559 *pict= *(AVFrame*)out; 6603 *pict= *(AVFrame*)out;
6560 } 6604 }
6561 6605
6606 assert(pict->data[0]);
6562 ff_print_debug_info(s, pict); 6607 ff_print_debug_info(s, pict);
6563 assert(pict->data[0]);
6564 //printf("out %d\n", (int)pict->data[0]); 6608 //printf("out %d\n", (int)pict->data[0]);
6565 #if 0 //? 6609 #if 0 //?
6566 6610
6567 /* Return the Picture timestamp as the frame number */ 6611 /* Return the Picture timestamp as the frame number */
6568 /* we substract 1 because it is added on utils.c */ 6612 /* we substract 1 because it is added on utils.c */
6569 avctx->frame_number = s->picture_number - 1; 6613 avctx->frame_number = s->picture_number - 1;
6570 #endif 6614 #endif
6571 #if 0
6572 /* dont output the last pic after seeking */
6573 if(s->last_picture_ptr || s->low_delay)
6574 //Note this isnt a issue as a IDR pic should flush the buffers
6575 #endif
6576 *data_size = sizeof(AVFrame);
6577 return get_consumed_bytes(s, buf_index, buf_size); 6615 return get_consumed_bytes(s, buf_index, buf_size);
6578 } 6616 }
6579 #if 0 6617 #if 0
6580 static inline void fill_mb_avail(H264Context *h){ 6618 static inline void fill_mb_avail(H264Context *h){
6581 MpegEncContext * const s = &h->s; 6619 MpegEncContext * const s = &h->s;