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 "riff.h"
39 #include "audiointerleave.h"
40 #include "url.h"
41 #include <sys/time.h>
42 #include <time.h>
43 #include <stdarg.h>
44 #if CONFIG_NETWORK
45 #include "network.h"
46 #endif
47 
48 #undef NDEBUG
49 #include <assert.h>
50 
56 unsigned avformat_version(void)
57 {
59 }
60 
61 const char *avformat_configuration(void)
62 {
63  return LIBAV_CONFIGURATION;
64 }
65 
66 const char *avformat_license(void)
67 {
68 #define LICENSE_PREFIX "libavformat license: "
69  return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
70 }
71 
72 /* fraction handling */
73 
84 static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
85 {
86  num += (den >> 1);
87  if (num >= den) {
88  val += num / den;
89  num = num % den;
90  }
91  f->val = val;
92  f->num = num;
93  f->den = den;
94 }
95 
102 static void frac_add(AVFrac *f, int64_t incr)
103 {
104  int64_t num, den;
105 
106  num = f->num + incr;
107  den = f->den;
108  if (num < 0) {
109  f->val += num / den;
110  num = num % den;
111  if (num < 0) {
112  num += den;
113  f->val--;
114  }
115  } else if (num >= den) {
116  f->val += num / den;
117  num = num % den;
118  }
119  f->num = num;
120 }
121 
126 
128 {
129  if(f) return f->next;
130  else return first_iformat;
131 }
132 
134 {
135  if(f) return f->next;
136  else return first_oformat;
137 }
138 
140 {
141  AVInputFormat **p;
142  p = &first_iformat;
143  while (*p != NULL) p = &(*p)->next;
144  *p = format;
145  format->next = NULL;
146 }
147 
149 {
150  AVOutputFormat **p;
151  p = &first_oformat;
152  while (*p != NULL) p = &(*p)->next;
153  *p = format;
154  format->next = NULL;
155 }
156 
157 int av_match_ext(const char *filename, const char *extensions)
158 {
159  const char *ext, *p;
160  char ext1[32], *q;
161 
162  if(!filename)
163  return 0;
164 
165  ext = strrchr(filename, '.');
166  if (ext) {
167  ext++;
168  p = extensions;
169  for(;;) {
170  q = ext1;
171  while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
172  *q++ = *p++;
173  *q = '\0';
174  if (!av_strcasecmp(ext1, ext))
175  return 1;
176  if (*p == '\0')
177  break;
178  p++;
179  }
180  }
181  return 0;
182 }
183 
184 static int match_format(const char *name, const char *names)
185 {
186  const char *p;
187  int len, namelen;
188 
189  if (!name || !names)
190  return 0;
191 
192  namelen = strlen(name);
193  while ((p = strchr(names, ','))) {
194  len = FFMAX(p - names, namelen);
195  if (!av_strncasecmp(name, names, len))
196  return 1;
197  names = p+1;
198  }
199  return !av_strcasecmp(name, names);
200 }
201 
202 AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
203  const char *mime_type)
204 {
205  AVOutputFormat *fmt = NULL, *fmt_found;
206  int score_max, score;
207 
208  /* specific test for image sequences */
209 #if CONFIG_IMAGE2_MUXER
210  if (!short_name && filename &&
211  av_filename_number_test(filename) &&
212  ff_guess_image2_codec(filename) != CODEC_ID_NONE) {
213  return av_guess_format("image2", NULL, NULL);
214  }
215 #endif
216  /* Find the proper file type. */
217  fmt_found = NULL;
218  score_max = 0;
219  while ((fmt = av_oformat_next(fmt))) {
220  score = 0;
221  if (fmt->name && short_name && !strcmp(fmt->name, short_name))
222  score += 100;
223  if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
224  score += 10;
225  if (filename && fmt->extensions &&
226  av_match_ext(filename, fmt->extensions)) {
227  score += 5;
228  }
229  if (score > score_max) {
230  score_max = score;
231  fmt_found = fmt;
232  }
233  }
234  return fmt_found;
235 }
236 
237 enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
238  const char *filename, const char *mime_type, enum AVMediaType type){
239  if(type == AVMEDIA_TYPE_VIDEO){
241 
242 #if CONFIG_IMAGE2_MUXER
243  if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
244  codec_id= ff_guess_image2_codec(filename);
245  }
246 #endif
247  if(codec_id == CODEC_ID_NONE)
248  codec_id= fmt->video_codec;
249  return codec_id;
250  }else if(type == AVMEDIA_TYPE_AUDIO)
251  return fmt->audio_codec;
252  else if (type == AVMEDIA_TYPE_SUBTITLE)
253  return fmt->subtitle_codec;
254  else
255  return CODEC_ID_NONE;
256 }
257 
258 AVInputFormat *av_find_input_format(const char *short_name)
259 {
260  AVInputFormat *fmt = NULL;
261  while ((fmt = av_iformat_next(fmt))) {
262  if (match_format(short_name, fmt->name))
263  return fmt;
264  }
265  return NULL;
266 }
267 
268 
270 {
271  int ret= av_new_packet(pkt, size);
272 
273  if(ret<0)
274  return ret;
275 
276  pkt->pos= avio_tell(s);
277 
278  ret= avio_read(s, pkt->data, size);
279  if(ret<=0)
280  av_free_packet(pkt);
281  else
282  av_shrink_packet(pkt, ret);
283 
284  return ret;
285 }
286 
288 {
289  int ret;
290  int old_size;
291  if (!pkt->size)
292  return av_get_packet(s, pkt, size);
293  old_size = pkt->size;
294  ret = av_grow_packet(pkt, size);
295  if (ret < 0)
296  return ret;
297  ret = avio_read(s, pkt->data + old_size, size);
298  av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
299  return ret;
300 }
301 
302 
303 int av_filename_number_test(const char *filename)
304 {
305  char buf[1024];
306  return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
307 }
308 
309 AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
310 {
311  AVProbeData lpd = *pd;
312  AVInputFormat *fmt1 = NULL, *fmt;
313  int score, id3 = 0;
314 
315  if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
316  int id3len = ff_id3v2_tag_len(lpd.buf);
317  if (lpd.buf_size > id3len + 16) {
318  lpd.buf += id3len;
319  lpd.buf_size -= id3len;
320  }
321  id3 = 1;
322  }
323 
324  fmt = NULL;
325  while ((fmt1 = av_iformat_next(fmt1))) {
326  if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
327  continue;
328  score = 0;
329  if (fmt1->read_probe) {
330  score = fmt1->read_probe(&lpd);
331  } else if (fmt1->extensions) {
332  if (av_match_ext(lpd.filename, fmt1->extensions)) {
333  score = 50;
334  }
335  }
336  if (score > *score_max) {
337  *score_max = score;
338  fmt = fmt1;
339  }else if (score == *score_max)
340  fmt = NULL;
341  }
342 
343  /* a hack for files with huge id3v2 tags -- try to guess by file extension. */
344  if (!fmt && is_opened && *score_max < AVPROBE_SCORE_MAX/4) {
345  while ((fmt = av_iformat_next(fmt)))
346  if (fmt->extensions && av_match_ext(lpd.filename, fmt->extensions)) {
347  *score_max = AVPROBE_SCORE_MAX/4;
348  break;
349  }
350  }
351 
352  if (!fmt && id3 && *score_max < AVPROBE_SCORE_MAX/4-1) {
353  while ((fmt = av_iformat_next(fmt)))
354  if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
355  *score_max = AVPROBE_SCORE_MAX/4-1;
356  break;
357  }
358  }
359 
360  return fmt;
361 }
362 
364  int score=0;
365  return av_probe_input_format2(pd, is_opened, &score);
366 }
367 
369 {
370  static const struct {
371  const char *name; enum CodecID id; enum AVMediaType type;
372  } fmt_id_type[] = {
373  { "aac" , CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
374  { "ac3" , CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
375  { "dts" , CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
376  { "eac3" , CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
377  { "h264" , CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
378  { "m4v" , CODEC_ID_MPEG4 , AVMEDIA_TYPE_VIDEO },
379  { "mp3" , CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
380  { "mpegvideo", CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
381  { 0 }
382  };
383  AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
384 
385  if (fmt) {
386  int i;
387  av_log(s, AV_LOG_DEBUG, "Probe with size=%d, packets=%d detected %s with score=%d\n",
388  pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets, fmt->name, score);
389  for (i = 0; fmt_id_type[i].name; i++) {
390  if (!strcmp(fmt->name, fmt_id_type[i].name)) {
391  st->codec->codec_id = fmt_id_type[i].id;
392  st->codec->codec_type = fmt_id_type[i].type;
393  break;
394  }
395  }
396  }
397  return !!fmt;
398 }
399 
400 /************************************************************/
401 /* input media file */
402 
403 #if FF_API_FORMAT_PARAMETERS
404 static AVDictionary *convert_format_parameters(AVFormatParameters *ap)
405 {
406  char buf[1024];
407  AVDictionary *opts = NULL;
408 
409  if (!ap)
410  return NULL;
411 
412  if (ap->time_base.num) {
413  snprintf(buf, sizeof(buf), "%d/%d", ap->time_base.den, ap->time_base.num);
414  av_dict_set(&opts, "framerate", buf, 0);
415  }
416  if (ap->sample_rate) {
417  snprintf(buf, sizeof(buf), "%d", ap->sample_rate);
418  av_dict_set(&opts, "sample_rate", buf, 0);
419  }
420  if (ap->channels) {
421  snprintf(buf, sizeof(buf), "%d", ap->channels);
422  av_dict_set(&opts, "channels", buf, 0);
423  }
424  if (ap->width || ap->height) {
425  snprintf(buf, sizeof(buf), "%dx%d", ap->width, ap->height);
426  av_dict_set(&opts, "video_size", buf, 0);
427  }
428  if (ap->pix_fmt != PIX_FMT_NONE) {
429  av_dict_set(&opts, "pixel_format", av_get_pix_fmt_name(ap->pix_fmt), 0);
430  }
431  if (ap->channel) {
432  snprintf(buf, sizeof(buf), "%d", ap->channel);
433  av_dict_set(&opts, "channel", buf, 0);
434  }
435  if (ap->standard) {
436  av_dict_set(&opts, "standard", ap->standard, 0);
437  }
438  if (ap->mpeg2ts_compute_pcr) {
439  av_dict_set(&opts, "mpeg2ts_compute_pcr", "1", 0);
440  }
441  if (ap->initial_pause) {
442  av_dict_set(&opts, "initial_pause", "1", 0);
443  }
444  return opts;
445 }
446 
450 int av_open_input_stream(AVFormatContext **ic_ptr,
451  AVIOContext *pb, const char *filename,
453 {
454  int err;
455  AVDictionary *opts;
456  AVFormatContext *ic;
457  AVFormatParameters default_ap;
458 
459  if(!ap){
460  ap=&default_ap;
461  memset(ap, 0, sizeof(default_ap));
462  }
463  opts = convert_format_parameters(ap);
464 
465  if(!ap->prealloced_context)
466  ic = avformat_alloc_context();
467  else
468  ic = *ic_ptr;
469  if (!ic) {
470  err = AVERROR(ENOMEM);
471  goto fail;
472  }
473  if (pb && fmt && fmt->flags & AVFMT_NOFILE)
474  av_log(ic, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
475  "will be ignored with AVFMT_NOFILE format.\n");
476  else
477  ic->pb = pb;
478 
479  if ((err = avformat_open_input(&ic, filename, fmt, &opts)) < 0)
480  goto fail;
481  ic->pb = ic->pb ? ic->pb : pb; // don't leak custom pb if it wasn't set above
482 
483 fail:
484  *ic_ptr = ic;
485  av_dict_free(&opts);
486  return err;
487 }
488 #endif
489 
491 #define PROBE_BUF_MIN 2048
492 #define PROBE_BUF_MAX (1<<20)
493 
495  const char *filename, void *logctx,
496  unsigned int offset, unsigned int max_probe_size)
497 {
498  AVProbeData pd = { filename ? filename : "", NULL, -offset };
499  unsigned char *buf = NULL;
500  int ret = 0, probe_size;
501 
502  if (!max_probe_size) {
503  max_probe_size = PROBE_BUF_MAX;
504  } else if (max_probe_size > PROBE_BUF_MAX) {
505  max_probe_size = PROBE_BUF_MAX;
506  } else if (max_probe_size < PROBE_BUF_MIN) {
507  return AVERROR(EINVAL);
508  }
509 
510  if (offset >= max_probe_size) {
511  return AVERROR(EINVAL);
512  }
513 
514  for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt;
515  probe_size = FFMIN(probe_size<<1, FFMAX(max_probe_size, probe_size+1))) {
516  int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
517  int buf_offset = (probe_size == PROBE_BUF_MIN) ? 0 : probe_size>>1;
518 
519  if (probe_size < offset) {
520  continue;
521  }
522 
523  /* read probe data */
524  buf = av_realloc(buf, probe_size + AVPROBE_PADDING_SIZE);
525  if ((ret = avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
526  /* fail if error was not end of file, otherwise, lower score */
527  if (ret != AVERROR_EOF) {
528  av_free(buf);
529  return ret;
530  }
531  score = 0;
532  ret = 0; /* error was end of file, nothing read */
533  }
534  pd.buf_size += ret;
535  pd.buf = &buf[offset];
536 
537  memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
538 
539  /* guess file format */
540  *fmt = av_probe_input_format2(&pd, 1, &score);
541  if(*fmt){
542  if(score <= AVPROBE_SCORE_MAX/4){ //this can only be true in the last iteration
543  av_log(logctx, AV_LOG_WARNING, "Format detected only with low score of %d, misdetection possible!\n", score);
544  }else
545  av_log(logctx, AV_LOG_DEBUG, "Probed with size=%d and score=%d\n", probe_size, score);
546  }
547  }
548 
549  if (!*fmt) {
550  av_free(buf);
551  return AVERROR_INVALIDDATA;
552  }
553 
554  /* rewind. reuse probe buffer to avoid seeking */
555  if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
556  av_free(buf);
557 
558  return ret;
559 }
560 
561 #if FF_API_FORMAT_PARAMETERS
562 int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
563  AVInputFormat *fmt,
564  int buf_size,
565  AVFormatParameters *ap)
566 {
567  int err;
568  AVDictionary *opts = convert_format_parameters(ap);
569 
570  if (!ap || !ap->prealloced_context)
571  *ic_ptr = NULL;
572 
573  err = avformat_open_input(ic_ptr, filename, fmt, &opts);
574 
575  av_dict_free(&opts);
576  return err;
577 }
578 #endif
579 
580 /* open input file and probe the format if necessary */
581 static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
582 {
583  int ret;
584  AVProbeData pd = {filename, NULL, 0};
585 
586  if (s->pb) {
588  if (!s->iformat)
589  return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, 0);
590  else if (s->iformat->flags & AVFMT_NOFILE)
591  return AVERROR(EINVAL);
592  return 0;
593  }
594 
595  if ( (s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
596  (!s->iformat && (s->iformat = av_probe_input_format(&pd, 0))))
597  return 0;
598 
599  if ((ret = avio_open2(&s->pb, filename, AVIO_FLAG_READ,
600  &s->interrupt_callback, options)) < 0)
601  return ret;
602  if (s->iformat)
603  return 0;
604  return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, 0);
605 }
606 
607 int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
608 {
609  AVFormatContext *s = *ps;
610  int ret = 0;
611  AVFormatParameters ap = { { 0 } };
612  AVDictionary *tmp = NULL;
613 
614  if (!s && !(s = avformat_alloc_context()))
615  return AVERROR(ENOMEM);
616  if (fmt)
617  s->iformat = fmt;
618 
619  if (options)
620  av_dict_copy(&tmp, *options, 0);
621 
622  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
623  goto fail;
624 
625  if ((ret = init_input(s, filename, &tmp)) < 0)
626  goto fail;
627 
628  /* check filename in case an image number is expected */
629  if (s->iformat->flags & AVFMT_NEEDNUMBER) {
630  if (!av_filename_number_test(filename)) {
631  ret = AVERROR(EINVAL);
632  goto fail;
633  }
634  }
635 
637  av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
638 
639  /* allocate private data */
640  if (s->iformat->priv_data_size > 0) {
641  if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
642  ret = AVERROR(ENOMEM);
643  goto fail;
644  }
645  if (s->iformat->priv_class) {
646  *(const AVClass**)s->priv_data = s->iformat->priv_class;
648  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
649  goto fail;
650  }
651  }
652 
653  /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
654  if (s->pb)
656 
657  if (s->iformat->read_header)
658  if ((ret = s->iformat->read_header(s, &ap)) < 0)
659  goto fail;
660 
661  if (s->pb && !s->data_offset)
662  s->data_offset = avio_tell(s->pb);
663 
665 
666  if (options) {
667  av_dict_free(options);
668  *options = tmp;
669  }
670  *ps = s;
671  return 0;
672 
673 fail:
674  av_dict_free(&tmp);
675  if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
676  avio_close(s->pb);
678  *ps = NULL;
679  return ret;
680 }
681 
682 /*******************************************************/
683 
684 static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
685  AVPacketList **plast_pktl){
686  AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
687  if (!pktl)
688  return NULL;
689 
690  if (*packet_buffer)
691  (*plast_pktl)->next = pktl;
692  else
693  *packet_buffer = pktl;
694 
695  /* add the packet in the buffered packet list */
696  *plast_pktl = pktl;
697  pktl->pkt= *pkt;
698  return &pktl->pkt;
699 }
700 
702 {
703  int ret, i;
704  AVStream *st;
705 
706  for(;;){
707  AVPacketList *pktl = s->raw_packet_buffer;
708 
709  if (pktl) {
710  *pkt = pktl->pkt;
711  if(s->streams[pkt->stream_index]->codec->codec_id != CODEC_ID_PROBE ||
712  !s->streams[pkt->stream_index]->probe_packets ||
714  AVProbeData *pd = &s->streams[pkt->stream_index]->probe_data;
715  av_freep(&pd->buf);
716  pd->buf_size = 0;
717  s->raw_packet_buffer = pktl->next;
719  av_free(pktl);
720  return 0;
721  }
722  }
723 
724  av_init_packet(pkt);
725  ret= s->iformat->read_packet(s, pkt);
726  if (ret < 0) {
727  if (!pktl || ret == AVERROR(EAGAIN))
728  return ret;
729  for (i = 0; i < s->nb_streams; i++)
730  s->streams[i]->probe_packets = 0;
731  continue;
732  }
733 
734  if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
735  (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
737  "Dropped corrupted packet (stream = %d)\n",
738  pkt->stream_index);
739  av_free_packet(pkt);
740  continue;
741  }
742 
743  st= s->streams[pkt->stream_index];
744 
745  switch(st->codec->codec_type){
746  case AVMEDIA_TYPE_VIDEO:
748  break;
749  case AVMEDIA_TYPE_AUDIO:
751  break;
754  break;
755  }
756 
757  if(!pktl && (st->codec->codec_id != CODEC_ID_PROBE ||
758  !st->probe_packets))
759  return ret;
760 
763 
764  if(st->codec->codec_id == CODEC_ID_PROBE){
765  AVProbeData *pd = &st->probe_data;
766  av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
767  --st->probe_packets;
768 
769  pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
770  memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
771  pd->buf_size += pkt->size;
772  memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
773 
774  if(av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
775  //FIXME we do not reduce score to 0 for the case of running out of buffer space in bytes
776  set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0);
777  if(st->codec->codec_id != CODEC_ID_PROBE){
778  pd->buf_size=0;
779  av_freep(&pd->buf);
780  av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
781  }
782  }
783  }
784  }
785 }
786 
787 /**********************************************************/
788 
793 {
794  int frame_size;
795 
796  if(enc->codec_id == CODEC_ID_VORBIS)
797  return -1;
798 
799  if (enc->frame_size <= 1) {
800  int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
801 
802  if (bits_per_sample) {
803  if (enc->channels == 0)
804  return -1;
805  frame_size = (size << 3) / (bits_per_sample * enc->channels);
806  } else {
807  /* used for example by ADPCM codecs */
808  if (enc->bit_rate == 0)
809  return -1;
810  frame_size = ((int64_t)size * 8 * enc->sample_rate) / enc->bit_rate;
811  }
812  } else {
813  frame_size = enc->frame_size;
814  }
815  return frame_size;
816 }
817 
818 
822 static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
823  AVCodecParserContext *pc, AVPacket *pkt)
824 {
825  int frame_size;
826 
827  *pnum = 0;
828  *pden = 0;
829  switch(st->codec->codec_type) {
830  case AVMEDIA_TYPE_VIDEO:
831  if (st->r_frame_rate.num) {
832  *pnum = st->r_frame_rate.den;
833  *pden = st->r_frame_rate.num;
834  } else if(st->time_base.num*1000LL > st->time_base.den) {
835  *pnum = st->time_base.num;
836  *pden = st->time_base.den;
837  }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
838  *pnum = st->codec->time_base.num;
839  *pden = st->codec->time_base.den;
840  if (pc && pc->repeat_pict) {
841  if (*pnum > INT_MAX / (1 + pc->repeat_pict))
842  *pden /= 1 + pc->repeat_pict;
843  else
844  *pnum *= 1 + pc->repeat_pict;
845  }
846  //If this codec can be interlaced or progressive then we need a parser to compute duration of a packet
847  //Thus if we have no parser in such case leave duration undefined.
848  if(st->codec->ticks_per_frame>1 && !pc){
849  *pnum = *pden = 0;
850  }
851  }
852  break;
853  case AVMEDIA_TYPE_AUDIO:
854  frame_size = get_audio_frame_size(st->codec, pkt->size);
855  if (frame_size <= 0 || st->codec->sample_rate <= 0)
856  break;
857  *pnum = frame_size;
858  *pden = st->codec->sample_rate;
859  break;
860  default:
861  break;
862  }
863 }
864 
865 static int is_intra_only(AVCodecContext *enc){
866  if(enc->codec_type == AVMEDIA_TYPE_AUDIO){
867  return 1;
868  }else if(enc->codec_type == AVMEDIA_TYPE_VIDEO){
869  switch(enc->codec_id){
870  case CODEC_ID_MJPEG:
871  case CODEC_ID_MJPEGB:
872  case CODEC_ID_LJPEG:
873  case CODEC_ID_PRORES:
874  case CODEC_ID_RAWVIDEO:
875  case CODEC_ID_DVVIDEO:
876  case CODEC_ID_HUFFYUV:
877  case CODEC_ID_FFVHUFF:
878  case CODEC_ID_ASV1:
879  case CODEC_ID_ASV2:
880  case CODEC_ID_VCR1:
881  case CODEC_ID_DNXHD:
882  case CODEC_ID_JPEG2000:
883  return 1;
884  default: break;
885  }
886  }
887  return 0;
888 }
889 
890 static void update_initial_timestamps(AVFormatContext *s, int stream_index,
891  int64_t dts, int64_t pts)
892 {
893  AVStream *st= s->streams[stream_index];
894  AVPacketList *pktl= s->packet_buffer;
895 
896  if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE)
897  return;
898 
899  st->first_dts= dts - st->cur_dts;
900  st->cur_dts= dts;
901 
902  for(; pktl; pktl= pktl->next){
903  if(pktl->pkt.stream_index != stream_index)
904  continue;
905  //FIXME think more about this check
906  if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
907  pktl->pkt.pts += st->first_dts;
908 
909  if(pktl->pkt.dts != AV_NOPTS_VALUE)
910  pktl->pkt.dts += st->first_dts;
911 
912  if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
913  st->start_time= pktl->pkt.pts;
914  }
915  if (st->start_time == AV_NOPTS_VALUE)
916  st->start_time = pts;
917 }
918 
920 {
921  AVPacketList *pktl= s->packet_buffer;
922  int64_t cur_dts= 0;
923 
924  if(st->first_dts != AV_NOPTS_VALUE){
925  cur_dts= st->first_dts;
926  for(; pktl; pktl= pktl->next){
927  if(pktl->pkt.stream_index == pkt->stream_index){
928  if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
929  break;
930  cur_dts -= pkt->duration;
931  }
932  }
933  pktl= s->packet_buffer;
934  st->first_dts = cur_dts;
935  }else if(st->cur_dts)
936  return;
937 
938  for(; pktl; pktl= pktl->next){
939  if(pktl->pkt.stream_index != pkt->stream_index)
940  continue;
941  if(pktl->pkt.pts == pktl->pkt.dts && pktl->pkt.dts == AV_NOPTS_VALUE
942  && !pktl->pkt.duration){
943  pktl->pkt.dts= cur_dts;
944  if(!st->codec->has_b_frames)
945  pktl->pkt.pts= cur_dts;
946  cur_dts += pkt->duration;
947  pktl->pkt.duration= pkt->duration;
948  }else
949  break;
950  }
951  if(st->first_dts == AV_NOPTS_VALUE)
952  st->cur_dts= cur_dts;
953 }
954 
956  AVCodecParserContext *pc, AVPacket *pkt)
957 {
958  int num, den, presentation_delayed, delay, i;
959  int64_t offset;
960 
961  if (s->flags & AVFMT_FLAG_NOFILLIN)
962  return;
963 
964  if((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
965  pkt->dts= AV_NOPTS_VALUE;
966 
967  if (st->codec->codec_id != CODEC_ID_H264 && pc && pc->pict_type == AV_PICTURE_TYPE_B)
968  //FIXME Set low_delay = 0 when has_b_frames = 1
969  st->codec->has_b_frames = 1;
970 
971  /* do we have a video B-frame ? */
972  delay= st->codec->has_b_frames;
973  presentation_delayed = 0;
974 
975  /* XXX: need has_b_frame, but cannot get it if the codec is
976  not initialized */
977  if (delay &&
978  pc && pc->pict_type != AV_PICTURE_TYPE_B)
979  presentation_delayed = 1;
980 
981  if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
982  /*&& pkt->dts-(1LL<<st->pts_wrap_bits) < pkt->pts*/){
983  pkt->dts -= 1LL<<st->pts_wrap_bits;
984  }
985 
986  // some mpeg2 in mpeg-ps lack dts (issue171 / input_file.mpg)
987  // we take the conservative approach and discard both
988  // Note, if this is misbehaving for a H.264 file then possibly presentation_delayed is not set correctly.
989  if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){
990  av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
991  pkt->dts= pkt->pts= AV_NOPTS_VALUE;
992  }
993 
994  if (pkt->duration == 0) {
995  compute_frame_duration(&num, &den, st, pc, pkt);
996  if (den && num) {
997  pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
998 
999  if(pkt->duration != 0 && s->packet_buffer)
1000  update_initial_durations(s, st, pkt);
1001  }
1002  }
1003 
1004  /* correct timestamps with byte offset if demuxers only have timestamps
1005  on packet boundaries */
1006  if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
1007  /* this will estimate bitrate based on this frame's duration and size */
1008  offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1009  if(pkt->pts != AV_NOPTS_VALUE)
1010  pkt->pts += offset;
1011  if(pkt->dts != AV_NOPTS_VALUE)
1012  pkt->dts += offset;
1013  }
1014 
1015  if (pc && pc->dts_sync_point >= 0) {
1016  // we have synchronization info from the parser
1017  int64_t den = st->codec->time_base.den * (int64_t) st->time_base.num;
1018  if (den > 0) {
1019  int64_t num = st->codec->time_base.num * (int64_t) st->time_base.den;
1020  if (pkt->dts != AV_NOPTS_VALUE) {
1021  // got DTS from the stream, update reference timestamp
1022  st->reference_dts = pkt->dts - pc->dts_ref_dts_delta * num / den;
1023  pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
1024  } else if (st->reference_dts != AV_NOPTS_VALUE) {
1025  // compute DTS based on reference timestamp
1026  pkt->dts = st->reference_dts + pc->dts_ref_dts_delta * num / den;
1027  pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
1028  }
1029  if (pc->dts_sync_point > 0)
1030  st->reference_dts = pkt->dts; // new reference
1031  }
1032  }
1033 
1034  /* This may be redundant, but it should not hurt. */
1035  if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
1036  presentation_delayed = 1;
1037 
1038 // av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64" st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc);
1039  /* interpolate PTS and DTS if they are not present */
1040  //We skip H264 currently because delay and has_b_frames are not reliably set
1041  if((delay==0 || (delay==1 && pc)) && st->codec->codec_id != CODEC_ID_H264){
1042  if (presentation_delayed) {
1043  /* DTS = decompression timestamp */
1044  /* PTS = presentation timestamp */
1045  if (pkt->dts == AV_NOPTS_VALUE)
1046  pkt->dts = st->last_IP_pts;
1047  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
1048  if (pkt->dts == AV_NOPTS_VALUE)
1049  pkt->dts = st->cur_dts;
1050 
1051  /* this is tricky: the dts must be incremented by the duration
1052  of the frame we are displaying, i.e. the last I- or P-frame */
1053  if (st->last_IP_duration == 0)
1054  st->last_IP_duration = pkt->duration;
1055  if(pkt->dts != AV_NOPTS_VALUE)
1056  st->cur_dts = pkt->dts + st->last_IP_duration;
1057  st->last_IP_duration = pkt->duration;
1058  st->last_IP_pts= pkt->pts;
1059  /* cannot compute PTS if not present (we can compute it only
1060  by knowing the future */
1061  } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){
1062  if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){
1063  int64_t old_diff= FFABS(st->cur_dts - pkt->duration - pkt->pts);
1064  int64_t new_diff= FFABS(st->cur_dts - pkt->pts);
1065  if(old_diff < new_diff && old_diff < (pkt->duration>>3)){
1066  pkt->pts += pkt->duration;
1067  // av_log(NULL, AV_LOG_DEBUG, "id:%d old:%"PRId64" new:%"PRId64" dur:%d cur:%"PRId64" size:%d\n", pkt->stream_index, old_diff, new_diff, pkt->duration, st->cur_dts, pkt->size);
1068  }
1069  }
1070 
1071  /* presentation is not delayed : PTS and DTS are the same */
1072  if(pkt->pts == AV_NOPTS_VALUE)
1073  pkt->pts = pkt->dts;
1074  update_initial_timestamps(s, pkt->stream_index, pkt->pts, pkt->pts);
1075  if(pkt->pts == AV_NOPTS_VALUE)
1076  pkt->pts = st->cur_dts;
1077  pkt->dts = pkt->pts;
1078  if(pkt->pts != AV_NOPTS_VALUE)
1079  st->cur_dts = pkt->pts + pkt->duration;
1080  }
1081  }
1082 
1083  if(pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
1084  st->pts_buffer[0]= pkt->pts;
1085  for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
1086  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
1087  if(pkt->dts == AV_NOPTS_VALUE)
1088  pkt->dts= st->pts_buffer[0];
1089  if(st->codec->codec_id == CODEC_ID_H264){ // we skipped it above so we try here
1090  update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts); // this should happen on the first packet
1091  }
1092  if(pkt->dts > st->cur_dts)
1093  st->cur_dts = pkt->dts;
1094  }
1095 
1096 // av_log(NULL, AV_LOG_ERROR, "OUTdelayed:%d/%d pts:%"PRId64", dts:%"PRId64" cur_dts:%"PRId64"\n", presentation_delayed, delay, pkt->pts, pkt->dts, st->cur_dts);
1097 
1098  /* update flags */
1099  if(is_intra_only(st->codec))
1100  pkt->flags |= AV_PKT_FLAG_KEY;
1101  else if (pc) {
1102  pkt->flags = 0;
1103  /* keyframe computation */
1104  if (pc->key_frame == 1)
1105  pkt->flags |= AV_PKT_FLAG_KEY;
1106  else if (pc->key_frame == -1 && pc->pict_type == AV_PICTURE_TYPE_I)
1107  pkt->flags |= AV_PKT_FLAG_KEY;
1108  }
1109  if (pc)
1111 }
1112 
1113 
1115 {
1116  AVStream *st;
1117  int len, ret, i;
1118 
1119  av_init_packet(pkt);
1120 
1121  for(;;) {
1122  /* select current input stream component */
1123  st = s->cur_st;
1124  if (st) {
1125  if (!st->need_parsing || !st->parser) {
1126  /* no parsing needed: we just output the packet as is */
1127  /* raw data support */
1128  *pkt = st->cur_pkt; st->cur_pkt.data= NULL;
1129  compute_pkt_fields(s, st, NULL, pkt);
1130  s->cur_st = NULL;
1131  if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
1132  (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
1133  ff_reduce_index(s, st->index);
1134  av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1135  }
1136  break;
1137  } else if (st->cur_len > 0 && st->discard < AVDISCARD_ALL) {
1138  len = av_parser_parse2(st->parser, st->codec, &pkt->data, &pkt->size,
1139  st->cur_ptr, st->cur_len,
1140  st->cur_pkt.pts, st->cur_pkt.dts,
1141  st->cur_pkt.pos);
1142  st->cur_pkt.pts = AV_NOPTS_VALUE;
1143  st->cur_pkt.dts = AV_NOPTS_VALUE;
1144  /* increment read pointer */
1145  st->cur_ptr += len;
1146  st->cur_len -= len;
1147 
1148  /* return packet if any */
1149  if (pkt->size) {
1150  got_packet:
1151  pkt->duration = 0;
1152  pkt->stream_index = st->index;
1153  pkt->pts = st->parser->pts;
1154  pkt->dts = st->parser->dts;
1155  pkt->pos = st->parser->pos;
1156  if(pkt->data == st->cur_pkt.data && pkt->size == st->cur_pkt.size){
1157  s->cur_st = NULL;
1158  pkt->destruct= st->cur_pkt.destruct;
1159  st->cur_pkt.destruct= NULL;
1160  st->cur_pkt.data = NULL;
1161  assert(st->cur_len == 0);
1162  }else{
1163  pkt->destruct = NULL;
1164  }
1165  compute_pkt_fields(s, st, st->parser, pkt);
1166 
1167  if((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY){
1168  ff_reduce_index(s, st->index);
1169  av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
1170  0, 0, AVINDEX_KEYFRAME);
1171  }
1172 
1173  break;
1174  }
1175  } else {
1176  /* free packet */
1177  av_free_packet(&st->cur_pkt);
1178  s->cur_st = NULL;
1179  }
1180  } else {
1181  AVPacket cur_pkt;
1182  /* read next packet */
1183  ret = av_read_packet(s, &cur_pkt);
1184  if (ret < 0) {
1185  if (ret == AVERROR(EAGAIN))
1186  return ret;
1187  /* return the last frames, if any */
1188  for(i = 0; i < s->nb_streams; i++) {
1189  st = s->streams[i];
1190  if (st->parser && st->need_parsing) {
1191  av_parser_parse2(st->parser, st->codec,
1192  &pkt->data, &pkt->size,
1193  NULL, 0,
1195  AV_NOPTS_VALUE);
1196  if (pkt->size)
1197  goto got_packet;
1198  }
1199  }
1200  /* no more packets: really terminate parsing */
1201  return ret;
1202  }
1203  st = s->streams[cur_pkt.stream_index];
1204  st->cur_pkt= cur_pkt;
1205 
1206  if(st->cur_pkt.pts != AV_NOPTS_VALUE &&
1207  st->cur_pkt.dts != AV_NOPTS_VALUE &&
1208  st->cur_pkt.pts < st->cur_pkt.dts){
1209  av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
1210  st->cur_pkt.stream_index,
1211  st->cur_pkt.pts,
1212  st->cur_pkt.dts,
1213  st->cur_pkt.size);
1214 // av_free_packet(&st->cur_pkt);
1215 // return -1;
1216  }
1217 
1218  if(s->debug & FF_FDEBUG_TS)
1219  av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
1220  st->cur_pkt.stream_index,
1221  st->cur_pkt.pts,
1222  st->cur_pkt.dts,
1223  st->cur_pkt.size,
1224  st->cur_pkt.duration,
1225  st->cur_pkt.flags);
1226 
1227  s->cur_st = st;
1228  st->cur_ptr = st->cur_pkt.data;
1229  st->cur_len = st->cur_pkt.size;
1230  if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
1231  st->parser = av_parser_init(st->codec->codec_id);
1232  if (!st->parser) {
1233  /* no parser available: just output the raw packets */
1235  }else if(st->need_parsing == AVSTREAM_PARSE_HEADERS){
1237  }else if(st->need_parsing == AVSTREAM_PARSE_FULL_ONCE){
1238  st->parser->flags |= PARSER_FLAG_ONCE;
1239  }
1240  }
1241  }
1242  }
1243  if(s->debug & FF_FDEBUG_TS)
1244  av_log(s, AV_LOG_DEBUG, "read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
1245  pkt->stream_index,
1246  pkt->pts,
1247  pkt->dts,
1248  pkt->size,
1249  pkt->duration,
1250  pkt->flags);
1251 
1252  return 0;
1253 }
1254 
1256 {
1257  AVPacketList *pktl = s->packet_buffer;
1258  av_assert0(pktl);
1259  *pkt = pktl->pkt;
1260  s->packet_buffer = pktl->next;
1261  av_freep(&pktl);
1262  return 0;
1263 }
1264 
1266 {
1267  const int genpts = s->flags & AVFMT_FLAG_GENPTS;
1268  int eof = 0;
1269 
1270  if (!genpts)
1271  return s->packet_buffer ? read_from_packet_buffer(s, pkt) :
1272  read_frame_internal(s, pkt);
1273 
1274  for (;;) {
1275  int ret;
1276  AVPacketList *pktl = s->packet_buffer;
1277 
1278  if (pktl) {
1279  AVPacket *next_pkt = &pktl->pkt;
1280 
1281  if (next_pkt->dts != AV_NOPTS_VALUE) {
1282  int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
1283  while (pktl && next_pkt->pts == AV_NOPTS_VALUE) {
1284  if (pktl->pkt.stream_index == next_pkt->stream_index &&
1285  (av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)) < 0) &&
1286  av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) { //not b frame
1287  next_pkt->pts = pktl->pkt.dts;
1288  }
1289  pktl = pktl->next;
1290  }
1291  pktl = s->packet_buffer;
1292  }
1293 
1294  /* read packet from packet buffer, if there is data */
1295  if (!(next_pkt->pts == AV_NOPTS_VALUE &&
1296  next_pkt->dts != AV_NOPTS_VALUE && !eof))
1297  return read_from_packet_buffer(s, pkt);
1298  }
1299 
1300  ret = read_frame_internal(s, pkt);
1301  if (ret < 0) {
1302  if (pktl && ret != AVERROR(EAGAIN)) {
1303  eof = 1;
1304  continue;
1305  } else
1306  return ret;
1307  }
1308 
1310  &s->packet_buffer_end)) < 0)
1311  return AVERROR(ENOMEM);
1312  }
1313 }
1314 
1315 /* XXX: suppress the packet queue */
1317 {
1318  AVPacketList *pktl;
1319 
1320  for(;;) {
1321  pktl = s->packet_buffer;
1322  if (!pktl)
1323  break;
1324  s->packet_buffer = pktl->next;
1325  av_free_packet(&pktl->pkt);
1326  av_free(pktl);
1327  }
1328  while(s->raw_packet_buffer){
1329  pktl = s->raw_packet_buffer;
1330  s->raw_packet_buffer = pktl->next;
1331  av_free_packet(&pktl->pkt);
1332  av_free(pktl);
1333  }
1334  s->packet_buffer_end=
1337 }
1338 
1339 /*******************************************************/
1340 /* seek support */
1341 
1343 {
1344  int first_audio_index = -1;
1345  int i;
1346  AVStream *st;
1347 
1348  if (s->nb_streams <= 0)
1349  return -1;
1350  for(i = 0; i < s->nb_streams; i++) {
1351  st = s->streams[i];
1352  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
1353  return i;
1354  }
1355  if (first_audio_index < 0 && st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
1356  first_audio_index = i;
1357  }
1358  return first_audio_index >= 0 ? first_audio_index : 0;
1359 }
1360 
1365 {
1366  AVStream *st;
1367  int i, j;
1368 
1369  flush_packet_queue(s);
1370 
1371  s->cur_st = NULL;
1372 
1373  /* for each stream, reset read state */
1374  for(i = 0; i < s->nb_streams; i++) {
1375  st = s->streams[i];
1376 
1377  if (st->parser) {
1378  av_parser_close(st->parser);
1379  st->parser = NULL;
1380  av_free_packet(&st->cur_pkt);
1381  }
1383  st->cur_dts = AV_NOPTS_VALUE; /* we set the current DTS to an unspecified origin */
1385  /* fail safe */
1386  st->cur_ptr = NULL;
1387  st->cur_len = 0;
1388 
1390 
1391  for(j=0; j<MAX_REORDER_DELAY+1; j++)
1392  st->pts_buffer[j]= AV_NOPTS_VALUE;
1393  }
1394 }
1395 
1396 #if FF_API_SEEK_PUBLIC
1397 void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1398 {
1399  ff_update_cur_dts(s, ref_st, timestamp);
1400 }
1401 #endif
1402 
1403 void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1404 {
1405  int i;
1406 
1407  for(i = 0; i < s->nb_streams; i++) {
1408  AVStream *st = s->streams[i];
1409 
1410  st->cur_dts = av_rescale(timestamp,
1411  st->time_base.den * (int64_t)ref_st->time_base.num,
1412  st->time_base.num * (int64_t)ref_st->time_base.den);
1413  }
1414 }
1415 
1416 void ff_reduce_index(AVFormatContext *s, int stream_index)
1417 {
1418  AVStream *st= s->streams[stream_index];
1419  unsigned int max_entries= s->max_index_size / sizeof(AVIndexEntry);
1420 
1421  if((unsigned)st->nb_index_entries >= max_entries){
1422  int i;
1423  for(i=0; 2*i<st->nb_index_entries; i++)
1424  st->index_entries[i]= st->index_entries[2*i];
1425  st->nb_index_entries= i;
1426  }
1427 }
1428 
1429 int ff_add_index_entry(AVIndexEntry **index_entries,
1430  int *nb_index_entries,
1431  unsigned int *index_entries_allocated_size,
1432  int64_t pos, int64_t timestamp, int size, int distance, int flags)
1433 {
1434  AVIndexEntry *entries, *ie;
1435  int index;
1436 
1437  if((unsigned)*nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
1438  return -1;
1439 
1440  entries = av_fast_realloc(*index_entries,
1441  index_entries_allocated_size,
1442  (*nb_index_entries + 1) *
1443  sizeof(AVIndexEntry));
1444  if(!entries)
1445  return -1;
1446 
1447  *index_entries= entries;
1448 
1449  index= ff_index_search_timestamp(*index_entries, *nb_index_entries, timestamp, AVSEEK_FLAG_ANY);
1450 
1451  if(index<0){
1452  index= (*nb_index_entries)++;
1453  ie= &entries[index];
1454  assert(index==0 || ie[-1].timestamp < timestamp);
1455  }else{
1456  ie= &entries[index];
1457  if(ie->timestamp != timestamp){
1458  if(ie->timestamp <= timestamp)
1459  return -1;
1460  memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(*nb_index_entries - index));
1461  (*nb_index_entries)++;
1462  }else if(ie->pos == pos && distance < ie->min_distance) //do not reduce the distance
1463  distance= ie->min_distance;
1464  }
1465 
1466  ie->pos = pos;
1467  ie->timestamp = timestamp;
1468  ie->min_distance= distance;
1469  ie->size= size;
1470  ie->flags = flags;
1471 
1472  return index;
1473 }
1474 
1476  int64_t pos, int64_t timestamp, int size, int distance, int flags)
1477 {
1479  &st->index_entries_allocated_size, pos,
1480  timestamp, size, distance, flags);
1481 }
1482 
1483 int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1484  int64_t wanted_timestamp, int flags)
1485 {
1486  int a, b, m;
1487  int64_t timestamp;
1488 
1489  a = - 1;
1490  b = nb_entries;
1491 
1492  //optimize appending index entries at the end
1493  if(b && entries[b-1].timestamp < wanted_timestamp)
1494  a= b-1;
1495 
1496  while (b - a > 1) {
1497  m = (a + b) >> 1;
1498  timestamp = entries[m].timestamp;
1499  if(timestamp >= wanted_timestamp)
1500  b = m;
1501  if(timestamp <= wanted_timestamp)
1502  a = m;
1503  }
1504  m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
1505 
1506  if(!(flags & AVSEEK_FLAG_ANY)){
1507  while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){
1508  m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1509  }
1510  }
1511 
1512  if(m == nb_entries)
1513  return -1;
1514  return m;
1515 }
1516 
1517 int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
1518  int flags)
1519 {
1521  wanted_timestamp, flags);
1522 }
1523 
1524 #if FF_API_SEEK_PUBLIC
1525 int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
1526  return ff_seek_frame_binary(s, stream_index, target_ts, flags);
1527 }
1528 #endif
1529 
1530 int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1531 {
1532  AVInputFormat *avif= s->iformat;
1533  int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
1534  int64_t ts_min, ts_max, ts;
1535  int index;
1536  int64_t ret;
1537  AVStream *st;
1538 
1539  if (stream_index < 0)
1540  return -1;
1541 
1542  av_dlog(s, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
1543 
1544  ts_max=
1545  ts_min= AV_NOPTS_VALUE;
1546  pos_limit= -1; //gcc falsely says it may be uninitialized
1547 
1548  st= s->streams[stream_index];
1549  if(st->index_entries){
1550  AVIndexEntry *e;
1551 
1552  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()
1553  index= FFMAX(index, 0);
1554  e= &st->index_entries[index];
1555 
1556  if(e->timestamp <= target_ts || e->pos == e->min_distance){
1557  pos_min= e->pos;
1558  ts_min= e->timestamp;
1559  av_dlog(s, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
1560  pos_min,ts_min);
1561  }else{
1562  assert(index==0);
1563  }
1564 
1565  index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
1566  assert(index < st->nb_index_entries);
1567  if(index >= 0){
1568  e= &st->index_entries[index];
1569  assert(e->timestamp >= target_ts);
1570  pos_max= e->pos;
1571  ts_max= e->timestamp;
1572  pos_limit= pos_max - e->min_distance;
1573  av_dlog(s, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64" dts_max=%"PRId64"\n",
1574  pos_max,pos_limit, ts_max);
1575  }
1576  }
1577 
1578  pos= ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp);
1579  if(pos<0)
1580  return -1;
1581 
1582  /* do the seek */
1583  if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
1584  return ret;
1585 
1586  ff_update_cur_dts(s, st, ts);
1587 
1588  return 0;
1589 }
1590 
1591 #if FF_API_SEEK_PUBLIC
1592 int64_t av_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1593  int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1594  int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret,
1595  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
1596 {
1597  return ff_gen_search(s, stream_index, target_ts, pos_min, pos_max,
1598  pos_limit, ts_min, ts_max, flags, ts_ret,
1599  read_timestamp);
1600 }
1601 #endif
1602 
1603 int64_t ff_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1604  int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1605  int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret,
1606  int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
1607 {
1608  int64_t pos, ts;
1609  int64_t start_pos, filesize;
1610  int no_change;
1611 
1612  av_dlog(s, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
1613 
1614  if(ts_min == AV_NOPTS_VALUE){
1615  pos_min = s->data_offset;
1616  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1617  if (ts_min == AV_NOPTS_VALUE)
1618  return -1;
1619  }
1620 
1621  if(ts_max == AV_NOPTS_VALUE){
1622  int step= 1024;
1623  filesize = avio_size(s->pb);
1624  pos_max = filesize - 1;
1625  do{
1626  pos_max -= step;
1627  ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step);
1628  step += step;
1629  }while(ts_max == AV_NOPTS_VALUE && pos_max >= step);
1630  if (ts_max == AV_NOPTS_VALUE)
1631  return -1;
1632 
1633  for(;;){
1634  int64_t tmp_pos= pos_max + 1;
1635  int64_t tmp_ts= read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
1636  if(tmp_ts == AV_NOPTS_VALUE)
1637  break;
1638  ts_max= tmp_ts;
1639  pos_max= tmp_pos;
1640  if(tmp_pos >= filesize)
1641  break;
1642  }
1643  pos_limit= pos_max;
1644  }
1645 
1646  if(ts_min > ts_max){
1647  return -1;
1648  }else if(ts_min == ts_max){
1649  pos_limit= pos_min;
1650  }
1651 
1652  no_change=0;
1653  while (pos_min < pos_limit) {
1654  av_dlog(s, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64" dts_max=%"PRId64"\n",
1655  pos_min, pos_max, ts_min, ts_max);
1656  assert(pos_limit <= pos_max);
1657 
1658  if(no_change==0){
1659  int64_t approximate_keyframe_distance= pos_max - pos_limit;
1660  // interpolate position (better than dichotomy)
1661  pos = av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
1662  + pos_min - approximate_keyframe_distance;
1663  }else if(no_change==1){
1664  // bisection, if interpolation failed to change min or max pos last time
1665  pos = (pos_min + pos_limit)>>1;
1666  }else{
1667  /* linear search if bisection failed, can only happen if there
1668  are very few or no keyframes between min/max */
1669  pos=pos_min;
1670  }
1671  if(pos <= pos_min)
1672  pos= pos_min + 1;
1673  else if(pos > pos_limit)
1674  pos= pos_limit;
1675  start_pos= pos;
1676 
1677  ts = read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1
1678  if(pos == pos_max)
1679  no_change++;
1680  else
1681  no_change=0;
1682  av_dlog(s, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64" target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
1683  pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts,
1684  pos_limit, start_pos, no_change);
1685  if(ts == AV_NOPTS_VALUE){
1686  av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
1687  return -1;
1688  }
1689  assert(ts != AV_NOPTS_VALUE);
1690  if (target_ts <= ts) {
1691  pos_limit = start_pos - 1;
1692  pos_max = pos;
1693  ts_max = ts;
1694  }
1695  if (target_ts >= ts) {
1696  pos_min = pos;
1697  ts_min = ts;
1698  }
1699  }
1700 
1701  pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
1702  ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
1703  pos_min = pos;
1704  ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1705  pos_min++;
1706  ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
1707  av_dlog(s, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
1708  pos, ts_min, target_ts, ts_max);
1709  *ts_ret= ts;
1710  return pos;
1711 }
1712 
1713 static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
1714  int64_t pos_min, pos_max;
1715 #if 0
1716  AVStream *st;
1717 
1718  if (stream_index < 0)
1719  return -1;
1720 
1721  st= s->streams[stream_index];
1722 #endif
1723 
1724  pos_min = s->data_offset;
1725  pos_max = avio_size(s->pb) - 1;
1726 
1727  if (pos < pos_min) pos= pos_min;
1728  else if(pos > pos_max) pos= pos_max;
1729 
1730  avio_seek(s->pb, pos, SEEK_SET);
1731 
1732 #if 0
1733  av_update_cur_dts(s, st, ts);
1734 #endif
1735  return 0;
1736 }
1737 
1739  int stream_index, int64_t timestamp, int flags)
1740 {
1741  int index;
1742  int64_t ret;
1743  AVStream *st;
1744  AVIndexEntry *ie;
1745 
1746  st = s->streams[stream_index];
1747 
1748  index = av_index_search_timestamp(st, timestamp, flags);
1749 
1750  if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
1751  return -1;
1752 
1753  if(index < 0 || index==st->nb_index_entries-1){
1754  AVPacket pkt;
1755 
1756  if(st->nb_index_entries){
1757  assert(st->index_entries);
1758  ie= &st->index_entries[st->nb_index_entries-1];
1759  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1760  return ret;
1761  ff_update_cur_dts(s, st, ie->timestamp);
1762  }else{
1763  if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
1764  return ret;
1765  }
1766  for (;;) {
1767  int read_status;
1768  do{
1769  read_status = av_read_frame(s, &pkt);
1770  } while (read_status == AVERROR(EAGAIN));
1771  if (read_status < 0)
1772  break;
1773  av_free_packet(&pkt);
1774  if(stream_index == pkt.stream_index){
1775  if((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
1776  break;
1777  }
1778  }
1779  index = av_index_search_timestamp(st, timestamp, flags);
1780  }
1781  if (index < 0)
1782  return -1;
1783 
1785  if (s->iformat->read_seek){
1786  if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
1787  return 0;
1788  }
1789  ie = &st->index_entries[index];
1790  if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
1791  return ret;
1792  ff_update_cur_dts(s, st, ie->timestamp);
1793 
1794  return 0;
1795 }
1796 
1797 int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
1798 {
1799  int ret;
1800  AVStream *st;
1801 
1802  if (flags & AVSEEK_FLAG_BYTE) {
1803  if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
1804  return -1;
1806  return seek_frame_byte(s, stream_index, timestamp, flags);
1807  }
1808 
1809  if(stream_index < 0){
1810  stream_index= av_find_default_stream_index(s);
1811  if(stream_index < 0)
1812  return -1;
1813 
1814  st= s->streams[stream_index];
1815  /* timestamp for default must be expressed in AV_TIME_BASE units */
1816  timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
1817  }
1818 
1819  /* first, we try the format specific seek */
1820  if (s->iformat->read_seek) {
1822  ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
1823  } else
1824  ret = -1;
1825  if (ret >= 0) {
1826  return 0;
1827  }
1828 
1829  if (s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
1831  return ff_seek_frame_binary(s, stream_index, timestamp, flags);
1832  } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
1834  return seek_frame_generic(s, stream_index, timestamp, flags);
1835  }
1836  else
1837  return -1;
1838 }
1839 
1840 int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
1841 {
1842  if(min_ts > ts || max_ts < ts)
1843  return -1;
1844 
1845  if (s->iformat->read_seek2) {
1847  return s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
1848  }
1849 
1850  if(s->iformat->read_timestamp){
1851  //try to seek via read_timestamp()
1852  }
1853 
1854  //Fallback to old API if new is not implemented but old is
1855  //Note the old has somewat different sematics
1856  if(s->iformat->read_seek || 1)
1857  return av_seek_frame(s, stream_index, ts, flags | ((uint64_t)ts - min_ts > (uint64_t)max_ts - ts ? AVSEEK_FLAG_BACKWARD : 0));
1858 
1859  // try some generic seek like seek_frame_generic() but with new ts semantics
1860 }
1861 
1862 /*******************************************************/
1863 
1870 {
1871  int i;
1872  AVStream *st;
1873 
1874  for(i = 0;i < ic->nb_streams; i++) {
1875  st = ic->streams[i];
1876  if (st->duration != AV_NOPTS_VALUE)
1877  return 1;
1878  }
1879  return 0;
1880 }
1881 
1888 {
1889  int64_t start_time, start_time1, end_time, end_time1;
1890  int64_t duration, duration1, filesize;
1891  int i;
1892  AVStream *st;
1893 
1894  start_time = INT64_MAX;
1895  end_time = INT64_MIN;
1896  duration = INT64_MIN;
1897  for(i = 0;i < ic->nb_streams; i++) {
1898  st = ic->streams[i];
1899  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
1900  start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
1901  start_time = FFMIN(start_time, start_time1);
1902  if (st->duration != AV_NOPTS_VALUE) {
1903  end_time1 = start_time1
1905  end_time = FFMAX(end_time, end_time1);
1906  }
1907  }
1908  if (st->duration != AV_NOPTS_VALUE) {
1909  duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
1910  duration = FFMAX(duration, duration1);
1911  }
1912  }
1913  if (start_time != INT64_MAX) {
1914  ic->start_time = start_time;
1915  if (end_time != INT64_MIN)
1916  duration = FFMAX(duration, end_time - start_time);
1917  }
1918  if (duration != INT64_MIN) {
1919  ic->duration = duration;
1920  if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
1921  /* compute the bitrate */
1922  ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
1923  (double)ic->duration;
1924  }
1925  }
1926 }
1927 
1929 {
1930  int i;
1931  AVStream *st;
1932 
1934  for(i = 0;i < ic->nb_streams; i++) {
1935  st = ic->streams[i];
1936  if (st->start_time == AV_NOPTS_VALUE) {
1937  if(ic->start_time != AV_NOPTS_VALUE)
1939  if(ic->duration != AV_NOPTS_VALUE)
1941  }
1942  }
1943 }
1944 
1946 {
1947  int64_t filesize, duration;
1948  int bit_rate, i;
1949  AVStream *st;
1950 
1951  /* if bit_rate is already set, we believe it */
1952  if (ic->bit_rate <= 0) {
1953  bit_rate = 0;
1954  for(i=0;i<ic->nb_streams;i++) {
1955  st = ic->streams[i];
1956  if (st->codec->bit_rate > 0)
1957  bit_rate += st->codec->bit_rate;
1958  }
1959  ic->bit_rate = bit_rate;
1960  }
1961 
1962  /* if duration is already set, we believe it */
1963  if (ic->duration == AV_NOPTS_VALUE &&
1964  ic->bit_rate != 0) {
1965  filesize = ic->pb ? avio_size(ic->pb) : 0;
1966  if (filesize > 0) {
1967  for(i = 0; i < ic->nb_streams; i++) {
1968  st = ic->streams[i];
1969  duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
1970  if (st->duration == AV_NOPTS_VALUE)
1971  st->duration = duration;
1972  }
1973  }
1974  }
1975 }
1976 
1977 #define DURATION_MAX_READ_SIZE 250000
1978 #define DURATION_MAX_RETRY 3
1979 
1980 /* only usable for MPEG-PS streams */
1981 static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
1982 {
1983  AVPacket pkt1, *pkt = &pkt1;
1984  AVStream *st;
1985  int read_size, i, ret;
1986  int64_t end_time;
1987  int64_t filesize, offset, duration;
1988  int retry=0;
1989 
1990  ic->cur_st = NULL;
1991 
1992  /* flush packet queue */
1993  flush_packet_queue(ic);
1994 
1995  for (i=0; i<ic->nb_streams; i++) {
1996  st = ic->streams[i];
1997  if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
1998  av_log(st->codec, AV_LOG_WARNING, "start time is not set in estimate_timings_from_pts\n");
1999 
2000  if (st->parser) {
2001  av_parser_close(st->parser);
2002  st->parser= NULL;
2003  av_free_packet(&st->cur_pkt);
2004  }
2005  }
2006 
2007  /* estimate the end time (duration) */
2008  /* XXX: may need to support wrapping */
2009  filesize = ic->pb ? avio_size(ic->pb) : 0;
2010  end_time = AV_NOPTS_VALUE;
2011  do{
2012  offset = filesize - (DURATION_MAX_READ_SIZE<<retry);
2013  if (offset < 0)
2014  offset = 0;
2015 
2016  avio_seek(ic->pb, offset, SEEK_SET);
2017  read_size = 0;
2018  for(;;) {
2019  if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))
2020  break;
2021 
2022  do {
2023  ret = av_read_packet(ic, pkt);
2024  } while(ret == AVERROR(EAGAIN));
2025  if (ret != 0)
2026  break;
2027  read_size += pkt->size;
2028  st = ic->streams[pkt->stream_index];
2029  if (pkt->pts != AV_NOPTS_VALUE &&
2030  (st->start_time != AV_NOPTS_VALUE ||
2031  st->first_dts != AV_NOPTS_VALUE)) {
2032  duration = end_time = pkt->pts;
2033  if (st->start_time != AV_NOPTS_VALUE)
2034  duration -= st->start_time;
2035  else
2036  duration -= st->first_dts;
2037  if (duration < 0)
2038  duration += 1LL<<st->pts_wrap_bits;
2039  if (duration > 0) {
2040  if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
2041  st->duration = duration;
2042  }
2043  }
2044  av_free_packet(pkt);
2045  }
2046  }while( end_time==AV_NOPTS_VALUE
2047  && filesize > (DURATION_MAX_READ_SIZE<<retry)
2048  && ++retry <= DURATION_MAX_RETRY);
2049 
2051 
2052  avio_seek(ic->pb, old_offset, SEEK_SET);
2053  for (i=0; i<ic->nb_streams; i++) {
2054  st= ic->streams[i];
2055  st->cur_dts= st->first_dts;
2058  }
2059 }
2060 
2061 static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
2062 {
2063  int64_t file_size;
2064 
2065  /* get the file size, if possible */
2066  if (ic->iformat->flags & AVFMT_NOFILE) {
2067  file_size = 0;
2068  } else {
2069  file_size = avio_size(ic->pb);
2070  file_size = FFMAX(0, file_size);
2071  }
2072 
2073  if ((!strcmp(ic->iformat->name, "mpeg") ||
2074  !strcmp(ic->iformat->name, "mpegts")) &&
2075  file_size && ic->pb->seekable) {
2076  /* get accurate estimate from the PTSes */
2077  estimate_timings_from_pts(ic, old_offset);
2078  } else if (has_duration(ic)) {
2079  /* at least one component has timings - we use them for all
2080  the components */
2082  } else {
2083  av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
2084  /* less precise: use bitrate info */
2086  }
2088 
2089  {
2090  int i;
2091  AVStream av_unused *st;
2092  for(i = 0;i < ic->nb_streams; i++) {
2093  st = ic->streams[i];
2094  av_dlog(ic, "%d: start_time: %0.3f duration: %0.3f\n", i,
2095  (double) st->start_time / AV_TIME_BASE,
2096  (double) st->duration / AV_TIME_BASE);
2097  }
2098  av_dlog(ic, "stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
2099  (double) ic->start_time / AV_TIME_BASE,
2100  (double) ic->duration / AV_TIME_BASE,
2101  ic->bit_rate / 1000);
2102  }
2103 }
2104 
2106 {
2107  int val;
2108  switch (avctx->codec_type) {
2109  case AVMEDIA_TYPE_AUDIO:
2110  val = avctx->sample_rate && avctx->channels && avctx->sample_fmt != AV_SAMPLE_FMT_NONE;
2111  if (!avctx->frame_size &&
2112  (avctx->codec_id == CODEC_ID_VORBIS ||
2113  avctx->codec_id == CODEC_ID_AAC ||
2114  avctx->codec_id == CODEC_ID_MP1 ||
2115  avctx->codec_id == CODEC_ID_MP2 ||
2116  avctx->codec_id == CODEC_ID_MP3 ||
2117  avctx->codec_id == CODEC_ID_CELT))
2118  return 0;
2119  break;
2120  case AVMEDIA_TYPE_VIDEO:
2121  val = avctx->width && avctx->pix_fmt != PIX_FMT_NONE;
2122  break;
2123  default:
2124  val = 1;
2125  break;
2126  }
2127  return avctx->codec_id != CODEC_ID_NONE && val != 0;
2128 }
2129 
2131 {
2132  return st->codec->codec_id != CODEC_ID_H264 ||
2133  st->info->nb_decoded_frames >= 6;
2134 }
2135 
2136 /* returns 1 or 0 if or if not decoded data was returned, or a negative error */
2138 {
2139  AVCodec *codec;
2140  int got_picture = 1, ret = 0;
2141  AVFrame picture;
2142  AVPacket pkt = *avpkt;
2143 
2144  if (!avcodec_is_open(st->codec)) {
2145  AVDictionary *thread_opt = NULL;
2146 
2147  codec = st->codec->codec ? st->codec->codec :
2149 
2150  if (!codec)
2151  return -1;
2152 
2153  /* force thread count to 1 since the h264 decoder will not extract SPS
2154  * and PPS to extradata during multi-threaded decoding */
2155  av_dict_set(options ? options : &thread_opt, "threads", "1", 0);
2156  ret = avcodec_open2(st->codec, codec, options ? options : &thread_opt);
2157  if (!options)
2158  av_dict_free(&thread_opt);
2159  if (ret < 0)
2160  return ret;
2161  }
2162 
2163  while ((pkt.size > 0 || (!pkt.data && got_picture)) &&
2164  ret >= 0 &&
2165  (!has_codec_parameters(st->codec) ||
2168  got_picture = 0;
2169  avcodec_get_frame_defaults(&picture);
2170  switch(st->codec->codec_type) {
2171  case AVMEDIA_TYPE_VIDEO:
2172  ret = avcodec_decode_video2(st->codec, &picture,
2173  &got_picture, &pkt);
2174  break;
2175  case AVMEDIA_TYPE_AUDIO:
2176  ret = avcodec_decode_audio4(st->codec, &picture, &got_picture, &pkt);
2177  break;
2178  default:
2179  break;
2180  }
2181  if (ret >= 0) {
2182  if (got_picture)
2183  st->info->nb_decoded_frames++;
2184  pkt.data += ret;
2185  pkt.size -= ret;
2186  ret = got_picture;
2187  }
2188  }
2189  return ret;
2190 }
2191 
2192 unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
2193 {
2194  while (tags->id != CODEC_ID_NONE) {
2195  if (tags->id == id)
2196  return tags->tag;
2197  tags++;
2198  }
2199  return 0;
2200 }
2201 
2202 enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
2203 {
2204  int i;
2205  for(i=0; tags[i].id != CODEC_ID_NONE;i++) {
2206  if(tag == tags[i].tag)
2207  return tags[i].id;
2208  }
2209  for(i=0; tags[i].id != CODEC_ID_NONE; i++) {
2210  if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
2211  return tags[i].id;
2212  }
2213  return CODEC_ID_NONE;
2214 }
2215 
2216 unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum CodecID id)
2217 {
2218  int i;
2219  for(i=0; tags && tags[i]; i++){
2220  int tag= ff_codec_get_tag(tags[i], id);
2221  if(tag) return tag;
2222  }
2223  return 0;
2224 }
2225 
2226 enum CodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
2227 {
2228  int i;
2229  for(i=0; tags && tags[i]; i++){
2230  enum CodecID id= ff_codec_get_id(tags[i], tag);
2231  if(id!=CODEC_ID_NONE) return id;
2232  }
2233  return CODEC_ID_NONE;
2234 }
2235 
2237 {
2238  unsigned int i, j;
2239  int64_t max_time = s->duration + ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
2240 
2241  for (i = 0; i < s->nb_chapters; i++)
2242  if (s->chapters[i]->end == AV_NOPTS_VALUE) {
2243  AVChapter *ch = s->chapters[i];
2244  int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, ch->time_base)
2245  : INT64_MAX;
2246 
2247  for (j = 0; j < s->nb_chapters; j++) {
2248  AVChapter *ch1 = s->chapters[j];
2249  int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, ch->time_base);
2250  if (j != i && next_start > ch->start && next_start < end)
2251  end = next_start;
2252  }
2253  ch->end = (end == INT64_MAX) ? ch->start : end;
2254  }
2255 }
2256 
2257 static int get_std_framerate(int i){
2258  if(i<60*12) return i*1001;
2259  else return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
2260 }
2261 
2262 /*
2263  * Is the time base unreliable.
2264  * This is a heuristic to balance between quick acceptance of the values in
2265  * the headers vs. some extra checks.
2266  * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2267  * MPEG-2 commonly misuses field repeat flags to store different framerates.
2268  * And there are "variable" fps files this needs to detect as well.
2269  */
2271  if( c->time_base.den >= 101L*c->time_base.num
2272  || c->time_base.den < 5L*c->time_base.num
2273 /* || c->codec_tag == AV_RL32("DIVX")
2274  || c->codec_tag == AV_RL32("XVID")*/
2275  || c->codec_id == CODEC_ID_MPEG2VIDEO
2276  || c->codec_id == CODEC_ID_H264
2277  )
2278  return 1;
2279  return 0;
2280 }
2281 
2282 #if FF_API_FORMAT_PARAMETERS
2283 int av_find_stream_info(AVFormatContext *ic)
2284 {
2285  return avformat_find_stream_info(ic, NULL);
2286 }
2287 #endif
2288 
2290 {
2291  int i, count, ret, read_size, j;
2292  AVStream *st;
2293  AVPacket pkt1, *pkt;
2294  int64_t old_offset = avio_tell(ic->pb);
2295  int orig_nb_streams = ic->nb_streams; // new streams might appear, no options for those
2296 
2297  for(i=0;i<ic->nb_streams;i++) {
2298  AVCodec *codec;
2299  AVDictionary *thread_opt = NULL;
2300  st = ic->streams[i];
2301 
2302  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2304 /* if(!st->time_base.num)
2305  st->time_base= */
2306  if(!st->codec->time_base.num)
2307  st->codec->time_base= st->time_base;
2308  }
2309  //only for the split stuff
2310  if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
2311  st->parser = av_parser_init(st->codec->codec_id);
2312  if(st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser){
2314  }
2315  }
2316  codec = st->codec->codec ? st->codec->codec :
2318 
2319  /* force thread count to 1 since the h264 decoder will not extract SPS
2320  * and PPS to extradata during multi-threaded decoding */
2321  av_dict_set(options ? &options[i] : &thread_opt, "threads", "1", 0);
2322 
2323  /* Ensure that subtitle_header is properly set. */
2325  && codec && !st->codec->codec)
2326  avcodec_open2(st->codec, codec, options ? &options[i]
2327  : &thread_opt);
2328 
2329  //try to just open decoders, in case this is enough to get parameters
2330  if(!has_codec_parameters(st->codec)){
2331  if (codec && !st->codec->codec)
2332  avcodec_open2(st->codec, codec, options ? &options[i]
2333  : &thread_opt);
2334  }
2335  if (!options)
2336  av_dict_free(&thread_opt);
2337  }
2338 
2339  for (i=0; i<ic->nb_streams; i++) {
2340  ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
2341  }
2342 
2343  count = 0;
2344  read_size = 0;
2345  for(;;) {
2347  ret= AVERROR_EXIT;
2348  av_log(ic, AV_LOG_DEBUG, "interrupted\n");
2349  break;
2350  }
2351 
2352  /* check if one codec still needs to be handled */
2353  for(i=0;i<ic->nb_streams;i++) {
2354  int fps_analyze_framecount = 20;
2355 
2356  st = ic->streams[i];
2357  if (!has_codec_parameters(st->codec))
2358  break;
2359  /* if the timebase is coarse (like the usual millisecond precision
2360  of mkv), we need to analyze more frames to reliably arrive at
2361  the correct fps */
2362  if (av_q2d(st->time_base) > 0.0005)
2363  fps_analyze_framecount *= 2;
2364  if (ic->fps_probe_size >= 0)
2365  fps_analyze_framecount = ic->fps_probe_size;
2366  /* variable fps and no guess at the real fps */
2367  if( tb_unreliable(st->codec) && !(st->r_frame_rate.num && st->avg_frame_rate.num)
2368  && st->info->duration_count < fps_analyze_framecount
2369  && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2370  break;
2371  if(st->parser && st->parser->parser->split && !st->codec->extradata)
2372  break;
2373  if(st->first_dts == AV_NOPTS_VALUE)
2374  break;
2375  }
2376  if (i == ic->nb_streams) {
2377  /* NOTE: if the format has no header, then we need to read
2378  some packets to get most of the streams, so we cannot
2379  stop here */
2380  if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
2381  /* if we found the info for all the codecs, we can stop */
2382  ret = count;
2383  av_log(ic, AV_LOG_DEBUG, "All info found\n");
2384  break;
2385  }
2386  }
2387  /* we did not get all the codec info, but we read too much data */
2388  if (read_size >= ic->probesize) {
2389  ret = count;
2390  av_log(ic, AV_LOG_DEBUG, "Probe buffer size limit %d reached\n", ic->probesize);
2391  break;
2392  }
2393 
2394  /* NOTE: a new stream can be added there if no header in file
2395  (AVFMTCTX_NOHEADER) */
2396  ret = read_frame_internal(ic, &pkt1);
2397  if (ret == AVERROR(EAGAIN))
2398  continue;
2399 
2400  if (ret < 0) {
2401  /* EOF or error*/
2402  AVPacket empty_pkt = { 0 };
2403  int err;
2404  av_init_packet(&empty_pkt);
2405 
2406  ret = -1; /* we could not have all the codec parameters before EOF */
2407  for(i=0;i<ic->nb_streams;i++) {
2408  st = ic->streams[i];
2409 
2410  /* flush the decoders */
2411  do {
2412  err = try_decode_frame(st, &empty_pkt,
2413  (options && i < orig_nb_streams) ?
2414  &options[i] : NULL);
2415  } while (err > 0 && !has_codec_parameters(st->codec));
2416 
2417  if (err < 0) {
2418  av_log(ic, AV_LOG_WARNING,
2419  "decoding for stream %d failed\n", st->index);
2420  } else if (!has_codec_parameters(st->codec)){
2421  char buf[256];
2422  avcodec_string(buf, sizeof(buf), st->codec, 0);
2423  av_log(ic, AV_LOG_WARNING,
2424  "Could not find codec parameters (%s)\n", buf);
2425  } else {
2426  ret = 0;
2427  }
2428  }
2429  break;
2430  }
2431 
2432  pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
2433  if ((ret = av_dup_packet(pkt)) < 0)
2434  goto find_stream_info_err;
2435 
2436  read_size += pkt->size;
2437 
2438  st = ic->streams[pkt->stream_index];
2439  if (st->codec_info_nb_frames>1) {
2441  av_log(ic, AV_LOG_WARNING, "max_analyze_duration reached\n");
2442  break;
2443  }
2444  st->info->codec_info_duration += pkt->duration;
2445  }
2446  {
2447  int64_t last = st->info->last_dts;
2448 
2449  if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && pkt->dts > last){
2450  int64_t duration= pkt->dts - last;
2451  double dur= duration * av_q2d(st->time_base);
2452 
2453 // if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2454 // av_log(NULL, AV_LOG_ERROR, "%f\n", dur);
2455  if (st->info->duration_count < 2)
2456  memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
2457  for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
2458  int framerate= get_std_framerate(i);
2459  int ticks= lrintf(dur*framerate/(1001*12));
2460  double error = dur - (double)ticks*1001*12 / framerate;
2461  st->info->duration_error[i] += error*error;
2462  }
2463  st->info->duration_count++;
2464  // ignore the first 4 values, they might have some random jitter
2465  if (st->info->duration_count > 3)
2466  st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
2467  }
2468  if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
2469  st->info->last_dts = pkt->dts;
2470  }
2471  if(st->parser && st->parser->parser->split && !st->codec->extradata){
2472  int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
2473  if (i > 0 && i < FF_MAX_EXTRADATA_SIZE) {
2474  st->codec->extradata_size= i;
2476  if (!st->codec->extradata)
2477  return AVERROR(ENOMEM);
2478  memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size);
2479  memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
2480  }
2481  }
2482 
2483  /* if still no information, we try to open the codec and to
2484  decompress the frame. We try to avoid that in most cases as
2485  it takes longer and uses more memory. For MPEG-4, we need to
2486  decompress for QuickTime.
2487 
2488  If CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2489  least one frame of codec data, this makes sure the codec initializes
2490  the channel configuration and does not only trust the values from the container.
2491  */
2492  try_decode_frame(st, pkt, (options && i < orig_nb_streams ) ? &options[i] : NULL);
2493 
2494  st->codec_info_nb_frames++;
2495  count++;
2496  }
2497 
2498  // close codecs which were opened in try_decode_frame()
2499  for(i=0;i<ic->nb_streams;i++) {
2500  st = ic->streams[i];
2501  avcodec_close(st->codec);
2502  }
2503  for(i=0;i<ic->nb_streams;i++) {
2504  st = ic->streams[i];
2507  (st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
2508  st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
2509  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2510  // the check for tb_unreliable() is not completely correct, since this is not about handling
2511  // a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2512  // ipmovie.c produces.
2513  if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > 1 && !st->r_frame_rate.num)
2514  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);
2515  if (st->info->duration_count && !st->r_frame_rate.num
2516  && tb_unreliable(st->codec) /*&&
2517  //FIXME we should not special-case MPEG-2, but this needs testing with non-MPEG-2 ...
2518  st->time_base.num*duration_sum[i]/st->info->duration_count*101LL > st->time_base.den*/){
2519  int num = 0;
2520  double best_error= 2*av_q2d(st->time_base);
2521  best_error = best_error*best_error*st->info->duration_count*1000*12*30;
2522 
2523  for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
2524  double error = st->info->duration_error[j] * get_std_framerate(j);
2525 // if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
2526 // av_log(NULL, AV_LOG_ERROR, "%f %f\n", get_std_framerate(j) / 12.0/1001, error);
2527  if(error < best_error){
2528  best_error= error;
2529  num = get_std_framerate(j);
2530  }
2531  }
2532  // do not increase frame rate by more than 1 % in order to match a standard rate.
2533  if (num && (!st->r_frame_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(st->r_frame_rate)))
2534  av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
2535  }
2536 
2537  if (!st->r_frame_rate.num){
2538  if( st->codec->time_base.den * (int64_t)st->time_base.num
2539  <= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t)st->time_base.den){
2540  st->r_frame_rate.num = st->codec->time_base.den;
2542  }else{
2543  st->r_frame_rate.num = st->time_base.den;
2544  st->r_frame_rate.den = st->time_base.num;
2545  }
2546  }
2547  }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
2548  if(!st->codec->bits_per_coded_sample)
2550  // set stream disposition based on audio service type
2551  switch (st->codec->audio_service_type) {
2559  st->disposition = AV_DISPOSITION_COMMENT; break;
2561  st->disposition = AV_DISPOSITION_KARAOKE; break;
2562  }
2563  }
2564  }
2565 
2566  estimate_timings(ic, old_offset);
2567 
2569 
2570 #if 0
2571  /* correct DTS for B-frame streams with no timestamps */
2572  for(i=0;i<ic->nb_streams;i++) {
2573  st = ic->streams[i];
2574  if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
2575  if(b-frames){
2576  ppktl = &ic->packet_buffer;
2577  while(ppkt1){
2578  if(ppkt1->stream_index != i)
2579  continue;
2580  if(ppkt1->pkt->dts < 0)
2581  break;
2582  if(ppkt1->pkt->pts != AV_NOPTS_VALUE)
2583  break;
2584  ppkt1->pkt->dts -= delta;
2585  ppkt1= ppkt1->next;
2586  }
2587  if(ppkt1)
2588  continue;
2589  st->cur_dts -= delta;
2590  }
2591  }
2592  }
2593 #endif
2594 
2595  find_stream_info_err:
2596  for (i=0; i < ic->nb_streams; i++) {
2597  if (ic->streams[i]->codec)
2598  ic->streams[i]->codec->thread_count = 0;
2599  av_freep(&ic->streams[i]->info);
2600  }
2601  return ret;
2602 }
2603 
2605 {
2606  int i, j;
2607 
2608  for (i = 0; i < ic->nb_programs; i++)
2609  for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2610  if (ic->programs[i]->stream_index[j] == s)
2611  return ic->programs[i];
2612  return NULL;
2613 }
2614 
2616  enum AVMediaType type,
2617  int wanted_stream_nb,
2618  int related_stream,
2619  AVCodec **decoder_ret,
2620  int flags)
2621 {
2622  int i, nb_streams = ic->nb_streams;
2623  int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2624  unsigned *program = NULL;
2625  AVCodec *decoder = NULL, *best_decoder = NULL;
2626 
2627  if (related_stream >= 0 && wanted_stream_nb < 0) {
2628  AVProgram *p = find_program_from_stream(ic, related_stream);
2629  if (p) {
2630  program = p->stream_index;
2631  nb_streams = p->nb_stream_indexes;
2632  }
2633  }
2634  for (i = 0; i < nb_streams; i++) {
2635  int real_stream_index = program ? program[i] : i;
2636  AVStream *st = ic->streams[real_stream_index];
2637  AVCodecContext *avctx = st->codec;
2638  if (avctx->codec_type != type)
2639  continue;
2640  if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
2641  continue;
2643  continue;
2644  if (decoder_ret) {
2645  decoder = avcodec_find_decoder(st->codec->codec_id);
2646  if (!decoder) {
2647  if (ret < 0)
2649  continue;
2650  }
2651  }
2652  if (best_count >= st->codec_info_nb_frames)
2653  continue;
2654  best_count = st->codec_info_nb_frames;
2655  ret = real_stream_index;
2656  best_decoder = decoder;
2657  if (program && i == nb_streams - 1 && ret < 0) {
2658  program = NULL;
2659  nb_streams = ic->nb_streams;
2660  i = 0; /* no related stream found, try again with everything */
2661  }
2662  }
2663  if (decoder_ret)
2664  *decoder_ret = best_decoder;
2665  return ret;
2666 }
2667 
2668 /*******************************************************/
2669 
2671 {
2672  if (s->iformat->read_play)
2673  return s->iformat->read_play(s);
2674  if (s->pb)
2675  return avio_pause(s->pb, 0);
2676  return AVERROR(ENOSYS);
2677 }
2678 
2680 {
2681  if (s->iformat->read_pause)
2682  return s->iformat->read_pause(s);
2683  if (s->pb)
2684  return avio_pause(s->pb, 1);
2685  return AVERROR(ENOSYS);
2686 }
2687 
2688 #if FF_API_FORMAT_PARAMETERS
2689 void av_close_input_stream(AVFormatContext *s)
2690 {
2691  flush_packet_queue(s);
2692  if (s->iformat->read_close)
2693  s->iformat->read_close(s);
2695 }
2696 #endif
2697 
2699 {
2700  int i;
2701  AVStream *st;
2702 
2703  av_opt_free(s);
2704  if (s->iformat && s->iformat->priv_class && s->priv_data)
2705  av_opt_free(s->priv_data);
2706 
2707  for(i=0;i<s->nb_streams;i++) {
2708  /* free all data in a stream component */
2709  st = s->streams[i];
2710  if (st->parser) {
2711  av_parser_close(st->parser);
2712  av_free_packet(&st->cur_pkt);
2713  }
2714  av_dict_free(&st->metadata);
2715  av_free(st->index_entries);
2716  av_free(st->codec->extradata);
2718  av_free(st->codec);
2719  av_free(st->priv_data);
2720  av_free(st->info);
2721  av_free(st);
2722  }
2723  for(i=s->nb_programs-1; i>=0; i--) {
2724  av_dict_free(&s->programs[i]->metadata);
2725  av_freep(&s->programs[i]->stream_index);
2726  av_freep(&s->programs[i]);
2727  }
2728  av_freep(&s->programs);
2729  av_freep(&s->priv_data);
2730  while(s->nb_chapters--) {
2732  av_free(s->chapters[s->nb_chapters]);
2733  }
2734  av_freep(&s->chapters);
2735  av_dict_free(&s->metadata);
2736  av_freep(&s->streams);
2737  av_free(s);
2738 }
2739 
2740 #if FF_API_CLOSE_INPUT_FILE
2741 void av_close_input_file(AVFormatContext *s)
2742 {
2744 }
2745 #endif
2746 
2748 {
2749  AVFormatContext *s = *ps;
2751  NULL : s->pb;
2752  flush_packet_queue(s);
2753  if (s->iformat->read_close)
2754  s->iformat->read_close(s);
2756  *ps = NULL;
2757  if (pb)
2758  avio_close(pb);
2759 }
2760 
2761 #if FF_API_NEW_STREAM
2762 AVStream *av_new_stream(AVFormatContext *s, int id)
2763 {
2764  AVStream *st = avformat_new_stream(s, NULL);
2765  if (st)
2766  st->id = id;
2767  return st;
2768 }
2769 #endif
2770 
2772 {
2773  AVStream *st;
2774  int i;
2775  AVStream **streams;
2776 
2777  if (s->nb_streams >= INT_MAX/sizeof(*streams))
2778  return NULL;
2779  streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));
2780  if (!streams)
2781  return NULL;
2782  s->streams = streams;
2783 
2784  st = av_mallocz(sizeof(AVStream));
2785  if (!st)
2786  return NULL;
2787  if (!(st->info = av_mallocz(sizeof(*st->info)))) {
2788  av_free(st);
2789  return NULL;
2790  }
2791 
2792  st->codec = avcodec_alloc_context3(c);
2793  if (s->iformat) {
2794  /* no default bitrate if decoding */
2795  st->codec->bit_rate = 0;
2796  }
2797  st->index = s->nb_streams;
2798  st->start_time = AV_NOPTS_VALUE;
2799  st->duration = AV_NOPTS_VALUE;
2800  /* we set the current DTS to 0 so that formats without any timestamps
2801  but durations get some timestamps, formats with some unknown
2802  timestamps have their first few packets buffered and the
2803  timestamps corrected before they are returned to the user */
2804  st->cur_dts = 0;
2805  st->first_dts = AV_NOPTS_VALUE;
2807 
2808  /* default pts setting is MPEG-like */
2809  avpriv_set_pts_info(st, 33, 1, 90000);
2811  for(i=0; i<MAX_REORDER_DELAY+1; i++)
2812  st->pts_buffer[i]= AV_NOPTS_VALUE;
2814 
2815  st->sample_aspect_ratio = (AVRational){0,1};
2816 
2817  s->streams[s->nb_streams++] = st;
2818  return st;
2819 }
2820 
2822 {
2823  AVProgram *program=NULL;
2824  int i;
2825 
2826  av_dlog(ac, "new_program: id=0x%04x\n", id);
2827 
2828  for(i=0; i<ac->nb_programs; i++)
2829  if(ac->programs[i]->id == id)
2830  program = ac->programs[i];
2831 
2832  if(!program){
2833  program = av_mallocz(sizeof(AVProgram));
2834  if (!program)
2835  return NULL;
2836  dynarray_add(&ac->programs, &ac->nb_programs, program);
2837  program->discard = AVDISCARD_NONE;
2838  }
2839  program->id = id;
2840 
2841  return program;
2842 }
2843 
2844 AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
2845 {
2846  AVChapter *chapter = NULL;
2847  int i;
2848 
2849  for(i=0; i<s->nb_chapters; i++)
2850  if(s->chapters[i]->id == id)
2851  chapter = s->chapters[i];
2852 
2853  if(!chapter){
2854  chapter= av_mallocz(sizeof(AVChapter));
2855  if(!chapter)
2856  return NULL;
2857  dynarray_add(&s->chapters, &s->nb_chapters, chapter);
2858  }
2859  av_dict_set(&chapter->metadata, "title", title, 0);
2860  chapter->id = id;
2861  chapter->time_base= time_base;
2862  chapter->start = start;
2863  chapter->end = end;
2864 
2865  return chapter;
2866 }
2867 
2868 /************************************************************/
2869 /* output media file */
2870 
2871 #if FF_API_FORMAT_PARAMETERS
2872 int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
2873 {
2874  int ret;
2875 
2876  if (s->oformat->priv_data_size > 0) {
2878  if (!s->priv_data)
2879  return AVERROR(ENOMEM);
2880  if (s->oformat->priv_class) {
2881  *(const AVClass**)s->priv_data= s->oformat->priv_class;
2883  }
2884  } else
2885  s->priv_data = NULL;
2886 
2887  if (s->oformat->set_parameters) {
2888  ret = s->oformat->set_parameters(s, ap);
2889  if (ret < 0)
2890  return ret;
2891  }
2892  return 0;
2893 }
2894 #endif
2895 
2897 {
2898  const AVCodecTag *avctag;
2899  int n;
2900  enum CodecID id = CODEC_ID_NONE;
2901  unsigned int tag = 0;
2902 
2909  for (n = 0; s->oformat->codec_tag[n]; n++) {
2910  avctag = s->oformat->codec_tag[n];
2911  while (avctag->id != CODEC_ID_NONE) {
2912  if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
2913  id = avctag->id;
2914  if (id == st->codec->codec_id)
2915  return 1;
2916  }
2917  if (avctag->id == st->codec->codec_id)
2918  tag = avctag->tag;
2919  avctag++;
2920  }
2921  }
2922  if (id != CODEC_ID_NONE)
2923  return 0;
2924  if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
2925  return 0;
2926  return 1;
2927 }
2928 
2929 #if FF_API_FORMAT_PARAMETERS
2930 int av_write_header(AVFormatContext *s)
2931 {
2932  return avformat_write_header(s, NULL);
2933 }
2934 #endif
2935 
2937 {
2938  int ret = 0, i;
2939  AVStream *st;
2940  AVDictionary *tmp = NULL;
2941 
2942  if (options)
2943  av_dict_copy(&tmp, *options, 0);
2944  if ((ret = av_opt_set_dict(s, &tmp)) < 0)
2945  goto fail;
2946 
2947  // some sanity checks
2948  if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
2949  av_log(s, AV_LOG_ERROR, "no streams\n");
2950  ret = AVERROR(EINVAL);
2951  goto fail;
2952  }
2953 
2954  for(i=0;i<s->nb_streams;i++) {
2955  st = s->streams[i];
2956 
2957  switch (st->codec->codec_type) {
2958  case AVMEDIA_TYPE_AUDIO:
2959  if(st->codec->sample_rate<=0){
2960  av_log(s, AV_LOG_ERROR, "sample rate not set\n");
2961  ret = AVERROR(EINVAL);
2962  goto fail;
2963  }
2964  if(!st->codec->block_align)
2965  st->codec->block_align = st->codec->channels *
2967  break;
2968  case AVMEDIA_TYPE_VIDEO:
2969  if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too?
2970  av_log(s, AV_LOG_ERROR, "time base not set\n");
2971  ret = AVERROR(EINVAL);
2972  goto fail;
2973  }
2974  if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
2975  av_log(s, AV_LOG_ERROR, "dimensions not set\n");
2976  ret = AVERROR(EINVAL);
2977  goto fail;
2978  }
2980  av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between encoder and muxer layer\n");
2981  ret = AVERROR(EINVAL);
2982  goto fail;
2983  }
2984  break;
2985  }
2986 
2987  if(s->oformat->codec_tag){
2989  //the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here
2990  st->codec->codec_tag= 0;
2991  }
2992  if(st->codec->codec_tag){
2993  if (!validate_codec_tag(s, st)) {
2994  char tagbuf[32];
2995  av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
2996  av_log(s, AV_LOG_ERROR,
2997  "Tag %s/0x%08x incompatible with output codec id '%d'\n",
2998  tagbuf, st->codec->codec_tag, st->codec->codec_id);
2999  ret = AVERROR_INVALIDDATA;
3000  goto fail;
3001  }
3002  }else
3004  }
3005 
3006  if(s->oformat->flags & AVFMT_GLOBALHEADER &&
3008  av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);
3009  }
3010 
3011  if (!s->priv_data && s->oformat->priv_data_size > 0) {
3013  if (!s->priv_data) {
3014  ret = AVERROR(ENOMEM);
3015  goto fail;
3016  }
3017  if (s->oformat->priv_class) {
3018  *(const AVClass**)s->priv_data= s->oformat->priv_class;
3020  if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
3021  goto fail;
3022  }
3023  }
3024 
3025  /* set muxer identification string */
3026  if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
3027  av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
3028  }
3029 
3030  if(s->oformat->write_header){
3031  ret = s->oformat->write_header(s);
3032  if (ret < 0)
3033  goto fail;
3034  }
3035 
3036  /* init PTS generation */
3037  for(i=0;i<s->nb_streams;i++) {
3038  int64_t den = AV_NOPTS_VALUE;
3039  st = s->streams[i];
3040 
3041  switch (st->codec->codec_type) {
3042  case AVMEDIA_TYPE_AUDIO:
3043  den = (int64_t)st->time_base.num * st->codec->sample_rate;
3044  break;
3045  case AVMEDIA_TYPE_VIDEO:
3046  den = (int64_t)st->time_base.num * st->codec->time_base.den;
3047  break;
3048  default:
3049  break;
3050  }
3051  if (den != AV_NOPTS_VALUE) {
3052  if (den <= 0) {
3053  ret = AVERROR_INVALIDDATA;
3054  goto fail;
3055  }
3056  frac_init(&st->pts, 0, 0, den);
3057  }
3058  }
3059 
3060  if (options) {
3061  av_dict_free(options);
3062  *options = tmp;
3063  }
3064  return 0;
3065 fail:
3066  av_dict_free(&tmp);
3067  return ret;
3068 }
3069 
3070 //FIXME merge with compute_pkt_fields
3072  int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
3073  int num, den, frame_size, i;
3074 
3075  av_dlog(s, "compute_pkt_fields2: pts:%"PRId64" dts:%"PRId64" cur_dts:%"PRId64" b:%d size:%d st:%d\n",
3076  pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
3077 
3078 /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
3079  return AVERROR(EINVAL);*/
3080 
3081  /* duration field */
3082  if (pkt->duration == 0) {
3083  compute_frame_duration(&num, &den, st, NULL, pkt);
3084  if (den && num) {
3085  pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
3086  }
3087  }
3088 
3089  if(pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay==0)
3090  pkt->pts= pkt->dts;
3091 
3092  //XXX/FIXME this is a temporary hack until all encoders output pts
3093  if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay){
3094  pkt->dts=
3095 // pkt->pts= st->cur_dts;
3096  pkt->pts= st->pts.val;
3097  }
3098 
3099  //calculate dts from pts
3100  if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
3101  st->pts_buffer[0]= pkt->pts;
3102  for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
3103  st->pts_buffer[i]= pkt->pts + (i-delay-1) * pkt->duration;
3104  for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
3105  FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
3106 
3107  pkt->dts= st->pts_buffer[0];
3108  }
3109 
3110  if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
3111  av_log(s, AV_LOG_ERROR,
3112  "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %"PRId64" >= %"PRId64"\n",
3113  st->index, st->cur_dts, pkt->dts);
3114  return AVERROR(EINVAL);
3115  }
3116  if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
3117  av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
3118  return AVERROR(EINVAL);
3119  }
3120 
3121 // av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n", pkt->pts, pkt->dts);
3122  st->cur_dts= pkt->dts;
3123  st->pts.val= pkt->dts;
3124 
3125  /* update pts */
3126  switch (st->codec->codec_type) {
3127  case AVMEDIA_TYPE_AUDIO:
3128  frame_size = get_audio_frame_size(st->codec, pkt->size);
3129 
3130  /* HACK/FIXME, we skip the initial 0 size packets as they are most
3131  likely equal to the encoder delay, but it would be better if we
3132  had the real timestamps from the encoder */
3133  if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
3134  frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
3135  }
3136  break;
3137  case AVMEDIA_TYPE_VIDEO:
3138  frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
3139  break;
3140  default:
3141  break;
3142  }
3143  return 0;
3144 }
3145 
3147 {
3148  int ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
3149 
3150  if(ret<0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
3151  return ret;
3152 
3153  ret= s->oformat->write_packet(s, pkt);
3154 
3155  if (ret >= 0)
3156  s->streams[pkt->stream_index]->nb_frames++;
3157  return ret;
3158 }
3159 
3161  int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
3162 {
3163  AVPacketList **next_point, *this_pktl;
3164 
3165  this_pktl = av_mallocz(sizeof(AVPacketList));
3166  this_pktl->pkt= *pkt;
3167  pkt->destruct= NULL; // do not free original but only the copy
3168  av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-alloced memory
3169 
3171  next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
3172  }else
3173  next_point = &s->packet_buffer;
3174 
3175  if(*next_point){
3176  if(compare(s, &s->packet_buffer_end->pkt, pkt)){
3177  while(!compare(s, &(*next_point)->pkt, pkt)){
3178  next_point= &(*next_point)->next;
3179  }
3180  goto next_non_null;
3181  }else{
3182  next_point = &(s->packet_buffer_end->next);
3183  }
3184  }
3185  assert(!*next_point);
3186 
3187  s->packet_buffer_end= this_pktl;
3188 next_non_null:
3189 
3190  this_pktl->next= *next_point;
3191 
3193  *next_point= this_pktl;
3194 }
3195 
3197 {
3198  AVStream *st = s->streams[ pkt ->stream_index];
3199  AVStream *st2= s->streams[ next->stream_index];
3200  int comp = av_compare_ts(next->dts, st2->time_base, pkt->dts,
3201  st->time_base);
3202 
3203  if (comp == 0)
3204  return pkt->stream_index < next->stream_index;
3205  return comp > 0;
3206 }
3207 
3209  AVPacketList *pktl;
3210  int stream_count=0;
3211  int i;
3212 
3213  if(pkt){
3215  }
3216 
3217  for(i=0; i < s->nb_streams; i++)
3218  stream_count+= !!s->streams[i]->last_in_packet_buffer;
3219 
3220  if(stream_count && (s->nb_streams == stream_count || flush)){
3221  pktl= s->packet_buffer;
3222  *out= pktl->pkt;
3223 
3224  s->packet_buffer= pktl->next;
3225  if(!s->packet_buffer)
3226  s->packet_buffer_end= NULL;
3227 
3228  if(s->streams[out->stream_index]->last_in_packet_buffer == pktl)
3230  av_freep(&pktl);
3231  return 1;
3232  }else{
3233  av_init_packet(out);
3234  return 0;
3235  }
3236 }
3237 
3248  if (s->oformat->interleave_packet) {
3249  int ret = s->oformat->interleave_packet(s, out, in, flush);
3250  if (in)
3251  av_free_packet(in);
3252  return ret;
3253  } else
3254  return av_interleave_packet_per_dts(s, out, in, flush);
3255 }
3256 
3258  AVStream *st= s->streams[ pkt->stream_index];
3259  int ret;
3260 
3261  //FIXME/XXX/HACK drop zero sized packets
3262  if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO && pkt->size==0)
3263  return 0;
3264 
3265  av_dlog(s, "av_interleaved_write_frame size:%d dts:%"PRId64" pts:%"PRId64"\n",
3266  pkt->size, pkt->dts, pkt->pts);
3267  if((ret = compute_pkt_fields2(s, st, pkt)) < 0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
3268  return ret;
3269 
3270  if(pkt->dts == AV_NOPTS_VALUE && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
3271  return AVERROR(EINVAL);
3272 
3273  for(;;){
3274  AVPacket opkt;
3275  int ret= interleave_packet(s, &opkt, pkt, 0);
3276  if(ret<=0) //FIXME cleanup needed for ret<0 ?
3277  return ret;
3278 
3279  ret= s->oformat->write_packet(s, &opkt);
3280  if (ret >= 0)
3281  s->streams[opkt.stream_index]->nb_frames++;
3282 
3283  av_free_packet(&opkt);
3284  pkt= NULL;
3285 
3286  if(ret<0)
3287  return ret;
3288  }
3289 }
3290 
3292 {
3293  int ret, i;
3294 
3295  for(;;){
3296  AVPacket pkt;
3297  ret= interleave_packet(s, &pkt, NULL, 1);
3298  if(ret<0) //FIXME cleanup needed for ret<0 ?
3299  goto fail;
3300  if(!ret)
3301  break;
3302 
3303  ret= s->oformat->write_packet(s, &pkt);
3304  if (ret >= 0)
3305  s->streams[pkt.stream_index]->nb_frames++;
3306 
3307  av_free_packet(&pkt);
3308 
3309  if(ret<0)
3310  goto fail;
3311  }
3312 
3313  if(s->oformat->write_trailer)
3314  ret = s->oformat->write_trailer(s);
3315 fail:
3316  for(i=0;i<s->nb_streams;i++) {
3317  av_freep(&s->streams[i]->priv_data);
3318  av_freep(&s->streams[i]->index_entries);
3319  }
3320  if (s->oformat->priv_class)
3321  av_opt_free(s->priv_data);
3322  av_freep(&s->priv_data);
3323  return ret;
3324 }
3325 
3326 void ff_program_add_stream_index(AVFormatContext *ac, int progid, unsigned int idx)
3327 {
3328  int i, j;
3329  AVProgram *program=NULL;
3330  void *tmp;
3331 
3332  if (idx >= ac->nb_streams) {
3333  av_log(ac, AV_LOG_ERROR, "stream index %d is not valid\n", idx);
3334  return;
3335  }
3336 
3337  for(i=0; i<ac->nb_programs; i++){
3338  if(ac->programs[i]->id != progid)
3339  continue;
3340  program = ac->programs[i];
3341  for(j=0; j<program->nb_stream_indexes; j++)
3342  if(program->stream_index[j] == idx)
3343  return;
3344 
3345  tmp = av_realloc(program->stream_index, sizeof(unsigned int)*(program->nb_stream_indexes+1));
3346  if(!tmp)
3347  return;
3348  program->stream_index = tmp;
3349  program->stream_index[program->nb_stream_indexes++] = idx;
3350  return;
3351  }
3352 }
3353 
3354 static void print_fps(double d, const char *postfix){
3355  uint64_t v= lrintf(d*100);
3356  if (v% 100 ) av_log(NULL, AV_LOG_INFO, ", %3.2f %s", d, postfix);
3357  else if(v%(100*1000)) av_log(NULL, AV_LOG_INFO, ", %1.0f %s", d, postfix);
3358  else av_log(NULL, AV_LOG_INFO, ", %1.0fk %s", d/1000, postfix);
3359 }
3360 
3361 static void dump_metadata(void *ctx, AVDictionary *m, const char *indent)
3362 {
3363  if(m && !(m->count == 1 && av_dict_get(m, "language", NULL, 0))){
3365 
3366  av_log(ctx, AV_LOG_INFO, "%sMetadata:\n", indent);
3367  while((tag=av_dict_get(m, "", tag, AV_DICT_IGNORE_SUFFIX))) {
3368  if(strcmp("language", tag->key))
3369  av_log(ctx, AV_LOG_INFO, "%s %-16s: %s\n", indent, tag->key, tag->value);
3370  }
3371  }
3372 }
3373 
3374 /* "user interface" functions */
3375 static void dump_stream_format(AVFormatContext *ic, int i, int index, int is_output)
3376 {
3377  char buf[256];
3378  int flags = (is_output ? ic->oformat->flags : ic->iformat->flags);
3379  AVStream *st = ic->streams[i];
3380  int g = av_gcd(st->time_base.num, st->time_base.den);
3381  AVDictionaryEntry *lang = av_dict_get(st->metadata, "language", NULL, 0);
3382  avcodec_string(buf, sizeof(buf), st->codec, is_output);
3383  av_log(NULL, AV_LOG_INFO, " Stream #%d.%d", index, i);
3384  /* the pid is an important information, so we display it */
3385  /* XXX: add a generic system */
3386  if (flags & AVFMT_SHOW_IDS)
3387  av_log(NULL, AV_LOG_INFO, "[0x%x]", st->id);
3388  if (lang)
3389  av_log(NULL, AV_LOG_INFO, "(%s)", lang->value);
3390  av_log(NULL, AV_LOG_DEBUG, ", %d, %d/%d", st->codec_info_nb_frames, st->time_base.num/g, st->time_base.den/g);
3391  av_log(NULL, AV_LOG_INFO, ": %s", buf);
3392  if (st->sample_aspect_ratio.num && // default
3394  AVRational display_aspect_ratio;
3395  av_reduce(&display_aspect_ratio.num, &display_aspect_ratio.den,
3398  1024*1024);
3399  av_log(NULL, AV_LOG_INFO, ", PAR %d:%d DAR %d:%d",
3401  display_aspect_ratio.num, display_aspect_ratio.den);
3402  }
3403  if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO){
3404  if(st->avg_frame_rate.den && st->avg_frame_rate.num)
3405  print_fps(av_q2d(st->avg_frame_rate), "fps");
3406  if(st->r_frame_rate.den && st->r_frame_rate.num)
3407  print_fps(av_q2d(st->r_frame_rate), "tbr");
3408  if(st->time_base.den && st->time_base.num)
3409  print_fps(1/av_q2d(st->time_base), "tbn");
3410  if(st->codec->time_base.den && st->codec->time_base.num)
3411  print_fps(1/av_q2d(st->codec->time_base), "tbc");
3412  }
3414  av_log(NULL, AV_LOG_INFO, " (default)");
3415  if (st->disposition & AV_DISPOSITION_DUB)
3416  av_log(NULL, AV_LOG_INFO, " (dub)");
3418  av_log(NULL, AV_LOG_INFO, " (original)");
3420  av_log(NULL, AV_LOG_INFO, " (comment)");
3422  av_log(NULL, AV_LOG_INFO, " (lyrics)");
3424  av_log(NULL, AV_LOG_INFO, " (karaoke)");
3426  av_log(NULL, AV_LOG_INFO, " (forced)");
3428  av_log(NULL, AV_LOG_INFO, " (hearing impaired)");
3430  av_log(NULL, AV_LOG_INFO, " (visual impaired)");
3432  av_log(NULL, AV_LOG_INFO, " (clean effects)");
3433  av_log(NULL, AV_LOG_INFO, "\n");
3434  dump_metadata(NULL, st->metadata, " ");
3435 }
3436 
3437 #if FF_API_DUMP_FORMAT
3438 void dump_format(AVFormatContext *ic,
3439  int index,
3440  const char *url,
3441  int is_output)
3442 {
3443  av_dump_format(ic, index, url, is_output);
3444 }
3445 #endif
3446 
3448  int index,
3449  const char *url,
3450  int is_output)
3451 {
3452  int i;
3453  uint8_t *printed = ic->nb_streams ? av_mallocz(ic->nb_streams) : NULL;
3454  if (ic->nb_streams && !printed)
3455  return;
3456 
3457  av_log(NULL, AV_LOG_INFO, "%s #%d, %s, %s '%s':\n",
3458  is_output ? "Output" : "Input",
3459  index,
3460  is_output ? ic->oformat->name : ic->iformat->name,
3461  is_output ? "to" : "from", url);
3462  dump_metadata(NULL, ic->metadata, " ");
3463  if (!is_output) {
3464  av_log(NULL, AV_LOG_INFO, " Duration: ");
3465  if (ic->duration != AV_NOPTS_VALUE) {
3466  int hours, mins, secs, us;
3467  secs = ic->duration / AV_TIME_BASE;
3468  us = ic->duration % AV_TIME_BASE;
3469  mins = secs / 60;
3470  secs %= 60;
3471  hours = mins / 60;
3472  mins %= 60;
3473  av_log(NULL, AV_LOG_INFO, "%02d:%02d:%02d.%02d", hours, mins, secs,
3474  (100 * us) / AV_TIME_BASE);
3475  } else {
3476  av_log(NULL, AV_LOG_INFO, "N/A");
3477  }
3478  if (ic->start_time != AV_NOPTS_VALUE) {
3479  int secs, us;
3480  av_log(NULL, AV_LOG_INFO, ", start: ");
3481  secs = ic->start_time / AV_TIME_BASE;
3482  us = abs(ic->start_time % AV_TIME_BASE);
3483  av_log(NULL, AV_LOG_INFO, "%d.%06d",
3484  secs, (int)av_rescale(us, 1000000, AV_TIME_BASE));
3485  }
3486  av_log(NULL, AV_LOG_INFO, ", bitrate: ");
3487  if (ic->bit_rate) {
3488  av_log(NULL, AV_LOG_INFO,"%d kb/s", ic->bit_rate / 1000);
3489  } else {
3490  av_log(NULL, AV_LOG_INFO, "N/A");
3491  }
3492  av_log(NULL, AV_LOG_INFO, "\n");
3493  }
3494  for (i = 0; i < ic->nb_chapters; i++) {
3495  AVChapter *ch = ic->chapters[i];
3496  av_log(NULL, AV_LOG_INFO, " Chapter #%d.%d: ", index, i);
3497  av_log(NULL, AV_LOG_INFO, "start %f, ", ch->start * av_q2d(ch->time_base));
3498  av_log(NULL, AV_LOG_INFO, "end %f\n", ch->end * av_q2d(ch->time_base));
3499 
3500  dump_metadata(NULL, ch->metadata, " ");
3501  }
3502  if(ic->nb_programs) {
3503  int j, k, total = 0;
3504  for(j=0; j<ic->nb_programs; j++) {
3506  "name", NULL, 0);
3507  av_log(NULL, AV_LOG_INFO, " Program %d %s\n", ic->programs[j]->id,
3508  name ? name->value : "");
3509  dump_metadata(NULL, ic->programs[j]->metadata, " ");
3510  for(k=0; k<ic->programs[j]->nb_stream_indexes; k++) {
3511  dump_stream_format(ic, ic->programs[j]->stream_index[k], index, is_output);
3512  printed[ic->programs[j]->stream_index[k]] = 1;
3513  }
3514  total += ic->programs[j]->nb_stream_indexes;
3515  }
3516  if (total < ic->nb_streams)
3517  av_log(NULL, AV_LOG_INFO, " No Program\n");
3518  }
3519  for(i=0;i<ic->nb_streams;i++)
3520  if (!printed[i])
3521  dump_stream_format(ic, i, index, is_output);
3522 
3523  av_free(printed);
3524 }
3525 
3526 int64_t av_gettime(void)
3527 {
3528  struct timeval tv;
3529  gettimeofday(&tv,NULL);
3530  return (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
3531 }
3532 
3533 uint64_t ff_ntp_time(void)
3534 {
3535  return (av_gettime() / 1000) * 1000 + NTP_OFFSET_US;
3536 }
3537 
3538 #if FF_API_PARSE_DATE
3539 #include "libavutil/parseutils.h"
3540 
3541 int64_t parse_date(const char *timestr, int duration)
3542 {
3543  int64_t timeval;
3544  av_parse_time(&timeval, timestr, duration);
3545  return timeval;
3546 }
3547 #endif
3548 
3549 #if FF_API_FIND_INFO_TAG
3550 #include "libavutil/parseutils.h"
3551 
3552 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
3553 {
3554  return av_find_info_tag(arg, arg_size, tag1, info);
3555 }
3556 #endif
3557 
3558 int av_get_frame_filename(char *buf, int buf_size,
3559  const char *path, int number)
3560 {
3561  const char *p;
3562  char *q, buf1[20], c;
3563  int nd, len, percentd_found;
3564 
3565  q = buf;
3566  p = path;
3567  percentd_found = 0;
3568  for(;;) {
3569  c = *p++;
3570  if (c == '\0')
3571  break;
3572  if (c == '%') {
3573  do {
3574  nd = 0;
3575  while (isdigit(*p)) {
3576  nd = nd * 10 + *p++ - '0';
3577  }
3578  c = *p++;
3579  } while (isdigit(c));
3580 
3581  switch(c) {
3582  case '%':
3583  goto addchar;
3584  case 'd':
3585  if (percentd_found)
3586  goto fail;
3587  percentd_found = 1;
3588  snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
3589  len = strlen(buf1);
3590  if ((q - buf + len) > buf_size - 1)
3591  goto fail;
3592  memcpy(q, buf1, len);
3593  q += len;
3594  break;
3595  default:
3596  goto fail;
3597  }
3598  } else {
3599  addchar:
3600  if ((q - buf) < buf_size - 1)
3601  *q++ = c;
3602  }
3603  }
3604  if (!percentd_found)
3605  goto fail;
3606  *q = '\0';
3607  return 0;
3608  fail:
3609  *q = '\0';
3610  return -1;
3611 }
3612 
3613 static void hex_dump_internal(void *avcl, FILE *f, int level, uint8_t *buf, int size)
3614 {
3615  int len, i, j, c;
3616 #undef fprintf
3617 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3618 
3619  for(i=0;i<size;i+=16) {
3620  len = size - i;
3621  if (len > 16)
3622  len = 16;
3623  PRINT("%08x ", i);
3624  for(j=0;j<16;j++) {
3625  if (j < len)
3626  PRINT(" %02x", buf[i+j]);
3627  else
3628  PRINT(" ");
3629  }
3630  PRINT(" ");
3631  for(j=0;j<len;j++) {
3632  c = buf[i+j];
3633  if (c < ' ' || c > '~')
3634  c = '.';
3635  PRINT("%c", c);
3636  }
3637  PRINT("\n");
3638  }
3639 #undef PRINT
3640 }
3641 
3642 void av_hex_dump(FILE *f, uint8_t *buf, int size)
3643 {
3644  hex_dump_internal(NULL, f, 0, buf, size);
3645 }
3646 
3647 void av_hex_dump_log(void *avcl, int level, uint8_t *buf, int size)
3648 {
3649  hex_dump_internal(avcl, NULL, level, buf, size);
3650 }
3651 
3652 static void pkt_dump_internal(void *avcl, FILE *f, int level, AVPacket *pkt, int dump_payload, AVRational time_base)
3653 {
3654 #undef fprintf
3655 #define PRINT(...) do { if (!f) av_log(avcl, level, __VA_ARGS__); else fprintf(f, __VA_ARGS__); } while(0)
3656  PRINT("stream #%d:\n", pkt->stream_index);
3657  PRINT(" keyframe=%d\n", ((pkt->flags & AV_PKT_FLAG_KEY) != 0));
3658  PRINT(" duration=%0.3f\n", pkt->duration * av_q2d(time_base));
3659  /* DTS is _always_ valid after av_read_frame() */
3660  PRINT(" dts=");
3661  if (pkt->dts == AV_NOPTS_VALUE)
3662  PRINT("N/A");
3663  else
3664  PRINT("%0.3f", pkt->dts * av_q2d(time_base));
3665  /* PTS may not be known if B-frames are present. */
3666  PRINT(" pts=");
3667  if (pkt->pts == AV_NOPTS_VALUE)
3668  PRINT("N/A");
3669  else
3670  PRINT("%0.3f", pkt->pts * av_q2d(time_base));
3671  PRINT("\n");
3672  PRINT(" size=%d\n", pkt->size);
3673 #undef PRINT
3674  if (dump_payload)
3675  av_hex_dump(f, pkt->data, pkt->size);
3676 }
3677 
3678 #if FF_API_PKT_DUMP
3679 void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload)
3680 {
3681  AVRational tb = { 1, AV_TIME_BASE };
3682  pkt_dump_internal(NULL, f, 0, pkt, dump_payload, tb);
3683 }
3684 #endif
3685 
3686 void av_pkt_dump2(FILE *f, AVPacket *pkt, int dump_payload, AVStream *st)
3687 {
3688  pkt_dump_internal(NULL, f, 0, pkt, dump_payload, st->time_base);
3689 }
3690 
3691 #if FF_API_PKT_DUMP
3692 void av_pkt_dump_log(void *avcl, int level, AVPacket *pkt, int dump_payload)
3693 {
3694  AVRational tb = { 1, AV_TIME_BASE };
3695  pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, tb);
3696 }
3697 #endif
3698 
3699 void av_pkt_dump_log2(void *avcl, int level, AVPacket *pkt, int dump_payload,
3700  AVStream *st)
3701 {
3702  pkt_dump_internal(avcl, NULL, level, pkt, dump_payload, st->time_base);
3703 }
3704 
3705 void av_url_split(char *proto, int proto_size,
3706  char *authorization, int authorization_size,
3707  char *hostname, int hostname_size,
3708  int *port_ptr,
3709  char *path, int path_size,
3710  const char *url)
3711 {
3712  const char *p, *ls, *at, *col, *brk;
3713 
3714  if (port_ptr) *port_ptr = -1;
3715  if (proto_size > 0) proto[0] = 0;
3716  if (authorization_size > 0) authorization[0] = 0;
3717  if (hostname_size > 0) hostname[0] = 0;
3718  if (path_size > 0) path[0] = 0;
3719 
3720  /* parse protocol */
3721  if ((p = strchr(url, ':'))) {
3722  av_strlcpy(proto, url, FFMIN(proto_size, p + 1 - url));
3723  p++; /* skip ':' */
3724  if (*p == '/') p++;
3725  if (*p == '/') p++;
3726  } else {
3727  /* no protocol means plain filename */
3728  av_strlcpy(path, url, path_size);
3729  return;
3730  }
3731 
3732  /* separate path from hostname */
3733  ls = strchr(p, '/');
3734  if(!ls)
3735  ls = strchr(p, '?');
3736  if(ls)
3737  av_strlcpy(path, ls, path_size);
3738  else
3739  ls = &p[strlen(p)]; // XXX
3740 
3741  /* the rest is hostname, use that to parse auth/port */
3742  if (ls != p) {
3743  /* authorization (user[:pass]@hostname) */
3744  if ((at = strchr(p, '@')) && at < ls) {
3745  av_strlcpy(authorization, p,
3746  FFMIN(authorization_size, at + 1 - p));
3747  p = at + 1; /* skip '@' */
3748  }
3749 
3750  if (*p == '[' && (brk = strchr(p, ']')) && brk < ls) {
3751  /* [host]:port */
3752  av_strlcpy(hostname, p + 1,
3753  FFMIN(hostname_size, brk - p));
3754  if (brk[1] == ':' && port_ptr)
3755  *port_ptr = atoi(brk + 2);
3756  } else if ((col = strchr(p, ':')) && col < ls) {
3757  av_strlcpy(hostname, p,
3758  FFMIN(col + 1 - p, hostname_size));
3759  if (port_ptr) *port_ptr = atoi(col + 1);
3760  } else
3761  av_strlcpy(hostname, p,
3762  FFMIN(ls + 1 - p, hostname_size));
3763  }
3764 }
3765 
3766 char *ff_data_to_hex(char *buff, const uint8_t *src, int s, int lowercase)
3767 {
3768  int i;
3769  static const char hex_table_uc[16] = { '0', '1', '2', '3',
3770  '4', '5', '6', '7',
3771  '8', '9', 'A', 'B',
3772  'C', 'D', 'E', 'F' };
3773  static const char hex_table_lc[16] = { '0', '1', '2', '3',
3774  '4', '5', '6', '7',
3775  '8', '9', 'a', 'b',
3776  'c', 'd', 'e', 'f' };
3777  const char *hex_table = lowercase ? hex_table_lc : hex_table_uc;
3778 
3779  for(i = 0; i < s; i++) {
3780  buff[i * 2] = hex_table[src[i] >> 4];
3781  buff[i * 2 + 1] = hex_table[src[i] & 0xF];
3782  }
3783 
3784  return buff;
3785 }
3786 
3787 int ff_hex_to_data(uint8_t *data, const char *p)
3788 {
3789  int c, len, v;
3790 
3791  len = 0;
3792  v = 1;
3793  for (;;) {
3794  p += strspn(p, SPACE_CHARS);
3795  if (*p == '\0')
3796  break;
3797  c = toupper((unsigned char) *p++);
3798  if (c >= '0' && c <= '9')
3799  c = c - '0';
3800  else if (c >= 'A' && c <= 'F')
3801  c = c - 'A' + 10;
3802  else
3803  break;
3804  v = (v << 4) | c;
3805  if (v & 0x100) {
3806  if (data)
3807  data[len] = v;
3808  len++;
3809  v = 1;
3810  }
3811  }
3812  return len;
3813 }
3814 
3815 #if FF_API_SET_PTS_INFO
3816 void av_set_pts_info(AVStream *s, int pts_wrap_bits,
3817  unsigned int pts_num, unsigned int pts_den)
3818 {
3819  avpriv_set_pts_info(s, pts_wrap_bits, pts_num, pts_den);
3820 }
3821 #endif
3822 
3823 void avpriv_set_pts_info(AVStream *s, int pts_wrap_bits,
3824  unsigned int pts_num, unsigned int pts_den)
3825 {
3826  AVRational new_tb;
3827  if(av_reduce(&new_tb.num, &new_tb.den, pts_num, pts_den, INT_MAX)){
3828  if(new_tb.num != pts_num)
3829  av_log(NULL, AV_LOG_DEBUG, "st:%d removing common factor %d from timebase\n", s->index, pts_num/new_tb.num);
3830  }else
3831  av_log(NULL, AV_LOG_WARNING, "st:%d has too large timebase, reducing\n", s->index);
3832 
3833  if(new_tb.num <= 0 || new_tb.den <= 0) {
3834  av_log(NULL, AV_LOG_ERROR, "Ignoring attempt to set invalid timebase for st:%d\n", s->index);
3835  return;
3836  }
3837  s->time_base = new_tb;
3838  s->pts_wrap_bits = pts_wrap_bits;
3839 }
3840 
3841 int ff_url_join(char *str, int size, const char *proto,
3842  const char *authorization, const char *hostname,
3843  int port, const char *fmt, ...)
3844 {
3845 #if CONFIG_NETWORK
3846  struct addrinfo hints, *ai;
3847 #endif
3848 
3849  str[0] = '\0';
3850  if (proto)
3851  av_strlcatf(str, size, "%s://", proto);
3852  if (authorization && authorization[0])
3853  av_strlcatf(str, size, "%s@", authorization);
3854 #if CONFIG_NETWORK && defined(AF_INET6)
3855  /* Determine if hostname is a numerical IPv6 address,
3856  * properly escape it within [] in that case. */
3857  memset(&hints, 0, sizeof(hints));
3858  hints.ai_flags = AI_NUMERICHOST;
3859  if (!getaddrinfo(hostname, NULL, &hints, &ai)) {
3860  if (ai->ai_family == AF_INET6) {
3861  av_strlcat(str, "[", size);
3862  av_strlcat(str, hostname, size);
3863  av_strlcat(str, "]", size);
3864  } else {
3865  av_strlcat(str, hostname, size);
3866  }
3867  freeaddrinfo(ai);
3868  } else
3869 #endif
3870  /* Not an IPv6 address, just output the plain string. */
3871  av_strlcat(str, hostname, size);
3872 
3873  if (port >= 0)
3874  av_strlcatf(str, size, ":%d", port);
3875  if (fmt) {
3876  va_list vl;
3877  int len = strlen(str);
3878 
3879  va_start(vl, fmt);
3880  vsnprintf(str + len, size > len ? size - len : 0, fmt, vl);
3881  va_end(vl);
3882  }
3883  return strlen(str);
3884 }
3885 
3886 int ff_write_chained(AVFormatContext *dst, int dst_stream, AVPacket *pkt,
3887  AVFormatContext *src)
3888 {
3889  AVPacket local_pkt;
3890 
3891  local_pkt = *pkt;
3892  local_pkt.stream_index = dst_stream;
3893  if (pkt->pts != AV_NOPTS_VALUE)
3894  local_pkt.pts = av_rescale_q(pkt->pts,
3895  src->streams[pkt->stream_index]->time_base,
3896  dst->streams[dst_stream]->time_base);
3897  if (pkt->dts != AV_NOPTS_VALUE)
3898  local_pkt.dts = av_rescale_q(pkt->dts,
3899  src->streams[pkt->stream_index]->time_base,
3900  dst->streams[dst_stream]->time_base);
3901  return av_write_frame(dst, &local_pkt);
3902 }
3903 
3904 void ff_parse_key_value(const char *str, ff_parse_key_val_cb callback_get_buf,
3905  void *context)
3906 {
3907  const char *ptr = str;
3908 
3909  /* Parse key=value pairs. */
3910  for (;;) {
3911  const char *key;
3912  char *dest = NULL, *dest_end;
3913  int key_len, dest_len = 0;
3914 
3915  /* Skip whitespace and potential commas. */
3916  while (*ptr && (isspace(*ptr) || *ptr == ','))
3917  ptr++;
3918  if (!*ptr)
3919  break;
3920 
3921  key = ptr;
3922 
3923  if (!(ptr = strchr(key, '=')))
3924  break;
3925  ptr++;
3926  key_len = ptr - key;
3927 
3928  callback_get_buf(context, key, key_len, &dest, &dest_len);
3929  dest_end = dest + dest_len - 1;
3930 
3931  if (*ptr == '\"') {
3932  ptr++;
3933  while (*ptr && *ptr != '\"') {
3934  if (*ptr == '\\') {
3935  if (!ptr[1])
3936  break;
3937  if (dest && dest < dest_end)
3938  *dest++ = ptr[1];
3939  ptr += 2;
3940  } else {
3941  if (dest && dest < dest_end)
3942  *dest++ = *ptr;
3943  ptr++;
3944  }
3945  }
3946  if (*ptr == '\"')
3947  ptr++;
3948  } else {
3949  for (; *ptr && !(isspace(*ptr) || *ptr == ','); ptr++)
3950  if (dest && dest < dest_end)
3951  *dest++ = *ptr;
3952  }
3953  if (dest)
3954  *dest = 0;
3955  }
3956 }
3957 
3959 {
3960  int i;
3961  for (i = 0; i < s->nb_streams; i++) {
3962  if (s->streams[i]->id == id)
3963  return i;
3964  }
3965  return -1;
3966 }
3967 
3968 void ff_make_absolute_url(char *buf, int size, const char *base,
3969  const char *rel)
3970 {
3971  char *sep;
3972  /* Absolute path, relative to the current server */
3973  if (base && strstr(base, "://") && rel[0] == '/') {
3974  if (base != buf)
3975  av_strlcpy(buf, base, size);
3976  sep = strstr(buf, "://");
3977  if (sep) {
3978  sep += 3;
3979  sep = strchr(sep, '/');
3980  if (sep)
3981  *sep = '\0';
3982  }
3983  av_strlcat(buf, rel, size);
3984  return;
3985  }
3986  /* If rel actually is an absolute url, just copy it */
3987  if (!base || strstr(rel, "://") || rel[0] == '/') {
3988  av_strlcpy(buf, rel, size);
3989  return;
3990  }
3991  if (base != buf)
3992  av_strlcpy(buf, base, size);
3993  /* Remove the file name from the base url */
3994  sep = strrchr(buf, '/');
3995  if (sep)
3996  sep[1] = '\0';
3997  else
3998  buf[0] = '\0';
3999  while (av_strstart(rel, "../", NULL) && sep) {
4000  /* Remove the path delimiter at the end */
4001  sep[0] = '\0';
4002  sep = strrchr(buf, '/');
4003  /* If the next directory name to pop off is "..", break here */
4004  if (!strcmp(sep ? &sep[1] : buf, "..")) {
4005  /* Readd the slash we just removed */
4006  av_strlcat(buf, "/", size);
4007  break;
4008  }
4009  /* Cut off the directory name */
4010  if (sep)
4011  sep[1] = '\0';
4012  else
4013  buf[0] = '\0';
4014  rel += 3;
4015  }
4016  av_strlcat(buf, rel, size);
4017 }
4018 
4019 int64_t ff_iso8601_to_unix_time(const char *datestr)
4020 {
4021 #if HAVE_STRPTIME
4022  struct tm time1 = {0}, time2 = {0};
4023  char *ret1, *ret2;
4024  ret1 = strptime(datestr, "%Y - %m - %d %T", &time1);
4025  ret2 = strptime(datestr, "%Y - %m - %dT%T", &time2);
4026  if (ret2 && !ret1)
4027  return av_timegm(&time2);
4028  else
4029  return av_timegm(&time1);
4030 #else
4031  av_log(NULL, AV_LOG_WARNING, "strptime() unavailable on this system, cannot convert "
4032  "the date string.\n");
4033  return 0;
4034 #endif
4035 }
4036 
4037 int avformat_query_codec(AVOutputFormat *ofmt, enum CodecID codec_id, int std_compliance)
4038 {
4039  if (ofmt) {
4040  if (ofmt->query_codec)
4041  return ofmt->query_codec(codec_id, std_compliance);
4042  else if (ofmt->codec_tag)
4043  return !!av_codec_get_tag(ofmt->codec_tag, codec_id);
4044  else if (codec_id == ofmt->video_codec || codec_id == ofmt->audio_codec ||
4045  codec_id == ofmt->subtitle_codec)
4046  return 1;
4047  }
4048  return AVERROR_PATCHWELCOME;
4049 }
4050 
4052 {
4053 #if CONFIG_NETWORK
4054  int ret;
4056  if ((ret = ff_network_init()) < 0)
4057  return ret;
4058  ff_tls_init();
4059 #endif
4060  return 0;
4061 }
4062 
4064 {
4065 #if CONFIG_NETWORK
4066  ff_network_close();
4067  ff_tls_deinit();
4068 #endif
4069  return 0;
4070 }
4071 
4072 int ff_add_param_change(AVPacket *pkt, int32_t channels,
4073  uint64_t channel_layout, int32_t sample_rate,
4074  int32_t width, int32_t height)
4075 {
4076  uint32_t flags = 0;
4077  int size = 4;
4078  uint8_t *data;
4079  if (!pkt)
4080  return AVERROR(EINVAL);
4081  if (channels) {
4082  size += 4;
4084  }
4085  if (channel_layout) {
4086  size += 8;
4088  }
4089  if (sample_rate) {
4090  size += 4;
4092  }
4093  if (width || height) {
4094  size += 8;
4096  }
4098  if (!data)
4099  return AVERROR(ENOMEM);
4100  bytestream_put_le32(&data, flags);
4101  if (channels)
4102  bytestream_put_le32(&data, channels);
4103  if (channel_layout)
4104  bytestream_put_le64(&data, channel_layout);
4105  if (sample_rate)
4106  bytestream_put_le32(&data, sample_rate);
4107  if (width || height) {
4108  bytestream_put_le32(&data, width);
4109  bytestream_put_le32(&data, height);
4110  }
4111  return 0;
4112 }
4113 
4115 {
4116  return ff_codec_bmp_tags;
4117 }
4119 {
4120  return ff_codec_wav_tags;
4121 }