2 * Audio and 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
27 #include "libavutil/avassert.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/thread.h"
36 extern AVCodecParser ff_aac_parser;
37 extern AVCodecParser ff_aac_latm_parser;
38 extern AVCodecParser ff_ac3_parser;
39 extern AVCodecParser ff_adx_parser;
40 extern AVCodecParser ff_bmp_parser;
41 extern AVCodecParser ff_cavsvideo_parser;
42 extern AVCodecParser ff_cook_parser;
43 extern AVCodecParser ff_dca_parser;
44 extern AVCodecParser ff_dirac_parser;
45 extern AVCodecParser ff_dnxhd_parser;
46 extern AVCodecParser ff_dpx_parser;
47 extern AVCodecParser ff_dvaudio_parser;
48 extern AVCodecParser ff_dvbsub_parser;
49 extern AVCodecParser ff_dvdsub_parser;
50 extern AVCodecParser ff_dvd_nav_parser;
51 extern AVCodecParser ff_flac_parser;
52 extern AVCodecParser ff_g729_parser;
53 extern AVCodecParser ff_gsm_parser;
54 extern AVCodecParser ff_h261_parser;
55 extern AVCodecParser ff_h263_parser;
56 extern AVCodecParser ff_h264_parser;
57 extern AVCodecParser ff_hevc_parser;
58 extern AVCodecParser ff_mjpeg_parser;
59 extern AVCodecParser ff_mlp_parser;
60 extern AVCodecParser ff_mpeg4video_parser;
61 extern AVCodecParser ff_mpegaudio_parser;
62 extern AVCodecParser ff_mpegvideo_parser;
63 extern AVCodecParser ff_opus_parser;
64 extern AVCodecParser ff_png_parser;
65 extern AVCodecParser ff_pnm_parser;
66 extern AVCodecParser ff_rv30_parser;
67 extern AVCodecParser ff_rv40_parser;
68 extern AVCodecParser ff_sbc_parser;
69 extern AVCodecParser ff_sipr_parser;
70 extern AVCodecParser ff_tak_parser;
71 extern AVCodecParser ff_vc1_parser;
72 extern AVCodecParser ff_vorbis_parser;
73 extern AVCodecParser ff_vp3_parser;
74 extern AVCodecParser ff_vp8_parser;
75 extern AVCodecParser ff_vp9_parser;
76 extern AVCodecParser ff_xma_parser;
78 #include "libavcodec/parser_list.c"
80 static AVOnce av_parser_next_init = AV_ONCE_INIT;
82 static void av_parser_init_next(void)
84 AVCodecParser *prev = NULL, *p;
86 while ((p = (AVCodecParser*)parser_list[i++])) {
93 AVCodecParser *av_parser_next(const AVCodecParser *p)
95 ff_thread_once(&av_parser_next_init, av_parser_init_next);
100 return (AVCodecParser*)parser_list[0];
103 const AVCodecParser *av_parser_iterate(void **opaque)
105 uintptr_t i = (uintptr_t)*opaque;
106 const AVCodecParser *p = parser_list[i];
109 *opaque = (void*)(i + 1);
114 void av_register_codec_parser(AVCodecParser *parser)
116 ff_thread_once(&av_parser_next_init, av_parser_init_next);
119 AVCodecParserContext *av_parser_init(int codec_id)
121 AVCodecParserContext *s = NULL;
122 const AVCodecParser *parser;
126 if (codec_id == AV_CODEC_ID_NONE)
129 while ((parser = av_parser_iterate(&i))) {
130 if (parser->codec_ids[0] == codec_id ||
131 parser->codec_ids[1] == codec_id ||
132 parser->codec_ids[2] == codec_id ||
133 parser->codec_ids[3] == codec_id ||
134 parser->codec_ids[4] == codec_id)
140 s = av_mallocz(sizeof(AVCodecParserContext));
143 s->parser = (AVCodecParser*)parser;
144 s->priv_data = av_mallocz(parser->priv_data_size);
147 s->fetch_timestamp=1;
148 s->pict_type = AV_PICTURE_TYPE_I;
149 if (parser->parser_init) {
150 ret = parser->parser_init(s);
155 #if FF_API_CONVERGENCE_DURATION
156 FF_DISABLE_DEPRECATION_WARNINGS
157 s->convergence_duration = 0;
158 FF_ENABLE_DEPRECATION_WARNINGS
160 s->dts_sync_point = INT_MIN;
161 s->dts_ref_dts_delta = INT_MIN;
162 s->pts_dts_delta = INT_MIN;
169 av_freep(&s->priv_data);
174 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
180 s->pts = AV_NOPTS_VALUE;
184 for (i = 0; i < AV_PARSER_PTS_NB; i++) {
185 if (s->cur_offset + off >= s->cur_frame_offset[i] &&
186 (s->frame_offset < s->cur_frame_offset[i] ||
187 (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
188 // check disabled since MPEG-TS does not send complete PES packets
189 /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){
191 if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
192 s->dts = s->cur_frame_dts[i];
193 s->pts = s->cur_frame_pts[i];
194 s->pos = s->cur_frame_pos[i];
195 s->offset = s->next_frame_offset - s->cur_frame_offset[i];
198 s->cur_frame_offset[i] = INT64_MAX;
199 if (s->cur_offset + off < s->cur_frame_end[i])
205 int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx,
206 uint8_t **poutbuf, int *poutbuf_size,
207 const uint8_t *buf, int buf_size,
208 int64_t pts, int64_t dts, int64_t pos)
211 uint8_t dummy_buf[AV_INPUT_BUFFER_PADDING_SIZE];
213 av_assert1(avctx->codec_id != AV_CODEC_ID_NONE);
215 /* Parsers only work for the specified codec ids. */
216 av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
217 avctx->codec_id == s->parser->codec_ids[1] ||
218 avctx->codec_id == s->parser->codec_ids[2] ||
219 avctx->codec_id == s->parser->codec_ids[3] ||
220 avctx->codec_id == s->parser->codec_ids[4]);
222 if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
223 s->next_frame_offset =
225 s->flags |= PARSER_FLAG_FETCHED_OFFSET;
229 /* padding is always necessary even if EOF, so we add it here */
230 memset(dummy_buf, 0, sizeof(dummy_buf));
232 } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
233 /* add a new packet descriptor */
234 i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
235 s->cur_frame_start_index = i;
236 s->cur_frame_offset[i] = s->cur_offset;
237 s->cur_frame_end[i] = s->cur_offset + buf_size;
238 s->cur_frame_pts[i] = pts;
239 s->cur_frame_dts[i] = dts;
240 s->cur_frame_pos[i] = pos;
243 if (s->fetch_timestamp) {
244 s->fetch_timestamp = 0;
245 s->last_pts = s->pts;
246 s->last_dts = s->dts;
247 s->last_pos = s->pos;
248 ff_fetch_timestamp(s, 0, 0, 0);
250 /* WARNING: the returned index can be negative */
251 index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
252 poutbuf_size, buf, buf_size);
253 av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
254 #define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
255 if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
259 /* update the file pointer */
261 /* fill the data for the current frame */
262 s->frame_offset = s->next_frame_offset;
264 /* offset of the next frame */
265 s->next_frame_offset = s->cur_offset + index;
266 s->fetch_timestamp = 1;
270 s->cur_offset += index;
274 int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx,
275 uint8_t **poutbuf, int *poutbuf_size,
276 const uint8_t *buf, int buf_size, int keyframe)
278 if (s && s->parser->split) {
279 if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
280 avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER) {
281 int i = s->parser->split(avctx, buf, buf_size);
287 /* cast to avoid warning about discarding qualifiers */
288 *poutbuf = (uint8_t *) buf;
289 *poutbuf_size = buf_size;
290 if (avctx->extradata) {
291 if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
292 int size = buf_size + avctx->extradata_size;
294 *poutbuf_size = size;
295 *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
297 return AVERROR(ENOMEM);
299 memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
300 memcpy(*poutbuf + avctx->extradata_size, buf,
301 buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
309 void av_parser_close(AVCodecParserContext *s)
312 if (s->parser->parser_close)
313 s->parser->parser_close(s);
314 av_freep(&s->priv_data);
319 int ff_combine_frame(ParseContext *pc, int next,
320 const uint8_t **buf, int *buf_size)
323 ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
324 pc->overread, pc->state, next, pc->index, pc->overread_index);
325 ff_dlog(NULL, "%X %X %X %X\n",
326 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
329 /* Copy overread bytes from last frame into buffer. */
330 for (; pc->overread > 0; pc->overread--)
331 pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
333 /* flush remaining if EOF */
334 if (!*buf_size && next == END_NOT_FOUND)
337 pc->last_index = pc->index;
339 /* copy into buffer end return */
340 if (next == END_NOT_FOUND) {
341 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
342 *buf_size + pc->index +
343 AV_INPUT_BUFFER_PADDING_SIZE);
346 av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
348 return AVERROR(ENOMEM);
350 pc->buffer = new_buffer;
351 memcpy(&pc->buffer[pc->index], *buf, *buf_size);
352 pc->index += *buf_size;
356 av_assert0(next >= 0 || pc->buffer);
359 pc->overread_index = pc->index + next;
361 /* append to buffer */
363 void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
365 AV_INPUT_BUFFER_PADDING_SIZE);
367 av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
370 return AVERROR(ENOMEM);
372 pc->buffer = new_buffer;
373 if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
374 memcpy(&pc->buffer[pc->index], *buf,
375 next + AV_INPUT_BUFFER_PADDING_SIZE);
380 /* store overread bytes */
381 for (; next < 0; next++) {
382 pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
383 pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
388 ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
389 pc->overread, pc->state, next, pc->index, pc->overread_index);
390 ff_dlog(NULL, "%X %X %X %X\n",
391 (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
397 void ff_parse_close(AVCodecParserContext *s)
399 ParseContext *pc = s->priv_data;
401 av_freep(&pc->buffer);
404 int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
407 const uint8_t *ptr = buf, *end = buf + buf_size;
410 ptr = avpriv_find_start_code(ptr, end, &state);
411 if (state == 0x1B3 || state == 0x1B6)
412 return ptr - 4 - buf;