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