1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
32 #include <vlc_demux.h>
34 #include <vlc_interface.h>
37 #include <vlc_codecs.h>
38 #include <vlc_charset.h>
42 /*****************************************************************************
44 *****************************************************************************/
46 #define INTERLEAVE_TEXT N_("Force interleaved method" )
47 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
49 #define INDEX_TEXT N_("Force index creation")
50 #define INDEX_LONGTEXT N_( \
51 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
52 "or incomplete (not seekable)." )
54 static int Open ( vlc_object_t * );
55 static void Close( vlc_object_t * );
57 static int pi_index[] = {0,1,2};
59 static const char *ppsz_indexes[] = { N_("Ask"), N_("Always fix"),
63 set_shortname( "AVI" );
64 set_description( _("AVI demuxer") );
65 set_capability( "demux2", 212 );
66 set_category( CAT_INPUT );
67 set_subcategory( SUBCAT_INPUT_DEMUX );
69 add_bool( "avi-interleaved", 0, NULL,
70 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
71 add_integer( "avi-index", 0, NULL,
72 INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE );
73 change_integer_list( pi_index, ppsz_indexes, 0 );
75 set_callbacks( Open, Close );
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Control ( demux_t *, int, va_list );
82 static int Seek ( demux_t *, mtime_t, int );
83 static int Demux_Seekable ( demux_t * );
84 static int Demux_UnSeekable( demux_t * );
86 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
90 vlc_fourcc_t i_fourcc;
93 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
95 uint8_t i_peek[8]; /* first 8 bytes */
97 unsigned int i_stream;
108 uint32_t i_lengthtotal;
114 vlc_bool_t b_activated;
116 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
117 vlc_fourcc_t i_codec;
126 avi_entry_t *p_index;
127 unsigned int i_idxnb;
128 unsigned int i_idxmax;
130 unsigned int i_idxposc; /* numero of chunk */
131 unsigned int i_idxposb; /* byte in the current chunk */
133 /* For VBR audio only */
134 unsigned int i_blockno;
135 unsigned int i_blocksize;
137 /* For muxed streams */
138 stream_t *p_out_muxed;
146 vlc_bool_t b_seekable;
153 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
155 /* number of streams and information */
156 unsigned int i_track;
167 static inline off_t __EVEN( off_t i )
169 return (i & 1) ? i + 1 : i;
172 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
173 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
174 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
175 static mtime_t AVI_GetPTS ( avi_track_t * );
178 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
179 static int AVI_StreamChunkSet ( demux_t *,
180 unsigned int i_stream, unsigned int i_ck );
181 static int AVI_StreamBytesSet ( demux_t *,
182 unsigned int i_stream, off_t i_byte );
184 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
185 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
187 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
188 static int AVI_PacketNext ( demux_t * );
189 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
190 static int AVI_PacketSearch ( demux_t * );
192 static void AVI_IndexLoad ( demux_t * );
193 static void AVI_IndexCreate ( demux_t * );
194 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
196 static mtime_t AVI_MovieGetLength( demux_t * );
198 /*****************************************************************************
200 *****************************************************************************/
201 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
202 static int AVI_TrackStopFinishedStreams( demux_t *);
205 - For VBR mp3 stream:
206 count blocks by rounded-up chunksizes instead of chunks
207 we need full emulation of dshow avi demuxer bugs :(
208 fixes silly nandub-style a-v delaying in avi with vbr mp3...
209 (from mplayer 2002/08/02)
213 /*****************************************************************************
214 * Open: check file and initializes AVI structures
215 *****************************************************************************/
216 static int Open( vlc_object_t * p_this )
218 demux_t *p_demux = (demux_t *)p_this;
221 vlc_bool_t b_index = VLC_FALSE;
225 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
226 avi_chunk_list_t *p_hdrl, *p_movi;
227 avi_chunk_avih_t *p_avih;
229 unsigned int i_track;
230 unsigned int i, i_peeker;
232 const uint8_t *p_peek;
234 /* Is it an avi file ? */
235 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
237 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
239 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
241 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
245 if( i_peeker == 188 )
250 /* Initialize input structures. */
251 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
252 memset( p_sys, 0, sizeof( demux_sys_t ) );
255 p_sys->i_movi_lastchunk_pos = 0;
256 p_sys->b_odml = VLC_FALSE;
257 p_sys->b_muxed = VLC_FALSE;
262 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
264 p_demux->pf_control = Control;
265 p_demux->pf_demux = Demux_Seekable;
267 /* For unseekable stream, automaticaly use Demux_UnSeekable */
268 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
270 p_demux->pf_demux = Demux_UnSeekable;
275 stream_Read( p_demux->s, NULL, i_peeker );
278 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
280 msg_Err( p_demux, "avi module discarded (invalid file)" );
284 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
286 unsigned int i_count =
287 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
289 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
290 for( i = 1; i < i_count; i++ )
292 avi_chunk_list_t *p_sysx;
294 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
295 if( p_sysx->i_type == AVIFOURCC_AVIX )
297 msg_Warn( p_demux, "detected OpenDML file" );
298 p_sys->b_odml = VLC_TRUE;
304 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
305 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
306 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
308 if( !p_hdrl || !p_movi )
310 msg_Err( p_demux, "avi module discarded (invalid file)" );
314 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
316 msg_Err( p_demux, "cannot find avih chunk" );
319 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
320 if( p_avih->i_streams != i_track )
323 "found %d stream but %d are declared",
324 i_track, p_avih->i_streams );
328 msg_Err( p_demux, "no stream defined!" );
332 /* print information on streams */
333 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
335 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
336 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
337 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
338 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
339 if( ( p_sys->meta = vlc_meta_New() ) )
342 sprintf( buffer, "%s%s%s%s",
343 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
344 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
345 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
346 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
347 vlc_meta_SetSetting( p_sys->meta, buffer );
350 /* now read info on each stream and create ES */
351 for( i = 0 ; i < i_track; i++ )
353 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
354 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
355 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
356 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
357 avi_chunk_strf_auds_t *p_auds = NULL;
358 avi_chunk_strf_vids_t *p_vids = NULL;
361 tk->b_activated = VLC_FALSE;
372 tk->p_out_muxed = NULL;
374 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
375 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
377 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
379 msg_Warn( p_demux, "stream[%d] incomplete", i );
383 tk->i_rate = p_strh->i_rate;
384 tk->i_scale = p_strh->i_scale;
385 tk->i_samplesize = p_strh->i_samplesize;
386 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
387 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
389 switch( p_strh->i_type )
391 case( AVIFOURCC_auds ):
392 tk->i_cat = AUDIO_ES;
393 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
394 p_auds->p_wf->wFormatTag );
395 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
397 if( p_auds->p_wf->wFormatTag == 1 )
399 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
406 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
408 fmt.audio.i_channels = p_auds->p_wf->nChannels;
409 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
410 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
411 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
412 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
413 fmt.b_packetized = !tk->i_blocksize;
414 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
415 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
416 fmt.p_extra = &p_auds->p_wf[1];
417 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
418 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
419 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
422 case( AVIFOURCC_vids ):
423 tk->i_cat = VIDEO_ES;
424 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
425 p_vids->p_bih->biCompression );
426 if( p_vids->p_bih->biCompression == 0x00 )
428 switch( p_vids->p_bih->biBitCount )
431 tk->i_codec = VLC_FOURCC('R','V','3','2');
434 tk->i_codec = VLC_FOURCC('R','V','2','4');
437 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
440 tk->i_codec = VLC_FOURCC('R','V','1','5');
443 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
446 tk->i_codec = VLC_FOURCC('Y','8','0','0');
449 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
451 if( p_vids->p_bih->biBitCount == 24 )
453 /* This is in BGR format */
454 fmt.video.i_bmask = 0x00ff0000;
455 fmt.video.i_gmask = 0x0000ff00;
456 fmt.video.i_rmask = 0x000000ff;
461 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
462 if( tk->i_codec == FOURCC_mp4v &&
463 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
465 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
468 tk->i_samplesize = 0;
469 fmt.video.i_width = p_vids->p_bih->biWidth;
470 fmt.video.i_height = p_vids->p_bih->biHeight;
471 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
472 fmt.video.i_frame_rate = tk->i_rate;
473 fmt.video.i_frame_rate_base = tk->i_scale;
475 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
476 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
477 fmt.p_extra = &p_vids->p_bih[1];
478 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
479 i, (char*)&p_vids->p_bih->biCompression,
480 p_vids->p_bih->biWidth,
481 p_vids->p_bih->biHeight,
482 p_vids->p_bih->biBitCount,
483 (float)tk->i_rate/(float)tk->i_scale );
485 if( p_vids->p_bih->biCompression == 0x00 )
487 /* RGB DIB are coded from bottom to top */
489 (unsigned int)(-(int)p_vids->p_bih->biHeight);
492 /* Extract palette from extradata if bpp <= 8
493 * (assumes that extradata contains only palette but appears
494 * to be true for all palettized codecs we support) */
495 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
496 fmt.video.i_bits_per_pixel > 0 )
500 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
501 fmt.video.p_palette->i_entries = 1;
503 /* Apparently this is necessary. But why ? */
505 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
506 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
508 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
509 GetDWLE((uint32_t*)fmt.p_extra + i);
514 case( AVIFOURCC_txts):
516 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
517 msg_Dbg( p_demux, "stream[%d] subtitles", i );
518 es_format_Init( &fmt, SPU_ES, tk->i_codec );
521 case( AVIFOURCC_iavs):
522 case( AVIFOURCC_ivas):
523 p_sys->b_muxed = VLC_TRUE;
524 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
525 if( p_strh->i_handler == FOURCC_dvsd ||
526 p_strh->i_handler == FOURCC_dvhd ||
527 p_strh->i_handler == FOURCC_dvsl )
529 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
530 if( !tk->p_out_muxed )
531 msg_Err( p_demux, "could not load the DV parser" );
537 case( AVIFOURCC_mids):
538 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
541 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
547 /* The charset of p_strn is undefined */
548 EnsureUTF8( p_strn->p_str );
549 fmt.psz_description = strdup( p_strn->p_str );
551 if( tk->p_out_muxed == NULL )
552 tk->p_es = es_out_Add( p_demux->out, &fmt );
553 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
556 if( p_sys->i_track <= 0 )
558 msg_Err( p_demux, "no valid track" );
562 i_do_index = config_GetInt( p_demux, "avi-index" );
563 if( i_do_index == 1 ) /* Always fix */
566 if( p_sys->b_seekable )
568 AVI_IndexCreate( p_demux );
572 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
573 AVI_IndexLoad( p_demux );
578 AVI_IndexLoad( p_demux );
581 /* *** movie length in sec *** */
582 p_sys->i_length = AVI_MovieGetLength( p_demux );
583 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
584 (mtime_t)p_avih->i_microsecperframe /
587 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
588 "axproximative or will have strange behaviour" );
589 if( i_do_index == 0 && !b_index )
592 i_create = intf_UserYesNo( p_demux, _("AVI Index") ,
593 _( "This AVI file is broken. Seeking will not "
594 "work correctly.\nDo you want to "
595 "try to repair it?\n\nThis might take a long time." ),
596 _( "Repair" ), _( "Don't repair" ), _( "Cancel") );
597 if( i_create == DIALOG_OK_YES )
600 msg_Dbg( p_demux, "Fixing AVI index" );
603 else if( i_create == DIALOG_CANCELLED )
606 vlc_object_kill( p_demux->p_parent );
612 /* fix some BeOS MediaKit generated file */
613 for( i = 0 ; i < p_sys->i_track; i++ )
615 avi_track_t *tk = p_sys->track[i];
616 avi_chunk_list_t *p_strl;
617 avi_chunk_strh_t *p_strh;
618 avi_chunk_strf_auds_t *p_auds;
620 if( tk->i_cat != AUDIO_ES )
624 if( tk->i_idxnb < 1 ||
626 tk->i_samplesize != 0 )
630 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
631 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
632 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
634 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
635 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
637 int64_t i_track_length =
638 tk->p_index[tk->i_idxnb-1].i_length +
639 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
640 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
641 (mtime_t)p_avih->i_microsecperframe;
645 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
648 tk->i_samplesize = 1;
649 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
650 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
654 if( p_sys->b_seekable )
656 /* we have read all chunk so go back to movi */
657 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
659 /* Skip movi header */
660 stream_Read( p_demux->s, NULL, 12 );
662 p_sys->i_movi_begin = p_movi->i_chunk_pos;
668 vlc_meta_Delete( p_sys->meta );
670 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
675 /*****************************************************************************
676 * Close: frees unused data
677 *****************************************************************************/
678 static void Close ( vlc_object_t * p_this )
680 demux_t * p_demux = (demux_t *)p_this;
682 demux_sys_t *p_sys = p_demux->p_sys ;
684 for( i = 0; i < p_sys->i_track; i++ )
686 if( p_sys->track[i] )
688 if( p_sys->track[i]->p_out_muxed )
689 stream_DemuxDelete( p_sys->track[i]->p_out_muxed );
690 FREENULL( p_sys->track[i]->p_index );
691 free( p_sys->track[i] );
694 FREENULL( p_sys->track );
695 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
696 vlc_meta_Delete( p_sys->meta );
701 /*****************************************************************************
702 * Demux_Seekable: reads and demuxes data packets for stream seekable
703 *****************************************************************************
704 * AVIDemux: reads and demuxes data packets
705 *****************************************************************************
706 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
707 *****************************************************************************/
714 off_t i_posf; /* where we will read :
715 if i_idxposb == 0 : begining of chunk (+8 to acces data)
716 else : point on data directly */
717 } avi_track_toread_t;
719 static int Demux_Seekable( demux_t *p_demux )
721 demux_sys_t *p_sys = p_demux->p_sys;
723 unsigned int i_track_count = 0;
724 unsigned int i_track;
726 /* cannot be more than 100 stream (dcXX or wbXX) */
727 avi_track_toread_t toread[100];
730 /* detect new selected/unselected streams */
731 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
733 avi_track_t *tk = p_sys->track[i_track];
736 if( p_sys->b_muxed && tk->p_out_muxed )
739 tk->b_activated = VLC_TRUE;
743 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
744 if( b && !tk->b_activated )
746 if( p_sys->b_seekable)
748 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
750 tk->b_activated = VLC_TRUE;
752 else if( !b && tk->b_activated )
754 tk->b_activated = VLC_FALSE;
762 if( i_track_count <= 0 )
764 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
766 p_sys->i_time += 25*1000; /* read 25ms */
769 if( p_sys->i_time >= i_length )
773 msg_Warn( p_demux, "no track selected, exiting..." );
777 /* wait for the good time */
778 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
779 p_sys->i_time += 25*1000; /* read 25ms */
782 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
784 avi_track_t *tk = p_sys->track[i_track];
787 toread[i_track].b_ok = tk->b_activated;
788 if( tk->i_idxposc < tk->i_idxnb )
790 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
791 if( tk->i_idxposb > 0 )
793 toread[i_track].i_posf += 8 + tk->i_idxposb;
798 toread[i_track].i_posf = -1;
801 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
803 if( tk->i_samplesize )
805 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
809 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
814 toread[i_track].i_toread *= -1;
818 b_stream = VLC_FALSE;
829 /* search for first chunk to be read */
830 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
832 if( !toread[i].b_ok ||
833 AVI_GetDPTS( p_sys->track[i],
834 toread[i].i_toread ) <= -25 * 1000 )
839 if( toread[i].i_toread > 0 )
841 b_done = VLC_FALSE; /* not yet finished */
843 if( toread[i].i_posf > 0 )
845 if( i_pos == -1 || i_pos > toread[i].i_posf )
848 i_pos = toread[i].i_posf;
860 int i_loop_count = 0;
862 /* no valid index, we will parse directly the stream
863 * in case we fail we will disable all finished stream */
864 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
866 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
867 if( AVI_PacketNext( p_demux ) )
869 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
874 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
881 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
884 "cannot get packet header, track disabled" );
885 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
887 if( avi_pk.i_stream >= p_sys->i_track ||
888 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
890 if( AVI_PacketNext( p_demux ) )
893 "cannot skip packet, track disabled" );
894 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
897 /* Prevents from eating all the CPU with broken files.
898 * This value should be low enough so that it doesn't
899 * affect the reading speed too much. */
900 if( !(++i_loop_count % 1024) )
902 if( p_demux->b_die ) return -1;
905 if( !(i_loop_count % (1024 * 10)) )
907 "don't seem to find any data..." );
913 /* add this chunk to the index */
916 index.i_id = avi_pk.i_fourcc;
918 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
920 index.i_pos = avi_pk.i_pos;
921 index.i_length = avi_pk.i_size;
922 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
924 i_track = avi_pk.i_stream;
925 tk = p_sys->track[i_track];
926 /* do we will read this data ? */
927 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
933 if( AVI_PacketNext( p_demux ) )
936 "cannot skip packet, track disabled" );
937 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
946 stream_Seek( p_demux->s, i_pos );
949 /* Set the track to use */
950 tk = p_sys->track[i_track];
952 /* read thoses data */
953 if( tk->i_samplesize )
955 unsigned int i_toread;
957 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
959 if( tk->i_samplesize > 1 )
961 i_toread = tk->i_samplesize;
965 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
966 i_toread = __MAX( i_toread, 100 );
969 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
975 i_size = tk->p_index[tk->i_idxposc].i_length;
978 if( tk->i_idxposb == 0 )
980 i_size += 8; /* need to read and skip header */
983 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
985 msg_Warn( p_demux, "failed reading data" );
986 tk->b_activated = VLC_FALSE;
987 toread[i_track].b_ok = VLC_FALSE;
990 if( i_size % 2 ) /* read was padded on word boundary */
995 if( tk->i_idxposb == 0 )
997 p_frame->p_buffer += 8;
998 p_frame->i_buffer -= 8;
1000 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1001 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1003 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1007 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1011 if( tk->i_samplesize )
1013 if( tk->i_idxposb == 0 )
1017 toread[i_track].i_toread -= i_size;
1018 tk->i_idxposb += i_size;
1019 if( tk->i_idxposb >=
1020 tk->p_index[tk->i_idxposc].i_length )
1028 int i_length = tk->p_index[tk->i_idxposc].i_length;
1031 if( tk->i_cat == AUDIO_ES )
1033 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1035 toread[i_track].i_toread--;
1038 if( tk->i_idxposc < tk->i_idxnb)
1040 toread[i_track].i_posf =
1041 tk->p_index[tk->i_idxposc].i_pos;
1042 if( tk->i_idxposb > 0 )
1044 toread[i_track].i_posf += 8 + tk->i_idxposb;
1050 toread[i_track].i_posf = -1;
1053 b_stream = VLC_TRUE; /* at least one read succeed */
1055 if( tk->i_cat != VIDEO_ES )
1056 p_frame->i_dts = p_frame->i_pts;
1059 p_frame->i_dts = p_frame->i_pts;
1063 //p_pes->i_rate = p_demux->stream.control.i_rate;
1064 if( tk->p_out_muxed )
1065 stream_DemuxSend( tk->p_out_muxed, p_frame );
1067 es_out_Send( p_demux->out, tk->p_es, p_frame );
1072 /*****************************************************************************
1073 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1074 *****************************************************************************
1075 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1076 *****************************************************************************/
1077 static int Demux_UnSeekable( demux_t *p_demux )
1079 demux_sys_t *p_sys = p_demux->p_sys;
1080 avi_track_t *p_stream_master = NULL;
1081 unsigned int i_stream;
1082 unsigned int i_packet;
1084 if( p_sys->b_muxed )
1086 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1087 return VLC_EGENERIC;
1090 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1092 /* *** find master stream for data packet skipping algo *** */
1093 /* *** -> first video, if any, or first audio ES *** */
1094 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1096 avi_track_t *tk = p_sys->track[i_stream];
1099 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1101 if( b && tk->i_cat == VIDEO_ES )
1103 p_stream_master = tk;
1107 p_stream_master = tk;
1111 if( !p_stream_master )
1113 msg_Warn( p_demux, "no more stream selected" );
1117 p_sys->i_time = AVI_GetPTS( p_stream_master );
1119 for( i_packet = 0; i_packet < 10; i_packet++)
1121 #define p_stream p_sys->track[avi_pk.i_stream]
1123 avi_packet_t avi_pk;
1125 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1130 if( avi_pk.i_stream >= p_sys->i_track ||
1131 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1133 /* we haven't found an audio or video packet:
1134 * - we have seek, found first next packet
1135 * - others packets could be found, skip them
1137 switch( avi_pk.i_fourcc )
1139 case AVIFOURCC_JUNK:
1140 case AVIFOURCC_LIST:
1141 case AVIFOURCC_RIFF:
1142 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1143 case AVIFOURCC_idx1:
1146 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1148 return( 0 ); /* eof */
1151 "seems to have lost position, resync" );
1152 if( AVI_PacketSearch( p_demux ) )
1154 msg_Err( p_demux, "resync failed" );
1161 /* check for time */
1162 if( __ABS( AVI_GetPTS( p_stream ) -
1163 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1165 /* load it and send to decoder */
1167 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1171 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1173 if( avi_pk.i_cat != VIDEO_ES )
1174 p_frame->i_dts = p_frame->i_pts;
1177 p_frame->i_dts = p_frame->i_pts;
1181 //p_pes->i_rate = p_demux->stream.control.i_rate;
1182 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1186 if( AVI_PacketNext( p_demux ) )
1192 /* *** update stream time position *** */
1193 if( p_stream->i_samplesize )
1195 p_stream->i_idxposb += avi_pk.i_size;
1199 if( p_stream->i_cat == AUDIO_ES )
1201 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1203 p_stream->i_idxposc++;
1213 /*****************************************************************************
1214 * Seek: goto to i_date or i_percent
1215 *****************************************************************************/
1216 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1219 demux_sys_t *p_sys = p_demux->p_sys;
1220 unsigned int i_stream;
1221 msg_Dbg( p_demux, "seek requested: "I64Fd" seconds %d%%",
1222 i_date / 1000000, i_percent );
1224 if( p_sys->b_seekable )
1226 if( !p_sys->i_length )
1228 avi_track_t *p_stream;
1231 /* use i_percent to create a true i_date */
1232 msg_Warn( p_demux, "seeking without index at %d%%"
1233 " only works for interleaved files", i_percent );
1234 if( i_percent >= 100 )
1236 msg_Warn( p_demux, "cannot seek so far !" );
1237 return VLC_EGENERIC;
1239 i_percent = __MAX( i_percent, 0 );
1241 /* try to find chunk that is at i_percent or the file */
1242 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1243 p_sys->i_movi_begin );
1244 /* search first selected stream */
1245 for( i_stream = 0, p_stream = NULL;
1246 i_stream < p_sys->i_track; i_stream++ )
1248 p_stream = p_sys->track[i_stream];
1249 if( p_stream->b_activated )
1254 if( !p_stream || !p_stream->b_activated )
1256 msg_Warn( p_demux, "cannot find any selected stream" );
1257 return VLC_EGENERIC;
1260 /* be sure that the index exist */
1261 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1263 msg_Warn( p_demux, "cannot seek" );
1264 return VLC_EGENERIC;
1267 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1268 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1270 /* search after i_idxposc */
1271 if( AVI_StreamChunkSet( p_demux,
1272 i_stream, p_stream->i_idxposc + 1 ) )
1274 msg_Warn( p_demux, "cannot seek" );
1275 return VLC_EGENERIC;
1279 i_date = AVI_GetPTS( p_stream );
1280 /* TODO better support for i_samplesize != 0 */
1281 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1285 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1287 avi_track_t *p_stream = p_sys->track[i_stream];
1289 if( !p_stream->b_activated )
1292 AVI_TrackSeek( p_demux, i_stream, i_date );
1294 p_sys->i_time = i_date;
1295 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1300 msg_Err( p_demux, "shouldn't yet be executed" );
1301 return VLC_EGENERIC;
1305 /*****************************************************************************
1307 *****************************************************************************/
1308 static double ControlGetPosition( demux_t *p_demux )
1310 demux_sys_t *p_sys = p_demux->p_sys;
1312 if( p_sys->i_length > 0 )
1314 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1316 else if( stream_Size( p_demux->s ) > 0 )
1322 /* search the more advanced selected es */
1323 for( i = 0; i < p_sys->i_track; i++ )
1325 avi_track_t *tk = p_sys->track[i];
1326 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1328 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1329 tk->p_index[tk->i_idxposc].i_length + 8;
1336 return (double)i64 / stream_Size( p_demux->s );
1341 static int Control( demux_t *p_demux, int i_query, va_list args )
1343 demux_sys_t *p_sys = p_demux->p_sys;
1351 case DEMUX_GET_POSITION:
1352 pf = (double*)va_arg( args, double * );
1353 *pf = ControlGetPosition( p_demux );
1355 case DEMUX_SET_POSITION:
1356 f = (double)va_arg( args, double );
1357 if( p_sys->b_seekable )
1359 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1360 return Seek( p_demux, i64, (int)(f * 100) );
1364 int64_t i_pos = stream_Size( p_demux->s ) * f;
1365 return stream_Seek( p_demux->s, i_pos );
1368 case DEMUX_GET_TIME:
1369 pi64 = (int64_t*)va_arg( args, int64_t * );
1370 *pi64 = p_sys->i_time;
1373 case DEMUX_SET_TIME:
1377 i64 = (int64_t)va_arg( args, int64_t );
1378 if( p_sys->i_length > 0 )
1380 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1382 else if( p_sys->i_time > 0 )
1384 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1385 (double)i64 / (double)p_sys->i_time );
1387 return Seek( p_demux, i64, i_percent );
1389 case DEMUX_GET_LENGTH:
1390 pi64 = (int64_t*)va_arg( args, int64_t * );
1391 *pi64 = p_sys->i_length * (mtime_t)1000000;
1395 pf = (double*)va_arg( args, double * );
1397 for( i = 0; i < (int)p_sys->i_track; i++ )
1399 avi_track_t *tk = p_sys->track[i];
1400 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1402 *pf = (float)tk->i_rate / (float)tk->i_scale;
1407 case DEMUX_GET_META:
1408 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1409 vlc_meta_Merge( p_meta, p_sys->meta );
1413 return VLC_EGENERIC;
1417 /*****************************************************************************
1418 * Function to convert pts to chunk or byte
1419 *****************************************************************************/
1421 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1426 return (mtime_t)((int64_t)i_pts *
1427 (int64_t)tk->i_rate /
1428 (int64_t)tk->i_scale /
1431 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1433 if( !tk->i_scale || !tk->i_samplesize )
1436 return (mtime_t)((int64_t)i_pts *
1437 (int64_t)tk->i_rate /
1438 (int64_t)tk->i_scale /
1440 (int64_t)tk->i_samplesize );
1443 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1450 i_dpts = (mtime_t)( (int64_t)1000000 *
1452 (int64_t)tk->i_scale /
1453 (int64_t)tk->i_rate );
1455 if( tk->i_samplesize )
1457 return i_dpts / tk->i_samplesize;
1462 static mtime_t AVI_GetPTS( avi_track_t *tk )
1464 if( tk->i_samplesize )
1466 int64_t i_count = 0;
1468 /* we need a valid entry we will emulate one */
1469 if( tk->i_idxposc == tk->i_idxnb )
1473 /* use the last entry */
1474 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1475 + tk->p_index[tk->i_idxnb - 1].i_length;
1480 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1482 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1486 if( tk->i_cat == AUDIO_ES )
1488 return AVI_GetDPTS( tk, tk->i_blockno );
1492 return AVI_GetDPTS( tk, tk->i_idxposc );
1497 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1499 demux_sys_t *p_sys = p_demux->p_sys;
1500 avi_packet_t avi_pk;
1501 int i_loop_count = 0;
1503 /* find first chunk of i_stream that isn't in index */
1505 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1507 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1508 if( AVI_PacketNext( p_demux ) )
1510 return VLC_EGENERIC;
1515 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1520 if( p_demux->b_die ) return VLC_EGENERIC;
1522 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1524 msg_Warn( p_demux, "cannot get packet header" );
1525 return VLC_EGENERIC;
1527 if( avi_pk.i_stream >= p_sys->i_track ||
1528 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1530 if( AVI_PacketNext( p_demux ) )
1532 return VLC_EGENERIC;
1535 /* Prevents from eating all the CPU with broken files.
1536 * This value should be low enough so that it doesn't
1537 * affect the reading speed too much. */
1538 if( !(++i_loop_count % 1024) )
1540 if( p_demux->b_die ) return VLC_EGENERIC;
1543 if( !(i_loop_count % (1024 * 10)) )
1544 msg_Warn( p_demux, "don't seem to find any data..." );
1549 /* add this chunk to the index */
1552 index.i_id = avi_pk.i_fourcc;
1554 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1556 index.i_pos = avi_pk.i_pos;
1557 index.i_length = avi_pk.i_size;
1558 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1560 if( avi_pk.i_stream == i_stream )
1565 if( AVI_PacketNext( p_demux ) )
1567 return VLC_EGENERIC;
1573 /* be sure that i_ck will be a valid index entry */
1574 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1577 demux_sys_t *p_sys = p_demux->p_sys;
1578 avi_track_t *p_stream = p_sys->track[i_stream];
1580 p_stream->i_idxposc = i_ck;
1581 p_stream->i_idxposb = 0;
1583 if( i_ck >= p_stream->i_idxnb )
1585 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1588 p_stream->i_idxposc++;
1589 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1591 return VLC_EGENERIC;
1594 } while( p_stream->i_idxposc < i_ck );
1600 /* XXX FIXME up to now, we assume that all chunk are one after one */
1601 static int AVI_StreamBytesSet( demux_t *p_demux,
1602 unsigned int i_stream,
1605 demux_sys_t *p_sys = p_demux->p_sys;
1606 avi_track_t *p_stream = p_sys->track[i_stream];
1608 if( ( p_stream->i_idxnb > 0 )
1609 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1610 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1612 /* index is valid to find the ck */
1613 /* uses dichototmie to be fast enougth */
1614 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1615 int i_idxmax = p_stream->i_idxnb;
1619 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1621 i_idxmax = i_idxposc ;
1622 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1626 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1627 p_stream->p_index[i_idxposc].i_length <= i_byte)
1629 i_idxmin = i_idxposc ;
1630 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1634 p_stream->i_idxposc = i_idxposc;
1635 p_stream->i_idxposb = i_byte -
1636 p_stream->p_index[i_idxposc].i_lengthtotal;
1645 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1646 p_stream->i_idxposb = 0;
1649 p_stream->i_idxposc++;
1650 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1652 return VLC_EGENERIC;
1655 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1656 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1658 p_stream->i_idxposb = i_byte -
1659 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1664 static int AVI_TrackSeek( demux_t *p_demux,
1668 demux_sys_t *p_sys = p_demux->p_sys;
1669 avi_track_t *tk = p_sys->track[i_stream];
1671 #define p_stream p_sys->track[i_stream]
1674 i_oldpts = AVI_GetPTS( p_stream );
1676 if( !p_stream->i_samplesize )
1678 if( AVI_StreamChunkSet( p_demux,
1680 AVI_PTSToChunk( p_stream, i_date ) ) )
1682 return VLC_EGENERIC;
1685 if( p_stream->i_cat == AUDIO_ES )
1689 for( i = 0; i < tk->i_idxposc; i++ )
1691 if( tk->i_blocksize > 0 )
1693 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1703 "old:"I64Fd" %s new "I64Fd,
1705 i_oldpts > i_date ? ">" : "<",
1708 if( p_stream->i_cat == VIDEO_ES )
1710 /* search key frame */
1711 //if( i_date < i_oldpts || 1 )
1713 while( p_stream->i_idxposc > 0 &&
1714 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1717 if( AVI_StreamChunkSet( p_demux,
1719 p_stream->i_idxposc - 1 ) )
1721 return VLC_EGENERIC;
1724 if( p_stream->p_es )
1725 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_stream->p_es, i_date );
1730 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1731 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1734 if( AVI_StreamChunkSet( p_demux,
1736 p_stream->i_idxposc + 1 ) )
1738 return VLC_EGENERIC;
1747 if( AVI_StreamBytesSet( p_demux,
1749 AVI_PTSToByte( p_stream, i_date ) ) )
1751 return VLC_EGENERIC;
1758 /****************************************************************************
1759 * Return VLC_TRUE if it's a key frame
1760 ****************************************************************************/
1761 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1767 * startcode: 0x00000100 32bits
1768 * framenumber ? 5bits
1769 * piture type 0(I),1(P) 2bits
1771 if( GetDWBE( p_byte ) != 0x00000100 )
1773 /* it's not an msmpegv1 stream, strange...*/
1774 return AVIIF_KEYFRAME;
1776 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1779 case FOURCC_DIV3: /* wmv1 also */
1781 * picture type 0(I),1(P) 2bits
1783 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1785 /* we should find first occurrence of 0x000001b6 (32bits)
1786 * startcode: 0x000001b6 32bits
1787 * piture type 0(I),1(P) 2bits
1789 if( GetDWBE( p_byte ) != 0x000001b6 )
1791 /* not true , need to find the first VOP header */
1792 return AVIIF_KEYFRAME;
1794 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1797 /* I can't do it, so say yes */
1798 return AVIIF_KEYFRAME;
1802 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1807 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1811 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1815 return VLC_FOURCC('m','r','l','e');
1864 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1868 /****************************************************************************
1870 ****************************************************************************/
1871 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1872 unsigned int *pi_number, unsigned int *pi_type )
1874 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1877 c1 = ((uint8_t *)&i_id)[0];
1878 c2 = ((uint8_t *)&i_id)[1];
1880 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1882 SET_PTR( pi_number, 100 ); /* > max stream number */
1883 SET_PTR( pi_type, UNKNOWN_ES );
1887 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1888 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1891 SET_PTR( pi_type, AUDIO_ES );
1895 SET_PTR( pi_type, VIDEO_ES );
1898 SET_PTR( pi_type, UNKNOWN_ES );
1905 /****************************************************************************
1907 ****************************************************************************/
1908 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1910 const uint8_t *p_peek;
1912 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1914 return VLC_EGENERIC;
1916 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1917 p_pk->i_size = GetDWLE( p_peek + 4 );
1918 p_pk->i_pos = stream_Tell( p_demux->s );
1919 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1921 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1922 p_peek[10], p_peek[11] );
1929 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1931 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1935 static int AVI_PacketNext( demux_t *p_demux )
1937 avi_packet_t avi_ck;
1940 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1942 return VLC_EGENERIC;
1945 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1946 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1950 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1951 avi_ck.i_type == AVIFOURCC_AVIX )
1957 i_skip = __EVEN( avi_ck.i_size ) + 8;
1960 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1962 return VLC_EGENERIC;
1967 static int AVI_PacketRead( demux_t *p_demux,
1969 block_t **pp_frame )
1973 i_size = __EVEN( p_pk->i_size + 8 );
1975 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1977 return VLC_EGENERIC;
1979 (*pp_frame)->p_buffer += 8;
1980 (*pp_frame)->i_buffer -= 8;
1982 if( i_size != p_pk->i_size + 8 )
1984 (*pp_frame)->i_buffer--;
1990 static int AVI_PacketSearch( demux_t *p_demux )
1992 demux_sys_t *p_sys = p_demux->p_sys;
1993 avi_packet_t avi_pk;
1998 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2000 return VLC_EGENERIC;
2002 AVI_PacketGetHeader( p_demux, &avi_pk );
2003 if( avi_pk.i_stream < p_sys->i_track &&
2004 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2008 switch( avi_pk.i_fourcc )
2010 case AVIFOURCC_JUNK:
2011 case AVIFOURCC_LIST:
2012 case AVIFOURCC_RIFF:
2013 case AVIFOURCC_idx1:
2017 /* Prevents from eating all the CPU with broken files.
2018 * This value should be low enough so that it doesn't affect the
2019 * reading speed too much (not that we care much anyway because
2020 * this code is called only on broken files). */
2021 if( !(++i_count % 1024) )
2023 if( p_demux->b_die ) return VLC_EGENERIC;
2026 if( !(i_count % (1024 * 10)) )
2027 msg_Warn( p_demux, "trying to resync..." );
2032 /****************************************************************************
2034 ****************************************************************************/
2035 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2037 avi_entry_t *p_index)
2039 avi_track_t *tk = p_sys->track[i_stream];
2041 /* Update i_movi_lastchunk_pos */
2042 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2044 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2048 if( tk->i_idxnb >= tk->i_idxmax )
2050 tk->i_idxmax += 16384;
2051 tk->p_index = realloc( tk->p_index,
2052 tk->i_idxmax * sizeof( avi_entry_t ) );
2053 if( tk->p_index == NULL )
2058 /* calculate cumulate length */
2059 if( tk->i_idxnb > 0 )
2061 p_index->i_lengthtotal =
2062 tk->p_index[tk->i_idxnb - 1].i_length +
2063 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2067 p_index->i_lengthtotal = 0;
2070 tk->p_index[tk->i_idxnb++] = *p_index;
2073 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2075 demux_sys_t *p_sys = p_demux->p_sys;
2077 avi_chunk_list_t *p_riff;
2078 avi_chunk_list_t *p_movi;
2079 avi_chunk_idx1_t *p_idx1;
2081 unsigned int i_stream;
2082 unsigned int i_index;
2086 vlc_bool_t b_keyset[100];
2088 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2089 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2090 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2094 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2095 return VLC_EGENERIC;
2098 /* *** calculate offset *** */
2099 /* Well, avi is __SHIT__ so test more than one entry
2100 * (needed for some avi files) */
2102 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2104 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2106 i_offset = p_movi->i_chunk_pos + 8;
2111 /* Reset b_keyset */
2112 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2113 b_keyset[i_stream] = VLC_FALSE;
2115 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2119 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2122 if( i_stream < p_sys->i_track &&
2123 i_cat == p_sys->track[i_stream]->i_cat )
2126 index.i_id = p_idx1->entry[i_index].i_fourcc;
2128 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2129 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2130 index.i_length = p_idx1->entry[i_index].i_length;
2131 AVI_IndexAddEntry( p_sys, i_stream, &index );
2133 if( index.i_flags&AVIIF_KEYFRAME )
2134 b_keyset[i_stream] = VLC_TRUE;
2138 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2140 if( !b_keyset[i_stream] )
2142 avi_track_t *tk = p_sys->track[i_stream];
2144 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2145 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2146 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2152 static void __Parse_indx( demux_t *p_demux,
2154 avi_chunk_indx_t *p_indx )
2156 demux_sys_t *p_sys = p_demux->p_sys;
2160 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2161 if( p_indx->i_indexsubtype == 0 )
2163 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2165 index.i_id = p_indx->i_id;
2166 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2167 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2168 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2170 AVI_IndexAddEntry( p_sys, i_stream, &index );
2173 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2175 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2177 index.i_id = p_indx->i_id;
2178 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2179 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2180 index.i_length = p_indx->idx.field[i].i_size;
2182 AVI_IndexAddEntry( p_sys, i_stream, &index );
2187 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2191 static void AVI_IndexLoad_indx( demux_t *p_demux )
2193 demux_sys_t *p_sys = p_demux->p_sys;
2194 unsigned int i_stream;
2197 avi_chunk_list_t *p_riff;
2198 avi_chunk_list_t *p_hdrl;
2200 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2201 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2203 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2205 avi_chunk_list_t *p_strl;
2206 avi_chunk_indx_t *p_indx;
2208 #define p_stream p_sys->track[i_stream]
2209 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2210 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2214 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2219 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2221 __Parse_indx( p_demux, i_stream, p_indx );
2223 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2226 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2228 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2229 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2233 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2238 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2244 static void AVI_IndexLoad( demux_t *p_demux )
2246 demux_sys_t *p_sys = p_demux->p_sys;
2247 unsigned int i_stream;
2249 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2251 p_sys->track[i_stream]->i_idxnb = 0;
2252 p_sys->track[i_stream]->i_idxmax = 0;
2253 p_sys->track[i_stream]->p_index = NULL;
2258 AVI_IndexLoad_indx( p_demux );
2260 else if( AVI_IndexLoad_idx1( p_demux ) )
2262 /* try indx if idx1 failed as some "normal" file have indx too */
2263 AVI_IndexLoad_indx( p_demux );
2266 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2268 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2269 i_stream, p_sys->track[i_stream]->i_idxnb );
2273 static void AVI_IndexCreate( demux_t *p_demux )
2275 demux_sys_t *p_sys = p_demux->p_sys;
2277 avi_chunk_list_t *p_riff;
2278 avi_chunk_list_t *p_movi;
2280 unsigned int i_stream;
2283 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2284 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2288 msg_Err( p_demux, "cannot find p_movi" );
2292 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2294 p_sys->track[i_stream]->i_idxnb = 0;
2295 p_sys->track[i_stream]->i_idxmax = 0;
2296 p_sys->track[i_stream]->p_index = NULL;
2298 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2299 stream_Size( p_demux->s ) );
2301 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2302 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2305 /* Only show dialog if AVI is > 10MB */
2306 p_demux->p_sys->i_dialog_id = -1;
2307 if( stream_Size( p_demux->s ) > 10000000 )
2309 p_demux->p_sys->i_dialog_id = intf_IntfProgress( p_demux,
2310 _( "Fixing AVI Index..." ),
2312 p_demux->p_sys->last_update = mdate();
2319 if( p_demux->b_die )
2324 /* Don't update dialog too often */
2325 if( p_demux->p_sys->i_dialog_id > 0 &&
2326 mdate() - p_demux->p_sys->last_update > 100000 )
2328 int64_t i_pos = stream_Tell( p_demux->s )* 100 /
2329 stream_Size( p_demux->s );
2330 float f_pos = (float)i_pos;
2331 p_demux->p_sys->last_update = mdate();
2332 intf_ProgressUpdate( p_demux, p_demux->p_sys->i_dialog_id,
2333 _( "Fixing AVI Index..." ), f_pos, -1 );
2336 if( AVI_PacketGetHeader( p_demux, &pk ) )
2340 if( pk.i_stream < p_sys->i_track &&
2341 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2344 index.i_id = pk.i_fourcc;
2346 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2347 index.i_pos = pk.i_pos;
2348 index.i_length = pk.i_size;
2349 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2353 switch( pk.i_fourcc )
2355 case AVIFOURCC_idx1:
2358 avi_chunk_list_t *p_sysx;
2359 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2360 AVIFOURCC_RIFF, 1 );
2362 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2363 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2370 case AVIFOURCC_RIFF:
2371 msg_Dbg( p_demux, "new RIFF chunk found" );
2373 case AVIFOURCC_JUNK:
2376 msg_Warn( p_demux, "need resync, probably broken avi" );
2377 if( AVI_PacketSearch( p_demux ) )
2379 msg_Warn( p_demux, "lost sync, abord index creation" );
2385 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2386 AVI_PacketNext( p_demux ) )
2393 if( p_demux->p_sys->i_dialog_id > 0 )
2395 intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
2398 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2401 "stream[%d] creating %d index entries",
2403 p_sys->track[i_stream]->i_idxnb );
2407 /*****************************************************************************
2409 *****************************************************************************/
2410 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2412 demux_sys_t *p_sys = p_demux->p_sys;
2414 int b_end = VLC_TRUE;
2416 for( i = 0; i < p_sys->i_track; i++ )
2418 avi_track_t *tk = p_sys->track[i];
2419 if( tk->i_idxposc >= tk->i_idxnb )
2421 tk->b_activated = VLC_FALSE;
2422 if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2432 /****************************************************************************
2433 * AVI_MovieGetLength give max streams length in second
2434 ****************************************************************************/
2435 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2437 demux_sys_t *p_sys = p_demux->p_sys;
2438 mtime_t i_maxlength = 0;
2441 for( i = 0; i < p_sys->i_track; i++ )
2443 avi_track_t *tk = p_sys->track[i];
2446 /* fix length for each stream */
2447 if( tk->i_idxnb < 1 || !tk->p_index )
2452 if( tk->i_samplesize )
2454 i_length = AVI_GetDPTS( tk,
2455 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2456 tk->p_index[tk->i_idxnb-1].i_length );
2460 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2462 i_length /= (mtime_t)1000000; /* in seconds */
2465 "stream[%d] length:"I64Fd" (based on index)",
2468 i_maxlength = __MAX( i_maxlength, i_length );