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