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