demux: libavi: force chunk type check
[vlc.git] / modules / demux / avi / libavi.c
blobe529348b574248cb60d9911c6f9fddb05adcc532
1 /*****************************************************************************
2 * libavi.c : LibAVI
3 *****************************************************************************
4 * Copyright (C) 2001 VLC authors and VideoLAN
5 * $Id$
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <vlc_common.h>
29 #include <vlc_demux.h> /* stream_*, *_ES */
30 #include <vlc_codecs.h> /* VLC_BITMAPINFOHEADER */
32 #include "libavi.h"
34 #include <limits.h>
36 #ifndef NDEBUG
37 # define AVI_DEBUG 1
38 #endif
40 #define __EVEN( x ) (((x) + 1) & ~1)
42 static vlc_fourcc_t GetFOURCC( const uint8_t *p_buff )
44 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
47 static uint64_t AVI_ChunkSize( const avi_chunk_t *p_ck )
49 return __EVEN(p_ck->common.i_chunk_size) + 8;
52 static uint64_t AVI_ChunkEnd( const avi_chunk_t *p_ck )
54 return p_ck->common.i_chunk_pos + AVI_ChunkSize( p_ck );
57 /****************************************************************************
59 * Basics functions to manipulates chunks
61 ****************************************************************************/
62 static int AVI_ChunkReadCommon( stream_t *s, avi_chunk_t *p_chk,
63 const avi_chunk_t *p_father )
65 const uint8_t *p_peek;
67 memset( p_chk, 0, sizeof( avi_chunk_t ) );
69 const uint64_t i_pos = vlc_stream_Tell( s );
70 if( vlc_stream_Peek( s, &p_peek, 8 ) < 8 )
72 if( stream_Size( s ) > 0 && (uint64_t) stream_Size( s ) > i_pos )
73 msg_Warn( s, "can't peek at %"PRIu64, i_pos );
74 else
75 msg_Dbg( s, "no more data at %"PRIu64, i_pos );
76 return VLC_EGENERIC;
79 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
80 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
81 p_chk->common.i_chunk_pos = i_pos;
83 if( p_chk->common.i_chunk_size >= UINT64_MAX - 8 ||
84 p_chk->common.i_chunk_pos > UINT64_MAX - 8 ||
85 UINT64_MAX - p_chk->common.i_chunk_pos - 8 < __EVEN(p_chk->common.i_chunk_size) )
86 return VLC_EGENERIC;
88 if( p_father && AVI_ChunkEnd( p_chk ) > AVI_ChunkEnd( p_father ) )
90 msg_Warn( s, "chunk %4.4s does not fit into parent %ld",
91 (char*)&p_chk->common.i_chunk_fourcc, AVI_ChunkEnd( p_father ) );
92 return VLC_EGENERIC;
95 #ifdef AVI_DEBUG
96 msg_Dbg( (vlc_object_t*)s,
97 "found chunk, fourcc: %4.4s size:%"PRIu64" pos:%"PRIu64,
98 (char*)&p_chk->common.i_chunk_fourcc,
99 p_chk->common.i_chunk_size,
100 p_chk->common.i_chunk_pos );
101 #endif
102 return VLC_SUCCESS;
105 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
107 avi_chunk_t chk;
109 if( !p_chk )
111 if( AVI_ChunkReadCommon( s, &chk, p_chk->common.p_father ) )
113 return VLC_EGENERIC;
115 p_chk = &chk;
118 bool b_seekable = false;
119 const uint64_t i_offset = AVI_ChunkEnd( p_chk );
120 if ( !vlc_stream_Control(s, STREAM_CAN_SEEK, &b_seekable) && b_seekable )
122 return vlc_stream_Seek( s, i_offset );
124 else
126 ssize_t i_read = i_offset - vlc_stream_Tell( s );
127 return (i_read >=0 && vlc_stream_Read( s, NULL, i_read ) == i_read) ?
128 VLC_SUCCESS : VLC_EGENERIC;
132 /****************************************************************************
134 * Functions to read chunks
136 ****************************************************************************/
137 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
139 avi_chunk_t *p_chk;
140 const uint8_t *p_peek;
141 bool b_seekable;
142 int i_ret = VLC_SUCCESS;
144 if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 4 )
146 /* empty box */
147 msg_Warn( (vlc_object_t*)s, "empty list chunk" );
148 return VLC_EGENERIC;
150 if( vlc_stream_Peek( s, &p_peek, 12 ) < 12 )
152 msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
153 return VLC_EGENERIC;
156 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
158 p_container->list.i_type = GetFOURCC( p_peek + 8 );
160 /* XXX fixed for on2 hack */
161 if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
163 p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
164 p_container->list.i_type = AVIFOURCC_AVI;
167 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
168 p_container->list.i_type == AVIFOURCC_movi )
170 if( !b_seekable )
171 return VLC_SUCCESS;
172 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
173 return AVI_NextChunk( s, p_container ); /* points at begining of LIST-movi if not seekable */
176 if( vlc_stream_Read( s, NULL, 12 ) != 12 )
178 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
179 return VLC_EGENERIC;
182 #ifdef AVI_DEBUG
183 msg_Dbg( (vlc_object_t*)s,
184 "found LIST chunk: \'%4.4s\'",
185 (char*)&p_container->list.i_type );
186 #endif
187 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
189 union avi_chunk_u **pp_append = &p_container->common.p_first;
190 for( ; ; )
192 p_chk = calloc( 1, sizeof( avi_chunk_t ) );
193 if( !p_chk )
194 return VLC_EGENERIC;
196 i_ret = AVI_ChunkRead( s, p_chk, p_container );
197 if( i_ret )
199 AVI_ChunkClean( s, p_chk );
200 free( p_chk );
201 break;
204 *pp_append = p_chk;
205 while( *pp_append )
206 pp_append = &((*pp_append)->common.p_next);
207 p_container->common.p_last = p_chk;
209 if( p_container->common.i_chunk_size > 0 &&
210 vlc_stream_Tell( s ) >= AVI_ChunkEnd( p_container ) )
212 break;
215 /* If we can't seek then stop when we 've found LIST-movi */
216 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
217 p_chk->list.i_type == AVIFOURCC_movi &&
218 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
220 break;
224 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
226 if ( i_ret == AVI_ZERO_FOURCC ) return i_ret;
227 return VLC_SUCCESS;
230 /* Allow to append indexes after starting playback */
231 int AVI_ChunkFetchIndexes( stream_t *s, avi_chunk_t *p_riff )
233 avi_chunk_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0, true );
234 if ( !p_movi )
235 return VLC_EGENERIC;
237 avi_chunk_t *p_chk;
238 const uint64_t i_indexpos = AVI_ChunkEnd( p_movi );
239 bool b_seekable = false;
240 int i_ret = VLC_SUCCESS;
242 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
243 if ( !b_seekable || vlc_stream_Seek( s, i_indexpos ) )
244 return VLC_EGENERIC;
246 union avi_chunk_u **pp_append = &p_riff->common.p_first;
247 for( ; ; )
249 p_chk = calloc( 1, sizeof( avi_chunk_t ) );
250 if( !p_chk )
252 i_ret = VLC_EGENERIC;
253 break;
256 i_ret = AVI_ChunkRead( s, p_chk, p_riff );
257 if( i_ret )
259 AVI_ChunkClean( s, p_chk );
260 free( p_chk );
261 break;
264 *pp_append = p_chk;
265 while( *pp_append )
266 pp_append = &((*pp_append)->common.p_next);
267 p_riff->common.p_last = p_chk;
269 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
270 ( vlc_stream_Tell( s ) >
271 p_chk->common.p_father->common.i_chunk_pos +
272 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
274 break;
277 /* If we can't seek then stop when we 've found any index */
278 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_indx ||
279 p_chk->common.i_chunk_fourcc == AVIFOURCC_idx1 )
281 break;
286 return i_ret;
289 #define AVI_READCHUNK_ENTER \
290 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
291 if( i_read > 100000000 ) \
293 msg_Err( s, "Big chunk ignored" ); \
294 return VLC_EGENERIC; \
296 uint8_t *p_read, *p_buff; \
297 if( !( p_read = p_buff = malloc(i_read ) ) ) \
299 return VLC_EGENERIC; \
301 i_read = vlc_stream_Read( s, p_read, i_read ); \
302 if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
304 free( p_buff ); \
305 return VLC_EGENERIC; \
307 p_read += 8; \
308 i_read -= 8
310 #define AVI_READ( res, func, size ) \
311 if( i_read < size ) { \
312 free( p_buff); \
313 return VLC_EGENERIC; \
315 i_read -= size; \
316 res = func( p_read ); \
317 p_read += size \
319 #define AVI_READCHUNK_EXIT( code ) \
320 do { \
321 free( p_buff ); \
322 return code; \
323 } while(0)
325 static inline uint8_t GetB( uint8_t *ptr )
327 return *ptr;
330 #define AVI_READ1BYTE( i_byte ) \
331 AVI_READ( i_byte, GetB, 1 )
333 #define AVI_READ2BYTES( i_word ) \
334 AVI_READ( i_word, GetWLE, 2 )
336 #define AVI_READ4BYTES( i_dword ) \
337 AVI_READ( i_dword, GetDWLE, 4 )
339 #define AVI_READ8BYTES( i_qword ) \
340 AVI_READ( i_qword, GetQWLE, 8 )
342 #define AVI_READFOURCC( i_dword ) \
343 AVI_READ( i_dword, GetFOURCC, 4 )
345 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
347 AVI_READCHUNK_ENTER;
349 p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
350 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
351 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
352 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
353 AVI_READ4BYTES( p_chk->avih.i_flags );
354 AVI_READ4BYTES( p_chk->avih.i_totalframes );
355 AVI_READ4BYTES( p_chk->avih.i_initialframes );
356 AVI_READ4BYTES( p_chk->avih.i_streams );
357 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
358 AVI_READ4BYTES( p_chk->avih.i_width );
359 AVI_READ4BYTES( p_chk->avih.i_height );
360 AVI_READ4BYTES( p_chk->avih.i_scale );
361 AVI_READ4BYTES( p_chk->avih.i_rate );
362 AVI_READ4BYTES( p_chk->avih.i_start );
363 AVI_READ4BYTES( p_chk->avih.i_length );
364 #ifdef AVI_DEBUG
365 msg_Dbg( (vlc_object_t*)s,
366 "avih: streams:%d flags:%s%s%s%s %dx%d",
367 p_chk->avih.i_streams,
368 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
369 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
370 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
371 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
372 p_chk->avih.i_width, p_chk->avih.i_height );
373 #endif
374 AVI_READCHUNK_EXIT( VLC_SUCCESS );
377 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
379 AVI_READCHUNK_ENTER;
381 AVI_READFOURCC( p_chk->strh.i_type );
382 AVI_READFOURCC( p_chk->strh.i_handler );
383 AVI_READ4BYTES( p_chk->strh.i_flags );
384 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
385 AVI_READ4BYTES( p_chk->strh.i_initialframes );
386 AVI_READ4BYTES( p_chk->strh.i_scale );
387 AVI_READ4BYTES( p_chk->strh.i_rate );
388 AVI_READ4BYTES( p_chk->strh.i_start );
389 AVI_READ4BYTES( p_chk->strh.i_length );
390 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
391 AVI_READ4BYTES( p_chk->strh.i_quality );
392 AVI_READ4BYTES( p_chk->strh.i_samplesize );
393 #ifdef AVI_DEBUG
394 msg_Dbg( (vlc_object_t*)s,
395 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
396 (char*)&p_chk->strh.i_type,
397 p_chk->strh.i_handler,
398 p_chk->strh.i_samplesize,
399 ( p_chk->strh.i_scale ?
400 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
401 #endif
403 AVI_READCHUNK_EXIT( VLC_SUCCESS );
406 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
408 avi_chunk_t *p_strh;
410 AVI_READCHUNK_ENTER;
411 if( p_chk->common.p_father == NULL )
413 msg_Err( (vlc_object_t*)s, "malformed avi file" );
414 AVI_READCHUNK_EXIT( VLC_EGENERIC );
416 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0, false ) ) )
418 msg_Err( (vlc_object_t*)s, "malformed avi file" );
419 AVI_READCHUNK_EXIT( p_chk->common.i_chunk_size > 0 ? VLC_EGENERIC : AVI_ZEROSIZED_CHUNK );
422 switch( p_strh->strh.i_type )
424 case( AVIFOURCC_auds ):
425 p_chk->strf.auds.i_cat = AUDIO_ES;
426 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
427 if ( !p_chk->strf.auds.p_wf )
429 AVI_READCHUNK_EXIT( VLC_ENOMEM );
431 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
432 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
433 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
434 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
435 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
436 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
438 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
439 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
441 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
443 /* prevent segfault */
444 if( p_chk->strf.auds.p_wf->cbSize >
445 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
447 p_chk->strf.auds.p_wf->cbSize =
448 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
451 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
453 msg_Dbg( s, "Extended header found" );
456 else
458 p_chk->strf.auds.p_wf->cbSize = 0;
460 if( p_chk->strf.auds.p_wf->cbSize > 0 )
462 memcpy( &p_chk->strf.auds.p_wf[1] ,
463 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourcc+size */
464 p_chk->strf.auds.p_wf->cbSize );
466 #ifdef AVI_DEBUG
467 msg_Dbg( (vlc_object_t*)s,
468 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkbps",
469 p_chk->strf.auds.p_wf->wFormatTag,
470 p_chk->strf.auds.p_wf->nChannels,
471 p_chk->strf.auds.p_wf->nSamplesPerSec,
472 p_chk->strf.auds.p_wf->wBitsPerSample,
473 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1000 );
474 #endif
475 break;
476 case( AVIFOURCC_vids ):
477 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
478 p_chk->strf.vids.i_cat = VIDEO_ES;
479 p_chk->strf.vids.p_bih = malloc( __MAX( p_chk->common.i_chunk_size,
480 sizeof( *p_chk->strf.vids.p_bih ) ) );
481 if ( !p_chk->strf.vids.p_bih )
483 AVI_READCHUNK_EXIT( VLC_ENOMEM );
485 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
486 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
487 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
488 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
489 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
490 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
491 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
492 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
493 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
494 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
495 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
496 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
498 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
500 if ( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
502 uint64_t i_extrasize = p_chk->common.i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
504 /* There's a color palette appended, set up VLC_BITMAPINFO */
505 memcpy( &p_chk->strf.vids.p_bih[1],
506 p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
507 i_extrasize );
509 if ( !p_chk->strf.vids.p_bih->biClrUsed )
511 if( p_chk->strf.vids.p_bih->biBitCount < 32 )
512 p_chk->strf.vids.p_bih->biClrUsed = (1 << p_chk->strf.vids.p_bih->biBitCount);
513 else
514 p_chk->strf.vids.p_bih->biBitCount = UINT16_MAX;
517 if( i_extrasize / sizeof(uint32_t) > UINT32_MAX )
518 p_chk->strf.vids.p_bih->biClrUsed = UINT32_MAX;
519 else
521 p_chk->strf.vids.p_bih->biClrUsed =
522 __MIN( i_extrasize / sizeof(uint32_t),
523 p_chk->strf.vids.p_bih->biClrUsed );
526 /* stay within VLC's limits */
527 p_chk->strf.vids.p_bih->biClrUsed =
528 __MIN( VIDEO_PALETTE_COLORS_MAX, p_chk->strf.vids.p_bih->biClrUsed );
530 else p_chk->strf.vids.p_bih->biClrUsed = 0;
531 #ifdef AVI_DEBUG
532 msg_Dbg( (vlc_object_t*)s,
533 "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
534 (char*)&p_chk->strf.vids.p_bih->biCompression,
535 (uint32_t)p_chk->strf.vids.p_bih->biWidth,
536 (uint32_t)p_chk->strf.vids.p_bih->biHeight,
537 p_chk->strf.vids.p_bih->biPlanes,
538 p_chk->strf.vids.p_bih->biBitCount );
539 #endif
540 break;
541 case AVIFOURCC_iavs:
542 case AVIFOURCC_ivas:
543 p_chk->strf.common.i_cat = UNKNOWN_ES;
544 break;
545 case( AVIFOURCC_txts ):
546 p_chk->strf.common.i_cat = SPU_ES;
547 break;
548 default:
549 msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
550 (char*)&p_strh->strh.i_type );
551 p_chk->strf.common.i_cat = UNKNOWN_ES;
552 break;
554 AVI_READCHUNK_EXIT( VLC_SUCCESS );
556 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
558 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
559 if( p_strf->common.i_cat == AUDIO_ES )
561 FREENULL( p_strf->auds.p_wf );
563 else if( p_strf->common.i_cat == VIDEO_ES )
565 FREENULL( p_strf->vids.p_bih );
569 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
571 if ( p_chk->common.i_chunk_size == 0 )
573 msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
574 return AVI_ZEROSIZED_CHUNK;
577 AVI_READCHUNK_ENTER;
578 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
579 if( p_chk->strd.p_data )
580 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
581 AVI_READCHUNK_EXIT( p_chk->strd.p_data ? VLC_SUCCESS : VLC_EGENERIC );
584 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
586 free( p_chk->strd.p_data );
589 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
591 unsigned int i_count, i_index;
593 AVI_READCHUNK_ENTER;
595 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
597 p_chk->idx1.i_entry_count = i_count;
598 p_chk->idx1.i_entry_max = i_count;
599 if( i_count > 0 )
601 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
602 if( !p_chk->idx1.entry )
603 AVI_READCHUNK_EXIT( VLC_EGENERIC );
605 for( i_index = 0; i_index < i_count ; i_index++ )
607 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
608 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
609 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
610 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
613 else
615 p_chk->idx1.entry = NULL;
617 #ifdef AVI_DEBUG
618 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
619 #endif
620 AVI_READCHUNK_EXIT( VLC_SUCCESS );
623 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
625 p_chk->idx1.i_entry_count = 0;
626 p_chk->idx1.i_entry_max = 0;
627 FREENULL( p_chk->idx1.entry );
632 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
634 unsigned int i_count, i;
635 int i_ret = VLC_SUCCESS;
636 int32_t i_dummy;
637 VLC_UNUSED(i_dummy);
638 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
640 AVI_READCHUNK_ENTER;
642 AVI_READ2BYTES( p_indx->i_longsperentry );
643 AVI_READ1BYTE ( p_indx->i_indexsubtype );
644 AVI_READ1BYTE ( p_indx->i_indextype );
645 AVI_READ4BYTES( p_indx->i_entriesinuse );
647 AVI_READ4BYTES( p_indx->i_id );
648 p_indx->idx.std = NULL;
649 p_indx->idx.field = NULL;
650 p_indx->idx.super = NULL;
652 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
654 AVI_READ8BYTES( p_indx->i_baseoffset );
655 AVI_READ4BYTES( i_dummy );
657 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
658 p_indx->i_entriesinuse = i_count;
659 p_indx->idx.std = calloc( i_count, sizeof( indx_std_entry_t ) );
660 if( i_count == 0 || p_indx->idx.std )
662 for( i = 0; i < i_count; i++ )
664 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
665 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
668 else i_ret = VLC_EGENERIC;
670 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
672 AVI_READ8BYTES( p_indx->i_baseoffset );
673 AVI_READ4BYTES( i_dummy );
675 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
676 p_indx->i_entriesinuse = i_count;
677 p_indx->idx.field = calloc( i_count, sizeof( indx_field_entry_t ) );
678 if( i_count == 0 || p_indx->idx.field )
680 for( i = 0; i < i_count; i++ )
682 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
683 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
684 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
687 else i_ret = VLC_EGENERIC;
689 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
691 p_indx->i_baseoffset = 0;
692 AVI_READ4BYTES( i_dummy );
693 AVI_READ4BYTES( i_dummy );
694 AVI_READ4BYTES( i_dummy );
696 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
697 p_indx->i_entriesinuse = i_count;
698 p_indx->idx.super = calloc( i_count, sizeof( indx_super_entry_t ) );
699 if( i_count == 0 || p_indx->idx.super )
701 for( i = 0; i < i_count; i++ )
703 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
704 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
705 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
708 else i_ret = VLC_EGENERIC;
710 else
712 msg_Warn( (vlc_object_t*)s, "unknown type/subtype index" );
715 #ifdef AVI_DEBUG
716 msg_Dbg( (vlc_object_t*)s, "indx: type=%d subtype=%d entry=%d",
717 p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
718 #endif
719 AVI_READCHUNK_EXIT( i_ret );
721 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
723 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
725 FREENULL( p_indx->idx.std );
726 FREENULL( p_indx->idx.field );
727 FREENULL( p_indx->idx.super );
730 static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
732 avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;
734 AVI_READCHUNK_ENTER;
736 AVI_READ4BYTES( p_vprp->i_video_format_token );
737 AVI_READ4BYTES( p_vprp->i_video_standard );
738 AVI_READ4BYTES( p_vprp->i_vertical_refresh );
739 AVI_READ4BYTES( p_vprp->i_h_total_in_t );
740 AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
741 AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
742 AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
743 AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
744 AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
745 for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
747 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
748 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
749 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
750 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
751 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
752 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
753 AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
754 AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
757 #ifdef AVI_DEBUG
758 msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
759 p_vprp->i_video_format_token, p_vprp->i_video_standard );
760 #endif
761 AVI_READCHUNK_EXIT( VLC_SUCCESS );
764 static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
766 avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
768 AVI_READCHUNK_ENTER;
770 AVI_READ4BYTES( p_dmlh->dwTotalFrames );
772 #ifdef AVI_DEBUG
773 msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
774 p_dmlh->dwTotalFrames );
775 #endif
776 AVI_READCHUNK_EXIT( VLC_SUCCESS );
779 static const struct
781 vlc_fourcc_t i_fourcc;
782 const char *psz_type;
783 } AVI_strz_type[] =
785 { AVIFOURCC_IARL, "Archive location" },
786 { AVIFOURCC_IART, "Artist" },
787 { AVIFOURCC_ICMS, "Commisioned" },
788 { AVIFOURCC_ICMT, "Comments" },
789 { AVIFOURCC_ICOP, "Copyright" },
790 { AVIFOURCC_ICRD, "Creation date" },
791 { AVIFOURCC_ICRP, "Cropped" },
792 { AVIFOURCC_IDIM, "Dimensions" },
793 { AVIFOURCC_IDPI, "Dots per inch" },
794 { AVIFOURCC_IENG, "Engineer" },
795 { AVIFOURCC_IGNR, "Genre" },
796 { AVIFOURCC_ISGN, "Secondary Genre" },
797 { AVIFOURCC_IKEY, "Keywords" },
798 { AVIFOURCC_ILGT, "Lightness" },
799 { AVIFOURCC_IMED, "Medium" },
800 { AVIFOURCC_INAM, "Name" },
801 { AVIFOURCC_IPLT, "Palette setting" },
802 { AVIFOURCC_IPRD, "Product" },
803 { AVIFOURCC_ISBJ, "Subject" },
804 { AVIFOURCC_ISFT, "Software" },
805 { AVIFOURCC_ISHP, "Sharpness" },
806 { AVIFOURCC_ISRC, "Source" },
807 { AVIFOURCC_ISRF, "Source form" },
808 { AVIFOURCC_ITCH, "Technician" },
809 { AVIFOURCC_ISMP, "Time code" },
810 { AVIFOURCC_IDIT, "Digitalization time" },
811 { AVIFOURCC_IWRI, "Writer" },
812 { AVIFOURCC_IPRO, "Producer" },
813 { AVIFOURCC_ICNM, "Cinematographer" },
814 { AVIFOURCC_IPDS, "Production designer" },
815 { AVIFOURCC_IEDT, "Editor" },
816 { AVIFOURCC_ICDS, "Costume designer" },
817 { AVIFOURCC_IMUS, "Music" },
818 { AVIFOURCC_ISTD, "Production studio" },
819 { AVIFOURCC_IDST, "Distributor" },
820 { AVIFOURCC_ICNT, "Country" },
821 { AVIFOURCC_ISTR, "Starring" },
822 { AVIFOURCC_IFRM, "Total number of parts" },
823 { AVIFOURCC_strn, "Stream name" },
824 { AVIFOURCC_IAS1, "First Language" },
825 { AVIFOURCC_IAS2, "Second Language" },
826 { AVIFOURCC_IAS3, "Third Language" },
827 { AVIFOURCC_IAS4, "Fourth Language" },
828 { AVIFOURCC_IAS5, "Fifth Language" },
829 { AVIFOURCC_IAS6, "Sixth Language" },
830 { AVIFOURCC_IAS7, "Seventh Language" },
831 { AVIFOURCC_IAS8, "Eighth Language" },
832 { AVIFOURCC_IAS9, "Ninth Language" },
834 { 0, "???" }
837 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
839 int i_index;
840 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
841 AVI_READCHUNK_ENTER;
843 for( i_index = 0;; i_index++)
845 if( !AVI_strz_type[i_index].i_fourcc ||
846 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
848 break;
851 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
852 p_strz->p_str = malloc( p_strz->i_chunk_size + 1 );
853 if( !p_strz->p_type || !p_strz->p_str )
855 free( p_strz->p_type );
856 free( p_strz->p_str );
857 AVI_READCHUNK_EXIT( VLC_EGENERIC );
859 memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
860 p_strz->p_str[p_strz->i_chunk_size] = 0;
862 #ifdef AVI_DEBUG
863 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
864 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
865 #endif
866 AVI_READCHUNK_EXIT( VLC_SUCCESS );
868 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
870 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
871 FREENULL( p_strz->p_type );
872 FREENULL( p_strz->p_str );
875 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
877 return AVI_NextChunk( s, p_chk );
879 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
881 VLC_UNUSED( p_chk );
884 static const struct
886 vlc_fourcc_t i_fourcc;
887 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
888 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
889 } AVI_Chunk_Function [] =
891 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
892 { AVIFOURCC_ON2, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
893 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
894 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
895 { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
896 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
897 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
898 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
899 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
900 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
901 { AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
902 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
903 { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
905 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
906 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
907 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
908 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
909 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
910 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
911 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
912 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
913 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
914 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
915 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
916 { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
917 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
918 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
919 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
920 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
921 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
922 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
923 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
924 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
925 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
926 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
927 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
928 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
929 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
930 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
931 { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
932 { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
933 { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
934 { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
935 { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
936 { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
937 { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
938 { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
939 { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
940 { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
941 { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
942 { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
943 { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
944 { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
945 { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
946 { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
947 { AVIFOURCC_IAS1, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
948 { AVIFOURCC_IAS2, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
949 { AVIFOURCC_IAS3, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
950 { AVIFOURCC_IAS4, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
951 { AVIFOURCC_IAS5, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
952 { AVIFOURCC_IAS6, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
953 { AVIFOURCC_IAS7, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
954 { AVIFOURCC_IAS8, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
955 { AVIFOURCC_IAS9, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
958 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
959 { 0, NULL, NULL }
962 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
964 unsigned int i_index;
965 for( i_index = 0; ; i_index++ )
967 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
968 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
970 return i_index;
975 int AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
977 int i_index;
979 if( !p_chk )
981 msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
982 return VLC_EGENERIC;
985 if( AVI_ChunkReadCommon( s, p_chk, p_father ) )
986 return VLC_EGENERIC;
988 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
990 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
991 return AVI_ZERO_FOURCC;
993 p_chk->common.p_father = p_father;
995 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
996 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
998 int i_return = AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
999 if ( i_return == AVI_ZEROSIZED_CHUNK || i_return == AVI_ZERO_FOURCC )
1001 if ( !p_father )
1002 return VLC_EGENERIC;
1003 p_chk->common.i_chunk_fourcc = 0;
1004 return AVI_NextChunk( s, ( i_return == AVI_ZEROSIZED_CHUNK ) ? p_chk : p_father );
1006 return i_return;
1008 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
1009 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
1010 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
1011 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
1013 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
1014 return AVI_ChunkRead_indx( s, p_chk );
1017 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
1018 (char*)&p_chk->common.i_chunk_fourcc );
1019 return AVI_NextChunk( s, p_chk );
1022 void AVI_ChunkClean( stream_t *s,
1023 avi_chunk_t *p_chk )
1025 int i_index;
1026 avi_chunk_t *p_child, *p_next;
1028 if( !p_chk )
1030 return;
1033 /* Free all child chunk */
1034 p_child = p_chk->common.p_first;
1035 while( p_child )
1037 p_next = p_child->common.p_next;
1038 AVI_ChunkClean( s, p_child );
1039 free( p_child );
1040 p_child = p_next;
1043 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
1044 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
1046 #ifdef AVI_DEBUG
1047 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
1048 (char*)&p_chk->common.i_chunk_fourcc );
1049 #endif
1050 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
1052 else if( p_chk->common.i_chunk_fourcc != 0 )
1054 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
1055 (char*)&p_chk->common.i_chunk_fourcc );
1057 p_chk->common.p_first = NULL;
1058 p_chk->common.p_last = NULL;
1060 return;
1063 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
1064 avi_chunk_t *p_chk, unsigned i_level )
1066 avi_chunk_t *p_child;
1068 char str[512];
1069 if( i_level >= (sizeof(str) - 1)/4 )
1070 return;
1072 memset( str, ' ', sizeof( str ) );
1073 for( unsigned i = 1; i < i_level; i++ )
1075 str[i * 4] = '|';
1077 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
1078 p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
1079 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1081 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1082 "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
1083 i_level ? '+' : '*',
1084 (char*)&p_chk->common.i_chunk_fourcc,
1085 (char*)&p_chk->list.i_type,
1086 p_chk->common.i_chunk_size,
1087 p_chk->common.i_chunk_pos );
1089 else
1091 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1092 "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
1093 (char*)&p_chk->common.i_chunk_fourcc,
1094 p_chk->common.i_chunk_size,
1095 p_chk->common.i_chunk_pos );
1097 msg_Dbg( p_obj, "%s", str );
1099 p_child = p_chk->common.p_first;
1100 while( p_child )
1102 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
1103 p_child = p_child->common.p_next;
1107 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
1109 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
1110 avi_chunk_t *p_chk;
1111 bool b_seekable;
1113 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
1115 p_list->i_chunk_pos = 0;
1116 p_list->i_chunk_size = ((UINT64_MAX - 12) >> 1) << 1;
1117 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
1118 p_list->p_father = NULL;
1119 p_list->p_next = NULL;
1120 p_list->p_first = NULL;
1121 p_list->p_last = NULL;
1123 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
1125 union avi_chunk_u **pp_append = &p_root->common.p_first;
1126 for( ; ; )
1128 p_chk = calloc( 1, sizeof( avi_chunk_t ) );
1129 if( !p_chk )
1130 return VLC_EGENERIC;
1132 if( AVI_ChunkRead( s, p_chk, p_root ) != VLC_SUCCESS )
1134 AVI_ChunkClean( s, p_chk );
1135 free( p_chk );
1136 break;
1138 else if( vlc_stream_Tell( s ) >=
1139 p_chk->common.p_father->common.i_chunk_pos +
1140 __EVEN( p_chk->common.p_father->common.i_chunk_size ) )
1142 break;
1145 *pp_append = p_chk;
1146 while( *pp_append )
1147 pp_append = &((*pp_append)->common.p_next);
1148 p_root->common.p_last = p_chk;
1150 /* If we can't seek then stop when we 've found first RIFF-AVI */
1151 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
1152 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
1154 break;
1158 p_list->i_chunk_size = stream_Size( s );
1160 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
1161 return VLC_SUCCESS;
1164 void AVI_ChunkFreeRoot( stream_t *s,
1165 avi_chunk_t *p_chk )
1167 AVI_ChunkClean( s, p_chk );
1171 int AVI_ChunkCount_( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc, bool b_list )
1173 if( !p_chk )
1174 return 0;
1176 int i_count = 0;
1177 for( avi_chunk_t *p_child = p_chk->common.p_first;
1178 p_child; p_child = p_child->common.p_next )
1180 if( b_list && p_child->list.i_type == 0 )
1181 continue;
1183 if( p_child->common.i_chunk_fourcc != i_fourcc &&
1184 p_child->list.i_type != i_fourcc )
1185 continue;
1187 i_count++;
1190 return i_count;
1193 void *AVI_ChunkFind_( avi_chunk_t *p_chk,
1194 vlc_fourcc_t i_fourcc, int i_number, bool b_list )
1196 if( !p_chk )
1197 return NULL;
1199 for( avi_chunk_t *p_child = p_chk->common.p_first;
1200 p_child; p_child = p_child->common.p_next )
1202 if( b_list && p_child->list.i_type == 0 )
1203 continue;
1205 if( p_child->common.i_chunk_fourcc != i_fourcc &&
1206 p_child->list.i_type != i_fourcc )
1207 continue;
1209 if( i_number-- == 0 )
1210 return p_child; /* We found it */
1213 return NULL;