libavi.c 29.7 KB
Newer Older
1
/*****************************************************************************
2
 * libavi.c :
3
4
 *****************************************************************************
 * Copyright (C) 2001 VideoLAN
5
 * $Id: libavi.c,v 1.29 2003/10/24 12:22:51 fenrir Exp $
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
8
9
10
11
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
12
 *
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#include <stdlib.h>                                      /* malloc(), free() */

#include <vlc/vlc.h>
#include <vlc/input.h>
27
#include "codecs.h"                                      /* BITMAPINFOHEADER */
28
29
30
31

#include "libavi.h"

#define AVI_DEBUG 1
32

33
34
35
36
#define FREE( p ) \
    if( p ) {free( p ); p = NULL; }

#define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
37

38
39
static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
{
40
    return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
41
}
42

43
44
#define AVI_ChunkFree( a, b ) _AVI_ChunkFree( (a), (avi_chunk_t*)(b) )
void    _AVI_ChunkFree( stream_t *, avi_chunk_t *p_chk );
45

46
47
48
49
50
/****************************************************************************
 *
 * Basics functions to manipulates chunks
 *
 ****************************************************************************/
51
static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
52
{
53
    uint8_t  *p_peek;
54
55
56
57
    int i_peek;

    memset( p_chk, 0, sizeof( avi_chunk_t ) );

58
    if( ( i_peek = stream_Peek( s, &p_peek, 8 ) ) < 8 )
59
    {
60
        return VLC_EGENERIC;
61
    }
62

63
    p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
64
    p_chk->common.i_chunk_size   = GetDWLE( p_peek + 4 );
65
    p_chk->common.i_chunk_pos    = stream_Tell( s );
66
67
68
69
70

    p_chk->common.p_father = NULL;
    p_chk->common.p_next = NULL;
    p_chk->common.p_first = NULL;
    p_chk->common.p_next = NULL;
71

72
#ifdef AVI_DEBUG
73
    msg_Dbg( (vlc_object_t*)s,
74
             "Found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
75
             p_chk->common.i_chunk_fourcc,
76
             (char*)&p_chk->common.i_chunk_fourcc,
77
78
79
             p_chk->common.i_chunk_size,
             p_chk->common.i_chunk_pos );
#endif
80
    return VLC_SUCCESS;
81
82
}

83
static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
84
85
{
    avi_chunk_t chk;
86

87
88
    if( !p_chk )
    {
89
        if( AVI_ChunkReadCommon( s, &chk ) )
90
        {
91
            return VLC_EGENERIC;
92
93
94
95
96
97
        }
        p_chk = &chk;
    }

    if( p_chk->common.p_father )
    {
98
        if( p_chk->common.p_father->common.i_chunk_pos +
99
                __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
100
            p_chk->common.i_chunk_pos +
101
102
                __EVEN( p_chk->common.i_chunk_size ) + 8 )
        {
103
            return VLC_EGENERIC;
104
105
        }
    }
106
    return stream_Seek( s, p_chk->common.i_chunk_pos +
107
                                 __EVEN( p_chk->common.i_chunk_size ) + 8 );
108
109
110
111
}

/****************************************************************************
 *
112
 * Functions to read chunks
113
114
 *
 ****************************************************************************/
115
static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
116
117
{
    avi_chunk_t *p_chk;
118
    uint8_t *p_peek;
119
    vlc_bool_t b_seekable;
120

121
    if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
122
123
    {
        /* empty box */
124
        msg_Warn( (vlc_object_t*)s, "empty list chunk" );
125
        return VLC_EGENERIC;
126
    }
127
    if( stream_Peek( s, &p_peek, 12 ) < 12 )
128
    {
129
        msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
130
        return VLC_EGENERIC;
131
    }
132
133
134

    stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );

135
    p_container->list.i_type = GetFOURCC( p_peek + 8 );
136
137
138
139

    if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
        p_container->list.i_type == AVIFOURCC_movi )
    {
140
        msg_Dbg( (vlc_object_t*)s, "Skipping movi chunk" );
141
142
        if( b_seekable )
        {
143
            return AVI_NextChunk( s, p_container );
144
        }
hartman's avatar
hartman committed
145
        return VLC_SUCCESS; /* point at begining of LIST-movi */
146
147
    }

148
    if( stream_Read( s, NULL, 12 ) != 12 )
149
    {
150
        msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
151
152
        return VLC_EGENERIC;
    }
153

154
#ifdef AVI_DEBUG
155
    msg_Dbg( (vlc_object_t*)s,
156
157
             "found LIST chunk: \'%4.4s\'",
             (char*)&p_container->list.i_type );
158
#endif
159
    msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
160
161
162
163
164
165
166
167
168
169
170
171
172
173
    for( ; ; )
    {
        p_chk = malloc( sizeof( avi_chunk_t ) );
        memset( p_chk, 0, sizeof( avi_chunk_t ) );
        if( !p_container->common.p_first )
        {
            p_container->common.p_first = p_chk;
        }
        else
        {
            p_container->common.p_last->common.p_next = p_chk;
        }
        p_container->common.p_last = p_chk;

174
175
176
177
178
        if( AVI_ChunkRead( s, p_chk, p_container ) )
        {
            break;
        }
        if( p_chk->common.p_father->common.i_chunk_size > 0 &&
179
           ( stream_Tell( s ) >=
180
181
              (off_t)p_chk->common.p_father->common.i_chunk_pos +
               (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
182
183
184
        {
            break;
        }
185

186
187
        /* If we can't seek then stop when we 've found LIST-movi */
        if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
188
189
            p_chk->list.i_type == AVIFOURCC_movi &&
            ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
190
191
192
193
        {
            break;
        }

194
    }
195
    msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
196

197
    return VLC_SUCCESS;
198
199
200
}

#define AVI_READCHUNK_ENTER \
201
202
    int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
    uint8_t  *p_read, *p_buff;    \
203
204
    if( !( p_read = p_buff = malloc(i_read ) ) ) \
    { \
205
        return VLC_EGENERIC; \
206
    } \
207
    i_read = stream_Read( s, p_read, i_read ); \
208
209
210
211
    if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
    { \
        return VLC_EGENERIC; \
    }\
212
213
    p_read += 8; \
    i_read -= 8
214

215
216
217
218
#define AVI_READCHUNK_EXIT( code ) \
    free( p_buff ); \
    if( i_read < 0 ) \
    { \
219
        msg_Warn( (vlc_object_t*)s, "not enough data" ); \
220
    } \
221
    return code
222
223
224
225
226
227

#define AVI_READ1BYTE( i_byte ) \
    i_byte = *p_read; \
    p_read++; \
    i_read--

228
229
230
231
232
233
234
235
236
#define AVI_READ2BYTES( i_word ) \
    i_word = GetWLE( p_read ); \
    p_read += 2; \
    i_read -= 2

#define AVI_READ4BYTES( i_dword ) \
    i_dword = GetDWLE( p_read ); \
    p_read += 4; \
    i_read -= 4
237

238
239
240
241
242
#define AVI_READ8BYTES( i_dword ) \
    i_dword = GetQWLE( p_read ); \
    p_read += 8; \
    i_read -= 8

243
244
245
246
247
#define AVI_READFOURCC( i_dword ) \
    i_dword = GetFOURCC( p_read ); \
    p_read += 4; \
    i_read -= 4

248
static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
{
    AVI_READCHUNK_ENTER;

    AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
    AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
    AVI_READ4BYTES( p_chk->avih.i_reserved1 );
    AVI_READ4BYTES( p_chk->avih.i_flags );
    AVI_READ4BYTES( p_chk->avih.i_totalframes );
    AVI_READ4BYTES( p_chk->avih.i_initialframes );
    AVI_READ4BYTES( p_chk->avih.i_streams );
    AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
    AVI_READ4BYTES( p_chk->avih.i_width );
    AVI_READ4BYTES( p_chk->avih.i_height );
    AVI_READ4BYTES( p_chk->avih.i_scale );
    AVI_READ4BYTES( p_chk->avih.i_rate );
    AVI_READ4BYTES( p_chk->avih.i_start );
    AVI_READ4BYTES( p_chk->avih.i_length );
#ifdef AVI_DEBUG
267
268
    msg_Dbg( (vlc_object_t*)s,
             "avih: streams:%d flags:%s%s%s%s %dx%d",
269
270
271
272
273
274
             p_chk->avih.i_streams,
             p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
             p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
             p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
             p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
             p_chk->avih.i_width, p_chk->avih.i_height );
275
#endif
276
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
277
278
}

279
static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
{
    AVI_READCHUNK_ENTER;

    AVI_READFOURCC( p_chk->strh.i_type );
    AVI_READFOURCC( p_chk->strh.i_handler );
    AVI_READ4BYTES( p_chk->strh.i_flags );
    AVI_READ4BYTES( p_chk->strh.i_reserved1 );
    AVI_READ4BYTES( p_chk->strh.i_initialframes );
    AVI_READ4BYTES( p_chk->strh.i_scale );
    AVI_READ4BYTES( p_chk->strh.i_rate );
    AVI_READ4BYTES( p_chk->strh.i_start );
    AVI_READ4BYTES( p_chk->strh.i_length );
    AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
    AVI_READ4BYTES( p_chk->strh.i_quality );
    AVI_READ4BYTES( p_chk->strh.i_samplesize );
#ifdef AVI_DEBUG
296
    msg_Dbg( (vlc_object_t*)s,
297
298
             "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
             (char*)&p_chk->strh.i_type,
299
300
             p_chk->strh.i_handler,
             p_chk->strh.i_samplesize,
301
             ( p_chk->strh.i_scale ?
302
303
                (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
#endif
304

305
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
306
307
}

308
static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
309
310
311
312
313
314
{
    avi_chunk_t *p_strh;

    AVI_READCHUNK_ENTER;
    if( p_chk->common.p_father == NULL )
    {
315
        msg_Err( (vlc_object_t*)s, "malformed avi file" );
316
        AVI_READCHUNK_EXIT( VLC_EGENERIC );
317
318
319
    }
    if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
    {
320
        msg_Err( (vlc_object_t*)s, "malformed avi file" );
321
        AVI_READCHUNK_EXIT( VLC_EGENERIC );
322
    }
323

324
325
326
    switch( p_strh->strh.i_type )
    {
        case( AVIFOURCC_auds ):
327
            p_chk->strf.auds.i_cat = AUDIO_ES;
328
            p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
329
330
331
332
333
334
            AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
            AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
            AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
            AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
            AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
            AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
335
336
            if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
                 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
337
            {
338
                AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
339
340
341
342
343
344
345
                /* prevent segfault */
                if( p_chk->strf.auds.p_wf->cbSize >
                        p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
                {
                    p_chk->strf.auds.p_wf->cbSize =
                        p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
                }
hartman's avatar
hartman committed
346
347
348
                if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
                {
                    /* Found an extensible header atm almost nothing uses that. */
gbazin's avatar
   
gbazin committed
349
350
                    msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
                              "vorbis audio dectected: not supported" );
hartman's avatar
hartman committed
351
                }
352
353
354
355
356
357
358
            }
            else
            {
                p_chk->strf.auds.p_wf->cbSize = 0;
            }
            if( p_chk->strf.auds.p_wf->cbSize > 0 )
            {
359
                memcpy( &p_chk->strf.auds.p_wf[1] ,
hartman's avatar
hartman committed
360
                        p_buff + 8 + sizeof( WAVEFORMATEX ),    /*  8=fourrc+size */
361
                        p_chk->strf.auds.p_wf->cbSize );
362
363
            }
#ifdef AVI_DEBUG
364
            msg_Dbg( (vlc_object_t*)s,
365
                     "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
366
367
368
369
370
                     p_chk->strf.auds.p_wf->wFormatTag,
                     p_chk->strf.auds.p_wf->nChannels,
                     p_chk->strf.auds.p_wf->nSamplesPerSec,
                     p_chk->strf.auds.p_wf->wBitsPerSample,
                     p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
371
372
373
#endif
            break;
        case( AVIFOURCC_vids ):
hartman's avatar
hartman committed
374
            p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
375
            p_chk->strf.vids.i_cat = VIDEO_ES;
376
            p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
gbazin's avatar
   
gbazin committed
377
378
379
380
381
382
383
384
385
386
387
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
            AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
            AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
            AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
            AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
388
389
390
391
392
393
394
395
            if( p_chk->strf.vids.p_bih->biSize >
                        p_chk->common.i_chunk_size )
            {
                p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
            }
            if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
            {
                memcpy( &p_chk->strf.vids.p_bih[1],
hartman's avatar
hartman committed
396
                        p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
397
398
399
                        p_chk->strf.vids.p_bih->biSize -
                                                    sizeof(BITMAPINFOHEADER) );
            }
400
#ifdef AVI_DEBUG
401
            msg_Dbg( (vlc_object_t*)s,
402
403
                     "strf: video:%4.4s %dx%d planes:%d %dbpp",
                     (char*)&p_chk->strf.vids.p_bih->biCompression,
gbazin's avatar
   
gbazin committed
404
405
406
407
                     p_chk->strf.vids.p_bih->biWidth,
                     p_chk->strf.vids.p_bih->biHeight,
                     p_chk->strf.vids.p_bih->biPlanes,
                     p_chk->strf.vids.p_bih->biBitCount );
408
409
410
#endif
            break;
        default:
411
            msg_Warn( (vlc_object_t*)s, "unknown stream type" );
412
            p_chk->strf.common.i_cat = UNKNOWN_ES;
413
414
            break;
    }
415
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
416
}
417
static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
418
{
419
    avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
420
    if( p_strf->common.i_cat == AUDIO_ES )
421
    {
422
423
424
425
426
        FREE( p_strf->auds.p_wf );
    }
    else if( p_strf->common.i_cat == VIDEO_ES )
    {
        FREE( p_strf->vids.p_bih );
427
    }
428
429
}

430
static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
431
432
433
434
{
    AVI_READCHUNK_ENTER;
    p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
    memcpy( p_chk->strd.p_data,
435
            p_buff + 8,
436
            p_chk->common.i_chunk_size );
437
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
438
439
}

440
static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
441
{
442
    unsigned int i_count, i_index;
443
444
445

    AVI_READCHUNK_ENTER;

446
    i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
447
448
449
450
451
452
453
454
455

    p_chk->idx1.i_entry_count = i_count;
    p_chk->idx1.i_entry_max   = i_count;
    if( i_count > 0 )
    {
        p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );

        for( i_index = 0; i_index < i_count ; i_index++ )
        {
456
            AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
457
458
459
460
461
462
463
464
465
466
            AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
            AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
            AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
        }
    }
    else
    {
        p_chk->idx1.entry = NULL;
    }
#ifdef AVI_DEBUG
467
    msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
468
#endif
469
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
470
471
}

472
static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
473
474
475
476
477
478
{
    p_chk->idx1.i_entry_count = 0;
    p_chk->idx1.i_entry_max   = 0;
    FREE( p_chk->idx1.entry )
}

479
480


481
static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
{
    unsigned int i_count, i;
    int32_t      i_dummy;
    avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;

    AVI_READCHUNK_ENTER;

    AVI_READ2BYTES( p_indx->i_longsperentry );
    AVI_READ1BYTE ( p_indx->i_indexsubtype );
    AVI_READ1BYTE ( p_indx->i_indextype );
    AVI_READ4BYTES( p_indx->i_entriesinuse );

    AVI_READ4BYTES( p_indx->i_id );
    p_indx->idx.std     = NULL;
    p_indx->idx.field   = NULL;
    p_indx->idx.super   = NULL;

    if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
    {
        AVI_READ8BYTES( p_indx->i_baseoffset );
        AVI_READ4BYTES( i_dummy );

        i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
        p_indx->i_entriesinuse = i_count;
        p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );

        for( i = 0; i < i_count; i++ )
        {
            AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
            AVI_READ4BYTES( p_indx->idx.std[i].i_size );
        }
    }
    else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
    {
        AVI_READ8BYTES( p_indx->i_baseoffset );
        AVI_READ4BYTES( i_dummy );

        i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
        p_indx->i_entriesinuse = i_count;
        p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
        for( i = 0; i < i_count; i++ )
        {
            AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
            AVI_READ4BYTES( p_indx->idx.field[i].i_size );
            AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
        }
    }
    else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
    {
        p_indx->i_baseoffset = 0;
        AVI_READ4BYTES( i_dummy );
        AVI_READ4BYTES( i_dummy );
        AVI_READ4BYTES( i_dummy );

        i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
        p_indx->i_entriesinuse = i_count;
        p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );

        for( i = 0; i < i_count; i++ )
        {
            AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
            AVI_READ4BYTES( p_indx->idx.super[i].i_size );
            AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
        }
    }
    else
    {
549
        msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
550
551
552
    }

#ifdef AVI_DEBUG
553
    msg_Dbg( (vlc_object_t*)s, "indx: type=%d subtype=%d entry=%d", p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
554
555
556
#endif
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
}
557
static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
558
559
560
561
562
563
564
565
566
567
568
{
    avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;

    FREE( p_indx->idx.std );
    FREE( p_indx->idx.field );
    FREE( p_indx->idx.super );
}



static struct
569
{
570
    vlc_fourcc_t i_fourcc;
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
    char *psz_type;
} AVI_strz_type[] =
{
    { AVIFOURCC_IARL, "archive location" },
    { AVIFOURCC_IART, "artist" },
    { AVIFOURCC_ICMS, "commisioned" },
    { AVIFOURCC_ICMT, "comments" },
    { AVIFOURCC_ICOP, "copyright" },
    { AVIFOURCC_ICRD, "creation date" },
    { AVIFOURCC_ICRP, "cropped" },
    { AVIFOURCC_IDIM, "dimensions" },
    { AVIFOURCC_IDPI, "dots per inch" },
    { AVIFOURCC_IENG, "enginner" },
    { AVIFOURCC_IGNR, "genre" },
    { AVIFOURCC_IKEY, "keywords" },
    { AVIFOURCC_ILGT, "lightness" },
    { AVIFOURCC_IMED, "medium" },
    { AVIFOURCC_INAM, "name" },
    { AVIFOURCC_IPLT, "palette setting" },
    { AVIFOURCC_IPRD, "product" },
    { AVIFOURCC_ISBJ, "subject" },
    { AVIFOURCC_ISFT, "software" },
    { AVIFOURCC_ISHP, "sharpness" },
    { AVIFOURCC_ISRC, "source" },
    { AVIFOURCC_ISRF, "source form" },
    { AVIFOURCC_ITCH, "technician" },
    { AVIFOURCC_ISMP, "time code" },
    { AVIFOURCC_IDIT, "digitalization time" },
599
    { AVIFOURCC_strn, "stream name" },
600
601
    { 0,              "???" }
};
602
static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
{
    int i_index;
    avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
    AVI_READCHUNK_ENTER;

    for( i_index = 0;; i_index++)
    {
        if( !AVI_strz_type[i_index].i_fourcc ||
            AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
        {
            break;
        }
    }
    p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
    p_strz->p_str = malloc( i_read + 1);

    if( p_strz->i_chunk_size )
    {
        memcpy( p_strz->p_str, p_read, i_read );
    }
    p_strz->p_str[i_read] = 0;
624

625
#ifdef AVI_DEBUG
626
    msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
627
             (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
628
#endif
629
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
630
}
631
static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
632
633
634
635
636
637
{
    avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
    FREE( p_strz->p_type );
    FREE( p_strz->p_str );
}

638
static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
639
{
640
    return AVI_NextChunk( s, p_chk );
641
}
642
static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
643
644
645
646
647
648
{

}

static struct
{
649
    vlc_fourcc_t i_fourcc;
650
651
    int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
    void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
652
653
654
655
656
657
658
659
660
} AVI_Chunk_Function [] =
{
    { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
    { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
    { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
    { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
    { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
    { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
    { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
661
    { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
    { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },

    { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
    { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
690
    { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
691
692
693
    { 0,           NULL,               NULL }
};

694
static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
695
{
696
    unsigned int i_index;
697
698
699
700
701
    for( i_index = 0; ; i_index++ )
    {
        if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
            ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
        {
702
            return i_index;
703
704
705
706
        }
    }
}

707
int  _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
708
709
710
711
712
{
    int i_index;

    if( !p_chk )
    {
713
        return VLC_EGENERIC;
714
715
    }

716
    if( AVI_ChunkReadCommon( s, p_chk ) )
717
    {
718
        msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
719
        return VLC_EGENERIC;
720
    }
721
722
    if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
    {
723
        msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
724
725
        return VLC_EGENERIC;
    }
726
727
728
729
730
    p_chk->common.p_father = p_father;

    i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
    if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
    {
731
        return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
732
    }
733
734
735
736
    else if( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
             ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' )
    {
        p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
737
        return AVI_ChunkRead_indx( s, p_chk );
738
    }
739
740
    msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
    return AVI_NextChunk( s, p_chk );
741
742
}

743
void _AVI_ChunkFree( stream_t *s,
744
745
746
747
748
749
750
751
752
                     avi_chunk_t *p_chk )
{
    int i_index;
    avi_chunk_t *p_child, *p_next;

    if( !p_chk )
    {
        return;
    }
753

754
755
756
757
758
    /* Free all child chunk */
    p_child = p_chk->common.p_first;
    while( p_child )
    {
        p_next = p_child->common.p_next;
759
        AVI_ChunkFree( s, p_child );
760
761
762
763
764
765
766
767
        free( p_child );
        p_child = p_next;
    }

    i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
    if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
    {
#ifdef AVI_DEBUG
768
        msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
769
                 (char*)&p_chk->common.i_chunk_fourcc );
770
#endif
771
        AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
772
773
774
    }
    else
    {
775
        msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
776
777
778
    }
    p_chk->common.p_first = NULL;
    p_chk->common.p_last  = NULL;
779

780
781
782
    return;
}

783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
                                      avi_chunk_t  *p_chk, int i_level )
{
    char str[1024];
    int i;
    avi_chunk_t *p_child;

    memset( str, ' ', sizeof( str ) );
    for( i = 1; i < i_level; i++ )
    {
        str[i * 5] = '|';
    }
    if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
        p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
    {
        sprintf( str + i_level * 5,
                 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
                 i_level ? '+' : '*',
                 (char*)&p_chk->common.i_chunk_fourcc,
                 (char*)&p_chk->list.i_type,
                 p_chk->common.i_chunk_size,
                 p_chk->common.i_chunk_pos );
    }
    else
    {
        sprintf( str + i_level * 5,
                 "+ %4.4s size:"I64Fu" pos:"I64Fu,
                 (char*)&p_chk->common.i_chunk_fourcc,
                 p_chk->common.i_chunk_size,
                 p_chk->common.i_chunk_pos );
    }
    msg_Dbg( p_obj, "%s", str );

    p_child = p_chk->common.p_first;
    while( p_child )
    {
        AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
        p_child = p_child->common.p_next;
    }
}
823
824

int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
825
826
827
{
    avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
    avi_chunk_t      *p_chk;
828
829
830
    vlc_bool_t b_seekable;

    stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
831

832
    p_list->i_chunk_pos  = 0;
833
    p_list->i_chunk_size = stream_Size( s );
834
835
836
837
838
839
    p_list->i_chunk_fourcc = AVIFOURCC_LIST;
    p_list->p_father = NULL;
    p_list->p_next  = NULL;
    p_list->p_first = NULL;
    p_list->p_last  = NULL;

gbazin's avatar
   
gbazin committed
840
    p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
841

842
843
844
845
846
847
848
849
850
851
852
853
854
855
    for( ; ; )
    {
        p_chk = malloc( sizeof( avi_chunk_t ) );
        memset( p_chk, 0, sizeof( avi_chunk_t ) );
        if( !p_root->common.p_first )
        {
            p_root->common.p_first = p_chk;
        }
        else
        {
            p_root->common.p_last->common.p_next = p_chk;
        }
        p_root->common.p_last = p_chk;

856
857
        if( AVI_ChunkRead( s, p_chk, p_root ) ||
           ( stream_Tell( s ) >=
858
859
              (off_t)p_chk->common.p_father->common.i_chunk_pos +
               (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
860
861
862
863
864
865
866
867
868
        {
            break;
        }
        /* If we can't seek then stop when we 've found first RIFF-AVI */
        if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
            p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
        {
            break;
        }
869
    }
870

871
    AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
872
    return VLC_SUCCESS;
873
874
}

875
void AVI_ChunkFreeRoot( stream_t *s,
876
877
                        avi_chunk_t  *p_chk )
{
878
    AVI_ChunkFree( s, p_chk );
879
880
881
}


882
int  _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
883
884
885
886
887
888
{
    int i_count;
    avi_chunk_t *p_child;

    if( !p_chk )
    {
889
        return 0;
890
891
892
893
894
895
896
    }

    i_count = 0;
    p_child = p_chk->common.p_first;
    while( p_child )
    {
        if( p_child->common.i_chunk_fourcc == i_fourcc ||
897
            ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
898
899
900
901
902
903
              p_child->list.i_type == i_fourcc ) )
        {
            i_count++;
        }
        p_child = p_child->common.p_next;
    }
904
    return i_count;
905
906
}

907
908
void *_AVI_ChunkFind( avi_chunk_t *p_chk,
                      vlc_fourcc_t i_fourcc, int i_number )
909
910
911
912
{
    avi_chunk_t *p_child;
    if( !p_chk )
    {
913
        return NULL;
914
915
916
917
918
919
    }
    p_child = p_chk->common.p_first;

    while( p_child )
    {
        if( p_child->common.i_chunk_fourcc == i_fourcc ||
920
            ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
921
922
923
924
925
              p_child->list.i_type == i_fourcc ) )
        {
            if( i_number == 0 )
            {
                /* We found it */
926
                return p_child;
927
928
929
930
931
932
            }

            i_number--;
        }
        p_child = p_child->common.p_next;
    }
933
    return NULL;
934
935
}

936