1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.16 2003/01/28 16:57:28 sam 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_Warn( 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 (off_t)__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.i_cat = AUDIO_ES;
524 p_chk->strf.auds.p_wf = malloc( p_chk->common.i_chunk_size );
525 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
526 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
527 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
528 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
529 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
530 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
531 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
532 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
534 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
535 /* prevent segfault */
536 if( p_chk->strf.auds.p_wf->cbSize >
537 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
539 p_chk->strf.auds.p_wf->cbSize =
540 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
545 p_chk->strf.auds.p_wf->cbSize = 0;
547 if( p_chk->strf.auds.p_wf->cbSize > 0 )
549 memcpy( &p_chk->strf.auds.p_wf[1] ,
550 p_buff + 8 + sizeof( WAVEFORMATEX ), // 8=fourrc+size
551 p_chk->strf.auds.p_wf->cbSize );
555 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
556 p_chk->strf.auds.p_wf->wFormatTag,
557 p_chk->strf.auds.p_wf->nChannels,
558 p_chk->strf.auds.p_wf->nSamplesPerSec,
559 p_chk->strf.auds.p_wf->wBitsPerSample,
560 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
563 case( AVIFOURCC_vids ):
564 p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
565 p_chk->strf.vids.i_cat = VIDEO_ES;
566 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
567 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
568 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
569 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
570 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
571 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
572 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
573 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
574 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
575 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
576 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
577 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
578 if( p_chk->strf.vids.p_bih->biSize >
579 p_chk->common.i_chunk_size )
581 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
583 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
585 memcpy( &p_chk->strf.vids.p_bih[1],
586 p_buff + 8 + sizeof(BITMAPINFOHEADER), // 8=fourrc+size
587 p_chk->strf.vids.p_bih->biSize -
588 sizeof(BITMAPINFOHEADER) );
592 "strf: video:%c%c%c%c %dx%d planes:%d %dbpp",
593 AVIFOURCC_PRINT( p_chk->strf.vids.p_bih->biCompression ),
594 p_chk->strf.vids.p_bih->biWidth,
595 p_chk->strf.vids.p_bih->biHeight,
596 p_chk->strf.vids.p_bih->biPlanes,
597 p_chk->strf.vids.p_bih->biBitCount );
601 msg_Warn( p_input, "unknown stream type" );
602 p_chk->strf.common.i_cat = UNKNOWN_ES;
605 AVI_READCHUNK_EXIT( VLC_SUCCESS );
607 static void AVI_ChunkFree_strf( input_thread_t *p_input,
610 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
611 switch( p_strf->common.i_cat )
614 FREE( p_strf->auds.p_wf );
617 FREE( p_strf->vids.p_bih );
624 static int AVI_ChunkRead_strd( input_thread_t *p_input,
626 vlc_bool_t b_seekable )
629 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
630 memcpy( p_chk->strd.p_data,
632 p_chk->common.i_chunk_size );
633 AVI_READCHUNK_EXIT( VLC_SUCCESS );
636 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
638 vlc_bool_t b_seekable )
640 unsigned int i_count, i_index;
644 i_count = __MIN( p_chk->common.i_chunk_size, i_read ) / 16;
646 p_chk->idx1.i_entry_count = i_count;
647 p_chk->idx1.i_entry_max = i_count;
650 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
652 for( i_index = 0; i_index < i_count ; i_index++ )
654 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
655 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
656 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
657 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
662 p_chk->idx1.entry = NULL;
665 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
667 AVI_READCHUNK_EXIT( VLC_SUCCESS );
670 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
673 p_chk->idx1.i_entry_count = 0;
674 p_chk->idx1.i_entry_max = 0;
675 FREE( p_chk->idx1.entry )
680 vlc_fourcc_t i_fourcc;
684 { AVIFOURCC_IARL, "archive location" },
685 { AVIFOURCC_IART, "artist" },
686 { AVIFOURCC_ICMS, "commisioned" },
687 { AVIFOURCC_ICMT, "comments" },
688 { AVIFOURCC_ICOP, "copyright" },
689 { AVIFOURCC_ICRD, "creation date" },
690 { AVIFOURCC_ICRP, "cropped" },
691 { AVIFOURCC_IDIM, "dimensions" },
692 { AVIFOURCC_IDPI, "dots per inch" },
693 { AVIFOURCC_IENG, "enginner" },
694 { AVIFOURCC_IGNR, "genre" },
695 { AVIFOURCC_IKEY, "keywords" },
696 { AVIFOURCC_ILGT, "lightness" },
697 { AVIFOURCC_IMED, "medium" },
698 { AVIFOURCC_INAM, "name" },
699 { AVIFOURCC_IPLT, "palette setting" },
700 { AVIFOURCC_IPRD, "product" },
701 { AVIFOURCC_ISBJ, "subject" },
702 { AVIFOURCC_ISFT, "software" },
703 { AVIFOURCC_ISHP, "sharpness" },
704 { AVIFOURCC_ISRC, "source" },
705 { AVIFOURCC_ISRF, "source form" },
706 { AVIFOURCC_ITCH, "technician" },
707 { AVIFOURCC_ISMP, "time code" },
708 { AVIFOURCC_IDIT, "digitalization time" },
711 static int AVI_ChunkRead_strz( input_thread_t *p_input,
713 vlc_bool_t b_seekable )
716 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
719 for( i_index = 0;; i_index++)
721 if( !AVI_strz_type[i_index].i_fourcc ||
722 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
727 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
728 p_strz->p_str = malloc( i_read + 1);
730 if( p_strz->i_chunk_size )
732 memcpy( p_strz->p_str, p_read, i_read );
734 p_strz->p_str[i_read] = 0;
737 msg_Dbg( p_input, "%c%c%c%c: %s : %s",
738 AVIFOURCC_PRINT( p_strz->i_chunk_fourcc), p_strz->p_type, p_strz->p_str);
740 AVI_READCHUNK_EXIT( VLC_SUCCESS );
742 static void AVI_ChunkFree_strz( input_thread_t *p_input,
745 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
746 FREE( p_strz->p_type );
747 FREE( p_strz->p_str );
750 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
752 vlc_bool_t b_seekable )
754 return AVI_NextChunk( p_input, p_chk );
756 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
764 vlc_fourcc_t i_fourcc;
765 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
767 vlc_bool_t b_seekable );
768 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
769 avi_chunk_t *p_chk );
770 } AVI_Chunk_Function [] =
772 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
773 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
774 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
775 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
776 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
777 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
778 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
779 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
781 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
782 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
783 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
784 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
785 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
786 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
787 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
788 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
789 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
790 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
791 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
792 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
793 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
794 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
795 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
796 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
797 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
798 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
799 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
800 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
801 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
802 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
803 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
804 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
805 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
806 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
810 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
812 unsigned int i_index;
813 for( i_index = 0; ; i_index++ )
815 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
816 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
823 int _AVI_ChunkRead( input_thread_t *p_input,
825 avi_chunk_t *p_father,
826 vlc_bool_t b_seekable )
835 if( AVI_ChunkReadCommon( p_input, p_chk ) )
837 msg_Warn( p_input, "cannot read one chunk" );
840 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
842 msg_Warn( p_input, "found null fourcc chunk (corrupted file?)" );
845 p_chk->common.p_father = p_father;
847 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
848 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
850 return AVI_Chunk_Function[i_index].
851 AVI_ChunkRead_function( p_input, p_chk, b_seekable );
854 msg_Warn( p_input, "unknown chunk (not loaded)" );
855 return AVI_NextChunk( p_input, p_chk );
858 void _AVI_ChunkFree( input_thread_t *p_input,
862 avi_chunk_t *p_child, *p_next;
869 /* Free all child chunk */
870 p_child = p_chk->common.p_first;
873 p_next = p_child->common.p_next;
874 AVI_ChunkFree( p_input, p_child );
879 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
880 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
883 msg_Dbg( p_input, "free chunk %c%c%c%c",
884 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ) );
886 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
890 msg_Warn( p_input, "unknown chunk (not unloaded)" );
892 p_chk->common.p_first = NULL;
893 p_chk->common.p_last = NULL;
898 int AVI_ChunkReadRoot( input_thread_t *p_input,
900 vlc_bool_t b_seekable )
902 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
905 p_list->i_chunk_pos = 0;
906 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
907 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
908 p_list->p_father = NULL;
909 p_list->p_next = NULL;
910 p_list->p_first = NULL;
911 p_list->p_last = NULL;
913 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
917 p_chk = malloc( sizeof( avi_chunk_t ) );
918 memset( p_chk, 0, sizeof( avi_chunk_t ) );
919 if( !p_root->common.p_first )
921 p_root->common.p_first = p_chk;
925 p_root->common.p_last->common.p_next = p_chk;
927 p_root->common.p_last = p_chk;
929 if( AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
930 ( AVI_TellAbsolute( p_input ) >=
931 (off_t)p_chk->common.p_father->common.i_chunk_pos +
932 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
936 /* If we can't seek then stop when we 've found first RIFF-AVI */
937 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
938 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
947 void AVI_ChunkFreeRoot( input_thread_t *p_input,
950 AVI_ChunkFree( p_input, p_chk );
954 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
957 avi_chunk_t *p_child;
965 p_child = p_chk->common.p_first;
968 if( p_child->common.i_chunk_fourcc == i_fourcc ||
969 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
970 p_child->list.i_type == i_fourcc ) )
974 p_child = p_child->common.p_next;
979 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk,
980 vlc_fourcc_t i_fourcc, int i_number )
982 avi_chunk_t *p_child;
987 p_child = p_chk->common.p_first;
991 if( p_child->common.i_chunk_fourcc == i_fourcc ||
992 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
993 p_child->list.i_type == i_fourcc ) )
1003 p_child = p_child->common.p_next;
1008 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
1009 avi_chunk_t *p_chk, int i_level )
1013 avi_chunk_t *p_child;
1015 memset( str, ' ', sizeof( str ) );
1016 for( i = 1; i < i_level; i++ )
1020 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
1021 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1023 sprintf( str + i_level * 5,
1024 "%c %c%c%c%c-%c%c%c%c size:"I64Fu" pos:"I64Fu,
1025 i_level ? '+' : '*',
1026 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
1027 AVIFOURCC_PRINT( p_chk->list.i_type ),
1028 p_chk->common.i_chunk_size,
1029 p_chk->common.i_chunk_pos );
1033 sprintf( str + i_level * 5,
1034 "+ %c%c%c%c size:"I64Fu" pos:"I64Fu,
1035 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
1036 p_chk->common.i_chunk_size,
1037 p_chk->common.i_chunk_pos );
1039 msg_Dbg( p_input, "%s", str );
1041 p_child = p_chk->common.p_first;
1044 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
1045 p_child = p_child->common.p_next;
1048 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
1049 avi_chunk_t *p_chk )
1051 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );