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 */
76 
77 #include "libavcodec/parser_list.c"
78 
80 
81 static void av_parser_init_next(void)
82 {
83  AVCodecParser *prev = NULL, *p;
84  int i = 0;
85  while ((p = (AVCodecParser*)parser_list[i++])) {
86  if (prev)
87  prev->next = p;
88  prev = p;
89  }
90 }
91 
93 {
95 
96  if (p)
97  return p->next;
98  else
99  return (AVCodecParser*)parser_list[0];
100 }
101 
102 const AVCodecParser *av_parser_iterate(void **opaque)
103 {
104  uintptr_t i = (uintptr_t)*opaque;
105  const AVCodecParser *p = parser_list[i];
106 
107  if (p)
108  *opaque = (void*)(i + 1);
109 
110  return p;
111 }
112 
114 {
116 }
117 
119 {
121  const AVCodecParser *parser;
122  void *i = 0;
123  int ret;
124 
125  if (codec_id == AV_CODEC_ID_NONE)
126  return NULL;
127 
128  while ((parser = av_parser_iterate(&i))) {
129  if (parser->codec_ids[0] == codec_id ||
130  parser->codec_ids[1] == codec_id ||
131  parser->codec_ids[2] == codec_id ||
132  parser->codec_ids[3] == codec_id ||
133  parser->codec_ids[4] == codec_id)
134  goto found;
135  }
136  return NULL;
137 
138 found:
139  s = av_mallocz(sizeof(AVCodecParserContext));
140  if (!s)
141  goto err_out;
142  s->parser = (AVCodecParser*)parser;
143  s->priv_data = av_mallocz(parser->priv_data_size);
144  if (!s->priv_data)
145  goto err_out;
146  s->fetch_timestamp=1;
148  if (parser->parser_init) {
149  ret = parser->parser_init(s);
150  if (ret != 0)
151  goto err_out;
152  }
153  s->key_frame = -1;
154 #if FF_API_CONVERGENCE_DURATION
156  s->convergence_duration = 0;
158 #endif
159  s->dts_sync_point = INT_MIN;
160  s->dts_ref_dts_delta = INT_MIN;
161  s->pts_dts_delta = INT_MIN;
162  s->format = -1;
163 
164  return s;
165 
166 err_out:
167  if (s)
168  av_freep(&s->priv_data);
169  av_free(s);
170  return NULL;
171 }
172 
173 void ff_fetch_timestamp(AVCodecParserContext *s, int off, int remove, int fuzzy)
174 {
175  int i;
176 
177  if (!fuzzy) {
178  s->dts =
179  s->pts = AV_NOPTS_VALUE;
180  s->pos = -1;
181  s->offset = 0;
182  }
183  for (i = 0; i < AV_PARSER_PTS_NB; i++) {
184  if (s->cur_offset + off >= s->cur_frame_offset[i] &&
185  (s->frame_offset < s->cur_frame_offset[i] ||
186  (!s->frame_offset && !s->next_frame_offset)) && // first field/frame
187  // check disabled since MPEG-TS does not send complete PES packets
188  /*s->next_frame_offset + off <*/ s->cur_frame_end[i]){
189 
190  if (!fuzzy || s->cur_frame_dts[i] != AV_NOPTS_VALUE) {
191  s->dts = s->cur_frame_dts[i];
192  s->pts = s->cur_frame_pts[i];
193  s->pos = s->cur_frame_pos[i];
194  s->offset = s->next_frame_offset - s->cur_frame_offset[i];
195  }
196  if (remove)
197  s->cur_frame_offset[i] = INT64_MAX;
198  if (s->cur_offset + off < s->cur_frame_end[i])
199  break;
200  }
201  }
202 }
203 
205  uint8_t **poutbuf, int *poutbuf_size,
206  const uint8_t *buf, int buf_size,
207  int64_t pts, int64_t dts, int64_t pos)
208 {
209  int index, i;
211 
213 
214  /* Parsers only work for the specified codec ids. */
215  av_assert1(avctx->codec_id == s->parser->codec_ids[0] ||
216  avctx->codec_id == s->parser->codec_ids[1] ||
217  avctx->codec_id == s->parser->codec_ids[2] ||
218  avctx->codec_id == s->parser->codec_ids[3] ||
219  avctx->codec_id == s->parser->codec_ids[4]);
220 
221  if (!(s->flags & PARSER_FLAG_FETCHED_OFFSET)) {
222  s->next_frame_offset =
223  s->cur_offset = pos;
225  }
226 
227  if (buf_size == 0) {
228  /* padding is always necessary even if EOF, so we add it here */
229  memset(dummy_buf, 0, sizeof(dummy_buf));
230  buf = dummy_buf;
231  } else if (s->cur_offset + buf_size != s->cur_frame_end[s->cur_frame_start_index]) { /* skip remainder packets */
232  /* add a new packet descriptor */
233  i = (s->cur_frame_start_index + 1) & (AV_PARSER_PTS_NB - 1);
234  s->cur_frame_start_index = i;
235  s->cur_frame_offset[i] = s->cur_offset;
236  s->cur_frame_end[i] = s->cur_offset + buf_size;
237  s->cur_frame_pts[i] = pts;
238  s->cur_frame_dts[i] = dts;
239  s->cur_frame_pos[i] = pos;
240  }
241 
242  if (s->fetch_timestamp) {
243  s->fetch_timestamp = 0;
244  s->last_pts = s->pts;
245  s->last_dts = s->dts;
246  s->last_pos = s->pos;
247  ff_fetch_timestamp(s, 0, 0, 0);
248  }
249  /* WARNING: the returned index can be negative */
250  index = s->parser->parser_parse(s, avctx, (const uint8_t **) poutbuf,
251  poutbuf_size, buf, buf_size);
252  av_assert0(index > -0x20000000); // The API does not allow returning AVERROR codes
253 #define FILL(name) if(s->name > 0 && avctx->name <= 0) avctx->name = s->name
254  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
255  FILL(field_order);
256  }
257 
258  /* update the file pointer */
259  if (*poutbuf_size) {
260  /* fill the data for the current frame */
262 
263  /* offset of the next frame */
265  s->fetch_timestamp = 1;
266  }
267  if (index < 0)
268  index = 0;
269  s->cur_offset += index;
270  return index;
271 }
272 
274  uint8_t **poutbuf, int *poutbuf_size,
275  const uint8_t *buf, int buf_size, int keyframe)
276 {
277  if (s && s->parser->split) {
278  if (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER ||
280  int i = s->parser->split(avctx, buf, buf_size);
281  buf += i;
282  buf_size -= i;
283  }
284  }
285 
286  /* cast to avoid warning about discarding qualifiers */
287  *poutbuf = (uint8_t *) buf;
288  *poutbuf_size = buf_size;
289  if (avctx->extradata) {
290  if (keyframe && (avctx->flags2 & AV_CODEC_FLAG2_LOCAL_HEADER)) {
291  int size = buf_size + avctx->extradata_size;
292 
293  *poutbuf_size = size;
294  *poutbuf = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
295  if (!*poutbuf)
296  return AVERROR(ENOMEM);
297 
298  memcpy(*poutbuf, avctx->extradata, avctx->extradata_size);
299  memcpy(*poutbuf + avctx->extradata_size, buf,
300  buf_size + AV_INPUT_BUFFER_PADDING_SIZE);
301  return 1;
302  }
303  }
304 
305  return 0;
306 }
307 
309 {
310  if (s) {
311  if (s->parser->parser_close)
312  s->parser->parser_close(s);
313  av_freep(&s->priv_data);
314  av_free(s);
315  }
316 }
317 
318 int ff_combine_frame(ParseContext *pc, int next,
319  const uint8_t **buf, int *buf_size)
320 {
321  if (pc->overread) {
322  ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
323  pc->overread, pc->state, next, pc->index, pc->overread_index);
324  ff_dlog(NULL, "%X %X %X %X\n",
325  (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
326  }
327 
328  /* Copy overread bytes from last frame into buffer. */
329  for (; pc->overread > 0; pc->overread--)
330  pc->buffer[pc->index++] = pc->buffer[pc->overread_index++];
331 
332  /* flush remaining if EOF */
333  if (!*buf_size && next == END_NOT_FOUND)
334  next = 0;
335 
336  pc->last_index = pc->index;
337 
338  /* copy into buffer end return */
339  if (next == END_NOT_FOUND) {
340  void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
341  *buf_size + pc->index +
343 
344  if (!new_buffer) {
345  av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", *buf_size + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
346  pc->index = 0;
347  return AVERROR(ENOMEM);
348  }
349  pc->buffer = new_buffer;
350  memcpy(&pc->buffer[pc->index], *buf, *buf_size);
351  pc->index += *buf_size;
352  return -1;
353  }
354 
355  av_assert0(next >= 0 || pc->buffer);
356 
357  *buf_size =
358  pc->overread_index = pc->index + next;
359 
360  /* append to buffer */
361  if (pc->index) {
362  void *new_buffer = av_fast_realloc(pc->buffer, &pc->buffer_size,
363  next + pc->index +
365  if (!new_buffer) {
366  av_log(NULL, AV_LOG_ERROR, "Failed to reallocate parser buffer to %d\n", next + pc->index + AV_INPUT_BUFFER_PADDING_SIZE);
367  pc->overread_index =
368  pc->index = 0;
369  return AVERROR(ENOMEM);
370  }
371  pc->buffer = new_buffer;
372  if (next > -AV_INPUT_BUFFER_PADDING_SIZE)
373  memcpy(&pc->buffer[pc->index], *buf,
375  pc->index = 0;
376  *buf = pc->buffer;
377  }
378 
379  /* store overread bytes */
380  for (; next < 0; next++) {
381  pc->state = pc->state << 8 | pc->buffer[pc->last_index + next];
382  pc->state64 = pc->state64 << 8 | pc->buffer[pc->last_index + next];
383  pc->overread++;
384  }
385 
386  if (pc->overread) {
387  ff_dlog(NULL, "overread %d, state:%"PRIX32" next:%d index:%d o_index:%d\n",
388  pc->overread, pc->state, next, pc->index, pc->overread_index);
389  ff_dlog(NULL, "%X %X %X %X\n",
390  (*buf)[0], (*buf)[1], (*buf)[2], (*buf)[3]);
391  }
392 
393  return 0;
394 }
395 
397 {
398  ParseContext *pc = s->priv_data;
399 
400  av_freep(&pc->buffer);
401 }
402 
403 int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
404 {
405  uint32_t state = -1;
406  const uint8_t *ptr = buf, *end = buf + buf_size;
407 
408  while (ptr < end) {
409  ptr = avpriv_find_start_code(ptr, end, &state);
410  if (state == 0x1B3 || state == 0x1B6)
411  return ptr - 4 - buf;
412  }
413 
414  return 0;
415 }
AVCodecParser ff_mlp_parser
Definition: mlp_parser.c:394
int(* parser_init)(AVCodecParserContext *s)
Definition: avcodec.h:5153
#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:113
Memory handling functions.
int64_t next_frame_offset
Definition: avcodec.h:4989
AVCodecParser ff_mpeg4video_parser
int codec_ids[5]
Definition: avcodec.h:5151
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:5088
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:5067
static AVOnce av_parser_next_init
Definition: parser.c:79
int ff_mpeg4video_split(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: parser.c:403
#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:273
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:1603
AVCodecParser ff_g729_parser
Definition: g729_parser.c:77
AVCodecParser ff_dnxhd_parser
Definition: dnxhd_parser.c:138
static struct @281 state
struct AVCodecParser * next
Definition: avcodec.h:5162
#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:5156
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:5093
int priv_data_size
Definition: avcodec.h:5152
struct AVCodecParser * parser
Definition: avcodec.h:4985
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:318
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 * av_parser_next(const AVCodecParser *p)
Definition: parser.c:92
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1583
simple assert() macros that are a bit more flexible than ISO C assert().
static void av_parser_init_next(void)
Definition: parser.c:81
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:5014
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:396
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:204
void(* parser_close)(AVCodecParserContext *s)
Definition: avcodec.h:5160
AVCodecParser ff_aac_latm_parser
Definition: latm_parser.c:107
int64_t offset
byte offset from starting packet start
Definition: avcodec.h:5023
AVCodecParser ff_dvaudio_parser
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:308
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
int64_t cur_frame_end[AV_PARSER_PTS_NB]
Definition: avcodec.h:5024
int64_t cur_frame_pts[AV_PARSER_PTS_NB]
Definition: avcodec.h:5013
int64_t last_pos
Previous frame byte position.
Definition: avcodec.h:5098
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:5081
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:118
uint8_t * buffer
Definition: parser.h:29
enum AVMediaType codec_type
Definition: avcodec.h:1511
enum AVCodecID codec_id
Definition: avcodec.h:1513
const AVCodecParser * av_parser_iterate(void **opaque)
Iterate over all registered codec parsers.
Definition: parser.c:102
AVCodecParser ff_vp3_parser
Definition: vp3_parser.c:38
main external API structure.
Definition: avcodec.h:1503
#define AV_CODEC_FLAG2_LOCAL_HEADER
Place global headers at every keyframe instead of in extradata.
Definition: avcodec.h:907
#define PARSER_FLAG_FETCHED_OFFSET
Set if the parser has a valid file offset.
Definition: avcodec.h:5020
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:1604
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:5010
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:5039
static int64_t pts
#define AV_CODEC_FLAG_GLOBAL_HEADER
Place global headers in extradata instead of every keyframe.
Definition: avcodec.h:880
int(* split)(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: avcodec.h:5161
#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:5012
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:771
AVCodecParser ff_tak_parser
Definition: tak_parser.c:121
int64_t frame_offset
Definition: avcodec.h:4986
#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:5147
AVCodecParser ff_dvbsub_parser
AVCodecParser ff_mjpeg_parser
Definition: mjpeg_parser.c:129
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1590
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:173
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:5032
int dts_sync_point
Synchronization point for start of timestamp generation.
Definition: avcodec.h:5052
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248