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 /* #define DEBUG */
23 
24 #include "avformat.h"
25 #include "avio_internal.h"
26 #include "internal.h"
27 #include "libavcodec/internal.h"
28 #include "libavcodec/bytestream.h"
29 #include "libavutil/opt.h"
30 #include "libavutil/dict.h"
31 #include "libavutil/pixdesc.h"
32 #include "metadata.h"
33 #include "id3v2.h"
34 #include "libavutil/avassert.h"
35 #include "libavutil/avstring.h"
36 #include "libavutil/mathematics.h"
37 #include "libavutil/parseutils.h"
38 #include "libavutil/time.h"
39 #include "riff.h"
40 #include "audiointerleave.h"
41 #include "url.h"
42 #include <stdarg.h>
43 #if CONFIG_NETWORK
44 #include "network.h"
45 #endif
46 
47 #undef NDEBUG
48 #include <assert.h>
49 
55 unsigned avformat_version(void)
56 {
58 }
59 
60 const char *avformat_configuration(void)
61 {
62  return LIBAV_CONFIGURATION;
63 }
64 
65 const char *avformat_license(void)
66 {
67 #define LICENSE_PREFIX "libavformat license: "
68  return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
69 }
70 
75 
77 {
78  if(f) return f->next;
79  else return first_iformat;
80 }
81 
83 {
84  if(f) return f->next;
85  else return first_oformat;
86 }
87 
89 {
90  AVInputFormat **p;
91  p = &first_iformat;
92  while (*p != NULL) p = &(*p)->next;
93  *p = format;
94  format->next = NULL;
95 }
96 
98 {
99  AVOutputFormat **p;
100  p = &first_oformat;
101  while (*p != NULL) p = &(*p)->next;
102  *p = format;
103  format->next = NULL;
104 }
105 
106 int av_match_ext(const char *filename, const char *extensions)
107 {
108  const char *ext, *p;
109  char ext1[32], *q;
110 
111  if(!filename)
112  return 0;
113 
114  ext = strrchr(filename, '.');
115  if (ext) {
116  ext++;
117  p = extensions;
118  for(;;) {
119  q = ext1;
120  while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
121  *q++ = *p++;
122  *q = '\0';
123  if (!av_strcasecmp(ext1, ext))
124  return 1;
125  if (*p == '\0')
126  break;
127  p++;
128  }
129  }
130  return 0;
131 }
132 
133 static int match_format(const char *name, const char *names)
134 {
135  const char *p;
136  int len, namelen;
137 
138  if (!name || !names)
139  return 0;
140 
141  namelen = strlen(name);
142  while ((p = strchr(names, ','))) {
143  len = FFMAX(p - names, namelen);
144  if (!av_strncasecmp(name, names, len))
145  return 1;
146  names = p+1;
147  }
148  return !av_strcasecmp(name, names);
149 }
150 
151 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
152  const char *mime_type)
153 {
154  AVOutputFormat *fmt = NULL, *fmt_found;
155  int score_max, score;
156 
157  /* specific test for image sequences */
158 #if CONFIG_IMAGE2_MUXER
159  if (!short_name && filename &&
160  av_filename_number_test(filename) &&
162  return av_guess_format("image2", NULL, NULL);
163  }
164 #endif
165  /* Find the proper file type. */
166  fmt_found = NULL;
167  score_max = 0;
168  while ((fmt = av_oformat_next(fmt))) {
169  score = 0;
170  if (fmt->name && short_name && !av_strcasecmp(fmt->name, short_name))
171  score += 100;
172  if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
173  score += 10;
174  if (filename && fmt->extensions &&
175  av_match_ext(filename, fmt->extensions)) {
176  score += 5;
177  }
178  if (score > score_max) {
179  score_max = score;
180  fmt_found = fmt;
181  }
182  }
183  return fmt_found;
184 }
185 
186 enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
187  const char *filename, const char *mime_type, enum AVMediaType type){
188  if(type == AVMEDIA_TYPE_VIDEO){
190 
191 #if CONFIG_IMAGE2_MUXER
192  if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
193  codec_id= ff_guess_image2_codec(filename);
194  }
195 #endif
196  if(codec_id == AV_CODEC_ID_NONE)
197  codec_id= fmt->video_codec;
198  return codec_id;
199  }else if(type == AVMEDIA_TYPE_AUDIO)
200  return fmt->audio_codec;
201  else if (type == AVMEDIA_TYPE_SUBTITLE)
202  return fmt->subtitle_codec;
203  else
204  return AV_CODEC_ID_NONE;
205 }
206 
207 AVInputFormat *av_find_input_format(const char *short_name)
208 {
209  AVInputFormat *fmt = NULL;
210  while ((fmt = av_iformat_next(fmt))) {
211  if (match_format(short_name, fmt->name))
212  return fmt;
213  }
214  return NULL;
215 }
216 
217 
219 {
220  int ret= av_new_packet(pkt, size);
221 
222  if(ret<0)
223  return ret;
224 
225  pkt->pos= avio_tell(s);
226 
227  ret= avio_read(s, pkt->data, size);
228  if(ret<=0)
229  av_free_packet(pkt);
230  else
231  av_shrink_packet(pkt, ret);
232 
233  return ret;
234 }
235 
237 {
238  int ret;
239  int old_size;
240  if (!pkt->size)
241  return av_get_packet(s, pkt, size);
242  old_size = pkt->size;
243  ret = av_grow_packet(pkt, size);
244  if (ret < 0)
245  return ret;
246  ret = avio_read(s, pkt->data + old_size, size);
247  av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
248  return ret;
249 }
250 
251 
252 int av_filename_number_test(const char *filename)
253 {
254  char buf[1024];
255  return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
256 }
257 
258 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
259 {
260  AVProbeData lpd = *pd;
261  AVInputFormat *fmt1 = NULL, *fmt;
262  int score, id3 = 0;
263 
264  if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
265  int id3len = ff_id3v2_tag_len(lpd.buf);
266  if (lpd.buf_size > id3len + 16) {
267  lpd.buf += id3len;
268  lpd.buf_size -= id3len;
269  }
270  id3 = 1;
271  }
272 
273  fmt = NULL;
274  while ((fmt1 = av_iformat_next(fmt1))) {
275  if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
276  continue;
277  score = 0;
278  if (fmt1->read_probe) {
279  score = fmt1->read_probe(&lpd);
280  } else if (fmt1->extensions) {
281  if (av_match_ext(lpd.filename, fmt1->extensions)) {
282  score = 50;
283  }
284  }
285  if (score > *score_max) {
286  *score_max = score;
287  fmt = fmt1;
288  }else if (score == *score_max)
289  fmt = NULL;
290  }
291 
292  /* a hack for files with huge id3v2 tags -- try to guess by file extension. */
293  if (!fmt && is_opened && *score_max < AVPROBE_SCORE_MAX/4) {
294  while ((fmt = av_iformat_next(fmt)))
295  if (fmt->extensions && av_match_ext(lpd.filename, fmt->extensions)) {
296  *score_max = AVPROBE_SCORE_MAX/4;
297  break;
298  }
299  }
300 
301  if (!fmt && id3 && *score_max < AVPROBE_SCORE_MAX/4-1) {
302  while ((fmt = av_iformat_next(fmt)))
303  if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
304  *score_max = AVPROBE_SCORE_MAX/4-1;
305  break;
306  }
307  }
308 
309  return fmt;
310 }
311 
313  int score=0;
314  return av_probe_input_format2(pd, is_opened, &score);
315 }
316 
318 {
319  static const struct {
320  const char *name; enum AVCodecID id; enum AVMediaType type;
321  } fmt_id_type[] = {
322  { "aac" , AV_CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
323  { "ac3" , AV_CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
324  { "dts" , AV_CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
325  { "eac3" , AV_CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
326  { "h264" , AV_CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
328  { "mp3" , AV_CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
329  { "mpegvideo", AV_CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
330  { 0 }
331  };
332  AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
333 
334  if (fmt) {
335  int i;
336  av_log(s, AV_LOG_DEBUG, "Probe with size=%d, packets=%d detected %s with score=%d\n",
337  pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets, fmt->name, score);
338  for (i = 0; fmt_id_type[i].name; i++) {
339  if (!strcmp(fmt->name, fmt_id_type[i].name)) {
340  st->codec->codec_id = fmt_id_type[i].id;
341  st->codec->codec_type = fmt_id_type[i].type;
342  break;
343  }
344  }
345  }
346  return !!fmt;
347 }
348 
349 /************************************************************/
350 /* input media file */
351 
353 #define PROBE_BUF_MIN 2048
354 #define PROBE_BUF_MAX (1<<20)
355 
357  const char *filename, void *logctx,
358  unsigned int offset, unsigned int max_probe_size)
359 {
360  AVProbeData pd = { filename ? filename : "" };
361  uint8_t *buf = NULL;
362  int ret = 0, probe_size;
363 
364  if (!max_probe_size) {
365  max_probe_size = PROBE_BUF_MAX;
366  } else if (max_probe_size > PROBE_BUF_MAX) {
367  max_probe_size = PROBE_BUF_MAX;
368  } else if (max_probe_size < PROBE_BUF_MIN) {
369  return AVERROR(EINVAL);
370  }
371 
372  if (offset >= max_probe_size) {
373  return AVERROR(EINVAL);
374  }
375  avio_skip(pb, offset);
376  max_probe_size -= offset;
377 
378  for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt;
379  probe_size = FFMIN(probe_size<<1, FFMAX(max_probe_size, probe_size+1))) {
380  int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
381 
382  /* read probe data */
383  buf = av_realloc(buf, probe_size + AVPROBE_PADDING_SIZE);
384  if ((ret = avio_read(pb, buf + pd.buf_size, probe_size - pd.buf_size)) < 0) {
385  /* fail if error was not end of file, otherwise, lower score */
386  if (ret != AVERROR_EOF) {
387  av_free(buf);
388  return ret;
389  }
390  score = 0;
391  ret = 0; /* error was end of file, nothing read */
392  }
393  pd.buf_size += ret;
394  pd.buf = buf;
395 
396  memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
397 
398  /* guess file format */
399  *fmt = av_probe_input_format2(&pd, 1, &score);
400  if(*fmt){
401  if(score <= AVPROBE_SCORE_MAX/4){ //this can only be true in the last iteration
402  av_log(logctx, AV_LOG_WARNING, "Format detected only with low score of %d, misdetection possible!\n", score);
403  }else
404  av_log(logctx, AV_LOG_DEBUG, "Probed with size=%d and score=%d\n", probe_size, score);
405  }
406  }
407 
408  if (!*fmt) {
409  av_free(buf);
410  return AVERROR_INVALIDDATA;
411  }
412 
413  /* rewind. reuse probe buffer to avoid seeking */
414  if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
415  av_free(buf);
416 
417  return ret;
418 }
419 
420 /* open input file and probe the format if necessary */
421 static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
422 {
423  int ret;
424  AVProbeData pd = {filename, NULL, 0};
425 
426  if (s->pb) {
428  if (!s->iformat)
429  return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, s->probesize);
430  else if (s->iformat->flags & AVFMT_NOFILE)
431  return AVERROR(EINVAL);
432  return 0;
433  }
434 
435  if ( (s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
436  (!s->iformat && (s->iformat = av_probe_input_format(&pd, 0))))
437  return 0;
438 
439  if ((ret = avio_open2(&s->pb, filename, AVIO_FLAG_READ,
440  &s->interrupt_callback, options)) < 0)
441  return ret;
442  if (s->iformat)
443  return 0;
444  return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, s->probesize);
445 }
446 
447 static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
448  AVPacketList **plast_pktl){
449  AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
450  if (!pktl)
451  return NULL;
452 
453  if (*packet_buffer)
454  (*plast_pktl)->next = pktl;
455  else
456  *packet_buffer = pktl;
457 
458  /* add the packet in the buffered packet list */
459  *plast_pktl = pktl;
460  pktl->pkt= *pkt;
461  return &pktl->pkt;
462 }
463 
465 {
466  int i;
467  for (i = 0; i < s->nb_streams; i++)
469  s->streams[i]->discard < AVDISCARD_ALL) {
471  copy.destruct = NULL;
473  }
474 }
475 
476 int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
477 {
478  AVFormatContext *s = *ps;
479  int ret = 0;
480  AVDictionary *tmp = NULL;
481  ID3v2ExtraMeta *id3v2_extra_meta = NULL;
482 
483  if (!s && !(s = avformat_alloc_context()))
484  return AVERROR(ENOMEM);
485  if (fmt)
486  s->iformat = fmt;
487 
488  if (options)
489  av_dict_copy(&tmp, *options, 0);
490 
491  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
492  goto fail;
493 
494  if ((ret = init_input(s, filename, &tmp)) < 0)
495  goto fail;
496 
497  /* check filename in case an image number is expected */
498  if (s->iformat->flags & AVFMT_NEEDNUMBER) {
499  if (!av_filename_number_test(filename)) {
500  ret = AVERROR(EINVAL);
501  goto fail;
502  }
503  }
504 
506  av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
507 
508  /* allocate private data */
509  if (s->iformat->priv_data_size > 0) {
510  if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
511  ret = AVERROR(ENOMEM);
512  goto fail;
513  }
514  if (s->iformat->priv_class) {
515  *(const AVClass**)s->priv_data = s->iformat->priv_class;
517  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
518  goto fail;
519  }
520  }
521 
522  /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
523  if (s->pb)
524  ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC, &id3v2_extra_meta);
525 
526  if (s->iformat->read_header)
527  if ((ret = s->iformat->read_header(s)) < 0)
528  goto fail;
529 
530  if (id3v2_extra_meta &&
531  (ret = ff_id3v2_parse_apic(s, &id3v2_extra_meta)) < 0)
532  goto fail;
533  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
534 
536 
537  if (s->pb && !s->data_offset)
538  s->data_offset = avio_tell(s->pb);
539 
541 
542  if (options) {
543  av_dict_free(options);
544  *options = tmp;
545  }
546  *ps = s;
547  return 0;
548 
549 fail:
550  ff_id3v2_free_extra_meta(&id3v2_extra_meta);
551  av_dict_free(&tmp);
552  if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
553  avio_close(s->pb);
555  *ps = NULL;
556  return ret;
557 }
558 
559 /*******************************************************/
560 
561 static void probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
562 {
563  if(st->codec->codec_id == AV_CODEC_ID_PROBE){
564  AVProbeData *pd = &st->probe_data;
565  av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
566  --st->probe_packets;
567 
568  if (pkt) {
569  pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
570  memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
571  pd->buf_size += pkt->size;
572  memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
573  } else {
574  st->probe_packets = 0;
575  if (!pd->buf_size) {
576  av_log(s, AV_LOG_ERROR, "nothing to probe for stream %d\n",
577  st->index);
578  return;
579  }
580  }
581 
582  if (!st->probe_packets ||
583  av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)) {
584  set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0);
585  if(st->codec->codec_id != AV_CODEC_ID_PROBE){
586  pd->buf_size=0;
587  av_freep(&pd->buf);
588  av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
589  }
590  }
591  }
592 }
593 
595 {
596  int ret, i;
597  AVStream *st;
598 
599  for(;;){
600  AVPacketList *pktl = s->raw_packet_buffer;
601 
602  if (pktl) {
603  *pkt = pktl->pkt;
604  st = s->streams[pkt->stream_index];
605  if (st->codec->codec_id != AV_CODEC_ID_PROBE || !st->probe_packets ||
607  AVProbeData *pd;
608  if (st->probe_packets) {
609  probe_codec(s, st, NULL);
610  }
611  pd = &st->probe_data;
612  av_freep(&pd->buf);
613  pd->buf_size = 0;
614  s->raw_packet_buffer = pktl->next;
616  av_free(pktl);
617  return 0;
618  }
619  }
620 
621  pkt->data = NULL;
622  pkt->size = 0;
623  av_init_packet(pkt);
624  ret= s->iformat->read_packet(s, pkt);
625  if (ret < 0) {
626  if (!pktl || ret == AVERROR(EAGAIN))
627  return ret;
628  for (i = 0; i < s->nb_streams; i++) {
629  st = s->streams[i];
630  if (st->probe_packets) {
631  probe_codec(s, st, NULL);
632  }
633  }
634  continue;
635  }
636 
637  if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
638  (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
640  "Dropped corrupted packet (stream = %d)\n",
641  pkt->stream_index);
642  av_free_packet(pkt);
643  continue;
644  }
645 
646  st= s->streams[pkt->stream_index];
647 
648  switch(st->codec->codec_type){
649  case AVMEDIA_TYPE_VIDEO:
651  break;
652  case AVMEDIA_TYPE_AUDIO:
654  break;
657  break;
658  }
659 
660  if(!pktl && (st->codec->codec_id != AV_CODEC_ID_PROBE ||
661  !st->probe_packets))
662  return ret;
663 
666 
667  probe_codec(s, st, pkt);
668  }
669 }
670 
671 #if FF_API_READ_PACKET
672 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
673 {
674  return ff_read_packet(s, pkt);
675 }
676 #endif
677 
678 
679 /**********************************************************/
680 
685 {
686  int frame_size;
687 
688  /* give frame_size priority if demuxing */
689  if (!mux && enc->frame_size > 1)
690  return enc->frame_size;
691 
692  if ((frame_size = av_get_audio_frame_duration(enc, size)) > 0)
693  return frame_size;
694 
695  /* fallback to using frame_size if muxing */
696  if (enc->frame_size > 1)
697  return enc->frame_size;
698 
699  return -1;
700 }
701 
702 
706 void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st,
707  AVCodecParserContext *pc, AVPacket *pkt)
708 {
709  int frame_size;
710 
711  *pnum = 0;
712  *pden = 0;
713  switch(st->codec->codec_type) {
714  case AVMEDIA_TYPE_VIDEO:
715  if (st->avg_frame_rate.num) {
716  *pnum = st->avg_frame_rate.den;
717  *pden = st->avg_frame_rate.num;
718  } else if(st->time_base.num*1000LL > st->time_base.den) {
719  *pnum = st->time_base.num;
720  *pden = st->time_base.den;
721  }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
722  *pnum = st->codec->time_base.num;
723  *pden = st->codec->time_base.den;
724  if (pc && pc->repeat_pict) {
725  if (*pnum > INT_MAX / (1 + pc->repeat_pict))
726  *pden /= 1 + pc->repeat_pict;
727  else
728  *pnum *= 1 + pc->repeat_pict;
729  }
730  //If this codec can be interlaced or progressive then we need a parser to compute duration of a packet
731  //Thus if we have no parser in such case leave duration undefined.
732  if(st->codec->ticks_per_frame>1 && !pc){
733  *pnum = *pden = 0;
734  }
735  }
736  break;
737  case AVMEDIA_TYPE_AUDIO:
738  frame_size = ff_get_audio_frame_size(st->codec, pkt->size, 0);
739  if (frame_size <= 0 || st->codec->sample_rate <= 0)
740  break;
741  *pnum = frame_size;
742  *pden = st->codec->sample_rate;
743  break;
744  default:
745  break;
746  }
747 }
748 
749 static int is_intra_only(enum AVCodecID id)
750 {
752  if (!d)
753  return 0;
755  return 0;
756  return 1;
757 }
758 
759 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
760  int64_t dts, int64_t pts)
761 {
762  AVStream *st= s->streams[stream_index];
763  AVPacketList *pktl= s->packet_buffer;
764 
765  if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE)
766  return;
767 
768  st->first_dts= dts - st->cur_dts;
769  st->cur_dts= dts;
770 
771  for(; pktl; pktl= pktl->next){
772  if(pktl->pkt.stream_index != stream_index)
773  continue;
774  //FIXME think more about this check
775  if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
776  pktl->pkt.pts += st->first_dts;
777 
778  if(pktl->pkt.dts != AV_NOPTS_VALUE)
779  pktl->pkt.dts += st->first_dts;
780 
781  if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
782  st->start_time= pktl->pkt.pts;
783  }
784  if (st->start_time == AV_NOPTS_VALUE)
785  st->start_time = pts;
786 }
787 
789  int stream_index, int duration)
790 {
791  AVPacketList *pktl= s->packet_buffer;
792  int64_t cur_dts= 0;
793 
794  if(st->first_dts != AV_NOPTS_VALUE){
795  cur_dts= st->first_dts;
796  for(; pktl; pktl= pktl->next){
797  if(pktl->pkt.stream_index == stream_index){
798  if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
799  break;
800  cur_dts -= duration;
801  }
802  }
803  pktl= s->packet_buffer;
804  st->first_dts = cur_dts;
805  }else if(st->cur_dts)
806  return;
807 
808  for(; pktl; pktl= pktl->next){
809  if(pktl->pkt.stream_index != stream_index)
810  continue;
811  if(pktl->pkt.pts == pktl->pkt.dts && pktl->pkt.dts == AV_NOPTS_VALUE
812  && !pktl->pkt.duration){
813  pktl->pkt.dts= cur_dts;
814  if(!st->codec->has_b_frames)
815  pktl->pkt.pts= cur_dts;
816  cur_dts += duration;
817  if (st->codec->codec_type != AVMEDIA_TYPE_AUDIO)
818  pktl->pkt.duration = duration;
819  }else
820  break;
821  }
822  if(st->first_dts == AV_NOPTS_VALUE)
823  st->cur_dts= cur_dts;
824 }
825 
827  AVCodecParserContext *pc, AVPacket *pkt)
828 {
829  int num, den, presentation_delayed, delay, i;
830  int64_t offset;
831 
832  if (s->flags & AVFMT_FLAG_NOFILLIN)
833  return;
834 
835  if((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
836  pkt->dts= AV_NOPTS_VALUE;
837 
838  /* do we have a video B-frame ? */
839  delay= st->codec->has_b_frames;
840  presentation_delayed = 0;
841 
842  /* XXX: need has_b_frame, but cannot get it if the codec is
843  not initialized */
844  if (delay &&
845  pc && pc->pict_type != AV_PICTURE_TYPE_B)
846  presentation_delayed = 1;
847 
848  if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
849  /*&& pkt->dts-(1LL<<st->pts_wrap_bits) < pkt->pts*/){
850  pkt->dts -= 1LL<<st->pts_wrap_bits;
851  }
852 
853  // some mpeg2 in mpeg-ps lack dts (issue171 / input_file.mpg)
854  // we take the conservative approach and discard both
855  // Note, if this is misbehaving for a H.264 file then possibly presentation_delayed is not set correctly.
856  if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){
857  av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
858  pkt->dts= pkt->pts= AV_NOPTS_VALUE;
859  }
860 
861  if (pkt->duration == 0 && st->codec->codec_type != AVMEDIA_TYPE_AUDIO) {
862  ff_compute_frame_duration(&num, &den, st, pc, pkt);
863  if (den && num) {
864  pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
865 
866  if(pkt->duration != 0 && s->packet_buffer)
868  }
869  }
870 
871  /* correct timestamps with byte offset if demuxers only have timestamps
872  on packet boundaries */
873  if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
874  /* this will estimate bitrate based on this frame's duration and size */
875  offset = av_rescale(pc->offset, pkt->duration, pkt->size);
876  if(pkt->pts != AV_NOPTS_VALUE)
877  pkt->pts += offset;
878  if(pkt->dts != AV_NOPTS_VALUE)
879  pkt->dts += offset;
880  }
881 
882  if (pc && pc->dts_sync_point >= 0) {
883  // we have synchronization info from the parser
884  int64_t den = st->codec->time_base.den * (int64_t) st->time_base.num;
885  if (den > 0) {
886  int64_t num = st->codec->time_base.num * (int64_t) st->time_base.den;
887  if (pkt->dts != AV_NOPTS_VALUE) {
888  // got DTS from the stream, update reference timestamp
889  st->reference_dts = pkt->dts - pc->dts_ref_dts_delta * num / den;
890  pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
891  } else if (st->reference_dts != AV_NOPTS_VALUE) {
892  // compute DTS based on reference timestamp
893  pkt->dts = st->reference_dts + pc->dts_ref_dts_delta * num / den;
894  pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
895  }
896  if (pc->dts_sync_point > 0)
897  st->reference_dts = pkt->dts; // new reference
898  }
899  }
900 
901  /* This may be redundant, but it should not hurt. */
902  if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
903  presentation_delayed = 1;
904 
905  av_dlog(NULL,
906  "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n",
907  presentation_delayed, pkt->pts, pkt->dts, st->cur_dts,
908  pkt->stream_index, pc);
909  /* interpolate PTS and DTS if they are not present */
910  //We skip H264 currently because delay and has_b_frames are not reliably set
911  if((delay==0 || (delay==1 && pc)) && st->codec->codec_id != AV_CODEC_ID_H264){
912  if (presentation_delayed) {
913  /* DTS = decompression timestamp */
914  /* PTS = presentation timestamp */
915  if (pkt->dts == AV_NOPTS_VALUE)
916  pkt->dts = st->last_IP_pts;
917  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
918  if (pkt->dts == AV_NOPTS_VALUE)
919  pkt->dts = st->cur_dts;
920 
921  /* this is tricky: the dts must be incremented by the duration
922  of the frame we are displaying, i.e. the last I- or P-frame */
923  if (st->last_IP_duration == 0)
924  st->last_IP_duration = pkt->duration;
925  if(pkt->dts != AV_NOPTS_VALUE)
926  st->cur_dts = pkt->dts + st->last_IP_duration;
927  st->last_IP_duration = pkt->duration;
928  st->last_IP_pts= pkt->pts;
929  /* cannot compute PTS if not present (we can compute it only
930  by knowing the future */
931  } else if (pkt->pts != AV_NOPTS_VALUE ||
932  pkt->dts != AV_NOPTS_VALUE ||
933  pkt->duration ||
935  int duration = pkt->duration;
936  if (!duration && st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
937  ff_compute_frame_duration(&num, &den, st, pc, pkt);
938  if (den && num) {
939  duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den,
940  den * (int64_t)st->time_base.num,
941  AV_ROUND_DOWN);
942  if (duration != 0 && s->packet_buffer) {
944  duration);
945  }
946  }
947  }
948 
949  if (pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE ||
950  duration) {
951  /* presentation is not delayed : PTS and DTS are the same */
952  if (pkt->pts == AV_NOPTS_VALUE)
953  pkt->pts = pkt->dts;
955  pkt->pts);
956  if (pkt->pts == AV_NOPTS_VALUE)
957  pkt->pts = st->cur_dts;
958  pkt->dts = pkt->pts;
959  if (pkt->pts != AV_NOPTS_VALUE)
960  st->cur_dts = pkt->pts + duration;
961  }
962  }
963  }
964 
965  if(pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
966  st->pts_buffer[0]= pkt->pts;
967  for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
968  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
969  if(pkt->dts == AV_NOPTS_VALUE)
970  pkt->dts= st->pts_buffer[0];
971  if(st->codec->codec_id == AV_CODEC_ID_H264){ // we skipped it above so we try here
972  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts); // this should happen on the first packet
973  }
974  if(pkt->dts > st->cur_dts)
975  st->cur_dts = pkt->dts;
976  }
977 
978  av_dlog(NULL,
979  "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n",
980  presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
981 
982  /* update flags */
983  if (is_intra_only(st->codec->codec_id))
984  pkt->flags |= AV_PKT_FLAG_KEY;
985  if (pc)
987 }
988 
989 static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
990 {
991  while (*pkt_buf) {
992  AVPacketList *pktl = *pkt_buf;
993  *pkt_buf = pktl->next;
994  av_free_packet(&pktl->pkt);
995  av_freep(&pktl);
996  }
997  *pkt_buf_end = NULL;
998 }
999 
1005 static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
1006 {
1007  AVPacket out_pkt = { 0 }, flush_pkt = { 0 };
1008  AVStream *st = s->streams[stream_index];
1009  uint8_t *data = pkt ? pkt->data : NULL;
1010  int size = pkt ? pkt->size : 0;
1011  int ret = 0, got_output = 0;
1012 
1013  if (!pkt) {
1015  pkt = &flush_pkt;
1016  got_output = 1;
1017  }
1018 
1019  while (size > 0 || (pkt == &flush_pkt && got_output)) {
1020  int len;
1021 
1022  av_init_packet(&out_pkt);
1023  len = av_parser_parse2(st->parser, st->codec,
1024  &out_pkt.data, &out_pkt.size, data, size,
1025  pkt->pts, pkt->dts, pkt->pos);
1026 
1027  pkt->pts = pkt->dts = AV_NOPTS_VALUE;
1028  /* increment read pointer */
1029  data += len;
1030  size -= len;
1031 
1032  got_output = !!out_pkt.size;
1033 
1034  if (!out_pkt.size)
1035  continue;
1036 
1037  /* set the duration */
1038  out_pkt.duration = 0;
1039  if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
1040  if (st->codec->sample_rate > 0) {
1041  out_pkt.duration = av_rescale_q_rnd(st->parser->duration,
1042  (AVRational){ 1, st->codec->sample_rate },
1043  st->time_base,
1044  AV_ROUND_DOWN);
1045  }
1046  } else if (st->codec->time_base.num != 0 &&
1047  st->codec->time_base.den != 0) {
1048  out_pkt.duration = av_rescale_q_rnd(st->parser->duration,
1049  st->codec->time_base,
1050  st->time_base,
1051  AV_ROUND_DOWN);
1052  }
1053 
1054  out_pkt.stream_index = st->index;
1055  out_pkt.pts = st->parser->pts;
1056  out_pkt.dts = st->parser->dts;
1057  out_pkt.pos = st->parser->pos;
1058 
1059  if (st->parser->key_frame == 1 ||
1060  (st->parser->key_frame == -1 &&
1062  out_pkt.flags |= AV_PKT_FLAG_KEY;
1063 
1064  compute_pkt_fields(s, st, st->parser, &out_pkt);
1065 
1066  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1067  out_pkt.flags & AV_PKT_FLAG_KEY) {
1068  ff_reduce_index(s, st->index);
1069  av_add_index_entry(st, st->parser->frame_offset, out_pkt.dts,
1070  0, 0, AVINDEX_KEYFRAME);
1071  }
1072 
1073  if (out_pkt.data == pkt->data && out_pkt.size == pkt->size) {
1074  out_pkt.destruct = pkt->destruct;
1075  pkt->destruct = NULL;
1076  }
1077  if ((ret = av_dup_packet(&out_pkt)) < 0)
1078  goto fail;
1079 
1080  if (!add_to_pktbuf(&s->parse_queue, &out_pkt, &s->parse_queue_end)) {
1081  av_free_packet(&out_pkt);
1082  ret = AVERROR(ENOMEM);
1083  goto fail;
1084  }
1085  }
1086 
1087 
1088  /* end of the stream => close and free the parser */
1089  if (pkt == &flush_pkt) {
1090  av_parser_close(st->parser);
1091  st->parser = NULL;
1092  }
1093 
1094 fail:
1095  av_free_packet(pkt);
1096  return ret;
1097 }
1098 
1099 static int read_from_packet_buffer(AVPacketList **pkt_buffer,
1100  AVPacketList **pkt_buffer_end,
1101  AVPacket *pkt)
1102 {
1103  AVPacketList *pktl;
1104  av_assert0(*pkt_buffer);
1105  pktl = *pkt_buffer;
1106  *pkt = pktl->pkt;
1107  *pkt_buffer = pktl->next;
1108  if (!pktl->next)
1109  *pkt_buffer_end = NULL;
1110  av_freep(&pktl);
1111  return 0;
1112 }
1113 
1115 {
1116  int ret = 0, i, got_packet = 0;
1117 
1118  av_init_packet(pkt);
1119 
1120  while (!got_packet && !s->parse_queue) {
1121  AVStream *st;
1122  AVPacket cur_pkt;
1123 
1124  /* read next packet */
1125  ret = ff_read_packet(s, &cur_pkt);
1126  if (ret < 0) {
1127  if (ret == AVERROR(EAGAIN))
1128  return ret;
1129  /* flush the parsers */
1130  for(i = 0; i < s->nb_streams; i++) {
1131  st = s->streams[i];
1132  if (st->parser && st->need_parsing)
1133  parse_packet(s, NULL, st->index);
1134  }
1135  /* all remaining packets are now in parse_queue =>
1136  * really terminate parsing */
1137  break;
1138  }
1139  ret = 0;
1140  st = s->streams[cur_pkt.stream_index];
1141 
1142  if (cur_pkt.pts != AV_NOPTS_VALUE &&
1143  cur_pkt.dts != AV_NOPTS_VALUE &&
1144  cur_pkt.pts < cur_pkt.dts) {
1145  av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
1146  cur_pkt.stream_index,
1147  cur_pkt.pts,
1148  cur_pkt.dts,
1149  cur_pkt.size);
1150  }
1151  if (s->debug & FF_FDEBUG_TS)
1152  av_log(s, AV_LOG_DEBUG, "ff_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
1153  cur_pkt.stream_index,
1154  cur_pkt.pts,
1155  cur_pkt.dts,
1156  cur_pkt.size,
1157  cur_pkt.duration,
1158  cur_pkt.flags);
1159 
1160  if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1161  st->parser = av_parser_init(st->codec->codec_id);
1162  if (!st->parser) {
1163  /* no parser available: just output the raw packets */
1165  } else if(st->need_parsing == AVSTREAM_PARSE_HEADERS) {
1167  } else if(st->need_parsing == AVSTREAM_PARSE_FULL_ONCE) {
1168  st->parser->flags |= PARSER_FLAG_ONCE;
1169  }
1170  }
1171 
1172  if (!st->need_parsing || !st->parser) {
1173  /* no parsing needed: we just output the packet as is */
1174  *pkt = cur_pkt;
1175  compute_pkt_fields(s, st, NULL, pkt);
1176  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1177  (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1178  ff_reduce_index(s, st->index);
1179  av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1180  }
1181  got_packet = 1;
1182  } else if (st->discard < AVDISCARD_ALL) {
1183  if ((ret = parse_packet(s, &cur_pkt, cur_pkt.stream_index)) < 0)
1184  return ret;
1185  } else {
1186  /* free packet */
1187  av_free_packet(&cur_pkt);
1188  }
1189  }
1190 
1191  if (!got_packet && s->parse_queue)
1193 
1194  if(s->debug & FF_FDEBUG_TS)
1195  av_log(s, AV_LOG_DEBUG, "read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
1196  pkt->stream_index,
1197  pkt->pts,
1198  pkt->dts,
1199  pkt->size,
1200  pkt->duration,
1201  pkt->flags);
1202 
1203  return ret;
1204 }
1205 
1207 {
1208  const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1209  int eof = 0;
1210 
1211  if (!genpts)
1213  &s->packet_buffer_end,
1214  pkt) :
1215  read_frame_internal(s, pkt);
1216 
1217  for (;;) {
1218  int ret;
1219  AVPacketList *pktl = s->packet_buffer;
1220 
1221  if (pktl) {
1222  AVPacket *next_pkt = &pktl->pkt;
1223 
1224  if (next_pkt->dts != AV_NOPTS_VALUE) {
1225  int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1226  while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1227  if (pktl->pkt.stream_index == next_pkt->stream_index &&
1228  (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0) &&
1229  av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) { //not b frame
1230  next_pkt->pts = pktl->pkt.dts;
1231  }
1232  pktl = pktl->next;
1233  }
1234  pktl = s->packet_buffer;
1235  }
1236 
1237  /* read packet from packet buffer, if there is data */
1238  if (!(next_pkt->pts == AV_NOPTS_VALUE &&
1239  next_pkt->dts != AV_NOPTS_VALUE && !eof))
1241  &s->packet_buffer_end, pkt);
1242  }
1243 
1244  ret = read_frame_internal(s, pkt);
1245  if (ret < 0) {
1246  if (pktl && ret != AVERROR(EAGAIN)) {
1247  eof = 1;
1248  continue;
1249  } else
1250  return ret;
1251  }
1252 
1254  &s->packet_buffer_end)) < 0)
1255  return AVERROR(ENOMEM);
1256  }
1257 }
1258 
1259 /* XXX: suppress the packet queue */
1261 {
1265 
1267 }
1268 
1269 /*******************************************************/
1270 /* seek support */
1271 
1273 {
1274  int first_audio_index = -1;
1275  int i;
1276  AVStream *st;
1277 
1278  if (s->nb_streams <= 0)
1279  return -1;
1280  for(i = 0; i < s->nb_streams; i++) {
1281  st = s->streams[i];
1282  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO &&
1284  return i;
1285  }
1286  if (first_audio_index < 0 && st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1287  first_audio_index = i;
1288  }
1289  return first_audio_index >= 0 ? first_audio_index : 0;
1290 }
1291 
1296 {
1297  AVStream *st;
1298  int i, j;
1299 
1300  flush_packet_queue(s);
1301 
1302  /* for each stream, reset read state */
1303  for(i = 0; i < s->nb_streams; i++) {
1304  st = s->streams[i];
1305 
1306  if (st->parser) {
1307  av_parser_close(st->parser);
1308  st->parser = NULL;
1309  }
1311  st->cur_dts = AV_NOPTS_VALUE; /* we set the current DTS to an unspecified origin */
1313 
1315 
1316  for(j=0; j<MAX_REORDER_DELAY+1; j++)
1317  st->pts_buffer[j]= AV_NOPTS_VALUE;
1318  }
1319 }
1320 
1321 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1322 {
1323  int i;
1324 
1325  for(i = 0; i < s->nb_streams; i++) {
1326  AVStream *st = s->streams[i];
1327 
1328  st->cur_dts = av_rescale(timestamp,
1329  st->time_base.den * (int64_t)ref_st->time_base.num,
1330  st->time_base.num * (int64_t)ref_st->time_base.den);
1331  }
1332 }
1333 
1334 void ff_reduce_index(AVFormatContext *s, int stream_index)
1335 {
1336  AVStream *st= s->streams[stream_index];
1337  unsigned int max_entries= s->max_index_size / sizeof(AVIndexEntry);
1338 
1339  if((unsigned)st->nb_index_entries >= max_entries){
1340  int i;
1341  for(i=0; 2*i<st->nb_index_entries; i++)
1342  st->index_entries[i]= st->index_entries[2*i];
1343  st->nb_index_entries= i;
1344  }
1345 }
1346 
1347 int ff_add_index_entry(AVIndexEntry **index_entries,
1348  int *nb_index_entries,
1349  unsigned int *index_entries_allocated_size,
1350  int64_t pos, int64_t timestamp, int size, int distance, int flags)
1351 {
1352  AVIndexEntry *entries, *ie;
1353  int index;
1354 
1355  if((unsigned)*nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1356  return -1;
1357 
1358  entries = av_fast_realloc(*index_entries,
1359  index_entries_allocated_size,
1360  (*nb_index_entries + 1) *
1361  sizeof(AVIndexEntry));
1362  if(!entries)
1363  return -1;
1364 
1365  *index_entries= entries;
1366 
1367  index= ff_index_search_timestamp(*index_entries, *nb_index_entries, timestamp, AVSEEK_FLAG_ANY);
1368 
1369  if(index<0){
1370  index= (*nb_index_entries)++;
1371  ie= &entries[index];
1372  assert(index==0 || ie[-1].timestamp < timestamp);
1373  }else{
1374  ie= &entries[index];
1375  if(ie->timestamp != timestamp){
1376  if(ie->timestamp <= timestamp)
1377  return -1;
1378  memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(*nb_index_entries - index));
1379  (*nb_index_entries)++;
1380  }else if(ie->pos == pos && distance < ie->min_distance) //do not reduce the distance
1381  distance= ie->min_distance;
1382  }
1383 
1384  ie->pos = pos;
1385  ie->timestamp = timestamp;
1386  ie->min_distance= distance;
1387  ie->size= size;
1388  ie->flags = flags;
1389 
1390  return index;
1391 }
1392 
1394  int64_t pos, int64_t timestamp, int size, int distance, int flags)
1395 {
1397  &st->index_entries_allocated_size, pos,
1398  timestamp, size, distance, flags);
1399 }
1400 
1401 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1402  int64_t wanted_timestamp, int flags)
1403 {
1404  int a, b, m;
1405  int64_t timestamp;
1406 
1407  a = - 1;
1408  b = nb_entries;
1409 
1410  //optimize appending index entries at the end
1411  if(b && entries[b-1].timestamp < wanted_timestamp)
1412  a= b-1;
1413 
1414  while (b - a > 1) {
1415  m = (a + b) >> 1;
1416  timestamp = entries[m].timestamp;
1417  if(timestamp >= wanted_timestamp)
1418  b = m;
1419  if(timestamp <= wanted_timestamp)
1420  a = m;
1421  }
1422  m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
1423 
1424  if(!(flags & AVSEEK_FLAG_ANY)){
1425  while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){
1426  m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1427  }
1428  }
1429 
1430  if(m == nb_entries)
1431  return -1;
1432  return m;
1433 }
1434 
1435 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
1436  int flags)
1437 {
1439  wanted_timestamp, flags);
1440 }
1441 
1442 int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1443 {
1444  AVInputFormat *avif= s->iformat;
1445  int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
1446  int64_t ts_min, ts_max, ts;
1447  int index;
1448  int64_t ret;
1449  AVStream *st;
1450 
1451  if (stream_index < 0)
1452  return -1;
1453 
1454  av_dlog(s, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
1455 
1456  ts_max=
1457  ts_min= AV_NOPTS_VALUE;
1458  pos_limit= -1; //gcc falsely says it may be uninitialized
1459 
1460  st= s->streams[stream_index];
1461  if(st->index_entries){
1462  AVIndexEntry *e;
1463 
1464  index= av_index_search_timestamp(st, target_ts, flags | AVSEEK_FLAG_BACKWARD); //FIXME whole func must be checked for non-keyframe entries in index case, especially read_timestamp()
1465  index= FFMAX(index, 0);
1466  e= &st->index_entries[index];
1467 
1468  if(e->timestamp <= target_ts || e->pos == e->min_distance){
1469  pos_min= e->pos;
1470  ts_min= e->timestamp;
1471  av_dlog(s, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
1472  pos_min,ts_min);
1473  }else{
1474  assert(index==0);
1475  }
1476 
1477  index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
1478  assert(index < st->nb_index_entries);
1479  if(index >= 0){
1480  e= &st->index_entries[index];
1481  assert(e->timestamp >= target_ts);
1482  pos_max= e->pos;
1483  ts_max= e->timestamp;
1484  pos_limit= pos_max - e->min_distance;
1485  av_dlog(s, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64" dts_max=%"PRId64"\n",
1486  pos_max,pos_limit, ts_max);
1487  }
1488  }
1489 
1490  pos= ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp);
1491  if(pos<0)
1492  return -1;
1493 
1494  /* do the seek */
1495  if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
1496  return ret;
1497 
1498  ff_update_cur_dts(s, st, ts);
1499 
1500  return 0;
1501 }
1502 
1503 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1504  int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1505  int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret,
1506  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
1507 {
1508  int64_t pos, ts;
1509  int64_t start_pos, filesize;
1510  int no_change;
1511 
1512  av_dlog(s, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
1513 
1514  if(ts_min == AV_NOPTS_VALUE){
1515  pos_min = s->data_offset;
1516  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1517  if (ts_min == AV_NOPTS_VALUE)
1518  return -1;
1519  }
1520 
1521  if(ts_max == AV_NOPTS_VALUE){
1522  int step= 1024;
1523  filesize = avio_size(s->pb);
1524  pos_max = filesize - 1;
1525  do{
1526  pos_max -= step;
1527  ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step);
1528  step += step;
1529  }while(ts_max == AV_NOPTS_VALUE && pos_max >= step);
1530  if (ts_max == AV_NOPTS_VALUE)
1531  return -1;
1532 
1533  for(;;){
1534  int64_t tmp_pos= pos_max + 1;
1535  int64_t tmp_ts= read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
1536  if(tmp_ts == AV_NOPTS_VALUE)
1537  break;
1538  ts_max= tmp_ts;
1539  pos_max= tmp_pos;
1540  if(tmp_pos >= filesize)
1541  break;
1542  }
1543  pos_limit= pos_max;
1544  }
1545 
1546  if(ts_min > ts_max){
1547  return -1;
1548  }else if(ts_min == ts_max){
1549  pos_limit= pos_min;
1550  }
1551 
1552  no_change=0;
1553  while (pos_min < pos_limit) {
1554  av_dlog(s, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64" dts_max=%"PRId64"\n",
1555  pos_min, pos_max, ts_min, ts_max);
1556  assert(pos_limit <= pos_max);
1557 
1558  if(no_change==0){
1559  int64_t approximate_keyframe_distance= pos_max - pos_limit;
1560  // interpolate position (better than dichotomy)
1561  pos = av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
1562  + pos_min - approximate_keyframe_distance;
1563  }else if(no_change==1){
1564  // bisection, if interpolation failed to change min or max pos last time
1565  pos = (pos_min + pos_limit)>>1;
1566  }else{
1567  /* linear search if bisection failed, can only happen if there
1568  are very few or no keyframes between min/max */
1569  pos=pos_min;
1570  }
1571  if(pos <= pos_min)
1572  pos= pos_min + 1;
1573  else if(pos > pos_limit)
1574  pos= pos_limit;
1575  start_pos= pos;
1576 
1577  ts = read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1
1578  if(pos == pos_max)
1579  no_change++;
1580  else
1581  no_change=0;
1582  av_dlog(s, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64" target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
1583  pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts,
1584  pos_limit, start_pos, no_change);
1585  if(ts == AV_NOPTS_VALUE){
1586  av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
1587  return -1;
1588  }
1589  assert(ts != AV_NOPTS_VALUE);
1590  if (target_ts <= ts) {
1591  pos_limit = start_pos - 1;
1592  pos_max = pos;
1593  ts_max = ts;
1594  }
1595  if (target_ts >= ts) {
1596  pos_min = pos;
1597  ts_min = ts;
1598  }
1599  }
1600 
1601  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
1602  ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
1603  pos_min = pos;
1604  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1605  pos_min++;
1606  ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1607  av_dlog(s, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
1608  pos, ts_min, target_ts, ts_max);
1609  *ts_ret= ts;
1610  return pos;
1611 }
1612 
1613 static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
1614  int64_t pos_min, pos_max;
1615 
1616  pos_min = s->data_offset;
1617  pos_max = avio_size(s->pb) - 1;
1618 
1619  if (pos < pos_min) pos= pos_min;
1620  else if(pos > pos_max) pos= pos_max;
1621 
1622  avio_seek(s->pb, pos, SEEK_SET);
1623 
1624  return 0;
1625 }
1626 
1628  int stream_index, int64_t timestamp, int flags)
1629 {
1630  int index;
1631  int64_t ret;
1632  AVStream *st;
1633  AVIndexEntry *ie;
1634 
1635  st = s->streams[stream_index];
1636 
1637  index = av_index_search_timestamp(st, timestamp, flags);
1638 
1639  if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
1640  return -1;
1641 
1642  if(index < 0 || index==st->nb_index_entries-1){
1643  AVPacket pkt;
1644 
1645  if(st->nb_index_entries){
1646  assert(st->index_entries);
1647  ie= &st->index_entries[st->nb_index_entries-1];
1648  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1649  return ret;
1650  ff_update_cur_dts(s, st, ie->timestamp);
1651  }else{
1652  if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
1653  return ret;
1654  }
1655  for (;;) {
1656  int read_status;
1657  do{
1658  read_status = av_read_frame(s, &pkt);
1659  } while (read_status == AVERROR(EAGAIN));
1660  if (read_status < 0)
1661  break;
1662  av_free_packet(&pkt);
1663  if(stream_index == pkt.stream_index){
1664  if((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
1665  break;
1666  }
1667  }
1668  index = av_index_search_timestamp(st, timestamp, flags);
1669  }
1670  if (index < 0)
1671  return -1;
1672 
1674  if (s->iformat->read_seek){
1675  if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
1676  return 0;
1677  }
1678  ie = &st->index_entries[index];
1679  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1680  return ret;
1681  ff_update_cur_dts(s, st, ie->timestamp);
1682 
1683  return 0;
1684 }
1685 
1686 static int seek_frame_internal(AVFormatContext *s, int stream_index,
1687  int64_t timestamp, int flags)
1688 {
1689  int ret;
1690  AVStream *st;
1691 
1692  if (flags & AVSEEK_FLAG_BYTE) {
1693  if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
1694  return -1;
1696  return seek_frame_byte(s, stream_index, timestamp, flags);
1697  }
1698 
1699  if(stream_index < 0){
1700  stream_index= av_find_default_stream_index(s);
1701  if(stream_index < 0)
1702  return -1;
1703 
1704  st= s->streams[stream_index];
1705  /* timestamp for default must be expressed in AV_TIME_BASE units */
1706  timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
1707  }
1708 
1709  /* first, we try the format specific seek */
1710  if (s->iformat->read_seek) {
1712  ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
1713  } else
1714  ret = -1;
1715  if (ret >= 0) {
1716  return 0;
1717  }
1718 
1719  if (s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
1721  return ff_seek_frame_binary(s, stream_index, timestamp, flags);
1722  } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
1724  return seek_frame_generic(s, stream_index, timestamp, flags);
1725  }
1726  else
1727  return -1;
1728 }
1729 
1730 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
1731 {
1732  int ret = seek_frame_internal(s, stream_index, timestamp, flags);
1733 
1734  if (ret >= 0)
1736 
1737  return ret;
1738 }
1739 
1740 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
1741 {
1742  if(min_ts > ts || max_ts < ts)
1743  return -1;
1744 
1745  if (s->iformat->read_seek2) {
1746  int ret;
1748  ret = s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
1749 
1750  if (ret >= 0)
1752  return ret;
1753  }
1754 
1755  if(s->iformat->read_timestamp){
1756  //try to seek via read_timestamp()
1757  }
1758 
1759  //Fallback to old API if new is not implemented but old is
1760  //Note the old has somewat different sematics
1761  if(s->iformat->read_seek || 1)
1762  return av_seek_frame(s, stream_index, ts, flags | ((uint64_t)ts - min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0));
1763 
1764  // try some generic seek like seek_frame_generic() but with new ts semantics
1765 }
1766 
1767 /*******************************************************/
1768 
1775 {
1776  int i;
1777  AVStream *st;
1778 
1779  for(i = 0;i < ic->nb_streams; i++) {
1780  st = ic->streams[i];
1781  if (st->duration != AV_NOPTS_VALUE)
1782  return 1;
1783  }
1784  if (ic->duration != AV_NOPTS_VALUE)
1785  return 1;
1786  return 0;
1787 }
1788 
1795 {
1796  int64_t start_time, start_time1, end_time, end_time1;
1797  int64_t duration, duration1, filesize;
1798  int i;
1799  AVStream *st;
1800 
1801  start_time = INT64_MAX;
1802  end_time = INT64_MIN;
1803  duration = INT64_MIN;
1804  for(i = 0;i < ic->nb_streams; i++) {
1805  st = ic->streams[i];
1806  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1807  start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
1808  start_time = FFMIN(start_time, start_time1);
1809  if (st->duration != AV_NOPTS_VALUE) {
1810  end_time1 = start_time1
1812  end_time = FFMAX(end_time, end_time1);
1813  }
1814  }
1815  if (st->duration != AV_NOPTS_VALUE) {
1816  duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
1817  duration = FFMAX(duration, duration1);
1818  }
1819  }
1820  if (start_time != INT64_MAX) {
1821  ic->start_time = start_time;
1822  if (end_time != INT64_MIN)
1823  duration = FFMAX(duration, end_time - start_time);
1824  }
1825  if (duration != INT64_MIN) {
1826  ic->duration = duration;
1827  if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
1828  /* compute the bitrate */
1829  ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
1830  (double)ic->duration;
1831  }
1832  }
1833 }
1834 
1836 {
1837  int i;
1838  AVStream *st;
1839 
1841  for(i = 0;i < ic->nb_streams; i++) {
1842  st = ic->streams[i];
1843  if (st->start_time == AV_NOPTS_VALUE) {
1844  if(ic->start_time != AV_NOPTS_VALUE)
1846  if(ic->duration != AV_NOPTS_VALUE)
1848  }
1849  }
1850 }
1851 
1853 {
1854  int64_t filesize, duration;
1855  int bit_rate, i;
1856  AVStream *st;
1857 
1858  /* if bit_rate is already set, we believe it */
1859  if (ic->bit_rate <= 0) {
1860  bit_rate = 0;
1861  for(i=0;i<ic->nb_streams;i++) {
1862  st = ic->streams[i];
1863  if (st->codec->bit_rate > 0) {
1864  if (INT_MAX - st->codec->bit_rate < bit_rate) {
1865  bit_rate = 0;
1866  break;
1867  }
1868  bit_rate += st->codec->bit_rate;
1869  }
1870  }
1871  ic->bit_rate = bit_rate;
1872  }
1873 
1874  /* if duration is already set, we believe it */
1875  if (ic->duration == AV_NOPTS_VALUE &&
1876  ic->bit_rate != 0) {
1877  filesize = ic->pb ? avio_size(ic->pb) : 0;
1878  if (filesize > 0) {
1879  for(i = 0; i < ic->nb_streams; i++) {
1880  st = ic->streams[i];
1881  duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
1882  if (st->duration == AV_NOPTS_VALUE)
1883  st->duration = duration;
1884  }
1885  }
1886  }
1887 }
1888 
1889 #define DURATION_MAX_READ_SIZE 250000
1890 #define DURATION_MAX_RETRY 3
1891 
1892 /* only usable for MPEG-PS streams */
1893 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1894 {
1895  AVPacket pkt1, *pkt = &pkt1;
1896  AVStream *st;
1897  int read_size, i, ret;
1898  int64_t end_time;
1899  int64_t filesize, offset, duration;
1900  int retry=0;
1901 
1902  /* flush packet queue */
1903  flush_packet_queue(ic);
1904 
1905  for (i=0; i<ic->nb_streams; i++) {
1906  st = ic->streams[i];
1907  if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
1908  av_log(st->codec, AV_LOG_WARNING, "start time is not set in estimate_timings_from_pts\n");
1909 
1910  if (st->parser) {
1911  av_parser_close(st->parser);
1912  st->parser= NULL;
1913  }
1914  }
1915 
1916  /* estimate the end time (duration) */
1917  /* XXX: may need to support wrapping */
1918  filesize = ic->pb ? avio_size(ic->pb) : 0;
1919  end_time = AV_NOPTS_VALUE;
1920  do{
1921  offset = filesize - (DURATION_MAX_READ_SIZE<<retry);
1922  if (offset < 0)
1923  offset = 0;
1924 
1925  avio_seek(ic->pb, offset, SEEK_SET);
1926  read_size = 0;
1927  for(;;) {
1928  if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))
1929  break;
1930 
1931  do {
1932  ret = ff_read_packet(ic, pkt);
1933  } while(ret == AVERROR(EAGAIN));
1934  if (ret != 0)
1935  break;
1936  read_size += pkt->size;
1937  st = ic->streams[pkt->stream_index];
1938  if (pkt->pts != AV_NOPTS_VALUE &&
1939  (st->start_time != AV_NOPTS_VALUE ||
1940  st->first_dts != AV_NOPTS_VALUE)) {
1941  duration = end_time = pkt->pts;
1942  if (st->start_time != AV_NOPTS_VALUE)
1943  duration -= st->start_time;
1944  else
1945  duration -= st->first_dts;
1946  if (duration < 0)
1947  duration += 1LL<<st->pts_wrap_bits;
1948  if (duration > 0) {
1949  if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
1950  st->duration = duration;
1951  }
1952  }
1953  av_free_packet(pkt);
1954  }
1955  }while( end_time==AV_NOPTS_VALUE
1956  && filesize > (DURATION_MAX_READ_SIZE<<retry)
1957  && ++retry <= DURATION_MAX_RETRY);
1958 
1960 
1961  avio_seek(ic->pb, old_offset, SEEK_SET);
1962  for (i=0; i<ic->nb_streams; i++) {
1963  st= ic->streams[i];
1964  st->cur_dts= st->first_dts;
1967  }
1968 }
1969 
1970 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
1971 {
1972  int64_t file_size;
1973 
1974  /* get the file size, if possible */
1975  if (ic->iformat->flags & AVFMT_NOFILE) {
1976  file_size = 0;
1977  } else {
1978  file_size = avio_size(ic->pb);
1979  file_size = FFMAX(0, file_size);
1980  }
1981 
1982  if ((!strcmp(ic->iformat->name, "mpeg") ||
1983  !strcmp(ic->iformat->name, "mpegts")) &&
1984  file_size && ic->pb->seekable) {
1985  /* get accurate estimate from the PTSes */
1986  estimate_timings_from_pts(ic, old_offset);
1987  } else if (has_duration(ic)) {
1988  /* at least one component has timings - we use them for all
1989  the components */
1991  } else {
1992  av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
1993  /* less precise: use bitrate info */
1995  }
1997 
1998  {
1999  int i;
2000  AVStream av_unused *st;
2001  for(i = 0;i < ic->nb_streams; i++) {
2002  st = ic->streams[i];
2003  av_dlog(ic, "%d: start_time: %0.3f duration: %0.3f\n", i,
2004  (double) st->start_time / AV_TIME_BASE,
2005  (double) st->duration / AV_TIME_BASE);
2006  }
2007  av_dlog(ic, "stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
2008  (double) ic->start_time / AV_TIME_BASE,
2009  (double) ic->duration / AV_TIME_BASE,
2010  ic->bit_rate / 1000);
2011  }
2012 }
2013 
2015 {
2016  AVCodecContext *avctx = st->codec;
2017  int val;
2018  switch (avctx->codec_type) {
2019  case AVMEDIA_TYPE_AUDIO:
2020  val = avctx->sample_rate && avctx->channels;
2021  if (st->info->found_decoder >= 0 && avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
2022  return 0;
2023  break;
2024  case AVMEDIA_TYPE_VIDEO:
2025  val = avctx->width;
2026  if (st->info->found_decoder >= 0 && avctx->pix_fmt == AV_PIX_FMT_NONE)
2027  return 0;
2028  break;
2029  default:
2030  val = 1;
2031  break;
2032  }
2033  return avctx->codec_id != AV_CODEC_ID_NONE && val != 0;
2034 }
2035 
2037 {
2038  return st->codec->codec_id != AV_CODEC_ID_H264 ||
2039  st->info->nb_decoded_frames >= 6;
2040 }
2041 
2042 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2044 {
2045  const AVCodec *codec;
2046  int got_picture = 1, ret = 0;
2047  AVFrame *frame = avcodec_alloc_frame();
2048  AVPacket pkt = *avpkt;
2049 
2050  if (!frame)
2051  return AVERROR(ENOMEM);
2052 
2053  if (!avcodec_is_open(st->codec) && !st->info->found_decoder) {
2054  AVDictionary *thread_opt = NULL;
2055 
2056  codec = st->codec->codec ? st->codec->codec :
2058 
2059  if (!codec) {
2060  st->info->found_decoder = -1;
2061  ret = -1;
2062  goto fail;
2063  }
2064 
2065  /* force thread count to 1 since the h264 decoder will not extract SPS
2066  * and PPS to extradata during multi-threaded decoding */
2067  av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2068  ret = avcodec_open2(st->codec, codec, options ? options : &thread_opt);
2069  if (!options)
2070  av_dict_free(&thread_opt);
2071  if (ret < 0) {
2072  st->info->found_decoder = -1;
2073  goto fail;
2074  }
2075  st->info->found_decoder = 1;
2076  } else if (!st->info->found_decoder)
2077  st->info->found_decoder = 1;
2078 
2079  if (st->info->found_decoder < 0) {
2080  ret = -1;
2081  goto fail;
2082  }
2083 
2084  while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
2085  ret >= 0 &&
2086  (!has_codec_parameters(st) ||
2089  got_picture = 0;
2091  switch(st->codec->codec_type) {
2092  case AVMEDIA_TYPE_VIDEO:
2093  ret = avcodec_decode_video2(st->codec, frame,
2094  &got_picture, &pkt);
2095  break;
2096  case AVMEDIA_TYPE_AUDIO:
2097  ret = avcodec_decode_audio4(st->codec, frame, &got_picture, &pkt);
2098  break;
2099  default:
2100  break;
2101  }
2102  if (ret >= 0) {
2103  if (got_picture)
2104  st->info->nb_decoded_frames++;
2105  pkt.data += ret;
2106  pkt.size -= ret;
2107  ret = got_picture;
2108  }
2109  }
2110 
2111 fail:
2112  avcodec_free_frame(&frame);
2113  return ret;
2114 }
2115 
2116 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
2117 {
2118  while (tags->id != AV_CODEC_ID_NONE) {
2119  if (tags->id == id)
2120  return tags->tag;
2121  tags++;
2122  }
2123  return 0;
2124 }
2125 
2126 enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
2127 {
2128  int i;
2129  for(i=0; tags[i].id != AV_CODEC_ID_NONE;i++) {
2130  if(tag == tags[i].tag)
2131  return tags[i].id;
2132  }
2133  for(i=0; tags[i].id != AV_CODEC_ID_NONE; i++) {
2134  if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
2135  return tags[i].id;
2136  }
2137  return AV_CODEC_ID_NONE;
2138 }
2139 
2140 enum AVCodecID ff_get_pcm_codec_id(int bps, int flt, int be, int sflags)
2141 {
2142  if (flt) {
2143  switch (bps) {
2144  case 32: return be ? AV_CODEC_ID_PCM_F32BE : AV_CODEC_ID_PCM_F32LE;
2145  case 64: return be ? AV_CODEC_ID_PCM_F64BE : AV_CODEC_ID_PCM_F64LE;
2146  default: return AV_CODEC_ID_NONE;
2147  }
2148  } else {
2149  bps >>= 3;
2150  if (sflags & (1 << (bps - 1))) {
2151  switch (bps) {
2152  case 1: return AV_CODEC_ID_PCM_S8;
2153  case 2: return be ? AV_CODEC_ID_PCM_S16BE : AV_CODEC_ID_PCM_S16LE;
2154  case 3: return be ? AV_CODEC_ID_PCM_S24BE : AV_CODEC_ID_PCM_S24LE;
2155  case 4: return be ? AV_CODEC_ID_PCM_S32BE : AV_CODEC_ID_PCM_S32LE;
2156  default: return AV_CODEC_ID_NONE;
2157  }
2158  } else {
2159  switch (bps) {
2160  case 1: return AV_CODEC_ID_PCM_U8;
2161  case 2: return be ? AV_CODEC_ID_PCM_U16BE : AV_CODEC_ID_PCM_U16LE;
2162  case 3: return be ? AV_CODEC_ID_PCM_U24BE : AV_CODEC_ID_PCM_U24LE;
2163  case 4: return be ? AV_CODEC_ID_PCM_U32BE : AV_CODEC_ID_PCM_U32LE;
2164  default: return AV_CODEC_ID_NONE;
2165  }
2166  }
2167  }
2168 }
2169 
2170 unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum AVCodecID id)
2171 {
2172  int i;
2173  for(i=0; tags && tags[i]; i++){
2174  int tag= ff_codec_get_tag(tags[i], id);
2175  if(tag) return tag;
2176  }
2177  return 0;
2178 }
2179 
2180 enum AVCodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
2181 {
2182  int i;
2183  for(i=0; tags && tags[i]; i++){
2184  enum AVCodecID id= ff_codec_get_id(tags[i], tag);
2185  if(id!=AV_CODEC_ID_NONE) return id;
2186  }
2187  return AV_CODEC_ID_NONE;
2188 }
2189 
2191 {
2192  unsigned int i, j;
2193  int64_t max_time = s->duration + ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2194 
2195  for (i = 0; i < s->nb_chapters; i++)
2196  if (s->chapters[i]->end == AV_NOPTS_VALUE) {
2197  AVChapter *ch = s->chapters[i];
2198  int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, ch->time_base)
2199  : INT64_MAX;
2200 
2201  for (j = 0; j < s->nb_chapters; j++) {
2202  AVChapter *ch1 = s->chapters[j];
2203  int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, ch->time_base);
2204  if (j != i && next_start > ch->start && next_start < end)
2205  end = next_start;
2206  }
2207  ch->end = (end == INT64_MAX) ? ch->start : end;
2208  }
2209 }
2210 
2211 static int get_std_framerate(int i){
2212  if(i<60*12) return i*1001;
2213  else return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
2214 }
2215 
2216 /*
2217  * Is the time base unreliable.
2218  * This is a heuristic to balance between quick acceptance of the values in
2219  * the headers vs. some extra checks.
2220  * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2221  * MPEG-2 commonly misuses field repeat flags to store different framerates.
2222  * And there are "variable" fps files this needs to detect as well.
2223  */
2225  if( c->time_base.den >= 101L*c->time_base.num
2226  || c->time_base.den < 5L*c->time_base.num
2227 /* || c->codec_tag == AV_RL32("DIVX")
2228  || c->codec_tag == AV_RL32("XVID")*/
2230  || c->codec_id == AV_CODEC_ID_H264
2231  )
2232  return 1;
2233  return 0;
2234 }
2235 
2237 {
2238  int i, count, ret, read_size, j;
2239  AVStream *st;
2240  AVPacket pkt1, *pkt;
2241  int64_t old_offset = avio_tell(ic->pb);
2242  int orig_nb_streams = ic->nb_streams; // new streams might appear, no options for those
2243 
2244  for(i=0;i<ic->nb_streams;i++) {
2245  const AVCodec *codec;
2246  AVDictionary *thread_opt = NULL;
2247  st = ic->streams[i];
2248 
2249  //only for the split stuff
2250  if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
2251  st->parser = av_parser_init(st->codec->codec_id);
2252  if(st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser){
2254  }
2255  }
2256  codec = st->codec->codec ? st->codec->codec :
2258 
2259  /* force thread count to 1 since the h264 decoder will not extract SPS
2260  * and PPS to extradata during multi-threaded decoding */
2261  av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2262 
2263  /* Ensure that subtitle_header is properly set. */
2265  && codec && !st->codec->codec)
2266  avcodec_open2(st->codec, codec, options ? &options[i]
2267  : &thread_opt);
2268 
2269  //try to just open decoders, in case this is enough to get parameters
2270  if (!has_codec_parameters(st)) {
2271  if (codec && !st->codec->codec)
2272  avcodec_open2(st->codec, codec, options ? &options[i]
2273  : &thread_opt);
2274  }
2275  if (!options)
2276  av_dict_free(&thread_opt);
2277  }
2278 
2279  for (i=0; i<ic->nb_streams; i++) {
2280 #if FF_API_R_FRAME_RATE
2281  ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
2282 #endif
2285  }
2286 
2287  count = 0;
2288  read_size = 0;
2289  for(;;) {
2291  ret= AVERROR_EXIT;
2292  av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2293  break;
2294  }
2295 
2296  /* check if one codec still needs to be handled */
2297  for(i=0;i<ic->nb_streams;i++) {
2298  int fps_analyze_framecount = 20;
2299 
2300  st = ic->streams[i];
2301  if (!has_codec_parameters(st))
2302  break;
2303  /* if the timebase is coarse (like the usual millisecond precision
2304  of mkv), we need to analyze more frames to reliably arrive at
2305  the correct fps */
2306  if (av_q2d(st->time_base) > 0.0005)
2307  fps_analyze_framecount *= 2;
2308  if (ic->fps_probe_size >= 0)
2309  fps_analyze_framecount = ic->fps_probe_size;
2310  /* variable fps and no guess at the real fps */
2311  if( tb_unreliable(st->codec) && !st->avg_frame_rate.num
2312  && st->codec_info_nb_frames < fps_analyze_framecount
2313  && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2314  break;
2315  if(st->parser && st->parser->parser->split && !st->codec->extradata)
2316  break;
2317  if (st->first_dts == AV_NOPTS_VALUE &&
2318  (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2320  break;
2321  }
2322  if (i == ic->nb_streams) {
2323  /* NOTE: if the format has no header, then we need to read
2324  some packets to get most of the streams, so we cannot
2325  stop here */
2326  if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2327  /* if we found the info for all the codecs, we can stop */
2328  ret = count;
2329  av_log(ic, AV_LOG_DEBUG, "All info found\n");
2330  break;
2331  }
2332  }
2333  /* we did not get all the codec info, but we read too much data */
2334  if (read_size >= ic->probesize) {
2335  ret = count;
2336  av_log(ic, AV_LOG_DEBUG, "Probe buffer size limit %d reached\n", ic->probesize);
2337  break;
2338  }
2339 
2340  /* NOTE: a new stream can be added there if no header in file
2341  (AVFMTCTX_NOHEADER) */
2342  ret = read_frame_internal(ic, &pkt1);
2343  if (ret == AVERROR(EAGAIN))
2344  continue;
2345 
2346  if (ret < 0) {
2347  /* EOF or error*/
2348  AVPacket empty_pkt = { 0 };
2349  int err = 0;
2350  av_init_packet(&empty_pkt);
2351 
2352  ret = -1; /* we could not have all the codec parameters before EOF */
2353  for(i=0;i<ic->nb_streams;i++) {
2354  st = ic->streams[i];
2355 
2356  /* flush the decoders */
2357  if (st->info->found_decoder == 1) {
2358  do {
2359  err = try_decode_frame(st, &empty_pkt,
2360  (options && i < orig_nb_streams) ?
2361  &options[i] : NULL);
2362  } while (err > 0 && !has_codec_parameters(st));
2363  }
2364 
2365  if (err < 0) {
2366  av_log(ic, AV_LOG_WARNING,
2367  "decoding for stream %d failed\n", st->index);
2368  } else if (!has_codec_parameters(st)) {
2369  char buf[256];
2370  avcodec_string(buf, sizeof(buf), st->codec, 0);
2371  av_log(ic, AV_LOG_WARNING,
2372  "Could not find codec parameters (%s)\n", buf);
2373  } else {
2374  ret = 0;
2375  }
2376  }
2377  break;
2378  }
2379 
2380  if (ic->flags & AVFMT_FLAG_NOBUFFER) {
2381  pkt = &pkt1;
2382  } else {
2383  pkt = add_to_pktbuf(&ic->packet_buffer, &pkt1,
2384  &ic->packet_buffer_end);
2385  if ((ret = av_dup_packet(pkt)) < 0)
2386  goto find_stream_info_err;
2387  }
2388 
2389  read_size += pkt->size;
2390 
2391  st = ic->streams[pkt->stream_index];
2392  if (pkt->dts != AV_NOPTS_VALUE && st->codec_info_nb_frames > 1) {
2393  /* check for non-increasing dts */
2394  if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2395  st->info->fps_last_dts >= pkt->dts) {
2396  av_log(ic, AV_LOG_WARNING, "Non-increasing DTS in stream %d: "
2397  "packet %d with DTS %"PRId64", packet %d with DTS "
2398  "%"PRId64"\n", st->index, st->info->fps_last_dts_idx,
2399  st->info->fps_last_dts, st->codec_info_nb_frames, pkt->dts);
2401  }
2402  /* check for a discontinuity in dts - if the difference in dts
2403  * is more than 1000 times the average packet duration in the sequence,
2404  * we treat it as a discontinuity */
2405  if (st->info->fps_last_dts != AV_NOPTS_VALUE &&
2407  (pkt->dts - st->info->fps_last_dts) / 1000 >
2409  av_log(ic, AV_LOG_WARNING, "DTS discontinuity in stream %d: "
2410  "packet %d with DTS %"PRId64", packet %d with DTS "
2411  "%"PRId64"\n", st->index, st->info->fps_last_dts_idx,
2412  st->info->fps_last_dts, st->codec_info_nb_frames, pkt->dts);
2414  }
2415 
2416  /* update stored dts values */
2417  if (st->info->fps_first_dts == AV_NOPTS_VALUE) {
2418  st->info->fps_first_dts = pkt->dts;
2420  }
2421  st->info->fps_last_dts = pkt->dts;
2423 
2424  /* check max_analyze_duration */
2425  if (av_rescale_q(pkt->dts - st->info->fps_first_dts, st->time_base,
2427  av_log(ic, AV_LOG_WARNING, "max_analyze_duration reached\n");
2428  break;
2429  }
2430  }
2431 #if FF_API_R_FRAME_RATE
2432  {
2433  int64_t last = st->info->last_dts;
2434 
2435  if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && pkt->dts > last){
2436  int64_t duration= pkt->dts - last;
2437  double dur= duration * av_q2d(st->time_base);
2438 
2439  if (st->info->duration_count < 2)
2440  memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
2441  for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
2442  int framerate= get_std_framerate(i);
2443  int ticks= lrintf(dur*framerate/(1001*12));
2444  double error = dur - (double)ticks*1001*12 / framerate;
2445  st->info->duration_error[i] += error*error;
2446  }
2447  st->info->duration_count++;
2448  // ignore the first 4 values, they might have some random jitter
2449  if (st->info->duration_count > 3)
2450  st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
2451  }
2452  if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
2453  st->info->last_dts = pkt->dts;
2454  }
2455 #endif
2456  if(st->parser && st->parser->parser->split && !st->codec->extradata){
2457  int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
2458  if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
2459  st->codec->extradata_size= i;
2461  if (!st->codec->extradata)
2462  return AVERROR(ENOMEM);
2463  memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size);
2464  memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2465  }
2466  }
2467 
2468  /* if still no information, we try to open the codec and to
2469  decompress the frame. We try to avoid that in most cases as
2470  it takes longer and uses more memory. For MPEG-4, we need to
2471  decompress for QuickTime.
2472 
2473  If CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2474  least one frame of codec data, this makes sure the codec initializes
2475  the channel configuration and does not only trust the values from the container.
2476  */
2477  try_decode_frame(st, pkt, (options && i < orig_nb_streams ) ? &options[i] : NULL);
2478 
2479  st->codec_info_nb_frames++;
2480  count++;
2481  }
2482 
2483  // close codecs which were opened in try_decode_frame()
2484  for(i=0;i<ic->nb_streams;i++) {
2485  st = ic->streams[i];
2486  avcodec_close(st->codec);
2487  }
2488  for(i=0;i<ic->nb_streams;i++) {
2489  st = ic->streams[i];
2490  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2491  /* estimate average framerate if not set by demuxer */
2492  if (!st->avg_frame_rate.num && st->info->fps_last_dts != st->info->fps_first_dts) {
2493  int64_t delta_dts = st->info->fps_last_dts - st->info->fps_first_dts;
2494  int delta_packets = st->info->fps_last_dts_idx - st->info->fps_first_dts_idx;
2495  int best_fps = 0;
2496  double best_error = 0.01;
2497 
2498  if (delta_dts >= INT64_MAX / st->time_base.num ||
2499  delta_packets >= INT64_MAX / st->time_base.den ||
2500  delta_dts < 0)
2501  continue;
2503  delta_packets*(int64_t)st->time_base.den,
2504  delta_dts*(int64_t)st->time_base.num, 60000);
2505 
2506  /* round guessed framerate to a "standard" framerate if it's
2507  * within 1% of the original estimate*/
2508  for (j = 1; j < MAX_STD_TIMEBASES; j++) {
2509  AVRational std_fps = { get_std_framerate(j), 12*1001 };
2510  double error = fabs(av_q2d(st->avg_frame_rate) / av_q2d(std_fps) - 1);
2511 
2512  if (error < best_error) {
2513  best_error = error;
2514  best_fps = std_fps.num;
2515  }
2516  }
2517  if (best_fps) {
2519  best_fps, 12*1001, INT_MAX);
2520  }
2521  }
2522 #if FF_API_R_FRAME_RATE
2523  // the check for tb_unreliable() is not completely correct, since this is not about handling
2524  // a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2525  // ipmovie.c produces.
2526  if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > 1 && !st->r_frame_rate.num)
2527  av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, st->time_base.den, st->time_base.num * st->info->duration_gcd, INT_MAX);
2528  if (st->info->duration_count && !st->r_frame_rate.num
2529  && tb_unreliable(st->codec)) {
2530  int num = 0;
2531  double best_error= 2*av_q2d(st->time_base);
2532  best_error = best_error*best_error*st->info->duration_count*1000*12*30;
2533 
2534  for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
2535  double error = st->info->duration_error[j] * get_std_framerate(j);
2536  if(error < best_error){
2537  best_error= error;
2538  num = get_std_framerate(j);
2539  }
2540  }
2541  // do not increase frame rate by more than 1 % in order to match a standard rate.
2542  if (num && (!st->r_frame_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(st->r_frame_rate)))
2543  av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2544  }
2545 #endif
2546  }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2547  if(!st->codec->bits_per_coded_sample)
2549  // set stream disposition based on audio service type
2550  switch (st->codec->audio_service_type) {
2558  st->disposition = AV_DISPOSITION_COMMENT; break;
2560  st->disposition = AV_DISPOSITION_KARAOKE; break;
2561  }
2562  }
2563  }
2564 
2565  estimate_timings(ic, old_offset);
2566 
2568 
2569  find_stream_info_err:
2570  for (i=0; i < ic->nb_streams; i++) {
2571  if (ic->streams[i]->codec)
2572  ic->streams[i]->codec->thread_count = 0;
2573  av_freep(&ic->streams[i]->info);
2574  }
2575  return ret;
2576 }
2577 
2579 {
2580  int i, j;
2581 
2582  for (i = 0; i < ic->nb_programs; i++)
2583  for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2584  if (ic->programs[i]->stream_index[j] == s)
2585  return ic->programs[i];
2586  return NULL;
2587 }
2588 
2590  enum AVMediaType type,
2591  int wanted_stream_nb,
2592  int related_stream,
2593  AVCodec **decoder_ret,
2594  int flags)
2595 {
2596  int i, nb_streams = ic->nb_streams;
2597  int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2598  unsigned *program = NULL;
2599  AVCodec *decoder = NULL, *best_decoder = NULL;
2600 
2601  if (related_stream >= 0 && wanted_stream_nb < 0) {
2602  AVProgram *p = find_program_from_stream(ic, related_stream);
2603  if (p) {
2604  program = p->stream_index;
2605  nb_streams = p->nb_stream_indexes;
2606  }
2607  }
2608  for (i = 0; i < nb_streams; i++) {
2609  int real_stream_index = program ? program[i] : i;
2610  AVStream *st = ic->streams[real_stream_index];
2611  AVCodecContext *avctx = st->codec;
2612  if (avctx->codec_type != type)
2613  continue;
2614  if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
2615  continue;
2617  continue;
2618  if (decoder_ret) {
2619  decoder = avcodec_find_decoder(st->codec->codec_id);
2620  if (!decoder) {
2621  if (ret < 0)
2623  continue;
2624  }
2625  }
2626  if (best_count >= st->codec_info_nb_frames)
2627  continue;
2628  best_count = st->codec_info_nb_frames;
2629  ret = real_stream_index;
2630  best_decoder = decoder;
2631  if (program && i == nb_streams - 1 && ret < 0) {
2632  program = NULL;
2633  nb_streams = ic->nb_streams;
2634  i = 0; /* no related stream found, try again with everything */
2635  }
2636  }
2637  if (decoder_ret)
2638  *decoder_ret = best_decoder;
2639  return ret;
2640 }
2641 
2642 /*******************************************************/
2643 
2645 {
2646  if (s->iformat->read_play)
2647  return s->iformat->read_play(s);
2648  if (s->pb)
2649  return avio_pause(s->pb, 0);
2650  return AVERROR(ENOSYS);
2651 }
2652 
2654 {
2655  if (s->iformat->read_pause)
2656  return s->iformat->read_pause(s);
2657  if (s->pb)
2658  return avio_pause(s->pb, 1);
2659  return AVERROR(ENOSYS);
2660 }
2661 
2663 {
2664  int i;
2665  AVStream *st;
2666 
2667  av_opt_free(s);
2668  if (s->iformat && s->iformat->priv_class && s->priv_data)
2669  av_opt_free(s->priv_data);
2670 
2671  for(i=0;i<s->nb_streams;i++) {
2672  /* free all data in a stream component */
2673  st = s->streams[i];
2674  if (st->parser) {
2675  av_parser_close(st->parser);
2676  }
2677  if (st->attached_pic.data)
2679  av_dict_free(&st->metadata);
2680  av_freep(&st->probe_data.buf);
2681  av_free(st->index_entries);
2682  av_free(st->codec->extradata);
2684  av_free(st->codec);
2685  av_free(st->priv_data);
2686  av_free(st->info);
2687  av_free(st);
2688  }
2689  for(i=s->nb_programs-1; i>=0; i--) {
2690  av_dict_free(&s->programs[i]->metadata);
2691  av_freep(&s->programs[i]->stream_index);
2692  av_freep(&s->programs[i]);
2693  }
2694  av_freep(&s->programs);
2695  av_freep(&s->priv_data);
2696  while(s->nb_chapters--) {
2698  av_free(s->chapters[s->nb_chapters]);
2699  }
2700  av_freep(&s->chapters);
2701  av_dict_free(&s->metadata);
2702  av_freep(&s->streams);
2703  av_free(s);
2704 }
2705 
2706 #if FF_API_CLOSE_INPUT_FILE
2707 void av_close_input_file(AVFormatContext *s)
2708 {
2710 }
2711 #endif
2712 
2714 {
2715  AVFormatContext *s = *ps;
2716  AVIOContext *pb = s->pb;
2717 
2718  if ((s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
2719  (s->flags & AVFMT_FLAG_CUSTOM_IO))
2720  pb = NULL;
2721 
2722  flush_packet_queue(s);
2723 
2724  if (s->iformat) {
2725  if (s->iformat->read_close)
2726  s->iformat->read_close(s);
2727  }
2728 
2730 
2731  *ps = NULL;
2732 
2733  avio_close(pb);
2734 }
2735 
2737 {
2738  AVStream *st;
2739  int i;
2740  AVStream **streams;
2741 
2742  if (s->nb_streams >= INT_MAX/sizeof(*streams))
2743  return NULL;
2744  streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));
2745  if (!streams)
2746  return NULL;
2747  s->streams = streams;
2748 
2749  st = av_mallocz(sizeof(AVStream));
2750  if (!st)
2751  return NULL;
2752  if (!(st->info = av_mallocz(sizeof(*st->info)))) {
2753  av_free(st);
2754  return NULL;
2755  }
2756 
2757  st->codec = avcodec_alloc_context3(c);
2758  if (s->iformat) {
2759  /* no default bitrate if decoding */
2760  st->codec->bit_rate = 0;
2761  }
2762  st->index = s->nb_streams;
2763  st->start_time = AV_NOPTS_VALUE;
2764  st->duration = AV_NOPTS_VALUE;
2765  /* we set the current DTS to 0 so that formats without any timestamps
2766  but durations get some timestamps, formats with some unknown
2767  timestamps have their first few packets buffered and the
2768  timestamps corrected before they are returned to the user */
2769  st->cur_dts = 0;
2770  st->first_dts = AV_NOPTS_VALUE;
2772 
2773  /* default pts setting is MPEG-like */
2774  avpriv_set_pts_info(st, 33, 1, 90000);
2776  for(i=0; i<MAX_REORDER_DELAY+1; i++)
2777  st->pts_buffer[i]= AV_NOPTS_VALUE;
2779 
2780  st->sample_aspect_ratio = (AVRational){0,1};
2781 
2782 #if FF_API_R_FRAME_RATE
2783  st->info->last_dts = AV_NOPTS_VALUE;
2784 #endif
2787 
2788  s->streams[s->nb_streams++] = st;
2789  return st;
2790 }
2791 
2793 {
2794  AVProgram *program=NULL;
2795  int i;
2796 
2797  av_dlog(ac, "new_program: id=0x%04x\n", id);
2798 
2799  for(i=0; i<ac->nb_programs; i++)
2800  if(ac->programs[i]->id == id)
2801  program = ac->programs[i];
2802 
2803  if(!program){
2804  program = av_mallocz(sizeof(AVProgram));
2805  if (!program)
2806  return NULL;
2807  dynarray_add(&ac->programs, &ac->nb_programs, program);
2808  program->discard = AVDISCARD_NONE;
2809  }
2810  program->id = id;
2811 
2812  return program;
2813 }
2814 
2815 AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
2816 {
2817  AVChapter *chapter = NULL;
2818  int i;
2819 
2820  for(i=0; i<s->nb_chapters; i++)
2821  if(s->chapters[i]->id == id)
2822  chapter = s->chapters[i];
2823 
2824  if(!chapter){
2825  chapter= av_mallocz(sizeof(AVChapter));
2826  if(!chapter)
2827  return NULL;
2828  dynarray_add(&s->chapters, &s->nb_chapters, chapter);
2829  }
2830  av_dict_set(&chapter->metadata, "title", title, 0);
2831  chapter->id = id;
2832  chapter->time_base= time_base;
2833  chapter->start = start;
2834  chapter->end = end;
2835 
2836  return chapter;
2837 }
2838 
2839 void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
2840 {
2841  int i, j;
2842  AVProgram *program=NULL;
2843  void *tmp;
2844 
2845  if (idx >= ac->nb_streams) {
2846  av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
2847  return;
2848  }
2849 
2850  for(i=0; i<ac->nb_programs; i++){
2851  if(ac->programs[i]->id != progid)
2852  continue;
2853  program = ac->programs[i];
2854  for(j=0; j<program->nb_stream_indexes; j++)
2855  if(program->stream_index[j] == idx)
2856  return;
2857 
2858  tmp = av_realloc(program->stream_index, sizeof(unsigned int)*(program->nb_stream_indexes+1));
2859  if(!tmp)
2860  return;
2861  program->stream_index = tmp;
2862  program->stream_index[program->nb_stream_indexes++] = idx;
2863  return;
2864  }
2865 }
2866 
2867 static void print_fps(double d, const char *postfix){
2868  uint64_t v= lrintf(d*100);
2869  if (v% 100 ) av_log(NULL, AV_LOG_INFO, ", %3.2f %s", d, postfix);
2870  else if(v%(100*1000)) av_log(NULL, AV_LOG_INFO, ", %1.0f %s", d, postfix);
2871  else av_log(NULL, AV_LOG_INFO, ", %1.0fk %s", d/1000, postfix);
2872 }
2873 
2874 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
2875 {
2876  if(m && !(av_dict_count(m) == 1 && av_dict_get(m, "language", NULL, 0))){
2878 
2879  av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
2880  while((tag=av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX))) {
2881  if(strcmp("language", tag->key))
2882  av_log(ctx, AV_LOG_INFO, "%s %-16s: %s\n", indent, tag->key, tag->value);
2883  }
2884  }
2885 }
2886 
2887 /* "user interface" functions */
2888 static void dump_stream_format(AVFormatContext *ic, int i, int index, int is_output)
2889 {
2890  char buf[256];
2891  int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
2892  AVStream *st = ic->streams[i];
2893  int g = av_gcd(st->time_base.num, st->time_base.den);
2894  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
2895  avcodec_string(buf, sizeof(buf), st->codec, is_output);
2896  av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i);
2897  /* the pid is an important information, so we display it */
2898  /* XXX: add a generic system */
2899  if (flags & AVFMT_SHOW_IDS)
2900  av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
2901  if (lang)
2902  av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
2903  av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames, st->time_base.num/g, st->time_base.den/g);
2904  av_log(NULL, AV_LOG_INFO, ": %s", buf);
2905  if (st->sample_aspect_ratio.num && // default
2907  AVRational display_aspect_ratio;
2908  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
2911  1024*1024);
2912  av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
2914  display_aspect_ratio.num, display_aspect_ratio.den);
2915  }
2916  if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
2917  if(st->avg_frame_rate.den && st->avg_frame_rate.num)
2918  print_fps(av_q2d(st->avg_frame_rate), "fps");
2919 #if FF_API_R_FRAME_RATE
2920  if(st->r_frame_rate.den && st->r_frame_rate.num)
2921  print_fps(av_q2d(st->r_frame_rate), "tbr");
2922 #endif
2923  if(st->time_base.den && st->time_base.num)
2924  print_fps(1/av_q2d(st->time_base), "tbn");
2925  if(st->codec->time_base.den && st->codec->time_base.num)
2926  print_fps(1/av_q2d(st->codec->time_base), "tbc");
2927  }
2929  av_log(NULL, AV_LOG_INFO, " (default)");
2930  if (st->disposition & AV_DISPOSITION_DUB)
2931  av_log(NULL, AV_LOG_INFO, " (dub)");
2933  av_log(NULL, AV_LOG_INFO, " (original)");
2935  av_log(NULL, AV_LOG_INFO, " (comment)");
2937  av_log(NULL, AV_LOG_INFO, " (lyrics)");
2939  av_log(NULL, AV_LOG_INFO, " (karaoke)");
2941  av_log(NULL, AV_LOG_INFO, " (forced)");
2943  av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
2945  av_log(NULL, AV_LOG_INFO, " (visual impaired)");
2947  av_log(NULL, AV_LOG_INFO, " (clean effects)");
2948  av_log(NULL, AV_LOG_INFO, "\n");
2949  dump_metadata(NULL, st->metadata, " ");
2950 }
2951 
2953  int index,
2954  const char *url,
2955  int is_output)
2956 {
2957  int i;
2958  uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
2959  if (ic->nb_streams && !printed)
2960  return;
2961 
2962  av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
2963  is_output ? "Output" : "Input",
2964  index,
2965  is_output ? ic->oformat->name : ic->iformat->name,
2966  is_output ? "to" : "from", url);
2967  dump_metadata(NULL, ic->metadata, " ");
2968  if (!is_output) {
2969  av_log(NULL, AV_LOG_INFO, " Duration: ");
2970  if (ic->duration != AV_NOPTS_VALUE) {
2971  int hours, mins, secs, us;
2972  secs = ic->duration / AV_TIME_BASE;
2973  us = ic->duration % AV_TIME_BASE;
2974  mins = secs / 60;
2975  secs %= 60;
2976  hours = mins / 60;
2977  mins %= 60;
2978  av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
2979  (100 * us) / AV_TIME_BASE);
2980  } else {
2981  av_log(NULL, AV_LOG_INFO, "N/A");
2982  }
2983  if (ic->start_time != AV_NOPTS_VALUE) {
2984  int secs, us;
2985  av_log(NULL, AV_LOG_INFO, ", start: ");
2986  secs = ic->start_time / AV_TIME_BASE;
2987  us = abs(ic->start_time % AV_TIME_BASE);
2988  av_log(NULL, AV_LOG_INFO, "%d.%06d",
2989  secs, (int)av_rescale(us, 1000000, AV_TIME_BASE));
2990  }
2991  av_log(NULL, AV_LOG_INFO, ", bitrate: ");
2992  if (ic->bit_rate) {
2993  av_log(NULL, AV_LOG_INFO,"%d kb/s", ic->bit_rate / 1000);
2994  } else {
2995  av_log(NULL, AV_LOG_INFO, "N/A");
2996  }
2997  av_log(NULL, AV_LOG_INFO, "\n");
2998  }
2999  for (i = 0; i < ic->nb_chapters; i++) {
3000  AVChapter *ch = ic->chapters[i];
3001  av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
3002  av_log(NULL, AV_LOG_INFO, "start %f, ", ch->start * av_q2d(ch->time_base));
3003  av_log(NULL, AV_LOG_INFO, "end %f\n", ch->end * av_q2d(ch->time_base));
3004 
3005  dump_metadata(NULL, ch->metadata, " ");
3006  }
3007  if(ic->nb_programs) {
3008  int j, k, total = 0;
3009  for(j=0; j<ic->nb_programs; j++) {
3011  "name", NULL, 0);
3012  av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
3013  name ? name->value : "");
3014  dump_metadata(NULL, ic->programs[j]->metadata, " ");
3015  for(k=0; k<ic->programs[j]->nb_stream_indexes; k++) {
3016  dump_stream_format(ic, ic->programs[j]->stream_index[k], index, is_output);
3017  printed[ic->programs[j]->stream_index[k]] = 1;
3018  }
3019  total += ic->programs[j]->nb_stream_indexes;
3020  }
3021  if (total < ic->nb_streams)
3022  av_log(NULL, AV_LOG_INFO, " No Program\n");
3023  }
3024  for(i=0;i<ic->nb_streams;i++)
3025  if (!printed[i])
3026  dump_stream_format(ic, i, index, is_output);
3027 
3028  av_free(printed);
3029 }
3030 
3031 #if FF_API_AV_GETTIME && CONFIG_SHARED && HAVE_SYMVER
3032 FF_SYMVER(int64_t, av_gettime, (void), "LIBAVFORMAT_54")
3033 {
3034  return av_gettime();
3035 }
3036 #endif
3037 
3038 uint64_t ff_ntp_time(void)
3039 {
3040  return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
3041 }
3042 
3043 int av_get_frame_filename(char *buf, int buf_size,
3044  const char *path, int number)
3045 {
3046  const char *p;
3047  char *q, buf1[20], c;
3048  int nd, len, percentd_found;
3049 
3050  q = buf;
3051  p = path;
3052  percentd_found = 0;
3053  for(;;) {
3054  c = *p++;
3055  if (c == '\0')
3056  break;
3057  if (c == '%') {
3058  do {
3059  nd = 0;
3060  while (isdigit(*p)) {
3061  nd = nd * 10 + *p++ - '0';
3062  }
3063  c = *p++;
3064  } while (isdigit(c));
3065 
3066  switch(c) {
3067  case '%':
3068  goto addchar;
3069  case 'd':
3070  if (percentd_found)
3071  goto fail;
3072  percentd_found = 1;
3073  snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
3074  len = strlen(buf1);
3075  if ((q - buf + len) > buf_size - 1)
3076  goto fail;
3077  memcpy(q, buf1, len);
3078  q += len;
3079  break;
3080  default:
3081  goto fail;
3082  }
3083  } else {
3084  addchar:
3085  if ((q - buf) < buf_size - 1)
3086  *q++ = c;
3087  }
3088  }
3089  if (!percentd_found)
3090  goto fail;
3091  *q = '\0';
3092  return 0;
3093  fail:
3094  *q = '\0';
3095  return -1;
3096 }
3097 
3098 static void hex_dump_internal(void *avcl, FILE *f, int level,
3099  const uint8_t *buf, int size)
3100 {
3101  int len, i, j, c;
3102 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3103 
3104  for(i=0;i<size;i+=16) {
3105  len = size - i;
3106  if (len > 16)
3107  len = 16;
3108  PRINT("%08x ", i);
3109  for(j=0;j<16;j++) {
3110  if (j < len)
3111  PRINT(" %02x", buf[i+j]);
3112  else
3113  PRINT(" ");
3114  }
3115  PRINT(" ");
3116  for(j=0;j<len;j++) {
3117  c = buf[i+j];
3118  if (c < ' ' || c > '~')
3119  c = '.';
3120  PRINT("%c", c);
3121  }
3122  PRINT("\n");
3123  }
3124 #undef PRINT
3125 }
3126 
3127 void av_hex_dump(FILE *f, const uint8_t *buf, int size)
3128 {
3129  hex_dump_internal(NULL, f, 0, buf, size);
3130 }
3131 
3132 void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
3133 {
3134  hex_dump_internal(avcl, NULL, level, buf, size);
3135 }
3136 
3137 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload, AVRational time_base)
3138 {
3139 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3140  PRINT("stream #%d:\n", pkt->stream_index);
3141  PRINT(" keyframe=%d\n", ((pkt->flags & AV_PKT_FLAG_KEY) != 0));
3142  PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
3143  /* DTS is _always_ valid after av_read_frame() */
3144  PRINT(" dts=");
3145  if (pkt->dts == AV_NOPTS_VALUE)
3146  PRINT("N/A");
3147  else
3148  PRINT("%0.3f", pkt->dts * av_q2d(time_base));
3149  /* PTS may not be known if B-frames are present. */
3150  PRINT(" pts=");
3151  if (pkt->pts == AV_NOPTS_VALUE)
3152  PRINT("N/A");
3153  else
3154  PRINT("%0.3f", pkt->pts * av_q2d(time_base));
3155  PRINT("\n");
3156  PRINT(" size=%d\n", pkt->size);
3157 #undef PRINT
3158  if (dump_payload)
3159  av_hex_dump(f, pkt->data, pkt->size);
3160 }
3161 
3162 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
3163 {
3164  pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
3165 }
3166 
3167 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
3168  AVStream *st)
3169 {
3170  pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
3171 }
3172 
3173 void av_url_split(char *proto, int proto_size,
3174  char *authorization, int authorization_size,
3175  char *hostname, int hostname_size,
3176  int *port_ptr,
3177  char *path, int path_size,
3178  const char *url)
3179 {
3180  const char *p, *ls, *at, *col, *brk;
3181 
3182  if (port_ptr) *port_ptr = -1;
3183  if (proto_size > 0) proto[0] = 0;
3184  if (authorization_size > 0) authorization[0] = 0;
3185  if (hostname_size > 0) hostname[0] = 0;
3186  if (path_size > 0) path[0] = 0;
3187 
3188  /* parse protocol */
3189  if ((p = strchr(url, ':'))) {
3190  av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
3191  p++; /* skip ':' */
3192  if (*p == '/') p++;
3193  if (*p == '/') p++;
3194  } else {
3195  /* no protocol means plain filename */
3196  av_strlcpy(path, url, path_size);
3197  return;
3198  }
3199 
3200  /* separate path from hostname */
3201  ls = strchr(p, '/');
3202  if(!ls)
3203  ls = strchr(p, '?');
3204  if(ls)
3205  av_strlcpy(path, ls, path_size);
3206  else
3207  ls = &p[strlen(p)]; // XXX
3208 
3209  /* the rest is hostname, use that to parse auth/port */
3210  if (ls != p) {
3211  /* authorization (user[:pass]@hostname) */
3212  if ((at = strchr(p, '@')) && at < ls) {
3213  av_strlcpy(authorization, p,
3214  FFMIN(authorization_size, at + 1 - p));
3215  p = at + 1; /* skip '@' */
3216  }
3217 
3218  if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
3219  /* [host]:port */
3220  av_strlcpy(hostname, p + 1,
3221  FFMIN(hostname_size, brk - p));
3222  if (brk[1] == ':' && port_ptr)
3223  *port_ptr = atoi(brk + 2);
3224  } else if ((col = strchr(p, ':')) && col < ls) {
3225  av_strlcpy(hostname, p,
3226  FFMIN(col + 1 - p, hostname_size));
3227  if (port_ptr) *port_ptr = atoi(col + 1);
3228  } else
3229  av_strlcpy(hostname, p,
3230  FFMIN(ls + 1 - p, hostname_size));
3231  }
3232 }
3233 
3234 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
3235 {
3236  int i;
3237  static const char hex_table_uc[16] = { '0', '1', '2', '3',
3238  '4', '5', '6', '7',
3239  '8', '9', 'A', 'B',
3240  'C', 'D', 'E', 'F' };
3241  static const char hex_table_lc[16] = { '0', '1', '2', '3',
3242  '4', '5', '6', '7',
3243  '8', '9', 'a', 'b',
3244  'c', 'd', 'e', 'f' };
3245  const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
3246 
3247  for(i = 0; i < s; i++) {
3248  buff[i * 2] = hex_table[src[i] >> 4];
3249  buff[i * 2 + 1] = hex_table[src[i] & 0xF];
3250  }
3251 
3252  return buff;
3253 }
3254 
3255 int ff_hex_to_data(uint8_t *data, const char *p)
3256 {
3257  int c, len, v;
3258 
3259  len = 0;
3260  v = 1;
3261  for (;;) {
3262  p += strspn(p, SPACE_CHARS);
3263  if (*p == '\0')
3264  break;
3265  c = toupper((unsigned char) *p++);
3266  if (c >= '0' && c <= '9')
3267  c = c - '0';
3268  else if (c >= 'A' && c <= 'F')
3269  c = c - 'A' + 10;
3270  else
3271  break;
3272  v = (v << 4) | c;
3273  if (v & 0x100) {
3274  if (data)
3275  data[len] = v;
3276  len++;
3277  v = 1;
3278  }
3279  }
3280  return len;
3281 }
3282 
3283 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
3284  unsigned int pts_num, unsigned int pts_den)
3285 {
3286  AVRational new_tb;
3287  if(av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)){
3288  if(new_tb.num != pts_num)
3289  av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, pts_num/new_tb.num);
3290  }else
3291  av_log(NULL, AV_LOG_WARNING, "st:%d has too large timebase, reducing\n", s->index);
3292 
3293  if(new_tb.num <= 0 || new_tb.den <= 0) {
3294  av_log(NULL, AV_LOG_ERROR, "Ignoring attempt to set invalid timebase for st:%d\n", s->index);
3295  return;
3296  }
3297  s->time_base = new_tb;
3298  s->pts_wrap_bits = pts_wrap_bits;
3299 }
3300 
3301 int ff_url_join(char *str, int size, const char *proto,
3302  const char *authorization, const char *hostname,
3303  int port, const char *fmt, ...)
3304 {
3305 #if CONFIG_NETWORK
3306  struct addrinfo hints = { 0 }, *ai;
3307 #endif
3308 
3309  str[0] = '\0';
3310  if (proto)
3311  av_strlcatf(str, size, "%s://", proto);
3312  if (authorization && authorization[0])
3313  av_strlcatf(str, size, "%s@", authorization);
3314 #if CONFIG_NETWORK && defined(AF_INET6)
3315  /* Determine if hostname is a numerical IPv6 address,
3316  * properly escape it within [] in that case. */
3317  hints.ai_flags = AI_NUMERICHOST;
3318  if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
3319  if (ai->ai_family == AF_INET6) {
3320  av_strlcat(str, "[", size);
3321  av_strlcat(str, hostname, size);
3322  av_strlcat(str, "]", size);
3323  } else {
3324  av_strlcat(str, hostname, size);
3325  }
3326  freeaddrinfo(ai);
3327  } else
3328 #endif
3329  /* Not an IPv6 address, just output the plain string. */
3330  av_strlcat(str, hostname, size);
3331 
3332  if (port >= 0)
3333  av_strlcatf(str, size, ":%d", port);
3334  if (fmt) {
3335  va_list vl;
3336  int len = strlen(str);
3337 
3338  va_start(vl, fmt);
3339  vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
3340  va_end(vl);
3341  }
3342  return strlen(str);
3343 }
3344 
3345 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
3346  AVFormatContext *src)
3347 {
3348  AVPacket local_pkt;
3349 
3350  local_pkt = *pkt;
3351  local_pkt.stream_index = dst_stream;
3352  if (pkt->pts != AV_NOPTS_VALUE)
3353  local_pkt.pts = av_rescale_q(pkt->pts,
3354  src->streams[pkt->stream_index]->time_base,
3355  dst->streams[dst_stream]->time_base);
3356  if (pkt->dts != AV_NOPTS_VALUE)
3357  local_pkt.dts = av_rescale_q(pkt->dts,
3358  src->streams[pkt->stream_index]->time_base,
3359  dst->streams[dst_stream]->time_base);
3360  return av_write_frame(dst, &local_pkt);
3361 }
3362 
3363 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
3364  void *context)
3365 {
3366  const char *ptr = str;
3367 
3368  /* Parse key=value pairs. */
3369  for (;;) {
3370  const char *key;
3371  char *dest = NULL, *dest_end;
3372  int key_len, dest_len = 0;
3373 
3374  /* Skip whitespace and potential commas. */
3375  while (*ptr && (isspace(*ptr) || *ptr == ','))
3376  ptr++;
3377  if (!*ptr)
3378  break;
3379 
3380  key = ptr;
3381 
3382  if (!(ptr = strchr(key, '=')))
3383  break;
3384  ptr++;
3385  key_len = ptr - key;
3386 
3387  callback_get_buf(context, key, key_len, &dest, &dest_len);
3388  dest_end = dest + dest_len - 1;
3389 
3390  if (*ptr == '\"') {
3391  ptr++;
3392  while (*ptr && *ptr != '\"') {
3393  if (*ptr == '\\') {
3394  if (!ptr[1])
3395  break;
3396  if (dest && dest < dest_end)
3397  *dest++ = ptr[1];
3398  ptr += 2;
3399  } else {
3400  if (dest && dest < dest_end)
3401  *dest++ = *ptr;
3402  ptr++;
3403  }
3404  }
3405  if (*ptr == '\"')
3406  ptr++;
3407  } else {
3408  for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
3409  if (dest && dest < dest_end)
3410  *dest++ = *ptr;
3411  }
3412  if (dest)
3413  *dest = 0;
3414  }
3415 }
3416 
3418 {
3419  int i;
3420  for (i = 0; i < s->nb_streams; i++) {
3421  if (s->streams[i]->id == id)
3422  return i;
3423  }
3424  return -1;
3425 }
3426 
3427 void ff_make_absolute_url(char *buf, int size, const char *base,
3428  const char *rel)
3429 {
3430  char *sep, *path_query;
3431  /* Absolute path, relative to the current server */
3432  if (base && strstr(base, "://") && rel[0] == '/') {
3433  if (base != buf)
3434  av_strlcpy(buf, base, size);
3435  sep = strstr(buf, "://");
3436  if (sep) {
3437  /* Take scheme from base url */
3438  if (rel[1] == '/') {
3439  sep[1] = '\0';
3440  } else {
3441  /* Take scheme and host from base url */
3442  sep += 3;
3443  sep = strchr(sep, '/');
3444  if (sep)
3445  *sep = '\0';
3446  }
3447  }
3448  av_strlcat(buf, rel, size);
3449  return;
3450  }
3451  /* If rel actually is an absolute url, just copy it */
3452  if (!base || strstr(rel, "://") || rel[0] == '/') {
3453  av_strlcpy(buf, rel, size);
3454  return;
3455  }
3456  if (base != buf)
3457  av_strlcpy(buf, base, size);
3458 
3459  /* Strip off any query string from base */
3460  path_query = strchr(buf, '?');
3461  if (path_query != NULL)
3462  *path_query = '\0';
3463 
3464  /* Is relative path just a new query part? */
3465  if (rel[0] == '?') {
3466  av_strlcat(buf, rel, size);
3467  return;
3468  }
3469 
3470  /* Remove the file name from the base url */
3471  sep = strrchr(buf, '/');
3472  if (sep)
3473  sep[1] = '\0';
3474  else
3475  buf[0] = '\0';
3476  while (av_strstart(rel, "../", NULL) && sep) {
3477  /* Remove the path delimiter at the end */
3478  sep[0] = '\0';
3479  sep = strrchr(buf, '/');
3480  /* If the next directory name to pop off is "..", break here */
3481  if (!strcmp(sep ? &sep[1] : buf, "..")) {
3482  /* Readd the slash we just removed */
3483  av_strlcat(buf, "/", size);
3484  break;
3485  }
3486  /* Cut off the directory name */
3487  if (sep)
3488  sep[1] = '\0';
3489  else
3490  buf[0] = '\0';
3491  rel += 3;
3492  }
3493  av_strlcat(buf, rel, size);
3494 }
3495 
3496 int64_t ff_iso8601_to_unix_time(const char *datestr)
3497 {
3498 #if HAVE_STRPTIME
3499  struct tm time1 = {0}, time2 = {0};
3500  char *ret1, *ret2;
3501  ret1 = strptime(datestr, "%Y - %m - %d %T", &time1);
3502  ret2 = strptime(datestr, "%Y - %m - %dT%T", &time2);
3503  if (ret2 && !ret1)
3504  return av_timegm(&time2);
3505  else
3506  return av_timegm(&time1);
3507 #else
3508  av_log(NULL, AV_LOG_WARNING, "strptime() unavailable on this system, cannot convert "
3509  "the date string.\n");
3510  return 0;
3511 #endif
3512 }
3513 
3514 int avformat_query_codec(AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance)
3515 {
3516  if (ofmt) {
3517  if (ofmt->query_codec)
3518  return ofmt->query_codec(codec_id, std_compliance);
3519  else if (ofmt->codec_tag)
3520  return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
3521  else if (codec_id == ofmt->video_codec || codec_id == ofmt->audio_codec ||
3522  codec_id == ofmt->subtitle_codec)
3523  return 1;
3524  }
3525  return AVERROR_PATCHWELCOME;
3526 }
3527 
3529 {
3530 #if CONFIG_NETWORK
3531  int ret;
3533  if ((ret = ff_network_init()) < 0)
3534  return ret;
3535  ff_tls_init();
3536 #endif
3537  return 0;
3538 }
3539 
3541 {
3542 #if CONFIG_NETWORK
3543  ff_network_close();
3544  ff_tls_deinit();
3545 #endif
3546  return 0;
3547 }
3548 
3550  uint64_t channel_layout, int32_t sample_rate,
3552 {
3553  uint32_t flags = 0;
3554  int size = 4;
3555  uint8_t *data;
3556  if (!pkt)
3557  return AVERROR(EINVAL);
3558  if (channels) {
3559  size += 4;
3561  }
3562  if (channel_layout) {
3563  size += 8;
3565  }
3566  if (sample_rate) {
3567  size += 4;
3569  }
3570  if (width || height) {
3571  size += 8;
3573  }
3575  if (!data)
3576  return AVERROR(ENOMEM);
3577  bytestream_put_le32(&data, flags);
3578  if (channels)
3579  bytestream_put_le32(&data, channels);
3580  if (channel_layout)
3581  bytestream_put_le64(&data, channel_layout);
3582  if (sample_rate)
3583  bytestream_put_le32(&data, sample_rate);
3584  if (width || height) {
3585  bytestream_put_le32(&data, width);
3586  bytestream_put_le32(&data, height);
3587  }
3588  return 0;
3589 }
3590 
3592 {
3593  return ff_codec_bmp_tags;
3594 }
3596 {
3597  return ff_codec_wav_tags;
3598 }
unsigned int nb_chapters
Definition: avformat.h:969
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:961
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:3173
codec_id is not known (like AV_CODEC_ID_NONE) but lavf should attempt to identify it ...
Definition: avcodec.h:428
int64_t first_dts
Definition: avformat.h:758
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:2815
const struct AVCodec * codec
Definition: avcodec.h:1348
int ff_url_join(char *str, int size, const char *proto, const char *authorization, const char *hostname, int port, const char *fmt,...)
Definition: utils.c:3301
full parsing and interpolation of timestamps for frames not starting on a packet boundary ...
Definition: avformat.h:578
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:61
static void fill_all_stream_timings(AVFormatContext *ic)
Definition: utils.c:1835
Bytestream IO Context.
Definition: avio.h:68
int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt, AVFormatContext *src)
Write a packet to another muxer than the one the user originally intended.
Definition: utils.c:3345
static void update_initial_durations(AVFormatContext *s, AVStream *st, int stream_index, int duration)
Definition: utils.c:788
AVProbeData probe_data
Definition: avformat.h:782
int64_t avio_size(AVIOContext *s)
Get the filesize.
Definition: aviobuf.c:241
enum AVCodecID id
Definition: internal.h:36
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:153
#define PROBE_BUF_MIN
size of probe buffer, for guessing file type from file contents
Definition: utils.c:353
This structure describes decoded (raw) audio or video data.
Definition: avcodec.h:989
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:547
AVIOInterruptCB interrupt_callback
Custom interrupt callbacks for the I/O layer.
Definition: avformat.h:1005
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:189
static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload, AVRational time_base)
Definition: utils.c:3137
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:1393
static void update_stream_timings(AVFormatContext *ic)
Estimate the stream timings from the one of each components.
Definition: utils.c:1794
enum AVCodecID id
Definition: mxfenc.c:85
void ff_compute_frame_duration(int *pnum, int *pden, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Return the frame duration in seconds.
Definition: utils.c:706
int av_write_frame(AVFormatContext *s, AVPacket *pkt)
Write a packet to an output media file.
Definition: mux.c:394
static int get_std_framerate(int i)
Definition: utils.c:2211
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
Definition: mathematics.c:58
const char * filename
Definition: avformat.h:339
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:940
int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
Open an input stream and read the header.
Definition: utils.c:476
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:75
int avformat_query_codec(AVOutputFormat *ofmt, enum AVCodecID codec_id, int std_compliance)
Test if the given container can store a codec.
Definition: utils.c:3514
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:1442
int64_t pts_buffer[MAX_REORDER_DELAY+1]
Definition: avformat.h:784
int64_t pos
Definition: avformat.h:583
int probe_packets
Definition: avformat.h:767
#define NTP_OFFSET_US
Definition: internal.h:88
enum AVCodecID video_codec
default video codec
Definition: avformat.h:387
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
Definition: opt.c:505
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown)
Definition: avformat.h:697
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:1401
int index
stream index in AVFormatContext
Definition: avformat.h:623
int size
Definition: avcodec.h:916
int av_dict_count(const AVDictionary *m)
Get number of entries in dictionary.
Definition: dict.c:34
static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:1893
#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
AVIndexEntry * index_entries
Only used if the format does not support seeking natively.
Definition: avformat.h:786
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
Definition: avcodec.h:1724
int ff_find_stream_index(AVFormatContext *s, int id)
Find stream index based on format-specific stream ID.
Definition: utils.c:3417
void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
Send a nice dump of a packet to the specified file stream.
Definition: utils.c:3162
int av_strncasecmp(const char *a, const char *b, size_t n)
Locale-independent case-insensitive compare.
Definition: avstring.c:150
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1533
void ff_network_close(void)
Definition: network.c:148
static int av_cmp_q(AVRational a, AVRational b)
Compare two rationals.
Definition: rational.h:55
static void estimate_timings_from_bit_rate(AVFormatContext *ic)
Definition: utils.c:1852
void * priv_data
Definition: avformat.h:653
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)
static void hex_dump_internal(void *avcl, FILE *f, int level, const uint8_t *buf, int size)
Definition: utils.c:3098
#define freeaddrinfo
Definition: network.h:182
int64_t data_offset
offset of the first packet
Definition: avformat.h:1029
int duration
Duration of the current frame.
Definition: avcodec.h:3851
discard all
Definition: avcodec.h:536
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:3363
struct AVStream::@75 * info
int priv_data_size
Size of private data so that it can be allocated in the wrapper.
Definition: avformat.h:499
int ctx_flags
Format-specific flags, see AVFMTCTX_xx.
Definition: avformat.h:864
int dts_ref_dts_delta
Offset of the current timestamp against last timestamp sync point in units of AVCodecContext.time_base.
Definition: avcodec.h:3813
AVDictionary * metadata
Definition: avformat.h:817
static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options)
Definition: utils.c:2043
AVDictionaryEntry * av_dict_get(AVDictionary *m, const char *key, const AVDictionaryEntry *prev, int flags)
Get a dictionary entry with matching key.
Definition: dict.c:40
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:122
static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
Definition: utils.c:1114
int id
Definition: avformat.h:801
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:717
enum AVCodecID subtitle_codec_id
Forced subtitle codec_id.
Definition: avformat.h:949
AVCodec.
Definition: avcodec.h:2960
#define AI_NUMERICHOST
Definition: network.h:151
static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
Definition: utils.c:1970
static int64_t duration
Definition: avplay.c:249
static int has_decode_delay_been_guessed(AVStream *st)
Definition: utils.c:2036
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:1025
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1465
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:151
enum AVAudioServiceType audio_service_type
Type of service that the audio stream conveys.
Definition: avcodec.h:2179
Format I/O context.
Definition: avformat.h:828
static int has_codec_parameters(AVStream *st)
Definition: utils.c:2014
unsigned int nb_stream_indexes
Definition: avformat.h:805
int ff_network_inited_globally
Definition: network.c:119
int64_t cur_dts
Definition: avformat.h:759
#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:1889
int(* read_close)(struct AVFormatContext *)
Close the stream.
Definition: avformat.h:530
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2112
int flags
can use flags: AVFMT_NOFILE, AVFMT_NEEDNUMBER, AVFMT_RAWPICTURE, AVFMT_GLOBALHEADER, AVFMT_NOTIMESTAMPS, AVFMT_VARIABLE_FPS, AVFMT_NODIMENSIONS, AVFMT_NOSTREAMS, AVFMT_ALLOW_FLUSH, AVFMT_TS_NONSTRICT
Definition: avformat.h:395
uint8_t
const struct AVCodecTag * avformat_get_riff_video_tags(void)
Definition: utils.c:3591
int ff_network_init(void)
Definition: network.c:121
AVOptions.
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:469
unsigned avformat_version(void)
Return the LIBAVFORMAT_VERSION_INT constant.
Definition: utils.c:55
const char * avformat_license(void)
Return the libavformat license.
Definition: utils.c:65
AVPacket pkt
Definition: avformat.h:1052
static AVOutputFormat * first_oformat
head of registered output format linked list
Definition: utils.c:74
int id
unique ID to identify the chapter
Definition: avformat.h:814
int id
Format-specific stream ID.
Definition: avformat.h:629
enum AVStreamParseType need_parsing
Definition: avformat.h:775
#define b
Definition: input.c:52
enum AVCodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
Definition: utils.c:2126
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1454
const char * name
static int match_format(const char *name, const char *names)
Definition: utils.c:133
AVStream ** streams
Definition: avformat.h:876
AVFormatContext * avformat_alloc_context(void)
Allocate an AVFormatContext.
Definition: options.c:97
const char * avformat_configuration(void)
Return the libavformat build-time configuration.
Definition: utils.c:60
const char data[16]
Definition: mxf.c:66
const AVClass * priv_class
AVClass for the private context.
Definition: avformat.h:480
static double av_q2d(AVRational a)
Convert rational to double.
Definition: rational.h:69
#define DURATION_MAX_RETRY
Definition: utils.c:1890
uint8_t * data
Definition: avcodec.h:915
AVProgram * av_new_program(AVFormatContext *ac, int id)
Definition: utils.c:2792
int(* read_header)(struct AVFormatContext *)
Read the format header and initialize the AVFormatContext structure.
Definition: avformat.h:513
static int flags
Definition: log.c:42
uint32_t tag
Definition: movenc.c:802
int avformat_network_init(void)
Do global initialization of network components.
Definition: utils.c:3528
const struct AVCodecTag * avformat_get_riff_audio_tags(void)
Definition: utils.c:3595
int fps_probe_size
decoding: number of frames used to probe fps
Definition: avformat.h:986
int av_match_ext(const char *filename, const char *extensions)
Return a positive value if the given filename has one of the given extensions, 0 otherwise.
Definition: utils.c:106
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
Definition: lzo.c:79
static void dump_stream_format(AVFormatContext *ic, int i, int index, int is_output)
Definition: utils.c:2888
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:2704
enum AVDiscard discard
selects which program to discard and which to feed to the caller
Definition: avformat.h:803
static int genpts
Definition: avplay.c:255
static AVPacket flush_pkt
Definition: avplay.c:279
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:937
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:2140
unsigned int * stream_index
Definition: avformat.h:804
enum AVCodecID video_codec_id
Forced video codec_id.
Definition: avformat.h:937
static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd, int score)
Definition: utils.c:317
int avio_read(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:446
struct AVOutputFormat * oformat
Definition: avformat.h:842
static void free_packet_buffer(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
Definition: utils.c:989
void(* destruct)(struct AVPacket *)
Definition: avcodec.h:938
int ff_read_packet(AVFormatContext *s, AVPacket *pkt)
Read a transport packet from a media file.
Definition: utils.c:594
struct AVPacketList * raw_packet_buffer
Raw packets from the demuxer, prior to parsing and decoding.
Definition: avformat.h:1037
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:122
static const uint8_t frame_size[4]
Definition: g723_1_data.h:47
void av_dump_format(AVFormatContext *ic, int index, const char *url, int is_output)
Definition: utils.c:2952
void av_dict_copy(AVDictionary **dst, AVDictionary *src, int flags)
Copy entries from one AVDictionary struct into another.
Definition: dict.c:127
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:2589
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:1437
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:56
void ff_read_frame_flush(AVFormatContext *s)
Flush the frame reader.
Definition: utils.c:1295
static int64_t start_time
Definition: avplay.c:248
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition: avcodec.h:95
AVDictionary * metadata
Definition: avformat.h:972
int av_get_bits_per_sample(enum AVCodecID codec_id)
Return codec bits per sample.
Definition: utils.c:1813
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1634
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:139
int nb_decoded_frames
Definition: avformat.h:734
int64_t pos
Byte position of currently parsed frame in stream.
Definition: avcodec.h:3839
int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp, int flags)
Get the index for a specific timestamp.
Definition: utils.c:1435
char * ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
Definition: utils.c:3234
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:1334
int(* read_probe)(AVProbeData *)
Tell if a given file has a chance of being parsed as this format.
Definition: avformat.h:506
void av_hex_dump(FILE *f, const uint8_t *buf, int size)
Send a nice hexadecimal dump of a buffer to the specified file stream.
Definition: utils.c:3127
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: utils.c:53
struct AVCodecParser * parser
Definition: avcodec.h:3721
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
Definition: avcodec.h:852
unsigned int avpriv_toupper4(unsigned int x)
Definition: utils.c:2097
int64_t timestamp
Definition: avformat.h:584
int(* query_codec)(enum AVCodecID id, int std_compliance)
Test if the given codec can be stored in this container.
Definition: avformat.h:440
int avio_close(AVIOContext *s)
Close the resource accessed by the AVIOContext s and free it.
Definition: aviobuf.c:782
int avcodec_is_open(AVCodecContext *s)
Definition: utils.c:2146
g
Definition: yuv2rgb.c:540
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:959
int capabilities
Codec capabilities.
Definition: avcodec.h:2979
unsigned int nb_programs
Definition: avformat.h:930
int last_IP_duration
Definition: avformat.h:761
int av_read_play(AVFormatContext *s)
Start playing a network-based stream (e.g.
Definition: utils.c:2644
void av_dict_free(AVDictionary **pm)
Free all the memory allocated for an AVDictionary struct and all keys and values. ...
Definition: dict.c:113
preferred ID for decoding MPEG audio layer 1, 2 or 3
Definition: avcodec.h:348
void ff_id3v2_free_extra_meta(ID3v2ExtraMeta **extra_meta)
Free memory allocated parsing special (non-text) metadata.
Definition: id3v2.c:703
AVChapter ** chapters
Definition: avformat.h:970
simple assert() macros that are a bit more flexible than ISO C assert().
void ff_make_absolute_url(char *buf, int size, const char *base, const char *rel)
Definition: utils.c:3427
int64_t av_gcd(int64_t a, int64_t b)
Return the greatest common divisor of a and b.
Definition: mathematics.c:53
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:146
AVStream * avformat_new_stream(AVFormatContext *s, AVCodec *c)
Add a new stream to a media file.
Definition: utils.c:2736
enum AVCodecID codec_id
Definition: mov_chan.c:432
AVRational avg_frame_rate
Average framerate.
Definition: avformat.h:704
const AVCodecTag ff_codec_wav_tags[]
Definition: riff.c:297
New fields can be added to the end with minor version bumps.
Definition: avformat.h:800
int ff_get_audio_frame_size(AVCodecContext *enc, int size, int mux)
Get the number of samples of an audio frame.
Definition: utils.c:684
AVInputFormat * av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
Guess the file format.
Definition: utils.c:258
#define LIBAV_CONFIGURATION
Definition: config.h:4
int min_distance
Minimum distance between this and the previous keyframe, used to avoid unneeded searching.
Definition: avformat.h:588
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:67
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:921
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
Definition: codec_desc.c:2187
Only parse headers, do not repack.
Definition: avformat.h:577
static AVInputFormat * first_iformat
head of registered input format linked list
Definition: utils.c:72
AVCodecContext * codec
Codec context associated with this stream.
Definition: avformat.h:641
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:1347
static float distance(float x, float y, int band)
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: avcodec.h:466
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
Definition: rational.c:36
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:341
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:3283
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:340
static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
Definition: utils.c:421
#define FF_MAX_EXTRADATA_SIZE
Maximum size in bytes of extradata.
Definition: internal.h:117
AVFrame * avcodec_alloc_frame(void)
Allocate an AVFrame and set its fields to default values.
Definition: utils.c:618
unsigned int nb_streams
A list of all streams in the file.
Definition: avformat.h:875
int64_t fps_first_dts
Those are used for average framerate estimation.
Definition: avformat.h:740
int av_read_pause(AVFormatContext *s)
Pause a network-based stream (e.g.
Definition: utils.c:2653
AVInputFormat * av_find_input_format(const char *short_name)
Find AVInputFormat based on the short name of the input format.
Definition: utils.c:207
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:114
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:1404
#define dynarray_add(tab, nb_ptr, elem)
Definition: internal.h:56
char filename[1024]
input or output filename
Definition: avformat.h:878
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:110
enum AVCodecID audio_codec_id
Forced audio codec_id.
Definition: avformat.h:943
const AVCodecTag ff_codec_bmp_tags[]
Definition: riff.c:31
int av_strcasecmp(const char *a, const char *b)
Definition: avstring.c:140
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition: options.c:126
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:113
enum AVCodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name, const char *filename, const char *mime_type, enum AVMediaType type)
Guess the codec ID based upon muxer and filename.
Definition: utils.c:186
static const chunk_decoder decoder[8]
Definition: dfa.c:303
int width
picture width / height.
Definition: avcodec.h:1508
int64_t offset
byte offset from starting packet start
Definition: avcodec.h:3758
int av_find_default_stream_index(AVFormatContext *s)
Definition: utils.c:1272
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: utils.c:356
static av_always_inline int64_t avio_skip(AVIOContext *s, int64_t offset)
Skip given number of bytes forward.
Definition: avio.h:210
const char * name
Definition: avformat.h:376
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:3786
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:203
static int seek_frame_generic(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: utils.c:1627
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:1503
static av_always_inline av_const long int lrintf(float x)
Definition: libm.h:144
static AVPacket * add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt, AVPacketList **plast_pktl)
Definition: utils.c:447
AVDictionary * metadata
Definition: avformat.h:699
AVOutputFormat * av_guess_format(const char *short_name, const char *filename, const char *mime_type)
Return the output format in the list of registered output formats which best matches the provided par...
Definition: utils.c:151
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1474
int ffio_rewind_with_probe_data(AVIOContext *s, unsigned char *buf, int buf_size)
Rewind the AVIOContext using the specified buffer containing the first buf_size bytes of the file...
Definition: aviobuf.c:721
void av_register_input_format(AVInputFormat *format)
Definition: utils.c:88
#define L(x)
Definition: vp56_arith.h:36
int(* read_pause)(struct AVFormatContext *)
Pause playing - only meaningful if using a network-based format (RTSP).
Definition: avformat.h:560
unsigned int probesize
decoding: size of data to probe; encoding: unused.
Definition: avformat.h:919
preferred ID for MPEG-1/2 video decoding
Definition: avcodec.h:100
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:1321
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2733
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:3043
static void probe_codec(AVFormatContext *s, AVStream *st, const AVPacket *pkt)
Definition: utils.c:561
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:236
void ff_id3v2_read(AVFormatContext *s, const char *magic, ID3v2ExtraMeta **extra_meta)
Read an ID3v2 tag, including supported extra metadata.
Definition: id3v2.c:672
AVOutputFormat * av_oformat_next(AVOutputFormat *f)
If f is NULL, returns the first registered output format, if f is non-NULL, returns the next register...
Definition: utils.c:82
int raw_packet_buffer_remaining_size
Definition: avformat.h:1048
int64_t av_gettime(void)
Get the current time in microseconds.
Definition: time.c:37
Stream structure.
Definition: avformat.h:622
int64_t end
chapter start/end time in time_base units
Definition: avformat.h:816
void av_register_output_format(AVOutputFormat *format)
Definition: utils.c:97
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:3540
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:2124
int pts_dts_delta
Presentation delay of current frame in units of AVCodecContext.time_base.
Definition: avcodec.h:3827
NULL
Definition: eval.c:52
static int width
Definition: utils.c:156
AVCodecParserContext * av_parser_init(int codec_id)
Definition: parser.c:41
struct AVOutputFormat * next
Definition: avformat.h:413
enum AVMediaType codec_type
Definition: avcodec.h:1347
unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum AVCodecID id)
Definition: utils.c:2116
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:540
int ff_check_interrupt(AVIOInterruptCB *cb)
Check if the user has requested to interrup a blocking function associated with cb.
Definition: avio.c:369
int debug
Flags to enable debugging.
Definition: avformat.h:1010
enum AVCodecID codec_id
Definition: avcodec.h:1350
int av_opt_set_dict(void *obj, AVDictionary **options)
Definition: opt.c:615
#define PRINT(...)
AV_SAMPLE_FMT_NONE
Definition: avconv_filter.c:63
int sample_rate
samples per second
Definition: avcodec.h:2104
AVIOContext * pb
I/O context.
Definition: avformat.h:861
unsigned int av_codec_get_tag(const AVCodecTag *const *tags, enum AVCodecID id)
Definition: utils.c:2170
const struct AVCodecTag *const * codec_tag
List of supported codec_id-codec_tag pairs, ordered by "better choice first".
Definition: avformat.h:401
int64_t reference_dts
Timestamp corresponding to the last dts sync point.
Definition: avformat.h:757
static void update_initial_timestamps(AVFormatContext *s, int stream_index, int64_t dts, int64_t pts)
Definition: utils.c:759
main external API structure.
Definition: avcodec.h:1339
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
Definition: utils.c:1515
int av_filename_number_test(const char *filename)
Check whether filename actually is a numbered sequence generator.
Definition: utils.c:252
int(* read_packet)(struct AVFormatContext *, AVPacket *pkt)
Read one packet and put it in 'pkt'.
Definition: avformat.h:524
static void compute_pkt_fields(AVFormatContext *s, AVStream *st, AVCodecParserContext *pc, AVPacket *pkt)
Definition: utils.c:826
int extradata_size
Definition: avcodec.h:1455
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:63
int nb_index_entries
Definition: avformat.h:788
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:604
enum AVCodecID av_codec_get_id(const AVCodecTag *const *tags, unsigned int tag)
Definition: utils.c:2180
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:3255
int index
Definition: gxfenc.c:72
enum AVCodecID subtitle_codec
default subtitle codec
Definition: avformat.h:388
#define SPACE_CHARS
Definition: internal.h:177
rational number numerator/denominator
Definition: rational.h:43
struct AVPacketList * packet_buffer_end
Definition: avformat.h:1026
uint64_t ff_ntp_time(void)
Get the current time since NTP epoch in microseconds.
Definition: utils.c:3038
AVMediaType
Definition: avutil.h:177
size_t av_strlcatf(char *dst, size_t size, const char *fmt,...)
Definition: avstring.c:85
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:765
int64_t fps_last_dts
Definition: avformat.h:742
static int seek_frame_internal(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
Definition: utils.c:1686
int64_t ff_iso8601_to_unix_time(const char *datestr)
Convert a date string in ISO8601 format to Unix timestamp.
Definition: utils.c:3496
static int step
Definition: avplay.c:252
int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
Definition: utils.c:645
int found_decoder
Definition: avformat.h:735
void avformat_free_context(AVFormatContext *s)
Free an AVFormatContext and all its streams.
Definition: utils.c:2662
This structure contains the data a format has to probe a file.
Definition: avformat.h:338
int av_get_audio_frame_duration(AVCodecContext *avctx, int frame_bytes)
Return audio frame duration.
Definition: utils.c:1831
misc parsing utilities
int av_read_frame(AVFormatContext *s, AVPacket *pkt)
Return the next frame of a stream.
Definition: utils.c:1206
static int has_duration(AVFormatContext *ic)
Return TRUE if the stream has accurate duration in any stream.
Definition: utils.c:1774
size_t av_strlcat(char *dst, const char *src, size_t size)
Append the string src to the string dst, but to a total length of no more than size - 1 bytes...
Definition: avstring.c:77
int avio_pause(AVIOContext *h, int pause)
Pause and resume playing - only meaningful if using a network streaming protocol (e.g.
Definition: aviobuf.c:816
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:1740
This struct describes the properties of a single codec described by an AVCodecID. ...
Definition: avcodec.h:450
full parsing and repack of the first frame only, only implemented for H.264 currently ...
Definition: avformat.h:579
struct AVPacketList * parse_queue_end
Definition: avformat.h:1043
AVDictionary * metadata
Definition: avformat.h:806
int fps_first_dts_idx
Definition: avformat.h:741
struct AVPacketList * parse_queue
Packets split by the parser get queued here.
Definition: avformat.h:1042
int64_t start_time
Decoding: position of the first frame of the component, in AV_TIME_BASE fractional seconds...
Definition: avformat.h:885
uint8_t level
Definition: svq3.c:125
#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:1730
unsigned int tag
Definition: internal.h:37
int height
Definition: gxfenc.c:72
static int is_intra_only(enum AVCodecID id)
Definition: utils.c:749
int64_t start
Definition: avformat.h:816
int64_t duration
Decoding: duration of the stream, in stream time base.
Definition: avformat.h:684
const OptionDef options[]
Definition: avserver.c:4665
enum AVMediaType type
Definition: avcodec.h:452
static void queue_attached_pictures(AVFormatContext *s)
Definition: utils.c:464
const char * extensions
If extensions are defined, then no probe is done.
Definition: avformat.h:476
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:32
static int tb_unreliable(AVCodecContext *c)
Definition: utils.c:2224
#define getaddrinfo
Definition: network.h:181
Main libavformat public API header.
int(* split)(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: avcodec.h:3863
static void print_fps(double d, const char *postfix)
Definition: utils.c:2867
void av_opt_free(void *obj)
Free all string and binary options in obj.
Definition: opt.c:607
common internal api header.
struct AVPacketList * next
Definition: avformat.h:1053
int ff_id3v2_match(const uint8_t *buf, const char *magic)
Detect ID3v2 Header.
Definition: id3v2.c:126
int(* read_play)(struct AVFormatContext *)
Start/resume playing - only meaningful if using a network-based format (RTSP).
Definition: avformat.h:554
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:116
int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
Read packets of a media file to get stream information.
Definition: utils.c:2236
int64_t start_time
Decoding: pts of the first frame of the stream, in stream time base.
Definition: avformat.h:677
int disposition
AV_DISPOSITION_* bit field.
Definition: avformat.h:688
int pts_wrap_bits
number of bits in pts (used for wrapping control)
Definition: avformat.h:747
Bi-dir predicted.
Definition: avutil.h:247
AVRational time_base
time base in which the start/end timestamps are specified
Definition: avformat.h:815
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:83
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:42
char * key
Definition: dict.h:75
int den
denominator
Definition: rational.h:45
unsigned bps
Definition: movenc.c:803
int max_analyze_duration
decoding: maximum time (in AV_TIME_BASE units) during which the input should be analyzed in avformat_...
Definition: avformat.h:925
struct AVInputFormat * iformat
Can only be iformat or oformat, not both at the same time.
Definition: avformat.h:841
void avformat_close_input(AVFormatContext **ps)
Close an opened input AVFormatContext.
Definition: utils.c:2713
enum AVCodecID ff_guess_image2_codec(const char *filename)
Definition: img2.c:88
AVInputFormat * av_iformat_next(AVInputFormat *f)
If f is NULL, returns the first registered input format, if f is non-NULL, returns the next registere...
Definition: utils.c:76
void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
Definition: utils.c:1572
unsigned int index_entries_allocated_size
Definition: avformat.h:789
int64_t frame_offset
Definition: avcodec.h:3722
static int parse_packet(AVFormatContext *s, AVPacket *pkt, int stream_index)
Parse a packet, add all split parts to parse_queue.
Definition: utils.c:1005
static int read_from_packet_buffer(AVPacketList **pkt_buffer, AVPacketList **pkt_buffer_end, AVPacket *pkt)
Definition: utils.c:1099
static AVProgram * find_program_from_stream(AVFormatContext *ic, int s)
Definition: utils.c:2578
char * value
Definition: dict.h:76
int len
int channels
number of audio channels
Definition: avcodec.h:2105
enum AVCodecID audio_codec
default audio codec
Definition: avformat.h:386
#define av_log2
Definition: intmath.h:85
void ff_tls_init(void)
Definition: network.c:64
int ff_id3v2_tag_len(const uint8_t *buf)
Get the length of an ID3v2 tag.
Definition: id3v2.c:139
struct AVCodecParserContext * parser
Definition: avformat.h:776
void * priv_data
Format private data.
Definition: avformat.h:848
int codec_info_nb_frames
Number of frames that have been demuxed during av_find_stream_info()
Definition: avformat.h:772
static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
Definition: utils.c:2874
static void flush_packet_queue(AVFormatContext *s)
Definition: utils.c:1260
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:1268
void avcodec_free_frame(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: utils.c:630
void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
Definition: utils.c:2839
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:914
static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags)
Definition: utils.c:1613
int bit_rate
Decoding: total stream bitrate in bit/s, 0 if not available.
Definition: avformat.h:900
int ai_flags
Definition: network.h:102
struct AVIndexEntry AVIndexEntry
int repeat_pict
This field is used for proper frame duration computation in lavf.
Definition: avcodec.h:3737
int64_t duration
Decoding: duration of the stream, in AV_TIME_BASE fractional seconds.
Definition: avformat.h:893
int64_t last_IP_pts
Definition: avformat.h:760
void ff_tls_deinit(void)
Definition: network.c:96
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:568
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:455
unbuffered private I/O API
static void compute_chapters_end(AVFormatContext *s)
Definition: utils.c:2190
void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload, AVStream *st)
Send a nice dump of a packet to the log.
Definition: utils.c:3167
void av_hex_dump_log(void *avcl, int level, const uint8_t *buf, int size)
Send a nice hexadecimal dump of a buffer to the log.
Definition: utils.c:3132
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:165
int fps_last_dts_idx
Definition: avformat.h:743
int stream_index
Definition: avcodec.h:917
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avformat.h:669
struct AVInputFormat * next
Definition: avformat.h:489
#define LIBAV_LICENSE
Definition: config.h:5
const char * extensions
comma-separated filename extensions
Definition: avformat.h:384
enum AVDiscard discard
Selects which packets can be discarded at will and do not need to be demuxed.
Definition: avformat.h:690
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:3549
AVInputFormat * av_probe_input_format(AVProbeData *pd, int is_opened)
Guess the file format.
Definition: utils.c:312
const char * mime_type
Definition: avformat.h:383
struct AVPacketList * raw_packet_buffer_end
Definition: avformat.h:1038
This structure stores compressed data.
Definition: avcodec.h:898
int key_frame
Set by parser to 1 for key frames and 0 for non-key frames.
Definition: avcodec.h:3767
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:158
int dts_sync_point
Synchronization point for start of timestamp generation.
Definition: avcodec.h:3798
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:908
AVPacket attached_pic
For streams with AV_DISPOSITION_ATTACHED_PIC disposition, this packet will contain the attached pictu...
Definition: avformat.h:713
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:218
AVProgram ** programs
Definition: avformat.h:931
#define PROBE_BUF_MAX
Definition: utils.c:354
discard nothing
Definition: avcodec.h:531
int64_t av_compare_mod(uint64_t a, uint64_t b, uint64_t mod)
Compare 2 integers modulo mod.
Definition: mathematics.c:135
uint8_t * subtitle_header
Header containing style information for text subtitles.
Definition: avcodec.h:2917
if(!(ptr_align%ac->ptr_align)&&samples_align >=aligned_len)
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:1362