Statistics
| Branch: | Revision:

ffmpeg / libavformat / avidec.c @ 8c653280

History | View | Annotate | Download (13.9 KB)

1
/*
2
 * AVI decoder.
3
 * Copyright (c) 2001 Fabrice Bellard.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 2 of the License, or (at your option) any later version.
9
 *
10
 * This library 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 GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 */
19
#include "avformat.h"
20
#include "avi.h"
21

    
22
//#define DEBUG
23

    
24
static const struct AVI1Handler {
25
   enum CodecID vcid;
26
   enum CodecID acid;
27
   uint32_t tag;
28
} AVI1Handlers[] = {
29
  { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 's', 'd') },
30
  { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 'h', 'd') },
31
  { CODEC_ID_DVVIDEO, CODEC_ID_DVAUDIO, MKTAG('d', 'v', 's', 'l') },
32
  /* This is supposed to be the last one */
33
  { CODEC_ID_NONE, CODEC_ID_NONE, 0 },
34
};
35

    
36
typedef struct AVIIndex {
37
    unsigned char tag[4];
38
    unsigned int flags, pos, len;
39
    struct AVIIndex *next;
40
} AVIIndex;
41

    
42
typedef struct {
43
    int64_t riff_end;
44
    int64_t movi_end;
45
    int     type;
46
    uint8_t *buf;
47
    int      buf_size;
48
    int      stream_index;
49
    offset_t movi_list;
50
    AVIIndex *first, *last;
51
} AVIContext;
52

    
53
#ifdef DEBUG
54
static void print_tag(const char *str, unsigned int tag, int size)
55
{
56
    printf("%s: tag=%c%c%c%c size=0x%x\n",
57
           str, tag & 0xff,
58
           (tag >> 8) & 0xff,
59
           (tag >> 16) & 0xff,
60
           (tag >> 24) & 0xff,
61
           size);
62
}
63
#endif
64

    
65
static int get_riff(AVIContext *avi, ByteIOContext *pb)
66
{
67
    uint32_t tag; 
68
    /* check RIFF header */
69
    tag = get_le32(pb);
70

    
71
    if (tag != MKTAG('R', 'I', 'F', 'F'))
72
        return -1;
73
    avi->riff_end = get_le32(pb);   /* RIFF chunk size */
74
    avi->riff_end += url_ftell(pb); /* RIFF chunk end */
75
    tag = get_le32(pb);
76
    if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
77
        return -1;
78
    
79
    return 0;
80
}
81

    
82
static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
83
{
84
    AVIContext *avi = s->priv_data;
85
    ByteIOContext *pb = &s->pb;
86
    uint32_t tag, tag1, handler;
87
    int codec_type, stream_index, frame_period, bit_rate, scale, rate;
88
    unsigned int size, nb_frames;
89
    int i, n;
90
    AVStream *st;
91
    int xan_video = 0;  /* hack to support Xan A/V */
92

    
93
    if (get_riff(avi, pb) < 0)
94
        return -1;
95

    
96
    /* first list tag */
97
    stream_index = -1;
98
    codec_type = -1;
99
    frame_period = 0;
100
    avi->type = 2;
101
    avi->buf = av_malloc(1);
102
    if (!avi->buf)
103
        return -1;
104
    avi->buf_size = 1;
105
    for(;;) {
106
        if (url_feof(pb))
107
            goto fail;
108
        tag = get_le32(pb);
109
        size = get_le32(pb);
110
#ifdef DEBUG
111
        print_tag("tag", tag, size);
112
#endif
113

    
114
        switch(tag) {
115
        case MKTAG('L', 'I', 'S', 'T'):
116
            /* ignored, except when start of video packets */
117
            tag1 = get_le32(pb);
118
#ifdef DEBUG
119
            print_tag("list", tag1, 0);
120
#endif
121
            if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
122
                avi->movi_end = url_ftell(pb) + size - 4;
123
#ifdef DEBUG
124
                printf("movi end=%Lx\n", avi->movi_end);
125
#endif
126
                goto end_of_header;
127
            }
128
            break;
129
        case MKTAG('a', 'v', 'i', 'h'):
130
            /* avi header */
131
            /* using frame_period is bad idea */
132
            frame_period = get_le32(pb);
133
            bit_rate = get_le32(pb) * 8;
134
            url_fskip(pb, 4 * 4);
135
            n = get_le32(pb);
136
            for(i=0;i<n;i++) {
137
                st = av_new_stream(s, 0);
138
                if (!st)
139
                    goto fail;
140
            }
141
            url_fskip(pb, size - 7 * 4);
142
            break;
143
        case MKTAG('s', 't', 'r', 'h'):
144
            /* stream header */
145
            stream_index++;
146
            tag1 = get_le32(pb);
147
            switch(tag1) {
148
            case MKTAG('i', 'a', 'v', 's'):
149
            case MKTAG('i', 'v', 'a', 's'):
150
                if (s->nb_streams != 1)
151
                    goto fail;
152
                avi->type = 1;
153
                avi->stream_index = 0;
154
            case MKTAG('v', 'i', 'd', 's'):
155
                codec_type = CODEC_TYPE_VIDEO;
156

    
157
                if (stream_index >= s->nb_streams) {
158
                    url_fskip(pb, size - 4);
159
                    break;
160
                } 
161

    
162
                st = s->streams[stream_index];
163

    
164
                handler = get_le32(pb); /* codec tag */
165
                get_le32(pb); /* flags */
166
                get_le16(pb); /* priority */
167
                get_le16(pb); /* language */
168
                get_le32(pb); /* XXX: initial frame ? */
169
                scale = get_le32(pb); /* scale */
170
                rate = get_le32(pb); /* rate */
171

    
172
                if(scale && rate){
173
                    st->codec.frame_rate = rate;
174
                    st->codec.frame_rate_base = scale;
175
                }else if(frame_period){
176
                    st->codec.frame_rate = 1000000;
177
                    st->codec.frame_rate_base = frame_period;
178
                }else{
179
                    st->codec.frame_rate = 25;
180
                    st->codec.frame_rate_base = 1;
181
                }
182
                get_le32(pb); /* start */
183
                nb_frames = get_le32(pb);
184
                st->start_time = 0;
185
                st->duration = (double)nb_frames * 
186
                    st->codec.frame_rate_base * AV_TIME_BASE / 
187
                    st->codec.frame_rate;
188
                
189
                if (avi->type == 1) {
190
                    AVStream *st;
191

    
192
                    st = av_new_stream(s, 0);
193
                    if (!st)
194
                        goto fail;
195
                    
196
                    stream_index++;
197
                    
198
                    for (i=0; AVI1Handlers[i].tag != 0; ++i)
199
                       if (AVI1Handlers[i].tag == handler)
200
                           break;
201

    
202
                    if (AVI1Handlers[i].tag != 0) {
203
                        s->streams[0]->codec.codec_type = CODEC_TYPE_VIDEO;
204
                        s->streams[0]->codec.codec_id   = AVI1Handlers[i].vcid;
205
                        s->streams[1]->codec.codec_type = CODEC_TYPE_AUDIO;
206
                        s->streams[1]->codec.codec_id   = AVI1Handlers[i].acid;
207
                    } else {
208
                        goto fail;
209
                    }
210
                }
211
                
212
                url_fskip(pb, size - 9 * 4);
213
                break;
214
            case MKTAG('a', 'u', 'd', 's'):
215
                {
216
                    unsigned int length, rate;
217

    
218
                    codec_type = CODEC_TYPE_AUDIO;
219

    
220
                    if (stream_index >= s->nb_streams) {
221
                        url_fskip(pb, size - 4);
222
                        break;
223
                    } 
224
                    st = s->streams[stream_index];
225

    
226
                    get_le32(pb); /* tag */
227
                    get_le32(pb); /* flags */
228
                    get_le16(pb); /* priority */
229
                    get_le16(pb); /* language */
230
                    get_le32(pb); /* initial frame */
231
                    get_le32(pb); /* scale */
232
                    rate = get_le32(pb);
233
                    get_le32(pb); /* start */
234
                    length = get_le32(pb); /* length, in samples or bytes */
235
                    st->start_time = 0;
236
                    if (rate != 0)
237
                        st->duration = (int64_t)length * AV_TIME_BASE / rate;
238
                    url_fskip(pb, size - 9 * 4);
239
                }
240
                break;
241
            default:
242
                goto fail;
243
            }
244
            break;
245
        case MKTAG('s', 't', 'r', 'f'):
246
            /* stream header */
247
            if (stream_index >= s->nb_streams || avi->type == 1) {
248
                url_fskip(pb, size);
249
            } else {
250
                st = s->streams[stream_index];
251
                switch(codec_type) {
252
                case CODEC_TYPE_VIDEO:
253
                    get_le32(pb); /* size */
254
                    st->codec.width = get_le32(pb);
255
                    st->codec.height = get_le32(pb);
256
                    get_le16(pb); /* panes */
257
                    st->codec.bits_per_sample= get_le16(pb); /* depth */
258
                    tag1 = get_le32(pb);
259
                    get_le32(pb); /* ImageSize */
260
                    get_le32(pb); /* XPelsPerMeter */
261
                    get_le32(pb); /* YPelsPerMeter */
262
                    get_le32(pb); /* ClrUsed */
263
                    get_le32(pb); /* ClrImportant */
264

    
265
                    st->codec.extradata_size= size - 10*4;
266
                    st->codec.extradata= av_malloc(st->codec.extradata_size);
267
                    get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
268
                    
269
                    if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
270
                        get_byte(pb);
271

    
272
#ifdef DEBUG
273
                    print_tag("video", tag1, 0);
274
#endif
275
                    st->codec.codec_type = CODEC_TYPE_VIDEO;
276
                    st->codec.codec_tag = tag1;
277
                    st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
278
                    if (st->codec.codec_id == CODEC_ID_XAN_WC4)
279
                        xan_video = 1;
280
//                    url_fskip(pb, size - 5 * 4);
281
                    break;
282
                case CODEC_TYPE_AUDIO:
283
                    get_wav_header(pb, &st->codec, size);
284
                    if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
285
                        url_fskip(pb, 1);
286
                    /* special case time: To support Xan DPCM, hardcode
287
                     * the format if Xxan is the video codec */
288
                    if (xan_video)
289
                        st->codec.codec_id = CODEC_ID_XAN_DPCM;
290
                    break;
291
                default:
292
                    url_fskip(pb, size);
293
                    break;
294
                }
295
            }
296
            break;
297
        default:
298
            /* skip tag */
299
            size += (size & 1);
300
            url_fskip(pb, size);
301
            break;
302
        }
303
    }
304
 end_of_header:
305
    /* check stream number */
306
    if (stream_index != s->nb_streams - 1) {
307
    fail:
308
        av_free(avi->buf);
309
        for(i=0;i<s->nb_streams;i++) {
310
            av_freep(&s->streams[i]->codec.extradata);
311
            av_freep(&s->streams[i]);
312
        }
313
        return -1;
314
    }
315

    
316
    return 0;
317
}
318

    
319
static void __destruct_pkt(struct AVPacket *pkt)
320
{
321
    pkt->data = NULL; pkt->size = 0;
322
    return;
323
}
324

    
325
static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
326
{
327
    AVIContext *avi = s->priv_data;
328
    ByteIOContext *pb = &s->pb;
329
    int n, d[8], size, i;
330

    
331
    memset(d, -1, sizeof(int)*8);
332
    
333
    if (avi->type == 1 && avi->stream_index) {
334
        /* duplicate DV packet */
335
        av_init_packet(pkt);
336
        pkt->data = avi->buf;
337
        pkt->size = avi->buf_size;
338
        pkt->destruct = __destruct_pkt;
339
        pkt->stream_index = avi->stream_index;
340
        avi->stream_index = !avi->stream_index;
341
        return 0;
342
    }
343

    
344
    for(i=url_ftell(pb); !url_feof(pb); i++) {
345
        int j;
346

    
347
        if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */
348
            uint32_t tag, size, tag2;
349
            url_fskip(pb, avi->riff_end - url_ftell(pb));
350
            if (get_riff(avi, pb) < 0)
351
                return -1;
352
            
353
            tag = get_le32(pb);
354
            size = get_le32(pb);
355
            tag2 = get_le32(pb);
356
            if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
357
                avi->movi_end = url_ftell(pb) + size - 4; 
358
            else
359
                return -1;
360
        }
361

    
362
        for(j=0; j<7; j++)
363
            d[j]= d[j+1];
364
        d[7]= get_byte(pb);
365
        
366
        size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
367
        
368
        //parse ix##
369
        n= (d[2] - '0') * 10 + (d[3] - '0');
370
        if(    d[2] >= '0' && d[2] <= '9'
371
            && d[3] >= '0' && d[3] <= '9'
372
            && d[0] == 'i' && d[1] == 'x'
373
            && n < s->nb_streams
374
            && i + size <= avi->movi_end){
375
            
376
            url_fskip(pb, size);
377
        }
378
        
379
        //parse ##dc/##wb
380
        n= (d[0] - '0') * 10 + (d[1] - '0');
381
        if(    d[0] >= '0' && d[0] <= '9'
382
            && d[1] >= '0' && d[1] <= '9'
383
            && ((d[2] == 'd' && d[3] == 'c') || 
384
                (d[2] == 'w' && d[3] == 'b') || 
385
                (d[2] == 'd' && d[3] == 'b') ||
386
                (d[2] == '_' && d[3] == '_'))
387
            && n < s->nb_streams
388
            && i + size <= avi->movi_end) {
389
        
390
            if (avi->type == 1) {
391
                uint8_t *tbuf = av_realloc(avi->buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
392
                if (!tbuf)
393
                    return -1;
394
                avi->buf = tbuf;
395
                avi->buf_size = size;
396
                av_init_packet(pkt);
397
                pkt->data = avi->buf;
398
                pkt->size = avi->buf_size;
399
                pkt->destruct = __destruct_pkt;
400
                avi->stream_index = n;
401
            } else {
402
                av_new_packet(pkt, size);
403
            }
404
            get_buffer(pb, pkt->data, size);
405
            if (size & 1)
406
                get_byte(pb);
407
            pkt->stream_index = n;
408
            pkt->flags |= PKT_FLAG_KEY; // FIXME: We really should read index for that
409
            return 0;
410
        }
411
    }
412
    return -1;
413
}
414

    
415
static int avi_read_close(AVFormatContext *s)
416
{
417
    int i;
418
    AVIContext *avi = s->priv_data;
419
    av_free(avi->buf);
420

    
421
    for(i=0;i<s->nb_streams;i++) {
422
        AVStream *st = s->streams[i];
423
//        av_free(st->priv_data);
424
        av_free(st->codec.extradata);
425
    }
426

    
427
    return 0;
428
}
429

    
430
static int avi_probe(AVProbeData *p)
431
{
432
    /* check file header */
433
    if (p->buf_size <= 32)
434
        return 0;
435
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
436
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
437
        p->buf[8] == 'A' && p->buf[9] == 'V' &&
438
        p->buf[10] == 'I' && p->buf[11] == ' ')
439
        return AVPROBE_SCORE_MAX;
440
    else
441
        return 0;
442
}
443

    
444
static AVInputFormat avi_iformat = {
445
    "avi",
446
    "avi format",
447
    sizeof(AVIContext),
448
    avi_probe,
449
    avi_read_header,
450
    avi_read_packet,
451
    avi_read_close,
452
};
453

    
454
int avidec_init(void)
455
{
456
    av_register_input_format(&avi_iformat);
457
    return 0;
458
}