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

libavcodec/mpeg4video_parser.c

Go to the documentation of this file.
00001 /*
00002  * MPEG4 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 #define UNCHECKED_BITSTREAM_READER 1
00024 
00025 #include "parser.h"
00026 #include "mpegvideo.h"
00027 #include "mpeg4video.h"
00028 #include "mpeg4video_parser.h"
00029 
00030 
00031 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
00032     int vop_found, i;
00033     uint32_t state;
00034 
00035     vop_found= pc->frame_start_found;
00036     state= pc->state;
00037 
00038     i=0;
00039     if(!vop_found){
00040         for(i=0; i<buf_size; i++){
00041             state= (state<<8) | buf[i];
00042             if(state == 0x1B6){
00043                 i++;
00044                 vop_found=1;
00045                 break;
00046             }
00047         }
00048     }
00049 
00050     if(vop_found){
00051         /* EOF considered as end of frame */
00052         if (buf_size == 0)
00053             return 0;
00054         for(; i<buf_size; i++){
00055             state= (state<<8) | buf[i];
00056             if((state&0xFFFFFF00) == 0x100){
00057                 pc->frame_start_found=0;
00058                 pc->state=-1;
00059                 return i-3;
00060             }
00061         }
00062     }
00063     pc->frame_start_found= vop_found;
00064     pc->state= state;
00065     return END_NOT_FOUND;
00066 }
00067 
00068 /* XXX: make it use less memory */
00069 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
00070                                   AVCodecContext *avctx,
00071                                   const uint8_t *buf, int buf_size)
00072 {
00073     ParseContext1 *pc = s1->priv_data;
00074     MpegEncContext *s = pc->enc;
00075     GetBitContext gb1, *gb = &gb1;
00076     int ret;
00077 
00078     s->avctx = avctx;
00079     s->current_picture_ptr = &s->current_picture;
00080 
00081     if (avctx->extradata_size && pc->first_picture){
00082         init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
00083         ret = ff_mpeg4_decode_picture_header(s, gb);
00084     }
00085 
00086     init_get_bits(gb, buf, 8 * buf_size);
00087     ret = ff_mpeg4_decode_picture_header(s, gb);
00088     if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
00089         avcodec_set_dimensions(avctx, s->width, s->height);
00090     }
00091     s1->pict_type= s->pict_type;
00092     pc->first_picture = 0;
00093     return ret;
00094 }
00095 
00096 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
00097 {
00098     ParseContext1 *pc = s->priv_data;
00099 
00100     pc->enc = av_mallocz(sizeof(MpegEncContext));
00101     if (!pc->enc)
00102         return -1;
00103     pc->first_picture = 1;
00104     pc->enc->quant_precision=5;
00105     pc->enc->slice_context_count = 1;
00106     return 0;
00107 }
00108 
00109 static int mpeg4video_parse(AVCodecParserContext *s,
00110                            AVCodecContext *avctx,
00111                            const uint8_t **poutbuf, int *poutbuf_size,
00112                            const uint8_t *buf, int buf_size)
00113 {
00114     ParseContext *pc = s->priv_data;
00115     int next;
00116 
00117     if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00118         next= buf_size;
00119     }else{
00120         next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
00121 
00122         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00123             *poutbuf = NULL;
00124             *poutbuf_size = 0;
00125             return buf_size;
00126         }
00127     }
00128     av_mpeg4_decode_header(s, avctx, buf, buf_size);
00129 
00130     *poutbuf = buf;
00131     *poutbuf_size = buf_size;
00132     return next;
00133 }
00134 
00135 
00136 AVCodecParser ff_mpeg4video_parser = {
00137     .codec_ids      = { CODEC_ID_MPEG4 },
00138     .priv_data_size = sizeof(ParseContext1),
00139     .parser_init    = mpeg4video_parse_init,
00140     .parser_parse   = mpeg4video_parse,
00141     .parser_close   = ff_parse1_close,
00142     .split          = ff_mpeg4video_split,
00143 };
Generated on Fri Feb 1 2013 14:34:39 for FFmpeg by doxygen 1.7.1