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