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