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