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 *****************************************************************************/
25 #include <vlc_demux.h>
26 #include <vlc_codecs.h> /* BITMAPINFOHEADER */
32 #define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
34 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
36 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
39 #define AVI_ChunkFree( a, b ) _AVI_ChunkFree( (a), (avi_chunk_t*)(b) )
40 void _AVI_ChunkFree( stream_t *, avi_chunk_t *p_chk );
42 /****************************************************************************
44 * Basics functions to manipulates chunks
46 ****************************************************************************/
47 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
52 memset( p_chk, 0, sizeof( avi_chunk_t ) );
54 if( ( i_peek = stream_Peek( s, &p_peek, 8 ) ) < 8 )
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 );
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;
69 msg_Dbg( (vlc_object_t*)s,
70 "found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
71 p_chk->common.i_chunk_fourcc,
72 (char*)&p_chk->common.i_chunk_fourcc,
73 p_chk->common.i_chunk_size,
74 p_chk->common.i_chunk_pos );
79 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
85 if( AVI_ChunkReadCommon( s, &chk ) )
92 if( p_chk->common.p_father )
94 if( p_chk->common.p_father->common.i_chunk_pos +
95 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
96 p_chk->common.i_chunk_pos +
97 __EVEN( p_chk->common.i_chunk_size ) + 8 )
102 return stream_Seek( s, p_chk->common.i_chunk_pos +
103 __EVEN( p_chk->common.i_chunk_size ) + 8 );
106 /****************************************************************************
108 * Functions to read chunks
110 ****************************************************************************/
111 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
115 vlc_bool_t b_seekable;
117 if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
120 msg_Warn( (vlc_object_t*)s, "empty list chunk" );
123 if( stream_Peek( s, &p_peek, 12 ) < 12 )
125 msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
129 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
131 p_container->list.i_type = GetFOURCC( p_peek + 8 );
133 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
134 p_container->list.i_type == AVIFOURCC_movi )
136 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
139 return AVI_NextChunk( s, p_container );
141 return VLC_SUCCESS; /* point at begining of LIST-movi */
144 if( stream_Read( s, NULL, 12 ) != 12 )
146 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
151 msg_Dbg( (vlc_object_t*)s,
152 "found LIST chunk: \'%4.4s\'",
153 (char*)&p_container->list.i_type );
155 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
158 p_chk = malloc( sizeof( avi_chunk_t ) );
159 memset( p_chk, 0, sizeof( avi_chunk_t ) );
160 if( !p_container->common.p_first )
162 p_container->common.p_first = p_chk;
166 p_container->common.p_last->common.p_next = p_chk;
168 p_container->common.p_last = p_chk;
170 if( AVI_ChunkRead( s, p_chk, p_container ) )
174 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
176 (off_t)p_chk->common.p_father->common.i_chunk_pos +
177 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
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 &&
185 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
191 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
196 #define AVI_READCHUNK_ENTER \
197 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
198 uint8_t *p_read, *p_buff; \
199 if( !( p_read = p_buff = malloc(i_read ) ) ) \
201 return VLC_EGENERIC; \
203 i_read = stream_Read( s, p_read, i_read ); \
204 if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
207 return VLC_EGENERIC; \
212 #define AVI_READ( res, func, size ) \
213 if( i_read < size ) { \
215 return VLC_EGENERIC; \
218 res = func( p_read ); \
221 #define AVI_READCHUNK_EXIT( code ) \
225 static inline uint8_t GetB( uint8_t *ptr )
230 #define AVI_READ1BYTE( i_byte ) \
231 AVI_READ( i_byte, GetB, 1 )
233 #define AVI_READ2BYTES( i_word ) \
234 AVI_READ( i_word, GetWLE, 2 )
236 #define AVI_READ4BYTES( i_dword ) \
237 AVI_READ( i_dword, GetDWLE, 4 )
239 #define AVI_READ8BYTES( i_qword ) \
240 AVI_READ( i_qword, GetQWLE, 8 )
242 #define AVI_READFOURCC( i_dword ) \
243 AVI_READ( i_dword, GetFOURCC, 4 )
245 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
249 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
250 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
251 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
252 AVI_READ4BYTES( p_chk->avih.i_flags );
253 AVI_READ4BYTES( p_chk->avih.i_totalframes );
254 AVI_READ4BYTES( p_chk->avih.i_initialframes );
255 AVI_READ4BYTES( p_chk->avih.i_streams );
256 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
257 AVI_READ4BYTES( p_chk->avih.i_width );
258 AVI_READ4BYTES( p_chk->avih.i_height );
259 AVI_READ4BYTES( p_chk->avih.i_scale );
260 AVI_READ4BYTES( p_chk->avih.i_rate );
261 AVI_READ4BYTES( p_chk->avih.i_start );
262 AVI_READ4BYTES( p_chk->avih.i_length );
264 msg_Dbg( (vlc_object_t*)s,
265 "avih: streams:%d flags:%s%s%s%s %dx%d",
266 p_chk->avih.i_streams,
267 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
268 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
269 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
270 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
271 p_chk->avih.i_width, p_chk->avih.i_height );
273 AVI_READCHUNK_EXIT( VLC_SUCCESS );
276 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
280 AVI_READFOURCC( p_chk->strh.i_type );
281 AVI_READFOURCC( p_chk->strh.i_handler );
282 AVI_READ4BYTES( p_chk->strh.i_flags );
283 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
284 AVI_READ4BYTES( p_chk->strh.i_initialframes );
285 AVI_READ4BYTES( p_chk->strh.i_scale );
286 AVI_READ4BYTES( p_chk->strh.i_rate );
287 AVI_READ4BYTES( p_chk->strh.i_start );
288 AVI_READ4BYTES( p_chk->strh.i_length );
289 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
290 AVI_READ4BYTES( p_chk->strh.i_quality );
291 AVI_READ4BYTES( p_chk->strh.i_samplesize );
293 msg_Dbg( (vlc_object_t*)s,
294 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
295 (char*)&p_chk->strh.i_type,
296 p_chk->strh.i_handler,
297 p_chk->strh.i_samplesize,
298 ( p_chk->strh.i_scale ?
299 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
302 AVI_READCHUNK_EXIT( VLC_SUCCESS );
305 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
310 if( p_chk->common.p_father == NULL )
312 msg_Err( (vlc_object_t*)s, "malformed avi file" );
313 AVI_READCHUNK_EXIT( VLC_EGENERIC );
315 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
317 msg_Err( (vlc_object_t*)s, "malformed avi file" );
318 AVI_READCHUNK_EXIT( VLC_EGENERIC );
321 switch( p_strh->strh.i_type )
323 case( AVIFOURCC_auds ):
324 p_chk->strf.auds.i_cat = AUDIO_ES;
325 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
326 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
327 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
328 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
329 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
330 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
331 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
332 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
333 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
335 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
336 /* prevent segfault */
337 if( p_chk->strf.auds.p_wf->cbSize >
338 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
340 p_chk->strf.auds.p_wf->cbSize =
341 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
343 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
345 /* Found an extensible header atm almost nothing uses that. */
346 msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
347 "vorbis audio dectected: not supported" );
352 p_chk->strf.auds.p_wf->cbSize = 0;
354 if( p_chk->strf.auds.p_wf->cbSize > 0 )
356 memcpy( &p_chk->strf.auds.p_wf[1] ,
357 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourrc+size */
358 p_chk->strf.auds.p_wf->cbSize );
361 msg_Dbg( (vlc_object_t*)s,
362 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
363 p_chk->strf.auds.p_wf->wFormatTag,
364 p_chk->strf.auds.p_wf->nChannels,
365 p_chk->strf.auds.p_wf->nSamplesPerSec,
366 p_chk->strf.auds.p_wf->wBitsPerSample,
367 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
370 case( AVIFOURCC_vids ):
371 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
372 p_chk->strf.vids.i_cat = VIDEO_ES;
373 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
374 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
375 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
376 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
377 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
378 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
379 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
380 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
381 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
382 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
383 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
384 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
385 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
387 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
389 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
391 memcpy( &p_chk->strf.vids.p_bih[1],
392 p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
393 p_chk->common.i_chunk_size -sizeof(BITMAPINFOHEADER) );
396 msg_Dbg( (vlc_object_t*)s,
397 "strf: video:%4.4s %dx%d planes:%d %dbpp",
398 (char*)&p_chk->strf.vids.p_bih->biCompression,
399 p_chk->strf.vids.p_bih->biWidth,
400 p_chk->strf.vids.p_bih->biHeight,
401 p_chk->strf.vids.p_bih->biPlanes,
402 p_chk->strf.vids.p_bih->biBitCount );
406 msg_Warn( (vlc_object_t*)s, "unknown stream type" );
407 p_chk->strf.common.i_cat = UNKNOWN_ES;
410 AVI_READCHUNK_EXIT( VLC_SUCCESS );
412 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
414 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
415 if( p_strf->common.i_cat == AUDIO_ES )
417 FREENULL( p_strf->auds.p_wf );
419 else if( p_strf->common.i_cat == VIDEO_ES )
421 FREENULL( p_strf->vids.p_bih );
425 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
428 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
429 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
430 AVI_READCHUNK_EXIT( VLC_SUCCESS );
433 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
435 if( p_chk->strd.p_data ) free( p_chk->strd.p_data );
438 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
440 unsigned int i_count, i_index;
444 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
446 p_chk->idx1.i_entry_count = i_count;
447 p_chk->idx1.i_entry_max = i_count;
450 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
452 for( i_index = 0; i_index < i_count ; i_index++ )
454 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
455 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
456 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
457 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
462 p_chk->idx1.entry = NULL;
465 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
467 AVI_READCHUNK_EXIT( VLC_SUCCESS );
470 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
472 p_chk->idx1.i_entry_count = 0;
473 p_chk->idx1.i_entry_max = 0;
474 FREENULL( p_chk->idx1.entry )
479 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
481 unsigned int i_count, i;
483 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
487 AVI_READ2BYTES( p_indx->i_longsperentry );
488 AVI_READ1BYTE ( p_indx->i_indexsubtype );
489 AVI_READ1BYTE ( p_indx->i_indextype );
490 AVI_READ4BYTES( p_indx->i_entriesinuse );
492 AVI_READ4BYTES( p_indx->i_id );
493 p_indx->idx.std = NULL;
494 p_indx->idx.field = NULL;
495 p_indx->idx.super = NULL;
497 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
499 AVI_READ8BYTES( p_indx->i_baseoffset );
500 AVI_READ4BYTES( i_dummy );
502 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
503 p_indx->i_entriesinuse = i_count;
504 p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
506 for( i = 0; i < i_count; i++ )
508 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
509 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
512 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
514 AVI_READ8BYTES( p_indx->i_baseoffset );
515 AVI_READ4BYTES( i_dummy );
517 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
518 p_indx->i_entriesinuse = i_count;
519 p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
520 for( i = 0; i < i_count; i++ )
522 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
523 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
524 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
527 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
529 p_indx->i_baseoffset = 0;
530 AVI_READ4BYTES( i_dummy );
531 AVI_READ4BYTES( i_dummy );
532 AVI_READ4BYTES( i_dummy );
534 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
535 p_indx->i_entriesinuse = i_count;
536 p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
538 for( i = 0; i < i_count; i++ )
540 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
541 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
542 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
547 msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
551 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 );
553 AVI_READCHUNK_EXIT( VLC_SUCCESS );
555 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
557 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
559 FREENULL( p_indx->idx.std );
560 FREENULL( p_indx->idx.field );
561 FREENULL( p_indx->idx.super );
568 vlc_fourcc_t i_fourcc;
569 const char *psz_type;
572 { AVIFOURCC_IARL, "archive location" },
573 { AVIFOURCC_IART, "artist" },
574 { AVIFOURCC_ICMS, "commisioned" },
575 { AVIFOURCC_ICMT, "comments" },
576 { AVIFOURCC_ICOP, "copyright" },
577 { AVIFOURCC_ICRD, "creation date" },
578 { AVIFOURCC_ICRP, "cropped" },
579 { AVIFOURCC_IDIM, "dimensions" },
580 { AVIFOURCC_IDPI, "dots per inch" },
581 { AVIFOURCC_IENG, "engineer" },
582 { AVIFOURCC_IGNR, "genre" },
583 { AVIFOURCC_IKEY, "keywords" },
584 { AVIFOURCC_ILGT, "lightness" },
585 { AVIFOURCC_IMED, "medium" },
586 { AVIFOURCC_INAM, "name" },
587 { AVIFOURCC_IPLT, "palette setting" },
588 { AVIFOURCC_IPRD, "product" },
589 { AVIFOURCC_ISBJ, "subject" },
590 { AVIFOURCC_ISFT, "software" },
591 { AVIFOURCC_ISHP, "sharpness" },
592 { AVIFOURCC_ISRC, "source" },
593 { AVIFOURCC_ISRF, "source form" },
594 { AVIFOURCC_ITCH, "technician" },
595 { AVIFOURCC_ISMP, "time code" },
596 { AVIFOURCC_IDIT, "digitalization time" },
597 { AVIFOURCC_strn, "stream name" },
600 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
603 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
606 for( i_index = 0;; i_index++)
608 if( !AVI_strz_type[i_index].i_fourcc ||
609 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
614 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
615 p_strz->p_str = malloc( i_read + 1);
617 if( p_strz->i_chunk_size )
619 memcpy( p_strz->p_str, p_read, i_read );
621 p_strz->p_str[i_read] = 0;
624 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
625 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
627 AVI_READCHUNK_EXIT( VLC_SUCCESS );
629 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
631 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
632 FREENULL( p_strz->p_type );
633 FREENULL( p_strz->p_str );
636 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
638 return AVI_NextChunk( s, p_chk );
640 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
647 vlc_fourcc_t i_fourcc;
648 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
649 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
650 } AVI_Chunk_Function [] =
652 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
653 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
654 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
655 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
656 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
657 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
658 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
659 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
660 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
662 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
663 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
664 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
665 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
666 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
667 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
668 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
669 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
670 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
671 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
672 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
673 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
674 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
675 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
676 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
677 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
678 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
679 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
680 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
681 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
682 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
683 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
684 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
685 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
686 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
687 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
688 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
692 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
694 unsigned int i_index;
695 for( i_index = 0; ; i_index++ )
697 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
698 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
705 int _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
714 if( AVI_ChunkReadCommon( s, p_chk ) )
716 msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
719 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
721 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
724 p_chk->common.p_father = p_father;
726 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
727 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
729 return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
731 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
732 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
733 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
734 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
736 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
737 return AVI_ChunkRead_indx( s, p_chk );
739 msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
740 return AVI_NextChunk( s, p_chk );
743 void _AVI_ChunkFree( stream_t *s,
747 avi_chunk_t *p_child, *p_next;
754 /* Free all child chunk */
755 p_child = p_chk->common.p_first;
758 p_next = p_child->common.p_next;
759 AVI_ChunkFree( s, p_child );
764 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
765 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
768 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
769 (char*)&p_chk->common.i_chunk_fourcc );
771 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
775 msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
777 p_chk->common.p_first = NULL;
778 p_chk->common.p_last = NULL;
783 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
784 avi_chunk_t *p_chk, int i_level )
788 avi_chunk_t *p_child;
790 memset( str, ' ', sizeof( str ) );
791 for( i = 1; i < i_level; i++ )
795 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
796 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
798 sprintf( str + i_level * 5,
799 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
801 (char*)&p_chk->common.i_chunk_fourcc,
802 (char*)&p_chk->list.i_type,
803 p_chk->common.i_chunk_size,
804 p_chk->common.i_chunk_pos );
808 sprintf( str + i_level * 5,
809 "+ %4.4s size:"I64Fu" pos:"I64Fu,
810 (char*)&p_chk->common.i_chunk_fourcc,
811 p_chk->common.i_chunk_size,
812 p_chk->common.i_chunk_pos );
814 msg_Dbg( p_obj, "%s", str );
816 p_child = p_chk->common.p_first;
819 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
820 p_child = p_child->common.p_next;
824 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
826 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
828 vlc_bool_t b_seekable;
830 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
832 p_list->i_chunk_pos = 0;
833 p_list->i_chunk_size = stream_Size( s );
834 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
835 p_list->p_father = NULL;
836 p_list->p_next = NULL;
837 p_list->p_first = NULL;
838 p_list->p_last = NULL;
840 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
844 p_chk = malloc( sizeof( avi_chunk_t ) );
845 memset( p_chk, 0, sizeof( avi_chunk_t ) );
846 if( !p_root->common.p_first )
848 p_root->common.p_first = p_chk;
852 p_root->common.p_last->common.p_next = p_chk;
854 p_root->common.p_last = p_chk;
856 if( AVI_ChunkRead( s, p_chk, p_root ) ||
857 ( stream_Tell( s ) >=
858 (off_t)p_chk->common.p_father->common.i_chunk_pos +
859 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
863 /* If we can't seek then stop when we 've found first RIFF-AVI */
864 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
865 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
871 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
875 void AVI_ChunkFreeRoot( stream_t *s,
878 AVI_ChunkFree( s, p_chk );
882 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
885 avi_chunk_t *p_child;
893 p_child = p_chk->common.p_first;
896 if( p_child->common.i_chunk_fourcc == i_fourcc ||
897 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
898 p_child->list.i_type == i_fourcc ) )
902 p_child = p_child->common.p_next;
907 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
908 vlc_fourcc_t i_fourcc, int i_number )
910 avi_chunk_t *p_child;
915 p_child = p_chk->common.p_first;
919 if( p_child->common.i_chunk_fourcc == i_fourcc ||
920 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
921 p_child->list.i_type == i_fourcc ) )
931 p_child = p_child->common.p_next;