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