libavi.c 39.9 KB
Newer Older
1
/*****************************************************************************
hartman's avatar
hartman committed
2
 * libavi.c : LibAVI
3
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2001 VLC authors and VideoLAN
5
 * $Id$
6
 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
8 9 10
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13 14
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
17
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19 20
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23
 *****************************************************************************/


24 25 26 27
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

28
#include <vlc_common.h>
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
29
#include <vlc_demux.h>                                   /* stream_*, *_ES */
30
#include <vlc_codecs.h>                            /* VLC_BITMAPINFOHEADER */
31 32 33

#include "libavi.h"

34 35
#include <limits.h>

36
#ifndef NDEBUG
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
37
# define AVI_DEBUG 1
38
#endif
39

40
#define __EVEN( x ) (((x) + 1) & ~1)
41

42
static vlc_fourcc_t GetFOURCC( const uint8_t *p_buff )
43
{
44
    return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
45
}
46

47 48 49 50 51 52 53 54 55 56
static uint64_t AVI_ChunkSize( const avi_chunk_t *p_ck )
{
    return __EVEN(p_ck->common.i_chunk_size) + 8;
}

static uint64_t AVI_ChunkEnd( const avi_chunk_t *p_ck )
{
    return p_ck->common.i_chunk_pos + AVI_ChunkSize( p_ck );
}

57 58 59 60 61
/****************************************************************************
 *
 * Basics functions to manipulates chunks
 *
 ****************************************************************************/
62
static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
63
{
64
    const uint8_t *p_peek;
65 66 67

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

68
    if( vlc_stream_Peek( s, &p_peek, 8 ) < 8 )
69
        return VLC_EGENERIC;
70

71
    p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
72
    p_chk->common.i_chunk_size   = GetDWLE( p_peek + 4 );
73
    p_chk->common.i_chunk_pos    = vlc_stream_Tell( s );
74

75 76 77
    if( p_chk->common.i_chunk_size >= UINT64_MAX - 8 ||
        p_chk->common.i_chunk_pos > UINT64_MAX - 8 ||
        UINT64_MAX - p_chk->common.i_chunk_pos - 8 < __EVEN(p_chk->common.i_chunk_size) )
78 79
        return VLC_EGENERIC;

80

81
#ifdef AVI_DEBUG
82
    msg_Dbg( (vlc_object_t*)s,
83
             "found chunk, fourcc: %4.4s size:%"PRIu64" pos:%"PRIu64,
84
             (char*)&p_chk->common.i_chunk_fourcc,
85 86 87
             p_chk->common.i_chunk_size,
             p_chk->common.i_chunk_pos );
#endif
88
    return VLC_SUCCESS;
89 90
}

91
static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
92 93
{
    avi_chunk_t chk;
94

95 96
    if( !p_chk )
    {
97
        if( AVI_ChunkReadCommon( s, &chk ) )
98
        {
99
            return VLC_EGENERIC;
100 101 102 103 104 105
        }
        p_chk = &chk;
    }

    if( p_chk->common.p_father )
    {
106
        if( AVI_ChunkEnd( p_chk->common.p_father ) < AVI_ChunkEnd( p_chk ) )
107
        {
108
            return VLC_EGENERIC;
109 110
        }
    }
111 112

    bool b_seekable = false;
113
    const uint64_t i_offset = AVI_ChunkEnd( p_chk );
114
    if ( !vlc_stream_Control(s, STREAM_CAN_SEEK, &b_seekable) && b_seekable )
115
    {
116
        return vlc_stream_Seek( s, i_offset );
117 118 119
    }
    else
    {
120 121
        ssize_t i_read = i_offset - vlc_stream_Tell( s );
        return (i_read >=0 && vlc_stream_Read( s, NULL, i_read ) == i_read) ?
122 123
                    VLC_SUCCESS : VLC_EGENERIC;
    }
124 125 126 127
}

/****************************************************************************
 *
128
 * Functions to read chunks
129 130
 *
 ****************************************************************************/
131
static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
132 133
{
    avi_chunk_t *p_chk;
134
    const uint8_t *p_peek;
135
    bool b_seekable;
136
    int i_ret = VLC_SUCCESS;
137

138
    if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 4 )
139 140
    {
        /* empty box */
141
        msg_Warn( (vlc_object_t*)s, "empty list chunk" );
142
        return VLC_EGENERIC;
143
    }
144
    if( vlc_stream_Peek( s, &p_peek, 12 ) < 12 )
145
    {
146
        msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
147
        return VLC_EGENERIC;
148
    }
149

150
    vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
151

152
    p_container->list.i_type = GetFOURCC( p_peek + 8 );
153

154 155 156 157 158 159 160
    /* XXX fixed for on2 hack */
    if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
    {
        p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
        p_container->list.i_type = AVIFOURCC_AVI;
    }

161 162 163
    if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
        p_container->list.i_type == AVIFOURCC_movi )
    {
164 165
        if( !b_seekable )
            return VLC_SUCCESS;
hartman's avatar
hartman committed
166
        msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
167
        return AVI_NextChunk( s, p_container ); /* points at begining of LIST-movi if not seekable */
168 169
    }

170
    if( vlc_stream_Read( s, NULL, 12 ) != 12 )
171
    {
172
        msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
173 174
        return VLC_EGENERIC;
    }
175

176
#ifdef AVI_DEBUG
177
    msg_Dbg( (vlc_object_t*)s,
178 179
             "found LIST chunk: \'%4.4s\'",
             (char*)&p_container->list.i_type );
180
#endif
181
    msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
182 183
    for( ; ; )
    {
184 185 186 187
        p_chk = calloc( 1, sizeof( avi_chunk_t ) );
        if( !p_chk )
            return VLC_EGENERIC;

188 189 190 191 192 193 194 195 196 197
        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;

198 199
        i_ret = AVI_ChunkRead( s, p_chk, p_container );
        if( i_ret )
200 201 202 203
        {
            break;
        }
        if( p_chk->common.p_father->common.i_chunk_size > 0 &&
204
            vlc_stream_Tell( s ) >= AVI_ChunkEnd( p_chk->common.p_father ) )
205 206 207
        {
            break;
        }
208

209 210
        /* If we can't seek then stop when we 've found LIST-movi */
        if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
211 212
            p_chk->list.i_type == AVIFOURCC_movi &&
            ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
213 214 215 216
        {
            break;
        }

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

220
    if ( i_ret == AVI_ZERO_FOURCC ) return i_ret;
221
    return VLC_SUCCESS;
222 223
}

224 225 226 227 228 229 230 231
/* Allow to append indexes after starting playback */
int AVI_ChunkFetchIndexes( stream_t *s, avi_chunk_t *p_riff )
{
    avi_chunk_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
    if ( !p_movi )
        return VLC_EGENERIC;

    avi_chunk_t *p_chk;
232
    const uint64_t i_indexpos = AVI_ChunkEnd( p_movi );
233 234 235
    bool b_seekable = false;
    int i_ret = VLC_SUCCESS;

236 237
    vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
    if ( !b_seekable || vlc_stream_Seek( s, i_indexpos ) )
238 239 240 241
        return VLC_EGENERIC;

    for( ; ; )
    {
242 243 244 245 246 247 248
        p_chk = calloc( 1, sizeof( avi_chunk_t ) );
        if( !p_chk )
        {
            i_ret = VLC_EGENERIC;
            break;
        }

249 250 251 252 253 254 255 256 257 258 259
        if (unlikely( !p_riff->common.p_first ))
            p_riff->common.p_first = p_chk;
        else
            p_riff->common.p_last->common.p_next = p_chk;
        p_riff->common.p_last = p_chk;

        i_ret = AVI_ChunkRead( s, p_chk, p_riff );
        if( i_ret )
            break;

        if( p_chk->common.p_father->common.i_chunk_size > 0 &&
260
           ( vlc_stream_Tell( s ) >
261 262
             p_chk->common.p_father->common.i_chunk_pos +
             __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        {
            break;
        }

        /* If we can't seek then stop when we 've found any index */
        if( p_chk->common.i_chunk_fourcc == AVIFOURCC_indx ||
            p_chk->common.i_chunk_fourcc == AVIFOURCC_idx1 )
        {
            break;
        }

    }

    return i_ret;
}

279
#define AVI_READCHUNK_ENTER \
280
    int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
281 282 283 284 285
    if( i_read > 100000000 ) \
    { \
        msg_Err( s, "Big chunk ignored" ); \
        return VLC_EGENERIC; \
    } \
286
    uint8_t  *p_read, *p_buff;    \
287 288
    if( !( p_read = p_buff = malloc(i_read ) ) ) \
    { \
289
        return VLC_EGENERIC; \
290
    } \
291
    i_read = vlc_stream_Read( s, p_read, i_read ); \
292 293
    if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
    { \
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
294
        free( p_buff ); \
295 296
        return VLC_EGENERIC; \
    }\
297 298
    p_read += 8; \
    i_read -= 8
299

300 301 302 303 304 305 306 307 308
#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 \

309
#define AVI_READCHUNK_EXIT( code ) \
310 311 312 313
    do { \
        free( p_buff ); \
        return code; \
    } while(0)
314

315 316 317 318 319
static inline uint8_t GetB( uint8_t *ptr )
{
    return *ptr;
}

320
#define AVI_READ1BYTE( i_byte ) \
321
    AVI_READ( i_byte, GetB, 1 )
322

323
#define AVI_READ2BYTES( i_word ) \
324
    AVI_READ( i_word, GetWLE, 2 )
325 326

#define AVI_READ4BYTES( i_dword ) \
327
    AVI_READ( i_dword, GetDWLE, 4 )
328

329 330
#define AVI_READ8BYTES( i_qword ) \
    AVI_READ( i_qword, GetQWLE, 8 )
331

332
#define AVI_READFOURCC( i_dword ) \
333
    AVI_READ( i_dword, GetFOURCC, 4 )
334

335
static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
336 337 338
{
    AVI_READCHUNK_ENTER;

339
    p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
    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
355 356
    msg_Dbg( (vlc_object_t*)s,
             "avih: streams:%d flags:%s%s%s%s %dx%d",
357 358 359 360 361 362
             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 );
363
#endif
364
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
365 366
}

367
static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
    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
384
    msg_Dbg( (vlc_object_t*)s,
385 386
             "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
             (char*)&p_chk->strh.i_type,
387 388
             p_chk->strh.i_handler,
             p_chk->strh.i_samplesize,
389
             ( p_chk->strh.i_scale ?
390 391
                (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
#endif
392

393
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
394 395
}

396
static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
397 398 399 400 401 402
{
    avi_chunk_t *p_strh;

    AVI_READCHUNK_ENTER;
    if( p_chk->common.p_father == NULL )
    {
403
        msg_Err( (vlc_object_t*)s, "malformed avi file" );
404
        AVI_READCHUNK_EXIT( VLC_EGENERIC );
405 406 407
    }
    if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
    {
408
        msg_Err( (vlc_object_t*)s, "malformed avi file" );
409
        AVI_READCHUNK_EXIT( p_chk->common.i_chunk_size > 0  ? VLC_EGENERIC : AVI_ZEROSIZED_CHUNK );
410
    }
411

412 413 414
    switch( p_strh->strh.i_type )
    {
        case( AVIFOURCC_auds ):
415
            p_chk->strf.auds.i_cat = AUDIO_ES;
416
            p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
417 418 419 420
            if ( !p_chk->strf.auds.p_wf )
            {
                AVI_READCHUNK_EXIT( VLC_ENOMEM );
            }
421 422 423 424 425 426
            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 );
427

428 429
            if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
                 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
430
            {
431
                AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
432

433 434 435 436 437 438 439
                /* 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 );
                }
440

hartman's avatar
hartman committed
441 442
                if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
                {
443
                    msg_Dbg( s, "Extended header found" );
hartman's avatar
hartman committed
444
                }
445 446 447 448 449 450 451
            }
            else
            {
                p_chk->strf.auds.p_wf->cbSize = 0;
            }
            if( p_chk->strf.auds.p_wf->cbSize > 0 )
            {
452
                memcpy( &p_chk->strf.auds.p_wf[1] ,
Rémi Denis-Courmont's avatar
Typo  
Rémi Denis-Courmont committed
453
                        p_buff + 8 + sizeof( WAVEFORMATEX ),    /*  8=fourcc+size */
454
                        p_chk->strf.auds.p_wf->cbSize );
455 456
            }
#ifdef AVI_DEBUG
457
            msg_Dbg( (vlc_object_t*)s,
458
                     "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkbps",
459 460 461 462
                     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,
463
                     p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1000 );
464 465 466
#endif
            break;
        case( AVIFOURCC_vids ):
hartman's avatar
hartman committed
467
            p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
468
            p_chk->strf.vids.i_cat = VIDEO_ES;
469
            p_chk->strf.vids.p_bih = malloc( __MAX( p_chk->common.i_chunk_size,
470
                                         sizeof( *p_chk->strf.vids.p_bih ) ) );
471 472 473 474
            if ( !p_chk->strf.vids.p_bih )
            {
                AVI_READCHUNK_EXIT( VLC_ENOMEM );
            }
gbazin's avatar
 
gbazin committed
475 476 477 478 479 480 481 482 483 484 485
            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 );
486
            if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
487 488 489
            {
                p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
            }
490
            if ( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
491
            {
492 493
                uint64_t i_extrasize = p_chk->common.i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);

494
                /* There's a color palette appended, set up VLC_BITMAPINFO */
495
                memcpy( &p_chk->strf.vids.p_bih[1],
496
                        p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
497 498 499
                        i_extrasize );

                if ( !p_chk->strf.vids.p_bih->biClrUsed )
500 501 502 503 504 505
                {
                    if( p_chk->strf.vids.p_bih->biBitCount < 32 )
                        p_chk->strf.vids.p_bih->biClrUsed = (1 << p_chk->strf.vids.p_bih->biBitCount);
                    else
                        p_chk->strf.vids.p_bih->biBitCount = UINT16_MAX;
                }
506

507
                if( i_extrasize / sizeof(uint32_t) > UINT32_MAX )
508 509 510 511
                    p_chk->strf.vids.p_bih->biClrUsed = UINT32_MAX;
                else
                {
                    p_chk->strf.vids.p_bih->biClrUsed =
512
                            __MIN( i_extrasize / sizeof(uint32_t),
513 514
                                   p_chk->strf.vids.p_bih->biClrUsed );
                }
515 516 517

                /* stay within VLC's limits */
                p_chk->strf.vids.p_bih->biClrUsed =
518
                    __MIN( VIDEO_PALETTE_COLORS_MAX, p_chk->strf.vids.p_bih->biClrUsed );
519
            }
520
            else p_chk->strf.vids.p_bih->biClrUsed = 0;
521
#ifdef AVI_DEBUG
522
            msg_Dbg( (vlc_object_t*)s,
523
                     "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
524
                     (char*)&p_chk->strf.vids.p_bih->biCompression,
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
525 526
                     (uint32_t)p_chk->strf.vids.p_bih->biWidth,
                     (uint32_t)p_chk->strf.vids.p_bih->biHeight,
gbazin's avatar
 
gbazin committed
527 528
                     p_chk->strf.vids.p_bih->biPlanes,
                     p_chk->strf.vids.p_bih->biBitCount );
529 530
#endif
            break;
531 532 533 534
        case AVIFOURCC_iavs:
        case AVIFOURCC_ivas:
            p_chk->strf.common.i_cat = UNKNOWN_ES;
            break;
535 536 537
        case( AVIFOURCC_txts ):
            p_chk->strf.common.i_cat = SPU_ES;
            break;
538
        default:
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
539 540
            msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
                    (char*)&p_strh->strh.i_type );
541
            p_chk->strf.common.i_cat = UNKNOWN_ES;
542 543
            break;
    }
544
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
545
}
546
static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
547
{
548
    avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
549
    if( p_strf->common.i_cat == AUDIO_ES )
550
    {
zorglub's avatar
zorglub committed
551
        FREENULL( p_strf->auds.p_wf );
552 553 554
    }
    else if( p_strf->common.i_cat == VIDEO_ES )
    {
zorglub's avatar
zorglub committed
555
        FREENULL( p_strf->vids.p_bih );
556
    }
557 558
}

559
static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
560
{
561 562 563
    if ( p_chk->common.i_chunk_size == 0 )
    {
        msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
564
        return AVI_ZEROSIZED_CHUNK;
565 566
    }

567
    AVI_READCHUNK_ENTER;
568 569 570 571
    p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
    if( p_chk->strd.p_data )
        memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
    AVI_READCHUNK_EXIT( p_chk->strd.p_data ? VLC_SUCCESS : VLC_EGENERIC );
572 573
}

574 575
static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
{
576
    free( p_chk->strd.p_data );
577 578
}

579
static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
580
{
581
    unsigned int i_count, i_index;
582 583 584

    AVI_READCHUNK_ENTER;

585
    i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
586 587 588 589 590

    p_chk->idx1.i_entry_count = i_count;
    p_chk->idx1.i_entry_max   = i_count;
    if( i_count > 0 )
    {
591 592 593
        p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
        if( !p_chk->idx1.entry )
            AVI_READCHUNK_EXIT( VLC_EGENERIC );
594 595 596

        for( i_index = 0; i_index < i_count ; i_index++ )
        {
597
            AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
598 599 600 601 602 603 604 605 606 607
            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
608
    msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
609
#endif
610
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
611 612
}

613
static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
614 615 616
{
    p_chk->idx1.i_entry_count = 0;
    p_chk->idx1.i_entry_max   = 0;
617
    FREENULL( p_chk->idx1.entry );
618 619
}

620 621


622
static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
623 624
{
    unsigned int i_count, i;
625
    int          i_ret = VLC_SUCCESS;
626
    int32_t      i_dummy;
627
    VLC_UNUSED(i_dummy);
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
    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;
649 650
        p_indx->idx.std = calloc( i_count, sizeof( indx_std_entry_t ) );
        if( i_count == 0 || p_indx->idx.std )
651
        {
652 653 654 655 656
            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 );
            }
657
        }
658
        else i_ret = VLC_EGENERIC;
659 660 661 662 663 664 665 666
    }
    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;
667 668
        p_indx->idx.field = calloc( i_count, sizeof( indx_field_entry_t ) );
        if( i_count == 0 || p_indx->idx.field )
669
        {
670 671 672 673 674 675
            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 );
            }
676
        }
677
        else i_ret = VLC_EGENERIC;
678 679 680 681 682 683 684 685 686 687
    }
    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;
688 689
        p_indx->idx.super = calloc( i_count, sizeof( indx_super_entry_t ) );
        if( i_count == 0 || p_indx->idx.super )
690
        {
691 692 693 694 695 696
            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 );
            }
697
        }
698
        else i_ret = VLC_EGENERIC;
699 700 701
    }
    else
    {
Sebastian Ramacher's avatar
Sebastian Ramacher committed
702
        msg_Warn( (vlc_object_t*)s, "unknown type/subtype index" );
703 704 705
    }

#ifdef AVI_DEBUG
706 707
    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 );
708
#endif
709
    AVI_READCHUNK_EXIT( i_ret );
710
}
711
static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
712 713 714
{
    avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;

zorglub's avatar
zorglub committed
715 716 717
    FREENULL( p_indx->idx.std );
    FREENULL( p_indx->idx.field );
    FREENULL( p_indx->idx.super );
718 719
}

720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745
static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
{
    avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;

    AVI_READCHUNK_ENTER;

    AVI_READ4BYTES( p_vprp->i_video_format_token );
    AVI_READ4BYTES( p_vprp->i_video_standard );
    AVI_READ4BYTES( p_vprp->i_vertical_refresh );
    AVI_READ4BYTES( p_vprp->i_h_total_in_t );
    AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
    AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
    AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
    AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
    AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
    for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
    {
        AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
        AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
        AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
        AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
        AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
        AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
        AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
        AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
    }
746

747 748 749 750 751 752
#ifdef AVI_DEBUG
    msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
             p_vprp->i_video_format_token, p_vprp->i_video_standard );
#endif
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
}
753

Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
{
    avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;

    AVI_READCHUNK_ENTER;

    AVI_READ4BYTES( p_dmlh->dwTotalFrames );

#ifdef AVI_DEBUG
    msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
             p_dmlh->dwTotalFrames );
#endif
    AVI_READCHUNK_EXIT( VLC_SUCCESS );
}

769
static const struct
770
{
771
    vlc_fourcc_t i_fourcc;
zorglub's avatar
zorglub committed
772
    const char *psz_type;
773 774
} AVI_strz_type[] =
{
775 776 777 778 779 780 781 782 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
    { 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, "Engineer" },
    { AVIFOURCC_IGNR, "Genre" },
    { AVIFOURCC_ISGN, "Secondary 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" },
    { AVIFOURCC_IWRI, "Writer" },
    { AVIFOURCC_IPRO, "Producer" },
    { AVIFOURCC_ICNM, "Cinematographer" },
    { AVIFOURCC_IPDS, "Production designer" },
    { AVIFOURCC_IEDT, "Editor" },
    { AVIFOURCC_ICDS, "Costume designer" },
    { AVIFOURCC_IMUS, "Music" },
    { AVIFOURCC_ISTD, "Production studio" },
    { AVIFOURCC_IDST, "Distributor" },
    { AVIFOURCC_ICNT, "Country" },
    { AVIFOURCC_ISTR, "Starring" },
    { AVIFOURCC_IFRM, "Total number of parts" },
    { AVIFOURCC_strn, "Stream name" },
814 815 816 817 818 819 820 821 822 823
    { AVIFOURCC_IAS1, "First Language" },
    { AVIFOURCC_IAS2, "Second Language" },
    { AVIFOURCC_IAS3, "Third Language" },
    { AVIFOURCC_IAS4, "Fourth Language" },
    { AVIFOURCC_IAS5, "Fifth Language" },
    { AVIFOURCC_IAS6, "Sixth Language" },
    { AVIFOURCC_IAS7, "Seventh Language" },
    { AVIFOURCC_IAS8, "Eighth Language" },
    { AVIFOURCC_IAS9, "Ninth Language" },

824 825
    { 0,              "???" }
};
826

827
static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
828 829 830 831 832 833 834 835 836 837 838 839 840 841
{
    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 );
842 843
    p_strz->p_str = malloc( p_strz->i_chunk_size + 1 );
    if( !p_strz->p_type || !p_strz->p_str )
844
    {
845 846 847
        free( p_strz->p_type );
        free( p_strz->p_str );
        AVI_READCHUNK_EXIT( VLC_EGENERIC );
848
    }
849
    memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
850
    p_strz->p_str[p_strz->i_chunk_size] = 0;
851

852
#ifdef AVI_DEBUG
853
    msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
854