demux: libavi: remove useless member
[vlc.git] / modules / demux / avi / libavi.c
blob07328829e225c3b3254d85a86fae04445e3946db
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, NULL ) )
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);
208 if( p_container->common.i_chunk_size > 0 &&
209 vlc_stream_Tell( s ) >= AVI_ChunkEnd( p_container ) )
211 break;
214 /* If we can't seek then stop when we 've found LIST-movi */
215 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
216 p_chk->list.i_type == AVIFOURCC_movi &&
217 ( !b_seekable || p_chk->common.i_chunk_size == 0 ) )
219 break;
223 msg_Dbg( (vlc_object_t*)s, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
225 if ( i_ret == AVI_ZERO_FOURCC ) return i_ret;
226 return VLC_SUCCESS;
229 /* Allow to append indexes after starting playback */
230 int AVI_ChunkFetchIndexes( stream_t *s, avi_chunk_t *p_riff )
232 avi_chunk_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0, true );
233 if ( !p_movi )
234 return VLC_EGENERIC;
236 avi_chunk_t *p_chk;
237 const uint64_t i_indexpos = AVI_ChunkEnd( p_movi );
238 bool b_seekable = false;
239 int i_ret = VLC_SUCCESS;
241 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
242 if ( !b_seekable || vlc_stream_Seek( s, i_indexpos ) )
243 return VLC_EGENERIC;
245 union avi_chunk_u **pp_append = &p_riff->common.p_first;
246 for( ; ; )
248 p_chk = calloc( 1, sizeof( avi_chunk_t ) );
249 if( !p_chk )
251 i_ret = VLC_EGENERIC;
252 break;
255 i_ret = AVI_ChunkRead( s, p_chk, p_riff );
256 if( i_ret )
258 AVI_ChunkClean( s, p_chk );
259 free( p_chk );
260 break;
263 *pp_append = p_chk;
264 while( *pp_append )
265 pp_append = &((*pp_append)->common.p_next);
267 if( p_chk->common.p_father->common.i_chunk_size > 0 &&
268 ( vlc_stream_Tell( s ) >
269 p_chk->common.p_father->common.i_chunk_pos +
270 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
272 break;
275 /* If we can't seek then stop when we 've found any index */
276 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_indx ||
277 p_chk->common.i_chunk_fourcc == AVIFOURCC_idx1 )
279 break;
284 return i_ret;
287 #define AVI_READCHUNK_ENTER \
288 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
289 if( i_read > 100000000 ) \
291 msg_Err( s, "Big chunk ignored" ); \
292 return VLC_EGENERIC; \
294 uint8_t *p_read, *p_buff; \
295 if( !( p_read = p_buff = malloc(i_read ) ) ) \
297 return VLC_EGENERIC; \
299 i_read = vlc_stream_Read( s, p_read, i_read ); \
300 if( i_read < (int64_t)__EVEN(p_chk->common.i_chunk_size ) + 8 ) \
302 free( p_buff ); \
303 return VLC_EGENERIC; \
305 p_read += 8; \
306 i_read -= 8
308 #define AVI_READ( res, func, size ) \
309 if( i_read < size ) { \
310 free( p_buff); \
311 return VLC_EGENERIC; \
313 i_read -= size; \
314 res = func( p_read ); \
315 p_read += size \
317 #define AVI_READCHUNK_EXIT( code ) \
318 do { \
319 free( p_buff ); \
320 return code; \
321 } while(0)
323 static inline uint8_t GetB( uint8_t *ptr )
325 return *ptr;
328 #define AVI_READ1BYTE( i_byte ) \
329 AVI_READ( i_byte, GetB, 1 )
331 #define AVI_READ2BYTES( i_word ) \
332 AVI_READ( i_word, GetWLE, 2 )
334 #define AVI_READ4BYTES( i_dword ) \
335 AVI_READ( i_dword, GetDWLE, 4 )
337 #define AVI_READ8BYTES( i_qword ) \
338 AVI_READ( i_qword, GetQWLE, 8 )
340 #define AVI_READFOURCC( i_dword ) \
341 AVI_READ( i_dword, GetFOURCC, 4 )
343 static int AVI_ChunkRead_avih( stream_t *s, avi_chunk_t *p_chk )
345 AVI_READCHUNK_ENTER;
347 p_chk->common.i_chunk_fourcc = AVIFOURCC_avih;
348 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
349 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
350 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
351 AVI_READ4BYTES( p_chk->avih.i_flags );
352 AVI_READ4BYTES( p_chk->avih.i_totalframes );
353 AVI_READ4BYTES( p_chk->avih.i_initialframes );
354 AVI_READ4BYTES( p_chk->avih.i_streams );
355 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
356 AVI_READ4BYTES( p_chk->avih.i_width );
357 AVI_READ4BYTES( p_chk->avih.i_height );
358 AVI_READ4BYTES( p_chk->avih.i_scale );
359 AVI_READ4BYTES( p_chk->avih.i_rate );
360 AVI_READ4BYTES( p_chk->avih.i_start );
361 AVI_READ4BYTES( p_chk->avih.i_length );
362 #ifdef AVI_DEBUG
363 msg_Dbg( (vlc_object_t*)s,
364 "avih: streams:%d flags:%s%s%s%s %dx%d",
365 p_chk->avih.i_streams,
366 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
367 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
368 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
369 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
370 p_chk->avih.i_width, p_chk->avih.i_height );
371 #endif
372 AVI_READCHUNK_EXIT( VLC_SUCCESS );
375 static int AVI_ChunkRead_strh( stream_t *s, avi_chunk_t *p_chk )
377 AVI_READCHUNK_ENTER;
379 AVI_READFOURCC( p_chk->strh.i_type );
380 AVI_READFOURCC( p_chk->strh.i_handler );
381 AVI_READ4BYTES( p_chk->strh.i_flags );
382 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
383 AVI_READ4BYTES( p_chk->strh.i_initialframes );
384 AVI_READ4BYTES( p_chk->strh.i_scale );
385 AVI_READ4BYTES( p_chk->strh.i_rate );
386 AVI_READ4BYTES( p_chk->strh.i_start );
387 AVI_READ4BYTES( p_chk->strh.i_length );
388 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
389 AVI_READ4BYTES( p_chk->strh.i_quality );
390 AVI_READ4BYTES( p_chk->strh.i_samplesize );
391 #ifdef AVI_DEBUG
392 msg_Dbg( (vlc_object_t*)s,
393 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
394 (char*)&p_chk->strh.i_type,
395 p_chk->strh.i_handler,
396 p_chk->strh.i_samplesize,
397 ( p_chk->strh.i_scale ?
398 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
399 #endif
401 AVI_READCHUNK_EXIT( VLC_SUCCESS );
404 static int AVI_ChunkRead_strf( stream_t *s, avi_chunk_t *p_chk )
406 avi_chunk_t *p_strh;
408 AVI_READCHUNK_ENTER;
409 if( p_chk->common.p_father == NULL )
411 msg_Err( (vlc_object_t*)s, "malformed avi file" );
412 AVI_READCHUNK_EXIT( VLC_EGENERIC );
414 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0, false ) ) )
416 msg_Err( (vlc_object_t*)s, "malformed avi file" );
417 AVI_READCHUNK_EXIT( p_chk->common.i_chunk_size > 0 ? VLC_EGENERIC : AVI_ZEROSIZED_CHUNK );
420 switch( p_strh->strh.i_type )
422 case( AVIFOURCC_auds ):
423 p_chk->strf.auds.i_cat = AUDIO_ES;
424 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
425 if ( !p_chk->strf.auds.p_wf )
427 AVI_READCHUNK_EXIT( VLC_ENOMEM );
429 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
430 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
431 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
432 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
433 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
434 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
436 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
437 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
439 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
441 /* prevent segfault */
442 if( p_chk->strf.auds.p_wf->cbSize >
443 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
445 p_chk->strf.auds.p_wf->cbSize =
446 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
449 if( p_chk->strf.auds.p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE )
451 msg_Dbg( s, "Extended header found" );
454 else
456 p_chk->strf.auds.p_wf->cbSize = 0;
458 if( p_chk->strf.auds.p_wf->cbSize > 0 )
460 memcpy( &p_chk->strf.auds.p_wf[1] ,
461 p_buff + 8 + sizeof( WAVEFORMATEX ), /* 8=fourcc+size */
462 p_chk->strf.auds.p_wf->cbSize );
464 #ifdef AVI_DEBUG
465 msg_Dbg( (vlc_object_t*)s,
466 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkbps",
467 p_chk->strf.auds.p_wf->wFormatTag,
468 p_chk->strf.auds.p_wf->nChannels,
469 p_chk->strf.auds.p_wf->nSamplesPerSec,
470 p_chk->strf.auds.p_wf->wBitsPerSample,
471 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1000 );
472 #endif
473 break;
474 case( AVIFOURCC_vids ):
475 p_strh->strh.i_samplesize = 0; /* XXX for ffmpeg avi file */
476 p_chk->strf.vids.i_cat = VIDEO_ES;
477 p_chk->strf.vids.p_bih = malloc( __MAX( p_chk->common.i_chunk_size,
478 sizeof( *p_chk->strf.vids.p_bih ) ) );
479 if ( !p_chk->strf.vids.p_bih )
481 AVI_READCHUNK_EXIT( VLC_ENOMEM );
483 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
484 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
485 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
486 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
487 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
488 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
489 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
490 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
491 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
492 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
493 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
494 if( p_chk->strf.vids.p_bih->biSize > p_chk->common.i_chunk_size )
496 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
498 if ( p_chk->common.i_chunk_size > sizeof(VLC_BITMAPINFOHEADER) )
500 uint64_t i_extrasize = p_chk->common.i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
502 /* There's a color palette appended, set up VLC_BITMAPINFO */
503 memcpy( &p_chk->strf.vids.p_bih[1],
504 p_buff + 8 + sizeof(VLC_BITMAPINFOHEADER), /* 8=fourrc+size */
505 i_extrasize );
507 if ( !p_chk->strf.vids.p_bih->biClrUsed )
509 if( p_chk->strf.vids.p_bih->biBitCount < 32 )
510 p_chk->strf.vids.p_bih->biClrUsed = (1 << p_chk->strf.vids.p_bih->biBitCount);
511 else
512 p_chk->strf.vids.p_bih->biBitCount = UINT16_MAX;
515 if( i_extrasize / sizeof(uint32_t) > UINT32_MAX )
516 p_chk->strf.vids.p_bih->biClrUsed = UINT32_MAX;
517 else
519 p_chk->strf.vids.p_bih->biClrUsed =
520 __MIN( i_extrasize / sizeof(uint32_t),
521 p_chk->strf.vids.p_bih->biClrUsed );
524 /* stay within VLC's limits */
525 p_chk->strf.vids.p_bih->biClrUsed =
526 __MIN( VIDEO_PALETTE_COLORS_MAX, p_chk->strf.vids.p_bih->biClrUsed );
528 else p_chk->strf.vids.p_bih->biClrUsed = 0;
529 #ifdef AVI_DEBUG
530 msg_Dbg( (vlc_object_t*)s,
531 "strf: video:%4.4s %"PRIu32"x%"PRIu32" planes:%d %dbpp",
532 (char*)&p_chk->strf.vids.p_bih->biCompression,
533 (uint32_t)p_chk->strf.vids.p_bih->biWidth,
534 (uint32_t)p_chk->strf.vids.p_bih->biHeight,
535 p_chk->strf.vids.p_bih->biPlanes,
536 p_chk->strf.vids.p_bih->biBitCount );
537 #endif
538 break;
539 case AVIFOURCC_iavs:
540 case AVIFOURCC_ivas:
541 p_chk->strf.common.i_cat = UNKNOWN_ES;
542 break;
543 case( AVIFOURCC_txts ):
544 p_chk->strf.common.i_cat = SPU_ES;
545 break;
546 default:
547 msg_Warn( (vlc_object_t*)s, "unknown stream type: %4.4s",
548 (char*)&p_strh->strh.i_type );
549 p_chk->strf.common.i_cat = UNKNOWN_ES;
550 break;
552 AVI_READCHUNK_EXIT( VLC_SUCCESS );
554 static void AVI_ChunkFree_strf( avi_chunk_t *p_chk )
556 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
557 if( p_strf->common.i_cat == AUDIO_ES )
559 FREENULL( p_strf->auds.p_wf );
561 else if( p_strf->common.i_cat == VIDEO_ES )
563 FREENULL( p_strf->vids.p_bih );
567 static int AVI_ChunkRead_strd( stream_t *s, avi_chunk_t *p_chk )
569 if ( p_chk->common.i_chunk_size == 0 )
571 msg_Dbg( (vlc_object_t*)s, "Zero sized pre-JUNK section met" );
572 return AVI_ZEROSIZED_CHUNK;
575 AVI_READCHUNK_ENTER;
576 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
577 if( p_chk->strd.p_data )
578 memcpy( p_chk->strd.p_data, p_buff + 8, p_chk->common.i_chunk_size );
579 AVI_READCHUNK_EXIT( p_chk->strd.p_data ? VLC_SUCCESS : VLC_EGENERIC );
582 static void AVI_ChunkFree_strd( avi_chunk_t *p_chk )
584 free( p_chk->strd.p_data );
587 static int AVI_ChunkRead_idx1( stream_t *s, avi_chunk_t *p_chk )
589 unsigned int i_count, i_index;
591 AVI_READCHUNK_ENTER;
593 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
595 p_chk->idx1.i_entry_count = i_count;
596 p_chk->idx1.i_entry_max = i_count;
597 if( i_count > 0 )
599 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
600 if( !p_chk->idx1.entry )
601 AVI_READCHUNK_EXIT( VLC_EGENERIC );
603 for( i_index = 0; i_index < i_count ; i_index++ )
605 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
606 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
607 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
608 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
611 else
613 p_chk->idx1.entry = NULL;
615 #ifdef AVI_DEBUG
616 msg_Dbg( (vlc_object_t*)s, "idx1: index entry:%d", i_count );
617 #endif
618 AVI_READCHUNK_EXIT( VLC_SUCCESS );
621 static void AVI_ChunkFree_idx1( avi_chunk_t *p_chk )
623 p_chk->idx1.i_entry_count = 0;
624 p_chk->idx1.i_entry_max = 0;
625 FREENULL( p_chk->idx1.entry );
630 static int AVI_ChunkRead_indx( stream_t *s, avi_chunk_t *p_chk )
632 unsigned int i_count, i;
633 int i_ret = VLC_SUCCESS;
634 int32_t i_dummy;
635 VLC_UNUSED(i_dummy);
636 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
638 AVI_READCHUNK_ENTER;
640 AVI_READ2BYTES( p_indx->i_longsperentry );
641 AVI_READ1BYTE ( p_indx->i_indexsubtype );
642 AVI_READ1BYTE ( p_indx->i_indextype );
643 AVI_READ4BYTES( p_indx->i_entriesinuse );
645 AVI_READ4BYTES( p_indx->i_id );
646 p_indx->idx.std = NULL;
647 p_indx->idx.field = NULL;
648 p_indx->idx.super = NULL;
650 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
652 AVI_READ8BYTES( p_indx->i_baseoffset );
653 AVI_READ4BYTES( i_dummy );
655 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
656 p_indx->i_entriesinuse = i_count;
657 p_indx->idx.std = calloc( i_count, sizeof( indx_std_entry_t ) );
658 if( i_count == 0 || p_indx->idx.std )
660 for( i = 0; i < i_count; i++ )
662 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
663 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
666 else i_ret = VLC_EGENERIC;
668 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
670 AVI_READ8BYTES( p_indx->i_baseoffset );
671 AVI_READ4BYTES( i_dummy );
673 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
674 p_indx->i_entriesinuse = i_count;
675 p_indx->idx.field = calloc( i_count, sizeof( indx_field_entry_t ) );
676 if( i_count == 0 || p_indx->idx.field )
678 for( i = 0; i < i_count; i++ )
680 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
681 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
682 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
685 else i_ret = VLC_EGENERIC;
687 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
689 p_indx->i_baseoffset = 0;
690 AVI_READ4BYTES( i_dummy );
691 AVI_READ4BYTES( i_dummy );
692 AVI_READ4BYTES( i_dummy );
694 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
695 p_indx->i_entriesinuse = i_count;
696 p_indx->idx.super = calloc( i_count, sizeof( indx_super_entry_t ) );
697 if( i_count == 0 || p_indx->idx.super )
699 for( i = 0; i < i_count; i++ )
701 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
702 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
703 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
706 else i_ret = VLC_EGENERIC;
708 else
710 msg_Warn( (vlc_object_t*)s, "unknown type/subtype index" );
713 #ifdef AVI_DEBUG
714 msg_Dbg( (vlc_object_t*)s, "indx: type=%d subtype=%d entry=%d",
715 p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
716 #endif
717 AVI_READCHUNK_EXIT( i_ret );
719 static void AVI_ChunkFree_indx( avi_chunk_t *p_chk )
721 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
723 FREENULL( p_indx->idx.std );
724 FREENULL( p_indx->idx.field );
725 FREENULL( p_indx->idx.super );
728 static int AVI_ChunkRead_vprp( stream_t *s, avi_chunk_t *p_chk )
730 avi_chunk_vprp_t *p_vprp = (avi_chunk_vprp_t*)p_chk;
732 AVI_READCHUNK_ENTER;
734 AVI_READ4BYTES( p_vprp->i_video_format_token );
735 AVI_READ4BYTES( p_vprp->i_video_standard );
736 AVI_READ4BYTES( p_vprp->i_vertical_refresh );
737 AVI_READ4BYTES( p_vprp->i_h_total_in_t );
738 AVI_READ4BYTES( p_vprp->i_v_total_in_lines );
739 AVI_READ4BYTES( p_vprp->i_frame_aspect_ratio );
740 AVI_READ4BYTES( p_vprp->i_frame_width_in_pixels );
741 AVI_READ4BYTES( p_vprp->i_frame_height_in_pixels );
742 AVI_READ4BYTES( p_vprp->i_nb_fields_per_frame );
743 for( unsigned i = 0; i < __MIN( p_vprp->i_nb_fields_per_frame, 2 ); i++ )
745 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_height );
746 AVI_READ4BYTES( p_vprp->field_info[i].i_compressed_bm_width );
747 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_height );
748 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_width );
749 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_x_offset );
750 AVI_READ4BYTES( p_vprp->field_info[i].i_valid_bm_y_offset );
751 AVI_READ4BYTES( p_vprp->field_info[i].i_video_x_offset_in_t );
752 AVI_READ4BYTES( p_vprp->field_info[i].i_video_y_valid_start_line );
755 #ifdef AVI_DEBUG
756 msg_Dbg( (vlc_object_t*)s, "vprp: format:%d standard:%d",
757 p_vprp->i_video_format_token, p_vprp->i_video_standard );
758 #endif
759 AVI_READCHUNK_EXIT( VLC_SUCCESS );
762 static int AVI_ChunkRead_dmlh( stream_t *s, avi_chunk_t *p_chk )
764 avi_chunk_dmlh_t *p_dmlh = (avi_chunk_dmlh_t*)p_chk;
766 AVI_READCHUNK_ENTER;
768 AVI_READ4BYTES( p_dmlh->dwTotalFrames );
770 #ifdef AVI_DEBUG
771 msg_Dbg( (vlc_object_t*)s, "dmlh: dwTotalFrames %d",
772 p_dmlh->dwTotalFrames );
773 #endif
774 AVI_READCHUNK_EXIT( VLC_SUCCESS );
777 static const struct
779 vlc_fourcc_t i_fourcc;
780 const char *psz_type;
781 } AVI_strz_type[] =
783 { AVIFOURCC_IARL, "Archive location" },
784 { AVIFOURCC_IART, "Artist" },
785 { AVIFOURCC_ICMS, "Commisioned" },
786 { AVIFOURCC_ICMT, "Comments" },
787 { AVIFOURCC_ICOP, "Copyright" },
788 { AVIFOURCC_ICRD, "Creation date" },
789 { AVIFOURCC_ICRP, "Cropped" },
790 { AVIFOURCC_IDIM, "Dimensions" },
791 { AVIFOURCC_IDPI, "Dots per inch" },
792 { AVIFOURCC_IENG, "Engineer" },
793 { AVIFOURCC_IGNR, "Genre" },
794 { AVIFOURCC_ISGN, "Secondary Genre" },
795 { AVIFOURCC_IKEY, "Keywords" },
796 { AVIFOURCC_ILGT, "Lightness" },
797 { AVIFOURCC_IMED, "Medium" },
798 { AVIFOURCC_INAM, "Name" },
799 { AVIFOURCC_IPLT, "Palette setting" },
800 { AVIFOURCC_IPRD, "Product" },
801 { AVIFOURCC_ISBJ, "Subject" },
802 { AVIFOURCC_ISFT, "Software" },
803 { AVIFOURCC_ISHP, "Sharpness" },
804 { AVIFOURCC_ISRC, "Source" },
805 { AVIFOURCC_ISRF, "Source form" },
806 { AVIFOURCC_ITCH, "Technician" },
807 { AVIFOURCC_ISMP, "Time code" },
808 { AVIFOURCC_IDIT, "Digitalization time" },
809 { AVIFOURCC_IWRI, "Writer" },
810 { AVIFOURCC_IPRO, "Producer" },
811 { AVIFOURCC_ICNM, "Cinematographer" },
812 { AVIFOURCC_IPDS, "Production designer" },
813 { AVIFOURCC_IEDT, "Editor" },
814 { AVIFOURCC_ICDS, "Costume designer" },
815 { AVIFOURCC_IMUS, "Music" },
816 { AVIFOURCC_ISTD, "Production studio" },
817 { AVIFOURCC_IDST, "Distributor" },
818 { AVIFOURCC_ICNT, "Country" },
819 { AVIFOURCC_ISTR, "Starring" },
820 { AVIFOURCC_IFRM, "Total number of parts" },
821 { AVIFOURCC_strn, "Stream name" },
822 { AVIFOURCC_IAS1, "First Language" },
823 { AVIFOURCC_IAS2, "Second Language" },
824 { AVIFOURCC_IAS3, "Third Language" },
825 { AVIFOURCC_IAS4, "Fourth Language" },
826 { AVIFOURCC_IAS5, "Fifth Language" },
827 { AVIFOURCC_IAS6, "Sixth Language" },
828 { AVIFOURCC_IAS7, "Seventh Language" },
829 { AVIFOURCC_IAS8, "Eighth Language" },
830 { AVIFOURCC_IAS9, "Ninth Language" },
832 { 0, "???" }
835 static int AVI_ChunkRead_strz( stream_t *s, avi_chunk_t *p_chk )
837 int i_index;
838 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
839 AVI_READCHUNK_ENTER;
841 for( i_index = 0;; i_index++)
843 if( !AVI_strz_type[i_index].i_fourcc ||
844 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
846 break;
849 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
850 p_strz->p_str = malloc( p_strz->i_chunk_size + 1 );
851 if( !p_strz->p_type || !p_strz->p_str )
853 free( p_strz->p_type );
854 free( p_strz->p_str );
855 AVI_READCHUNK_EXIT( VLC_EGENERIC );
857 memcpy( p_strz->p_str, p_read, p_strz->i_chunk_size );
858 p_strz->p_str[p_strz->i_chunk_size] = 0;
860 #ifdef AVI_DEBUG
861 msg_Dbg( (vlc_object_t*)s, "%4.4s: %s : %s",
862 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
863 #endif
864 AVI_READCHUNK_EXIT( VLC_SUCCESS );
866 static void AVI_ChunkFree_strz( avi_chunk_t *p_chk )
868 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
869 FREENULL( p_strz->p_type );
870 FREENULL( p_strz->p_str );
873 static int AVI_ChunkRead_nothing( stream_t *s, avi_chunk_t *p_chk )
875 return AVI_NextChunk( s, p_chk );
877 static void AVI_ChunkFree_nothing( avi_chunk_t *p_chk )
879 VLC_UNUSED( p_chk );
882 static const struct
884 vlc_fourcc_t i_fourcc;
885 int (*AVI_ChunkRead_function)( stream_t *s, avi_chunk_t *p_chk );
886 void (*AVI_ChunkFree_function)( avi_chunk_t *p_chk );
887 } AVI_Chunk_Function [] =
889 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
890 { AVIFOURCC_ON2, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
891 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
892 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
893 { AVIFOURCC_ON2h, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
894 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
895 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
896 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_strd },
897 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
898 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
899 { AVIFOURCC_vprp, AVI_ChunkRead_vprp, AVI_ChunkFree_nothing },
900 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
901 { AVIFOURCC_dmlh, AVI_ChunkRead_dmlh, AVI_ChunkFree_nothing },
903 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
904 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
905 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
906 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
907 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
908 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
909 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
910 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
911 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
912 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
913 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
914 { AVIFOURCC_ISGN, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
915 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
916 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
917 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
918 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
919 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
920 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
921 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
922 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
923 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
924 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
925 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
926 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
927 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
928 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
929 { AVIFOURCC_ILNG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
930 { AVIFOURCC_IRTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
931 { AVIFOURCC_IWEB, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
932 { AVIFOURCC_IPRT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
933 { AVIFOURCC_IWRI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
934 { AVIFOURCC_IPRO, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
935 { AVIFOURCC_ICNM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
936 { AVIFOURCC_IPDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
937 { AVIFOURCC_IEDT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
938 { AVIFOURCC_ICDS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
939 { AVIFOURCC_IMUS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
940 { AVIFOURCC_ISTD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
941 { AVIFOURCC_IDST, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
942 { AVIFOURCC_ICNT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
943 { AVIFOURCC_ISTR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
944 { AVIFOURCC_IFRM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
945 { AVIFOURCC_IAS1, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
946 { AVIFOURCC_IAS2, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
947 { AVIFOURCC_IAS3, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
948 { AVIFOURCC_IAS4, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
949 { AVIFOURCC_IAS5, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
950 { AVIFOURCC_IAS6, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
951 { AVIFOURCC_IAS7, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
952 { AVIFOURCC_IAS8, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
953 { AVIFOURCC_IAS9, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
956 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
957 { 0, NULL, NULL }
960 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
962 unsigned int i_index;
963 for( i_index = 0; ; i_index++ )
965 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
966 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
968 return i_index;
973 int AVI_ChunkRead( stream_t *s, avi_chunk_t *p_chk, avi_chunk_t *p_father )
975 int i_index;
977 if( !p_chk )
979 msg_Warn( (vlc_object_t*)s, "cannot read null chunk" );
980 return VLC_EGENERIC;
983 if( AVI_ChunkReadCommon( s, p_chk, p_father ) )
984 return VLC_EGENERIC;
986 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
988 msg_Warn( (vlc_object_t*)s, "found null fourcc chunk (corrupted file?)" );
989 return AVI_ZERO_FOURCC;
991 p_chk->common.p_father = p_father;
993 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
994 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
996 int i_return = AVI_Chunk_Function[i_index].AVI_ChunkRead_function( s, p_chk );
997 if ( i_return == AVI_ZEROSIZED_CHUNK || i_return == AVI_ZERO_FOURCC )
999 if ( !p_father )
1000 return VLC_EGENERIC;
1001 p_chk->common.i_chunk_fourcc = 0;
1002 return AVI_NextChunk( s, ( i_return == AVI_ZEROSIZED_CHUNK ) ? p_chk : p_father );
1004 return i_return;
1006 else if( ( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
1007 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' ) ||
1008 ( ((char*)&p_chk->common.i_chunk_fourcc)[2] == 'i' &&
1009 ((char*)&p_chk->common.i_chunk_fourcc)[3] == 'x' ) )
1011 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
1012 return AVI_ChunkRead_indx( s, p_chk );
1015 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not loaded)",
1016 (char*)&p_chk->common.i_chunk_fourcc );
1017 return AVI_NextChunk( s, p_chk );
1020 void AVI_ChunkClean( stream_t *s,
1021 avi_chunk_t *p_chk )
1023 int i_index;
1024 avi_chunk_t *p_child, *p_next;
1026 if( !p_chk )
1028 return;
1031 /* Free all child chunk */
1032 p_child = p_chk->common.p_first;
1033 while( p_child )
1035 p_next = p_child->common.p_next;
1036 AVI_ChunkClean( s, p_child );
1037 free( p_child );
1038 p_child = p_next;
1041 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
1042 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
1044 #ifdef AVI_DEBUG
1045 msg_Dbg( (vlc_object_t*)s, "free chunk %4.4s",
1046 (char*)&p_chk->common.i_chunk_fourcc );
1047 #endif
1048 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_chk);
1050 else if( p_chk->common.i_chunk_fourcc != 0 )
1052 msg_Warn( (vlc_object_t*)s, "unknown chunk: %4.4s (not unloaded)",
1053 (char*)&p_chk->common.i_chunk_fourcc );
1055 p_chk->common.p_first = NULL;
1057 return;
1060 static void AVI_ChunkDumpDebug_level( vlc_object_t *p_obj,
1061 avi_chunk_t *p_chk, unsigned i_level )
1063 avi_chunk_t *p_child;
1065 char str[512];
1066 if( i_level >= (sizeof(str) - 1)/4 )
1067 return;
1069 memset( str, ' ', sizeof( str ) );
1070 for( unsigned i = 1; i < i_level; i++ )
1072 str[i * 4] = '|';
1074 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF ||
1075 p_chk->common.i_chunk_fourcc == AVIFOURCC_ON2 ||
1076 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1078 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1079 "%c %4.4s-%4.4s size:%"PRIu64" pos:%"PRIu64,
1080 i_level ? '+' : '*',
1081 (char*)&p_chk->common.i_chunk_fourcc,
1082 (char*)&p_chk->list.i_type,
1083 p_chk->common.i_chunk_size,
1084 p_chk->common.i_chunk_pos );
1086 else
1088 snprintf( &str[i_level * 4], sizeof(str) - 4*i_level,
1089 "+ %4.4s size:%"PRIu64" pos:%"PRIu64,
1090 (char*)&p_chk->common.i_chunk_fourcc,
1091 p_chk->common.i_chunk_size,
1092 p_chk->common.i_chunk_pos );
1094 msg_Dbg( p_obj, "%s", str );
1096 p_child = p_chk->common.p_first;
1097 while( p_child )
1099 AVI_ChunkDumpDebug_level( p_obj, p_child, i_level + 1 );
1100 p_child = p_child->common.p_next;
1104 int AVI_ChunkReadRoot( stream_t *s, avi_chunk_t *p_root )
1106 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
1107 avi_chunk_t *p_chk;
1108 bool b_seekable;
1110 vlc_stream_Control( s, STREAM_CAN_SEEK, &b_seekable );
1112 p_list->i_chunk_pos = 0;
1113 p_list->i_chunk_size = ((UINT64_MAX - 12) >> 1) << 1;
1114 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
1115 p_list->p_father = NULL;
1116 p_list->p_next = NULL;
1117 p_list->p_first = NULL;
1119 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
1121 union avi_chunk_u **pp_append = &p_root->common.p_first;
1122 for( ; ; )
1124 p_chk = calloc( 1, sizeof( avi_chunk_t ) );
1125 if( !p_chk )
1126 return VLC_EGENERIC;
1128 if( AVI_ChunkRead( s, p_chk, p_root ) != VLC_SUCCESS )
1130 AVI_ChunkClean( s, p_chk );
1131 free( p_chk );
1132 break;
1134 else if( vlc_stream_Tell( s ) >=
1135 p_chk->common.p_father->common.i_chunk_pos +
1136 __EVEN( p_chk->common.p_father->common.i_chunk_size ) )
1138 break;
1141 *pp_append = p_chk;
1142 while( *pp_append )
1143 pp_append = &((*pp_append)->common.p_next);
1145 /* If we can't seek then stop when we 've found first RIFF-AVI */
1146 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
1147 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
1149 break;
1153 p_list->i_chunk_size = stream_Size( s );
1155 AVI_ChunkDumpDebug_level( (vlc_object_t*)s, p_root, 0 );
1156 return VLC_SUCCESS;
1159 void AVI_ChunkFreeRoot( stream_t *s,
1160 avi_chunk_t *p_chk )
1162 AVI_ChunkClean( s, p_chk );
1166 int AVI_ChunkCount_( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc, bool b_list )
1168 if( !p_chk )
1169 return 0;
1171 int i_count = 0;
1172 for( avi_chunk_t *p_child = p_chk->common.p_first;
1173 p_child; p_child = p_child->common.p_next )
1175 if( b_list && p_child->list.i_type == 0 )
1176 continue;
1178 if( p_child->common.i_chunk_fourcc != i_fourcc &&
1179 p_child->list.i_type != i_fourcc )
1180 continue;
1182 i_count++;
1185 return i_count;
1188 void *AVI_ChunkFind_( avi_chunk_t *p_chk,
1189 vlc_fourcc_t i_fourcc, int i_number, bool b_list )
1191 if( !p_chk )
1192 return NULL;
1194 for( avi_chunk_t *p_child = p_chk->common.p_first;
1195 p_child; p_child = p_child->common.p_next )
1197 if( b_list && p_child->list.i_type == 0 )
1198 continue;
1200 if( p_child->common.i_chunk_fourcc != i_fourcc &&
1201 p_child->list.i_type != i_fourcc )
1202 continue;
1204 if( i_number-- == 0 )
1205 return p_child; /* We found it */
1208 return NULL;