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