1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 VideoLAN
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() */
29 #include <vlc/input.h>
36 /*****************************************************************************
38 *****************************************************************************/
40 #define INTERLEAVE_TEXT N_("Force interleaved method" )
41 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method" )
43 #define INDEX_TEXT N_("Force index creation")
44 #define INDEX_LONGTEXT N_( \
45 "Recreate a index for the AVI file so we can seek trough it more reliably." )
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
51 set_description( _("AVI demuxer") );
52 set_capability( "demux2", 212 );
53 set_category( CAT_INPUT );
54 set_subcategory( SUBCAT_INPUT_DEMUX );
56 add_bool( "avi-interleaved", 0, NULL,
57 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
58 add_bool( "avi-index", 0, NULL,
59 INDEX_TEXT, INDEX_LONGTEXT, VLC_TRUE );
61 set_callbacks( Open, Close );
64 /*****************************************************************************
66 *****************************************************************************/
67 static int Control ( demux_t *, int, va_list );
68 static int Seek ( demux_t *, mtime_t, int );
69 static int Demux_Seekable ( demux_t * );
70 static int Demux_UnSeekable( demux_t * );
72 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
73 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
77 vlc_fourcc_t i_fourcc;
80 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
82 uint8_t i_peek[8]; /* first 8 bytes */
84 unsigned int i_stream;
95 uint32_t i_lengthtotal;
101 vlc_bool_t b_activated;
103 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
104 vlc_fourcc_t i_codec;
112 avi_entry_t *p_index;
113 unsigned int i_idxnb;
114 unsigned int i_idxmax;
116 unsigned int i_idxposc; /* numero of chunk */
117 unsigned int i_idxposb; /* byte in the current chunk */
119 /* For VBR audio only */
120 unsigned int i_blockno;
121 unsigned int i_blocksize;
129 vlc_bool_t b_seekable;
135 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
137 /* number of streams and information */
138 unsigned int i_track;
145 static inline off_t __EVEN( off_t i )
147 return (i & 1) ? i + 1 : i;
150 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
151 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
152 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
153 static mtime_t AVI_GetPTS ( avi_track_t * );
156 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
157 static int AVI_StreamChunkSet ( demux_t *,
158 unsigned int i_stream, unsigned int i_ck );
159 static int AVI_StreamBytesSet ( demux_t *,
160 unsigned int i_stream, off_t i_byte );
162 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
163 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
165 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
166 static int AVI_PacketNext ( demux_t * );
167 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
168 static int AVI_PacketSearch ( demux_t * );
170 static void AVI_IndexLoad ( demux_t * );
171 static void AVI_IndexCreate ( demux_t * );
172 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
174 static mtime_t AVI_MovieGetLength( demux_t * );
176 /*****************************************************************************
178 *****************************************************************************/
179 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
180 static int AVI_TrackStopFinishedStreams( demux_t *);
183 - For VBR mp3 stream:
184 count blocks by rounded-up chunksizes instead of chunks
185 we need full emulation of dshow avi demuxer bugs :(
186 fixes silly nandub-style a-v delaying in avi with vbr mp3...
187 (from mplayer 2002/08/02)
191 /*****************************************************************************
192 * Open: check file and initializes AVI structures
193 *****************************************************************************/
194 static int Open( vlc_object_t * p_this )
196 demux_t *p_demux = (demux_t *)p_this;
200 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
201 avi_chunk_list_t *p_hdrl, *p_movi;
202 avi_chunk_avih_t *p_avih;
204 unsigned int i_track;
205 unsigned int i, i_peeker;
210 /* Is it an avi file ? */
211 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 )
213 msg_Dbg( p_demux, "cannot peek()" );
216 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
218 if( !strncmp( &p_peek[0], "RIFF", 4 ) && !strncmp( &p_peek[8], "AVI ", 4 ) )
224 if( i_peeker == 188 )
226 msg_Warn( p_demux, "avi module discarded (invalid header)" );
230 /* Initialize input structures. */
231 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
232 memset( p_sys, 0, sizeof( demux_sys_t ) );
235 p_sys->i_movi_lastchunk_pos = 0;
236 p_sys->b_odml = VLC_FALSE;
241 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
243 p_demux->pf_control = Control;
244 p_demux->pf_demux = Demux_Seekable;
245 /* For unseekable stream, automaticaly use Demux_UnSeekable */
246 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
248 p_demux->pf_demux = Demux_UnSeekable;
253 stream_Read( p_demux->s, NULL, i_peeker );
256 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
258 msg_Err( p_demux, "avi module discarded (invalid file)" );
262 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
264 unsigned int i_count =
265 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
267 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
268 for( i = 1; i < i_count; i++ )
270 avi_chunk_list_t *p_sysx;
272 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
273 if( p_sysx->i_type == AVIFOURCC_AVIX )
275 msg_Warn( p_demux, "detected OpenDML file" );
276 p_sys->b_odml = VLC_TRUE;
282 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
283 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
284 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
286 if( !p_hdrl || !p_movi )
288 msg_Err( p_demux, "avi module discarded (invalid file)" );
292 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
294 msg_Err( p_demux, "cannot find avih chunk" );
297 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
298 if( p_avih->i_streams != i_track )
301 "found %d stream but %d are declared",
302 i_track, p_avih->i_streams );
306 msg_Err( p_demux, "no stream defined!" );
310 /* print information on streams */
311 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
313 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
314 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
315 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
316 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
317 if( ( p_sys->meta = vlc_meta_New() ) )
320 sprintf( buffer, "%s%s%s%s",
321 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
322 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
323 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
324 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
325 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
328 /* now read info on each stream and create ES */
329 for( i = 0 ; i < i_track; i++ )
331 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
332 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
333 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
334 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
335 avi_chunk_strf_auds_t *p_auds;
336 avi_chunk_strf_vids_t *p_vids;
339 tk->b_activated = VLC_FALSE;
349 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
350 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
352 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
354 msg_Warn( p_demux, "stream[%d] incomplete", i );
358 tk->i_rate = p_strh->i_rate;
359 tk->i_scale = p_strh->i_scale;
360 tk->i_samplesize = p_strh->i_samplesize;
361 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
362 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
364 switch( p_strh->i_type )
366 case( AVIFOURCC_auds ):
367 tk->i_cat = AUDIO_ES;
368 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
369 p_auds->p_wf->wFormatTag );
370 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
372 if( p_auds->p_wf->wFormatTag == 1 )
374 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
381 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
383 fmt.audio.i_channels = p_auds->p_wf->nChannels;
384 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
385 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
386 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
387 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
388 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
389 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
390 fmt.p_extra = &p_auds->p_wf[1];
391 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
392 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
393 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
396 case( AVIFOURCC_vids ):
397 tk->i_cat = VIDEO_ES;
398 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
399 p_vids->p_bih->biCompression );
400 if( p_vids->p_bih->biCompression == 0x00 )
402 switch( p_vids->p_bih->biBitCount )
405 tk->i_codec = VLC_FOURCC('R','V','3','2');
408 tk->i_codec = VLC_FOURCC('R','V','2','4');
411 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
414 tk->i_codec = VLC_FOURCC('R','V','1','5');
417 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
420 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
422 if( p_vids->p_bih->biBitCount == 24 )
424 /* This is in BGR format */
425 fmt.video.i_bmask = 0x00ff0000;
426 fmt.video.i_gmask = 0x0000ff00;
427 fmt.video.i_rmask = 0x000000ff;
432 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
433 if( tk->i_codec == FOURCC_mp4v &&
434 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
436 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
439 tk->i_samplesize = 0;
440 fmt.video.i_width = p_vids->p_bih->biWidth;
441 fmt.video.i_height = p_vids->p_bih->biHeight;
442 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
444 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
445 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
446 fmt.p_extra = &p_vids->p_bih[1];
447 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
449 (char*)&p_vids->p_bih->biCompression,
450 p_vids->p_bih->biWidth,
451 p_vids->p_bih->biHeight,
452 p_vids->p_bih->biBitCount,
453 (float)tk->i_rate/(float)tk->i_scale );
455 if( p_vids->p_bih->biCompression == 0x00 )
457 /* RGB DIB are coded from bottom to top */
459 (unsigned int)(-(int)p_vids->p_bih->biHeight);
463 case( AVIFOURCC_txts):
465 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
466 msg_Dbg( p_demux, "stream[%d] subtitles", i );
467 es_format_Init( &fmt, SPU_ES, tk->i_codec );
470 case( AVIFOURCC_mids):
471 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
473 msg_Warn( p_demux, "stream[%d] unknown type", i );
479 fmt.psz_description = strdup( p_strn->p_str );
481 tk->p_es = es_out_Add( p_demux->out, &fmt );
482 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
485 if( p_sys->i_track <= 0 )
487 msg_Err( p_demux, "no valid track" );
491 if( config_GetInt( p_demux, "avi-index" ) )
493 if( p_sys->b_seekable )
495 AVI_IndexCreate( p_demux );
499 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
500 AVI_IndexLoad( p_demux );
505 AVI_IndexLoad( p_demux );
508 /* *** movie length in sec *** */
509 p_sys->i_length = AVI_MovieGetLength( p_demux );
510 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
511 (mtime_t)p_avih->i_microsecperframe /
514 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have strange behavour" );
516 /* fix some BeOS MediaKit generated file */
517 for( i = 0 ; i < p_sys->i_track; i++ )
519 avi_track_t *tk = p_sys->track[i];
520 avi_chunk_list_t *p_strl;
521 avi_chunk_strh_t *p_strh;
522 avi_chunk_strf_auds_t *p_auds;
524 if( tk->i_cat != AUDIO_ES )
528 if( tk->i_idxnb < 1 ||
530 tk->i_samplesize != 0 )
534 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
535 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
536 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
538 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
539 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
541 int64_t i_track_length =
542 tk->p_index[tk->i_idxnb-1].i_length +
543 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
544 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
545 (mtime_t)p_avih->i_microsecperframe;
549 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
552 tk->i_samplesize = 1;
553 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
554 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
558 if( p_sys->b_seekable )
560 /* we have read all chunk so go back to movi */
561 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
563 /* Skip movi header */
564 stream_Read( p_demux->s, NULL, 12 );
566 p_sys->i_movi_begin = p_movi->i_chunk_pos;
572 vlc_meta_Delete( p_sys->meta );
574 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
579 /*****************************************************************************
580 * Close: frees unused data
581 *****************************************************************************/
582 static void Close ( vlc_object_t * p_this )
584 demux_t * p_demux = (demux_t *)p_this;
586 demux_sys_t *p_sys = p_demux->p_sys ;
588 for( i = 0; i < p_sys->i_track; i++ )
590 if( p_sys->track[i] )
592 FREE( p_sys->track[i]->p_index );
593 free( p_sys->track[i] );
596 FREE( p_sys->track );
597 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
598 vlc_meta_Delete( p_sys->meta );
603 /*****************************************************************************
604 * Demux_Seekable: reads and demuxes data packets for stream seekable
605 *****************************************************************************
606 * AVIDemux: reads and demuxes data packets
607 *****************************************************************************
608 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
609 *****************************************************************************/
616 off_t i_posf; /* where we will read :
617 if i_idxposb == 0 : begining of chunk (+8 to acces data)
618 else : point on data directly */
619 } avi_track_toread_t;
621 static int Demux_Seekable( demux_t *p_demux )
623 demux_sys_t *p_sys = p_demux->p_sys;
625 unsigned int i_track_count = 0;
626 unsigned int i_track;
628 /* cannot be more than 100 stream (dcXX or wbXX) */
629 avi_track_toread_t toread[100];
632 /* detect new selected/unselected streams */
633 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
635 avi_track_t *tk = p_sys->track[i_track];
638 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
639 if( b && !tk->b_activated )
641 if( p_sys->b_seekable)
643 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
645 tk->b_activated = VLC_TRUE;
647 else if( !b && tk->b_activated )
649 tk->b_activated = VLC_FALSE;
657 if( i_track_count <= 0 )
659 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
661 p_sys->i_time += 25*1000; /* read 25ms */
664 if( p_sys->i_time >= i_length )
668 msg_Warn( p_demux, "no track selected, exiting..." );
672 /* wait for the good time */
673 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
674 p_sys->i_time += 25*1000; /* read 25ms */
677 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
679 avi_track_t *tk = p_sys->track[i_track];
682 toread[i_track].b_ok = tk->b_activated;
683 if( tk->i_idxposc < tk->i_idxnb )
685 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
686 if( tk->i_idxposb > 0 )
688 toread[i_track].i_posf += 8 + tk->i_idxposb;
693 toread[i_track].i_posf = -1;
696 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
698 if( tk->i_samplesize )
700 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
704 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
709 toread[i_track].i_toread *= -1;
713 b_stream = VLC_FALSE;
724 /* search for first chunk to be read */
725 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
727 if( !toread[i].b_ok ||
728 AVI_GetDPTS( p_sys->track[i],
729 toread[i].i_toread ) <= -25 * 1000 )
734 if( toread[i].i_toread > 0 )
736 b_done = VLC_FALSE; /* not yet finished */
738 if( toread[i].i_posf > 0 )
740 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
743 i_pos = toread[i].i_posf;
755 int i_loop_count = 0;
757 /* no valid index, we will parse directly the stream
758 * in case we fail we will disable all finished stream */
759 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
761 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
762 if( AVI_PacketNext( p_demux ) )
764 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
769 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
776 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
779 "cannot get packet header, track disabled" );
780 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
782 if( avi_pk.i_stream >= p_sys->i_track ||
783 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
785 if( AVI_PacketNext( p_demux ) )
788 "cannot skip packet, track disabled" );
789 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
792 /* Prevents from eating all the CPU with broken files.
793 * This value should be low enough so that it doesn't
794 * affect the reading speed too much. */
795 if( !(++i_loop_count % 1024) )
797 if( p_demux->b_die ) return -1;
800 if( !(i_loop_count % (1024 * 10)) )
802 "don't seem to find any data..." );
808 /* add this chunk to the index */
811 index.i_id = avi_pk.i_fourcc;
813 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
815 index.i_pos = avi_pk.i_pos;
816 index.i_length = avi_pk.i_size;
817 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
819 i_track = avi_pk.i_stream;
820 tk = p_sys->track[i_track];
821 /* do we will read this data ? */
822 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
828 if( AVI_PacketNext( p_demux ) )
831 "cannot skip packet, track disabled" );
832 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
841 stream_Seek( p_demux->s, i_pos );
844 /* Set the track to use */
845 tk = p_sys->track[i_track];
847 /* read thoses data */
848 if( tk->i_samplesize )
850 unsigned int i_toread;
852 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
854 if( tk->i_samplesize > 1 )
856 i_toread = tk->i_samplesize;
860 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
861 i_toread = __MAX( i_toread, 100 );
864 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
870 i_size = tk->p_index[tk->i_idxposc].i_length;
873 if( tk->i_idxposb == 0 )
875 i_size += 8; /* need to read and skip header */
878 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
880 msg_Warn( p_demux, "failled reading data" );
881 tk->b_activated = VLC_FALSE;
882 toread[i_track].b_ok = VLC_FALSE;
885 if( i_size % 2 ) /* read was padded on word boundary */
890 if( tk->i_idxposb == 0 )
892 p_frame->p_buffer += 8;
893 p_frame->i_buffer -= 8;
895 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
896 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
898 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
902 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
906 if( tk->i_samplesize )
908 if( tk->i_idxposb == 0 )
912 toread[i_track].i_toread -= i_size;
913 tk->i_idxposb += i_size;
915 tk->p_index[tk->i_idxposc].i_length )
923 int i_length = tk->p_index[tk->i_idxposc].i_length;
926 if( tk->i_cat == AUDIO_ES )
928 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
930 toread[i_track].i_toread--;
933 if( tk->i_idxposc < tk->i_idxnb)
935 toread[i_track].i_posf =
936 tk->p_index[tk->i_idxposc].i_pos;
937 if( tk->i_idxposb > 0 )
939 toread[i_track].i_posf += 8 + tk->i_idxposb;
945 toread[i_track].i_posf = -1;
948 b_stream = VLC_TRUE; /* at least one read succeed */
950 if( tk->i_cat != VIDEO_ES )
951 p_frame->i_dts = p_frame->i_pts;
954 p_frame->i_dts = p_frame->i_pts;
958 //p_pes->i_rate = p_demux->stream.control.i_rate;
959 es_out_Send( p_demux->out, tk->p_es, p_frame );
964 /*****************************************************************************
965 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
966 *****************************************************************************
967 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
968 *****************************************************************************/
969 static int Demux_UnSeekable( demux_t *p_demux )
971 demux_sys_t *p_sys = p_demux->p_sys;
972 avi_track_t *p_stream_master = NULL;
973 unsigned int i_stream;
974 unsigned int i_packet;
976 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
978 /* *** find master stream for data packet skipping algo *** */
979 /* *** -> first video, if any, or first audio ES *** */
980 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
982 avi_track_t *tk = p_sys->track[i_stream];
985 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
987 if( b && tk->i_cat == VIDEO_ES )
989 p_stream_master = tk;
993 p_stream_master = tk;
997 if( !p_stream_master )
999 msg_Warn( p_demux, "no more stream selected" );
1003 p_sys->i_time = AVI_GetPTS( p_stream_master );
1005 for( i_packet = 0; i_packet < 10; i_packet++)
1007 #define p_stream p_sys->track[avi_pk.i_stream]
1009 avi_packet_t avi_pk;
1011 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1016 if( avi_pk.i_stream >= p_sys->i_track ||
1017 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1019 /* we haven't found an audio or video packet:
1020 * - we have seek, found first next packet
1021 * - others packets could be found, skip them
1023 switch( avi_pk.i_fourcc )
1025 case AVIFOURCC_JUNK:
1026 case AVIFOURCC_LIST:
1027 case AVIFOURCC_RIFF:
1028 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1029 case AVIFOURCC_idx1:
1032 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1034 return( 0 ); /* eof */
1037 "seems to have lost position, resync" );
1038 if( AVI_PacketSearch( p_demux ) )
1040 msg_Err( p_demux, "resync failed" );
1047 /* check for time */
1048 if( __ABS( AVI_GetPTS( p_stream ) -
1049 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1051 /* load it and send to decoder */
1053 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1057 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1059 if( avi_pk.i_cat != VIDEO_ES )
1060 p_frame->i_dts = p_frame->i_pts;
1063 p_frame->i_dts = p_frame->i_pts;
1067 //p_pes->i_rate = p_demux->stream.control.i_rate;
1068 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1072 if( AVI_PacketNext( p_demux ) )
1078 /* *** update stream time position *** */
1079 if( p_stream->i_samplesize )
1081 p_stream->i_idxposb += avi_pk.i_size;
1085 if( p_stream->i_cat == AUDIO_ES )
1087 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1089 p_stream->i_idxposc++;
1099 /*****************************************************************************
1100 * Seek: goto to i_date or i_percent
1101 *****************************************************************************/
1102 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1105 demux_sys_t *p_sys = p_demux->p_sys;
1106 unsigned int i_stream;
1107 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1108 i_date / 1000000, i_percent );
1110 if( p_sys->b_seekable )
1112 if( !p_sys->i_length )
1114 avi_track_t *p_stream;
1117 /* use i_percent to create a true i_date */
1118 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1119 " work only for interleaved file", i_percent );
1120 if( i_percent >= 100 )
1122 msg_Warn( p_demux, "cannot seek so far !" );
1123 return VLC_EGENERIC;
1125 i_percent = __MAX( i_percent, 0 );
1127 /* try to find chunk that is at i_percent or the file */
1128 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1129 p_sys->i_movi_begin );
1130 /* search first selected stream */
1131 for( i_stream = 0, p_stream = NULL;
1132 i_stream < p_sys->i_track; i_stream++ )
1134 p_stream = p_sys->track[i_stream];
1135 if( p_stream->b_activated )
1140 if( !p_stream || !p_stream->b_activated )
1142 msg_Warn( p_demux, "cannot find any selected stream" );
1143 return VLC_EGENERIC;
1146 /* be sure that the index exist */
1147 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1149 msg_Warn( p_demux, "cannot seek" );
1150 return VLC_EGENERIC;
1153 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1154 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1156 /* search after i_idxposc */
1157 if( AVI_StreamChunkSet( p_demux,
1158 i_stream, p_stream->i_idxposc + 1 ) )
1160 msg_Warn( p_demux, "cannot seek" );
1161 return VLC_EGENERIC;
1165 i_date = AVI_GetPTS( p_stream );
1166 /* TODO better support for i_samplesize != 0 */
1167 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1170 #define p_stream p_sys->track[i_stream]
1172 /* seek for chunk based streams */
1173 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1175 if( p_stream->b_activated && !p_stream->i_samplesize )
1176 /* if( p_stream->b_activated ) */
1178 AVI_TrackSeek( p_demux, i_stream, i_date );
1179 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1186 i_date = p_sys->i_time;
1188 /* seek for bytes based streams */
1189 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1191 if( p_stream->b_activated && p_stream->i_samplesize )
1193 AVI_TrackSeek( p_demux, i_stream, i_date );
1194 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1197 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1198 /* set true movie time */
1200 if( !p_sys->i_time )
1202 p_sys->i_time = i_date;
1209 msg_Err( p_demux, "shouldn't yet be executed" );
1210 return VLC_EGENERIC;
1214 /*****************************************************************************
1216 *****************************************************************************
1218 *****************************************************************************/
1219 static double ControlGetPosition( demux_t *p_demux )
1221 demux_sys_t *p_sys = p_demux->p_sys;
1223 if( p_sys->i_length > 0 )
1225 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1227 else if( stream_Size( p_demux->s ) > 0 )
1233 /* search the more advanced selected es */
1234 for( i = 0; i < p_sys->i_track; i++ )
1236 avi_track_t *tk = p_sys->track[i];
1237 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1239 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1240 tk->p_index[tk->i_idxposc].i_length + 8;
1247 return (double)i64 / (double)stream_Size( p_demux->s );
1252 static int Control( demux_t *p_demux, int i_query, va_list args )
1254 demux_sys_t *p_sys = p_demux->p_sys;
1258 vlc_meta_t **pp_meta;
1262 case DEMUX_GET_POSITION:
1263 pf = (double*)va_arg( args, double * );
1264 *pf = ControlGetPosition( p_demux );
1266 case DEMUX_SET_POSITION:
1267 f = (double)va_arg( args, double );
1268 if( p_sys->b_seekable )
1270 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1271 return Seek( p_demux, i64, (int)(f * 100) );
1275 int64_t i_pos = stream_Size( p_demux->s ) * f;
1276 return stream_Seek( p_demux->s, i_pos );
1279 case DEMUX_GET_TIME:
1280 pi64 = (int64_t*)va_arg( args, int64_t * );
1281 *pi64 = p_sys->i_time;
1284 case DEMUX_SET_TIME:
1288 i64 = (int64_t)va_arg( args, int64_t );
1289 if( p_sys->i_length > 0 )
1291 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1293 else if( p_sys->i_time > 0 )
1295 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1296 (double)i64 / (double)p_sys->i_time );
1298 return Seek( p_demux, i64, i_percent );
1300 case DEMUX_GET_LENGTH:
1301 pi64 = (int64_t*)va_arg( args, int64_t * );
1302 *pi64 = p_sys->i_length * (mtime_t)1000000;
1306 pf = (double*)va_arg( args, double * );
1308 for( i = 0; i < (int)p_sys->i_track; i++ )
1310 avi_track_t *tk = p_sys->track[i];
1311 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1313 *pf = (float)tk->i_rate / (float)tk->i_scale;
1318 case DEMUX_GET_META:
1319 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1320 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1324 return VLC_EGENERIC;
1328 /*****************************************************************************
1329 * Function to convert pts to chunk or byte
1330 *****************************************************************************/
1332 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1334 return (mtime_t)((int64_t)i_pts *
1335 (int64_t)tk->i_rate /
1336 (int64_t)tk->i_scale /
1339 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1341 return (mtime_t)((int64_t)i_pts *
1342 (int64_t)tk->i_rate /
1343 (int64_t)tk->i_scale /
1345 (int64_t)tk->i_samplesize );
1348 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1352 i_dpts = (mtime_t)( (int64_t)1000000 *
1354 (int64_t)tk->i_scale /
1355 (int64_t)tk->i_rate );
1357 if( tk->i_samplesize )
1359 return i_dpts / tk->i_samplesize;
1364 static mtime_t AVI_GetPTS( avi_track_t *tk )
1366 if( tk->i_samplesize )
1368 int64_t i_count = 0;
1370 /* we need a valid entry we will emulate one */
1371 if( tk->i_idxposc == tk->i_idxnb )
1375 /* use the last entry */
1376 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1377 + tk->p_index[tk->i_idxnb - 1].i_length;
1382 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1384 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1388 if( tk->i_cat == AUDIO_ES )
1390 return AVI_GetDPTS( tk, tk->i_blockno );
1394 return AVI_GetDPTS( tk, tk->i_idxposc );
1399 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1401 demux_sys_t *p_sys = p_demux->p_sys;
1402 avi_packet_t avi_pk;
1403 int i_loop_count = 0;
1405 /* find first chunk of i_stream that isn't in index */
1407 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1409 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1410 if( AVI_PacketNext( p_demux ) )
1412 return VLC_EGENERIC;
1417 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1422 if( p_demux->b_die ) return VLC_EGENERIC;
1424 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1426 msg_Warn( p_demux, "cannot get packet header" );
1427 return VLC_EGENERIC;
1429 if( avi_pk.i_stream >= p_sys->i_track ||
1430 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1432 if( AVI_PacketNext( p_demux ) )
1434 return VLC_EGENERIC;
1437 /* Prevents from eating all the CPU with broken files.
1438 * This value should be low enough so that it doesn't
1439 * affect the reading speed too much. */
1440 if( !(++i_loop_count % 1024) )
1442 if( p_demux->b_die ) return VLC_EGENERIC;
1445 if( !(i_loop_count % (1024 * 10)) )
1446 msg_Warn( p_demux, "don't seem to find any data..." );
1451 /* add this chunk to the index */
1454 index.i_id = avi_pk.i_fourcc;
1456 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1458 index.i_pos = avi_pk.i_pos;
1459 index.i_length = avi_pk.i_size;
1460 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1462 if( avi_pk.i_stream == i_stream )
1467 if( AVI_PacketNext( p_demux ) )
1469 return VLC_EGENERIC;
1476 /* be sure that i_ck will be a valid index entry */
1477 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1480 demux_sys_t *p_sys = p_demux->p_sys;
1481 avi_track_t *p_stream = p_sys->track[i_stream];
1483 p_stream->i_idxposc = i_ck;
1484 p_stream->i_idxposb = 0;
1486 if( i_ck >= p_stream->i_idxnb )
1488 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1491 p_stream->i_idxposc++;
1492 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1494 return VLC_EGENERIC;
1497 } while( p_stream->i_idxposc < i_ck );
1504 /* XXX FIXME up to now, we assume that all chunk are one after one */
1505 static int AVI_StreamBytesSet( demux_t *p_demux,
1506 unsigned int i_stream,
1509 demux_sys_t *p_sys = p_demux->p_sys;
1510 avi_track_t *p_stream = p_sys->track[i_stream];
1512 if( ( p_stream->i_idxnb > 0 )
1513 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1514 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1516 /* index is valid to find the ck */
1517 /* uses dichototmie to be fast enougth */
1518 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1519 int i_idxmax = p_stream->i_idxnb;
1523 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1525 i_idxmax = i_idxposc ;
1526 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1530 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1531 p_stream->p_index[i_idxposc].i_length <= i_byte)
1533 i_idxmin = i_idxposc ;
1534 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1538 p_stream->i_idxposc = i_idxposc;
1539 p_stream->i_idxposb = i_byte -
1540 p_stream->p_index[i_idxposc].i_lengthtotal;
1549 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1550 p_stream->i_idxposb = 0;
1553 p_stream->i_idxposc++;
1554 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1556 return VLC_EGENERIC;
1559 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1560 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1562 p_stream->i_idxposb = i_byte -
1563 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1568 static int AVI_TrackSeek( demux_t *p_demux,
1572 demux_sys_t *p_sys = p_demux->p_sys;
1573 avi_track_t *tk = p_sys->track[i_stream];
1575 #define p_stream p_sys->track[i_stream]
1578 i_oldpts = AVI_GetPTS( p_stream );
1580 if( !p_stream->i_samplesize )
1582 if( AVI_StreamChunkSet( p_demux,
1584 AVI_PTSToChunk( p_stream, i_date ) ) )
1586 return VLC_EGENERIC;
1589 if( p_stream->i_cat == AUDIO_ES )
1593 for( i = 0; i < tk->i_idxposc; i++ )
1595 if( tk->i_blocksize > 0 )
1597 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1607 "old:"I64Fd" %s new "I64Fd,
1609 i_oldpts > i_date ? ">" : "<",
1612 if( p_stream->i_cat == VIDEO_ES )
1614 /* search key frame */
1615 if( i_date < i_oldpts )
1617 while( p_stream->i_idxposc > 0 &&
1618 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1621 if( AVI_StreamChunkSet( p_demux,
1623 p_stream->i_idxposc - 1 ) )
1625 return VLC_EGENERIC;
1631 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1632 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1635 if( AVI_StreamChunkSet( p_demux,
1637 p_stream->i_idxposc + 1 ) )
1639 return VLC_EGENERIC;
1647 if( AVI_StreamBytesSet( p_demux,
1649 AVI_PTSToByte( p_stream, i_date ) ) )
1651 return VLC_EGENERIC;
1658 /****************************************************************************
1659 * Return VLC_TRUE if it's a key frame
1660 ****************************************************************************/
1661 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1667 * startcode: 0x00000100 32bits
1668 * framenumber ? 5bits
1669 * piture type 0(I),1(P) 2bits
1671 if( GetDWBE( p_byte ) != 0x00000100 )
1673 /* it's not an msmpegv1 stream, strange...*/
1674 return AVIIF_KEYFRAME;
1676 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1679 case FOURCC_DIV3: /* wmv1 also */
1681 * picture type 0(I),1(P) 2bits
1683 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1685 /* we should find first occurrence of 0x000001b6 (32bits)
1686 * startcode: 0x000001b6 32bits
1687 * piture type 0(I),1(P) 2bits
1689 if( GetDWBE( p_byte ) != 0x000001b6 )
1691 /* not true , need to find the first VOP header */
1692 return AVIIF_KEYFRAME;
1694 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1697 /* I can't do it, so say yes */
1698 return AVIIF_KEYFRAME;
1702 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1707 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1711 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1715 return VLC_FOURCC('m','r','l','e');
1764 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1768 /****************************************************************************
1770 ****************************************************************************/
1771 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1772 int *pi_number, int *pi_type )
1774 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1777 c1 = ((uint8_t *)&i_id)[0];
1778 c2 = ((uint8_t *)&i_id)[1];
1780 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1782 SET_PTR( pi_number, 100 ); /* > max stream number */
1783 SET_PTR( pi_type, UNKNOWN_ES );
1787 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1788 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1791 SET_PTR( pi_type, AUDIO_ES );
1795 SET_PTR( pi_type, VIDEO_ES );
1798 SET_PTR( pi_type, UNKNOWN_ES );
1805 /****************************************************************************
1807 ****************************************************************************/
1808 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1812 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1814 return VLC_EGENERIC;
1816 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1817 p_pk->i_size = GetDWLE( p_peek + 4 );
1818 p_pk->i_pos = stream_Tell( p_demux->s );
1819 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1821 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1822 p_peek[10], p_peek[11] );
1829 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1831 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1835 static int AVI_PacketNext( demux_t *p_demux )
1837 avi_packet_t avi_ck;
1840 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1842 return VLC_EGENERIC;
1845 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1846 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1850 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1851 avi_ck.i_type == AVIFOURCC_AVIX )
1857 i_skip = __EVEN( avi_ck.i_size ) + 8;
1860 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1862 return VLC_EGENERIC;
1866 static int AVI_PacketRead( demux_t *p_demux,
1868 block_t **pp_frame )
1872 i_size = __EVEN( p_pk->i_size + 8 );
1874 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1876 return VLC_EGENERIC;
1878 (*pp_frame)->p_buffer += 8;
1879 (*pp_frame)->i_buffer -= 8;
1881 if( i_size != p_pk->i_size + 8 )
1883 (*pp_frame)->i_buffer--;
1889 static int AVI_PacketSearch( demux_t *p_demux )
1891 demux_sys_t *p_sys = p_demux->p_sys;
1892 avi_packet_t avi_pk;
1897 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1899 return VLC_EGENERIC;
1901 AVI_PacketGetHeader( p_demux, &avi_pk );
1902 if( avi_pk.i_stream < p_sys->i_track &&
1903 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1907 switch( avi_pk.i_fourcc )
1909 case AVIFOURCC_JUNK:
1910 case AVIFOURCC_LIST:
1911 case AVIFOURCC_RIFF:
1912 case AVIFOURCC_idx1:
1916 /* Prevents from eating all the CPU with broken files.
1917 * This value should be low enough so that it doesn't affect the
1918 * reading speed too much (not that we care much anyway because
1919 * this code is called only on broken files). */
1920 if( !(++i_count % 1024) )
1922 if( p_demux->b_die ) return VLC_EGENERIC;
1925 if( !(i_count % (1024 * 10)) )
1926 msg_Warn( p_demux, "trying to resync..." );
1931 /****************************************************************************
1933 ****************************************************************************/
1934 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1936 avi_entry_t *p_index)
1938 avi_track_t *tk = p_sys->track[i_stream];
1940 /* Update i_movi_lastchunk_pos */
1941 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1943 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
1947 if( tk->i_idxnb >= tk->i_idxmax )
1949 tk->i_idxmax += 16384;
1950 tk->p_index = realloc( tk->p_index,
1951 tk->i_idxmax * sizeof( avi_entry_t ) );
1952 if( tk->p_index == NULL )
1957 /* calculate cumulate length */
1958 if( tk->i_idxnb > 0 )
1960 p_index->i_lengthtotal =
1961 tk->p_index[tk->i_idxnb - 1].i_length +
1962 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
1966 p_index->i_lengthtotal = 0;
1969 tk->p_index[tk->i_idxnb++] = *p_index;
1972 static int AVI_IndexLoad_idx1( demux_t *p_demux )
1974 demux_sys_t *p_sys = p_demux->p_sys;
1976 avi_chunk_list_t *p_riff;
1977 avi_chunk_list_t *p_movi;
1978 avi_chunk_idx1_t *p_idx1;
1980 unsigned int i_stream;
1981 unsigned int i_index;
1985 vlc_bool_t b_keyset[100];
1987 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
1988 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
1989 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
1993 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
1994 return VLC_EGENERIC;
1997 /* *** calculate offset *** */
1998 /* Well, avi is __SHIT__ so test more than one entry
1999 * (needed for some avi files) */
2001 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2003 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2005 i_offset = p_movi->i_chunk_pos + 8;
2010 /* Reset b_keyset */
2011 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2012 b_keyset[i_stream] = VLC_FALSE;
2014 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2018 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2021 if( i_stream < p_sys->i_track &&
2022 i_cat == p_sys->track[i_stream]->i_cat )
2025 index.i_id = p_idx1->entry[i_index].i_fourcc;
2027 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2028 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2029 index.i_length = p_idx1->entry[i_index].i_length;
2030 AVI_IndexAddEntry( p_sys, i_stream, &index );
2032 if( index.i_flags&AVIIF_KEYFRAME )
2033 b_keyset[i_stream] = VLC_TRUE;
2037 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2039 if( !b_keyset[i_stream] )
2041 avi_track_t *tk = p_sys->track[i_stream];
2043 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2044 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2045 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2051 static void __Parse_indx( demux_t *p_demux,
2053 avi_chunk_indx_t *p_indx )
2055 demux_sys_t *p_sys = p_demux->p_sys;
2059 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2060 if( p_indx->i_indexsubtype == 0 )
2062 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2064 index.i_id = p_indx->i_id;
2065 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2066 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2067 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2069 AVI_IndexAddEntry( p_sys, i_stream, &index );
2072 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2074 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2076 index.i_id = p_indx->i_id;
2077 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2078 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2079 index.i_length = p_indx->idx.field[i].i_size;
2081 AVI_IndexAddEntry( p_sys, i_stream, &index );
2086 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2090 static void AVI_IndexLoad_indx( demux_t *p_demux )
2092 demux_sys_t *p_sys = p_demux->p_sys;
2093 unsigned int i_stream;
2096 avi_chunk_list_t *p_riff;
2097 avi_chunk_list_t *p_hdrl;
2099 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2100 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2102 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2104 avi_chunk_list_t *p_strl;
2105 avi_chunk_indx_t *p_indx;
2107 #define p_stream p_sys->track[i_stream]
2108 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2109 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2113 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2117 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2119 __Parse_indx( p_demux, i_stream, p_indx );
2121 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2124 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2126 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2127 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2131 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2136 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2142 static void AVI_IndexLoad( demux_t *p_demux )
2144 demux_sys_t *p_sys = p_demux->p_sys;
2145 unsigned int i_stream;
2147 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2149 p_sys->track[i_stream]->i_idxnb = 0;
2150 p_sys->track[i_stream]->i_idxmax = 0;
2151 p_sys->track[i_stream]->p_index = NULL;
2156 AVI_IndexLoad_indx( p_demux );
2158 else if( AVI_IndexLoad_idx1( p_demux ) )
2160 /* try indx if idx1 failed as some "normal" file have indx too */
2161 AVI_IndexLoad_indx( p_demux );
2164 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2166 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2167 i_stream, p_sys->track[i_stream]->i_idxnb );
2171 static void AVI_IndexCreate( demux_t *p_demux )
2173 demux_sys_t *p_sys = p_demux->p_sys;
2175 avi_chunk_list_t *p_riff;
2176 avi_chunk_list_t *p_movi;
2178 unsigned int i_stream;
2181 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2182 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2186 msg_Err( p_demux, "cannot find p_movi" );
2190 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2192 p_sys->track[i_stream]->i_idxnb = 0;
2193 p_sys->track[i_stream]->i_idxmax = 0;
2194 p_sys->track[i_stream]->p_index = NULL;
2196 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2197 stream_Size( p_demux->s ) );
2199 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2200 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2205 if( p_demux->b_die )
2210 if( AVI_PacketGetHeader( p_demux, &pk ) )
2214 if( pk.i_stream < p_sys->i_track &&
2215 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2218 index.i_id = pk.i_fourcc;
2220 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2221 index.i_pos = pk.i_pos;
2222 index.i_length = pk.i_size;
2223 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2227 switch( pk.i_fourcc )
2229 case AVIFOURCC_idx1:
2232 avi_chunk_list_t *p_sysx;
2233 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2234 AVIFOURCC_RIFF, 1 );
2236 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2237 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2244 case AVIFOURCC_RIFF:
2245 msg_Dbg( p_demux, "new RIFF chunk found" );
2247 case AVIFOURCC_JUNK:
2250 msg_Warn( p_demux, "need resync, probably broken avi" );
2251 if( AVI_PacketSearch( p_demux ) )
2253 msg_Warn( p_demux, "lost sync, abord index creation" );
2259 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2260 AVI_PacketNext( p_demux ) )
2267 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2270 "stream[%d] creating %d index entries",
2272 p_sys->track[i_stream]->i_idxnb );
2276 /*****************************************************************************
2278 *****************************************************************************/
2279 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2281 demux_sys_t *p_sys = p_demux->p_sys;
2283 int b_end = VLC_TRUE;
2285 for( i = 0; i < p_sys->i_track; i++ )
2287 avi_track_t *tk = p_sys->track[i];
2288 if( tk->i_idxposc >= tk->i_idxnb )
2290 tk->b_activated = VLC_FALSE;
2291 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2301 /****************************************************************************
2302 * AVI_MovieGetLength give max streams length in second
2303 ****************************************************************************/
2304 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2306 demux_sys_t *p_sys = p_demux->p_sys;
2307 mtime_t i_maxlength = 0;
2310 for( i = 0; i < p_sys->i_track; i++ )
2312 avi_track_t *tk = p_sys->track[i];
2315 /* fix length for each stream */
2316 if( tk->i_idxnb < 1 || !tk->p_index )
2321 if( tk->i_samplesize )
2323 i_length = AVI_GetDPTS( tk,
2324 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2325 tk->p_index[tk->i_idxnb-1].i_length );
2329 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2331 i_length /= (mtime_t)1000000; /* in seconds */
2334 "stream[%d] length:"I64Fd" (based on index)",
2337 i_maxlength = __MAX( i_maxlength, i_length );