1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.10 2002/12/16 13:04:36 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 uint16_t GetWLE( uint8_t *p_buff )
48 return (uint16_t)p_buff[0] | ( ((uint16_t)p_buff[1]) << 8 );
51 static uint32_t GetDWLE( uint8_t *p_buff )
53 return (uint32_t)p_buff[0] | ( ((uint32_t)p_buff[1]) << 8 ) |
54 ( ((uint32_t)p_buff[2]) << 16 ) | ( ((uint32_t)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 );
112 data_packet_t *p_data;
113 int i_skip = i_pos - i_filepos;
115 msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
118 return VLC_EGENERIC; // failed
124 i_read = input_SplitBuffer( p_input, &p_data,
125 __MIN( 4096, i_skip ) );
132 input_DeletePacket( p_input->p_method_data, p_data );
133 if( i_read == 0 && i_skip > 0 )
141 i_peek = input_Peek( p_input, &p_peek, i_skip+1 );
144 vlc_mutex_lock( &p_input->stream.stream_lock );
145 p_input->p_current_data += i_peek; // skip them
146 vlc_mutex_unlock( &p_input->stream.stream_lock );
157 /* return amount read if success, 0 if failed */
158 int AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
160 data_packet_t *p_data;
173 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
178 memcpy( p_buff, p_data->p_payload_start, i_count );
179 input_DeletePacket( p_input->p_method_data, p_data );
190 int AVI_SkipBytes( input_thread_t *p_input, int i_count )
192 /* broken with new use of i_tell */
195 vlc_mutex_lock( &p_input->stream.stream_lock );
196 i_buff_size = p_input->p_last_data - p_input->p_current_data;
197 vlc_mutex_unlock( &p_input->stream.stream_lock );
199 if( i_count > 0 && i_count + 1 < i_buff_size )
203 input_Peek( p_input, &p_peek, i_count + 1 );
205 vlc_mutex_lock( &p_input->stream.stream_lock );
206 p_input->p_current_data += i_count; // skip them
207 vlc_mutex_unlock( &p_input->stream.stream_lock );
213 return AVI_SeekAbsolute( p_input,
214 AVI_TellAbsolute( p_input ) + i_count );
218 /*****************************************************************************
220 * AVI_TestFile: look at first bytes to see if it's a valid avi file
224 *****************************************************************************/
225 int AVI_TestFile( input_thread_t *p_input )
229 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
231 msg_Err( p_input, "cannot peek()" );
235 if( GetFOURCC( p_peek ) == AVIFOURCC_RIFF &&
236 GetFOURCC( p_peek + 8 ) == AVIFOURCC_AVI )
245 /****************************************************************************
247 * Basics functions to manipulates chunks
249 ****************************************************************************/
250 static int AVI_ChunkReadCommon( input_thread_t *p_input,
256 memset( p_chk, 0, sizeof( avi_chunk_t ) );
258 if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
263 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
264 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
265 p_chk->common.i_chunk_pos = AVI_TellAbsolute( p_input );
267 p_chk->common.p_father = NULL;
268 p_chk->common.p_next = NULL;
269 p_chk->common.p_first = NULL;
270 p_chk->common.p_next = NULL;
273 "Found Chunk fourcc:%8.8x (%c%c%c%c) size:"I64Fd" pos:"I64Fd,
274 p_chk->common.i_chunk_fourcc,
275 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
276 p_chk->common.i_chunk_size,
277 p_chk->common.i_chunk_pos );
282 static int AVI_NextChunk( input_thread_t *p_input,
289 if( AVI_ChunkReadCommon( p_input, &chk ) )
296 if( p_chk->common.p_father )
298 if( p_chk->common.p_father->common.i_chunk_pos +
299 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
300 p_chk->common.i_chunk_pos +
301 __EVEN( p_chk->common.i_chunk_size ) + 8 )
306 return AVI_SeekAbsolute( p_input,
307 p_chk->common.i_chunk_pos +
308 __EVEN( p_chk->common.i_chunk_size ) + 8 );
311 int _AVI_ChunkGoto( input_thread_t *p_input,
318 return AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos );
321 /****************************************************************************
323 * Functions to read chunks
325 ****************************************************************************/
326 static int AVI_ChunkRead_list( input_thread_t *p_input,
327 avi_chunk_t *p_container,
328 vlc_bool_t b_seekable )
333 if( p_container->common.i_chunk_size < 8 )
336 msg_Warn( p_input, "empty list chunk" );
339 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
341 msg_Warn( p_input, "cannot peek while reading list chunk" );
344 p_container->list.i_type = GetFOURCC( p_peek + 8 );
346 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
347 p_container->list.i_type == AVIFOURCC_movi )
349 msg_Dbg( p_input, "Skipping movi chunk" );
352 return AVI_NextChunk( p_input, p_container );
356 return VLC_SUCCESS; // point at begining of LIST-movi
360 if( AVI_SkipBytes( p_input, 12 ) )
362 msg_Err( p_input, "cannot enter chunk" );
367 "found LIST chunk: \'%c%c%c%c\'",
368 AVIFOURCC_PRINT( p_container->list.i_type ) );
370 msg_Dbg( p_input, "<list \'%c%c%c%c\'>", AVIFOURCC_PRINT( p_container->list.i_type ) );
373 p_chk = malloc( sizeof( avi_chunk_t ) );
374 memset( p_chk, 0, sizeof( avi_chunk_t ) );
375 if( !p_container->common.p_first )
377 p_container->common.p_first = p_chk;
381 p_container->common.p_last->common.p_next = p_chk;
383 p_container->common.p_last = p_chk;
385 if( AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
386 ( AVI_TellAbsolute( p_input ) >=
387 (off_t)p_chk->common.p_father->common.i_chunk_pos +
388 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
392 /* If we can't seek then stop when we 've found LIST-movi */
393 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
394 p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
400 msg_Dbg( p_input, "</list \'%c%c%c%c\'>", AVIFOURCC_PRINT( p_container->list.i_type ) );
405 #define AVI_READCHUNK_ENTER \
406 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
407 uint8_t *p_read, *p_buff; \
408 if( !( p_read = p_buff = malloc(i_read ) ) ) \
412 i_read = AVI_ReadData( p_input, p_read, i_read ); \
416 #define AVI_READCHUNK_EXIT( code ) \
420 msg_Warn( p_input, "not enough data" ); \
423 #define AVI_READ2BYTES( i_word ) \
424 i_word = GetWLE( p_read ); \
428 #define AVI_READ4BYTES( i_dword ) \
429 i_dword = GetDWLE( p_read ); \
433 #define AVI_READFOURCC( i_dword ) \
434 i_dword = GetFOURCC( p_read ); \
438 static int AVI_ChunkRead_avih( input_thread_t *p_input,
440 vlc_bool_t b_seekable )
444 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
445 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
446 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
447 AVI_READ4BYTES( p_chk->avih.i_flags );
448 AVI_READ4BYTES( p_chk->avih.i_totalframes );
449 AVI_READ4BYTES( p_chk->avih.i_initialframes );
450 AVI_READ4BYTES( p_chk->avih.i_streams );
451 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
452 AVI_READ4BYTES( p_chk->avih.i_width );
453 AVI_READ4BYTES( p_chk->avih.i_height );
454 AVI_READ4BYTES( p_chk->avih.i_scale );
455 AVI_READ4BYTES( p_chk->avih.i_rate );
456 AVI_READ4BYTES( p_chk->avih.i_start );
457 AVI_READ4BYTES( p_chk->avih.i_length );
460 "avih: streams:%d flags:%s%s%s%s %dx%d",
461 p_chk->avih.i_streams,
462 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
463 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
464 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
465 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
466 p_chk->avih.i_width, p_chk->avih.i_height );
468 AVI_READCHUNK_EXIT( VLC_SUCCESS );
471 static int AVI_ChunkRead_strh( input_thread_t *p_input,
473 vlc_bool_t b_seekable )
477 AVI_READFOURCC( p_chk->strh.i_type );
478 AVI_READFOURCC( p_chk->strh.i_handler );
479 AVI_READ4BYTES( p_chk->strh.i_flags );
480 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
481 AVI_READ4BYTES( p_chk->strh.i_initialframes );
482 AVI_READ4BYTES( p_chk->strh.i_scale );
483 AVI_READ4BYTES( p_chk->strh.i_rate );
484 AVI_READ4BYTES( p_chk->strh.i_start );
485 AVI_READ4BYTES( p_chk->strh.i_length );
486 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
487 AVI_READ4BYTES( p_chk->strh.i_quality );
488 AVI_READ4BYTES( p_chk->strh.i_samplesize );
491 "strh: type:%c%c%c%c handler:0x%8.8x samplesize:%d %.2ffps",
492 AVIFOURCC_PRINT( p_chk->strh.i_type ),
493 p_chk->strh.i_handler,
494 p_chk->strh.i_samplesize,
495 ( p_chk->strh.i_scale ?
496 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
499 AVI_READCHUNK_EXIT( VLC_SUCCESS );
502 static int AVI_ChunkRead_strf( input_thread_t *p_input,
504 vlc_bool_t b_seekable )
509 if( p_chk->common.p_father == NULL )
511 msg_Err( p_input, "malformed avi file" );
512 AVI_READCHUNK_EXIT( VLC_EGENERIC );
514 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
516 msg_Err( p_input, "malformed avi file" );
517 AVI_READCHUNK_EXIT( VLC_EGENERIC );
520 switch( p_strh->strh.i_type )
522 case( AVIFOURCC_auds ):
523 p_chk->strf.auds.p_wf = malloc( p_chk->common.i_chunk_size );
524 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
525 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
526 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
527 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
528 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
529 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
530 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM )
532 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
536 p_chk->strf.auds.p_wf->cbSize = 0;
538 if( p_chk->strf.auds.p_wf->cbSize > 0 )
540 memcpy( &p_chk->strf.auds.p_wf[1] ,
541 p_buff + sizeof( WAVEFORMATEX ),
542 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ));
546 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
547 p_chk->strf.auds.p_wf->wFormatTag,
548 p_chk->strf.auds.p_wf->nChannels,
549 p_chk->strf.auds.p_wf->nSamplesPerSec,
550 p_chk->strf.auds.p_wf->wBitsPerSample,
551 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
554 case( AVIFOURCC_vids ):
555 p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
556 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
557 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
558 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
559 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
560 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
561 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
562 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
563 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
564 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
565 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
566 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
567 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
568 memcpy( &p_chk->strf.vids.p_bih[1],
569 p_buff + sizeof(BITMAPINFOHEADER),
570 p_chk->common.i_chunk_size - sizeof(BITMAPINFOHEADER) );
573 "strf: video:%c%c%c%c %dx%d planes:%d %dbpp",
574 AVIFOURCC_PRINT( p_chk->strf.vids.p_bih->biCompression ),
575 p_chk->strf.vids.p_bih->biWidth,
576 p_chk->strf.vids.p_bih->biHeight,
577 p_chk->strf.vids.p_bih->biPlanes,
578 p_chk->strf.vids.p_bih->biBitCount );
582 msg_Warn( p_input, "unknown stream type" );
585 AVI_READCHUNK_EXIT( VLC_SUCCESS );
587 static void AVI_ChunkFree_strf( input_thread_t *p_input,
593 static int AVI_ChunkRead_strd( input_thread_t *p_input,
595 vlc_bool_t b_seekable )
598 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
599 memcpy( p_chk->strd.p_data,
601 p_chk->common.i_chunk_size );
602 AVI_READCHUNK_EXIT( VLC_SUCCESS );
605 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
607 vlc_bool_t b_seekable )
609 unsigned int i_count, i_index;
613 i_count = __MIN( p_chk->common.i_chunk_size, i_read ) / 16;
615 p_chk->idx1.i_entry_count = i_count;
616 p_chk->idx1.i_entry_max = i_count;
619 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
621 for( i_index = 0; i_index < i_count ; i_index++ )
623 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
624 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
625 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
626 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
631 p_chk->idx1.entry = NULL;
634 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
636 AVI_READCHUNK_EXIT( VLC_SUCCESS );
639 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
642 p_chk->idx1.i_entry_count = 0;
643 p_chk->idx1.i_entry_max = 0;
644 FREE( p_chk->idx1.entry )
649 vlc_fourcc_t i_fourcc;
653 { AVIFOURCC_IARL, "archive location" },
654 { AVIFOURCC_IART, "artist" },
655 { AVIFOURCC_ICMS, "commisioned" },
656 { AVIFOURCC_ICMT, "comments" },
657 { AVIFOURCC_ICOP, "copyright" },
658 { AVIFOURCC_ICRD, "creation date" },
659 { AVIFOURCC_ICRP, "cropped" },
660 { AVIFOURCC_IDIM, "dimensions" },
661 { AVIFOURCC_IDPI, "dots per inch" },
662 { AVIFOURCC_IENG, "enginner" },
663 { AVIFOURCC_IGNR, "genre" },
664 { AVIFOURCC_IKEY, "keywords" },
665 { AVIFOURCC_ILGT, "lightness" },
666 { AVIFOURCC_IMED, "medium" },
667 { AVIFOURCC_INAM, "name" },
668 { AVIFOURCC_IPLT, "palette setting" },
669 { AVIFOURCC_IPRD, "product" },
670 { AVIFOURCC_ISBJ, "subject" },
671 { AVIFOURCC_ISFT, "software" },
672 { AVIFOURCC_ISHP, "sharpness" },
673 { AVIFOURCC_ISRC, "source" },
674 { AVIFOURCC_ISRF, "source form" },
675 { AVIFOURCC_ITCH, "technician" },
676 { AVIFOURCC_ISMP, "time code" },
677 { AVIFOURCC_IDIT, "digitalization time" },
680 static int AVI_ChunkRead_strz( input_thread_t *p_input,
682 vlc_bool_t b_seekable )
685 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
688 for( i_index = 0;; i_index++)
690 if( !AVI_strz_type[i_index].i_fourcc ||
691 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
696 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
697 p_strz->p_str = malloc( i_read + 1);
699 if( p_strz->i_chunk_size )
701 memcpy( p_strz->p_str, p_read, i_read );
703 p_strz->p_str[i_read] = 0;
706 msg_Dbg( p_input, "%c%c%c%c: %s : %s",
707 AVIFOURCC_PRINT( p_strz->i_chunk_fourcc), p_strz->p_type, p_strz->p_str);
709 AVI_READCHUNK_EXIT( VLC_SUCCESS );
711 static void AVI_ChunkFree_strz( input_thread_t *p_input,
714 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
715 FREE( p_strz->p_type );
716 FREE( p_strz->p_str );
719 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
721 vlc_bool_t b_seekable )
723 return AVI_NextChunk( p_input, p_chk );
725 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
733 vlc_fourcc_t i_fourcc;
734 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
736 vlc_bool_t b_seekable );
737 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
738 avi_chunk_t *p_chk );
739 } AVI_Chunk_Function [] =
741 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
742 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
743 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
744 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
745 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
746 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
747 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
748 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
750 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
751 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
752 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
753 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
754 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
755 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
756 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
757 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
758 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
759 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
760 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
761 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
762 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
763 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
764 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
765 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
766 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
767 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
768 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
769 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
770 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
771 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
772 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
773 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
774 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
775 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
779 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
781 unsigned int i_index;
782 for( i_index = 0; ; i_index++ )
784 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
785 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
792 int _AVI_ChunkRead( input_thread_t *p_input,
794 avi_chunk_t *p_father,
795 vlc_bool_t b_seekable )
804 if( AVI_ChunkReadCommon( p_input, p_chk ) )
806 msg_Warn( p_input, "cannot read one chunk" );
809 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
811 msg_Warn( p_input, "found null fourcc chunk (corrupted file?)" );
814 p_chk->common.p_father = p_father;
816 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
817 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
819 return AVI_Chunk_Function[i_index].
820 AVI_ChunkRead_function( p_input, p_chk, b_seekable );
823 msg_Warn( p_input, "unknown chunk (not loaded)" );
824 return AVI_NextChunk( p_input, p_chk );
827 void _AVI_ChunkFree( input_thread_t *p_input,
831 avi_chunk_t *p_child, *p_next;
838 /* Free all child chunk */
839 p_child = p_chk->common.p_first;
842 p_next = p_child->common.p_next;
843 AVI_ChunkFree( p_input, p_child );
848 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
849 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
852 msg_Dbg( p_input, "free chunk %c%c%c%c",
853 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ) );
855 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
859 msg_Warn( p_input, "unknown chunk (not unloaded)" );
861 p_chk->common.p_first = NULL;
862 p_chk->common.p_last = NULL;
867 int AVI_ChunkReadRoot( input_thread_t *p_input,
869 vlc_bool_t b_seekable )
871 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
874 p_list->i_chunk_pos = 0;
875 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
876 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
877 p_list->p_father = NULL;
878 p_list->p_next = NULL;
879 p_list->p_first = NULL;
880 p_list->p_last = NULL;
882 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
886 p_chk = malloc( sizeof( avi_chunk_t ) );
887 memset( p_chk, 0, sizeof( avi_chunk_t ) );
888 if( !p_root->common.p_first )
890 p_root->common.p_first = p_chk;
894 p_root->common.p_last->common.p_next = p_chk;
896 p_root->common.p_last = p_chk;
898 if( AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
899 ( AVI_TellAbsolute( p_input ) >=
900 (off_t)p_chk->common.p_father->common.i_chunk_pos +
901 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
905 /* If we can't seek then stop when we 've found first RIFF-AVI */
906 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
907 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
916 void AVI_ChunkFreeRoot( input_thread_t *p_input,
919 AVI_ChunkFree( p_input, p_chk );
923 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
926 avi_chunk_t *p_child;
934 p_child = p_chk->common.p_first;
937 if( p_child->common.i_chunk_fourcc == i_fourcc ||
938 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
939 p_child->list.i_type == i_fourcc ) )
943 p_child = p_child->common.p_next;
948 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk,
949 vlc_fourcc_t i_fourcc, int i_number )
951 avi_chunk_t *p_child;
956 p_child = p_chk->common.p_first;
960 if( p_child->common.i_chunk_fourcc == i_fourcc ||
961 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
962 p_child->list.i_type == i_fourcc ) )
972 p_child = p_child->common.p_next;
977 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
978 avi_chunk_t *p_chk, int i_level )
982 avi_chunk_t *p_child;
984 memset( str, ' ', sizeof( str ) );
985 for( i = 1; i < i_level; i++ )
989 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
990 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
992 sprintf( str + i_level * 5,
993 "%c %c%c%c%c-%c%c%c%c size:"I64Fu" pos:"I64Fu,
995 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
996 AVIFOURCC_PRINT( p_chk->list.i_type ),
997 p_chk->common.i_chunk_size,
998 p_chk->common.i_chunk_pos );
1002 sprintf( str + i_level * 5,
1003 "+ %c%c%c%c size:"I64Fu" pos:"I64Fu,
1004 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
1005 p_chk->common.i_chunk_size,
1006 p_chk->common.i_chunk_pos );
1008 msg_Dbg( p_input, "%s", str );
1010 p_child = p_chk->common.p_first;
1013 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
1014 p_child = p_child->common.p_next;
1017 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
1018 avi_chunk_t *p_chk )
1020 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );