1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: avi.c,v 1.46 2003/05/03 01:12:13 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 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
27 #include <string.h> /* strdup() */
29 #include <sys/types.h>
32 #include <vlc/input.h>
38 #define __AVI_SUBTITLE__ 1
40 #ifdef __AVI_SUBTITLE__
41 # include "../util/sub.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static int AVIInit ( vlc_object_t * );
49 static void __AVIEnd ( vlc_object_t * );
50 static int AVISeek ( input_thread_t *, mtime_t, int );
51 static int AVIDemux_Seekable ( input_thread_t * );
52 static int AVIDemux_UnSeekable( input_thread_t *p_input );
54 #define AVIEnd(a) __AVIEnd(VLC_OBJECT(a))
55 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
56 /*****************************************************************************
58 *****************************************************************************/
60 add_category_hint( N_("avi-demuxer"), NULL, VLC_TRUE );
61 add_bool( "avi-interleaved", 0, NULL,
62 N_("force interleaved method"),
63 N_("force interleaved method"), VLC_TRUE );
64 add_bool( "avi-index", 0, NULL,
65 N_("force index creation"),
66 N_("force index creation"), VLC_TRUE );
68 set_description( N_("AVI demuxer") );
69 set_capability( "demux", 212 );
70 set_callbacks( AVIInit, __AVIEnd );
73 /*****************************************************************************
74 * Some useful functions to manipulate memory
75 *****************************************************************************/
77 static uint16_t GetWLE( uint8_t *p_buff )
79 return (uint16_t)p_buff[0] | ( ((uint16_t)p_buff[1]) << 8 );
82 static uint32_t GetDWLE( uint8_t *p_buff )
84 return (uint32_t)p_buff[0] | ( ((uint32_t)p_buff[1]) << 8 ) |
85 ( ((uint32_t)p_buff[2]) << 16 ) | ( ((uint32_t)p_buff[3]) << 24 );
88 static uint32_t GetDWBE( uint8_t *p_buff )
90 return (uint32_t)p_buff[3] | ( ((uint32_t)p_buff[2]) << 8 ) |
91 ( ((uint32_t)p_buff[1]) << 16 ) | ( ((uint32_t)p_buff[0]) << 24 );
93 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
95 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
98 static inline off_t __EVEN( off_t i )
100 return (i & 1) ? i + 1 : i;
103 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
105 /* read data in a pes */
106 static int input_ReadInPES( input_thread_t *p_input,
107 pes_packet_t **pp_pes,
111 data_packet_t *p_data;
114 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
126 input_NewPacket( p_input->p_method_data, 0 );
127 p_pes->i_nb_data = 1;
128 p_pes->i_pes_size = 0;
132 p_pes->i_nb_data = 0;
133 p_pes->i_pes_size = 0;
135 while( p_pes->i_pes_size < i_size )
139 i_read = input_SplitBuffer(p_input,
142 p_pes->i_pes_size, 2048 ) );
145 /* should occur only with EOF and max allocation reached
146 * it safer to return an error */
148 input_DeletePES( p_input->p_method_data, p_pes );
152 if( !p_pes->p_first )
154 p_pes->p_first = p_data;
158 p_pes->p_last->p_next = p_data;
160 p_pes->p_last = p_data;
162 p_pes->i_pes_size += i_read;
166 return p_pes->i_pes_size;
169 /* Test if it seems that it's a key frame */
170 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
176 * startcode: 0x00000100 32bits
177 * framenumber ? 5bits
178 * piture type 0(I),1(P) 2bits
180 if( GetDWBE( p_byte ) != 0x00000100 )
182 /* it's not an msmpegv1 stream, strange...*/
183 return AVIIF_KEYFRAME;
187 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
190 case FOURCC_DIV3: // wmv1 also
192 * picture type 0(I),1(P) 2bits
194 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
196 /* we should find first occurence of 0x000001b6 (32bits)
197 * startcode: 0x000001b6 32bits
198 * piture type 0(I),1(P) 2bits
200 if( GetDWBE( p_byte ) != 0x000001b6 )
202 /* not true , need to find the first VOP header */
203 return AVIIF_KEYFRAME;
207 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
210 /* I can't do it, so say yes */
211 return AVIIF_KEYFRAME;
215 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
222 case WAVE_FORMAT_PCM:
223 return VLC_FOURCC( 'a', 'r', 'a', 'w' );
224 case WAVE_FORMAT_MPEG:
225 case WAVE_FORMAT_MPEGLAYER3:
226 return VLC_FOURCC( 'm', 'p', 'g', 'a' );
227 case WAVE_FORMAT_A52:
228 return VLC_FOURCC( 'a', '5', '2', ' ' );
229 case WAVE_FORMAT_WMA1:
230 return VLC_FOURCC( 'w', 'm', 'a', '1' );
231 case WAVE_FORMAT_WMA2:
232 return VLC_FOURCC( 'w', 'm', 'a', '2' );
234 return VLC_FOURCC( 'm', 's',
235 ( i_codec >> 8 )&0xff, i_codec&0xff );
238 // XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4
288 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
292 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
293 int *pi_number, int *pi_type )
295 #define SET_PTR( p, v ) if( p ) *(p) = (v);
298 c1 = ((uint8_t *)&i_id)[0];
299 c2 = ((uint8_t *)&i_id)[1];
301 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
303 SET_PTR( pi_number, 100 ); /* > max stream number */
304 SET_PTR( pi_type, UNKNOWN_ES );
308 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
309 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
312 SET_PTR( pi_type, AUDIO_ES );
316 SET_PTR( pi_type, VIDEO_ES );
319 SET_PTR( pi_type, UNKNOWN_ES );
326 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
330 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
334 p_pk->i_fourcc = GetFOURCC( p_peek );
335 p_pk->i_size = GetDWLE( p_peek + 4 );
336 p_pk->i_pos = AVI_TellAbsolute( p_input );
337 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
339 p_pk->i_type = GetFOURCC( p_peek + 8 );
346 memcpy( p_pk->i_peek, p_peek + 8, 8 );
348 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
352 static int AVI_PacketNext( input_thread_t *p_input )
356 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
360 //msg_Dbg( p_input, "skip %4.4s:%4.4s", (char*)&avi_ck.i_fourcc, (char*)&avi_ck.i_type );
362 if( avi_ck.i_fourcc == AVIFOURCC_LIST && ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
364 return AVI_SkipBytes( p_input, 12 );
366 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF && avi_ck.i_type == AVIFOURCC_AVIX )
368 return AVI_SkipBytes( p_input, 24 );
372 return AVI_SkipBytes( p_input, __EVEN( avi_ck.i_size ) + 8 );
375 static int AVI_PacketRead( input_thread_t *p_input,
377 pes_packet_t **pp_pes )
382 i_size = __EVEN( p_pk->i_size + 8 );
383 b_pad = ( i_size != p_pk->i_size + 8 );
385 if( input_ReadInPES( p_input, pp_pes, i_size ) != (ssize_t)i_size )
389 (*pp_pes)->p_first->p_payload_start += 8;
390 (*pp_pes)->i_pes_size -= 8;
394 (*pp_pes)->p_last->p_payload_end--;
395 (*pp_pes)->i_pes_size--;
401 static int AVI_PacketSearch( input_thread_t *p_input )
403 demux_sys_t *p_avi = p_input->p_demux_data;
408 if( AVI_SkipBytes( p_input, 1 ) )
412 AVI_PacketGetHeader( p_input, &avi_pk );
413 if( avi_pk.i_stream < p_avi->i_streams &&
414 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
418 switch( avi_pk.i_fourcc )
430 static void __AVI_AddEntryIndex( avi_stream_t *p_info,
431 AVIIndexEntry_t *p_index)
433 if( p_info->p_index == NULL )
435 p_info->i_idxmax = 16384;
437 if( !( p_info->p_index = calloc( p_info->i_idxmax,
438 sizeof( AVIIndexEntry_t ) ) ) )
443 if( p_info->i_idxnb >= p_info->i_idxmax )
445 p_info->i_idxmax += 16384;
446 if( !( p_info->p_index = realloc( (void*)p_info->p_index,
448 sizeof( AVIIndexEntry_t ) ) ) )
453 /* calculate cumulate length */
454 if( p_info->i_idxnb > 0 )
456 p_index->i_lengthtotal =
457 p_info->p_index[p_info->i_idxnb - 1].i_length +
458 p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal;
462 p_index->i_lengthtotal = 0;
465 p_info->p_index[p_info->i_idxnb] = *p_index;
470 static void AVI_IndexAddEntry( demux_sys_t *p_avi,
472 AVIIndexEntry_t *p_index)
474 __AVI_AddEntryIndex( p_avi->pp_info[i_stream],
476 if( p_avi->i_movi_lastchunk_pos < p_index->i_pos )
478 p_avi->i_movi_lastchunk_pos = p_index->i_pos;
482 static int AVI_IndexLoad_idx1( input_thread_t *p_input )
484 demux_sys_t *p_avi = p_input->p_demux_data;
486 avi_chunk_list_t *p_riff;
487 avi_chunk_list_t *p_movi;
488 avi_chunk_idx1_t *p_idx1;
490 unsigned int i_stream;
491 unsigned int i_index;
494 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
497 p_idx1 = (avi_chunk_idx1_t*)AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
498 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
502 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
506 /* *** calculate offset *** */
507 if( p_idx1->i_entry_count > 0 &&
508 p_idx1->entry[0].i_pos < p_movi->i_chunk_pos )
510 i_offset = p_movi->i_chunk_pos + 8;
517 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
521 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
524 if( i_stream < p_avi->i_streams &&
525 i_cat == p_avi->pp_info[i_stream]->i_cat )
527 AVIIndexEntry_t index;
528 index.i_id = p_idx1->entry[i_index].i_fourcc;
530 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
531 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
532 index.i_length = p_idx1->entry[i_index].i_length;
533 AVI_IndexAddEntry( p_avi, i_stream, &index );
539 static void __Parse_indx( input_thread_t *p_input,
541 avi_chunk_indx_t *p_indx )
543 demux_sys_t *p_avi = p_input->p_demux_data;
544 AVIIndexEntry_t index;
547 msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
548 if( p_indx->i_indexsubtype == 0 )
550 for( i = 0; i < p_indx->i_entriesinuse; i++ )
552 index.i_id = p_indx->i_id;
553 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
554 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
555 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
557 AVI_IndexAddEntry( p_avi, i_stream, &index );
560 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
562 for( i = 0; i < p_indx->i_entriesinuse; i++ )
564 index.i_id = p_indx->i_id;
565 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
566 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
567 index.i_length = p_indx->idx.field[i].i_size;
569 AVI_IndexAddEntry( p_avi, i_stream, &index );
574 msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
578 static void AVI_IndexLoad_indx( input_thread_t *p_input )
580 demux_sys_t *p_avi = p_input->p_demux_data;
581 unsigned int i_stream;
584 avi_chunk_list_t *p_riff;
585 avi_chunk_list_t *p_hdrl;
587 p_riff = (void*)AVI_ChunkFind( &p_avi->ck_root,
589 p_hdrl = (void*)AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
591 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
593 avi_chunk_list_t *p_strl;
594 avi_chunk_indx_t *p_indx;
596 #define p_stream p_avi->pp_info[i_stream]
597 p_strl = (void*)AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
598 p_indx = (void*)AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
602 msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
606 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
608 __Parse_indx( p_input, i_stream, p_indx );
610 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
612 avi_chunk_indx_t ck_sub;
613 for( i = 0; i < p_indx->i_entriesinuse; i++ )
615 if( AVI_SeekAbsolute( p_input, p_indx->idx.super[i].i_offset ) )
620 if( AVI_ChunkRead( p_input, &ck_sub, NULL, p_avi->b_seekable ) )
628 msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
633 static void AVI_IndexLoad( input_thread_t *p_input )
635 demux_sys_t *p_avi = p_input->p_demux_data;
636 unsigned int i_stream;
638 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
640 p_avi->pp_info[i_stream]->i_idxnb = 0;
641 p_avi->pp_info[i_stream]->i_idxmax = 0;
642 p_avi->pp_info[i_stream]->p_index = NULL;
647 AVI_IndexLoad_indx( p_input );
651 if( AVI_IndexLoad_idx1( p_input ) )
653 /* try indx if idx1 failed as some "normal" file have indx too */
654 AVI_IndexLoad_indx( p_input );
659 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
662 "stream[%d] created %d index entries",
664 p_avi->pp_info[i_stream]->i_idxnb );
667 static void AVI_IndexCreate( input_thread_t *p_input )
669 demux_sys_t *p_avi = p_input->p_demux_data;
671 avi_chunk_list_t *p_riff;
672 avi_chunk_list_t *p_movi;
674 unsigned int i_stream;
677 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
679 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
683 msg_Err( p_input, "cannot find p_movi" );
687 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
689 p_avi->pp_info[i_stream]->i_idxnb = 0;
690 p_avi->pp_info[i_stream]->i_idxmax = 0;
691 p_avi->pp_info[i_stream]->p_index = NULL;
693 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
694 p_input->stream.p_selected_area->i_size );
696 AVI_SeekAbsolute( p_input, p_movi->i_chunk_pos + 12);
697 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
702 if( AVI_PacketGetHeader( p_input, &pk ) )
706 if( pk.i_stream < p_avi->i_streams &&
707 pk.i_cat == p_avi->pp_info[pk.i_stream]->i_cat )
709 AVIIndexEntry_t index;
710 index.i_id = pk.i_fourcc;
712 AVI_GetKeyFlag(p_avi->pp_info[pk.i_stream]->i_codec, pk.i_peek);
713 index.i_pos = pk.i_pos;
714 index.i_length = pk.i_size;
715 AVI_IndexAddEntry( p_avi, pk.i_stream, &index );
719 switch( pk.i_fourcc )
724 avi_chunk_list_t *p_avix;
725 p_avix = (void*)AVI_ChunkFind( &p_avi->ck_root,
728 msg_Dbg( p_input, "looking for new RIFF chunk" );
729 if( AVI_SeekAbsolute( p_input, p_avix->i_chunk_pos + 24) )
737 msg_Dbg( p_input, "new RIFF chunk found" );
742 msg_Warn( p_input, "need resync, probably broken avi" );
743 if( AVI_PacketSearch( p_input ) )
745 msg_Warn( p_input, "lost sync, abord index creation" );
751 if( ( !p_avi->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
752 AVI_PacketNext( p_input ) )
759 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
762 "stream[%d] creating %d index entries",
764 p_avi->pp_info[i_stream]->i_idxnb );
769 /*****************************************************************************
771 *****************************************************************************/
772 static vlc_bool_t AVI_StreamStart ( input_thread_t *, demux_sys_t *, int );
773 static int AVI_StreamSeek ( input_thread_t *, demux_sys_t *, int, mtime_t );
774 static void AVI_StreamStop ( input_thread_t *, demux_sys_t *, int );
775 static int AVI_StreamStopFinishedStreams( input_thread_t *, demux_sys_t * );
777 static vlc_bool_t AVI_StreamStart( input_thread_t *p_input,
778 demux_sys_t *p_avi, int i_stream )
780 #define p_stream p_avi->pp_info[i_stream]
781 if( !p_stream->p_es )
783 msg_Warn( p_input, "stream[%d] unselectable", i_stream );
786 if( p_stream->b_activated )
788 msg_Warn( p_input, "stream[%d] already selected", i_stream );
792 if( !p_stream->p_es->p_decoder_fifo )
794 vlc_mutex_lock( &p_input->stream.stream_lock );
795 input_SelectES( p_input, p_stream->p_es );
796 vlc_mutex_unlock( &p_input->stream.stream_lock );
798 p_stream->b_activated = p_stream->p_es->p_decoder_fifo ? VLC_TRUE
800 if( p_stream->b_activated && p_avi->b_seekable)
802 AVI_StreamSeek( p_input, p_avi, i_stream, p_avi->i_time );
805 return p_stream->b_activated;
809 static void AVI_StreamStop( input_thread_t *p_input,
810 demux_sys_t *p_avi, int i_stream )
812 #define p_stream p_avi->pp_info[i_stream]
814 if( !p_stream->b_activated )
816 msg_Warn( p_input, "stream[%d] already unselected", i_stream );
820 if( p_stream->p_es->p_decoder_fifo )
822 vlc_mutex_lock( &p_input->stream.stream_lock );
823 input_UnselectES( p_input, p_stream->p_es );
824 vlc_mutex_unlock( &p_input->stream.stream_lock );
828 p_stream->b_activated = VLC_FALSE;
833 static int AVI_StreamStopFinishedStreams( input_thread_t *p_input,
836 unsigned int i_stream;
839 for( i_stream = 0,b_end = VLC_TRUE;
840 i_stream < p_avi->i_streams; i_stream++ )
842 #define p_stream p_avi->pp_info[i_stream]
843 if( p_stream->i_idxposc >= p_stream->i_idxnb )
845 AVI_StreamStop( p_input, p_avi, i_stream );
855 /****************************************************************************
856 * AVI_MovieGetLength give max streams length in second
857 ****************************************************************************/
858 static mtime_t AVI_MovieGetLength( input_thread_t *p_input, demux_sys_t *p_avi )
860 unsigned int i_stream;
864 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
866 #define p_stream p_avi->pp_info[i_stream]
868 /* fix length for each stream */
869 if( p_stream->i_idxnb < 1 || !p_stream->p_index )
874 if( p_stream->i_samplesize )
877 (mtime_t)( p_stream->p_index[p_stream->i_idxnb-1].i_lengthtotal +
878 p_stream->p_index[p_stream->i_idxnb-1].i_length ) *
879 (mtime_t)p_stream->i_scale /
880 (mtime_t)p_stream->i_rate /
881 (mtime_t)p_stream->i_samplesize;
885 i_length = (mtime_t)p_stream->i_idxnb *
886 (mtime_t)p_stream->i_scale /
887 (mtime_t)p_stream->i_rate;
891 "stream[%d] length:"I64Fd" (based on index)",
894 i_maxlength = __MAX( i_maxlength, i_length );
901 /*****************************************************************************
902 * AVIEnd: frees unused data
903 *****************************************************************************/
904 static void __AVIEnd ( vlc_object_t * p_this )
906 input_thread_t * p_input = (input_thread_t *)p_this;
908 demux_sys_t *p_avi = p_input->p_demux_data ;
912 for( i = 0; i < p_avi->i_streams; i++ )
914 if( p_avi->pp_info[i] )
916 if( p_avi->pp_info[i]->p_index )
918 free( p_avi->pp_info[i]->p_index );
920 free( p_avi->pp_info[i] );
923 free( p_avi->pp_info );
925 #ifdef __AVI_SUBTITLE__
928 subtitle_Close( p_avi->p_sub );
932 AVI_ChunkFreeRoot( p_input, &p_avi->ck_root );
934 FREE( p_input->p_demux_data );
937 /*****************************************************************************
938 * AVIInit: check file and initializes AVI structures
939 *****************************************************************************/
940 static int AVIInit( vlc_object_t * p_this )
942 input_thread_t * p_input = (input_thread_t *)p_this;
944 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
945 avi_chunk_list_t *p_hdrl, *p_movi;
947 avi_chunk_list_t *p_INFO;
948 avi_chunk_strz_t *p_name;
950 avi_chunk_avih_t *p_avih;
952 es_descriptor_t *p_es = NULL; /* avoid warning */
954 #ifdef __AVI_SUBTITLE__
955 mtime_t i_microsecperframe = 0; // for some subtitle format
958 vlc_bool_t b_stream_audio, b_stream_video;
960 p_input->pf_demux = AVIDemux_Seekable;
961 if( AVI_TestFile( p_input ) )
963 msg_Warn( p_input, "avi module discarded (invalid headr)" );
967 /* Initialize access plug-in structures. */
968 if( p_input->i_mtu == 0 )
971 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
974 if( !( p_input->p_demux_data =
975 p_avi = malloc( sizeof(demux_sys_t) ) ) )
977 msg_Err( p_input, "out of memory" );
980 memset( p_avi, 0, sizeof( demux_sys_t ) );
983 p_avi->b_seekable = ( ( p_input->stream.b_seekable )
984 &&( p_input->stream.i_method == INPUT_METHOD_FILE ) );
985 p_avi->i_movi_lastchunk_pos = 0;
986 p_avi->b_odml = VLC_FALSE;
988 /* *** for unseekable stream, automaticaly use AVIDemux_interleaved *** */
989 if( !p_avi->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
991 p_input->pf_demux = AVIDemux_UnSeekable;
994 if( AVI_ChunkReadRoot( p_input, &p_avi->ck_root, p_avi->b_seekable ) )
996 msg_Err( p_input, "avi module discarded (invalid file)" );
999 AVI_ChunkDumpDebug( p_input, &p_avi->ck_root );
1001 if( AVI_ChunkCount( &p_avi->ck_root, AVIFOURCC_RIFF ) > 1 )
1003 int i_count = AVI_ChunkCount( &p_avi->ck_root, AVIFOURCC_RIFF );
1006 msg_Warn( p_input, "multiple riff -> OpenDML ?" );
1007 for( i = 1; i < i_count; i++ )
1009 avi_chunk_list_t *p_avix;
1011 p_avix = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
1012 AVIFOURCC_RIFF, i );
1013 if( p_avix->i_type == AVIFOURCC_AVIX )
1015 msg_Warn( p_input, "detected OpenDML file" );
1017 p_avi->b_odml = VLC_TRUE;
1021 p_avi->b_odml = VLC_TRUE;
1025 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
1026 AVIFOURCC_RIFF, 0 );
1027 p_hdrl = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
1028 AVIFOURCC_hdrl, 0 );
1029 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
1030 AVIFOURCC_movi, 0 );
1032 p_INFO = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
1033 AVIFOURCC_INFO, 0 );
1034 p_name = (avi_chunk_strz_t*)AVI_ChunkFind( p_INFO,
1035 AVIFOURCC_INAM, 0 );
1042 if( !p_hdrl || !p_movi )
1044 msg_Err( p_input, "avi module discarded (invalid file)" );
1045 return VLC_EGENERIC;
1048 if( !( p_avih = (avi_chunk_avih_t*)AVI_ChunkFind( p_hdrl,
1049 AVIFOURCC_avih, 0 ) ) )
1051 msg_Err( p_input, "cannot find avih chunk" );
1052 return VLC_EGENERIC;
1054 p_avi->i_streams = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
1055 if( p_avih->i_streams != p_avi->i_streams )
1058 "found %d stream but %d are declared",
1060 p_avih->i_streams );
1062 if( p_avi->i_streams == 0 )
1065 msg_Err( p_input, "no stream defined!" );
1066 return VLC_EGENERIC;
1069 /* create one program */
1070 vlc_mutex_lock( &p_input->stream.stream_lock );
1071 if( input_InitStream( p_input, 0 ) == -1)
1073 vlc_mutex_unlock( &p_input->stream.stream_lock );
1075 msg_Err( p_input, "cannot init stream" );
1076 return VLC_EGENERIC;
1078 if( input_AddProgram( p_input, 0, 0) == NULL )
1080 vlc_mutex_unlock( &p_input->stream.stream_lock );
1082 msg_Err( p_input, "cannot add program" );
1083 return VLC_EGENERIC;
1085 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1086 vlc_mutex_unlock( &p_input->stream.stream_lock );
1088 /* print informations on streams */
1089 msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
1091 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
1092 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
1093 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
1094 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
1096 input_info_category_t *p_cat = input_InfoCategory( p_input, _("Avi") );
1097 input_AddInfo( p_cat, _("Number of Streams"), "%d", p_avi->i_streams );
1098 input_AddInfo( p_cat, _("Flags"), "%s%s%s%s",
1099 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
1100 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
1101 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
1102 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
1105 /* now read info on each stream and create ES */
1106 p_avi->pp_info = calloc( p_avi->i_streams,
1107 sizeof( avi_stream_t* ) );
1108 memset( p_avi->pp_info,
1110 sizeof( avi_stream_t* ) * p_avi->i_streams );
1112 for( i = 0 ; i < p_avi->i_streams; i++ )
1114 avi_chunk_list_t *p_avi_strl;
1115 avi_chunk_strh_t *p_avi_strh;
1116 avi_chunk_strf_auds_t *p_avi_strf_auds;
1117 avi_chunk_strf_vids_t *p_avi_strf_vids;
1120 #define p_info p_avi->pp_info[i]
1121 p_info = malloc( sizeof(avi_stream_t ) );
1122 memset( p_info, 0, sizeof( avi_stream_t ) );
1124 p_avi_strl = (avi_chunk_list_t*)AVI_ChunkFind( p_hdrl,
1125 AVIFOURCC_strl, i );
1126 p_avi_strh = (avi_chunk_strh_t*)AVI_ChunkFind( p_avi_strl,
1127 AVIFOURCC_strh, 0 );
1128 p_avi_strf_auds = (avi_chunk_strf_auds_t*)
1129 p_avi_strf_vids = (avi_chunk_strf_vids_t*)
1130 AVI_ChunkFind( p_avi_strl, AVIFOURCC_strf, 0 );
1132 if( !p_avi_strl || !p_avi_strh ||
1133 ( !p_avi_strf_auds && !p_avi_strf_vids ) )
1135 msg_Warn( p_input, "stream[%d] incomlete", i );
1139 /* *** Init p_info *** */
1140 p_info->i_rate = p_avi_strh->i_rate;
1141 p_info->i_scale = p_avi_strh->i_scale;
1142 p_info->i_samplesize = p_avi_strh->i_samplesize;
1143 msg_Dbg( p_input, "stream[%d] rate:%d scale:%d samplesize:%d",
1145 p_info->i_rate, p_info->i_scale, p_info->i_samplesize );
1146 switch( p_avi_strh->i_type )
1148 case( AVIFOURCC_auds ):
1149 p_info->i_cat = AUDIO_ES;
1151 AVI_FourccGetCodec( AUDIO_ES,
1152 p_avi_strf_auds->p_wf->wFormatTag );
1153 p_info->i_codec = p_info->i_fourcc;
1154 i_init_size = p_avi_strf_auds->i_chunk_size;
1155 p_init_data = p_avi_strf_auds->p_wf;
1156 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
1158 p_avi_strf_auds->p_wf->wFormatTag,
1159 p_avi_strf_auds->p_wf->nChannels,
1160 p_avi_strf_auds->p_wf->nSamplesPerSec,
1161 p_avi_strf_auds->p_wf->wBitsPerSample );
1163 char hepp[sizeof("Stream") + 10];
1164 input_info_category_t *p_cat;
1165 sprintf(hepp, "Stream %d", i);
1166 p_cat = input_InfoCategory( p_input, hepp);
1167 input_AddInfo( p_cat, _("Type"), "Audio(0x%x)",
1168 p_avi_strf_auds->p_wf->wFormatTag );
1169 input_AddInfo( p_cat, _("Codec"), "%4.4s",
1170 (const char*)&(p_info->i_codec) );
1171 input_AddInfo( p_cat, _("Channels"), "%d",
1172 p_avi_strf_auds->p_wf->nChannels );
1173 input_AddInfo( p_cat, _("Sample Rate"), "%d",
1174 p_avi_strf_auds->p_wf->nSamplesPerSec );
1175 input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
1176 p_avi_strf_auds->p_wf->wBitsPerSample );
1180 case( AVIFOURCC_vids ):
1181 p_info->i_cat = VIDEO_ES;
1182 /* XXX quick hack for playing ffmpeg video, I don't know
1183 who is doing something wrong */
1184 p_info->i_samplesize = 0;
1185 p_info->i_fourcc = p_avi_strf_vids->p_bih->biCompression;
1187 AVI_FourccGetCodec( VIDEO_ES, p_info->i_fourcc );
1188 i_init_size = p_avi_strf_vids->i_chunk_size;
1189 p_init_data = p_avi_strf_vids->p_bih;
1190 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
1192 (char*)&p_avi_strf_vids->p_bih->biCompression,
1193 p_avi_strf_vids->p_bih->biWidth,
1194 p_avi_strf_vids->p_bih->biHeight,
1195 p_avi_strf_vids->p_bih->biBitCount,
1196 (float)p_info->i_rate /
1197 (float)p_info->i_scale );
1199 char hepp[sizeof("Stream") + 10];
1200 input_info_category_t *p_cat;
1201 sprintf(hepp, "Stream %d", i);
1202 p_cat = input_InfoCategory( p_input, hepp);
1203 input_AddInfo( p_cat, _("Type"), _("Video") );
1204 input_AddInfo( p_cat, _("Codec"), "%4.4s",
1205 (const char*)&(p_info->i_codec) );
1206 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
1207 p_avi_strf_vids->p_bih->biWidth,
1208 p_avi_strf_vids->p_bih->biHeight );
1209 input_AddInfo( p_cat, _("Frame Rate"), "%f",
1210 (float)p_info->i_rate /
1211 (float)p_info->i_scale );
1212 input_AddInfo( p_cat, _("Bits Per Pixel"), "%d",
1213 p_avi_strf_vids->p_bih->biBitCount );
1215 #ifdef __AVI_SUBTITLE__
1216 if( i_microsecperframe == 0 )
1218 i_microsecperframe = (mtime_t)1000000 *
1219 (mtime_t)p_info->i_scale /
1220 (mtime_t)p_info->i_rate;
1225 msg_Warn( p_input, "stream[%d] unknown type", i );
1226 p_info->i_cat = UNKNOWN_ES;
1230 char psz_cat[32]; /* We'll clip i just in case */
1231 input_info_category_t *p_cat;
1232 sprintf( psz_cat, "Stream %d", __MIN( i, 100000 ) );
1233 p_cat = input_InfoCategory( p_input, psz_cat );
1234 input_AddInfo( p_cat, _("Type"), _("Unknown") );
1238 p_info->b_activated = VLC_FALSE;
1240 vlc_mutex_lock( &p_input->stream.stream_lock );
1242 p_es = input_AddES( p_input,
1243 p_input->stream.p_selected_program, 1+i,
1245 vlc_mutex_unlock( &p_input->stream.stream_lock );
1246 p_es->i_stream_id =i; /* XXX: i don't use it */
1247 p_es->i_fourcc = p_info->i_fourcc;
1248 p_es->i_cat = p_info->i_cat;
1249 if( p_es->i_cat == AUDIO_ES )
1251 if( i_init_size < sizeof( WAVEFORMATEX ) )
1253 i_init_size = sizeof( WAVEFORMATEX );
1255 p_es->p_waveformatex = malloc( i_init_size );
1256 memcpy( p_es->p_waveformatex, p_init_data, i_init_size );
1258 else if( p_es->i_cat == VIDEO_ES )
1260 p_es->p_bitmapinfoheader = malloc( i_init_size );
1261 memcpy( p_es->p_bitmapinfoheader, p_init_data, i_init_size );
1266 #ifdef __AVI_SUBTITLE__
1267 if( ( p_avi->p_sub = subtitle_New( p_input, NULL, i_microsecperframe ) ) )
1269 subtitle_Select( p_avi->p_sub );
1273 if( config_GetInt( p_input, "avi-index" ) )
1275 if( p_avi->b_seekable )
1277 AVI_IndexCreate( p_input );
1281 msg_Warn( p_input, "cannot create index (unseekable stream)" );
1282 AVI_IndexLoad( p_input );
1287 AVI_IndexLoad( p_input );
1290 /* *** movie length in sec *** */
1291 p_avi->i_length = AVI_MovieGetLength( p_input, p_avi );
1292 if( p_avi->i_length < (mtime_t)p_avih->i_totalframes *
1293 (mtime_t)p_avih->i_microsecperframe /
1296 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
1299 /* fix some BeOS MediaKit generated file */
1300 for( i = 0 ; i < p_avi->i_streams; i++ )
1302 avi_chunk_list_t *p_avi_strl;
1303 avi_chunk_strh_t *p_avi_strh;
1304 avi_chunk_strf_auds_t *p_avi_strf_auds;
1305 #define p_stream p_avi->pp_info[i]
1307 if( p_stream->i_cat != AUDIO_ES )
1311 if( p_stream->i_idxnb < 1 ||
1312 p_stream->i_scale != 1 ||
1313 p_stream->i_samplesize != 0 )
1317 p_avi_strl = (avi_chunk_list_t*)AVI_ChunkFind( p_hdrl,
1318 AVIFOURCC_strl, i );
1319 p_avi_strh = (avi_chunk_strh_t*)AVI_ChunkFind( p_avi_strl,
1320 AVIFOURCC_strh, 0 );
1322 (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_avi_strl,
1323 AVIFOURCC_strf, 0 );
1325 if( p_avi_strf_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
1326 (unsigned int)p_stream->i_rate == p_avi_strf_auds->p_wf->nSamplesPerSec )
1328 int64_t i_track_length =
1329 p_stream->p_index[p_stream->i_idxnb-1].i_length +
1330 p_stream->p_index[p_stream->i_idxnb-1].i_lengthtotal;
1331 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
1332 (mtime_t)p_avih->i_microsecperframe;
1336 msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
1339 p_stream->i_samplesize = 1;
1340 p_stream->i_rate = i_track_length * (int64_t)1000000/ i_length;
1341 msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, p_stream->i_rate, p_stream->i_scale );
1346 vlc_mutex_lock( &p_input->stream.stream_lock );
1347 if( p_avi->i_length )
1349 p_input->stream.i_mux_rate =
1350 p_input->stream.p_selected_area->i_size / 50 / p_avi->i_length;
1354 p_input->stream.i_mux_rate = 0;
1356 vlc_mutex_unlock( &p_input->stream.stream_lock );
1358 b_stream_audio = VLC_FALSE;
1359 b_stream_video = VLC_FALSE;
1361 for( i = 0; i < p_avi->i_streams; i++ )
1363 #define p_info p_avi->pp_info[i]
1364 switch( p_info->p_es->i_cat )
1368 if( !b_stream_video )
1370 b_stream_video = AVI_StreamStart( p_input, p_avi, i );
1375 if( !b_stream_audio )
1377 b_stream_audio = AVI_StreamStart( p_input, p_avi, i );
1386 if( !b_stream_video )
1388 msg_Warn( p_input, "no video stream found" );
1390 if( !b_stream_audio )
1392 msg_Warn( p_input, "no audio stream found!" );
1395 vlc_mutex_lock( &p_input->stream.stream_lock );
1396 p_input->stream.p_selected_program->b_is_ok = 1;
1397 vlc_mutex_unlock( &p_input->stream.stream_lock );
1399 if( p_avi->b_seekable )
1401 AVI_ChunkGoto( p_input, p_movi );
1405 // already at begining of p_movi
1407 AVI_SkipBytes( p_input, 12 ); // enter in p_movi
1409 p_avi->i_movi_begin = p_movi->i_chunk_pos;
1416 /*****************************************************************************
1417 * Function to convert pts to chunk or byte
1418 *****************************************************************************/
1420 static inline mtime_t AVI_PTSToChunk( avi_stream_t *p_info,
1423 return (mtime_t)((int64_t)i_pts *
1424 (int64_t)p_info->i_rate /
1425 (int64_t)p_info->i_scale /
1428 static inline mtime_t AVI_PTSToByte( avi_stream_t *p_info,
1431 return (mtime_t)((int64_t)i_pts *
1432 (int64_t)p_info->i_rate /
1433 (int64_t)p_info->i_scale /
1435 (int64_t)p_info->i_samplesize );
1438 static mtime_t AVI_GetDPTS( avi_stream_t *p_stream, int i_count )
1440 if( p_stream->i_samplesize )
1442 return (mtime_t)( (int64_t)1000000 *
1444 (int64_t)p_stream->i_scale /
1445 (int64_t)p_stream->i_rate /
1446 (int64_t)p_stream->i_samplesize );
1450 return (mtime_t)( (int64_t)1000000 *
1452 (int64_t)p_stream->i_scale /
1453 (int64_t)p_stream->i_rate);
1458 static mtime_t AVI_GetPTS( avi_stream_t *p_info )
1461 if( p_info->i_samplesize )
1463 /* we need a valid entry we will emulate one */
1465 if( p_info->i_idxposc == p_info->i_idxnb )
1467 if( p_info->i_idxposc )
1469 /* use the last entry */
1470 i_len = p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal
1471 + p_info->p_index[p_info->i_idxnb - 1].i_length
1472 + p_info->i_idxposb; /* should be 0 */
1476 i_len = p_info->i_idxposb;
1477 /* no valid entry use only offset*/
1482 i_len = p_info->p_index[p_info->i_idxposc].i_lengthtotal
1483 + p_info->i_idxposb;
1485 return (mtime_t)( (int64_t)1000000 *
1487 (int64_t)p_info->i_scale /
1488 (int64_t)p_info->i_rate /
1489 (int64_t)p_info->i_samplesize );
1493 /* even if p_info->i_idxposc isn't valid, there isn't any problem */
1494 return (mtime_t)( (int64_t)1000000 *
1495 (int64_t)(p_info->i_idxposc ) *
1496 (int64_t)p_info->i_scale /
1497 (int64_t)p_info->i_rate);
1502 static void AVI_FixPTS( avi_stream_t *p_stream, pes_packet_t *p_pes )
1504 data_packet_t *p_data;
1508 switch( p_stream->i_fourcc )
1510 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1511 p_data = p_pes->p_first;
1514 p = p_data->p_payload_start;
1515 while( p < p_data->p_payload_end - 2 )
1517 if( p[0] == 0xff && ( p[1]&0xe0) == 0xe0 )
1519 mtime_t i_diff = AVI_GetDPTS( p_stream, i_pos );
1520 p_pes->i_dts += i_diff;
1521 p_pes->i_pts += i_diff;
1526 p_data = p_data->p_next;
1529 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1530 p_data = p_pes->p_first;
1533 p = p_data->p_payload_start;
1534 while( p < p_data->p_payload_end - 2 )
1536 if( p[0] == 0x0b && p[1] == 0x77 )
1538 mtime_t i_diff = AVI_GetDPTS( p_stream, i_pos );
1539 p_pes->i_dts += i_diff;
1540 p_pes->i_pts += i_diff;
1544 p_data = p_data->p_next;
1548 /* we can't fix :( */
1554 static int AVI_StreamChunkFind( input_thread_t *p_input,
1555 unsigned int i_stream )
1557 demux_sys_t *p_avi = p_input->p_demux_data;
1558 avi_packet_t avi_pk;
1560 /* find first chunk of i_stream that isn't in index */
1562 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin + 12 )
1564 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
1565 if( AVI_PacketNext( p_input ) )
1567 return VLC_EGENERIC;
1572 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin + 12 );
1578 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1580 msg_Warn( p_input, "cannot get packet header" );
1581 return VLC_EGENERIC;
1583 if( avi_pk.i_stream >= p_avi->i_streams ||
1584 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1586 if( AVI_PacketNext( p_input ) )
1588 return VLC_EGENERIC;
1593 /* add this chunk to the index */
1594 AVIIndexEntry_t index;
1596 index.i_id = avi_pk.i_fourcc;
1598 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
1600 index.i_pos = avi_pk.i_pos;
1601 index.i_length = avi_pk.i_size;
1602 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
1604 if( avi_pk.i_stream == i_stream )
1609 if( AVI_PacketNext( p_input ) )
1611 return VLC_EGENERIC;
1618 /* be sure that i_ck will be a valid index entry */
1619 static int AVI_SetStreamChunk( input_thread_t *p_input,
1620 unsigned int i_stream,
1623 demux_sys_t *p_avi = p_input->p_demux_data;
1624 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1626 p_stream->i_idxposc = i_ck;
1627 p_stream->i_idxposb = 0;
1629 if( i_ck >= p_stream->i_idxnb )
1631 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1634 p_stream->i_idxposc++;
1635 if( AVI_StreamChunkFind( p_input, i_stream ) )
1637 return VLC_EGENERIC;
1640 } while( p_stream->i_idxposc < i_ck );
1647 /* XXX FIXME up to now, we assume that all chunk are one after one */
1648 static int AVI_SetStreamBytes( input_thread_t *p_input,
1649 unsigned int i_stream,
1652 demux_sys_t *p_avi = p_input->p_demux_data;
1653 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1655 if( ( p_stream->i_idxnb > 0 )
1656 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1657 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1659 /* index is valid to find the ck */
1660 /* uses dichototmie to be fast enougth */
1661 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1662 int i_idxmax = p_stream->i_idxnb;
1666 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1668 i_idxmax = i_idxposc ;
1669 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1673 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1674 p_stream->p_index[i_idxposc].i_length <= i_byte)
1676 i_idxmin = i_idxposc ;
1677 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1681 p_stream->i_idxposc = i_idxposc;
1682 p_stream->i_idxposb = i_byte -
1683 p_stream->p_index[i_idxposc].i_lengthtotal;
1692 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1693 p_stream->i_idxposb = 0;
1696 p_stream->i_idxposc++;
1697 if( AVI_StreamChunkFind( p_input, i_stream ) )
1699 return VLC_EGENERIC;
1702 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1703 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1705 p_stream->i_idxposb = i_byte -
1706 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1711 static int AVI_StreamSeek( input_thread_t *p_input,
1716 #define p_stream p_avi->pp_info[i_stream]
1719 i_oldpts = AVI_GetPTS( p_stream );
1721 if( !p_stream->i_samplesize )
1723 if( AVI_SetStreamChunk( p_input,
1725 AVI_PTSToChunk( p_stream, i_date ) ) )
1727 return VLC_EGENERIC;
1730 /* search key frame */
1732 "old:"I64Fd" %s new "I64Fd,
1734 i_oldpts > i_date ? ">" : "<",
1737 if( i_date < i_oldpts )
1739 while( p_stream->i_idxposc > 0 &&
1740 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1743 if( AVI_SetStreamChunk( p_input,
1745 p_stream->i_idxposc - 1 ) )
1747 return VLC_EGENERIC;
1753 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1754 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1757 if( AVI_SetStreamChunk( p_input,
1759 p_stream->i_idxposc + 1 ) )
1761 return VLC_EGENERIC;
1768 if( AVI_SetStreamBytes( p_input,
1770 AVI_PTSToByte( p_stream, i_date ) ) )
1772 return VLC_EGENERIC;
1779 /*****************************************************************************
1780 * AVISeek: goto to i_date or i_percent
1781 *****************************************************************************
1782 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1783 *****************************************************************************/
1784 static int AVISeek ( input_thread_t *p_input,
1785 mtime_t i_date, int i_percent )
1788 demux_sys_t *p_avi = p_input->p_demux_data;
1789 unsigned int i_stream;
1791 "seek requested: "I64Fd" secondes %d%%",
1795 if( p_avi->b_seekable )
1797 if( !p_avi->i_length )
1799 avi_stream_t *p_stream;
1802 /* use i_percent to create a true i_date */
1804 "mmh, seeking without index at %d%%"
1805 " work only for interleaved file", i_percent );
1807 if( i_percent >= 100 )
1809 msg_Warn( p_input, "cannot seek so far !" );
1812 i_percent = __MAX( i_percent, 0 );
1814 /* try to find chunk that is at i_percent or the file */
1815 i_pos = __MAX( i_percent *
1816 p_input->stream.p_selected_area->i_size / 100,
1817 p_avi->i_movi_begin );
1818 /* search first selected stream */
1819 for( i_stream = 0, p_stream = NULL;
1820 i_stream < p_avi->i_streams; i_stream++ )
1822 p_stream = p_avi->pp_info[i_stream];
1823 if( p_stream->b_activated )
1828 if( !p_stream || !p_stream->b_activated )
1830 msg_Warn( p_input, "cannot find any selected stream" );
1834 /* be sure that the index exit */
1835 if( AVI_SetStreamChunk( p_input,
1839 msg_Warn( p_input, "cannot seek" );
1843 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1844 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1846 /* search after i_idxposc */
1847 if( AVI_SetStreamChunk( p_input,
1848 i_stream, p_stream->i_idxposc + 1 ) )
1850 msg_Warn( p_input, "cannot seek" );
1854 i_date = AVI_GetPTS( p_stream );
1855 /* TODO better support for i_samplesize != 0 */
1856 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
1859 #define p_stream p_avi->pp_info[i_stream]
1861 /* seek for chunk based streams */
1862 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1864 if( p_stream->b_activated && !p_stream->i_samplesize )
1865 // if( p_stream->b_activated )
1867 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1868 p_avi->i_time = __MAX( AVI_GetPTS( p_stream ),
1875 i_date = p_avi->i_time;
1877 /* seek for bytes based streams */
1878 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1880 if( p_stream->b_activated && p_stream->i_samplesize )
1882 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1883 // p_avi->i_time = __MAX( AVI_GetPTS( p_stream ), p_avi->i_time );
1886 msg_Dbg( p_input, "seek: "I64Fd" secondes", p_avi->i_time /1000000 );
1887 /* set true movie time */
1889 if( !p_avi->i_time )
1891 p_avi->i_time = i_date;
1898 msg_Err( p_input, "shouldn't yet be executed" );
1904 static pes_packet_t *PES_split( input_thread_t *p_input, avi_stream_t *p_stream, pes_packet_t *p_pes )
1906 pes_packet_t *p_pes2;
1907 data_packet_t *p_data;
1910 if( p_pes->i_nb_data < 2 )
1914 p_pes2 = input_NewPES( p_input->p_method_data );
1915 p_pes2->i_pts = p_pes->i_pts;
1916 p_pes2->i_dts = p_pes->i_dts;
1917 p_pes2->i_nb_data = p_pes->i_nb_data/2;
1918 p_pes2->i_pes_size = 0;
1919 for( i_nb_data = 0, p_data = p_pes->p_first;
1920 i_nb_data < p_pes2->i_nb_data;
1921 i_nb_data++, p_data = p_data->p_next )
1923 p_pes2->i_pes_size +=
1924 p_data->p_payload_end - p_data->p_payload_start;
1925 p_pes2->p_last = p_data;
1927 p_pes2->p_first = p_pes->p_first;
1928 p_pes2->p_last->p_next = NULL;
1930 p_pes->p_first = p_data;
1931 p_pes->i_pes_size -= p_pes2->i_pes_size;
1932 p_pes->i_nb_data -= p_pes2->i_nb_data;
1933 // p_pes->i_pts += AVI_GetDPTS( p_stream, p_pes2->i_pes_size );
1934 // p_pes->i_dts += AVI_GetDPTS( p_stream, p_pes2->i_pes_size );
1941 /*****************************************************************************
1942 * AVIDemux_Seekable: reads and demuxes data packets for stream seekable
1943 *****************************************************************************
1944 * AVIDemux: reads and demuxes data packets
1945 *****************************************************************************
1946 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1947 *****************************************************************************/
1948 typedef struct avi_stream_toread_s
1954 off_t i_posf; // where we will read :
1955 // if i_idxposb == 0 : begining of chunk (+8 to acces data)
1956 // else : point on data directly
1957 } avi_stream_toread_t;
1959 static int AVIDemux_Seekable( input_thread_t *p_input )
1961 unsigned int i_stream_count;
1962 unsigned int i_stream;
1963 vlc_bool_t b_stream;
1964 vlc_bool_t b_play_audio;
1965 vlc_bool_t b_video; /* is there some video track selected */
1966 // cannot be more than 100 stream (dcXX or wbXX)
1967 avi_stream_toread_t toread[100];
1969 demux_sys_t *p_avi = p_input->p_demux_data;
1972 /* detect new selected/unselected streams */
1973 for( i_stream = 0,i_stream_count= 0, b_video = VLC_FALSE;
1974 i_stream < p_avi->i_streams; i_stream++ )
1976 #define p_stream p_avi->pp_info[i_stream]
1977 if( p_stream->p_es )
1979 if( p_stream->p_es->p_decoder_fifo &&
1980 !p_stream->b_activated )
1982 AVI_StreamStart( p_input, p_avi, i_stream );
1985 if( !p_stream->p_es->p_decoder_fifo &&
1986 p_stream->b_activated )
1988 AVI_StreamStop( p_input, p_avi, i_stream );
1991 if( p_stream->b_activated )
1994 if( p_stream->i_cat == VIDEO_ES )
2002 if( i_stream_count <= 0 )
2004 msg_Warn( p_input, "no track selected, exiting..." );
2008 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
2012 /* first wait for empty buffer, arbitrary time FIXME */
2013 //msleep( DEFAULT_PTS_DELAY );
2015 i_date = (mtime_t)1000000 *
2016 (mtime_t)p_avi->i_length *
2017 (mtime_t)AVI_TellAbsolute( p_input ) /
2018 (mtime_t)p_input->stream.p_selected_area->i_size;
2019 i_percent = 100 * AVI_TellAbsolute( p_input ) /
2020 p_input->stream.p_selected_area->i_size;
2022 // input_ClockInit( p_input->stream.p_selected_program );
2023 AVISeek( p_input, i_date, i_percent);
2025 #ifdef __AVI_SUBTITLE__
2028 subtitle_Seek( p_avi->p_sub, p_avi->i_time );
2034 /* wait for the good time */
2036 p_avi->i_pcr = p_avi->i_time * 9 / 100;
2038 input_ClockManageRef( p_input,
2039 p_input->stream.p_selected_program,
2043 p_avi->i_time += 25*1000; /* read 25ms */
2045 #ifdef __AVI_SUBTITLE__
2048 subtitle_Demux( p_avi->p_sub, p_avi->i_time );
2052 /* *** send audio data to decoder if rate == DEFAULT_RATE or no video *** */
2053 vlc_mutex_lock( &p_input->stream.stream_lock );
2054 if( p_input->stream.control.i_rate == DEFAULT_RATE || !b_video )
2056 b_play_audio = VLC_TRUE;
2060 b_play_audio = VLC_FALSE;
2062 vlc_mutex_unlock( &p_input->stream.stream_lock );
2065 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
2067 #define p_stream p_avi->pp_info[i_stream]
2070 toread[i_stream].b_ok = p_stream->b_activated;
2071 if( p_stream->i_idxposc < p_stream->i_idxnb )
2073 toread[i_stream].i_posf =
2074 p_stream->p_index[p_stream->i_idxposc].i_pos;
2075 if( p_stream->i_idxposb > 0 )
2077 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
2082 toread[i_stream].i_posf = -1;
2085 i_dpts = p_avi->i_time - AVI_GetPTS( p_stream );
2087 if( p_stream->i_samplesize )
2089 toread[i_stream].i_toread = AVI_PTSToByte( p_stream,
2094 toread[i_stream].i_toread = AVI_PTSToChunk( p_stream,
2100 toread[i_stream].i_toread *= -1;
2105 b_stream = VLC_FALSE;
2109 #define p_stream p_avi->pp_info[i_stream]
2111 pes_packet_t *p_pes;
2116 /* search for first chunk to be read */
2117 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_avi->i_streams; i++ )
2119 if( !toread[i].b_ok ||
2120 AVI_GetDPTS( p_avi->pp_info[i],
2121 toread[i].i_toread ) <= -25 * 1000 )
2126 if( toread[i].i_toread > 0 )
2128 b_done = VLC_FALSE; // not yet finished
2131 if( toread[i].i_posf > 0 )
2133 if( i_pos == -1 || i_pos > toread[i_stream].i_posf )
2136 i_pos = toread[i].i_posf;
2143 // return( b_stream ? 1 : 0 );
2149 /* no valid index, we will parse directly the stream
2150 * in case we fail we will disable all finished stream */
2151 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin + 12 )
2153 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
2154 if( AVI_PacketNext( p_input ) )
2156 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2161 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin + 12 );
2166 avi_packet_t avi_pk;
2168 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
2171 "cannot get packet header, track disabled" );
2172 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2174 if( avi_pk.i_stream >= p_avi->i_streams ||
2175 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
2177 if( AVI_PacketNext( p_input ) )
2180 "cannot skip packet, track disabled" );
2181 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2187 /* add this chunk to the index */
2188 AVIIndexEntry_t index;
2190 index.i_id = avi_pk.i_fourcc;
2192 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
2194 index.i_pos = avi_pk.i_pos;
2195 index.i_length = avi_pk.i_size;
2196 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
2198 i_stream = avi_pk.i_stream;
2199 /* do we will read this data ? */
2200 if( AVI_GetDPTS( p_stream,
2201 toread[i_stream].i_toread ) > -25 * 1000 )
2207 if( AVI_PacketNext( p_input ) )
2210 "cannot skip packet, track disabled" );
2211 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2220 AVI_SeekAbsolute( p_input, i_pos );
2223 /* read thoses data */
2224 if( p_stream->i_samplesize )
2226 unsigned int i_toread;
2228 if( ( i_toread = toread[i_stream].i_toread ) <= 0 )
2230 if( p_stream->i_samplesize > 1 )
2232 i_toread = p_stream->i_samplesize;
2236 i_toread = __MAX( AVI_PTSToByte( p_stream, 20 * 1000 ), 100 );
2239 i_size = __MIN( p_stream->p_index[p_stream->i_idxposc].i_length -
2240 p_stream->i_idxposb,
2245 i_size = p_stream->p_index[p_stream->i_idxposc].i_length;
2248 if( p_stream->i_idxposb == 0 )
2250 i_size += 8; // need to read and skip header
2253 if( input_ReadInPES( p_input, &p_pes, __EVEN( i_size ) ) < 0 )
2255 msg_Warn( p_input, "failled reading data" );
2256 AVI_StreamStop( p_input, p_avi, i_stream );
2257 toread[i_stream].b_ok = VLC_FALSE;
2261 if( i_size % 2 ) // read was padded on word boundary
2263 p_pes->p_last->p_payload_end--;
2264 p_pes->i_pes_size--;
2267 if( p_stream->i_idxposb == 0 )
2269 p_pes->p_first->p_payload_start += 8;
2270 p_pes->i_pes_size -= 8;
2273 p_pes->i_pts = AVI_GetPTS( p_stream );
2276 /* fix pts for audio: ie pts sould be for the first byte of the first frame */
2277 if( p_stream->i_samplesize == 1 )
2279 AVI_FixPTS( p_stream, p_pes );
2284 if( p_stream->i_samplesize )
2286 if( p_stream->i_idxposb == 0 )
2290 toread[i_stream].i_toread -= i_size;
2291 p_stream->i_idxposb += i_size;
2292 if( p_stream->i_idxposb >=
2293 p_stream->p_index[p_stream->i_idxposc].i_length )
2295 p_stream->i_idxposb = 0;
2296 p_stream->i_idxposc++;
2301 toread[i_stream].i_toread--;
2302 p_stream->i_idxposc++;
2305 if( p_stream->i_idxposc < p_stream->i_idxnb)
2307 toread[i_stream].i_posf =
2308 p_stream->p_index[p_stream->i_idxposc].i_pos;
2309 if( p_stream->i_idxposb > 0 )
2311 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
2317 toread[i_stream].i_posf = -1;
2320 b_stream = VLC_TRUE; // at least one read succeed
2322 if( p_stream->p_es && p_stream->p_es->p_decoder_fifo &&
2323 ( b_play_audio || p_stream->i_cat != AUDIO_ES ) )
2327 input_ClockGetTS( p_input,
2328 p_input->stream.p_selected_program,
2329 p_pes->i_pts * 9/100);
2331 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
2335 input_DeletePES( p_input->p_method_data, p_pes );
2341 /*****************************************************************************
2342 * AVIDemux_UnSeekable: reads and demuxes data packets for unseekable file
2343 *****************************************************************************
2344 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2345 *****************************************************************************/
2346 static int AVIDemux_UnSeekable( input_thread_t *p_input )
2348 demux_sys_t *p_avi = p_input->p_demux_data;
2349 avi_stream_t *p_stream_master;
2351 unsigned int i_stream;
2352 unsigned int i_packet;
2354 /* *** send audio data to decoder only if rate == DEFAULT_RATE *** */
2355 vlc_mutex_lock( &p_input->stream.stream_lock );
2356 b_audio = p_input->stream.control.i_rate == DEFAULT_RATE;
2357 vlc_mutex_unlock( &p_input->stream.stream_lock );
2359 input_ClockManageRef( p_input,
2360 p_input->stream.p_selected_program,
2362 /* *** find master stream for data packet skipping algo *** */
2363 /* *** -> first video, if any, or first audio ES *** */
2364 for( i_stream = 0, p_stream_master = NULL;
2365 i_stream < p_avi->i_streams; i_stream++ )
2367 #define p_stream p_avi->pp_info[i_stream]
2368 if( p_stream->p_es &&
2369 p_stream->p_es->p_decoder_fifo )
2371 if( p_stream->i_cat == VIDEO_ES )
2373 p_stream_master = p_stream;
2376 if( p_stream->i_cat == AUDIO_ES && !p_stream_master )
2378 p_stream_master = p_stream;
2383 if( !p_stream_master )
2385 msg_Warn( p_input, "no more stream selected" );
2389 p_avi->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
2391 for( i_packet = 0; i_packet < 10; i_packet++)
2393 #define p_stream p_avi->pp_info[avi_pk.i_stream]
2395 avi_packet_t avi_pk;
2397 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
2401 // AVI_ParseStreamHeader( avi_pk.i_fourcc, &i_stream, &i_cat );
2403 if( avi_pk.i_stream >= p_avi->i_streams ||
2404 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
2406 /* we haven't found an audio or video packet:
2407 * - we have seek, found first next packet
2408 * - others packets could be found, skip them
2410 switch( avi_pk.i_fourcc )
2412 case AVIFOURCC_JUNK:
2413 case AVIFOURCC_LIST:
2414 case AVIFOURCC_RIFF:
2415 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
2416 case AVIFOURCC_idx1:
2419 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
2424 "seems to have lost position, resync" );
2425 if( AVI_PacketSearch( p_input ) )
2427 msg_Err( p_input, "resync failed" );
2434 /* do will send this packet to decoder ? */
2435 if( ( !b_audio && avi_pk.i_cat == AUDIO_ES )||
2437 !p_stream->p_es->p_decoder_fifo )
2439 if( AVI_PacketNext( p_input ) )
2446 /* it's a selected stream, check for time */
2447 if( __ABS( AVI_GetPTS( p_stream ) -
2448 AVI_GetPTS( p_stream_master ) )< 600*1000 )
2450 /* load it and send to decoder */
2451 pes_packet_t *p_pes;
2452 if( AVI_PacketRead( p_input, &avi_pk, &p_pes ) || !p_pes)
2458 input_ClockGetTS( p_input,
2459 p_input->stream.p_selected_program,
2460 AVI_GetPTS( p_stream ) * 9/100);
2461 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
2465 if( AVI_PacketNext( p_input ) )
2472 /* *** update stream time position *** */
2473 if( p_stream->i_samplesize )
2475 p_stream->i_idxposb += avi_pk.i_size;
2479 p_stream->i_idxposc++;