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
24 #include "mpegvideo.h"
25 #include "mpeg4video_parser.h"
28 int ff_mpeg4_find_frame_end(ParseContext
*pc
, const uint8_t *buf
, int buf_size
){
32 vop_found
= pc
->frame_start_found
;
37 for(i
=0; i
<buf_size
; i
++){
38 state
= (state
<<8) | buf
[i
];
48 /* EOF considered as end of frame */
51 for(; i
<buf_size
; i
++){
52 state
= (state
<<8) | buf
[i
];
53 if((state
&0xFFFFFF00) == 0x100){
54 pc
->frame_start_found
=0;
60 pc
->frame_start_found
= vop_found
;
65 /* XXX: make it use less memory */
66 static int av_mpeg4_decode_header(AVCodecParserContext
*s1
,
67 AVCodecContext
*avctx
,
68 const uint8_t *buf
, int buf_size
)
70 ParseContext1
*pc
= s1
->priv_data
;
71 MpegEncContext
*s
= pc
->enc
;
72 GetBitContext gb1
, *gb
= &gb1
;
76 s
->current_picture_ptr
= &s
->current_picture
;
78 if (avctx
->extradata_size
&& pc
->first_picture
){
79 init_get_bits(gb
, avctx
->extradata
, avctx
->extradata_size
*8);
80 ret
= ff_mpeg4_decode_picture_header(s
, gb
);
83 init_get_bits(gb
, buf
, 8 * buf_size
);
84 ret
= ff_mpeg4_decode_picture_header(s
, gb
);
86 avcodec_set_dimensions(avctx
, s
->width
, s
->height
);
88 s1
->pict_type
= s
->pict_type
;
89 pc
->first_picture
= 0;
93 static av_cold
int mpeg4video_parse_init(AVCodecParserContext
*s
)
95 ParseContext1
*pc
= s
->priv_data
;
97 pc
->enc
= av_mallocz(sizeof(MpegEncContext
));
100 pc
->first_picture
= 1;
104 static int mpeg4video_parse(AVCodecParserContext
*s
,
105 AVCodecContext
*avctx
,
106 const uint8_t **poutbuf
, int *poutbuf_size
,
107 const uint8_t *buf
, int buf_size
)
109 ParseContext
*pc
= s
->priv_data
;
112 if(s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
){
115 next
= ff_mpeg4_find_frame_end(pc
, buf
, buf_size
);
117 if (ff_combine_frame(pc
, next
, &buf
, &buf_size
) < 0) {
123 av_mpeg4_decode_header(s
, avctx
, buf
, buf_size
);
126 *poutbuf_size
= buf_size
;
131 AVCodecParser mpeg4video_parser
= {
133 sizeof(ParseContext1
),
134 mpeg4video_parse_init
,