]> git.sesse.net Git - vlc/blob - modules/demux/avi/libavi.c
* all : demuxers *have to* set pf_demux_control. (demux_vaControlDefault
[vlc] / modules / demux / avi / libavi.c
1 /*****************************************************************************
2  * libavi.c :
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: libavi.c,v 1.26 2003/09/07 22:48:29 fenrir 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             }
341             else
342             {
343                 p_chk->strf.auds.p_wf->cbSize = 0;
344             }
345             if( p_chk->strf.auds.p_wf->cbSize > 0 )
346             {
347                 memcpy( &p_chk->strf.auds.p_wf[1] ,
348                         p_buff + 8 + sizeof( WAVEFORMATEX ),    // 8=fourrc+size
349                         p_chk->strf.auds.p_wf->cbSize );
350             }
351 #ifdef AVI_DEBUG
352             msg_Dbg( (vlc_object_t*)s,
353                      "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
354                      p_chk->strf.auds.p_wf->wFormatTag,
355                      p_chk->strf.auds.p_wf->nChannels,
356                      p_chk->strf.auds.p_wf->nSamplesPerSec,
357                      p_chk->strf.auds.p_wf->wBitsPerSample,
358                      p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
359 #endif
360             break;
361         case( AVIFOURCC_vids ):
362             p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
363             p_chk->strf.vids.i_cat = VIDEO_ES;
364             p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
365             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
366             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
367             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
368             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
369             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
370             AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
371             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
372             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
373             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
374             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
375             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
376             if( p_chk->strf.vids.p_bih->biSize >
377                         p_chk->common.i_chunk_size )
378             {
379                 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
380             }
381             if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
382             {
383                 memcpy( &p_chk->strf.vids.p_bih[1],
384                         p_buff + 8 + sizeof(BITMAPINFOHEADER), // 8=fourrc+size
385                         p_chk->strf.vids.p_bih->biSize -
386                                                     sizeof(BITMAPINFOHEADER) );
387             }
388 #ifdef AVI_DEBUG
389             msg_Dbg( (vlc_object_t*)s,
390                      "strf: video:%4.4s %dx%d planes:%d %dbpp",
391                      (char*)&p_chk->strf.vids.p_bih->biCompression,
392                      p_chk->strf.vids.p_bih->biWidth,
393                      p_chk->strf.vids.p_bih->biHeight,
394                      p_chk->strf.vids.p_bih->biPlanes,
395                      p_chk->strf.vids.p_bih->biBitCount );
396 #endif
397             break;
398         default:
399             msg_Warn( (vlc_object_t*)s, "unknown stream type" );
400             p_chk->strf.common.i_cat = UNKNOWN_ES;
401             break;
402     }
403     AVI_READCHUNK_EXIT( VLC_SUCCESS );
404 }
405 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
406 {
407     avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
408     if( p_strf->common.i_cat == AUDIO_ES )
409     {
410         FREE( p_strf->auds.p_wf );
411     }
412     else if( p_strf->common.i_cat == VIDEO_ES )
413     {
414         FREE( p_strf->vids.p_bih );
415     }
416 }
417
418 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
419 {
420     AVI_READCHUNK_ENTER;
421     p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
422     memcpy( p_chk->strd.p_data,
423             p_buff + 8,
424             p_chk->common.i_chunk_size );
425     AVI_READCHUNK_EXIT( VLC_SUCCESS );
426 }
427
428 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
429 {
430     unsigned int i_count, i_index;
431
432     AVI_READCHUNK_ENTER;
433
434     i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
435
436     p_chk->idx1.i_entry_count = i_count;
437     p_chk->idx1.i_entry_max   = i_count;
438     if( i_count > 0 )
439     {
440         p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
441
442         for( i_index = 0; i_index < i_count ; i_index++ )
443         {
444             AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
445             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
446             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
447             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
448         }
449     }
450     else
451     {
452         p_chk->idx1.entry = NULL;
453     }
454 #ifdef AVI_DEBUG
455     msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
456 #endif
457     AVI_READCHUNK_EXIT( VLC_SUCCESS );
458 }
459
460 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
461 {
462     p_chk->idx1.i_entry_count = 0;
463     p_chk->idx1.i_entry_max   = 0;
464     FREE( p_chk->idx1.entry )
465 }
466
467
468
469 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
470 {
471     unsigned int i_count, i;
472     int32_t      i_dummy;
473     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
474
475     AVI_READCHUNK_ENTER;
476
477     AVI_READ2BYTES( p_indx->i_longsperentry );
478     AVI_READ1BYTE ( p_indx->i_indexsubtype );
479     AVI_READ1BYTE ( p_indx->i_indextype );
480     AVI_READ4BYTES( p_indx->i_entriesinuse );
481
482     AVI_READ4BYTES( p_indx->i_id );
483     p_indx->idx.std     = NULL;
484     p_indx->idx.field   = NULL;
485     p_indx->idx.super   = NULL;
486
487     if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
488     {
489         AVI_READ8BYTES( p_indx->i_baseoffset );
490         AVI_READ4BYTES( i_dummy );
491
492         i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
493         p_indx->i_entriesinuse = i_count;
494         p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
495
496         for( i = 0; i < i_count; i++ )
497         {
498             AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
499             AVI_READ4BYTES( p_indx->idx.std[i].i_size );
500         }
501     }
502     else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
503     {
504         AVI_READ8BYTES( p_indx->i_baseoffset );
505         AVI_READ4BYTES( i_dummy );
506
507         i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
508         p_indx->i_entriesinuse = i_count;
509         p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
510         for( i = 0; i < i_count; i++ )
511         {
512             AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
513             AVI_READ4BYTES( p_indx->idx.field[i].i_size );
514             AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
515         }
516     }
517     else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
518     {
519         p_indx->i_baseoffset = 0;
520         AVI_READ4BYTES( i_dummy );
521         AVI_READ4BYTES( i_dummy );
522         AVI_READ4BYTES( i_dummy );
523
524         i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
525         p_indx->i_entriesinuse = i_count;
526         p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
527
528         for( i = 0; i < i_count; i++ )
529         {
530             AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
531             AVI_READ4BYTES( p_indx->idx.super[i].i_size );
532             AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
533         }
534     }
535     else
536     {
537         msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
538     }
539
540 #ifdef AVI_DEBUG
541     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 );
542 #endif
543     AVI_READCHUNK_EXIT( VLC_SUCCESS );
544 }
545 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
546 {
547     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
548
549     FREE( p_indx->idx.std );
550     FREE( p_indx->idx.field );
551     FREE( p_indx->idx.super );
552 }
553
554
555
556 static struct
557 {
558     vlc_fourcc_t i_fourcc;
559     char *psz_type;
560 } AVI_strz_type[] =
561 {
562     { AVIFOURCC_IARL, "archive location" },
563     { AVIFOURCC_IART, "artist" },
564     { AVIFOURCC_ICMS, "commisioned" },
565     { AVIFOURCC_ICMT, "comments" },
566     { AVIFOURCC_ICOP, "copyright" },
567     { AVIFOURCC_ICRD, "creation date" },
568     { AVIFOURCC_ICRP, "cropped" },
569     { AVIFOURCC_IDIM, "dimensions" },
570     { AVIFOURCC_IDPI, "dots per inch" },
571     { AVIFOURCC_IENG, "enginner" },
572     { AVIFOURCC_IGNR, "genre" },
573     { AVIFOURCC_IKEY, "keywords" },
574     { AVIFOURCC_ILGT, "lightness" },
575     { AVIFOURCC_IMED, "medium" },
576     { AVIFOURCC_INAM, "name" },
577     { AVIFOURCC_IPLT, "palette setting" },
578     { AVIFOURCC_IPRD, "product" },
579     { AVIFOURCC_ISBJ, "subject" },
580     { AVIFOURCC_ISFT, "software" },
581     { AVIFOURCC_ISHP, "sharpness" },
582     { AVIFOURCC_ISRC, "source" },
583     { AVIFOURCC_ISRF, "source form" },
584     { AVIFOURCC_ITCH, "technician" },
585     { AVIFOURCC_ISMP, "time code" },
586     { AVIFOURCC_IDIT, "digitalization time" },
587     { AVIFOURCC_strn, "stream name" },
588     { 0,              "???" }
589 };
590 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
591 {
592     int i_index;
593     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
594     AVI_READCHUNK_ENTER;
595
596     for( i_index = 0;; i_index++)
597     {
598         if( !AVI_strz_type[i_index].i_fourcc ||
599             AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
600         {
601             break;
602         }
603     }
604     p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
605     p_strz->p_str = malloc( i_read + 1);
606
607     if( p_strz->i_chunk_size )
608     {
609         memcpy( p_strz->p_str, p_read, i_read );
610     }
611     p_strz->p_str[i_read] = 0;
612
613 #ifdef AVI_DEBUG
614     msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
615              (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
616 #endif
617     AVI_READCHUNK_EXIT( VLC_SUCCESS );
618 }
619 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
620 {
621     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
622     FREE( p_strz->p_type );
623     FREE( p_strz->p_str );
624 }
625
626 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
627 {
628     return AVI_NextChunk( s, p_chk );
629 }
630 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
631 {
632
633 }
634
635 static struct
636 {
637     vlc_fourcc_t i_fourcc;
638     int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
639     void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
640 } AVI_Chunk_Function [] =
641 {
642     { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
643     { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
644     { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
645     { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
646     { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
647     { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
648     { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
649     { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
650     { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
651
652     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
653     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
654     { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
655     { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
656     { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
657     { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
658     { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
659     { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
660     { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
661     { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
662     { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
663     { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
664     { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
665     { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
666     { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
667     { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
668     { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
669     { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
670     { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
671     { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
672     { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
673     { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
674     { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
675     { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
676     { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
677     { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
678     { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
679     { 0,           NULL,               NULL }
680 };
681
682 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
683 {
684     unsigned int i_index;
685     for( i_index = 0; ; i_index++ )
686     {
687         if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
688             ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
689         {
690             return i_index;
691         }
692     }
693 }
694
695 int  _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
696 {
697     int i_index;
698
699     if( !p_chk )
700     {
701         return VLC_EGENERIC;
702     }
703
704     if( AVI_ChunkReadCommon( s, p_chk ) )
705     {
706         msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
707         return VLC_EGENERIC;
708     }
709     if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
710     {
711         msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
712         return VLC_EGENERIC;
713     }
714     p_chk->common.p_father = p_father;
715
716     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
717     if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
718     {
719         return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
720     }
721     else if( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
722              ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' )
723     {
724         p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
725         return AVI_ChunkRead_indx( s, p_chk );
726     }
727     msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
728     return AVI_NextChunk( s, p_chk );
729 }
730
731 void _AVI_ChunkFree( stream_t *s,
732                      avi_chunk_t *p_chk )
733 {
734     int i_index;
735     avi_chunk_t *p_child, *p_next;
736
737     if( !p_chk )
738     {
739         return;
740     }
741
742     /* Free all child chunk */
743     p_child = p_chk->common.p_first;
744     while( p_child )
745     {
746         p_next = p_child->common.p_next;
747         AVI_ChunkFree( s, p_child );
748         free( p_child );
749         p_child = p_next;
750     }
751
752     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
753     if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
754     {
755 #ifdef AVI_DEBUG
756         msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
757                  (char*)&p_chk->common.i_chunk_fourcc );
758 #endif
759         AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
760     }
761     else
762     {
763         msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
764     }
765     p_chk->common.p_first = NULL;
766     p_chk->common.p_last  = NULL;
767
768     return;
769 }
770
771 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
772                                       avi_chunk_t  *p_chk, int i_level )
773 {
774     char str[1024];
775     int i;
776     avi_chunk_t *p_child;
777
778     memset( str, ' ', sizeof( str ) );
779     for( i = 1; i < i_level; i++ )
780     {
781         str[i * 5] = '|';
782     }
783     if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
784         p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
785     {
786         sprintf( str + i_level * 5,
787                  "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
788                  i_level ? '+' : '*',
789                  (char*)&p_chk->common.i_chunk_fourcc,
790                  (char*)&p_chk->list.i_type,
791                  p_chk->common.i_chunk_size,
792                  p_chk->common.i_chunk_pos );
793     }
794     else
795     {
796         sprintf( str + i_level * 5,
797                  "+ %4.4s size:"I64Fu" pos:"I64Fu,
798                  (char*)&p_chk->common.i_chunk_fourcc,
799                  p_chk->common.i_chunk_size,
800                  p_chk->common.i_chunk_pos );
801     }
802     msg_Dbg( p_obj, "%s", str );
803
804     p_child = p_chk->common.p_first;
805     while( p_child )
806     {
807         AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
808         p_child = p_child->common.p_next;
809     }
810 }
811
812 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
813 {
814     avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
815     avi_chunk_t      *p_chk;
816     vlc_bool_t b_seekable;
817
818     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
819
820     p_list->i_chunk_pos  = 0;
821     p_list->i_chunk_size = stream_Size( s );
822     p_list->i_chunk_fourcc = AVIFOURCC_LIST;
823     p_list->p_father = NULL;
824     p_list->p_next  = NULL;
825     p_list->p_first = NULL;
826     p_list->p_last  = NULL;
827
828     p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
829
830     for( ; ; )
831     {
832         p_chk = malloc( sizeof( avi_chunk_t ) );
833         memset( p_chk, 0, sizeof( avi_chunk_t ) );
834         if( !p_root->common.p_first )
835         {
836             p_root->common.p_first = p_chk;
837         }
838         else
839         {
840             p_root->common.p_last->common.p_next = p_chk;
841         }
842         p_root->common.p_last = p_chk;
843
844         if( AVI_ChunkRead( s, p_chk, p_root ) ||
845            ( stream_Tell( s ) >=
846               (off_t)p_chk->common.p_father->common.i_chunk_pos +
847                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
848         {
849             break;
850         }
851         /* If we can't seek then stop when we 've found first RIFF-AVI */
852         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
853             p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
854         {
855             break;
856         }
857     }
858
859     AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
860     return VLC_SUCCESS;
861 }
862
863 void AVI_ChunkFreeRoot( stream_t *s,
864                         avi_chunk_t  *p_chk )
865 {
866     AVI_ChunkFree( s, p_chk );
867 }
868
869
870 int  _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
871 {
872     int i_count;
873     avi_chunk_t *p_child;
874
875     if( !p_chk )
876     {
877         return 0;
878     }
879
880     i_count = 0;
881     p_child = p_chk->common.p_first;
882     while( p_child )
883     {
884         if( p_child->common.i_chunk_fourcc == i_fourcc ||
885             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
886               p_child->list.i_type == i_fourcc ) )
887         {
888             i_count++;
889         }
890         p_child = p_child->common.p_next;
891     }
892     return i_count;
893 }
894
895 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
896                       vlc_fourcc_t i_fourcc, int i_number )
897 {
898     avi_chunk_t *p_child;
899     if( !p_chk )
900     {
901         return NULL;
902     }
903     p_child = p_chk->common.p_first;
904
905     while( p_child )
906     {
907         if( p_child->common.i_chunk_fourcc == i_fourcc ||
908             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
909               p_child->list.i_type == i_fourcc ) )
910         {
911             if( i_number == 0 )
912             {
913                 /* We found it */
914                 return p_child;
915             }
916
917             i_number--;
918         }
919         p_child = p_child->common.p_next;
920     }
921     return NULL;
922 }
923
924