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