Commit | Line | Data |
---|---|---|
de6d9b64 FB |
1 | /* |
2 | * Various utilities for ffmpeg system | |
19720f15 | 3 | * Copyright (c) 2000, 2001, 2002 Fabrice Bellard |
de6d9b64 | 4 | * |
19720f15 FB |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
7 | * License as published by the Free Software Foundation; either | |
8 | * version 2 of the License, or (at your option) any later version. | |
de6d9b64 | 9 | * |
19720f15 | 10 | * This library is distributed in the hope that it will be useful, |
de6d9b64 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
19720f15 FB |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
de6d9b64 | 14 | * |
19720f15 FB |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
de6d9b64 | 18 | */ |
8be1c656 | 19 | #include "avformat.h" |
c5510dd6 | 20 | |
b754978a MN |
21 | #undef NDEBUG |
22 | #include <assert.h> | |
23 | ||
b9a281db FB |
24 | AVInputFormat *first_iformat; |
25 | AVOutputFormat *first_oformat; | |
87a0a681 | 26 | AVImageFormat *first_image_format; |
de6d9b64 | 27 | |
b9a281db | 28 | void av_register_input_format(AVInputFormat *format) |
de6d9b64 | 29 | { |
b9a281db FB |
30 | AVInputFormat **p; |
31 | p = &first_iformat; | |
32 | while (*p != NULL) p = &(*p)->next; | |
33 | *p = format; | |
34 | format->next = NULL; | |
35 | } | |
36 | ||
37 | void av_register_output_format(AVOutputFormat *format) | |
38 | { | |
39 | AVOutputFormat **p; | |
40 | p = &first_oformat; | |
de6d9b64 FB |
41 | while (*p != NULL) p = &(*p)->next; |
42 | *p = format; | |
43 | format->next = NULL; | |
44 | } | |
45 | ||
5b25dfa7 | 46 | int match_ext(const char *filename, const char *extensions) |
de6d9b64 FB |
47 | { |
48 | const char *ext, *p; | |
49 | char ext1[32], *q; | |
50 | ||
293ed23f MN |
51 | if(!filename) |
52 | return 0; | |
53 | ||
de6d9b64 FB |
54 | ext = strrchr(filename, '.'); |
55 | if (ext) { | |
56 | ext++; | |
57 | p = extensions; | |
58 | for(;;) { | |
59 | q = ext1; | |
60 | while (*p != '\0' && *p != ',') | |
61 | *q++ = *p++; | |
62 | *q = '\0'; | |
63 | if (!strcasecmp(ext1, ext)) | |
64 | return 1; | |
65 | if (*p == '\0') | |
66 | break; | |
67 | p++; | |
68 | } | |
69 | } | |
70 | return 0; | |
71 | } | |
72 | ||
b9a281db FB |
73 | AVOutputFormat *guess_format(const char *short_name, const char *filename, |
74 | const char *mime_type) | |
de6d9b64 | 75 | { |
b9a281db | 76 | AVOutputFormat *fmt, *fmt_found; |
de6d9b64 FB |
77 | int score_max, score; |
78 | ||
87a0a681 | 79 | /* specific test for image sequences */ |
94d883e8 FB |
80 | if (!short_name && filename && |
81 | filename_number_test(filename) >= 0 && | |
82 | guess_image_format(filename)) { | |
87a0a681 FB |
83 | return guess_format("image", NULL, NULL); |
84 | } | |
85 | ||
de6d9b64 FB |
86 | /* find the proper file type */ |
87 | fmt_found = NULL; | |
88 | score_max = 0; | |
b9a281db | 89 | fmt = first_oformat; |
de6d9b64 FB |
90 | while (fmt != NULL) { |
91 | score = 0; | |
92 | if (fmt->name && short_name && !strcmp(fmt->name, short_name)) | |
93 | score += 100; | |
94 | if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type)) | |
95 | score += 10; | |
96 | if (filename && fmt->extensions && | |
97 | match_ext(filename, fmt->extensions)) { | |
98 | score += 5; | |
99 | } | |
100 | if (score > score_max) { | |
101 | score_max = score; | |
102 | fmt_found = fmt; | |
103 | } | |
104 | fmt = fmt->next; | |
105 | } | |
106 | return fmt_found; | |
107 | } | |
108 | ||
c5510dd6 PG |
109 | AVOutputFormat *guess_stream_format(const char *short_name, const char *filename, |
110 | const char *mime_type) | |
111 | { | |
112 | AVOutputFormat *fmt = guess_format(short_name, filename, mime_type); | |
113 | ||
114 | if (fmt) { | |
115 | AVOutputFormat *stream_fmt; | |
116 | char stream_format_name[64]; | |
117 | ||
118 | snprintf(stream_format_name, sizeof(stream_format_name), "%s_stream", fmt->name); | |
119 | stream_fmt = guess_format(stream_format_name, NULL, NULL); | |
120 | ||
121 | if (stream_fmt) | |
122 | fmt = stream_fmt; | |
123 | } | |
124 | ||
125 | return fmt; | |
126 | } | |
127 | ||
b9a281db FB |
128 | AVInputFormat *av_find_input_format(const char *short_name) |
129 | { | |
130 | AVInputFormat *fmt; | |
131 | for(fmt = first_iformat; fmt != NULL; fmt = fmt->next) { | |
132 | if (!strcmp(fmt->name, short_name)) | |
133 | return fmt; | |
134 | } | |
135 | return NULL; | |
136 | } | |
137 | ||
de6d9b64 FB |
138 | /* memory handling */ |
139 | ||
b9a281db | 140 | /** |
6fa5a56c FB |
141 | * Default packet destructor |
142 | */ | |
143 | static void av_destruct_packet(AVPacket *pkt) | |
144 | { | |
145 | av_free(pkt->data); | |
146 | pkt->data = NULL; pkt->size = 0; | |
147 | } | |
148 | ||
149 | /** | |
b9a281db FB |
150 | * Allocate the payload of a packet and intialized its fields to default values. |
151 | * | |
152 | * @param pkt packet | |
153 | * @param size wanted payload size | |
154 | * @return 0 if OK. AVERROR_xxx otherwise. | |
155 | */ | |
de6d9b64 FB |
156 | int av_new_packet(AVPacket *pkt, int size) |
157 | { | |
6fa5a56c FB |
158 | void *data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE); |
159 | if (!data) | |
b9a281db | 160 | return AVERROR_NOMEM; |
6fa5a56c | 161 | memset(data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
8cd5be98 | 162 | |
6fa5a56c FB |
163 | av_init_packet(pkt); |
164 | pkt->data = data; | |
165 | pkt->size = size; | |
166 | pkt->destruct = av_destruct_packet; | |
de6d9b64 FB |
167 | return 0; |
168 | } | |
169 | ||
fb2758c8 FB |
170 | /* This is a hack - the packet memory allocation stuff is broken. The |
171 | packet is allocated if it was not really allocated */ | |
172 | int av_dup_packet(AVPacket *pkt) | |
173 | { | |
174 | if (pkt->destruct != av_destruct_packet) { | |
175 | uint8_t *data; | |
8a56ac7b FB |
176 | /* we duplicate the packet and don't forget to put the padding |
177 | again */ | |
178 | data = av_malloc(pkt->size + FF_INPUT_BUFFER_PADDING_SIZE); | |
fb2758c8 FB |
179 | if (!data) { |
180 | return AVERROR_NOMEM; | |
181 | } | |
182 | memcpy(data, pkt->data, pkt->size); | |
8a56ac7b | 183 | memset(data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE); |
fb2758c8 FB |
184 | pkt->data = data; |
185 | pkt->destruct = av_destruct_packet; | |
186 | } | |
187 | return 0; | |
188 | } | |
189 | ||
de6d9b64 FB |
190 | /* fifo handling */ |
191 | ||
192 | int fifo_init(FifoBuffer *f, int size) | |
193 | { | |
1ea4f593 | 194 | f->buffer = av_malloc(size); |
de6d9b64 FB |
195 | if (!f->buffer) |
196 | return -1; | |
197 | f->end = f->buffer + size; | |
198 | f->wptr = f->rptr = f->buffer; | |
199 | return 0; | |
200 | } | |
201 | ||
202 | void fifo_free(FifoBuffer *f) | |
203 | { | |
1ea4f593 | 204 | av_free(f->buffer); |
de6d9b64 FB |
205 | } |
206 | ||
0c1a9eda | 207 | int fifo_size(FifoBuffer *f, uint8_t *rptr) |
de6d9b64 FB |
208 | { |
209 | int size; | |
210 | ||
211 | if (f->wptr >= rptr) { | |
212 | size = f->wptr - rptr; | |
213 | } else { | |
214 | size = (f->end - rptr) + (f->wptr - f->buffer); | |
215 | } | |
216 | return size; | |
217 | } | |
218 | ||
219 | /* get data from the fifo (return -1 if not enough data) */ | |
0c1a9eda | 220 | int fifo_read(FifoBuffer *f, uint8_t *buf, int buf_size, uint8_t **rptr_ptr) |
de6d9b64 | 221 | { |
0c1a9eda | 222 | uint8_t *rptr = *rptr_ptr; |
de6d9b64 FB |
223 | int size, len; |
224 | ||
225 | if (f->wptr >= rptr) { | |
226 | size = f->wptr - rptr; | |
227 | } else { | |
228 | size = (f->end - rptr) + (f->wptr - f->buffer); | |
229 | } | |
230 | ||
231 | if (size < buf_size) | |
232 | return -1; | |
233 | while (buf_size > 0) { | |
234 | len = f->end - rptr; | |
235 | if (len > buf_size) | |
236 | len = buf_size; | |
237 | memcpy(buf, rptr, len); | |
238 | buf += len; | |
239 | rptr += len; | |
240 | if (rptr >= f->end) | |
241 | rptr = f->buffer; | |
242 | buf_size -= len; | |
243 | } | |
244 | *rptr_ptr = rptr; | |
245 | return 0; | |
246 | } | |
247 | ||
0c1a9eda | 248 | void fifo_write(FifoBuffer *f, uint8_t *buf, int size, uint8_t **wptr_ptr) |
de6d9b64 FB |
249 | { |
250 | int len; | |
0c1a9eda | 251 | uint8_t *wptr; |
de6d9b64 FB |
252 | wptr = *wptr_ptr; |
253 | while (size > 0) { | |
254 | len = f->end - wptr; | |
255 | if (len > size) | |
256 | len = size; | |
257 | memcpy(wptr, buf, len); | |
258 | wptr += len; | |
259 | if (wptr >= f->end) | |
260 | wptr = f->buffer; | |
261 | buf += len; | |
262 | size -= len; | |
263 | } | |
264 | *wptr_ptr = wptr; | |
265 | } | |
266 | ||
b9a281db FB |
267 | int filename_number_test(const char *filename) |
268 | { | |
269 | char buf[1024]; | |
293ed23f MN |
270 | if(!filename) |
271 | return -1; | |
b9a281db FB |
272 | return get_frame_filename(buf, sizeof(buf), filename, 1); |
273 | } | |
274 | ||
275 | /* guess file format */ | |
a25e098d | 276 | AVInputFormat *av_probe_input_format(AVProbeData *pd, int is_opened) |
b9a281db FB |
277 | { |
278 | AVInputFormat *fmt1, *fmt; | |
279 | int score, score_max; | |
280 | ||
281 | fmt = NULL; | |
282 | score_max = 0; | |
283 | for(fmt1 = first_iformat; fmt1 != NULL; fmt1 = fmt1->next) { | |
284 | if (!is_opened && !(fmt1->flags & AVFMT_NOFILE)) | |
285 | continue; | |
286 | score = 0; | |
a8dbe951 PG |
287 | if (fmt1->read_probe) { |
288 | score = fmt1->read_probe(pd); | |
289 | } else if (fmt1->extensions) { | |
b9a281db FB |
290 | if (match_ext(pd->filename, fmt1->extensions)) { |
291 | score = 50; | |
292 | } | |
a8dbe951 | 293 | } |
b9a281db FB |
294 | if (score > score_max) { |
295 | score_max = score; | |
296 | fmt = fmt1; | |
297 | } | |
298 | } | |
299 | return fmt; | |
300 | } | |
301 | ||
302 | /************************************************************/ | |
303 | /* input media file */ | |
96baaa6a | 304 | |
da24c5e3 FB |
305 | /** |
306 | * open a media file from an IO stream. 'fmt' must be specified. | |
307 | */ | |
bc874dae | 308 | |
43465395 | 309 | static const char* format_to_name(void* ptr) |
bc874dae | 310 | { |
43465395 | 311 | AVFormatContext* fc = (AVFormatContext*) ptr; |
bc874dae MB |
312 | if(fc->iformat) return fc->iformat->name; |
313 | else if(fc->oformat) return fc->oformat->name; | |
314 | else return "NULL"; | |
315 | } | |
316 | ||
317 | static const AVClass av_format_context_class = { "AVFormatContext", format_to_name }; | |
318 | ||
319 | AVFormatContext *av_alloc_format_context(void) | |
320 | { | |
321 | AVFormatContext *ic; | |
322 | ic = av_mallocz(sizeof(AVFormatContext)); | |
323 | if (!ic) return ic; | |
43465395 | 324 | ic->av_class = &av_format_context_class; |
bc874dae MB |
325 | return ic; |
326 | } | |
327 | ||
da24c5e3 FB |
328 | int av_open_input_stream(AVFormatContext **ic_ptr, |
329 | ByteIOContext *pb, const char *filename, | |
330 | AVInputFormat *fmt, AVFormatParameters *ap) | |
331 | { | |
332 | int err; | |
333 | AVFormatContext *ic; | |
334 | ||
bc874dae | 335 | ic = av_alloc_format_context(); |
da24c5e3 FB |
336 | if (!ic) { |
337 | err = AVERROR_NOMEM; | |
338 | goto fail; | |
339 | } | |
340 | ic->iformat = fmt; | |
341 | if (pb) | |
342 | ic->pb = *pb; | |
343 | ic->duration = AV_NOPTS_VALUE; | |
344 | ic->start_time = AV_NOPTS_VALUE; | |
345 | pstrcpy(ic->filename, sizeof(ic->filename), filename); | |
346 | ||
347 | /* allocate private data */ | |
348 | if (fmt->priv_data_size > 0) { | |
349 | ic->priv_data = av_mallocz(fmt->priv_data_size); | |
350 | if (!ic->priv_data) { | |
351 | err = AVERROR_NOMEM; | |
352 | goto fail; | |
353 | } | |
354 | } else { | |
355 | ic->priv_data = NULL; | |
356 | } | |
357 | ||
da24c5e3 FB |
358 | err = ic->iformat->read_header(ic, ap); |
359 | if (err < 0) | |
360 | goto fail; | |
fb2758c8 FB |
361 | |
362 | if (pb) | |
363 | ic->data_offset = url_ftell(&ic->pb); | |
364 | ||
da24c5e3 FB |
365 | *ic_ptr = ic; |
366 | return 0; | |
367 | fail: | |
368 | if (ic) { | |
369 | av_freep(&ic->priv_data); | |
370 | } | |
371 | av_free(ic); | |
372 | *ic_ptr = NULL; | |
373 | return err; | |
374 | } | |
375 | ||
b9a281db FB |
376 | #define PROBE_BUF_SIZE 2048 |
377 | ||
378 | /** | |
379 | * Open a media file as input. The codec are not opened. Only the file | |
380 | * header (if present) is read. | |
381 | * | |
382 | * @param ic_ptr the opened media file handle is put here | |
383 | * @param filename filename to open. | |
384 | * @param fmt if non NULL, force the file format to use | |
385 | * @param buf_size optional buffer size (zero if default is OK) | |
386 | * @param ap additionnal parameters needed when opening the file (NULL if default) | |
387 | * @return 0 if OK. AVERROR_xxx otherwise. | |
388 | */ | |
389 | int av_open_input_file(AVFormatContext **ic_ptr, const char *filename, | |
390 | AVInputFormat *fmt, | |
391 | int buf_size, | |
392 | AVFormatParameters *ap) | |
de6d9b64 | 393 | { |
da24c5e3 FB |
394 | int err, must_open_file, file_opened; |
395 | uint8_t buf[PROBE_BUF_SIZE]; | |
b9a281db | 396 | AVProbeData probe_data, *pd = &probe_data; |
da24c5e3 FB |
397 | ByteIOContext pb1, *pb = &pb1; |
398 | ||
399 | file_opened = 0; | |
400 | pd->filename = ""; | |
401 | if (filename) | |
402 | pd->filename = filename; | |
b9a281db FB |
403 | pd->buf = buf; |
404 | pd->buf_size = 0; | |
405 | ||
406 | if (!fmt) { | |
407 | /* guess format if no file can be opened */ | |
a25e098d | 408 | fmt = av_probe_input_format(pd, 0); |
de6d9b64 | 409 | } |
de6d9b64 | 410 | |
b6892136 FB |
411 | /* do not open file if the format does not need it. XXX: specific |
412 | hack needed to handle RTSP/TCP */ | |
413 | must_open_file = 1; | |
da24c5e3 | 414 | if (fmt && (fmt->flags & AVFMT_NOFILE)) { |
b6892136 FB |
415 | must_open_file = 0; |
416 | } | |
417 | ||
418 | if (!fmt || must_open_file) { | |
87a0a681 | 419 | /* if no file needed do not try to open one */ |
da24c5e3 | 420 | if (url_fopen(pb, filename, URL_RDONLY) < 0) { |
b9a281db | 421 | err = AVERROR_IO; |
96baaa6a | 422 | goto fail; |
b9a281db | 423 | } |
da24c5e3 | 424 | file_opened = 1; |
96baaa6a | 425 | if (buf_size > 0) { |
da24c5e3 | 426 | url_setbufsize(pb, buf_size); |
96baaa6a | 427 | } |
5b25dfa7 FB |
428 | if (!fmt) { |
429 | /* read probe data */ | |
da24c5e3 FB |
430 | pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); |
431 | url_fseek(pb, 0, SEEK_SET); | |
5b25dfa7 | 432 | } |
96baaa6a FB |
433 | } |
434 | ||
b9a281db FB |
435 | /* guess file format */ |
436 | if (!fmt) { | |
a25e098d | 437 | fmt = av_probe_input_format(pd, 1); |
b9a281db FB |
438 | } |
439 | ||
440 | /* if still no format found, error */ | |
441 | if (!fmt) { | |
442 | err = AVERROR_NOFMT; | |
da24c5e3 | 443 | goto fail; |
de6d9b64 | 444 | } |
b9a281db | 445 | |
67d06418 | 446 | /* XXX: suppress this hack for redirectors */ |
f3ec2d46 | 447 | #ifdef CONFIG_NETWORK |
67d06418 | 448 | if (fmt == &redir_demux) { |
da24c5e3 FB |
449 | err = redir_open(ic_ptr, pb); |
450 | url_fclose(pb); | |
67d06418 FB |
451 | return err; |
452 | } | |
9b2e001f | 453 | #endif |
67d06418 | 454 | |
87a0a681 | 455 | /* check filename in case of an image number is expected */ |
da24c5e3 FB |
456 | if (fmt->flags & AVFMT_NEEDNUMBER) { |
457 | if (filename_number_test(filename) < 0) { | |
87a0a681 | 458 | err = AVERROR_NUMEXPECTED; |
da24c5e3 | 459 | goto fail; |
87a0a681 FB |
460 | } |
461 | } | |
da24c5e3 FB |
462 | err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap); |
463 | if (err) | |
464 | goto fail; | |
b9a281db | 465 | return 0; |
de6d9b64 | 466 | fail: |
da24c5e3 FB |
467 | if (file_opened) |
468 | url_fclose(pb); | |
b9a281db FB |
469 | *ic_ptr = NULL; |
470 | return err; | |
da24c5e3 | 471 | |
de6d9b64 FB |
472 | } |
473 | ||
da24c5e3 FB |
474 | /*******************************************************/ |
475 | ||
b9a281db | 476 | /** |
fb2758c8 FB |
477 | * Read a transport packet from a media file. This function is |
478 | * absolete and should never be used. Use av_read_frame() instead. | |
da24c5e3 | 479 | * |
b9a281db FB |
480 | * @param s media file handle |
481 | * @param pkt is filled | |
da24c5e3 | 482 | * @return 0 if OK. AVERROR_xxx if error. |
b9a281db | 483 | */ |
de6d9b64 FB |
484 | int av_read_packet(AVFormatContext *s, AVPacket *pkt) |
485 | { | |
fb2758c8 FB |
486 | return s->iformat->read_packet(s, pkt); |
487 | } | |
488 | ||
489 | /**********************************************************/ | |
490 | ||
fb2758c8 FB |
491 | /* get the number of samples of an audio frame. Return (-1) if error */ |
492 | static int get_audio_frame_size(AVCodecContext *enc, int size) | |
493 | { | |
494 | int frame_size; | |
495 | ||
496 | if (enc->frame_size <= 1) { | |
497 | /* specific hack for pcm codecs because no frame size is | |
498 | provided */ | |
499 | switch(enc->codec_id) { | |
500 | case CODEC_ID_PCM_S16LE: | |
501 | case CODEC_ID_PCM_S16BE: | |
502 | case CODEC_ID_PCM_U16LE: | |
503 | case CODEC_ID_PCM_U16BE: | |
504 | if (enc->channels == 0) | |
505 | return -1; | |
506 | frame_size = size / (2 * enc->channels); | |
507 | break; | |
508 | case CODEC_ID_PCM_S8: | |
509 | case CODEC_ID_PCM_U8: | |
510 | case CODEC_ID_PCM_MULAW: | |
511 | case CODEC_ID_PCM_ALAW: | |
512 | if (enc->channels == 0) | |
513 | return -1; | |
514 | frame_size = size / (enc->channels); | |
515 | break; | |
516 | default: | |
517 | /* used for example by ADPCM codecs */ | |
518 | if (enc->bit_rate == 0) | |
519 | return -1; | |
520 | frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate; | |
521 | break; | |
522 | } | |
523 | } else { | |
524 | frame_size = enc->frame_size; | |
525 | } | |
526 | return frame_size; | |
527 | } | |
528 | ||
529 | ||
530 | /* return the frame duration in seconds, return 0 if not available */ | |
531 | static void compute_frame_duration(int *pnum, int *pden, | |
532 | AVFormatContext *s, AVStream *st, | |
533 | AVCodecParserContext *pc, AVPacket *pkt) | |
534 | { | |
535 | int frame_size; | |
536 | ||
537 | *pnum = 0; | |
538 | *pden = 0; | |
539 | switch(st->codec.codec_type) { | |
540 | case CODEC_TYPE_VIDEO: | |
541 | *pnum = st->codec.frame_rate_base; | |
542 | *pden = st->codec.frame_rate; | |
543 | if (pc && pc->repeat_pict) { | |
544 | *pden *= 2; | |
545 | *pnum = (*pnum) * (2 + pc->repeat_pict); | |
546 | } | |
547 | break; | |
548 | case CODEC_TYPE_AUDIO: | |
549 | frame_size = get_audio_frame_size(&st->codec, pkt->size); | |
550 | if (frame_size < 0) | |
551 | break; | |
552 | *pnum = frame_size; | |
553 | *pden = st->codec.sample_rate; | |
554 | break; | |
555 | default: | |
556 | break; | |
557 | } | |
558 | } | |
559 | ||
77405fc8 | 560 | static int64_t lsb2full(int64_t lsb, int64_t last_ts, int lsb_bits){ |
4fc2c644 | 561 | int64_t mask = lsb_bits < 64 ? (1LL<<lsb_bits)-1 : -1LL; |
77405fc8 MN |
562 | int64_t delta= last_ts - mask/2; |
563 | return ((lsb - delta)&mask) + delta; | |
564 | } | |
565 | ||
fb2758c8 FB |
566 | static void compute_pkt_fields(AVFormatContext *s, AVStream *st, |
567 | AVCodecParserContext *pc, AVPacket *pkt) | |
568 | { | |
569 | int num, den, presentation_delayed; | |
570 | ||
77405fc8 MN |
571 | /* handle wrapping */ |
572 | if(pkt->pts != AV_NOPTS_VALUE) | |
573 | pkt->pts= lsb2full(pkt->pts, st->cur_dts, st->pts_wrap_bits); | |
574 | if(pkt->dts != AV_NOPTS_VALUE) | |
575 | pkt->dts= lsb2full(pkt->dts, st->cur_dts, st->pts_wrap_bits); | |
576 | ||
fb2758c8 FB |
577 | if (pkt->duration == 0) { |
578 | compute_frame_duration(&num, &den, s, st, pc, pkt); | |
579 | if (den && num) { | |
77405fc8 | 580 | pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num); |
fb2758c8 FB |
581 | } |
582 | } | |
583 | ||
584 | /* do we have a video B frame ? */ | |
585 | presentation_delayed = 0; | |
586 | if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
587 | /* XXX: need has_b_frame, but cannot get it if the codec is | |
588 | not initialized */ | |
589 | if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || | |
590 | st->codec.codec_id == CODEC_ID_MPEG2VIDEO || | |
591 | st->codec.codec_id == CODEC_ID_MPEG4 || | |
592 | st->codec.codec_id == CODEC_ID_H264) && | |
593 | pc && pc->pict_type != FF_B_TYPE) | |
594 | presentation_delayed = 1; | |
77405fc8 MN |
595 | /* this may be redundant, but it shouldnt hurt */ |
596 | if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts) | |
597 | presentation_delayed = 1; | |
fb2758c8 FB |
598 | } |
599 | ||
600 | /* interpolate PTS and DTS if they are not present */ | |
601 | if (presentation_delayed) { | |
602 | /* DTS = decompression time stamp */ | |
603 | /* PTS = presentation time stamp */ | |
604 | if (pkt->dts == AV_NOPTS_VALUE) { | |
77405fc8 MN |
605 | /* if we know the last pts, use it */ |
606 | if(st->last_IP_pts != AV_NOPTS_VALUE) | |
607 | st->cur_dts = pkt->dts = st->last_IP_pts; | |
608 | else | |
609 | pkt->dts = st->cur_dts; | |
fb2758c8 FB |
610 | } else { |
611 | st->cur_dts = pkt->dts; | |
612 | } | |
613 | /* this is tricky: the dts must be incremented by the duration | |
614 | of the frame we are displaying, i.e. the last I or P frame */ | |
615 | if (st->last_IP_duration == 0) | |
616 | st->cur_dts += pkt->duration; | |
617 | else | |
618 | st->cur_dts += st->last_IP_duration; | |
619 | st->last_IP_duration = pkt->duration; | |
77405fc8 | 620 | st->last_IP_pts= pkt->pts; |
fb2758c8 FB |
621 | /* cannot compute PTS if not present (we can compute it only |
622 | by knowing the futur */ | |
623 | } else { | |
624 | /* presentation is not delayed : PTS and DTS are the same */ | |
625 | if (pkt->pts == AV_NOPTS_VALUE) { | |
2092bd75 GB |
626 | if (pkt->dts == AV_NOPTS_VALUE) { |
627 | pkt->pts = st->cur_dts; | |
628 | pkt->dts = st->cur_dts; | |
629 | } | |
630 | else { | |
631 | st->cur_dts = pkt->dts; | |
632 | pkt->pts = pkt->dts; | |
633 | } | |
fb2758c8 FB |
634 | } else { |
635 | st->cur_dts = pkt->pts; | |
636 | pkt->dts = pkt->pts; | |
637 | } | |
638 | st->cur_dts += pkt->duration; | |
639 | } | |
640 | ||
641 | /* update flags */ | |
642 | if (pc) { | |
643 | pkt->flags = 0; | |
644 | /* key frame computation */ | |
645 | switch(st->codec.codec_type) { | |
646 | case CODEC_TYPE_VIDEO: | |
647 | if (pc->pict_type == FF_I_TYPE) | |
648 | pkt->flags |= PKT_FLAG_KEY; | |
649 | break; | |
650 | case CODEC_TYPE_AUDIO: | |
651 | pkt->flags |= PKT_FLAG_KEY; | |
652 | break; | |
653 | default: | |
654 | break; | |
655 | } | |
656 | } | |
657 | ||
77405fc8 MN |
658 | /* convert the packet time stamp units */ |
659 | if(pkt->pts != AV_NOPTS_VALUE) | |
660 | pkt->pts = av_rescale(pkt->pts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den); | |
661 | if(pkt->dts != AV_NOPTS_VALUE) | |
662 | pkt->dts = av_rescale(pkt->dts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den); | |
663 | ||
664 | /* duration field */ | |
665 | pkt->duration = av_rescale(pkt->duration, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den); | |
fb2758c8 FB |
666 | } |
667 | ||
668 | static void av_destruct_packet_nofree(AVPacket *pkt) | |
669 | { | |
670 | pkt->data = NULL; pkt->size = 0; | |
671 | } | |
672 | ||
673 | static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt) | |
674 | { | |
675 | AVStream *st; | |
37353960 | 676 | int len, ret, i; |
fb2758c8 FB |
677 | |
678 | for(;;) { | |
679 | /* select current input stream component */ | |
680 | st = s->cur_st; | |
681 | if (st) { | |
682 | if (!st->parser) { | |
683 | /* no parsing needed: we just output the packet as is */ | |
684 | /* raw data support */ | |
685 | *pkt = s->cur_pkt; | |
686 | compute_pkt_fields(s, st, NULL, pkt); | |
687 | s->cur_st = NULL; | |
688 | return 0; | |
689 | } else if (s->cur_len > 0) { | |
fb2758c8 | 690 | len = av_parser_parse(st->parser, &st->codec, &pkt->data, &pkt->size, |
6ec87caa FB |
691 | s->cur_ptr, s->cur_len, |
692 | s->cur_pkt.pts, s->cur_pkt.dts); | |
693 | s->cur_pkt.pts = AV_NOPTS_VALUE; | |
694 | s->cur_pkt.dts = AV_NOPTS_VALUE; | |
fb2758c8 FB |
695 | /* increment read pointer */ |
696 | s->cur_ptr += len; | |
697 | s->cur_len -= len; | |
698 | ||
699 | /* return packet if any */ | |
700 | if (pkt->size) { | |
37353960 | 701 | got_packet: |
fb2758c8 FB |
702 | pkt->duration = 0; |
703 | pkt->stream_index = st->index; | |
6ec87caa FB |
704 | pkt->pts = st->parser->pts; |
705 | pkt->dts = st->parser->dts; | |
fb2758c8 FB |
706 | pkt->destruct = av_destruct_packet_nofree; |
707 | compute_pkt_fields(s, st, st->parser, pkt); | |
fb2758c8 FB |
708 | return 0; |
709 | } | |
710 | } else { | |
bcbecff1 FB |
711 | /* free packet */ |
712 | av_free_packet(&s->cur_pkt); | |
fb2758c8 FB |
713 | s->cur_st = NULL; |
714 | } | |
715 | } else { | |
fb2758c8 FB |
716 | /* read next packet */ |
717 | ret = av_read_packet(s, &s->cur_pkt); | |
37353960 FB |
718 | if (ret < 0) { |
719 | if (ret == -EAGAIN) | |
720 | return ret; | |
721 | /* return the last frames, if any */ | |
722 | for(i = 0; i < s->nb_streams; i++) { | |
723 | st = s->streams[i]; | |
724 | if (st->parser) { | |
725 | av_parser_parse(st->parser, &st->codec, | |
726 | &pkt->data, &pkt->size, | |
6ec87caa FB |
727 | NULL, 0, |
728 | AV_NOPTS_VALUE, AV_NOPTS_VALUE); | |
37353960 FB |
729 | if (pkt->size) |
730 | goto got_packet; | |
731 | } | |
732 | } | |
733 | /* no more packets: really terminates parsing */ | |
fb2758c8 | 734 | return ret; |
37353960 | 735 | } |
9ee91c2f MN |
736 | |
737 | st = s->streams[s->cur_pkt.stream_index]; | |
fb2758c8 | 738 | |
fb2758c8 FB |
739 | s->cur_st = st; |
740 | s->cur_ptr = s->cur_pkt.data; | |
741 | s->cur_len = s->cur_pkt.size; | |
742 | if (st->need_parsing && !st->parser) { | |
743 | st->parser = av_parser_init(st->codec.codec_id); | |
744 | if (!st->parser) { | |
745 | /* no parser available : just output the raw packets */ | |
746 | st->need_parsing = 0; | |
747 | } | |
748 | } | |
749 | } | |
750 | } | |
751 | } | |
752 | ||
753 | /** | |
754 | * Return the next frame of a stream. The returned packet is valid | |
755 | * until the next av_read_frame() or until av_close_input_file() and | |
756 | * must be freed with av_free_packet. For video, the packet contains | |
757 | * exactly one frame. For audio, it contains an integer number of | |
758 | * frames if each frame has a known fixed size (e.g. PCM or ADPCM | |
759 | * data). If the audio frames have a variable size (e.g. MPEG audio), | |
760 | * then it contains one frame. | |
761 | * | |
762 | * pkt->pts, pkt->dts and pkt->duration are always set to correct | |
763 | * values in AV_TIME_BASE unit (and guessed if the format cannot | |
764 | * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format | |
765 | * has B frames, so it is better to rely on pkt->dts if you do not | |
766 | * decompress the payload. | |
767 | * | |
768 | * Return 0 if OK, < 0 if error or end of file. | |
769 | */ | |
770 | int av_read_frame(AVFormatContext *s, AVPacket *pkt) | |
771 | { | |
de6d9b64 FB |
772 | AVPacketList *pktl; |
773 | ||
774 | pktl = s->packet_buffer; | |
775 | if (pktl) { | |
776 | /* read packet from packet buffer, if there is data */ | |
777 | *pkt = pktl->pkt; | |
778 | s->packet_buffer = pktl->next; | |
1ea4f593 | 779 | av_free(pktl); |
de6d9b64 FB |
780 | return 0; |
781 | } else { | |
fb2758c8 FB |
782 | return av_read_frame_internal(s, pkt); |
783 | } | |
784 | } | |
785 | ||
786 | /* XXX: suppress the packet queue */ | |
787 | static void flush_packet_queue(AVFormatContext *s) | |
788 | { | |
789 | AVPacketList *pktl; | |
790 | ||
791 | for(;;) { | |
792 | pktl = s->packet_buffer; | |
793 | if (!pktl) | |
794 | break; | |
795 | s->packet_buffer = pktl->next; | |
796 | av_free_packet(&pktl->pkt); | |
797 | av_free(pktl); | |
b9a281db FB |
798 | } |
799 | } | |
800 | ||
da24c5e3 | 801 | /*******************************************************/ |
fb2758c8 FB |
802 | /* seek support */ |
803 | ||
b754978a MN |
804 | int av_find_default_stream_index(AVFormatContext *s) |
805 | { | |
806 | int i; | |
807 | AVStream *st; | |
808 | ||
809 | if (s->nb_streams <= 0) | |
810 | return -1; | |
811 | for(i = 0; i < s->nb_streams; i++) { | |
812 | st = s->streams[i]; | |
813 | if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
814 | return i; | |
815 | } | |
816 | } | |
817 | return 0; | |
818 | } | |
819 | ||
fb2758c8 FB |
820 | /* flush the frame reader */ |
821 | static void av_read_frame_flush(AVFormatContext *s) | |
822 | { | |
823 | AVStream *st; | |
824 | int i; | |
825 | ||
826 | flush_packet_queue(s); | |
827 | ||
828 | /* free previous packet */ | |
829 | if (s->cur_st) { | |
830 | if (s->cur_st->parser) | |
831 | av_free_packet(&s->cur_pkt); | |
832 | s->cur_st = NULL; | |
833 | } | |
834 | /* fail safe */ | |
835 | s->cur_ptr = NULL; | |
836 | s->cur_len = 0; | |
837 | ||
838 | /* for each stream, reset read state */ | |
839 | for(i = 0; i < s->nb_streams; i++) { | |
840 | st = s->streams[i]; | |
841 | ||
842 | if (st->parser) { | |
843 | av_parser_close(st->parser); | |
844 | st->parser = NULL; | |
845 | } | |
77405fc8 | 846 | st->last_IP_pts = AV_NOPTS_VALUE; |
fb2758c8 FB |
847 | st->cur_dts = 0; /* we set the current DTS to an unspecified origin */ |
848 | } | |
849 | } | |
850 | ||
cdd5034f MN |
851 | /** |
852 | * add a index entry into a sorted list updateing if it is already there. | |
853 | * @param timestamp timestamp in the timebase of the given stream | |
854 | */ | |
3e9245a9 MN |
855 | int av_add_index_entry(AVStream *st, |
856 | int64_t pos, int64_t timestamp, int distance, int flags) | |
fb2758c8 FB |
857 | { |
858 | AVIndexEntry *entries, *ie; | |
b754978a | 859 | int index; |
fb2758c8 FB |
860 | |
861 | entries = av_fast_realloc(st->index_entries, | |
862 | &st->index_entries_allocated_size, | |
863 | (st->nb_index_entries + 1) * | |
864 | sizeof(AVIndexEntry)); | |
b754978a MN |
865 | st->index_entries= entries; |
866 | ||
867 | if(st->nb_index_entries){ | |
868 | index= av_index_search_timestamp(st, timestamp); | |
869 | ie= &entries[index]; | |
870 | ||
871 | if(ie->timestamp != timestamp){ | |
872 | if(ie->timestamp < timestamp){ | |
873 | index++; //index points to next instead of previous entry, maybe nonexistant | |
874 | ie= &st->index_entries[index]; | |
875 | }else | |
876 | assert(index==0); | |
877 | ||
878 | if(index != st->nb_index_entries){ | |
879 | assert(index < st->nb_index_entries); | |
880 | memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(st->nb_index_entries - index)); | |
881 | } | |
882 | st->nb_index_entries++; | |
c9247fc3 MN |
883 | }else{ |
884 | if(ie->pos == pos && distance < ie->min_distance) //dont reduce the distance | |
885 | distance= ie->min_distance; | |
b754978a | 886 | } |
3e9245a9 MN |
887 | }else{ |
888 | index= st->nb_index_entries++; | |
889 | ie= &entries[index]; | |
890 | } | |
b754978a MN |
891 | |
892 | ie->pos = pos; | |
893 | ie->timestamp = timestamp; | |
3e9245a9 | 894 | ie->min_distance= distance; |
b754978a | 895 | ie->flags = flags; |
3e9245a9 MN |
896 | |
897 | return index; | |
fb2758c8 FB |
898 | } |
899 | ||
900 | /* build an index for raw streams using a parser */ | |
901 | static void av_build_index_raw(AVFormatContext *s) | |
902 | { | |
903 | AVPacket pkt1, *pkt = &pkt1; | |
904 | int ret; | |
905 | AVStream *st; | |
906 | ||
907 | st = s->streams[0]; | |
908 | av_read_frame_flush(s); | |
909 | url_fseek(&s->pb, s->data_offset, SEEK_SET); | |
910 | ||
911 | for(;;) { | |
912 | ret = av_read_frame(s, pkt); | |
913 | if (ret < 0) | |
914 | break; | |
915 | if (pkt->stream_index == 0 && st->parser && | |
916 | (pkt->flags & PKT_FLAG_KEY)) { | |
cdd5034f MN |
917 | int64_t dts= av_rescale(pkt->dts, st->time_base.den, AV_TIME_BASE*(int64_t)st->time_base.num); |
918 | av_add_index_entry(st, st->parser->frame_offset, dts, | |
3e9245a9 | 919 | 0, AVINDEX_KEYFRAME); |
fb2758c8 FB |
920 | } |
921 | av_free_packet(pkt); | |
922 | } | |
923 | } | |
924 | ||
925 | /* return TRUE if we deal with a raw stream (raw codec data and | |
926 | parsing needed) */ | |
927 | static int is_raw_stream(AVFormatContext *s) | |
928 | { | |
929 | AVStream *st; | |
930 | ||
931 | if (s->nb_streams != 1) | |
932 | return 0; | |
933 | st = s->streams[0]; | |
934 | if (!st->need_parsing) | |
935 | return 0; | |
936 | return 1; | |
937 | } | |
938 | ||
939 | /* return the largest index entry whose timestamp is <= | |
940 | wanted_timestamp */ | |
b754978a | 941 | int av_index_search_timestamp(AVStream *st, int wanted_timestamp) |
fb2758c8 | 942 | { |
b754978a MN |
943 | AVIndexEntry *entries= st->index_entries; |
944 | int nb_entries= st->nb_index_entries; | |
fb2758c8 FB |
945 | int a, b, m; |
946 | int64_t timestamp; | |
947 | ||
948 | if (nb_entries <= 0) | |
949 | return -1; | |
950 | ||
951 | a = 0; | |
952 | b = nb_entries - 1; | |
b754978a MN |
953 | |
954 | while (a < b) { | |
955 | m = (a + b + 1) >> 1; | |
fb2758c8 | 956 | timestamp = entries[m].timestamp; |
b754978a | 957 | if (timestamp > wanted_timestamp) { |
fb2758c8 FB |
958 | b = m - 1; |
959 | } else { | |
b754978a | 960 | a = m; |
fb2758c8 FB |
961 | } |
962 | } | |
b754978a | 963 | return a; |
fb2758c8 FB |
964 | } |
965 | ||
8d14a25c MN |
966 | #define DEBUG_SEEK |
967 | ||
cdd5034f MN |
968 | /** |
969 | * Does a binary search using av_index_search_timestamp() and AVCodec.read_timestamp(). | |
970 | * this isnt supposed to be called directly by a user application, but by demuxers | |
971 | * @param target_ts target timestamp in the time base of the given stream | |
972 | * @param stream_index stream number | |
973 | */ | |
8d14a25c MN |
974 | int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts){ |
975 | AVInputFormat *avif= s->iformat; | |
976 | int64_t pos_min, pos_max, pos, pos_limit; | |
977 | int64_t ts_min, ts_max, ts; | |
978 | int64_t start_pos; | |
cdd5034f | 979 | int index, no_change, i; |
8d14a25c MN |
980 | AVStream *st; |
981 | ||
cdd5034f MN |
982 | if (stream_index < 0) |
983 | return -1; | |
984 | ||
8d14a25c MN |
985 | #ifdef DEBUG_SEEK |
986 | av_log(s, AV_LOG_DEBUG, "read_seek: %d %lld\n", stream_index, target_ts); | |
987 | #endif | |
988 | ||
989 | ts_max= | |
990 | ts_min= AV_NOPTS_VALUE; | |
991 | pos_limit= -1; //gcc falsely says it may be uninitalized | |
992 | ||
993 | st= s->streams[stream_index]; | |
994 | if(st->index_entries){ | |
995 | AVIndexEntry *e; | |
996 | ||
997 | index= av_index_search_timestamp(st, target_ts); | |
998 | e= &st->index_entries[index]; | |
999 | ||
1000 | if(e->timestamp <= target_ts || e->pos == e->min_distance){ | |
1001 | pos_min= e->pos; | |
1002 | ts_min= e->timestamp; | |
1003 | #ifdef DEBUG_SEEK | |
1004 | av_log(s, AV_LOG_DEBUG, "unsing cached pos_min=0x%llx dts_min=%lld\n", | |
1005 | pos_min,ts_min); | |
1006 | #endif | |
1007 | }else{ | |
1008 | assert(index==0); | |
1009 | } | |
1010 | index++; | |
1011 | if(index < st->nb_index_entries){ | |
1012 | e= &st->index_entries[index]; | |
1013 | assert(e->timestamp >= target_ts); | |
1014 | pos_max= e->pos; | |
1015 | ts_max= e->timestamp; | |
1016 | pos_limit= pos_max - e->min_distance; | |
1017 | #ifdef DEBUG_SEEK | |
1018 | av_log(s, AV_LOG_DEBUG, "unsing cached pos_max=0x%llx pos_limit=0x%llx dts_max=%lld\n", | |
1019 | pos_max,pos_limit, ts_max); | |
1020 | #endif | |
1021 | } | |
1022 | } | |
1023 | ||
1024 | if(ts_min == AV_NOPTS_VALUE){ | |
1025 | pos_min = s->data_offset; | |
1026 | ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); | |
1027 | if (ts_min == AV_NOPTS_VALUE) | |
1028 | return -1; | |
1029 | } | |
1030 | ||
1031 | if(ts_max == AV_NOPTS_VALUE){ | |
1032 | int step= 1024; | |
1033 | pos_max = url_filesize(url_fileno(&s->pb)) - 1; | |
1034 | do{ | |
1035 | pos_max -= step; | |
1036 | ts_max = avif->read_timestamp(s, stream_index, &pos_max, pos_max + step); | |
1037 | step += step; | |
1038 | }while(ts_max == AV_NOPTS_VALUE && pos_max >= step); | |
1039 | if (ts_max == AV_NOPTS_VALUE) | |
1040 | return -1; | |
1041 | ||
1042 | for(;;){ | |
1043 | int64_t tmp_pos= pos_max + 1; | |
1044 | int64_t tmp_ts= avif->read_timestamp(s, stream_index, &tmp_pos, INT64_MAX); | |
1045 | if(tmp_ts == AV_NOPTS_VALUE) | |
1046 | break; | |
1047 | ts_max= tmp_ts; | |
1048 | pos_max= tmp_pos; | |
1049 | } | |
1050 | pos_limit= pos_max; | |
1051 | } | |
1052 | ||
1053 | no_change=0; | |
1054 | while (pos_min < pos_limit) { | |
1055 | #ifdef DEBUG_SEEK | |
1056 | av_log(s, AV_LOG_DEBUG, "pos_min=0x%llx pos_max=0x%llx dts_min=%lld dts_max=%lld\n", | |
1057 | pos_min, pos_max, | |
1058 | ts_min, ts_max); | |
1059 | #endif | |
1060 | assert(pos_limit <= pos_max); | |
1061 | ||
1062 | if(no_change==0){ | |
1063 | int64_t approximate_keyframe_distance= pos_max - pos_limit; | |
1064 | // interpolate position (better than dichotomy) | |
1065 | pos = (int64_t)((double)(pos_max - pos_min) * | |
1066 | (double)(target_ts - ts_min) / | |
1067 | (double)(ts_max - ts_min)) + pos_min - approximate_keyframe_distance; | |
1068 | }else if(no_change==1){ | |
1069 | // bisection, if interpolation failed to change min or max pos last time | |
1070 | pos = (pos_min + pos_limit)>>1; | |
1071 | }else{ | |
1072 | // linear search if bisection failed, can only happen if there are very few or no keframes between min/max | |
1073 | pos=pos_min; | |
1074 | } | |
1075 | if(pos <= pos_min) | |
1076 | pos= pos_min + 1; | |
1077 | else if(pos > pos_limit) | |
1078 | pos= pos_limit; | |
1079 | start_pos= pos; | |
1080 | ||
1081 | ts = avif->read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1 | |
1082 | if(pos == pos_max) | |
1083 | no_change++; | |
1084 | else | |
1085 | no_change=0; | |
1086 | #ifdef DEBUG_SEEK | |
1087 | av_log(s, AV_LOG_DEBUG, "%Ld %Ld %Ld / %Ld %Ld %Ld target:%Ld limit:%Ld start:%Ld noc:%d\n", pos_min, pos, pos_max, ts_min, ts, ts_max, target_ts, pos_limit, start_pos, no_change); | |
1088 | #endif | |
1089 | assert(ts != AV_NOPTS_VALUE); | |
1090 | if (target_ts < ts) { | |
1091 | pos_limit = start_pos - 1; | |
1092 | pos_max = pos; | |
1093 | ts_max = ts; | |
1094 | } else { | |
1095 | pos_min = pos; | |
1096 | ts_min = ts; | |
1097 | /* check if we are lucky */ | |
1098 | if (target_ts == ts) | |
1099 | break; | |
1100 | } | |
1101 | } | |
1102 | ||
1103 | pos = pos_min; | |
1104 | #ifdef DEBUG_SEEK | |
1105 | pos_min = pos; | |
1106 | ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); | |
1107 | pos_min++; | |
1108 | ts_max = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); | |
1109 | av_log(s, AV_LOG_DEBUG, "pos=0x%llx %lld<=%lld<=%lld\n", | |
1110 | pos, ts_min, target_ts, ts_max); | |
1111 | #endif | |
1112 | /* do the seek */ | |
1113 | url_fseek(&s->pb, pos, SEEK_SET); | |
cdd5034f MN |
1114 | |
1115 | ts= av_rescale(ts_min, AV_TIME_BASE*(int64_t)st->time_base.num, st->time_base.den); | |
1116 | for(i = 0; i < s->nb_streams; i++) { | |
1117 | st = s->streams[i]; | |
1118 | ||
77405fc8 | 1119 | st->cur_dts = av_rescale(ts, st->time_base.den, AV_TIME_BASE*(int64_t)st->time_base.num); |
cdd5034f | 1120 | } |
8d14a25c MN |
1121 | |
1122 | return 0; | |
1123 | } | |
1124 | ||
fb2758c8 FB |
1125 | static int av_seek_frame_generic(AVFormatContext *s, |
1126 | int stream_index, int64_t timestamp) | |
1127 | { | |
cdd5034f | 1128 | int index, i; |
fb2758c8 FB |
1129 | AVStream *st; |
1130 | AVIndexEntry *ie; | |
1131 | ||
1132 | if (!s->index_built) { | |
1133 | if (is_raw_stream(s)) { | |
1134 | av_build_index_raw(s); | |
1135 | } else { | |
1136 | return -1; | |
1137 | } | |
1138 | s->index_built = 1; | |
1139 | } | |
1140 | ||
fb2758c8 | 1141 | st = s->streams[stream_index]; |
b754978a | 1142 | index = av_index_search_timestamp(st, timestamp); |
fb2758c8 FB |
1143 | if (index < 0) |
1144 | return -1; | |
1145 | ||
1146 | /* now we have found the index, we can seek */ | |
1147 | ie = &st->index_entries[index]; | |
1148 | av_read_frame_flush(s); | |
1149 | url_fseek(&s->pb, ie->pos, SEEK_SET); | |
cdd5034f MN |
1150 | |
1151 | timestamp= av_rescale(ie->timestamp, AV_TIME_BASE*(int64_t)st->time_base.num, st->time_base.den); | |
1152 | for(i = 0; i < s->nb_streams; i++) { | |
1153 | st = s->streams[i]; | |
1154 | ||
77405fc8 | 1155 | st->cur_dts = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE*(int64_t)st->time_base.num); |
cdd5034f MN |
1156 | } |
1157 | ||
fb2758c8 FB |
1158 | return 0; |
1159 | } | |
1160 | ||
1161 | /** | |
1162 | * Seek to the key frame just before the frame at timestamp | |
cdd5034f MN |
1163 | * 'timestamp' in 'stream_index'. |
1164 | * @param stream_index If stream_index is (-1), a default | |
1165 | * stream is selected | |
1166 | * @param timestamp timestamp in AV_TIME_BASE units | |
1167 | * @return >= 0 on success | |
fb2758c8 FB |
1168 | */ |
1169 | int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp) | |
1170 | { | |
1171 | int ret; | |
cdd5034f | 1172 | AVStream *st; |
fb2758c8 FB |
1173 | |
1174 | av_read_frame_flush(s); | |
cdd5034f MN |
1175 | |
1176 | if(stream_index < 0){ | |
1177 | stream_index= av_find_default_stream_index(s); | |
1178 | if(stream_index < 0) | |
1179 | return -1; | |
1180 | } | |
1181 | st= s->streams[stream_index]; | |
1182 | ||
1183 | timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); | |
fb2758c8 FB |
1184 | |
1185 | /* first, we try the format specific seek */ | |
1186 | if (s->iformat->read_seek) | |
1187 | ret = s->iformat->read_seek(s, stream_index, timestamp); | |
1188 | else | |
1189 | ret = -1; | |
1190 | if (ret >= 0) { | |
1191 | return 0; | |
1192 | } | |
8d14a25c MN |
1193 | |
1194 | if(s->iformat->read_timestamp) | |
1195 | return av_seek_frame_binary(s, stream_index, timestamp); | |
1196 | else | |
1197 | return av_seek_frame_generic(s, stream_index, timestamp); | |
fb2758c8 FB |
1198 | } |
1199 | ||
1200 | /*******************************************************/ | |
12f996ed FB |
1201 | |
1202 | /* return TRUE if the stream has accurate timings for at least one component */ | |
1203 | static int av_has_timings(AVFormatContext *ic) | |
1204 | { | |
1205 | int i; | |
1206 | AVStream *st; | |
1207 | ||
1208 | for(i = 0;i < ic->nb_streams; i++) { | |
1209 | st = ic->streams[i]; | |
1210 | if (st->start_time != AV_NOPTS_VALUE && | |
1211 | st->duration != AV_NOPTS_VALUE) | |
1212 | return 1; | |
1213 | } | |
1214 | return 0; | |
1215 | } | |
1216 | ||
1217 | /* estimate the stream timings from the one of each components. Also | |
1218 | compute the global bitrate if possible */ | |
1219 | static void av_update_stream_timings(AVFormatContext *ic) | |
1220 | { | |
1221 | int64_t start_time, end_time, end_time1; | |
1222 | int i; | |
1223 | AVStream *st; | |
1224 | ||
1225 | start_time = MAXINT64; | |
1226 | end_time = MININT64; | |
1227 | for(i = 0;i < ic->nb_streams; i++) { | |
1228 | st = ic->streams[i]; | |
1229 | if (st->start_time != AV_NOPTS_VALUE) { | |
1230 | if (st->start_time < start_time) | |
1231 | start_time = st->start_time; | |
1232 | if (st->duration != AV_NOPTS_VALUE) { | |
1233 | end_time1 = st->start_time + st->duration; | |
1234 | if (end_time1 > end_time) | |
1235 | end_time = end_time1; | |
1236 | } | |
1237 | } | |
1238 | } | |
1239 | if (start_time != MAXINT64) { | |
1240 | ic->start_time = start_time; | |
1241 | if (end_time != MAXINT64) { | |
1242 | ic->duration = end_time - start_time; | |
1243 | if (ic->file_size > 0) { | |
1244 | /* compute the bit rate */ | |
1245 | ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE / | |
1246 | (double)ic->duration; | |
1247 | } | |
1248 | } | |
1249 | } | |
1250 | ||
1251 | } | |
1252 | ||
1253 | static void fill_all_stream_timings(AVFormatContext *ic) | |
1254 | { | |
1255 | int i; | |
1256 | AVStream *st; | |
1257 | ||
1258 | av_update_stream_timings(ic); | |
1259 | for(i = 0;i < ic->nb_streams; i++) { | |
1260 | st = ic->streams[i]; | |
1261 | if (st->start_time == AV_NOPTS_VALUE) { | |
1262 | st->start_time = ic->start_time; | |
1263 | st->duration = ic->duration; | |
1264 | } | |
1265 | } | |
1266 | } | |
1267 | ||
1268 | static void av_estimate_timings_from_bit_rate(AVFormatContext *ic) | |
1269 | { | |
1270 | int64_t filesize, duration; | |
1271 | int bit_rate, i; | |
1272 | AVStream *st; | |
1273 | ||
1274 | /* if bit_rate is already set, we believe it */ | |
1275 | if (ic->bit_rate == 0) { | |
1276 | bit_rate = 0; | |
1277 | for(i=0;i<ic->nb_streams;i++) { | |
1278 | st = ic->streams[i]; | |
1279 | bit_rate += st->codec.bit_rate; | |
1280 | } | |
1281 | ic->bit_rate = bit_rate; | |
1282 | } | |
1283 | ||
1284 | /* if duration is already set, we believe it */ | |
1285 | if (ic->duration == AV_NOPTS_VALUE && | |
1286 | ic->bit_rate != 0 && | |
1287 | ic->file_size != 0) { | |
1288 | filesize = ic->file_size; | |
1289 | if (filesize > 0) { | |
1290 | duration = (int64_t)((8 * AV_TIME_BASE * (double)filesize) / (double)ic->bit_rate); | |
1291 | for(i = 0; i < ic->nb_streams; i++) { | |
1292 | st = ic->streams[i]; | |
1293 | if (st->start_time == AV_NOPTS_VALUE || | |
1294 | st->duration == AV_NOPTS_VALUE) { | |
1295 | st->start_time = 0; | |
1296 | st->duration = duration; | |
1297 | } | |
1298 | } | |
1299 | } | |
1300 | } | |
1301 | } | |
1302 | ||
12f996ed FB |
1303 | #define DURATION_MAX_READ_SIZE 250000 |
1304 | ||
1305 | /* only usable for MPEG-PS streams */ | |
1306 | static void av_estimate_timings_from_pts(AVFormatContext *ic) | |
1307 | { | |
1308 | AVPacket pkt1, *pkt = &pkt1; | |
1309 | AVStream *st; | |
1310 | int read_size, i, ret; | |
1311 | int64_t start_time, end_time, end_time1; | |
1312 | int64_t filesize, offset, duration; | |
1313 | ||
fb2758c8 FB |
1314 | /* free previous packet */ |
1315 | if (ic->cur_st && ic->cur_st->parser) | |
1316 | av_free_packet(&ic->cur_pkt); | |
1317 | ic->cur_st = NULL; | |
1318 | ||
1319 | /* flush packet queue */ | |
1320 | flush_packet_queue(ic); | |
1321 | ||
1322 | ||
12f996ed FB |
1323 | /* we read the first packets to get the first PTS (not fully |
1324 | accurate, but it is enough now) */ | |
1325 | url_fseek(&ic->pb, 0, SEEK_SET); | |
1326 | read_size = 0; | |
1327 | for(;;) { | |
1328 | if (read_size >= DURATION_MAX_READ_SIZE) | |
1329 | break; | |
1330 | /* if all info is available, we can stop */ | |
1331 | for(i = 0;i < ic->nb_streams; i++) { | |
1332 | st = ic->streams[i]; | |
1333 | if (st->start_time == AV_NOPTS_VALUE) | |
1334 | break; | |
1335 | } | |
1336 | if (i == ic->nb_streams) | |
1337 | break; | |
1338 | ||
1339 | ret = av_read_packet(ic, pkt); | |
1340 | if (ret != 0) | |
1341 | break; | |
1342 | read_size += pkt->size; | |
1343 | st = ic->streams[pkt->stream_index]; | |
1344 | if (pkt->pts != AV_NOPTS_VALUE) { | |
1345 | if (st->start_time == AV_NOPTS_VALUE) | |
9ee91c2f | 1346 | st->start_time = av_rescale(pkt->pts, st->time_base.num * (int64_t)AV_TIME_BASE, st->time_base.den); |
0a5f92a1 MN |
1347 | } |
1348 | av_free_packet(pkt); | |
1349 | } | |
12f996ed FB |
1350 | |
1351 | /* we compute the minimum start_time and use it as default */ | |
1352 | start_time = MAXINT64; | |
1353 | for(i = 0; i < ic->nb_streams; i++) { | |
1354 | st = ic->streams[i]; | |
1355 | if (st->start_time != AV_NOPTS_VALUE && | |
1356 | st->start_time < start_time) | |
1357 | start_time = st->start_time; | |
1358 | } | |
12f996ed FB |
1359 | if (start_time != MAXINT64) |
1360 | ic->start_time = start_time; | |
1361 | ||
1362 | /* estimate the end time (duration) */ | |
1363 | /* XXX: may need to support wrapping */ | |
1364 | filesize = ic->file_size; | |
1365 | offset = filesize - DURATION_MAX_READ_SIZE; | |
1366 | if (offset < 0) | |
1367 | offset = 0; | |
1368 | ||
12f996ed FB |
1369 | url_fseek(&ic->pb, offset, SEEK_SET); |
1370 | read_size = 0; | |
1371 | for(;;) { | |
1372 | if (read_size >= DURATION_MAX_READ_SIZE) | |
1373 | break; | |
1374 | /* if all info is available, we can stop */ | |
1375 | for(i = 0;i < ic->nb_streams; i++) { | |
1376 | st = ic->streams[i]; | |
1377 | if (st->duration == AV_NOPTS_VALUE) | |
1378 | break; | |
1379 | } | |
1380 | if (i == ic->nb_streams) | |
1381 | break; | |
1382 | ||
1383 | ret = av_read_packet(ic, pkt); | |
1384 | if (ret != 0) | |
1385 | break; | |
1386 | read_size += pkt->size; | |
1387 | st = ic->streams[pkt->stream_index]; | |
1388 | if (pkt->pts != AV_NOPTS_VALUE) { | |
9ee91c2f | 1389 | end_time = av_rescale(pkt->pts, st->time_base.num * (int64_t)AV_TIME_BASE, st->time_base.den); |
12f996ed FB |
1390 | duration = end_time - st->start_time; |
1391 | if (duration > 0) { | |
1392 | if (st->duration == AV_NOPTS_VALUE || | |
1393 | st->duration < duration) | |
1394 | st->duration = duration; | |
1395 | } | |
1396 | } | |
1397 | av_free_packet(pkt); | |
1398 | } | |
1399 | ||
1400 | /* estimate total duration */ | |
1401 | end_time = MININT64; | |
1402 | for(i = 0;i < ic->nb_streams; i++) { | |
1403 | st = ic->streams[i]; | |
1404 | if (st->duration != AV_NOPTS_VALUE) { | |
1405 | end_time1 = st->start_time + st->duration; | |
1406 | if (end_time1 > end_time) | |
1407 | end_time = end_time1; | |
1408 | } | |
1409 | } | |
1410 | ||
1411 | /* update start_time (new stream may have been created, so we do | |
1412 | it at the end */ | |
1413 | if (ic->start_time != AV_NOPTS_VALUE) { | |
1414 | for(i = 0; i < ic->nb_streams; i++) { | |
1415 | st = ic->streams[i]; | |
1416 | if (st->start_time == AV_NOPTS_VALUE) | |
1417 | st->start_time = ic->start_time; | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | if (end_time != MININT64) { | |
1422 | /* put dummy values for duration if needed */ | |
1423 | for(i = 0;i < ic->nb_streams; i++) { | |
1424 | st = ic->streams[i]; | |
1425 | if (st->duration == AV_NOPTS_VALUE && | |
1426 | st->start_time != AV_NOPTS_VALUE) | |
1427 | st->duration = end_time - st->start_time; | |
1428 | } | |
1429 | ic->duration = end_time - ic->start_time; | |
1430 | } | |
1431 | ||
1432 | url_fseek(&ic->pb, 0, SEEK_SET); | |
1433 | } | |
1434 | ||
1435 | static void av_estimate_timings(AVFormatContext *ic) | |
1436 | { | |
1437 | URLContext *h; | |
1438 | int64_t file_size; | |
1439 | ||
1440 | /* get the file size, if possible */ | |
1441 | if (ic->iformat->flags & AVFMT_NOFILE) { | |
1442 | file_size = 0; | |
1443 | } else { | |
1444 | h = url_fileno(&ic->pb); | |
1445 | file_size = url_filesize(h); | |
1446 | if (file_size < 0) | |
1447 | file_size = 0; | |
1448 | } | |
1449 | ic->file_size = file_size; | |
1450 | ||
1451 | if (ic->iformat == &mpegps_demux) { | |
1452 | /* get accurate estimate from the PTSes */ | |
1453 | av_estimate_timings_from_pts(ic); | |
1454 | } else if (av_has_timings(ic)) { | |
1455 | /* at least one components has timings - we use them for all | |
1456 | the components */ | |
1457 | fill_all_stream_timings(ic); | |
1458 | } else { | |
1459 | /* less precise: use bit rate info */ | |
1460 | av_estimate_timings_from_bit_rate(ic); | |
1461 | } | |
1462 | av_update_stream_timings(ic); | |
1463 | ||
1464 | #if 0 | |
1465 | { | |
1466 | int i; | |
1467 | AVStream *st; | |
1468 | for(i = 0;i < ic->nb_streams; i++) { | |
1469 | st = ic->streams[i]; | |
1470 | printf("%d: start_time: %0.3f duration: %0.3f\n", | |
1471 | i, (double)st->start_time / AV_TIME_BASE, | |
1472 | (double)st->duration / AV_TIME_BASE); | |
1473 | } | |
1474 | printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n", | |
1475 | (double)ic->start_time / AV_TIME_BASE, | |
1476 | (double)ic->duration / AV_TIME_BASE, | |
1477 | ic->bit_rate / 1000); | |
1478 | } | |
1479 | #endif | |
1480 | } | |
1481 | ||
b9a281db FB |
1482 | static int has_codec_parameters(AVCodecContext *enc) |
1483 | { | |
1484 | int val; | |
1485 | switch(enc->codec_type) { | |
1486 | case CODEC_TYPE_AUDIO: | |
1487 | val = enc->sample_rate; | |
1488 | break; | |
1489 | case CODEC_TYPE_VIDEO: | |
1490 | val = enc->width; | |
1491 | break; | |
1492 | default: | |
1493 | val = 1; | |
1494 | break; | |
1495 | } | |
1496 | return (val != 0); | |
1497 | } | |
1498 | ||
fb2758c8 FB |
1499 | static int try_decode_frame(AVStream *st, const uint8_t *data, int size) |
1500 | { | |
1501 | int16_t *samples; | |
1502 | AVCodec *codec; | |
1503 | int got_picture, ret; | |
1504 | AVFrame picture; | |
1505 | ||
1506 | codec = avcodec_find_decoder(st->codec.codec_id); | |
1507 | if (!codec) | |
1508 | return -1; | |
1509 | ret = avcodec_open(&st->codec, codec); | |
1510 | if (ret < 0) | |
1511 | return ret; | |
1512 | switch(st->codec.codec_type) { | |
1513 | case CODEC_TYPE_VIDEO: | |
1514 | ret = avcodec_decode_video(&st->codec, &picture, | |
1515 | &got_picture, (uint8_t *)data, size); | |
1516 | break; | |
1517 | case CODEC_TYPE_AUDIO: | |
1518 | samples = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); | |
1519 | if (!samples) | |
1520 | goto fail; | |
1521 | ret = avcodec_decode_audio(&st->codec, samples, | |
1522 | &got_picture, (uint8_t *)data, size); | |
1523 | av_free(samples); | |
1524 | break; | |
1525 | default: | |
1526 | break; | |
1527 | } | |
1528 | fail: | |
1529 | avcodec_close(&st->codec); | |
1530 | return ret; | |
1531 | } | |
1532 | ||
1533 | /* absolute maximum size we read until we abort */ | |
1534 | #define MAX_READ_SIZE 5000000 | |
1535 | ||
1536 | /* maximum duration until we stop analysing the stream */ | |
1537 | #define MAX_STREAM_DURATION ((int)(AV_TIME_BASE * 1.0)) | |
1538 | ||
b9a281db FB |
1539 | /** |
1540 | * Read the beginning of a media file to get stream information. This | |
1541 | * is useful for file formats with no headers such as MPEG. This | |
1542 | * function also compute the real frame rate in case of mpeg2 repeat | |
1543 | * frame mode. | |
1544 | * | |
1545 | * @param ic media file handle | |
1546 | * @return >=0 if OK. AVERROR_xxx if error. | |
1547 | */ | |
1548 | int av_find_stream_info(AVFormatContext *ic) | |
1549 | { | |
fb2758c8 | 1550 | int i, count, ret, read_size; |
b9a281db | 1551 | AVStream *st; |
fb2758c8 | 1552 | AVPacket pkt1, *pkt; |
b9a281db | 1553 | AVPacketList *pktl=NULL, **ppktl; |
b9a281db FB |
1554 | |
1555 | count = 0; | |
1556 | read_size = 0; | |
1557 | ppktl = &ic->packet_buffer; | |
1558 | for(;;) { | |
1559 | /* check if one codec still needs to be handled */ | |
1560 | for(i=0;i<ic->nb_streams;i++) { | |
1561 | st = ic->streams[i]; | |
fb2758c8 | 1562 | if (!has_codec_parameters(&st->codec)) |
b9a281db FB |
1563 | break; |
1564 | } | |
1565 | if (i == ic->nb_streams) { | |
1566 | /* NOTE: if the format has no header, then we need to read | |
1567 | some packets to get most of the streams, so we cannot | |
1568 | stop here */ | |
fb2758c8 | 1569 | if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { |
b9a281db FB |
1570 | /* if we found the info for all the codecs, we can stop */ |
1571 | ret = count; | |
1572 | break; | |
1573 | } | |
1574 | } else { | |
1575 | /* we did not get all the codec info, but we read too much data */ | |
fb2758c8 | 1576 | if (read_size >= MAX_READ_SIZE) { |
b9a281db FB |
1577 | ret = count; |
1578 | break; | |
1579 | } | |
1580 | } | |
1581 | ||
fb2758c8 FB |
1582 | /* NOTE: a new stream can be added there if no header in file |
1583 | (AVFMTCTX_NOHEADER) */ | |
1584 | ret = av_read_frame_internal(ic, &pkt1); | |
1585 | if (ret < 0) { | |
1586 | /* EOF or error */ | |
1587 | ret = -1; /* we could not have all the codec parameters before EOF */ | |
1588 | if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && | |
1589 | i == ic->nb_streams) | |
1590 | ret = 0; | |
1591 | break; | |
1592 | } | |
1593 | ||
b9a281db FB |
1594 | pktl = av_mallocz(sizeof(AVPacketList)); |
1595 | if (!pktl) { | |
1596 | ret = AVERROR_NOMEM; | |
1597 | break; | |
1598 | } | |
1599 | ||
1600 | /* add the packet in the buffered packet list */ | |
1601 | *ppktl = pktl; | |
1602 | ppktl = &pktl->next; | |
1603 | ||
b9a281db | 1604 | pkt = &pktl->pkt; |
fb2758c8 FB |
1605 | *pkt = pkt1; |
1606 | ||
1607 | /* duplicate the packet */ | |
1608 | if (av_dup_packet(pkt) < 0) { | |
1609 | ret = AVERROR_NOMEM; | |
1610 | break; | |
b9a281db | 1611 | } |
b9a281db | 1612 | |
fb2758c8 | 1613 | read_size += pkt->size; |
b9a281db FB |
1614 | |
1615 | st = ic->streams[pkt->stream_index]; | |
fb2758c8 FB |
1616 | st->codec_info_duration += pkt->duration; |
1617 | if (pkt->duration != 0) | |
1618 | st->codec_info_nb_frames++; | |
1619 | ||
1620 | /* if still no information, we try to open the codec and to | |
1621 | decompress the frame. We try to avoid that in most cases as | |
1622 | it takes longer and uses more memory. For MPEG4, we need to | |
1623 | decompress for Quicktime. */ | |
1624 | if (!has_codec_parameters(&st->codec) && | |
1625 | (st->codec.codec_id == CODEC_ID_FLV1 || | |
1626 | st->codec.codec_id == CODEC_ID_H264 || | |
1627 | st->codec.codec_id == CODEC_ID_H263 || | |
8bfed902 | 1628 | st->codec.codec_id == CODEC_ID_VORBIS || |
fb2758c8 FB |
1629 | (st->codec.codec_id == CODEC_ID_MPEG4 && !st->need_parsing))) |
1630 | try_decode_frame(st, pkt->data, pkt->size); | |
1631 | ||
1632 | if (st->codec_info_duration >= MAX_STREAM_DURATION) { | |
1633 | break; | |
b9a281db FB |
1634 | } |
1635 | count++; | |
1636 | } | |
1637 | ||
b9a281db FB |
1638 | /* set real frame rate info */ |
1639 | for(i=0;i<ic->nb_streams;i++) { | |
1640 | st = ic->streams[i]; | |
1641 | if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
fb2758c8 FB |
1642 | /* compute the real frame rate for telecine */ |
1643 | if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || | |
1644 | st->codec.codec_id == CODEC_ID_MPEG2VIDEO) && | |
1645 | st->codec.sub_id == 2) { | |
1646 | if (st->codec_info_nb_frames >= 20) { | |
1647 | float coded_frame_rate, est_frame_rate; | |
1648 | est_frame_rate = ((double)st->codec_info_nb_frames * AV_TIME_BASE) / | |
1649 | (double)st->codec_info_duration ; | |
1650 | coded_frame_rate = (double)st->codec.frame_rate / | |
1651 | (double)st->codec.frame_rate_base; | |
1652 | #if 0 | |
1653 | printf("telecine: coded_frame_rate=%0.3f est_frame_rate=%0.3f\n", | |
1654 | coded_frame_rate, est_frame_rate); | |
1655 | #endif | |
1656 | /* if we detect that it could be a telecine, we | |
1657 | signal it. It would be better to do it at a | |
1658 | higher level as it can change in a film */ | |
1659 | if (coded_frame_rate >= 24.97 && | |
1660 | (est_frame_rate >= 23.5 && est_frame_rate < 24.5)) { | |
1661 | st->r_frame_rate = 24024; | |
1662 | st->r_frame_rate_base = 1001; | |
1663 | } | |
1664 | } | |
1665 | } | |
1666 | /* if no real frame rate, use the codec one */ | |
14bea432 MN |
1667 | if (!st->r_frame_rate){ |
1668 | st->r_frame_rate = st->codec.frame_rate; | |
1669 | st->r_frame_rate_base = st->codec.frame_rate_base; | |
1670 | } | |
b9a281db | 1671 | } |
de6d9b64 | 1672 | } |
b9a281db | 1673 | |
12f996ed | 1674 | av_estimate_timings(ic); |
b9a281db | 1675 | return ret; |
de6d9b64 FB |
1676 | } |
1677 | ||
fb2758c8 FB |
1678 | /*******************************************************/ |
1679 | ||
1680 | /** | |
1681 | * start playing a network based stream (e.g. RTSP stream) at the | |
1682 | * current position | |
1683 | */ | |
1684 | int av_read_play(AVFormatContext *s) | |
1685 | { | |
1686 | if (!s->iformat->read_play) | |
1687 | return AVERROR_NOTSUPP; | |
1688 | return s->iformat->read_play(s); | |
1689 | } | |
1690 | ||
1691 | /** | |
1692 | * pause a network based stream (e.g. RTSP stream). Use av_read_play() | |
1693 | * to resume it. | |
1694 | */ | |
1695 | int av_read_pause(AVFormatContext *s) | |
1696 | { | |
1697 | if (!s->iformat->read_pause) | |
1698 | return AVERROR_NOTSUPP; | |
1699 | return s->iformat->read_pause(s); | |
1700 | } | |
1701 | ||
b9a281db FB |
1702 | /** |
1703 | * Close a media file (but not its codecs) | |
1704 | * | |
1705 | * @param s media file handle | |
1706 | */ | |
de6d9b64 FB |
1707 | void av_close_input_file(AVFormatContext *s) |
1708 | { | |
b6892136 | 1709 | int i, must_open_file; |
da24c5e3 | 1710 | AVStream *st; |
de6d9b64 | 1711 | |
fb2758c8 FB |
1712 | /* free previous packet */ |
1713 | if (s->cur_st && s->cur_st->parser) | |
1714 | av_free_packet(&s->cur_pkt); | |
1715 | ||
b9a281db FB |
1716 | if (s->iformat->read_close) |
1717 | s->iformat->read_close(s); | |
de6d9b64 | 1718 | for(i=0;i<s->nb_streams;i++) { |
da24c5e3 FB |
1719 | /* free all data in a stream component */ |
1720 | st = s->streams[i]; | |
fb2758c8 FB |
1721 | if (st->parser) { |
1722 | av_parser_close(st->parser); | |
de6d9b64 | 1723 | } |
fb2758c8 FB |
1724 | av_free(st->index_entries); |
1725 | av_free(st); | |
de6d9b64 | 1726 | } |
fb2758c8 | 1727 | flush_packet_queue(s); |
b6892136 | 1728 | must_open_file = 1; |
da24c5e3 | 1729 | if (s->iformat->flags & AVFMT_NOFILE) { |
b6892136 FB |
1730 | must_open_file = 0; |
1731 | } | |
1732 | if (must_open_file) { | |
96baaa6a FB |
1733 | url_fclose(&s->pb); |
1734 | } | |
a8dbe951 | 1735 | av_freep(&s->priv_data); |
1ea4f593 | 1736 | av_free(s); |
de6d9b64 FB |
1737 | } |
1738 | ||
b9a281db FB |
1739 | /** |
1740 | * Add a new stream to a media file. Can only be called in the | |
da24c5e3 FB |
1741 | * read_header function. If the flag AVFMTCTX_NOHEADER is in the |
1742 | * format context, then new streams can be added in read_packet too. | |
b9a281db FB |
1743 | * |
1744 | * | |
1745 | * @param s media file handle | |
da24c5e3 | 1746 | * @param id file format dependent stream id |
b9a281db FB |
1747 | */ |
1748 | AVStream *av_new_stream(AVFormatContext *s, int id) | |
1749 | { | |
1750 | AVStream *st; | |
1751 | ||
1752 | if (s->nb_streams >= MAX_STREAMS) | |
1753 | return NULL; | |
1754 | ||
1755 | st = av_mallocz(sizeof(AVStream)); | |
1756 | if (!st) | |
1757 | return NULL; | |
1e491e29 | 1758 | avcodec_get_context_defaults(&st->codec); |
48091512 FB |
1759 | if (s->iformat) { |
1760 | /* no default bitrate if decoding */ | |
1761 | st->codec.bit_rate = 0; | |
1762 | } | |
b9a281db FB |
1763 | st->index = s->nb_streams; |
1764 | st->id = id; | |
12f996ed FB |
1765 | st->start_time = AV_NOPTS_VALUE; |
1766 | st->duration = AV_NOPTS_VALUE; | |
9ee91c2f MN |
1767 | |
1768 | /* default pts settings is MPEG like */ | |
1769 | av_set_pts_info(st, 33, 1, 90000); | |
77405fc8 | 1770 | st->last_IP_pts = AV_NOPTS_VALUE; |
9ee91c2f | 1771 | |
b9a281db FB |
1772 | s->streams[s->nb_streams++] = st; |
1773 | return st; | |
1774 | } | |
1775 | ||
1776 | /************************************************************/ | |
1777 | /* output media file */ | |
de6d9b64 | 1778 | |
87a0a681 FB |
1779 | int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
1780 | { | |
1781 | int ret; | |
98486a6b RS |
1782 | |
1783 | if (s->oformat->priv_data_size > 0) { | |
1784 | s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
1785 | if (!s->priv_data) | |
1786 | return AVERROR_NOMEM; | |
1787 | } else | |
1788 | s->priv_data = NULL; | |
1789 | ||
87a0a681 FB |
1790 | if (s->oformat->set_parameters) { |
1791 | ret = s->oformat->set_parameters(s, ap); | |
1792 | if (ret < 0) | |
1793 | return ret; | |
1794 | } | |
1795 | return 0; | |
1796 | } | |
1797 | ||
b9a281db FB |
1798 | /** |
1799 | * allocate the stream private data and write the stream header to an | |
1800 | * output media file | |
1801 | * | |
1802 | * @param s media file handle | |
1803 | * @return 0 if OK. AVERROR_xxx if error. | |
1804 | */ | |
1805 | int av_write_header(AVFormatContext *s) | |
1806 | { | |
1e51d801 FB |
1807 | int ret, i; |
1808 | AVStream *st; | |
1809 | ||
1e51d801 FB |
1810 | ret = s->oformat->write_header(s); |
1811 | if (ret < 0) | |
1812 | return ret; | |
1813 | ||
1814 | /* init PTS generation */ | |
1815 | for(i=0;i<s->nb_streams;i++) { | |
1816 | st = s->streams[i]; | |
1817 | ||
1818 | switch (st->codec.codec_type) { | |
1819 | case CODEC_TYPE_AUDIO: | |
1820 | av_frac_init(&st->pts, 0, 0, | |
9ee91c2f | 1821 | (int64_t)st->time_base.num * st->codec.sample_rate); |
1e51d801 FB |
1822 | break; |
1823 | case CODEC_TYPE_VIDEO: | |
1824 | av_frac_init(&st->pts, 0, 0, | |
9ee91c2f | 1825 | (int64_t)st->time_base.num * st->codec.frame_rate); |
1e51d801 FB |
1826 | break; |
1827 | default: | |
1828 | break; | |
1829 | } | |
1830 | } | |
1831 | return 0; | |
b9a281db FB |
1832 | } |
1833 | ||
1834 | /** | |
1e51d801 FB |
1835 | * Write a packet to an output media file. The packet shall contain |
1836 | * one audio or video frame. | |
b9a281db FB |
1837 | * |
1838 | * @param s media file handle | |
1e51d801 FB |
1839 | * @param stream_index stream index |
1840 | * @param buf buffer containing the frame data | |
1841 | * @param size size of buffer | |
d4c0ff91 | 1842 | * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. |
b9a281db | 1843 | */ |
1e51d801 FB |
1844 | int av_write_frame(AVFormatContext *s, int stream_index, const uint8_t *buf, |
1845 | int size) | |
de6d9b64 | 1846 | { |
1e51d801 | 1847 | AVStream *st; |
0c1a9eda | 1848 | int64_t pts_mask; |
7feb950a | 1849 | int ret, frame_size; |
1e51d801 FB |
1850 | |
1851 | st = s->streams[stream_index]; | |
9ee91c2f | 1852 | pts_mask = (1LL << st->pts_wrap_bits) - 1; |
b0c7f5a9 MN |
1853 | |
1854 | /* HACK/FIXME we skip all zero size audio packets so a encoder can pass pts by outputing zero size packets */ | |
1855 | if(st->codec.codec_type==CODEC_TYPE_AUDIO && size==0) | |
1856 | ret = 0; | |
1857 | else | |
1858 | ret = s->oformat->write_packet(s, stream_index, buf, size, | |
1859 | st->pts.val & pts_mask); | |
1860 | ||
1e51d801 FB |
1861 | if (ret < 0) |
1862 | return ret; | |
1863 | ||
1864 | /* update pts */ | |
1865 | switch (st->codec.codec_type) { | |
1866 | case CODEC_TYPE_AUDIO: | |
fb2758c8 | 1867 | frame_size = get_audio_frame_size(&st->codec, size); |
6d8f985e | 1868 | |
b0c7f5a9 | 1869 | /* HACK/FIXME, we skip the initial 0-size packets as they are most likely equal to the encoder delay, |
6d8f985e | 1870 | but it would be better if we had the real timestamps from the encoder */ |
6d8f985e | 1871 | if (frame_size >= 0 && (size || st->pts.num!=st->pts.den>>1 || st->pts.val)) { |
9ee91c2f | 1872 | av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size); |
7feb950a | 1873 | } |
1e51d801 FB |
1874 | break; |
1875 | case CODEC_TYPE_VIDEO: | |
9ee91c2f | 1876 | av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec.frame_rate_base); |
1e51d801 FB |
1877 | break; |
1878 | default: | |
1879 | break; | |
1880 | } | |
d4c0ff91 | 1881 | return ret; |
b9a281db FB |
1882 | } |
1883 | ||
1884 | /** | |
1885 | * write the stream trailer to an output media file and and free the | |
1886 | * file private data. | |
1887 | * | |
1888 | * @param s media file handle | |
1889 | * @return 0 if OK. AVERROR_xxx if error. */ | |
1890 | int av_write_trailer(AVFormatContext *s) | |
1891 | { | |
1892 | int ret; | |
1893 | ret = s->oformat->write_trailer(s); | |
1894 | av_freep(&s->priv_data); | |
1895 | return ret; | |
de6d9b64 FB |
1896 | } |
1897 | ||
1898 | /* "user interface" functions */ | |
1899 | ||
1900 | void dump_format(AVFormatContext *ic, | |
1901 | int index, | |
1902 | const char *url, | |
1903 | int is_output) | |
1904 | { | |
b9a281db | 1905 | int i, flags; |
de6d9b64 FB |
1906 | char buf[256]; |
1907 | ||
43465395 | 1908 | av_log(NULL, AV_LOG_DEBUG, "%s #%d, %s, %s '%s':\n", |
de6d9b64 | 1909 | is_output ? "Output" : "Input", |
b9a281db FB |
1910 | index, |
1911 | is_output ? ic->oformat->name : ic->iformat->name, | |
de6d9b64 | 1912 | is_output ? "to" : "from", url); |
12f996ed | 1913 | if (!is_output) { |
43465395 | 1914 | av_log(NULL, AV_LOG_DEBUG, " Duration: "); |
12f996ed FB |
1915 | if (ic->duration != AV_NOPTS_VALUE) { |
1916 | int hours, mins, secs, us; | |
1917 | secs = ic->duration / AV_TIME_BASE; | |
1918 | us = ic->duration % AV_TIME_BASE; | |
1919 | mins = secs / 60; | |
1920 | secs %= 60; | |
1921 | hours = mins / 60; | |
1922 | mins %= 60; | |
43465395 | 1923 | av_log(NULL, AV_LOG_DEBUG, "%02d:%02d:%02d.%01d", hours, mins, secs, |
12f996ed FB |
1924 | (10 * us) / AV_TIME_BASE); |
1925 | } else { | |
43465395 | 1926 | av_log(NULL, AV_LOG_DEBUG, "N/A"); |
12f996ed | 1927 | } |
43465395 | 1928 | av_log(NULL, AV_LOG_DEBUG, ", bitrate: "); |
12f996ed | 1929 | if (ic->bit_rate) { |
43465395 | 1930 | av_log(NULL, AV_LOG_DEBUG,"%d kb/s", ic->bit_rate / 1000); |
12f996ed | 1931 | } else { |
43465395 | 1932 | av_log(NULL, AV_LOG_DEBUG, "N/A"); |
12f996ed | 1933 | } |
43465395 | 1934 | av_log(NULL, AV_LOG_DEBUG, "\n"); |
12f996ed | 1935 | } |
de6d9b64 FB |
1936 | for(i=0;i<ic->nb_streams;i++) { |
1937 | AVStream *st = ic->streams[i]; | |
1938 | avcodec_string(buf, sizeof(buf), &st->codec, is_output); | |
43465395 | 1939 | av_log(NULL, AV_LOG_DEBUG, " Stream #%d.%d", index, i); |
b9a281db FB |
1940 | /* the pid is an important information, so we display it */ |
1941 | /* XXX: add a generic system */ | |
1942 | if (is_output) | |
1943 | flags = ic->oformat->flags; | |
1944 | else | |
1945 | flags = ic->iformat->flags; | |
1946 | if (flags & AVFMT_SHOW_IDS) { | |
43465395 | 1947 | av_log(NULL, AV_LOG_DEBUG, "[0x%x]", st->id); |
b9a281db | 1948 | } |
43465395 | 1949 | av_log(NULL, AV_LOG_DEBUG, ": %s\n", buf); |
de6d9b64 FB |
1950 | } |
1951 | } | |
1952 | ||
1953 | typedef struct { | |
445f1b83 | 1954 | const char *abv; |
de6d9b64 | 1955 | int width, height; |
445f1b83 RS |
1956 | int frame_rate, frame_rate_base; |
1957 | } AbvEntry; | |
1958 | ||
1959 | static AbvEntry frame_abvs[] = { | |
ba2a8cb4 RS |
1960 | { "ntsc", 720, 480, 30000, 1001 }, |
1961 | { "pal", 720, 576, 25, 1 }, | |
1962 | { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */ | |
1963 | { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */ | |
904736b5 RS |
1964 | { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */ |
1965 | { "spal", 768, 576, 25, 1 }, /* square pixel pal */ | |
445f1b83 RS |
1966 | { "film", 352, 240, 24, 1 }, |
1967 | { "ntsc-film", 352, 240, 24000, 1001 }, | |
1968 | { "sqcif", 128, 96, 0, 0 }, | |
1969 | { "qcif", 176, 144, 0, 0 }, | |
1970 | { "cif", 352, 288, 0, 0 }, | |
1971 | { "4cif", 704, 576, 0, 0 }, | |
de6d9b64 | 1972 | }; |
445f1b83 | 1973 | |
de6d9b64 FB |
1974 | int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
1975 | { | |
1976 | int i; | |
445f1b83 | 1977 | int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
de6d9b64 FB |
1978 | const char *p; |
1979 | int frame_width = 0, frame_height = 0; | |
1980 | ||
1981 | for(i=0;i<n;i++) { | |
445f1b83 RS |
1982 | if (!strcmp(frame_abvs[i].abv, str)) { |
1983 | frame_width = frame_abvs[i].width; | |
1984 | frame_height = frame_abvs[i].height; | |
de6d9b64 FB |
1985 | break; |
1986 | } | |
1987 | } | |
1988 | if (i == n) { | |
1989 | p = str; | |
1990 | frame_width = strtol(p, (char **)&p, 10); | |
1991 | if (*p) | |
1992 | p++; | |
1993 | frame_height = strtol(p, (char **)&p, 10); | |
1994 | } | |
1995 | if (frame_width <= 0 || frame_height <= 0) | |
1996 | return -1; | |
1997 | *width_ptr = frame_width; | |
1998 | *height_ptr = frame_height; | |
1999 | return 0; | |
2000 | } | |
2001 | ||
445f1b83 RS |
2002 | int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
2003 | { | |
2004 | int i; | |
2005 | char* cp; | |
2006 | ||
2007 | /* First, we check our abbreviation table */ | |
2008 | for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
2009 | if (!strcmp(frame_abvs[i].abv, arg)) { | |
2010 | *frame_rate = frame_abvs[i].frame_rate; | |
2011 | *frame_rate_base = frame_abvs[i].frame_rate_base; | |
2012 | return 0; | |
2013 | } | |
2014 | ||
2015 | /* Then, we try to parse it as fraction */ | |
2016 | cp = strchr(arg, '/'); | |
2017 | if (cp) { | |
2018 | char* cpp; | |
2019 | *frame_rate = strtol(arg, &cpp, 10); | |
2020 | if (cpp != arg || cpp == cp) | |
2021 | *frame_rate_base = strtol(cp+1, &cpp, 10); | |
2022 | else | |
2023 | *frame_rate = 0; | |
2024 | } | |
2025 | else { | |
2026 | /* Finally we give up and parse it as double */ | |
8bfed902 | 2027 | *frame_rate_base = DEFAULT_FRAME_RATE_BASE; //FIXME use av_d2q() |
445f1b83 RS |
2028 | *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5); |
2029 | } | |
2030 | if (!*frame_rate || !*frame_rate_base) | |
2031 | return -1; | |
2032 | else | |
2033 | return 0; | |
2034 | } | |
2035 | ||
916c80e9 FB |
2036 | /* Syntax: |
2037 | * - If not a duration: | |
2038 | * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
c5510dd6 | 2039 | * Time is localtime unless Z is suffixed to the end. In this case GMT |
916c80e9 FB |
2040 | * Return the date in micro seconds since 1970 |
2041 | * - If duration: | |
2042 | * HH[:MM[:SS[.m...]]] | |
2043 | * S+[.m...] | |
2044 | */ | |
0c1a9eda | 2045 | int64_t parse_date(const char *datestr, int duration) |
de6d9b64 FB |
2046 | { |
2047 | const char *p; | |
0c1a9eda | 2048 | int64_t t; |
2dbceb9f | 2049 | struct tm dt; |
c5510dd6 PG |
2050 | int i; |
2051 | static const char *date_fmt[] = { | |
2052 | "%Y-%m-%d", | |
2053 | "%Y%m%d", | |
2054 | }; | |
2055 | static const char *time_fmt[] = { | |
2056 | "%H:%M:%S", | |
2057 | "%H%M%S", | |
2058 | }; | |
2059 | const char *q; | |
916c80e9 | 2060 | int is_utc, len; |
c5510dd6 | 2061 | char lastch; |
6d8f985e MN |
2062 | |
2063 | #undef time | |
c5510dd6 PG |
2064 | time_t now = time(0); |
2065 | ||
916c80e9 FB |
2066 | len = strlen(datestr); |
2067 | if (len > 0) | |
2068 | lastch = datestr[len - 1]; | |
2069 | else | |
2070 | lastch = '\0'; | |
c5510dd6 | 2071 | is_utc = (lastch == 'z' || lastch == 'Z'); |
2dbceb9f PG |
2072 | |
2073 | memset(&dt, 0, sizeof(dt)); | |
de6d9b64 FB |
2074 | |
2075 | p = datestr; | |
916c80e9 | 2076 | q = NULL; |
de6d9b64 | 2077 | if (!duration) { |
c5510dd6 | 2078 | for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { |
f71869a4 | 2079 | q = small_strptime(p, date_fmt[i], &dt); |
c5510dd6 PG |
2080 | if (q) { |
2081 | break; | |
2082 | } | |
2083 | } | |
2084 | ||
2085 | if (!q) { | |
2086 | if (is_utc) { | |
2087 | dt = *gmtime(&now); | |
2088 | } else { | |
2089 | dt = *localtime(&now); | |
2090 | } | |
2091 | dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
de6d9b64 | 2092 | } else { |
c5510dd6 | 2093 | p = q; |
de6d9b64 | 2094 | } |
c5510dd6 PG |
2095 | |
2096 | if (*p == 'T' || *p == 't' || *p == ' ') | |
2097 | p++; | |
c5510dd6 | 2098 | |
916c80e9 | 2099 | for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { |
f71869a4 | 2100 | q = small_strptime(p, time_fmt[i], &dt); |
916c80e9 FB |
2101 | if (q) { |
2102 | break; | |
2103 | } | |
2104 | } | |
2105 | } else { | |
f71869a4 | 2106 | q = small_strptime(p, time_fmt[0], &dt); |
916c80e9 FB |
2107 | if (!q) { |
2108 | dt.tm_sec = strtol(p, (char **)&q, 10); | |
2109 | dt.tm_min = 0; | |
2110 | dt.tm_hour = 0; | |
c5510dd6 PG |
2111 | } |
2112 | } | |
2113 | ||
2114 | /* Now we have all the fields that we can get */ | |
2115 | if (!q) { | |
2116 | if (duration) | |
2117 | return 0; | |
2118 | else | |
0c1a9eda | 2119 | return now * int64_t_C(1000000); |
de6d9b64 | 2120 | } |
2dbceb9f PG |
2121 | |
2122 | if (duration) { | |
c5510dd6 | 2123 | t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; |
2dbceb9f | 2124 | } else { |
c5510dd6 PG |
2125 | dt.tm_isdst = -1; /* unknown */ |
2126 | if (is_utc) { | |
2127 | t = mktimegm(&dt); | |
2128 | } else { | |
2129 | t = mktime(&dt); | |
2130 | } | |
de6d9b64 | 2131 | } |
2dbceb9f | 2132 | |
c5510dd6 PG |
2133 | t *= 1000000; |
2134 | ||
2135 | if (*q == '.') { | |
de6d9b64 | 2136 | int val, n; |
c5510dd6 PG |
2137 | q++; |
2138 | for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
2139 | if (!isdigit(*q)) | |
2140 | break; | |
2141 | val += n * (*q - '0'); | |
de6d9b64 FB |
2142 | } |
2143 | t += val; | |
2144 | } | |
2145 | return t; | |
2146 | } | |
2147 | ||
2dbceb9f | 2148 | /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return |
de6d9b64 FB |
2149 | 1 if found */ |
2150 | int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) | |
2151 | { | |
2152 | const char *p; | |
2153 | char tag[128], *q; | |
2154 | ||
2155 | p = info; | |
2156 | if (*p == '?') | |
2157 | p++; | |
2158 | for(;;) { | |
2159 | q = tag; | |
2160 | while (*p != '\0' && *p != '=' && *p != '&') { | |
2161 | if ((q - tag) < sizeof(tag) - 1) | |
2162 | *q++ = *p; | |
2163 | p++; | |
2164 | } | |
2165 | *q = '\0'; | |
2166 | q = arg; | |
2167 | if (*p == '=') { | |
2168 | p++; | |
2169 | while (*p != '&' && *p != '\0') { | |
2dbceb9f PG |
2170 | if ((q - arg) < arg_size - 1) { |
2171 | if (*p == '+') | |
2172 | *q++ = ' '; | |
2173 | else | |
2174 | *q++ = *p; | |
2175 | } | |
de6d9b64 FB |
2176 | p++; |
2177 | } | |
2178 | *q = '\0'; | |
2179 | } | |
2180 | if (!strcmp(tag, tag1)) | |
2181 | return 1; | |
2182 | if (*p != '&') | |
2183 | break; | |
8d1335ea | 2184 | p++; |
de6d9b64 FB |
2185 | } |
2186 | return 0; | |
2187 | } | |
2188 | ||
9150f42e FB |
2189 | /* Return in 'buf' the path with '%d' replaced by number. Also handles |
2190 | the '%0nd' format where 'n' is the total number of digits and | |
2191 | '%%'. Return 0 if OK, and -1 if format error */ | |
2192 | int get_frame_filename(char *buf, int buf_size, | |
2193 | const char *path, int number) | |
2194 | { | |
2195 | const char *p; | |
0bf92f79 PI |
2196 | char *q, buf1[20], c; |
2197 | int nd, len, percentd_found; | |
9150f42e FB |
2198 | |
2199 | q = buf; | |
2200 | p = path; | |
2201 | percentd_found = 0; | |
2202 | for(;;) { | |
2203 | c = *p++; | |
2204 | if (c == '\0') | |
2205 | break; | |
2206 | if (c == '%') { | |
c9646fda PG |
2207 | do { |
2208 | nd = 0; | |
2209 | while (isdigit(*p)) { | |
2210 | nd = nd * 10 + *p++ - '0'; | |
2211 | } | |
2212 | c = *p++; | |
c9646fda PG |
2213 | } while (isdigit(c)); |
2214 | ||
9150f42e FB |
2215 | switch(c) { |
2216 | case '%': | |
2217 | goto addchar; | |
2218 | case 'd': | |
2219 | if (percentd_found) | |
2220 | goto fail; | |
2221 | percentd_found = 1; | |
2222 | snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
2223 | len = strlen(buf1); | |
2224 | if ((q - buf + len) > buf_size - 1) | |
2225 | goto fail; | |
2226 | memcpy(q, buf1, len); | |
2227 | q += len; | |
2228 | break; | |
2229 | default: | |
2230 | goto fail; | |
2231 | } | |
2232 | } else { | |
2233 | addchar: | |
2234 | if ((q - buf) < buf_size - 1) | |
2235 | *q++ = c; | |
2236 | } | |
2237 | } | |
2238 | if (!percentd_found) | |
2239 | goto fail; | |
2240 | *q = '\0'; | |
2241 | return 0; | |
2242 | fail: | |
2243 | *q = '\0'; | |
2244 | return -1; | |
2245 | } | |
2246 | ||
b9a281db | 2247 | /** |
fb2758c8 FB |
2248 | * Print nice hexa dump of a buffer |
2249 | * @param f stream for output | |
b9a281db FB |
2250 | * @param buf buffer |
2251 | * @param size buffer size | |
2252 | */ | |
fb2758c8 | 2253 | void av_hex_dump(FILE *f, uint8_t *buf, int size) |
b9a281db FB |
2254 | { |
2255 | int len, i, j, c; | |
2256 | ||
2257 | for(i=0;i<size;i+=16) { | |
2258 | len = size - i; | |
2259 | if (len > 16) | |
2260 | len = 16; | |
fb2758c8 | 2261 | fprintf(f, "%08x ", i); |
b9a281db FB |
2262 | for(j=0;j<16;j++) { |
2263 | if (j < len) | |
fb2758c8 | 2264 | fprintf(f, " %02x", buf[i+j]); |
b9a281db | 2265 | else |
fb2758c8 | 2266 | fprintf(f, " "); |
b9a281db | 2267 | } |
fb2758c8 | 2268 | fprintf(f, " "); |
b9a281db FB |
2269 | for(j=0;j<len;j++) { |
2270 | c = buf[i+j]; | |
2271 | if (c < ' ' || c > '~') | |
2272 | c = '.'; | |
fb2758c8 | 2273 | fprintf(f, "%c", c); |
b9a281db | 2274 | } |
fb2758c8 | 2275 | fprintf(f, "\n"); |
b9a281db FB |
2276 | } |
2277 | } | |
2278 | ||
fb2758c8 FB |
2279 | /** |
2280 | * Print on 'f' a nice dump of a packet | |
2281 | * @param f stream for output | |
2282 | * @param pkt packet to dump | |
2283 | * @param dump_payload true if the payload must be displayed too | |
2284 | */ | |
2285 | void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload) | |
2286 | { | |
2287 | fprintf(f, "stream #%d:\n", pkt->stream_index); | |
2288 | fprintf(f, " keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0)); | |
2289 | fprintf(f, " duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE); | |
37353960 FB |
2290 | /* DTS is _always_ valid after av_read_frame() */ |
2291 | fprintf(f, " dts="); | |
2292 | if (pkt->dts == AV_NOPTS_VALUE) | |
2293 | fprintf(f, "N/A"); | |
2294 | else | |
2295 | fprintf(f, "%0.3f", (double)pkt->dts / AV_TIME_BASE); | |
fb2758c8 FB |
2296 | /* PTS may be not known if B frames are present */ |
2297 | fprintf(f, " pts="); | |
2298 | if (pkt->pts == AV_NOPTS_VALUE) | |
2299 | fprintf(f, "N/A"); | |
2300 | else | |
2301 | fprintf(f, "%0.3f", (double)pkt->pts / AV_TIME_BASE); | |
2302 | fprintf(f, "\n"); | |
2303 | fprintf(f, " size=%d\n", pkt->size); | |
2304 | if (dump_payload) | |
2305 | av_hex_dump(f, pkt->data, pkt->size); | |
2306 | } | |
2307 | ||
a9a721da FB |
2308 | void url_split(char *proto, int proto_size, |
2309 | char *hostname, int hostname_size, | |
2310 | int *port_ptr, | |
2311 | char *path, int path_size, | |
2312 | const char *url) | |
2313 | { | |
2314 | const char *p; | |
2315 | char *q; | |
2316 | int port; | |
2317 | ||
2318 | port = -1; | |
2319 | ||
2320 | p = url; | |
2321 | q = proto; | |
2322 | while (*p != ':' && *p != '\0') { | |
2323 | if ((q - proto) < proto_size - 1) | |
2324 | *q++ = *p; | |
2325 | p++; | |
2326 | } | |
2327 | if (proto_size > 0) | |
2328 | *q = '\0'; | |
2329 | if (*p == '\0') { | |
2330 | if (proto_size > 0) | |
2331 | proto[0] = '\0'; | |
2332 | if (hostname_size > 0) | |
2333 | hostname[0] = '\0'; | |
2334 | p = url; | |
2335 | } else { | |
2336 | p++; | |
2337 | if (*p == '/') | |
2338 | p++; | |
2339 | if (*p == '/') | |
2340 | p++; | |
2341 | q = hostname; | |
2342 | while (*p != ':' && *p != '/' && *p != '?' && *p != '\0') { | |
2343 | if ((q - hostname) < hostname_size - 1) | |
2344 | *q++ = *p; | |
2345 | p++; | |
2346 | } | |
2347 | if (hostname_size > 0) | |
2348 | *q = '\0'; | |
2349 | if (*p == ':') { | |
2350 | p++; | |
2351 | port = strtoul(p, (char **)&p, 10); | |
2352 | } | |
2353 | } | |
2354 | if (port_ptr) | |
2355 | *port_ptr = port; | |
2356 | pstrcpy(path, path_size, p); | |
2357 | } | |
2358 | ||
916c80e9 FB |
2359 | /** |
2360 | * Set the pts for a given stream | |
2361 | * @param s stream | |
2362 | * @param pts_wrap_bits number of bits effectively used by the pts | |
2363 | * (used for wrap control, 33 is the value for MPEG) | |
2364 | * @param pts_num numerator to convert to seconds (MPEG: 1) | |
2365 | * @param pts_den denominator to convert to seconds (MPEG: 90000) | |
2366 | */ | |
9ee91c2f | 2367 | void av_set_pts_info(AVStream *s, int pts_wrap_bits, |
916c80e9 FB |
2368 | int pts_num, int pts_den) |
2369 | { | |
2370 | s->pts_wrap_bits = pts_wrap_bits; | |
9ee91c2f MN |
2371 | s->time_base.num = pts_num; |
2372 | s->time_base.den = pts_den; | |
916c80e9 FB |
2373 | } |
2374 | ||
2375 | /* fraction handling */ | |
2376 | ||
2377 | /** | |
2378 | * f = val + (num / den) + 0.5. 'num' is normalized so that it is such | |
2379 | * as 0 <= num < den. | |
2380 | * | |
2381 | * @param f fractional number | |
2382 | * @param val integer value | |
2383 | * @param num must be >= 0 | |
2384 | * @param den must be >= 1 | |
2385 | */ | |
0c1a9eda | 2386 | void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
916c80e9 FB |
2387 | { |
2388 | num += (den >> 1); | |
2389 | if (num >= den) { | |
2390 | val += num / den; | |
2391 | num = num % den; | |
2392 | } | |
2393 | f->val = val; | |
2394 | f->num = num; | |
2395 | f->den = den; | |
2396 | } | |
2397 | ||
2398 | /* set f to (val + 0.5) */ | |
0c1a9eda | 2399 | void av_frac_set(AVFrac *f, int64_t val) |
916c80e9 FB |
2400 | { |
2401 | f->val = val; | |
2402 | f->num = f->den >> 1; | |
2403 | } | |
2404 | ||
2405 | /** | |
2406 | * Fractionnal addition to f: f = f + (incr / f->den) | |
2407 | * | |
2408 | * @param f fractional number | |
2409 | * @param incr increment, can be positive or negative | |
2410 | */ | |
0c1a9eda | 2411 | void av_frac_add(AVFrac *f, int64_t incr) |
916c80e9 | 2412 | { |
0c1a9eda | 2413 | int64_t num, den; |
916c80e9 FB |
2414 | |
2415 | num = f->num + incr; | |
2416 | den = f->den; | |
2417 | if (num < 0) { | |
2418 | f->val += num / den; | |
2419 | num = num % den; | |
2420 | if (num < 0) { | |
2421 | num += den; | |
2422 | f->val--; | |
2423 | } | |
2424 | } else if (num >= den) { | |
2425 | f->val += num / den; | |
2426 | num = num % den; | |
2427 | } | |
2428 | f->num = num; | |
2429 | } | |
87a0a681 FB |
2430 | |
2431 | /** | |
2432 | * register a new image format | |
2433 | * @param img_fmt Image format descriptor | |
2434 | */ | |
2435 | void av_register_image_format(AVImageFormat *img_fmt) | |
2436 | { | |
2437 | AVImageFormat **p; | |
2438 | ||
2439 | p = &first_image_format; | |
2440 | while (*p != NULL) p = &(*p)->next; | |
2441 | *p = img_fmt; | |
2442 | img_fmt->next = NULL; | |
2443 | } | |
2444 | ||
2445 | /* guess image format */ | |
2446 | AVImageFormat *av_probe_image_format(AVProbeData *pd) | |
2447 | { | |
2448 | AVImageFormat *fmt1, *fmt; | |
2449 | int score, score_max; | |
2450 | ||
2451 | fmt = NULL; | |
2452 | score_max = 0; | |
2453 | for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
2454 | if (fmt1->img_probe) { | |
2455 | score = fmt1->img_probe(pd); | |
2456 | if (score > score_max) { | |
2457 | score_max = score; | |
2458 | fmt = fmt1; | |
2459 | } | |
2460 | } | |
2461 | } | |
2462 | return fmt; | |
2463 | } | |
2464 | ||
2465 | AVImageFormat *guess_image_format(const char *filename) | |
2466 | { | |
2467 | AVImageFormat *fmt1; | |
2468 | ||
2469 | for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
2470 | if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
2471 | return fmt1; | |
2472 | } | |
2473 | return NULL; | |
2474 | } | |
2475 | ||
2476 | /** | |
2477 | * Read an image from a stream. | |
2478 | * @param gb byte stream containing the image | |
2479 | * @param fmt image format, NULL if probing is required | |
2480 | */ | |
2481 | int av_read_image(ByteIOContext *pb, const char *filename, | |
2482 | AVImageFormat *fmt, | |
2483 | int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
2484 | { | |
2485 | char buf[PROBE_BUF_SIZE]; | |
2486 | AVProbeData probe_data, *pd = &probe_data; | |
2487 | offset_t pos; | |
2488 | int ret; | |
2489 | ||
2490 | if (!fmt) { | |
5c91a675 | 2491 | pd->filename = filename; |
87a0a681 FB |
2492 | pd->buf = buf; |
2493 | pos = url_ftell(pb); | |
2494 | pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); | |
2495 | url_fseek(pb, pos, SEEK_SET); | |
2496 | fmt = av_probe_image_format(pd); | |
2497 | } | |
2498 | if (!fmt) | |
2499 | return AVERROR_NOFMT; | |
2500 | ret = fmt->img_read(pb, alloc_cb, opaque); | |
2501 | return ret; | |
2502 | } | |
2503 | ||
2504 | /** | |
2505 | * Write an image to a stream. | |
2506 | * @param pb byte stream for the image output | |
2507 | * @param fmt image format | |
2508 | * @param img image data and informations | |
2509 | */ | |
2510 | int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
2511 | { | |
2512 | return fmt->img_write(pb, img); | |
2513 | } | |
2514 |