1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: libavi.c,v 1.8 2002/12/04 15:47:31 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;
79 // - ( p_input->p_last_data - p_input->p_current_data );
81 vlc_mutex_unlock( &p_input->stream.stream_lock );
86 int AVI_SeekAbsolute( input_thread_t *p_input,
91 if( i_pos >= p_input->stream.p_selected_area->i_size )
96 i_filepos = AVI_TellAbsolute( p_input );
98 if( i_filepos == i_pos )
103 if( p_input->stream.b_seekable &&
104 p_input->stream.i_method == INPUT_METHOD_FILE )
106 p_input->pf_seek( p_input, i_pos );
107 input_AccessReinit( p_input );
112 data_packet_t *p_data;
113 int i_skip = i_pos - i_filepos;
115 msg_Warn( p_input, "will skip %d bytes, slow", i_skip );
118 return VLC_EGENERIC; // failed
124 i_read = input_SplitBuffer( p_input, &p_data,
125 __MIN( 4096, i_skip ) );
132 input_DeletePacket( p_input->p_method_data, p_data );
133 if( i_read == 0 && i_skip > 0 )
141 i_peek = input_Peek( p_input, &p_peek, i_skip+1 );
144 vlc_mutex_lock( &p_input->stream.stream_lock );
145 p_input->p_current_data += i_peek; // skip them
146 vlc_mutex_unlock( &p_input->stream.stream_lock );
157 /* return amount read if success, 0 if failed */
158 int AVI_ReadData( input_thread_t *p_input, uint8_t *p_buff, int i_size )
160 data_packet_t *p_data;
173 i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 1024 ) );
178 memcpy( p_buff, p_data->p_payload_start, i_count );
179 input_DeletePacket( p_input->p_method_data, p_data );
190 int AVI_SkipBytes( input_thread_t *p_input, int i_count )
192 /* broken with new use of i_tell */
195 vlc_mutex_lock( &p_input->stream.stream_lock );
196 i_buff_size = p_input->p_last_data - p_input->p_current_data;
197 vlc_mutex_unlock( &p_input->stream.stream_lock );
199 if( i_count > 0 && i_count + 1 < i_buff_size )
203 input_Peek( p_input, &p_peek, i_count + 1 );
205 vlc_mutex_lock( &p_input->stream.stream_lock );
206 p_input->p_current_data += i_count; // skip them
207 vlc_mutex_unlock( &p_input->stream.stream_lock );
213 return AVI_SeekAbsolute( p_input,
214 AVI_TellAbsolute( p_input ) + i_count );
218 /*****************************************************************************
220 * AVI_TestFile: look at first bytes to see if it's a valid avi file
224 *****************************************************************************/
225 int AVI_TestFile( input_thread_t *p_input )
229 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
231 msg_Err( p_input, "cannot peek()" );
235 if( GetFOURCC( p_peek ) == AVIFOURCC_RIFF &&
236 GetFOURCC( p_peek + 8 ) == AVIFOURCC_AVI )
245 /****************************************************************************
247 * Basics functions to manipulates chunks
249 ****************************************************************************/
250 static int AVI_ChunkReadCommon( input_thread_t *p_input,
256 memset( p_chk, 0, sizeof( avi_chunk_t ) );
258 if( ( i_peek = input_Peek( p_input, &p_peek, 8 ) ) < 8 )
263 p_chk->common.i_chunk_fourcc = GetFOURCC( p_peek );
264 p_chk->common.i_chunk_size = GetDWLE( p_peek + 4 );
265 p_chk->common.i_chunk_pos = AVI_TellAbsolute( p_input );
267 p_chk->common.p_father = NULL;
268 p_chk->common.p_next = NULL;
269 p_chk->common.p_first = NULL;
270 p_chk->common.p_next = NULL;
273 "Found Chunk fourcc:%c%c%c%c size:"I64Fd" pos:"I64Fd,
274 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
275 p_chk->common.i_chunk_size,
276 p_chk->common.i_chunk_pos );
281 static int AVI_NextChunk( input_thread_t *p_input,
288 if( AVI_ChunkReadCommon( p_input, &chk ) )
295 if( p_chk->common.p_father )
297 if( p_chk->common.p_father->common.i_chunk_pos +
298 __EVEN( p_chk->common.p_father->common.i_chunk_size ) + 8 <
299 p_chk->common.i_chunk_pos +
300 __EVEN( p_chk->common.i_chunk_size ) + 8 )
305 return AVI_SeekAbsolute( p_input,
306 p_chk->common.i_chunk_pos +
307 __EVEN( p_chk->common.i_chunk_size ) + 8 );
310 int _AVI_ChunkGoto( input_thread_t *p_input,
317 return AVI_SeekAbsolute( p_input, p_chk->common.i_chunk_pos );
320 /****************************************************************************
322 * Functions to read chunks
324 ****************************************************************************/
325 static int AVI_ChunkRead_list( input_thread_t *p_input,
326 avi_chunk_t *p_container,
327 vlc_bool_t b_seekable )
332 if( p_container->common.i_chunk_size < 8 )
335 msg_Warn( p_input, "empty list chunk" );
338 if( input_Peek( p_input, &p_peek, 12 ) < 12 )
340 msg_Warn( p_input, "cannot peek while reading list chunk" );
343 p_container->list.i_type = GetFOURCC( p_peek + 8 );
345 if( p_container->common.i_chunk_fourcc == AVIFOURCC_LIST &&
346 p_container->list.i_type == AVIFOURCC_movi )
348 msg_Dbg( p_input, "Skipping movi chunk" );
351 return AVI_NextChunk( p_input, p_container );
355 return VLC_SUCCESS; // point at begining of LIST-movi
359 AVI_SkipBytes( p_input, 12 );
362 "found LIST chunk: \'%c%c%c%c\'",
363 AVIFOURCC_PRINT( p_container->list.i_type ) );
367 p_chk = malloc( sizeof( avi_chunk_t ) );
368 memset( p_chk, 0, sizeof( avi_chunk_t ) );
369 if( !p_container->common.p_first )
371 p_container->common.p_first = p_chk;
375 p_container->common.p_last->common.p_next = p_chk;
377 p_container->common.p_last = p_chk;
379 if( AVI_ChunkRead( p_input, p_chk, p_container, b_seekable ) ||
380 ( AVI_TellAbsolute( p_input ) >=
381 p_chk->common.p_father->common.i_chunk_pos +
382 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
386 /* If we can't seek then stop when we 've found LIST-movi */
387 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST &&
388 p_chk->list.i_type == AVIFOURCC_movi && !b_seekable )
398 #define AVI_READCHUNK_ENTER \
399 int64_t i_read = __EVEN(p_chk->common.i_chunk_size ) + 8; \
400 uint8_t *p_read, *p_buff; \
401 if( !( p_read = p_buff = malloc(i_read ) ) ) \
405 i_read = AVI_ReadData( p_input, p_read, i_read ); \
409 #define AVI_READCHUNK_EXIT( code ) \
413 msg_Warn( p_input, "not enough data" ); \
416 #define AVI_READ2BYTES( i_word ) \
417 i_word = GetWLE( p_read ); \
421 #define AVI_READ4BYTES( i_dword ) \
422 i_dword = GetDWLE( p_read ); \
426 #define AVI_READFOURCC( i_dword ) \
427 i_dword = GetFOURCC( p_read ); \
431 static int AVI_ChunkRead_avih( input_thread_t *p_input,
433 vlc_bool_t b_seekable )
437 AVI_READ4BYTES( p_chk->avih.i_microsecperframe);
438 AVI_READ4BYTES( p_chk->avih.i_maxbytespersec );
439 AVI_READ4BYTES( p_chk->avih.i_reserved1 );
440 AVI_READ4BYTES( p_chk->avih.i_flags );
441 AVI_READ4BYTES( p_chk->avih.i_totalframes );
442 AVI_READ4BYTES( p_chk->avih.i_initialframes );
443 AVI_READ4BYTES( p_chk->avih.i_streams );
444 AVI_READ4BYTES( p_chk->avih.i_suggestedbuffersize );
445 AVI_READ4BYTES( p_chk->avih.i_width );
446 AVI_READ4BYTES( p_chk->avih.i_height );
447 AVI_READ4BYTES( p_chk->avih.i_scale );
448 AVI_READ4BYTES( p_chk->avih.i_rate );
449 AVI_READ4BYTES( p_chk->avih.i_start );
450 AVI_READ4BYTES( p_chk->avih.i_length );
453 "avih: streams:%d flags:%s%s%s%s %dx%d",
454 p_chk->avih.i_streams,
455 p_chk->avih.i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
456 p_chk->avih.i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
457 p_chk->avih.i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
458 p_chk->avih.i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"",
459 p_chk->avih.i_width, p_chk->avih.i_height );
461 AVI_READCHUNK_EXIT( VLC_SUCCESS );
464 static int AVI_ChunkRead_strh( input_thread_t *p_input,
466 vlc_bool_t b_seekable )
470 AVI_READFOURCC( p_chk->strh.i_type );
471 AVI_READFOURCC( p_chk->strh.i_handler );
472 AVI_READ4BYTES( p_chk->strh.i_flags );
473 AVI_READ4BYTES( p_chk->strh.i_reserved1 );
474 AVI_READ4BYTES( p_chk->strh.i_initialframes );
475 AVI_READ4BYTES( p_chk->strh.i_scale );
476 AVI_READ4BYTES( p_chk->strh.i_rate );
477 AVI_READ4BYTES( p_chk->strh.i_start );
478 AVI_READ4BYTES( p_chk->strh.i_length );
479 AVI_READ4BYTES( p_chk->strh.i_suggestedbuffersize );
480 AVI_READ4BYTES( p_chk->strh.i_quality );
481 AVI_READ4BYTES( p_chk->strh.i_samplesize );
484 "strh: type:%c%c%c%c handler:0x%8.8x samplesize:%d %.2ffps",
485 AVIFOURCC_PRINT( p_chk->strh.i_type ),
486 p_chk->strh.i_handler,
487 p_chk->strh.i_samplesize,
488 ( p_chk->strh.i_scale ?
489 (float)p_chk->strh.i_rate / (float)p_chk->strh.i_scale : -1) );
492 AVI_READCHUNK_EXIT( VLC_SUCCESS );
495 static int AVI_ChunkRead_strf( input_thread_t *p_input,
497 vlc_bool_t b_seekable )
502 if( p_chk->common.p_father == NULL )
504 msg_Err( p_input, "malformed avi file" );
505 AVI_READCHUNK_EXIT( VLC_EGENERIC );
507 if( !( p_strh = AVI_ChunkFind( p_chk->common.p_father, AVIFOURCC_strh, 0 ) ) )
509 msg_Err( p_input, "malformed avi file" );
510 AVI_READCHUNK_EXIT( VLC_EGENERIC );
513 switch( p_strh->strh.i_type )
515 case( AVIFOURCC_auds ):
516 p_chk->strf.auds.p_wf = malloc( p_chk->common.i_chunk_size );
517 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wFormatTag );
518 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nChannels );
519 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nSamplesPerSec );
520 AVI_READ4BYTES( p_chk->strf.auds.p_wf->nAvgBytesPerSec );
521 AVI_READ2BYTES( p_chk->strf.auds.p_wf->nBlockAlign );
522 AVI_READ2BYTES( p_chk->strf.auds.p_wf->wBitsPerSample );
523 if( p_chk->strf.auds.p_wf->wFormatTag != WAVE_FORMAT_PCM )
525 AVI_READ2BYTES( p_chk->strf.auds.p_wf->cbSize );
529 p_chk->strf.auds.p_wf->cbSize = 0;
531 if( p_chk->strf.auds.p_wf->cbSize > 0 )
533 memcpy( &p_chk->strf.auds.p_wf[1] ,
534 p_buff + sizeof( WAVEFORMATEX ),
535 p_chk->common.i_chunk_size - sizeof( WAVEFORMATEX ));
539 "strf: audio:0x%4.4x channels:%d %dHz %dbits/sample %dkb/s",
540 p_chk->strf.auds.p_wf->wFormatTag,
541 p_chk->strf.auds.p_wf->nChannels,
542 p_chk->strf.auds.p_wf->nSamplesPerSec,
543 p_chk->strf.auds.p_wf->wBitsPerSample,
544 p_chk->strf.auds.p_wf->nAvgBytesPerSec * 8 / 1024 );
547 case( AVIFOURCC_vids ):
548 p_strh->strh.i_samplesize = 0; // XXX for ffmpeg avi file
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 memcpy( &p_chk->strf.vids.p_bih[1],
562 p_buff + sizeof(BITMAPINFOHEADER),
563 p_chk->common.i_chunk_size - sizeof(BITMAPINFOHEADER) );
566 "strf: video:%c%c%c%c %dx%d planes:%d %dbpp",
567 AVIFOURCC_PRINT( p_chk->strf.vids.p_bih->biCompression ),
568 p_chk->strf.vids.p_bih->biWidth,
569 p_chk->strf.vids.p_bih->biHeight,
570 p_chk->strf.vids.p_bih->biPlanes,
571 p_chk->strf.vids.p_bih->biBitCount );
575 msg_Warn( p_input, "unknown stream type" );
578 AVI_READCHUNK_EXIT( VLC_SUCCESS );
580 static void AVI_ChunkFree_strf( input_thread_t *p_input,
586 static int AVI_ChunkRead_strd( input_thread_t *p_input,
588 vlc_bool_t b_seekable )
591 p_chk->strd.p_data = malloc( p_chk->common.i_chunk_size );
592 memcpy( p_chk->strd.p_data,
594 p_chk->common.i_chunk_size );
595 AVI_READCHUNK_EXIT( VLC_SUCCESS );
598 static int AVI_ChunkRead_idx1( input_thread_t *p_input,
600 vlc_bool_t b_seekable )
602 int i_count, i_index;
606 i_count = __MIN( p_chk->common.i_chunk_size, i_read ) / 16;
608 p_chk->idx1.i_entry_count = i_count;
609 p_chk->idx1.i_entry_max = i_count;
612 p_chk->idx1.entry = calloc( i_count, sizeof( idx1_entry_t ) );
614 for( i_index = 0; i_index < i_count ; i_index++ )
616 AVI_READFOURCC( p_chk->idx1.entry[i_index].i_fourcc );
617 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_flags );
618 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_pos );
619 AVI_READ4BYTES( p_chk->idx1.entry[i_index].i_length );
624 p_chk->idx1.entry = NULL;
627 msg_Dbg( p_input, "idx1: index entry:%d", i_count );
629 AVI_READCHUNK_EXIT( VLC_SUCCESS );
632 static void AVI_ChunkFree_idx1( input_thread_t *p_input,
635 p_chk->idx1.i_entry_count = 0;
636 p_chk->idx1.i_entry_max = 0;
637 FREE( p_chk->idx1.entry )
642 vlc_fourcc_t i_fourcc;
646 { AVIFOURCC_IARL, "archive location" },
647 { AVIFOURCC_IART, "artist" },
648 { AVIFOURCC_ICMS, "commisioned" },
649 { AVIFOURCC_ICMT, "comments" },
650 { AVIFOURCC_ICOP, "copyright" },
651 { AVIFOURCC_ICRD, "creation date" },
652 { AVIFOURCC_ICRP, "cropped" },
653 { AVIFOURCC_IDIM, "dimensions" },
654 { AVIFOURCC_IDPI, "dots per inch" },
655 { AVIFOURCC_IENG, "enginner" },
656 { AVIFOURCC_IGNR, "genre" },
657 { AVIFOURCC_IKEY, "keywords" },
658 { AVIFOURCC_ILGT, "lightness" },
659 { AVIFOURCC_IMED, "medium" },
660 { AVIFOURCC_INAM, "name" },
661 { AVIFOURCC_IPLT, "palette setting" },
662 { AVIFOURCC_IPRD, "product" },
663 { AVIFOURCC_ISBJ, "subject" },
664 { AVIFOURCC_ISFT, "software" },
665 { AVIFOURCC_ISHP, "sharpness" },
666 { AVIFOURCC_ISRC, "source" },
667 { AVIFOURCC_ISRF, "source form" },
668 { AVIFOURCC_ITCH, "technician" },
669 { AVIFOURCC_ISMP, "time code" },
670 { AVIFOURCC_IDIT, "digitalization time" },
673 static int AVI_ChunkRead_strz( input_thread_t *p_input,
675 vlc_bool_t b_seekable )
678 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
681 for( i_index = 0;; i_index++)
683 if( !AVI_strz_type[i_index].i_fourcc ||
684 AVI_strz_type[i_index].i_fourcc == p_strz->i_chunk_fourcc )
689 p_strz->p_type = strdup( AVI_strz_type[i_index].psz_type );
690 p_strz->p_str = malloc( i_read + 1);
692 if( p_strz->i_chunk_size )
694 memcpy( p_strz->p_str, p_read, i_read );
696 p_strz->p_str[i_read] = 0;
699 msg_Dbg( p_input, "%c%c%c%c: %s : %s",
700 AVIFOURCC_PRINT( p_strz->i_chunk_fourcc), p_strz->p_type, p_strz->p_str);
702 AVI_READCHUNK_EXIT( VLC_SUCCESS );
704 static void AVI_ChunkFree_strz( input_thread_t *p_input,
707 avi_chunk_STRING_t *p_strz = (avi_chunk_STRING_t*)p_chk;
708 FREE( p_strz->p_type );
709 FREE( p_strz->p_str );
712 static int AVI_ChunkRead_nothing( input_thread_t *p_input,
714 vlc_bool_t b_seekable )
716 return AVI_NextChunk( p_input, p_chk );
718 static void AVI_ChunkFree_nothing( input_thread_t *p_input,
726 vlc_fourcc_t i_fourcc;
727 int (*AVI_ChunkRead_function)( input_thread_t *p_input,
729 vlc_bool_t b_seekable );
730 void (*AVI_ChunkFree_function)( input_thread_t *p_input,
731 avi_chunk_t *p_chk );
732 } AVI_Chunk_Function [] =
734 { AVIFOURCC_RIFF, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
735 { AVIFOURCC_LIST, AVI_ChunkRead_list, AVI_ChunkFree_nothing },
736 { AVIFOURCC_avih, AVI_ChunkRead_avih, AVI_ChunkFree_nothing },
737 { AVIFOURCC_strh, AVI_ChunkRead_strh, AVI_ChunkFree_nothing },
738 { AVIFOURCC_strf, AVI_ChunkRead_strf, AVI_ChunkFree_strf },
739 { AVIFOURCC_strd, AVI_ChunkRead_strd, AVI_ChunkFree_nothing },
740 { AVIFOURCC_idx1, AVI_ChunkRead_idx1, AVI_ChunkFree_idx1 },
741 { AVIFOURCC_JUNK, AVI_ChunkRead_nothing, AVI_ChunkFree_nothing },
743 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
744 { AVIFOURCC_IARL, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
745 { AVIFOURCC_IART, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
746 { AVIFOURCC_ICMS, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
747 { AVIFOURCC_ICMT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
748 { AVIFOURCC_ICOP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
749 { AVIFOURCC_ICRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
750 { AVIFOURCC_ICRP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
751 { AVIFOURCC_IDIM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
752 { AVIFOURCC_IDPI, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
753 { AVIFOURCC_IENG, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
754 { AVIFOURCC_IGNR, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
755 { AVIFOURCC_IKEY, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
756 { AVIFOURCC_ILGT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
757 { AVIFOURCC_IMED, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
758 { AVIFOURCC_INAM, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
759 { AVIFOURCC_IPLT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
760 { AVIFOURCC_IPRD, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
761 { AVIFOURCC_ISBJ, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
762 { AVIFOURCC_ISFT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
763 { AVIFOURCC_ISHP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
764 { AVIFOURCC_ISRC, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
765 { AVIFOURCC_ISRF, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
766 { AVIFOURCC_ITCH, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
767 { AVIFOURCC_ISMP, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
768 { AVIFOURCC_IDIT, AVI_ChunkRead_strz, AVI_ChunkFree_strz },
772 static int AVI_ChunkFunctionFind( int i_fourcc )
775 for( i_index = 0; ; i_index++ )
777 if( ( AVI_Chunk_Function[i_index].i_fourcc == i_fourcc )||
778 ( AVI_Chunk_Function[i_index].i_fourcc == 0 ) )
785 int _AVI_ChunkRead( input_thread_t *p_input,
787 avi_chunk_t *p_father,
788 vlc_bool_t b_seekable )
797 if( AVI_ChunkReadCommon( p_input, p_chk ) )
799 msg_Warn( p_input, "cannot read one chunk" );
802 p_chk->common.p_father = p_father;
804 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
805 if( AVI_Chunk_Function[i_index].AVI_ChunkRead_function )
807 return AVI_Chunk_Function[i_index].
808 AVI_ChunkRead_function( p_input, p_chk, b_seekable );
811 msg_Warn( p_input, "unknown chunk (not loaded)" );
812 return AVI_NextChunk( p_input, p_chk );
815 void _AVI_ChunkFree( input_thread_t *p_input,
819 avi_chunk_t *p_child, *p_next;
826 /* Free all child chunk */
827 p_child = p_chk->common.p_first;
830 p_next = p_child->common.p_next;
831 AVI_ChunkFree( p_input, p_child );
836 i_index = AVI_ChunkFunctionFind( p_chk->common.i_chunk_fourcc );
837 if( AVI_Chunk_Function[i_index].AVI_ChunkFree_function )
840 msg_Dbg( p_input, "free chunk %c%c%c%c",
841 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ) );
843 AVI_Chunk_Function[i_index].AVI_ChunkFree_function( p_input, p_chk);
847 msg_Warn( p_input, "unknown chunk (not unloaded)" );
849 p_chk->common.p_first = NULL;
850 p_chk->common.p_last = NULL;
855 int AVI_ChunkReadRoot( input_thread_t *p_input,
857 vlc_bool_t b_seekable )
859 avi_chunk_list_t *p_list = (avi_chunk_list_t*)p_root;
862 p_list->i_chunk_pos = 0;
863 p_list->i_chunk_size = p_input->stream.p_selected_area->i_size;
864 p_list->i_chunk_fourcc = AVIFOURCC_LIST;
865 p_list->p_father = NULL;
866 p_list->p_next = NULL;
867 p_list->p_first = NULL;
868 p_list->p_last = NULL;
870 p_list->i_type = VLC_FOURCC( 'r', 'o', 'o', 't' );
874 p_chk = malloc( sizeof( avi_chunk_t ) );
875 memset( p_chk, 0, sizeof( avi_chunk_t ) );
876 if( !p_root->common.p_first )
878 p_root->common.p_first = p_chk;
882 p_root->common.p_last->common.p_next = p_chk;
884 p_root->common.p_last = p_chk;
886 if( AVI_ChunkRead( p_input, p_chk, p_root, b_seekable ) ||
887 ( AVI_TellAbsolute( p_input ) >=
888 p_chk->common.p_father->common.i_chunk_pos +
889 __EVEN( p_chk->common.p_father->common.i_chunk_size ) ) )
893 /* If we can't seek then stop when we 've found first RIFF-AVI */
894 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF &&
895 p_chk->list.i_type == AVIFOURCC_AVI && !b_seekable )
904 void AVI_ChunkFreeRoot( input_thread_t *p_input,
907 AVI_ChunkFree( p_input, p_chk );
911 int _AVI_ChunkCount( avi_chunk_t *p_chk, vlc_fourcc_t i_fourcc )
914 avi_chunk_t *p_child;
922 p_child = p_chk->common.p_first;
925 if( p_child->common.i_chunk_fourcc == i_fourcc ||
926 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
927 p_child->list.i_type == i_fourcc ) )
931 p_child = p_child->common.p_next;
936 avi_chunk_t *_AVI_ChunkFind( avi_chunk_t *p_chk,
937 vlc_fourcc_t i_fourcc, int i_number )
939 avi_chunk_t *p_child;
944 p_child = p_chk->common.p_first;
948 if( p_child->common.i_chunk_fourcc == i_fourcc ||
949 ( p_child->common.i_chunk_fourcc == AVIFOURCC_LIST &&
950 p_child->list.i_type == i_fourcc ) )
960 p_child = p_child->common.p_next;
965 static void AVI_ChunkDumpDebug_level( input_thread_t *p_input,
966 avi_chunk_t *p_chk, int i_level )
970 avi_chunk_t *p_child;
972 memset( str, ' ', sizeof( str ) );
973 for( i = 1; i < i_level; i++ )
977 if( p_chk->common.i_chunk_fourcc == AVIFOURCC_RIFF||
978 p_chk->common.i_chunk_fourcc == AVIFOURCC_LIST )
980 sprintf( str + i_level * 5,
981 "%c %c%c%c%c-%c%c%c%c size:"I64Fu" pos:"I64Fu,
983 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
984 AVIFOURCC_PRINT( p_chk->list.i_type ),
985 p_chk->common.i_chunk_size,
986 p_chk->common.i_chunk_pos );
990 sprintf( str + i_level * 5,
991 "+ %c%c%c%c size:"I64Fu" pos:"I64Fu,
992 AVIFOURCC_PRINT( p_chk->common.i_chunk_fourcc ),
993 p_chk->common.i_chunk_size,
994 p_chk->common.i_chunk_pos );
996 msg_Dbg( p_input, "%s", str );
998 p_child = p_chk->common.p_first;
1001 AVI_ChunkDumpDebug_level( p_input, p_child, i_level + 1 );
1002 p_child = p_child->common.p_next;
1005 void _AVI_ChunkDumpDebug( input_thread_t *p_input,
1006 avi_chunk_t *p_chk )
1008 AVI_ChunkDumpDebug_level( p_input, p_chk, 0 );