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 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
31 #include <vlc_interaction.h>
39 /*****************************************************************************
41 *****************************************************************************/
43 #define INTERLEAVE_TEXT N_("Force interleaved method" )
44 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
46 #define INDEX_TEXT N_("Force index creation")
47 #define INDEX_LONGTEXT N_( \
48 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
49 "or incomplete (not seekable)." )
51 static int Open ( vlc_object_t * );
52 static void Close( vlc_object_t * );
54 static int pi_index[] = {0,1,2};
56 static const char *ppsz_indexes[] = { N_("Ask"), N_("Always fix"),
60 set_shortname( "AVI" );
61 set_description( _("AVI demuxer") );
62 set_capability( "demux2", 212 );
63 set_category( CAT_INPUT );
64 set_subcategory( SUBCAT_INPUT_DEMUX );
66 add_bool( "avi-interleaved", 0, NULL,
67 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
68 add_integer( "avi-index", 0, NULL,
69 INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE );
70 change_integer_list( pi_index, ppsz_indexes, 0 );
72 set_callbacks( Open, Close );
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Control ( demux_t *, int, va_list );
79 static int Seek ( demux_t *, mtime_t, int );
80 static int Demux_Seekable ( demux_t * );
81 static int Demux_UnSeekable( demux_t * );
83 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
87 vlc_fourcc_t i_fourcc;
90 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
92 uint8_t i_peek[8]; /* first 8 bytes */
94 unsigned int i_stream;
105 uint32_t i_lengthtotal;
111 vlc_bool_t b_activated;
113 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
114 vlc_fourcc_t i_codec;
123 avi_entry_t *p_index;
124 unsigned int i_idxnb;
125 unsigned int i_idxmax;
127 unsigned int i_idxposc; /* numero of chunk */
128 unsigned int i_idxposb; /* byte in the current chunk */
130 /* For VBR audio only */
131 unsigned int i_blockno;
132 unsigned int i_blocksize;
134 /* For muxed streams */
135 stream_t *p_out_muxed;
143 vlc_bool_t b_seekable;
150 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
152 /* number of streams and information */
153 unsigned int i_track;
164 static inline off_t __EVEN( off_t i )
166 return (i & 1) ? i + 1 : i;
169 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
170 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
171 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
172 static mtime_t AVI_GetPTS ( avi_track_t * );
175 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
176 static int AVI_StreamChunkSet ( demux_t *,
177 unsigned int i_stream, unsigned int i_ck );
178 static int AVI_StreamBytesSet ( demux_t *,
179 unsigned int i_stream, off_t i_byte );
181 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
182 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
184 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
185 static int AVI_PacketNext ( demux_t * );
186 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
187 static int AVI_PacketSearch ( demux_t * );
189 static void AVI_IndexLoad ( demux_t * );
190 static void AVI_IndexCreate ( demux_t * );
191 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
193 static mtime_t AVI_MovieGetLength( demux_t * );
195 /*****************************************************************************
197 *****************************************************************************/
198 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
199 static int AVI_TrackStopFinishedStreams( demux_t *);
202 - For VBR mp3 stream:
203 count blocks by rounded-up chunksizes instead of chunks
204 we need full emulation of dshow avi demuxer bugs :(
205 fixes silly nandub-style a-v delaying in avi with vbr mp3...
206 (from mplayer 2002/08/02)
210 /*****************************************************************************
211 * Open: check file and initializes AVI structures
212 *****************************************************************************/
213 static int Open( vlc_object_t * p_this )
215 demux_t *p_demux = (demux_t *)p_this;
218 vlc_bool_t b_index = VLC_FALSE;
222 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
223 avi_chunk_list_t *p_hdrl, *p_movi;
224 avi_chunk_avih_t *p_avih;
226 unsigned int i_track;
227 unsigned int i, i_peeker;
231 /* Is it an avi file ? */
232 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
234 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
236 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) &&
237 !strncmp( (char *)&p_peek[8], "AVI ", 4 ) ) break;
240 if( i_peeker == 188 )
245 /* Initialize input structures. */
246 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
247 memset( p_sys, 0, sizeof( demux_sys_t ) );
250 p_sys->i_movi_lastchunk_pos = 0;
251 p_sys->b_odml = VLC_FALSE;
252 p_sys->b_muxed = VLC_FALSE;
257 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
259 p_demux->pf_control = Control;
260 p_demux->pf_demux = Demux_Seekable;
262 /* For unseekable stream, automaticaly use Demux_UnSeekable */
263 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
265 p_demux->pf_demux = Demux_UnSeekable;
270 stream_Read( p_demux->s, NULL, i_peeker );
273 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
275 msg_Err( p_demux, "avi module discarded (invalid file)" );
279 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
281 unsigned int i_count =
282 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
284 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
285 for( i = 1; i < i_count; i++ )
287 avi_chunk_list_t *p_sysx;
289 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
290 if( p_sysx->i_type == AVIFOURCC_AVIX )
292 msg_Warn( p_demux, "detected OpenDML file" );
293 p_sys->b_odml = VLC_TRUE;
299 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
300 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
301 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
303 if( !p_hdrl || !p_movi )
305 msg_Err( p_demux, "avi module discarded (invalid file)" );
309 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
311 msg_Err( p_demux, "cannot find avih chunk" );
314 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
315 if( p_avih->i_streams != i_track )
318 "found %d stream but %d are declared",
319 i_track, p_avih->i_streams );
323 msg_Err( p_demux, "no stream defined!" );
327 /* print information on streams */
328 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
330 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
331 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
332 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
333 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
334 if( ( p_sys->meta = vlc_meta_New() ) )
337 sprintf( buffer, "%s%s%s%s",
338 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
339 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
340 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
341 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
342 vlc_meta_SetSetting( p_sys->meta, buffer );
345 /* now read info on each stream and create ES */
346 for( i = 0 ; i < i_track; i++ )
348 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
349 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
350 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
351 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
352 avi_chunk_strf_auds_t *p_auds = NULL;
353 avi_chunk_strf_vids_t *p_vids = NULL;
356 tk->b_activated = VLC_FALSE;
367 tk->p_out_muxed = NULL;
369 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
370 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
372 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
374 msg_Warn( p_demux, "stream[%d] incomplete", i );
378 tk->i_rate = p_strh->i_rate;
379 tk->i_scale = p_strh->i_scale;
380 tk->i_samplesize = p_strh->i_samplesize;
381 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
382 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
384 switch( p_strh->i_type )
386 case( AVIFOURCC_auds ):
387 tk->i_cat = AUDIO_ES;
388 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
389 p_auds->p_wf->wFormatTag );
390 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
392 if( p_auds->p_wf->wFormatTag == 1 )
394 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
401 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
403 fmt.audio.i_channels = p_auds->p_wf->nChannels;
404 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
405 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
406 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
407 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
408 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
409 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
410 fmt.p_extra = &p_auds->p_wf[1];
411 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
412 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
413 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
416 case( AVIFOURCC_vids ):
417 tk->i_cat = VIDEO_ES;
418 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
419 p_vids->p_bih->biCompression );
420 if( p_vids->p_bih->biCompression == 0x00 )
422 switch( p_vids->p_bih->biBitCount )
425 tk->i_codec = VLC_FOURCC('R','V','3','2');
428 tk->i_codec = VLC_FOURCC('R','V','2','4');
431 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
434 tk->i_codec = VLC_FOURCC('R','V','1','5');
437 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
440 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
442 if( p_vids->p_bih->biBitCount == 24 )
444 /* This is in BGR format */
445 fmt.video.i_bmask = 0x00ff0000;
446 fmt.video.i_gmask = 0x0000ff00;
447 fmt.video.i_rmask = 0x000000ff;
452 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
453 if( tk->i_codec == FOURCC_mp4v &&
454 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
456 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
459 tk->i_samplesize = 0;
460 fmt.video.i_width = p_vids->p_bih->biWidth;
461 fmt.video.i_height = p_vids->p_bih->biHeight;
462 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
463 fmt.video.i_frame_rate = tk->i_rate;
464 fmt.video.i_frame_rate_base = tk->i_scale;
466 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
467 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
468 fmt.p_extra = &p_vids->p_bih[1];
469 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
470 i, (char*)&p_vids->p_bih->biCompression,
471 p_vids->p_bih->biWidth,
472 p_vids->p_bih->biHeight,
473 p_vids->p_bih->biBitCount,
474 (float)tk->i_rate/(float)tk->i_scale );
476 if( p_vids->p_bih->biCompression == 0x00 )
478 /* RGB DIB are coded from bottom to top */
480 (unsigned int)(-(int)p_vids->p_bih->biHeight);
483 /* Extract palette from extradata if bpp <= 8
484 * (assumes that extradata contains only palette but appears
485 * to be true for all palettized codecs we support) */
486 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
487 fmt.video.i_bits_per_pixel > 0 )
491 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
492 fmt.video.p_palette->i_entries = 1;
494 /* Apparently this is necessary. But why ? */
496 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
497 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
499 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
500 GetDWLE((uint32_t*)fmt.p_extra + i);
505 case( AVIFOURCC_txts):
507 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
508 msg_Dbg( p_demux, "stream[%d] subtitles", i );
509 es_format_Init( &fmt, SPU_ES, tk->i_codec );
512 case( AVIFOURCC_iavs):
513 case( AVIFOURCC_ivas):
514 p_sys->b_muxed = VLC_TRUE;
515 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
516 if( p_strh->i_handler == FOURCC_dvsd ||
517 p_strh->i_handler == FOURCC_dvhd ||
518 p_strh->i_handler == FOURCC_dvsl )
520 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
521 if( !tk->p_out_muxed )
522 msg_Err( p_demux, "could not load the DV parser" );
528 case( AVIFOURCC_mids):
529 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
532 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
538 /* The charset of p_strn is undefined */
539 EnsureUTF8( p_strn->p_str );
540 fmt.psz_description = strdup( p_strn->p_str );
542 if( tk->p_out_muxed == NULL )
543 tk->p_es = es_out_Add( p_demux->out, &fmt );
544 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
547 if( p_sys->i_track <= 0 )
549 msg_Err( p_demux, "no valid track" );
553 i_do_index = config_GetInt( p_demux, "avi-index" );
554 if( i_do_index == 1 ) /* Always fix */
557 if( p_sys->b_seekable )
559 AVI_IndexCreate( p_demux );
563 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
564 AVI_IndexLoad( p_demux );
569 AVI_IndexLoad( p_demux );
572 /* *** movie length in sec *** */
573 p_sys->i_length = AVI_MovieGetLength( p_demux );
574 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
575 (mtime_t)p_avih->i_microsecperframe /
578 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
579 "axproximative or will have strange behaviour" );
580 if( i_do_index == 0 && !b_index )
583 i_create = intf_UserYesNo( p_demux, _("AVI Index") ,
584 _( "This AVI file is broken. Seeking will not "
585 "work correctly.\nDo you want to "
586 "try to repair it (this might take a long time) ?" ),
587 _( "Repair" ), _( "Don't repair" ), _( "Cancel") );
588 if( i_create == DIALOG_OK_YES )
591 msg_Dbg( p_demux, "Fixing AVI index" );
594 else if( i_create == DIALOG_CANCELLED )
597 p_demux->p_parent->b_die = VLC_TRUE;
603 /* fix some BeOS MediaKit generated file */
604 for( i = 0 ; i < p_sys->i_track; i++ )
606 avi_track_t *tk = p_sys->track[i];
607 avi_chunk_list_t *p_strl;
608 avi_chunk_strh_t *p_strh;
609 avi_chunk_strf_auds_t *p_auds;
611 if( tk->i_cat != AUDIO_ES )
615 if( tk->i_idxnb < 1 ||
617 tk->i_samplesize != 0 )
621 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
622 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
623 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
625 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
626 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
628 int64_t i_track_length =
629 tk->p_index[tk->i_idxnb-1].i_length +
630 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
631 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
632 (mtime_t)p_avih->i_microsecperframe;
636 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
639 tk->i_samplesize = 1;
640 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
641 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
645 if( p_sys->b_seekable )
647 /* we have read all chunk so go back to movi */
648 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
650 /* Skip movi header */
651 stream_Read( p_demux->s, NULL, 12 );
653 p_sys->i_movi_begin = p_movi->i_chunk_pos;
659 vlc_meta_Delete( p_sys->meta );
661 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
666 /*****************************************************************************
667 * Close: frees unused data
668 *****************************************************************************/
669 static void Close ( vlc_object_t * p_this )
671 demux_t * p_demux = (demux_t *)p_this;
673 demux_sys_t *p_sys = p_demux->p_sys ;
675 for( i = 0; i < p_sys->i_track; i++ )
677 if( p_sys->track[i] )
679 if( p_sys->track[i]->p_out_muxed )
680 stream_DemuxDelete( p_sys->track[i]->p_out_muxed );
681 FREENULL( p_sys->track[i]->p_index );
682 free( p_sys->track[i] );
685 FREENULL( p_sys->track );
686 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
687 vlc_meta_Delete( p_sys->meta );
692 /*****************************************************************************
693 * Demux_Seekable: reads and demuxes data packets for stream seekable
694 *****************************************************************************
695 * AVIDemux: reads and demuxes data packets
696 *****************************************************************************
697 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
698 *****************************************************************************/
705 off_t i_posf; /* where we will read :
706 if i_idxposb == 0 : begining of chunk (+8 to acces data)
707 else : point on data directly */
708 } avi_track_toread_t;
710 static int Demux_Seekable( demux_t *p_demux )
712 demux_sys_t *p_sys = p_demux->p_sys;
714 unsigned int i_track_count = 0;
715 unsigned int i_track;
717 /* cannot be more than 100 stream (dcXX or wbXX) */
718 avi_track_toread_t toread[100];
721 /* detect new selected/unselected streams */
722 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
724 avi_track_t *tk = p_sys->track[i_track];
727 if( p_sys->b_muxed && tk->p_out_muxed )
730 tk->b_activated = VLC_TRUE;
734 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
735 if( b && !tk->b_activated )
737 if( p_sys->b_seekable)
739 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
741 tk->b_activated = VLC_TRUE;
743 else if( !b && tk->b_activated )
745 tk->b_activated = VLC_FALSE;
753 if( i_track_count <= 0 )
755 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
757 p_sys->i_time += 25*1000; /* read 25ms */
760 if( p_sys->i_time >= i_length )
764 msg_Warn( p_demux, "no track selected, exiting..." );
768 /* wait for the good time */
769 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
770 p_sys->i_time += 25*1000; /* read 25ms */
773 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
775 avi_track_t *tk = p_sys->track[i_track];
778 toread[i_track].b_ok = tk->b_activated;
779 if( tk->i_idxposc < tk->i_idxnb )
781 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
782 if( tk->i_idxposb > 0 )
784 toread[i_track].i_posf += 8 + tk->i_idxposb;
789 toread[i_track].i_posf = -1;
792 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
794 if( tk->i_samplesize )
796 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
800 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
805 toread[i_track].i_toread *= -1;
809 b_stream = VLC_FALSE;
820 /* search for first chunk to be read */
821 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
823 if( !toread[i].b_ok ||
824 AVI_GetDPTS( p_sys->track[i],
825 toread[i].i_toread ) <= -25 * 1000 )
830 if( toread[i].i_toread > 0 )
832 b_done = VLC_FALSE; /* not yet finished */
834 if( toread[i].i_posf > 0 )
836 if( i_pos == -1 || i_pos > toread[i].i_posf )
839 i_pos = toread[i].i_posf;
851 int i_loop_count = 0;
853 /* no valid index, we will parse directly the stream
854 * in case we fail we will disable all finished stream */
855 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
857 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
858 if( AVI_PacketNext( p_demux ) )
860 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
865 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
872 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
875 "cannot get packet header, track disabled" );
876 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
878 if( avi_pk.i_stream >= p_sys->i_track ||
879 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
881 if( AVI_PacketNext( p_demux ) )
884 "cannot skip packet, track disabled" );
885 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
888 /* Prevents from eating all the CPU with broken files.
889 * This value should be low enough so that it doesn't
890 * affect the reading speed too much. */
891 if( !(++i_loop_count % 1024) )
893 if( p_demux->b_die ) return -1;
896 if( !(i_loop_count % (1024 * 10)) )
898 "don't seem to find any data..." );
904 /* add this chunk to the index */
907 index.i_id = avi_pk.i_fourcc;
909 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
911 index.i_pos = avi_pk.i_pos;
912 index.i_length = avi_pk.i_size;
913 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
915 i_track = avi_pk.i_stream;
916 tk = p_sys->track[i_track];
917 /* do we will read this data ? */
918 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
924 if( AVI_PacketNext( p_demux ) )
927 "cannot skip packet, track disabled" );
928 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
937 stream_Seek( p_demux->s, i_pos );
940 /* Set the track to use */
941 tk = p_sys->track[i_track];
943 /* read thoses data */
944 if( tk->i_samplesize )
946 unsigned int i_toread;
948 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
950 if( tk->i_samplesize > 1 )
952 i_toread = tk->i_samplesize;
956 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
957 i_toread = __MAX( i_toread, 100 );
960 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
966 i_size = tk->p_index[tk->i_idxposc].i_length;
969 if( tk->i_idxposb == 0 )
971 i_size += 8; /* need to read and skip header */
974 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
976 msg_Warn( p_demux, "failed reading data" );
977 tk->b_activated = VLC_FALSE;
978 toread[i_track].b_ok = VLC_FALSE;
981 if( i_size % 2 ) /* read was padded on word boundary */
986 if( tk->i_idxposb == 0 )
988 p_frame->p_buffer += 8;
989 p_frame->i_buffer -= 8;
991 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
992 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
994 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
998 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1002 if( tk->i_samplesize )
1004 if( tk->i_idxposb == 0 )
1008 toread[i_track].i_toread -= i_size;
1009 tk->i_idxposb += i_size;
1010 if( tk->i_idxposb >=
1011 tk->p_index[tk->i_idxposc].i_length )
1019 int i_length = tk->p_index[tk->i_idxposc].i_length;
1022 if( tk->i_cat == AUDIO_ES )
1024 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1026 toread[i_track].i_toread--;
1029 if( tk->i_idxposc < tk->i_idxnb)
1031 toread[i_track].i_posf =
1032 tk->p_index[tk->i_idxposc].i_pos;
1033 if( tk->i_idxposb > 0 )
1035 toread[i_track].i_posf += 8 + tk->i_idxposb;
1041 toread[i_track].i_posf = -1;
1044 b_stream = VLC_TRUE; /* at least one read succeed */
1046 if( tk->i_cat != VIDEO_ES )
1047 p_frame->i_dts = p_frame->i_pts;
1050 p_frame->i_dts = p_frame->i_pts;
1054 //p_pes->i_rate = p_demux->stream.control.i_rate;
1055 if( tk->p_out_muxed )
1056 stream_DemuxSend( tk->p_out_muxed, p_frame );
1058 es_out_Send( p_demux->out, tk->p_es, p_frame );
1063 /*****************************************************************************
1064 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1065 *****************************************************************************
1066 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1067 *****************************************************************************/
1068 static int Demux_UnSeekable( demux_t *p_demux )
1070 demux_sys_t *p_sys = p_demux->p_sys;
1071 avi_track_t *p_stream_master = NULL;
1072 unsigned int i_stream;
1073 unsigned int i_packet;
1075 if( p_sys->b_muxed )
1077 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1078 return VLC_EGENERIC;
1081 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1083 /* *** find master stream for data packet skipping algo *** */
1084 /* *** -> first video, if any, or first audio ES *** */
1085 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1087 avi_track_t *tk = p_sys->track[i_stream];
1090 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1092 if( b && tk->i_cat == VIDEO_ES )
1094 p_stream_master = tk;
1098 p_stream_master = tk;
1102 if( !p_stream_master )
1104 msg_Warn( p_demux, "no more stream selected" );
1108 p_sys->i_time = AVI_GetPTS( p_stream_master );
1110 for( i_packet = 0; i_packet < 10; i_packet++)
1112 #define p_stream p_sys->track[avi_pk.i_stream]
1114 avi_packet_t avi_pk;
1116 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1121 if( avi_pk.i_stream >= p_sys->i_track ||
1122 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1124 /* we haven't found an audio or video packet:
1125 * - we have seek, found first next packet
1126 * - others packets could be found, skip them
1128 switch( avi_pk.i_fourcc )
1130 case AVIFOURCC_JUNK:
1131 case AVIFOURCC_LIST:
1132 case AVIFOURCC_RIFF:
1133 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1134 case AVIFOURCC_idx1:
1137 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1139 return( 0 ); /* eof */
1142 "seems to have lost position, resync" );
1143 if( AVI_PacketSearch( p_demux ) )
1145 msg_Err( p_demux, "resync failed" );
1152 /* check for time */
1153 if( __ABS( AVI_GetPTS( p_stream ) -
1154 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1156 /* load it and send to decoder */
1158 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1162 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1164 if( avi_pk.i_cat != VIDEO_ES )
1165 p_frame->i_dts = p_frame->i_pts;
1168 p_frame->i_dts = p_frame->i_pts;
1172 //p_pes->i_rate = p_demux->stream.control.i_rate;
1173 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1177 if( AVI_PacketNext( p_demux ) )
1183 /* *** update stream time position *** */
1184 if( p_stream->i_samplesize )
1186 p_stream->i_idxposb += avi_pk.i_size;
1190 if( p_stream->i_cat == AUDIO_ES )
1192 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1194 p_stream->i_idxposc++;
1204 /*****************************************************************************
1205 * Seek: goto to i_date or i_percent
1206 *****************************************************************************/
1207 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1210 demux_sys_t *p_sys = p_demux->p_sys;
1211 unsigned int i_stream;
1212 msg_Dbg( p_demux, "seek requested: "I64Fd" seconds %d%%",
1213 i_date / 1000000, i_percent );
1215 if( p_sys->b_seekable )
1217 if( !p_sys->i_length )
1219 avi_track_t *p_stream;
1222 /* use i_percent to create a true i_date */
1223 msg_Warn( p_demux, "seeking without index at %d%%"
1224 " only works for interleaved files", i_percent );
1225 if( i_percent >= 100 )
1227 msg_Warn( p_demux, "cannot seek so far !" );
1228 return VLC_EGENERIC;
1230 i_percent = __MAX( i_percent, 0 );
1232 /* try to find chunk that is at i_percent or the file */
1233 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1234 p_sys->i_movi_begin );
1235 /* search first selected stream */
1236 for( i_stream = 0, p_stream = NULL;
1237 i_stream < p_sys->i_track; i_stream++ )
1239 p_stream = p_sys->track[i_stream];
1240 if( p_stream->b_activated )
1245 if( !p_stream || !p_stream->b_activated )
1247 msg_Warn( p_demux, "cannot find any selected stream" );
1248 return VLC_EGENERIC;
1251 /* be sure that the index exist */
1252 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1254 msg_Warn( p_demux, "cannot seek" );
1255 return VLC_EGENERIC;
1258 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1259 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1261 /* search after i_idxposc */
1262 if( AVI_StreamChunkSet( p_demux,
1263 i_stream, p_stream->i_idxposc + 1 ) )
1265 msg_Warn( p_demux, "cannot seek" );
1266 return VLC_EGENERIC;
1270 i_date = AVI_GetPTS( p_stream );
1271 /* TODO better support for i_samplesize != 0 */
1272 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1275 #define p_stream p_sys->track[i_stream]
1277 /* seek for chunk based streams */
1278 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1280 if( p_stream->b_activated && !p_stream->i_samplesize )
1281 /* if( p_stream->b_activated ) */
1283 AVI_TrackSeek( p_demux, i_stream, i_date );
1284 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1291 i_date = p_sys->i_time;
1293 /* seek for bytes based streams */
1294 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1296 if( p_stream->b_activated && p_stream->i_samplesize )
1298 AVI_TrackSeek( p_demux, i_stream, i_date );
1299 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1302 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1303 /* set true movie time */
1305 if( !p_sys->i_time )
1307 p_sys->i_time = i_date;
1314 msg_Err( p_demux, "shouldn't yet be executed" );
1315 return VLC_EGENERIC;
1319 /*****************************************************************************
1321 *****************************************************************************
1323 *****************************************************************************/
1324 static double ControlGetPosition( demux_t *p_demux )
1326 demux_sys_t *p_sys = p_demux->p_sys;
1328 if( p_sys->i_length > 0 )
1330 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1332 else if( stream_Size( p_demux->s ) > 0 )
1338 /* search the more advanced selected es */
1339 for( i = 0; i < p_sys->i_track; i++ )
1341 avi_track_t *tk = p_sys->track[i];
1342 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1344 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1345 tk->p_index[tk->i_idxposc].i_length + 8;
1352 return (double)i64 / stream_Size( p_demux->s );
1357 static int Control( demux_t *p_demux, int i_query, va_list args )
1359 demux_sys_t *p_sys = p_demux->p_sys;
1367 case DEMUX_GET_POSITION:
1368 pf = (double*)va_arg( args, double * );
1369 *pf = ControlGetPosition( p_demux );
1371 case DEMUX_SET_POSITION:
1372 f = (double)va_arg( args, double );
1373 if( p_sys->b_seekable )
1375 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1376 return Seek( p_demux, i64, (int)(f * 100) );
1380 int64_t i_pos = stream_Size( p_demux->s ) * f;
1381 return stream_Seek( p_demux->s, i_pos );
1384 case DEMUX_GET_TIME:
1385 pi64 = (int64_t*)va_arg( args, int64_t * );
1386 *pi64 = p_sys->i_time;
1389 case DEMUX_SET_TIME:
1393 i64 = (int64_t)va_arg( args, int64_t );
1394 if( p_sys->i_length > 0 )
1396 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1398 else if( p_sys->i_time > 0 )
1400 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1401 (double)i64 / (double)p_sys->i_time );
1403 return Seek( p_demux, i64, i_percent );
1405 case DEMUX_GET_LENGTH:
1406 pi64 = (int64_t*)va_arg( args, int64_t * );
1407 *pi64 = p_sys->i_length * (mtime_t)1000000;
1411 pf = (double*)va_arg( args, double * );
1413 for( i = 0; i < (int)p_sys->i_track; i++ )
1415 avi_track_t *tk = p_sys->track[i];
1416 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1418 *pf = (float)tk->i_rate / (float)tk->i_scale;
1423 case DEMUX_GET_META:
1424 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1425 vlc_meta_Merge( p_meta, p_sys->meta );
1429 return VLC_EGENERIC;
1433 /*****************************************************************************
1434 * Function to convert pts to chunk or byte
1435 *****************************************************************************/
1437 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1442 return (mtime_t)((int64_t)i_pts *
1443 (int64_t)tk->i_rate /
1444 (int64_t)tk->i_scale /
1447 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1449 if( !tk->i_scale || !tk->i_samplesize )
1452 return (mtime_t)((int64_t)i_pts *
1453 (int64_t)tk->i_rate /
1454 (int64_t)tk->i_scale /
1456 (int64_t)tk->i_samplesize );
1459 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1466 i_dpts = (mtime_t)( (int64_t)1000000 *
1468 (int64_t)tk->i_scale /
1469 (int64_t)tk->i_rate );
1471 if( tk->i_samplesize )
1473 return i_dpts / tk->i_samplesize;
1478 static mtime_t AVI_GetPTS( avi_track_t *tk )
1480 if( tk->i_samplesize )
1482 int64_t i_count = 0;
1484 /* we need a valid entry we will emulate one */
1485 if( tk->i_idxposc == tk->i_idxnb )
1489 /* use the last entry */
1490 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1491 + tk->p_index[tk->i_idxnb - 1].i_length;
1496 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1498 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1502 if( tk->i_cat == AUDIO_ES )
1504 return AVI_GetDPTS( tk, tk->i_blockno );
1508 return AVI_GetDPTS( tk, tk->i_idxposc );
1513 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1515 demux_sys_t *p_sys = p_demux->p_sys;
1516 avi_packet_t avi_pk;
1517 int i_loop_count = 0;
1519 /* find first chunk of i_stream that isn't in index */
1521 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1523 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1524 if( AVI_PacketNext( p_demux ) )
1526 return VLC_EGENERIC;
1531 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1536 if( p_demux->b_die ) return VLC_EGENERIC;
1538 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1540 msg_Warn( p_demux, "cannot get packet header" );
1541 return VLC_EGENERIC;
1543 if( avi_pk.i_stream >= p_sys->i_track ||
1544 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1546 if( AVI_PacketNext( p_demux ) )
1548 return VLC_EGENERIC;
1551 /* Prevents from eating all the CPU with broken files.
1552 * This value should be low enough so that it doesn't
1553 * affect the reading speed too much. */
1554 if( !(++i_loop_count % 1024) )
1556 if( p_demux->b_die ) return VLC_EGENERIC;
1559 if( !(i_loop_count % (1024 * 10)) )
1560 msg_Warn( p_demux, "don't seem to find any data..." );
1565 /* add this chunk to the index */
1568 index.i_id = avi_pk.i_fourcc;
1570 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1572 index.i_pos = avi_pk.i_pos;
1573 index.i_length = avi_pk.i_size;
1574 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1576 if( avi_pk.i_stream == i_stream )
1581 if( AVI_PacketNext( p_demux ) )
1583 return VLC_EGENERIC;
1589 /* be sure that i_ck will be a valid index entry */
1590 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1593 demux_sys_t *p_sys = p_demux->p_sys;
1594 avi_track_t *p_stream = p_sys->track[i_stream];
1596 p_stream->i_idxposc = i_ck;
1597 p_stream->i_idxposb = 0;
1599 if( i_ck >= p_stream->i_idxnb )
1601 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1604 p_stream->i_idxposc++;
1605 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1607 return VLC_EGENERIC;
1610 } while( p_stream->i_idxposc < i_ck );
1616 /* XXX FIXME up to now, we assume that all chunk are one after one */
1617 static int AVI_StreamBytesSet( demux_t *p_demux,
1618 unsigned int i_stream,
1621 demux_sys_t *p_sys = p_demux->p_sys;
1622 avi_track_t *p_stream = p_sys->track[i_stream];
1624 if( ( p_stream->i_idxnb > 0 )
1625 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1626 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1628 /* index is valid to find the ck */
1629 /* uses dichototmie to be fast enougth */
1630 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1631 int i_idxmax = p_stream->i_idxnb;
1635 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1637 i_idxmax = i_idxposc ;
1638 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1642 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1643 p_stream->p_index[i_idxposc].i_length <= i_byte)
1645 i_idxmin = i_idxposc ;
1646 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1650 p_stream->i_idxposc = i_idxposc;
1651 p_stream->i_idxposb = i_byte -
1652 p_stream->p_index[i_idxposc].i_lengthtotal;
1661 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1662 p_stream->i_idxposb = 0;
1665 p_stream->i_idxposc++;
1666 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1668 return VLC_EGENERIC;
1671 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1672 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1674 p_stream->i_idxposb = i_byte -
1675 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1680 static int AVI_TrackSeek( demux_t *p_demux,
1684 demux_sys_t *p_sys = p_demux->p_sys;
1685 avi_track_t *tk = p_sys->track[i_stream];
1687 #define p_stream p_sys->track[i_stream]
1690 i_oldpts = AVI_GetPTS( p_stream );
1692 if( !p_stream->i_samplesize )
1694 if( AVI_StreamChunkSet( p_demux,
1696 AVI_PTSToChunk( p_stream, i_date ) ) )
1698 return VLC_EGENERIC;
1701 if( p_stream->i_cat == AUDIO_ES )
1705 for( i = 0; i < tk->i_idxposc; i++ )
1707 if( tk->i_blocksize > 0 )
1709 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1719 "old:"I64Fd" %s new "I64Fd,
1721 i_oldpts > i_date ? ">" : "<",
1724 if( p_stream->i_cat == VIDEO_ES )
1726 /* search key frame */
1727 if( i_date < i_oldpts )
1729 while( p_stream->i_idxposc > 0 &&
1730 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1733 if( AVI_StreamChunkSet( p_demux,
1735 p_stream->i_idxposc - 1 ) )
1737 return VLC_EGENERIC;
1743 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1744 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1747 if( AVI_StreamChunkSet( p_demux,
1749 p_stream->i_idxposc + 1 ) )
1751 return VLC_EGENERIC;
1759 if( AVI_StreamBytesSet( p_demux,
1761 AVI_PTSToByte( p_stream, i_date ) ) )
1763 return VLC_EGENERIC;
1770 /****************************************************************************
1771 * Return VLC_TRUE if it's a key frame
1772 ****************************************************************************/
1773 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1779 * startcode: 0x00000100 32bits
1780 * framenumber ? 5bits
1781 * piture type 0(I),1(P) 2bits
1783 if( GetDWBE( p_byte ) != 0x00000100 )
1785 /* it's not an msmpegv1 stream, strange...*/
1786 return AVIIF_KEYFRAME;
1788 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1791 case FOURCC_DIV3: /* wmv1 also */
1793 * picture type 0(I),1(P) 2bits
1795 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1797 /* we should find first occurrence of 0x000001b6 (32bits)
1798 * startcode: 0x000001b6 32bits
1799 * piture type 0(I),1(P) 2bits
1801 if( GetDWBE( p_byte ) != 0x000001b6 )
1803 /* not true , need to find the first VOP header */
1804 return AVIIF_KEYFRAME;
1806 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1809 /* I can't do it, so say yes */
1810 return AVIIF_KEYFRAME;
1814 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1819 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1823 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1827 return VLC_FOURCC('m','r','l','e');
1876 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1880 /****************************************************************************
1882 ****************************************************************************/
1883 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1884 unsigned int *pi_number, unsigned int *pi_type )
1886 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1889 c1 = ((uint8_t *)&i_id)[0];
1890 c2 = ((uint8_t *)&i_id)[1];
1892 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1894 SET_PTR( pi_number, 100 ); /* > max stream number */
1895 SET_PTR( pi_type, UNKNOWN_ES );
1899 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1900 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1903 SET_PTR( pi_type, AUDIO_ES );
1907 SET_PTR( pi_type, VIDEO_ES );
1910 SET_PTR( pi_type, UNKNOWN_ES );
1917 /****************************************************************************
1919 ****************************************************************************/
1920 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1924 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1926 return VLC_EGENERIC;
1928 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1929 p_pk->i_size = GetDWLE( p_peek + 4 );
1930 p_pk->i_pos = stream_Tell( p_demux->s );
1931 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1933 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1934 p_peek[10], p_peek[11] );
1941 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1943 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1947 static int AVI_PacketNext( demux_t *p_demux )
1949 avi_packet_t avi_ck;
1952 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1954 return VLC_EGENERIC;
1957 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1958 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1962 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1963 avi_ck.i_type == AVIFOURCC_AVIX )
1969 i_skip = __EVEN( avi_ck.i_size ) + 8;
1972 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1974 return VLC_EGENERIC;
1979 static int AVI_PacketRead( demux_t *p_demux,
1981 block_t **pp_frame )
1985 i_size = __EVEN( p_pk->i_size + 8 );
1987 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1989 return VLC_EGENERIC;
1991 (*pp_frame)->p_buffer += 8;
1992 (*pp_frame)->i_buffer -= 8;
1994 if( i_size != p_pk->i_size + 8 )
1996 (*pp_frame)->i_buffer--;
2002 static int AVI_PacketSearch( demux_t *p_demux )
2004 demux_sys_t *p_sys = p_demux->p_sys;
2005 avi_packet_t avi_pk;
2010 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2012 return VLC_EGENERIC;
2014 AVI_PacketGetHeader( p_demux, &avi_pk );
2015 if( avi_pk.i_stream < p_sys->i_track &&
2016 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2020 switch( avi_pk.i_fourcc )
2022 case AVIFOURCC_JUNK:
2023 case AVIFOURCC_LIST:
2024 case AVIFOURCC_RIFF:
2025 case AVIFOURCC_idx1:
2029 /* Prevents from eating all the CPU with broken files.
2030 * This value should be low enough so that it doesn't affect the
2031 * reading speed too much (not that we care much anyway because
2032 * this code is called only on broken files). */
2033 if( !(++i_count % 1024) )
2035 if( p_demux->b_die ) return VLC_EGENERIC;
2038 if( !(i_count % (1024 * 10)) )
2039 msg_Warn( p_demux, "trying to resync..." );
2044 /****************************************************************************
2046 ****************************************************************************/
2047 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2049 avi_entry_t *p_index)
2051 avi_track_t *tk = p_sys->track[i_stream];
2053 /* Update i_movi_lastchunk_pos */
2054 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2056 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2060 if( tk->i_idxnb >= tk->i_idxmax )
2062 tk->i_idxmax += 16384;
2063 tk->p_index = realloc( tk->p_index,
2064 tk->i_idxmax * sizeof( avi_entry_t ) );
2065 if( tk->p_index == NULL )
2070 /* calculate cumulate length */
2071 if( tk->i_idxnb > 0 )
2073 p_index->i_lengthtotal =
2074 tk->p_index[tk->i_idxnb - 1].i_length +
2075 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2079 p_index->i_lengthtotal = 0;
2082 tk->p_index[tk->i_idxnb++] = *p_index;
2085 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2087 demux_sys_t *p_sys = p_demux->p_sys;
2089 avi_chunk_list_t *p_riff;
2090 avi_chunk_list_t *p_movi;
2091 avi_chunk_idx1_t *p_idx1;
2093 unsigned int i_stream;
2094 unsigned int i_index;
2098 vlc_bool_t b_keyset[100];
2100 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2101 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2102 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2106 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2107 return VLC_EGENERIC;
2110 /* *** calculate offset *** */
2111 /* Well, avi is __SHIT__ so test more than one entry
2112 * (needed for some avi files) */
2114 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2116 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2118 i_offset = p_movi->i_chunk_pos + 8;
2123 /* Reset b_keyset */
2124 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2125 b_keyset[i_stream] = VLC_FALSE;
2127 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2131 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2134 if( i_stream < p_sys->i_track &&
2135 i_cat == p_sys->track[i_stream]->i_cat )
2138 index.i_id = p_idx1->entry[i_index].i_fourcc;
2140 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2141 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2142 index.i_length = p_idx1->entry[i_index].i_length;
2143 AVI_IndexAddEntry( p_sys, i_stream, &index );
2145 if( index.i_flags&AVIIF_KEYFRAME )
2146 b_keyset[i_stream] = VLC_TRUE;
2150 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2152 if( !b_keyset[i_stream] )
2154 avi_track_t *tk = p_sys->track[i_stream];
2156 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2157 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2158 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2164 static void __Parse_indx( demux_t *p_demux,
2166 avi_chunk_indx_t *p_indx )
2168 demux_sys_t *p_sys = p_demux->p_sys;
2172 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2173 if( p_indx->i_indexsubtype == 0 )
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.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2179 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2180 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2182 AVI_IndexAddEntry( p_sys, i_stream, &index );
2185 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2187 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2189 index.i_id = p_indx->i_id;
2190 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2191 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2192 index.i_length = p_indx->idx.field[i].i_size;
2194 AVI_IndexAddEntry( p_sys, i_stream, &index );
2199 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2203 static void AVI_IndexLoad_indx( demux_t *p_demux )
2205 demux_sys_t *p_sys = p_demux->p_sys;
2206 unsigned int i_stream;
2209 avi_chunk_list_t *p_riff;
2210 avi_chunk_list_t *p_hdrl;
2212 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2213 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2215 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2217 avi_chunk_list_t *p_strl;
2218 avi_chunk_indx_t *p_indx;
2220 #define p_stream p_sys->track[i_stream]
2221 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2222 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2226 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2231 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2233 __Parse_indx( p_demux, i_stream, p_indx );
2235 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2238 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2240 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2241 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2245 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2250 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2256 static void AVI_IndexLoad( demux_t *p_demux )
2258 demux_sys_t *p_sys = p_demux->p_sys;
2259 unsigned int i_stream;
2261 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2263 p_sys->track[i_stream]->i_idxnb = 0;
2264 p_sys->track[i_stream]->i_idxmax = 0;
2265 p_sys->track[i_stream]->p_index = NULL;
2270 AVI_IndexLoad_indx( p_demux );
2272 else if( AVI_IndexLoad_idx1( p_demux ) )
2274 /* try indx if idx1 failed as some "normal" file have indx too */
2275 AVI_IndexLoad_indx( p_demux );
2278 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2280 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2281 i_stream, p_sys->track[i_stream]->i_idxnb );
2285 static void AVI_IndexCreate( demux_t *p_demux )
2287 demux_sys_t *p_sys = p_demux->p_sys;
2289 avi_chunk_list_t *p_riff;
2290 avi_chunk_list_t *p_movi;
2292 unsigned int i_stream;
2295 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2296 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2300 msg_Err( p_demux, "cannot find p_movi" );
2304 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2306 p_sys->track[i_stream]->i_idxnb = 0;
2307 p_sys->track[i_stream]->i_idxmax = 0;
2308 p_sys->track[i_stream]->p_index = NULL;
2310 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2311 stream_Size( p_demux->s ) );
2313 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2314 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2317 /* Only show dialog if AVI is > 10MB */
2318 p_demux->p_sys->i_dialog_id = -1;
2319 if( stream_Size( p_demux->s ) > 10000000 )
2321 p_demux->p_sys->i_dialog_id = intf_IntfProgress( p_demux,
2322 _( "Fixing AVI Index..." ),
2324 p_demux->p_sys->last_update = mdate();
2331 if( p_demux->b_die )
2336 /* Don't update dialog too often */
2337 if( p_demux->p_sys->i_dialog_id > 0 &&
2338 mdate() - p_demux->p_sys->last_update > 100000 )
2340 int64_t i_pos = stream_Tell( p_demux->s )* 100 /
2341 stream_Size( p_demux->s );
2342 float f_pos = (float)i_pos;
2343 p_demux->p_sys->last_update = mdate();
2344 intf_ProgressUpdate( p_demux, p_demux->p_sys->i_dialog_id,
2345 _( "Fixing AVI Index..." ), f_pos, -1 );
2348 if( AVI_PacketGetHeader( p_demux, &pk ) )
2352 if( pk.i_stream < p_sys->i_track &&
2353 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2356 index.i_id = pk.i_fourcc;
2358 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2359 index.i_pos = pk.i_pos;
2360 index.i_length = pk.i_size;
2361 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2365 switch( pk.i_fourcc )
2367 case AVIFOURCC_idx1:
2370 avi_chunk_list_t *p_sysx;
2371 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2372 AVIFOURCC_RIFF, 1 );
2374 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2375 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2382 case AVIFOURCC_RIFF:
2383 msg_Dbg( p_demux, "new RIFF chunk found" );
2385 case AVIFOURCC_JUNK:
2388 msg_Warn( p_demux, "need resync, probably broken avi" );
2389 if( AVI_PacketSearch( p_demux ) )
2391 msg_Warn( p_demux, "lost sync, abord index creation" );
2397 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2398 AVI_PacketNext( p_demux ) )
2405 if( p_demux->p_sys->i_dialog_id > 0 )
2407 intf_UserHide( p_demux, p_demux->p_sys->i_dialog_id );
2410 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2413 "stream[%d] creating %d index entries",
2415 p_sys->track[i_stream]->i_idxnb );
2419 /*****************************************************************************
2421 *****************************************************************************/
2422 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2424 demux_sys_t *p_sys = p_demux->p_sys;
2426 int b_end = VLC_TRUE;
2428 for( i = 0; i < p_sys->i_track; i++ )
2430 avi_track_t *tk = p_sys->track[i];
2431 if( tk->i_idxposc >= tk->i_idxnb )
2433 tk->b_activated = VLC_FALSE;
2434 if( tk->p_es ) es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2444 /****************************************************************************
2445 * AVI_MovieGetLength give max streams length in second
2446 ****************************************************************************/
2447 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2449 demux_sys_t *p_sys = p_demux->p_sys;
2450 mtime_t i_maxlength = 0;
2453 for( i = 0; i < p_sys->i_track; i++ )
2455 avi_track_t *tk = p_sys->track[i];
2458 /* fix length for each stream */
2459 if( tk->i_idxnb < 1 || !tk->p_index )
2464 if( tk->i_samplesize )
2466 i_length = AVI_GetDPTS( tk,
2467 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2468 tk->p_index[tk->i_idxnb-1].i_length );
2472 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2474 i_length /= (mtime_t)1000000; /* in seconds */
2477 "stream[%d] length:"I64Fd" (based on index)",
2480 i_maxlength = __MAX( i_maxlength, i_length );