]> git.sesse.net Git - vlc/blob - modules/demux/avi/libavi.c
* a few more
[vlc] / modules / demux / avi / libavi.c
1 /*****************************************************************************
2  * libavi.c : LibAVI
3  *****************************************************************************
4  * Copyright (C) 2001 the VideoLAN team
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23
24 #include <vlc/vlc.h>
25 #include <vlc_demux.h>
26 #include <vlc_codecs.h>                                /* BITMAPINFOHEADER */
27
28 #include "libavi.h"
29
30 #define AVI_DEBUG 1
31
32 #define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
33
34 static vlc_fourcc_t GetFOURCC( const byte_t *p_buff )
35 {
36     return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
37 }
38
39 #define AVI_ChunkFree( a, b ) _AVI_ChunkFree( (a), (avi_chunk_t*)(b) )
40 void    _AVI_ChunkFree( stream_t *, avi_chunk_t *p_chk );
41
42 /****************************************************************************
43  *
44  * Basics functions to manipulates chunks
45  *
46  ****************************************************************************/
47 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
48 {
49     const uint8_t *p_peek;
50     int i_peek;
51
52     memset( p_chk, 0, sizeof( avi_chunk_t ) );
53
54     if( ( i_peek = stream_Peek( s, &p_peek, 8 ) ) < 8 )
55     {
56         return VLC_EGENERIC;
57     }
58
59     p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
60     p_chk->common.i_chunk_size   = GetDWLE( p_peek + 4 );
61     p_chk->common.i_chunk_pos    = stream_Tell( s );
62
63     p_chk->common.p_father = NULL;
64     p_chk->common.p_next = NULL;
65     p_chk->common.p_first = NULL;
66     p_chk->common.p_next = NULL;
67
68 #ifdef AVI_DEBUG
69     msg_Dbg( (vlc_object_t*)s,
70              "found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
71              p_chk->common.i_chunk_fourcc,
72              (char*)&p_chk->common.i_chunk_fourcc,
73              p_chk->common.i_chunk_size,
74              p_chk->common.i_chunk_pos );
75 #endif
76     return VLC_SUCCESS;
77 }
78
79 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
80 {
81     avi_chunk_t chk;
82
83     if( !p_chk )
84     {
85         if( AVI_ChunkReadCommon( s, &chk ) )
86         {
87             return VLC_EGENERIC;
88         }
89         p_chk = &chk;
90     }
91
92     if( p_chk->common.p_father )
93     {
94         if( p_chk->common.p_father->common.i_chunk_pos +
95                 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
96             p_chk->common.i_chunk_pos +
97                 __EVEN( p_chk->common.i_chunk_size ) + 8 )
98         {
99             return VLC_EGENERIC;
100         }
101     }
102     return stream_Seek( s, p_chk->common.i_chunk_pos +
103                                  __EVEN( p_chk->common.i_chunk_size ) + 8 );
104 }
105
106 /****************************************************************************
107  *
108  * Functions to read chunks
109  *
110  ****************************************************************************/
111 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
112 {
113     avi_chunk_t *p_chk;
114     const uint8_t *p_peek;
115     vlc_bool_t b_seekable;
116
117     if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
118     {
119         /* empty box */
120         msg_Warn( (vlc_object_t*)s, "empty list chunk" );
121         return VLC_EGENERIC;
122     }
123     if( stream_Peek( s, &p_peek, 12 ) < 12 )
124     {
125         msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
126         return VLC_EGENERIC;
127     }
128
129     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
130
131     p_container->list.i_type = GetFOURCC( p_peek + 8 );
132
133     /* XXX fixed for on2 hack */
134     if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
135     {
136         p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
137         p_container->list.i_type = AVIFOURCC_AVI;
138     }
139
140     if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
141         p_container->list.i_type == AVIFOURCC_movi )
142     {
143         msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
144         if( b_seekable )
145         {
146             return AVI_NextChunk( s, p_container );
147         }
148         return VLC_SUCCESS; /* point at begining of LIST-movi */
149     }
150
151     if( stream_Read( s, NULL, 12 ) != 12 )
152     {
153         msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
154         return VLC_EGENERIC;
155     }
156
157 #ifdef AVI_DEBUG
158     msg_Dbg( (vlc_object_t*)s,
159              "found LIST chunk: \'%4.4s\'",
160              (char*)&p_container->list.i_type );
161 #endif
162     msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
163     for( ; ; )
164     {
165         p_chk = malloc( sizeof( avi_chunk_t ) );
166         memset( p_chk, 0, sizeof( avi_chunk_t ) );
167         if( !p_container->common.p_first )
168         {
169             p_container->common.p_first = p_chk;
170         }
171         else
172         {
173             p_container->common.p_last->common.p_next = p_chk;
174         }
175         p_container->common.p_last = p_chk;
176
177         if( AVI_ChunkRead( s, p_chk, p_container ) )
178         {
179             break;
180         }
181         if( p_chk->common.p_father->common.i_chunk_size > 0 &&
182            ( stream_Tell( s ) >
183               (off_t)p_chk->common.p_father->common.i_chunk_pos +
184                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
185         {
186             break;
187         }
188
189         /* If we can't seek then stop when we 've found LIST-movi */
190         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
191             p_chk->list.i_type == AVIFOURCC_movi &&
192             ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
193         {
194             break;
195         }
196
197     }
198     msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
199
200     return VLC_SUCCESS;
201 }
202
203 #define AVI_READCHUNK_ENTER \
204     int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
205     uint8_t  *p_read, *p_buff;    \
206     if( !( p_read = p_buff = malloc(i_read ) ) ) \
207     { \
208         return VLC_EGENERIC; \
209     } \
210     i_read = stream_Read( s, p_read, i_read ); \
211     if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
212     { \
213         free( p_buff ); \
214         return VLC_EGENERIC; \
215     }\
216     p_read += 8; \
217     i_read -= 8
218
219 #define AVI_READ( res, func, size ) \
220     if( i_read < size ) { \
221         free( p_buff); \
222         return VLC_EGENERIC; \
223     } \
224     i_read -= size; \
225     res = func( p_read ); \
226     p_read += size \
227
228 #define AVI_READCHUNK_EXIT( code ) \
229     free( p_buff ); \
230     return code
231
232 static inline uint8_t GetB( uint8_t *ptr )
233 {
234     return *ptr;
235 }
236
237 #define AVI_READ1BYTE( i_byte ) \
238     AVI_READ( i_byte, GetB, 1 )
239
240 #define AVI_READ2BYTES( i_word ) \
241     AVI_READ( i_word, GetWLE, 2 )
242
243 #define AVI_READ4BYTES( i_dword ) \
244     AVI_READ( i_dword, GetDWLE, 4 )
245
246 #define AVI_READ8BYTES( i_qword ) \
247     AVI_READ( i_qword, GetQWLE, 8 )
248
249 #define AVI_READFOURCC( i_dword ) \
250     AVI_READ( i_dword, GetFOURCC, 4 )
251
252 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
253 {
254     AVI_READCHUNK_ENTER;
255
256     p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
257     AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
258     AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
259     AVI_READ4BYTES( p_chk->avih.i_reserved1 );
260     AVI_READ4BYTES( p_chk->avih.i_flags );
261     AVI_READ4BYTES( p_chk->avih.i_totalframes );
262     AVI_READ4BYTES( p_chk->avih.i_initialframes );
263     AVI_READ4BYTES( p_chk->avih.i_streams );
264     AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
265     AVI_READ4BYTES( p_chk->avih.i_width );
266     AVI_READ4BYTES( p_chk->avih.i_height );
267     AVI_READ4BYTES( p_chk->avih.i_scale );
268     AVI_READ4BYTES( p_chk->avih.i_rate );
269     AVI_READ4BYTES( p_chk->avih.i_start );
270     AVI_READ4BYTES( p_chk->avih.i_length );
271 #ifdef AVI_DEBUG
272     msg_Dbg( (vlc_object_t*)s,
273              "avih: streams:%d flags:%s%s%s%s %dx%d",
274              p_chk->avih.i_streams,
275              p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
276              p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
277              p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
278              p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
279              p_chk->avih.i_width, p_chk->avih.i_height );
280 #endif
281     AVI_READCHUNK_EXIT( VLC_SUCCESS );
282 }
283
284 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
285 {
286     AVI_READCHUNK_ENTER;
287
288     AVI_READFOURCC( p_chk->strh.i_type );
289     AVI_READFOURCC( p_chk->strh.i_handler );
290     AVI_READ4BYTES( p_chk->strh.i_flags );
291     AVI_READ4BYTES( p_chk->strh.i_reserved1 );
292     AVI_READ4BYTES( p_chk->strh.i_initialframes );
293     AVI_READ4BYTES( p_chk->strh.i_scale );
294     AVI_READ4BYTES( p_chk->strh.i_rate );
295     AVI_READ4BYTES( p_chk->strh.i_start );
296     AVI_READ4BYTES( p_chk->strh.i_length );
297     AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
298     AVI_READ4BYTES( p_chk->strh.i_quality );
299     AVI_READ4BYTES( p_chk->strh.i_samplesize );
300 #ifdef AVI_DEBUG
301     msg_Dbg( (vlc_object_t*)s,
302              "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
303              (char*)&p_chk->strh.i_type,
304              p_chk->strh.i_handler,
305              p_chk->strh.i_samplesize,
306              ( p_chk->strh.i_scale ?
307                 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
308 #endif
309
310     AVI_READCHUNK_EXIT( VLC_SUCCESS );
311 }
312
313 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
314 {
315     avi_chunk_t *p_strh;
316
317     AVI_READCHUNK_ENTER;
318     if( p_chk->common.p_father == NULL )
319     {
320         msg_Err( (vlc_object_t*)s, "malformed avi file" );
321         AVI_READCHUNK_EXIT( VLC_EGENERIC );
322     }
323     if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
324     {
325         msg_Err( (vlc_object_t*)s, "malformed avi file" );
326         AVI_READCHUNK_EXIT( VLC_EGENERIC );
327     }
328
329     switch( p_strh->strh.i_type )
330     {
331         case( AVIFOURCC_auds ):
332             p_chk->strf.auds.i_cat = AUDIO_ES;
333             p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
334             AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
335             AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
336             AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
337             AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
338             AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
339             AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
340             if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
341                  && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
342             {
343                 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
344                 /* prevent segfault */
345                 if( p_chk->strf.auds.p_wf->cbSize >
346                         p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
347                 {
348                     p_chk->strf.auds.p_wf->cbSize =
349                         p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
350                 }
351                 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
352                 {
353                     /* Found an extensible header atm almost nothing uses that. */
354                     msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
355                               "vorbis audio dectected: not supported" );
356                 }
357             }
358             else
359             {
360                 p_chk->strf.auds.p_wf->cbSize = 0;
361             }
362             if( p_chk->strf.auds.p_wf->cbSize > 0 )
363             {
364                 memcpy( &p_chk->strf.auds.p_wf[1] ,
365                         p_buff + 8 + sizeof( WAVEFORMATEX ),    /*  8=fourrc+size */
366                         p_chk->strf.auds.p_wf->cbSize );
367             }
368 #ifdef AVI_DEBUG
369             msg_Dbg( (vlc_object_t*)s,
370                      "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
371                      p_chk->strf.auds.p_wf->wFormatTag,
372                      p_chk->strf.auds.p_wf->nChannels,
373                      p_chk->strf.auds.p_wf->nSamplesPerSec,
374                      p_chk->strf.auds.p_wf->wBitsPerSample,
375                      p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
376 #endif
377             break;
378         case( AVIFOURCC_vids ):
379             p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
380             p_chk->strf.vids.i_cat = VIDEO_ES;
381             p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
382             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
383             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
384             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
385             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
386             AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
387             AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
388             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
389             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
390             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
391             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
392             AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
393             if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
394             {
395                 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
396             }
397             if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
398             {
399                 memcpy( &p_chk->strf.vids.p_bih[1],
400                         p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
401                         p_chk->common.i_chunk_size -sizeof(BITMAPINFOHEADER) );
402             }
403 #ifdef AVI_DEBUG
404             msg_Dbg( (vlc_object_t*)s,
405                      "strf: video:%4.4s %dx%d planes:%d %dbpp",
406                      (char*)&p_chk->strf.vids.p_bih->biCompression,
407                      p_chk->strf.vids.p_bih->biWidth,
408                      p_chk->strf.vids.p_bih->biHeight,
409                      p_chk->strf.vids.p_bih->biPlanes,
410                      p_chk->strf.vids.p_bih->biBitCount );
411 #endif
412             break;
413         default:
414             msg_Warn( (vlc_object_t*)s, "unknown stream type" );
415             p_chk->strf.common.i_cat = UNKNOWN_ES;
416             break;
417     }
418     AVI_READCHUNK_EXIT( VLC_SUCCESS );
419 }
420 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
421 {
422     avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
423     if( p_strf->common.i_cat == AUDIO_ES )
424     {
425         FREENULL( p_strf->auds.p_wf );
426     }
427     else if( p_strf->common.i_cat == VIDEO_ES )
428     {
429         FREENULL( p_strf->vids.p_bih );
430     }
431 }
432
433 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
434 {
435     AVI_READCHUNK_ENTER;
436     p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
437     memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
438     AVI_READCHUNK_EXIT( VLC_SUCCESS );
439 }
440
441 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
442 {
443     if( p_chk->strd.p_data ) free( p_chk->strd.p_data );
444 }
445
446 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
447 {
448     unsigned int i_count, i_index;
449
450     AVI_READCHUNK_ENTER;
451
452     i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
453
454     p_chk->idx1.i_entry_count = i_count;
455     p_chk->idx1.i_entry_max   = i_count;
456     if( i_count > 0 )
457     {
458         p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
459
460         for( i_index = 0; i_index < i_count ; i_index++ )
461         {
462             AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
463             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
464             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
465             AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
466         }
467     }
468     else
469     {
470         p_chk->idx1.entry = NULL;
471     }
472 #ifdef AVI_DEBUG
473     msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
474 #endif
475     AVI_READCHUNK_EXIT( VLC_SUCCESS );
476 }
477
478 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
479 {
480     p_chk->idx1.i_entry_count = 0;
481     p_chk->idx1.i_entry_max   = 0;
482     FREENULL( p_chk->idx1.entry );
483 }
484
485
486
487 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
488 {
489     unsigned int i_count, i;
490     int32_t      i_dummy;
491     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
492
493     AVI_READCHUNK_ENTER;
494
495     AVI_READ2BYTES( p_indx->i_longsperentry );
496     AVI_READ1BYTE ( p_indx->i_indexsubtype );
497     AVI_READ1BYTE ( p_indx->i_indextype );
498     AVI_READ4BYTES( p_indx->i_entriesinuse );
499
500     AVI_READ4BYTES( p_indx->i_id );
501     p_indx->idx.std     = NULL;
502     p_indx->idx.field   = NULL;
503     p_indx->idx.super   = NULL;
504
505     if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
506     {
507         AVI_READ8BYTES( p_indx->i_baseoffset );
508         AVI_READ4BYTES( i_dummy );
509
510         i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
511         p_indx->i_entriesinuse = i_count;
512         p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
513
514         for( i = 0; i < i_count; i++ )
515         {
516             AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
517             AVI_READ4BYTES( p_indx->idx.std[i].i_size );
518         }
519     }
520     else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
521     {
522         AVI_READ8BYTES( p_indx->i_baseoffset );
523         AVI_READ4BYTES( i_dummy );
524
525         i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
526         p_indx->i_entriesinuse = i_count;
527         p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
528         for( i = 0; i < i_count; i++ )
529         {
530             AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
531             AVI_READ4BYTES( p_indx->idx.field[i].i_size );
532             AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
533         }
534     }
535     else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
536     {
537         p_indx->i_baseoffset = 0;
538         AVI_READ4BYTES( i_dummy );
539         AVI_READ4BYTES( i_dummy );
540         AVI_READ4BYTES( i_dummy );
541
542         i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
543         p_indx->i_entriesinuse = i_count;
544         p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
545
546         for( i = 0; i < i_count; i++ )
547         {
548             AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
549             AVI_READ4BYTES( p_indx->idx.super[i].i_size );
550             AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
551         }
552     }
553     else
554     {
555         msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
556     }
557
558 #ifdef AVI_DEBUG
559     msg_Dbg( (vlc_object_t*)s, "indx: type=%d subtype=%d entry=%d", p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
560 #endif
561     AVI_READCHUNK_EXIT( VLC_SUCCESS );
562 }
563 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
564 {
565     avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
566
567     FREENULL( p_indx->idx.std );
568     FREENULL( p_indx->idx.field );
569     FREENULL( p_indx->idx.super );
570 }
571
572
573
574 static struct
575 {
576     vlc_fourcc_t i_fourcc;
577     const char *psz_type;
578 } AVI_strz_type[] =
579 {
580     { AVIFOURCC_IARL, "archive location" },
581     { AVIFOURCC_IART, "artist" },
582     { AVIFOURCC_ICMS, "commisioned" },
583     { AVIFOURCC_ICMT, "comments" },
584     { AVIFOURCC_ICOP, "copyright" },
585     { AVIFOURCC_ICRD, "creation date" },
586     { AVIFOURCC_ICRP, "cropped" },
587     { AVIFOURCC_IDIM, "dimensions" },
588     { AVIFOURCC_IDPI, "dots per inch" },
589     { AVIFOURCC_IENG, "engineer" },
590     { AVIFOURCC_IGNR, "genre" },
591     { AVIFOURCC_IKEY, "keywords" },
592     { AVIFOURCC_ILGT, "lightness" },
593     { AVIFOURCC_IMED, "medium" },
594     { AVIFOURCC_INAM, "name" },
595     { AVIFOURCC_IPLT, "palette setting" },
596     { AVIFOURCC_IPRD, "product" },
597     { AVIFOURCC_ISBJ, "subject" },
598     { AVIFOURCC_ISFT, "software" },
599     { AVIFOURCC_ISHP, "sharpness" },
600     { AVIFOURCC_ISRC, "source" },
601     { AVIFOURCC_ISRF, "source form" },
602     { AVIFOURCC_ITCH, "technician" },
603     { AVIFOURCC_ISMP, "time code" },
604     { AVIFOURCC_IDIT, "digitalization time" },
605     { AVIFOURCC_strn, "stream name" },
606     { 0,              "???" }
607 };
608 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
609 {
610     int i_index;
611     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
612     AVI_READCHUNK_ENTER;
613
614     for( i_index = 0;; i_index++)
615     {
616         if( !AVI_strz_type[i_index].i_fourcc ||
617             AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
618         {
619             break;
620         }
621     }
622     p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
623     p_strz->p_str = malloc( i_read + 1);
624
625     if( p_strz->i_chunk_size )
626     {
627         memcpy( p_strz->p_str, p_read, i_read );
628     }
629     p_strz->p_str[i_read] = 0;
630
631 #ifdef AVI_DEBUG
632     msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
633              (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
634 #endif
635     AVI_READCHUNK_EXIT( VLC_SUCCESS );
636 }
637 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
638 {
639     avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
640     FREENULL( p_strz->p_type );
641     FREENULL( p_strz->p_str );
642 }
643
644 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
645 {
646     return AVI_NextChunk( s, p_chk );
647 }
648 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
649 {
650
651 }
652
653 static struct
654 {
655     vlc_fourcc_t i_fourcc;
656     int   (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
657     void  (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
658 } AVI_Chunk_Function [] =
659 {
660     { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
661     { AVIFOURCC_ON2,  AVI_ChunkRead_list, AVI_ChunkFree_nothing },
662     { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
663     { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
664     { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
665     { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
666     { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
667     { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
668     { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
669     { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
670     { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
671
672     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
673     { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
674     { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
675     { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
676     { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
677     { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
678     { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
679     { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
680     { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
681     { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
682     { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
683     { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
684     { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
685     { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
686     { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
687     { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
688     { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
689     { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
690     { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
691     { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
692     { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
693     { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
694     { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
695     { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
696     { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
697     { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
698     { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
699     { 0,           NULL,               NULL }
700 };
701
702 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
703 {
704     unsigned int i_index;
705     for( i_index = 0; ; i_index++ )
706     {
707         if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
708             ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
709         {
710             return i_index;
711         }
712     }
713 }
714
715 int  _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
716 {
717     int i_index;
718
719     if( !p_chk )
720     {
721         return VLC_EGENERIC;
722     }
723
724     if( AVI_ChunkReadCommon( s, p_chk ) )
725     {
726         msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
727         return VLC_EGENERIC;
728     }
729     if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
730     {
731         msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
732         return VLC_EGENERIC;
733     }
734     p_chk->common.p_father = p_father;
735
736     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
737     if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
738     {
739         return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
740     }
741     else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
742                ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
743              ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
744                ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
745     {
746         p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
747         return AVI_ChunkRead_indx( s, p_chk );
748     }
749     msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
750     return AVI_NextChunk( s, p_chk );
751 }
752
753 void _AVI_ChunkFree( stream_t *s,
754                      avi_chunk_t *p_chk )
755 {
756     int i_index;
757     avi_chunk_t *p_child, *p_next;
758
759     if( !p_chk )
760     {
761         return;
762     }
763
764     /* Free all child chunk */
765     p_child = p_chk->common.p_first;
766     while( p_child )
767     {
768         p_next = p_child->common.p_next;
769         AVI_ChunkFree( s, p_child );
770         free( p_child );
771         p_child = p_next;
772     }
773
774     i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
775     if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
776     {
777 #ifdef AVI_DEBUG
778         msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
779                  (char*)&p_chk->common.i_chunk_fourcc );
780 #endif
781         AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
782     }
783     else
784     {
785         msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
786     }
787     p_chk->common.p_first = NULL;
788     p_chk->common.p_last  = NULL;
789
790     return;
791 }
792
793 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
794                                       avi_chunk_t  *p_chk, int i_level )
795 {
796     char str[1024];
797     int i;
798     avi_chunk_t *p_child;
799
800     memset( str, ' ', sizeof( str ) );
801     for( i = 1; i < i_level; i++ )
802     {
803         str[i * 5] = '|';
804     }
805     if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
806         p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2  ||
807         p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
808     {
809         sprintf( str + i_level * 5,
810                  "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
811                  i_level ? '+' : '*',
812                  (char*)&p_chk->common.i_chunk_fourcc,
813                  (char*)&p_chk->list.i_type,
814                  p_chk->common.i_chunk_size,
815                  p_chk->common.i_chunk_pos );
816     }
817     else
818     {
819         sprintf( str + i_level * 5,
820                  "+ %4.4s size:"I64Fu" pos:"I64Fu,
821                  (char*)&p_chk->common.i_chunk_fourcc,
822                  p_chk->common.i_chunk_size,
823                  p_chk->common.i_chunk_pos );
824     }
825     msg_Dbg( p_obj, "%s", str );
826
827     p_child = p_chk->common.p_first;
828     while( p_child )
829     {
830         AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
831         p_child = p_child->common.p_next;
832     }
833 }
834
835 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
836 {
837     avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
838     avi_chunk_t      *p_chk;
839     vlc_bool_t b_seekable;
840
841     stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
842
843     p_list->i_chunk_pos  = 0;
844     p_list->i_chunk_size = stream_Size( s );
845     p_list->i_chunk_fourcc = AVIFOURCC_LIST;
846     p_list->p_father = NULL;
847     p_list->p_next  = NULL;
848     p_list->p_first = NULL;
849     p_list->p_last  = NULL;
850
851     p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
852
853     for( ; ; )
854     {
855         p_chk = malloc( sizeof( avi_chunk_t ) );
856         memset( p_chk, 0, sizeof( avi_chunk_t ) );
857         if( !p_root->common.p_first )
858         {
859             p_root->common.p_first = p_chk;
860         }
861         else
862         {
863             p_root->common.p_last->common.p_next = p_chk;
864         }
865         p_root->common.p_last = p_chk;
866
867         if( AVI_ChunkRead( s, p_chk, p_root ) ||
868            ( stream_Tell( s ) >=
869               (off_t)p_chk->common.p_father->common.i_chunk_pos +
870                (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
871         {
872             break;
873         }
874         /* If we can't seek then stop when we 've found first RIFF-AVI */
875         if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
876             p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
877         {
878             break;
879         }
880     }
881
882     AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
883     return VLC_SUCCESS;
884 }
885
886 void AVI_ChunkFreeRoot( stream_t *s,
887                         avi_chunk_t  *p_chk )
888 {
889     AVI_ChunkFree( s, p_chk );
890 }
891
892
893 int  _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
894 {
895     int i_count;
896     avi_chunk_t *p_child;
897
898     if( !p_chk )
899     {
900         return 0;
901     }
902
903     i_count = 0;
904     p_child = p_chk->common.p_first;
905     while( p_child )
906     {
907         if( p_child->common.i_chunk_fourcc == i_fourcc ||
908             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
909               p_child->list.i_type == i_fourcc ) )
910         {
911             i_count++;
912         }
913         p_child = p_child->common.p_next;
914     }
915     return i_count;
916 }
917
918 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
919                       vlc_fourcc_t i_fourcc, int i_number )
920 {
921     avi_chunk_t *p_child;
922     if( !p_chk )
923     {
924         return NULL;
925     }
926     p_child = p_chk->common.p_first;
927
928     while( p_child )
929     {
930         if( p_child->common.i_chunk_fourcc == i_fourcc ||
931             ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
932               p_child->list.i_type == i_fourcc ) )
933         {
934             if( i_number == 0 )
935             {
936                 /* We found it */
937                 return p_child;
938             }
939
940             i_number--;
941         }
942         p_child = p_child->common.p_next;
943     }
944     return NULL;
945 }
946
947