1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 the VideoLAN team
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
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.
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.
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 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_demux.h>
30 #include <vlc_codecs.h> /* BITMAPINFOHEADER */
36 #define __EVEN( x ) (((x) + 1) & ~1)
38 static vlc_fourcc_t GetFOURCC( const uint8_t *p_buff )
40 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
43 #define AVI_ChunkFree( a, b ) _AVI_ChunkFree( (a), (avi_chunk_t*)(b) )
44 void _AVI_ChunkFree( stream_t *, avi_chunk_t *p_chk );
46 /****************************************************************************
48 * Basics functions to manipulates chunks
50 ****************************************************************************/
51 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
53 const uint8_t *p_peek;
56 memset( p_chk, 0, sizeof( avi_chunk_t ) );
58 if( ( i_peek = stream_Peek( s, &p_peek, 8 ) ) < 8 )
63 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
64 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
65 p_chk->common.i_chunk_pos = stream_Tell( s );
67 p_chk->common.p_father = NULL;
68 p_chk->common.p_next = NULL;
69 p_chk->common.p_first = NULL;
70 p_chk->common.p_next = NULL;
73 msg_Dbg( (vlc_object_t*)s,
74 "found Chunk fourcc:%8.8x (%4.4s) size:%"PRId64" pos:%"PRId64,
75 p_chk->common.i_chunk_fourcc,
76 (char*)&p_chk->common.i_chunk_fourcc,
77 p_chk->common.i_chunk_size,
78 p_chk->common.i_chunk_pos );
83 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
89 if( AVI_ChunkReadCommon( s, &chk ) )
96 if( p_chk->common.p_father )
98 if( p_chk->common.p_father->common.i_chunk_pos +
99 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
100 p_chk->common.i_chunk_pos +
101 __EVEN( p_chk->common.i_chunk_size ) + 8 )
106 return stream_Seek( s, p_chk->common.i_chunk_pos +
107 __EVEN( p_chk->common.i_chunk_size ) + 8 );
110 /****************************************************************************
112 * Functions to read chunks
114 ****************************************************************************/
115 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
118 const uint8_t *p_peek;
121 if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
124 msg_Warn( (vlc_object_t*)s, "empty list chunk" );
127 if( stream_Peek( s, &p_peek, 12 ) < 12 )
129 msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
133 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
135 p_container->list.i_type = GetFOURCC( p_peek + 8 );
137 /* XXX fixed for on2 hack */
138 if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
140 p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
141 p_container->list.i_type = AVIFOURCC_AVI;
144 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
145 p_container->list.i_type == AVIFOURCC_movi )
147 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
150 return AVI_NextChunk( s, p_container );
152 return VLC_SUCCESS; /* point at begining of LIST-movi */
155 if( stream_Read( s, NULL, 12 ) != 12 )
157 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
162 msg_Dbg( (vlc_object_t*)s,
163 "found LIST chunk: \'%4.4s\'",
164 (char*)&p_container->list.i_type );
166 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
169 p_chk = malloc( sizeof( avi_chunk_t ) );
170 memset( p_chk, 0, sizeof( avi_chunk_t ) );
171 if( !p_container->common.p_first )
173 p_container->common.p_first = p_chk;
177 p_container->common.p_last->common.p_next = p_chk;
179 p_container->common.p_last = p_chk;
181 if( AVI_ChunkRead( s, p_chk, p_container ) )
185 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
187 (off_t)p_chk->common.p_father->common.i_chunk_pos +
188 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
193 /* If we can't seek then stop when we 've found LIST-movi */
194 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
195 p_chk->list.i_type == AVIFOURCC_movi &&
196 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
202 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
207 #define AVI_READCHUNK_ENTER \
208 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
209 uint8_t *p_read, *p_buff; \
210 if( !( p_read = p_buff = malloc(i_read ) ) ) \
212 return VLC_EGENERIC; \
214 i_read = stream_Read( s, p_read, i_read ); \
215 if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
218 return VLC_EGENERIC; \
223 #define AVI_READ( res, func, size ) \
224 if( i_read < size ) { \
226 return VLC_EGENERIC; \
229 res = func( p_read ); \
232 #define AVI_READCHUNK_EXIT( code ) \
236 static inline uint8_t GetB( uint8_t *ptr )
241 #define AVI_READ1BYTE( i_byte ) \
242 AVI_READ( i_byte, GetB, 1 )
244 #define AVI_READ2BYTES( i_word ) \
245 AVI_READ( i_word, GetWLE, 2 )
247 #define AVI_READ4BYTES( i_dword ) \
248 AVI_READ( i_dword, GetDWLE, 4 )
250 #define AVI_READ8BYTES( i_qword ) \
251 AVI_READ( i_qword, GetQWLE, 8 )
253 #define AVI_READFOURCC( i_dword ) \
254 AVI_READ( i_dword, GetFOURCC, 4 )
256 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
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 );
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 );
285 AVI_READCHUNK_EXIT( VLC_SUCCESS );
288 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
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 );
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) );
314 AVI_READCHUNK_EXIT( VLC_SUCCESS );
317 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
322 if( p_chk->common.p_father == NULL )
324 msg_Err( (vlc_object_t*)s, "malformed avi file" );
325 AVI_READCHUNK_EXIT( VLC_EGENERIC );
327 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
329 msg_Err( (vlc_object_t*)s, "malformed avi file" );
330 AVI_READCHUNK_EXIT( VLC_EGENERIC );
333 switch( p_strh->strh.i_type )
335 case( AVIFOURCC_auds ):
336 p_chk->strf.auds.i_cat = AUDIO_ES;
337 p_chk->strf.auds.p_wf = malloc( __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 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
345 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
347 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
348 /* prevent segfault */
349 if( p_chk->strf.auds.p_wf->cbSize >
350 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
352 p_chk->strf.auds.p_wf->cbSize =
353 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
355 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
357 /* Found an extensible header atm almost nothing uses that. */
358 msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
359 "vorbis audio dectected: not supported" );
364 p_chk->strf.auds.p_wf->cbSize = 0;
366 if( p_chk->strf.auds.p_wf->cbSize > 0 )
368 memcpy( &p_chk->strf.auds.p_wf[1] ,
369 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourrc+size */
370 p_chk->strf.auds.p_wf->cbSize );
373 msg_Dbg( (vlc_object_t*)s,
374 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
375 p_chk->strf.auds.p_wf->wFormatTag,
376 p_chk->strf.auds.p_wf->nChannels,
377 p_chk->strf.auds.p_wf->nSamplesPerSec,
378 p_chk->strf.auds.p_wf->wBitsPerSample,
379 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
382 case( AVIFOURCC_vids ):
383 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
384 p_chk->strf.vids.i_cat = VIDEO_ES;
385 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
386 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
387 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
388 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
389 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
390 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
391 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
392 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
393 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
394 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
395 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
396 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
397 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
399 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
401 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
403 memcpy( &p_chk->strf.vids.p_bih[1],
404 p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
405 p_chk->common.i_chunk_size -sizeof(BITMAPINFOHEADER) );
408 msg_Dbg( (vlc_object_t*)s,
409 "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
410 (char*)&p_chk->strf.vids.p_bih->biCompression,
411 (uint32_t)p_chk->strf.vids.p_bih->biWidth,
412 (uint32_t)p_chk->strf.vids.p_bih->biHeight,
413 p_chk->strf.vids.p_bih->biPlanes,
414 p_chk->strf.vids.p_bih->biBitCount );
418 msg_Warn( (vlc_object_t*)s, "unknown stream type" );
419 p_chk->strf.common.i_cat = UNKNOWN_ES;
422 AVI_READCHUNK_EXIT( VLC_SUCCESS );
424 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
426 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
427 if( p_strf->common.i_cat == AUDIO_ES )
429 FREENULL( p_strf->auds.p_wf );
431 else if( p_strf->common.i_cat == VIDEO_ES )
433 FREENULL( p_strf->vids.p_bih );
437 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
440 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
441 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
442 AVI_READCHUNK_EXIT( VLC_SUCCESS );
445 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
447 free( p_chk->strd.p_data );
450 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
452 unsigned int i_count, i_index;
456 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
458 p_chk->idx1.i_entry_count = i_count;
459 p_chk->idx1.i_entry_max = i_count;
462 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
464 for( i_index = 0; i_index < i_count ; i_index++ )
466 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
467 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
468 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
469 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
474 p_chk->idx1.entry = NULL;
477 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
479 AVI_READCHUNK_EXIT( VLC_SUCCESS );
482 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
484 p_chk->idx1.i_entry_count = 0;
485 p_chk->idx1.i_entry_max = 0;
486 FREENULL( p_chk->idx1.entry );
491 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
493 unsigned int i_count, i;
495 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
499 AVI_READ2BYTES( p_indx->i_longsperentry );
500 AVI_READ1BYTE ( p_indx->i_indexsubtype );
501 AVI_READ1BYTE ( p_indx->i_indextype );
502 AVI_READ4BYTES( p_indx->i_entriesinuse );
504 AVI_READ4BYTES( p_indx->i_id );
505 p_indx->idx.std = NULL;
506 p_indx->idx.field = NULL;
507 p_indx->idx.super = NULL;
509 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
511 AVI_READ8BYTES( p_indx->i_baseoffset );
512 AVI_READ4BYTES( i_dummy );
514 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
515 p_indx->i_entriesinuse = i_count;
516 p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
518 for( i = 0; i < i_count; i++ )
520 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
521 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
524 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
526 AVI_READ8BYTES( p_indx->i_baseoffset );
527 AVI_READ4BYTES( i_dummy );
529 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
530 p_indx->i_entriesinuse = i_count;
531 p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
532 for( i = 0; i < i_count; i++ )
534 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
535 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
536 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
539 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
541 p_indx->i_baseoffset = 0;
542 AVI_READ4BYTES( i_dummy );
543 AVI_READ4BYTES( i_dummy );
544 AVI_READ4BYTES( i_dummy );
546 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
547 p_indx->i_entriesinuse = i_count;
548 p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
550 for( i = 0; i < i_count; i++ )
552 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
553 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
554 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
559 msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
563 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 );
565 AVI_READCHUNK_EXIT( VLC_SUCCESS );
567 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
569 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
571 FREENULL( p_indx->idx.std );
572 FREENULL( p_indx->idx.field );
573 FREENULL( p_indx->idx.super );
580 vlc_fourcc_t i_fourcc;
581 const char *psz_type;
584 { AVIFOURCC_IARL, "archive location" },
585 { AVIFOURCC_IART, "artist" },
586 { AVIFOURCC_ICMS, "commisioned" },
587 { AVIFOURCC_ICMT, "comments" },
588 { AVIFOURCC_ICOP, "copyright" },
589 { AVIFOURCC_ICRD, "creation date" },
590 { AVIFOURCC_ICRP, "cropped" },
591 { AVIFOURCC_IDIM, "dimensions" },
592 { AVIFOURCC_IDPI, "dots per inch" },
593 { AVIFOURCC_IENG, "engineer" },
594 { AVIFOURCC_IGNR, "genre" },
595 { AVIFOURCC_IKEY, "keywords" },
596 { AVIFOURCC_ILGT, "lightness" },
597 { AVIFOURCC_IMED, "medium" },
598 { AVIFOURCC_INAM, "name" },
599 { AVIFOURCC_IPLT, "palette setting" },
600 { AVIFOURCC_IPRD, "product" },
601 { AVIFOURCC_ISBJ, "subject" },
602 { AVIFOURCC_ISFT, "software" },
603 { AVIFOURCC_ISHP, "sharpness" },
604 { AVIFOURCC_ISRC, "source" },
605 { AVIFOURCC_ISRF, "source form" },
606 { AVIFOURCC_ITCH, "technician" },
607 { AVIFOURCC_ISMP, "time code" },
608 { AVIFOURCC_IDIT, "digitalization time" },
609 { AVIFOURCC_strn, "stream name" },
612 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
615 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
618 for( i_index = 0;; i_index++)
620 if( !AVI_strz_type[i_index].i_fourcc ||
621 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
626 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
627 p_strz->p_str = malloc( i_read + 1);
629 if( p_strz->i_chunk_size )
631 memcpy( p_strz->p_str, p_read, i_read );
633 p_strz->p_str[i_read] = 0;
636 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
637 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
639 AVI_READCHUNK_EXIT( VLC_SUCCESS );
641 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
643 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
644 FREENULL( p_strz->p_type );
645 FREENULL( p_strz->p_str );
648 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
650 return AVI_NextChunk( s, p_chk );
652 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
659 vlc_fourcc_t i_fourcc;
660 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
661 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
662 } AVI_Chunk_Function [] =
664 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
665 { AVIFOURCC_ON2, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
666 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
667 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
668 { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
669 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
670 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
671 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
672 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
673 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
674 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
676 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
677 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
678 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
679 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
680 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
681 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
682 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
683 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
684 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
685 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
686 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
687 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
688 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
689 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
690 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
691 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
692 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
693 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
694 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
695 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
696 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
697 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
698 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
699 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
700 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
701 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
702 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
706 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
708 unsigned int i_index;
709 for( i_index = 0; ; i_index++ )
711 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
712 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
719 int _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
728 if( AVI_ChunkReadCommon( s, p_chk ) )
730 msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
733 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
735 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
738 p_chk->common.p_father = p_father;
740 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
741 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
743 return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
745 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
746 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
747 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
748 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
750 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
751 return AVI_ChunkRead_indx( s, p_chk );
753 msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
754 return AVI_NextChunk( s, p_chk );
757 void _AVI_ChunkFree( stream_t *s,
761 avi_chunk_t *p_child, *p_next;
768 /* Free all child chunk */
769 p_child = p_chk->common.p_first;
772 p_next = p_child->common.p_next;
773 AVI_ChunkFree( s, p_child );
778 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
779 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
782 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
783 (char*)&p_chk->common.i_chunk_fourcc );
785 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
789 msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
791 p_chk->common.p_first = NULL;
792 p_chk->common.p_last = NULL;
797 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
798 avi_chunk_t *p_chk, int i_level )
802 avi_chunk_t *p_child;
804 memset( str, ' ', sizeof( str ) );
805 for( i = 1; i < i_level; i++ )
809 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
810 p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
811 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
813 sprintf( str + i_level * 5,
814 "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
816 (char*)&p_chk->common.i_chunk_fourcc,
817 (char*)&p_chk->list.i_type,
818 p_chk->common.i_chunk_size,
819 p_chk->common.i_chunk_pos );
823 sprintf( str + i_level * 5,
824 "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
825 (char*)&p_chk->common.i_chunk_fourcc,
826 p_chk->common.i_chunk_size,
827 p_chk->common.i_chunk_pos );
829 msg_Dbg( p_obj, "%s", str );
831 p_child = p_chk->common.p_first;
834 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
835 p_child = p_child->common.p_next;
839 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
841 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
845 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
847 p_list->i_chunk_pos = 0;
848 p_list->i_chunk_size = stream_Size( s );
849 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
850 p_list->p_father = NULL;
851 p_list->p_next = NULL;
852 p_list->p_first = NULL;
853 p_list->p_last = NULL;
855 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
859 p_chk = malloc( sizeof( avi_chunk_t ) );
860 memset( p_chk, 0, sizeof( avi_chunk_t ) );
861 if( !p_root->common.p_first )
863 p_root->common.p_first = p_chk;
867 p_root->common.p_last->common.p_next = p_chk;
869 p_root->common.p_last = p_chk;
871 if( AVI_ChunkRead( s, p_chk, p_root ) ||
872 ( stream_Tell( s ) >=
873 (off_t)p_chk->common.p_father->common.i_chunk_pos +
874 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
878 /* If we can't seek then stop when we 've found first RIFF-AVI */
879 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
880 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
886 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
890 void AVI_ChunkFreeRoot( stream_t *s,
893 AVI_ChunkFree( s, p_chk );
897 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
900 avi_chunk_t *p_child;
908 p_child = p_chk->common.p_first;
911 if( p_child->common.i_chunk_fourcc == i_fourcc ||
912 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
913 p_child->list.i_type == i_fourcc ) )
917 p_child = p_child->common.p_next;
922 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
923 vlc_fourcc_t i_fourcc, int i_number )
925 avi_chunk_t *p_child;
930 p_child = p_chk->common.p_first;
934 if( p_child->common.i_chunk_fourcc == i_fourcc ||
935 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
936 p_child->list.i_type == i_fourcc ) )
946 p_child = p_child->common.p_next;