1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.21 2003/08/01 00:05:07 gbazin 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 ) );
139 input_DeletePacket( p_input->p_method_data, p_data );
149 /* return amount read if success, 0 if failed */
150 int AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
152 data_packet_t *p_data;
165 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
170 memcpy( p_buff, p_data->p_payload_start, i_count );
171 input_DeletePacket( p_input->p_method_data, p_data );
182 int AVI_SkipBytes( input_thread_t *p_input, int64_t i_count )
184 /* broken with new use of i_tell */
187 vlc_mutex_lock( &p_input->stream.stream_lock );
188 i_buff_size = p_input->p_last_data - p_input->p_current_data;
189 vlc_mutex_unlock( &p_input->stream.stream_lock );
191 if( i_count > 0 && i_count + 1 < i_buff_size )
195 input_Peek( p_input, &p_peek, i_count + 1 );
197 vlc_mutex_lock( &p_input->stream.stream_lock );
198 p_input->p_current_data += i_count; // skip them
199 vlc_mutex_unlock( &p_input->stream.stream_lock );
205 return AVI_SeekAbsolute( p_input,
206 AVI_TellAbsolute( p_input ) + i_count );
210 /*****************************************************************************
212 * AVI_TestFile: look at first bytes to see if it's a valid avi file
216 *****************************************************************************/
217 int AVI_TestFile( input_thread_t *p_input )
221 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
223 msg_Err( p_input, "cannot peek()" );
227 if( GetFOURCC( p_peek ) == AVIFOURCC_RIFF &&
228 GetFOURCC( p_peek + 8 ) == AVIFOURCC_AVI )
237 /****************************************************************************
239 * Basics functions to manipulates chunks
241 ****************************************************************************/
242 static int AVI_ChunkReadCommon( input_thread_t *p_input,
248 memset( p_chk, 0, sizeof( avi_chunk_t ) );
250 if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
255 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
256 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
257 p_chk->common.i_chunk_pos = AVI_TellAbsolute( p_input );
259 p_chk->common.p_father = NULL;
260 p_chk->common.p_next = NULL;
261 p_chk->common.p_first = NULL;
262 p_chk->common.p_next = NULL;
265 "Found Chunk fourcc:%8.8x (%4.4s) size:"I64Fd" pos:"I64Fd,
266 p_chk->common.i_chunk_fourcc,
267 (char*)&p_chk->common.i_chunk_fourcc,
268 p_chk->common.i_chunk_size,
269 p_chk->common.i_chunk_pos );
274 static int AVI_NextChunk( input_thread_t *p_input,
281 if( AVI_ChunkReadCommon( p_input, &chk ) )
288 if( p_chk->common.p_father )
290 if( p_chk->common.p_father->common.i_chunk_pos +
291 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
292 p_chk->common.i_chunk_pos +
293 __EVEN( p_chk->common.i_chunk_size ) + 8 )
298 return AVI_SeekAbsolute( p_input,
299 p_chk->common.i_chunk_pos +
300 __EVEN( p_chk->common.i_chunk_size ) + 8 );
303 int _AVI_ChunkGoto( input_thread_t *p_input,
310 return AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos );
313 /****************************************************************************
315 * Functions to read chunks
317 ****************************************************************************/
318 static int AVI_ChunkRead_list( input_thread_t *p_input,
319 avi_chunk_t *p_container,
320 vlc_bool_t b_seekable )
325 if( p_container->common.i_chunk_size < 8 )
328 msg_Warn( p_input, "empty list chunk" );
331 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
333 msg_Warn( p_input, "cannot peek while reading list chunk" );
336 p_container->list.i_type = GetFOURCC( p_peek + 8 );
338 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
339 p_container->list.i_type == AVIFOURCC_movi )
341 msg_Dbg( p_input, "Skipping movi chunk" );
344 return AVI_NextChunk( p_input, p_container );
348 return VLC_SUCCESS; // point at begining of LIST-movi
352 if( AVI_SkipBytes( p_input, 12 ) )
354 msg_Warn( p_input, "cannot enter chunk" );
359 "found LIST chunk: \'%4.4s\'",
360 (char*)&p_container->list.i_type );
362 msg_Dbg( p_input, "<list \'%4.4s\'>", (char*)&p_container->list.i_type );
365 p_chk = malloc( sizeof( avi_chunk_t ) );
366 memset( p_chk, 0, sizeof( avi_chunk_t ) );
367 if( !p_container->common.p_first )
369 p_container->common.p_first = p_chk;
373 p_container->common.p_last->common.p_next = p_chk;
375 p_container->common.p_last = p_chk;
377 if( AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
378 ( AVI_TellAbsolute( p_input ) >=
379 (off_t)p_chk->common.p_father->common.i_chunk_pos +
380 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
384 /* If we can't seek then stop when we 've found LIST-movi */
385 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
386 p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
392 msg_Dbg( p_input, "</list \'%4.4s\'>", (char*)&p_container->list.i_type );
397 #define AVI_READCHUNK_ENTER \
398 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
399 uint8_t *p_read, *p_buff; \
400 if( !( p_read = p_buff = malloc(i_read ) ) ) \
404 i_read = AVI_ReadData( p_input, p_read, i_read ); \
408 #define AVI_READCHUNK_EXIT( code ) \
412 msg_Warn( p_input, "not enough data" ); \
416 #define AVI_READ1BYTE( i_byte ) \
421 #define AVI_READ2BYTES( i_word ) \
422 i_word = GetWLE( p_read ); \
426 #define AVI_READ4BYTES( i_dword ) \
427 i_dword = GetDWLE( p_read ); \
431 #define AVI_READ8BYTES( i_dword ) \
432 i_dword = GetQWLE( p_read ); \
436 #define AVI_READFOURCC( i_dword ) \
437 i_dword = GetFOURCC( p_read ); \
441 static int AVI_ChunkRead_avih( input_thread_t *p_input,
443 vlc_bool_t b_seekable )
447 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
448 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
449 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
450 AVI_READ4BYTES( p_chk->avih.i_flags );
451 AVI_READ4BYTES( p_chk->avih.i_totalframes );
452 AVI_READ4BYTES( p_chk->avih.i_initialframes );
453 AVI_READ4BYTES( p_chk->avih.i_streams );
454 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
455 AVI_READ4BYTES( p_chk->avih.i_width );
456 AVI_READ4BYTES( p_chk->avih.i_height );
457 AVI_READ4BYTES( p_chk->avih.i_scale );
458 AVI_READ4BYTES( p_chk->avih.i_rate );
459 AVI_READ4BYTES( p_chk->avih.i_start );
460 AVI_READ4BYTES( p_chk->avih.i_length );
463 "avih: streams:%d flags:%s%s%s%s %dx%d",
464 p_chk->avih.i_streams,
465 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
466 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
467 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
468 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
469 p_chk->avih.i_width, p_chk->avih.i_height );
471 AVI_READCHUNK_EXIT( VLC_SUCCESS );
474 static int AVI_ChunkRead_strh( input_thread_t *p_input,
476 vlc_bool_t b_seekable )
480 AVI_READFOURCC( p_chk->strh.i_type );
481 AVI_READFOURCC( p_chk->strh.i_handler );
482 AVI_READ4BYTES( p_chk->strh.i_flags );
483 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
484 AVI_READ4BYTES( p_chk->strh.i_initialframes );
485 AVI_READ4BYTES( p_chk->strh.i_scale );
486 AVI_READ4BYTES( p_chk->strh.i_rate );
487 AVI_READ4BYTES( p_chk->strh.i_start );
488 AVI_READ4BYTES( p_chk->strh.i_length );
489 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
490 AVI_READ4BYTES( p_chk->strh.i_quality );
491 AVI_READ4BYTES( p_chk->strh.i_samplesize );
494 "strh: type:%4.4s handler:0x%8.8x samplesize:%d %.2ffps",
495 (char*)&p_chk->strh.i_type,
496 p_chk->strh.i_handler,
497 p_chk->strh.i_samplesize,
498 ( p_chk->strh.i_scale ?
499 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
502 AVI_READCHUNK_EXIT( VLC_SUCCESS );
505 static int AVI_ChunkRead_strf( input_thread_t *p_input,
507 vlc_bool_t b_seekable )
512 if( p_chk->common.p_father == NULL )
514 msg_Err( p_input, "malformed avi file" );
515 AVI_READCHUNK_EXIT( VLC_EGENERIC );
517 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
519 msg_Err( p_input, "malformed avi file" );
520 AVI_READCHUNK_EXIT( VLC_EGENERIC );
523 switch( p_strh->strh.i_type )
525 case( AVIFOURCC_auds ):
526 p_chk->strf.auds.i_cat = AUDIO_ES;
527 p_chk->strf.auds.p_wf = malloc( __MAX( p_chk->common.i_chunk_size, sizeof( WAVEFORMATEX ) ) );
528 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
529 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
530 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
531 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
532 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
533 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
534 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM
535 && p_chk->common.i_chunk_size > sizeof( WAVEFORMATEX ) )
537 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
538 /* prevent segfault */
539 if( p_chk->strf.auds.p_wf->cbSize >
540 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ) )
542 p_chk->strf.auds.p_wf->cbSize =
543 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX );
548 p_chk->strf.auds.p_wf->cbSize = 0;
550 if( p_chk->strf.auds.p_wf->cbSize > 0 )
552 memcpy( &p_chk->strf.auds.p_wf[1] ,
553 p_buff + 8 + sizeof( WAVEFORMATEX ), // 8=fourrc+size
554 p_chk->strf.auds.p_wf->cbSize );
558 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
559 p_chk->strf.auds.p_wf->wFormatTag,
560 p_chk->strf.auds.p_wf->nChannels,
561 p_chk->strf.auds.p_wf->nSamplesPerSec,
562 p_chk->strf.auds.p_wf->wBitsPerSample,
563 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
566 case( AVIFOURCC_vids ):
567 p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
568 p_chk->strf.vids.i_cat = VIDEO_ES;
569 p_chk->strf.vids.p_bih = malloc( p_chk->common.i_chunk_size );
570 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSize );
571 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biWidth );
572 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biHeight );
573 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biPlanes );
574 AVI_READ2BYTES( p_chk->strf.vids.p_bih->biBitCount );
575 AVI_READFOURCC( p_chk->strf.vids.p_bih->biCompression );
576 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biSizeImage );
577 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biXPelsPerMeter );
578 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biYPelsPerMeter );
579 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrUsed );
580 AVI_READ4BYTES( p_chk->strf.vids.p_bih->biClrImportant );
581 if( p_chk->strf.vids.p_bih->biSize >
582 p_chk->common.i_chunk_size )
584 p_chk->strf.vids.p_bih->biSize = p_chk->common.i_chunk_size;
586 if( p_chk->strf.vids.p_bih->biSize - sizeof(BITMAPINFOHEADER) > 0 )
588 memcpy( &p_chk->strf.vids.p_bih[1],
589 p_buff + 8 + sizeof(BITMAPINFOHEADER), // 8=fourrc+size
590 p_chk->strf.vids.p_bih->biSize -
591 sizeof(BITMAPINFOHEADER) );
595 "strf: video:%4.4s %dx%d planes:%d %dbpp",
596 (char*)&p_chk->strf.vids.p_bih->biCompression,
597 p_chk->strf.vids.p_bih->biWidth,
598 p_chk->strf.vids.p_bih->biHeight,
599 p_chk->strf.vids.p_bih->biPlanes,
600 p_chk->strf.vids.p_bih->biBitCount );
604 msg_Warn( p_input, "unknown stream type" );
605 p_chk->strf.common.i_cat = UNKNOWN_ES;
608 AVI_READCHUNK_EXIT( VLC_SUCCESS );
610 static void AVI_ChunkFree_strf( input_thread_t *p_input,
613 avi_chunk_strf_t *p_strf = (avi_chunk_strf_t*)p_chk;
614 switch( p_strf->common.i_cat )
617 FREE( p_strf->auds.p_wf );
620 FREE( p_strf->vids.p_bih );
627 static int AVI_ChunkRead_strd( input_thread_t *p_input,
629 vlc_bool_t b_seekable )
632 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
633 memcpy( p_chk->strd.p_data,
635 p_chk->common.i_chunk_size );
636 AVI_READCHUNK_EXIT( VLC_SUCCESS );
639 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
641 vlc_bool_t b_seekable )
643 unsigned int i_count, i_index;
647 i_count = __MIN( (int64_t)p_chk->common.i_chunk_size, i_read ) / 16;
649 p_chk->idx1.i_entry_count = i_count;
650 p_chk->idx1.i_entry_max = i_count;
653 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
655 for( i_index = 0; i_index < i_count ; i_index++ )
657 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
658 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
659 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
660 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
665 p_chk->idx1.entry = NULL;
668 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
670 AVI_READCHUNK_EXIT( VLC_SUCCESS );
673 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
676 p_chk->idx1.i_entry_count = 0;
677 p_chk->idx1.i_entry_max = 0;
678 FREE( p_chk->idx1.entry )
683 static int AVI_ChunkRead_indx( input_thread_t *p_input,
685 vlc_bool_t b_seekable )
687 unsigned int i_count, i;
689 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
693 AVI_READ2BYTES( p_indx->i_longsperentry );
694 AVI_READ1BYTE ( p_indx->i_indexsubtype );
695 AVI_READ1BYTE ( p_indx->i_indextype );
696 AVI_READ4BYTES( p_indx->i_entriesinuse );
698 AVI_READ4BYTES( p_indx->i_id );
699 p_indx->idx.std = NULL;
700 p_indx->idx.field = NULL;
701 p_indx->idx.super = NULL;
703 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == 0 )
705 AVI_READ8BYTES( p_indx->i_baseoffset );
706 AVI_READ4BYTES( i_dummy );
708 i_count = __MIN( p_indx->i_entriesinuse, i_read / 8 );
709 p_indx->i_entriesinuse = i_count;
710 p_indx->idx.std = calloc( sizeof( indx_std_entry_t ), i_count );
712 for( i = 0; i < i_count; i++ )
714 AVI_READ4BYTES( p_indx->idx.std[i].i_offset );
715 AVI_READ4BYTES( p_indx->idx.std[i].i_size );
718 else if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS && p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
720 AVI_READ8BYTES( p_indx->i_baseoffset );
721 AVI_READ4BYTES( i_dummy );
723 i_count = __MIN( p_indx->i_entriesinuse, i_read / 12 );
724 p_indx->i_entriesinuse = i_count;
725 p_indx->idx.field = calloc( sizeof( indx_field_entry_t ), i_count );
726 for( i = 0; i < i_count; i++ )
728 AVI_READ4BYTES( p_indx->idx.field[i].i_offset );
729 AVI_READ4BYTES( p_indx->idx.field[i].i_size );
730 AVI_READ4BYTES( p_indx->idx.field[i].i_offsetfield2 );
733 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
735 p_indx->i_baseoffset = 0;
736 AVI_READ4BYTES( i_dummy );
737 AVI_READ4BYTES( i_dummy );
738 AVI_READ4BYTES( i_dummy );
740 i_count = __MIN( p_indx->i_entriesinuse, i_read / 16 );
741 p_indx->i_entriesinuse = i_count;
742 p_indx->idx.super = calloc( sizeof( indx_super_entry_t ), i_count );
744 for( i = 0; i < i_count; i++ )
746 AVI_READ8BYTES( p_indx->idx.super[i].i_offset );
747 AVI_READ4BYTES( p_indx->idx.super[i].i_size );
748 AVI_READ4BYTES( p_indx->idx.super[i].i_duration );
753 msg_Warn( p_input, "unknow type/subtype index" );
757 msg_Dbg( p_input, "indx: type=%d subtype=%d entry=%d", p_indx->i_indextype, p_indx->i_indexsubtype, p_indx->i_entriesinuse );
759 AVI_READCHUNK_EXIT( VLC_SUCCESS );
761 static void AVI_ChunkFree_indx( input_thread_t *p_input,
764 avi_chunk_indx_t *p_indx = (avi_chunk_indx_t*)p_chk;
766 FREE( p_indx->idx.std );
767 FREE( p_indx->idx.field );
768 FREE( p_indx->idx.super );
775 vlc_fourcc_t i_fourcc;
779 { AVIFOURCC_IARL, "archive location" },
780 { AVIFOURCC_IART, "artist" },
781 { AVIFOURCC_ICMS, "commisioned" },
782 { AVIFOURCC_ICMT, "comments" },
783 { AVIFOURCC_ICOP, "copyright" },
784 { AVIFOURCC_ICRD, "creation date" },
785 { AVIFOURCC_ICRP, "cropped" },
786 { AVIFOURCC_IDIM, "dimensions" },
787 { AVIFOURCC_IDPI, "dots per inch" },
788 { AVIFOURCC_IENG, "enginner" },
789 { AVIFOURCC_IGNR, "genre" },
790 { AVIFOURCC_IKEY, "keywords" },
791 { AVIFOURCC_ILGT, "lightness" },
792 { AVIFOURCC_IMED, "medium" },
793 { AVIFOURCC_INAM, "name" },
794 { AVIFOURCC_IPLT, "palette setting" },
795 { AVIFOURCC_IPRD, "product" },
796 { AVIFOURCC_ISBJ, "subject" },
797 { AVIFOURCC_ISFT, "software" },
798 { AVIFOURCC_ISHP, "sharpness" },
799 { AVIFOURCC_ISRC, "source" },
800 { AVIFOURCC_ISRF, "source form" },
801 { AVIFOURCC_ITCH, "technician" },
802 { AVIFOURCC_ISMP, "time code" },
803 { AVIFOURCC_IDIT, "digitalization time" },
804 { AVIFOURCC_strn, "stream name" },
807 static int AVI_ChunkRead_strz( input_thread_t *p_input,
809 vlc_bool_t b_seekable )
812 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
815 for( i_index = 0;; i_index++)
817 if( !AVI_strz_type[i_index].i_fourcc ||
818 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
823 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
824 p_strz->p_str = malloc( i_read + 1);
826 if( p_strz->i_chunk_size )
828 memcpy( p_strz->p_str, p_read, i_read );
830 p_strz->p_str[i_read] = 0;
833 msg_Dbg( p_input, "%4.4s: %s : %s",
834 (char*)&p_strz->i_chunk_fourcc, p_strz->p_type, p_strz->p_str);
836 AVI_READCHUNK_EXIT( VLC_SUCCESS );
838 static void AVI_ChunkFree_strz( input_thread_t *p_input,
841 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
842 FREE( p_strz->p_type );
843 FREE( p_strz->p_str );
846 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
848 vlc_bool_t b_seekable )
850 return AVI_NextChunk( p_input, p_chk );
852 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
860 vlc_fourcc_t i_fourcc;
861 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
863 vlc_bool_t b_seekable );
864 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
865 avi_chunk_t *p_chk );
866 } AVI_Chunk_Function [] =
868 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
869 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
870 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
871 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
872 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
873 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
874 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
875 { AVIFOURCC_indx, AVI_ChunkRead_indx, AVI_ChunkFree_indx },
876 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
878 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
879 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
880 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
881 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
882 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
883 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
884 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
885 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
886 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
887 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
888 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
889 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
890 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
891 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
892 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
893 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
894 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
895 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
896 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
897 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
898 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
899 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
900 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
901 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
902 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
903 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
904 { AVIFOURCC_strn, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
908 static int AVI_ChunkFunctionFind( vlc_fourcc_t i_fourcc )
910 unsigned int i_index;
911 for( i_index = 0; ; i_index++ )
913 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
914 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
921 int _AVI_ChunkRead( input_thread_t *p_input,
923 avi_chunk_t *p_father,
924 vlc_bool_t b_seekable )
933 if( AVI_ChunkReadCommon( p_input, p_chk ) )
935 msg_Warn( p_input, "cannot read one chunk" );
938 if( p_chk->common.i_chunk_fourcc == VLC_FOURCC( 0, 0, 0, 0 ) )
940 msg_Warn( p_input, "found null fourcc chunk (corrupted file?)" );
943 p_chk->common.p_father = p_father;
945 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
946 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
948 return AVI_Chunk_Function[i_index].
949 AVI_ChunkRead_function( p_input, p_chk, b_seekable );
951 else if( ((char*)&p_chk->common.i_chunk_fourcc)[0] == 'i' &&
952 ((char*)&p_chk->common.i_chunk_fourcc)[1] == 'x' )
954 p_chk->common.i_chunk_fourcc = AVIFOURCC_indx;
955 return AVI_ChunkRead_indx( p_input, p_chk, b_seekable );
957 msg_Warn( p_input, "unknown chunk (not loaded)" );
958 return AVI_NextChunk( p_input, p_chk );
961 void _AVI_ChunkFree( input_thread_t *p_input,
965 avi_chunk_t *p_child, *p_next;
972 /* Free all child chunk */
973 p_child = p_chk->common.p_first;
976 p_next = p_child->common.p_next;
977 AVI_ChunkFree( p_input, p_child );
982 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
983 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
986 msg_Dbg( p_input, "free chunk %4.4s",
987 (char*)&p_chk->common.i_chunk_fourcc );
989 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
993 msg_Warn( p_input, "unknown chunk (not unloaded)" );
995 p_chk->common.p_first = NULL;
996 p_chk->common.p_last = NULL;
1001 int AVI_ChunkReadRoot( input_thread_t *p_input,
1002 avi_chunk_t *p_root,
1003 vlc_bool_t b_seekable )
1005 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
1008 p_list->i_chunk_pos = 0;
1009 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
1010 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
1011 p_list->p_father = NULL;
1012 p_list->p_next = NULL;
1013 p_list->p_first = NULL;
1014 p_list->p_last = NULL;
1016 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
1020 p_chk = malloc( sizeof( avi_chunk_t ) );
1021 memset( p_chk, 0, sizeof( avi_chunk_t ) );
1022 if( !p_root->common.p_first )
1024 p_root->common.p_first = p_chk;
1028 p_root->common.p_last->common.p_next = p_chk;
1030 p_root->common.p_last = p_chk;
1032 if( AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
1033 ( AVI_TellAbsolute( p_input ) >=
1034 (off_t)p_chk->common.p_father->common.i_chunk_pos +
1035 (off_t)__EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
1039 /* If we can't seek then stop when we 've found first RIFF-AVI */
1040 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
1041 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
1050 void AVI_ChunkFreeRoot( input_thread_t *p_input,
1051 avi_chunk_t *p_chk )
1053 AVI_ChunkFree( p_input, p_chk );
1057 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
1060 avi_chunk_t *p_child;
1068 p_child = p_chk->common.p_first;
1071 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1072 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1073 p_child->list.i_type == i_fourcc ) )
1077 p_child = p_child->common.p_next;
1082 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk,
1083 vlc_fourcc_t i_fourcc, int i_number )
1085 avi_chunk_t *p_child;
1090 p_child = p_chk->common.p_first;
1094 if( p_child->common.i_chunk_fourcc == i_fourcc ||
1095 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
1096 p_child->list.i_type == i_fourcc ) )
1106 p_child = p_child->common.p_next;
1111 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
1112 avi_chunk_t *p_chk, int i_level )
1116 avi_chunk_t *p_child;
1118 memset( str, ' ', sizeof( str ) );
1119 for( i = 1; i < i_level; i++ )
1123 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
1124 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
1126 sprintf( str + i_level * 5,
1127 "%c %4.4s-%4.4s size:"I64Fu" pos:"I64Fu,
1128 i_level ? '+' : '*',
1129 (char*)&p_chk->common.i_chunk_fourcc,
1130 (char*)&p_chk->list.i_type,
1131 p_chk->common.i_chunk_size,
1132 p_chk->common.i_chunk_pos );
1136 sprintf( str + i_level * 5,
1137 "+ %4.4s size:"I64Fu" pos:"I64Fu,
1138 (char*)&p_chk->common.i_chunk_fourcc,
1139 p_chk->common.i_chunk_size,
1140 p_chk->common.i_chunk_pos );
1142 msg_Dbg( p_input, "%s", str );
1144 p_child = p_chk->common.p_first;
1147 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
1148 p_child = p_child->common.p_next;
1151 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
1152 avi_chunk_t *p_chk )
1154 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );