libavi.c 29.7 KB
Newer Older
1
/*****************************************************************************
hartman's avatar
hartman committed
2
 * libavi.c : LibAVI
3
 *****************************************************************************
4
 * Copyright (C) 2001 the VideoLAN team
5
 * $Id$
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
 * 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
dionoea's avatar
dionoea committed
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23 24
 *****************************************************************************/


#include <vlc/vlc.h>
zorglub's avatar
zorglub committed
25 26
#include <vlc_demux.h>
#include <vlc_codecs.h>                                /* BITMAPINFOHEADER */
27 28 29 30

#include "libavi.h"

#define AVI_DEBUG 1
31

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

34
static vlc_fourcc_t GetFOURCC( const byte_t *p_buff )
35
{
36
    return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
37
}
38

39 40
#define AVI_ChunkFree( a, b ) _AVI_ChunkFree( (a), (avi_chunk_t*)(b) )
void    _AVI_ChunkFree( stream_t *, avi_chunk_t *p_chk );
41

42 43 44 45 46
/****************************************************************************
 *
 * Basics functions to manipulates chunks
 *
 ****************************************************************************/
47
static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
48
{
49
    const uint8_t *p_peek;
50 51 52 53
    int i_peek;

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

54
    if( ( i_peek = stream_Peek( s, &p_peek, 8 ) ) < 8 )
55
    {
56
        return VLC_EGENERIC;
57
    }
58

59
    p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
60
    p_chk->common.i_chunk_size   = GetDWLE( p_peek + 4 );
61
    p_chk->common.i_chunk_pos    = stream_Tell( s );
62 63 64 65 66

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

68
#ifdef AVI_DEBUG
69
    msg_Dbg( (vlc_object_t*)s,
hartman's avatar
hartman committed
70
             "found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
71
             p_chk->common.i_chunk_fourcc,
72
             (char*)&p_chk->common.i_chunk_fourcc,
73 74 75
             p_chk->common.i_chunk_size,
             p_chk->common.i_chunk_pos );
#endif
76
    return VLC_SUCCESS;
77 78
}

79
static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
80 81
{
    avi_chunk_t chk;
82

83 84
    if( !p_chk )
    {
85
        if( AVI_ChunkReadCommon( s, &chk ) )
86
        {
87
            return VLC_EGENERIC;
88 89 90 91 92 93
        }
        p_chk = &chk;
    }

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

/****************************************************************************
 *
108
 * Functions to read chunks
109 110
 *
 ****************************************************************************/
111
static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
112 113
{
    avi_chunk_t *p_chk;
114
    const uint8_t *p_peek;
115
    vlc_bool_t b_seekable;
116

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

    stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );

131
    p_container->list.i_type = GetFOURCC( p_peek + 8 );
132 133 134 135

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

144
    if( stream_Read( s, NULL, 12 ) != 12 )
145
    {
146
        msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
147 148
        return VLC_EGENERIC;
    }
149

150
#ifdef AVI_DEBUG
151
    msg_Dbg( (vlc_object_t*)s,
152 153
             "found LIST chunk: \'%4.4s\'",
             (char*)&p_container->list.i_type );
154
#endif
155
    msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
156 157 158 159 160 161 162 163 164 165 166 167 168 169
    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;

170 171 172 173 174
        if( AVI_ChunkRead( s, p_chk, p_container ) )
        {
            break;
        }
        if( p_chk->common.p_father->common.i_chunk_size > 0 &&
175
           ( stream_Tell( s ) >
176 177
              (off_t)p_chk->common.p_father->common.i_chunk_pos +
               (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
178 179 180
        {
            break;
        }
181

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

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

193
    return VLC_SUCCESS;
194 195 196
}

#define AVI_READCHUNK_ENTER \
197 198
    int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
    uint8_t  *p_read, *p_buff;    \
199 200
    if( !( p_read = p_buff = malloc(i_read ) ) ) \
    { \
201
        return VLC_EGENERIC; \
202
    } \
203
    i_read = stream_Read( s, p_read, i_read ); \
204 205
    if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
    { \
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
206
        free( p_buff ); \
207 208
        return VLC_EGENERIC; \
    }\
209 210
    p_read += 8; \
    i_read -= 8
211

212 213 214 215 216 217 218 219 220
#define AVI_READ( res, func, size ) \
    if( i_read < size ) { \
        free( p_buff); \
        return VLC_EGENERIC; \
    } \
    i_read -= size; \
    res = func( p_read ); \
    p_read += size \

221 222
#define AVI_READCHUNK_EXIT( code ) \
    free( p_buff ); \
223
    return code
224

225 226 227 228 229
static inline uint8_t GetB( uint8_t *ptr )
{
    return *ptr;
}

230
#define AVI_READ1BYTE( i_byte ) \
231
    AVI_READ( i_byte, GetB, 1 )
232

233
#define AVI_READ2BYTES( i_word ) \
234
    AVI_READ( i_word, GetWLE, 2 )
235 236

#define AVI_READ4BYTES( i_dword ) \
237
    AVI_READ( i_dword, GetDWLE, 4 )
238

239 240
#define AVI_READ8BYTES( i_qword ) \
    AVI_READ( i_qword, GetQWLE, 8 )
241

242
#define AVI_READFOURCC( i_dword ) \
243
    AVI_READ( i_dword, GetFOURCC, 4 )
244

245
static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
{
    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
264 265
    msg_Dbg( (vlc_object_t*)s,
             "avih: streams:%d flags:%s%s%s%s %dx%d",
266 267 268 269 270 271
             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 );
272
#endif
273
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
274 275
}

276
static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
{
    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
293
    msg_Dbg( (vlc_object_t*)s,
294 295
             "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
             (char*)&p_chk->strh.i_type,
296 297
             p_chk->strh.i_handler,
             p_chk->strh.i_samplesize,
298
             ( p_chk->strh.i_scale ?
299 300
                (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
#endif
301

302
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
303 304
}

305
static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
306 307 308 309 310 311
{
    avi_chunk_t *p_strh;

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

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

425
static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
426 427 428
{
    AVI_READCHUNK_ENTER;
    p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
429
    memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
430
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
431 432
}

433 434 435 436 437
static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
{
    if( p_chk->strd.p_data ) free( p_chk->strd.p_data );
}

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

    AVI_READCHUNK_ENTER;

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

    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++ )
        {
454
            AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
455 456 457 458 459 460 461 462 463 464
            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
465
    msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
466
#endif
467
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
468 469
}

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

477 478


479
static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
480 481 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
{
    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
    {
547
        msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
548 549 550
    }

#ifdef AVI_DEBUG
551
    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 );
552 553 554
#endif
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
}
555
static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
556 557 558
{
    avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;

zorglub's avatar
zorglub committed
559 560 561
    FREENULL( p_indx->idx.std );
    FREENULL( p_indx->idx.field );
    FREENULL( p_indx->idx.super );
562 563 564 565 566
}



static struct
567
{
568
    vlc_fourcc_t i_fourcc;
zorglub's avatar
zorglub committed
569
    const char *psz_type;
570 571 572 573 574 575 576 577 578 579 580
} 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" },
hartman's avatar
hartman committed
581
    { AVIFOURCC_IENG, "engineer" },
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
    { 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" },
597
    { AVIFOURCC_strn, "stream name" },
598 599
    { 0,              "???" }
};
600
static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
{
    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;
622

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

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

}

static struct
{
647
    vlc_fourcc_t i_fourcc;
648 649
    int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
    void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
650 651 652 653 654 655 656
} 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 },
657
    { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
658
    { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
659
    { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
660 661 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
    { 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 },
688
    { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
689 690 691
    { 0,           NULL,               NULL }
};

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

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

    if( !p_chk )
    {
711
        return VLC_EGENERIC;
712 713
    }

714
    if( AVI_ChunkReadCommon( s, p_chk ) )
715
    {
716
        msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
717
        return VLC_EGENERIC;
718
    }
719 720
    if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
    {
721
        msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
722 723
        return VLC_EGENERIC;
    }
724 725 726 727 728
    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 )
    {
729
        return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
730
    }
731
    else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
732
               ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
733 734
             ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
               ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
735 736
    {
        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;
    }