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/input.h>
27 #include "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 ) \
207 return VLC_EGENERIC; \
212 #define AVI_READCHUNK_EXIT( code ) \
216 msg_Warn( (vlc_object_t*)s, "not enough data" ); \
220 #define AVI_READ1BYTE( i_byte ) \
225 #define AVI_READ2BYTES( i_word ) \
226 i_word = GetWLE( p_read ); \
230 #define AVI_READ4BYTES( i_dword ) \
231 i_dword = GetDWLE( p_read ); \
235 #define AVI_READ8BYTES( i_dword ) \
236 i_dword = GetQWLE( p_read ); \
240 #define AVI_READFOURCC( i_dword ) \
241 i_dword = GetFOURCC( p_read ); \
245 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
249 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
250 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
251 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
252 AVI_READ4BYTES( p_chk->avih.i_flags );
253 AVI_READ4BYTES( p_chk->avih.i_totalframes );
254 AVI_READ4BYTES( p_chk->avih.i_initialframes );
255 AVI_READ4BYTES( p_chk->avih.i_streams );
256 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
257 AVI_READ4BYTES( p_chk->avih.i_width );
258 AVI_READ4BYTES( p_chk->avih.i_height );
259 AVI_READ4BYTES( p_chk->avih.i_scale );
260 AVI_READ4BYTES( p_chk->avih.i_rate );
261 AVI_READ4BYTES( p_chk->avih.i_start );
262 AVI_READ4BYTES( p_chk->avih.i_length );
264 msg_Dbg( (vlc_object_t*)s,
265 "avih: streams:%d flags:%s%s%s%s %dx%d",
266 p_chk->avih.i_streams,
267 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
268 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
269 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
270 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
271 p_chk->avih.i_width, p_chk->avih.i_height );
273 AVI_READCHUNK_EXIT( VLC_SUCCESS );
276 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
280 AVI_READFOURCC( p_chk->strh.i_type );
281 AVI_READFOURCC( p_chk->strh.i_handler );
282 AVI_READ4BYTES( p_chk->strh.i_flags );
283 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
284 AVI_READ4BYTES( p_chk->strh.i_initialframes );
285 AVI_READ4BYTES( p_chk->strh.i_scale );
286 AVI_READ4BYTES( p_chk->strh.i_rate );
287 AVI_READ4BYTES( p_chk->strh.i_start );
288 AVI_READ4BYTES( p_chk->strh.i_length );
289 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
290 AVI_READ4BYTES( p_chk->strh.i_quality );
291 AVI_READ4BYTES( p_chk->strh.i_samplesize );
293 msg_Dbg( (vlc_object_t*)s,
294 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
295 (char*)&p_chk->strh.i_type,
296 p_chk->strh.i_handler,
297 p_chk->strh.i_samplesize,
298 ( p_chk->strh.i_scale ?
299 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
302 AVI_READCHUNK_EXIT( VLC_SUCCESS );
305 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
310 if( p_chk->common.p_father == NULL )
312 msg_Err( (vlc_object_t*)s, "malformed avi file" );
313 AVI_READCHUNK_EXIT( VLC_EGENERIC );
315 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
317 msg_Err( (vlc_object_t*)s, "malformed avi file" );
318 AVI_READCHUNK_EXIT( VLC_EGENERIC );
321 switch( p_strh->strh.i_type )
323 case( AVIFOURCC_auds ):
324 p_chk->strf.auds.i_cat = AUDIO_ES;
325 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
326 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
327 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
328 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
329 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
330 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
331 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
332 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
333 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
335 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
336 /* prevent segfault */
337 if( p_chk->strf.auds.p_wf->cbSize >
338 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
340 p_chk->strf.auds.p_wf->cbSize =
341 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
343 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
345 /* Found an extensible header atm almost nothing uses that. */
346 msg_Warn( (vlc_object_t*)s, "WAVE_FORMAT_EXTENSIBLE or "
347 "vorbis audio dectected: not supported" );
352 p_chk->strf.auds.p_wf->cbSize = 0;
354 if( p_chk->strf.auds.p_wf->cbSize > 0 )
356 memcpy( &p_chk->strf.auds.p_wf[1] ,
357 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourrc+size */
358 p_chk->strf.auds.p_wf->cbSize );
361 msg_Dbg( (vlc_object_t*)s,
362 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
363 p_chk->strf.auds.p_wf->wFormatTag,
364 p_chk->strf.auds.p_wf->nChannels,
365 p_chk->strf.auds.p_wf->nSamplesPerSec,
366 p_chk->strf.auds.p_wf->wBitsPerSample,
367 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
370 case( AVIFOURCC_vids ):
371 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
372 p_chk->strf.vids.i_cat = VIDEO_ES;
373 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
374 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
375 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
376 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
377 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
378 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
379 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
380 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
381 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
382 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
383 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
384 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
385 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
387 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
389 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
391 memcpy( &p_chk->strf.vids.p_bih[1],
392 p_buff + 8 + sizeof(BITMAPINFOHEADER), /* 8=fourrc+size */
393 p_chk->common.i_chunk_size -sizeof(BITMAPINFOHEADER) );
396 msg_Dbg( (vlc_object_t*)s,
397 "strf: video:%4.4s %dx%d planes:%d %dbpp",
398 (char*)&p_chk->strf.vids.p_bih->biCompression,
399 p_chk->strf.vids.p_bih->biWidth,
400 p_chk->strf.vids.p_bih->biHeight,
401 p_chk->strf.vids.p_bih->biPlanes,
402 p_chk->strf.vids.p_bih->biBitCount );
406 msg_Warn( (vlc_object_t*)s, "unknown stream type" );
407 p_chk->strf.common.i_cat = UNKNOWN_ES;
410 AVI_READCHUNK_EXIT( VLC_SUCCESS );
412 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
414 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
415 if( p_strf->common.i_cat == AUDIO_ES )
417 FREENULL( p_strf->auds.p_wf );
419 else if( p_strf->common.i_cat == VIDEO_ES )
421 FREENULL( p_strf->vids.p_bih );
425 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
428 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
429 memcpy( p_chk->strd.p_data,
431 p_chk->common.i_chunk_size );
432 AVI_READCHUNK_EXIT( VLC_SUCCESS );
435 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
437 unsigned int i_count, i_index;
441 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
443 p_chk->idx1.i_entry_count = i_count;
444 p_chk->idx1.i_entry_max = i_count;
447 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
449 for( i_index = 0; i_index < i_count ; i_index++ )
451 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
452 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
453 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
454 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
459 p_chk->idx1.entry = NULL;
462 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
464 AVI_READCHUNK_EXIT( VLC_SUCCESS );
467 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
469 p_chk->idx1.i_entry_count = 0;
470 p_chk->idx1.i_entry_max = 0;
471 FREENULL( p_chk->idx1.entry )
476 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
478 unsigned int i_count, i;
480 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
484 AVI_READ2BYTES( p_indx->i_longsperentry );
485 AVI_READ1BYTE ( p_indx->i_indexsubtype );
486 AVI_READ1BYTE ( p_indx->i_indextype );
487 AVI_READ4BYTES( p_indx->i_entriesinuse );
489 AVI_READ4BYTES( p_indx->i_id );
490 p_indx->idx.std = NULL;
491 p_indx->idx.field = NULL;
492 p_indx->idx.super = NULL;
494 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
496 AVI_READ8BYTES( p_indx->i_baseoffset );
497 AVI_READ4BYTES( i_dummy );
499 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
500 p_indx->i_entriesinuse = i_count;
501 p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
503 for( i = 0; i < i_count; i++ )
505 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
506 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
509 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
511 AVI_READ8BYTES( p_indx->i_baseoffset );
512 AVI_READ4BYTES( i_dummy );
514 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
515 p_indx->i_entriesinuse = i_count;
516 p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
517 for( i = 0; i < i_count; i++ )
519 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
520 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
521 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
524 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
526 p_indx->i_baseoffset = 0;
527 AVI_READ4BYTES( i_dummy );
528 AVI_READ4BYTES( i_dummy );
529 AVI_READ4BYTES( i_dummy );
531 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
532 p_indx->i_entriesinuse = i_count;
533 p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
535 for( i = 0; i < i_count; i++ )
537 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
538 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
539 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
544 msg_Warn( (vlc_object_t*)s, "unknow type/subtype index" );
548 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 );
550 AVI_READCHUNK_EXIT( VLC_SUCCESS );
552 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
554 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
556 FREENULL( p_indx->idx.std );
557 FREENULL( p_indx->idx.field );
558 FREENULL( p_indx->idx.super );
565 vlc_fourcc_t i_fourcc;
569 { AVIFOURCC_IARL, "archive location" },
570 { AVIFOURCC_IART, "artist" },
571 { AVIFOURCC_ICMS, "commisioned" },
572 { AVIFOURCC_ICMT, "comments" },
573 { AVIFOURCC_ICOP, "copyright" },
574 { AVIFOURCC_ICRD, "creation date" },
575 { AVIFOURCC_ICRP, "cropped" },
576 { AVIFOURCC_IDIM, "dimensions" },
577 { AVIFOURCC_IDPI, "dots per inch" },
578 { AVIFOURCC_IENG, "engineer" },
579 { AVIFOURCC_IGNR, "genre" },
580 { AVIFOURCC_IKEY, "keywords" },
581 { AVIFOURCC_ILGT, "lightness" },
582 { AVIFOURCC_IMED, "medium" },
583 { AVIFOURCC_INAM, "name" },
584 { AVIFOURCC_IPLT, "palette setting" },
585 { AVIFOURCC_IPRD, "product" },
586 { AVIFOURCC_ISBJ, "subject" },
587 { AVIFOURCC_ISFT, "software" },
588 { AVIFOURCC_ISHP, "sharpness" },
589 { AVIFOURCC_ISRC, "source" },
590 { AVIFOURCC_ISRF, "source form" },
591 { AVIFOURCC_ITCH, "technician" },
592 { AVIFOURCC_ISMP, "time code" },
593 { AVIFOURCC_IDIT, "digitalization time" },
594 { AVIFOURCC_strn, "stream name" },
597 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
600 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
603 for( i_index = 0;; i_index++)
605 if( !AVI_strz_type[i_index].i_fourcc ||
606 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
611 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
612 p_strz->p_str = malloc( i_read + 1);
614 if( p_strz->i_chunk_size )
616 memcpy( p_strz->p_str, p_read, i_read );
618 p_strz->p_str[i_read] = 0;
621 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
622 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
624 AVI_READCHUNK_EXIT( VLC_SUCCESS );
626 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
628 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
629 FREENULL( p_strz->p_type );
630 FREENULL( p_strz->p_str );
633 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
635 return AVI_NextChunk( s, p_chk );
637 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
644 vlc_fourcc_t i_fourcc;
645 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
646 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
647 } AVI_Chunk_Function [] =
649 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
650 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
651 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
652 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
653 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
654 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
655 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
656 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
657 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
659 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
660 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
661 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
662 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
663 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
664 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
665 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
666 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
667 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
668 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
669 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
670 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
671 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
672 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
673 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
674 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
675 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
676 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
677 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
678 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
679 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
680 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
681 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
682 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
683 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
684 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
685 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
689 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
691 unsigned int i_index;
692 for( i_index = 0; ; i_index++ )
694 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
695 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
702 int _AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
711 if( AVI_ChunkReadCommon( s, p_chk ) )
713 msg_Warn( (vlc_object_t*)s, "cannot read one chunk" );
716 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
718 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
721 p_chk->common.p_father = p_father;
723 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
724 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
726 return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
728 else if( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
729 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' )
731 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
732 return AVI_ChunkRead_indx( s, p_chk );
734 msg_Warn( (vlc_object_t*)s, "unknown chunk (not loaded)" );
735 return AVI_NextChunk( s, p_chk );
738 void _AVI_ChunkFree( stream_t *s,
742 avi_chunk_t *p_child, *p_next;
749 /* Free all child chunk */
750 p_child = p_chk->common.p_first;
753 p_next = p_child->common.p_next;
754 AVI_ChunkFree( s, p_child );
759 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
760 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
763 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
764 (char*)&p_chk->common.i_chunk_fourcc );
766 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
770 msg_Warn( (vlc_object_t*)s, "unknown chunk (not unloaded)" );
772 p_chk->common.p_first = NULL;
773 p_chk->common.p_last = NULL;
778 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
779 avi_chunk_t *p_chk, int i_level )
783 avi_chunk_t *p_child;
785 memset( str, ' ', sizeof( str ) );
786 for( i = 1; i < i_level; i++ )
790 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
791 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
793 sprintf( str + i_level * 5,
794 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
796 (char*)&p_chk->common.i_chunk_fourcc,
797 (char*)&p_chk->list.i_type,
798 p_chk->common.i_chunk_size,
799 p_chk->common.i_chunk_pos );
803 sprintf( str + i_level * 5,
804 "+ %4.4s size:"I64Fu" pos:"I64Fu,
805 (char*)&p_chk->common.i_chunk_fourcc,
806 p_chk->common.i_chunk_size,
807 p_chk->common.i_chunk_pos );
809 msg_Dbg( p_obj, "%s", str );
811 p_child = p_chk->common.p_first;
814 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
815 p_child = p_child->common.p_next;
819 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
821 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
823 vlc_bool_t b_seekable;
825 stream_Control( s, STREAM_CAN_FASTSEEK, &b_seekable );
827 p_list->i_chunk_pos = 0;
828 p_list->i_chunk_size = stream_Size( s );
829 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
830 p_list->p_father = NULL;
831 p_list->p_next = NULL;
832 p_list->p_first = NULL;
833 p_list->p_last = NULL;
835 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
839 p_chk = malloc( sizeof( avi_chunk_t ) );
840 memset( p_chk, 0, sizeof( avi_chunk_t ) );
841 if( !p_root->common.p_first )
843 p_root->common.p_first = p_chk;
847 p_root->common.p_last->common.p_next = p_chk;
849 p_root->common.p_last = p_chk;
851 if( AVI_ChunkRead( s, p_chk, p_root ) ||
852 ( stream_Tell( s ) >=
853 (off_t)p_chk->common.p_father->common.i_chunk_pos +
854 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
858 /* If we can't seek then stop when we 've found first RIFF-AVI */
859 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
860 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
866 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
870 void AVI_ChunkFreeRoot( stream_t *s,
873 AVI_ChunkFree( s, p_chk );
877 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
880 avi_chunk_t *p_child;
888 p_child = p_chk->common.p_first;
891 if( p_child->common.i_chunk_fourcc == i_fourcc ||
892 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
893 p_child->list.i_type == i_fourcc ) )
897 p_child = p_child->common.p_next;
902 void *_AVI_ChunkFind( avi_chunk_t *p_chk,
903 vlc_fourcc_t i_fourcc, int i_number )
905 avi_chunk_t *p_child;
910 p_child = p_chk->common.p_first;
914 if( p_child->common.i_chunk_fourcc == i_fourcc ||
915 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
916 p_child->list.i_type == i_fourcc ) )
926 p_child = p_child->common.p_next;