Statistics
| Branch: | Revision:

ffmpeg / libav / utils.c @ d771bcae

History | View | Annotate | Download (12.4 KB)

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

    
30
AVFormat *first_format;
31

    
32
void 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

    
41
int 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

    
65
AVFormat *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 */
95
int 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

    
111
void 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

    
125
void register_all(void)
126
{
127
    avcodec_init();
128
    avcodec_register_all();
129

    
130
    register_avformat(&mp2_format);
131
    register_avformat(&ac3_format);
132
    register_avformat(&mpeg_mux_format);
133
    register_avformat(&mpeg1video_format);
134
    register_avformat(&h263_format);
135
    register_avformat(&rm_format);
136
    register_avformat(&asf_format);
137
    register_avformat(&avi_format);
138
    register_avformat(&mpjpeg_format);
139
    register_avformat(&jpeg_format);
140
    register_avformat(&swf_format);
141
    register_avformat(&wav_format);
142
    register_avformat(&pcm_format);
143
    register_avformat(&rawvideo_format);
144
    register_avformat(&ffm_format);
145
    register_avformat(&pgm_format);
146
    register_avformat(&pgmyuv_format);
147
    register_avformat(&imgyuv_format);
148
    register_avformat(&pgmpipe_format);
149

    
150
    register_protocol(&file_protocol);
151
    register_protocol(&pipe_protocol);
152
    register_protocol(&audio_protocol);
153
    register_protocol(&video_protocol);
154
    register_protocol(&udp_protocol);
155
    register_protocol(&http_protocol);
156
}
157

    
158
/* memory handling */
159

    
160
int av_new_packet(AVPacket *pkt, int size)
161
{
162
    pkt->data = malloc(size);
163
    if (!pkt->data)
164
        return -ENOMEM;
165
    pkt->size = size;
166
    /* sane state */
167
    pkt->pts = 0;
168
    pkt->stream_index = 0;
169
    pkt->flags = 0;
170
    return 0;
171
}
172

    
173
void av_free_packet(AVPacket *pkt)
174
{
175
    free(pkt->data);
176
    /* fail safe */
177
    pkt->data = NULL;
178
    pkt->size = 0;
179
}
180

    
181
/* fifo handling */
182

    
183
int fifo_init(FifoBuffer *f, int size)
184
{
185
    f->buffer = malloc(size);
186
    if (!f->buffer)
187
        return -1;
188
    f->end = f->buffer + size;
189
    f->wptr = f->rptr = f->buffer;
190
    return 0;
191
}
192

    
193
void fifo_free(FifoBuffer *f)
194
{
195
    free(f->buffer);
196
}
197

    
198
int fifo_size(FifoBuffer *f, UINT8 *rptr)
199
{
200
    int size;
201

    
202
    if (f->wptr >= rptr) {
203
        size = f->wptr - rptr;
204
    } else {
205
        size = (f->end - rptr) + (f->wptr - f->buffer);
206
    }
207
    return size;
208
}
209

    
210
/* get data from the fifo (return -1 if not enough data) */
211
int fifo_read(FifoBuffer *f, UINT8 *buf, int buf_size, UINT8 **rptr_ptr)
212
{
213
    UINT8 *rptr = *rptr_ptr;
214
    int size, len;
215

    
216
    if (f->wptr >= rptr) {
217
        size = f->wptr - rptr;
218
    } else {
219
        size = (f->end - rptr) + (f->wptr - f->buffer);
220
    }
221
    
222
    if (size < buf_size)
223
        return -1;
224
    while (buf_size > 0) {
225
        len = f->end - rptr;
226
        if (len > buf_size)
227
            len = buf_size;
228
        memcpy(buf, rptr, len);
229
        buf += len;
230
        rptr += len;
231
        if (rptr >= f->end)
232
            rptr = f->buffer;
233
        buf_size -= len;
234
    }
235
    *rptr_ptr = rptr;
236
    return 0;
237
}
238

    
239
void fifo_write(FifoBuffer *f, UINT8 *buf, int size, UINT8 **wptr_ptr)
240
{
241
    int len;
242
    UINT8 *wptr;
243
    wptr = *wptr_ptr;
244
    while (size > 0) {
245
        len = f->end - wptr;
246
        if (len > size)
247
            len = size;
248
        memcpy(wptr, buf, len);
249
        wptr += len;
250
        if (wptr >= f->end)
251
            wptr = f->buffer;
252
        buf += len;
253
        size -= len;
254
    }
255
    *wptr_ptr = wptr;
256
}
257

    
258
/* media file handling */
259

    
260
AVFormatContext *av_open_input_file(const char *filename, int buf_size)
261
{
262
    AVFormatParameters params, *ap;
263
    AVFormat *fmt;
264
    AVFormatContext *ic = NULL;
265
    URLFormat url_format;
266
    int err;
267

    
268
    ic = av_mallocz(sizeof(AVFormatContext));
269
    if (!ic)
270
        goto fail;
271
    if (url_fopen(&ic->pb, filename, URL_RDONLY) < 0)
272
        goto fail;
273
    
274
    if (buf_size > 0) {
275
        url_setbufsize(&ic->pb, buf_size);
276
    }
277

    
278
    /* find format */
279
    err = url_getformat(url_fileno(&ic->pb), &url_format);
280
    if (err >= 0) {
281
        fmt = guess_format(url_format.format_name, NULL, NULL);
282
        ap = &params;
283
        ap->sample_rate = url_format.sample_rate;
284
        ap->frame_rate = url_format.frame_rate;
285
        ap->channels = url_format.channels;
286
        ap->width = url_format.width;
287
        ap->height = url_format.height;
288
        ap->pix_fmt = url_format.pix_fmt;
289
    } else {
290
        fmt = guess_format(NULL, filename, NULL);
291
        ap = NULL;
292
    }
293
    if (!fmt || !fmt->read_header) {
294
        return NULL;
295
    }
296
    ic->format = fmt;
297

    
298
    err = ic->format->read_header(ic, ap);
299
    if (err < 0) {
300
        url_fclose(&ic->pb);
301
        goto fail;
302
    }
303
    
304
    return ic;
305

    
306
 fail:
307
    if (ic)
308
        free(ic);
309
    return NULL;
310
}
311

    
312
int av_read_packet(AVFormatContext *s, AVPacket *pkt)
313
{
314
    AVPacketList *pktl;
315

    
316
    pktl = s->packet_buffer;
317
    if (pktl) {
318
        /* read packet from packet buffer, if there is data */
319
        *pkt = pktl->pkt;
320
        s->packet_buffer = pktl->next;
321
        free(pktl);
322
        return 0;
323
    } else {
324
        return s->format->read_packet(s, pkt);
325
    }
326
}
327

    
328
void av_close_input_file(AVFormatContext *s)
329
{
330
    int i;
331

    
332
    if (s->format->read_close)
333
        s->format->read_close(s);
334
    for(i=0;i<s->nb_streams;i++) {
335
        free(s->streams[i]);
336
    }
337
    if (s->packet_buffer) {
338
        AVPacketList *p, *p1;
339
        p = s->packet_buffer;
340
        while (p != NULL) {
341
            p1 = p->next;
342
            av_free_packet(&p->pkt);
343
            free(p);
344
            p = p1;
345
        }
346
        s->packet_buffer = NULL;
347
    }
348
    url_fclose(&s->pb);
349
    free(s);
350
}
351

    
352

    
353
int av_write_packet(AVFormatContext *s, AVPacket *pkt)
354
{
355
    /* XXX: currently, an emulation because internal API must change */
356
    return s->format->write_packet(s, pkt->stream_index, pkt->data, pkt->size);
357
}
358

    
359
/* "user interface" functions */
360

    
361
void dump_format(AVFormatContext *ic,
362
                 int index, 
363
                 const char *url,
364
                 int is_output)
365
{
366
    int i;
367
    char buf[256];
368

    
369
    fprintf(stderr, "%s #%d, %s, %s '%s':\n", 
370
            is_output ? "Output" : "Input",
371
            index, ic->format->name, 
372
            is_output ? "to" : "from", url);
373
    for(i=0;i<ic->nb_streams;i++) {
374
        AVStream *st = ic->streams[i];
375
        avcodec_string(buf, sizeof(buf), &st->codec, is_output);
376
        fprintf(stderr, "  Stream #%d.%d: %s\n", index, i, buf);
377
    }
378
}
379

    
380
typedef struct {
381
    const char *str;
382
    int width, height;
383
} SizeEntry;
384

    
385
static SizeEntry sizes[] = {
386
    { "sqcif", 128, 96 },
387
    { "qcif", 176, 144 },
388
    { "cif", 352, 288 },
389
    { "4cif", 704, 576 },
390
};
391
    
392
int parse_image_size(int *width_ptr, int *height_ptr, const char *str)
393
{
394
    int i;
395
    int n = sizeof(sizes) / sizeof(SizeEntry);
396
    const char *p;
397
    int frame_width = 0, frame_height = 0;
398

    
399
    for(i=0;i<n;i++) {
400
        if (!strcmp(sizes[i].str, str)) {
401
            frame_width = sizes[i].width;
402
            frame_height = sizes[i].height;
403
            break;
404
        }
405
    }
406
    if (i == n) {
407
        p = str;
408
        frame_width = strtol(p, (char **)&p, 10);
409
        if (*p)
410
            p++;
411
        frame_height = strtol(p, (char **)&p, 10);
412
    }
413
    if (frame_width <= 0 || frame_height <= 0)
414
        return -1;
415
    *width_ptr = frame_width;
416
    *height_ptr = frame_height;
417
    return 0;
418
}
419

    
420
INT64 gettime(void)
421
{
422
    struct timeval tv;
423
    gettimeofday(&tv,NULL);
424
    return (INT64)tv.tv_sec * 1000000 + tv.tv_usec;
425
}
426

    
427
/* syntax: [YYYY-MM-DD ][[HH:]MM:]SS[.m...] . Return the date in micro seconds since 1970 */
428
INT64 parse_date(const char *datestr, int duration)
429
{
430
    const char *p;
431
    INT64 t;
432
    int sec;
433

    
434
    p = datestr;
435
    if (!duration) {
436
        static const UINT8 months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
437
        int year, month, day, i;
438

    
439
        if (strlen(p) >= 5 && p[4] == '-') {
440
            
441
            year = strtol(p, (char **)&p, 10);
442
            if (*p)
443
                p++;
444
            month = strtol(p, (char **)&p, 10) - 1;
445
            if (*p)
446
                p++;
447
            day = strtol(p, (char **)&p, 10) - 1;
448
            if (*p)
449
                p++;
450
            day += (year - 1970) * 365;
451
            /* if >= March, take February of current year into account too */
452
            if (month >= 2)
453
                year++;
454
            for(i=1970;i<year;i++) {
455
                if ((i % 100) == 0) {
456
                    if ((i % 400) == 0) day++;
457
                } else if ((i % 4) == 0) {
458
                    day++;
459
                }
460
            }
461
            for(i=0;i<month;i++)
462
                day += months[i];
463
        } else {
464
            day = (time(NULL) / (3600 * 24));
465
        }
466
        t = day * (3600 * 24);
467
    } else {
468
        t = 0;
469
    }
470
    
471
    sec = 0;
472
    for(;;) {
473
        int val;
474
        val = strtol(p, (char **)&p, 10);
475
        sec = sec * 60 + val;
476
        if (*p != ':')
477
            break;
478
        p++;
479
    }
480
    t = (t + sec) * 1000000;
481
    if (*p == '.') {
482
        int val, n;
483
        p++;
484
        n = strlen(p);
485
        if (n > 6)
486
            n = 6;
487
        val = strtol(p, NULL, 10);
488
        while (n < 6) {
489
            val = val * 10;
490
            n++;
491
        }
492
        t += val;
493
    }
494
    return t;
495
}
496

    
497
/* syntax: '?tag1=val1&tag2=val2...'. No URL decoding is done. Return
498
   1 if found */
499
int find_info_tag(char *arg, int arg_size, const char *tag1, const char *info)
500
{
501
    const char *p;
502
    char tag[128], *q;
503

    
504
    p = info;
505
    if (*p == '?')
506
        p++;
507
    for(;;) {
508
        q = tag;
509
        while (*p != '\0' && *p != '=' && *p != '&') {
510
            if ((q - tag) < sizeof(tag) - 1)
511
                *q++ = *p;
512
            p++;
513
        }
514
        *q = '\0';
515
        q = arg;
516
        if (*p == '=') {
517
            p++;
518
            while (*p != '&' && *p != '\0') {
519
                if ((q - arg) < arg_size - 1)
520
                    *q++ = *p;
521
                p++;
522
            }
523
            *q = '\0';
524
        }
525
        if (!strcmp(tag, tag1)) 
526
            return 1;
527
        if (*p != '&')
528
            break;
529
    }
530
    return 0;
531
}
532