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