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