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 /****************************************************************************
45 * Basics functions to manipulates chunks
47 ****************************************************************************/
48 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
50 const uint8_t *p_peek;
53 memset( p_chk, 0, sizeof( avi_chunk_t ) );
55 if( ( i_peek = stream_Peek( s, &p_peek, 8 ) ) < 8 )
60 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
61 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
62 p_chk->common.i_chunk_pos = stream_Tell( s );
64 p_chk->common.p_father = NULL;
65 p_chk->common.p_next = NULL;
66 p_chk->common.p_first = NULL;
67 p_chk->common.p_next = NULL;
70 msg_Dbg( (vlc_object_t*)s,
71 "found Chunk fourcc:%8.8x (%4.4s) size:%"PRId64" pos:%"PRId64,
72 p_chk->common.i_chunk_fourcc,
73 (char*)&p_chk->common.i_chunk_fourcc,
74 p_chk->common.i_chunk_size,
75 p_chk->common.i_chunk_pos );
80 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
86 if( AVI_ChunkReadCommon( s, &chk ) )
93 if( p_chk->common.p_father )
95 if( p_chk->common.p_father->common.i_chunk_pos +
96 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
97 p_chk->common.i_chunk_pos +
98 __EVEN( p_chk->common.i_chunk_size ) + 8 )
103 return stream_Seek( s, p_chk->common.i_chunk_pos +
104 __EVEN( p_chk->common.i_chunk_size ) + 8 );
107 /****************************************************************************
109 * Functions to read chunks
111 ****************************************************************************/
112 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
115 const uint8_t *p_peek;
118 if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 8 )
121 msg_Warn( (vlc_object_t*)s, "empty list chunk" );
124 if( stream_Peek( s, &p_peek, 12 ) < 12 )
126 msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
130 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
132 p_container->list.i_type = GetFOURCC( p_peek + 8 );
134 /* XXX fixed for on2 hack */
135 if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
137 p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
138 p_container->list.i_type = AVIFOURCC_AVI;
141 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
142 p_container->list.i_type == AVIFOURCC_movi )
144 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
147 return AVI_NextChunk( s, p_container );
149 return VLC_SUCCESS; /* point at begining of LIST-movi */
152 if( stream_Read( s, NULL, 12 ) != 12 )
154 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
159 msg_Dbg( (vlc_object_t*)s,
160 "found LIST chunk: \'%4.4s\'",
161 (char*)&p_container->list.i_type );
163 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
166 p_chk = malloc( sizeof( avi_chunk_t ) );
167 memset( p_chk, 0, sizeof( avi_chunk_t ) );
168 if( !p_container->common.p_first )
170 p_container->common.p_first = p_chk;
174 p_container->common.p_last->common.p_next = p_chk;
176 p_container->common.p_last = p_chk;
178 if( AVI_ChunkRead( s, p_chk, p_container ) )
182 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
184 (off_t)p_chk->common.p_father->common.i_chunk_pos +
185 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
190 /* If we can't seek then stop when we 've found LIST-movi */
191 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
192 p_chk->list.i_type == AVIFOURCC_movi &&
193 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
199 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
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 = malloc( __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 );
346 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
347 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
349 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 );
357 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
359 /* Found an extensible header atm almost nothing uses that. */
360 msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
361 "vorbis audio dectected: not supported" );
366 p_chk->strf.auds.p_wf->cbSize = 0;
368 if( p_chk->strf.auds.p_wf->cbSize > 0 )
370 memcpy( &p_chk->strf.auds.p_wf[1] ,
371 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourrc+size */
372 p_chk->strf.auds.p_wf->cbSize );
375 msg_Dbg( (vlc_object_t*)s,
376 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
377 p_chk->strf.auds.p_wf->wFormatTag,
378 p_chk->strf.auds.p_wf->nChannels,
379 p_chk->strf.auds.p_wf->nSamplesPerSec,
380 p_chk->strf.auds.p_wf->wBitsPerSample,
381 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
384 case( AVIFOURCC_vids ):
385 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
386 p_chk->strf.vids.i_cat = VIDEO_ES;
387 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
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(BITMAPINFOHEADER) > 0 )
405 memcpy( &p_chk->strf.vids.p_bih[1],
406 p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
407 p_chk->common.i_chunk_size -sizeof(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 );
420 msg_Warn( (vlc_object_t*)s, "unknown stream type" );
421 p_chk->strf.common.i_cat = UNKNOWN_ES;
424 AVI_READCHUNK_EXIT( VLC_SUCCESS );
426 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
428 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
429 if( p_strf->common.i_cat == AUDIO_ES )
431 FREENULL( p_strf->auds.p_wf );
433 else if( p_strf->common.i_cat == VIDEO_ES )
435 FREENULL( p_strf->vids.p_bih );
439 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
442 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
443 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
444 AVI_READCHUNK_EXIT( VLC_SUCCESS );
447 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
449 free( p_chk->strd.p_data );
452 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
454 unsigned int i_count, i_index;
458 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
460 p_chk->idx1.i_entry_count = i_count;
461 p_chk->idx1.i_entry_max = i_count;
464 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
466 for( i_index = 0; i_index < i_count ; i_index++ )
468 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
469 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
470 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
471 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
476 p_chk->idx1.entry = NULL;
479 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
481 AVI_READCHUNK_EXIT( VLC_SUCCESS );
484 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
486 p_chk->idx1.i_entry_count = 0;
487 p_chk->idx1.i_entry_max = 0;
488 FREENULL( p_chk->idx1.entry );
493 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
495 unsigned int i_count, i;
497 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
501 AVI_READ2BYTES( p_indx->i_longsperentry );
502 AVI_READ1BYTE ( p_indx->i_indexsubtype );
503 AVI_READ1BYTE ( p_indx->i_indextype );
504 AVI_READ4BYTES( p_indx->i_entriesinuse );
506 AVI_READ4BYTES( p_indx->i_id );
507 p_indx->idx.std = NULL;
508 p_indx->idx.field = NULL;
509 p_indx->idx.super = NULL;
511 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
513 AVI_READ8BYTES( p_indx->i_baseoffset );
514 AVI_READ4BYTES( i_dummy );
516 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
517 p_indx->i_entriesinuse = i_count;
518 p_indx->idx.std = calloc( i_count, sizeof( indx_std_entry_t ) );
520 for( i = 0; i < i_count; i++ )
522 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
523 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
526 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
528 AVI_READ8BYTES( p_indx->i_baseoffset );
529 AVI_READ4BYTES( i_dummy );
531 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
532 p_indx->i_entriesinuse = i_count;
533 p_indx->idx.field = calloc( i_count, sizeof( indx_field_entry_t ) );
534 for( i = 0; i < i_count; i++ )
536 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
537 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
538 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
541 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
543 p_indx->i_baseoffset = 0;
544 AVI_READ4BYTES( i_dummy );
545 AVI_READ4BYTES( i_dummy );
546 AVI_READ4BYTES( i_dummy );
548 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
549 p_indx->i_entriesinuse = i_count;
550 p_indx->idx.super = calloc( i_count, sizeof( indx_super_entry_t ) );
552 for( i = 0; i < i_count; i++ )
554 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
555 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
556 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
561 msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
565 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 );
567 AVI_READCHUNK_EXIT( VLC_SUCCESS );
569 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
571 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
573 FREENULL( p_indx->idx.std );
574 FREENULL( p_indx->idx.field );
575 FREENULL( p_indx->idx.super );
582 vlc_fourcc_t i_fourcc;
583 const char *psz_type;
586 { AVIFOURCC_IARL, "archive location" },
587 { AVIFOURCC_IART, "artist" },
588 { AVIFOURCC_ICMS, "commisioned" },
589 { AVIFOURCC_ICMT, "comments" },
590 { AVIFOURCC_ICOP, "copyright" },
591 { AVIFOURCC_ICRD, "creation date" },
592 { AVIFOURCC_ICRP, "cropped" },
593 { AVIFOURCC_IDIM, "dimensions" },
594 { AVIFOURCC_IDPI, "dots per inch" },
595 { AVIFOURCC_IENG, "engineer" },
596 { AVIFOURCC_IGNR, "genre" },
597 { AVIFOURCC_IKEY, "keywords" },
598 { AVIFOURCC_ILGT, "lightness" },
599 { AVIFOURCC_IMED, "medium" },
600 { AVIFOURCC_INAM, "name" },
601 { AVIFOURCC_IPLT, "palette setting" },
602 { AVIFOURCC_IPRD, "product" },
603 { AVIFOURCC_ISBJ, "subject" },
604 { AVIFOURCC_ISFT, "software" },
605 { AVIFOURCC_ISHP, "sharpness" },
606 { AVIFOURCC_ISRC, "source" },
607 { AVIFOURCC_ISRF, "source form" },
608 { AVIFOURCC_ITCH, "technician" },
609 { AVIFOURCC_ISMP, "time code" },
610 { AVIFOURCC_IDIT, "digitalization time" },
611 { AVIFOURCC_strn, "stream name" },
614 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
617 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
620 for( i_index = 0;; i_index++)
622 if( !AVI_strz_type[i_index].i_fourcc ||
623 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
628 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
629 p_strz->p_str = malloc( i_read + 1);
631 if( p_strz->i_chunk_size )
633 memcpy( p_strz->p_str, p_read, i_read );
635 p_strz->p_str[i_read] = 0;
638 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
639 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
641 AVI_READCHUNK_EXIT( VLC_SUCCESS );
643 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
645 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
646 FREENULL( p_strz->p_type );
647 FREENULL( p_strz->p_str );
650 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
652 return AVI_NextChunk( s, p_chk );
654 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
661 vlc_fourcc_t i_fourcc;
662 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
663 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
664 } AVI_Chunk_Function [] =
666 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
667 { AVIFOURCC_ON2, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
668 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
669 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
670 { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
671 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
672 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
673 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
674 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
675 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
676 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
678 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
679 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
680 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
681 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
682 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
683 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
684 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
685 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
686 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
687 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
688 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
689 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
690 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
691 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
692 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
693 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
694 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
695 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
696 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
697 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
698 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
699 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
700 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
701 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
702 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
703 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
704 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
708 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
710 unsigned int i_index;
711 for( i_index = 0; ; i_index++ )
713 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
714 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
721 int _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
730 if( AVI_ChunkReadCommon( s, p_chk ) )
732 msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
735 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
737 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
740 p_chk->common.p_father = p_father;
742 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
743 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
745 return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
747 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
748 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
749 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
750 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
752 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
753 return AVI_ChunkRead_indx( s, p_chk );
755 msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
756 return AVI_NextChunk( s, p_chk );
759 void _AVI_ChunkFree( stream_t *s,
763 avi_chunk_t *p_child, *p_next;
770 /* Free all child chunk */
771 p_child = p_chk->common.p_first;
774 p_next = p_child->common.p_next;
775 AVI_ChunkFree( s, p_child );
780 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
781 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
784 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
785 (char*)&p_chk->common.i_chunk_fourcc );
787 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
791 msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
793 p_chk->common.p_first = NULL;
794 p_chk->common.p_last = NULL;
799 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
800 avi_chunk_t *p_chk, unsigned i_level )
803 avi_chunk_t *p_child;
806 if( i_level >= (sizeof(str) - 1)/5 )
809 memset( str, ' ', sizeof( str ) );
810 for( i = 1; i < i_level; i++ )
814 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
815 p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
816 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
818 snprintf( &str[i_level * 5], sizeof(str) - 5*i_level,
819 "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
821 (char*)&p_chk->common.i_chunk_fourcc,
822 (char*)&p_chk->list.i_type,
823 p_chk->common.i_chunk_size,
824 p_chk->common.i_chunk_pos );
828 snprintf( &str[i_level * 5], sizeof(str) - 5*i_level,
829 "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
830 (char*)&p_chk->common.i_chunk_fourcc,
831 p_chk->common.i_chunk_size,
832 p_chk->common.i_chunk_pos );
834 msg_Dbg( p_obj, "%s", str );
836 p_child = p_chk->common.p_first;
839 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
840 p_child = p_child->common.p_next;
844 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
846 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
850 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
852 p_list->i_chunk_pos = 0;
853 p_list->i_chunk_size = stream_Size( s );
854 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
855 p_list->p_father = NULL;
856 p_list->p_next = NULL;
857 p_list->p_first = NULL;
858 p_list->p_last = NULL;
860 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
864 p_chk = malloc( sizeof( avi_chunk_t ) );
865 memset( p_chk, 0, sizeof( avi_chunk_t ) );
866 if( !p_root->common.p_first )
868 p_root->common.p_first = p_chk;
872 p_root->common.p_last->common.p_next = p_chk;
874 p_root->common.p_last = p_chk;
876 if( AVI_ChunkRead( s, p_chk, p_root ) ||
877 ( stream_Tell( s ) >=
878 (off_t)p_chk->common.p_father->common.i_chunk_pos +
879 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
883 /* If we can't seek then stop when we 've found first RIFF-AVI */
884 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
885 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
891 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
895 void AVI_ChunkFreeRoot( stream_t *s,
898 AVI_ChunkFree( s, p_chk );
902 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
905 avi_chunk_t *p_child;
913 p_child = p_chk->common.p_first;
916 if( p_child->common.i_chunk_fourcc == i_fourcc ||
917 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
918 p_child->list.i_type == i_fourcc ) )
922 p_child = p_child->common.p_next;
927 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
928 vlc_fourcc_t i_fourcc, int i_number )
930 avi_chunk_t *p_child;
935 p_child = p_chk->common.p_first;
939 if( p_child->common.i_chunk_fourcc == i_fourcc ||
940 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
941 p_child->list.i_type == i_fourcc ) )
951 p_child = p_child->common.p_next;