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 if(e==1 && build==1 && !strncmp(user_data+16, "x264 - core 0000", 16))
00115 h->x264_build = 67;
00116
00117 if(s->avctx->debug & FF_DEBUG_BUGS)
00118 av_log(s->avctx, AV_LOG_DEBUG, "user data:\"%s\"\n", user_data+16);
00119
00120 for(; i<size; i++)
00121 skip_bits(&s->gb, 8);
00122
00123 return 0;
00124 }
00125
00126 static int decode_recovery_point(H264Context *h){
00127 MpegEncContext * const s = &h->s;
00128
00129 h->sei_recovery_frame_cnt = get_ue_golomb(&s->gb);
00130 skip_bits(&s->gb, 4);
00131
00132 return 0;
00133 }
00134
00135 static int decode_buffering_period(H264Context *h){
00136 MpegEncContext * const s = &h->s;
00137 unsigned int sps_id;
00138 int sched_sel_idx;
00139 SPS *sps;
00140
00141 sps_id = get_ue_golomb_31(&s->gb);
00142 if(sps_id > 31 || !h->sps_buffers[sps_id]) {
00143 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS %d referenced in buffering period\n", sps_id);
00144 return -1;
00145 }
00146 sps = h->sps_buffers[sps_id];
00147
00148
00149 if (sps->nal_hrd_parameters_present_flag) {
00150 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
00151 h->initial_cpb_removal_delay[sched_sel_idx] = get_bits_long(&s->gb, sps->initial_cpb_removal_delay_length);
00152 skip_bits(&s->gb, sps->initial_cpb_removal_delay_length);
00153 }
00154 }
00155 if (sps->vcl_hrd_parameters_present_flag) {
00156 for (sched_sel_idx = 0; sched_sel_idx < sps->cpb_cnt; sched_sel_idx++) {
00157 h->initial_cpb_removal_delay[sched_sel_idx] = get_bits_long(&s->gb, sps->initial_cpb_removal_delay_length);
00158 skip_bits(&s->gb, sps->initial_cpb_removal_delay_length);
00159 }
00160 }
00161
00162 h->sei_buffering_period_present = 1;
00163 return 0;
00164 }
00165
00166 int ff_h264_decode_sei(H264Context *h){
00167 MpegEncContext * const s = &h->s;
00168
00169 while (get_bits_left(&s->gb) > 16) {
00170 int size, type;
00171
00172 type=0;
00173 do{
00174 if (get_bits_left(&s->gb) < 8)
00175 return -1;
00176 type+= show_bits(&s->gb, 8);
00177 }while(get_bits(&s->gb, 8) == 255);
00178
00179 size=0;
00180 do{
00181 if (get_bits_left(&s->gb) < 8)
00182 return -1;
00183 size+= show_bits(&s->gb, 8);
00184 }while(get_bits(&s->gb, 8) == 255);
00185
00186 if(s->avctx->debug&FF_DEBUG_STARTCODE)
00187 av_log(h->s.avctx, AV_LOG_DEBUG, "SEI %d len:%d\n", type, size);
00188
00189 switch(type){
00190 case SEI_TYPE_PIC_TIMING:
00191 if(decode_picture_timing(h) < 0)
00192 return -1;
00193 break;
00194 case SEI_TYPE_USER_DATA_UNREGISTERED:
00195 if(decode_unregistered_user_data(h, size) < 0)
00196 return -1;
00197 break;
00198 case SEI_TYPE_RECOVERY_POINT:
00199 if(decode_recovery_point(h) < 0)
00200 return -1;
00201 break;
00202 case SEI_BUFFERING_PERIOD:
00203 if(decode_buffering_period(h) < 0)
00204 return -1;
00205 break;
00206 default:
00207 skip_bits(&s->gb, 8*size);
00208 }
00209
00210
00211 align_get_bits(&s->gb);
00212 }
00213
00214 return 0;
00215 }