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