00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028 #include "parser.h"
00029 #include "h264data.h"
00030 #include "golomb.h"
00031
00032 #include <assert.h>
00033
00034
00035 static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size)
00036 {
00037 int i;
00038 uint32_t state;
00039 ParseContext *pc = &(h->s.parse_context);
00040
00041
00042 state= pc->state;
00043 if(state>13)
00044 state= 7;
00045
00046 for(i=0; i<buf_size; i++){
00047 if(state==7){
00048 #if HAVE_FAST_UNALIGNED
00049
00050
00051
00052 # if HAVE_FAST_64BIT
00053 while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL))
00054 i+=8;
00055 # else
00056 while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U))
00057 i+=4;
00058 # endif
00059 #endif
00060 for(; i<buf_size; i++){
00061 if(!buf[i]){
00062 state=2;
00063 break;
00064 }
00065 }
00066 }else if(state<=2){
00067 if(buf[i]==1) state^= 5;
00068 else if(buf[i]) state = 7;
00069 else state>>=1;
00070 }else if(state<=5){
00071 int v= buf[i] & 0x1F;
00072 if(v==6 || v==7 || v==8 || v==9){
00073 if(pc->frame_start_found){
00074 i++;
00075 goto found;
00076 }
00077 }else if(v==1 || v==2 || v==5){
00078 if(pc->frame_start_found){
00079 state+=8;
00080 continue;
00081 }else
00082 pc->frame_start_found = 1;
00083 }
00084 state= 7;
00085 }else{
00086 if(buf[i] & 0x80)
00087 goto found;
00088 state= 7;
00089 }
00090 }
00091 pc->state= state;
00092 return END_NOT_FOUND;
00093
00094 found:
00095 pc->state=7;
00096 pc->frame_start_found= 0;
00097 return i-(state&5);
00098 }
00099
00108 static inline int parse_nal_units(AVCodecParserContext *s,
00109 AVCodecContext *avctx,
00110 const uint8_t *buf, int buf_size)
00111 {
00112 H264Context *h = s->priv_data;
00113 const uint8_t *buf_end = buf + buf_size;
00114 unsigned int pps_id;
00115 unsigned int slice_type;
00116 int state = -1;
00117 const uint8_t *ptr;
00118
00119
00120 s->pict_type = AV_PICTURE_TYPE_I;
00121 s->key_frame = 0;
00122
00123 h->s.avctx= avctx;
00124 h->sei_recovery_frame_cnt = -1;
00125 h->sei_dpb_output_delay = 0;
00126 h->sei_cpb_removal_delay = -1;
00127 h->sei_buffering_period_present = 0;
00128
00129 if (!buf_size)
00130 return 0;
00131
00132 for(;;) {
00133 int src_length, dst_length, consumed;
00134 buf = ff_find_start_code(buf, buf_end, &state);
00135 if(buf >= buf_end)
00136 break;
00137 --buf;
00138 src_length = buf_end - buf;
00139 switch (state & 0x1f) {
00140 case NAL_SLICE:
00141 case NAL_IDR_SLICE:
00142
00143 if (src_length > 20)
00144 src_length = 20;
00145 break;
00146 }
00147 ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
00148 if (ptr==NULL || dst_length < 0)
00149 break;
00150
00151 init_get_bits(&h->s.gb, ptr, 8*dst_length);
00152 switch(h->nal_unit_type) {
00153 case NAL_SPS:
00154 ff_h264_decode_seq_parameter_set(h);
00155 break;
00156 case NAL_PPS:
00157 ff_h264_decode_picture_parameter_set(h, h->s.gb.size_in_bits);
00158 break;
00159 case NAL_SEI:
00160 ff_h264_decode_sei(h);
00161 break;
00162 case NAL_IDR_SLICE:
00163 s->key_frame = 1;
00164
00165 case NAL_SLICE:
00166 get_ue_golomb(&h->s.gb);
00167 slice_type = get_ue_golomb_31(&h->s.gb);
00168 s->pict_type = golomb_to_pict_type[slice_type % 5];
00169 if (h->sei_recovery_frame_cnt >= 0) {
00170
00171 s->key_frame = 1;
00172 }
00173 pps_id= get_ue_golomb(&h->s.gb);
00174 if(pps_id>=MAX_PPS_COUNT) {
00175 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
00176 return -1;
00177 }
00178 if(!h->pps_buffers[pps_id]) {
00179 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
00180 return -1;
00181 }
00182 h->pps= *h->pps_buffers[pps_id];
00183 if(!h->sps_buffers[h->pps.sps_id]) {
00184 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
00185 return -1;
00186 }
00187 h->sps = *h->sps_buffers[h->pps.sps_id];
00188 h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num);
00189
00190 avctx->profile = ff_h264_get_profile(&h->sps);
00191 avctx->level = h->sps.level_idc;
00192
00193 if(h->sps.frame_mbs_only_flag){
00194 h->s.picture_structure= PICT_FRAME;
00195 }else{
00196 if(get_bits1(&h->s.gb)) {
00197 h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb);
00198 } else {
00199 h->s.picture_structure= PICT_FRAME;
00200 }
00201 }
00202
00203 if(h->sps.pic_struct_present_flag) {
00204 switch (h->sei_pic_struct) {
00205 case SEI_PIC_STRUCT_TOP_FIELD:
00206 case SEI_PIC_STRUCT_BOTTOM_FIELD:
00207 s->repeat_pict = 0;
00208 break;
00209 case SEI_PIC_STRUCT_FRAME:
00210 case SEI_PIC_STRUCT_TOP_BOTTOM:
00211 case SEI_PIC_STRUCT_BOTTOM_TOP:
00212 s->repeat_pict = 1;
00213 break;
00214 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
00215 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
00216 s->repeat_pict = 2;
00217 break;
00218 case SEI_PIC_STRUCT_FRAME_DOUBLING:
00219 s->repeat_pict = 3;
00220 break;
00221 case SEI_PIC_STRUCT_FRAME_TRIPLING:
00222 s->repeat_pict = 5;
00223 break;
00224 default:
00225 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
00226 break;
00227 }
00228 } else {
00229 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
00230 }
00231
00232 return 0;
00233 }
00234 buf += consumed;
00235 }
00236
00237 av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit\n");
00238 return -1;
00239 }
00240
00241 static int h264_parse(AVCodecParserContext *s,
00242 AVCodecContext *avctx,
00243 const uint8_t **poutbuf, int *poutbuf_size,
00244 const uint8_t *buf, int buf_size)
00245 {
00246 H264Context *h = s->priv_data;
00247 ParseContext *pc = &h->s.parse_context;
00248 int next;
00249
00250 if (!h->got_first) {
00251 h->got_first = 1;
00252 if (avctx->extradata_size) {
00253 h->s.avctx = avctx;
00254
00255
00256
00257
00258 if (!avctx->has_b_frames)
00259 h->s.low_delay = 1;
00260 ff_h264_decode_extradata(h, avctx->extradata, avctx->extradata_size);
00261 }
00262 }
00263
00264 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00265 next= buf_size;
00266 }else{
00267 next= ff_h264_find_frame_end(h, buf, buf_size);
00268
00269 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00270 *poutbuf = NULL;
00271 *poutbuf_size = 0;
00272 return buf_size;
00273 }
00274
00275 if(next<0 && next != END_NOT_FOUND){
00276 assert(pc->last_index + next >= 0 );
00277 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next);
00278 }
00279 }
00280
00281 parse_nal_units(s, avctx, buf, buf_size);
00282
00283 if (h->sei_cpb_removal_delay >= 0) {
00284 s->dts_sync_point = h->sei_buffering_period_present;
00285 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
00286 s->pts_dts_delta = h->sei_dpb_output_delay;
00287 } else {
00288 s->dts_sync_point = INT_MIN;
00289 s->dts_ref_dts_delta = INT_MIN;
00290 s->pts_dts_delta = INT_MIN;
00291 }
00292
00293 if (s->flags & PARSER_FLAG_ONCE) {
00294 s->flags &= PARSER_FLAG_COMPLETE_FRAMES;
00295 }
00296
00297 *poutbuf = buf;
00298 *poutbuf_size = buf_size;
00299 return next;
00300 }
00301
00302 static int h264_split(AVCodecContext *avctx,
00303 const uint8_t *buf, int buf_size)
00304 {
00305 int i;
00306 uint32_t state = -1;
00307 int has_sps= 0;
00308
00309 for(i=0; i<=buf_size; i++){
00310 if((state&0xFFFFFF1F) == 0x107)
00311 has_sps=1;
00312
00313
00314 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
00315 if(has_sps){
00316 while(i>4 && buf[i-5]==0) i--;
00317 return i-4;
00318 }
00319 }
00320 if (i<buf_size)
00321 state= (state<<8) | buf[i];
00322 }
00323 return 0;
00324 }
00325
00326 static void close(AVCodecParserContext *s)
00327 {
00328 H264Context *h = s->priv_data;
00329 ParseContext *pc = &h->s.parse_context;
00330
00331 av_free(pc->buffer);
00332 ff_h264_free_context(h);
00333 }
00334
00335 static int init(AVCodecParserContext *s)
00336 {
00337 H264Context *h = s->priv_data;
00338 h->thread_context[0] = h;
00339 return 0;
00340 }
00341
00342 AVCodecParser ff_h264_parser = {
00343 { CODEC_ID_H264 },
00344 sizeof(H264Context),
00345 init,
00346 h264_parse,
00347 close,
00348 h264_split,
00349 };