comparison h264.c @ 8107:e61f76efc9f3 libavcodec

h264: Implement decoding of picture timing SEI message. Now correct values are propagated to interlaced_frame, top_field_first and repeat_pict in AVFrame structure. patch by ffdshow tryouts
author andoma
date Tue, 04 Nov 2008 18:31:48 +0000
parents 2d3c7cd7d143
children a9734fe0811e
comparison
equal deleted inserted replaced
8106:2f5101a67500 8107:e61f76efc9f3
6801 } 6801 }
6802 #endif 6802 #endif
6803 return -1; //not reached 6803 return -1; //not reached
6804 } 6804 }
6805 6805
6806 static int decode_picture_timing(H264Context *h){
6807 MpegEncContext * const s = &h->s;
6808 if(h->sps.nal_hrd_parameters_present_flag || h->sps.vcl_hrd_parameters_present_flag){
6809 skip_bits(&s->gb, h->sps.cpb_removal_delay_length); /* cpb_removal_delay */
6810 skip_bits(&s->gb, h->sps.dpb_output_delay_length); /* dpb_output_delay */
6811 }
6812 if(h->sps.pic_struct_present_flag){
6813 unsigned int i, num_clock_ts;
6814 h->sei_pic_struct = get_bits(&s->gb, 4);
6815
6816 if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
6817 return -1;
6818
6819 num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
6820
6821 for (i = 0 ; i < num_clock_ts ; i++){
6822 if(get_bits(&s->gb, 1)){ /* clock_timestamp_flag */
6823 unsigned int full_timestamp_flag;
6824 skip_bits(&s->gb, 2); /* ct_type */
6825 skip_bits(&s->gb, 1); /* nuit_field_based_flag */
6826 skip_bits(&s->gb, 5); /* counting_type */
6827 full_timestamp_flag = get_bits(&s->gb, 1);
6828 skip_bits(&s->gb, 1); /* discontinuity_flag */
6829 skip_bits(&s->gb, 1); /* cnt_dropped_flag */
6830 skip_bits(&s->gb, 8); /* n_frames */
6831 if(full_timestamp_flag){
6832 skip_bits(&s->gb, 6); /* seconds_value 0..59 */
6833 skip_bits(&s->gb, 6); /* minutes_value 0..59 */
6834 skip_bits(&s->gb, 5); /* hours_value 0..23 */
6835 }else{
6836 if(get_bits(&s->gb, 1)){ /* seconds_flag */
6837 skip_bits(&s->gb, 6); /* seconds_value range 0..59 */
6838 if(get_bits(&s->gb, 1)){ /* minutes_flag */
6839 skip_bits(&s->gb, 6); /* minutes_value 0..59 */
6840 if(get_bits(&s->gb, 1)) /* hours_flag */
6841 skip_bits(&s->gb, 5); /* hours_value 0..23 */
6842 }
6843 }
6844 }
6845 if(h->sps.time_offset_length > 0)
6846 skip_bits(&s->gb, h->sps.time_offset_length); /* time_offset */
6847 }
6848 }
6849 }
6850 return 0;
6851 }
6852
6806 static int decode_unregistered_user_data(H264Context *h, int size){ 6853 static int decode_unregistered_user_data(H264Context *h, int size){
6807 MpegEncContext * const s = &h->s; 6854 MpegEncContext * const s = &h->s;
6808 uint8_t user_data[16+256]; 6855 uint8_t user_data[16+256];
6809 int e, build, i; 6856 int e, build, i;
6810 6857
6844 do{ 6891 do{
6845 size+= show_bits(&s->gb, 8); 6892 size+= show_bits(&s->gb, 8);
6846 }while(get_bits(&s->gb, 8) == 255); 6893 }while(get_bits(&s->gb, 8) == 255);
6847 6894
6848 switch(type){ 6895 switch(type){
6896 case 1: // Picture timing SEI
6897 if(decode_picture_timing(h) < 0)
6898 return -1;
6899 break;
6849 case 5: 6900 case 5:
6850 if(decode_unregistered_user_data(h, size) < 0) 6901 if(decode_unregistered_user_data(h, size) < 0)
6851 return -1; 6902 return -1;
6852 break; 6903 break;
6853 default: 6904 default:
6871 get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */ 6922 get_ue_golomb(&s->gb); /* bit_rate_value_minus1 */
6872 get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */ 6923 get_ue_golomb(&s->gb); /* cpb_size_value_minus1 */
6873 get_bits1(&s->gb); /* cbr_flag */ 6924 get_bits1(&s->gb); /* cbr_flag */
6874 } 6925 }
6875 get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */ 6926 get_bits(&s->gb, 5); /* initial_cpb_removal_delay_length_minus1 */
6876 get_bits(&s->gb, 5); /* cpb_removal_delay_length_minus1 */ 6927 sps->cpb_removal_delay_length = get_bits(&s->gb, 5) + 1;
6877 get_bits(&s->gb, 5); /* dpb_output_delay_length_minus1 */ 6928 sps->dpb_output_delay_length = get_bits(&s->gb, 5) + 1;
6878 get_bits(&s->gb, 5); /* time_offset_length */ 6929 sps->time_offset_length = get_bits(&s->gb, 5);
6879 } 6930 }
6880 6931
6881 static inline int decode_vui_parameters(H264Context *h, SPS *sps){ 6932 static inline int decode_vui_parameters(H264Context *h, SPS *sps){
6882 MpegEncContext * const s = &h->s; 6933 MpegEncContext * const s = &h->s;
6883 int aspect_ratio_info_present_flag; 6934 int aspect_ratio_info_present_flag;
6884 unsigned int aspect_ratio_idc; 6935 unsigned int aspect_ratio_idc;
6885 int nal_hrd_parameters_present_flag, vcl_hrd_parameters_present_flag;
6886 6936
6887 aspect_ratio_info_present_flag= get_bits1(&s->gb); 6937 aspect_ratio_info_present_flag= get_bits1(&s->gb);
6888 6938
6889 if( aspect_ratio_info_present_flag ) { 6939 if( aspect_ratio_info_present_flag ) {
6890 aspect_ratio_idc= get_bits(&s->gb, 8); 6940 aspect_ratio_idc= get_bits(&s->gb, 8);
6927 sps->num_units_in_tick = get_bits_long(&s->gb, 32); 6977 sps->num_units_in_tick = get_bits_long(&s->gb, 32);
6928 sps->time_scale = get_bits_long(&s->gb, 32); 6978 sps->time_scale = get_bits_long(&s->gb, 32);
6929 sps->fixed_frame_rate_flag = get_bits1(&s->gb); 6979 sps->fixed_frame_rate_flag = get_bits1(&s->gb);
6930 } 6980 }
6931 6981
6932 nal_hrd_parameters_present_flag = get_bits1(&s->gb); 6982 sps->nal_hrd_parameters_present_flag = get_bits1(&s->gb);
6933 if(nal_hrd_parameters_present_flag) 6983 if(sps->nal_hrd_parameters_present_flag)
6934 decode_hrd_parameters(h, sps); 6984 decode_hrd_parameters(h, sps);
6935 vcl_hrd_parameters_present_flag = get_bits1(&s->gb); 6985 sps->vcl_hrd_parameters_present_flag = get_bits1(&s->gb);
6936 if(vcl_hrd_parameters_present_flag) 6986 if(sps->vcl_hrd_parameters_present_flag)
6937 decode_hrd_parameters(h, sps); 6987 decode_hrd_parameters(h, sps);
6938 if(nal_hrd_parameters_present_flag || vcl_hrd_parameters_present_flag) 6988 if(sps->nal_hrd_parameters_present_flag || sps->vcl_hrd_parameters_present_flag)
6939 get_bits1(&s->gb); /* low_delay_hrd_flag */ 6989 get_bits1(&s->gb); /* low_delay_hrd_flag */
6940 get_bits1(&s->gb); /* pic_struct_present_flag */ 6990 sps->pic_struct_present_flag = get_bits1(&s->gb);
6941 6991
6942 sps->bitstream_restriction_flag = get_bits1(&s->gb); 6992 sps->bitstream_restriction_flag = get_bits1(&s->gb);
6943 if(sps->bitstream_restriction_flag){ 6993 if(sps->bitstream_restriction_flag){
6944 unsigned int num_reorder_frames; 6994 unsigned int num_reorder_frames;
6945 get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */ 6995 get_bits1(&s->gb); /* motion_vectors_over_pic_boundaries_flag */
7637 if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) { 7687 if (cur->field_poc[0]==INT_MAX || cur->field_poc[1]==INT_MAX) {
7638 /* Wait for second field. */ 7688 /* Wait for second field. */
7639 *data_size = 0; 7689 *data_size = 0;
7640 7690
7641 } else { 7691 } else {
7642 cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE; 7692 cur->repeat_pict = 0;
7643 /* Derive top_field_first from field pocs. */ 7693
7644 cur->top_field_first = cur->field_poc[0] < cur->field_poc[1]; 7694 /* Signal interlacing information externally. */
7695 /* Prioritize picture timing SEI information over used decoding process if it exists. */
7696 if(h->sps.pic_struct_present_flag){
7697 switch (h->sei_pic_struct)
7698 {
7699 case SEI_PIC_STRUCT_FRAME:
7700 cur->interlaced_frame = 0;
7701 break;
7702 case SEI_PIC_STRUCT_TOP_FIELD:
7703 case SEI_PIC_STRUCT_BOTTOM_FIELD:
7704 case SEI_PIC_STRUCT_TOP_BOTTOM:
7705 case SEI_PIC_STRUCT_BOTTOM_TOP:
7706 cur->interlaced_frame = 1;
7707 break;
7708 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
7709 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
7710 // Signal the possibility of telecined film externally (pic_struct 5,6)
7711 // From these hints, let the applications decide if they apply deinterlacing.
7712 cur->repeat_pict = 1;
7713 cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
7714 break;
7715 case SEI_PIC_STRUCT_FRAME_DOUBLING:
7716 // Force progressive here, as doubling interlaced frame is a bad idea.
7717 cur->interlaced_frame = 0;
7718 cur->repeat_pict = 2;
7719 break;
7720 case SEI_PIC_STRUCT_FRAME_TRIPLING:
7721 cur->interlaced_frame = 0;
7722 cur->repeat_pict = 4;
7723 break;
7724 }
7725 }else{
7726 /* Derive interlacing flag from used decoding process. */
7727 cur->interlaced_frame = FIELD_OR_MBAFF_PICTURE;
7728 }
7729
7730 if (cur->field_poc[0] != cur->field_poc[1]){
7731 /* Derive top_field_first from field pocs. */
7732 cur->top_field_first = cur->field_poc[0] < cur->field_poc[1];
7733 }else{
7734 if(cur->interlaced_frame || h->sps.pic_struct_present_flag){
7735 /* Use picture timing SEI information. Even if it is a information of a past frame, better than nothing. */
7736 if(h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM
7737 || h->sei_pic_struct == SEI_PIC_STRUCT_TOP_BOTTOM_TOP)
7738 cur->top_field_first = 1;
7739 else
7740 cur->top_field_first = 0;
7741 }else{
7742 /* Most likely progressive */
7743 cur->top_field_first = 0;
7744 }
7745 }
7645 7746
7646 //FIXME do something with unavailable reference frames 7747 //FIXME do something with unavailable reference frames
7647 7748
7648 /* Sort B-frames into display order */ 7749 /* Sort B-frames into display order */
7649 7750