2 * Audio and 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
26 #include "libavutil/internal.h"
27 #include "libavutil/mem.h"
32 static AVCodecParser *av_first_parser = NULL;
34 AVCodecParser *av_parser_next(const AVCodecParser *p)
39 return av_first_parser;
42 void av_register_codec_parser(AVCodecParser *parser)
44 parser->next = av_first_parser;
45 av_first_parser = parser;
48 AVCodecParserContext *av_parser_init(int codec_id)
50 AVCodecParserContext *s;
51 AVCodecParser *parser;
54 if (codec_id == AV_CODEC_ID_NONE)
57 for (parser = av_first_parser; parser != NULL; parser = parser->next) {
58 if (parser->codec_ids[0] == codec_id ||
59 parser->codec_ids[1] == codec_id ||
60 parser->codec_ids[2] == codec_id ||
61 parser->codec_ids[3] == codec_id ||
62 parser->codec_ids[4] == codec_id)
68 s = av_mallocz(sizeof(AVCodecParserContext));
72 if (parser->priv_data_size) {
73 s->priv_data = av_mallocz(parser->priv_data_size);
79 if (parser->parser_init) {
80 ret = parser->parser_init(s);
82 av_free(s->priv_data);
87 s->fetch_timestamp = 1;
88 s->pict_type = AV_PICTURE_TYPE_I;
90 #if FF_API_CONVERGENCE_DURATION
91 FF_DISABLE_DEPRECATION_WARNINGS
92 s->convergence_duration = 0;
93 FF_ENABLE_DEPRECATION_WARNINGS
95 s->dts_sync_point = INT_MIN;
96 s->dts_ref_dts_delta = INT_MIN;
97 s->pts_dts_delta = INT_MIN;
103 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove)
108 s->pts = AV_NOPTS_VALUE;
111 for (i = 0; i < AV_PARSER_PTS_NB; i++) {
112 if (s->cur_offset + off >= s->cur_frame_offset[i] &&
113 (s->frame_offset < s->cur_frame_offset[i] ||
114 (!s->frame_offset && !s->next_frame_offset)) &&
115 s->cur_frame_end[i]) {
116 s->dts = s->cur_frame_dts[i];
117 s->pts = s->cur_frame_pts[i];
118 s->pos = s->cur_frame_pos[i];
119 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
121 s->cur_frame_offset[i] = INT64_MAX;
122 if (s->cur_offset + off < s->cur_frame_end[i])
128 int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
129 uint8_t **poutbuf, int *poutbuf_size,
130 const uint8_t *buf, int buf_size,
131 int64_t pts, int64_t dts, int64_t pos)
134 uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
136 if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
137 s->next_frame_offset =
139 s->flags |= PARSER_FLAG_FETCHED_OFFSET;
143 /* padding is always necessary even if EOF, so we add it here */
144 memset(dummy_buf, 0, sizeof(dummy_buf));
146 } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
147 /* add a new packet descriptor */
148 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
149 s->cur_frame_start_index = i;
150 s->cur_frame_offset[i] = s->cur_offset;
151 s->cur_frame_end[i] = s->cur_offset + buf_size;
152 s->cur_frame_pts[i] = pts;
153 s->cur_frame_dts[i] = dts;
154 s->cur_frame_pos[i] = pos;
157 if (s->fetch_timestamp) {
158 s->fetch_timestamp = 0;
159 s->last_pts = s->pts;
160 s->last_dts = s->dts;
161 s->last_pos = s->pos;
162 ff_fetch_timestamp(s, 0, 0);
164 /* WARNING: the returned index can be negative */
165 index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
166 poutbuf_size, buf, buf_size);
167 /* update the file pointer */
169 /* fill the data for the current frame */
170 s->frame_offset = s->next_frame_offset;
172 /* offset of the next frame */
173 s->next_frame_offset = s->cur_offset + index;
174 s->fetch_timestamp = 1;
178 s->cur_offset += index;
182 int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
183 uint8_t **poutbuf, int *poutbuf_size,
184 const uint8_t *buf, int buf_size, int keyframe)
186 if (s && s->parser->split) {
187 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
188 avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
189 int i = s->parser->split(avctx, buf, buf_size);
195 /* cast to avoid warning about discarding qualifiers */
196 *poutbuf = (uint8_t *) buf;
197 *poutbuf_size = buf_size;
198 if (avctx->extradata) {
199 if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
200 int size = buf_size + avctx->extradata_size;
202 *poutbuf_size = size;
203 *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
205 return AVERROR(ENOMEM);
207 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
208 memcpy(*poutbuf + avctx->extradata_size, buf,
209 buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
217 void av_parser_close(AVCodecParserContext *s)
220 if (s->parser->parser_close)
221 s->parser->parser_close(s);
222 av_free(s->priv_data);
227 int ff_combine_frame(ParseContext *pc, int next,
228 const uint8_t **buf, int *buf_size)
231 ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
232 pc->overread, pc->state, next, pc->index, pc->overread_index);
233 ff_dlog(NULL, "%X %X %X %X\n",
234 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
237 /* Copy overread bytes from last frame into buffer. */
238 for (; pc->overread > 0; pc->overread--)
239 pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
241 /* flush remaining if EOF */
242 if (!*buf_size && next == END_NOT_FOUND)
245 pc->last_index = pc->index;
247 /* copy into buffer end return */
248 if (next == END_NOT_FOUND) {
249 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
250 *buf_size + pc->index +
251 AV_INPUT_BUFFER_PADDING_SIZE);
254 return AVERROR(ENOMEM);
255 pc->buffer = new_buffer;
256 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
257 pc->index += *buf_size;
262 pc->overread_index = pc->index + next;
264 /* append to buffer */
266 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
268 AV_INPUT_BUFFER_PADDING_SIZE);
271 return AVERROR(ENOMEM);
272 pc->buffer = new_buffer;
273 if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
274 memcpy(&pc->buffer[pc->index], *buf,
275 next + AV_INPUT_BUFFER_PADDING_SIZE);
280 /* store overread bytes */
281 for (; next < 0; next++) {
282 pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
283 pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
288 ff_dlog(NULL, "overread %d, state:%X next:%d index:%d o_index:%d\n",
289 pc->overread, pc->state, next, pc->index, pc->overread_index);
290 ff_dlog(NULL, "%X %X %X %X\n",
291 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
297 void ff_parse_close(AVCodecParserContext *s)
299 ParseContext *pc = s->priv_data;
301 av_freep(&pc->buffer);
304 int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
309 for (i = 0; i < buf_size; i++) {
310 state = state << 8 | buf[i];
311 if (state == 0x1B3 || state == 0x1B6)