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