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