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 
460 {
461  int frame_size;
462 
463  /* give frame_size priority if demuxing */
464  if (!mux && enc->frame_size > 1)
465  return enc->frame_size;
466 
467  if ((frame_size = av_get_audio_frame_duration(enc, size)) > 0)
468  return frame_size;
469 
470  /* Fall back on using frame_size if muxing. */
471  if (enc->frame_size > 1)
472  return enc->frame_size;
473 
474  return -1;
475 }
476 
480 void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st,
481  AVCodecParserContext *pc, AVPacket *pkt)
482 {
483  int frame_size;
484 
485  *pnum = 0;
486  *pden = 0;
487  switch (st->codec->codec_type) {
488  case AVMEDIA_TYPE_VIDEO:
489  if (st->avg_frame_rate.num) {
490  *pnum = st->avg_frame_rate.den;
491  *pden = st->avg_frame_rate.num;
492  } else if (st->time_base.num * 1000LL > st->time_base.den) {
493  *pnum = st->time_base.num;
494  *pden = st->time_base.den;
495  } else if (st->codec->time_base.num * 1000LL > st->codec->time_base.den) {
496  *pnum = st->codec->time_base.num;
497  *pden = st->codec->time_base.den;
498  if (pc && pc->repeat_pict) {
499  if (*pnum > INT_MAX / (1 + pc->repeat_pict))
500  *pden /= 1 + pc->repeat_pict;
501  else
502  *pnum *= 1 + pc->repeat_pict;
503  }
504  /* If this codec can be interlaced or progressive then we need
505  * a parser to compute duration of a packet. Thus if we have
506  * no parser in such case leave duration undefined. */
507  if (st->codec->ticks_per_frame > 1 && !pc)
508  *pnum = *pden = 0;
509  }
510  break;
511  case AVMEDIA_TYPE_AUDIO:
512  frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 0);
513  if (frame_size <= 0 || st->codec->sample_rate <= 0)
514  break;
515  *pnum = frame_size;
516  *pden = st->codec->sample_rate;
517  break;
518  default:
519  break;
520  }
521 }
522 
523 static int is_intra_only(enum AVCodecID id)
524 {
526  if (!d)
527  return 0;
529  return 0;
530  return 1;
531 }
532 
533 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
534  int64_t dts, int64_t pts)
535 {
536  AVStream *st = s->streams[stream_index];
537  AVPacketList *pktl = s->packet_buffer;
538 
539  if (st->first_dts != AV_NOPTS_VALUE ||
540  dts == AV_NOPTS_VALUE ||
541  st->cur_dts == AV_NOPTS_VALUE)
542  return;
543 
544  st->first_dts = dts - st->cur_dts;
545  st->cur_dts = dts;
546 
547  for (; pktl; pktl = pktl->next) {
548  if (pktl->pkt.stream_index != stream_index)
549  continue;
550  // FIXME: think more about this check
551  if (pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
552  pktl->pkt.pts += st->first_dts;
553 
554  if (pktl->pkt.dts != AV_NOPTS_VALUE)
555  pktl->pkt.dts += st->first_dts;
556 
557  if (st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
558  st->start_time = pktl->pkt.pts;
559  }
560  if (st->start_time == AV_NOPTS_VALUE)
561  st->start_time = pts;
562 }
563 
565  int stream_index, int duration)
566 {
567  AVPacketList *pktl = s->packet_buffer;
568  int64_t cur_dts = 0;
569 
570  if (st->first_dts != AV_NOPTS_VALUE) {
571  cur_dts = st->first_dts;
572  for (; pktl; pktl = pktl->next) {
573  if (pktl->pkt.stream_index == stream_index) {
574  if (pktl->pkt.pts != pktl->pkt.dts ||
575  pktl->pkt.dts != AV_NOPTS_VALUE ||
576  pktl->pkt.duration)
577  break;
578  cur_dts -= duration;
579  }
580  }
581  pktl = s->packet_buffer;
582  st->first_dts = cur_dts;
583  } else if (st->cur_dts)
584  return;
585 
586  for (; pktl; pktl = pktl->next) {
587  if (pktl->pkt.stream_index != stream_index)
588  continue;
589  if (pktl->pkt.pts == pktl->pkt.dts &&
590  pktl->pkt.dts == AV_NOPTS_VALUE &&
591  !pktl->pkt.duration) {
592  pktl->pkt.dts = cur_dts;
593  if (!st->codec->has_b_frames)
594  pktl->pkt.pts = cur_dts;
595  cur_dts += duration;
596  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
597  pktl->pkt.duration = duration;
598  } else
599  break;
600  }
601  if (st->first_dts == AV_NOPTS_VALUE)
602  st->cur_dts = cur_dts;
603 }
604 
606  AVCodecParserContext *pc, AVPacket *pkt)
607 {
608  int num, den, presentation_delayed, delay, i;
609  int64_t offset;
610 
611  if (s->flags & AVFMT_FLAG_NOFILLIN)
612  return;
613 
614  if ((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
615  pkt->dts = AV_NOPTS_VALUE;
616 
617  /* do we have a video B-frame ? */
618  delay = st->codec->has_b_frames;
619  presentation_delayed = 0;
620 
621  /* XXX: need has_b_frame, but cannot get it if the codec is
622  * not initialized */
623  if (delay &&
624  pc && pc->pict_type != AV_PICTURE_TYPE_B)
625  presentation_delayed = 1;
626 
627  if (pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE &&
628  st->pts_wrap_bits < 63 &&
629  pkt->dts - (1LL << (st->pts_wrap_bits - 1)) > pkt->pts) {
630  pkt->dts -= 1LL << st->pts_wrap_bits;
631  }
632 
633  /* Some MPEG-2 in MPEG-PS lack dts (issue #171 / input_file.mpg).
634  * We take the conservative approach and discard both.
635  * Note: If this is misbehaving for an H.264 file, then possibly
636  * presentation_delayed is not set correctly. */
637  if (delay == 1 && pkt->dts == pkt->pts &&
638  pkt->dts != AV_NOPTS_VALUE && presentation_delayed) {
639  av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
640  pkt->dts = pkt->pts = AV_NOPTS_VALUE;
641  }
642 
643  if (pkt->duration == 0 && st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
644  ff_compute_frame_duration(&num, &den, st, pc, pkt);
645  if (den && num) {
646  pkt->duration = av_rescale_rnd(1, num * (int64_t) st->time_base.den,
647  den * (int64_t) st->time_base.num,
648  AV_ROUND_DOWN);
649 
650  if (pkt->duration != 0 && s->packet_buffer)
652  pkt->duration);
653  }
654  }
655 
656  /* Correct timestamps with byte offset if demuxers only have timestamps
657  * on packet boundaries */
658  if (pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size) {
659  /* this will estimate bitrate based on this frame's duration and size */
660  offset = av_rescale(pc->offset, pkt->duration, pkt->size);
661  if (pkt->pts != AV_NOPTS_VALUE)
662  pkt->pts += offset;
663  if (pkt->dts != AV_NOPTS_VALUE)
664  pkt->dts += offset;
665  }
666 
667  /* This may be redundant, but it should not hurt. */
668  if (pkt->dts != AV_NOPTS_VALUE &&
669  pkt->pts != AV_NOPTS_VALUE &&
670  pkt->pts > pkt->dts)
671  presentation_delayed = 1;
672 
673  av_dlog(NULL,
674  "IN delayed:%d pts:%"PRId64", dts:%"PRId64" "
675  "cur_dts:%"PRId64" st:%d pc:%p\n",
676  presentation_delayed, pkt->pts, pkt->dts, st->cur_dts,
677  pkt->stream_index, pc);
678  /* Interpolate PTS and DTS if they are not present. We skip H.264
679  * currently because delay and has_b_frames are not reliably set. */
680  if ((delay == 0 || (delay == 1 && pc)) &&
681  st->codec->codec_id != AV_CODEC_ID_H264) {
682  if (presentation_delayed) {
683  /* DTS = decompression timestamp */
684  /* PTS = presentation timestamp */
685  if (pkt->dts == AV_NOPTS_VALUE)
686  pkt->dts = st->last_IP_pts;
687  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
688  if (pkt->dts == AV_NOPTS_VALUE)
689  pkt->dts = st->cur_dts;
690 
691  /* This is tricky: the dts must be incremented by the duration
692  * of the frame we are displaying, i.e. the last I- or P-frame. */
693  if (st->last_IP_duration == 0)
694  st->last_IP_duration = pkt->duration;
695  if (pkt->dts != AV_NOPTS_VALUE)
696  st->cur_dts = pkt->dts + st->last_IP_duration;
697  st->last_IP_duration = pkt->duration;
698  st->last_IP_pts = pkt->pts;
699  /* Cannot compute PTS if not present (we can compute it only
700  * by knowing the future. */
701  } else if (pkt->pts != AV_NOPTS_VALUE ||
702  pkt->dts != AV_NOPTS_VALUE ||
703  pkt->duration ||
705  int duration = pkt->duration;
706  if (!duration && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
707  ff_compute_frame_duration(&num, &den, st, pc, pkt);
708  if (den && num) {
709  duration = av_rescale_rnd(1,
710  num * (int64_t) st->time_base.den,
711  den * (int64_t) st->time_base.num,
712  AV_ROUND_DOWN);
713  if (duration != 0 && s->packet_buffer)
715  duration);
716  }
717  }
718 
719  if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE ||
720  duration) {
721  /* presentation is not delayed : PTS and DTS are the same */
722  if (pkt->pts == AV_NOPTS_VALUE)
723  pkt->pts = pkt->dts;
725  pkt->pts);
726  if (pkt->pts == AV_NOPTS_VALUE)
727  pkt->pts = st->cur_dts;
728  pkt->dts = pkt->pts;
729  if (pkt->pts != AV_NOPTS_VALUE)
730  st->cur_dts = pkt->pts + duration;
731  }
732  }
733  }
734 
735  if (pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
736  st->pts_buffer[0] = pkt->pts;
737  for (i = 0; i<delay && st->pts_buffer[i] > st->pts_buffer[i + 1]; i++)
738  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i + 1]);
739  if (pkt->dts == AV_NOPTS_VALUE)
740  pkt->dts = st->pts_buffer[0];
741  // We skipped it above so we try here.
742  if (st->codec->codec_id == AV_CODEC_ID_H264)
743  // This should happen on the first packet
744  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
745  if (pkt->dts > st->cur_dts)
746  st->cur_dts = pkt->dts;
747  }
748 
749  av_dlog(NULL,
750  "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n",
751  presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
752 
753  /* update flags */
754  if (is_intra_only(st->codec->codec_id))
755  pkt->flags |= AV_PKT_FLAG_KEY;
756  if (pc)
758 }
759 
760 static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
761 {
762  while (*pkt_buf) {
763  AVPacketList *pktl = *pkt_buf;
764  *pkt_buf = pktl->next;
765  av_free_packet(&pktl->pkt);
766  av_freep(&pktl);
767  }
768  *pkt_buf_end = NULL;
769 }
770 
776 static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
777 {
778  AVPacket out_pkt = { 0 }, flush_pkt = { 0 };
779  AVStream *st = s->streams[stream_index];
780  uint8_t *data = pkt ? pkt->data : NULL;
781  int size = pkt ? pkt->size : 0;
782  int ret = 0, got_output = 0;
783 
784  if (!pkt) {
786  pkt = &flush_pkt;
787  got_output = 1;
788  }
789 
790  while (size > 0 || (pkt == &flush_pkt && got_output)) {
791  int len;
792 
793  av_init_packet(&out_pkt);
794  len = av_parser_parse2(st->parser, st->codec,
795  &out_pkt.data, &out_pkt.size, data, size,
796  pkt->pts, pkt->dts, pkt->pos);
797 
798  pkt->pts = pkt->dts = AV_NOPTS_VALUE;
799  /* increment read pointer */
800  data += len;
801  size -= len;
802 
803  got_output = !!out_pkt.size;
804 
805  if (!out_pkt.size)
806  continue;
807 
808  if (pkt->side_data) {
809  out_pkt.side_data = pkt->side_data;
810  out_pkt.side_data_elems = pkt->side_data_elems;
811  pkt->side_data = NULL;
812  pkt->side_data_elems = 0;
813  }
814 
815  /* set the duration */
816  out_pkt.duration = 0;
817  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
818  if (st->codec->sample_rate > 0) {
819  out_pkt.duration =
821  (AVRational) { 1, st->codec->sample_rate },
822  st->time_base,
823  AV_ROUND_DOWN);
824  }
825  }
826 
827  out_pkt.stream_index = st->index;
828  out_pkt.pts = st->parser->pts;
829  out_pkt.dts = st->parser->dts;
830  out_pkt.pos = st->parser->pos;
831 
832  if (st->parser->key_frame == 1 ||
833  (st->parser->key_frame == -1 &&
835  out_pkt.flags |= AV_PKT_FLAG_KEY;
836 
837  compute_pkt_fields(s, st, st->parser, &out_pkt);
838 
839  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
840  out_pkt.flags & AV_PKT_FLAG_KEY) {
841  ff_reduce_index(s, st->index);
842  av_add_index_entry(st, st->parser->frame_offset, out_pkt.dts,
843  0, 0, AVINDEX_KEYFRAME);
844  }
845 
846  if (out_pkt.data == pkt->data && out_pkt.size == pkt->size) {
847  out_pkt.buf = pkt->buf;
848  pkt->buf = NULL;
849 #if FF_API_DESTRUCT_PACKET
851  out_pkt.destruct = pkt->destruct;
852  pkt->destruct = NULL;
854 #endif
855  }
856  if ((ret = av_dup_packet(&out_pkt)) < 0)
857  goto fail;
858 
859  if (!add_to_pktbuf(&s->parse_queue, &out_pkt, &s->parse_queue_end)) {
860  av_free_packet(&out_pkt);
861  ret = AVERROR(ENOMEM);
862  goto fail;
863  }
864  }
865 
866  /* end of the stream => close and free the parser */
867  if (pkt == &flush_pkt) {
868  av_parser_close(st->parser);
869  st->parser = NULL;
870  }
871 
872 fail:
873  av_free_packet(pkt);
874  return ret;
875 }
876 
877 static int read_from_packet_buffer(AVPacketList **pkt_buffer,
878  AVPacketList **pkt_buffer_end,
879  AVPacket *pkt)
880 {
881  AVPacketList *pktl;
882  av_assert0(*pkt_buffer);
883  pktl = *pkt_buffer;
884  *pkt = pktl->pkt;
885  *pkt_buffer = pktl->next;
886  if (!pktl->next)
887  *pkt_buffer_end = NULL;
888  av_freep(&pktl);
889  return 0;
890 }
891 
893 {
894  int ret = 0, i, got_packet = 0;
895  AVDictionary *metadata = NULL;
896 
897  av_init_packet(pkt);
898 
899  while (!got_packet && !s->parse_queue) {
900  AVStream *st;
901  AVPacket cur_pkt;
902 
903  /* read next packet */
904  ret = ff_read_packet(s, &cur_pkt);
905  if (ret < 0) {
906  if (ret == AVERROR(EAGAIN))
907  return ret;
908  /* flush the parsers */
909  for (i = 0; i < s->nb_streams; i++) {
910  st = s->streams[i];
911  if (st->parser && st->need_parsing)
912  parse_packet(s, NULL, st->index);
913  }
914  /* all remaining packets are now in parse_queue =>
915  * really terminate parsing */
916  break;
917  }
918  ret = 0;
919  st = s->streams[cur_pkt.stream_index];
920 
921  if (cur_pkt.pts != AV_NOPTS_VALUE &&
922  cur_pkt.dts != AV_NOPTS_VALUE &&
923  cur_pkt.pts < cur_pkt.dts) {
925  "Invalid timestamps stream=%d, pts=%"PRId64", "
926  "dts=%"PRId64", size=%d\n",
927  cur_pkt.stream_index, cur_pkt.pts,
928  cur_pkt.dts, cur_pkt.size);
929  }
930  if (s->debug & FF_FDEBUG_TS)
931  av_log(s, AV_LOG_DEBUG,
932  "ff_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", "
933  "size=%d, duration=%d, flags=%d\n",
934  cur_pkt.stream_index, cur_pkt.pts, cur_pkt.dts,
935  cur_pkt.size, cur_pkt.duration, cur_pkt.flags);
936 
937  if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
938  st->parser = av_parser_init(st->codec->codec_id);
939  if (!st->parser)
940  /* no parser available: just output the raw packets */
942  else if (st->need_parsing == AVSTREAM_PARSE_HEADERS)
944  else if (st->need_parsing == AVSTREAM_PARSE_FULL_ONCE)
945  st->parser->flags |= PARSER_FLAG_ONCE;
946  }
947 
948  if (!st->need_parsing || !st->parser) {
949  /* no parsing needed: we just output the packet as is */
950  *pkt = cur_pkt;
951  compute_pkt_fields(s, st, NULL, pkt);
952  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
953  (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
954  ff_reduce_index(s, st->index);
955  av_add_index_entry(st, pkt->pos, pkt->dts,
956  0, 0, AVINDEX_KEYFRAME);
957  }
958  got_packet = 1;
959  } else if (st->discard < AVDISCARD_ALL) {
960  if ((ret = parse_packet(s, &cur_pkt, cur_pkt.stream_index)) < 0)
961  return ret;
962  } else {
963  /* free packet */
964  av_free_packet(&cur_pkt);
965  }
966  }
967 
968  if (!got_packet && s->parse_queue)
970 
971  av_opt_get_dict_val(s, "metadata", AV_OPT_SEARCH_CHILDREN, &metadata);
972  if (metadata) {
974  av_dict_copy(&s->metadata, metadata, 0);
975  av_dict_free(&metadata);
977  }
978 
979  if (s->debug & FF_FDEBUG_TS)
980  av_log(s, AV_LOG_DEBUG,
981  "read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", "
982  "size=%d, duration=%d, flags=%d\n",
983  pkt->stream_index, pkt->pts, pkt->dts,
984  pkt->size, pkt->duration, pkt->flags);
985 
986  return ret;
987 }
988 
990 {
991  const int genpts = s->flags & AVFMT_FLAG_GENPTS;
992  int eof = 0;
993 
994  if (!genpts)
995  return s->packet_buffer
997  &s->packet_buffer_end, pkt)
998  : read_frame_internal(s, pkt);
999 
1000  for (;;) {
1001  int ret;
1002  AVPacketList *pktl = s->packet_buffer;
1003 
1004  if (pktl) {
1005  AVPacket *next_pkt = &pktl->pkt;
1006 
1007  if (next_pkt->dts != AV_NOPTS_VALUE) {
1008  int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1009  while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1010  if (pktl->pkt.stream_index == next_pkt->stream_index &&
1011  (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0) &&
1012  av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) {
1013  // not B-frame
1014  next_pkt->pts = pktl->pkt.dts;
1015  }
1016  pktl = pktl->next;
1017  }
1018  pktl = s->packet_buffer;
1019  }
1020 
1021  /* read packet from packet buffer, if there is data */
1022  if (!(next_pkt->pts == AV_NOPTS_VALUE &&
1023  next_pkt->dts != AV_NOPTS_VALUE && !eof))
1025  &s->packet_buffer_end, pkt);
1026  }
1027 
1028  ret = read_frame_internal(s, pkt);
1029  if (ret < 0) {
1030  if (pktl && ret != AVERROR(EAGAIN)) {
1031  eof = 1;
1032  continue;
1033  } else
1034  return ret;
1035  }
1036 
1038  &s->packet_buffer_end)) < 0)
1039  return AVERROR(ENOMEM);
1040  }
1041 }
1042 
1043 /* XXX: suppress the packet queue */
1045 {
1049 
1051 }
1052 
1053 /*******************************************************/
1054 /* seek support */
1055 
1057 {
1058  int first_audio_index = -1;
1059  int i;
1060  AVStream *st;
1061 
1062  if (s->nb_streams <= 0)
1063  return -1;
1064  for (i = 0; i < s->nb_streams; i++) {
1065  st = s->streams[i];
1066  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
1068  return i;
1069  }
1070  if (first_audio_index < 0 &&
1072  first_audio_index = i;
1073  }
1074  return first_audio_index >= 0 ? first_audio_index : 0;
1075 }
1076 
1079 {
1080  AVStream *st;
1081  int i, j;
1082 
1083  flush_packet_queue(s);
1084 
1085  /* Reset read state for each stream. */
1086  for (i = 0; i < s->nb_streams; i++) {
1087  st = s->streams[i];
1088 
1089  if (st->parser) {
1090  av_parser_close(st->parser);
1091  st->parser = NULL;
1092  }
1094  /* We set the current DTS to an unspecified origin. */
1095  st->cur_dts = AV_NOPTS_VALUE;
1096 
1098 
1099  for (j = 0; j < MAX_REORDER_DELAY + 1; j++)
1100  st->pts_buffer[j] = AV_NOPTS_VALUE;
1101  }
1102 }
1103 
1104 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1105 {
1106  int i;
1107 
1108  for (i = 0; i < s->nb_streams; i++) {
1109  AVStream *st = s->streams[i];
1110 
1111  st->cur_dts =
1112  av_rescale(timestamp,
1113  st->time_base.den * (int64_t) ref_st->time_base.num,
1114  st->time_base.num * (int64_t) ref_st->time_base.den);
1115  }
1116 }
1117 
1118 void ff_reduce_index(AVFormatContext *s, int stream_index)
1119 {
1120  AVStream *st = s->streams[stream_index];
1121  unsigned int max_entries = s->max_index_size / sizeof(AVIndexEntry);
1122 
1123  if ((unsigned) st->nb_index_entries >= max_entries) {
1124  int i;
1125  for (i = 0; 2 * i < st->nb_index_entries; i++)
1126  st->index_entries[i] = st->index_entries[2 * i];
1127  st->nb_index_entries = i;
1128  }
1129 }
1130 
1131 int ff_add_index_entry(AVIndexEntry **index_entries,
1132  int *nb_index_entries,
1133  unsigned int *index_entries_allocated_size,
1134  int64_t pos, int64_t timestamp,
1135  int size, int distance, int flags)
1136 {
1137  AVIndexEntry *entries, *ie;
1138  int index;
1139 
1140  if ((unsigned) *nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1141  return -1;
1142 
1143  entries = av_fast_realloc(*index_entries,
1144  index_entries_allocated_size,
1145  (*nb_index_entries + 1) *
1146  sizeof(AVIndexEntry));
1147  if (!entries)
1148  return -1;
1149 
1150  *index_entries = entries;
1151 
1152  index = ff_index_search_timestamp(*index_entries, *nb_index_entries,
1153  timestamp, AVSEEK_FLAG_ANY);
1154 
1155  if (index < 0) {
1156  index = (*nb_index_entries)++;
1157  ie = &entries[index];
1158  assert(index == 0 || ie[-1].timestamp < timestamp);
1159  } else {
1160  ie = &entries[index];
1161  if (ie->timestamp != timestamp) {
1162  if (ie->timestamp <= timestamp)
1163  return -1;
1164  memmove(entries + index + 1, entries + index,
1165  sizeof(AVIndexEntry) * (*nb_index_entries - index));
1166  (*nb_index_entries)++;
1167  } else if (ie->pos == pos && distance < ie->min_distance)
1168  // do not reduce the distance
1169  distance = ie->min_distance;
1170  }
1171 
1172  ie->pos = pos;
1173  ie->timestamp = timestamp;
1174  ie->min_distance = distance;
1175  ie->size = size;
1176  ie->flags = flags;
1177 
1178  return index;
1179 }
1180 
1181 int av_add_index_entry(AVStream *st, int64_t pos, int64_t timestamp,
1182  int size, int distance, int flags)
1183 {
1185  &st->index_entries_allocated_size, pos,
1186  timestamp, size, distance, flags);
1187 }
1188 
1189 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1190  int64_t wanted_timestamp, int flags)
1191 {
1192  int a, b, m;
1193  int64_t timestamp;
1194 
1195  a = -1;
1196  b = nb_entries;
1197 
1198  // Optimize appending index entries at the end.
1199  if (b && entries[b - 1].timestamp < wanted_timestamp)
1200  a = b - 1;
1201 
1202  while (b - a > 1) {
1203  m = (a + b) >> 1;
1204  timestamp = entries[m].timestamp;
1205  if (timestamp >= wanted_timestamp)
1206  b = m;
1207  if (timestamp <= wanted_timestamp)
1208  a = m;
1209  }
1210  m = (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
1211 
1212  if (!(flags & AVSEEK_FLAG_ANY))
1213  while (m >= 0 && m < nb_entries &&
1214  !(entries[m].flags & AVINDEX_KEYFRAME))
1215  m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1216 
1217  if (m == nb_entries)
1218  return -1;
1219  return m;
1220 }
1221 
1222 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
1223 {
1225  wanted_timestamp, flags);
1226 }
1227 
1228 int ff_seek_frame_binary(AVFormatContext *s, int stream_index,
1229  int64_t target_ts, int flags)
1230 {
1231  AVInputFormat *avif = s->iformat;
1232  int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
1233  int64_t ts_min, ts_max, ts;
1234  int index;
1235  int64_t ret;
1236  AVStream *st;
1237 
1238  if (stream_index < 0)
1239  return -1;
1240 
1241  av_dlog(s, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
1242 
1243  ts_max =
1244  ts_min = AV_NOPTS_VALUE;
1245  pos_limit = -1; // GCC falsely says it may be uninitialized.
1246 
1247  st = s->streams[stream_index];
1248  if (st->index_entries) {
1249  AVIndexEntry *e;
1250 
1251  /* FIXME: Whole function must be checked for non-keyframe entries in
1252  * index case, especially read_timestamp(). */
1253  index = av_index_search_timestamp(st, target_ts,
1254  flags | AVSEEK_FLAG_BACKWARD);
1255  index = FFMAX(index, 0);
1256  e = &st->index_entries[index];
1257 
1258  if (e->timestamp <= target_ts || e->pos == e->min_distance) {
1259  pos_min = e->pos;
1260  ts_min = e->timestamp;
1261  av_dlog(s, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
1262  pos_min, ts_min);
1263  } else {
1264  assert(index == 0);
1265  }
1266 
1267  index = av_index_search_timestamp(st, target_ts,
1268  flags & ~AVSEEK_FLAG_BACKWARD);
1269  assert(index < st->nb_index_entries);
1270  if (index >= 0) {
1271  e = &st->index_entries[index];
1272  assert(e->timestamp >= target_ts);
1273  pos_max = e->pos;
1274  ts_max = e->timestamp;
1275  pos_limit = pos_max - e->min_distance;
1276  av_dlog(s, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64
1277  " dts_max=%"PRId64"\n", pos_max, pos_limit, ts_max);
1278  }
1279  }
1280 
1281  pos = ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit,
1282  ts_min, ts_max, flags, &ts, avif->read_timestamp);
1283  if (pos < 0)
1284  return -1;
1285 
1286  /* do the seek */
1287  if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
1288  return ret;
1289 
1290  ff_update_cur_dts(s, st, ts);
1291 
1292  return 0;
1293 }
1294 
1295 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1296  int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1297  int64_t ts_min, int64_t ts_max,
1298  int flags, int64_t *ts_ret,
1299  int64_t (*read_timestamp)(struct AVFormatContext *, int,
1300  int64_t *, int64_t))
1301 {
1302  int64_t pos, ts;
1303  int64_t start_pos, filesize;
1304  int no_change;
1305 
1306  av_dlog(s, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
1307 
1308  if (ts_min == AV_NOPTS_VALUE) {
1309  pos_min = s->data_offset;
1310  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1311  if (ts_min == AV_NOPTS_VALUE)
1312  return -1;
1313  }
1314 
1315  if (ts_max == AV_NOPTS_VALUE) {
1316  int step = 1024;
1317  filesize = avio_size(s->pb);
1318  pos_max = filesize - 1;
1319  do {
1320  pos_max -= step;
1321  ts_max = read_timestamp(s, stream_index, &pos_max,
1322  pos_max + step);
1323  step += step;
1324  } while (ts_max == AV_NOPTS_VALUE && pos_max >= step);
1325  if (ts_max == AV_NOPTS_VALUE)
1326  return -1;
1327 
1328  for (;;) {
1329  int64_t tmp_pos = pos_max + 1;
1330  int64_t tmp_ts = read_timestamp(s, stream_index,
1331  &tmp_pos, INT64_MAX);
1332  if (tmp_ts == AV_NOPTS_VALUE)
1333  break;
1334  ts_max = tmp_ts;
1335  pos_max = tmp_pos;
1336  if (tmp_pos >= filesize)
1337  break;
1338  }
1339  pos_limit = pos_max;
1340  }
1341 
1342  if (ts_min > ts_max)
1343  return -1;
1344  else if (ts_min == ts_max)
1345  pos_limit = pos_min;
1346 
1347  no_change = 0;
1348  while (pos_min < pos_limit) {
1349  av_dlog(s, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64
1350  " dts_max=%"PRId64"\n", pos_min, pos_max, ts_min, ts_max);
1351  assert(pos_limit <= pos_max);
1352 
1353  if (no_change == 0) {
1354  int64_t approximate_keyframe_distance = pos_max - pos_limit;
1355  // interpolate position (better than dichotomy)
1356  pos = av_rescale(target_ts - ts_min, pos_max - pos_min,
1357  ts_max - ts_min) +
1358  pos_min - approximate_keyframe_distance;
1359  } else if (no_change == 1) {
1360  // bisection if interpolation did not change min / max pos last time
1361  pos = (pos_min + pos_limit) >> 1;
1362  } else {
1363  /* linear search if bisection failed, can only happen if there
1364  * are very few or no keyframes between min/max */
1365  pos = pos_min;
1366  }
1367  if (pos <= pos_min)
1368  pos = pos_min + 1;
1369  else if (pos > pos_limit)
1370  pos = pos_limit;
1371  start_pos = pos;
1372 
1373  // May pass pos_limit instead of -1.
1374  ts = read_timestamp(s, stream_index, &pos, INT64_MAX);
1375  if (pos == pos_max)
1376  no_change++;
1377  else
1378  no_change = 0;
1379  av_dlog(s, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64
1380  " target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
1381  pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts,
1382  pos_limit, start_pos, no_change);
1383  if (ts == AV_NOPTS_VALUE) {
1384  av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
1385  return -1;
1386  }
1387  assert(ts != AV_NOPTS_VALUE);
1388  if (target_ts <= ts) {
1389  pos_limit = start_pos - 1;
1390  pos_max = pos;
1391  ts_max = ts;
1392  }
1393  if (target_ts >= ts) {
1394  pos_min = pos;
1395  ts_min = ts;
1396  }
1397  }
1398 
1399  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
1400  ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
1401  pos_min = pos;
1402  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1403  pos_min++;
1404  ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1405  av_dlog(s, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
1406  pos, ts_min, target_ts, ts_max);
1407  *ts_ret = ts;
1408  return pos;
1409 }
1410 
1411 static int seek_frame_byte(AVFormatContext *s, int stream_index,
1412  int64_t pos, int flags)
1413 {
1414  int64_t pos_min, pos_max;
1415 
1416  pos_min = s->data_offset;
1417  pos_max = avio_size(s->pb) - 1;
1418 
1419  if (pos < pos_min)
1420  pos = pos_min;
1421  else if (pos > pos_max)
1422  pos = pos_max;
1423 
1424  avio_seek(s->pb, pos, SEEK_SET);
1425 
1426  return 0;
1427 }
1428 
1429 static int seek_frame_generic(AVFormatContext *s, int stream_index,
1430  int64_t timestamp, int flags)
1431 {
1432  int index;
1433  int64_t ret;
1434  AVStream *st;
1435  AVIndexEntry *ie;
1436 
1437  st = s->streams[stream_index];
1438 
1439  index = av_index_search_timestamp(st, timestamp, flags);
1440 
1441  if (index < 0 && st->nb_index_entries &&
1442  timestamp < st->index_entries[0].timestamp)
1443  return -1;
1444 
1445  if (index < 0 || index == st->nb_index_entries - 1) {
1446  AVPacket pkt;
1447 
1448  if (st->nb_index_entries) {
1449  assert(st->index_entries);
1450  ie = &st->index_entries[st->nb_index_entries - 1];
1451  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1452  return ret;
1453  ff_update_cur_dts(s, st, ie->timestamp);
1454  } else {
1455  if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
1456  return ret;
1457  }
1458  for (;;) {
1459  int read_status;
1460  do {
1461  read_status = av_read_frame(s, &pkt);
1462  } while (read_status == AVERROR(EAGAIN));
1463  if (read_status < 0)
1464  break;
1465  av_free_packet(&pkt);
1466  if (stream_index == pkt.stream_index)
1467  if ((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
1468  break;
1469  }
1470  index = av_index_search_timestamp(st, timestamp, flags);
1471  }
1472  if (index < 0)
1473  return -1;
1474 
1476  if (s->iformat->read_seek)
1477  if (s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
1478  return 0;
1479  ie = &st->index_entries[index];
1480  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1481  return ret;
1482  ff_update_cur_dts(s, st, ie->timestamp);
1483 
1484  return 0;
1485 }
1486 
1487 static int seek_frame_internal(AVFormatContext *s, int stream_index,
1488  int64_t timestamp, int flags)
1489 {
1490  int ret;
1491  AVStream *st;
1492 
1493  if (flags & AVSEEK_FLAG_BYTE) {
1494  if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
1495  return -1;
1497  return seek_frame_byte(s, stream_index, timestamp, flags);
1498  }
1499 
1500  if (stream_index < 0) {
1501  stream_index = av_find_default_stream_index(s);
1502  if (stream_index < 0)
1503  return -1;
1504 
1505  st = s->streams[stream_index];
1506  /* timestamp for default must be expressed in AV_TIME_BASE units */
1507  timestamp = av_rescale(timestamp, st->time_base.den,
1508  AV_TIME_BASE * (int64_t) st->time_base.num);
1509  }
1510 
1511  /* first, we try the format specific seek */
1512  if (s->iformat->read_seek) {
1514  ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
1515  } else
1516  ret = -1;
1517  if (ret >= 0)
1518  return 0;
1519 
1520  if (s->iformat->read_timestamp &&
1521  !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
1523  return ff_seek_frame_binary(s, stream_index, timestamp, flags);
1524  } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
1526  return seek_frame_generic(s, stream_index, timestamp, flags);
1527  } else
1528  return -1;
1529 }
1530 
1531 int av_seek_frame(AVFormatContext *s, int stream_index,
1532  int64_t timestamp, int flags)
1533 {
1534  int ret = seek_frame_internal(s, stream_index, timestamp, flags);
1535 
1536  if (ret >= 0)
1537  ret = queue_attached_pictures(s);
1538 
1539  return ret;
1540 }
1541 
1542 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts,
1543  int64_t ts, int64_t max_ts, int flags)
1544 {
1545  if (min_ts > ts || max_ts < ts)
1546  return -1;
1547 
1548  if (s->iformat->read_seek2) {
1549  int ret;
1551  ret = s->iformat->read_seek2(s, stream_index, min_ts,
1552  ts, max_ts, flags);
1553 
1554  if (ret >= 0)
1555  ret = queue_attached_pictures(s);
1556  return ret;
1557  }
1558 
1559  if (s->iformat->read_timestamp) {
1560  // try to seek via read_timestamp()
1561  }
1562 
1563  // Fall back on old API if new is not implemented but old is.
1564  // Note the old API has somewhat different semantics.
1565  if (s->iformat->read_seek || 1)
1566  return av_seek_frame(s, stream_index, ts,
1567  flags | ((uint64_t) ts - min_ts >
1568  (uint64_t) max_ts - ts
1569  ? AVSEEK_FLAG_BACKWARD : 0));
1570 
1571  // try some generic seek like seek_frame_generic() but with new ts semantics
1572 }
1573 
1574 /*******************************************************/
1575 
1582 {
1583  int i;
1584  AVStream *st;
1585 
1586  for (i = 0; i < ic->nb_streams; i++) {
1587  st = ic->streams[i];
1588  if (st->duration != AV_NOPTS_VALUE)
1589  return 1;
1590  }
1591  if (ic->duration != AV_NOPTS_VALUE)
1592  return 1;
1593  return 0;
1594 }
1595 
1602 {
1603  int64_t start_time, start_time1, end_time, end_time1;
1604  int64_t duration, duration1, filesize;
1605  int i;
1606  AVStream *st;
1607 
1608  start_time = INT64_MAX;
1609  end_time = INT64_MIN;
1610  duration = INT64_MIN;
1611  for (i = 0; i < ic->nb_streams; i++) {
1612  st = ic->streams[i];
1613  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1614  start_time1 = av_rescale_q(st->start_time, st->time_base,
1615  AV_TIME_BASE_Q);
1616  start_time = FFMIN(start_time, start_time1);
1617  if (st->duration != AV_NOPTS_VALUE) {
1618  end_time1 = start_time1 +
1619  av_rescale_q(st->duration, st->time_base,
1620  AV_TIME_BASE_Q);
1621  end_time = FFMAX(end_time, end_time1);
1622  }
1623  }
1624  if (st->duration != AV_NOPTS_VALUE) {
1625  duration1 = av_rescale_q(st->duration, st->time_base,
1626  AV_TIME_BASE_Q);
1627  duration = FFMAX(duration, duration1);
1628  }
1629  }
1630  if (start_time != INT64_MAX) {
1631  ic->start_time = start_time;
1632  if (end_time != INT64_MIN)
1633  duration = FFMAX(duration, end_time - start_time);
1634  }
1635  if (duration != INT64_MIN) {
1636  ic->duration = duration;
1637  if (ic->pb && (filesize = avio_size(ic->pb)) > 0)
1638  /* compute the bitrate */
1639  ic->bit_rate = (double) filesize * 8.0 * AV_TIME_BASE /
1640  (double) ic->duration;
1641  }
1642 }
1643 
1645 {
1646  int i;
1647  AVStream *st;
1648 
1650  for (i = 0; i < ic->nb_streams; i++) {
1651  st = ic->streams[i];
1652  if (st->start_time == AV_NOPTS_VALUE) {
1653  if (ic->start_time != AV_NOPTS_VALUE)
1655  st->time_base);
1656  if (ic->duration != AV_NOPTS_VALUE)
1658  st->time_base);
1659  }
1660  }
1661 }
1662 
1664 {
1665  int64_t filesize, duration;
1666  int i;
1667  AVStream *st;
1668 
1669  /* if bit_rate is already set, we believe it */
1670  if (ic->bit_rate <= 0) {
1671  int bit_rate = 0;
1672  for (i = 0; i < ic->nb_streams; i++) {
1673  st = ic->streams[i];
1674  if (st->codec->bit_rate > 0) {
1675  if (INT_MAX - st->codec->bit_rate < bit_rate) {
1676  bit_rate = 0;
1677  break;
1678  }
1679  bit_rate += st->codec->bit_rate;
1680  }
1681  }
1682  ic->bit_rate = bit_rate;
1683  }
1684 
1685  /* if duration is already set, we believe it */
1686  if (ic->duration == AV_NOPTS_VALUE &&
1687  ic->bit_rate != 0) {
1688  filesize = ic->pb ? avio_size(ic->pb) : 0;
1689  if (filesize > 0) {
1690  for (i = 0; i < ic->nb_streams; i++) {
1691  st = ic->streams[i];
1692  duration = av_rescale(8 * filesize, st->time_base.den,
1693  ic->bit_rate *
1694  (int64_t) st->time_base.num);
1695  if (st->duration == AV_NOPTS_VALUE)
1696  st->duration = duration;
1697  }
1698  }
1699  }
1700 }
1701 
1702 #define DURATION_MAX_READ_SIZE 250000
1703 #define DURATION_MAX_RETRY 3
1704 
1705 /* only usable for MPEG-PS streams */
1706 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1707 {
1708  AVPacket pkt1, *pkt = &pkt1;
1709  AVStream *st;
1710  int read_size, i, ret;
1711  int64_t end_time;
1712  int64_t filesize, offset, duration;
1713  int retry = 0;
1714 
1715  /* flush packet queue */
1716  flush_packet_queue(ic);
1717 
1718  for (i = 0; i < ic->nb_streams; i++) {
1719  st = ic->streams[i];
1720  if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
1722  "start time is not set in estimate_timings_from_pts\n");
1723 
1724  if (st->parser) {
1725  av_parser_close(st->parser);
1726  st->parser = NULL;
1727  }
1728  }
1729 
1730  /* estimate the end time (duration) */
1731  /* XXX: may need to support wrapping */
1732  filesize = ic->pb ? avio_size(ic->pb) : 0;
1733  end_time = AV_NOPTS_VALUE;
1734  do {
1735  offset = filesize - (DURATION_MAX_READ_SIZE << retry);
1736  if (offset < 0)
1737  offset = 0;
1738 
1739  avio_seek(ic->pb, offset, SEEK_SET);
1740  read_size = 0;
1741  for (;;) {
1742  if (read_size >= DURATION_MAX_READ_SIZE << (FFMAX(retry - 1, 0)))
1743  break;
1744 
1745  do {
1746  ret = ff_read_packet(ic, pkt);
1747  } while (ret == AVERROR(EAGAIN));
1748  if (ret != 0)
1749  break;
1750  read_size += pkt->size;
1751  st = ic->streams[pkt->stream_index];
1752  if (pkt->pts != AV_NOPTS_VALUE &&
1753  (st->start_time != AV_NOPTS_VALUE ||
1754  st->first_dts != AV_NOPTS_VALUE)) {
1755  duration = end_time = pkt->pts;
1756  if (st->start_time != AV_NOPTS_VALUE)
1757  duration -= st->start_time;
1758  else
1759  duration -= st->first_dts;
1760  if (duration < 0)
1761  duration += 1LL << st->pts_wrap_bits;
1762  if (duration > 0) {
1763  if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
1764  st->duration = duration;
1765  }
1766  }
1767  av_free_packet(pkt);
1768  }
1769  } while (end_time == AV_NOPTS_VALUE &&
1770  filesize > (DURATION_MAX_READ_SIZE << retry) &&
1771  ++retry <= DURATION_MAX_RETRY);
1772 
1774 
1775  avio_seek(ic->pb, old_offset, SEEK_SET);
1776  for (i = 0; i < ic->nb_streams; i++) {
1777  st = ic->streams[i];
1778  st->cur_dts = st->first_dts;
1780  }
1781 }
1782 
1783 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1784 {
1785  int64_t file_size;
1786 
1787  /* get the file size, if possible */
1788  if (ic->iformat->flags & AVFMT_NOFILE) {
1789  file_size = 0;
1790  } else {
1791  file_size = avio_size(ic->pb);
1792  file_size = FFMAX(0, file_size);
1793  }
1794 
1795  if ((!strcmp(ic->iformat->name, "mpeg") ||
1796  !strcmp(ic->iformat->name, "mpegts")) &&
1797  file_size && ic->pb->seekable) {
1798  /* get accurate estimate from the PTSes */
1799  estimate_timings_from_pts(ic, old_offset);
1800  } else if (has_duration(ic)) {
1801  /* at least one component has timings - we use them for all
1802  * the components */
1804  } else {
1805  av_log(ic, AV_LOG_WARNING,
1806  "Estimating duration from bitrate, this may be inaccurate\n");
1807  /* less precise: use bitrate info */
1809  }
1811 
1812  {
1813  int i;
1814  AVStream av_unused *st;
1815  for (i = 0; i < ic->nb_streams; i++) {
1816  st = ic->streams[i];
1817  av_dlog(ic, "%d: start_time: %0.3f duration: %0.3f\n", i,
1818  (double) st->start_time / AV_TIME_BASE,
1819  (double) st->duration / AV_TIME_BASE);
1820  }
1821  av_dlog(ic,
1822  "stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
1823  (double) ic->start_time / AV_TIME_BASE,
1824  (double) ic->duration / AV_TIME_BASE,
1825  ic->bit_rate / 1000);
1826  }
1827 }
1828 
1830 {
1831  AVCodecContext *avctx = st->codec;
1832  int val;
1833 
1834  switch (avctx->codec_type) {
1835  case AVMEDIA_TYPE_AUDIO:
1836  val = avctx->sample_rate && avctx->channels;
1837  if (st->info->found_decoder >= 0 &&
1838  avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
1839  return 0;
1840  break;
1841  case AVMEDIA_TYPE_VIDEO:
1842  val = avctx->width;
1843  if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
1844  return 0;
1845  break;
1846  default:
1847  val = 1;
1848  break;
1849  }
1850  return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
1851 }
1852 
1854 {
1855  return st->codec->codec_id != AV_CODEC_ID_H264 ||
1856  st->info->nb_decoded_frames >= 6;
1857 }
1858 
1859 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
1860 static int try_decode_frame(AVStream *st, AVPacket *avpkt,
1862 {
1863  const AVCodec *codec;
1864  int got_picture = 1, ret = 0;
1865  AVFrame *frame = av_frame_alloc();
1866  AVPacket pkt = *avpkt;
1867 
1868  if (!frame)
1869  return AVERROR(ENOMEM);
1870 
1871  if (!avcodec_is_open(st->codec) && !st->info->found_decoder) {
1872  AVDictionary *thread_opt = NULL;
1873 
1874  codec = st->codec->codec ? st->codec->codec
1876 
1877  if (!codec) {
1878  st->info->found_decoder = -1;
1879  ret = -1;
1880  goto fail;
1881  }
1882 
1883  /* Force thread count to 1 since the H.264 decoder will not extract
1884  * SPS and PPS to extradata during multi-threaded decoding. */
1885  av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
1886  ret = avcodec_open2(st->codec, codec, options ? options : &thread_opt);
1887  if (!options)
1888  av_dict_free(&thread_opt);
1889  if (ret < 0) {
1890  st->info->found_decoder = -1;
1891  goto fail;
1892  }
1893  st->info->found_decoder = 1;
1894  } else if (!st->info->found_decoder)
1895  st->info->found_decoder = 1;
1896 
1897  if (st->info->found_decoder < 0) {
1898  ret = -1;
1899  goto fail;
1900  }
1901 
1902  while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
1903  ret >= 0 &&
1905  (!st->codec_info_nb_frames &&
1907  got_picture = 0;
1908  switch (st->codec->codec_type) {
1909  case AVMEDIA_TYPE_VIDEO:
1910  ret = avcodec_decode_video2(st->codec, frame,
1911  &got_picture, &pkt);
1912  break;
1913  case AVMEDIA_TYPE_AUDIO:
1914  ret = avcodec_decode_audio4(st->codec, frame, &got_picture, &pkt);
1915  break;
1916  default:
1917  break;
1918  }
1919  if (ret >= 0) {
1920  if (got_picture)
1921  st->info->nb_decoded_frames++;
1922  pkt.data += ret;
1923  pkt.size -= ret;
1924  ret = got_picture;
1925  }
1926  }
1927 
1928 fail:
1929  av_frame_free(&frame);
1930  return ret;
1931 }
1932 
1933 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
1934 {
1935  while (tags->id != AV_CODEC_ID_NONE) {
1936  if (tags->id == id)
1937  return tags->tag;
1938  tags++;
1939  }
1940  return 0;
1941 }
1942 
1943 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
1944 {
1945  int i;
1946  for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
1947  if (tag == tags[i].tag)
1948  return tags[i].id;
1949  for (i = 0; tags[i].id != AV_CODEC_ID_NONE; i++)
1950  if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
1951  return tags[i].id;
1952  return AV_CODEC_ID_NONE;
1953 }
1954 
1955 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
1956 {
1957  if (flt) {
1958  switch (bps) {
1959  case 32:
1961  case 64:
1963  default:
1964  return AV_CODEC_ID_NONE;
1965  }
1966  } else {
1967  bps >>= 3;
1968  if (sflags & (1 << (bps - 1))) {
1969  switch (bps) {
1970  case 1:
1971  return AV_CODEC_ID_PCM_S8;
1972  case 2:
1974  case 3:
1976  case 4:
1978  default:
1979  return AV_CODEC_ID_NONE;
1980  }
1981  } else {
1982  switch (bps) {
1983  case 1:
1984  return AV_CODEC_ID_PCM_U8;
1985  case 2:
1987  case 3:
1989  case 4:
1991  default:
1992  return AV_CODEC_ID_NONE;
1993  }
1994  }
1995  }
1996 }
1997 
1998 unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
1999 {
2000  int i;
2001  for (i = 0; tags && tags[i]; i++) {
2002  int tag = ff_codec_get_tag(tags[i], id);
2003  if (tag)
2004  return tag;
2005  }
2006  return 0;
2007 }
2008 
2009 enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
2010 {
2011  int i;
2012  for (i = 0; tags && tags[i]; i++) {
2013  enum AVCodecID id = ff_codec_get_id(tags[i], tag);
2014  if (id != AV_CODEC_ID_NONE)
2015  return id;
2016  }
2017  return AV_CODEC_ID_NONE;
2018 }
2019 
2021 {
2022  unsigned int i, j;
2023  int64_t max_time = s->duration +
2024  ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2025 
2026  for (i = 0; i < s->nb_chapters; i++)
2027  if (s->chapters[i]->end == AV_NOPTS_VALUE) {
2028  AVChapter *ch = s->chapters[i];
2029  int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q,
2030  ch->time_base)
2031  : INT64_MAX;
2032 
2033  for (j = 0; j < s->nb_chapters; j++) {
2034  AVChapter *ch1 = s->chapters[j];
2035  int64_t next_start = av_rescale_q(ch1->start, ch1->time_base,
2036  ch->time_base);
2037  if (j != i && next_start > ch->start && next_start < end)
2038  end = next_start;
2039  }
2040  ch->end = (end == INT64_MAX) ? ch->start : end;
2041  }
2042 }
2043 
2044 static int get_std_framerate(int i)
2045 {
2046  if (i < 60 * 12)
2047  return (i + 1) * 1001;
2048  else
2049  return ((const int[]) { 24, 30, 60, 12, 15 })[i - 60 * 12] * 1000 * 12;
2050 }
2051 
2053 {
2054  int i, count, ret, read_size, j;
2055  AVStream *st;
2056  AVPacket pkt1, *pkt;
2057  int64_t old_offset = avio_tell(ic->pb);
2058  // new streams might appear, no options for those
2059  int orig_nb_streams = ic->nb_streams;
2060 
2061  for (i = 0; i < ic->nb_streams; i++) {
2062  const AVCodec *codec;
2063  AVDictionary *thread_opt = NULL;
2064  st = ic->streams[i];
2065 
2066  // only for the split stuff
2067  if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
2068  st->parser = av_parser_init(st->codec->codec_id);
2069  if (st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser)
2071  }
2072  codec = st->codec->codec ? st->codec->codec
2074 
2075  /* Force thread count to 1 since the H.264 decoder will not extract
2076  * SPS and PPS to extradata during multi-threaded decoding. */
2077  av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2078 
2079  /* Ensure that subtitle_header is properly set. */
2081  && codec && !st->codec->codec)
2082  avcodec_open2(st->codec, codec,
2083  options ? &options[i] : &thread_opt);
2084 
2085  // Try to just open decoders, in case this is enough to get parameters.
2086  if (!has_codec_parameters(st)) {
2087  if (codec && !st->codec->codec)
2088  avcodec_open2(st->codec, codec,
2089  options ? &options[i] : &thread_opt);
2090  }
2091  if (!options)
2092  av_dict_free(&thread_opt);
2093  }
2094 
2095  for (i = 0; i < ic->nb_streams; i++) {
2098  }
2099 
2100  count = 0;
2101  read_size = 0;
2102  for (;;) {
2104  ret = AVERROR_EXIT;
2105  av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2106  break;
2107  }
2108 
2109  /* check if one codec still needs to be handled */
2110  for (i = 0; i < ic->nb_streams; i++) {
2111  int fps_analyze_framecount = 20;
2112 
2113  st = ic->streams[i];
2114  if (!has_codec_parameters(st))
2115  break;
2116  /* If the timebase is coarse (like the usual millisecond precision
2117  * of mkv), we need to analyze more frames to reliably arrive at
2118  * the correct fps. */
2119  if (av_q2d(st->time_base) > 0.0005)
2120  fps_analyze_framecount *= 2;
2121  if (ic->fps_probe_size >= 0)
2122  fps_analyze_framecount = ic->fps_probe_size;
2123  /* variable fps and no guess at the real fps */
2124  if (!st->avg_frame_rate.num &&
2125  st->codec_info_nb_frames < fps_analyze_framecount &&
2127  break;
2128  if (st->parser && st->parser->parser->split &&
2129  !st->codec->extradata)
2130  break;
2131  if (st->first_dts == AV_NOPTS_VALUE &&
2132  (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2134  break;
2135  }
2136  if (i == ic->nb_streams) {
2137  /* NOTE: If the format has no header, then we need to read some
2138  * packets to get most of the streams, so we cannot stop here. */
2139  if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2140  /* If we found the info for all the codecs, we can stop. */
2141  ret = count;
2142  av_log(ic, AV_LOG_DEBUG, "All info found\n");
2143  break;
2144  }
2145  }
2146  /* We did not get all the codec info, but we read too much data. */
2147  if (read_size >= ic->probesize) {
2148  ret = count;
2149  av_log(ic, AV_LOG_DEBUG,
2150  "Probe buffer size limit %d reached\n", ic->probesize);
2151  break;
2152  }
2153 
2154  /* NOTE: A new stream can be added there if no header in file
2155  * (AVFMTCTX_NOHEADER). */
2156  ret = read_frame_internal(ic, &pkt1);
2157  if (ret == AVERROR(EAGAIN))
2158  continue;
2159 
2160  if (ret < 0) {
2161  /* EOF or error*/
2162  AVPacket empty_pkt = { 0 };
2163  int err = 0;
2164  av_init_packet(&empty_pkt);
2165 
2166  /* We could not have all the codec parameters before EOF. */
2167  ret = -1;
2168  for (i = 0; i < ic->nb_streams; i++) {
2169  st = ic->streams[i];
2170 
2171  /* flush the decoders */
2172  if (st->info->found_decoder == 1) {
2173  do {
2174  err = try_decode_frame(st, &empty_pkt,
2175  (options && i < orig_nb_streams)
2176  ? &options[i] : NULL);
2177  } while (err > 0 && !has_codec_parameters(st));
2178  }
2179 
2180  if (err < 0) {
2181  av_log(ic, AV_LOG_WARNING,
2182  "decoding for stream %d failed\n", st->index);
2183  } else if (!has_codec_parameters(st)) {
2184  char buf[256];
2185  avcodec_string(buf, sizeof(buf), st->codec, 0);
2186  av_log(ic, AV_LOG_WARNING,
2187  "Could not find codec parameters (%s)\n", buf);
2188  } else {
2189  ret = 0;
2190  }
2191  }
2192  break;
2193  }
2194 
2195  if (ic->flags & AVFMT_FLAG_NOBUFFER) {
2196  pkt = &pkt1;
2197  } else {
2198  pkt = add_to_pktbuf(&ic->packet_buffer, &pkt1,
2199  &ic->packet_buffer_end);
2200  if ((ret = av_dup_packet(pkt)) < 0)
2201  goto find_stream_info_err;
2202  }
2203 
2204  read_size += pkt->size;
2205 
2206  st = ic->streams[pkt->stream_index];
2207  if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
2208  /* check for non-increasing dts */
2209  if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2210  st->info->fps_last_dts >= pkt->dts) {
2211  av_log(ic, AV_LOG_WARNING,
2212  "Non-increasing DTS in stream %d: packet %d with DTS "
2213  "%"PRId64", packet %d with DTS %"PRId64"\n",
2214  st->index, st->info->fps_last_dts_idx,
2216  pkt->dts);
2217  st->info->fps_first_dts =
2219  }
2220  /* Check for a discontinuity in dts. If the difference in dts
2221  * is more than 1000 times the average packet duration in the
2222  * sequence, we treat it as a discontinuity. */
2223  if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2225  (pkt->dts - st->info->fps_last_dts) / 1000 >
2226  (st->info->fps_last_dts - st->info->fps_first_dts) /
2227  (st->info->fps_last_dts_idx - st->info->fps_first_dts_idx)) {
2228  av_log(ic, AV_LOG_WARNING,
2229  "DTS discontinuity in stream %d: packet %d with DTS "
2230  "%"PRId64", packet %d with DTS %"PRId64"\n",
2231  st->index, st->info->fps_last_dts_idx,
2233  pkt->dts);
2234  st->info->fps_first_dts =
2236  }
2237 
2238  /* update stored dts values */
2239  if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
2240  st->info->fps_first_dts = pkt->dts;
2242  }
2243  st->info->fps_last_dts = pkt->dts;
2245 
2246  /* check max_analyze_duration */
2247  if (av_rescale_q(pkt->dts - st->info->fps_first_dts, st->time_base,
2249  av_log(ic, AV_LOG_WARNING, "max_analyze_duration %d reached\n",
2250  ic->max_analyze_duration);
2251  break;
2252  }
2253  }
2254  if (st->parser && st->parser->parser->split && !st->codec->extradata) {
2255  int i = st->parser->parser->split(st->codec, pkt->data, pkt->size);
2256  if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
2257  st->codec->extradata_size = i;
2260  if (!st->codec->extradata)
2261  return AVERROR(ENOMEM);
2262  memcpy(st->codec->extradata, pkt->data,
2263  st->codec->extradata_size);
2264  }
2265  }
2266 
2267  /* If still no information, we try to open the codec and to
2268  * decompress the frame. We try to avoid that in most cases as
2269  * it takes longer and uses more memory. For MPEG-4, we need to
2270  * decompress for QuickTime.
2271  *
2272  * If CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2273  * least one frame of codec data, this makes sure the codec initializes
2274  * the channel configuration and does not only trust the values from
2275  * the container. */
2276  try_decode_frame(st, pkt,
2277  (options && i < orig_nb_streams) ? &options[i] : NULL);
2278 
2279  st->codec_info_nb_frames++;
2280  count++;
2281  }
2282 
2283  // close codecs which were opened in try_decode_frame()
2284  for (i = 0; i < ic->nb_streams; i++) {
2285  st = ic->streams[i];
2286  avcodec_close(st->codec);
2287  }
2288  for (i = 0; i < ic->nb_streams; i++) {
2289  st = ic->streams[i];
2290  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2291  /* estimate average framerate if not set by demuxer */
2292  if (!st->avg_frame_rate.num &&
2293  st->info->fps_last_dts != st->info->fps_first_dts) {
2294  int64_t delta_dts = st->info->fps_last_dts -
2295  st->info->fps_first_dts;
2296  int delta_packets = st->info->fps_last_dts_idx -
2297  st->info->fps_first_dts_idx;
2298  int best_fps = 0;
2299  double best_error = 0.01;
2300 
2301  if (delta_dts >= INT64_MAX / st->time_base.num ||
2302  delta_packets >= INT64_MAX / st->time_base.den ||
2303  delta_dts < 0)
2304  continue;
2306  delta_packets * (int64_t) st->time_base.den,
2307  delta_dts * (int64_t) st->time_base.num, 60000);
2308 
2309  /* Round guessed framerate to a "standard" framerate if it's
2310  * within 1% of the original estimate. */
2311  for (j = 0; j < MAX_STD_TIMEBASES; j++) {
2312  AVRational std_fps = { get_std_framerate(j), 12 * 1001 };
2313  double error = fabs(av_q2d(st->avg_frame_rate) /
2314  av_q2d(std_fps) - 1);
2315 
2316  if (error < best_error) {
2317  best_error = error;
2318  best_fps = std_fps.num;
2319  }
2320  }
2321  if (best_fps)
2323  best_fps, 12 * 1001, INT_MAX);
2324  }
2325  } else if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2326  if (!st->codec->bits_per_coded_sample)
2329  // set stream disposition based on audio service type
2330  switch (st->codec->audio_service_type) {
2333  break;
2336  break;
2339  break;
2342  break;
2345  break;
2346  }
2347  }
2348  }
2349 
2350  estimate_timings(ic, old_offset);
2351 
2353 
2354 find_stream_info_err:
2355  for (i = 0; i < ic->nb_streams; i++) {
2356  ic->streams[i]->codec->thread_count = 0;
2357  av_freep(&ic->streams[i]->info);
2358  }
2359  return ret;
2360 }
2361 
2363 {
2364  int i, j;
2365 
2366  for (i = 0; i < ic->nb_programs; i++)
2367  for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2368  if (ic->programs[i]->stream_index[j] == s)
2369  return ic->programs[i];
2370  return NULL;
2371 }
2372 
2374  int wanted_stream_nb, int related_stream,
2375  AVCodec **decoder_ret, int flags)
2376 {
2377  int i, nb_streams = ic->nb_streams;
2378  int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2379  unsigned *program = NULL;
2380  AVCodec *decoder = NULL, *best_decoder = NULL;
2381 
2382  if (related_stream >= 0 && wanted_stream_nb < 0) {
2383  AVProgram *p = find_program_from_stream(ic, related_stream);
2384  if (p) {
2385  program = p->stream_index;
2386  nb_streams = p->nb_stream_indexes;
2387  }
2388  }
2389  for (i = 0; i < nb_streams; i++) {
2390  int real_stream_index = program ? program[i] : i;
2391  AVStream *st = ic->streams[real_stream_index];
2392  AVCodecContext *avctx = st->codec;
2393  if (avctx->codec_type != type)
2394  continue;
2395  if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
2396  continue;
2399  continue;
2400  if (decoder_ret) {
2401  decoder = avcodec_find_decoder(st->codec->codec_id);
2402  if (!decoder) {
2403  if (ret < 0)
2405  continue;
2406  }
2407  }
2408  if (best_count >= st->codec_info_nb_frames)
2409  continue;
2410  best_count = st->codec_info_nb_frames;
2411  ret = real_stream_index;
2412  best_decoder = decoder;
2413  if (program && i == nb_streams - 1 && ret < 0) {
2414  program = NULL;
2415  nb_streams = ic->nb_streams;
2416  /* no related stream found, try again with everything */
2417  i = 0;
2418  }
2419  }
2420  if (decoder_ret)
2421  *decoder_ret = best_decoder;
2422  return ret;
2423 }
2424 
2425 /*******************************************************/
2426 
2428 {
2429  if (s->iformat->read_play)
2430  return s->iformat->read_play(s);
2431  if (s->pb)
2432  return avio_pause(s->pb, 0);
2433  return AVERROR(ENOSYS);
2434 }
2435 
2437 {
2438  if (s->iformat->read_pause)
2439  return s->iformat->read_pause(s);
2440  if (s->pb)
2441  return avio_pause(s->pb, 1);
2442  return AVERROR(ENOSYS);
2443 }
2444 
2446 {
2447  int i, j;
2448  AVStream *st;
2449 
2450  if (!s)
2451  return;
2452 
2453  av_opt_free(s);
2454  if (s->iformat && s->iformat->priv_class && s->priv_data)
2455  av_opt_free(s->priv_data);
2456 
2457  for (i = 0; i < s->nb_streams; i++) {
2458  /* free all data in a stream component */
2459  st = s->streams[i];
2460 
2461  for (j = 0; j < st->nb_side_data; j++)
2462  av_freep(&st->side_data[j].data);
2463  av_freep(&st->side_data);
2464  st->nb_side_data = 0;
2465 
2466  if (st->parser) {
2467  av_parser_close(st->parser);
2468  }
2469  if (st->attached_pic.data)
2471  av_dict_free(&st->metadata);
2472  av_freep(&st->probe_data.buf);
2473  av_free(st->index_entries);
2474  av_free(st->codec->extradata);
2476  av_free(st->codec);
2477  av_free(st->priv_data);
2478  av_free(st->info);
2479  av_free(st);
2480  }
2481  for (i = s->nb_programs - 1; i >= 0; i--) {
2482  av_dict_free(&s->programs[i]->metadata);
2483  av_freep(&s->programs[i]->stream_index);
2484  av_freep(&s->programs[i]);
2485  }
2486  av_freep(&s->programs);
2487  av_freep(&s->priv_data);
2488  while (s->nb_chapters--) {
2490  av_free(s->chapters[s->nb_chapters]);
2491  }
2492  av_freep(&s->chapters);
2493  av_dict_free(&s->metadata);
2494  av_freep(&s->streams);
2495  av_freep(&s->internal);
2496  av_free(s);
2497 }
2498 
2500 {
2501  AVFormatContext *s = *ps;
2502  AVIOContext *pb = s->pb;
2503 
2504  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
2505  (s->flags & AVFMT_FLAG_CUSTOM_IO))
2506  pb = NULL;
2507 
2508  flush_packet_queue(s);
2509 
2510  if (s->iformat)
2511  if (s->iformat->read_close)
2512  s->iformat->read_close(s);
2513 
2515 
2516  *ps = NULL;
2517 
2518  avio_close(pb);
2519 }
2520 
2522 {
2523  AVStream *st;
2524  int i;
2525 
2526  if (av_reallocp_array(&s->streams, s->nb_streams + 1,
2527  sizeof(*s->streams)) < 0) {
2528  s->nb_streams = 0;
2529  return NULL;
2530  }
2531 
2532  st = av_mallocz(sizeof(AVStream));
2533  if (!st)
2534  return NULL;
2535  if (!(st->info = av_mallocz(sizeof(*st->info)))) {
2536  av_free(st);
2537  return NULL;
2538  }
2539 
2540  st->codec = avcodec_alloc_context3(c);
2541  if (!st->codec) {
2542  av_free(st->info);
2543  av_free(st);
2544  return NULL;
2545  }
2546  if (s->iformat) {
2547  /* no default bitrate if decoding */
2548  st->codec->bit_rate = 0;
2549 
2550  /* default pts setting is MPEG-like */
2551  avpriv_set_pts_info(st, 33, 1, 90000);
2552  }
2553 
2554  st->index = s->nb_streams;
2555  st->start_time = AV_NOPTS_VALUE;
2556  st->duration = AV_NOPTS_VALUE;
2557  /* we set the current DTS to 0 so that formats without any timestamps
2558  * but durations get some timestamps, formats with some unknown
2559  * timestamps have their first few packets buffered and the
2560  * timestamps corrected before they are returned to the user */
2561  st->cur_dts = 0;
2562  st->first_dts = AV_NOPTS_VALUE;
2564 
2566  for (i = 0; i < MAX_REORDER_DELAY + 1; i++)
2567  st->pts_buffer[i] = AV_NOPTS_VALUE;
2568 
2569  st->sample_aspect_ratio = (AVRational) { 0, 1 };
2570 
2573 
2574  s->streams[s->nb_streams++] = st;
2575  return st;
2576 }
2577 
2579 {
2580  AVProgram *program = NULL;
2581  int i;
2582 
2583  av_dlog(ac, "new_program: id=0x%04x\n", id);
2584 
2585  for (i = 0; i < ac->nb_programs; i++)
2586  if (ac->programs[i]->id == id)
2587  program = ac->programs[i];
2588 
2589  if (!program) {
2590  program = av_mallocz(sizeof(AVProgram));
2591  if (!program)
2592  return NULL;
2593  dynarray_add(&ac->programs, &ac->nb_programs, program);
2594  program->discard = AVDISCARD_NONE;
2595  }
2596  program->id = id;
2597 
2598  return program;
2599 }
2600 
2602  int64_t start, int64_t end, const char *title)
2603 {
2604  AVChapter *chapter = NULL;
2605  int i;
2606 
2607  for (i = 0; i < s->nb_chapters; i++)
2608  if (s->chapters[i]->id == id)
2609  chapter = s->chapters[i];
2610 
2611  if (!chapter) {
2612  chapter = av_mallocz(sizeof(AVChapter));
2613  if (!chapter)
2614  return NULL;
2615  dynarray_add(&s->chapters, &s->nb_chapters, chapter);
2616  }
2617  av_dict_set(&chapter->metadata, "title", title, 0);
2618  chapter->id = id;
2619  chapter->time_base = time_base;
2620  chapter->start = start;
2621  chapter->end = end;
2622 
2623  return chapter;
2624 }
2625 
2626 void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
2627 {
2628  int i, j;
2629  AVProgram *program = NULL;
2630 
2631  if (idx >= ac->nb_streams) {
2632  av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
2633  return;
2634  }
2635 
2636  for (i = 0; i < ac->nb_programs; i++) {
2637  if (ac->programs[i]->id != progid)
2638  continue;
2639  program = ac->programs[i];
2640  for (j = 0; j < program->nb_stream_indexes; j++)
2641  if (program->stream_index[j] == idx)
2642  return;
2643 
2644  if (av_reallocp_array(&program->stream_index,
2645  program->nb_stream_indexes + 1,
2646  sizeof(*program->stream_index)) < 0) {
2647  program->nb_stream_indexes = 0;
2648  return;
2649  }
2650  program->stream_index[program->nb_stream_indexes++] = idx;
2651  return;
2652  }
2653 }
2654 
2655 uint64_t ff_ntp_time(void)
2656 {
2657  return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
2658 }
2659 
2660 int av_get_frame_filename(char *buf, int buf_size, const char *path, int number)
2661 {
2662  const char *p;
2663  char *q, buf1[20], c;
2664  int nd, len, percentd_found;
2665 
2666  q = buf;
2667  p = path;
2668  percentd_found = 0;
2669  for (;;) {
2670  c = *p++;
2671  if (c == '\0')
2672  break;
2673  if (c == '%') {
2674  do {
2675  nd = 0;
2676  while (av_isdigit(*p))
2677  nd = nd * 10 + *p++ - '0';
2678  c = *p++;
2679  } while (av_isdigit(c));
2680 
2681  switch (c) {
2682  case '%':
2683  goto addchar;
2684  case 'd':
2685  if (percentd_found)
2686  goto fail;
2687  percentd_found = 1;
2688  snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
2689  len = strlen(buf1);
2690  if ((q - buf + len) > buf_size - 1)
2691  goto fail;
2692  memcpy(q, buf1, len);
2693  q += len;
2694  break;
2695  default:
2696  goto fail;
2697  }
2698  } else {
2699 addchar:
2700  if ((q - buf) < buf_size - 1)
2701  *q++ = c;
2702  }
2703  }
2704  if (!percentd_found)
2705  goto fail;
2706  *q = '\0';
2707  return 0;
2708 fail:
2709  *q = '\0';
2710  return -1;
2711 }
2712 
2713 void av_url_split(char *proto, int proto_size,
2714  char *authorization, int authorization_size,
2715  char *hostname, int hostname_size,
2716  int *port_ptr, char *path, int path_size, const char *url)
2717 {
2718  const char *p, *ls, *at, *col, *brk;
2719 
2720  if (port_ptr)
2721  *port_ptr = -1;
2722  if (proto_size > 0)
2723  proto[0] = 0;
2724  if (authorization_size > 0)
2725  authorization[0] = 0;
2726  if (hostname_size > 0)
2727  hostname[0] = 0;
2728  if (path_size > 0)
2729  path[0] = 0;
2730 
2731  /* parse protocol */
2732  if ((p = strchr(url, ':'))) {
2733  av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
2734  p++; /* skip ':' */
2735  if (*p == '/')
2736  p++;
2737  if (*p == '/')
2738  p++;
2739  } else {
2740  /* no protocol means plain filename */
2741  av_strlcpy(path, url, path_size);
2742  return;
2743  }
2744 
2745  /* separate path from hostname */
2746  ls = strchr(p, '/');
2747  if (!ls)
2748  ls = strchr(p, '?');
2749  if (ls)
2750  av_strlcpy(path, ls, path_size);
2751  else
2752  ls = &p[strlen(p)]; // XXX
2753 
2754  /* the rest is hostname, use that to parse auth/port */
2755  if (ls != p) {
2756  /* authorization (user[:pass]@hostname) */
2757  if ((at = strchr(p, '@')) && at < ls) {
2758  av_strlcpy(authorization, p,
2759  FFMIN(authorization_size, at + 1 - p));
2760  p = at + 1; /* skip '@' */
2761  }
2762 
2763  if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
2764  /* [host]:port */
2765  av_strlcpy(hostname, p + 1,
2766  FFMIN(hostname_size, brk - p));
2767  if (brk[1] == ':' && port_ptr)
2768  *port_ptr = atoi(brk + 2);
2769  } else if ((col = strchr(p, ':')) && col < ls) {
2770  av_strlcpy(hostname, p,
2771  FFMIN(col + 1 - p, hostname_size));
2772  if (port_ptr)
2773  *port_ptr = atoi(col + 1);
2774  } else
2775  av_strlcpy(hostname, p,
2776  FFMIN(ls + 1 - p, hostname_size));
2777  }
2778 }
2779 
2780 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
2781 {
2782  int i;
2783  static const char hex_table_uc[16] = { '0', '1', '2', '3',
2784  '4', '5', '6', '7',
2785  '8', '9', 'A', 'B',
2786  'C', 'D', 'E', 'F' };
2787  static const char hex_table_lc[16] = { '0', '1', '2', '3',
2788  '4', '5', '6', '7',
2789  '8', '9', 'a', 'b',
2790  'c', 'd', 'e', 'f' };
2791  const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
2792 
2793  for (i = 0; i < s; i++) {
2794  buff[i * 2] = hex_table[src[i] >> 4];
2795  buff[i * 2 + 1] = hex_table[src[i] & 0xF];
2796  }
2797 
2798  return buff;
2799 }
2800 
2801 int ff_hex_to_data(uint8_t *data, const char *p)
2802 {
2803  int c, len, v;
2804 
2805  len = 0;
2806  v = 1;
2807  for (;;) {
2808  p += strspn(p, SPACE_CHARS);
2809  if (*p == '\0')
2810  break;
2811  c = av_toupper((unsigned char) *p++);
2812  if (c >= '0' && c <= '9')
2813  c = c - '0';
2814  else if (c >= 'A' && c <= 'F')
2815  c = c - 'A' + 10;
2816  else
2817  break;
2818  v = (v << 4) | c;
2819  if (v & 0x100) {
2820  if (data)
2821  data[len] = v;
2822  len++;
2823  v = 1;
2824  }
2825  }
2826  return len;
2827 }
2828 
2829 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
2830  unsigned int pts_num, unsigned int pts_den)
2831 {
2832  AVRational new_tb;
2833  if (av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)) {
2834  if (new_tb.num != pts_num)
2836  "st:%d removing common factor %d from timebase\n",
2837  s->index, pts_num / new_tb.num);
2838  } else
2840  "st:%d has too large timebase, reducing\n", s->index);
2841 
2842  if (new_tb.num <= 0 || new_tb.den <= 0) {
2844  "Ignoring attempt to set invalid timebase for st:%d\n",
2845  s->index);
2846  return;
2847  }
2848  s->time_base = new_tb;
2849  s->pts_wrap_bits = pts_wrap_bits;
2850 }
2851 
2852 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
2853  void *context)
2854 {
2855  const char *ptr = str;
2856 
2857  /* Parse key=value pairs. */
2858  for (;;) {
2859  const char *key;
2860  char *dest = NULL, *dest_end;
2861  int key_len, dest_len = 0;
2862 
2863  /* Skip whitespace and potential commas. */
2864  while (*ptr && (av_isspace(*ptr) || *ptr == ','))
2865  ptr++;
2866  if (!*ptr)
2867  break;
2868 
2869  key = ptr;
2870 
2871  if (!(ptr = strchr(key, '=')))
2872  break;
2873  ptr++;
2874  key_len = ptr - key;
2875 
2876  callback_get_buf(context, key, key_len, &dest, &dest_len);
2877  dest_end = dest + dest_len - 1;
2878 
2879  if (*ptr == '\"') {
2880  ptr++;
2881  while (*ptr && *ptr != '\"') {
2882  if (*ptr == '\\') {
2883  if (!ptr[1])
2884  break;
2885  if (dest && dest < dest_end)
2886  *dest++ = ptr[1];
2887  ptr += 2;
2888  } else {
2889  if (dest && dest < dest_end)
2890  *dest++ = *ptr;
2891  ptr++;
2892  }
2893  }
2894  if (*ptr == '\"')
2895  ptr++;
2896  } else {
2897  for (; *ptr && !(av_isspace(*ptr) || *ptr == ','); ptr++)
2898  if (dest && dest < dest_end)
2899  *dest++ = *ptr;
2900  }
2901  if (dest)
2902  *dest = 0;
2903  }
2904 }
2905 
2907 {
2908  int i;
2909  for (i = 0; i < s->nb_streams; i++)
2910  if (s->streams[i]->id == id)
2911  return i;
2912  return -1;
2913 }
2914 
2915 int64_t ff_iso8601_to_unix_time(const char *datestr)
2916 {
2917 #if HAVE_STRPTIME
2918  struct tm time1 = { 0 }, time2 = { 0 };
2919  char *ret1, *ret2;
2920  ret1 = strptime(datestr, "%Y - %m - %d %T", &time1);
2921  ret2 = strptime(datestr, "%Y - %m - %dT%T", &time2);
2922  if (ret2 && !ret1)
2923  return av_timegm(&time2);
2924  else
2925  return av_timegm(&time1);
2926 #else
2928  "strptime() unavailable on this system, cannot convert "
2929  "the date string.\n");
2930  return 0;
2931 #endif
2932 }
2933 
2935  int std_compliance)
2936 {
2937  if (ofmt) {
2938  if (ofmt->query_codec)
2939  return ofmt->query_codec(codec_id, std_compliance);
2940  else if (ofmt->codec_tag)
2941  return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
2942  else if (codec_id == ofmt->video_codec ||
2943  codec_id == ofmt->audio_codec ||
2944  codec_id == ofmt->subtitle_codec)
2945  return 1;
2946  }
2947  return AVERROR_PATCHWELCOME;
2948 }
2949 
2951 {
2952 #if CONFIG_NETWORK
2953  int ret;
2955  if ((ret = ff_network_init()) < 0)
2956  return ret;
2957  ff_tls_init();
2958 #endif
2959  return 0;
2960 }
2961 
2963 {
2964 #if CONFIG_NETWORK
2965  ff_network_close();
2966  ff_tls_deinit();
2967 #endif
2968  return 0;
2969 }
2970 
2972  uint64_t channel_layout, int32_t sample_rate,
2974 {
2975  uint32_t flags = 0;
2976  int size = 4;
2977  uint8_t *data;
2978  if (!pkt)
2979  return AVERROR(EINVAL);
2980  if (channels) {
2981  size += 4;
2983  }
2984  if (channel_layout) {
2985  size += 8;
2987  }
2988  if (sample_rate) {
2989  size += 4;
2991  }
2992  if (width || height) {
2993  size += 8;
2995  }
2997  if (!data)
2998  return AVERROR(ENOMEM);
2999  bytestream_put_le32(&data, flags);
3000  if (channels)
3001  bytestream_put_le32(&data, channels);
3002  if (channel_layout)
3003  bytestream_put_le64(&data, channel_layout);
3004  if (sample_rate)
3005  bytestream_put_le32(&data, sample_rate);
3006  if (width || height) {
3007  bytestream_put_le32(&data, width);
3008  bytestream_put_le32(&data, height);
3009  }
3010  return 0;
3011 }
3012 
3014 {
3015  static const uint8_t avci100_1080p_extradata[] = {
3016  // SPS
3017  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3018  0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
3019  0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
3020  0x18, 0x21, 0x02, 0x56, 0xb9, 0x3d, 0x7d, 0x7e,
3021  0x4f, 0xe3, 0x3f, 0x11, 0xf1, 0x9e, 0x08, 0xb8,
3022  0x8c, 0x54, 0x43, 0xc0, 0x78, 0x02, 0x27, 0xe2,
3023  0x70, 0x1e, 0x30, 0x10, 0x10, 0x14, 0x00, 0x00,
3024  0x03, 0x00, 0x04, 0x00, 0x00, 0x03, 0x00, 0xca,
3025  0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3026  // PPS
3027  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
3028  0xd0
3029  };
3030  static const uint8_t avci100_1080i_extradata[] = {
3031  // SPS
3032  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3033  0xb6, 0xd4, 0x20, 0x22, 0x33, 0x19, 0xc6, 0x63,
3034  0x23, 0x21, 0x01, 0x11, 0x98, 0xce, 0x33, 0x19,
3035  0x18, 0x21, 0x03, 0x3a, 0x46, 0x65, 0x6a, 0x65,
3036  0x24, 0xad, 0xe9, 0x12, 0x32, 0x14, 0x1a, 0x26,
3037  0x34, 0xad, 0xa4, 0x41, 0x82, 0x23, 0x01, 0x50,
3038  0x2b, 0x1a, 0x24, 0x69, 0x48, 0x30, 0x40, 0x2e,
3039  0x11, 0x12, 0x08, 0xc6, 0x8c, 0x04, 0x41, 0x28,
3040  0x4c, 0x34, 0xf0, 0x1e, 0x01, 0x13, 0xf2, 0xe0,
3041  0x3c, 0x60, 0x20, 0x20, 0x28, 0x00, 0x00, 0x03,
3042  0x00, 0x08, 0x00, 0x00, 0x03, 0x01, 0x94, 0x00,
3043  // PPS
3044  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x33, 0x48,
3045  0xd0
3046  };
3047  static const uint8_t avci50_1080i_extradata[] = {
3048  // SPS
3049  0x00, 0x00, 0x00, 0x01, 0x67, 0x6e, 0x10, 0x28,
3050  0xa6, 0xd4, 0x20, 0x32, 0x33, 0x0c, 0x71, 0x18,
3051  0x88, 0x62, 0x10, 0x19, 0x19, 0x86, 0x38, 0x8c,
3052  0x44, 0x30, 0x21, 0x02, 0x56, 0x4e, 0x6e, 0x61,
3053  0x87, 0x3e, 0x73, 0x4d, 0x98, 0x0c, 0x03, 0x06,
3054  0x9c, 0x0b, 0x73, 0xe6, 0xc0, 0xb5, 0x18, 0x63,
3055  0x0d, 0x39, 0xe0, 0x5b, 0x02, 0xd4, 0xc6, 0x19,
3056  0x1a, 0x79, 0x8c, 0x32, 0x34, 0x24, 0xf0, 0x16,
3057  0x81, 0x13, 0xf7, 0xff, 0x80, 0x01, 0x80, 0x02,
3058  0x71, 0x80, 0x80, 0x80, 0xa0, 0x00, 0x00, 0x03,
3059  0x00, 0x20, 0x00, 0x00, 0x06, 0x50, 0x80, 0x00,
3060  // PPS
3061  0x00, 0x00, 0x00, 0x01, 0x68, 0xee, 0x31, 0x12,
3062  0x11
3063  };
3064  static const uint8_t avci100_720p_extradata[] = {
3065  // SPS
3066  0x00, 0x00, 0x00, 0x01, 0x67, 0x7a, 0x10, 0x29,
3067  0xb6, 0xd4, 0x20, 0x2a, 0x33, 0x1d, 0xc7, 0x62,
3068  0xa1, 0x08, 0x40, 0x54, 0x66, 0x3b, 0x8e, 0xc5,
3069  0x42, 0x02, 0x10, 0x25, 0x64, 0x2c, 0x89, 0xe8,
3070  0x85, 0xe4, 0x21, 0x4b, 0x90, 0x83, 0x06, 0x95,
3071  0xd1, 0x06, 0x46, 0x97, 0x20, 0xc8, 0xd7, 0x43,
3072  0x08, 0x11, 0xc2, 0x1e, 0x4c, 0x91, 0x0f, 0x01,
3073  0x40, 0x16, 0xec, 0x07, 0x8c, 0x04, 0x04, 0x05,
3074  0x00, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x03,
3075  0x00, 0x64, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00,
3076  // PPS
3077  0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x31, 0x12,
3078  0x11
3079  };
3080 
3081  const uint8_t *data = NULL;
3082  int size = 0;
3083 
3084  if (st->codec->width == 1920) {
3085  if (st->codec->field_order == AV_FIELD_PROGRESSIVE) {
3086  data = avci100_1080p_extradata;
3087  size = sizeof(avci100_1080p_extradata);
3088  } else {
3089  data = avci100_1080i_extradata;
3090  size = sizeof(avci100_1080i_extradata);
3091  }
3092  } else if (st->codec->width == 1440) {
3093  data = avci50_1080i_extradata;
3094  size = sizeof(avci50_1080i_extradata);
3095  } else if (st->codec->width == 1280) {
3096  data = avci100_720p_extradata;
3097  size = sizeof(avci100_720p_extradata);
3098  }
3099 
3100  if (!size)
3101  return 0;
3102 
3103  av_freep(&st->codec->extradata);
3104  st->codec->extradata_size = 0;
3106  if (!st->codec->extradata)
3107  return AVERROR(ENOMEM);
3108 
3109  memcpy(st->codec->extradata, data, size);
3110  st->codec->extradata_size = size;
3111 
3112  return 0;
3113 }
3114 
3116  int *size)
3117 {
3118  int i;
3119 
3120  for (i = 0; i < st->nb_side_data; i++) {
3121  if (st->side_data[i].type == type) {
3122  if (size)
3123  *size = st->side_data[i].size;
3124  return st->side_data[i].data;
3125  }
3126  }
3127  return NULL;
3128 }
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:2713
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:433
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:2601
const struct AVCodec * codec
Definition: avcodec.h:1059
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:1644
#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:564
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
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1163
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:1181
static void update_stream_timings(AVFormatContext *ic)
Estimate the stream timings from the one of each components.
Definition: utils.c:1601
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:480
static int get_std_framerate(int i)
Definition: utils.c:2044
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:998
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:1228
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:546
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:1189
int index
stream index in AVFormatContext
Definition: avformat.h:700
int size
Definition: avcodec.h:974
static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:1706
#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(* read_close)(struct AVFormatContext *)
Close the stream.
Definition: avformat.h:607
int ff_find_stream_index(AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: utils.c:2906
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1254
uint8_t * av_stream_get_side_data(AVStream *st, enum AVPacketSideDataType type, int *size)
Get side information from stream.
Definition: utils.c:3115
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
int(* split)(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: avcodec.h:3830
#define LIBAV_CONFIGURATION
Definition: config.h:4
static void estimate_timings_from_bit_rate(AVFormatContext *ic)
Definition: utils.c:1663
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:3798
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:2852
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:1860
int av_opt_set_dict_val(void *obj, const char *name, const AVDictionary *val, int search_flags)
Definition: opt.c:312
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:190
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
Definition: utils.c:892
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:2796
static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:1783
static int64_t duration
Definition: avplay.c:246
static int has_decode_delay_been_guessed(AVStream *st)
Definition: utils.c:1853
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:1175
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:1866
Format I/O context.
Definition: avformat.h:922
static int has_codec_parameters(AVStream *st)
Definition: utils.c:1829
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:1702
#define AVFMT_FLAG_DISCARD_CORRUPT
Discard frames marked corrupted.
Definition: avformat.h:1042
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
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1799
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
int(* query_codec)(enum AVCodecID id, int std_compliance)
Test if the given codec can be stored in this container.
Definition: avformat.h:510
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
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:1943
#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:1164
const char * name
AVStream * avformat_new_stream(AVFormatContext *s, const AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2521
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:1703
uint8_t * data
Definition: avcodec.h:973
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:2578
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:2950
int(* read_play)(struct AVFormatContext *)
Start/resume playing - only meaningful if using a network-based format (RTSP).
Definition: avformat.h:631
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:994
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:923
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:2507
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:991
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:1955
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:760
#define AV_PKT_FLAG_KEY
The packet contains a keyframe.
Definition: avcodec.h:1019
const OptionDef options[]
Definition: avconv_opt.c:2187
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:2373
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:1710
#define AVINDEX_KEYFRAME
Definition: avformat.h:662
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1078
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:2043
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:1339
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:3786
int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1222
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:2780
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:1118
struct AVCodecParser * parser
Definition: avcodec.h:3668
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:2320
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 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:2389
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:1017
int capabilities
Codec capabilities.
Definition: avcodec.h:2815
#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:2427
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:956
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:925
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:169
int side_data_elems
Definition: avcodec.h:985
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
int ff_get_audio_frame_size(AVCodecContext *enc, int size, int mux)
Get the number of samples of an audio frame.
Definition: utils.c:459
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:979
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:2934
#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:1131
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:2829
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:128
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:2436
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:1114
#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
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
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:1224
int64_t offset
byte offset from starting packet start
Definition: avcodec.h:3705
int av_find_default_stream_index(AVFormatContext *s)
Definition: utils.c:1056
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:3733
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:1429
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:1295
#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:1184
#define AVFMT_FLAG_CUSTOM_IO
The caller has supplied a custom AVIOContext, don't avio_close() it.
Definition: avformat.h:1041
#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
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
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:1104
#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:2540
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:2660
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
int(* read_pause)(struct AVFormatContext *)
Pause playing - only meaningful if using a network-based format (RTSP).
Definition: avformat.h:637
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:2962
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1811
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:1058
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:1933
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:1067
#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:679
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:68
int sample_rate
samples per second
Definition: avcodec.h:1791
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:1998
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:471
int(* read_packet)(struct AVFormatContext *, AVPacket *pkt)
Read one packet and put it in 'pkt'.
Definition: avformat.h:601
static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts, int64_t pts)
Definition: utils.c:533
main external API structure.
Definition: avcodec.h:1050
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:1780
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
static void compute_pkt_fields(AVFormatContext *s, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Definition: utils.c:605
int extradata_size
Definition: avcodec.h:1165
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:2009
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:2801
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:2655
#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:3013
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:1487
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:2915
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:982
int found_decoder
Definition: avformat.h:835
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2445
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:2061
misc parsing utilities
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:989
static int has_duration(AVFormatContext *ic)
Return TRUE if the stream has accurate duration in any stream.
Definition: utils.c:1581
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:1542
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:1531
unsigned int tag
Definition: internal.h:37
int height
Definition: gxfenc.c:72
static int is_intra_only(enum AVCodecID id)
Definition: utils.c:523
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:3701
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:984
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:659
#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:2626
#define AVFMT_NOFILE
Demuxer will use avio_open, no opened file should be provided by the caller.
Definition: avformat.h:409
#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:2052
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
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
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
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:3700
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:1020
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:2499
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
Definition: utils.c:1842
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:3669
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
Parse a packet, add all split parts to parse_queue.
Definition: utils.c:776
static int read_from_packet_buffer(AVPacketList **pkt_buffer, AVPacketList **pkt_buffer_end, AVPacket *pkt)
Definition: utils.c:877
static AVProgram * find_program_from_stream(AVFormatContext *ic, int s)
Definition: utils.c:2362
#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(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition: avformat.h:590
int len
int channels
number of audio channels
Definition: avcodec.h:1792
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:1044
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:1574
#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:972
static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags)
Definition: utils.c:1411
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:3684
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
enum AVFieldOrder field_order
Field order.
Definition: avcodec.h:1788
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:2020
#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:975
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:2971
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:950
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3714
#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:966
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:2737
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:1630