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