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