2 * MPEG-4 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
27 #include "mpegvideo.h"
28 #include "mpeg4video.h"
29 #include "mpeg4videodec.h"
30 #if FF_API_FLAG_TRUNCATED
31 /* Nuke this header when removing FF_API_FLAG_TRUNCATED */
32 #include "mpeg4video_parser.h"
35 struct Mp4vParseContext
{
37 Mpeg4DecContext dec_ctx
;
41 #if FF_API_FLAG_TRUNCATED
42 int ff_mpeg4_find_frame_end(ParseContext
*pc
, const uint8_t *buf
, int buf_size
)
45 * Find the end of the current frame in the bitstream.
46 * @return the position of the first byte of the next frame, or -1
48 static int mpeg4_find_frame_end(ParseContext
*pc
, const uint8_t *buf
, int buf_size
)
54 vop_found
= pc
->frame_start_found
;
59 for (i
= 0; i
< buf_size
; i
++) {
60 state
= (state
<< 8) | buf
[i
];
61 if (state
== VOP_STARTCODE
) {
70 /* EOF considered as end of frame */
73 for (; i
< buf_size
; i
++) {
74 state
= (state
<< 8) | buf
[i
];
75 if ((state
& 0xFFFFFF00) == 0x100) {
76 if (state
== SLICE_STARTCODE
|| state
== EXT_STARTCODE
)
78 pc
->frame_start_found
= 0;
84 pc
->frame_start_found
= vop_found
;
89 /* XXX: make it use less memory */
90 static int mpeg4_decode_header(AVCodecParserContext
*s1
, AVCodecContext
*avctx
,
91 const uint8_t *buf
, int buf_size
)
93 struct Mp4vParseContext
*pc
= s1
->priv_data
;
94 Mpeg4DecContext
*dec_ctx
= &pc
->dec_ctx
;
95 MpegEncContext
*s
= &dec_ctx
->m
;
96 GetBitContext gb1
, *gb
= &gb1
;
100 s
->current_picture_ptr
= &s
->current_picture
;
102 if (avctx
->extradata_size
&& pc
->first_picture
) {
103 init_get_bits(gb
, avctx
->extradata
, avctx
->extradata_size
* 8);
104 ret
= ff_mpeg4_decode_picture_header(dec_ctx
, gb
, 1, 1);
106 av_log(avctx
, AV_LOG_WARNING
, "Failed to parse extradata\n");
109 init_get_bits(gb
, buf
, 8 * buf_size
);
110 ret
= ff_mpeg4_decode_picture_header(dec_ctx
, gb
, 0, 1);
111 if (s
->width
&& (!avctx
->width
|| !avctx
->height
||
112 !avctx
->coded_width
|| !avctx
->coded_height
)) {
113 ret
= ff_set_dimensions(avctx
, s
->width
, s
->height
);
117 if((s1
->flags
& PARSER_FLAG_USE_CODEC_TS
) && s
->avctx
->time_base
.den
>0 && ret
>=0){
118 av_assert1(s1
->pts
== AV_NOPTS_VALUE
);
119 av_assert1(s1
->dts
== AV_NOPTS_VALUE
);
121 s1
->pts
= av_rescale_q(s
->time
, (AVRational
){1, s
->avctx
->time_base
.den
}, (AVRational
){1, 1200000});
124 s1
->pict_type
= s
->pict_type
;
125 pc
->first_picture
= 0;
129 static av_cold
int mpeg4video_parse_init(AVCodecParserContext
*s
)
131 struct Mp4vParseContext
*pc
= s
->priv_data
;
133 pc
->first_picture
= 1;
134 pc
->dec_ctx
.m
.quant_precision
= 5;
135 pc
->dec_ctx
.m
.slice_context_count
= 1;
136 pc
->dec_ctx
.showed_packed_warning
= 1;
140 static int mpeg4video_parse(AVCodecParserContext
*s
,
141 AVCodecContext
*avctx
,
142 const uint8_t **poutbuf
, int *poutbuf_size
,
143 const uint8_t *buf
, int buf_size
)
145 ParseContext
*pc
= s
->priv_data
;
148 if (s
->flags
& PARSER_FLAG_COMPLETE_FRAMES
) {
151 #if FF_API_FLAG_TRUNCATED
152 next
= ff_mpeg4_find_frame_end(pc
, buf
, buf_size
);
154 next
= mpeg4_find_frame_end(pc
, buf
, buf_size
);
157 if (ff_combine_frame(pc
, next
, &buf
, &buf_size
) < 0) {
163 mpeg4_decode_header(s
, avctx
, buf
, buf_size
);
166 *poutbuf_size
= buf_size
;
170 const AVCodecParser ff_mpeg4video_parser
= {
171 .codec_ids
= { AV_CODEC_ID_MPEG4
},
172 .priv_data_size
= sizeof(struct Mp4vParseContext
),
173 .parser_init
= mpeg4video_parse_init
,
174 .parser_parse
= mpeg4video_parse
,
175 .parser_close
= ff_parse_close
,