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