Statistics
| Branch: | Revision:

ffmpeg / libavformat / avidec.c @ 7458ccbb

History | View | Annotate | Download (12.5 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
#include "dv.h"
22

    
23
//#define DEBUG
24

    
25
typedef struct AVIIndex {
26
    unsigned char tag[4];
27
    unsigned int flags, pos, len;
28
    struct AVIIndex *next;
29
} AVIIndex;
30

    
31
typedef struct {
32
    int64_t  riff_end;
33
    int64_t  movi_end;
34
    offset_t movi_list;
35
    AVIIndex *first, *last;
36
    void* dv_demux;
37
} AVIContext;
38

    
39
#ifdef DEBUG
40
static void print_tag(const char *str, unsigned int tag, int size)
41
{
42
    printf("%s: tag=%c%c%c%c size=0x%x\n",
43
           str, tag & 0xff,
44
           (tag >> 8) & 0xff,
45
           (tag >> 16) & 0xff,
46
           (tag >> 24) & 0xff,
47
           size);
48
}
49
#endif
50

    
51
static int get_riff(AVIContext *avi, ByteIOContext *pb)
52
{
53
    uint32_t tag; 
54
    /* check RIFF header */
55
    tag = get_le32(pb);
56

    
57
    if (tag != MKTAG('R', 'I', 'F', 'F'))
58
        return -1;
59
    avi->riff_end = get_le32(pb);   /* RIFF chunk size */
60
    avi->riff_end += url_ftell(pb); /* RIFF chunk end */
61
    tag = get_le32(pb);
62
    if (tag != MKTAG('A', 'V', 'I', ' ') && tag != MKTAG('A', 'V', 'I', 'X'))
63
        return -1;
64
    
65
    return 0;
66
}
67

    
68
static int avi_read_header(AVFormatContext *s, AVFormatParameters *ap)
69
{
70
    AVIContext *avi = s->priv_data;
71
    ByteIOContext *pb = &s->pb;
72
    uint32_t tag, tag1, handler;
73
    int codec_type, stream_index, frame_period, bit_rate, scale, rate;
74
    unsigned int size, nb_frames;
75
    int i, n;
76
    AVStream *st;
77
    int xan_video = 0;  /* hack to support Xan A/V */
78

    
79
    if (get_riff(avi, pb) < 0)
80
        return -1;
81

    
82
    /* first list tag */
83
    stream_index = -1;
84
    codec_type = -1;
85
    frame_period = 0;
86
    for(;;) {
87
        if (url_feof(pb))
88
            goto fail;
89
        tag = get_le32(pb);
90
        size = get_le32(pb);
91
#ifdef DEBUG
92
        print_tag("tag", tag, size);
93
#endif
94

    
95
        switch(tag) {
96
        case MKTAG('L', 'I', 'S', 'T'):
97
            /* ignored, except when start of video packets */
98
            tag1 = get_le32(pb);
99
#ifdef DEBUG
100
            print_tag("list", tag1, 0);
101
#endif
102
            if (tag1 == MKTAG('m', 'o', 'v', 'i')) {
103
                avi->movi_end = url_ftell(pb) + size - 4;
104
#ifdef DEBUG
105
                printf("movi end=%Lx\n", avi->movi_end);
106
#endif
107
                goto end_of_header;
108
            }
109
            break;
110
        case MKTAG('a', 'v', 'i', 'h'):
111
            /* avi header */
112
            /* using frame_period is bad idea */
113
            frame_period = get_le32(pb);
114
            bit_rate = get_le32(pb) * 8;
115
            url_fskip(pb, 4 * 4);
116
            n = get_le32(pb);
117
            for(i=0;i<n;i++) {
118
                st = av_new_stream(s, i);
119
                if (!st)
120
                    goto fail;
121
            }
122
            url_fskip(pb, size - 7 * 4);
123
            break;
124
        case MKTAG('s', 't', 'r', 'h'):
125
            /* stream header */
126
            stream_index++;
127
            tag1 = get_le32(pb);
128
            handler = get_le32(pb); /* codec tag */
129
            switch(tag1) {
130
            case MKTAG('i', 'a', 'v', 's'):
131
            case MKTAG('i', 'v', 'a', 's'):
132
                /* 
133
                 * After some consideration -- I don't think we 
134
                 * have to support anything but DV in a type1 AVIs.
135
                 */
136
                if (s->nb_streams != 1)
137
                    goto fail;
138
                
139
                if (handler != MKTAG('d', 'v', 's', 'd') &&
140
                    handler != MKTAG('d', 'v', 'h', 'd') &&
141
                    handler != MKTAG('d', 'v', 's', 'l'))
142
                   goto fail;
143

    
144
                avi->dv_demux = dv_init_demux(s, stream_index, stream_index + 1);
145
                if (!avi->dv_demux)
146
                    goto fail;
147
                stream_index++;
148
            case MKTAG('v', 'i', 'd', 's'):
149
                codec_type = CODEC_TYPE_VIDEO;
150

    
151
                if (stream_index >= s->nb_streams) {
152
                    url_fskip(pb, size - 8);
153
                    break;
154
                } 
155

    
156
                st = s->streams[stream_index];
157

    
158
                get_le32(pb); /* flags */
159
                get_le16(pb); /* priority */
160
                get_le16(pb); /* language */
161
                get_le32(pb); /* XXX: initial frame ? */
162
                scale = get_le32(pb); /* scale */
163
                rate = get_le32(pb); /* rate */
164

    
165
                if(scale && rate){
166
                    st->codec.frame_rate = rate;
167
                    st->codec.frame_rate_base = scale;
168
                }else if(frame_period){
169
                    st->codec.frame_rate = 1000000;
170
                    st->codec.frame_rate_base = frame_period;
171
                }else{
172
                    st->codec.frame_rate = 25;
173
                    st->codec.frame_rate_base = 1;
174
                }
175
                get_le32(pb); /* start */
176
                nb_frames = get_le32(pb);
177
                st->start_time = 0;
178
                st->duration = (double)nb_frames * 
179
                    st->codec.frame_rate_base * AV_TIME_BASE / 
180
                    st->codec.frame_rate;
181
                
182
                url_fskip(pb, size - 9 * 4);
183
                break;
184
            case MKTAG('a', 'u', 'd', 's'):
185
                {
186
                    unsigned int length, rate;
187

    
188
                    codec_type = CODEC_TYPE_AUDIO;
189

    
190
                    if (stream_index >= s->nb_streams) {
191
                        url_fskip(pb, size - 8);
192
                        break;
193
                    } 
194
                    st = s->streams[stream_index];
195

    
196
                    get_le32(pb); /* flags */
197
                    get_le16(pb); /* priority */
198
                    get_le16(pb); /* language */
199
                    get_le32(pb); /* initial frame */
200
                    get_le32(pb); /* scale */
201
                    rate = get_le32(pb);
202
                    get_le32(pb); /* start */
203
                    length = get_le32(pb); /* length, in samples or bytes */
204
                    st->start_time = 0;
205
                    if (rate != 0)
206
                        st->duration = (int64_t)length * AV_TIME_BASE / rate;
207
                    url_fskip(pb, size - 9 * 4);
208
                }
209
                break;
210
            default:
211
                goto fail;
212
            }
213
            break;
214
        case MKTAG('s', 't', 'r', 'f'):
215
            /* stream header */
216
            if (stream_index >= s->nb_streams || avi->dv_demux) {
217
                url_fskip(pb, size);
218
            } else {
219
                st = s->streams[stream_index];
220
                switch(codec_type) {
221
                case CODEC_TYPE_VIDEO:
222
                    get_le32(pb); /* size */
223
                    st->codec.width = get_le32(pb);
224
                    st->codec.height = get_le32(pb);
225
                    get_le16(pb); /* panes */
226
                    st->codec.bits_per_sample= get_le16(pb); /* depth */
227
                    tag1 = get_le32(pb);
228
                    get_le32(pb); /* ImageSize */
229
                    get_le32(pb); /* XPelsPerMeter */
230
                    get_le32(pb); /* YPelsPerMeter */
231
                    get_le32(pb); /* ClrUsed */
232
                    get_le32(pb); /* ClrImportant */
233

    
234
                    st->codec.extradata_size= size - 10*4;
235
                    st->codec.extradata= av_malloc(st->codec.extradata_size);
236
                    get_buffer(pb, st->codec.extradata, st->codec.extradata_size);
237
                    
238
                    if(st->codec.extradata_size & 1) //FIXME check if the encoder really did this correctly
239
                        get_byte(pb);
240

    
241
#ifdef DEBUG
242
                    print_tag("video", tag1, 0);
243
#endif
244
                    st->codec.codec_type = CODEC_TYPE_VIDEO;
245
                    st->codec.codec_tag = tag1;
246
                    st->codec.codec_id = codec_get_id(codec_bmp_tags, tag1);
247
                    if (st->codec.codec_id == CODEC_ID_XAN_WC4)
248
                        xan_video = 1;
249
//                    url_fskip(pb, size - 5 * 4);
250
                    break;
251
                case CODEC_TYPE_AUDIO:
252
                    get_wav_header(pb, &st->codec, size);
253
                    if (size%2) /* 2-aligned (fix for Stargate SG-1 - 3x18 - Shades of Grey.avi) */
254
                        url_fskip(pb, 1);
255
                    /* special case time: To support Xan DPCM, hardcode
256
                     * the format if Xxan is the video codec */
257
                    if (xan_video)
258
                        st->codec.codec_id = CODEC_ID_XAN_DPCM;
259
                    break;
260
                default:
261
                    url_fskip(pb, size);
262
                    break;
263
                }
264
            }
265
            break;
266
        default:
267
            /* skip tag */
268
            size += (size & 1);
269
            url_fskip(pb, size);
270
            break;
271
        }
272
    }
273
 end_of_header:
274
    /* check stream number */
275
    if (stream_index != s->nb_streams - 1) {
276
    fail:
277
        for(i=0;i<s->nb_streams;i++) {
278
            av_freep(&s->streams[i]->codec.extradata);
279
            av_freep(&s->streams[i]);
280
        }
281
        return -1;
282
    }
283

    
284
    return 0;
285
}
286

    
287
static int avi_read_packet(AVFormatContext *s, AVPacket *pkt)
288
{
289
    AVIContext *avi = s->priv_data;
290
    ByteIOContext *pb = &s->pb;
291
    int n, d[8], size, i;
292
    void* dstr;
293

    
294
    memset(d, -1, sizeof(int)*8);
295
   
296
    if (avi->dv_demux) {
297
        size = dv_get_packet(avi->dv_demux, pkt);
298
        if (size >= 0)
299
            return size;
300
    }
301
        
302
    for(i=url_ftell(pb); !url_feof(pb); i++) {
303
        int j;
304

    
305
        if (i >= avi->movi_end) { /* Let's see if it's an OpenDML AVI */
306
            uint32_t tag, size, tag2;
307
            url_fskip(pb, avi->riff_end - url_ftell(pb));
308
            if (get_riff(avi, pb) < 0)
309
                return -1;
310
            
311
            tag = get_le32(pb);
312
            size = get_le32(pb);
313
            tag2 = get_le32(pb);
314
            if (tag == MKTAG('L','I','S','T') && tag2 == MKTAG('m','o','v','i'))
315
                avi->movi_end = url_ftell(pb) + size - 4; 
316
            else
317
                return -1;
318
        }
319

    
320
        for(j=0; j<7; j++)
321
            d[j]= d[j+1];
322
        d[7]= get_byte(pb);
323
        
324
        size= d[4] + (d[5]<<8) + (d[6]<<16) + (d[7]<<24);
325
        
326
        //parse ix##
327
        n= (d[2] - '0') * 10 + (d[3] - '0');
328
        if(    d[2] >= '0' && d[2] <= '9'
329
            && d[3] >= '0' && d[3] <= '9'
330
            && d[0] == 'i' && d[1] == 'x'
331
            && n < s->nb_streams
332
            && i + size <= avi->movi_end){
333
            
334
            url_fskip(pb, size);
335
        }
336
        
337
        //parse ##dc/##wb
338
        n= (d[0] - '0') * 10 + (d[1] - '0');
339
        if(    d[0] >= '0' && d[0] <= '9'
340
            && d[1] >= '0' && d[1] <= '9'
341
            && ((d[2] == 'd' && d[3] == 'c') || 
342
                (d[2] == 'w' && d[3] == 'b') || 
343
                (d[2] == 'd' && d[3] == 'b') ||
344
                (d[2] == '_' && d[3] == '_'))
345
            && n < s->nb_streams
346
            && i + size <= avi->movi_end) {
347
        
348
            av_new_packet(pkt, size);
349
            get_buffer(pb, pkt->data, size);
350
            if (size & 1) {
351
                get_byte(pb);
352
                size++;
353
            }
354
        
355
            if (avi->dv_demux) {
356
                dstr = pkt->destruct;
357
                size = dv_produce_packet(avi->dv_demux, pkt,
358
                                         pkt->data, pkt->size);
359
                pkt->destruct = dstr;
360
            } else {
361
                pkt->stream_index = n;
362
                pkt->flags |= PKT_FLAG_KEY; // FIXME: We really should read 
363
                                            //        index for that
364
            }
365
            return size;
366
        }
367
    }
368
    return -1;
369
}
370

    
371
static int avi_read_close(AVFormatContext *s)
372
{
373
    int i;
374
    AVIContext *avi = s->priv_data;
375

    
376
    for(i=0;i<s->nb_streams;i++) {
377
        AVStream *st = s->streams[i];
378
//        av_free(st->priv_data);
379
        av_free(st->codec.extradata);
380
    }
381

    
382
    if (avi->dv_demux)
383
        av_free(avi->dv_demux);
384

    
385
    return 0;
386
}
387

    
388
static int avi_probe(AVProbeData *p)
389
{
390
    /* check file header */
391
    if (p->buf_size <= 32)
392
        return 0;
393
    if (p->buf[0] == 'R' && p->buf[1] == 'I' &&
394
        p->buf[2] == 'F' && p->buf[3] == 'F' &&
395
        p->buf[8] == 'A' && p->buf[9] == 'V' &&
396
        p->buf[10] == 'I' && p->buf[11] == ' ')
397
        return AVPROBE_SCORE_MAX;
398
    else
399
        return 0;
400
}
401

    
402
static AVInputFormat avi_iformat = {
403
    "avi",
404
    "avi format",
405
    sizeof(AVIContext),
406
    avi_probe,
407
    avi_read_header,
408
    avi_read_packet,
409
    avi_read_close,
410
};
411

    
412
int avidec_init(void)
413
{
414
    av_register_input_format(&avi_iformat);
415
    return 0;
416
}