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