FFmpeg
utils.c
Go to the documentation of this file.
1 /*
2  * various utility functions for use within FFmpeg
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #include <stdint.h>
23 
24 #include "config.h"
25 
26 #include "libavutil/avassert.h"
27 #include "libavutil/avstring.h"
28 #include "libavutil/dict.h"
29 #include "libavutil/internal.h"
30 #include "libavutil/mathematics.h"
31 #include "libavutil/opt.h"
32 #include "libavutil/parseutils.h"
33 #include "libavutil/pixfmt.h"
34 #include "libavutil/thread.h"
35 #include "libavutil/time.h"
36 #include "libavutil/timestamp.h"
37 
38 #include "libavcodec/bytestream.h"
39 #include "libavcodec/internal.h"
41 #include "libavcodec/raw.h"
42 
43 #include "avformat.h"
44 #include "avio_internal.h"
45 #include "id3v2.h"
46 #include "internal.h"
47 #if CONFIG_NETWORK
48 #include "network.h"
49 #endif
50 #include "url.h"
51 
52 #include "libavutil/ffversion.h"
53 const char av_format_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
54 
56 
57 /**
58  * @file
59  * various utility functions for use within FFmpeg
60  */
61 
62 unsigned avformat_version(void)
63 {
66 }
67 
68 const char *avformat_configuration(void)
69 {
70  return FFMPEG_CONFIGURATION;
71 }
72 
73 const char *avformat_license(void)
74 {
75 #define LICENSE_PREFIX "libavformat license: "
76  return &LICENSE_PREFIX FFMPEG_LICENSE[sizeof(LICENSE_PREFIX) - 1];
77 }
78 
80 {
81  return ff_mutex_lock(&avformat_mutex) ? -1 : 0;
82 }
83 
85 {
86  return ff_mutex_unlock(&avformat_mutex) ? -1 : 0;
87 }
88 
89 #define RELATIVE_TS_BASE (INT64_MAX - (1LL<<48))
90 
91 static int is_relative(int64_t ts) {
92  return ts > (RELATIVE_TS_BASE - (1LL<<48));
93 }
94 
95 /**
96  * Wrap a given time stamp, if there is an indication for an overflow
97  *
98  * @param st stream
99  * @param timestamp the time stamp to wrap
100  * @return resulting time stamp
101  */
102 static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
103 {
105  st->internal->pts_wrap_reference != AV_NOPTS_VALUE && timestamp != AV_NOPTS_VALUE) {
107  timestamp < st->internal->pts_wrap_reference)
108  return timestamp + (1ULL << st->pts_wrap_bits);
110  timestamp >= st->internal->pts_wrap_reference)
111  return timestamp - (1ULL << st->pts_wrap_bits);
112  }
113  return timestamp;
114 }
115 
116 #if FF_API_FORMAT_GET_SET
117 MAKE_ACCESSORS(AVStream, stream, AVRational, r_frame_rate)
118 #if FF_API_LAVF_FFSERVER
120 MAKE_ACCESSORS(AVStream, stream, char *, recommended_encoder_configuration)
122 #endif
125 MAKE_ACCESSORS(AVFormatContext, format, AVCodec *, subtitle_codec)
127 MAKE_ACCESSORS(AVFormatContext, format, int, metadata_header_padding)
128 MAKE_ACCESSORS(AVFormatContext, format, void *, opaque)
130 #if FF_API_OLD_OPEN_CALLBACKS
134 #endif
135 #endif
136 
137 int64_t av_stream_get_end_pts(const AVStream *st)
138 {
139  if (st->internal->priv_pts) {
140  return st->internal->priv_pts->val;
141  } else
142  return AV_NOPTS_VALUE;
143 }
144 
146 {
147  return st->parser;
148 }
149 
151 {
152  int i;
153  s->internal->inject_global_side_data = 1;
154  for (i = 0; i < s->nb_streams; i++) {
155  AVStream *st = s->streams[i];
157  }
158 }
159 
161 {
162  av_assert0(!dst->codec_whitelist &&
163  !dst->format_whitelist &&
164  !dst->protocol_whitelist &&
165  !dst->protocol_blacklist);
166  dst-> codec_whitelist = av_strdup(src->codec_whitelist);
167  dst->format_whitelist = av_strdup(src->format_whitelist);
168  dst->protocol_whitelist = av_strdup(src->protocol_whitelist);
169  dst->protocol_blacklist = av_strdup(src->protocol_blacklist);
170  if ( (src-> codec_whitelist && !dst-> codec_whitelist)
171  || (src-> format_whitelist && !dst-> format_whitelist)
172  || (src->protocol_whitelist && !dst->protocol_whitelist)
173  || (src->protocol_blacklist && !dst->protocol_blacklist)) {
174  av_log(dst, AV_LOG_ERROR, "Failed to duplicate black/whitelist\n");
175  return AVERROR(ENOMEM);
176  }
177  return 0;
178 }
179 
181 {
182 #if FF_API_LAVF_AVCTX
184  if (st->codec->codec)
185  return st->codec->codec;
187 #endif
188 
189  switch (st->codecpar->codec_type) {
190  case AVMEDIA_TYPE_VIDEO:
191  if (s->video_codec) return s->video_codec;
192  break;
193  case AVMEDIA_TYPE_AUDIO:
194  if (s->audio_codec) return s->audio_codec;
195  break;
197  if (s->subtitle_codec) return s->subtitle_codec;
198  break;
199  }
200 
202 }
203 
205 {
206  const AVCodec *codec;
207 
208 #if CONFIG_H264_DECODER
209  /* Other parts of the code assume this decoder to be used for h264,
210  * so force it if possible. */
211  if (codec_id == AV_CODEC_ID_H264)
212  return avcodec_find_decoder_by_name("h264");
213 #endif
214 
215  codec = find_decoder(s, st, codec_id);
216  if (!codec)
217  return NULL;
218 
220  const AVCodec *probe_codec = NULL;
221  void *iter = NULL;
222  while ((probe_codec = av_codec_iterate(&iter))) {
223  if (probe_codec->id == codec->id &&
226  return probe_codec;
227  }
228  }
229  }
230 
231  return codec;
232 }
233 
234 #if FF_API_FORMAT_GET_SET
235 int av_format_get_probe_score(const AVFormatContext *s)
236 {
237  return s->probe_score;
238 }
239 #endif
240 
241 /* an arbitrarily chosen "sane" max packet size -- 50M */
242 #define SANE_CHUNK_SIZE (50000000)
243 
245 {
246  if (s->maxsize>= 0) {
247  int64_t pos = avio_tell(s);
248  int64_t remaining= s->maxsize - pos;
249  if (remaining < size) {
250  int64_t newsize = avio_size(s);
251  if (!s->maxsize || s->maxsize<newsize)
252  s->maxsize = newsize - !newsize;
253  if (pos > s->maxsize && s->maxsize >= 0)
254  s->maxsize = AVERROR(EIO);
255  if (s->maxsize >= 0)
256  remaining = s->maxsize - pos;
257  }
258 
259  if (s->maxsize >= 0 && remaining < size && size > 1) {
260  av_log(NULL, remaining ? AV_LOG_ERROR : AV_LOG_DEBUG,
261  "Truncating packet of size %d to %"PRId64"\n",
262  size, remaining + !remaining);
263  size = remaining + !remaining;
264  }
265  }
266  return size;
267 }
268 
269 /* Read the data in sane-sized chunks and append to pkt.
270  * Return the number of bytes read or an error. */
272 {
273  int orig_size = pkt->size;
274  int ret;
275 
276  do {
277  int prev_size = pkt->size;
278  int read_size;
279 
280  /* When the caller requests a lot of data, limit it to the amount
281  * left in file or SANE_CHUNK_SIZE when it is not known. */
282  read_size = size;
283  if (read_size > SANE_CHUNK_SIZE/10) {
284  read_size = ffio_limit(s, read_size);
285  // If filesize/maxsize is unknown, limit to SANE_CHUNK_SIZE
286  if (s->maxsize < 0)
287  read_size = FFMIN(read_size, SANE_CHUNK_SIZE);
288  }
289 
290  ret = av_grow_packet(pkt, read_size);
291  if (ret < 0)
292  break;
293 
294  ret = avio_read(s, pkt->data + prev_size, read_size);
295  if (ret != read_size) {
296  av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
297  break;
298  }
299 
300  size -= read_size;
301  } while (size > 0);
302  if (size > 0)
304 
305  if (!pkt->size)
307  return pkt->size > orig_size ? pkt->size - orig_size : ret;
308 }
309 
311 {
312 #if FF_API_INIT_PACKET
315  pkt->data = NULL;
316  pkt->size = 0;
318 #else
320 #endif
321  pkt->pos = avio_tell(s);
322 
323  return append_packet_chunked(s, pkt, size);
324 }
325 
327 {
328  if (!pkt->size)
329  return av_get_packet(s, pkt, size);
330  return append_packet_chunked(s, pkt, size);
331 }
332 
333 int av_filename_number_test(const char *filename)
334 {
335  char buf[1024];
336  return filename &&
337  (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
338 }
339 
341  AVProbeData *pd)
342 {
343  static const struct {
344  const char *name;
345  enum AVCodecID id;
346  enum AVMediaType type;
347  } fmt_id_type[] = {
359  { "mjpeg_2000",AV_CODEC_ID_JPEG2000, AVMEDIA_TYPE_VIDEO },
361  { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
362  { "truehd", AV_CODEC_ID_TRUEHD, AVMEDIA_TYPE_AUDIO },
363  { 0 }
364  };
365  int score;
366  const AVInputFormat *fmt = av_probe_input_format3(pd, 1, &score);
367 
368  if (fmt) {
369  int i;
371  "Probe with size=%d, packets=%d detected %s with score=%d\n",
372  pd->buf_size, s->max_probe_packets - st->probe_packets,
373  fmt->name, score);
374  for (i = 0; fmt_id_type[i].name; i++) {
375  if (!strcmp(fmt->name, fmt_id_type[i].name)) {
376  if (fmt_id_type[i].type != AVMEDIA_TYPE_AUDIO &&
377  st->codecpar->sample_rate)
378  continue;
379  if (st->internal->request_probe > score &&
380  st->codecpar->codec_id != fmt_id_type[i].id)
381  continue;
382  st->codecpar->codec_id = fmt_id_type[i].id;
383  st->codecpar->codec_type = fmt_id_type[i].type;
384  st->internal->need_context_update = 1;
385 #if FF_API_LAVF_AVCTX
387  st->codec->codec_type = st->codecpar->codec_type;
388  st->codec->codec_id = st->codecpar->codec_id;
390 #endif
391  return score;
392  }
393  }
394  }
395  return 0;
396 }
397 
398 /************************************************************/
399 /* input media file */
400 
401 #if FF_API_DEMUXER_OPEN
402 int av_demuxer_open(AVFormatContext *ic) {
403  int err;
404 
405  if (ic->format_whitelist && av_match_list(ic->iformat->name, ic->format_whitelist, ',') <= 0) {
406  av_log(ic, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", ic->format_whitelist);
407  return AVERROR(EINVAL);
408  }
409 
410  if (ic->iformat->read_header) {
411  err = ic->iformat->read_header(ic);
412  if (err < 0)
413  return err;
414  }
415 
416  if (ic->pb && !ic->internal->data_offset)
417  ic->internal->data_offset = avio_tell(ic->pb);
418 
419  return 0;
420 }
421 #endif
422 /* Open input file and probe the format if necessary. */
423 static int init_input(AVFormatContext *s, const char *filename,
425 {
426  int ret;
427  AVProbeData pd = { filename, NULL, 0 };
428  int score = AVPROBE_SCORE_RETRY;
429 
430  if (s->pb) {
431  s->flags |= AVFMT_FLAG_CUSTOM_IO;
432  if (!s->iformat)
433  return av_probe_input_buffer2(s->pb, &s->iformat, filename,
434  s, 0, s->format_probesize);
435  else if (s->iformat->flags & AVFMT_NOFILE)
436  av_log(s, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
437  "will be ignored with AVFMT_NOFILE format.\n");
438  return 0;
439  }
440 
441  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
442  (!s->iformat && (s->iformat = av_probe_input_format2(&pd, 0, &score))))
443  return score;
444 
445  if ((ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ | s->avio_flags, options)) < 0)
446  return ret;
447 
448  if (s->iformat)
449  return 0;
450  return av_probe_input_buffer2(s->pb, &s->iformat, filename,
451  s, 0, s->format_probesize);
452 }
453 
455 {
456  int i, ret;
457  for (i = 0; i < s->nb_streams; i++)
458  if (s->streams[i]->disposition & AV_DISPOSITION_ATTACHED_PIC &&
459  s->streams[i]->discard < AVDISCARD_ALL) {
460  if (s->streams[i]->attached_pic.size <= 0) {
462  "Attached picture on stream %d has invalid size, "
463  "ignoring\n", i);
464  continue;
465  }
466 
467  ret = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
468  &s->internal->raw_packet_buffer_end,
469  &s->streams[i]->attached_pic,
470  av_packet_ref, 0);
471  if (ret < 0)
472  return ret;
473  }
474  return 0;
475 }
476 
478 {
479  int i, ret;
480  for (i = 0; i < s->nb_streams; i++) {
481  AVStream *st = s->streams[i];
482 
483  if (!st->internal->need_context_update)
484  continue;
485 
486  /* close parser, because it depends on the codec */
487  if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
488  av_parser_close(st->parser);
489  st->parser = NULL;
490  }
491 
492  /* update internal codec context, for the parser */
494  if (ret < 0)
495  return ret;
496 
497 #if FF_API_LAVF_AVCTX
499  /* update deprecated public codec context */
500  ret = avcodec_parameters_to_context(st->codec, st->codecpar);
501  if (ret < 0)
502  return ret;
504 #endif
505 
506  st->internal->need_context_update = 0;
507  }
508  return 0;
509 }
510 
511 
512 int avformat_open_input(AVFormatContext **ps, const char *filename,
514 {
515  AVFormatContext *s = *ps;
516  int i, ret = 0;
517  AVDictionary *tmp = NULL;
518  ID3v2ExtraMeta *id3v2_extra_meta = NULL;
519 
520  if (!s && !(s = avformat_alloc_context()))
521  return AVERROR(ENOMEM);
522  if (!s->av_class) {
523  av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
524  return AVERROR(EINVAL);
525  }
526  if (fmt)
527  s->iformat = fmt;
528 
529  if (options)
530  av_dict_copy(&tmp, *options, 0);
531 
532  if (s->pb) // must be before any goto fail
533  s->flags |= AVFMT_FLAG_CUSTOM_IO;
534 
535  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
536  goto fail;
537 
538  if (!(s->url = av_strdup(filename ? filename : ""))) {
539  ret = AVERROR(ENOMEM);
540  goto fail;
541  }
542 
543 #if FF_API_FORMAT_FILENAME
545  av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
547 #endif
548  if ((ret = init_input(s, filename, &tmp)) < 0)
549  goto fail;
550  s->probe_score = ret;
551 
552  if (!s->protocol_whitelist && s->pb && s->pb->protocol_whitelist) {
553  s->protocol_whitelist = av_strdup(s->pb->protocol_whitelist);
554  if (!s->protocol_whitelist) {
555  ret = AVERROR(ENOMEM);
556  goto fail;
557  }
558  }
559 
560  if (!s->protocol_blacklist && s->pb && s->pb->protocol_blacklist) {
561  s->protocol_blacklist = av_strdup(s->pb->protocol_blacklist);
562  if (!s->protocol_blacklist) {
563  ret = AVERROR(ENOMEM);
564  goto fail;
565  }
566  }
567 
568  if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) {
569  av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
570  ret = AVERROR(EINVAL);
571  goto fail;
572  }
573 
574  avio_skip(s->pb, s->skip_initial_bytes);
575 
576  /* Check filename in case an image number is expected. */
577  if (s->iformat->flags & AVFMT_NEEDNUMBER) {
578  if (!av_filename_number_test(filename)) {
579  ret = AVERROR(EINVAL);
580  goto fail;
581  }
582  }
583 
584  s->duration = s->start_time = AV_NOPTS_VALUE;
585 
586  /* Allocate private data. */
587  if (s->iformat->priv_data_size > 0) {
588  if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
589  ret = AVERROR(ENOMEM);
590  goto fail;
591  }
592  if (s->iformat->priv_class) {
593  *(const AVClass **) s->priv_data = s->iformat->priv_class;
594  av_opt_set_defaults(s->priv_data);
595  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
596  goto fail;
597  }
598  }
599 
600  /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
601  if (s->pb)
602  ff_id3v2_read_dict(s->pb, &s->internal->id3v2_meta, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
603 
604 #if FF_API_DEMUXER_OPEN
605  if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->iformat->read_header)
606 #else
607  if (s->iformat->read_header)
608 #endif
609  if ((ret = s->iformat->read_header(s)) < 0)
610  goto fail;
611 
612  if (!s->metadata) {
613  s->metadata = s->internal->id3v2_meta;
614  s->internal->id3v2_meta = NULL;
615  } else if (s->internal->id3v2_meta) {
616  av_log(s, AV_LOG_WARNING, "Discarding ID3 tags because more suitable tags were found.\n");
617  av_dict_free(&s->internal->id3v2_meta);
618  }
619 
620  if (id3v2_extra_meta) {
621  if (!strcmp(s->iformat->name, "mp3") || !strcmp(s->iformat->name, "aac") ||
622  !strcmp(s->iformat->name, "tta") || !strcmp(s->iformat->name, "wav")) {
623  if ((ret = ff_id3v2_parse_apic(s, id3v2_extra_meta)) < 0)
624  goto close;
625  if ((ret = ff_id3v2_parse_chapters(s, id3v2_extra_meta)) < 0)
626  goto close;
627  if ((ret = ff_id3v2_parse_priv(s, id3v2_extra_meta)) < 0)
628  goto close;
629  } else
630  av_log(s, AV_LOG_DEBUG, "demuxer does not support additional id3 data, skipping\n");
631  }
632  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
633 
635  goto close;
636 
637 #if FF_API_DEMUXER_OPEN
638  if (!(s->flags&AVFMT_FLAG_PRIV_OPT) && s->pb && !s->internal->data_offset)
639 #else
640  if (s->pb && !s->internal->data_offset)
641 #endif
642  s->internal->data_offset = avio_tell(s->pb);
643 
644  s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
645 
647 
648  for (i = 0; i < s->nb_streams; i++)
649  s->streams[i]->internal->orig_codec_id = s->streams[i]->codecpar->codec_id;
650 
651  if (options) {
653  *options = tmp;
654  }
655  *ps = s;
656  return 0;
657 
658 close:
659  if (s->iformat->read_close)
660  s->iformat->read_close(s);
661 fail:
662  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
663  av_dict_free(&tmp);
664  if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
665  avio_closep(&s->pb);
667  *ps = NULL;
668  return ret;
669 }
670 
671 /*******************************************************/
672 
674 {
675  switch (st->codecpar->codec_type) {
676  case AVMEDIA_TYPE_VIDEO:
677  if (s->video_codec_id)
678  st->codecpar->codec_id = s->video_codec_id;
679  break;
680  case AVMEDIA_TYPE_AUDIO:
681  if (s->audio_codec_id)
682  st->codecpar->codec_id = s->audio_codec_id;
683  break;
685  if (s->subtitle_codec_id)
686  st->codecpar->codec_id = s->subtitle_codec_id;
687  break;
688  case AVMEDIA_TYPE_DATA:
689  if (s->data_codec_id)
690  st->codecpar->codec_id = s->data_codec_id;
691  break;
692  }
693 }
694 
696 {
697  if (st->internal->request_probe>0) {
698  AVProbeData *pd = &st->internal->probe_data;
699  int end;
700  av_log(s, AV_LOG_DEBUG, "probing stream %d pp:%d\n", st->index, st->probe_packets);
701  --st->probe_packets;
702 
703  if (pkt) {
705  if (!new_buf) {
707  "Failed to reallocate probe buffer for stream %d\n",
708  st->index);
709  goto no_packet;
710  }
711  pd->buf = new_buf;
712  memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
713  pd->buf_size += pkt->size;
714  memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
715  } else {
716 no_packet:
717  st->probe_packets = 0;
718  if (!pd->buf_size) {
720  "nothing to probe for stream %d\n", st->index);
721  }
722  }
723 
724  end= s->internal->raw_packet_buffer_remaining_size <= 0
725  || st->probe_packets<= 0;
726 
727  if (end || av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
728  int score = set_codec_from_probe_data(s, st, pd);
730  || end) {
731  pd->buf_size = 0;
732  av_freep(&pd->buf);
733  st->internal->request_probe = -1;
734  if (st->codecpar->codec_id != AV_CODEC_ID_NONE) {
735  av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
736  } else
737  av_log(s, AV_LOG_WARNING, "probed stream %d failed\n", st->index);
738  }
739  force_codec_ids(s, st);
740  }
741  }
742  return 0;
743 }
744 
745 static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
746 {
747  int64_t ref = pkt->dts;
748  int i, pts_wrap_behavior;
749  int64_t pts_wrap_reference;
750  AVProgram *first_program;
751 
752  if (ref == AV_NOPTS_VALUE)
753  ref = pkt->pts;
754  if (st->internal->pts_wrap_reference != AV_NOPTS_VALUE || st->pts_wrap_bits >= 63 || ref == AV_NOPTS_VALUE || !s->correct_ts_overflow)
755  return 0;
756  ref &= (1LL << st->pts_wrap_bits)-1;
757 
758  // reference time stamp should be 60 s before first time stamp
759  pts_wrap_reference = ref - av_rescale(60, st->time_base.den, st->time_base.num);
760  // if first time stamp is not more than 1/8 and 60s before the wrap point, subtract rather than add wrap offset
761  pts_wrap_behavior = (ref < (1LL << st->pts_wrap_bits) - (1LL << st->pts_wrap_bits-3)) ||
762  (ref < (1LL << st->pts_wrap_bits) - av_rescale(60, st->time_base.den, st->time_base.num)) ?
764 
765  first_program = av_find_program_from_stream(s, NULL, stream_index);
766 
767  if (!first_program) {
768  int default_stream_index = av_find_default_stream_index(s);
769  if (s->streams[default_stream_index]->internal->pts_wrap_reference == AV_NOPTS_VALUE) {
770  for (i = 0; i < s->nb_streams; i++) {
772  continue;
773  s->streams[i]->internal->pts_wrap_reference = pts_wrap_reference;
774  s->streams[i]->internal->pts_wrap_behavior = pts_wrap_behavior;
775  }
776  }
777  else {
778  st->internal->pts_wrap_reference = s->streams[default_stream_index]->internal->pts_wrap_reference;
779  st->internal->pts_wrap_behavior = s->streams[default_stream_index]->internal->pts_wrap_behavior;
780  }
781  }
782  else {
783  AVProgram *program = first_program;
784  while (program) {
785  if (program->pts_wrap_reference != AV_NOPTS_VALUE) {
786  pts_wrap_reference = program->pts_wrap_reference;
787  pts_wrap_behavior = program->pts_wrap_behavior;
788  break;
789  }
790  program = av_find_program_from_stream(s, program, stream_index);
791  }
792 
793  // update every program with differing pts_wrap_reference
794  program = first_program;
795  while (program) {
796  if (program->pts_wrap_reference != pts_wrap_reference) {
797  for (i = 0; i<program->nb_stream_indexes; i++) {
798  s->streams[program->stream_index[i]]->internal->pts_wrap_reference = pts_wrap_reference;
799  s->streams[program->stream_index[i]]->internal->pts_wrap_behavior = pts_wrap_behavior;
800  }
801 
802  program->pts_wrap_reference = pts_wrap_reference;
803  program->pts_wrap_behavior = pts_wrap_behavior;
804  }
805  program = av_find_program_from_stream(s, program, stream_index);
806  }
807  }
808  return 1;
809 }
810 
812 {
813  int ret, i, err;
814  AVStream *st;
815 
816 #if FF_API_INIT_PACKET
818  pkt->data = NULL;
819  pkt->size = 0;
822 #else
824 #endif
825 
826  for (;;) {
827  PacketList *pktl = s->internal->raw_packet_buffer;
828  const AVPacket *pkt1;
829 
830  if (pktl) {
831  st = s->streams[pktl->pkt.stream_index];
832  if (s->internal->raw_packet_buffer_remaining_size <= 0)
833  if ((err = probe_codec(s, st, NULL)) < 0)
834  return err;
835  if (st->internal->request_probe <= 0) {
836  avpriv_packet_list_get(&s->internal->raw_packet_buffer,
837  &s->internal->raw_packet_buffer_end, pkt);
838  s->internal->raw_packet_buffer_remaining_size += pkt->size;
839  return 0;
840  }
841  }
842 
843  ret = s->iformat->read_packet(s, pkt);
844  if (ret < 0) {
846 
847  /* Some demuxers return FFERROR_REDO when they consume
848  data and discard it (ignored streams, junk, extradata).
849  We must re-call the demuxer to get the real packet. */
850  if (ret == FFERROR_REDO)
851  continue;
852  if (!pktl || ret == AVERROR(EAGAIN))
853  return ret;
854  for (i = 0; i < s->nb_streams; i++) {
855  st = s->streams[i];
856  if (st->probe_packets || st->internal->request_probe > 0)
857  if ((err = probe_codec(s, st, NULL)) < 0)
858  return err;
859  av_assert0(st->internal->request_probe <= 0);
860  }
861  continue;
862  }
863 
865  if (err < 0) {
867  return err;
868  }
869 
870  if (pkt->flags & AV_PKT_FLAG_CORRUPT) {
872  "Packet corrupt (stream = %d, dts = %s)",
874  if (s->flags & AVFMT_FLAG_DISCARD_CORRUPT) {
875  av_log(s, AV_LOG_WARNING, ", dropping it.\n");
877  continue;
878  }
879  av_log(s, AV_LOG_WARNING, ".\n");
880  }
881 
882  av_assert0(pkt->stream_index < (unsigned)s->nb_streams &&
883  "Invalid stream index.\n");
884 
885  st = s->streams[pkt->stream_index];
886 
888  // correct first time stamps to negative values
889  if (!is_relative(st->first_dts))
890  st->first_dts = wrap_timestamp(st, st->first_dts);
891  if (!is_relative(st->start_time))
892  st->start_time = wrap_timestamp(st, st->start_time);
893  if (!is_relative(st->cur_dts))
894  st->cur_dts = wrap_timestamp(st, st->cur_dts);
895  }
896 
897  pkt->dts = wrap_timestamp(st, pkt->dts);
898  pkt->pts = wrap_timestamp(st, pkt->pts);
899 
900  force_codec_ids(s, st);
901 
902  /* TODO: audio: time filter; video: frame reordering (pts != dts) */
903  if (s->use_wallclock_as_timestamps)
905 
906  if (!pktl && st->internal->request_probe <= 0)
907  return ret;
908 
909  err = avpriv_packet_list_put(&s->internal->raw_packet_buffer,
910  &s->internal->raw_packet_buffer_end,
911  pkt, NULL, 0);
912  if (err < 0) {
914  return err;
915  }
916  pkt1 = &s->internal->raw_packet_buffer_end->pkt;
917  s->internal->raw_packet_buffer_remaining_size -= pkt1->size;
918 
919  if ((err = probe_codec(s, st, pkt1)) < 0)
920  return err;
921  }
922 }
923 
924 
925 /**********************************************************/
926 
928 {
929  switch(avctx->codec_id) {
930  case AV_CODEC_ID_MP1:
931  case AV_CODEC_ID_MP2:
932  case AV_CODEC_ID_MP3:
933  case AV_CODEC_ID_CODEC2:
934  return 1;
935  }
936 
937  return 0;
938 }
939 
940 /**
941  * Return the frame duration in seconds. Return 0 if not available.
942  */
943 void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st,
945 {
946  AVRational codec_framerate = s->iformat ? st->internal->avctx->framerate :
947  av_mul_q(av_inv_q(st->internal->avctx->time_base), (AVRational){1, st->internal->avctx->ticks_per_frame});
948  int frame_size, sample_rate;
949 
950 #if FF_API_LAVF_AVCTX
952  if ((!codec_framerate.den || !codec_framerate.num) && st->codec->time_base.den && st->codec->time_base.num)
953  codec_framerate = av_mul_q(av_inv_q(st->codec->time_base), (AVRational){1, st->codec->ticks_per_frame});
955 #endif
956 
957  *pnum = 0;
958  *pden = 0;
959  switch (st->codecpar->codec_type) {
960  case AVMEDIA_TYPE_VIDEO:
961  if (st->r_frame_rate.num && !pc && s->iformat) {
962  *pnum = st->r_frame_rate.den;
963  *pden = st->r_frame_rate.num;
964  } else if (st->time_base.num * 1000LL > st->time_base.den) {
965  *pnum = st->time_base.num;
966  *pden = st->time_base.den;
967  } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
969  av_reduce(pnum, pden,
970  codec_framerate.den,
971  codec_framerate.num * (int64_t)st->internal->avctx->ticks_per_frame,
972  INT_MAX);
973 
974  if (pc && pc->repeat_pict) {
975  av_assert0(s->iformat); // this may be wrong for interlaced encoding but its not used for that case
976  av_reduce(pnum, pden,
977  (*pnum) * (1LL + pc->repeat_pict),
978  (*pden),
979  INT_MAX);
980  }
981  /* If this codec can be interlaced or progressive then we need
982  * a parser to compute duration of a packet. Thus if we have
983  * no parser in such case leave duration undefined. */
984  if (st->internal->avctx->ticks_per_frame > 1 && !pc)
985  *pnum = *pden = 0;
986  }
987  break;
988  case AVMEDIA_TYPE_AUDIO:
989  if (st->internal->avctx_inited) {
992  } else {
995  }
996  if (frame_size <= 0 || sample_rate <= 0)
997  break;
998  *pnum = frame_size;
999  *pden = sample_rate;
1000  break;
1001  default:
1002  break;
1003  }
1004 }
1005 
1007 {
1009  if (!d)
1010  return 0;
1011  if ((d->type == AVMEDIA_TYPE_VIDEO || d->type == AVMEDIA_TYPE_AUDIO) &&
1013  return 0;
1014  return 1;
1015 }
1016 
1018 {
1019  if (st->codecpar->codec_id != AV_CODEC_ID_H264) return 1;
1020  if (!st->internal->info) // if we have left find_stream_info then nb_decoded_frames won't increase anymore for stream copy
1021  return 1;
1022 #if CONFIG_H264_DECODER
1023  if (st->internal->avctx->has_b_frames &&
1025  return 1;
1026 #endif
1027  if (st->internal->avctx->has_b_frames<3)
1028  return st->internal->nb_decoded_frames >= 7;
1029  else if (st->internal->avctx->has_b_frames<4)
1030  return st->internal->nb_decoded_frames >= 18;
1031  else
1032  return st->internal->nb_decoded_frames >= 20;
1033 }
1034 
1036 {
1037  if (pktl->next)
1038  return pktl->next;
1039  if (pktl == s->internal->packet_buffer_end)
1040  return s->internal->parse_queue;
1041  return NULL;
1042 }
1043 
1044 static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts) {
1045  int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1047 
1048  if(!onein_oneout) {
1049  int delay = st->internal->avctx->has_b_frames;
1050  int i;
1051 
1052  if (dts == AV_NOPTS_VALUE) {
1053  int64_t best_score = INT64_MAX;
1054  for (i = 0; i<delay; i++) {
1055  if (st->internal->pts_reorder_error_count[i]) {
1056  int64_t score = st->internal->pts_reorder_error[i] / st->internal->pts_reorder_error_count[i];
1057  if (score < best_score) {
1058  best_score = score;
1059  dts = pts_buffer[i];
1060  }
1061  }
1062  }
1063  } else {
1064  for (i = 0; i<delay; i++) {
1065  if (pts_buffer[i] != AV_NOPTS_VALUE) {
1066  int64_t diff = FFABS(pts_buffer[i] - dts)
1067  + (uint64_t)st->internal->pts_reorder_error[i];
1069  st->internal->pts_reorder_error[i] = diff;
1071  if (st->internal->pts_reorder_error_count[i] > 250) {
1072  st->internal->pts_reorder_error[i] >>= 1;
1073  st->internal->pts_reorder_error_count[i] >>= 1;
1074  }
1075  }
1076  }
1077  }
1078  }
1079 
1080  if (dts == AV_NOPTS_VALUE)
1081  dts = pts_buffer[0];
1082 
1083  return dts;
1084 }
1085 
1086 /**
1087  * Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts
1088  * of the packets in a window.
1089  */
1090 static void update_dts_from_pts(AVFormatContext *s, int stream_index,
1091  PacketList *pkt_buffer)
1092 {
1093  AVStream *st = s->streams[stream_index];
1094  int delay = st->internal->avctx->has_b_frames;
1095  int i;
1096 
1097  int64_t pts_buffer[MAX_REORDER_DELAY+1];
1098 
1099  for (i = 0; i<MAX_REORDER_DELAY+1; i++)
1100  pts_buffer[i] = AV_NOPTS_VALUE;
1101 
1102  for (; pkt_buffer; pkt_buffer = get_next_pkt(s, st, pkt_buffer)) {
1103  if (pkt_buffer->pkt.stream_index != stream_index)
1104  continue;
1105 
1106  if (pkt_buffer->pkt.pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1107  pts_buffer[0] = pkt_buffer->pkt.pts;
1108  for (i = 0; i<delay && pts_buffer[i] > pts_buffer[i + 1]; i++)
1109  FFSWAP(int64_t, pts_buffer[i], pts_buffer[i + 1]);
1110 
1111  pkt_buffer->pkt.dts = select_from_pts_buffer(st, pts_buffer, pkt_buffer->pkt.dts);
1112  }
1113  }
1114 }
1115 
1116 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
1117  int64_t dts, int64_t pts, AVPacket *pkt)
1118 {
1119  AVStream *st = s->streams[stream_index];
1120  PacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
1121  PacketList *pktl_it;
1122 
1123  uint64_t shift;
1124 
1125  if (st->first_dts != AV_NOPTS_VALUE ||
1126  dts == AV_NOPTS_VALUE ||
1127  st->cur_dts == AV_NOPTS_VALUE ||
1128  st->cur_dts < INT_MIN + RELATIVE_TS_BASE ||
1129  dts < INT_MIN + (st->cur_dts - RELATIVE_TS_BASE) ||
1130  is_relative(dts))
1131  return;
1132 
1133  st->first_dts = dts - (st->cur_dts - RELATIVE_TS_BASE);
1134  st->cur_dts = dts;
1135  shift = (uint64_t)st->first_dts - RELATIVE_TS_BASE;
1136 
1137  if (is_relative(pts))
1138  pts += shift;
1139 
1140  for (pktl_it = pktl; pktl_it; pktl_it = get_next_pkt(s, st, pktl_it)) {
1141  if (pktl_it->pkt.stream_index != stream_index)
1142  continue;
1143  if (is_relative(pktl_it->pkt.pts))
1144  pktl_it->pkt.pts += shift;
1145 
1146  if (is_relative(pktl_it->pkt.dts))
1147  pktl_it->pkt.dts += shift;
1148 
1149  if (st->start_time == AV_NOPTS_VALUE && pktl_it->pkt.pts != AV_NOPTS_VALUE) {
1150  st->start_time = pktl_it->pkt.pts;
1152  st->start_time = av_sat_add64(st->start_time, av_rescale_q(st->internal->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
1153  }
1154  }
1155 
1157  update_dts_from_pts(s, stream_index, pktl);
1158  }
1159 
1160  if (st->start_time == AV_NOPTS_VALUE) {
1162  st->start_time = pts;
1163  }
1165  st->start_time = av_sat_add64(st->start_time, av_rescale_q(st->internal->skip_samples, (AVRational){1, st->codecpar->sample_rate}, st->time_base));
1166  }
1167 }
1168 
1170  int stream_index, int64_t duration)
1171 {
1172  PacketList *pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
1173  int64_t cur_dts = RELATIVE_TS_BASE;
1174 
1175  if (st->first_dts != AV_NOPTS_VALUE) {
1177  return;
1179  cur_dts = st->first_dts;
1180  for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
1181  if (pktl->pkt.stream_index == stream_index) {
1182  if (pktl->pkt.pts != pktl->pkt.dts ||
1183  pktl->pkt.dts != AV_NOPTS_VALUE ||
1184  pktl->pkt.duration)
1185  break;
1186  cur_dts -= duration;
1187  }
1188  }
1189  if (pktl && pktl->pkt.dts != st->first_dts) {
1190  av_log(s, AV_LOG_DEBUG, "first_dts %s not matching first dts %s (pts %s, duration %"PRId64") in the queue\n",
1191  av_ts2str(st->first_dts), av_ts2str(pktl->pkt.dts), av_ts2str(pktl->pkt.pts), pktl->pkt.duration);
1192  return;
1193  }
1194  if (!pktl) {
1195  av_log(s, AV_LOG_DEBUG, "first_dts %s but no packet with dts in the queue\n", av_ts2str(st->first_dts));
1196  return;
1197  }
1198  pktl = s->internal->packet_buffer ? s->internal->packet_buffer : s->internal->parse_queue;
1199  st->first_dts = cur_dts;
1200  } else if (st->cur_dts != RELATIVE_TS_BASE)
1201  return;
1202 
1203  for (; pktl; pktl = get_next_pkt(s, st, pktl)) {
1204  if (pktl->pkt.stream_index != stream_index)
1205  continue;
1206  if ((pktl->pkt.pts == pktl->pkt.dts ||
1207  pktl->pkt.pts == AV_NOPTS_VALUE) &&
1208  (pktl->pkt.dts == AV_NOPTS_VALUE ||
1209  pktl->pkt.dts == st->first_dts ||
1210  pktl->pkt.dts == RELATIVE_TS_BASE) &&
1211  !pktl->pkt.duration &&
1212  av_sat_add64(cur_dts, duration) == cur_dts + (uint64_t)duration
1213  ) {
1214  pktl->pkt.dts = cur_dts;
1215  if (!st->internal->avctx->has_b_frames)
1216  pktl->pkt.pts = cur_dts;
1217  pktl->pkt.duration = duration;
1218  } else
1219  break;
1220  cur_dts = pktl->pkt.dts + pktl->pkt.duration;
1221  }
1222  if (!pktl)
1223  st->cur_dts = cur_dts;
1224 }
1225 
1228  int64_t next_dts, int64_t next_pts)
1229 {
1230  int num, den, presentation_delayed, delay, i;
1231  int64_t offset;
1233  int onein_oneout = st->codecpar->codec_id != AV_CODEC_ID_H264 &&
1235 
1236  if (s->flags & AVFMT_FLAG_NOFILLIN)
1237  return;
1238 
1241  if (st->internal->last_dts_for_order_check <= pkt->dts) {
1242  st->internal->dts_ordered++;
1243  } else {
1245  "DTS %"PRIi64" < %"PRIi64" out of order\n",
1246  pkt->dts,
1248  st->internal->dts_misordered++;
1249  }
1250  if (st->internal->dts_ordered + st->internal->dts_misordered > 250) {
1251  st->internal->dts_ordered >>= 1;
1252  st->internal->dts_misordered >>= 1;
1253  }
1254  }
1255 
1257  if (st->internal->dts_ordered < 8*st->internal->dts_misordered && pkt->dts == pkt->pts)
1258  pkt->dts = AV_NOPTS_VALUE;
1259  }
1260 
1261  if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
1262  pkt->dts = AV_NOPTS_VALUE;
1263 
1264  if (pc && pc->pict_type == AV_PICTURE_TYPE_B
1265  && !st->internal->avctx->has_b_frames)
1266  //FIXME Set low_delay = 0 when has_b_frames = 1
1267  st->internal->avctx->has_b_frames = 1;
1268 
1269  /* do we have a video B-frame ? */
1270  delay = st->internal->avctx->has_b_frames;
1271  presentation_delayed = 0;
1272 
1273  /* XXX: need has_b_frame, but cannot get it if the codec is
1274  * not initialized */
1275  if (delay &&
1276  pc && pc->pict_type != AV_PICTURE_TYPE_B)
1277  presentation_delayed = 1;
1278 
1279  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
1280  st->pts_wrap_bits < 63 && pkt->dts > INT64_MIN + (1LL << st->pts_wrap_bits) &&
1281  pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
1282  if (is_relative(st->cur_dts) || pkt->dts - (1LL<<(st->pts_wrap_bits - 1)) > st->cur_dts) {
1283  pkt->dts -= 1LL << st->pts_wrap_bits;
1284  } else
1285  pkt->pts += 1LL << st->pts_wrap_bits;
1286  }
1287 
1288  /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
1289  * We take the conservative approach and discard both.
1290  * Note: If this is misbehaving for an H.264 file, then possibly
1291  * presentation_delayed is not set correctly. */
1292  if (delay == 1 && pkt->dts == pkt->pts &&
1293  pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
1294  av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination %"PRIi64"\n", pkt->dts);
1295  if ( strcmp(s->iformat->name, "mov,mp4,m4a,3gp,3g2,mj2")
1296  && strcmp(s->iformat->name, "flv")) // otherwise we discard correct timestamps for vc1-wmapro.ism
1297  pkt->dts = AV_NOPTS_VALUE;
1298  }
1299 
1301  if (pkt->duration <= 0) {
1302  ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
1303  if (den && num) {
1304  duration = (AVRational) {num, den};
1306  num * (int64_t) st->time_base.den,
1307  den * (int64_t) st->time_base.num,
1308  AV_ROUND_DOWN);
1309  }
1310  }
1311 
1312  if (pkt->duration > 0 && (s->internal->packet_buffer || s->internal->parse_queue))
1314 
1315  /* Correct timestamps with byte offset if demuxers only have timestamps
1316  * on packet boundaries */
1317  if (pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
1318  /* this will estimate bitrate based on this frame's duration and size */
1320  if (pkt->pts != AV_NOPTS_VALUE)
1321  pkt->pts += offset;
1322  if (pkt->dts != AV_NOPTS_VALUE)
1323  pkt->dts += offset;
1324  }
1325 
1326  /* This may be redundant, but it should not hurt. */
1327  if (pkt->dts != AV_NOPTS_VALUE &&
1328  pkt->pts != AV_NOPTS_VALUE &&
1329  pkt->pts > pkt->dts)
1330  presentation_delayed = 1;
1331 
1332  if (s->debug & FF_FDEBUG_TS)
1334  "IN delayed:%d pts:%s, dts:%s cur_dts:%s st:%d pc:%p duration:%"PRId64" delay:%d onein_oneout:%d\n",
1335  presentation_delayed, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts),
1336  pkt->stream_index, pc, pkt->duration, delay, onein_oneout);
1337 
1338  /* Interpolate PTS and DTS if they are not present. We skip H264
1339  * currently because delay and has_b_frames are not reliably set. */
1340  if ((delay == 0 || (delay == 1 && pc)) &&
1341  onein_oneout) {
1342  if (presentation_delayed) {
1343  /* DTS = decompression timestamp */
1344  /* PTS = presentation timestamp */
1345  if (pkt->dts == AV_NOPTS_VALUE)
1346  pkt->dts = st->last_IP_pts;
1348  if (pkt->dts == AV_NOPTS_VALUE)
1349  pkt->dts = st->cur_dts;
1350 
1351  /* This is tricky: the dts must be incremented by the duration
1352  * of the frame we are displaying, i.e. the last I- or P-frame. */
1353  if (st->last_IP_duration == 0 && (uint64_t)pkt->duration <= INT32_MAX)
1354  st->last_IP_duration = pkt->duration;
1355  if (pkt->dts != AV_NOPTS_VALUE)
1357  if (pkt->dts != AV_NOPTS_VALUE &&
1358  pkt->pts == AV_NOPTS_VALUE &&
1359  st->last_IP_duration > 0 &&
1360  ((uint64_t)st->cur_dts - (uint64_t)next_dts + 1) <= 2 &&
1361  next_dts != next_pts &&
1362  next_pts != AV_NOPTS_VALUE)
1363  pkt->pts = next_dts;
1364 
1365  if ((uint64_t)pkt->duration <= INT32_MAX)
1366  st->last_IP_duration = pkt->duration;
1367  st->last_IP_pts = pkt->pts;
1368  /* Cannot compute PTS if not present (we can compute it only
1369  * by knowing the future. */
1370  } else if (pkt->pts != AV_NOPTS_VALUE ||
1371  pkt->dts != AV_NOPTS_VALUE ||
1372  pkt->duration > 0 ) {
1373 
1374  /* presentation is not delayed : PTS and DTS are the same */
1375  if (pkt->pts == AV_NOPTS_VALUE)
1376  pkt->pts = pkt->dts;
1378  pkt->pts, pkt);
1379  if (pkt->pts == AV_NOPTS_VALUE)
1380  pkt->pts = st->cur_dts;
1381  pkt->dts = pkt->pts;
1382  if (pkt->pts != AV_NOPTS_VALUE && duration.num >= 0)
1383  st->cur_dts = av_add_stable(st->time_base, pkt->pts, duration, 1);
1384  }
1385  }
1386 
1387  if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
1388  st->internal->pts_buffer[0] = pkt->pts;
1389  for (i = 0; i<delay && st->internal->pts_buffer[i] > st->internal->pts_buffer[i + 1]; i++)
1390  FFSWAP(int64_t, st->internal->pts_buffer[i], st->internal->pts_buffer[i + 1]);
1391 
1394  }
1395  // We skipped it above so we try here.
1396  if (!onein_oneout)
1397  // This should happen on the first packet
1399  if (pkt->dts > st->cur_dts)
1400  st->cur_dts = pkt->dts;
1401 
1402  if (s->debug & FF_FDEBUG_TS)
1403  av_log(s, AV_LOG_DEBUG, "OUTdelayed:%d/%d pts:%s, dts:%s cur_dts:%s st:%d (%d)\n",
1404  presentation_delayed, delay, av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->cur_dts), st->index, st->id);
1405 
1406  /* update flags */
1409 #if FF_API_CONVERGENCE_DURATION
1411  if (pc)
1412  pkt->convergence_duration = pc->convergence_duration;
1414 #endif
1415 }
1416 
1417 /**
1418  * Parse a packet, add all split parts to parse_queue.
1419  *
1420  * @param pkt Packet to parse; must not be NULL.
1421  * @param flush Indicates whether to flush. If set, pkt must be blank.
1422  */
1424  int stream_index, int flush)
1425 {
1426  AVPacket *out_pkt = s->internal->parse_pkt;
1427  AVStream *st = s->streams[stream_index];
1428  uint8_t *data = pkt->data;
1429  int size = pkt->size;
1430  int ret = 0, got_output = flush;
1431 
1432  if (size || flush) {
1433  av_packet_unref(out_pkt);
1434  } else if (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) {
1435  // preserve 0-size sync packets
1437  }
1438 
1439  while (size > 0 || (flush && got_output)) {
1440  int len;
1441  int64_t next_pts = pkt->pts;
1442  int64_t next_dts = pkt->dts;
1443 
1445  &out_pkt->data, &out_pkt->size, data, size,
1446  pkt->pts, pkt->dts, pkt->pos);
1447 
1448  pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1449  pkt->pos = -1;
1450  /* increment read pointer */
1451  av_assert1(data || !len);
1452  data = len ? data + len : data;
1453  size -= len;
1454 
1455  got_output = !!out_pkt->size;
1456 
1457  if (!out_pkt->size)
1458  continue;
1459 
1460  if (pkt->buf && out_pkt->data == pkt->data) {
1461  /* reference pkt->buf only when out_pkt->data is guaranteed to point
1462  * to data in it and not in the parser's internal buffer. */
1463  /* XXX: Ensure this is the case with all parsers when st->parser->flags
1464  * is PARSER_FLAG_COMPLETE_FRAMES and check for that instead? */
1465  out_pkt->buf = av_buffer_ref(pkt->buf);
1466  if (!out_pkt->buf) {
1467  ret = AVERROR(ENOMEM);
1468  goto fail;
1469  }
1470  } else {
1471  ret = av_packet_make_refcounted(out_pkt);
1472  if (ret < 0)
1473  goto fail;
1474  }
1475 
1476  if (pkt->side_data) {
1477  out_pkt->side_data = pkt->side_data;
1478  out_pkt->side_data_elems = pkt->side_data_elems;
1479  pkt->side_data = NULL;
1480  pkt->side_data_elems = 0;
1481  }
1482 
1483  /* set the duration */
1484  out_pkt->duration = (st->parser->flags & PARSER_FLAG_COMPLETE_FRAMES) ? pkt->duration : 0;
1486  if (st->internal->avctx->sample_rate > 0) {
1487  out_pkt->duration =
1489  (AVRational) { 1, st->internal->avctx->sample_rate },
1490  st->time_base,
1491  AV_ROUND_DOWN);
1492  }
1493  }
1494 
1495  out_pkt->stream_index = st->index;
1496  out_pkt->pts = st->parser->pts;
1497  out_pkt->dts = st->parser->dts;
1498  out_pkt->pos = st->parser->pos;
1499  out_pkt->flags |= pkt->flags & AV_PKT_FLAG_DISCARD;
1500 
1502  out_pkt->pos = st->parser->frame_offset;
1503 
1504  if (st->parser->key_frame == 1 ||
1505  (st->parser->key_frame == -1 &&
1507  out_pkt->flags |= AV_PKT_FLAG_KEY;
1508 
1510  out_pkt->flags |= AV_PKT_FLAG_KEY;
1511 
1512  compute_pkt_fields(s, st, st->parser, out_pkt, next_dts, next_pts);
1513 
1514  ret = avpriv_packet_list_put(&s->internal->parse_queue,
1515  &s->internal->parse_queue_end,
1516  out_pkt, NULL, 0);
1517  if (ret < 0) {
1518  av_packet_unref(out_pkt);
1519  goto fail;
1520  }
1521  }
1522 
1523  /* end of the stream => close and free the parser */
1524  if (flush) {
1525  av_parser_close(st->parser);
1526  st->parser = NULL;
1527  }
1528 
1529 fail:
1531  return ret;
1532 }
1533 
1534 static int64_t ts_to_samples(AVStream *st, int64_t ts)
1535 {
1536  return av_rescale(ts, st->time_base.num * st->codecpar->sample_rate, st->time_base.den);
1537 }
1538 
1540 {
1541  int ret, i, got_packet = 0;
1542  AVDictionary *metadata = NULL;
1543 
1544  while (!got_packet && !s->internal->parse_queue) {
1545  AVStream *st;
1546 
1547  /* read next packet */
1548  ret = ff_read_packet(s, pkt);
1549  if (ret < 0) {
1550  if (ret == AVERROR(EAGAIN))
1551  return ret;
1552  /* flush the parsers */
1553  for (i = 0; i < s->nb_streams; i++) {
1554  st = s->streams[i];
1555  if (st->parser && st->need_parsing)
1556  parse_packet(s, pkt, st->index, 1);
1557  }
1558  /* all remaining packets are now in parse_queue =>
1559  * really terminate parsing */
1560  break;
1561  }
1562  ret = 0;
1563  st = s->streams[pkt->stream_index];
1564 
1566 
1567  /* update context if required */
1568  if (st->internal->need_context_update) {
1569  if (avcodec_is_open(st->internal->avctx)) {
1570  av_log(s, AV_LOG_DEBUG, "Demuxer context update while decoder is open, closing and trying to re-open\n");
1572  st->internal->info->found_decoder = 0;
1573  }
1574 
1575  /* close parser, because it depends on the codec */
1576  if (st->parser && st->internal->avctx->codec_id != st->codecpar->codec_id) {
1577  av_parser_close(st->parser);
1578  st->parser = NULL;
1579  }
1580 
1582  if (ret < 0) {
1584  return ret;
1585  }
1586 
1587 #if FF_API_LAVF_AVCTX
1589  /* update deprecated public codec context */
1590  ret = avcodec_parameters_to_context(st->codec, st->codecpar);
1591  if (ret < 0) {
1593  return ret;
1594  }
1596 #endif
1597 
1598  st->internal->need_context_update = 0;
1599  }
1600 
1601  if (pkt->pts != AV_NOPTS_VALUE &&
1602  pkt->dts != AV_NOPTS_VALUE &&
1603  pkt->pts < pkt->dts) {
1605  "Invalid timestamps stream=%d, pts=%s, dts=%s, size=%d\n",
1606  pkt->stream_index,
1607  av_ts2str(pkt->pts),
1608  av_ts2str(pkt->dts),
1609  pkt->size);
1610  }
1611  if (s->debug & FF_FDEBUG_TS)
1613  "ff_read_packet stream=%d, pts=%s, dts=%s, size=%d, duration=%"PRId64", flags=%d\n",
1614  pkt->stream_index,
1615  av_ts2str(pkt->pts),
1616  av_ts2str(pkt->dts),
1617  pkt->size, pkt->duration, pkt->flags);
1618 
1619  if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1620  st->parser = av_parser_init(st->codecpar->codec_id);
1621  if (!st->parser) {
1622  av_log(s, AV_LOG_VERBOSE, "parser not found for codec "
1623  "%s, packets or times may be invalid.\n",
1625  /* no parser available: just output the raw packets */
1627  } else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
1629  else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
1630  st->parser->flags |= PARSER_FLAG_ONCE;
1631  else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW)
1633  }
1634 
1635  if (!st->need_parsing || !st->parser) {
1636  /* no parsing needed: we just output the packet as is */
1638  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1640  ff_reduce_index(s, st->index);
1641  av_add_index_entry(st, pkt->pos, pkt->dts,
1642  0, 0, AVINDEX_KEYFRAME);
1643  }
1644  got_packet = 1;
1645  } else if (st->discard < AVDISCARD_ALL) {
1646  if ((ret = parse_packet(s, pkt, pkt->stream_index, 0)) < 0)
1647  return ret;
1649  st->codecpar->bit_rate = st->internal->avctx->bit_rate;
1650  st->codecpar->channels = st->internal->avctx->channels;
1652  st->codecpar->codec_id = st->internal->avctx->codec_id;
1653  } else {
1654  /* free packet */
1656  }
1657  if (pkt->flags & AV_PKT_FLAG_KEY)
1658  st->internal->skip_to_keyframe = 0;
1659  if (st->internal->skip_to_keyframe) {
1661  got_packet = 0;
1662  }
1663  }
1664 
1665  if (!got_packet && s->internal->parse_queue)
1666  ret = avpriv_packet_list_get(&s->internal->parse_queue, &s->internal->parse_queue_end, pkt);
1667 
1668  if (ret >= 0) {
1669  AVStream *st = s->streams[pkt->stream_index];
1670  int discard_padding = 0;
1672  int64_t pts = pkt->pts - (is_relative(pkt->pts) ? RELATIVE_TS_BASE : 0);
1673  int64_t sample = ts_to_samples(st, pts);
1674  int duration = ts_to_samples(st, pkt->duration);
1675  int64_t end_sample = sample + duration;
1676  if (duration > 0 && end_sample >= st->internal->first_discard_sample &&
1677  sample < st->internal->last_discard_sample)
1678  discard_padding = FFMIN(end_sample - st->internal->first_discard_sample, duration);
1679  }
1680  if (st->internal->start_skip_samples && (pkt->pts == 0 || pkt->pts == RELATIVE_TS_BASE))
1682  if (st->internal->skip_samples || discard_padding) {
1684  if (p) {
1685  AV_WL32(p, st->internal->skip_samples);
1686  AV_WL32(p + 4, discard_padding);
1687  av_log(s, AV_LOG_DEBUG, "demuxer injecting skip %d / discard %d\n", st->internal->skip_samples, discard_padding);
1688  }
1689  st->internal->skip_samples = 0;
1690  }
1691 
1692  if (st->internal->inject_global_side_data) {
1693  for (i = 0; i < st->nb_side_data; i++) {
1694  AVPacketSideData *src_sd = &st->side_data[i];
1695  uint8_t *dst_data;
1696 
1697  if (av_packet_get_side_data(pkt, src_sd->type, NULL))
1698  continue;
1699 
1700  dst_data = av_packet_new_side_data(pkt, src_sd->type, src_sd->size);
1701  if (!dst_data) {
1702  av_log(s, AV_LOG_WARNING, "Could not inject global side data\n");
1703  continue;
1704  }
1705 
1706  memcpy(dst_data, src_sd->data, src_sd->size);
1707  }
1709  }
1710  }
1711 
1712  av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1713  if (metadata) {
1714  s->event_flags |= AVFMT_EVENT_FLAG_METADATA_UPDATED;
1715  av_dict_copy(&s->metadata, metadata, 0);
1716  av_dict_free(&metadata);
1718  }
1719 
1720 #if FF_API_LAVF_AVCTX
1722 #endif
1723 
1724  if (s->debug & FF_FDEBUG_TS)
1726  "read_frame_internal stream=%d, pts=%s, dts=%s, "
1727  "size=%d, duration=%"PRId64", flags=%d\n",
1728  pkt->stream_index,
1729  av_ts2str(pkt->pts),
1730  av_ts2str(pkt->dts),
1731  pkt->size, pkt->duration, pkt->flags);
1732 
1733  /* A demuxer might have returned EOF because of an IO error, let's
1734  * propagate this back to the user. */
1735  if (ret == AVERROR_EOF && s->pb && s->pb->error < 0 && s->pb->error != AVERROR(EAGAIN))
1736  ret = s->pb->error;
1737 
1738  return ret;
1739 }
1740 
1742 {
1743  const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1744  int eof = 0;
1745  int ret;
1746  AVStream *st;
1747 
1748  if (!genpts) {
1749  ret = s->internal->packet_buffer
1750  ? avpriv_packet_list_get(&s->internal->packet_buffer,
1751  &s->internal->packet_buffer_end, pkt)
1753  if (ret < 0)
1754  return ret;
1755  goto return_packet;
1756  }
1757 
1758  for (;;) {
1759  PacketList *pktl = s->internal->packet_buffer;
1760 
1761  if (pktl) {
1762  AVPacket *next_pkt = &pktl->pkt;
1763 
1764  if (next_pkt->dts != AV_NOPTS_VALUE) {
1765  int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1766  // last dts seen for this stream. if any of packets following
1767  // current one had no dts, we will set this to AV_NOPTS_VALUE.
1768  int64_t last_dts = next_pkt->dts;
1769  av_assert2(wrap_bits <= 64);
1770  while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1771  if (pktl->pkt.stream_index == next_pkt->stream_index &&
1772  av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2ULL << (wrap_bits - 1)) < 0) {
1773  if (av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2ULL << (wrap_bits - 1))) {
1774  // not B-frame
1775  next_pkt->pts = pktl->pkt.dts;
1776  }
1777  if (last_dts != AV_NOPTS_VALUE) {
1778  // Once last dts was set to AV_NOPTS_VALUE, we don't change it.
1779  last_dts = pktl->pkt.dts;
1780  }
1781  }
1782  pktl = pktl->next;
1783  }
1784  if (eof && next_pkt->pts == AV_NOPTS_VALUE && last_dts != AV_NOPTS_VALUE) {
1785  // Fixing the last reference frame had none pts issue (For MXF etc).
1786  // We only do this when
1787  // 1. eof.
1788  // 2. we are not able to resolve a pts value for current packet.
1789  // 3. the packets for this stream at the end of the files had valid dts.
1790  next_pkt->pts = last_dts + next_pkt->duration;
1791  }
1792  pktl = s->internal->packet_buffer;
1793  }
1794 
1795  /* read packet from packet buffer, if there is data */
1796  st = s->streams[next_pkt->stream_index];
1797  if (!(next_pkt->pts == AV_NOPTS_VALUE && st->discard < AVDISCARD_ALL &&
1798  next_pkt->dts != AV_NOPTS_VALUE && !eof)) {
1799  ret = avpriv_packet_list_get(&s->internal->packet_buffer,
1800  &s->internal->packet_buffer_end, pkt);
1801  goto return_packet;
1802  }
1803  }
1804 
1806  if (ret < 0) {
1807  if (pktl && ret != AVERROR(EAGAIN)) {
1808  eof = 1;
1809  continue;
1810  } else
1811  return ret;
1812  }
1813 
1814  ret = avpriv_packet_list_put(&s->internal->packet_buffer,
1815  &s->internal->packet_buffer_end,
1816  pkt, NULL, 0);
1817  if (ret < 0) {
1819  return ret;
1820  }
1821  }
1822 
1823 return_packet:
1824 
1825  st = s->streams[pkt->stream_index];
1826  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY) {
1827  ff_reduce_index(s, st->index);
1829  }
1830 
1831  if (is_relative(pkt->dts))
1832  pkt->dts -= RELATIVE_TS_BASE;
1833  if (is_relative(pkt->pts))
1834  pkt->pts -= RELATIVE_TS_BASE;
1835 
1836  return ret;
1837 }
1838 
1839 /* XXX: suppress the packet queue */
1841 {
1842  if (!s->internal)
1843  return;
1844  avpriv_packet_list_free(&s->internal->parse_queue, &s->internal->parse_queue_end);
1845  avpriv_packet_list_free(&s->internal->packet_buffer, &s->internal->packet_buffer_end);
1846  avpriv_packet_list_free(&s->internal->raw_packet_buffer, &s->internal->raw_packet_buffer_end);
1847 
1848  s->internal->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
1849 }
1850 
1851 /*******************************************************/
1852 /* seek support */
1853 
1855 {
1856  int i;
1857  AVStream *st;
1858  int best_stream = 0;
1859  int best_score = INT_MIN;
1860 
1861  if (s->nb_streams <= 0)
1862  return -1;
1863  for (i = 0; i < s->nb_streams; i++) {
1864  int score = 0;
1865  st = s->streams[i];
1866  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
1868  score -= 400;
1869  if (st->codecpar->width && st->codecpar->height)
1870  score += 50;
1871  score+= 25;
1872  }
1873  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
1874  if (st->codecpar->sample_rate)
1875  score += 50;
1876  }
1877  if (st->codec_info_nb_frames)
1878  score += 12;
1879 
1880  if (st->discard != AVDISCARD_ALL)
1881  score += 200;
1882 
1883  if (score > best_score) {
1884  best_score = score;
1885  best_stream = i;
1886  }
1887  }
1888  return best_stream;
1889 }
1890 
1891 /** Flush the frame reader. */
1893 {
1894  AVStream *st;
1895  int i, j;
1896 
1898 
1899  /* Reset read state for each stream. */
1900  for (i = 0; i < s->nb_streams; i++) {
1901  st = s->streams[i];
1902 
1903  if (st->parser) {
1904  av_parser_close(st->parser);
1905  st->parser = NULL;
1906  }
1909  if (st->first_dts == AV_NOPTS_VALUE)
1910  st->cur_dts = RELATIVE_TS_BASE;
1911  else
1912  /* We set the current DTS to an unspecified origin. */
1913  st->cur_dts = AV_NOPTS_VALUE;
1914 
1915  st->probe_packets = s->max_probe_packets;
1916 
1917  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
1919 
1920  if (s->internal->inject_global_side_data)
1922 
1923  st->internal->skip_samples = 0;
1924  }
1925 }
1926 
1927 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1928 {
1929  int i;
1930 
1931  for (i = 0; i < s->nb_streams; i++) {
1932  AVStream *st = s->streams[i];
1933 
1934  st->cur_dts =
1935  av_rescale(timestamp,
1936  st->time_base.den * (int64_t) ref_st->time_base.num,
1937  st->time_base.num * (int64_t) ref_st->time_base.den);
1938  }
1939 }
1940 
1941 void ff_reduce_index(AVFormatContext *s, int stream_index)
1942 {
1943  AVStream *st = s->streams[stream_index];
1944  unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
1945 
1946  if ((unsigned) st->nb_index_entries >= max_entries) {
1947  int i;
1948  for (i = 0; 2 * i < st->nb_index_entries; i++)
1949  st->index_entries[i] = st->index_entries[2 * i];
1950  st->nb_index_entries = i;
1951  }
1952 }
1953 
1954 int ff_add_index_entry(AVIndexEntry **index_entries,
1955  int *nb_index_entries,
1956  unsigned int *index_entries_allocated_size,
1957  int64_t pos, int64_t timestamp,
1958  int size, int distance, int flags)
1959 {
1960  AVIndexEntry *entries, *ie;
1961  int index;
1962 
1963  if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1964  return -1;
1965 
1966  if (timestamp == AV_NOPTS_VALUE)
1967  return AVERROR(EINVAL);
1968 
1969  if (size < 0 || size > 0x3FFFFFFF)
1970  return AVERROR(EINVAL);
1971 
1972  if (is_relative(timestamp)) //FIXME this maintains previous behavior but we should shift by the correct offset once known
1973  timestamp -= RELATIVE_TS_BASE;
1974 
1975  entries = av_fast_realloc(*index_entries,
1976  index_entries_allocated_size,
1977  (*nb_index_entries + 1) *
1978  sizeof(AVIndexEntry));
1979  if (!entries)
1980  return -1;
1981 
1982  *index_entries = entries;
1983 
1984  index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
1985  timestamp, AVSEEK_FLAG_ANY);
1986 
1987  if (index < 0) {
1988  index = (*nb_index_entries)++;
1989  ie = &entries[index];
1990  av_assert0(index == 0 || ie[-1].timestamp < timestamp);
1991  } else {
1992  ie = &entries[index];
1993  if (ie->timestamp != timestamp) {
1994  if (ie->timestamp <= timestamp)
1995  return -1;
1996  memmove(entries + index + 1, entries + index,
1997  sizeof(AVIndexEntry) * (*nb_index_entries - index));
1998  (*nb_index_entries)++;
1999  } else if (ie->pos == pos && distance < ie->min_distance)
2000  // do not reduce the distance
2001  distance = ie->min_distance;
2002  }
2003 
2004  ie->pos = pos;
2005  ie->timestamp = timestamp;
2006  ie->min_distance = distance;
2007  ie->size = size;
2008  ie->flags = flags;
2009 
2010  return index;
2011 }
2012 
2013 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
2014  int size, int distance, int flags)
2015 {
2016  timestamp = wrap_timestamp(st, timestamp);
2019  timestamp, size, distance, flags);
2020 }
2021 
2022 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
2023  int64_t wanted_timestamp, int flags)
2024 {
2025  int a, b, m;
2026  int64_t timestamp;
2027 
2028  a = -1;
2029  b = nb_entries;
2030 
2031  // Optimize appending index entries at the end.
2032  if (b && entries[b - 1].timestamp < wanted_timestamp)
2033  a = b - 1;
2034 
2035  while (b - a > 1) {
2036  m = (a + b) >> 1;
2037 
2038  // Search for the next non-discarded packet.
2039  while ((entries[m].flags & AVINDEX_DISCARD_FRAME) && m < b && m < nb_entries - 1) {
2040  m++;
2041  if (m == b && entries[m].timestamp >= wanted_timestamp) {
2042  m = b - 1;
2043  break;
2044  }
2045  }
2046 
2047  timestamp = entries[m].timestamp;
2048  if (timestamp >= wanted_timestamp)
2049  b = m;
2050  if (timestamp <= wanted_timestamp)
2051  a = m;
2052  }
2053  m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
2054 
2055  if (!(flags & AVSEEK_FLAG_ANY))
2056  while (m >= 0 && m < nb_entries &&
2057  !(entries[m].flags & AVINDEX_KEYFRAME))
2058  m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
2059 
2060  if (m == nb_entries)
2061  return -1;
2062  return m;
2063 }
2064 
2065 void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
2066 {
2067  int ist1, ist2;
2068  int64_t pos_delta = 0;
2069  int64_t skip = 0;
2070  //We could use URLProtocol flags here but as many user applications do not use URLProtocols this would be unreliable
2071  const char *proto = avio_find_protocol_name(s->url);
2072 
2073  av_assert0(time_tolerance >= 0);
2074 
2075  if (!proto) {
2076  av_log(s, AV_LOG_INFO,
2077  "Protocol name not provided, cannot determine if input is local or "
2078  "a network protocol, buffers and access patterns cannot be configured "
2079  "optimally without knowing the protocol\n");
2080  }
2081 
2082  if (proto && !(strcmp(proto, "file") && strcmp(proto, "pipe") && strcmp(proto, "cache")))
2083  return;
2084 
2085  for (ist1 = 0; ist1 < s->nb_streams; ist1++) {
2086  AVStream *st1 = s->streams[ist1];
2087  for (ist2 = 0; ist2 < s->nb_streams; ist2++) {
2088  AVStream *st2 = s->streams[ist2];
2089  int i1, i2;
2090 
2091  if (ist1 == ist2)
2092  continue;
2093 
2094  for (i1 = i2 = 0; i1 < st1->nb_index_entries; i1++) {
2095  AVIndexEntry *e1 = &st1->index_entries[i1];
2096  int64_t e1_pts = av_rescale_q(e1->timestamp, st1->time_base, AV_TIME_BASE_Q);
2097 
2098  skip = FFMAX(skip, e1->size);
2099  for (; i2 < st2->nb_index_entries; i2++) {
2100  AVIndexEntry *e2 = &st2->index_entries[i2];
2101  int64_t e2_pts = av_rescale_q(e2->timestamp, st2->time_base, AV_TIME_BASE_Q);
2102  if (e2_pts < e1_pts || e2_pts - (uint64_t)e1_pts < time_tolerance)
2103  continue;
2104  pos_delta = FFMAX(pos_delta, e1->pos - e2->pos);
2105  break;
2106  }
2107  }
2108  }
2109  }
2110 
2111  pos_delta *= 2;
2112  /* XXX This could be adjusted depending on protocol*/
2113  if (s->pb->buffer_size < pos_delta && pos_delta < (1<<24)) {
2114  av_log(s, AV_LOG_VERBOSE, "Reconfiguring buffers to size %"PRId64"\n", pos_delta);
2115 
2116  /* realloc the buffer and the original data will be retained */
2117  if (ffio_realloc_buf(s->pb, pos_delta)) {
2118  av_log(s, AV_LOG_ERROR, "Realloc buffer fail.\n");
2119  return;
2120  }
2121 
2122  s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, pos_delta/2);
2123  }
2124 
2125  if (skip < (1<<23)) {
2126  s->pb->short_seek_threshold = FFMAX(s->pb->short_seek_threshold, skip);
2127  }
2128 }
2129 
2130 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
2131 {
2133  wanted_timestamp, flags);
2134 }
2135 
2136 static int64_t ff_read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit,
2137  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
2138 {
2139  int64_t ts = read_timestamp(s, stream_index, ppos, pos_limit);
2140  if (stream_index >= 0)
2141  ts = wrap_timestamp(s->streams[stream_index], ts);
2142  return ts;
2143 }
2144 
2145 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
2146  int64_t target_ts, int flags)
2147 {
2148  const AVInputFormat *avif = s->iformat;
2149  int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
2150  int64_t ts_min, ts_max, ts;
2151  int index;
2152  int64_t ret;
2153  AVStream *st;
2154 
2155  if (stream_index < 0)
2156  return -1;
2157 
2158  av_log(s, AV_LOG_TRACE, "read_seek: %d %s\n", stream_index, av_ts2str(target_ts));
2159 
2160  ts_max =
2161  ts_min = AV_NOPTS_VALUE;
2162  pos_limit = -1; // GCC falsely says it may be uninitialized.
2163 
2164  st = s->streams[stream_index];
2165  if (st->index_entries) {
2166  AVIndexEntry *e;
2167 
2168  /* FIXME: Whole function must be checked for non-keyframe entries in
2169  * index case, especially read_timestamp(). */
2170  index = av_index_search_timestamp(st, target_ts,
2172  index = FFMAX(index, 0);
2173  e = &st->index_entries[index];
2174 
2175  if (e->timestamp <= target_ts || e->pos == e->min_distance) {
2176  pos_min = e->pos;
2177  ts_min = e->timestamp;
2178  av_log(s, AV_LOG_TRACE, "using cached pos_min=0x%"PRIx64" dts_min=%s\n",
2179  pos_min, av_ts2str(ts_min));
2180  } else {
2181  av_assert1(index == 0);
2182  }
2183 
2184  index = av_index_search_timestamp(st, target_ts,
2186  av_assert0(index < st->nb_index_entries);
2187  if (index >= 0) {
2188  e = &st->index_entries[index];
2189  av_assert1(e->timestamp >= target_ts);
2190  pos_max = e->pos;
2191  ts_max = e->timestamp;
2192  pos_limit = pos_max - e->min_distance;
2193  av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
2194  " dts_max=%s\n", pos_max, pos_limit, av_ts2str(ts_max));
2195  }
2196  }
2197 
2198  pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
2199  ts_min, ts_max, flags, &ts, avif->read_timestamp);
2200  if (pos < 0)
2201  return -1;
2202 
2203  /* do the seek */
2204  if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
2205  return ret;
2206 
2208  ff_update_cur_dts(s, st, ts);
2209 
2210  return 0;
2211 }
2212 
2213 int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos,
2214  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
2215 {
2216  int64_t step = 1024;
2217  int64_t limit, ts_max;
2218  int64_t filesize = avio_size(s->pb);
2219  int64_t pos_max = filesize - 1;
2220  do {
2221  limit = pos_max;
2222  pos_max = FFMAX(0, (pos_max) - step);
2223  ts_max = ff_read_timestamp(s, stream_index,
2224  &pos_max, limit, read_timestamp);
2225  step += step;
2226  } while (ts_max == AV_NOPTS_VALUE && 2*limit > step);
2227  if (ts_max == AV_NOPTS_VALUE)
2228  return -1;
2229 
2230  for (;;) {
2231  int64_t tmp_pos = pos_max + 1;
2232  int64_t tmp_ts = ff_read_timestamp(s, stream_index,
2233  &tmp_pos, INT64_MAX, read_timestamp);
2234  if (tmp_ts == AV_NOPTS_VALUE)
2235  break;
2236  av_assert0(tmp_pos > pos_max);
2237  ts_max = tmp_ts;
2238  pos_max = tmp_pos;
2239  if (tmp_pos >= filesize)
2240  break;
2241  }
2242 
2243  if (ts)
2244  *ts = ts_max;
2245  if (pos)
2246  *pos = pos_max;
2247 
2248  return 0;
2249 }
2250 
2251 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
2252  int64_t pos_min, int64_t pos_max, int64_t pos_limit,
2253  int64_t ts_min, int64_t ts_max,
2254  int flags, int64_t *ts_ret,
2255  int64_t (*read_timestamp)(struct AVFormatContext *, int,
2256  int64_t *, int64_t))
2257 {
2258  int64_t pos, ts;
2259  int64_t start_pos;
2260  int no_change;
2261  int ret;
2262 
2263  av_log(s, AV_LOG_TRACE, "gen_seek: %d %s\n", stream_index, av_ts2str(target_ts));
2264 
2265  if (ts_min == AV_NOPTS_VALUE) {
2266  pos_min = s->internal->data_offset;
2267  ts_min = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2268  if (ts_min == AV_NOPTS_VALUE)
2269  return -1;
2270  }
2271 
2272  if (ts_min >= target_ts) {
2273  *ts_ret = ts_min;
2274  return pos_min;
2275  }
2276 
2277  if (ts_max == AV_NOPTS_VALUE) {
2278  if ((ret = ff_find_last_ts(s, stream_index, &ts_max, &pos_max, read_timestamp)) < 0)
2279  return ret;
2280  pos_limit = pos_max;
2281  }
2282 
2283  if (ts_max <= target_ts) {
2284  *ts_ret = ts_max;
2285  return pos_max;
2286  }
2287 
2288  av_assert0(ts_min < ts_max);
2289 
2290  no_change = 0;
2291  while (pos_min < pos_limit) {
2293  "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%s dts_max=%s\n",
2294  pos_min, pos_max, av_ts2str(ts_min), av_ts2str(ts_max));
2295  av_assert0(pos_limit <= pos_max);
2296 
2297  if (no_change == 0) {
2298  int64_t approximate_keyframe_distance = pos_max - pos_limit;
2299  // interpolate position (better than dichotomy)
2300  pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
2301  ts_max - ts_min) +
2302  pos_min - approximate_keyframe_distance;
2303  } else if (no_change == 1) {
2304  // bisection if interpolation did not change min / max pos last time
2305  pos = (pos_min + pos_limit) >> 1;
2306  } else {
2307  /* linear search if bisection failed, can only happen if there
2308  * are very few or no keyframes between min/max */
2309  pos = pos_min;
2310  }
2311  if (pos <= pos_min)
2312  pos = pos_min + 1;
2313  else if (pos > pos_limit)
2314  pos = pos_limit;
2315  start_pos = pos;
2316 
2317  // May pass pos_limit instead of -1.
2318  ts = ff_read_timestamp(s, stream_index, &pos, INT64_MAX, read_timestamp);
2319  if (pos == pos_max)
2320  no_change++;
2321  else
2322  no_change = 0;
2323  av_log(s, AV_LOG_TRACE, "%"PRId64" %"PRId64" %"PRId64" / %s %s %s"
2324  " target:%s limit:%"PRId64" start:%"PRId64" noc:%d\n",
2325  pos_min, pos, pos_max,
2326  av_ts2str(ts_min), av_ts2str(ts), av_ts2str(ts_max), av_ts2str(target_ts),
2327  pos_limit, start_pos, no_change);
2328  if (ts == AV_NOPTS_VALUE) {
2329  av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
2330  return -1;
2331  }
2332  if (target_ts <= ts) {
2333  pos_limit = start_pos - 1;
2334  pos_max = pos;
2335  ts_max = ts;
2336  }
2337  if (target_ts >= ts) {
2338  pos_min = pos;
2339  ts_min = ts;
2340  }
2341  }
2342 
2343  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
2344  ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
2345 #if 0
2346  pos_min = pos;
2347  ts_min = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2348  pos_min++;
2349  ts_max = ff_read_timestamp(s, stream_index, &pos_min, INT64_MAX, read_timestamp);
2350  av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" %s<=%s<=%s\n",
2351  pos, av_ts2str(ts_min), av_ts2str(target_ts), av_ts2str(ts_max));
2352 #endif
2353  *ts_ret = ts;
2354  return pos;
2355 }
2356 
2357 static int seek_frame_byte(AVFormatContext *s, int stream_index,
2358  int64_t pos, int flags)
2359 {
2360  int64_t pos_min, pos_max;
2361 
2362  pos_min = s->internal->data_offset;
2363  pos_max = avio_size(s->pb) - 1;
2364 
2365  if (pos < pos_min)
2366  pos = pos_min;
2367  else if (pos > pos_max)
2368  pos = pos_max;
2369 
2370  avio_seek(s->pb, pos, SEEK_SET);
2371 
2372  s->io_repositioned = 1;
2373 
2374  return 0;
2375 }
2376 
2377 static int seek_frame_generic(AVFormatContext *s, int stream_index,
2378  int64_t timestamp, int flags)
2379 {
2380  int index;
2381  int64_t ret;
2382  AVStream *st;
2383  AVIndexEntry *ie;
2384 
2385  st = s->streams[stream_index];
2386 
2387  index = av_index_search_timestamp(st, timestamp, flags);
2388 
2389  if (index < 0 && st->nb_index_entries &&
2390  timestamp < st->index_entries[0].timestamp)
2391  return -1;
2392 
2393  if (index < 0 || index == st->nb_index_entries - 1) {
2394  AVPacket *pkt = s->internal->pkt;
2395  int nonkey = 0;
2396 
2397  if (st->nb_index_entries) {
2399  ie = &st->index_entries[st->nb_index_entries - 1];
2400  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2401  return ret;
2402  ff_update_cur_dts(s, st, ie->timestamp);
2403  } else {
2404  if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
2405  return ret;
2406  }
2408  for (;;) {
2409  int read_status;
2410  do {
2411  read_status = av_read_frame(s, pkt);
2412  } while (read_status == AVERROR(EAGAIN));
2413  if (read_status < 0)
2414  break;
2415  if (stream_index == pkt->stream_index && pkt->dts > timestamp) {
2416  if (pkt->flags & AV_PKT_FLAG_KEY) {
2418  break;
2419  }
2420  if (nonkey++ > 1000 && st->codecpar->codec_id != AV_CODEC_ID_CDGRAPHICS) {
2421  av_log(s, AV_LOG_ERROR,"seek_frame_generic failed as this stream seems to contain no keyframes after the target timestamp, %d non keyframes found\n", nonkey);
2423  break;
2424  }
2425  }
2427  }
2428  index = av_index_search_timestamp(st, timestamp, flags);
2429  }
2430  if (index < 0)
2431  return -1;
2432 
2434  if (s->iformat->read_seek)
2435  if (s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
2436  return 0;
2437  ie = &st->index_entries[index];
2438  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
2439  return ret;
2440  ff_update_cur_dts(s, st, ie->timestamp);
2441 
2442  return 0;
2443 }
2444 
2445 static int seek_frame_internal(AVFormatContext *s, int stream_index,
2446  int64_t timestamp, int flags)
2447 {
2448  int ret;
2449  AVStream *st;
2450 
2451  if (flags & AVSEEK_FLAG_BYTE) {
2452  if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
2453  return -1;
2455  return seek_frame_byte(s, stream_index, timestamp, flags);
2456  }
2457 
2458  if (stream_index < 0) {
2459  stream_index = av_find_default_stream_index(s);
2460  if (stream_index < 0)
2461  return -1;
2462 
2463  st = s->streams[stream_index];
2464  /* timestamp for default must be expressed in AV_TIME_BASE units */
2465  timestamp = av_rescale(timestamp, st->time_base.den,
2466  AV_TIME_BASE * (int64_t) st->time_base.num);
2467  }
2468 
2469  /* first, we try the format specific seek */
2470  if (s->iformat->read_seek) {
2472  ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
2473  } else
2474  ret = -1;
2475  if (ret >= 0)
2476  return 0;
2477 
2478  if (s->iformat->read_timestamp &&
2479  !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
2481  return ff_seek_frame_binary(s, stream_index, timestamp, flags);
2482  } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
2484  return seek_frame_generic(s, stream_index, timestamp, flags);
2485  } else
2486  return -1;
2487 }
2488 
2489 int av_seek_frame(AVFormatContext *s, int stream_index,
2490  int64_t timestamp, int flags)
2491 {
2492  int ret;
2493 
2494  if (s->iformat->read_seek2 && !s->iformat->read_seek) {
2495  int64_t min_ts = INT64_MIN, max_ts = INT64_MAX;
2496  if ((flags & AVSEEK_FLAG_BACKWARD))
2497  max_ts = timestamp;
2498  else
2499  min_ts = timestamp;
2500  return avformat_seek_file(s, stream_index, min_ts, timestamp, max_ts,
2502  }
2503 
2504  ret = seek_frame_internal(s, stream_index, timestamp, flags);
2505 
2506  if (ret >= 0)
2508 
2509  return ret;
2510 }
2511 
2512 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
2513  int64_t ts, int64_t max_ts, int flags)
2514 {
2515  if (min_ts > ts || max_ts < ts)
2516  return -1;
2517  if (stream_index < -1 || stream_index >= (int)s->nb_streams)
2518  return AVERROR(EINVAL);
2519 
2520  if (s->seek2any>0)
2523 
2524  if (s->iformat->read_seek2) {
2525  int ret;
2527 
2528  if (stream_index == -1 && s->nb_streams == 1) {
2529  AVRational time_base = s->streams[0]->time_base;
2530  ts = av_rescale_q(ts, AV_TIME_BASE_Q, time_base);
2531  min_ts = av_rescale_rnd(min_ts, time_base.den,
2532  time_base.num * (int64_t)AV_TIME_BASE,
2534  max_ts = av_rescale_rnd(max_ts, time_base.den,
2535  time_base.num * (int64_t)AV_TIME_BASE,
2537  stream_index = 0;
2538  }
2539 
2540  ret = s->iformat->read_seek2(s, stream_index, min_ts,
2541  ts, max_ts, flags);
2542 
2543  if (ret >= 0)
2545  return ret;
2546  }
2547 
2548  if (s->iformat->read_timestamp) {
2549  // try to seek via read_timestamp()
2550  }
2551 
2552  // Fall back on old API if new is not implemented but old is.
2553  // Note the old API has somewhat different semantics.
2554  if (s->iformat->read_seek || 1) {
2555  int dir = (ts - (uint64_t)min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0);
2556  int ret = av_seek_frame(s, stream_index, ts, flags | dir);
2557  if (ret<0 && ts != min_ts && max_ts != ts) {
2558  ret = av_seek_frame(s, stream_index, dir ? max_ts : min_ts, flags | dir);
2559  if (ret >= 0)
2560  ret = av_seek_frame(s, stream_index, ts, flags | (dir^AVSEEK_FLAG_BACKWARD));
2561  }
2562  return ret;
2563  }
2564 
2565  // try some generic seek like seek_frame_generic() but with new ts semantics
2566  return -1; //unreachable
2567 }
2568 
2570 {
2572  return 0;
2573 }
2574 
2575 /*******************************************************/
2576 
2577 /**
2578  * Return TRUE if the stream has accurate duration in any stream.
2579  *
2580  * @return TRUE if the stream has accurate duration for at least one component.
2581  */
2583 {
2584  int i;
2585  AVStream *st;
2586 
2587  for (i = 0; i < ic->nb_streams; i++) {
2588  st = ic->streams[i];
2589  if (st->duration != AV_NOPTS_VALUE)
2590  return 1;
2591  }
2592  if (ic->duration != AV_NOPTS_VALUE)
2593  return 1;
2594  return 0;
2595 }
2596 
2597 /**
2598  * Estimate the stream timings from the one of each components.
2599  *
2600  * Also computes the global bitrate if possible.
2601  */
2603 {
2604  int64_t start_time, start_time1, start_time_text, end_time, end_time1, end_time_text;
2605  int64_t duration, duration1, duration_text, filesize;
2606  int i;
2607  AVProgram *p;
2608 
2609  start_time = INT64_MAX;
2610  start_time_text = INT64_MAX;
2611  end_time = INT64_MIN;
2612  end_time_text = INT64_MIN;
2613  duration = INT64_MIN;
2614  duration_text = INT64_MIN;
2615 
2616  for (i = 0; i < ic->nb_streams; i++) {
2617  AVStream *st = ic->streams[i];
2618  int is_text = st->codecpar->codec_type == AVMEDIA_TYPE_SUBTITLE ||
2620  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
2621  start_time1 = av_rescale_q(st->start_time, st->time_base,
2622  AV_TIME_BASE_Q);
2623  if (is_text)
2624  start_time_text = FFMIN(start_time_text, start_time1);
2625  else
2626  start_time = FFMIN(start_time, start_time1);
2627  end_time1 = av_rescale_q_rnd(st->duration, st->time_base,
2630  if (end_time1 != AV_NOPTS_VALUE && (end_time1 > 0 ? start_time1 <= INT64_MAX - end_time1 : start_time1 >= INT64_MIN - end_time1)) {
2631  end_time1 += start_time1;
2632  if (is_text)
2633  end_time_text = FFMAX(end_time_text, end_time1);
2634  else
2635  end_time = FFMAX(end_time, end_time1);
2636  }
2637  for (p = NULL; (p = av_find_program_from_stream(ic, p, i)); ) {
2638  if (p->start_time == AV_NOPTS_VALUE || p->start_time > start_time1)
2639  p->start_time = start_time1;
2640  if (p->end_time < end_time1)
2641  p->end_time = end_time1;
2642  }
2643  }
2644  if (st->duration != AV_NOPTS_VALUE) {
2645  duration1 = av_rescale_q(st->duration, st->time_base,
2646  AV_TIME_BASE_Q);
2647  if (is_text)
2648  duration_text = FFMAX(duration_text, duration1);
2649  else
2650  duration = FFMAX(duration, duration1);
2651  }
2652  }
2653  if (start_time == INT64_MAX || (start_time > start_time_text && start_time - (uint64_t)start_time_text < AV_TIME_BASE))
2654  start_time = start_time_text;
2655  else if (start_time > start_time_text)
2656  av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream starttime %f\n", start_time_text / (float)AV_TIME_BASE);
2657 
2658  if (end_time == INT64_MIN || (end_time < end_time_text && end_time_text - (uint64_t)end_time < AV_TIME_BASE))
2659  end_time = end_time_text;
2660  else if (end_time < end_time_text)
2661  av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream endtime %f\n", end_time_text / (float)AV_TIME_BASE);
2662 
2663  if (duration == INT64_MIN || (duration < duration_text && duration_text - duration < AV_TIME_BASE))
2664  duration = duration_text;
2665  else if (duration < duration_text)
2666  av_log(ic, AV_LOG_VERBOSE, "Ignoring outlier non primary stream duration %f\n", duration_text / (float)AV_TIME_BASE);
2667 
2668  if (start_time != INT64_MAX) {
2669  ic->start_time = start_time;
2670  if (end_time != INT64_MIN) {
2671  if (ic->nb_programs > 1) {
2672  for (i = 0; i < ic->nb_programs; i++) {
2673  p = ic->programs[i];
2674  if (p->start_time != AV_NOPTS_VALUE &&
2675  p->end_time > p->start_time &&
2676  p->end_time - (uint64_t)p->start_time <= INT64_MAX)
2678  }
2679  } else if (end_time >= start_time && end_time - (uint64_t)start_time <= INT64_MAX) {
2680  duration = FFMAX(duration, end_time - start_time);
2681  }
2682  }
2683  }
2684  if (duration != INT64_MIN && duration > 0 && ic->duration == AV_NOPTS_VALUE) {
2685  ic->duration = duration;
2686  }
2687  if (ic->pb && (filesize = avio_size(ic->pb)) > 0 && ic->duration > 0) {
2688  /* compute the bitrate */
2689  double bitrate = (double) filesize * 8.0 * AV_TIME_BASE /
2690  (double) ic->duration;
2691  if (bitrate >= 0 && bitrate <= INT64_MAX)
2692  ic->bit_rate = bitrate;
2693  }
2694 }
2695 
2697 {
2698  int i;
2699  AVStream *st;
2700 
2702  for (i = 0; i < ic->nb_streams; i++) {
2703  st = ic->streams[i];
2704  if (st->start_time == AV_NOPTS_VALUE) {
2705  if (ic->start_time != AV_NOPTS_VALUE)
2707  st->time_base);
2708  if (ic->duration != AV_NOPTS_VALUE)
2710  st->time_base);
2711  }
2712  }
2713 }
2714 
2716 {
2717  int64_t filesize, duration;
2718  int i, show_warning = 0;
2719  AVStream *st;
2720 
2721  /* if bit_rate is already set, we believe it */
2722  if (ic->bit_rate <= 0) {
2723  int64_t bit_rate = 0;
2724  for (i = 0; i < ic->nb_streams; i++) {
2725  st = ic->streams[i];
2726  if (st->codecpar->bit_rate <= 0 && st->internal->avctx->bit_rate > 0)
2727  st->codecpar->bit_rate = st->internal->avctx->bit_rate;
2728  if (st->codecpar->bit_rate > 0) {
2729  if (INT64_MAX - st->codecpar->bit_rate < bit_rate) {
2730  bit_rate = 0;
2731  break;
2732  }
2733  bit_rate += st->codecpar->bit_rate;
2734  } else if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && st->codec_info_nb_frames > 1) {
2735  // If we have a videostream with packets but without a bitrate
2736  // then consider the sum not known
2737  bit_rate = 0;
2738  break;
2739  }
2740  }
2741  ic->bit_rate = bit_rate;
2742  }
2743 
2744  /* if duration is already set, we believe it */
2745  if (ic->duration == AV_NOPTS_VALUE &&
2746  ic->bit_rate != 0) {
2747  filesize = ic->pb ? avio_size(ic->pb) : 0;
2748  if (filesize > ic->internal->data_offset) {
2749  filesize -= ic->internal->data_offset;
2750  for (i = 0; i < ic->nb_streams; i++) {
2751  st = ic->streams[i];
2752  if ( st->time_base.num <= INT64_MAX / ic->bit_rate
2753  && st->duration == AV_NOPTS_VALUE) {
2754  duration = av_rescale(filesize, 8LL * st->time_base.den,
2755  ic->bit_rate *
2756  (int64_t) st->time_base.num);
2757  st->duration = duration;
2758  show_warning = 1;
2759  }
2760  }
2761  }
2762  }
2763  if (show_warning)
2764  av_log(ic, AV_LOG_WARNING,
2765  "Estimating duration from bitrate, this may be inaccurate\n");
2766 }
2767 
2768 #define DURATION_MAX_READ_SIZE 250000LL
2769 #define DURATION_MAX_RETRY 6
2770 
2771 /* only usable for MPEG-PS streams */
2772 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
2773 {
2774  AVPacket *pkt = ic->internal->pkt;
2775  AVStream *st;
2776  int num, den, read_size, i, ret;
2777  int found_duration = 0;
2778  int is_end;
2779  int64_t filesize, offset, duration;
2780  int retry = 0;
2781 
2782  /* flush packet queue */
2783  flush_packet_queue(ic);
2784 
2785  for (i = 0; i < ic->nb_streams; i++) {
2786  st = ic->streams[i];
2787  if (st->start_time == AV_NOPTS_VALUE &&
2788  st->first_dts == AV_NOPTS_VALUE &&
2790  av_log(ic, AV_LOG_WARNING,
2791  "start time for stream %d is not set in estimate_timings_from_pts\n", i);
2792 
2793  if (st->parser) {
2794  av_parser_close(st->parser);
2795  st->parser = NULL;
2796  }
2797  }
2798 
2800  av_log(ic, AV_LOG_INFO, "Skipping duration calculation in estimate_timings_from_pts\n");
2801  goto skip_duration_calc;
2802  }
2803 
2804  av_opt_set(ic, "skip_changes", "1", AV_OPT_SEARCH_CHILDREN);
2805  /* estimate the end time (duration) */
2806  /* XXX: may need to support wrapping */
2807  filesize = ic->pb ? avio_size(ic->pb) : 0;
2808  do {
2809  is_end = found_duration;
2810  offset = filesize - (DURATION_MAX_READ_SIZE << retry);
2811  if (offset < 0)
2812  offset = 0;
2813 
2814  avio_seek(ic->pb, offset, SEEK_SET);
2815  read_size = 0;
2816  for (;;) {
2817  if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
2818  break;
2819 
2820  do {
2821  ret = ff_read_packet(ic, pkt);
2822  } while (ret == AVERROR(EAGAIN));
2823  if (ret != 0)
2824  break;
2825  read_size += pkt->size;
2826  st = ic->streams[pkt->stream_index];
2827  if (pkt->pts != AV_NOPTS_VALUE &&
2828  (st->start_time != AV_NOPTS_VALUE ||
2829  st->first_dts != AV_NOPTS_VALUE)) {
2830  if (pkt->duration == 0) {
2831  ff_compute_frame_duration(ic, &num, &den, st, st->parser, pkt);
2832  if (den && num) {
2834  num * (int64_t) st->time_base.den,
2835  den * (int64_t) st->time_base.num,
2836  AV_ROUND_DOWN);
2837  }
2838  }
2839  duration = pkt->pts + pkt->duration;
2840  found_duration = 1;
2841  if (st->start_time != AV_NOPTS_VALUE)
2842  duration -= st->start_time;
2843  else
2844  duration -= st->first_dts;
2845  if (duration > 0) {
2846  if (st->duration == AV_NOPTS_VALUE || st->internal->info->last_duration<= 0 ||
2847  (st->duration < duration && FFABS(duration - st->internal->info->last_duration) < 60LL*st->time_base.den / st->time_base.num))
2848  st->duration = duration;
2850  }
2851  }
2853  }
2854 
2855  /* check if all audio/video streams have valid duration */
2856  if (!is_end) {
2857  is_end = 1;
2858  for (i = 0; i < ic->nb_streams; i++) {
2859  st = ic->streams[i];
2860  switch (st->codecpar->codec_type) {
2861  case AVMEDIA_TYPE_VIDEO:
2862  case AVMEDIA_TYPE_AUDIO:
2863  if (st->duration == AV_NOPTS_VALUE)
2864  is_end = 0;
2865  }
2866  }
2867  }
2868  } while (!is_end &&
2869  offset &&
2870  ++retry <= DURATION_MAX_RETRY);
2871 
2872  av_opt_set(ic, "skip_changes", "0", AV_OPT_SEARCH_CHILDREN);
2873 
2874  /* warn about audio/video streams which duration could not be estimated */
2875  for (i = 0; i < ic->nb_streams; i++) {
2876  st = ic->streams[i];
2877  if (st->duration == AV_NOPTS_VALUE) {
2878  switch (st->codecpar->codec_type) {
2879  case AVMEDIA_TYPE_VIDEO:
2880  case AVMEDIA_TYPE_AUDIO:
2881  if (st->start_time != AV_NOPTS_VALUE || st->first_dts != AV_NOPTS_VALUE) {
2882  av_log(ic, AV_LOG_WARNING, "stream %d : no PTS found at end of file, duration not set\n", i);
2883  } else
2884  av_log(ic, AV_LOG_WARNING, "stream %d : no TS found at start of file, duration not set\n", i);
2885  }
2886  }
2887  }
2888 skip_duration_calc:
2890 
2891  avio_seek(ic->pb, old_offset, SEEK_SET);
2892  for (i = 0; i < ic->nb_streams; i++) {
2893  int j;
2894 
2895  st = ic->streams[i];
2896  st->cur_dts = st->first_dts;
2899  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
2901  }
2902 }
2903 
2904 /* 1:1 map to AVDurationEstimationMethod */
2905 static const char *const duration_name[] = {
2906  [AVFMT_DURATION_FROM_PTS] = "pts",
2907  [AVFMT_DURATION_FROM_STREAM] = "stream",
2908  [AVFMT_DURATION_FROM_BITRATE] = "bit rate",
2909 };
2910 
2912 {
2913  return duration_name[method];
2914 }
2915 
2916 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
2917 {
2918  int64_t file_size;
2919 
2920  /* get the file size, if possible */
2921  if (ic->iformat->flags & AVFMT_NOFILE) {
2922  file_size = 0;
2923  } else {
2924  file_size = avio_size(ic->pb);
2925  file_size = FFMAX(0, file_size);
2926  }
2927 
2928  if ((!strcmp(ic->iformat->name, "mpeg") ||
2929  !strcmp(ic->iformat->name, "mpegts")) &&
2930  file_size && (ic->pb->seekable & AVIO_SEEKABLE_NORMAL)) {
2931  /* get accurate estimate from the PTSes */
2932  estimate_timings_from_pts(ic, old_offset);
2934  } else if (has_duration(ic)) {
2935  /* at least one component has timings - we use them for all
2936  * the components */
2938  /* nut demuxer estimate the duration from PTS */
2939  if(!strcmp(ic->iformat->name, "nut"))
2941  else
2943  } else {
2944  /* less precise: use bitrate info */
2947  }
2949 
2950  {
2951  int i;
2952  AVStream av_unused *st;
2953  for (i = 0; i < ic->nb_streams; i++) {
2954  st = ic->streams[i];
2955  if (st->time_base.den)
2956  av_log(ic, AV_LOG_TRACE, "stream %d: start_time: %s duration: %s\n", i,
2957  av_ts2timestr(st->start_time, &st->time_base),
2958  av_ts2timestr(st->duration, &st->time_base));
2959  }
2960  av_log(ic, AV_LOG_TRACE,
2961  "format: start_time: %s duration: %s (estimate from %s) bitrate=%"PRId64" kb/s\n",
2965  (int64_t)ic->bit_rate / 1000);
2966  }
2967 }
2968 
2969 static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
2970 {
2971  AVCodecContext *avctx = st->internal->avctx;
2972 
2973 #define FAIL(errmsg) do { \
2974  if (errmsg_ptr) \
2975  *errmsg_ptr = errmsg; \
2976  return 0; \
2977  } while (0)
2978 
2979  if ( avctx->codec_id == AV_CODEC_ID_NONE
2980  && avctx->codec_type != AVMEDIA_TYPE_DATA)
2981  FAIL("unknown codec");
2982  switch (avctx->codec_type) {
2983  case AVMEDIA_TYPE_AUDIO:
2984  if (!avctx->frame_size && determinable_frame_size(avctx))
2985  FAIL("unspecified frame size");
2986  if (st->internal->info->found_decoder >= 0 &&
2987  avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2988  FAIL("unspecified sample format");
2989  if (!avctx->sample_rate)
2990  FAIL("unspecified sample rate");
2991  if (!avctx->channels)
2992  FAIL("unspecified number of channels");
2993  if (st->internal->info->found_decoder >= 0 && !st->internal->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
2994  FAIL("no decodable DTS frames");
2995  break;
2996  case AVMEDIA_TYPE_VIDEO:
2997  if (!avctx->width)
2998  FAIL("unspecified size");
2999  if (st->internal->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
3000  FAIL("unspecified pixel format");
3003  FAIL("no frame in rv30/40 and no sar");
3004  break;
3005  case AVMEDIA_TYPE_SUBTITLE:
3006  if (avctx->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !avctx->width)
3007  FAIL("unspecified size");
3008  break;
3009  case AVMEDIA_TYPE_DATA:
3010  if (avctx->codec_id == AV_CODEC_ID_NONE) return 1;
3011  }
3012 
3013  return 1;
3014 }
3015 
3016 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
3018  const AVPacket *avpkt, AVDictionary **options)
3019 {
3020  AVCodecContext *avctx = st->internal->avctx;
3021  const AVCodec *codec;
3022  int got_picture = 1, ret = 0;
3024  AVSubtitle subtitle;
3025  AVPacket pkt = *avpkt;
3026  int do_skip_frame = 0;
3027  enum AVDiscard skip_frame;
3028 
3029  if (!frame)
3030  return AVERROR(ENOMEM);
3031 
3032  if (!avcodec_is_open(avctx) &&
3033  st->internal->info->found_decoder <= 0 &&
3034  (st->codecpar->codec_id != -st->internal->info->found_decoder || !st->codecpar->codec_id)) {
3035  AVDictionary *thread_opt = NULL;
3036 
3037  codec = find_probe_decoder(s, st, st->codecpar->codec_id);
3038 
3039  if (!codec) {
3041  ret = -1;
3042  goto fail;
3043  }
3044 
3045  /* Force thread count to 1 since the H.264 decoder will not extract
3046  * SPS and PPS to extradata during multi-threaded decoding. */
3047  av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
3048  /* Force lowres to 0. The decoder might reduce the video size by the
3049  * lowres factor, and we don't want that propagated to the stream's
3050  * codecpar */
3051  av_dict_set(options ? options : &thread_opt, "lowres", "0", 0);
3052  if (s->codec_whitelist)
3053  av_dict_set(options ? options : &thread_opt, "codec_whitelist", s->codec_whitelist, 0);
3054  ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
3055  if (!options)
3056  av_dict_free(&thread_opt);
3057  if (ret < 0) {
3058  st->internal->info->found_decoder = -avctx->codec_id;
3059  goto fail;
3060  }
3061  st->internal->info->found_decoder = 1;
3062  } else if (!st->internal->info->found_decoder)
3063  st->internal->info->found_decoder = 1;
3064 
3065  if (st->internal->info->found_decoder < 0) {
3066  ret = -1;
3067  goto fail;
3068  }
3069 
3071  do_skip_frame = 1;
3072  skip_frame = avctx->skip_frame;
3073  avctx->skip_frame = AVDISCARD_ALL;
3074  }
3075 
3076  while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
3077  ret >= 0 &&
3079  (!st->codec_info_nb_frames &&
3081  got_picture = 0;
3082  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
3083  avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
3084  ret = avcodec_send_packet(avctx, &pkt);
3085  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
3086  break;
3087  if (ret >= 0)
3088  pkt.size = 0;
3089  ret = avcodec_receive_frame(avctx, frame);
3090  if (ret >= 0)
3091  got_picture = 1;
3092  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
3093  ret = 0;
3094  } else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) {
3095  ret = avcodec_decode_subtitle2(avctx, &subtitle,
3096  &got_picture, &pkt);
3097  if (got_picture)
3098  avsubtitle_free(&subtitle);
3099  if (ret >= 0)
3100  pkt.size = 0;
3101  }
3102  if (ret >= 0) {
3103  if (got_picture)
3104  st->internal->nb_decoded_frames++;
3105  ret = got_picture;
3106  }
3107  }
3108 
3109  if (!pkt.data && !got_picture)
3110  ret = -1;
3111 
3112 fail:
3113  if (do_skip_frame) {
3114  avctx->skip_frame = skip_frame;
3115  }
3116 
3117  av_frame_free(&frame);
3118  return ret;
3119 }
3120 
3121 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
3122 {
3123  while (tags->id != AV_CODEC_ID_NONE) {
3124  if (tags->id == id)
3125  return tags->tag;
3126  tags++;
3127  }
3128  return 0;
3129 }
3130 
3131 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
3132 {
3133  int i;
3134  for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
3135  if (tag == tags[i].tag)
3136  return tags[i].id;
3137  for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
3138  if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
3139  return tags[i].id;
3140  return AV_CODEC_ID_NONE;
3141 }
3142 
3143 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
3144 {
3145  if (bps <= 0 || bps > 64)
3146  return AV_CODEC_ID_NONE;
3147 
3148  if (flt) {
3149  switch (bps) {
3150  case 32:
3152  case 64:
3154  default:
3155  return AV_CODEC_ID_NONE;
3156  }
3157  } else {
3158  bps += 7;
3159  bps >>= 3;
3160  if (sflags & (1 << (bps - 1))) {
3161  switch (bps) {
3162  case 1:
3163  return AV_CODEC_ID_PCM_S8;
3164  case 2:
3166  case 3:
3168  case 4:
3170  case 8:
3172  default:
3173  return AV_CODEC_ID_NONE;
3174  }
3175  } else {
3176  switch (bps) {
3177  case 1:
3178  return AV_CODEC_ID_PCM_U8;
3179  case 2:
3181  case 3:
3183  case 4:
3185  default:
3186  return AV_CODEC_ID_NONE;
3187  }
3188  }
3189  }
3190 }
3191 
3192 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
3193 {
3194  unsigned int tag;
3195  if (!av_codec_get_tag2(tags, id, &tag))
3196  return 0;
3197  return tag;
3198 }
3199 
3200 int av_codec_get_tag2(const AVCodecTag * const *tags, enum AVCodecID id,
3201  unsigned int *tag)
3202 {
3203  int i;
3204  for (i = 0; tags && tags[i]; i++) {
3205  const AVCodecTag *codec_tags = tags[i];
3206  while (codec_tags->id != AV_CODEC_ID_NONE) {
3207  if (codec_tags->id == id) {
3208  *tag = codec_tags->tag;
3209  return 1;
3210  }
3211  codec_tags++;
3212  }
3213  }
3214  return 0;
3215 }
3216 
3217 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
3218 {
3219  int i;
3220  for (i = 0; tags && tags[i]; i++) {
3221  enum AVCodecID id = ff_codec_get_id(tags[i], tag);
3222  if (id != AV_CODEC_ID_NONE)
3223  return id;
3224  }
3225  return AV_CODEC_ID_NONE;
3226 }
3227 
3228 static int chapter_start_cmp(const void *p1, const void *p2)
3229 {
3230  AVChapter *ch1 = *(AVChapter**)p1;
3231  AVChapter *ch2 = *(AVChapter**)p2;
3232  int delta = av_compare_ts(ch1->start, ch1->time_base, ch2->start, ch2->time_base);
3233  if (delta)
3234  return delta;
3235  return (ch1 > ch2) - (ch1 < ch2);
3236 }
3237 
3239 {
3240  unsigned int i;
3241  int64_t max_time = 0;
3242  AVChapter **timetable = av_malloc(s->nb_chapters * sizeof(*timetable));
3243 
3244  if (!timetable)
3245  return AVERROR(ENOMEM);
3246 
3247  if (s->duration > 0 && s->start_time < INT64_MAX - s->duration)
3248  max_time = s->duration +
3249  ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
3250 
3251  for (i = 0; i < s->nb_chapters; i++)
3252  timetable[i] = s->chapters[i];
3253  qsort(timetable, s->nb_chapters, sizeof(*timetable), chapter_start_cmp);
3254 
3255  for (i = 0; i < s->nb_chapters; i++)
3256  if (timetable[i]->end == AV_NOPTS_VALUE) {
3257  AVChapter *ch = timetable[i];
3258  int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
3259  ch->time_base)
3260  : INT64_MAX;
3261 
3262  if (i + 1 < s->nb_chapters) {
3263  AVChapter *ch1 = timetable[i + 1];
3264  int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
3265  ch->time_base);
3266  if (next_start > ch->start && next_start < end)
3267  end = next_start;
3268  }
3269  ch->end = (end == INT64_MAX || end < ch->start) ? ch->start : end;
3270  }
3271  av_free(timetable);
3272  return 0;
3273 }
3274 
3275 static int get_std_framerate(int i)
3276 {
3277  if (i < 30*12)
3278  return (i + 1) * 1001;
3279  i -= 30*12;
3280 
3281  if (i < 30)
3282  return (i + 31) * 1001 * 12;
3283  i -= 30;
3284 
3285  if (i < 3)
3286  return ((const int[]) { 80, 120, 240})[i] * 1001 * 12;
3287 
3288  i -= 3;
3289 
3290  return ((const int[]) { 24, 30, 60, 12, 15, 48 })[i] * 1000 * 12;
3291 }
3292 
3293 /* Is the time base unreliable?
3294  * This is a heuristic to balance between quick acceptance of the values in
3295  * the headers vs. some extra checks.
3296  * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
3297  * MPEG-2 commonly misuses field repeat flags to store different framerates.
3298  * And there are "variable" fps files this needs to detect as well. */
3300 {
3301  if (c->time_base.den >= 101LL * c->time_base.num ||
3302  c->time_base.den < 5LL * c->time_base.num ||
3303  // c->codec_tag == AV_RL32("DIVX") ||
3304  // c->codec_tag == AV_RL32("XVID") ||
3305  c->codec_tag == AV_RL32("mp4v") ||
3306  c->codec_id == AV_CODEC_ID_MPEG2VIDEO ||
3307  c->codec_id == AV_CODEC_ID_GIF ||
3308  c->codec_id == AV_CODEC_ID_HEVC ||
3309  c->codec_id == AV_CODEC_ID_H264)
3310  return 1;
3311  return 0;
3312 }
3313 
3315 {
3316  av_freep(&par->extradata);
3317  par->extradata_size = 0;
3318 
3319  if (size < 0 || size >= INT32_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
3320  return AVERROR(EINVAL);
3321 
3323  if (!par->extradata)
3324  return AVERROR(ENOMEM);
3325 
3326  memset(par->extradata + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
3327  par->extradata_size = size;
3328 
3329  return 0;
3330 }
3331 
3333 {
3334  int ret = ff_alloc_extradata(par, size);
3335  if (ret < 0)
3336  return ret;
3337  ret = avio_read(pb, par->extradata, size);
3338  if (ret != size) {
3339  av_freep(&par->extradata);
3340  par->extradata_size = 0;
3341  av_log(s, AV_LOG_ERROR, "Failed to read extradata of size %d\n", size);
3342  return ret < 0 ? ret : AVERROR_INVALIDDATA;
3343  }
3344 
3345  return ret;
3346 }
3347 
3348 int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
3349 {
3350  int i, j;
3351  int64_t last = st->internal->info->last_dts;
3352 
3353  if ( ts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && ts > last
3354  && ts - (uint64_t)last < INT64_MAX) {
3355  double dts = (is_relative(ts) ? ts - RELATIVE_TS_BASE : ts) * av_q2d(st->time_base);
3356  int64_t duration = ts - last;
3357 
3358  if (!st->internal->info->duration_error)
3359  st->internal->info->duration_error = av_mallocz(sizeof(st->internal->info->duration_error[0])*2);
3360  if (!st->internal->info->duration_error)
3361  return AVERROR(ENOMEM);
3362 
3363 // if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3364 // av_log(NULL, AV_LOG_ERROR, "%f\n", dts);
3365  for (i = 0; i<MAX_STD_TIMEBASES; i++) {
3366  if (st->internal->info->duration_error[0][1][i] < 1e10) {
3367  int framerate = get_std_framerate(i);
3368  double sdts = dts*framerate/(1001*12);
3369  for (j= 0; j<2; j++) {
3370  int64_t ticks = llrint(sdts+j*0.5);
3371  double error= sdts - ticks + j*0.5;
3372  st->internal->info->duration_error[j][0][i] += error;
3373  st->internal->info->duration_error[j][1][i] += error*error;
3374  }
3375  }
3376  }
3377  if (st->internal->info->rfps_duration_sum <= INT64_MAX - duration) {
3378  st->internal->info->duration_count++;
3380  }
3381 
3382  if (st->internal->info->duration_count % 10 == 0) {
3383  int n = st->internal->info->duration_count;
3384  for (i = 0; i<MAX_STD_TIMEBASES; i++) {
3385  if (st->internal->info->duration_error[0][1][i] < 1e10) {
3386  double a0 = st->internal->info->duration_error[0][0][i] / n;
3387  double error0 = st->internal->info->duration_error[0][1][i] / n - a0*a0;
3388  double a1 = st->internal->info->duration_error[1][0][i] / n;
3389  double error1 = st->internal->info->duration_error[1][1][i] / n - a1*a1;
3390  if (error0 > 0.04 && error1 > 0.04) {
3391  st->internal->info->duration_error[0][1][i] = 2e10;
3392  st->internal->info->duration_error[1][1][i] = 2e10;
3393  }
3394  }
3395  }
3396  }
3397 
3398  // ignore the first 4 values, they might have some random jitter
3399  if (st->internal->info->duration_count > 3 && is_relative(ts) == is_relative(last))
3401  }
3402  if (ts != AV_NOPTS_VALUE)
3403  st->internal->info->last_dts = ts;
3404 
3405  return 0;
3406 }
3407 
3409 {
3410  int i, j;
3411 
3412  for (i = 0; i < ic->nb_streams; i++) {
3413  AVStream *st = ic->streams[i];
3414 
3416  continue;
3417  // the check for tb_unreliable() is not completely correct, since this is not about handling
3418  // an unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
3419  // ipmovie.c produces.
3420  if (tb_unreliable(st->internal->avctx) && st->internal->info->duration_count > 15 && st->internal->info->duration_gcd > FFMAX(1, st->time_base.den/(500LL*st->time_base.num)) && !st->r_frame_rate.num &&
3421  st->internal->info->duration_gcd < INT64_MAX / st->time_base.num)
3422  av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->internal->info->duration_gcd, INT_MAX);
3423  if (st->internal->info->duration_count>1 && !st->r_frame_rate.num
3424  && tb_unreliable(st->internal->avctx)) {
3425  int num = 0;
3426  double best_error= 0.01;
3427  AVRational ref_rate = st->r_frame_rate.num ? st->r_frame_rate : av_inv_q(st->time_base);
3428 
3429  for (j= 0; j<MAX_STD_TIMEBASES; j++) {
3430  int k;
3431 
3432  if (st->internal->info->codec_info_duration &&
3433  st->internal->info->codec_info_duration*av_q2d(st->time_base) < (1001*11.5)/get_std_framerate(j))
3434  continue;
3435  if (!st->internal->info->codec_info_duration && get_std_framerate(j) < 1001*12)
3436  continue;
3437 
3438  if (av_q2d(st->time_base) * st->internal->info->rfps_duration_sum / st->internal->info->duration_count < (1001*12.0 * 0.8)/get_std_framerate(j))
3439  continue;
3440 
3441  for (k= 0; k<2; k++) {
3442  int n = st->internal->info->duration_count;
3443  double a= st->internal->info->duration_error[k][0][j] / n;
3444  double error= st->internal->info->duration_error[k][1][j]/n - a*a;
3445 
3446  if (error < best_error && best_error> 0.000000001) {
3447  best_error= error;
3448  num = get_std_framerate(j);
3449  }
3450  if (error < 0.02)
3451  av_log(ic, AV_LOG_DEBUG, "rfps: %f %f\n", get_std_framerate(j) / 12.0/1001, error);
3452  }
3453  }
3454  // do not increase frame rate by more than 1 % in order to match a standard rate.
3455  if (num && (!ref_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(ref_rate)))
3456  av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
3457  }
3458  if ( !st->avg_frame_rate.num
3460  && st->internal->info->codec_info_duration <= 0
3461  && st->internal->info->duration_count > 2
3462  && fabs(1.0 / (av_q2d(st->r_frame_rate) * av_q2d(st->time_base)) - st->internal->info->rfps_duration_sum / (double)st->internal->info->duration_count) <= 1.0
3463  ) {
3464  av_log(ic, AV_LOG_DEBUG, "Setting avg frame rate based on r frame rate\n");
3465  st->avg_frame_rate = st->r_frame_rate;
3466  }
3467 
3470  st->internal->info->duration_count = 0;
3471  st->internal->info->rfps_duration_sum = 0;
3472  }
3473 }
3474 
3476 {
3477  const AVBitStreamFilter *f;
3478 
3479  f = av_bsf_get_by_name("extract_extradata");
3480  if (!f)
3481  return 0;
3482 
3483  if (f->codec_ids) {
3484  const enum AVCodecID *ids;
3485  for (ids = f->codec_ids; *ids != AV_CODEC_ID_NONE; ids++)
3486  if (*ids == st->codecpar->codec_id)
3487  return 1;
3488  }
3489 
3490  return 0;
3491 }
3492 
3494 {
3495  AVStreamInternal *sti = st->internal;
3496  const AVBitStreamFilter *f;
3497  int ret;
3498 
3499  f = av_bsf_get_by_name("extract_extradata");
3500  if (!f)
3501  goto finish;
3502 
3503  /* check that the codec id is supported */
3505  if (!ret)
3506  goto finish;
3507 
3509  if (!sti->extract_extradata.pkt)
3510  return AVERROR(ENOMEM);
3511 
3513  if (ret < 0)
3514  goto fail;
3515 
3517  st->codecpar);
3518  if (ret < 0)
3519  goto fail;
3520 
3522 
3524  if (ret < 0)
3525  goto fail;
3526 
3527 finish:
3528  sti->extract_extradata.inited = 1;
3529 
3530  return 0;
3531 fail:
3534  return ret;
3535 }
3536 
3537 static int extract_extradata(AVStream *st, const AVPacket *pkt)
3538 {
3539  AVStreamInternal *sti = st->internal;
3540  AVPacket *pkt_ref;
3541  int ret;
3542 
3543  if (!sti->extract_extradata.inited) {
3545  if (ret < 0)
3546  return ret;
3547  }
3548 
3549  if (sti->extract_extradata.inited && !sti->extract_extradata.bsf)
3550  return 0;
3551 
3552  pkt_ref = sti->extract_extradata.pkt;
3553  ret = av_packet_ref(pkt_ref, pkt);
3554  if (ret < 0)
3555  return ret;
3556 
3557  ret = av_bsf_send_packet(sti->extract_extradata.bsf, pkt_ref);
3558  if (ret < 0) {
3559  av_packet_unref(pkt_ref);
3560  return ret;
3561  }
3562 
3563  while (ret >= 0 && !sti->avctx->extradata) {
3565  if (ret < 0) {
3566  if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
3567  return ret;
3568  continue;
3569  }
3570 
3571  for (int i = 0; i < pkt_ref->side_data_elems; i++) {
3572  AVPacketSideData *side_data = &pkt_ref->side_data[i];
3573  if (side_data->type == AV_PKT_DATA_NEW_EXTRADATA) {
3574  sti->avctx->extradata = side_data->data;
3575  sti->avctx->extradata_size = side_data->size;
3576  side_data->data = NULL;
3577  side_data->size = 0;
3578  break;
3579  }
3580  }
3581  av_packet_unref(pkt_ref);
3582  }
3583 
3584  return 0;
3585 }
3586 
3588 {
3589  int i;
3590 
3591  for (i = 0; i < avctx->nb_coded_side_data; i++) {
3592  const AVPacketSideData *sd_src = &avctx->coded_side_data[i];
3593  uint8_t *dst_data;
3594  dst_data = av_stream_new_side_data(st, sd_src->type, sd_src->size);
3595  if (!dst_data)
3596  return AVERROR(ENOMEM);
3597  memcpy(dst_data, sd_src->data, sd_src->size);
3598  }
3599  return 0;
3600 }
3601 
3603 {
3604  int i, count = 0, ret = 0, j;
3605  int64_t read_size;
3606  AVStream *st;
3607  AVCodecContext *avctx;
3608  AVPacket *pkt1 = ic->internal->pkt;
3609  int64_t old_offset = avio_tell(ic->pb);
3610  // new streams might appear, no options for those
3611  int orig_nb_streams = ic->nb_streams;
3612  int flush_codecs;
3613  int64_t max_analyze_duration = ic->max_analyze_duration;
3614  int64_t max_stream_analyze_duration;
3615  int64_t max_subtitle_analyze_duration;
3616  int64_t probesize = ic->probesize;
3617  int eof_reached = 0;
3618  int *missing_streams = av_opt_ptr(ic->iformat->priv_class, ic->priv_data, "missing_streams");
3619 
3620  flush_codecs = probesize > 0;
3621 
3622  av_opt_set(ic, "skip_clear", "1", AV_OPT_SEARCH_CHILDREN);
3623 
3624  max_stream_analyze_duration = max_analyze_duration;
3625  max_subtitle_analyze_duration = max_analyze_duration;
3626  if (!max_analyze_duration) {
3627  max_stream_analyze_duration =
3628  max_analyze_duration = 5*AV_TIME_BASE;
3629  max_subtitle_analyze_duration = 30*AV_TIME_BASE;
3630  if (!strcmp(ic->iformat->name, "flv"))
3631  max_stream_analyze_duration = 90*AV_TIME_BASE;
3632  if (!strcmp(ic->iformat->name, "mpeg") || !strcmp(ic->iformat->name, "mpegts"))
3633  max_stream_analyze_duration = 7*AV_TIME_BASE;
3634  }
3635 
3636  if (ic->pb)
3637  av_log(ic, AV_LOG_DEBUG, "Before avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d nb_streams:%d\n",
3638  avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, ic->nb_streams);
3639 
3640  for (i = 0; i < ic->nb_streams; i++) {
3641  const AVCodec *codec;
3642  AVDictionary *thread_opt = NULL;
3643  st = ic->streams[i];
3644  avctx = st->internal->avctx;
3645 
3646  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO ||
3648 /* if (!st->time_base.num)
3649  st->time_base = */
3650  if (!avctx->time_base.num)
3651  avctx->time_base = st->time_base;
3652  }
3653 
3654  /* check if the caller has overridden the codec id */
3655 #if FF_API_LAVF_AVCTX
3657  if (st->codec->codec_id != st->internal->orig_codec_id) {
3658  st->codecpar->codec_id = st->codec->codec_id;
3659  st->codecpar->codec_type = st->codec->codec_type;
3660  st->internal->orig_codec_id = st->codec->codec_id;
3661  }
3663 #endif
3664  // only for the split stuff
3665  if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE) && st->internal->request_probe <= 0) {
3666  st->parser = av_parser_init(st->codecpar->codec_id);
3667  if (st->parser) {
3668  if (st->need_parsing == AVSTREAM_PARSE_HEADERS) {
3670  } else if (st->need_parsing == AVSTREAM_PARSE_FULL_RAW) {
3672  }
3673  } else if (st->need_parsing) {
3674  av_log(ic, AV_LOG_VERBOSE, "parser not found for codec "
3675  "%s, packets or times may be invalid.\n",
3677  }
3678  }
3679 
3680  if (st->codecpar->codec_id != st->internal->orig_codec_id)
3682 
3684  if (ret < 0)
3685  goto find_stream_info_err;
3686  if (st->internal->request_probe <= 0)
3687  st->internal->avctx_inited = 1;
3688 
3689  codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
3690 
3691  /* Force thread count to 1 since the H.264 decoder will not extract
3692  * SPS and PPS to extradata during multi-threaded decoding. */
3693  av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
3694  /* Force lowres to 0. The decoder might reduce the video size by the
3695  * lowres factor, and we don't want that propagated to the stream's
3696  * codecpar */
3697  av_dict_set(options ? &options[i] : &thread_opt, "lowres", "0", 0);
3698 
3699  if (ic->codec_whitelist)
3700  av_dict_set(options ? &options[i] : &thread_opt, "codec_whitelist", ic->codec_whitelist, 0);
3701 
3702  /* Ensure that subtitle_header is properly set. */
3704  && codec && !avctx->codec) {
3705  if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
3706  av_log(ic, AV_LOG_WARNING,
3707  "Failed to open codec in %s\n",__FUNCTION__);
3708  }
3709 
3710  // Try to just open decoders, in case this is enough to get parameters.
3711  if (!has_codec_parameters(st, NULL) && st->internal->request_probe <= 0) {
3712  if (codec && !avctx->codec)
3713  if (avcodec_open2(avctx, codec, options ? &options[i] : &thread_opt) < 0)
3714  av_log(ic, AV_LOG_WARNING,
3715  "Failed to open codec in %s\n",__FUNCTION__);
3716  }
3717  if (!options)
3718  av_dict_free(&thread_opt);
3719  }
3720 
3721  for (i = 0; i < ic->nb_streams; i++) {
3722 #if FF_API_R_FRAME_RATE
3724 #endif
3727  }
3728 
3729  read_size = 0;
3730  for (;;) {
3731  const AVPacket *pkt;
3732  int analyzed_all_streams;
3734  ret = AVERROR_EXIT;
3735  av_log(ic, AV_LOG_DEBUG, "interrupted\n");
3736  break;
3737  }
3738 
3739  /* check if one codec still needs to be handled */
3740  for (i = 0; i < ic->nb_streams; i++) {
3741  int fps_analyze_framecount = 20;
3742  int count;
3743 
3744  st = ic->streams[i];
3745  if (!has_codec_parameters(st, NULL))
3746  break;
3747  /* If the timebase is coarse (like the usual millisecond precision
3748  * of mkv), we need to analyze more frames to reliably arrive at
3749  * the correct fps. */
3750  if (av_q2d(st->time_base) > 0.0005)
3751  fps_analyze_framecount *= 2;
3752  if (!tb_unreliable(st->internal->avctx))
3753  fps_analyze_framecount = 0;
3754  if (ic->fps_probe_size >= 0)
3755  fps_analyze_framecount = ic->fps_probe_size;
3757  fps_analyze_framecount = 0;
3758  /* variable fps and no guess at the real fps */
3759  count = (ic->iformat->flags & AVFMT_NOTIMESTAMPS) ?
3762  if (!(st->r_frame_rate.num && st->avg_frame_rate.num) &&
3764  if (count < fps_analyze_framecount)
3765  break;
3766  }
3767  // Look at the first 3 frames if there is evidence of frame delay
3768  // but the decoder delay is not set.
3769  if (st->internal->info->frame_delay_evidence && count < 2 && st->internal->avctx->has_b_frames == 0)
3770  break;
3771  if (!st->internal->avctx->extradata &&
3773  st->internal->extract_extradata.bsf) &&
3775  break;
3776  if (st->first_dts == AV_NOPTS_VALUE &&
3777  !(ic->iformat->flags & AVFMT_NOTIMESTAMPS) &&
3781  break;
3782  }
3783  analyzed_all_streams = 0;
3784  if (!missing_streams || !*missing_streams)
3785  if (i == ic->nb_streams) {
3786  analyzed_all_streams = 1;
3787  /* NOTE: If the format has no header, then we need to read some
3788  * packets to get most of the streams, so we cannot stop here. */
3789  if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
3790  /* If we found the info for all the codecs, we can stop. */
3791  ret = count;
3792  av_log(ic, AV_LOG_DEBUG, "All info found\n");
3793  flush_codecs = 0;
3794  break;
3795  }
3796  }
3797  /* We did not get all the codec info, but we read too much data. */
3798  if (read_size >= probesize) {
3799  ret = count;
3800  av_log(ic, AV_LOG_DEBUG,
3801  "Probe buffer size limit of %"PRId64" bytes reached\n", probesize);
3802  for (i = 0; i < ic->nb_streams; i++)
3803  if (!ic->streams[i]->r_frame_rate.num &&
3804  ic->streams[i]->internal->info->duration_count <= 1 &&
3806  strcmp(ic->iformat->name, "image2"))
3807  av_log(ic, AV_LOG_WARNING,
3808  "Stream #%d: not enough frames to estimate rate; "
3809  "consider increasing probesize\n", i);
3810  break;
3811  }
3812 
3813  /* NOTE: A new stream can be added there if no header in file
3814  * (AVFMTCTX_NOHEADER). */
3815  ret = read_frame_internal(ic, pkt1);
3816  if (ret == AVERROR(EAGAIN))
3817  continue;
3818 
3819  if (ret < 0) {
3820  /* EOF or error*/
3821  eof_reached = 1;
3822  break;
3823  }
3824 
3825  if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
3828  pkt1, NULL, 0);
3829  if (ret < 0)
3830  goto unref_then_goto_end;
3831 
3832  pkt = &ic->internal->packet_buffer_end->pkt;
3833  } else {
3834  pkt = pkt1;
3835  }
3836 
3837  st = ic->streams[pkt->stream_index];
3839  read_size += pkt->size;
3840 
3841  avctx = st->internal->avctx;
3842  if (!st->internal->avctx_inited) {
3844  if (ret < 0)
3845  goto unref_then_goto_end;
3846  st->internal->avctx_inited = 1;
3847  }
3848 
3849  if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
3850  /* check for non-increasing dts */
3851  if (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&
3852  st->internal->info->fps_last_dts >= pkt->dts) {
3853  av_log(ic, AV_LOG_DEBUG,
3854  "Non-increasing DTS in stream %d: packet %d with DTS "
3855  "%"PRId64", packet %d with DTS %"PRId64"\n",
3856  st->index, st->internal->info->fps_last_dts_idx,
3858  pkt->dts);
3859  st->internal->info->fps_first_dts =
3861  }
3862  /* Check for a discontinuity in dts. If the difference in dts
3863  * is more than 1000 times the average packet duration in the
3864  * sequence, we treat it as a discontinuity. */
3865  if (st->internal->info->fps_last_dts != AV_NOPTS_VALUE &&
3867  (pkt->dts - (uint64_t)st->internal->info->fps_last_dts) / 1000 >
3868  (st->internal->info->fps_last_dts - (uint64_t)st->internal->info->fps_first_dts) /
3870  av_log(ic, AV_LOG_WARNING,
3871  "DTS discontinuity in stream %d: packet %d with DTS "
3872  "%"PRId64", packet %d with DTS %"PRId64"\n",
3873  st->index, st->internal->info->fps_last_dts_idx,
3875  pkt->dts);
3876  st->internal->info->fps_first_dts =
3878  }
3879 
3880  /* update stored dts values */
3881  if (st->internal->info->fps_first_dts == AV_NOPTS_VALUE) {
3882  st->internal->info->fps_first_dts = pkt->dts;
3884  }
3885  st->internal->info->fps_last_dts = pkt->dts;
3887  }
3888  if (st->codec_info_nb_frames>1) {
3889  int64_t t = 0;
3890  int64_t limit;
3891 
3892  if (st->time_base.den > 0)
3894  if (st->avg_frame_rate.num > 0)
3896 
3897  if ( t == 0
3898  && st->codec_info_nb_frames>30
3900  && st->internal->info->fps_last_dts != AV_NOPTS_VALUE) {
3901  int64_t dur = av_sat_sub64(st->internal->info->fps_last_dts, st->internal->info->fps_first_dts);
3902  t = FFMAX(t, av_rescale_q(dur, st->time_base, AV_TIME_BASE_Q));
3903  }
3904 
3905  if (analyzed_all_streams) limit = max_analyze_duration;
3906  else if (avctx->codec_type == AVMEDIA_TYPE_SUBTITLE) limit = max_subtitle_analyze_duration;
3907  else limit = max_stream_analyze_duration;
3908 
3909  if (t >= limit) {
3910  av_log(ic, AV_LOG_VERBOSE, "max_analyze_duration %"PRId64" reached at %"PRId64" microseconds st:%d\n",
3911  limit,
3912  t, pkt->stream_index);
3913  if (ic->flags & AVFMT_FLAG_NOBUFFER)
3914  av_packet_unref(pkt1);
3915  break;
3916  }
3917  if (pkt->duration > 0) {
3919  && (uint64_t)pkt->pts - st->start_time < INT64_MAX
3920  ) {
3922  } else
3924  st->internal->info->codec_info_duration_fields += st->parser && st->need_parsing && avctx->ticks_per_frame ==2 ? st->parser->repeat_pict + 1 : 2;
3925  }
3926  }
3927  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
3928 #if FF_API_R_FRAME_RATE
3929  ff_rfps_add_frame(ic, st, pkt->dts);
3930 #endif
3931  if (pkt->dts != pkt->pts && pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
3933  }
3934  if (!st->internal->avctx->extradata) {
3935  ret = extract_extradata(st, pkt);
3936  if (ret < 0)
3937  goto unref_then_goto_end;
3938  }
3939 
3940  /* If still no information, we try to open the codec and to
3941  * decompress the frame. We try to avoid that in most cases as
3942  * it takes longer and uses more memory. For MPEG-4, we need to
3943  * decompress for QuickTime.
3944  *
3945  * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
3946  * least one frame of codec data, this makes sure the codec initializes
3947  * the channel configuration and does not only trust the values from
3948  * the container. */
3949  try_decode_frame(ic, st, pkt,
3950  (options && i < orig_nb_streams) ? &options[i] : NULL);
3951 
3952  if (ic->flags & AVFMT_FLAG_NOBUFFER)
3953  av_packet_unref(pkt1);
3954 
3955  st->codec_info_nb_frames++;
3956  count++;
3957  }
3958 
3959  if (eof_reached) {
3960  int stream_index;
3961  for (stream_index = 0; stream_index < ic->nb_streams; stream_index++) {
3962  st = ic->streams[stream_index];
3963  avctx = st->internal->avctx;
3964  if (!has_codec_parameters(st, NULL)) {
3965  const AVCodec *codec = find_probe_decoder(ic, st, st->codecpar->codec_id);
3966  if (codec && !avctx->codec) {
3967  AVDictionary *opts = NULL;
3968  if (ic->codec_whitelist)
3969  av_dict_set(&opts, "codec_whitelist", ic->codec_whitelist, 0);
3970  if (avcodec_open2(avctx, codec, (options && stream_index < orig_nb_streams) ? &options[stream_index] : &opts) < 0)
3971  av_log(ic, AV_LOG_WARNING,
3972  "Failed to open codec in %s\n",__FUNCTION__);
3973  av_dict_free(&opts);
3974  }
3975  }
3976 
3977  // EOF already reached while reading the stream above.
3978  // So continue with reoordering DTS with whatever delay we have.
3980  update_dts_from_pts(ic, stream_index, ic->internal->packet_buffer);
3981  }
3982  }
3983  }
3984 
3985  if (flush_codecs) {
3986  AVPacket *empty_pkt = ic->internal->pkt;
3987  int err = 0;
3988  av_packet_unref(empty_pkt);
3989 
3990  for (i = 0; i < ic->nb_streams; i++) {
3991 
3992  st = ic->streams[i];
3993 
3994  /* flush the decoders */
3995  if (st->internal->info->found_decoder == 1) {
3996  do {
3997  err = try_decode_frame(ic, st, empty_pkt,
3998  (options && i < orig_nb_streams)
3999  ? &options[i] : NULL);
4000  } while (err > 0 && !has_codec_parameters(st, NULL));
4001 
4002  if (err < 0) {
4003  av_log(ic, AV_LOG_INFO,
4004  "decoding for stream %d failed\n", st->index);
4005  }
4006  }
4007  }
4008  }
4009 
4010  ff_rfps_calculate(ic);
4011 
4012  for (i = 0; i < ic->nb_streams; i++) {
4013  st = ic->streams[i];
4014  avctx = st->internal->avctx;
4015  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
4016  if (avctx->codec_id == AV_CODEC_ID_RAWVIDEO && !avctx->codec_tag && !avctx->bits_per_coded_sample) {
4017  uint32_t tag= avcodec_pix_fmt_to_codec_tag(avctx->pix_fmt);
4019  avctx->codec_tag= tag;
4020  }
4021 
4022  /* estimate average framerate if not set by demuxer */
4024  !st->avg_frame_rate.num &&
4026  int best_fps = 0;
4027  double best_error = 0.01;
4028  AVRational codec_frame_rate = avctx->framerate;
4029 
4030  if (st->internal->info->codec_info_duration >= INT64_MAX / st->time_base.num / 2||
4031  st->internal->info->codec_info_duration_fields >= INT64_MAX / st->time_base.den ||
4032  st->internal->info->codec_info_duration < 0)
4033  continue;
4035  st->internal->info->codec_info_duration_fields * (int64_t) st->time_base.den,
4036  st->internal->info->codec_info_duration * 2 * (int64_t) st->time_base.num, 60000);
4037 
4038  /* Round guessed framerate to a "standard" framerate if it's
4039  * within 1% of the original estimate. */
4040  for (j = 0; j < MAX_STD_TIMEBASES; j++) {
4041  AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
4042  double error = fabs(av_q2d(st->avg_frame_rate) /
4043  av_q2d(std_fps) - 1);
4044 
4045  if (error < best_error) {
4046  best_error = error;
4047  best_fps = std_fps.num;
4048  }
4049 
4050  if (ic->internal->prefer_codec_framerate && codec_frame_rate.num > 0 && codec_frame_rate.den > 0) {
4051  error = fabs(av_q2d(codec_frame_rate) /
4052  av_q2d(std_fps) - 1);
4053  if (error < best_error) {
4054  best_error = error;
4055  best_fps = std_fps.num;
4056  }
4057  }
4058  }
4059  if (best_fps)
4061  best_fps, 12 * 1001, INT_MAX);
4062  }
4063 
4064  if (!st->r_frame_rate.num) {
4065  if ( avctx->time_base.den * (int64_t) st->time_base.num
4066  <= avctx->time_base.num * (uint64_t)avctx->ticks_per_frame * st->time_base.den) {
4068  avctx->time_base.den, (int64_t)avctx->time_base.num * avctx->ticks_per_frame, INT_MAX);
4069  } else {
4070  st->r_frame_rate.num = st->time_base.den;
4071  st->r_frame_rate.den = st->time_base.num;
4072  }
4073  }
4075  AVRational hw_ratio = { avctx->height, avctx->width };
4077  hw_ratio);
4078  }
4079  } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
4080  if (!avctx->bits_per_coded_sample)
4081  avctx->bits_per_coded_sample =
4083  // set stream disposition based on audio service type
4084  switch (avctx->audio_service_type) {
4087  break;
4090  break;
4093  break;
4096  break;
4099  break;
4100  }
4101  }
4102  }
4103 
4104  if (probesize)
4105  estimate_timings(ic, old_offset);
4106 
4107  av_opt_set(ic, "skip_clear", "0", AV_OPT_SEARCH_CHILDREN);
4108 
4109  if (ret >= 0 && ic->nb_streams)
4110  /* We could not have all the codec parameters before EOF. */
4111  ret = -1;
4112  for (i = 0; i < ic->nb_streams; i++) {
4113  const char *errmsg;
4114  st = ic->streams[i];
4115 
4116  /* if no packet was ever seen, update context now for has_codec_parameters */
4117  if (!st->internal->avctx_inited) {
4118  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
4120  st->codecpar->format = st->internal->avctx->sample_fmt;
4122  if (ret < 0)
4123  goto find_stream_info_err;
4124  }
4125  if (!has_codec_parameters(st, &errmsg)) {
4126  char buf[256];
4127  avcodec_string(buf, sizeof(buf), st->internal->avctx, 0);
4128  av_log(ic, AV_LOG_WARNING,
4129  "Could not find codec parameters for stream %d (%s): %s\n"
4130  "Consider increasing the value for the 'analyzeduration' (%"PRId64") and 'probesize' (%"PRId64") options\n",
4131  i, buf, errmsg, ic->max_analyze_duration, ic->probesize);
4132  } else {
4133  ret = 0;
4134  }
4135  }
4136 
4137  ret = compute_chapters_end(ic);
4138  if (ret < 0)
4139  goto find_stream_info_err;
4140 
4141  /* update the stream parameters from the internal codec contexts */
4142  for (i = 0; i < ic->nb_streams; i++) {
4143  st = ic->streams[i];
4144 
4145  if (st->internal->avctx_inited) {
4147  if (ret < 0)
4148  goto find_stream_info_err;
4149  ret = add_coded_side_data(st, st->internal->avctx);
4150  if (ret < 0)
4151  goto find_stream_info_err;
4152  }
4153 
4154 #if FF_API_LAVF_AVCTX
4156  ret = avcodec_parameters_to_context(st->codec, st->codecpar);
4157  if (ret < 0)
4158  goto find_stream_info_err;
4159 
4160  // The old API (AVStream.codec) "requires" the resolution to be adjusted
4161  // by the lowres factor.
4162  if (st->internal->avctx->lowres && st->internal->avctx->width) {
4163  st->codec->lowres = st->internal->avctx->lowres;
4164  st->codec->width = st->internal->avctx->width;
4165  st->codec->height = st->internal->avctx->height;
4166  }
4167 
4168  if (st->codec->codec_tag != MKTAG('t','m','c','d')) {
4169  st->codec->time_base = st->internal->avctx->time_base;
4170  st->codec->ticks_per_frame = st->internal->avctx->ticks_per_frame;
4171  }
4172  st->codec->framerate = st->avg_frame_rate;
4173 
4174  if (st->internal->avctx->subtitle_header) {
4175  st->codec->subtitle_header = av_malloc(st->internal->avctx->subtitle_header_size);
4176  if (!st->codec->subtitle_header)
4177  goto find_stream_info_err;
4178  st->codec->subtitle_header_size = st->internal->avctx->subtitle_header_size;
4179  memcpy(st->codec->subtitle_header, st->internal->avctx->subtitle_header,
4180  st->codec->subtitle_header_size);
4181  }
4182 
4183  // Fields unavailable in AVCodecParameters
4184  st->codec->coded_width = st->internal->avctx->coded_width;
4185  st->codec->coded_height = st->internal->avctx->coded_height;
4186  st->codec->properties = st->internal->avctx->properties;
4188 #endif
4189 
4190  st->internal->avctx_inited = 0;
4191  }
4192 
4193 find_stream_info_err:
4194  for (i = 0; i < ic->nb_streams; i++) {
4195  st = ic->streams[i];
4196  if (st->internal->info)
4199  av_freep(&ic->streams[i]->internal->info);
4202  }
4203  if (ic->pb)
4204  av_log(ic, AV_LOG_DEBUG, "After avformat_find_stream_info() pos: %"PRId64" bytes read:%"PRId64" seeks:%d frames:%d\n",
4205  avio_tell(ic->pb), ic->pb->bytes_read, ic->pb->seek_count, count);
4206  return ret;
4207 
4208 unref_then_goto_end:
4209  av_packet_unref(pkt1);
4210  goto find_stream_info_err;
4211 }
4212 
4214 {
4215  int i, j;
4216 
4217  for (i = 0; i < ic->nb_programs; i++) {
4218  if (ic->programs[i] == last) {
4219  last = NULL;
4220  } else {
4221  if (!last)
4222  for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
4223  if (ic->programs[i]->stream_index[j] == s)
4224  return ic->programs[i];
4225  }
4226  }
4227  return NULL;
4228 }
4229 
4231  int wanted_stream_nb, int related_stream,
4232  AVCodec **decoder_ret, int flags)
4233 {
4234  int i, nb_streams = ic->nb_streams;
4236  int best_count = -1, best_multiframe = -1, best_disposition = -1;
4237  int count, multiframe, disposition;
4238  int64_t best_bitrate = -1;
4239  int64_t bitrate;
4240  unsigned *program = NULL;
4241  const AVCodec *decoder = NULL, *best_decoder = NULL;
4242 
4243  if (related_stream >= 0 && wanted_stream_nb < 0) {
4244  AVProgram *p = av_find_program_from_stream(ic, NULL, related_stream);
4245  if (p) {
4246  program = p->stream_index;
4248  }
4249  }
4250  for (i = 0; i < nb_streams; i++) {
4251  int real_stream_index = program ? program[i] : i;
4252  AVStream *st = ic->streams[real_stream_index];
4253  AVCodecParameters *par = st->codecpar;
4254  if (par->codec_type != type)
4255  continue;
4256  if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
4257  continue;
4258  if (type == AVMEDIA_TYPE_AUDIO && !(par->channels && par->sample_rate))
4259  continue;
4260  if (decoder_ret) {
4261  decoder = find_decoder(ic, st, par->codec_id);
4262  if (!decoder) {
4263  if (ret < 0)
4265  continue;
4266  }
4267  }
4269  + !! (st->disposition & AV_DISPOSITION_DEFAULT);
4270  count = st->codec_info_nb_frames;
4271  bitrate = par->bit_rate;
4272  multiframe = FFMIN(5, count);
4273  if ((best_disposition > disposition) ||
4274  (best_disposition == disposition && best_multiframe > multiframe) ||
4275  (best_disposition == disposition && best_multiframe == multiframe && best_bitrate > bitrate) ||
4276  (best_disposition == disposition && best_multiframe == multiframe && best_bitrate == bitrate && best_count >= count))
4277  continue;
4278  best_disposition = disposition;
4279  best_count = count;
4280  best_bitrate = bitrate;
4281  best_multiframe = multiframe;
4282  ret = real_stream_index;
4283  best_decoder = decoder;
4284  if (program && i == nb_streams - 1 && ret < 0) {
4285  program = NULL;
4286  nb_streams = ic->nb_streams;
4287  /* no related stream found, try again with everything */
4288  i = 0;
4289  }
4290  }
4291  if (decoder_ret)
4292  *decoder_ret = (AVCodec*)best_decoder;
4293  return ret;
4294 }
4295 
4296 /*******************************************************/
4297 
4299 {
4300  if (s->iformat->read_play)
4301  return s->iformat->read_play(s);
4302  if (s->pb)
4303  return avio_pause(s->pb, 0);
4304  return AVERROR(ENOSYS);
4305 }
4306 
4308 {
4309  if (s->iformat->read_pause)
4310  return s->iformat->read_pause(s);
4311  if (s->pb)
4312  return avio_pause(s->pb, 1);
4313  return AVERROR(ENOSYS);
4314 }
4315 
4317 {
4318  int ret, i;
4319 
4320  dst->id = src->id;
4321  dst->time_base = src->time_base;
4322  dst->nb_frames = src->nb_frames;
4323  dst->disposition = src->disposition;
4324  dst->sample_aspect_ratio = src->sample_aspect_ratio;
4325  dst->avg_frame_rate = src->avg_frame_rate;
4326  dst->r_frame_rate = src->r_frame_rate;
4327 
4328  av_dict_free(&dst->metadata);
4329  ret = av_dict_copy(&dst->metadata, src->metadata, 0);
4330  if (ret < 0)
4331  return ret;
4332 
4333  ret = avcodec_parameters_copy(dst->codecpar, src->codecpar);
4334  if (ret < 0)
4335  return ret;
4336 
4337  /* Free existing side data*/
4338  for (i = 0; i < dst->nb_side_data; i++)
4339  av_free(dst->side_data[i].data);
4340  av_freep(&dst->side_data);
4341  dst->nb_side_data = 0;
4342 
4343  /* Copy side data if present */
4344  if (src->nb_side_data) {
4345  dst->side_data = av_mallocz_array(src->nb_side_data,
4346  sizeof(AVPacketSideData));
4347  if (!dst->side_data)
4348  return AVERROR(ENOMEM);
4349  dst->nb_side_data = src->nb_side_data;
4350 
4351  for (i = 0; i < src->nb_side_data; i++) {
4352  uint8_t *data = av_memdup(src->side_data[i].data,
4353  src->side_data[i].size);
4354  if (!data)
4355  return AVERROR(ENOMEM);
4356  dst->side_data[i].type = src->side_data[i].type;
4357  dst->side_data[i].size = src->side_data[i].size;
4358  dst->side_data[i].data = data;
4359  }
4360  }
4361 
4362 #if FF_API_LAVF_FFSERVER
4364  av_freep(&dst->recommended_encoder_configuration);
4365  if (src->recommended_encoder_configuration) {
4366  const char *conf_str = src->recommended_encoder_configuration;
4367  dst->recommended_encoder_configuration = av_strdup(conf_str);
4368  if (!dst->recommended_encoder_configuration)
4369  return AVERROR(ENOMEM);
4370  }
4372 #endif
4373 
4374  return 0;
4375 }
4376 
4377 static void free_stream(AVStream **pst)
4378 {
4379  AVStream *st = *pst;
4380  int i;
4381 
4382  if (!st)
4383  return;
4384 
4385  for (i = 0; i < st->nb_side_data; i++)
4386  av_freep(&st->side_data[i].data);
4387  av_freep(&st->side_data);
4388 
4389  if (st->parser)
4390  av_parser_close(st->parser);
4391 
4392  if (st->attached_pic.data)
4394 
4395  if (st->internal) {
4397  av_bsf_free(&st->internal->bsfc);
4398  av_freep(&st->internal->priv_pts);
4399  av_freep(&st->index_entries);
4401 
4404 
4405  if (st->internal->info)
4407  av_freep(&st->internal->info);
4408  }
4409  av_freep(&st->internal);
4410 
4411  av_dict_free(&st->metadata);
4413 #if FF_API_LAVF_AVCTX
4415  avcodec_free_context(&st->codec);
4417 #endif
4418  av_freep(&st->priv_data);
4419 #if FF_API_LAVF_FFSERVER
4421  av_freep(&st->recommended_encoder_configuration);
4423 #endif
4424 
4425  av_freep(pst);
4426 }
4427 
4429 {
4430  av_assert0(s->nb_streams>0);
4431  av_assert0(s->streams[ s->nb_streams - 1 ] == st);
4432 
4433  free_stream(&s->streams[ --s->nb_streams ]);
4434 }
4435 
4437 {
4438  int i;
4439 
4440  if (!s)
4441  return;
4442 
4443  if (s->oformat && s->oformat->deinit && s->internal->initialized)
4444  s->oformat->deinit(s);
4445 
4446  av_opt_free(s);
4447  if (s->iformat && s->iformat->priv_class && s->priv_data)
4448  av_opt_free(s->priv_data);
4449  if (s->oformat && s->oformat->priv_class && s->priv_data)
4450  av_opt_free(s->priv_data);
4451 
4452  for (i = 0; i < s->nb_streams; i++)
4453  free_stream(&s->streams[i]);
4454  s->nb_streams = 0;
4455 
4456  for (i = 0; i < s->nb_programs; i++) {
4457  av_dict_free(&s->programs[i]->metadata);
4458  av_freep(&s->programs[i]->stream_index);
4459  av_freep(&s->programs[i]);
4460  }
4461  s->nb_programs = 0;
4462 
4463  av_freep(&s->programs);
4464  av_freep(&s->priv_data);
4465  while (s->nb_chapters--) {
4466  av_dict_free(&s->chapters[s->nb_chapters]->metadata);
4467  av_freep(&s->chapters[s->nb_chapters]);
4468  }
4469  av_freep(&s->chapters);
4470  av_dict_free(&s->metadata);
4471  av_dict_free(&s->internal->id3v2_meta);
4472  av_packet_free(&s->internal->pkt);
4473  av_packet_free(&s->internal->parse_pkt);
4474  av_freep(&s->streams);
4476  av_freep(&s->internal);
4477  av_freep(&s->url);
4478  av_free(s);
4479 }
4480 
4482 {
4483  AVFormatContext *s;
4484  AVIOContext *pb;
4485 
4486  if (!ps || !*ps)
4487  return;
4488 
4489  s = *ps;
4490  pb = s->pb;
4491 
4492  if ((s->iformat && strcmp(s->iformat->name, "image2") && s->iformat->flags & AVFMT_NOFILE) ||
4493  (s->flags & AVFMT_FLAG_CUSTOM_IO))
4494  pb = NULL;
4495 
4497 
4498  if (s->iformat)
4499  if (s->iformat->read_close)
4500  s->iformat->read_close(s);
4501 
4503 
4504  *ps = NULL;
4505 
4506  avio_close(pb);
4507 }
4508 
4510 {
4511  AVStream *st;
4512  int i;
4513  AVStream **streams;
4514 
4515  if (s->nb_streams >= FFMIN(s->max_streams, INT_MAX/sizeof(*streams))) {
4516  if (s->max_streams < INT_MAX/sizeof(*streams))
4517  av_log(s, AV_LOG_ERROR, "Number of streams exceeds max_streams parameter (%d), see the documentation if you wish to increase it\n", s->max_streams);
4518  return NULL;
4519  }
4520  streams = av_realloc_array(s->streams, s->nb_streams + 1, sizeof(*streams));
4521  if (!streams)
4522  return NULL;
4523  s->streams = streams;
4524 
4525  st = av_mallocz(sizeof(AVStream));
4526  if (!st)
4527  return NULL;
4528 
4529 #if FF_API_LAVF_AVCTX
4531  st->codec = avcodec_alloc_context3(c);
4532  if (!st->codec) {
4533  av_free(st);
4534  return NULL;
4535  }
4537 #endif
4538 
4539  st->internal = av_mallocz(sizeof(*st->internal));
4540  if (!st->internal)
4541  goto fail;
4542 
4543  st->internal->info = av_mallocz(sizeof(*st->internal->info));
4544  if (!st->internal->info)
4545  goto fail;
4547 
4549  if (!st->codecpar)
4550  goto fail;
4551 
4553  if (!st->internal->avctx)
4554  goto fail;
4555 
4556  if (s->iformat) {
4557 #if FF_API_LAVF_AVCTX
4559  /* no default bitrate if decoding */
4560  st->codec->bit_rate = 0;
4562 #endif
4563 
4564  /* default pts setting is MPEG-like */
4565  avpriv_set_pts_info(st, 33, 1, 90000);
4566  /* we set the current DTS to 0 so that formats without any timestamps
4567  * but durations get some timestamps, formats with some unknown
4568  * timestamps have their first few packets buffered and the
4569  * timestamps corrected before they are returned to the user */
4570  st->cur_dts = RELATIVE_TS_BASE;
4571  } else {
4572  st->cur_dts = AV_NOPTS_VALUE;
4573  }
4574 
4575  st->index = s->nb_streams;
4576  st->start_time = AV_NOPTS_VALUE;
4577  st->duration = AV_NOPTS_VALUE;
4578  st->first_dts = AV_NOPTS_VALUE;
4579  st->probe_packets = s->max_probe_packets;
4582 
4585  for (i = 0; i < MAX_REORDER_DELAY + 1; i++)
4587 
4588  st->sample_aspect_ratio = (AVRational) { 0, 1 };
4589 
4590 #if FF_API_R_FRAME_RATE
4592 #endif
4595 
4596  st->internal->inject_global_side_data = s->internal->inject_global_side_data;
4597 
4598  st->internal->need_context_update = 1;
4599 
4600  s->streams[s->nb_streams++] = st;
4601  return st;
4602 fail:
4603  free_stream(&st);
4604  return NULL;
4605 }
4606 
4608 {
4609  AVProgram *program = NULL;
4610  int i, ret;
4611 
4612  av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
4613 
4614  for (i = 0; i < ac->nb_programs; i++)
4615  if (ac->programs[i]->id == id)
4616  program = ac->programs[i];
4617 
4618  if (!program) {
4619  program = av_mallocz(sizeof(AVProgram));
4620  if (!program)
4621  return NULL;
4623  if (ret < 0) {
4624  av_free(program);
4625  return NULL;
4626  }
4627  program->discard = AVDISCARD_NONE;
4628  program->pmt_version = -1;
4629  program->id = id;
4630  program->pts_wrap_reference = AV_NOPTS_VALUE;
4631  program->pts_wrap_behavior = AV_PTS_WRAP_IGNORE;
4632  program->start_time =
4633  program->end_time = AV_NOPTS_VALUE;
4634  }
4635  return program;
4636 }
4637 
4638 #if FF_API_CHAPTER_ID_INT
4640 #else
4642 #endif
4643  int64_t start, int64_t end, const char *title)
4644 {
4645  AVChapter *chapter = NULL;
4646  int i, ret;
4647 
4648  if (end != AV_NOPTS_VALUE && start > end) {
4649  av_log(s, AV_LOG_ERROR, "Chapter end time %"PRId64" before start %"PRId64"\n", end, start);
4650  return NULL;
4651  }
4652 
4653  if (!s->nb_chapters) {
4654  s->internal->chapter_ids_monotonic = 1;
4655  } else if (!s->internal->chapter_ids_monotonic || s->chapters[s->nb_chapters-1]->id >= id) {
4656  s->internal->chapter_ids_monotonic = 0;
4657  for (i = 0; i < s->nb_chapters; i++)
4658  if (s->chapters[i]->id == id)
4659  chapter = s->chapters[i];
4660  }
4661 
4662  if (!chapter) {
4663  chapter = av_mallocz(sizeof(AVChapter));
4664  if (!chapter)
4665  return NULL;
4666  ret = av_dynarray_add_nofree(&s->chapters, &s->nb_chapters, chapter);
4667  if (ret < 0) {
4668  av_free(chapter);
4669  return NULL;
4670  }
4671  }
4672  av_dict_set(&chapter->metadata, "title", title, 0);
4673  chapter->id = id;
4674  chapter->time_base = time_base;
4675  chapter->start = start;
4676  chapter->end = end;
4677 
4678  return chapter;
4679 }
4680 
4681 void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
4682 {
4683  int i, j;
4684  AVProgram *program = NULL;
4685  void *tmp;
4686 
4687  if (idx >= ac->nb_streams) {
4688  av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
4689  return;
4690  }
4691 
4692  for (i = 0; i < ac->nb_programs; i++) {
4693  if (ac->programs[i]->id != progid)
4694  continue;
4695  program = ac->programs[i];
4696  for (j = 0; j < program->nb_stream_indexes; j++)
4697  if (program->stream_index[j] == idx)
4698  return;
4699 
4700  tmp = av_realloc_array(program->stream_index, program->nb_stream_indexes+1, sizeof(unsigned int));
4701  if (!tmp)
4702  return;
4703  program->stream_index = tmp;
4704  program->stream_index[program->nb_stream_indexes++] = idx;
4705  return;
4706  }
4707 }
4708 
4709 uint64_t ff_ntp_time(void)
4710 {
4711  return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
4712 }
4713 
4714 uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
4715 {
4716  uint64_t ntp_ts, frac_part, sec;
4717  uint32_t usec;
4718 
4719  //current ntp time in seconds and micro seconds
4720  sec = ntp_time_us / 1000000;
4721  usec = ntp_time_us % 1000000;
4722 
4723  //encoding in ntp timestamp format
4724  frac_part = usec * 0xFFFFFFFFULL;
4725  frac_part /= 1000000;
4726 
4727  if (sec > 0xFFFFFFFFULL)
4728  av_log(NULL, AV_LOG_WARNING, "NTP time format roll over detected\n");
4729 
4730  ntp_ts = sec << 32;
4731  ntp_ts |= frac_part;
4732 
4733  return ntp_ts;
4734 }
4735 
4736 int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
4737 {
4738  const char *p;
4739  char *q, buf1[20], c;
4740  int nd, len, percentd_found;
4741 
4742  q = buf;
4743  p = path;
4744  percentd_found = 0;
4745  for (;;) {
4746  c = *p++;
4747  if (c == '\0')
4748  break;
4749  if (c == '%') {
4750  do {
4751  nd = 0;
4752  while (av_isdigit(*p)) {
4753  if (nd >= INT_MAX / 10 - 255)
4754  goto fail;
4755  nd = nd * 10 + *p++ - '0';
4756  }
4757  c = *p++;
4758  } while (av_isdigit(c));
4759 
4760  switch (c) {
4761  case '%':
4762  goto addchar;
4763  case 'd':
4764  if (!(flags & AV_FRAME_FILENAME_FLAGS_MULTIPLE) && percentd_found)
4765  goto fail;
4766  percentd_found = 1;
4767  if (number < 0)
4768  nd += 1;
4769  snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
4770  len = strlen(buf1);
4771  if ((q - buf + len) > buf_size - 1)
4772  goto fail;
4773  memcpy(q, buf1, len);
4774  q += len;
4775  break;
4776  default:
4777  goto fail;
4778  }
4779  } else {
4780 addchar:
4781  if ((q - buf) < buf_size - 1)
4782  *q++ = c;
4783  }
4784  }
4785  if (!percentd_found)
4786  goto fail;
4787  *q = '\0';
4788  return 0;
4789 fail:
4790  *q = '\0';
4791  return -1;
4792 }
4793 
4794 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
4795 {
4796  return av_get_frame_filename2(buf, buf_size, path, number, 0);
4797 }
4798 
4799 void av_url_split(char *proto, int proto_size,
4800  char *authorization, int authorization_size,
4801  char *hostname, int hostname_size,
4802  int *port_ptr, char *path, int path_size, const char *url)
4803 {
4804  const char *p, *ls, *at, *at2, *col, *brk;
4805 
4806  if (port_ptr)
4807  *port_ptr = -1;
4808  if (proto_size > 0)
4809  proto[0] = 0;
4810  if (authorization_size > 0)
4811  authorization[0] = 0;
4812  if (hostname_size > 0)
4813  hostname[0] = 0;
4814  if (path_size > 0)
4815  path[0] = 0;
4816 
4817  /* parse protocol */
4818  if ((p = strchr(url, ':'))) {
4819  av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
4820  p++; /* skip ':' */
4821  if (*p == '/')
4822  p++;
4823  if (*p == '/')
4824  p++;
4825  } else {
4826  /* no protocol means plain filename */
4827  av_strlcpy(path, url, path_size);
4828  return;
4829  }
4830 
4831  /* separate path from hostname */
4832  ls = p + strcspn(p, "/?#");
4833  av_strlcpy(path, ls, path_size);
4834 
4835  /* the rest is hostname, use that to parse auth/port */
4836  if (ls != p) {
4837  /* authorization (user[:pass]@hostname) */
4838  at2 = p;
4839  while ((at = strchr(p, '@')) && at < ls) {
4840  av_strlcpy(authorization, at2,
4841  FFMIN(authorization_size, at + 1 - at2));
4842  p = at + 1; /* skip '@' */
4843  }
4844 
4845  if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
4846  /* [host]:port */
4847  av_strlcpy(hostname, p + 1,
4848  FFMIN(hostname_size, brk - p));
4849  if (brk[1] == ':' && port_ptr)
4850  *port_ptr = atoi(brk + 2);
4851  } else if ((col = strchr(p, ':')) && col < ls) {
4852  av_strlcpy(hostname, p,
4853  FFMIN(col + 1 - p, hostname_size));
4854  if (port_ptr)
4855  *port_ptr = atoi(col + 1);
4856  } else
4857  av_strlcpy(hostname, p,
4858  FFMIN(ls + 1 - p, hostname_size));
4859  }
4860 }
4861 
4862 int ff_mkdir_p(const char *path)
4863 {
4864  int ret = 0;
4865  char *temp = av_strdup(path);
4866  char *pos = temp;
4867  char tmp_ch = '\0';
4868 
4869  if (!path || !temp) {
4870  return -1;
4871  }
4872 
4873  if (!av_strncasecmp(temp, "/", 1) || !av_strncasecmp(temp, "\\", 1)) {
4874  pos++;
4875  } else if (!av_strncasecmp(temp, "./", 2) || !av_strncasecmp(temp, ".\\", 2)) {
4876  pos += 2;
4877  }
4878 
4879  for ( ; *pos != '\0'; ++pos) {
4880  if (*pos == '/' || *pos == '\\') {
4881  tmp_ch = *pos;
4882  *pos = '\0';
4883  ret = mkdir(temp, 0755);
4884  *pos = tmp_ch;
4885  }
4886  }
4887 
4888  if ((*(pos - 1) != '/') || (*(pos - 1) != '\\')) {
4889  ret = mkdir(temp, 0755);
4890  }
4891 
4892  av_free(temp);
4893  return ret;
4894 }
4895 
4896 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
4897 {
4898  int i;
4899  static const char hex_table_uc[16] = { '0', '1', '2', '3',
4900  '4', '5', '6', '7',
4901  '8', '9', 'A', 'B',
4902  'C', 'D', 'E', 'F' };
4903  static const char hex_table_lc[16] = { '0', '1', '2', '3',
4904  '4', '5', '6', '7',
4905  '8', '9', 'a', 'b',
4906  'c', 'd', 'e', 'f' };
4907  const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
4908 
4909  for (i = 0; i < s; i++) {
4910  buff[i * 2] = hex_table[src[i] >> 4];
4911  buff[i * 2 + 1] = hex_table[src[i] & 0xF];
4912  }
4913 
4914  return buff;
4915 }
4916 
4917 int ff_hex_to_data(uint8_t *data, const char *p)
4918 {
4919  int c, len, v;
4920 
4921  len = 0;
4922  v = 1;
4923  for (;;) {
4924  p += strspn(p, SPACE_CHARS);
4925  if (*p == '\0')
4926  break;
4927  c = av_toupper((unsigned char) *p++);
4928  if (c >= '0' && c <= '9')
4929  c = c - '0';
4930  else if (c >= 'A' && c <= 'F')
4931  c = c - 'A' + 10;
4932  else
4933  break;
4934  v = (v << 4) | c;
4935  if (v & 0x100) {
4936  if (data)
4937  data[len] = v;
4938  len++;
4939  v = 1;
4940  }
4941  }
4942  return len;
4943 }
4944 
4945 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
4946  unsigned int pts_num, unsigned int pts_den)
4947 {
4948  AVRational new_tb;
4949  if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
4950  if (new_tb.num != pts_num)
4952  "st:%d removing common factor %d from timebase\n",
4953  s->index, pts_num / new_tb.num);
4954  } else
4956  "st:%d has too large timebase, reducing\n", s->index);
4957 
4958  if (new_tb.num <= 0 || new_tb.den <= 0) {
4960  "Ignoring attempt to set invalid timebase %d/%d for st:%d\n",
4961  new_tb.num, new_tb.den,
4962  s->index);
4963  return;
4964  }
4965  s->time_base = new_tb;
4966 #if FF_API_LAVF_AVCTX
4968  s->codec->pkt_timebase = new_tb;
4970 #endif
4971  s->internal->avctx->pkt_timebase = new_tb;
4972  s->pts_wrap_bits = pts_wrap_bits;
4973 }
4974 
4975 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
4976  void *context)
4977 {
4978  const char *ptr = str;
4979 
4980  /* Parse key=value pairs. */
4981  for (;;) {
4982  const char *key;
4983  char *dest = NULL, *dest_end;
4984  int key_len, dest_len = 0;
4985 
4986  /* Skip whitespace and potential commas. */
4987  while (*ptr && (av_isspace(*ptr) || *ptr == ','))
4988  ptr++;
4989  if (!*ptr)
4990  break;
4991 
4992  key = ptr;
4993 
4994  if (!(ptr = strchr(key, '=')))
4995  break;
4996  ptr++;
4997  key_len = ptr - key;
4998 
4999  callback_get_buf(context, key, key_len, &dest, &dest_len);
5000  dest_end = dest ? dest + dest_len - 1 : NULL;
5001 
5002  if (*ptr == '\"') {
5003  ptr++;
5004  while (*ptr && *ptr != '\"') {
5005  if (*ptr == '\\') {
5006  if (!ptr[1])
5007  break;
5008  if (dest && dest < dest_end)
5009  *dest++ = ptr[1];
5010  ptr += 2;
5011  } else {
5012  if (dest && dest < dest_end)
5013  *dest++ = *ptr;
5014  ptr++;
5015  }
5016  }
5017  if (*ptr == '\"')
5018  ptr++;
5019  } else {
5020  for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
5021  if (dest && dest < dest_end)
5022  *dest++ = *ptr;
5023  }
5024  if (dest)
5025  *dest = 0;
5026  }
5027 }
5028 
5030 {
5031  int i;
5032  for (i = 0; i < s->nb_streams; i++)
5033  if (s->streams[i]->id == id)
5034  return i;
5035  return -1;
5036 }
5037 
5039  int std_compliance)
5040 {
5041  if (ofmt) {
5042  unsigned int codec_tag;
5043  if (ofmt->query_codec)
5044  return ofmt->query_codec(codec_id, std_compliance);
5045  else if (ofmt->codec_tag)
5046  return !!av_codec_get_tag2(ofmt->codec_tag, codec_id, &codec_tag);
5047  else if (codec_id == ofmt->video_codec ||
5048  codec_id == ofmt->audio_codec ||
5049  codec_id == ofmt->subtitle_codec ||
5050  codec_id == ofmt->data_codec)
5051  return 1;
5052  }
5053  return AVERROR_PATCHWELCOME;
5054 }
5055 
5057 {
5058 #if CONFIG_NETWORK
5059  int ret;
5060  if ((ret = ff_network_init()) < 0)
5061  return ret;
5062  if ((ret = ff_tls_init()) < 0)
5063  return ret;
5064 #endif
5065  return 0;
5066 }
5067 
5069 {
5070 #if CONFIG_NETWORK
5071  ff_network_close();
5072  ff_tls_deinit();
5073 #endif
5074  return 0;
5075 }
5076 
5078  uint64_t channel_layout, int32_t sample_rate,
5080 {
5081  uint32_t flags = 0;
5082  int size = 4;
5083  uint8_t *data;
5084  if (!pkt)
5085  return AVERROR(EINVAL);
5086  if (channels) {
5087  size += 4;
5089  }
5090  if (channel_layout) {
5091  size += 8;
5093  }
5094  if (sample_rate) {
5095  size += 4;
5097  }
5098  if (width || height) {
5099  size += 8;
5101  }
5103  if (!data)
5104  return AVERROR(ENOMEM);
5105  bytestream_put_le32(&data, flags);
5106  if (channels)
5107  bytestream_put_le32(&data, channels);
5108  if (channel_layout)
5109  bytestream_put_le64(&data, channel_layout);
5110  if (sample_rate)
5111  bytestream_put_le32(&data, sample_rate);
5112  if (width || height) {
5113  bytestream_put_le32(&data, width);
5114  bytestream_put_le32(&data, height);
5115  }
5116  return 0;
5117 }
5118 
5120 {
5121  AVRational undef = {0, 1};
5122  AVRational stream_sample_aspect_ratio = stream ? stream->sample_aspect_ratio : undef;
5123  AVRational codec_sample_aspect_ratio = stream && stream->codecpar ? stream->codecpar->sample_aspect_ratio : undef;
5124  AVRational frame_sample_aspect_ratio = frame ? frame->sample_aspect_ratio : codec_sample_aspect_ratio;
5125 
5126  av_reduce(&stream_sample_aspect_ratio.num, &stream_sample_aspect_ratio.den,
5127  stream_sample_aspect_ratio.num, stream_sample_aspect_ratio.den, INT_MAX);
5128  if (stream_sample_aspect_ratio.num <= 0 || stream_sample_aspect_ratio.den <= 0)
5129  stream_sample_aspect_ratio = undef;
5130 
5131  av_reduce(&frame_sample_aspect_ratio.num, &frame_sample_aspect_ratio.den,
5132  frame_sample_aspect_ratio.num, frame_sample_aspect_ratio.den, INT_MAX);
5133  if (frame_sample_aspect_ratio.num <= 0 || frame_sample_aspect_ratio.den <= 0)
5134  frame_sample_aspect_ratio = undef;
5135 
5136  if (stream_sample_aspect_ratio.num)
5137  return stream_sample_aspect_ratio;
5138  else
5139  return frame_sample_aspect_ratio;
5140 }
5141 
5143 {
5144  AVRational fr = st->r_frame_rate;
5145  AVRational codec_fr = st->internal->avctx->framerate;
5146  AVRational avg_fr = st->avg_frame_rate;
5147 
5148  if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
5149  av_q2d(avg_fr) < 70 && av_q2d(fr) > 210) {
5150  fr = avg_fr;
5151  }
5152 
5153 
5154  if (st->internal->avctx->ticks_per_frame > 1) {
5155  if ( codec_fr.num > 0 && codec_fr.den > 0 &&
5156  (fr.num == 0 || av_q2d(codec_fr) < av_q2d(fr)*0.7 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1))
5157  fr = codec_fr;
5158  }
5159 
5160  return fr;
5161 }
5162 
5163 /**
5164  * Matches a stream specifier (but ignores requested index).
5165  *
5166  * @param indexptr set to point to the requested stream index if there is one
5167  *
5168  * @return <0 on error
5169  * 0 if st is NOT a matching stream
5170  * >0 if st is a matching stream
5171  */
5173  const char *spec, const char **indexptr, AVProgram **p)
5174 {
5175  int match = 1; /* Stores if the specifier matches so far. */
5176  while (*spec) {
5177  if (*spec <= '9' && *spec >= '0') { /* opt:index */
5178  if (indexptr)
5179  *indexptr = spec;
5180  return match;
5181  } else if (*spec == 'v' || *spec == 'a' || *spec == 's' || *spec == 'd' ||
5182  *spec == 't' || *spec == 'V') { /* opt:[vasdtV] */
5183  enum AVMediaType type;
5184  int nopic = 0;
5185 
5186  switch (*spec++) {
5187  case 'v': type = AVMEDIA_TYPE_VIDEO; break;
5188  case 'a': type = AVMEDIA_TYPE_AUDIO; break;
5189  case 's': type = AVMEDIA_TYPE_SUBTITLE; break;
5190  case 'd': type = AVMEDIA_TYPE_DATA; break;
5191  case 't': type = AVMEDIA_TYPE_ATTACHMENT; break;
5192  case 'V': type = AVMEDIA_TYPE_VIDEO; nopic = 1; break;
5193  default: av_assert0(0);
5194  }
5195  if (*spec && *spec++ != ':') /* If we are not at the end, then another specifier must follow. */
5196  return AVERROR(EINVAL);
5197 
5198 #if FF_API_LAVF_AVCTX
5200  if (type != st->codecpar->codec_type
5201  && (st->codecpar->codec_type != AVMEDIA_TYPE_UNKNOWN || st->codec->codec_type != type))
5202  match = 0;
5204 #else
5205  if (type != st->codecpar->codec_type)
5206  match = 0;
5207 #endif
5208  if (nopic && (st->disposition & AV_DISPOSITION_ATTACHED_PIC))
5209  match = 0;
5210  } else if (*spec == 'p' && *(spec + 1) == ':') {
5211  int prog_id, i, j;
5212  int found = 0;
5213  char *endptr;
5214  spec += 2;
5215  prog_id = strtol(spec, &endptr, 0);
5216  /* Disallow empty id and make sure that if we are not at the end, then another specifier must follow. */
5217  if (spec == endptr || (*endptr && *endptr++ != ':'))
5218  return AVERROR(EINVAL);
5219  spec = endptr;
5220  if (match) {
5221  for (i = 0; i < s->nb_programs; i++) {
5222  if (s->programs[i]->id != prog_id)
5223  continue;
5224 
5225  for (j = 0; j < s->programs[i]->nb_stream_indexes; j++) {
5226  if (st->index == s->programs[i]->stream_index[j]) {
5227  found = 1;
5228  if (p)
5229  *p = s->programs[i];
5230  i = s->nb_programs;
5231  break;
5232  }
5233  }
5234  }
5235  }
5236  if (!found)
5237  match = 0;
5238  } else if (*spec == '#' ||
5239  (*spec == 'i' && *(spec + 1) == ':')) {
5240  int stream_id;
5241  char *endptr;
5242  spec += 1 + (*spec == 'i');
5243  stream_id = strtol(spec, &endptr, 0);
5244  if (spec == endptr || *endptr) /* Disallow empty id and make sure we are at the end. */
5245  return AVERROR(EINVAL);
5246  return match && (stream_id == st->id);
5247  } else if (*spec == 'm' && *(spec + 1) == ':') {
5249  char *key, *val;
5250  int ret;
5251 
5252  if (match) {
5253  spec += 2;
5254  val = strchr(spec, ':');
5255 
5256  key = val ? av_strndup(spec, val - spec) : av_strdup(spec);
5257  if (!key)
5258  return AVERROR(ENOMEM);
5259 
5260  tag = av_dict_get(st->metadata, key, NULL, 0);
5261  if (tag) {
5262  if (!val || !strcmp(tag->value, val + 1))
5263  ret = 1;
5264  else
5265  ret = 0;
5266  } else
5267  ret = 0;
5268 
5269  av_freep(&key);
5270  }
5271  return match && ret;
5272  } else if (*spec == 'u' && *(spec + 1) == '\0') {
5273  AVCodecParameters *par = st->codecpar;
5274 #if FF_API_LAVF_AVCTX
5276  AVCodecContext *codec = st->codec;
5278 #endif
5279  int val;
5280  switch (par->codec_type) {
5281  case AVMEDIA_TYPE_AUDIO:
5282  val = par->sample_rate && par->channels;
5283 #if FF_API_LAVF_AVCTX
5284  val = val || (codec->sample_rate && codec->channels);
5285 #endif
5286  if (par->format == AV_SAMPLE_FMT_NONE
5288  && codec->sample_fmt == AV_SAMPLE_FMT_NONE
5289 #endif
5290  )
5291  return 0;
5292  break;
5293  case AVMEDIA_TYPE_VIDEO:
5294  val = par->width && par->height;
5295 #if FF_API_LAVF_AVCTX
5296  val = val || (codec->width && codec->height);
5297 #endif
5298  if (par->format == AV_PIX_FMT_NONE
5300  && codec->pix_fmt == AV_PIX_FMT_NONE
5301 #endif
5302  )
5303  return 0;
5304  break;
5305  case AVMEDIA_TYPE_UNKNOWN:
5306  val = 0;
5307  break;
5308  default:
5309  val = 1;
5310  break;
5311  }
5312 #if FF_API_LAVF_AVCTX
5313  return match && ((par->codec_id != AV_CODEC_ID_NONE || codec->codec_id != AV_CODEC_ID_NONE) && val != 0);
5314 #else
5315  return match && (par->codec_id != AV_CODEC_ID_NONE && val != 0);
5316 #endif
5317  } else {
5318  return AVERROR(EINVAL);
5319  }
5320  }
5321 
5322  return match;
5323 }
5324 
5325 
5327  const char *spec)
5328 {
5329  int ret, index;
5330  char *endptr;
5331  const char *indexptr = NULL;
5332  AVProgram *p = NULL;
5333  int nb_streams;
5334 
5335  ret = match_stream_specifier(s, st, spec, &indexptr, &p);
5336  if (ret < 0)
5337  goto error;
5338 
5339  if (!indexptr)
5340  return ret;
5341 
5342  index = strtol(indexptr, &endptr, 0);
5343  if (*endptr) { /* We can't have anything after the requested index. */
5344  ret = AVERROR(EINVAL);
5345  goto error;
5346  }
5347 
5348  /* This is not really needed but saves us a loop for simple stream index specifiers. */
5349  if (spec == indexptr)
5350  return (index == st->index);
5351 
5352  /* If we requested a matching stream index, we have to ensure st is that. */
5353  nb_streams = p ? p->nb_stream_indexes : s->nb_streams;
5354  for (int i = 0; i < nb_streams && index >= 0; i++) {
5355  AVStream *candidate = p ? s->streams[p->stream_index[i]] : s->streams[i];
5356  ret = match_stream_specifier(s, candidate, spec, NULL, NULL);
5357  if (ret < 0)
5358  goto error;
5359  if (ret > 0 && index-- == 0 && st == candidate)
5360  return 1;
5361  }
5362  return 0;
5363 
5364 error:
5365  if (ret == AVERROR(EINVAL))
5366  av_log(s, AV_LOG_ERROR, "Invalid stream specifier: %s.\n", spec);
5367  return ret;
5368 }
5369 
5371 {
5372  static const uint8_t avci100_1080p_extradata[] = {
5373  // SPS
5374  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
5375  0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
5376  0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
5377  0x18, 0x21, 0x02, 0x56, 0xb9, 0x3d, 0x7d, 0x7e,
5378  0x4f, 0xe3, 0x3f, 0x11, 0xf1, 0x9e, 0x08, 0xb8,
5379  0x8c, 0x54, 0x43, 0xc0, 0x78, 0x02, 0x27, 0xe2,
5380  0x70, 0x1e, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00,
5381  0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xca,
5382  0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
5383  // PPS
5384  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
5385  0xd0
5386  };
5387  static const uint8_t avci100_1080i_extradata[] = {
5388  // SPS
5389  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
5390  0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
5391  0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
5392  0x18, 0x21, 0x03, 0x3a, 0x46, 0x65, 0x6a, 0x65,
5393  0x24, 0xad, 0xe9, 0x12, 0x32, 0x14, 0x1a, 0x26,
5394  0x34, 0xad, 0xa4, 0x41, 0x82, 0x23, 0x01, 0x50,
5395  0x2b, 0x1a, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2e,
5396  0x11, 0x12, 0x08, 0xc6, 0x8c, 0x04, 0x41, 0x28,
5397  0x4c, 0x34, 0xf0, 0x1e, 0x01, 0x13, 0xf2, 0xe0,
5398  0x3c, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03,
5399  0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x20,
5400  // PPS
5401  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
5402  0xd0
5403  };
5404  static const uint8_t avci50_1080p_extradata[] = {
5405  // SPS
5406  0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
5407  0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
5408  0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
5409  0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
5410  0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
5411  0x6e, 0x6c, 0xd3, 0x3c, 0x05, 0xa0, 0x22, 0x7e,
5412  0x5f, 0xfc, 0x00, 0x0c, 0x00, 0x13, 0x8c, 0x04,
5413  0x04, 0x05, 0x00, 0x00, 0x03, 0x00, 0x01, 0x00,
5414  0x00, 0x03, 0x00, 0x32, 0x84, 0x00, 0x00, 0x00,
5415  // PPS
5416  0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
5417  0x11
5418  };
5419  static const uint8_t avci50_1080i_extradata[] = {
5420  // SPS
5421  0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
5422  0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
5423  0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
5424  0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6e, 0x61,
5425  0x87, 0x3e, 0x73, 0x4d, 0x98, 0x0c, 0x03, 0x06,
5426  0x9c, 0x0b, 0x73, 0xe6, 0xc0, 0xb5, 0x18, 0x63,
5427  0x0d, 0x39, 0xe0, 0x5b, 0x02, 0xd4, 0xc6, 0x19,
5428  0x1a, 0x79, 0x8c, 0x32, 0x34, 0x24, 0xf0, 0x16,
5429  0x81, 0x13, 0xf7, 0xff, 0x80, 0x02, 0x00, 0x01,
5430  0xf1, 0x80, 0x80, 0x80, 0xa0, 0x00, 0x00, 0x03,
5431  0x00, 0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00,
5432  // PPS
5433  0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
5434  0x11
5435  };
5436  static const uint8_t avci100_720p_extradata[] = {
5437  // SPS
5438  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
5439  0xb6, 0xd4, 0x20, 0x2a, 0x33, 0x1d, 0xc7, 0x62,
5440  0xa1, 0x08, 0x40, 0x54, 0x66, 0x3b, 0x8e, 0xc5,
5441  0x42, 0x02, 0x10, 0x25, 0x64, 0x2c, 0x89, 0xe8,
5442  0x85, 0xe4, 0x21, 0x4b, 0x90, 0x83, 0x06, 0x95,
5443  0xd1, 0x06, 0x46, 0x97, 0x20, 0xc8, 0xd7, 0x43,
5444  0x08, 0x11, 0xc2, 0x1e, 0x4c, 0x91, 0x0f, 0x01,
5445  0x40, 0x16, 0xec, 0x07, 0x8c, 0x04, 0x04, 0x05,
5446  0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03,
5447  0x00, 0x64, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
5448  // PPS
5449  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x31, 0x12,
5450  0x11
5451  };
5452  static const uint8_t avci50_720p_extradata[] = {
5453  // SPS
5454  0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x20,
5455  0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
5456  0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
5457  0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6f, 0x37,
5458  0xcd, 0xf9, 0xbf, 0x81, 0x6b, 0xf3, 0x7c, 0xde,
5459  0x6e, 0x6c, 0xd3, 0x3c, 0x0f, 0x01, 0x6e, 0xff,
5460  0xc0, 0x00, 0xc0, 0x01, 0x38, 0xc0, 0x40, 0x40,
5461  0x50, 0x00, 0x00, 0x03, 0x00, 0x10, 0x00, 0x00,
5462  0x06, 0x48, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
5463  // PPS
5464  0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
5465  0x11
5466  };
5467 
5468  const uint8_t *data = NULL;
5469  int ret, size = 0;
5470 
5471  if (st->codecpar->width == 1920) {
5473  data = avci100_1080p_extradata;
5474  size = sizeof(avci100_1080p_extradata);
5475  } else {
5476  data = avci100_1080i_extradata;
5477  size = sizeof(avci100_1080i_extradata);
5478  }
5479  } else if (st->codecpar->width == 1440) {
5481  data = avci50_1080p_extradata;
5482  size = sizeof(avci50_1080p_extradata);
5483  } else {
5484  data = avci50_1080i_extradata;
5485  size = sizeof(avci50_1080i_extradata);
5486  }
5487  } else if (st->codecpar->width == 1280) {
5488  data = avci100_720p_extradata;
5489  size = sizeof(avci100_720p_extradata);
5490  } else if (st->codecpar->width == 960) {
5491  data = avci50_720p_extradata;
5492  size = sizeof(avci50_720p_extradata);
5493  }
5494 
5495  if (!size)
5496  return 0;
5497 
5498  if ((ret = ff_alloc_extradata(st->codecpar, size)) < 0)
5499  return ret;
5500  memcpy(st->codecpar->extradata, data, size);
5501 
5502  return 0;
5503 }
5504 
5507 {
5508  int i;
5509 
5510  for (i = 0; i < st->nb_side_data; i++) {
5511  if (st->side_data[i].type == type) {
5512  if (size)
5513  *size = st->side_data[i].size;
5514  return st->side_data[i].data;
5515  }
5516  }
5517  if (size)
5518  *size = 0;
5519  return NULL;
5520 }
5521 
5523  uint8_t *data, size_t size)
5524 {
5525  AVPacketSideData *sd, *tmp;
5526  int i;
5527 
5528  for (i = 0; i < st->nb_side_data; i++) {
5529  sd = &st->side_data[i];
5530 
5531  if (sd->type == type) {
5532  av_freep(&sd->data);
5533  sd->data = data;
5534  sd->size = size;
5535  return 0;
5536  }
5537  }
5538 
5539  if ((unsigned)st->nb_side_data + 1 >= INT_MAX / sizeof(*st->side_data))
5540  return AVERROR(ERANGE);
5541 
5542  tmp = av_realloc(st->side_data, (st->nb_side_data + 1) * sizeof(*tmp));
5543  if (!tmp) {
5544  return AVERROR(ENOMEM);
5545  }
5546 
5547  st->side_data = tmp;
5548  st->nb_side_data++;
5549 
5550  sd = &st->side_data[st->nb_side_data - 1];
5551  sd->type = type;
5552  sd->data = data;
5553  sd->size = size;
5554 
5555  return 0;
5556 }
5557 
5560 {
5561  int ret;
5562  uint8_t *data = av_malloc(size);
5563 
5564  if (!data)
5565  return NULL;
5566 
5568  if (ret < 0) {
5569  av_freep(&data);
5570  return NULL;
5571  }
5572 
5573  return data;
5574 }
5575 
5576 int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
5577 {
5578  int ret;
5579  const AVBitStreamFilter *bsf;
5580  AVBSFContext *bsfc;
5581 
5582  av_assert0(!st->internal->bsfc);
5583 
5584  if (!(bsf = av_bsf_get_by_name(name))) {
5585  av_log(NULL, AV_LOG_ERROR, "Unknown bitstream filter '%s'\n", name);
5586  return AVERROR_BSF_NOT_FOUND;
5587  }
5588 
5589  if ((ret = av_bsf_alloc(bsf, &bsfc)) < 0)
5590  return ret;
5591 
5592  bsfc->time_base_in = st->time_base;
5593  if ((ret = avcodec_parameters_copy(bsfc->par_in, st->codecpar)) < 0) {
5594  av_bsf_free(&bsfc);
5595  return ret;
5596  }
5597 
5598  if (args && bsfc->filter->priv_class) {
5599  const AVOption *opt = av_opt_next(bsfc->priv_data, NULL);
5600  const char * shorthand[2] = {NULL};
5601 
5602  if (opt)
5603  shorthand[0] = opt->name;
5604 
5605  if ((ret = av_opt_set_from_string(bsfc->priv_data, args, shorthand, "=", ":")) < 0) {
5606  av_bsf_free(&bsfc);
5607  return ret;
5608  }
5609  }
5610 
5611  if ((ret = av_bsf_init(bsfc)) < 0) {
5612  av_bsf_free(&bsfc);
5613  return ret;
5614  }
5615 
5616  st->internal->bsfc = bsfc;
5617 
5619  "Automatically inserted bitstream filter '%s'; args='%s'\n",
5620  name, args ? args : "");
5621  return 1;
5622 }
5623 
5624 #if FF_API_OLD_BSF
5626 int av_apply_bitstream_filters(AVCodecContext *codec, AVPacket *pkt,
5628 {
5629  int ret = 0;
5630  while (bsfc) {
5631  AVPacket new_pkt = *pkt;
5632  int a = av_bitstream_filter_filter(bsfc, codec, NULL,
5633  &new_pkt.data, &new_pkt.size,
5634  pkt->data, pkt->size,
5636  if (a == 0 && new_pkt.size == 0 && new_pkt.side_data_elems == 0) {
5638  memset(pkt, 0, sizeof(*pkt));
5639  return 0;
5640  }
5641  if(a == 0 && new_pkt.data != pkt->data) {
5642  uint8_t *t = av_malloc(new_pkt.size + AV_INPUT_BUFFER_PADDING_SIZE); //the new should be a subset of the old so cannot overflow
5643  if (t) {
5644  memcpy(t, new_pkt.data, new_pkt.size);
5645  memset(t + new_pkt.size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
5646  new_pkt.data = t;
5647  new_pkt.buf = NULL;
5648  a = 1;
5649  } else {
5650  a = AVERROR(ENOMEM);
5651  }
5652  }
5653  if (a > 0) {
5654  new_pkt.buf = av_buffer_create(new_pkt.data, new_pkt.size,
5656  if (new_pkt.buf) {
5657  pkt->side_data = NULL;
5658  pkt->side_data_elems = 0;
5660  } else {
5661  av_freep(&new_pkt.data);
5662  a = AVERROR(ENOMEM);
5663  }
5664  }
5665  if (a < 0) {
5666  av_log(codec, AV_LOG_ERROR,
5667  "Failed to open bitstream filter %s for stream %d with codec %s",
5668  bsfc->filter->name, pkt->stream_index,
5669  codec->codec ? codec->codec->name : "copy");
5670  ret = a;
5671  break;
5672  }
5673  *pkt = new_pkt;
5674 
5675  bsfc = bsfc->next;
5676  }
5677  return ret;
5678 }
5680 #endif
5681 
5683 {
5684  if (!s->oformat)
5685  return AVERROR(EINVAL);
5686 
5687  if (!(s->oformat->flags & AVFMT_NOFILE))
5688  return s->io_open(s, &s->pb, url, AVIO_FLAG_WRITE, options);
5689  return 0;
5690 }
5691 
5693 {
5694  if (*pb)
5695  s->io_close(s, *pb);
5696  *pb = NULL;
5697 }
5698 
5699 int ff_is_http_proto(char *filename) {
5700  const char *proto = avio_find_protocol_name(filename);
5701  return proto ? (!av_strcasecmp(proto, "http") || !av_strcasecmp(proto, "https")) : 0;
5702 }
5703 
5704 int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
5705 {
5706  AVDictionaryEntry *entry;
5707  int64_t parsed_timestamp;
5708  int ret;
5709  if ((entry = av_dict_get(s->metadata, "creation_time", NULL, 0))) {
5710  if ((ret = av_parse_time(&parsed_timestamp, entry->value, 0)) >= 0) {
5711  *timestamp = return_seconds ? parsed_timestamp / 1000000 : parsed_timestamp;
5712  return 1;
5713  } else {
5714  av_log(s, AV_LOG_WARNING, "Failed to parse creation_time %s\n", entry->value);
5715  return ret;
5716  }
5717  }
5718  return 0;
5719 }
5720 
5722 {
5723  int64_t timestamp;
5724  int ret = ff_parse_creation_time_metadata(s, &timestamp, 0);
5725  if (ret == 1)
5726  return avpriv_dict_set_timestamp(&s->metadata, "creation_time", timestamp);
5727  return ret;
5728 }
5729 
5730 int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
5731 {
5732  uint8_t *side_data;
5734 
5736  if (side_data) {
5737  if (size != AVPALETTE_SIZE) {
5738  av_log(s, AV_LOG_ERROR, "Invalid palette side data\n");
5739  return AVERROR_INVALIDDATA;
5740  }
5741  memcpy(palette, side_data, AVPALETTE_SIZE);
5742  return 1;
5743  }
5744 
5745  if (ret == CONTAINS_PAL) {
5746  int i;
5747  for (i = 0; i < AVPALETTE_COUNT; i++)
5748  palette[i] = AV_RL32(pkt->data + pkt->size - AVPALETTE_SIZE + i*4);
5749  return 1;
5750  }
5751 
5752  return 0;
5753 }
5754 
5756 {
5757  int ret;
5758  char *str;
5759 
5760  ret = av_bprint_finalize(buf, &str);
5761  if (ret < 0)
5762  return ret;
5763  if (!av_bprint_is_complete(buf)) {
5764  av_free(str);
5765  return AVERROR(ENOMEM);
5766  }
5767 
5768  par->extradata = str;
5769  /* Note: the string is NUL terminated (so extradata can be read as a
5770  * string), but the ending character is not accounted in the size (in
5771  * binary formats you are likely not supposed to mux that character). When
5772  * extradata is copied, it is also padded with AV_INPUT_BUFFER_PADDING_SIZE
5773  * zeros. */
5774  par->extradata_size = buf->len;
5775  return 0;
5776 }
5777 
5779  AVStream *ost, const AVStream *ist,
5781 {
5782  //TODO: use [io]st->internal->avctx
5783  const AVCodecContext *dec_ctx;
5784  AVCodecContext *enc_ctx;
5785 
5786 #if FF_API_LAVF_AVCTX
5788  dec_ctx = ist->codec;
5789  enc_ctx = ost->codec;
5791 #else
5792  dec_ctx = ist->internal->avctx;
5793  enc_ctx = ost->internal->avctx;
5794 #endif
5795 
5796  enc_ctx->time_base = ist->time_base;
5797  /*
5798  * Avi is a special case here because it supports variable fps but
5799  * having the fps and timebase differe significantly adds quite some
5800  * overhead
5801  */
5802  if (!strcmp(ofmt->name, "avi")) {
5803 #if FF_API_R_FRAME_RATE
5804  if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num
5805  && av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate)
5806  && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base)
5807  && 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx->time_base)
5808  && av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx->time_base) < 1.0/500
5809  || copy_tb == AVFMT_TBCF_R_FRAMERATE) {
5810  enc_ctx->time_base.num = ist->r_frame_rate.den;
5811  enc_ctx->time_base.den = 2*ist->r_frame_rate.num;
5812  enc_ctx->ticks_per_frame = 2;
5813  } else
5814 #endif
5816  && av_q2d(ist->time_base) < 1.0/500
5817  || copy_tb == AVFMT_TBCF_DECODER) {
5818  enc_ctx->time_base = dec_ctx->time_base;
5819  enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
5820  enc_ctx->time_base.den *= 2;
5821  enc_ctx->ticks_per_frame = 2;
5822  }
5823  } else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
5824  && !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
5827  && av_q2d(ist->time_base) < 1.0/500
5828  || copy_tb == AVFMT_TBCF_DECODER) {
5829  enc_ctx->time_base = dec_ctx->time_base;
5830  enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
5831  }
5832  }
5833 
5834  if ((enc_ctx->codec_tag == AV_RL32("tmcd") || ost->codecpar->codec_tag == AV_RL32("tmcd"))
5836  && dec_ctx->time_base.num > 0
5837  && 121LL*dec_ctx->time_base.num > dec_ctx->time_base.den) {
5838  enc_ctx->time_base = dec_ctx->time_base;
5839  }
5840 
5841  if (ost->avg_frame_rate.num)
5842  enc_ctx->time_base = av_inv_q(ost->avg_frame_rate);
5843 
5844  av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
5845  enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
5846 
5847  return 0;
5848 }
5849 
5851 {
5852  // See avformat_transfer_internal_stream_timing_info() TODO.
5853 #if FF_API_LAVF_AVCTX
5855  return st->codec->time_base;
5857 #else
5858  return st->internal->avctx->time_base;
5859 #endif
5860 }
5861 
5863 {
5864  av_assert0(url);
5865  av_freep(&s->url);
5866  s->url = url;
5867 #if FF_API_FORMAT_FILENAME
5869  av_strlcpy(s->filename, url, sizeof(s->filename));
5871 #endif
5872 }
AVStream::index_entries
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:1090
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:30
AV_CODEC_ID_PCM_S16LE
@ AV_CODEC_ID_PCM_S16LE
Definition: codec_id.h:313
AVStreamInternal::priv_pts
FFFrac * priv_pts
Definition: internal.h:204
AVSubtitle
Definition: avcodec.h:2722
avcodec_close
av_cold int avcodec_close(AVCodecContext *avctx)
Close a given AVCodecContext and free all the data associated with it (but not the AVCodecContext its...
Definition: avcodec.c:570
AVCodecContext::frame_size
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1216
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:1031
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
be
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 be(in the first position) for now. Options ------- Then comes the options array. This is what will define the user accessible options. For example
AVCodec
AVCodec.
Definition: codec.h:197
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
ff_codec_get_id
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:3131
DURATION_MAX_RETRY
#define DURATION_MAX_RETRY
Definition: utils.c:2769
AVBSFContext::par_in
AVCodecParameters * par_in
Parameters of the input stream.
Definition: bsf.h:77
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
AVFMT_FLAG_DISCARD_CORRUPT
#define AVFMT_FLAG_DISCARD_CORRUPT
Discard frames marked corrupted.
Definition: avformat.h:1372
AV_CODEC_ID_PCM_F32BE
@ AV_CODEC_ID_PCM_F32BE
Definition: codec_id.h:333
ff_data_to_hex
char * ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
Definition: utils.c:4896
SPACE_CHARS
#define SPACE_CHARS
Definition: internal.h:499
AVCodecParameters::extradata
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: codec_par.h:74
AVCodecParserContext::convergence_duration
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:3433
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
AVFMT_NO_BYTE_SEEK
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:470
av_format_control_message
int(* av_format_control_message)(struct AVFormatContext *s, int type, void *data, size_t data_size)
Callback used by devices to communicate with application.
Definition: avformat.h:1200
AVCodecParserContext::pts
int64_t pts
Definition: avcodec.h:3396
AV_CODEC_ID_AC3
@ AV_CODEC_ID_AC3
Definition: codec_id.h:427
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
is_relative
static int is_relative(int64_t ts)
Definition: utils.c:91
AVFMT_DURATION_FROM_BITRATE
@ AVFMT_DURATION_FROM_BITRATE
Duration estimated from bitrate (less accurate)
Definition: avformat.h:1213
LIBAVFORMAT_VERSION_INT
#define LIBAVFORMAT_VERSION_INT
Definition: version.h:38
ffio_realloc_buf
int ffio_realloc_buf(AVIOContext *s, int buf_size)
Reallocate a given buffer for AVIOContext.
Definition: aviobuf.c:1049
AVOutputFormat::name
const char * name
Definition: avformat.h:491
AVChapter::metadata
AVDictionary * metadata
Definition: avformat.h:1193
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
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:4509
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:56
avpriv_get_raw_pix_fmt_tags
const struct PixelFormatTag * avpriv_get_raw_pix_fmt_tags(void)
Definition: raw.c:300
ff_lock_avformat
int ff_lock_avformat(void)
Definition: utils.c:79
av_packet_get_side_data
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t *size)
Definition: avpacket.c:368
AVCodecContext::audio_service_type
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:1261
AV_WL32
#define AV_WL32(p, v)
Definition: intreadwrite.h:426
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1247
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:1358
ff_rfps_calculate
void ff_rfps_calculate(AVFormatContext *ic)
Definition: utils.c:3408
av_codec_get_tag2
int av_codec_get_tag2(const AVCodecTag *const *tags, enum AVCodecID id, unsigned int *tag)
Definition: utils.c:3200
AVProgram::nb_stream_indexes
unsigned int nb_stream_indexes
Definition: avformat.h:1155
av_bprint_finalize
int av_bprint_finalize(AVBPrint *buf, char **ret_str)
Finalize a print buffer.
Definition: bprint.c:235
FF_FDEBUG_TS
#define FF_FDEBUG_TS
Definition: avformat.h:1518
AVSTREAM_EVENT_FLAG_NEW_PACKETS
#define AVSTREAM_EVENT_FLAG_NEW_PACKETS
Definition: avformat.h:1005
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
ff_mkdir_p
int ff_mkdir_p(const char *path)
Automatically create sub-directories.
Definition: utils.c:4862
avio_close
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:1169
FFERROR_REDO
#define FFERROR_REDO
Returned by demuxers to indicate that data was consumed but discarded (ignored streams or junk data).
Definition: internal.h:793
AVCodecParserContext::pict_type
int pict_type
Definition: avcodec.h:3385
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1196
FFSWAP
#define FFSWAP(type, a, b)
Definition: common.h:108
ff_is_http_proto
int ff_is_http_proto(char *filename)
Utility function to check if the file uses http or https protocol.
Definition: utils.c:5699
AVStreamInternal::avctx
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:180
ff_ntp_time
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition: utils.c:4709
AVCodecParameters
This struct describes the properties of an encoded stream.
Definition: codec_par.h:52
AV_PKT_DATA_PARAM_CHANGE
@ AV_PKT_DATA_PARAM_CHANGE
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: packet.h:72
AVIOContext::seek_count
int seek_count
seek statistic This field is internal to libavformat and access from outside is not allowed.
Definition: avio.h:285
av_stream_new_side_data
uint8_t * av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type, buffer_size_t size)
Definition: utils.c:5558
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:1746
thread.h
AVStream::priv_data
void * priv_data
Definition: avformat.h:888
AVERROR_EOF
#define AVERROR_EOF
End of file.
Definition: error.h:55
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:465
MKTAG
#define MKTAG(a, b, c, d)
Definition: common.h:478
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:841
AVStream::discard
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:928
ff_rfps_add_frame
int ff_rfps_add_frame(AVFormatContext *ic, AVStream *st, int64_t ts)
add frame for rfps calculation.
Definition: utils.c:3348
av_find_program_from_stream
AVProgram * av_find_program_from_stream(AVFormatContext *ic, AVProgram *last, int s)
Find the programs which belong to a given stream.
Definition: utils.c:4213
av_div_q
AVRational av_div_q(AVRational b, AVRational c)
Divide one rational by another.
Definition: rational.c:88
avcodec_parameters_from_context
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: codec_par.c:90
av_bitstream_filter_filter
attribute_deprecated int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc, AVCodecContext *avctx, const char *args, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int keyframe)
Definition: bitstream_filter.c:97
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:417
AVFormatContext::protocol_blacklist
char * protocol_blacklist
',' separated list of disallowed protocols.
Definition: avformat.h:1841
AVFMT_TBCF_DECODER
@ AVFMT_TBCF_DECODER
Definition: avformat.h:2978
AVCodecParserContext::duration
int duration
Duration of the current frame.
Definition: avcodec.h:3499
avcodec_string
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
Definition: avcodec.c:652
av_stream_get_parser
struct AVCodecParserContext * av_stream_get_parser(const AVStream *st)
Definition: utils.c:145
avpriv_toupper4
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:926
AVFMT_NOTIMESTAMPS
#define AVFMT_NOTIMESTAMPS
Format does not need / have any timestamps.
Definition: avformat.h:462
AV_TIME_BASE_Q
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
AVCodecTag::id
enum AVCodecID id
Definition: internal.h:43
AVBitStreamFilter::name
const char * name
Definition: bsf.h:99
AVCodecContext::coded_side_data
AVPacketSideData * coded_side_data
Additional data associated with the entire coded stream.
Definition: avcodec.h:2193
MAX_STD_TIMEBASES
#define MAX_STD_TIMEBASES
Definition: internal.h:206
av_grow_packet
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:122
av_strcasecmp
int av_strcasecmp(const char *a, const char *b)
Locale-independent case-insensitive compare.
Definition: avstring.c:215
ff_find_last_ts
int ff_find_last_ts(AVFormatContext *s, int stream_index, int64_t *ts, int64_t *pos, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition: utils.c:2213
AV_CODEC_ID_RAWVIDEO
@ AV_CODEC_ID_RAWVIDEO
Definition: codec_id.h:62
av_unused
#define av_unused
Definition: attributes.h:131
av_isspace
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:227
AV_DISPOSITION_DEFAULT
#define AV_DISPOSITION_DEFAULT
Definition: avformat.h:818
AV_CODEC_ID_MPEG4
@ AV_CODEC_ID_MPEG4
Definition: codec_id.h:61
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
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:148
id3v2.h
AVFMT_TBCF_AUTO
@ AVFMT_TBCF_AUTO
Definition: avformat.h:2977
AVStreamInternal::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:330
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:27
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1300
step
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
Definition: rate_distortion.txt:58
AVPacketSideData
Definition: packet.h:306
ff_read_frame_flush
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1892
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:216
avcodec_decode_subtitle2
int avcodec_decode_subtitle2(AVCodecContext *avctx, AVSubtitle *sub, int *got_sub_ptr, AVPacket *avpkt)
Decode a subtitle message.
Definition: decode.c:1034
internal.h
AVPacket::data
uint8_t * data
Definition: packet.h:369
AV_CODEC_ID_DVB_TELETEXT
@ AV_CODEC_ID_DVB_TELETEXT
Definition: codec_id.h:530
av_get_frame_filename2
int av_get_frame_filename2(char *buf, int buf_size, const char *path, int number, int flags)
Return in 'buf' the path with 'd' replaced by a number.
Definition: utils.c:4736
AVStream::internal
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1113
AVOption
AVOption.
Definition: opt.h:248
b
#define b
Definition: input.c:41
AV_PKT_DATA_PALETTE
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
Definition: packet.h:46
AVStream::avg_frame_rate
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:946
PacketList
Definition: packet_internal.h:26
AVChapter::start
int64_t start
Definition: avformat.h:1192
AVFMT_FLAG_CUSTOM_IO
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1371
data
const char data[16]
Definition: mxf.c:142
AV_CODEC_ID_PCM_U24LE
@ AV_CODEC_ID_PCM_U24LE
Definition: codec_id.h:327
AVStreamInternal::info
struct AVStreamInternal::@260 * info
Stream information used internally by avformat_find_stream_info()
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:1633
AVStream::cur_dts
int64_t cur_dts
Definition: avformat.h:1066
AVSEEK_FLAG_BYTE
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:2416
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:61
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: utils.c:1226
AVCodecContext::subtitle_header
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:2016
AVBitStreamFilterContext::filter
const struct AVBitStreamFilter * filter
Definition: avcodec.h:4019
AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
@ AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE
Definition: packet.h:434
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
LICENSE_PREFIX
#define LICENSE_PREFIX
AV_LOG_VERBOSE
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:210
AVFormatInternal::packet_buffer
struct PacketList * packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: internal.h:76
LIBAVFORMAT_VERSION_MICRO
#define LIBAVFORMAT_VERSION_MICRO
Definition: version.h:36
av_buffer_create
AVBufferRef * av_buffer_create(uint8_t *data, buffer_size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:29
AVFormatContext::programs
AVProgram ** programs
Definition: avformat.h:1414
AVINDEX_DISCARD_FRAME
#define AVINDEX_DISCARD_FRAME
Definition: avformat.h:812
AVPacket::duration
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: packet.h:387
AVCodecParameters::codec_tag
uint32_t codec_tag
Additional information about the codec (corresponds to the AVI FOURCC).
Definition: codec_par.h:64
mathematics.h
avpriv_new_chapter
AVChapter * avpriv_new_chapter(AVFormatContext *s, int64_t id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:4641
AVStreamInternal::inject_global_side_data
int inject_global_side_data
Internal data to inject global side data.
Definition: internal.h:337
AVDictionary
Definition: dict.c:30
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:444
ff_network_close
void ff_network_close(void)
Definition: network.c:116
AVFormatContext::probesize
int64_t probesize
Maximum size of the data read from input for determining the input container format.
Definition: avformat.h:1400
AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT
@ AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT
Definition: packet.h:433
av_read_frame
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1741
avcodec_is_open
int avcodec_is_open(AVCodecContext *s)
Definition: avcodec.c:848
RELATIVE_TS_BASE
#define RELATIVE_TS_BASE
Definition: utils.c:89
AVStreamInternal::bsfc
AVBSFContext * bsfc
bitstream filter to run on stream
Definition: internal.h:170
AVFMT_NOBINSEARCH
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:468
AV_CODEC_ID_HDMV_PGS_SUBTITLE
@ AV_CODEC_ID_HDMV_PGS_SUBTITLE
Definition: codec_id.h:529
ff_id3v2_parse_chapters
int ff_id3v2_parse_chapters(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create chapters for all CHAP tags found in the ID3v2 header.
Definition: id3v2.c:1182
AVFormatContext::internal
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
av_get_frame_filename
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Definition: utils.c:4794
avio_size
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:342
ost
static AVStream * ost
Definition: vaapi_transcode.c:45
AV_CODEC_ID_TRUEHD
@ AV_CODEC_ID_TRUEHD
Definition: codec_id.h:468
extract_extradata_init
static int extract_extradata_init(AVStream *st)
Definition: utils.c:3493
AV_PKT_FLAG_KEY
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: packet.h:410
sample_rate
sample_rate
Definition: ffmpeg_filter.c:170
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
AVBSFContext
The bitstream filter state.
Definition: bsf.h:49
AVStreamInternal::skip_samples
int skip_samples
Number of samples to skip at the start of the frame decoded from the next packet.
Definition: internal.h:258
AVIndexEntry
Definition: avformat.h:803
ff_network_init
int ff_network_init(void)
Definition: network.c:58
ff_tls_init
int ff_tls_init(void)
Definition: network.c:31
AVOutputFormat::subtitle_codec
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:503
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
AVStreamInternal::last_duration
int64_t last_duration
Definition: internal.h:227
update_wrap_reference
static int update_wrap_reference(AVFormatContext *s, AVStream *st, int stream_index, AVPacket *pkt)
Definition: utils.c:745
ff_const59
#define ff_const59
The ff_const59 define is not part of the public API and will be removed without further warning.
Definition: avformat.h:535
AVINDEX_KEYFRAME
#define AVINDEX_KEYFRAME
Definition: avformat.h:811
avcodec_pix_fmt_to_codec_tag
unsigned int avcodec_pix_fmt_to_codec_tag(enum AVPixelFormat pix_fmt)
Return a value representing the fourCC code associated to the pixel format pix_fmt,...
Definition: raw.c:305
ff_free_stream
void ff_free_stream(AVFormatContext *s, AVStream *st)
Definition: utils.c:4428
av_bsf_get_by_name
const AVBitStreamFilter * av_bsf_get_by_name(const char *name)
Definition: bitstream_filters.c:84
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:335
av_filename_number_test
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:333
AV_CODEC_ID_PCM_S64LE
@ AV_CODEC_ID_PCM_S64LE
Definition: codec_id.h:345
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:285
ff_format_io_close
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
Definition: utils.c:5692
ff_mutex_unlock
static int ff_mutex_unlock(AVMutex *mutex)
Definition: thread.h:169
avformat_queue_attached_pictures
int avformat_queue_attached_pictures(AVFormatContext *s)
Definition: utils.c:454
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:2071
framerate
int framerate
Definition: h264_levels.c:65
avformat_close_input
void avformat_close_input(AVFormatContext **ps)
Close an opened input AVFormatContext.
Definition: utils.c:4481
ff_stream_encode_params_copy
int ff_stream_encode_params_copy(AVStream *dst, const AVStream *src)
Copy encoding parameters from source to destination stream.
Definition: utils.c:4316
AVPacketSideData::size
size_t size
Definition: packet.h:311
AVCodecParserContext::offset
int64_t offset
byte offset from starting packet start
Definition: avcodec.h:3417
AVFormatContext::interrupt_callback
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1512
avcodec_find_decoder_by_name
AVCodec * avcodec_find_decoder_by_name(const char *name)
Find a registered decoder with the specified name.
Definition: allcodecs.c:974
AVCodecParserContext::key_frame
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3426
AVCodecParameters::channels
int channels
Audio only.
Definition: codec_par.h:166
decoder
static const chunk_decoder decoder[8]
Definition: dfa.c:330
finish
static void finish(void)
Definition: movenc.c:342
AVFormatContext::iformat
ff_const59 struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:1244
av_stream_get_end_pts
int64_t av_stream_get_end_pts(const AVStream *st)
Returns the pts of the last muxed packet + its duration.
Definition: utils.c:137
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:545
AV_CODEC_ID_PCM_S16BE
@ AV_CODEC_ID_PCM_S16BE
Definition: codec_id.h:314
add_coded_side_data
static int add_coded_side_data(AVStream *st, AVCodecContext *avctx)
Definition: utils.c:3587
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2006
fail
#define fail()
Definition: checkasm.h:133
AVSEEK_FLAG_ANY
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:2417
find_decoder
static const AVCodec * find_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
Definition: utils.c:180
av_shrink_packet
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:114
duration_name
static const char *const duration_name[]
Definition: utils.c:2905
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: utils.c:2013
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:797
update_dts_from_pts
static void update_dts_from_pts(AVFormatContext *s, int stream_index, PacketList *pkt_buffer)
Updates the dts of packets of a stream in pkt_buffer, by re-ordering the pts of the packets in a wind...
Definition: utils.c:1090
MAKE_ACCESSORS
#define MAKE_ACCESSORS(str, name, type, field)
Definition: internal.h:90
AVFormatInternal::prefer_codec_framerate
int prefer_codec_framerate
Definition: internal.h:150
avformat_version
unsigned avformat_version(void)
Return the LIBAVFORMAT_VERSION_INT constant.
Definition: utils.c:62
ff_configure_buffers_for_index
void ff_configure_buffers_for_index(AVFormatContext *s, int64_t time_tolerance)
Definition: utils.c:2065
free_stream
static void free_stream(AVStream **pst)
Definition: utils.c:4377
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:557
AVDISCARD_NONE
@ AVDISCARD_NONE
discard nothing
Definition: avcodec.h:230
select_from_pts_buffer
static int64_t select_from_pts_buffer(AVStream *st, int64_t *pts_buffer, int64_t dts)
Definition: utils.c:1044
AVChapter
Definition: avformat.h:1185
AVFormatInternal::data_offset
int64_t data_offset
offset of the first packet
Definition: internal.h:80
AVFMT_GENERIC_INDEX
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:463
val
static double val(void *priv, double ch)
Definition: aeval.c:76
AVFMT_DURATION_FROM_PTS
@ AVFMT_DURATION_FROM_PTS
Duration accurately estimated from PTSes.
Definition: avformat.h:1211
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:455
av_parser_init
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:34
pts
static int64_t pts
Definition: transcode_aac.c:652
AV_ROUND_UP
@ AV_ROUND_UP
Round toward +infinity.
Definition: mathematics.h:83
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:724
AVBitStreamFilterContext::next
struct AVBitStreamFilterContext * next
Definition: avcodec.h:4021
av_opt_set
int av_opt_set(void *obj, const char *name, const char *val, int search_flags)
Definition: opt.c:465
av_new_program
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition: utils.c:4607
AV_CODEC_ID_MP3
@ AV_CODEC_ID_MP3
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: codec_id.h:425
AVStream::duration
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:922
AV_PTS_WRAP_IGNORE
#define AV_PTS_WRAP_IGNORE
Options for behavior on timestamp wrap detection.
Definition: avformat.h:862
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:661
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
AVCodecParserContext::dts
int64_t dts
Definition: avcodec.h:3397
AVRational::num
int num
Numerator.
Definition: rational.h:59
ff_get_packet_palette
int ff_get_packet_palette(AVFormatContext *s, AVPacket *pkt, int ret, uint32_t *palette)
Retrieves the palette from a packet, either from side data, or appended to the video data in the pack...
Definition: utils.c:5730
avformat_network_init
int avformat_network_init(void)
Do global initialization of network libraries.
Definition: utils.c:5056
try_decode_frame
static int try_decode_frame(AVFormatContext *s, AVStream *st, const AVPacket *avpkt, AVDictionary **options)
Definition: utils.c:3017
AVStream::attached_pic
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:955
AVStream::last_IP_duration
int last_IP_duration
Definition: avformat.h:1068
avsubtitle_free
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition: avcodec.c:551
raw.h
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
AV_CODEC_ID_DVB_SUBTITLE
@ AV_CODEC_ID_DVB_SUBTITLE
Definition: codec_id.h:524
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:1354
AV_DISPOSITION_CLEAN_EFFECTS
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:833
AVFormatContext::max_ts_probe
int max_ts_probe
Maximum number of packets to read while waiting for the first timestamp.
Definition: avformat.h:1568
av_init_packet
void av_init_packet(AVPacket *pkt)
Definition: avpacket.c:36
AV_CODEC_ID_PCM_S8
@ AV_CODEC_ID_PCM_S8
Definition: codec_id.h:317
avassert.h
AVFormatContext::format_whitelist
char * format_whitelist
',' separated list of allowed demuxers.
Definition: avformat.h:1693
av_codec_get_tag
unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
Definition: utils.c:3192
av_guess_sample_aspect_ratio
AVRational av_guess_sample_aspect_ratio(AVFormatContext *format, AVStream *stream, AVFrame *frame)
Guess the sample aspect ratio of a frame, based on both the stream and the frame aspect ratio.
Definition: utils.c:5119
AV_LOG_TRACE
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:220
pkt
AVPacket * pkt
Definition: movenc.c:59
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
ff_update_cur_dts
void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
Update cur_dts of all streams based on the given timestamp and AVStream.
Definition: utils.c:1927
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:100
AVInputFormat
Definition: avformat.h:640
AV_PKT_FLAG_CORRUPT
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: packet.h:411
ff_add_param_change
int ff_add_param_change(AVPacket *pkt, int32_t channels, uint64_t channel_layout, int32_t sample_rate, int32_t width, int32_t height)
Add side data to a packet for changing parameters to the given values.
Definition: utils.c:5077
RAW_PACKET_BUFFER_SIZE
#define RAW_PACKET_BUFFER_SIZE
Remaining size available for raw_packet_buffer, in bytes.
Definition: internal.h:104
AVCodecTag
Definition: internal.h:42
ID3v2ExtraMeta
Definition: id3v2.h:84
AVFormatContext::ctx_flags
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:1281
AVOutputFormat::data_codec
enum AVCodecID data_codec
default data codec
Definition: avformat.h:605
AVStream::first_dts
int64_t first_dts
Timestamp corresponding to the last dts sync point.
Definition: avformat.h:1065
avpriv_find_pix_fmt
enum AVPixelFormat avpriv_find_pix_fmt(const PixelFormatTag *tags, unsigned int fourcc)
Definition: utils.c:438
AVProgram::id
int id
Definition: avformat.h:1151
duration
int64_t duration
Definition: movenc.c:64
AVMutex
#define AVMutex
Definition: thread.h:164
av_dict_get
AVDictionaryEntry * av_dict_get(const AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
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:1656
avformat_query_codec
int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance)
Test if the given container can store a codec.
Definition: utils.c:5038
AVCodecContext::extradata_size
int extradata_size
Definition: avcodec.h:638
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:826
AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
@ AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS
Definition: packet.h:435
avcodec_alloc_context3
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:173
av_fast_realloc
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:478
AVChapter::end
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:1192
AVStreamInternal::display_aspect_ratio
AVRational display_aspect_ratio
display aspect ratio (0 if unknown)
Definition: internal.h:344
AVCodecDescriptor
This struct describes the properties of a single codec described by an AVCodecID.
Definition: codec_desc.h:38
s
#define s(width, name)
Definition: cbs_vp9.c:257
compute_chapters_end
static int compute_chapters_end(AVFormatContext *s)
Definition: utils.c:3238
av_seek_frame
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:2489
height
static int height
Definition: utils.c:158
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:198
AVFormatContext::flags
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1363
format
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 format(the sample packing is implied by the sample format) and sample rate. The lists are not just lists
AVCodecParameters::sample_aspect_ratio
AVRational sample_aspect_ratio
Video only.
Definition: codec_par.h:136
buffer_size_t
int buffer_size_t
Definition: internal.h:306
AVFormatContext::nb_programs
unsigned int nb_programs
Definition: avformat.h:1413
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:645
av_append_packet
int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
Read data and append it to the current content of the AVPacket.
Definition: utils.c:326
AVStreamInternal::codec_info_duration_fields
int64_t codec_info_duration_fields
Definition: internal.h:217
AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED
@ AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED
Definition: avcodec.h:242
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:443
frame_size
int frame_size
Definition: mxfenc.c:2206
AVStreamInternal::pts_wrap_behavior
int pts_wrap_behavior
Options for behavior, when a wrap is detected.
Definition: internal.h:310
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecParameters::width
int width
Video only.
Definition: codec_par.h:126
AVCodecContext::ticks_per_frame
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:668
AV_CODEC_ID_MP2
@ AV_CODEC_ID_MP2
Definition: codec_id.h:424
av_q2d
static double av_q2d(AVRational a)
Convert an AVRational to a double.
Definition: rational.h:104
AVIndexEntry::size
int size
Definition: avformat.h:814
ts_to_samples
static int64_t ts_to_samples(AVStream *st, int64_t ts)
Definition: utils.c:1534
AVOpenCallback
int(* AVOpenCallback)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, const AVIOInterruptCB *int_cb, AVDictionary **options)
Definition: avformat.h:1203
av_opt_set_dict_val
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:725
av_buffer_default_free
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:62
avpriv_h264_has_num_reorder_frames
int avpriv_h264_has_num_reorder_frames(AVCodecContext *avctx)
Definition: h264dec.c:61
avcodec_receive_frame
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: decode.c:652
AVIndexEntry::timestamp
int64_t timestamp
Timestamp in AVStream.time_base units, preferably the time from which on correctly decoded frames are...
Definition: avformat.h:805
AVOutputFormat::audio_codec
enum AVCodecID audio_codec
default audio codec
Definition: avformat.h:501
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVCodecDescriptor::type
enum AVMediaType type
Definition: codec_desc.h:40
AVIO_FLAG_WRITE
#define AVIO_FLAG_WRITE
write-only
Definition: avio.h:675
ff_find_stream_index
int ff_find_stream_index(AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: utils.c:5029
av_read_play
int av_read_play(AVFormatContext *s)
Start playing a network-based stream (e.g.
Definition: utils.c:4298
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
AVPacketSideData::data
uint8_t * data
Definition: packet.h:307
AVStream::need_parsing
enum AVStreamParseType need_parsing
Definition: avformat.h:1081
channels
channels
Definition: aptx.h:33
AVBSFContext::time_base_in
AVRational time_base_in
The timebase used for the timestamps of the input packets.
Definition: bsf.h:89
AVStream::parser
struct AVCodecParserContext * parser
Definition: avformat.h:1082
avformat_flush
int avformat_flush(AVFormatContext *s)
Discard all internally buffered data.
Definition: utils.c:2569
nb_streams
static int nb_streams
Definition: ffprobe.c:283
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
AV_CODEC_ID_PCM_U16BE
@ AV_CODEC_ID_PCM_U16BE
Definition: codec_id.h:316
AVOutputFormat::codec_tag
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:516
AVIndexEntry::min_distance
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:815
AV_CODEC_ID_CODEC2
@ AV_CODEC_ID_CODEC2
Definition: codec_id.h:491
AVStreamInternal::pkt
AVPacket * pkt
Definition: internal.h:193
codec_id
enum AVCodecID codec_id
Definition: vaapi_decode.c:369
AVProgram::start_time
int64_t start_time
Definition: avformat.h:1170
key
const char * key
Definition: hwcontext_opencl.c:168
ff_read_timestamp
static int64_t ff_read_timestamp(AVFormatContext *s, int stream_index, int64_t *ppos, int64_t pos_limit, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Definition: utils.c:2136
AVMEDIA_TYPE_DATA
@ AVMEDIA_TYPE_DATA
Opaque data information usually continuous.
Definition: avutil.h:203
AVStreamInternal::update_initial_durations_done
int update_initial_durations_done
Internal data to prevent doing update_initial_durations() twice.
Definition: internal.h:315
f
#define f(width, name)
Definition: cbs_vp9.c:255
AV_CODEC_ID_H264
@ AV_CODEC_ID_H264
Definition: codec_id.h:76
ff_hex_to_data
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition: utils.c:4917
AVStreamInternal::avctx_inited
int avctx_inited
1 if avctx has been initialized with the values from the codec parameters
Definition: internal.h:184
AVFMT_NEEDNUMBER
#define AVFMT_NEEDNUMBER
Needs 'd' in filename.
Definition: avformat.h:459
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:546
int32_t
int32_t
Definition: audio_convert.c:194
match_stream_specifier
static int match_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec, const char **indexptr, AVProgram **p)
Matches a stream specifier (but ignores requested index).
Definition: utils.c:5172
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:1408
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:1369
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
AVStreamInternal::duration_gcd
int64_t duration_gcd
Definition: internal.h:212
if
if(ret)
Definition: filter_design.txt:179
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: utils.c:1423
AVCodecParserContext::repeat_pict
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:3395
context
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 keep it simple and lowercase description are in without and describe what they for example set the foo of the bar offset is the offset of the field in your context
Definition: writing_filters.txt:91
AVDISCARD_ALL
@ AVDISCARD_ALL
discard all
Definition: avcodec.h:236
AVFormatContext
Format I/O context.
Definition: avformat.h:1232
AV_CODEC_PROP_INTRA_ONLY
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
Definition: codec_desc.h:72
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:352
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:1038
AVStreamInternal::duration_count
int duration_count
Definition: internal.h:213
ff_format_output_open
int ff_format_output_open(AVFormatContext *s, const char *url, AVDictionary **options)
Utility function to open IO stream of output format.
Definition: utils.c:5682
AVSEEK_FLAG_BACKWARD
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:2415
AVStreamInternal::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:288
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
extract_extradata
static int extract_extradata(AVStream *st, const AVPacket *pkt)
Definition: utils.c:3537
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:902
flush
static void flush(AVCodecContext *avctx)
Definition: aacdec_template.c:592
NULL
#define NULL
Definition: coverity.c:32
av_match_list
int av_match_list(const char *name, const char *list, char separator)
Check if a name is in a list.
Definition: avstring.c:452
AVERROR_PATCHWELCOME
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
AVStreamInternal::codec_info_duration
int64_t codec_info_duration
Definition: internal.h:216
AVFMTCTX_NOHEADER
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:1177
AV_CODEC_ID_PCM_U24BE
@ AV_CODEC_ID_PCM_U24BE
Definition: codec_id.h:328
ff_index_search_timestamp
int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries, int64_t wanted_timestamp, int flags)
Internal version of av_index_search_timestamp.
Definition: utils.c:2022
duration_estimate_name
static const char * duration_estimate_name(enum AVDurationEstimationMethod method)
Definition: utils.c:2911
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:1494
estimate_timings_from_pts
static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:2772
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:188
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:1212
AV_CODEC_ID_PCM_U32BE
@ AV_CODEC_ID_PCM_U32BE
Definition: codec_id.h:324
AVCodecContext::nb_coded_side_data
int nb_coded_side_data
Definition: avcodec.h:2194
AVFormatContext::protocol_whitelist
char * protocol_whitelist
',' separated list of allowed protocols.
Definition: avformat.h:1806
AVCodecContext::bit_rate
int64_t bit_rate
the average bitrate
Definition: avcodec.h:586
AVStreamInternal::frame_delay_evidence
int frame_delay_evidence
Definition: internal.h:218
AVPacketSideData::type
enum AVPacketSideDataType type
Definition: packet.h:313
AV_DISPOSITION_COMMENT
#define AV_DISPOSITION_COMMENT
Definition: avformat.h:821
av_stream_add_side_data
int av_stream_add_side_data(AVStream *st, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as stream side data.
Definition: utils.c:5522
av_opt_set_from_string
int av_opt_set_from_string(void *ctx, const char *opts, const char *const *shorthand, const char *key_val_sep, const char *pairs_sep)
Parse the key-value pairs list in opts.
Definition: opt.c:1559
AVPALETTE_SIZE
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
AVStreamInternal::skip_to_keyframe
int skip_to_keyframe
Indicates that everything up to the next keyframe should be discarded.
Definition: internal.h:253
AVSTREAM_PARSE_NONE
@ AVSTREAM_PARSE_NONE
Definition: avformat.h:793
seek_frame_internal
static int seek_frame_internal(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: utils.c:2445
AVIndexEntry::flags
int flags
Definition: avformat.h:813
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:274
ff_id3v2_parse_apic
int ff_id3v2_parse_apic(AVFormatContext *s, ID3v2ExtraMeta *extra_meta)
Create a stream for each APIC (attached picture) extracted from the ID3v2 header.
Definition: id3v2.c:1142
AVCodecContext::subtitle_header_size
int subtitle_header_size
Definition: avcodec.h:2017
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1274
AVStreamInternal::dts_misordered
uint8_t dts_misordered
Definition: internal.h:332
src
#define src
Definition: vp8dsp.c:255
AVERROR_BSF_NOT_FOUND
#define AVERROR_BSF_NOT_FOUND
Bitstream filter not found.
Definition: error.h:49
AV_CODEC_ID_PCM_S64BE
@ AV_CODEC_ID_PCM_S64BE
Definition: codec_id.h:346
parseutils.h
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:1210
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:441
AVProgram::stream_index
unsigned int * stream_index
Definition: avformat.h:1154
AVBitStreamFilter::priv_class
const AVClass * priv_class
A class for the private data, used to declare bitstream filter private AVOptions.
Definition: bsf.h:117
AVStream::metadata
AVDictionary * metadata
Definition: avformat.h:937
AV_ROUND_NEAR_INF
@ AV_ROUND_NEAR_INF
Round to nearest and halfway cases away from zero.
Definition: mathematics.h:84
has_duration
static int has_duration(AVFormatContext *ic)
Return TRUE if the stream has accurate duration in any stream.
Definition: utils.c:2582
avio_pause
int avio_pause(AVIOContext *h, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e....
Definition: aviobuf.c:1224
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1611
ff_parse_key_value
void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf, void *context)
Parse a string with comma-separated key=value pairs.
Definition: utils.c:4975
PacketList::next
struct PacketList * next
Definition: packet_internal.h:28
av_parse_time
int av_parse_time(int64_t *timeval, const char *timestr, int duration)
Parse timestr and return in *time a corresponding number of microseconds.
Definition: parseutils.c:587
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:144
AVCodecParserContext::flags
int flags
Definition: avcodec.h:3410
time.h
ff_get_formatted_ntp_time
uint64_t ff_get_formatted_ntp_time(uint64_t ntp_time_us)
Get the NTP time stamp formatted as per the RFC-5905.
Definition: utils.c:4714
AVFormatContext::skip_estimate_duration_from_pts
int skip_estimate_duration_from_pts
Skip duration calcuation in estimate_timings_from_pts.
Definition: avformat.h:1855
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:641
force_codec_ids
static void force_codec_ids(AVFormatContext *s, AVStream *st)
Definition: utils.c:673
index
int index
Definition: gxfenc.c:89
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
AVPALETTE_COUNT
#define AVPALETTE_COUNT
Definition: pixfmt.h:33
AVCodecParameters::sample_rate
int sample_rate
Audio only.
Definition: codec_par.h:170
ff_get_extradata
int ff_get_extradata(AVFormatContext *s, AVCodecParameters *par, AVIOContext *pb, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0 and f...
Definition: utils.c:3332
AV_CODEC_ID_CDGRAPHICS
@ AV_CODEC_ID_CDGRAPHICS
Definition: codec_id.h:181
av_bprint_is_complete
static int av_bprint_is_complete(const AVBPrint *buf)
Test if the print buffer is complete (not truncated).
Definition: bprint.h:185
AVStream::nb_frames
int64_t nb_frames
number of frames in this stream if known or 0
Definition: avformat.h:924
AVCodecID
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: codec_id.h:46
AV_CODEC_ID_EAC3
@ AV_CODEC_ID_EAC3
Definition: codec_id.h:464
AVCodecParameters::extradata_size
int extradata_size
Size of the extradata content in bytes.
Definition: codec_par.h:78
AV_AUDIO_SERVICE_TYPE_KARAOKE
@ AV_AUDIO_SERVICE_TYPE_KARAOKE
Definition: avcodec.h:248
AVStreamInternal::fps_first_dts_idx
int fps_first_dts_idx
Definition: internal.h:233
AVFormatContext::nb_streams
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:1288
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:104
AV_CODEC_ID_AAC
@ AV_CODEC_ID_AAC
Definition: codec_id.h:426
AVOutputFormat::flags
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS,...
Definition: avformat.h:510
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:1368
av_strncasecmp
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:225
set_codec_from_probe_data
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd)
Definition: utils.c:340
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:659
av_codec_is_decoder
int av_codec_is_decoder(const AVCodec *codec)
Definition: utils.c:79
has_codec_parameters
static int has_codec_parameters(AVStream *st, const char **errmsg_ptr)
Definition: utils.c:2969
av_sat_sub64
#define av_sat_sub64
Definition: common.h:167
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
AVCodecContext::lowres
int lowres
low resolution decoding, 1-> 1/2 size, 2->1/4 size
Definition: avcodec.h:1754
av_get_bits_per_sample
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:630
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: utils.c:3602
ff_standardize_creation_time
int ff_standardize_creation_time(AVFormatContext *s)
Standardize creation_time metadata in AVFormatContext to an ISO-8601 timestamp string.
Definition: utils.c:5721
get_next_pkt
static PacketList * get_next_pkt(AVFormatContext *s, AVStream *st, PacketList *pktl)
Definition: utils.c:1035
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
tb_unreliable
static int tb_unreliable(AVCodecContext *c)
Definition: utils.c:3299
AV_CODEC_ID_PCM_S24LE
@ AV_CODEC_ID_PCM_S24LE
Definition: codec_id.h:325
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
AVStreamInternal::rfps_duration_sum
int64_t rfps_duration_sum
Definition: internal.h:214
AVMediaType
AVMediaType
Definition: avutil.h:199
AVCodecParserContext::frame_offset
int64_t frame_offset
Definition: avcodec.h:3380
AV_PTS_WRAP_SUB_OFFSET
#define AV_PTS_WRAP_SUB_OFFSET
subtract the format specific offset on wrap detection
Definition: avformat.h:864
AVPacket::size
int size
Definition: packet.h:370
avformat_match_stream_specifier
int avformat_match_stream_specifier(AVFormatContext *s, AVStream *st, const char *spec)
Check if the stream st contained in s is matched by the stream specifier spec.
Definition: utils.c:5326
id
enum AVCodecID id
Definition: extract_extradata_bsf.c:325
avformat_alloc_context
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:211
seek_frame_byte
static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags)
Definition: utils.c:2357
AVIOContext::seekable
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:260
AVStream::nb_index_entries
int nb_index_entries
Definition: avformat.h:1092
FFFrac::val
int64_t val
Definition: internal.h:60
AVStreamInternal::extract_extradata
struct AVStreamInternal::@259 extract_extradata
AVStreamInternal::fps_last_dts_idx
int fps_last_dts_idx
Definition: internal.h:235
AVProgram::end_time
int64_t end_time
Definition: avformat.h:1171
AVStreamInternal::inited
int inited
Definition: internal.h:194
ff_copy_whiteblacklists
int ff_copy_whiteblacklists(AVFormatContext *dst, const AVFormatContext *src)
Copies the whilelists from one context to the other.
Definition: utils.c:160
AVStreamInternal::request_probe
int request_probe
stream probing state -1 -> probing finished 0 -> no probing requested rest -> perform probing with re...
Definition: internal.h:248
start_time
static int64_t start_time
Definition: ffplay.c:332
FFMAX
#define FFMAX(a, b)
Definition: common.h:103
bps
unsigned bps
Definition: movenc.c:1612
AV_CODEC_ID_DTS
@ AV_CODEC_ID_DTS
Definition: codec_id.h:428
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:59
sample
#define sample
Definition: flacdsp_template.c:44
AV_MUTEX_INITIALIZER
#define AV_MUTEX_INITIALIZER
Definition: thread.h:165
ff_get_pcm_codec_id
enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
Select a PCM codec based on the given parameters.
Definition: utils.c:3143
AVStream::probe_packets
int probe_packets
Number of packets to buffer for codec probing.
Definition: avformat.h:1073
size
int size
Definition: twinvq_data.h:10344
avpriv_codec_get_cap_skip_frame_fill_param
int avpriv_codec_get_cap_skip_frame_fill_param(const AVCodec *codec)
Definition: utils.c:467
AVStreamInternal::found_decoder
int found_decoder
0 -> decoder has not been searched for yet.
Definition: internal.h:225
ID3v2_DEFAULT_MAGIC
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
copy_tb
int copy_tb
Definition: ffmpeg_opt.c:165
avformat_seek_file
int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
Definition: utils.c:2512
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
NTP_OFFSET_US
#define NTP_OFFSET_US
Definition: internal.h:400
AVStream::event_flags
int event_flags
Flags indicating events happening on the stream, a combination of AVSTREAM_EVENT_FLAG_*.
Definition: avformat.h:992
av_guess_frame_rate
AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *frame)
Guess the frame rate, based on both the container and codec information.
Definition: utils.c:5142
av_stream_get_codec_timebase
AVRational av_stream_get_codec_timebase(const AVStream *st)
Get the internal codec timebase from a stream.
Definition: utils.c:5850
AV_PTS_WRAP_ADD_OFFSET
#define AV_PTS_WRAP_ADD_OFFSET
add the format specific offset on wrap detection
Definition: avformat.h:863
ff_is_intra_only
int ff_is_intra_only(enum AVCodecID id)
Definition: utils.c:1006
determinable_frame_size
static int determinable_frame_size(AVCodecContext *avctx)
Definition: utils.c:927
ffio_limit
int ffio_limit(AVIOContext *s, int size)
Definition: utils.c:244
AVFMT_FLAG_IGNDTS
#define AVFMT_FLAG_IGNDTS
Ignore DTS on frames that contain both DTS & PTS.
Definition: avformat.h:1367
av_probe_input_buffer2
int av_probe_input_buffer2(AVIOContext *pb, ff_const59 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:222
AVFMT_NOFILE
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:458
AVTimebaseSource
AVTimebaseSource
Definition: avformat.h:2976
AVStreamInternal::pts_wrap_reference
int64_t pts_wrap_reference
Internal data to check for wrapping of the time stamp.
Definition: internal.h:298
AVMEDIA_TYPE_UNKNOWN
@ AVMEDIA_TYPE_UNKNOWN
Usually treated as AVMEDIA_TYPE_DATA.
Definition: avutil.h:200
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:273
AVOption::name
const char * name
Definition: opt.h:249
ff_unlock_avformat
int ff_unlock_avformat(void)
Definition: utils.c:84
AVStream::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:935
ff_stream_add_bitstream_filter
int ff_stream_add_bitstream_filter(AVStream *st, const char *name, const char *args)
Add a bitstream filter to a stream.
Definition: utils.c:5576
AV_DISPOSITION_HEARING_IMPAIRED
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:831
AV_OPT_SEARCH_CHILDREN
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:560
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
av_isdigit
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:211
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
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
AV_CODEC_ID_RV30
@ AV_CODEC_ID_RV30
Definition: codec_id.h:117
av_format_inject_global_side_data
void av_format_inject_global_side_data(AVFormatContext *s)
This function will cause global side data to be injected in the next packet of each stream as well as...
Definition: utils.c:150
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
AVPacketSideDataType
AVPacketSideDataType
Definition: packet.h:40
ff_mutex_lock
static int ff_mutex_lock(AVMutex *mutex)
Definition: thread.h:168
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:696
AVPacket::flags
int flags
A combination of AV_PKT_FLAG values.
Definition: packet.h:375
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
av_dict_free
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values.
Definition: dict.c:203
bitrate
int64_t bitrate
Definition: h264_levels.c:131
a0
#define a0
Definition: regdef.h:46
PacketList::pkt
AVPacket pkt
Definition: packet_internal.h:27
flush_packet_queue
static void flush_packet_queue(AVFormatContext *s)
Definition: utils.c:1840
AVStreamInternal::pts_buffer
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition: internal.h:325
AV_FRAME_FILENAME_FLAGS_MULTIPLE
#define AV_FRAME_FILENAME_FLAGS_MULTIPLE
Allow multiple d.
Definition: avformat.h:2809
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1197
AVStream::side_data
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:975
AVCodec::id
enum AVCodecID id
Definition: codec.h:211
SANE_CHUNK_SIZE
#define SANE_CHUNK_SIZE
Definition: utils.c:242
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
avcodec_parameters_copy
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
Copy the contents of src to dst.
Definition: codec_par.c:72
AV_CODEC_ID_GIF
@ AV_CODEC_ID_GIF
Definition: codec_id.h:146
avcodec_get_name
const char * avcodec_get_name(enum AVCodecID id)
Get the name of a codec.
Definition: utils.c:471
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1740
avcodec_send_packet
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: decode.c:589
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:1192
av_assert2
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
Definition: avassert.h:64
AVIOContext::bytes_read
int64_t bytes_read
Bytes read statistic This field is internal to libavformat and access from outside is not allowed.
Definition: avio.h:279
AVFMT_FLAG_NOBUFFER
#define AVFMT_FLAG_NOBUFFER
Do not buffer frames when possible.
Definition: avformat.h:1370
AV_CODEC_ID_RV40
@ AV_CODEC_ID_RV40
Definition: codec_id.h:118
update_initial_timestamps
static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts, int64_t pts, AVPacket *pkt)
Definition: utils.c:1116
i
int i
Definition: input.c:407
AVCodecParserContext::pos
int64_t pos
Byte position of currently parsed frame in stream.
Definition: avcodec.h:3487
AVStreamInternal
Definition: internal.h:158
fill_all_stream_timings
static void fill_all_stream_timings(AVFormatContext *ic)
Definition: utils.c:2696
PARSER_FLAG_COMPLETE_FRAMES
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:3411
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:47
av_bsf_receive_packet
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt)
Retrieve a filtered packet.
Definition: bsf.c:227
AVOutputFormat
Definition: avformat.h:490
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:362
avio_internal.h
update_stream_timings
static void update_stream_timings(AVFormatContext *ic)
Estimate the stream timings from the one of each components.
Definition: utils.c:2602
AVCodecContext::properties
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:2183
width
static int width
Definition: utils.c:158
avformat_open_input
int avformat_open_input(AVFormatContext **ps, const char *filename, ff_const59 AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:512
AVChapter::id
int64_t id
unique ID to identify the chapter
Definition: avformat.h:1189
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:637
AVStreamInternal::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:267
ff_read_packet
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: utils.c:811
internal.h
AVCodecParameters::height
int height
Definition: codec_par.h:127
avcodec_parameters_to_context
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: codec_par.c:147
AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED
@ AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED
Definition: avcodec.h:243
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:1114
AV_AUDIO_SERVICE_TYPE_EFFECTS
@ AV_AUDIO_SERVICE_TYPE_EFFECTS
Definition: avcodec.h:241
update_stream_avctx
static int update_stream_avctx(AVFormatContext *s)
Definition: utils.c:477
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:53
AVFormatContext::codec_whitelist
char * codec_whitelist
',' separated list of allowed decoders.
Definition: avformat.h:1685
ff_add_index_entry
int ff_add_index_entry(AVIndexEntry **index_entries, int *nb_index_entries, unsigned int *index_entries_allocated_size, int64_t pos, int64_t timestamp, int size, int distance, int flags)
Internal version of av_add_index_entry.
Definition: utils.c:1954
avformat_mutex
static AVMutex avformat_mutex
Definition: utils.c:55
delta
float delta
Definition: vorbis_enc_data.h:457
AV_ROUND_DOWN
@ AV_ROUND_DOWN
Round toward -infinity.
Definition: mathematics.h:82
AV_CODEC_ID_PCM_F64BE
@ AV_CODEC_ID_PCM_F64BE
Definition: codec_id.h:335
AV_CODEC_ID_HEVC
@ AV_CODEC_ID_HEVC
Definition: codec_id.h:223
AV_DISPOSITION_KARAOKE
#define AV_DISPOSITION_KARAOKE
Definition: avformat.h:823
av_toupper
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:236
AVMEDIA_TYPE_ATTACHMENT
@ AVMEDIA_TYPE_ATTACHMENT
Opaque data information usually sparse.
Definition: avutil.h:205
av_url_split
void av_url_split(char *proto, int proto_size, char *authorization, int authorization_size, char *hostname, int hostname_size, int *port_ptr, char *path, int path_size, const char *url)
Split a URL string into components.
Definition: utils.c:4799
AV_CODEC_ID_PCM_S32BE
@ AV_CODEC_ID_PCM_S32BE
Definition: codec_id.h:322
url.h
uint8_t
uint8_t
Definition: audio_convert.c:194
av_find_default_stream_index
int av_find_default_stream_index(AVFormatContext *s)
Definition: utils.c:1854
AVPROBE_SCORE_RETRY
#define AVPROBE_SCORE_RETRY
Definition: avformat.h:448
av_get_audio_frame_duration
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition: utils.c:855
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:237
AVProgram
New fields can be added to the end with minor version bumps.
Definition: avformat.h:1150
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:51
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:204
av_inv_q
static av_always_inline AVRational av_inv_q(AVRational q)
Invert a rational.
Definition: rational.h:159
AV_PKT_DATA_SKIP_SAMPLES
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
Definition: packet.h:156
av_read_pause
int av_read_pause(AVFormatContext *s)
Pause a network-based stream (e.g.
Definition: utils.c:4307
len
int len
Definition: vorbis_enc_data.h:452
AV_CODEC_ID_JPEG2000
@ AV_CODEC_ID_JPEG2000
Definition: codec_id.h:137
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:310
AVStreamInternal::fps_first_dts
int64_t fps_first_dts
Those are used for average framerate estimation.
Definition: internal.h:232
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:709
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
av_opt_next
const AVOption * av_opt_next(const void *obj, const AVOption *last)
Iterate over all AVOptions belonging to obj.
Definition: opt.c:45
update_initial_durations
static void update_initial_durations(AVFormatContext *s, AVStream *st, int stream_index, int64_t duration)
Definition: utils.c:1169
av_packet_new_side_data
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, buffer_size_t size)
Definition: avpacket.c:343
init_input
static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
Definition: utils.c:423
av_codec_iterate
const AVCodec * av_codec_iterate(void **opaque)
Iterate over all registered codecs.
Definition: allcodecs.c:859
AVCodecParameters::field_order
enum AVFieldOrder field_order
Video only.
Definition: codec_par.h:141
AVCodecParserContext
Definition: avcodec.h:3377
AVStreamInternal::probe_data
AVProbeData probe_data
Definition: internal.h:346
AVBSFContext::priv_data
void * priv_data
Opaque filter-specific private data.
Definition: bsf.h:70
ff_tls_deinit
void ff_tls_deinit(void)
Definition: network.c:46
av_uninit
#define av_uninit(x)
Definition: attributes.h:154
AVStream::disposition
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:926
AVFMT_FLAG_GENPTS
#define AVFMT_FLAG_GENPTS
Generate missing pts even if it requires parsing future frames.
Definition: avformat.h:1364
AV_DISPOSITION_VISUAL_IMPAIRED
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:832
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: utils.c:1941
tag
uint32_t tag
Definition: movenc.c:1611
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:880
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:873
pixfmt.h
avio_seek
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:253
frame
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
Definition: filter_design.txt:264
FAIL
#define FAIL(errmsg)
avcodec_find_decoder
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: allcodecs.c:946
avpriv_packet_list_get
int avpriv_packet_list_get(PacketList **pkt_buffer, PacketList **pkt_buffer_end, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
Definition: avpacket.c:790
AVStream::nb_side_data
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:979
ff_bprint_to_codecpar_extradata
int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf)
Finalize buf into extradata and set its size appropriately.
Definition: utils.c:5755
lowercase
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 keep it simple and lowercase description are in lowercase
Definition: writing_filters.txt:89
AVSTREAM_PARSE_HEADERS
@ AVSTREAM_PARSE_HEADERS
Only parse headers, do not repack.
Definition: avformat.h:795
pos
unsigned int pos
Definition: spdifenc.c:412
avformat.h
dict.h
AVPacket::side_data
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: packet.h:380
network.h
AV_INPUT_BUFFER_PADDING_SIZE
#define AV_INPUT_BUFFER_PADDING_SIZE
Definition: avcodec.h:215
get_std_framerate
static int get_std_framerate(int i)
Definition: utils.c:3275
av_bsf_send_packet
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt)
Submit a packet for filtering.
Definition: bsf.c:201
av_sat_add64
#define av_sat_add64
Definition: common.h:164
AV_RL32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_RL32
Definition: bytestream.h:92
AV_AUDIO_SERVICE_TYPE_COMMENTARY
@ AV_AUDIO_SERVICE_TYPE_COMMENTARY
Definition: avcodec.h:245
AVStreamInternal::fps_last_dts
int64_t fps_last_dts
Definition: internal.h:234
estimate_timings_from_bit_rate
static void estimate_timings_from_bit_rate(AVFormatContext *ic)
Definition: utils.c:2715
avformat_network_deinit
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:5068
av_program_add_stream_index
void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition: utils.c:4681
ff_gen_search
int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts, int64_t pos_min, int64_t pos_max, int64_t pos_limit, int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret, int64_t(*read_timestamp)(struct AVFormatContext *, int, int64_t *, int64_t))
Perform a binary search using read_timestamp().
Definition: utils.c:2251
av_dynarray_add_nofree
int av_dynarray_add_nofree(void *tab_ptr, int *nb_ptr, void *elem)
Add an element to a dynamic array.
Definition: mem.c:296
wrap_timestamp
static int64_t wrap_timestamp(const AVStream *st, int64_t timestamp)
Wrap a given time stamp, if there is an indication for an overflow.
Definition: utils.c:102
AVCodecContext
main external API structure.
Definition: avcodec.h:536
AVStream::index
int index
stream index in AVFormatContext
Definition: avformat.h:874
avformat_transfer_internal_stream_timing_info
int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt, AVStream *ost, const AVStream *ist, enum AVTimebaseSource copy_tb)
Transfer internal timing information from one stream to another.
Definition: utils.c:5778
AVStreamInternal::pts_reorder_error_count
uint8_t pts_reorder_error_count[MAX_REORDER_DELAY+1]
Definition: internal.h:323
AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT
@ AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT
Definition: packet.h:432
AVStreamInternal::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:275
AVIO_SEEKABLE_NORMAL
#define AVIO_SEEKABLE_NORMAL
Seeking works like for a local file.
Definition: avio.h:40
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:276
AVBitStreamFilter
Definition: bsf.h:98
AVERROR_STREAM_NOT_FOUND
#define AVERROR_STREAM_NOT_FOUND
Stream not found.
Definition: error.h:65
AVInputFormat::flags
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_NOTIMESTAMPS,...
Definition: avformat.h:659
AV_PKT_DATA_NEW_EXTRADATA
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
Definition: packet.h:55
AVRational::den
int den
Denominator.
Definition: rational.h:60
ff_seek_frame_binary
int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
Perform a binary search using av_index_search_timestamp() and AVInputFormat.read_timestamp().
Definition: utils.c:2145
AV_PIX_FMT_NONE
@ AV_PIX_FMT_NONE
Definition: pixfmt.h:65
av_match_name
int av_match_name(const char *name, const char *names)
Match instances of a name in a comma-separated list of names.
Definition: avstring.c:353
avformat_free_context
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:4436
AVFMT_NOGENSEARCH
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:469
avio_read
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:633
ff_parse_key_val_cb
void(* ff_parse_key_val_cb)(void *context, const char *key, int key_len, char **dest, int *dest_len)
Callback function type for ff_parse_key_value.
Definition: internal.h:511
ff_compute_frame_duration
void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Return the frame duration in seconds.
Definition: utils.c:943
av_format_ffversion
const char av_format_ffversion[]
Definition: utils.c:53
estimate_timings
static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:2916
PARSER_FLAG_USE_CODEC_TS
#define PARSER_FLAG_USE_CODEC_TS
Definition: avcodec.h:3415
AV_CODEC_ID_PCM_U32LE
@ AV_CODEC_ID_PCM_U32LE
Definition: codec_id.h:323
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:107
AVOutputFormat::video_codec
enum AVCodecID video_codec
default video codec
Definition: avformat.h:502
temp
else temp
Definition: vf_mcdeint.c:259
AVStream::r_frame_rate
AVRational r_frame_rate
Real base framerate of the stream.
Definition: avformat.h:1015
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: utils.c:4945
has_decode_delay_been_guessed
static int has_decode_delay_been_guessed(AVStream *st)
Definition: utils.c:1017
AVIndexEntry::pos
int64_t pos
Definition: avformat.h:804
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:865
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:798
av_probe_input_format2
ff_const59 AVInputFormat * av_probe_input_format2(ff_const59 AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: format.c:205
AVFormatContext::duration
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1347
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:371
AVPROBE_SCORE_STREAM_RETRY
#define AVPROBE_SCORE_STREAM_RETRY
Definition: avformat.h:449
av_buffer_ref
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
DURATION_MAX_READ_SIZE
#define DURATION_MAX_READ_SIZE
Definition: utils.c:2768
avio_skip
int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: aviobuf.c:337
ff_format_set_url
void ff_format_set_url(AVFormatContext *s, char *url)
Set AVFormatContext url field to the provided pointer.
Definition: utils.c:5862
avformat_license
const char * avformat_license(void)
Return the libavformat license.
Definition: utils.c:73
av_stream_get_side_data
uint8_t * av_stream_get_side_data(const AVStream *st, enum AVPacketSideDataType type, buffer_size_t *size)
Definition: utils.c:5505
av_gettime
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:39
shift
static int shift(int a, int b)
Definition: sonic.c:82
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
AVStreamInternal::dts_ordered
uint8_t dts_ordered
Definition: internal.h:331
AVBSFContext::filter
const struct AVBitStreamFilter * filter
The bitstream filter this context is an instance of.
Definition: bsf.h:58
AVERROR_DECODER_NOT_FOUND
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:52
AVIO_FLAG_READ
#define AVIO_FLAG_READ
read-only
Definition: avio.h:674
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:724
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:544
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:253
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:120
AV_CODEC_ID_PCM_S32LE
@ AV_CODEC_ID_PCM_S32LE
Definition: codec_id.h:321
AVStreamInternal::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:200
packet_internal.h
ff_codec_get_tag
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:3121
AV_CODEC_ID_PCM_U8
@ AV_CODEC_ID_PCM_U8
Definition: codec_id.h:318
MAX_REORDER_DELAY
@ MAX_REORDER_DELAY
Definition: vaapi_encode.h:44
seek_frame_generic
static int seek_frame_generic(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: utils.c:2377
AV_FIELD_PROGRESSIVE
@ AV_FIELD_PROGRESSIVE
Definition: codec_par.h:38
llrint
#define llrint(x)
Definition: libm.h:394
AVCodecParameters::format
int format
Definition: codec_par.h:84
AVStreamInternal::orig_codec_id
enum AVCodecID orig_codec_id
Definition: internal.h:186
avformat_configuration
const char * avformat_configuration(void)
Return the libavformat build-time configuration.
Definition: utils.c:68
probe_codec
static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition: utils.c:695
read_frame_internal
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
Definition: utils.c:1539
AVStreamInternal::pts_reorder_error
int64_t pts_reorder_error[MAX_REORDER_DELAY+1]
Internal data to generate dts from pts.
Definition: internal.h:322
AV_CODEC_ID_PCM_F64LE
@ AV_CODEC_ID_PCM_F64LE
Definition: codec_id.h:336
diff
static av_always_inline int diff(const uint32_t a, const uint32_t b)
Definition: vf_palettegen.c:136
AVFormatInternal::packet_buffer_end
struct PacketList * packet_buffer_end
Definition: internal.h:77
av_free
#define av_free(p)
Definition: tableprint_vlc.h:34
AVDictionaryEntry
Definition: dict.h:81
AVCodecContext::codec_tag
unsigned int codec_tag
fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
Definition: avcodec.h:561
AV_ROUND_PASS_MINMAX
@ AV_ROUND_PASS_MINMAX
Flag telling rescaling functions to pass INT64_MIN/MAX through unchanged, avoiding special cases for ...
Definition: mathematics.h:108
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:60
AVPacket
This structure stores compressed data.
Definition: packet.h:346
AVStreamInternal::bsf
AVBSFContext * bsf
Definition: internal.h:192
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
av_dict_set
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
Definition: dict.c:70
av_dict_copy
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:217
AVStreamInternal::duration_error
double(* duration_error)[2][MAX_STD_TIMEBASES]
Definition: internal.h:215
find_probe_decoder
static const AVCodec * find_probe_decoder(AVFormatContext *s, const AVStream *st, enum AVCodecID codec_id)
Definition: utils.c:204
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:1126
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:389
AVCodecParameters::channel_layout
uint64_t channel_layout
Audio only.
Definition: codec_par.h:162
av_bsf_free
void av_bsf_free(AVBSFContext **pctx)
Free a bitstream filter context and everything associated with it; write NULL into the supplied point...
Definition: bsf.c:40
avio_find_protocol_name
const char * avio_find_protocol_name(const char *url)
Return the name of the protocol that will handle the passed URL.
Definition: avio.c:470
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:709
extract_extradata_check
static int extract_extradata_check(AVStream *st)
Definition: utils.c:3475
bytestream.h
convert_header.str
string str
Definition: convert_header.py:20
AVStreamInternal::last_dts
int64_t last_dts
Definition: internal.h:211
distance
static float distance(float x, float y, int band)
Definition: nellymoserenc.c:232
AV_CODEC_ID_PCM_U16LE
@ AV_CODEC_ID_PCM_U16LE
Definition: codec_id.h:315
timestamp.h
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
av_strlcpy
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
Definition: avstring.c:83
AV_CODEC_ID_PCM_F32LE
@ AV_CODEC_ID_PCM_F32LE
Definition: codec_id.h:334
AVCodecParameters::bit_rate
int64_t bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: codec_par.h:89
AVInputFormat::read_timestamp
int64_t(* read_timestamp)(struct AVFormatContext *s, int stream_index, int64_t *pos, int64_t pos_limit)
Get the next timestamp in stream[stream_index].time_base units.
Definition: avformat.h:745
CONTAINS_PAL
#define CONTAINS_PAL
Definition: internal.h:839
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:1273
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
AVFormatContext::start_time
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1337
AV_CODEC_ID_AAC_LATM
@ AV_CODEC_ID_AAC_LATM
Definition: codec_id.h:473
AV_CODEC_CAP_AVOID_PROBING
#define AV_CODEC_CAP_AVOID_PROBING
Decoder is not a preferred choice for probing.
Definition: codec.h:139
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
ff_parse_creation_time_metadata
int ff_parse_creation_time_metadata(AVFormatContext *s, int64_t *timestamp, int return_seconds)
Parse creation_time in AVFormatContext metadata if exists and warn if the parsing fails.
Definition: utils.c:5704
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
AVERROR_EXIT
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:56
AVBitStreamFilterContext
Definition: avcodec.h:4017
avcodec_descriptor_get
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:3501
AVDictionaryEntry::value
char * value
Definition: dict.h:83
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:912
PARSER_FLAG_ONCE
#define PARSER_FLAG_ONCE
Definition: avcodec.h:3412
AV_CODEC_ID_APTX
@ AV_CODEC_ID_APTX
Definition: codec_id.h:510
avstring.h
AVFormatInternal::pkt
AVPacket * pkt
Used to hold temporary packets.
Definition: internal.h:100
AVSTREAM_PARSE_TIMESTAMPS
@ AVSTREAM_PARSE_TIMESTAMPS
full parsing and interpolation of timestamps for frames not starting on a packet boundary
Definition: avformat.h:796
av_codec_get_id
enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
Definition: utils.c:3217
AVOutputFormat::query_codec
int(* query_codec)(enum AVCodecID id, int std_compliance)
Test if the given codec can be stored in this container.
Definition: avformat.h:568
AVDiscard
AVDiscard
Definition: avcodec.h:227
av_strndup
char * av_strndup(const char *s, size_t len)
Duplicate a substring of a string.
Definition: mem.c:265
AVStream::pts_wrap_bits
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:1055
AVInputFormat::read_header
int(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition: avformat.h:712
AVChapter::time_base
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:1191
AVCodecTag::tag
unsigned int tag
Definition: internal.h:44
AV_CODEC_ID_MPEG2VIDEO
@ AV_CODEC_ID_MPEG2VIDEO
preferred ID for MPEG-1/2 video decoding
Definition: codec_id.h:51
AVStream::codec_info_nb_frames
int codec_info_nb_frames
Number of frames that have been demuxed during avformat_find_stream_info()
Definition: avformat.h:1078
snprintf
#define snprintf
Definition: snprintf.h:34
AV_CODEC_ID_MP1
@ AV_CODEC_ID_MP1
Definition: codec_id.h:466
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1260
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
ff_alloc_extradata
int ff_alloc_extradata(AVCodecParameters *par, int size)
Allocate extradata with additional AV_INPUT_BUFFER_PADDING_SIZE at end which is always set to 0.
Definition: utils.c:3314
AV_CODEC_ID_PCM_S24BE
@ AV_CODEC_ID_PCM_S24BE
Definition: codec_id.h:326
avpriv_packet_list_put
int avpriv_packet_list_put(PacketList **packet_buffer, PacketList **plast_pktl, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Definition: avpacket.c:753
avpriv_dict_set_timestamp
int avpriv_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestamp)
Set a dictionary value to an ISO-8601 compliant timestamp string.
Definition: dict.c:258
chapter_start_cmp
static int chapter_start_cmp(const void *p1, const void *p2)
Definition: utils.c:3228
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
av_probe_input_format3
ff_const59 AVInputFormat * av_probe_input_format3(ff_const59 AVProbeData *pd, int is_opened, int *score_ret)
Guess the file format.
Definition: format.c:128
av_bsf_alloc
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **pctx)
Allocate a context for a given bitstream filter.
Definition: bsf.c:95
avpriv_packet_list_free
void avpriv_packet_list_free(PacketList **pkt_buf, PacketList **pkt_buf_end)
Wipe the list and unref all the packets in it.
Definition: avpacket.c:806
AVStream::index_entries_allocated_size
unsigned int index_entries_allocated_size
Definition: avformat.h:1093
AVFMT_EVENT_FLAG_METADATA_UPDATED
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
Definition: avformat.h:1562
AVInputFormat::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:670
dec_ctx
static AVCodecContext * dec_ctx
Definition: filtering_audio.c:43
FF_API_LAVF_AVCTX
#define FF_API_LAVF_AVCTX
Definition: version.h:65
AVPacket::side_data_elems
int side_data_elems
Definition: packet.h:381
av_parser_close
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:228
ff_generate_avci_extradata
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: utils.c:5370
av_index_search_timestamp
int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:2130
av_find_best_stream
int av_find_best_stream(AVFormatContext *ic, enum AVMediaType type, int wanted_stream_nb, int related_stream, AVCodec **decoder_ret, int flags)
Find the "best" stream in the file.
Definition: utils.c:4230
append_packet_chunked
static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
Definition: utils.c:271
AVStream::last_IP_pts
int64_t last_IP_pts
Definition: avformat.h:1067