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