00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "parser.h"
00024 #include "mpegvideo.h"
00025 #include "mpeg4video_parser.h"
00026
00027
00028 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
00029 int vop_found, i;
00030 uint32_t state;
00031
00032 vop_found= pc->frame_start_found;
00033 state= pc->state;
00034
00035 i=0;
00036 if(!vop_found){
00037 for(i=0; i<buf_size; i++){
00038 state= (state<<8) | buf[i];
00039 if(state == 0x1B6){
00040 i++;
00041 vop_found=1;
00042 break;
00043 }
00044 }
00045 }
00046
00047 if(vop_found){
00048
00049 if (buf_size == 0)
00050 return 0;
00051 for(; i<buf_size; i++){
00052 state= (state<<8) | buf[i];
00053 if((state&0xFFFFFF00) == 0x100){
00054 pc->frame_start_found=0;
00055 pc->state=-1;
00056 return i-3;
00057 }
00058 }
00059 }
00060 pc->frame_start_found= vop_found;
00061 pc->state= state;
00062 return END_NOT_FOUND;
00063 }
00064
00065
00066 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
00067 AVCodecContext *avctx,
00068 const uint8_t *buf, int buf_size)
00069 {
00070 ParseContext1 *pc = s1->priv_data;
00071 MpegEncContext *s = pc->enc;
00072 GetBitContext gb1, *gb = &gb1;
00073 int ret;
00074
00075 s->avctx = avctx;
00076 s->current_picture_ptr = &s->current_picture;
00077
00078 if (avctx->extradata_size && pc->first_picture){
00079 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
00080 ret = ff_mpeg4_decode_picture_header(s, gb);
00081 }
00082
00083 init_get_bits(gb, buf, 8 * buf_size);
00084 ret = ff_mpeg4_decode_picture_header(s, gb);
00085 if (s->width) {
00086 avcodec_set_dimensions(avctx, s->width, s->height);
00087 }
00088 s1->pict_type= s->pict_type;
00089 pc->first_picture = 0;
00090 return ret;
00091 }
00092
00093 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
00094 {
00095 ParseContext1 *pc = s->priv_data;
00096
00097 pc->enc = av_mallocz(sizeof(MpegEncContext));
00098 if (!pc->enc)
00099 return -1;
00100 pc->first_picture = 1;
00101 return 0;
00102 }
00103
00104 static int mpeg4video_parse(AVCodecParserContext *s,
00105 AVCodecContext *avctx,
00106 const uint8_t **poutbuf, int *poutbuf_size,
00107 const uint8_t *buf, int buf_size)
00108 {
00109 ParseContext *pc = s->priv_data;
00110 int next;
00111
00112 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
00113 next= buf_size;
00114 }else{
00115 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
00116
00117 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
00118 *poutbuf = NULL;
00119 *poutbuf_size = 0;
00120 return buf_size;
00121 }
00122 }
00123 av_mpeg4_decode_header(s, avctx, buf, buf_size);
00124
00125 *poutbuf = buf;
00126 *poutbuf_size = buf_size;
00127 return next;
00128 }
00129
00130
00131 AVCodecParser mpeg4video_parser = {
00132 { CODEC_ID_MPEG4 },
00133 sizeof(ParseContext1),
00134 mpeg4video_parse_init,
00135 mpeg4video_parse,
00136 ff_parse1_close,
00137 ff_mpeg4video_split,
00138 };