- Added force_pts to av_write_packet() to be able to force PTS, this helps
[libav.git] / libav / utils.c
1 /*
2 * Various utilities for ffmpeg system
3 * Copyright (c) 2000,2001 Gerard Lantau
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 #include "avformat.h"
20 #include "tick.h"
21 #ifndef CONFIG_WIN32
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/time.h>
25 #include <time.h>
26 #else
27 #define strcasecmp _stricmp
28 #include <sys/types.h>
29 #include <sys/timeb.h>
30 #endif
31
32 AVFormat *first_format;
33
34 void register_avformat(AVFormat *format)
35 {
36 AVFormat **p;
37 p = &first_format;
38 while (*p != NULL) p = &(*p)->next;
39 *p = format;
40 format->next = NULL;
41 }
42
43 int match_ext(const char *filename, const char *extensions)
44 {
45 const char *ext, *p;
46 char ext1[32], *q;
47
48 ext = strrchr(filename, '.');
49 if (ext) {
50 ext++;
51 p = extensions;
52 for(;;) {
53 q = ext1;
54 while (*p != '\0' && *p != ',')
55 *q++ = *p++;
56 *q = '\0';
57 if (!strcasecmp(ext1, ext))
58 return 1;
59 if (*p == '\0')
60 break;
61 p++;
62 }
63 }
64 return 0;
65 }
66
67 AVFormat *guess_format(const char *short_name, const char *filename, const char *mime_type)
68 {
69 AVFormat *fmt, *fmt_found;
70 int score_max, score;
71
72 /* find the proper file type */
73 fmt_found = NULL;
74 score_max = 0;
75 fmt = first_format;
76 while (fmt != NULL) {
77 score = 0;
78 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
79 score += 100;
80 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
81 score += 10;
82 if (filename && fmt->extensions &&
83 match_ext(filename, fmt->extensions)) {
84 score += 5;
85 }
86 if (score > score_max) {
87 score_max = score;
88 fmt_found = fmt;
89 }
90 fmt = fmt->next;
91 }
92 return fmt_found;
93 }
94
95 /* return TRUE if val is a prefix of str. If it returns TRUE, ptr is
96 set to the next character in 'str' after the prefix */
97 int strstart(const char *str, const char *val, const char **ptr)
98 {
99 const char *p, *q;
100 p = str;
101 q = val;
102 while (*q != '\0') {
103 if (*p != *q)
104 return 0;
105 p++;
106 q++;
107 }
108 if (ptr)
109 *ptr = p;
110 return 1;
111 }
112
113 void nstrcpy(char *buf, int buf_size, const char *str)
114 {
115 int c;
116 char *q = buf;
117
118 for(;;) {
119 c = *str++;
120 if (c == 0 || q >= buf + buf_size - 1)
121 break;
122 *q++ = c;
123 }
124 *q = '\0';
125 }
126
127 void register_all(void)
128 {
129 avcodec_init();
130 avcodec_register_all();
131
132 register_avformat(&mp2_format);
133 register_avformat(&ac3_format);
134 register_avformat(&mpeg_mux_format);
135 register_avformat(&mpeg1video_format);
136 register_avformat(&mjpeg_format);
137 register_avformat(&h263_format);
138 register_avformat(&rm_format);
139 register_avformat(&asf_format);
140 register_avformat(&avi_format);
141 register_avformat(&mpjpeg_format);
142 register_avformat(&jpeg_format);
143 register_avformat(&single_jpeg_format);
144 register_avformat(&swf_format);
145 register_avformat(&wav_format);
146 register_avformat(&pcm_s16le_format);
147 register_avformat(&pcm_s16be_format);
148 register_avformat(&pcm_u16le_format);
149 register_avformat(&pcm_u16be_format);
150 register_avformat(&pcm_s8_format);
151 register_avformat(&pcm_u8_format);
152 register_avformat(&pcm_mulaw_format);
153 register_avformat(&pcm_alaw_format);
154 register_avformat(&rawvideo_format);
155 #ifndef CONFIG_WIN32
156 register_avformat(&ffm_format);
157 #endif
158 register_avformat(&pgm_format);
159 register_avformat(&ppm_format);
160 register_avformat(&pgmyuv_format);
161 register_avformat(&imgyuv_format);
162 register_avformat(&pgmpipe_format);
163 register_avformat(&pgmyuvpipe_format);
164 register_avformat(&ppmpipe_format);
165 #ifdef CONFIG_GRAB
166 register_avformat(&video_grab_device_format);
167 register_avformat(&audio_device_format);
168 #endif
169
170 /* file protocols */
171 register_protocol(&file_protocol);
172 register_protocol(&pipe_protocol);
173 #ifndef CONFIG_WIN32
174 register_protocol(&udp_protocol);
175 register_protocol(&http_protocol);
176 #endif
177 }
178
179 /* memory handling */
180
181 int av_new_packet(AVPacket *pkt, int size)
182 {
183 pkt->data = malloc(size);
184 if (!pkt->data)
185 return -ENOMEM;
186 pkt->size = size;
187 /* sane state */
188 pkt->pts = 0;
189 pkt->stream_index = 0;
190 pkt->flags = 0;
191 return 0;
192 }
193
194 void av_free_packet(AVPacket *pkt)
195 {
196 free(pkt->data);
197 /* fail safe */
198 pkt->data = NULL;
199 pkt->size = 0;
200 }
201
202 /* fifo handling */
203
204 int fifo_init(FifoBuffer *f, int size)
205 {
206 f->buffer = malloc(size);
207 if (!f->buffer)
208 return -1;
209 f->end = f->buffer + size;
210 f->wptr = f->rptr = f->buffer;
211 return 0;
212 }
213
214 void fifo_free(FifoBuffer *f)
215 {
216 free(f->buffer);
217 }
218
219 int fifo_size(FifoBuffer *f, UINT8 *rptr)
220 {
221 int size;
222
223 if (f->wptr >= rptr) {
224 size = f->wptr - rptr;
225 } else {
226 size = (f->end - rptr) + (f->wptr - f->buffer);
227 }
228 return size;
229 }
230
231 /* get data from the fifo (return -1 if not enough data) */
232 int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr)
233 {
234 UINT8 *rptr = *rptr_ptr;
235 int size, len;
236
237 if (f->wptr >= rptr) {
238 size = f->wptr - rptr;
239 } else {
240 size = (f->end - rptr) + (f->wptr - f->buffer);
241 }
242
243 if (size < buf_size)
244 return -1;
245 while (buf_size > 0) {
246 len = f->end - rptr;
247 if (len > buf_size)
248 len = buf_size;
249 memcpy(buf, rptr, len);
250 buf += len;
251 rptr += len;
252 if (rptr >= f->end)
253 rptr = f->buffer;
254 buf_size -= len;
255 }
256 *rptr_ptr = rptr;
257 return 0;
258 }
259
260 void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr)
261 {
262 int len;
263 UINT8 *wptr;
264 wptr = *wptr_ptr;
265 while (size > 0) {
266 len = f->end - wptr;
267 if (len > size)
268 len = size;
269 memcpy(wptr, buf, len);
270 wptr += len;
271 if (wptr >= f->end)
272 wptr = f->buffer;
273 buf += len;
274 size -= len;
275 }
276 *wptr_ptr = wptr;
277 }
278
279 /* media file handling.
280 'filename' is the filename to open.
281 'format_name' is used to force the file format (NULL if auto guess).
282 'buf_size' is the optional buffer size (zero if default is OK).
283 'ap' are additionnal parameters needed when opening the file (NULL if default).
284 */
285
286 AVFormatContext *av_open_input_file(const char *filename,
287 const char *format_name,
288 int buf_size,
289 AVFormatParameters *ap)
290 {
291 AVFormat *fmt;
292 AVFormatContext *ic = NULL;
293 int err;
294
295 ic = av_mallocz(sizeof(AVFormatContext));
296 if (!ic)
297 goto fail;
298
299 /* find format */
300 if (format_name != NULL) {
301 fmt = guess_format(format_name, NULL, NULL);
302 } else {
303 fmt = guess_format(NULL, filename, NULL);
304 }
305 if (!fmt || !fmt->read_header) {
306 return NULL;
307 }
308 ic->format = fmt;
309
310 /* if no file needed do not try to open one */
311 if (!(fmt->flags & AVFMT_NOFILE)) {
312 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0)
313 goto fail;
314 if (buf_size > 0) {
315 url_setbufsize(&ic->pb, buf_size);
316 }
317 }
318
319 err = ic->format->read_header(ic, ap);
320 if (err < 0) {
321 if (!(fmt->flags & AVFMT_NOFILE)) {
322 url_fclose(&ic->pb);
323 }
324 goto fail;
325 }
326
327 return ic;
328
329 fail:
330 if (ic)
331 free(ic);
332 return NULL;
333 }
334
335 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
336 {
337 AVPacketList *pktl;
338
339 pktl = s->packet_buffer;
340 if (pktl) {
341 /* read packet from packet buffer, if there is data */
342 *pkt = pktl->pkt;
343 s->packet_buffer = pktl->next;
344 free(pktl);
345 return 0;
346 } else {
347 return s->format->read_packet(s, pkt);
348 }
349 }
350
351 void av_close_input_file(AVFormatContext *s)
352 {
353 int i;
354
355 if (s->format->read_close)
356 s->format->read_close(s);
357 for(i=0;i<s->nb_streams;i++) {
358 free(s->streams[i]);
359 }
360 if (s->packet_buffer) {
361 AVPacketList *p, *p1;
362 p = s->packet_buffer;
363 while (p != NULL) {
364 p1 = p->next;
365 av_free_packet(&p->pkt);
366 free(p);
367 p = p1;
368 }
369 s->packet_buffer = NULL;
370 }
371 if (!(s->format->flags & AVFMT_NOFILE)) {
372 url_fclose(&s->pb);
373 }
374 free(s);
375 }
376
377
378 int av_write_packet(AVFormatContext *s, AVPacket *pkt, int force_pts)
379 {
380 /* XXX: currently, an emulation because internal API must change */
381 return s->format->write_packet(s, pkt->stream_index, pkt->data, pkt->size, force_pts);
382 }
383
384 /* "user interface" functions */
385
386 void dump_format(AVFormatContext *ic,
387 int index,
388 const char *url,
389 int is_output)
390 {
391 int i;
392 char buf[256];
393
394 fprintf(stderr, "%s #%d, %s, %s '%s':\n",
395 is_output ? "Output" : "Input",
396 index, ic->format->name,
397 is_output ? "to" : "from", url);
398 for(i=0;i<ic->nb_streams;i++) {
399 AVStream *st = ic->streams[i];
400 avcodec_string(buf, sizeof(buf), &st->codec, is_output);
401 fprintf(stderr, " Stream #%d.%d: %s\n", index, i, buf);
402 }
403 }
404
405 typedef struct {
406 const char *str;
407 int width, height;
408 } SizeEntry;
409
410 static SizeEntry sizes[] = {
411 { "sqcif", 128, 96 },
412 { "qcif", 176, 144 },
413 { "cif", 352, 288 },
414 { "4cif", 704, 576 },
415 };
416
417 int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
418 {
419 int i;
420 int n = sizeof(sizes) / sizeof(SizeEntry);
421 const char *p;
422 int frame_width = 0, frame_height = 0;
423
424 for(i=0;i<n;i++) {
425 if (!strcmp(sizes[i].str, str)) {
426 frame_width = sizes[i].width;
427 frame_height = sizes[i].height;
428 break;
429 }
430 }
431 if (i == n) {
432 p = str;
433 frame_width = strtol(p, (char **)&p, 10);
434 if (*p)
435 p++;
436 frame_height = strtol(p, (char **)&p, 10);
437 }
438 if (frame_width <= 0 || frame_height <= 0)
439 return -1;
440 *width_ptr = frame_width;
441 *height_ptr = frame_height;
442 return 0;
443 }
444
445 INT64 gettime(void)
446 {
447 #ifdef CONFIG_WIN32
448 struct _timeb tb;
449 _ftime(&tb);
450 return ((INT64)tb.time * INT64_C(1000) + (INT64)tb.millitm) * INT64_C(1000);
451 #else
452 struct timeval tv;
453 gettimeofday(&tv,NULL);
454 return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
455 #endif
456 }
457
458 /* syntax: [YYYY-MM-DD ][[HH:]MM:]SS[.m...] . Return the date in micro seconds since 1970 */
459 INT64 parse_date(const char *datestr, int duration)
460 {
461 const char *p;
462 INT64 t;
463 int sec;
464
465 p = datestr;
466 if (!duration) {
467 static const UINT8 months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
468 int year, month, day, i;
469
470 if (strlen(p) >= 5 && p[4] == '-') {
471
472 year = strtol(p, (char **)&p, 10);
473 if (*p)
474 p++;
475 month = strtol(p, (char **)&p, 10) - 1;
476 if (*p)
477 p++;
478 day = strtol(p, (char **)&p, 10) - 1;
479 if (*p)
480 p++;
481 day += (year - 1970) * 365;
482 /* if >= March, take February of current year into account too */
483 if (month >= 2)
484 year++;
485 for(i=1970;i<year;i++) {
486 if ((i % 100) == 0) {
487 if ((i % 400) == 0) day++;
488 } else if ((i % 4) == 0) {
489 day++;
490 }
491 }
492 for(i=0;i<month;i++)
493 day += months[i];
494 } else {
495 day = (time(NULL) / (3600 * 24));
496 }
497 t = day * (3600 * 24);
498 } else {
499 t = 0;
500 }
501
502 sec = 0;
503 for(;;) {
504 int val;
505 val = strtol(p, (char **)&p, 10);
506 sec = sec * 60 + val;
507 if (*p != ':')
508 break;
509 p++;
510 }
511 t = (t + sec) * 1000000;
512 if (*p == '.') {
513 int val, n;
514 p++;
515 n = strlen(p);
516 if (n > 6)
517 n = 6;
518 val = strtol(p, NULL, 10);
519 while (n < 6) {
520 val = val * 10;
521 n++;
522 }
523 t += val;
524 }
525 return t;
526 }
527
528 /* syntax: '?tag1=val1&tag2=val2...'. No URL decoding is done. Return
529 1 if found */
530 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
531 {
532 const char *p;
533 char tag[128], *q;
534
535 p = info;
536 if (*p == '?')
537 p++;
538 for(;;) {
539 q = tag;
540 while (*p != '\0' && *p != '=' && *p != '&') {
541 if ((q - tag) < sizeof(tag) - 1)
542 *q++ = *p;
543 p++;
544 }
545 *q = '\0';
546 q = arg;
547 if (*p == '=') {
548 p++;
549 while (*p != '&' && *p != '\0') {
550 if ((q - arg) < arg_size - 1)
551 *q++ = *p;
552 p++;
553 }
554 *q = '\0';
555 }
556 if (!strcmp(tag, tag1))
557 return 1;
558 if (*p != '&')
559 break;
560 }
561 return 0;
562 }
563
564 /* Return in 'buf' the path with '%d' replaced by number. Also handles
565 the '%0nd' format where 'n' is the total number of digits and
566 '%%'. Return 0 if OK, and -1 if format error */
567 int get_frame_filename(char *buf, int buf_size,
568 const char *path, int number)
569 {
570 const char *p;
571 char *q, buf1[20];
572 int nd, len, c, percentd_found;
573
574 q = buf;
575 p = path;
576 percentd_found = 0;
577 for(;;) {
578 c = *p++;
579 if (c == '\0')
580 break;
581 if (c == '%') {
582 nd = 0;
583 while (*p >= '0' && *p <= '9') {
584 nd = nd * 10 + *p++ - '0';
585 }
586 c = *p++;
587 switch(c) {
588 case '%':
589 goto addchar;
590 case 'd':
591 if (percentd_found)
592 goto fail;
593 percentd_found = 1;
594 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
595 len = strlen(buf1);
596 if ((q - buf + len) > buf_size - 1)
597 goto fail;
598 memcpy(q, buf1, len);
599 q += len;
600 break;
601 default:
602 goto fail;
603 }
604 } else {
605 addchar:
606 if ((q - buf) < buf_size - 1)
607 *q++ = c;
608 }
609 }
610 if (!percentd_found)
611 goto fail;
612 *q = '\0';
613 return 0;
614 fail:
615 *q = '\0';
616 return -1;
617 }
618
619 static int gcd(INT64 a, INT64 b)
620 {
621 INT64 c;
622
623 while (1) {
624 c = a % b;
625 if (c == 0)
626 return b;
627 a = b;
628 b = c;
629 }
630 }
631
632 void ticker_init(Ticker *tick, INT64 inrate, INT64 outrate)
633 {
634 int g;
635
636 g = gcd(inrate, outrate);
637 inrate /= g;
638 outrate /= g;
639
640 tick->value = -outrate/2;
641
642 tick->inrate = inrate;
643 tick->outrate = outrate;
644 tick->div = tick->outrate / tick->inrate;
645 tick->mod = tick->outrate % tick->inrate;
646 }