mux:ts: convert vlc_tick_t to seconds explicitly using SEC_FROM_VLC_TICK()
[vlc.git] / modules / demux / avi / libavi.c
bloba622b9a3cda308c7dca663e122f0f932d5e4030b
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 ) );
93 /* How hard is to produce files with the correct declared size ? */
94 if( p_father->common.i_chunk_fourcc != AVIFOURCC_RIFF ||
95 p_father->common.p_father == NULL ||
96 p_father->common.p_father->common.p_father != NULL ) /* Root > RIFF only */
97 return VLC_EGENERIC;
100 #ifdef AVI_DEBUG
101 msg_Dbg( (vlc_object_t*)s,
102 "found chunk, fourcc: %4.4s size:%"PRIu64" pos:%"PRIu64,
103 (char*)&p_chk->common.i_chunk_fourcc,
104 p_chk->common.i_chunk_size,
105 p_chk->common.i_chunk_pos );
106 #endif
107 return VLC_SUCCESS;
110 static int AVI_GotoNextChunk( stream_t *s, const avi_chunk_t *p_chk )
112 bool b_seekable = false;
113 const uint64_t i_offset = AVI_ChunkEnd( p_chk );
114 if ( !vlc_stream_Control(s, STREAM_CAN_SEEK, &b_seekable) && b_seekable )
116 return vlc_stream_Seek( s, i_offset );
118 else
120 ssize_t i_read = i_offset - vlc_stream_Tell( s );
121 return (i_read >=0 && vlc_stream_Read( s, NULL, i_read ) == i_read) ?
122 VLC_SUCCESS : VLC_EGENERIC;
126 static int AVI_NextChunk( stream_t *s, avi_chunk_t *p_chk )
128 avi_chunk_t chk;
130 if( !p_chk )
132 if( AVI_ChunkReadCommon( s, &chk, NULL ) )
134 return VLC_EGENERIC;
136 p_chk = &chk;
139 return AVI_GotoNextChunk( s, p_chk );
142 /****************************************************************************
144 * Functions to read chunks
146 ****************************************************************************/
147 static int AVI_ChunkRead_list( stream_t *s, avi_chunk_t *p_container )
149 avi_chunk_t *p_chk;
150 const uint8_t *p_peek;
151 bool b_seekable;
152 int i_ret = VLC_SUCCESS;
154 if( p_container->common.i_chunk_size > 0 && p_container->common.i_chunk_size < 4 )
156 /* empty box */
157 msg_Warn( (vlc_object_t*)s, "empty list chunk" );
158 return VLC_EGENERIC;
160 if( vlc_stream_Peek( s, &p_peek, 12 ) < 12 )
162 msg_Warn( (vlc_object_t*)s, "cannot peek while reading list chunk" );
163 return VLC_EGENERIC;
166 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
168 p_container->list.i_type = GetFOURCC( p_peek + 8 );
170 /* XXX fixed for on2 hack */
171 if( p_container->common.i_chunk_fourcc == AVIFOURCC_ON2 && p_container->list.i_type == AVIFOURCC_ON2f )
173 p_container->common.i_chunk_fourcc = AVIFOURCC_RIFF;
174 p_container->list.i_type = AVIFOURCC_AVI;
177 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
178 p_container->list.i_type == AVIFOURCC_movi )
180 if( !b_seekable )
181 return VLC_SUCCESS;
182 msg_Dbg( (vlc_object_t*)s, "skipping movi chunk" );
183 return AVI_NextChunk( s, p_container ); /* points at begining of LIST-movi if not seekable */
186 if( vlc_stream_Read( s, NULL, 12 ) != 12 )
188 msg_Warn( (vlc_object_t*)s, "cannot enter chunk" );
189 return VLC_EGENERIC;
192 #ifdef AVI_DEBUG
193 msg_Dbg( (vlc_object_t*)s,
194 "found LIST chunk: \'%4.4s\'",
195 (char*)&p_container->list.i_type );
196 #endif
197 msg_Dbg( (vlc_object_t*)s, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
199 union avi_chunk_u **pp_append = &p_container->common.p_first;
200 for( ; ; )
202 p_chk = calloc( 1, sizeof( avi_chunk_t ) );
203 if( !p_chk )
204 return VLC_EGENERIC;
206 i_ret = AVI_ChunkRead( s, p_chk, p_container );
207 if( i_ret )
209 AVI_ChunkClean( s, p_chk );
210 free( p_chk );
211 break;
214 *pp_append = p_chk;
215 while( *pp_append )
216 pp_append = &((*pp_append)->common.p_next);
218 if( p_container->common.i_chunk_size > 0 &&
219 vlc_stream_Tell( s ) >= AVI_ChunkEnd( p_container ) )
221 break;
224 /* If we can't seek then stop when we 've found LIST-movi */
225 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
226 p_chk->list.i_type == AVIFOURCC_movi &&
227 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
229 break;
233 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>%x", (char*)&p_container->list.i_type, i_ret );
235 if( i_ret == AVI_ZERO_FOURCC || i_ret == AVI_ZEROSIZED_CHUNK )
236 return AVI_GotoNextChunk( s, p_container );
238 return VLC_SUCCESS;
241 /* Allow to append indexes after starting playback */
242 int AVI_ChunkFetchIndexes( stream_t *s, avi_chunk_t *p_riff )
244 avi_chunk_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0, true );
245 if ( !p_movi )
246 return VLC_EGENERIC;
248 avi_chunk_t *p_chk;
249 const uint64_t i_indexpos = AVI_ChunkEnd( p_movi );
250 bool b_seekable = false;
251 int i_ret = VLC_SUCCESS;
253 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
254 if ( !b_seekable || vlc_stream_Seek( s, i_indexpos ) )
255 return VLC_EGENERIC;
257 union avi_chunk_u **pp_append = &p_riff->common.p_first;
258 for( ; ; )
260 p_chk = calloc( 1, sizeof( avi_chunk_t ) );
261 if( !p_chk )
263 i_ret = VLC_EGENERIC;
264 break;
267 i_ret = AVI_ChunkRead( s, p_chk, p_riff );
268 if( i_ret )
270 AVI_ChunkClean( s, p_chk );
271 free( p_chk );
272 break;
275 *pp_append = p_chk;
276 while( *pp_append )
277 pp_append = &((*pp_append)->common.p_next);
279 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
280 ( vlc_stream_Tell( s ) >
281 p_chk->common.p_father->common.i_chunk_pos +
282 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
284 break;
287 /* If we can't seek then stop when we 've found any index */
288 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_indx ||
289 p_chk->common.i_chunk_fourcc == AVIFOURCC_idx1 )
291 break;
296 return i_ret;
299 #define AVI_READCHUNK_ENTER \
300 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
301 if( i_read > 100000000 ) \
303 msg_Err( s, "Big chunk ignored" ); \
304 return VLC_EGENERIC; \
306 uint8_t *p_read, *p_buff; \
307 if( !( p_read = p_buff = malloc(i_read ) ) ) \
309 return VLC_EGENERIC; \
311 i_read = vlc_stream_Read( s, p_read, i_read ); \
312 if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
314 free( p_buff ); \
315 return VLC_EGENERIC; \
317 p_read += 8; \
318 i_read -= 8
320 #define AVI_READ( res, func, size ) \
321 if( i_read < size ) { \
322 free( p_buff); \
323 return VLC_EGENERIC; \
325 i_read -= size; \
326 res = func( p_read ); \
327 p_read += size \
329 #define AVI_READCHUNK_EXIT( code ) \
330 do { \
331 free( p_buff ); \
332 return code; \
333 } while(0)
335 static inline uint8_t GetB( uint8_t *ptr )
337 return *ptr;
340 #define AVI_READ1BYTE( i_byte ) \
341 AVI_READ( i_byte, GetB, 1 )
343 #define AVI_READ2BYTES( i_word ) \
344 AVI_READ( i_word, GetWLE, 2 )
346 #define AVI_READ4BYTES( i_dword ) \
347 AVI_READ( i_dword, GetDWLE, 4 )
349 #define AVI_READ8BYTES( i_qword ) \
350 AVI_READ( i_qword, GetQWLE, 8 )
352 #define AVI_READFOURCC( i_dword ) \
353 AVI_READ( i_dword, GetFOURCC, 4 )
355 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
357 AVI_READCHUNK_ENTER;
359 p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
360 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
361 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
362 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
363 AVI_READ4BYTES( p_chk->avih.i_flags );
364 AVI_READ4BYTES( p_chk->avih.i_totalframes );
365 AVI_READ4BYTES( p_chk->avih.i_initialframes );
366 AVI_READ4BYTES( p_chk->avih.i_streams );
367 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
368 AVI_READ4BYTES( p_chk->avih.i_width );
369 AVI_READ4BYTES( p_chk->avih.i_height );
370 AVI_READ4BYTES( p_chk->avih.i_scale );
371 AVI_READ4BYTES( p_chk->avih.i_rate );
372 AVI_READ4BYTES( p_chk->avih.i_start );
373 AVI_READ4BYTES( p_chk->avih.i_length );
374 #ifdef AVI_DEBUG
375 msg_Dbg( (vlc_object_t*)s,
376 "avih: streams:%d flags:%s%s%s%s %dx%d",
377 p_chk->avih.i_streams,
378 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
379 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
380 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
381 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
382 p_chk->avih.i_width, p_chk->avih.i_height );
383 #endif
384 AVI_READCHUNK_EXIT( VLC_SUCCESS );
387 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
389 AVI_READCHUNK_ENTER;
391 AVI_READFOURCC( p_chk->strh.i_type );
392 AVI_READFOURCC( p_chk->strh.i_handler );
393 AVI_READ4BYTES( p_chk->strh.i_flags );
394 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
395 AVI_READ4BYTES( p_chk->strh.i_initialframes );
396 AVI_READ4BYTES( p_chk->strh.i_scale );
397 AVI_READ4BYTES( p_chk->strh.i_rate );
398 AVI_READ4BYTES( p_chk->strh.i_start );
399 AVI_READ4BYTES( p_chk->strh.i_length );
400 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
401 AVI_READ4BYTES( p_chk->strh.i_quality );
402 AVI_READ4BYTES( p_chk->strh.i_samplesize );
403 #ifdef AVI_DEBUG
404 msg_Dbg( (vlc_object_t*)s,
405 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
406 (char*)&p_chk->strh.i_type,
407 p_chk->strh.i_handler,
408 p_chk->strh.i_samplesize,
409 ( p_chk->strh.i_scale ?
410 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
411 #endif
413 AVI_READCHUNK_EXIT( VLC_SUCCESS );
416 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
418 avi_chunk_t *p_strh;
420 AVI_READCHUNK_ENTER;
421 if( p_chk->common.p_father == NULL )
423 msg_Err( (vlc_object_t*)s, "malformed avi file" );
424 AVI_READCHUNK_EXIT( VLC_EGENERIC );
426 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0, false ) ) )
428 msg_Err( (vlc_object_t*)s, "malformed avi file" );
429 AVI_READCHUNK_EXIT( p_chk->common.i_chunk_size > 0 ? VLC_EGENERIC : AVI_ZEROSIZED_CHUNK );
432 switch( p_strh->strh.i_type )
434 case( AVIFOURCC_auds ):
435 p_chk->strf.auds.i_cat = AUDIO_ES;
436 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
437 if ( !p_chk->strf.auds.p_wf )
439 AVI_READCHUNK_EXIT( VLC_ENOMEM );
441 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
442 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
443 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
444 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
445 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
446 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
448 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
449 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
451 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
453 /* prevent segfault */
454 if( p_chk->strf.auds.p_wf->cbSize >
455 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
457 p_chk->strf.auds.p_wf->cbSize =
458 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
461 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
463 msg_Dbg( s, "Extended header found" );
466 else
468 p_chk->strf.auds.p_wf->cbSize = 0;
470 if( p_chk->strf.auds.p_wf->cbSize > 0 )
472 memcpy( &p_chk->strf.auds.p_wf[1] ,
473 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourcc+size */
474 p_chk->strf.auds.p_wf->cbSize );
476 #ifdef AVI_DEBUG
477 msg_Dbg( (vlc_object_t*)s,
478 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkbps",
479 p_chk->strf.auds.p_wf->wFormatTag,
480 p_chk->strf.auds.p_wf->nChannels,
481 p_chk->strf.auds.p_wf->nSamplesPerSec,
482 p_chk->strf.auds.p_wf->wBitsPerSample,
483 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1000 );
484 #endif
485 break;
486 case( AVIFOURCC_vids ):
487 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
488 p_chk->strf.vids.i_cat = VIDEO_ES;
489 p_chk->strf.vids.p_bih = malloc( __MAX( p_chk->common.i_chunk_size,
490 sizeof( *p_chk->strf.vids.p_bih ) ) );
491 if ( !p_chk->strf.vids.p_bih )
493 AVI_READCHUNK_EXIT( VLC_ENOMEM );
495 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
496 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
497 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
498 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
499 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
500 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
501 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
502 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
503 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
504 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
505 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
506 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
508 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
510 if ( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
512 uint64_t i_extrasize = p_chk->common.i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
514 /* There's a color palette appended, set up VLC_BITMAPINFO */
515 memcpy( &p_chk->strf.vids.p_bih[1],
516 p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
517 i_extrasize );
519 if ( !p_chk->strf.vids.p_bih->biClrUsed )
521 if( p_chk->strf.vids.p_bih->biBitCount < 32 )
522 p_chk->strf.vids.p_bih->biClrUsed = (1 << p_chk->strf.vids.p_bih->biBitCount);
523 else
524 p_chk->strf.vids.p_bih->biBitCount = UINT16_MAX;
527 if( i_extrasize / sizeof(uint32_t) > UINT32_MAX )
528 p_chk->strf.vids.p_bih->biClrUsed = UINT32_MAX;
529 else
531 p_chk->strf.vids.p_bih->biClrUsed =
532 __MIN( i_extrasize / sizeof(uint32_t),
533 p_chk->strf.vids.p_bih->biClrUsed );
536 /* stay within VLC's limits */
537 p_chk->strf.vids.p_bih->biClrUsed =
538 __MIN( VIDEO_PALETTE_COLORS_MAX, p_chk->strf.vids.p_bih->biClrUsed );
540 else p_chk->strf.vids.p_bih->biClrUsed = 0;
541 #ifdef AVI_DEBUG
542 msg_Dbg( (vlc_object_t*)s,
543 "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
544 (char*)&p_chk->strf.vids.p_bih->biCompression,
545 (uint32_t)p_chk->strf.vids.p_bih->biWidth,
546 (uint32_t)p_chk->strf.vids.p_bih->biHeight,
547 p_chk->strf.vids.p_bih->biPlanes,
548 p_chk->strf.vids.p_bih->biBitCount );
549 #endif
550 break;
551 case AVIFOURCC_iavs:
552 case AVIFOURCC_ivas:
553 p_chk->strf.common.i_cat = UNKNOWN_ES;
554 break;
555 case( AVIFOURCC_txts ):
556 p_chk->strf.common.i_cat = SPU_ES;
557 break;
558 default:
559 msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
560 (char*)&p_strh->strh.i_type );
561 p_chk->strf.common.i_cat = UNKNOWN_ES;
562 break;
564 AVI_READCHUNK_EXIT( VLC_SUCCESS );
566 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
568 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
569 if( p_strf->common.i_cat == AUDIO_ES )
571 FREENULL( p_strf->auds.p_wf );
573 else if( p_strf->common.i_cat == VIDEO_ES )
575 FREENULL( p_strf->vids.p_bih );
579 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
581 if ( p_chk->common.i_chunk_size == 0 )
583 msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
584 return AVI_ZEROSIZED_CHUNK;
587 AVI_READCHUNK_ENTER;
588 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
589 if( p_chk->strd.p_data )
590 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
591 AVI_READCHUNK_EXIT( p_chk->strd.p_data ? VLC_SUCCESS : VLC_EGENERIC );
594 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
596 free( p_chk->strd.p_data );
599 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
601 unsigned int i_count, i_index;
603 AVI_READCHUNK_ENTER;
605 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
607 p_chk->idx1.i_entry_count = i_count;
608 p_chk->idx1.i_entry_max = i_count;
609 if( i_count > 0 )
611 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
612 if( !p_chk->idx1.entry )
613 AVI_READCHUNK_EXIT( VLC_EGENERIC );
615 for( i_index = 0; i_index < i_count ; i_index++ )
617 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
618 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
619 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
620 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
623 else
625 p_chk->idx1.entry = NULL;
627 #ifdef AVI_DEBUG
628 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
629 #endif
630 AVI_READCHUNK_EXIT( VLC_SUCCESS );
633 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
635 p_chk->idx1.i_entry_count = 0;
636 p_chk->idx1.i_entry_max = 0;
637 FREENULL( p_chk->idx1.entry );
642 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
644 unsigned int i_count, i;
645 int i_ret = VLC_SUCCESS;
646 int32_t i_dummy;
647 VLC_UNUSED(i_dummy);
648 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
650 AVI_READCHUNK_ENTER;
652 AVI_READ2BYTES( p_indx->i_longsperentry );
653 AVI_READ1BYTE ( p_indx->i_indexsubtype );
654 AVI_READ1BYTE ( p_indx->i_indextype );
655 AVI_READ4BYTES( p_indx->i_entriesinuse );
657 AVI_READ4BYTES( p_indx->i_id );
658 p_indx->idx.std = NULL;
659 p_indx->idx.field = NULL;
660 p_indx->idx.super = NULL;
662 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
664 AVI_READ8BYTES( p_indx->i_baseoffset );
665 AVI_READ4BYTES( i_dummy );
667 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
668 p_indx->i_entriesinuse = i_count;
669 p_indx->idx.std = calloc( i_count, sizeof( indx_std_entry_t ) );
670 if( i_count == 0 || p_indx->idx.std )
672 for( i = 0; i < i_count; i++ )
674 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
675 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
678 else i_ret = VLC_EGENERIC;
680 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
682 AVI_READ8BYTES( p_indx->i_baseoffset );
683 AVI_READ4BYTES( i_dummy );
685 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
686 p_indx->i_entriesinuse = i_count;
687 p_indx->idx.field = calloc( i_count, sizeof( indx_field_entry_t ) );
688 if( i_count == 0 || p_indx->idx.field )
690 for( i = 0; i < i_count; i++ )
692 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
693 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
694 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
697 else i_ret = VLC_EGENERIC;
699 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
701 p_indx->i_baseoffset = 0;
702 AVI_READ4BYTES( i_dummy );
703 AVI_READ4BYTES( i_dummy );
704 AVI_READ4BYTES( i_dummy );
706 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
707 p_indx->i_entriesinuse = i_count;
708 p_indx->idx.super = calloc( i_count, sizeof( indx_super_entry_t ) );
709 if( i_count == 0 || p_indx->idx.super )
711 for( i = 0; i < i_count; i++ )
713 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
714 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
715 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
718 else i_ret = VLC_EGENERIC;
720 else
722 msg_Warn( (vlc_object_t*)s, "unknown type/subtype index" );
725 #ifdef AVI_DEBUG
726 msg_Dbg( (vlc_object_t*)s, "indx: type=%d subtype=%d entry=%d",
727 p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
728 #endif
729 AVI_READCHUNK_EXIT( i_ret );
731 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
733 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
735 FREENULL( p_indx->idx.std );
736 FREENULL( p_indx->idx.field );
737 FREENULL( p_indx->idx.super );
740 static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
742 avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;
744 AVI_READCHUNK_ENTER;
746 AVI_READ4BYTES( p_vprp->i_video_format_token );
747 AVI_READ4BYTES( p_vprp->i_video_standard );
748 AVI_READ4BYTES( p_vprp->i_vertical_refresh );
749 AVI_READ4BYTES( p_vprp->i_h_total_in_t );
750 AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
751 AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
752 AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
753 AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
754 AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
755 for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
757 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
758 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
759 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
760 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
761 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
762 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
763 AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
764 AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
767 #ifdef AVI_DEBUG
768 msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
769 p_vprp->i_video_format_token, p_vprp->i_video_standard );
770 #endif
771 AVI_READCHUNK_EXIT( VLC_SUCCESS );
774 static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
776 avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
778 AVI_READCHUNK_ENTER;
780 AVI_READ4BYTES( p_dmlh->dwTotalFrames );
782 #ifdef AVI_DEBUG
783 msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
784 p_dmlh->dwTotalFrames );
785 #endif
786 AVI_READCHUNK_EXIT( VLC_SUCCESS );
789 static const struct
791 vlc_fourcc_t i_fourcc;
792 const char *psz_type;
793 } AVI_strz_type[] =
795 { AVIFOURCC_IARL, "Archive location" },
796 { AVIFOURCC_IART, "Artist" },
797 { AVIFOURCC_ICMS, "Commisioned" },
798 { AVIFOURCC_ICMT, "Comments" },
799 { AVIFOURCC_ICOP, "Copyright" },
800 { AVIFOURCC_ICRD, "Creation date" },
801 { AVIFOURCC_ICRP, "Cropped" },
802 { AVIFOURCC_IDIM, "Dimensions" },
803 { AVIFOURCC_IDPI, "Dots per inch" },
804 { AVIFOURCC_IENG, "Engineer" },
805 { AVIFOURCC_IGNR, "Genre" },
806 { AVIFOURCC_ISGN, "Secondary Genre" },
807 { AVIFOURCC_IKEY, "Keywords" },
808 { AVIFOURCC_ILGT, "Lightness" },
809 { AVIFOURCC_IMED, "Medium" },
810 { AVIFOURCC_INAM, "Name" },
811 { AVIFOURCC_IPLT, "Palette setting" },
812 { AVIFOURCC_IPRD, "Product" },
813 { AVIFOURCC_ISBJ, "Subject" },
814 { AVIFOURCC_ISFT, "Software" },
815 { AVIFOURCC_ISHP, "Sharpness" },
816 { AVIFOURCC_ISRC, "Source" },
817 { AVIFOURCC_ISRF, "Source form" },
818 { AVIFOURCC_ITCH, "Technician" },
819 { AVIFOURCC_ISMP, "Time code" },
820 { AVIFOURCC_IDIT, "Digitalization time" },
821 { AVIFOURCC_IWRI, "Writer" },
822 { AVIFOURCC_IPRO, "Producer" },
823 { AVIFOURCC_ICNM, "Cinematographer" },
824 { AVIFOURCC_IPDS, "Production designer" },
825 { AVIFOURCC_IEDT, "Editor" },
826 { AVIFOURCC_ICDS, "Costume designer" },
827 { AVIFOURCC_IMUS, "Music" },
828 { AVIFOURCC_ISTD, "Production studio" },
829 { AVIFOURCC_IDST, "Distributor" },
830 { AVIFOURCC_ICNT, "Country" },
831 { AVIFOURCC_ISTR, "Starring" },
832 { AVIFOURCC_IFRM, "Total number of parts" },
833 { AVIFOURCC_strn, "Stream name" },
834 { AVIFOURCC_IAS1, "First Language" },
835 { AVIFOURCC_IAS2, "Second Language" },
836 { AVIFOURCC_IAS3, "Third Language" },
837 { AVIFOURCC_IAS4, "Fourth Language" },
838 { AVIFOURCC_IAS5, "Fifth Language" },
839 { AVIFOURCC_IAS6, "Sixth Language" },
840 { AVIFOURCC_IAS7, "Seventh Language" },
841 { AVIFOURCC_IAS8, "Eighth Language" },
842 { AVIFOURCC_IAS9, "Ninth Language" },
844 { 0, "???" }
847 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
849 int i_index;
850 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
851 AVI_READCHUNK_ENTER;
853 for( i_index = 0;; i_index++)
855 if( !AVI_strz_type[i_index].i_fourcc ||
856 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
858 break;
861 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
862 p_strz->p_str = malloc( p_strz->i_chunk_size + 1 );
863 if( !p_strz->p_type || !p_strz->p_str )
865 free( p_strz->p_type );
866 free( p_strz->p_str );
867 AVI_READCHUNK_EXIT( VLC_EGENERIC );
869 memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
870 p_strz->p_str[p_strz->i_chunk_size] = 0;
872 #ifdef AVI_DEBUG
873 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
874 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
875 #endif
876 AVI_READCHUNK_EXIT( VLC_SUCCESS );
878 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
880 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
881 FREENULL( p_strz->p_type );
882 FREENULL( p_strz->p_str );
885 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
887 return AVI_NextChunk( s, p_chk );
889 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
891 VLC_UNUSED( p_chk );
894 static const struct
896 vlc_fourcc_t i_fourcc;
897 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
898 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
899 } AVI_Chunk_Function [] =
901 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
902 { AVIFOURCC_ON2, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
903 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
904 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
905 { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
906 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
907 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
908 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
909 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
910 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
911 { AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
912 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
913 { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
915 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
916 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
917 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
918 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
919 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
920 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
921 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
922 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
923 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
924 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
925 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
926 { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
927 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
928 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
929 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
930 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
931 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
932 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
933 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
934 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
935 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
936 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
937 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
938 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
939 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
940 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
941 { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
942 { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
943 { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
944 { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
945 { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
946 { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
947 { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
948 { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
949 { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
950 { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
951 { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
952 { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
953 { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
954 { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
955 { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
956 { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
957 { AVIFOURCC_IAS1, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
958 { AVIFOURCC_IAS2, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
959 { AVIFOURCC_IAS3, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
960 { AVIFOURCC_IAS4, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
961 { AVIFOURCC_IAS5, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
962 { AVIFOURCC_IAS6, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
963 { AVIFOURCC_IAS7, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
964 { AVIFOURCC_IAS8, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
965 { AVIFOURCC_IAS9, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
968 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
969 { 0, NULL, NULL }
972 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
974 unsigned int i_index;
975 for( i_index = 0; ; i_index++ )
977 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
978 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
980 return i_index;
985 int AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
987 int i_index;
989 if( !p_chk )
991 msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
992 return VLC_EGENERIC;
995 if( AVI_ChunkReadCommon( s, p_chk, p_father ) )
996 return VLC_EGENERIC;
998 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
1000 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
1001 return AVI_ZERO_FOURCC;
1003 p_chk->common.p_father = p_father;
1005 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
1006 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
1008 return AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
1010 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
1011 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
1012 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
1013 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
1015 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
1016 return AVI_ChunkRead_indx( s, p_chk );
1019 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
1020 (char*)&p_chk->common.i_chunk_fourcc );
1021 return AVI_NextChunk( s, p_chk );
1024 void AVI_ChunkClean( stream_t *s,
1025 avi_chunk_t *p_chk )
1027 int i_index;
1028 avi_chunk_t *p_child, *p_next;
1030 if( !p_chk )
1032 return;
1035 /* Free all child chunk */
1036 p_child = p_chk->common.p_first;
1037 while( p_child )
1039 p_next = p_child->common.p_next;
1040 AVI_ChunkClean( s, p_child );
1041 free( p_child );
1042 p_child = p_next;
1045 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
1046 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
1048 #ifdef AVI_DEBUG
1049 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
1050 (char*)&p_chk->common.i_chunk_fourcc );
1051 #endif
1052 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
1054 else if( p_chk->common.i_chunk_fourcc != 0 )
1056 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
1057 (char*)&p_chk->common.i_chunk_fourcc );
1059 p_chk->common.p_first = NULL;
1061 return;
1064 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
1065 avi_chunk_t *p_chk, unsigned i_level )
1067 avi_chunk_t *p_child;
1069 char str[512];
1070 if( i_level >= (sizeof(str) - 1)/4 )
1071 return;
1073 memset( str, ' ', sizeof( str ) );
1074 for( unsigned i = 1; i < i_level; i++ )
1076 str[i * 4] = '|';
1078 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
1079 p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
1080 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1082 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1083 "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
1084 i_level ? '+' : '*',
1085 (char*)&p_chk->common.i_chunk_fourcc,
1086 (char*)&p_chk->list.i_type,
1087 p_chk->common.i_chunk_size,
1088 p_chk->common.i_chunk_pos );
1090 else
1092 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1093 "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
1094 (char*)&p_chk->common.i_chunk_fourcc,
1095 p_chk->common.i_chunk_size,
1096 p_chk->common.i_chunk_pos );
1098 msg_Dbg( p_obj, "%s", str );
1100 p_child = p_chk->common.p_first;
1101 while( p_child )
1103 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
1104 p_child = p_child->common.p_next;
1108 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
1110 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
1111 avi_chunk_t *p_chk;
1112 bool b_seekable;
1114 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
1116 p_list->i_chunk_pos = 0;
1117 p_list->i_chunk_size = ((UINT64_MAX - 12) >> 1) << 1;
1118 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
1119 p_list->p_father = NULL;
1120 p_list->p_next = NULL;
1121 p_list->p_first = 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;
1139 *pp_append = p_chk;
1140 while( *pp_append )
1141 pp_append = &((*pp_append)->common.p_next);
1143 if( vlc_stream_Tell( s ) >=
1144 p_chk->common.p_father->common.i_chunk_pos +
1145 __EVEN( p_chk->common.p_father->common.i_chunk_size ) )
1147 break;
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 (!b_list || 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 (!b_list || p_child->list.i_type != i_fourcc) )
1207 continue;
1209 if( i_number-- == 0 )
1210 return p_child; /* We found it */
1213 return NULL;