• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/h264_parser.c

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