1 /*****************************************************************************
2 * mkv.cpp : matroska demuxer
3 *****************************************************************************
4 * Copyright (C) 2003-2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Steve Lhomme <steve.lhomme@free.fr>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
33 # include <time.h> /* time() */
36 #include <vlc/input.h>
38 #include <codecs.h> /* BITMAPINFOHEADER, WAVEFORMATEX */
53 /* libebml and matroska */
54 #include "ebml/EbmlHead.h"
55 #include "ebml/EbmlSubHead.h"
56 #include "ebml/EbmlStream.h"
57 #include "ebml/EbmlContexts.h"
58 #include "ebml/EbmlVoid.h"
59 #include "ebml/EbmlVersion.h"
60 #include "ebml/StdIOCallback.h"
62 #include "matroska/KaxAttachments.h"
63 #include "matroska/KaxBlock.h"
64 #include "matroska/KaxBlockData.h"
65 #include "matroska/KaxChapters.h"
66 #include "matroska/KaxCluster.h"
67 #include "matroska/KaxClusterData.h"
68 #include "matroska/KaxContexts.h"
69 #include "matroska/KaxCues.h"
70 #include "matroska/KaxCuesData.h"
71 #include "matroska/KaxInfo.h"
72 #include "matroska/KaxInfoData.h"
73 #include "matroska/KaxSeekHead.h"
74 #include "matroska/KaxSegment.h"
75 #include "matroska/KaxTag.h"
76 #include "matroska/KaxTags.h"
77 #include "matroska/KaxTagMulti.h"
78 #include "matroska/KaxTracks.h"
79 #include "matroska/KaxTrackAudio.h"
80 #include "matroska/KaxTrackVideo.h"
81 #include "matroska/KaxTrackEntryData.h"
82 #include "matroska/KaxContentEncoding.h"
84 #include "ebml/StdIOCallback.h"
87 #include "mp4/libmp4.h"
93 #define MATROSKA_COMPRESSION_NONE 0
94 #define MATROSKA_COMPRESSION_ZLIB 1
96 #define MKVD_TIMECODESCALE 1000000
99 * What's between a directory and a filename?
102 #define DIRECTORY_SEPARATOR '\\'
104 #define DIRECTORY_SEPARATOR '/'
107 using namespace LIBMATROSKA_NAMESPACE;
110 /*****************************************************************************
112 *****************************************************************************/
113 static int Open ( vlc_object_t * );
114 static void Close( vlc_object_t * );
117 set_shortname( _("Matroska") );
118 set_description( _("Matroska stream demuxer" ) );
119 set_capability( "demux2", 50 );
120 set_callbacks( Open, Close );
121 set_category( CAT_INPUT );
122 set_subcategory( SUBCAT_INPUT_DEMUX );
124 add_bool( "mkv-seek-percent", 1, NULL,
125 N_("Seek based on percent not time"),
126 N_("Seek based on percent not time"), VLC_TRUE );
128 add_shortcut( "mka" );
129 add_shortcut( "mkv" );
132 /*****************************************************************************
134 *****************************************************************************/
136 block_t *block_zlib_decompress( vlc_object_t *p_this, block_t *p_in_block ) {
137 int result, dstsize, n;
142 d_stream.zalloc = (alloc_func)0;
143 d_stream.zfree = (free_func)0;
144 d_stream.opaque = (voidpf)0;
145 result = inflateInit(&d_stream);
148 msg_Dbg( p_this, "inflateInit() failed. Result: %d", result );
152 d_stream.next_in = (Bytef *)p_in_block->p_buffer;
153 d_stream.avail_in = p_in_block->i_buffer;
155 p_block = block_New( p_this, 0 );
160 p_block = block_Realloc( p_block, 0, n * 1000 );
161 dst = (unsigned char *)p_block->p_buffer;
162 d_stream.next_out = (Bytef *)&dst[(n - 1) * 1000];
163 d_stream.avail_out = 1000;
164 result = inflate(&d_stream, Z_NO_FLUSH);
165 if( ( result != Z_OK ) && ( result != Z_STREAM_END ) )
167 msg_Dbg( p_this, "Zlib decompression failed. Result: %d", result );
171 while( ( d_stream.avail_out == 0 ) && ( d_stream.avail_in != 0 ) &&
172 ( result != Z_STREAM_END ) );
174 dstsize = d_stream.total_out;
175 inflateEnd( &d_stream );
177 p_block = block_Realloc( p_block, 0, dstsize );
178 p_block->i_buffer = dstsize;
179 block_Release( p_in_block );
186 * Helper function to print the mkv parse tree
188 static void MkvTree( demux_t & demuxer, int i_level, char *psz_format, ... )
193 msg_Err( &demuxer, "too deep tree" );
196 va_start( args, psz_format );
197 static char *psz_foo = "| | | | | | | | | |";
198 char *psz_foo2 = (char*)malloc( ( i_level * 4 + 3 + strlen( psz_format ) ) * sizeof(char) );
199 strncpy( psz_foo2, psz_foo, 4 * i_level );
200 psz_foo2[ 4 * i_level ] = '+';
201 psz_foo2[ 4 * i_level + 1 ] = ' ';
202 strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
203 __msg_GenericVa( VLC_OBJECT(&demuxer), VLC_MSG_DBG, "mkv", psz_foo2, args );
208 /*****************************************************************************
210 *****************************************************************************/
211 class vlc_stream_io_callback: public IOCallback
218 vlc_stream_io_callback( stream_t * );
220 virtual uint32 read ( void *p_buffer, size_t i_size);
221 virtual void setFilePointer ( int64_t i_offset, seek_mode mode = seek_beginning );
222 virtual size_t write ( const void *p_buffer, size_t i_size);
223 virtual uint64 getFilePointer ( void );
224 virtual void close ( void );
227 /*****************************************************************************
229 *****************************************************************************/
233 EbmlParser( EbmlStream *es, EbmlElement *el_start );
239 EbmlElement *Get( void );
242 int GetLevel( void );
247 EbmlElement *m_el[10];
256 /*****************************************************************************
257 * Some functions to manipulate memory
258 *****************************************************************************/
259 #define GetFOURCC( p ) __GetFOURCC( (uint8_t*)p )
260 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
262 return VLC_FOURCC( p[0], p[1], p[2], p[3] );
265 /*****************************************************************************
266 * definitions of structures and functions used by this plugins
267 *****************************************************************************/
270 vlc_bool_t b_default;
271 vlc_bool_t b_enabled;
275 uint8_t *p_extra_data;
279 uint64_t i_default_duration;
280 float f_timecodescale;
288 /* data to be send first */
290 uint8_t *p_data_init;
292 /* hack : it's for seek */
293 vlc_bool_t b_search_keyframe;
297 char *psz_codec_name;
298 char *psz_codec_settings;
299 char *psz_codec_info_url;
300 char *psz_codec_download_url;
302 /* encryption/compression */
303 int i_compression_type;
324 ,i_user_start_time(-1)
327 ,b_display_seekpoint(true)
331 int64_t RefreshChapters( bool b_ordered, int64_t i_prev_user_time, input_title_t & title );
332 const chapter_item_t * FindTimecode( mtime_t i_timecode ) const;
334 int64_t i_start_time, i_end_time;
335 int64_t i_user_start_time, i_user_end_time; /* the time in the stream when an edition is ordered */
336 std::vector<chapter_item_t> sub_chapters;
339 bool b_display_seekpoint;
340 std::string psz_name;
341 chapter_item_t *psz_parent;
343 bool operator<( const chapter_item_t & item ) const
345 return ( i_user_start_time < item.i_user_start_time || (i_user_start_time == item.i_user_start_time && i_user_end_time < item.i_user_end_time) );
353 class chapter_edition_t
361 void RefreshChapters( input_title_t & title );
362 double Duration() const;
363 const chapter_item_t * FindTimecode( mtime_t i_timecode ) const;
365 std::vector<chapter_item_t> chapters;
372 class matroska_segment_t
375 matroska_segment_t( demux_sys_t & demuxer, EbmlStream & estream )
378 ,i_timescale(MKVD_TIMECODESCALE)
381 ,i_chapters_position(-1)
387 ,psz_muxing_application(NULL)
388 ,psz_writing_application(NULL)
389 ,psz_segment_filename(NULL)
392 ,i_current_edition(-1)
393 ,psz_current_chapter(NULL)
398 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
401 ~matroska_segment_t()
403 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
405 #define tk tracks[i_track]
406 if( tk->fmt.psz_description )
408 free( tk->fmt.psz_description );
412 free( tk->psz_codec );
414 if( tk->fmt.psz_language )
416 free( tk->fmt.psz_language );
422 if( psz_writing_application )
424 free( psz_writing_application );
426 if( psz_muxing_application )
428 free( psz_muxing_application );
430 if( psz_segment_filename )
432 free( psz_segment_filename );
440 free( psz_date_utc );
452 uint64_t i_timescale;
454 /* duration of the segment */
458 std::vector<mkv_track_t*> tracks;
461 int64_t i_cues_position;
462 int64_t i_chapters_position;
463 int64_t i_tags_position;
466 KaxSegmentUID segment_uid;
467 KaxPrevUID prev_segment_uid;
468 KaxNextUID next_segment_uid;
476 char *psz_muxing_application;
477 char *psz_writing_application;
478 char *psz_segment_filename;
482 std::vector<chapter_edition_t> editions;
483 int i_current_edition;
484 const chapter_item_t *psz_current_chapter;
486 std::vector<KaxSegmentFamily> families;
492 inline chapter_edition_t *Edition()
494 if ( i_current_edition >= 0 && size_t(i_current_edition) < editions.size() )
495 return &editions[i_current_edition];
500 bool PreloadFamily( const matroska_segment_t & segment );
501 size_t PreloadLinked( const demux_sys_t & of_sys );
502 void ParseInfo( EbmlElement *info );
503 void ParseChapters( EbmlElement *chapters );
504 void ParseSeekHead( EbmlElement *seekhead );
505 void ParseTracks( EbmlElement *tracks );
506 void ParseChapterAtom( int i_level, EbmlMaster *ca, chapter_item_t & chapters );
507 void ParseTrackEntry( EbmlMaster *m );
508 void IndexAppendCluster( KaxCluster *cluster );
509 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
512 class matroska_stream_t
515 matroska_stream_t( demux_sys_t & demuxer )
518 ,i_current_segment(-1)
524 for ( size_t i=0; i<segments.size(); i++ )
533 std::vector<matroska_segment_t*> segments;
534 int i_current_segment;
538 inline matroska_segment_t *Segment()
540 if ( i_current_segment >= 0 && size_t(i_current_segment) < segments.size() )
541 return segments[i_current_segment];
545 matroska_segment_t *FindSegment( EbmlBinary & uid ) const;
547 void PreloadFamily( const matroska_segment_t & segment );
548 size_t PreloadLinked( const demux_sys_t & of_sys );
554 demux_sys_t( demux_t & demux )
561 ,i_current_stream(-1)
566 for (size_t i=0; i<streams.size(); i++)
575 mtime_t i_chapter_time;
579 input_title_t *title;
581 std::vector<matroska_stream_t*> streams;
582 int i_current_stream;
584 inline matroska_stream_t *Stream()
586 if ( i_current_stream >= 0 && size_t(i_current_stream) < streams.size() )
587 return streams[i_current_stream];
591 matroska_segment_t *FindSegment( EbmlBinary & uid ) const;
592 void PreloadFamily( );
593 void PreloadLinked( );
594 matroska_stream_t *AnalyseAllSegmentsFound( EbmlStream *p_estream );
597 static int Demux ( demux_t * );
598 static int Control( demux_t *, int, va_list );
599 static void Seek ( demux_t *, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter );
601 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
603 static char *UTF8ToStr ( const UTFstring &u );
604 static void LoadCues ( demux_t * );
605 static void InformationCreate ( demux_t * );
607 /*****************************************************************************
608 * Open: initializes matroska demux structures
609 *****************************************************************************/
610 static int Open( vlc_object_t * p_this )
612 demux_t *p_demux = (demux_t*)p_this;
614 matroska_stream_t *p_stream;
615 matroska_segment_t *p_segment;
617 std::string s_path, s_filename;
619 vlc_stream_io_callback *p_io_callback;
620 EbmlStream *p_io_stream;
622 /* peek the begining */
623 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
625 /* is a valid file */
626 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
627 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
629 /* Set the demux function */
630 p_demux->pf_demux = Demux;
631 p_demux->pf_control = Control;
632 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
634 p_io_callback = new vlc_stream_io_callback( p_demux->s );
635 p_io_stream = new EbmlStream( *p_io_callback );
637 if( p_io_stream == NULL )
639 msg_Err( p_demux, "failed to create EbmlStream" );
640 delete p_io_callback;
645 p_stream = p_sys->AnalyseAllSegmentsFound( p_io_stream );
646 if( p_stream == NULL )
648 msg_Err( p_demux, "cannot find KaxSegment" );
651 p_sys->streams.push_back( p_stream );
652 p_sys->i_current_stream = 0;
654 p_stream->p_in = p_io_callback;
655 p_stream->p_es = p_io_stream;
657 for (size_t i=0; i<p_stream->segments.size(); i++)
659 p_stream->segments[i]->Preload();
661 p_stream->i_current_segment = 0;
663 p_segment = p_stream->Segment();
664 if( p_segment->cluster == NULL )
666 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
669 // reset the stream reading to the first cluster of the segment used
670 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
672 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
673 /* TODO handle multi-segment files */
674 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
676 // assume it's a regular file
677 // get the directory path
678 s_path = p_demux->psz_path;
679 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
681 s_path = s_path.substr(0,s_path.length()-1);
685 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
687 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
691 struct dirent *p_file_item;
692 DIR *p_src_dir = opendir(s_path.c_str());
694 if (p_src_dir != NULL)
696 while ((p_file_item = (dirent *) readdir(p_src_dir)))
698 if (strlen(p_file_item->d_name) > 4)
700 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
702 if (!s_filename.compare(p_demux->psz_path))
703 continue; // don't reuse the original opened file
705 #if defined(__GNUC__) && (__GNUC__ < 3)
706 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
707 !s_filename.compare("mka", s_filename.length() - 3, 3))
709 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
710 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
713 // test wether this file belongs to the our family
714 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
715 EbmlStream *p_estream = new EbmlStream(*p_file_io);
717 p_stream = p_sys->AnalyseAllSegmentsFound( p_estream );
718 if ( p_stream == NULL )
720 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
726 p_stream->p_in = p_file_io;
727 p_stream->p_es = p_estream;
728 p_sys->streams.push_back( p_stream );
733 closedir( p_src_dir );
737 p_sys->PreloadFamily( );
738 p_sys->PreloadLinked( );
740 /* *** Load the cue if found *** */
741 if( p_segment->i_cues_position >= 0 )
743 vlc_bool_t b_seekable;
745 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
752 if( !p_segment->b_cues || p_segment->i_index <= 0 )
754 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
756 p_segment->IndexAppendCluster( p_segment->cluster );
758 p_segment->b_cues = VLC_FALSE;
762 msg_Dbg( p_demux, "found %d es", p_segment->tracks.size() );
763 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
765 #define tk p_segment->tracks[i_track]
766 if( tk->fmt.i_cat == UNKNOWN_ES )
768 msg_Warn( p_demux, "invalid track[%d, n=%d]", i_track, tk->i_number );
773 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
775 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
777 msg_Err( p_demux, "missing/invalid BITMAPINFOHEADER" );
778 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
782 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
784 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
785 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
786 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
788 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
789 if( tk->fmt.i_extra > 0 )
791 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
792 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
796 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
797 !strcmp( tk->psz_codec, "V_MPEG2" ) )
799 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
801 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
803 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
805 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
807 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
809 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
810 tk->fmt.b_packetized = VLC_FALSE;
811 tk->fmt.i_extra = tk->i_extra_data;
812 tk->fmt.p_extra = malloc( tk->i_extra_data );
813 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
817 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
820 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
822 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
823 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(p_demux),
826 MP4_ReadBoxCommon( p_mp4_stream, p_box );
827 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
828 tk->fmt.i_codec = p_box->i_type;
829 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
830 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
831 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
832 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
833 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
834 MP4_FreeBox_sample_vide( p_box );
835 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
837 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
839 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
841 msg_Err( p_demux, "missing/invalid WAVEFORMATEX" );
842 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
846 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
848 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
850 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
851 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
852 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
853 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
854 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
856 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
857 if( tk->fmt.i_extra > 0 )
859 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
860 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
864 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
865 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
866 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
868 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
870 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
872 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
874 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
876 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
878 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
880 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
881 tk->fmt.i_extra = tk->i_extra_data;
882 tk->fmt.p_extra = malloc( tk->i_extra_data );
883 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
885 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
887 int i, i_offset = 1, i_size[3], i_extra;
890 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
892 /* Split the 3 headers */
893 if( tk->p_extra_data[0] != 0x02 )
894 msg_Err( p_demux, "invalid vorbis header" );
896 for( i = 0; i < 2; i++ )
899 while( i_offset < tk->i_extra_data )
901 i_size[i] += tk->p_extra_data[i_offset];
902 if( tk->p_extra_data[i_offset++] != 0xff ) break;
906 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
907 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
908 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
910 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
911 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
912 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
913 for( i = 0; i < 3; i++ )
915 *(p_extra++) = i_size[i] >> 8;
916 *(p_extra++) = i_size[i] & 0xFF;
917 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
919 p_extra += i_size[i];
920 i_extra += i_size[i];
923 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
924 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
926 int i_profile, i_srate;
927 static unsigned int i_sample_rates[] =
929 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
930 16000, 12000, 11025, 8000, 7350, 0, 0, 0
933 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
934 /* create data for faad (MP4DecSpecificDescrTag)*/
936 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
940 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
944 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
953 for( i_srate = 0; i_srate < 13; i_srate++ )
955 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
960 msg_Dbg( p_demux, "profile=%d srate=%d", i_profile, i_srate );
963 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
964 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
965 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
967 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
968 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
969 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
971 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
973 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
977 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
979 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
981 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
983 /* FIXME: support this codec */
984 msg_Err( p_demux, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
985 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
987 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
989 /* FIXME: support this codec */
990 msg_Err( p_demux, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
991 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
993 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
995 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
996 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
998 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
999 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1000 !strcmp( tk->psz_codec, "S_SSA" ) ||
1001 !strcmp( tk->psz_codec, "S_ASS" ))
1003 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1004 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1006 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1008 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1009 if( tk->i_extra_data )
1012 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1013 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1014 p_buf[tk->i_extra_data] = '\0';
1016 p_start = strstr( p_buf, "size:" );
1017 if( sscanf( p_start, "size: %dx%d",
1018 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1020 msg_Dbg( p_demux, "original frame size vobsubs: %dx%d", tk->fmt.subs.spu.i_original_frame_width, tk->fmt.subs.spu.i_original_frame_height );
1024 msg_Warn( p_demux, "reading original frame size for vobsub failed" );
1029 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1031 /* FIXME: support this codec */
1032 msg_Err( p_demux, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1033 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1037 msg_Err( p_demux, "unknow codec id=`%s'", tk->psz_codec );
1038 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1042 tk->fmt.i_priority = 1000;
1045 tk->p_es = es_out_Add( p_demux->out, &tk->fmt );
1049 /* add information */
1050 InformationCreate( p_demux );
1056 return VLC_EGENERIC;
1059 /*****************************************************************************
1060 * Close: frees unused data
1061 *****************************************************************************/
1062 static void Close( vlc_object_t *p_this )
1064 demux_t *p_demux = (demux_t*)p_this;
1065 demux_sys_t *p_sys = p_demux->p_sys;
1066 matroska_stream_t *p_stream = p_sys->Stream();
1067 matroska_segment_t *p_segment = p_stream->Segment();
1069 /* TODO close everything ? */
1071 delete p_segment->segment;
1076 /*****************************************************************************
1078 *****************************************************************************/
1079 static int Control( demux_t *p_demux, int i_query, va_list args )
1081 demux_sys_t *p_sys = p_demux->p_sys;
1082 matroska_stream_t *p_stream = p_sys->Stream();
1083 matroska_segment_t *p_segment = p_stream->Segment();
1088 vlc_meta_t **pp_meta;
1092 case DEMUX_GET_META:
1093 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
1094 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
1097 case DEMUX_GET_LENGTH:
1098 pi64 = (int64_t*)va_arg( args, int64_t * );
1099 if( p_segment->f_duration > 0.0 )
1101 *pi64 = (int64_t)(p_segment->f_duration * 1000);
1104 return VLC_EGENERIC;
1106 case DEMUX_GET_POSITION:
1107 pf = (double*)va_arg( args, double * );
1108 *pf = (double)p_sys->i_pts / (1000.0 * p_segment->f_duration);
1111 case DEMUX_SET_POSITION:
1112 f = (double)va_arg( args, double );
1113 Seek( p_demux, -1, f, NULL );
1116 case DEMUX_GET_TIME:
1117 pi64 = (int64_t*)va_arg( args, int64_t * );
1118 *pi64 = p_sys->i_pts;
1121 case DEMUX_GET_TITLE_INFO:
1122 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1124 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
1125 int *pi_int = (int*)va_arg( args, int* );
1128 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
1130 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
1134 return VLC_EGENERIC;
1136 case DEMUX_SET_TITLE:
1137 /* TODO handle editions as titles & DVD titles as well */
1138 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
1142 return VLC_EGENERIC;
1144 case DEMUX_SET_SEEKPOINT:
1145 /* FIXME do a better implementation */
1146 i_skp = (int)va_arg( args, int );
1148 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
1150 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1, NULL);
1151 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
1152 p_demux->info.i_seekpoint = i_skp;
1155 return VLC_EGENERIC;
1157 case DEMUX_SET_TIME:
1160 return VLC_EGENERIC;
1164 int matroska_segment_t::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
1175 if( sys.demuxer.b_die )
1177 return VLC_EGENERIC;
1181 i_level = ep->GetLevel();
1183 if( el == NULL && *pp_block != NULL )
1185 /* update the index */
1186 #define idx index[i_index - 1]
1187 if( i_index > 0 && idx.i_time == -1 )
1189 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
1190 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
1198 if( ep->GetLevel() > 1 )
1203 msg_Warn( &sys.demuxer, "EOF" );
1204 return VLC_EGENERIC;
1210 if( MKV_IS_ID( el, KaxCluster ) )
1212 cluster = (KaxCluster*)el;
1214 /* add it to the index */
1216 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
1218 IndexAppendCluster( cluster );
1221 // reset silent tracks
1222 for (size_t i=0; i<tracks.size(); i++)
1224 tracks[i]->b_silent = VLC_FALSE;
1229 else if( MKV_IS_ID( el, KaxCues ) )
1231 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
1232 return VLC_EGENERIC;
1236 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
1239 else if( i_level == 2 )
1241 if( MKV_IS_ID( el, KaxClusterTimecode ) )
1243 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
1245 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
1246 cluster->InitTimecode( uint64( ctc ), i_timescale );
1248 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
1252 else if( MKV_IS_ID( el, KaxBlockGroup ) )
1257 else if( i_level == 3 )
1259 if( MKV_IS_ID( el, KaxBlock ) )
1261 *pp_block = (KaxBlock*)el;
1263 (*pp_block)->ReadData( es.I_O() );
1264 (*pp_block)->SetParent( *cluster );
1268 else if( MKV_IS_ID( el, KaxBlockDuration ) )
1270 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
1272 dur.ReadData( es.I_O() );
1273 *pi_duration = uint64( dur );
1275 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1277 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1279 ref.ReadData( es.I_O() );
1280 if( *pi_ref1 == -1 )
1282 *pi_ref1 = int64( ref );
1286 *pi_ref2 = int64( ref );
1289 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1291 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1292 track_num.ReadData( es.I_O() );
1294 for (size_t i=0; i<tracks.size(); i++)
1296 if ( tracks[i]->i_number == uint32(track_num))
1298 tracks[i]->b_silent = VLC_TRUE;
1306 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1307 return VLC_EGENERIC;
1312 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1315 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1316 memcpy( p_block->p_buffer, p_mem, i_mem );
1317 //p_block->i_rate = p_input->stream.control.i_rate;
1321 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1322 mtime_t i_duration )
1324 demux_sys_t *p_sys = p_demux->p_sys;
1325 matroska_stream_t *p_stream = p_sys->Stream();
1326 matroska_segment_t *p_segment = p_stream->Segment();
1332 #define tk p_segment->tracks[i_track]
1333 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1335 if( tk->i_number == block->TrackNum() )
1341 if( i_track >= p_segment->tracks.size() )
1343 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1346 if( tk->p_es == NULL )
1348 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1351 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1353 return; /* discard audio packets that shouldn't be rendered */
1356 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1359 tk->b_inited = VLC_FALSE;
1363 /* First send init data */
1364 if( !tk->b_inited && tk->i_data_init > 0 )
1368 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1369 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1370 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1372 tk->b_inited = VLC_TRUE;
1375 for( i = 0; i < block->NumberFrames(); i++ )
1378 DataBuffer &data = block->GetBuffer(i);
1380 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1382 if( p_block == NULL )
1387 #if defined(HAVE_ZLIB_H)
1388 if( tk->i_compression_type )
1390 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1394 // TODO implement correct timestamping when B frames are used
1395 if( tk->fmt.i_cat != VIDEO_ES )
1397 p_block->i_dts = p_block->i_pts = i_pts;
1401 p_block->i_dts = i_pts;
1405 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1407 p_block->i_length = i_duration * 1000;
1409 es_out_Send( p_demux->out, tk->p_es, p_block );
1411 /* use time stamp only for first block */
1418 matroska_stream_t *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1420 int i_upper_lvl = 0;
1422 EbmlElement *p_l0, *p_l1, *p_l2;
1423 bool b_keep_stream = false, b_keep_segment;
1425 // verify the EBML Header
1426 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1431 p_l0->SkipData(*p_estream, EbmlHead_Context);
1434 // find all segments in this file
1435 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1441 matroska_stream_t *p_stream1 = new matroska_stream_t( *this );
1445 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1448 matroska_segment_t *p_segment1 = new matroska_segment_t( *this, *p_estream );
1449 b_keep_segment = false;
1451 ep = new EbmlParser(p_estream, p_l0);
1452 p_segment1->ep = ep;
1453 p_segment1->segment = (KaxSegment*)p_l0;
1455 while ((p_l1 = ep->Get()))
1457 if (MKV_IS_ID(p_l1, KaxInfo))
1459 // find the families of this segment
1460 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1462 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1463 for( i = 0; i < p_info->ListSize(); i++ )
1465 EbmlElement *l = (*p_info)[i];
1467 if( MKV_IS_ID( l, KaxSegmentUID ) )
1469 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1470 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1471 if ( !b_keep_segment )
1472 break; // this segment is already known
1473 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1475 else if( MKV_IS_ID( l, KaxPrevUID ) )
1477 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1479 else if( MKV_IS_ID( l, KaxNextUID ) )
1481 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1483 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1485 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1486 std::vector<KaxSegmentFamily>::iterator iter;
1487 p_segment1->families.push_back( *p_fam );
1493 if ( b_keep_segment )
1495 b_keep_stream = true;
1496 p_stream1->segments.push_back( p_segment1 );
1502 p_l0->SkipData(*p_estream, EbmlHead_Context);
1503 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1506 if ( !b_keep_stream )
1515 static void UpdateCurrentToChapter( demux_t & demux )
1517 demux_sys_t & sys = *demux.p_sys;
1518 matroska_stream_t *p_stream = sys.Stream();
1519 matroska_segment_t *p_segment = p_stream->Segment();
1520 const chapter_item_t *psz_curr_chapter;
1522 /* update current chapter/seekpoint */
1523 if ( p_segment->editions.size())
1525 /* 1st, we need to know in which chapter we are */
1526 psz_curr_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( sys.i_pts );
1528 /* we have moved to a new chapter */
1529 if (p_segment->psz_current_chapter != NULL && psz_curr_chapter != NULL && p_segment->psz_current_chapter != psz_curr_chapter)
1531 if (p_segment->psz_current_chapter->i_seekpoint_num != psz_curr_chapter->i_seekpoint_num && psz_curr_chapter->i_seekpoint_num > 0)
1533 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1534 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1537 if (p_segment->editions[p_segment->i_current_edition].b_ordered )
1539 /* TODO check if we need to silently seek to a new location in the stream (switch to another chapter) */
1540 if (p_segment->psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time)
1541 Seek(&demux, sys.i_pts, -1, psz_curr_chapter);
1542 /* count the last duration time found for each track in a table (-1 not found, -2 silent) */
1543 /* only seek after each duration >= end timecode of the current chapter */
1546 // p_segment->i_user_time = psz_curr_chapter->i_user_start_time - psz_curr_chapter->i_start_time;
1547 // p_segment->i_start_pts = psz_curr_chapter->i_user_start_time;
1549 p_segment->psz_current_chapter = psz_curr_chapter;
1553 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter)
1555 demux_sys_t *p_sys = p_demux->p_sys;
1556 matroska_stream_t *p_stream = p_sys->Stream();
1557 matroska_segment_t *p_segment = p_stream->Segment();
1558 mtime_t i_time_offset = 0;
1561 int64_t i_block_duration;
1562 int64_t i_block_ref1;
1563 int64_t i_block_ref2;
1566 int i_track_skipping;
1569 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
1570 if( i_date < 0 && f_percent < 0 )
1572 msg_Warn( p_demux, "cannot seek nowhere !" );
1575 if( f_percent > 1.0 )
1577 msg_Warn( p_demux, "cannot seek so far !" );
1581 delete p_segment->ep;
1582 p_segment->ep = new EbmlParser( p_stream->p_es, p_segment->segment );
1583 p_segment->cluster = NULL;
1585 /* seek without index or without date */
1586 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
1588 if (p_segment->f_duration >= 0)
1590 i_date = int64_t( f_percent * p_segment->f_duration * 1000.0 );
1594 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
1596 msg_Dbg( p_demux, "inacurate way of seeking" );
1597 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
1599 if( p_segment->index[i_index].i_position >= i_pos)
1604 if( i_index == p_segment->i_index )
1609 i_date = p_segment->index[i_index].i_time;
1612 if( p_segment->index[i_index].i_position < i_pos )
1616 msg_Warn( p_demux, "searching for cluster, could take some time" );
1618 /* search a cluster */
1619 while( ( el = p_sys->ep->Get() ) != NULL )
1621 if( MKV_IS_ID( el, KaxCluster ) )
1623 KaxCluster *cluster = (KaxCluster*)el;
1625 /* add it to the index */
1626 p_segment->IndexAppendCluster( cluster );
1628 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1630 p_sys->cluster = cluster;
1641 // find the actual time for an ordered edition
1642 if ( psz_chapter == NULL )
1644 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1646 /* 1st, we need to know in which chapter we are */
1647 psz_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( i_date );
1651 if ( psz_chapter != NULL )
1653 p_segment->psz_current_chapter = psz_chapter;
1654 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
1655 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1656 p_demux->info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
1659 for( ; i_index < p_segment->i_index; i_index++ )
1661 if( p_segment->index[i_index].i_time + i_time_offset > i_date )
1672 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1673 p_segment->index[i_index].i_time,
1674 (int)( 100 * p_segment->index[i_index].i_position /
1675 stream_Size( p_demux->s ) ) );
1677 p_stream->p_in->setFilePointer( p_segment->index[i_index].i_position,
1680 p_sys->i_start_pts = i_date;
1682 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1684 /* now parse until key frame */
1685 #define tk p_segment->tracks[i_track]
1686 i_track_skipping = 0;
1687 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1689 if( tk->fmt.i_cat == VIDEO_ES )
1691 tk->b_search_keyframe = VLC_TRUE;
1694 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
1698 while( i_track_skipping > 0 )
1700 if( p_segment->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1702 msg_Warn( p_demux, "cannot get block EOF?" );
1707 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1709 if( tk->i_number == block->TrackNum() )
1715 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1717 if( i_track < p_segment->tracks.size() )
1719 if( p_sys->i_pts >= p_sys->i_start_pts )
1721 BlockDecode( p_demux, block, p_sys->i_pts, 0 );
1722 i_track_skipping = 0;
1724 else if( tk->fmt.i_cat == VIDEO_ES )
1726 if( i_block_ref1 == -1 && tk->b_search_keyframe )
1728 tk->b_search_keyframe = VLC_FALSE;
1731 if( !tk->b_search_keyframe )
1733 BlockDecode( p_demux, block, p_sys->i_pts, 0 );
1743 /*****************************************************************************
1744 * Demux: reads and demuxes data packets
1745 *****************************************************************************
1746 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1747 *****************************************************************************/
1748 static int Demux( demux_t *p_demux)
1750 demux_sys_t *p_sys = p_demux->p_sys;
1751 matroska_stream_t *p_stream = p_sys->Stream();
1752 matroska_segment_t *p_segment = p_stream->Segment();
1753 int i_block_count = 0;
1756 int64_t i_block_duration;
1757 int64_t i_block_ref1;
1758 int64_t i_block_ref2;
1762 if( p_sys->i_pts >= p_sys->i_start_pts )
1763 UpdateCurrentToChapter( *p_demux );
1765 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered && p_segment->psz_current_chapter == NULL )
1767 /* nothing left to read in this ordered edition */
1768 if ( p_stream->i_current_segment == p_stream->segments.size() - 1)
1770 /* switch to the next segment (TODO update the duration) */
1771 p_stream->i_current_segment++;
1775 if( p_segment->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1777 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1779 // check if there are more chapters to read
1780 if ( p_segment->psz_current_chapter != NULL )
1782 p_sys->i_pts = p_segment->psz_current_chapter->i_user_end_time;
1788 msg_Warn( p_demux, "cannot get block EOF?" );
1793 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1795 if( p_sys->i_pts >= p_sys->i_start_pts )
1797 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1800 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1805 // TODO optimize when there is need to leave or when seeking has been called
1806 if( i_block_count > 5 )
1815 /*****************************************************************************
1817 *****************************************************************************/
1818 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1824 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1826 if( i_size <= 0 || mb_eof )
1831 return stream_Read( s, p_buffer, i_size );
1833 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1839 case seek_beginning:
1843 i_pos = stream_Size( s ) - i_offset;
1846 i_pos= stream_Tell( s ) + i_offset;
1850 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1857 if( stream_Seek( s, i_pos ) )
1863 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1867 uint64 vlc_stream_io_callback::getFilePointer( void )
1869 return stream_Tell( s );
1871 void vlc_stream_io_callback::close( void )
1877 /*****************************************************************************
1878 * Ebml Stream parser
1879 *****************************************************************************/
1880 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1888 for( i = 1; i < 6; i++ )
1894 mb_keep = VLC_FALSE;
1897 EbmlParser::~EbmlParser( void )
1901 for( i = 1; i < mi_level; i++ )
1907 mb_keep = VLC_FALSE;
1911 void EbmlParser::Up( void )
1913 if( mi_user_level == mi_level )
1915 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1921 void EbmlParser::Down( void )
1927 void EbmlParser::Keep( void )
1932 int EbmlParser::GetLevel( void )
1934 return mi_user_level;
1937 void EbmlParser::Reset( void )
1939 delete m_el[mi_level];
1940 m_el[mi_level] = NULL;
1941 #if LIBEBML_VERSION >= 0x000704
1942 // a little faster and cleaner
1943 m_es->I_O().setFilePointer( static_cast<EbmlMaster*>(m_el[0])->GetDataStart() );
1945 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
1949 EbmlElement *EbmlParser::Get( void )
1953 if( mi_user_level != mi_level )
1959 EbmlElement *ret = m_got;
1965 if( m_el[mi_level] )
1967 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
1970 delete m_el[mi_level];
1972 mb_keep = VLC_FALSE;
1975 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
1986 delete m_el[mi_level - 1];
1987 m_got = m_el[mi_level -1] = m_el[mi_level];
1988 m_el[mi_level] = NULL;
1995 else if( m_el[mi_level] == NULL )
1997 fprintf( stderr," m_el[mi_level] == NULL\n" );
2000 return m_el[mi_level];
2004 /*****************************************************************************
2006 * * LoadCues : load the cues element and update index
2008 * * LoadTags : load ... the tags element
2010 * * InformationCreate : create all information, load tags if present
2012 *****************************************************************************/
2013 static void LoadCues( demux_t *p_demux )
2015 demux_sys_t *p_sys = p_demux->p_sys;
2016 matroska_stream_t *p_stream = p_sys->Stream();
2017 matroska_segment_t *p_segment = p_stream->Segment();
2018 int64_t i_sav_position = p_stream->p_in->getFilePointer();
2020 EbmlElement *el, *cues;
2022 msg_Dbg( p_demux, "loading cues" );
2023 p_stream->p_in->setFilePointer( p_segment->i_cues_position, seek_beginning );
2024 cues = p_stream->p_es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2028 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
2029 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2033 ep = new EbmlParser( p_stream->p_es, cues );
2034 while( ( el = ep->Get() ) != NULL )
2036 if( MKV_IS_ID( el, KaxCuePoint ) )
2038 #define idx p_segment->index[p_segment->i_index]
2041 idx.i_block_number= -1;
2042 idx.i_position = -1;
2044 idx.b_key = VLC_TRUE;
2047 while( ( el = ep->Get() ) != NULL )
2049 if( MKV_IS_ID( el, KaxCueTime ) )
2051 KaxCueTime &ctime = *(KaxCueTime*)el;
2053 ctime.ReadData( p_stream->p_es->I_O() );
2055 idx.i_time = uint64( ctime ) * p_segment->i_timescale / (mtime_t)1000;
2057 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2060 while( ( el = ep->Get() ) != NULL )
2062 if( MKV_IS_ID( el, KaxCueTrack ) )
2064 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2066 ctrack.ReadData( p_stream->p_es->I_O() );
2067 idx.i_track = uint16( ctrack );
2069 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2071 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2073 ccpos.ReadData( p_stream->p_es->I_O() );
2074 idx.i_position = p_segment->segment->GetGlobalPosition( uint64( ccpos ) );
2076 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2078 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2080 cbnum.ReadData( p_stream->p_es->I_O() );
2081 idx.i_block_number = uint32( cbnum );
2085 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2092 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2098 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
2099 " track=%d bnum=%d", idx.i_time, idx.i_position,
2100 idx.i_track, idx.i_block_number );
2103 p_segment->i_index++;
2104 if( p_segment->i_index >= p_segment->i_index_max )
2106 p_segment->i_index_max += 1024;
2107 p_segment->index = (mkv_index_t*)realloc( p_segment->index, sizeof( mkv_index_t ) * p_segment->i_index_max );
2113 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2119 p_segment->b_cues = VLC_TRUE;
2121 msg_Dbg( p_demux, "loading cues done." );
2122 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2125 static void LoadTags( demux_t *p_demux )
2127 demux_sys_t *p_sys = p_demux->p_sys;
2128 matroska_stream_t *p_stream = p_sys->Stream();
2129 matroska_segment_t *p_segment = p_stream->Segment();
2130 int64_t i_sav_position = p_stream->p_in->getFilePointer();
2132 EbmlElement *el, *tags;
2134 msg_Dbg( p_demux, "loading tags" );
2135 p_stream->p_in->setFilePointer( p_segment->i_tags_position, seek_beginning );
2136 tags = p_stream->p_es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2140 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
2141 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2145 msg_Dbg( p_demux, "Tags" );
2146 ep = new EbmlParser( p_stream->p_es, tags );
2147 while( ( el = ep->Get() ) != NULL )
2149 if( MKV_IS_ID( el, KaxTag ) )
2151 msg_Dbg( p_demux, "+ Tag" );
2153 while( ( el = ep->Get() ) != NULL )
2155 if( MKV_IS_ID( el, KaxTagTargets ) )
2157 msg_Dbg( p_demux, "| + Targets" );
2159 while( ( el = ep->Get() ) != NULL )
2161 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2165 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2167 msg_Dbg( p_demux, "| + General" );
2169 while( ( el = ep->Get() ) != NULL )
2171 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2175 else if( MKV_IS_ID( el, KaxTagGenres ) )
2177 msg_Dbg( p_demux, "| + Genres" );
2179 while( ( el = ep->Get() ) != NULL )
2181 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2185 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2187 msg_Dbg( p_demux, "| + Audio Specific" );
2189 while( ( el = ep->Get() ) != NULL )
2191 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2195 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2197 msg_Dbg( p_demux, "| + Images Specific" );
2199 while( ( el = ep->Get() ) != NULL )
2201 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2205 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2207 msg_Dbg( p_demux, "| + Multi Comment" );
2209 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2211 msg_Dbg( p_demux, "| + Multi Commercial" );
2213 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2215 msg_Dbg( p_demux, "| + Multi Date" );
2217 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2219 msg_Dbg( p_demux, "| + Multi Entity" );
2221 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2223 msg_Dbg( p_demux, "| + Multi Identifier" );
2225 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2227 msg_Dbg( p_demux, "| + Multi Legal" );
2229 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2231 msg_Dbg( p_demux, "| + Multi Title" );
2235 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
2242 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
2248 msg_Dbg( p_demux, "loading tags done." );
2249 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2252 /*****************************************************************************
2254 *****************************************************************************/
2255 void matroska_segment_t::ParseSeekHead( EbmlElement *seekhead )
2260 int i_upper_level = 0;
2262 msg_Dbg( &sys.demuxer, "| + Seek head" );
2264 /* Master elements */
2265 m = static_cast<EbmlMaster *>(seekhead);
2266 m->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2268 for( i = 0; i < m->ListSize(); i++ )
2270 EbmlElement *l = (*m)[i];
2272 if( MKV_IS_ID( l, KaxSeek ) )
2274 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2275 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2280 for( j = 0; j < sk->ListSize(); j++ )
2282 EbmlElement *l = (*sk)[j];
2284 if( MKV_IS_ID( l, KaxSeekID ) )
2286 KaxSeekID &sid = *(KaxSeekID*)l;
2287 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2289 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2291 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2292 i_pos = uint64( spos );
2296 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2302 if( id == KaxCues::ClassInfos.GlobalId )
2304 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2305 i_cues_position = segment->GetGlobalPosition( i_pos );
2307 else if( id == KaxChapters::ClassInfos.GlobalId )
2309 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2310 i_chapters_position = segment->GetGlobalPosition( i_pos );
2312 else if( id == KaxTags::ClassInfos.GlobalId )
2314 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2315 i_tags_position = segment->GetGlobalPosition( i_pos );
2321 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2326 /*****************************************************************************
2328 *****************************************************************************/
2329 void matroska_segment_t::ParseTrackEntry( EbmlMaster *m )
2335 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2337 tk = new mkv_track_t();
2338 tracks.push_back( tk );
2340 /* Init the track */
2341 memset( tk, 0, sizeof( mkv_track_t ) );
2343 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2344 tk->fmt.psz_language = strdup("English");
2345 tk->fmt.psz_description = NULL;
2347 tk->b_default = VLC_TRUE;
2348 tk->b_enabled = VLC_TRUE;
2349 tk->b_silent = VLC_FALSE;
2350 tk->i_number = tracks.size() - 1;
2351 tk->i_extra_data = 0;
2352 tk->p_extra_data = NULL;
2353 tk->psz_codec = NULL;
2354 tk->i_default_duration = 0;
2355 tk->f_timecodescale = 1.0;
2357 tk->b_inited = VLC_FALSE;
2358 tk->i_data_init = 0;
2359 tk->p_data_init = NULL;
2361 tk->psz_codec_name = NULL;
2362 tk->psz_codec_settings = NULL;
2363 tk->psz_codec_info_url = NULL;
2364 tk->psz_codec_download_url = NULL;
2366 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2368 for( i = 0; i < m->ListSize(); i++ )
2370 EbmlElement *l = (*m)[i];
2372 if( MKV_IS_ID( l, KaxTrackNumber ) )
2374 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2376 tk->i_number = uint32( tnum );
2377 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2379 else if( MKV_IS_ID( l, KaxTrackUID ) )
2381 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2383 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2385 else if( MKV_IS_ID( l, KaxTrackType ) )
2388 KaxTrackType &ttype = *(KaxTrackType*)l;
2390 switch( uint8(ttype) )
2394 tk->fmt.i_cat = AUDIO_ES;
2398 tk->fmt.i_cat = VIDEO_ES;
2400 case track_subtitle:
2401 psz_type = "subtitle";
2402 tk->fmt.i_cat = SPU_ES;
2405 psz_type = "unknown";
2406 tk->fmt.i_cat = UNKNOWN_ES;
2410 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2412 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2414 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2416 // tk->b_enabled = uint32( fenb );
2417 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2418 // uint32( fenb ) );
2420 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2422 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2424 tk->b_default = uint32( fdef );
2425 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2427 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2429 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2431 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2433 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2435 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2437 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2439 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2441 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2443 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2445 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2447 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2449 tk->i_default_duration = uint64(defd);
2450 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2452 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2454 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2456 tk->f_timecodescale = float( ttcs );
2457 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2459 else if( MKV_IS_ID( l, KaxTrackName ) )
2461 KaxTrackName &tname = *(KaxTrackName*)l;
2463 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2464 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2466 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2468 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2470 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2471 msg_Dbg( &sys.demuxer,
2472 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2474 else if( MKV_IS_ID( l, KaxCodecID ) )
2476 KaxCodecID &codecid = *(KaxCodecID*)l;
2478 tk->psz_codec = strdup( string( codecid ).c_str() );
2479 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2481 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2483 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2485 tk->i_extra_data = cpriv.GetSize();
2486 if( tk->i_extra_data > 0 )
2488 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2489 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2491 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2493 else if( MKV_IS_ID( l, KaxCodecName ) )
2495 KaxCodecName &cname = *(KaxCodecName*)l;
2497 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2498 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2500 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2502 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2503 MkvTree( sys.demuxer, 3, "Content Encodings" );
2504 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2506 EbmlElement *l2 = (*cencs)[i];
2507 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2509 MkvTree( sys.demuxer, 4, "Content Encoding" );
2510 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2511 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2513 EbmlElement *l3 = (*cenc)[i];
2514 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2516 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2517 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2519 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2521 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2522 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2524 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2526 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2527 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2529 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2531 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2532 MkvTree( sys.demuxer, 5, "Content Compression" );
2533 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2535 EbmlElement *l4 = (*compr)[i];
2536 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2538 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2539 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2540 if( uint32( compalg ) == 0 )
2542 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2547 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2554 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2561 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2566 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2568 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2570 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2571 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2573 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2575 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2577 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2578 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2580 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2582 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2584 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2585 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2587 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2589 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2591 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2593 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2595 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2597 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2599 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2601 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2604 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
2607 for( j = 0; j < tkv->ListSize(); j++ )
2609 EbmlElement *l = (*tkv)[j];
2610 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2612 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2614 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2616 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2618 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2620 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2623 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2625 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2627 tk->fmt.video.i_width = uint16( vwidth );
2628 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
2630 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2632 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2634 tk->fmt.video.i_height = uint16( vheight );
2635 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
2637 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2639 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2641 tk->fmt.video.i_visible_width = uint16( vwidth );
2642 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
2644 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2646 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2648 tk->fmt.video.i_visible_height = uint16( vheight );
2649 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
2651 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2653 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2655 tk->f_fps = float( vfps );
2656 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
2658 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2660 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2662 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
2663 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2665 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2667 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2669 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2671 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2673 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2675 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
2679 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
2682 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
2683 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
2685 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2687 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2690 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
2692 for( j = 0; j < tka->ListSize(); j++ )
2694 EbmlElement *l = (*tka)[j];
2696 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2698 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2700 tk->fmt.audio.i_rate = (int)float( afreq );
2701 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2703 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2705 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2707 tk->fmt.audio.i_channels = uint8( achan );
2708 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
2710 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2712 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2714 tk->fmt.audio.i_bitspersample = uint8( abits );
2715 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
2719 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
2725 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
2726 typeid(*l).name() );
2731 /*****************************************************************************
2733 *****************************************************************************/
2734 void matroska_segment_t::ParseTracks( EbmlElement *tracks )
2739 int i_upper_level = 0;
2741 msg_Dbg( &sys.demuxer, "| + Tracks" );
2743 /* Master elements */
2744 m = static_cast<EbmlMaster *>(tracks);
2745 m->Read( es, tracks->Generic().Context, i_upper_level, el, true );
2747 for( i = 0; i < m->ListSize(); i++ )
2749 EbmlElement *l = (*m)[i];
2751 if( MKV_IS_ID( l, KaxTrackEntry ) )
2753 ParseTrackEntry( static_cast<EbmlMaster *>(l) );
2757 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2762 /*****************************************************************************
2764 *****************************************************************************/
2765 void matroska_segment_t::ParseInfo( EbmlElement *info )
2770 int i_upper_level = 0;
2772 msg_Dbg( &sys.demuxer, "| + Information" );
2774 /* Master elements */
2775 m = static_cast<EbmlMaster *>(info);
2776 m->Read( es, info->Generic().Context, i_upper_level, el, true );
2778 for( i = 0; i < m->ListSize(); i++ )
2780 EbmlElement *l = (*m)[i];
2782 if( MKV_IS_ID( l, KaxSegmentUID ) )
2784 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
2786 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
2788 else if( MKV_IS_ID( l, KaxPrevUID ) )
2790 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
2792 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
2794 else if( MKV_IS_ID( l, KaxNextUID ) )
2796 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
2798 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
2800 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2802 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2804 i_timescale = uint64(tcs);
2806 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
2809 else if( MKV_IS_ID( l, KaxDuration ) )
2811 KaxDuration &dur = *(KaxDuration*)l;
2813 f_duration = float(dur);
2815 msg_Dbg( &sys.demuxer, "| | + Duration=%f",
2818 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2820 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2822 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2824 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
2825 psz_muxing_application );
2827 else if( MKV_IS_ID( l, KaxWritingApp ) )
2829 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2831 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2833 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
2834 psz_writing_application );
2836 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2838 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2840 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2842 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
2843 psz_segment_filename );
2845 else if( MKV_IS_ID( l, KaxTitle ) )
2847 KaxTitle &title = *(KaxTitle*)l;
2849 psz_title = UTF8ToStr( UTFstring( title ) );
2851 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
2853 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2855 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
2857 families.push_back(*uid);
2859 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
2861 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2862 else if( MKV_IS_ID( l, KaxDateUTC ) )
2864 KaxDateUTC &date = *(KaxDateUTC*)l;
2869 i_date = date.GetEpochDate();
2870 memset( buffer, 0, 256 );
2871 if( gmtime_r( &i_date, &tmres ) &&
2872 asctime_r( &tmres, buffer ) )
2874 buffer[strlen( buffer)-1]= '\0';
2875 psz_date_utc = strdup( buffer );
2876 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
2882 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2886 f_duration *= i_timescale / 1000000.0;
2890 /*****************************************************************************
2892 *****************************************************************************/
2893 void matroska_segment_t::ParseChapterAtom( int i_level, EbmlMaster *ca, chapter_item_t & chapters )
2897 if( sys.title == NULL )
2899 sys.title = vlc_input_title_New();
2902 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
2903 for( i = 0; i < ca->ListSize(); i++ )
2905 EbmlElement *l = (*ca)[i];
2907 if( MKV_IS_ID( l, KaxChapterUID ) )
2909 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
2910 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
2912 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
2914 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
2915 chapters.b_display_seekpoint = uint8( flag ) == 0;
2917 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
2919 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
2921 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
2922 chapters.i_start_time = uint64( start ) / I64C(1000);
2924 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
2926 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
2928 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
2929 chapters.i_end_time = uint64( end ) / I64C(1000);
2931 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
2933 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
2935 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
2938 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
2939 for( j = 0; j < cd->ListSize(); j++ )
2941 EbmlElement *l= (*cd)[j];
2943 if( MKV_IS_ID( l, KaxChapterString ) )
2947 KaxChapterString &name =*(KaxChapterString*)l;
2948 for (k = 0; k < i_level; k++)
2949 chapters.psz_name += '+';
2950 chapters.psz_name += ' ';
2951 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
2953 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
2955 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
2957 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
2958 const char *psz = string( lang ).c_str();
2960 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
2962 else if( MKV_IS_ID( l, KaxChapterCountry ) )
2964 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
2965 const char *psz = string( ct ).c_str();
2967 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
2971 else if( MKV_IS_ID( l, KaxChapterAtom ) )
2973 chapter_item_t new_sub_chapter;
2974 ParseChapterAtom( i_level+1, static_cast<EbmlMaster *>(l), new_sub_chapter );
2975 new_sub_chapter.psz_parent = &chapters;
2976 chapters.sub_chapters.push_back( new_sub_chapter );
2981 /*****************************************************************************
2983 *****************************************************************************/
2984 void matroska_segment_t::ParseChapters( EbmlElement *chapters )
2989 int i_upper_level = 0;
2990 int i_default_edition = 0;
2993 /* Master elements */
2994 m = static_cast<EbmlMaster *>(chapters);
2995 m->Read( es, chapters->Generic().Context, i_upper_level, el, true );
2997 for( i = 0; i < m->ListSize(); i++ )
2999 EbmlElement *l = (*m)[i];
3001 if( MKV_IS_ID( l, KaxEditionEntry ) )
3003 chapter_edition_t edition;
3005 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3007 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3008 for( j = 0; j < E->ListSize(); j++ )
3010 EbmlElement *l = (*E)[j];
3012 if( MKV_IS_ID( l, KaxChapterAtom ) )
3014 chapter_item_t new_sub_chapter;
3015 ParseChapterAtom( 0, static_cast<EbmlMaster *>(l), new_sub_chapter );
3016 edition.chapters.push_back( new_sub_chapter );
3018 else if( MKV_IS_ID( l, KaxEditionUID ) )
3020 edition.i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3022 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3024 edition.b_ordered = uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0;
3026 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3028 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3029 i_default_edition = editions.size();
3033 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3036 editions.push_back( edition );
3040 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3044 for( i = 0; i < editions.size(); i++ )
3046 editions[i].RefreshChapters( *sys.title );
3049 i_current_edition = i_default_edition;
3051 if ( editions[i_default_edition].b_ordered )
3053 /* update the duration of the segment according to the sum of all sub chapters */
3054 f_dur = editions[i_default_edition].Duration() / I64C(1000);
3060 /*****************************************************************************
3061 * InformationCreate:
3062 *****************************************************************************/
3063 static void InformationCreate( demux_t *p_demux )
3065 demux_sys_t *p_sys = p_demux->p_sys;
3066 matroska_stream_t *p_stream = p_sys->Stream();
3067 matroska_segment_t *p_segment = p_stream->Segment();
3070 p_sys->meta = vlc_meta_New();
3072 if( p_segment->psz_title )
3074 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_segment->psz_title );
3076 if( p_segment->psz_date_utc )
3078 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_segment->psz_date_utc );
3080 if( p_segment->psz_segment_filename )
3082 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_segment->psz_segment_filename );
3084 if( p_segment->psz_muxing_application )
3086 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_segment->psz_muxing_application );
3088 if( p_segment->psz_writing_application )
3090 vlc_meta_Add( p_sys->meta, _("Writing application"), p_segment->psz_writing_application );
3093 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
3095 mkv_track_t *tk = p_segment->tracks[i_track];
3096 vlc_meta_t *mtk = vlc_meta_New();
3098 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
3099 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
3100 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
3102 if( tk->fmt.psz_description )
3104 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3106 if( tk->psz_codec_name )
3108 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3110 if( tk->psz_codec_settings )
3112 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
3114 if( tk->psz_codec_info_url )
3116 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3118 if( tk->psz_codec_download_url )
3120 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
3124 if( p_segment->i_tags_position >= 0 )
3126 vlc_bool_t b_seekable;
3128 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
3131 LoadTags( p_demux );
3137 /*****************************************************************************
3139 *****************************************************************************/
3141 void matroska_segment_t::IndexAppendCluster( KaxCluster *cluster )
3143 #define idx index[i_index]
3145 idx.i_block_number= -1;
3146 idx.i_position = cluster->GetElementPosition();
3148 idx.b_key = VLC_TRUE;
3151 if( i_index >= i_index_max )
3153 i_index_max += 1024;
3154 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3159 static char * UTF8ToStr( const UTFstring &u )
3168 p = dst = (char*)malloc( i_src + 1);
3187 void chapter_edition_t::RefreshChapters( input_title_t & title )
3189 int64_t i_prev_user_time = 0;
3190 std::vector<chapter_item_t>::iterator index = chapters.begin();
3192 while ( index != chapters.end() )
3194 i_prev_user_time = (*index).RefreshChapters( b_ordered, i_prev_user_time, title );
3199 int64_t chapter_item_t::RefreshChapters( bool b_ordered, int64_t i_prev_user_time, input_title_t & title )
3201 int64_t i_user_time = i_prev_user_time;
3203 // first the sub-chapters, and then ourself
3204 std::vector<chapter_item_t>::iterator index = sub_chapters.begin();
3205 while ( index != sub_chapters.end() )
3207 i_user_time = (*index).RefreshChapters( b_ordered, i_user_time, title );
3213 i_user_start_time = i_prev_user_time;
3214 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3216 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3220 i_user_end_time = i_user_time;
3225 std::sort( sub_chapters.begin(), sub_chapters.end() );
3226 i_user_start_time = i_start_time;
3227 i_user_end_time = i_end_time;
3230 if (b_display_seekpoint)
3232 seekpoint_t *sk = vlc_seekpoint_New();
3234 // sk->i_level = i_level;
3235 sk->i_time_offset = i_start_time;
3236 sk->psz_name = strdup( psz_name.c_str() );
3238 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3239 title.i_seekpoint++;
3240 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3241 title.seekpoint[title.i_seekpoint-1] = sk;
3244 i_seekpoint_num = title.i_seekpoint;
3246 return i_user_end_time;
3249 double chapter_edition_t::Duration() const
3251 double f_result = 0.0;
3253 if ( chapters.size() )
3255 std::vector<chapter_item_t>::const_iterator index = chapters.end();
3257 f_result = (*index).i_user_end_time;
3263 const chapter_item_t *chapter_item_t::FindTimecode( mtime_t i_user_timecode ) const
3265 const chapter_item_t *psz_result = NULL;
3267 if (i_user_timecode >= i_user_start_time && i_user_timecode < i_user_end_time)
3269 std::vector<chapter_item_t>::const_iterator index = sub_chapters.begin();
3270 while ( index != sub_chapters.end() && psz_result == NULL )
3272 psz_result = (*index).FindTimecode( i_user_timecode );
3276 if ( psz_result == NULL )
3283 const chapter_item_t *chapter_edition_t::FindTimecode( mtime_t i_user_timecode ) const
3285 const chapter_item_t *psz_result = NULL;
3287 std::vector<chapter_item_t>::const_iterator index = chapters.begin();
3288 while ( index != chapters.end() && psz_result == NULL )
3290 psz_result = (*index).FindTimecode( i_user_timecode );
3297 void demux_sys_t::PreloadFamily( )
3299 matroska_stream_t *p_stream = Stream();
3302 matroska_segment_t *p_segment = p_stream->Segment();
3305 for (size_t i=0; i<streams.size(); i++)
3307 streams[i]->PreloadFamily( *p_segment );
3313 void matroska_stream_t::PreloadFamily( const matroska_segment_t & of_segment )
3315 for (size_t i=0; i<segments.size(); i++)
3317 segments[i]->PreloadFamily( of_segment );
3321 bool matroska_segment_t::PreloadFamily( const matroska_segment_t & of_segment )
3326 for (size_t i=0; i<families.size(); i++)
3328 for (size_t j=0; j<of_segment.families.size(); j++)
3330 if ( families[i] == of_segment.families[j] )
3338 // preload all the linked segments for all preloaded segments
3339 void demux_sys_t::PreloadLinked( )
3341 size_t i_prealoaded;
3344 for (size_t i=0; i<streams.size(); i++)
3346 i_prealoaded += streams[i]->PreloadLinked( *this );
3348 } while ( i_prealoaded ); // worst case: will stop when all segments are preloaded
3351 size_t matroska_stream_t::PreloadLinked( const demux_sys_t & of_sys )
3353 size_t i_result = 0;
3354 for (size_t i=0; i<segments.size(); i++)
3356 i_result += segments[i]->PreloadLinked( of_sys );
3361 size_t matroska_segment_t::PreloadLinked( const demux_sys_t & of_sys )
3363 size_t i_result = 0;
3364 if ( prev_segment_uid.GetBuffer() )
3366 matroska_segment_t *p_segment = of_sys.FindSegment( prev_segment_uid );
3369 i_result += p_segment->Preload( ) ? 1 : 0;
3372 if ( next_segment_uid.GetBuffer() )
3374 matroska_segment_t *p_segment = of_sys.FindSegment( next_segment_uid );
3377 i_result += p_segment->Preload( ) ? 1 : 0;
3383 bool matroska_segment_t::Preload( )
3388 EbmlElement *el = NULL;
3392 while( ( el = ep->Get() ) != NULL )
3394 if( MKV_IS_ID( el, KaxInfo ) )
3398 else if( MKV_IS_ID( el, KaxTracks ) )
3402 else if( MKV_IS_ID( el, KaxSeekHead ) )
3404 ParseSeekHead( el );
3406 else if( MKV_IS_ID( el, KaxCues ) )
3408 msg_Dbg( &sys.demuxer, "| + Cues" );
3410 else if( MKV_IS_ID( el, KaxCluster ) )
3412 msg_Dbg( &sys.demuxer, "| + Cluster" );
3414 cluster = (KaxCluster*)el;
3417 /* stop parsing the stream */
3420 else if( MKV_IS_ID( el, KaxAttachments ) )
3422 msg_Dbg( &sys.demuxer, "| + Attachments FIXME TODO (but probably never supported)" );
3424 else if( MKV_IS_ID( el, KaxChapters ) )
3426 msg_Dbg( &sys.demuxer, "| + Chapters" );
3427 ParseChapters( el );
3429 else if( MKV_IS_ID( el, KaxTag ) )
3431 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3435 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3444 matroska_segment_t *demux_sys_t::FindSegment( EbmlBinary & uid ) const
3446 matroska_segment_t *p_segment = NULL;
3447 for (size_t i=0; i<streams.size() && p_segment == NULL; i++)
3449 p_segment = streams[i]->FindSegment( uid );
3454 matroska_segment_t *matroska_stream_t::FindSegment( EbmlBinary & uid ) const
3456 for (size_t i=0; i<segments.size(); i++)
3458 if ( segments[i]->segment_uid == uid )