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