]> git.sesse.net Git - vlc/blob - modules/demux/avi/libavi.c
e2892128231cfe69ccfd198bb0cac7ad4228cbab
[vlc] / modules / demux / avi / libavi.c
1 /*****************************************************************************
2  * libavi.c : LibAVI
3  *****************************************************************************
4  * Copyright (C) 2001 the VideoLAN team
5  * $Id$
6  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 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 General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, 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>
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:%8.8x (%4.4s) size:%"PRId64" pos:%"PRId64,
71              p_chk->common.i_chunk_fourcc,
72              (char*)&p_chk->common.i_chunk_fourcc,
73              p_chk->common.i_chunk_size,
74              p_chk->common.i_chunk_pos );
75 #endif
76     return VLC_SUCCESS;
77 }
78
79 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
80 {
81     avi_chunk_t chk;
82
83     if( !p_chk )
84     {
85         if( AVI_ChunkReadCommon( s, &chk ) )
86         {
87             return VLC_EGENERIC;
88         }
89         p_chk = &chk;
90     }
91
92     if( p_chk->common.p_father )
93     {
94         if( p_chk->common.p_father->common.i_chunk_pos +
95                 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
96             p_chk->common.i_chunk_pos +
97                 __EVEN( p_chk->common.i_chunk_size ) + 8 )
98         {
99             return VLC_EGENERIC;
100         }
101     }
102     return stream_Seek( s, p_chk->common.i_chunk_pos +
103                                  __EVEN( p_chk->common.i_chunk_size ) + 8 );
104 }
105
106 /****************************************************************************
107  *
108  * Functions to read chunks
109  *
110  ****************************************************************************/
111 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
112 {
113     avi_chunk_t *p_chk;
114     const uint8_t *p_peek;
115     bool b_seekable;
116
117     if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
118     {
119         /* empty box */
120         msg_Warn( (vlc_object_t*)s, "empty list chunk" );
121         return VLC_EGENERIC;
122     }
123     if( stream_Peek( s, &p_peek, 12 ) < 12 )
124     {
125         msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
126         return VLC_EGENERIC;
127     }
128
129     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
130
131     p_container->list.i_type = GetFOURCC( p_peek + 8 );
132
133     /* XXX fixed for on2 hack */
134     if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
135     {
136         p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
137         p_container->list.i_type = AVIFOURCC_AVI;
138     }
139
140     if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
141         p_container->list.i_type == AVIFOURCC_movi )
142     {
143         msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
144         if( b_seekable )
145         {
146             return AVI_NextChunk( s, p_container );
147         }
148         return VLC_SUCCESS; /* point at begining of LIST-movi */
149     }
150
151     if( stream_Read( s, NULL, 12 ) != 12 )
152     {
153         msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
154         return VLC_EGENERIC;
155     }
156
157 #ifdef AVI_DEBUG
158     msg_Dbg( (vlc_object_t*)s,
159              "found LIST chunk: \'%4.4s\'",
160              (char*)&p_container->list.i_type );
161 #endif
162     msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
163     for( ; ; )
164     {
165         p_chk = xmalloc( sizeof( avi_chunk_t ) );
166         memset( p_chk, 0, sizeof( avi_chunk_t ) );
167         if( !p_container->common.p_first )
168         {
169             p_container->common.p_first = p_chk;
170         }
171         else
172         {
173             p_container->common.p_last->common.p_next = p_chk;
174         }
175         p_container->common.p_last = p_chk;
176
177         if( AVI_ChunkRead( s, p_chk, p_container ) )
178         {
179             break;
180         }
181         if( p_chk->common.p_father->common.i_chunk_size > 0 &&
182            ( stream_Tell( s ) >
183               (off_t)p_chk->common.p_father->common.i_chunk_pos +
184                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
185         {
186             break;
187         }
188
189         /* If we can't seek then stop when we 've found LIST-movi */
190         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
191             p_chk->list.i_type == AVIFOURCC_movi &&
192             ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
193         {
194             break;
195         }
196
197     }
198     msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
199
200     return VLC_SUCCESS;
201 }
202
203 #define AVI_READCHUNK_ENTER \
204     int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
205     if( i_read > 100000000 ) \
206     { \
207         msg_Err( s, "Big chunk ignored" ); \
208         return VLC_EGENERIC; \
209     } \
210     uint8_t  *p_read, *p_buff;    \
211     if( !( p_read = p_buff = malloc(i_read ) ) ) \
212     { \
213         return VLC_EGENERIC; \
214     } \
215     i_read = stream_Read( s, p_read, i_read ); \
216     if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
217     { \
218         free( p_buff ); \
219         return VLC_EGENERIC; \
220     }\
221     p_read += 8; \
222     i_read -= 8
223
224 #define AVI_READ( res, func, size ) \
225     if( i_read < size ) { \
226         free( p_buff); \
227         return VLC_EGENERIC; \
228     } \
229     i_read -= size; \
230     res = func( p_read ); \
231     p_read += size \
232
233 #define AVI_READCHUNK_EXIT( code ) \
234     free( p_buff ); \
235     return code
236
237 static inline uint8_t GetB( uint8_t *ptr )
238 {
239     return *ptr;
240 }
241
242 #define AVI_READ1BYTE( i_byte ) \
243     AVI_READ( i_byte, GetB, 1 )
244
245 #define AVI_READ2BYTES( i_word ) \
246     AVI_READ( i_word, GetWLE, 2 )
247
248 #define AVI_READ4BYTES( i_dword ) \
249     AVI_READ( i_dword, GetDWLE, 4 )
250
251 #define AVI_READ8BYTES( i_qword ) \
252     AVI_READ( i_qword, GetQWLE, 8 )
253
254 #define AVI_READFOURCC( i_dword ) \
255     AVI_READ( i_dword, GetFOURCC, 4 )
256
257 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
258 {
259     AVI_READCHUNK_ENTER;
260
261     p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
262     AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
263     AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
264     AVI_READ4BYTES( p_chk->avih.i_reserved1 );
265     AVI_READ4BYTES( p_chk->avih.i_flags );
266     AVI_READ4BYTES( p_chk->avih.i_totalframes );
267     AVI_READ4BYTES( p_chk->avih.i_initialframes );
268     AVI_READ4BYTES( p_chk->avih.i_streams );
269     AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
270     AVI_READ4BYTES( p_chk->avih.i_width );
271     AVI_READ4BYTES( p_chk->avih.i_height );
272     AVI_READ4BYTES( p_chk->avih.i_scale );
273     AVI_READ4BYTES( p_chk->avih.i_rate );
274     AVI_READ4BYTES( p_chk->avih.i_start );
275     AVI_READ4BYTES( p_chk->avih.i_length );
276 #ifdef AVI_DEBUG
277     msg_Dbg( (vlc_object_t*)s,
278              "avih: streams:%d flags:%s%s%s%s %dx%d",
279              p_chk->avih.i_streams,
280              p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
281              p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
282              p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
283              p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
284              p_chk->avih.i_width, p_chk->avih.i_height );
285 #endif
286     AVI_READCHUNK_EXIT( VLC_SUCCESS );
287 }
288
289 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
290 {
291     AVI_READCHUNK_ENTER;
292
293     AVI_READFOURCC( p_chk->strh.i_type );
294     AVI_READFOURCC( p_chk->strh.i_handler );
295     AVI_READ4BYTES( p_chk->strh.i_flags );
296     AVI_READ4BYTES( p_chk->strh.i_reserved1 );
297     AVI_READ4BYTES( p_chk->strh.i_initialframes );
298     AVI_READ4BYTES( p_chk->strh.i_scale );
299     AVI_READ4BYTES( p_chk->strh.i_rate );
300     AVI_READ4BYTES( p_chk->strh.i_start );
301     AVI_READ4BYTES( p_chk->strh.i_length );
302     AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
303     AVI_READ4BYTES( p_chk->strh.i_quality );
304     AVI_READ4BYTES( p_chk->strh.i_samplesize );
305 #ifdef AVI_DEBUG
306     msg_Dbg( (vlc_object_t*)s,
307              "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
308              (char*)&p_chk->strh.i_type,
309              p_chk->strh.i_handler,
310              p_chk->strh.i_samplesize,
311              ( p_chk->strh.i_scale ?
312                 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
313 #endif
314
315     AVI_READCHUNK_EXIT( VLC_SUCCESS );
316 }
317
318 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
319 {
320     avi_chunk_t *p_strh;
321
322     AVI_READCHUNK_ENTER;
323     if( p_chk->common.p_father == NULL )
324     {
325         msg_Err( (vlc_object_t*)s, "malformed avi file" );
326         AVI_READCHUNK_EXIT( VLC_EGENERIC );
327     }
328     if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
329     {
330         msg_Err( (vlc_object_t*)s, "malformed avi file" );
331         AVI_READCHUNK_EXIT( VLC_EGENERIC );
332     }
333
334     switch( p_strh->strh.i_type )
335     {
336         case( AVIFOURCC_auds ):
337             p_chk->strf.auds.i_cat = AUDIO_ES;
338             p_chk->strf.auds.p_wf = xmalloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
339             AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
340             AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
341             AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
342             AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
343             AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
344             AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
345
346             if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
347                  && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
348             {
349                 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
350
351                 /* prevent segfault */
352                 if( p_chk->strf.auds.p_wf->cbSize >
353                         p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
354                 {
355                     p_chk->strf.auds.p_wf->cbSize =
356                         p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
357                 }
358
359                 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
360                 {
361                     msg_Dbg( s, "Extended header found" );
362                 }
363             }
364             else
365             {
366                 p_chk->strf.auds.p_wf->cbSize = 0;
367             }
368             if( p_chk->strf.auds.p_wf->cbSize > 0 )
369             {
370                 memcpy( &p_chk->strf.auds.p_wf[1] ,
371                         p_buff + 8 + sizeof( WAVEFORMATEX ),    /*  8=fourcc+size */
372                         p_chk->strf.auds.p_wf->cbSize );
373             }
374 #ifdef AVI_DEBUG
375             msg_Dbg( (vlc_object_t*)s,
376                      "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
377                      p_chk->strf.auds.p_wf->wFormatTag,
378                      p_chk->strf.auds.p_wf->nChannels,
379                      p_chk->strf.auds.p_wf->nSamplesPerSec,
380                      p_chk->strf.auds.p_wf->wBitsPerSample,
381                      p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
382 #endif
383             break;
384         case( AVIFOURCC_vids ):
385             p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
386             p_chk->strf.vids.i_cat = VIDEO_ES;
387             p_chk->strf.vids.p_bih = xmalloc( __MAX( p_chk->common.i_chunk_size,
388                                          sizeof( *p_chk->strf.vids.p_bih ) ) );
389             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
390             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
391             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
392             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
393             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
394             AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
395             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
396             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
397             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
398             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
399             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
400             if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
401             {
402                 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
403             }
404             if( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
405             {
406                 memcpy( &p_chk->strf.vids.p_bih[1],
407                         p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
408                         p_chk->common.i_chunk_size -sizeof(VLC_BITMAPINFOHEADER) );
409             }
410 #ifdef AVI_DEBUG
411             msg_Dbg( (vlc_object_t*)s,
412                      "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
413                      (char*)&p_chk->strf.vids.p_bih->biCompression,
414                      (uint32_t)p_chk->strf.vids.p_bih->biWidth,
415                      (uint32_t)p_chk->strf.vids.p_bih->biHeight,
416                      p_chk->strf.vids.p_bih->biPlanes,
417                      p_chk->strf.vids.p_bih->biBitCount );
418 #endif
419             break;
420         default:
421             msg_Warn( (vlc_object_t*)s, "unknown stream type" );
422             p_chk->strf.common.i_cat = UNKNOWN_ES;
423             break;
424     }
425     AVI_READCHUNK_EXIT( VLC_SUCCESS );
426 }
427 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
428 {
429     avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
430     if( p_strf->common.i_cat == AUDIO_ES )
431     {
432         FREENULL( p_strf->auds.p_wf );
433     }
434     else if( p_strf->common.i_cat == VIDEO_ES )
435     {
436         FREENULL( p_strf->vids.p_bih );
437     }
438 }
439
440 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
441 {
442     AVI_READCHUNK_ENTER;
443     p_chk->strd.p_data = xmalloc( p_chk->common.i_chunk_size );
444     memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
445     AVI_READCHUNK_EXIT( VLC_SUCCESS );
446 }
447
448 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
449 {
450     free( p_chk->strd.p_data );
451 }
452
453 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
454 {
455     unsigned int i_count, i_index;
456
457     AVI_READCHUNK_ENTER;
458
459     i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
460
461     p_chk->idx1.i_entry_count = i_count;
462     p_chk->idx1.i_entry_max   = i_count;
463     if( i_count > 0 )
464     {
465         p_chk->idx1.entry = xcalloc( i_count, sizeof( idx1_entry_t ) );
466
467         for( i_index = 0; i_index < i_count ; i_index++ )
468         {
469             AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
470             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
471             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
472             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
473         }
474     }
475     else
476     {
477         p_chk->idx1.entry = NULL;
478     }
479 #ifdef AVI_DEBUG
480     msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
481 #endif
482     AVI_READCHUNK_EXIT( VLC_SUCCESS );
483 }
484
485 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
486 {
487     p_chk->idx1.i_entry_count = 0;
488     p_chk->idx1.i_entry_max   = 0;
489     FREENULL( p_chk->idx1.entry );
490 }
491
492
493
494 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
495 {
496     unsigned int i_count, i;
497     int32_t      i_dummy;
498     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
499
500     AVI_READCHUNK_ENTER;
501
502     AVI_READ2BYTES( p_indx->i_longsperentry );
503     AVI_READ1BYTE ( p_indx->i_indexsubtype );
504     AVI_READ1BYTE ( p_indx->i_indextype );
505     AVI_READ4BYTES( p_indx->i_entriesinuse );
506
507     AVI_READ4BYTES( p_indx->i_id );
508     p_indx->idx.std     = NULL;
509     p_indx->idx.field   = NULL;
510     p_indx->idx.super   = NULL;
511
512     if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
513     {
514         AVI_READ8BYTES( p_indx->i_baseoffset );
515         AVI_READ4BYTES( i_dummy );
516
517         i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
518         p_indx->i_entriesinuse = i_count;
519         p_indx->idx.std = xcalloc( i_count, sizeof( indx_std_entry_t ) );
520
521         for( i = 0; i < i_count; i++ )
522         {
523             AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
524             AVI_READ4BYTES( p_indx->idx.std[i].i_size );
525         }
526     }
527     else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
528     {
529         AVI_READ8BYTES( p_indx->i_baseoffset );
530         AVI_READ4BYTES( i_dummy );
531
532         i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
533         p_indx->i_entriesinuse = i_count;
534         p_indx->idx.field = xcalloc( i_count, sizeof( indx_field_entry_t ) );
535         for( i = 0; i < i_count; i++ )
536         {
537             AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
538             AVI_READ4BYTES( p_indx->idx.field[i].i_size );
539             AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
540         }
541     }
542     else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
543     {
544         p_indx->i_baseoffset = 0;
545         AVI_READ4BYTES( i_dummy );
546         AVI_READ4BYTES( i_dummy );
547         AVI_READ4BYTES( i_dummy );
548
549         i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
550         p_indx->i_entriesinuse = i_count;
551         p_indx->idx.super = xcalloc( i_count, sizeof( indx_super_entry_t ) );
552
553         for( i = 0; i < i_count; i++ )
554         {
555             AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
556             AVI_READ4BYTES( p_indx->idx.super[i].i_size );
557             AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
558         }
559     }
560     else
561     {
562         msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
563     }
564
565 #ifdef AVI_DEBUG
566     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 );
567 #endif
568     AVI_READCHUNK_EXIT( VLC_SUCCESS );
569 }
570 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
571 {
572     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
573
574     FREENULL( p_indx->idx.std );
575     FREENULL( p_indx->idx.field );
576     FREENULL( p_indx->idx.super );
577 }
578
579
580
581 static const struct
582 {
583     vlc_fourcc_t i_fourcc;
584     const char *psz_type;
585 } AVI_strz_type[] =
586 {
587     { AVIFOURCC_IARL, "archive location" },
588     { AVIFOURCC_IART, "artist" },
589     { AVIFOURCC_ICMS, "commisioned" },
590     { AVIFOURCC_ICMT, "comments" },
591     { AVIFOURCC_ICOP, "copyright" },
592     { AVIFOURCC_ICRD, "creation date" },
593     { AVIFOURCC_ICRP, "cropped" },
594     { AVIFOURCC_IDIM, "dimensions" },
595     { AVIFOURCC_IDPI, "dots per inch" },
596     { AVIFOURCC_IENG, "engineer" },
597     { AVIFOURCC_IGNR, "genre" },
598     { AVIFOURCC_IKEY, "keywords" },
599     { AVIFOURCC_ILGT, "lightness" },
600     { AVIFOURCC_IMED, "medium" },
601     { AVIFOURCC_INAM, "name" },
602     { AVIFOURCC_IPLT, "palette setting" },
603     { AVIFOURCC_IPRD, "product" },
604     { AVIFOURCC_ISBJ, "subject" },
605     { AVIFOURCC_ISFT, "software" },
606     { AVIFOURCC_ISHP, "sharpness" },
607     { AVIFOURCC_ISRC, "source" },
608     { AVIFOURCC_ISRF, "source form" },
609     { AVIFOURCC_ITCH, "technician" },
610     { AVIFOURCC_ISMP, "time code" },
611     { AVIFOURCC_IDIT, "digitalization time" },
612     { AVIFOURCC_strn, "stream name" },
613     { 0,              "???" }
614 };
615 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
616 {
617     int i_index;
618     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
619     AVI_READCHUNK_ENTER;
620
621     for( i_index = 0;; i_index++)
622     {
623         if( !AVI_strz_type[i_index].i_fourcc ||
624             AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
625         {
626             break;
627         }
628     }
629     p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
630     p_strz->p_str = xmalloc( p_strz->i_chunk_size + 1);
631
632     if( p_strz->i_chunk_size )
633     {
634         memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
635     }
636     p_strz->p_str[p_strz->i_chunk_size] = 0;
637
638 #ifdef AVI_DEBUG
639     msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
640              (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
641 #endif
642     AVI_READCHUNK_EXIT( VLC_SUCCESS );
643 }
644 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
645 {
646     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
647     FREENULL( p_strz->p_type );
648     FREENULL( p_strz->p_str );
649 }
650
651 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
652 {
653     return AVI_NextChunk( s, p_chk );
654 }
655 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
656 {
657     VLC_UNUSED( p_chk );
658 }
659
660 static const struct
661 {
662     vlc_fourcc_t i_fourcc;
663     int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
664     void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
665 } AVI_Chunk_Function [] =
666 {
667     { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
668     { AVIFOURCC_ON2,  AVI_ChunkRead_list, AVI_ChunkFree_nothing },
669     { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
670     { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
671     { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
672     { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
673     { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
674     { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
675     { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
676     { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
677     { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
678
679     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
680     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
681     { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
682     { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
683     { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
684     { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
685     { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
686     { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
687     { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
688     { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
689     { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
690     { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
691     { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
692     { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
693     { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
694     { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
695     { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
696     { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
697     { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
698     { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
699     { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
700     { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
701     { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
702     { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
703     { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
704     { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
705     { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
706     { 0,           NULL,               NULL }
707 };
708
709 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
710 {
711     unsigned int i_index;
712     for( i_index = 0; ; i_index++ )
713     {
714         if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
715             ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
716         {
717             return i_index;
718         }
719     }
720 }
721
722 int  _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
723 {
724     int i_index;
725
726     if( !p_chk )
727     {
728         return VLC_EGENERIC;
729     }
730
731     if( AVI_ChunkReadCommon( s, p_chk ) )
732     {
733         msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
734         return VLC_EGENERIC;
735     }
736     if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
737     {
738         msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
739         return VLC_EGENERIC;
740     }
741     p_chk->common.p_father = p_father;
742
743     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
744     if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
745     {
746         return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
747     }
748     else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
749                ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
750              ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
751                ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
752     {
753         p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
754         return AVI_ChunkRead_indx( s, p_chk );
755     }
756     msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
757     return AVI_NextChunk( s, p_chk );
758 }
759
760 void _AVI_ChunkFree( stream_t *s,
761                      avi_chunk_t *p_chk )
762 {
763     int i_index;
764     avi_chunk_t *p_child, *p_next;
765
766     if( !p_chk )
767     {
768         return;
769     }
770
771     /* Free all child chunk */
772     p_child = p_chk->common.p_first;
773     while( p_child )
774     {
775         p_next = p_child->common.p_next;
776         AVI_ChunkFree( s, p_child );
777         free( p_child );
778         p_child = p_next;
779     }
780
781     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
782     if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
783     {
784 #ifdef AVI_DEBUG
785         msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
786                  (char*)&p_chk->common.i_chunk_fourcc );
787 #endif
788         AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
789     }
790     else
791     {
792         msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
793     }
794     p_chk->common.p_first = NULL;
795     p_chk->common.p_last  = NULL;
796
797     return;
798 }
799
800 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
801                                       avi_chunk_t  *p_chk, unsigned i_level )
802 {
803     unsigned i;
804     avi_chunk_t *p_child;
805
806     char str[512];
807     if( i_level >= (sizeof(str) - 1)/5 )
808         return;
809
810     memset( str, ' ', sizeof( str ) );
811     for( i = 1; i < i_level; i++ )
812     {
813         str[i * 5] = '|';
814     }
815     if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
816         p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2  ||
817         p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
818     {
819         snprintf( &str[i_level * 5], sizeof(str) - 5*i_level,
820                  "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
821                  i_level ? '+' : '*',
822                  (char*)&p_chk->common.i_chunk_fourcc,
823                  (char*)&p_chk->list.i_type,
824                  p_chk->common.i_chunk_size,
825                  p_chk->common.i_chunk_pos );
826     }
827     else
828     {
829         snprintf( &str[i_level * 5], sizeof(str) - 5*i_level,
830                  "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
831                  (char*)&p_chk->common.i_chunk_fourcc,
832                  p_chk->common.i_chunk_size,
833                  p_chk->common.i_chunk_pos );
834     }
835     msg_Dbg( p_obj, "%s", str );
836
837     p_child = p_chk->common.p_first;
838     while( p_child )
839     {
840         AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
841         p_child = p_child->common.p_next;
842     }
843 }
844
845 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
846 {
847     avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
848     avi_chunk_t      *p_chk;
849     bool b_seekable;
850
851     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
852
853     p_list->i_chunk_pos  = 0;
854     p_list->i_chunk_size = stream_Size( s );
855     p_list->i_chunk_fourcc = AVIFOURCC_LIST;
856     p_list->p_father = NULL;
857     p_list->p_next  = NULL;
858     p_list->p_first = NULL;
859     p_list->p_last  = NULL;
860
861     p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
862
863     for( ; ; )
864     {
865         p_chk = xmalloc( sizeof( avi_chunk_t ) );
866         memset( p_chk, 0, sizeof( avi_chunk_t ) );
867         if( !p_root->common.p_first )
868         {
869             p_root->common.p_first = p_chk;
870         }
871         else
872         {
873             p_root->common.p_last->common.p_next = p_chk;
874         }
875         p_root->common.p_last = p_chk;
876
877         if( AVI_ChunkRead( s, p_chk, p_root ) ||
878            ( stream_Tell( s ) >=
879               (off_t)p_chk->common.p_father->common.i_chunk_pos +
880                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
881         {
882             break;
883         }
884         /* If we can't seek then stop when we 've found first RIFF-AVI */
885         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
886             p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
887         {
888             break;
889         }
890     }
891
892     AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
893     return VLC_SUCCESS;
894 }
895
896 void AVI_ChunkFreeRoot( stream_t *s,
897                         avi_chunk_t  *p_chk )
898 {
899     AVI_ChunkFree( s, p_chk );
900 }
901
902
903 int  _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
904 {
905     int i_count;
906     avi_chunk_t *p_child;
907
908     if( !p_chk )
909     {
910         return 0;
911     }
912
913     i_count = 0;
914     p_child = p_chk->common.p_first;
915     while( p_child )
916     {
917         if( p_child->common.i_chunk_fourcc == i_fourcc ||
918             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
919               p_child->list.i_type == i_fourcc ) )
920         {
921             i_count++;
922         }
923         p_child = p_child->common.p_next;
924     }
925     return i_count;
926 }
927
928 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
929                       vlc_fourcc_t i_fourcc, int i_number )
930 {
931     avi_chunk_t *p_child;
932     if( !p_chk )
933     {
934         return NULL;
935     }
936     p_child = p_chk->common.p_first;
937
938     while( p_child )
939     {
940         if( p_child->common.i_chunk_fourcc == i_fourcc ||
941             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
942               p_child->list.i_type == i_fourcc ) )
943         {
944             if( i_number == 0 )
945             {
946                 /* We found it */
947                 return p_child;
948             }
949
950             i_number--;
951         }
952         p_child = p_child->common.p_next;
953     }
954     return NULL;
955 }
956
957