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