00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
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 struct Mp4vParseContext {
00031     ParseContext pc;
00032     struct MpegEncContext enc;
00033     int first_picture;
00034 };
00035 
00036 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
00037     int vop_found, i;
00038     uint32_t state;
00039 
00040     vop_found= pc->frame_start_found;
00041     state= pc->state;
00042 
00043     i=0;
00044     if(!vop_found){
00045         for(i=0; i<buf_size; i++){
00046             state= (state<<8) | buf[i];
00047             if(state == 0x1B6){
00048                 i++;
00049                 vop_found=1;
00050                 break;
00051             }
00052         }
00053     }
00054 
00055     if(vop_found){
00056         
00057         if (buf_size == 0)
00058             return 0;
00059         for(; i<buf_size; i++){
00060             state= (state<<8) | buf[i];
00061             if((state&0xFFFFFF00) == 0x100){
00062                 pc->frame_start_found=0;
00063                 pc->state=-1;
00064                 return i-3;
00065             }
00066         }
00067     }
00068     pc->frame_start_found= vop_found;
00069     pc->state= state;
00070     return END_NOT_FOUND;
00071 }
00072 
00073 
00074 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
00075                                   AVCodecContext *avctx,
00076                                   const uint8_t *buf, int buf_size)
00077 {
00078     struct Mp4vParseContext *pc = s1->priv_data;
00079     MpegEncContext *s = &pc->enc;
00080     GetBitContext gb1, *gb = &gb1;
00081     int ret;
00082 
00083     s->avctx = avctx;
00084     s->current_picture_ptr = &s->current_picture;
00085 
00086     if (avctx->extradata_size && pc->first_picture){
00087         init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
00088         ret = ff_mpeg4_decode_picture_header(s, gb);
00089     }
00090 
00091     init_get_bits(gb, buf, 8 * buf_size);
00092     ret = ff_mpeg4_decode_picture_header(s, gb);
00093     if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
00094         avcodec_set_dimensions(avctx, s->width, s->height);
00095     }
00096     if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
00097         av_assert1(s1->pts == AV_NOPTS_VALUE);
00098         av_assert1(s1->dts == AV_NOPTS_VALUE);
00099 
00100         s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
00101     }
00102 
00103     s1->pict_type= s->pict_type;
00104     pc->first_picture = 0;
00105     return ret;
00106 }
00107 
00108 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
00109 {
00110     struct Mp4vParseContext *pc = s->priv_data;
00111 
00112     pc->first_picture = 1;
00113     pc->enc.quant_precision=5;
00114     pc->enc.slice_context_count = 1;
00115     return 0;
00116 }
00117 
00118 static int mpeg4video_parse(AVCodecParserContext *s,
00119                            AVCodecContext *avctx,
00120                            const uint8_t **poutbuf, int *poutbuf_size,
00121                            const uint8_t *buf, int buf_size)
00122 {
00123     ParseContext *pc = s->priv_data;
00124     int next;
00125 
00126     if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00127         next= buf_size;
00128     }else{
00129         next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
00130 
00131         if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00132             *poutbuf = NULL;
00133             *poutbuf_size = 0;
00134             return buf_size;
00135         }
00136     }
00137     av_mpeg4_decode_header(s, avctx, buf, buf_size);
00138 
00139     *poutbuf = buf;
00140     *poutbuf_size = buf_size;
00141     return next;
00142 }
00143 
00144 
00145 AVCodecParser ff_mpeg4video_parser = {
00146     .codec_ids      = { AV_CODEC_ID_MPEG4 },
00147     .priv_data_size = sizeof(struct Mp4vParseContext),
00148     .parser_init    = mpeg4video_parse_init,
00149     .parser_parse   = mpeg4video_parse,
00150     .parser_close   = ff_parse_close,
00151     .split          = ff_mpeg4video_split,
00152 };