1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.17 2003/03/10 01:07:09 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;
80 vlc_mutex_unlock( &p_input->stream.stream_lock );
85 int AVI_SeekAbsolute( input_thread_t *p_input,
90 if( p_input->stream.p_selected_area->i_size > 0 &&
91 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 ||
105 i_pos - i_filepos < 0 ||
106 i_pos - i_filepos > 1024 ) )
108 input_AccessReinit( p_input );
109 p_input->pf_seek( p_input, i_pos );
112 else if( i_pos - i_filepos > 0 )
114 data_packet_t *p_data;
115 int i_skip = i_pos - i_filepos;
117 msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
120 return VLC_EGENERIC; // failed
126 i_read = input_SplitBuffer( p_input, &p_data,
127 __MIN( 4096, i_skip ) );
134 input_DeletePacket( p_input->p_method_data, p_data );
135 if( i_read == 0 && i_skip > 0 )
148 /* return amount read if success, 0 if failed */
149 int AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
151 data_packet_t *p_data;
164 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
169 memcpy( p_buff, p_data->p_payload_start, i_count );
170 input_DeletePacket( p_input->p_method_data, p_data );
181 int AVI_SkipBytes( input_thread_t *p_input, int i_count )
183 /* broken with new use of i_tell */
186 vlc_mutex_lock( &p_input->stream.stream_lock );
187 i_buff_size = p_input->p_last_data - p_input->p_current_data;
188 vlc_mutex_unlock( &p_input->stream.stream_lock );
190 if( i_count > 0 && i_count + 1 < i_buff_size )
194 input_Peek( p_input, &p_peek, i_count + 1 );
196 vlc_mutex_lock( &p_input->stream.stream_lock );
197 p_input->p_current_data += i_count; // skip them
198 vlc_mutex_unlock( &p_input->stream.stream_lock );
204 return AVI_SeekAbsolute( p_input,
205 AVI_TellAbsolute( p_input ) + i_count );
209 /*****************************************************************************
211 * AVI_TestFile: look at first bytes to see if it's a valid avi file
215 *****************************************************************************/
216 int AVI_TestFile( input_thread_t *p_input )
220 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
222 msg_Err( p_input, "cannot peek()" );
226 if( GetFOURCC( p_peek ) == AVIFOURCC_RIFF &&
227 GetFOURCC( p_peek + 8 ) == AVIFOURCC_AVI )
236 /****************************************************************************
238 * Basics functions to manipulates chunks
240 ****************************************************************************/
241 static int AVI_ChunkReadCommon( input_thread_t *p_input,
247 memset( p_chk, 0, sizeof( avi_chunk_t ) );
249 if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
254 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
255 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
256 p_chk->common.i_chunk_pos = AVI_TellAbsolute( p_input );
258 p_chk->common.p_father = NULL;
259 p_chk->common.p_next = NULL;
260 p_chk->common.p_first = NULL;
261 p_chk->common.p_next = NULL;
264 "Found Chunk fourcc:%8.8x (%c%c%c%c) size:"I64Fd" pos:"I64Fd,
265 p_chk->common.i_chunk_fourcc,
266 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
267 p_chk->common.i_chunk_size,
268 p_chk->common.i_chunk_pos );
273 static int AVI_NextChunk( input_thread_t *p_input,
280 if( AVI_ChunkReadCommon( p_input, &chk ) )
287 if( p_chk->common.p_father )
289 if( p_chk->common.p_father->common.i_chunk_pos +
290 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
291 p_chk->common.i_chunk_pos +
292 __EVEN( p_chk->common.i_chunk_size ) + 8 )
297 return AVI_SeekAbsolute( p_input,
298 p_chk->common.i_chunk_pos +
299 __EVEN( p_chk->common.i_chunk_size ) + 8 );
302 int _AVI_ChunkGoto( input_thread_t *p_input,
309 return AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos );
312 /****************************************************************************
314 * Functions to read chunks
316 ****************************************************************************/
317 static int AVI_ChunkRead_list( input_thread_t *p_input,
318 avi_chunk_t *p_container,
319 vlc_bool_t b_seekable )
324 if( p_container->common.i_chunk_size < 8 )
327 msg_Warn( p_input, "empty list chunk" );
330 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
332 msg_Warn( p_input, "cannot peek while reading list chunk" );
335 p_container->list.i_type = GetFOURCC( p_peek + 8 );
337 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
338 p_container->list.i_type == AVIFOURCC_movi )
340 msg_Dbg( p_input, "Skipping movi chunk" );
343 return AVI_NextChunk( p_input, p_container );
347 return VLC_SUCCESS; // point at begining of LIST-movi
351 if( AVI_SkipBytes( p_input, 12 ) )
353 msg_Warn( p_input, "cannot enter chunk" );
358 "found LIST chunk: \'%c%c%c%c\'",
359 AVIFOURCC_PRINT( p_container->list.i_type ) );
361 msg_Dbg( p_input, "<list \'%c%c%c%c\'>", AVIFOURCC_PRINT( p_container->list.i_type ) );
364 p_chk = malloc( sizeof( avi_chunk_t ) );
365 memset( p_chk, 0, sizeof( avi_chunk_t ) );
366 if( !p_container->common.p_first )
368 p_container->common.p_first = p_chk;
372 p_container->common.p_last->common.p_next = p_chk;
374 p_container->common.p_last = p_chk;
376 if( AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
377 ( AVI_TellAbsolute( p_input ) >=
378 (off_t)p_chk->common.p_father->common.i_chunk_pos +
379 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
383 /* If we can't seek then stop when we 've found LIST-movi */
384 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
385 p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
391 msg_Dbg( p_input, "</list \'%c%c%c%c\'>", AVIFOURCC_PRINT( p_container->list.i_type ) );
396 #define AVI_READCHUNK_ENTER \
397 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
398 uint8_t *p_read, *p_buff; \
399 if( !( p_read = p_buff = malloc(i_read ) ) ) \
403 i_read = AVI_ReadData( p_input, p_read, i_read ); \
407 #define AVI_READCHUNK_EXIT( code ) \
411 msg_Warn( p_input, "not enough data" ); \
414 #define AVI_READ2BYTES( i_word ) \
415 i_word = GetWLE( p_read ); \
419 #define AVI_READ4BYTES( i_dword ) \
420 i_dword = GetDWLE( p_read ); \
424 #define AVI_READFOURCC( i_dword ) \
425 i_dword = GetFOURCC( p_read ); \
429 static int AVI_ChunkRead_avih( input_thread_t *p_input,
431 vlc_bool_t b_seekable )
435 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
436 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
437 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
438 AVI_READ4BYTES( p_chk->avih.i_flags );
439 AVI_READ4BYTES( p_chk->avih.i_totalframes );
440 AVI_READ4BYTES( p_chk->avih.i_initialframes );
441 AVI_READ4BYTES( p_chk->avih.i_streams );
442 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
443 AVI_READ4BYTES( p_chk->avih.i_width );
444 AVI_READ4BYTES( p_chk->avih.i_height );
445 AVI_READ4BYTES( p_chk->avih.i_scale );
446 AVI_READ4BYTES( p_chk->avih.i_rate );
447 AVI_READ4BYTES( p_chk->avih.i_start );
448 AVI_READ4BYTES( p_chk->avih.i_length );
451 "avih: streams:%d flags:%s%s%s%s %dx%d",
452 p_chk->avih.i_streams,
453 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
454 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
455 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
456 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
457 p_chk->avih.i_width, p_chk->avih.i_height );
459 AVI_READCHUNK_EXIT( VLC_SUCCESS );
462 static int AVI_ChunkRead_strh( input_thread_t *p_input,
464 vlc_bool_t b_seekable )
468 AVI_READFOURCC( p_chk->strh.i_type );
469 AVI_READFOURCC( p_chk->strh.i_handler );
470 AVI_READ4BYTES( p_chk->strh.i_flags );
471 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
472 AVI_READ4BYTES( p_chk->strh.i_initialframes );
473 AVI_READ4BYTES( p_chk->strh.i_scale );
474 AVI_READ4BYTES( p_chk->strh.i_rate );
475 AVI_READ4BYTES( p_chk->strh.i_start );
476 AVI_READ4BYTES( p_chk->strh.i_length );
477 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
478 AVI_READ4BYTES( p_chk->strh.i_quality );
479 AVI_READ4BYTES( p_chk->strh.i_samplesize );
482 "strh: type:%c%c%c%c handler:0x%8.8x samplesize:%d %.2ffps",
483 AVIFOURCC_PRINT( p_chk->strh.i_type ),
484 p_chk->strh.i_handler,
485 p_chk->strh.i_samplesize,
486 ( p_chk->strh.i_scale ?
487 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
490 AVI_READCHUNK_EXIT( VLC_SUCCESS );
493 static int AVI_ChunkRead_strf( input_thread_t *p_input,
495 vlc_bool_t b_seekable )
500 if( p_chk->common.p_father == NULL )
502 msg_Err( p_input, "malformed avi file" );
503 AVI_READCHUNK_EXIT( VLC_EGENERIC );
505 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
507 msg_Err( p_input, "malformed avi file" );
508 AVI_READCHUNK_EXIT( VLC_EGENERIC );
511 switch( p_strh->strh.i_type )
513 case( AVIFOURCC_auds ):
514 p_chk->strf.auds.i_cat = AUDIO_ES;
515 p_chk->strf.auds.p_wf = malloc( p_chk->common.i_chunk_size );
516 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
517 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
518 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
519 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
520 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
521 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
522 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
523 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
525 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
526 /* prevent segfault */
527 if( p_chk->strf.auds.p_wf->cbSize >
528 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
530 p_chk->strf.auds.p_wf->cbSize =
531 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
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 + 8 + sizeof( WAVEFORMATEX ), // 8=fourrc+size
542 p_chk->strf.auds.p_wf->cbSize );
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.i_cat = VIDEO_ES;
557 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
558 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
559 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
560 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
561 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
562 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
563 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
564 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
565 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
566 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
567 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
568 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
569 if( p_chk->strf.vids.p_bih->biSize >
570 p_chk->common.i_chunk_size )
572 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
574 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
576 memcpy( &p_chk->strf.vids.p_bih[1],
577 p_buff + 8 + sizeof(BITMAPINFOHEADER), // 8=fourrc+size
578 p_chk->strf.vids.p_bih->biSize -
579 sizeof(BITMAPINFOHEADER) );
583 "strf: video:%c%c%c%c %dx%d planes:%d %dbpp",
584 AVIFOURCC_PRINT( p_chk->strf.vids.p_bih->biCompression ),
585 p_chk->strf.vids.p_bih->biWidth,
586 p_chk->strf.vids.p_bih->biHeight,
587 p_chk->strf.vids.p_bih->biPlanes,
588 p_chk->strf.vids.p_bih->biBitCount );
592 msg_Warn( p_input, "unknown stream type" );
593 p_chk->strf.common.i_cat = UNKNOWN_ES;
596 AVI_READCHUNK_EXIT( VLC_SUCCESS );
598 static void AVI_ChunkFree_strf( input_thread_t *p_input,
601 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
602 switch( p_strf->common.i_cat )
605 FREE( p_strf->auds.p_wf );
608 FREE( p_strf->vids.p_bih );
615 static int AVI_ChunkRead_strd( input_thread_t *p_input,
617 vlc_bool_t b_seekable )
620 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
621 memcpy( p_chk->strd.p_data,
623 p_chk->common.i_chunk_size );
624 AVI_READCHUNK_EXIT( VLC_SUCCESS );
627 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
629 vlc_bool_t b_seekable )
631 unsigned int i_count, i_index;
635 i_count = __MIN( p_chk->common.i_chunk_size, i_read ) / 16;
637 p_chk->idx1.i_entry_count = i_count;
638 p_chk->idx1.i_entry_max = i_count;
641 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
643 for( i_index = 0; i_index < i_count ; i_index++ )
645 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
646 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
647 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
648 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
653 p_chk->idx1.entry = NULL;
656 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
658 AVI_READCHUNK_EXIT( VLC_SUCCESS );
661 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
664 p_chk->idx1.i_entry_count = 0;
665 p_chk->idx1.i_entry_max = 0;
666 FREE( p_chk->idx1.entry )
671 vlc_fourcc_t i_fourcc;
675 { AVIFOURCC_IARL, "archive location" },
676 { AVIFOURCC_IART, "artist" },
677 { AVIFOURCC_ICMS, "commisioned" },
678 { AVIFOURCC_ICMT, "comments" },
679 { AVIFOURCC_ICOP, "copyright" },
680 { AVIFOURCC_ICRD, "creation date" },
681 { AVIFOURCC_ICRP, "cropped" },
682 { AVIFOURCC_IDIM, "dimensions" },
683 { AVIFOURCC_IDPI, "dots per inch" },
684 { AVIFOURCC_IENG, "enginner" },
685 { AVIFOURCC_IGNR, "genre" },
686 { AVIFOURCC_IKEY, "keywords" },
687 { AVIFOURCC_ILGT, "lightness" },
688 { AVIFOURCC_IMED, "medium" },
689 { AVIFOURCC_INAM, "name" },
690 { AVIFOURCC_IPLT, "palette setting" },
691 { AVIFOURCC_IPRD, "product" },
692 { AVIFOURCC_ISBJ, "subject" },
693 { AVIFOURCC_ISFT, "software" },
694 { AVIFOURCC_ISHP, "sharpness" },
695 { AVIFOURCC_ISRC, "source" },
696 { AVIFOURCC_ISRF, "source form" },
697 { AVIFOURCC_ITCH, "technician" },
698 { AVIFOURCC_ISMP, "time code" },
699 { AVIFOURCC_IDIT, "digitalization time" },
702 static int AVI_ChunkRead_strz( input_thread_t *p_input,
704 vlc_bool_t b_seekable )
707 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
710 for( i_index = 0;; i_index++)
712 if( !AVI_strz_type[i_index].i_fourcc ||
713 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
718 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
719 p_strz->p_str = malloc( i_read + 1);
721 if( p_strz->i_chunk_size )
723 memcpy( p_strz->p_str, p_read, i_read );
725 p_strz->p_str[i_read] = 0;
728 msg_Dbg( p_input, "%c%c%c%c: %s : %s",
729 AVIFOURCC_PRINT( p_strz->i_chunk_fourcc), p_strz->p_type, p_strz->p_str);
731 AVI_READCHUNK_EXIT( VLC_SUCCESS );
733 static void AVI_ChunkFree_strz( input_thread_t *p_input,
736 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
737 FREE( p_strz->p_type );
738 FREE( p_strz->p_str );
741 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
743 vlc_bool_t b_seekable )
745 return AVI_NextChunk( p_input, p_chk );
747 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
755 vlc_fourcc_t i_fourcc;
756 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
758 vlc_bool_t b_seekable );
759 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
760 avi_chunk_t *p_chk );
761 } AVI_Chunk_Function [] =
763 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
764 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
765 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
766 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
767 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
768 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
769 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
770 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
772 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
773 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
774 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
775 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
776 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
777 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
778 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
779 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
780 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
781 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
782 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
783 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
784 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
785 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
786 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
787 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
788 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
789 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
790 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
791 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
792 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
793 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
794 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
795 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
796 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
797 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
801 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
803 unsigned int i_index;
804 for( i_index = 0; ; i_index++ )
806 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
807 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
814 int _AVI_ChunkRead( input_thread_t *p_input,
816 avi_chunk_t *p_father,
817 vlc_bool_t b_seekable )
826 if( AVI_ChunkReadCommon( p_input, p_chk ) )
828 msg_Warn( p_input, "cannot read one chunk" );
831 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
833 msg_Warn( p_input, "found null fourcc chunk (corrupted file?)" );
836 p_chk->common.p_father = p_father;
838 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
839 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
841 return AVI_Chunk_Function[i_index].
842 AVI_ChunkRead_function( p_input, p_chk, b_seekable );
845 msg_Warn( p_input, "unknown chunk (not loaded)" );
846 return AVI_NextChunk( p_input, p_chk );
849 void _AVI_ChunkFree( input_thread_t *p_input,
853 avi_chunk_t *p_child, *p_next;
860 /* Free all child chunk */
861 p_child = p_chk->common.p_first;
864 p_next = p_child->common.p_next;
865 AVI_ChunkFree( p_input, p_child );
870 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
871 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
874 msg_Dbg( p_input, "free chunk %c%c%c%c",
875 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ) );
877 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
881 msg_Warn( p_input, "unknown chunk (not unloaded)" );
883 p_chk->common.p_first = NULL;
884 p_chk->common.p_last = NULL;
889 int AVI_ChunkReadRoot( input_thread_t *p_input,
891 vlc_bool_t b_seekable )
893 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
896 p_list->i_chunk_pos = 0;
897 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
898 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
899 p_list->p_father = NULL;
900 p_list->p_next = NULL;
901 p_list->p_first = NULL;
902 p_list->p_last = NULL;
904 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
908 p_chk = malloc( sizeof( avi_chunk_t ) );
909 memset( p_chk, 0, sizeof( avi_chunk_t ) );
910 if( !p_root->common.p_first )
912 p_root->common.p_first = p_chk;
916 p_root->common.p_last->common.p_next = p_chk;
918 p_root->common.p_last = p_chk;
920 if( AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
921 ( AVI_TellAbsolute( p_input ) >=
922 (off_t)p_chk->common.p_father->common.i_chunk_pos +
923 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
927 /* If we can't seek then stop when we 've found first RIFF-AVI */
928 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
929 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
938 void AVI_ChunkFreeRoot( input_thread_t *p_input,
941 AVI_ChunkFree( p_input, p_chk );
945 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
948 avi_chunk_t *p_child;
956 p_child = p_chk->common.p_first;
959 if( p_child->common.i_chunk_fourcc == i_fourcc ||
960 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
961 p_child->list.i_type == i_fourcc ) )
965 p_child = p_child->common.p_next;
970 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk,
971 vlc_fourcc_t i_fourcc, int i_number )
973 avi_chunk_t *p_child;
978 p_child = p_chk->common.p_first;
982 if( p_child->common.i_chunk_fourcc == i_fourcc ||
983 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
984 p_child->list.i_type == i_fourcc ) )
994 p_child = p_child->common.p_next;
999 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
1000 avi_chunk_t *p_chk, int i_level )
1004 avi_chunk_t *p_child;
1006 memset( str, ' ', sizeof( str ) );
1007 for( i = 1; i < i_level; i++ )
1011 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
1012 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1014 sprintf( str + i_level * 5,
1015 "%c %c%c%c%c-%c%c%c%c size:"I64Fu" pos:"I64Fu,
1016 i_level ? '+' : '*',
1017 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
1018 AVIFOURCC_PRINT( p_chk->list.i_type ),
1019 p_chk->common.i_chunk_size,
1020 p_chk->common.i_chunk_pos );
1024 sprintf( str + i_level * 5,
1025 "+ %c%c%c%c size:"I64Fu" pos:"I64Fu,
1026 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
1027 p_chk->common.i_chunk_size,
1028 p_chk->common.i_chunk_pos );
1030 msg_Dbg( p_input, "%s", str );
1032 p_child = p_chk->common.p_first;
1035 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
1036 p_child = p_child->common.p_next;
1039 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
1040 avi_chunk_t *p_chk )
1042 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );