1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2009 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 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
35 #include <vlc_demux.h>
36 #include <vlc_input.h>
38 #include <vlc_dialog.h>
41 #include <vlc_codecs.h>
42 #include <vlc_charset.h>
43 #include <vlc_memory.h>
47 /*****************************************************************************
49 *****************************************************************************/
51 #define INTERLEAVE_TEXT N_("Force interleaved method" )
52 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
54 #define INDEX_TEXT N_("Force index creation")
55 #define INDEX_LONGTEXT N_( \
56 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
57 "or incomplete (not seekable)." )
59 static int Open ( vlc_object_t * );
60 static void Close( vlc_object_t * );
62 static const int pi_index[] = {0,1,2,3};
64 static const char *const ppsz_indexes[] = { N_("Ask for action"),
67 N_("Fix when necessary")};
70 set_shortname( "AVI" )
71 set_description( N_("AVI demuxer") )
72 set_capability( "demux", 212 )
73 set_category( CAT_INPUT )
74 set_subcategory( SUBCAT_INPUT_DEMUX )
76 add_bool( "avi-interleaved", false,
77 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
78 add_integer( "avi-index", 0,
79 INDEX_TEXT, INDEX_LONGTEXT, false )
80 change_integer_list( pi_index, ppsz_indexes )
82 set_callbacks( Open, Close )
85 /*****************************************************************************
87 *****************************************************************************/
88 static int Control ( demux_t *, int, va_list );
89 static int Seek ( demux_t *, mtime_t, int );
90 static int Demux_Seekable ( demux_t * );
91 static int Demux_UnSeekable( demux_t * );
93 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
95 static char *FromACP( const char *str )
97 return FromCharset(vlc_pgettext("GetACP", "CP1252"), str, strlen(str));
100 #define IGNORE_ES NAV_ES
104 vlc_fourcc_t i_fourcc;
107 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
109 uint8_t i_peek[8]; /* first 8 bytes */
111 unsigned int i_stream;
122 int64_t i_lengthtotal;
130 avi_entry_t *p_entry;
133 static void avi_index_Init( avi_index_t * );
134 static void avi_index_Clean( avi_index_t * );
135 static void avi_index_Append( avi_index_t *, off_t *, avi_entry_t * );
142 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
143 vlc_fourcc_t i_codec;
147 unsigned int i_samplesize;
154 unsigned int i_idxposc; /* numero of chunk */
155 unsigned int i_idxposb; /* byte in the current chunk */
157 /* For VBR audio only */
158 unsigned int i_blockno;
159 unsigned int i_blocksize;
161 /* For muxed streams */
162 stream_t *p_out_muxed;
177 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
179 /* number of streams and information */
180 unsigned int i_track;
186 unsigned int i_attachment;
187 input_attachment_t **attachment;
190 static inline off_t __EVEN( off_t i )
192 return (i & 1) ? i + 1 : i;
195 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
196 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
197 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
198 static mtime_t AVI_GetPTS ( avi_track_t * );
201 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
202 static int AVI_StreamChunkSet ( demux_t *,
203 unsigned int i_stream, unsigned int i_ck );
204 static int AVI_StreamBytesSet ( demux_t *,
205 unsigned int i_stream, off_t i_byte );
207 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
208 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
210 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
211 static int AVI_PacketNext ( demux_t * );
212 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
213 static int AVI_PacketSearch ( demux_t * );
215 static void AVI_IndexLoad ( demux_t * );
216 static void AVI_IndexCreate ( demux_t * );
218 static void AVI_ExtractSubtitle( demux_t *, unsigned int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
220 static mtime_t AVI_MovieGetLength( demux_t * );
222 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
224 /*****************************************************************************
226 *****************************************************************************/
227 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
228 static int AVI_TrackStopFinishedStreams( demux_t *);
231 - For VBR mp3 stream:
232 count blocks by rounded-up chunksizes instead of chunks
233 we need full emulation of dshow avi demuxer bugs :(
234 fixes silly nandub-style a-v delaying in avi with vbr mp3...
235 (from mplayer 2002/08/02)
239 /*****************************************************************************
240 * Open: check file and initializes AVI structures
241 *****************************************************************************/
242 static int Open( vlc_object_t * p_this )
244 demux_t *p_demux = (demux_t *)p_this;
247 bool b_index = false;
250 avi_chunk_list_t *p_riff;
251 avi_chunk_list_t *p_hdrl, *p_movi;
252 avi_chunk_avih_t *p_avih;
254 unsigned int i_track;
255 unsigned int i, i_peeker;
257 const uint8_t *p_peek;
259 /* Is it an avi file ? */
260 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
262 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
264 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
266 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
270 if( i_peeker == 188 )
275 /* Initialize input structures. */
276 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
277 memset( p_sys, 0, sizeof( demux_sys_t ) );
280 p_sys->i_movi_lastchunk_pos = 0;
281 p_sys->b_odml = false;
282 p_sys->b_muxed = false;
286 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
288 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
290 p_demux->pf_control = Control;
291 p_demux->pf_demux = Demux_Seekable;
293 /* For unseekable stream, automatically use Demux_UnSeekable */
294 if( !p_sys->b_seekable
295 || var_InheritBool( p_demux, "avi-interleaved" ) )
297 p_demux->pf_demux = Demux_UnSeekable;
302 stream_Read( p_demux->s, NULL, i_peeker );
305 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
307 msg_Err( p_demux, "avi module discarded (invalid file)" );
312 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
314 unsigned int i_count =
315 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
317 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
318 for( i = 1; i < i_count; i++ )
320 avi_chunk_list_t *p_sysx;
322 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
323 if( p_sysx->i_type == AVIFOURCC_AVIX )
325 msg_Warn( p_demux, "detected OpenDML file" );
326 p_sys->b_odml = true;
332 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
333 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
334 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
336 if( !p_hdrl || !p_movi )
338 msg_Err( p_demux, "avi module discarded (invalid file)" );
342 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
344 msg_Err( p_demux, "cannot find avih chunk" );
347 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
348 if( p_avih->i_streams != i_track )
351 "found %d stream but %d are declared",
352 i_track, p_avih->i_streams );
356 msg_Err( p_demux, "no stream defined!" );
360 /* print information on streams */
361 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
363 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
364 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
365 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
366 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
368 AVI_MetaLoad( p_demux, p_riff, p_avih );
370 /* now read info on each stream and create ES */
371 for( i = 0 ; i < i_track; i++ )
373 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
377 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
378 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
379 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
380 avi_chunk_strf_auds_t *p_auds = NULL;
381 avi_chunk_strf_vids_t *p_vids = NULL;
384 memset( tk, 0, sizeof(*tk) );
386 tk->b_activated = true;
388 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
389 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
391 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
393 msg_Warn( p_demux, "stream[%d] incomplete", i );
398 tk->i_rate = p_strh->i_rate;
399 tk->i_scale = p_strh->i_scale;
400 tk->i_samplesize = p_strh->i_samplesize;
401 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
402 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
404 switch( p_strh->i_type )
406 case( AVIFOURCC_auds ):
407 tk->i_cat = AUDIO_ES;
408 if( p_auds->p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
409 p_auds->p_wf->cbSize >= sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) )
411 WAVEFORMATEXTENSIBLE *p_wfe = (WAVEFORMATEXTENSIBLE *)p_auds->p_wf;
412 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
413 p_wfe->SubFormat.Data1 );
416 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
417 p_auds->p_wf->wFormatTag );
419 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
420 if( tk->i_blocksize == 0 )
422 if( p_auds->p_wf->wFormatTag == 1 )
423 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
427 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
429 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
430 "Using blocksize as a workaround.",
431 i, tk->i_samplesize, tk->i_blocksize );
432 tk->i_samplesize = tk->i_blocksize;
435 if( tk->i_codec == VLC_CODEC_VORBIS )
437 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
440 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
442 fmt.audio.i_channels = p_auds->p_wf->nChannels;
443 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
444 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
445 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
446 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
447 fmt.b_packetized = !tk->i_blocksize;
450 "stream[%d] audio(0x%x - %s) %d channels %dHz %dbits",
451 i, p_auds->p_wf->wFormatTag,vlc_fourcc_GetDescription(AUDIO_ES,tk->i_codec),
452 p_auds->p_wf->nChannels,
453 p_auds->p_wf->nSamplesPerSec,
454 p_auds->p_wf->wBitsPerSample );
456 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
457 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
458 if( fmt.i_extra > 0 )
460 fmt.p_extra = malloc( fmt.i_extra );
461 if( !fmt.p_extra ) goto error;
462 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
466 case( AVIFOURCC_vids ):
467 tk->i_cat = VIDEO_ES;
468 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
469 p_vids->p_bih->biCompression );
470 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
472 msg_Dbg( p_demux, "stream[%d] subtitles", i );
473 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
477 else if( p_vids->p_bih->biCompression == 0x00 )
479 switch( p_vids->p_bih->biBitCount )
482 tk->i_codec = VLC_CODEC_RGB32;
485 tk->i_codec = VLC_CODEC_RGB24;
487 case 16: /* Yes it is RV15 */
489 tk->i_codec = VLC_CODEC_RGB15;
491 case 9: /* <- TODO check that */
492 tk->i_codec = VLC_CODEC_I410;
494 case 8: /* <- TODO check that */
495 tk->i_codec = VLC_CODEC_GREY;
498 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
500 switch( tk->i_codec )
502 case VLC_CODEC_RGB24:
503 case VLC_CODEC_RGB32:
504 fmt.video.i_rmask = 0x00ff0000;
505 fmt.video.i_gmask = 0x0000ff00;
506 fmt.video.i_bmask = 0x000000ff;
508 case VLC_CODEC_RGB15:
509 fmt.video.i_rmask = 0x7c00;
510 fmt.video.i_gmask = 0x03e0;
511 fmt.video.i_bmask = 0x001f;
519 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
520 if( tk->i_codec == VLC_CODEC_MP4V &&
521 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
524 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
527 tk->i_samplesize = 0;
528 fmt.video.i_width = p_vids->p_bih->biWidth;
529 fmt.video.i_height = p_vids->p_bih->biHeight;
530 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
531 fmt.video.i_frame_rate = tk->i_rate;
532 fmt.video.i_frame_rate_base = tk->i_scale;
534 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
535 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
536 if( fmt.i_extra > 0 )
538 fmt.p_extra = malloc( fmt.i_extra );
539 if( !fmt.p_extra ) goto error;
540 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
543 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
544 i, (char*)&p_vids->p_bih->biCompression,
545 (uint32_t)p_vids->p_bih->biWidth,
546 (uint32_t)p_vids->p_bih->biHeight,
547 p_vids->p_bih->biBitCount,
548 (float)tk->i_rate/(float)tk->i_scale );
550 if( p_vids->p_bih->biCompression == 0x00 )
552 /* RGB DIB are coded from bottom to top */
554 (unsigned int)(-(int)p_vids->p_bih->biHeight);
557 /* Extract palette from extradata if bpp <= 8
558 * (assumes that extradata contains only palette but appears
559 * to be true for all palettized codecs we support) */
560 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
562 /* The palette is not always included in biSize */
563 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
564 if( fmt.i_extra > 0 && fmt.p_extra )
566 const uint8_t *p_pal = fmt.p_extra;
568 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
569 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
571 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
573 for( int j = 0; j < 4; j++ )
574 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
580 case( AVIFOURCC_txts):
581 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
582 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
586 case( AVIFOURCC_iavs):
587 case( AVIFOURCC_ivas):
588 p_sys->b_muxed = true;
589 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
590 if( p_strh->i_handler == FOURCC_dvsd ||
591 p_strh->i_handler == FOURCC_dvhd ||
592 p_strh->i_handler == FOURCC_dvsl ||
593 p_strh->i_handler == FOURCC_dv25 ||
594 p_strh->i_handler == FOURCC_dv50 )
596 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
597 if( !tk->p_out_muxed )
598 msg_Err( p_demux, "could not load the DV parser" );
604 case( AVIFOURCC_mids):
605 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
608 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
613 fmt.psz_description = FromACP( p_strn->p_str );
614 if( tk->p_out_muxed == NULL )
615 tk->p_es = es_out_Add( p_demux->out, &fmt );
616 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
619 es_format_Clean( &fmt );
623 if( p_sys->i_track <= 0 )
625 msg_Err( p_demux, "no valid track" );
629 i_do_index = var_InheritInteger( p_demux, "avi-index" );
630 if( i_do_index == 1 ) /* Always fix */
633 if( p_sys->b_seekable )
635 AVI_IndexCreate( p_demux );
639 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
640 AVI_IndexLoad( p_demux );
645 AVI_IndexLoad( p_demux );
648 /* *** movie length in sec *** */
649 p_sys->i_length = AVI_MovieGetLength( p_demux );
651 /* Check the index completeness */
652 unsigned int i_idx_totalframes = 0;
653 for( unsigned int i = 0; i < p_sys->i_track; i++ )
655 const avi_track_t *tk = p_sys->track[i];
656 if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
657 i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
660 if( i_idx_totalframes != p_avih->i_totalframes &&
661 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
662 (mtime_t)p_avih->i_microsecperframe /
665 if( !vlc_object_alive( p_demux) )
668 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
669 "approximative or will exhibit strange behavior" );
670 if( (i_do_index == 0 || i_do_index == 3) && !b_index )
672 if( !p_sys->b_seekable ) {
676 if( i_do_index == 0 )
678 switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
679 _( "Because this AVI file index is broken or missing, "
680 "seeking will not work correctly.\n"
681 "VLC won't repair your file but can temporary fix this "
682 "problem by building an index in memory.\n"
683 "This step might take a long time on a large file.\n"
684 "What do you want to do?" ),
685 _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
689 msg_Dbg( p_demux, "Fixing AVI index" );
693 vlc_object_kill( p_demux->p_parent );
700 msg_Dbg( p_demux, "Fixing AVI index" );
706 /* fix some BeOS MediaKit generated file */
707 for( i = 0 ; i < p_sys->i_track; i++ )
709 avi_track_t *tk = p_sys->track[i];
710 avi_chunk_list_t *p_strl;
711 avi_chunk_strh_t *p_strh;
712 avi_chunk_strf_auds_t *p_auds;
714 if( tk->i_cat != AUDIO_ES )
718 if( tk->idx.i_size < 1 ||
720 tk->i_samplesize != 0 )
724 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
725 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
726 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
728 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
729 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
731 int64_t i_track_length =
732 tk->idx.p_entry[tk->idx.i_size-1].i_length +
733 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
734 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
735 (mtime_t)p_avih->i_microsecperframe;
739 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
742 tk->i_samplesize = 1;
743 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
744 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
748 if( p_sys->b_seekable )
750 /* we have read all chunk so go back to movi */
751 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
753 /* Skip movi header */
754 stream_Read( p_demux->s, NULL, 12 );
756 p_sys->i_movi_begin = p_movi->i_chunk_pos;
760 for( unsigned i = 0; i < p_sys->i_attachment; i++)
761 vlc_input_attachment_Delete(p_sys->attachment[i]);
762 free(p_sys->attachment);
765 vlc_meta_Delete( p_sys->meta );
767 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
769 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
772 /*****************************************************************************
773 * Close: frees unused data
774 *****************************************************************************/
775 static void Close ( vlc_object_t * p_this )
777 demux_t * p_demux = (demux_t *)p_this;
779 demux_sys_t *p_sys = p_demux->p_sys ;
781 for( i = 0; i < p_sys->i_track; i++ )
783 if( p_sys->track[i] )
785 if( p_sys->track[i]->p_out_muxed )
786 stream_Delete( p_sys->track[i]->p_out_muxed );
787 avi_index_Clean( &p_sys->track[i]->idx );
788 free( p_sys->track[i] );
791 free( p_sys->track );
792 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
793 vlc_meta_Delete( p_sys->meta );
794 for( unsigned i = 0; i < p_sys->i_attachment; i++)
795 vlc_input_attachment_Delete(p_sys->attachment[i]);
796 free(p_sys->attachment);
801 /*****************************************************************************
802 * Demux_Seekable: reads and demuxes data packets for stream seekable
803 *****************************************************************************
804 * AVIDemux: reads and demuxes data packets
805 *****************************************************************************
806 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
807 *****************************************************************************/
814 off_t i_posf; /* where we will read :
815 if i_idxposb == 0 : begining of chunk (+8 to acces data)
816 else : point on data directly */
817 } avi_track_toread_t;
819 static int Demux_Seekable( demux_t *p_demux )
821 demux_sys_t *p_sys = p_demux->p_sys;
823 unsigned int i_track_count = 0;
824 unsigned int i_track;
825 /* cannot be more than 100 stream (dcXX or wbXX) */
826 avi_track_toread_t toread[100];
829 /* detect new selected/unselected streams */
830 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
832 avi_track_t *tk = p_sys->track[i_track];
835 if( p_sys->b_muxed && tk->p_out_muxed )
838 tk->b_activated = true;
842 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
843 if( b && !tk->b_activated )
845 if( p_sys->b_seekable)
847 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
849 tk->b_activated = true;
851 else if( !b && tk->b_activated )
853 tk->b_activated = false;
861 if( i_track_count <= 0 )
863 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
865 p_sys->i_time += 25*1000; /* read 25ms */
868 if( p_sys->i_time >= i_length )
872 msg_Warn( p_demux, "no track selected, exiting..." );
876 /* wait for the good time */
877 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
878 p_sys->i_time += 25*1000; /* read 25ms */
881 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
883 avi_track_t *tk = p_sys->track[i_track];
886 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
887 if( tk->i_idxposc < tk->idx.i_size )
889 toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
890 if( tk->i_idxposb > 0 )
892 toread[i_track].i_posf += 8 + tk->i_idxposb;
897 toread[i_track].i_posf = -1;
900 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
902 if( tk->i_samplesize )
904 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
908 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
913 toread[i_track].i_toread *= -1;
926 /* search for first chunk to be read */
927 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
929 if( !toread[i].b_ok ||
930 AVI_GetDPTS( p_sys->track[i],
931 toread[i].i_toread ) <= -25 * 1000 )
936 if( toread[i].i_toread > 0 )
938 b_done = false; /* not yet finished */
940 if( toread[i].i_posf > 0 )
942 if( i_pos == -1 || i_pos > toread[i].i_posf )
945 i_pos = toread[i].i_posf;
952 for( i = 0; i < p_sys->i_track; i++ )
957 msg_Warn( p_demux, "all tracks have failed, exiting..." );
963 int i_loop_count = 0;
965 /* no valid index, we will parse directly the stream
966 * in case we fail we will disable all finished stream */
967 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
969 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
970 if( AVI_PacketNext( p_demux ) )
972 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
977 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
984 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
987 "cannot get packet header, track disabled" );
988 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
990 if( avi_pk.i_stream >= p_sys->i_track ||
991 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
993 if( AVI_PacketNext( p_demux ) )
996 "cannot skip packet, track disabled" );
997 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1000 /* Prevents from eating all the CPU with broken files.
1001 * This value should be low enough so that it doesn't
1002 * affect the reading speed too much. */
1003 if( !(++i_loop_count % 1024) )
1005 if( !vlc_object_alive (p_demux) ) return -1;
1008 if( !(i_loop_count % (1024 * 10)) )
1010 "don't seem to find any data..." );
1016 i_track = avi_pk.i_stream;
1017 tk = p_sys->track[i_track];
1019 /* add this chunk to the index */
1021 index.i_id = avi_pk.i_fourcc;
1022 index.i_flags = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
1023 index.i_pos = avi_pk.i_pos;
1024 index.i_length = avi_pk.i_size;
1025 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1027 /* do we will read this data ? */
1028 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1034 if( AVI_PacketNext( p_demux ) )
1037 "cannot skip packet, track disabled" );
1038 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1047 stream_Seek( p_demux->s, i_pos );
1050 /* Set the track to use */
1051 tk = p_sys->track[i_track];
1053 /* read thoses data */
1054 if( tk->i_samplesize )
1056 unsigned int i_toread;
1058 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1060 if( tk->i_samplesize > 1 )
1062 i_toread = tk->i_samplesize;
1066 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1067 i_toread = __MAX( i_toread, 100 );
1070 i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1076 i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1079 if( tk->i_idxposb == 0 )
1081 i_size += 8; /* need to read and skip header */
1084 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1086 msg_Warn( p_demux, "failed reading data" );
1088 toread[i_track].b_ok = false;
1091 if( i_size % 2 ) /* read was padded on word boundary */
1093 p_frame->i_buffer--;
1096 if( tk->i_idxposb == 0 )
1098 p_frame->p_buffer += 8;
1099 p_frame->i_buffer -= 8;
1101 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1102 if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1104 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1108 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1112 if( tk->i_samplesize )
1114 if( tk->i_idxposb == 0 )
1118 toread[i_track].i_toread -= i_size;
1119 tk->i_idxposb += i_size;
1120 if( tk->i_idxposb >=
1121 tk->idx.p_entry[tk->i_idxposc].i_length )
1129 int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1132 if( tk->i_cat == AUDIO_ES )
1134 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1136 toread[i_track].i_toread--;
1139 if( tk->i_idxposc < tk->idx.i_size)
1141 toread[i_track].i_posf =
1142 tk->idx.p_entry[tk->i_idxposc].i_pos;
1143 if( tk->i_idxposb > 0 )
1145 toread[i_track].i_posf += 8 + tk->i_idxposb;
1151 toread[i_track].i_posf = -1;
1154 if( tk->i_cat != VIDEO_ES )
1155 p_frame->i_dts = p_frame->i_pts;
1158 p_frame->i_dts = p_frame->i_pts;
1159 p_frame->i_pts = VLC_TS_INVALID;
1162 //p_pes->i_rate = p_demux->stream.control.i_rate;
1163 if( tk->p_out_muxed )
1164 stream_DemuxSend( tk->p_out_muxed, p_frame );
1166 es_out_Send( p_demux->out, tk->p_es, p_frame );
1171 /*****************************************************************************
1172 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1173 *****************************************************************************
1174 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1175 *****************************************************************************/
1176 static int Demux_UnSeekable( demux_t *p_demux )
1178 demux_sys_t *p_sys = p_demux->p_sys;
1179 avi_track_t *p_stream_master = NULL;
1180 unsigned int i_stream;
1181 unsigned int i_packet;
1183 if( p_sys->b_muxed )
1185 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1186 return VLC_EGENERIC;
1189 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1191 /* *** find master stream for data packet skipping algo *** */
1192 /* *** -> first video, if any, or first audio ES *** */
1193 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1195 avi_track_t *tk = p_sys->track[i_stream];
1198 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1200 if( b && tk->i_cat == VIDEO_ES )
1202 p_stream_master = tk;
1206 p_stream_master = tk;
1210 if( !p_stream_master )
1212 msg_Warn( p_demux, "no more stream selected" );
1216 p_sys->i_time = AVI_GetPTS( p_stream_master );
1218 for( i_packet = 0; i_packet < 10; i_packet++)
1220 #define p_stream p_sys->track[avi_pk.i_stream]
1222 avi_packet_t avi_pk;
1224 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1229 if( avi_pk.i_stream >= p_sys->i_track ||
1230 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1232 /* we haven't found an audio or video packet:
1233 * - we have seek, found first next packet
1234 * - others packets could be found, skip them
1236 switch( avi_pk.i_fourcc )
1238 case AVIFOURCC_JUNK:
1239 case AVIFOURCC_LIST:
1240 case AVIFOURCC_RIFF:
1241 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1242 case AVIFOURCC_idx1:
1245 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1247 return( 0 ); /* eof */
1250 "seems to have lost position, resync" );
1251 if( AVI_PacketSearch( p_demux ) )
1253 msg_Err( p_demux, "resync failed" );
1260 /* check for time */
1261 if( __ABS( AVI_GetPTS( p_stream ) -
1262 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1264 /* load it and send to decoder */
1266 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1270 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1272 if( avi_pk.i_cat != VIDEO_ES )
1273 p_frame->i_dts = p_frame->i_pts;
1276 p_frame->i_dts = p_frame->i_pts;
1277 p_frame->i_pts = VLC_TS_INVALID;
1280 //p_pes->i_rate = p_demux->stream.control.i_rate;
1281 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1285 if( AVI_PacketNext( p_demux ) )
1291 /* *** update stream time position *** */
1292 if( p_stream->i_samplesize )
1294 p_stream->i_idxposb += avi_pk.i_size;
1298 if( p_stream->i_cat == AUDIO_ES )
1300 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1302 p_stream->i_idxposc++;
1312 /*****************************************************************************
1313 * Seek: goto to i_date or i_percent
1314 *****************************************************************************/
1315 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1318 demux_sys_t *p_sys = p_demux->p_sys;
1319 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1320 i_date / 1000000, i_percent );
1322 if( p_sys->b_seekable )
1326 if( !p_sys->i_length )
1328 avi_track_t *p_stream = NULL;
1331 /* use i_percent to create a true i_date */
1332 msg_Warn( p_demux, "seeking without index at %d%%"
1333 " only works for interleaved files", i_percent );
1334 if( i_percent >= 100 )
1336 msg_Warn( p_demux, "cannot seek so far !" );
1337 return VLC_EGENERIC;
1339 i_percent = __MAX( i_percent, 0 );
1341 /* try to find chunk that is at i_percent or the file */
1342 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1343 p_sys->i_movi_begin );
1344 /* search first selected stream (and prefer non-EOF ones) */
1345 for( unsigned i = 0; i < p_sys->i_track; i++ )
1347 avi_track_t *p_track = p_sys->track[i];
1348 if( !p_track->b_activated )
1353 if( !p_track->b_eof )
1356 if( p_stream == NULL )
1358 msg_Warn( p_demux, "cannot find any selected stream" );
1359 return VLC_EGENERIC;
1362 /* be sure that the index exist */
1363 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1365 msg_Warn( p_demux, "cannot seek" );
1366 return VLC_EGENERIC;
1369 while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1370 p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1372 /* search after i_idxposc */
1373 if( AVI_StreamChunkSet( p_demux,
1374 i_stream, p_stream->i_idxposc + 1 ) )
1376 msg_Warn( p_demux, "cannot seek" );
1377 return VLC_EGENERIC;
1381 i_date = AVI_GetPTS( p_stream );
1382 /* TODO better support for i_samplesize != 0 */
1383 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1387 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1389 avi_track_t *p_stream = p_sys->track[i_stream];
1391 if( !p_stream->b_activated )
1394 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1396 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1397 p_sys->i_time = i_date;
1398 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1403 msg_Err( p_demux, "shouldn't yet be executed" );
1404 return VLC_EGENERIC;
1408 /*****************************************************************************
1410 *****************************************************************************/
1411 static double ControlGetPosition( demux_t *p_demux )
1413 demux_sys_t *p_sys = p_demux->p_sys;
1415 if( p_sys->i_length > 0 )
1417 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1419 else if( stream_Size( p_demux->s ) > 0 )
1425 /* search the more advanced selected es */
1426 for( i = 0; i < p_sys->i_track; i++ )
1428 avi_track_t *tk = p_sys->track[i];
1429 if( tk->b_activated && tk->i_idxposc < tk->idx.i_size )
1431 i_tmp = tk->idx.p_entry[tk->i_idxposc].i_pos +
1432 tk->idx.p_entry[tk->i_idxposc].i_length + 8;
1439 return (double)i64 / stream_Size( p_demux->s );
1444 static int Control( demux_t *p_demux, int i_query, va_list args )
1446 demux_sys_t *p_sys = p_demux->p_sys;
1454 case DEMUX_GET_POSITION:
1455 pf = (double*)va_arg( args, double * );
1456 *pf = ControlGetPosition( p_demux );
1458 case DEMUX_SET_POSITION:
1459 f = (double)va_arg( args, double );
1460 if( p_sys->b_seekable )
1462 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1463 return Seek( p_demux, i64, (int)(f * 100) );
1467 int64_t i_pos = stream_Size( p_demux->s ) * f;
1468 return stream_Seek( p_demux->s, i_pos );
1471 case DEMUX_GET_TIME:
1472 pi64 = (int64_t*)va_arg( args, int64_t * );
1473 *pi64 = p_sys->i_time;
1476 case DEMUX_SET_TIME:
1480 i64 = (int64_t)va_arg( args, int64_t );
1481 if( p_sys->i_length > 0 )
1483 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1485 else if( p_sys->i_time > 0 )
1487 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1488 (double)i64 / (double)p_sys->i_time );
1490 return Seek( p_demux, i64, i_percent );
1492 case DEMUX_GET_LENGTH:
1493 pi64 = (int64_t*)va_arg( args, int64_t * );
1494 *pi64 = p_sys->i_length * (mtime_t)1000000;
1498 pf = (double*)va_arg( args, double * );
1500 for( i = 0; i < (int)p_sys->i_track; i++ )
1502 avi_track_t *tk = p_sys->track[i];
1503 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1505 *pf = (float)tk->i_rate / (float)tk->i_scale;
1511 case DEMUX_GET_META:
1512 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1513 vlc_meta_Merge( p_meta, p_sys->meta );
1516 case DEMUX_GET_ATTACHMENTS:
1518 if( p_sys->i_attachment <= 0 )
1519 return VLC_EGENERIC;
1521 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1522 int *pi_int = va_arg( args, int * );
1524 *pi_int = p_sys->i_attachment;
1525 *ppp_attach = calloc( p_sys->i_attachment, sizeof(*ppp_attach));
1526 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1527 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1532 return VLC_EGENERIC;
1536 /*****************************************************************************
1537 * Function to convert pts to chunk or byte
1538 *****************************************************************************/
1540 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1545 return (mtime_t)((int64_t)i_pts *
1546 (int64_t)tk->i_rate /
1547 (int64_t)tk->i_scale /
1550 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1552 if( !tk->i_scale || !tk->i_samplesize )
1555 return (mtime_t)((int64_t)i_pts *
1556 (int64_t)tk->i_rate /
1557 (int64_t)tk->i_scale /
1559 (int64_t)tk->i_samplesize );
1562 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1569 i_dpts = (mtime_t)( (int64_t)1000000 *
1571 (int64_t)tk->i_scale /
1572 (int64_t)tk->i_rate );
1574 if( tk->i_samplesize )
1576 return i_dpts / tk->i_samplesize;
1581 static mtime_t AVI_GetPTS( avi_track_t *tk )
1583 if( tk->i_samplesize )
1585 int64_t i_count = 0;
1587 /* we need a valid entry we will emulate one */
1588 if( tk->i_idxposc == tk->idx.i_size )
1592 /* use the last entry */
1593 i_count = tk->idx.p_entry[tk->idx.i_size - 1].i_lengthtotal
1594 + tk->idx.p_entry[tk->idx.i_size - 1].i_length;
1599 i_count = tk->idx.p_entry[tk->i_idxposc].i_lengthtotal;
1601 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1605 if( tk->i_cat == AUDIO_ES )
1607 return AVI_GetDPTS( tk, tk->i_blockno );
1611 return AVI_GetDPTS( tk, tk->i_idxposc );
1616 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1618 demux_sys_t *p_sys = p_demux->p_sys;
1619 avi_packet_t avi_pk;
1620 int i_loop_count = 0;
1622 /* find first chunk of i_stream that isn't in index */
1624 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1626 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1627 if( AVI_PacketNext( p_demux ) )
1629 return VLC_EGENERIC;
1634 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1639 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1641 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1643 msg_Warn( p_demux, "cannot get packet header" );
1644 return VLC_EGENERIC;
1646 if( avi_pk.i_stream >= p_sys->i_track ||
1647 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1649 if( AVI_PacketNext( p_demux ) )
1651 return VLC_EGENERIC;
1654 /* Prevents from eating all the CPU with broken files.
1655 * This value should be low enough so that it doesn't
1656 * affect the reading speed too much. */
1657 if( !(++i_loop_count % 1024) )
1659 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1662 if( !(i_loop_count % (1024 * 10)) )
1663 msg_Warn( p_demux, "don't seem to find any data..." );
1668 avi_track_t *tk_pk = p_sys->track[avi_pk.i_stream];
1670 /* add this chunk to the index */
1672 index.i_id = avi_pk.i_fourcc;
1673 index.i_flags = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
1674 index.i_pos = avi_pk.i_pos;
1675 index.i_length = avi_pk.i_size;
1676 avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1678 if( avi_pk.i_stream == i_stream )
1683 if( AVI_PacketNext( p_demux ) )
1685 return VLC_EGENERIC;
1691 /* be sure that i_ck will be a valid index entry */
1692 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1695 demux_sys_t *p_sys = p_demux->p_sys;
1696 avi_track_t *p_stream = p_sys->track[i_stream];
1698 p_stream->i_idxposc = i_ck;
1699 p_stream->i_idxposb = 0;
1701 if( i_ck >= p_stream->idx.i_size )
1703 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1706 p_stream->i_idxposc++;
1707 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1709 return VLC_EGENERIC;
1712 } while( p_stream->i_idxposc < i_ck );
1718 /* XXX FIXME up to now, we assume that all chunk are one after one */
1719 static int AVI_StreamBytesSet( demux_t *p_demux,
1720 unsigned int i_stream,
1723 demux_sys_t *p_sys = p_demux->p_sys;
1724 avi_track_t *p_stream = p_sys->track[i_stream];
1726 if( ( p_stream->idx.i_size > 0 )
1727 &&( i_byte < p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_lengthtotal +
1728 p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_length ) )
1730 /* index is valid to find the ck */
1731 /* uses dichototmie to be fast enougth */
1732 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->idx.i_size - 1 );
1733 int i_idxmax = p_stream->idx.i_size;
1737 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal > i_byte )
1739 i_idxmax = i_idxposc ;
1740 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1744 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal +
1745 p_stream->idx.p_entry[i_idxposc].i_length <= i_byte)
1747 i_idxmin = i_idxposc ;
1748 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1752 p_stream->i_idxposc = i_idxposc;
1753 p_stream->i_idxposb = i_byte -
1754 p_stream->idx.p_entry[i_idxposc].i_lengthtotal;
1763 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1764 p_stream->i_idxposb = 0;
1767 p_stream->i_idxposc++;
1768 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1770 return VLC_EGENERIC;
1773 } while( p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal +
1774 p_stream->idx.p_entry[p_stream->i_idxposc].i_length <= i_byte );
1776 p_stream->i_idxposb = i_byte -
1777 p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal;
1782 static int AVI_TrackSeek( demux_t *p_demux,
1786 demux_sys_t *p_sys = p_demux->p_sys;
1787 avi_track_t *tk = p_sys->track[i_stream];
1789 #define p_stream p_sys->track[i_stream]
1792 i_oldpts = AVI_GetPTS( p_stream );
1794 if( !p_stream->i_samplesize )
1796 if( AVI_StreamChunkSet( p_demux,
1798 AVI_PTSToChunk( p_stream, i_date ) ) )
1800 return VLC_EGENERIC;
1803 if( p_stream->i_cat == AUDIO_ES )
1807 for( i = 0; i < tk->i_idxposc; i++ )
1809 if( tk->i_blocksize > 0 )
1811 tk->i_blockno += ( tk->idx.p_entry[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1821 "old:%"PRId64" %s new %"PRId64,
1823 i_oldpts > i_date ? ">" : "<",
1826 if( p_stream->i_cat == VIDEO_ES )
1828 /* search key frame */
1829 //if( i_date < i_oldpts || 1 )
1831 while( p_stream->i_idxposc > 0 &&
1832 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1835 if( AVI_StreamChunkSet( p_demux,
1837 p_stream->i_idxposc - 1 ) )
1839 return VLC_EGENERIC;
1846 while( p_stream->i_idxposc < p_stream->idx.i_size &&
1847 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1850 if( AVI_StreamChunkSet( p_demux,
1852 p_stream->i_idxposc + 1 ) )
1854 return VLC_EGENERIC;
1863 if( AVI_StreamBytesSet( p_demux,
1865 AVI_PTSToByte( p_stream, i_date ) ) )
1867 return VLC_EGENERIC;
1874 /****************************************************************************
1875 * Return true if it's a key frame
1876 ****************************************************************************/
1877 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1881 case VLC_CODEC_DIV1:
1883 * startcode: 0x00000100 32bits
1884 * framenumber ? 5bits
1885 * piture type 0(I),1(P) 2bits
1887 if( GetDWBE( p_byte ) != 0x00000100 )
1889 /* it's not an msmpegv1 stream, strange...*/
1890 return AVIIF_KEYFRAME;
1892 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1894 case VLC_CODEC_DIV2:
1895 case VLC_CODEC_DIV3:
1896 case VLC_CODEC_WMV1:
1898 * picture type 0(I),1(P) 2bits
1900 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1901 case VLC_CODEC_MP4V:
1902 /* we should find first occurrence of 0x000001b6 (32bits)
1903 * startcode: 0x000001b6 32bits
1904 * piture type 0(I),1(P) 2bits
1906 if( GetDWBE( p_byte ) != 0x000001b6 )
1908 /* not true , need to find the first VOP header */
1909 return AVIIF_KEYFRAME;
1911 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1914 /* I can't do it, so say yes */
1915 return AVIIF_KEYFRAME;
1919 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1924 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1927 return vlc_fourcc_GetCodec( i_cat, i_codec );
1929 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1933 /****************************************************************************
1935 ****************************************************************************/
1936 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1937 unsigned int *pi_number, unsigned int *pi_type )
1939 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1942 c1 = ((uint8_t *)&i_id)[0];
1943 c2 = ((uint8_t *)&i_id)[1];
1945 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1947 SET_PTR( pi_number, 100 ); /* > max stream number */
1948 SET_PTR( pi_type, UNKNOWN_ES );
1952 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1953 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1956 SET_PTR( pi_type, AUDIO_ES );
1961 SET_PTR( pi_type, VIDEO_ES );
1965 SET_PTR( pi_type, SPU_ES );
1968 SET_PTR( pi_type, IGNORE_ES );
1971 SET_PTR( pi_type, UNKNOWN_ES );
1978 /****************************************************************************
1980 ****************************************************************************/
1981 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1983 const uint8_t *p_peek;
1985 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1987 return VLC_EGENERIC;
1989 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1990 p_pk->i_size = GetDWLE( p_peek + 4 );
1991 p_pk->i_pos = stream_Tell( p_demux->s );
1992 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1994 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1995 p_peek[10], p_peek[11] );
2002 memcpy( p_pk->i_peek, p_peek + 8, 8 );
2004 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
2008 static int AVI_PacketNext( demux_t *p_demux )
2010 avi_packet_t avi_ck;
2013 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
2015 return VLC_EGENERIC;
2018 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
2019 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
2023 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
2024 avi_ck.i_type == AVIFOURCC_AVIX )
2030 i_skip = __EVEN( avi_ck.i_size ) + 8;
2033 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2035 return VLC_EGENERIC;
2040 static int AVI_PacketRead( demux_t *p_demux,
2042 block_t **pp_frame )
2046 i_size = __EVEN( p_pk->i_size + 8 );
2048 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2050 return VLC_EGENERIC;
2052 (*pp_frame)->p_buffer += 8;
2053 (*pp_frame)->i_buffer -= 8;
2055 if( i_size != p_pk->i_size + 8 )
2057 (*pp_frame)->i_buffer--;
2063 static int AVI_PacketSearch( demux_t *p_demux )
2065 demux_sys_t *p_sys = p_demux->p_sys;
2066 avi_packet_t avi_pk;
2071 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2073 return VLC_EGENERIC;
2075 AVI_PacketGetHeader( p_demux, &avi_pk );
2076 if( avi_pk.i_stream < p_sys->i_track &&
2077 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2081 switch( avi_pk.i_fourcc )
2083 case AVIFOURCC_JUNK:
2084 case AVIFOURCC_LIST:
2085 case AVIFOURCC_RIFF:
2086 case AVIFOURCC_idx1:
2090 /* Prevents from eating all the CPU with broken files.
2091 * This value should be low enough so that it doesn't affect the
2092 * reading speed too much (not that we care much anyway because
2093 * this code is called only on broken files). */
2094 if( !(++i_count % 1024) )
2096 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2099 if( !(i_count % (1024 * 10)) )
2100 msg_Warn( p_demux, "trying to resync..." );
2105 /****************************************************************************
2107 ****************************************************************************/
2108 static void avi_index_Init( avi_index_t *p_index )
2110 p_index->i_size = 0;
2112 p_index->p_entry = NULL;
2114 static void avi_index_Clean( avi_index_t *p_index )
2116 free( p_index->p_entry );
2118 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2119 avi_entry_t *p_entry )
2121 /* Update last chunk position */
2122 if( *pi_last_pos < p_entry->i_pos )
2123 *pi_last_pos = p_entry->i_pos;
2126 if( p_index->i_size >= p_index->i_max )
2128 p_index->i_max += 16384;
2129 p_index->p_entry = realloc_or_free( p_index->p_entry,
2130 p_index->i_max * sizeof( *p_index->p_entry ) );
2131 if( !p_index->p_entry )
2134 /* calculate cumulate length */
2135 if( p_index->i_size > 0 )
2137 p_entry->i_lengthtotal =
2138 p_index->p_entry[p_index->i_size - 1].i_length +
2139 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2143 p_entry->i_lengthtotal = 0;
2146 p_index->p_entry[p_index->i_size++] = *p_entry;
2149 static int AVI_IndexFind_idx1( demux_t *p_demux,
2150 avi_chunk_idx1_t **pp_idx1,
2151 uint64_t *pi_offset )
2153 demux_sys_t *p_sys = p_demux->p_sys;
2155 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2156 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2160 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2161 return VLC_EGENERIC;
2165 /* The offset in the index should be from the start of the movi content,
2166 * but some broken files use offset from the start of the file. Just
2167 * checking the offset of the first packet is not enough as some files
2168 * has unused chunk at the beginning of the movi content.
2170 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2171 uint64_t i_first_pos = UINT64_MAX;
2172 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 100 ); i++ )
2173 i_first_pos = __MIN( i_first_pos, p_idx1->entry[i].i_pos );
2175 const uint64_t i_movi_content = p_movi->i_chunk_pos + 8;
2176 if( i_first_pos < i_movi_content )
2178 *pi_offset = i_movi_content;
2180 else if( p_sys->b_seekable && i_first_pos < UINT64_MAX )
2182 const uint8_t *p_peek;
2183 if( !stream_Seek( p_demux->s, i_movi_content + i_first_pos ) &&
2184 stream_Peek( p_demux->s, &p_peek, 4 ) >= 4 &&
2185 ( !isdigit( p_peek[0] ) || !isdigit( p_peek[1] ) ||
2186 !isalpha( p_peek[2] ) || !isalpha( p_peek[3] ) ) )
2189 *pi_offset = i_movi_content;
2198 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2199 avi_index_t p_index[], off_t *pi_last_offset )
2201 demux_sys_t *p_sys = p_demux->p_sys;
2203 avi_chunk_idx1_t *p_idx1;
2205 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2206 return VLC_EGENERIC;
2208 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2213 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2216 if( i_stream < p_sys->i_track &&
2217 (i_cat == p_sys->track[i_stream]->i_cat || i_cat == UNKNOWN_ES ) )
2220 index.i_id = p_idx1->entry[i_index].i_fourcc;
2221 index.i_flags = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2222 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2223 index.i_length = p_idx1->entry[i_index].i_length;
2225 avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2231 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2232 avi_chunk_indx_t *p_indx )
2236 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2237 if( p_indx->i_indexsubtype == 0 )
2239 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2241 index.i_id = p_indx->i_id;
2242 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2243 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2244 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2246 avi_index_Append( p_index, pi_max_offset, &index );
2249 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2251 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2253 index.i_id = p_indx->i_id;
2254 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2255 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2256 index.i_length = p_indx->idx.field[i].i_size;
2258 avi_index_Append( p_index, pi_max_offset, &index );
2263 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2267 static void AVI_IndexLoad_indx( demux_t *p_demux,
2268 avi_index_t p_index[], off_t *pi_last_offset )
2270 demux_sys_t *p_sys = p_demux->p_sys;
2272 avi_chunk_list_t *p_riff;
2273 avi_chunk_list_t *p_hdrl;
2275 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2276 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2278 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2280 avi_chunk_list_t *p_strl;
2281 avi_chunk_indx_t *p_indx;
2283 #define p_stream p_sys->track[i_stream]
2284 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2285 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2290 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2295 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2297 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2299 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2302 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2304 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2305 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2309 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2310 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2311 AVI_ChunkFree( p_demux->s, &ck_sub );
2316 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2322 static void AVI_IndexLoad( demux_t *p_demux )
2324 demux_sys_t *p_sys = p_demux->p_sys;
2327 assert( p_sys->i_track <= 100 );
2328 avi_index_t p_idx_indx[p_sys->i_track];
2329 avi_index_t p_idx_idx1[p_sys->i_track];
2330 for( unsigned i = 0; i < p_sys->i_track; i++ )
2332 avi_index_Init( &p_idx_indx[i] );
2333 avi_index_Init( &p_idx_idx1[i] );
2335 off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2336 off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2338 AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2339 if( !p_sys->b_odml )
2340 AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2342 /* Select the longest index */
2343 for( unsigned i = 0; i < p_sys->i_track; i++ )
2345 if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2347 msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2348 p_sys->track[i]->idx = p_idx_indx[i];
2349 avi_index_Clean( &p_idx_idx1[i] );
2353 msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2354 p_sys->track[i]->idx = p_idx_idx1[i];
2355 avi_index_Clean( &p_idx_indx[i] );
2358 p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2360 for( unsigned i = 0; i < p_sys->i_track; i++ )
2362 avi_index_t *p_index = &p_sys->track[i]->idx;
2366 for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2367 b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2370 msg_Err( p_demux, "no key frame set for track %u", i );
2371 for( unsigned j = 0; j < p_index->i_size; j++ )
2372 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2376 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2377 i, p_index->i_size );
2381 static void AVI_IndexCreate( demux_t *p_demux )
2383 demux_sys_t *p_sys = p_demux->p_sys;
2385 avi_chunk_list_t *p_riff;
2386 avi_chunk_list_t *p_movi;
2388 unsigned int i_stream;
2391 mtime_t i_dialog_update;
2392 dialog_progress_bar_t *p_dialog = NULL;
2394 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2395 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2399 msg_Err( p_demux, "cannot find p_movi" );
2403 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2404 avi_index_Init( &p_sys->track[i_stream]->idx );
2406 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2407 stream_Size( p_demux->s ) );
2409 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2410 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2413 /* Only show dialog if AVI is > 10MB */
2414 i_dialog_update = mdate();
2415 if( stream_Size( p_demux->s ) > 10000000 )
2416 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2417 NULL, _("Cancel") );
2423 if( !vlc_object_alive (p_demux) )
2426 /* Don't update/check dialog too often */
2427 if( p_dialog && mdate() - i_dialog_update > 100000 )
2429 if( dialog_ProgressCancelled( p_dialog ) )
2432 double f_current = stream_Tell( p_demux->s );
2433 double f_size = stream_Size( p_demux->s );
2434 double f_pos = f_current / f_size;
2435 dialog_ProgressSet( p_dialog, NULL, f_pos );
2437 i_dialog_update = mdate();
2440 if( AVI_PacketGetHeader( p_demux, &pk ) )
2443 if( pk.i_stream < p_sys->i_track &&
2444 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2446 avi_track_t *tk = p_sys->track[pk.i_stream];
2449 index.i_id = pk.i_fourcc;
2450 index.i_flags = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2451 index.i_pos = pk.i_pos;
2452 index.i_length = pk.i_size;
2453 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2457 switch( pk.i_fourcc )
2459 case AVIFOURCC_idx1:
2462 avi_chunk_list_t *p_sysx;
2463 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2464 AVIFOURCC_RIFF, 1 );
2466 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2467 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2473 case AVIFOURCC_RIFF:
2474 msg_Dbg( p_demux, "new RIFF chunk found" );
2478 case AVIFOURCC_JUNK:
2482 msg_Warn( p_demux, "need resync, probably broken avi" );
2483 if( AVI_PacketSearch( p_demux ) )
2485 msg_Warn( p_demux, "lost sync, abord index creation" );
2491 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2492 AVI_PacketNext( p_demux ) )
2499 if( p_dialog != NULL )
2500 dialog_ProgressDestroy( p_dialog );
2502 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2504 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2505 i_stream, p_sys->track[i_stream]->idx.i_size );
2510 static void AVI_MetaLoad( demux_t *p_demux,
2511 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2513 demux_sys_t *p_sys = p_demux->p_sys;
2515 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2520 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2521 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2522 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2523 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2524 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2525 vlc_meta_SetSetting( p_meta, buffer );
2527 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2531 static const struct {
2535 { AVIFOURCC_IART, vlc_meta_Artist },
2536 { AVIFOURCC_ICMT, vlc_meta_Description },
2537 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2538 { AVIFOURCC_IGNR, vlc_meta_Genre },
2539 { AVIFOURCC_INAM, vlc_meta_Title },
2542 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2544 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2547 char *psz_value = FromACP( p_strz->p_str );
2552 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2557 /*****************************************************************************
2559 *****************************************************************************/
2560 static void AVI_ExtractSubtitle( demux_t *p_demux,
2561 unsigned int i_stream,
2562 avi_chunk_list_t *p_strl,
2563 avi_chunk_STRING_t *p_strn )
2565 demux_sys_t *p_sys = p_demux->p_sys;
2566 block_t *p_block = NULL;
2567 input_attachment_t *p_attachment = NULL;
2568 char *psz_description = NULL;
2569 avi_chunk_indx_t *p_indx = NULL;
2571 if( !p_sys->b_seekable )
2574 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2580 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2581 p_indx->i_entriesinuse > 0 )
2583 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2584 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2589 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2590 p_indx->i_entriesinuse != 1 ||
2591 p_indx->i_indexsubtype != 0 )
2594 i_position = p_indx->i_baseoffset +
2595 p_indx->idx.std[0].i_offset - 8;
2596 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2600 avi_chunk_idx1_t *p_idx1;
2603 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2607 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2609 const idx1_entry_t *e = &p_idx1->entry[i];
2611 unsigned i_stream_idx;
2613 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2614 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2616 i_position = e->i_pos + i_offset;
2617 i_size = e->i_length + 8;
2626 if( i_size > 1000000 )
2629 if( stream_Seek( p_demux->s, i_position ) )
2631 p_block = stream_Block( p_demux->s, i_size );
2635 /* Parse packet header */
2636 const uint8_t *p = p_block->p_buffer;
2637 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2642 /* Parse subtitle chunk header */
2643 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2644 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2646 const unsigned i_name = GetDWLE( &p[7] );
2647 if( 11 + i_size <= i_name )
2650 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2652 i_size -= 11 + i_name;
2653 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2655 const unsigned i_payload = GetDWLE( &p[2] );
2656 if( i_size < 6 + i_payload || i_payload <= 0 )
2661 if( !psz_description )
2662 psz_description = p_strn ? FromACP( p_strn->p_str ) : NULL;
2664 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2666 p_attachment = vlc_input_attachment_New( psz_name,
2667 "application/x-srt",
2671 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2675 free( psz_description );
2678 block_Release( p_block );
2681 msg_Dbg( p_demux, "Loaded an embed subtitle" );
2683 msg_Warn( p_demux, "Failed to load an embed subtitle" );
2685 if( p_indx == &ck.indx )
2686 AVI_ChunkFree( p_demux->s, &ck );
2688 /*****************************************************************************
2690 *****************************************************************************/
2691 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2693 demux_sys_t *p_sys = p_demux->p_sys;
2697 for( i = 0; i < p_sys->i_track; i++ )
2699 avi_track_t *tk = p_sys->track[i];
2700 if( tk->i_idxposc >= tk->idx.i_size )
2712 /****************************************************************************
2713 * AVI_MovieGetLength give max streams length in second
2714 ****************************************************************************/
2715 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2717 demux_sys_t *p_sys = p_demux->p_sys;
2718 mtime_t i_maxlength = 0;
2721 for( i = 0; i < p_sys->i_track; i++ )
2723 avi_track_t *tk = p_sys->track[i];
2726 /* fix length for each stream */
2727 if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2732 if( tk->i_samplesize )
2734 i_length = AVI_GetDPTS( tk,
2735 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2736 tk->idx.p_entry[tk->idx.i_size-1].i_length );
2740 i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2742 i_length /= (mtime_t)1000000; /* in seconds */
2745 "stream[%d] length:%"PRId64" (based on index)",
2748 i_maxlength = __MAX( i_maxlength, i_length );