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 | ||
8b69867f MN |
24 | AVInputFormat *first_iformat = NULL; |
25 | AVOutputFormat *first_oformat = NULL; | |
26 | AVImageFormat *first_image_format = NULL; | |
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 | 415 | must_open_file = 0; |
63dd1377 | 416 | pb= NULL; //FIXME this or memset(pb, 0, sizeof(ByteIOContext)); otherwise its uninitalized |
b6892136 FB |
417 | } |
418 | ||
419 | if (!fmt || must_open_file) { | |
87a0a681 | 420 | /* if no file needed do not try to open one */ |
da24c5e3 | 421 | if (url_fopen(pb, filename, URL_RDONLY) < 0) { |
b9a281db | 422 | err = AVERROR_IO; |
96baaa6a | 423 | goto fail; |
b9a281db | 424 | } |
da24c5e3 | 425 | file_opened = 1; |
96baaa6a | 426 | if (buf_size > 0) { |
da24c5e3 | 427 | url_setbufsize(pb, buf_size); |
96baaa6a | 428 | } |
5b25dfa7 FB |
429 | if (!fmt) { |
430 | /* read probe data */ | |
da24c5e3 | 431 | pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); |
53e2f9ca MN |
432 | if (url_fseek(pb, 0, SEEK_SET) == (offset_t)-EPIPE) { |
433 | url_fclose(pb); | |
434 | if (url_fopen(pb, filename, URL_RDONLY) < 0) { | |
435 | err = AVERROR_IO; | |
436 | goto fail; | |
437 | } | |
438 | } | |
5b25dfa7 | 439 | } |
96baaa6a FB |
440 | } |
441 | ||
b9a281db FB |
442 | /* guess file format */ |
443 | if (!fmt) { | |
a25e098d | 444 | fmt = av_probe_input_format(pd, 1); |
b9a281db FB |
445 | } |
446 | ||
447 | /* if still no format found, error */ | |
448 | if (!fmt) { | |
449 | err = AVERROR_NOFMT; | |
da24c5e3 | 450 | goto fail; |
de6d9b64 | 451 | } |
b9a281db | 452 | |
67d06418 | 453 | /* XXX: suppress this hack for redirectors */ |
f3ec2d46 | 454 | #ifdef CONFIG_NETWORK |
67d06418 | 455 | if (fmt == &redir_demux) { |
da24c5e3 FB |
456 | err = redir_open(ic_ptr, pb); |
457 | url_fclose(pb); | |
67d06418 FB |
458 | return err; |
459 | } | |
9b2e001f | 460 | #endif |
67d06418 | 461 | |
87a0a681 | 462 | /* check filename in case of an image number is expected */ |
da24c5e3 FB |
463 | if (fmt->flags & AVFMT_NEEDNUMBER) { |
464 | if (filename_number_test(filename) < 0) { | |
87a0a681 | 465 | err = AVERROR_NUMEXPECTED; |
da24c5e3 | 466 | goto fail; |
87a0a681 FB |
467 | } |
468 | } | |
da24c5e3 FB |
469 | err = av_open_input_stream(ic_ptr, pb, filename, fmt, ap); |
470 | if (err) | |
471 | goto fail; | |
b9a281db | 472 | return 0; |
de6d9b64 | 473 | fail: |
da24c5e3 FB |
474 | if (file_opened) |
475 | url_fclose(pb); | |
b9a281db FB |
476 | *ic_ptr = NULL; |
477 | return err; | |
da24c5e3 | 478 | |
de6d9b64 FB |
479 | } |
480 | ||
da24c5e3 FB |
481 | /*******************************************************/ |
482 | ||
b9a281db | 483 | /** |
fb2758c8 FB |
484 | * Read a transport packet from a media file. This function is |
485 | * absolete and should never be used. Use av_read_frame() instead. | |
da24c5e3 | 486 | * |
b9a281db FB |
487 | * @param s media file handle |
488 | * @param pkt is filled | |
da24c5e3 | 489 | * @return 0 if OK. AVERROR_xxx if error. |
b9a281db | 490 | */ |
de6d9b64 FB |
491 | int av_read_packet(AVFormatContext *s, AVPacket *pkt) |
492 | { | |
fb2758c8 FB |
493 | return s->iformat->read_packet(s, pkt); |
494 | } | |
495 | ||
496 | /**********************************************************/ | |
497 | ||
fb2758c8 FB |
498 | /* get the number of samples of an audio frame. Return (-1) if error */ |
499 | static int get_audio_frame_size(AVCodecContext *enc, int size) | |
500 | { | |
501 | int frame_size; | |
502 | ||
503 | if (enc->frame_size <= 1) { | |
504 | /* specific hack for pcm codecs because no frame size is | |
505 | provided */ | |
506 | switch(enc->codec_id) { | |
507 | case CODEC_ID_PCM_S16LE: | |
508 | case CODEC_ID_PCM_S16BE: | |
509 | case CODEC_ID_PCM_U16LE: | |
510 | case CODEC_ID_PCM_U16BE: | |
511 | if (enc->channels == 0) | |
512 | return -1; | |
513 | frame_size = size / (2 * enc->channels); | |
514 | break; | |
515 | case CODEC_ID_PCM_S8: | |
516 | case CODEC_ID_PCM_U8: | |
517 | case CODEC_ID_PCM_MULAW: | |
518 | case CODEC_ID_PCM_ALAW: | |
519 | if (enc->channels == 0) | |
520 | return -1; | |
521 | frame_size = size / (enc->channels); | |
522 | break; | |
523 | default: | |
524 | /* used for example by ADPCM codecs */ | |
525 | if (enc->bit_rate == 0) | |
526 | return -1; | |
527 | frame_size = (size * 8 * enc->sample_rate) / enc->bit_rate; | |
528 | break; | |
529 | } | |
530 | } else { | |
531 | frame_size = enc->frame_size; | |
532 | } | |
533 | return frame_size; | |
534 | } | |
535 | ||
536 | ||
537 | /* return the frame duration in seconds, return 0 if not available */ | |
3c895fc0 | 538 | static void compute_frame_duration(int *pnum, int *pden, AVStream *st, |
fb2758c8 FB |
539 | AVCodecParserContext *pc, AVPacket *pkt) |
540 | { | |
541 | int frame_size; | |
542 | ||
543 | *pnum = 0; | |
544 | *pden = 0; | |
545 | switch(st->codec.codec_type) { | |
546 | case CODEC_TYPE_VIDEO: | |
547 | *pnum = st->codec.frame_rate_base; | |
548 | *pden = st->codec.frame_rate; | |
549 | if (pc && pc->repeat_pict) { | |
550 | *pden *= 2; | |
551 | *pnum = (*pnum) * (2 + pc->repeat_pict); | |
552 | } | |
553 | break; | |
554 | case CODEC_TYPE_AUDIO: | |
555 | frame_size = get_audio_frame_size(&st->codec, pkt->size); | |
556 | if (frame_size < 0) | |
557 | break; | |
558 | *pnum = frame_size; | |
559 | *pden = st->codec.sample_rate; | |
560 | break; | |
561 | default: | |
562 | break; | |
563 | } | |
564 | } | |
565 | ||
77405fc8 | 566 | static int64_t lsb2full(int64_t lsb, int64_t last_ts, int lsb_bits){ |
4fc2c644 | 567 | int64_t mask = lsb_bits < 64 ? (1LL<<lsb_bits)-1 : -1LL; |
77405fc8 MN |
568 | int64_t delta= last_ts - mask/2; |
569 | return ((lsb - delta)&mask) + delta; | |
570 | } | |
571 | ||
fb2758c8 FB |
572 | static void compute_pkt_fields(AVFormatContext *s, AVStream *st, |
573 | AVCodecParserContext *pc, AVPacket *pkt) | |
574 | { | |
575 | int num, den, presentation_delayed; | |
576 | ||
77405fc8 | 577 | /* handle wrapping */ |
e928649b MN |
578 | if(st->cur_dts != AV_NOPTS_VALUE){ |
579 | if(pkt->pts != AV_NOPTS_VALUE) | |
580 | pkt->pts= lsb2full(pkt->pts, st->cur_dts, st->pts_wrap_bits); | |
581 | if(pkt->dts != AV_NOPTS_VALUE) | |
582 | pkt->dts= lsb2full(pkt->dts, st->cur_dts, st->pts_wrap_bits); | |
583 | } | |
77405fc8 | 584 | |
fb2758c8 | 585 | if (pkt->duration == 0) { |
3c895fc0 | 586 | compute_frame_duration(&num, &den, st, pc, pkt); |
fb2758c8 | 587 | if (den && num) { |
77405fc8 | 588 | pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num); |
fb2758c8 FB |
589 | } |
590 | } | |
591 | ||
592 | /* do we have a video B frame ? */ | |
593 | presentation_delayed = 0; | |
594 | if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
595 | /* XXX: need has_b_frame, but cannot get it if the codec is | |
596 | not initialized */ | |
597 | if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || | |
598 | st->codec.codec_id == CODEC_ID_MPEG2VIDEO || | |
599 | st->codec.codec_id == CODEC_ID_MPEG4 || | |
600 | st->codec.codec_id == CODEC_ID_H264) && | |
601 | pc && pc->pict_type != FF_B_TYPE) | |
602 | presentation_delayed = 1; | |
77405fc8 MN |
603 | /* this may be redundant, but it shouldnt hurt */ |
604 | if(pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts > pkt->dts) | |
605 | presentation_delayed = 1; | |
fb2758c8 | 606 | } |
e928649b MN |
607 | |
608 | if(st->cur_dts == AV_NOPTS_VALUE){ | |
609 | if(presentation_delayed) st->cur_dts = -pkt->duration; | |
610 | else st->cur_dts = 0; | |
611 | } | |
fb2758c8 | 612 | |
3c895fc0 | 613 | // av_log(NULL, AV_LOG_DEBUG, "IN delayed:%d pts:%lld, dts:%lld cur_dts:%lld st:%d pc:%p\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts, pkt->stream_index, pc); |
fb2758c8 FB |
614 | /* interpolate PTS and DTS if they are not present */ |
615 | if (presentation_delayed) { | |
616 | /* DTS = decompression time stamp */ | |
617 | /* PTS = presentation time stamp */ | |
618 | if (pkt->dts == AV_NOPTS_VALUE) { | |
77405fc8 MN |
619 | /* if we know the last pts, use it */ |
620 | if(st->last_IP_pts != AV_NOPTS_VALUE) | |
621 | st->cur_dts = pkt->dts = st->last_IP_pts; | |
622 | else | |
623 | pkt->dts = st->cur_dts; | |
fb2758c8 FB |
624 | } else { |
625 | st->cur_dts = pkt->dts; | |
626 | } | |
627 | /* this is tricky: the dts must be incremented by the duration | |
628 | of the frame we are displaying, i.e. the last I or P frame */ | |
629 | if (st->last_IP_duration == 0) | |
630 | st->cur_dts += pkt->duration; | |
631 | else | |
632 | st->cur_dts += st->last_IP_duration; | |
633 | st->last_IP_duration = pkt->duration; | |
77405fc8 | 634 | st->last_IP_pts= pkt->pts; |
fb2758c8 FB |
635 | /* cannot compute PTS if not present (we can compute it only |
636 | by knowing the futur */ | |
637 | } else { | |
638 | /* presentation is not delayed : PTS and DTS are the same */ | |
639 | if (pkt->pts == AV_NOPTS_VALUE) { | |
2092bd75 GB |
640 | if (pkt->dts == AV_NOPTS_VALUE) { |
641 | pkt->pts = st->cur_dts; | |
642 | pkt->dts = st->cur_dts; | |
643 | } | |
644 | else { | |
645 | st->cur_dts = pkt->dts; | |
646 | pkt->pts = pkt->dts; | |
647 | } | |
fb2758c8 FB |
648 | } else { |
649 | st->cur_dts = pkt->pts; | |
650 | pkt->dts = pkt->pts; | |
651 | } | |
652 | st->cur_dts += pkt->duration; | |
653 | } | |
e928649b | 654 | // av_log(NULL, AV_LOG_DEBUG, "OUTdelayed:%d pts:%lld, dts:%lld cur_dts:%lld\n", presentation_delayed, pkt->pts, pkt->dts, st->cur_dts); |
fb2758c8 FB |
655 | |
656 | /* update flags */ | |
657 | if (pc) { | |
658 | pkt->flags = 0; | |
659 | /* key frame computation */ | |
660 | switch(st->codec.codec_type) { | |
661 | case CODEC_TYPE_VIDEO: | |
662 | if (pc->pict_type == FF_I_TYPE) | |
663 | pkt->flags |= PKT_FLAG_KEY; | |
664 | break; | |
665 | case CODEC_TYPE_AUDIO: | |
666 | pkt->flags |= PKT_FLAG_KEY; | |
667 | break; | |
668 | default: | |
669 | break; | |
670 | } | |
671 | } | |
672 | ||
77405fc8 MN |
673 | /* convert the packet time stamp units */ |
674 | if(pkt->pts != AV_NOPTS_VALUE) | |
675 | pkt->pts = av_rescale(pkt->pts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den); | |
676 | if(pkt->dts != AV_NOPTS_VALUE) | |
677 | pkt->dts = av_rescale(pkt->dts, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den); | |
678 | ||
679 | /* duration field */ | |
680 | pkt->duration = av_rescale(pkt->duration, AV_TIME_BASE * (int64_t)st->time_base.num, st->time_base.den); | |
fb2758c8 FB |
681 | } |
682 | ||
63dd1377 | 683 | void av_destruct_packet_nofree(AVPacket *pkt) |
fb2758c8 FB |
684 | { |
685 | pkt->data = NULL; pkt->size = 0; | |
686 | } | |
687 | ||
688 | static int av_read_frame_internal(AVFormatContext *s, AVPacket *pkt) | |
689 | { | |
690 | AVStream *st; | |
37353960 | 691 | int len, ret, i; |
fb2758c8 FB |
692 | |
693 | for(;;) { | |
694 | /* select current input stream component */ | |
695 | st = s->cur_st; | |
696 | if (st) { | |
697 | if (!st->parser) { | |
698 | /* no parsing needed: we just output the packet as is */ | |
699 | /* raw data support */ | |
700 | *pkt = s->cur_pkt; | |
701 | compute_pkt_fields(s, st, NULL, pkt); | |
702 | s->cur_st = NULL; | |
703 | return 0; | |
704 | } else if (s->cur_len > 0) { | |
fb2758c8 | 705 | len = av_parser_parse(st->parser, &st->codec, &pkt->data, &pkt->size, |
6ec87caa FB |
706 | s->cur_ptr, s->cur_len, |
707 | s->cur_pkt.pts, s->cur_pkt.dts); | |
708 | s->cur_pkt.pts = AV_NOPTS_VALUE; | |
709 | s->cur_pkt.dts = AV_NOPTS_VALUE; | |
fb2758c8 FB |
710 | /* increment read pointer */ |
711 | s->cur_ptr += len; | |
712 | s->cur_len -= len; | |
713 | ||
714 | /* return packet if any */ | |
715 | if (pkt->size) { | |
37353960 | 716 | got_packet: |
fb2758c8 FB |
717 | pkt->duration = 0; |
718 | pkt->stream_index = st->index; | |
6ec87caa FB |
719 | pkt->pts = st->parser->pts; |
720 | pkt->dts = st->parser->dts; | |
fb2758c8 FB |
721 | pkt->destruct = av_destruct_packet_nofree; |
722 | compute_pkt_fields(s, st, st->parser, pkt); | |
fb2758c8 FB |
723 | return 0; |
724 | } | |
725 | } else { | |
bcbecff1 FB |
726 | /* free packet */ |
727 | av_free_packet(&s->cur_pkt); | |
fb2758c8 FB |
728 | s->cur_st = NULL; |
729 | } | |
730 | } else { | |
fb2758c8 FB |
731 | /* read next packet */ |
732 | ret = av_read_packet(s, &s->cur_pkt); | |
37353960 FB |
733 | if (ret < 0) { |
734 | if (ret == -EAGAIN) | |
735 | return ret; | |
736 | /* return the last frames, if any */ | |
737 | for(i = 0; i < s->nb_streams; i++) { | |
738 | st = s->streams[i]; | |
739 | if (st->parser) { | |
740 | av_parser_parse(st->parser, &st->codec, | |
741 | &pkt->data, &pkt->size, | |
6ec87caa FB |
742 | NULL, 0, |
743 | AV_NOPTS_VALUE, AV_NOPTS_VALUE); | |
37353960 FB |
744 | if (pkt->size) |
745 | goto got_packet; | |
746 | } | |
747 | } | |
748 | /* no more packets: really terminates parsing */ | |
fb2758c8 | 749 | return ret; |
37353960 | 750 | } |
9ee91c2f MN |
751 | |
752 | st = s->streams[s->cur_pkt.stream_index]; | |
fb2758c8 | 753 | |
fb2758c8 FB |
754 | s->cur_st = st; |
755 | s->cur_ptr = s->cur_pkt.data; | |
756 | s->cur_len = s->cur_pkt.size; | |
757 | if (st->need_parsing && !st->parser) { | |
758 | st->parser = av_parser_init(st->codec.codec_id); | |
759 | if (!st->parser) { | |
760 | /* no parser available : just output the raw packets */ | |
761 | st->need_parsing = 0; | |
762 | } | |
763 | } | |
764 | } | |
765 | } | |
766 | } | |
767 | ||
768 | /** | |
769 | * Return the next frame of a stream. The returned packet is valid | |
770 | * until the next av_read_frame() or until av_close_input_file() and | |
771 | * must be freed with av_free_packet. For video, the packet contains | |
772 | * exactly one frame. For audio, it contains an integer number of | |
773 | * frames if each frame has a known fixed size (e.g. PCM or ADPCM | |
774 | * data). If the audio frames have a variable size (e.g. MPEG audio), | |
775 | * then it contains one frame. | |
776 | * | |
777 | * pkt->pts, pkt->dts and pkt->duration are always set to correct | |
778 | * values in AV_TIME_BASE unit (and guessed if the format cannot | |
779 | * provided them). pkt->pts can be AV_NOPTS_VALUE if the video format | |
780 | * has B frames, so it is better to rely on pkt->dts if you do not | |
781 | * decompress the payload. | |
782 | * | |
783 | * Return 0 if OK, < 0 if error or end of file. | |
784 | */ | |
785 | int av_read_frame(AVFormatContext *s, AVPacket *pkt) | |
786 | { | |
de6d9b64 FB |
787 | AVPacketList *pktl; |
788 | ||
789 | pktl = s->packet_buffer; | |
790 | if (pktl) { | |
791 | /* read packet from packet buffer, if there is data */ | |
792 | *pkt = pktl->pkt; | |
793 | s->packet_buffer = pktl->next; | |
1ea4f593 | 794 | av_free(pktl); |
de6d9b64 FB |
795 | return 0; |
796 | } else { | |
fb2758c8 FB |
797 | return av_read_frame_internal(s, pkt); |
798 | } | |
799 | } | |
800 | ||
801 | /* XXX: suppress the packet queue */ | |
802 | static void flush_packet_queue(AVFormatContext *s) | |
803 | { | |
804 | AVPacketList *pktl; | |
805 | ||
806 | for(;;) { | |
807 | pktl = s->packet_buffer; | |
808 | if (!pktl) | |
809 | break; | |
810 | s->packet_buffer = pktl->next; | |
811 | av_free_packet(&pktl->pkt); | |
812 | av_free(pktl); | |
b9a281db FB |
813 | } |
814 | } | |
815 | ||
da24c5e3 | 816 | /*******************************************************/ |
fb2758c8 FB |
817 | /* seek support */ |
818 | ||
b754978a MN |
819 | int av_find_default_stream_index(AVFormatContext *s) |
820 | { | |
821 | int i; | |
822 | AVStream *st; | |
823 | ||
824 | if (s->nb_streams <= 0) | |
825 | return -1; | |
826 | for(i = 0; i < s->nb_streams; i++) { | |
827 | st = s->streams[i]; | |
828 | if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
829 | return i; | |
830 | } | |
831 | } | |
832 | return 0; | |
833 | } | |
834 | ||
fb2758c8 FB |
835 | /* flush the frame reader */ |
836 | static void av_read_frame_flush(AVFormatContext *s) | |
837 | { | |
838 | AVStream *st; | |
839 | int i; | |
840 | ||
841 | flush_packet_queue(s); | |
842 | ||
843 | /* free previous packet */ | |
844 | if (s->cur_st) { | |
845 | if (s->cur_st->parser) | |
846 | av_free_packet(&s->cur_pkt); | |
847 | s->cur_st = NULL; | |
848 | } | |
849 | /* fail safe */ | |
850 | s->cur_ptr = NULL; | |
851 | s->cur_len = 0; | |
852 | ||
853 | /* for each stream, reset read state */ | |
854 | for(i = 0; i < s->nb_streams; i++) { | |
855 | st = s->streams[i]; | |
856 | ||
857 | if (st->parser) { | |
858 | av_parser_close(st->parser); | |
859 | st->parser = NULL; | |
860 | } | |
77405fc8 | 861 | st->last_IP_pts = AV_NOPTS_VALUE; |
fb2758c8 FB |
862 | st->cur_dts = 0; /* we set the current DTS to an unspecified origin */ |
863 | } | |
864 | } | |
865 | ||
cdd5034f MN |
866 | /** |
867 | * add a index entry into a sorted list updateing if it is already there. | |
868 | * @param timestamp timestamp in the timebase of the given stream | |
869 | */ | |
3e9245a9 MN |
870 | int av_add_index_entry(AVStream *st, |
871 | int64_t pos, int64_t timestamp, int distance, int flags) | |
fb2758c8 FB |
872 | { |
873 | AVIndexEntry *entries, *ie; | |
b754978a | 874 | int index; |
fb2758c8 FB |
875 | |
876 | entries = av_fast_realloc(st->index_entries, | |
877 | &st->index_entries_allocated_size, | |
878 | (st->nb_index_entries + 1) * | |
879 | sizeof(AVIndexEntry)); | |
b754978a MN |
880 | st->index_entries= entries; |
881 | ||
882 | if(st->nb_index_entries){ | |
883 | index= av_index_search_timestamp(st, timestamp); | |
884 | ie= &entries[index]; | |
885 | ||
886 | if(ie->timestamp != timestamp){ | |
887 | if(ie->timestamp < timestamp){ | |
888 | index++; //index points to next instead of previous entry, maybe nonexistant | |
889 | ie= &st->index_entries[index]; | |
890 | }else | |
891 | assert(index==0); | |
892 | ||
893 | if(index != st->nb_index_entries){ | |
894 | assert(index < st->nb_index_entries); | |
895 | memmove(entries + index + 1, entries + index, sizeof(AVIndexEntry)*(st->nb_index_entries - index)); | |
896 | } | |
897 | st->nb_index_entries++; | |
c9247fc3 MN |
898 | }else{ |
899 | if(ie->pos == pos && distance < ie->min_distance) //dont reduce the distance | |
900 | distance= ie->min_distance; | |
b754978a | 901 | } |
3e9245a9 MN |
902 | }else{ |
903 | index= st->nb_index_entries++; | |
904 | ie= &entries[index]; | |
905 | } | |
b754978a MN |
906 | |
907 | ie->pos = pos; | |
908 | ie->timestamp = timestamp; | |
3e9245a9 | 909 | ie->min_distance= distance; |
b754978a | 910 | ie->flags = flags; |
3e9245a9 MN |
911 | |
912 | return index; | |
fb2758c8 FB |
913 | } |
914 | ||
915 | /* build an index for raw streams using a parser */ | |
916 | static void av_build_index_raw(AVFormatContext *s) | |
917 | { | |
918 | AVPacket pkt1, *pkt = &pkt1; | |
919 | int ret; | |
920 | AVStream *st; | |
921 | ||
922 | st = s->streams[0]; | |
923 | av_read_frame_flush(s); | |
924 | url_fseek(&s->pb, s->data_offset, SEEK_SET); | |
925 | ||
926 | for(;;) { | |
927 | ret = av_read_frame(s, pkt); | |
928 | if (ret < 0) | |
929 | break; | |
930 | if (pkt->stream_index == 0 && st->parser && | |
931 | (pkt->flags & PKT_FLAG_KEY)) { | |
cdd5034f MN |
932 | int64_t dts= av_rescale(pkt->dts, st->time_base.den, AV_TIME_BASE*(int64_t)st->time_base.num); |
933 | av_add_index_entry(st, st->parser->frame_offset, dts, | |
3e9245a9 | 934 | 0, AVINDEX_KEYFRAME); |
fb2758c8 FB |
935 | } |
936 | av_free_packet(pkt); | |
937 | } | |
938 | } | |
939 | ||
940 | /* return TRUE if we deal with a raw stream (raw codec data and | |
941 | parsing needed) */ | |
942 | static int is_raw_stream(AVFormatContext *s) | |
943 | { | |
944 | AVStream *st; | |
945 | ||
946 | if (s->nb_streams != 1) | |
947 | return 0; | |
948 | st = s->streams[0]; | |
949 | if (!st->need_parsing) | |
950 | return 0; | |
951 | return 1; | |
952 | } | |
953 | ||
954 | /* return the largest index entry whose timestamp is <= | |
955 | wanted_timestamp */ | |
b754978a | 956 | int av_index_search_timestamp(AVStream *st, int wanted_timestamp) |
fb2758c8 | 957 | { |
b754978a MN |
958 | AVIndexEntry *entries= st->index_entries; |
959 | int nb_entries= st->nb_index_entries; | |
fb2758c8 FB |
960 | int a, b, m; |
961 | int64_t timestamp; | |
962 | ||
963 | if (nb_entries <= 0) | |
964 | return -1; | |
965 | ||
966 | a = 0; | |
967 | b = nb_entries - 1; | |
b754978a MN |
968 | |
969 | while (a < b) { | |
970 | m = (a + b + 1) >> 1; | |
fb2758c8 | 971 | timestamp = entries[m].timestamp; |
b754978a | 972 | if (timestamp > wanted_timestamp) { |
fb2758c8 FB |
973 | b = m - 1; |
974 | } else { | |
b754978a | 975 | a = m; |
fb2758c8 FB |
976 | } |
977 | } | |
b754978a | 978 | return a; |
fb2758c8 FB |
979 | } |
980 | ||
8d14a25c MN |
981 | #define DEBUG_SEEK |
982 | ||
cdd5034f MN |
983 | /** |
984 | * Does a binary search using av_index_search_timestamp() and AVCodec.read_timestamp(). | |
985 | * this isnt supposed to be called directly by a user application, but by demuxers | |
986 | * @param target_ts target timestamp in the time base of the given stream | |
987 | * @param stream_index stream number | |
988 | */ | |
8d14a25c MN |
989 | int av_seek_frame_binary(AVFormatContext *s, int stream_index, int64_t target_ts){ |
990 | AVInputFormat *avif= s->iformat; | |
991 | int64_t pos_min, pos_max, pos, pos_limit; | |
992 | int64_t ts_min, ts_max, ts; | |
993 | int64_t start_pos; | |
cdd5034f | 994 | int index, no_change, i; |
8d14a25c MN |
995 | AVStream *st; |
996 | ||
cdd5034f MN |
997 | if (stream_index < 0) |
998 | return -1; | |
999 | ||
8d14a25c MN |
1000 | #ifdef DEBUG_SEEK |
1001 | av_log(s, AV_LOG_DEBUG, "read_seek: %d %lld\n", stream_index, target_ts); | |
1002 | #endif | |
1003 | ||
1004 | ts_max= | |
1005 | ts_min= AV_NOPTS_VALUE; | |
1006 | pos_limit= -1; //gcc falsely says it may be uninitalized | |
1007 | ||
1008 | st= s->streams[stream_index]; | |
1009 | if(st->index_entries){ | |
1010 | AVIndexEntry *e; | |
1011 | ||
1012 | index= av_index_search_timestamp(st, target_ts); | |
1013 | e= &st->index_entries[index]; | |
1014 | ||
1015 | if(e->timestamp <= target_ts || e->pos == e->min_distance){ | |
1016 | pos_min= e->pos; | |
1017 | ts_min= e->timestamp; | |
1018 | #ifdef DEBUG_SEEK | |
a6a92a9a | 1019 | av_log(s, AV_LOG_DEBUG, "using cached pos_min=0x%llx dts_min=%lld\n", |
8d14a25c MN |
1020 | pos_min,ts_min); |
1021 | #endif | |
1022 | }else{ | |
1023 | assert(index==0); | |
1024 | } | |
1025 | index++; | |
1026 | if(index < st->nb_index_entries){ | |
1027 | e= &st->index_entries[index]; | |
1028 | assert(e->timestamp >= target_ts); | |
1029 | pos_max= e->pos; | |
1030 | ts_max= e->timestamp; | |
1031 | pos_limit= pos_max - e->min_distance; | |
1032 | #ifdef DEBUG_SEEK | |
a6a92a9a | 1033 | av_log(s, AV_LOG_DEBUG, "using cached pos_max=0x%llx pos_limit=0x%llx dts_max=%lld\n", |
8d14a25c MN |
1034 | pos_max,pos_limit, ts_max); |
1035 | #endif | |
1036 | } | |
1037 | } | |
1038 | ||
1039 | if(ts_min == AV_NOPTS_VALUE){ | |
1040 | pos_min = s->data_offset; | |
1041 | ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); | |
1042 | if (ts_min == AV_NOPTS_VALUE) | |
1043 | return -1; | |
1044 | } | |
1045 | ||
1046 | if(ts_max == AV_NOPTS_VALUE){ | |
1047 | int step= 1024; | |
1048 | pos_max = url_filesize(url_fileno(&s->pb)) - 1; | |
1049 | do{ | |
1050 | pos_max -= step; | |
1051 | ts_max = avif->read_timestamp(s, stream_index, &pos_max, pos_max + step); | |
1052 | step += step; | |
1053 | }while(ts_max == AV_NOPTS_VALUE && pos_max >= step); | |
1054 | if (ts_max == AV_NOPTS_VALUE) | |
1055 | return -1; | |
1056 | ||
1057 | for(;;){ | |
1058 | int64_t tmp_pos= pos_max + 1; | |
1059 | int64_t tmp_ts= avif->read_timestamp(s, stream_index, &tmp_pos, INT64_MAX); | |
1060 | if(tmp_ts == AV_NOPTS_VALUE) | |
1061 | break; | |
1062 | ts_max= tmp_ts; | |
1063 | pos_max= tmp_pos; | |
1064 | } | |
1065 | pos_limit= pos_max; | |
1066 | } | |
1067 | ||
1068 | no_change=0; | |
1069 | while (pos_min < pos_limit) { | |
1070 | #ifdef DEBUG_SEEK | |
1071 | av_log(s, AV_LOG_DEBUG, "pos_min=0x%llx pos_max=0x%llx dts_min=%lld dts_max=%lld\n", | |
1072 | pos_min, pos_max, | |
1073 | ts_min, ts_max); | |
1074 | #endif | |
1075 | assert(pos_limit <= pos_max); | |
1076 | ||
1077 | if(no_change==0){ | |
1078 | int64_t approximate_keyframe_distance= pos_max - pos_limit; | |
1079 | // interpolate position (better than dichotomy) | |
1080 | pos = (int64_t)((double)(pos_max - pos_min) * | |
1081 | (double)(target_ts - ts_min) / | |
1082 | (double)(ts_max - ts_min)) + pos_min - approximate_keyframe_distance; | |
1083 | }else if(no_change==1){ | |
1084 | // bisection, if interpolation failed to change min or max pos last time | |
1085 | pos = (pos_min + pos_limit)>>1; | |
1086 | }else{ | |
1087 | // linear search if bisection failed, can only happen if there are very few or no keframes between min/max | |
1088 | pos=pos_min; | |
1089 | } | |
1090 | if(pos <= pos_min) | |
1091 | pos= pos_min + 1; | |
1092 | else if(pos > pos_limit) | |
1093 | pos= pos_limit; | |
1094 | start_pos= pos; | |
1095 | ||
1096 | ts = avif->read_timestamp(s, stream_index, &pos, INT64_MAX); //may pass pos_limit instead of -1 | |
1097 | if(pos == pos_max) | |
1098 | no_change++; | |
1099 | else | |
1100 | no_change=0; | |
1101 | #ifdef DEBUG_SEEK | |
1102 | 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); | |
1103 | #endif | |
1104 | assert(ts != AV_NOPTS_VALUE); | |
1105 | if (target_ts < ts) { | |
1106 | pos_limit = start_pos - 1; | |
1107 | pos_max = pos; | |
1108 | ts_max = ts; | |
1109 | } else { | |
1110 | pos_min = pos; | |
1111 | ts_min = ts; | |
1112 | /* check if we are lucky */ | |
1113 | if (target_ts == ts) | |
1114 | break; | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | pos = pos_min; | |
1119 | #ifdef DEBUG_SEEK | |
1120 | pos_min = pos; | |
1121 | ts_min = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); | |
1122 | pos_min++; | |
1123 | ts_max = avif->read_timestamp(s, stream_index, &pos_min, INT64_MAX); | |
1124 | av_log(s, AV_LOG_DEBUG, "pos=0x%llx %lld<=%lld<=%lld\n", | |
1125 | pos, ts_min, target_ts, ts_max); | |
1126 | #endif | |
1127 | /* do the seek */ | |
1128 | url_fseek(&s->pb, pos, SEEK_SET); | |
cdd5034f MN |
1129 | |
1130 | ts= av_rescale(ts_min, AV_TIME_BASE*(int64_t)st->time_base.num, st->time_base.den); | |
1131 | for(i = 0; i < s->nb_streams; i++) { | |
1132 | st = s->streams[i]; | |
1133 | ||
77405fc8 | 1134 | st->cur_dts = av_rescale(ts, st->time_base.den, AV_TIME_BASE*(int64_t)st->time_base.num); |
cdd5034f | 1135 | } |
8d14a25c MN |
1136 | |
1137 | return 0; | |
1138 | } | |
1139 | ||
fb2758c8 FB |
1140 | static int av_seek_frame_generic(AVFormatContext *s, |
1141 | int stream_index, int64_t timestamp) | |
1142 | { | |
cdd5034f | 1143 | int index, i; |
fb2758c8 FB |
1144 | AVStream *st; |
1145 | AVIndexEntry *ie; | |
1146 | ||
1147 | if (!s->index_built) { | |
1148 | if (is_raw_stream(s)) { | |
1149 | av_build_index_raw(s); | |
1150 | } else { | |
1151 | return -1; | |
1152 | } | |
1153 | s->index_built = 1; | |
1154 | } | |
1155 | ||
fb2758c8 | 1156 | st = s->streams[stream_index]; |
b754978a | 1157 | index = av_index_search_timestamp(st, timestamp); |
fb2758c8 FB |
1158 | if (index < 0) |
1159 | return -1; | |
1160 | ||
1161 | /* now we have found the index, we can seek */ | |
1162 | ie = &st->index_entries[index]; | |
1163 | av_read_frame_flush(s); | |
1164 | url_fseek(&s->pb, ie->pos, SEEK_SET); | |
cdd5034f MN |
1165 | |
1166 | timestamp= av_rescale(ie->timestamp, AV_TIME_BASE*(int64_t)st->time_base.num, st->time_base.den); | |
1167 | for(i = 0; i < s->nb_streams; i++) { | |
1168 | st = s->streams[i]; | |
1169 | ||
77405fc8 | 1170 | st->cur_dts = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE*(int64_t)st->time_base.num); |
cdd5034f MN |
1171 | } |
1172 | ||
fb2758c8 FB |
1173 | return 0; |
1174 | } | |
1175 | ||
1176 | /** | |
1177 | * Seek to the key frame just before the frame at timestamp | |
cdd5034f MN |
1178 | * 'timestamp' in 'stream_index'. |
1179 | * @param stream_index If stream_index is (-1), a default | |
1180 | * stream is selected | |
1181 | * @param timestamp timestamp in AV_TIME_BASE units | |
1182 | * @return >= 0 on success | |
fb2758c8 FB |
1183 | */ |
1184 | int av_seek_frame(AVFormatContext *s, int stream_index, int64_t timestamp) | |
1185 | { | |
1186 | int ret; | |
cdd5034f | 1187 | AVStream *st; |
fb2758c8 FB |
1188 | |
1189 | av_read_frame_flush(s); | |
cdd5034f MN |
1190 | |
1191 | if(stream_index < 0){ | |
1192 | stream_index= av_find_default_stream_index(s); | |
1193 | if(stream_index < 0) | |
1194 | return -1; | |
1195 | } | |
1196 | st= s->streams[stream_index]; | |
1197 | ||
1198 | timestamp = av_rescale(timestamp, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); | |
fb2758c8 FB |
1199 | |
1200 | /* first, we try the format specific seek */ | |
1201 | if (s->iformat->read_seek) | |
1202 | ret = s->iformat->read_seek(s, stream_index, timestamp); | |
1203 | else | |
1204 | ret = -1; | |
1205 | if (ret >= 0) { | |
1206 | return 0; | |
1207 | } | |
8d14a25c MN |
1208 | |
1209 | if(s->iformat->read_timestamp) | |
1210 | return av_seek_frame_binary(s, stream_index, timestamp); | |
1211 | else | |
1212 | return av_seek_frame_generic(s, stream_index, timestamp); | |
fb2758c8 FB |
1213 | } |
1214 | ||
1215 | /*******************************************************/ | |
12f996ed FB |
1216 | |
1217 | /* return TRUE if the stream has accurate timings for at least one component */ | |
1218 | static int av_has_timings(AVFormatContext *ic) | |
1219 | { | |
1220 | int i; | |
1221 | AVStream *st; | |
1222 | ||
1223 | for(i = 0;i < ic->nb_streams; i++) { | |
1224 | st = ic->streams[i]; | |
1225 | if (st->start_time != AV_NOPTS_VALUE && | |
1226 | st->duration != AV_NOPTS_VALUE) | |
1227 | return 1; | |
1228 | } | |
1229 | return 0; | |
1230 | } | |
1231 | ||
1232 | /* estimate the stream timings from the one of each components. Also | |
1233 | compute the global bitrate if possible */ | |
1234 | static void av_update_stream_timings(AVFormatContext *ic) | |
1235 | { | |
1236 | int64_t start_time, end_time, end_time1; | |
1237 | int i; | |
1238 | AVStream *st; | |
1239 | ||
1240 | start_time = MAXINT64; | |
1241 | end_time = MININT64; | |
1242 | for(i = 0;i < ic->nb_streams; i++) { | |
1243 | st = ic->streams[i]; | |
1244 | if (st->start_time != AV_NOPTS_VALUE) { | |
1245 | if (st->start_time < start_time) | |
1246 | start_time = st->start_time; | |
1247 | if (st->duration != AV_NOPTS_VALUE) { | |
1248 | end_time1 = st->start_time + st->duration; | |
1249 | if (end_time1 > end_time) | |
1250 | end_time = end_time1; | |
1251 | } | |
1252 | } | |
1253 | } | |
1254 | if (start_time != MAXINT64) { | |
1255 | ic->start_time = start_time; | |
1256 | if (end_time != MAXINT64) { | |
1257 | ic->duration = end_time - start_time; | |
1258 | if (ic->file_size > 0) { | |
1259 | /* compute the bit rate */ | |
1260 | ic->bit_rate = (double)ic->file_size * 8.0 * AV_TIME_BASE / | |
1261 | (double)ic->duration; | |
1262 | } | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | } | |
1267 | ||
1268 | static void fill_all_stream_timings(AVFormatContext *ic) | |
1269 | { | |
1270 | int i; | |
1271 | AVStream *st; | |
1272 | ||
1273 | av_update_stream_timings(ic); | |
1274 | for(i = 0;i < ic->nb_streams; i++) { | |
1275 | st = ic->streams[i]; | |
1276 | if (st->start_time == AV_NOPTS_VALUE) { | |
1277 | st->start_time = ic->start_time; | |
1278 | st->duration = ic->duration; | |
1279 | } | |
1280 | } | |
1281 | } | |
1282 | ||
1283 | static void av_estimate_timings_from_bit_rate(AVFormatContext *ic) | |
1284 | { | |
1285 | int64_t filesize, duration; | |
1286 | int bit_rate, i; | |
1287 | AVStream *st; | |
1288 | ||
1289 | /* if bit_rate is already set, we believe it */ | |
1290 | if (ic->bit_rate == 0) { | |
1291 | bit_rate = 0; | |
1292 | for(i=0;i<ic->nb_streams;i++) { | |
1293 | st = ic->streams[i]; | |
1294 | bit_rate += st->codec.bit_rate; | |
1295 | } | |
1296 | ic->bit_rate = bit_rate; | |
1297 | } | |
1298 | ||
1299 | /* if duration is already set, we believe it */ | |
1300 | if (ic->duration == AV_NOPTS_VALUE && | |
1301 | ic->bit_rate != 0 && | |
1302 | ic->file_size != 0) { | |
1303 | filesize = ic->file_size; | |
1304 | if (filesize > 0) { | |
1305 | duration = (int64_t)((8 * AV_TIME_BASE * (double)filesize) / (double)ic->bit_rate); | |
1306 | for(i = 0; i < ic->nb_streams; i++) { | |
1307 | st = ic->streams[i]; | |
1308 | if (st->start_time == AV_NOPTS_VALUE || | |
1309 | st->duration == AV_NOPTS_VALUE) { | |
1310 | st->start_time = 0; | |
1311 | st->duration = duration; | |
1312 | } | |
1313 | } | |
1314 | } | |
1315 | } | |
1316 | } | |
1317 | ||
12f996ed FB |
1318 | #define DURATION_MAX_READ_SIZE 250000 |
1319 | ||
1320 | /* only usable for MPEG-PS streams */ | |
1321 | static void av_estimate_timings_from_pts(AVFormatContext *ic) | |
1322 | { | |
1323 | AVPacket pkt1, *pkt = &pkt1; | |
1324 | AVStream *st; | |
1325 | int read_size, i, ret; | |
1326 | int64_t start_time, end_time, end_time1; | |
1327 | int64_t filesize, offset, duration; | |
1328 | ||
fb2758c8 FB |
1329 | /* free previous packet */ |
1330 | if (ic->cur_st && ic->cur_st->parser) | |
1331 | av_free_packet(&ic->cur_pkt); | |
1332 | ic->cur_st = NULL; | |
1333 | ||
1334 | /* flush packet queue */ | |
1335 | flush_packet_queue(ic); | |
fb2758c8 | 1336 | |
0ff7199f MN |
1337 | for(i=0;i<ic->nb_streams;i++) { |
1338 | st = ic->streams[i]; | |
1339 | if (st->parser) { | |
1340 | av_parser_close(st->parser); | |
1341 | st->parser= NULL; | |
1342 | } | |
1343 | } | |
1344 | ||
12f996ed FB |
1345 | /* we read the first packets to get the first PTS (not fully |
1346 | accurate, but it is enough now) */ | |
1347 | url_fseek(&ic->pb, 0, SEEK_SET); | |
1348 | read_size = 0; | |
1349 | for(;;) { | |
1350 | if (read_size >= DURATION_MAX_READ_SIZE) | |
1351 | break; | |
1352 | /* if all info is available, we can stop */ | |
1353 | for(i = 0;i < ic->nb_streams; i++) { | |
1354 | st = ic->streams[i]; | |
1355 | if (st->start_time == AV_NOPTS_VALUE) | |
1356 | break; | |
1357 | } | |
1358 | if (i == ic->nb_streams) | |
1359 | break; | |
1360 | ||
1361 | ret = av_read_packet(ic, pkt); | |
1362 | if (ret != 0) | |
1363 | break; | |
1364 | read_size += pkt->size; | |
1365 | st = ic->streams[pkt->stream_index]; | |
1366 | if (pkt->pts != AV_NOPTS_VALUE) { | |
1367 | if (st->start_time == AV_NOPTS_VALUE) | |
9ee91c2f | 1368 | st->start_time = av_rescale(pkt->pts, st->time_base.num * (int64_t)AV_TIME_BASE, st->time_base.den); |
0a5f92a1 MN |
1369 | } |
1370 | av_free_packet(pkt); | |
1371 | } | |
12f996ed FB |
1372 | |
1373 | /* we compute the minimum start_time and use it as default */ | |
1374 | start_time = MAXINT64; | |
1375 | for(i = 0; i < ic->nb_streams; i++) { | |
1376 | st = ic->streams[i]; | |
1377 | if (st->start_time != AV_NOPTS_VALUE && | |
1378 | st->start_time < start_time) | |
1379 | start_time = st->start_time; | |
1380 | } | |
12f996ed FB |
1381 | if (start_time != MAXINT64) |
1382 | ic->start_time = start_time; | |
1383 | ||
1384 | /* estimate the end time (duration) */ | |
1385 | /* XXX: may need to support wrapping */ | |
1386 | filesize = ic->file_size; | |
1387 | offset = filesize - DURATION_MAX_READ_SIZE; | |
1388 | if (offset < 0) | |
1389 | offset = 0; | |
1390 | ||
12f996ed FB |
1391 | url_fseek(&ic->pb, offset, SEEK_SET); |
1392 | read_size = 0; | |
1393 | for(;;) { | |
1394 | if (read_size >= DURATION_MAX_READ_SIZE) | |
1395 | break; | |
1396 | /* if all info is available, we can stop */ | |
1397 | for(i = 0;i < ic->nb_streams; i++) { | |
1398 | st = ic->streams[i]; | |
1399 | if (st->duration == AV_NOPTS_VALUE) | |
1400 | break; | |
1401 | } | |
1402 | if (i == ic->nb_streams) | |
1403 | break; | |
1404 | ||
1405 | ret = av_read_packet(ic, pkt); | |
1406 | if (ret != 0) | |
1407 | break; | |
1408 | read_size += pkt->size; | |
1409 | st = ic->streams[pkt->stream_index]; | |
1410 | if (pkt->pts != AV_NOPTS_VALUE) { | |
9ee91c2f | 1411 | end_time = av_rescale(pkt->pts, st->time_base.num * (int64_t)AV_TIME_BASE, st->time_base.den); |
12f996ed FB |
1412 | duration = end_time - st->start_time; |
1413 | if (duration > 0) { | |
1414 | if (st->duration == AV_NOPTS_VALUE || | |
1415 | st->duration < duration) | |
1416 | st->duration = duration; | |
1417 | } | |
1418 | } | |
1419 | av_free_packet(pkt); | |
1420 | } | |
1421 | ||
1422 | /* estimate total duration */ | |
1423 | end_time = MININT64; | |
1424 | for(i = 0;i < ic->nb_streams; i++) { | |
1425 | st = ic->streams[i]; | |
1426 | if (st->duration != AV_NOPTS_VALUE) { | |
1427 | end_time1 = st->start_time + st->duration; | |
1428 | if (end_time1 > end_time) | |
1429 | end_time = end_time1; | |
1430 | } | |
1431 | } | |
1432 | ||
1433 | /* update start_time (new stream may have been created, so we do | |
1434 | it at the end */ | |
1435 | if (ic->start_time != AV_NOPTS_VALUE) { | |
1436 | for(i = 0; i < ic->nb_streams; i++) { | |
1437 | st = ic->streams[i]; | |
1438 | if (st->start_time == AV_NOPTS_VALUE) | |
1439 | st->start_time = ic->start_time; | |
1440 | } | |
1441 | } | |
1442 | ||
1443 | if (end_time != MININT64) { | |
1444 | /* put dummy values for duration if needed */ | |
1445 | for(i = 0;i < ic->nb_streams; i++) { | |
1446 | st = ic->streams[i]; | |
1447 | if (st->duration == AV_NOPTS_VALUE && | |
1448 | st->start_time != AV_NOPTS_VALUE) | |
1449 | st->duration = end_time - st->start_time; | |
1450 | } | |
1451 | ic->duration = end_time - ic->start_time; | |
1452 | } | |
1453 | ||
1454 | url_fseek(&ic->pb, 0, SEEK_SET); | |
1455 | } | |
1456 | ||
1457 | static void av_estimate_timings(AVFormatContext *ic) | |
1458 | { | |
1459 | URLContext *h; | |
1460 | int64_t file_size; | |
1461 | ||
1462 | /* get the file size, if possible */ | |
1463 | if (ic->iformat->flags & AVFMT_NOFILE) { | |
1464 | file_size = 0; | |
1465 | } else { | |
1466 | h = url_fileno(&ic->pb); | |
1467 | file_size = url_filesize(h); | |
1468 | if (file_size < 0) | |
1469 | file_size = 0; | |
1470 | } | |
1471 | ic->file_size = file_size; | |
1472 | ||
1473 | if (ic->iformat == &mpegps_demux) { | |
1474 | /* get accurate estimate from the PTSes */ | |
1475 | av_estimate_timings_from_pts(ic); | |
1476 | } else if (av_has_timings(ic)) { | |
1477 | /* at least one components has timings - we use them for all | |
1478 | the components */ | |
1479 | fill_all_stream_timings(ic); | |
1480 | } else { | |
1481 | /* less precise: use bit rate info */ | |
1482 | av_estimate_timings_from_bit_rate(ic); | |
1483 | } | |
1484 | av_update_stream_timings(ic); | |
1485 | ||
1486 | #if 0 | |
1487 | { | |
1488 | int i; | |
1489 | AVStream *st; | |
1490 | for(i = 0;i < ic->nb_streams; i++) { | |
1491 | st = ic->streams[i]; | |
1492 | printf("%d: start_time: %0.3f duration: %0.3f\n", | |
1493 | i, (double)st->start_time / AV_TIME_BASE, | |
1494 | (double)st->duration / AV_TIME_BASE); | |
1495 | } | |
1496 | printf("stream: start_time: %0.3f duration: %0.3f bitrate=%d kb/s\n", | |
1497 | (double)ic->start_time / AV_TIME_BASE, | |
1498 | (double)ic->duration / AV_TIME_BASE, | |
1499 | ic->bit_rate / 1000); | |
1500 | } | |
1501 | #endif | |
1502 | } | |
1503 | ||
b9a281db FB |
1504 | static int has_codec_parameters(AVCodecContext *enc) |
1505 | { | |
1506 | int val; | |
1507 | switch(enc->codec_type) { | |
1508 | case CODEC_TYPE_AUDIO: | |
1509 | val = enc->sample_rate; | |
1510 | break; | |
1511 | case CODEC_TYPE_VIDEO: | |
1512 | val = enc->width; | |
1513 | break; | |
1514 | default: | |
1515 | val = 1; | |
1516 | break; | |
1517 | } | |
1518 | return (val != 0); | |
1519 | } | |
1520 | ||
fb2758c8 FB |
1521 | static int try_decode_frame(AVStream *st, const uint8_t *data, int size) |
1522 | { | |
1523 | int16_t *samples; | |
1524 | AVCodec *codec; | |
1525 | int got_picture, ret; | |
1526 | AVFrame picture; | |
1527 | ||
1528 | codec = avcodec_find_decoder(st->codec.codec_id); | |
1529 | if (!codec) | |
1530 | return -1; | |
1531 | ret = avcodec_open(&st->codec, codec); | |
1532 | if (ret < 0) | |
1533 | return ret; | |
1534 | switch(st->codec.codec_type) { | |
1535 | case CODEC_TYPE_VIDEO: | |
1536 | ret = avcodec_decode_video(&st->codec, &picture, | |
1537 | &got_picture, (uint8_t *)data, size); | |
1538 | break; | |
1539 | case CODEC_TYPE_AUDIO: | |
1540 | samples = av_malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); | |
1541 | if (!samples) | |
1542 | goto fail; | |
1543 | ret = avcodec_decode_audio(&st->codec, samples, | |
1544 | &got_picture, (uint8_t *)data, size); | |
1545 | av_free(samples); | |
1546 | break; | |
1547 | default: | |
1548 | break; | |
1549 | } | |
1550 | fail: | |
1551 | avcodec_close(&st->codec); | |
1552 | return ret; | |
1553 | } | |
1554 | ||
1555 | /* absolute maximum size we read until we abort */ | |
1556 | #define MAX_READ_SIZE 5000000 | |
1557 | ||
1558 | /* maximum duration until we stop analysing the stream */ | |
1559 | #define MAX_STREAM_DURATION ((int)(AV_TIME_BASE * 1.0)) | |
1560 | ||
b9a281db FB |
1561 | /** |
1562 | * Read the beginning of a media file to get stream information. This | |
1563 | * is useful for file formats with no headers such as MPEG. This | |
1564 | * function also compute the real frame rate in case of mpeg2 repeat | |
1565 | * frame mode. | |
1566 | * | |
1567 | * @param ic media file handle | |
1568 | * @return >=0 if OK. AVERROR_xxx if error. | |
1569 | */ | |
1570 | int av_find_stream_info(AVFormatContext *ic) | |
1571 | { | |
fb2758c8 | 1572 | int i, count, ret, read_size; |
b9a281db | 1573 | AVStream *st; |
fb2758c8 | 1574 | AVPacket pkt1, *pkt; |
b9a281db | 1575 | AVPacketList *pktl=NULL, **ppktl; |
b9a281db FB |
1576 | |
1577 | count = 0; | |
1578 | read_size = 0; | |
1579 | ppktl = &ic->packet_buffer; | |
1580 | for(;;) { | |
1581 | /* check if one codec still needs to be handled */ | |
1582 | for(i=0;i<ic->nb_streams;i++) { | |
1583 | st = ic->streams[i]; | |
fb2758c8 | 1584 | if (!has_codec_parameters(&st->codec)) |
b9a281db FB |
1585 | break; |
1586 | } | |
1587 | if (i == ic->nb_streams) { | |
1588 | /* NOTE: if the format has no header, then we need to read | |
1589 | some packets to get most of the streams, so we cannot | |
1590 | stop here */ | |
fb2758c8 | 1591 | if (!(ic->ctx_flags & AVFMTCTX_NOHEADER)) { |
b9a281db FB |
1592 | /* if we found the info for all the codecs, we can stop */ |
1593 | ret = count; | |
1594 | break; | |
1595 | } | |
1596 | } else { | |
1597 | /* we did not get all the codec info, but we read too much data */ | |
fb2758c8 | 1598 | if (read_size >= MAX_READ_SIZE) { |
b9a281db FB |
1599 | ret = count; |
1600 | break; | |
1601 | } | |
1602 | } | |
1603 | ||
fb2758c8 FB |
1604 | /* NOTE: a new stream can be added there if no header in file |
1605 | (AVFMTCTX_NOHEADER) */ | |
1606 | ret = av_read_frame_internal(ic, &pkt1); | |
1607 | if (ret < 0) { | |
1608 | /* EOF or error */ | |
1609 | ret = -1; /* we could not have all the codec parameters before EOF */ | |
1610 | if ((ic->ctx_flags & AVFMTCTX_NOHEADER) && | |
1611 | i == ic->nb_streams) | |
1612 | ret = 0; | |
1613 | break; | |
1614 | } | |
1615 | ||
b9a281db FB |
1616 | pktl = av_mallocz(sizeof(AVPacketList)); |
1617 | if (!pktl) { | |
1618 | ret = AVERROR_NOMEM; | |
1619 | break; | |
1620 | } | |
1621 | ||
1622 | /* add the packet in the buffered packet list */ | |
1623 | *ppktl = pktl; | |
1624 | ppktl = &pktl->next; | |
1625 | ||
b9a281db | 1626 | pkt = &pktl->pkt; |
fb2758c8 FB |
1627 | *pkt = pkt1; |
1628 | ||
1629 | /* duplicate the packet */ | |
1630 | if (av_dup_packet(pkt) < 0) { | |
1631 | ret = AVERROR_NOMEM; | |
1632 | break; | |
b9a281db | 1633 | } |
b9a281db | 1634 | |
fb2758c8 | 1635 | read_size += pkt->size; |
b9a281db FB |
1636 | |
1637 | st = ic->streams[pkt->stream_index]; | |
fb2758c8 FB |
1638 | st->codec_info_duration += pkt->duration; |
1639 | if (pkt->duration != 0) | |
1640 | st->codec_info_nb_frames++; | |
1641 | ||
1642 | /* if still no information, we try to open the codec and to | |
1643 | decompress the frame. We try to avoid that in most cases as | |
1644 | it takes longer and uses more memory. For MPEG4, we need to | |
1645 | decompress for Quicktime. */ | |
1646 | if (!has_codec_parameters(&st->codec) && | |
1647 | (st->codec.codec_id == CODEC_ID_FLV1 || | |
1648 | st->codec.codec_id == CODEC_ID_H264 || | |
1649 | st->codec.codec_id == CODEC_ID_H263 || | |
8bfed902 | 1650 | st->codec.codec_id == CODEC_ID_VORBIS || |
03cfe134 | 1651 | st->codec.codec_id == CODEC_ID_MJPEG || |
fb2758c8 FB |
1652 | (st->codec.codec_id == CODEC_ID_MPEG4 && !st->need_parsing))) |
1653 | try_decode_frame(st, pkt->data, pkt->size); | |
1654 | ||
1655 | if (st->codec_info_duration >= MAX_STREAM_DURATION) { | |
1656 | break; | |
b9a281db FB |
1657 | } |
1658 | count++; | |
1659 | } | |
1660 | ||
b9a281db FB |
1661 | /* set real frame rate info */ |
1662 | for(i=0;i<ic->nb_streams;i++) { | |
1663 | st = ic->streams[i]; | |
1664 | if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
fb2758c8 FB |
1665 | /* compute the real frame rate for telecine */ |
1666 | if ((st->codec.codec_id == CODEC_ID_MPEG1VIDEO || | |
1667 | st->codec.codec_id == CODEC_ID_MPEG2VIDEO) && | |
1668 | st->codec.sub_id == 2) { | |
1669 | if (st->codec_info_nb_frames >= 20) { | |
1670 | float coded_frame_rate, est_frame_rate; | |
1671 | est_frame_rate = ((double)st->codec_info_nb_frames * AV_TIME_BASE) / | |
1672 | (double)st->codec_info_duration ; | |
1673 | coded_frame_rate = (double)st->codec.frame_rate / | |
1674 | (double)st->codec.frame_rate_base; | |
1675 | #if 0 | |
1676 | printf("telecine: coded_frame_rate=%0.3f est_frame_rate=%0.3f\n", | |
1677 | coded_frame_rate, est_frame_rate); | |
1678 | #endif | |
1679 | /* if we detect that it could be a telecine, we | |
1680 | signal it. It would be better to do it at a | |
1681 | higher level as it can change in a film */ | |
1682 | if (coded_frame_rate >= 24.97 && | |
1683 | (est_frame_rate >= 23.5 && est_frame_rate < 24.5)) { | |
1684 | st->r_frame_rate = 24024; | |
1685 | st->r_frame_rate_base = 1001; | |
1686 | } | |
1687 | } | |
1688 | } | |
1689 | /* if no real frame rate, use the codec one */ | |
14bea432 MN |
1690 | if (!st->r_frame_rate){ |
1691 | st->r_frame_rate = st->codec.frame_rate; | |
1692 | st->r_frame_rate_base = st->codec.frame_rate_base; | |
1693 | } | |
b9a281db | 1694 | } |
de6d9b64 | 1695 | } |
b9a281db | 1696 | |
12f996ed | 1697 | av_estimate_timings(ic); |
e928649b MN |
1698 | #if 0 |
1699 | /* correct DTS for b frame streams with no timestamps */ | |
1700 | for(i=0;i<ic->nb_streams;i++) { | |
1701 | st = ic->streams[i]; | |
1702 | if (st->codec.codec_type == CODEC_TYPE_VIDEO) { | |
1703 | if(b-frames){ | |
1704 | ppktl = &ic->packet_buffer; | |
1705 | while(ppkt1){ | |
1706 | if(ppkt1->stream_index != i) | |
1707 | continue; | |
1708 | if(ppkt1->pkt->dts < 0) | |
1709 | break; | |
1710 | if(ppkt1->pkt->pts != AV_NOPTS_VALUE) | |
1711 | break; | |
1712 | ppkt1->pkt->dts -= delta; | |
1713 | ppkt1= ppkt1->next; | |
1714 | } | |
1715 | if(ppkt1) | |
1716 | continue; | |
1717 | st->cur_dts -= delta; | |
1718 | } | |
1719 | } | |
1720 | } | |
1721 | #endif | |
b9a281db | 1722 | return ret; |
de6d9b64 FB |
1723 | } |
1724 | ||
fb2758c8 FB |
1725 | /*******************************************************/ |
1726 | ||
1727 | /** | |
1728 | * start playing a network based stream (e.g. RTSP stream) at the | |
1729 | * current position | |
1730 | */ | |
1731 | int av_read_play(AVFormatContext *s) | |
1732 | { | |
1733 | if (!s->iformat->read_play) | |
1734 | return AVERROR_NOTSUPP; | |
1735 | return s->iformat->read_play(s); | |
1736 | } | |
1737 | ||
1738 | /** | |
1739 | * pause a network based stream (e.g. RTSP stream). Use av_read_play() | |
1740 | * to resume it. | |
1741 | */ | |
1742 | int av_read_pause(AVFormatContext *s) | |
1743 | { | |
1744 | if (!s->iformat->read_pause) | |
1745 | return AVERROR_NOTSUPP; | |
1746 | return s->iformat->read_pause(s); | |
1747 | } | |
1748 | ||
b9a281db FB |
1749 | /** |
1750 | * Close a media file (but not its codecs) | |
1751 | * | |
1752 | * @param s media file handle | |
1753 | */ | |
de6d9b64 FB |
1754 | void av_close_input_file(AVFormatContext *s) |
1755 | { | |
b6892136 | 1756 | int i, must_open_file; |
da24c5e3 | 1757 | AVStream *st; |
de6d9b64 | 1758 | |
fb2758c8 FB |
1759 | /* free previous packet */ |
1760 | if (s->cur_st && s->cur_st->parser) | |
1761 | av_free_packet(&s->cur_pkt); | |
1762 | ||
b9a281db FB |
1763 | if (s->iformat->read_close) |
1764 | s->iformat->read_close(s); | |
de6d9b64 | 1765 | for(i=0;i<s->nb_streams;i++) { |
da24c5e3 FB |
1766 | /* free all data in a stream component */ |
1767 | st = s->streams[i]; | |
fb2758c8 FB |
1768 | if (st->parser) { |
1769 | av_parser_close(st->parser); | |
de6d9b64 | 1770 | } |
fb2758c8 FB |
1771 | av_free(st->index_entries); |
1772 | av_free(st); | |
de6d9b64 | 1773 | } |
fb2758c8 | 1774 | flush_packet_queue(s); |
b6892136 | 1775 | must_open_file = 1; |
da24c5e3 | 1776 | if (s->iformat->flags & AVFMT_NOFILE) { |
b6892136 FB |
1777 | must_open_file = 0; |
1778 | } | |
1779 | if (must_open_file) { | |
96baaa6a FB |
1780 | url_fclose(&s->pb); |
1781 | } | |
a8dbe951 | 1782 | av_freep(&s->priv_data); |
1ea4f593 | 1783 | av_free(s); |
de6d9b64 FB |
1784 | } |
1785 | ||
b9a281db FB |
1786 | /** |
1787 | * Add a new stream to a media file. Can only be called in the | |
da24c5e3 FB |
1788 | * read_header function. If the flag AVFMTCTX_NOHEADER is in the |
1789 | * format context, then new streams can be added in read_packet too. | |
b9a281db FB |
1790 | * |
1791 | * | |
1792 | * @param s media file handle | |
da24c5e3 | 1793 | * @param id file format dependent stream id |
b9a281db FB |
1794 | */ |
1795 | AVStream *av_new_stream(AVFormatContext *s, int id) | |
1796 | { | |
1797 | AVStream *st; | |
1798 | ||
1799 | if (s->nb_streams >= MAX_STREAMS) | |
1800 | return NULL; | |
1801 | ||
1802 | st = av_mallocz(sizeof(AVStream)); | |
1803 | if (!st) | |
1804 | return NULL; | |
1e491e29 | 1805 | avcodec_get_context_defaults(&st->codec); |
48091512 FB |
1806 | if (s->iformat) { |
1807 | /* no default bitrate if decoding */ | |
1808 | st->codec.bit_rate = 0; | |
1809 | } | |
b9a281db FB |
1810 | st->index = s->nb_streams; |
1811 | st->id = id; | |
12f996ed FB |
1812 | st->start_time = AV_NOPTS_VALUE; |
1813 | st->duration = AV_NOPTS_VALUE; | |
e928649b | 1814 | st->cur_dts = AV_NOPTS_VALUE; |
9ee91c2f MN |
1815 | |
1816 | /* default pts settings is MPEG like */ | |
1817 | av_set_pts_info(st, 33, 1, 90000); | |
77405fc8 | 1818 | st->last_IP_pts = AV_NOPTS_VALUE; |
9ee91c2f | 1819 | |
b9a281db FB |
1820 | s->streams[s->nb_streams++] = st; |
1821 | return st; | |
1822 | } | |
1823 | ||
1824 | /************************************************************/ | |
1825 | /* output media file */ | |
de6d9b64 | 1826 | |
87a0a681 FB |
1827 | int av_set_parameters(AVFormatContext *s, AVFormatParameters *ap) |
1828 | { | |
1829 | int ret; | |
98486a6b RS |
1830 | |
1831 | if (s->oformat->priv_data_size > 0) { | |
1832 | s->priv_data = av_mallocz(s->oformat->priv_data_size); | |
1833 | if (!s->priv_data) | |
1834 | return AVERROR_NOMEM; | |
1835 | } else | |
1836 | s->priv_data = NULL; | |
1837 | ||
87a0a681 FB |
1838 | if (s->oformat->set_parameters) { |
1839 | ret = s->oformat->set_parameters(s, ap); | |
1840 | if (ret < 0) | |
1841 | return ret; | |
1842 | } | |
1843 | return 0; | |
1844 | } | |
1845 | ||
b9a281db FB |
1846 | /** |
1847 | * allocate the stream private data and write the stream header to an | |
1848 | * output media file | |
1849 | * | |
1850 | * @param s media file handle | |
1851 | * @return 0 if OK. AVERROR_xxx if error. | |
1852 | */ | |
1853 | int av_write_header(AVFormatContext *s) | |
1854 | { | |
1e51d801 FB |
1855 | int ret, i; |
1856 | AVStream *st; | |
1857 | ||
1e51d801 FB |
1858 | ret = s->oformat->write_header(s); |
1859 | if (ret < 0) | |
1860 | return ret; | |
1861 | ||
1862 | /* init PTS generation */ | |
1863 | for(i=0;i<s->nb_streams;i++) { | |
1864 | st = s->streams[i]; | |
1865 | ||
1866 | switch (st->codec.codec_type) { | |
1867 | case CODEC_TYPE_AUDIO: | |
1868 | av_frac_init(&st->pts, 0, 0, | |
9ee91c2f | 1869 | (int64_t)st->time_base.num * st->codec.sample_rate); |
1e51d801 FB |
1870 | break; |
1871 | case CODEC_TYPE_VIDEO: | |
1872 | av_frac_init(&st->pts, 0, 0, | |
9ee91c2f | 1873 | (int64_t)st->time_base.num * st->codec.frame_rate); |
1e51d801 FB |
1874 | break; |
1875 | default: | |
1876 | break; | |
1877 | } | |
1878 | } | |
1879 | return 0; | |
b9a281db FB |
1880 | } |
1881 | ||
3c895fc0 MN |
1882 | //FIXME merge with compute_pkt_fields |
1883 | static void compute_pkt_fields2(AVStream *st, AVPacket *pkt){ | |
1884 | int b_frames = FFMAX(st->codec.has_b_frames, st->codec.max_b_frames); | |
1885 | int num, den, frame_size; | |
b0c7f5a9 | 1886 | |
3c895fc0 | 1887 | // av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts:%lld dts:%lld cur_dts:%lld b:%d size:%d st:%d\n", pkt->pts, pkt->dts, st->cur_dts, b_frames, pkt->size, pkt->stream_index); |
e928649b MN |
1888 | |
1889 | /* if(pkt->pts == AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE) | |
1890 | return -1;*/ | |
1891 | ||
1892 | if(pkt->pts != AV_NOPTS_VALUE) | |
1893 | pkt->pts = av_rescale(pkt->pts, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); | |
1894 | if(pkt->dts != AV_NOPTS_VALUE) | |
1895 | pkt->dts = av_rescale(pkt->dts, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); | |
1896 | ||
1897 | /* duration field */ | |
1898 | pkt->duration = av_rescale(pkt->duration, st->time_base.den, AV_TIME_BASE * (int64_t)st->time_base.num); | |
3c895fc0 MN |
1899 | if (pkt->duration == 0) { |
1900 | compute_frame_duration(&num, &den, st, NULL, pkt); | |
1901 | if (den && num) { | |
1902 | pkt->duration = av_rescale(1, num * (int64_t)st->time_base.den, den * (int64_t)st->time_base.num); | |
1903 | } | |
1904 | } | |
e928649b MN |
1905 | |
1906 | //XXX/FIXME this is a temporary hack until all encoders output pts | |
1907 | if((pkt->pts == 0 || pkt->pts == AV_NOPTS_VALUE) && pkt->dts == AV_NOPTS_VALUE && !b_frames){ | |
1908 | pkt->dts= | |
1909 | // pkt->pts= st->cur_dts; | |
1910 | pkt->pts= st->pts.val; | |
1911 | } | |
1912 | ||
1913 | //calculate dts from pts | |
1914 | if(pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE){ | |
1915 | if(b_frames){ | |
1916 | if(st->last_IP_pts == AV_NOPTS_VALUE){ | |
3c895fc0 | 1917 | st->last_IP_pts= -pkt->duration; |
e928649b MN |
1918 | } |
1919 | if(st->last_IP_pts < pkt->pts){ | |
1920 | pkt->dts= st->last_IP_pts; | |
1921 | st->last_IP_pts= pkt->pts; | |
1922 | }else | |
1923 | pkt->dts= pkt->pts; | |
1924 | }else | |
1925 | pkt->dts= pkt->pts; | |
1926 | } | |
1927 | ||
3c895fc0 | 1928 | // av_log(NULL, AV_LOG_DEBUG, "av_write_frame: pts2:%lld dts2:%lld\n", pkt->pts, pkt->dts); |
e928649b MN |
1929 | st->cur_dts= pkt->dts; |
1930 | st->pts.val= pkt->dts; | |
1931 | ||
1e51d801 FB |
1932 | /* update pts */ |
1933 | switch (st->codec.codec_type) { | |
1934 | case CODEC_TYPE_AUDIO: | |
e928649b | 1935 | frame_size = get_audio_frame_size(&st->codec, pkt->size); |
6d8f985e | 1936 | |
b0c7f5a9 | 1937 | /* HACK/FIXME, we skip the initial 0-size packets as they are most likely equal to the encoder delay, |
6d8f985e | 1938 | but it would be better if we had the real timestamps from the encoder */ |
e928649b | 1939 | if (frame_size >= 0 && (pkt->size || st->pts.num!=st->pts.den>>1 || st->pts.val)) { |
9ee91c2f | 1940 | av_frac_add(&st->pts, (int64_t)st->time_base.den * frame_size); |
7feb950a | 1941 | } |
1e51d801 FB |
1942 | break; |
1943 | case CODEC_TYPE_VIDEO: | |
9ee91c2f | 1944 | av_frac_add(&st->pts, (int64_t)st->time_base.den * st->codec.frame_rate_base); |
1e51d801 FB |
1945 | break; |
1946 | default: | |
1947 | break; | |
1948 | } | |
3c895fc0 MN |
1949 | } |
1950 | ||
1951 | static void truncate_ts(AVStream *st, AVPacket *pkt){ | |
1952 | int64_t pts_mask = (2LL << (st->pts_wrap_bits-1)) - 1; | |
1953 | ||
1954 | if(pkt->dts < 0) | |
1955 | pkt->dts= 0; //this happens for low_delay=0 and b frames, FIXME, needs further invstigation about what we should do here | |
1956 | ||
1957 | pkt->pts &= pts_mask; | |
1958 | pkt->dts &= pts_mask; | |
1959 | } | |
1960 | ||
1961 | /** | |
1962 | * Write a packet to an output media file. The packet shall contain | |
1963 | * one audio or video frame. | |
1964 | * | |
1965 | * @param s media file handle | |
1966 | * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ... | |
1967 | * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
1968 | */ | |
1969 | int av_write_frame(AVFormatContext *s, AVPacket *pkt) | |
1970 | { | |
1971 | compute_pkt_fields2(s->streams[pkt->stream_index], pkt); | |
1972 | ||
1973 | truncate_ts(s->streams[pkt->stream_index], pkt); | |
1974 | ||
1975 | return s->oformat->write_packet(s, pkt); | |
1976 | } | |
1977 | ||
1978 | /** | |
1979 | * Writes a packet to an output media file ensuring correct interleaving. | |
1980 | * The packet shall contain one audio or video frame. | |
1981 | * If the packets are already correctly interleaved the application should | |
1982 | * call av_write_frame() instead as its slightly faster, its also important | |
1983 | * to keep in mind that non interlaved input will need huge amounts | |
1984 | * of memory to interleave with this, so its prefereable to interleave at the | |
1985 | * demuxer level | |
1986 | * | |
1987 | * @param s media file handle | |
1988 | * @param pkt the packet, which contains the stream_index, buf/buf_size, dts/pts, ... | |
1989 | * @return < 0 if error, = 0 if OK, 1 if end of stream wanted. | |
1990 | */ | |
1991 | int av_interleaved_write_frame(AVFormatContext *s, AVPacket *pkt){ | |
1992 | AVPacketList *pktl, **next_point, *this_pktl; | |
1993 | int stream_count=0; | |
1994 | int streams[MAX_STREAMS]; | |
1995 | AVStream *st= s->streams[ pkt->stream_index]; | |
1996 | ||
1997 | compute_pkt_fields2(st, pkt); | |
6f824977 MN |
1998 | |
1999 | //FIXME/XXX/HACK drop zero sized packets | |
2000 | if(st->codec.codec_type == CODEC_TYPE_AUDIO && pkt->size==0) | |
2001 | return 0; | |
3c895fc0 MN |
2002 | |
2003 | if(pkt->dts == AV_NOPTS_VALUE) | |
2004 | return -1; | |
2005 | ||
2006 | assert(pkt->destruct != av_destruct_packet); //FIXME | |
2007 | ||
2008 | this_pktl = av_mallocz(sizeof(AVPacketList)); | |
2009 | this_pktl->pkt= *pkt; | |
2010 | av_dup_packet(&this_pktl->pkt); | |
2011 | ||
2012 | next_point = &s->packet_buffer; | |
2013 | while(*next_point){ | |
2014 | AVStream *st2= s->streams[ (*next_point)->pkt.stream_index]; | |
cf7eef66 MN |
2015 | int64_t left= st2->time_base.num * (int64_t)st ->time_base.den; |
2016 | int64_t right= st ->time_base.num * (int64_t)st2->time_base.den; | |
3c895fc0 MN |
2017 | if((*next_point)->pkt.dts * left > pkt->dts * right) //FIXME this can overflow |
2018 | break; | |
2019 | next_point= &(*next_point)->next; | |
2020 | } | |
2021 | this_pktl->next= *next_point; | |
2022 | *next_point= this_pktl; | |
2023 | ||
2024 | memset(streams, 0, sizeof(streams)); | |
2025 | pktl= s->packet_buffer; | |
2026 | while(pktl){ | |
2027 | //av_log(s, AV_LOG_DEBUG, "show st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts); | |
2028 | if(streams[ pktl->pkt.stream_index ] == 0) | |
2029 | stream_count++; | |
2030 | streams[ pktl->pkt.stream_index ]++; | |
2031 | pktl= pktl->next; | |
2032 | } | |
2033 | ||
2034 | while(s->nb_streams == stream_count){ | |
2035 | int ret; | |
2036 | ||
2037 | pktl= s->packet_buffer; | |
2038 | //av_log(s, AV_LOG_DEBUG, "write st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts); | |
2039 | truncate_ts(s->streams[pktl->pkt.stream_index], &pktl->pkt); | |
2040 | ret= s->oformat->write_packet(s, &pktl->pkt); | |
2041 | ||
2042 | s->packet_buffer= pktl->next; | |
2043 | if((--streams[ pktl->pkt.stream_index ]) == 0) | |
2044 | stream_count--; | |
2045 | ||
2046 | av_free_packet(&pktl->pkt); | |
2047 | av_freep(&pktl); | |
2048 | ||
2049 | if(ret<0) | |
2050 | return ret; | |
2051 | } | |
2052 | return 0; | |
b9a281db FB |
2053 | } |
2054 | ||
2055 | /** | |
2056 | * write the stream trailer to an output media file and and free the | |
2057 | * file private data. | |
2058 | * | |
2059 | * @param s media file handle | |
2060 | * @return 0 if OK. AVERROR_xxx if error. */ | |
2061 | int av_write_trailer(AVFormatContext *s) | |
2062 | { | |
2063 | int ret; | |
3c895fc0 MN |
2064 | |
2065 | while(s->packet_buffer){ | |
2066 | int ret; | |
2067 | AVPacketList *pktl= s->packet_buffer; | |
2068 | ||
2069 | //av_log(s, AV_LOG_DEBUG, "write_trailer st:%d dts:%lld\n", pktl->pkt.stream_index, pktl->pkt.dts); | |
2070 | truncate_ts(s->streams[pktl->pkt.stream_index], &pktl->pkt); | |
2071 | ret= s->oformat->write_packet(s, &pktl->pkt); | |
2072 | ||
2073 | s->packet_buffer= pktl->next; | |
2074 | ||
2075 | av_free_packet(&pktl->pkt); | |
2076 | av_freep(&pktl); | |
2077 | ||
2078 | if(ret<0) | |
2079 | return ret; | |
2080 | } | |
2081 | ||
b9a281db FB |
2082 | ret = s->oformat->write_trailer(s); |
2083 | av_freep(&s->priv_data); | |
2084 | return ret; | |
de6d9b64 FB |
2085 | } |
2086 | ||
2087 | /* "user interface" functions */ | |
2088 | ||
2089 | void dump_format(AVFormatContext *ic, | |
2090 | int index, | |
2091 | const char *url, | |
2092 | int is_output) | |
2093 | { | |
b9a281db | 2094 | int i, flags; |
de6d9b64 FB |
2095 | char buf[256]; |
2096 | ||
43465395 | 2097 | av_log(NULL, AV_LOG_DEBUG, "%s #%d, %s, %s '%s':\n", |
de6d9b64 | 2098 | is_output ? "Output" : "Input", |
b9a281db FB |
2099 | index, |
2100 | is_output ? ic->oformat->name : ic->iformat->name, | |
de6d9b64 | 2101 | is_output ? "to" : "from", url); |
12f996ed | 2102 | if (!is_output) { |
43465395 | 2103 | av_log(NULL, AV_LOG_DEBUG, " Duration: "); |
12f996ed FB |
2104 | if (ic->duration != AV_NOPTS_VALUE) { |
2105 | int hours, mins, secs, us; | |
2106 | secs = ic->duration / AV_TIME_BASE; | |
2107 | us = ic->duration % AV_TIME_BASE; | |
2108 | mins = secs / 60; | |
2109 | secs %= 60; | |
2110 | hours = mins / 60; | |
2111 | mins %= 60; | |
43465395 | 2112 | av_log(NULL, AV_LOG_DEBUG, "%02d:%02d:%02d.%01d", hours, mins, secs, |
12f996ed FB |
2113 | (10 * us) / AV_TIME_BASE); |
2114 | } else { | |
43465395 | 2115 | av_log(NULL, AV_LOG_DEBUG, "N/A"); |
12f996ed | 2116 | } |
43465395 | 2117 | av_log(NULL, AV_LOG_DEBUG, ", bitrate: "); |
12f996ed | 2118 | if (ic->bit_rate) { |
43465395 | 2119 | av_log(NULL, AV_LOG_DEBUG,"%d kb/s", ic->bit_rate / 1000); |
12f996ed | 2120 | } else { |
43465395 | 2121 | av_log(NULL, AV_LOG_DEBUG, "N/A"); |
12f996ed | 2122 | } |
43465395 | 2123 | av_log(NULL, AV_LOG_DEBUG, "\n"); |
12f996ed | 2124 | } |
de6d9b64 FB |
2125 | for(i=0;i<ic->nb_streams;i++) { |
2126 | AVStream *st = ic->streams[i]; | |
2127 | avcodec_string(buf, sizeof(buf), &st->codec, is_output); | |
43465395 | 2128 | av_log(NULL, AV_LOG_DEBUG, " Stream #%d.%d", index, i); |
b9a281db FB |
2129 | /* the pid is an important information, so we display it */ |
2130 | /* XXX: add a generic system */ | |
2131 | if (is_output) | |
2132 | flags = ic->oformat->flags; | |
2133 | else | |
2134 | flags = ic->iformat->flags; | |
2135 | if (flags & AVFMT_SHOW_IDS) { | |
43465395 | 2136 | av_log(NULL, AV_LOG_DEBUG, "[0x%x]", st->id); |
b9a281db | 2137 | } |
43465395 | 2138 | av_log(NULL, AV_LOG_DEBUG, ": %s\n", buf); |
de6d9b64 FB |
2139 | } |
2140 | } | |
2141 | ||
2142 | typedef struct { | |
445f1b83 | 2143 | const char *abv; |
de6d9b64 | 2144 | int width, height; |
445f1b83 RS |
2145 | int frame_rate, frame_rate_base; |
2146 | } AbvEntry; | |
2147 | ||
2148 | static AbvEntry frame_abvs[] = { | |
ba2a8cb4 RS |
2149 | { "ntsc", 720, 480, 30000, 1001 }, |
2150 | { "pal", 720, 576, 25, 1 }, | |
2151 | { "qntsc", 352, 240, 30000, 1001 }, /* VCD compliant ntsc */ | |
2152 | { "qpal", 352, 288, 25, 1 }, /* VCD compliant pal */ | |
904736b5 RS |
2153 | { "sntsc", 640, 480, 30000, 1001 }, /* square pixel ntsc */ |
2154 | { "spal", 768, 576, 25, 1 }, /* square pixel pal */ | |
445f1b83 RS |
2155 | { "film", 352, 240, 24, 1 }, |
2156 | { "ntsc-film", 352, 240, 24000, 1001 }, | |
2157 | { "sqcif", 128, 96, 0, 0 }, | |
2158 | { "qcif", 176, 144, 0, 0 }, | |
2159 | { "cif", 352, 288, 0, 0 }, | |
2160 | { "4cif", 704, 576, 0, 0 }, | |
de6d9b64 | 2161 | }; |
445f1b83 | 2162 | |
de6d9b64 FB |
2163 | int parse_image_size(int *width_ptr, int *height_ptr, const char *str) |
2164 | { | |
2165 | int i; | |
445f1b83 | 2166 | int n = sizeof(frame_abvs) / sizeof(AbvEntry); |
de6d9b64 FB |
2167 | const char *p; |
2168 | int frame_width = 0, frame_height = 0; | |
2169 | ||
2170 | for(i=0;i<n;i++) { | |
445f1b83 RS |
2171 | if (!strcmp(frame_abvs[i].abv, str)) { |
2172 | frame_width = frame_abvs[i].width; | |
2173 | frame_height = frame_abvs[i].height; | |
de6d9b64 FB |
2174 | break; |
2175 | } | |
2176 | } | |
2177 | if (i == n) { | |
2178 | p = str; | |
2179 | frame_width = strtol(p, (char **)&p, 10); | |
2180 | if (*p) | |
2181 | p++; | |
2182 | frame_height = strtol(p, (char **)&p, 10); | |
2183 | } | |
2184 | if (frame_width <= 0 || frame_height <= 0) | |
2185 | return -1; | |
2186 | *width_ptr = frame_width; | |
2187 | *height_ptr = frame_height; | |
2188 | return 0; | |
2189 | } | |
2190 | ||
445f1b83 RS |
2191 | int parse_frame_rate(int *frame_rate, int *frame_rate_base, const char *arg) |
2192 | { | |
2193 | int i; | |
2194 | char* cp; | |
2195 | ||
2196 | /* First, we check our abbreviation table */ | |
2197 | for (i = 0; i < sizeof(frame_abvs)/sizeof(*frame_abvs); ++i) | |
2198 | if (!strcmp(frame_abvs[i].abv, arg)) { | |
2199 | *frame_rate = frame_abvs[i].frame_rate; | |
2200 | *frame_rate_base = frame_abvs[i].frame_rate_base; | |
2201 | return 0; | |
2202 | } | |
2203 | ||
2204 | /* Then, we try to parse it as fraction */ | |
2205 | cp = strchr(arg, '/'); | |
2206 | if (cp) { | |
2207 | char* cpp; | |
2208 | *frame_rate = strtol(arg, &cpp, 10); | |
2209 | if (cpp != arg || cpp == cp) | |
2210 | *frame_rate_base = strtol(cp+1, &cpp, 10); | |
2211 | else | |
2212 | *frame_rate = 0; | |
2213 | } | |
2214 | else { | |
2215 | /* Finally we give up and parse it as double */ | |
8bfed902 | 2216 | *frame_rate_base = DEFAULT_FRAME_RATE_BASE; //FIXME use av_d2q() |
445f1b83 RS |
2217 | *frame_rate = (int)(strtod(arg, 0) * (*frame_rate_base) + 0.5); |
2218 | } | |
2219 | if (!*frame_rate || !*frame_rate_base) | |
2220 | return -1; | |
2221 | else | |
2222 | return 0; | |
2223 | } | |
2224 | ||
916c80e9 FB |
2225 | /* Syntax: |
2226 | * - If not a duration: | |
2227 | * [{YYYY-MM-DD|YYYYMMDD}]{T| }{HH[:MM[:SS[.m...]]][Z]|HH[MM[SS[.m...]]][Z]} | |
c5510dd6 | 2228 | * Time is localtime unless Z is suffixed to the end. In this case GMT |
916c80e9 FB |
2229 | * Return the date in micro seconds since 1970 |
2230 | * - If duration: | |
2231 | * HH[:MM[:SS[.m...]]] | |
2232 | * S+[.m...] | |
2233 | */ | |
0c1a9eda | 2234 | int64_t parse_date(const char *datestr, int duration) |
de6d9b64 FB |
2235 | { |
2236 | const char *p; | |
0c1a9eda | 2237 | int64_t t; |
2dbceb9f | 2238 | struct tm dt; |
c5510dd6 PG |
2239 | int i; |
2240 | static const char *date_fmt[] = { | |
2241 | "%Y-%m-%d", | |
2242 | "%Y%m%d", | |
2243 | }; | |
2244 | static const char *time_fmt[] = { | |
2245 | "%H:%M:%S", | |
2246 | "%H%M%S", | |
2247 | }; | |
2248 | const char *q; | |
916c80e9 | 2249 | int is_utc, len; |
c5510dd6 | 2250 | char lastch; |
a6a92a9a | 2251 | int negative = 0; |
6d8f985e MN |
2252 | |
2253 | #undef time | |
c5510dd6 PG |
2254 | time_t now = time(0); |
2255 | ||
916c80e9 FB |
2256 | len = strlen(datestr); |
2257 | if (len > 0) | |
2258 | lastch = datestr[len - 1]; | |
2259 | else | |
2260 | lastch = '\0'; | |
c5510dd6 | 2261 | is_utc = (lastch == 'z' || lastch == 'Z'); |
2dbceb9f PG |
2262 | |
2263 | memset(&dt, 0, sizeof(dt)); | |
de6d9b64 FB |
2264 | |
2265 | p = datestr; | |
916c80e9 | 2266 | q = NULL; |
de6d9b64 | 2267 | if (!duration) { |
c5510dd6 | 2268 | for (i = 0; i < sizeof(date_fmt) / sizeof(date_fmt[0]); i++) { |
f71869a4 | 2269 | q = small_strptime(p, date_fmt[i], &dt); |
c5510dd6 PG |
2270 | if (q) { |
2271 | break; | |
2272 | } | |
2273 | } | |
2274 | ||
2275 | if (!q) { | |
2276 | if (is_utc) { | |
2277 | dt = *gmtime(&now); | |
2278 | } else { | |
2279 | dt = *localtime(&now); | |
2280 | } | |
2281 | dt.tm_hour = dt.tm_min = dt.tm_sec = 0; | |
de6d9b64 | 2282 | } else { |
c5510dd6 | 2283 | p = q; |
de6d9b64 | 2284 | } |
c5510dd6 PG |
2285 | |
2286 | if (*p == 'T' || *p == 't' || *p == ' ') | |
2287 | p++; | |
c5510dd6 | 2288 | |
916c80e9 | 2289 | for (i = 0; i < sizeof(time_fmt) / sizeof(time_fmt[0]); i++) { |
f71869a4 | 2290 | q = small_strptime(p, time_fmt[i], &dt); |
916c80e9 FB |
2291 | if (q) { |
2292 | break; | |
2293 | } | |
2294 | } | |
2295 | } else { | |
a6a92a9a WG |
2296 | if (p[0] == '-') { |
2297 | negative = 1; | |
2298 | ++p; | |
2299 | } | |
f71869a4 | 2300 | q = small_strptime(p, time_fmt[0], &dt); |
916c80e9 FB |
2301 | if (!q) { |
2302 | dt.tm_sec = strtol(p, (char **)&q, 10); | |
2303 | dt.tm_min = 0; | |
2304 | dt.tm_hour = 0; | |
c5510dd6 PG |
2305 | } |
2306 | } | |
2307 | ||
2308 | /* Now we have all the fields that we can get */ | |
2309 | if (!q) { | |
2310 | if (duration) | |
2311 | return 0; | |
2312 | else | |
0c1a9eda | 2313 | return now * int64_t_C(1000000); |
de6d9b64 | 2314 | } |
2dbceb9f PG |
2315 | |
2316 | if (duration) { | |
c5510dd6 | 2317 | t = dt.tm_hour * 3600 + dt.tm_min * 60 + dt.tm_sec; |
2dbceb9f | 2318 | } else { |
c5510dd6 PG |
2319 | dt.tm_isdst = -1; /* unknown */ |
2320 | if (is_utc) { | |
2321 | t = mktimegm(&dt); | |
2322 | } else { | |
2323 | t = mktime(&dt); | |
2324 | } | |
de6d9b64 | 2325 | } |
2dbceb9f | 2326 | |
c5510dd6 PG |
2327 | t *= 1000000; |
2328 | ||
2329 | if (*q == '.') { | |
de6d9b64 | 2330 | int val, n; |
c5510dd6 PG |
2331 | q++; |
2332 | for (val = 0, n = 100000; n >= 1; n /= 10, q++) { | |
2333 | if (!isdigit(*q)) | |
2334 | break; | |
2335 | val += n * (*q - '0'); | |
de6d9b64 FB |
2336 | } |
2337 | t += val; | |
2338 | } | |
a6a92a9a | 2339 | return negative ? -t : t; |
de6d9b64 FB |
2340 | } |
2341 | ||
2dbceb9f | 2342 | /* syntax: '?tag1=val1&tag2=val2...'. Little URL decoding is done. Return |
de6d9b64 FB |
2343 | 1 if found */ |
2344 | int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info) | |
2345 | { | |
2346 | const char *p; | |
2347 | char tag[128], *q; | |
2348 | ||
2349 | p = info; | |
2350 | if (*p == '?') | |
2351 | p++; | |
2352 | for(;;) { | |
2353 | q = tag; | |
2354 | while (*p != '\0' && *p != '=' && *p != '&') { | |
2355 | if ((q - tag) < sizeof(tag) - 1) | |
2356 | *q++ = *p; | |
2357 | p++; | |
2358 | } | |
2359 | *q = '\0'; | |
2360 | q = arg; | |
2361 | if (*p == '=') { | |
2362 | p++; | |
2363 | while (*p != '&' && *p != '\0') { | |
2dbceb9f PG |
2364 | if ((q - arg) < arg_size - 1) { |
2365 | if (*p == '+') | |
2366 | *q++ = ' '; | |
2367 | else | |
2368 | *q++ = *p; | |
2369 | } | |
de6d9b64 FB |
2370 | p++; |
2371 | } | |
2372 | *q = '\0'; | |
2373 | } | |
2374 | if (!strcmp(tag, tag1)) | |
2375 | return 1; | |
2376 | if (*p != '&') | |
2377 | break; | |
8d1335ea | 2378 | p++; |
de6d9b64 FB |
2379 | } |
2380 | return 0; | |
2381 | } | |
2382 | ||
9150f42e FB |
2383 | /* Return in 'buf' the path with '%d' replaced by number. Also handles |
2384 | the '%0nd' format where 'n' is the total number of digits and | |
2385 | '%%'. Return 0 if OK, and -1 if format error */ | |
2386 | int get_frame_filename(char *buf, int buf_size, | |
2387 | const char *path, int number) | |
2388 | { | |
2389 | const char *p; | |
0bf92f79 PI |
2390 | char *q, buf1[20], c; |
2391 | int nd, len, percentd_found; | |
9150f42e FB |
2392 | |
2393 | q = buf; | |
2394 | p = path; | |
2395 | percentd_found = 0; | |
2396 | for(;;) { | |
2397 | c = *p++; | |
2398 | if (c == '\0') | |
2399 | break; | |
2400 | if (c == '%') { | |
c9646fda PG |
2401 | do { |
2402 | nd = 0; | |
2403 | while (isdigit(*p)) { | |
2404 | nd = nd * 10 + *p++ - '0'; | |
2405 | } | |
2406 | c = *p++; | |
c9646fda PG |
2407 | } while (isdigit(c)); |
2408 | ||
9150f42e FB |
2409 | switch(c) { |
2410 | case '%': | |
2411 | goto addchar; | |
2412 | case 'd': | |
2413 | if (percentd_found) | |
2414 | goto fail; | |
2415 | percentd_found = 1; | |
2416 | snprintf(buf1, sizeof(buf1), "%0*d", nd, number); | |
2417 | len = strlen(buf1); | |
2418 | if ((q - buf + len) > buf_size - 1) | |
2419 | goto fail; | |
2420 | memcpy(q, buf1, len); | |
2421 | q += len; | |
2422 | break; | |
2423 | default: | |
2424 | goto fail; | |
2425 | } | |
2426 | } else { | |
2427 | addchar: | |
2428 | if ((q - buf) < buf_size - 1) | |
2429 | *q++ = c; | |
2430 | } | |
2431 | } | |
2432 | if (!percentd_found) | |
2433 | goto fail; | |
2434 | *q = '\0'; | |
2435 | return 0; | |
2436 | fail: | |
2437 | *q = '\0'; | |
2438 | return -1; | |
2439 | } | |
2440 | ||
b9a281db | 2441 | /** |
fb2758c8 FB |
2442 | * Print nice hexa dump of a buffer |
2443 | * @param f stream for output | |
b9a281db FB |
2444 | * @param buf buffer |
2445 | * @param size buffer size | |
2446 | */ | |
fb2758c8 | 2447 | void av_hex_dump(FILE *f, uint8_t *buf, int size) |
b9a281db FB |
2448 | { |
2449 | int len, i, j, c; | |
2450 | ||
2451 | for(i=0;i<size;i+=16) { | |
2452 | len = size - i; | |
2453 | if (len > 16) | |
2454 | len = 16; | |
fb2758c8 | 2455 | fprintf(f, "%08x ", i); |
b9a281db FB |
2456 | for(j=0;j<16;j++) { |
2457 | if (j < len) | |
fb2758c8 | 2458 | fprintf(f, " %02x", buf[i+j]); |
b9a281db | 2459 | else |
fb2758c8 | 2460 | fprintf(f, " "); |
b9a281db | 2461 | } |
fb2758c8 | 2462 | fprintf(f, " "); |
b9a281db FB |
2463 | for(j=0;j<len;j++) { |
2464 | c = buf[i+j]; | |
2465 | if (c < ' ' || c > '~') | |
2466 | c = '.'; | |
fb2758c8 | 2467 | fprintf(f, "%c", c); |
b9a281db | 2468 | } |
fb2758c8 | 2469 | fprintf(f, "\n"); |
b9a281db FB |
2470 | } |
2471 | } | |
2472 | ||
fb2758c8 FB |
2473 | /** |
2474 | * Print on 'f' a nice dump of a packet | |
2475 | * @param f stream for output | |
2476 | * @param pkt packet to dump | |
2477 | * @param dump_payload true if the payload must be displayed too | |
2478 | */ | |
2479 | void av_pkt_dump(FILE *f, AVPacket *pkt, int dump_payload) | |
2480 | { | |
2481 | fprintf(f, "stream #%d:\n", pkt->stream_index); | |
2482 | fprintf(f, " keyframe=%d\n", ((pkt->flags & PKT_FLAG_KEY) != 0)); | |
2483 | fprintf(f, " duration=%0.3f\n", (double)pkt->duration / AV_TIME_BASE); | |
37353960 FB |
2484 | /* DTS is _always_ valid after av_read_frame() */ |
2485 | fprintf(f, " dts="); | |
2486 | if (pkt->dts == AV_NOPTS_VALUE) | |
2487 | fprintf(f, "N/A"); | |
2488 | else | |
2489 | fprintf(f, "%0.3f", (double)pkt->dts / AV_TIME_BASE); | |
fb2758c8 FB |
2490 | /* PTS may be not known if B frames are present */ |
2491 | fprintf(f, " pts="); | |
2492 | if (pkt->pts == AV_NOPTS_VALUE) | |
2493 | fprintf(f, "N/A"); | |
2494 | else | |
2495 | fprintf(f, "%0.3f", (double)pkt->pts / AV_TIME_BASE); | |
2496 | fprintf(f, "\n"); | |
2497 | fprintf(f, " size=%d\n", pkt->size); | |
2498 | if (dump_payload) | |
2499 | av_hex_dump(f, pkt->data, pkt->size); | |
2500 | } | |
2501 | ||
a9a721da | 2502 | void url_split(char *proto, int proto_size, |
6ba5cbc6 | 2503 | char *authorization, int authorization_size, |
a9a721da FB |
2504 | char *hostname, int hostname_size, |
2505 | int *port_ptr, | |
2506 | char *path, int path_size, | |
2507 | const char *url) | |
2508 | { | |
2509 | const char *p; | |
2510 | char *q; | |
2511 | int port; | |
2512 | ||
2513 | port = -1; | |
2514 | ||
2515 | p = url; | |
2516 | q = proto; | |
2517 | while (*p != ':' && *p != '\0') { | |
2518 | if ((q - proto) < proto_size - 1) | |
2519 | *q++ = *p; | |
2520 | p++; | |
2521 | } | |
2522 | if (proto_size > 0) | |
2523 | *q = '\0'; | |
6ba5cbc6 PD |
2524 | if (authorization_size > 0) |
2525 | authorization[0] = '\0'; | |
a9a721da FB |
2526 | if (*p == '\0') { |
2527 | if (proto_size > 0) | |
2528 | proto[0] = '\0'; | |
2529 | if (hostname_size > 0) | |
2530 | hostname[0] = '\0'; | |
2531 | p = url; | |
2532 | } else { | |
6ba5cbc6 PD |
2533 | char *at,*slash; // PETR: position of '@' character and '/' character |
2534 | ||
a9a721da FB |
2535 | p++; |
2536 | if (*p == '/') | |
2537 | p++; | |
2538 | if (*p == '/') | |
2539 | p++; | |
6ba5cbc6 PD |
2540 | at = strchr(p,'@'); // PETR: get the position of '@' |
2541 | slash = strchr(p,'/'); // PETR: get position of '/' - end of hostname | |
2542 | if (at && slash && at > slash) at = NULL; // PETR: not interested in '@' behind '/' | |
2543 | ||
2544 | q = at ? authorization : hostname; // PETR: if '@' exists starting with auth. | |
2545 | ||
2546 | while ((at || *p != ':') && *p != '/' && *p != '?' && *p != '\0') { // PETR: | |
2547 | if (*p == '@') { // PETR: passed '@' | |
2548 | if (authorization_size > 0) | |
2549 | *q = '\0'; | |
2550 | q = hostname; | |
2551 | at = NULL; | |
2552 | } else if (!at) { // PETR: hostname | |
2553 | if ((q - hostname) < hostname_size - 1) | |
2554 | *q++ = *p; | |
2555 | } else { | |
2556 | if ((q - authorization) < authorization_size - 1) | |
a9a721da | 2557 | *q++ = *p; |
6ba5cbc6 | 2558 | } |
a9a721da FB |
2559 | p++; |
2560 | } | |
2561 | if (hostname_size > 0) | |
2562 | *q = '\0'; | |
2563 | if (*p == ':') { | |
2564 | p++; | |
2565 | port = strtoul(p, (char **)&p, 10); | |
2566 | } | |
2567 | } | |
2568 | if (port_ptr) | |
2569 | *port_ptr = port; | |
2570 | pstrcpy(path, path_size, p); | |
2571 | } | |
2572 | ||
916c80e9 FB |
2573 | /** |
2574 | * Set the pts for a given stream | |
2575 | * @param s stream | |
2576 | * @param pts_wrap_bits number of bits effectively used by the pts | |
2577 | * (used for wrap control, 33 is the value for MPEG) | |
2578 | * @param pts_num numerator to convert to seconds (MPEG: 1) | |
2579 | * @param pts_den denominator to convert to seconds (MPEG: 90000) | |
2580 | */ | |
9ee91c2f | 2581 | void av_set_pts_info(AVStream *s, int pts_wrap_bits, |
916c80e9 FB |
2582 | int pts_num, int pts_den) |
2583 | { | |
2584 | s->pts_wrap_bits = pts_wrap_bits; | |
9ee91c2f MN |
2585 | s->time_base.num = pts_num; |
2586 | s->time_base.den = pts_den; | |
916c80e9 FB |
2587 | } |
2588 | ||
2589 | /* fraction handling */ | |
2590 | ||
2591 | /** | |
2592 | * f = val + (num / den) + 0.5. 'num' is normalized so that it is such | |
2593 | * as 0 <= num < den. | |
2594 | * | |
2595 | * @param f fractional number | |
2596 | * @param val integer value | |
2597 | * @param num must be >= 0 | |
2598 | * @param den must be >= 1 | |
2599 | */ | |
0c1a9eda | 2600 | void av_frac_init(AVFrac *f, int64_t val, int64_t num, int64_t den) |
916c80e9 FB |
2601 | { |
2602 | num += (den >> 1); | |
2603 | if (num >= den) { | |
2604 | val += num / den; | |
2605 | num = num % den; | |
2606 | } | |
2607 | f->val = val; | |
2608 | f->num = num; | |
2609 | f->den = den; | |
2610 | } | |
2611 | ||
2612 | /* set f to (val + 0.5) */ | |
0c1a9eda | 2613 | void av_frac_set(AVFrac *f, int64_t val) |
916c80e9 FB |
2614 | { |
2615 | f->val = val; | |
2616 | f->num = f->den >> 1; | |
2617 | } | |
2618 | ||
2619 | /** | |
2620 | * Fractionnal addition to f: f = f + (incr / f->den) | |
2621 | * | |
2622 | * @param f fractional number | |
2623 | * @param incr increment, can be positive or negative | |
2624 | */ | |
0c1a9eda | 2625 | void av_frac_add(AVFrac *f, int64_t incr) |
916c80e9 | 2626 | { |
0c1a9eda | 2627 | int64_t num, den; |
916c80e9 FB |
2628 | |
2629 | num = f->num + incr; | |
2630 | den = f->den; | |
2631 | if (num < 0) { | |
2632 | f->val += num / den; | |
2633 | num = num % den; | |
2634 | if (num < 0) { | |
2635 | num += den; | |
2636 | f->val--; | |
2637 | } | |
2638 | } else if (num >= den) { | |
2639 | f->val += num / den; | |
2640 | num = num % den; | |
2641 | } | |
2642 | f->num = num; | |
2643 | } | |
87a0a681 FB |
2644 | |
2645 | /** | |
2646 | * register a new image format | |
2647 | * @param img_fmt Image format descriptor | |
2648 | */ | |
2649 | void av_register_image_format(AVImageFormat *img_fmt) | |
2650 | { | |
2651 | AVImageFormat **p; | |
2652 | ||
2653 | p = &first_image_format; | |
2654 | while (*p != NULL) p = &(*p)->next; | |
2655 | *p = img_fmt; | |
2656 | img_fmt->next = NULL; | |
2657 | } | |
2658 | ||
2659 | /* guess image format */ | |
2660 | AVImageFormat *av_probe_image_format(AVProbeData *pd) | |
2661 | { | |
2662 | AVImageFormat *fmt1, *fmt; | |
2663 | int score, score_max; | |
2664 | ||
2665 | fmt = NULL; | |
2666 | score_max = 0; | |
2667 | for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
2668 | if (fmt1->img_probe) { | |
2669 | score = fmt1->img_probe(pd); | |
2670 | if (score > score_max) { | |
2671 | score_max = score; | |
2672 | fmt = fmt1; | |
2673 | } | |
2674 | } | |
2675 | } | |
2676 | return fmt; | |
2677 | } | |
2678 | ||
2679 | AVImageFormat *guess_image_format(const char *filename) | |
2680 | { | |
2681 | AVImageFormat *fmt1; | |
2682 | ||
2683 | for(fmt1 = first_image_format; fmt1 != NULL; fmt1 = fmt1->next) { | |
2684 | if (fmt1->extensions && match_ext(filename, fmt1->extensions)) | |
2685 | return fmt1; | |
2686 | } | |
2687 | return NULL; | |
2688 | } | |
2689 | ||
2690 | /** | |
2691 | * Read an image from a stream. | |
2692 | * @param gb byte stream containing the image | |
2693 | * @param fmt image format, NULL if probing is required | |
2694 | */ | |
2695 | int av_read_image(ByteIOContext *pb, const char *filename, | |
2696 | AVImageFormat *fmt, | |
2697 | int (*alloc_cb)(void *, AVImageInfo *info), void *opaque) | |
2698 | { | |
2699 | char buf[PROBE_BUF_SIZE]; | |
2700 | AVProbeData probe_data, *pd = &probe_data; | |
2701 | offset_t pos; | |
2702 | int ret; | |
2703 | ||
2704 | if (!fmt) { | |
5c91a675 | 2705 | pd->filename = filename; |
87a0a681 FB |
2706 | pd->buf = buf; |
2707 | pos = url_ftell(pb); | |
2708 | pd->buf_size = get_buffer(pb, buf, PROBE_BUF_SIZE); | |
2709 | url_fseek(pb, pos, SEEK_SET); | |
2710 | fmt = av_probe_image_format(pd); | |
2711 | } | |
2712 | if (!fmt) | |
2713 | return AVERROR_NOFMT; | |
2714 | ret = fmt->img_read(pb, alloc_cb, opaque); | |
2715 | return ret; | |
2716 | } | |
2717 | ||
2718 | /** | |
2719 | * Write an image to a stream. | |
2720 | * @param pb byte stream for the image output | |
2721 | * @param fmt image format | |
2722 | * @param img image data and informations | |
2723 | */ | |
2724 | int av_write_image(ByteIOContext *pb, AVImageFormat *fmt, AVImageInfo *img) | |
2725 | { | |
2726 | return fmt->img_write(pb, img); | |
2727 | } | |
2728 |