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