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