1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.2 2002/10/26 19:14:45 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 #include <stdlib.h> /* malloc(), free() */
24 #include <string.h> /* strdup() */
26 #include <sys/types.h>
29 #include <vlc/input.h>
34 #define AVIFOURCC_PRINT( x ) \
41 if( p ) {free( p ); p = NULL; }
43 #define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
45 /* Some functions to manipulate memory */
46 static u16 GetWLE( u8 *p_buff )
48 return( (p_buff[0]) + ( p_buff[1] <<8 ) );
51 static u32 GetDWLE( u8 *p_buff )
53 return( p_buff[0] + ( p_buff[1] <<8 ) +
54 ( p_buff[2] <<16 ) + ( p_buff[3] <<24 ) );
56 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
58 return( VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] ) );
60 /*****************************************************************************
61 * Some basic functions to manipulate stream more easily in vlc
63 * AVI_TellAbsolute get file position
65 * AVI_SeekAbsolute seek in the file
67 * AVI_ReadData read data from the file in a buffer
69 * AVI_SkipBytes skip bytes
71 *****************************************************************************/
72 off_t AVI_TellAbsolute( input_thread_t *p_input )
76 vlc_mutex_lock( &p_input->stream.stream_lock );
78 i_pos= p_input->stream.p_selected_area->i_tell;
79 // - ( p_input->p_last_data - p_input->p_current_data );
81 vlc_mutex_unlock( &p_input->stream.stream_lock );
86 int AVI_SeekAbsolute( input_thread_t *p_input,
91 if( i_pos >= p_input->stream.p_selected_area->i_size )
96 i_filepos = AVI_TellAbsolute( p_input );
98 if( i_filepos == i_pos )
103 if( p_input->stream.b_seekable &&
104 p_input->stream.i_method == INPUT_METHOD_FILE )
106 p_input->pf_seek( p_input, i_pos );
107 input_AccessReinit( p_input );
113 int i_skip = i_pos - i_filepos;
115 msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
118 return( 0 ); // failed
122 i_peek = input_Peek( p_input, &p_peek, i_skip+1 );
125 vlc_mutex_lock( &p_input->stream.stream_lock );
126 p_input->p_current_data += i_peek; // skip them
127 vlc_mutex_unlock( &p_input->stream.stream_lock );
137 /* return 1 if success, 0 if fail */
138 int AVI_ReadData( input_thread_t *p_input, u8 *p_buff, int i_size )
140 data_packet_t *p_data;
153 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
158 memcpy( p_buff, p_data->p_payload_start, i_count );
159 input_DeletePacket( p_input->p_method_data, p_data );
170 int AVI_SkipBytes( input_thread_t *p_input, int i_count )
172 /* broken with new use of i_tell */
175 vlc_mutex_lock( &p_input->stream.stream_lock );
176 i_buff_size = p_input->p_last_data - p_input->p_current_data;
177 vlc_mutex_unlock( &p_input->stream.stream_lock );
179 if( i_count > 0 && i_count + 1 < i_buff_size )
183 input_Peek( p_input, &p_peek, i_count + 1 );
185 vlc_mutex_lock( &p_input->stream.stream_lock );
186 p_input->p_current_data += i_count; // skip them
187 vlc_mutex_unlock( &p_input->stream.stream_lock );
193 return( AVI_SeekAbsolute( p_input,
194 AVI_TellAbsolute( p_input ) + i_count ) );
198 /*****************************************************************************
200 * AVI_TestFile: look at first bytes to see if it's a valid avi file
204 *****************************************************************************/
205 int AVI_TestFile( input_thread_t *p_input )
209 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
211 msg_Err( p_input, "cannot peek()" );
215 if( GetDWLE( p_peek ) == AVIFOURCC_RIFF &&
216 GetDWLE( p_peek + 8 ) == AVIFOURCC_AVI )
225 /****************************************************************************
227 * Basics functions to manipulates chunks
229 ****************************************************************************/
230 static int AVI_ChunkReadCommon( input_thread_t *p_input,
236 memset( p_chk, 0, sizeof( avi_chunk_t ) );
238 if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
243 p_chk->common.i_chunk_fourcc = GetDWLE( p_peek );
244 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
245 p_chk->common.i_chunk_pos = AVI_TellAbsolute( p_input );
247 p_chk->common.p_father = NULL;
248 p_chk->common.p_next = NULL;
249 p_chk->common.p_first = NULL;
250 p_chk->common.p_next = NULL;
253 "Found Chunk fourcc:%c%c%c%c size:%lld pos:%lld",
254 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
255 p_chk->common.i_chunk_size,
256 p_chk->common.i_chunk_pos );
261 static int AVI_NextChunk( input_thread_t *p_input,
268 if( !AVI_ChunkReadCommon( p_input, &chk ) )
275 if( p_chk->common.p_father )
277 if( p_chk->common.p_father->common.i_chunk_pos +
278 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
279 p_chk->common.i_chunk_pos +
280 __EVEN( p_chk->common.i_chunk_size ) + 8 )
285 return( AVI_SeekAbsolute( p_input,
286 p_chk->common.i_chunk_pos +
287 __EVEN( p_chk->common.i_chunk_size ) + 8 ) );
290 int _AVI_ChunkGoto( input_thread_t *p_input,
297 return( AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos ) );
300 /****************************************************************************
302 * Functions to read chunks
304 ****************************************************************************/
305 static int AVI_ChunkRead_list( input_thread_t *p_input,
306 avi_chunk_t *p_container,
312 if( p_container->common.i_chunk_size < 8 )
315 msg_Warn( p_input, "empty list chunk" );
318 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
320 msg_Warn( p_input, "cannot peek while reading list chunk" );
323 p_container->list.i_type = GetDWLE( p_peek + 8 );
325 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
326 p_container->list.i_type == AVIFOURCC_movi )
328 msg_Dbg( p_input, "Skipping movi chunk" );
331 return( AVI_NextChunk( p_input, p_container ) );
335 return( 1 ); // point at begining of LIST-movi
339 AVI_SkipBytes( p_input, 12 );
342 "found LIST chunk: \'%c%c%c%c\'",
343 AVIFOURCC_PRINT( p_container->list.i_type ) );
347 p_chk = malloc( sizeof( avi_chunk_t ) );
348 memset( p_chk, 0, sizeof( avi_chunk_t ) );
349 if( !p_container->common.p_first )
351 p_container->common.p_first = p_chk;
355 p_container->common.p_last->common.p_next = p_chk;
357 p_container->common.p_last = p_chk;
359 if( !AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
360 ( AVI_TellAbsolute( p_input ) >=
361 p_chk->common.p_father->common.i_chunk_pos +
362 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
366 /* If we can't seek then stop when we 've found LIST-movi */
367 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
368 p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
378 #define AVI_READCHUNK_ENTER \
379 s64 i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
380 u8 *p_read, *p_buff; \
381 if( !( p_read = p_buff = malloc(i_read ) ) ) \
385 i_read = AVI_ReadData( p_input, p_read, i_read ); \
389 #define AVI_READCHUNK_EXIT( code ) \
393 msg_Warn( p_input, "not enougth data" ); \
396 #define AVI_READ2BYTES( i_word ) \
397 i_word = GetWLE( p_read ); \
401 #define AVI_READ4BYTES( i_dword ) \
402 i_dword = GetDWLE( p_read ); \
406 #define AVI_READFOURCC( i_dword ) \
407 i_dword = GetFOURCC( p_read ); \
411 static int AVI_ChunkRead_avih( input_thread_t *p_input,
417 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
418 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
419 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
420 AVI_READ4BYTES( p_chk->avih.i_flags );
421 AVI_READ4BYTES( p_chk->avih.i_totalframes );
422 AVI_READ4BYTES( p_chk->avih.i_initialframes );
423 AVI_READ4BYTES( p_chk->avih.i_streams );
424 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
425 AVI_READ4BYTES( p_chk->avih.i_width );
426 AVI_READ4BYTES( p_chk->avih.i_height );
427 AVI_READ4BYTES( p_chk->avih.i_scale );
428 AVI_READ4BYTES( p_chk->avih.i_rate );
429 AVI_READ4BYTES( p_chk->avih.i_start );
430 AVI_READ4BYTES( p_chk->avih.i_length );
433 "avih: streams:%d flags:%s%s%s%s %dx%d",
434 p_chk->avih.i_streams,
435 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
436 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
437 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
438 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
439 p_chk->avih.i_width, p_chk->avih.i_height );
441 AVI_READCHUNK_EXIT( 1 );
444 static int AVI_ChunkRead_strh( input_thread_t *p_input,
450 AVI_READFOURCC( p_chk->strh.i_type );
451 AVI_READFOURCC( p_chk->strh.i_handler );
452 AVI_READ4BYTES( p_chk->strh.i_flags );
453 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
454 AVI_READ4BYTES( p_chk->strh.i_initialframes );
455 AVI_READ4BYTES( p_chk->strh.i_scale );
456 AVI_READ4BYTES( p_chk->strh.i_rate );
457 AVI_READ4BYTES( p_chk->strh.i_start );
458 AVI_READ4BYTES( p_chk->strh.i_length );
459 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
460 AVI_READ4BYTES( p_chk->strh.i_quality );
461 AVI_READ4BYTES( p_chk->strh.i_samplesize );
464 "strh: type:%c%c%c%c handler:0x%8.8x samplesize:%d %.2ffps",
465 AVIFOURCC_PRINT( p_chk->strh.i_type ),
466 p_chk->strh.i_handler,
467 p_chk->strh.i_samplesize,
468 ( p_chk->strh.i_scale ?
469 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
472 AVI_READCHUNK_EXIT( 1 );
475 static int AVI_ChunkRead_strf( input_thread_t *p_input,
482 if( p_chk->common.p_father == NULL )
484 msg_Err( p_input, "malformed avi file" );
485 AVI_READCHUNK_EXIT( 0 );
487 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
489 msg_Err( p_input, "malformed avi file" );
490 AVI_READCHUNK_EXIT( 0 );
493 switch( p_strh->strh.i_type )
495 case( AVIFOURCC_auds ):
496 AVI_READ2BYTES( p_chk->strf.auds.i_formattag );
497 AVI_READ2BYTES( p_chk->strf.auds.i_channels );
498 AVI_READ4BYTES( p_chk->strf.auds.i_samplespersec );
499 AVI_READ4BYTES( p_chk->strf.auds.i_avgbytespersec );
500 AVI_READ2BYTES( p_chk->strf.auds.i_blockalign );
501 AVI_READ2BYTES( p_chk->strf.auds.i_bitspersample );
502 if( p_chk->strf.auds.i_formattag != WAVE_FORMAT_PCM )
504 AVI_READ2BYTES( p_chk->strf.auds.i_size );
506 p_chk->strf.auds.p_wfx = malloc( p_chk->common.i_chunk_size );
507 memcpy( p_chk->strf.auds.p_wfx,
509 p_chk->common.i_chunk_size );
512 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
513 p_chk->strf.auds.i_formattag,
514 p_chk->strf.auds.i_channels,
515 p_chk->strf.auds.i_samplespersec,
516 p_chk->strf.auds.i_bitspersample,
517 p_chk->strf.auds.i_avgbytespersec * 8 / 1024 );
520 case( AVIFOURCC_vids ):
521 p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
522 AVI_READ4BYTES( p_chk->strf.vids.i_size );
523 AVI_READ4BYTES( p_chk->strf.vids.i_width );
524 AVI_READ4BYTES( p_chk->strf.vids.i_height );
525 AVI_READ2BYTES( p_chk->strf.vids.i_planes );
526 AVI_READ2BYTES( p_chk->strf.vids.i_bitcount );
527 AVI_READFOURCC( p_chk->strf.vids.i_compression );
528 AVI_READ4BYTES( p_chk->strf.vids.i_sizeimage );
529 AVI_READ4BYTES( p_chk->strf.vids.i_xpelspermeter );
530 AVI_READ4BYTES( p_chk->strf.vids.i_ypelspermeter );
531 AVI_READ4BYTES( p_chk->strf.vids.i_clrused );
532 AVI_READ4BYTES( p_chk->strf.vids.i_clrimportant );
533 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
534 memcpy( p_chk->strf.vids.p_bih,
536 p_chk->common.i_chunk_size );
539 "strf: video:%c%c%c%c %dx%d planes:%d %dbpp",
540 AVIFOURCC_PRINT( p_chk->strf.vids.i_compression ),
541 p_chk->strf.vids.i_width,
542 p_chk->strf.vids.i_height,
543 p_chk->strf.vids.i_planes,
544 p_chk->strf.vids.i_bitcount );
548 msg_Warn( p_input, "unknown stream type" );
551 AVI_READCHUNK_EXIT( 1 );
553 static void AVI_ChunkFree_strf( input_thread_t *p_input,
559 static int AVI_ChunkRead_strd( input_thread_t *p_input,
564 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
565 memcpy( p_chk->strd.p_data,
567 p_chk->common.i_chunk_size );
568 AVI_READCHUNK_EXIT( 1 );
571 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
575 int i_count, i_index;
579 i_count = __MIN( p_chk->common.i_chunk_size, i_read ) / 16;
581 p_chk->idx1.i_entry_count = i_count;
582 p_chk->idx1.i_entry_max = i_count;
585 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
587 for( i_index = 0; i_index < i_count ; i_index++ )
589 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_fourcc );
590 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
591 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
592 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
597 p_chk->idx1.entry = NULL;
600 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
602 AVI_READCHUNK_EXIT( 1 );
605 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
608 p_chk->idx1.i_entry_count = 0;
609 p_chk->idx1.i_entry_max = 0;
610 FREE( p_chk->idx1.entry )
619 { AVIFOURCC_IARL, "archive location" },
620 { AVIFOURCC_IART, "artist" },
621 { AVIFOURCC_ICMS, "commisioned" },
622 { AVIFOURCC_ICMT, "comments" },
623 { AVIFOURCC_ICOP, "copyright" },
624 { AVIFOURCC_ICRD, "creation date" },
625 { AVIFOURCC_ICRP, "cropped" },
626 { AVIFOURCC_IDIM, "dimensions" },
627 { AVIFOURCC_IDPI, "dots per inch" },
628 { AVIFOURCC_IENG, "enginner" },
629 { AVIFOURCC_IGNR, "genre" },
630 { AVIFOURCC_IKEY, "keywords" },
631 { AVIFOURCC_ILGT, "lightness" },
632 { AVIFOURCC_IMED, "medium" },
633 { AVIFOURCC_INAM, "name" },
634 { AVIFOURCC_IPLT, "palette setting" },
635 { AVIFOURCC_IPRD, "product" },
636 { AVIFOURCC_ISBJ, "subject" },
637 { AVIFOURCC_ISFT, "software" },
638 { AVIFOURCC_ISHP, "sharpness" },
639 { AVIFOURCC_ISRC, "source" },
640 { AVIFOURCC_ISRF, "source form" },
641 { AVIFOURCC_ITCH, "technician" },
642 { AVIFOURCC_ISMP, "time code" },
643 { AVIFOURCC_IDIT, "digitalization time" },
646 static int AVI_ChunkRead_strz( input_thread_t *p_input,
651 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
654 for( i_index = 0;; i_index++)
656 if( !AVI_strz_type[i_index].i_fourcc ||
657 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
662 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
663 p_strz->p_str = malloc( i_read + 1);
665 if( p_strz->i_chunk_size )
667 memcpy( p_strz->p_str, p_read, i_read );
669 p_strz->p_str[i_read] = 0;
672 msg_Dbg( p_input, "%c%c%c%c: %s : %s",
673 AVIFOURCC_PRINT( p_strz->i_chunk_fourcc), p_strz->p_type, p_strz->p_str);
675 AVI_READCHUNK_EXIT( 1 );
677 static void AVI_ChunkFree_strz( input_thread_t *p_input,
680 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
681 FREE( p_strz->p_type );
682 FREE( p_strz->p_str );
685 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
689 return( AVI_NextChunk( p_input, p_chk ) );
691 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
700 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
703 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
704 avi_chunk_t *p_chk );
705 } AVI_Chunk_Function [] =
707 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
708 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
709 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
710 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
711 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
712 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
713 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
714 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
716 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
717 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
718 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
719 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
720 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
721 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
722 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
723 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
724 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
725 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
726 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
727 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
728 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
729 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
730 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
731 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
732 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
733 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
734 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
735 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
736 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
737 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
738 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
739 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
740 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
741 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
745 static int AVI_ChunkFunctionFind( int i_fourcc )
748 for( i_index = 0; ; i_index++ )
750 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
751 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
758 int _AVI_ChunkRead( input_thread_t *p_input,
760 avi_chunk_t *p_father,
772 if( !AVI_ChunkReadCommon( p_input, p_chk ) )
774 msg_Warn( p_input, "cannot read one chunk" );
777 p_chk->common.p_father = p_father;
779 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
780 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
783 AVI_Chunk_Function[i_index].AVI_ChunkRead_function( p_input,
789 msg_Warn( p_input, "unknown chunk (not loaded)" );
790 i_result = AVI_NextChunk( p_input, p_chk );
796 void _AVI_ChunkFree( input_thread_t *p_input,
800 avi_chunk_t *p_child, *p_next;
807 /* Free all child chunk */
808 p_child = p_chk->common.p_first;
811 p_next = p_child->common.p_next;
812 AVI_ChunkFree( p_input, p_child );
817 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
818 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
821 msg_Dbg( p_input, "free chunk %c%c%c%c",
822 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ) );
824 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
828 msg_Warn( p_input, "unknown chunk (not unloaded)" );
830 p_chk->common.p_first = NULL;
831 p_chk->common.p_last = NULL;
836 int AVI_ChunkReadRoot( input_thread_t *p_input,
840 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
843 p_list->i_chunk_pos = 0;
844 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
845 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
846 p_list->p_father = NULL;
847 p_list->p_next = NULL;
848 p_list->p_first = NULL;
849 p_list->p_last = NULL;
851 p_list->i_type = MKFOURCC( 'r', 'o', 'o', 't' );
855 p_chk = malloc( sizeof( avi_chunk_t ) );
856 memset( p_chk, 0, sizeof( avi_chunk_t ) );
857 if( !p_root->common.p_first )
859 p_root->common.p_first = p_chk;
863 p_root->common.p_last->common.p_next = p_chk;
865 p_root->common.p_last = p_chk;
867 if( !AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
868 ( AVI_TellAbsolute( p_input ) >=
869 p_chk->common.p_father->common.i_chunk_pos +
870 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
874 /* If we can't seek then stop when we 've found first RIFF-AVI */
875 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
876 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
885 void AVI_ChunkFreeRoot( input_thread_t *p_input,
888 AVI_ChunkFree( p_input, p_chk );
892 int _AVI_ChunkCount( avi_chunk_t *p_chk, u32 i_fourcc )
895 avi_chunk_t *p_child;
903 p_child = p_chk->common.p_first;
906 if( p_child->common.i_chunk_fourcc == i_fourcc ||
907 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
908 p_child->list.i_type == i_fourcc ) )
912 p_child = p_child->common.p_next;
917 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk, u32 i_fourcc, int i_number )
919 avi_chunk_t *p_child;
924 p_child = p_chk->common.p_first;
928 if( p_child->common.i_chunk_fourcc == i_fourcc ||
929 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
930 p_child->list.i_type == i_fourcc ) )
940 p_child = p_child->common.p_next;
945 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
946 avi_chunk_t *p_chk, int i_level )
950 avi_chunk_t *p_child;
952 memset( str, ' ', sizeof( str ) );
953 for( i = 1; i < i_level; i++ )
957 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
958 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
960 sprintf( str + i_level * 5,
961 "%c %c%c%c%c-%c%c%c%c size:%lld pos:%lld",
963 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
964 AVIFOURCC_PRINT( p_chk->list.i_type ),
965 p_chk->common.i_chunk_size,
966 p_chk->common.i_chunk_pos );
970 sprintf( str + i_level * 5,
971 "+ %c%c%c%c size:%lld pos:%lld",
972 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
973 p_chk->common.i_chunk_size,
974 p_chk->common.i_chunk_pos );
976 msg_Dbg( p_input, "%s", str );
978 p_child = p_chk->common.p_first;
981 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
982 p_child = p_child->common.p_next;
985 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
988 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );