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