2 * MPEG4 Video frame extraction
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2003 Michael Niedermayer
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #define UNCHECKED_BITSTREAM_READER 1
26 #include "mpegvideo.h"
27 #include "mpeg4video.h"
28 #include "mpeg4video_parser.h"
30 struct Mp4vParseContext {
32 struct MpegEncContext enc;
36 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
40 vop_found= pc->frame_start_found;
45 for(i=0; i<buf_size; i++){
46 state= (state<<8) | buf[i];
56 /* EOF considered as end of frame */
59 for(; i<buf_size; i++){
60 state= (state<<8) | buf[i];
61 if((state&0xFFFFFF00) == 0x100){
62 pc->frame_start_found=0;
68 pc->frame_start_found= vop_found;
73 /* XXX: make it use less memory */
74 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
75 AVCodecContext *avctx,
76 const uint8_t *buf, int buf_size)
78 struct Mp4vParseContext *pc = s1->priv_data;
79 MpegEncContext *s = &pc->enc;
80 GetBitContext gb1, *gb = &gb1;
84 s->current_picture_ptr = &s->current_picture;
86 if (avctx->extradata_size && pc->first_picture){
87 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
88 ret = ff_mpeg4_decode_picture_header(s, gb);
91 init_get_bits(gb, buf, 8 * buf_size);
92 ret = ff_mpeg4_decode_picture_header(s, gb);
93 if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
94 avcodec_set_dimensions(avctx, s->width, s->height);
96 if((s1->flags & PARSER_FLAG_USE_CODEC_TS) && s->avctx->time_base.den>0 && ret>=0){
97 av_assert1(s1->pts == AV_NOPTS_VALUE);
98 av_assert1(s1->dts == AV_NOPTS_VALUE);
100 s1->pts = av_rescale_q(s->time, (AVRational){1, s->avctx->time_base.den}, (AVRational){1, 1200000});
103 s1->pict_type= s->pict_type;
104 pc->first_picture = 0;
108 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
110 struct Mp4vParseContext *pc = s->priv_data;
112 pc->first_picture = 1;
113 pc->enc.quant_precision=5;
114 pc->enc.slice_context_count = 1;
118 static int mpeg4video_parse(AVCodecParserContext *s,
119 AVCodecContext *avctx,
120 const uint8_t **poutbuf, int *poutbuf_size,
121 const uint8_t *buf, int buf_size)
123 ParseContext *pc = s->priv_data;
126 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
129 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
131 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
137 av_mpeg4_decode_header(s, avctx, buf, buf_size);
140 *poutbuf_size = buf_size;
145 AVCodecParser ff_mpeg4video_parser = {
146 .codec_ids = { AV_CODEC_ID_MPEG4 },
147 .priv_data_size = sizeof(struct Mp4vParseContext),
148 .parser_init = mpeg4video_parse_init,
149 .parser_parse = mpeg4video_parse,
150 .parser_close = ff_parse_close,
151 .split = ff_mpeg4video_split,