2 * Various utilities for ffmpeg system
3 * Copyright (c) 2000,2001 Gerard Lantau
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.
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.
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.
26 #define strcasecmp _stricmp
27 #include <sys/types.h>
28 #include <sys/timeb.h>
31 AVFormat *first_format;
33 void register_avformat(AVFormat *format)
37 while (*p != NULL) p = &(*p)->next;
42 int match_ext(const char *filename, const char *extensions)
47 ext = strrchr(filename, '.');
53 while (*p != '\0' && *p != ',')
56 if (!strcasecmp(ext1, ext))
66 AVFormat *guess_format(const char *short_name, const char *filename, const char *mime_type)
68 AVFormat *fmt, *fmt_found;
71 /* find the proper file type */
77 if (fmt->name && short_name && !strcmp(fmt->name, short_name))
79 if (fmt->mime_type && mime_type && !strcmp(fmt->mime_type, mime_type))
81 if (filename && fmt->extensions &&
82 match_ext(filename, fmt->extensions)) {
85 if (score > score_max) {
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)
112 void nstrcpy(char *buf, int buf_size, const char *str)
119 if (c == 0 || q >= buf + buf_size - 1)
126 void register_all(void)
129 avcodec_register_all();
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_format);
146 register_avformat(&rawvideo_format);
148 register_avformat(&ffm_format);
150 register_avformat(&pgm_format);
151 register_avformat(&ppm_format);
152 register_avformat(&pgmyuv_format);
153 register_avformat(&imgyuv_format);
154 register_avformat(&pgmpipe_format);
155 register_avformat(&pgmyuvpipe_format);
156 register_avformat(&ppmpipe_format);
158 register_protocol(&file_protocol);
159 register_protocol(&pipe_protocol);
161 register_protocol(&audio_protocol);
162 register_protocol(&video_protocol);
165 register_protocol(&udp_protocol);
166 register_protocol(&http_protocol);
170 /* memory handling */
172 int av_new_packet(AVPacket *pkt, int size)
174 pkt->data = malloc(size);
180 pkt->stream_index = 0;
185 void av_free_packet(AVPacket *pkt)
195 int fifo_init(FifoBuffer *f, int size)
197 f->buffer = malloc(size);
200 f->end = f->buffer + size;
201 f->wptr = f->rptr = f->buffer;
205 void fifo_free(FifoBuffer *f)
210 int fifo_size(FifoBuffer *f, UINT8 *rptr)
214 if (f->wptr >= rptr) {
215 size = f->wptr - rptr;
217 size = (f->end - rptr) + (f->wptr - f->buffer);
222 /* get data from the fifo (return -1 if not enough data) */
223 int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr)
225 UINT8 *rptr = *rptr_ptr;
228 if (f->wptr >= rptr) {
229 size = f->wptr - rptr;
231 size = (f->end - rptr) + (f->wptr - f->buffer);
236 while (buf_size > 0) {
240 memcpy(buf, rptr, len);
251 void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr)
260 memcpy(wptr, buf, len);
270 /* media file handling */
272 AVFormatContext *av_open_input_file(const char *filename, int buf_size)
274 AVFormatParameters params, *ap;
276 AVFormatContext *ic = NULL;
277 URLFormat url_format;
280 ic = av_mallocz(sizeof(AVFormatContext));
283 if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0)
287 url_setbufsize(&ic->pb, buf_size);
291 err = url_getformat(url_fileno(&ic->pb), &url_format);
293 fmt = guess_format(url_format.format_name, NULL, NULL);
295 ap->sample_rate = url_format.sample_rate;
296 ap->frame_rate = url_format.frame_rate;
297 ap->channels = url_format.channels;
298 ap->width = url_format.width;
299 ap->height = url_format.height;
300 ap->pix_fmt = url_format.pix_fmt;
302 fmt = guess_format(NULL, filename, NULL);
305 if (!fmt || !fmt->read_header) {
310 err = ic->format->read_header(ic, ap);
324 int av_read_packet(AVFormatContext *s, AVPacket *pkt)
328 pktl = s->packet_buffer;
330 /* read packet from packet buffer, if there is data */
332 s->packet_buffer = pktl->next;
336 return s->format->read_packet(s, pkt);
340 void av_close_input_file(AVFormatContext *s)
344 if (s->format->read_close)
345 s->format->read_close(s);
346 for(i=0;i<s->nb_streams;i++) {
349 if (s->packet_buffer) {
350 AVPacketList *p, *p1;
351 p = s->packet_buffer;
354 av_free_packet(&p->pkt);
358 s->packet_buffer = NULL;
365 int av_write_packet(AVFormatContext *s, AVPacket *pkt)
367 /* XXX: currently, an emulation because internal API must change */
368 return s->format->write_packet(s, pkt->stream_index, pkt->data, pkt->size);
371 /* "user interface" functions */
373 void dump_format(AVFormatContext *ic,
381 fprintf(stderr, "%s #%d, %s, %s '%s':\n",
382 is_output ? "Output" : "Input",
383 index, ic->format->name,
384 is_output ? "to" : "from", url);
385 for(i=0;i<ic->nb_streams;i++) {
386 AVStream *st = ic->streams[i];
387 avcodec_string(buf, sizeof(buf), &st->codec, is_output);
388 fprintf(stderr, " Stream #%d.%d: %s\n", index, i, buf);
397 static SizeEntry sizes[] = {
398 { "sqcif", 128, 96 },
399 { "qcif", 176, 144 },
401 { "4cif", 704, 576 },
404 int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
407 int n = sizeof(sizes) / sizeof(SizeEntry);
409 int frame_width = 0, frame_height = 0;
412 if (!strcmp(sizes[i].str, str)) {
413 frame_width = sizes[i].width;
414 frame_height = sizes[i].height;
420 frame_width = strtol(p, (char **)&p, 10);
423 frame_height = strtol(p, (char **)&p, 10);
425 if (frame_width <= 0 || frame_height <= 0)
427 *width_ptr = frame_width;
428 *height_ptr = frame_height;
437 return ((INT64)tb.time * INT64_C(1000) + (INT64)tb.millitm) * INT64_C(1000);
440 gettimeofday(&tv,NULL);
441 return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
445 /* syntax: [YYYY-MM-DD ][[HH:]MM:]SS[.m...] . Return the date in micro seconds since 1970 */
446 INT64 parse_date(const char *datestr, int duration)
454 static const UINT8 months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
455 int year, month, day, i;
457 if (strlen(p) >= 5 && p[4] == '-') {
459 year = strtol(p, (char **)&p, 10);
462 month = strtol(p, (char **)&p, 10) - 1;
465 day = strtol(p, (char **)&p, 10) - 1;
468 day += (year - 1970) * 365;
469 /* if >= March, take February of current year into account too */
472 for(i=1970;i<year;i++) {
473 if ((i % 100) == 0) {
474 if ((i % 400) == 0) day++;
475 } else if ((i % 4) == 0) {
482 day = (time(NULL) / (3600 * 24));
484 t = day * (3600 * 24);
492 val = strtol(p, (char **)&p, 10);
493 sec = sec * 60 + val;
498 t = (t + sec) * 1000000;
505 val = strtol(p, NULL, 10);
515 /* syntax: '?tag1=val1&tag2=val2...'. No URL decoding is done. Return
517 int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
527 while (*p != '\0' && *p != '=' && *p != '&') {
528 if ((q - tag) < sizeof(tag) - 1)
536 while (*p != '&' && *p != '\0') {
537 if ((q - arg) < arg_size - 1)
543 if (!strcmp(tag, tag1))