avconv: use avcodec_decode_audio4() instead of avcodec_decode_audio3()
[libav.git] / libavformat / utils.c
CommitLineData
de6d9b64 1/*
2912e87a 2 * various utility functions for use within Libav
19720f15 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
de6d9b64 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav is free software; you can redistribute it and/or
19720f15
FB
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
de6d9b64 11 *
2912e87a 12 * Libav is distributed in the hope that it will be useful,
de6d9b64 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19720f15
FB
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
de6d9b64 16 *
19720f15 17 * You should have received a copy of the GNU Lesser General Public
2912e87a 18 * License along with Libav; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
de6d9b64 20 */
79f43a8c
SS
21
22/* #define DEBUG */
23
8be1c656 24#include "avformat.h"
f1ef2cd9 25#include "avio_internal.h"
f1c80e35 26#include "internal.h"
603a5f04 27#include "libavcodec/internal.h"
6ed04040 28#include "libavutil/opt.h"
d2d67e42 29#include "libavutil/dict.h"
603b8bc2 30#include "libavutil/pixdesc.h"
06a7bd9a 31#include "metadata.h"
6612d8cf 32#include "id3v2.h"
245976da 33#include "libavutil/avstring.h"
0ebcdf5c 34#include "libavutil/mathematics.h"
4a835416 35#include "libavutil/parseutils.h"
45da8124 36#include "riff.h"
c26e58e3 37#include "audiointerleave.h"
5cec8971 38#include "url.h"
82e4ac2c
RP
39#include <sys/time.h>
40#include <time.h>
780d7897
MS
41#include <stdarg.h>
42#if CONFIG_NETWORK
43#include "network.h"
44#endif
c5510dd6 45
b754978a
MN
46#undef NDEBUG
47#include <assert.h>
48
e36bdf8b 49/**
ba87f080 50 * @file
2912e87a 51 * various utility functions for use within Libav
e36bdf8b
DK
52 */
53
c97429e2
SS
54unsigned avformat_version(void)
55{
56 return LIBAVFORMAT_VERSION_INT;
57}
58
41600690 59const char *avformat_configuration(void)
c1736936 60{
29ba0911 61 return LIBAV_CONFIGURATION;
c1736936
DB
62}
63
41600690 64const char *avformat_license(void)
c1736936
DB
65{
66#define LICENSE_PREFIX "libavformat license: "
a03be6e1 67 return LICENSE_PREFIX LIBAV_LICENSE + sizeof(LICENSE_PREFIX) - 1;
c1736936
DB
68}
69
8163c870
SS
70/* fraction handling */
71
72/**
73 * f = val + (num / den) + 0.5.
74 *
75 * 'num' is normalized so that it is such as 0 <= num < den.
76 *
77 * @param f fractional number
78 * @param val integer value
79 * @param num must be >= 0
80 * @param den must be >= 1
81 */
d3bb7191 82static void frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den)
8163c870
SS
83{
84 num += (den >> 1);
85 if (num >= den) {
86 val += num / den;
87 num = num % den;
88 }
89 f->val = val;
90 f->num = num;
91 f->den = den;
92}
93
94/**
95 * Fractional addition to f: f = f + (incr / f->den).
96 *
97 * @param f fractional number
98 * @param incr increment, can be positive or negative
99 */
d3bb7191 100static void frac_add(AVFrac *f, int64_t incr)
8163c870
SS
101{
102 int64_t num, den;
103
104 num = f->num + incr;
105 den = f->den;
106 if (num < 0) {
107 f->val += num / den;
108 num = num % den;
109 if (num < 0) {
110 num += den;
111 f->val--;
112 }
113 } else if (num >= den) {
114 f->val += num / den;
115 num = num % den;
116 }
117 f->num = num;
118}
f3a30e3a 119
a85736f2 120/** head of registered input format linked list */
b4c5acab 121static AVInputFormat *first_iformat = NULL;
a85736f2 122/** head of registered output format linked list */
b4c5acab 123static AVOutputFormat *first_oformat = NULL;
de6d9b64 124
84be6e72
MN
125AVInputFormat *av_iformat_next(AVInputFormat *f)
126{
127 if(f) return f->next;
128 else return first_iformat;
129}
130
131AVOutputFormat *av_oformat_next(AVOutputFormat *f)
132{
133 if(f) return f->next;
134 else return first_oformat;
135}
136
b9a281db 137void av_register_input_format(AVInputFormat *format)
de6d9b64 138{
b9a281db
FB
139 AVInputFormat **p;
140 p = &first_iformat;
141 while (*p != NULL) p = &(*p)->next;
142 *p = format;
143 format->next = NULL;
144}
145
146void av_register_output_format(AVOutputFormat *format)
147{
148 AVOutputFormat **p;
149 p = &first_oformat;
de6d9b64
FB
150 while (*p != NULL) p = &(*p)->next;
151 *p = format;
152 format->next = NULL;
153}
154
8eb631fa
SS
155int av_match_ext(const char *filename, const char *extensions)
156{
de6d9b64
FB
157 const char *ext, *p;
158 char ext1[32], *q;
159
293ed23f
MN
160 if(!filename)
161 return 0;
115329f1 162
de6d9b64
FB
163 ext = strrchr(filename, '.');
164 if (ext) {
165 ext++;
166 p = extensions;
167 for(;;) {
168 q = ext1;
115329f1 169 while (*p != '\0' && *p != ',' && q-ext1<sizeof(ext1)-1)
de6d9b64
FB
170 *q++ = *p++;
171 *q = '\0';
bb3244de 172 if (!av_strcasecmp(ext1, ext))
de6d9b64 173 return 1;
115329f1 174 if (*p == '\0')
de6d9b64
FB
175 break;
176 p++;
177 }
178 }
179 return 0;
180}
181
2323ac01
BC
182static int match_format(const char *name, const char *names)
183{
184 const char *p;
185 int len, namelen;
186
187 if (!name || !names)
188 return 0;
189
190 namelen = strlen(name);
191 while ((p = strchr(names, ','))) {
192 len = FFMAX(p - names, namelen);
bb3244de 193 if (!av_strncasecmp(name, names, len))
2323ac01
BC
194 return 1;
195 names = p+1;
196 }
bb3244de 197 return !av_strcasecmp(name, names);
2323ac01
BC
198}
199
a1f547b9
SS
200AVOutputFormat *av_guess_format(const char *short_name, const char *filename,
201 const char *mime_type)
202{
27323146 203 AVOutputFormat *fmt = NULL, *fmt_found;
de6d9b64
FB
204 int score_max, score;
205
87a0a681 206 /* specific test for image sequences */
b250f9c6 207#if CONFIG_IMAGE2_MUXER
115329f1 208 if (!short_name && filename &&
5c07cf53 209 av_filename_number_test(filename) &&
a9bf9d8e 210 ff_guess_image2_codec(filename) != CODEC_ID_NONE) {
0f52ef1a 211 return av_guess_format("image2", NULL, NULL);
5b6d5596 212 }
ff70e601 213#endif
a85736f2 214 /* Find the proper file type. */
de6d9b64
FB
215 fmt_found = NULL;
216 score_max = 0;
27323146 217 while ((fmt = av_oformat_next(fmt))) {
de6d9b64
FB
218 score = 0;
219 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
220 score += 100;
221 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
222 score += 10;
115329f1 223 if (filename && fmt->extensions &&
aa13b573 224 av_match_ext(filename, fmt->extensions)) {
de6d9b64
FB
225 score += 5;
226 }
227 if (score > score_max) {
228 score_max = score;
229 fmt_found = fmt;
230 }
de6d9b64
FB
231 }
232 return fmt_found;
115329f1 233}
de6d9b64 234
115329f1 235enum CodecID av_guess_codec(AVOutputFormat *fmt, const char *short_name,
72415b2a
SS
236 const char *filename, const char *mime_type, enum AVMediaType type){
237 if(type == AVMEDIA_TYPE_VIDEO){
5b6d5596
MN
238 enum CodecID codec_id= CODEC_ID_NONE;
239
b250f9c6 240#if CONFIG_IMAGE2_MUXER
ae214ac3 241 if(!strcmp(fmt->name, "image2") || !strcmp(fmt->name, "image2pipe")){
a9bf9d8e 242 codec_id= ff_guess_image2_codec(filename);
5b6d5596 243 }
ff70e601 244#endif
5b6d5596
MN
245 if(codec_id == CODEC_ID_NONE)
246 codec_id= fmt->video_codec;
247 return codec_id;
72415b2a 248 }else if(type == AVMEDIA_TYPE_AUDIO)
5b6d5596 249 return fmt->audio_codec;
118ccae0
AJ
250 else if (type == AVMEDIA_TYPE_SUBTITLE)
251 return fmt->subtitle_codec;
5b6d5596
MN
252 else
253 return CODEC_ID_NONE;
254}
255
b9a281db
FB
256AVInputFormat *av_find_input_format(const char *short_name)
257{
27323146
AK
258 AVInputFormat *fmt = NULL;
259 while ((fmt = av_iformat_next(fmt))) {
2323ac01 260 if (match_format(short_name, fmt->name))
b9a281db
FB
261 return fmt;
262 }
263 return NULL;
264}
265
de6d9b64 266
ae628ec1 267int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
2692067a
MN
268{
269 int ret= av_new_packet(pkt, size);
270
271 if(ret<0)
272 return ret;
273
a2704c97 274 pkt->pos= avio_tell(s);
2692067a 275
b7effd4e 276 ret= avio_read(s, pkt->data, size);
2692067a
MN
277 if(ret<=0)
278 av_free_packet(pkt);
279 else
feb993e5 280 av_shrink_packet(pkt, ret);
2692067a
MN
281
282 return ret;
283}
284
ae628ec1 285int av_append_packet(AVIOContext *s, AVPacket *pkt, int size)
6bfc2683
RD
286{
287 int ret;
288 int old_size;
289 if (!pkt->size)
290 return av_get_packet(s, pkt, size);
291 old_size = pkt->size;
292 ret = av_grow_packet(pkt, size);
293 if (ret < 0)
294 return ret;
b7effd4e 295 ret = avio_read(s, pkt->data + old_size, size);
6bfc2683
RD
296 av_shrink_packet(pkt, old_size + FFMAX(ret, 0));
297 return ret;
298}
299
fb2758c8 300
5c07cf53 301int av_filename_number_test(const char *filename)
b9a281db
FB
302{
303 char buf[1024];
5c07cf53 304 return filename && (av_get_frame_filename(buf, sizeof(buf), filename, 1)>=0);
b9a281db
FB
305}
306
8e2ee182 307AVInputFormat *av_probe_input_format2(AVProbeData *pd, int is_opened, int *score_max)
b9a281db 308{
6612d8cf 309 AVProbeData lpd = *pd;
27323146 310 AVInputFormat *fmt1 = NULL, *fmt;
56a10009 311 int score, id3 = 0;
b9a281db 312
6612d8cf
RD
313 if (lpd.buf_size > 10 && ff_id3v2_match(lpd.buf, ID3v2_DEFAULT_MAGIC)) {
314 int id3len = ff_id3v2_tag_len(lpd.buf);
315 if (lpd.buf_size > id3len + 16) {
316 lpd.buf += id3len;
317 lpd.buf_size -= id3len;
318 }
56a10009 319 id3 = 1;
6612d8cf
RD
320 }
321
b9a281db 322 fmt = NULL;
27323146 323 while ((fmt1 = av_iformat_next(fmt1))) {
b8e705ec 324 if (!is_opened == !(fmt1->flags & AVFMT_NOFILE))
b9a281db
FB
325 continue;
326 score = 0;
a8dbe951 327 if (fmt1->read_probe) {
6612d8cf 328 score = fmt1->read_probe(&lpd);
a8dbe951 329 } else if (fmt1->extensions) {
6612d8cf 330 if (av_match_ext(lpd.filename, fmt1->extensions)) {
b9a281db
FB
331 score = 50;
332 }
115329f1 333 }
79750486
MN
334 if (score > *score_max) {
335 *score_max = score;
b9a281db 336 fmt = fmt1;
4b3cca36
MN
337 }else if (score == *score_max)
338 fmt = NULL;
b9a281db 339 }
56a10009
AK
340
341 /* a hack for files with huge id3v2 tags -- try to guess by file extension. */
7a773d4d 342 if (!fmt && is_opened && *score_max < AVPROBE_SCORE_MAX/4) {
56a10009
AK
343 while ((fmt = av_iformat_next(fmt)))
344 if (fmt->extensions && av_match_ext(lpd.filename, fmt->extensions)) {
345 *score_max = AVPROBE_SCORE_MAX/4;
346 break;
347 }
348 }
349
61856d06
AC
350 if (!fmt && id3 && *score_max < AVPROBE_SCORE_MAX/4-1) {
351 while ((fmt = av_iformat_next(fmt)))
352 if (fmt->extensions && av_match_ext("mp3", fmt->extensions)) {
353 *score_max = AVPROBE_SCORE_MAX/4-1;
354 break;
355 }
356 }
357
b9a281db
FB
358 return fmt;
359}
360
79750486
MN
361AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened){
362 int score=0;
363 return av_probe_input_format2(pd, is_opened, &score);
364}
365
db46c4e1 366static int set_codec_from_probe_data(AVFormatContext *s, AVStream *st, AVProbeData *pd, int score)
64cd3108 367{
cf5b33d9
SS
368 static const struct {
369 const char *name; enum CodecID id; enum AVMediaType type;
370 } fmt_id_type[] = {
371 { "aac" , CODEC_ID_AAC , AVMEDIA_TYPE_AUDIO },
372 { "ac3" , CODEC_ID_AC3 , AVMEDIA_TYPE_AUDIO },
373 { "dts" , CODEC_ID_DTS , AVMEDIA_TYPE_AUDIO },
374 { "eac3" , CODEC_ID_EAC3 , AVMEDIA_TYPE_AUDIO },
375 { "h264" , CODEC_ID_H264 , AVMEDIA_TYPE_VIDEO },
376 { "m4v" , CODEC_ID_MPEG4 , AVMEDIA_TYPE_VIDEO },
377 { "mp3" , CODEC_ID_MP3 , AVMEDIA_TYPE_AUDIO },
378 { "mpegvideo", CODEC_ID_MPEG2VIDEO, AVMEDIA_TYPE_VIDEO },
379 { 0 }
380 };
381 AVInputFormat *fmt = av_probe_input_format2(pd, 1, &score);
64cd3108
MN
382
383 if (fmt) {
cf5b33d9 384 int i;
db46c4e1
BC
385 av_log(s, AV_LOG_DEBUG, "Probe with size=%d, packets=%d detected %s with score=%d\n",
386 pd->buf_size, MAX_PROBE_PACKETS - st->probe_packets, fmt->name, score);
cf5b33d9
SS
387 for (i = 0; fmt_id_type[i].name; i++) {
388 if (!strcmp(fmt->name, fmt_id_type[i].name)) {
389 st->codec->codec_id = fmt_id_type[i].id;
390 st->codec->codec_type = fmt_id_type[i].type;
391 break;
392 }
f1588ed5 393 }
64cd3108
MN
394 }
395 return !!fmt;
396}
397
b9a281db
FB
398/************************************************************/
399/* input media file */
96baaa6a 400
603b8bc2
AK
401#if FF_API_FORMAT_PARAMETERS
402static AVDictionary *convert_format_parameters(AVFormatParameters *ap)
403{
404 char buf[1024];
405 AVDictionary *opts = NULL;
406
84bd2b4b
AK
407 if (!ap)
408 return NULL;
409
603b8bc2
AK
410 if (ap->time_base.num) {
411 snprintf(buf, sizeof(buf), "%d/%d", ap->time_base.den, ap->time_base.num);
412 av_dict_set(&opts, "framerate", buf, 0);
413 }
414 if (ap->sample_rate) {
415 snprintf(buf, sizeof(buf), "%d", ap->sample_rate);
416 av_dict_set(&opts, "sample_rate", buf, 0);
417 }
418 if (ap->channels) {
419 snprintf(buf, sizeof(buf), "%d", ap->channels);
420 av_dict_set(&opts, "channels", buf, 0);
421 }
422 if (ap->width || ap->height) {
423 snprintf(buf, sizeof(buf), "%dx%d", ap->width, ap->height);
424 av_dict_set(&opts, "video_size", buf, 0);
425 }
426 if (ap->pix_fmt != PIX_FMT_NONE) {
427 av_dict_set(&opts, "pixel_format", av_get_pix_fmt_name(ap->pix_fmt), 0);
428 }
429 if (ap->channel) {
430 snprintf(buf, sizeof(buf), "%d", ap->channel);
431 av_dict_set(&opts, "channel", buf, 0);
432 }
433 if (ap->standard) {
434 av_dict_set(&opts, "standard", ap->standard, 0);
435 }
436 if (ap->mpeg2ts_compute_pcr) {
437 av_dict_set(&opts, "mpeg2ts_compute_pcr", "1", 0);
438 }
439 if (ap->initial_pause) {
440 av_dict_set(&opts, "initial_pause", "1", 0);
441 }
442 return opts;
443}
444
da24c5e3 445/**
e36bdf8b 446 * Open a media file from an IO stream. 'fmt' must be specified.
da24c5e3 447 */
115329f1 448int av_open_input_stream(AVFormatContext **ic_ptr,
ae628ec1 449 AVIOContext *pb, const char *filename,
da24c5e3
FB
450 AVInputFormat *fmt, AVFormatParameters *ap)
451{
452 int err;
603b8bc2 453 AVDictionary *opts;
da24c5e3 454 AVFormatContext *ic;
c04c3282
MN
455 AVFormatParameters default_ap;
456
457 if(!ap){
458 ap=&default_ap;
459 memset(ap, 0, sizeof(default_ap));
460 }
603b8bc2 461 opts = convert_format_parameters(ap);
da24c5e3 462
4eb72c6b 463 if(!ap->prealloced_context)
8e2fd8e1 464 ic = avformat_alloc_context();
4eb72c6b
NS
465 else
466 ic = *ic_ptr;
da24c5e3 467 if (!ic) {
769e10f0 468 err = AVERROR(ENOMEM);
da24c5e3
FB
469 goto fail;
470 }
4f731c44
AK
471 if (pb && fmt && fmt->flags & AVFMT_NOFILE)
472 av_log(ic, AV_LOG_WARNING, "Custom AVIOContext makes no sense and "
473 "will be ignored with AVFMT_NOFILE format.\n");
474 else
475 ic->pb = pb;
fb2758c8 476
13551ad1
AK
477 if ((err = avformat_open_input(&ic, filename, fmt, &opts)) < 0)
478 goto fail;
4f731c44 479 ic->pb = ic->pb ? ic->pb : pb; // don't leak custom pb if it wasn't set above
fb2758c8 480
603b8bc2 481fail:
63d64228 482 *ic_ptr = ic;
603b8bc2 483 av_dict_free(&opts);
da24c5e3
FB
484 return err;
485}
603b8bc2 486#endif
da24c5e3 487
a85736f2 488/** size of probe buffer, for guessing file type from file contents */
a877eedc 489#define PROBE_BUF_MIN 2048
329b1e75 490#define PROBE_BUF_MAX (1<<20)
b9a281db 491
ae628ec1 492int av_probe_input_buffer(AVIOContext *pb, AVInputFormat **fmt,
eadd495d
MG
493 const char *filename, void *logctx,
494 unsigned int offset, unsigned int max_probe_size)
495{
496 AVProbeData pd = { filename ? filename : "", NULL, -offset };
497 unsigned char *buf = NULL;
498 int ret = 0, probe_size;
499
500 if (!max_probe_size) {
501 max_probe_size = PROBE_BUF_MAX;
502 } else if (max_probe_size > PROBE_BUF_MAX) {
503 max_probe_size = PROBE_BUF_MAX;
504 } else if (max_probe_size < PROBE_BUF_MIN) {
505 return AVERROR(EINVAL);
506 }
507
508 if (offset >= max_probe_size) {
509 return AVERROR(EINVAL);
510 }
511
5ef953e8 512 for(probe_size= PROBE_BUF_MIN; probe_size<=max_probe_size && !*fmt;
532aa889 513 probe_size = FFMIN(probe_size<<1, FFMAX(max_probe_size, probe_size+1))) {
5ef953e8 514 int score = probe_size < max_probe_size ? AVPROBE_SCORE_MAX/4 : 0;
eadd495d
MG
515 int buf_offset = (probe_size == PROBE_BUF_MIN) ? 0 : probe_size>>1;
516
517 if (probe_size < offset) {
518 continue;
519 }
520
521 /* read probe data */
522 buf = av_realloc(buf, probe_size + AVPROBE_PADDING_SIZE);
b7effd4e 523 if ((ret = avio_read(pb, buf + buf_offset, probe_size - buf_offset)) < 0) {
eadd495d
MG
524 /* fail if error was not end of file, otherwise, lower score */
525 if (ret != AVERROR_EOF) {
526 av_free(buf);
527 return ret;
528 }
529 score = 0;
c7f625ee 530 ret = 0; /* error was end of file, nothing read */
eadd495d
MG
531 }
532 pd.buf_size += ret;
533 pd.buf = &buf[offset];
534
535 memset(pd.buf + pd.buf_size, 0, AVPROBE_PADDING_SIZE);
536
537 /* guess file format */
538 *fmt = av_probe_input_format2(&pd, 1, &score);
539 if(*fmt){
540 if(score <= AVPROBE_SCORE_MAX/4){ //this can only be true in the last iteration
541 av_log(logctx, AV_LOG_WARNING, "Format detected only with low score of %d, misdetection possible!\n", score);
542 }else
543 av_log(logctx, AV_LOG_DEBUG, "Probed with size=%d and score=%d\n", probe_size, score);
544 }
545 }
546
532aa889 547 if (!*fmt) {
01d91b9b 548 av_free(buf);
532aa889
MG
549 return AVERROR_INVALIDDATA;
550 }
551
01d91b9b 552 /* rewind. reuse probe buffer to avoid seeking */
f1ef2cd9 553 if ((ret = ffio_rewind_with_probe_data(pb, buf, pd.buf_size)) < 0)
01d91b9b 554 av_free(buf);
eadd495d 555
01d91b9b 556 return ret;
eadd495d
MG
557}
558
603b8bc2 559#if FF_API_FORMAT_PARAMETERS
115329f1 560int av_open_input_file(AVFormatContext **ic_ptr, const char *filename,
b9a281db
FB
561 AVInputFormat *fmt,
562 int buf_size,
563 AVFormatParameters *ap)
de6d9b64 564{
eadd495d 565 int err;
603b8bc2 566 AVDictionary *opts = convert_format_parameters(ap);
de6d9b64 567
dbafb0e0 568 if (!ap || !ap->prealloced_context)
603b8bc2 569 *ic_ptr = NULL;
b9a281db 570
603b8bc2 571 err = avformat_open_input(ic_ptr, filename, fmt, &opts);
115329f1 572
603b8bc2 573 av_dict_free(&opts);
b9a281db 574 return err;
de6d9b64 575}
603b8bc2 576#endif
de6d9b64 577
05e84c95 578/* open input file and probe the format if necessary */
32caa7b1 579static int init_input(AVFormatContext *s, const char *filename, AVDictionary **options)
05e84c95
AK
580{
581 int ret;
582 AVProbeData pd = {filename, NULL, 0};
583
584 if (s->pb) {
585 s->flags |= AVFMT_FLAG_CUSTOM_IO;
586 if (!s->iformat)
587 return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, 0);
588 else if (s->iformat->flags & AVFMT_NOFILE)
589 return AVERROR(EINVAL);
590 return 0;
591 }
592
593 if ( (s->iformat && s->iformat->flags & AVFMT_NOFILE) ||
594 (!s->iformat && (s->iformat = av_probe_input_format(&pd, 0))))
595 return 0;
596
9d77a8fa 597 if ((ret = avio_open2(&s->pb, filename, AVIO_FLAG_READ,
32caa7b1 598 &s->interrupt_callback, options)) < 0)
7e6029f9 599 return ret;
05e84c95
AK
600 if (s->iformat)
601 return 0;
602 return av_probe_input_buffer(s->pb, &s->iformat, filename, s, 0, 0);
603}
604
605int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputFormat *fmt, AVDictionary **options)
606{
607 AVFormatContext *s = *ps;
608 int ret = 0;
5a819c5e 609 AVFormatParameters ap = { { 0 } };
05e84c95
AK
610 AVDictionary *tmp = NULL;
611
612 if (!s && !(s = avformat_alloc_context()))
613 return AVERROR(ENOMEM);
614 if (fmt)
615 s->iformat = fmt;
616
617 if (options)
618 av_dict_copy(&tmp, *options, 0);
619
620 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
621 goto fail;
622
32caa7b1 623 if ((ret = init_input(s, filename, &tmp)) < 0)
05e84c95
AK
624 goto fail;
625
626 /* check filename in case an image number is expected */
627 if (s->iformat->flags & AVFMT_NEEDNUMBER) {
628 if (!av_filename_number_test(filename)) {
629 ret = AVERROR(EINVAL);
630 goto fail;
631 }
632 }
633
634 s->duration = s->start_time = AV_NOPTS_VALUE;
635 av_strlcpy(s->filename, filename, sizeof(s->filename));
636
637 /* allocate private data */
638 if (s->iformat->priv_data_size > 0) {
639 if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
640 ret = AVERROR(ENOMEM);
641 goto fail;
642 }
643 if (s->iformat->priv_class) {
644 *(const AVClass**)s->priv_data = s->iformat->priv_class;
645 av_opt_set_defaults(s->priv_data);
646 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
647 goto fail;
648 }
649 }
650
651 /* e.g. AVFMT_NOFILE formats will not have a AVIOContext */
652 if (s->pb)
653 ff_id3v2_read(s, ID3v2_DEFAULT_MAGIC);
654
655 if (s->iformat->read_header)
656 if ((ret = s->iformat->read_header(s, &ap)) < 0)
657 goto fail;
658
659 if (s->pb && !s->data_offset)
660 s->data_offset = avio_tell(s->pb);
661
662 s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
663
664 if (options) {
665 av_dict_free(options);
666 *options = tmp;
667 }
668 *ps = s;
669 return 0;
670
671fail:
672 av_dict_free(&tmp);
673 if (s->pb && !(s->flags & AVFMT_FLAG_CUSTOM_IO))
674 avio_close(s->pb);
675 avformat_free_context(s);
676 *ps = NULL;
677 return ret;
678}
679
da24c5e3
FB
680/*******************************************************/
681
5c5b1731
MR
682static AVPacket *add_to_pktbuf(AVPacketList **packet_buffer, AVPacket *pkt,
683 AVPacketList **plast_pktl){
684 AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
9d3b9f2c
MN
685 if (!pktl)
686 return NULL;
687
5c5b1731
MR
688 if (*packet_buffer)
689 (*plast_pktl)->next = pktl;
690 else
691 *packet_buffer = pktl;
692
9d3b9f2c
MN
693 /* add the packet in the buffered packet list */
694 *plast_pktl = pktl;
695 pktl->pkt= *pkt;
696 return &pktl->pkt;
697}
698
de6d9b64
FB
699int av_read_packet(AVFormatContext *s, AVPacket *pkt)
700{
86cb7e33 701 int ret, i;
62600469 702 AVStream *st;
0bef08e5
MN
703
704 for(;;){
705 AVPacketList *pktl = s->raw_packet_buffer;
706
707 if (pktl) {
708 *pkt = pktl->pkt;
86cb7e33 709 if(s->streams[pkt->stream_index]->codec->codec_id != CODEC_ID_PROBE ||
af122d6a
BC
710 !s->streams[pkt->stream_index]->probe_packets ||
711 s->raw_packet_buffer_remaining_size < pkt->size){
712 AVProbeData *pd = &s->streams[pkt->stream_index]->probe_data;
713 av_freep(&pd->buf);
714 pd->buf_size = 0;
0bef08e5 715 s->raw_packet_buffer = pktl->next;
af122d6a 716 s->raw_packet_buffer_remaining_size += pkt->size;
0bef08e5
MN
717 av_free(pktl);
718 return 0;
719 }
720 }
721
55823964
MN
722 av_init_packet(pkt);
723 ret= s->iformat->read_packet(s, pkt);
86cb7e33
BC
724 if (ret < 0) {
725 if (!pktl || ret == AVERROR(EAGAIN))
726 return ret;
727 for (i = 0; i < s->nb_streams; i++)
728 s->streams[i]->probe_packets = 0;
729 continue;
730 }
73e8e8db
ZK
731
732 if ((s->flags & AVFMT_FLAG_DISCARD_CORRUPT) &&
733 (pkt->flags & AV_PKT_FLAG_CORRUPT)) {
734 av_log(s, AV_LOG_WARNING,
735 "Dropped corrupted packet (stream = %d)\n",
736 pkt->stream_index);
055a141e 737 av_free_packet(pkt);
73e8e8db
ZK
738 continue;
739 }
740
55823964 741 st= s->streams[pkt->stream_index];
62600469 742
55823964 743 switch(st->codec->codec_type){
72415b2a 744 case AVMEDIA_TYPE_VIDEO:
55823964
MN
745 if(s->video_codec_id) st->codec->codec_id= s->video_codec_id;
746 break;
72415b2a 747 case AVMEDIA_TYPE_AUDIO:
55823964
MN
748 if(s->audio_codec_id) st->codec->codec_id= s->audio_codec_id;
749 break;
72415b2a 750 case AVMEDIA_TYPE_SUBTITLE:
55823964
MN
751 if(s->subtitle_codec_id)st->codec->codec_id= s->subtitle_codec_id;
752 break;
753 }
62600469 754
86cb7e33
BC
755 if(!pktl && (st->codec->codec_id != CODEC_ID_PROBE ||
756 !st->probe_packets))
744b4c02
MN
757 return ret;
758
5c5b1731 759 add_to_pktbuf(&s->raw_packet_buffer, pkt, &s->raw_packet_buffer_end);
af122d6a 760 s->raw_packet_buffer_remaining_size -= pkt->size;
744b4c02 761
0bef08e5
MN
762 if(st->codec->codec_id == CODEC_ID_PROBE){
763 AVProbeData *pd = &st->probe_data;
0e500e0d 764 av_log(s, AV_LOG_DEBUG, "probing stream %d\n", st->index);
86cb7e33
BC
765 --st->probe_packets;
766
0bef08e5
MN
767 pd->buf = av_realloc(pd->buf, pd->buf_size+pkt->size+AVPROBE_PADDING_SIZE);
768 memcpy(pd->buf+pd->buf_size, pkt->data, pkt->size);
769 pd->buf_size += pkt->size;
770 memset(pd->buf+pd->buf_size, 0, AVPROBE_PADDING_SIZE);
771
942de2f4 772 if(av_log2(pd->buf_size) != av_log2(pd->buf_size - pkt->size)){
0f1f4816
MN
773 //FIXME we dont reduce score to 0 for the case of running out of buffer space in bytes
774 set_codec_from_probe_data(s, st, pd, st->probe_packets > 0 ? AVPROBE_SCORE_MAX/4 : 0);
25d3fb73
MN
775 if(st->codec->codec_id != CODEC_ID_PROBE){
776 pd->buf_size=0;
777 av_freep(&pd->buf);
0e500e0d 778 av_log(s, AV_LOG_DEBUG, "probed stream %d\n", st->index);
25d3fb73 779 }
942de2f4 780 }
0bef08e5
MN
781 }
782 }
fb2758c8
FB
783}
784
785/**********************************************************/
786
e36bdf8b 787/**
a85736f2 788 * Get the number of samples of an audio frame. Return -1 on error.
e36bdf8b 789 */
fb2758c8
FB
790static int get_audio_frame_size(AVCodecContext *enc, int size)
791{
792 int frame_size;
793
c924ca78
MN
794 if(enc->codec_id == CODEC_ID_VORBIS)
795 return -1;
796
fb2758c8 797 if (enc->frame_size <= 1) {
ac3e1834
BC
798 int bits_per_sample = av_get_bits_per_sample(enc->codec_id);
799
800 if (bits_per_sample) {
fb2758c8
FB
801 if (enc->channels == 0)
802 return -1;
f1b163e0 803 frame_size = (size << 3) / (bits_per_sample * enc->channels);
ac3e1834 804 } else {
fb2758c8
FB
805 /* used for example by ADPCM codecs */
806 if (enc->bit_rate == 0)
807 return -1;
db5dc02b 808 frame_size = ((int64_t)size * 8 * enc->sample_rate) / enc->bit_rate;
fb2758c8
FB
809 }
810 } else {
811 frame_size = enc->frame_size;
812 }
813 return frame_size;
814}
815
816
e36bdf8b 817/**
a85736f2 818 * Return the frame duration in seconds. Return 0 if not available.
e36bdf8b 819 */
115329f1 820static void compute_frame_duration(int *pnum, int *pden, AVStream *st,
fb2758c8
FB
821 AVCodecParserContext *pc, AVPacket *pkt)
822{
823 int frame_size;
824
825 *pnum = 0;
826 *pden = 0;
01f4895c 827 switch(st->codec->codec_type) {
72415b2a 828 case AVMEDIA_TYPE_VIDEO:
20922325
AK
829 if (st->r_frame_rate.num) {
830 *pnum = st->r_frame_rate.den;
831 *pden = st->r_frame_rate.num;
832 } else if(st->time_base.num*1000LL > st->time_base.den) {
c0df9d75
MN
833 *pnum = st->time_base.num;
834 *pden = st->time_base.den;
01f4895c
MN
835 }else if(st->codec->time_base.num*1000LL > st->codec->time_base.den){
836 *pnum = st->codec->time_base.num;
837 *pden = st->codec->time_base.den;
327c4076 838 if (pc && pc->repeat_pict) {
810c451b 839 *pnum = (*pnum) * (1 + pc->repeat_pict);
327c4076 840 }
497431a5
MN
841 //If this codec can be interlaced or progressive then we need a parser to compute duration of a packet
842 //Thus if we have no parser in such case leave duration undefined.
843 if(st->codec->ticks_per_frame>1 && !pc){
844 *pnum = *pden = 0;
845 }
fb2758c8
FB
846 }
847 break;
72415b2a 848 case AVMEDIA_TYPE_AUDIO:
01f4895c 849 frame_size = get_audio_frame_size(st->codec, pkt->size);
6cbce636 850 if (frame_size <= 0 || st->codec->sample_rate <= 0)
fb2758c8
FB
851 break;
852 *pnum = frame_size;
01f4895c 853 *pden = st->codec->sample_rate;
fb2758c8
FB
854 break;
855 default:
856 break;
857 }
858}
859
5ba7c3d7 860static int is_intra_only(AVCodecContext *enc){
72415b2a 861 if(enc->codec_type == AVMEDIA_TYPE_AUDIO){
5ba7c3d7 862 return 1;
72415b2a 863 }else if(enc->codec_type == AVMEDIA_TYPE_VIDEO){
5ba7c3d7
MN
864 switch(enc->codec_id){
865 case CODEC_ID_MJPEG:
866 case CODEC_ID_MJPEGB:
867 case CODEC_ID_LJPEG:
5cc2530f 868 case CODEC_ID_PRORES:
5ba7c3d7
MN
869 case CODEC_ID_RAWVIDEO:
870 case CODEC_ID_DVVIDEO:
871 case CODEC_ID_HUFFYUV:
f37b9768 872 case CODEC_ID_FFVHUFF:
5ba7c3d7
MN
873 case CODEC_ID_ASV1:
874 case CODEC_ID_ASV2:
875 case CODEC_ID_VCR1:
b774fdd7 876 case CODEC_ID_DNXHD:
aa915625 877 case CODEC_ID_JPEG2000:
5ba7c3d7
MN
878 return 1;
879 default: break;
880 }
881 }
882 return 0;
883}
884
9fcbcca6
NB
885static void update_initial_timestamps(AVFormatContext *s, int stream_index,
886 int64_t dts, int64_t pts)
887{
82583548
MN
888 AVStream *st= s->streams[stream_index];
889 AVPacketList *pktl= s->packet_buffer;
890
7efeb73a 891 if(st->first_dts != AV_NOPTS_VALUE || dts == AV_NOPTS_VALUE || st->cur_dts == AV_NOPTS_VALUE)
82583548
MN
892 return;
893
894 st->first_dts= dts - st->cur_dts;
895 st->cur_dts= dts;
896
897 for(; pktl; pktl= pktl->next){
898 if(pktl->pkt.stream_index != stream_index)
899 continue;
900 //FIXME think more about this check
901 if(pktl->pkt.pts != AV_NOPTS_VALUE && pktl->pkt.pts == pktl->pkt.dts)
902 pktl->pkt.pts += st->first_dts;
903
904 if(pktl->pkt.dts != AV_NOPTS_VALUE)
905 pktl->pkt.dts += st->first_dts;
48a59dfe
MN
906
907 if(st->start_time == AV_NOPTS_VALUE && pktl->pkt.pts != AV_NOPTS_VALUE)
908 st->start_time= pktl->pkt.pts;
82583548 909 }
9fcbcca6
NB
910 if (st->start_time == AV_NOPTS_VALUE)
911 st->start_time = pts;
82583548
MN
912}
913
83a9db42
MN
914static void update_initial_durations(AVFormatContext *s, AVStream *st, AVPacket *pkt)
915{
916 AVPacketList *pktl= s->packet_buffer;
820ad60c
MN
917 int64_t cur_dts= 0;
918
919 if(st->first_dts != AV_NOPTS_VALUE){
920 cur_dts= st->first_dts;
921 for(; pktl; pktl= pktl->next){
922 if(pktl->pkt.stream_index == pkt->stream_index){
923 if(pktl->pkt.pts != pktl->pkt.dts || pktl->pkt.dts != AV_NOPTS_VALUE || pktl->pkt.duration)
924 break;
925 cur_dts -= pkt->duration;
926 }
927 }
928 pktl= s->packet_buffer;
929 st->first_dts = cur_dts;
930 }else if(st->cur_dts)
931 return;
83a9db42
MN
932
933 for(; pktl; pktl= pktl->next){
934 if(pktl->pkt.stream_index != pkt->stream_index)
935 continue;
91acf9a8
MN
936 if(pktl->pkt.pts == pktl->pkt.dts && pktl->pkt.dts == AV_NOPTS_VALUE
937 && !pktl->pkt.duration){
820ad60c 938 pktl->pkt.dts= cur_dts;
5853423c 939 if(!st->codec->has_b_frames)
820ad60c
MN
940 pktl->pkt.pts= cur_dts;
941 cur_dts += pkt->duration;
83a9db42
MN
942 pktl->pkt.duration= pkt->duration;
943 }else
944 break;
945 }
820ad60c
MN
946 if(st->first_dts == AV_NOPTS_VALUE)
947 st->cur_dts= cur_dts;
83a9db42
MN
948}
949
115329f1 950static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
fb2758c8
FB
951 AVCodecParserContext *pc, AVPacket *pkt)
952{
d9e1efb7 953 int num, den, presentation_delayed, delay, i;
a74008a4 954 int64_t offset;
115329f1 955
fe8344a2
MN
956 if (s->flags & AVFMT_FLAG_NOFILLIN)
957 return;
958
c55806e3
MN
959 if((s->flags & AVFMT_FLAG_IGNDTS) && pkt->pts != AV_NOPTS_VALUE)
960 pkt->dts= AV_NOPTS_VALUE;
961
975a1447 962 if (st->codec->codec_id != CODEC_ID_H264 && pc && pc->pict_type == AV_PICTURE_TYPE_B)
e18027ac 963 //FIXME Set low_delay = 0 when has_b_frames = 1
818062f2
BC
964 st->codec->has_b_frames = 1;
965
f781f748
MN
966 /* do we have a video B-frame ? */
967 delay= st->codec->has_b_frames;
968 presentation_delayed = 0;
37b00b47 969
f781f748
MN
970 /* XXX: need has_b_frame, but cannot get it if the codec is
971 not initialized */
972 if (delay &&
975a1447 973 pc && pc->pict_type != AV_PICTURE_TYPE_B)
f781f748
MN
974 presentation_delayed = 1;
975
10a7571b
MN
976 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && pkt->dts > pkt->pts && st->pts_wrap_bits<63
977 /*&& pkt->dts-(1LL<<st->pts_wrap_bits) < pkt->pts*/){
978 pkt->dts -= 1LL<<st->pts_wrap_bits;
979 }
980
9806f846
MN
981 // some mpeg2 in mpeg-ps lack dts (issue171 / input_file.mpg)
982 // we take the conservative approach and discard both
983 // Note, if this is misbehaving for a H.264 file then possibly presentation_delayed is not set correctly.
984 if(delay==1 && pkt->dts == pkt->pts && pkt->dts != AV_NOPTS_VALUE && presentation_delayed){
b0a18c2f 985 av_log(s, AV_LOG_DEBUG, "invalid dts/pts combination\n");
9806f846
MN
986 pkt->dts= pkt->pts= AV_NOPTS_VALUE;
987 }
988
fb2758c8 989 if (pkt->duration == 0) {
3c895fc0 990 compute_frame_duration(&num, &den, st, pc, pkt);
fb2758c8 991 if (den && num) {
0e1f78f9 992 pkt->duration = av_rescale_rnd(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num, AV_ROUND_DOWN);
83a9db42 993
820ad60c 994 if(pkt->duration != 0 && s->packet_buffer)
83a9db42 995 update_initial_durations(s, st, pkt);
fb2758c8
FB
996 }
997 }
998
a85736f2
DB
999 /* correct timestamps with byte offset if demuxers only have timestamps
1000 on packet boundaries */
a74008a4
JP
1001 if(pc && st->need_parsing == AVSTREAM_PARSE_TIMESTAMPS && pkt->size){
1002 /* this will estimate bitrate based on this frame's duration and size */
1003 offset = av_rescale(pc->offset, pkt->duration, pkt->size);
1004 if(pkt->pts != AV_NOPTS_VALUE)
1005 pkt->pts += offset;
1006 if(pkt->dts != AV_NOPTS_VALUE)
1007 pkt->dts += offset;
1008 }
1009
27ca0a79
IS
1010 if (pc && pc->dts_sync_point >= 0) {
1011 // we have synchronization info from the parser
1012 int64_t den = st->codec->time_base.den * (int64_t) st->time_base.num;
1013 if (den > 0) {
1014 int64_t num = st->codec->time_base.num * (int64_t) st->time_base.den;
1015 if (pkt->dts != AV_NOPTS_VALUE) {
1016 // got DTS from the stream, update reference timestamp
1017 st->reference_dts = pkt->dts - pc->dts_ref_dts_delta * num / den;
1018 pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
1019 } else if (st->reference_dts != AV_NOPTS_VALUE) {
1020 // compute DTS based on reference timestamp
1021 pkt->dts = st->reference_dts + pc->dts_ref_dts_delta * num / den;
1022 pkt->pts = pkt->dts + pc->pts_dts_delta * num / den;
1023 }
1024 if (pc->dts_sync_point > 0)
1025 st->reference_dts = pkt->dts; // new reference
1026 }
1027 }
1028
755bfeab 1029 /* This may be redundant, but it should not hurt. */
7e4baa66
MN
1030 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts)
1031 presentation_delayed = 1;
115329f1 1032
949b1a13 1033// 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);
fb2758c8 1034 /* interpolate PTS and DTS if they are not present */
9e6c124a
MN
1035 //We skip H264 currently because delay and has_b_frames are not reliably set
1036 if((delay==0 || (delay==1 && pc)) && st->codec->codec_id != CODEC_ID_H264){
7e4baa66 1037 if (presentation_delayed) {
a85736f2
DB
1038 /* DTS = decompression timestamp */
1039 /* PTS = presentation timestamp */
7e4baa66 1040 if (pkt->dts == AV_NOPTS_VALUE)
635fbcb1 1041 pkt->dts = st->last_IP_pts;
9fcbcca6 1042 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts);
7e4baa66
MN
1043 if (pkt->dts == AV_NOPTS_VALUE)
1044 pkt->dts = st->cur_dts;
1045
1046 /* this is tricky: the dts must be incremented by the duration
a85736f2 1047 of the frame we are displaying, i.e. the last I- or P-frame */
635fbcb1
MN
1048 if (st->last_IP_duration == 0)
1049 st->last_IP_duration = pkt->duration;
7efeb73a 1050 if(pkt->dts != AV_NOPTS_VALUE)
cdb5af79 1051 st->cur_dts = pkt->dts + st->last_IP_duration;
635fbcb1
MN
1052 st->last_IP_duration = pkt->duration;
1053 st->last_IP_pts= pkt->pts;
7e4baa66 1054 /* cannot compute PTS if not present (we can compute it only
a85736f2 1055 by knowing the future */
028d6f3e 1056 } else if(pkt->pts != AV_NOPTS_VALUE || pkt->dts != AV_NOPTS_VALUE || pkt->duration){
7e4baa66
MN
1057 if(pkt->pts != AV_NOPTS_VALUE && pkt->duration){
1058 int64_t old_diff= FFABS(st->cur_dts - pkt->duration - pkt->pts);
1059 int64_t new_diff= FFABS(st->cur_dts - pkt->pts);
1060 if(old_diff < new_diff && old_diff < (pkt->duration>>3)){
1061 pkt->pts += pkt->duration;
1062 // 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);
1063 }
90bb394d 1064 }
115329f1 1065
7e4baa66
MN
1066 /* presentation is not delayed : PTS and DTS are the same */
1067 if(pkt->pts == AV_NOPTS_VALUE)
1068 pkt->pts = pkt->dts;
9fcbcca6 1069 update_initial_timestamps(s, pkt->stream_index, pkt->pts, pkt->pts);
7e4baa66
MN
1070 if(pkt->pts == AV_NOPTS_VALUE)
1071 pkt->pts = st->cur_dts;
1072 pkt->dts = pkt->pts;
7efeb73a 1073 if(pkt->pts != AV_NOPTS_VALUE)
cdb5af79 1074 st->cur_dts = pkt->pts + pkt->duration;
7e4baa66 1075 }
fb2758c8 1076 }
d9e1efb7 1077
cb5b96cd 1078 if(pkt->pts != AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
d9e1efb7 1079 st->pts_buffer[0]= pkt->pts;
d9e1efb7
MN
1080 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
1081 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
1082 if(pkt->dts == AV_NOPTS_VALUE)
1083 pkt->dts= st->pts_buffer[0];
9e6c124a 1084 if(st->codec->codec_id == CODEC_ID_H264){ //we skiped it above so we try here
9fcbcca6 1085 update_initial_timestamps(s, pkt->stream_index, pkt->dts, pkt->pts); // this should happen on the first packet
82583548 1086 }
d9e1efb7
MN
1087 if(pkt->dts > st->cur_dts)
1088 st->cur_dts = pkt->dts;
1089 }
1090
1091// 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);
115329f1 1092
fb2758c8 1093 /* update flags */
3a1d3588 1094 if(is_intra_only(st->codec))
cc947f04 1095 pkt->flags |= AV_PKT_FLAG_KEY;
3a1d3588 1096 else if (pc) {
fb2758c8 1097 pkt->flags = 0;
a85736f2 1098 /* keyframe computation */
6363af44 1099 if (pc->key_frame == 1)
cc947f04 1100 pkt->flags |= AV_PKT_FLAG_KEY;
975a1447 1101 else if (pc->key_frame == -1 && pc->pict_type == AV_PICTURE_TYPE_I)
cc947f04 1102 pkt->flags |= AV_PKT_FLAG_KEY;
fb2758c8 1103 }
b1fa4942
IS
1104 if (pc)
1105 pkt->convergence_duration = pc->convergence_duration;
fb2758c8
FB
1106}
1107
fb2758c8 1108
d3bb7191 1109static int read_frame_internal(AVFormatContext *s, AVPacket *pkt)
fb2758c8
FB
1110{
1111 AVStream *st;
37353960 1112 int len, ret, i;
fb2758c8 1113
b237eb80
MN
1114 av_init_packet(pkt);
1115
fb2758c8
FB
1116 for(;;) {
1117 /* select current input stream component */
1118 st = s->cur_st;
1119 if (st) {
90ad92b3 1120 if (!st->need_parsing || !st->parser) {
fb2758c8
FB
1121 /* no parsing needed: we just output the packet as is */
1122 /* raw data support */
3a41c2f7 1123 *pkt = st->cur_pkt; st->cur_pkt.data= NULL;
fb2758c8
FB
1124 compute_pkt_fields(s, st, NULL, pkt);
1125 s->cur_st = NULL;
74f5ae84 1126 if ((s->iformat->flags & AVFMT_GENERIC_INDEX) &&
cc947f04 1127 (pkt->flags & AV_PKT_FLAG_KEY) && pkt->dts != AV_NOPTS_VALUE) {
74f5ae84
RD
1128 ff_reduce_index(s, st->index);
1129 av_add_index_entry(st, pkt->pos, pkt->dts, 0, 0, AVINDEX_KEYFRAME);
1130 }
434cab9e 1131 break;
3a41c2f7 1132 } else if (st->cur_len > 0 && st->discard < AVDISCARD_ALL) {
61c23c15
IS
1133 len = av_parser_parse2(st->parser, st->codec, &pkt->data, &pkt->size,
1134 st->cur_ptr, st->cur_len,
1135 st->cur_pkt.pts, st->cur_pkt.dts,
1136 st->cur_pkt.pos);
3a41c2f7
MN
1137 st->cur_pkt.pts = AV_NOPTS_VALUE;
1138 st->cur_pkt.dts = AV_NOPTS_VALUE;
fb2758c8 1139 /* increment read pointer */
3a41c2f7
MN
1140 st->cur_ptr += len;
1141 st->cur_len -= len;
115329f1 1142
fb2758c8
FB
1143 /* return packet if any */
1144 if (pkt->size) {
37353960 1145 got_packet:
fb2758c8
FB
1146 pkt->duration = 0;
1147 pkt->stream_index = st->index;
6ec87caa
FB
1148 pkt->pts = st->parser->pts;
1149 pkt->dts = st->parser->dts;
61c23c15 1150 pkt->pos = st->parser->pos;
a5266a47
MN
1151 if(pkt->data == st->cur_pkt.data && pkt->size == st->cur_pkt.size){
1152 s->cur_st = NULL;
1153 pkt->destruct= st->cur_pkt.destruct;
22d78b05 1154 st->cur_pkt.destruct= NULL;
a5266a47
MN
1155 st->cur_pkt.data = NULL;
1156 assert(st->cur_len == 0);
1157 }else{
7e6029f9 1158 pkt->destruct = NULL;
a5266a47 1159 }
fb2758c8 1160 compute_pkt_fields(s, st, st->parser, pkt);
e9b78eeb 1161
cc947f04 1162 if((s->iformat->flags & AVFMT_GENERIC_INDEX) && pkt->flags & AV_PKT_FLAG_KEY){
3dea63bd 1163 ff_reduce_index(s, st->index);
e9b78eeb
MN
1164 av_add_index_entry(st, st->parser->frame_offset, pkt->dts,
1165 0, 0, AVINDEX_KEYFRAME);
1166 }
1167
434cab9e 1168 break;
fb2758c8
FB
1169 }
1170 } else {
bcbecff1 1171 /* free packet */
3a41c2f7 1172 av_free_packet(&st->cur_pkt);
fb2758c8
FB
1173 s->cur_st = NULL;
1174 }
1175 } else {
3a41c2f7 1176 AVPacket cur_pkt;
fb2758c8 1177 /* read next packet */
3a41c2f7 1178 ret = av_read_packet(s, &cur_pkt);
37353960 1179 if (ret < 0) {
8fa36ae0 1180 if (ret == AVERROR(EAGAIN))
37353960
FB
1181 return ret;
1182 /* return the last frames, if any */
1183 for(i = 0; i < s->nb_streams; i++) {
1184 st = s->streams[i];
90ad92b3 1185 if (st->parser && st->need_parsing) {
61c23c15 1186 av_parser_parse2(st->parser, st->codec,
115329f1
DB
1187 &pkt->data, &pkt->size,
1188 NULL, 0,
61c23c15
IS
1189 AV_NOPTS_VALUE, AV_NOPTS_VALUE,
1190 AV_NOPTS_VALUE);
37353960
FB
1191 if (pkt->size)
1192 goto got_packet;
1193 }
1194 }
a85736f2 1195 /* no more packets: really terminate parsing */
fb2758c8 1196 return ret;
37353960 1197 }
3a41c2f7
MN
1198 st = s->streams[cur_pkt.stream_index];
1199 st->cur_pkt= cur_pkt;
115329f1 1200
3a41c2f7
MN
1201 if(st->cur_pkt.pts != AV_NOPTS_VALUE &&
1202 st->cur_pkt.dts != AV_NOPTS_VALUE &&
1203 st->cur_pkt.pts < st->cur_pkt.dts){
b18a4ab2 1204 av_log(s, AV_LOG_WARNING, "Invalid timestamps stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d\n",
3a41c2f7
MN
1205 st->cur_pkt.stream_index,
1206 st->cur_pkt.pts,
1207 st->cur_pkt.dts,
1208 st->cur_pkt.size);
1209// av_free_packet(&st->cur_pkt);
b18a4ab2
MN
1210// return -1;
1211 }
1212
45b2b05f 1213 if(s->debug & FF_FDEBUG_TS)
050ba6f4 1214 av_log(s, AV_LOG_DEBUG, "av_read_packet stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
3a41c2f7
MN
1215 st->cur_pkt.stream_index,
1216 st->cur_pkt.pts,
1217 st->cur_pkt.dts,
1218 st->cur_pkt.size,
050ba6f4 1219 st->cur_pkt.duration,
3a41c2f7 1220 st->cur_pkt.flags);
fb2758c8 1221
fb2758c8 1222 s->cur_st = st;
3a41c2f7
MN
1223 st->cur_ptr = st->cur_pkt.data;
1224 st->cur_len = st->cur_pkt.size;
fe8344a2 1225 if (st->need_parsing && !st->parser && !(s->flags & AVFMT_FLAG_NOPARSE)) {
01f4895c 1226 st->parser = av_parser_init(st->codec->codec_id);
fb2758c8 1227 if (!st->parser) {
a85736f2 1228 /* no parser available: just output the raw packets */
57004ff1
AJ
1229 st->need_parsing = AVSTREAM_PARSE_NONE;
1230 }else if(st->need_parsing == AVSTREAM_PARSE_HEADERS){
7cbaa7ba 1231 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
74a6df59
AC
1232 }else if(st->need_parsing == AVSTREAM_PARSE_FULL_ONCE){
1233 st->parser->flags |= PARSER_FLAG_ONCE;
fb2758c8
FB
1234 }
1235 }
1236 }
1237 }
45b2b05f 1238 if(s->debug & FF_FDEBUG_TS)
d3bb7191 1239 av_log(s, AV_LOG_DEBUG, "read_frame_internal stream=%d, pts=%"PRId64", dts=%"PRId64", size=%d, duration=%d, flags=%d\n",
434cab9e
MN
1240 pkt->stream_index,
1241 pkt->pts,
1242 pkt->dts,
3041a4a1 1243 pkt->size,
050ba6f4 1244 pkt->duration,
3041a4a1 1245 pkt->flags);
434cab9e
MN
1246
1247 return 0;
fb2758c8
FB
1248}
1249
fb2758c8
FB
1250int av_read_frame(AVFormatContext *s, AVPacket *pkt)
1251{
de6d9b64 1252 AVPacketList *pktl;
30bc6613
MN
1253 int eof=0;
1254 const int genpts= s->flags & AVFMT_FLAG_GENPTS;
1255
1256 for(;;){
1257 pktl = s->packet_buffer;
1258 if (pktl) {
1259 AVPacket *next_pkt= &pktl->pkt;
30bc6613
MN
1260
1261 if(genpts && next_pkt->dts != AV_NOPTS_VALUE){
5be5d28c 1262 int wrap_bits = s->streams[next_pkt->stream_index]->pts_wrap_bits;
30bc6613 1263 while(pktl && next_pkt->pts == AV_NOPTS_VALUE){
115329f1 1264 if( pktl->pkt.stream_index == next_pkt->stream_index
5be5d28c
SD
1265 && (0 > av_compare_mod(next_pkt->dts, pktl->pkt.dts, 2LL << (wrap_bits - 1)))
1266 && av_compare_mod(pktl->pkt.pts, pktl->pkt.dts, 2LL << (wrap_bits - 1))) { //not b frame
30bc6613
MN
1267 next_pkt->pts= pktl->pkt.dts;
1268 }
1269 pktl= pktl->next;
1270 }
1271 pktl = s->packet_buffer;
1272 }
115329f1
DB
1273
1274 if( next_pkt->pts != AV_NOPTS_VALUE
1275 || next_pkt->dts == AV_NOPTS_VALUE
30bc6613
MN
1276 || !genpts || eof){
1277 /* read packet from packet buffer, if there is data */
1278 *pkt = *next_pkt;
1279 s->packet_buffer = pktl->next;
1280 av_free(pktl);
1281 return 0;
1282 }
1283 }
1284 if(genpts){
d3bb7191 1285 int ret= read_frame_internal(s, pkt);
30bc6613 1286 if(ret<0){
8fa36ae0 1287 if(pktl && ret != AVERROR(EAGAIN)){
30bc6613
MN
1288 eof=1;
1289 continue;
1290 }else
1291 return ret;
1292 }
115329f1 1293
5c5b1731
MR
1294 if(av_dup_packet(add_to_pktbuf(&s->packet_buffer, pkt,
1295 &s->packet_buffer_end)) < 0)
769e10f0 1296 return AVERROR(ENOMEM);
30bc6613
MN
1297 }else{
1298 assert(!s->packet_buffer);
d3bb7191 1299 return read_frame_internal(s, pkt);
30bc6613 1300 }
fb2758c8
FB
1301 }
1302}
1303
1304/* XXX: suppress the packet queue */
1305static void flush_packet_queue(AVFormatContext *s)
1306{
1307 AVPacketList *pktl;
1308
1309 for(;;) {
1310 pktl = s->packet_buffer;
115329f1 1311 if (!pktl)
fb2758c8
FB
1312 break;
1313 s->packet_buffer = pktl->next;
1314 av_free_packet(&pktl->pkt);
1315 av_free(pktl);
b9a281db 1316 }
86cb7e33
BC
1317 while(s->raw_packet_buffer){
1318 pktl = s->raw_packet_buffer;
1319 s->raw_packet_buffer = pktl->next;
1320 av_free_packet(&pktl->pkt);
1321 av_free(pktl);
1322 }
42831b46
MN
1323 s->packet_buffer_end=
1324 s->raw_packet_buffer_end= NULL;
af122d6a 1325 s->raw_packet_buffer_remaining_size = RAW_PACKET_BUFFER_SIZE;
b9a281db
FB
1326}
1327
da24c5e3 1328/*******************************************************/
fb2758c8
FB
1329/* seek support */
1330
b754978a
MN
1331int av_find_default_stream_index(AVFormatContext *s)
1332{
ca162a50 1333 int first_audio_index = -1;
b754978a
MN
1334 int i;
1335 AVStream *st;
1336
1337 if (s->nb_streams <= 0)
1338 return -1;
1339 for(i = 0; i < s->nb_streams; i++) {
1340 st = s->streams[i];
72415b2a 1341 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
b754978a
MN
1342 return i;
1343 }
72415b2a 1344 if (first_audio_index < 0 && st->codec->codec_type == AVMEDIA_TYPE_AUDIO)
ca162a50 1345 first_audio_index = i;
b754978a 1346 }
ca162a50 1347 return first_audio_index >= 0 ? first_audio_index : 0;
b754978a
MN
1348}
1349
e36bdf8b
DK
1350/**
1351 * Flush the frame reader.
1352 */
972ffe62 1353void ff_read_frame_flush(AVFormatContext *s)
fb2758c8
FB
1354{
1355 AVStream *st;
106fa129 1356 int i, j;
fb2758c8
FB
1357
1358 flush_packet_queue(s);
1359
3a41c2f7 1360 s->cur_st = NULL;
115329f1 1361
fb2758c8
FB
1362 /* for each stream, reset read state */
1363 for(i = 0; i < s->nb_streams; i++) {
1364 st = s->streams[i];
115329f1 1365
fb2758c8
FB
1366 if (st->parser) {
1367 av_parser_close(st->parser);
1368 st->parser = NULL;
3a41c2f7 1369 av_free_packet(&st->cur_pkt);
fb2758c8 1370 }
635fbcb1 1371 st->last_IP_pts = AV_NOPTS_VALUE;
a843d1ff 1372 st->cur_dts = AV_NOPTS_VALUE; /* we set the current DTS to an unspecified origin */
27ca0a79 1373 st->reference_dts = AV_NOPTS_VALUE;
3a41c2f7
MN
1374 /* fail safe */
1375 st->cur_ptr = NULL;
1376 st->cur_len = 0;
86cb7e33
BC
1377
1378 st->probe_packets = MAX_PROBE_PACKETS;
106fa129
JS
1379
1380 for(j=0; j<MAX_REORDER_DELAY+1; j++)
1381 st->pts_buffer[j]= AV_NOPTS_VALUE;
fb2758c8
FB
1382 }
1383}
1384
a2faa951
AK
1385#if FF_API_SEEK_PUBLIC
1386void av_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1387{
eb0de710 1388 ff_update_cur_dts(s, ref_st, timestamp);
a2faa951
AK
1389}
1390#endif
1391
1392void ff_update_cur_dts(AVFormatContext *s, AVStream *ref_st, int64_t timestamp)
1393{
8bcb147f
MN
1394 int i;
1395
1396 for(i = 0; i < s->nb_streams; i++) {
1a1dc611 1397 AVStream *st = s->streams[i];
8bcb147f 1398
115329f1 1399 st->cur_dts = av_rescale(timestamp,
1a1dc611
NK
1400 st->time_base.den * (int64_t)ref_st->time_base.num,
1401 st->time_base.num * (int64_t)ref_st->time_base.den);
8bcb147f
MN
1402 }
1403}
1404
3dea63bd
PK
1405void ff_reduce_index(AVFormatContext *s, int stream_index)
1406{
1407 AVStream *st= s->streams[stream_index];
1408 unsigned int max_entries= s->max_index_size / sizeof(AVIndexEntry);
1409
1410 if((unsigned)st->nb_index_entries >= max_entries){
1411 int i;
1412 for(i=0; 2*i<st->nb_index_entries; i++)
1413 st->index_entries[i]= st->index_entries[2*i];
1414 st->nb_index_entries= i;
1415 }
1416}
1417
e6fb5a4f
PR
1418int ff_add_index_entry(AVIndexEntry **index_entries,
1419 int *nb_index_entries,
1420 unsigned int *index_entries_allocated_size,
1421 int64_t pos, int64_t timestamp, int size, int distance, int flags)
fb2758c8
FB
1422{
1423 AVIndexEntry *entries, *ie;
b754978a 1424 int index;
115329f1 1425
e6fb5a4f 1426 if((unsigned)*nb_index_entries + 1 >= UINT_MAX / sizeof(AVIndexEntry))
568e18b1 1427 return -1;
115329f1 1428
e6fb5a4f
PR
1429 entries = av_fast_realloc(*index_entries,
1430 index_entries_allocated_size,
1431 (*nb_index_entries + 1) *
fb2758c8 1432 sizeof(AVIndexEntry));
568e18b1
MN
1433 if(!entries)
1434 return -1;
1435
e6fb5a4f 1436 *index_entries= entries;
b754978a 1437
e6fb5a4f 1438 index= ff_index_search_timestamp(*index_entries, *nb_index_entries, timestamp, AVSEEK_FLAG_ANY);
b754978a 1439
3ba1438d 1440 if(index<0){
e6fb5a4f 1441 index= (*nb_index_entries)++;
3e9245a9 1442 ie= &entries[index];
3ba1438d
MN
1443 assert(index==0 || ie[-1].timestamp < timestamp);
1444 }else{
1445 ie= &entries[index];
1446 if(ie->timestamp != timestamp){
528c2c73
MN
1447 if(ie->timestamp <= timestamp)
1448 return -1;
e6fb5a4f
PR
1449 memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(*nb_index_entries - index));
1450 (*nb_index_entries)++;
755bfeab 1451 }else if(ie->pos == pos && distance < ie->min_distance) //do not reduce the distance
3ba1438d 1452 distance= ie->min_distance;
3e9245a9 1453 }
3ba1438d 1454
b754978a
MN
1455 ie->pos = pos;
1456 ie->timestamp = timestamp;
3e9245a9 1457 ie->min_distance= distance;
30a43f2d 1458 ie->size= size;
b754978a 1459 ie->flags = flags;
115329f1 1460
3e9245a9 1461 return index;
fb2758c8
FB
1462}
1463
e6fb5a4f
PR
1464int av_add_index_entry(AVStream *st,
1465 int64_t pos, int64_t timestamp, int size, int distance, int flags)
1466{
1467 return ff_add_index_entry(&st->index_entries, &st->nb_index_entries,
1468 &st->index_entries_allocated_size, pos,
1469 timestamp, size, distance, flags);
1470}
1471
1472int ff_index_search_timestamp(const AVIndexEntry *entries, int nb_entries,
1473 int64_t wanted_timestamp, int flags)
fb2758c8
FB
1474{
1475 int a, b, m;
1476 int64_t timestamp;
1477
3ba1438d
MN
1478 a = - 1;
1479 b = nb_entries;
b754978a 1480
67c10de7
MN
1481 //optimize appending index entries at the end
1482 if(b && entries[b-1].timestamp < wanted_timestamp)
1483 a= b-1;
1484
3ba1438d
MN
1485 while (b - a > 1) {
1486 m = (a + b) >> 1;
fb2758c8 1487 timestamp = entries[m].timestamp;
3ba1438d
MN
1488 if(timestamp >= wanted_timestamp)
1489 b = m;
1490 if(timestamp <= wanted_timestamp)
b754978a 1491 a = m;
fb2758c8 1492 }
27a5fe5f 1493 m= (flags & AVSEEK_FLAG_BACKWARD) ? a : b;
115329f1 1494
27a5fe5f
MN
1495 if(!(flags & AVSEEK_FLAG_ANY)){
1496 while(m>=0 && m<nb_entries && !(entries[m].flags & AVINDEX_KEYFRAME)){
1497 m += (flags & AVSEEK_FLAG_BACKWARD) ? -1 : 1;
1498 }
1499 }
3ba1438d 1500
115329f1 1501 if(m == nb_entries)
3ba1438d
MN
1502 return -1;
1503 return m;
fb2758c8
FB
1504}
1505
e6fb5a4f
PR
1506int av_index_search_timestamp(AVStream *st, int64_t wanted_timestamp,
1507 int flags)
1508{
1509 return ff_index_search_timestamp(st->index_entries, st->nb_index_entries,
1510 wanted_timestamp, flags);
1511}
1512
a2faa951 1513#if FF_API_SEEK_PUBLIC
3ba1438d 1514int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags){
a2faa951
AK
1515 return ff_seek_frame_binary(s, stream_index, target_ts, flags);
1516}
1517#endif
1518
1519int ff_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts, int flags)
1520{
8d14a25c 1521 AVInputFormat *avif= s->iformat;
e6586575 1522 int64_t av_uninit(pos_min), av_uninit(pos_max), pos, pos_limit;
8d14a25c 1523 int64_t ts_min, ts_max, ts;
89ddd2a9 1524 int index;
b593f7fd 1525 int64_t ret;
8d14a25c
MN
1526 AVStream *st;
1527
cdd5034f
MN
1528 if (stream_index < 0)
1529 return -1;
115329f1 1530
919d7a34 1531 av_dlog(s, "read_seek: %d %"PRId64"\n", stream_index, target_ts);
8d14a25c
MN
1532
1533 ts_max=
1534 ts_min= AV_NOPTS_VALUE;
90b5b51e 1535 pos_limit= -1; //gcc falsely says it may be uninitialized
8d14a25c
MN
1536
1537 st= s->streams[stream_index];
1538 if(st->index_entries){
1539 AVIndexEntry *e;
1540
a85736f2 1541 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()
3ba1438d 1542 index= FFMAX(index, 0);
8d14a25c
MN
1543 e= &st->index_entries[index];
1544
1545 if(e->timestamp <= target_ts || e->pos == e->min_distance){
1546 pos_min= e->pos;
1547 ts_min= e->timestamp;
919d7a34
DB
1548 av_dlog(s, "using cached pos_min=0x%"PRIx64" dts_min=%"PRId64"\n",
1549 pos_min,ts_min);
8d14a25c
MN
1550 }else{
1551 assert(index==0);
1552 }
115329f1
DB
1553
1554 index= av_index_search_timestamp(st, target_ts, flags & ~AVSEEK_FLAG_BACKWARD);
27a5fe5f
MN
1555 assert(index < st->nb_index_entries);
1556 if(index >= 0){
8d14a25c
MN
1557 e= &st->index_entries[index];
1558 assert(e->timestamp >= target_ts);
1559 pos_max= e->pos;
1560 ts_max= e->timestamp;
1561 pos_limit= pos_max - e->min_distance;
919d7a34
DB
1562 av_dlog(s, "using cached pos_max=0x%"PRIx64" pos_limit=0x%"PRIx64" dts_max=%"PRId64"\n",
1563 pos_max,pos_limit, ts_max);
8d14a25c
MN
1564 }
1565 }
1566
a2faa951 1567 pos= ff_gen_search(s, stream_index, target_ts, pos_min, pos_max, pos_limit, ts_min, ts_max, flags, &ts, avif->read_timestamp);
89ddd2a9
MN
1568 if(pos<0)
1569 return -1;
1570
1571 /* do the seek */
6b4aa5da 1572 if ((ret = avio_seek(s->pb, pos, SEEK_SET)) < 0)
b593f7fd 1573 return ret;
89ddd2a9 1574
a2faa951 1575 ff_update_cur_dts(s, st, ts);
89ddd2a9
MN
1576
1577 return 0;
1578}
1579
a2faa951
AK
1580#if FF_API_SEEK_PUBLIC
1581int64_t av_gen_search(AVFormatContext *s, int stream_index, int64_t target_ts,
1582 int64_t pos_min, int64_t pos_max, int64_t pos_limit,
1583 int64_t ts_min, int64_t ts_max, int flags, int64_t *ts_ret,
1584 int64_t (*read_timestamp)(struct AVFormatContext *, int , int64_t *, int64_t ))
1585{
1586 return ff_gen_search(s, stream_index, target_ts, pos_min, pos_max,
1587 pos_limit, ts_min, ts_max, flags, ts_ret,
1588 read_timestamp);
1589}
1590#endif
1591
1592int64_t ff_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{
89ddd2a9
MN
1597 int64_t pos, ts;
1598 int64_t start_pos, filesize;
1599 int no_change;
1600
919d7a34 1601 av_dlog(s, "gen_seek: %d %"PRId64"\n", stream_index, target_ts);
89ddd2a9 1602
8d14a25c
MN
1603 if(ts_min == AV_NOPTS_VALUE){
1604 pos_min = s->data_offset;
89ddd2a9 1605 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
8d14a25c
MN
1606 if (ts_min == AV_NOPTS_VALUE)
1607 return -1;
1608 }
1609
1610 if(ts_max == AV_NOPTS_VALUE){
1611 int step= 1024;
76aa876e 1612 filesize = avio_size(s->pb);
6fd93ce2 1613 pos_max = filesize - 1;
8d14a25c
MN
1614 do{
1615 pos_max -= step;
89ddd2a9 1616 ts_max = read_timestamp(s, stream_index, &pos_max, pos_max + step);
8d14a25c
MN
1617 step += step;
1618 }while(ts_max == AV_NOPTS_VALUE && pos_max >= step);
1619 if (ts_max == AV_NOPTS_VALUE)
1620 return -1;
115329f1 1621
8d14a25c
MN
1622 for(;;){
1623 int64_t tmp_pos= pos_max + 1;
89ddd2a9 1624 int64_t tmp_ts= read_timestamp(s, stream_index, &tmp_pos, INT64_MAX);
8d14a25c
MN
1625 if(tmp_ts == AV_NOPTS_VALUE)
1626 break;
1627 ts_max= tmp_ts;
1628 pos_max= tmp_pos;
6fd93ce2
KA
1629 if(tmp_pos >= filesize)
1630 break;
8d14a25c
MN
1631 }
1632 pos_limit= pos_max;
1633 }
1634
53f7c43f
MN
1635 if(ts_min > ts_max){
1636 return -1;
1637 }else if(ts_min == ts_max){
1638 pos_limit= pos_min;
1639 }
1640
8d14a25c
MN
1641 no_change=0;
1642 while (pos_min < pos_limit) {
919d7a34
DB
1643 av_dlog(s, "pos_min=0x%"PRIx64" pos_max=0x%"PRIx64" dts_min=%"PRId64" dts_max=%"PRId64"\n",
1644 pos_min, pos_max, ts_min, ts_max);
8d14a25c
MN
1645 assert(pos_limit <= pos_max);
1646
1647 if(no_change==0){
1648 int64_t approximate_keyframe_distance= pos_max - pos_limit;
1649 // interpolate position (better than dichotomy)
3ba1438d
MN
1650 pos = av_rescale(target_ts - ts_min, pos_max - pos_min, ts_max - ts_min)
1651 + pos_min - approximate_keyframe_distance;
8d14a25c
MN
1652 }else if(no_change==1){
1653 // bisection, if interpolation failed to change min or max pos last time
1654 pos = (pos_min + pos_limit)>>1;
1655 }else{
a85736f2
DB
1656 /* linear search if bisection failed, can only happen if there
1657 are very few or no keyframes between min/max */
8d14a25c
MN
1658 pos=pos_min;
1659 }
1660 if(pos <= pos_min)
1661 pos= pos_min + 1;
1662 else if(pos > pos_limit)
1663 pos= pos_limit;
1664 start_pos= pos;
1665
89ddd2a9 1666 ts = read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1
8d14a25c
MN
1667 if(pos == pos_max)
1668 no_change++;
1669 else
1670 no_change=0;
919d7a34
DB
1671 av_dlog(s, "%"PRId64" %"PRId64" %"PRId64" / %"PRId64" %"PRId64" %"PRId64" target:%"PRId64" limit:%"PRId64" start:%"PRId64" noc:%d\n",
1672 pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts,
1673 pos_limit, start_pos, no_change);
db2a0e22
MN
1674 if(ts == AV_NOPTS_VALUE){
1675 av_log(s, AV_LOG_ERROR, "read_timestamp() failed in the middle\n");
1676 return -1;
1677 }
8d14a25c 1678 assert(ts != AV_NOPTS_VALUE);
3ba1438d 1679 if (target_ts <= ts) {
8d14a25c
MN
1680 pos_limit = start_pos - 1;
1681 pos_max = pos;
1682 ts_max = ts;
3ba1438d
MN
1683 }
1684 if (target_ts >= ts) {
8d14a25c
MN
1685 pos_min = pos;
1686 ts_min = ts;
8d14a25c
MN
1687 }
1688 }
115329f1 1689
3ba1438d
MN
1690 pos = (flags & AVSEEK_FLAG_BACKWARD) ? pos_min : pos_max;
1691 ts = (flags & AVSEEK_FLAG_BACKWARD) ? ts_min : ts_max;
8d14a25c 1692 pos_min = pos;
89ddd2a9 1693 ts_min = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
8d14a25c 1694 pos_min++;
89ddd2a9 1695 ts_max = read_timestamp(s, stream_index, &pos_min, INT64_MAX);
919d7a34
DB
1696 av_dlog(s, "pos=0x%"PRIx64" %"PRId64"<=%"PRId64"<=%"PRId64"\n",
1697 pos, ts_min, target_ts, ts_max);
89ddd2a9
MN
1698 *ts_ret= ts;
1699 return pos;
8d14a25c
MN
1700}
1701
d3bb7191 1702static int seek_frame_byte(AVFormatContext *s, int stream_index, int64_t pos, int flags){
3ba1438d
MN
1703 int64_t pos_min, pos_max;
1704#if 0
1705 AVStream *st;
1706
1707 if (stream_index < 0)
1708 return -1;
1709
1710 st= s->streams[stream_index];
1711#endif
1712
1713 pos_min = s->data_offset;
76aa876e 1714 pos_max = avio_size(s->pb) - 1;
3ba1438d
MN
1715
1716 if (pos < pos_min) pos= pos_min;
1717 else if(pos > pos_max) pos= pos_max;
1718
6b4aa5da 1719 avio_seek(s->pb, pos, SEEK_SET);
3ba1438d
MN
1720
1721#if 0
8bcb147f 1722 av_update_cur_dts(s, st, ts);
3ba1438d
MN
1723#endif
1724 return 0;
1725}
1726
d3bb7191 1727static int seek_frame_generic(AVFormatContext *s,
3ba1438d 1728 int stream_index, int64_t timestamp, int flags)
fb2758c8 1729{
6659b32a
SS
1730 int index;
1731 int64_t ret;
fb2758c8
FB
1732 AVStream *st;
1733 AVIndexEntry *ie;
1734
fb2758c8 1735 st = s->streams[stream_index];
e9b78eeb 1736
27a5fe5f 1737 index = av_index_search_timestamp(st, timestamp, flags);
e9b78eeb 1738
8c3b161e
MN
1739 if(index < 0 && st->nb_index_entries && timestamp < st->index_entries[0].timestamp)
1740 return -1;
1741
b5a3541d 1742 if(index < 0 || index==st->nb_index_entries-1){
e9b78eeb
MN
1743 AVPacket pkt;
1744
5e5c9086
MN
1745 if(st->nb_index_entries){
1746 assert(st->index_entries);
e9b78eeb 1747 ie= &st->index_entries[st->nb_index_entries-1];
6b4aa5da 1748 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
aaec4e03 1749 return ret;
a2faa951 1750 ff_update_cur_dts(s, st, ie->timestamp);
aaec4e03 1751 }else{
6b4aa5da 1752 if ((ret = avio_seek(s->pb, s->data_offset, SEEK_SET)) < 0)
aaec4e03
BC
1753 return ret;
1754 }
940173d4 1755 for (;;) {
4439caa4 1756 int read_status;
cda6902d 1757 do{
4439caa4
AC
1758 read_status = av_read_frame(s, &pkt);
1759 } while (read_status == AVERROR(EAGAIN));
1760 if (read_status < 0)
e9b78eeb
MN
1761 break;
1762 av_free_packet(&pkt);
1763 if(stream_index == pkt.stream_index){
cc947f04 1764 if((pkt.flags & AV_PKT_FLAG_KEY) && pkt.dts > timestamp)
e9b78eeb
MN
1765 break;
1766 }
1767 }
1768 index = av_index_search_timestamp(st, timestamp, flags);
1769 }
fb2758c8
FB
1770 if (index < 0)
1771 return -1;
1772
972ffe62 1773 ff_read_frame_flush(s);
e9b78eeb
MN
1774 if (s->iformat->read_seek){
1775 if(s->iformat->read_seek(s, stream_index, timestamp, flags) >= 0)
1776 return 0;
1777 }
1778 ie = &st->index_entries[index];
6b4aa5da 1779 if ((ret = avio_seek(s->pb, ie->pos, SEEK_SET)) < 0)
aaec4e03 1780 return ret;
a2faa951 1781 ff_update_cur_dts(s, st, ie->timestamp);
cdd5034f 1782
fb2758c8
FB
1783 return 0;
1784}
1785
3ba1438d 1786int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp, int flags)
fb2758c8
FB
1787{
1788 int ret;
cdd5034f 1789 AVStream *st;
115329f1 1790
0041cdba 1791 if (flags & AVSEEK_FLAG_BYTE) {
b631fba9
JR
1792 if (s->iformat->flags & AVFMT_NO_BYTE_SEEK)
1793 return -1;
0041cdba 1794 ff_read_frame_flush(s);
d3bb7191 1795 return seek_frame_byte(s, stream_index, timestamp, flags);
0041cdba 1796 }
115329f1 1797
cdd5034f
MN
1798 if(stream_index < 0){
1799 stream_index= av_find_default_stream_index(s);
1800 if(stream_index < 0)
1801 return -1;
115329f1 1802
3ba1438d 1803 st= s->streams[stream_index];
7e6029f9 1804 /* timestamp for default must be expressed in AV_TIME_BASE units */
3ba1438d 1805 timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num);
cdd5034f 1806 }
cdd5034f 1807
fb2758c8 1808 /* first, we try the format specific seek */
0041cdba
JR
1809 if (s->iformat->read_seek) {
1810 ff_read_frame_flush(s);
3ba1438d 1811 ret = s->iformat->read_seek(s, stream_index, timestamp, flags);
0041cdba 1812 } else
fb2758c8
FB
1813 ret = -1;
1814 if (ret >= 0) {
1815 return 0;
1816 }
8d14a25c 1817
0041cdba
JR
1818 if (s->iformat->read_timestamp && !(s->iformat->flags & AVFMT_NOBINSEARCH)) {
1819 ff_read_frame_flush(s);
a2faa951 1820 return ff_seek_frame_binary(s, stream_index, timestamp, flags);
0041cdba
JR
1821 } else if (!(s->iformat->flags & AVFMT_NOGENSEARCH)) {
1822 ff_read_frame_flush(s);
d3bb7191 1823 return seek_frame_generic(s, stream_index, timestamp, flags);
0041cdba 1824 }
69fa2396
VP
1825 else
1826 return -1;
fb2758c8
FB
1827}
1828
32d88592
MN
1829int avformat_seek_file(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
1830{
1831 if(min_ts > ts || max_ts < ts)
1832 return -1;
1833
0041cdba
JR
1834 if (s->iformat->read_seek2) {
1835 ff_read_frame_flush(s);
32d88592 1836 return s->iformat->read_seek2(s, stream_index, min_ts, ts, max_ts, flags);
0041cdba 1837 }
32d88592
MN
1838
1839 if(s->iformat->read_timestamp){
1840 //try to seek via read_timestamp()
1841 }
1842
1843 //Fallback to old API if new is not implemented but old is
1844 //Note the old has somewat different sematics
1845 if(s->iformat->read_seek || 1)
85b4230f 1846 return av_seek_frame(s, stream_index, ts, flags | (ts - min_ts > (uint64_t)(max_ts - ts) ? AVSEEK_FLAG_BACKWARD : 0));
32d88592 1847
d3bb7191 1848 // try some generic seek like seek_frame_generic() but with new ts semantics
32d88592
MN
1849}
1850
fb2758c8 1851/*******************************************************/
12f996ed 1852
e36bdf8b 1853/**
49bd8e4b 1854 * Return TRUE if the stream has accurate duration in any stream.
e36bdf8b 1855 *
c1e8b678 1856 * @return TRUE if the stream has accurate duration for at least one component.
e36bdf8b 1857 */
d3bb7191 1858static int has_duration(AVFormatContext *ic)
12f996ed
FB
1859{
1860 int i;
1861 AVStream *st;
1862
1863 for(i = 0;i < ic->nb_streams; i++) {
1864 st = ic->streams[i];
c1e8b678 1865 if (st->duration != AV_NOPTS_VALUE)
12f996ed
FB
1866 return 1;
1867 }
1868 return 0;
1869}
1870
e36bdf8b
DK
1871/**
1872 * Estimate the stream timings from the one of each components.
1873 *
1874 * Also computes the global bitrate if possible.
1875 */
d3bb7191 1876static void update_stream_timings(AVFormatContext *ic)
12f996ed 1877{
c0df9d75 1878 int64_t start_time, start_time1, end_time, end_time1;
c10731e7 1879 int64_t duration, duration1, filesize;
12f996ed
FB
1880 int i;
1881 AVStream *st;
1882
f27a7268
MR
1883 start_time = INT64_MAX;
1884 end_time = INT64_MIN;
c1e8b678 1885 duration = INT64_MIN;
12f996ed
FB
1886 for(i = 0;i < ic->nb_streams; i++) {
1887 st = ic->streams[i];
7f938dd3 1888 if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
c0df9d75 1889 start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
a7503430 1890 start_time = FFMIN(start_time, start_time1);
12f996ed 1891 if (st->duration != AV_NOPTS_VALUE) {
c0df9d75
MN
1892 end_time1 = start_time1
1893 + av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
a7503430 1894 end_time = FFMAX(end_time, end_time1);
12f996ed
FB
1895 }
1896 }
c1e8b678
NB
1897 if (st->duration != AV_NOPTS_VALUE) {
1898 duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
a7503430 1899 duration = FFMAX(duration, duration1);
c1e8b678 1900 }
12f996ed 1901 }
f27a7268 1902 if (start_time != INT64_MAX) {
12f996ed 1903 ic->start_time = start_time;
a7503430
AK
1904 if (end_time != INT64_MIN)
1905 duration = FFMAX(duration, end_time - start_time);
c1e8b678
NB
1906 }
1907 if (duration != INT64_MIN) {
1908 ic->duration = duration;
c10731e7 1909 if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
a85736f2 1910 /* compute the bitrate */
c10731e7 1911 ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
c1e8b678 1912 (double)ic->duration;
12f996ed
FB
1913 }
1914 }
12f996ed
FB
1915}
1916
1917static void fill_all_stream_timings(AVFormatContext *ic)
1918{
1919 int i;
1920 AVStream *st;
1921
d3bb7191 1922 update_stream_timings(ic);
12f996ed
FB
1923 for(i = 0;i < ic->nb_streams; i++) {
1924 st = ic->streams[i];
1925 if (st->start_time == AV_NOPTS_VALUE) {
c0df9d75
MN
1926 if(ic->start_time != AV_NOPTS_VALUE)
1927 st->start_time = av_rescale_q(ic->start_time, AV_TIME_BASE_Q, st->time_base);
1928 if(ic->duration != AV_NOPTS_VALUE)
1929 st->duration = av_rescale_q(ic->duration, AV_TIME_BASE_Q, st->time_base);
12f996ed
FB
1930 }
1931 }
1932}
1933
d3bb7191 1934static void estimate_timings_from_bit_rate(AVFormatContext *ic)
12f996ed
FB
1935{
1936 int64_t filesize, duration;
1937 int bit_rate, i;
1938 AVStream *st;
1939
1940 /* if bit_rate is already set, we believe it */
9100d4d6 1941 if (ic->bit_rate <= 0) {
12f996ed
FB
1942 bit_rate = 0;
1943 for(i=0;i<ic->nb_streams;i++) {
1944 st = ic->streams[i];
9100d4d6 1945 if (st->codec->bit_rate > 0)
01f4895c 1946 bit_rate += st->codec->bit_rate;
12f996ed
FB
1947 }
1948 ic->bit_rate = bit_rate;
1949 }
1950
1951 /* if duration is already set, we believe it */
115329f1 1952 if (ic->duration == AV_NOPTS_VALUE &&
c10731e7
AK
1953 ic->bit_rate != 0) {
1954 filesize = ic->pb ? avio_size(ic->pb) : 0;
12f996ed 1955 if (filesize > 0) {
12f996ed
FB
1956 for(i = 0; i < ic->nb_streams; i++) {
1957 st = ic->streams[i];
c0df9d75 1958 duration= av_rescale(8*filesize, st->time_base.den, ic->bit_rate*(int64_t)st->time_base.num);
9f32041d 1959 if (st->duration == AV_NOPTS_VALUE)
12f996ed 1960 st->duration = duration;
12f996ed
FB
1961 }
1962 }
1963 }
1964}
1965
12f996ed 1966#define DURATION_MAX_READ_SIZE 250000
411ff322 1967#define DURATION_MAX_RETRY 3
12f996ed
FB
1968
1969/* only usable for MPEG-PS streams */
d3bb7191 1970static void estimate_timings_from_pts(AVFormatContext *ic, int64_t old_offset)
12f996ed
FB
1971{
1972 AVPacket pkt1, *pkt = &pkt1;
1973 AVStream *st;
1974 int read_size, i, ret;
3e4318bf 1975 int64_t end_time;
12f996ed 1976 int64_t filesize, offset, duration;
411ff322 1977 int retry=0;
115329f1 1978
578688fa
LA
1979 ic->cur_st = NULL;
1980
1981 /* flush packet queue */
1982 flush_packet_queue(ic);
1983
e99179de 1984 for (i=0; i<ic->nb_streams; i++) {
578688fa 1985 st = ic->streams[i];
3e4318bf 1986 if (st->start_time == AV_NOPTS_VALUE && st->first_dts == AV_NOPTS_VALUE)
d3bb7191 1987 av_log(st->codec, AV_LOG_WARNING, "start time is not set in estimate_timings_from_pts\n");
cc20679a 1988
578688fa
LA
1989 if (st->parser) {
1990 av_parser_close(st->parser);
1991 st->parser= NULL;
3a41c2f7 1992 av_free_packet(&st->cur_pkt);
578688fa
LA
1993 }
1994 }
115329f1 1995
12f996ed
FB
1996 /* estimate the end time (duration) */
1997 /* XXX: may need to support wrapping */
c10731e7 1998 filesize = ic->pb ? avio_size(ic->pb) : 0;
411ff322
MN
1999 end_time = AV_NOPTS_VALUE;
2000 do{
7e6029f9
AC
2001 offset = filesize - (DURATION_MAX_READ_SIZE<<retry);
2002 if (offset < 0)
2003 offset = 0;
12f996ed 2004
7e6029f9
AC
2005 avio_seek(ic->pb, offset, SEEK_SET);
2006 read_size = 0;
2007 for(;;) {
2008 if (read_size >= DURATION_MAX_READ_SIZE<<(FFMAX(retry-1,0)))
2009 break;
115329f1 2010
7e6029f9
AC
2011 do {
2012 ret = av_read_packet(ic, pkt);
2013 } while(ret == AVERROR(EAGAIN));
2014 if (ret != 0)
2015 break;
2016 read_size += pkt->size;
2017 st = ic->streams[pkt->stream_index];
2018 if (pkt->pts != AV_NOPTS_VALUE &&
2019 (st->start_time != AV_NOPTS_VALUE ||
2020 st->first_dts != AV_NOPTS_VALUE)) {
2021 duration = end_time = pkt->pts;
2022 if (st->start_time != AV_NOPTS_VALUE)
2023 duration -= st->start_time;
2024 else
2025 duration -= st->first_dts;
2026 if (duration < 0)
2027 duration += 1LL<<st->pts_wrap_bits;
2028 if (duration > 0) {
2029 if (st->duration == AV_NOPTS_VALUE || st->duration < duration)
2030 st->duration = duration;
2031 }
12f996ed 2032 }
7e6029f9 2033 av_free_packet(pkt);
12f996ed 2034 }
411ff322
MN
2035 }while( end_time==AV_NOPTS_VALUE
2036 && filesize > (DURATION_MAX_READ_SIZE<<retry)
2037 && ++retry <= DURATION_MAX_RETRY);
115329f1 2038
c0df9d75 2039 fill_all_stream_timings(ic);
12f996ed 2040
6b4aa5da 2041 avio_seek(ic->pb, old_offset, SEEK_SET);
e99179de 2042 for (i=0; i<ic->nb_streams; i++) {
df886e7e
MN
2043 st= ic->streams[i];
2044 st->cur_dts= st->first_dts;
635fbcb1 2045 st->last_IP_pts = AV_NOPTS_VALUE;
a8fd2f4e 2046 st->reference_dts = AV_NOPTS_VALUE;
df886e7e 2047 }
12f996ed
FB
2048}
2049
d3bb7191 2050static void estimate_timings(AVFormatContext *ic, int64_t old_offset)
12f996ed 2051{
12f996ed
FB
2052 int64_t file_size;
2053
2054 /* get the file size, if possible */
2055 if (ic->iformat->flags & AVFMT_NOFILE) {
2056 file_size = 0;
2057 } else {
76aa876e 2058 file_size = avio_size(ic->pb);
a7503430 2059 file_size = FFMAX(0, file_size);
12f996ed 2060 }
12f996ed 2061
ff70e601
MR
2062 if ((!strcmp(ic->iformat->name, "mpeg") ||
2063 !strcmp(ic->iformat->name, "mpegts")) &&
8978feda 2064 file_size && ic->pb->seekable) {
12f996ed 2065 /* get accurate estimate from the PTSes */
d3bb7191
AK
2066 estimate_timings_from_pts(ic, old_offset);
2067 } else if (has_duration(ic)) {
a85736f2 2068 /* at least one component has timings - we use them for all
12f996ed
FB
2069 the components */
2070 fill_all_stream_timings(ic);
2071 } else {
77ac76a3 2072 av_log(ic, AV_LOG_WARNING, "Estimating duration from bitrate, this may be inaccurate\n");
a85736f2 2073 /* less precise: use bitrate info */
d3bb7191 2074 estimate_timings_from_bit_rate(ic);
12f996ed 2075 }
d3bb7191 2076 update_stream_timings(ic);
12f996ed 2077
12f996ed
FB
2078 {
2079 int i;
5e1166b3 2080 AVStream av_unused *st;
12f996ed
FB
2081 for(i = 0;i < ic->nb_streams; i++) {
2082 st = ic->streams[i];
045dd4b9
DB
2083 av_dlog(ic, "%d: start_time: %0.3f duration: %0.3f\n", i,
2084 (double) st->start_time / AV_TIME_BASE,
2085 (double) st->duration / AV_TIME_BASE);
12f996ed 2086 }
045dd4b9
DB
2087 av_dlog(ic, "stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n",
2088 (double) ic->start_time / AV_TIME_BASE,
2089 (double) ic->duration / AV_TIME_BASE,
2090 ic->bit_rate / 1000);
12f996ed 2091 }
12f996ed
FB
2092}
2093
25dfda7f 2094static int has_codec_parameters(AVCodecContext *avctx)
b9a281db
FB
2095{
2096 int val;
25dfda7f 2097 switch (avctx->codec_type) {
72415b2a 2098 case AVMEDIA_TYPE_AUDIO:
25dfda7f
SS
2099 val = avctx->sample_rate && avctx->channels && avctx->sample_fmt != AV_SAMPLE_FMT_NONE;
2100 if (!avctx->frame_size &&
2101 (avctx->codec_id == CODEC_ID_VORBIS ||
2102 avctx->codec_id == CODEC_ID_AAC ||
2103 avctx->codec_id == CODEC_ID_MP1 ||
2104 avctx->codec_id == CODEC_ID_MP2 ||
2105 avctx->codec_id == CODEC_ID_MP3 ||
4ca59d19 2106 avctx->codec_id == CODEC_ID_CELT))
4d35bf74 2107 return 0;
b9a281db 2108 break;
72415b2a 2109 case AVMEDIA_TYPE_VIDEO:
25dfda7f 2110 val = avctx->width && avctx->pix_fmt != PIX_FMT_NONE;
b9a281db
FB
2111 break;
2112 default:
2113 val = 1;
2114 break;
2115 }
25dfda7f 2116 return avctx->codec_id != CODEC_ID_NONE && val != 0;
b9a281db
FB
2117}
2118
ae447836
BC
2119static int has_decode_delay_been_guessed(AVStream *st)
2120{
2121 return st->codec->codec_id != CODEC_ID_H264 ||
38a4be3f 2122 st->info->nb_decoded_frames >= 6;
ae447836
BC
2123}
2124
a67c061e 2125static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **options)
fb2758c8
FB
2126{
2127 int16_t *samples;
2128 AVCodec *codec;
3fdb6af9 2129 int got_picture, data_size, ret=0;
fb2758c8 2130 AVFrame picture;
115329f1 2131
e472ea34
BC
2132 if(!st->codec->codec){
2133 codec = avcodec_find_decoder(st->codec->codec_id);
2134 if (!codec)
2135 return -1;
a67c061e 2136 ret = avcodec_open2(st->codec, codec, options);
e472ea34
BC
2137 if (ret < 0)
2138 return ret;
2139 }
644a9262 2140
a58858d6
AC
2141 if(!has_codec_parameters(st->codec) || !has_decode_delay_been_guessed(st) ||
2142 (!st->codec_info_nb_frames && st->codec->codec->capabilities & CODEC_CAP_CHANNEL_CONF)) {
e472ea34 2143 switch(st->codec->codec_type) {
72415b2a 2144 case AVMEDIA_TYPE_VIDEO:
1bd280f7 2145 avcodec_get_frame_defaults(&picture);
e472ea34
BC
2146 ret = avcodec_decode_video2(st->codec, &picture,
2147 &got_picture, avpkt);
38a4be3f
RB
2148 if (got_picture)
2149 st->info->nb_decoded_frames++;
e472ea34 2150 break;
72415b2a 2151 case AVMEDIA_TYPE_AUDIO:
e472ea34
BC
2152 data_size = FFMAX(avpkt->size, AVCODEC_MAX_AUDIO_FRAME_SIZE);
2153 samples = av_malloc(data_size);
2154 if (!samples)
2155 goto fail;
2156 ret = avcodec_decode_audio3(st->codec, samples,
2157 &data_size, avpkt);
2158 av_free(samples);
2159 break;
2160 default:
2161 break;
2162 }
fb2758c8
FB
2163 }
2164 fail:
fb2758c8
FB
2165 return ret;
2166}
2167
83c27079 2168unsigned int ff_codec_get_tag(const AVCodecTag *tags, enum CodecID id)
45da8124
AJ
2169{
2170 while (tags->id != CODEC_ID_NONE) {
2171 if (tags->id == id)
2172 return tags->tag;
2173 tags++;
2174 }
2175 return 0;
2176}
2177
1a40491e 2178enum CodecID ff_codec_get_id(const AVCodecTag *tags, unsigned int tag)
45da8124 2179{
41415d28
MN
2180 int i;
2181 for(i=0; tags[i].id != CODEC_ID_NONE;i++) {
2182 if(tag == tags[i].tag)
2183 return tags[i].id;
2184 }
2185 for(i=0; tags[i].id != CODEC_ID_NONE; i++) {
0842d589 2186 if (avpriv_toupper4(tag) == avpriv_toupper4(tags[i].tag))
41415d28 2187 return tags[i].id;
45da8124
AJ
2188 }
2189 return CODEC_ID_NONE;
2190}
2191
15545a09 2192unsigned int av_codec_get_tag(const AVCodecTag * const *tags, enum CodecID id)
45da8124
AJ
2193{
2194 int i;
2195 for(i=0; tags && tags[i]; i++){
1a40491e 2196 int tag= ff_codec_get_tag(tags[i], id);
45da8124
AJ
2197 if(tag) return tag;
2198 }
2199 return 0;
2200}
2201
15545a09 2202enum CodecID av_codec_get_id(const AVCodecTag * const *tags, unsigned int tag)
45da8124
AJ
2203{
2204 int i;
2205 for(i=0; tags && tags[i]; i++){
1a40491e 2206 enum CodecID id= ff_codec_get_id(tags[i], tag);
45da8124
AJ
2207 if(id!=CODEC_ID_NONE) return id;
2208 }
2209 return CODEC_ID_NONE;
2210}
2211
c2c3dedf
AJ
2212static void compute_chapters_end(AVFormatContext *s)
2213{
ab11317c 2214 unsigned int i, j;
656566d7 2215 int64_t max_time = s->duration + ((s->start_time == AV_NOPTS_VALUE) ? 0 : s->start_time);
c2c3dedf 2216
ab11317c 2217 for (i = 0; i < s->nb_chapters; i++)
c2c3dedf 2218 if (s->chapters[i]->end == AV_NOPTS_VALUE) {
ab11317c
AK
2219 AVChapter *ch = s->chapters[i];
2220 int64_t end = max_time ? av_rescale_q(max_time, AV_TIME_BASE_Q, ch->time_base)
2221 : INT64_MAX;
2222
2223 for (j = 0; j < s->nb_chapters; j++) {
2224 AVChapter *ch1 = s->chapters[j];
2225 int64_t next_start = av_rescale_q(ch1->start, ch1->time_base, ch->time_base);
2226 if (j != i && next_start > ch->start && next_start < end)
2227 end = next_start;
2228 }
2229 ch->end = (end == INT64_MAX) ? ch->start : end;
c2c3dedf 2230 }
c2c3dedf
AJ
2231}
2232
4d43cbcc
MN
2233static int get_std_framerate(int i){
2234 if(i<60*12) return i*1001;
aecf157e 2235 else return ((const int[]){24,30,60,12,15})[i-60*12]*1000*12;
4d43cbcc
MN
2236}
2237
945208ca
MN
2238/*
2239 * Is the time base unreliable.
2240 * This is a heuristic to balance between quick acceptance of the values in
2241 * the headers vs. some extra checks.
a85736f2
DB
2242 * Old DivX and Xvid often have nonsense timebases like 1fps or 2fps.
2243 * MPEG-2 commonly misuses field repeat flags to store different framerates.
945208ca
MN
2244 * And there are "variable" fps files this needs to detect as well.
2245 */
2246static int tb_unreliable(AVCodecContext *c){
2247 if( c->time_base.den >= 101L*c->time_base.num
2248 || c->time_base.den < 5L*c->time_base.num
2bb6eba2
AJ
2249/* || c->codec_tag == AV_RL32("DIVX")
2250 || c->codec_tag == AV_RL32("XVID")*/
7f123e7f
MN
2251 || c->codec_id == CODEC_ID_MPEG2VIDEO
2252 || c->codec_id == CODEC_ID_H264
2253 )
945208ca
MN
2254 return 1;
2255 return 0;
2256}
2257
a67c061e 2258#if FF_API_FORMAT_PARAMETERS
b9a281db
FB
2259int av_find_stream_info(AVFormatContext *ic)
2260{
a67c061e
AK
2261 return avformat_find_stream_info(ic, NULL);
2262}
2263#endif
2264
2265int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2266{
9f75260e 2267 int i, count, ret, read_size, j;
b9a281db 2268 AVStream *st;
fb2758c8 2269 AVPacket pkt1, *pkt;
a2704c97 2270 int64_t old_offset = avio_tell(ic->pb);
a67c061e 2271 int orig_nb_streams = ic->nb_streams; // new streams might appear, no options for those
0cbff027 2272
c0df9d75 2273 for(i=0;i<ic->nb_streams;i++) {
62784e37 2274 AVCodec *codec;
c0df9d75 2275 st = ic->streams[i];
dafaef2f 2276
b3c0fc76
AJ
2277 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO ||
2278 st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE) {
c0df9d75
MN
2279/* if(!st->time_base.num)
2280 st->time_base= */
01f4895c
MN
2281 if(!st->codec->time_base.num)
2282 st->codec->time_base= st->time_base;
c0df9d75 2283 }
90ad92b3 2284 //only for the split stuff
fe8344a2 2285 if (!st->parser && !(ic->flags & AVFMT_FLAG_NOPARSE)) {
01f4895c 2286 st->parser = av_parser_init(st->codec->codec_id);
57004ff1 2287 if(st->need_parsing == AVSTREAM_PARSE_HEADERS && st->parser){
7cbaa7ba
MN
2288 st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
2289 }
90ad92b3 2290 }
43e4d57f 2291 assert(!st->codec->codec);
62784e37
BL
2292 codec = avcodec_find_decoder(st->codec->codec_id);
2293
cb2c971d
AJ
2294 /* Ensure that subtitle_header is properly set. */
2295 if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
2296 && codec && !st->codec->codec)
a67c061e 2297 avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
cb2c971d 2298
43e4d57f
MN
2299 //try to just open decoders, in case this is enough to get parameters
2300 if(!has_codec_parameters(st->codec)){
cb2c971d 2301 if (codec && !st->codec->codec)
a67c061e 2302 avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
43e4d57f 2303 }
c0df9d75
MN
2304 }
2305
feb2440c 2306 for (i=0; i<ic->nb_streams; i++) {
fd0368e7 2307 ic->streams[i]->info->last_dts = AV_NOPTS_VALUE;
15bc38e5 2308 }
115329f1 2309
b9a281db
FB
2310 count = 0;
2311 read_size = 0;
b9a281db 2312 for(;;) {
9957cdbf 2313 if (ff_check_interrupt(&ic->interrupt_callback)){
c76374c6 2314 ret= AVERROR_EXIT;
71ee6515 2315 av_log(ic, AV_LOG_DEBUG, "interrupted\n");
1d14361d
MN
2316 break;
2317 }
2318
b9a281db
FB
2319 /* check if one codec still needs to be handled */
2320 for(i=0;i<ic->nb_streams;i++) {
7c152a45
AH
2321 int fps_analyze_framecount = 20;
2322
b9a281db 2323 st = ic->streams[i];
01f4895c 2324 if (!has_codec_parameters(st->codec))
b9a281db 2325 break;
7c152a45
AH
2326 /* if the timebase is coarse (like the usual millisecond precision
2327 of mkv), we need to analyze more frames to reliably arrive at
2328 the correct fps */
2329 if (av_q2d(st->time_base) > 0.0005)
2330 fps_analyze_framecount *= 2;
30315a8d
AC
2331 if (ic->fps_probe_size >= 0)
2332 fps_analyze_framecount = ic->fps_probe_size;
3e76d1b5 2333 /* variable fps and no guess at the real fps */
0e5f33f2 2334 if( tb_unreliable(st->codec) && !(st->r_frame_rate.num && st->avg_frame_rate.num)
7c152a45
AH
2335 && st->info->duration_count < fps_analyze_framecount
2336 && st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
3e76d1b5 2337 break;
01f4895c 2338 if(st->parser && st->parser->parser->split && !st->codec->extradata)
90ad92b3 2339 break;
82583548
MN
2340 if(st->first_dts == AV_NOPTS_VALUE)
2341 break;
b9a281db
FB
2342 }
2343 if (i == ic->nb_streams) {
2344 /* NOTE: if the format has no header, then we need to read
2345 some packets to get most of the streams, so we cannot
2346 stop here */
fb2758c8 2347 if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) {
b9a281db
FB
2348 /* if we found the info for all the codecs, we can stop */
2349 ret = count;
71ee6515 2350 av_log(ic, AV_LOG_DEBUG, "All info found\n");
b9a281db
FB
2351 break;
2352 }
5fb83c38 2353 }
35eab0c0 2354 /* we did not get all the codec info, but we read too much data */
57011a13 2355 if (read_size >= ic->probesize) {
35eab0c0 2356 ret = count;
9bbe9a0d 2357 av_log(ic, AV_LOG_DEBUG, "Probe buffer size limit %d reached\n", ic->probesize);
35eab0c0
MN
2358 break;
2359 }
b9a281db 2360
fb2758c8
FB
2361 /* NOTE: a new stream can be added there if no header in file
2362 (AVFMTCTX_NOHEADER) */
d3bb7191 2363 ret = read_frame_internal(ic, &pkt1);
59ca3955
AF
2364 if (ret == AVERROR(EAGAIN))
2365 continue;
2366
2367 if (ret < 0) {
fb2758c8
FB
2368 /* EOF or error */
2369 ret = -1; /* we could not have all the codec parameters before EOF */
e19456e3
MN
2370 for(i=0;i<ic->nb_streams;i++) {
2371 st = ic->streams[i];
305ee50f
MN
2372 if (!has_codec_parameters(st->codec)){
2373 char buf[256];
2374 avcodec_string(buf, sizeof(buf), st->codec, 0);
657eca1f 2375 av_log(ic, AV_LOG_WARNING, "Could not find codec parameters (%s)\n", buf);
344a18c3
MR
2376 } else {
2377 ret = 0;
305ee50f 2378 }
e19456e3 2379 }
fb2758c8
FB
2380 break;
2381 }
2382
5c5b1731 2383 pkt= add_to_pktbuf(&ic->packet_buffer, &pkt1, &ic->packet_buffer_end);
fd0368e7
AJ
2384 if ((ret = av_dup_packet(pkt)) < 0)
2385 goto find_stream_info_err;
b9a281db 2386
fb2758c8 2387 read_size += pkt->size;
b9a281db
FB
2388
2389 st = ic->streams[pkt->stream_index];
fd0368e7
AJ
2390 if (st->codec_info_nb_frames>1) {
2391 if (st->time_base.den > 0 && av_rescale_q(st->info->codec_info_duration, st->time_base, AV_TIME_BASE_Q) >= ic->max_analyze_duration) {
657eca1f 2392 av_log(ic, AV_LOG_WARNING, "max_analyze_duration reached\n");
3a560188 2393 break;
71ee6515 2394 }
fd0368e7 2395 st->info->codec_info_duration += pkt->duration;
3a560188 2396 }
cefe0607 2397 {
fd0368e7 2398 int64_t last = st->info->last_dts;
3c150d16 2399
a31e9f68
MR
2400 if(pkt->dts != AV_NOPTS_VALUE && last != AV_NOPTS_VALUE && pkt->dts > last){
2401 int64_t duration= pkt->dts - last;
4d43cbcc
MN
2402 double dur= duration * av_q2d(st->time_base);
2403
72415b2a 2404// if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
4d43cbcc 2405// av_log(NULL, AV_LOG_ERROR, "%f\n", dur);
fd0368e7
AJ
2406 if (st->info->duration_count < 2)
2407 memset(st->info->duration_error, 0, sizeof(st->info->duration_error));
2408 for (i=1; i<FF_ARRAY_ELEMS(st->info->duration_error); i++) {
69c262d1
MN
2409 int framerate= get_std_framerate(i);
2410 int ticks= lrintf(dur*framerate/(1001*12));
52767d89 2411 double error = dur - (double)ticks*1001*12 / framerate;
fd0368e7 2412 st->info->duration_error[i] += error*error;
69c262d1 2413 }
fd0368e7 2414 st->info->duration_count++;
85142724 2415 // ignore the first 4 values, they might have some random jitter
fd0368e7
AJ
2416 if (st->info->duration_count > 3)
2417 st->info->duration_gcd = av_gcd(st->info->duration_gcd, duration);
15bc38e5 2418 }
fd0368e7
AJ
2419 if (last == AV_NOPTS_VALUE || st->info->duration_count <= 1)
2420 st->info->last_dts = pkt->dts;
15bc38e5 2421 }
01f4895c
MN
2422 if(st->parser && st->parser->parser->split && !st->codec->extradata){
2423 int i= st->parser->parser->split(st->codec, pkt->data, pkt->size);
90ad92b3 2424 if(i){
01f4895c 2425 st->codec->extradata_size= i;
62c52121 2426 st->codec->extradata= av_malloc(st->codec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
01f4895c 2427 memcpy(st->codec->extradata, pkt->data, st->codec->extradata_size);
62c52121 2428 memset(st->codec->extradata + i, 0, FF_INPUT_BUFFER_PADDING_SIZE);
90ad92b3
MN
2429 }
2430 }
115329f1 2431
fb2758c8
FB
2432 /* if still no information, we try to open the codec and to
2433 decompress the frame. We try to avoid that in most cases as
a85736f2 2434 it takes longer and uses more memory. For MPEG-4, we need to
dafaef2f
BL
2435 decompress for QuickTime.
2436
2437 If CODEC_CAP_CHANNEL_CONF is set this will force decoding of at
2438 least one frame of codec data, this makes sure the codec initializes
2439 the channel configuration and does not only trust the values from the container.
2440 */
e358f7ee 2441 try_decode_frame(st, pkt, (options && i < orig_nb_streams )? &options[i] : NULL);
115329f1 2442
9d3fdf20 2443 st->codec_info_nb_frames++;
b9a281db
FB
2444 count++;
2445 }
2446
bd107136 2447 // close codecs which were opened in try_decode_frame()
43c0040a
MN
2448 for(i=0;i<ic->nb_streams;i++) {
2449 st = ic->streams[i];
01f4895c
MN
2450 if(st->codec->codec)
2451 avcodec_close(st->codec);
43c0040a 2452 }
b9a281db
FB
2453 for(i=0;i<ic->nb_streams;i++) {
2454 st = ic->streams[i];
fd0368e7 2455 if (st->codec_info_nb_frames>2 && !st->avg_frame_rate.num && st->info->codec_info_duration)
02b398ef 2456 av_reduce(&st->avg_frame_rate.num, &st->avg_frame_rate.den,
6c6e6ef5 2457 (st->codec_info_nb_frames-2)*(int64_t)st->time_base.den,
fd0368e7 2458 st->info->codec_info_duration*(int64_t)st->time_base.num, 60000);
72415b2a 2459 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
85142724
RD
2460 // the check for tb_unreliable() is not completely correct, since this is not about handling
2461 // a unreliable/inexact time base, but a time base that is finer than necessary, as e.g.
2462 // ipmovie.c produces.
fd0368e7
AJ
2463 if (tb_unreliable(st->codec) && st->info->duration_count > 15 && st->info->duration_gcd > 1 && !st->r_frame_rate.num)
2464 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);
2465 if (st->info->duration_count && !st->r_frame_rate.num
945208ca 2466 && tb_unreliable(st->codec) /*&&
a85736f2 2467 //FIXME we should not special-case MPEG-2, but this needs testing with non-MPEG-2 ...
fd0368e7 2468 st->time_base.num*duration_sum[i]/st->info->duration_count*101LL > st->time_base.den*/){
fe02d9e7 2469 int num = 0;
4d43cbcc 2470 double best_error= 2*av_q2d(st->time_base);
fd0368e7 2471 best_error = best_error*best_error*st->info->duration_count*1000*12*30;
4d43cbcc 2472
fd0368e7
AJ
2473 for (j=1; j<FF_ARRAY_ELEMS(st->info->duration_error); j++) {
2474 double error = st->info->duration_error[j] * get_std_framerate(j);
72415b2a 2475// if(st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
4d43cbcc 2476// av_log(NULL, AV_LOG_ERROR, "%f %f\n", get_std_framerate(j) / 12.0/1001, error);
9f75260e
MN
2477 if(error < best_error){
2478 best_error= error;
fe02d9e7 2479 num = get_std_framerate(j);
9f75260e 2480 }
3c150d16 2481 }
fe02d9e7
RD
2482 // do not increase frame rate by more than 1 % in order to match a standard rate.
2483 if (num && (!st->r_frame_rate.num || (double)num/(12*1001) < 1.01 * av_q2d(st->r_frame_rate)))
2484 av_reduce(&st->r_frame_rate.num, &st->r_frame_rate.den, num, 12*1001, INT_MAX);
15bc38e5
MN
2485 }
2486
c0df9d75 2487 if (!st->r_frame_rate.num){
5523d5f4 2488 if( st->codec->time_base.den * (int64_t)st->time_base.num
3797c74b 2489 <= st->codec->time_base.num * st->codec->ticks_per_frame * (int64_t)st->time_base.den){
5523d5f4 2490 st->r_frame_rate.num = st->codec->time_base.den;
3797c74b 2491 st->r_frame_rate.den = st->codec->time_base.num * st->codec->ticks_per_frame;
5523d5f4
MN
2492 }else{
2493 st->r_frame_rate.num = st->time_base.den;
2494 st->r_frame_rate.den = st->time_base.num;
2495 }
14bea432 2496 }
72415b2a 2497 }else if(st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
dd1c8f3e
LA
2498 if(!st->codec->bits_per_coded_sample)
2499 st->codec->bits_per_coded_sample= av_get_bits_per_sample(st->codec->codec_id);
c70a6a41
JR
2500 // set stream disposition based on audio service type
2501 switch (st->codec->audio_service_type) {
2502 case AV_AUDIO_SERVICE_TYPE_EFFECTS:
2503 st->disposition = AV_DISPOSITION_CLEAN_EFFECTS; break;
2504 case AV_AUDIO_SERVICE_TYPE_VISUALLY_IMPAIRED:
2505 st->disposition = AV_DISPOSITION_VISUAL_IMPAIRED; break;
2506 case AV_AUDIO_SERVICE_TYPE_HEARING_IMPAIRED:
2507 st->disposition = AV_DISPOSITION_HEARING_IMPAIRED; break;
2508 case AV_AUDIO_SERVICE_TYPE_COMMENTARY:
2509 st->disposition = AV_DISPOSITION_COMMENT; break;
2510 case AV_AUDIO_SERVICE_TYPE_KARAOKE:
2511 st->disposition = AV_DISPOSITION_KARAOKE; break;
2512 }
b9a281db 2513 }
de6d9b64 2514 }
b9a281db 2515
d3bb7191 2516 estimate_timings(ic, old_offset);
6fea687e 2517
c2c3dedf
AJ
2518 compute_chapters_end(ic);
2519
e928649b 2520#if 0
a85736f2 2521 /* correct DTS for B-frame streams with no timestamps */
e928649b
MN
2522 for(i=0;i<ic->nb_streams;i++) {
2523 st = ic->streams[i];
72415b2a 2524 if (st->codec->codec_type == AVMEDIA_TYPE_VIDEO) {
e928649b
MN
2525 if(b-frames){
2526 ppktl = &ic->packet_buffer;
2527 while(ppkt1){
2528 if(ppkt1->stream_index != i)
2529 continue;
2530 if(ppkt1->pkt->dts < 0)
2531 break;
2532 if(ppkt1->pkt->pts != AV_NOPTS_VALUE)
2533 break;
2534 ppkt1->pkt->dts -= delta;
2535 ppkt1= ppkt1->next;
2536 }
2537 if(ppkt1)
2538 continue;
2539 st->cur_dts -= delta;
2540 }
2541 }
2542 }
2543#endif
0cbff027 2544
fd0368e7
AJ
2545 find_stream_info_err:
2546 for (i=0; i < ic->nb_streams; i++)
2547 av_freep(&ic->streams[i]->info);
b9a281db 2548 return ret;
de6d9b64
FB
2549}
2550
9128ae08
NG
2551static AVProgram *find_program_from_stream(AVFormatContext *ic, int s)
2552{
2553 int i, j;
2554
2555 for (i = 0; i < ic->nb_programs; i++)
2556 for (j = 0; j < ic->programs[i]->nb_stream_indexes; j++)
2557 if (ic->programs[i]->stream_index[j] == s)
2558 return ic->programs[i];
2559 return NULL;
2560}
2561
2562int av_find_best_stream(AVFormatContext *ic,
2563 enum AVMediaType type,
2564 int wanted_stream_nb,
2565 int related_stream,
2566 AVCodec **decoder_ret,
2567 int flags)
2568{
2c715816 2569 int i, nb_streams = ic->nb_streams;
9128ae08
NG
2570 int ret = AVERROR_STREAM_NOT_FOUND, best_count = -1;
2571 unsigned *program = NULL;
2572 AVCodec *decoder = NULL, *best_decoder = NULL;
2573
2574 if (related_stream >= 0 && wanted_stream_nb < 0) {
2575 AVProgram *p = find_program_from_stream(ic, related_stream);
2576 if (p) {
2577 program = p->stream_index;
2578 nb_streams = p->nb_stream_indexes;
2579 }
2580 }
2581 for (i = 0; i < nb_streams; i++) {
2c715816
MB
2582 int real_stream_index = program ? program[i] : i;
2583 AVStream *st = ic->streams[real_stream_index];
9128ae08
NG
2584 AVCodecContext *avctx = st->codec;
2585 if (avctx->codec_type != type)
2586 continue;
2c715816 2587 if (wanted_stream_nb >= 0 && real_stream_index != wanted_stream_nb)
9128ae08 2588 continue;
52091491
PR
2589 if (st->disposition & (AV_DISPOSITION_HEARING_IMPAIRED|AV_DISPOSITION_VISUAL_IMPAIRED))
2590 continue;
9128ae08 2591 if (decoder_ret) {
6b35a795 2592 decoder = avcodec_find_decoder(st->codec->codec_id);
9128ae08
NG
2593 if (!decoder) {
2594 if (ret < 0)
2595 ret = AVERROR_DECODER_NOT_FOUND;
2596 continue;
2597 }
2598 }
2599 if (best_count >= st->codec_info_nb_frames)
2600 continue;
2601 best_count = st->codec_info_nb_frames;
2c715816 2602 ret = real_stream_index;
9128ae08
NG
2603 best_decoder = decoder;
2604 if (program && i == nb_streams - 1 && ret < 0) {
2605 program = NULL;
2606 nb_streams = ic->nb_streams;
2607 i = 0; /* no related stream found, try again with everything */
2608 }
2609 }
2610 if (decoder_ret)
2611 *decoder_ret = best_decoder;
2612 return ret;
2613}
2614
fb2758c8
FB
2615/*******************************************************/
2616
fb2758c8
FB
2617int av_read_play(AVFormatContext *s)
2618{
fa13095a
BA
2619 if (s->iformat->read_play)
2620 return s->iformat->read_play(s);
fd2982a0 2621 if (s->pb)
ff1ec0c3 2622 return avio_pause(s->pb, 0);
fa13095a 2623 return AVERROR(ENOSYS);
fb2758c8
FB
2624}
2625
fb2758c8
FB
2626int av_read_pause(AVFormatContext *s)
2627{
fa13095a
BA
2628 if (s->iformat->read_pause)
2629 return s->iformat->read_pause(s);
fd2982a0 2630 if (s->pb)
ff1ec0c3 2631 return avio_pause(s->pb, 1);
fa13095a 2632 return AVERROR(ENOSYS);
fb2758c8
FB
2633}
2634
2506fd54 2635void av_close_input_stream(AVFormatContext *s)
de6d9b64 2636{
81bd4119 2637 flush_packet_queue(s);
b9a281db
FB
2638 if (s->iformat->read_close)
2639 s->iformat->read_close(s);
f124b087
MS
2640 avformat_free_context(s);
2641}
2642
2643void avformat_free_context(AVFormatContext *s)
2644{
2645 int i;
2646 AVStream *st;
2647
36773283 2648 av_opt_free(s);
dbaba52e 2649 if (s->iformat && s->iformat->priv_class && s->priv_data)
36773283
AK
2650 av_opt_free(s->priv_data);
2651
de6d9b64 2652 for(i=0;i<s->nb_streams;i++) {
da24c5e3
FB
2653 /* free all data in a stream component */
2654 st = s->streams[i];
fb2758c8
FB
2655 if (st->parser) {
2656 av_parser_close(st->parser);
3a41c2f7 2657 av_free_packet(&st->cur_pkt);
de6d9b64 2658 }
d2d67e42 2659 av_dict_free(&st->metadata);
fb2758c8 2660 av_free(st->index_entries);
a5e9102b 2661 av_free(st->codec->extradata);
cb2c971d 2662 av_free(st->codec->subtitle_header);
01f4895c 2663 av_free(st->codec);
ade8d8b9 2664 av_free(st->priv_data);
fd0368e7 2665 av_free(st->info);
fb2758c8 2666 av_free(st);
de6d9b64 2667 }
15afa396 2668 for(i=s->nb_programs-1; i>=0; i--) {
d2d67e42 2669 av_dict_free(&s->programs[i]->metadata);
526efa10 2670 av_freep(&s->programs[i]->stream_index);
15afa396
NS
2671 av_freep(&s->programs[i]);
2672 }
ceedda75 2673 av_freep(&s->programs);
a8dbe951 2674 av_freep(&s->priv_data);
7c8202cc 2675 while(s->nb_chapters--) {
d2d67e42 2676 av_dict_free(&s->chapters[s->nb_chapters]->metadata);
7c8202cc 2677 av_free(s->chapters[s->nb_chapters]);
79d7836a
AK
2678 }
2679 av_freep(&s->chapters);
d2d67e42 2680 av_dict_free(&s->metadata);
7bbb67d5 2681 av_freep(&s->streams);
1ea4f593 2682 av_free(s);
de6d9b64
FB
2683}
2684
2506fd54
RD
2685void av_close_input_file(AVFormatContext *s)
2686{
05e84c95
AK
2687 AVIOContext *pb = (s->iformat->flags & AVFMT_NOFILE) || (s->flags & AVFMT_FLAG_CUSTOM_IO) ?
2688 NULL : s->pb;
2506fd54
RD
2689 av_close_input_stream(s);
2690 if (pb)
22a3212e 2691 avio_close(pb);
2506fd54
RD
2692}
2693
569129a6 2694#if FF_API_NEW_STREAM
b9a281db
FB
2695AVStream *av_new_stream(AVFormatContext *s, int id)
2696{
569129a6
AK
2697 AVStream *st = avformat_new_stream(s, NULL);
2698 if (st)
2699 st->id = id;
2700 return st;
2701}
2702#endif
2703
2704AVStream *avformat_new_stream(AVFormatContext *s, AVCodec *c)
2705{
b9a281db 2706 AVStream *st;
504ee036 2707 int i;
38aab35f 2708 AVStream **streams;
b9a281db 2709
38aab35f
AJ
2710 if (s->nb_streams >= INT_MAX/sizeof(*streams))
2711 return NULL;
2712 streams = av_realloc(s->streams, (s->nb_streams + 1) * sizeof(*streams));
2713 if (!streams)
2714 return NULL;
2715 s->streams = streams;
b9a281db
FB
2716
2717 st = av_mallocz(sizeof(AVStream));
2718 if (!st)
2719 return NULL;
fd0368e7
AJ
2720 if (!(st->info = av_mallocz(sizeof(*st->info)))) {
2721 av_free(st);
2722 return NULL;
2723 }
115329f1 2724
569129a6 2725 st->codec = avcodec_alloc_context3(c);
48091512
FB
2726 if (s->iformat) {
2727 /* no default bitrate if decoding */
01f4895c 2728 st->codec->bit_rate = 0;
48091512 2729 }
b9a281db 2730 st->index = s->nb_streams;
12f996ed
FB
2731 st->start_time = AV_NOPTS_VALUE;
2732 st->duration = AV_NOPTS_VALUE;
6d77d9ac
MN
2733 /* we set the current DTS to 0 so that formats without any timestamps
2734 but durations get some timestamps, formats with some unknown
2735 timestamps have their first few packets buffered and the
2736 timestamps corrected before they are returned to the user */
2737 st->cur_dts = 0;
82583548 2738 st->first_dts = AV_NOPTS_VALUE;
86cb7e33 2739 st->probe_packets = MAX_PROBE_PACKETS;
9ee91c2f 2740
a85736f2 2741 /* default pts setting is MPEG-like */
c3f9ebf7 2742 avpriv_set_pts_info(st, 33, 1, 90000);
635fbcb1 2743 st->last_IP_pts = AV_NOPTS_VALUE;
504ee036
MN
2744 for(i=0; i<MAX_REORDER_DELAY+1; i++)
2745 st->pts_buffer[i]= AV_NOPTS_VALUE;
27ca0a79 2746 st->reference_dts = AV_NOPTS_VALUE;
9ee91c2f 2747
c30a4489
AJ
2748 st->sample_aspect_ratio = (AVRational){0,1};
2749
b9a281db
FB
2750 s->streams[s->nb_streams++] = st;
2751 return st;
2752}
2753
15afa396
NS
2754AVProgram *av_new_program(AVFormatContext *ac, int id)
2755{
2756 AVProgram *program=NULL;
2757 int i;
2758
919d7a34 2759 av_dlog(ac, "new_program: id=0x%04x\n", id);
15afa396
NS
2760
2761 for(i=0; i<ac->nb_programs; i++)
2762 if(ac->programs[i]->id == id)
2763 program = ac->programs[i];
2764
2765 if(!program){
2766 program = av_mallocz(sizeof(AVProgram));
2767 if (!program)
2768 return NULL;
2769 dynarray_add(&ac->programs, &ac->nb_programs, program);
2770 program->discard = AVDISCARD_NONE;
2771 }
2772 program->id = id;
2773
2774 return program;
2775}
2776
1fa395e4 2777AVChapter *avpriv_new_chapter(AVFormatContext *s, int id, AVRational time_base, int64_t start, int64_t end, const char *title)
79d7836a 2778{
7a2a3e8e
MN
2779 AVChapter *chapter = NULL;
2780 int i;
2781
7c8202cc 2782 for(i=0; i<s->nb_chapters; i++)
7a2a3e8e
MN
2783 if(s->chapters[i]->id == id)
2784 chapter = s->chapters[i];
2785
2786 if(!chapter){
2787 chapter= av_mallocz(sizeof(AVChapter));
6b43e2c7 2788 if(!chapter)
5c37f43a 2789 return NULL;
7c8202cc 2790 dynarray_add(&s->chapters, &s->nb_chapters, chapter);
7a2a3e8e 2791 }
d2d67e42 2792 av_dict_set(&chapter->metadata, "title", title, 0);
7a2a3e8e 2793 chapter->id = id;
abd2256d 2794 chapter->time_base= time_base;
79d7836a 2795 chapter->start = start;
747fb6c6 2796 chapter->end = end;
79d7836a 2797
5c37f43a 2798 return chapter;
79d7836a 2799}
15afa396 2800
b9a281db
FB
2801/************************************************************/
2802/* output media file */
de6d9b64 2803
25de5958 2804#if FF_API_FORMAT_PARAMETERS
87a0a681
FB
2805int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap)
2806{
2807 int ret;
115329f1 2808
98486a6b
RS
2809 if (s->oformat->priv_data_size > 0) {
2810 s->priv_data = av_mallocz(s->oformat->priv_data_size);
2811 if (!s->priv_data)
769e10f0 2812 return AVERROR(ENOMEM);
cf99e4aa
AH
2813 if (s->oformat->priv_class) {
2814 *(const AVClass**)s->priv_data= s->oformat->priv_class;
2815 av_opt_set_defaults(s->priv_data);
2816 }
98486a6b
RS
2817 } else
2818 s->priv_data = NULL;
115329f1 2819
87a0a681
FB
2820 if (s->oformat->set_parameters) {
2821 ret = s->oformat->set_parameters(s, ap);
2822 if (ret < 0)
2823 return ret;
2824 }
2825 return 0;
2826}
25de5958 2827#endif
87a0a681 2828
698f4cc7
FL
2829static int validate_codec_tag(AVFormatContext *s, AVStream *st)
2830{
2831 const AVCodecTag *avctag;
2832 int n;
2833 enum CodecID id = CODEC_ID_NONE;
2834 unsigned int tag = 0;
2835
2836 /**
2837 * Check that tag + id is in the table
2838 * If neither is in the table -> OK
2839 * If tag is in the table with another id -> FAIL
2840 * If id is in the table with another tag -> FAIL unless strict < normal
2841 */
2842 for (n = 0; s->oformat->codec_tag[n]; n++) {
2843 avctag = s->oformat->codec_tag[n];
2844 while (avctag->id != CODEC_ID_NONE) {
0842d589 2845 if (avpriv_toupper4(avctag->tag) == avpriv_toupper4(st->codec->codec_tag)) {
698f4cc7
FL
2846 id = avctag->id;
2847 if (id == st->codec->codec_id)
2848 return 1;
2849 }
2850 if (avctag->id == st->codec->codec_id)
2851 tag = avctag->tag;
2852 avctag++;
2853 }
2854 }
2855 if (id != CODEC_ID_NONE)
2856 return 0;
2857 if (tag && (st->codec->strict_std_compliance >= FF_COMPLIANCE_NORMAL))
2858 return 0;
2859 return 1;
2860}
2861
25de5958 2862#if FF_API_FORMAT_PARAMETERS
b9a281db
FB
2863int av_write_header(AVFormatContext *s)
2864{
25de5958
AK
2865 return avformat_write_header(s, NULL);
2866}
2867#endif
2868
2869int avformat_write_header(AVFormatContext *s, AVDictionary **options)
2870{
2871 int ret = 0, i;
1e51d801 2872 AVStream *st;
25de5958
AK
2873 AVDictionary *tmp = NULL;
2874
2875 if (options)
2876 av_dict_copy(&tmp, *options, 0);
2877 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
2878 goto fail;
1e51d801 2879
9450118b 2880 // some sanity checks
bb62d5c1 2881 if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
094a63f1 2882 av_log(s, AV_LOG_ERROR, "no streams\n");
25de5958
AK
2883 ret = AVERROR(EINVAL);
2884 goto fail;
094a63f1
TH
2885 }
2886
9450118b
MN
2887 for(i=0;i<s->nb_streams;i++) {
2888 st = s->streams[i];
2889
2890 switch (st->codec->codec_type) {
72415b2a 2891 case AVMEDIA_TYPE_AUDIO:
9450118b
MN
2892 if(st->codec->sample_rate<=0){
2893 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
25de5958
AK
2894 ret = AVERROR(EINVAL);
2895 goto fail;
9450118b 2896 }
bf912a48
BC
2897 if(!st->codec->block_align)
2898 st->codec->block_align = st->codec->channels *
2899 av_get_bits_per_sample(st->codec->codec_id) >> 3;
9450118b 2900 break;
72415b2a 2901 case AVMEDIA_TYPE_VIDEO:
9450118b
MN
2902 if(st->codec->time_base.num<=0 || st->codec->time_base.den<=0){ //FIXME audio too?
2903 av_log(s, AV_LOG_ERROR, "time base not set\n");
25de5958
AK
2904 ret = AVERROR(EINVAL);
2905 goto fail;
9450118b 2906 }
ab5a0175 2907 if((st->codec->width<=0 || st->codec->height<=0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)){
9450118b 2908 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
25de5958
AK
2909 ret = AVERROR(EINVAL);
2910 goto fail;
9450118b 2911 }
0354ddb7
MN
2912 if(av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)){
2913 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between encoder and muxer layer\n");
25de5958
AK
2914 ret = AVERROR(EINVAL);
2915 goto fail;
0354ddb7 2916 }
9450118b
MN
2917 break;
2918 }
5ecfa9f5
MN
2919
2920 if(s->oformat->codec_tag){
7686ab07
MN
2921 if(st->codec->codec_tag && st->codec->codec_id == CODEC_ID_RAWVIDEO && av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id) == 0 && !validate_codec_tag(s, st)){
2922 //the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here
2923 st->codec->codec_tag= 0;
2924 }
5ecfa9f5 2925 if(st->codec->codec_tag){
698f4cc7 2926 if (!validate_codec_tag(s, st)) {
b603ab8d
SS
2927 char tagbuf[32];
2928 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);
698f4cc7 2929 av_log(s, AV_LOG_ERROR,
d2064fd4
BC
2930 "Tag %s/0x%08x incompatible with output codec id '%d'\n",
2931 tagbuf, st->codec->codec_tag, st->codec->codec_id);
25de5958
AK
2932 ret = AVERROR_INVALIDDATA;
2933 goto fail;
698f4cc7 2934 }
5ecfa9f5
MN
2935 }else
2936 st->codec->codec_tag= av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);
2937 }
d5cce0a4
AC
2938
2939 if(s->oformat->flags & AVFMT_GLOBALHEADER &&
2940 !(st->codec->flags & CODEC_FLAG_GLOBAL_HEADER))
2941 av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);
9450118b
MN
2942 }
2943
8fae2df5 2944 if (!s->priv_data && s->oformat->priv_data_size > 0) {
c6efa4b5 2945 s->priv_data = av_mallocz(s->oformat->priv_data_size);
25de5958
AK
2946 if (!s->priv_data) {
2947 ret = AVERROR(ENOMEM);
2948 goto fail;
2949 }
2950 if (s->oformat->priv_class) {
2951 *(const AVClass**)s->priv_data= s->oformat->priv_class;
2952 av_opt_set_defaults(s->priv_data);
2953 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
2954 goto fail;
2955 }
8fae2df5 2956 }
c6efa4b5 2957
ed7694d8 2958 /* set muxer identification string */
bb62d5c1 2959 if (s->nb_streams && !(s->streams[0]->codec->flags & CODEC_FLAG_BITEXACT)) {
d2d67e42 2960 av_dict_set(&s->metadata, "encoder", LIBAVFORMAT_IDENT, 0);
ed7694d8
AK
2961 }
2962
31e11451
MN
2963 if(s->oformat->write_header){
2964 ret = s->oformat->write_header(s);
2965 if (ret < 0)
25de5958 2966 goto fail;
31e11451 2967 }
1e51d801
FB
2968
2969 /* init PTS generation */
2970 for(i=0;i<s->nb_streams;i++) {
f0ff20a1 2971 int64_t den = AV_NOPTS_VALUE;
1e51d801
FB
2972 st = s->streams[i];
2973
01f4895c 2974 switch (st->codec->codec_type) {
72415b2a 2975 case AVMEDIA_TYPE_AUDIO:
f0ff20a1 2976 den = (int64_t)st->time_base.num * st->codec->sample_rate;
1e51d801 2977 break;
72415b2a 2978 case AVMEDIA_TYPE_VIDEO:
f0ff20a1 2979 den = (int64_t)st->time_base.num * st->codec->time_base.den;
1e51d801
FB
2980 break;
2981 default:
2982 break;
2983 }
f0ff20a1 2984 if (den != AV_NOPTS_VALUE) {
25de5958
AK
2985 if (den <= 0) {
2986 ret = AVERROR_INVALIDDATA;
2987 goto fail;
2988 }
d3bb7191 2989 frac_init(&st->pts, 0, 0, den);
f0ff20a1 2990 }
1e51d801 2991 }
25de5958
AK
2992
2993 if (options) {
2994 av_dict_free(options);
2995 *options = tmp;
2996 }
1e51d801 2997 return 0;
25de5958
AK
2998fail:
2999 av_dict_free(&tmp);
3000 return ret;
b9a281db
FB
3001}
3002
3c895fc0 3003//FIXME merge with compute_pkt_fields
66765b59 3004static int compute_pkt_fields2(AVFormatContext *s, AVStream *st, AVPacket *pkt){
504ee036
MN
3005 int delay = FFMAX(st->codec->has_b_frames, !!st->codec->max_b_frames);
3006 int num, den, frame_size, i;
b0c7f5a9 3007
4ad0693e 3008 av_dlog(s, "compute_pkt_fields2: pts:%"PRId64" dts:%"PRId64" cur_dts:%"PRId64" b:%d size:%d st:%d\n",
79f43a8c 3009 pkt->pts, pkt->dts, st->cur_dts, delay, pkt->size, pkt->stream_index);
115329f1 3010
e928649b 3011/* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE)
996bbdbf 3012 return AVERROR(EINVAL);*/
115329f1 3013
e928649b 3014 /* duration field */
3c895fc0
MN
3015 if (pkt->duration == 0) {
3016 compute_frame_duration(&num, &den, st, NULL, pkt);
3017 if (den && num) {
3797c74b 3018 pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den * st->codec->ticks_per_frame, den * (int64_t)st->time_base.num);
3c895fc0
MN
3019 }
3020 }
e928649b 3021
6e1aa0f3
MN
3022 if(pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay==0)
3023 pkt->pts= pkt->dts;
3024
e928649b 3025 //XXX/FIXME this is a temporary hack until all encoders output pts
504ee036 3026 if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !delay){
e928649b
MN
3027 pkt->dts=
3028// pkt->pts= st->cur_dts;
3029 pkt->pts= st->pts.val;
3030 }
3031
115329f1 3032 //calculate dts from pts
cb5b96cd 3033 if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY){
504ee036
MN
3034 st->pts_buffer[0]= pkt->pts;
3035 for(i=1; i<delay+1 && st->pts_buffer[i] == AV_NOPTS_VALUE; i++)
663322c1 3036 st->pts_buffer[i]= pkt->pts + (i-delay-1) * pkt->duration;
504ee036 3037 for(i=0; i<delay && st->pts_buffer[i] > st->pts_buffer[i+1]; i++)
1345f4ed 3038 FFSWAP(int64_t, st->pts_buffer[i], st->pts_buffer[i+1]);
504ee036
MN
3039
3040 pkt->dts= st->pts_buffer[0];
e928649b 3041 }
115329f1 3042
5edea431 3043 if(st->cur_dts && st->cur_dts != AV_NOPTS_VALUE && st->cur_dts >= pkt->dts){
66765b59 3044 av_log(s, AV_LOG_ERROR,
440d761e 3045 "Application provided invalid, non monotonically increasing dts to muxer in stream %d: %"PRId64" >= %"PRId64"\n",
66765b59 3046 st->index, st->cur_dts, pkt->dts);
996bbdbf 3047 return AVERROR(EINVAL);
5edea431
MN
3048 }
3049 if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts){
440d761e 3050 av_log(s, AV_LOG_ERROR, "pts < dts in stream %d\n", st->index);
996bbdbf 3051 return AVERROR(EINVAL);
5edea431
MN
3052 }
3053
66765b59 3054// av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%"PRId64" dts2:%"PRId64"\n", pkt->pts, pkt->dts);
e928649b
MN
3055 st->cur_dts= pkt->dts;
3056 st->pts.val= pkt->dts;
3057
1e51d801 3058 /* update pts */
01f4895c 3059 switch (st->codec->codec_type) {
72415b2a 3060 case AVMEDIA_TYPE_AUDIO:
01f4895c 3061 frame_size = get_audio_frame_size(st->codec, pkt->size);
6d8f985e 3062
a85736f2
DB
3063 /* HACK/FIXME, we skip the initial 0 size packets as they are most
3064 likely equal to the encoder delay, but it would be better if we
3065 had the real timestamps from the encoder */
e928649b 3066 if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) {
d3bb7191 3067 frac_add(&st->pts, (int64_t)st->time_base.den * frame_size);
7feb950a 3068 }
1e51d801 3069 break;
72415b2a 3070 case AVMEDIA_TYPE_VIDEO:
d3bb7191 3071 frac_add(&st->pts, (int64_t)st->time_base.den * st->codec->time_base.num);
1e51d801
FB
3072 break;
3073 default:
3074 break;
3075 }
5edea431 3076 return 0;
3c895fc0
MN
3077}
3078
3c895fc0
MN
3079int av_write_frame(AVFormatContext *s, AVPacket *pkt)
3080{
66765b59 3081 int ret = compute_pkt_fields2(s, s->streams[pkt->stream_index], pkt);
576ae256 3082
494bbf58 3083 if(ret<0 && !(s->oformat->flags & AVFMT_NOTIMESTAMPS))
5edea431 3084 return ret;
115329f1 3085
576ae256 3086 ret= s->oformat->write_packet(s, pkt);
1c6d2b7d
AK
3087
3088 if (ret >= 0)
3089 s->streams[pkt->stream_index]->nb_frames++;
576ae256 3090 return ret;
3c895fc0
MN
3091}
3092
ccf0071d
BC
3093void ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
3094 int (*compare)(AVFormatContext *, AVPacket *, AVPacket *))
3095{
3096 AVPacketList **next_point, *this_pktl;
3097
3098 this_pktl = av_mallocz(sizeof(AVPacketList));
3099 this_pktl->pkt= *pkt;
d2e63e8b
RD
3100 pkt->destruct= NULL; // do not free original but only the copy
3101 av_dup_packet(&this_pktl->pkt); // duplicate the packet if it uses non-alloced memory
ccf0071d 3102
e07b882b
MN
3103 if(s->streams[pkt->stream_index]->last_in_packet_buffer){
3104 next_point = &(s->streams[pkt->stream_index]->last_in_packet_buffer->next);
3105 }else
03555376 3106 next_point = &s->packet_buffer;
e07b882b
MN
3107
3108 if(*next_point){
3109 if(compare(s, &s->packet_buffer_end->pkt, pkt)){
3110 while(!compare(s, &(*next_point)->pkt, pkt)){
8a6c7a52
MN
3111 next_point= &(*next_point)->next;
3112 }
e07b882b 3113 goto next_non_null;
8a6c7a52
MN
3114 }else{
3115 next_point = &(s->packet_buffer_end->next);
e07b882b 3116 }
ddce56ef 3117 }
8a6c7a52 3118 assert(!*next_point);
ddce56ef 3119
8a6c7a52 3120 s->packet_buffer_end= this_pktl;
e07b882b 3121next_non_null:
ddce56ef 3122
e07b882b 3123 this_pktl->next= *next_point;
ddce56ef 3124
e07b882b
MN
3125 s->streams[pkt->stream_index]->last_in_packet_buffer=
3126 *next_point= this_pktl;
ccf0071d
BC
3127}
3128
101e1f6f 3129static int ff_interleave_compare_dts(AVFormatContext *s, AVPacket *next, AVPacket *pkt)
ccf0071d
BC
3130{
3131 AVStream *st = s->streams[ pkt ->stream_index];
3132 AVStream *st2= s->streams[ next->stream_index];