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

libavcodec/parser.c

Go to the documentation of this file.
00001 /*
00002  * Audio and Video frame extraction
00003  * Copyright (c) 2003 Fabrice Bellard
00004  * Copyright (c) 2003 Michael Niedermayer
00005  *
00006  * This file is part of FFmpeg.
00007  *
00008  * FFmpeg is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public
00010  * License as published by the Free Software Foundation; either
00011  * version 2.1 of the License, or (at your option) any later version.
00012  *
00013  * FFmpeg is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with FFmpeg; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00021  */
00022 
00023 #include "parser.h"
00024 
00025 static AVCodecParser *av_first_parser = NULL;
00026 
00027 AVCodecParser* av_parser_next(AVCodecParser *p){
00028     if(p) return p->next;
00029     else  return av_first_parser;
00030 }
00031 
00032 void av_register_codec_parser(AVCodecParser *parser)
00033 {
00034     parser->next = av_first_parser;
00035     av_first_parser = parser;
00036 }
00037 
00038 AVCodecParserContext *av_parser_init(int codec_id)
00039 {
00040     AVCodecParserContext *s;
00041     AVCodecParser *parser;
00042     int ret;
00043 
00044     if(codec_id == CODEC_ID_NONE)
00045         return NULL;
00046 
00047     for(parser = av_first_parser; parser != NULL; parser = parser->next) {
00048         if (parser->codec_ids[0] == codec_id ||
00049             parser->codec_ids[1] == codec_id ||
00050             parser->codec_ids[2] == codec_id ||
00051             parser->codec_ids[3] == codec_id ||
00052             parser->codec_ids[4] == codec_id)
00053             goto found;
00054     }
00055     return NULL;
00056  found:
00057     s = av_mallocz(sizeof(AVCodecParserContext));
00058     if (!s)
00059         return NULL;
00060     s->parser = parser;
00061     s->priv_data = av_mallocz(parser->priv_data_size);
00062     if (!s->priv_data) {
00063         av_free(s);
00064         return NULL;
00065     }
00066     if (parser->parser_init) {
00067         ret = parser->parser_init(s);
00068         if (ret != 0) {
00069             av_free(s->priv_data);
00070             av_free(s);
00071             return NULL;
00072         }
00073     }
00074     s->fetch_timestamp=1;
00075     s->pict_type = FF_I_TYPE;
00076     s->key_frame = -1;
00077     s->convergence_duration = AV_NOPTS_VALUE;
00078     s->dts_sync_point       = INT_MIN;
00079     s->dts_ref_dts_delta    = INT_MIN;
00080     s->pts_dts_delta        = INT_MIN;
00081     return s;
00082 }
00083 
00084 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove){
00085     int i;
00086 
00087     s->dts= s->pts= AV_NOPTS_VALUE;
00088     s->pos= -1;
00089     s->offset= 0;
00090     for(i = 0; i < AV_PARSER_PTS_NB; i++) {
00091         if (   s->cur_offset + off >= s->cur_frame_offset[i]
00092             && (s->frame_offset < s->cur_frame_offset[i] ||
00093               (!s->frame_offset && !s->next_frame_offset)) // first field/frame
00094             //check is disabled  because mpeg-ts doesnt send complete PES packets
00095             && /*s->next_frame_offset + off <*/  s->cur_frame_end[i]){
00096             s->dts= s->cur_frame_dts[i];
00097             s->pts= s->cur_frame_pts[i];
00098             s->pos= s->cur_frame_pos[i];
00099             s->offset = s->next_frame_offset - s->cur_frame_offset[i];
00100             if(remove)
00101                 s->cur_frame_offset[i]= INT64_MAX;
00102             if(s->cur_offset + off < s->cur_frame_end[i])
00103                 break;
00104         }
00105     }
00106 }
00107 
00134 int av_parser_parse(AVCodecParserContext *s,
00135                     AVCodecContext *avctx,
00136                     uint8_t **poutbuf, int *poutbuf_size,
00137                     const uint8_t *buf, int buf_size,
00138                     int64_t pts, int64_t dts)
00139 {
00140     return av_parser_parse2(s, avctx, poutbuf, poutbuf_size, buf, buf_size, pts, dts, AV_NOPTS_VALUE);
00141 }
00142 
00143 int av_parser_parse2(AVCodecParserContext *s,
00144                      AVCodecContext *avctx,
00145                      uint8_t **poutbuf, int *poutbuf_size,
00146                      const uint8_t *buf, int buf_size,
00147                      int64_t pts, int64_t dts,
00148                      int64_t pos)
00149 {
00150     int index, i;
00151     uint8_t dummy_buf[FF_INPUT_BUFFER_PADDING_SIZE];
00152 
00153     if (buf_size == 0) {
00154         /* padding is always necessary even if EOF, so we add it here */
00155         memset(dummy_buf, 0, sizeof(dummy_buf));
00156         buf = dummy_buf;
00157     } else if (s->cur_offset + buf_size !=
00158                s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
00159         /* add a new packet descriptor */
00160             i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
00161             s->cur_frame_start_index = i;
00162             s->cur_frame_offset[i] = s->cur_offset;
00163             s->cur_frame_end[i] = s->cur_offset + buf_size;
00164             s->cur_frame_pts[i] = pts;
00165             s->cur_frame_dts[i] = dts;
00166             s->cur_frame_pos[i] = pos;
00167     }
00168 
00169     if (s->fetch_timestamp){
00170         s->fetch_timestamp=0;
00171         s->last_pts = s->pts;
00172         s->last_dts = s->dts;
00173         s->last_pos = s->pos;
00174         ff_fetch_timestamp(s, 0, 0);
00175     }
00176 
00177     /* WARNING: the returned index can be negative */
00178     index = s->parser->parser_parse(s, avctx, (const uint8_t **)poutbuf, poutbuf_size, buf, buf_size);
00179 //av_log(NULL, AV_LOG_DEBUG, "parser: in:%"PRId64", %"PRId64", out:%"PRId64", %"PRId64", in:%d out:%d id:%d\n", pts, dts, s->last_pts, s->last_dts, buf_size, *poutbuf_size, avctx->codec_id);
00180     /* update the file pointer */
00181     if (*poutbuf_size) {
00182         /* fill the data for the current frame */
00183         s->frame_offset = s->next_frame_offset;
00184 
00185         /* offset of the next frame */
00186         s->next_frame_offset = s->cur_offset + index;
00187         s->fetch_timestamp=1;
00188     }
00189     if (index < 0)
00190         index = 0;
00191     s->cur_offset += index;
00192     return index;
00193 }
00194 
00200 int av_parser_change(AVCodecParserContext *s,
00201                      AVCodecContext *avctx,
00202                      uint8_t **poutbuf, int *poutbuf_size,
00203                      const uint8_t *buf, int buf_size, int keyframe){
00204 
00205     if(s && s->parser->split){
00206         if((avctx->flags & CODEC_FLAG_GLOBAL_HEADER) || (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER)){
00207             int i= s->parser->split(avctx, buf, buf_size);
00208             buf += i;
00209             buf_size -= i;
00210         }
00211     }
00212 
00213     /* cast to avoid warning about discarding qualifiers */
00214     *poutbuf= (uint8_t *) buf;
00215     *poutbuf_size= buf_size;
00216     if(avctx->extradata){
00217         if(  (keyframe && (avctx->flags2 & CODEC_FLAG2_LOCAL_HEADER))
00218             /*||(s->pict_type != FF_I_TYPE && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_NOKEY))*/
00219             /*||(? && (s->flags & PARSER_FLAG_DUMP_EXTRADATA_AT_BEGIN)*/){
00220             int size= buf_size + avctx->extradata_size;
00221             *poutbuf_size= size;
00222             *poutbuf= av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
00223 
00224             memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
00225             memcpy((*poutbuf) + avctx->extradata_size, buf, buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
00226             return 1;
00227         }
00228     }
00229 
00230     return 0;
00231 }
00232 
00233 void av_parser_close(AVCodecParserContext *s)
00234 {
00235     if(s){
00236         if (s->parser->parser_close)
00237             s->parser->parser_close(s);
00238         av_free(s->priv_data);
00239         av_free(s);
00240     }
00241 }
00242 
00243 /*****************************************************/
00244 
00249 int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
00250 {
00251 #if 0
00252     if(pc->overread){
00253         printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00254         printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00255     }
00256 #endif
00257 
00258     /* Copy overread bytes from last frame into buffer. */
00259     for(; pc->overread>0; pc->overread--){
00260         pc->buffer[pc->index++]= pc->buffer[pc->overread_index++];
00261     }
00262 
00263     /* flush remaining if EOF */
00264     if(!*buf_size && next == END_NOT_FOUND){
00265         next= 0;
00266     }
00267 
00268     pc->last_index= pc->index;
00269 
00270     /* copy into buffer end return */
00271     if(next == END_NOT_FOUND){
00272         void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, (*buf_size) + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00273 
00274         if(!new_buffer)
00275             return AVERROR(ENOMEM);
00276         pc->buffer = new_buffer;
00277         memcpy(&pc->buffer[pc->index], *buf, *buf_size);
00278         pc->index += *buf_size;
00279         return -1;
00280     }
00281 
00282     *buf_size=
00283     pc->overread_index= pc->index + next;
00284 
00285     /* append to buffer */
00286     if(pc->index){
00287         void* new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size, next + pc->index + FF_INPUT_BUFFER_PADDING_SIZE);
00288 
00289         if(!new_buffer)
00290             return AVERROR(ENOMEM);
00291         pc->buffer = new_buffer;
00292         memcpy(&pc->buffer[pc->index], *buf, next + FF_INPUT_BUFFER_PADDING_SIZE );
00293         pc->index = 0;
00294         *buf= pc->buffer;
00295     }
00296 
00297     /* store overread bytes */
00298     for(;next < 0; next++){
00299         pc->state = (pc->state<<8) | pc->buffer[pc->last_index + next];
00300         pc->state64 = (pc->state64<<8) | pc->buffer[pc->last_index + next];
00301         pc->overread++;
00302     }
00303 
00304 #if 0
00305     if(pc->overread){
00306         printf("overread %d, state:%X next:%d index:%d o_index:%d\n", pc->overread, pc->state, next, pc->index, pc->overread_index);
00307         printf("%X %X %X %X\n", (*buf)[0], (*buf)[1],(*buf)[2],(*buf)[3]);
00308     }
00309 #endif
00310 
00311     return 0;
00312 }
00313 
00314 void ff_parse_close(AVCodecParserContext *s)
00315 {
00316     ParseContext *pc = s->priv_data;
00317 
00318     av_freep(&pc->buffer);
00319 }
00320 
00321 void ff_parse1_close(AVCodecParserContext *s)
00322 {
00323     ParseContext1 *pc1 = s->priv_data;
00324 
00325     av_free(pc1->pc.buffer);
00326     av_free(pc1->enc);
00327 }
00328 
00329 /*************************/
00330 
00331 int ff_mpeg4video_split(AVCodecContext *avctx,
00332                            const uint8_t *buf, int buf_size)
00333 {
00334     int i;
00335     uint32_t state= -1;
00336 
00337     for(i=0; i<buf_size; i++){
00338         state= (state<<8) | buf[i];
00339         if(state == 0x1B3 || state == 0x1B6)
00340             return i-3;
00341     }
00342     return 0;
00343 }

Generated on Fri Sep 16 2011 17:17:41 for FFmpeg by  doxygen 1.7.1