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 *****************************************************************************/
23 #include <stdlib.h> /* malloc(), free() */
26 #include <vlc_demux.h>
27 #include <vlc_codecs.h> /* BITMAPINFOHEADER */
33 #define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
35 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
37 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
40 #define AVI_ChunkFree( a, b ) _AVI_ChunkFree( (a), (avi_chunk_t*)(b) )
41 void _AVI_ChunkFree( stream_t *, avi_chunk_t *p_chk );
43 /****************************************************************************
45 * Basics functions to manipulates chunks
47 ****************************************************************************/
48 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk )
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:"I64Fd" pos:"I64Fd,
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 )
116 vlc_bool_t b_seekable;
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 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
135 p_container->list.i_type == AVIFOURCC_movi )
137 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
140 return AVI_NextChunk( s, p_container );
142 return VLC_SUCCESS; /* point at begining of LIST-movi */
145 if( stream_Read( s, NULL, 12 ) != 12 )
147 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
152 msg_Dbg( (vlc_object_t*)s,
153 "found LIST chunk: \'%4.4s\'",
154 (char*)&p_container->list.i_type );
156 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
159 p_chk = malloc( sizeof( avi_chunk_t ) );
160 memset( p_chk, 0, sizeof( avi_chunk_t ) );
161 if( !p_container->common.p_first )
163 p_container->common.p_first = p_chk;
167 p_container->common.p_last->common.p_next = p_chk;
169 p_container->common.p_last = p_chk;
171 if( AVI_ChunkRead( s, p_chk, p_container ) )
175 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
176 ( stream_Tell( s ) >=
177 (off_t)p_chk->common.p_father->common.i_chunk_pos +
178 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
183 /* If we can't seek then stop when we 've found LIST-movi */
184 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
185 p_chk->list.i_type == AVIFOURCC_movi &&
186 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
192 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
197 #define AVI_READCHUNK_ENTER \
198 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
199 uint8_t *p_read, *p_buff; \
200 if( !( p_read = p_buff = malloc(i_read ) ) ) \
202 return VLC_EGENERIC; \
204 i_read = stream_Read( s, p_read, i_read ); \
205 if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
208 return VLC_EGENERIC; \
213 #define AVI_READCHUNK_EXIT( code ) \
217 msg_Warn( (vlc_object_t*)s, "not enough data" ); \
221 #define AVI_READ1BYTE( i_byte ) \
226 #define AVI_READ2BYTES( i_word ) \
227 i_word = GetWLE( p_read ); \
231 #define AVI_READ4BYTES( i_dword ) \
232 i_dword = GetDWLE( p_read ); \
236 #define AVI_READ8BYTES( i_dword ) \
237 i_dword = GetQWLE( p_read ); \
241 #define AVI_READFOURCC( i_dword ) \
242 i_dword = GetFOURCC( p_read ); \
246 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
250 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
251 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
252 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
253 AVI_READ4BYTES( p_chk->avih.i_flags );
254 AVI_READ4BYTES( p_chk->avih.i_totalframes );
255 AVI_READ4BYTES( p_chk->avih.i_initialframes );
256 AVI_READ4BYTES( p_chk->avih.i_streams );
257 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
258 AVI_READ4BYTES( p_chk->avih.i_width );
259 AVI_READ4BYTES( p_chk->avih.i_height );
260 AVI_READ4BYTES( p_chk->avih.i_scale );
261 AVI_READ4BYTES( p_chk->avih.i_rate );
262 AVI_READ4BYTES( p_chk->avih.i_start );
263 AVI_READ4BYTES( p_chk->avih.i_length );
265 msg_Dbg( (vlc_object_t*)s,
266 "avih: streams:%d flags:%s%s%s%s %dx%d",
267 p_chk->avih.i_streams,
268 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
269 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
270 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
271 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
272 p_chk->avih.i_width, p_chk->avih.i_height );
274 AVI_READCHUNK_EXIT( VLC_SUCCESS );
277 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
281 AVI_READFOURCC( p_chk->strh.i_type );
282 AVI_READFOURCC( p_chk->strh.i_handler );
283 AVI_READ4BYTES( p_chk->strh.i_flags );
284 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
285 AVI_READ4BYTES( p_chk->strh.i_initialframes );
286 AVI_READ4BYTES( p_chk->strh.i_scale );
287 AVI_READ4BYTES( p_chk->strh.i_rate );
288 AVI_READ4BYTES( p_chk->strh.i_start );
289 AVI_READ4BYTES( p_chk->strh.i_length );
290 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
291 AVI_READ4BYTES( p_chk->strh.i_quality );
292 AVI_READ4BYTES( p_chk->strh.i_samplesize );
294 msg_Dbg( (vlc_object_t*)s,
295 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
296 (char*)&p_chk->strh.i_type,
297 p_chk->strh.i_handler,
298 p_chk->strh.i_samplesize,
299 ( p_chk->strh.i_scale ?
300 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
303 AVI_READCHUNK_EXIT( VLC_SUCCESS );
306 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
311 if( p_chk->common.p_father == NULL )
313 msg_Err( (vlc_object_t*)s, "malformed avi file" );
314 AVI_READCHUNK_EXIT( VLC_EGENERIC );
316 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
318 msg_Err( (vlc_object_t*)s, "malformed avi file" );
319 AVI_READCHUNK_EXIT( VLC_EGENERIC );
322 switch( p_strh->strh.i_type )
324 case( AVIFOURCC_auds ):
325 p_chk->strf.auds.i_cat = AUDIO_ES;
326 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
327 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
328 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
329 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
330 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
331 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
332 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
333 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
334 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
336 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
337 /* prevent segfault */
338 if( p_chk->strf.auds.p_wf->cbSize >
339 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
341 p_chk->strf.auds.p_wf->cbSize =
342 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
344 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
346 /* Found an extensible header atm almost nothing uses that. */
347 msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
348 "vorbis audio dectected: not supported" );
353 p_chk->strf.auds.p_wf->cbSize = 0;
355 if( p_chk->strf.auds.p_wf->cbSize > 0 )
357 memcpy( &p_chk->strf.auds.p_wf[1] ,
358 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourrc+size */
359 p_chk->strf.auds.p_wf->cbSize );
362 msg_Dbg( (vlc_object_t*)s,
363 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
364 p_chk->strf.auds.p_wf->wFormatTag,
365 p_chk->strf.auds.p_wf->nChannels,
366 p_chk->strf.auds.p_wf->nSamplesPerSec,
367 p_chk->strf.auds.p_wf->wBitsPerSample,
368 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
371 case( AVIFOURCC_vids ):
372 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
373 p_chk->strf.vids.i_cat = VIDEO_ES;
374 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
375 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
376 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
377 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
378 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
379 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
380 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
381 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
382 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
383 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
384 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
385 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
386 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
388 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
390 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
392 memcpy( &p_chk->strf.vids.p_bih[1],
393 p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
394 p_chk->common.i_chunk_size -sizeof(BITMAPINFOHEADER) );
397 msg_Dbg( (vlc_object_t*)s,
398 "strf: video:%4.4s %dx%d planes:%d %dbpp",
399 (char*)&p_chk->strf.vids.p_bih->biCompression,
400 p_chk->strf.vids.p_bih->biWidth,
401 p_chk->strf.vids.p_bih->biHeight,
402 p_chk->strf.vids.p_bih->biPlanes,
403 p_chk->strf.vids.p_bih->biBitCount );
407 msg_Warn( (vlc_object_t*)s, "unknown stream type" );
408 p_chk->strf.common.i_cat = UNKNOWN_ES;
411 AVI_READCHUNK_EXIT( VLC_SUCCESS );
413 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
415 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
416 if( p_strf->common.i_cat == AUDIO_ES )
418 FREENULL( p_strf->auds.p_wf );
420 else if( p_strf->common.i_cat == VIDEO_ES )
422 FREENULL( p_strf->vids.p_bih );
426 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
429 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
430 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
431 AVI_READCHUNK_EXIT( VLC_SUCCESS );
434 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
436 if( p_chk->strd.p_data ) free( p_chk->strd.p_data );
439 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
441 unsigned int i_count, i_index;
445 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
447 p_chk->idx1.i_entry_count = i_count;
448 p_chk->idx1.i_entry_max = i_count;
451 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
453 for( i_index = 0; i_index < i_count ; i_index++ )
455 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
456 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
457 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
458 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
463 p_chk->idx1.entry = NULL;
466 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
468 AVI_READCHUNK_EXIT( VLC_SUCCESS );
471 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
473 p_chk->idx1.i_entry_count = 0;
474 p_chk->idx1.i_entry_max = 0;
475 FREENULL( p_chk->idx1.entry )
480 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
482 unsigned int i_count, i;
484 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
488 AVI_READ2BYTES( p_indx->i_longsperentry );
489 AVI_READ1BYTE ( p_indx->i_indexsubtype );
490 AVI_READ1BYTE ( p_indx->i_indextype );
491 AVI_READ4BYTES( p_indx->i_entriesinuse );
493 AVI_READ4BYTES( p_indx->i_id );
494 p_indx->idx.std = NULL;
495 p_indx->idx.field = NULL;
496 p_indx->idx.super = NULL;
498 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
500 AVI_READ8BYTES( p_indx->i_baseoffset );
501 AVI_READ4BYTES( i_dummy );
503 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
504 p_indx->i_entriesinuse = i_count;
505 p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
507 for( i = 0; i < i_count; i++ )
509 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
510 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
513 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
515 AVI_READ8BYTES( p_indx->i_baseoffset );
516 AVI_READ4BYTES( i_dummy );
518 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
519 p_indx->i_entriesinuse = i_count;
520 p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
521 for( i = 0; i < i_count; i++ )
523 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
524 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
525 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
528 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
530 p_indx->i_baseoffset = 0;
531 AVI_READ4BYTES( i_dummy );
532 AVI_READ4BYTES( i_dummy );
533 AVI_READ4BYTES( i_dummy );
535 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
536 p_indx->i_entriesinuse = i_count;
537 p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
539 for( i = 0; i < i_count; i++ )
541 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
542 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
543 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
548 msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
552 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 );
554 AVI_READCHUNK_EXIT( VLC_SUCCESS );
556 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
558 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
560 FREENULL( p_indx->idx.std );
561 FREENULL( p_indx->idx.field );
562 FREENULL( p_indx->idx.super );
569 vlc_fourcc_t i_fourcc;
570 const char *psz_type;
573 { AVIFOURCC_IARL, "archive location" },
574 { AVIFOURCC_IART, "artist" },
575 { AVIFOURCC_ICMS, "commisioned" },
576 { AVIFOURCC_ICMT, "comments" },
577 { AVIFOURCC_ICOP, "copyright" },
578 { AVIFOURCC_ICRD, "creation date" },
579 { AVIFOURCC_ICRP, "cropped" },
580 { AVIFOURCC_IDIM, "dimensions" },
581 { AVIFOURCC_IDPI, "dots per inch" },
582 { AVIFOURCC_IENG, "engineer" },
583 { AVIFOURCC_IGNR, "genre" },
584 { AVIFOURCC_IKEY, "keywords" },
585 { AVIFOURCC_ILGT, "lightness" },
586 { AVIFOURCC_IMED, "medium" },
587 { AVIFOURCC_INAM, "name" },
588 { AVIFOURCC_IPLT, "palette setting" },
589 { AVIFOURCC_IPRD, "product" },
590 { AVIFOURCC_ISBJ, "subject" },
591 { AVIFOURCC_ISFT, "software" },
592 { AVIFOURCC_ISHP, "sharpness" },
593 { AVIFOURCC_ISRC, "source" },
594 { AVIFOURCC_ISRF, "source form" },
595 { AVIFOURCC_ITCH, "technician" },
596 { AVIFOURCC_ISMP, "time code" },
597 { AVIFOURCC_IDIT, "digitalization time" },
598 { AVIFOURCC_strn, "stream name" },
601 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
604 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
607 for( i_index = 0;; i_index++)
609 if( !AVI_strz_type[i_index].i_fourcc ||
610 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
615 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
616 p_strz->p_str = malloc( i_read + 1);
618 if( p_strz->i_chunk_size )
620 memcpy( p_strz->p_str, p_read, i_read );
622 p_strz->p_str[i_read] = 0;
625 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
626 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
628 AVI_READCHUNK_EXIT( VLC_SUCCESS );
630 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
632 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
633 FREENULL( p_strz->p_type );
634 FREENULL( p_strz->p_str );
637 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
639 return AVI_NextChunk( s, p_chk );
641 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
648 vlc_fourcc_t i_fourcc;
649 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
650 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
651 } AVI_Chunk_Function [] =
653 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
654 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
655 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
656 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
657 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
658 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
659 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
660 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
661 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
663 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
664 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
665 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
666 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
667 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
668 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
669 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
670 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
671 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
672 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
673 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
674 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
675 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
676 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
677 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
678 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
679 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
680 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
681 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
682 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
683 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
684 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
685 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
686 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
687 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
688 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
689 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
693 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
695 unsigned int i_index;
696 for( i_index = 0; ; i_index++ )
698 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
699 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
706 int _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
715 if( AVI_ChunkReadCommon( s, p_chk ) )
717 msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
720 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
722 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
725 p_chk->common.p_father = p_father;
727 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
728 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
730 return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
732 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
733 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
734 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
735 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
737 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
738 return AVI_ChunkRead_indx( s, p_chk );
740 msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
741 return AVI_NextChunk( s, p_chk );
744 void _AVI_ChunkFree( stream_t *s,
748 avi_chunk_t *p_child, *p_next;
755 /* Free all child chunk */
756 p_child = p_chk->common.p_first;
759 p_next = p_child->common.p_next;
760 AVI_ChunkFree( s, p_child );
765 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
766 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
769 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
770 (char*)&p_chk->common.i_chunk_fourcc );
772 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
776 msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
778 p_chk->common.p_first = NULL;
779 p_chk->common.p_last = NULL;
784 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
785 avi_chunk_t *p_chk, int i_level )
789 avi_chunk_t *p_child;
791 memset( str, ' ', sizeof( str ) );
792 for( i = 1; i < i_level; i++ )
796 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
797 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
799 sprintf( str + i_level * 5,
800 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
802 (char*)&p_chk->common.i_chunk_fourcc,
803 (char*)&p_chk->list.i_type,
804 p_chk->common.i_chunk_size,
805 p_chk->common.i_chunk_pos );
809 sprintf( str + i_level * 5,
810 "+ %4.4s size:"I64Fu" pos:"I64Fu,
811 (char*)&p_chk->common.i_chunk_fourcc,
812 p_chk->common.i_chunk_size,
813 p_chk->common.i_chunk_pos );
815 msg_Dbg( p_obj, "%s", str );
817 p_child = p_chk->common.p_first;
820 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
821 p_child = p_child->common.p_next;
825 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
827 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
829 vlc_bool_t b_seekable;
831 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
833 p_list->i_chunk_pos = 0;
834 p_list->i_chunk_size = stream_Size( s );
835 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
836 p_list->p_father = NULL;
837 p_list->p_next = NULL;
838 p_list->p_first = NULL;
839 p_list->p_last = NULL;
841 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
845 p_chk = malloc( sizeof( avi_chunk_t ) );
846 memset( p_chk, 0, sizeof( avi_chunk_t ) );
847 if( !p_root->common.p_first )
849 p_root->common.p_first = p_chk;
853 p_root->common.p_last->common.p_next = p_chk;
855 p_root->common.p_last = p_chk;
857 if( AVI_ChunkRead( s, p_chk, p_root ) ||
858 ( stream_Tell( s ) >=
859 (off_t)p_chk->common.p_father->common.i_chunk_pos +
860 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
864 /* If we can't seek then stop when we 've found first RIFF-AVI */
865 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
866 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
872 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
876 void AVI_ChunkFreeRoot( stream_t *s,
879 AVI_ChunkFree( s, p_chk );
883 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
886 avi_chunk_t *p_child;
894 p_child = p_chk->common.p_first;
897 if( p_child->common.i_chunk_fourcc == i_fourcc ||
898 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
899 p_child->list.i_type == i_fourcc ) )
903 p_child = p_child->common.p_next;
908 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
909 vlc_fourcc_t i_fourcc, int i_number )
911 avi_chunk_t *p_child;
916 p_child = p_chk->common.p_first;
920 if( p_child->common.i_chunk_fourcc == i_fourcc ||
921 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
922 p_child->list.i_type == i_fourcc ) )
932 p_child = p_child->common.p_next;