]> git.sesse.net Git - vlc/blob - modules/demux/avi/libavi.c
demux: avi: handle zero sized lists (fix #9056, fix #8413)
[vlc] / modules / demux / avi / libavi.c
1 /*****************************************************************************
2  * libavi.c : LibAVI
3  *****************************************************************************
4  * Copyright (C) 2001 VLC authors and VideoLAN
5  * $Id$
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <vlc_common.h>
29 #include <vlc_demux.h>                                   /* stream_*, *_ES */
30 #include <vlc_codecs.h>                            /* VLC_BITMAPINFOHEADER */
31
32 #include "libavi.h"
33
34 #ifndef NDEBUG
35 # define AVI_DEBUG 1
36 #endif
37
38 #define __EVEN( x ) (((x) + 1) & ~1)
39
40 static vlc_fourcc_t GetFOURCC( const uint8_t *p_buff )
41 {
42     return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
43 }
44
45 /****************************************************************************
46  *
47  * Basics functions to manipulates chunks
48  *
49  ****************************************************************************/
50 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
51 {
52     const uint8_t *p_peek;
53
54     memset( p_chk, 0, sizeof( avi_chunk_t ) );
55
56     if( stream_Peek( s, &p_peek, 8 ) < 8 )
57         return VLC_EGENERIC;
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     p_chk->common.p_father = NULL;
64     p_chk->common.p_next = NULL;
65     p_chk->common.p_first = NULL;
66     p_chk->common.p_next = NULL;
67
68 #ifdef AVI_DEBUG
69     msg_Dbg( (vlc_object_t*)s,
70              "found chunk, fourcc: %4.4s size:%"PRId64" pos:%"PRId64,
71              (char*)&p_chk->common.i_chunk_fourcc,
72              p_chk->common.i_chunk_size,
73              p_chk->common.i_chunk_pos );
74 #endif
75     return VLC_SUCCESS;
76 }
77
78 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
79 {
80     avi_chunk_t chk;
81
82     if( !p_chk )
83     {
84         if( AVI_ChunkReadCommon( s, &chk ) )
85         {
86             return VLC_EGENERIC;
87         }
88         p_chk = &chk;
89     }
90
91     if( p_chk->common.p_father )
92     {
93         if( p_chk->common.p_father->common.i_chunk_pos +
94                 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
95             p_chk->common.i_chunk_pos +
96                 __EVEN( p_chk->common.i_chunk_size ) + 8 )
97         {
98             return VLC_EGENERIC;
99         }
100     }
101     return stream_Seek( s, p_chk->common.i_chunk_pos +
102                                  __EVEN( p_chk->common.i_chunk_size ) + 8 );
103 }
104
105 /****************************************************************************
106  *
107  * Functions to read chunks
108  *
109  ****************************************************************************/
110 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
111 {
112     avi_chunk_t *p_chk;
113     const uint8_t *p_peek;
114     bool b_seekable;
115     int i_ret = VLC_SUCCESS;
116
117     if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 4 )
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     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
130
131     p_container->list.i_type = GetFOURCC( p_peek + 8 );
132
133     /* XXX fixed for on2 hack */
134     if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
135     {
136         p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
137         p_container->list.i_type = AVIFOURCC_AVI;
138     }
139
140     if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
141         p_container->list.i_type == AVIFOURCC_movi )
142     {
143         msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
144         if( b_seekable )
145         {
146             return AVI_NextChunk( s, p_container );
147         }
148         return VLC_SUCCESS; /* point at begining of LIST-movi */
149     }
150
151     if( stream_Read( s, NULL, 12 ) != 12 )
152     {
153         msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
154         return VLC_EGENERIC;
155     }
156
157 #ifdef AVI_DEBUG
158     msg_Dbg( (vlc_object_t*)s,
159              "found LIST chunk: \'%4.4s\'",
160              (char*)&p_container->list.i_type );
161 #endif
162     msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
163     for( ; ; )
164     {
165         p_chk = xmalloc( sizeof( avi_chunk_t ) );
166         memset( p_chk, 0, sizeof( avi_chunk_t ) );
167         if( !p_container->common.p_first )
168         {
169             p_container->common.p_first = p_chk;
170         }
171         else
172         {
173             p_container->common.p_last->common.p_next = p_chk;
174         }
175         p_container->common.p_last = p_chk;
176
177         if( i_ret = AVI_ChunkRead( s, p_chk, p_container ) )
178         {
179             break;
180         }
181         if( p_chk->common.p_father->common.i_chunk_size > 0 &&
182            ( stream_Tell( s ) >
183               (off_t)p_chk->common.p_father->common.i_chunk_pos +
184                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
185         {
186             break;
187         }
188
189         /* If we can't seek then stop when we 've found LIST-movi */
190         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
191             p_chk->list.i_type == AVIFOURCC_movi &&
192             ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
193         {
194             break;
195         }
196
197     }
198     msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
199
200     if ( i_ret == AVI_ZERO_FOURCC ) return i_ret;
201     return VLC_SUCCESS;
202 }
203
204 #define AVI_READCHUNK_ENTER \
205     int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
206     if( i_read > 100000000 ) \
207     { \
208         msg_Err( s, "Big chunk ignored" ); \
209         return VLC_EGENERIC; \
210     } \
211     uint8_t  *p_read, *p_buff;    \
212     if( !( p_read = p_buff = malloc(i_read ) ) ) \
213     { \
214         return VLC_EGENERIC; \
215     } \
216     i_read = stream_Read( s, p_read, i_read ); \
217     if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
218     { \
219         free( p_buff ); \
220         return VLC_EGENERIC; \
221     }\
222     p_read += 8; \
223     i_read -= 8
224
225 #define AVI_READ( res, func, size ) \
226     if( i_read < size ) { \
227         free( p_buff); \
228         return VLC_EGENERIC; \
229     } \
230     i_read -= size; \
231     res = func( p_read ); \
232     p_read += size \
233
234 #define AVI_READCHUNK_EXIT( code ) \
235     free( p_buff ); \
236     return code
237
238 static inline uint8_t GetB( uint8_t *ptr )
239 {
240     return *ptr;
241 }
242
243 #define AVI_READ1BYTE( i_byte ) \
244     AVI_READ( i_byte, GetB, 1 )
245
246 #define AVI_READ2BYTES( i_word ) \
247     AVI_READ( i_word, GetWLE, 2 )
248
249 #define AVI_READ4BYTES( i_dword ) \
250     AVI_READ( i_dword, GetDWLE, 4 )
251
252 #define AVI_READ8BYTES( i_qword ) \
253     AVI_READ( i_qword, GetQWLE, 8 )
254
255 #define AVI_READFOURCC( i_dword ) \
256     AVI_READ( i_dword, GetFOURCC, 4 )
257
258 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
259 {
260     AVI_READCHUNK_ENTER;
261
262     p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
263     AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
264     AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
265     AVI_READ4BYTES( p_chk->avih.i_reserved1 );
266     AVI_READ4BYTES( p_chk->avih.i_flags );
267     AVI_READ4BYTES( p_chk->avih.i_totalframes );
268     AVI_READ4BYTES( p_chk->avih.i_initialframes );
269     AVI_READ4BYTES( p_chk->avih.i_streams );
270     AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
271     AVI_READ4BYTES( p_chk->avih.i_width );
272     AVI_READ4BYTES( p_chk->avih.i_height );
273     AVI_READ4BYTES( p_chk->avih.i_scale );
274     AVI_READ4BYTES( p_chk->avih.i_rate );
275     AVI_READ4BYTES( p_chk->avih.i_start );
276     AVI_READ4BYTES( p_chk->avih.i_length );
277 #ifdef AVI_DEBUG
278     msg_Dbg( (vlc_object_t*)s,
279              "avih: streams:%d flags:%s%s%s%s %dx%d",
280              p_chk->avih.i_streams,
281              p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
282              p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
283              p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
284              p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
285              p_chk->avih.i_width, p_chk->avih.i_height );
286 #endif
287     AVI_READCHUNK_EXIT( VLC_SUCCESS );
288 }
289
290 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
291 {
292     AVI_READCHUNK_ENTER;
293
294     AVI_READFOURCC( p_chk->strh.i_type );
295     AVI_READFOURCC( p_chk->strh.i_handler );
296     AVI_READ4BYTES( p_chk->strh.i_flags );
297     AVI_READ4BYTES( p_chk->strh.i_reserved1 );
298     AVI_READ4BYTES( p_chk->strh.i_initialframes );
299     AVI_READ4BYTES( p_chk->strh.i_scale );
300     AVI_READ4BYTES( p_chk->strh.i_rate );
301     AVI_READ4BYTES( p_chk->strh.i_start );
302     AVI_READ4BYTES( p_chk->strh.i_length );
303     AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
304     AVI_READ4BYTES( p_chk->strh.i_quality );
305     AVI_READ4BYTES( p_chk->strh.i_samplesize );
306 #ifdef AVI_DEBUG
307     msg_Dbg( (vlc_object_t*)s,
308              "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
309              (char*)&p_chk->strh.i_type,
310              p_chk->strh.i_handler,
311              p_chk->strh.i_samplesize,
312              ( p_chk->strh.i_scale ?
313                 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
314 #endif
315
316     AVI_READCHUNK_EXIT( VLC_SUCCESS );
317 }
318
319 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
320 {
321     avi_chunk_t *p_strh;
322
323     AVI_READCHUNK_ENTER;
324     if( p_chk->common.p_father == NULL )
325     {
326         msg_Err( (vlc_object_t*)s, "malformed avi file" );
327         AVI_READCHUNK_EXIT( VLC_EGENERIC );
328     }
329     if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
330     {
331         msg_Err( (vlc_object_t*)s, "malformed avi file" );
332         AVI_READCHUNK_EXIT( VLC_EGENERIC );
333     }
334
335     switch( p_strh->strh.i_type )
336     {
337         case( AVIFOURCC_auds ):
338             p_chk->strf.auds.i_cat = AUDIO_ES;
339             p_chk->strf.auds.p_wf = xmalloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
340             AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
341             AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
342             AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
343             AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
344             AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
345             AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
346
347             if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
348                  && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
349             {
350                 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
351
352                 /* prevent segfault */
353                 if( p_chk->strf.auds.p_wf->cbSize >
354                         p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
355                 {
356                     p_chk->strf.auds.p_wf->cbSize =
357                         p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
358                 }
359
360                 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
361                 {
362                     msg_Dbg( s, "Extended header found" );
363                 }
364             }
365             else
366             {
367                 p_chk->strf.auds.p_wf->cbSize = 0;
368             }
369             if( p_chk->strf.auds.p_wf->cbSize > 0 )
370             {
371                 memcpy( &p_chk->strf.auds.p_wf[1] ,
372                         p_buff + 8 + sizeof( WAVEFORMATEX ),    /*  8=fourcc+size */
373                         p_chk->strf.auds.p_wf->cbSize );
374             }
375 #ifdef AVI_DEBUG
376             msg_Dbg( (vlc_object_t*)s,
377                      "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
378                      p_chk->strf.auds.p_wf->wFormatTag,
379                      p_chk->strf.auds.p_wf->nChannels,
380                      p_chk->strf.auds.p_wf->nSamplesPerSec,
381                      p_chk->strf.auds.p_wf->wBitsPerSample,
382                      p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
383 #endif
384             break;
385         case( AVIFOURCC_vids ):
386             p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
387             p_chk->strf.vids.i_cat = VIDEO_ES;
388             p_chk->strf.vids.p_bih = xmalloc( __MAX( p_chk->common.i_chunk_size,
389                                          sizeof( *p_chk->strf.vids.p_bih ) ) );
390             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
391             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
392             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
393             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
394             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
395             AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
396             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
397             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
398             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
399             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
400             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
401             if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
402             {
403                 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
404             }
405             if( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
406             {
407                 memcpy( &p_chk->strf.vids.p_bih[1],
408                         p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
409                         p_chk->common.i_chunk_size -sizeof(VLC_BITMAPINFOHEADER) );
410             }
411 #ifdef AVI_DEBUG
412             msg_Dbg( (vlc_object_t*)s,
413                      "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
414                      (char*)&p_chk->strf.vids.p_bih->biCompression,
415                      (uint32_t)p_chk->strf.vids.p_bih->biWidth,
416                      (uint32_t)p_chk->strf.vids.p_bih->biHeight,
417                      p_chk->strf.vids.p_bih->biPlanes,
418                      p_chk->strf.vids.p_bih->biBitCount );
419 #endif
420             break;
421         case AVIFOURCC_iavs:
422         case AVIFOURCC_ivas:
423             p_chk->strf.common.i_cat = UNKNOWN_ES;
424             break;
425         case( AVIFOURCC_txts ):
426             p_chk->strf.common.i_cat = SPU_ES;
427             break;
428         default:
429             msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
430                     (char*)&p_strh->strh.i_type );
431             p_chk->strf.common.i_cat = UNKNOWN_ES;
432             break;
433     }
434     AVI_READCHUNK_EXIT( VLC_SUCCESS );
435 }
436 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
437 {
438     avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
439     if( p_strf->common.i_cat == AUDIO_ES )
440     {
441         FREENULL( p_strf->auds.p_wf );
442     }
443     else if( p_strf->common.i_cat == VIDEO_ES )
444     {
445         FREENULL( p_strf->vids.p_bih );
446     }
447 }
448
449 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
450 {
451     if ( p_chk->common.i_chunk_size == 0 )
452     {
453         msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
454         return AVI_STRD_ZERO_CHUNK;
455     }
456
457     AVI_READCHUNK_ENTER;
458     p_chk->strd.p_data = xmalloc( p_chk->common.i_chunk_size );
459     memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
460     AVI_READCHUNK_EXIT( VLC_SUCCESS );
461 }
462
463 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
464 {
465     free( p_chk->strd.p_data );
466 }
467
468 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
469 {
470     unsigned int i_count, i_index;
471
472     AVI_READCHUNK_ENTER;
473
474     i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
475
476     p_chk->idx1.i_entry_count = i_count;
477     p_chk->idx1.i_entry_max   = i_count;
478     if( i_count > 0 )
479     {
480         p_chk->idx1.entry = xcalloc( i_count, sizeof( idx1_entry_t ) );
481
482         for( i_index = 0; i_index < i_count ; i_index++ )
483         {
484             AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
485             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
486             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
487             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
488         }
489     }
490     else
491     {
492         p_chk->idx1.entry = NULL;
493     }
494 #ifdef AVI_DEBUG
495     msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
496 #endif
497     AVI_READCHUNK_EXIT( VLC_SUCCESS );
498 }
499
500 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
501 {
502     p_chk->idx1.i_entry_count = 0;
503     p_chk->idx1.i_entry_max   = 0;
504     FREENULL( p_chk->idx1.entry );
505 }
506
507
508
509 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
510 {
511     unsigned int i_count, i;
512     int32_t      i_dummy;
513     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
514
515     AVI_READCHUNK_ENTER;
516
517     AVI_READ2BYTES( p_indx->i_longsperentry );
518     AVI_READ1BYTE ( p_indx->i_indexsubtype );
519     AVI_READ1BYTE ( p_indx->i_indextype );
520     AVI_READ4BYTES( p_indx->i_entriesinuse );
521
522     AVI_READ4BYTES( p_indx->i_id );
523     p_indx->idx.std     = NULL;
524     p_indx->idx.field   = NULL;
525     p_indx->idx.super   = NULL;
526
527     if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
528     {
529         AVI_READ8BYTES( p_indx->i_baseoffset );
530         AVI_READ4BYTES( i_dummy );
531
532         i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
533         p_indx->i_entriesinuse = i_count;
534         p_indx->idx.std = xcalloc( i_count, sizeof( indx_std_entry_t ) );
535
536         for( i = 0; i < i_count; i++ )
537         {
538             AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
539             AVI_READ4BYTES( p_indx->idx.std[i].i_size );
540         }
541     }
542     else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
543     {
544         AVI_READ8BYTES( p_indx->i_baseoffset );
545         AVI_READ4BYTES( i_dummy );
546
547         i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
548         p_indx->i_entriesinuse = i_count;
549         p_indx->idx.field = xcalloc( i_count, sizeof( indx_field_entry_t ) );
550         for( i = 0; i < i_count; i++ )
551         {
552             AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
553             AVI_READ4BYTES( p_indx->idx.field[i].i_size );
554             AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
555         }
556     }
557     else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
558     {
559         p_indx->i_baseoffset = 0;
560         AVI_READ4BYTES( i_dummy );
561         AVI_READ4BYTES( i_dummy );
562         AVI_READ4BYTES( i_dummy );
563
564         i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
565         p_indx->i_entriesinuse = i_count;
566         p_indx->idx.super = xcalloc( i_count, sizeof( indx_super_entry_t ) );
567
568         for( i = 0; i < i_count; i++ )
569         {
570             AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
571             AVI_READ4BYTES( p_indx->idx.super[i].i_size );
572             AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
573         }
574     }
575     else
576     {
577         msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
578     }
579
580 #ifdef AVI_DEBUG
581     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 );
582 #endif
583     AVI_READCHUNK_EXIT( VLC_SUCCESS );
584 }
585 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
586 {
587     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
588
589     FREENULL( p_indx->idx.std );
590     FREENULL( p_indx->idx.field );
591     FREENULL( p_indx->idx.super );
592 }
593
594 static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
595 {
596     avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;
597
598     AVI_READCHUNK_ENTER;
599
600     AVI_READ4BYTES( p_vprp->i_video_format_token );
601     AVI_READ4BYTES( p_vprp->i_video_standard );
602     AVI_READ4BYTES( p_vprp->i_vertical_refresh );
603     AVI_READ4BYTES( p_vprp->i_h_total_in_t );
604     AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
605     AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
606     AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
607     AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
608     AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
609     for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
610     {
611         AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
612         AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
613         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
614         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
615         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
616         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
617         AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
618         AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
619     }
620
621 #ifdef AVI_DEBUG
622     msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
623              p_vprp->i_video_format_token, p_vprp->i_video_standard );
624 #endif
625     AVI_READCHUNK_EXIT( VLC_SUCCESS );
626 }
627
628 static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
629 {
630     avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
631
632     AVI_READCHUNK_ENTER;
633
634     AVI_READ4BYTES( p_dmlh->dwTotalFrames );
635
636 #ifdef AVI_DEBUG
637     msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
638              p_dmlh->dwTotalFrames );
639 #endif
640     AVI_READCHUNK_EXIT( VLC_SUCCESS );
641 }
642
643 static const struct
644 {
645     vlc_fourcc_t i_fourcc;
646     const char *psz_type;
647 } AVI_strz_type[] =
648 {
649     { AVIFOURCC_IARL, "Archive location" },
650     { AVIFOURCC_IART, "Artist" },
651     { AVIFOURCC_ICMS, "Commisioned" },
652     { AVIFOURCC_ICMT, "Comments" },
653     { AVIFOURCC_ICOP, "Copyright" },
654     { AVIFOURCC_ICRD, "Creation date" },
655     { AVIFOURCC_ICRP, "Cropped" },
656     { AVIFOURCC_IDIM, "Dimensions" },
657     { AVIFOURCC_IDPI, "Dots per inch" },
658     { AVIFOURCC_IENG, "Engineer" },
659     { AVIFOURCC_IGNR, "Genre" },
660     { AVIFOURCC_ISGN, "Secondary Genre" },
661     { AVIFOURCC_IKEY, "Keywords" },
662     { AVIFOURCC_ILGT, "Lightness" },
663     { AVIFOURCC_IMED, "Medium" },
664     { AVIFOURCC_INAM, "Name" },
665     { AVIFOURCC_IPLT, "Palette setting" },
666     { AVIFOURCC_IPRD, "Product" },
667     { AVIFOURCC_ISBJ, "Subject" },
668     { AVIFOURCC_ISFT, "Software" },
669     { AVIFOURCC_ISHP, "Sharpness" },
670     { AVIFOURCC_ISRC, "Source" },
671     { AVIFOURCC_ISRF, "Source form" },
672     { AVIFOURCC_ITCH, "Technician" },
673     { AVIFOURCC_ISMP, "Time code" },
674     { AVIFOURCC_IDIT, "Digitalization time" },
675     { AVIFOURCC_IWRI, "Writer" },
676     { AVIFOURCC_IPRO, "Producer" },
677     { AVIFOURCC_ICNM, "Cinematographer" },
678     { AVIFOURCC_IPDS, "Production designer" },
679     { AVIFOURCC_IEDT, "Editor" },
680     { AVIFOURCC_ICDS, "Costume designer" },
681     { AVIFOURCC_IMUS, "Music" },
682     { AVIFOURCC_ISTD, "Production studio" },
683     { AVIFOURCC_IDST, "Distributor" },
684     { AVIFOURCC_ICNT, "Country" },
685     { AVIFOURCC_ISTR, "Starring" },
686     { AVIFOURCC_IFRM, "Total number of parts" },
687     { AVIFOURCC_strn, "Stream name" },
688     { 0,              "???" }
689 };
690 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
691 {
692     int i_index;
693     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
694     AVI_READCHUNK_ENTER;
695
696     for( i_index = 0;; i_index++)
697     {
698         if( !AVI_strz_type[i_index].i_fourcc ||
699             AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
700         {
701             break;
702         }
703     }
704     p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
705     p_strz->p_str = xmalloc( p_strz->i_chunk_size + 1);
706
707     if( p_strz->i_chunk_size )
708     {
709         memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
710     }
711     p_strz->p_str[p_strz->i_chunk_size] = 0;
712
713 #ifdef AVI_DEBUG
714     msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
715              (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
716 #endif
717     AVI_READCHUNK_EXIT( VLC_SUCCESS );
718 }
719 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
720 {
721     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
722     FREENULL( p_strz->p_type );
723     FREENULL( p_strz->p_str );
724 }
725
726 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
727 {
728     return AVI_NextChunk( s, p_chk );
729 }
730 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
731 {
732     VLC_UNUSED( p_chk );
733 }
734
735 static const struct
736 {
737     vlc_fourcc_t i_fourcc;
738     int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
739     void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
740 } AVI_Chunk_Function [] =
741 {
742     { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
743     { AVIFOURCC_ON2,  AVI_ChunkRead_list, AVI_ChunkFree_nothing },
744     { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
745     { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
746     { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
747     { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
748     { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
749     { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
750     { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
751     { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
752     { AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
753     { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
754     { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
755
756     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
757     { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
758     { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
759     { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
760     { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
761     { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
762     { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
763     { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
764     { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
765     { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
766     { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
767     { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
768     { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
769     { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
770     { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
771     { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
772     { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
773     { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
774     { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
775     { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
776     { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
777     { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
778     { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
779     { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
780     { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
781     { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
782     { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
783     { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
784     { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
785     { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
786     { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
787     { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
788     { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
789     { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
790     { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
791     { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
792     { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
793     { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
794     { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
795     { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
796     { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
797     { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
798
799
800     { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
801     { 0,           NULL,               NULL }
802 };
803
804 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
805 {
806     unsigned int i_index;
807     for( i_index = 0; ; i_index++ )
808     {
809         if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
810             ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
811         {
812             return i_index;
813         }
814     }
815 }
816
817 int  AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
818 {
819     int i_index;
820
821     if( !p_chk )
822     {
823         msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
824         return VLC_EGENERIC;
825     }
826
827     if( AVI_ChunkReadCommon( s, p_chk ) )
828     {
829         msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
830         return VLC_EGENERIC;
831     }
832
833     if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
834     {
835         msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
836         return AVI_ZERO_FOURCC;
837     }
838     p_chk->common.p_father = p_father;
839
840     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
841     if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
842     {
843         int i_return = AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
844         if ( i_return == AVI_STRD_ZERO_CHUNK || i_return == AVI_ZERO_FOURCC )
845         {
846             if ( !p_father ) return VLC_EGENERIC;
847             return AVI_NextChunk( s, p_father );
848         }
849         return i_return;
850     }
851     else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
852                ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
853              ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
854                ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
855     {
856         p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
857         return AVI_ChunkRead_indx( s, p_chk );
858     }
859
860     msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
861             (char*)&p_chk->common.i_chunk_fourcc );
862     return AVI_NextChunk( s, p_chk );
863 }
864
865 void AVI_ChunkFree( stream_t *s,
866                      avi_chunk_t *p_chk )
867 {
868     int i_index;
869     avi_chunk_t *p_child, *p_next;
870
871     if( !p_chk )
872     {
873         return;
874     }
875
876     /* Free all child chunk */
877     p_child = p_chk->common.p_first;
878     while( p_child )
879     {
880         p_next = p_child->common.p_next;
881         AVI_ChunkFree( s, p_child );
882         free( p_child );
883         p_child = p_next;
884     }
885
886     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
887     if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
888     {
889 #ifdef AVI_DEBUG
890         msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
891                  (char*)&p_chk->common.i_chunk_fourcc );
892 #endif
893         AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
894     }
895     else
896     {
897         msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
898                 (char*)&p_chk->common.i_chunk_fourcc );
899     }
900     p_chk->common.p_first = NULL;
901     p_chk->common.p_last  = NULL;
902
903     return;
904 }
905
906 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
907                                       avi_chunk_t  *p_chk, unsigned i_level )
908 {
909     avi_chunk_t *p_child;
910
911     char str[512];
912     if( i_level >= (sizeof(str) - 1)/4 )
913         return;
914
915     memset( str, ' ', sizeof( str ) );
916     for( unsigned i = 1; i < i_level; i++ )
917     {
918         str[i * 4] = '|';
919     }
920     if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
921         p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2  ||
922         p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
923     {
924         snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
925                  "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
926                  i_level ? '+' : '*',
927                  (char*)&p_chk->common.i_chunk_fourcc,
928                  (char*)&p_chk->list.i_type,
929                  p_chk->common.i_chunk_size,
930                  p_chk->common.i_chunk_pos );
931     }
932     else
933     {
934         snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
935                  "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
936                  (char*)&p_chk->common.i_chunk_fourcc,
937                  p_chk->common.i_chunk_size,
938                  p_chk->common.i_chunk_pos );
939     }
940     msg_Dbg( p_obj, "%s", str );
941
942     p_child = p_chk->common.p_first;
943     while( p_child )
944     {
945         AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
946         p_child = p_child->common.p_next;
947     }
948 }
949
950 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
951 {
952     avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
953     avi_chunk_t      *p_chk;
954     bool b_seekable;
955
956     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
957
958     p_list->i_chunk_pos  = 0;
959     p_list->i_chunk_size = stream_Size( s );
960     p_list->i_chunk_fourcc = AVIFOURCC_LIST;
961     p_list->p_father = NULL;
962     p_list->p_next  = NULL;
963     p_list->p_first = NULL;
964     p_list->p_last  = NULL;
965
966     p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
967
968     for( ; ; )
969     {
970         p_chk = xmalloc( sizeof( avi_chunk_t ) );
971         memset( p_chk, 0, sizeof( avi_chunk_t ) );
972         if( !p_root->common.p_first )
973         {
974             p_root->common.p_first = p_chk;
975         }
976         else
977         {
978             p_root->common.p_last->common.p_next = p_chk;
979         }
980         p_root->common.p_last = p_chk;
981
982         if( AVI_ChunkRead( s, p_chk, p_root ) ||
983            ( stream_Tell( s ) >=
984               (off_t)p_chk->common.p_father->common.i_chunk_pos +
985                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
986         {
987             break;
988         }
989         /* If we can't seek then stop when we 've found first RIFF-AVI */
990         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
991             p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
992         {
993             break;
994         }
995     }
996
997     AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
998     return VLC_SUCCESS;
999 }
1000
1001 void AVI_ChunkFreeRoot( stream_t *s,
1002                         avi_chunk_t  *p_chk )
1003 {
1004     AVI_ChunkFree( s, p_chk );
1005 }
1006
1007
1008 int  _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
1009 {
1010     int i_count;
1011     avi_chunk_t *p_child;
1012
1013     if( !p_chk )
1014     {
1015         return 0;
1016     }
1017
1018     i_count = 0;
1019     p_child = p_chk->common.p_first;
1020     while( p_child )
1021     {
1022         if( p_child->common.i_chunk_fourcc == i_fourcc ||
1023             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1024               p_child->list.i_type == i_fourcc ) )
1025         {
1026             i_count++;
1027         }
1028         p_child = p_child->common.p_next;
1029     }
1030     return i_count;
1031 }
1032
1033 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
1034                       vlc_fourcc_t i_fourcc, int i_number )
1035 {
1036     avi_chunk_t *p_child;
1037     if( !p_chk )
1038     {
1039         return NULL;
1040     }
1041     p_child = p_chk->common.p_first;
1042
1043     while( p_child )
1044     {
1045         if( p_child->common.i_chunk_fourcc == i_fourcc ||
1046             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1047               p_child->list.i_type == i_fourcc ) )
1048         {
1049             if( i_number == 0 )
1050             {
1051                 /* We found it */
1052                 return p_child;
1053             }
1054
1055             i_number--;
1056         }
1057         p_child = p_child->common.p_next;
1058     }
1059     return NULL;
1060 }
1061