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 ff_mpeg4videodec_static_init();
00113
00114 pc->first_picture = 1;
00115 pc->enc.quant_precision=5;
00116 pc->enc.slice_context_count = 1;
00117 return 0;
00118 }
00119
00120 static int mpeg4video_parse(AVCodecParserContext *s,
00121 AVCodecContext *avctx,
00122 const uint8_t **poutbuf, int *poutbuf_size,
00123 const uint8_t *buf, int buf_size)
00124 {
00125 ParseContext *pc = s->priv_data;
00126 int next;
00127
00128 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00129 next= buf_size;
00130 }else{
00131 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
00132
00133 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00134 *poutbuf = NULL;
00135 *poutbuf_size = 0;
00136 return buf_size;
00137 }
00138 }
00139 av_mpeg4_decode_header(s, avctx, buf, buf_size);
00140
00141 *poutbuf = buf;
00142 *poutbuf_size = buf_size;
00143 return next;
00144 }
00145
00146
00147 AVCodecParser ff_mpeg4video_parser = {
00148 .codec_ids = { AV_CODEC_ID_MPEG4 },
00149 .priv_data_size = sizeof(struct Mp4vParseContext),
00150 .parser_init = mpeg4video_parse_init,
00151 .parser_parse = mpeg4video_parse,
00152 .parser_close = ff_parse_close,
00153 .split = ff_mpeg4video_split,
00154 };