FFmpeg
demux.c
Go to the documentation of this file.
1 /*
2  * Core demuxing component
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 
24 #include "config_components.h"
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/intreadwrite.h"
31 #include "libavutil/mathematics.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/time.h"
35 #include "libavutil/timestamp.h"
36 
37 #include "libavcodec/avcodec.h"
38 #include "libavcodec/bsf.h"
39 #include "libavcodec/internal.h"
41 #include "libavcodec/raw.h"
42 
43 #include "avformat.h"
44 #include "avio_internal.h"
45 #include "demux.h"
46 #include "id3v2.h"
47 #include "internal.h"
48 #include "url.h"
49 
50 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
51 {
52  const FFStream *const sti = cffstream(st);
53  if (sti->pts_wrap_behavior != AV_PTS_WRAP_IGNORE && st->pts_wrap_bits < 64 &&
54  sti->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
56  timestamp < sti->pts_wrap_reference)
57  return timestamp + (1ULL << st->pts_wrap_bits);
58  else if (sti->pts_wrap_behavior == AV_PTS_WRAP_SUB_OFFSET &&
59  timestamp >= sti->pts_wrap_reference)
60  return timestamp - (1ULL << st->pts_wrap_bits);
61  }
62  return timestamp;
63 }
64 
65 int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
66 {
67  return wrap_timestamp(st, timestamp);
68 }
69 
71 {
72  const AVCodec *codec;
73 
74 #if CONFIG_H264_DECODER
75  /* Other parts of the code assume this decoder to be used for h264,
76  * so force it if possible. */
78  return avcodec_find_decoder_by_name("h264");
79 #endif
80 
81  codec = ff_find_decoder(s, st, codec_id);
82  if (!codec)
83  return NULL;
84 
86  const AVCodec *probe_codec = NULL;
87  void *iter = NULL;
88  while ((probe_codec = av_codec_iterate(&iter))) {
89  if (probe_codec->id == codec->id &&
92  return probe_codec;
93  }
94  }
95  }
96 
97  return codec;
98 }
99 
101  AVProbeData *pd)
102 {
103  static const struct {
104  const char *name;
105  enum AVCodecID id;
106  enum AVMediaType type;
107  } fmt_id_type[] = {
119  { "mjpeg_2000", AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
121  { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
122  { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
123  { 0 }
124  };
125  int score;
126  const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
127  FFStream *const sti = ffstream(st);
128 
129  if (fmt) {
131  "Probe with size=%d, packets=%d detected %s with score=%d\n",
132  pd->buf_size, s->max_probe_packets - sti->probe_packets,
133  fmt->name, score);
134  for (int i = 0; fmt_id_type[i].name; i++) {
135  if (!strcmp(fmt->name, fmt_id_type[i].name)) {
136  if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
137  st->codecpar->sample_rate)
138  continue;
139  if (sti->request_probe > score &&
140  st->codecpar->codec_id != fmt_id_type[i].id)
141  continue;
142  st->codecpar->codec_id = fmt_id_type[i].id;
143  st->codecpar->codec_type = fmt_id_type[i].type;
144  sti->need_context_update = 1;
145  return score;
146  }
147  }
148  }
149  return 0;
150 }
151 
152 static int init_input(AVFormatContext *s, const char *filename,
154 {
155  int ret;
156  AVProbeData pd = { filename, NULL, 0 };
157  int score = AVPROBE_SCORE_RETRY;
158 
159  if (s->pb) {
160  s->flags |= AVFMT_FLAG_CUSTOM_IO;
161  if (!s->iformat)
162  return av_probe_input_buffer2(s->pb, &s->iformat, filename,
163  s, 0, s->format_probesize);
164  else if (s->iformat->flags & AVFMT_NOFILE)
165  av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
166  "will be ignored with AVFMT_NOFILE format.\n");
167  return 0;
168  }
169 
170  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
171  (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
172  return score;
173 
174  if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
175  return ret;
176 
177  if (s->iformat)
178  return 0;
179  return av_probe_input_buffer2(s->pb, &s->iformat, filename,
180  s, 0, s->format_probesize);
181 }
182 
184 {
185  int ret;
186  for (unsigned i = 0; i < s->nb_streams; i++) {
187  AVStream *const st = s->streams[i];
188  FFStream *const sti = ffstream(st);
189 
190  if (!sti->need_context_update)
191  continue;
192 
193  /* close parser, because it depends on the codec */
194  if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
195  av_parser_close(sti->parser);
196  sti->parser = NULL;
197  }
198 
199 #if FF_API_OLD_CHANNEL_LAYOUT
201  if (st->codecpar->ch_layout.nb_channels &&
202  !st->codecpar->channels) {
203  st->codecpar->channels = st->codecpar->ch_layout.nb_channels;
204  st->codecpar->channel_layout = st->codecpar->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
205  st->codecpar->ch_layout.u.mask : 0;
206 
207  }
209 #endif
210 
211  /* update internal codec context, for the parser */
213  if (ret < 0)
214  return ret;
215 
217 
218  sti->need_context_update = 0;
219  }
220  return 0;
221 }
222 
223 int avformat_open_input(AVFormatContext **ps, const char *filename,
224  const AVInputFormat *fmt, AVDictionary **options)
225 {
226  AVFormatContext *s = *ps;
227  FFFormatContext *si;
228  AVDictionary *tmp = NULL;
229  ID3v2ExtraMeta *id3v2_extra_meta = NULL;
230  int ret = 0;
231 
232  if (!s && !(s = avformat_alloc_context()))
233  return AVERROR(ENOMEM);
234  si = ffformatcontext(s);
235  if (!s->av_class) {
236  av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
237  return AVERROR(EINVAL);
238  }
239  if (fmt)
240  s->iformat = fmt;
241 
242  if (options)
243  av_dict_copy(&tmp, *options, 0);
244 
245  if (s->pb) // must be before any goto fail
246  s->flags |= AVFMT_FLAG_CUSTOM_IO;
247 
248  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
249  goto fail;
250 
251  if (!(s->url = av_strdup(filename ? filename : ""))) {
252  ret = AVERROR(ENOMEM);
253  goto fail;
254  }
255 
256  if ((ret = init_input(s, filename, &tmp)) < 0)
257  goto fail;
258  s->probe_score = ret;
259 
260  if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
261  s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
262  if (!s->protocol_whitelist) {
263  ret = AVERROR(ENOMEM);
264  goto fail;
265  }
266  }
267 
268  if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
269  s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
270  if (!s->protocol_blacklist) {
271  ret = AVERROR(ENOMEM);
272  goto fail;
273  }
274  }
275 
276  if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
277  av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
278  ret = AVERROR(EINVAL);
279  goto fail;
280  }
281 
282  avio_skip(s->pb, s->skip_initial_bytes);
283 
284  /* Check filename in case an image number is expected. */
285  if (s->iformat->flags & AVFMT_NEEDNUMBER) {
286  if (!av_filename_number_test(filename)) {
287  ret = AVERROR(EINVAL);
288  goto fail;
289  }
290  }
291 
292  s->duration = s->start_time = AV_NOPTS_VALUE;
293 
294  /* Allocate private data. */
295  if (s->iformat->priv_data_size > 0) {
296  if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
297  ret = AVERROR(ENOMEM);
298  goto fail;
299  }
300  if (s->iformat->priv_class) {
301  *(const AVClass **) s->priv_data = s->iformat->priv_class;
302  av_opt_set_defaults(s->priv_data);
303  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
304  goto fail;
305  }
306  }
307 
308  /* e.g. AVFMT_NOFILE formats will not have an AVIOContext */
309  if (s->pb)
310  ff_id3v2_read_dict(s->pb, &si->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
311 
312  if (s->iformat->read_header)
313  if ((ret = s->iformat->read_header(s)) < 0) {
314  if (s->iformat->flags_internal & FF_FMT_INIT_CLEANUP)
315  goto close;
316  goto fail;
317  }
318 
319  if (!s->metadata) {
320  s->metadata = si->id3v2_meta;
321  si->id3v2_meta = NULL;
322  } else if (si->id3v2_meta) {
323  av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
324  av_dict_free(&si->id3v2_meta);
325  }
326 
327  if (id3v2_extra_meta) {
328  if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") ||
329  !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
330  if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
331  goto close;
332  if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
333  goto close;
334  if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
335  goto close;
336  } else
337  av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
338  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
339  }
340 
342  goto close;
343 
344  if (s->pb && !si->data_offset)
345  si->data_offset = avio_tell(s->pb);
346 
347  si->raw_packet_buffer_size = 0;
348 
350 
351  if (options) {
353  *options = tmp;
354  }
355  *ps = s;
356  return 0;
357 
358 close:
359  if (s->iformat->read_close)
360  s->iformat->read_close(s);
361 fail:
362  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
363  av_dict_free(&tmp);
364  if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
365  avio_closep(&s->pb);
367  *ps = NULL;
368  return ret;
369 }
370 
372 {
374  AVIOContext *pb;
375 
376  if (!ps || !*ps)
377  return;
378 
379  s = *ps;
380  pb = s->pb;
381 
382  if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
383  (s->flags & AVFMT_FLAG_CUSTOM_IO))
384  pb = NULL;
385 
386  if (s->iformat)
387  if (s->iformat->read_close)
388  s->iformat->read_close(s);
389 
391 
392  *ps = NULL;
393 
394  avio_close(pb);
395 }
396 
398 {
399  switch (st->codecpar->codec_type) {
400  case AVMEDIA_TYPE_VIDEO:
401  if (s->video_codec_id)
402  st->codecpar->codec_id = s->video_codec_id;
403  break;
404  case AVMEDIA_TYPE_AUDIO:
405  if (s->audio_codec_id)
406  st->codecpar->codec_id = s->audio_codec_id;
407  break;
409  if (s->subtitle_codec_id)
410  st->codecpar->codec_id = s->subtitle_codec_id;
411  break;
412  case AVMEDIA_TYPE_DATA:
413  if (s->data_codec_id)
414  st->codecpar->codec_id = s->data_codec_id;
415  break;
416  }
417 }
418 
420 {
421  FFFormatContext *const si = ffformatcontext(s);
422  FFStream *const sti = ffstream(st);
423 
424  if (sti->request_probe > 0) {
425  AVProbeData *const pd = &sti->probe_data;
426  int end;
427  av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, sti->probe_packets);
428  --sti->probe_packets;
429 
430  if (pkt) {
431  uint8_t *new_buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
432  if (!new_buf) {
434  "Failed to reallocate probe buffer for stream %d\n",
435  st->index);
436  goto no_packet;
437  }
438  pd->buf = new_buf;
439  memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
440  pd->buf_size += pkt->size;
441  memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
442  } else {
443 no_packet:
444  sti->probe_packets = 0;
445  if (!pd->buf_size) {
447  "nothing to probe for stream %d\n", st->index);
448  }
449  }
450 
451  end = si->raw_packet_buffer_size >= s->probesize
452  || sti->probe_packets <= 0;
453 
454  if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
455  int score = set_codec_from_probe_data(s, st, pd);
457  || end) {
458  pd->buf_size = 0;
459  av_freep(&pd->buf);
460  sti->request_probe = -1;
461  if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
462  av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
463  } else
464  av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
465  }
466  force_codec_ids(s, st);
467  }
468  }
469  return 0;
470 }
471 
472 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
473 {
474  FFStream *const sti = ffstream(st);
475  int64_t ref = pkt->dts;
476  int pts_wrap_behavior;
477  int64_t pts_wrap_reference;
478  AVProgram *first_program;
479 
480  if (ref == AV_NOPTS_VALUE)
481  ref = pkt->pts;
482  if (sti->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
483  return 0;
484  ref &= (1LL << st->pts_wrap_bits)-1;
485 
486  // reference time stamp should be 60 s before first time stamp
487  pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
488  // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
489  pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
490  (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
492 
493  first_program = av_find_program_from_stream(s, NULL, stream_index);
494 
495  if (!first_program) {
496  int default_stream_index = av_find_default_stream_index(s);
497  FFStream *const default_sti = ffstream(s->streams[default_stream_index]);
498  if (default_sti->pts_wrap_reference == AV_NOPTS_VALUE) {
499  for (unsigned i = 0; i < s->nb_streams; i++) {
500  FFStream *const sti = ffstream(s->streams[i]);
502  continue;
503  sti->pts_wrap_reference = pts_wrap_reference;
504  sti->pts_wrap_behavior = pts_wrap_behavior;
505  }
506  } else {
507  sti->pts_wrap_reference = default_sti->pts_wrap_reference;
508  sti->pts_wrap_behavior = default_sti->pts_wrap_behavior;
509  }
510  } else {
511  AVProgram *program = first_program;
512  while (program) {
513  if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
514  pts_wrap_reference = program->pts_wrap_reference;
515  pts_wrap_behavior = program->pts_wrap_behavior;
516  break;
517  }
518  program = av_find_program_from_stream(s, program, stream_index);
519  }
520 
521  // update every program with differing pts_wrap_reference
522  program = first_program;
523  while (program) {
524  if (program->pts_wrap_reference != pts_wrap_reference) {
525  for (unsigned i = 0; i < program->nb_stream_indexes; i++) {
526  FFStream *const sti = ffstream(s->streams[program->stream_index[i]]);
527  sti->pts_wrap_reference = pts_wrap_reference;
528  sti->pts_wrap_behavior = pts_wrap_behavior;
529  }
530 
531  program->pts_wrap_reference = pts_wrap_reference;
532  program->pts_wrap_behavior = pts_wrap_behavior;
533  }
534  program = av_find_program_from_stream(s, program, stream_index);
535  }
536  }
537  return 1;
538 }
539 
541 {
542  FFFormatContext *const si = ffformatcontext(s);
543  int err;
544 
545 #if FF_API_INIT_PACKET
547  pkt->data = NULL;
548  pkt->size = 0;
549  av_init_packet(pkt);
551 #else
553 #endif
554 
555  for (;;) {
557  AVStream *st;
558  FFStream *sti;
559  const AVPacket *pkt1;
560 
561  if (pktl) {
562  AVStream *const st = s->streams[pktl->pkt.stream_index];
563  if (si->raw_packet_buffer_size >= s->probesize)
564  if ((err = probe_codec(s, st, NULL)) < 0)
565  return err;
566  if (ffstream(st)->request_probe <= 0) {
569  return 0;
570  }
571  }
572 
573  err = s->iformat->read_packet(s, pkt);
574  if (err < 0) {
576 
577  /* Some demuxers return FFERROR_REDO when they consume
578  data and discard it (ignored streams, junk, extradata).
579  We must re-call the demuxer to get the real packet. */
580  if (err == FFERROR_REDO)
581  continue;
582  if (!pktl || err == AVERROR(EAGAIN))
583  return err;
584  for (unsigned i = 0; i < s->nb_streams; i++) {
585  AVStream *const st = s->streams[i];
586  FFStream *const sti = ffstream(st);
587  if (sti->probe_packets || sti->request_probe > 0)
588  if ((err = probe_codec(s, st, NULL)) < 0)
589  return err;
590  av_assert0(sti->request_probe <= 0);
591  }
592  continue;
593  }
594 
596  if (err < 0) {
598  return err;
599  }
600 
601  if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
603  "Packet corrupt (stream = %d, dts = %s)",
605  if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
606  av_log(s, AV_LOG_WARNING, ", dropping it.\n");
608  continue;
609  }
610  av_log(s, AV_LOG_WARNING, ".\n");
611  }
612 
613  av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
614  "Invalid stream index.\n");
615 
616  st = s->streams[pkt->stream_index];
617  sti = ffstream(st);
618 
620  // correct first time stamps to negative values
621  if (!is_relative(sti->first_dts))
622  sti->first_dts = wrap_timestamp(st, sti->first_dts);
623  if (!is_relative(st->start_time))
624  st->start_time = wrap_timestamp(st, st->start_time);
625  if (!is_relative(sti->cur_dts))
626  sti->cur_dts = wrap_timestamp(st, sti->cur_dts);
627  }
628 
629  pkt->dts = wrap_timestamp(st, pkt->dts);
630  pkt->pts = wrap_timestamp(st, pkt->pts);
631 
632  force_codec_ids(s, st);
633 
634  /* TODO: audio: time filter; video: frame reordering (pts != dts) */
635  if (s->use_wallclock_as_timestamps)
637 
638  if (!pktl && sti->request_probe <= 0)
639  return 0;
640 
642  pkt, NULL, 0);
643  if (err < 0) {
645  return err;
646  }
647  pkt1 = &si->raw_packet_buffer.tail->pkt;
648  si->raw_packet_buffer_size += pkt1->size;
649 
650  if ((err = probe_codec(s, st, pkt1)) < 0)
651  return err;
652  }
653 }
654 
655 /**
656  * Return the frame duration in seconds. Return 0 if not available.
657  */
658 static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden,
660  AVPacket *pkt)
661 {
662  FFStream *const sti = ffstream(st);
663  AVRational codec_framerate = sti->avctx->framerate;
664  int frame_size, sample_rate;
665 
666  *pnum = 0;
667  *pden = 0;
668  switch (st->codecpar->codec_type) {
669  case AVMEDIA_TYPE_VIDEO:
670  if (st->r_frame_rate.num && (!pc || !codec_framerate.num)) {
671  *pnum = st->r_frame_rate.den;
672  *pden = st->r_frame_rate.num;
673  } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) &&
674  !codec_framerate.num &&
675  st->avg_frame_rate.num && st->avg_frame_rate.den) {
676  *pnum = st->avg_frame_rate.den;
677  *pden = st->avg_frame_rate.num;
678  } else if (st->time_base.num * 1000LL > st->time_base.den) {
679  *pnum = st->time_base.num;
680  *pden = st->time_base.den;
681  } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
682  int ticks_per_frame = (sti->codec_desc &&
683  (sti->codec_desc->props & AV_CODEC_PROP_FIELDS)) ? 2 : 1;
684  av_reduce(pnum, pden,
685  codec_framerate.den,
686  codec_framerate.num * (int64_t)ticks_per_frame,
687  INT_MAX);
688 
689  if (pc && pc->repeat_pict) {
690  av_reduce(pnum, pden,
691  (*pnum) * (1LL + pc->repeat_pict),
692  (*pden),
693  INT_MAX);
694  }
695  /* If this codec can be interlaced or progressive then we need
696  * a parser to compute duration of a packet. Thus if we have
697  * no parser in such case leave duration undefined. */
698  if (sti->codec_desc &&
699  (sti->codec_desc->props & AV_CODEC_PROP_FIELDS) && !pc)
700  *pnum = *pden = 0;
701  }
702  break;
703  case AVMEDIA_TYPE_AUDIO:
704  if (sti->avctx_inited) {
706  sample_rate = sti->avctx->sample_rate;
707  } else {
710  }
711  if (frame_size <= 0 || sample_rate <= 0)
712  break;
713  *pnum = frame_size;
714  *pden = sample_rate;
715  break;
716  default:
717  break;
718  }
719 }
720 
722 {
723  FFStream *const sti = ffstream(st);
724  if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
725  if (!sti->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
726  return 1;
727 #if CONFIG_H264_DECODER
728  if (sti->avctx->has_b_frames &&
730  return 1;
731 #endif
732  if (sti->avctx->has_b_frames < 3)
733  return sti->nb_decoded_frames >= 7;
734  else if (sti->avctx->has_b_frames < 4)
735  return sti->nb_decoded_frames >= 18;
736  else
737  return sti->nb_decoded_frames >= 20;
738 }
739 
741  PacketListEntry *pktl)
742 {
743  FFFormatContext *const si = ffformatcontext(s);
744  if (pktl->next)
745  return pktl->next;
746  if (pktl == si->packet_buffer.tail)
747  return si->parse_queue.head;
748  return NULL;
749 }
750 
751 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
752 {
753  FFStream *const sti = ffstream(st);
754  int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
756 
757  if (!onein_oneout) {
758  int delay = sti->avctx->has_b_frames;
759 
760  if (dts == AV_NOPTS_VALUE) {
761  int64_t best_score = INT64_MAX;
762  for (int i = 0; i < delay; i++) {
763  if (sti->pts_reorder_error_count[i]) {
764  int64_t score = sti->pts_reorder_error[i] / sti->pts_reorder_error_count[i];
765  if (score < best_score) {
766  best_score = score;
767  dts = pts_buffer[i];
768  }
769  }
770  }
771  } else {
772  for (int i = 0; i < delay; i++) {
773  if (pts_buffer[i] != AV_NOPTS_VALUE) {
774  int64_t diff = FFABS(pts_buffer[i] - dts)
775  + (uint64_t)sti->pts_reorder_error[i];
776  diff = FFMAX(diff, sti->pts_reorder_error[i]);
777  sti->pts_reorder_error[i] = diff;
778  sti->pts_reorder_error_count[i]++;
779  if (sti->pts_reorder_error_count[i] > 250) {
780  sti->pts_reorder_error[i] >>= 1;
781  sti->pts_reorder_error_count[i] >>= 1;
782  }
783  }
784  }
785  }
786  }
787 
788  if (dts == AV_NOPTS_VALUE)
789  dts = pts_buffer[0];
790 
791  return dts;
792 }
793 
794 /**
795  * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
796  * of the packets in a window.
797  */
798 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
799  PacketListEntry *pkt_buffer)
800 {
801  AVStream *const st = s->streams[stream_index];
802  int delay = ffstream(st)->avctx->has_b_frames;
803 
804  int64_t pts_buffer[MAX_REORDER_DELAY+1];
805 
806  for (int i = 0; i < MAX_REORDER_DELAY + 1; i++)
807  pts_buffer[i] = AV_NOPTS_VALUE;
808 
809  for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
810  if (pkt_buffer->pkt.stream_index != stream_index)
811  continue;
812 
813  if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
814  pts_buffer[0] = pkt_buffer->pkt.pts;
815  for (int i = 0; i < delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
816  FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
817 
818  pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
819  }
820  }
821 }
822 
823 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
824  int64_t dts, int64_t pts, AVPacket *pkt)
825 {
826  FFFormatContext *const si = ffformatcontext(s);
827  AVStream *const st = s->streams[stream_index];
828  FFStream *const sti = ffstream(st);
830 
831  uint64_t shift;
832 
833  if (sti->first_dts != AV_NOPTS_VALUE ||
834  dts == AV_NOPTS_VALUE ||
835  sti->cur_dts == AV_NOPTS_VALUE ||
836  sti->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
837  dts < INT_MIN + (sti->cur_dts - RELATIVE_TS_BASE) ||
838  is_relative(dts))
839  return;
840 
841  sti->first_dts = dts - (sti->cur_dts - RELATIVE_TS_BASE);
842  sti->cur_dts = dts;
843  shift = (uint64_t)sti->first_dts - RELATIVE_TS_BASE;
844 
845  if (is_relative(pts))
846  pts += shift;
847 
848  for (PacketListEntry *pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
849  if (pktl_it->pkt.stream_index != stream_index)
850  continue;
851  if (is_relative(pktl_it->pkt.pts))
852  pktl_it->pkt.pts += shift;
853 
854  if (is_relative(pktl_it->pkt.dts))
855  pktl_it->pkt.dts += shift;
856 
857  if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
858  st->start_time = pktl_it->pkt.pts;
860  st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
861  }
862  }
863 
865  update_dts_from_pts(s, stream_index, pktl);
866 
867  if (st->start_time == AV_NOPTS_VALUE) {
869  st->start_time = pts;
870  }
872  st->start_time = av_sat_add64(st->start_time, av_rescale_q(sti->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
873  }
874 }
875 
877  int stream_index, int64_t duration)
878 {
879  FFFormatContext *const si = ffformatcontext(s);
880  FFStream *const sti = ffstream(st);
882  int64_t cur_dts = RELATIVE_TS_BASE;
883 
884  if (sti->first_dts != AV_NOPTS_VALUE) {
886  return;
888  cur_dts = sti->first_dts;
889  for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
890  if (pktl->pkt.stream_index == stream_index) {
891  if (pktl->pkt.pts != pktl->pkt.dts ||
892  pktl->pkt.dts != AV_NOPTS_VALUE ||
893  pktl->pkt.duration)
894  break;
895  cur_dts -= duration;
896  }
897  }
898  if (pktl && pktl->pkt.dts != sti->first_dts) {
899  av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
900  av_ts2str(sti->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
901  return;
902  }
903  if (!pktl) {
904  av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(sti->first_dts));
905  return;
906  }
907  pktl = si->packet_buffer.head ? si->packet_buffer.head : si->parse_queue.head;
908  sti->first_dts = cur_dts;
909  } else if (sti->cur_dts != RELATIVE_TS_BASE)
910  return;
911 
912  for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
913  if (pktl->pkt.stream_index != stream_index)
914  continue;
915  if ((pktl->pkt.pts == pktl->pkt.dts ||
916  pktl->pkt.pts == AV_NOPTS_VALUE) &&
917  (pktl->pkt.dts == AV_NOPTS_VALUE ||
918  pktl->pkt.dts == sti->first_dts ||
919  pktl->pkt.dts == RELATIVE_TS_BASE) &&
920  !pktl->pkt.duration &&
921  av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
922  ) {
923  pktl->pkt.dts = cur_dts;
924  if (!sti->avctx->has_b_frames)
925  pktl->pkt.pts = cur_dts;
926  pktl->pkt.duration = duration;
927  } else
928  break;
929  cur_dts = pktl->pkt.dts + pktl->pkt.duration;
930  }
931  if (!pktl)
932  sti->cur_dts = cur_dts;
933 }
934 
937  int64_t next_dts, int64_t next_pts)
938 {
939  FFFormatContext *const si = ffformatcontext(s);
940  FFStream *const sti = ffstream(st);
941  int num, den, presentation_delayed, delay;
942  int64_t offset;
944  int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
946 
947  if (s->flags & AVFMT_FLAG_NOFILLIN)
948  return;
949 
951  if (pkt->dts == pkt->pts && sti->last_dts_for_order_check != AV_NOPTS_VALUE) {
952  if (sti->last_dts_for_order_check <= pkt->dts) {
953  sti->dts_ordered++;
954  } else {
956  "DTS %"PRIi64" < %"PRIi64" out of order\n",
957  pkt->dts,
959  sti->dts_misordered++;
960  }
961  if (sti->dts_ordered + sti->dts_misordered > 250) {
962  sti->dts_ordered >>= 1;
963  sti->dts_misordered >>= 1;
964  }
965  }
966 
968  if (sti->dts_ordered < 8 * sti->dts_misordered && pkt->dts == pkt->pts)
970  }
971 
972  if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
974 
975  if (pc && pc->pict_type == AV_PICTURE_TYPE_B
976  && !sti->avctx->has_b_frames)
977  //FIXME Set low_delay = 0 when has_b_frames = 1
978  sti->avctx->has_b_frames = 1;
979 
980  /* do we have a video B-frame ? */
981  delay = sti->avctx->has_b_frames;
982  presentation_delayed = 0;
983 
984  /* XXX: need has_b_frame, but cannot get it if the codec is
985  * not initialized */
986  if (delay &&
987  pc && pc->pict_type != AV_PICTURE_TYPE_B)
988  presentation_delayed = 1;
989 
990  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
991  st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
992  pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
993  if (is_relative(sti->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > sti->cur_dts) {
994  pkt->dts -= 1LL << st->pts_wrap_bits;
995  } else
996  pkt->pts += 1LL << st->pts_wrap_bits;
997  }
998 
999  /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1000  * We take the conservative approach and discard both.
1001  * Note: If this is misbehaving for an H.264 file, then possibly
1002  * presentation_delayed is not set correctly. */
1003  if (delay == 1 && pkt->dts == pkt->pts &&
1004  pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1005  av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1006  if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1007  && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1008  pkt->dts = AV_NOPTS_VALUE;
1009  }
1010 
1012  if (pkt->duration <= 0) {
1013  compute_frame_duration(s, &num, &den, st, pc, pkt);
1014  if (den && num) {
1015  duration = (AVRational) {num, den};
1017  num * (int64_t) st->time_base.den,
1018  den * (int64_t) st->time_base.num,
1019  AV_ROUND_DOWN);
1020  }
1021  }
1022 
1023  if (pkt->duration > 0 && (si->packet_buffer.head || si->parse_queue.head))
1025 
1026  /* Correct timestamps with byte offset if demuxers only have timestamps
1027  * on packet boundaries */
1028  if (pc && sti->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1029  /* this will estimate bitrate based on this frame's duration and size */
1031  if (pkt->pts != AV_NOPTS_VALUE)
1032  pkt->pts += offset;
1033  if (pkt->dts != AV_NOPTS_VALUE)
1034  pkt->dts += offset;
1035  }
1036 
1037  /* This may be redundant, but it should not hurt. */
1038  if (pkt->dts != AV_NOPTS_VALUE &&
1039  pkt->pts != AV_NOPTS_VALUE &&
1040  pkt->pts > pkt->dts)
1041  presentation_delayed = 1;
1042 
1043  if (s->debug & FF_FDEBUG_TS)
1045  "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1046  presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts),
1047  pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1048 
1049  /* Interpolate PTS and DTS if they are not present. We skip H264
1050  * currently because delay and has_b_frames are not reliably set. */
1051  if ((delay == 0 || (delay == 1 && pc)) &&
1052  onein_oneout) {
1053  if (presentation_delayed) {
1054  /* DTS = decompression timestamp */
1055  /* PTS = presentation timestamp */
1056  if (pkt->dts == AV_NOPTS_VALUE)
1057  pkt->dts = sti->last_IP_pts;
1059  if (pkt->dts == AV_NOPTS_VALUE)
1060  pkt->dts = sti->cur_dts;
1061 
1062  /* This is tricky: the dts must be incremented by the duration
1063  * of the frame we are displaying, i.e. the last I- or P-frame. */
1064  if (sti->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1065  sti->last_IP_duration = pkt->duration;
1066  if (pkt->dts != AV_NOPTS_VALUE)
1067  sti->cur_dts = av_sat_add64(pkt->dts, sti->last_IP_duration);
1068  if (pkt->dts != AV_NOPTS_VALUE &&
1069  pkt->pts == AV_NOPTS_VALUE &&
1070  sti->last_IP_duration > 0 &&
1071  ((uint64_t)sti->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1072  next_dts != next_pts &&
1073  next_pts != AV_NOPTS_VALUE)
1074  pkt->pts = next_dts;
1075 
1076  if ((uint64_t)pkt->duration <= INT32_MAX)
1077  sti->last_IP_duration = pkt->duration;
1078  sti->last_IP_pts = pkt->pts;
1079  /* Cannot compute PTS if not present (we can compute it only
1080  * by knowing the future. */
1081  } else if (pkt->pts != AV_NOPTS_VALUE ||
1082  pkt->dts != AV_NOPTS_VALUE ||
1083  pkt->duration > 0 ) {
1084 
1085  /* presentation is not delayed : PTS and DTS are the same */
1086  if (pkt->pts == AV_NOPTS_VALUE)
1087  pkt->pts = pkt->dts;
1089  pkt->pts, pkt);
1090  if (pkt->pts == AV_NOPTS_VALUE)
1091  pkt->pts = sti->cur_dts;
1092  pkt->dts = pkt->pts;
1093  if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1094  sti->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1095  }
1096  }
1097 
1098  if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1099  sti->pts_buffer[0] = pkt->pts;
1100  for (int i = 0; i < delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
1101  FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
1102 
1105  }
1106  // We skipped it above so we try here.
1107  if (!onein_oneout)
1108  // This should happen on the first packet
1110  if (pkt->dts > sti->cur_dts)
1111  sti->cur_dts = pkt->dts;
1112 
1113  if (s->debug & FF_FDEBUG_TS)
1114  av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1115  presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), st->index, st->id);
1116 
1117  /* update flags */
1120 }
1121 
1122 /**
1123  * Parse a packet, add all split parts to parse_queue.
1124  *
1125  * @param pkt Packet to parse; must not be NULL.
1126  * @param flush Indicates whether to flush. If set, pkt must be blank.
1127  */
1129  int stream_index, int flush)
1130 {
1131  FFFormatContext *const si = ffformatcontext(s);
1132  AVPacket *out_pkt = si->parse_pkt;
1133  AVStream *st = s->streams[stream_index];
1134  FFStream *const sti = ffstream(st);
1135  const uint8_t *data = pkt->data;
1136  int size = pkt->size;
1137  int ret = 0, got_output = flush;
1138 
1139  if (!size && !flush && sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1140  // preserve 0-size sync packets
1142  }
1143 
1144  while (size > 0 || (flush && got_output)) {
1145  int64_t next_pts = pkt->pts;
1146  int64_t next_dts = pkt->dts;
1147  int len;
1148 
1149  len = av_parser_parse2(sti->parser, sti->avctx,
1150  &out_pkt->data, &out_pkt->size, data, size,
1151  pkt->pts, pkt->dts, pkt->pos);
1152 
1153  pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1154  pkt->pos = -1;
1155  /* increment read pointer */
1156  av_assert1(data || !len);
1157  data = len ? data + len : data;
1158  size -= len;
1159 
1160  got_output = !!out_pkt->size;
1161 
1162  if (!out_pkt->size)
1163  continue;
1164 
1165  if (pkt->buf && out_pkt->data == pkt->data) {
1166  /* reference pkt->buf only when out_pkt->data is guaranteed to point
1167  * to data in it and not in the parser's internal buffer. */
1168  /* XXX: Ensure this is the case with all parsers when sti->parser->flags
1169  * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1170  out_pkt->buf = av_buffer_ref(pkt->buf);
1171  if (!out_pkt->buf) {
1172  ret = AVERROR(ENOMEM);
1173  goto fail;
1174  }
1175  } else {
1176  ret = av_packet_make_refcounted(out_pkt);
1177  if (ret < 0)
1178  goto fail;
1179  }
1180 
1181  if (pkt->side_data) {
1182  out_pkt->side_data = pkt->side_data;
1183  out_pkt->side_data_elems = pkt->side_data_elems;
1184  pkt->side_data = NULL;
1185  pkt->side_data_elems = 0;
1186  }
1187 
1188  /* set the duration */
1189  out_pkt->duration = (sti->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1191  if (sti->avctx->sample_rate > 0) {
1192  out_pkt->duration =
1194  (AVRational) { 1, sti->avctx->sample_rate },
1195  st->time_base,
1196  AV_ROUND_DOWN);
1197  }
1198  } else if ((s->iformat->flags & AVFMT_NOTIMESTAMPS) && st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1199  if (st->time_base.num > 0 && st->time_base.den > 0 &&
1200  sti->parser->duration) {
1201  out_pkt->duration = sti->parser->duration;
1202  }
1203  }
1204 
1205  out_pkt->stream_index = st->index;
1206  out_pkt->pts = sti->parser->pts;
1207  out_pkt->dts = sti->parser->dts;
1208  out_pkt->pos = sti->parser->pos;
1210 
1212  out_pkt->pos = sti->parser->frame_offset;
1213 
1214  if (sti->parser->key_frame == 1 ||
1215  (sti->parser->key_frame == -1 &&
1217  out_pkt->flags |= AV_PKT_FLAG_KEY;
1218 
1220  out_pkt->flags |= AV_PKT_FLAG_KEY;
1221 
1222  compute_pkt_fields(s, st, sti->parser, out_pkt, next_dts, next_pts);
1223 
1225  out_pkt, NULL, 0);
1226  if (ret < 0)
1227  goto fail;
1228  }
1229 
1230  /* end of the stream => close and free the parser */
1231  if (flush) {
1232  av_parser_close(sti->parser);
1233  sti->parser = NULL;
1234  }
1235 
1236 fail:
1237  if (ret < 0)
1238  av_packet_unref(out_pkt);
1240  return ret;
1241 }
1242 
1243 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1244 {
1245  return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1246 }
1247 
1249 {
1250  FFFormatContext *const si = ffformatcontext(s);
1251  int ret, got_packet = 0;
1252  AVDictionary *metadata = NULL;
1253 
1254  while (!got_packet && !si->parse_queue.head) {
1255  AVStream *st;
1256  FFStream *sti;
1257 
1258  /* read next packet */
1259  ret = ff_read_packet(s, pkt);
1260  if (ret < 0) {
1261  if (ret == AVERROR(EAGAIN))
1262  return ret;
1263  /* flush the parsers */
1264  for (unsigned i = 0; i < s->nb_streams; i++) {
1265  AVStream *const st = s->streams[i];
1266  FFStream *const sti = ffstream(st);
1267  if (sti->parser && sti->need_parsing)
1268  parse_packet(s, pkt, st->index, 1);
1269  }
1270  /* all remaining packets are now in parse_queue =>
1271  * really terminate parsing */
1272  break;
1273  }
1274  ret = 0;
1275  st = s->streams[pkt->stream_index];
1276  sti = ffstream(st);
1277 
1279 
1280  /* update context if required */
1281  if (sti->need_context_update) {
1282  if (avcodec_is_open(sti->avctx)) {
1283  av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1284  avcodec_close(sti->avctx);
1285  sti->info->found_decoder = 0;
1286  }
1287 
1288  /* close parser, because it depends on the codec */
1289  if (sti->parser && sti->avctx->codec_id != st->codecpar->codec_id) {
1290  av_parser_close(sti->parser);
1291  sti->parser = NULL;
1292  }
1293 
1295  if (ret < 0) {
1297  return ret;
1298  }
1299 
1301 
1302  sti->need_context_update = 0;
1303  }
1304 
1305  if (pkt->pts != AV_NOPTS_VALUE &&
1306  pkt->dts != AV_NOPTS_VALUE &&
1307  pkt->pts < pkt->dts) {
1309  "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1310  pkt->stream_index,
1311  av_ts2str(pkt->pts),
1312  av_ts2str(pkt->dts),
1313  pkt->size);
1314  }
1315  if (s->debug & FF_FDEBUG_TS)
1317  "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1318  pkt->stream_index,
1319  av_ts2str(pkt->pts),
1320  av_ts2str(pkt->dts),
1321  pkt->size, pkt->duration, pkt->flags);
1322 
1323  if (sti->need_parsing && !sti->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1324  sti->parser = av_parser_init(st->codecpar->codec_id);
1325  if (!sti->parser) {
1326  av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1327  "%s, packets or times may be invalid.\n",
1329  /* no parser available: just output the raw packets */
1331  } else if (sti->need_parsing == AVSTREAM_PARSE_HEADERS)
1333  else if (sti->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1334  sti->parser->flags |= PARSER_FLAG_ONCE;
1335  else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1337  }
1338 
1339  if (!sti->need_parsing || !sti->parser) {
1340  /* no parsing needed: we just output the packet as is */
1342  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1344  ff_reduce_index(s, st->index);
1345  av_add_index_entry(st, pkt->pos, pkt->dts,
1346  0, 0, AVINDEX_KEYFRAME);
1347  }
1348  got_packet = 1;
1349  } else if (st->discard < AVDISCARD_ALL) {
1350  if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1351  return ret;
1352  st->codecpar->sample_rate = sti->avctx->sample_rate;
1353  st->codecpar->bit_rate = sti->avctx->bit_rate;
1354 #if FF_API_OLD_CHANNEL_LAYOUT
1356  st->codecpar->channels = sti->avctx->ch_layout.nb_channels;
1357  st->codecpar->channel_layout = sti->avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE ?
1358  sti->avctx->ch_layout.u.mask : 0;
1360 #endif
1362  if (ret < 0)
1363  return ret;
1364  st->codecpar->codec_id = sti->avctx->codec_id;
1365  } else {
1366  /* free packet */
1368  }
1369  if (pkt->flags & AV_PKT_FLAG_KEY)
1370  sti->skip_to_keyframe = 0;
1371  if (sti->skip_to_keyframe) {
1373  got_packet = 0;
1374  }
1375  }
1376 
1377  if (!got_packet && si->parse_queue.head)
1379 
1380  if (ret >= 0) {
1381  AVStream *const st = s->streams[pkt->stream_index];
1382  FFStream *const sti = ffstream(st);
1383  int discard_padding = 0;
1384  if (sti->first_discard_sample && pkt->pts != AV_NOPTS_VALUE) {
1385  int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1386  int64_t sample = ts_to_samples(st, pts);
1387  int64_t duration = ts_to_samples(st, pkt->duration);
1388  int64_t end_sample = sample + duration;
1389  if (duration > 0 && end_sample >= sti->first_discard_sample &&
1390  sample < sti->last_discard_sample)
1391  discard_padding = FFMIN(end_sample - sti->first_discard_sample, duration);
1392  }
1393  if (sti->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1394  sti->skip_samples = sti->start_skip_samples;
1395  sti->skip_samples = FFMAX(0, sti->skip_samples);
1396  if (sti->skip_samples || discard_padding) {
1398  if (p) {
1399  AV_WL32(p, sti->skip_samples);
1400  AV_WL32(p + 4, discard_padding);
1401  av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %u / discard %u\n",
1402  (unsigned)sti->skip_samples, (unsigned)discard_padding);
1403  }
1404  sti->skip_samples = 0;
1405  }
1406 
1407  if (sti->inject_global_side_data) {
1408  for (int i = 0; i < st->nb_side_data; i++) {
1409  const AVPacketSideData *const src_sd = &st->side_data[i];
1410  uint8_t *dst_data;
1411 
1412  if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1413  continue;
1414 
1415  dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1416  if (!dst_data) {
1417  av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1418  continue;
1419  }
1420 
1421  memcpy(dst_data, src_sd->data, src_sd->size);
1422  }
1423  sti->inject_global_side_data = 0;
1424  }
1425  }
1426 
1427  if (!si->metafree) {
1428  int metaret = av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1429  if (metadata) {
1430  s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1431  av_dict_copy(&s->metadata, metadata, 0);
1432  av_dict_free(&metadata);
1434  }
1435  si->metafree = metaret == AVERROR_OPTION_NOT_FOUND;
1436  }
1437 
1438  if (s->debug & FF_FDEBUG_TS)
1440  "read_frame_internal stream=%d, pts=%s, dts=%s, "
1441  "size=%d, duration=%"PRId64", flags=%d\n",
1442  pkt->stream_index,
1443  av_ts2str(pkt->pts),
1444  av_ts2str(pkt->dts),
1445  pkt->size, pkt->duration, pkt->flags);
1446 
1447  /* A demuxer might have returned EOF because of an IO error, let's
1448  * propagate this back to the user. */
1449  if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1450  ret = s->pb->error;
1451 
1452  return ret;
1453 }
1454 
1456 {
1457  FFFormatContext *const si = ffformatcontext(s);
1458  const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1459  int eof = 0;
1460  int ret;
1461  AVStream *st;
1462 
1463  if (!genpts) {
1464  ret = si->packet_buffer.head
1467  if (ret < 0)
1468  return ret;
1469  goto return_packet;
1470  }
1471 
1472  for (;;) {
1473  PacketListEntry *pktl = si->packet_buffer.head;
1474 
1475  if (pktl) {
1476  AVPacket *next_pkt = &pktl->pkt;
1477 
1478  if (next_pkt->dts != AV_NOPTS_VALUE) {
1479  int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1480  // last dts seen for this stream. if any of packets following
1481  // current one had no dts, we will set this to AV_NOPTS_VALUE.
1482  int64_t last_dts = next_pkt->dts;
1483  av_assert2(wrap_bits <= 64);
1484  while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1485  if (pktl->pkt.stream_index == next_pkt->stream_index &&
1486  av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1487  if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1488  // not B-frame
1489  next_pkt->pts = pktl->pkt.dts;
1490  }
1491  if (last_dts != AV_NOPTS_VALUE) {
1492  // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1493  last_dts = pktl->pkt.dts;
1494  }
1495  }
1496  pktl = pktl->next;
1497  }
1498  if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1499  // Fixing the last reference frame had none pts issue (For MXF etc).
1500  // We only do this when
1501  // 1. eof.
1502  // 2. we are not able to resolve a pts value for current packet.
1503  // 3. the packets for this stream at the end of the files had valid dts.
1504  next_pkt->pts = last_dts + next_pkt->duration;
1505  }
1506  pktl = si->packet_buffer.head;
1507  }
1508 
1509  /* read packet from packet buffer, if there is data */
1510  st = s->streams[next_pkt->stream_index];
1511  if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1512  next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1514  goto return_packet;
1515  }
1516  }
1517 
1519  if (ret < 0) {
1520  if (pktl && ret != AVERROR(EAGAIN)) {
1521  eof = 1;
1522  continue;
1523  } else
1524  return ret;
1525  }
1526 
1528  pkt, NULL, 0);
1529  if (ret < 0) {
1531  return ret;
1532  }
1533  }
1534 
1535 return_packet:
1536  st = s->streams[pkt->stream_index];
1537  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1538  ff_reduce_index(s, st->index);
1540  }
1541 
1542  if (is_relative(pkt->dts))
1543  pkt->dts -= RELATIVE_TS_BASE;
1544  if (is_relative(pkt->pts))
1545  pkt->pts -= RELATIVE_TS_BASE;
1546 
1547  return ret;
1548 }
1549 
1550 /**
1551  * Return TRUE if the stream has accurate duration in any stream.
1552  *
1553  * @return TRUE if the stream has accurate duration for at least one component.
1554  */
1556 {
1557  for (unsigned i = 0; i < ic->nb_streams; i++) {
1558  const AVStream *const st = ic->streams[i];
1559  if (st->duration != AV_NOPTS_VALUE)
1560  return 1;
1561  }
1562  if (ic->duration != AV_NOPTS_VALUE)
1563  return 1;
1564  return 0;
1565 }
1566 
1567 /**
1568  * Estimate the stream timings from the one of each components.
1569  *
1570  * Also computes the global bitrate if possible.
1571  */
1573 {
1574  int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
1575  int64_t duration, duration1, duration_text, filesize;
1576 
1577  start_time = INT64_MAX;
1578  start_time_text = INT64_MAX;
1579  end_time = INT64_MIN;
1580  end_time_text = INT64_MIN;
1581  duration = INT64_MIN;
1582  duration_text = INT64_MIN;
1583 
1584  for (unsigned i = 0; i < ic->nb_streams; i++) {
1585  AVStream *const st = ic->streams[i];
1586  int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
1588 
1589  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1590  start_time1 = av_rescale_q(st->start_time, st->time_base,
1591  AV_TIME_BASE_Q);
1592  if (is_text)
1593  start_time_text = FFMIN(start_time_text, start_time1);
1594  else
1595  start_time = FFMIN(start_time, start_time1);
1596  end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
1599  if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
1600  end_time1 += start_time1;
1601  if (is_text)
1602  end_time_text = FFMAX(end_time_text, end_time1);
1603  else
1604  end_time = FFMAX(end_time, end_time1);
1605  }
1606  for (AVProgram *p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
1607  if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
1608  p->start_time = start_time1;
1609  if (p->end_time < end_time1)
1610  p->end_time = end_time1;
1611  }
1612  }
1613  if (st->duration != AV_NOPTS_VALUE) {
1614  duration1 = av_rescale_q(st->duration, st->time_base,
1615  AV_TIME_BASE_Q);
1616  if (is_text)
1617  duration_text = FFMAX(duration_text, duration1);
1618  else
1619  duration = FFMAX(duration, duration1);
1620  }
1621  }
1622  if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
1623  start_time = start_time_text;
1624  else if (start_time > start_time_text)
1625  av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
1626 
1627  if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
1628  end_time = end_time_text;
1629  else if (end_time < end_time_text)
1630  av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
1631 
1632  if (duration == INT64_MIN || (duration < duration_text && (uint64_t)duration_text - duration < AV_TIME_BASE))
1633  duration = duration_text;
1634  else if (duration < duration_text)
1635  av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
1636 
1637  if (start_time != INT64_MAX) {
1638  ic->start_time = start_time;
1639  if (end_time != INT64_MIN) {
1640  if (ic->nb_programs > 1) {
1641  for (unsigned i = 0; i < ic->nb_programs; i++) {
1642  AVProgram *const p = ic->programs[i];
1643 
1644  if (p->start_time != AV_NOPTS_VALUE &&
1645  p->end_time > p->start_time &&
1646  p->end_time - (uint64_t)p->start_time <= INT64_MAX)
1648  }
1649  } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
1650  duration = FFMAX(duration, end_time - start_time);
1651  }
1652  }
1653  }
1654  if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
1655  ic->duration = duration;
1656  }
1657  if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
1658  /* compute the bitrate */
1659  double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
1660  (double) ic->duration;
1661  if (bitrate >= 0 && bitrate <= INT64_MAX)
1662  ic->bit_rate = bitrate;
1663  }
1664 }
1665 
1667 {
1669  for (unsigned i = 0; i < ic->nb_streams; i++) {
1670  AVStream *const st = ic->streams[i];
1671 
1672  if (st->start_time == AV_NOPTS_VALUE) {
1673  if (ic->start_time != AV_NOPTS_VALUE)
1675  st->time_base);
1676  if (ic->duration != AV_NOPTS_VALUE)
1678  st->time_base);
1679  }
1680  }
1681 }
1682 
1684 {
1685  FFFormatContext *const si = ffformatcontext(ic);
1686  int show_warning = 0;
1687 
1688  /* if bit_rate is already set, we believe it */
1689  if (ic->bit_rate <= 0) {
1690  int64_t bit_rate = 0;
1691  for (unsigned i = 0; i < ic->nb_streams; i++) {
1692  const AVStream *const st = ic->streams[i];
1693  const FFStream *const sti = cffstream(st);
1694  if (st->codecpar->bit_rate <= 0 && sti->avctx->bit_rate > 0)
1695  st->codecpar->bit_rate = sti->avctx->bit_rate;
1696  if (st->codecpar->bit_rate > 0) {
1697  if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
1698  bit_rate = 0;
1699  break;
1700  }
1701  bit_rate += st->codecpar->bit_rate;
1702  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && sti->codec_info_nb_frames > 1) {
1703  // If we have a videostream with packets but without a bitrate
1704  // then consider the sum not known
1705  bit_rate = 0;
1706  break;
1707  }
1708  }
1709  ic->bit_rate = bit_rate;
1710  }
1711 
1712  /* if duration is already set, we believe it */
1713  if (ic->duration == AV_NOPTS_VALUE &&
1714  ic->bit_rate != 0) {
1715  int64_t filesize = ic->pb ? avio_size(ic->pb) : 0;
1716  if (filesize > si->data_offset) {
1717  filesize -= si->data_offset;
1718  for (unsigned i = 0; i < ic->nb_streams; i++) {
1719  AVStream *const st = ic->streams[i];
1720 
1721  if ( st->time_base.num <= INT64_MAX / ic->bit_rate
1722  && st->duration == AV_NOPTS_VALUE) {
1723  st->duration = av_rescale(filesize, 8LL * st->time_base.den,
1724  ic->bit_rate *
1725  (int64_t) st->time_base.num);
1726  show_warning = 1;
1727  }
1728  }
1729  }
1730  }
1731  if (show_warning)
1732  av_log(ic, AV_LOG_WARNING,
1733  "Estimating duration from bitrate, this may be inaccurate\n");
1734 }
1735 
1736 #define DURATION_MAX_READ_SIZE 250000LL
1737 #define DURATION_MAX_RETRY 6
1738 
1739 /* only usable for MPEG-PS streams */
1740 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1741 {
1742  FFFormatContext *const si = ffformatcontext(ic);
1743  AVPacket *const pkt = si->pkt;
1744  int num, den, read_size, ret;
1745  int found_duration = 0;
1746  int is_end;
1747  int64_t filesize, offset, duration;
1748  int retry = 0;
1749 
1750  /* flush packet queue */
1752 
1753  for (unsigned i = 0; i < ic->nb_streams; i++) {
1754  AVStream *const st = ic->streams[i];
1755  FFStream *const sti = ffstream(st);
1756 
1757  if (st->start_time == AV_NOPTS_VALUE &&
1758  sti->first_dts == AV_NOPTS_VALUE &&
1760  av_log(ic, AV_LOG_WARNING,
1761  "start time for stream %d is not set in estimate_timings_from_pts\n", i);
1762 
1763  if (sti->parser) {
1764  av_parser_close(sti->parser);
1765  sti->parser = NULL;
1766  }
1767  }
1768 
1770  av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
1771  goto skip_duration_calc;
1772  }
1773 
1774  av_opt_set_int(ic, "skip_changes", 1, AV_OPT_SEARCH_CHILDREN);
1775  /* estimate the end time (duration) */
1776  /* XXX: may need to support wrapping */
1777  filesize = ic->pb ? avio_size(ic->pb) : 0;
1778  do {
1779  is_end = found_duration;
1780  offset = filesize - (DURATION_MAX_READ_SIZE << retry);
1781  if (offset < 0)
1782  offset = 0;
1783 
1784  avio_seek(ic->pb, offset, SEEK_SET);
1785  read_size = 0;
1786  for (;;) {
1787  AVStream *st;
1788  FFStream *sti;
1789  if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
1790  break;
1791 
1792  do {
1793  ret = ff_read_packet(ic, pkt);
1794  } while (ret == AVERROR(EAGAIN));
1795  if (ret != 0)
1796  break;
1797  read_size += pkt->size;
1798  st = ic->streams[pkt->stream_index];
1799  sti = ffstream(st);
1800  if (pkt->pts != AV_NOPTS_VALUE &&
1801  (st->start_time != AV_NOPTS_VALUE ||
1802  sti->first_dts != AV_NOPTS_VALUE)) {
1803  if (pkt->duration == 0) {
1804  compute_frame_duration(ic, &num, &den, st, sti->parser, pkt);
1805  if (den && num) {
1807  num * (int64_t) st->time_base.den,
1808  den * (int64_t) st->time_base.num,
1809  AV_ROUND_DOWN);
1810  }
1811  }
1812  duration = pkt->pts + pkt->duration;
1813  found_duration = 1;
1814  if (st->start_time != AV_NOPTS_VALUE)
1815  duration -= st->start_time;
1816  else
1817  duration -= sti->first_dts;
1818  if (duration > 0) {
1819  if (st->duration == AV_NOPTS_VALUE || sti->info->last_duration<= 0 ||
1820  (st->duration < duration && FFABS(duration - sti->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
1821  st->duration = duration;
1822  sti->info->last_duration = duration;
1823  }
1824  }
1826  }
1827 
1828  /* check if all audio/video streams have valid duration */
1829  if (!is_end) {
1830  is_end = 1;
1831  for (unsigned i = 0; i < ic->nb_streams; i++) {
1832  const AVStream *const st = ic->streams[i];
1833  switch (st->codecpar->codec_type) {
1834  case AVMEDIA_TYPE_VIDEO:
1835  case AVMEDIA_TYPE_AUDIO:
1836  if (st->duration == AV_NOPTS_VALUE)
1837  is_end = 0;
1838  }
1839  }
1840  }
1841  } while (!is_end &&
1842  offset &&
1843  ++retry <= DURATION_MAX_RETRY);
1844 
1845  av_opt_set_int(ic, "skip_changes", 0, AV_OPT_SEARCH_CHILDREN);
1846 
1847  /* warn about audio/video streams which duration could not be estimated */
1848  for (unsigned i = 0; i < ic->nb_streams; i++) {
1849  const AVStream *const st = ic->streams[i];
1850  const FFStream *const sti = cffstream(st);
1851 
1852  if (st->duration == AV_NOPTS_VALUE) {
1853  switch (st->codecpar->codec_type) {
1854  case AVMEDIA_TYPE_VIDEO:
1855  case AVMEDIA_TYPE_AUDIO:
1856  if (st->start_time != AV_NOPTS_VALUE || sti->first_dts != AV_NOPTS_VALUE) {
1857  av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
1858  } else
1859  av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
1860  }
1861  }
1862  }
1863 skip_duration_calc:
1865 
1866  avio_seek(ic->pb, old_offset, SEEK_SET);
1867  for (unsigned i = 0; i < ic->nb_streams; i++) {
1868  AVStream *const st = ic->streams[i];
1869  FFStream *const sti = ffstream(st);
1870 
1871  sti->cur_dts = sti->first_dts;
1872  sti->last_IP_pts = AV_NOPTS_VALUE;
1874  for (int j = 0; j < MAX_REORDER_DELAY + 1; j++)
1875  sti->pts_buffer[j] = AV_NOPTS_VALUE;
1876  }
1877 }
1878 
1879 /* 1:1 map to AVDurationEstimationMethod */
1880 static const char *const duration_name[] = {
1881  [AVFMT_DURATION_FROM_PTS] = "pts",
1882  [AVFMT_DURATION_FROM_STREAM] = "stream",
1883  [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
1884 };
1885 
1887 {
1888  return duration_name[method];
1889 }
1890 
1891 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1892 {
1893  int64_t file_size;
1894 
1895  /* get the file size, if possible */
1896  if (ic->iformat->flags & AVFMT_NOFILE) {
1897  file_size = 0;
1898  } else {
1899  file_size = avio_size(ic->pb);
1900  file_size = FFMAX(0, file_size);
1901  }
1902 
1903  if ((!strcmp(ic->iformat->name, "mpeg") ||
1904  !strcmp(ic->iformat->name, "mpegts")) &&
1905  file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
1906  /* get accurate estimate from the PTSes */
1907  estimate_timings_from_pts(ic, old_offset);
1909  } else if (has_duration(ic)) {
1910  /* at least one component has timings - we use them for all
1911  * the components */
1913  /* nut demuxer estimate the duration from PTS */
1914  if (!strcmp(ic->iformat->name, "nut"))
1916  else
1918  } else {
1919  /* less precise: use bitrate info */
1922  }
1924 
1925  for (unsigned i = 0; i < ic->nb_streams; i++) {
1926  AVStream *const st = ic->streams[i];
1927  if (st->time_base.den)
1928  av_log(ic, AV_LOG_TRACE, "stream %u: start_time: %s duration: %s\n", i,
1929  av_ts2timestr(st->start_time, &st->time_base),
1930  av_ts2timestr(st->duration, &st->time_base));
1931  }
1932  av_log(ic, AV_LOG_TRACE,
1933  "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
1937  (int64_t)ic->bit_rate / 1000);
1938 }
1939 
1940 static int determinable_frame_size(const AVCodecContext *avctx)
1941 {
1942  switch(avctx->codec_id) {
1943  case AV_CODEC_ID_MP1:
1944  case AV_CODEC_ID_MP2:
1945  case AV_CODEC_ID_MP3:
1946  case AV_CODEC_ID_CODEC2:
1947  return 1;
1948  }
1949 
1950  return 0;
1951 }
1952 
1953 static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
1954 {
1955  const FFStream *const sti = cffstream(st);
1956  const AVCodecContext *const avctx = sti->avctx;
1957 
1958 #define FAIL(errmsg) do { \
1959  if (errmsg_ptr) \
1960  *errmsg_ptr = errmsg; \
1961  return 0; \
1962  } while (0)
1963 
1964  if ( avctx->codec_id == AV_CODEC_ID_NONE
1965  && avctx->codec_type != AVMEDIA_TYPE_DATA)
1966  FAIL("unknown codec");
1967  switch (avctx->codec_type) {
1968  case AVMEDIA_TYPE_AUDIO:
1969  if (!avctx->frame_size && determinable_frame_size(avctx))
1970  FAIL("unspecified frame size");
1971  if (sti->info->found_decoder >= 0 &&
1972  avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
1973  FAIL("unspecified sample format");
1974  if (!avctx->sample_rate)
1975  FAIL("unspecified sample rate");
1976  if (!avctx->ch_layout.nb_channels)
1977  FAIL("unspecified number of channels");
1978  if (sti->info->found_decoder >= 0 && !sti->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
1979  FAIL("no decodable DTS frames");
1980  break;
1981  case AVMEDIA_TYPE_VIDEO:
1982  if (!avctx->width)
1983  FAIL("unspecified size");
1984  if (sti->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
1985  FAIL("unspecified pixel format");
1988  FAIL("no frame in rv30/40 and no sar");
1989  break;
1990  case AVMEDIA_TYPE_SUBTITLE:
1991  if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
1992  FAIL("unspecified size");
1993  break;
1994  case AVMEDIA_TYPE_DATA:
1995  if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
1996  }
1997 
1998  return 1;
1999 }
2000 
2001 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2003  const AVPacket *pkt, AVDictionary **options)
2004 {
2005  FFStream *const sti = ffstream(st);
2006  AVCodecContext *const avctx = sti->avctx;
2007  const AVCodec *codec;
2008  int got_picture = 1, ret = 0;
2010  AVSubtitle subtitle;
2011  int do_skip_frame = 0;
2012  enum AVDiscard skip_frame;
2013  int pkt_to_send = pkt->size > 0;
2014 
2015  if (!frame)
2016  return AVERROR(ENOMEM);
2017 
2018  if (!avcodec_is_open(avctx) &&
2019  sti->info->found_decoder <= 0 &&
2020  (st->codecpar->codec_id != -sti->info->found_decoder || !st->codecpar->codec_id)) {
2021  AVDictionary *thread_opt = NULL;
2022 
2023  codec = find_probe_decoder(s, st, st->codecpar->codec_id);
2024 
2025  if (!codec) {
2026  sti->info->found_decoder = -st->codecpar->codec_id;
2027  ret = -1;
2028  goto fail;
2029  }
2030 
2031  /* Force thread count to 1 since the H.264 decoder will not extract
2032  * SPS and PPS to extradata during multi-threaded decoding. */
2033  av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2034  /* Force lowres to 0. The decoder might reduce the video size by the
2035  * lowres factor, and we don't want that propagated to the stream's
2036  * codecpar */
2037  av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
2038  if (s->codec_whitelist)
2039  av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
2040  ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
2041  if (!options)
2042  av_dict_free(&thread_opt);
2043  if (ret < 0) {
2044  sti->info->found_decoder = -avctx->codec_id;
2045  goto fail;
2046  }
2047  sti->info->found_decoder = 1;
2048  } else if (!sti->info->found_decoder)
2049  sti->info->found_decoder = 1;
2050 
2051  if (sti->info->found_decoder < 0) {
2052  ret = -1;
2053  goto fail;
2054  }
2055 
2057  do_skip_frame = 1;
2058  skip_frame = avctx->skip_frame;
2059  avctx->skip_frame = AVDISCARD_ALL;
2060  }
2061 
2062  while ((pkt_to_send || (!pkt->data && got_picture)) &&
2063  ret >= 0 &&
2065  (!sti->codec_info_nb_frames &&
2067  got_picture = 0;
2068  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
2069  avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2070  ret = avcodec_send_packet(avctx, pkt);
2071  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2072  break;
2073  if (ret >= 0)
2074  pkt_to_send = 0;
2075  ret = avcodec_receive_frame(avctx, frame);
2076  if (ret >= 0)
2077  got_picture = 1;
2078  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
2079  ret = 0;
2080  } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
2081  ret = avcodec_decode_subtitle2(avctx, &subtitle,
2082  &got_picture, pkt);
2083  if (got_picture)
2084  avsubtitle_free(&subtitle);
2085  if (ret >= 0)
2086  pkt_to_send = 0;
2087  }
2088  if (ret >= 0) {
2089  if (got_picture)
2090  sti->nb_decoded_frames++;
2091  ret = got_picture;
2092  }
2093  }
2094 
2095 fail:
2096  if (do_skip_frame) {
2097  avctx->skip_frame = skip_frame;
2098  }
2099 
2100  av_frame_free(&frame);
2101  return ret;
2102 }
2103 
2104 static int chapter_start_cmp(const void *p1, const void *p2)
2105 {
2106  const AVChapter *const ch1 = *(AVChapter**)p1;
2107  const AVChapter *const ch2 = *(AVChapter**)p2;
2108  int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
2109  if (delta)
2110  return delta;
2111  return FFDIFFSIGN(ch1->id, ch2->id);
2112 }
2113 
2115 {
2116  int64_t max_time = 0;
2117  AVChapter **timetable;
2118 
2119  if (!s->nb_chapters)
2120  return 0;
2121 
2122  if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
2123  max_time = s->duration +
2124  ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2125 
2126  timetable = av_memdup(s->chapters, s->nb_chapters * sizeof(*timetable));
2127  if (!timetable)
2128  return AVERROR(ENOMEM);
2129  qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
2130 
2131  for (unsigned i = 0; i < s->nb_chapters; i++)
2132  if (timetable[i]->end == AV_NOPTS_VALUE) {
2133  AVChapter *const ch = timetable[i];
2134  int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2135  ch->time_base)
2136  : INT64_MAX;
2137 
2138  if (i + 1 < s->nb_chapters) {
2139  const AVChapter *const ch1 = timetable[i + 1];
2140  int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2141  ch->time_base);
2142  if (next_start > ch->start && next_start < end)
2143  end = next_start;
2144  }
2145  ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
2146  }
2147  av_free(timetable);
2148  return 0;
2149 }
2150 
2151 static int get_std_framerate(int i)
2152 {
2153  if (i < 30*12)
2154  return (i + 1) * 1001;
2155  i -= 30*12;
2156 
2157  if (i < 30)
2158  return (i + 31) * 1001 * 12;
2159  i -= 30;
2160 
2161  if (i < 3)
2162  return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
2163 
2164  i -= 3;
2165 
2166  return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
2167 }
2168 
2169 /* Is the time base unreliable?
2170  * This is a heuristic to balance between quick acceptance of the values in
2171  * the headers vs. some extra checks.
2172  * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2173  * MPEG-2 commonly misuses field repeat flags to store different framerates.
2174  * And there are "variable" fps files this needs to detect as well. */
2176 {
2177  FFStream *const sti = ffstream(st);
2178  const AVCodecDescriptor *desc = sti->codec_desc;
2179  AVCodecContext *c = sti->avctx;
2180  AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2181  AVRational time_base = c->framerate.num ? av_inv_q(av_mul_q(c->framerate, mul))
2182  /* NOHEADER check added to not break existing behavior */
2183  : (((ic->ctx_flags & AVFMTCTX_NOHEADER) ||
2185  : st->time_base);
2186 
2187  if (time_base.den >= 101LL * time_base.num ||
2188  time_base.den < 5LL * time_base.num ||
2189  // c->codec_tag == AV_RL32("DIVX") ||
2190  // c->codec_tag == AV_RL32("XVID") ||
2191  c->codec_tag == AV_RL32("mp4v") ||
2192  c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
2193  c->codec_id == AV_CODEC_ID_GIF ||
2194  c->codec_id == AV_CODEC_ID_HEVC ||
2195  c->codec_id == AV_CODEC_ID_H264)
2196  return 1;
2197  return 0;
2198 }
2199 
2200 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
2201 {
2202  FFStream *const sti = ffstream(st);
2203  FFStreamInfo *info = sti->info;
2204  int64_t last = info->last_dts;
2205 
2206  if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
2207  && ts - (uint64_t)last < INT64_MAX) {
2208  double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
2209  int64_t duration = ts - last;
2210 
2211  if (!info->duration_error)
2212  info->duration_error = av_mallocz(sizeof(info->duration_error[0])*2);
2213  if (!info->duration_error)
2214  return AVERROR(ENOMEM);
2215 
2216 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2217 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
2218  for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2219  if (info->duration_error[0][1][i] < 1e10) {
2220  int framerate = get_std_framerate(i);
2221  double sdts = dts*framerate/(1001*12);
2222  for (int j = 0; j < 2; j++) {
2223  int64_t ticks = llrint(sdts+j*0.5);
2224  double error = sdts - ticks + j*0.5;
2225  info->duration_error[j][0][i] += error;
2226  info->duration_error[j][1][i] += error*error;
2227  }
2228  }
2229  }
2230  if (info->rfps_duration_sum <= INT64_MAX - duration) {
2231  info->duration_count++;
2232  info->rfps_duration_sum += duration;
2233  }
2234 
2235  if (info->duration_count % 10 == 0) {
2236  int n = info->duration_count;
2237  for (int i = 0; i < MAX_STD_TIMEBASES; i++) {
2238  if (info->duration_error[0][1][i] < 1e10) {
2239  double a0 = info->duration_error[0][0][i] / n;
2240  double error0 = info->duration_error[0][1][i] / n - a0*a0;
2241  double a1 = info->duration_error[1][0][i] / n;
2242  double error1 = info->duration_error[1][1][i] / n - a1*a1;
2243  if (error0 > 0.04 && error1 > 0.04) {
2244  info->duration_error[0][1][i] = 2e10;
2245  info->duration_error[1][1][i] = 2e10;
2246  }
2247  }
2248  }
2249  }
2250 
2251  // ignore the first 4 values, they might have some random jitter
2252  if (info->duration_count > 3 && is_relative(ts) == is_relative(last))
2253  info->duration_gcd = av_gcd(info->duration_gcd, duration);
2254  }
2255  if (ts != AV_NOPTS_VALUE)
2256  info->last_dts = ts;
2257 
2258  return 0;
2259 }
2260 
2262 {
2263  for (unsigned i = 0; i < ic->nb_streams; i++) {
2264  AVStream *const st = ic->streams[i];
2265  FFStream *const sti = ffstream(st);
2266 
2268  continue;
2269  // the check for tb_unreliable() is not completely correct, since this is not about handling
2270  // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2271  // ipmovie.c produces.
2272  if (tb_unreliable(ic, st) && sti->info->duration_count > 15 && sti->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num &&
2273  sti->info->duration_gcd < INT64_MAX / st->time_base.num)
2274  av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * sti->info->duration_gcd, INT_MAX);
2275  if (sti->info->duration_count > 1 && !st->r_frame_rate.num
2276  && tb_unreliable(ic, st)) {
2277  int num = 0;
2278  double best_error = 0.01;
2279  AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
2280 
2281  for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2282  if (sti->info->codec_info_duration &&
2283  sti->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
2284  continue;
2285  if (!sti->info->codec_info_duration && get_std_framerate(j) < 1001*12)
2286  continue;
2287 
2288  if (av_q2d(st->time_base) * sti->info->rfps_duration_sum / sti->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
2289  continue;
2290 
2291  for (int k = 0; k < 2; k++) {
2292  int n = sti->info->duration_count;
2293  double a = sti->info->duration_error[k][0][j] / n;
2294  double error = sti->info->duration_error[k][1][j]/n - a*a;
2295 
2296  if (error < best_error && best_error> 0.000000001) {
2297  best_error= error;
2298  num = get_std_framerate(j);
2299  }
2300  if (error < 0.02)
2301  av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
2302  }
2303  }
2304  // do not increase frame rate by more than 1 % in order to match a standard rate.
2305  if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
2306  av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2307  }
2308  if ( !st->avg_frame_rate.num
2309  && st->r_frame_rate.num && sti->info->rfps_duration_sum
2310  && sti->info->codec_info_duration <= 0
2311  && sti->info->duration_count > 2
2312  && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - sti->info->rfps_duration_sum / (double)sti->info->duration_count) <= 1.0
2313  ) {
2314  av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
2315  st->avg_frame_rate = st->r_frame_rate;
2316  }
2317 
2318  av_freep(&sti->info->duration_error);
2319  sti->info->last_dts = AV_NOPTS_VALUE;
2320  sti->info->duration_count = 0;
2321  sti->info->rfps_duration_sum = 0;
2322  }
2323 }
2324 
2326 {
2327  const AVBitStreamFilter *const f = av_bsf_get_by_name("extract_extradata");
2328  if (!f)
2329  return 0;
2330 
2331  if (f->codec_ids) {
2332  const enum AVCodecID *ids;
2333  for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
2334  if (*ids == st->codecpar->codec_id)
2335  return 1;
2336  }
2337 
2338  return 0;
2339 }
2340 
2342 {
2343  FFStream *const sti = ffstream(st);
2344  const AVBitStreamFilter *f;
2345  int ret;
2346 
2347  f = av_bsf_get_by_name("extract_extradata");
2348  if (!f)
2349  goto finish;
2350 
2351  /* check that the codec id is supported */
2353  if (!ret)
2354  goto finish;
2355 
2357  if (ret < 0)
2358  return ret;
2359 
2361  st->codecpar);
2362  if (ret < 0)
2363  goto fail;
2364 
2366 
2368  if (ret < 0)
2369  goto fail;
2370 
2371 finish:
2372  sti->extract_extradata.inited = 1;
2373 
2374  return 0;
2375 fail:
2377  return ret;
2378 }
2379 
2381 {
2382  FFStream *const sti = ffstream(st);
2383  AVPacket *const pkt_ref = si->parse_pkt;
2384  int ret;
2385 
2386  if (!sti->extract_extradata.inited) {
2388  if (ret < 0)
2389  return ret;
2390  }
2391 
2392  if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
2393  return 0;
2394 
2395  ret = av_packet_ref(pkt_ref, pkt);
2396  if (ret < 0)
2397  return ret;
2398 
2399  ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
2400  if (ret < 0) {
2401  av_packet_unref(pkt_ref);
2402  return ret;
2403  }
2404 
2405  while (ret >= 0 && !sti->avctx->extradata) {
2407  if (ret < 0) {
2408  if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
2409  return ret;
2410  continue;
2411  }
2412 
2413  for (int i = 0; i < pkt_ref->side_data_elems; i++) {
2414  AVPacketSideData *const side_data = &pkt_ref->side_data[i];
2415  if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
2416  sti->avctx->extradata = side_data->data;
2417  sti->avctx->extradata_size = side_data->size;
2418  side_data->data = NULL;
2419  side_data->size = 0;
2420  break;
2421  }
2422  }
2423  av_packet_unref(pkt_ref);
2424  }
2425 
2426  return 0;
2427 }
2428 
2430 {
2431  for (int i = 0; i < avctx->nb_coded_side_data; i++) {
2432  const AVPacketSideData *const sd_src = &avctx->coded_side_data[i];
2433  uint8_t *dst_data;
2434  dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
2435  if (!dst_data)
2436  return AVERROR(ENOMEM);
2437  memcpy(dst_data, sd_src->data, sd_src->size);
2438  }
2439  return 0;
2440 }
2441 
2443 {
2444  FFFormatContext *const si = ffformatcontext(ic);
2445  int count = 0, ret = 0;
2446  int64_t read_size;
2447  AVPacket *pkt1 = si->pkt;
2448  int64_t old_offset = avio_tell(ic->pb);
2449  // new streams might appear, no options for those
2450  int orig_nb_streams = ic->nb_streams;
2451  int flush_codecs;
2452  int64_t max_analyze_duration = ic->max_analyze_duration;
2453  int64_t max_stream_analyze_duration;
2454  int64_t max_subtitle_analyze_duration;
2455  int64_t probesize = ic->probesize;
2456  int eof_reached = 0;
2457  int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
2458 
2459  flush_codecs = probesize > 0;
2460 
2461  av_opt_set_int(ic, "skip_clear", 1, AV_OPT_SEARCH_CHILDREN);
2462 
2463  max_stream_analyze_duration = max_analyze_duration;
2464  max_subtitle_analyze_duration = max_analyze_duration;
2465  if (!max_analyze_duration) {
2466  max_stream_analyze_duration =
2467  max_analyze_duration = 5*AV_TIME_BASE;
2468  max_subtitle_analyze_duration = 30*AV_TIME_BASE;
2469  if (!strcmp(ic->iformat->name, "flv"))
2470  max_stream_analyze_duration = 90*AV_TIME_BASE;
2471  if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
2472  max_stream_analyze_duration = 7*AV_TIME_BASE;
2473  }
2474 
2475  if (ic->pb) {
2476  FFIOContext *const ctx = ffiocontext(ic->pb);
2477  av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
2478  avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, ic->nb_streams);
2479  }
2480 
2481  for (unsigned i = 0; i < ic->nb_streams; i++) {
2482  const AVCodec *codec;
2483  AVDictionary *thread_opt = NULL;
2484  AVStream *const st = ic->streams[i];
2485  FFStream *const sti = ffstream(st);
2486  AVCodecContext *const avctx = sti->avctx;
2487 
2488  /* check if the caller has overridden the codec id */
2489  // only for the split stuff
2490  if (!sti->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && sti->request_probe <= 0) {
2491  sti->parser = av_parser_init(st->codecpar->codec_id);
2492  if (sti->parser) {
2493  if (sti->need_parsing == AVSTREAM_PARSE_HEADERS) {
2495  } else if (sti->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
2497  }
2498  } else if (sti->need_parsing) {
2499  av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
2500  "%s, packets or times may be invalid.\n",
2502  }
2503  }
2504 
2506  if (ret < 0)
2507  goto find_stream_info_err;
2508  if (sti->request_probe <= 0)
2509  sti->avctx_inited = 1;
2510 
2511  codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2512 
2513  /* Force thread count to 1 since the H.264 decoder will not extract
2514  * SPS and PPS to extradata during multi-threaded decoding. */
2515  av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2516  /* Force lowres to 0. The decoder might reduce the video size by the
2517  * lowres factor, and we don't want that propagated to the stream's
2518  * codecpar */
2519  av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
2520 
2521  if (ic->codec_whitelist)
2522  av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
2523 
2524  // Try to just open decoders, in case this is enough to get parameters.
2525  // Also ensure that subtitle_header is properly set.
2526  if (!has_codec_parameters(st, NULL) && sti->request_probe <= 0 ||
2528  if (codec && !avctx->codec)
2529  if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
2530  av_log(ic, AV_LOG_WARNING,
2531  "Failed to open codec in %s\n",__FUNCTION__);
2532  }
2533  if (!options)
2534  av_dict_free(&thread_opt);
2535  }
2536 
2537  read_size = 0;
2538  for (;;) {
2539  const AVPacket *pkt;
2540  AVStream *st;
2541  FFStream *sti;
2542  AVCodecContext *avctx;
2543  int analyzed_all_streams;
2544  unsigned i;
2546  ret = AVERROR_EXIT;
2547  av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2548  break;
2549  }
2550 
2551  /* check if one codec still needs to be handled */
2552  for (i = 0; i < ic->nb_streams; i++) {
2553  AVStream *const st = ic->streams[i];
2554  FFStream *const sti = ffstream(st);
2555  int fps_analyze_framecount = 20;
2556  int count;
2557 
2558  if (!has_codec_parameters(st, NULL))
2559  break;
2560  /* If the timebase is coarse (like the usual millisecond precision
2561  * of mkv), we need to analyze more frames to reliably arrive at
2562  * the correct fps. */
2563  if (av_q2d(st->time_base) > 0.0005)
2564  fps_analyze_framecount *= 2;
2565  if (!tb_unreliable(ic, st))
2566  fps_analyze_framecount = 0;
2567  if (ic->fps_probe_size >= 0)
2568  fps_analyze_framecount = ic->fps_probe_size;
2570  fps_analyze_framecount = 0;
2571  /* variable fps and no guess at the real fps */
2572  count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
2574  sti->info->duration_count;
2575  if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
2577  if (count < fps_analyze_framecount)
2578  break;
2579  }
2580  // Look at the first 3 frames if there is evidence of frame delay
2581  // but the decoder delay is not set.
2582  if (sti->info->frame_delay_evidence && count < 2 && sti->avctx->has_b_frames == 0)
2583  break;
2584  if (!sti->avctx->extradata &&
2585  (!sti->extract_extradata.inited || sti->extract_extradata.bsf) &&
2587  break;
2588  if (sti->first_dts == AV_NOPTS_VALUE &&
2593  break;
2594  }
2595  analyzed_all_streams = 0;
2596  if (!missing_streams || !*missing_streams)
2597  if (i == ic->nb_streams) {
2598  analyzed_all_streams = 1;
2599  /* NOTE: If the format has no header, then we need to read some
2600  * packets to get most of the streams, so we cannot stop here. */
2601  if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2602  /* If we found the info for all the codecs, we can stop. */
2603  ret = count;
2604  av_log(ic, AV_LOG_DEBUG, "All info found\n");
2605  flush_codecs = 0;
2606  break;
2607  }
2608  }
2609  /* We did not get all the codec info, but we read too much data. */
2610  if (read_size >= probesize) {
2611  ret = count;
2612  av_log(ic, AV_LOG_DEBUG,
2613  "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
2614  for (unsigned i = 0; i < ic->nb_streams; i++) {
2615  AVStream *const st = ic->streams[i];
2616  FFStream *const sti = ffstream(st);
2617  if (!st->r_frame_rate.num &&
2618  sti->info->duration_count <= 1 &&
2620  strcmp(ic->iformat->name, "image2"))
2621  av_log(ic, AV_LOG_WARNING,
2622  "Stream #%d: not enough frames to estimate rate; "
2623  "consider increasing probesize\n", i);
2624  }
2625  break;
2626  }
2627 
2628  /* NOTE: A new stream can be added there if no header in file
2629  * (AVFMTCTX_NOHEADER). */
2630  ret = read_frame_internal(ic, pkt1);
2631  if (ret == AVERROR(EAGAIN))
2632  continue;
2633 
2634  if (ret < 0) {
2635  /* EOF or error*/
2636  eof_reached = 1;
2637  break;
2638  }
2639 
2640  if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2642  pkt1, NULL, 0);
2643  if (ret < 0)
2644  goto unref_then_goto_end;
2645 
2646  pkt = &si->packet_buffer.tail->pkt;
2647  } else {
2648  pkt = pkt1;
2649  }
2650 
2651  st = ic->streams[pkt->stream_index];
2652  sti = ffstream(st);
2654  read_size += pkt->size;
2655 
2656  avctx = sti->avctx;
2657  if (!sti->avctx_inited) {
2659  if (ret < 0)
2660  goto unref_then_goto_end;
2661  sti->avctx_inited = 1;
2662  }
2663 
2664  if (pkt->dts != AV_NOPTS_VALUE && sti->codec_info_nb_frames > 1) {
2665  /* check for non-increasing dts */
2666  if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2667  sti->info->fps_last_dts >= pkt->dts) {
2668  av_log(ic, AV_LOG_DEBUG,
2669  "Non-increasing DTS in stream %d: packet %d with DTS "
2670  "%"PRId64", packet %d with DTS %"PRId64"\n",
2671  st->index, sti->info->fps_last_dts_idx,
2673  pkt->dts);
2674  sti->info->fps_first_dts =
2676  }
2677  /* Check for a discontinuity in dts. If the difference in dts
2678  * is more than 1000 times the average packet duration in the
2679  * sequence, we treat it as a discontinuity. */
2680  if (sti->info->fps_last_dts != AV_NOPTS_VALUE &&
2681  sti->info->fps_last_dts_idx > sti->info->fps_first_dts_idx &&
2682  (pkt->dts - (uint64_t)sti->info->fps_last_dts) / 1000 >
2683  (sti->info->fps_last_dts - (uint64_t)sti->info->fps_first_dts) /
2684  (sti->info->fps_last_dts_idx - sti->info->fps_first_dts_idx)) {
2685  av_log(ic, AV_LOG_WARNING,
2686  "DTS discontinuity in stream %d: packet %d with DTS "
2687  "%"PRId64", packet %d with DTS %"PRId64"\n",
2688  st->index, sti->info->fps_last_dts_idx,
2690  pkt->dts);
2691  sti->info->fps_first_dts =
2693  }
2694 
2695  /* update stored dts values */
2696  if (sti->info->fps_first_dts == AV_NOPTS_VALUE) {
2697  sti->info->fps_first_dts = pkt->dts;
2699  }
2700  sti->info->fps_last_dts = pkt->dts;
2702  }
2703  if (sti->codec_info_nb_frames > 1) {
2704  int64_t t = 0;
2705  int64_t limit;
2706 
2707  if (st->time_base.den > 0)
2709  if (st->avg_frame_rate.num > 0)
2711 
2712  if ( t == 0
2713  && sti->codec_info_nb_frames > 30
2714  && sti->info->fps_first_dts != AV_NOPTS_VALUE
2715  && sti->info->fps_last_dts != AV_NOPTS_VALUE) {
2716  int64_t dur = av_sat_sub64(sti->info->fps_last_dts, sti->info->fps_first_dts);
2717  t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
2718  }
2719 
2720  if (analyzed_all_streams) limit = max_analyze_duration;
2721  else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
2722  else limit = max_stream_analyze_duration;
2723 
2724  if (t >= limit) {
2725  av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
2726  limit,
2727  t, pkt->stream_index);
2728  if (ic->flags & AVFMT_FLAG_NOBUFFER)
2729  av_packet_unref(pkt1);
2730  break;
2731  }
2732  if (pkt->duration > 0) {
2733  const int fields = sti->codec_desc && (sti->codec_desc->props & AV_CODEC_PROP_FIELDS);
2735  && (uint64_t)pkt->pts - st->start_time < INT64_MAX
2736  ) {
2738  } else
2740  sti->info->codec_info_duration_fields += sti->parser && sti->need_parsing && fields
2741  ? sti->parser->repeat_pict + 1 : 2;
2742  }
2743  }
2744  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
2745 #if FF_API_R_FRAME_RATE
2746  ff_rfps_add_frame(ic, st, pkt->dts);
2747 #endif
2748  if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
2749  sti->info->frame_delay_evidence = 1;
2750  }
2751  if (!sti->avctx->extradata) {
2752  ret = extract_extradata(si, st, pkt);
2753  if (ret < 0)
2754  goto unref_then_goto_end;
2755  }
2756 
2757  /* If still no information, we try to open the codec and to
2758  * decompress the frame. We try to avoid that in most cases as
2759  * it takes longer and uses more memory. For MPEG-4, we need to
2760  * decompress for QuickTime.
2761  *
2762  * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2763  * least one frame of codec data, this makes sure the codec initializes
2764  * the channel configuration and does not only trust the values from
2765  * the container. */
2766  try_decode_frame(ic, st, pkt,
2767  (options && i < orig_nb_streams) ? &options[i] : NULL);
2768 
2769  if (ic->flags & AVFMT_FLAG_NOBUFFER)
2770  av_packet_unref(pkt1);
2771 
2772  sti->codec_info_nb_frames++;
2773  count++;
2774  }
2775 
2776  if (eof_reached) {
2777  for (unsigned stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
2778  AVStream *const st = ic->streams[stream_index];
2779  AVCodecContext *const avctx = ffstream(st)->avctx;
2780  if (!has_codec_parameters(st, NULL)) {
2781  const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
2782  if (codec && !avctx->codec) {
2783  AVDictionary *opts = NULL;
2784  if (ic->codec_whitelist)
2785  av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
2786  if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
2787  av_log(ic, AV_LOG_WARNING,
2788  "Failed to open codec in %s\n",__FUNCTION__);
2789  av_dict_free(&opts);
2790  }
2791  }
2792 
2793  // EOF already reached while reading the stream above.
2794  // So continue with reoordering DTS with whatever delay we have.
2796  update_dts_from_pts(ic, stream_index, si->packet_buffer.head);
2797  }
2798  }
2799  }
2800 
2801  if (flush_codecs) {
2802  AVPacket *empty_pkt = si->pkt;
2803  int err = 0;
2804  av_packet_unref(empty_pkt);
2805 
2806  for (unsigned i = 0; i < ic->nb_streams; i++) {
2807  AVStream *const st = ic->streams[i];
2808  FFStream *const sti = ffstream(st);
2809 
2810  /* flush the decoders */
2811  if (sti->info->found_decoder == 1) {
2812  err = try_decode_frame(ic, st, empty_pkt,
2813  (options && i < orig_nb_streams)
2814  ? &options[i] : NULL);
2815 
2816  if (err < 0) {
2817  av_log(ic, AV_LOG_INFO,
2818  "decoding for stream %d failed\n", st->index);
2819  }
2820  }
2821  }
2822  }
2823 
2824  ff_rfps_calculate(ic);
2825 
2826  for (unsigned i = 0; i < ic->nb_streams; i++) {
2827  AVStream *const st = ic->streams[i];
2828  FFStream *const sti = ffstream(st);
2829  AVCodecContext *const avctx = sti->avctx;
2830 
2831  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2832  if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
2833  uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
2835  avctx->codec_tag= tag;
2836  }
2837 
2838  /* estimate average framerate if not set by demuxer */
2839  if (sti->info->codec_info_duration_fields &&
2840  !st->avg_frame_rate.num &&
2841  sti->info->codec_info_duration) {
2842  int best_fps = 0;
2843  double best_error = 0.01;
2844  AVRational codec_frame_rate = avctx->framerate;
2845 
2846  if (sti->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
2847  sti->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
2848  sti->info->codec_info_duration < 0)
2849  continue;
2851  sti->info->codec_info_duration_fields * (int64_t) st->time_base.den,
2852  sti->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
2853 
2854  /* Round guessed framerate to a "standard" framerate if it's
2855  * within 1% of the original estimate. */
2856  for (int j = 0; j < MAX_STD_TIMEBASES; j++) {
2857  AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2858  double error = fabs(av_q2d(st->avg_frame_rate) /
2859  av_q2d(std_fps) - 1);
2860 
2861  if (error < best_error) {
2862  best_error = error;
2863  best_fps = std_fps.num;
2864  }
2865 
2866  if (si->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
2867  error = fabs(av_q2d(codec_frame_rate) /
2868  av_q2d(std_fps) - 1);
2869  if (error < best_error) {
2870  best_error = error;
2871  best_fps = std_fps.num;
2872  }
2873  }
2874  }
2875  if (best_fps)
2877  best_fps, 12 * 1001, INT_MAX);
2878  }
2879  if (!st->r_frame_rate.num) {
2880  const AVCodecDescriptor *desc = sti->codec_desc;
2881  AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
2882  AVRational fr = av_mul_q(avctx->framerate, mul);
2883 
2884  if (fr.num && fr.den && av_cmp_q(st->time_base, av_inv_q(fr)) <= 0) {
2885  st->r_frame_rate = fr;
2886  } else {
2887  st->r_frame_rate.num = st->time_base.den;
2888  st->r_frame_rate.den = st->time_base.num;
2889  }
2890  }
2891  st->codecpar->framerate = avctx->framerate;
2892  if (sti->display_aspect_ratio.num && sti->display_aspect_ratio.den) {
2893  AVRational hw_ratio = { avctx->height, avctx->width };
2895  hw_ratio);
2896  }
2897  } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2898  if (!avctx->bits_per_coded_sample)
2899  avctx->bits_per_coded_sample =
2901  // set stream disposition based on audio service type
2902  switch (avctx->audio_service_type) {
2905  break;
2908  break;
2911  break;
2914  break;
2917  break;
2918  }
2919  }
2920  }
2921 
2922  if (probesize)
2923  estimate_timings(ic, old_offset);
2924 
2925  av_opt_set_int(ic, "skip_clear", 0, AV_OPT_SEARCH_CHILDREN);
2926 
2927  if (ret >= 0 && ic->nb_streams)
2928  /* We could not have all the codec parameters before EOF. */
2929  ret = -1;
2930  for (unsigned i = 0; i < ic->nb_streams; i++) {
2931  AVStream *const st = ic->streams[i];
2932  FFStream *const sti = ffstream(st);
2933  const char *errmsg;
2934 
2935  /* if no packet was ever seen, update context now for has_codec_parameters */
2936  if (!sti->avctx_inited) {
2937  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
2939  st->codecpar->format = sti->avctx->sample_fmt;
2941  if (ret < 0)
2942  goto find_stream_info_err;
2943  }
2944  if (!has_codec_parameters(st, &errmsg)) {
2945  char buf[256];
2946  avcodec_string(buf, sizeof(buf), sti->avctx, 0);
2947  av_log(ic, AV_LOG_WARNING,
2948  "Could not find codec parameters for stream %d (%s): %s\n"
2949  "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
2950  i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
2951  } else {
2952  ret = 0;
2953  }
2954  }
2955 
2956  ret = compute_chapters_end(ic);
2957  if (ret < 0)
2958  goto find_stream_info_err;
2959 
2960  /* update the stream parameters from the internal codec contexts */
2961  for (unsigned i = 0; i < ic->nb_streams; i++) {
2962  AVStream *const st = ic->streams[i];
2963  FFStream *const sti = ffstream(st);
2964 
2965  if (sti->avctx_inited) {
2967  if (ret < 0)
2968  goto find_stream_info_err;
2969  ret = add_coded_side_data(st, sti->avctx);
2970  if (ret < 0)
2971  goto find_stream_info_err;
2972  }
2973 
2974  sti->avctx_inited = 0;
2975  }
2976 
2977 find_stream_info_err:
2978  for (unsigned i = 0; i < ic->nb_streams; i++) {
2979  AVStream *const st = ic->streams[i];
2980  FFStream *const sti = ffstream(st);
2981  if (sti->info) {
2982  av_freep(&sti->info->duration_error);
2983  av_freep(&sti->info);
2984  }
2985  avcodec_close(sti->avctx);
2986  // FIXME: avcodec_close() frees AVOption settable fields which includes ch_layout,
2987  // so we need to restore it.
2990  }
2991  if (ic->pb) {
2992  FFIOContext *const ctx = ffiocontext(ic->pb);
2993  av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
2994  avio_tell(ic->pb), ctx->bytes_read, ctx->seek_count, count);
2995  }
2996  return ret;
2997 
2998 unref_then_goto_end:
2999  av_packet_unref(pkt1);
3000  goto find_stream_info_err;
3001 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
AVSubtitle
Definition: avcodec.h:2384
avcodec_close
av_cold int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: avcodec.c:433
FFStreamInfo::fps_last_dts
int64_t fps_last_dts
Definition: demux.h:54
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1086
av_opt_get_dict_val
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
Definition: opt.c:1120
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AVCodec
AVCodec.
Definition: codec.h:187
ff_rfps_add_frame
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
add frame for rfps calculation.
Definition: demux.c:2200
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
AVBSFContext::par_in
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:90
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVFMT_FLAG_DISCARD_CORRUPT
#define AVFMT_FLAG_DISCARD_CORRUPT
Discard frames marked corrupted.
Definition: avformat.h:1231
avpriv_packet_list_put
int avpriv_packet_list_put(PacketList *packet_buffer, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition: avpacket.c:536
FFStream::skip_samples
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet.
Definition: internal.h:276
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
PacketList::head
PacketListEntry * head
Definition: packet_internal.h:32
avpriv_packet_list_get
int avpriv_packet_list_get(PacketList *pkt_buffer, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: avpacket.c:575
FFStream::inject_global_side_data
int inject_global_side_data
Internal data to inject global side data.
Definition: internal.h:362
AVCodecParserContext::pts
int64_t pts
Definition: avcodec.h:2911
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:443
program
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C program
Definition: undefined.txt:6
AVFMT_DURATION_FROM_BITRATE
@ AVFMT_DURATION_FROM_BITRATE
Duration estimated from bitrate (less accurate)
Definition: avformat.h:1087
FF_FMT_INIT_CLEANUP
#define FF_FMT_INIT_CLEANUP
For an AVInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: internal.h:48
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FFFormatContext::prefer_codec_framerate
int prefer_codec_framerate
Definition: internal.h:179
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:58
av_probe_input_buffer2
int av_probe_input_buffer2(AVIOContext *pb, const AVInputFormat **fmt, const char *url, void *logctx, unsigned int offset, unsigned int max_probe_size)
Probe a bytestream to determine the input format.
Definition: format.c:250
AVCodecContext::audio_service_type
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:1141
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
av_compare_ts
int av_compare_ts(int64_t ts_a, AVRational tb_a, int64_t ts_b, AVRational tb_b)
Compare two timestamps each in its own time base.
Definition: mathematics.c:147
av_opt_set_defaults
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:1459
FF_FDEBUG_TS
#define FF_FDEBUG_TS
Definition: avformat.h:1374
AVSTREAM_EVENT_FLAG_NEW_PACKETS
#define AVSTREAM_EVENT_FLAG_NEW_PACKETS
Definition: avformat.h:986
av_add_stable
int64_t av_add_stable(AVRational ts_tb, int64_t ts, AVRational inc_tb, int64_t inc)
Add a value to a timestamp.
Definition: mathematics.c:191
avio_close
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1247
ff_find_decoder
const AVCodec * ff_find_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
Definition: avformat.c:811
FFStream::first_dts
int64_t first_dts
Timestamp corresponding to the last dts sync point.
Definition: internal.h:410
AVCodecParserContext::pict_type
int pict_type
Definition: avcodec.h:2900
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1058
ffiocontext
static av_always_inline FFIOContext * ffiocontext(AVIOContext *ctx)
Definition: avio_internal.h:82
FFStream::bsf
struct AVBSFContext * bsf
Definition: internal.h:234
FFStream::last_IP_pts
int64_t last_IP_pts
Definition: internal.h:378
ffformatcontext
static av_always_inline FFFormatContext * ffformatcontext(AVFormatContext *s)
Definition: internal.h:192
av_opt_ptr
void * av_opt_ptr(const AVClass *class, void *obj, const char *name)
Gets a pointer to the requested field in a struct.
Definition: opt.c:1838
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:57
AV_DISPOSITION_ATTACHED_PIC
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:769
AVStream::discard
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:909
av_find_program_from_stream
AVProgram * av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
Find the programs which belong to a given stream.
Definition: avformat.c:363
avcodec_parameters_from_context
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: codec_par.c:100
AV_PKT_FLAG_DISCARD
#define AV_PKT_FLAG_DISCARD
Flag is used to discard packets which are required to maintain valid decoder state but are not requir...
Definition: packet.h:436
AVCodecParserContext::duration
int duration
Duration of the current frame.
Definition: avcodec.h:3006
avcodec_string
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
Definition: avcodec.c:505
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:479
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
AVCodecContext::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire coded stream.
Definition: avcodec.h:1897
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:65
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:64
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
id3v2.h
FFStream::first_discard_sample
int64_t first_discard_sample
If not 0, the first audio sample that should be discarded from the stream.
Definition: internal.h:293
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1172
AVPacketSideData
Definition: packet.h:315
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:206
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:374
AV_CODEC_ID_DVB_TELETEXT
@ AV_CODEC_ID_DVB_TELETEXT
Definition: codec_id.h:553
extract_extradata_check
static int extract_extradata_check(AVStream *st)
Definition: demux.c:2325
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:927
AVChapter::start
int64_t start
Definition: avformat.h:1066
AVFMT_FLAG_CUSTOM_IO
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1230
data
const char data[16]
Definition: mxf.c:148
has_duration
static int has_duration(AVFormatContext *ic)
Return TRUE if the stream has accurate duration in any stream.
Definition: demux.c:1555
AVFormatContext::duration_estimation_method
enum AVDurationEstimationMethod duration_estimation_method
The duration field can be estimated through various ways, and this field can be used to know how the ...
Definition: avformat.h:1490
AVCodecParameters::framerate
AVRational framerate
Video only.
Definition: codec_par.h:225
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:196
AVFormatContext::programs
AVProgram ** programs
Definition: avformat.h:1270
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:392
ts_to_samples
static int64_t ts_to_samples(AVStream *st, int64_t ts)
Definition: demux.c:1243
mathematics.h
AVDictionary
Definition: dict.c:32
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
AVChannelLayout::order
enum AVChannelOrder order
Channel order used in this layout.
Definition: channel_layout.h:306
FFMAX
#define FFMAX(a, b)
Definition: macros.h:47
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVFormatContext::probesize
int64_t probesize
Maximum number of bytes read from input in order to determine stream properties.
Definition: avformat.h:1256
estimate_timings_from_pts
static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
Definition: demux.c:1740
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: demux.c:1455
cffstream
static const av_always_inline FFStream * cffstream(const AVStream *st)
Definition: internal.h:421
avcodec_is_open
int avcodec_is_open(AVCodecContext *s)
Definition: avcodec.c:704
AVChannelLayout::mask
uint64_t mask
This member must be used for AV_CHANNEL_ORDER_NATIVE, and may be used for AV_CHANNEL_ORDER_AMBISONIC ...
Definition: channel_layout.h:333
AVChannelLayout::nb_channels
int nb_channels
Number of channels in this layout.
Definition: channel_layout.h:311
wrap_timestamp
static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
Definition: demux.c:50
FFStream::last_dts_for_order_check
int64_t last_dts_for_order_check
Internal data to analyze DTS and detect faulty mpeg streams.
Definition: internal.h:355
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:552
av_bsf_free
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:53
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:344
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:484
update_stream_timings
static void update_stream_timings(AVFormatContext *ic)
Estimate the stream timings from the one of each components.
Definition: demux.c:1572
FFIOContext
Definition: avio_internal.h:29
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:429
sample_rate
sample_rate
Definition: ffmpeg_filter.c:354
DURATION_MAX_READ_SIZE
#define DURATION_MAX_READ_SIZE
Definition: demux.c:1736
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:705
avcodec_pix_fmt_to_codec_tag
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt)
Return a value representing the fourCC code associated to the pixel format pix_fmt,...
Definition: raw.c:306
av_gcd
int64_t av_gcd(int64_t a, int64_t b)
Compute the greatest common divisor of two integer operands.
Definition: mathematics.c:37
genpts
static int genpts
Definition: ffplay.c:329
av_filename_number_test
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:125
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:302
avformat_queue_attached_pictures
int avformat_queue_attached_pictures(AVFormatContext *s)
Definition: demux_utils.c:93
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1785
framerate
int framerate
Definition: h264_levels.c:65
avformat_close_input
void avformat_close_input(AVFormatContext **ps)
Close an opened input AVFormatContext.
Definition: demux.c:371
AVPacketSideData::size
size_t size
Definition: packet.h:317
AVCodecParserContext::offset
int64_t offset
byte offset from starting packet start
Definition: avcodec.h:2932
MAX_STD_TIMEBASES
#define MAX_STD_TIMEBASES
Definition: demux.h:29
AVFormatContext::interrupt_callback
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1368
AVCodecParserContext::key_frame
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:2941
FFStream::dts_ordered
uint8_t dts_ordered
Definition: internal.h:356
finish
static void finish(void)
Definition: movenc.c:342
FFStream::last_IP_duration
int last_IP_duration
Definition: internal.h:379
force_codec_ids
static void force_codec_ids(AVFormatContext *s, AVStream *st)
Definition: demux.c:397
bsf.h
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:444
ffstream
static av_always_inline FFStream * ffstream(AVStream *st)
Definition: internal.h:416
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2089
update_initial_timestamps
static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts, int64_t pts, AVPacket *pkt)
Definition: demux.c:823
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1748
fail
#define fail()
Definition: checkasm.h:137
av_add_index_entry
int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Add an index entry into a sorted list.
Definition: seek.c:120
AVSTREAM_PARSE_FULL_ONCE
@ AVSTREAM_PARSE_FULL_ONCE
full parsing and repack of the first frame only, only implemented for H.264 currently
Definition: avformat.h:691
FFStreamInfo::fps_first_dts
int64_t fps_first_dts
Those are used for average framerate estimation.
Definition: demux.h:52
FFStream::inited
int inited
Definition: internal.h:235
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:500
AVERROR_OPTION_NOT_FOUND
#define AVERROR_OPTION_NOT_FOUND
Option not found.
Definition: error.h:63
FFStream::avctx
struct AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:224
AVChapter
Definition: avformat.h:1063
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:480
AVFMT_DURATION_FROM_PTS
@ AVFMT_DURATION_FROM_PTS
Duration accurately estimated from PTSes.
Definition: avformat.h:1085
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AVPROBE_PADDING_SIZE
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition: avformat.h:465
av_parser_init
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:32
pts
static int64_t pts
Definition: transcode_aac.c:643
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:441
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:897
FFFormatContext::metafree
int metafree
Contexts and child contexts do not contain a metadata option.
Definition: internal.h:189
av_probe_input_format3
const AVInputFormat * av_probe_input_format3(const AVProbeData *pd, int is_opened, int *score_ret)
Guess the file format.
Definition: format.c:153
AV_PTS_WRAP_IGNORE
#define AV_PTS_WRAP_IGNORE
Options for behavior on timestamp wrap detection.
Definition: avformat.h:827
ff_check_interrupt
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:664
av_reduce
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:35
extract_extradata_init
static int extract_extradata_init(AVStream *st)
Definition: demux.c:2341
AVCodecParserContext::dts
int64_t dts
Definition: avcodec.h:2912
AVRational::num
int num
Numerator.
Definition: rational.h:59
FFDIFFSIGN
#define FFDIFFSIGN(x, y)
Comparator.
Definition: macros.h:45
ff_id3v2_parse_chapters
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *cur)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition: id3v2.c:1189
avsubtitle_free
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition: avcodec.c:411
raw.h
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:547
a1
#define a1
Definition: regdef.h:47
AVFormatContext::bit_rate
int64_t bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1213
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:583
avcodec_decode_subtitle2
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, const AVPacket *avpkt)
Decode a subtitle message.
Definition: decode.c:874
AV_DISPOSITION_CLEAN_EFFECTS
#define AV_DISPOSITION_CLEAN_EFFECTS
The audio stream contains music and sound effects without voice.
Definition: avformat.h:761
AVFormatContext::max_ts_probe
int max_ts_probe
Maximum number of packets to read while waiting for the first timestamp.
Definition: avformat.h:1424
avassert.h
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:206
pkt
AVPacket * pkt
Definition: movenc.c:59
chapter_start_cmp
static int chapter_start_cmp(const void *p1, const void *p2)
Definition: demux.c:2104
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AV_CODEC_CAP_EXPERIMENTAL
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: codec.h:102
AVInputFormat
Definition: avformat.h:546
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:430
ID3v2ExtraMeta
Definition: id3v2.h:84
AVFormatContext::ctx_flags
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1153
duration
int64_t duration
Definition: movenc.c:64
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *filename, const AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: demux.c:223
av_opt_set_dict
int av_opt_set_dict(void *obj, AVDictionary **options)
Set all the options from a given dictionary on an object.
Definition: opt.c:1767
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:537
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:738
FFFormatContext::parse_queue
PacketList parse_queue
Packets split by the parser get queued here.
Definition: internal.h:121
AVChapter::end
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1066
AVCodecDescriptor
This struct describes the properties of a single codec described by an AVCodecID.
Definition: codec_desc.h:38
intreadwrite.h
s
#define s(width, name)
Definition: cbs_vp9.c:256
FFFormatContext::packet_buffer
PacketList packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: internal.h:106
FFStreamInfo::last_dts
int64_t last_dts
Definition: demux.h:31
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1222
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:138
duration_name
static const char *const duration_name[]
Definition: demux.c:1880
AVFormatContext::nb_programs
unsigned int nb_programs
Definition: avformat.h:1269
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:551
AVFormatContext::iformat
const struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1116
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
frame_size
int frame_size
Definition: mxfenc.c:2307
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:440
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
info
MIPS optimizations info
Definition: mips.txt:2
RELATIVE_TS_BASE
#define RELATIVE_TS_BASE
Definition: demux.h:64
FFStream::nb_decoded_frames
int nb_decoded_frames
Number of internally decoded frames, used internally in libavformat, do not access its lifetime diffe...
Definition: internal.h:306
av_opt_set_dict_val
FF_ENABLE_DEPRECATION_WARNINGS int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:767
avpriv_h264_has_num_reorder_frames
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
Definition: h264dec.c:57
avcodec_receive_frame
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder or encoder (when the AV_CODEC_FLAG_RECON_FRAME flag is used...
Definition: avcodec.c:709
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
AVPacketSideData::data
uint8_t * data
Definition: packet.h:316
FFFormatContext::data_offset
int64_t data_offset
offset of the first packet
Definition: internal.h:109
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FFStream::codec_info_nb_frames
int codec_info_nb_frames
Number of frames that have been demuxed during avformat_find_stream_info()
Definition: internal.h:393
AVBSFContext::time_base_in
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: bsf.h:102
FFStream::pts_reorder_error_count
uint8_t pts_reorder_error_count[MAX_REORDER_DELAY+1]
Definition: internal.h:348
av_rescale_q
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
FFStream::display_aspect_ratio
AVRational display_aspect_ratio
display aspect ratio (0 if unknown)
Definition: internal.h:369
AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED
@ AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED
Definition: defs.h:83
av_bsf_alloc
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:105
DURATION_MAX_RETRY
#define DURATION_MAX_RETRY
Definition: demux.c:1737
AV_CODEC_ID_CODEC2
@ AV_CODEC_ID_CODEC2
Definition: codec_id.h:507
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:388
AVProgram::start_time
int64_t start_time
Definition: avformat.h:1048
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
FFStream::pts_reorder_error
int64_t pts_reorder_error[MAX_REORDER_DELAY+1]
Internal data to generate dts from pts.
Definition: internal.h:347
determinable_frame_size
static int determinable_frame_size(const AVCodecContext *avctx)
Definition: demux.c:1940
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:79
PacketList::tail
PacketListEntry * tail
Definition: packet_internal.h:32
AVFMT_NEEDNUMBER
#define AVFMT_NEEDNUMBER
Needs 'd' in filename.
Definition: avformat.h:469
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:445
FFStreamInfo::found_decoder
int found_decoder
0 -> decoder has not been searched for yet.
Definition: demux.h:45
AVFormatContext::max_analyze_duration
int64_t max_analyze_duration
Maximum duration (in AV_TIME_BASE units) of the data read from input in avformat_find_stream_info().
Definition: avformat.h:1264
AVCodecDescriptor::props
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: codec_desc.h:54
AVFMT_FLAG_NOPARSE
#define AVFMT_FLAG_NOPARSE
Do not use AVParsers, you also must set AVFMT_FLAG_NOFILLIN as the fillin code works on frames and no...
Definition: avformat.h:1228
fields
the definition of that something depends on the semantic of the filter The callback must examine the status of the filter s links and proceed accordingly The status of output links is stored in the status_in and status_out fields and tested by the then the processing requires a frame on this link and the filter is expected to make efforts in that direction The status of input links is stored by the fifo and status_out fields
Definition: filter_design.txt:155
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
if
if(ret)
Definition: filter_design.txt:179
av_probe_input_format2
const AVInputFormat * av_probe_input_format2(const AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: format.c:232
get_std_framerate
static int get_std_framerate(int i)
Definition: demux.c:2151
has_codec_parameters
static int has_codec_parameters(const AVStream *st, const char **errmsg_ptr)
Definition: demux.c:1953
FFFormatContext
Definition: internal.h:72
AVCodecParserContext::repeat_pict
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:2910
FFStream::need_parsing
enum AVStreamParseType need_parsing
Definition: internal.h:387
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: defs.h:76
AVFormatContext
Format I/O context.
Definition: avformat.h:1104
opts
AVDictionary * opts
Definition: movenc.c:50
AVPacket::buf
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: packet.h:357
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:861
av_bsf_init
int av_bsf_init(AVBSFContext *ctx)
Prepare the filter for use, after all the parameters and options have been set.
Definition: bsf.c:150
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
fabs
static __device__ float fabs(float a)
Definition: cuda_runtime.h:182
AVStream::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avformat.h:877
NULL
#define NULL
Definition: coverity.c:32
FFStream::avctx_inited
int avctx_inited
1 if avctx has been initialized with the values from the codec parameters
Definition: internal.h:228
avcodec_find_decoder_by_name
const AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
Definition: allcodecs.c:998
av_match_list
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
Definition: avstring.c:445
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1055
av_bsf_receive_packet
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:231
FFStreamInfo::codec_info_duration
int64_t codec_info_duration
Definition: demux.h:36
AVFormatContext::fps_probe_size
int fps_probe_size
The number of frames used for determining the framerate in avformat_find_stream_info().
Definition: avformat.h:1350
AVRational
Rational number (pair of numerator and denominator).
Definition: rational.h:58
AVFMT_DURATION_FROM_STREAM
@ AVFMT_DURATION_FROM_STREAM
Duration estimated from a stream with a known duration.
Definition: avformat.h:1086
AVCodecContext::nb_coded_side_data
int nb_coded_side_data
Definition: avcodec.h:1898
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:485
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:318
AV_DISPOSITION_COMMENT
#define AV_DISPOSITION_COMMENT
The stream is a commentary track.
Definition: avformat.h:734
AVSTREAM_PARSE_NONE
@ AVSTREAM_PARSE_NONE
Definition: avformat.h:687
update_initial_durations
static void update_initial_durations(AVFormatContext *s, AVStream *st, int stream_index, int64_t duration)
Definition: demux.c:876
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
ff_id3v2_parse_apic
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
Definition: id3v2.c:1158
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1146
probe_codec
static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition: demux.c:419
AVDurationEstimationMethod
AVDurationEstimationMethod
The duration of a video can be estimated through various ways, and this enum can be used to know how ...
Definition: avformat.h:1084
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
AV_ROUND_NEAR_INF
@ AV_ROUND_NEAR_INF
Round to nearest and halfway cases away from zero.
Definition: mathematics.h:135
get_next_pkt
static PacketListEntry * get_next_pkt(AVFormatContext *s, AVStream *st, PacketListEntry *pktl)
Definition: demux.c:740
double
double
Definition: af_crystalizer.c:132
avcodec_open2
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: avcodec.c:117
AVCodecParserContext::flags
int flags
Definition: avcodec.h:2925
time.h
AVFormatContext::skip_estimate_duration_from_pts
int skip_estimate_duration_from_pts
Skip duration calcuation in estimate_timings_from_pts.
Definition: avformat.h:1693
FFStreamInfo::codec_info_duration_fields
int64_t codec_info_duration_fields
Definition: demux.h:37
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:430
AVCodecParameters::ch_layout
AVChannelLayout ch_layout
Audio only.
Definition: codec_par.h:213
PacketListEntry::next
struct PacketListEntry * next
Definition: packet_internal.h:27
read_frame_internal
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
Definition: demux.c:1248
add_coded_side_data
static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
Definition: demux.c:2429
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
FAIL
#define FAIL(errmsg)
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:178
av_opt_set_int
int av_opt_set_int(void *obj, const char *name, int64_t val, int search_flags)
Definition: opt.c:624
duration_estimate_name
static const char * duration_estimate_name(enum AVDurationEstimationMethod method)
Definition: demux.c:1886
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:49
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:480
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1160
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
FFFormatContext::id3v2_meta
AVDictionary * id3v2_meta
ID3v2 tag useful for MP3 demuxing.
Definition: internal.h:174
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:442
AVFMT_FLAG_NOFILLIN
#define AVFMT_FLAG_NOFILLIN
Do not infer any values from other values, just return what is stored in the container.
Definition: avformat.h:1227
av_codec_is_decoder
int av_codec_is_decoder(const AVCodec *codec)
Definition: utils.c:83
av_sat_sub64
#define av_sat_sub64
Definition: common.h:140
av_rescale_rnd
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
options
const OptionDef options[]
avformat_find_stream_info
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: demux.c:2442
f
f
Definition: af_crystalizer.c:122
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
av_ts2timestr
#define av_ts2timestr(ts, tb)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:76
AVMediaType
AVMediaType
Definition: avutil.h:199
AVCodecParserContext::frame_offset
int64_t frame_offset
Definition: avcodec.h:2895
AV_PTS_WRAP_SUB_OFFSET
#define AV_PTS_WRAP_SUB_OFFSET
subtract the format specific offset on wrap detection
Definition: avformat.h:829
AVPacket::size
int size
Definition: packet.h:375
avpriv_pix_fmt_find
enum AVPixelFormat avpriv_pix_fmt_find(enum PixelFormatTagLists list, unsigned fourcc)
Definition: raw.c:353
FFFormatContext::parse_pkt
AVPacket * parse_pkt
The generic code uses this as a temporary packet to parse packets or for muxing, especially flushing.
Definition: internal.h:135
FFStreamInfo
Definition: demux.h:30
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:166
init_input
static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
Definition: demux.c:152
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:267
FFStream
Definition: internal.h:197
shift
static int shift(int a, int b)
Definition: bonk.c:262
av_bsf_send_packet
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:203
AVProgram::end_time
int64_t end_time
Definition: avformat.h:1049
start_time
static int64_t start_time
Definition: ffplay.c:326
AV_CODEC_ID_DTS
@ AV_CODEC_ID_DTS
Definition: codec_id.h:444
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1074
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
sample
#define sample
Definition: flacdsp_template.c:44
FFStreamInfo::duration_count
int duration_count
Definition: demux.h:33
size
int size
Definition: twinvq_data.h:10344
ID3v2_DEFAULT_MAGIC
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
PIX_FMT_LIST_RAW
@ PIX_FMT_LIST_RAW
Definition: raw.h:40
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
AV_CODEC_PROP_FIELDS
#define AV_CODEC_PROP_FIELDS
Video codec supports separate coding of fields in interlaced frames.
Definition: codec_desc.h:97
update_wrap_reference
static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
Definition: demux.c:472
AVStream::event_flags
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition: avformat.h:973
FFStream::dts_misordered
uint8_t dts_misordered
Definition: internal.h:357
av_get_audio_frame_duration2
int av_get_audio_frame_duration2(AVCodecParameters *par, int frame_bytes)
This function is the same as av_get_audio_frame_duration(), except it works with AVCodecParameters in...
Definition: utils.c:837
PacketListEntry::pkt
AVPacket pkt
Definition: packet_internal.h:28
AV_PTS_WRAP_ADD_OFFSET
#define AV_PTS_WRAP_ADD_OFFSET
add the format specific offset on wrap detection
Definition: avformat.h:828
AVFMT_FLAG_IGNDTS
#define AVFMT_FLAG_IGNDTS
Ignore DTS on frames that contain both DTS & PTS.
Definition: avformat.h:1226
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:468
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:916
set_codec_from_probe_data
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd)
Definition: demux.c:100
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:162
AV_DISPOSITION_HEARING_IMPAIRED
#define AV_DISPOSITION_HEARING_IMPAIRED
The stream is intended for hearing impaired audiences.
Definition: avformat.h:753
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:563
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
update_stream_avctx
static int update_stream_avctx(AVFormatContext *s)
Definition: demux.c:183
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AV_CODEC_ID_RV30
@ AV_CODEC_ID_RV30
Definition: codec_id.h:120
offset
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Definition: writing_filters.txt:86
FFStream::pts_wrap_behavior
int pts_wrap_behavior
Options for behavior, when a wrap is detected.
Definition: internal.h:335
FFStreamInfo::last_duration
int64_t last_duration
Definition: demux.h:47
AV_CHANNEL_ORDER_NATIVE
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
Definition: channel_layout.h:118
FFFormatContext::raw_packet_buffer_size
int raw_packet_buffer_size
Sum of the size of packets in raw_packet_buffer, in bytes.
Definition: internal.h:146
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:380
av_packet_make_refcounted
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
Definition: avpacket.c:485
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:223
FFStreamInfo::rfps_duration_sum
int64_t rfps_duration_sum
Definition: demux.h:34
FFStream::update_initial_durations_done
int update_initial_durations_done
Internal data to prevent doing update_initial_durations() twice.
Definition: internal.h:340
FFStream::start_skip_samples
int64_t start_skip_samples
If not 0, the number of samples that should be skipped from the start of the stream (the samples are ...
Definition: internal.h:285
bitrate
int64_t bitrate
Definition: h264_levels.c:131
estimate_timings_from_bit_rate
static void estimate_timings_from_bit_rate(AVFormatContext *ic)
Definition: demux.c:1683
FFStream::probe_packets
int probe_packets
Number of packets to buffer for codec probing.
Definition: internal.h:384
a0
#define a0
Definition: regdef.h:46
ff_rfps_calculate
void ff_rfps_calculate(AVFormatContext *ic)
Definition: demux.c:2261
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
FFERROR_REDO
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data).
Definition: demux.h:62
FFStream::probe_data
AVProbeData probe_data
Definition: internal.h:371
AVStream::side_data
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:956
AVCodec::id
enum AVCodecID id
Definition: codec.h:201
filesize
static int64_t filesize(AVIOContext *pb)
Definition: ffmpeg_mux.c:48
FFStream::skip_to_keyframe
int skip_to_keyframe
Indicates that everything up to the next keyframe should be discarded.
Definition: internal.h:271
FFStreamInfo::duration_error
double(* duration_error)[2][MAX_STD_TIMEBASES]
Definition: demux.h:35
AV_CODEC_ID_GIF
@ AV_CODEC_ID_GIF
Definition: codec_id.h:149
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:442
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1504
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:613
avio_closep
int avio_closep(AVIOContext **s)
Close the resource accessed by the AVIOContext *s, free it and set the pointer pointing to it to NULL...
Definition: aviobuf.c:1280
FFFormatContext::raw_packet_buffer
PacketList raw_packet_buffer
Raw packets from the demuxer, prior to parsing and decoding.
Definition: internal.h:117
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVFMT_FLAG_NOBUFFER
#define AVFMT_FLAG_NOBUFFER
Do not buffer frames when possible.
Definition: avformat.h:1229
ff_is_intra_only
int ff_is_intra_only(enum AVCodecID id)
Definition: avformat.c:849
AV_CODEC_ID_RV40
@ AV_CODEC_ID_RV40
Definition: codec_id.h:121
AVCodecParserContext::pos
int64_t pos
Byte position of currently parsed frame in stream.
Definition: avcodec.h:2994
PacketListEntry
Definition: packet_internal.h:26
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:2926
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:367
avio_internal.h
ff_read_packet
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: demux.c:540
AVChapter::id
int64_t id
unique ID to identify the chapter
Definition: avformat.h:1064
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:536
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
Definition: avpacket.c:251
internal.h
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: codec_par.c:184
find_probe_decoder
static const AVCodec * find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
Definition: demux.c:70
AV_TIME_BASE
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:254
ff_id3v2_read_dict
void ff_id3v2_read_dict(AVIOContext *pb, AVDictionary **metadata, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag into specified dictionary and retrieve supported extra metadata.
Definition: id3v2.c:1130
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVFormatContext::codec_whitelist
char * codec_whitelist
',' separated list of allowed decoders.
Definition: avformat.h:1546
MAX_REORDER_DELAY
@ MAX_REORDER_DELAY
Definition: vaapi_encode.h:45
compute_frame_duration
static void compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Return the frame duration in seconds.
Definition: demux.c:658
delta
float delta
Definition: vorbis_enc_data.h:430
AV_ROUND_DOWN
@ AV_ROUND_DOWN
Round toward -infinity.
Definition: mathematics.h:133
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:226
AV_DISPOSITION_KARAOKE
#define AV_DISPOSITION_KARAOKE
The stream contains karaoke audio.
Definition: avformat.h:742
ff_wrap_timestamp
int64_t ff_wrap_timestamp(const AVStream *st, int64_t timestamp)
Wrap a given time stamp, if there is an indication for an overflow.
Definition: demux.c:65
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
url.h
FFStream::pts_buffer
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition: internal.h:350
av_find_default_stream_index
int av_find_default_stream_index(AVFormatContext *s)
Definition: avformat.c:378
AVPROBE_SCORE_RETRY
#define AVPROBE_SCORE_RETRY
Definition: avformat.h:458
AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED
@ AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED
Definition: defs.h:82
av_get_audio_frame_duration
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition: utils.c:819
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AVProgram
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1028
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
demux.h
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:157
len
int len
Definition: vorbis_enc_data.h:426
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:140
av_rescale
int64_t av_rescale(int64_t a, int64_t b, int64_t c)
Rescale a 64-bit integer with rounding to nearest.
Definition: mathematics.c:129
AVCodecContext::height
int height
Definition: avcodec.h:615
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:652
FFStreamInfo::frame_delay_evidence
int frame_delay_evidence
Definition: demux.h:38
av_codec_iterate
const AVCodec * av_codec_iterate(void **opaque)
Iterate over all registered codecs.
Definition: allcodecs.c:921
try_decode_frame
static int try_decode_frame(AVFormatContext *s, AVStream *st, const AVPacket *pkt, AVDictionary **options)
Definition: demux.c:2002
avcodec.h
limit
static double limit(double x)
Definition: vf_pseudocolor.c:130
AVCodecParserContext
Definition: avcodec.h:2892
av_cmp_q
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:89
AVStream::disposition
int disposition
Stream disposition - a combination of AV_DISPOSITION_* flags.
Definition: avformat.h:907
AVFMT_FLAG_GENPTS
#define AVFMT_FLAG_GENPTS
Generate missing pts even if it requires parsing future frames.
Definition: avformat.h:1223
AV_DISPOSITION_VISUAL_IMPAIRED
#define AV_DISPOSITION_VISUAL_IMPAIRED
The stream is intended for visually impaired audiences.
Definition: avformat.h:757
tag
uint32_t tag
Definition: movenc.c:1709
av_compare_mod
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
Compare the remainders of two integer operands divided by a common divisor.
Definition: mathematics.c:160
AVStream::id
int id
Format-specific stream ID.
Definition: avformat.h:850
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:838
pixfmt.h
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:252
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
tb_unreliable
static int tb_unreliable(AVFormatContext *ic, AVStream *st)
Definition: demux.c:2175
AVStream::nb_side_data
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:960
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:689
avformat.h
dict.h
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: defs.h:88
AVPacket::side_data
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:385
av_sat_add64
#define av_sat_add64
Definition: common.h:137
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
id
enum AVCodecID id
Definition: dts2pts_bsf.c:364
AVCodecContext
main external API structure.
Definition: avcodec.h:435
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:844
FFStreamInfo::fps_first_dts_idx
int fps_first_dts_idx
Definition: demux.h:53
AVChannelLayout::u
union AVChannelLayout::@321 u
Details about which channels are present in this layout.
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:41
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
FFStreamInfo::duration_gcd
int64_t duration_gcd
Definition: demux.h:32
AVBitStreamFilter
Definition: bsf.h:111
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
Definition: avpacket.c:230
AVInputFormat::flags
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_NOTIMESTAMPS,...
Definition: avformat.h:565
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:56
AVRational::den
int den
Denominator.
Definition: rational.h:60
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
AV_AUDIO_SERVICE_TYPE_COMMENTARY
@ AV_AUDIO_SERVICE_TYPE_COMMENTARY
Definition: defs.h:85
ff_flush_packet_queue
void ff_flush_packet_queue(AVFormatContext *s)
Definition: avformat.c:86
update_dts_from_pts
static void update_dts_from_pts(AVFormatContext *s, int stream_index, PacketListEntry *pkt_buffer)
Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts of the packets in a wind...
Definition: demux.c:798
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: avformat.c:96
PARSER_FLAG_USE_CODEC_TS
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:2930
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
AVStream::r_frame_rate
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:996
compute_pkt_fields
static void compute_pkt_fields(AVFormatContext *s, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt, int64_t next_dts, int64_t next_pts)
Definition: demux.c:935
AVSTREAM_PARSE_FULL_RAW
@ AVSTREAM_PARSE_FULL_RAW
full parsing and repack with timestamp and position generation by parser for raw this assumes that ea...
Definition: avformat.h:692
FFStream::info
struct FFStreamInfo * info
Stream information used internally by avformat_find_stream_info()
Definition: internal.h:250
AVFormatContext::duration
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1206
av_mul_q
AVRational av_mul_q(AVRational b, AVRational c)
Multiply two rationals.
Definition: rational.c:80
AVPacket::stream_index
int stream_index
Definition: packet.h:376
AVPROBE_SCORE_STREAM_RETRY
#define AVPROBE_SCORE_STREAM_RETRY
Definition: avformat.h:459
parse_packet
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index, int flush)
Parse a packet, add all split parts to parse_queue.
Definition: demux.c:1128
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:339
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:639
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:623
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:443
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:270
desc
const char * desc
Definition: libsvtav1.c:83
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
av_parser_parse2
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:115
extract_extradata
static int extract_extradata(FFFormatContext *si, AVStream *st, const AVPacket *pkt)
Definition: demux.c:2380
flush
void(* flush)(AVBSFContext *ctx)
Definition: dts2pts_bsf.c:367
packet_internal.h
FFFormatContext::pkt
AVPacket * pkt
Used to hold temporary packets for the generic demuxing code.
Definition: internal.h:142
llrint
#define llrint(x)
Definition: libm.h:394
AVCodecParameters::format
int format
Definition: codec_par.h:86
FFStream::request_probe
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition: internal.h:266
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:460
AV_AUDIO_SERVICE_TYPE_EFFECTS
@ AV_AUDIO_SERVICE_TYPE_EFFECTS
Definition: defs.h:81
AV_ROUND_PASS_MINMAX
@ AV_ROUND_PASS_MINMAX
Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
Definition: mathematics.h:159
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:62
AVPacket
This structure stores compressed data.
Definition: packet.h:351
fill_all_stream_timings
static void fill_all_stream_timings(AVFormatContext *ic)
Definition: demux.c:1666
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:86
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:237
select_from_pts_buffer
static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
Definition: demux.c:751
ff_id3v2_free_extra_meta
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:1142
FFStream::cur_dts
int64_t cur_dts
Definition: internal.h:411
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:394
av_stream_new_side_data
uint8_t * av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type, size_t size)
Allocate new information from stream.
Definition: avformat.c:191
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:615
timestamp.h
FFStream::need_context_update
int need_context_update
Whether the internal avctx needs to be updated from codecpar (after a late change to codecpar)
Definition: internal.h:241
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:91
ff_id3v2_parse_priv
int ff_id3v2_parse_priv(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Add metadata for all PRIV tags in the ID3v2 header.
Definition: id3v2.c:1254
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVFormatContext::start_time
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1196
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:489
FFStreamInfo::fps_last_dts_idx
int fps_last_dts_idx
Definition: demux.h:55
FFStream::parser
struct AVCodecParserContext * parser
Definition: internal.h:388
AV_CODEC_CAP_AVOID_PROBING
#define AV_CODEC_CAP_AVOID_PROBING
Decoder is not a preferred choice for probing.
Definition: codec.h:138
av_ts2str
#define av_ts2str(ts)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: timestamp.h:54
estimate_timings
static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
Definition: demux.c:1891
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:58
avcodec_descriptor_get
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3694
has_decode_delay_been_guessed
static int has_decode_delay_been_guessed(AVStream *st)
Definition: demux.c:721
is_relative
static av_always_inline int is_relative(int64_t ts)
Definition: demux.h:66
AVStream::start_time
int64_t start_time
Decoding: pts of the first frame of the stream in presentation order, in stream time base.
Definition: avformat.h:887
PARSER_FLAG_ONCE
#define PARSER_FLAG_ONCE
Definition: avcodec.h:2927
FFStream::codec_desc
const AVCodecDescriptor * codec_desc
Definition: internal.h:413
AV_CODEC_ID_APTX
@ AV_CODEC_ID_APTX
Definition: codec_id.h:525
avstring.h
ff_reduce_index
void ff_reduce_index(AVFormatContext *s, int stream_index)
Ensure the index uses less memory than the maximum specified in AVFormatContext.max_index_size by dis...
Definition: seek.c:48
AVSTREAM_PARSE_TIMESTAMPS
@ AVSTREAM_PARSE_TIMESTAMPS
full parsing and interpolation of timestamps for frames not starting on a packet boundary
Definition: avformat.h:690
AVDiscard
AVDiscard
Definition: defs.h:67
AVStream::pts_wrap_bits
int pts_wrap_bits
Number of bits in timestamps.
Definition: avformat.h:1005
AVChapter::time_base
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1065
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:54
FFStream::extract_extradata
struct FFStream::@290 extract_extradata
AV_CODEC_ID_MP1
@ AV_CODEC_ID_MP1
Definition: codec_id.h:482
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1132
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
FFStream::pts_wrap_reference
int64_t pts_wrap_reference
Internal data to check for wrapping of the time stamp.
Definition: internal.h:323
av_rescale_q_rnd
int64_t av_rescale_q_rnd(int64_t a, AVRational bq, AVRational cq, enum AVRounding rnd)
Rescale a 64-bit integer by 2 rational numbers with specified rounding.
Definition: mathematics.c:134
compute_chapters_end
static int compute_chapters_end(AVFormatContext *s)
Definition: demux.c:2114
avpriv_codec_get_cap_skip_frame_fill_param
int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec)
Definition: utils.c:438
AVFMT_EVENT_FLAG_METADATA_UPDATED
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1418
AVInputFormat::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:576
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:75
AVPacket::side_data_elems
int side_data_elems
Definition: packet.h:386
av_parser_close
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:193
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:153
av_bsf_get_by_name
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
Definition: bitstream_filters.c:83