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