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> /* 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;
116 if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 4 )
119 msg_Warn( (vlc_object_t*)s, "empty list chunk" );
122 if( stream_Peek( s, &p_peek, 12 ) < 12 )
124 msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
128 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
130 p_container->list.i_type = GetFOURCC( p_peek + 8 );
132 /* XXX fixed for on2 hack */
133 if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
135 p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
136 p_container->list.i_type = AVIFOURCC_AVI;
139 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
140 p_container->list.i_type == AVIFOURCC_movi )
142 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
145 return AVI_NextChunk( s, p_container );
147 return VLC_SUCCESS; /* point at begining of LIST-movi */
150 if( stream_Read( s, NULL, 12 ) != 12 )
152 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
157 msg_Dbg( (vlc_object_t*)s,
158 "found LIST chunk: \'%4.4s\'",
159 (char*)&p_container->list.i_type );
161 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
164 p_chk = xmalloc( sizeof( avi_chunk_t ) );
165 memset( p_chk, 0, sizeof( avi_chunk_t ) );
166 if( !p_container->common.p_first )
168 p_container->common.p_first = p_chk;
172 p_container->common.p_last->common.p_next = p_chk;
174 p_container->common.p_last = p_chk;
176 if( AVI_ChunkRead( s, p_chk, p_container ) )
180 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
182 (off_t)p_chk->common.p_father->common.i_chunk_pos +
183 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
188 /* If we can't seek then stop when we 've found LIST-movi */
189 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
190 p_chk->list.i_type == AVIFOURCC_movi &&
191 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
197 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
202 #define AVI_READCHUNK_ENTER \
203 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
204 if( i_read > 100000000 ) \
206 msg_Err( s, "Big chunk ignored" ); \
207 return VLC_EGENERIC; \
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 = xmalloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
338 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
339 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
340 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
341 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
342 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
343 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
345 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
346 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
348 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
350 /* prevent segfault */
351 if( p_chk->strf.auds.p_wf->cbSize >
352 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
354 p_chk->strf.auds.p_wf->cbSize =
355 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
358 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
360 msg_Dbg( s, "Extended header found" );
365 p_chk->strf.auds.p_wf->cbSize = 0;
367 if( p_chk->strf.auds.p_wf->cbSize > 0 )
369 memcpy( &p_chk->strf.auds.p_wf[1] ,
370 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourcc+size */
371 p_chk->strf.auds.p_wf->cbSize );
374 msg_Dbg( (vlc_object_t*)s,
375 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
376 p_chk->strf.auds.p_wf->wFormatTag,
377 p_chk->strf.auds.p_wf->nChannels,
378 p_chk->strf.auds.p_wf->nSamplesPerSec,
379 p_chk->strf.auds.p_wf->wBitsPerSample,
380 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
383 case( AVIFOURCC_vids ):
384 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
385 p_chk->strf.vids.i_cat = VIDEO_ES;
386 p_chk->strf.vids.p_bih = xmalloc( __MAX( p_chk->common.i_chunk_size,
387 sizeof( *p_chk->strf.vids.p_bih ) ) );
388 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
389 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
390 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
391 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
392 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
393 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
394 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
395 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
396 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
397 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
398 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
399 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
401 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
403 if( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
405 memcpy( &p_chk->strf.vids.p_bih[1],
406 p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
407 p_chk->common.i_chunk_size -sizeof(VLC_BITMAPINFOHEADER) );
410 msg_Dbg( (vlc_object_t*)s,
411 "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
412 (char*)&p_chk->strf.vids.p_bih->biCompression,
413 (uint32_t)p_chk->strf.vids.p_bih->biWidth,
414 (uint32_t)p_chk->strf.vids.p_bih->biHeight,
415 p_chk->strf.vids.p_bih->biPlanes,
416 p_chk->strf.vids.p_bih->biBitCount );
419 case( AVIFOURCC_txts ):
420 p_chk->strf.common.i_cat = SPU_ES;
423 msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
424 (char*)&p_strh->strh.i_type );
425 p_chk->strf.common.i_cat = UNKNOWN_ES;
428 AVI_READCHUNK_EXIT( VLC_SUCCESS );
430 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
432 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
433 if( p_strf->common.i_cat == AUDIO_ES )
435 FREENULL( p_strf->auds.p_wf );
437 else if( p_strf->common.i_cat == VIDEO_ES )
439 FREENULL( p_strf->vids.p_bih );
443 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
446 p_chk->strd.p_data = xmalloc( p_chk->common.i_chunk_size );
447 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
448 AVI_READCHUNK_EXIT( VLC_SUCCESS );
451 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
453 free( p_chk->strd.p_data );
456 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
458 unsigned int i_count, i_index;
462 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
464 p_chk->idx1.i_entry_count = i_count;
465 p_chk->idx1.i_entry_max = i_count;
468 p_chk->idx1.entry = xcalloc( i_count, sizeof( idx1_entry_t ) );
470 for( i_index = 0; i_index < i_count ; i_index++ )
472 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
473 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
474 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
475 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
480 p_chk->idx1.entry = NULL;
483 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
485 AVI_READCHUNK_EXIT( VLC_SUCCESS );
488 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
490 p_chk->idx1.i_entry_count = 0;
491 p_chk->idx1.i_entry_max = 0;
492 FREENULL( p_chk->idx1.entry );
497 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
499 unsigned int i_count, i;
501 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
505 AVI_READ2BYTES( p_indx->i_longsperentry );
506 AVI_READ1BYTE ( p_indx->i_indexsubtype );
507 AVI_READ1BYTE ( p_indx->i_indextype );
508 AVI_READ4BYTES( p_indx->i_entriesinuse );
510 AVI_READ4BYTES( p_indx->i_id );
511 p_indx->idx.std = NULL;
512 p_indx->idx.field = NULL;
513 p_indx->idx.super = NULL;
515 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
517 AVI_READ8BYTES( p_indx->i_baseoffset );
518 AVI_READ4BYTES( i_dummy );
520 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
521 p_indx->i_entriesinuse = i_count;
522 p_indx->idx.std = xcalloc( i_count, sizeof( indx_std_entry_t ) );
524 for( i = 0; i < i_count; i++ )
526 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
527 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
530 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
532 AVI_READ8BYTES( p_indx->i_baseoffset );
533 AVI_READ4BYTES( i_dummy );
535 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
536 p_indx->i_entriesinuse = i_count;
537 p_indx->idx.field = xcalloc( i_count, sizeof( indx_field_entry_t ) );
538 for( i = 0; i < i_count; i++ )
540 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
541 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
542 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
545 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
547 p_indx->i_baseoffset = 0;
548 AVI_READ4BYTES( i_dummy );
549 AVI_READ4BYTES( i_dummy );
550 AVI_READ4BYTES( i_dummy );
552 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
553 p_indx->i_entriesinuse = i_count;
554 p_indx->idx.super = xcalloc( i_count, sizeof( indx_super_entry_t ) );
556 for( i = 0; i < i_count; i++ )
558 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
559 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
560 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
565 msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
569 msg_Dbg( (vlc_object_t*)s, "indx: type=%d subtype=%d entry=%d", p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
571 AVI_READCHUNK_EXIT( VLC_SUCCESS );
573 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
575 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
577 FREENULL( p_indx->idx.std );
578 FREENULL( p_indx->idx.field );
579 FREENULL( p_indx->idx.super );
582 static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
584 avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;
588 AVI_READ4BYTES( p_vprp->i_video_format_token );
589 AVI_READ4BYTES( p_vprp->i_video_standard );
590 AVI_READ4BYTES( p_vprp->i_vertical_refresh );
591 AVI_READ4BYTES( p_vprp->i_h_total_in_t );
592 AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
593 AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
594 AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
595 AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
596 AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
597 for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
599 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
600 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
601 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
602 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
603 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
604 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
605 AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
606 AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
610 msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
611 p_vprp->i_video_format_token, p_vprp->i_video_standard );
613 AVI_READCHUNK_EXIT( VLC_SUCCESS );
616 static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
618 avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
622 AVI_READ4BYTES( p_dmlh->dwTotalFrames );
625 msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
626 p_dmlh->dwTotalFrames );
628 AVI_READCHUNK_EXIT( VLC_SUCCESS );
633 vlc_fourcc_t i_fourcc;
634 const char *psz_type;
637 { AVIFOURCC_IARL, "Archive location" },
638 { AVIFOURCC_IART, "Artist" },
639 { AVIFOURCC_ICMS, "Commisioned" },
640 { AVIFOURCC_ICMT, "Comments" },
641 { AVIFOURCC_ICOP, "Copyright" },
642 { AVIFOURCC_ICRD, "Creation date" },
643 { AVIFOURCC_ICRP, "Cropped" },
644 { AVIFOURCC_IDIM, "Dimensions" },
645 { AVIFOURCC_IDPI, "Dots per inch" },
646 { AVIFOURCC_IENG, "Engineer" },
647 { AVIFOURCC_IGNR, "Genre" },
648 { AVIFOURCC_ISGN, "Secondary Genre" },
649 { AVIFOURCC_IKEY, "Keywords" },
650 { AVIFOURCC_ILGT, "Lightness" },
651 { AVIFOURCC_IMED, "Medium" },
652 { AVIFOURCC_INAM, "Name" },
653 { AVIFOURCC_IPLT, "Palette setting" },
654 { AVIFOURCC_IPRD, "Product" },
655 { AVIFOURCC_ISBJ, "Subject" },
656 { AVIFOURCC_ISFT, "Software" },
657 { AVIFOURCC_ISHP, "Sharpness" },
658 { AVIFOURCC_ISRC, "Source" },
659 { AVIFOURCC_ISRF, "Source form" },
660 { AVIFOURCC_ITCH, "Technician" },
661 { AVIFOURCC_ISMP, "Time code" },
662 { AVIFOURCC_IDIT, "Digitalization time" },
663 { AVIFOURCC_IWRI, "Writer" },
664 { AVIFOURCC_IPRO, "Producer" },
665 { AVIFOURCC_ICNM, "Cinematographer" },
666 { AVIFOURCC_IPDS, "Production designer" },
667 { AVIFOURCC_IEDT, "Editor" },
668 { AVIFOURCC_ICDS, "Costume designer" },
669 { AVIFOURCC_IMUS, "Music" },
670 { AVIFOURCC_ISTD, "Production studio" },
671 { AVIFOURCC_IDST, "Distributor" },
672 { AVIFOURCC_ICNT, "Country" },
673 { AVIFOURCC_ISTR, "Starring" },
674 { AVIFOURCC_IFRM, "Total number of parts" },
675 { AVIFOURCC_strn, "Stream name" },
678 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
681 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
684 for( i_index = 0;; i_index++)
686 if( !AVI_strz_type[i_index].i_fourcc ||
687 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
692 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
693 p_strz->p_str = xmalloc( p_strz->i_chunk_size + 1);
695 if( p_strz->i_chunk_size )
697 memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
699 p_strz->p_str[p_strz->i_chunk_size] = 0;
702 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
703 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
705 AVI_READCHUNK_EXIT( VLC_SUCCESS );
707 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
709 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
710 FREENULL( p_strz->p_type );
711 FREENULL( p_strz->p_str );
714 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
716 return AVI_NextChunk( s, p_chk );
718 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
725 vlc_fourcc_t i_fourcc;
726 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
727 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
728 } AVI_Chunk_Function [] =
730 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
731 { AVIFOURCC_ON2, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
732 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
733 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
734 { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
735 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
736 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
737 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
738 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
739 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
740 { AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
741 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
742 { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
744 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
745 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
746 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
747 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
748 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
749 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
750 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
751 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
752 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
753 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
754 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
755 { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
756 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
757 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
758 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
759 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
760 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
761 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
762 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
763 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
764 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
765 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
766 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
767 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
768 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
769 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
770 { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
771 { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
772 { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
773 { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
774 { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
775 { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
776 { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
777 { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
778 { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
779 { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
780 { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
781 { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
782 { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
783 { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
784 { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
785 { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
788 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
792 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
794 unsigned int i_index;
795 for( i_index = 0; ; i_index++ )
797 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
798 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
805 int AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
811 msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
815 if( AVI_ChunkReadCommon( s, p_chk ) )
817 msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
821 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
823 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
826 p_chk->common.p_father = p_father;
828 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
829 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
831 return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
833 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
834 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
835 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
836 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
838 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
839 return AVI_ChunkRead_indx( s, p_chk );
842 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
843 (char*)&p_chk->common.i_chunk_fourcc );
844 return AVI_NextChunk( s, p_chk );
847 void AVI_ChunkFree( stream_t *s,
851 avi_chunk_t *p_child, *p_next;
858 /* Free all child chunk */
859 p_child = p_chk->common.p_first;
862 p_next = p_child->common.p_next;
863 AVI_ChunkFree( s, p_child );
868 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
869 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
872 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
873 (char*)&p_chk->common.i_chunk_fourcc );
875 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
879 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
880 (char*)&p_chk->common.i_chunk_fourcc );
882 p_chk->common.p_first = NULL;
883 p_chk->common.p_last = NULL;
888 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
889 avi_chunk_t *p_chk, unsigned i_level )
891 avi_chunk_t *p_child;
894 if( i_level >= (sizeof(str) - 1)/4 )
897 memset( str, ' ', sizeof( str ) );
898 for( unsigned i = 1; i < i_level; i++ )
902 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
903 p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
904 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
906 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
907 "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
909 (char*)&p_chk->common.i_chunk_fourcc,
910 (char*)&p_chk->list.i_type,
911 p_chk->common.i_chunk_size,
912 p_chk->common.i_chunk_pos );
916 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
917 "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
918 (char*)&p_chk->common.i_chunk_fourcc,
919 p_chk->common.i_chunk_size,
920 p_chk->common.i_chunk_pos );
922 msg_Dbg( p_obj, "%s", str );
924 p_child = p_chk->common.p_first;
927 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
928 p_child = p_child->common.p_next;
932 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
934 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
938 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
940 p_list->i_chunk_pos = 0;
941 p_list->i_chunk_size = stream_Size( s );
942 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
943 p_list->p_father = NULL;
944 p_list->p_next = NULL;
945 p_list->p_first = NULL;
946 p_list->p_last = NULL;
948 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
952 p_chk = xmalloc( sizeof( avi_chunk_t ) );
953 memset( p_chk, 0, sizeof( avi_chunk_t ) );
954 if( !p_root->common.p_first )
956 p_root->common.p_first = p_chk;
960 p_root->common.p_last->common.p_next = p_chk;
962 p_root->common.p_last = p_chk;
964 if( AVI_ChunkRead( s, p_chk, p_root ) ||
965 ( stream_Tell( s ) >=
966 (off_t)p_chk->common.p_father->common.i_chunk_pos +
967 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
971 /* If we can't seek then stop when we 've found first RIFF-AVI */
972 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
973 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
979 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
983 void AVI_ChunkFreeRoot( stream_t *s,
986 AVI_ChunkFree( s, p_chk );
990 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
993 avi_chunk_t *p_child;
1001 p_child = p_chk->common.p_first;
1004 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1005 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1006 p_child->list.i_type == i_fourcc ) )
1010 p_child = p_child->common.p_next;
1015 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
1016 vlc_fourcc_t i_fourcc, int i_number )
1018 avi_chunk_t *p_child;
1023 p_child = p_chk->common.p_first;
1027 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1028 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1029 p_child->list.i_type == i_fourcc ) )
1039 p_child = p_child->common.p_next;