1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VLC authors and VideoLAN
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
28 #include <vlc_common.h>
29 #include <vlc_demux.h> /* stream_*, *_ES */
30 #include <vlc_codecs.h> /* VLC_BITMAPINFOHEADER */
38 #define __EVEN( x ) (((x) + 1) & ~1)
40 static vlc_fourcc_t GetFOURCC( const uint8_t *p_buff )
42 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
45 /****************************************************************************
47 * Basics functions to manipulates chunks
49 ****************************************************************************/
50 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
52 const uint8_t *p_peek;
54 memset( p_chk, 0, sizeof( avi_chunk_t ) );
56 if( 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: %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 );
78 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
84 if( AVI_ChunkReadCommon( s, &chk ) )
91 if( p_chk->common.p_father )
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 )
101 return stream_Seek( s, p_chk->common.i_chunk_pos +
102 __EVEN( p_chk->common.i_chunk_size ) + 8 );
105 /****************************************************************************
107 * Functions to read chunks
109 ****************************************************************************/
110 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
113 const uint8_t *p_peek;
115 int i_ret = VLC_SUCCESS;
117 if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 4 )
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 /* XXX fixed for on2 hack */
134 if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
136 p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
137 p_container->list.i_type = AVIFOURCC_AVI;
140 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
141 p_container->list.i_type == AVIFOURCC_movi )
143 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
146 return AVI_NextChunk( s, p_container );
148 return VLC_SUCCESS; /* point at begining of LIST-movi */
151 if( stream_Read( s, NULL, 12 ) != 12 )
153 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
158 msg_Dbg( (vlc_object_t*)s,
159 "found LIST chunk: \'%4.4s\'",
160 (char*)&p_container->list.i_type );
162 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
165 p_chk = xmalloc( sizeof( avi_chunk_t ) );
166 memset( p_chk, 0, sizeof( avi_chunk_t ) );
167 if( !p_container->common.p_first )
169 p_container->common.p_first = p_chk;
173 p_container->common.p_last->common.p_next = p_chk;
175 p_container->common.p_last = p_chk;
177 if( i_ret = AVI_ChunkRead( s, p_chk, p_container ) )
181 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
183 (off_t)p_chk->common.p_father->common.i_chunk_pos +
184 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
189 /* If we can't seek then stop when we 've found LIST-movi */
190 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
191 p_chk->list.i_type == AVIFOURCC_movi &&
192 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
198 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
200 if ( i_ret == AVI_ZERO_FOURCC ) return i_ret;
204 #define AVI_READCHUNK_ENTER \
205 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
206 if( i_read > 100000000 ) \
208 msg_Err( s, "Big chunk ignored" ); \
209 return VLC_EGENERIC; \
211 uint8_t *p_read, *p_buff; \
212 if( !( p_read = p_buff = malloc(i_read ) ) ) \
214 return VLC_EGENERIC; \
216 i_read = stream_Read( s, p_read, i_read ); \
217 if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
220 return VLC_EGENERIC; \
225 #define AVI_READ( res, func, size ) \
226 if( i_read < size ) { \
228 return VLC_EGENERIC; \
231 res = func( p_read ); \
234 #define AVI_READCHUNK_EXIT( code ) \
238 static inline uint8_t GetB( uint8_t *ptr )
243 #define AVI_READ1BYTE( i_byte ) \
244 AVI_READ( i_byte, GetB, 1 )
246 #define AVI_READ2BYTES( i_word ) \
247 AVI_READ( i_word, GetWLE, 2 )
249 #define AVI_READ4BYTES( i_dword ) \
250 AVI_READ( i_dword, GetDWLE, 4 )
252 #define AVI_READ8BYTES( i_qword ) \
253 AVI_READ( i_qword, GetQWLE, 8 )
255 #define AVI_READFOURCC( i_dword ) \
256 AVI_READ( i_dword, GetFOURCC, 4 )
258 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
262 p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
263 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
264 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
265 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
266 AVI_READ4BYTES( p_chk->avih.i_flags );
267 AVI_READ4BYTES( p_chk->avih.i_totalframes );
268 AVI_READ4BYTES( p_chk->avih.i_initialframes );
269 AVI_READ4BYTES( p_chk->avih.i_streams );
270 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
271 AVI_READ4BYTES( p_chk->avih.i_width );
272 AVI_READ4BYTES( p_chk->avih.i_height );
273 AVI_READ4BYTES( p_chk->avih.i_scale );
274 AVI_READ4BYTES( p_chk->avih.i_rate );
275 AVI_READ4BYTES( p_chk->avih.i_start );
276 AVI_READ4BYTES( p_chk->avih.i_length );
278 msg_Dbg( (vlc_object_t*)s,
279 "avih: streams:%d flags:%s%s%s%s %dx%d",
280 p_chk->avih.i_streams,
281 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
282 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
283 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
284 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
285 p_chk->avih.i_width, p_chk->avih.i_height );
287 AVI_READCHUNK_EXIT( VLC_SUCCESS );
290 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
294 AVI_READFOURCC( p_chk->strh.i_type );
295 AVI_READFOURCC( p_chk->strh.i_handler );
296 AVI_READ4BYTES( p_chk->strh.i_flags );
297 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
298 AVI_READ4BYTES( p_chk->strh.i_initialframes );
299 AVI_READ4BYTES( p_chk->strh.i_scale );
300 AVI_READ4BYTES( p_chk->strh.i_rate );
301 AVI_READ4BYTES( p_chk->strh.i_start );
302 AVI_READ4BYTES( p_chk->strh.i_length );
303 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
304 AVI_READ4BYTES( p_chk->strh.i_quality );
305 AVI_READ4BYTES( p_chk->strh.i_samplesize );
307 msg_Dbg( (vlc_object_t*)s,
308 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
309 (char*)&p_chk->strh.i_type,
310 p_chk->strh.i_handler,
311 p_chk->strh.i_samplesize,
312 ( p_chk->strh.i_scale ?
313 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
316 AVI_READCHUNK_EXIT( VLC_SUCCESS );
319 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
324 if( p_chk->common.p_father == NULL )
326 msg_Err( (vlc_object_t*)s, "malformed avi file" );
327 AVI_READCHUNK_EXIT( VLC_EGENERIC );
329 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
331 msg_Err( (vlc_object_t*)s, "malformed avi file" );
332 AVI_READCHUNK_EXIT( VLC_EGENERIC );
335 switch( p_strh->strh.i_type )
337 case( AVIFOURCC_auds ):
338 p_chk->strf.auds.i_cat = AUDIO_ES;
339 p_chk->strf.auds.p_wf = xmalloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
340 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
341 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
342 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
343 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
344 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
345 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
347 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
348 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
350 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
352 /* prevent segfault */
353 if( p_chk->strf.auds.p_wf->cbSize >
354 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
356 p_chk->strf.auds.p_wf->cbSize =
357 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
360 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
362 msg_Dbg( s, "Extended header found" );
367 p_chk->strf.auds.p_wf->cbSize = 0;
369 if( p_chk->strf.auds.p_wf->cbSize > 0 )
371 memcpy( &p_chk->strf.auds.p_wf[1] ,
372 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourcc+size */
373 p_chk->strf.auds.p_wf->cbSize );
376 msg_Dbg( (vlc_object_t*)s,
377 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
378 p_chk->strf.auds.p_wf->wFormatTag,
379 p_chk->strf.auds.p_wf->nChannels,
380 p_chk->strf.auds.p_wf->nSamplesPerSec,
381 p_chk->strf.auds.p_wf->wBitsPerSample,
382 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
385 case( AVIFOURCC_vids ):
386 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
387 p_chk->strf.vids.i_cat = VIDEO_ES;
388 p_chk->strf.vids.p_bih = xmalloc( __MAX( p_chk->common.i_chunk_size,
389 sizeof( *p_chk->strf.vids.p_bih ) ) );
390 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
391 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
392 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
393 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
394 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
395 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
396 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
397 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
398 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
399 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
400 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
401 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
403 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
405 if( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
407 memcpy( &p_chk->strf.vids.p_bih[1],
408 p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
409 p_chk->common.i_chunk_size -sizeof(VLC_BITMAPINFOHEADER) );
412 msg_Dbg( (vlc_object_t*)s,
413 "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
414 (char*)&p_chk->strf.vids.p_bih->biCompression,
415 (uint32_t)p_chk->strf.vids.p_bih->biWidth,
416 (uint32_t)p_chk->strf.vids.p_bih->biHeight,
417 p_chk->strf.vids.p_bih->biPlanes,
418 p_chk->strf.vids.p_bih->biBitCount );
423 p_chk->strf.common.i_cat = UNKNOWN_ES;
425 case( AVIFOURCC_txts ):
426 p_chk->strf.common.i_cat = SPU_ES;
429 msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
430 (char*)&p_strh->strh.i_type );
431 p_chk->strf.common.i_cat = UNKNOWN_ES;
434 AVI_READCHUNK_EXIT( VLC_SUCCESS );
436 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
438 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
439 if( p_strf->common.i_cat == AUDIO_ES )
441 FREENULL( p_strf->auds.p_wf );
443 else if( p_strf->common.i_cat == VIDEO_ES )
445 FREENULL( p_strf->vids.p_bih );
449 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
451 if ( p_chk->common.i_chunk_size == 0 )
453 msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
454 return AVI_STRD_ZERO_CHUNK;
458 p_chk->strd.p_data = xmalloc( p_chk->common.i_chunk_size );
459 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
460 AVI_READCHUNK_EXIT( VLC_SUCCESS );
463 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
465 free( p_chk->strd.p_data );
468 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
470 unsigned int i_count, i_index;
474 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
476 p_chk->idx1.i_entry_count = i_count;
477 p_chk->idx1.i_entry_max = i_count;
480 p_chk->idx1.entry = xcalloc( i_count, sizeof( idx1_entry_t ) );
482 for( i_index = 0; i_index < i_count ; i_index++ )
484 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
485 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
486 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
487 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
492 p_chk->idx1.entry = NULL;
495 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
497 AVI_READCHUNK_EXIT( VLC_SUCCESS );
500 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
502 p_chk->idx1.i_entry_count = 0;
503 p_chk->idx1.i_entry_max = 0;
504 FREENULL( p_chk->idx1.entry );
509 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
511 unsigned int i_count, i;
513 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
517 AVI_READ2BYTES( p_indx->i_longsperentry );
518 AVI_READ1BYTE ( p_indx->i_indexsubtype );
519 AVI_READ1BYTE ( p_indx->i_indextype );
520 AVI_READ4BYTES( p_indx->i_entriesinuse );
522 AVI_READ4BYTES( p_indx->i_id );
523 p_indx->idx.std = NULL;
524 p_indx->idx.field = NULL;
525 p_indx->idx.super = NULL;
527 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
529 AVI_READ8BYTES( p_indx->i_baseoffset );
530 AVI_READ4BYTES( i_dummy );
532 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
533 p_indx->i_entriesinuse = i_count;
534 p_indx->idx.std = xcalloc( i_count, sizeof( indx_std_entry_t ) );
536 for( i = 0; i < i_count; i++ )
538 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
539 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
542 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
544 AVI_READ8BYTES( p_indx->i_baseoffset );
545 AVI_READ4BYTES( i_dummy );
547 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
548 p_indx->i_entriesinuse = i_count;
549 p_indx->idx.field = xcalloc( i_count, sizeof( indx_field_entry_t ) );
550 for( i = 0; i < i_count; i++ )
552 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
553 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
554 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
557 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
559 p_indx->i_baseoffset = 0;
560 AVI_READ4BYTES( i_dummy );
561 AVI_READ4BYTES( i_dummy );
562 AVI_READ4BYTES( i_dummy );
564 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
565 p_indx->i_entriesinuse = i_count;
566 p_indx->idx.super = xcalloc( i_count, sizeof( indx_super_entry_t ) );
568 for( i = 0; i < i_count; i++ )
570 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
571 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
572 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
577 msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
581 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 );
583 AVI_READCHUNK_EXIT( VLC_SUCCESS );
585 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
587 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
589 FREENULL( p_indx->idx.std );
590 FREENULL( p_indx->idx.field );
591 FREENULL( p_indx->idx.super );
594 static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
596 avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;
600 AVI_READ4BYTES( p_vprp->i_video_format_token );
601 AVI_READ4BYTES( p_vprp->i_video_standard );
602 AVI_READ4BYTES( p_vprp->i_vertical_refresh );
603 AVI_READ4BYTES( p_vprp->i_h_total_in_t );
604 AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
605 AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
606 AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
607 AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
608 AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
609 for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
611 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
612 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
613 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
614 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
615 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
616 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
617 AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
618 AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
622 msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
623 p_vprp->i_video_format_token, p_vprp->i_video_standard );
625 AVI_READCHUNK_EXIT( VLC_SUCCESS );
628 static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
630 avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
634 AVI_READ4BYTES( p_dmlh->dwTotalFrames );
637 msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
638 p_dmlh->dwTotalFrames );
640 AVI_READCHUNK_EXIT( VLC_SUCCESS );
645 vlc_fourcc_t i_fourcc;
646 const char *psz_type;
649 { AVIFOURCC_IARL, "Archive location" },
650 { AVIFOURCC_IART, "Artist" },
651 { AVIFOURCC_ICMS, "Commisioned" },
652 { AVIFOURCC_ICMT, "Comments" },
653 { AVIFOURCC_ICOP, "Copyright" },
654 { AVIFOURCC_ICRD, "Creation date" },
655 { AVIFOURCC_ICRP, "Cropped" },
656 { AVIFOURCC_IDIM, "Dimensions" },
657 { AVIFOURCC_IDPI, "Dots per inch" },
658 { AVIFOURCC_IENG, "Engineer" },
659 { AVIFOURCC_IGNR, "Genre" },
660 { AVIFOURCC_ISGN, "Secondary Genre" },
661 { AVIFOURCC_IKEY, "Keywords" },
662 { AVIFOURCC_ILGT, "Lightness" },
663 { AVIFOURCC_IMED, "Medium" },
664 { AVIFOURCC_INAM, "Name" },
665 { AVIFOURCC_IPLT, "Palette setting" },
666 { AVIFOURCC_IPRD, "Product" },
667 { AVIFOURCC_ISBJ, "Subject" },
668 { AVIFOURCC_ISFT, "Software" },
669 { AVIFOURCC_ISHP, "Sharpness" },
670 { AVIFOURCC_ISRC, "Source" },
671 { AVIFOURCC_ISRF, "Source form" },
672 { AVIFOURCC_ITCH, "Technician" },
673 { AVIFOURCC_ISMP, "Time code" },
674 { AVIFOURCC_IDIT, "Digitalization time" },
675 { AVIFOURCC_IWRI, "Writer" },
676 { AVIFOURCC_IPRO, "Producer" },
677 { AVIFOURCC_ICNM, "Cinematographer" },
678 { AVIFOURCC_IPDS, "Production designer" },
679 { AVIFOURCC_IEDT, "Editor" },
680 { AVIFOURCC_ICDS, "Costume designer" },
681 { AVIFOURCC_IMUS, "Music" },
682 { AVIFOURCC_ISTD, "Production studio" },
683 { AVIFOURCC_IDST, "Distributor" },
684 { AVIFOURCC_ICNT, "Country" },
685 { AVIFOURCC_ISTR, "Starring" },
686 { AVIFOURCC_IFRM, "Total number of parts" },
687 { AVIFOURCC_strn, "Stream name" },
690 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
693 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
696 for( i_index = 0;; i_index++)
698 if( !AVI_strz_type[i_index].i_fourcc ||
699 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
704 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
705 p_strz->p_str = xmalloc( p_strz->i_chunk_size + 1);
707 if( p_strz->i_chunk_size )
709 memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
711 p_strz->p_str[p_strz->i_chunk_size] = 0;
714 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
715 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
717 AVI_READCHUNK_EXIT( VLC_SUCCESS );
719 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
721 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
722 FREENULL( p_strz->p_type );
723 FREENULL( p_strz->p_str );
726 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
728 return AVI_NextChunk( s, p_chk );
730 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
737 vlc_fourcc_t i_fourcc;
738 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
739 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
740 } AVI_Chunk_Function [] =
742 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
743 { AVIFOURCC_ON2, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
744 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
745 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
746 { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
747 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
748 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
749 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
750 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
751 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
752 { AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
753 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
754 { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
756 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
757 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
758 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
759 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
760 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
761 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
762 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
763 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
764 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
765 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
766 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
767 { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
768 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
769 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
770 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
771 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
772 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
773 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
774 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
775 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
776 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
777 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
778 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
779 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
780 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
781 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
782 { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
783 { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
784 { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
785 { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
786 { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
787 { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
788 { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
789 { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
790 { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
791 { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
792 { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
793 { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
794 { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
795 { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
796 { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
797 { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
800 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
804 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
806 unsigned int i_index;
807 for( i_index = 0; ; i_index++ )
809 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
810 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
817 int AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
823 msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
827 if( AVI_ChunkReadCommon( s, p_chk ) )
829 msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
833 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
835 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
836 return AVI_ZERO_FOURCC;
838 p_chk->common.p_father = p_father;
840 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
841 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
843 int i_return = AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
844 if ( i_return == AVI_STRD_ZERO_CHUNK || i_return == AVI_ZERO_FOURCC )
846 if ( !p_father ) return VLC_EGENERIC;
847 return AVI_NextChunk( s, p_father );
851 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
852 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
853 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
854 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
856 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
857 return AVI_ChunkRead_indx( s, p_chk );
860 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
861 (char*)&p_chk->common.i_chunk_fourcc );
862 return AVI_NextChunk( s, p_chk );
865 void AVI_ChunkFree( stream_t *s,
869 avi_chunk_t *p_child, *p_next;
876 /* Free all child chunk */
877 p_child = p_chk->common.p_first;
880 p_next = p_child->common.p_next;
881 AVI_ChunkFree( s, p_child );
886 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
887 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
890 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
891 (char*)&p_chk->common.i_chunk_fourcc );
893 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
897 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
898 (char*)&p_chk->common.i_chunk_fourcc );
900 p_chk->common.p_first = NULL;
901 p_chk->common.p_last = NULL;
906 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
907 avi_chunk_t *p_chk, unsigned i_level )
909 avi_chunk_t *p_child;
912 if( i_level >= (sizeof(str) - 1)/4 )
915 memset( str, ' ', sizeof( str ) );
916 for( unsigned i = 1; i < i_level; i++ )
920 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
921 p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
922 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
924 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
925 "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
927 (char*)&p_chk->common.i_chunk_fourcc,
928 (char*)&p_chk->list.i_type,
929 p_chk->common.i_chunk_size,
930 p_chk->common.i_chunk_pos );
934 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
935 "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
936 (char*)&p_chk->common.i_chunk_fourcc,
937 p_chk->common.i_chunk_size,
938 p_chk->common.i_chunk_pos );
940 msg_Dbg( p_obj, "%s", str );
942 p_child = p_chk->common.p_first;
945 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
946 p_child = p_child->common.p_next;
950 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
952 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
956 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
958 p_list->i_chunk_pos = 0;
959 p_list->i_chunk_size = stream_Size( s );
960 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
961 p_list->p_father = NULL;
962 p_list->p_next = NULL;
963 p_list->p_first = NULL;
964 p_list->p_last = NULL;
966 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
970 p_chk = xmalloc( sizeof( avi_chunk_t ) );
971 memset( p_chk, 0, sizeof( avi_chunk_t ) );
972 if( !p_root->common.p_first )
974 p_root->common.p_first = p_chk;
978 p_root->common.p_last->common.p_next = p_chk;
980 p_root->common.p_last = p_chk;
982 if( AVI_ChunkRead( s, p_chk, p_root ) ||
983 ( stream_Tell( s ) >=
984 (off_t)p_chk->common.p_father->common.i_chunk_pos +
985 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
989 /* If we can't seek then stop when we 've found first RIFF-AVI */
990 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
991 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
997 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
1001 void AVI_ChunkFreeRoot( stream_t *s,
1002 avi_chunk_t *p_chk )
1004 AVI_ChunkFree( s, p_chk );
1008 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
1011 avi_chunk_t *p_child;
1019 p_child = p_chk->common.p_first;
1022 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1023 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1024 p_child->list.i_type == i_fourcc ) )
1028 p_child = p_child->common.p_next;
1033 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
1034 vlc_fourcc_t i_fourcc, int i_number )
1036 avi_chunk_t *p_child;
1041 p_child = p_chk->common.p_first;
1045 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1046 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1047 p_child->list.i_type == i_fourcc ) )
1057 p_child = p_child->common.p_next;