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 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_demux.h>
35 #include <vlc_input.h>
37 #include <vlc_dialog.h>
40 #include <vlc_codecs.h>
41 #include <vlc_charset.h>
42 #include <vlc_memory.h>
46 /*****************************************************************************
48 *****************************************************************************/
50 #define INTERLEAVE_TEXT N_("Force interleaved method" )
51 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
53 #define INDEX_TEXT N_("Force index creation")
54 #define INDEX_LONGTEXT N_( \
55 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
56 "or incomplete (not seekable)." )
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 static const int pi_index[] = {0,1,2};
63 static const char *const ppsz_indexes[] = { N_("Ask for action"),
68 set_shortname( "AVI" )
69 set_description( N_("AVI demuxer") )
70 set_capability( "demux", 212 )
71 set_category( CAT_INPUT )
72 set_subcategory( SUBCAT_INPUT_DEMUX )
74 add_bool( "avi-interleaved", false,
75 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
76 add_integer( "avi-index", 0,
77 INDEX_TEXT, INDEX_LONGTEXT, false )
78 change_integer_list( pi_index, ppsz_indexes )
80 set_callbacks( Open, Close )
83 /*****************************************************************************
85 *****************************************************************************/
86 static int Control ( demux_t *, int, va_list );
87 static int Seek ( demux_t *, mtime_t, int );
88 static int Demux_Seekable ( demux_t * );
89 static int Demux_UnSeekable( demux_t * );
91 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
93 static char *FromACP( const char *str )
95 return FromCharset(vlc_pgettext("GetACP", "CP1252"), str, strlen(str));
98 #define IGNORE_ES NAV_ES
102 vlc_fourcc_t i_fourcc;
105 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
107 uint8_t i_peek[8]; /* first 8 bytes */
109 unsigned int i_stream;
120 int64_t i_lengthtotal;
128 avi_entry_t *p_entry;
131 static void avi_index_Init( avi_index_t * );
132 static void avi_index_Clean( avi_index_t * );
133 static void avi_index_Append( avi_index_t *, off_t *, avi_entry_t * );
140 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
141 vlc_fourcc_t i_codec;
145 unsigned int i_samplesize;
152 unsigned int i_idxposc; /* numero of chunk */
153 unsigned int i_idxposb; /* byte in the current chunk */
155 /* For VBR audio only */
156 unsigned int i_blockno;
157 unsigned int i_blocksize;
159 /* For muxed streams */
160 stream_t *p_out_muxed;
175 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
177 /* number of streams and information */
178 unsigned int i_track;
184 unsigned int i_attachment;
185 input_attachment_t **attachment;
188 static inline off_t __EVEN( off_t i )
190 return (i & 1) ? i + 1 : i;
193 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
194 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
195 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
196 static mtime_t AVI_GetPTS ( avi_track_t * );
199 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
200 static int AVI_StreamChunkSet ( demux_t *,
201 unsigned int i_stream, unsigned int i_ck );
202 static int AVI_StreamBytesSet ( demux_t *,
203 unsigned int i_stream, off_t i_byte );
205 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
206 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
208 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
209 static int AVI_PacketNext ( demux_t * );
210 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
211 static int AVI_PacketSearch ( demux_t * );
213 static void AVI_IndexLoad ( demux_t * );
214 static void AVI_IndexCreate ( demux_t * );
216 static void AVI_ExtractSubtitle( demux_t *, unsigned int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
218 static mtime_t AVI_MovieGetLength( demux_t * );
220 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
222 /*****************************************************************************
224 *****************************************************************************/
225 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
226 static int AVI_TrackStopFinishedStreams( demux_t *);
229 - For VBR mp3 stream:
230 count blocks by rounded-up chunksizes instead of chunks
231 we need full emulation of dshow avi demuxer bugs :(
232 fixes silly nandub-style a-v delaying in avi with vbr mp3...
233 (from mplayer 2002/08/02)
237 /*****************************************************************************
238 * Open: check file and initializes AVI structures
239 *****************************************************************************/
240 static int Open( vlc_object_t * p_this )
242 demux_t *p_demux = (demux_t *)p_this;
245 bool b_index = false;
248 avi_chunk_list_t *p_riff;
249 avi_chunk_list_t *p_hdrl, *p_movi;
250 avi_chunk_avih_t *p_avih;
252 unsigned int i_track;
253 unsigned int i, i_peeker;
255 const uint8_t *p_peek;
257 /* Is it an avi file ? */
258 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
260 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
262 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
264 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
268 if( i_peeker == 188 )
273 /* Initialize input structures. */
274 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
275 memset( p_sys, 0, sizeof( demux_sys_t ) );
278 p_sys->i_movi_lastchunk_pos = 0;
279 p_sys->b_odml = false;
280 p_sys->b_muxed = false;
284 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
286 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
288 p_demux->pf_control = Control;
289 p_demux->pf_demux = Demux_Seekable;
291 /* For unseekable stream, automatically use Demux_UnSeekable */
292 if( !p_sys->b_seekable
293 || var_InheritBool( p_demux, "avi-interleaved" ) )
295 p_demux->pf_demux = Demux_UnSeekable;
300 stream_Read( p_demux->s, NULL, i_peeker );
303 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
305 msg_Err( p_demux, "avi module discarded (invalid file)" );
310 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
312 unsigned int i_count =
313 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
315 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
316 for( i = 1; i < i_count; i++ )
318 avi_chunk_list_t *p_sysx;
320 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
321 if( p_sysx->i_type == AVIFOURCC_AVIX )
323 msg_Warn( p_demux, "detected OpenDML file" );
324 p_sys->b_odml = true;
330 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
331 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
332 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
334 if( !p_hdrl || !p_movi )
336 msg_Err( p_demux, "avi module discarded (invalid file)" );
340 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
342 msg_Err( p_demux, "cannot find avih chunk" );
345 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
346 if( p_avih->i_streams != i_track )
349 "found %d stream but %d are declared",
350 i_track, p_avih->i_streams );
354 msg_Err( p_demux, "no stream defined!" );
358 /* print information on streams */
359 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
361 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
362 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
363 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
364 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
366 AVI_MetaLoad( p_demux, p_riff, p_avih );
368 /* now read info on each stream and create ES */
369 for( i = 0 ; i < i_track; i++ )
371 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
375 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
376 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
377 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
378 avi_chunk_strf_auds_t *p_auds = NULL;
379 avi_chunk_strf_vids_t *p_vids = NULL;
382 memset( tk, 0, sizeof(*tk) );
384 tk->b_activated = true;
386 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
387 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
389 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
391 msg_Warn( p_demux, "stream[%d] incomplete", i );
396 tk->i_rate = p_strh->i_rate;
397 tk->i_scale = p_strh->i_scale;
398 tk->i_samplesize = p_strh->i_samplesize;
399 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
400 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
402 switch( p_strh->i_type )
404 case( AVIFOURCC_auds ):
405 tk->i_cat = AUDIO_ES;
406 if( p_auds->p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE &&
407 p_auds->p_wf->cbSize >= sizeof(WAVEFORMATEXTENSIBLE) - sizeof(WAVEFORMATEX) )
409 WAVEFORMATEXTENSIBLE *p_wfe = (WAVEFORMATEXTENSIBLE *)p_auds->p_wf;
410 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
411 p_wfe->SubFormat.Data1 );
414 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
415 p_auds->p_wf->wFormatTag );
417 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
418 if( tk->i_blocksize == 0 )
420 if( p_auds->p_wf->wFormatTag == 1 )
421 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
425 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
427 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
428 "Using blocksize as a workaround.",
429 i, tk->i_samplesize, tk->i_blocksize );
430 tk->i_samplesize = tk->i_blocksize;
433 if( tk->i_codec == VLC_CODEC_VORBIS )
435 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
438 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
440 fmt.audio.i_channels = p_auds->p_wf->nChannels;
441 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
442 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
443 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
444 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
445 fmt.b_packetized = !tk->i_blocksize;
448 "stream[%d] audio(0x%x - %s) %d channels %dHz %dbits",
449 i, p_auds->p_wf->wFormatTag,vlc_fourcc_GetDescription(AUDIO_ES,tk->i_codec),
450 p_auds->p_wf->nChannels,
451 p_auds->p_wf->nSamplesPerSec,
452 p_auds->p_wf->wBitsPerSample );
454 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
455 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
456 if( fmt.i_extra > 0 )
458 fmt.p_extra = malloc( fmt.i_extra );
459 if( !fmt.p_extra ) goto error;
460 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
464 case( AVIFOURCC_vids ):
465 tk->i_cat = VIDEO_ES;
466 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
467 p_vids->p_bih->biCompression );
468 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
470 msg_Dbg( p_demux, "stream[%d] subtitles", i );
471 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
475 else if( p_vids->p_bih->biCompression == 0x00 )
477 switch( p_vids->p_bih->biBitCount )
480 tk->i_codec = VLC_CODEC_RGB32;
483 tk->i_codec = VLC_CODEC_RGB24;
485 case 16: /* Yes it is RV15 */
487 tk->i_codec = VLC_CODEC_RGB15;
489 case 9: /* <- TODO check that */
490 tk->i_codec = VLC_CODEC_I410;
492 case 8: /* <- TODO check that */
493 tk->i_codec = VLC_CODEC_GREY;
496 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
498 switch( tk->i_codec )
500 case VLC_CODEC_RGB24:
501 case VLC_CODEC_RGB32:
502 fmt.video.i_rmask = 0x00ff0000;
503 fmt.video.i_gmask = 0x0000ff00;
504 fmt.video.i_bmask = 0x000000ff;
506 case VLC_CODEC_RGB15:
507 fmt.video.i_rmask = 0x7c00;
508 fmt.video.i_gmask = 0x03e0;
509 fmt.video.i_bmask = 0x001f;
517 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
518 if( tk->i_codec == VLC_CODEC_MP4V &&
519 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
522 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
525 tk->i_samplesize = 0;
526 fmt.video.i_width = p_vids->p_bih->biWidth;
527 fmt.video.i_height = p_vids->p_bih->biHeight;
528 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
529 fmt.video.i_frame_rate = tk->i_rate;
530 fmt.video.i_frame_rate_base = tk->i_scale;
532 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
533 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
534 if( fmt.i_extra > 0 )
536 fmt.p_extra = malloc( fmt.i_extra );
537 if( !fmt.p_extra ) goto error;
538 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
541 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
542 i, (char*)&p_vids->p_bih->biCompression,
543 (uint32_t)p_vids->p_bih->biWidth,
544 (uint32_t)p_vids->p_bih->biHeight,
545 p_vids->p_bih->biBitCount,
546 (float)tk->i_rate/(float)tk->i_scale );
548 if( p_vids->p_bih->biCompression == 0x00 )
550 /* RGB DIB are coded from bottom to top */
552 (unsigned int)(-(int)p_vids->p_bih->biHeight);
555 /* Extract palette from extradata if bpp <= 8
556 * (assumes that extradata contains only palette but appears
557 * to be true for all palettized codecs we support) */
558 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
560 /* The palette is not always included in biSize */
561 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
562 if( fmt.i_extra > 0 && fmt.p_extra )
564 const uint8_t *p_pal = fmt.p_extra;
566 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
567 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
569 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
571 for( int j = 0; j < 4; j++ )
572 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
578 case( AVIFOURCC_txts):
579 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
580 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
584 case( AVIFOURCC_iavs):
585 case( AVIFOURCC_ivas):
586 p_sys->b_muxed = true;
587 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
588 if( p_strh->i_handler == FOURCC_dvsd ||
589 p_strh->i_handler == FOURCC_dvhd ||
590 p_strh->i_handler == FOURCC_dvsl ||
591 p_strh->i_handler == FOURCC_dv25 ||
592 p_strh->i_handler == FOURCC_dv50 )
594 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
595 if( !tk->p_out_muxed )
596 msg_Err( p_demux, "could not load the DV parser" );
602 case( AVIFOURCC_mids):
603 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
606 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
611 fmt.psz_description = FromACP( p_strn->p_str );
612 if( tk->p_out_muxed == NULL )
613 tk->p_es = es_out_Add( p_demux->out, &fmt );
614 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
617 es_format_Clean( &fmt );
621 if( p_sys->i_track <= 0 )
623 msg_Err( p_demux, "no valid track" );
627 i_do_index = var_InheritInteger( p_demux, "avi-index" );
628 if( i_do_index == 1 ) /* Always fix */
631 if( p_sys->b_seekable )
633 AVI_IndexCreate( p_demux );
637 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
638 AVI_IndexLoad( p_demux );
643 AVI_IndexLoad( p_demux );
646 /* *** movie length in sec *** */
647 p_sys->i_length = AVI_MovieGetLength( p_demux );
649 /* Check the index completeness */
650 unsigned int i_idx_totalframes = 0;
651 for( unsigned int i = 0; i < p_sys->i_track; i++ )
653 const avi_track_t *tk = p_sys->track[i];
654 if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
655 i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
658 if( i_idx_totalframes != p_avih->i_totalframes &&
659 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
660 (mtime_t)p_avih->i_microsecperframe /
663 if( !vlc_object_alive( p_demux) )
666 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
667 "approximative or will exhibit strange behavior" );
668 if( i_do_index == 0 && !b_index )
670 if( !p_sys->b_seekable ) {
674 switch( dialog_Question( p_demux, _("Broken or missing AVI Index") ,
675 _( "Because this AVI file index is broken or missing, "
676 "seeking will not work correctly.\n"
677 "VLC won't repair your file but can temporary fix this "
678 "problem by building an index in memory.\n"
679 "This step might take a long time on a large file.\n"
680 "What do you want to do ?" ),
681 _( "Build index then play" ), _( "Play as is" ), _( "Do not play") ) )
685 msg_Dbg( p_demux, "Fixing AVI index" );
689 vlc_object_kill( p_demux->p_parent );
695 /* fix some BeOS MediaKit generated file */
696 for( i = 0 ; i < p_sys->i_track; i++ )
698 avi_track_t *tk = p_sys->track[i];
699 avi_chunk_list_t *p_strl;
700 avi_chunk_strh_t *p_strh;
701 avi_chunk_strf_auds_t *p_auds;
703 if( tk->i_cat != AUDIO_ES )
707 if( tk->idx.i_size < 1 ||
709 tk->i_samplesize != 0 )
713 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
714 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
715 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
717 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
718 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
720 int64_t i_track_length =
721 tk->idx.p_entry[tk->idx.i_size-1].i_length +
722 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
723 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
724 (mtime_t)p_avih->i_microsecperframe;
728 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
731 tk->i_samplesize = 1;
732 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
733 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
737 if( p_sys->b_seekable )
739 /* we have read all chunk so go back to movi */
740 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
742 /* Skip movi header */
743 stream_Read( p_demux->s, NULL, 12 );
745 p_sys->i_movi_begin = p_movi->i_chunk_pos;
749 for( unsigned i = 0; i < p_sys->i_attachment; i++)
750 vlc_input_attachment_Delete(p_sys->attachment[i]);
751 free(p_sys->attachment);
754 vlc_meta_Delete( p_sys->meta );
756 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
758 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
761 /*****************************************************************************
762 * Close: frees unused data
763 *****************************************************************************/
764 static void Close ( vlc_object_t * p_this )
766 demux_t * p_demux = (demux_t *)p_this;
768 demux_sys_t *p_sys = p_demux->p_sys ;
770 for( i = 0; i < p_sys->i_track; i++ )
772 if( p_sys->track[i] )
774 if( p_sys->track[i]->p_out_muxed )
775 stream_Delete( p_sys->track[i]->p_out_muxed );
776 avi_index_Clean( &p_sys->track[i]->idx );
777 free( p_sys->track[i] );
780 free( p_sys->track );
781 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
782 vlc_meta_Delete( p_sys->meta );
783 for( unsigned i = 0; i < p_sys->i_attachment; i++)
784 vlc_input_attachment_Delete(p_sys->attachment[i]);
785 free(p_sys->attachment);
790 /*****************************************************************************
791 * Demux_Seekable: reads and demuxes data packets for stream seekable
792 *****************************************************************************
793 * AVIDemux: reads and demuxes data packets
794 *****************************************************************************
795 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
796 *****************************************************************************/
803 off_t i_posf; /* where we will read :
804 if i_idxposb == 0 : begining of chunk (+8 to acces data)
805 else : point on data directly */
806 } avi_track_toread_t;
808 static int Demux_Seekable( demux_t *p_demux )
810 demux_sys_t *p_sys = p_demux->p_sys;
812 unsigned int i_track_count = 0;
813 unsigned int i_track;
814 /* cannot be more than 100 stream (dcXX or wbXX) */
815 avi_track_toread_t toread[100];
818 /* detect new selected/unselected streams */
819 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
821 avi_track_t *tk = p_sys->track[i_track];
824 if( p_sys->b_muxed && tk->p_out_muxed )
827 tk->b_activated = true;
831 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
832 if( b && !tk->b_activated )
834 if( p_sys->b_seekable)
836 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
838 tk->b_activated = true;
840 else if( !b && tk->b_activated )
842 tk->b_activated = false;
850 if( i_track_count <= 0 )
852 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
854 p_sys->i_time += 25*1000; /* read 25ms */
857 if( p_sys->i_time >= i_length )
861 msg_Warn( p_demux, "no track selected, exiting..." );
865 /* wait for the good time */
866 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
867 p_sys->i_time += 25*1000; /* read 25ms */
870 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
872 avi_track_t *tk = p_sys->track[i_track];
875 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
876 if( tk->i_idxposc < tk->idx.i_size )
878 toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
879 if( tk->i_idxposb > 0 )
881 toread[i_track].i_posf += 8 + tk->i_idxposb;
886 toread[i_track].i_posf = -1;
889 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
891 if( tk->i_samplesize )
893 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
897 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
902 toread[i_track].i_toread *= -1;
915 /* search for first chunk to be read */
916 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
918 if( !toread[i].b_ok ||
919 AVI_GetDPTS( p_sys->track[i],
920 toread[i].i_toread ) <= -25 * 1000 )
925 if( toread[i].i_toread > 0 )
927 b_done = false; /* not yet finished */
929 if( toread[i].i_posf > 0 )
931 if( i_pos == -1 || i_pos > toread[i].i_posf )
934 i_pos = toread[i].i_posf;
941 for( i = 0; i < p_sys->i_track; i++ )
946 msg_Warn( p_demux, "all tracks have failed, exiting..." );
952 int i_loop_count = 0;
954 /* no valid index, we will parse directly the stream
955 * in case we fail we will disable all finished stream */
956 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
958 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
959 if( AVI_PacketNext( p_demux ) )
961 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
966 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
973 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
976 "cannot get packet header, track disabled" );
977 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
979 if( avi_pk.i_stream >= p_sys->i_track ||
980 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
982 if( AVI_PacketNext( p_demux ) )
985 "cannot skip packet, track disabled" );
986 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
989 /* Prevents from eating all the CPU with broken files.
990 * This value should be low enough so that it doesn't
991 * affect the reading speed too much. */
992 if( !(++i_loop_count % 1024) )
994 if( !vlc_object_alive (p_demux) ) return -1;
997 if( !(i_loop_count % (1024 * 10)) )
999 "don't seem to find any data..." );
1005 i_track = avi_pk.i_stream;
1006 tk = p_sys->track[i_track];
1008 /* add this chunk to the index */
1010 index.i_id = avi_pk.i_fourcc;
1011 index.i_flags = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
1012 index.i_pos = avi_pk.i_pos;
1013 index.i_length = avi_pk.i_size;
1014 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1016 /* do we will read this data ? */
1017 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
1023 if( AVI_PacketNext( p_demux ) )
1026 "cannot skip packet, track disabled" );
1027 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1036 stream_Seek( p_demux->s, i_pos );
1039 /* Set the track to use */
1040 tk = p_sys->track[i_track];
1042 /* read thoses data */
1043 if( tk->i_samplesize )
1045 unsigned int i_toread;
1047 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1049 if( tk->i_samplesize > 1 )
1051 i_toread = tk->i_samplesize;
1055 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1056 i_toread = __MAX( i_toread, 100 );
1059 i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1065 i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1068 if( tk->i_idxposb == 0 )
1070 i_size += 8; /* need to read and skip header */
1073 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1075 msg_Warn( p_demux, "failed reading data" );
1077 toread[i_track].b_ok = false;
1080 if( i_size % 2 ) /* read was padded on word boundary */
1082 p_frame->i_buffer--;
1085 if( tk->i_idxposb == 0 )
1087 p_frame->p_buffer += 8;
1088 p_frame->i_buffer -= 8;
1090 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1091 if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1093 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1097 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1101 if( tk->i_samplesize )
1103 if( tk->i_idxposb == 0 )
1107 toread[i_track].i_toread -= i_size;
1108 tk->i_idxposb += i_size;
1109 if( tk->i_idxposb >=
1110 tk->idx.p_entry[tk->i_idxposc].i_length )
1118 int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1121 if( tk->i_cat == AUDIO_ES )
1123 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1125 toread[i_track].i_toread--;
1128 if( tk->i_idxposc < tk->idx.i_size)
1130 toread[i_track].i_posf =
1131 tk->idx.p_entry[tk->i_idxposc].i_pos;
1132 if( tk->i_idxposb > 0 )
1134 toread[i_track].i_posf += 8 + tk->i_idxposb;
1140 toread[i_track].i_posf = -1;
1143 if( tk->i_cat != VIDEO_ES )
1144 p_frame->i_dts = p_frame->i_pts;
1147 p_frame->i_dts = p_frame->i_pts;
1148 p_frame->i_pts = VLC_TS_INVALID;
1151 //p_pes->i_rate = p_demux->stream.control.i_rate;
1152 if( tk->p_out_muxed )
1153 stream_DemuxSend( tk->p_out_muxed, p_frame );
1155 es_out_Send( p_demux->out, tk->p_es, p_frame );
1160 /*****************************************************************************
1161 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1162 *****************************************************************************
1163 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1164 *****************************************************************************/
1165 static int Demux_UnSeekable( demux_t *p_demux )
1167 demux_sys_t *p_sys = p_demux->p_sys;
1168 avi_track_t *p_stream_master = NULL;
1169 unsigned int i_stream;
1170 unsigned int i_packet;
1172 if( p_sys->b_muxed )
1174 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1175 return VLC_EGENERIC;
1178 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1180 /* *** find master stream for data packet skipping algo *** */
1181 /* *** -> first video, if any, or first audio ES *** */
1182 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1184 avi_track_t *tk = p_sys->track[i_stream];
1187 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1189 if( b && tk->i_cat == VIDEO_ES )
1191 p_stream_master = tk;
1195 p_stream_master = tk;
1199 if( !p_stream_master )
1201 msg_Warn( p_demux, "no more stream selected" );
1205 p_sys->i_time = AVI_GetPTS( p_stream_master );
1207 for( i_packet = 0; i_packet < 10; i_packet++)
1209 #define p_stream p_sys->track[avi_pk.i_stream]
1211 avi_packet_t avi_pk;
1213 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1218 if( avi_pk.i_stream >= p_sys->i_track ||
1219 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1221 /* we haven't found an audio or video packet:
1222 * - we have seek, found first next packet
1223 * - others packets could be found, skip them
1225 switch( avi_pk.i_fourcc )
1227 case AVIFOURCC_JUNK:
1228 case AVIFOURCC_LIST:
1229 case AVIFOURCC_RIFF:
1230 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1231 case AVIFOURCC_idx1:
1234 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1236 return( 0 ); /* eof */
1239 "seems to have lost position, resync" );
1240 if( AVI_PacketSearch( p_demux ) )
1242 msg_Err( p_demux, "resync failed" );
1249 /* check for time */
1250 if( __ABS( AVI_GetPTS( p_stream ) -
1251 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1253 /* load it and send to decoder */
1255 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1259 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1261 if( avi_pk.i_cat != VIDEO_ES )
1262 p_frame->i_dts = p_frame->i_pts;
1265 p_frame->i_dts = p_frame->i_pts;
1266 p_frame->i_pts = VLC_TS_INVALID;
1269 //p_pes->i_rate = p_demux->stream.control.i_rate;
1270 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1274 if( AVI_PacketNext( p_demux ) )
1280 /* *** update stream time position *** */
1281 if( p_stream->i_samplesize )
1283 p_stream->i_idxposb += avi_pk.i_size;
1287 if( p_stream->i_cat == AUDIO_ES )
1289 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1291 p_stream->i_idxposc++;
1301 /*****************************************************************************
1302 * Seek: goto to i_date or i_percent
1303 *****************************************************************************/
1304 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1307 demux_sys_t *p_sys = p_demux->p_sys;
1308 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1309 i_date / 1000000, i_percent );
1311 if( p_sys->b_seekable )
1315 if( !p_sys->i_length )
1317 avi_track_t *p_stream = NULL;
1320 /* use i_percent to create a true i_date */
1321 msg_Warn( p_demux, "seeking without index at %d%%"
1322 " only works for interleaved files", i_percent );
1323 if( i_percent >= 100 )
1325 msg_Warn( p_demux, "cannot seek so far !" );
1326 return VLC_EGENERIC;
1328 i_percent = __MAX( i_percent, 0 );
1330 /* try to find chunk that is at i_percent or the file */
1331 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1332 p_sys->i_movi_begin );
1333 /* search first selected stream (and prefer non-EOF ones) */
1334 for( unsigned i = 0; i < p_sys->i_track; i++ )
1336 avi_track_t *p_track = p_sys->track[i];
1337 if( !p_track->b_activated )
1342 if( !p_track->b_eof )
1345 if( p_stream == NULL )
1347 msg_Warn( p_demux, "cannot find any selected stream" );
1348 return VLC_EGENERIC;
1351 /* be sure that the index exist */
1352 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1354 msg_Warn( p_demux, "cannot seek" );
1355 return VLC_EGENERIC;
1358 while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1359 p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1361 /* search after i_idxposc */
1362 if( AVI_StreamChunkSet( p_demux,
1363 i_stream, p_stream->i_idxposc + 1 ) )
1365 msg_Warn( p_demux, "cannot seek" );
1366 return VLC_EGENERIC;
1370 i_date = AVI_GetPTS( p_stream );
1371 /* TODO better support for i_samplesize != 0 */
1372 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1376 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1378 avi_track_t *p_stream = p_sys->track[i_stream];
1380 if( !p_stream->b_activated )
1383 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1385 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1386 p_sys->i_time = i_date;
1387 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1392 msg_Err( p_demux, "shouldn't yet be executed" );
1393 return VLC_EGENERIC;
1397 /*****************************************************************************
1399 *****************************************************************************/
1400 static double ControlGetPosition( demux_t *p_demux )
1402 demux_sys_t *p_sys = p_demux->p_sys;
1404 if( p_sys->i_length > 0 )
1406 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1408 else if( stream_Size( p_demux->s ) > 0 )
1414 /* search the more advanced selected es */
1415 for( i = 0; i < p_sys->i_track; i++ )
1417 avi_track_t *tk = p_sys->track[i];
1418 if( tk->b_activated && tk->i_idxposc < tk->idx.i_size )
1420 i_tmp = tk->idx.p_entry[tk->i_idxposc].i_pos +
1421 tk->idx.p_entry[tk->i_idxposc].i_length + 8;
1428 return (double)i64 / stream_Size( p_demux->s );
1433 static int Control( demux_t *p_demux, int i_query, va_list args )
1435 demux_sys_t *p_sys = p_demux->p_sys;
1443 case DEMUX_GET_POSITION:
1444 pf = (double*)va_arg( args, double * );
1445 *pf = ControlGetPosition( p_demux );
1447 case DEMUX_SET_POSITION:
1448 f = (double)va_arg( args, double );
1449 if( p_sys->b_seekable )
1451 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1452 return Seek( p_demux, i64, (int)(f * 100) );
1456 int64_t i_pos = stream_Size( p_demux->s ) * f;
1457 return stream_Seek( p_demux->s, i_pos );
1460 case DEMUX_GET_TIME:
1461 pi64 = (int64_t*)va_arg( args, int64_t * );
1462 *pi64 = p_sys->i_time;
1465 case DEMUX_SET_TIME:
1469 i64 = (int64_t)va_arg( args, int64_t );
1470 if( p_sys->i_length > 0 )
1472 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1474 else if( p_sys->i_time > 0 )
1476 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1477 (double)i64 / (double)p_sys->i_time );
1479 return Seek( p_demux, i64, i_percent );
1481 case DEMUX_GET_LENGTH:
1482 pi64 = (int64_t*)va_arg( args, int64_t * );
1483 *pi64 = p_sys->i_length * (mtime_t)1000000;
1487 pf = (double*)va_arg( args, double * );
1489 for( i = 0; i < (int)p_sys->i_track; i++ )
1491 avi_track_t *tk = p_sys->track[i];
1492 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1494 *pf = (float)tk->i_rate / (float)tk->i_scale;
1500 case DEMUX_GET_META:
1501 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1502 vlc_meta_Merge( p_meta, p_sys->meta );
1505 case DEMUX_GET_ATTACHMENTS:
1507 if( p_sys->i_attachment <= 0 )
1508 return VLC_EGENERIC;
1510 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1511 int *pi_int = va_arg( args, int * );
1513 *pi_int = p_sys->i_attachment;
1514 *ppp_attach = calloc( p_sys->i_attachment, sizeof(*ppp_attach));
1515 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1516 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1521 return VLC_EGENERIC;
1525 /*****************************************************************************
1526 * Function to convert pts to chunk or byte
1527 *****************************************************************************/
1529 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1534 return (mtime_t)((int64_t)i_pts *
1535 (int64_t)tk->i_rate /
1536 (int64_t)tk->i_scale /
1539 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1541 if( !tk->i_scale || !tk->i_samplesize )
1544 return (mtime_t)((int64_t)i_pts *
1545 (int64_t)tk->i_rate /
1546 (int64_t)tk->i_scale /
1548 (int64_t)tk->i_samplesize );
1551 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1558 i_dpts = (mtime_t)( (int64_t)1000000 *
1560 (int64_t)tk->i_scale /
1561 (int64_t)tk->i_rate );
1563 if( tk->i_samplesize )
1565 return i_dpts / tk->i_samplesize;
1570 static mtime_t AVI_GetPTS( avi_track_t *tk )
1572 if( tk->i_samplesize )
1574 int64_t i_count = 0;
1576 /* we need a valid entry we will emulate one */
1577 if( tk->i_idxposc == tk->idx.i_size )
1581 /* use the last entry */
1582 i_count = tk->idx.p_entry[tk->idx.i_size - 1].i_lengthtotal
1583 + tk->idx.p_entry[tk->idx.i_size - 1].i_length;
1588 i_count = tk->idx.p_entry[tk->i_idxposc].i_lengthtotal;
1590 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1594 if( tk->i_cat == AUDIO_ES )
1596 return AVI_GetDPTS( tk, tk->i_blockno );
1600 return AVI_GetDPTS( tk, tk->i_idxposc );
1605 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1607 demux_sys_t *p_sys = p_demux->p_sys;
1608 avi_packet_t avi_pk;
1609 int i_loop_count = 0;
1611 /* find first chunk of i_stream that isn't in index */
1613 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1615 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1616 if( AVI_PacketNext( p_demux ) )
1618 return VLC_EGENERIC;
1623 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1628 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1630 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1632 msg_Warn( p_demux, "cannot get packet header" );
1633 return VLC_EGENERIC;
1635 if( avi_pk.i_stream >= p_sys->i_track ||
1636 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1638 if( AVI_PacketNext( p_demux ) )
1640 return VLC_EGENERIC;
1643 /* Prevents from eating all the CPU with broken files.
1644 * This value should be low enough so that it doesn't
1645 * affect the reading speed too much. */
1646 if( !(++i_loop_count % 1024) )
1648 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1651 if( !(i_loop_count % (1024 * 10)) )
1652 msg_Warn( p_demux, "don't seem to find any data..." );
1657 avi_track_t *tk_pk = p_sys->track[avi_pk.i_stream];
1659 /* add this chunk to the index */
1661 index.i_id = avi_pk.i_fourcc;
1662 index.i_flags = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
1663 index.i_pos = avi_pk.i_pos;
1664 index.i_length = avi_pk.i_size;
1665 avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1667 if( avi_pk.i_stream == i_stream )
1672 if( AVI_PacketNext( p_demux ) )
1674 return VLC_EGENERIC;
1680 /* be sure that i_ck will be a valid index entry */
1681 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1684 demux_sys_t *p_sys = p_demux->p_sys;
1685 avi_track_t *p_stream = p_sys->track[i_stream];
1687 p_stream->i_idxposc = i_ck;
1688 p_stream->i_idxposb = 0;
1690 if( i_ck >= p_stream->idx.i_size )
1692 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1695 p_stream->i_idxposc++;
1696 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1698 return VLC_EGENERIC;
1701 } while( p_stream->i_idxposc < i_ck );
1707 /* XXX FIXME up to now, we assume that all chunk are one after one */
1708 static int AVI_StreamBytesSet( demux_t *p_demux,
1709 unsigned int i_stream,
1712 demux_sys_t *p_sys = p_demux->p_sys;
1713 avi_track_t *p_stream = p_sys->track[i_stream];
1715 if( ( p_stream->idx.i_size > 0 )
1716 &&( i_byte < p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_lengthtotal +
1717 p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_length ) )
1719 /* index is valid to find the ck */
1720 /* uses dichototmie to be fast enougth */
1721 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->idx.i_size - 1 );
1722 int i_idxmax = p_stream->idx.i_size;
1726 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal > i_byte )
1728 i_idxmax = i_idxposc ;
1729 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1733 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal +
1734 p_stream->idx.p_entry[i_idxposc].i_length <= i_byte)
1736 i_idxmin = i_idxposc ;
1737 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1741 p_stream->i_idxposc = i_idxposc;
1742 p_stream->i_idxposb = i_byte -
1743 p_stream->idx.p_entry[i_idxposc].i_lengthtotal;
1752 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1753 p_stream->i_idxposb = 0;
1756 p_stream->i_idxposc++;
1757 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1759 return VLC_EGENERIC;
1762 } while( p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal +
1763 p_stream->idx.p_entry[p_stream->i_idxposc].i_length <= i_byte );
1765 p_stream->i_idxposb = i_byte -
1766 p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal;
1771 static int AVI_TrackSeek( demux_t *p_demux,
1775 demux_sys_t *p_sys = p_demux->p_sys;
1776 avi_track_t *tk = p_sys->track[i_stream];
1778 #define p_stream p_sys->track[i_stream]
1781 i_oldpts = AVI_GetPTS( p_stream );
1783 if( !p_stream->i_samplesize )
1785 if( AVI_StreamChunkSet( p_demux,
1787 AVI_PTSToChunk( p_stream, i_date ) ) )
1789 return VLC_EGENERIC;
1792 if( p_stream->i_cat == AUDIO_ES )
1796 for( i = 0; i < tk->i_idxposc; i++ )
1798 if( tk->i_blocksize > 0 )
1800 tk->i_blockno += ( tk->idx.p_entry[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1810 "old:%"PRId64" %s new %"PRId64,
1812 i_oldpts > i_date ? ">" : "<",
1815 if( p_stream->i_cat == VIDEO_ES )
1817 /* search key frame */
1818 //if( i_date < i_oldpts || 1 )
1820 while( p_stream->i_idxposc > 0 &&
1821 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1824 if( AVI_StreamChunkSet( p_demux,
1826 p_stream->i_idxposc - 1 ) )
1828 return VLC_EGENERIC;
1835 while( p_stream->i_idxposc < p_stream->idx.i_size &&
1836 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1839 if( AVI_StreamChunkSet( p_demux,
1841 p_stream->i_idxposc + 1 ) )
1843 return VLC_EGENERIC;
1852 if( AVI_StreamBytesSet( p_demux,
1854 AVI_PTSToByte( p_stream, i_date ) ) )
1856 return VLC_EGENERIC;
1863 /****************************************************************************
1864 * Return true if it's a key frame
1865 ****************************************************************************/
1866 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1870 case VLC_CODEC_DIV1:
1872 * startcode: 0x00000100 32bits
1873 * framenumber ? 5bits
1874 * piture type 0(I),1(P) 2bits
1876 if( GetDWBE( p_byte ) != 0x00000100 )
1878 /* it's not an msmpegv1 stream, strange...*/
1879 return AVIIF_KEYFRAME;
1881 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1883 case VLC_CODEC_DIV2:
1884 case VLC_CODEC_DIV3:
1885 case VLC_CODEC_WMV1:
1887 * picture type 0(I),1(P) 2bits
1889 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1890 case VLC_CODEC_MP4V:
1891 /* we should find first occurrence of 0x000001b6 (32bits)
1892 * startcode: 0x000001b6 32bits
1893 * piture type 0(I),1(P) 2bits
1895 if( GetDWBE( p_byte ) != 0x000001b6 )
1897 /* not true , need to find the first VOP header */
1898 return AVIIF_KEYFRAME;
1900 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1903 /* I can't do it, so say yes */
1904 return AVIIF_KEYFRAME;
1908 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1913 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1916 return vlc_fourcc_GetCodec( i_cat, i_codec );
1918 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1922 /****************************************************************************
1924 ****************************************************************************/
1925 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1926 unsigned int *pi_number, unsigned int *pi_type )
1928 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1931 c1 = ((uint8_t *)&i_id)[0];
1932 c2 = ((uint8_t *)&i_id)[1];
1934 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1936 SET_PTR( pi_number, 100 ); /* > max stream number */
1937 SET_PTR( pi_type, UNKNOWN_ES );
1941 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1942 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1945 SET_PTR( pi_type, AUDIO_ES );
1950 SET_PTR( pi_type, VIDEO_ES );
1954 SET_PTR( pi_type, SPU_ES );
1957 SET_PTR( pi_type, IGNORE_ES );
1960 SET_PTR( pi_type, UNKNOWN_ES );
1967 /****************************************************************************
1969 ****************************************************************************/
1970 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1972 const uint8_t *p_peek;
1974 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1976 return VLC_EGENERIC;
1978 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1979 p_pk->i_size = GetDWLE( p_peek + 4 );
1980 p_pk->i_pos = stream_Tell( p_demux->s );
1981 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1983 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1984 p_peek[10], p_peek[11] );
1991 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1993 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1997 static int AVI_PacketNext( demux_t *p_demux )
1999 avi_packet_t avi_ck;
2002 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
2004 return VLC_EGENERIC;
2007 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
2008 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
2012 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
2013 avi_ck.i_type == AVIFOURCC_AVIX )
2019 i_skip = __EVEN( avi_ck.i_size ) + 8;
2022 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
2024 return VLC_EGENERIC;
2029 static int AVI_PacketRead( demux_t *p_demux,
2031 block_t **pp_frame )
2035 i_size = __EVEN( p_pk->i_size + 8 );
2037 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2039 return VLC_EGENERIC;
2041 (*pp_frame)->p_buffer += 8;
2042 (*pp_frame)->i_buffer -= 8;
2044 if( i_size != p_pk->i_size + 8 )
2046 (*pp_frame)->i_buffer--;
2052 static int AVI_PacketSearch( demux_t *p_demux )
2054 demux_sys_t *p_sys = p_demux->p_sys;
2055 avi_packet_t avi_pk;
2060 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2062 return VLC_EGENERIC;
2064 AVI_PacketGetHeader( p_demux, &avi_pk );
2065 if( avi_pk.i_stream < p_sys->i_track &&
2066 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2070 switch( avi_pk.i_fourcc )
2072 case AVIFOURCC_JUNK:
2073 case AVIFOURCC_LIST:
2074 case AVIFOURCC_RIFF:
2075 case AVIFOURCC_idx1:
2079 /* Prevents from eating all the CPU with broken files.
2080 * This value should be low enough so that it doesn't affect the
2081 * reading speed too much (not that we care much anyway because
2082 * this code is called only on broken files). */
2083 if( !(++i_count % 1024) )
2085 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2088 if( !(i_count % (1024 * 10)) )
2089 msg_Warn( p_demux, "trying to resync..." );
2094 /****************************************************************************
2096 ****************************************************************************/
2097 static void avi_index_Init( avi_index_t *p_index )
2099 p_index->i_size = 0;
2101 p_index->p_entry = NULL;
2103 static void avi_index_Clean( avi_index_t *p_index )
2105 free( p_index->p_entry );
2107 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2108 avi_entry_t *p_entry )
2110 /* Update last chunk position */
2111 if( *pi_last_pos < p_entry->i_pos )
2112 *pi_last_pos = p_entry->i_pos;
2115 if( p_index->i_size >= p_index->i_max )
2117 p_index->i_max += 16384;
2118 p_index->p_entry = realloc_or_free( p_index->p_entry,
2119 p_index->i_max * sizeof( *p_index->p_entry ) );
2120 if( !p_index->p_entry )
2123 /* calculate cumulate length */
2124 if( p_index->i_size > 0 )
2126 p_entry->i_lengthtotal =
2127 p_index->p_entry[p_index->i_size - 1].i_length +
2128 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2132 p_entry->i_lengthtotal = 0;
2135 p_index->p_entry[p_index->i_size++] = *p_entry;
2138 static int AVI_IndexFind_idx1( demux_t *p_demux,
2139 avi_chunk_idx1_t **pp_idx1,
2140 uint64_t *pi_offset )
2142 demux_sys_t *p_sys = p_demux->p_sys;
2144 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2145 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2149 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2150 return VLC_EGENERIC;
2154 /* *** calculate offset *** */
2155 /* Well, avi is __SHIT__ so test more than one entry
2156 * (needed for some avi files) */
2157 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2159 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2161 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2163 *pi_offset = p_movi->i_chunk_pos + 8;
2170 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2171 avi_index_t p_index[], off_t *pi_last_offset )
2173 demux_sys_t *p_sys = p_demux->p_sys;
2175 avi_chunk_idx1_t *p_idx1;
2177 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2178 return VLC_EGENERIC;
2180 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2185 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2188 if( i_stream < p_sys->i_track &&
2189 (i_cat == p_sys->track[i_stream]->i_cat || i_cat == UNKNOWN_ES ) )
2192 index.i_id = p_idx1->entry[i_index].i_fourcc;
2193 index.i_flags = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2194 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2195 index.i_length = p_idx1->entry[i_index].i_length;
2197 avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2203 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2204 avi_chunk_indx_t *p_indx )
2208 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2209 if( p_indx->i_indexsubtype == 0 )
2211 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2213 index.i_id = p_indx->i_id;
2214 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2215 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2216 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2218 avi_index_Append( p_index, pi_max_offset, &index );
2221 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2223 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2225 index.i_id = p_indx->i_id;
2226 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2227 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2228 index.i_length = p_indx->idx.field[i].i_size;
2230 avi_index_Append( p_index, pi_max_offset, &index );
2235 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2239 static void AVI_IndexLoad_indx( demux_t *p_demux,
2240 avi_index_t p_index[], off_t *pi_last_offset )
2242 demux_sys_t *p_sys = p_demux->p_sys;
2244 avi_chunk_list_t *p_riff;
2245 avi_chunk_list_t *p_hdrl;
2247 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2248 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2250 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2252 avi_chunk_list_t *p_strl;
2253 avi_chunk_indx_t *p_indx;
2255 #define p_stream p_sys->track[i_stream]
2256 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2257 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2262 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2267 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2269 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2271 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2274 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2276 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2277 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2281 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2282 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2283 AVI_ChunkFree( p_demux->s, &ck_sub );
2288 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2294 static void AVI_IndexLoad( demux_t *p_demux )
2296 demux_sys_t *p_sys = p_demux->p_sys;
2299 assert( p_sys->i_track <= 100 );
2300 avi_index_t p_idx_indx[p_sys->i_track];
2301 avi_index_t p_idx_idx1[p_sys->i_track];
2302 for( unsigned i = 0; i < p_sys->i_track; i++ )
2304 avi_index_Init( &p_idx_indx[i] );
2305 avi_index_Init( &p_idx_idx1[i] );
2307 off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2308 off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2310 AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2311 if( !p_sys->b_odml )
2312 AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2314 /* Select the longest index */
2315 for( unsigned i = 0; i < p_sys->i_track; i++ )
2317 if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2319 msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2320 p_sys->track[i]->idx = p_idx_indx[i];
2321 avi_index_Clean( &p_idx_idx1[i] );
2325 msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2326 p_sys->track[i]->idx = p_idx_idx1[i];
2327 avi_index_Clean( &p_idx_indx[i] );
2330 p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2332 for( unsigned i = 0; i < p_sys->i_track; i++ )
2334 avi_index_t *p_index = &p_sys->track[i]->idx;
2338 for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2339 b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2342 msg_Err( p_demux, "no key frame set for track %u", i );
2343 for( unsigned j = 0; j < p_index->i_size; j++ )
2344 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2348 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2349 i, p_index->i_size );
2353 static void AVI_IndexCreate( demux_t *p_demux )
2355 demux_sys_t *p_sys = p_demux->p_sys;
2357 avi_chunk_list_t *p_riff;
2358 avi_chunk_list_t *p_movi;
2360 unsigned int i_stream;
2363 mtime_t i_dialog_update;
2364 dialog_progress_bar_t *p_dialog = NULL;
2366 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2367 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2371 msg_Err( p_demux, "cannot find p_movi" );
2375 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2376 avi_index_Init( &p_sys->track[i_stream]->idx );
2378 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2379 stream_Size( p_demux->s ) );
2381 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2382 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2385 /* Only show dialog if AVI is > 10MB */
2386 i_dialog_update = mdate();
2387 if( stream_Size( p_demux->s ) > 10000000 )
2388 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2389 NULL, _("Cancel") );
2395 if( !vlc_object_alive (p_demux) )
2398 /* Don't update/check dialog too often */
2399 if( p_dialog && mdate() - i_dialog_update > 100000 )
2401 if( dialog_ProgressCancelled( p_dialog ) )
2404 double f_current = stream_Tell( p_demux->s );
2405 double f_size = stream_Size( p_demux->s );
2406 double f_pos = f_current / f_size;
2407 dialog_ProgressSet( p_dialog, NULL, f_pos );
2409 i_dialog_update = mdate();
2412 if( AVI_PacketGetHeader( p_demux, &pk ) )
2415 if( pk.i_stream < p_sys->i_track &&
2416 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2418 avi_track_t *tk = p_sys->track[pk.i_stream];
2421 index.i_id = pk.i_fourcc;
2422 index.i_flags = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2423 index.i_pos = pk.i_pos;
2424 index.i_length = pk.i_size;
2425 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2429 switch( pk.i_fourcc )
2431 case AVIFOURCC_idx1:
2434 avi_chunk_list_t *p_sysx;
2435 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2436 AVIFOURCC_RIFF, 1 );
2438 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2439 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2445 case AVIFOURCC_RIFF:
2446 msg_Dbg( p_demux, "new RIFF chunk found" );
2450 case AVIFOURCC_JUNK:
2454 msg_Warn( p_demux, "need resync, probably broken avi" );
2455 if( AVI_PacketSearch( p_demux ) )
2457 msg_Warn( p_demux, "lost sync, abord index creation" );
2463 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2464 AVI_PacketNext( p_demux ) )
2471 if( p_dialog != NULL )
2472 dialog_ProgressDestroy( p_dialog );
2474 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2476 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2477 i_stream, p_sys->track[i_stream]->idx.i_size );
2482 static void AVI_MetaLoad( demux_t *p_demux,
2483 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2485 demux_sys_t *p_sys = p_demux->p_sys;
2487 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2492 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2493 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2494 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2495 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2496 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2497 vlc_meta_SetSetting( p_meta, buffer );
2499 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2503 static const struct {
2507 { AVIFOURCC_IART, vlc_meta_Artist },
2508 { AVIFOURCC_ICMT, vlc_meta_Description },
2509 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2510 { AVIFOURCC_IGNR, vlc_meta_Genre },
2511 { AVIFOURCC_INAM, vlc_meta_Title },
2514 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2516 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2519 char *psz_value = FromACP( p_strz->p_str );
2524 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2529 /*****************************************************************************
2531 *****************************************************************************/
2532 static void AVI_ExtractSubtitle( demux_t *p_demux,
2533 unsigned int i_stream,
2534 avi_chunk_list_t *p_strl,
2535 avi_chunk_STRING_t *p_strn )
2537 demux_sys_t *p_sys = p_demux->p_sys;
2538 block_t *p_block = NULL;
2539 input_attachment_t *p_attachment = NULL;
2540 char *psz_description = NULL;
2541 avi_chunk_indx_t *p_indx = NULL;
2543 if( !p_sys->b_seekable )
2546 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2552 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2553 p_indx->i_entriesinuse > 0 )
2555 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2556 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2561 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2562 p_indx->i_entriesinuse != 1 ||
2563 p_indx->i_indexsubtype != 0 )
2566 i_position = p_indx->i_baseoffset +
2567 p_indx->idx.std[0].i_offset - 8;
2568 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2572 avi_chunk_idx1_t *p_idx1;
2575 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2579 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2581 const idx1_entry_t *e = &p_idx1->entry[i];
2583 unsigned i_stream_idx;
2585 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2586 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2588 i_position = e->i_pos + i_offset;
2589 i_size = e->i_length + 8;
2598 if( i_size > 1000000 )
2601 if( stream_Seek( p_demux->s, i_position ) )
2603 p_block = stream_Block( p_demux->s, i_size );
2607 /* Parse packet header */
2608 const uint8_t *p = p_block->p_buffer;
2609 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2614 /* Parse subtitle chunk header */
2615 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2616 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2618 const unsigned i_name = GetDWLE( &p[7] );
2619 if( 11 + i_size <= i_name )
2622 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2624 i_size -= 11 + i_name;
2625 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2627 const unsigned i_payload = GetDWLE( &p[2] );
2628 if( i_size < 6 + i_payload || i_payload <= 0 )
2633 if( !psz_description )
2634 psz_description = p_strn ? FromACP( p_strn->p_str ) : NULL;
2636 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2638 p_attachment = vlc_input_attachment_New( psz_name,
2639 "application/x-srt",
2643 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2647 free( psz_description );
2650 block_Release( p_block );
2653 msg_Dbg( p_demux, "Loaded an embed subtitle" );
2655 msg_Warn( p_demux, "Failed to load an embed subtitle" );
2657 if( p_indx == &ck.indx )
2658 AVI_ChunkFree( p_demux->s, &ck );
2660 /*****************************************************************************
2662 *****************************************************************************/
2663 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2665 demux_sys_t *p_sys = p_demux->p_sys;
2669 for( i = 0; i < p_sys->i_track; i++ )
2671 avi_track_t *tk = p_sys->track[i];
2672 if( tk->i_idxposc >= tk->idx.i_size )
2684 /****************************************************************************
2685 * AVI_MovieGetLength give max streams length in second
2686 ****************************************************************************/
2687 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2689 demux_sys_t *p_sys = p_demux->p_sys;
2690 mtime_t i_maxlength = 0;
2693 for( i = 0; i < p_sys->i_track; i++ )
2695 avi_track_t *tk = p_sys->track[i];
2698 /* fix length for each stream */
2699 if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2704 if( tk->i_samplesize )
2706 i_length = AVI_GetDPTS( tk,
2707 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2708 tk->idx.p_entry[tk->idx.i_size-1].i_length );
2712 i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2714 i_length /= (mtime_t)1000000; /* in seconds */
2717 "stream[%d] length:%"PRId64" (based on index)",
2720 i_maxlength = __MAX( i_maxlength, i_length );