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