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 *****************************************************************************/
31 #include <vlc_common.h>
32 #include <vlc_plugin.h>
33 #include <vlc_demux.h>
34 #include <vlc_input.h>
36 #include <vlc_dialog.h>
39 #include <vlc_codecs.h>
40 #include <vlc_charset.h>
41 #include <vlc_memory.h>
45 /*****************************************************************************
47 *****************************************************************************/
49 #define INTERLEAVE_TEXT N_("Force interleaved method" )
50 #define INTERLEAVE_LONGTEXT N_( "Force interleaved method." )
52 #define INDEX_TEXT N_("Force index creation")
53 #define INDEX_LONGTEXT N_( \
54 "Recreate a index for the AVI file. Use this if your AVI file is damaged "\
55 "or incomplete (not seekable)." )
57 static int Open ( vlc_object_t * );
58 static void Close( vlc_object_t * );
60 static const int pi_index[] = {0,1,2};
62 static const char *const ppsz_indexes[] = { N_("Ask for action"),
67 set_shortname( "AVI" )
68 set_description( N_("AVI demuxer") )
69 set_capability( "demux", 212 )
70 set_category( CAT_INPUT )
71 set_subcategory( SUBCAT_INPUT_DEMUX )
73 add_bool( "avi-interleaved", false, NULL,
74 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
75 add_integer( "avi-index", 0, NULL,
76 INDEX_TEXT, INDEX_LONGTEXT, false )
77 change_integer_list( pi_index, ppsz_indexes, NULL )
79 set_callbacks( Open, Close )
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Control ( demux_t *, int, va_list );
86 static int Seek ( demux_t *, mtime_t, int );
87 static int Demux_Seekable ( demux_t * );
88 static int Demux_UnSeekable( demux_t * );
90 #define __ABS( x ) ( (x) < 0 ? (-(x)) : (x) )
94 vlc_fourcc_t i_fourcc;
97 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
99 uint8_t i_peek[8]; /* first 8 bytes */
101 unsigned int i_stream;
112 int64_t i_lengthtotal;
121 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
122 vlc_fourcc_t i_codec;
126 unsigned int i_samplesize;
131 avi_entry_t *p_index;
132 unsigned int i_idxnb;
133 unsigned int i_idxmax;
135 unsigned int i_idxposc; /* numero of chunk */
136 unsigned int i_idxposb; /* byte in the current chunk */
138 /* For VBR audio only */
139 unsigned int i_blockno;
140 unsigned int i_blocksize;
142 /* For muxed streams */
143 stream_t *p_out_muxed;
158 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
160 /* number of streams and information */
161 unsigned int i_track;
167 unsigned int i_attachment;
168 input_attachment_t **attachment;
171 static inline off_t __EVEN( off_t i )
173 return (i & 1) ? i + 1 : i;
176 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
177 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
178 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
179 static mtime_t AVI_GetPTS ( avi_track_t * );
182 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
183 static int AVI_StreamChunkSet ( demux_t *,
184 unsigned int i_stream, unsigned int i_ck );
185 static int AVI_StreamBytesSet ( demux_t *,
186 unsigned int i_stream, off_t i_byte );
188 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
189 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
191 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
192 static int AVI_PacketNext ( demux_t * );
193 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
194 static int AVI_PacketSearch ( demux_t * );
196 static void AVI_IndexLoad ( demux_t * );
197 static void AVI_IndexCreate ( demux_t * );
198 static void AVI_IndexAddEntry( demux_sys_t *, int, avi_entry_t * );
200 static void AVI_ExtractSubtitle( demux_t *, int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
202 static mtime_t AVI_MovieGetLength( demux_t * );
204 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
206 /*****************************************************************************
208 *****************************************************************************/
209 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
210 static int AVI_TrackStopFinishedStreams( demux_t *);
213 - For VBR mp3 stream:
214 count blocks by rounded-up chunksizes instead of chunks
215 we need full emulation of dshow avi demuxer bugs :(
216 fixes silly nandub-style a-v delaying in avi with vbr mp3...
217 (from mplayer 2002/08/02)
221 /*****************************************************************************
222 * Open: check file and initializes AVI structures
223 *****************************************************************************/
224 static int Open( vlc_object_t * p_this )
226 demux_t *p_demux = (demux_t *)p_this;
229 bool b_index = false;
232 avi_chunk_list_t *p_riff;
233 avi_chunk_list_t *p_hdrl, *p_movi;
234 avi_chunk_avih_t *p_avih;
236 unsigned int i_track;
237 unsigned int i, i_peeker;
239 const uint8_t *p_peek;
241 /* Is it an avi file ? */
242 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
244 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
246 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
248 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
252 if( i_peeker == 188 )
257 /* Initialize input structures. */
258 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
259 memset( p_sys, 0, sizeof( demux_sys_t ) );
262 p_sys->i_movi_lastchunk_pos = 0;
263 p_sys->b_odml = false;
264 p_sys->b_muxed = false;
268 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
270 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
272 p_demux->pf_control = Control;
273 p_demux->pf_demux = Demux_Seekable;
275 /* For unseekable stream, automaticaly use Demux_UnSeekable */
276 if( !p_sys->b_seekable
277 || var_InheritInteger( p_demux, "avi-interleaved" ) )
279 p_demux->pf_demux = Demux_UnSeekable;
284 stream_Read( p_demux->s, NULL, i_peeker );
287 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
289 msg_Err( p_demux, "avi module discarded (invalid file)" );
294 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
296 unsigned int i_count =
297 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
299 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
300 for( i = 1; i < i_count; i++ )
302 avi_chunk_list_t *p_sysx;
304 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
305 if( p_sysx->i_type == AVIFOURCC_AVIX )
307 msg_Warn( p_demux, "detected OpenDML file" );
308 p_sys->b_odml = true;
314 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
315 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
316 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
318 if( !p_hdrl || !p_movi )
320 msg_Err( p_demux, "avi module discarded (invalid file)" );
324 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
326 msg_Err( p_demux, "cannot find avih chunk" );
329 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
330 if( p_avih->i_streams != i_track )
333 "found %d stream but %d are declared",
334 i_track, p_avih->i_streams );
338 msg_Err( p_demux, "no stream defined!" );
342 /* print information on streams */
343 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
345 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
346 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
347 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
348 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
350 AVI_MetaLoad( p_demux, p_riff, p_avih );
352 /* now read info on each stream and create ES */
353 for( i = 0 ; i < i_track; i++ )
355 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
359 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
360 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
361 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
362 avi_chunk_strf_auds_t *p_auds = NULL;
363 avi_chunk_strf_vids_t *p_vids = NULL;
366 memset( tk, 0, sizeof(*tk) );
368 tk->b_activated = true;
370 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
371 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
373 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
375 msg_Warn( p_demux, "stream[%d] incomplete", i );
380 tk->i_rate = p_strh->i_rate;
381 tk->i_scale = p_strh->i_scale;
382 tk->i_samplesize = p_strh->i_samplesize;
383 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
384 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
386 switch( p_strh->i_type )
388 case( AVIFOURCC_auds ):
389 tk->i_cat = AUDIO_ES;
390 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
391 p_auds->p_wf->wFormatTag );
393 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
394 if( tk->i_blocksize == 0 )
396 if( p_auds->p_wf->wFormatTag == 1 )
397 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
401 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
403 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
404 "Using blocksize as a workaround.",
405 i, tk->i_samplesize, tk->i_blocksize );
406 tk->i_samplesize = tk->i_blocksize;
409 if( tk->i_codec == VLC_CODEC_VORBIS )
411 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
414 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
416 fmt.audio.i_channels = p_auds->p_wf->nChannels;
417 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
418 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
419 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
420 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
421 fmt.b_packetized = !tk->i_blocksize;
424 "stream[%d] audio(0x%x) %d channels %dHz %dbits",
425 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
426 p_auds->p_wf->nSamplesPerSec,
427 p_auds->p_wf->wBitsPerSample );
429 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
430 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
431 fmt.p_extra = malloc( fmt.i_extra );
432 if( !fmt.p_extra ) goto error;
433 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
436 case( AVIFOURCC_vids ):
437 tk->i_cat = VIDEO_ES;
438 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
439 p_vids->p_bih->biCompression );
440 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
442 msg_Dbg( p_demux, "stream[%d] subtitles", i );
443 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
446 else if( p_vids->p_bih->biCompression == 0x00 )
448 switch( p_vids->p_bih->biBitCount )
451 tk->i_codec = VLC_CODEC_RGB32;
454 tk->i_codec = VLC_CODEC_RGB24;
456 case 16: /* Yes it is RV15 */
458 tk->i_codec = VLC_CODEC_RGB15;
460 case 9: /* <- TODO check that */
461 tk->i_codec = VLC_CODEC_I410;
463 case 8: /* <- TODO check that */
464 tk->i_codec = VLC_CODEC_GREY;
467 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
469 switch( tk->i_codec )
471 case VLC_CODEC_RGB24:
472 case VLC_CODEC_RGB32:
473 fmt.video.i_rmask = 0x00ff0000;
474 fmt.video.i_gmask = 0x0000ff00;
475 fmt.video.i_bmask = 0x000000ff;
477 case VLC_CODEC_RGB15:
478 fmt.video.i_rmask = 0x7c00;
479 fmt.video.i_gmask = 0x03e0;
480 fmt.video.i_bmask = 0x001f;
488 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
489 if( tk->i_codec == VLC_CODEC_MP4V &&
490 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
493 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
496 tk->i_samplesize = 0;
497 fmt.video.i_width = p_vids->p_bih->biWidth;
498 fmt.video.i_height = p_vids->p_bih->biHeight;
499 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
500 fmt.video.i_frame_rate = tk->i_rate;
501 fmt.video.i_frame_rate_base = tk->i_scale;
503 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
504 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
505 fmt.p_extra = malloc( fmt.i_extra );
506 if( !fmt.p_extra ) goto error;
507 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
509 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
510 i, (char*)&p_vids->p_bih->biCompression,
511 (uint32_t)p_vids->p_bih->biWidth,
512 (uint32_t)p_vids->p_bih->biHeight,
513 p_vids->p_bih->biBitCount,
514 (float)tk->i_rate/(float)tk->i_scale );
516 if( p_vids->p_bih->biCompression == 0x00 )
518 /* RGB DIB are coded from bottom to top */
520 (unsigned int)(-(int)p_vids->p_bih->biHeight);
523 /* Extract palette from extradata if bpp <= 8
524 * (assumes that extradata contains only palette but appears
525 * to be true for all palettized codecs we support) */
526 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
528 /* The palette is not always included in biSize */
529 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
530 if( fmt.i_extra > 0 )
532 const uint8_t *p_pal = fmt.p_extra;
534 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
535 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
537 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
539 for( int j = 0; j < 4; j++ )
540 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
546 case( AVIFOURCC_txts):
547 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
548 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
552 case( AVIFOURCC_iavs):
553 case( AVIFOURCC_ivas):
554 p_sys->b_muxed = true;
555 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
556 if( p_strh->i_handler == FOURCC_dvsd ||
557 p_strh->i_handler == FOURCC_dvhd ||
558 p_strh->i_handler == FOURCC_dvsl ||
559 p_strh->i_handler == FOURCC_dv25 ||
560 p_strh->i_handler == FOURCC_dv50 )
562 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
563 if( !tk->p_out_muxed )
564 msg_Err( p_demux, "could not load the DV parser" );
570 case( AVIFOURCC_mids):
571 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
574 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
579 fmt.psz_description = FromLatin1( p_strn->p_str );
580 if( tk->p_out_muxed == NULL )
581 tk->p_es = es_out_Add( p_demux->out, &fmt );
582 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
583 es_format_Clean( &fmt );
586 if( p_sys->i_track <= 0 )
588 msg_Err( p_demux, "no valid track" );
592 i_do_index = var_InheritInteger( p_demux, "avi-index" );
593 if( i_do_index == 1 ) /* Always fix */
596 if( p_sys->b_seekable )
598 AVI_IndexCreate( p_demux );
602 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
603 AVI_IndexLoad( p_demux );
608 AVI_IndexLoad( p_demux );
611 /* *** movie length in sec *** */
612 p_sys->i_length = AVI_MovieGetLength( p_demux );
614 /* Check the index completeness */
615 unsigned int i_idx_totalframes = 0;
616 for( unsigned int i = 0; i < p_sys->i_track; i++ )
618 const avi_track_t *tk = p_sys->track[i];
619 if( tk->i_cat == VIDEO_ES && tk->p_index )
620 i_idx_totalframes = __MAX(i_idx_totalframes, tk->i_idxnb);
623 if( i_idx_totalframes != p_avih->i_totalframes &&
624 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
625 (mtime_t)p_avih->i_microsecperframe /
628 if( !vlc_object_alive( p_demux) )
631 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
632 "approximative or will exhibit strange behavior" );
633 if( i_do_index == 0 && !b_index )
635 if( !p_sys->b_seekable ) {
639 switch( dialog_Question( p_demux, _("AVI Index") ,
640 _( "This AVI file is broken. Seeking will not work correctly.\n"
641 "Do you want to try to fix it?\n\n"
642 "This might take a long time." ),
643 _( "Repair" ), _( "Don't repair" ), _( "Cancel") ) )
647 msg_Dbg( p_demux, "Fixing AVI index" );
651 vlc_object_kill( p_demux->p_parent );
657 /* fix some BeOS MediaKit generated file */
658 for( i = 0 ; i < p_sys->i_track; i++ )
660 avi_track_t *tk = p_sys->track[i];
661 avi_chunk_list_t *p_strl;
662 avi_chunk_strh_t *p_strh;
663 avi_chunk_strf_auds_t *p_auds;
665 if( tk->i_cat != AUDIO_ES )
669 if( tk->i_idxnb < 1 ||
671 tk->i_samplesize != 0 )
675 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
676 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
677 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
679 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
680 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
682 int64_t i_track_length =
683 tk->p_index[tk->i_idxnb-1].i_length +
684 tk->p_index[tk->i_idxnb-1].i_lengthtotal;
685 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
686 (mtime_t)p_avih->i_microsecperframe;
690 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
693 tk->i_samplesize = 1;
694 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
695 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
699 if( p_sys->b_seekable )
701 /* we have read all chunk so go back to movi */
702 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
704 /* Skip movi header */
705 stream_Read( p_demux->s, NULL, 12 );
707 p_sys->i_movi_begin = p_movi->i_chunk_pos;
711 for( unsigned i = 0; i < p_sys->i_attachment; i++)
712 vlc_input_attachment_Delete(p_sys->attachment[i]);
713 free(p_sys->attachment);
716 vlc_meta_Delete( p_sys->meta );
718 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
720 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
723 /*****************************************************************************
724 * Close: frees unused data
725 *****************************************************************************/
726 static void Close ( vlc_object_t * p_this )
728 demux_t * p_demux = (demux_t *)p_this;
730 demux_sys_t *p_sys = p_demux->p_sys ;
732 for( i = 0; i < p_sys->i_track; i++ )
734 if( p_sys->track[i] )
736 if( p_sys->track[i]->p_out_muxed )
737 stream_Delete( p_sys->track[i]->p_out_muxed );
738 free( p_sys->track[i]->p_index );
739 free( p_sys->track[i] );
742 free( p_sys->track );
743 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
744 vlc_meta_Delete( p_sys->meta );
745 for( unsigned i = 0; i < p_sys->i_attachment; i++)
746 vlc_input_attachment_Delete(p_sys->attachment[i]);
747 free(p_sys->attachment);
752 /*****************************************************************************
753 * Demux_Seekable: reads and demuxes data packets for stream seekable
754 *****************************************************************************
755 * AVIDemux: reads and demuxes data packets
756 *****************************************************************************
757 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
758 *****************************************************************************/
765 off_t i_posf; /* where we will read :
766 if i_idxposb == 0 : begining of chunk (+8 to acces data)
767 else : point on data directly */
768 } avi_track_toread_t;
770 static int Demux_Seekable( demux_t *p_demux )
772 demux_sys_t *p_sys = p_demux->p_sys;
774 unsigned int i_track_count = 0;
775 unsigned int i_track;
776 /* cannot be more than 100 stream (dcXX or wbXX) */
777 avi_track_toread_t toread[100];
780 /* detect new selected/unselected streams */
781 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
783 avi_track_t *tk = p_sys->track[i_track];
786 if( p_sys->b_muxed && tk->p_out_muxed )
789 tk->b_activated = true;
793 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
794 if( b && !tk->b_activated )
796 if( p_sys->b_seekable)
798 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
800 tk->b_activated = true;
802 else if( !b && tk->b_activated )
804 tk->b_activated = false;
812 if( i_track_count <= 0 )
814 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
816 p_sys->i_time += 25*1000; /* read 25ms */
819 if( p_sys->i_time >= i_length )
823 msg_Warn( p_demux, "no track selected, exiting..." );
827 /* wait for the good time */
828 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
829 p_sys->i_time += 25*1000; /* read 25ms */
832 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
834 avi_track_t *tk = p_sys->track[i_track];
837 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
838 if( tk->i_idxposc < tk->i_idxnb )
840 toread[i_track].i_posf = tk->p_index[tk->i_idxposc].i_pos;
841 if( tk->i_idxposb > 0 )
843 toread[i_track].i_posf += 8 + tk->i_idxposb;
848 toread[i_track].i_posf = -1;
851 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
853 if( tk->i_samplesize )
855 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
859 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
864 toread[i_track].i_toread *= -1;
877 /* search for first chunk to be read */
878 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
880 if( !toread[i].b_ok ||
881 AVI_GetDPTS( p_sys->track[i],
882 toread[i].i_toread ) <= -25 * 1000 )
887 if( toread[i].i_toread > 0 )
889 b_done = false; /* not yet finished */
891 if( toread[i].i_posf > 0 )
893 if( i_pos == -1 || i_pos > toread[i].i_posf )
896 i_pos = toread[i].i_posf;
903 for( i = 0; i < p_sys->i_track; i++ )
908 msg_Warn( p_demux, "all tracks have failed, exiting..." );
914 int i_loop_count = 0;
916 /* no valid index, we will parse directly the stream
917 * in case we fail we will disable all finished stream */
918 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
920 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
921 if( AVI_PacketNext( p_demux ) )
923 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
928 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
935 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
938 "cannot get packet header, track disabled" );
939 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
941 if( avi_pk.i_stream >= p_sys->i_track ||
942 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
944 if( AVI_PacketNext( p_demux ) )
947 "cannot skip packet, track disabled" );
948 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
951 /* Prevents from eating all the CPU with broken files.
952 * This value should be low enough so that it doesn't
953 * affect the reading speed too much. */
954 if( !(++i_loop_count % 1024) )
956 if( !vlc_object_alive (p_demux) ) return -1;
959 if( !(i_loop_count % (1024 * 10)) )
961 "don't seem to find any data..." );
967 /* add this chunk to the index */
970 index.i_id = avi_pk.i_fourcc;
972 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
974 index.i_pos = avi_pk.i_pos;
975 index.i_length = avi_pk.i_size;
976 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
978 i_track = avi_pk.i_stream;
979 tk = p_sys->track[i_track];
980 /* do we will read this data ? */
981 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
987 if( AVI_PacketNext( p_demux ) )
990 "cannot skip packet, track disabled" );
991 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1000 stream_Seek( p_demux->s, i_pos );
1003 /* Set the track to use */
1004 tk = p_sys->track[i_track];
1006 /* read thoses data */
1007 if( tk->i_samplesize )
1009 unsigned int i_toread;
1011 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1013 if( tk->i_samplesize > 1 )
1015 i_toread = tk->i_samplesize;
1019 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1020 i_toread = __MAX( i_toread, 100 );
1023 i_size = __MIN( tk->p_index[tk->i_idxposc].i_length -
1029 i_size = tk->p_index[tk->i_idxposc].i_length;
1032 if( tk->i_idxposb == 0 )
1034 i_size += 8; /* need to read and skip header */
1037 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1039 msg_Warn( p_demux, "failed reading data" );
1041 toread[i_track].b_ok = false;
1044 if( i_size % 2 ) /* read was padded on word boundary */
1046 p_frame->i_buffer--;
1049 if( tk->i_idxposb == 0 )
1051 p_frame->p_buffer += 8;
1052 p_frame->i_buffer -= 8;
1054 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1055 if( tk->p_index[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1057 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1061 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1065 if( tk->i_samplesize )
1067 if( tk->i_idxposb == 0 )
1071 toread[i_track].i_toread -= i_size;
1072 tk->i_idxposb += i_size;
1073 if( tk->i_idxposb >=
1074 tk->p_index[tk->i_idxposc].i_length )
1082 int i_length = tk->p_index[tk->i_idxposc].i_length;
1085 if( tk->i_cat == AUDIO_ES )
1087 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1089 toread[i_track].i_toread--;
1092 if( tk->i_idxposc < tk->i_idxnb)
1094 toread[i_track].i_posf =
1095 tk->p_index[tk->i_idxposc].i_pos;
1096 if( tk->i_idxposb > 0 )
1098 toread[i_track].i_posf += 8 + tk->i_idxposb;
1104 toread[i_track].i_posf = -1;
1107 if( tk->i_cat != VIDEO_ES )
1108 p_frame->i_dts = p_frame->i_pts;
1111 p_frame->i_dts = p_frame->i_pts;
1112 p_frame->i_pts = VLC_TS_INVALID;
1115 //p_pes->i_rate = p_demux->stream.control.i_rate;
1116 if( tk->p_out_muxed )
1117 stream_DemuxSend( tk->p_out_muxed, p_frame );
1119 es_out_Send( p_demux->out, tk->p_es, p_frame );
1124 /*****************************************************************************
1125 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1126 *****************************************************************************
1127 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1128 *****************************************************************************/
1129 static int Demux_UnSeekable( demux_t *p_demux )
1131 demux_sys_t *p_sys = p_demux->p_sys;
1132 avi_track_t *p_stream_master = NULL;
1133 unsigned int i_stream;
1134 unsigned int i_packet;
1136 if( p_sys->b_muxed )
1138 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1139 return VLC_EGENERIC;
1142 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1144 /* *** find master stream for data packet skipping algo *** */
1145 /* *** -> first video, if any, or first audio ES *** */
1146 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1148 avi_track_t *tk = p_sys->track[i_stream];
1151 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1153 if( b && tk->i_cat == VIDEO_ES )
1155 p_stream_master = tk;
1159 p_stream_master = tk;
1163 if( !p_stream_master )
1165 msg_Warn( p_demux, "no more stream selected" );
1169 p_sys->i_time = AVI_GetPTS( p_stream_master );
1171 for( i_packet = 0; i_packet < 10; i_packet++)
1173 #define p_stream p_sys->track[avi_pk.i_stream]
1175 avi_packet_t avi_pk;
1177 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1182 if( avi_pk.i_stream >= p_sys->i_track ||
1183 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1185 /* we haven't found an audio or video packet:
1186 * - we have seek, found first next packet
1187 * - others packets could be found, skip them
1189 switch( avi_pk.i_fourcc )
1191 case AVIFOURCC_JUNK:
1192 case AVIFOURCC_LIST:
1193 case AVIFOURCC_RIFF:
1194 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1195 case AVIFOURCC_idx1:
1198 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1200 return( 0 ); /* eof */
1203 "seems to have lost position, resync" );
1204 if( AVI_PacketSearch( p_demux ) )
1206 msg_Err( p_demux, "resync failed" );
1213 /* check for time */
1214 if( __ABS( AVI_GetPTS( p_stream ) -
1215 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1217 /* load it and send to decoder */
1219 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1223 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1225 if( avi_pk.i_cat != VIDEO_ES )
1226 p_frame->i_dts = p_frame->i_pts;
1229 p_frame->i_dts = p_frame->i_pts;
1230 p_frame->i_pts = VLC_TS_INVALID;
1233 //p_pes->i_rate = p_demux->stream.control.i_rate;
1234 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1238 if( AVI_PacketNext( p_demux ) )
1244 /* *** update stream time position *** */
1245 if( p_stream->i_samplesize )
1247 p_stream->i_idxposb += avi_pk.i_size;
1251 if( p_stream->i_cat == AUDIO_ES )
1253 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1255 p_stream->i_idxposc++;
1265 /*****************************************************************************
1266 * Seek: goto to i_date or i_percent
1267 *****************************************************************************/
1268 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1271 demux_sys_t *p_sys = p_demux->p_sys;
1272 unsigned int i_stream;
1273 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1274 i_date / 1000000, i_percent );
1276 if( p_sys->b_seekable )
1278 if( !p_sys->i_length )
1280 avi_track_t *p_stream;
1283 /* use i_percent to create a true i_date */
1284 msg_Warn( p_demux, "seeking without index at %d%%"
1285 " only works for interleaved files", i_percent );
1286 if( i_percent >= 100 )
1288 msg_Warn( p_demux, "cannot seek so far !" );
1289 return VLC_EGENERIC;
1291 i_percent = __MAX( i_percent, 0 );
1293 /* try to find chunk that is at i_percent or the file */
1294 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1295 p_sys->i_movi_begin );
1296 /* search first selected stream (and prefer non eof ones) */
1297 for( i_stream = 0, p_stream = NULL;
1298 i_stream < p_sys->i_track; i_stream++ )
1300 if( !p_stream || p_stream->b_eof )
1301 p_stream = p_sys->track[i_stream];
1303 if( p_stream->b_activated && !p_stream->b_eof )
1306 if( !p_stream || !p_stream->b_activated )
1308 msg_Warn( p_demux, "cannot find any selected stream" );
1309 return VLC_EGENERIC;
1312 /* be sure that the index exist */
1313 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1315 msg_Warn( p_demux, "cannot seek" );
1316 return VLC_EGENERIC;
1319 while( i_pos >= p_stream->p_index[p_stream->i_idxposc].i_pos +
1320 p_stream->p_index[p_stream->i_idxposc].i_length + 8 )
1322 /* search after i_idxposc */
1323 if( AVI_StreamChunkSet( p_demux,
1324 i_stream, p_stream->i_idxposc + 1 ) )
1326 msg_Warn( p_demux, "cannot seek" );
1327 return VLC_EGENERIC;
1331 i_date = AVI_GetPTS( p_stream );
1332 /* TODO better support for i_samplesize != 0 */
1333 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1337 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1339 avi_track_t *p_stream = p_sys->track[i_stream];
1341 if( !p_stream->b_activated )
1344 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1346 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1347 p_sys->i_time = i_date;
1348 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1353 msg_Err( p_demux, "shouldn't yet be executed" );
1354 return VLC_EGENERIC;
1358 /*****************************************************************************
1360 *****************************************************************************/
1361 static double ControlGetPosition( demux_t *p_demux )
1363 demux_sys_t *p_sys = p_demux->p_sys;
1365 if( p_sys->i_length > 0 )
1367 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1369 else if( stream_Size( p_demux->s ) > 0 )
1375 /* search the more advanced selected es */
1376 for( i = 0; i < p_sys->i_track; i++ )
1378 avi_track_t *tk = p_sys->track[i];
1379 if( tk->b_activated && tk->i_idxposc < tk->i_idxnb )
1381 i_tmp = tk->p_index[tk->i_idxposc].i_pos +
1382 tk->p_index[tk->i_idxposc].i_length + 8;
1389 return (double)i64 / stream_Size( p_demux->s );
1394 static int Control( demux_t *p_demux, int i_query, va_list args )
1396 demux_sys_t *p_sys = p_demux->p_sys;
1404 case DEMUX_GET_POSITION:
1405 pf = (double*)va_arg( args, double * );
1406 *pf = ControlGetPosition( p_demux );
1408 case DEMUX_SET_POSITION:
1409 f = (double)va_arg( args, double );
1410 if( p_sys->b_seekable )
1412 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1413 return Seek( p_demux, i64, (int)(f * 100) );
1417 int64_t i_pos = stream_Size( p_demux->s ) * f;
1418 return stream_Seek( p_demux->s, i_pos );
1421 case DEMUX_GET_TIME:
1422 pi64 = (int64_t*)va_arg( args, int64_t * );
1423 *pi64 = p_sys->i_time;
1426 case DEMUX_SET_TIME:
1430 i64 = (int64_t)va_arg( args, int64_t );
1431 if( p_sys->i_length > 0 )
1433 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1435 else if( p_sys->i_time > 0 )
1437 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1438 (double)i64 / (double)p_sys->i_time );
1440 return Seek( p_demux, i64, i_percent );
1442 case DEMUX_GET_LENGTH:
1443 pi64 = (int64_t*)va_arg( args, int64_t * );
1444 *pi64 = p_sys->i_length * (mtime_t)1000000;
1448 pf = (double*)va_arg( args, double * );
1450 for( i = 0; i < (int)p_sys->i_track; i++ )
1452 avi_track_t *tk = p_sys->track[i];
1453 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1455 *pf = (float)tk->i_rate / (float)tk->i_scale;
1461 case DEMUX_GET_META:
1462 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1463 vlc_meta_Merge( p_meta, p_sys->meta );
1466 case DEMUX_GET_ATTACHMENTS:
1468 if( p_sys->i_attachment <= 0 )
1469 return VLC_EGENERIC;
1471 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1472 int *pi_int = va_arg( args, int * );
1474 *pi_int = p_sys->i_attachment;
1475 *ppp_attach = calloc( p_sys->i_attachment, sizeof(*ppp_attach));
1476 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1477 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1482 return VLC_EGENERIC;
1486 /*****************************************************************************
1487 * Function to convert pts to chunk or byte
1488 *****************************************************************************/
1490 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1495 return (mtime_t)((int64_t)i_pts *
1496 (int64_t)tk->i_rate /
1497 (int64_t)tk->i_scale /
1500 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1502 if( !tk->i_scale || !tk->i_samplesize )
1505 return (mtime_t)((int64_t)i_pts *
1506 (int64_t)tk->i_rate /
1507 (int64_t)tk->i_scale /
1509 (int64_t)tk->i_samplesize );
1512 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1519 i_dpts = (mtime_t)( (int64_t)1000000 *
1521 (int64_t)tk->i_scale /
1522 (int64_t)tk->i_rate );
1524 if( tk->i_samplesize )
1526 return i_dpts / tk->i_samplesize;
1531 static mtime_t AVI_GetPTS( avi_track_t *tk )
1533 if( tk->i_samplesize )
1535 int64_t i_count = 0;
1537 /* we need a valid entry we will emulate one */
1538 if( tk->i_idxposc == tk->i_idxnb )
1542 /* use the last entry */
1543 i_count = tk->p_index[tk->i_idxnb - 1].i_lengthtotal
1544 + tk->p_index[tk->i_idxnb - 1].i_length;
1549 i_count = tk->p_index[tk->i_idxposc].i_lengthtotal;
1551 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1555 if( tk->i_cat == AUDIO_ES )
1557 return AVI_GetDPTS( tk, tk->i_blockno );
1561 return AVI_GetDPTS( tk, tk->i_idxposc );
1566 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1568 demux_sys_t *p_sys = p_demux->p_sys;
1569 avi_packet_t avi_pk;
1570 int i_loop_count = 0;
1572 /* find first chunk of i_stream that isn't in index */
1574 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1576 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1577 if( AVI_PacketNext( p_demux ) )
1579 return VLC_EGENERIC;
1584 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1589 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1591 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1593 msg_Warn( p_demux, "cannot get packet header" );
1594 return VLC_EGENERIC;
1596 if( avi_pk.i_stream >= p_sys->i_track ||
1597 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1599 if( AVI_PacketNext( p_demux ) )
1601 return VLC_EGENERIC;
1604 /* Prevents from eating all the CPU with broken files.
1605 * This value should be low enough so that it doesn't
1606 * affect the reading speed too much. */
1607 if( !(++i_loop_count % 1024) )
1609 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1612 if( !(i_loop_count % (1024 * 10)) )
1613 msg_Warn( p_demux, "don't seem to find any data..." );
1618 /* add this chunk to the index */
1621 index.i_id = avi_pk.i_fourcc;
1623 AVI_GetKeyFlag(p_sys->track[avi_pk.i_stream]->i_codec,
1625 index.i_pos = avi_pk.i_pos;
1626 index.i_length = avi_pk.i_size;
1627 AVI_IndexAddEntry( p_sys, avi_pk.i_stream, &index );
1629 if( avi_pk.i_stream == i_stream )
1634 if( AVI_PacketNext( p_demux ) )
1636 return VLC_EGENERIC;
1642 /* be sure that i_ck will be a valid index entry */
1643 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1646 demux_sys_t *p_sys = p_demux->p_sys;
1647 avi_track_t *p_stream = p_sys->track[i_stream];
1649 p_stream->i_idxposc = i_ck;
1650 p_stream->i_idxposb = 0;
1652 if( i_ck >= p_stream->i_idxnb )
1654 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1657 p_stream->i_idxposc++;
1658 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1660 return VLC_EGENERIC;
1663 } while( p_stream->i_idxposc < i_ck );
1669 /* XXX FIXME up to now, we assume that all chunk are one after one */
1670 static int AVI_StreamBytesSet( demux_t *p_demux,
1671 unsigned int i_stream,
1674 demux_sys_t *p_sys = p_demux->p_sys;
1675 avi_track_t *p_stream = p_sys->track[i_stream];
1677 if( ( p_stream->i_idxnb > 0 )
1678 &&( i_byte < p_stream->p_index[p_stream->i_idxnb - 1].i_lengthtotal +
1679 p_stream->p_index[p_stream->i_idxnb - 1].i_length ) )
1681 /* index is valid to find the ck */
1682 /* uses dichototmie to be fast enougth */
1683 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->i_idxnb - 1 );
1684 int i_idxmax = p_stream->i_idxnb;
1688 if( p_stream->p_index[i_idxposc].i_lengthtotal > i_byte )
1690 i_idxmax = i_idxposc ;
1691 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1695 if( p_stream->p_index[i_idxposc].i_lengthtotal +
1696 p_stream->p_index[i_idxposc].i_length <= i_byte)
1698 i_idxmin = i_idxposc ;
1699 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1703 p_stream->i_idxposc = i_idxposc;
1704 p_stream->i_idxposb = i_byte -
1705 p_stream->p_index[i_idxposc].i_lengthtotal;
1714 p_stream->i_idxposc = p_stream->i_idxnb - 1;
1715 p_stream->i_idxposb = 0;
1718 p_stream->i_idxposc++;
1719 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1721 return VLC_EGENERIC;
1724 } while( p_stream->p_index[p_stream->i_idxposc].i_lengthtotal +
1725 p_stream->p_index[p_stream->i_idxposc].i_length <= i_byte );
1727 p_stream->i_idxposb = i_byte -
1728 p_stream->p_index[p_stream->i_idxposc].i_lengthtotal;
1733 static int AVI_TrackSeek( demux_t *p_demux,
1737 demux_sys_t *p_sys = p_demux->p_sys;
1738 avi_track_t *tk = p_sys->track[i_stream];
1740 #define p_stream p_sys->track[i_stream]
1743 i_oldpts = AVI_GetPTS( p_stream );
1745 if( !p_stream->i_samplesize )
1747 if( AVI_StreamChunkSet( p_demux,
1749 AVI_PTSToChunk( p_stream, i_date ) ) )
1751 return VLC_EGENERIC;
1754 if( p_stream->i_cat == AUDIO_ES )
1758 for( i = 0; i < tk->i_idxposc; i++ )
1760 if( tk->i_blocksize > 0 )
1762 tk->i_blockno += ( tk->p_index[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1772 "old:%"PRId64" %s new %"PRId64,
1774 i_oldpts > i_date ? ">" : "<",
1777 if( p_stream->i_cat == VIDEO_ES )
1779 /* search key frame */
1780 //if( i_date < i_oldpts || 1 )
1782 while( p_stream->i_idxposc > 0 &&
1783 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1786 if( AVI_StreamChunkSet( p_demux,
1788 p_stream->i_idxposc - 1 ) )
1790 return VLC_EGENERIC;
1797 while( p_stream->i_idxposc < p_stream->i_idxnb &&
1798 !( p_stream->p_index[p_stream->i_idxposc].i_flags &
1801 if( AVI_StreamChunkSet( p_demux,
1803 p_stream->i_idxposc + 1 ) )
1805 return VLC_EGENERIC;
1814 if( AVI_StreamBytesSet( p_demux,
1816 AVI_PTSToByte( p_stream, i_date ) ) )
1818 return VLC_EGENERIC;
1825 /****************************************************************************
1826 * Return true if it's a key frame
1827 ****************************************************************************/
1828 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1832 case VLC_CODEC_DIV1:
1834 * startcode: 0x00000100 32bits
1835 * framenumber ? 5bits
1836 * piture type 0(I),1(P) 2bits
1838 if( GetDWBE( p_byte ) != 0x00000100 )
1840 /* it's not an msmpegv1 stream, strange...*/
1841 return AVIIF_KEYFRAME;
1843 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1845 case VLC_CODEC_DIV2:
1846 case VLC_CODEC_DIV3:
1847 case VLC_CODEC_WMV1:
1849 * picture type 0(I),1(P) 2bits
1851 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1852 case VLC_CODEC_MP4V:
1853 /* we should find first occurrence of 0x000001b6 (32bits)
1854 * startcode: 0x000001b6 32bits
1855 * piture type 0(I),1(P) 2bits
1857 if( GetDWBE( p_byte ) != 0x000001b6 )
1859 /* not true , need to find the first VOP header */
1860 return AVIIF_KEYFRAME;
1862 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1865 /* I can't do it, so say yes */
1866 return AVIIF_KEYFRAME;
1870 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1875 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1878 return vlc_fourcc_GetCodec( i_cat, i_codec );
1880 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1884 /****************************************************************************
1886 ****************************************************************************/
1887 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1888 unsigned int *pi_number, unsigned int *pi_type )
1890 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1893 c1 = ((uint8_t *)&i_id)[0];
1894 c2 = ((uint8_t *)&i_id)[1];
1896 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1898 SET_PTR( pi_number, 100 ); /* > max stream number */
1899 SET_PTR( pi_type, UNKNOWN_ES );
1903 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1904 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1907 SET_PTR( pi_type, AUDIO_ES );
1912 SET_PTR( pi_type, VIDEO_ES );
1915 SET_PTR( pi_type, SPU_ES );
1918 SET_PTR( pi_type, UNKNOWN_ES );
1925 /****************************************************************************
1927 ****************************************************************************/
1928 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1930 const uint8_t *p_peek;
1932 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1934 return VLC_EGENERIC;
1936 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1937 p_pk->i_size = GetDWLE( p_peek + 4 );
1938 p_pk->i_pos = stream_Tell( p_demux->s );
1939 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1941 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1942 p_peek[10], p_peek[11] );
1949 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1951 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1955 static int AVI_PacketNext( demux_t *p_demux )
1957 avi_packet_t avi_ck;
1960 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1962 return VLC_EGENERIC;
1965 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1966 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1970 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1971 avi_ck.i_type == AVIFOURCC_AVIX )
1977 i_skip = __EVEN( avi_ck.i_size ) + 8;
1980 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1982 return VLC_EGENERIC;
1987 static int AVI_PacketRead( demux_t *p_demux,
1989 block_t **pp_frame )
1993 i_size = __EVEN( p_pk->i_size + 8 );
1995 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
1997 return VLC_EGENERIC;
1999 (*pp_frame)->p_buffer += 8;
2000 (*pp_frame)->i_buffer -= 8;
2002 if( i_size != p_pk->i_size + 8 )
2004 (*pp_frame)->i_buffer--;
2010 static int AVI_PacketSearch( demux_t *p_demux )
2012 demux_sys_t *p_sys = p_demux->p_sys;
2013 avi_packet_t avi_pk;
2018 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2020 return VLC_EGENERIC;
2022 AVI_PacketGetHeader( p_demux, &avi_pk );
2023 if( avi_pk.i_stream < p_sys->i_track &&
2024 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2028 switch( avi_pk.i_fourcc )
2030 case AVIFOURCC_JUNK:
2031 case AVIFOURCC_LIST:
2032 case AVIFOURCC_RIFF:
2033 case AVIFOURCC_idx1:
2037 /* Prevents from eating all the CPU with broken files.
2038 * This value should be low enough so that it doesn't affect the
2039 * reading speed too much (not that we care much anyway because
2040 * this code is called only on broken files). */
2041 if( !(++i_count % 1024) )
2043 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2046 if( !(i_count % (1024 * 10)) )
2047 msg_Warn( p_demux, "trying to resync..." );
2052 /****************************************************************************
2054 ****************************************************************************/
2055 static void AVI_IndexAddEntry( demux_sys_t *p_sys,
2057 avi_entry_t *p_index)
2059 avi_track_t *tk = p_sys->track[i_stream];
2061 /* Update i_movi_lastchunk_pos */
2062 if( p_sys->i_movi_lastchunk_pos < p_index->i_pos )
2064 p_sys->i_movi_lastchunk_pos = p_index->i_pos;
2068 if( tk->i_idxnb >= tk->i_idxmax )
2070 tk->i_idxmax += 16384;
2071 tk->p_index = realloc_or_free( tk->p_index,
2072 tk->i_idxmax * sizeof( avi_entry_t ) );
2073 if( tk->p_index == NULL )
2078 /* calculate cumulate length */
2079 if( tk->i_idxnb > 0 )
2081 p_index->i_lengthtotal =
2082 tk->p_index[tk->i_idxnb - 1].i_length +
2083 tk->p_index[tk->i_idxnb - 1].i_lengthtotal;
2087 p_index->i_lengthtotal = 0;
2090 tk->p_index[tk->i_idxnb++] = *p_index;
2093 static int AVI_IndexFind_idx1( demux_t *p_demux,
2094 avi_chunk_idx1_t **pp_idx1,
2095 uint64_t *pi_offset )
2097 demux_sys_t *p_sys = p_demux->p_sys;
2099 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2100 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2104 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2105 return VLC_EGENERIC;
2109 /* *** calculate offset *** */
2110 /* Well, avi is __SHIT__ so test more than one entry
2111 * (needed for some avi files) */
2112 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2114 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2116 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2118 *pi_offset = p_movi->i_chunk_pos + 8;
2125 static int AVI_IndexLoad_idx1( demux_t *p_demux )
2127 demux_sys_t *p_sys = p_demux->p_sys;
2129 avi_chunk_idx1_t *p_idx1;
2131 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2132 return VLC_EGENERIC;
2136 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2137 b_keyset[i_stream] = false;
2139 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2144 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2147 if( i_stream < p_sys->i_track &&
2148 i_cat == p_sys->track[i_stream]->i_cat )
2151 index.i_id = p_idx1->entry[i_index].i_fourcc;
2153 p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2154 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2155 index.i_length = p_idx1->entry[i_index].i_length;
2156 AVI_IndexAddEntry( p_sys, i_stream, &index );
2158 if( index.i_flags&AVIIF_KEYFRAME )
2159 b_keyset[i_stream] = true;
2163 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2165 if( !b_keyset[i_stream] )
2167 avi_track_t *tk = p_sys->track[i_stream];
2169 msg_Dbg( p_demux, "no key frame set for track %d", i_stream );
2170 for( unsigned i_index = 0; i_index < tk->i_idxnb; i_index++ )
2171 tk->p_index[i_index].i_flags |= AVIIF_KEYFRAME;
2177 static void __Parse_indx( demux_t *p_demux,
2179 avi_chunk_indx_t *p_indx )
2181 demux_sys_t *p_sys = p_demux->p_sys;
2185 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2186 if( p_indx->i_indexsubtype == 0 )
2188 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2190 index.i_id = p_indx->i_id;
2191 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2192 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2193 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2195 AVI_IndexAddEntry( p_sys, i_stream, &index );
2198 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2200 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2202 index.i_id = p_indx->i_id;
2203 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2204 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2205 index.i_length = p_indx->idx.field[i].i_size;
2207 AVI_IndexAddEntry( p_sys, i_stream, &index );
2212 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2216 static void AVI_IndexLoad_indx( demux_t *p_demux )
2218 demux_sys_t *p_sys = p_demux->p_sys;
2219 unsigned int i_stream;
2222 avi_chunk_list_t *p_riff;
2223 avi_chunk_list_t *p_hdrl;
2225 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2226 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2228 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2230 avi_chunk_list_t *p_strl;
2231 avi_chunk_indx_t *p_indx;
2233 #define p_stream p_sys->track[i_stream]
2234 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2235 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2239 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2244 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2246 __Parse_indx( p_demux, i_stream, p_indx );
2248 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2251 for( i = 0; i < p_indx->i_entriesinuse; i++ )
2253 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2254 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2258 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2259 __Parse_indx( p_demux, i_stream, &ck_sub.indx );
2260 AVI_ChunkFree( p_demux->s, &ck_sub );
2265 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2271 static void AVI_IndexLoad( demux_t *p_demux )
2273 demux_sys_t *p_sys = p_demux->p_sys;
2274 unsigned int i_stream;
2276 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2278 p_sys->track[i_stream]->i_idxnb = 0;
2279 p_sys->track[i_stream]->i_idxmax = 0;
2280 p_sys->track[i_stream]->p_index = NULL;
2285 AVI_IndexLoad_indx( p_demux );
2287 else if( AVI_IndexLoad_idx1( p_demux ) )
2289 /* try indx if idx1 failed as some "normal" file have indx too */
2290 AVI_IndexLoad_indx( p_demux );
2293 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2295 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2296 i_stream, p_sys->track[i_stream]->i_idxnb );
2300 static void AVI_IndexCreate( demux_t *p_demux )
2302 demux_sys_t *p_sys = p_demux->p_sys;
2304 avi_chunk_list_t *p_riff;
2305 avi_chunk_list_t *p_movi;
2307 unsigned int i_stream;
2310 mtime_t i_dialog_update;
2311 dialog_progress_bar_t *p_dialog = NULL;
2313 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2314 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2318 msg_Err( p_demux, "cannot find p_movi" );
2322 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2324 p_sys->track[i_stream]->i_idxnb = 0;
2325 p_sys->track[i_stream]->i_idxmax = 0;
2326 p_sys->track[i_stream]->p_index = NULL;
2328 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2329 stream_Size( p_demux->s ) );
2331 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2332 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2335 /* Only show dialog if AVI is > 10MB */
2336 i_dialog_update = mdate();
2337 if( stream_Size( p_demux->s ) > 10000000 )
2338 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2339 NULL, _("Cancel") );
2345 if( !vlc_object_alive (p_demux) )
2348 /* Don't update/check dialog too often */
2349 if( p_dialog && mdate() - i_dialog_update > 100000 )
2351 if( dialog_ProgressCancelled( p_dialog ) )
2354 double f_current = stream_Tell( p_demux->s );
2355 double f_size = stream_Size( p_demux->s );
2356 double f_pos = f_current / f_size;
2357 dialog_ProgressSet( p_dialog, NULL, f_pos );
2359 i_dialog_update = mdate();
2362 if( AVI_PacketGetHeader( p_demux, &pk ) )
2365 if( pk.i_stream < p_sys->i_track &&
2366 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2369 index.i_id = pk.i_fourcc;
2371 AVI_GetKeyFlag(p_sys->track[pk.i_stream]->i_codec, pk.i_peek);
2372 index.i_pos = pk.i_pos;
2373 index.i_length = pk.i_size;
2374 AVI_IndexAddEntry( p_sys, pk.i_stream, &index );
2378 switch( pk.i_fourcc )
2380 case AVIFOURCC_idx1:
2383 avi_chunk_list_t *p_sysx;
2384 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2385 AVIFOURCC_RIFF, 1 );
2387 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2388 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2394 case AVIFOURCC_RIFF:
2395 msg_Dbg( p_demux, "new RIFF chunk found" );
2399 case AVIFOURCC_JUNK:
2403 msg_Warn( p_demux, "need resync, probably broken avi" );
2404 if( AVI_PacketSearch( p_demux ) )
2406 msg_Warn( p_demux, "lost sync, abord index creation" );
2412 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2413 AVI_PacketNext( p_demux ) )
2420 if( p_dialog != NULL )
2421 dialog_ProgressDestroy( p_dialog );
2423 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2425 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2426 i_stream, p_sys->track[i_stream]->i_idxnb );
2431 static void AVI_MetaLoad( demux_t *p_demux,
2432 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2434 demux_sys_t *p_sys = p_demux->p_sys;
2436 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2441 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2442 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2443 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2444 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2445 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2446 vlc_meta_SetSetting( p_meta, buffer );
2448 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2452 static const struct {
2456 { AVIFOURCC_IART, vlc_meta_Artist },
2457 { AVIFOURCC_ICMT, vlc_meta_Description },
2458 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2459 { AVIFOURCC_IGNR, vlc_meta_Genre },
2460 { AVIFOURCC_INAM, vlc_meta_Title },
2463 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2465 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2468 char *psz_value = FromLatin1( p_strz->p_str );
2473 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2478 /*****************************************************************************
2480 *****************************************************************************/
2481 static void AVI_ExtractSubtitle( demux_t *p_demux,
2483 avi_chunk_list_t *p_strl,
2484 avi_chunk_STRING_t *p_strn )
2486 demux_sys_t *p_sys = p_demux->p_sys;
2487 block_t *p_block = NULL;
2488 input_attachment_t *p_attachment = NULL;
2489 char *psz_description = NULL;
2490 avi_chunk_indx_t *p_indx = NULL;
2492 if( !p_sys->b_seekable )
2495 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2501 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2502 p_indx->i_entriesinuse > 0 )
2504 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2505 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2510 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2511 p_indx->i_entriesinuse != 1 ||
2512 p_indx->i_indexsubtype != 0 )
2515 i_position = p_indx->i_baseoffset +
2516 p_indx->idx.std[0].i_offset - 8;
2517 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2521 avi_chunk_idx1_t *p_idx1;
2524 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2528 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2530 const idx1_entry_t *e = &p_idx1->entry[i];
2532 unsigned i_stream_idx;
2534 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2535 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2537 i_position = e->i_pos + i_offset;
2538 i_size = e->i_length + 8;
2547 if( i_size > 1000000 )
2550 if( stream_Seek( p_demux->s, i_position ) )
2552 p_block = stream_Block( p_demux->s, i_size );
2556 /* Parse packet header */
2557 const uint8_t *p = p_block->p_buffer;
2558 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2563 /* Parse subtitle chunk header */
2564 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2565 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2567 const unsigned i_name = GetDWLE( &p[7] );
2568 if( 11 + i_size <= i_name )
2571 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2573 i_size -= 11 + i_name;
2574 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2576 const unsigned i_payload = GetDWLE( &p[2] );
2577 if( i_size < 6 + i_payload || i_payload <= 0 )
2582 if( !psz_description )
2583 psz_description = p_strn ? FromLatin1( p_strn->p_str ) : NULL;
2585 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2587 p_attachment = vlc_input_attachment_New( psz_name,
2588 "application/x-srt",
2592 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2596 free( psz_description );
2599 block_Release( p_block );
2602 msg_Dbg( p_demux, "Loaded an embed subtitle" );
2604 msg_Warn( p_demux, "Failed to load an embed subtitle" );
2606 if( p_indx == &ck.indx )
2607 AVI_ChunkFree( p_demux->s, &ck );
2609 /*****************************************************************************
2611 *****************************************************************************/
2612 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2614 demux_sys_t *p_sys = p_demux->p_sys;
2618 for( i = 0; i < p_sys->i_track; i++ )
2620 avi_track_t *tk = p_sys->track[i];
2621 if( tk->i_idxposc >= tk->i_idxnb )
2633 /****************************************************************************
2634 * AVI_MovieGetLength give max streams length in second
2635 ****************************************************************************/
2636 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2638 demux_sys_t *p_sys = p_demux->p_sys;
2639 mtime_t i_maxlength = 0;
2642 for( i = 0; i < p_sys->i_track; i++ )
2644 avi_track_t *tk = p_sys->track[i];
2647 /* fix length for each stream */
2648 if( tk->i_idxnb < 1 || !tk->p_index )
2653 if( tk->i_samplesize )
2655 i_length = AVI_GetDPTS( tk,
2656 tk->p_index[tk->i_idxnb-1].i_lengthtotal +
2657 tk->p_index[tk->i_idxnb-1].i_length );
2661 i_length = AVI_GetDPTS( tk, tk->i_idxnb );
2663 i_length /= (mtime_t)1000000; /* in seconds */
2666 "stream[%d] length:%"PRId64" (based on index)",
2669 i_maxlength = __MAX( i_maxlength, i_length );