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