FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
parser.c
Go to the documentation of this file.
1 /*
2  * Audio and Video frame extraction
3  * Copyright (c) 2003 Fabrice Bellard
4  * Copyright (c) 2003 Michael Niedermayer
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22 
23 #include <inttypes.h>
24 #include <stdint.h>
25 #include <string.h>
26 
27 #include "libavutil/avassert.h"
28 #include "libavutil/internal.h"
29 #include "libavutil/mem.h"
30 #include "libavutil/thread.h"
31 
32 #include "internal.h"
33 #include "parser.h"
34 
35 /* Parsers */
77 
78 #include "libavcodec/parser_list.c"
79 
81 
82 static void av_parser_init_next(void)
83 {
84  AVCodecParser *prev = NULL, *p;
85  int i = 0;
86  while ((p = (AVCodecParser*)parser_list[i++])) {
87  if (prev)
88  prev->next = p;
89  prev = p;
90  }
91 }
92 
94 {
96 
97  if (p)
98  return p->next;
99  else
100  return (AVCodecParser*)parser_list[0];
101 }
102 
103 const AVCodecParser *av_parser_iterate(void **opaque)
104 {
105  uintptr_t i = (uintptr_t)*opaque;
106  const AVCodecParser *p = parser_list[i];
107 
108  if (p)
109  *opaque = (void*)(i + 1);
110 
111  return p;
112 }
113 
115 {
117 }
118 
120 {
122  const AVCodecParser *parser;
123  void *i = 0;
124  int ret;
125 
126  if (codec_id == AV_CODEC_ID_NONE)
127  return NULL;
128 
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)
135  goto found;
136  }
137  return NULL;
138 
139 found:
140  s = av_mallocz(sizeof(AVCodecParserContext));
141  if (!s)
142  goto err_out;
143  s->parser = (AVCodecParser*)parser;
144  s->priv_data = av_mallocz(parser->priv_data_size);
145  if (!s->priv_data)
146  goto err_out;
147  s->fetch_timestamp=1;
149  if (parser->parser_init) {
150  ret = parser->parser_init(s);
151  if (ret != 0)
152  goto err_out;
153  }
154  s->key_frame = -1;
155 #if FF_API_CONVERGENCE_DURATION
157  s->convergence_duration = 0;
159 #endif
160  s->dts_sync_point = INT_MIN;
161  s->dts_ref_dts_delta = INT_MIN;
162  s->pts_dts_delta = INT_MIN;
163  s->format = -1;
164 
165  return s;
166 
167 err_out:
168  if (s)
169  av_freep(&s->priv_data);
170  av_free(s);
171  return NULL;
172 }
173 
174 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
175 {
176  int i;
177 
178  if (!fuzzy) {
179  s->dts =
180  s->pts = AV_NOPTS_VALUE;
181  s->pos = -1;
182  s->offset = 0;
183  }
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]){
190 
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];
196  }
197  if (remove)
198  s->cur_frame_offset[i] = INT64_MAX;
199  if (s->cur_offset + off < s->cur_frame_end[i])
200  break;
201  }
202  }
203 }
204 
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)
209 {
210  int index, i;
212 
214 
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]);
221 
222  if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
223  s->next_frame_offset =
224  s->cur_offset = pos;
226  }
227 
228  if (buf_size == 0) {
229  /* padding is always necessary even if EOF, so we add it here */
230  memset(dummy_buf, 0, sizeof(dummy_buf));
231  buf = 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;
241  }
242 
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);
249  }
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) {
256  FILL(field_order);
257  }
258 
259  /* update the file pointer */
260  if (*poutbuf_size) {
261  /* fill the data for the current frame */
263 
264  /* offset of the next frame */
266  s->fetch_timestamp = 1;
267  }
268  if (index < 0)
269  index = 0;
270  s->cur_offset += index;
271  return index;
272 }
273 
275  uint8_t **poutbuf, int *poutbuf_size,
276  const uint8_t *buf, int buf_size, int keyframe)
277 {
278  if (s && s->parser->split) {
279  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
281  int i = s->parser->split(avctx, buf, buf_size);
282  buf += i;
283  buf_size -= i;
284  }
285  }
286 
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;
293 
294  *poutbuf_size = size;
295  *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
296  if (!*poutbuf)
297  return AVERROR(ENOMEM);
298 
299  memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
300  memcpy(*poutbuf + avctx->extradata_size, buf,
301  buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
302  return 1;
303  }
304  }
305 
306  return 0;
307 }
308 
310 {
311  if (s) {
312  if (s->parser->parser_close)
313  s->parser->parser_close(s);
314  av_freep(&s->priv_data);
315  av_free(s);
316  }
317 }
318 
319 int ff_combine_frame(ParseContext *pc, int next,
320  const uint8_t **buf, int *buf_size)
321 {
322  if (pc->overread) {
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]);
327  }
328 
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++];
332 
333  /* flush remaining if EOF */
334  if (!*buf_size && next == END_NOT_FOUND)
335  next = 0;
336 
337  pc->last_index = pc->index;
338 
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 +
344 
345  if (!new_buffer) {
346  av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
347  pc->index = 0;
348  return AVERROR(ENOMEM);
349  }
350  pc->buffer = new_buffer;
351  memcpy(&pc->buffer[pc->index], *buf, *buf_size);
352  pc->index += *buf_size;
353  return -1;
354  }
355 
356  av_assert0(next >= 0 || pc->buffer);
357 
358  *buf_size =
359  pc->overread_index = pc->index + next;
360 
361  /* append to buffer */
362  if (pc->index) {
363  void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
364  next + pc->index +
366  if (!new_buffer) {
367  av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
368  pc->overread_index =
369  pc->index = 0;
370  return AVERROR(ENOMEM);
371  }
372  pc->buffer = new_buffer;
373  if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
374  memcpy(&pc->buffer[pc->index], *buf,
376  pc->index = 0;
377  *buf = pc->buffer;
378  }
379 
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];
384  pc->overread++;
385  }
386 
387  if (pc->overread) {
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]);
392  }
393 
394  return 0;
395 }
396 
398 {
399  ParseContext *pc = s->priv_data;
400 
401  av_freep(&pc->buffer);
402 }
403 
404 int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
405 {
406  uint32_t state = -1;
407  const uint8_t *ptr = buf, *end = buf + buf_size;
408 
409  while (ptr < end) {
410  ptr = avpriv_find_start_code(ptr, end, &state);
411  if (state == 0x1B3 || state == 0x1B6)
412  return ptr - 4 - buf;
413  }
414 
415  return 0;
416 }
AVCodecParser ff_mlp_parser
Definition: mlp_parser.c:394
int(* parser_init)(AVCodecParserContext *s)
Definition: avcodec.h:5202
#define NULL
Definition: coverity.c:32
const char * s
Definition: avisynth_c.h:768
AVCodecParser ff_dvd_nav_parser
AVCodecParser ff_dpx_parser
Definition: dpx_parser.c:111
AVCodecParser ff_opus_parser
Definition: opus_parser.c:193
void av_register_codec_parser(AVCodecParser *parser)
Definition: parser.c:114
Memory handling functions.
int64_t next_frame_offset
Definition: avcodec.h:5038
AVCodecParser ff_mpeg4video_parser
int codec_ids[5]
Definition: avcodec.h:5200
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int64_t cur_frame_pos[AV_PARSER_PTS_NB]
Position of the packet in file.
Definition: avcodec.h:5137
AVCodecParser ff_dca_parser
Definition: dca_parser.c:346
AVCodecParser ff_h261_parser
Definition: h261_parser.c:89
int dts_ref_dts_delta
Offset of the current timestamp against last timestamp sync point in units of AVCodecContext.time_base.
Definition: avcodec.h:5116
static AVOnce av_parser_next_init
Definition: parser.c:80
int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: parser.c:404
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVCodecParser ff_rv40_parser
uint8_t
#define av_malloc(s)
int av_parser_change(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Definition: parser.c:274
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
AVCodecParser ff_vc1_parser
Definition: vc1_parser.c:289
AVCodecParser ff_flac_parser
Definition: flac_parser.c:751
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1618
AVCodecParser ff_g729_parser
Definition: g729_parser.c:77
AVCodecParser ff_dnxhd_parser
Definition: dnxhd_parser.c:138
struct AVCodecParser * next
Definition: avcodec.h:5211
#define ff_dlog(a,...)
int(* parser_parse)(AVCodecParserContext *s, AVCodecContext *avctx, const uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size)
Definition: avcodec.h:5205
ptrdiff_t size
Definition: opengl_enc.c:101
#define AVOnce
Definition: thread.h:159
#define av_log(a,...)
const uint8_t * avpriv_find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
#define FILL(name)
AVCodecParser ff_rv30_parser
unsigned int buffer_size
Definition: parser.h:32
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int64_t pos
Byte position of currently parsed frame in stream.
Definition: avcodec.h:5142
int priv_data_size
Definition: avcodec.h:5201
struct AVCodecParser * parser
Definition: avcodec.h:5034
int ff_combine_frame(ParseContext *pc, int next, const uint8_t **buf, int *buf_size)
Combine the (truncated) bitstream to a complete frame.
Definition: parser.c:319
AVCodecParser ff_hevc_parser
Definition: hevc_parser.c:367
#define AVERROR(e)
Definition: error.h:43
AVCodecParser ff_gsm_parser
Definition: gsm_parser.c:86
AVCodecParser ff_sbc_parser
Definition: sbc_parser.c:117
AVCodecParser * av_parser_next(const AVCodecParser *p)
Definition: parser.c:93
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1598
simple assert() macros that are a bit more flexible than ISO C assert().
static void av_parser_init_next(void)
Definition: parser.c:82
int overread_index
the index into ParseContext.buffer of the overread bytes
Definition: parser.h:36
int64_t cur_frame_dts[AV_PARSER_PTS_NB]
Definition: avcodec.h:5063
AVCodecParser ff_h263_parser
Definition: h263_parser.c:90
AVCodecParser ff_png_parser
Definition: png_parser.c:112
AVCodecParser ff_vp9_parser
Definition: vp9_parser.c:63
void ff_parse_close(AVCodecParserContext *s)
Definition: parser.c:397
common internal API header
AVCodecParser ff_aac_parser
Definition: aac_parser.c:65
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
int overread
the number of bytes which where irreversibly read from the next frame
Definition: parser.h:35
int last_index
Definition: parser.h:31
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
Definition: parser.c:205
void(* parser_close)(AVCodecParserContext *s)
Definition: avcodec.h:5209
AVCodecParser ff_aac_latm_parser
Definition: latm_parser.c:107
int64_t offset
byte offset from starting packet start
Definition: avcodec.h:5072
AVCodecParser ff_dvaudio_parser
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:309
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:464
enum AVCodecID codec_id
Definition: vaapi_decode.c:362
static struct @271 state
int64_t cur_frame_end[AV_PARSER_PTS_NB]
Definition: avcodec.h:5073
int64_t cur_frame_pts[AV_PARSER_PTS_NB]
Definition: avcodec.h:5062
int64_t last_pos
Previous frame byte position.
Definition: avcodec.h:5147
AVCodecParser ff_cook_parser
Definition: cook_parser.c:56
int pts_dts_delta
Presentation delay of current frame in units of AVCodecContext.time_base.
Definition: avcodec.h:5130
AVCodecParser ff_cavsvideo_parser
Definition: cavs_parser.c:100
AVCodecParser ff_vorbis_parser
#define AV_ONCE_INIT
Definition: thread.h:160
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:119
uint8_t * buffer
Definition: parser.h:29
enum AVMediaType codec_type
Definition: avcodec.h:1526
enum AVCodecID codec_id
Definition: avcodec.h:1528
const AVCodecParser * av_parser_iterate(void **opaque)
Iterate over all registered codec parsers.
Definition: parser.c:103
AVCodecParser ff_vp3_parser
Definition: vp3_parser.c:38
main external API structure.
Definition: avcodec.h:1518
#define AV_CODEC_FLAG2_LOCAL_HEADER
Place global headers at every keyframe instead of in extradata.
Definition: avcodec.h:909
#define PARSER_FLAG_FETCHED_OFFSET
Set if the parser has a valid file offset.
Definition: avcodec.h:5069
AVCodecParser ff_dvdsub_parser
Definition: dvdsub_parser.c:85
void * buf
Definition: avisynth_c.h:690
uint32_t state
contains the last few bytes in MSB order
Definition: parser.h:33
int extradata_size
Definition: avcodec.h:1619
int index
Definition: gxfenc.c:89
uint64_t state64
contains the last 8 bytes in MSB order
Definition: parser.h:37
AVCodecParser ff_ac3_parser
AVCodecParser ff_mpegvideo_parser
AVCodecParser ff_adx_parser
Definition: adx_parser.c:91
#define END_NOT_FOUND
Definition: parser.h:40
AVCodecParser ff_bmp_parser
Definition: bmp_parser.c:107
AVCodecParser ff_dirac_parser
Definition: dirac_parser.c:276
#define AV_PARSER_PTS_NB
Definition: avcodec.h:5059
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:5088
static int64_t pts
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:882
int(* split)(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: avcodec.h:5210
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
AVCodecParser ff_mpegaudio_parser
if(ret< 0)
Definition: vf_mcdeint.c:279
int index
Definition: parser.h:30
AVCodecParser ff_vp8_parser
Definition: vp8_parser.c:75
int64_t cur_frame_offset[AV_PARSER_PTS_NB]
Definition: avcodec.h:5061
AVCodecParser ff_h264_parser
Definition: h264_parser.c:705
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:773
AVCodecParser ff_tak_parser
Definition: tak_parser.c:121
int64_t frame_offset
Definition: avcodec.h:5035
#define av_free(p)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
static int ff_thread_once(char *control, void(*routine)(void))
Definition: thread.h:162
int format
The format of the coded data, corresponds to enum AVPixelFormat for video and for enum AVSampleFormat...
Definition: avcodec.h:5196
AVCodecParser ff_dvbsub_parser
AVCodecParser ff_mjpeg_parser
Definition: mjpeg_parser.c:129
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1605
AVCodecParser ff_xma_parser
Definition: xma_parser.c:58
#define av_freep(p)
AVCodecParser ff_sipr_parser
Definition: sipr_parser.c:69
AVCodecParser ff_pnm_parser
Definition: pnm_parser.c:81
void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
Fetch timestamps for a specific byte within the current access unit.
Definition: parser.c:174
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:5081
int dts_sync_point
Synchronization point for start of timestamp generation.
Definition: avcodec.h:5101
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248