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