Libav 0.7.1
|
00001 /* 00002 * H.26L/H.264/AVC/JVT/14496-10/... parser 00003 * Copyright (c) 2003 Michael Niedermayer <michaelni@gmx.at> 00004 * 00005 * This file is part of Libav. 00006 * 00007 * Libav is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * Libav is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with Libav; if not, write to the Free Software 00019 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00020 */ 00021 00028 #include "parser.h" 00029 #include "h264data.h" 00030 #include "golomb.h" 00031 00032 #include <assert.h> 00033 00034 00035 static int ff_h264_find_frame_end(H264Context *h, const uint8_t *buf, int buf_size) 00036 { 00037 int i; 00038 uint32_t state; 00039 ParseContext *pc = &(h->s.parse_context); 00040 //printf("first %02X%02X%02X%02X\n", buf[0], buf[1],buf[2],buf[3]); 00041 // mb_addr= pc->mb_addr - 1; 00042 state= pc->state; 00043 if(state>13) 00044 state= 7; 00045 00046 for(i=0; i<buf_size; i++){ 00047 if(state==7){ 00048 #if HAVE_FAST_UNALIGNED 00049 /* we check i<buf_size instead of i+3/7 because its simpler 00050 * and there should be FF_INPUT_BUFFER_PADDING_SIZE bytes at the end 00051 */ 00052 # if HAVE_FAST_64BIT 00053 while(i<buf_size && !((~*(const uint64_t*)(buf+i) & (*(const uint64_t*)(buf+i) - 0x0101010101010101ULL)) & 0x8080808080808080ULL)) 00054 i+=8; 00055 # else 00056 while(i<buf_size && !((~*(const uint32_t*)(buf+i) & (*(const uint32_t*)(buf+i) - 0x01010101U)) & 0x80808080U)) 00057 i+=4; 00058 # endif 00059 #endif 00060 for(; i<buf_size; i++){ 00061 if(!buf[i]){ 00062 state=2; 00063 break; 00064 } 00065 } 00066 }else if(state<=2){ 00067 if(buf[i]==1) state^= 5; //2->7, 1->4, 0->5 00068 else if(buf[i]) state = 7; 00069 else state>>=1; //2->1, 1->0, 0->0 00070 }else if(state<=5){ 00071 int v= buf[i] & 0x1F; 00072 if(v==6 || v==7 || v==8 || v==9){ 00073 if(pc->frame_start_found){ 00074 i++; 00075 goto found; 00076 } 00077 }else if(v==1 || v==2 || v==5){ 00078 if(pc->frame_start_found){ 00079 state+=8; 00080 continue; 00081 }else 00082 pc->frame_start_found = 1; 00083 } 00084 state= 7; 00085 }else{ 00086 if(buf[i] & 0x80) 00087 goto found; 00088 state= 7; 00089 } 00090 } 00091 pc->state= state; 00092 return END_NOT_FOUND; 00093 00094 found: 00095 pc->state=7; 00096 pc->frame_start_found= 0; 00097 return i-(state&5); 00098 } 00099 00108 static inline int parse_nal_units(AVCodecParserContext *s, 00109 AVCodecContext *avctx, 00110 const uint8_t *buf, int buf_size) 00111 { 00112 H264Context *h = s->priv_data; 00113 const uint8_t *buf_end = buf + buf_size; 00114 unsigned int pps_id; 00115 unsigned int slice_type; 00116 int state = -1; 00117 const uint8_t *ptr; 00118 00119 /* set some sane default values */ 00120 s->pict_type = AV_PICTURE_TYPE_I; 00121 s->key_frame = 0; 00122 00123 h->s.avctx= avctx; 00124 h->sei_recovery_frame_cnt = -1; 00125 h->sei_dpb_output_delay = 0; 00126 h->sei_cpb_removal_delay = -1; 00127 h->sei_buffering_period_present = 0; 00128 00129 if (!buf_size) 00130 return 0; 00131 00132 for(;;) { 00133 int src_length, dst_length, consumed; 00134 buf = ff_find_start_code(buf, buf_end, &state); 00135 if(buf >= buf_end) 00136 break; 00137 --buf; 00138 src_length = buf_end - buf; 00139 switch (state & 0x1f) { 00140 case NAL_SLICE: 00141 case NAL_IDR_SLICE: 00142 // Do not walk the whole buffer just to decode slice header 00143 if (src_length > 20) 00144 src_length = 20; 00145 break; 00146 } 00147 ptr= ff_h264_decode_nal(h, buf, &dst_length, &consumed, src_length); 00148 if (ptr==NULL || dst_length < 0) 00149 break; 00150 00151 init_get_bits(&h->s.gb, ptr, 8*dst_length); 00152 switch(h->nal_unit_type) { 00153 case NAL_SPS: 00154 ff_h264_decode_seq_parameter_set(h); 00155 break; 00156 case NAL_PPS: 00157 ff_h264_decode_picture_parameter_set(h, h->s.gb.size_in_bits); 00158 break; 00159 case NAL_SEI: 00160 ff_h264_decode_sei(h); 00161 break; 00162 case NAL_IDR_SLICE: 00163 s->key_frame = 1; 00164 /* fall through */ 00165 case NAL_SLICE: 00166 get_ue_golomb(&h->s.gb); // skip first_mb_in_slice 00167 slice_type = get_ue_golomb_31(&h->s.gb); 00168 s->pict_type = golomb_to_pict_type[slice_type % 5]; 00169 if (h->sei_recovery_frame_cnt >= 0) { 00170 /* key frame, since recovery_frame_cnt is set */ 00171 s->key_frame = 1; 00172 } 00173 pps_id= get_ue_golomb(&h->s.gb); 00174 if(pps_id>=MAX_PPS_COUNT) { 00175 av_log(h->s.avctx, AV_LOG_ERROR, "pps_id out of range\n"); 00176 return -1; 00177 } 00178 if(!h->pps_buffers[pps_id]) { 00179 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing PPS referenced\n"); 00180 return -1; 00181 } 00182 h->pps= *h->pps_buffers[pps_id]; 00183 if(!h->sps_buffers[h->pps.sps_id]) { 00184 av_log(h->s.avctx, AV_LOG_ERROR, "non-existing SPS referenced\n"); 00185 return -1; 00186 } 00187 h->sps = *h->sps_buffers[h->pps.sps_id]; 00188 h->frame_num = get_bits(&h->s.gb, h->sps.log2_max_frame_num); 00189 00190 avctx->profile = ff_h264_get_profile(&h->sps); 00191 avctx->level = h->sps.level_idc; 00192 00193 if(h->sps.frame_mbs_only_flag){ 00194 h->s.picture_structure= PICT_FRAME; 00195 }else{ 00196 if(get_bits1(&h->s.gb)) { //field_pic_flag 00197 h->s.picture_structure= PICT_TOP_FIELD + get_bits1(&h->s.gb); //bottom_field_flag 00198 } else { 00199 h->s.picture_structure= PICT_FRAME; 00200 } 00201 } 00202 00203 if(h->sps.pic_struct_present_flag) { 00204 switch (h->sei_pic_struct) { 00205 case SEI_PIC_STRUCT_TOP_FIELD: 00206 case SEI_PIC_STRUCT_BOTTOM_FIELD: 00207 s->repeat_pict = 0; 00208 break; 00209 case SEI_PIC_STRUCT_FRAME: 00210 case SEI_PIC_STRUCT_TOP_BOTTOM: 00211 case SEI_PIC_STRUCT_BOTTOM_TOP: 00212 s->repeat_pict = 1; 00213 break; 00214 case SEI_PIC_STRUCT_TOP_BOTTOM_TOP: 00215 case SEI_PIC_STRUCT_BOTTOM_TOP_BOTTOM: 00216 s->repeat_pict = 2; 00217 break; 00218 case SEI_PIC_STRUCT_FRAME_DOUBLING: 00219 s->repeat_pict = 3; 00220 break; 00221 case SEI_PIC_STRUCT_FRAME_TRIPLING: 00222 s->repeat_pict = 5; 00223 break; 00224 default: 00225 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0; 00226 break; 00227 } 00228 } else { 00229 s->repeat_pict = h->s.picture_structure == PICT_FRAME ? 1 : 0; 00230 } 00231 00232 return 0; /* no need to evaluate the rest */ 00233 } 00234 buf += consumed; 00235 } 00236 /* didn't find a picture! */ 00237 av_log(h->s.avctx, AV_LOG_ERROR, "missing picture in access unit\n"); 00238 return -1; 00239 } 00240 00241 static int h264_parse(AVCodecParserContext *s, 00242 AVCodecContext *avctx, 00243 const uint8_t **poutbuf, int *poutbuf_size, 00244 const uint8_t *buf, int buf_size) 00245 { 00246 H264Context *h = s->priv_data; 00247 ParseContext *pc = &h->s.parse_context; 00248 int next; 00249 00250 if (!h->got_first) { 00251 h->got_first = 1; 00252 if (avctx->extradata_size) { 00253 h->s.avctx = avctx; 00254 // must be done like in the decoder. 00255 // otherwise opening the parser, creating extradata, 00256 // and then closing and opening again 00257 // will cause has_b_frames to be always set. 00258 // NB: estimate_timings_from_pts behaves exactly like this. 00259 if (!avctx->has_b_frames) 00260 h->s.low_delay = 1; 00261 ff_h264_decode_extradata(h); 00262 } 00263 } 00264 00265 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ 00266 next= buf_size; 00267 }else{ 00268 next= ff_h264_find_frame_end(h, buf, buf_size); 00269 00270 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { 00271 *poutbuf = NULL; 00272 *poutbuf_size = 0; 00273 return buf_size; 00274 } 00275 00276 if(next<0 && next != END_NOT_FOUND){ 00277 assert(pc->last_index + next >= 0 ); 00278 ff_h264_find_frame_end(h, &pc->buffer[pc->last_index + next], -next); //update state 00279 } 00280 } 00281 00282 parse_nal_units(s, avctx, buf, buf_size); 00283 00284 if (h->sei_cpb_removal_delay >= 0) { 00285 s->dts_sync_point = h->sei_buffering_period_present; 00286 s->dts_ref_dts_delta = h->sei_cpb_removal_delay; 00287 s->pts_dts_delta = h->sei_dpb_output_delay; 00288 } else { 00289 s->dts_sync_point = INT_MIN; 00290 s->dts_ref_dts_delta = INT_MIN; 00291 s->pts_dts_delta = INT_MIN; 00292 } 00293 00294 if (s->flags & PARSER_FLAG_ONCE) { 00295 s->flags &= PARSER_FLAG_COMPLETE_FRAMES; 00296 } 00297 00298 *poutbuf = buf; 00299 *poutbuf_size = buf_size; 00300 return next; 00301 } 00302 00303 static int h264_split(AVCodecContext *avctx, 00304 const uint8_t *buf, int buf_size) 00305 { 00306 int i; 00307 uint32_t state = -1; 00308 int has_sps= 0; 00309 00310 for(i=0; i<=buf_size; i++){ 00311 if((state&0xFFFFFF1F) == 0x107) 00312 has_sps=1; 00313 /* if((state&0xFFFFFF1F) == 0x101 || (state&0xFFFFFF1F) == 0x102 || (state&0xFFFFFF1F) == 0x105){ 00314 }*/ 00315 if((state&0xFFFFFF00) == 0x100 && (state&0xFFFFFF1F) != 0x107 && (state&0xFFFFFF1F) != 0x108 && (state&0xFFFFFF1F) != 0x109){ 00316 if(has_sps){ 00317 while(i>4 && buf[i-5]==0) i--; 00318 return i-4; 00319 } 00320 } 00321 if (i<buf_size) 00322 state= (state<<8) | buf[i]; 00323 } 00324 return 0; 00325 } 00326 00327 static void close(AVCodecParserContext *s) 00328 { 00329 H264Context *h = s->priv_data; 00330 ParseContext *pc = &h->s.parse_context; 00331 00332 av_free(pc->buffer); 00333 ff_h264_free_context(h); 00334 } 00335 00336 static int init(AVCodecParserContext *s) 00337 { 00338 H264Context *h = s->priv_data; 00339 h->thread_context[0] = h; 00340 return 0; 00341 } 00342 00343 AVCodecParser ff_h264_parser = { 00344 { CODEC_ID_H264 }, 00345 sizeof(H264Context), 00346 init, 00347 h264_parse, 00348 close, 00349 h264_split, 00350 };