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, NULL,
75 INTERLEAVE_TEXT, INTERLEAVE_LONGTEXT, true )
76 add_integer( "avi-index", 0, NULL,
77 INDEX_TEXT, INDEX_LONGTEXT, false )
78 change_integer_list( pi_index, ppsz_indexes, NULL )
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) )
95 vlc_fourcc_t i_fourcc;
98 vlc_fourcc_t i_type; /* only for AVIFOURCC_LIST */
100 uint8_t i_peek[8]; /* first 8 bytes */
102 unsigned int i_stream;
113 int64_t i_lengthtotal;
121 avi_entry_t *p_entry;
124 static void avi_index_Init( avi_index_t * );
125 static void avi_index_Clean( avi_index_t * );
126 static void avi_index_Append( avi_index_t *, off_t *, avi_entry_t * );
133 unsigned int i_cat; /* AUDIO_ES, VIDEO_ES */
134 vlc_fourcc_t i_codec;
138 unsigned int i_samplesize;
145 unsigned int i_idxposc; /* numero of chunk */
146 unsigned int i_idxposb; /* byte in the current chunk */
148 /* For VBR audio only */
149 unsigned int i_blockno;
150 unsigned int i_blocksize;
152 /* For muxed streams */
153 stream_t *p_out_muxed;
168 off_t i_movi_lastchunk_pos; /* XXX position of last valid chunk */
170 /* number of streams and information */
171 unsigned int i_track;
177 unsigned int i_attachment;
178 input_attachment_t **attachment;
181 static inline off_t __EVEN( off_t i )
183 return (i & 1) ? i + 1 : i;
186 static mtime_t AVI_PTSToChunk( avi_track_t *, mtime_t i_pts );
187 static mtime_t AVI_PTSToByte ( avi_track_t *, mtime_t i_pts );
188 static mtime_t AVI_GetDPTS ( avi_track_t *, int64_t i_count );
189 static mtime_t AVI_GetPTS ( avi_track_t * );
192 static int AVI_StreamChunkFind( demux_t *, unsigned int i_stream );
193 static int AVI_StreamChunkSet ( demux_t *,
194 unsigned int i_stream, unsigned int i_ck );
195 static int AVI_StreamBytesSet ( demux_t *,
196 unsigned int i_stream, off_t i_byte );
198 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t );
199 static int AVI_GetKeyFlag ( vlc_fourcc_t , uint8_t * );
201 static int AVI_PacketGetHeader( demux_t *, avi_packet_t *p_pk );
202 static int AVI_PacketNext ( demux_t * );
203 static int AVI_PacketRead ( demux_t *, avi_packet_t *, block_t **);
204 static int AVI_PacketSearch ( demux_t * );
206 static void AVI_IndexLoad ( demux_t * );
207 static void AVI_IndexCreate ( demux_t * );
209 static void AVI_ExtractSubtitle( demux_t *, int i_stream, avi_chunk_list_t *, avi_chunk_STRING_t * );
211 static mtime_t AVI_MovieGetLength( demux_t * );
213 static void AVI_MetaLoad( demux_t *, avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih );
215 /*****************************************************************************
217 *****************************************************************************/
218 static int AVI_TrackSeek ( demux_t *, int, mtime_t );
219 static int AVI_TrackStopFinishedStreams( demux_t *);
222 - For VBR mp3 stream:
223 count blocks by rounded-up chunksizes instead of chunks
224 we need full emulation of dshow avi demuxer bugs :(
225 fixes silly nandub-style a-v delaying in avi with vbr mp3...
226 (from mplayer 2002/08/02)
230 /*****************************************************************************
231 * Open: check file and initializes AVI structures
232 *****************************************************************************/
233 static int Open( vlc_object_t * p_this )
235 demux_t *p_demux = (demux_t *)p_this;
238 bool b_index = false;
241 avi_chunk_list_t *p_riff;
242 avi_chunk_list_t *p_hdrl, *p_movi;
243 avi_chunk_avih_t *p_avih;
245 unsigned int i_track;
246 unsigned int i, i_peeker;
248 const uint8_t *p_peek;
250 /* Is it an avi file ? */
251 if( stream_Peek( p_demux->s, &p_peek, 200 ) < 200 ) return VLC_EGENERIC;
253 for( i_peeker = 0; i_peeker < 188; i_peeker++ )
255 if( !strncmp( (char *)&p_peek[0], "RIFF", 4 ) && !strncmp( (char *)&p_peek[8], "AVI ", 4 ) )
257 if( !strncmp( (char *)&p_peek[0], "ON2 ", 4 ) && !strncmp( (char *)&p_peek[8], "ON2f", 4 ) )
261 if( i_peeker == 188 )
266 /* Initialize input structures. */
267 p_sys = p_demux->p_sys = malloc( sizeof(demux_sys_t) );
268 memset( p_sys, 0, sizeof( demux_sys_t ) );
271 p_sys->i_movi_lastchunk_pos = 0;
272 p_sys->b_odml = false;
273 p_sys->b_muxed = false;
277 TAB_INIT(p_sys->i_attachment, p_sys->attachment);
279 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &p_sys->b_seekable );
281 p_demux->pf_control = Control;
282 p_demux->pf_demux = Demux_Seekable;
284 /* For unseekable stream, automaticaly use Demux_UnSeekable */
285 if( !p_sys->b_seekable
286 || var_InheritInteger( p_demux, "avi-interleaved" ) )
288 p_demux->pf_demux = Demux_UnSeekable;
293 stream_Read( p_demux->s, NULL, i_peeker );
296 if( AVI_ChunkReadRoot( p_demux->s, &p_sys->ck_root ) )
298 msg_Err( p_demux, "avi module discarded (invalid file)" );
303 if( AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF ) > 1 )
305 unsigned int i_count =
306 AVI_ChunkCount( &p_sys->ck_root, AVIFOURCC_RIFF );
308 msg_Warn( p_demux, "multiple riff -> OpenDML ?" );
309 for( i = 1; i < i_count; i++ )
311 avi_chunk_list_t *p_sysx;
313 p_sysx = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, i );
314 if( p_sysx->i_type == AVIFOURCC_AVIX )
316 msg_Warn( p_demux, "detected OpenDML file" );
317 p_sys->b_odml = true;
323 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0 );
324 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
325 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0 );
327 if( !p_hdrl || !p_movi )
329 msg_Err( p_demux, "avi module discarded (invalid file)" );
333 if( !( p_avih = AVI_ChunkFind( p_hdrl, AVIFOURCC_avih, 0 ) ) )
335 msg_Err( p_demux, "cannot find avih chunk" );
338 i_track = AVI_ChunkCount( p_hdrl, AVIFOURCC_strl );
339 if( p_avih->i_streams != i_track )
342 "found %d stream but %d are declared",
343 i_track, p_avih->i_streams );
347 msg_Err( p_demux, "no stream defined!" );
351 /* print information on streams */
352 msg_Dbg( p_demux, "AVIH: %d stream, flags %s%s%s%s ",
354 p_avih->i_flags&AVIF_HASINDEX?" HAS_INDEX":"",
355 p_avih->i_flags&AVIF_MUSTUSEINDEX?" MUST_USE_INDEX":"",
356 p_avih->i_flags&AVIF_ISINTERLEAVED?" IS_INTERLEAVED":"",
357 p_avih->i_flags&AVIF_TRUSTCKTYPE?" TRUST_CKTYPE":"" );
359 AVI_MetaLoad( p_demux, p_riff, p_avih );
361 /* now read info on each stream and create ES */
362 for( i = 0 ; i < i_track; i++ )
364 avi_track_t *tk = malloc( sizeof( avi_track_t ) );
368 avi_chunk_list_t *p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
369 avi_chunk_strh_t *p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
370 avi_chunk_STRING_t *p_strn = AVI_ChunkFind( p_strl, AVIFOURCC_strn, 0 );
371 avi_chunk_strf_auds_t *p_auds = NULL;
372 avi_chunk_strf_vids_t *p_vids = NULL;
375 memset( tk, 0, sizeof(*tk) );
377 tk->b_activated = true;
379 p_vids = (avi_chunk_strf_vids_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
380 p_auds = (avi_chunk_strf_auds_t*)AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
382 if( p_strl == NULL || p_strh == NULL || p_auds == NULL || p_vids == NULL )
384 msg_Warn( p_demux, "stream[%d] incomplete", i );
389 tk->i_rate = p_strh->i_rate;
390 tk->i_scale = p_strh->i_scale;
391 tk->i_samplesize = p_strh->i_samplesize;
392 msg_Dbg( p_demux, "stream[%d] rate:%d scale:%d samplesize:%d",
393 i, tk->i_rate, tk->i_scale, tk->i_samplesize );
395 switch( p_strh->i_type )
397 case( AVIFOURCC_auds ):
398 tk->i_cat = AUDIO_ES;
399 tk->i_codec = AVI_FourccGetCodec( AUDIO_ES,
400 p_auds->p_wf->wFormatTag );
402 tk->i_blocksize = p_auds->p_wf->nBlockAlign;
403 if( tk->i_blocksize == 0 )
405 if( p_auds->p_wf->wFormatTag == 1 )
406 tk->i_blocksize = p_auds->p_wf->nChannels * (p_auds->p_wf->wBitsPerSample/8);
410 else if( tk->i_samplesize != 0 && tk->i_samplesize != tk->i_blocksize )
412 msg_Warn( p_demux, "track[%d] samplesize=%d and blocksize=%d are not equal."
413 "Using blocksize as a workaround.",
414 i, tk->i_samplesize, tk->i_blocksize );
415 tk->i_samplesize = tk->i_blocksize;
418 if( tk->i_codec == VLC_CODEC_VORBIS )
420 tk->i_blocksize = 0; /* fix vorbis VBR decoding */
423 es_format_Init( &fmt, AUDIO_ES, tk->i_codec );
425 fmt.audio.i_channels = p_auds->p_wf->nChannels;
426 fmt.audio.i_rate = p_auds->p_wf->nSamplesPerSec;
427 fmt.i_bitrate = p_auds->p_wf->nAvgBytesPerSec*8;
428 fmt.audio.i_blockalign = p_auds->p_wf->nBlockAlign;
429 fmt.audio.i_bitspersample = p_auds->p_wf->wBitsPerSample;
430 fmt.b_packetized = !tk->i_blocksize;
433 "stream[%d] audio(0x%x) %d channels %dHz %dbits",
434 i, p_auds->p_wf->wFormatTag, p_auds->p_wf->nChannels,
435 p_auds->p_wf->nSamplesPerSec,
436 p_auds->p_wf->wBitsPerSample );
438 fmt.i_extra = __MIN( p_auds->p_wf->cbSize,
439 p_auds->i_chunk_size - sizeof(WAVEFORMATEX) );
440 fmt.p_extra = malloc( fmt.i_extra );
441 if( !fmt.p_extra ) goto error;
442 memcpy( fmt.p_extra, &p_auds->p_wf[1], fmt.i_extra );
445 case( AVIFOURCC_vids ):
446 tk->i_cat = VIDEO_ES;
447 tk->i_codec = AVI_FourccGetCodec( VIDEO_ES,
448 p_vids->p_bih->biCompression );
449 if( p_vids->p_bih->biCompression == VLC_FOURCC( 'D', 'X', 'S', 'B' ) )
451 msg_Dbg( p_demux, "stream[%d] subtitles", i );
452 es_format_Init( &fmt, SPU_ES, p_vids->p_bih->biCompression );
455 else if( p_vids->p_bih->biCompression == 0x00 )
457 switch( p_vids->p_bih->biBitCount )
460 tk->i_codec = VLC_CODEC_RGB32;
463 tk->i_codec = VLC_CODEC_RGB24;
465 case 16: /* Yes it is RV15 */
467 tk->i_codec = VLC_CODEC_RGB15;
469 case 9: /* <- TODO check that */
470 tk->i_codec = VLC_CODEC_I410;
472 case 8: /* <- TODO check that */
473 tk->i_codec = VLC_CODEC_GREY;
476 es_format_Init( &fmt, VIDEO_ES, tk->i_codec );
478 switch( tk->i_codec )
480 case VLC_CODEC_RGB24:
481 case VLC_CODEC_RGB32:
482 fmt.video.i_rmask = 0x00ff0000;
483 fmt.video.i_gmask = 0x0000ff00;
484 fmt.video.i_bmask = 0x000000ff;
486 case VLC_CODEC_RGB15:
487 fmt.video.i_rmask = 0x7c00;
488 fmt.video.i_gmask = 0x03e0;
489 fmt.video.i_bmask = 0x001f;
497 es_format_Init( &fmt, VIDEO_ES, p_vids->p_bih->biCompression );
498 if( tk->i_codec == VLC_CODEC_MP4V &&
499 !strncasecmp( (char*)&p_strh->i_handler, "XVID", 4 ) )
502 fmt.i_original_fourcc = VLC_FOURCC( 'X', 'V', 'I', 'D' );
505 tk->i_samplesize = 0;
506 fmt.video.i_width = p_vids->p_bih->biWidth;
507 fmt.video.i_height = p_vids->p_bih->biHeight;
508 fmt.video.i_bits_per_pixel = p_vids->p_bih->biBitCount;
509 fmt.video.i_frame_rate = tk->i_rate;
510 fmt.video.i_frame_rate_base = tk->i_scale;
512 __MIN( p_vids->p_bih->biSize - sizeof( BITMAPINFOHEADER ),
513 p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER) );
514 fmt.p_extra = malloc( fmt.i_extra );
515 if( !fmt.p_extra ) goto error;
516 memcpy( fmt.p_extra, &p_vids->p_bih[1], fmt.i_extra );
518 msg_Dbg( p_demux, "stream[%d] video(%4.4s) %"PRIu32"x%"PRIu32" %dbpp %ffps",
519 i, (char*)&p_vids->p_bih->biCompression,
520 (uint32_t)p_vids->p_bih->biWidth,
521 (uint32_t)p_vids->p_bih->biHeight,
522 p_vids->p_bih->biBitCount,
523 (float)tk->i_rate/(float)tk->i_scale );
525 if( p_vids->p_bih->biCompression == 0x00 )
527 /* RGB DIB are coded from bottom to top */
529 (unsigned int)(-(int)p_vids->p_bih->biHeight);
532 /* Extract palette from extradata if bpp <= 8
533 * (assumes that extradata contains only palette but appears
534 * to be true for all palettized codecs we support) */
535 if( fmt.video.i_bits_per_pixel > 0 && fmt.video.i_bits_per_pixel <= 8 )
537 /* The palette is not always included in biSize */
538 fmt.i_extra = p_vids->i_chunk_size - sizeof(BITMAPINFOHEADER);
539 if( fmt.i_extra > 0 )
541 const uint8_t *p_pal = fmt.p_extra;
543 fmt.video.p_palette = calloc( 1, sizeof(video_palette_t) );
544 fmt.video.p_palette->i_entries = __MIN(fmt.i_extra/4, 256);
546 for( int i = 0; i < fmt.video.p_palette->i_entries; i++ )
548 for( int j = 0; j < 4; j++ )
549 fmt.video.p_palette->palette[i][j] = p_pal[4*i+j];
555 case( AVIFOURCC_txts):
556 msg_Dbg( p_demux, "stream[%d] subtitle attachment", i );
557 AVI_ExtractSubtitle( p_demux, i, p_strl, p_strn );
561 case( AVIFOURCC_iavs):
562 case( AVIFOURCC_ivas):
563 p_sys->b_muxed = true;
564 msg_Dbg( p_demux, "stream[%d] iavs with handler %4.4s", i, (char *)&p_strh->i_handler );
565 if( p_strh->i_handler == FOURCC_dvsd ||
566 p_strh->i_handler == FOURCC_dvhd ||
567 p_strh->i_handler == FOURCC_dvsl ||
568 p_strh->i_handler == FOURCC_dv25 ||
569 p_strh->i_handler == FOURCC_dv50 )
571 tk->p_out_muxed = stream_DemuxNew( p_demux, (char *)"rawdv", p_demux->out );
572 if( !tk->p_out_muxed )
573 msg_Err( p_demux, "could not load the DV parser" );
579 case( AVIFOURCC_mids):
580 msg_Dbg( p_demux, "stream[%d] midi is UNSUPPORTED", i );
583 msg_Warn( p_demux, "stream[%d] unknown type %4.4s", i, (char *)&p_strh->i_type );
588 fmt.psz_description = FromLatin1( p_strn->p_str );
589 if( tk->p_out_muxed == NULL )
590 tk->p_es = es_out_Add( p_demux->out, &fmt );
591 TAB_APPEND( p_sys->i_track, p_sys->track, tk );
592 es_format_Clean( &fmt );
595 if( p_sys->i_track <= 0 )
597 msg_Err( p_demux, "no valid track" );
601 i_do_index = var_InheritInteger( p_demux, "avi-index" );
602 if( i_do_index == 1 ) /* Always fix */
605 if( p_sys->b_seekable )
607 AVI_IndexCreate( p_demux );
611 msg_Warn( p_demux, "cannot create index (unseekable stream)" );
612 AVI_IndexLoad( p_demux );
617 AVI_IndexLoad( p_demux );
620 /* *** movie length in sec *** */
621 p_sys->i_length = AVI_MovieGetLength( p_demux );
623 /* Check the index completeness */
624 unsigned int i_idx_totalframes = 0;
625 for( unsigned int i = 0; i < p_sys->i_track; i++ )
627 const avi_track_t *tk = p_sys->track[i];
628 if( tk->i_cat == VIDEO_ES && tk->idx.p_entry )
629 i_idx_totalframes = __MAX(i_idx_totalframes, tk->idx.i_size);
632 if( i_idx_totalframes != p_avih->i_totalframes &&
633 p_sys->i_length < (mtime_t)p_avih->i_totalframes *
634 (mtime_t)p_avih->i_microsecperframe /
637 if( !vlc_object_alive( p_demux) )
640 msg_Warn( p_demux, "broken or missing index, 'seek' will be "
641 "approximative or will exhibit strange behavior" );
642 if( i_do_index == 0 && !b_index )
644 if( !p_sys->b_seekable ) {
648 switch( dialog_Question( p_demux, _("AVI Index") ,
649 _( "This AVI file is broken. Seeking will not work correctly.\n"
650 "Do you want to try to fix it?\n\n"
651 "This might take a long time." ),
652 _( "Repair" ), _( "Don't repair" ), _( "Cancel") ) )
656 msg_Dbg( p_demux, "Fixing AVI index" );
660 vlc_object_kill( p_demux->p_parent );
666 /* fix some BeOS MediaKit generated file */
667 for( i = 0 ; i < p_sys->i_track; i++ )
669 avi_track_t *tk = p_sys->track[i];
670 avi_chunk_list_t *p_strl;
671 avi_chunk_strh_t *p_strh;
672 avi_chunk_strf_auds_t *p_auds;
674 if( tk->i_cat != AUDIO_ES )
678 if( tk->idx.i_size < 1 ||
680 tk->i_samplesize != 0 )
684 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i );
685 p_strh = AVI_ChunkFind( p_strl, AVIFOURCC_strh, 0 );
686 p_auds = AVI_ChunkFind( p_strl, AVIFOURCC_strf, 0 );
688 if( p_auds->p_wf->wFormatTag != WAVE_FORMAT_PCM &&
689 (unsigned int)tk->i_rate == p_auds->p_wf->nSamplesPerSec )
691 int64_t i_track_length =
692 tk->idx.p_entry[tk->idx.i_size-1].i_length +
693 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal;
694 mtime_t i_length = (mtime_t)p_avih->i_totalframes *
695 (mtime_t)p_avih->i_microsecperframe;
699 msg_Warn( p_demux, "track[%d] cannot be fixed (BeOS MediaKit generated)", i );
702 tk->i_samplesize = 1;
703 tk->i_rate = i_track_length * (int64_t)1000000/ i_length;
704 msg_Warn( p_demux, "track[%d] fixed with rate=%d scale=%d (BeOS MediaKit generated)", i, tk->i_rate, tk->i_scale );
708 if( p_sys->b_seekable )
710 /* we have read all chunk so go back to movi */
711 stream_Seek( p_demux->s, p_movi->i_chunk_pos );
713 /* Skip movi header */
714 stream_Read( p_demux->s, NULL, 12 );
716 p_sys->i_movi_begin = p_movi->i_chunk_pos;
720 for( unsigned i = 0; i < p_sys->i_attachment; i++)
721 vlc_input_attachment_Delete(p_sys->attachment[i]);
722 free(p_sys->attachment);
725 vlc_meta_Delete( p_sys->meta );
727 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
729 return vlc_object_alive( p_demux ) ? VLC_EGENERIC : VLC_ETIMEOUT;
732 /*****************************************************************************
733 * Close: frees unused data
734 *****************************************************************************/
735 static void Close ( vlc_object_t * p_this )
737 demux_t * p_demux = (demux_t *)p_this;
739 demux_sys_t *p_sys = p_demux->p_sys ;
741 for( i = 0; i < p_sys->i_track; i++ )
743 if( p_sys->track[i] )
745 if( p_sys->track[i]->p_out_muxed )
746 stream_Delete( p_sys->track[i]->p_out_muxed );
747 avi_index_Clean( &p_sys->track[i]->idx );
748 free( p_sys->track[i] );
751 free( p_sys->track );
752 AVI_ChunkFreeRoot( p_demux->s, &p_sys->ck_root );
753 vlc_meta_Delete( p_sys->meta );
754 for( unsigned i = 0; i < p_sys->i_attachment; i++)
755 vlc_input_attachment_Delete(p_sys->attachment[i]);
756 free(p_sys->attachment);
761 /*****************************************************************************
762 * Demux_Seekable: reads and demuxes data packets for stream seekable
763 *****************************************************************************
764 * AVIDemux: reads and demuxes data packets
765 *****************************************************************************
766 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
767 *****************************************************************************/
774 off_t i_posf; /* where we will read :
775 if i_idxposb == 0 : begining of chunk (+8 to acces data)
776 else : point on data directly */
777 } avi_track_toread_t;
779 static int Demux_Seekable( demux_t *p_demux )
781 demux_sys_t *p_sys = p_demux->p_sys;
783 unsigned int i_track_count = 0;
784 unsigned int i_track;
785 /* cannot be more than 100 stream (dcXX or wbXX) */
786 avi_track_toread_t toread[100];
789 /* detect new selected/unselected streams */
790 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
792 avi_track_t *tk = p_sys->track[i_track];
795 if( p_sys->b_muxed && tk->p_out_muxed )
798 tk->b_activated = true;
802 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
803 if( b && !tk->b_activated )
805 if( p_sys->b_seekable)
807 AVI_TrackSeek( p_demux, i_track, p_sys->i_time );
809 tk->b_activated = true;
811 else if( !b && tk->b_activated )
813 tk->b_activated = false;
821 if( i_track_count <= 0 )
823 int64_t i_length = p_sys->i_length * (mtime_t)1000000;
825 p_sys->i_time += 25*1000; /* read 25ms */
828 if( p_sys->i_time >= i_length )
832 msg_Warn( p_demux, "no track selected, exiting..." );
836 /* wait for the good time */
837 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
838 p_sys->i_time += 25*1000; /* read 25ms */
841 for( i_track = 0; i_track < p_sys->i_track; i_track++ )
843 avi_track_t *tk = p_sys->track[i_track];
846 toread[i_track].b_ok = tk->b_activated && !tk->b_eof;
847 if( tk->i_idxposc < tk->idx.i_size )
849 toread[i_track].i_posf = tk->idx.p_entry[tk->i_idxposc].i_pos;
850 if( tk->i_idxposb > 0 )
852 toread[i_track].i_posf += 8 + tk->i_idxposb;
857 toread[i_track].i_posf = -1;
860 i_dpts = p_sys->i_time - AVI_GetPTS( tk );
862 if( tk->i_samplesize )
864 toread[i_track].i_toread = AVI_PTSToByte( tk, __ABS( i_dpts ) );
868 toread[i_track].i_toread = AVI_PTSToChunk( tk, __ABS( i_dpts ) );
873 toread[i_track].i_toread *= -1;
886 /* search for first chunk to be read */
887 for( i = 0, b_done = true, i_pos = -1; i < p_sys->i_track; i++ )
889 if( !toread[i].b_ok ||
890 AVI_GetDPTS( p_sys->track[i],
891 toread[i].i_toread ) <= -25 * 1000 )
896 if( toread[i].i_toread > 0 )
898 b_done = false; /* not yet finished */
900 if( toread[i].i_posf > 0 )
902 if( i_pos == -1 || i_pos > toread[i].i_posf )
905 i_pos = toread[i].i_posf;
912 for( i = 0; i < p_sys->i_track; i++ )
917 msg_Warn( p_demux, "all tracks have failed, exiting..." );
923 int i_loop_count = 0;
925 /* no valid index, we will parse directly the stream
926 * in case we fail we will disable all finished stream */
927 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
929 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
930 if( AVI_PacketNext( p_demux ) )
932 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
937 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
944 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
947 "cannot get packet header, track disabled" );
948 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
950 if( avi_pk.i_stream >= p_sys->i_track ||
951 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
953 if( AVI_PacketNext( p_demux ) )
956 "cannot skip packet, track disabled" );
957 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
960 /* Prevents from eating all the CPU with broken files.
961 * This value should be low enough so that it doesn't
962 * affect the reading speed too much. */
963 if( !(++i_loop_count % 1024) )
965 if( !vlc_object_alive (p_demux) ) return -1;
968 if( !(i_loop_count % (1024 * 10)) )
970 "don't seem to find any data..." );
976 i_track = avi_pk.i_stream;
977 tk = p_sys->track[i_track];
979 /* add this chunk to the index */
981 index.i_id = avi_pk.i_fourcc;
982 index.i_flags = AVI_GetKeyFlag(tk->i_codec, avi_pk.i_peek);
983 index.i_pos = avi_pk.i_pos;
984 index.i_length = avi_pk.i_size;
985 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
987 /* do we will read this data ? */
988 if( AVI_GetDPTS( tk, toread[i_track].i_toread ) > -25*1000 )
994 if( AVI_PacketNext( p_demux ) )
997 "cannot skip packet, track disabled" );
998 return( AVI_TrackStopFinishedStreams( p_demux ) ? 0 : 1 );
1007 stream_Seek( p_demux->s, i_pos );
1010 /* Set the track to use */
1011 tk = p_sys->track[i_track];
1013 /* read thoses data */
1014 if( tk->i_samplesize )
1016 unsigned int i_toread;
1018 if( ( i_toread = toread[i_track].i_toread ) <= 0 )
1020 if( tk->i_samplesize > 1 )
1022 i_toread = tk->i_samplesize;
1026 i_toread = AVI_PTSToByte( tk, 20 * 1000 );
1027 i_toread = __MAX( i_toread, 100 );
1030 i_size = __MIN( tk->idx.p_entry[tk->i_idxposc].i_length -
1036 i_size = tk->idx.p_entry[tk->i_idxposc].i_length;
1039 if( tk->i_idxposb == 0 )
1041 i_size += 8; /* need to read and skip header */
1044 if( ( p_frame = stream_Block( p_demux->s, __EVEN( i_size ) ) )==NULL )
1046 msg_Warn( p_demux, "failed reading data" );
1048 toread[i_track].b_ok = false;
1051 if( i_size % 2 ) /* read was padded on word boundary */
1053 p_frame->i_buffer--;
1056 if( tk->i_idxposb == 0 )
1058 p_frame->p_buffer += 8;
1059 p_frame->i_buffer -= 8;
1061 p_frame->i_pts = AVI_GetPTS( tk ) + 1;
1062 if( tk->idx.p_entry[tk->i_idxposc].i_flags&AVIIF_KEYFRAME )
1064 p_frame->i_flags = BLOCK_FLAG_TYPE_I;
1068 p_frame->i_flags = BLOCK_FLAG_TYPE_PB;
1072 if( tk->i_samplesize )
1074 if( tk->i_idxposb == 0 )
1078 toread[i_track].i_toread -= i_size;
1079 tk->i_idxposb += i_size;
1080 if( tk->i_idxposb >=
1081 tk->idx.p_entry[tk->i_idxposc].i_length )
1089 int i_length = tk->idx.p_entry[tk->i_idxposc].i_length;
1092 if( tk->i_cat == AUDIO_ES )
1094 tk->i_blockno += tk->i_blocksize > 0 ? ( i_length + tk->i_blocksize - 1 ) / tk->i_blocksize : 1;
1096 toread[i_track].i_toread--;
1099 if( tk->i_idxposc < tk->idx.i_size)
1101 toread[i_track].i_posf =
1102 tk->idx.p_entry[tk->i_idxposc].i_pos;
1103 if( tk->i_idxposb > 0 )
1105 toread[i_track].i_posf += 8 + tk->i_idxposb;
1111 toread[i_track].i_posf = -1;
1114 if( tk->i_cat != VIDEO_ES )
1115 p_frame->i_dts = p_frame->i_pts;
1118 p_frame->i_dts = p_frame->i_pts;
1119 p_frame->i_pts = VLC_TS_INVALID;
1122 //p_pes->i_rate = p_demux->stream.control.i_rate;
1123 if( tk->p_out_muxed )
1124 stream_DemuxSend( tk->p_out_muxed, p_frame );
1126 es_out_Send( p_demux->out, tk->p_es, p_frame );
1131 /*****************************************************************************
1132 * Demux_UnSeekable: reads and demuxes data packets for unseekable file
1133 *****************************************************************************
1134 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1135 *****************************************************************************/
1136 static int Demux_UnSeekable( demux_t *p_demux )
1138 demux_sys_t *p_sys = p_demux->p_sys;
1139 avi_track_t *p_stream_master = NULL;
1140 unsigned int i_stream;
1141 unsigned int i_packet;
1143 if( p_sys->b_muxed )
1145 msg_Err( p_demux, "Can not yet process muxed avi substreams without seeking" );
1146 return VLC_EGENERIC;
1149 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_time + 1 );
1151 /* *** find master stream for data packet skipping algo *** */
1152 /* *** -> first video, if any, or first audio ES *** */
1153 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1155 avi_track_t *tk = p_sys->track[i_stream];
1158 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1160 if( b && tk->i_cat == VIDEO_ES )
1162 p_stream_master = tk;
1166 p_stream_master = tk;
1170 if( !p_stream_master )
1172 msg_Warn( p_demux, "no more stream selected" );
1176 p_sys->i_time = AVI_GetPTS( p_stream_master );
1178 for( i_packet = 0; i_packet < 10; i_packet++)
1180 #define p_stream p_sys->track[avi_pk.i_stream]
1182 avi_packet_t avi_pk;
1184 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1189 if( avi_pk.i_stream >= p_sys->i_track ||
1190 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1192 /* we haven't found an audio or video packet:
1193 * - we have seek, found first next packet
1194 * - others packets could be found, skip them
1196 switch( avi_pk.i_fourcc )
1198 case AVIFOURCC_JUNK:
1199 case AVIFOURCC_LIST:
1200 case AVIFOURCC_RIFF:
1201 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1202 case AVIFOURCC_idx1:
1205 return( !AVI_PacketNext( p_demux ) ? 1 : 0 );
1207 return( 0 ); /* eof */
1210 "seems to have lost position, resync" );
1211 if( AVI_PacketSearch( p_demux ) )
1213 msg_Err( p_demux, "resync failed" );
1220 /* check for time */
1221 if( __ABS( AVI_GetPTS( p_stream ) -
1222 AVI_GetPTS( p_stream_master ) )< 600*1000 )
1224 /* load it and send to decoder */
1226 if( AVI_PacketRead( p_demux, &avi_pk, &p_frame ) || p_frame == NULL )
1230 p_frame->i_pts = AVI_GetPTS( p_stream ) + 1;
1232 if( avi_pk.i_cat != VIDEO_ES )
1233 p_frame->i_dts = p_frame->i_pts;
1236 p_frame->i_dts = p_frame->i_pts;
1237 p_frame->i_pts = VLC_TS_INVALID;
1240 //p_pes->i_rate = p_demux->stream.control.i_rate;
1241 es_out_Send( p_demux->out, p_stream->p_es, p_frame );
1245 if( AVI_PacketNext( p_demux ) )
1251 /* *** update stream time position *** */
1252 if( p_stream->i_samplesize )
1254 p_stream->i_idxposb += avi_pk.i_size;
1258 if( p_stream->i_cat == AUDIO_ES )
1260 p_stream->i_blockno += p_stream->i_blocksize > 0 ? ( avi_pk.i_size + p_stream->i_blocksize - 1 ) / p_stream->i_blocksize : 1;
1262 p_stream->i_idxposc++;
1272 /*****************************************************************************
1273 * Seek: goto to i_date or i_percent
1274 *****************************************************************************/
1275 static int Seek( demux_t *p_demux, mtime_t i_date, int i_percent )
1278 demux_sys_t *p_sys = p_demux->p_sys;
1279 unsigned int i_stream;
1280 msg_Dbg( p_demux, "seek requested: %"PRId64" seconds %d%%",
1281 i_date / 1000000, i_percent );
1283 if( p_sys->b_seekable )
1285 if( !p_sys->i_length )
1287 avi_track_t *p_stream;
1290 /* use i_percent to create a true i_date */
1291 msg_Warn( p_demux, "seeking without index at %d%%"
1292 " only works for interleaved files", i_percent );
1293 if( i_percent >= 100 )
1295 msg_Warn( p_demux, "cannot seek so far !" );
1296 return VLC_EGENERIC;
1298 i_percent = __MAX( i_percent, 0 );
1300 /* try to find chunk that is at i_percent or the file */
1301 i_pos = __MAX( i_percent * stream_Size( p_demux->s ) / 100,
1302 p_sys->i_movi_begin );
1303 /* search first selected stream (and prefer non eof ones) */
1304 for( i_stream = 0, p_stream = NULL;
1305 i_stream < p_sys->i_track; i_stream++ )
1307 if( !p_stream || p_stream->b_eof )
1308 p_stream = p_sys->track[i_stream];
1310 if( p_stream->b_activated && !p_stream->b_eof )
1313 if( !p_stream || !p_stream->b_activated )
1315 msg_Warn( p_demux, "cannot find any selected stream" );
1316 return VLC_EGENERIC;
1319 /* be sure that the index exist */
1320 if( AVI_StreamChunkSet( p_demux, i_stream, 0 ) )
1322 msg_Warn( p_demux, "cannot seek" );
1323 return VLC_EGENERIC;
1326 while( i_pos >= p_stream->idx.p_entry[p_stream->i_idxposc].i_pos +
1327 p_stream->idx.p_entry[p_stream->i_idxposc].i_length + 8 )
1329 /* search after i_idxposc */
1330 if( AVI_StreamChunkSet( p_demux,
1331 i_stream, p_stream->i_idxposc + 1 ) )
1333 msg_Warn( p_demux, "cannot seek" );
1334 return VLC_EGENERIC;
1338 i_date = AVI_GetPTS( p_stream );
1339 /* TODO better support for i_samplesize != 0 */
1340 msg_Dbg( p_demux, "estimate date %"PRId64, i_date );
1344 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
1346 avi_track_t *p_stream = p_sys->track[i_stream];
1348 if( !p_stream->b_activated )
1351 p_stream->b_eof = AVI_TrackSeek( p_demux, i_stream, i_date ) != 0;
1353 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
1354 p_sys->i_time = i_date;
1355 msg_Dbg( p_demux, "seek: %"PRId64" seconds", p_sys->i_time /1000000 );
1360 msg_Err( p_demux, "shouldn't yet be executed" );
1361 return VLC_EGENERIC;
1365 /*****************************************************************************
1367 *****************************************************************************/
1368 static double ControlGetPosition( demux_t *p_demux )
1370 demux_sys_t *p_sys = p_demux->p_sys;
1372 if( p_sys->i_length > 0 )
1374 return (double)p_sys->i_time / (double)( p_sys->i_length * (mtime_t)1000000 );
1376 else if( stream_Size( p_demux->s ) > 0 )
1382 /* search the more advanced selected es */
1383 for( i = 0; i < p_sys->i_track; i++ )
1385 avi_track_t *tk = p_sys->track[i];
1386 if( tk->b_activated && tk->i_idxposc < tk->idx.i_size )
1388 i_tmp = tk->idx.p_entry[tk->i_idxposc].i_pos +
1389 tk->idx.p_entry[tk->i_idxposc].i_length + 8;
1396 return (double)i64 / stream_Size( p_demux->s );
1401 static int Control( demux_t *p_demux, int i_query, va_list args )
1403 demux_sys_t *p_sys = p_demux->p_sys;
1411 case DEMUX_GET_POSITION:
1412 pf = (double*)va_arg( args, double * );
1413 *pf = ControlGetPosition( p_demux );
1415 case DEMUX_SET_POSITION:
1416 f = (double)va_arg( args, double );
1417 if( p_sys->b_seekable )
1419 i64 = (mtime_t)(1000000.0 * p_sys->i_length * f );
1420 return Seek( p_demux, i64, (int)(f * 100) );
1424 int64_t i_pos = stream_Size( p_demux->s ) * f;
1425 return stream_Seek( p_demux->s, i_pos );
1428 case DEMUX_GET_TIME:
1429 pi64 = (int64_t*)va_arg( args, int64_t * );
1430 *pi64 = p_sys->i_time;
1433 case DEMUX_SET_TIME:
1437 i64 = (int64_t)va_arg( args, int64_t );
1438 if( p_sys->i_length > 0 )
1440 i_percent = 100 * i64 / (p_sys->i_length*1000000);
1442 else if( p_sys->i_time > 0 )
1444 i_percent = (int)( 100.0 * ControlGetPosition( p_demux ) *
1445 (double)i64 / (double)p_sys->i_time );
1447 return Seek( p_demux, i64, i_percent );
1449 case DEMUX_GET_LENGTH:
1450 pi64 = (int64_t*)va_arg( args, int64_t * );
1451 *pi64 = p_sys->i_length * (mtime_t)1000000;
1455 pf = (double*)va_arg( args, double * );
1457 for( i = 0; i < (int)p_sys->i_track; i++ )
1459 avi_track_t *tk = p_sys->track[i];
1460 if( tk->i_cat == VIDEO_ES && tk->i_scale > 0)
1462 *pf = (float)tk->i_rate / (float)tk->i_scale;
1468 case DEMUX_GET_META:
1469 p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
1470 vlc_meta_Merge( p_meta, p_sys->meta );
1473 case DEMUX_GET_ATTACHMENTS:
1475 if( p_sys->i_attachment <= 0 )
1476 return VLC_EGENERIC;
1478 input_attachment_t ***ppp_attach = va_arg( args, input_attachment_t*** );
1479 int *pi_int = va_arg( args, int * );
1481 *pi_int = p_sys->i_attachment;
1482 *ppp_attach = calloc( p_sys->i_attachment, sizeof(*ppp_attach));
1483 for( unsigned i = 0; i < p_sys->i_attachment && *ppp_attach; i++ )
1484 (*ppp_attach)[i] = vlc_input_attachment_Duplicate( p_sys->attachment[i] );
1489 return VLC_EGENERIC;
1493 /*****************************************************************************
1494 * Function to convert pts to chunk or byte
1495 *****************************************************************************/
1497 static mtime_t AVI_PTSToChunk( avi_track_t *tk, mtime_t i_pts )
1502 return (mtime_t)((int64_t)i_pts *
1503 (int64_t)tk->i_rate /
1504 (int64_t)tk->i_scale /
1507 static mtime_t AVI_PTSToByte( avi_track_t *tk, mtime_t i_pts )
1509 if( !tk->i_scale || !tk->i_samplesize )
1512 return (mtime_t)((int64_t)i_pts *
1513 (int64_t)tk->i_rate /
1514 (int64_t)tk->i_scale /
1516 (int64_t)tk->i_samplesize );
1519 static mtime_t AVI_GetDPTS( avi_track_t *tk, int64_t i_count )
1526 i_dpts = (mtime_t)( (int64_t)1000000 *
1528 (int64_t)tk->i_scale /
1529 (int64_t)tk->i_rate );
1531 if( tk->i_samplesize )
1533 return i_dpts / tk->i_samplesize;
1538 static mtime_t AVI_GetPTS( avi_track_t *tk )
1540 if( tk->i_samplesize )
1542 int64_t i_count = 0;
1544 /* we need a valid entry we will emulate one */
1545 if( tk->i_idxposc == tk->idx.i_size )
1549 /* use the last entry */
1550 i_count = tk->idx.p_entry[tk->idx.i_size - 1].i_lengthtotal
1551 + tk->idx.p_entry[tk->idx.i_size - 1].i_length;
1556 i_count = tk->idx.p_entry[tk->i_idxposc].i_lengthtotal;
1558 return AVI_GetDPTS( tk, i_count + tk->i_idxposb );
1562 if( tk->i_cat == AUDIO_ES )
1564 return AVI_GetDPTS( tk, tk->i_blockno );
1568 return AVI_GetDPTS( tk, tk->i_idxposc );
1573 static int AVI_StreamChunkFind( demux_t *p_demux, unsigned int i_stream )
1575 demux_sys_t *p_sys = p_demux->p_sys;
1576 avi_packet_t avi_pk;
1577 int i_loop_count = 0;
1579 /* find first chunk of i_stream that isn't in index */
1581 if( p_sys->i_movi_lastchunk_pos >= p_sys->i_movi_begin + 12 )
1583 stream_Seek( p_demux->s, p_sys->i_movi_lastchunk_pos );
1584 if( AVI_PacketNext( p_demux ) )
1586 return VLC_EGENERIC;
1591 stream_Seek( p_demux->s, p_sys->i_movi_begin + 12 );
1596 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1598 if( AVI_PacketGetHeader( p_demux, &avi_pk ) )
1600 msg_Warn( p_demux, "cannot get packet header" );
1601 return VLC_EGENERIC;
1603 if( avi_pk.i_stream >= p_sys->i_track ||
1604 ( avi_pk.i_cat != AUDIO_ES && avi_pk.i_cat != VIDEO_ES ) )
1606 if( AVI_PacketNext( p_demux ) )
1608 return VLC_EGENERIC;
1611 /* Prevents from eating all the CPU with broken files.
1612 * This value should be low enough so that it doesn't
1613 * affect the reading speed too much. */
1614 if( !(++i_loop_count % 1024) )
1616 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
1619 if( !(i_loop_count % (1024 * 10)) )
1620 msg_Warn( p_demux, "don't seem to find any data..." );
1625 avi_track_t *tk_pk = p_sys->track[avi_pk.i_stream];
1627 /* add this chunk to the index */
1629 index.i_id = avi_pk.i_fourcc;
1630 index.i_flags = AVI_GetKeyFlag(tk_pk->i_codec, avi_pk.i_peek);
1631 index.i_pos = avi_pk.i_pos;
1632 index.i_length = avi_pk.i_size;
1633 avi_index_Append( &tk_pk->idx, &p_sys->i_movi_lastchunk_pos, &index );
1635 if( avi_pk.i_stream == i_stream )
1640 if( AVI_PacketNext( p_demux ) )
1642 return VLC_EGENERIC;
1648 /* be sure that i_ck will be a valid index entry */
1649 static int AVI_StreamChunkSet( demux_t *p_demux, unsigned int i_stream,
1652 demux_sys_t *p_sys = p_demux->p_sys;
1653 avi_track_t *p_stream = p_sys->track[i_stream];
1655 p_stream->i_idxposc = i_ck;
1656 p_stream->i_idxposb = 0;
1658 if( i_ck >= p_stream->idx.i_size )
1660 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1663 p_stream->i_idxposc++;
1664 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1666 return VLC_EGENERIC;
1669 } while( p_stream->i_idxposc < i_ck );
1675 /* XXX FIXME up to now, we assume that all chunk are one after one */
1676 static int AVI_StreamBytesSet( demux_t *p_demux,
1677 unsigned int i_stream,
1680 demux_sys_t *p_sys = p_demux->p_sys;
1681 avi_track_t *p_stream = p_sys->track[i_stream];
1683 if( ( p_stream->idx.i_size > 0 )
1684 &&( i_byte < p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_lengthtotal +
1685 p_stream->idx.p_entry[p_stream->idx.i_size - 1].i_length ) )
1687 /* index is valid to find the ck */
1688 /* uses dichototmie to be fast enougth */
1689 int i_idxposc = __MIN( p_stream->i_idxposc, p_stream->idx.i_size - 1 );
1690 int i_idxmax = p_stream->idx.i_size;
1694 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal > i_byte )
1696 i_idxmax = i_idxposc ;
1697 i_idxposc = ( i_idxmin + i_idxposc ) / 2 ;
1701 if( p_stream->idx.p_entry[i_idxposc].i_lengthtotal +
1702 p_stream->idx.p_entry[i_idxposc].i_length <= i_byte)
1704 i_idxmin = i_idxposc ;
1705 i_idxposc = (i_idxmax + i_idxposc ) / 2 ;
1709 p_stream->i_idxposc = i_idxposc;
1710 p_stream->i_idxposb = i_byte -
1711 p_stream->idx.p_entry[i_idxposc].i_lengthtotal;
1720 p_stream->i_idxposc = p_stream->idx.i_size - 1;
1721 p_stream->i_idxposb = 0;
1724 p_stream->i_idxposc++;
1725 if( AVI_StreamChunkFind( p_demux, i_stream ) )
1727 return VLC_EGENERIC;
1730 } while( p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal +
1731 p_stream->idx.p_entry[p_stream->i_idxposc].i_length <= i_byte );
1733 p_stream->i_idxposb = i_byte -
1734 p_stream->idx.p_entry[p_stream->i_idxposc].i_lengthtotal;
1739 static int AVI_TrackSeek( demux_t *p_demux,
1743 demux_sys_t *p_sys = p_demux->p_sys;
1744 avi_track_t *tk = p_sys->track[i_stream];
1746 #define p_stream p_sys->track[i_stream]
1749 i_oldpts = AVI_GetPTS( p_stream );
1751 if( !p_stream->i_samplesize )
1753 if( AVI_StreamChunkSet( p_demux,
1755 AVI_PTSToChunk( p_stream, i_date ) ) )
1757 return VLC_EGENERIC;
1760 if( p_stream->i_cat == AUDIO_ES )
1764 for( i = 0; i < tk->i_idxposc; i++ )
1766 if( tk->i_blocksize > 0 )
1768 tk->i_blockno += ( tk->idx.p_entry[i].i_length + tk->i_blocksize - 1 ) / tk->i_blocksize;
1778 "old:%"PRId64" %s new %"PRId64,
1780 i_oldpts > i_date ? ">" : "<",
1783 if( p_stream->i_cat == VIDEO_ES )
1785 /* search key frame */
1786 //if( i_date < i_oldpts || 1 )
1788 while( p_stream->i_idxposc > 0 &&
1789 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1792 if( AVI_StreamChunkSet( p_demux,
1794 p_stream->i_idxposc - 1 ) )
1796 return VLC_EGENERIC;
1803 while( p_stream->i_idxposc < p_stream->idx.i_size &&
1804 !( p_stream->idx.p_entry[p_stream->i_idxposc].i_flags &
1807 if( AVI_StreamChunkSet( p_demux,
1809 p_stream->i_idxposc + 1 ) )
1811 return VLC_EGENERIC;
1820 if( AVI_StreamBytesSet( p_demux,
1822 AVI_PTSToByte( p_stream, i_date ) ) )
1824 return VLC_EGENERIC;
1831 /****************************************************************************
1832 * Return true if it's a key frame
1833 ****************************************************************************/
1834 static int AVI_GetKeyFlag( vlc_fourcc_t i_fourcc, uint8_t *p_byte )
1838 case VLC_CODEC_DIV1:
1840 * startcode: 0x00000100 32bits
1841 * framenumber ? 5bits
1842 * piture type 0(I),1(P) 2bits
1844 if( GetDWBE( p_byte ) != 0x00000100 )
1846 /* it's not an msmpegv1 stream, strange...*/
1847 return AVIIF_KEYFRAME;
1849 return p_byte[4] & 0x06 ? 0 : AVIIF_KEYFRAME;
1851 case VLC_CODEC_DIV2:
1852 case VLC_CODEC_DIV3:
1853 case VLC_CODEC_WMV1:
1855 * picture type 0(I),1(P) 2bits
1857 return p_byte[0] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1858 case VLC_CODEC_MP4V:
1859 /* we should find first occurrence of 0x000001b6 (32bits)
1860 * startcode: 0x000001b6 32bits
1861 * piture type 0(I),1(P) 2bits
1863 if( GetDWBE( p_byte ) != 0x000001b6 )
1865 /* not true , need to find the first VOP header */
1866 return AVIIF_KEYFRAME;
1868 return p_byte[4] & 0xC0 ? 0 : AVIIF_KEYFRAME;
1871 /* I can't do it, so say yes */
1872 return AVIIF_KEYFRAME;
1876 vlc_fourcc_t AVI_FourccGetCodec( unsigned int i_cat, vlc_fourcc_t i_codec )
1881 wf_tag_to_fourcc( i_codec, &i_codec, NULL );
1884 return vlc_fourcc_GetCodec( i_cat, i_codec );
1886 return VLC_FOURCC( 'u', 'n', 'd', 'f' );
1890 /****************************************************************************
1892 ****************************************************************************/
1893 static void AVI_ParseStreamHeader( vlc_fourcc_t i_id,
1894 unsigned int *pi_number, unsigned int *pi_type )
1896 #define SET_PTR( p, v ) if( p ) *(p) = (v);
1899 c1 = ((uint8_t *)&i_id)[0];
1900 c2 = ((uint8_t *)&i_id)[1];
1902 if( c1 < '0' || c1 > '9' || c2 < '0' || c2 > '9' )
1904 SET_PTR( pi_number, 100 ); /* > max stream number */
1905 SET_PTR( pi_type, UNKNOWN_ES );
1909 SET_PTR( pi_number, (c1 - '0') * 10 + (c2 - '0' ) );
1910 switch( VLC_TWOCC( ((uint8_t *)&i_id)[2], ((uint8_t *)&i_id)[3] ) )
1913 SET_PTR( pi_type, AUDIO_ES );
1918 SET_PTR( pi_type, VIDEO_ES );
1921 SET_PTR( pi_type, SPU_ES );
1924 SET_PTR( pi_type, UNKNOWN_ES );
1931 /****************************************************************************
1933 ****************************************************************************/
1934 static int AVI_PacketGetHeader( demux_t *p_demux, avi_packet_t *p_pk )
1936 const uint8_t *p_peek;
1938 if( stream_Peek( p_demux->s, &p_peek, 16 ) < 16 )
1940 return VLC_EGENERIC;
1942 p_pk->i_fourcc = VLC_FOURCC( p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
1943 p_pk->i_size = GetDWLE( p_peek + 4 );
1944 p_pk->i_pos = stream_Tell( p_demux->s );
1945 if( p_pk->i_fourcc == AVIFOURCC_LIST || p_pk->i_fourcc == AVIFOURCC_RIFF )
1947 p_pk->i_type = VLC_FOURCC( p_peek[8], p_peek[9],
1948 p_peek[10], p_peek[11] );
1955 memcpy( p_pk->i_peek, p_peek + 8, 8 );
1957 AVI_ParseStreamHeader( p_pk->i_fourcc, &p_pk->i_stream, &p_pk->i_cat );
1961 static int AVI_PacketNext( demux_t *p_demux )
1963 avi_packet_t avi_ck;
1966 if( AVI_PacketGetHeader( p_demux, &avi_ck ) )
1968 return VLC_EGENERIC;
1971 if( avi_ck.i_fourcc == AVIFOURCC_LIST &&
1972 ( avi_ck.i_type == AVIFOURCC_rec || avi_ck.i_type == AVIFOURCC_movi ) )
1976 else if( avi_ck.i_fourcc == AVIFOURCC_RIFF &&
1977 avi_ck.i_type == AVIFOURCC_AVIX )
1983 i_skip = __EVEN( avi_ck.i_size ) + 8;
1986 if( stream_Read( p_demux->s, NULL, i_skip ) != i_skip )
1988 return VLC_EGENERIC;
1993 static int AVI_PacketRead( demux_t *p_demux,
1995 block_t **pp_frame )
1999 i_size = __EVEN( p_pk->i_size + 8 );
2001 if( ( *pp_frame = stream_Block( p_demux->s, i_size ) ) == NULL )
2003 return VLC_EGENERIC;
2005 (*pp_frame)->p_buffer += 8;
2006 (*pp_frame)->i_buffer -= 8;
2008 if( i_size != p_pk->i_size + 8 )
2010 (*pp_frame)->i_buffer--;
2016 static int AVI_PacketSearch( demux_t *p_demux )
2018 demux_sys_t *p_sys = p_demux->p_sys;
2019 avi_packet_t avi_pk;
2024 if( stream_Read( p_demux->s, NULL, 1 ) != 1 )
2026 return VLC_EGENERIC;
2028 AVI_PacketGetHeader( p_demux, &avi_pk );
2029 if( avi_pk.i_stream < p_sys->i_track &&
2030 ( avi_pk.i_cat == AUDIO_ES || avi_pk.i_cat == VIDEO_ES ) )
2034 switch( avi_pk.i_fourcc )
2036 case AVIFOURCC_JUNK:
2037 case AVIFOURCC_LIST:
2038 case AVIFOURCC_RIFF:
2039 case AVIFOURCC_idx1:
2043 /* Prevents from eating all the CPU with broken files.
2044 * This value should be low enough so that it doesn't affect the
2045 * reading speed too much (not that we care much anyway because
2046 * this code is called only on broken files). */
2047 if( !(++i_count % 1024) )
2049 if( !vlc_object_alive (p_demux) ) return VLC_EGENERIC;
2052 if( !(i_count % (1024 * 10)) )
2053 msg_Warn( p_demux, "trying to resync..." );
2058 /****************************************************************************
2060 ****************************************************************************/
2061 static void avi_index_Init( avi_index_t *p_index )
2063 p_index->i_size = 0;
2065 p_index->p_entry = NULL;
2067 static void avi_index_Clean( avi_index_t *p_index )
2069 free( p_index->p_entry );
2071 static void avi_index_Append( avi_index_t *p_index, off_t *pi_last_pos,
2072 avi_entry_t *p_entry )
2074 /* Update last chunk position */
2075 if( *pi_last_pos < p_entry->i_pos )
2076 *pi_last_pos = p_entry->i_pos;
2079 if( p_index->i_size >= p_index->i_max )
2081 p_index->i_max += 16384;
2082 p_index->p_entry = realloc_or_free( p_index->p_entry,
2083 p_index->i_max * sizeof( *p_index->p_entry ) );
2084 if( !p_index->p_entry )
2087 /* calculate cumulate length */
2088 if( p_index->i_size > 0 )
2090 p_entry->i_lengthtotal =
2091 p_index->p_entry[p_index->i_size - 1].i_length +
2092 p_index->p_entry[p_index->i_size - 1].i_lengthtotal;
2096 p_entry->i_lengthtotal = 0;
2099 p_index->p_entry[p_index->i_size++] = *p_entry;
2102 static int AVI_IndexFind_idx1( demux_t *p_demux,
2103 avi_chunk_idx1_t **pp_idx1,
2104 uint64_t *pi_offset )
2106 demux_sys_t *p_sys = p_demux->p_sys;
2108 avi_chunk_list_t *p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2109 avi_chunk_idx1_t *p_idx1 = AVI_ChunkFind( p_riff, AVIFOURCC_idx1, 0);
2113 msg_Warn( p_demux, "cannot find idx1 chunk, no index defined" );
2114 return VLC_EGENERIC;
2118 /* *** calculate offset *** */
2119 /* Well, avi is __SHIT__ so test more than one entry
2120 * (needed for some avi files) */
2121 avi_chunk_list_t *p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2123 for( unsigned i = 0; i < __MIN( p_idx1->i_entry_count, 10 ); i++ )
2125 if( p_idx1->entry[i].i_pos < p_movi->i_chunk_pos )
2127 *pi_offset = p_movi->i_chunk_pos + 8;
2134 static int AVI_IndexLoad_idx1( demux_t *p_demux,
2135 avi_index_t p_index[], off_t *pi_last_offset )
2137 demux_sys_t *p_sys = p_demux->p_sys;
2139 avi_chunk_idx1_t *p_idx1;
2141 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2142 return VLC_EGENERIC;
2144 for( unsigned i_index = 0; i_index < p_idx1->i_entry_count; i_index++ )
2149 AVI_ParseStreamHeader( p_idx1->entry[i_index].i_fourcc,
2152 if( i_stream < p_sys->i_track &&
2153 i_cat == p_sys->track[i_stream]->i_cat )
2156 index.i_id = p_idx1->entry[i_index].i_fourcc;
2157 index.i_flags = p_idx1->entry[i_index].i_flags&(~AVIIF_FIXKEYFRAME);
2158 index.i_pos = p_idx1->entry[i_index].i_pos + i_offset;
2159 index.i_length = p_idx1->entry[i_index].i_length;
2161 avi_index_Append( &p_index[i_stream], pi_last_offset, &index );
2167 static void __Parse_indx( demux_t *p_demux, avi_index_t *p_index, off_t *pi_max_offset,
2168 avi_chunk_indx_t *p_indx )
2172 msg_Dbg( p_demux, "loading subindex(0x%x) %d entries", p_indx->i_indextype, p_indx->i_entriesinuse );
2173 if( p_indx->i_indexsubtype == 0 )
2175 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2177 index.i_id = p_indx->i_id;
2178 index.i_flags = p_indx->idx.std[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2179 index.i_pos = p_indx->i_baseoffset + p_indx->idx.std[i].i_offset - 8;
2180 index.i_length = p_indx->idx.std[i].i_size&0x7fffffff;
2182 avi_index_Append( p_index, pi_max_offset, &index );
2185 else if( p_indx->i_indexsubtype == AVI_INDEX_2FIELD )
2187 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2189 index.i_id = p_indx->i_id;
2190 index.i_flags = p_indx->idx.field[i].i_size & 0x80000000 ? 0 : AVIIF_KEYFRAME;
2191 index.i_pos = p_indx->i_baseoffset + p_indx->idx.field[i].i_offset - 8;
2192 index.i_length = p_indx->idx.field[i].i_size;
2194 avi_index_Append( p_index, pi_max_offset, &index );
2199 msg_Warn( p_demux, "unknown subtype index(0x%x)", p_indx->i_indexsubtype );
2203 static void AVI_IndexLoad_indx( demux_t *p_demux,
2204 avi_index_t p_index[], off_t *pi_last_offset )
2206 demux_sys_t *p_sys = p_demux->p_sys;
2208 avi_chunk_list_t *p_riff;
2209 avi_chunk_list_t *p_hdrl;
2211 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2212 p_hdrl = AVI_ChunkFind( p_riff, AVIFOURCC_hdrl, 0 );
2214 for( unsigned i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2216 avi_chunk_list_t *p_strl;
2217 avi_chunk_indx_t *p_indx;
2219 #define p_stream p_sys->track[i_stream]
2220 p_strl = AVI_ChunkFind( p_hdrl, AVIFOURCC_strl, i_stream );
2221 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2226 msg_Warn( p_demux, "cannot find indx (misdetect/broken OpenDML "
2231 if( p_indx->i_indextype == AVI_INDEX_OF_CHUNKS )
2233 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, p_indx );
2235 else if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES )
2238 for( unsigned i = 0; i < p_indx->i_entriesinuse; i++ )
2240 if( stream_Seek( p_demux->s, p_indx->idx.super[i].i_offset )||
2241 AVI_ChunkRead( p_demux->s, &ck_sub, NULL ) )
2245 if( ck_sub.indx.i_indextype == AVI_INDEX_OF_CHUNKS )
2246 __Parse_indx( p_demux, &p_index[i_stream], pi_last_offset, &ck_sub.indx );
2247 AVI_ChunkFree( p_demux->s, &ck_sub );
2252 msg_Warn( p_demux, "unknown type index(0x%x)", p_indx->i_indextype );
2258 static void AVI_IndexLoad( demux_t *p_demux )
2260 demux_sys_t *p_sys = p_demux->p_sys;
2263 assert( p_sys->i_track <= 100 );
2264 avi_index_t p_idx_indx[p_sys->i_track];
2265 avi_index_t p_idx_idx1[p_sys->i_track];
2266 for( unsigned i = 0; i < p_sys->i_track; i++ )
2268 avi_index_Init( &p_idx_indx[i] );
2269 avi_index_Init( &p_idx_idx1[i] );
2271 off_t i_indx_last_pos = p_sys->i_movi_lastchunk_pos;
2272 off_t i_idx1_last_pos = p_sys->i_movi_lastchunk_pos;
2274 AVI_IndexLoad_indx( p_demux, p_idx_indx, &i_indx_last_pos );
2275 if( !p_sys->b_odml )
2276 AVI_IndexLoad_idx1( p_demux, p_idx_idx1, &i_idx1_last_pos );
2278 /* Select the longest index */
2279 for( unsigned i = 0; i < p_sys->i_track; i++ )
2281 if( p_idx_indx[i].i_size > p_idx_idx1[i].i_size )
2283 msg_Dbg( p_demux, "selected ODML index for stream[%u]", i );
2284 p_sys->track[i]->idx = p_idx_indx[i];
2285 avi_index_Clean( &p_idx_idx1[i] );
2289 msg_Dbg( p_demux, "selected standard index for stream[%u]", i );
2290 p_sys->track[i]->idx = p_idx_idx1[i];
2291 avi_index_Clean( &p_idx_indx[i] );
2294 p_sys->i_movi_lastchunk_pos = __MAX( i_indx_last_pos, i_idx1_last_pos );
2296 for( unsigned i = 0; i < p_sys->i_track; i++ )
2298 avi_index_t *p_index = &p_sys->track[i]->idx;
2302 for( unsigned j = 0; !b_key && j < p_index->i_size; j++ )
2303 b_key = p_index->p_entry[j].i_flags & AVIIF_KEYFRAME;
2306 msg_Err( p_demux, "no key frame set for track %u", i );
2307 for( unsigned j = 0; j < p_index->i_size; j++ )
2308 p_index->p_entry[j].i_flags |= AVIIF_KEYFRAME;
2312 msg_Dbg( p_demux, "stream[%d] created %d index entries",
2313 i, p_index->i_size );
2317 static void AVI_IndexCreate( demux_t *p_demux )
2319 demux_sys_t *p_sys = p_demux->p_sys;
2321 avi_chunk_list_t *p_riff;
2322 avi_chunk_list_t *p_movi;
2324 unsigned int i_stream;
2327 mtime_t i_dialog_update;
2328 dialog_progress_bar_t *p_dialog = NULL;
2330 p_riff = AVI_ChunkFind( &p_sys->ck_root, AVIFOURCC_RIFF, 0);
2331 p_movi = AVI_ChunkFind( p_riff, AVIFOURCC_movi, 0);
2335 msg_Err( p_demux, "cannot find p_movi" );
2339 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2340 avi_index_Init( &p_sys->track[i_stream]->idx );
2342 i_movi_end = __MIN( (off_t)(p_movi->i_chunk_pos + p_movi->i_chunk_size),
2343 stream_Size( p_demux->s ) );
2345 stream_Seek( p_demux->s, p_movi->i_chunk_pos + 12 );
2346 msg_Warn( p_demux, "creating index from LIST-movi, will take time !" );
2349 /* Only show dialog if AVI is > 10MB */
2350 i_dialog_update = mdate();
2351 if( stream_Size( p_demux->s ) > 10000000 )
2352 p_dialog = dialog_ProgressCreate( p_demux, _("Fixing AVI Index..."),
2353 NULL, _("Cancel") );
2359 if( !vlc_object_alive (p_demux) )
2362 /* Don't update/check dialog too often */
2363 if( p_dialog && mdate() - i_dialog_update > 100000 )
2365 if( dialog_ProgressCancelled( p_dialog ) )
2368 double f_current = stream_Tell( p_demux->s );
2369 double f_size = stream_Size( p_demux->s );
2370 double f_pos = f_current / f_size;
2371 dialog_ProgressSet( p_dialog, NULL, f_pos );
2373 i_dialog_update = mdate();
2376 if( AVI_PacketGetHeader( p_demux, &pk ) )
2379 if( pk.i_stream < p_sys->i_track &&
2380 pk.i_cat == p_sys->track[pk.i_stream]->i_cat )
2382 avi_track_t *tk = p_sys->track[pk.i_stream];
2385 index.i_id = pk.i_fourcc;
2386 index.i_flags = AVI_GetKeyFlag(tk->i_codec, pk.i_peek);
2387 index.i_pos = pk.i_pos;
2388 index.i_length = pk.i_size;
2389 avi_index_Append( &tk->idx, &p_sys->i_movi_lastchunk_pos, &index );
2393 switch( pk.i_fourcc )
2395 case AVIFOURCC_idx1:
2398 avi_chunk_list_t *p_sysx;
2399 p_sysx = AVI_ChunkFind( &p_sys->ck_root,
2400 AVIFOURCC_RIFF, 1 );
2402 msg_Dbg( p_demux, "looking for new RIFF chunk" );
2403 if( stream_Seek( p_demux->s, p_sysx->i_chunk_pos + 24 ) )
2409 case AVIFOURCC_RIFF:
2410 msg_Dbg( p_demux, "new RIFF chunk found" );
2414 case AVIFOURCC_JUNK:
2418 msg_Warn( p_demux, "need resync, probably broken avi" );
2419 if( AVI_PacketSearch( p_demux ) )
2421 msg_Warn( p_demux, "lost sync, abord index creation" );
2427 if( ( !p_sys->b_odml && pk.i_pos + pk.i_size >= i_movi_end ) ||
2428 AVI_PacketNext( p_demux ) )
2435 if( p_dialog != NULL )
2436 dialog_ProgressDestroy( p_dialog );
2438 for( i_stream = 0; i_stream < p_sys->i_track; i_stream++ )
2440 msg_Dbg( p_demux, "stream[%d] creating %d index entries",
2441 i_stream, p_sys->track[i_stream]->idx.i_size );
2446 static void AVI_MetaLoad( demux_t *p_demux,
2447 avi_chunk_list_t *p_riff, avi_chunk_avih_t *p_avih )
2449 demux_sys_t *p_sys = p_demux->p_sys;
2451 vlc_meta_t *p_meta = p_sys->meta = vlc_meta_New();
2456 snprintf( buffer, sizeof(buffer), "%s%s%s%s",
2457 p_avih->i_flags&AVIF_HASINDEX ? " HAS_INDEX" : "",
2458 p_avih->i_flags&AVIF_MUSTUSEINDEX ? " MUST_USE_INDEX" : "",
2459 p_avih->i_flags&AVIF_ISINTERLEAVED ? " IS_INTERLEAVED" : "",
2460 p_avih->i_flags&AVIF_TRUSTCKTYPE ? " TRUST_CKTYPE" : "" );
2461 vlc_meta_SetSetting( p_meta, buffer );
2463 avi_chunk_list_t *p_info = AVI_ChunkFind( p_riff, AVIFOURCC_INFO, 0 );
2467 static const struct {
2471 { AVIFOURCC_IART, vlc_meta_Artist },
2472 { AVIFOURCC_ICMT, vlc_meta_Description },
2473 { AVIFOURCC_ICOP, vlc_meta_Copyright },
2474 { AVIFOURCC_IGNR, vlc_meta_Genre },
2475 { AVIFOURCC_INAM, vlc_meta_Title },
2478 for( int i = 0; p_dsc[i].i_id != 0; i++ )
2480 avi_chunk_STRING_t *p_strz = AVI_ChunkFind( p_info, p_dsc[i].i_id, 0 );
2483 char *psz_value = FromLatin1( p_strz->p_str );
2488 vlc_meta_Set( p_meta, p_dsc[i].i_type, psz_value );
2493 /*****************************************************************************
2495 *****************************************************************************/
2496 static void AVI_ExtractSubtitle( demux_t *p_demux,
2498 avi_chunk_list_t *p_strl,
2499 avi_chunk_STRING_t *p_strn )
2501 demux_sys_t *p_sys = p_demux->p_sys;
2502 block_t *p_block = NULL;
2503 input_attachment_t *p_attachment = NULL;
2504 char *psz_description = NULL;
2505 avi_chunk_indx_t *p_indx = NULL;
2507 if( !p_sys->b_seekable )
2510 p_indx = AVI_ChunkFind( p_strl, AVIFOURCC_indx, 0 );
2516 if( p_indx->i_indextype == AVI_INDEX_OF_INDEXES &&
2517 p_indx->i_entriesinuse > 0 )
2519 if( stream_Seek( p_demux->s, p_indx->idx.super[0].i_offset )||
2520 AVI_ChunkRead( p_demux->s, &ck, NULL ) )
2525 if( p_indx->i_indextype != AVI_INDEX_OF_CHUNKS ||
2526 p_indx->i_entriesinuse != 1 ||
2527 p_indx->i_indexsubtype != 0 )
2530 i_position = p_indx->i_baseoffset +
2531 p_indx->idx.std[0].i_offset - 8;
2532 i_size = (p_indx->idx.std[0].i_size & 0x7fffffff) + 8;
2536 avi_chunk_idx1_t *p_idx1;
2539 if( AVI_IndexFind_idx1( p_demux, &p_idx1, &i_offset ) )
2543 for( unsigned i = 0; i < p_idx1->i_entry_count; i++ )
2545 const idx1_entry_t *e = &p_idx1->entry[i];
2547 unsigned i_stream_idx;
2549 AVI_ParseStreamHeader( e->i_fourcc, &i_stream_idx, &i_cat );
2550 if( i_cat == SPU_ES && i_stream_idx == i_stream )
2552 i_position = e->i_pos + i_offset;
2553 i_size = e->i_length + 8;
2562 if( i_size > 1000000 )
2565 if( stream_Seek( p_demux->s, i_position ) )
2567 p_block = stream_Block( p_demux->s, i_size );
2571 /* Parse packet header */
2572 const uint8_t *p = p_block->p_buffer;
2573 if( i_size < 8 || p[2] != 't' || p[3] != 'x' )
2578 /* Parse subtitle chunk header */
2579 if( i_size < 11 || memcmp( p, "GAB2", 4 ) ||
2580 p[4] != 0x00 || GetWLE( &p[5] ) != 0x2 )
2582 const unsigned i_name = GetDWLE( &p[7] );
2583 if( 11 + i_size <= i_name )
2586 psz_description = FromCharset( "UTF-16LE", &p[11], i_name );
2588 i_size -= 11 + i_name;
2589 if( i_size < 6 || GetWLE( &p[0] ) != 0x04 )
2591 const unsigned i_payload = GetDWLE( &p[2] );
2592 if( i_size < 6 + i_payload || i_payload <= 0 )
2597 if( !psz_description )
2598 psz_description = p_strn ? FromLatin1( p_strn->p_str ) : NULL;
2600 if( asprintf( &psz_name, "subtitle%d.srt", p_sys->i_attachment ) <= 0 )
2602 p_attachment = vlc_input_attachment_New( psz_name,
2603 "application/x-srt",
2607 TAB_APPEND( p_sys->i_attachment, p_sys->attachment, p_attachment );
2611 free( psz_description );
2614 block_Release( p_block );
2617 msg_Dbg( p_demux, "Loaded an embed subtitle" );
2619 msg_Warn( p_demux, "Failed to load an embed subtitle" );
2621 if( p_indx == &ck.indx )
2622 AVI_ChunkFree( p_demux->s, &ck );
2624 /*****************************************************************************
2626 *****************************************************************************/
2627 static int AVI_TrackStopFinishedStreams( demux_t *p_demux )
2629 demux_sys_t *p_sys = p_demux->p_sys;
2633 for( i = 0; i < p_sys->i_track; i++ )
2635 avi_track_t *tk = p_sys->track[i];
2636 if( tk->i_idxposc >= tk->idx.i_size )
2648 /****************************************************************************
2649 * AVI_MovieGetLength give max streams length in second
2650 ****************************************************************************/
2651 static mtime_t AVI_MovieGetLength( demux_t *p_demux )
2653 demux_sys_t *p_sys = p_demux->p_sys;
2654 mtime_t i_maxlength = 0;
2657 for( i = 0; i < p_sys->i_track; i++ )
2659 avi_track_t *tk = p_sys->track[i];
2662 /* fix length for each stream */
2663 if( tk->idx.i_size < 1 || !tk->idx.p_entry )
2668 if( tk->i_samplesize )
2670 i_length = AVI_GetDPTS( tk,
2671 tk->idx.p_entry[tk->idx.i_size-1].i_lengthtotal +
2672 tk->idx.p_entry[tk->idx.i_size-1].i_length );
2676 i_length = AVI_GetDPTS( tk, tk->idx.i_size );
2678 i_length /= (mtime_t)1000000; /* in seconds */
2681 "stream[%d] length:%"PRId64" (based on index)",
2684 i_maxlength = __MAX( i_maxlength, i_length );