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