]> git.sesse.net Git - vlc/blob - modules/demux/avi/libavi.c
demux: remove redundant calls to vlc_object_alive()
[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             if ( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
464             {
465                 uint64_t i_extrasize = p_chk->common.i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
466
467                 /* There's a color palette appended, set up VLC_BITMAPINFO */
468                 memcpy( &p_chk->strf.vids.p_bih[1],
469                         p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
470                         i_extrasize );
471
472                 if ( !p_chk->strf.vids.p_bih->biClrUsed )
473                     p_chk->strf.vids.p_bih->biClrUsed = (1 << p_chk->strf.vids.p_bih->biBitCount);
474
475                 if( i_extrasize / sizeof(uint32_t) > UINT32_MAX )
476                     p_chk->strf.vids.p_bih->biClrUsed = UINT32_MAX;
477                 else
478                 {
479                     p_chk->strf.vids.p_bih->biClrUsed =
480                             __MIN( i_extrasize / sizeof(uint32_t),
481                                    p_chk->strf.vids.p_bih->biClrUsed );
482                 }
483
484                 /* stay within VLC's limits */
485                 p_chk->strf.vids.p_bih->biClrUsed =
486                     __MIN( VIDEO_PALETTE_COLORS_MAX, p_chk->strf.vids.p_bih->biClrUsed );
487             }
488             else p_chk->strf.vids.p_bih->biClrUsed = 0;
489 #ifdef AVI_DEBUG
490             msg_Dbg( (vlc_object_t*)s,
491                      "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
492                      (char*)&p_chk->strf.vids.p_bih->biCompression,
493                      (uint32_t)p_chk->strf.vids.p_bih->biWidth,
494                      (uint32_t)p_chk->strf.vids.p_bih->biHeight,
495                      p_chk->strf.vids.p_bih->biPlanes,
496                      p_chk->strf.vids.p_bih->biBitCount );
497 #endif
498             break;
499         case AVIFOURCC_iavs:
500         case AVIFOURCC_ivas:
501             p_chk->strf.common.i_cat = UNKNOWN_ES;
502             break;
503         case( AVIFOURCC_txts ):
504             p_chk->strf.common.i_cat = SPU_ES;
505             break;
506         default:
507             msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
508                     (char*)&p_strh->strh.i_type );
509             p_chk->strf.common.i_cat = UNKNOWN_ES;
510             break;
511     }
512     AVI_READCHUNK_EXIT( VLC_SUCCESS );
513 }
514 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
515 {
516     avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
517     if( p_strf->common.i_cat == AUDIO_ES )
518     {
519         FREENULL( p_strf->auds.p_wf );
520     }
521     else if( p_strf->common.i_cat == VIDEO_ES )
522     {
523         FREENULL( p_strf->vids.p_bih );
524     }
525 }
526
527 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
528 {
529     if ( p_chk->common.i_chunk_size == 0 )
530     {
531         msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
532         return AVI_STRD_ZERO_CHUNK;
533     }
534
535     AVI_READCHUNK_ENTER;
536     p_chk->strd.p_data = xmalloc( p_chk->common.i_chunk_size );
537     memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
538     AVI_READCHUNK_EXIT( VLC_SUCCESS );
539 }
540
541 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
542 {
543     free( p_chk->strd.p_data );
544 }
545
546 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
547 {
548     unsigned int i_count, i_index;
549
550     AVI_READCHUNK_ENTER;
551
552     i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
553
554     p_chk->idx1.i_entry_count = i_count;
555     p_chk->idx1.i_entry_max   = i_count;
556     if( i_count > 0 )
557     {
558         p_chk->idx1.entry = xcalloc( i_count, sizeof( idx1_entry_t ) );
559
560         for( i_index = 0; i_index < i_count ; i_index++ )
561         {
562             AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
563             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
564             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
565             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
566         }
567     }
568     else
569     {
570         p_chk->idx1.entry = NULL;
571     }
572 #ifdef AVI_DEBUG
573     msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
574 #endif
575     AVI_READCHUNK_EXIT( VLC_SUCCESS );
576 }
577
578 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
579 {
580     p_chk->idx1.i_entry_count = 0;
581     p_chk->idx1.i_entry_max   = 0;
582     FREENULL( p_chk->idx1.entry );
583 }
584
585
586
587 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
588 {
589     unsigned int i_count, i;
590     int32_t      i_dummy;
591     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
592
593     AVI_READCHUNK_ENTER;
594
595     AVI_READ2BYTES( p_indx->i_longsperentry );
596     AVI_READ1BYTE ( p_indx->i_indexsubtype );
597     AVI_READ1BYTE ( p_indx->i_indextype );
598     AVI_READ4BYTES( p_indx->i_entriesinuse );
599
600     AVI_READ4BYTES( p_indx->i_id );
601     p_indx->idx.std     = NULL;
602     p_indx->idx.field   = NULL;
603     p_indx->idx.super   = NULL;
604
605     if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
606     {
607         AVI_READ8BYTES( p_indx->i_baseoffset );
608         AVI_READ4BYTES( i_dummy );
609
610         i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
611         p_indx->i_entriesinuse = i_count;
612         p_indx->idx.std = xcalloc( i_count, sizeof( indx_std_entry_t ) );
613
614         for( i = 0; i < i_count; i++ )
615         {
616             AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
617             AVI_READ4BYTES( p_indx->idx.std[i].i_size );
618         }
619     }
620     else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
621     {
622         AVI_READ8BYTES( p_indx->i_baseoffset );
623         AVI_READ4BYTES( i_dummy );
624
625         i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
626         p_indx->i_entriesinuse = i_count;
627         p_indx->idx.field = xcalloc( i_count, sizeof( indx_field_entry_t ) );
628         for( i = 0; i < i_count; i++ )
629         {
630             AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
631             AVI_READ4BYTES( p_indx->idx.field[i].i_size );
632             AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
633         }
634     }
635     else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
636     {
637         p_indx->i_baseoffset = 0;
638         AVI_READ4BYTES( i_dummy );
639         AVI_READ4BYTES( i_dummy );
640         AVI_READ4BYTES( i_dummy );
641
642         i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
643         p_indx->i_entriesinuse = i_count;
644         p_indx->idx.super = xcalloc( i_count, sizeof( indx_super_entry_t ) );
645
646         for( i = 0; i < i_count; i++ )
647         {
648             AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
649             AVI_READ4BYTES( p_indx->idx.super[i].i_size );
650             AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
651         }
652     }
653     else
654     {
655         msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
656     }
657
658 #ifdef AVI_DEBUG
659     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 );
660 #endif
661     AVI_READCHUNK_EXIT( VLC_SUCCESS );
662 }
663 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
664 {
665     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
666
667     FREENULL( p_indx->idx.std );
668     FREENULL( p_indx->idx.field );
669     FREENULL( p_indx->idx.super );
670 }
671
672 static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
673 {
674     avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;
675
676     AVI_READCHUNK_ENTER;
677
678     AVI_READ4BYTES( p_vprp->i_video_format_token );
679     AVI_READ4BYTES( p_vprp->i_video_standard );
680     AVI_READ4BYTES( p_vprp->i_vertical_refresh );
681     AVI_READ4BYTES( p_vprp->i_h_total_in_t );
682     AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
683     AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
684     AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
685     AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
686     AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
687     for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
688     {
689         AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
690         AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
691         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
692         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
693         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
694         AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
695         AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
696         AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
697     }
698
699 #ifdef AVI_DEBUG
700     msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
701              p_vprp->i_video_format_token, p_vprp->i_video_standard );
702 #endif
703     AVI_READCHUNK_EXIT( VLC_SUCCESS );
704 }
705
706 static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
707 {
708     avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
709
710     AVI_READCHUNK_ENTER;
711
712     AVI_READ4BYTES( p_dmlh->dwTotalFrames );
713
714 #ifdef AVI_DEBUG
715     msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
716              p_dmlh->dwTotalFrames );
717 #endif
718     AVI_READCHUNK_EXIT( VLC_SUCCESS );
719 }
720
721 static const struct
722 {
723     vlc_fourcc_t i_fourcc;
724     const char *psz_type;
725 } AVI_strz_type[] =
726 {
727     { AVIFOURCC_IARL, "Archive location" },
728     { AVIFOURCC_IART, "Artist" },
729     { AVIFOURCC_ICMS, "Commisioned" },
730     { AVIFOURCC_ICMT, "Comments" },
731     { AVIFOURCC_ICOP, "Copyright" },
732     { AVIFOURCC_ICRD, "Creation date" },
733     { AVIFOURCC_ICRP, "Cropped" },
734     { AVIFOURCC_IDIM, "Dimensions" },
735     { AVIFOURCC_IDPI, "Dots per inch" },
736     { AVIFOURCC_IENG, "Engineer" },
737     { AVIFOURCC_IGNR, "Genre" },
738     { AVIFOURCC_ISGN, "Secondary Genre" },
739     { AVIFOURCC_IKEY, "Keywords" },
740     { AVIFOURCC_ILGT, "Lightness" },
741     { AVIFOURCC_IMED, "Medium" },
742     { AVIFOURCC_INAM, "Name" },
743     { AVIFOURCC_IPLT, "Palette setting" },
744     { AVIFOURCC_IPRD, "Product" },
745     { AVIFOURCC_ISBJ, "Subject" },
746     { AVIFOURCC_ISFT, "Software" },
747     { AVIFOURCC_ISHP, "Sharpness" },
748     { AVIFOURCC_ISRC, "Source" },
749     { AVIFOURCC_ISRF, "Source form" },
750     { AVIFOURCC_ITCH, "Technician" },
751     { AVIFOURCC_ISMP, "Time code" },
752     { AVIFOURCC_IDIT, "Digitalization time" },
753     { AVIFOURCC_IWRI, "Writer" },
754     { AVIFOURCC_IPRO, "Producer" },
755     { AVIFOURCC_ICNM, "Cinematographer" },
756     { AVIFOURCC_IPDS, "Production designer" },
757     { AVIFOURCC_IEDT, "Editor" },
758     { AVIFOURCC_ICDS, "Costume designer" },
759     { AVIFOURCC_IMUS, "Music" },
760     { AVIFOURCC_ISTD, "Production studio" },
761     { AVIFOURCC_IDST, "Distributor" },
762     { AVIFOURCC_ICNT, "Country" },
763     { AVIFOURCC_ISTR, "Starring" },
764     { AVIFOURCC_IFRM, "Total number of parts" },
765     { AVIFOURCC_strn, "Stream name" },
766     { AVIFOURCC_IAS1, "First Language" },
767     { AVIFOURCC_IAS2, "Second Language" },
768     { AVIFOURCC_IAS3, "Third Language" },
769     { AVIFOURCC_IAS4, "Fourth Language" },
770     { AVIFOURCC_IAS5, "Fifth Language" },
771     { AVIFOURCC_IAS6, "Sixth Language" },
772     { AVIFOURCC_IAS7, "Seventh Language" },
773     { AVIFOURCC_IAS8, "Eighth Language" },
774     { AVIFOURCC_IAS9, "Ninth Language" },
775
776     { 0,              "???" }
777 };
778
779 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
780 {
781     int i_index;
782     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
783     AVI_READCHUNK_ENTER;
784
785     for( i_index = 0;; i_index++)
786     {
787         if( !AVI_strz_type[i_index].i_fourcc ||
788             AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
789         {
790             break;
791         }
792     }
793     p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
794     p_strz->p_str = xmalloc( p_strz->i_chunk_size + 1);
795
796     if( p_strz->i_chunk_size )
797     {
798         memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
799     }
800     p_strz->p_str[p_strz->i_chunk_size] = 0;
801
802 #ifdef AVI_DEBUG
803     msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
804              (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
805 #endif
806     AVI_READCHUNK_EXIT( VLC_SUCCESS );
807 }
808 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
809 {
810     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
811     FREENULL( p_strz->p_type );
812     FREENULL( p_strz->p_str );
813 }
814
815 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
816 {
817     return AVI_NextChunk( s, p_chk );
818 }
819 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
820 {
821     VLC_UNUSED( p_chk );
822 }
823
824 static const struct
825 {
826     vlc_fourcc_t i_fourcc;
827     int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
828     void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
829 } AVI_Chunk_Function [] =
830 {
831     { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
832     { AVIFOURCC_ON2,  AVI_ChunkRead_list, AVI_ChunkFree_nothing },
833     { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
834     { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
835     { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
836     { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
837     { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
838     { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
839     { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
840     { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
841     { AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
842     { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
843     { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
844
845     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
846     { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
847     { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
848     { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
849     { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
850     { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
851     { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
852     { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
853     { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
854     { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
855     { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
856     { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
857     { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
858     { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
859     { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
860     { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
861     { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
862     { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
863     { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
864     { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
865     { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
866     { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
867     { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
868     { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
869     { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
870     { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
871     { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
872     { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
873     { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
874     { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
875     { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
876     { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
877     { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
878     { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
879     { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
880     { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
881     { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
882     { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
883     { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
884     { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
885     { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
886     { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
887     { AVIFOURCC_IAS1, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
888     { AVIFOURCC_IAS2, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
889     { AVIFOURCC_IAS3, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
890     { AVIFOURCC_IAS4, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
891     { AVIFOURCC_IAS5, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
892     { AVIFOURCC_IAS6, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
893     { AVIFOURCC_IAS7, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
894     { AVIFOURCC_IAS8, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
895     { AVIFOURCC_IAS9, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
896
897
898     { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
899     { 0,           NULL,               NULL }
900 };
901
902 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
903 {
904     unsigned int i_index;
905     for( i_index = 0; ; i_index++ )
906     {
907         if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
908             ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
909         {
910             return i_index;
911         }
912     }
913 }
914
915 int  AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
916 {
917     int i_index;
918
919     if( !p_chk )
920     {
921         msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
922         return VLC_EGENERIC;
923     }
924
925     if( AVI_ChunkReadCommon( s, p_chk ) )
926     {
927         msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
928         return VLC_EGENERIC;
929     }
930
931     if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
932     {
933         msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
934         return AVI_ZERO_FOURCC;
935     }
936     p_chk->common.p_father = p_father;
937
938     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
939     if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
940     {
941         int i_return = AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
942         if ( i_return == AVI_STRD_ZERO_CHUNK || i_return == AVI_ZERO_FOURCC )
943         {
944             if ( !p_father ) return VLC_EGENERIC;
945             return AVI_NextChunk( s, p_father );
946         }
947         return i_return;
948     }
949     else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
950                ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
951              ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
952                ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
953     {
954         p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
955         return AVI_ChunkRead_indx( s, p_chk );
956     }
957
958     msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
959             (char*)&p_chk->common.i_chunk_fourcc );
960     return AVI_NextChunk( s, p_chk );
961 }
962
963 void AVI_ChunkFree( stream_t *s,
964                      avi_chunk_t *p_chk )
965 {
966     int i_index;
967     avi_chunk_t *p_child, *p_next;
968
969     if( !p_chk )
970     {
971         return;
972     }
973
974     /* Free all child chunk */
975     p_child = p_chk->common.p_first;
976     while( p_child )
977     {
978         p_next = p_child->common.p_next;
979         AVI_ChunkFree( s, p_child );
980         free( p_child );
981         p_child = p_next;
982     }
983
984     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
985     if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
986     {
987 #ifdef AVI_DEBUG
988         msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
989                  (char*)&p_chk->common.i_chunk_fourcc );
990 #endif
991         AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
992     }
993     else
994     {
995         msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
996                 (char*)&p_chk->common.i_chunk_fourcc );
997     }
998     p_chk->common.p_first = NULL;
999     p_chk->common.p_last  = NULL;
1000
1001     return;
1002 }
1003
1004 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
1005                                       avi_chunk_t  *p_chk, unsigned i_level )
1006 {
1007     avi_chunk_t *p_child;
1008
1009     char str[512];
1010     if( i_level >= (sizeof(str) - 1)/4 )
1011         return;
1012
1013     memset( str, ' ', sizeof( str ) );
1014     for( unsigned i = 1; i < i_level; i++ )
1015     {
1016         str[i * 4] = '|';
1017     }
1018     if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
1019         p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2  ||
1020         p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1021     {
1022         snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1023                  "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
1024                  i_level ? '+' : '*',
1025                  (char*)&p_chk->common.i_chunk_fourcc,
1026                  (char*)&p_chk->list.i_type,
1027                  p_chk->common.i_chunk_size,
1028                  p_chk->common.i_chunk_pos );
1029     }
1030     else
1031     {
1032         snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1033                  "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
1034                  (char*)&p_chk->common.i_chunk_fourcc,
1035                  p_chk->common.i_chunk_size,
1036                  p_chk->common.i_chunk_pos );
1037     }
1038     msg_Dbg( p_obj, "%s", str );
1039
1040     p_child = p_chk->common.p_first;
1041     while( p_child )
1042     {
1043         AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
1044         p_child = p_child->common.p_next;
1045     }
1046 }
1047
1048 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
1049 {
1050     avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
1051     avi_chunk_t      *p_chk;
1052     bool b_seekable;
1053
1054     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
1055
1056     p_list->i_chunk_pos  = 0;
1057     p_list->i_chunk_size = stream_Size( s );
1058     p_list->i_chunk_fourcc = AVIFOURCC_LIST;
1059     p_list->p_father = NULL;
1060     p_list->p_next  = NULL;
1061     p_list->p_first = NULL;
1062     p_list->p_last  = NULL;
1063
1064     p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
1065
1066     for( ; ; )
1067     {
1068         p_chk = xmalloc( sizeof( avi_chunk_t ) );
1069         memset( p_chk, 0, sizeof( avi_chunk_t ) );
1070         if( !p_root->common.p_first )
1071         {
1072             p_root->common.p_first = p_chk;
1073         }
1074         else
1075         {
1076             p_root->common.p_last->common.p_next = p_chk;
1077         }
1078         p_root->common.p_last = p_chk;
1079
1080         if( AVI_ChunkRead( s, p_chk, p_root ) ||
1081            ( stream_Tell( s ) >=
1082               (off_t)p_chk->common.p_father->common.i_chunk_pos +
1083                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
1084         {
1085             break;
1086         }
1087         /* If we can't seek then stop when we 've found first RIFF-AVI */
1088         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
1089             p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
1090         {
1091             break;
1092         }
1093     }
1094
1095     AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
1096     return VLC_SUCCESS;
1097 }
1098
1099 void AVI_ChunkFreeRoot( stream_t *s,
1100                         avi_chunk_t  *p_chk )
1101 {
1102     AVI_ChunkFree( s, p_chk );
1103 }
1104
1105
1106 int  _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
1107 {
1108     int i_count;
1109     avi_chunk_t *p_child;
1110
1111     if( !p_chk )
1112     {
1113         return 0;
1114     }
1115
1116     i_count = 0;
1117     p_child = p_chk->common.p_first;
1118     while( p_child )
1119     {
1120         if( p_child->common.i_chunk_fourcc == i_fourcc ||
1121             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1122               p_child->list.i_type == i_fourcc ) )
1123         {
1124             i_count++;
1125         }
1126         p_child = p_child->common.p_next;
1127     }
1128     return i_count;
1129 }
1130
1131 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
1132                       vlc_fourcc_t i_fourcc, int i_number )
1133 {
1134     avi_chunk_t *p_child;
1135     if( !p_chk )
1136     {
1137         return NULL;
1138     }
1139     p_child = p_chk->common.p_first;
1140
1141     while( p_child )
1142     {
1143         if( p_child->common.i_chunk_fourcc == i_fourcc ||
1144             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1145               p_child->list.i_type == i_fourcc ) )
1146         {
1147             if( i_number == 0 )
1148             {
1149                 /* We found it */
1150                 return p_child;
1151             }
1152
1153             i_number--;
1154         }
1155         p_child = p_child->common.p_next;
1156     }
1157     return NULL;
1158 }
1159