1 /*****************************************************************************
2 * avi.c : AVI file Stream input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2001-2009 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
34 #include <vlc_common.h>
35 #include <vlc_plugin.h>
36 #include <vlc_demux.h>
37 #include <vlc_input.h>
39 #include <vlc_dialog.h>
42 #include <vlc_codecs.h>
43 #include <vlc_charset.h>
44 #include <vlc_memory.h>
49 /*****************************************************************************
51 *****************************************************************************/
53 #define INTERLEAVE_TEXT N_("Force interleaved method" )
55 #define INDEX_TEXT N_("Force index creation")
56 #define INDEX_LONGTEXT N_( \
57 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
58 "or incomplete (not seekable)." )
60 #define BI_RAWRGB 0x00
61 #define BI_RGBBITFIELDS 0x03
63 static int Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
66 static const int pi_index[] = {0,1,2,3};
68 static const char *const ppsz_indexes[] = { N_("Ask for action"),
71 N_("Fix when necessary")};
74 set_shortname( "AVI" )
75 set_description( N_("AVI demuxer") )
76 set_capability( "demux", 212 )
77 set_category( CAT_INPUT )
78 set_subcategory( SUBCAT_INPUT_DEMUX )
80 add_bool( "avi-interleaved", false,
81 INTERLEAVE_TEXT, INTERLEAVE_TEXT, true )
82 add_integer( "avi-index", 0,
83 INDEX_TEXT, INDEX_LONGTEXT, false )
84 change_integer_list( pi_index, ppsz_indexes )
86 set_callbacks( Open, Close )
89 /*****************************************************************************
91 *****************************************************************************/
92 static int Control ( demux_t *, int, va_list );
93 static int Seek ( demux_t *, mtime_t, int );
94 static int Demux_Seekable ( demux_t * );
95 static int Demux_UnSeekable( demux_t * );
97 static char *FromACP( const char *str )
99 return FromCharset(vlc_pgettext("GetACP", "CP1252"), str, strlen(str));
102 #define IGNORE_ES NAV_ES
103 #define READ_LENGTH (25 * 1000) // 25ms
107 vlc_fourcc_t i_fourcc;
110 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
112 uint8_t i_peek[8]; /* first 8 bytes */
114 unsigned int i_stream;
125 int64_t i_lengthtotal;
133 avi_entry_t *p_entry;
136 static void avi_index_Init( avi_index_t * );
137 static void avi_index_Clean( avi_index_t * );
138 static void avi_index_Append( avi_index_t *, off_t *, avi_entry_t * );
145 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
146 vlc_fourcc_t i_codec;
150 unsigned int i_samplesize;
152 unsigned int i_width_bytes;
158 es_out_id_t *p_es_dv_audio;
163 unsigned int i_idxposc; /* numero of chunk */
164 unsigned int i_idxposb; /* byte in the current chunk */
166 /* For VBR audio only */
167 unsigned int i_blockno;
168 unsigned int i_blocksize;
180 bool b_indexloaded; /* if we read indexes from end of file before starting */
181 uint32_t i_avih_flags;
187 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
189 /* number of streams and information */
190 unsigned int i_track;
196 unsigned int i_attachment;
197 input_attachment_t **attachment;
200 static inline off_t __EVEN( off_t i )
202 return (i & 1) ? i + 1 : i;
205 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
206 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
207 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
208 static mtime_t AVI_GetPTS ( avi_track_t * );
211 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
212 static int AVI_StreamChunkSet ( demux_t *,
213 unsigned int i_stream, unsigned int i_ck );
214 static int AVI_StreamBytesSet ( demux_t *,
215 unsigned int i_stream, off_t i_byte );
217 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
218 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
220 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
221 static int AVI_PacketNext ( demux_t * );
222 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
223 static int AVI_PacketSearch ( demux_t * );
225 static void AVI_IndexLoad ( demux_t * );
226 static void AVI_IndexCreate ( demux_t * );
228 static void AVI_ExtractSubtitle( demux_t *, unsigned int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
230 static void AVI_DvHandleAudio( demux_t *, avi_track_t *, block_t * );
232 static mtime_t AVI_MovieGetLength( demux_t * );
234 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
236 block_t * ReadFrame( demux_t *p_demux, const avi_track_t *tk,
237 const int i_header, const int i_size );
239 /*****************************************************************************
241 *****************************************************************************/
242 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
243 static int AVI_TrackStopFinishedStreams( demux_t *);
246 - For VBR mp3 stream:
247 count blocks by rounded-up chunksizes instead of chunks
248 we need full emulation of dshow avi demuxer bugs :(
249 fixes silly nandub-style a-v delaying in avi with vbr mp3...
250 (from mplayer 2002/08/02)
254 /*****************************************************************************
255 * Open: check file and initializes AVI structures
256 *****************************************************************************/
257 static int Open( vlc_object_t * p_this )
259 demux_t *p_demux = (demux_t *)p_this;
262 bool b_index = false, b_aborted = false;
265 avi_chunk_list_t *p_riff;
266 avi_chunk_list_t *p_hdrl, *p_movi;
267 avi_chunk_avih_t *p_avih;
269 unsigned int i_track;
270 unsigned int i, i_peeker;
272 const uint8_t *p_peek;
274 /* Is it an avi file ? */
275 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
277 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
279 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
281 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
285 if( i_peeker == 188 )
290 /* Initialize input structures. */
291 p_sys = p_demux->p_sys = calloc( 1, sizeof(demux_sys_t) );
292 p_sys->b_odml = false;
295 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
297 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_fastseekable );
298 stream_Control( p_demux->s, STREAM_CAN_SEEK, &p_sys->b_seekable );
300 p_demux->pf_control = Control;
301 p_demux->pf_demux = (p_sys->b_seekable) ? Demux_Seekable : Demux_UnSeekable;
303 p_sys->b_interleaved = var_InheritBool( p_demux, "avi-interleaved" );
307 stream_Read( p_demux->s, NULL, i_peeker );
310 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
312 msg_Err( p_demux, "avi module discarded (invalid file)" );
317 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
319 unsigned int i_count =
320 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
322 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
323 for( i = 1; i < i_count; i++ )
325 avi_chunk_list_t *p_sysx;
327 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
328 if( p_sysx->i_type == AVIFOURCC_AVIX )
330 msg_Warn( p_demux, "detected OpenDML file" );
331 p_sys->b_odml = true;
337 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
338 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
339 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
341 p_movi = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_movi, 0 );
343 if( !p_hdrl || !p_movi )
345 msg_Err( p_demux, "invalid file: cannot find hdrl or movi chunks" );
349 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
351 msg_Err( p_demux, "invalid file: cannot find avih chunk" );
354 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
355 if( p_avih->i_streams != i_track )
358 "found %d stream but %d are declared",
359 i_track, p_avih->i_streams );
363 msg_Err( p_demux, "no stream defined!" );
367 /* print information on streams */
368 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
370 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
371 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
372 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
373 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
375 p_sys->b_interleaved |= (p_avih->i_flags & AVIF_ISINTERLEAVED);
377 AVI_MetaLoad( p_demux, p_riff, p_avih );
378 p_sys->i_avih_flags = p_avih->i_flags;
380 /* now read info on each stream and create ES */
381 for( i = 0 ; i < i_track; i++ )
383 avi_track_t *tk = calloc( 1, sizeof( avi_track_t ) );
384 if( unlikely( !tk ) )
387 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
388 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
389 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
390 avi_chunk_strf_auds_t *p_auds = NULL;
391 avi_chunk_strf_vids_t *p_vids = NULL;
395 tk->b_activated = true;
397 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
398 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
400 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
402 msg_Warn( p_demux, "stream[%d] incomplete", i );
407 tk->i_rate = p_strh->i_rate;
408 tk->i_scale = p_strh->i_scale;
409 tk->i_samplesize = p_strh->i_samplesize;
410 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
411 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
413 switch( p_strh->i_type )
415 case( AVIFOURCC_auds ):
416 tk->i_cat = AUDIO_ES;
417 if( p_auds->p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
418 p_auds->p_wf->cbSize >= sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) )
420 WAVEFORMATEXTENSIBLE *p_wfe = (WAVEFORMATEXTENSIBLE *)p_auds->p_wf;
421 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
422 p_wfe->SubFormat.Data1 );
425 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
426 p_auds->p_wf->wFormatTag );
428 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
429 if( tk->i_blocksize == 0 )
431 if( p_auds->p_wf->wFormatTag == 1 )
432 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
436 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
438 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
439 "Using blocksize as a workaround.",
440 i, tk->i_samplesize, tk->i_blocksize );
441 tk->i_samplesize = tk->i_blocksize;
444 if( tk->i_codec == VLC_CODEC_VORBIS )
446 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
449 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
451 fmt.audio.i_channels = p_auds->p_wf->nChannels;
452 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
453 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
454 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
455 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
456 fmt.b_packetized = !tk->i_blocksize;
458 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
461 int i_chunk = AVIFOURCC_IAS1 + ((i - 1) << 24);
462 avi_chunk_STRING_t *p_lang = AVI_ChunkFind( p_info, i_chunk, 0 );
464 fmt.psz_language = FromACP( p_lang->p_str );
468 "stream[%d] audio(0x%x - %s) %d channels %dHz %dbits",
469 i, p_auds->p_wf->wFormatTag,vlc_fourcc_GetDescription(AUDIO_ES,tk->i_codec),
470 p_auds->p_wf->nChannels,
471 p_auds->p_wf->nSamplesPerSec,
472 p_auds->p_wf->wBitsPerSample );
474 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
475 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
476 if( fmt.i_extra > 0 )
478 fmt.p_extra = malloc( fmt.i_extra );
479 if( unlikely(fmt.p_extra == NULL) )
484 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
488 case( AVIFOURCC_vids ):
490 tk->i_cat = VIDEO_ES;
491 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
492 p_vids->p_bih->biCompression );
493 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
495 msg_Dbg( p_demux, "stream[%d] subtitles", i );
496 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
500 else if( p_vids->p_bih->biCompression == BI_RAWRGB )
502 switch( p_vids->p_bih->biBitCount )
505 tk->i_codec = VLC_CODEC_RGB32;
508 tk->i_codec = VLC_CODEC_RGB24;
510 case 16: /* Yes it is RV15 */
512 tk->i_codec = VLC_CODEC_RGB15;
514 case 9: /* <- TODO check that */
515 tk->i_codec = VLC_CODEC_I410;
518 if ( p_vids->p_bih->biClrUsed )
519 tk->i_codec = VLC_CODEC_RGBP;
521 tk->i_codec = VLC_CODEC_GREY;
524 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
526 switch( tk->i_codec )
528 case VLC_CODEC_RGB24:
529 case VLC_CODEC_RGB32:
530 fmt.video.i_rmask = 0x00ff0000;
531 fmt.video.i_gmask = 0x0000ff00;
532 fmt.video.i_bmask = 0x000000ff;
534 case VLC_CODEC_RGB15:
535 fmt.video.i_rmask = 0x7c00;
536 fmt.video.i_gmask = 0x03e0;
537 fmt.video.i_bmask = 0x001f;
541 const VLC_BITMAPINFO *p_bi = (const VLC_BITMAPINFO *) p_vids->p_bih;
542 fmt.video.p_palette = malloc( sizeof(video_palette_t) );
543 if ( fmt.video.p_palette )
546 for ( uint32_t i=0; i<p_vids->p_bih->biClrUsed; i++ )
548 entry = GetDWBE( &p_bi->bmiColors[i] );
549 fmt.video.p_palette->palette[i][0] = entry >> 24;
550 fmt.video.p_palette->palette[i][1] = (entry >> 16) & 0xFF;
551 fmt.video.p_palette->palette[i][2] = (entry >> 8) & 0xFF;
552 fmt.video.p_palette->palette[i][3] = entry & 0xFF;
554 fmt.video.p_palette->i_entries = p_vids->p_bih->biClrUsed;
562 tk->i_width_bytes = p_vids->p_bih->biWidth * (p_vids->p_bih->biBitCount >> 3);
563 /* RGB DIB are coded from bottom to top */
564 if ( p_vids->p_bih->biHeight < INT32_MAX ) tk->b_flipped = true;
568 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
569 if( tk->i_codec == VLC_CODEC_MP4V &&
570 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
573 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
576 tk->i_samplesize = 0;
578 fmt.video.i_width = p_vids->p_bih->biWidth;
579 fmt.video.i_height = p_vids->p_bih->biHeight;
580 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
581 fmt.video.i_frame_rate = tk->i_rate;
582 fmt.video.i_frame_rate_base = tk->i_scale;
584 /* Uncompresse Bitmap or YUV, YUV being always topdown */
585 if ( fmt.video.i_height > INT32_MAX )
587 (unsigned int)(-(int)p_vids->p_bih->biHeight);
589 avi_chunk_vprp_t *p_vprp = AVI_ChunkFind( p_strl, AVIFOURCC_vprp, 0 );
592 uint32_t i_frame_aspect_ratio = p_vprp->i_frame_aspect_ratio;
593 if( p_vprp->i_video_format_token >= 1 &&
594 p_vprp->i_video_format_token <= 4 )
595 i_frame_aspect_ratio = 0x00040003;
596 fmt.video.i_sar_num = ((i_frame_aspect_ratio >> 16) & 0xffff) * fmt.video.i_height;
597 fmt.video.i_sar_den = ((i_frame_aspect_ratio >> 0) & 0xffff) * fmt.video.i_width;
599 /* Extradata is the remainder of the chunk less the BIH */
600 fmt.i_extra = p_vids->i_chunk_size - sizeof(VLC_BITMAPINFOHEADER);
601 if( fmt.i_extra > 0 )
603 fmt.p_extra = malloc( fmt.i_extra );
604 if( unlikely(fmt.p_extra == NULL) )
609 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
612 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
613 i, (char*)&p_vids->p_bih->biCompression,
614 (uint32_t)p_vids->p_bih->biWidth,
615 (uint32_t)p_vids->p_bih->biHeight,
616 p_vids->p_bih->biBitCount,
617 (float)tk->i_rate/(float)tk->i_scale );
619 /* Extract palette from extradata if bpp <= 8 */
620 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
622 /* The palette should not be included in biSize, but come
623 * directly after BITMAPINFORHEADER in the BITMAPINFO structure */
624 if( fmt.i_extra > 0 && fmt.p_extra )
626 const uint8_t *p_pal = fmt.p_extra;
628 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
629 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
631 for( int k = 0; k < fmt.video.p_palette->i_entries; k++ )
633 for( int j = 0; j < 4; j++ )
634 fmt.video.p_palette->palette[k][j] = p_pal[4*k+j];
641 case( AVIFOURCC_txts):
642 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
643 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
647 case( AVIFOURCC_iavs):
648 case( AVIFOURCC_ivas):
649 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
650 tk->i_cat = VIDEO_ES;
651 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES, p_strh->i_handler );
652 tk->i_samplesize = 0;
653 tk->i_dv_audio_rate = tk->i_codec == VLC_CODEC_DV ? -1 : 0;
655 es_format_Init( &fmt, VIDEO_ES, p_strh->i_handler );
656 fmt.video.i_width = p_avih->i_width;
657 fmt.video.i_height = p_avih->i_height;
660 case( AVIFOURCC_mids):
661 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
664 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
669 fmt.psz_description = FromACP( p_strn->p_str );
670 tk->p_es = es_out_Add( p_demux->out, &fmt );
671 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
672 es_format_Clean( &fmt );
675 if( p_sys->i_track <= 0 )
677 msg_Err( p_demux, "no valid track" );
681 i_do_index = var_InheritInteger( p_demux, "avi-index" );
682 if( i_do_index == 1 ) /* Always fix */
685 if( p_sys->b_fastseekable )
687 AVI_IndexCreate( p_demux );
691 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
692 AVI_IndexLoad( p_demux );
697 AVI_IndexLoad( p_demux );
700 /* *** movie length in sec *** */
701 p_sys->i_length = AVI_MovieGetLength( p_demux );
703 /* Check the index completeness */
704 unsigned int i_idx_totalframes = 0;
705 for( unsigned int i = 0; i < p_sys->i_track; i++ )
707 const avi_track_t *tk = p_sys->track[i];
708 if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
709 i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
711 if( i_idx_totalframes != p_avih->i_totalframes &&
712 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
713 (mtime_t)p_avih->i_microsecperframe /
716 if( !vlc_object_alive( p_demux) )
719 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
720 "approximative or will exhibit strange behavior" );
721 if( (i_do_index == 0 || i_do_index == 3) && !b_index )
723 if( !p_sys->b_fastseekable ) {
727 if( i_do_index == 0 )
729 switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
730 _( "Because this AVI file index is broken or missing, "
731 "seeking will not work correctly.\n"
732 "VLC won't repair your file but can temporary fix this "
733 "problem by building an index in memory.\n"
734 "This step might take a long time on a large file.\n"
735 "What do you want to do?" ),
736 _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
740 msg_Dbg( p_demux, "Fixing AVI index" );
750 msg_Dbg( p_demux, "Fixing AVI index" );
756 /* fix some BeOS MediaKit generated file */
757 for( i = 0 ; i < p_sys->i_track; i++ )
759 avi_track_t *tk = p_sys->track[i];
760 avi_chunk_list_t *p_strl;
761 avi_chunk_strf_auds_t *p_auds;
763 if( tk->i_cat != AUDIO_ES )
767 if( tk->idx.i_size < 1 ||
769 tk->i_samplesize != 0 )
773 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
774 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
776 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
777 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
779 int64_t i_track_length =
780 tk->idx.p_entry[tk->idx.i_size-1].i_length +
781 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
782 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
783 (mtime_t)p_avih->i_microsecperframe;
787 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
790 tk->i_samplesize = 1;
791 tk->i_rate = i_track_length * CLOCK_FREQ / i_length;
792 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
796 if( p_sys->b_seekable )
798 /* we have read all chunk so go back to movi */
799 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
801 /* Skip movi header */
802 stream_Read( p_demux->s, NULL, 12 );
804 p_sys->i_movi_begin = p_movi->i_chunk_pos;
808 for( unsigned i = 0; i < p_sys->i_attachment; i++)
809 vlc_input_attachment_Delete(p_sys->attachment[i]);
810 free(p_sys->attachment);
813 vlc_meta_Delete( p_sys->meta );
815 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
817 return b_aborted ? VLC_ETIMEOUT : VLC_EGENERIC;
820 /*****************************************************************************
821 * Close: frees unused data
822 *****************************************************************************/
823 static void Close ( vlc_object_t * p_this )
825 demux_t * p_demux = (demux_t *)p_this;
826 demux_sys_t *p_sys = p_demux->p_sys ;
828 for( unsigned int i = 0; i < p_sys->i_track; i++ )
830 if( p_sys->track[i] )
832 avi_index_Clean( &p_sys->track[i]->idx );
833 free( p_sys->track[i] );
836 free( p_sys->track );
838 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
839 vlc_meta_Delete( p_sys->meta );
841 for( unsigned i = 0; i < p_sys->i_attachment; i++)
842 vlc_input_attachment_Delete(p_sys->attachment[i]);
843 free(p_sys->attachment);
848 /*****************************************************************************
849 * ReadFrame: Reads frame, using stride if necessary
850 *****************************************************************************/
852 block_t * ReadFrame( demux_t *p_demux, const avi_track_t *tk,
853 const int i_header, const int i_size )
855 block_t *p_frame = stream_Block( p_demux->s, __EVEN( i_size ) );
856 if ( !p_frame ) return p_frame;
858 if( i_size % 2 ) /* read was padded on word boundary */
864 if( tk->i_idxposb == 0 )
866 p_frame->p_buffer += i_header;
867 p_frame->i_buffer -= i_header;
870 if ( !tk->i_width_bytes )
873 const unsigned int i_stride_bytes = ((( (tk->i_width_bytes << 3) + 31) & ~31) >> 3);
875 if ( p_frame->i_buffer < i_stride_bytes )
877 p_frame->i_buffer = 0;
883 const uint8_t *p_src = p_frame->p_buffer + i_stride_bytes;
884 const uint8_t *p_end = p_frame->p_buffer + p_frame->i_buffer;
885 uint8_t *p_dst = p_frame->p_buffer + tk->i_width_bytes;
887 p_frame->i_buffer = tk->i_width_bytes;
889 while ( p_src + i_stride_bytes <= p_end )
891 memmove( p_dst, p_src, tk->i_width_bytes );
892 p_src += i_stride_bytes;
893 p_dst += tk->i_width_bytes;
894 p_frame->i_buffer += tk->i_width_bytes;
899 block_t *p_flippedframe = block_Alloc( p_frame->i_buffer );
900 if ( !p_flippedframe )
902 block_Release( p_frame );
906 unsigned int i_lines = p_frame->i_buffer / i_stride_bytes;
907 const uint8_t *p_src = p_frame->p_buffer + i_lines * i_stride_bytes;
908 uint8_t *p_dst = p_flippedframe->p_buffer;
910 p_flippedframe->i_buffer = 0;
912 while ( i_lines-- > 0 )
914 p_src -= i_stride_bytes;
915 memcpy( p_dst, p_src, tk->i_width_bytes );
916 p_dst += tk->i_width_bytes;
917 p_flippedframe->i_buffer += tk->i_width_bytes;
920 block_Release( p_frame );
921 p_frame = p_flippedframe;
927 /*****************************************************************************
928 * Demux_Seekable: reads and demuxes data packets for stream seekable
929 *****************************************************************************
930 * AVIDemux: reads and demuxes data packets
931 *****************************************************************************
932 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
933 *****************************************************************************/
940 off_t i_posf; /* where we will read :
941 if i_idxposb == 0 : begining of chunk (+8 to acces data)
942 else : point on data directly */
943 } avi_track_toread_t;
945 static int Demux_Seekable( demux_t *p_demux )
947 demux_sys_t *p_sys = p_demux->p_sys;
949 unsigned int i_track_count = 0;
950 unsigned int i_track;
951 /* cannot be more than 100 stream (dcXX or wbXX) */
952 avi_track_toread_t toread[100];
955 /* detect new selected/unselected streams */
956 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
958 avi_track_t *tk = p_sys->track[i_track];
961 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
962 if( tk->p_es_dv_audio )
965 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
968 if( b && !tk->b_activated )
970 if( p_sys->b_seekable)
972 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
974 tk->b_activated = true;
976 else if( !b && tk->b_activated )
978 tk->b_activated = false;
986 if( i_track_count <= 0 )
988 int64_t i_length = p_sys->i_length * CLOCK_FREQ;
990 p_sys->i_time += READ_LENGTH;
993 if( p_sys->i_time >= i_length )
997 msg_Warn( p_demux, "no track selected, exiting..." );
1001 /* wait for the good time */
1002 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
1003 p_sys->i_time += READ_LENGTH;
1006 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
1008 avi_track_t *tk = p_sys->track[i_track];
1011 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
1012 if( tk->i_idxposc < tk->idx.i_size )
1014 toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
1015 if( tk->i_idxposb > 0 )
1017 toread[i_track].i_posf += 8 + tk->i_idxposb;
1022 toread[i_track].i_posf = -1;
1025 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
1027 if( tk->i_samplesize )
1029 toread[i_track].i_toread = AVI_PTSToByte( tk, llabs( i_dpts ) );
1033 toread[i_track].i_toread = AVI_PTSToChunk( tk, llabs( i_dpts ) );
1038 toread[i_track].i_toread *= -1;
1051 /* search for first chunk to be read */
1052 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
1054 if( !toread[i].b_ok ||
1055 AVI_GetDPTS( p_sys->track[i],
1056 toread[i].i_toread ) <= -READ_LENGTH )
1061 if( toread[i].i_toread > 0 )
1063 b_done = false; /* not yet finished */
1065 if( toread[i].i_posf > 0 )
1067 if( i_pos == -1 || i_pos > toread[i].i_posf )
1070 i_pos = toread[i].i_posf;
1077 for( i = 0; i < p_sys->i_track; i++ )
1079 if( toread[i].b_ok )
1082 msg_Warn( p_demux, "all tracks have failed, exiting..." );
1088 int i_loop_count = 0;
1090 /* no valid index, we will parse directly the stream
1091 * in case we fail we will disable all finished stream */
1092 if( p_sys->b_seekable && p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1094 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1095 if( AVI_PacketNext( p_demux ) )
1097 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1102 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1107 avi_packet_t avi_pk;
1109 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1112 "cannot get packet header, track disabled" );
1113 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1115 if( avi_pk.i_stream >= p_sys->i_track ||
1116 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1118 if( AVI_PacketNext( p_demux ) )
1121 "cannot skip packet, track disabled" );
1122 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1125 /* Prevents from eating all the CPU with broken files.
1126 * This value should be low enough so that it doesn't
1127 * affect the reading speed too much. */
1128 if( !(++i_loop_count % 1024) )
1130 if( !vlc_object_alive (p_demux) ) return -1;
1133 if( !(i_loop_count % (1024 * 10)) )
1135 "don't seem to find any data..." );
1141 i_track = avi_pk.i_stream;
1142 tk = p_sys->track[i_track];
1144 /* add this chunk to the index */
1146 index.i_id = avi_pk.i_fourcc;
1147 index.i_flags = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
1148 index.i_pos = avi_pk.i_pos;
1149 index.i_length = avi_pk.i_size;
1150 index.i_lengthtotal = index.i_length;
1151 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1153 /* do we will read this data ? */
1154 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -READ_LENGTH )
1160 if( AVI_PacketNext( p_demux ) )
1163 "cannot skip packet, track disabled" );
1164 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1173 stream_Seek( p_demux->s, i_pos );
1176 /* Set the track to use */
1177 tk = p_sys->track[i_track];
1179 /* read thoses data */
1180 if( tk->i_samplesize )
1182 unsigned int i_toread;
1184 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1186 if( tk->i_samplesize > 1 )
1188 i_toread = tk->i_samplesize;
1192 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1193 i_toread = __MAX( i_toread, 100 );
1196 i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1202 i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1205 if( tk->i_idxposb == 0 )
1207 i_size += 8; /* need to read and skip header */
1210 if( ( p_frame = ReadFrame( p_demux, tk,
1211 ( tk->i_idxposb == 0 ) ? 8 : 0, i_size ) )==NULL )
1213 msg_Warn( p_demux, "failed reading data" );
1215 toread[i_track].b_ok = false;
1219 p_frame->i_pts = VLC_TS_0 + AVI_GetPTS( tk );
1220 if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1222 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1226 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1230 if( tk->i_samplesize )
1232 if( tk->i_idxposb == 0 )
1236 toread[i_track].i_toread -= i_size;
1237 tk->i_idxposb += i_size;
1238 if( tk->i_idxposb >=
1239 tk->idx.p_entry[tk->i_idxposc].i_length )
1247 int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1250 if( tk->i_cat == AUDIO_ES )
1252 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1254 toread[i_track].i_toread--;
1257 if( tk->i_idxposc < tk->idx.i_size)
1259 toread[i_track].i_posf =
1260 tk->idx.p_entry[tk->i_idxposc].i_pos;
1261 if( tk->i_idxposb > 0 )
1263 toread[i_track].i_posf += 8 + tk->i_idxposb;
1269 toread[i_track].i_posf = -1;
1272 if( tk->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 if( tk->i_dv_audio_rate )
1281 AVI_DvHandleAudio( p_demux, tk, p_frame );
1282 es_out_Send( p_demux->out, tk->p_es, p_frame );
1287 /*****************************************************************************
1288 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1289 *****************************************************************************
1290 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1291 *****************************************************************************/
1292 static int Demux_UnSeekable( demux_t *p_demux )
1294 demux_sys_t *p_sys = p_demux->p_sys;
1295 avi_track_t *p_stream_master = NULL;
1296 unsigned int i_stream;
1297 unsigned int i_packet;
1299 es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_time );
1301 /* *** find master stream for data packet skipping algo *** */
1302 /* *** -> first video, if any, or first audio ES *** */
1303 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1305 avi_track_t *tk = p_sys->track[i_stream];
1308 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1309 if( tk->p_es_dv_audio )
1312 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es_dv_audio, &b_extra );
1316 if( b && tk->i_cat == VIDEO_ES )
1318 p_stream_master = tk;
1322 p_stream_master = tk;
1326 if( !p_stream_master )
1328 msg_Warn( p_demux, "no more stream selected" );
1332 p_sys->i_time = AVI_GetPTS( p_stream_master );
1334 for( i_packet = 0; i_packet < 10; i_packet++)
1336 #define p_stream p_sys->track[avi_pk.i_stream]
1338 avi_packet_t avi_pk;
1340 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1345 if( avi_pk.i_stream >= p_sys->i_track ||
1346 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1348 /* we haven't found an audio or video packet:
1349 * - we have seek, found first next packet
1350 * - others packets could be found, skip them
1352 switch( avi_pk.i_fourcc )
1354 case AVIFOURCC_JUNK:
1355 case AVIFOURCC_LIST:
1356 case AVIFOURCC_RIFF:
1357 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1358 case AVIFOURCC_idx1:
1361 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1363 return( 0 ); /* eof */
1366 "seems to have lost position, resync" );
1367 if( AVI_PacketSearch( p_demux ) )
1369 msg_Err( p_demux, "resync failed" );
1376 /* check for time */
1377 if( llabs( AVI_GetPTS( p_stream ) -
1378 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1380 /* load it and send to decoder */
1381 block_t *p_frame = ReadFrame( p_demux, p_stream, 8, avi_pk.i_size + 8 ) ;
1382 if( p_frame == NULL )
1386 p_frame->i_pts = VLC_TS_0 + AVI_GetPTS( p_stream );
1388 if( avi_pk.i_cat != VIDEO_ES )
1389 p_frame->i_dts = p_frame->i_pts;
1392 p_frame->i_dts = p_frame->i_pts;
1393 p_frame->i_pts = VLC_TS_INVALID;
1396 if( p_stream->i_dv_audio_rate )
1397 AVI_DvHandleAudio( p_demux, p_stream, p_frame );
1398 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1402 if( AVI_PacketNext( p_demux ) )
1408 /* *** update stream time position *** */
1409 if( p_stream->i_samplesize )
1411 p_stream->i_idxposb += avi_pk.i_size;
1415 if( p_stream->i_cat == AUDIO_ES )
1417 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1419 p_stream->i_idxposc++;
1429 /*****************************************************************************
1430 * Seek: goto to i_date or i_percent
1431 *****************************************************************************/
1432 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1434 demux_sys_t *p_sys = p_demux->p_sys;
1435 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1436 i_date / CLOCK_FREQ, i_percent );
1438 if( p_sys->b_seekable )
1440 int64_t i_pos_backup = stream_Tell( p_demux->s );
1442 /* Check and lazy load indexes if it was not done (not fastseekable) */
1443 if ( !p_sys->b_indexloaded && ( p_sys->i_avih_flags & AVIF_HASINDEX ) )
1445 avi_chunk_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
1446 if (unlikely( !p_riff ))
1447 return VLC_EGENERIC;
1449 int i_ret = AVI_ChunkFetchIndexes( p_demux->s, p_riff );
1452 /* Go back to position before index failure */
1453 if ( stream_Tell( p_demux->s ) - i_pos_backup )
1454 stream_Seek( p_demux->s, i_pos_backup );
1456 if ( p_sys->i_avih_flags & AVIF_MUSTUSEINDEX )
1457 return VLC_EGENERIC;
1459 else AVI_IndexLoad( p_demux );
1461 p_sys->b_indexloaded = true; /* we don't want to try each time */
1464 if( !p_sys->i_length )
1466 avi_track_t *p_stream = NULL;
1467 unsigned i_stream = 0;
1470 if ( !p_sys->i_movi_lastchunk_pos && /* set when index is successfully loaded */
1471 ! ( p_sys->i_avih_flags & AVIF_ISINTERLEAVED ) )
1473 msg_Err( p_demux, "seeking without index at %d%%"
1474 " only works for interleaved files", i_percent );
1475 goto failandresetpos;
1477 /* use i_percent to create a true i_date */
1478 if( i_percent >= 100 )
1480 msg_Warn( p_demux, "cannot seek so far !" );
1481 goto failandresetpos;
1483 i_percent = __MAX( i_percent, 0 );
1485 /* try to find chunk that is at i_percent or the file */
1486 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1487 p_sys->i_movi_begin );
1488 /* search first selected stream (and prefer non-EOF ones) */
1489 for( unsigned i = 0; i < p_sys->i_track; i++ )
1491 avi_track_t *p_track = p_sys->track[i];
1492 if( !p_track->b_activated )
1497 if( !p_track->b_eof )
1500 if( p_stream == NULL )
1502 msg_Warn( p_demux, "cannot find any selected stream" );
1503 goto failandresetpos;
1506 /* be sure that the index exist */
1507 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1509 msg_Warn( p_demux, "cannot seek" );
1510 goto failandresetpos;
1513 while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1514 p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1516 /* search after i_idxposc */
1517 if( AVI_StreamChunkSet( p_demux,
1518 i_stream, p_stream->i_idxposc + 1 ) )
1520 msg_Warn( p_demux, "cannot seek" );
1521 goto failandresetpos;
1525 i_date = AVI_GetPTS( p_stream );
1526 /* TODO better support for i_samplesize != 0 */
1527 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1531 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1533 avi_track_t *p_stream = p_sys->track[i_stream];
1535 if( !p_stream->b_activated )
1538 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1540 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1541 p_sys->i_time = i_date;
1542 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /CLOCK_FREQ );
1546 /* Go back to position before index failure */
1547 if ( stream_Tell( p_demux->s ) - i_pos_backup )
1548 stream_Seek( p_demux->s, i_pos_backup );
1550 return VLC_EGENERIC;
1554 msg_Err( p_demux, "shouldn't yet be executed" );
1555 return VLC_EGENERIC;
1559 /*****************************************************************************
1561 *****************************************************************************/
1562 static double ControlGetPosition( demux_t *p_demux )
1564 demux_sys_t *p_sys = p_demux->p_sys;
1566 if( p_sys->i_length > 0 )
1568 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)CLOCK_FREQ );
1570 else if( stream_Size( p_demux->s ) > 0 )
1572 double i64 = (uint64_t)stream_Tell( p_demux->s );
1573 return i64 / stream_Size( p_demux->s );
1578 static int Control( demux_t *p_demux, int i_query, va_list args )
1580 demux_sys_t *p_sys = p_demux->p_sys;
1588 case DEMUX_GET_POSITION:
1589 pf = (double*)va_arg( args, double * );
1590 *pf = ControlGetPosition( p_demux );
1592 case DEMUX_SET_POSITION:
1593 f = (double)va_arg( args, double );
1594 if ( !p_sys->b_seekable )
1596 return VLC_EGENERIC;
1600 i64 = (mtime_t)(f * CLOCK_FREQ * p_sys->i_length);
1601 return Seek( p_demux, i64, (int)(f * 100) );
1604 case DEMUX_GET_TIME:
1605 pi64 = (int64_t*)va_arg( args, int64_t * );
1606 *pi64 = p_sys->i_time;
1609 case DEMUX_SET_TIME:
1613 i64 = (int64_t)va_arg( args, int64_t );
1614 if( !p_sys->b_seekable )
1616 return VLC_EGENERIC;
1618 else if( p_sys->i_length > 0 )
1620 i_percent = 100 * i64 / (p_sys->i_length*CLOCK_FREQ);
1622 else if( p_sys->i_time > 0 )
1624 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1625 (double)i64 / (double)p_sys->i_time );
1627 return Seek( p_demux, i64, i_percent );
1629 case DEMUX_GET_LENGTH:
1630 pi64 = (int64_t*)va_arg( args, int64_t * );
1631 *pi64 = p_sys->i_length * (mtime_t)CLOCK_FREQ;
1635 pf = (double*)va_arg( args, double * );
1637 for( i = 0; i < (int)p_sys->i_track; i++ )
1639 avi_track_t *tk = p_sys->track[i];
1640 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1642 *pf = (float)tk->i_rate / (float)tk->i_scale;
1648 case DEMUX_GET_META:
1649 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1650 vlc_meta_Merge( p_meta, p_sys->meta );
1653 case DEMUX_GET_ATTACHMENTS:
1655 if( p_sys->i_attachment <= 0 )
1656 return VLC_EGENERIC;
1658 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1659 int *pi_int = va_arg( args, int * );
1661 *pi_int = p_sys->i_attachment;
1662 *ppp_attach = calloc( p_sys->i_attachment, sizeof(**ppp_attach));
1663 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1664 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1669 return VLC_EGENERIC;
1673 /*****************************************************************************
1674 * Function to convert pts to chunk or byte
1675 *****************************************************************************/
1677 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1682 return (mtime_t)((int64_t)i_pts *
1683 (int64_t)tk->i_rate /
1684 (int64_t)tk->i_scale /
1685 (int64_t)CLOCK_FREQ );
1687 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1689 if( !tk->i_scale || !tk->i_samplesize )
1692 return (mtime_t)((int64_t)i_pts *
1693 (int64_t)tk->i_rate /
1694 (int64_t)tk->i_scale /
1696 (int64_t)tk->i_samplesize );
1699 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1706 i_dpts = (mtime_t)( (int64_t)1000000 *
1708 (int64_t)tk->i_scale /
1709 (int64_t)tk->i_rate );
1711 if( tk->i_samplesize )
1713 return i_dpts / tk->i_samplesize;
1718 static mtime_t AVI_GetPTS( avi_track_t *tk )
1720 if( tk->i_samplesize )
1722 int64_t i_count = 0;
1724 /* we need a valid entry we will emulate one */
1725 if( tk->i_idxposc == tk->idx.i_size )
1729 /* use the last entry */
1730 i_count = tk->idx.p_entry[tk->idx.i_size - 1].i_lengthtotal
1731 + tk->idx.p_entry[tk->idx.i_size - 1].i_length;
1736 i_count = tk->idx.p_entry[tk->i_idxposc].i_lengthtotal;
1738 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1742 if( tk->i_cat == AUDIO_ES )
1744 return AVI_GetDPTS( tk, tk->i_blockno );
1748 return AVI_GetDPTS( tk, tk->i_idxposc );
1753 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1755 demux_sys_t *p_sys = p_demux->p_sys;
1756 avi_packet_t avi_pk;
1757 int i_loop_count = 0;
1759 /* find first chunk of i_stream that isn't in index */
1761 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1763 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1764 if( AVI_PacketNext( p_demux ) )
1766 return VLC_EGENERIC;
1771 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1776 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1778 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1780 msg_Warn( p_demux, "cannot get packet header" );
1781 return VLC_EGENERIC;
1783 if( avi_pk.i_stream >= p_sys->i_track ||
1784 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1786 if( AVI_PacketNext( p_demux ) )
1788 return VLC_EGENERIC;
1791 /* Prevents from eating all the CPU with broken files.
1792 * This value should be low enough so that it doesn't
1793 * affect the reading speed too much. */
1794 if( !(++i_loop_count % 1024) )
1796 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1799 if( !(i_loop_count % (1024 * 10)) )
1800 msg_Warn( p_demux, "don't seem to find any data..." );
1805 avi_track_t *tk_pk = p_sys->track[avi_pk.i_stream];
1807 /* add this chunk to the index */
1809 index.i_id = avi_pk.i_fourcc;
1810 index.i_flags = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
1811 index.i_pos = avi_pk.i_pos;
1812 index.i_length = avi_pk.i_size;
1813 index.i_lengthtotal = index.i_length;
1814 avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1816 if( avi_pk.i_stream == i_stream )
1821 if( AVI_PacketNext( p_demux ) )
1823 return VLC_EGENERIC;
1829 /* be sure that i_ck will be a valid index entry */
1830 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1833 demux_sys_t *p_sys = p_demux->p_sys;
1834 avi_track_t *p_stream = p_sys->track[i_stream];
1836 p_stream->i_idxposc = i_ck;
1837 p_stream->i_idxposb = 0;
1839 if( i_ck >= p_stream->idx.i_size )
1841 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1844 p_stream->i_idxposc++;
1845 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1847 return VLC_EGENERIC;
1850 } while( p_stream->i_idxposc < i_ck );
1856 /* XXX FIXME up to now, we assume that all chunk are one after one */
1857 static int AVI_StreamBytesSet( demux_t *p_demux,
1858 unsigned int i_stream,
1861 demux_sys_t *p_sys = p_demux->p_sys;
1862 avi_track_t *p_stream = p_sys->track[i_stream];
1864 if( ( p_stream->idx.i_size > 0 )
1865 &&( i_byte < p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_lengthtotal +
1866 p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_length ) )
1868 /* index is valid to find the ck */
1869 /* uses dichototmie to be fast enougth */
1870 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->idx.i_size - 1 );
1871 int i_idxmax = p_stream->idx.i_size;
1875 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal > i_byte )
1877 i_idxmax = i_idxposc ;
1878 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1882 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal +
1883 p_stream->idx.p_entry[i_idxposc].i_length <= i_byte)
1885 i_idxmin = i_idxposc ;
1886 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1890 p_stream->i_idxposc = i_idxposc;
1891 p_stream->i_idxposb = i_byte -
1892 p_stream->idx.p_entry[i_idxposc].i_lengthtotal;
1901 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1902 p_stream->i_idxposb = 0;
1905 p_stream->i_idxposc++;
1906 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1908 return VLC_EGENERIC;
1911 } while( p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal +
1912 p_stream->idx.p_entry[p_stream->i_idxposc].i_length <= i_byte );
1914 p_stream->i_idxposb = i_byte -
1915 p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal;
1920 static int AVI_TrackSeek( demux_t *p_demux,
1924 demux_sys_t *p_sys = p_demux->p_sys;
1925 avi_track_t *tk = p_sys->track[i_stream];
1927 #define p_stream p_sys->track[i_stream]
1930 i_oldpts = AVI_GetPTS( p_stream );
1932 if( !p_stream->i_samplesize )
1934 if( AVI_StreamChunkSet( p_demux,
1936 AVI_PTSToChunk( p_stream, i_date ) ) )
1938 return VLC_EGENERIC;
1941 if( p_stream->i_cat == AUDIO_ES )
1945 for( i = 0; i < tk->i_idxposc; i++ )
1947 if( tk->i_blocksize > 0 )
1949 tk->i_blockno += ( tk->idx.p_entry[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1959 "old:%"PRId64" %s new %"PRId64,
1961 i_oldpts > i_date ? ">" : "<",
1964 if( p_stream->i_cat == VIDEO_ES )
1966 /* search key frame */
1967 //if( i_date < i_oldpts || 1 )
1969 while( p_stream->i_idxposc > 0 &&
1970 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1973 if( AVI_StreamChunkSet( p_demux,
1975 p_stream->i_idxposc - 1 ) )
1977 return VLC_EGENERIC;
1984 while( p_stream->i_idxposc < p_stream->idx.i_size &&
1985 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1988 if( AVI_StreamChunkSet( p_demux,
1990 p_stream->i_idxposc + 1 ) )
1992 return VLC_EGENERIC;
2001 if( AVI_StreamBytesSet( p_demux,
2003 AVI_PTSToByte( p_stream, i_date ) ) )
2005 return VLC_EGENERIC;
2012 /****************************************************************************
2013 * Return true if it's a key frame
2014 ****************************************************************************/
2015 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
2019 case VLC_CODEC_DIV1:
2021 * startcode: 0x00000100 32bits
2022 * framenumber ? 5bits
2023 * piture type 0(I),1(P) 2bits
2025 if( GetDWBE( p_byte ) != 0x00000100 )
2027 /* it's not an msmpegv1 stream, strange...*/
2028 return AVIIF_KEYFRAME;
2030 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
2032 case VLC_CODEC_DIV2:
2033 case VLC_CODEC_DIV3:
2034 case VLC_CODEC_WMV1:
2036 * picture type 0(I),1(P) 2bits
2038 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
2039 case VLC_CODEC_MP4V:
2040 /* we should find first occurrence of 0x000001b6 (32bits)
2041 * startcode: 0x000001b6 32bits
2042 * piture type 0(I),1(P) 2bits
2044 if( GetDWBE( p_byte ) != 0x000001b6 )
2046 /* not true , need to find the first VOP header */
2047 return AVIIF_KEYFRAME;
2049 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
2052 /* I can't do it, so say yes */
2053 return AVIIF_KEYFRAME;
2057 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
2062 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
2065 return vlc_fourcc_GetCodec( i_cat, i_codec );
2067 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
2071 /****************************************************************************
2073 ****************************************************************************/
2074 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
2075 unsigned int *pi_number, unsigned int *pi_type )
2077 #define SET_PTR( p, v ) if( p ) *(p) = (v);
2080 c1 = ((uint8_t *)&i_id)[0];
2081 c2 = ((uint8_t *)&i_id)[1];
2083 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
2085 SET_PTR( pi_number, 100 ); /* > max stream number */
2086 SET_PTR( pi_type, UNKNOWN_ES );
2090 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
2091 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
2094 SET_PTR( pi_type, AUDIO_ES );
2099 SET_PTR( pi_type, VIDEO_ES );
2103 SET_PTR( pi_type, SPU_ES );
2106 SET_PTR( pi_type, IGNORE_ES );
2109 SET_PTR( pi_type, UNKNOWN_ES );
2116 /****************************************************************************
2118 ****************************************************************************/
2119 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
2121 const uint8_t *p_peek;
2123 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
2125 return VLC_EGENERIC;
2127 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
2128 p_pk->i_size = GetDWLE( p_peek + 4 );
2129 p_pk->i_pos = stream_Tell( p_demux->s );
2130 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
2132 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
2133 p_peek[10], p_peek[11] );
2140 memcpy( p_pk->i_peek, p_peek + 8, 8 );
2142 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
2146 static int AVI_PacketNext( demux_t *p_demux )
2148 avi_packet_t avi_ck;
2151 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
2153 return VLC_EGENERIC;
2156 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
2157 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
2161 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
2162 avi_ck.i_type == AVIFOURCC_AVIX )
2168 i_skip = __EVEN( avi_ck.i_size ) + 8;
2171 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2173 return VLC_EGENERIC;
2178 static int AVI_PacketRead( demux_t *p_demux,
2180 block_t **pp_frame )
2184 i_size = __EVEN( p_pk->i_size + 8 );
2186 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2188 return VLC_EGENERIC;
2190 (*pp_frame)->p_buffer += 8;
2191 (*pp_frame)->i_buffer -= 8;
2193 if( i_size != p_pk->i_size + 8 )
2195 (*pp_frame)->i_buffer--;
2201 static int AVI_PacketSearch( demux_t *p_demux )
2203 demux_sys_t *p_sys = p_demux->p_sys;
2204 avi_packet_t avi_pk;
2209 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2211 return VLC_EGENERIC;
2213 AVI_PacketGetHeader( p_demux, &avi_pk );
2214 if( avi_pk.i_stream < p_sys->i_track &&
2215 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2219 switch( avi_pk.i_fourcc )
2221 case AVIFOURCC_JUNK:
2222 case AVIFOURCC_LIST:
2223 case AVIFOURCC_RIFF:
2224 case AVIFOURCC_idx1:
2228 /* Prevents from eating all the CPU with broken files.
2229 * This value should be low enough so that it doesn't affect the
2230 * reading speed too much (not that we care much anyway because
2231 * this code is called only on broken files). */
2232 if( !(++i_count % 1024) )
2234 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2237 if( !(i_count % (1024 * 10)) )
2238 msg_Warn( p_demux, "trying to resync..." );
2243 /****************************************************************************
2245 ****************************************************************************/
2246 static void avi_index_Init( avi_index_t *p_index )
2248 p_index->i_size = 0;
2250 p_index->p_entry = NULL;
2252 static void avi_index_Clean( avi_index_t *p_index )
2254 free( p_index->p_entry );
2256 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2257 avi_entry_t *p_entry )
2259 /* Update last chunk position */
2260 if( *pi_last_pos < p_entry->i_pos )
2261 *pi_last_pos = p_entry->i_pos;
2264 if( p_index->i_size >= p_index->i_max )
2266 p_index->i_max += 16384;
2267 p_index->p_entry = realloc_or_free( p_index->p_entry,
2268 p_index->i_max * sizeof( *p_index->p_entry ) );
2269 if( !p_index->p_entry )
2272 /* calculate cumulate length */
2273 if( p_index->i_size > 0 )
2275 p_entry->i_lengthtotal =
2276 p_index->p_entry[p_index->i_size - 1].i_length +
2277 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2281 p_entry->i_lengthtotal = 0;
2284 p_index->p_entry[p_index->i_size++] = *p_entry;
2287 static int AVI_IndexFind_idx1( demux_t *p_demux,
2288 avi_chunk_idx1_t **pp_idx1,
2289 uint64_t *pi_offset )
2291 demux_sys_t *p_sys = p_demux->p_sys;
2293 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2294 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2298 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2299 return VLC_EGENERIC;
2303 /* The offset in the index should be from the start of the movi content,
2304 * but some broken files use offset from the start of the file. Just
2305 * checking the offset of the first packet is not enough as some files
2306 * has unused chunk at the beginning of the movi content.
2308 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2309 uint64_t i_first_pos = UINT64_MAX;
2310 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 100 ); i++ )
2312 if ( p_idx1->entry[i].i_length > 0 )
2313 i_first_pos = __MIN( i_first_pos, p_idx1->entry[i].i_pos );
2316 const uint64_t i_movi_content = p_movi->i_chunk_pos + 8;
2317 if( i_first_pos < i_movi_content )
2319 *pi_offset = i_movi_content;
2321 else if( p_sys->b_seekable && i_first_pos < UINT64_MAX )
2323 const uint8_t *p_peek;
2324 if( !stream_Seek( p_demux->s, i_movi_content + i_first_pos ) &&
2325 stream_Peek( p_demux->s, &p_peek, 4 ) >= 4 &&
2326 ( !isdigit( p_peek[0] ) || !isdigit( p_peek[1] ) ||
2327 !isalpha( p_peek[2] ) || !isalpha( p_peek[3] ) ) )
2330 *pi_offset = i_movi_content;
2339 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2340 avi_index_t p_index[], off_t *pi_last_offset )
2342 demux_sys_t *p_sys = p_demux->p_sys;
2344 avi_chunk_idx1_t *p_idx1;
2346 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2347 return VLC_EGENERIC;
2349 p_sys->b_indexloaded = true;
2351 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2356 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2359 if( i_stream < p_sys->i_track &&
2360 (i_cat == p_sys->track[i_stream]->i_cat || i_cat == UNKNOWN_ES ) )
2363 index.i_id = p_idx1->entry[i_index].i_fourcc;
2364 index.i_flags = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2365 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2366 index.i_length = p_idx1->entry[i_index].i_length;
2367 index.i_lengthtotal = index.i_length;
2369 avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2375 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2376 avi_chunk_indx_t *p_indx )
2380 p_demux->p_sys->b_indexloaded = true;
2382 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2383 if( p_indx->i_indexsubtype == 0 )
2385 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2387 index.i_id = p_indx->i_id;
2388 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2389 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2390 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2391 index.i_lengthtotal = index.i_length;
2393 avi_index_Append( p_index, pi_max_offset, &index );
2396 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2398 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2400 index.i_id = p_indx->i_id;
2401 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2402 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2403 index.i_length = p_indx->idx.field[i].i_size;
2404 index.i_lengthtotal = index.i_length;
2406 avi_index_Append( p_index, pi_max_offset, &index );
2411 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2415 static void AVI_IndexLoad_indx( demux_t *p_demux,
2416 avi_index_t p_index[], off_t *pi_last_offset )
2418 demux_sys_t *p_sys = p_demux->p_sys;
2420 avi_chunk_list_t *p_riff;
2421 avi_chunk_list_t *p_hdrl;
2423 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2424 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2426 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2428 avi_chunk_list_t *p_strl;
2429 avi_chunk_indx_t *p_indx;
2431 #define p_stream p_sys->track[i_stream]
2432 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2433 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2438 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2443 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2445 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2447 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2449 if ( !p_sys->b_seekable )
2452 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2454 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2455 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2459 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2460 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2461 AVI_ChunkFree( p_demux->s, &ck_sub );
2466 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2472 static void AVI_IndexLoad( demux_t *p_demux )
2474 demux_sys_t *p_sys = p_demux->p_sys;
2477 assert( p_sys->i_track <= 100 );
2478 avi_index_t p_idx_indx[p_sys->i_track];
2479 avi_index_t p_idx_idx1[p_sys->i_track];
2480 for( unsigned i = 0; i < p_sys->i_track; i++ )
2482 avi_index_Init( &p_idx_indx[i] );
2483 avi_index_Init( &p_idx_idx1[i] );
2485 off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2486 off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2488 AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2489 if( !p_sys->b_odml )
2490 AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2492 /* Select the longest index */
2493 for( unsigned i = 0; i < p_sys->i_track; i++ )
2495 if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2497 msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2498 p_sys->track[i]->idx = p_idx_indx[i];
2499 avi_index_Clean( &p_idx_idx1[i] );
2503 msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2504 p_sys->track[i]->idx = p_idx_idx1[i];
2505 avi_index_Clean( &p_idx_indx[i] );
2508 p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2510 for( unsigned i = 0; i < p_sys->i_track; i++ )
2512 avi_index_t *p_index = &p_sys->track[i]->idx;
2516 for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2517 b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2520 msg_Err( p_demux, "no key frame set for track %u", i );
2521 for( unsigned j = 0; j < p_index->i_size; j++ )
2522 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2526 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2527 i, p_index->i_size );
2531 static void AVI_IndexCreate( demux_t *p_demux )
2533 demux_sys_t *p_sys = p_demux->p_sys;
2535 avi_chunk_list_t *p_riff;
2536 avi_chunk_list_t *p_movi;
2538 unsigned int i_stream;
2541 mtime_t i_dialog_update;
2542 dialog_progress_bar_t *p_dialog = NULL;
2544 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2545 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2549 msg_Err( p_demux, "cannot find p_movi" );
2553 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2554 avi_index_Init( &p_sys->track[i_stream]->idx );
2556 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2557 stream_Size( p_demux->s ) );
2559 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2560 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2563 /* Only show dialog if AVI is > 10MB */
2564 i_dialog_update = mdate();
2565 if( stream_Size( p_demux->s ) > 10000000 )
2566 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2567 NULL, _("Cancel") );
2573 if( !vlc_object_alive (p_demux) )
2576 /* Don't update/check dialog too often */
2577 if( p_dialog && mdate() - i_dialog_update > 100000 )
2579 if( dialog_ProgressCancelled( p_dialog ) )
2582 double f_current = stream_Tell( p_demux->s );
2583 double f_size = stream_Size( p_demux->s );
2584 double f_pos = f_current / f_size;
2585 dialog_ProgressSet( p_dialog, NULL, f_pos );
2587 i_dialog_update = mdate();
2590 if( AVI_PacketGetHeader( p_demux, &pk ) )
2593 if( pk.i_stream < p_sys->i_track &&
2594 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2596 avi_track_t *tk = p_sys->track[pk.i_stream];
2599 index.i_id = pk.i_fourcc;
2600 index.i_flags = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2601 index.i_pos = pk.i_pos;
2602 index.i_length = pk.i_size;
2603 index.i_lengthtotal = pk.i_size;
2604 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2608 switch( pk.i_fourcc )
2610 case AVIFOURCC_idx1:
2613 avi_chunk_list_t *p_sysx;
2614 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2615 AVIFOURCC_RIFF, 1 );
2617 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2618 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2624 case AVIFOURCC_RIFF:
2625 msg_Dbg( p_demux, "new RIFF chunk found" );
2629 case AVIFOURCC_JUNK:
2633 msg_Warn( p_demux, "need resync, probably broken avi" );
2634 if( AVI_PacketSearch( p_demux ) )
2636 msg_Warn( p_demux, "lost sync, abord index creation" );
2642 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2643 AVI_PacketNext( p_demux ) )
2650 if( p_dialog != NULL )
2651 dialog_ProgressDestroy( p_dialog );
2653 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2655 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2656 i_stream, p_sys->track[i_stream]->idx.i_size );
2661 static void AVI_MetaLoad( demux_t *p_demux,
2662 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2664 demux_sys_t *p_sys = p_demux->p_sys;
2666 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2671 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2672 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2673 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2674 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2675 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2676 vlc_meta_SetSetting( p_meta, buffer );
2678 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2682 static const struct {
2686 { AVIFOURCC_IART, vlc_meta_Artist },
2687 { AVIFOURCC_ICMT, vlc_meta_Description },
2688 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2689 { AVIFOURCC_IGNR, vlc_meta_Genre },
2690 { AVIFOURCC_INAM, vlc_meta_Title },
2691 { AVIFOURCC_ICRD, vlc_meta_Date },
2692 { AVIFOURCC_ILNG, vlc_meta_Language },
2693 { AVIFOURCC_IRTD, vlc_meta_Rating },
2694 { AVIFOURCC_IWEB, vlc_meta_URL },
2695 { AVIFOURCC_IPRT, vlc_meta_TrackNumber },
2696 { AVIFOURCC_IFRM, vlc_meta_TrackTotal },
2699 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2701 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2704 char *psz_value = FromACP( p_strz->p_str );
2709 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2713 static const vlc_fourcc_t p_extra[] = {
2714 AVIFOURCC_IARL, AVIFOURCC_ICMS, AVIFOURCC_ICRP, AVIFOURCC_IDIM, AVIFOURCC_IDPI,
2715 AVIFOURCC_IENG, AVIFOURCC_IKEY, AVIFOURCC_ILGT, AVIFOURCC_IMED, AVIFOURCC_IPLT,
2716 AVIFOURCC_IPRD, AVIFOURCC_ISBJ, AVIFOURCC_ISFT, AVIFOURCC_ISHP, AVIFOURCC_ISRC,
2717 AVIFOURCC_ISRF, AVIFOURCC_ITCH, AVIFOURCC_ISMP, AVIFOURCC_IDIT, AVIFOURCC_ISGN,
2718 AVIFOURCC_IWRI, AVIFOURCC_IPRO, AVIFOURCC_ICNM, AVIFOURCC_IPDS, AVIFOURCC_IEDT,
2719 AVIFOURCC_ICDS, AVIFOURCC_IMUS, AVIFOURCC_ISTD, AVIFOURCC_IDST, AVIFOURCC_ICNT,
2723 for( int i = 0; p_extra[i] != 0; i++ )
2725 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_extra[i], 0 );
2728 char *psz_value = FromACP( p_strz->p_str );
2733 vlc_meta_AddExtra( p_meta, p_strz->p_type, psz_value );
2738 static void AVI_DvHandleAudio( demux_t *p_demux, avi_track_t *tk, block_t *p_frame )
2740 size_t i_offset = 80 * 6 + 80 * 16 * 3 + 3;
2741 if( p_frame->i_buffer < i_offset + 5 )
2744 if( p_frame->p_buffer[i_offset] != 0x50 )
2748 dv_get_audio_format( &fmt, &p_frame->p_buffer[i_offset + 1] );
2750 if( tk->p_es_dv_audio && tk->i_dv_audio_rate != (int)fmt.audio.i_rate )
2752 es_out_Del( p_demux->out, tk->p_es_dv_audio );
2753 tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
2755 else if( !tk->p_es_dv_audio )
2757 tk->p_es_dv_audio = es_out_Add( p_demux->out, &fmt );
2759 tk->i_dv_audio_rate = fmt.audio.i_rate;
2761 block_t *p_frame_audio = dv_extract_audio( p_frame );
2763 es_out_Send( p_demux->out, tk->p_es_dv_audio, p_frame_audio );
2766 /*****************************************************************************
2768 *****************************************************************************/
2769 static void AVI_ExtractSubtitle( demux_t *p_demux,
2770 unsigned int i_stream,
2771 avi_chunk_list_t *p_strl,
2772 avi_chunk_STRING_t *p_strn )
2774 demux_sys_t *p_sys = p_demux->p_sys;
2775 block_t *p_block = NULL;
2776 input_attachment_t *p_attachment = NULL;
2777 char *psz_description = NULL;
2778 avi_chunk_indx_t *p_indx = NULL;
2780 if( !p_sys->b_seekable )
2783 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2789 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2790 p_indx->i_entriesinuse > 0 )
2792 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2793 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2798 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2799 p_indx->i_entriesinuse != 1 ||
2800 p_indx->i_indexsubtype != 0 )
2803 i_position = p_indx->i_baseoffset +
2804 p_indx->idx.std[0].i_offset - 8;
2805 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2809 avi_chunk_idx1_t *p_idx1;
2812 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2816 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2818 const idx1_entry_t *e = &p_idx1->entry[i];
2820 unsigned i_stream_idx;
2822 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2823 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2825 i_position = e->i_pos + i_offset;
2826 i_size = e->i_length + 8;
2835 if( i_size > 10000000 )
2837 msg_Dbg( p_demux, "Attached subtitle too big: %u", i_size );
2841 if( stream_Seek( p_demux->s, i_position ) )
2843 p_block = stream_Block( p_demux->s, i_size );
2847 /* Parse packet header */
2848 const uint8_t *p = p_block->p_buffer;
2849 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2854 /* Parse subtitle chunk header */
2855 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2856 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2858 const unsigned i_name = GetDWLE( &p[7] );
2859 if( 11 + i_size <= i_name )
2862 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2864 i_size -= 11 + i_name;
2865 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2867 const unsigned i_payload = GetDWLE( &p[2] );
2868 if( i_size < 6 + i_payload || i_payload <= 0 )
2873 if( !psz_description )
2874 psz_description = p_strn ? FromACP( p_strn->p_str ) : NULL;
2876 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2878 p_attachment = vlc_input_attachment_New( psz_name,
2879 "application/x-srt",
2883 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2887 free( psz_description );
2890 block_Release( p_block );
2893 msg_Dbg( p_demux, "Loaded an embedded subtitle" );
2895 msg_Warn( p_demux, "Failed to load an embedded subtitle" );
2897 if( p_indx == &ck.indx )
2898 AVI_ChunkFree( p_demux->s, &ck );
2900 /*****************************************************************************
2902 *****************************************************************************/
2903 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2905 demux_sys_t *p_sys = p_demux->p_sys;
2909 for( i = 0; i < p_sys->i_track; i++ )
2911 avi_track_t *tk = p_sys->track[i];
2912 if( tk->i_idxposc >= tk->idx.i_size )
2924 /****************************************************************************
2925 * AVI_MovieGetLength give max streams length in second
2926 ****************************************************************************/
2927 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2929 demux_sys_t *p_sys = p_demux->p_sys;
2930 mtime_t i_maxlength = 0;
2933 for( i = 0; i < p_sys->i_track; i++ )
2935 avi_track_t *tk = p_sys->track[i];
2938 /* fix length for each stream */
2939 if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2944 if( tk->i_samplesize )
2946 i_length = AVI_GetDPTS( tk,
2947 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2948 tk->idx.p_entry[tk->idx.i_size-1].i_length );
2952 i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2954 i_length /= CLOCK_FREQ; /* in seconds */
2957 "stream[%d] length:%"PRId64" (based on index)",
2960 i_maxlength = __MAX( i_maxlength, i_length );