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)
388 ,psz_muxing_application(NULL)
389 ,psz_writing_application(NULL)
390 ,psz_segment_filename(NULL)
393 ,i_current_edition(-1)
394 ,psz_current_chapter(NULL)
399 index = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * i_index_max );
402 ~matroska_segment_t()
404 for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
406 #define tk tracks[i_track]
407 if( tk->fmt.psz_description )
409 free( tk->fmt.psz_description );
413 free( tk->psz_codec );
415 if( tk->fmt.psz_language )
417 free( tk->fmt.psz_language );
423 if( psz_writing_application )
425 free( psz_writing_application );
427 if( psz_muxing_application )
429 free( psz_muxing_application );
431 if( psz_segment_filename )
433 free( psz_segment_filename );
441 free( psz_date_utc );
453 uint64_t i_timescale;
455 /* duration of the segment */
459 std::vector<mkv_track_t*> tracks;
462 int64_t i_cues_position;
463 int64_t i_chapters_position;
464 int64_t i_tags_position;
468 KaxSegmentUID segment_uid;
469 KaxPrevUID prev_segment_uid;
470 KaxNextUID next_segment_uid;
478 char *psz_muxing_application;
479 char *psz_writing_application;
480 char *psz_segment_filename;
484 std::vector<chapter_edition_t> editions;
485 int i_current_edition;
486 const chapter_item_t *psz_current_chapter;
488 std::vector<KaxSegmentFamily> families;
494 inline chapter_edition_t *Edition()
496 if ( i_current_edition >= 0 && size_t(i_current_edition) < editions.size() )
497 return &editions[i_current_edition];
502 bool PreloadFamily( const matroska_segment_t & segment );
503 size_t PreloadLinked( const demux_sys_t & of_sys );
504 void ParseInfo( EbmlElement *info );
505 void ParseChapters( EbmlElement *chapters );
506 void ParseSeekHead( EbmlElement *seekhead );
507 void ParseTracks( EbmlElement *tracks );
508 void ParseChapterAtom( int i_level, EbmlMaster *ca, chapter_item_t & chapters );
509 void ParseTrackEntry( EbmlMaster *m );
510 void IndexAppendCluster( KaxCluster *cluster );
511 int BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration );
512 bool Select( mtime_t i_start_time );
516 class matroska_stream_t
519 matroska_stream_t( demux_sys_t & demuxer )
522 ,i_current_segment(-1)
528 for ( size_t i=0; i<segments.size(); i++ )
537 std::vector<matroska_segment_t*> segments;
538 int i_current_segment;
542 inline matroska_segment_t *Segment()
544 if ( i_current_segment >= 0 && size_t(i_current_segment) < segments.size() )
545 return segments[i_current_segment];
549 matroska_segment_t *FindSegment( const EbmlBinary & uid ) const;
551 void PreloadFamily( const matroska_segment_t & segment );
552 size_t PreloadLinked( const demux_sys_t & of_sys );
558 demux_sys_t( demux_t & demux )
565 ,i_current_stream(-1)
570 for (size_t i=0; i<streams.size(); i++)
579 mtime_t i_chapter_time;
583 input_title_t *title;
585 std::vector<matroska_stream_t*> streams;
586 int i_current_stream;
588 inline matroska_stream_t *Stream()
590 if ( i_current_stream >= 0 && size_t(i_current_stream) < streams.size() )
591 return streams[i_current_stream];
595 matroska_segment_t *FindSegment( const EbmlBinary & uid ) const;
596 void PreloadFamily( );
597 void PreloadLinked( );
598 matroska_stream_t *AnalyseAllSegmentsFound( EbmlStream *p_estream );
601 static int Demux ( demux_t * );
602 static int Control( demux_t *, int, va_list );
603 static void Seek ( demux_t *, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter );
605 #define MKV_IS_ID( el, C ) ( EbmlId( (*el) ) == C::ClassInfos.GlobalId )
607 static char *UTF8ToStr ( const UTFstring &u );
608 static void LoadCues ( demux_t * );
609 static void InformationCreate ( demux_t * );
611 /*****************************************************************************
612 * Open: initializes matroska demux structures
613 *****************************************************************************/
614 static int Open( vlc_object_t * p_this )
616 demux_t *p_demux = (demux_t*)p_this;
618 matroska_stream_t *p_stream;
619 matroska_segment_t *p_segment;
621 std::string s_path, s_filename;
622 vlc_stream_io_callback *p_io_callback;
623 EbmlStream *p_io_stream;
625 /* peek the begining */
626 if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
628 /* is a valid file */
629 if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
630 p_peek[2] != 0xdf || p_peek[3] != 0xa3 ) return VLC_EGENERIC;
632 /* Set the demux function */
633 p_demux->pf_demux = Demux;
634 p_demux->pf_control = Control;
635 p_demux->p_sys = p_sys = new demux_sys_t( *p_demux );
637 p_io_callback = new vlc_stream_io_callback( p_demux->s );
638 p_io_stream = new EbmlStream( *p_io_callback );
640 if( p_io_stream == NULL )
642 msg_Err( p_demux, "failed to create EbmlStream" );
643 delete p_io_callback;
648 p_stream = p_sys->AnalyseAllSegmentsFound( p_io_stream );
649 if( p_stream == NULL )
651 msg_Err( p_demux, "cannot find KaxSegment" );
654 p_sys->streams.push_back( p_stream );
655 p_sys->i_current_stream = 0;
657 p_stream->p_in = p_io_callback;
658 p_stream->p_es = p_io_stream;
660 for (size_t i=0; i<p_stream->segments.size(); i++)
662 p_stream->segments[i]->Preload();
664 p_stream->i_current_segment = 0;
666 p_segment = p_stream->Segment();
667 if( p_segment->cluster == NULL )
669 msg_Err( p_demux, "cannot find any cluster, damaged file ?" );
672 // reset the stream reading to the first cluster of the segment used
673 p_stream->p_in->setFilePointer( p_segment->cluster->GetElementPosition() );
675 /* get the files from the same dir from the same family (based on p_demux->psz_path) */
676 /* TODO handle multi-segment files */
677 if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
679 // assume it's a regular file
680 // get the directory path
681 s_path = p_demux->psz_path;
682 if (s_path.at(s_path.length() - 1) == DIRECTORY_SEPARATOR)
684 s_path = s_path.substr(0,s_path.length()-1);
688 if (s_path.find_last_of(DIRECTORY_SEPARATOR) > 0)
690 s_path = s_path.substr(0,s_path.find_last_of(DIRECTORY_SEPARATOR));
694 struct dirent *p_file_item;
695 DIR *p_src_dir = opendir(s_path.c_str());
697 if (p_src_dir != NULL)
699 while ((p_file_item = (dirent *) readdir(p_src_dir)))
701 if (strlen(p_file_item->d_name) > 4)
703 s_filename = s_path + DIRECTORY_SEPARATOR + p_file_item->d_name;
705 if (!s_filename.compare(p_demux->psz_path))
706 continue; // don't reuse the original opened file
708 #if defined(__GNUC__) && (__GNUC__ < 3)
709 if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
710 !s_filename.compare("mka", s_filename.length() - 3, 3))
712 if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
713 !s_filename.compare(s_filename.length() - 3, 3, "mka"))
716 // test wether this file belongs to the our family
717 StdIOCallback *p_file_io = new StdIOCallback(s_filename.c_str(), MODE_READ);
718 EbmlStream *p_estream = new EbmlStream(*p_file_io);
720 p_stream = p_sys->AnalyseAllSegmentsFound( p_estream );
721 if ( p_stream == NULL )
723 msg_Dbg( p_demux, "the file '%s' will not be used", s_filename.c_str() );
729 p_stream->p_in = p_file_io;
730 p_stream->p_es = p_estream;
731 p_sys->streams.push_back( p_stream );
736 closedir( p_src_dir );
740 p_sys->PreloadFamily( );
741 p_sys->PreloadLinked( );
743 /* *** Load the cue if found *** */
744 if( p_segment->i_cues_position >= 0 )
746 vlc_bool_t b_seekable;
748 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
755 if( !p_segment->b_cues || p_segment->i_index <= 0 )
757 msg_Warn( p_demux, "no cues/empty cues found->seek won't be precise" );
759 p_segment->IndexAppendCluster( p_segment->cluster );
761 p_segment->b_cues = VLC_FALSE;
764 /* add information */
765 InformationCreate( p_demux );
767 if ( !p_segment->Select( 0 ) )
769 msg_Err( p_demux, "cannot use the segment" );
780 /*****************************************************************************
781 * Close: frees unused data
782 *****************************************************************************/
783 static void Close( vlc_object_t *p_this )
785 demux_t *p_demux = (demux_t*)p_this;
786 demux_sys_t *p_sys = p_demux->p_sys;
787 matroska_stream_t *p_stream = p_sys->Stream();
788 if ( p_stream != NULL )
790 matroska_segment_t *p_segment = p_stream->Segment();
793 delete p_segment->segment;
799 /*****************************************************************************
801 *****************************************************************************/
802 static int Control( demux_t *p_demux, int i_query, va_list args )
804 demux_sys_t *p_sys = p_demux->p_sys;
805 matroska_stream_t *p_stream = p_sys->Stream();
806 if ( p_stream == NULL ) return VLC_EGENERIC;
807 matroska_segment_t *p_segment = p_stream->Segment();
812 vlc_meta_t **pp_meta;
817 pp_meta = (vlc_meta_t**)va_arg( args, vlc_meta_t** );
818 *pp_meta = vlc_meta_Duplicate( p_sys->meta );
821 case DEMUX_GET_LENGTH:
822 pi64 = (int64_t*)va_arg( args, int64_t * );
823 if( p_segment && p_segment->f_duration > 0.0 )
825 *pi64 = (int64_t)(p_segment->f_duration * 1000);
830 case DEMUX_GET_POSITION:
831 pf = (double*)va_arg( args, double * );
832 if ( p_segment && p_segment->f_duration > 0.0 )
833 *pf = (double)p_sys->i_pts / (1000.0 * p_segment->f_duration);
836 case DEMUX_SET_POSITION:
837 f = (double)va_arg( args, double );
838 Seek( p_demux, -1, f, NULL );
842 pi64 = (int64_t*)va_arg( args, int64_t * );
843 *pi64 = p_sys->i_pts;
846 case DEMUX_GET_TITLE_INFO:
847 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
849 input_title_t ***ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
850 int *pi_int = (int*)va_arg( args, int* );
853 *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) );
855 (*ppp_title)[0] = vlc_input_title_Duplicate( p_sys->title );
861 case DEMUX_SET_TITLE:
862 /* TODO handle editions as titles & DVD titles as well */
863 if( p_sys->title && p_sys->title->i_seekpoint > 0 )
869 case DEMUX_SET_SEEKPOINT:
870 /* FIXME do a better implementation */
871 i_skp = (int)va_arg( args, int );
873 if( p_sys->title && i_skp < p_sys->title->i_seekpoint)
875 Seek( p_demux, (int64_t)p_sys->title->seekpoint[i_skp]->i_time_offset, -1, NULL);
876 p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
877 p_demux->info.i_seekpoint = i_skp;
889 int matroska_segment_t::BlockGet( KaxBlock **pp_block, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
900 if( sys.demuxer.b_die )
906 i_level = ep->GetLevel();
908 if( el == NULL && *pp_block != NULL )
910 /* update the index */
911 #define idx index[i_index - 1]
912 if( i_index > 0 && idx.i_time == -1 )
914 idx.i_time = (*pp_block)->GlobalTimecode() / (mtime_t)1000;
915 idx.b_key = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
923 if( ep->GetLevel() > 1 )
928 msg_Warn( &sys.demuxer, "EOF" );
935 if( MKV_IS_ID( el, KaxCluster ) )
937 cluster = (KaxCluster*)el;
939 /* add it to the index */
941 ( i_index > 0 && index[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
943 IndexAppendCluster( cluster );
946 // reset silent tracks
947 for (size_t i=0; i<tracks.size(); i++)
949 tracks[i]->b_silent = VLC_FALSE;
954 else if( MKV_IS_ID( el, KaxCues ) )
956 msg_Warn( &sys.demuxer, "find KaxCues FIXME" );
961 msg_Dbg( &sys.demuxer, "unknown (%s)", typeid( el ).name() );
964 else if( i_level == 2 )
966 if( MKV_IS_ID( el, KaxClusterTimecode ) )
968 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
970 ctc.ReadData( es.I_O(), SCOPE_ALL_DATA );
971 cluster->InitTimecode( uint64( ctc ), i_timescale );
973 else if( MKV_IS_ID( el, KaxClusterSilentTracks ) )
977 else if( MKV_IS_ID( el, KaxBlockGroup ) )
982 else if( i_level == 3 )
984 if( MKV_IS_ID( el, KaxBlock ) )
986 *pp_block = (KaxBlock*)el;
988 (*pp_block)->ReadData( es.I_O() );
989 (*pp_block)->SetParent( *cluster );
993 else if( MKV_IS_ID( el, KaxBlockDuration ) )
995 KaxBlockDuration &dur = *(KaxBlockDuration*)el;
997 dur.ReadData( es.I_O() );
998 *pi_duration = uint64( dur );
1000 else if( MKV_IS_ID( el, KaxReferenceBlock ) )
1002 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
1004 ref.ReadData( es.I_O() );
1005 if( *pi_ref1 == -1 )
1007 *pi_ref1 = int64( ref );
1011 *pi_ref2 = int64( ref );
1014 else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
1016 KaxClusterSilentTrackNumber &track_num = *(KaxClusterSilentTrackNumber*)el;
1017 track_num.ReadData( es.I_O() );
1019 for (size_t i=0; i<tracks.size(); i++)
1021 if ( tracks[i]->i_number == uint32(track_num))
1023 tracks[i]->b_silent = VLC_TRUE;
1031 msg_Err( &sys.demuxer, "invalid level = %d", i_level );
1032 return VLC_EGENERIC;
1037 static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem)
1040 if( !(p_block = block_New( p_demux, i_mem ) ) ) return NULL;
1041 memcpy( p_block->p_buffer, p_mem, i_mem );
1042 //p_block->i_rate = p_input->stream.control.i_rate;
1046 static void BlockDecode( demux_t *p_demux, KaxBlock *block, mtime_t i_pts,
1047 mtime_t i_duration )
1049 demux_sys_t *p_sys = p_demux->p_sys;
1050 matroska_stream_t *p_stream = p_sys->Stream();
1051 matroska_segment_t *p_segment = p_stream->Segment();
1057 #define tk p_segment->tracks[i_track]
1058 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1060 if( tk->i_number == block->TrackNum() )
1066 if( i_track >= p_segment->tracks.size() )
1068 msg_Err( p_demux, "invalid track number=%d", block->TrackNum() );
1071 if( tk->p_es == NULL )
1073 msg_Err( p_demux, "unknown track number=%d", block->TrackNum() );
1076 if( i_pts < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
1078 return; /* discard audio packets that shouldn't be rendered */
1081 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
1084 tk->b_inited = VLC_FALSE;
1088 /* First send init data */
1089 if( !tk->b_inited && tk->i_data_init > 0 )
1093 msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
1094 p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init );
1095 if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
1097 tk->b_inited = VLC_TRUE;
1100 for( i = 0; i < block->NumberFrames(); i++ )
1103 DataBuffer &data = block->GetBuffer(i);
1105 p_block = MemToBlock( p_demux, data.Buffer(), data.Size() );
1107 if( p_block == NULL )
1112 #if defined(HAVE_ZLIB_H)
1113 if( tk->i_compression_type )
1115 p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
1119 // TODO implement correct timestamping when B frames are used
1120 if( tk->fmt.i_cat != VIDEO_ES )
1122 p_block->i_dts = p_block->i_pts = i_pts;
1126 p_block->i_dts = i_pts;
1130 if( tk->fmt.i_cat == SPU_ES && strcmp( tk->psz_codec, "S_VOBSUB" ) )
1132 p_block->i_length = i_duration * 1000;
1134 msg_Warn( p_demux, "Sending block %d", p_block );
1135 es_out_Send( p_demux->out, tk->p_es, p_block );
1137 /* use time stamp only for first block */
1144 matroska_stream_t *demux_sys_t::AnalyseAllSegmentsFound( EbmlStream *p_estream )
1146 int i_upper_lvl = 0;
1148 EbmlElement *p_l0, *p_l1, *p_l2;
1149 bool b_keep_stream = false, b_keep_segment;
1151 // verify the EBML Header
1152 p_l0 = p_estream->FindNextID(EbmlHead::ClassInfos, 0xFFFFFFFFL);
1157 p_l0->SkipData(*p_estream, EbmlHead_Context);
1160 // find all segments in this file
1161 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1167 matroska_stream_t *p_stream1 = new matroska_stream_t( *this );
1171 if (EbmlId(*p_l0) == KaxSegment::ClassInfos.GlobalId)
1174 matroska_segment_t *p_segment1 = new matroska_segment_t( *this, *p_estream );
1175 b_keep_segment = false;
1177 ep = new EbmlParser(p_estream, p_l0);
1178 p_segment1->ep = ep;
1179 p_segment1->segment = (KaxSegment*)p_l0;
1181 while ((p_l1 = ep->Get()))
1183 if (MKV_IS_ID(p_l1, KaxInfo))
1185 // find the families of this segment
1186 KaxInfo *p_info = static_cast<KaxInfo*>(p_l1);
1188 p_info->Read(*p_estream, KaxInfo::ClassInfos.Context, i_upper_lvl, p_l2, true);
1189 for( i = 0; i < p_info->ListSize(); i++ )
1191 EbmlElement *l = (*p_info)[i];
1193 if( MKV_IS_ID( l, KaxSegmentUID ) )
1195 KaxSegmentUID *p_uid = static_cast<KaxSegmentUID*>(l);
1196 b_keep_segment = (FindSegment( *p_uid ) == NULL);
1197 if ( !b_keep_segment )
1198 break; // this segment is already known
1199 p_segment1->segment_uid = *( new KaxSegmentUID(*p_uid) );
1201 else if( MKV_IS_ID( l, KaxPrevUID ) )
1203 p_segment1->prev_segment_uid = *( new KaxPrevUID( *static_cast<KaxPrevUID*>(l) ) );
1205 else if( MKV_IS_ID( l, KaxNextUID ) )
1207 p_segment1->next_segment_uid = *( new KaxNextUID( *static_cast<KaxNextUID*>(l) ) );
1209 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
1211 KaxSegmentFamily *p_fam = new KaxSegmentFamily( *static_cast<KaxSegmentFamily*>(l) );
1212 std::vector<KaxSegmentFamily>::iterator iter;
1213 p_segment1->families.push_back( *p_fam );
1219 if ( b_keep_segment )
1221 b_keep_stream = true;
1222 p_stream1->segments.push_back( p_segment1 );
1228 p_l0->SkipData(*p_estream, EbmlHead_Context);
1229 p_l0 = p_estream->FindNextID(KaxSegment::ClassInfos, 0xFFFFFFFFL);
1232 if ( !b_keep_stream )
1241 bool matroska_segment_t::Select( mtime_t i_start_time )
1246 msg_Dbg( &sys.demuxer, "found %d es", tracks.size() );
1247 for( i_track = 0; i_track < tracks.size(); i_track++ )
1249 #define tk tracks[i_track]
1250 if( tk->fmt.i_cat == UNKNOWN_ES )
1252 msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", i_track, tk->i_number );
1257 if( !strcmp( tk->psz_codec, "V_MS/VFW/FOURCC" ) )
1259 if( tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
1261 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
1262 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1266 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk->p_extra_data;
1268 tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
1269 tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
1270 tk->fmt.i_codec = GetFOURCC( &p_bih->biCompression );
1272 tk->fmt.i_extra = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
1273 if( tk->fmt.i_extra > 0 )
1275 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1276 memcpy( tk->fmt.p_extra, &p_bih[1], tk->fmt.i_extra );
1280 else if( !strcmp( tk->psz_codec, "V_MPEG1" ) ||
1281 !strcmp( tk->psz_codec, "V_MPEG2" ) )
1283 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
1285 else if( !strncmp( tk->psz_codec, "V_MPEG4", 7 ) )
1287 if( !strcmp( tk->psz_codec, "V_MPEG4/MS/V3" ) )
1289 tk->fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
1291 else if( !strcmp( tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
1293 tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
1294 tk->fmt.b_packetized = VLC_FALSE;
1295 tk->fmt.i_extra = tk->i_extra_data;
1296 tk->fmt.p_extra = malloc( tk->i_extra_data );
1297 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1301 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
1304 else if( !strcmp( tk->psz_codec, "V_QUICKTIME" ) )
1306 MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
1307 stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
1310 MP4_ReadBoxCommon( p_mp4_stream, p_box );
1311 MP4_ReadBox_sample_vide( p_mp4_stream, p_box );
1312 tk->fmt.i_codec = p_box->i_type;
1313 tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
1314 tk->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
1315 tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
1316 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1317 memcpy( tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tk->fmt.i_extra );
1318 MP4_FreeBox_sample_vide( p_box );
1319 stream_MemoryDelete( p_mp4_stream, VLC_TRUE );
1321 else if( !strcmp( tk->psz_codec, "A_MS/ACM" ) )
1323 if( tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
1325 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
1326 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1330 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk->p_extra_data;
1332 wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk->fmt.i_codec, NULL );
1334 tk->fmt.audio.i_channels = GetWLE( &p_wf->nChannels );
1335 tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
1336 tk->fmt.i_bitrate = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
1337 tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
1338 tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
1340 tk->fmt.i_extra = GetWLE( &p_wf->cbSize );
1341 if( tk->fmt.i_extra > 0 )
1343 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1344 memcpy( tk->fmt.p_extra, &p_wf[1], tk->fmt.i_extra );
1348 else if( !strcmp( tk->psz_codec, "A_MPEG/L3" ) ||
1349 !strcmp( tk->psz_codec, "A_MPEG/L2" ) ||
1350 !strcmp( tk->psz_codec, "A_MPEG/L1" ) )
1352 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
1354 else if( !strcmp( tk->psz_codec, "A_AC3" ) )
1356 tk->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
1358 else if( !strcmp( tk->psz_codec, "A_DTS" ) )
1360 tk->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
1362 else if( !strcmp( tk->psz_codec, "A_FLAC" ) )
1364 tk->fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
1365 tk->fmt.i_extra = tk->i_extra_data;
1366 tk->fmt.p_extra = malloc( tk->i_extra_data );
1367 memcpy( tk->fmt.p_extra,tk->p_extra_data, tk->i_extra_data );
1369 else if( !strcmp( tk->psz_codec, "A_VORBIS" ) )
1371 int i, i_offset = 1, i_size[3], i_extra;
1374 tk->fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
1376 /* Split the 3 headers */
1377 if( tk->p_extra_data[0] != 0x02 )
1378 msg_Err( &sys.demuxer, "invalid vorbis header" );
1380 for( i = 0; i < 2; i++ )
1383 while( i_offset < tk->i_extra_data )
1385 i_size[i] += tk->p_extra_data[i_offset];
1386 if( tk->p_extra_data[i_offset++] != 0xff ) break;
1390 i_size[0] = __MIN(i_size[0], tk->i_extra_data - i_offset);
1391 i_size[1] = __MIN(i_size[1], tk->i_extra_data -i_offset -i_size[0]);
1392 i_size[2] = tk->i_extra_data - i_offset - i_size[0] - i_size[1];
1394 tk->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
1395 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1396 p_extra = (uint8_t *)tk->fmt.p_extra; i_extra = 0;
1397 for( i = 0; i < 3; i++ )
1399 *(p_extra++) = i_size[i] >> 8;
1400 *(p_extra++) = i_size[i] & 0xFF;
1401 memcpy( p_extra, tk->p_extra_data + i_offset + i_extra,
1403 p_extra += i_size[i];
1404 i_extra += i_size[i];
1407 else if( !strncmp( tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
1408 !strncmp( tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
1410 int i_profile, i_srate;
1411 static unsigned int i_sample_rates[] =
1413 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
1414 16000, 12000, 11025, 8000, 7350, 0, 0, 0
1417 tk->fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
1418 /* create data for faad (MP4DecSpecificDescrTag)*/
1420 if( !strcmp( &tk->psz_codec[12], "MAIN" ) )
1424 else if( !strcmp( &tk->psz_codec[12], "LC" ) )
1428 else if( !strcmp( &tk->psz_codec[12], "SSR" ) )
1437 for( i_srate = 0; i_srate < 13; i_srate++ )
1439 if( i_sample_rates[i_srate] == tk->fmt.audio.i_rate )
1444 msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
1446 tk->fmt.i_extra = 2;
1447 tk->fmt.p_extra = malloc( tk->fmt.i_extra );
1448 ((uint8_t*)tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
1449 ((uint8_t*)tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk->fmt.audio.i_channels << 3);
1451 else if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) ||
1452 !strcmp( tk->psz_codec, "A_PCM/INT/LIT" ) ||
1453 !strcmp( tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
1455 if( !strcmp( tk->psz_codec, "A_PCM/INT/BIG" ) )
1457 tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
1461 tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
1463 tk->fmt.audio.i_blockalign = ( tk->fmt.audio.i_bitspersample + 7 ) / 8 * tk->fmt.audio.i_channels;
1465 else if( !strcmp( tk->psz_codec, "A_TTA1" ) )
1467 /* FIXME: support this codec */
1468 msg_Err( &sys.demuxer, "TTA not supported yet[%d, n=%d]", i_track, tk->i_number );
1469 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1471 else if( !strcmp( tk->psz_codec, "A_WAVPACK4" ) )
1473 /* FIXME: support this codec */
1474 msg_Err( &sys.demuxer, "Wavpack not supported yet[%d, n=%d]", i_track, tk->i_number );
1475 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1477 else if( !strcmp( tk->psz_codec, "S_TEXT/UTF8" ) )
1479 tk->fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
1480 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1482 else if( !strcmp( tk->psz_codec, "S_TEXT/SSA" ) ||
1483 !strcmp( tk->psz_codec, "S_TEXT/ASS" ) ||
1484 !strcmp( tk->psz_codec, "S_SSA" ) ||
1485 !strcmp( tk->psz_codec, "S_ASS" ))
1487 tk->fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
1488 tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
1490 else if( !strcmp( tk->psz_codec, "S_VOBSUB" ) )
1492 tk->fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
1493 if( tk->i_extra_data )
1496 char *p_buf = (char *)malloc( tk->i_extra_data + 1);
1497 memcpy( p_buf, tk->p_extra_data , tk->i_extra_data );
1498 p_buf[tk->i_extra_data] = '\0';
1500 p_start = strstr( p_buf, "size:" );
1501 if( sscanf( p_start, "size: %dx%d",
1502 &tk->fmt.subs.spu.i_original_frame_width, &tk->fmt.subs.spu.i_original_frame_height ) == 2 )
1504 msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d", tk->fmt.subs.spu.i_original_frame_width, tk->fmt.subs.spu.i_original_frame_height );
1508 msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
1513 else if( !strcmp( tk->psz_codec, "B_VOBBTN" ) )
1515 /* FIXME: support this codec */
1516 msg_Err( &sys.demuxer, "Vob Buttons not supported yet[%d, n=%d]", i_track, tk->i_number );
1517 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1521 msg_Err( &sys.demuxer, "unknow codec id=`%s'", tk->psz_codec );
1522 tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
1526 tk->fmt.i_priority = 1000;
1529 tk->p_es = es_out_Add( sys.demuxer.out, &tk->fmt );
1531 es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_start_time );
1535 sys.i_start_pts = i_start_time;
1538 // reset the stream reading to the first cluster of the segment used
1539 es.I_O().setFilePointer( i_start_pos );
1544 void matroska_segment_t::UnSelect( )
1548 for( i_track = 0; i_track < tracks.size(); i_track++ )
1550 #define tk tracks[i_track]
1551 if ( tk->p_es != NULL )
1553 es_out_Del( sys.demuxer.out, tk->p_es );
1560 static void UpdateCurrentToChapter( demux_t & demux )
1562 demux_sys_t & sys = *demux.p_sys;
1563 matroska_stream_t *p_stream = sys.Stream();
1564 matroska_segment_t *p_segment = p_stream->Segment();
1565 const chapter_item_t *psz_curr_chapter;
1567 /* update current chapter/seekpoint */
1568 if ( p_segment->editions.size())
1570 /* 1st, we need to know in which chapter we are */
1571 psz_curr_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( sys.i_pts );
1573 /* we have moved to a new chapter */
1574 if (p_segment->psz_current_chapter != NULL && psz_curr_chapter != NULL && p_segment->psz_current_chapter != psz_curr_chapter)
1576 if (p_segment->psz_current_chapter->i_seekpoint_num != psz_curr_chapter->i_seekpoint_num && psz_curr_chapter->i_seekpoint_num > 0)
1578 demux.info.i_update |= INPUT_UPDATE_SEEKPOINT;
1579 demux.info.i_seekpoint = psz_curr_chapter->i_seekpoint_num - 1;
1582 if (p_segment->editions[p_segment->i_current_edition].b_ordered )
1584 /* TODO check if we need to silently seek to a new location in the stream (switch to another chapter) */
1585 if (p_segment->psz_current_chapter->i_end_time != psz_curr_chapter->i_start_time)
1586 Seek(&demux, sys.i_pts, -1, psz_curr_chapter);
1587 /* count the last duration time found for each track in a table (-1 not found, -2 silent) */
1588 /* only seek after each duration >= end timecode of the current chapter */
1591 // p_segment->i_user_time = psz_curr_chapter->i_user_start_time - psz_curr_chapter->i_start_time;
1592 // p_segment->i_start_pts = psz_curr_chapter->i_user_start_time;
1594 p_segment->psz_current_chapter = psz_curr_chapter;
1598 static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, const chapter_item_t *psz_chapter)
1600 demux_sys_t *p_sys = p_demux->p_sys;
1601 matroska_stream_t *p_stream = p_sys->Stream();
1602 matroska_segment_t *p_segment = p_stream->Segment();
1603 mtime_t i_time_offset = 0;
1606 int64_t i_block_duration;
1607 int64_t i_block_ref1;
1608 int64_t i_block_ref2;
1611 int i_track_skipping;
1614 msg_Dbg( p_demux, "seek request to "I64Fd" (%f%%)", i_date, f_percent );
1615 if( i_date < 0 && f_percent < 0 )
1617 msg_Warn( p_demux, "cannot seek nowhere !" );
1620 if( f_percent > 1.0 )
1622 msg_Warn( p_demux, "cannot seek so far !" );
1626 delete p_segment->ep;
1627 p_segment->ep = new EbmlParser( p_stream->p_es, p_segment->segment );
1628 p_segment->cluster = NULL;
1630 /* seek without index or without date */
1631 if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
1633 if (p_segment->f_duration >= 0)
1635 i_date = int64_t( f_percent * p_segment->f_duration * 1000.0 );
1639 int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
1641 msg_Dbg( p_demux, "inacurate way of seeking" );
1642 for( i_index = 0; i_index < p_segment->i_index; i_index++ )
1644 if( p_segment->index[i_index].i_position >= i_pos)
1649 if( i_index == p_segment->i_index )
1654 i_date = p_segment->index[i_index].i_time;
1657 if( p_segment->index[i_index].i_position < i_pos )
1661 msg_Warn( p_demux, "searching for cluster, could take some time" );
1663 /* search a cluster */
1664 while( ( el = p_sys->ep->Get() ) != NULL )
1666 if( MKV_IS_ID( el, KaxCluster ) )
1668 KaxCluster *cluster = (KaxCluster*)el;
1670 /* add it to the index */
1671 p_segment->IndexAppendCluster( cluster );
1673 if( (int64_t)cluster->GetElementPosition() >= i_pos )
1675 p_sys->cluster = cluster;
1686 // find the actual time for an ordered edition
1687 if ( psz_chapter == NULL )
1689 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1691 /* 1st, we need to know in which chapter we are */
1692 psz_chapter = p_segment->editions[p_segment->i_current_edition].FindTimecode( i_date );
1696 if ( psz_chapter != NULL )
1698 p_segment->psz_current_chapter = psz_chapter;
1699 p_sys->i_chapter_time = i_time_offset = psz_chapter->i_user_start_time - psz_chapter->i_start_time;
1700 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
1701 p_demux->info.i_seekpoint = psz_chapter->i_seekpoint_num - 1;
1704 for( ; i_index < p_segment->i_index; i_index++ )
1706 if( p_segment->index[i_index].i_time + i_time_offset > i_date )
1717 msg_Dbg( p_demux, "seek got "I64Fd" (%d%%)",
1718 p_segment->index[i_index].i_time,
1719 (int)( 100 * p_segment->index[i_index].i_position /
1720 stream_Size( p_demux->s ) ) );
1722 p_stream->p_in->setFilePointer( p_segment->index[i_index].i_position,
1725 p_sys->i_start_pts = i_date;
1727 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1729 /* now parse until key frame */
1730 #define tk p_segment->tracks[i_track]
1731 i_track_skipping = 0;
1732 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1734 if( tk->fmt.i_cat == VIDEO_ES )
1736 tk->b_search_keyframe = VLC_TRUE;
1739 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME, tk->p_es, i_date );
1743 while( i_track_skipping > 0 )
1745 if( p_segment->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1747 msg_Warn( p_demux, "cannot get block EOF?" );
1752 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
1754 if( tk->i_number == block->TrackNum() )
1760 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1762 if( i_track < p_segment->tracks.size() )
1764 if( p_sys->i_pts >= p_sys->i_start_pts )
1766 BlockDecode( p_demux, block, p_sys->i_pts, 0 );
1767 i_track_skipping = 0;
1769 else if( tk->fmt.i_cat == VIDEO_ES )
1771 if( i_block_ref1 == -1 && tk->b_search_keyframe )
1773 tk->b_search_keyframe = VLC_FALSE;
1776 if( !tk->b_search_keyframe )
1778 BlockDecode( p_demux, block, p_sys->i_pts, 0 );
1788 /*****************************************************************************
1789 * Demux: reads and demuxes data packets
1790 *****************************************************************************
1791 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
1792 *****************************************************************************/
1793 static int Demux( demux_t *p_demux)
1795 demux_sys_t *p_sys = p_demux->p_sys;
1796 matroska_stream_t *p_stream = p_sys->Stream();
1797 matroska_segment_t *p_segment = p_stream->Segment();
1798 int i_block_count = 0;
1801 int64_t i_block_duration;
1802 int64_t i_block_ref1;
1803 int64_t i_block_ref2;
1807 if( p_sys->i_pts >= p_sys->i_start_pts )
1808 UpdateCurrentToChapter( *p_demux );
1810 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered && p_segment->psz_current_chapter == NULL )
1812 /* nothing left to read in this ordered edition */
1813 if ( p_stream->i_current_segment == p_stream->segments.size() - 1)
1815 p_segment->UnSelect( );
1817 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1819 /* switch to the next segment (TODO update the duration) */
1820 p_stream->i_current_segment++;
1821 p_segment = p_stream->Segment();
1822 if ( !p_segment || !p_segment->Select( 0 ) )
1827 if( p_segment->BlockGet( &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
1829 if ( p_segment->editions.size() && p_segment->editions[p_segment->i_current_edition].b_ordered )
1831 // check if there are more chapters to read
1832 if ( p_segment->psz_current_chapter != NULL )
1834 p_sys->i_pts = p_segment->psz_current_chapter->i_user_end_time;
1840 msg_Warn( p_demux, "cannot get block EOF?" );
1841 p_segment->UnSelect( );
1843 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1845 /* switch to the next segment (TODO update the duration) */
1846 p_stream->i_current_segment++;
1847 p_segment = p_stream->Segment();
1848 if ( !p_segment || !p_segment->Select( 0 ) )
1852 p_sys->i_current_stream++;
1853 p_stream = p_sys->Stream();
1856 p_segment = p_stream->Segment();
1857 if ( p_segment && p_segment->Select( 0 ) )
1867 p_sys->i_pts = p_sys->i_chapter_time + block->GlobalTimecode() / (mtime_t) 1000;
1869 if( p_sys->i_pts >= p_sys->i_start_pts )
1871 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
1874 BlockDecode( p_demux, block, p_sys->i_pts, i_block_duration );
1879 // TODO optimize when there is need to leave or when seeking has been called
1880 if( i_block_count > 5 )
1889 /*****************************************************************************
1891 *****************************************************************************/
1892 vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
1898 uint32 vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
1900 if( i_size <= 0 || mb_eof )
1905 return stream_Read( s, p_buffer, i_size );
1907 void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
1913 case seek_beginning:
1917 i_pos = stream_Size( s ) - i_offset;
1920 i_pos= stream_Tell( s ) + i_offset;
1924 if( i_pos < 0 || i_pos >= stream_Size( s ) )
1931 if( stream_Seek( s, i_pos ) )
1937 size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
1941 uint64 vlc_stream_io_callback::getFilePointer( void )
1943 return stream_Tell( s );
1945 void vlc_stream_io_callback::close( void )
1951 /*****************************************************************************
1952 * Ebml Stream parser
1953 *****************************************************************************/
1954 EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
1962 for( i = 1; i < 6; i++ )
1968 mb_keep = VLC_FALSE;
1971 EbmlParser::~EbmlParser( void )
1975 for( i = 1; i < mi_level; i++ )
1981 mb_keep = VLC_FALSE;
1985 void EbmlParser::Up( void )
1987 if( mi_user_level == mi_level )
1989 fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
1995 void EbmlParser::Down( void )
2001 void EbmlParser::Keep( void )
2006 int EbmlParser::GetLevel( void )
2008 return mi_user_level;
2011 void EbmlParser::Reset( void )
2013 while ( mi_level > 0)
2015 delete m_el[mi_level];
2016 m_el[mi_level] = NULL;
2019 mi_user_level = mi_level = 1;
2020 #if LIBEBML_VERSION >= 0x000704
2021 // a little faster and cleaner
2022 m_es->I_O().setFilePointer( static_cast<KaxSegment*>(m_el[0])->GetGlobalPosition(0) );
2024 m_es->I_O().setFilePointer( m_el[0]->GetElementPosition() + m_el[0]->ElementSize(true) - m_el[0]->GetSize() );
2028 EbmlElement *EbmlParser::Get( void )
2032 if( mi_user_level != mi_level )
2038 EbmlElement *ret = m_got;
2044 if( m_el[mi_level] )
2046 m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
2049 delete m_el[mi_level];
2051 mb_keep = VLC_FALSE;
2054 m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
2065 delete m_el[mi_level - 1];
2066 m_got = m_el[mi_level -1] = m_el[mi_level];
2067 m_el[mi_level] = NULL;
2074 else if( m_el[mi_level] == NULL )
2076 fprintf( stderr," m_el[mi_level] == NULL\n" );
2079 return m_el[mi_level];
2083 /*****************************************************************************
2085 * * LoadCues : load the cues element and update index
2087 * * LoadTags : load ... the tags element
2089 * * InformationCreate : create all information, load tags if present
2091 *****************************************************************************/
2092 static void LoadCues( demux_t *p_demux )
2094 demux_sys_t *p_sys = p_demux->p_sys;
2095 matroska_stream_t *p_stream = p_sys->Stream();
2096 matroska_segment_t *p_segment = p_stream->Segment();
2097 int64_t i_sav_position = p_stream->p_in->getFilePointer();
2099 EbmlElement *el, *cues;
2101 msg_Dbg( p_demux, "loading cues" );
2102 p_stream->p_in->setFilePointer( p_segment->i_cues_position, seek_beginning );
2103 cues = p_stream->p_es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
2107 msg_Err( p_demux, "cannot load cues (broken seekhead or file)" );
2108 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2112 ep = new EbmlParser( p_stream->p_es, cues );
2113 while( ( el = ep->Get() ) != NULL )
2115 if( MKV_IS_ID( el, KaxCuePoint ) )
2117 #define idx p_segment->index[p_segment->i_index]
2120 idx.i_block_number= -1;
2121 idx.i_position = -1;
2123 idx.b_key = VLC_TRUE;
2126 while( ( el = ep->Get() ) != NULL )
2128 if( MKV_IS_ID( el, KaxCueTime ) )
2130 KaxCueTime &ctime = *(KaxCueTime*)el;
2132 ctime.ReadData( p_stream->p_es->I_O() );
2134 idx.i_time = uint64( ctime ) * p_segment->i_timescale / (mtime_t)1000;
2136 else if( MKV_IS_ID( el, KaxCueTrackPositions ) )
2139 while( ( el = ep->Get() ) != NULL )
2141 if( MKV_IS_ID( el, KaxCueTrack ) )
2143 KaxCueTrack &ctrack = *(KaxCueTrack*)el;
2145 ctrack.ReadData( p_stream->p_es->I_O() );
2146 idx.i_track = uint16( ctrack );
2148 else if( MKV_IS_ID( el, KaxCueClusterPosition ) )
2150 KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
2152 ccpos.ReadData( p_stream->p_es->I_O() );
2153 idx.i_position = p_segment->segment->GetGlobalPosition( uint64( ccpos ) );
2155 else if( MKV_IS_ID( el, KaxCueBlockNumber ) )
2157 KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
2159 cbnum.ReadData( p_stream->p_es->I_O() );
2160 idx.i_block_number = uint32( cbnum );
2164 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2171 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2177 msg_Dbg( p_demux, " * added time="I64Fd" pos="I64Fd
2178 " track=%d bnum=%d", idx.i_time, idx.i_position,
2179 idx.i_track, idx.i_block_number );
2182 p_segment->i_index++;
2183 if( p_segment->i_index >= p_segment->i_index_max )
2185 p_segment->i_index_max += 1024;
2186 p_segment->index = (mkv_index_t*)realloc( p_segment->index, sizeof( mkv_index_t ) * p_segment->i_index_max );
2192 msg_Dbg( p_demux, " * Unknown (%s)", typeid(*el).name() );
2198 p_segment->b_cues = VLC_TRUE;
2200 msg_Dbg( p_demux, "loading cues done." );
2201 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2204 static void LoadTags( demux_t *p_demux )
2206 demux_sys_t *p_sys = p_demux->p_sys;
2207 matroska_stream_t *p_stream = p_sys->Stream();
2208 matroska_segment_t *p_segment = p_stream->Segment();
2209 int64_t i_sav_position = p_stream->p_in->getFilePointer();
2211 EbmlElement *el, *tags;
2213 msg_Dbg( p_demux, "loading tags" );
2214 p_stream->p_in->setFilePointer( p_segment->i_tags_position, seek_beginning );
2215 tags = p_stream->p_es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
2219 msg_Err( p_demux, "cannot load tags (broken seekhead or file)" );
2220 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2224 msg_Dbg( p_demux, "Tags" );
2225 ep = new EbmlParser( p_stream->p_es, tags );
2226 while( ( el = ep->Get() ) != NULL )
2228 if( MKV_IS_ID( el, KaxTag ) )
2230 msg_Dbg( p_demux, "+ Tag" );
2232 while( ( el = ep->Get() ) != NULL )
2234 if( MKV_IS_ID( el, KaxTagTargets ) )
2236 msg_Dbg( p_demux, "| + Targets" );
2238 while( ( el = ep->Get() ) != NULL )
2240 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2244 else if( MKV_IS_ID( el, KaxTagGeneral ) )
2246 msg_Dbg( p_demux, "| + General" );
2248 while( ( el = ep->Get() ) != NULL )
2250 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2254 else if( MKV_IS_ID( el, KaxTagGenres ) )
2256 msg_Dbg( p_demux, "| + Genres" );
2258 while( ( el = ep->Get() ) != NULL )
2260 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2264 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
2266 msg_Dbg( p_demux, "| + Audio Specific" );
2268 while( ( el = ep->Get() ) != NULL )
2270 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2274 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
2276 msg_Dbg( p_demux, "| + Images Specific" );
2278 while( ( el = ep->Get() ) != NULL )
2280 msg_Dbg( p_demux, "| | + Unknown (%s)", typeid( *el ).name() );
2284 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
2286 msg_Dbg( p_demux, "| + Multi Comment" );
2288 else if( MKV_IS_ID( el, KaxTagMultiCommercial ) )
2290 msg_Dbg( p_demux, "| + Multi Commercial" );
2292 else if( MKV_IS_ID( el, KaxTagMultiDate ) )
2294 msg_Dbg( p_demux, "| + Multi Date" );
2296 else if( MKV_IS_ID( el, KaxTagMultiEntity ) )
2298 msg_Dbg( p_demux, "| + Multi Entity" );
2300 else if( MKV_IS_ID( el, KaxTagMultiIdentifier ) )
2302 msg_Dbg( p_demux, "| + Multi Identifier" );
2304 else if( MKV_IS_ID( el, KaxTagMultiLegal ) )
2306 msg_Dbg( p_demux, "| + Multi Legal" );
2308 else if( MKV_IS_ID( el, KaxTagMultiTitle ) )
2310 msg_Dbg( p_demux, "| + Multi Title" );
2314 msg_Dbg( p_demux, "| + Unknown (%s)", typeid( *el ).name() );
2321 msg_Dbg( p_demux, "+ Unknown (%s)", typeid( *el ).name() );
2327 msg_Dbg( p_demux, "loading tags done." );
2328 p_stream->p_in->setFilePointer( i_sav_position, seek_beginning );
2331 /*****************************************************************************
2333 *****************************************************************************/
2334 void matroska_segment_t::ParseSeekHead( EbmlElement *seekhead )
2339 int i_upper_level = 0;
2341 msg_Dbg( &sys.demuxer, "| + Seek head" );
2343 /* Master elements */
2344 m = static_cast<EbmlMaster *>(seekhead);
2345 m->Read( es, seekhead->Generic().Context, i_upper_level, el, true );
2347 for( i = 0; i < m->ListSize(); i++ )
2349 EbmlElement *l = (*m)[i];
2351 if( MKV_IS_ID( l, KaxSeek ) )
2353 EbmlMaster *sk = static_cast<EbmlMaster *>(l);
2354 EbmlId id = EbmlVoid::ClassInfos.GlobalId;
2359 for( j = 0; j < sk->ListSize(); j++ )
2361 EbmlElement *l = (*sk)[j];
2363 if( MKV_IS_ID( l, KaxSeekID ) )
2365 KaxSeekID &sid = *(KaxSeekID*)l;
2366 id = EbmlId( sid.GetBuffer(), sid.GetSize() );
2368 else if( MKV_IS_ID( l, KaxSeekPosition ) )
2370 KaxSeekPosition &spos = *(KaxSeekPosition*)l;
2371 i_pos = uint64( spos );
2375 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
2381 if( id == KaxCues::ClassInfos.GlobalId )
2383 msg_Dbg( &sys.demuxer, "| | | = cues at "I64Fd, i_pos );
2384 i_cues_position = segment->GetGlobalPosition( i_pos );
2386 else if( id == KaxChapters::ClassInfos.GlobalId )
2388 msg_Dbg( &sys.demuxer, "| | | = chapters at "I64Fd, i_pos );
2389 i_chapters_position = segment->GetGlobalPosition( i_pos );
2391 else if( id == KaxTags::ClassInfos.GlobalId )
2393 msg_Dbg( &sys.demuxer, "| | | = tags at "I64Fd, i_pos );
2394 i_tags_position = segment->GetGlobalPosition( i_pos );
2400 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2405 /*****************************************************************************
2407 *****************************************************************************/
2408 void matroska_segment_t::ParseTrackEntry( EbmlMaster *m )
2414 msg_Dbg( &sys.demuxer, "| | + Track Entry" );
2416 tk = new mkv_track_t();
2417 tracks.push_back( tk );
2419 /* Init the track */
2420 memset( tk, 0, sizeof( mkv_track_t ) );
2422 es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
2423 tk->fmt.psz_language = strdup("English");
2424 tk->fmt.psz_description = NULL;
2426 tk->b_default = VLC_TRUE;
2427 tk->b_enabled = VLC_TRUE;
2428 tk->b_silent = VLC_FALSE;
2429 tk->i_number = tracks.size() - 1;
2430 tk->i_extra_data = 0;
2431 tk->p_extra_data = NULL;
2432 tk->psz_codec = NULL;
2433 tk->i_default_duration = 0;
2434 tk->f_timecodescale = 1.0;
2436 tk->b_inited = VLC_FALSE;
2437 tk->i_data_init = 0;
2438 tk->p_data_init = NULL;
2440 tk->psz_codec_name = NULL;
2441 tk->psz_codec_settings = NULL;
2442 tk->psz_codec_info_url = NULL;
2443 tk->psz_codec_download_url = NULL;
2445 tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
2447 for( i = 0; i < m->ListSize(); i++ )
2449 EbmlElement *l = (*m)[i];
2451 if( MKV_IS_ID( l, KaxTrackNumber ) )
2453 KaxTrackNumber &tnum = *(KaxTrackNumber*)l;
2455 tk->i_number = uint32( tnum );
2456 msg_Dbg( &sys.demuxer, "| | | + Track Number=%u", uint32( tnum ) );
2458 else if( MKV_IS_ID( l, KaxTrackUID ) )
2460 KaxTrackUID &tuid = *(KaxTrackUID*)l;
2462 msg_Dbg( &sys.demuxer, "| | | + Track UID=%u", uint32( tuid ) );
2464 else if( MKV_IS_ID( l, KaxTrackType ) )
2467 KaxTrackType &ttype = *(KaxTrackType*)l;
2469 switch( uint8(ttype) )
2473 tk->fmt.i_cat = AUDIO_ES;
2477 tk->fmt.i_cat = VIDEO_ES;
2479 case track_subtitle:
2480 psz_type = "subtitle";
2481 tk->fmt.i_cat = SPU_ES;
2484 psz_type = "unknown";
2485 tk->fmt.i_cat = UNKNOWN_ES;
2489 msg_Dbg( &sys.demuxer, "| | | + Track Type=%s", psz_type );
2491 // else if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
2493 // KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
2495 // tk->b_enabled = uint32( fenb );
2496 // msg_Dbg( &sys.demuxer, "| | | + Track Enabled=%u",
2497 // uint32( fenb ) );
2499 else if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
2501 KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
2503 tk->b_default = uint32( fdef );
2504 msg_Dbg( &sys.demuxer, "| | | + Track Default=%u", uint32( fdef ) );
2506 else if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
2508 KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
2510 msg_Dbg( &sys.demuxer, "| | | + Track Lacing=%d", uint32( lac ) );
2512 else if( MKV_IS_ID( l, KaxTrackMinCache ) )
2514 KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
2516 msg_Dbg( &sys.demuxer, "| | | + Track MinCache=%d", uint32( cmin ) );
2518 else if( MKV_IS_ID( l, KaxTrackMaxCache ) )
2520 KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
2522 msg_Dbg( &sys.demuxer, "| | | + Track MaxCache=%d", uint32( cmax ) );
2524 else if( MKV_IS_ID( l, KaxTrackDefaultDuration ) )
2526 KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
2528 tk->i_default_duration = uint64(defd);
2529 msg_Dbg( &sys.demuxer, "| | | + Track Default Duration="I64Fd, uint64(defd) );
2531 else if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
2533 KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
2535 tk->f_timecodescale = float( ttcs );
2536 msg_Dbg( &sys.demuxer, "| | | + Track TimeCodeScale=%f", tk->f_timecodescale );
2538 else if( MKV_IS_ID( l, KaxTrackName ) )
2540 KaxTrackName &tname = *(KaxTrackName*)l;
2542 tk->fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
2543 msg_Dbg( &sys.demuxer, "| | | + Track Name=%s", tk->fmt.psz_description );
2545 else if( MKV_IS_ID( l, KaxTrackLanguage ) )
2547 KaxTrackLanguage &lang = *(KaxTrackLanguage*)l;
2549 tk->fmt.psz_language = strdup( string( lang ).c_str() );
2550 msg_Dbg( &sys.demuxer,
2551 "| | | + Track Language=`%s'", tk->fmt.psz_language );
2553 else if( MKV_IS_ID( l, KaxCodecID ) )
2555 KaxCodecID &codecid = *(KaxCodecID*)l;
2557 tk->psz_codec = strdup( string( codecid ).c_str() );
2558 msg_Dbg( &sys.demuxer, "| | | + Track CodecId=%s", string( codecid ).c_str() );
2560 else if( MKV_IS_ID( l, KaxCodecPrivate ) )
2562 KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)l;
2564 tk->i_extra_data = cpriv.GetSize();
2565 if( tk->i_extra_data > 0 )
2567 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
2568 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
2570 msg_Dbg( &sys.demuxer, "| | | + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
2572 else if( MKV_IS_ID( l, KaxCodecName ) )
2574 KaxCodecName &cname = *(KaxCodecName*)l;
2576 tk->psz_codec_name = UTF8ToStr( UTFstring( cname ) );
2577 msg_Dbg( &sys.demuxer, "| | | + Track Codec Name=%s", tk->psz_codec_name );
2579 else if( MKV_IS_ID( l, KaxContentEncodings ) )
2581 EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
2582 MkvTree( sys.demuxer, 3, "Content Encodings" );
2583 for( unsigned int i = 0; i < cencs->ListSize(); i++ )
2585 EbmlElement *l2 = (*cencs)[i];
2586 if( MKV_IS_ID( l2, KaxContentEncoding ) )
2588 MkvTree( sys.demuxer, 4, "Content Encoding" );
2589 EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
2590 for( unsigned int i = 0; i < cenc->ListSize(); i++ )
2592 EbmlElement *l3 = (*cenc)[i];
2593 if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
2595 KaxContentEncodingOrder &encord = *(KaxContentEncodingOrder*)l3;
2596 MkvTree( sys.demuxer, 5, "Order: %i", uint32( encord ) );
2598 else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
2600 KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
2601 MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
2603 else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
2605 KaxContentEncodingType &enctype = *(KaxContentEncodingType*)l3;
2606 MkvTree( sys.demuxer, 5, "Type: %i", uint32( enctype ) );
2608 else if( MKV_IS_ID( l3, KaxContentCompression ) )
2610 EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
2611 MkvTree( sys.demuxer, 5, "Content Compression" );
2612 for( unsigned int i = 0; i < compr->ListSize(); i++ )
2614 EbmlElement *l4 = (*compr)[i];
2615 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
2617 KaxContentCompAlgo &compalg = *(KaxContentCompAlgo*)l4;
2618 MkvTree( sys.demuxer, 6, "Compression Algorithm: %i", uint32(compalg) );
2619 if( uint32( compalg ) == 0 )
2621 tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
2626 MkvTree( sys.demuxer, 6, "Unknown (%s)", typeid(*l4).name() );
2633 MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
2640 MkvTree( sys.demuxer, 4, "Unknown (%s)", typeid(*l2).name() );
2645 // else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
2647 // KaxCodecSettings &cset = *(KaxCodecSettings*)l;
2649 // tk->psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
2650 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Settings=%s", tk->psz_codec_settings );
2652 // else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
2654 // KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
2656 // tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
2657 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_info_url );
2659 // else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
2661 // KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
2663 // tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
2664 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Info URL=%s", tk->psz_codec_download_url );
2666 // else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
2668 // KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
2670 // msg_Dbg( &sys.demuxer, "| | | + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
2672 // else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
2674 // KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
2676 // msg_Dbg( &sys.demuxer, "| | | + Track Overlay=%u <== UNUSED", uint32( tovr ) );
2678 else if( MKV_IS_ID( l, KaxTrackVideo ) )
2680 EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
2683 msg_Dbg( &sys.demuxer, "| | | + Track Video" );
2686 for( j = 0; j < tkv->ListSize(); j++ )
2688 EbmlElement *l = (*tkv)[j];
2689 // if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
2691 // KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
2693 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Interlaced=%u", uint8( fint ) );
2695 // else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
2697 // KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
2699 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Stereo Mode=%u", uint8( stereo ) );
2702 if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
2704 KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
2706 tk->fmt.video.i_width = uint16( vwidth );
2707 msg_Dbg( &sys.demuxer, "| | | | + width=%d", uint16( vwidth ) );
2709 else if( MKV_IS_ID( l, KaxVideoPixelHeight ) )
2711 KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)l;
2713 tk->fmt.video.i_height = uint16( vheight );
2714 msg_Dbg( &sys.demuxer, "| | | | + height=%d", uint16( vheight ) );
2716 else if( MKV_IS_ID( l, KaxVideoDisplayWidth ) )
2718 KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)l;
2720 tk->fmt.video.i_visible_width = uint16( vwidth );
2721 msg_Dbg( &sys.demuxer, "| | | | + display width=%d", uint16( vwidth ) );
2723 else if( MKV_IS_ID( l, KaxVideoDisplayHeight ) )
2725 KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)l;
2727 tk->fmt.video.i_visible_height = uint16( vheight );
2728 msg_Dbg( &sys.demuxer, "| | | | + display height=%d", uint16( vheight ) );
2730 else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
2732 KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
2734 tk->f_fps = float( vfps );
2735 msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( vfps ) );
2737 // else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
2739 // KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
2741 // msg_Dbg( &sys.demuxer, "| | | | + Track Video Display Unit=%s",
2742 // uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
2744 // else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
2746 // KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
2748 // msg_Dbg( &sys.demuxer, " | | | + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
2750 // else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
2752 // KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
2754 // msg_Dbg( &sys.demuxer, " | | | + fps=%f", float( gamma ) );
2758 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
2761 if ( tk->fmt.video.i_visible_height && tk->fmt.video.i_visible_width )
2762 tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * tk->fmt.video.i_visible_width / tk->fmt.video.i_visible_height;
2764 else if( MKV_IS_ID( l, KaxTrackAudio ) )
2766 EbmlMaster *tka = static_cast<EbmlMaster*>(l);
2769 msg_Dbg( &sys.demuxer, "| | | + Track Audio" );
2771 for( j = 0; j < tka->ListSize(); j++ )
2773 EbmlElement *l = (*tka)[j];
2775 if( MKV_IS_ID( l, KaxAudioSamplingFreq ) )
2777 KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)l;
2779 tk->fmt.audio.i_rate = (int)float( afreq );
2780 msg_Dbg( &sys.demuxer, "| | | | + afreq=%d", tk->fmt.audio.i_rate );
2782 else if( MKV_IS_ID( l, KaxAudioChannels ) )
2784 KaxAudioChannels &achan = *(KaxAudioChannels*)l;
2786 tk->fmt.audio.i_channels = uint8( achan );
2787 msg_Dbg( &sys.demuxer, "| | | | + achan=%u", uint8( achan ) );
2789 else if( MKV_IS_ID( l, KaxAudioBitDepth ) )
2791 KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)l;
2793 tk->fmt.audio.i_bitspersample = uint8( abits );
2794 msg_Dbg( &sys.demuxer, "| | | | + abits=%u", uint8( abits ) );
2798 msg_Dbg( &sys.demuxer, "| | | | + Unknown (%s)", typeid(*l).name() );
2804 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)",
2805 typeid(*l).name() );
2810 /*****************************************************************************
2812 *****************************************************************************/
2813 void matroska_segment_t::ParseTracks( EbmlElement *tracks )
2818 int i_upper_level = 0;
2820 msg_Dbg( &sys.demuxer, "| + Tracks" );
2822 /* Master elements */
2823 m = static_cast<EbmlMaster *>(tracks);
2824 m->Read( es, tracks->Generic().Context, i_upper_level, el, true );
2826 for( i = 0; i < m->ListSize(); i++ )
2828 EbmlElement *l = (*m)[i];
2830 if( MKV_IS_ID( l, KaxTrackEntry ) )
2832 ParseTrackEntry( static_cast<EbmlMaster *>(l) );
2836 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2841 /*****************************************************************************
2843 *****************************************************************************/
2844 void matroska_segment_t::ParseInfo( EbmlElement *info )
2849 int i_upper_level = 0;
2851 msg_Dbg( &sys.demuxer, "| + Information" );
2853 /* Master elements */
2854 m = static_cast<EbmlMaster *>(info);
2855 m->Read( es, info->Generic().Context, i_upper_level, el, true );
2857 for( i = 0; i < m->ListSize(); i++ )
2859 EbmlElement *l = (*m)[i];
2861 if( MKV_IS_ID( l, KaxSegmentUID ) )
2863 segment_uid = *(new KaxSegmentUID(*static_cast<KaxSegmentUID*>(l)));
2865 msg_Dbg( &sys.demuxer, "| | + UID=%d", *(uint32*)segment_uid.GetBuffer() );
2867 else if( MKV_IS_ID( l, KaxPrevUID ) )
2869 prev_segment_uid = *(new KaxPrevUID(*static_cast<KaxPrevUID*>(l)));
2871 msg_Dbg( &sys.demuxer, "| | + PrevUID=%d", *(uint32*)prev_segment_uid.GetBuffer() );
2873 else if( MKV_IS_ID( l, KaxNextUID ) )
2875 next_segment_uid = *(new KaxNextUID(*static_cast<KaxNextUID*>(l)));
2877 msg_Dbg( &sys.demuxer, "| | + NextUID=%d", *(uint32*)next_segment_uid.GetBuffer() );
2879 else if( MKV_IS_ID( l, KaxTimecodeScale ) )
2881 KaxTimecodeScale &tcs = *(KaxTimecodeScale*)l;
2883 i_timescale = uint64(tcs);
2885 msg_Dbg( &sys.demuxer, "| | + TimecodeScale="I64Fd,
2888 else if( MKV_IS_ID( l, KaxDuration ) )
2890 KaxDuration &dur = *(KaxDuration*)l;
2892 f_duration = float(dur);
2894 msg_Dbg( &sys.demuxer, "| | + Duration=%f",
2897 else if( MKV_IS_ID( l, KaxMuxingApp ) )
2899 KaxMuxingApp &mapp = *(KaxMuxingApp*)l;
2901 psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
2903 msg_Dbg( &sys.demuxer, "| | + Muxing Application=%s",
2904 psz_muxing_application );
2906 else if( MKV_IS_ID( l, KaxWritingApp ) )
2908 KaxWritingApp &wapp = *(KaxWritingApp*)l;
2910 psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
2912 msg_Dbg( &sys.demuxer, "| | + Writing Application=%s",
2913 psz_writing_application );
2915 else if( MKV_IS_ID( l, KaxSegmentFilename ) )
2917 KaxSegmentFilename &sfn = *(KaxSegmentFilename*)l;
2919 psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
2921 msg_Dbg( &sys.demuxer, "| | + Segment Filename=%s",
2922 psz_segment_filename );
2924 else if( MKV_IS_ID( l, KaxTitle ) )
2926 KaxTitle &title = *(KaxTitle*)l;
2928 psz_title = UTF8ToStr( UTFstring( title ) );
2930 msg_Dbg( &sys.demuxer, "| | + Title=%s", psz_title );
2932 else if( MKV_IS_ID( l, KaxSegmentFamily ) )
2934 KaxSegmentFamily *uid = static_cast<KaxSegmentFamily*>(l);
2936 families.push_back(*uid);
2938 msg_Dbg( &sys.demuxer, "| | + family=%d", *(uint32*)uid->GetBuffer() );
2940 #if defined( HAVE_GMTIME_R ) && !defined( SYS_DARWIN )
2941 else if( MKV_IS_ID( l, KaxDateUTC ) )
2943 KaxDateUTC &date = *(KaxDateUTC*)l;
2948 i_date = date.GetEpochDate();
2949 memset( buffer, 0, 256 );
2950 if( gmtime_r( &i_date, &tmres ) &&
2951 asctime_r( &tmres, buffer ) )
2953 buffer[strlen( buffer)-1]= '\0';
2954 psz_date_utc = strdup( buffer );
2955 msg_Dbg( &sys.demuxer, "| | + Date=%s", psz_date_utc );
2961 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
2965 f_duration *= i_timescale / 1000000.0;
2969 /*****************************************************************************
2971 *****************************************************************************/
2972 void matroska_segment_t::ParseChapterAtom( int i_level, EbmlMaster *ca, chapter_item_t & chapters )
2976 if( sys.title == NULL )
2978 sys.title = vlc_input_title_New();
2981 msg_Dbg( &sys.demuxer, "| | | + ChapterAtom (level=%d)", i_level );
2982 for( i = 0; i < ca->ListSize(); i++ )
2984 EbmlElement *l = (*ca)[i];
2986 if( MKV_IS_ID( l, KaxChapterUID ) )
2988 chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
2989 msg_Dbg( &sys.demuxer, "| | | | + ChapterUID: %lld", chapters.i_uid );
2991 else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
2993 KaxChapterFlagHidden &flag =*(KaxChapterFlagHidden*)l;
2994 chapters.b_display_seekpoint = uint8( flag ) == 0;
2996 msg_Dbg( &sys.demuxer, "| | | | + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
2998 else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
3000 KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
3001 chapters.i_start_time = uint64( start ) / I64C(1000);
3003 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeStart: %lld", chapters.i_start_time );
3005 else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
3007 KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
3008 chapters.i_end_time = uint64( end ) / I64C(1000);
3010 msg_Dbg( &sys.demuxer, "| | | | + ChapterTimeEnd: %lld", chapters.i_end_time );
3012 else if( MKV_IS_ID( l, KaxChapterDisplay ) )
3014 EbmlMaster *cd = static_cast<EbmlMaster *>(l);
3017 msg_Dbg( &sys.demuxer, "| | | | + ChapterDisplay" );
3018 for( j = 0; j < cd->ListSize(); j++ )
3020 EbmlElement *l= (*cd)[j];
3022 if( MKV_IS_ID( l, KaxChapterString ) )
3026 KaxChapterString &name =*(KaxChapterString*)l;
3027 for (k = 0; k < i_level; k++)
3028 chapters.psz_name += '+';
3029 chapters.psz_name += ' ';
3030 chapters.psz_name += UTF8ToStr( UTFstring( name ) );
3032 msg_Dbg( &sys.demuxer, "| | | | | + ChapterString '%s'", UTF8ToStr(UTFstring(name)) );
3034 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
3036 KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
3037 const char *psz = string( lang ).c_str();
3039 msg_Dbg( &sys.demuxer, "| | | | | + ChapterLanguage '%s'", psz );
3041 else if( MKV_IS_ID( l, KaxChapterCountry ) )
3043 KaxChapterCountry &ct =*(KaxChapterCountry*)l;
3044 const char *psz = string( ct ).c_str();
3046 msg_Dbg( &sys.demuxer, "| | | | | + ChapterCountry '%s'", psz );
3050 else if( MKV_IS_ID( l, KaxChapterAtom ) )
3052 chapter_item_t new_sub_chapter;
3053 ParseChapterAtom( i_level+1, static_cast<EbmlMaster *>(l), new_sub_chapter );
3054 new_sub_chapter.psz_parent = &chapters;
3055 chapters.sub_chapters.push_back( new_sub_chapter );
3060 /*****************************************************************************
3062 *****************************************************************************/
3063 void matroska_segment_t::ParseChapters( EbmlElement *chapters )
3068 int i_upper_level = 0;
3069 int i_default_edition = 0;
3072 /* Master elements */
3073 m = static_cast<EbmlMaster *>(chapters);
3074 m->Read( es, chapters->Generic().Context, i_upper_level, el, true );
3076 for( i = 0; i < m->ListSize(); i++ )
3078 EbmlElement *l = (*m)[i];
3080 if( MKV_IS_ID( l, KaxEditionEntry ) )
3082 chapter_edition_t edition;
3084 EbmlMaster *E = static_cast<EbmlMaster *>(l );
3086 msg_Dbg( &sys.demuxer, "| | + EditionEntry" );
3087 for( j = 0; j < E->ListSize(); j++ )
3089 EbmlElement *l = (*E)[j];
3091 if( MKV_IS_ID( l, KaxChapterAtom ) )
3093 chapter_item_t new_sub_chapter;
3094 ParseChapterAtom( 0, static_cast<EbmlMaster *>(l), new_sub_chapter );
3095 edition.chapters.push_back( new_sub_chapter );
3097 else if( MKV_IS_ID( l, KaxEditionUID ) )
3099 edition.i_uid = uint64(*static_cast<KaxEditionUID *>( l ));
3101 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
3103 edition.b_ordered = uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0;
3105 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
3107 if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
3108 i_default_edition = editions.size();
3112 msg_Dbg( &sys.demuxer, "| | | + Unknown (%s)", typeid(*l).name() );
3115 editions.push_back( edition );
3119 msg_Dbg( &sys.demuxer, "| | + Unknown (%s)", typeid(*l).name() );
3123 for( i = 0; i < editions.size(); i++ )
3125 editions[i].RefreshChapters( *sys.title );
3128 i_current_edition = i_default_edition;
3130 if ( editions[i_default_edition].b_ordered )
3132 /* update the duration of the segment according to the sum of all sub chapters */
3133 f_dur = editions[i_default_edition].Duration() / I64C(1000);
3139 /*****************************************************************************
3140 * InformationCreate:
3141 *****************************************************************************/
3142 static void InformationCreate( demux_t *p_demux )
3144 demux_sys_t *p_sys = p_demux->p_sys;
3145 matroska_stream_t *p_stream = p_sys->Stream();
3146 matroska_segment_t *p_segment = p_stream->Segment();
3149 p_sys->meta = vlc_meta_New();
3151 if( p_segment->psz_title )
3153 vlc_meta_Add( p_sys->meta, VLC_META_TITLE, p_segment->psz_title );
3155 if( p_segment->psz_date_utc )
3157 vlc_meta_Add( p_sys->meta, VLC_META_DATE, p_segment->psz_date_utc );
3159 if( p_segment->psz_segment_filename )
3161 vlc_meta_Add( p_sys->meta, _("Segment filename"), p_segment->psz_segment_filename );
3163 if( p_segment->psz_muxing_application )
3165 vlc_meta_Add( p_sys->meta, _("Muxing application"), p_segment->psz_muxing_application );
3167 if( p_segment->psz_writing_application )
3169 vlc_meta_Add( p_sys->meta, _("Writing application"), p_segment->psz_writing_application );
3172 for( i_track = 0; i_track < p_segment->tracks.size(); i_track++ )
3174 mkv_track_t *tk = p_segment->tracks[i_track];
3175 vlc_meta_t *mtk = vlc_meta_New();
3177 p_sys->meta->track = (vlc_meta_t**)realloc( p_sys->meta->track,
3178 sizeof( vlc_meta_t * ) * ( p_sys->meta->i_track + 1 ) );
3179 p_sys->meta->track[p_sys->meta->i_track++] = mtk;
3181 if( tk->fmt.psz_description )
3183 vlc_meta_Add( p_sys->meta, VLC_META_DESCRIPTION, tk->fmt.psz_description );
3185 if( tk->psz_codec_name )
3187 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_NAME, tk->psz_codec_name );
3189 if( tk->psz_codec_settings )
3191 vlc_meta_Add( p_sys->meta, VLC_META_SETTING, tk->psz_codec_settings );
3193 if( tk->psz_codec_info_url )
3195 vlc_meta_Add( p_sys->meta, VLC_META_CODEC_DESCRIPTION, tk->psz_codec_info_url );
3197 if( tk->psz_codec_download_url )
3199 vlc_meta_Add( p_sys->meta, VLC_META_URL, tk->psz_codec_download_url );
3203 if( p_segment->i_tags_position >= 0 )
3205 vlc_bool_t b_seekable;
3207 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &b_seekable );
3210 LoadTags( p_demux );
3216 /*****************************************************************************
3218 *****************************************************************************/
3220 void matroska_segment_t::IndexAppendCluster( KaxCluster *cluster )
3222 #define idx index[i_index]
3224 idx.i_block_number= -1;
3225 idx.i_position = cluster->GetElementPosition();
3227 idx.b_key = VLC_TRUE;
3230 if( i_index >= i_index_max )
3232 i_index_max += 1024;
3233 index = (mkv_index_t*)realloc( index, sizeof( mkv_index_t ) * i_index_max );
3238 static char * UTF8ToStr( const UTFstring &u )
3247 p = dst = (char*)malloc( i_src + 1);
3266 void chapter_edition_t::RefreshChapters( input_title_t & title )
3268 int64_t i_prev_user_time = 0;
3269 std::vector<chapter_item_t>::iterator index = chapters.begin();
3271 while ( index != chapters.end() )
3273 i_prev_user_time = (*index).RefreshChapters( b_ordered, i_prev_user_time, title );
3278 int64_t chapter_item_t::RefreshChapters( bool b_ordered, int64_t i_prev_user_time, input_title_t & title )
3280 int64_t i_user_time = i_prev_user_time;
3282 // first the sub-chapters, and then ourself
3283 std::vector<chapter_item_t>::iterator index = sub_chapters.begin();
3284 while ( index != sub_chapters.end() )
3286 i_user_time = (*index).RefreshChapters( b_ordered, i_user_time, title );
3292 i_user_start_time = i_prev_user_time;
3293 if ( i_end_time != -1 && i_user_time == i_prev_user_time )
3295 i_user_end_time = i_user_start_time - i_start_time + i_end_time;
3299 i_user_end_time = i_user_time;
3304 std::sort( sub_chapters.begin(), sub_chapters.end() );
3305 i_user_start_time = i_start_time;
3306 i_user_end_time = i_end_time;
3309 if (b_display_seekpoint)
3311 seekpoint_t *sk = vlc_seekpoint_New();
3313 // sk->i_level = i_level;
3314 sk->i_time_offset = i_start_time;
3315 sk->psz_name = strdup( psz_name.c_str() );
3317 // A start time of '0' is ok. A missing ChapterTime element is ok, too, because '0' is its default value.
3318 title.i_seekpoint++;
3319 title.seekpoint = (seekpoint_t**)realloc( title.seekpoint, title.i_seekpoint * sizeof( seekpoint_t* ) );
3320 title.seekpoint[title.i_seekpoint-1] = sk;
3323 i_seekpoint_num = title.i_seekpoint;
3325 return i_user_end_time;
3328 double chapter_edition_t::Duration() const
3330 double f_result = 0.0;
3332 if ( chapters.size() )
3334 std::vector<chapter_item_t>::const_iterator index = chapters.end();
3336 f_result = (*index).i_user_end_time;
3342 const chapter_item_t *chapter_item_t::FindTimecode( mtime_t i_user_timecode ) const
3344 const chapter_item_t *psz_result = NULL;
3346 if (i_user_timecode >= i_user_start_time && i_user_timecode < i_user_end_time)
3348 std::vector<chapter_item_t>::const_iterator index = sub_chapters.begin();
3349 while ( index != sub_chapters.end() && psz_result == NULL )
3351 psz_result = (*index).FindTimecode( i_user_timecode );
3355 if ( psz_result == NULL )
3362 const chapter_item_t *chapter_edition_t::FindTimecode( mtime_t i_user_timecode ) const
3364 const chapter_item_t *psz_result = NULL;
3366 std::vector<chapter_item_t>::const_iterator index = chapters.begin();
3367 while ( index != chapters.end() && psz_result == NULL )
3369 psz_result = (*index).FindTimecode( i_user_timecode );
3376 void demux_sys_t::PreloadFamily( )
3378 matroska_stream_t *p_stream = Stream();
3381 matroska_segment_t *p_segment = p_stream->Segment();
3384 for (size_t i=0; i<streams.size(); i++)
3386 streams[i]->PreloadFamily( *p_segment );
3392 void matroska_stream_t::PreloadFamily( const matroska_segment_t & of_segment )
3394 for (size_t i=0; i<segments.size(); i++)
3396 segments[i]->PreloadFamily( of_segment );
3400 bool matroska_segment_t::PreloadFamily( const matroska_segment_t & of_segment )
3405 for (size_t i=0; i<families.size(); i++)
3407 for (size_t j=0; j<of_segment.families.size(); j++)
3409 if ( families[i] == of_segment.families[j] )
3417 // preload all the linked segments for all preloaded segments
3418 void demux_sys_t::PreloadLinked( )
3420 size_t i_prealoaded;
3423 for (size_t i=0; i<streams.size(); i++)
3425 i_prealoaded += streams[i]->PreloadLinked( *this );
3427 } while ( i_prealoaded ); // worst case: will stop when all segments are preloaded
3430 size_t matroska_stream_t::PreloadLinked( const demux_sys_t & of_sys )
3432 size_t i_result = 0;
3433 for (size_t i=0; i<segments.size(); i++)
3435 i_result += segments[i]->PreloadLinked( of_sys );
3440 size_t matroska_segment_t::PreloadLinked( const demux_sys_t & of_sys )
3442 size_t i_result = 0;
3443 if ( prev_segment_uid.GetBuffer() )
3445 matroska_segment_t *p_segment = of_sys.FindSegment( prev_segment_uid );
3448 i_result += p_segment->Preload( ) ? 1 : 0;
3451 if ( next_segment_uid.GetBuffer() )
3453 matroska_segment_t *p_segment = of_sys.FindSegment( next_segment_uid );
3456 i_result += p_segment->Preload( ) ? 1 : 0;
3462 bool matroska_segment_t::Preload( )
3467 EbmlElement *el = NULL;
3471 while( ( el = ep->Get() ) != NULL )
3473 if( MKV_IS_ID( el, KaxInfo ) )
3477 else if( MKV_IS_ID( el, KaxTracks ) )
3481 else if( MKV_IS_ID( el, KaxSeekHead ) )
3483 ParseSeekHead( el );
3485 else if( MKV_IS_ID( el, KaxCues ) )
3487 msg_Dbg( &sys.demuxer, "| + Cues" );
3489 else if( MKV_IS_ID( el, KaxCluster ) )
3491 msg_Dbg( &sys.demuxer, "| + Cluster" );
3493 cluster = (KaxCluster*)el;
3495 i_start_pos = cluster->GetElementPosition();
3498 /* stop parsing the stream */
3501 else if( MKV_IS_ID( el, KaxAttachments ) )
3503 msg_Dbg( &sys.demuxer, "| + Attachments FIXME TODO (but probably never supported)" );
3505 else if( MKV_IS_ID( el, KaxChapters ) )
3507 msg_Dbg( &sys.demuxer, "| + Chapters" );
3508 ParseChapters( el );
3510 else if( MKV_IS_ID( el, KaxTag ) )
3512 msg_Dbg( &sys.demuxer, "| + Tags FIXME TODO" );
3516 msg_Dbg( &sys.demuxer, "| + Unknown (%s)", typeid(*el).name() );
3525 matroska_segment_t *demux_sys_t::FindSegment( const EbmlBinary & uid ) const
3527 matroska_segment_t *p_segment = NULL;
3528 for (size_t i=0; i<streams.size() && p_segment == NULL; i++)
3530 p_segment = streams[i]->FindSegment( uid );
3535 matroska_segment_t *matroska_stream_t::FindSegment( const EbmlBinary & uid ) const
3537 for (size_t i=0; i<segments.size(); i++)
3539 if ( segments[i]->segment_uid == uid )