2 * MPEG4 Video frame extraction
3 * Copyright (c) 2003 Fabrice Bellard
4 * Copyright (c) 2003 Michael Niedermayer
6 * This file is part of Libav.
8 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "mpegvideo.h"
25 #include "mpeg4video.h"
26 #include "mpeg4video_parser.h"
28 struct Mp4vParseContext {
30 struct MpegEncContext enc;
34 int ff_mpeg4_find_frame_end(ParseContext *pc, const uint8_t *buf, int buf_size){
38 vop_found= pc->frame_start_found;
43 for(i=0; i<buf_size; i++){
44 state= (state<<8) | buf[i];
54 /* EOF considered as end of frame */
57 for(; i<buf_size; i++){
58 state= (state<<8) | buf[i];
59 if((state&0xFFFFFF00) == 0x100){
60 pc->frame_start_found=0;
66 pc->frame_start_found= vop_found;
71 /* XXX: make it use less memory */
72 static int av_mpeg4_decode_header(AVCodecParserContext *s1,
73 AVCodecContext *avctx,
74 const uint8_t *buf, int buf_size)
76 struct Mp4vParseContext *pc = s1->priv_data;
77 MpegEncContext *s = &pc->enc;
78 GetBitContext gb1, *gb = &gb1;
82 s->current_picture_ptr = &s->current_picture;
84 if (avctx->extradata_size && pc->first_picture){
85 init_get_bits(gb, avctx->extradata, avctx->extradata_size*8);
86 ret = ff_mpeg4_decode_picture_header(s, gb);
89 init_get_bits(gb, buf, 8 * buf_size);
90 ret = ff_mpeg4_decode_picture_header(s, gb);
91 if (s->width && (!avctx->width || !avctx->height || !avctx->coded_width || !avctx->coded_height)) {
92 avcodec_set_dimensions(avctx, s->width, s->height);
94 s1->pict_type= s->pict_type;
95 pc->first_picture = 0;
99 static av_cold int mpeg4video_parse_init(AVCodecParserContext *s)
101 struct Mp4vParseContext *pc = s->priv_data;
103 pc->first_picture = 1;
104 pc->enc.slice_context_count = 1;
108 static int mpeg4video_parse(AVCodecParserContext *s,
109 AVCodecContext *avctx,
110 const uint8_t **poutbuf, int *poutbuf_size,
111 const uint8_t *buf, int buf_size)
113 ParseContext *pc = s->priv_data;
116 if(s->flags & PARSER_FLAG_COMPLETE_FRAMES){
119 next= ff_mpeg4_find_frame_end(pc, buf, buf_size);
121 if (ff_combine_frame(pc, next, &buf, &buf_size) < 0) {
127 av_mpeg4_decode_header(s, avctx, buf, buf_size);
130 *poutbuf_size = buf_size;
135 AVCodecParser ff_mpeg4video_parser = {
136 .codec_ids = { CODEC_ID_MPEG4 },
137 .priv_data_size = sizeof(struct Mp4vParseContext),
138 .parser_init = mpeg4video_parse_init,
139 .parser_parse = mpeg4video_parse,
140 .parser_close = ff_parse_close,
141 .split = ff_mpeg4video_split,