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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
23 /*****************************************************************************
25 *****************************************************************************/
26 #include <stdlib.h> /* malloc(), free() */
29 #include <vlc/input.h>
31 #include <vlc_interaction.h>
38 /*****************************************************************************
40 *****************************************************************************/
42 #define INTERLEAVE_TEXT N_("Force interleaved method" )
43 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method" )
45 #define INDEX_TEXT N_("Force index creation")
46 #define INDEX_LONGTEXT N_( \
47 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
48 "or incomplete (not seekable)" )
50 static int Open ( vlc_object_t * );
51 static void Close( vlc_object_t * );
54 set_shortname( "AVI" );
55 set_description( _("AVI demuxer") );
56 set_capability( "demux2", 212 );
57 set_category( CAT_INPUT );
58 set_subcategory( SUBCAT_INPUT_DEMUX );
60 add_bool( "avi-interleaved", 0, NULL,
61 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, VLC_TRUE );
62 add_bool( "avi-index", 0, NULL,
63 INDEX_TEXT, INDEX_LONGTEXT, VLC_FALSE );
65 set_callbacks( Open, Close );
68 /*****************************************************************************
70 *****************************************************************************/
71 static int Control ( demux_t *, int, va_list );
72 static int Seek ( demux_t *, mtime_t, int );
73 static int Demux_Seekable ( demux_t * );
74 static int Demux_UnSeekable( demux_t * );
76 #define FREE( p ) if( p ) { free( p ); (p) = NULL; }
77 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
81 vlc_fourcc_t i_fourcc;
84 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
86 uint8_t i_peek[8]; /* first 8 bytes */
88 unsigned int i_stream;
99 uint32_t i_lengthtotal;
105 vlc_bool_t b_activated;
107 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
108 vlc_fourcc_t i_codec;
116 avi_entry_t *p_index;
117 unsigned int i_idxnb;
118 unsigned int i_idxmax;
120 unsigned int i_idxposc; /* numero of chunk */
121 unsigned int i_idxposb; /* byte in the current chunk */
123 /* For VBR audio only */
124 unsigned int i_blockno;
125 unsigned int i_blocksize;
133 vlc_bool_t b_seekable;
139 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
141 /* number of streams and information */
142 unsigned int i_track;
149 static inline off_t __EVEN( off_t i )
151 return (i & 1) ? i + 1 : i;
154 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
155 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
156 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
157 static mtime_t AVI_GetPTS ( avi_track_t * );
160 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
161 static int AVI_StreamChunkSet ( demux_t *,
162 unsigned int i_stream, unsigned int i_ck );
163 static int AVI_StreamBytesSet ( demux_t *,
164 unsigned int i_stream, off_t i_byte );
166 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
167 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
169 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
170 static int AVI_PacketNext ( demux_t * );
171 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
172 static int AVI_PacketSearch ( demux_t * );
174 static void AVI_IndexLoad ( demux_t * );
175 static void AVI_IndexCreate ( demux_t * );
176 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
178 static mtime_t AVI_MovieGetLength( demux_t * );
180 /*****************************************************************************
182 *****************************************************************************/
183 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
184 static int AVI_TrackStopFinishedStreams( demux_t *);
187 - For VBR mp3 stream:
188 count blocks by rounded-up chunksizes instead of chunks
189 we need full emulation of dshow avi demuxer bugs :(
190 fixes silly nandub-style a-v delaying in avi with vbr mp3...
191 (from mplayer 2002/08/02)
195 /*****************************************************************************
196 * Open: check file and initializes AVI structures
197 *****************************************************************************/
198 static int Open( vlc_object_t * p_this )
200 demux_t *p_demux = (demux_t *)p_this;
203 vlc_bool_t b_index = VLC_FALSE;
206 avi_chunk_list_t *p_riff = (avi_chunk_list_t*)&ck_riff;
207 avi_chunk_list_t *p_hdrl, *p_movi;
208 avi_chunk_avih_t *p_avih;
210 unsigned int i_track;
211 unsigned int i, i_peeker;
215 /* Is it an avi file ? */
216 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
218 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
220 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) &&
221 !strncmp( (char *)&p_peek[8], "AVI ", 4 ) ) break;
224 if( i_peeker == 188 )
229 /* Initialize input structures. */
230 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
231 memset( p_sys, 0, sizeof( demux_sys_t ) );
234 p_sys->i_movi_lastchunk_pos = 0;
235 p_sys->b_odml = VLC_FALSE;
240 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
242 p_demux->pf_control = Control;
243 p_demux->pf_demux = Demux_Seekable;
245 /* For unseekable stream, automaticaly use Demux_UnSeekable */
246 if( !p_sys->b_seekable || config_GetInt( p_demux, "avi-interleaved" ) )
248 p_demux->pf_demux = Demux_UnSeekable;
253 stream_Read( p_demux->s, NULL, i_peeker );
256 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
258 msg_Err( p_demux, "avi module discarded (invalid file)" );
262 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
264 unsigned int i_count =
265 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
267 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
268 for( i = 1; i < i_count; i++ )
270 avi_chunk_list_t *p_sysx;
272 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
273 if( p_sysx->i_type == AVIFOURCC_AVIX )
275 msg_Warn( p_demux, "detected OpenDML file" );
276 p_sys->b_odml = VLC_TRUE;
282 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
283 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
284 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
286 if( !p_hdrl || !p_movi )
288 msg_Err( p_demux, "avi module discarded (invalid file)" );
292 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
294 msg_Err( p_demux, "cannot find avih chunk" );
297 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
298 if( p_avih->i_streams != i_track )
301 "found %d stream but %d are declared",
302 i_track, p_avih->i_streams );
306 msg_Err( p_demux, "no stream defined!" );
310 /* print information on streams */
311 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
313 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
314 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
315 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
316 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
317 if( ( p_sys->meta = vlc_meta_New() ) )
320 sprintf( buffer, "%s%s%s%s",
321 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
322 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
323 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
324 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
325 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, buffer );
328 /* now read info on each stream and create ES */
329 for( i = 0 ; i < i_track; i++ )
331 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
332 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
333 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
334 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
335 avi_chunk_strf_auds_t *p_auds;
336 avi_chunk_strf_vids_t *p_vids;
339 tk->b_activated = VLC_FALSE;
349 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
350 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
352 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
354 msg_Warn( p_demux, "stream[%d] incomplete", i );
358 tk->i_rate = p_strh->i_rate;
359 tk->i_scale = p_strh->i_scale;
360 tk->i_samplesize = p_strh->i_samplesize;
361 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
362 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
364 switch( p_strh->i_type )
366 case( AVIFOURCC_auds ):
367 tk->i_cat = AUDIO_ES;
368 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
369 p_auds->p_wf->wFormatTag );
370 if( ( tk->i_blocksize = p_auds->p_wf->nBlockAlign ) == 0 )
372 if( p_auds->p_wf->wFormatTag == 1 )
374 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
381 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
383 fmt.audio.i_channels = p_auds->p_wf->nChannels;
384 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
385 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
386 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
387 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
388 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
389 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
390 fmt.p_extra = &p_auds->p_wf[1];
391 msg_Dbg( p_demux, "stream[%d] audio(0x%x) %d channels %dHz %dbits",
392 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
393 p_auds->p_wf->nSamplesPerSec, p_auds->p_wf->wBitsPerSample);
396 case( AVIFOURCC_vids ):
397 tk->i_cat = VIDEO_ES;
398 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
399 p_vids->p_bih->biCompression );
400 if( p_vids->p_bih->biCompression == 0x00 )
402 switch( p_vids->p_bih->biBitCount )
405 tk->i_codec = VLC_FOURCC('R','V','3','2');
408 tk->i_codec = VLC_FOURCC('R','V','2','4');
411 /* tk->i_codec = VLC_FOURCC('R','V','1','6');*/
414 tk->i_codec = VLC_FOURCC('R','V','1','5');
417 tk->i_codec = VLC_FOURCC( 'Y', 'V', 'U', '9' ); /* <- TODO check that */
420 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
422 if( p_vids->p_bih->biBitCount == 24 )
424 /* This is in BGR format */
425 fmt.video.i_bmask = 0x00ff0000;
426 fmt.video.i_gmask = 0x0000ff00;
427 fmt.video.i_rmask = 0x000000ff;
432 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
433 if( tk->i_codec == FOURCC_mp4v &&
434 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
436 fmt.i_codec = VLC_FOURCC( 'X', 'V', 'I', 'D' );
439 tk->i_samplesize = 0;
440 fmt.video.i_width = p_vids->p_bih->biWidth;
441 fmt.video.i_height = p_vids->p_bih->biHeight;
442 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
443 fmt.video.i_frame_rate = tk->i_rate;
444 fmt.video.i_frame_rate_base = tk->i_scale;
446 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
447 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
448 fmt.p_extra = &p_vids->p_bih[1];
449 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %dx%d %dbpp %ffps",
450 i, (char*)&p_vids->p_bih->biCompression,
451 p_vids->p_bih->biWidth,
452 p_vids->p_bih->biHeight,
453 p_vids->p_bih->biBitCount,
454 (float)tk->i_rate/(float)tk->i_scale );
456 if( p_vids->p_bih->biCompression == 0x00 )
458 /* RGB DIB are coded from bottom to top */
460 (unsigned int)(-(int)p_vids->p_bih->biHeight);
463 /* Extract palette from extradata if bpp <= 8
464 * (assumes that extradata contains only palette but appears
465 * to be true for all palettized codecs we support) */
466 if( fmt.i_extra && fmt.video.i_bits_per_pixel <= 8 &&
467 fmt.video.i_bits_per_pixel > 0 )
471 fmt.video.p_palette = calloc( sizeof(video_palette_t), 1 );
472 fmt.video.p_palette->i_entries = 1;
474 /* Apparently this is necessary. But why ? */
476 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
477 for( i = 0; i < __MIN(fmt.i_extra/4, 256); i++ )
479 ((uint32_t *)&fmt.video.p_palette->palette[0][0])[i] =
480 GetDWLE((uint32_t*)fmt.p_extra + i);
485 case( AVIFOURCC_txts):
487 tk->i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
488 msg_Dbg( p_demux, "stream[%d] subtitles", i );
489 es_format_Init( &fmt, SPU_ES, tk->i_codec );
492 case( AVIFOURCC_mids):
493 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
496 msg_Warn( p_demux, "stream[%d] unknown type", i );
502 fmt.psz_description = strdup( p_strn->p_str );
504 tk->p_es = es_out_Add( p_demux->out, &fmt );
505 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
508 if( p_sys->i_track <= 0 )
510 msg_Err( p_demux, "no valid track" );
514 if( config_GetInt( p_demux, "avi-index" ) )
517 if( p_sys->b_seekable )
519 AVI_IndexCreate( p_demux );
523 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
524 AVI_IndexLoad( p_demux );
529 AVI_IndexLoad( p_demux );
532 /* *** movie length in sec *** */
533 p_sys->i_length = AVI_MovieGetLength( p_demux );
534 if( p_sys->i_length < (mtime_t)p_avih->i_totalframes *
535 (mtime_t)p_avih->i_microsecperframe /
538 msg_Warn( p_demux, "broken or missing index, 'seek' will be axproximative or will have "
539 "strange behavour" );
543 i_create = intf_UserYesNo( p_demux, "AVI Index", _("This AVI file is broken. Seeking will not "
544 "work correctly.\nDo you want to "
545 "try to repair it (this might take a long time) ?" ) );
546 if( i_create == DIALOG_OK_YES )
549 msg_Dbg( p_demux, "Fixing AVI index" );
552 else if( i_create == DIALOG_CANCELLED )
555 p_demux->p_parent->b_die = VLC_TRUE;
561 /* fix some BeOS MediaKit generated file */
562 for( i = 0 ; i < p_sys->i_track; i++ )
564 avi_track_t *tk = p_sys->track[i];
565 avi_chunk_list_t *p_strl;
566 avi_chunk_strh_t *p_strh;
567 avi_chunk_strf_auds_t *p_auds;
569 if( tk->i_cat != AUDIO_ES )
573 if( tk->i_idxnb < 1 ||
575 tk->i_samplesize != 0 )
579 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
580 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
581 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
583 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
584 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
586 int64_t i_track_length =
587 tk->p_index[tk->i_idxnb-1].i_length +
588 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
589 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
590 (mtime_t)p_avih->i_microsecperframe;
594 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
597 tk->i_samplesize = 1;
598 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
599 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
603 if( p_sys->b_seekable )
605 /* we have read all chunk so go back to movi */
606 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
608 /* Skip movi header */
609 stream_Read( p_demux->s, NULL, 12 );
611 p_sys->i_movi_begin = p_movi->i_chunk_pos;
617 vlc_meta_Delete( p_sys->meta );
619 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
624 /*****************************************************************************
625 * Close: frees unused data
626 *****************************************************************************/
627 static void Close ( vlc_object_t * p_this )
629 demux_t * p_demux = (demux_t *)p_this;
631 demux_sys_t *p_sys = p_demux->p_sys ;
633 for( i = 0; i < p_sys->i_track; i++ )
635 if( p_sys->track[i] )
637 FREE( p_sys->track[i]->p_index );
638 free( p_sys->track[i] );
641 FREE( p_sys->track );
642 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
643 vlc_meta_Delete( p_sys->meta );
648 /*****************************************************************************
649 * Demux_Seekable: reads and demuxes data packets for stream seekable
650 *****************************************************************************
651 * AVIDemux: reads and demuxes data packets
652 *****************************************************************************
653 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
654 *****************************************************************************/
661 off_t i_posf; /* where we will read :
662 if i_idxposb == 0 : begining of chunk (+8 to acces data)
663 else : point on data directly */
664 } avi_track_toread_t;
666 static int Demux_Seekable( demux_t *p_demux )
668 demux_sys_t *p_sys = p_demux->p_sys;
670 unsigned int i_track_count = 0;
671 unsigned int i_track;
673 /* cannot be more than 100 stream (dcXX or wbXX) */
674 avi_track_toread_t toread[100];
677 /* detect new selected/unselected streams */
678 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
680 avi_track_t *tk = p_sys->track[i_track];
683 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
684 if( b && !tk->b_activated )
686 if( p_sys->b_seekable)
688 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
690 tk->b_activated = VLC_TRUE;
692 else if( !b && tk->b_activated )
694 tk->b_activated = VLC_FALSE;
702 if( i_track_count <= 0 )
704 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
706 p_sys->i_time += 25*1000; /* read 25ms */
709 if( p_sys->i_time >= i_length )
713 msg_Warn( p_demux, "no track selected, exiting..." );
717 /* wait for the good time */
718 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
719 p_sys->i_time += 25*1000; /* read 25ms */
722 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
724 avi_track_t *tk = p_sys->track[i_track];
727 toread[i_track].b_ok = tk->b_activated;
728 if( tk->i_idxposc < tk->i_idxnb )
730 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
731 if( tk->i_idxposb > 0 )
733 toread[i_track].i_posf += 8 + tk->i_idxposb;
738 toread[i_track].i_posf = -1;
741 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
743 if( tk->i_samplesize )
745 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
749 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
754 toread[i_track].i_toread *= -1;
758 b_stream = VLC_FALSE;
769 /* search for first chunk to be read */
770 for( i = 0, b_done = VLC_TRUE, i_pos = -1; i < p_sys->i_track; i++ )
772 if( !toread[i].b_ok ||
773 AVI_GetDPTS( p_sys->track[i],
774 toread[i].i_toread ) <= -25 * 1000 )
779 if( toread[i].i_toread > 0 )
781 b_done = VLC_FALSE; /* not yet finished */
783 if( toread[i].i_posf > 0 )
785 if( i_pos == -1 || i_pos > toread[i_track].i_posf )
788 i_pos = toread[i].i_posf;
800 int i_loop_count = 0;
802 /* no valid index, we will parse directly the stream
803 * in case we fail we will disable all finished stream */
804 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
806 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
807 if( AVI_PacketNext( p_demux ) )
809 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
814 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
821 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
824 "cannot get packet header, track disabled" );
825 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
827 if( avi_pk.i_stream >= p_sys->i_track ||
828 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
830 if( AVI_PacketNext( p_demux ) )
833 "cannot skip packet, track disabled" );
834 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
837 /* Prevents from eating all the CPU with broken files.
838 * This value should be low enough so that it doesn't
839 * affect the reading speed too much. */
840 if( !(++i_loop_count % 1024) )
842 if( p_demux->b_die ) return -1;
845 if( !(i_loop_count % (1024 * 10)) )
847 "don't seem to find any data..." );
853 /* add this chunk to the index */
856 index.i_id = avi_pk.i_fourcc;
858 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
860 index.i_pos = avi_pk.i_pos;
861 index.i_length = avi_pk.i_size;
862 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
864 i_track = avi_pk.i_stream;
865 tk = p_sys->track[i_track];
866 /* do we will read this data ? */
867 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
873 if( AVI_PacketNext( p_demux ) )
876 "cannot skip packet, track disabled" );
877 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
886 stream_Seek( p_demux->s, i_pos );
889 /* Set the track to use */
890 tk = p_sys->track[i_track];
892 /* read thoses data */
893 if( tk->i_samplesize )
895 unsigned int i_toread;
897 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
899 if( tk->i_samplesize > 1 )
901 i_toread = tk->i_samplesize;
905 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
906 i_toread = __MAX( i_toread, 100 );
909 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
915 i_size = tk->p_index[tk->i_idxposc].i_length;
918 if( tk->i_idxposb == 0 )
920 i_size += 8; /* need to read and skip header */
923 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
925 msg_Warn( p_demux, "failed reading data" );
926 tk->b_activated = VLC_FALSE;
927 toread[i_track].b_ok = VLC_FALSE;
930 if( i_size % 2 ) /* read was padded on word boundary */
935 if( tk->i_idxposb == 0 )
937 p_frame->p_buffer += 8;
938 p_frame->i_buffer -= 8;
940 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
941 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
943 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
947 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
951 if( tk->i_samplesize )
953 if( tk->i_idxposb == 0 )
957 toread[i_track].i_toread -= i_size;
958 tk->i_idxposb += i_size;
960 tk->p_index[tk->i_idxposc].i_length )
968 int i_length = tk->p_index[tk->i_idxposc].i_length;
971 if( tk->i_cat == AUDIO_ES )
973 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
975 toread[i_track].i_toread--;
978 if( tk->i_idxposc < tk->i_idxnb)
980 toread[i_track].i_posf =
981 tk->p_index[tk->i_idxposc].i_pos;
982 if( tk->i_idxposb > 0 )
984 toread[i_track].i_posf += 8 + tk->i_idxposb;
990 toread[i_track].i_posf = -1;
993 b_stream = VLC_TRUE; /* at least one read succeed */
995 if( tk->i_cat != VIDEO_ES )
996 p_frame->i_dts = p_frame->i_pts;
999 p_frame->i_dts = p_frame->i_pts;
1003 //p_pes->i_rate = p_demux->stream.control.i_rate;
1004 es_out_Send( p_demux->out, tk->p_es, p_frame );
1009 /*****************************************************************************
1010 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1011 *****************************************************************************
1012 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1013 *****************************************************************************/
1014 static int Demux_UnSeekable( demux_t *p_demux )
1016 demux_sys_t *p_sys = p_demux->p_sys;
1017 avi_track_t *p_stream_master = NULL;
1018 unsigned int i_stream;
1019 unsigned int i_packet;
1021 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1023 /* *** find master stream for data packet skipping algo *** */
1024 /* *** -> first video, if any, or first audio ES *** */
1025 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1027 avi_track_t *tk = p_sys->track[i_stream];
1030 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1032 if( b && tk->i_cat == VIDEO_ES )
1034 p_stream_master = tk;
1038 p_stream_master = tk;
1042 if( !p_stream_master )
1044 msg_Warn( p_demux, "no more stream selected" );
1048 p_sys->i_time = AVI_GetPTS( p_stream_master );
1050 for( i_packet = 0; i_packet < 10; i_packet++)
1052 #define p_stream p_sys->track[avi_pk.i_stream]
1054 avi_packet_t avi_pk;
1056 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1061 if( avi_pk.i_stream >= p_sys->i_track ||
1062 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1064 /* we haven't found an audio or video packet:
1065 * - we have seek, found first next packet
1066 * - others packets could be found, skip them
1068 switch( avi_pk.i_fourcc )
1070 case AVIFOURCC_JUNK:
1071 case AVIFOURCC_LIST:
1072 case AVIFOURCC_RIFF:
1073 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1074 case AVIFOURCC_idx1:
1077 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1079 return( 0 ); /* eof */
1082 "seems to have lost position, resync" );
1083 if( AVI_PacketSearch( p_demux ) )
1085 msg_Err( p_demux, "resync failed" );
1092 /* check for time */
1093 if( __ABS( AVI_GetPTS( p_stream ) -
1094 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1096 /* load it and send to decoder */
1098 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1102 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1104 if( avi_pk.i_cat != VIDEO_ES )
1105 p_frame->i_dts = p_frame->i_pts;
1108 p_frame->i_dts = p_frame->i_pts;
1112 //p_pes->i_rate = p_demux->stream.control.i_rate;
1113 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1117 if( AVI_PacketNext( p_demux ) )
1123 /* *** update stream time position *** */
1124 if( p_stream->i_samplesize )
1126 p_stream->i_idxposb += avi_pk.i_size;
1130 if( p_stream->i_cat == AUDIO_ES )
1132 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1134 p_stream->i_idxposc++;
1144 /*****************************************************************************
1145 * Seek: goto to i_date or i_percent
1146 *****************************************************************************/
1147 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1150 demux_sys_t *p_sys = p_demux->p_sys;
1151 unsigned int i_stream;
1152 msg_Dbg( p_demux, "seek requested: "I64Fd" secondes %d%%",
1153 i_date / 1000000, i_percent );
1155 if( p_sys->b_seekable )
1157 if( !p_sys->i_length )
1159 avi_track_t *p_stream;
1162 /* use i_percent to create a true i_date */
1163 msg_Warn( p_demux, "mmh, seeking without index at %d%%"
1164 " work only for interleaved file", i_percent );
1165 if( i_percent >= 100 )
1167 msg_Warn( p_demux, "cannot seek so far !" );
1168 return VLC_EGENERIC;
1170 i_percent = __MAX( i_percent, 0 );
1172 /* try to find chunk that is at i_percent or the file */
1173 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1174 p_sys->i_movi_begin );
1175 /* search first selected stream */
1176 for( i_stream = 0, p_stream = NULL;
1177 i_stream < p_sys->i_track; i_stream++ )
1179 p_stream = p_sys->track[i_stream];
1180 if( p_stream->b_activated )
1185 if( !p_stream || !p_stream->b_activated )
1187 msg_Warn( p_demux, "cannot find any selected stream" );
1188 return VLC_EGENERIC;
1191 /* be sure that the index exist */
1192 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1194 msg_Warn( p_demux, "cannot seek" );
1195 return VLC_EGENERIC;
1198 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1199 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1201 /* search after i_idxposc */
1202 if( AVI_StreamChunkSet( p_demux,
1203 i_stream, p_stream->i_idxposc + 1 ) )
1205 msg_Warn( p_demux, "cannot seek" );
1206 return VLC_EGENERIC;
1210 i_date = AVI_GetPTS( p_stream );
1211 /* TODO better support for i_samplesize != 0 */
1212 msg_Dbg( p_demux, "estimate date "I64Fd, i_date );
1215 #define p_stream p_sys->track[i_stream]
1217 /* seek for chunk based streams */
1218 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1220 if( p_stream->b_activated && !p_stream->i_samplesize )
1221 /* if( p_stream->b_activated ) */
1223 AVI_TrackSeek( p_demux, i_stream, i_date );
1224 p_sys->i_time = __MAX( AVI_GetPTS( p_stream ),
1231 i_date = p_sys->i_time;
1233 /* seek for bytes based streams */
1234 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1236 if( p_stream->b_activated && p_stream->i_samplesize )
1238 AVI_TrackSeek( p_demux, i_stream, i_date );
1239 /* p_sys->i_time = __MAX( AVI_GetPTS( p_stream ), p_sys->i_time );*/
1242 msg_Dbg( p_demux, "seek: "I64Fd" seconds", p_sys->i_time /1000000 );
1243 /* set true movie time */
1245 if( !p_sys->i_time )
1247 p_sys->i_time = i_date;
1254 msg_Err( p_demux, "shouldn't yet be executed" );
1255 return VLC_EGENERIC;
1259 /*****************************************************************************
1261 *****************************************************************************
1263 *****************************************************************************/
1264 static double ControlGetPosition( demux_t *p_demux )
1266 demux_sys_t *p_sys = p_demux->p_sys;
1268 if( p_sys->i_length > 0 )
1270 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1272 else if( stream_Size( p_demux->s ) > 0 )
1278 /* search the more advanced selected es */
1279 for( i = 0; i < p_sys->i_track; i++ )
1281 avi_track_t *tk = p_sys->track[i];
1282 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1284 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1285 tk->p_index[tk->i_idxposc].i_length + 8;
1292 return (double)i64 / (double)stream_Size( p_demux->s );
1297 static int Control( demux_t *p_demux, int i_query, va_list args )
1299 demux_sys_t *p_sys = p_demux->p_sys;
1303 vlc_meta_t **pp_meta;
1307 case DEMUX_GET_POSITION:
1308 pf = (double*)va_arg( args, double * );
1309 *pf = ControlGetPosition( p_demux );
1311 case DEMUX_SET_POSITION:
1312 f = (double)va_arg( args, double );
1313 if( p_sys->b_seekable )
1315 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1316 return Seek( p_demux, i64, (int)(f * 100) );
1320 int64_t i_pos = stream_Size( p_demux->s ) * f;
1321 return stream_Seek( p_demux->s, i_pos );
1324 case DEMUX_GET_TIME:
1325 pi64 = (int64_t*)va_arg( args, int64_t * );
1326 *pi64 = p_sys->i_time;
1329 case DEMUX_SET_TIME:
1333 i64 = (int64_t)va_arg( args, int64_t );
1334 if( p_sys->i_length > 0 )
1336 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1338 else if( p_sys->i_time > 0 )
1340 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1341 (double)i64 / (double)p_sys->i_time );
1343 return Seek( p_demux, i64, i_percent );
1345 case DEMUX_GET_LENGTH:
1346 pi64 = (int64_t*)va_arg( args, int64_t * );
1347 *pi64 = p_sys->i_length * (mtime_t)1000000;
1351 pf = (double*)va_arg( args, double * );
1353 for( i = 0; i < (int)p_sys->i_track; i++ )
1355 avi_track_t *tk = p_sys->track[i];
1356 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1358 *pf = (float)tk->i_rate / (float)tk->i_scale;
1363 case DEMUX_GET_META:
1364 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1365 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1369 return VLC_EGENERIC;
1373 /*****************************************************************************
1374 * Function to convert pts to chunk or byte
1375 *****************************************************************************/
1377 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1382 return (mtime_t)((int64_t)i_pts *
1383 (int64_t)tk->i_rate /
1384 (int64_t)tk->i_scale /
1387 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1389 if( !tk->i_scale || !tk->i_samplesize )
1392 return (mtime_t)((int64_t)i_pts *
1393 (int64_t)tk->i_rate /
1394 (int64_t)tk->i_scale /
1396 (int64_t)tk->i_samplesize );
1399 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1406 i_dpts = (mtime_t)( (int64_t)1000000 *
1408 (int64_t)tk->i_scale /
1409 (int64_t)tk->i_rate );
1411 if( tk->i_samplesize )
1413 return i_dpts / tk->i_samplesize;
1418 static mtime_t AVI_GetPTS( avi_track_t *tk )
1420 if( tk->i_samplesize )
1422 int64_t i_count = 0;
1424 /* we need a valid entry we will emulate one */
1425 if( tk->i_idxposc == tk->i_idxnb )
1429 /* use the last entry */
1430 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1431 + tk->p_index[tk->i_idxnb - 1].i_length;
1436 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1438 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1442 if( tk->i_cat == AUDIO_ES )
1444 return AVI_GetDPTS( tk, tk->i_blockno );
1448 return AVI_GetDPTS( tk, tk->i_idxposc );
1453 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1455 demux_sys_t *p_sys = p_demux->p_sys;
1456 avi_packet_t avi_pk;
1457 int i_loop_count = 0;
1459 /* find first chunk of i_stream that isn't in index */
1461 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1463 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1464 if( AVI_PacketNext( p_demux ) )
1466 return VLC_EGENERIC;
1471 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1476 if( p_demux->b_die ) return VLC_EGENERIC;
1478 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1480 msg_Warn( p_demux, "cannot get packet header" );
1481 return VLC_EGENERIC;
1483 if( avi_pk.i_stream >= p_sys->i_track ||
1484 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1486 if( AVI_PacketNext( p_demux ) )
1488 return VLC_EGENERIC;
1491 /* Prevents from eating all the CPU with broken files.
1492 * This value should be low enough so that it doesn't
1493 * affect the reading speed too much. */
1494 if( !(++i_loop_count % 1024) )
1496 if( p_demux->b_die ) return VLC_EGENERIC;
1499 if( !(i_loop_count % (1024 * 10)) )
1500 msg_Warn( p_demux, "don't seem to find any data..." );
1505 /* add this chunk to the index */
1508 index.i_id = avi_pk.i_fourcc;
1510 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1512 index.i_pos = avi_pk.i_pos;
1513 index.i_length = avi_pk.i_size;
1514 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1516 if( avi_pk.i_stream == i_stream )
1521 if( AVI_PacketNext( p_demux ) )
1523 return VLC_EGENERIC;
1529 /* be sure that i_ck will be a valid index entry */
1530 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1533 demux_sys_t *p_sys = p_demux->p_sys;
1534 avi_track_t *p_stream = p_sys->track[i_stream];
1536 p_stream->i_idxposc = i_ck;
1537 p_stream->i_idxposb = 0;
1539 if( i_ck >= p_stream->i_idxnb )
1541 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1544 p_stream->i_idxposc++;
1545 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1547 return VLC_EGENERIC;
1550 } while( p_stream->i_idxposc < i_ck );
1556 /* XXX FIXME up to now, we assume that all chunk are one after one */
1557 static int AVI_StreamBytesSet( demux_t *p_demux,
1558 unsigned int i_stream,
1561 demux_sys_t *p_sys = p_demux->p_sys;
1562 avi_track_t *p_stream = p_sys->track[i_stream];
1564 if( ( p_stream->i_idxnb > 0 )
1565 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1566 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1568 /* index is valid to find the ck */
1569 /* uses dichototmie to be fast enougth */
1570 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1571 int i_idxmax = p_stream->i_idxnb;
1575 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1577 i_idxmax = i_idxposc ;
1578 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1582 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1583 p_stream->p_index[i_idxposc].i_length <= i_byte)
1585 i_idxmin = i_idxposc ;
1586 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1590 p_stream->i_idxposc = i_idxposc;
1591 p_stream->i_idxposb = i_byte -
1592 p_stream->p_index[i_idxposc].i_lengthtotal;
1601 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1602 p_stream->i_idxposb = 0;
1605 p_stream->i_idxposc++;
1606 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1608 return VLC_EGENERIC;
1611 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1612 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1614 p_stream->i_idxposb = i_byte -
1615 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1620 static int AVI_TrackSeek( demux_t *p_demux,
1624 demux_sys_t *p_sys = p_demux->p_sys;
1625 avi_track_t *tk = p_sys->track[i_stream];
1627 #define p_stream p_sys->track[i_stream]
1630 i_oldpts = AVI_GetPTS( p_stream );
1632 if( !p_stream->i_samplesize )
1634 if( AVI_StreamChunkSet( p_demux,
1636 AVI_PTSToChunk( p_stream, i_date ) ) )
1638 return VLC_EGENERIC;
1641 if( p_stream->i_cat == AUDIO_ES )
1645 for( i = 0; i < tk->i_idxposc; i++ )
1647 if( tk->i_blocksize > 0 )
1649 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1659 "old:"I64Fd" %s new "I64Fd,
1661 i_oldpts > i_date ? ">" : "<",
1664 if( p_stream->i_cat == VIDEO_ES )
1666 /* search key frame */
1667 if( i_date < i_oldpts )
1669 while( p_stream->i_idxposc > 0 &&
1670 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1673 if( AVI_StreamChunkSet( p_demux,
1675 p_stream->i_idxposc - 1 ) )
1677 return VLC_EGENERIC;
1683 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1684 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1687 if( AVI_StreamChunkSet( p_demux,
1689 p_stream->i_idxposc + 1 ) )
1691 return VLC_EGENERIC;
1699 if( AVI_StreamBytesSet( p_demux,
1701 AVI_PTSToByte( p_stream, i_date ) ) )
1703 return VLC_EGENERIC;
1710 /****************************************************************************
1711 * Return VLC_TRUE if it's a key frame
1712 ****************************************************************************/
1713 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1719 * startcode: 0x00000100 32bits
1720 * framenumber ? 5bits
1721 * piture type 0(I),1(P) 2bits
1723 if( GetDWBE( p_byte ) != 0x00000100 )
1725 /* it's not an msmpegv1 stream, strange...*/
1726 return AVIIF_KEYFRAME;
1728 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1731 case FOURCC_DIV3: /* wmv1 also */
1733 * picture type 0(I),1(P) 2bits
1735 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1737 /* we should find first occurrence of 0x000001b6 (32bits)
1738 * startcode: 0x000001b6 32bits
1739 * piture type 0(I),1(P) 2bits
1741 if( GetDWBE( p_byte ) != 0x000001b6 )
1743 /* not true , need to find the first VOP header */
1744 return AVIIF_KEYFRAME;
1746 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1749 /* I can't do it, so say yes */
1750 return AVIIF_KEYFRAME;
1754 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1759 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1763 /* XXX DIV1 <- msmpeg4v1, DIV2 <- msmpeg4v2, DIV3 <- msmpeg4v3, mp4v for mpeg4 */
1767 return VLC_FOURCC('m','r','l','e');
1816 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1820 /****************************************************************************
1822 ****************************************************************************/
1823 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1824 int *pi_number, int *pi_type )
1826 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1829 c1 = ((uint8_t *)&i_id)[0];
1830 c2 = ((uint8_t *)&i_id)[1];
1832 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1834 SET_PTR( pi_number, 100 ); /* > max stream number */
1835 SET_PTR( pi_type, UNKNOWN_ES );
1839 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1840 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1843 SET_PTR( pi_type, AUDIO_ES );
1847 SET_PTR( pi_type, VIDEO_ES );
1850 SET_PTR( pi_type, UNKNOWN_ES );
1857 /****************************************************************************
1859 ****************************************************************************/
1860 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1864 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1866 return VLC_EGENERIC;
1868 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1869 p_pk->i_size = GetDWLE( p_peek + 4 );
1870 p_pk->i_pos = stream_Tell( p_demux->s );
1871 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1873 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1874 p_peek[10], p_peek[11] );
1881 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1883 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1887 static int AVI_PacketNext( demux_t *p_demux )
1889 avi_packet_t avi_ck;
1892 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1894 return VLC_EGENERIC;
1897 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1898 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1902 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1903 avi_ck.i_type == AVIFOURCC_AVIX )
1909 i_skip = __EVEN( avi_ck.i_size ) + 8;
1912 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1914 return VLC_EGENERIC;
1919 static int AVI_PacketRead( demux_t *p_demux,
1921 block_t **pp_frame )
1925 i_size = __EVEN( p_pk->i_size + 8 );
1927 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1929 return VLC_EGENERIC;
1931 (*pp_frame)->p_buffer += 8;
1932 (*pp_frame)->i_buffer -= 8;
1934 if( i_size != p_pk->i_size + 8 )
1936 (*pp_frame)->i_buffer--;
1942 static int AVI_PacketSearch( demux_t *p_demux )
1944 demux_sys_t *p_sys = p_demux->p_sys;
1945 avi_packet_t avi_pk;
1950 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
1952 return VLC_EGENERIC;
1954 AVI_PacketGetHeader( p_demux, &avi_pk );
1955 if( avi_pk.i_stream < p_sys->i_track &&
1956 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
1960 switch( avi_pk.i_fourcc )
1962 case AVIFOURCC_JUNK:
1963 case AVIFOURCC_LIST:
1964 case AVIFOURCC_RIFF:
1965 case AVIFOURCC_idx1:
1969 /* Prevents from eating all the CPU with broken files.
1970 * This value should be low enough so that it doesn't affect the
1971 * reading speed too much (not that we care much anyway because
1972 * this code is called only on broken files). */
1973 if( !(++i_count % 1024) )
1975 if( p_demux->b_die ) return VLC_EGENERIC;
1978 if( !(i_count % (1024 * 10)) )
1979 msg_Warn( p_demux, "trying to resync..." );
1984 /****************************************************************************
1986 ****************************************************************************/
1987 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
1989 avi_entry_t *p_index)
1991 avi_track_t *tk = p_sys->track[i_stream];
1993 /* Update i_movi_lastchunk_pos */
1994 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
1996 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2000 if( tk->i_idxnb >= tk->i_idxmax )
2002 tk->i_idxmax += 16384;
2003 tk->p_index = realloc( tk->p_index,
2004 tk->i_idxmax * sizeof( avi_entry_t ) );
2005 if( tk->p_index == NULL )
2010 /* calculate cumulate length */
2011 if( tk->i_idxnb > 0 )
2013 p_index->i_lengthtotal =
2014 tk->p_index[tk->i_idxnb - 1].i_length +
2015 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2019 p_index->i_lengthtotal = 0;
2022 tk->p_index[tk->i_idxnb++] = *p_index;
2025 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2027 demux_sys_t *p_sys = p_demux->p_sys;
2029 avi_chunk_list_t *p_riff;
2030 avi_chunk_list_t *p_movi;
2031 avi_chunk_idx1_t *p_idx1;
2033 unsigned int i_stream;
2034 unsigned int i_index;
2038 vlc_bool_t b_keyset[100];
2040 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2041 p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2042 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2046 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2047 return VLC_EGENERIC;
2050 /* *** calculate offset *** */
2051 /* Well, avi is __SHIT__ so test more than one entry
2052 * (needed for some avi files) */
2054 for( i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2056 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2058 i_offset = p_movi->i_chunk_pos + 8;
2063 /* Reset b_keyset */
2064 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2065 b_keyset[i_stream] = VLC_FALSE;
2067 for( i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2071 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2074 if( i_stream < p_sys->i_track &&
2075 i_cat == p_sys->track[i_stream]->i_cat )
2078 index.i_id = p_idx1->entry[i_index].i_fourcc;
2080 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2081 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2082 index.i_length = p_idx1->entry[i_index].i_length;
2083 AVI_IndexAddEntry( p_sys, i_stream, &index );
2085 if( index.i_flags&AVIIF_KEYFRAME )
2086 b_keyset[i_stream] = VLC_TRUE;
2090 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2092 if( !b_keyset[i_stream] )
2094 avi_track_t *tk = p_sys->track[i_stream];
2096 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2097 for( i_index = 0; i_index < tk->i_idxnb; i_index++ )
2098 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2104 static void __Parse_indx( demux_t *p_demux,
2106 avi_chunk_indx_t *p_indx )
2108 demux_sys_t *p_sys = p_demux->p_sys;
2112 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2113 if( p_indx->i_indexsubtype == 0 )
2115 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2117 index.i_id = p_indx->i_id;
2118 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2119 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2120 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2122 AVI_IndexAddEntry( p_sys, i_stream, &index );
2125 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2127 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2129 index.i_id = p_indx->i_id;
2130 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2131 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2132 index.i_length = p_indx->idx.field[i].i_size;
2134 AVI_IndexAddEntry( p_sys, i_stream, &index );
2139 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2143 static void AVI_IndexLoad_indx( demux_t *p_demux )
2145 demux_sys_t *p_sys = p_demux->p_sys;
2146 unsigned int i_stream;
2149 avi_chunk_list_t *p_riff;
2150 avi_chunk_list_t *p_hdrl;
2152 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2153 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2155 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2157 avi_chunk_list_t *p_strl;
2158 avi_chunk_indx_t *p_indx;
2160 #define p_stream p_sys->track[i_stream]
2161 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2162 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2166 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML file?)" );
2170 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2172 __Parse_indx( p_demux, i_stream, p_indx );
2174 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2177 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2179 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2180 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2184 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2189 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2195 static void AVI_IndexLoad( demux_t *p_demux )
2197 demux_sys_t *p_sys = p_demux->p_sys;
2198 unsigned int i_stream;
2200 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2202 p_sys->track[i_stream]->i_idxnb = 0;
2203 p_sys->track[i_stream]->i_idxmax = 0;
2204 p_sys->track[i_stream]->p_index = NULL;
2209 AVI_IndexLoad_indx( p_demux );
2211 else if( AVI_IndexLoad_idx1( p_demux ) )
2213 /* try indx if idx1 failed as some "normal" file have indx too */
2214 AVI_IndexLoad_indx( p_demux );
2217 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2219 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2220 i_stream, p_sys->track[i_stream]->i_idxnb );
2224 static void AVI_IndexCreate( demux_t *p_demux )
2226 demux_sys_t *p_sys = p_demux->p_sys;
2228 avi_chunk_list_t *p_riff;
2229 avi_chunk_list_t *p_movi;
2231 unsigned int i_stream;
2234 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2235 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2239 msg_Err( p_demux, "cannot find p_movi" );
2243 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2245 p_sys->track[i_stream]->i_idxnb = 0;
2246 p_sys->track[i_stream]->i_idxmax = 0;
2247 p_sys->track[i_stream]->p_index = NULL;
2249 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2250 stream_Size( p_demux->s ) );
2252 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2253 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2258 if( p_demux->b_die )
2263 if( AVI_PacketGetHeader( p_demux, &pk ) )
2267 if( pk.i_stream < p_sys->i_track &&
2268 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2271 index.i_id = pk.i_fourcc;
2273 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2274 index.i_pos = pk.i_pos;
2275 index.i_length = pk.i_size;
2276 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2280 switch( pk.i_fourcc )
2282 case AVIFOURCC_idx1:
2285 avi_chunk_list_t *p_sysx;
2286 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2287 AVIFOURCC_RIFF, 1 );
2289 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2290 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24))
2297 case AVIFOURCC_RIFF:
2298 msg_Dbg( p_demux, "new RIFF chunk found" );
2300 case AVIFOURCC_JUNK:
2303 msg_Warn( p_demux, "need resync, probably broken avi" );
2304 if( AVI_PacketSearch( p_demux ) )
2306 msg_Warn( p_demux, "lost sync, abord index creation" );
2312 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2313 AVI_PacketNext( p_demux ) )
2320 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2323 "stream[%d] creating %d index entries",
2325 p_sys->track[i_stream]->i_idxnb );
2329 /*****************************************************************************
2331 *****************************************************************************/
2332 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2334 demux_sys_t *p_sys = p_demux->p_sys;
2336 int b_end = VLC_TRUE;
2338 for( i = 0; i < p_sys->i_track; i++ )
2340 avi_track_t *tk = p_sys->track[i];
2341 if( tk->i_idxposc >= tk->i_idxnb )
2343 tk->b_activated = VLC_FALSE;
2344 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->p_es, VLC_FALSE );
2354 /****************************************************************************
2355 * AVI_MovieGetLength give max streams length in second
2356 ****************************************************************************/
2357 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2359 demux_sys_t *p_sys = p_demux->p_sys;
2360 mtime_t i_maxlength = 0;
2363 for( i = 0; i < p_sys->i_track; i++ )
2365 avi_track_t *tk = p_sys->track[i];
2368 /* fix length for each stream */
2369 if( tk->i_idxnb < 1 || !tk->p_index )
2374 if( tk->i_samplesize )
2376 i_length = AVI_GetDPTS( tk,
2377 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2378 tk->p_index[tk->i_idxnb-1].i_length );
2382 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2384 i_length /= (mtime_t)1000000; /* in seconds */
2387 "stream[%d] length:"I64Fd" (based on index)",
2390 i_maxlength = __MAX( i_maxlength, i_length );