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