added first version of MPEG/DVB transport stream demux
[libav.git] / libav / utils.c
CommitLineData
de6d9b64
FB
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 */
8be1c656 19#include "avformat.h"
af469427 20#include "tick.h"
8be1c656 21#ifndef CONFIG_WIN32
de6d9b64
FB
22#include <unistd.h>
23#include <fcntl.h>
de6d9b64
FB
24#include <sys/time.h>
25#include <time.h>
8be1c656
FB
26#else
27#define strcasecmp _stricmp
28#include <sys/types.h>
29#include <sys/timeb.h>
30#endif
de6d9b64
FB
31
32AVFormat *first_format;
33
34void 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
43int 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
67AVFormat *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 */
97int 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
113void 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
8d1335ea
PG
127void strlcpy(char *dst, const char *src, int len)
128{
129 int slen = strlen(src) + 1;
130
131 if (slen <= len) {
132 memcpy(dst, src, slen);
133 } else {
134 memcpy(dst, src, len - 1);
135 dst[len - 1] = 0;
136 }
137}
138
de6d9b64
FB
139void register_all(void)
140{
141 avcodec_init();
142 avcodec_register_all();
de6d9b64
FB
143
144 register_avformat(&mp2_format);
145 register_avformat(&ac3_format);
146 register_avformat(&mpeg_mux_format);
147 register_avformat(&mpeg1video_format);
27e084bd 148 register_avformat(&mjpeg_format);
de6d9b64
FB
149 register_avformat(&h263_format);
150 register_avformat(&rm_format);
151 register_avformat(&asf_format);
152 register_avformat(&avi_format);
6cea494e
ZK
153 register_avformat(&mov_format);
154 register_avformat(&mp4_format);
de6d9b64
FB
155 register_avformat(&mpjpeg_format);
156 register_avformat(&jpeg_format);
27e084bd 157 register_avformat(&single_jpeg_format);
de6d9b64 158 register_avformat(&swf_format);
6cea494e
ZK
159 register_avformat(&gif_format);
160 register_avformat(&au_format);
de6d9b64 161 register_avformat(&wav_format);
1ea4f593
FB
162 register_avformat(&crc_format);
163
5ed8fafc
FB
164 register_avformat(&pcm_s16le_format);
165 register_avformat(&pcm_s16be_format);
166 register_avformat(&pcm_u16le_format);
167 register_avformat(&pcm_u16be_format);
168 register_avformat(&pcm_s8_format);
169 register_avformat(&pcm_u8_format);
170 register_avformat(&pcm_mulaw_format);
171 register_avformat(&pcm_alaw_format);
de6d9b64 172 register_avformat(&rawvideo_format);
8be1c656 173#ifndef CONFIG_WIN32
de6d9b64 174 register_avformat(&ffm_format);
8be1c656 175#endif
de6d9b64 176 register_avformat(&pgm_format);
6775c758 177 register_avformat(&ppm_format);
de6d9b64
FB
178 register_avformat(&pgmyuv_format);
179 register_avformat(&imgyuv_format);
180 register_avformat(&pgmpipe_format);
6775c758
FB
181 register_avformat(&pgmyuvpipe_format);
182 register_avformat(&ppmpipe_format);
96baaa6a
FB
183#ifdef CONFIG_GRAB
184 register_avformat(&video_grab_device_format);
185 register_avformat(&audio_device_format);
186#endif
de6d9b64 187
96baaa6a 188 /* file protocols */
de6d9b64
FB
189 register_protocol(&file_protocol);
190 register_protocol(&pipe_protocol);
8be1c656 191#ifndef CONFIG_WIN32
de6d9b64
FB
192 register_protocol(&udp_protocol);
193 register_protocol(&http_protocol);
8be1c656 194#endif
de6d9b64
FB
195}
196
197/* memory handling */
198
199int av_new_packet(AVPacket *pkt, int size)
200{
1ea4f593 201 pkt->data = av_malloc(size);
de6d9b64
FB
202 if (!pkt->data)
203 return -ENOMEM;
204 pkt->size = size;
205 /* sane state */
206 pkt->pts = 0;
207 pkt->stream_index = 0;
208 pkt->flags = 0;
209 return 0;
210}
211
212void av_free_packet(AVPacket *pkt)
213{
1ea4f593 214 av_freep(&pkt->data);
de6d9b64 215 /* fail safe */
de6d9b64
FB
216 pkt->size = 0;
217}
218
219/* fifo handling */
220
221int fifo_init(FifoBuffer *f, int size)
222{
1ea4f593 223 f->buffer = av_malloc(size);
de6d9b64
FB
224 if (!f->buffer)
225 return -1;
226 f->end = f->buffer + size;
227 f->wptr = f->rptr = f->buffer;
228 return 0;
229}
230
231void fifo_free(FifoBuffer *f)
232{
1ea4f593 233 av_free(f->buffer);
de6d9b64
FB
234}
235
236int fifo_size(FifoBuffer *f, UINT8 *rptr)
237{
238 int size;
239
240 if (f->wptr >= rptr) {
241 size = f->wptr - rptr;
242 } else {
243 size = (f->end - rptr) + (f->wptr - f->buffer);
244 }
245 return size;
246}
247
248/* get data from the fifo (return -1 if not enough data) */
249int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr)
250{
251 UINT8 *rptr = *rptr_ptr;
252 int size, len;
253
254 if (f->wptr >= rptr) {
255 size = f->wptr - rptr;
256 } else {
257 size = (f->end - rptr) + (f->wptr - f->buffer);
258 }
259
260 if (size < buf_size)
261 return -1;
262 while (buf_size > 0) {
263 len = f->end - rptr;
264 if (len > buf_size)
265 len = buf_size;
266 memcpy(buf, rptr, len);
267 buf += len;
268 rptr += len;
269 if (rptr >= f->end)
270 rptr = f->buffer;
271 buf_size -= len;
272 }
273 *rptr_ptr = rptr;
274 return 0;
275}
276
277void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr)
278{
279 int len;
280 UINT8 *wptr;
281 wptr = *wptr_ptr;
282 while (size > 0) {
283 len = f->end - wptr;
284 if (len > size)
285 len = size;
286 memcpy(wptr, buf, len);
287 wptr += len;
288 if (wptr >= f->end)
289 wptr = f->buffer;
290 buf += len;
291 size -= len;
292 }
293 *wptr_ptr = wptr;
294}
295
96baaa6a
FB
296/* media file handling.
297 'filename' is the filename to open.
298 'format_name' is used to force the file format (NULL if auto guess).
299 'buf_size' is the optional buffer size (zero if default is OK).
300 'ap' are additionnal parameters needed when opening the file (NULL if default).
301*/
302
303AVFormatContext *av_open_input_file(const char *filename,
304 const char *format_name,
305 int buf_size,
306 AVFormatParameters *ap)
de6d9b64 307{
de6d9b64
FB
308 AVFormat *fmt;
309 AVFormatContext *ic = NULL;
de6d9b64
FB
310 int err;
311
312 ic = av_mallocz(sizeof(AVFormatContext));
313 if (!ic)
314 goto fail;
de6d9b64
FB
315
316 /* find format */
96baaa6a
FB
317 if (format_name != NULL) {
318 fmt = guess_format(format_name, NULL, NULL);
de6d9b64
FB
319 } else {
320 fmt = guess_format(NULL, filename, NULL);
de6d9b64
FB
321 }
322 if (!fmt || !fmt->read_header) {
323 return NULL;
324 }
325 ic->format = fmt;
326
96baaa6a
FB
327 /* if no file needed do not try to open one */
328 if (!(fmt->flags & AVFMT_NOFILE)) {
329 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0)
330 goto fail;
331 if (buf_size > 0) {
332 url_setbufsize(&ic->pb, buf_size);
333 }
334 }
335
de6d9b64
FB
336 err = ic->format->read_header(ic, ap);
337 if (err < 0) {
96baaa6a
FB
338 if (!(fmt->flags & AVFMT_NOFILE)) {
339 url_fclose(&ic->pb);
340 }
de6d9b64
FB
341 goto fail;
342 }
343
344 return ic;
345
346 fail:
1ea4f593 347 av_free(ic);
de6d9b64
FB
348 return NULL;
349}
350
351int av_read_packet(AVFormatContext *s, AVPacket *pkt)
352{
353 AVPacketList *pktl;
354
355 pktl = s->packet_buffer;
356 if (pktl) {
357 /* read packet from packet buffer, if there is data */
358 *pkt = pktl->pkt;
359 s->packet_buffer = pktl->next;
1ea4f593 360 av_free(pktl);
de6d9b64
FB
361 return 0;
362 } else {
363 return s->format->read_packet(s, pkt);
364 }
365}
366
367void av_close_input_file(AVFormatContext *s)
368{
369 int i;
370
371 if (s->format->read_close)
372 s->format->read_close(s);
373 for(i=0;i<s->nb_streams;i++) {
1ea4f593 374 av_free(s->streams[i]);
de6d9b64
FB
375 }
376 if (s->packet_buffer) {
377 AVPacketList *p, *p1;
378 p = s->packet_buffer;
379 while (p != NULL) {
380 p1 = p->next;
381 av_free_packet(&p->pkt);
1ea4f593 382 av_free(p);
de6d9b64
FB
383 p = p1;
384 }
385 s->packet_buffer = NULL;
386 }
96baaa6a
FB
387 if (!(s->format->flags & AVFMT_NOFILE)) {
388 url_fclose(&s->pb);
389 }
1ea4f593 390 av_free(s);
de6d9b64
FB
391}
392
393
10bb7023 394int av_write_packet(AVFormatContext *s, AVPacket *pkt, int force_pts)
de6d9b64
FB
395{
396 /* XXX: currently, an emulation because internal API must change */
10bb7023 397 return s->format->write_packet(s, pkt->stream_index, pkt->data, pkt->size, force_pts);
de6d9b64
FB
398}
399
400/* "user interface" functions */
401
402void dump_format(AVFormatContext *ic,
403 int index,
404 const char *url,
405 int is_output)
406{
407 int i;
408 char buf[256];
409
410 fprintf(stderr, "%s #%d, %s, %s '%s':\n",
411 is_output ? "Output" : "Input",
412 index, ic->format->name,
413 is_output ? "to" : "from", url);
414 for(i=0;i<ic->nb_streams;i++) {
415 AVStream *st = ic->streams[i];
416 avcodec_string(buf, sizeof(buf), &st->codec, is_output);
417 fprintf(stderr, " Stream #%d.%d: %s\n", index, i, buf);
418 }
419}
420
421typedef struct {
422 const char *str;
423 int width, height;
424} SizeEntry;
425
426static SizeEntry sizes[] = {
427 { "sqcif", 128, 96 },
428 { "qcif", 176, 144 },
429 { "cif", 352, 288 },
430 { "4cif", 704, 576 },
431};
432
433int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
434{
435 int i;
436 int n = sizeof(sizes) / sizeof(SizeEntry);
437 const char *p;
438 int frame_width = 0, frame_height = 0;
439
440 for(i=0;i<n;i++) {
441 if (!strcmp(sizes[i].str, str)) {
442 frame_width = sizes[i].width;
443 frame_height = sizes[i].height;
444 break;
445 }
446 }
447 if (i == n) {
448 p = str;
449 frame_width = strtol(p, (char **)&p, 10);
450 if (*p)
451 p++;
452 frame_height = strtol(p, (char **)&p, 10);
453 }
454 if (frame_width <= 0 || frame_height <= 0)
455 return -1;
456 *width_ptr = frame_width;
457 *height_ptr = frame_height;
458 return 0;
459}
460
461INT64 gettime(void)
462{
8be1c656
FB
463#ifdef CONFIG_WIN32
464 struct _timeb tb;
465 _ftime(&tb);
466 return ((INT64)tb.time * INT64_C(1000) + (INT64)tb.millitm) * INT64_C(1000);
467#else
de6d9b64
FB
468 struct timeval tv;
469 gettimeofday(&tv,NULL);
470 return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
8be1c656 471#endif
de6d9b64
FB
472}
473
474/* syntax: [YYYY-MM-DD ][[HH:]MM:]SS[.m...] . Return the date in micro seconds since 1970 */
475INT64 parse_date(const char *datestr, int duration)
476{
477 const char *p;
478 INT64 t;
479 int sec;
480
481 p = datestr;
482 if (!duration) {
483 static const UINT8 months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
484 int year, month, day, i;
485
486 if (strlen(p) >= 5 && p[4] == '-') {
487
488 year = strtol(p, (char **)&p, 10);
489 if (*p)
490 p++;
491 month = strtol(p, (char **)&p, 10) - 1;
492 if (*p)
493 p++;
494 day = strtol(p, (char **)&p, 10) - 1;
495 if (*p)
496 p++;
497 day += (year - 1970) * 365;
498 /* if >= March, take February of current year into account too */
499 if (month >= 2)
500 year++;
501 for(i=1970;i<year;i++) {
502 if ((i % 100) == 0) {
503 if ((i % 400) == 0) day++;
504 } else if ((i % 4) == 0) {
505 day++;
506 }
507 }
508 for(i=0;i<month;i++)
509 day += months[i];
510 } else {
511 day = (time(NULL) / (3600 * 24));
512 }
513 t = day * (3600 * 24);
514 } else {
515 t = 0;
516 }
517
518 sec = 0;
519 for(;;) {
520 int val;
521 val = strtol(p, (char **)&p, 10);
522 sec = sec * 60 + val;
523 if (*p != ':')
524 break;
525 p++;
526 }
527 t = (t + sec) * 1000000;
528 if (*p == '.') {
529 int val, n;
530 p++;
531 n = strlen(p);
532 if (n > 6)
533 n = 6;
534 val = strtol(p, NULL, 10);
535 while (n < 6) {
536 val = val * 10;
537 n++;
538 }
539 t += val;
540 }
541 return t;
542}
543
544/* syntax: '?tag1=val1&tag2=val2...'. No URL decoding is done. Return
545 1 if found */
546int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
547{
548 const char *p;
549 char tag[128], *q;
550
551 p = info;
552 if (*p == '?')
553 p++;
554 for(;;) {
555 q = tag;
556 while (*p != '\0' && *p != '=' && *p != '&') {
557 if ((q - tag) < sizeof(tag) - 1)
558 *q++ = *p;
559 p++;
560 }
561 *q = '\0';
562 q = arg;
563 if (*p == '=') {
564 p++;
565 while (*p != '&' && *p != '\0') {
566 if ((q - arg) < arg_size - 1)
567 *q++ = *p;
568 p++;
569 }
570 *q = '\0';
571 }
572 if (!strcmp(tag, tag1))
573 return 1;
574 if (*p != '&')
575 break;
8d1335ea 576 p++;
de6d9b64
FB
577 }
578 return 0;
579}
580
9150f42e
FB
581/* Return in 'buf' the path with '%d' replaced by number. Also handles
582 the '%0nd' format where 'n' is the total number of digits and
583 '%%'. Return 0 if OK, and -1 if format error */
584int get_frame_filename(char *buf, int buf_size,
585 const char *path, int number)
586{
587 const char *p;
588 char *q, buf1[20];
589 int nd, len, c, percentd_found;
590
591 q = buf;
592 p = path;
593 percentd_found = 0;
594 for(;;) {
595 c = *p++;
596 if (c == '\0')
597 break;
598 if (c == '%') {
599 nd = 0;
600 while (*p >= '0' && *p <= '9') {
601 nd = nd * 10 + *p++ - '0';
602 }
603 c = *p++;
604 switch(c) {
605 case '%':
606 goto addchar;
607 case 'd':
608 if (percentd_found)
609 goto fail;
610 percentd_found = 1;
611 snprintf(buf1, sizeof(buf1), "%0*d", nd, number);
612 len = strlen(buf1);
613 if ((q - buf + len) > buf_size - 1)
614 goto fail;
615 memcpy(q, buf1, len);
616 q += len;
617 break;
618 default:
619 goto fail;
620 }
621 } else {
622 addchar:
623 if ((q - buf) < buf_size - 1)
624 *q++ = c;
625 }
626 }
627 if (!percentd_found)
628 goto fail;
629 *q = '\0';
630 return 0;
631 fail:
632 *q = '\0';
633 return -1;
634}
635
af469427
J
636static int gcd(INT64 a, INT64 b)
637{
638 INT64 c;
639
640 while (1) {
641 c = a % b;
642 if (c == 0)
643 return b;
644 a = b;
645 b = c;
646 }
647}
648
649void ticker_init(Ticker *tick, INT64 inrate, INT64 outrate)
650{
651 int g;
652
653 g = gcd(inrate, outrate);
654 inrate /= g;
655 outrate /= g;
9150f42e 656
af469427
J
657 tick->value = -outrate/2;
658
659 tick->inrate = inrate;
660 tick->outrate = outrate;
661 tick->div = tick->outrate / tick->inrate;
662 tick->mod = tick->outrate % tick->inrate;
663}