yading@10: /* yading@10: * MPEG4 Video frame extraction yading@10: * Copyright (c) 2003 Fabrice Bellard yading@10: * Copyright (c) 2003 Michael Niedermayer yading@10: * yading@10: * This file is part of FFmpeg. yading@10: * yading@10: * FFmpeg is free software; you can redistribute it and/or yading@10: * modify it under the terms of the GNU Lesser General Public yading@10: * License as published by the Free Software Foundation; either yading@10: * version 2.1 of the License, or (at your option) any later version. yading@10: * yading@10: * FFmpeg is distributed in the hope that it will be useful, yading@10: * but WITHOUT ANY WARRANTY; without even the implied warranty of yading@10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU yading@10: * Lesser General Public License for more details. yading@10: * yading@10: * You should have received a copy of the GNU Lesser General Public yading@10: * License along with FFmpeg; if not, write to the Free Software yading@10: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA yading@10: */ yading@10: yading@10: #define UNCHECKED_BITSTREAM_READER 1 yading@10: yading@10: #include "parser.h" yading@10: #include "mpegvideo.h" yading@10: #include "mpeg4video.h" yading@10: #include "mpeg4video_parser.h" yading@10: yading@10: struct Mp4vParseContext { yading@10: ParseContext pc; yading@10: struct MpegEncContext enc; yading@10: int first_picture; yading@10: }; yading@10: yading@10: int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){ yading@10: int vop_found, i; yading@10: uint32_t state; yading@10: yading@10: vop_found= pc->frame_start_found; yading@10: state= pc->state; yading@10: yading@10: i=0; yading@10: if(!vop_found){ yading@10: for(i=0; iframe_start_found=0; yading@10: pc->state=-1; yading@10: return i-3; yading@10: } yading@10: } yading@10: } yading@10: pc->frame_start_found= vop_found; yading@10: pc->state= state; yading@10: return END_NOT_FOUND; yading@10: } yading@10: yading@10: /* XXX: make it use less memory */ yading@10: static int av_mpeg4_decode_header(AVCodecParserContext *s1, yading@10: AVCodecContext *avctx, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: struct Mp4vParseContext *pc = s1->priv_data; yading@10: MpegEncContext *s = &pc->enc; yading@10: GetBitContext gb1, *gb = &gb1; yading@10: int ret; yading@10: yading@10: s->avctx = avctx; yading@10: s->current_picture_ptr = &s->current_picture; yading@10: yading@10: if (avctx->extradata_size && pc->first_picture){ yading@10: init_get_bits(gb, avctx->extradata, avctx->extradata_size*8); yading@10: ret = ff_mpeg4_decode_picture_header(s, gb); yading@10: } yading@10: yading@10: init_get_bits(gb, buf, 8 * buf_size); yading@10: ret = ff_mpeg4_decode_picture_header(s, gb); yading@10: if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) { yading@10: avcodec_set_dimensions(avctx, s->width, s->height); yading@10: } yading@10: if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){ yading@10: av_assert1(s1->pts == AV_NOPTS_VALUE); yading@10: av_assert1(s1->dts == AV_NOPTS_VALUE); yading@10: yading@10: s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000}); yading@10: } yading@10: yading@10: s1->pict_type= s->pict_type; yading@10: pc->first_picture = 0; yading@10: return ret; yading@10: } yading@10: yading@10: static av_cold int mpeg4video_parse_init(AVCodecParserContext *s) yading@10: { yading@10: struct Mp4vParseContext *pc = s->priv_data; yading@10: yading@10: ff_mpeg4videodec_static_init(); yading@10: yading@10: pc->first_picture = 1; yading@10: pc->enc.quant_precision=5; yading@10: pc->enc.slice_context_count = 1; yading@10: return 0; yading@10: } yading@10: yading@10: static int mpeg4video_parse(AVCodecParserContext *s, yading@10: AVCodecContext *avctx, yading@10: const uint8_t **poutbuf, int *poutbuf_size, yading@10: const uint8_t *buf, int buf_size) yading@10: { yading@10: ParseContext *pc = s->priv_data; yading@10: int next; yading@10: yading@10: if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){ yading@10: next= buf_size; yading@10: }else{ yading@10: next= ff_mpeg4_find_frame_end(pc, buf, buf_size); yading@10: yading@10: if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) { yading@10: *poutbuf = NULL; yading@10: *poutbuf_size = 0; yading@10: return buf_size; yading@10: } yading@10: } yading@10: av_mpeg4_decode_header(s, avctx, buf, buf_size); yading@10: yading@10: *poutbuf = buf; yading@10: *poutbuf_size = buf_size; yading@10: return next; yading@10: } yading@10: yading@10: yading@10: AVCodecParser ff_mpeg4video_parser = { yading@10: .codec_ids = { AV_CODEC_ID_MPEG4 }, yading@10: .priv_data_size = sizeof(struct Mp4vParseContext), yading@10: .parser_init = mpeg4video_parse_init, yading@10: .parser_parse = mpeg4video_parse, yading@10: .parser_close = ff_parse_close, yading@10: .split = ff_mpeg4video_split, yading@10: };