Libav
utils.c
Go to the documentation of this file.
1 /*
2  * various utility functions for use within Libav
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #undef NDEBUG
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdint.h>
26 
27 #include "config.h"
28 
29 #include "libavutil/avassert.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/dict.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/mathematics.h"
34 #include "libavutil/opt.h"
35 #include "libavutil/parseutils.h"
36 #include "libavutil/pixdesc.h"
37 #include "libavutil/time.h"
38 
39 #include "libavcodec/bytestream.h"
40 #include "libavcodec/internal.h"
41 
42 #include "audiointerleave.h"
43 #include "avformat.h"
44 #include "id3v2.h"
45 #include "internal.h"
46 #include "metadata.h"
47 #if CONFIG_NETWORK
48 #include "network.h"
49 #endif
50 #include "riff.h"
51 #include "url.h"
52 
58 unsigned avformat_version(void)
59 {
61 }
62 
63 const char *avformat_configuration(void)
64 {
65  return LIBAV_CONFIGURATION;
66 }
67 
68 const char *avformat_license(void)
69 {
70 #define LICENSE_PREFIX "libavformat license: "
71  return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
72 }
73 
74 /* an arbitrarily chosen "sane" max packet size -- 50M */
75 #define SANE_CHUNK_SIZE (50000000)
76 
77 /* Read the data in sane-sized chunks and append to pkt.
78  * Return the number of bytes read or an error. */
80 {
81  int64_t chunk_size = size;
82  int64_t orig_pos = pkt->pos; // av_grow_packet might reset pos
83  int orig_size = pkt->size;
84  int ret = 0;
85 
86  do {
87  int prev_size = pkt->size;
88  int read_size;
89 
90  /* When the caller requests a lot of data, limit it to the amount
91  * left in file or SANE_CHUNK_SIZE when it is not known. */
92  if (size > SANE_CHUNK_SIZE) {
93  int64_t filesize = avio_size(s) - avio_tell(s);
94  chunk_size = FFMAX(filesize, SANE_CHUNK_SIZE);
95  }
96  read_size = FFMIN(size, chunk_size);
97 
98  ret = av_grow_packet(pkt, read_size);
99  if (ret < 0)
100  break;
101 
102  ret = avio_read(s, pkt->data + prev_size, read_size);
103  if (ret != read_size) {
104  av_shrink_packet(pkt, prev_size + FFMAX(ret, 0));
105  break;
106  }
107 
108  size -= read_size;
109  } while (size > 0);
110 
111  pkt->pos = orig_pos;
112  if (!pkt->size)
113  av_packet_unref(pkt);
114  return pkt->size > orig_size ? pkt->size - orig_size : ret;
115 }
116 
118 {
119  av_init_packet(pkt);
120  pkt->data = NULL;
121  pkt->size = 0;
122  pkt->pos = avio_tell(s);
123 
124  return append_packet_chunked(s, pkt, size);
125 }
126 
128 {
129  if (!pkt->size)
130  return av_get_packet(s, pkt, size);
131  return append_packet_chunked(s, pkt, size);
132 }
133 
134 int av_filename_number_test(const char *filename)
135 {
136  char buf[1024];
137  return filename &&
138  (av_get_frame_filename(buf, sizeof(buf), filename, 1) >= 0);
139 }
140 
142  AVProbeData *pd, int score)
143 {
144  static const struct {
145  const char *name;
146  enum AVCodecID id;
147  enum AVMediaType type;
148  } fmt_id_type[] = {
150  { "ac3", AV_CODEC_ID_AC3, AVMEDIA_TYPE_AUDIO },
151  { "dts", AV_CODEC_ID_DTS, AVMEDIA_TYPE_AUDIO },
152  { "eac3", AV_CODEC_ID_EAC3, AVMEDIA_TYPE_AUDIO },
154  { "latm", AV_CODEC_ID_AAC_LATM, AVMEDIA_TYPE_AUDIO },
155  { "m4v", AV_CODEC_ID_MPEG4, AVMEDIA_TYPE_VIDEO },
156  { "mp3", AV_CODEC_ID_MP3, AVMEDIA_TYPE_AUDIO },
157  { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
158  { 0 }
159  };
160  AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
161 
162  if (fmt) {
163  int i;
164  av_log(s, AV_LOG_DEBUG,
165  "Probe with size=%d, packets=%d detected %s with score=%d\n",
167  fmt->name, score);
168  for (i = 0; fmt_id_type[i].name; i++) {
169  if (!strcmp(fmt->name, fmt_id_type[i].name)) {
170  st->codecpar->codec_id = fmt_id_type[i].id;
171  st->codecpar->codec_type = fmt_id_type[i].type;
172 #if FF_API_LAVF_AVCTX
174  st->codec->codec_type = st->codecpar->codec_type;
175  st->codec->codec_id = st->codecpar->codec_id;
177 #endif
178  break;
179  }
180  }
181  }
182  return !!fmt;
183 }
184 
185 /************************************************************/
186 /* input media file */
187 
188 /* Open input file and probe the format if necessary. */
189 static int init_input(AVFormatContext *s, const char *filename,
191 {
192  int ret;
193  AVProbeData pd = { filename, NULL, 0 };
194 
195  if (s->pb) {
197  if (!s->iformat)
198  return av_probe_input_buffer(s->pb, &s->iformat, filename,
199  s, 0, s->probesize);
200  else if (s->iformat->flags & AVFMT_NOFILE)
201  return AVERROR(EINVAL);
202  return 0;
203  }
204 
205  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
206  (!s->iformat && (s->iformat = av_probe_input_format(&pd, 0))))
207  return 0;
208 
209  ret = s->io_open(s, &s->pb, filename, AVIO_FLAG_READ, options);
210  if (ret < 0)
211  return ret;
212  if (s->iformat)
213  return 0;
214  return av_probe_input_buffer(s->pb, &s->iformat, filename,
215  s, 0, s->probesize);
216 }
217 
218 static int add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
219  AVPacketList **plast_pktl, int ref)
220 {
221  AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
222  int ret;
223 
224  if (!pktl)
225  return AVERROR(ENOMEM);
226 
227  if (ref) {
228  if ((ret = av_packet_ref(&pktl->pkt, pkt)) < 0) {
229  av_free(pktl);
230  return ret;
231  }
232  } else {
233  pktl->pkt = *pkt;
234  }
235 
236  if (*packet_buffer)
237  (*plast_pktl)->next = pktl;
238  else
239  *packet_buffer = pktl;
240 
241  /* Add the packet in the buffered packet list. */
242  *plast_pktl = pktl;
243  return 0;
244 }
245 
247 {
248  int i, ret;
249  for (i = 0; i < s->nb_streams; i++)
251  s->streams[i]->discard < AVDISCARD_ALL) {
252 
254  &s->streams[i]->attached_pic,
256  if (ret < 0)
257  return ret;
258  }
259  return 0;
260 }
261 
262 #if FF_API_LAVF_AVCTX
264 static int update_stream_avctx(AVFormatContext *s)
265 {
266  int i, ret;
267  for (i = 0; i < s->nb_streams; i++) {
268  AVStream *st = s->streams[i];
269 
270  if (!st->internal->need_codec_update)
271  continue;
272 
273  ret = avcodec_parameters_to_context(st->codec, st->codecpar);
274  if (ret < 0)
275  return ret;
276 
277  st->internal->need_codec_update = 0;
278  }
279  return 0;
280 }
282 #endif
283 
284 int avformat_open_input(AVFormatContext **ps, const char *filename,
286 {
287  AVFormatContext *s = *ps;
288  int i, ret = 0;
289  AVDictionary *tmp = NULL;
290  ID3v2ExtraMeta *id3v2_extra_meta = NULL;
291 
292  if (!s && !(s = avformat_alloc_context()))
293  return AVERROR(ENOMEM);
294  if (fmt)
295  s->iformat = fmt;
296 
297  if (options)
298  av_dict_copy(&tmp, *options, 0);
299 
300  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
301  goto fail;
302 
303  if ((ret = init_input(s, filename, &tmp)) < 0)
304  goto fail;
305 
306  /* Check filename in case an image number is expected. */
307  if (s->iformat->flags & AVFMT_NEEDNUMBER) {
308  if (!av_filename_number_test(filename)) {
309  ret = AVERROR(EINVAL);
310  goto fail;
311  }
312  }
313 
315  av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
316 
317  /* Allocate private data. */
318  if (s->iformat->priv_data_size > 0) {
319  if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
320  ret = AVERROR(ENOMEM);
321  goto fail;
322  }
323  if (s->iformat->priv_class) {
324  *(const AVClass **) s->priv_data = s->iformat->priv_class;
326  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
327  goto fail;
328  }
329  }
330 
331  /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
332  if (s->pb)
333  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
334 
335  if (s->iformat->read_header)
336  if ((ret = s->iformat->read_header(s)) < 0)
337  goto fail;
338 
339  if (id3v2_extra_meta &&
340  (ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0)
341  goto fail;
342  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
343 
344  if ((ret = queue_attached_pictures(s)) < 0)
345  goto fail;
346 
347  if (s->pb && !s->internal->data_offset)
348  s->internal->data_offset = avio_tell(s->pb);
349 
351 
352 #if FF_API_LAVF_AVCTX
353  update_stream_avctx(s);
354 #endif
355 
356  for (i = 0; i < s->nb_streams; i++)
358 
359  if (options) {
360  av_dict_free(options);
361  *options = tmp;
362  }
363  *ps = s;
364  return 0;
365 
366 fail:
367  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
368  av_dict_free(&tmp);
369  if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
370  avio_close(s->pb);
372  *ps = NULL;
373  return ret;
374 }
375 
376 /*******************************************************/
377 
378 static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
379 {
380  if (st->codecpar->codec_id == AV_CODEC_ID_PROBE) {
381  AVProbeData *pd = &st->probe_data;
382  av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
383  --st->probe_packets;
384 
385  if (pkt) {
386  int err;
387  if ((err = av_reallocp(&pd->buf, pd->buf_size + pkt->size +
388  AVPROBE_PADDING_SIZE)) < 0)
389  return err;
390  memcpy(pd->buf + pd->buf_size, pkt->data, pkt->size);
391  pd->buf_size += pkt->size;
392  memset(pd->buf + pd->buf_size, 0, AVPROBE_PADDING_SIZE);
393  } else {
394  st->probe_packets = 0;
395  if (!pd->buf_size) {
396  av_log(s, AV_LOG_ERROR,
397  "nothing to probe for stream %d\n", st->index);
398  return 0;
399  }
400  }
401 
402  if (!st->probe_packets ||
403  av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
404  set_codec_from_probe_data(s, st, pd, st->probe_packets > 0
405  ? AVPROBE_SCORE_MAX / 4 : 0);
406  if (st->codecpar->codec_id != AV_CODEC_ID_PROBE) {
407  pd->buf_size = 0;
408  av_freep(&pd->buf);
409  av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
410  }
411  }
412  }
413  return 0;
414 }
415 
417 {
418  int ret, i, err;
419  AVStream *st;
420 
421  for (;;) {
423 
424  if (pktl) {
425  *pkt = pktl->pkt;
426  st = s->streams[pkt->stream_index];
427  if (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
428  !st->probe_packets ||
430  AVProbeData *pd;
431  if (st->probe_packets)
432  if ((err = probe_codec(s, st, NULL)) < 0)
433  return err;
434  pd = &st->probe_data;
435  av_freep(&pd->buf);
436  pd->buf_size = 0;
437  s->internal->raw_packet_buffer = pktl->next;
439  av_free(pktl);
440  return 0;
441  }
442  }
443 
444  pkt->data = NULL;
445  pkt->size = 0;
446  av_init_packet(pkt);
447  ret = s->iformat->read_packet(s, pkt);
448  if (ret < 0) {
449  if (!pktl || ret == AVERROR(EAGAIN))
450  return ret;
451  for (i = 0; i < s->nb_streams; i++) {
452  st = s->streams[i];
453  if (st->probe_packets)
454  if ((err = probe_codec(s, st, NULL)) < 0)
455  return err;
456  }
457  continue;
458  }
459 
460  if (!pkt->buf) {
461  AVPacket tmp = { 0 };
462  ret = av_packet_ref(&tmp, pkt);
463  if (ret < 0)
464  return ret;
465  *pkt = tmp;
466  }
467 
468  if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
469  (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
471  "Dropped corrupted packet (stream = %d)\n",
472  pkt->stream_index);
473  av_packet_unref(pkt);
474  continue;
475  }
476 
477  st = s->streams[pkt->stream_index];
478 
479  switch (st->codecpar->codec_type) {
480  case AVMEDIA_TYPE_VIDEO:
481  if (s->video_codec_id)
482  st->codecpar->codec_id = s->video_codec_id;
483  break;
484  case AVMEDIA_TYPE_AUDIO:
485  if (s->audio_codec_id)
486  st->codecpar->codec_id = s->audio_codec_id;
487  break;
489  if (s->subtitle_codec_id)
491  break;
492  }
493 
494  if (!pktl && (st->codecpar->codec_id != AV_CODEC_ID_PROBE ||
495  !st->probe_packets))
496  return ret;
497 
498  err = add_to_pktbuf(&s->internal->raw_packet_buffer, pkt,
500  if (err)
501  return err;
503 
504  if ((err = probe_codec(s, st, pkt)) < 0)
505  return err;
506  }
507 }
508 
509 /**********************************************************/
510 
514 void ff_compute_frame_duration(AVFormatContext *s, int *pnum, int *pden, AVStream *st,
515  AVCodecParserContext *pc, AVPacket *pkt)
516 {
517  AVRational codec_framerate = s->iformat ? st->internal->avctx->framerate :
518  (AVRational){ 0, 1 };
519  int frame_size;
520 
521  *pnum = 0;
522  *pden = 0;
523  switch (st->codecpar->codec_type) {
524  case AVMEDIA_TYPE_VIDEO:
525  if (st->avg_frame_rate.num) {
526  *pnum = st->avg_frame_rate.den;
527  *pden = st->avg_frame_rate.num;
528  } else if (st->time_base.num * 1000LL > st->time_base.den) {
529  *pnum = st->time_base.num;
530  *pden = st->time_base.den;
531  } else if (codec_framerate.den * 1000LL > codec_framerate.num) {
532  *pnum = codec_framerate.den;
533  *pden = codec_framerate.num;
534  if (pc && pc->repeat_pict) {
535  if (*pnum > INT_MAX / (1 + pc->repeat_pict))
536  *pden /= 1 + pc->repeat_pict;
537  else
538  *pnum *= 1 + pc->repeat_pict;
539  }
540  /* If this codec can be interlaced or progressive then we need
541  * a parser to compute duration of a packet. Thus if we have
542  * no parser in such case leave duration undefined. */
543  if (st->internal->avctx->ticks_per_frame > 1 && !pc)
544  *pnum = *pden = 0;
545  }
546  break;
547  case AVMEDIA_TYPE_AUDIO:
548  frame_size = av_get_audio_frame_duration2(st->codecpar, pkt->size);
549  if (frame_size <= 0 || st->codecpar->sample_rate <= 0)
550  break;
551  *pnum = frame_size;
552  *pden = st->codecpar->sample_rate;
553  break;
554  default:
555  break;
556  }
557 }
558 
559 static int is_intra_only(enum AVCodecID id)
560 {
562  if (!d)
563  return 0;
565  return 0;
566  return 1;
567 }
568 
569 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
570  int64_t dts, int64_t pts)
571 {
572  AVStream *st = s->streams[stream_index];
573  AVPacketList *pktl = s->internal->packet_buffer;
574 
575  if (st->first_dts != AV_NOPTS_VALUE ||
576  dts == AV_NOPTS_VALUE ||
577  st->cur_dts == AV_NOPTS_VALUE)
578  return;
579 
580  st->first_dts = dts - st->cur_dts;
581  st->cur_dts = dts;
582 
583  for (; pktl; pktl = pktl->next) {
584  if (pktl->pkt.stream_index != stream_index)
585  continue;
586  // FIXME: think more about this check
587  if (pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
588  pktl->pkt.pts += st->first_dts;
589 
590  if (pktl->pkt.dts != AV_NOPTS_VALUE)
591  pktl->pkt.dts += st->first_dts;
592 
593  if (st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
594  st->start_time = pktl->pkt.pts;
595  }
596  if (st->start_time == AV_NOPTS_VALUE)
597  st->start_time = pts;
598 }
599 
601  int stream_index, int duration)
602 {
603  AVPacketList *pktl = s->internal->packet_buffer;
604  int64_t cur_dts = 0;
605 
606  if (st->first_dts != AV_NOPTS_VALUE) {
607  cur_dts = st->first_dts;
608  for (; pktl; pktl = pktl->next) {
609  if (pktl->pkt.stream_index == stream_index) {
610  if (pktl->pkt.pts != pktl->pkt.dts ||
611  pktl->pkt.dts != AV_NOPTS_VALUE ||
612  pktl->pkt.duration)
613  break;
614  cur_dts -= duration;
615  }
616  }
617  pktl = s->internal->packet_buffer;
618  st->first_dts = cur_dts;
619  } else if (st->cur_dts)
620  return;
621 
622  for (; pktl; pktl = pktl->next) {
623  if (pktl->pkt.stream_index != stream_index)
624  continue;
625  if (pktl->pkt.pts == pktl->pkt.dts &&
626  pktl->pkt.dts == AV_NOPTS_VALUE &&
627  !pktl->pkt.duration) {
628  pktl->pkt.dts = cur_dts;
629  if (!st->internal->avctx->has_b_frames)
630  pktl->pkt.pts = cur_dts;
631  cur_dts += duration;
633  pktl->pkt.duration = duration;
634  } else
635  break;
636  }
637  if (st->first_dts == AV_NOPTS_VALUE)
638  st->cur_dts = cur_dts;
639 }
640 
642  AVCodecParserContext *pc, AVPacket *pkt)
643 {
644  int num, den, presentation_delayed, delay, i;
645  int64_t offset;
646 
647  if (s->flags & AVFMT_FLAG_NOFILLIN)
648  return;
649 
650  if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
651  pkt->dts = AV_NOPTS_VALUE;
652 
653  /* do we have a video B-frame ? */
654  delay = st->internal->avctx->has_b_frames;
655  presentation_delayed = 0;
656 
657  /* XXX: need has_b_frame, but cannot get it if the codec is
658  * not initialized */
659  if (delay &&
660  pc && pc->pict_type != AV_PICTURE_TYPE_B)
661  presentation_delayed = 1;
662 
663  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
664  st->pts_wrap_bits < 63 &&
665  pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
666  pkt->dts -= 1LL << st->pts_wrap_bits;
667  }
668 
669  /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
670  * We take the conservative approach and discard both.
671  * Note: If this is misbehaving for an H.264 file, then possibly
672  * presentation_delayed is not set correctly. */
673  if (delay == 1 && pkt->dts == pkt->pts &&
674  pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
675  av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
676  pkt->dts = AV_NOPTS_VALUE;
677  }
678 
679  if (pkt->duration == 0 && st->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
680  ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
681  if (den && num) {
682  pkt->duration = av_rescale_rnd(1, num * (int64_t) st->time_base.den,
683  den * (int64_t) st->time_base.num,
684  AV_ROUND_DOWN);
685 
686  if (pkt->duration != 0 && s->internal->packet_buffer)
688  pkt->duration);
689  }
690  }
691 
692  /* Correct timestamps with byte offset if demuxers only have timestamps
693  * on packet boundaries */
694  if (pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
695  /* this will estimate bitrate based on this frame's duration and size */
696  offset = av_rescale(pc->offset, pkt->duration, pkt->size);
697  if (pkt->pts != AV_NOPTS_VALUE)
698  pkt->pts += offset;
699  if (pkt->dts != AV_NOPTS_VALUE)
700  pkt->dts += offset;
701  }
702 
703  /* This may be redundant, but it should not hurt. */
704  if (pkt->dts != AV_NOPTS_VALUE &&
705  pkt->pts != AV_NOPTS_VALUE &&
706  pkt->pts > pkt->dts)
707  presentation_delayed = 1;
708 
710  "IN delayed:%d pts:%"PRId64", dts:%"PRId64" "
711  "cur_dts:%"PRId64" st:%d pc:%p\n",
712  presentation_delayed, pkt->pts, pkt->dts, st->cur_dts,
713  pkt->stream_index, pc);
714  /* Interpolate PTS and DTS if they are not present. We skip H.264
715  * currently because delay and has_b_frames are not reliably set. */
716  if ((delay == 0 || (delay == 1 && pc)) &&
718  if (presentation_delayed) {
719  /* DTS = decompression timestamp */
720  /* PTS = presentation timestamp */
721  if (pkt->dts == AV_NOPTS_VALUE)
722  pkt->dts = st->last_IP_pts;
723  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
724  if (pkt->dts == AV_NOPTS_VALUE)
725  pkt->dts = st->cur_dts;
726 
727  /* This is tricky: the dts must be incremented by the duration
728  * of the frame we are displaying, i.e. the last I- or P-frame. */
729  if (st->last_IP_duration == 0)
730  st->last_IP_duration = pkt->duration;
731  if (pkt->dts != AV_NOPTS_VALUE)
732  st->cur_dts = pkt->dts + st->last_IP_duration;
733  st->last_IP_duration = pkt->duration;
734  st->last_IP_pts = pkt->pts;
735  /* Cannot compute PTS if not present (we can compute it only
736  * by knowing the future. */
737  } else if (pkt->pts != AV_NOPTS_VALUE ||
738  pkt->dts != AV_NOPTS_VALUE ||
739  pkt->duration ||
741  int duration = pkt->duration;
742  if (!duration && st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
743  ff_compute_frame_duration(s, &num, &den, st, pc, pkt);
744  if (den && num) {
745  duration = av_rescale_rnd(1,
746  num * (int64_t) st->time_base.den,
747  den * (int64_t) st->time_base.num,
748  AV_ROUND_DOWN);
749  if (duration != 0 && s->internal->packet_buffer)
751  duration);
752  }
753  }
754 
755  if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE ||
756  duration) {
757  /* presentation is not delayed : PTS and DTS are the same */
758  if (pkt->pts == AV_NOPTS_VALUE)
759  pkt->pts = pkt->dts;
761  pkt->pts);
762  if (pkt->pts == AV_NOPTS_VALUE)
763  pkt->pts = st->cur_dts;
764  pkt->dts = pkt->pts;
765  if (pkt->pts != AV_NOPTS_VALUE)
766  st->cur_dts = pkt->pts + duration;
767  }
768  }
769  }
770 
771  if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
772  st->pts_buffer[0] = pkt->pts;
773  for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
774  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
775  if (pkt->dts == AV_NOPTS_VALUE)
776  pkt->dts = st->pts_buffer[0];
777  // We skipped it above so we try here.
778  if (st->codecpar->codec_id == AV_CODEC_ID_H264)
779  // This should happen on the first packet
780  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
781  if (pkt->dts > st->cur_dts)
782  st->cur_dts = pkt->dts;
783  }
784 
786  "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n",
787  presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
788 
789  /* update flags */
790  if (is_intra_only(st->codecpar->codec_id))
791  pkt->flags |= AV_PKT_FLAG_KEY;
792 #if FF_API_CONVERGENCE_DURATION
794  if (pc)
797 #endif
798 }
799 
800 static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
801 {
802  while (*pkt_buf) {
803  AVPacketList *pktl = *pkt_buf;
804  *pkt_buf = pktl->next;
805  av_packet_unref(&pktl->pkt);
806  av_freep(&pktl);
807  }
808  *pkt_buf_end = NULL;
809 }
810 
816 static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
817 {
818  AVPacket out_pkt = { 0 }, flush_pkt = { 0 };
819  AVStream *st = s->streams[stream_index];
820  uint8_t *data = pkt ? pkt->data : NULL;
821  int size = pkt ? pkt->size : 0;
822  int ret = 0, got_output = 0;
823 
824  if (!pkt) {
826  pkt = &flush_pkt;
827  got_output = 1;
828  }
829 
830  while (size > 0 || (pkt == &flush_pkt && got_output)) {
831  int len;
832 
833  av_init_packet(&out_pkt);
834  len = av_parser_parse2(st->parser, st->internal->avctx,
835  &out_pkt.data, &out_pkt.size, data, size,
836  pkt->pts, pkt->dts, pkt->pos);
837 
838  pkt->pts = pkt->dts = AV_NOPTS_VALUE;
839  /* increment read pointer */
840  data += len;
841  size -= len;
842 
843  got_output = !!out_pkt.size;
844 
845  if (!out_pkt.size)
846  continue;
847 
848  if (pkt->side_data) {
849  out_pkt.side_data = pkt->side_data;
850  out_pkt.side_data_elems = pkt->side_data_elems;
851  pkt->side_data = NULL;
852  pkt->side_data_elems = 0;
853  }
854 
855  /* set the duration */
856  out_pkt.duration = 0;
857  if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
858  if (st->internal->avctx->sample_rate > 0) {
859  out_pkt.duration =
861  (AVRational) { 1, st->internal->avctx->sample_rate },
862  st->time_base,
863  AV_ROUND_DOWN);
864  }
865  }
866 
867  out_pkt.stream_index = st->index;
868  out_pkt.pts = st->parser->pts;
869  out_pkt.dts = st->parser->dts;
870  out_pkt.pos = st->parser->pos;
871 
872  if (st->parser->key_frame == 1 ||
873  (st->parser->key_frame == -1 &&
875  out_pkt.flags |= AV_PKT_FLAG_KEY;
876 
877  compute_pkt_fields(s, st, st->parser, &out_pkt);
878 
879  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
880  out_pkt.flags & AV_PKT_FLAG_KEY) {
881  ff_reduce_index(s, st->index);
882  av_add_index_entry(st, st->parser->frame_offset, out_pkt.dts,
883  0, 0, AVINDEX_KEYFRAME);
884  }
885 
886  if ((ret = add_to_pktbuf(&s->internal->parse_queue, &out_pkt,
888  1))) {
889  av_packet_unref(&out_pkt);
890  goto fail;
891  }
892  }
893 
894  /* end of the stream => close and free the parser */
895  if (pkt == &flush_pkt) {
896  av_parser_close(st->parser);
897  st->parser = NULL;
898  }
899 
900 fail:
901  av_packet_unref(pkt);
902  return ret;
903 }
904 
905 static int read_from_packet_buffer(AVPacketList **pkt_buffer,
906  AVPacketList **pkt_buffer_end,
907  AVPacket *pkt)
908 {
909  AVPacketList *pktl;
910  av_assert0(*pkt_buffer);
911  pktl = *pkt_buffer;
912  *pkt = pktl->pkt;
913  *pkt_buffer = pktl->next;
914  if (!pktl->next)
915  *pkt_buffer_end = NULL;
916  av_freep(&pktl);
917  return 0;
918 }
919 
921 {
922  int ret = 0, i, got_packet = 0;
923  AVDictionary *metadata = NULL;
924 
925  av_init_packet(pkt);
926 
927  while (!got_packet && !s->internal->parse_queue) {
928  AVStream *st;
929  AVPacket cur_pkt;
930 
931  /* read next packet */
932  ret = ff_read_packet(s, &cur_pkt);
933  if (ret < 0) {
934  if (ret == AVERROR(EAGAIN))
935  return ret;
936  /* flush the parsers */
937  for (i = 0; i < s->nb_streams; i++) {
938  st = s->streams[i];
939  if (st->parser && st->need_parsing)
940  parse_packet(s, NULL, st->index);
941  }
942  /* all remaining packets are now in parse_queue =>
943  * really terminate parsing */
944  break;
945  }
946  ret = 0;
947  st = s->streams[cur_pkt.stream_index];
948 
949  if (cur_pkt.pts != AV_NOPTS_VALUE &&
950  cur_pkt.dts != AV_NOPTS_VALUE &&
951  cur_pkt.pts < cur_pkt.dts) {
953  "Invalid timestamps stream=%d, pts=%"PRId64", "
954  "dts=%"PRId64", size=%d\n",
955  cur_pkt.stream_index, cur_pkt.pts,
956  cur_pkt.dts, cur_pkt.size);
957  }
958  if (s->debug & FF_FDEBUG_TS)
959  av_log(s, AV_LOG_DEBUG,
960  "ff_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", "
961  "size=%d, duration=%"PRId64", flags=%d\n",
962  cur_pkt.stream_index, cur_pkt.pts, cur_pkt.dts,
963  cur_pkt.size, cur_pkt.duration, cur_pkt.flags);
964 
965  if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
967  if (!st->parser)
968  /* no parser available: just output the raw packets */
970  else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
972  else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
973  st->parser->flags |= PARSER_FLAG_ONCE;
974  }
975 
976  if (!st->need_parsing || !st->parser) {
977  /* no parsing needed: we just output the packet as is */
978  *pkt = cur_pkt;
979  compute_pkt_fields(s, st, NULL, pkt);
980  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
981  (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
982  ff_reduce_index(s, st->index);
983  av_add_index_entry(st, pkt->pos, pkt->dts,
984  0, 0, AVINDEX_KEYFRAME);
985  }
986  got_packet = 1;
987  } else if (st->discard < AVDISCARD_ALL) {
988  if ((ret = parse_packet(s, &cur_pkt, cur_pkt.stream_index)) < 0)
989  return ret;
990  } else {
991  /* free packet */
992  av_packet_unref(&cur_pkt);
993  }
994  }
995 
996  if (!got_packet && s->internal->parse_queue)
998 
999  av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
1000  if (metadata) {
1002  av_dict_copy(&s->metadata, metadata, 0);
1003  av_dict_free(&metadata);
1005  }
1006 
1007 #if FF_API_LAVF_AVCTX
1008  update_stream_avctx(s);
1009 #endif
1010 
1011  if (s->debug & FF_FDEBUG_TS)
1012  av_log(s, AV_LOG_DEBUG,
1013  "read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", "
1014  "size=%d, duration=%"PRId64", flags=%d\n",
1015  pkt->stream_index, pkt->pts, pkt->dts,
1016  pkt->size, pkt->duration, pkt->flags);
1017 
1018  return ret;
1019 }
1020 
1022 {
1023  const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1024  int eof = 0;
1025 
1026  if (!genpts)
1027  return s->internal->packet_buffer
1029  &s->internal->packet_buffer_end, pkt)
1030  : read_frame_internal(s, pkt);
1031 
1032  for (;;) {
1033  int ret;
1034  AVPacketList *pktl = s->internal->packet_buffer;
1035 
1036  if (pktl) {
1037  AVPacket *next_pkt = &pktl->pkt;
1038 
1039  if (next_pkt->dts != AV_NOPTS_VALUE) {
1040  int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1041  while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1042  if (pktl->pkt.stream_index == next_pkt->stream_index &&
1043  (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0) &&
1044  av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
1045  // not B-frame
1046  next_pkt->pts = pktl->pkt.dts;
1047  }
1048  pktl = pktl->next;
1049  }
1050  pktl = s->internal->packet_buffer;
1051  }
1052 
1053  /* read packet from packet buffer, if there is data */
1054  if (!(next_pkt->pts == AV_NOPTS_VALUE &&
1055  next_pkt->dts != AV_NOPTS_VALUE && !eof))
1057  &s->internal->packet_buffer_end, pkt);
1058  }
1059 
1060  ret = read_frame_internal(s, pkt);
1061  if (ret < 0) {
1062  if (pktl && ret != AVERROR(EAGAIN)) {
1063  eof = 1;
1064  continue;
1065  } else
1066  return ret;
1067  }
1068 
1069  ret = add_to_pktbuf(&s->internal->packet_buffer, pkt,
1070  &s->internal->packet_buffer_end, 1);
1071  if (ret < 0)
1072  return ret;
1073  }
1074 }
1075 
1076 /* XXX: suppress the packet queue */
1078 {
1082 
1084 }
1085 
1086 /*******************************************************/
1087 /* seek support */
1088 
1090 {
1091  int first_audio_index = -1;
1092  int i;
1093  AVStream *st;
1094 
1095  if (s->nb_streams <= 0)
1096  return -1;
1097  for (i = 0; i < s->nb_streams; i++) {
1098  st = s->streams[i];
1099  if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
1101  return i;
1102  }
1103  if (first_audio_index < 0 &&
1105  first_audio_index = i;
1106  }
1107  return first_audio_index >= 0 ? first_audio_index : 0;
1108 }
1109 
1112 {
1113  AVStream *st;
1114  int i, j;
1115 
1116  flush_packet_queue(s);
1117 
1118  /* Reset read state for each stream. */
1119  for (i = 0; i < s->nb_streams; i++) {
1120  st = s->streams[i];
1121 
1122  if (st->parser) {
1123  av_parser_close(st->parser);
1124  st->parser = NULL;
1125  }
1127  /* We set the current DTS to an unspecified origin. */
1128  st->cur_dts = AV_NOPTS_VALUE;
1129 
1131 
1132  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
1133  st->pts_buffer[j] = AV_NOPTS_VALUE;
1134  }
1135 }
1136 
1137 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1138 {
1139  int i;
1140 
1141  for (i = 0; i < s->nb_streams; i++) {
1142  AVStream *st = s->streams[i];
1143 
1144  st->cur_dts =
1145  av_rescale(timestamp,
1146  st->time_base.den * (int64_t) ref_st->time_base.num,
1147  st->time_base.num * (int64_t) ref_st->time_base.den);
1148  }
1149 }
1150 
1151 void ff_reduce_index(AVFormatContext *s, int stream_index)
1152 {
1153  AVStream *st = s->streams[stream_index];
1154  unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
1155 
1156  if ((unsigned) st->nb_index_entries >= max_entries) {
1157  int i;
1158  for (i = 0; 2 * i < st->nb_index_entries; i++)
1159  st->index_entries[i] = st->index_entries[2 * i];
1160  st->nb_index_entries = i;
1161  }
1162 }
1163 
1164 int ff_add_index_entry(AVIndexEntry **index_entries,
1165  int *nb_index_entries,
1166  unsigned int *index_entries_allocated_size,
1167  int64_t pos, int64_t timestamp,
1168  int size, int distance, int flags)
1169 {
1170  AVIndexEntry *entries, *ie;
1171  int index;
1172 
1173  if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1174  return -1;
1175 
1176  entries = av_fast_realloc(*index_entries,
1177  index_entries_allocated_size,
1178  (*nb_index_entries + 1) *
1179  sizeof(AVIndexEntry));
1180  if (!entries)
1181  return -1;
1182 
1183  *index_entries = entries;
1184 
1185  index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
1186  timestamp, AVSEEK_FLAG_ANY);
1187 
1188  if (index < 0) {
1189  index = (*nb_index_entries)++;
1190  ie = &entries[index];
1191  assert(index == 0 || ie[-1].timestamp < timestamp);
1192  } else {
1193  ie = &entries[index];
1194  if (ie->timestamp != timestamp) {
1195  if (ie->timestamp <= timestamp)
1196  return -1;
1197  memmove(entries + index + 1, entries + index,
1198  sizeof(AVIndexEntry) * (*nb_index_entries - index));
1199  (*nb_index_entries)++;
1200  } else if (ie->pos == pos && distance < ie->min_distance)
1201  // do not reduce the distance
1202  distance = ie->min_distance;
1203  }
1204 
1205  ie->pos = pos;
1206  ie->timestamp = timestamp;
1207  ie->min_distance = distance;
1208  ie->size = size;
1209  ie->flags = flags;
1210 
1211  return index;
1212 }
1213 
1214 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
1215  int size, int distance, int flags)
1216 {
1218  &st->index_entries_allocated_size, pos,
1219  timestamp, size, distance, flags);
1220 }
1221 
1222 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1223  int64_t wanted_timestamp, int flags)
1224 {
1225  int a, b, m;
1226  int64_t timestamp;
1227 
1228  a = -1;
1229  b = nb_entries;
1230 
1231  // Optimize appending index entries at the end.
1232  if (b && entries[b - 1].timestamp < wanted_timestamp)
1233  a = b - 1;
1234 
1235  while (b - a > 1) {
1236  m = (a + b) >> 1;
1237  timestamp = entries[m].timestamp;
1238  if (timestamp >= wanted_timestamp)
1239  b = m;
1240  if (timestamp <= wanted_timestamp)
1241  a = m;
1242  }
1243  m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
1244 
1245  if (!(flags & AVSEEK_FLAG_ANY))
1246  while (m >= 0 && m < nb_entries &&
1247  !(entries[m].flags & AVINDEX_KEYFRAME))
1248  m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1249 
1250  if (m == nb_entries)
1251  return -1;
1252  return m;
1253 }
1254 
1255 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
1256 {
1258  wanted_timestamp, flags);
1259 }
1260 
1261 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
1262  int64_t target_ts, int flags)
1263 {
1264  AVInputFormat *avif = s->iformat;
1265  int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
1266  int64_t ts_min, ts_max, ts;
1267  int index;
1268  int64_t ret;
1269  AVStream *st;
1270 
1271  if (stream_index < 0)
1272  return -1;
1273 
1274  av_log(s, AV_LOG_TRACE, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
1275 
1276  ts_max =
1277  ts_min = AV_NOPTS_VALUE;
1278  pos_limit = -1; // GCC falsely says it may be uninitialized.
1279 
1280  st = s->streams[stream_index];
1281  if (st->index_entries) {
1282  AVIndexEntry *e;
1283 
1284  /* FIXME: Whole function must be checked for non-keyframe entries in
1285  * index case, especially read_timestamp(). */
1286  index = av_index_search_timestamp(st, target_ts,
1287  flags | AVSEEK_FLAG_BACKWARD);
1288  index = FFMAX(index, 0);
1289  e = &st->index_entries[index];
1290 
1291  if (e->timestamp <= target_ts || e->pos == e->min_distance) {
1292  pos_min = e->pos;
1293  ts_min = e->timestamp;
1294  av_log(s, AV_LOG_TRACE, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
1295  pos_min, ts_min);
1296  } else {
1297  assert(index == 0);
1298  }
1299 
1300  index = av_index_search_timestamp(st, target_ts,
1301  flags & ~AVSEEK_FLAG_BACKWARD);
1302  assert(index < st->nb_index_entries);
1303  if (index >= 0) {
1304  e = &st->index_entries[index];
1305  assert(e->timestamp >= target_ts);
1306  pos_max = e->pos;
1307  ts_max = e->timestamp;
1308  pos_limit = pos_max - e->min_distance;
1309  av_log(s, AV_LOG_TRACE, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
1310  " dts_max=%"PRId64"\n", pos_max, pos_limit, ts_max);
1311  }
1312  }
1313 
1314  pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
1315  ts_min, ts_max, flags, &ts, avif->read_timestamp);
1316  if (pos < 0)
1317  return -1;
1318 
1319  /* do the seek */
1320  if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
1321  return ret;
1322 
1323  ff_update_cur_dts(s, st, ts);
1324 
1325  return 0;
1326 }
1327 
1328 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1329  int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1330  int64_t ts_min, int64_t ts_max,
1331  int flags, int64_t *ts_ret,
1332  int64_t (*read_timestamp)(struct AVFormatContext *, int,
1333  int64_t *, int64_t))
1334 {
1335  int64_t pos, ts;
1336  int64_t start_pos, filesize;
1337  int no_change;
1338 
1339  av_log(s, AV_LOG_TRACE, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
1340 
1341  if (ts_min == AV_NOPTS_VALUE) {
1342  pos_min = s->internal->data_offset;
1343  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1344  if (ts_min == AV_NOPTS_VALUE)
1345  return -1;
1346  }
1347 
1348  if (ts_max == AV_NOPTS_VALUE) {
1349  int step = 1024;
1350  filesize = avio_size(s->pb);
1351  pos_max = filesize - 1;
1352  do {
1353  pos_max -= step;
1354  ts_max = read_timestamp(s, stream_index, &pos_max,
1355  pos_max + step);
1356  step += step;
1357  } while (ts_max == AV_NOPTS_VALUE && pos_max >= step);
1358  if (ts_max == AV_NOPTS_VALUE)
1359  return -1;
1360 
1361  for (;;) {
1362  int64_t tmp_pos = pos_max + 1;
1363  int64_t tmp_ts = read_timestamp(s, stream_index,
1364  &tmp_pos, INT64_MAX);
1365  if (tmp_ts == AV_NOPTS_VALUE)
1366  break;
1367  ts_max = tmp_ts;
1368  pos_max = tmp_pos;
1369  if (tmp_pos >= filesize)
1370  break;
1371  }
1372  pos_limit = pos_max;
1373  }
1374 
1375  if (ts_min > ts_max)
1376  return -1;
1377  else if (ts_min == ts_max)
1378  pos_limit = pos_min;
1379 
1380  no_change = 0;
1381  while (pos_min < pos_limit) {
1382  av_log(s, AV_LOG_TRACE, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64
1383  " dts_max=%"PRId64"\n", pos_min, pos_max, ts_min, ts_max);
1384  assert(pos_limit <= pos_max);
1385 
1386  if (no_change == 0) {
1387  int64_t approximate_keyframe_distance = pos_max - pos_limit;
1388  // interpolate position (better than dichotomy)
1389  pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
1390  ts_max - ts_min) +
1391  pos_min - approximate_keyframe_distance;
1392  } else if (no_change == 1) {
1393  // bisection if interpolation did not change min / max pos last time
1394  pos = (pos_min + pos_limit) >> 1;
1395  } else {
1396  /* linear search if bisection failed, can only happen if there
1397  * are very few or no keyframes between min/max */
1398  pos = pos_min;
1399  }
1400  if (pos <= pos_min)
1401  pos = pos_min + 1;
1402  else if (pos > pos_limit)
1403  pos = pos_limit;
1404  start_pos = pos;
1405 
1406  // May pass pos_limit instead of -1.
1407  ts = read_timestamp(s, stream_index, &pos, INT64_MAX);
1408  if (pos == pos_max)
1409  no_change++;
1410  else
1411  no_change = 0;
1412  av_log(s, AV_LOG_TRACE, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64
1413  " target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
1414  pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts,
1415  pos_limit, start_pos, no_change);
1416  if (ts == AV_NOPTS_VALUE) {
1417  av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
1418  return -1;
1419  }
1420  assert(ts != AV_NOPTS_VALUE);
1421  if (target_ts <= ts) {
1422  pos_limit = start_pos - 1;
1423  pos_max = pos;
1424  ts_max = ts;
1425  }
1426  if (target_ts >= ts) {
1427  pos_min = pos;
1428  ts_min = ts;
1429  }
1430  }
1431 
1432  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
1433  ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
1434  pos_min = pos;
1435  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1436  pos_min++;
1437  ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1438  av_log(s, AV_LOG_TRACE, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
1439  pos, ts_min, target_ts, ts_max);
1440  *ts_ret = ts;
1441  return pos;
1442 }
1443 
1444 static int seek_frame_byte(AVFormatContext *s, int stream_index,
1445  int64_t pos, int flags)
1446 {
1447  int64_t pos_min, pos_max;
1448 
1449  pos_min = s->internal->data_offset;
1450  pos_max = avio_size(s->pb) - 1;
1451 
1452  if (pos < pos_min)
1453  pos = pos_min;
1454  else if (pos > pos_max)
1455  pos = pos_max;
1456 
1457  avio_seek(s->pb, pos, SEEK_SET);
1458 
1459  return 0;
1460 }
1461 
1462 static int seek_frame_generic(AVFormatContext *s, int stream_index,
1463  int64_t timestamp, int flags)
1464 {
1465  int index;
1466  int64_t ret;
1467  AVStream *st;
1468  AVIndexEntry *ie;
1469 
1470  st = s->streams[stream_index];
1471 
1472  index = av_index_search_timestamp(st, timestamp, flags);
1473 
1474  if (index < 0 && st->nb_index_entries &&
1475  timestamp < st->index_entries[0].timestamp)
1476  return -1;
1477 
1478  if (index < 0 || index == st->nb_index_entries - 1) {
1479  AVPacket pkt;
1480 
1481  if (st->nb_index_entries) {
1482  assert(st->index_entries);
1483  ie = &st->index_entries[st->nb_index_entries - 1];
1484  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1485  return ret;
1486  ff_update_cur_dts(s, st, ie->timestamp);
1487  } else {
1488  if ((ret = avio_seek(s->pb, s->internal->data_offset, SEEK_SET)) < 0)
1489  return ret;
1490  }
1491  for (;;) {
1492  int read_status;
1493  do {
1494  read_status = av_read_frame(s, &pkt);
1495  } while (read_status == AVERROR(EAGAIN));
1496  if (read_status < 0)
1497  break;
1498  av_packet_unref(&pkt);
1499  if (stream_index == pkt.stream_index)
1500  if ((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
1501  break;
1502  }
1503  index = av_index_search_timestamp(st, timestamp, flags);
1504  }
1505  if (index < 0)
1506  return -1;
1507 
1509  if (s->iformat->read_seek)
1510  if (s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
1511  return 0;
1512  ie = &st->index_entries[index];
1513  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1514  return ret;
1515  ff_update_cur_dts(s, st, ie->timestamp);
1516 
1517  return 0;
1518 }
1519 
1520 static int seek_frame_internal(AVFormatContext *s, int stream_index,
1521  int64_t timestamp, int flags)
1522 {
1523  int ret;
1524  AVStream *st;
1525 
1526  if (flags & AVSEEK_FLAG_BYTE) {
1527  if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
1528  return -1;
1530  return seek_frame_byte(s, stream_index, timestamp, flags);
1531  }
1532 
1533  if (stream_index < 0) {
1534  stream_index = av_find_default_stream_index(s);
1535  if (stream_index < 0)
1536  return -1;
1537 
1538  st = s->streams[stream_index];
1539  /* timestamp for default must be expressed in AV_TIME_BASE units */
1540  timestamp = av_rescale(timestamp, st->time_base.den,
1541  AV_TIME_BASE * (int64_t) st->time_base.num);
1542  }
1543 
1544  /* first, we try the format specific seek */
1545  if (s->iformat->read_seek) {
1547  ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
1548  } else
1549  ret = -1;
1550  if (ret >= 0)
1551  return 0;
1552 
1553  if (s->iformat->read_timestamp &&
1554  !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
1556  return ff_seek_frame_binary(s, stream_index, timestamp, flags);
1557  } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
1559  return seek_frame_generic(s, stream_index, timestamp, flags);
1560  } else
1561  return -1;
1562 }
1563 
1564 int av_seek_frame(AVFormatContext *s, int stream_index,
1565  int64_t timestamp, int flags)
1566 {
1567  int ret = seek_frame_internal(s, stream_index, timestamp, flags);
1568 
1569  if (ret >= 0)
1570  ret = queue_attached_pictures(s);
1571 
1572  return ret;
1573 }
1574 
1575 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
1576  int64_t ts, int64_t max_ts, int flags)
1577 {
1578  if (min_ts > ts || max_ts < ts)
1579  return -1;
1580 
1581  if (s->iformat->read_seek2) {
1582  int ret;
1584  ret = s->iformat->read_seek2(s, stream_index, min_ts,
1585  ts, max_ts, flags);
1586 
1587  if (ret >= 0)
1588  ret = queue_attached_pictures(s);
1589  return ret;
1590  }
1591 
1592  if (s->iformat->read_timestamp) {
1593  // try to seek via read_timestamp()
1594  }
1595 
1596  // Fall back on old API if new is not implemented but old is.
1597  // Note the old API has somewhat different semantics.
1598  if (s->iformat->read_seek || 1)
1599  return av_seek_frame(s, stream_index, ts,
1600  flags | ((uint64_t) ts - min_ts >
1601  (uint64_t) max_ts - ts
1602  ? AVSEEK_FLAG_BACKWARD : 0));
1603 
1604  // try some generic seek like seek_frame_generic() but with new ts semantics
1605 }
1606 
1607 /*******************************************************/
1608 
1615 {
1616  int i;
1617  AVStream *st;
1618 
1619  for (i = 0; i < ic->nb_streams; i++) {
1620  st = ic->streams[i];
1621  if (st->duration != AV_NOPTS_VALUE)
1622  return 1;
1623  }
1624  if (ic->duration != AV_NOPTS_VALUE)
1625  return 1;
1626  return 0;
1627 }
1628 
1635 {
1636  int64_t start_time, start_time1, end_time, end_time1;
1637  int64_t duration, duration1, filesize;
1638  int i;
1639  AVStream *st;
1640 
1641  start_time = INT64_MAX;
1642  end_time = INT64_MIN;
1643  duration = INT64_MIN;
1644  for (i = 0; i < ic->nb_streams; i++) {
1645  st = ic->streams[i];
1646  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1647  start_time1 = av_rescale_q(st->start_time, st->time_base,
1648  AV_TIME_BASE_Q);
1649  start_time = FFMIN(start_time, start_time1);
1650  if (st->duration != AV_NOPTS_VALUE) {
1651  end_time1 = start_time1 +
1652  av_rescale_q(st->duration, st->time_base,
1653  AV_TIME_BASE_Q);
1654  end_time = FFMAX(end_time, end_time1);
1655  }
1656  }
1657  if (st->duration != AV_NOPTS_VALUE) {
1658  duration1 = av_rescale_q(st->duration, st->time_base,
1659  AV_TIME_BASE_Q);
1660  duration = FFMAX(duration, duration1);
1661  }
1662  }
1663  if (start_time != INT64_MAX) {
1664  ic->start_time = start_time;
1665  if (end_time != INT64_MIN)
1666  duration = FFMAX(duration, end_time - start_time);
1667  }
1668  if (duration != INT64_MIN) {
1669  ic->duration = duration;
1670  if (ic->pb && (filesize = avio_size(ic->pb)) > 0)
1671  /* compute the bitrate */
1672  ic->bit_rate = (double) filesize * 8.0 * AV_TIME_BASE /
1673  (double) ic->duration;
1674  }
1675 }
1676 
1678 {
1679  int i;
1680  AVStream *st;
1681 
1683  for (i = 0; i < ic->nb_streams; i++) {
1684  st = ic->streams[i];
1685  if (st->start_time == AV_NOPTS_VALUE) {
1686  if (ic->start_time != AV_NOPTS_VALUE)
1688  st->time_base);
1689  if (ic->duration != AV_NOPTS_VALUE)
1691  st->time_base);
1692  }
1693  }
1694 }
1695 
1697 {
1698  int64_t filesize, duration;
1699  int i;
1700  AVStream *st;
1701 
1702  /* if bit_rate is already set, we believe it */
1703  if (ic->bit_rate <= 0) {
1704  int bit_rate = 0;
1705  for (i = 0; i < ic->nb_streams; i++) {
1706  st = ic->streams[i];
1707  if (st->codecpar->bit_rate > 0) {
1708  if (INT_MAX - st->codecpar->bit_rate < bit_rate) {
1709  bit_rate = 0;
1710  break;
1711  }
1712  bit_rate += st->codecpar->bit_rate;
1713  }
1714  }
1715  ic->bit_rate = bit_rate;
1716  }
1717 
1718  /* if duration is already set, we believe it */
1719  if (ic->duration == AV_NOPTS_VALUE &&
1720  ic->bit_rate != 0) {
1721  filesize = ic->pb ? avio_size(ic->pb) : 0;
1722  if (filesize > 0) {
1723  for (i = 0; i < ic->nb_streams; i++) {
1724  st = ic->streams[i];
1725  duration = av_rescale(8 * filesize, st->time_base.den,
1726  ic->bit_rate *
1727  (int64_t) st->time_base.num);
1728  if (st->duration == AV_NOPTS_VALUE)
1729  st->duration = duration;
1730  }
1731  }
1732  }
1733 }
1734 
1735 #define DURATION_MAX_READ_SIZE 250000
1736 #define DURATION_MAX_RETRY 3
1737 
1738 /* only usable for MPEG-PS streams */
1739 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1740 {
1741  AVPacket pkt1, *pkt = &pkt1;
1742  AVStream *st;
1743  int read_size, i, ret;
1744  int64_t end_time;
1745  int64_t filesize, offset, duration;
1746  int retry = 0;
1747 
1748  /* flush packet queue */
1749  flush_packet_queue(ic);
1750 
1751  for (i = 0; i < ic->nb_streams; i++) {
1752  st = ic->streams[i];
1753  if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
1754  av_log(ic, AV_LOG_WARNING,
1755  "start time is not set in estimate_timings_from_pts\n");
1756 
1757  if (st->parser) {
1758  av_parser_close(st->parser);
1759  st->parser = NULL;
1760  }
1761  }
1762 
1763  /* estimate the end time (duration) */
1764  /* XXX: may need to support wrapping */
1765  filesize = ic->pb ? avio_size(ic->pb) : 0;
1766  end_time = AV_NOPTS_VALUE;
1767  do {
1768  offset = filesize - (DURATION_MAX_READ_SIZE << retry);
1769  if (offset < 0)
1770  offset = 0;
1771 
1772  avio_seek(ic->pb, offset, SEEK_SET);
1773  read_size = 0;
1774  for (;;) {
1775  if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
1776  break;
1777 
1778  do {
1779  ret = ff_read_packet(ic, pkt);
1780  } while (ret == AVERROR(EAGAIN));
1781  if (ret != 0)
1782  break;
1783  read_size += pkt->size;
1784  st = ic->streams[pkt->stream_index];
1785  if (pkt->pts != AV_NOPTS_VALUE &&
1786  (st->start_time != AV_NOPTS_VALUE ||
1787  st->first_dts != AV_NOPTS_VALUE)) {
1788  duration = end_time = pkt->pts;
1789  if (st->start_time != AV_NOPTS_VALUE)
1790  duration -= st->start_time;
1791  else
1792  duration -= st->first_dts;
1793  if (duration < 0)
1794  duration += 1LL << st->pts_wrap_bits;
1795  if (duration > 0) {
1796  if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
1797  st->duration = duration;
1798  }
1799  }
1800  av_packet_unref(pkt);
1801  }
1802  } while (end_time == AV_NOPTS_VALUE &&
1803  filesize > (DURATION_MAX_READ_SIZE << retry) &&
1804  ++retry <= DURATION_MAX_RETRY);
1805 
1807 
1808  avio_seek(ic->pb, old_offset, SEEK_SET);
1809  for (i = 0; i < ic->nb_streams; i++) {
1810  st = ic->streams[i];
1811  st->cur_dts = st->first_dts;
1813  }
1814 }
1815 
1816 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1817 {
1818  int64_t file_size;
1819 
1820  /* get the file size, if possible */
1821  if (ic->iformat->flags & AVFMT_NOFILE) {
1822  file_size = 0;
1823  } else {
1824  file_size = avio_size(ic->pb);
1825  file_size = FFMAX(0, file_size);
1826  }
1827 
1828  if ((!strcmp(ic->iformat->name, "mpeg") ||
1829  !strcmp(ic->iformat->name, "mpegts")) &&
1830  file_size && ic->pb->seekable) {
1831  /* get accurate estimate from the PTSes */
1832  estimate_timings_from_pts(ic, old_offset);
1833  } else if (has_duration(ic)) {
1834  /* at least one component has timings - we use them for all
1835  * the components */
1837  } else {
1838  av_log(ic, AV_LOG_WARNING,
1839  "Estimating duration from bitrate, this may be inaccurate\n");
1840  /* less precise: use bitrate info */
1842  }
1844 
1845  {
1846  int i;
1847  AVStream av_unused *st;
1848  for (i = 0; i < ic->nb_streams; i++) {
1849  st = ic->streams[i];
1850  av_log(ic, AV_LOG_TRACE, "%d: start_time: %0.3f duration: %0.3f\n", i,
1851  (double) st->start_time / AV_TIME_BASE,
1852  (double) st->duration / AV_TIME_BASE);
1853  }
1854  av_log(ic, AV_LOG_TRACE,
1855  "stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
1856  (double) ic->start_time / AV_TIME_BASE,
1857  (double) ic->duration / AV_TIME_BASE,
1858  ic->bit_rate / 1000);
1859  }
1860 }
1861 
1863 {
1864  AVCodecContext *avctx = st->internal->avctx;
1865  int val;
1866 
1867  switch (avctx->codec_type) {
1868  case AVMEDIA_TYPE_AUDIO:
1869  val = avctx->sample_rate && avctx->channels;
1870  if (st->info->found_decoder >= 0 &&
1871  avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
1872  return 0;
1873  break;
1874  case AVMEDIA_TYPE_VIDEO:
1875  val = avctx->width;
1876  if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
1877  return 0;
1878  break;
1879  default:
1880  val = 1;
1881  break;
1882  }
1883  return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
1884 }
1885 
1887 {
1888  return st->internal->avctx->codec_id != AV_CODEC_ID_H264 ||
1889  st->info->nb_decoded_frames >= 6;
1890 }
1891 
1892 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
1895 {
1896  AVCodecContext *avctx = st->internal->avctx;
1897  const AVCodec *codec;
1898  int got_picture = 1, ret = 0;
1899  AVFrame *frame = av_frame_alloc();
1900  AVPacket pkt = *avpkt;
1901 
1902  if (!frame)
1903  return AVERROR(ENOMEM);
1904 
1905  if (!avcodec_is_open(avctx) && !st->info->found_decoder) {
1906  AVDictionary *thread_opt = NULL;
1907 
1908 #if FF_API_LAVF_AVCTX
1910  codec = st->codec->codec ? st->codec->codec
1913 #else
1914  codec = avcodec_find_decoder(st->codecpar->codec_id);
1915 #endif
1916 
1917  if (!codec) {
1918  st->info->found_decoder = -1;
1919  ret = -1;
1920  goto fail;
1921  }
1922 
1923  /* Force thread count to 1 since the H.264 decoder will not extract
1924  * SPS and PPS to extradata during multi-threaded decoding. */
1925  av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
1926  ret = avcodec_open2(avctx, codec, options ? options : &thread_opt);
1927  if (!options)
1928  av_dict_free(&thread_opt);
1929  if (ret < 0) {
1930  st->info->found_decoder = -1;
1931  goto fail;
1932  }
1933  st->info->found_decoder = 1;
1934  } else if (!st->info->found_decoder)
1935  st->info->found_decoder = 1;
1936 
1937  if (st->info->found_decoder < 0) {
1938  ret = -1;
1939  goto fail;
1940  }
1941 
1942  while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
1943  ret >= 0 &&
1945  (!st->codec_info_nb_frames &&
1947  got_picture = 0;
1948  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
1949  avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
1950  ret = avcodec_send_packet(avctx, &pkt);
1951  if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
1952  break;
1953  if (ret >= 0)
1954  pkt.size = 0;
1955  ret = avcodec_receive_frame(avctx, frame);
1956  if (ret >= 0)
1957  got_picture = 1;
1958  if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
1959  ret = 0;
1960  }
1961  if (ret >= 0) {
1962  if (got_picture)
1963  st->info->nb_decoded_frames++;
1964  ret = got_picture;
1965  }
1966  }
1967 
1968 fail:
1969  av_frame_free(&frame);
1970  return ret;
1971 }
1972 
1973 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
1974 {
1975  while (tags->id != AV_CODEC_ID_NONE) {
1976  if (tags->id == id)
1977  return tags->tag;
1978  tags++;
1979  }
1980  return 0;
1981 }
1982 
1983 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
1984 {
1985  int i;
1986  for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
1987  if (tag == tags[i].tag)
1988  return tags[i].id;
1989  for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
1990  if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
1991  return tags[i].id;
1992  return AV_CODEC_ID_NONE;
1993 }
1994 
1995 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
1996 {
1997  if (flt) {
1998  switch (bps) {
1999  case 32:
2001  case 64:
2003  default:
2004  return AV_CODEC_ID_NONE;
2005  }
2006  } else {
2007  bps >>= 3;
2008  if (sflags & (1 << (bps - 1))) {
2009  switch (bps) {
2010  case 1:
2011  return AV_CODEC_ID_PCM_S8;
2012  case 2:
2014  case 3:
2016  case 4:
2018  default:
2019  return AV_CODEC_ID_NONE;
2020  }
2021  } else {
2022  switch (bps) {
2023  case 1:
2024  return AV_CODEC_ID_PCM_U8;
2025  case 2:
2027  case 3:
2029  case 4:
2031  default:
2032  return AV_CODEC_ID_NONE;
2033  }
2034  }
2035  }
2036 }
2037 
2038 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
2039 {
2040  int i;
2041  for (i = 0; tags && tags[i]; i++) {
2042  int tag = ff_codec_get_tag(tags[i], id);
2043  if (tag)
2044  return tag;
2045  }
2046  return 0;
2047 }
2048 
2049 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
2050 {
2051  int i;
2052  for (i = 0; tags && tags[i]; i++) {
2053  enum AVCodecID id = ff_codec_get_id(tags[i], tag);
2054  if (id != AV_CODEC_ID_NONE)
2055  return id;
2056  }
2057  return AV_CODEC_ID_NONE;
2058 }
2059 
2061 {
2062  unsigned int i, j;
2063  int64_t max_time = s->duration +
2064  ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2065 
2066  for (i = 0; i < s->nb_chapters; i++)
2067  if (s->chapters[i]->end == AV_NOPTS_VALUE) {
2068  AVChapter *ch = s->chapters[i];
2069  int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2070  ch->time_base)
2071  : INT64_MAX;
2072 
2073  for (j = 0; j < s->nb_chapters; j++) {
2074  AVChapter *ch1 = s->chapters[j];
2075  int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2076  ch->time_base);
2077  if (j != i && next_start > ch->start && next_start < end)
2078  end = next_start;
2079  }
2080  ch->end = (end == INT64_MAX) ? ch->start : end;
2081  }
2082 }
2083 
2084 static int get_std_framerate(int i)
2085 {
2086  if (i < 60 * 12)
2087  return (i + 1) * 1001;
2088  else
2089  return ((const int[]) { 24, 30, 60, 12, 15 })[i - 60 * 12] * 1000 * 12;
2090 }
2091 
2093 {
2094  int i, count, ret, read_size, j;
2095  AVStream *st;
2096  AVCodecContext *avctx;
2097  AVPacket pkt1, *pkt;
2098  int64_t old_offset = avio_tell(ic->pb);
2099  // new streams might appear, no options for those
2100  int orig_nb_streams = ic->nb_streams;
2101 
2102  for (i = 0; i < ic->nb_streams; i++) {
2103  const AVCodec *codec;
2104  AVDictionary *thread_opt = NULL;
2105  st = ic->streams[i];
2106  avctx = st->internal->avctx;
2107 
2108  // only for the split stuff
2109  if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
2110  st->parser = av_parser_init(st->codecpar->codec_id);
2111  if (st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser)
2113  }
2114 
2115  /* check if the caller has overridden the codec id */
2116 #if FF_API_LAVF_AVCTX
2118  if (st->codec->codec_id != st->internal->orig_codec_id) {
2119  st->codecpar->codec_id = st->codec->codec_id;
2120  st->codecpar->codec_type = st->codec->codec_type;
2121  st->internal->orig_codec_id = st->codec->codec_id;
2122  }
2124 #endif
2125  if (st->codecpar->codec_id != st->internal->orig_codec_id)
2127 
2128  ret = avcodec_parameters_to_context(avctx, st->codecpar);
2129  if (ret < 0)
2130  goto find_stream_info_err;
2131  if (st->codecpar->codec_id != AV_CODEC_ID_PROBE &&
2133  st->internal->avctx_inited = 1;
2134 
2135 #if FF_API_LAVF_AVCTX
2137  codec = st->codec->codec ? st->codec->codec
2140 #else
2141  codec = avcodec_find_decoder(st->codecpar->codec_id);
2142 #endif
2143 
2144  /* Force thread count to 1 since the H.264 decoder will not extract
2145  * SPS and PPS to extradata during multi-threaded decoding. */
2146  av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2147 
2148  /* Ensure that subtitle_header is properly set. */
2150  && codec && !avctx->codec)
2151  avcodec_open2(avctx, codec,
2152  options ? &options[i] : &thread_opt);
2153 
2154  // Try to just open decoders, in case this is enough to get parameters.
2155  if (!has_codec_parameters(st)) {
2156  if (codec && !avctx->codec)
2157  avcodec_open2(avctx, codec,
2158  options ? &options[i] : &thread_opt);
2159  }
2160  if (!options)
2161  av_dict_free(&thread_opt);
2162  }
2163 
2164  for (i = 0; i < ic->nb_streams; i++) {
2167  }
2168 
2169  count = 0;
2170  read_size = 0;
2171  for (;;) {
2173  ret = AVERROR_EXIT;
2174  av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2175  break;
2176  }
2177 
2178  /* check if one codec still needs to be handled */
2179  for (i = 0; i < ic->nb_streams; i++) {
2180  int fps_analyze_framecount = 20;
2181 
2182  st = ic->streams[i];
2183  if (!has_codec_parameters(st))
2184  break;
2185  /* If the timebase is coarse (like the usual millisecond precision
2186  * of mkv), we need to analyze more frames to reliably arrive at
2187  * the correct fps. */
2188  if (av_q2d(st->time_base) > 0.0005)
2189  fps_analyze_framecount *= 2;
2190  if (ic->fps_probe_size >= 0)
2191  fps_analyze_framecount = ic->fps_probe_size;
2192  /* variable fps and no guess at the real fps */
2193  if (!st->avg_frame_rate.num &&
2194  st->codec_info_nb_frames < fps_analyze_framecount &&
2196  break;
2197  if (st->parser && st->parser->parser->split &&
2198  !st->codecpar->extradata)
2199  break;
2200  if (st->first_dts == AV_NOPTS_VALUE &&
2201  st->codec_info_nb_frames < ic->max_ts_probe &&
2204  break;
2205  }
2206  if (i == ic->nb_streams) {
2207  /* NOTE: If the format has no header, then we need to read some
2208  * packets to get most of the streams, so we cannot stop here. */
2209  if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2210  /* If we found the info for all the codecs, we can stop. */
2211  ret = count;
2212  av_log(ic, AV_LOG_DEBUG, "All info found\n");
2213  break;
2214  }
2215  }
2216  /* We did not get all the codec info, but we read too much data. */
2217  if (read_size >= ic->probesize) {
2218  ret = count;
2219  av_log(ic, AV_LOG_DEBUG,
2220  "Probe buffer size limit %d reached\n", ic->probesize);
2221  break;
2222  }
2223 
2224  /* NOTE: A new stream can be added there if no header in file
2225  * (AVFMTCTX_NOHEADER). */
2226  ret = read_frame_internal(ic, &pkt1);
2227  if (ret == AVERROR(EAGAIN))
2228  continue;
2229 
2230  if (ret < 0) {
2231  /* EOF or error*/
2232  AVPacket empty_pkt = { 0 };
2233  int err = 0;
2234  av_init_packet(&empty_pkt);
2235 
2236  /* We could not have all the codec parameters before EOF. */
2237  ret = -1;
2238  for (i = 0; i < ic->nb_streams; i++) {
2239  st = ic->streams[i];
2240 
2241  /* flush the decoders */
2242  if (st->info->found_decoder == 1) {
2243  do {
2244  err = try_decode_frame(ic, st, &empty_pkt,
2245  (options && i < orig_nb_streams)
2246  ? &options[i] : NULL);
2247  } while (err > 0 && !has_codec_parameters(st));
2248  }
2249 
2250  if (err < 0) {
2251  av_log(ic, AV_LOG_WARNING,
2252  "decoding for stream %d failed\n", st->index);
2253  } else if (!has_codec_parameters(st)) {
2254  char buf[256];
2255  avcodec_string(buf, sizeof(buf), st->internal->avctx, 0);
2256  av_log(ic, AV_LOG_WARNING,
2257  "Could not find codec parameters (%s)\n", buf);
2258  } else {
2259  ret = 0;
2260  }
2261  }
2262  break;
2263  }
2264 
2265  pkt = &pkt1;
2266 
2267  if (!(ic->flags & AVFMT_FLAG_NOBUFFER)) {
2268  ret = add_to_pktbuf(&ic->internal->packet_buffer, pkt,
2269  &ic->internal->packet_buffer_end, 0);
2270  if (ret < 0)
2271  goto find_stream_info_err;
2272  }
2273 
2274  read_size += pkt->size;
2275 
2276  st = ic->streams[pkt->stream_index];
2277  avctx = st->internal->avctx;
2278  if (!st->internal->avctx_inited) {
2279  ret = avcodec_parameters_to_context(avctx, st->codecpar);
2280  if (ret < 0)
2281  goto find_stream_info_err;
2282  st->internal->avctx_inited = 1;
2283  }
2284 
2285  if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
2286  /* check for non-increasing dts */
2287  if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2288  st->info->fps_last_dts >= pkt->dts) {
2289  av_log(ic, AV_LOG_WARNING,
2290  "Non-increasing DTS in stream %d: packet %d with DTS "
2291  "%"PRId64", packet %d with DTS %"PRId64"\n",
2292  st->index, st->info->fps_last_dts_idx,
2294  pkt->dts);
2295  st->info->fps_first_dts =
2297  }
2298  /* Check for a discontinuity in dts. If the difference in dts
2299  * is more than 1000 times the average packet duration in the
2300  * sequence, we treat it as a discontinuity. */
2301  if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2303  (pkt->dts - st->info->fps_last_dts) / 1000 >
2304  (st->info->fps_last_dts - st->info->fps_first_dts) /
2305  (st->info->fps_last_dts_idx - st->info->fps_first_dts_idx)) {
2306  av_log(ic, AV_LOG_WARNING,
2307  "DTS discontinuity in stream %d: packet %d with DTS "
2308  "%"PRId64", packet %d with DTS %"PRId64"\n",
2309  st->index, st->info->fps_last_dts_idx,
2311  pkt->dts);
2312  st->info->fps_first_dts =
2314  }
2315 
2316  /* update stored dts values */
2317  if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
2318  st->info->fps_first_dts = pkt->dts;
2320  }
2321  st->info->fps_last_dts = pkt->dts;
2323 
2324  /* check max_analyze_duration */
2325  if (av_rescale_q(pkt->dts - st->info->fps_first_dts, st->time_base,
2327  av_log(ic, AV_LOG_WARNING, "max_analyze_duration %d reached\n",
2328  ic->max_analyze_duration);
2329  if (ic->flags & AVFMT_FLAG_NOBUFFER)
2330  av_packet_unref(pkt);
2331  break;
2332  }
2333  }
2334  if (st->parser && st->parser->parser->split && !avctx->extradata) {
2335  int i = st->parser->parser->split(avctx, pkt->data, pkt->size);
2336  if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
2337  avctx->extradata_size = i;
2338  avctx->extradata = av_mallocz(avctx->extradata_size +
2340  if (!avctx->extradata)
2341  return AVERROR(ENOMEM);
2342  memcpy(avctx->extradata, pkt->data,
2343  avctx->extradata_size);
2344  }
2345  }
2346 
2347  /* If still no information, we try to open the codec and to
2348  * decompress the frame. We try to avoid that in most cases as
2349  * it takes longer and uses more memory. For MPEG-4, we need to
2350  * decompress for QuickTime.
2351  *
2352  * If AV_CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2353  * least one frame of codec data, this makes sure the codec initializes
2354  * the channel configuration and does not only trust the values from
2355  * the container. */
2356  try_decode_frame(ic, st, pkt,
2357  (options && i < orig_nb_streams) ? &options[i] : NULL);
2358 
2359  if (ic->flags & AVFMT_FLAG_NOBUFFER)
2360  av_packet_unref(pkt);
2361 
2362  st->codec_info_nb_frames++;
2363  count++;
2364  }
2365 
2366  for (i = 0; i < ic->nb_streams; i++) {
2367  st = ic->streams[i];
2368  avctx = st->internal->avctx;
2369  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO) {
2370  /* estimate average framerate if not set by demuxer */
2371  if (!st->avg_frame_rate.num &&
2372  st->info->fps_last_dts != st->info->fps_first_dts) {
2373  int64_t delta_dts = st->info->fps_last_dts -
2374  st->info->fps_first_dts;
2375  int delta_packets = st->info->fps_last_dts_idx -
2376  st->info->fps_first_dts_idx;
2377  int best_fps = 0;
2378  double best_error = 0.01;
2379 
2380  if (delta_dts >= INT64_MAX / st->time_base.num ||
2381  delta_packets >= INT64_MAX / st->time_base.den ||
2382  delta_dts < 0)
2383  continue;
2385  delta_packets * (int64_t) st->time_base.den,
2386  delta_dts * (int64_t) st->time_base.num, 60000);
2387 
2388  /* Round guessed framerate to a "standard" framerate if it's
2389  * within 1% of the original estimate. */
2390  for (j = 0; j < MAX_STD_TIMEBASES; j++) {
2391  AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2392  double error = fabs(av_q2d(st->avg_frame_rate) /
2393  av_q2d(std_fps) - 1);
2394 
2395  if (error < best_error) {
2396  best_error = error;
2397  best_fps = std_fps.num;
2398  }
2399  }
2400  if (best_fps)
2402  best_fps, 12 * 1001, INT_MAX);
2403  }
2404  } else if (avctx->codec_type == AVMEDIA_TYPE_AUDIO) {
2405  if (!avctx->bits_per_coded_sample)
2406  avctx->bits_per_coded_sample =
2408  // set stream disposition based on audio service type
2409  switch (avctx->audio_service_type) {
2412  break;
2415  break;
2418  break;
2421  break;
2424  break;
2425  }
2426  }
2427  }
2428 
2430 
2431  /* update the stream parameters from the internal codec contexts */
2432  for (i = 0; i < ic->nb_streams; i++) {
2433  st = ic->streams[i];
2434  if (!st->internal->avctx_inited)
2435  continue;
2436 
2438  if (ret < 0)
2439  goto find_stream_info_err;
2440 
2441 #if FF_API_LAVF_AVCTX
2443  ret = avcodec_parameters_to_context(st->codec, st->codecpar);
2444  if (ret < 0)
2445  goto find_stream_info_err;
2446 
2447  if (st->internal->avctx->subtitle_header) {
2448  st->codec->subtitle_header = av_malloc(st->internal->avctx->subtitle_header_size);
2449  if (!st->codec->subtitle_header)
2450  goto find_stream_info_err;
2451  st->codec->subtitle_header_size = st->internal->avctx->subtitle_header_size;
2452  memcpy(st->codec->subtitle_header, st->internal->avctx->subtitle_header,
2453  st->codec->subtitle_header_size);
2454  }
2456 #endif
2457 
2458  st->internal->avctx_inited = 0;
2459  }
2460 
2461  estimate_timings(ic, old_offset);
2462 
2463 find_stream_info_err:
2464  for (i = 0; i < ic->nb_streams; i++) {
2465  avcodec_close(ic->streams[i]->internal->avctx);
2466  av_freep(&ic->streams[i]->info);
2467  }
2468  return ret;
2469 }
2470 
2472 {
2473  int i, j;
2474 
2475  for (i = 0; i < ic->nb_programs; i++)
2476  for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2477  if (ic->programs[i]->stream_index[j] == s)
2478  return ic->programs[i];
2479  return NULL;
2480 }
2481 
2483  int wanted_stream_nb, int related_stream,
2484  AVCodec **decoder_ret, int flags)
2485 {
2486  int i, nb_streams = ic->nb_streams;
2487  int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2488  unsigned *program = NULL;
2489  AVCodec *decoder = NULL, *best_decoder = NULL;
2490 
2491  if (related_stream >= 0 && wanted_stream_nb < 0) {
2492  AVProgram *p = find_program_from_stream(ic, related_stream);
2493  if (p) {
2494  program = p->stream_index;
2495  nb_streams = p->nb_stream_indexes;
2496  }
2497  }
2498  for (i = 0; i < nb_streams; i++) {
2499  int real_stream_index = program ? program[i] : i;
2500  AVStream *st = ic->streams[real_stream_index];
2501  AVCodecParameters *par = st->codecpar;
2502  if (par->codec_type != type)
2503  continue;
2504  if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
2505  continue;
2508  continue;
2509  if (decoder_ret) {
2510  decoder = avcodec_find_decoder(par->codec_id);
2511  if (!decoder) {
2512  if (ret < 0)
2514  continue;
2515  }
2516  }
2517  if (best_count >= st->codec_info_nb_frames)
2518  continue;
2519  best_count = st->codec_info_nb_frames;
2520  ret = real_stream_index;
2521  best_decoder = decoder;
2522  if (program && i == nb_streams - 1 && ret < 0) {
2523  program = NULL;
2524  nb_streams = ic->nb_streams;
2525  /* no related stream found, try again with everything */
2526  i = 0;
2527  }
2528  }
2529  if (decoder_ret)
2530  *decoder_ret = best_decoder;
2531  return ret;
2532 }
2533 
2534 /*******************************************************/
2535 
2537 {
2538  if (s->iformat->read_play)
2539  return s->iformat->read_play(s);
2540  if (s->pb)
2541  return avio_pause(s->pb, 0);
2542  return AVERROR(ENOSYS);
2543 }
2544 
2546 {
2547  if (s->iformat->read_pause)
2548  return s->iformat->read_pause(s);
2549  if (s->pb)
2550  return avio_pause(s->pb, 1);
2551  return AVERROR(ENOSYS);
2552 }
2553 
2554 static void free_stream(AVStream **pst)
2555 {
2556  AVStream *st = *pst;
2557  int i;
2558 
2559  if (!st)
2560  return;
2561 
2562  for (i = 0; i < st->nb_side_data; i++)
2563  av_freep(&st->side_data[i].data);
2564  av_freep(&st->side_data);
2565 
2566  if (st->parser)
2567  av_parser_close(st->parser);
2568 
2569  if (st->attached_pic.data)
2571 
2572  if (st->internal) {
2574  }
2575  av_freep(&st->internal);
2576 
2577  av_dict_free(&st->metadata);
2579  av_freep(&st->probe_data.buf);
2580  av_free(st->index_entries);
2581 #if FF_API_LAVF_AVCTX
2583  av_free(st->codec->extradata);
2584  av_free(st->codec->subtitle_header);
2585  av_free(st->codec);
2587 #endif
2588  av_free(st->priv_data);
2589  av_free(st->info);
2590 
2591  av_freep(pst);
2592 }
2593 
2595 {
2596  int i;
2597 
2598  if (!s)
2599  return;
2600 
2601  av_opt_free(s);
2602  if (s->iformat && s->iformat->priv_class && s->priv_data)
2603  av_opt_free(s->priv_data);
2604 
2605  for (i = 0; i < s->nb_streams; i++)
2606  free_stream(&s->streams[i]);
2607 
2608  for (i = s->nb_programs - 1; i >= 0; i--) {
2609  av_dict_free(&s->programs[i]->metadata);
2610  av_freep(&s->programs[i]->stream_index);
2611  av_freep(&s->programs[i]);
2612  }
2613  av_freep(&s->programs);
2614  av_freep(&s->priv_data);
2615  while (s->nb_chapters--) {
2617  av_free(s->chapters[s->nb_chapters]);
2618  }
2619  av_freep(&s->chapters);
2620  av_dict_free(&s->metadata);
2621  av_freep(&s->streams);
2622  av_freep(&s->internal);
2623  av_free(s);
2624 }
2625 
2627 {
2628  AVFormatContext *s = *ps;
2629  AVIOContext *pb = s->pb;
2630 
2631  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
2632  (s->flags & AVFMT_FLAG_CUSTOM_IO))
2633  pb = NULL;
2634 
2635  flush_packet_queue(s);
2636 
2637  if (s->iformat)
2638  if (s->iformat->read_close)
2639  s->iformat->read_close(s);
2640 
2642 
2643  *ps = NULL;
2644 
2645  avio_close(pb);
2646 }
2647 
2649 {
2650  AVStream *st;
2651  int i;
2652 
2653  if (av_reallocp_array(&s->streams, s->nb_streams + 1,
2654  sizeof(*s->streams)) < 0) {
2655  s->nb_streams = 0;
2656  return NULL;
2657  }
2658 
2659  st = av_mallocz(sizeof(AVStream));
2660  if (!st)
2661  return NULL;
2662  if (!(st->info = av_mallocz(sizeof(*st->info)))) {
2663  av_free(st);
2664  return NULL;
2665  }
2666 
2667 #if FF_API_LAVF_AVCTX
2669  st->codec = avcodec_alloc_context3(c);
2670  if (!st->codec) {
2671  av_free(st->info);
2672  av_free(st);
2673  return NULL;
2674  }
2676 #endif
2677 
2678  st->internal = av_mallocz(sizeof(*st->internal));
2679  if (!st->internal)
2680  goto fail;
2681 
2682  if (s->iformat) {
2683 #if FF_API_LAVF_AVCTX
2685  /* no default bitrate if decoding */
2686  st->codec->bit_rate = 0;
2688 #endif
2689 
2690  /* default pts setting is MPEG-like */
2691  avpriv_set_pts_info(st, 33, 1, 90000);
2692  /* we set the current DTS to 0 so that formats without any timestamps
2693  * but durations get some timestamps, formats with some unknown
2694  * timestamps have their first few packets buffered and the
2695  * timestamps corrected before they are returned to the user */
2696  st->cur_dts = 0;
2697  } else {
2698  st->cur_dts = AV_NOPTS_VALUE;
2699  }
2700 
2702  if (!st->codecpar)
2703  goto fail;
2704 
2706  if (!st->internal->avctx)
2707  goto fail;
2708 
2709  st->index = s->nb_streams;
2710  st->start_time = AV_NOPTS_VALUE;
2711  st->duration = AV_NOPTS_VALUE;
2712  st->first_dts = AV_NOPTS_VALUE;
2714 
2716  for (i = 0; i < MAX_REORDER_DELAY + 1; i++)
2717  st->pts_buffer[i] = AV_NOPTS_VALUE;
2718 
2719  st->sample_aspect_ratio = (AVRational) { 0, 1 };
2720 
2723 
2724 #if FF_API_LAVF_AVCTX
2725  st->internal->need_codec_update = 1;
2726 #endif
2727 
2728  s->streams[s->nb_streams++] = st;
2729  return st;
2730 fail:
2731  free_stream(&st);
2732  return NULL;
2733 }
2734 
2736 {
2737  AVProgram *program = NULL;
2738  int i;
2739 
2740  av_log(ac, AV_LOG_TRACE, "new_program: id=0x%04x\n", id);
2741 
2742  for (i = 0; i < ac->nb_programs; i++)
2743  if (ac->programs[i]->id == id)
2744  program = ac->programs[i];
2745 
2746  if (!program) {
2747  program = av_mallocz(sizeof(AVProgram));
2748  if (!program)
2749  return NULL;
2750  dynarray_add(&ac->programs, &ac->nb_programs, program);
2751  program->discard = AVDISCARD_NONE;
2752  }
2753  program->id = id;
2754 
2755  return program;
2756 }
2757 
2759  int64_t start, int64_t end, const char *title)
2760 {
2761  AVChapter *chapter = NULL;
2762  int i;
2763 
2764  for (i = 0; i < s->nb_chapters; i++)
2765  if (s->chapters[i]->id == id)
2766  chapter = s->chapters[i];
2767 
2768  if (!chapter) {
2769  chapter = av_mallocz(sizeof(AVChapter));
2770  if (!chapter)
2771  return NULL;
2772  dynarray_add(&s->chapters, &s->nb_chapters, chapter);
2773  }
2774  av_dict_set(&chapter->metadata, "title", title, 0);
2775  chapter->id = id;
2776  chapter->time_base = time_base;
2777  chapter->start = start;
2778  chapter->end = end;
2779 
2780  return chapter;
2781 }
2782 
2783 void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
2784 {
2785  int i, j;
2786  AVProgram *program = NULL;
2787 
2788  if (idx >= ac->nb_streams) {
2789  av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
2790  return;
2791  }
2792 
2793  for (i = 0; i < ac->nb_programs; i++) {
2794  if (ac->programs[i]->id != progid)
2795  continue;
2796  program = ac->programs[i];
2797  for (j = 0; j < program->nb_stream_indexes; j++)
2798  if (program->stream_index[j] == idx)
2799  return;
2800 
2801  if (av_reallocp_array(&program->stream_index,
2802  program->nb_stream_indexes + 1,
2803  sizeof(*program->stream_index)) < 0) {
2804  program->nb_stream_indexes = 0;
2805  return;
2806  }
2807  program->stream_index[program->nb_stream_indexes++] = idx;
2808  return;
2809  }
2810 }
2811 
2812 uint64_t ff_ntp_time(void)
2813 {
2814  return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
2815 }
2816 
2817 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
2818 {
2819  const char *p;
2820  char *q, buf1[20], c;
2821  int nd, len, percentd_found;
2822 
2823  q = buf;
2824  p = path;
2825  percentd_found = 0;
2826  for (;;) {
2827  c = *p++;
2828  if (c == '\0')
2829  break;
2830  if (c == '%') {
2831  do {
2832  nd = 0;
2833  while (av_isdigit(*p))
2834  nd = nd * 10 + *p++ - '0';
2835  c = *p++;
2836  } while (av_isdigit(c));
2837 
2838  switch (c) {
2839  case '%':
2840  goto addchar;
2841  case 'd':
2842  if (percentd_found)
2843  goto fail;
2844  percentd_found = 1;
2845  snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
2846  len = strlen(buf1);
2847  if ((q - buf + len) > buf_size - 1)
2848  goto fail;
2849  memcpy(q, buf1, len);
2850  q += len;
2851  break;
2852  default:
2853  goto fail;
2854  }
2855  } else {
2856 addchar:
2857  if ((q - buf) < buf_size - 1)
2858  *q++ = c;
2859  }
2860  }
2861  if (!percentd_found)
2862  goto fail;
2863  *q = '\0';
2864  return 0;
2865 fail:
2866  *q = '\0';
2867  return -1;
2868 }
2869 
2870 void av_url_split(char *proto, int proto_size,
2871  char *authorization, int authorization_size,
2872  char *hostname, int hostname_size,
2873  int *port_ptr, char *path, int path_size, const char *url)
2874 {
2875  const char *p, *ls, *at, *col, *brk;
2876 
2877  if (port_ptr)
2878  *port_ptr = -1;
2879  if (proto_size > 0)
2880  proto[0] = 0;
2881  if (authorization_size > 0)
2882  authorization[0] = 0;
2883  if (hostname_size > 0)
2884  hostname[0] = 0;
2885  if (path_size > 0)
2886  path[0] = 0;
2887 
2888  /* parse protocol */
2889  if ((p = strchr(url, ':'))) {
2890  av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
2891  p++; /* skip ':' */
2892  if (*p == '/')
2893  p++;
2894  if (*p == '/')
2895  p++;
2896  } else {
2897  /* no protocol means plain filename */
2898  av_strlcpy(path, url, path_size);
2899  return;
2900  }
2901 
2902  /* separate path from hostname */
2903  ls = strchr(p, '/');
2904  if (!ls)
2905  ls = strchr(p, '?');
2906  if (ls)
2907  av_strlcpy(path, ls, path_size);
2908  else
2909  ls = &p[strlen(p)]; // XXX
2910 
2911  /* the rest is hostname, use that to parse auth/port */
2912  if (ls != p) {
2913  /* authorization (user[:pass]@hostname) */
2914  if ((at = strchr(p, '@')) && at < ls) {
2915  av_strlcpy(authorization, p,
2916  FFMIN(authorization_size, at + 1 - p));
2917  p = at + 1; /* skip '@' */
2918  }
2919 
2920  if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
2921  /* [host]:port */
2922  av_strlcpy(hostname, p + 1,
2923  FFMIN(hostname_size, brk - p));
2924  if (brk[1] == ':' && port_ptr)
2925  *port_ptr = atoi(brk + 2);
2926  } else if ((col = strchr(p, ':')) && col < ls) {
2927  av_strlcpy(hostname, p,
2928  FFMIN(col + 1 - p, hostname_size));
2929  if (port_ptr)
2930  *port_ptr = atoi(col + 1);
2931  } else
2932  av_strlcpy(hostname, p,
2933  FFMIN(ls + 1 - p, hostname_size));
2934  }
2935 }
2936 
2937 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
2938 {
2939  int i;
2940  static const char hex_table_uc[16] = { '0', '1', '2', '3',
2941  '4', '5', '6', '7',
2942  '8', '9', 'A', 'B',
2943  'C', 'D', 'E', 'F' };
2944  static const char hex_table_lc[16] = { '0', '1', '2', '3',
2945  '4', '5', '6', '7',
2946  '8', '9', 'a', 'b',
2947  'c', 'd', 'e', 'f' };
2948  const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
2949 
2950  for (i = 0; i < s; i++) {
2951  buff[i * 2] = hex_table[src[i] >> 4];
2952  buff[i * 2 + 1] = hex_table[src[i] & 0xF];
2953  }
2954 
2955  return buff;
2956 }
2957 
2958 int ff_hex_to_data(uint8_t *data, const char *p)
2959 {
2960  int c, len, v;
2961 
2962  len = 0;
2963  v = 1;
2964  for (;;) {
2965  p += strspn(p, SPACE_CHARS);
2966  if (*p == '\0')
2967  break;
2968  c = av_toupper((unsigned char) *p++);
2969  if (c >= '0' && c <= '9')
2970  c = c - '0';
2971  else if (c >= 'A' && c <= 'F')
2972  c = c - 'A' + 10;
2973  else
2974  break;
2975  v = (v << 4) | c;
2976  if (v & 0x100) {
2977  if (data)
2978  data[len] = v;
2979  len++;
2980  v = 1;
2981  }
2982  }
2983  return len;
2984 }
2985 
2986 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
2987  unsigned int pts_num, unsigned int pts_den)
2988 {
2989  AVRational new_tb;
2990  if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
2991  if (new_tb.num != pts_num)
2993  "st:%d removing common factor %d from timebase\n",
2994  s->index, pts_num / new_tb.num);
2995  } else
2997  "st:%d has too large timebase, reducing\n", s->index);
2998 
2999  if (new_tb.num <= 0 || new_tb.den <= 0) {
3001  "Ignoring attempt to set invalid timebase for st:%d\n",
3002  s->index);
3003  return;
3004  }
3005  s->time_base = new_tb;
3006  s->pts_wrap_bits = pts_wrap_bits;
3007 }
3008 
3009 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
3010  void *context)
3011 {
3012  const char *ptr = str;
3013 
3014  /* Parse key=value pairs. */
3015  for (;;) {
3016  const char *key;
3017  char *dest = NULL, *dest_end;
3018  int key_len, dest_len = 0;
3019 
3020  /* Skip whitespace and potential commas. */
3021  while (*ptr && (av_isspace(*ptr) || *ptr == ','))
3022  ptr++;
3023  if (!*ptr)
3024  break;
3025 
3026  key = ptr;
3027 
3028  if (!(ptr = strchr(key, '=')))
3029  break;
3030  ptr++;
3031  key_len = ptr - key;
3032 
3033  callback_get_buf(context, key, key_len, &dest, &dest_len);
3034  dest_end = dest + dest_len - 1;
3035 
3036  if (*ptr == '\"') {
3037  ptr++;
3038  while (*ptr && *ptr != '\"') {
3039  if (*ptr == '\\') {
3040  if (!ptr[1])
3041  break;
3042  if (dest && dest < dest_end)
3043  *dest++ = ptr[1];
3044  ptr += 2;
3045  } else {
3046  if (dest && dest < dest_end)
3047  *dest++ = *ptr;
3048  ptr++;
3049  }
3050  }
3051  if (*ptr == '\"')
3052  ptr++;
3053  } else {
3054  for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
3055  if (dest && dest < dest_end)
3056  *dest++ = *ptr;
3057  }
3058  if (dest)
3059  *dest = 0;
3060  }
3061 }
3062 
3064 {
3065  int i;
3066  for (i = 0; i < s->nb_streams; i++)
3067  if (s->streams[i]->id == id)
3068  return i;
3069  return -1;
3070 }
3071 
3072 int64_t ff_iso8601_to_unix_time(const char *datestr)
3073 {
3074  struct tm time1 = { 0 }, time2 = { 0 };
3075  const char *ret1, *ret2;
3076  ret1 = av_small_strptime(datestr, "%Y - %m - %d %T", &time1);
3077  ret2 = av_small_strptime(datestr, "%Y - %m - %dT%T", &time2);
3078  if (ret2 && !ret1)
3079  return av_timegm(&time2);
3080  else
3081  return av_timegm(&time1);
3082 }
3083 
3085  int std_compliance)
3086 {
3087  if (ofmt) {
3088  if (ofmt->query_codec)
3089  return ofmt->query_codec(codec_id, std_compliance);
3090  else if (ofmt->codec_tag)
3091  return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
3092  else if (codec_id == ofmt->video_codec ||
3093  codec_id == ofmt->audio_codec ||
3094  codec_id == ofmt->subtitle_codec)
3095  return 1;
3096  }
3097  return AVERROR_PATCHWELCOME;
3098 }
3099 
3101 {
3102 #if CONFIG_NETWORK
3103  int ret;
3105  if ((ret = ff_network_init()) < 0)
3106  return ret;
3107  ff_tls_init();
3108 #endif
3109  return 0;
3110 }
3111 
3113 {
3114 #if CONFIG_NETWORK
3115  ff_network_close();
3116  ff_tls_deinit();
3117 #endif
3118  return 0;
3119 }
3120 
3122  uint64_t channel_layout, int32_t sample_rate,
3124 {
3125  uint32_t flags = 0;
3126  int size = 4;
3127  uint8_t *data;
3128  if (!pkt)
3129  return AVERROR(EINVAL);
3130  if (channels) {
3131  size += 4;
3133  }
3134  if (channel_layout) {
3135  size += 8;
3137  }
3138  if (sample_rate) {
3139  size += 4;
3141  }
3142  if (width || height) {
3143  size += 8;
3145  }
3147  if (!data)
3148  return AVERROR(ENOMEM);
3149  bytestream_put_le32(&data, flags);
3150  if (channels)
3151  bytestream_put_le32(&data, channels);
3152  if (channel_layout)
3153  bytestream_put_le64(&data, channel_layout);
3154  if (sample_rate)
3155  bytestream_put_le32(&data, sample_rate);
3156  if (width || height) {
3157  bytestream_put_le32(&data, width);
3158  bytestream_put_le32(&data, height);
3159  }
3160  return 0;
3161 }
3162 
3164 {
3165  static const uint8_t avci100_1080p_extradata[] = {
3166  // SPS
3167  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3168  0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
3169  0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
3170  0x18, 0x21, 0x02, 0x56, 0xb9, 0x3d, 0x7d, 0x7e,
3171  0x4f, 0xe3, 0x3f, 0x11, 0xf1, 0x9e, 0x08, 0xb8,
3172  0x8c, 0x54, 0x43, 0xc0, 0x78, 0x02, 0x27, 0xe2,
3173  0x70, 0x1e, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00,
3174  0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xca,
3175  0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3176  // PPS
3177  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
3178  0xd0
3179  };
3180  static const uint8_t avci100_1080i_extradata[] = {
3181  // SPS
3182  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3183  0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
3184  0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
3185  0x18, 0x21, 0x03, 0x3a, 0x46, 0x65, 0x6a, 0x65,
3186  0x24, 0xad, 0xe9, 0x12, 0x32, 0x14, 0x1a, 0x26,
3187  0x34, 0xad, 0xa4, 0x41, 0x82, 0x23, 0x01, 0x50,
3188  0x2b, 0x1a, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2e,
3189  0x11, 0x12, 0x08, 0xc6, 0x8c, 0x04, 0x41, 0x28,
3190  0x4c, 0x34, 0xf0, 0x1e, 0x01, 0x13, 0xf2, 0xe0,
3191  0x3c, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03,
3192  0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x00,
3193  // PPS
3194  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
3195  0xd0
3196  };
3197  static const uint8_t avci50_1080i_extradata[] = {
3198  // SPS
3199  0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
3200  0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
3201  0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
3202  0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6e, 0x61,
3203  0x87, 0x3e, 0x73, 0x4d, 0x98, 0x0c, 0x03, 0x06,
3204  0x9c, 0x0b, 0x73, 0xe6, 0xc0, 0xb5, 0x18, 0x63,
3205  0x0d, 0x39, 0xe0, 0x5b, 0x02, 0xd4, 0xc6, 0x19,
3206  0x1a, 0x79, 0x8c, 0x32, 0x34, 0x24, 0xf0, 0x16,
3207  0x81, 0x13, 0xf7, 0xff, 0x80, 0x01, 0x80, 0x02,
3208  0x71, 0x80, 0x80, 0x80, 0xa0, 0x00, 0x00, 0x03,
3209  0x00, 0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00,
3210  // PPS
3211  0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
3212  0x11
3213  };
3214  static const uint8_t avci100_720p_extradata[] = {
3215  // SPS
3216  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3217  0xb6, 0xd4, 0x20, 0x2a, 0x33, 0x1d, 0xc7, 0x62,
3218  0xa1, 0x08, 0x40, 0x54, 0x66, 0x3b, 0x8e, 0xc5,
3219  0x42, 0x02, 0x10, 0x25, 0x64, 0x2c, 0x89, 0xe8,
3220  0x85, 0xe4, 0x21, 0x4b, 0x90, 0x83, 0x06, 0x95,
3221  0xd1, 0x06, 0x46, 0x97, 0x20, 0xc8, 0xd7, 0x43,
3222  0x08, 0x11, 0xc2, 0x1e, 0x4c, 0x91, 0x0f, 0x01,
3223  0x40, 0x16, 0xec, 0x07, 0x8c, 0x04, 0x04, 0x05,
3224  0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03,
3225  0x00, 0x64, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
3226  // PPS
3227  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x31, 0x12,
3228  0x11
3229  };
3230 
3231  const uint8_t *data = NULL;
3232  int size = 0;
3233 
3234  if (st->codecpar->width == 1920) {
3236  data = avci100_1080p_extradata;
3237  size = sizeof(avci100_1080p_extradata);
3238  } else {
3239  data = avci100_1080i_extradata;
3240  size = sizeof(avci100_1080i_extradata);
3241  }
3242  } else if (st->codecpar->width == 1440) {
3243  data = avci50_1080i_extradata;
3244  size = sizeof(avci50_1080i_extradata);
3245  } else if (st->codecpar->width == 1280) {
3246  data = avci100_720p_extradata;
3247  size = sizeof(avci100_720p_extradata);
3248  }
3249 
3250  if (!size)
3251  return 0;
3252 
3253  av_freep(&st->codecpar->extradata);
3254  st->codecpar->extradata_size = 0;
3256  if (!st->codecpar->extradata)
3257  return AVERROR(ENOMEM);
3258 
3259  memcpy(st->codecpar->extradata, data, size);
3260  st->codecpar->extradata_size = size;
3261 
3262  return 0;
3263 }
3264 
3266  int *size)
3267 {
3268  int i;
3269 
3270  for (i = 0; i < st->nb_side_data; i++) {
3271  if (st->side_data[i].type == type) {
3272  if (size)
3273  *size = st->side_data[i].size;
3274  return st->side_data[i].data;
3275  }
3276  }
3277  return NULL;
3278 }
3279 
3281  int size)
3282 {
3283  AVPacketSideData *sd, *tmp;
3284  int i;
3285  uint8_t *data = av_malloc(size);
3286 
3287  if (!data)
3288  return NULL;
3289 
3290  for (i = 0; i < st->nb_side_data; i++) {
3291  sd = &st->side_data[i];
3292 
3293  if (sd->type == type) {
3294  av_freep(&sd->data);
3295  sd->data = data;
3296  sd->size = size;
3297  return sd->data;
3298  }
3299  }
3300 
3301  tmp = av_realloc_array(st->side_data, st->nb_side_data + 1, sizeof(*tmp));
3302  if (!tmp) {
3303  av_freep(&data);
3304  return NULL;
3305  }
3306 
3307  st->side_data = tmp;
3308  st->nb_side_data++;
3309 
3310  sd = &st->side_data[st->nb_side_data - 1];
3311  sd->type = type;
3312  sd->data = data;
3313  sd->size = size;
3314  return data;
3315 }
3316 
3318 {
3319  if (*pb)
3320  s->io_close(s, *pb);
3321  *pb = NULL;
3322 }
unsigned int nb_chapters
Number of chapters in AVChapter array.
Definition: avformat.h:1137
time_t av_timegm(struct tm *tm)
Convert the decomposed UTC time in tm to a time_t value.
Definition: parseutils.c:476
static int add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt, AVPacketList **plast_pktl, int ref)
Definition: utils.c:218
unsigned int max_index_size
Maximum amount of memory in bytes to use for the index of each stream.
Definition: avformat.h:1118
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:2870
codec_id is not known (like AV_CODEC_ID_NONE) but lavf should attempt to identify it ...
Definition: avcodec.h:565
#define AVSEEK_FLAG_BACKWARD
Definition: avformat.h:1657
static void free_stream(AVStream **pst)
Definition: utils.c:2554
int(* io_open)(struct AVFormatContext *s, AVIOContext **pb, const char *url, int flags, AVDictionary **options)
A callback for opening new IO streams.
Definition: avformat.h:1270
int av_opt_get_dict_val(void *obj, const char *name, int search_flags, AVDictionary **out_val)
Definition: opt.c:487
int64_t first_dts
Definition: avformat.h:862
AVChapter * avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
Add a new chapter.
Definition: utils.c:2758
const struct AVCodec * codec
Definition: avcodec.h:1418
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:659
AVRational framerate
Definition: avcodec.h:3063
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
Definition: avcodec.h:608
enum AVFieldOrder field_order
Video only.
Definition: avcodec.h:3540
static void fill_all_stream_timings(AVFormatContext *ic)
Definition: utils.c:1677
#define AVFMT_NOBINSEARCH
Format does not allow to fall back on binary search via read_timestamp.
Definition: avformat.h:426
Bytestream IO Context.
Definition: avio.h:104
static void update_initial_durations(AVFormatContext *s, AVStream *st, int stream_index, int duration)
Definition: utils.c:600
AVProbeData probe_data
Definition: avformat.h:886
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:297
AVPacketSideDataType
Definition: avcodec.h:1191
enum AVCodecID id
Definition: internal.h:37
static av_const int av_isdigit(int c)
Locale-independent conversion of ASCII isdigit.
Definition: avstring.h:157
int size
struct AVPacketList * raw_packet_buffer
Raw packets from the demuxer, prior to parsing and decoding.
Definition: internal.h:70
This structure describes decoded (raw) audio or video data.
Definition: frame.h:140
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1181
#define LIBAV_CONFIGURATION
Definition: config.h:4
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:1214
static void update_stream_timings(AVFormatContext *ic)
Estimate the stream timings from the one of each components.
Definition: utils.c:1634
struct AVPacketList * parse_queue_end
Definition: internal.h:76
enum AVCodecID id
Definition: mxfenc.c:85
static int get_std_framerate(int i)
Definition: utils.c:2084
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:40
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:130
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:284
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:99
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:1261
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition: avformat.h:888
static const uint8_t frame_size[4]
Definition: g723_1.h:219
int64_t pos
Definition: avformat.h:664
int probe_packets
Definition: avformat.h:871
#define NTP_OFFSET_US
Definition: internal.h:166
#define AVSEEK_FLAG_ANY
seek to any frame, even non-keyframes
Definition: avformat.h:1659
int64_t data_offset
offset of the first packet
Definition: internal.h:62
enum AVCodecID video_codec
default video codec
Definition: avformat.h:461
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:599
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: avcodec.h:3483
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:770
int num
numerator
Definition: rational.h:44
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:1222
int index
stream index in AVFormatContext
Definition: avformat.h:706
int size
Definition: avcodec.h:1347
static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:1739
#define ID3v2_DEFAULT_MAGIC
Default magic bytes for ID3v2 header: "ID3".
Definition: id3v2.h:35
av_log(ac->avr, AV_LOG_TRACE, "%d samples - audio_convert: %s to %s (%s)\, len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic ? ac->func_descr_generic :ac->func_descr)
int64_t avio_seek(AVIOContext *s, int64_t offset, int whence)
fseek() equivalent for AVIOContext.
Definition: aviobuf.c:242
#define AVIO_FLAG_READ
read-only
Definition: avio.h:368
#define AVPROBE_PADDING_SIZE
extra allocated bytes at the end of the probe buffer
Definition: avformat.h:409
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:890
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:173
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1243
int(* read_close)(struct AVFormatContext *)
Close the stream.
Definition: avformat.h:611
int ff_find_stream_index(AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: utils.c:3063
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1621
uint8_t * av_stream_get_side_data(AVStream *st, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:3265
void ff_network_close(void)
Definition: network.c:77
int event_flags
Flags for the user to detect events happening on the file.
Definition: avformat.h:1218
int av_dict_copy(AVDictionary **dst, const AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:189
#define AV_DISPOSITION_HEARING_IMPAIRED
stream for hearing impaired audiences
Definition: avformat.h:685
int(* split)(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: avcodec.h:4539
static void estimate_timings_from_bit_rate(AVFormatContext *ic)
Definition: utils.c:1696
void * priv_data
Definition: avformat.h:720
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: avcodec.h:889
#define AV_DISPOSITION_CLEAN_EFFECTS
stream without voice
Definition: avformat.h:687
struct AVStream::@125 * info
int duration
Duration of the current frame.
Definition: avcodec.h:4483
discard all
Definition: avcodec.h:689
AVPacketSideData * side_data
An array of side data that applies to the whole stream (i.e.
Definition: avformat.h:808
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:3009
int priv_data_size
Size of private data so that it can be allocated in the wrapper.
Definition: avformat.h:580
int ctx_flags
Flags signalling stream properties.
Definition: avformat.h:989
AVDictionary * metadata
Definition: avformat.h:927
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:351
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
Definition: utils.c:920
int id
Definition: avformat.h:911
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:748
enum AVCodecID subtitle_codec_id
Forced subtitle codec_id.
Definition: avformat.h:1106
AVCodec.
Definition: avcodec.h:3120
This struct describes the properties of an encoded stream.
Definition: avcodec.h:3475
static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:1816
static int has_decode_delay_been_guessed(AVStream *st)
Definition: utils.c:1886
#define AV_DISPOSITION_KARAOKE
Definition: avformat.h:677
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:2217
Format I/O context.
Definition: avformat.h:940
static int has_codec_parameters(AVStream *st)
Definition: utils.c:1862
unsigned int nb_stream_indexes
Definition: avformat.h:915
int ff_network_inited_globally
Definition: network.c:48
#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:1057
int64_t cur_dts
Definition: avformat.h:863
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
internal metadata API header see avformat.h or the public API!
Public dictionary API.
#define DURATION_MAX_READ_SIZE
Definition: utils.c:1735
#define AVFMT_FLAG_DISCARD_CORRUPT
Discard frames marked corrupted.
Definition: avformat.h:1060
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:628
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2160
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:68
int width
Video only.
Definition: avcodec.h:3525
int ff_network_init(void)
Definition: network.c:50
#define AVFMTCTX_NOHEADER
signal that no header is present (streams are added dynamically)
Definition: avformat.h:919
int(* query_codec)(enum AVCodecID id, int std_compliance)
Test if the given codec can be stored in this container.
Definition: avformat.h:514
AVOptions.
int subtitle_header_size
Definition: avcodec.h:3000
static int probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition: utils.c:378
int flags
Can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_SHOW_IDS, AVFMT_GENERIC_INDEX, AVFMT_TS_DISCONT, AVFMT_NOBINSEARCH, AVFMT_NOGENSEARCH, AVFMT_NO_BYTE_SEEK.
Definition: avformat.h:543
AVCodecParameters * avcodec_parameters_alloc(void)
Allocate a new AVCodecParameters and set its fields to default values (unknown/invalid/0).
Definition: utils.c:2798
unsigned avformat_version(void)
Return the LIBAVFORMAT_VERSION_INT constant.
Definition: utils.c:58
const char * avformat_license(void)
Return the libavformat license.
Definition: utils.c:68
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
Definition: log.h:150
AVPacket pkt
Definition: avformat.h:1298
int id
unique ID to identify the chapter
Definition: avformat.h:924
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
int id
Format-specific stream ID.
Definition: avformat.h:712
enum AVStreamParseType need_parsing
Definition: avformat.h:879
#define b
Definition: input.c:52
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:1983
#define AVFMT_FLAG_GENPTS
Generate missing pts even if it requires parsing future frames.
Definition: avformat.h:1052
int nb_side_data
The number of elements in the AVStream.side_data array.
Definition: avformat.h:812
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1523
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2648
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1008
int64_t duration
Definition: movenc.c:63
int avcodec_parameters_to_context(AVCodecContext *codec, const AVCodecParameters *par)
Fill the codec context based on the values from the supplied codec parameters.
Definition: utils.c:2885
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:136
const char * avformat_configuration(void)
Return the libavformat build-time configuration.
Definition: utils.c:63
const char data[16]
Definition: mxf.c:70
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:554
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
int flags
Flags modifying the (de)muxer behaviour.
Definition: avformat.h:1051
#define DURATION_MAX_RETRY
Definition: utils.c:1736
uint8_t * data
Definition: avcodec.h:1346
int av_reallocp(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:140
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition: utils.c:2735
static int flags
Definition: log.c:50
uint32_t tag
Definition: movenc.c:854
int avformat_network_init(void)
Do global initialization of network components.
Definition: utils.c:3100
int(* read_play)(struct AVFormatContext *)
Start/resume playing - only meaningful if using a network-based format (RTSP).
Definition: avformat.h:635
int fps_probe_size
The number of frames used for determining the framerate in avformat_find_stream_info().
Definition: avformat.h:1163
#define AVERROR_EOF
End of file.
Definition: error.h:51
uint8_t * data
Definition: avcodec.h:1290
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:295
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2769
struct AVPacketList * packet_buffer
This buffer is only needed when packets were already buffered but not decoded, for example to get the...
Definition: internal.h:58
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition: avformat.h:913
static int genpts
Definition: avplay.c:250
static AVPacket flush_pkt
Definition: avplay.c:274
int raw_packet_buffer_remaining_size
Definition: internal.h:81
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:1995
unsigned int * stream_index
Definition: avformat.h:914
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:1094
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd, int score)
Definition: utils.c:141
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:545
static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
Definition: utils.c:800
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1378
const OptionDef options[]
Definition: avconv_opt.c:2447
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: utils.c:416
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:99
#define src
Definition: vp8dsp.c:254
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:2482
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: utils.c:1943
#define AVINDEX_KEYFRAME
Definition: avformat.h:666
int avctx_inited
1 if avctx has been initialized with the values from the codec parameters
Definition: internal.h:112
int max_ts_probe
Maximum number of packets to read while waiting for the first timestamp.
Definition: avformat.h:1225
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1111
static int64_t start_time
Definition: avplay.c:245
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:168
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:193
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:124
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:2348
AVDictionary * metadata
Metadata that applies to the whole file.
Definition: avformat.h:1148
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1715
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
int nb_decoded_frames
Definition: avformat.h:846
int64_t pos
Byte position of currently parsed frame in stream.
Definition: avcodec.h:4471
int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1255
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
Definition: mem.c:375
char * ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
Definition: utils.c:2937
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:1151
struct AVCodecParser * parser
Definition: avcodec.h:4364
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: avcodec.h:1218
#define AVERROR(e)
Definition: error.h:43
#define SANE_CHUNK_SIZE
Definition: utils.c:75
#define AVFMT_FLAG_IGNDTS
Ignore DTS on frames that contain both DTS & PTS.
Definition: avformat.h:1055
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:2647
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:80
int64_t timestamp
Definition: avformat.h:665
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:983
int avcodec_is_open(AVCodecContext *s)
Definition: utils.c:2702
int attribute_align_arg avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
Definition: utils.c:1807
int capabilities
Codec capabilities.
Definition: avcodec.h:3139
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:145
unsigned int nb_programs
Definition: avformat.h:1087
int last_IP_duration
Definition: avformat.h:865
int av_read_play(AVFormatContext *s)
Start playing a network-based stream (e.g.
Definition: utils.c:2536
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:175
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:479
enum AVMediaType codec_type
General type of the encoded data.
Definition: avcodec.h:3479
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1329
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:734
AVChapter ** chapters
Definition: avformat.h:1138
simple assert() macros that are a bit more flexible than ISO C assert().
enum AVPacketSideDataType type
Definition: avcodec.h:1292
int side_data_elems
Definition: avcodec.h:1358
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:781
New fields can be added to the end with minor version bumps.
Definition: avformat.h:910
#define FFMAX(a, b)
Definition: common.h:64
void avcodec_parameters_free(AVCodecParameters **ppar)
Free an AVCodecParameters instance and everything associated with it and write NULL to the supplied p...
Definition: utils.c:2808
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:2515
AVInputFormat * av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: format.c:171
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:669
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:81
#define fail()
Definition: checkasm.h:80
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2462
int extradata_size
Size of the extradata content in bytes.
Definition: avcodec.h:3501
Only parse headers, do not repack.
Definition: avformat.h:658
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:3084
#define AVFMT_FLAG_NOBUFFER
Do not buffer frames when possible.
Definition: avformat.h:1058
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:1164
static float distance(float x, float y, int band)
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: avcodec.h:596
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
int bit_rate
The average bitrate of the encoded data (in bits per second).
Definition: avcodec.h:3512
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:401
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:2986
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:400
static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
Definition: utils.c:189
common internal API header
#define FF_MAX_EXTRADATA_SIZE
Maximum size in bytes of extradata.
Definition: internal.h:179
unsigned int nb_streams
Number of elements in AVFormatContext.streams.
Definition: avformat.h:996
int64_t fps_first_dts
Those are used for average framerate estimation.
Definition: avformat.h:852
int av_read_pause(AVFormatContext *s)
Pause a network-based stream (e.g.
Definition: utils.c:2545
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:91
const char * name
Definition: qsvenc.c:44
int seekable
A combination of AVIO_SEEKABLE_ flags or 0 when the stream is not seekable.
Definition: avio.h:153
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:134
char filename[1024]
input or output filename
Definition: avformat.h:1016
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:86
#define AV_TIME_BASE
Internal time base represented as integer.
Definition: avutil.h:241
#define FFMIN(a, b)
Definition: common.h:66
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:1100
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:242
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:136
#define AV_OPT_SEARCH_CHILDREN
Search in possible children of the given object first.
Definition: opt.h:383
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:128
enum AVCodecID codec_id
Definition: avconv_vaapi.c:149
static const chunk_decoder decoder[8]
Definition: dfa.c:322
int width
picture width / height.
Definition: avcodec.h:1580
int64_t offset
byte offset from starting packet start
Definition: avcodec.h:4401
int av_find_default_stream_index(AVFormatContext *s)
Definition: utils.c:1089
int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt, const char *filename, void *logctx, unsigned int offset, unsigned int max_probe_size)
Probe a bytestream to determine the input format.
Definition: format.c:238
internal header for RIFF based (de)muxers do NOT include this in end user applications ...
int32_t
void av_parser_close(AVCodecParserContext *s)
Definition: parser.c:217
static int seek_frame_generic(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: utils.c:1462
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:1328
#define AVFMT_EVENT_FLAG_METADATA_UPDATED
The call resulted in updated metadata.
Definition: avformat.h:1219
void ff_format_io_close(AVFormatContext *s, AVIOContext **pb)
A wrapper around AVFormatContext.io_close that should be used instead of calling the pointer directly...
Definition: utils.c:3317
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:514
AVDictionary * metadata
Definition: avformat.h:772
uint8_t * av_stream_new_side_data(AVStream *st, enum AVPacketSideDataType type, int size)
Allocate new information from stream.
Definition: utils.c:3280
const char * av_small_strptime(const char *p, const char *fmt, struct tm *dt)
Simplified version of strptime.
Definition: parseutils.c:407
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1544
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don&#39;t avio_close() it.
Definition: avformat.h:1059
#define AVERROR_EXIT
Immediate exit was requested; the called function should not be restarted.
Definition: error.h:52
unsigned int probesize
Maximum size of the data read from input for determining the input container format.
Definition: avformat.h:1075
int(* read_seek2)(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Seek to timestamp ts.
Definition: avformat.h:649
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:198
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:1137
#define AV_DISPOSITION_ATTACHED_PIC
The stream is stored in the file as an attached picture/"cover art" (e.g.
Definition: avformat.h:694
int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
Return in &#39;buf&#39; the path with &#39;d&#39; replaced by a number.
Definition: utils.c:2817
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:127
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag, including supported extra metadata.
Definition: id3v2.c:702
if(ac->has_optimized_func)
#define AV_DISPOSITION_VISUAL_IMPAIRED
stream for visual impaired audiences
Definition: avformat.h:686
int(* read_pause)(struct AVFormatContext *)
Pause playing - only meaningful if using a network-based format (RTSP).
Definition: avformat.h:641
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:40
Stream structure.
Definition: avformat.h:705
#define AVERROR_PATCHWELCOME
Not yet implemented in Libav, patches welcome.
Definition: error.h:57
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:926
int attribute_align_arg avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
Definition: utils.c:1767
#define MAX_PROBE_PACKETS
Number of packets to buffer for codec probing.
Definition: avformat.h:870
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:3112
#define LIBAV_LICENSE
Definition: config.h:5
NULL
Definition: eval.c:55
#define FF_FDEBUG_TS
Definition: avformat.h:1187
static int width
Definition: utils.c:156
#define LIBAVFORMAT_VERSION_INT
Definition: version.h:36
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:48
AVStreamInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:899
enum AVMediaType codec_type
Definition: avcodec.h:1417
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:1973
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrupt a blocking function associated with cb.
Definition: avio.c:374
int debug
Flags to enable debugging.
Definition: avformat.h:1186
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:151
enum AVCodecID codec_id
Definition: avcodec.h:1426
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:247
static int append_packet_chunked(AVIOContext *s, AVPacket *pkt, int size)
Definition: utils.c:79
int av_opt_set_dict(void *obj, AVDictionary **options)
Definition: opt.c:735
int sample_rate
samples per second
Definition: avcodec.h:2152
AVIOContext * pb
I/O context.
Definition: avformat.h:982
unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
Definition: utils.c:2038
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:475
int(* read_packet)(struct AVFormatContext *, AVPacket *pkt)
Read one packet and put it in &#39;pkt&#39;.
Definition: avformat.h:605
static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts, int64_t pts)
Definition: utils.c:569
main external API structure.
Definition: avcodec.h:1409
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:2029
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:134
#define AVFMT_NOGENSEARCH
Format does not allow to fall back on generic search.
Definition: avformat.h:427
void(* io_close)(struct AVFormatContext *s, AVIOContext *pb)
A callback for closing the streams opened with AVFormatContext.io_open().
Definition: avformat.h:1276
static void compute_pkt_fields(AVFormatContext *s, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Definition: utils.c:641
int extradata_size
Definition: avcodec.h:1524
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:68
int nb_index_entries
Definition: avformat.h:892
enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
Definition: utils.c:2049
Describe the class of an AVClass context structure.
Definition: log.h:34
int ff_hex_to_data(uint8_t *data, const char *p)
Parse a string of hexadecimal strings.
Definition: utils.c:2958
static av_const int av_toupper(int c)
Locale-independent conversion of ASCII characters to uppercase.
Definition: avstring.h:182
int index
Definition: gxfenc.c:72
#define AVFMT_GENERIC_INDEX
Use generic index building code.
Definition: avformat.h:421
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:462
#define SPACE_CHARS
Definition: internal.h:230
rational number numerator/denominator
Definition: rational.h:43
int avcodec_parameters_from_context(AVCodecParameters *par, const AVCodecContext *codec)
Fill the parameters struct based on the values from the supplied codec context.
Definition: utils.c:2837
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition: utils.c:2812
#define AVSEEK_FLAG_BYTE
seeking based on position in bytes
Definition: avformat.h:1658
AVMediaType
Definition: avutil.h:192
int ff_generate_avci_extradata(AVStream *st)
Generate standard extradata for AVC-Intra based on width/height and field order.
Definition: utils.c:3163
int64_t fps_last_dts
Definition: avformat.h:854
#define RAW_PACKET_BUFFER_SIZE
Remaining size available for raw_packet_buffer, in bytes.
Definition: internal.h:80
static int seek_frame_internal(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: utils.c:1520
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:3072
static int step
Definition: avplay.c:247
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:850
int found_decoder
Definition: avformat.h:847
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2594
This structure contains the data a format has to probe a file.
Definition: avformat.h:398
misc parsing utilities
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1021
static int has_duration(AVFormatContext *ic)
Return TRUE if the stream has accurate duration in any stream.
Definition: utils.c:1614
int avio_pause(AVIOContext *h, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:1024
Round toward -infinity.
Definition: mathematics.h:52
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:1575
static int try_decode_frame(AVFormatContext *s, AVStream *st, AVPacket *avpkt, AVDictionary **options)
Definition: utils.c:1893
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:4417
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:580
full parsing and repack of the first frame only, only implemented for H.264 currently ...
Definition: avformat.h:660
AVDictionary * metadata
Definition: avformat.h:916
static int64_t pts
Global timestamp for the audio frames.
int fps_first_dts_idx
Definition: avformat.h:853
int64_t start_time
Position of the first frame of the component, in AV_TIME_BASE fractional seconds. ...
Definition: avformat.h:1025
#define LICENSE_PREFIX
int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Seek to the keyframe at timestamp.
Definition: utils.c:1564
unsigned int tag
Definition: internal.h:38
int height
Definition: gxfenc.c:72
static int is_intra_only(enum AVCodecID id)
Definition: utils.c:559
struct AVPacketList * parse_queue
Packets split by the parser get queued here.
Definition: internal.h:75
int64_t start
Definition: avformat.h:926
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:757
#define PARSER_FLAG_ONCE
Definition: avcodec.h:4397
int sample_rate
Audio only.
Definition: avcodec.h:3564
enum AVMediaType type
Definition: avcodec.h:582
#define AVPROBE_SCORE_MAX
maximum score
Definition: avformat.h:407
Main libavformat public API header.
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: avcodec.h:1357
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:1375
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:715
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
common internal api header.
struct AVPacketList * next
Definition: avformat.h:1299
void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
Definition: utils.c:2783
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:412
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:2092
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:750
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:761
enum AVCodecID orig_codec_id
Definition: internal.h:114
int(* read_seek)(struct AVFormatContext *, int stream_index, int64_t timestamp, int flags)
Seek to a given timestamp relative to the frames in stream component stream_index.
Definition: avformat.h:621
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:859
Bi-dir predicted.
Definition: avutil.h:262
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:925
#define PARSER_FLAG_COMPLETE_FRAMES
Definition: avcodec.h:4396
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:161
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:107
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:31
int den
denominator
Definition: rational.h:45
#define AV_PKT_FLAG_CORRUPT
The packet content is corrupted.
Definition: avcodec.h:1379
unsigned bps
Definition: movenc.c:855
int max_analyze_duration
Maximum duration (in AV_TIME_BASE units) of the data read from input in avformat_find_stream_info().
Definition: avformat.h:1082
struct AVInputFormat * iformat
The input container format.
Definition: avformat.h:952
void avformat_close_input(AVFormatContext **ps)
Close an opened input AVFormatContext.
Definition: utils.c:2626
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
Definition: utils.c:2091
int av_packet_ref(AVPacket *dst, AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:356
unsigned int index_entries_allocated_size
Definition: avformat.h:893
#define AVERROR_DECODER_NOT_FOUND
Decoder not found.
Definition: error.h:48
int64_t frame_offset
Definition: avcodec.h:4365
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
Parse a packet, add all split parts to parse_queue.
Definition: utils.c:816
static int read_from_packet_buffer(AVPacketList **pkt_buffer, AVPacketList **pkt_buffer_end, AVPacket *pkt)
Definition: utils.c:905
struct AVPacketList * packet_buffer_end
Definition: internal.h:59
static AVProgram * find_program_from_stream(AVFormatContext *ic, int s)
Definition: utils.c:2471
#define AVFMT_NO_BYTE_SEEK
Format does not allow seeking by bytes.
Definition: avformat.h:428
static int queue_attached_pictures(AVFormatContext *s)
Definition: utils.c:246
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
int(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition: avformat.h:594
int len
int channels
number of audio channels
Definition: avcodec.h:2153
enum AVCodecID audio_codec
default audio codec
Definition: avformat.h:460
#define av_log2
Definition: intmath.h:85
void ff_tls_init(void)
Definition: network.c:28
static uint8_t tmp[8]
Definition: des.c:38
struct AVCodecParserContext * parser
Definition: avformat.h:880
void * priv_data
Format private data.
Definition: avformat.h:968
int codec_info_nb_frames
Number of frames that have been demuxed during av_find_stream_info()
Definition: avformat.h:876
#define AVERROR_STREAM_NOT_FOUND
Stream not found.
Definition: error.h:59
static void flush_packet_queue(AVFormatContext *s)
Definition: utils.c:1077
#define AV_DISPOSITION_COMMENT
Definition: avformat.h:675
uint8_t * extradata
Extra binary data needed for initializing the decoder, codec-dependent.
Definition: avcodec.h:3497
AVCodecContext * avctx
The codec context used by avformat_find_stream_info, the parser, etc.
Definition: internal.h:108
#define av_uninit(x)
Definition: attributes.h:109
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags)
Definition: utils.c:1444
int bit_rate
Total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:1042
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:4380
int64_t duration
Duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:1035
int64_t last_IP_pts
Definition: avformat.h:864
void ff_tls_deinit(void)
Definition: network.c:38
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:529
unbuffered private I/O API
static void compute_chapters_end(AVFormatContext *s)
Definition: utils.c:2060
AVCodecParameters * codecpar
Definition: avformat.h:831
#define FFSWAP(type, a, b)
Definition: common.h:69
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:263
int fps_last_dts_idx
Definition: avformat.h:855
int stream_index
Definition: avcodec.h:1348
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:742
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:763
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:3121
AVInputFormat * av_probe_input_format(AVProbeData *pd, int is_opened)
Guess the file format.
Definition: format.c:228
This structure stores compressed data.
Definition: avcodec.h:1323
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:4410
#define AVFMT_FLAG_NOFILLIN
Do not infer any values from other values, just return what is stored in the container.
Definition: avformat.h:1056
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
#define MAX_STD_TIMEBASES
Stream information used internally by av_find_stream_info()
Definition: avformat.h:844
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:790
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235
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:117
#define av_unused
Definition: attributes.h:86
AVProgram ** programs
Definition: avformat.h:1088
#define AVFMT_NEEDNUMBER
Needs &#39;d&#39; in filename.
Definition: avformat.h:413
discard nothing
Definition: avcodec.h:684
struct AVPacketList * raw_packet_buffer_end
Definition: internal.h:71
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
Compare 2 integers modulo mod.
Definition: mathematics.c:115
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:2999