00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "internal.h"
00029 #include "avcodec.h"
00030 #include "h264.h"
00031 #include "golomb.h"
00032
00033
00034 #include <assert.h>
00035
00036 static const uint8_t sei_num_clock_ts_table[9]={
00037 1, 1, 1, 2, 2, 3, 3, 2, 3
00038 };
00039
00040 void ff_h264_reset_sei(H264Context *h) {
00041 h->sei_recovery_frame_cnt = -1;
00042 h->sei_dpb_output_delay = 0;
00043 h->sei_cpb_removal_delay = -1;
00044 h->sei_buffering_period_present = 0;
00045 }
00046
00047 static int decode_picture_timing(H264Context *h){
00048 MpegEncContext * const s = &h->s;
00049 if(h->sps.nal_hrd_parameters_present_flag || h->sps.vcl_hrd_parameters_present_flag){
00050 h->sei_cpb_removal_delay = get_bits_long(&s->gb, h->sps.cpb_removal_delay_length);
00051 h->sei_dpb_output_delay = get_bits_long(&s->gb, h->sps.dpb_output_delay_length);
00052 }
00053 if(h->sps.pic_struct_present_flag){
00054 unsigned int i, num_clock_ts;
00055 h->sei_pic_struct = get_bits(&s->gb, 4);
00056 h->sei_ct_type = 0;
00057
00058 if (h->sei_pic_struct > SEI_PIC_STRUCT_FRAME_TRIPLING)
00059 return -1;
00060
00061 num_clock_ts = sei_num_clock_ts_table[h->sei_pic_struct];
00062
00063 for (i = 0 ; i < num_clock_ts ; i++){
00064 if(get_bits(&s->gb, 1)){
00065 unsigned int full_timestamp_flag;
00066 h->sei_ct_type |= 1<<get_bits(&s->gb, 2);
00067 skip_bits(&s->gb, 1);
00068 skip_bits(&s->gb, 5);
00069 full_timestamp_flag = get_bits(&s->gb, 1);
00070 skip_bits(&s->gb, 1);
00071 skip_bits(&s->gb, 1);
00072 skip_bits(&s->gb, 8);
00073 if(full_timestamp_flag){
00074 skip_bits(&s->gb, 6);
00075 skip_bits(&s->gb, 6);
00076 skip_bits(&s->gb, 5);
00077 }else{
00078 if(get_bits(&s->gb, 1)){
00079 skip_bits(&s->gb, 6);
00080 if(get_bits(&s->gb, 1)){
00081 skip_bits(&s->gb, 6);
00082 if(get_bits(&s->gb, 1))
00083 skip_bits(&s->gb, 5);
00084 }
00085 }
00086 }
00087 if(h->sps.time_offset_length > 0)
00088 skip_bits(&s->gb, h->sps.time_offset_length);
00089 }
00090 }
00091
00092 if(s->avctx->debug & FF_DEBUG_PICT_INFO)
00093 av_log(s->avctx, AV_LOG_DEBUG, "ct_type:%X pic_struct:%d\n", h->sei_ct_type, h->sei_pic_struct);
00094 }
00095 return 0;
00096 }
00097
00098 static int decode_unregistered_user_data(H264Context *h, int size){
00099 MpegEncContext * const s = &h->s;
00100 uint8_t user_data[16+256];
00101 int e, build, i;
00102
00103 if(size<16)
00104 return -1;
00105
00106 for(i=0; i<sizeof(user_data)-1 && i<size; i++){
00107 user_data[i]= get_bits(&s->gb, 8);
00108 }
00109
00110 user_data[i]= 0;
00111 e= sscanf(user_data+16, "x264 - core %d", &build);
00112 if(e==1 && build>0)
00113 h->x264_build= build;
00114
00115 if(s->avctx->debug & FF_DEBUG_BUGS)
00116 av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
00117
00118 for(; i<size; i++)
00119 skip_bits(&s->gb, 8);
00120
00121 return 0;
00122 }
00123
00124 static int decode_recovery_point(H264Context *h){
00125 MpegEncContext * const s = &h->s;
00126
00127 h->sei_recovery_frame_cnt = get_ue_golomb(&s->gb);
00128 skip_bits(&s->gb, 4);
00129
00130 return 0;
00131 }
00132
00133 static int decode_buffering_period(H264Context *h){
00134 MpegEncContext * const s = &h->s;
00135 unsigned int sps_id;
00136 int sched_sel_idx;
00137 SPS *sps;
00138
00139 sps_id = get_ue_golomb_31(&s->gb);
00140 if(sps_id > 31 || !h->sps_buffers[sps_id]) {
00141 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %d referenced in buffering period\n", sps_id);
00142 return -1;
00143 }
00144 sps = h->sps_buffers[sps_id];
00145
00146
00147 if (sps->nal_hrd_parameters_present_flag) {
00148 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
00149 h->initial_cpb_removal_delay[sched_sel_idx] = get_bits_long(&s->gb, sps->initial_cpb_removal_delay_length);
00150 skip_bits(&s->gb, sps->initial_cpb_removal_delay_length);
00151 }
00152 }
00153 if (sps->vcl_hrd_parameters_present_flag) {
00154 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
00155 h->initial_cpb_removal_delay[sched_sel_idx] = get_bits_long(&s->gb, sps->initial_cpb_removal_delay_length);
00156 skip_bits(&s->gb, sps->initial_cpb_removal_delay_length);
00157 }
00158 }
00159
00160 h->sei_buffering_period_present = 1;
00161 return 0;
00162 }
00163
00164 int ff_h264_decode_sei(H264Context *h){
00165 MpegEncContext * const s = &h->s;
00166
00167 while (get_bits_left(&s->gb) > 16) {
00168 int size, type;
00169
00170 type=0;
00171 do{
00172 if (get_bits_left(&s->gb) < 8)
00173 return -1;
00174 type+= show_bits(&s->gb, 8);
00175 }while(get_bits(&s->gb, 8) == 255);
00176
00177 size=0;
00178 do{
00179 if (get_bits_left(&s->gb) < 8)
00180 return -1;
00181 size+= show_bits(&s->gb, 8);
00182 }while(get_bits(&s->gb, 8) == 255);
00183
00184 if(s->avctx->debug&FF_DEBUG_STARTCODE)
00185 av_log(h->s.avctx, AV_LOG_DEBUG, "SEI %d len:%d\n", type, size);
00186
00187 switch(type){
00188 case SEI_TYPE_PIC_TIMING:
00189 if(decode_picture_timing(h) < 0)
00190 return -1;
00191 break;
00192 case SEI_TYPE_USER_DATA_UNREGISTERED:
00193 if(decode_unregistered_user_data(h, size) < 0)
00194 return -1;
00195 break;
00196 case SEI_TYPE_RECOVERY_POINT:
00197 if(decode_recovery_point(h) < 0)
00198 return -1;
00199 break;
00200 case SEI_BUFFERING_PERIOD:
00201 if(decode_buffering_period(h) < 0)
00202 return -1;
00203 break;
00204 default:
00205 skip_bits(&s->gb, 8*size);
00206 }
00207
00208
00209 align_get_bits(&s->gb);
00210 }
00211
00212 return 0;
00213 }