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