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