1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: avi.c,v 1.54 2003/08/17 23:02:52 fenrir Exp $
6 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
27 #include <string.h> /* strdup() */
29 #include <sys/types.h>
32 #include <vlc/input.h>
34 #include "vlc_video.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 static vlc_fourcc_t GetFOURCC( byte_t *p_buff )
75 return VLC_FOURCC( p_buff[0], p_buff[1], p_buff[2], p_buff[3] );
78 static inline off_t __EVEN( off_t i )
80 return (i & 1) ? i + 1 : i;
83 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
85 /* read data in a pes */
86 static int input_ReadInPES( input_thread_t *p_input,
87 pes_packet_t **pp_pes,
91 data_packet_t *p_data;
94 if( !(p_pes = input_NewPES( p_input->p_method_data ) ) )
106 input_NewPacket( p_input->p_method_data, 0 );
107 p_pes->i_nb_data = 1;
108 p_pes->i_pes_size = 0;
112 p_pes->i_nb_data = 0;
113 p_pes->i_pes_size = 0;
115 while( p_pes->i_pes_size < i_size )
119 i_read = input_SplitBuffer(p_input,
122 p_pes->i_pes_size, 2048 ) );
125 /* should occur only with EOF and max allocation reached
126 * it safer to return an error */
128 input_DeletePES( p_input->p_method_data, p_pes );
132 if( !p_pes->p_first )
134 p_pes->p_first = p_data;
138 p_pes->p_last->p_next = p_data;
140 p_pes->p_last = p_data;
142 p_pes->i_pes_size += i_read;
146 return p_pes->i_pes_size;
149 /* check if the file is interleaved */
150 static vlc_bool_t AVI_Interleaved( input_thread_t *p_input );
152 /* Test if it seems that it's a key frame */
153 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
159 * startcode: 0x00000100 32bits
160 * framenumber ? 5bits
161 * piture type 0(I),1(P) 2bits
163 if( GetDWBE( p_byte ) != 0x00000100 )
165 /* it's not an msmpegv1 stream, strange...*/
166 return AVIIF_KEYFRAME;
170 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
173 case FOURCC_DIV3: // wmv1 also
175 * picture type 0(I),1(P) 2bits
177 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
179 /* we should find first occurence of 0x000001b6 (32bits)
180 * startcode: 0x000001b6 32bits
181 * piture type 0(I),1(P) 2bits
183 if( GetDWBE( p_byte ) != 0x000001b6 )
185 /* not true , need to find the first VOP header */
186 return AVIIF_KEYFRAME;
190 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
193 /* I can't do it, so say yes */
194 return AVIIF_KEYFRAME;
198 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
205 case WAVE_FORMAT_PCM:
206 return VLC_FOURCC( 'a', 'r', 'a', 'w' );
207 case WAVE_FORMAT_MPEG:
208 case WAVE_FORMAT_MPEGLAYER3:
209 return VLC_FOURCC( 'm', 'p', 'g', 'a' );
210 case WAVE_FORMAT_A52:
211 return VLC_FOURCC( 'a', '5', '2', ' ' );
212 case WAVE_FORMAT_WMA1:
213 return VLC_FOURCC( 'w', 'm', 'a', '1' );
214 case WAVE_FORMAT_WMA2:
215 return VLC_FOURCC( 'w', 'm', 'a', '2' );
217 return VLC_FOURCC( 'm', 's',
218 ( i_codec >> 8 )&0xff, i_codec&0xff );
221 // XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4
271 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
275 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
276 int *pi_number, int *pi_type )
278 #define SET_PTR( p, v ) if( p ) *(p) = (v);
281 c1 = ((uint8_t *)&i_id)[0];
282 c2 = ((uint8_t *)&i_id)[1];
284 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
286 SET_PTR( pi_number, 100 ); /* > max stream number */
287 SET_PTR( pi_type, UNKNOWN_ES );
291 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
292 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
295 SET_PTR( pi_type, AUDIO_ES );
299 SET_PTR( pi_type, VIDEO_ES );
302 SET_PTR( pi_type, UNKNOWN_ES );
309 static int AVI_PacketGetHeader( input_thread_t *p_input, avi_packet_t *p_pk )
313 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
317 p_pk->i_fourcc = GetFOURCC( p_peek );
318 p_pk->i_size = GetDWLE( p_peek + 4 );
319 p_pk->i_pos = AVI_TellAbsolute( p_input );
320 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
322 p_pk->i_type = GetFOURCC( p_peek + 8 );
329 memcpy( p_pk->i_peek, p_peek + 8, 8 );
331 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
335 static int AVI_PacketNext( input_thread_t *p_input )
339 if( AVI_PacketGetHeader( p_input, &avi_ck ) )
343 //msg_Dbg( p_input, "skip %4.4s:%4.4s", (char*)&avi_ck.i_fourcc, (char*)&avi_ck.i_type );
345 if( avi_ck.i_fourcc == AVIFOURCC_LIST && ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
347 return AVI_SkipBytes( p_input, 12 );
349 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF && avi_ck.i_type == AVIFOURCC_AVIX )
351 return AVI_SkipBytes( p_input, 24 );
355 return AVI_SkipBytes( p_input, __EVEN( avi_ck.i_size ) + 8 );
358 static int AVI_PacketRead( input_thread_t *p_input,
360 pes_packet_t **pp_pes )
365 i_size = __EVEN( p_pk->i_size + 8 );
366 b_pad = ( i_size != p_pk->i_size + 8 );
368 if( input_ReadInPES( p_input, pp_pes, i_size ) != (ssize_t)i_size )
372 (*pp_pes)->p_first->p_payload_start += 8;
373 (*pp_pes)->i_pes_size -= 8;
377 (*pp_pes)->p_last->p_payload_end--;
378 (*pp_pes)->i_pes_size--;
384 static int AVI_PacketSearch( input_thread_t *p_input )
386 demux_sys_t *p_avi = p_input->p_demux_data;
391 if( AVI_SkipBytes( p_input, 1 ) )
395 AVI_PacketGetHeader( p_input, &avi_pk );
396 if( avi_pk.i_stream < p_avi->i_streams &&
397 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
401 switch( avi_pk.i_fourcc )
413 static void __AVI_AddEntryIndex( avi_stream_t *p_info,
414 AVIIndexEntry_t *p_index)
416 if( p_info->p_index == NULL )
418 p_info->i_idxmax = 16384;
420 if( !( p_info->p_index = calloc( p_info->i_idxmax,
421 sizeof( AVIIndexEntry_t ) ) ) )
426 if( p_info->i_idxnb >= p_info->i_idxmax )
428 p_info->i_idxmax += 16384;
429 if( !( p_info->p_index = realloc( (void*)p_info->p_index,
431 sizeof( AVIIndexEntry_t ) ) ) )
436 /* calculate cumulate length */
437 if( p_info->i_idxnb > 0 )
439 p_index->i_lengthtotal =
440 p_info->p_index[p_info->i_idxnb - 1].i_length +
441 p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal;
445 p_index->i_lengthtotal = 0;
448 p_info->p_index[p_info->i_idxnb] = *p_index;
453 static void AVI_IndexAddEntry( demux_sys_t *p_avi,
455 AVIIndexEntry_t *p_index)
457 __AVI_AddEntryIndex( p_avi->pp_info[i_stream],
459 if( p_avi->i_movi_lastchunk_pos < p_index->i_pos )
461 p_avi->i_movi_lastchunk_pos = p_index->i_pos;
465 static int AVI_IndexLoad_idx1( input_thread_t *p_input )
467 demux_sys_t *p_avi = p_input->p_demux_data;
469 avi_chunk_list_t *p_riff;
470 avi_chunk_list_t *p_movi;
471 avi_chunk_idx1_t *p_idx1;
473 unsigned int i_stream;
474 unsigned int i_index;
477 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
480 p_idx1 = (avi_chunk_idx1_t*)AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
481 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
485 msg_Warn( p_input, "cannot find idx1 chunk, no index defined" );
489 /* *** calculate offset *** */
490 if( p_idx1->i_entry_count > 0 &&
491 p_idx1->entry[0].i_pos < p_movi->i_chunk_pos )
493 i_offset = p_movi->i_chunk_pos + 8;
500 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
504 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
507 if( i_stream < p_avi->i_streams &&
508 i_cat == p_avi->pp_info[i_stream]->i_cat )
510 AVIIndexEntry_t index;
511 index.i_id = p_idx1->entry[i_index].i_fourcc;
513 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
514 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
515 index.i_length = p_idx1->entry[i_index].i_length;
516 AVI_IndexAddEntry( p_avi, i_stream, &index );
522 static void __Parse_indx( input_thread_t *p_input,
524 avi_chunk_indx_t *p_indx )
526 demux_sys_t *p_avi = p_input->p_demux_data;
527 AVIIndexEntry_t index;
530 msg_Dbg( p_input, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
531 if( p_indx->i_indexsubtype == 0 )
533 for( i = 0; i < p_indx->i_entriesinuse; i++ )
535 index.i_id = p_indx->i_id;
536 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
537 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
538 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
540 AVI_IndexAddEntry( p_avi, i_stream, &index );
543 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
545 for( i = 0; i < p_indx->i_entriesinuse; i++ )
547 index.i_id = p_indx->i_id;
548 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
549 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
550 index.i_length = p_indx->idx.field[i].i_size;
552 AVI_IndexAddEntry( p_avi, i_stream, &index );
557 msg_Warn( p_input, "unknow subtype index(0x%x)", p_indx->i_indexsubtype );
561 static void AVI_IndexLoad_indx( input_thread_t *p_input )
563 demux_sys_t *p_avi = p_input->p_demux_data;
564 unsigned int i_stream;
567 avi_chunk_list_t *p_riff;
568 avi_chunk_list_t *p_hdrl;
570 p_riff = (void*)AVI_ChunkFind( &p_avi->ck_root,
572 p_hdrl = (void*)AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
574 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
576 avi_chunk_list_t *p_strl;
577 avi_chunk_indx_t *p_indx;
579 #define p_stream p_avi->pp_info[i_stream]
580 p_strl = (void*)AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
581 p_indx = (void*)AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
585 msg_Warn( p_input, "cannot find indx (misdetect/broken OpenDML file?)" );
589 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
591 __Parse_indx( p_input, i_stream, p_indx );
593 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
595 avi_chunk_indx_t ck_sub;
596 for( i = 0; i < p_indx->i_entriesinuse; i++ )
598 if( AVI_SeekAbsolute( p_input, p_indx->idx.super[i].i_offset ) )
603 if( AVI_ChunkRead( p_input, &ck_sub, NULL, p_avi->b_seekable ) )
611 msg_Warn( p_input, "unknow type index(0x%x)", p_indx->i_indextype );
617 static void AVI_IndexLoad( input_thread_t *p_input )
619 demux_sys_t *p_avi = p_input->p_demux_data;
620 unsigned int i_stream;
622 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
624 p_avi->pp_info[i_stream]->i_idxnb = 0;
625 p_avi->pp_info[i_stream]->i_idxmax = 0;
626 p_avi->pp_info[i_stream]->p_index = NULL;
631 AVI_IndexLoad_indx( p_input );
635 if( AVI_IndexLoad_idx1( p_input ) )
637 /* try indx if idx1 failed as some "normal" file have indx too */
638 AVI_IndexLoad_indx( p_input );
642 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
645 "stream[%d] created %d index entries",
647 p_avi->pp_info[i_stream]->i_idxnb );
651 static void AVI_IndexCreate( input_thread_t *p_input )
653 demux_sys_t *p_avi = p_input->p_demux_data;
655 avi_chunk_list_t *p_riff;
656 avi_chunk_list_t *p_movi;
658 unsigned int i_stream;
661 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
663 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
667 msg_Err( p_input, "cannot find p_movi" );
671 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
673 p_avi->pp_info[i_stream]->i_idxnb = 0;
674 p_avi->pp_info[i_stream]->i_idxmax = 0;
675 p_avi->pp_info[i_stream]->p_index = NULL;
677 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
678 p_input->stream.p_selected_area->i_size );
680 AVI_SeekAbsolute( p_input, p_movi->i_chunk_pos + 12);
681 msg_Warn( p_input, "creating index from LIST-movi, will take time !" );
686 if( AVI_PacketGetHeader( p_input, &pk ) )
690 if( pk.i_stream < p_avi->i_streams &&
691 pk.i_cat == p_avi->pp_info[pk.i_stream]->i_cat )
693 AVIIndexEntry_t index;
694 index.i_id = pk.i_fourcc;
696 AVI_GetKeyFlag(p_avi->pp_info[pk.i_stream]->i_codec, pk.i_peek);
697 index.i_pos = pk.i_pos;
698 index.i_length = pk.i_size;
699 AVI_IndexAddEntry( p_avi, pk.i_stream, &index );
703 switch( pk.i_fourcc )
708 avi_chunk_list_t *p_avix;
709 p_avix = (void*)AVI_ChunkFind( &p_avi->ck_root,
712 msg_Dbg( p_input, "looking for new RIFF chunk" );
713 if( AVI_SeekAbsolute( p_input, p_avix->i_chunk_pos + 24) )
721 msg_Dbg( p_input, "new RIFF chunk found" );
726 msg_Warn( p_input, "need resync, probably broken avi" );
727 if( AVI_PacketSearch( p_input ) )
729 msg_Warn( p_input, "lost sync, abord index creation" );
735 if( ( !p_avi->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
736 AVI_PacketNext( p_input ) )
743 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
746 "stream[%d] creating %d index entries",
748 p_avi->pp_info[i_stream]->i_idxnb );
753 /*****************************************************************************
755 *****************************************************************************/
756 static vlc_bool_t AVI_StreamStart ( input_thread_t *, demux_sys_t *, int );
757 static int AVI_StreamSeek ( input_thread_t *, demux_sys_t *, int, mtime_t );
758 static void AVI_StreamStop ( input_thread_t *, demux_sys_t *, int );
759 static int AVI_StreamStopFinishedStreams( input_thread_t *, demux_sys_t * );
761 static vlc_bool_t AVI_StreamStart( input_thread_t *p_input,
762 demux_sys_t *p_avi, int i_stream )
764 #define p_stream p_avi->pp_info[i_stream]
765 if( !p_stream->p_es )
767 msg_Warn( p_input, "stream[%d] unselectable", i_stream );
770 if( p_stream->b_activated )
772 msg_Warn( p_input, "stream[%d] already selected", i_stream );
776 if( !p_stream->p_es->p_decoder_fifo )
778 vlc_mutex_lock( &p_input->stream.stream_lock );
779 input_SelectES( p_input, p_stream->p_es );
780 vlc_mutex_unlock( &p_input->stream.stream_lock );
782 p_stream->b_activated = p_stream->p_es->p_decoder_fifo ? VLC_TRUE
784 if( p_stream->b_activated && p_avi->b_seekable)
786 AVI_StreamSeek( p_input, p_avi, i_stream, p_avi->i_time );
789 return p_stream->b_activated;
793 static void AVI_StreamStop( input_thread_t *p_input,
794 demux_sys_t *p_avi, int i_stream )
796 #define p_stream p_avi->pp_info[i_stream]
798 if( !p_stream->b_activated )
800 msg_Warn( p_input, "stream[%d] already unselected", i_stream );
804 if( p_stream->p_es->p_decoder_fifo )
806 vlc_mutex_lock( &p_input->stream.stream_lock );
807 input_UnselectES( p_input, p_stream->p_es );
808 vlc_mutex_unlock( &p_input->stream.stream_lock );
812 p_stream->b_activated = VLC_FALSE;
817 static int AVI_StreamStopFinishedStreams( input_thread_t *p_input,
820 unsigned int i_stream;
823 for( i_stream = 0,b_end = VLC_TRUE;
824 i_stream < p_avi->i_streams; i_stream++ )
826 #define p_stream p_avi->pp_info[i_stream]
827 if( p_stream->i_idxposc >= p_stream->i_idxnb )
829 AVI_StreamStop( p_input, p_avi, i_stream );
839 /****************************************************************************
840 * AVI_MovieGetLength give max streams length in second
841 ****************************************************************************/
842 static mtime_t AVI_MovieGetLength( input_thread_t *p_input, demux_sys_t *p_avi )
844 unsigned int i_stream;
848 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
850 #define p_stream p_avi->pp_info[i_stream]
852 /* fix length for each stream */
853 if( p_stream->i_idxnb < 1 || !p_stream->p_index )
858 if( p_stream->i_samplesize )
861 (mtime_t)( p_stream->p_index[p_stream->i_idxnb-1].i_lengthtotal +
862 p_stream->p_index[p_stream->i_idxnb-1].i_length ) *
863 (mtime_t)p_stream->i_scale /
864 (mtime_t)p_stream->i_rate /
865 (mtime_t)p_stream->i_samplesize;
869 i_length = (mtime_t)p_stream->i_idxnb *
870 (mtime_t)p_stream->i_scale /
871 (mtime_t)p_stream->i_rate;
875 "stream[%d] length:"I64Fd" (based on index)",
878 i_maxlength = __MAX( i_maxlength, i_length );
885 /*****************************************************************************
886 * AVIEnd: frees unused data
887 *****************************************************************************/
888 static void __AVIEnd ( vlc_object_t * p_this )
890 input_thread_t * p_input = (input_thread_t *)p_this;
892 demux_sys_t *p_avi = p_input->p_demux_data ;
896 for( i = 0; i < p_avi->i_streams; i++ )
898 if( p_avi->pp_info[i] )
900 if( p_avi->pp_info[i]->p_index )
902 free( p_avi->pp_info[i]->p_index );
904 free( p_avi->pp_info[i] );
907 free( p_avi->pp_info );
909 #ifdef __AVI_SUBTITLE__
912 subtitle_Close( p_avi->p_sub );
916 AVI_ChunkFreeRoot( p_input, &p_avi->ck_root );
918 FREE( p_input->p_demux_data );
921 /*****************************************************************************
922 * AVIInit: check file and initializes AVI structures
923 *****************************************************************************/
924 static int AVIInit( vlc_object_t * p_this )
926 input_thread_t * p_input = (input_thread_t *)p_this;
928 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
929 avi_chunk_list_t *p_hdrl, *p_movi;
931 avi_chunk_list_t *p_INFO;
932 avi_chunk_strz_t *p_name;
934 avi_chunk_avih_t *p_avih;
936 es_descriptor_t *p_es = NULL; /* avoid warning */
938 #ifdef __AVI_SUBTITLE__
939 mtime_t i_microsecperframe = 0; // for some subtitle format
942 vlc_bool_t b_stream_audio, b_stream_video;
944 p_input->pf_demux = AVIDemux_Seekable;
945 if( AVI_TestFile( p_input ) )
947 msg_Warn( p_input, "avi module discarded (invalid header)" );
951 /* Initialize access plug-in structures. */
952 if( p_input->i_mtu == 0 )
955 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
958 if( !( p_input->p_demux_data =
959 p_avi = malloc( sizeof(demux_sys_t) ) ) )
961 msg_Err( p_input, "out of memory" );
964 memset( p_avi, 0, sizeof( demux_sys_t ) );
967 p_avi->b_seekable = ( ( p_input->stream.b_seekable )
968 &&( p_input->stream.i_method == INPUT_METHOD_FILE ) );
969 p_avi->i_movi_lastchunk_pos = 0;
970 p_avi->b_odml = VLC_FALSE;
971 p_avi->b_interleaved = VLC_FALSE;
973 /* *** for unseekable stream, automaticaly use AVIDemux_interleaved *** */
974 if( !p_avi->b_seekable || config_GetInt( p_input, "avi-interleaved" ) )
976 p_input->pf_demux = AVIDemux_UnSeekable;
979 if( AVI_ChunkReadRoot( p_input, &p_avi->ck_root, p_avi->b_seekable ) )
981 msg_Err( p_input, "avi module discarded (invalid file)" );
984 AVI_ChunkDumpDebug( p_input, &p_avi->ck_root );
986 if( AVI_ChunkCount( &p_avi->ck_root, AVIFOURCC_RIFF ) > 1 )
988 int i_count = AVI_ChunkCount( &p_avi->ck_root, AVIFOURCC_RIFF );
991 msg_Warn( p_input, "multiple riff -> OpenDML ?" );
992 for( i = 1; i < i_count; i++ )
994 avi_chunk_list_t *p_avix;
996 p_avix = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
998 if( p_avix->i_type == AVIFOURCC_AVIX )
1000 msg_Warn( p_input, "detected OpenDML file" );
1002 p_avi->b_odml = VLC_TRUE;
1006 p_avi->b_odml = VLC_TRUE;
1010 p_riff = (avi_chunk_list_t*)AVI_ChunkFind( &p_avi->ck_root,
1011 AVIFOURCC_RIFF, 0 );
1012 p_hdrl = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
1013 AVIFOURCC_hdrl, 0 );
1014 p_movi = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
1015 AVIFOURCC_movi, 0 );
1017 p_INFO = (avi_chunk_list_t*)AVI_ChunkFind( p_riff,
1018 AVIFOURCC_INFO, 0 );
1019 p_name = (avi_chunk_strz_t*)AVI_ChunkFind( p_INFO,
1020 AVIFOURCC_INAM, 0 );
1027 if( !p_hdrl || !p_movi )
1029 msg_Err( p_input, "avi module discarded (invalid file)" );
1030 return VLC_EGENERIC;
1033 if( !( p_avih = (avi_chunk_avih_t*)AVI_ChunkFind( p_hdrl,
1034 AVIFOURCC_avih, 0 ) ) )
1036 msg_Err( p_input, "cannot find avih chunk" );
1037 return VLC_EGENERIC;
1039 p_avi->i_streams = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
1040 if( p_avih->i_streams != p_avi->i_streams )
1043 "found %d stream but %d are declared",
1045 p_avih->i_streams );
1047 if( p_avi->i_streams == 0 )
1050 msg_Err( p_input, "no stream defined!" );
1051 return VLC_EGENERIC;
1054 /* create one program */
1055 vlc_mutex_lock( &p_input->stream.stream_lock );
1056 if( input_InitStream( p_input, 0 ) == -1)
1058 vlc_mutex_unlock( &p_input->stream.stream_lock );
1060 msg_Err( p_input, "cannot init stream" );
1061 return VLC_EGENERIC;
1063 if( input_AddProgram( p_input, 0, 0) == NULL )
1065 vlc_mutex_unlock( &p_input->stream.stream_lock );
1067 msg_Err( p_input, "cannot add program" );
1068 return VLC_EGENERIC;
1070 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1071 vlc_mutex_unlock( &p_input->stream.stream_lock );
1073 /* print informations on streams */
1074 msg_Dbg( p_input, "AVIH: %d stream, flags %s%s%s%s ",
1076 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
1077 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
1078 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
1079 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
1081 input_info_category_t *p_cat = input_InfoCategory( p_input, _("Avi") );
1082 input_AddInfo( p_cat, _("Number of Streams"), "%d", p_avi->i_streams );
1083 input_AddInfo( p_cat, _("Flags"), "%s%s%s%s",
1084 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
1085 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
1086 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
1087 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
1090 /* now read info on each stream and create ES */
1091 p_avi->pp_info = calloc( p_avi->i_streams,
1092 sizeof( avi_stream_t* ) );
1093 memset( p_avi->pp_info,
1095 sizeof( avi_stream_t* ) * p_avi->i_streams );
1097 for( i = 0 ; i < p_avi->i_streams; i++ )
1099 avi_chunk_list_t *p_avi_strl;
1100 avi_chunk_strh_t *p_avi_strh;
1101 avi_chunk_strf_auds_t *p_avi_strf_auds;
1102 avi_chunk_strf_vids_t *p_avi_strf_vids;
1105 #define p_info p_avi->pp_info[i]
1106 p_info = malloc( sizeof(avi_stream_t ) );
1107 memset( p_info, 0, sizeof( avi_stream_t ) );
1109 p_avi_strl = (avi_chunk_list_t*)AVI_ChunkFind( p_hdrl,
1110 AVIFOURCC_strl, i );
1111 p_avi_strh = (avi_chunk_strh_t*)AVI_ChunkFind( p_avi_strl,
1112 AVIFOURCC_strh, 0 );
1113 p_avi_strf_auds = (avi_chunk_strf_auds_t*)
1114 p_avi_strf_vids = (avi_chunk_strf_vids_t*)
1115 AVI_ChunkFind( p_avi_strl, AVIFOURCC_strf, 0 );
1117 if( !p_avi_strl || !p_avi_strh ||
1118 ( !p_avi_strf_auds && !p_avi_strf_vids ) )
1120 msg_Warn( p_input, "stream[%d] incomlete", i );
1124 /* *** Init p_info *** */
1125 p_info->i_rate = p_avi_strh->i_rate;
1126 p_info->i_scale = p_avi_strh->i_scale;
1127 p_info->i_samplesize = p_avi_strh->i_samplesize;
1128 msg_Dbg( p_input, "stream[%d] rate:%d scale:%d samplesize:%d",
1130 p_info->i_rate, p_info->i_scale, p_info->i_samplesize );
1131 switch( p_avi_strh->i_type )
1133 case( AVIFOURCC_auds ):
1134 p_info->i_cat = AUDIO_ES;
1136 AVI_FourccGetCodec( AUDIO_ES,
1137 p_avi_strf_auds->p_wf->wFormatTag );
1138 p_info->i_codec = p_info->i_fourcc;
1139 i_init_size = p_avi_strf_auds->i_chunk_size;
1140 p_init_data = p_avi_strf_auds->p_wf;
1141 msg_Dbg( p_input, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
1143 p_avi_strf_auds->p_wf->wFormatTag,
1144 p_avi_strf_auds->p_wf->nChannels,
1145 p_avi_strf_auds->p_wf->nSamplesPerSec,
1146 p_avi_strf_auds->p_wf->wBitsPerSample );
1148 char psz_cat[ sizeof("Stream") + 10 ];
1149 input_info_category_t *p_cat;
1150 sprintf( psz_cat, _("Stream %d"), i );
1151 p_cat = input_InfoCategory( p_input, psz_cat );
1152 input_AddInfo( p_cat, _("Type"), "Audio(0x%x)",
1153 p_avi_strf_auds->p_wf->wFormatTag );
1154 input_AddInfo( p_cat, _("Codec"), "%4.4s",
1155 (const char*)&(p_info->i_codec) );
1156 input_AddInfo( p_cat, _("FOURCC"), "0x%x",
1158 input_AddInfo( p_cat, _("Channels"), "%d",
1159 p_avi_strf_auds->p_wf->nChannels );
1160 input_AddInfo( p_cat, _("Sample Rate"), "%d",
1161 p_avi_strf_auds->p_wf->nSamplesPerSec );
1162 if( p_avi_strf_auds->p_wf->wBitsPerSample > 0
1163 && p_info->i_scale != 0 )
1165 input_AddInfo( p_cat, _("Bits Per Sample"), "%d",
1166 p_avi_strf_auds->p_wf->wBitsPerSample );
1167 input_AddInfo( p_cat, _("Audio Bitrate"), "%d",
1168 p_info->i_samplesize * p_info->i_rate
1169 / p_info->i_scale );
1174 case( AVIFOURCC_vids ):
1175 p_info->i_cat = VIDEO_ES;
1176 /* XXX quick hack for playing ffmpeg video, I don't know
1177 who is doing something wrong */
1178 p_info->i_samplesize = 0;
1179 p_info->i_fourcc = p_avi_strf_vids->p_bih->biCompression;
1181 AVI_FourccGetCodec( VIDEO_ES, p_info->i_fourcc );
1182 i_init_size = p_avi_strf_vids->i_chunk_size;
1183 p_init_data = p_avi_strf_vids->p_bih;
1184 msg_Dbg( p_input, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
1186 (char*)&p_avi_strf_vids->p_bih->biCompression,
1187 p_avi_strf_vids->p_bih->biWidth,
1188 p_avi_strf_vids->p_bih->biHeight,
1189 p_avi_strf_vids->p_bih->biBitCount,
1190 (float)p_info->i_rate /
1191 (float)p_info->i_scale );
1193 char psz_cat[ sizeof("Stream") + 10 ];
1194 input_info_category_t *p_cat;
1195 sprintf( psz_cat, "Stream %d", i );
1196 p_cat = input_InfoCategory( p_input, psz_cat );
1197 input_AddInfo( p_cat, _("Type"), _("Video") );
1198 input_AddInfo( p_cat, _("Codec"), "%4.4s",
1199 (const char*)&(p_info->i_codec) );
1200 input_AddInfo( p_cat, _("FOURCC"), "0x%x",
1202 input_AddInfo( p_cat, _("Resolution"), "%dx%d",
1203 p_avi_strf_vids->p_bih->biWidth,
1204 p_avi_strf_vids->p_bih->biHeight );
1205 input_AddInfo( p_cat, _("Frame Rate"), "%f",
1206 (float)p_info->i_rate /
1207 (float)p_info->i_scale );
1208 input_AddInfo( p_cat, _("Bits Per Pixel"), "%d",
1209 p_avi_strf_vids->p_bih->biBitCount );
1211 #ifdef __AVI_SUBTITLE__
1212 if( i_microsecperframe == 0 )
1214 i_microsecperframe = (mtime_t)1000000 *
1215 (mtime_t)p_info->i_scale /
1216 (mtime_t)p_info->i_rate;
1221 msg_Warn( p_input, "stream[%d] unknown type", i );
1222 p_info->i_cat = UNKNOWN_ES;
1226 char psz_cat[32]; /* We'll clip i just in case */
1227 input_info_category_t *p_cat;
1228 sprintf( psz_cat, "Stream %d", __MIN( i, 100000 ) );
1229 p_cat = input_InfoCategory( p_input, psz_cat );
1230 input_AddInfo( p_cat, _("Type"), _("Unknown") );
1234 p_info->b_activated = VLC_FALSE;
1236 vlc_mutex_lock( &p_input->stream.stream_lock );
1238 p_es = input_AddES( p_input, p_input->stream.p_selected_program,
1239 1+i, p_info->i_cat, NULL, 0 );
1240 vlc_mutex_unlock( &p_input->stream.stream_lock );
1241 p_es->i_stream_id =i; /* XXX: i don't use it */
1242 p_es->i_fourcc = p_info->i_fourcc;
1243 if( p_es->i_cat == AUDIO_ES )
1245 if( i_init_size < sizeof( WAVEFORMATEX ) )
1247 i_init_size = sizeof( WAVEFORMATEX );
1249 p_es->p_waveformatex = malloc( i_init_size );
1250 memcpy( p_es->p_waveformatex, p_init_data, i_init_size );
1252 else if( p_es->i_cat == VIDEO_ES )
1254 p_es->p_bitmapinfoheader = malloc( i_init_size );
1255 memcpy( p_es->p_bitmapinfoheader, p_init_data, i_init_size );
1260 #ifdef __AVI_SUBTITLE__
1261 if( ( p_avi->p_sub = subtitle_New( p_input, NULL, i_microsecperframe ) ) )
1263 subtitle_Select( p_avi->p_sub );
1267 if( config_GetInt( p_input, "avi-index" ) )
1269 if( p_avi->b_seekable )
1271 AVI_IndexCreate( p_input );
1275 msg_Warn( p_input, "cannot create index (unseekable stream)" );
1276 AVI_IndexLoad( p_input );
1281 AVI_IndexLoad( p_input );
1284 /* *** movie length in sec *** */
1285 p_avi->i_length = AVI_MovieGetLength( p_input, p_avi );
1286 if( p_avi->i_length < (mtime_t)p_avih->i_totalframes *
1287 (mtime_t)p_avih->i_microsecperframe /
1290 msg_Warn( p_input, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
1293 /* fix some BeOS MediaKit generated file */
1294 for( i = 0 ; i < p_avi->i_streams; i++ )
1296 avi_chunk_list_t *p_avi_strl;
1297 avi_chunk_strh_t *p_avi_strh;
1298 avi_chunk_strf_auds_t *p_avi_strf_auds;
1299 #define p_stream p_avi->pp_info[i]
1301 if( p_stream->i_cat != AUDIO_ES )
1305 if( p_stream->i_idxnb < 1 ||
1306 p_stream->i_scale != 1 ||
1307 p_stream->i_samplesize != 0 )
1311 p_avi_strl = (avi_chunk_list_t*)AVI_ChunkFind( p_hdrl,
1312 AVIFOURCC_strl, i );
1313 p_avi_strh = (avi_chunk_strh_t*)AVI_ChunkFind( p_avi_strl,
1314 AVIFOURCC_strh, 0 );
1316 (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_avi_strl,
1317 AVIFOURCC_strf, 0 );
1319 if( p_avi_strf_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
1320 (unsigned int)p_stream->i_rate == p_avi_strf_auds->p_wf->nSamplesPerSec )
1322 int64_t i_track_length =
1323 p_stream->p_index[p_stream->i_idxnb-1].i_length +
1324 p_stream->p_index[p_stream->i_idxnb-1].i_lengthtotal;
1325 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
1326 (mtime_t)p_avih->i_microsecperframe;
1330 msg_Warn( p_input, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
1333 p_stream->i_samplesize = 1;
1334 p_stream->i_rate = i_track_length * (int64_t)1000000/ i_length;
1335 msg_Warn( p_input, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, p_stream->i_rate, p_stream->i_scale );
1340 vlc_mutex_lock( &p_input->stream.stream_lock );
1341 if( p_avi->i_length )
1343 p_input->stream.i_mux_rate =
1344 p_input->stream.p_selected_area->i_size / 50 / p_avi->i_length;
1346 msg_Dbg( p_input, "checking interleaved" );
1347 p_avi->b_interleaved = AVI_Interleaved( p_input );
1348 msg_Dbg( p_input, "interleaved=%s", p_avi->b_interleaved ? "yes" : "no" );
1352 p_input->stream.i_mux_rate = 0;
1354 vlc_mutex_unlock( &p_input->stream.stream_lock );
1356 b_stream_audio = VLC_FALSE;
1357 b_stream_video = VLC_FALSE;
1359 for( i = 0; i < p_avi->i_streams; i++ )
1361 #define p_info p_avi->pp_info[i]
1362 switch( p_info->p_es->i_cat )
1366 if( !b_stream_video )
1368 b_stream_video = AVI_StreamStart( p_input, p_avi, i );
1373 if( !b_stream_audio )
1375 b_stream_audio = AVI_StreamStart( p_input, p_avi, i );
1384 if( !b_stream_video )
1386 msg_Warn( p_input, "no video stream found" );
1388 if( !b_stream_audio )
1390 msg_Warn( p_input, "no audio stream found!" );
1393 vlc_mutex_lock( &p_input->stream.stream_lock );
1394 p_input->stream.p_selected_program->b_is_ok = 1;
1395 vlc_mutex_unlock( &p_input->stream.stream_lock );
1397 if( p_avi->b_seekable )
1399 AVI_ChunkGoto( p_input, p_movi );
1403 // already at begining of p_movi
1405 AVI_SkipBytes( p_input, 12 ); // enter in p_movi
1407 p_avi->i_movi_begin = p_movi->i_chunk_pos;
1414 /*****************************************************************************
1415 * Function to convert pts to chunk or byte
1416 *****************************************************************************/
1418 static inline mtime_t AVI_PTSToChunk( avi_stream_t *p_info,
1421 return (mtime_t)((int64_t)i_pts *
1422 (int64_t)p_info->i_rate /
1423 (int64_t)p_info->i_scale /
1426 static inline mtime_t AVI_PTSToByte( avi_stream_t *p_info,
1429 return (mtime_t)((int64_t)i_pts *
1430 (int64_t)p_info->i_rate /
1431 (int64_t)p_info->i_scale /
1433 (int64_t)p_info->i_samplesize );
1436 static mtime_t AVI_GetDPTS( avi_stream_t *p_stream, int i_count )
1438 if( p_stream->i_samplesize )
1440 return (mtime_t)( (int64_t)1000000 *
1442 (int64_t)p_stream->i_scale /
1443 (int64_t)p_stream->i_rate /
1444 (int64_t)p_stream->i_samplesize );
1448 return (mtime_t)( (int64_t)1000000 *
1450 (int64_t)p_stream->i_scale /
1451 (int64_t)p_stream->i_rate);
1456 static mtime_t AVI_GetPTS( avi_stream_t *p_info )
1459 if( p_info->i_samplesize )
1461 /* we need a valid entry we will emulate one */
1463 if( p_info->i_idxposc == p_info->i_idxnb )
1465 if( p_info->i_idxposc )
1467 /* use the last entry */
1468 i_len = p_info->p_index[p_info->i_idxnb - 1].i_lengthtotal
1469 + p_info->p_index[p_info->i_idxnb - 1].i_length
1470 + p_info->i_idxposb; /* should be 0 */
1474 i_len = p_info->i_idxposb;
1475 /* no valid entry use only offset*/
1480 i_len = p_info->p_index[p_info->i_idxposc].i_lengthtotal
1481 + p_info->i_idxposb;
1483 return (mtime_t)( (int64_t)1000000 *
1485 (int64_t)p_info->i_scale /
1486 (int64_t)p_info->i_rate /
1487 (int64_t)p_info->i_samplesize );
1491 /* even if p_info->i_idxposc isn't valid, there isn't any problem */
1492 return (mtime_t)( (int64_t)1000000 *
1493 (int64_t)(p_info->i_idxposc ) *
1494 (int64_t)p_info->i_scale /
1495 (int64_t)p_info->i_rate);
1500 static void AVI_FixPTS( avi_stream_t *p_stream, pes_packet_t *p_pes )
1502 data_packet_t *p_data;
1506 switch( p_stream->i_fourcc )
1508 case VLC_FOURCC( 'm', 'p', 'g', 'a' ):
1509 p_data = p_pes->p_first;
1512 p = p_data->p_payload_start;
1513 while( p < p_data->p_payload_end - 2 )
1515 if( p[0] == 0xff && ( p[1]&0xe0) == 0xe0 )
1517 mtime_t i_diff = AVI_GetDPTS( p_stream, i_pos );
1518 p_pes->i_dts += i_diff;
1519 p_pes->i_pts += i_diff;
1524 p_data = p_data->p_next;
1527 case VLC_FOURCC( 'a', '5', '2', ' ' ):
1528 p_data = p_pes->p_first;
1531 p = p_data->p_payload_start;
1532 while( p < p_data->p_payload_end - 2 )
1534 if( p[0] == 0x0b && p[1] == 0x77 )
1536 mtime_t i_diff = AVI_GetDPTS( p_stream, i_pos );
1537 p_pes->i_dts += i_diff;
1538 p_pes->i_pts += i_diff;
1542 p_data = p_data->p_next;
1546 /* we can't fix :( */
1552 static int AVI_StreamChunkFind( input_thread_t *p_input,
1553 unsigned int i_stream )
1555 demux_sys_t *p_avi = p_input->p_demux_data;
1556 avi_packet_t avi_pk;
1558 /* find first chunk of i_stream that isn't in index */
1560 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin + 12 )
1562 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
1563 if( AVI_PacketNext( p_input ) )
1565 return VLC_EGENERIC;
1570 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin + 12 );
1576 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
1578 msg_Warn( p_input, "cannot get packet header" );
1579 return VLC_EGENERIC;
1581 if( avi_pk.i_stream >= p_avi->i_streams ||
1582 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1584 if( AVI_PacketNext( p_input ) )
1586 return VLC_EGENERIC;
1591 /* add this chunk to the index */
1592 AVIIndexEntry_t index;
1594 index.i_id = avi_pk.i_fourcc;
1596 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
1598 index.i_pos = avi_pk.i_pos;
1599 index.i_length = avi_pk.i_size;
1600 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
1602 if( avi_pk.i_stream == i_stream )
1607 if( AVI_PacketNext( p_input ) )
1609 return VLC_EGENERIC;
1616 /* be sure that i_ck will be a valid index entry */
1617 static int AVI_SetStreamChunk( input_thread_t *p_input,
1618 unsigned int i_stream,
1621 demux_sys_t *p_avi = p_input->p_demux_data;
1622 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1624 p_stream->i_idxposc = i_ck;
1625 p_stream->i_idxposb = 0;
1627 if( i_ck >= p_stream->i_idxnb )
1629 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1632 p_stream->i_idxposc++;
1633 if( AVI_StreamChunkFind( p_input, i_stream ) )
1635 return VLC_EGENERIC;
1638 } while( p_stream->i_idxposc < i_ck );
1645 /* XXX FIXME up to now, we assume that all chunk are one after one */
1646 static int AVI_SetStreamBytes( input_thread_t *p_input,
1647 unsigned int i_stream,
1650 demux_sys_t *p_avi = p_input->p_demux_data;
1651 avi_stream_t *p_stream = p_avi->pp_info[i_stream];
1653 if( ( p_stream->i_idxnb > 0 )
1654 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1655 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1657 /* index is valid to find the ck */
1658 /* uses dichototmie to be fast enougth */
1659 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1660 int i_idxmax = p_stream->i_idxnb;
1664 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1666 i_idxmax = i_idxposc ;
1667 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1671 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1672 p_stream->p_index[i_idxposc].i_length <= i_byte)
1674 i_idxmin = i_idxposc ;
1675 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1679 p_stream->i_idxposc = i_idxposc;
1680 p_stream->i_idxposb = i_byte -
1681 p_stream->p_index[i_idxposc].i_lengthtotal;
1690 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1691 p_stream->i_idxposb = 0;
1694 p_stream->i_idxposc++;
1695 if( AVI_StreamChunkFind( p_input, i_stream ) )
1697 return VLC_EGENERIC;
1700 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1701 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1703 p_stream->i_idxposb = i_byte -
1704 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1709 static int AVI_StreamSeek( input_thread_t *p_input,
1714 #define p_stream p_avi->pp_info[i_stream]
1717 i_oldpts = AVI_GetPTS( p_stream );
1719 if( !p_stream->i_samplesize )
1721 if( AVI_SetStreamChunk( p_input,
1723 AVI_PTSToChunk( p_stream, i_date ) ) )
1725 return VLC_EGENERIC;
1729 "old:"I64Fd" %s new "I64Fd,
1731 i_oldpts > i_date ? ">" : "<",
1734 if( p_stream->i_cat == VIDEO_ES )
1736 /* search key frame */
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;
1769 if( AVI_SetStreamBytes( p_input,
1771 AVI_PTSToByte( p_stream, i_date ) ) )
1773 return VLC_EGENERIC;
1780 /*****************************************************************************
1781 * AVI_Interleaved: check weither a file is interleaved or not.
1782 *****************************************************************************/
1783 static vlc_bool_t AVI_Interleaved( input_thread_t *p_input )
1785 demux_sys_t *p_sys = p_input->p_demux_data;
1788 vlc_bool_t b_ret = VLC_TRUE;
1792 if( p_input->stream.p_selected_area->i_size <= 100 )
1797 i_max = __MIN( 2000000, p_input->stream.p_selected_area->i_size / 100 );
1799 #define tk p_sys->pp_info[i]
1800 while( i_time < p_sys->i_length * (mtime_t)1000000)
1806 for( i = 0; i < p_sys->i_streams; i++ )
1808 while( AVI_GetPTS( tk ) < i_time && tk->i_idxposc < tk->i_idxnb - 1 )
1815 i_ref = tk->p_index[tk->i_idxposc].i_pos;
1817 if( tk->p_index[tk->i_idxposc].i_pos - i_ref > i_max ||
1818 tk->p_index[tk->i_idxposc].i_pos - i_ref < -i_max ||
1819 tk->p_index[tk->i_idxposc].i_length > i_max )
1821 msg_Dbg( p_input, "interleaved=no because ref=%lld pos=%lld length=%d (max=%lld)",
1822 i_ref, tk->p_index[tk->i_idxposc].i_pos, tk->p_index[tk->i_idxposc].i_length, i_max );
1830 for( i = 0; i < p_sys->i_streams; i++ )
1839 /*****************************************************************************
1840 * AVISeek: goto to i_date or i_percent
1841 *****************************************************************************
1842 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1843 *****************************************************************************/
1844 static int AVISeek ( input_thread_t *p_input,
1845 mtime_t i_date, int i_percent )
1848 demux_sys_t *p_avi = p_input->p_demux_data;
1849 unsigned int i_stream;
1851 "seek requested: "I64Fd" secondes %d%%",
1855 if( p_avi->b_seekable )
1857 if( !p_avi->i_length || p_avi->b_interleaved )
1859 avi_stream_t *p_stream;
1862 /* use i_percent to create a true i_date */
1863 if( !p_avi->b_interleaved )
1866 "mmh, seeking without index at %d%%"
1867 " work only for interleaved file", i_percent );
1870 if( i_percent >= 100 )
1872 msg_Warn( p_input, "cannot seek so far !" );
1875 i_percent = __MAX( i_percent, 0 );
1877 /* try to find chunk that is at i_percent or the file */
1878 i_pos = __MAX( i_percent *
1879 p_input->stream.p_selected_area->i_size / 100,
1880 p_avi->i_movi_begin );
1881 /* search first selected stream */
1882 for( i_stream = 0, p_stream = NULL;
1883 i_stream < p_avi->i_streams; i_stream++ )
1885 p_stream = p_avi->pp_info[i_stream];
1886 if( p_stream->b_activated )
1891 if( !p_stream || !p_stream->b_activated )
1893 msg_Warn( p_input, "cannot find any selected stream" );
1897 /* be sure that the index exit */
1898 if( AVI_SetStreamChunk( p_input,
1902 msg_Warn( p_input, "cannot seek" );
1906 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1907 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1909 /* search after i_idxposc */
1910 if( AVI_SetStreamChunk( p_input,
1911 i_stream, p_stream->i_idxposc + 1 ) )
1913 msg_Warn( p_input, "cannot seek" );
1917 i_date = AVI_GetPTS( p_stream );
1918 /* TODO better support for i_samplesize != 0 */
1919 msg_Dbg( p_input, "estimate date "I64Fd, i_date );
1922 #define p_stream p_avi->pp_info[i_stream]
1924 /* seek for chunk based streams */
1925 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1927 if( p_stream->b_activated && !p_stream->i_samplesize )
1928 // if( p_stream->b_activated )
1930 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1931 p_avi->i_time = __MAX( AVI_GetPTS( p_stream ),
1938 i_date = p_avi->i_time;
1940 /* seek for bytes based streams */
1941 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
1943 if( p_stream->b_activated && p_stream->i_samplesize )
1945 AVI_StreamSeek( p_input, p_avi, i_stream, i_date );
1946 // p_avi->i_time = __MAX( AVI_GetPTS( p_stream ), p_avi->i_time );
1949 msg_Dbg( p_input, "seek: "I64Fd" secondes", p_avi->i_time /1000000 );
1950 /* set true movie time */
1952 if( !p_avi->i_time )
1954 p_avi->i_time = i_date;
1961 msg_Err( p_input, "shouldn't yet be executed" );
1967 static pes_packet_t *PES_split( input_thread_t *p_input, avi_stream_t *p_stream, pes_packet_t *p_pes )
1969 pes_packet_t *p_pes2;
1970 data_packet_t *p_data;
1973 if( p_pes->i_nb_data < 2 )
1977 p_pes2 = input_NewPES( p_input->p_method_data );
1978 p_pes2->i_pts = p_pes->i_pts;
1979 p_pes2->i_dts = p_pes->i_dts;
1980 p_pes2->i_nb_data = p_pes->i_nb_data/2;
1981 p_pes2->i_pes_size = 0;
1982 for( i_nb_data = 0, p_data = p_pes->p_first;
1983 i_nb_data < p_pes2->i_nb_data;
1984 i_nb_data++, p_data = p_data->p_next )
1986 p_pes2->i_pes_size +=
1987 p_data->p_payload_end - p_data->p_payload_start;
1988 p_pes2->p_last = p_data;
1990 p_pes2->p_first = p_pes->p_first;
1991 p_pes2->p_last->p_next = NULL;
1993 p_pes->p_first = p_data;
1994 p_pes->i_pes_size -= p_pes2->i_pes_size;
1995 p_pes->i_nb_data -= p_pes2->i_nb_data;
1996 // p_pes->i_pts += AVI_GetDPTS( p_stream, p_pes2->i_pes_size );
1997 // p_pes->i_dts += AVI_GetDPTS( p_stream, p_pes2->i_pes_size );
2004 /*****************************************************************************
2005 * AVIDemux_Seekable: reads and demuxes data packets for stream seekable
2006 *****************************************************************************
2007 * AVIDemux: reads and demuxes data packets
2008 *****************************************************************************
2009 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2010 *****************************************************************************/
2011 typedef struct avi_stream_toread_s
2017 off_t i_posf; // where we will read :
2018 // if i_idxposb == 0 : begining of chunk (+8 to acces data)
2019 // else : point on data directly
2020 } avi_stream_toread_t;
2022 static int AVIDemux_Seekable( input_thread_t *p_input )
2024 unsigned int i_stream_count;
2025 unsigned int i_stream;
2026 vlc_bool_t b_stream;
2027 vlc_bool_t b_play_audio;
2028 vlc_bool_t b_video; /* is there some video track selected */
2029 // cannot be more than 100 stream (dcXX or wbXX)
2030 avi_stream_toread_t toread[100];
2032 demux_sys_t *p_avi = p_input->p_demux_data;
2035 /* detect new selected/unselected streams */
2036 for( i_stream = 0,i_stream_count= 0, b_video = VLC_FALSE;
2037 i_stream < p_avi->i_streams; i_stream++ )
2039 #define p_stream p_avi->pp_info[i_stream]
2040 if( p_stream->p_es )
2042 if( p_stream->p_es->p_decoder_fifo &&
2043 !p_stream->b_activated )
2045 AVI_StreamStart( p_input, p_avi, i_stream );
2048 if( !p_stream->p_es->p_decoder_fifo &&
2049 p_stream->b_activated )
2051 AVI_StreamStop( p_input, p_avi, i_stream );
2054 if( p_stream->b_activated )
2057 if( p_stream->i_cat == VIDEO_ES )
2065 if( i_stream_count <= 0 )
2067 msg_Warn( p_input, "no track selected, exiting..." );
2071 if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
2075 /* first wait for empty buffer, arbitrary time FIXME */
2076 //msleep( DEFAULT_PTS_DELAY );
2078 i_date = (mtime_t)1000000 *
2079 (mtime_t)p_avi->i_length *
2080 (mtime_t)AVI_TellAbsolute( p_input ) /
2081 (mtime_t)p_input->stream.p_selected_area->i_size;
2082 i_percent = 100 * AVI_TellAbsolute( p_input ) /
2083 p_input->stream.p_selected_area->i_size;
2085 // input_ClockInit( p_input->stream.p_selected_program );
2086 AVISeek( p_input, i_date, i_percent);
2088 #ifdef __AVI_SUBTITLE__
2091 subtitle_Seek( p_avi->p_sub, p_avi->i_time );
2097 /* wait for the good time */
2099 p_avi->i_pcr = p_avi->i_time * 9 / 100;
2101 input_ClockManageRef( p_input,
2102 p_input->stream.p_selected_program,
2106 p_avi->i_time += 25*1000; /* read 25ms */
2108 #ifdef __AVI_SUBTITLE__
2111 subtitle_Demux( p_avi->p_sub, p_avi->i_time );
2115 /* Check if we need to send the audio data to decoder */
2116 b_play_audio = !p_input->stream.control.b_mute;
2119 for( i_stream = 0; i_stream < p_avi->i_streams; i_stream++ )
2121 #define p_stream p_avi->pp_info[i_stream]
2124 toread[i_stream].b_ok = p_stream->b_activated;
2125 if( p_stream->i_idxposc < p_stream->i_idxnb )
2127 toread[i_stream].i_posf =
2128 p_stream->p_index[p_stream->i_idxposc].i_pos;
2129 if( p_stream->i_idxposb > 0 )
2131 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
2136 toread[i_stream].i_posf = -1;
2139 i_dpts = p_avi->i_time - AVI_GetPTS( p_stream );
2141 if( p_stream->i_samplesize )
2143 toread[i_stream].i_toread = AVI_PTSToByte( p_stream,
2148 toread[i_stream].i_toread = AVI_PTSToChunk( p_stream,
2154 toread[i_stream].i_toread *= -1;
2159 b_stream = VLC_FALSE;
2163 #define p_stream p_avi->pp_info[i_stream]
2165 pes_packet_t *p_pes;
2170 /* search for first chunk to be read */
2171 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_avi->i_streams; i++ )
2173 if( !toread[i].b_ok ||
2174 AVI_GetDPTS( p_avi->pp_info[i],
2175 toread[i].i_toread ) <= -25 * 1000 )
2180 if( toread[i].i_toread > 0 )
2182 b_done = VLC_FALSE; // not yet finished
2185 if( toread[i].i_posf > 0 )
2187 if( i_pos == -1 || i_pos > toread[i_stream].i_posf )
2190 i_pos = toread[i].i_posf;
2197 // return( b_stream ? 1 : 0 );
2203 /* no valid index, we will parse directly the stream
2204 * in case we fail we will disable all finished stream */
2205 if( p_avi->i_movi_lastchunk_pos >= p_avi->i_movi_begin + 12 )
2207 AVI_SeekAbsolute( p_input, p_avi->i_movi_lastchunk_pos );
2208 if( AVI_PacketNext( p_input ) )
2210 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2215 AVI_SeekAbsolute( p_input, p_avi->i_movi_begin + 12 );
2220 avi_packet_t avi_pk;
2222 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
2225 "cannot get packet header, track disabled" );
2226 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2228 if( avi_pk.i_stream >= p_avi->i_streams ||
2229 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
2231 if( AVI_PacketNext( p_input ) )
2234 "cannot skip packet, track disabled" );
2235 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2241 /* add this chunk to the index */
2242 AVIIndexEntry_t index;
2244 index.i_id = avi_pk.i_fourcc;
2246 AVI_GetKeyFlag(p_avi->pp_info[avi_pk.i_stream]->i_codec,
2248 index.i_pos = avi_pk.i_pos;
2249 index.i_length = avi_pk.i_size;
2250 AVI_IndexAddEntry( p_avi, avi_pk.i_stream, &index );
2252 i_stream = avi_pk.i_stream;
2253 /* do we will read this data ? */
2254 if( AVI_GetDPTS( p_stream,
2255 toread[i_stream].i_toread ) > -25 * 1000 )
2261 if( AVI_PacketNext( p_input ) )
2264 "cannot skip packet, track disabled" );
2265 return( AVI_StreamStopFinishedStreams( p_input, p_avi ) ? 0 : 1 );
2274 AVI_SeekAbsolute( p_input, i_pos );
2277 /* read thoses data */
2278 if( p_stream->i_samplesize )
2280 unsigned int i_toread;
2282 if( ( i_toread = toread[i_stream].i_toread ) <= 0 )
2284 if( p_stream->i_samplesize > 1 )
2286 i_toread = p_stream->i_samplesize;
2290 i_toread = __MAX( AVI_PTSToByte( p_stream, 20 * 1000 ), 100 );
2293 i_size = __MIN( p_stream->p_index[p_stream->i_idxposc].i_length -
2294 p_stream->i_idxposb,
2299 i_size = p_stream->p_index[p_stream->i_idxposc].i_length;
2302 if( p_stream->i_idxposb == 0 )
2304 i_size += 8; // need to read and skip header
2307 if( input_ReadInPES( p_input, &p_pes, __EVEN( i_size ) ) < 0 )
2309 msg_Warn( p_input, "failled reading data" );
2310 AVI_StreamStop( p_input, p_avi, i_stream );
2311 toread[i_stream].b_ok = VLC_FALSE;
2315 if( i_size % 2 ) // read was padded on word boundary
2317 p_pes->p_last->p_payload_end--;
2318 p_pes->i_pes_size--;
2321 if( p_stream->i_idxposb == 0 )
2323 p_pes->p_first->p_payload_start += 8;
2324 p_pes->i_pes_size -= 8;
2327 p_pes->i_pts = AVI_GetPTS( p_stream );
2330 /* fix pts for audio: ie pts sould be for the first byte of the first frame */
2331 if( p_stream->i_samplesize == 1 )
2333 AVI_FixPTS( p_stream, p_pes );
2338 if( p_stream->i_samplesize )
2340 if( p_stream->i_idxposb == 0 )
2344 toread[i_stream].i_toread -= i_size;
2345 p_stream->i_idxposb += i_size;
2346 if( p_stream->i_idxposb >=
2347 p_stream->p_index[p_stream->i_idxposc].i_length )
2349 p_stream->i_idxposb = 0;
2350 p_stream->i_idxposc++;
2355 toread[i_stream].i_toread--;
2356 p_stream->i_idxposc++;
2359 if( p_stream->i_idxposc < p_stream->i_idxnb)
2361 toread[i_stream].i_posf =
2362 p_stream->p_index[p_stream->i_idxposc].i_pos;
2363 if( p_stream->i_idxposb > 0 )
2365 toread[i_stream].i_posf += 8 + p_stream->i_idxposb;
2371 toread[i_stream].i_posf = -1;
2374 b_stream = VLC_TRUE; // at least one read succeed
2376 if( p_stream->p_es && p_stream->p_es->p_decoder_fifo &&
2377 ( b_play_audio || p_stream->i_cat != AUDIO_ES ) )
2381 input_ClockGetTS( p_input,
2382 p_input->stream.p_selected_program,
2383 p_pes->i_pts * 9/100);
2385 p_pes->i_rate = p_input->stream.control.i_rate;
2387 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
2391 input_DeletePES( p_input->p_method_data, p_pes );
2397 /*****************************************************************************
2398 * AVIDemux_UnSeekable: reads and demuxes data packets for unseekable file
2399 *****************************************************************************
2400 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
2401 *****************************************************************************/
2402 static int AVIDemux_UnSeekable( input_thread_t *p_input )
2404 demux_sys_t *p_avi = p_input->p_demux_data;
2405 avi_stream_t *p_stream_master;
2407 unsigned int i_stream;
2408 unsigned int i_packet;
2410 /* Check if we need to send the audio data to decoder */
2411 b_audio = !p_input->stream.control.b_mute;
2413 input_ClockManageRef( p_input,
2414 p_input->stream.p_selected_program,
2416 /* *** find master stream for data packet skipping algo *** */
2417 /* *** -> first video, if any, or first audio ES *** */
2418 for( i_stream = 0, p_stream_master = NULL;
2419 i_stream < p_avi->i_streams; i_stream++ )
2421 #define p_stream p_avi->pp_info[i_stream]
2422 if( p_stream->p_es &&
2423 p_stream->p_es->p_decoder_fifo )
2425 if( p_stream->i_cat == VIDEO_ES )
2427 p_stream_master = p_stream;
2430 if( p_stream->i_cat == AUDIO_ES && !p_stream_master )
2432 p_stream_master = p_stream;
2437 if( !p_stream_master )
2439 msg_Warn( p_input, "no more stream selected" );
2443 p_avi->i_pcr = AVI_GetPTS( p_stream_master ) * 9 / 100;
2445 for( i_packet = 0; i_packet < 10; i_packet++)
2447 #define p_stream p_avi->pp_info[avi_pk.i_stream]
2449 avi_packet_t avi_pk;
2451 if( AVI_PacketGetHeader( p_input, &avi_pk ) )
2455 // AVI_ParseStreamHeader( avi_pk.i_fourcc, &i_stream, &i_cat );
2457 if( avi_pk.i_stream >= p_avi->i_streams ||
2458 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
2460 /* we haven't found an audio or video packet:
2461 * - we have seek, found first next packet
2462 * - others packets could be found, skip them
2464 switch( avi_pk.i_fourcc )
2466 case AVIFOURCC_JUNK:
2467 case AVIFOURCC_LIST:
2468 case AVIFOURCC_RIFF:
2469 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
2470 case AVIFOURCC_idx1:
2473 return( !AVI_PacketNext( p_input ) ? 1 : 0 );
2478 "seems to have lost position, resync" );
2479 if( AVI_PacketSearch( p_input ) )
2481 msg_Err( p_input, "resync failed" );
2488 /* do will send this packet to decoder ? */
2489 if( ( !b_audio && avi_pk.i_cat == AUDIO_ES )||
2491 !p_stream->p_es->p_decoder_fifo )
2493 if( AVI_PacketNext( p_input ) )
2500 /* it's a selected stream, check for time */
2501 if( __ABS( AVI_GetPTS( p_stream ) -
2502 AVI_GetPTS( p_stream_master ) )< 600*1000 )
2504 /* load it and send to decoder */
2505 pes_packet_t *p_pes;
2506 if( AVI_PacketRead( p_input, &avi_pk, &p_pes ) || !p_pes)
2512 input_ClockGetTS( p_input,
2513 p_input->stream.p_selected_program,
2514 AVI_GetPTS( p_stream ) * 9/100);
2516 p_pes->i_rate = p_input->stream.control.i_rate;
2518 input_DecodePES( p_stream->p_es->p_decoder_fifo, p_pes );
2522 if( AVI_PacketNext( p_input ) )
2529 /* *** update stream time position *** */
2530 if( p_stream->i_samplesize )
2532 p_stream->i_idxposb += avi_pk.i_size;
2536 p_stream->i_idxposc++;