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 ||
528 p_strh->i_handler == FOURCC_dv25 ||
529 p_strh->i_handler == FOURCC_dv50 )
531 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
532 if( !tk->p_out_muxed )
533 msg_Err( p_demux, "could not load the DV parser" );
539 case( AVIFOURCC_mids):
540 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
543 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
549 /* The charset of p_strn is undefined */
550 EnsureUTF8( p_strn->p_str );
551 fmt.psz_description = strdup( p_strn->p_str );
553 if( tk->p_out_muxed == NULL )
554 tk->p_es = es_out_Add( p_demux->out, &fmt );
555 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
558 if( p_sys->i_track <= 0 )
560 msg_Err( p_demux, "no valid track" );
564 i_do_index = config_GetInt( p_demux, "avi-index" );
565 if( i_do_index == 1 ) /* Always fix */
568 if( p_sys->b_seekable )
570 AVI_IndexCreate( p_demux );
574 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
575 AVI_IndexLoad( p_demux );
580 AVI_IndexLoad( p_demux );
583 /* *** movie length in sec *** */
584 p_sys->i_length = AVI_MovieGetLength( p_demux );
585 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
586 (mtime_t)p_avih->i_microsecperframe /
589 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
590 "axproximative or will have strange behaviour" );
591 if( i_do_index == 0 && !b_index )
594 i_create = intf_UserYesNo( p_demux, _("AVI Index") ,
595 _( "This AVI file is broken. Seeking will not "
596 "work correctly.\nDo you want to "
597 "try to repair it?\n\nThis might take a long time." ),
598 _( "Repair" ), _( "Don't repair" ), _( "Cancel") );
599 if( i_create == DIALOG_OK_YES )
602 msg_Dbg( p_demux, "Fixing AVI index" );
605 else if( i_create == DIALOG_CANCELLED )
608 vlc_object_kill( p_demux->p_parent );
614 /* fix some BeOS MediaKit generated file */
615 for( i = 0 ; i < p_sys->i_track; i++ )
617 avi_track_t *tk = p_sys->track[i];
618 avi_chunk_list_t *p_strl;
619 avi_chunk_strh_t *p_strh;
620 avi_chunk_strf_auds_t *p_auds;
622 if( tk->i_cat != AUDIO_ES )
626 if( tk->i_idxnb < 1 ||
628 tk->i_samplesize != 0 )
632 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
633 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
634 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
636 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
637 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
639 int64_t i_track_length =
640 tk->p_index[tk->i_idxnb-1].i_length +
641 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
642 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
643 (mtime_t)p_avih->i_microsecperframe;
647 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
650 tk->i_samplesize = 1;
651 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
652 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
656 if( p_sys->b_seekable )
658 /* we have read all chunk so go back to movi */
659 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
661 /* Skip movi header */
662 stream_Read( p_demux->s, NULL, 12 );
664 p_sys->i_movi_begin = p_movi->i_chunk_pos;
670 vlc_meta_Delete( p_sys->meta );
672 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
677 /*****************************************************************************
678 * Close: frees unused data
679 *****************************************************************************/
680 static void Close ( vlc_object_t * p_this )
682 demux_t * p_demux = (demux_t *)p_this;
684 demux_sys_t *p_sys = p_demux->p_sys ;
686 for( i = 0; i < p_sys->i_track; i++ )
688 if( p_sys->track[i] )
690 if( p_sys->track[i]->p_out_muxed )
691 stream_DemuxDelete( p_sys->track[i]->p_out_muxed );
692 FREENULL( p_sys->track[i]->p_index );
693 free( p_sys->track[i] );
696 FREENULL( p_sys->track );
697 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
698 vlc_meta_Delete( p_sys->meta );
703 /*****************************************************************************
704 * Demux_Seekable: reads and demuxes data packets for stream seekable
705 *****************************************************************************
706 * AVIDemux: reads and demuxes data packets
707 *****************************************************************************
708 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
709 *****************************************************************************/
716 off_t i_posf; /* where we will read :
717 if i_idxposb == 0 : begining of chunk (+8 to acces data)
718 else : point on data directly */
719 } avi_track_toread_t;
721 static int Demux_Seekable( demux_t *p_demux )
723 demux_sys_t *p_sys = p_demux->p_sys;
725 unsigned int i_track_count = 0;
726 unsigned int i_track;
728 /* cannot be more than 100 stream (dcXX or wbXX) */
729 avi_track_toread_t toread[100];
732 /* detect new selected/unselected streams */
733 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
735 avi_track_t *tk = p_sys->track[i_track];
738 if( p_sys->b_muxed && tk->p_out_muxed )
741 tk->b_activated = VLC_TRUE;
745 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
746 if( b && !tk->b_activated )
748 if( p_sys->b_seekable)
750 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
752 tk->b_activated = VLC_TRUE;
754 else if( !b && tk->b_activated )
756 tk->b_activated = VLC_FALSE;
764 if( i_track_count <= 0 )
766 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
768 p_sys->i_time += 25*1000; /* read 25ms */
771 if( p_sys->i_time >= i_length )
775 msg_Warn( p_demux, "no track selected, exiting..." );
779 /* wait for the good time */
780 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
781 p_sys->i_time += 25*1000; /* read 25ms */
784 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
786 avi_track_t *tk = p_sys->track[i_track];
789 toread[i_track].b_ok = tk->b_activated;
790 if( tk->i_idxposc < tk->i_idxnb )
792 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
793 if( tk->i_idxposb > 0 )
795 toread[i_track].i_posf += 8 + tk->i_idxposb;
800 toread[i_track].i_posf = -1;
803 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
805 if( tk->i_samplesize )
807 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
811 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
816 toread[i_track].i_toread *= -1;
820 b_stream = VLC_FALSE;
831 /* search for first chunk to be read */
832 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
834 if( !toread[i].b_ok ||
835 AVI_GetDPTS( p_sys->track[i],
836 toread[i].i_toread ) <= -25 * 1000 )
841 if( toread[i].i_toread > 0 )
843 b_done = VLC_FALSE; /* not yet finished */
845 if( toread[i].i_posf > 0 )
847 if( i_pos == -1 || i_pos > toread[i].i_posf )
850 i_pos = toread[i].i_posf;
862 int i_loop_count = 0;
864 /* no valid index, we will parse directly the stream
865 * in case we fail we will disable all finished stream */
866 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
868 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
869 if( AVI_PacketNext( p_demux ) )
871 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
876 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
883 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
886 "cannot get packet header, track disabled" );
887 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
889 if( avi_pk.i_stream >= p_sys->i_track ||
890 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
892 if( AVI_PacketNext( p_demux ) )
895 "cannot skip packet, track disabled" );
896 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
899 /* Prevents from eating all the CPU with broken files.
900 * This value should be low enough so that it doesn't
901 * affect the reading speed too much. */
902 if( !(++i_loop_count % 1024) )
904 if( p_demux->b_die ) return -1;
907 if( !(i_loop_count % (1024 * 10)) )
909 "don't seem to find any data..." );
915 /* add this chunk to the index */
918 index.i_id = avi_pk.i_fourcc;
920 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
922 index.i_pos = avi_pk.i_pos;
923 index.i_length = avi_pk.i_size;
924 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
926 i_track = avi_pk.i_stream;
927 tk = p_sys->track[i_track];
928 /* do we will read this data ? */
929 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
935 if( AVI_PacketNext( p_demux ) )
938 "cannot skip packet, track disabled" );
939 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
948 stream_Seek( p_demux->s, i_pos );
951 /* Set the track to use */
952 tk = p_sys->track[i_track];
954 /* read thoses data */
955 if( tk->i_samplesize )
957 unsigned int i_toread;
959 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
961 if( tk->i_samplesize > 1 )
963 i_toread = tk->i_samplesize;
967 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
968 i_toread = __MAX( i_toread, 100 );
971 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
977 i_size = tk->p_index[tk->i_idxposc].i_length;
980 if( tk->i_idxposb == 0 )
982 i_size += 8; /* need to read and skip header */
985 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
987 msg_Warn( p_demux, "failed reading data" );
988 tk->b_activated = VLC_FALSE;
989 toread[i_track].b_ok = VLC_FALSE;
992 if( i_size % 2 ) /* read was padded on word boundary */
997 if( tk->i_idxposb == 0 )
999 p_frame->p_buffer += 8;
1000 p_frame->i_buffer -= 8;
1002 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1003 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1005 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1009 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1013 if( tk->i_samplesize )
1015 if( tk->i_idxposb == 0 )
1019 toread[i_track].i_toread -= i_size;
1020 tk->i_idxposb += i_size;
1021 if( tk->i_idxposb >=
1022 tk->p_index[tk->i_idxposc].i_length )
1030 int i_length = tk->p_index[tk->i_idxposc].i_length;
1033 if( tk->i_cat == AUDIO_ES )
1035 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1037 toread[i_track].i_toread--;
1040 if( tk->i_idxposc < tk->i_idxnb)
1042 toread[i_track].i_posf =
1043 tk->p_index[tk->i_idxposc].i_pos;
1044 if( tk->i_idxposb > 0 )
1046 toread[i_track].i_posf += 8 + tk->i_idxposb;
1052 toread[i_track].i_posf = -1;
1055 b_stream = VLC_TRUE; /* at least one read succeed */
1057 if( tk->i_cat != VIDEO_ES )
1058 p_frame->i_dts = p_frame->i_pts;
1061 p_frame->i_dts = p_frame->i_pts;
1065 //p_pes->i_rate = p_demux->stream.control.i_rate;
1066 if( tk->p_out_muxed )
1067 stream_DemuxSend( tk->p_out_muxed, p_frame );
1069 es_out_Send( p_demux->out, tk->p_es, p_frame );
1074 /*****************************************************************************
1075 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1076 *****************************************************************************
1077 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1078 *****************************************************************************/
1079 static int Demux_UnSeekable( demux_t *p_demux )
1081 demux_sys_t *p_sys = p_demux->p_sys;
1082 avi_track_t *p_stream_master = NULL;
1083 unsigned int i_stream;
1084 unsigned int i_packet;
1086 if( p_sys->b_muxed )
1088 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1089 return VLC_EGENERIC;
1092 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1094 /* *** find master stream for data packet skipping algo *** */
1095 /* *** -> first video, if any, or first audio ES *** */
1096 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1098 avi_track_t *tk = p_sys->track[i_stream];
1101 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1103 if( b && tk->i_cat == VIDEO_ES )
1105 p_stream_master = tk;
1109 p_stream_master = tk;
1113 if( !p_stream_master )
1115 msg_Warn( p_demux, "no more stream selected" );
1119 p_sys->i_time = AVI_GetPTS( p_stream_master );
1121 for( i_packet = 0; i_packet < 10; i_packet++)
1123 #define p_stream p_sys->track[avi_pk.i_stream]
1125 avi_packet_t avi_pk;
1127 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1132 if( avi_pk.i_stream >= p_sys->i_track ||
1133 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1135 /* we haven't found an audio or video packet:
1136 * - we have seek, found first next packet
1137 * - others packets could be found, skip them
1139 switch( avi_pk.i_fourcc )
1141 case AVIFOURCC_JUNK:
1142 case AVIFOURCC_LIST:
1143 case AVIFOURCC_RIFF:
1144 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1145 case AVIFOURCC_idx1:
1148 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1150 return( 0 ); /* eof */
1153 "seems to have lost position, resync" );
1154 if( AVI_PacketSearch( p_demux ) )
1156 msg_Err( p_demux, "resync failed" );
1163 /* check for time */
1164 if( __ABS( AVI_GetPTS( p_stream ) -
1165 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1167 /* load it and send to decoder */
1169 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1173 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1175 if( avi_pk.i_cat != VIDEO_ES )
1176 p_frame->i_dts = p_frame->i_pts;
1179 p_frame->i_dts = p_frame->i_pts;
1183 //p_pes->i_rate = p_demux->stream.control.i_rate;
1184 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1188 if( AVI_PacketNext( p_demux ) )
1194 /* *** update stream time position *** */
1195 if( p_stream->i_samplesize )
1197 p_stream->i_idxposb += avi_pk.i_size;
1201 if( p_stream->i_cat == AUDIO_ES )
1203 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1205 p_stream->i_idxposc++;
1215 /*****************************************************************************
1216 * Seek: goto to i_date or i_percent
1217 *****************************************************************************/
1218 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1221 demux_sys_t *p_sys = p_demux->p_sys;
1222 unsigned int i_stream;
1223 msg_Dbg( p_demux, "seek requested: "I64Fd" seconds %d%%",
1224 i_date / 1000000, i_percent );
1226 if( p_sys->b_seekable )
1228 if( !p_sys->i_length )
1230 avi_track_t *p_stream;
1233 /* use i_percent to create a true i_date */
1234 msg_Warn( p_demux, "seeking without index at %d%%"
1235 " only works for interleaved files", i_percent );
1236 if( i_percent >= 100 )
1238 msg_Warn( p_demux, "cannot seek so far !" );
1239 return VLC_EGENERIC;
1241 i_percent = __MAX( i_percent, 0 );
1243 /* try to find chunk that is at i_percent or the file */
1244 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1245 p_sys->i_movi_begin );
1246 /* search first selected stream */
1247 for( i_stream = 0, p_stream = NULL;
1248 i_stream < p_sys->i_track; i_stream++ )
1250 p_stream = p_sys->track[i_stream];
1251 if( p_stream->b_activated )
1256 if( !p_stream || !p_stream->b_activated )
1258 msg_Warn( p_demux, "cannot find any selected stream" );
1259 return VLC_EGENERIC;
1262 /* be sure that the index exist */
1263 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1265 msg_Warn( p_demux, "cannot seek" );
1266 return VLC_EGENERIC;
1269 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1270 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1272 /* search after i_idxposc */
1273 if( AVI_StreamChunkSet( p_demux,
1274 i_stream, p_stream->i_idxposc + 1 ) )
1276 msg_Warn( p_demux, "cannot seek" );
1277 return VLC_EGENERIC;
1281 i_date = AVI_GetPTS( p_stream );
1282 /* TODO better support for i_samplesize != 0 */
1283 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1287 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1289 avi_track_t *p_stream = p_sys->track[i_stream];
1291 if( !p_stream->b_activated )
1294 AVI_TrackSeek( p_demux, i_stream, i_date );
1296 p_sys->i_time = i_date;
1297 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1302 msg_Err( p_demux, "shouldn't yet be executed" );
1303 return VLC_EGENERIC;
1307 /*****************************************************************************
1309 *****************************************************************************/
1310 static double ControlGetPosition( demux_t *p_demux )
1312 demux_sys_t *p_sys = p_demux->p_sys;
1314 if( p_sys->i_length > 0 )
1316 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1318 else if( stream_Size( p_demux->s ) > 0 )
1324 /* search the more advanced selected es */
1325 for( i = 0; i < p_sys->i_track; i++ )
1327 avi_track_t *tk = p_sys->track[i];
1328 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1330 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1331 tk->p_index[tk->i_idxposc].i_length + 8;
1338 return (double)i64 / stream_Size( p_demux->s );
1343 static int Control( demux_t *p_demux, int i_query, va_list args )
1345 demux_sys_t *p_sys = p_demux->p_sys;
1353 case DEMUX_GET_POSITION:
1354 pf = (double*)va_arg( args, double * );
1355 *pf = ControlGetPosition( p_demux );
1357 case DEMUX_SET_POSITION:
1358 f = (double)va_arg( args, double );
1359 if( p_sys->b_seekable )
1361 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1362 return Seek( p_demux, i64, (int)(f * 100) );
1366 int64_t i_pos = stream_Size( p_demux->s ) * f;
1367 return stream_Seek( p_demux->s, i_pos );
1370 case DEMUX_GET_TIME:
1371 pi64 = (int64_t*)va_arg( args, int64_t * );
1372 *pi64 = p_sys->i_time;
1375 case DEMUX_SET_TIME:
1379 i64 = (int64_t)va_arg( args, int64_t );
1380 if( p_sys->i_length > 0 )
1382 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1384 else if( p_sys->i_time > 0 )
1386 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1387 (double)i64 / (double)p_sys->i_time );
1389 return Seek( p_demux, i64, i_percent );
1391 case DEMUX_GET_LENGTH:
1392 pi64 = (int64_t*)va_arg( args, int64_t * );
1393 *pi64 = p_sys->i_length * (mtime_t)1000000;
1397 pf = (double*)va_arg( args, double * );
1399 for( i = 0; i < (int)p_sys->i_track; i++ )
1401 avi_track_t *tk = p_sys->track[i];
1402 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1404 *pf = (float)tk->i_rate / (float)tk->i_scale;
1409 case DEMUX_GET_META:
1410 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1411 vlc_meta_Merge( p_meta, p_sys->meta );
1415 return VLC_EGENERIC;
1419 /*****************************************************************************
1420 * Function to convert pts to chunk or byte
1421 *****************************************************************************/
1423 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1428 return (mtime_t)((int64_t)i_pts *
1429 (int64_t)tk->i_rate /
1430 (int64_t)tk->i_scale /
1433 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1435 if( !tk->i_scale || !tk->i_samplesize )
1438 return (mtime_t)((int64_t)i_pts *
1439 (int64_t)tk->i_rate /
1440 (int64_t)tk->i_scale /
1442 (int64_t)tk->i_samplesize );
1445 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1452 i_dpts = (mtime_t)( (int64_t)1000000 *
1454 (int64_t)tk->i_scale /
1455 (int64_t)tk->i_rate );
1457 if( tk->i_samplesize )
1459 return i_dpts / tk->i_samplesize;
1464 static mtime_t AVI_GetPTS( avi_track_t *tk )
1466 if( tk->i_samplesize )
1468 int64_t i_count = 0;
1470 /* we need a valid entry we will emulate one */
1471 if( tk->i_idxposc == tk->i_idxnb )
1475 /* use the last entry */
1476 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1477 + tk->p_index[tk->i_idxnb - 1].i_length;
1482 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1484 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1488 if( tk->i_cat == AUDIO_ES )
1490 return AVI_GetDPTS( tk, tk->i_blockno );
1494 return AVI_GetDPTS( tk, tk->i_idxposc );
1499 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1501 demux_sys_t *p_sys = p_demux->p_sys;
1502 avi_packet_t avi_pk;
1503 int i_loop_count = 0;
1505 /* find first chunk of i_stream that isn't in index */
1507 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1509 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1510 if( AVI_PacketNext( p_demux ) )
1512 return VLC_EGENERIC;
1517 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1522 if( p_demux->b_die ) return VLC_EGENERIC;
1524 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1526 msg_Warn( p_demux, "cannot get packet header" );
1527 return VLC_EGENERIC;
1529 if( avi_pk.i_stream >= p_sys->i_track ||
1530 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1532 if( AVI_PacketNext( p_demux ) )
1534 return VLC_EGENERIC;
1537 /* Prevents from eating all the CPU with broken files.
1538 * This value should be low enough so that it doesn't
1539 * affect the reading speed too much. */
1540 if( !(++i_loop_count % 1024) )
1542 if( p_demux->b_die ) return VLC_EGENERIC;
1545 if( !(i_loop_count % (1024 * 10)) )
1546 msg_Warn( p_demux, "don't seem to find any data..." );
1551 /* add this chunk to the index */
1554 index.i_id = avi_pk.i_fourcc;
1556 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1558 index.i_pos = avi_pk.i_pos;
1559 index.i_length = avi_pk.i_size;
1560 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1562 if( avi_pk.i_stream == i_stream )
1567 if( AVI_PacketNext( p_demux ) )
1569 return VLC_EGENERIC;
1575 /* be sure that i_ck will be a valid index entry */
1576 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1579 demux_sys_t *p_sys = p_demux->p_sys;
1580 avi_track_t *p_stream = p_sys->track[i_stream];
1582 p_stream->i_idxposc = i_ck;
1583 p_stream->i_idxposb = 0;
1585 if( i_ck >= p_stream->i_idxnb )
1587 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1590 p_stream->i_idxposc++;
1591 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1593 return VLC_EGENERIC;
1596 } while( p_stream->i_idxposc < i_ck );
1602 /* XXX FIXME up to now, we assume that all chunk are one after one */
1603 static int AVI_StreamBytesSet( demux_t *p_demux,
1604 unsigned int i_stream,
1607 demux_sys_t *p_sys = p_demux->p_sys;
1608 avi_track_t *p_stream = p_sys->track[i_stream];
1610 if( ( p_stream->i_idxnb > 0 )
1611 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1612 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1614 /* index is valid to find the ck */
1615 /* uses dichototmie to be fast enougth */
1616 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1617 int i_idxmax = p_stream->i_idxnb;
1621 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1623 i_idxmax = i_idxposc ;
1624 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1628 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1629 p_stream->p_index[i_idxposc].i_length <= i_byte)
1631 i_idxmin = i_idxposc ;
1632 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1636 p_stream->i_idxposc = i_idxposc;
1637 p_stream->i_idxposb = i_byte -
1638 p_stream->p_index[i_idxposc].i_lengthtotal;
1647 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1648 p_stream->i_idxposb = 0;
1651 p_stream->i_idxposc++;
1652 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1654 return VLC_EGENERIC;
1657 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1658 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1660 p_stream->i_idxposb = i_byte -
1661 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1666 static int AVI_TrackSeek( demux_t *p_demux,
1670 demux_sys_t *p_sys = p_demux->p_sys;
1671 avi_track_t *tk = p_sys->track[i_stream];
1673 #define p_stream p_sys->track[i_stream]
1676 i_oldpts = AVI_GetPTS( p_stream );
1678 if( !p_stream->i_samplesize )
1680 if( AVI_StreamChunkSet( p_demux,
1682 AVI_PTSToChunk( p_stream, i_date ) ) )
1684 return VLC_EGENERIC;
1687 if( p_stream->i_cat == AUDIO_ES )
1691 for( i = 0; i < tk->i_idxposc; i++ )
1693 if( tk->i_blocksize > 0 )
1695 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1705 "old:"I64Fd" %s new "I64Fd,
1707 i_oldpts > i_date ? ">" : "<",
1710 if( p_stream->i_cat == VIDEO_ES )
1712 /* search key frame */
1713 //if( i_date < i_oldpts || 1 )
1715 while( p_stream->i_idxposc > 0 &&
1716 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1719 if( AVI_StreamChunkSet( p_demux,
1721 p_stream->i_idxposc - 1 ) )
1723 return VLC_EGENERIC;
1726 if( p_stream->p_es )
1727 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, p_stream->p_es, i_date );
1732 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1733 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1736 if( AVI_StreamChunkSet( p_demux,
1738 p_stream->i_idxposc + 1 ) )
1740 return VLC_EGENERIC;
1749 if( AVI_StreamBytesSet( p_demux,
1751 AVI_PTSToByte( p_stream, i_date ) ) )
1753 return VLC_EGENERIC;
1760 /****************************************************************************
1761 * Return VLC_TRUE if it's a key frame
1762 ****************************************************************************/
1763 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1769 * startcode: 0x00000100 32bits
1770 * framenumber ? 5bits
1771 * piture type 0(I),1(P) 2bits
1773 if( GetDWBE( p_byte ) != 0x00000100 )
1775 /* it's not an msmpegv1 stream, strange...*/
1776 return AVIIF_KEYFRAME;
1778 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1781 case FOURCC_DIV3: /* wmv1 also */
1783 * picture type 0(I),1(P) 2bits
1785 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1787 /* we should find first occurrence of 0x000001b6 (32bits)
1788 * startcode: 0x000001b6 32bits
1789 * piture type 0(I),1(P) 2bits
1791 if( GetDWBE( p_byte ) != 0x000001b6 )
1793 /* not true , need to find the first VOP header */
1794 return AVIIF_KEYFRAME;
1796 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1799 /* I can't do it, so say yes */
1800 return AVIIF_KEYFRAME;
1804 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1809 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1813 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1817 return VLC_FOURCC('m','r','l','e');
1866 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1870 /****************************************************************************
1872 ****************************************************************************/
1873 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1874 unsigned int *pi_number, unsigned int *pi_type )
1876 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1879 c1 = ((uint8_t *)&i_id)[0];
1880 c2 = ((uint8_t *)&i_id)[1];
1882 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1884 SET_PTR( pi_number, 100 ); /* > max stream number */
1885 SET_PTR( pi_type, UNKNOWN_ES );
1889 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1890 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1893 SET_PTR( pi_type, AUDIO_ES );
1897 SET_PTR( pi_type, VIDEO_ES );
1900 SET_PTR( pi_type, UNKNOWN_ES );
1907 /****************************************************************************
1909 ****************************************************************************/
1910 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1912 const uint8_t *p_peek;
1914 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1916 return VLC_EGENERIC;
1918 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1919 p_pk->i_size = GetDWLE( p_peek + 4 );
1920 p_pk->i_pos = stream_Tell( p_demux->s );
1921 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1923 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1924 p_peek[10], p_peek[11] );
1931 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1933 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1937 static int AVI_PacketNext( demux_t *p_demux )
1939 avi_packet_t avi_ck;
1942 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1944 return VLC_EGENERIC;
1947 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1948 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1952 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1953 avi_ck.i_type == AVIFOURCC_AVIX )
1959 i_skip = __EVEN( avi_ck.i_size ) + 8;
1962 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1964 return VLC_EGENERIC;
1969 static int AVI_PacketRead( demux_t *p_demux,
1971 block_t **pp_frame )
1975 i_size = __EVEN( p_pk->i_size + 8 );
1977 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1979 return VLC_EGENERIC;
1981 (*pp_frame)->p_buffer += 8;
1982 (*pp_frame)->i_buffer -= 8;
1984 if( i_size != p_pk->i_size + 8 )
1986 (*pp_frame)->i_buffer--;
1992 static int AVI_PacketSearch( demux_t *p_demux )
1994 demux_sys_t *p_sys = p_demux->p_sys;
1995 avi_packet_t avi_pk;
2000 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2002 return VLC_EGENERIC;
2004 AVI_PacketGetHeader( p_demux, &avi_pk );
2005 if( avi_pk.i_stream < p_sys->i_track &&
2006 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2010 switch( avi_pk.i_fourcc )
2012 case AVIFOURCC_JUNK:
2013 case AVIFOURCC_LIST:
2014 case AVIFOURCC_RIFF:
2015 case AVIFOURCC_idx1:
2019 /* Prevents from eating all the CPU with broken files.
2020 * This value should be low enough so that it doesn't affect the
2021 * reading speed too much (not that we care much anyway because
2022 * this code is called only on broken files). */
2023 if( !(++i_count % 1024) )
2025 if( p_demux->b_die ) return VLC_EGENERIC;
2028 if( !(i_count % (1024 * 10)) )
2029 msg_Warn( p_demux, "trying to resync..." );
2034 /****************************************************************************
2036 ****************************************************************************/
2037 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2039 avi_entry_t *p_index)
2041 avi_track_t *tk = p_sys->track[i_stream];
2043 /* Update i_movi_lastchunk_pos */
2044 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2046 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2050 if( tk->i_idxnb >= tk->i_idxmax )
2052 tk->i_idxmax += 16384;
2053 tk->p_index = realloc( tk->p_index,
2054 tk->i_idxmax * sizeof( avi_entry_t ) );
2055 if( tk->p_index == NULL )
2060 /* calculate cumulate length */
2061 if( tk->i_idxnb > 0 )
2063 p_index->i_lengthtotal =
2064 tk->p_index[tk->i_idxnb - 1].i_length +
2065 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2069 p_index->i_lengthtotal = 0;
2072 tk->p_index[tk->i_idxnb++] = *p_index;
2075 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2077 demux_sys_t *p_sys = p_demux->p_sys;
2079 avi_chunk_list_t *p_riff;
2080 avi_chunk_list_t *p_movi;
2081 avi_chunk_idx1_t *p_idx1;
2083 unsigned int i_stream;
2084 unsigned int i_index;
2088 vlc_bool_t b_keyset[100];
2090 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2091 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2092 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2096 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2097 return VLC_EGENERIC;
2100 /* *** calculate offset *** */
2101 /* Well, avi is __SHIT__ so test more than one entry
2102 * (needed for some avi files) */
2104 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2106 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2108 i_offset = p_movi->i_chunk_pos + 8;
2113 /* Reset b_keyset */
2114 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2115 b_keyset[i_stream] = VLC_FALSE;
2117 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2121 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2124 if( i_stream < p_sys->i_track &&
2125 i_cat == p_sys->track[i_stream]->i_cat )
2128 index.i_id = p_idx1->entry[i_index].i_fourcc;
2130 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2131 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2132 index.i_length = p_idx1->entry[i_index].i_length;
2133 AVI_IndexAddEntry( p_sys, i_stream, &index );
2135 if( index.i_flags&AVIIF_KEYFRAME )
2136 b_keyset[i_stream] = VLC_TRUE;
2140 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2142 if( !b_keyset[i_stream] )
2144 avi_track_t *tk = p_sys->track[i_stream];
2146 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2147 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2148 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2154 static void __Parse_indx( demux_t *p_demux,
2156 avi_chunk_indx_t *p_indx )
2158 demux_sys_t *p_sys = p_demux->p_sys;
2162 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2163 if( p_indx->i_indexsubtype == 0 )
2165 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2167 index.i_id = p_indx->i_id;
2168 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2169 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2170 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2172 AVI_IndexAddEntry( p_sys, i_stream, &index );
2175 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2177 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2179 index.i_id = p_indx->i_id;
2180 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2181 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2182 index.i_length = p_indx->idx.field[i].i_size;
2184 AVI_IndexAddEntry( p_sys, i_stream, &index );
2189 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2193 static void AVI_IndexLoad_indx( demux_t *p_demux )
2195 demux_sys_t *p_sys = p_demux->p_sys;
2196 unsigned int i_stream;
2199 avi_chunk_list_t *p_riff;
2200 avi_chunk_list_t *p_hdrl;
2202 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2203 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2205 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2207 avi_chunk_list_t *p_strl;
2208 avi_chunk_indx_t *p_indx;
2210 #define p_stream p_sys->track[i_stream]
2211 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2212 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2216 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2221 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2223 __Parse_indx( p_demux, i_stream, p_indx );
2225 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2228 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2230 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2231 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2235 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2240 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2246 static void AVI_IndexLoad( demux_t *p_demux )
2248 demux_sys_t *p_sys = p_demux->p_sys;
2249 unsigned int i_stream;
2251 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2253 p_sys->track[i_stream]->i_idxnb = 0;
2254 p_sys->track[i_stream]->i_idxmax = 0;
2255 p_sys->track[i_stream]->p_index = NULL;
2260 AVI_IndexLoad_indx( p_demux );
2262 else if( AVI_IndexLoad_idx1( p_demux ) )
2264 /* try indx if idx1 failed as some "normal" file have indx too */
2265 AVI_IndexLoad_indx( p_demux );
2268 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2270 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2271 i_stream, p_sys->track[i_stream]->i_idxnb );
2275 static void AVI_IndexCreate( demux_t *p_demux )
2277 demux_sys_t *p_sys = p_demux->p_sys;
2279 avi_chunk_list_t *p_riff;
2280 avi_chunk_list_t *p_movi;
2282 unsigned int i_stream;
2285 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2286 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2290 msg_Err( p_demux, "cannot find p_movi" );
2294 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2296 p_sys->track[i_stream]->i_idxnb = 0;
2297 p_sys->track[i_stream]->i_idxmax = 0;
2298 p_sys->track[i_stream]->p_index = NULL;
2300 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2301 stream_Size( p_demux->s ) );
2303 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2304 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2307 /* Only show dialog if AVI is > 10MB */
2308 p_demux->p_sys->i_dialog_id = -1;
2309 if( stream_Size( p_demux->s ) > 10000000 )
2311 p_demux->p_sys->i_dialog_id = intf_IntfProgress( p_demux,
2312 _( "Fixing AVI Index..." ),
2314 p_demux->p_sys->last_update = mdate();
2321 if( p_demux->b_die )
2326 /* Don't update dialog too often */
2327 if( p_demux->p_sys->i_dialog_id > 0 &&
2328 mdate() - p_demux->p_sys->last_update > 100000 )
2330 int64_t i_pos = stream_Tell( p_demux->s )* 100 /
2331 stream_Size( p_demux->s );
2332 float f_pos = (float)i_pos;
2333 p_demux->p_sys->last_update = mdate();
2334 intf_ProgressUpdate( p_demux, p_demux->p_sys->i_dialog_id,
2335 _( "Fixing AVI Index..." ), f_pos, -1 );
2338 if( AVI_PacketGetHeader( p_demux, &pk ) )
2342 if( pk.i_stream < p_sys->i_track &&
2343 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2346 index.i_id = pk.i_fourcc;
2348 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2349 index.i_pos = pk.i_pos;
2350 index.i_length = pk.i_size;
2351 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2355 switch( pk.i_fourcc )
2357 case AVIFOURCC_idx1:
2360 avi_chunk_list_t *p_sysx;
2361 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2362 AVIFOURCC_RIFF, 1 );
2364 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2365 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2372 case AVIFOURCC_RIFF:
2373 msg_Dbg( p_demux, "new RIFF chunk found" );
2375 case AVIFOURCC_JUNK:
2378 msg_Warn( p_demux, "need resync, probably broken avi" );
2379 if( AVI_PacketSearch( p_demux ) )
2381 msg_Warn( p_demux, "lost sync, abord index creation" );
2387 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2388 AVI_PacketNext( p_demux ) )
2395 if( p_demux->p_sys->i_dialog_id > 0 )
2397 intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
2400 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2403 "stream[%d] creating %d index entries",
2405 p_sys->track[i_stream]->i_idxnb );
2409 /*****************************************************************************
2411 *****************************************************************************/
2412 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2414 demux_sys_t *p_sys = p_demux->p_sys;
2416 int b_end = VLC_TRUE;
2418 for( i = 0; i < p_sys->i_track; i++ )
2420 avi_track_t *tk = p_sys->track[i];
2421 if( tk->i_idxposc >= tk->i_idxnb )
2423 tk->b_activated = VLC_FALSE;
2424 if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2434 /****************************************************************************
2435 * AVI_MovieGetLength give max streams length in second
2436 ****************************************************************************/
2437 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2439 demux_sys_t *p_sys = p_demux->p_sys;
2440 mtime_t i_maxlength = 0;
2443 for( i = 0; i < p_sys->i_track; i++ )
2445 avi_track_t *tk = p_sys->track[i];
2448 /* fix length for each stream */
2449 if( tk->i_idxnb < 1 || !tk->p_index )
2454 if( tk->i_samplesize )
2456 i_length = AVI_GetDPTS( tk,
2457 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2458 tk->p_index[tk->i_idxnb-1].i_length );
2462 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2464 i_length /= (mtime_t)1000000; /* in seconds */
2467 "stream[%d] length:"I64Fd" (based on index)",
2470 i_maxlength = __MAX( i_maxlength, i_length );