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