comparison h264.c @ 10858:31645cf81fcb libavcodec

Split SEI code off h264.c.
author michael
date Tue, 12 Jan 2010 18:54:40 +0000
parents b20434143fd5
children 762e6bb0ba40
comparison
equal deleted inserted replaced
10857:b20434143fd5 10858:31645cf81fcb
1566 1566
1567 memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t)); 1567 memset(h->pps.scaling_matrix4, 16, 6*16*sizeof(uint8_t));
1568 memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t)); 1568 memset(h->pps.scaling_matrix8, 16, 2*64*sizeof(uint8_t));
1569 } 1569 }
1570 1570
1571 /**
1572 * Reset SEI values at the beginning of the frame.
1573 *
1574 * @param h H.264 context.
1575 */
1576 static void reset_sei(H264Context *h) {
1577 h->sei_recovery_frame_cnt = -1;
1578 h->sei_dpb_output_delay = 0;
1579 h->sei_cpb_removal_delay = -1;
1580 h->sei_buffering_period_present = 0;
1581 }
1582
1583 av_cold int ff_h264_decode_init(AVCodecContext *avctx){ 1571 av_cold int ff_h264_decode_init(AVCodecContext *avctx){
1584 H264Context *h= avctx->priv_data; 1572 H264Context *h= avctx->priv_data;
1585 MpegEncContext * const s = &h->s; 1573 MpegEncContext * const s = &h->s;
1586 1574
1587 MPV_decode_defaults(s); 1575 MPV_decode_defaults(s);
1611 } 1599 }
1612 1600
1613 h->thread_context[0] = h; 1601 h->thread_context[0] = h;
1614 h->outputed_poc = INT_MIN; 1602 h->outputed_poc = INT_MIN;
1615 h->prev_poc_msb= 1<<16; 1603 h->prev_poc_msb= 1<<16;
1616 reset_sei(h); 1604 ff_h264_reset_sei(h);
1617 if(avctx->codec_id == CODEC_ID_H264){ 1605 if(avctx->codec_id == CODEC_ID_H264){
1618 if(avctx->ticks_per_frame == 1){ 1606 if(avctx->ticks_per_frame == 1){
1619 s->avctx->time_base.den *=2; 1607 s->avctx->time_base.den *=2;
1620 } 1608 }
1621 avctx->ticks_per_frame = 2; 1609 avctx->ticks_per_frame = 2;
2557 h->prev_interlaced_frame = 1; 2545 h->prev_interlaced_frame = 1;
2558 idr(h); 2546 idr(h);
2559 if(h->s.current_picture_ptr) 2547 if(h->s.current_picture_ptr)
2560 h->s.current_picture_ptr->reference= 0; 2548 h->s.current_picture_ptr->reference= 0;
2561 h->s.first_field= 0; 2549 h->s.first_field= 0;
2562 reset_sei(h); 2550 ff_h264_reset_sei(h);
2563 ff_mpeg_flush(avctx); 2551 ff_mpeg_flush(avctx);
2564 } 2552 }
2565 2553
2566 /** 2554 /**
2567 * Find a Picture in the short term reference list by frame number. 2555 * Find a Picture in the short term reference list by frame number.
5602 s->mb_x=0; 5590 s->mb_x=0;
5603 ff_draw_horiz_band(s, 16*s->mb_y, 16); 5591 ff_draw_horiz_band(s, 16*s->mb_y, 16);
5604 } 5592 }
5605 #endif 5593 #endif
5606 return -1; //not reached 5594 return -1; //not reached
5607 }
5608
5609 static int decode_picture_timing(H264Context *h){
5610 MpegEncContext * const s = &h->s;
5611 if(h->sps.nal_hrd_parameters_present_flag || h->sps.vcl_hrd_parameters_present_flag){
5612 h->sei_cpb_removal_delay = get_bits(&s->gb, h->sps.cpb_removal_delay_length);
5613 h->sei_dpb_output_delay = get_bits(&s->gb, h->sps.dpb_output_delay_length);
5614 }
5615 if(h->sps.pic_struct_present_flag){
5616 unsigned int i, num_clock_ts;
5617 h->sei_pic_struct = get_bits(&s->gb, 4);
5618 h->sei_ct_type = 0;
5619
5620 if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
5621 return -1;
5622
5623 num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
5624
5625 for (i = 0 ; i < num_clock_ts ; i++){
5626 if(get_bits(&s->gb, 1)){ /* clock_timestamp_flag */
5627 unsigned int full_timestamp_flag;
5628 h->sei_ct_type |= 1<<get_bits(&s->gb, 2);
5629 skip_bits(&s->gb, 1); /* nuit_field_based_flag */
5630 skip_bits(&s->gb, 5); /* counting_type */
5631 full_timestamp_flag = get_bits(&s->gb, 1);
5632 skip_bits(&s->gb, 1); /* discontinuity_flag */
5633 skip_bits(&s->gb, 1); /* cnt_dropped_flag */
5634 skip_bits(&s->gb, 8); /* n_frames */
5635 if(full_timestamp_flag){
5636 skip_bits(&s->gb, 6); /* seconds_value 0..59 */
5637 skip_bits(&s->gb, 6); /* minutes_value 0..59 */
5638 skip_bits(&s->gb, 5); /* hours_value 0..23 */
5639 }else{
5640 if(get_bits(&s->gb, 1)){ /* seconds_flag */
5641 skip_bits(&s->gb, 6); /* seconds_value range 0..59 */
5642 if(get_bits(&s->gb, 1)){ /* minutes_flag */
5643 skip_bits(&s->gb, 6); /* minutes_value 0..59 */
5644 if(get_bits(&s->gb, 1)) /* hours_flag */
5645 skip_bits(&s->gb, 5); /* hours_value 0..23 */
5646 }
5647 }
5648 }
5649 if(h->sps.time_offset_length > 0)
5650 skip_bits(&s->gb, h->sps.time_offset_length); /* time_offset */
5651 }
5652 }
5653
5654 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
5655 av_log(s->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n", h->sei_ct_type, h->sei_pic_struct);
5656 }
5657 return 0;
5658 }
5659
5660 static int decode_unregistered_user_data(H264Context *h, int size){
5661 MpegEncContext * const s = &h->s;
5662 uint8_t user_data[16+256];
5663 int e, build, i;
5664
5665 if(size<16)
5666 return -1;
5667
5668 for(i=0; i<sizeof(user_data)-1 && i<size; i++){
5669 user_data[i]= get_bits(&s->gb, 8);
5670 }
5671
5672 user_data[i]= 0;
5673 e= sscanf(user_data+16, "x264 - core %d"/*%s - H.264/MPEG-4 AVC codec - Copyleft 2005 - http://www.videolan.org/x264.html*/, &build);
5674 if(e==1 && build>=0)
5675 h->x264_build= build;
5676
5677 if(s->avctx->debug & FF_DEBUG_BUGS)
5678 av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
5679
5680 for(; i<size; i++)
5681 skip_bits(&s->gb, 8);
5682
5683 return 0;
5684 }
5685
5686 static int decode_recovery_point(H264Context *h){
5687 MpegEncContext * const s = &h->s;
5688
5689 h->sei_recovery_frame_cnt = get_ue_golomb(&s->gb);
5690 skip_bits(&s->gb, 4); /* 1b exact_match_flag, 1b broken_link_flag, 2b changing_slice_group_idc */
5691
5692 return 0;
5693 }
5694
5695 static int decode_buffering_period(H264Context *h){
5696 MpegEncContext * const s = &h->s;
5697 unsigned int sps_id;
5698 int sched_sel_idx;
5699 SPS *sps;
5700
5701 sps_id = get_ue_golomb_31(&s->gb);
5702 if(sps_id > 31 || !h->sps_buffers[sps_id]) {
5703 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %d referenced in buffering period\n", sps_id);
5704 return -1;
5705 }
5706 sps = h->sps_buffers[sps_id];
5707
5708 // NOTE: This is really so duplicated in the standard... See H.264, D.1.1
5709 if (sps->nal_hrd_parameters_present_flag) {
5710 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
5711 h->initial_cpb_removal_delay[sched_sel_idx] = get_bits(&s->gb, sps->initial_cpb_removal_delay_length);
5712 skip_bits(&s->gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
5713 }
5714 }
5715 if (sps->vcl_hrd_parameters_present_flag) {
5716 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
5717 h->initial_cpb_removal_delay[sched_sel_idx] = get_bits(&s->gb, sps->initial_cpb_removal_delay_length);
5718 skip_bits(&s->gb, sps->initial_cpb_removal_delay_length); // initial_cpb_removal_delay_offset
5719 }
5720 }
5721
5722 h->sei_buffering_period_present = 1;
5723 return 0;
5724 }
5725
5726 int ff_h264_decode_sei(H264Context *h){
5727 MpegEncContext * const s = &h->s;
5728
5729 while(get_bits_count(&s->gb) + 16 < s->gb.size_in_bits){
5730 int size, type;
5731
5732 type=0;
5733 do{
5734 type+= show_bits(&s->gb, 8);
5735 }while(get_bits(&s->gb, 8) == 255);
5736
5737 size=0;
5738 do{
5739 size+= show_bits(&s->gb, 8);
5740 }while(get_bits(&s->gb, 8) == 255);
5741
5742 switch(type){
5743 case SEI_TYPE_PIC_TIMING: // Picture timing SEI
5744 if(decode_picture_timing(h) < 0)
5745 return -1;
5746 break;
5747 case SEI_TYPE_USER_DATA_UNREGISTERED:
5748 if(decode_unregistered_user_data(h, size) < 0)
5749 return -1;
5750 break;
5751 case SEI_TYPE_RECOVERY_POINT:
5752 if(decode_recovery_point(h) < 0)
5753 return -1;
5754 break;
5755 case SEI_BUFFERING_PERIOD:
5756 if(decode_buffering_period(h) < 0)
5757 return -1;
5758 break;
5759 default:
5760 skip_bits(&s->gb, 8*size);
5761 }
5762
5763 //FIXME check bits here
5764 align_get_bits(&s->gb);
5765 }
5766
5767 return 0;
5768 } 5595 }
5769 5596
5770 static inline int decode_hrd_parameters(H264Context *h, SPS *sps){ 5597 static inline int decode_hrd_parameters(H264Context *h, SPS *sps){
5771 MpegEncContext * const s = &h->s; 5598 MpegEncContext * const s = &h->s;
5772 int cpb_count, i; 5599 int cpb_count, i;
6255 #endif 6082 #endif
6256 if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){ 6083 if(!(s->flags2 & CODEC_FLAG2_CHUNKS)){
6257 h->current_slice = 0; 6084 h->current_slice = 0;
6258 if (!s->first_field) 6085 if (!s->first_field)
6259 s->current_picture_ptr= NULL; 6086 s->current_picture_ptr= NULL;
6260 reset_sei(h); 6087 ff_h264_reset_sei(h);
6261 } 6088 }
6262 6089
6263 for(;;){ 6090 for(;;){
6264 int consumed; 6091 int consumed;
6265 int dst_length; 6092 int dst_length;