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