1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.22 2003/08/17 23:02:52 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>
36 if( p ) {free( p ); p = NULL; }
38 #define __EVEN( x ) ( (x)&0x01 ? (x)+1 : (x) )
40 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
42 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
44 /*****************************************************************************
45 * Some basic functions to manipulate stream more easily in vlc
47 * AVI_TellAbsolute get file position
49 * AVI_SeekAbsolute seek in the file
51 * AVI_ReadData read data from the file in a buffer
53 * AVI_SkipBytes skip bytes
55 *****************************************************************************/
56 off_t AVI_TellAbsolute( input_thread_t *p_input )
60 vlc_mutex_lock( &p_input->stream.stream_lock );
62 i_pos= p_input->stream.p_selected_area->i_tell;
64 vlc_mutex_unlock( &p_input->stream.stream_lock );
69 int AVI_SeekAbsolute( input_thread_t *p_input,
74 if( p_input->stream.p_selected_area->i_size > 0 &&
75 i_pos >= p_input->stream.p_selected_area->i_size )
80 i_filepos = AVI_TellAbsolute( p_input );
82 if( i_filepos == i_pos )
87 if( p_input->stream.b_seekable &&
88 ( p_input->stream.i_method == INPUT_METHOD_FILE ||
89 i_pos - i_filepos < 0 ||
90 i_pos - i_filepos > 1024 ) )
92 input_AccessReinit( p_input );
93 p_input->pf_seek( p_input, i_pos );
96 else if( i_pos - i_filepos > 0 )
98 data_packet_t *p_data;
99 int i_skip = i_pos - i_filepos;
101 msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
104 return VLC_EGENERIC; // failed
110 i_read = input_SplitBuffer( p_input, &p_data,
111 __MIN( 4096, i_skip ) );
119 input_DeletePacket( p_input->p_method_data, p_data );
129 /* return amount read if success, 0 if failed */
130 int AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
132 data_packet_t *p_data;
145 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
150 memcpy( p_buff, p_data->p_payload_start, i_count );
151 input_DeletePacket( p_input->p_method_data, p_data );
162 int AVI_SkipBytes( input_thread_t *p_input, int64_t i_count )
164 /* broken with new use of i_tell */
167 vlc_mutex_lock( &p_input->stream.stream_lock );
168 i_buff_size = p_input->p_last_data - p_input->p_current_data;
169 vlc_mutex_unlock( &p_input->stream.stream_lock );
171 if( i_count > 0 && i_count + 1 < i_buff_size )
175 input_Peek( p_input, &p_peek, i_count + 1 );
177 vlc_mutex_lock( &p_input->stream.stream_lock );
178 p_input->p_current_data += i_count; // skip them
179 vlc_mutex_unlock( &p_input->stream.stream_lock );
185 return AVI_SeekAbsolute( p_input,
186 AVI_TellAbsolute( p_input ) + i_count );
190 /*****************************************************************************
192 * AVI_TestFile: look at first bytes to see if it's a valid avi file
196 *****************************************************************************/
197 int AVI_TestFile( input_thread_t *p_input )
201 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
203 msg_Err( p_input, "cannot peek()" );
207 if( GetFOURCC( p_peek ) == AVIFOURCC_RIFF &&
208 GetFOURCC( p_peek + 8 ) == AVIFOURCC_AVI )
217 /****************************************************************************
219 * Basics functions to manipulates chunks
221 ****************************************************************************/
222 static int AVI_ChunkReadCommon( input_thread_t *p_input,
228 memset( p_chk, 0, sizeof( avi_chunk_t ) );
230 if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
235 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
236 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
237 p_chk->common.i_chunk_pos = AVI_TellAbsolute( p_input );
239 p_chk->common.p_father = NULL;
240 p_chk->common.p_next = NULL;
241 p_chk->common.p_first = NULL;
242 p_chk->common.p_next = NULL;
245 "Found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
246 p_chk->common.i_chunk_fourcc,
247 (char*)&p_chk->common.i_chunk_fourcc,
248 p_chk->common.i_chunk_size,
249 p_chk->common.i_chunk_pos );
254 static int AVI_NextChunk( input_thread_t *p_input,
261 if( AVI_ChunkReadCommon( p_input, &chk ) )
268 if( p_chk->common.p_father )
270 if( p_chk->common.p_father->common.i_chunk_pos +
271 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
272 p_chk->common.i_chunk_pos +
273 __EVEN( p_chk->common.i_chunk_size ) + 8 )
278 return AVI_SeekAbsolute( p_input,
279 p_chk->common.i_chunk_pos +
280 __EVEN( p_chk->common.i_chunk_size ) + 8 );
283 int _AVI_ChunkGoto( input_thread_t *p_input,
290 return AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos );
293 /****************************************************************************
295 * Functions to read chunks
297 ****************************************************************************/
298 static int AVI_ChunkRead_list( input_thread_t *p_input,
299 avi_chunk_t *p_container,
300 vlc_bool_t b_seekable )
305 if( p_container->common.i_chunk_size < 8 )
308 msg_Warn( p_input, "empty list chunk" );
311 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
313 msg_Warn( p_input, "cannot peek while reading list chunk" );
316 p_container->list.i_type = GetFOURCC( p_peek + 8 );
318 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
319 p_container->list.i_type == AVIFOURCC_movi )
321 msg_Dbg( p_input, "Skipping movi chunk" );
324 return AVI_NextChunk( p_input, p_container );
328 return VLC_SUCCESS; // point at begining of LIST-movi
332 if( AVI_SkipBytes( p_input, 12 ) )
334 msg_Warn( p_input, "cannot enter chunk" );
339 "found LIST chunk: \'%4.4s\'",
340 (char*)&p_container->list.i_type );
342 msg_Dbg( p_input, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
345 p_chk = malloc( sizeof( avi_chunk_t ) );
346 memset( p_chk, 0, sizeof( avi_chunk_t ) );
347 if( !p_container->common.p_first )
349 p_container->common.p_first = p_chk;
353 p_container->common.p_last->common.p_next = p_chk;
355 p_container->common.p_last = p_chk;
357 if( AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
358 ( AVI_TellAbsolute( p_input ) >=
359 (off_t)p_chk->common.p_father->common.i_chunk_pos +
360 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
364 /* If we can't seek then stop when we 've found LIST-movi */
365 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
366 p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
372 msg_Dbg( p_input, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
377 #define AVI_READCHUNK_ENTER \
378 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
379 uint8_t *p_read, *p_buff; \
380 if( !( p_read = p_buff = malloc(i_read ) ) ) \
384 i_read = AVI_ReadData( p_input, p_read, i_read ); \
388 #define AVI_READCHUNK_EXIT( code ) \
392 msg_Warn( p_input, "not enough data" ); \
396 #define AVI_READ1BYTE( i_byte ) \
401 #define AVI_READ2BYTES( i_word ) \
402 i_word = GetWLE( p_read ); \
406 #define AVI_READ4BYTES( i_dword ) \
407 i_dword = GetDWLE( p_read ); \
411 #define AVI_READ8BYTES( i_dword ) \
412 i_dword = GetQWLE( p_read ); \
416 #define AVI_READFOURCC( i_dword ) \
417 i_dword = GetFOURCC( p_read ); \
421 static int AVI_ChunkRead_avih( input_thread_t *p_input,
423 vlc_bool_t b_seekable )
427 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
428 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
429 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
430 AVI_READ4BYTES( p_chk->avih.i_flags );
431 AVI_READ4BYTES( p_chk->avih.i_totalframes );
432 AVI_READ4BYTES( p_chk->avih.i_initialframes );
433 AVI_READ4BYTES( p_chk->avih.i_streams );
434 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
435 AVI_READ4BYTES( p_chk->avih.i_width );
436 AVI_READ4BYTES( p_chk->avih.i_height );
437 AVI_READ4BYTES( p_chk->avih.i_scale );
438 AVI_READ4BYTES( p_chk->avih.i_rate );
439 AVI_READ4BYTES( p_chk->avih.i_start );
440 AVI_READ4BYTES( p_chk->avih.i_length );
443 "avih: streams:%d flags:%s%s%s%s %dx%d",
444 p_chk->avih.i_streams,
445 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
446 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
447 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
448 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
449 p_chk->avih.i_width, p_chk->avih.i_height );
451 AVI_READCHUNK_EXIT( VLC_SUCCESS );
454 static int AVI_ChunkRead_strh( input_thread_t *p_input,
456 vlc_bool_t b_seekable )
460 AVI_READFOURCC( p_chk->strh.i_type );
461 AVI_READFOURCC( p_chk->strh.i_handler );
462 AVI_READ4BYTES( p_chk->strh.i_flags );
463 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
464 AVI_READ4BYTES( p_chk->strh.i_initialframes );
465 AVI_READ4BYTES( p_chk->strh.i_scale );
466 AVI_READ4BYTES( p_chk->strh.i_rate );
467 AVI_READ4BYTES( p_chk->strh.i_start );
468 AVI_READ4BYTES( p_chk->strh.i_length );
469 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
470 AVI_READ4BYTES( p_chk->strh.i_quality );
471 AVI_READ4BYTES( p_chk->strh.i_samplesize );
474 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
475 (char*)&p_chk->strh.i_type,
476 p_chk->strh.i_handler,
477 p_chk->strh.i_samplesize,
478 ( p_chk->strh.i_scale ?
479 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
482 AVI_READCHUNK_EXIT( VLC_SUCCESS );
485 static int AVI_ChunkRead_strf( input_thread_t *p_input,
487 vlc_bool_t b_seekable )
492 if( p_chk->common.p_father == NULL )
494 msg_Err( p_input, "malformed avi file" );
495 AVI_READCHUNK_EXIT( VLC_EGENERIC );
497 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
499 msg_Err( p_input, "malformed avi file" );
500 AVI_READCHUNK_EXIT( VLC_EGENERIC );
503 switch( p_strh->strh.i_type )
505 case( AVIFOURCC_auds ):
506 p_chk->strf.auds.i_cat = AUDIO_ES;
507 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
508 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
509 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
510 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
511 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
512 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
513 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
514 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
515 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
517 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
518 /* prevent segfault */
519 if( p_chk->strf.auds.p_wf->cbSize >
520 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
522 p_chk->strf.auds.p_wf->cbSize =
523 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
528 p_chk->strf.auds.p_wf->cbSize = 0;
530 if( p_chk->strf.auds.p_wf->cbSize > 0 )
532 memcpy( &p_chk->strf.auds.p_wf[1] ,
533 p_buff + 8 + sizeof( WAVEFORMATEX ), // 8=fourrc+size
534 p_chk->strf.auds.p_wf->cbSize );
538 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
539 p_chk->strf.auds.p_wf->wFormatTag,
540 p_chk->strf.auds.p_wf->nChannels,
541 p_chk->strf.auds.p_wf->nSamplesPerSec,
542 p_chk->strf.auds.p_wf->wBitsPerSample,
543 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
546 case( AVIFOURCC_vids ):
547 p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
548 p_chk->strf.vids.i_cat = VIDEO_ES;
549 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
550 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
551 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
552 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
553 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
554 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
555 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
556 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
557 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
558 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
559 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
560 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
561 if( p_chk->strf.vids.p_bih->biSize >
562 p_chk->common.i_chunk_size )
564 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
566 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
568 memcpy( &p_chk->strf.vids.p_bih[1],
569 p_buff + 8 + sizeof(BITMAPINFOHEADER), // 8=fourrc+size
570 p_chk->strf.vids.p_bih->biSize -
571 sizeof(BITMAPINFOHEADER) );
575 "strf: video:%4.4s %dx%d planes:%d %dbpp",
576 (char*)&p_chk->strf.vids.p_bih->biCompression,
577 p_chk->strf.vids.p_bih->biWidth,
578 p_chk->strf.vids.p_bih->biHeight,
579 p_chk->strf.vids.p_bih->biPlanes,
580 p_chk->strf.vids.p_bih->biBitCount );
584 msg_Warn( p_input, "unknown stream type" );
585 p_chk->strf.common.i_cat = UNKNOWN_ES;
588 AVI_READCHUNK_EXIT( VLC_SUCCESS );
590 static void AVI_ChunkFree_strf( input_thread_t *p_input,
593 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
594 switch( p_strf->common.i_cat )
597 FREE( p_strf->auds.p_wf );
600 FREE( p_strf->vids.p_bih );
607 static int AVI_ChunkRead_strd( input_thread_t *p_input,
609 vlc_bool_t b_seekable )
612 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
613 memcpy( p_chk->strd.p_data,
615 p_chk->common.i_chunk_size );
616 AVI_READCHUNK_EXIT( VLC_SUCCESS );
619 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
621 vlc_bool_t b_seekable )
623 unsigned int i_count, i_index;
627 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
629 p_chk->idx1.i_entry_count = i_count;
630 p_chk->idx1.i_entry_max = i_count;
633 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
635 for( i_index = 0; i_index < i_count ; i_index++ )
637 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
638 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
639 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
640 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
645 p_chk->idx1.entry = NULL;
648 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
650 AVI_READCHUNK_EXIT( VLC_SUCCESS );
653 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
656 p_chk->idx1.i_entry_count = 0;
657 p_chk->idx1.i_entry_max = 0;
658 FREE( p_chk->idx1.entry )
663 static int AVI_ChunkRead_indx( input_thread_t *p_input,
665 vlc_bool_t b_seekable )
667 unsigned int i_count, i;
669 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
673 AVI_READ2BYTES( p_indx->i_longsperentry );
674 AVI_READ1BYTE ( p_indx->i_indexsubtype );
675 AVI_READ1BYTE ( p_indx->i_indextype );
676 AVI_READ4BYTES( p_indx->i_entriesinuse );
678 AVI_READ4BYTES( p_indx->i_id );
679 p_indx->idx.std = NULL;
680 p_indx->idx.field = NULL;
681 p_indx->idx.super = NULL;
683 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
685 AVI_READ8BYTES( p_indx->i_baseoffset );
686 AVI_READ4BYTES( i_dummy );
688 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
689 p_indx->i_entriesinuse = i_count;
690 p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
692 for( i = 0; i < i_count; i++ )
694 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
695 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
698 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
700 AVI_READ8BYTES( p_indx->i_baseoffset );
701 AVI_READ4BYTES( i_dummy );
703 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
704 p_indx->i_entriesinuse = i_count;
705 p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
706 for( i = 0; i < i_count; i++ )
708 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
709 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
710 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
713 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
715 p_indx->i_baseoffset = 0;
716 AVI_READ4BYTES( i_dummy );
717 AVI_READ4BYTES( i_dummy );
718 AVI_READ4BYTES( i_dummy );
720 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
721 p_indx->i_entriesinuse = i_count;
722 p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
724 for( i = 0; i < i_count; i++ )
726 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
727 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
728 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
733 msg_Warn( p_input, "unknow type/subtype index" );
737 msg_Dbg( p_input, "indx: type=%d subtype=%d entry=%d", p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
739 AVI_READCHUNK_EXIT( VLC_SUCCESS );
741 static void AVI_ChunkFree_indx( input_thread_t *p_input,
744 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
746 FREE( p_indx->idx.std );
747 FREE( p_indx->idx.field );
748 FREE( p_indx->idx.super );
755 vlc_fourcc_t i_fourcc;
759 { AVIFOURCC_IARL, "archive location" },
760 { AVIFOURCC_IART, "artist" },
761 { AVIFOURCC_ICMS, "commisioned" },
762 { AVIFOURCC_ICMT, "comments" },
763 { AVIFOURCC_ICOP, "copyright" },
764 { AVIFOURCC_ICRD, "creation date" },
765 { AVIFOURCC_ICRP, "cropped" },
766 { AVIFOURCC_IDIM, "dimensions" },
767 { AVIFOURCC_IDPI, "dots per inch" },
768 { AVIFOURCC_IENG, "enginner" },
769 { AVIFOURCC_IGNR, "genre" },
770 { AVIFOURCC_IKEY, "keywords" },
771 { AVIFOURCC_ILGT, "lightness" },
772 { AVIFOURCC_IMED, "medium" },
773 { AVIFOURCC_INAM, "name" },
774 { AVIFOURCC_IPLT, "palette setting" },
775 { AVIFOURCC_IPRD, "product" },
776 { AVIFOURCC_ISBJ, "subject" },
777 { AVIFOURCC_ISFT, "software" },
778 { AVIFOURCC_ISHP, "sharpness" },
779 { AVIFOURCC_ISRC, "source" },
780 { AVIFOURCC_ISRF, "source form" },
781 { AVIFOURCC_ITCH, "technician" },
782 { AVIFOURCC_ISMP, "time code" },
783 { AVIFOURCC_IDIT, "digitalization time" },
784 { AVIFOURCC_strn, "stream name" },
787 static int AVI_ChunkRead_strz( input_thread_t *p_input,
789 vlc_bool_t b_seekable )
792 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
795 for( i_index = 0;; i_index++)
797 if( !AVI_strz_type[i_index].i_fourcc ||
798 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
803 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
804 p_strz->p_str = malloc( i_read + 1);
806 if( p_strz->i_chunk_size )
808 memcpy( p_strz->p_str, p_read, i_read );
810 p_strz->p_str[i_read] = 0;
813 msg_Dbg( p_input, "%4.4s: %s : %s",
814 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
816 AVI_READCHUNK_EXIT( VLC_SUCCESS );
818 static void AVI_ChunkFree_strz( input_thread_t *p_input,
821 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
822 FREE( p_strz->p_type );
823 FREE( p_strz->p_str );
826 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
828 vlc_bool_t b_seekable )
830 return AVI_NextChunk( p_input, p_chk );
832 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
840 vlc_fourcc_t i_fourcc;
841 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
843 vlc_bool_t b_seekable );
844 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
845 avi_chunk_t *p_chk );
846 } AVI_Chunk_Function [] =
848 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
849 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
850 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
851 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
852 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
853 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
854 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
855 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
856 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
858 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
859 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
860 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
861 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
862 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
863 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
864 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
865 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
866 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
867 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
868 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
869 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
870 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
871 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
872 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
873 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
874 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
875 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
876 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
877 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
878 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
879 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
880 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
881 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
882 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
883 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
884 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
888 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
890 unsigned int i_index;
891 for( i_index = 0; ; i_index++ )
893 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
894 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
901 int _AVI_ChunkRead( input_thread_t *p_input,
903 avi_chunk_t *p_father,
904 vlc_bool_t b_seekable )
913 if( AVI_ChunkReadCommon( p_input, p_chk ) )
915 msg_Warn( p_input, "cannot read one chunk" );
918 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
920 msg_Warn( p_input, "found null fourcc chunk (corrupted file?)" );
923 p_chk->common.p_father = p_father;
925 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
926 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
928 return AVI_Chunk_Function[i_index].
929 AVI_ChunkRead_function( p_input, p_chk, b_seekable );
931 else if( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
932 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' )
934 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
935 return AVI_ChunkRead_indx( p_input, p_chk, b_seekable );
937 msg_Warn( p_input, "unknown chunk (not loaded)" );
938 return AVI_NextChunk( p_input, p_chk );
941 void _AVI_ChunkFree( input_thread_t *p_input,
945 avi_chunk_t *p_child, *p_next;
952 /* Free all child chunk */
953 p_child = p_chk->common.p_first;
956 p_next = p_child->common.p_next;
957 AVI_ChunkFree( p_input, p_child );
962 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
963 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
966 msg_Dbg( p_input, "free chunk %4.4s",
967 (char*)&p_chk->common.i_chunk_fourcc );
969 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
973 msg_Warn( p_input, "unknown chunk (not unloaded)" );
975 p_chk->common.p_first = NULL;
976 p_chk->common.p_last = NULL;
981 int AVI_ChunkReadRoot( input_thread_t *p_input,
983 vlc_bool_t b_seekable )
985 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
988 p_list->i_chunk_pos = 0;
989 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
990 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
991 p_list->p_father = NULL;
992 p_list->p_next = NULL;
993 p_list->p_first = NULL;
994 p_list->p_last = NULL;
996 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
1000 p_chk = malloc( sizeof( avi_chunk_t ) );
1001 memset( p_chk, 0, sizeof( avi_chunk_t ) );
1002 if( !p_root->common.p_first )
1004 p_root->common.p_first = p_chk;
1008 p_root->common.p_last->common.p_next = p_chk;
1010 p_root->common.p_last = p_chk;
1012 if( AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
1013 ( AVI_TellAbsolute( p_input ) >=
1014 (off_t)p_chk->common.p_father->common.i_chunk_pos +
1015 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
1019 /* If we can't seek then stop when we 've found first RIFF-AVI */
1020 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
1021 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
1030 void AVI_ChunkFreeRoot( input_thread_t *p_input,
1031 avi_chunk_t *p_chk )
1033 AVI_ChunkFree( p_input, p_chk );
1037 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
1040 avi_chunk_t *p_child;
1048 p_child = p_chk->common.p_first;
1051 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1052 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1053 p_child->list.i_type == i_fourcc ) )
1057 p_child = p_child->common.p_next;
1062 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk,
1063 vlc_fourcc_t i_fourcc, int i_number )
1065 avi_chunk_t *p_child;
1070 p_child = p_chk->common.p_first;
1074 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1075 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1076 p_child->list.i_type == i_fourcc ) )
1086 p_child = p_child->common.p_next;
1091 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
1092 avi_chunk_t *p_chk, int i_level )
1096 avi_chunk_t *p_child;
1098 memset( str, ' ', sizeof( str ) );
1099 for( i = 1; i < i_level; i++ )
1103 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
1104 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1106 sprintf( str + i_level * 5,
1107 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
1108 i_level ? '+' : '*',
1109 (char*)&p_chk->common.i_chunk_fourcc,
1110 (char*)&p_chk->list.i_type,
1111 p_chk->common.i_chunk_size,
1112 p_chk->common.i_chunk_pos );
1116 sprintf( str + i_level * 5,
1117 "+ %4.4s size:"I64Fu" pos:"I64Fu,
1118 (char*)&p_chk->common.i_chunk_fourcc,
1119 p_chk->common.i_chunk_size,
1120 p_chk->common.i_chunk_pos );
1122 msg_Dbg( p_input, "%s", str );
1124 p_child = p_chk->common.p_first;
1127 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
1128 p_child = p_child->common.p_next;
1131 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
1132 avi_chunk_t *p_chk )
1134 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );