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 for(;;) {
00131 int src_length, dst_length, consumed;
00132 buf = ff_find_start_code(buf, buf_end, &state);
00133 if(buf >= buf_end)
00134 break;
00135 --buf;
00136 src_length = buf_end - buf;
00137 switch (state & 0x1f) {
00138 case NAL_SLICE:
00139 case NAL_IDR_SLICE:
00140
00141 if (src_length > 20)
00142 src_length = 20;
00143 break;
00144 }
00145 ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length);
00146 if (ptr==NULL || dst_length < 0)
00147 break;
00148
00149 init_get_bits(&h->s.gb, ptr, 8*dst_length);
00150 switch(h->nal_unit_type) {
00151 case NAL_SPS:
00152 ff_h264_decode_seq_parameter_set(h);
00153 break;
00154 case NAL_PPS:
00155 ff_h264_decode_picture_parameter_set(h, h->s.gb.size_in_bits);
00156 break;
00157 case NAL_SEI:
00158 ff_h264_decode_sei(h);
00159 break;
00160 case NAL_IDR_SLICE:
00161 s->key_frame = 1;
00162
00163 case NAL_SLICE:
00164 get_ue_golomb(&h->s.gb);
00165 slice_type = get_ue_golomb_31(&h->s.gb);
00166 s->pict_type = golomb_to_pict_type[slice_type % 5];
00167 if (h->sei_recovery_frame_cnt >= 0) {
00168
00169 s->key_frame = 1;
00170 }
00171 pps_id= get_ue_golomb(&h->s.gb);
00172 if(pps_id>=MAX_PPS_COUNT) {
00173 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n");
00174 return -1;
00175 }
00176 if(!h->pps_buffers[pps_id]) {
00177 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n");
00178 return -1;
00179 }
00180 h->pps= *h->pps_buffers[pps_id];
00181 if(!h->sps_buffers[h->pps.sps_id]) {
00182 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n");
00183 return -1;
00184 }
00185 h->sps = *h->sps_buffers[h->pps.sps_id];
00186 h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num);
00187
00188 avctx->profile = h->sps.profile_idc;
00189 avctx->level = h->sps.level_idc;
00190
00191 if(h->sps.frame_mbs_only_flag){
00192 h->s.picture_structure= PICT_FRAME;
00193 }else{
00194 if(get_bits1(&h->s.gb)) {
00195 h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb);
00196 } else {
00197 h->s.picture_structure= PICT_FRAME;
00198 }
00199 }
00200
00201 if(h->sps.pic_struct_present_flag) {
00202 switch (h->sei_pic_struct) {
00203 case SEI_PIC_STRUCT_TOP_FIELD:
00204 case SEI_PIC_STRUCT_BOTTOM_FIELD:
00205 s->repeat_pict = 0;
00206 break;
00207 case SEI_PIC_STRUCT_FRAME:
00208 case SEI_PIC_STRUCT_TOP_BOTTOM:
00209 case SEI_PIC_STRUCT_BOTTOM_TOP:
00210 s->repeat_pict = 1;
00211 break;
00212 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP:
00213 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM:
00214 s->repeat_pict = 2;
00215 break;
00216 case SEI_PIC_STRUCT_FRAME_DOUBLING:
00217 s->repeat_pict = 3;
00218 break;
00219 case SEI_PIC_STRUCT_FRAME_TRIPLING:
00220 s->repeat_pict = 5;
00221 break;
00222 default:
00223 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
00224 break;
00225 }
00226 } else {
00227 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0;
00228 }
00229
00230 return 0;
00231 }
00232 buf += consumed;
00233 }
00234
00235 av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit\n");
00236 return -1;
00237 }
00238
00239 static int h264_parse(AVCodecParserContext *s,
00240 AVCodecContext *avctx,
00241 const uint8_t **poutbuf, int *poutbuf_size,
00242 const uint8_t *buf, int buf_size)
00243 {
00244 H264Context *h = s->priv_data;
00245 ParseContext *pc = &h->s.parse_context;
00246 int next;
00247
00248 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00249 next= buf_size;
00250 }else{
00251 next= ff_h264_find_frame_end(h, buf, buf_size);
00252
00253 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00254 *poutbuf = NULL;
00255 *poutbuf_size = 0;
00256 return buf_size;
00257 }
00258
00259 if(next<0 && next != END_NOT_FOUND){
00260 assert(pc->last_index + next >= 0 );
00261 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next);
00262 }
00263
00264 parse_nal_units(s, avctx, buf, buf_size);
00265
00266 if (h->sei_cpb_removal_delay >= 0) {
00267 s->dts_sync_point = h->sei_buffering_period_present;
00268 s->dts_ref_dts_delta = h->sei_cpb_removal_delay;
00269 s->pts_dts_delta = h->sei_dpb_output_delay;
00270 } else {
00271 s->dts_sync_point = INT_MIN;
00272 s->dts_ref_dts_delta = INT_MIN;
00273 s->pts_dts_delta = INT_MIN;
00274 }
00275 }
00276
00277 *poutbuf = buf;
00278 *poutbuf_size = buf_size;
00279 return next;
00280 }
00281
00282 static int h264_split(AVCodecContext *avctx,
00283 const uint8_t *buf, int buf_size)
00284 {
00285 int i;
00286 uint32_t state = -1;
00287 int has_sps= 0;
00288
00289 for(i=0; i<=buf_size; i++){
00290 if((state&0xFFFFFF1F) == 0x107)
00291 has_sps=1;
00292
00293
00294 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){
00295 if(has_sps){
00296 while(i>4 && buf[i-5]==0) i--;
00297 return i-4;
00298 }
00299 }
00300 if (i<buf_size)
00301 state= (state<<8) | buf[i];
00302 }
00303 return 0;
00304 }
00305
00306 static void close(AVCodecParserContext *s)
00307 {
00308 H264Context *h = s->priv_data;
00309 ParseContext *pc = &h->s.parse_context;
00310
00311 av_free(pc->buffer);
00312 ff_h264_free_context(h);
00313 }
00314
00315 static int init(AVCodecParserContext *s)
00316 {
00317 H264Context *h = s->priv_data;
00318 h->thread_context[0] = h;
00319 return 0;
00320 }
00321
00322 AVCodecParser h264_parser = {
00323 { CODEC_ID_H264 },
00324 sizeof(H264Context),
00325 init,
00326 h264_parse,
00327 close,
00328 h264_split,
00329 };