1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.19 2003/04/27 13:55:51 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 /* Some functions to manipulate memory */
41 static uint16_t GetWLE( uint8_t *p_buff )
43 return (uint16_t)p_buff[0] | ( ((uint16_t)p_buff[1]) << 8 );
46 static uint32_t GetDWLE( uint8_t *p_buff )
48 return (uint32_t)p_buff[0] | ( ((uint32_t)p_buff[1]) << 8 ) |
49 ( ((uint32_t)p_buff[2]) << 16 ) | ( ((uint32_t)p_buff[3]) << 24 );
52 static uint64_t GetQWLE( uint8_t *p )
54 return (uint64_t)p[0] | ( ((uint64_t)p[1]) << 8 ) |
55 ( ((uint64_t)p[2]) << 16 ) | ( ((uint64_t)p[3]) << 24 ) |
56 ( ((uint64_t)p[4]) << 32 ) | ( ((uint64_t)p[5]) << 40 ) |
57 ( ((uint64_t)p[6]) << 48 ) | ( ((uint64_t)p[7]) << 56 );
60 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
62 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
64 /*****************************************************************************
65 * Some basic functions to manipulate stream more easily in vlc
67 * AVI_TellAbsolute get file position
69 * AVI_SeekAbsolute seek in the file
71 * AVI_ReadData read data from the file in a buffer
73 * AVI_SkipBytes skip bytes
75 *****************************************************************************/
76 off_t AVI_TellAbsolute( input_thread_t *p_input )
80 vlc_mutex_lock( &p_input->stream.stream_lock );
82 i_pos= p_input->stream.p_selected_area->i_tell;
84 vlc_mutex_unlock( &p_input->stream.stream_lock );
89 int AVI_SeekAbsolute( input_thread_t *p_input,
94 if( p_input->stream.p_selected_area->i_size > 0 &&
95 i_pos >= p_input->stream.p_selected_area->i_size )
100 i_filepos = AVI_TellAbsolute( p_input );
102 if( i_filepos == i_pos )
107 if( p_input->stream.b_seekable &&
108 ( p_input->stream.i_method == INPUT_METHOD_FILE ||
109 i_pos - i_filepos < 0 ||
110 i_pos - i_filepos > 1024 ) )
112 input_AccessReinit( p_input );
113 p_input->pf_seek( p_input, i_pos );
116 else if( i_pos - i_filepos > 0 )
118 data_packet_t *p_data;
119 int i_skip = i_pos - i_filepos;
121 msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
124 return VLC_EGENERIC; // failed
130 i_read = input_SplitBuffer( p_input, &p_data,
131 __MIN( 4096, i_skip ) );
138 input_DeletePacket( p_input->p_method_data, p_data );
139 if( i_read == 0 && i_skip > 0 )
152 /* return amount read if success, 0 if failed */
153 int AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
155 data_packet_t *p_data;
168 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
173 memcpy( p_buff, p_data->p_payload_start, i_count );
174 input_DeletePacket( p_input->p_method_data, p_data );
185 int AVI_SkipBytes( input_thread_t *p_input, int64_t i_count )
187 /* broken with new use of i_tell */
190 vlc_mutex_lock( &p_input->stream.stream_lock );
191 i_buff_size = p_input->p_last_data - p_input->p_current_data;
192 vlc_mutex_unlock( &p_input->stream.stream_lock );
194 if( i_count > 0 && i_count + 1 < i_buff_size )
198 input_Peek( p_input, &p_peek, i_count + 1 );
200 vlc_mutex_lock( &p_input->stream.stream_lock );
201 p_input->p_current_data += i_count; // skip them
202 vlc_mutex_unlock( &p_input->stream.stream_lock );
208 return AVI_SeekAbsolute( p_input,
209 AVI_TellAbsolute( p_input ) + i_count );
213 /*****************************************************************************
215 * AVI_TestFile: look at first bytes to see if it's a valid avi file
219 *****************************************************************************/
220 int AVI_TestFile( input_thread_t *p_input )
224 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
226 msg_Err( p_input, "cannot peek()" );
230 if( GetFOURCC( p_peek ) == AVIFOURCC_RIFF &&
231 GetFOURCC( p_peek + 8 ) == AVIFOURCC_AVI )
240 /****************************************************************************
242 * Basics functions to manipulates chunks
244 ****************************************************************************/
245 static int AVI_ChunkReadCommon( input_thread_t *p_input,
251 memset( p_chk, 0, sizeof( avi_chunk_t ) );
253 if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
258 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
259 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
260 p_chk->common.i_chunk_pos = AVI_TellAbsolute( p_input );
262 p_chk->common.p_father = NULL;
263 p_chk->common.p_next = NULL;
264 p_chk->common.p_first = NULL;
265 p_chk->common.p_next = NULL;
268 "Found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
269 p_chk->common.i_chunk_fourcc,
270 (char*)&p_chk->common.i_chunk_fourcc,
271 p_chk->common.i_chunk_size,
272 p_chk->common.i_chunk_pos );
277 static int AVI_NextChunk( input_thread_t *p_input,
284 if( AVI_ChunkReadCommon( p_input, &chk ) )
291 if( p_chk->common.p_father )
293 if( p_chk->common.p_father->common.i_chunk_pos +
294 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
295 p_chk->common.i_chunk_pos +
296 __EVEN( p_chk->common.i_chunk_size ) + 8 )
301 return AVI_SeekAbsolute( p_input,
302 p_chk->common.i_chunk_pos +
303 __EVEN( p_chk->common.i_chunk_size ) + 8 );
306 int _AVI_ChunkGoto( input_thread_t *p_input,
313 return AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos );
316 /****************************************************************************
318 * Functions to read chunks
320 ****************************************************************************/
321 static int AVI_ChunkRead_list( input_thread_t *p_input,
322 avi_chunk_t *p_container,
323 vlc_bool_t b_seekable )
328 if( p_container->common.i_chunk_size < 8 )
331 msg_Warn( p_input, "empty list chunk" );
334 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
336 msg_Warn( p_input, "cannot peek while reading list chunk" );
339 p_container->list.i_type = GetFOURCC( p_peek + 8 );
341 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
342 p_container->list.i_type == AVIFOURCC_movi )
344 msg_Dbg( p_input, "Skipping movi chunk" );
347 return AVI_NextChunk( p_input, p_container );
351 return VLC_SUCCESS; // point at begining of LIST-movi
355 if( AVI_SkipBytes( p_input, 12 ) )
357 msg_Warn( p_input, "cannot enter chunk" );
362 "found LIST chunk: \'%4.4s\'",
363 (char*)&p_container->list.i_type );
365 msg_Dbg( p_input, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
368 p_chk = malloc( sizeof( avi_chunk_t ) );
369 memset( p_chk, 0, sizeof( avi_chunk_t ) );
370 if( !p_container->common.p_first )
372 p_container->common.p_first = p_chk;
376 p_container->common.p_last->common.p_next = p_chk;
378 p_container->common.p_last = p_chk;
380 if( AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
381 ( AVI_TellAbsolute( p_input ) >=
382 (off_t)p_chk->common.p_father->common.i_chunk_pos +
383 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
387 /* If we can't seek then stop when we 've found LIST-movi */
388 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
389 p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
395 msg_Dbg( p_input, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
400 #define AVI_READCHUNK_ENTER \
401 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
402 uint8_t *p_read, *p_buff; \
403 if( !( p_read = p_buff = malloc(i_read ) ) ) \
407 i_read = AVI_ReadData( p_input, p_read, i_read ); \
411 #define AVI_READCHUNK_EXIT( code ) \
415 msg_Warn( p_input, "not enough data" ); \
419 #define AVI_READ1BYTE( i_byte ) \
424 #define AVI_READ2BYTES( i_word ) \
425 i_word = GetWLE( p_read ); \
429 #define AVI_READ4BYTES( i_dword ) \
430 i_dword = GetDWLE( p_read ); \
434 #define AVI_READ8BYTES( i_dword ) \
435 i_dword = GetQWLE( p_read ); \
439 #define AVI_READFOURCC( i_dword ) \
440 i_dword = GetFOURCC( p_read ); \
444 static int AVI_ChunkRead_avih( input_thread_t *p_input,
446 vlc_bool_t b_seekable )
450 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
451 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
452 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
453 AVI_READ4BYTES( p_chk->avih.i_flags );
454 AVI_READ4BYTES( p_chk->avih.i_totalframes );
455 AVI_READ4BYTES( p_chk->avih.i_initialframes );
456 AVI_READ4BYTES( p_chk->avih.i_streams );
457 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
458 AVI_READ4BYTES( p_chk->avih.i_width );
459 AVI_READ4BYTES( p_chk->avih.i_height );
460 AVI_READ4BYTES( p_chk->avih.i_scale );
461 AVI_READ4BYTES( p_chk->avih.i_rate );
462 AVI_READ4BYTES( p_chk->avih.i_start );
463 AVI_READ4BYTES( p_chk->avih.i_length );
466 "avih: streams:%d flags:%s%s%s%s %dx%d",
467 p_chk->avih.i_streams,
468 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
469 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
470 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
471 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
472 p_chk->avih.i_width, p_chk->avih.i_height );
474 AVI_READCHUNK_EXIT( VLC_SUCCESS );
477 static int AVI_ChunkRead_strh( input_thread_t *p_input,
479 vlc_bool_t b_seekable )
483 AVI_READFOURCC( p_chk->strh.i_type );
484 AVI_READFOURCC( p_chk->strh.i_handler );
485 AVI_READ4BYTES( p_chk->strh.i_flags );
486 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
487 AVI_READ4BYTES( p_chk->strh.i_initialframes );
488 AVI_READ4BYTES( p_chk->strh.i_scale );
489 AVI_READ4BYTES( p_chk->strh.i_rate );
490 AVI_READ4BYTES( p_chk->strh.i_start );
491 AVI_READ4BYTES( p_chk->strh.i_length );
492 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
493 AVI_READ4BYTES( p_chk->strh.i_quality );
494 AVI_READ4BYTES( p_chk->strh.i_samplesize );
497 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
498 (char*)&p_chk->strh.i_type,
499 p_chk->strh.i_handler,
500 p_chk->strh.i_samplesize,
501 ( p_chk->strh.i_scale ?
502 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
505 AVI_READCHUNK_EXIT( VLC_SUCCESS );
508 static int AVI_ChunkRead_strf( input_thread_t *p_input,
510 vlc_bool_t b_seekable )
515 if( p_chk->common.p_father == NULL )
517 msg_Err( p_input, "malformed avi file" );
518 AVI_READCHUNK_EXIT( VLC_EGENERIC );
520 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
522 msg_Err( p_input, "malformed avi file" );
523 AVI_READCHUNK_EXIT( VLC_EGENERIC );
526 switch( p_strh->strh.i_type )
528 case( AVIFOURCC_auds ):
529 p_chk->strf.auds.i_cat = AUDIO_ES;
530 p_chk->strf.auds.p_wf = malloc( p_chk->common.i_chunk_size );
531 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
532 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
533 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
534 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
535 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
536 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
537 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
538 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
540 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
541 /* prevent segfault */
542 if( p_chk->strf.auds.p_wf->cbSize >
543 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
545 p_chk->strf.auds.p_wf->cbSize =
546 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
551 p_chk->strf.auds.p_wf->cbSize = 0;
553 if( p_chk->strf.auds.p_wf->cbSize > 0 )
555 memcpy( &p_chk->strf.auds.p_wf[1] ,
556 p_buff + 8 + sizeof( WAVEFORMATEX ), // 8=fourrc+size
557 p_chk->strf.auds.p_wf->cbSize );
561 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
562 p_chk->strf.auds.p_wf->wFormatTag,
563 p_chk->strf.auds.p_wf->nChannels,
564 p_chk->strf.auds.p_wf->nSamplesPerSec,
565 p_chk->strf.auds.p_wf->wBitsPerSample,
566 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
569 case( AVIFOURCC_vids ):
570 p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
571 p_chk->strf.vids.i_cat = VIDEO_ES;
572 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
573 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
574 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
575 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
576 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
577 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
578 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
579 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
580 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
581 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
582 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
583 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
584 if( p_chk->strf.vids.p_bih->biSize >
585 p_chk->common.i_chunk_size )
587 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
589 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
591 memcpy( &p_chk->strf.vids.p_bih[1],
592 p_buff + 8 + sizeof(BITMAPINFOHEADER), // 8=fourrc+size
593 p_chk->strf.vids.p_bih->biSize -
594 sizeof(BITMAPINFOHEADER) );
598 "strf: video:%4.4s %dx%d planes:%d %dbpp",
599 (char*)&p_chk->strf.vids.p_bih->biCompression,
600 p_chk->strf.vids.p_bih->biWidth,
601 p_chk->strf.vids.p_bih->biHeight,
602 p_chk->strf.vids.p_bih->biPlanes,
603 p_chk->strf.vids.p_bih->biBitCount );
607 msg_Warn( p_input, "unknown stream type" );
608 p_chk->strf.common.i_cat = UNKNOWN_ES;
611 AVI_READCHUNK_EXIT( VLC_SUCCESS );
613 static void AVI_ChunkFree_strf( input_thread_t *p_input,
616 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
617 switch( p_strf->common.i_cat )
620 FREE( p_strf->auds.p_wf );
623 FREE( p_strf->vids.p_bih );
630 static int AVI_ChunkRead_strd( input_thread_t *p_input,
632 vlc_bool_t b_seekable )
635 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
636 memcpy( p_chk->strd.p_data,
638 p_chk->common.i_chunk_size );
639 AVI_READCHUNK_EXIT( VLC_SUCCESS );
642 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
644 vlc_bool_t b_seekable )
646 unsigned int i_count, i_index;
650 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
652 p_chk->idx1.i_entry_count = i_count;
653 p_chk->idx1.i_entry_max = i_count;
656 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
658 for( i_index = 0; i_index < i_count ; i_index++ )
660 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
661 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
662 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
663 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
668 p_chk->idx1.entry = NULL;
671 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
673 AVI_READCHUNK_EXIT( VLC_SUCCESS );
676 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
679 p_chk->idx1.i_entry_count = 0;
680 p_chk->idx1.i_entry_max = 0;
681 FREE( p_chk->idx1.entry )
686 static int AVI_ChunkRead_indx( input_thread_t *p_input,
688 vlc_bool_t b_seekable )
690 unsigned int i_count, i;
692 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
696 AVI_READ2BYTES( p_indx->i_longsperentry );
697 AVI_READ1BYTE ( p_indx->i_indexsubtype );
698 AVI_READ1BYTE ( p_indx->i_indextype );
699 AVI_READ4BYTES( p_indx->i_entriesinuse );
701 AVI_READ4BYTES( p_indx->i_id );
702 p_indx->idx.std = NULL;
703 p_indx->idx.field = NULL;
704 p_indx->idx.super = NULL;
706 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
708 AVI_READ8BYTES( p_indx->i_baseoffset );
709 AVI_READ4BYTES( i_dummy );
711 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
712 p_indx->i_entriesinuse = i_count;
713 p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
715 for( i = 0; i < i_count; i++ )
717 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
718 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
721 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
723 AVI_READ8BYTES( p_indx->i_baseoffset );
724 AVI_READ4BYTES( i_dummy );
726 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
727 p_indx->i_entriesinuse = i_count;
728 p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
729 for( i = 0; i < i_count; i++ )
731 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
732 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
733 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
736 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
738 p_indx->i_baseoffset = 0;
739 AVI_READ4BYTES( i_dummy );
740 AVI_READ4BYTES( i_dummy );
741 AVI_READ4BYTES( i_dummy );
743 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
744 p_indx->i_entriesinuse = i_count;
745 p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
747 for( i = 0; i < i_count; i++ )
749 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
750 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
751 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
756 msg_Warn( p_input, "unknow type/subtype index" );
760 msg_Dbg( p_input, "indx: type=%d subtype=%d entry=%d", p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
762 AVI_READCHUNK_EXIT( VLC_SUCCESS );
764 static void AVI_ChunkFree_indx( input_thread_t *p_input,
767 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
769 FREE( p_indx->idx.std );
770 FREE( p_indx->idx.field );
771 FREE( p_indx->idx.super );
778 vlc_fourcc_t i_fourcc;
782 { AVIFOURCC_IARL, "archive location" },
783 { AVIFOURCC_IART, "artist" },
784 { AVIFOURCC_ICMS, "commisioned" },
785 { AVIFOURCC_ICMT, "comments" },
786 { AVIFOURCC_ICOP, "copyright" },
787 { AVIFOURCC_ICRD, "creation date" },
788 { AVIFOURCC_ICRP, "cropped" },
789 { AVIFOURCC_IDIM, "dimensions" },
790 { AVIFOURCC_IDPI, "dots per inch" },
791 { AVIFOURCC_IENG, "enginner" },
792 { AVIFOURCC_IGNR, "genre" },
793 { AVIFOURCC_IKEY, "keywords" },
794 { AVIFOURCC_ILGT, "lightness" },
795 { AVIFOURCC_IMED, "medium" },
796 { AVIFOURCC_INAM, "name" },
797 { AVIFOURCC_IPLT, "palette setting" },
798 { AVIFOURCC_IPRD, "product" },
799 { AVIFOURCC_ISBJ, "subject" },
800 { AVIFOURCC_ISFT, "software" },
801 { AVIFOURCC_ISHP, "sharpness" },
802 { AVIFOURCC_ISRC, "source" },
803 { AVIFOURCC_ISRF, "source form" },
804 { AVIFOURCC_ITCH, "technician" },
805 { AVIFOURCC_ISMP, "time code" },
806 { AVIFOURCC_IDIT, "digitalization time" },
807 { AVIFOURCC_strn, "stream name" },
810 static int AVI_ChunkRead_strz( input_thread_t *p_input,
812 vlc_bool_t b_seekable )
815 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
818 for( i_index = 0;; i_index++)
820 if( !AVI_strz_type[i_index].i_fourcc ||
821 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
826 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
827 p_strz->p_str = malloc( i_read + 1);
829 if( p_strz->i_chunk_size )
831 memcpy( p_strz->p_str, p_read, i_read );
833 p_strz->p_str[i_read] = 0;
836 msg_Dbg( p_input, "%4.4s: %s : %s",
837 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
839 AVI_READCHUNK_EXIT( VLC_SUCCESS );
841 static void AVI_ChunkFree_strz( input_thread_t *p_input,
844 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
845 FREE( p_strz->p_type );
846 FREE( p_strz->p_str );
849 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
851 vlc_bool_t b_seekable )
853 return AVI_NextChunk( p_input, p_chk );
855 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
863 vlc_fourcc_t i_fourcc;
864 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
866 vlc_bool_t b_seekable );
867 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
868 avi_chunk_t *p_chk );
869 } AVI_Chunk_Function [] =
871 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
872 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
873 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
874 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
875 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
876 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
877 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
878 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
879 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
881 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
882 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
883 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
884 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
885 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
886 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
887 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
888 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
889 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
890 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
891 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
892 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
893 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
894 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
895 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
896 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
897 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
898 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
899 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
900 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
901 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
902 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
903 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
904 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
905 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
906 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
907 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
911 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
913 unsigned int i_index;
914 for( i_index = 0; ; i_index++ )
916 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
917 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
924 int _AVI_ChunkRead( input_thread_t *p_input,
926 avi_chunk_t *p_father,
927 vlc_bool_t b_seekable )
936 if( AVI_ChunkReadCommon( p_input, p_chk ) )
938 msg_Warn( p_input, "cannot read one chunk" );
941 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
943 msg_Warn( p_input, "found null fourcc chunk (corrupted file?)" );
946 p_chk->common.p_father = p_father;
948 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
949 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
951 return AVI_Chunk_Function[i_index].
952 AVI_ChunkRead_function( p_input, p_chk, b_seekable );
954 else if( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
955 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' )
957 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
958 return AVI_ChunkRead_indx( p_input, p_chk, b_seekable );
960 msg_Warn( p_input, "unknown chunk (not loaded)" );
961 return AVI_NextChunk( p_input, p_chk );
964 void _AVI_ChunkFree( input_thread_t *p_input,
968 avi_chunk_t *p_child, *p_next;
975 /* Free all child chunk */
976 p_child = p_chk->common.p_first;
979 p_next = p_child->common.p_next;
980 AVI_ChunkFree( p_input, p_child );
985 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
986 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
989 msg_Dbg( p_input, "free chunk %4.4s",
990 (char*)&p_chk->common.i_chunk_fourcc );
992 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
996 msg_Warn( p_input, "unknown chunk (not unloaded)" );
998 p_chk->common.p_first = NULL;
999 p_chk->common.p_last = NULL;
1004 int AVI_ChunkReadRoot( input_thread_t *p_input,
1005 avi_chunk_t *p_root,
1006 vlc_bool_t b_seekable )
1008 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
1011 p_list->i_chunk_pos = 0;
1012 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
1013 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
1014 p_list->p_father = NULL;
1015 p_list->p_next = NULL;
1016 p_list->p_first = NULL;
1017 p_list->p_last = NULL;
1019 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
1023 p_chk = malloc( sizeof( avi_chunk_t ) );
1024 memset( p_chk, 0, sizeof( avi_chunk_t ) );
1025 if( !p_root->common.p_first )
1027 p_root->common.p_first = p_chk;
1031 p_root->common.p_last->common.p_next = p_chk;
1033 p_root->common.p_last = p_chk;
1035 if( AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
1036 ( AVI_TellAbsolute( p_input ) >=
1037 (off_t)p_chk->common.p_father->common.i_chunk_pos +
1038 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
1042 /* If we can't seek then stop when we 've found first RIFF-AVI */
1043 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
1044 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
1053 void AVI_ChunkFreeRoot( input_thread_t *p_input,
1054 avi_chunk_t *p_chk )
1056 AVI_ChunkFree( p_input, p_chk );
1060 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
1063 avi_chunk_t *p_child;
1071 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 ) )
1080 p_child = p_child->common.p_next;
1085 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk,
1086 vlc_fourcc_t i_fourcc, int i_number )
1088 avi_chunk_t *p_child;
1093 p_child = p_chk->common.p_first;
1097 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1098 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1099 p_child->list.i_type == i_fourcc ) )
1109 p_child = p_child->common.p_next;
1114 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
1115 avi_chunk_t *p_chk, int i_level )
1119 avi_chunk_t *p_child;
1121 memset( str, ' ', sizeof( str ) );
1122 for( i = 1; i < i_level; i++ )
1126 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
1127 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1129 sprintf( str + i_level * 5,
1130 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
1131 i_level ? '+' : '*',
1132 (char*)&p_chk->common.i_chunk_fourcc,
1133 (char*)&p_chk->list.i_type,
1134 p_chk->common.i_chunk_size,
1135 p_chk->common.i_chunk_pos );
1139 sprintf( str + i_level * 5,
1140 "+ %4.4s size:"I64Fu" pos:"I64Fu,
1141 (char*)&p_chk->common.i_chunk_fourcc,
1142 p_chk->common.i_chunk_size,
1143 p_chk->common.i_chunk_pos );
1145 msg_Dbg( p_input, "%s", str );
1147 p_child = p_chk->common.p_first;
1150 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
1151 p_child = p_child->common.p_next;
1154 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
1155 avi_chunk_t *p_chk )
1157 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );