]> git.sesse.net Git - vlc/blobdiff - modules/demux/mkv.cpp
* Stringreview !!!
[vlc] / modules / demux / mkv.cpp
index 6a210f60f4095e3117a24ccf176f22f79f3d1eab..428829dfec28202fceb1cc64d1953eda05e665a6 100644 (file)
@@ -1,8 +1,8 @@
 /*****************************************************************************
  * mkv.cpp : matroska demuxer
  *****************************************************************************
- * Copyright (C) 2001 VideoLAN
- * $Id: mkv.cpp,v 1.5 2003/06/22 23:22:11 fenrir Exp $
+ * Copyright (C) 2003-2004 VideoLAN
+ * $Id: mkv.cpp,v 1.55 2004/01/25 20:05:28 hartman Exp $
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *
 
 #include <vlc/vlc.h>
 
+#ifdef HAVE_TIME_H
+#   include <time.h>                                               /* time() */
+#endif
+
 #include <vlc/input.h>
 
 #include <codecs.h>                        /* BITMAPINFOHEADER, WAVEFORMATEX */
+#include "iso_lang.h"
 
 #include <iostream>
 #include <cassert>
 #include <typeinfo>
 
+#ifdef HAVE_WCHAR_H
+#   include <wchar.h>
+#endif
+
 /* libebml and matroska */
 #include "ebml/EbmlHead.h"
 #include "ebml/EbmlSubHead.h"
 #include "ebml/EbmlVoid.h"
 
 #include "matroska/FileKax.h"
+#ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
+#include "matroska/KaxAttachments.h"
+#else
 #include "matroska/KaxAttachements.h"
+#endif
 #include "matroska/KaxBlock.h"
 #include "matroska/KaxBlockData.h"
 #include "matroska/KaxChapters.h"
 #include "matroska/KaxSeekHead.h"
 #include "matroska/KaxSegment.h"
 #include "matroska/KaxTag.h"
+#include "matroska/KaxTags.h"
+#include "matroska/KaxTagMulti.h"
 #include "matroska/KaxTracks.h"
 #include "matroska/KaxTrackAudio.h"
 #include "matroska/KaxTrackVideo.h"
+#include "matroska/KaxTrackEntryData.h"
 
 #include "ebml/StdIOCallback.h"
 
 using namespace LIBMATROSKA_NAMESPACE;
 using namespace std;
 
-
-/*****************************************************************************
- * Local prototypes
- *****************************************************************************/
-static int  Activate  ( vlc_object_t * );
-static void Deactivate( vlc_object_t * );
-static int  Demux     ( input_thread_t * );
-
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
+static int  Open ( vlc_object_t * );
+static void Close( vlc_object_t * );
+
 vlc_module_begin();
-    add_category_hint( N_("mkv-demuxer"), NULL, VLC_TRUE );
-        add_bool( "mkv-index", 0, NULL,
-                  N_("Create index if no cues found"),
-                  N_("Create index if no cues found"), VLC_TRUE );
-    set_description( _("mka/mkv stream demuxer" ) );
+    set_description( _("Matroska stream demuxer" ) );
     set_capability( "demux", 50 );
-    set_callbacks( Activate, Deactivate );
+    set_callbacks( Open, Close );
+    
+    add_bool( "mkv-seek-percent", 1, NULL,
+            N_("Seek based on percent not time"),
+            N_("Seek based on percent not time"), VLC_TRUE );
+
     add_shortcut( "mka" );
     add_shortcut( "mkv" );
 vlc_module_end();
 
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int  Demux   ( input_thread_t * );
+static void Seek    ( input_thread_t *, mtime_t i_date, int i_percent );
+
 
 /*****************************************************************************
  * Stream managment
@@ -98,10 +116,11 @@ vlc_module_end();
 class vlc_stream_io_callback: public IOCallback
 {
   private:
-    input_thread_t *p_input;
+    stream_t       *s;
+    vlc_bool_t     mb_eof;
 
   public:
-    vlc_stream_io_callback( input_thread_t * );
+    vlc_stream_io_callback( stream_t * );
 
     virtual uint32_t read            ( void *p_buffer, size_t i_size);
     virtual void     setFilePointer  ( int64_t i_offset, seek_mode mode = seek_beginning );
@@ -110,112 +129,23 @@ class vlc_stream_io_callback: public IOCallback
     virtual void     close           ( void );
 };
 
-
-vlc_stream_io_callback::vlc_stream_io_callback( input_thread_t *p_input_ )
-{
-    p_input = p_input_;
-}
-
-uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
-{
-    data_packet_t *p_data;
-
-    int i_count;
-    int i_read = 0;
-
-
-    if( !i_size )
-    {
-        return 0;
-    }
-
-    do
-    {
-        i_count = input_SplitBuffer(p_input, &p_data, __MIN( i_size, 10240 ) );
-        if( i_count <= 0 )
-        {
-            return i_read;
-        }
-        memcpy( p_buffer, p_data->p_payload_start, i_count );
-        input_DeletePacket( p_input->p_method_data, p_data );
-
-        (uint8_t*)p_buffer += i_count;
-        i_size            -= i_count;
-        i_read            += i_count;
-
-    } while( i_size );
-
-    return i_read;
-}
-
-
-void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
-{
-    int64_t i_pos;
-
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    switch( mode )
-    {
-        case seek_beginning:
-            i_pos = i_offset;
-            break;
-        case seek_end:
-            i_pos = p_input->stream.p_selected_area->i_size - i_offset;
-            break;
-        default:
-            i_pos= p_input->stream.p_selected_area->i_tell + i_offset;
-            break;
-    }
-    if( i_pos < 0 || i_pos > p_input->stream.p_selected_area->i_size )
-    {
-        msg_Err( p_input, "seeking to wrong place (i_pos=%lld)", i_pos );
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        return;
-    }
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    input_AccessReinit( p_input );
-    p_input->pf_seek( p_input, i_pos );
-}
-
-size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
-{
-    return 0;
-}
-
-uint64_t vlc_stream_io_callback::getFilePointer( void )
-{
-    uint64_t i_pos;
-
-    vlc_mutex_lock( &p_input->stream.stream_lock );
-    i_pos= p_input->stream.p_selected_area->i_tell;
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-    return i_pos;
-}
-
-void vlc_stream_io_callback::close( void )
-{
-    return;
-}
-
 /*****************************************************************************
  * Ebml Stream parser
  *****************************************************************************/
 class EbmlParser
 {
-    public:
-        EbmlParser( EbmlStream *es, EbmlElement *el_start );
-        ~EbmlParser( void );
-        void Up( void );
-        void Down( void );
-        EbmlElement *Get( void );
-        void        Keep( void );
+  public:
+    EbmlParser( EbmlStream *es, EbmlElement *el_start );
+    ~EbmlParser( void );
 
-        int GetLevel( void );
+    void Up( void );
+    void Down( void );
+    EbmlElement *Get( void );
+    void        Keep( void );
 
-    private:
+    int GetLevel( void );
 
+  private:
     EbmlStream  *m_es;
     int         mi_level;
     EbmlElement *m_el[6];
@@ -224,175 +154,39 @@ class EbmlParser
 
     int         mi_user_level;
     vlc_bool_t  mb_keep;
-
 };
 
-EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
-{
-    int i;
-
-    m_es = es;
-    m_got = NULL;
-    m_el[0] = el_start;
-
-    for( i = 1; i < 6; i++ )
-    {
-        m_el[i] = NULL;
-    }
-    mi_level = 1;
-    mi_user_level = 1;
-    mb_keep = VLC_FALSE;
-}
-
-EbmlParser::~EbmlParser( void )
-{
-    int i;
-
-    for( i = 1; i < mi_level; i++ )
-    {
-        if( !mb_keep )
-        {
-            delete m_el[i];
-        }
-        mb_keep = VLC_FALSE;
-    }
-}
-
-void EbmlParser::Up( void )
-{
-    if( mi_user_level == mi_level )
-    {
-        fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
-    }
-
-    mi_user_level--;
-}
-
-void EbmlParser::Down( void )
-{
-    mi_user_level++;
-    mi_level++;
-}
-
-void EbmlParser::Keep( void )
-{
-    mb_keep = VLC_TRUE;
-}
-
-int EbmlParser::GetLevel( void )
-{
-    return mi_user_level;
-}
-
-EbmlElement *EbmlParser::Get( void )
-{
-    int i_ulev = 0;
-
-    if( mi_user_level != mi_level )
-    {
-        return NULL;
-    }
-    if( m_got )
-    {
-        EbmlElement *ret = m_got;
-        m_got = NULL;
-
-        return ret;
-    }
-
-    if( m_el[mi_level] )
-    {
-        m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
-        if( !mb_keep )
-        {
-            delete m_el[mi_level];
-        }
-        mb_keep = VLC_FALSE;
-    }
-
-    m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
-    if( i_ulev > 0 )
-    {
-        while( i_ulev > 0 )
-        {
-            if( mi_level == 1 )
-            {
-                mi_level = 0;
-                return NULL;
-            }
-
-            delete m_el[mi_level - 1];
-            m_got = m_el[mi_level -1] = m_el[mi_level];
-            m_el[mi_level] = NULL;
-
-            mi_level--;
-            i_ulev--;
-        }
-        return NULL;
-    }
-    else if( m_el[mi_level] == NULL )
-    {
-        fprintf( stderr," m_el[mi_level] == NULL\n" );
-    }
-
-    return m_el[mi_level];
-}
 
 /*****************************************************************************
  * Some functions to manipulate memory
  *****************************************************************************/
-#define GetWLE( p )     __GetWLE( (uint8_t*)p )
-#define GetDWLE( p )    __GetDWLE( (uint8_t*)p )
 #define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
-static uint16_t __GetWLE( uint8_t *p )
-{
-    return (uint16_t)p[0] | ( ((uint16_t)p[1]) << 8 );
-}
-static uint32_t __GetDWLE( uint8_t *p )
-{
-    return (uint32_t)p[0] | ( ((uint32_t)p[1]) << 8 ) |
-            ( ((uint32_t)p[2]) << 16 ) | ( ((uint32_t)p[3]) << 24 );
-}
 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
 {
     return VLC_FOURCC( p[0], p[1], p[2], p[3] );
 }
 
-
 /*****************************************************************************
  * definitions of structures and functions used by this plugins
  *****************************************************************************/
 typedef struct
 {
-    int         i_cat;
     vlc_bool_t  b_default;
+    vlc_bool_t  b_enabled;
     int         i_number;
 
     int         i_extra_data;
     uint8_t     *p_extra_data;
 
-    char         *psz_language;
-
     char         *psz_codec;
-    vlc_fourcc_t i_codec;
 
     uint64_t     i_default_duration;
+    float        f_timecodescale;
+
     /* video */
-    int         i_width;
-    int         i_height;
-    int         i_display_width;
-    int         i_display_height;
+    es_format_t fmt;
     float       f_fps;
-
-
-    /* audio */
-    int         i_channels;
-    int         i_samplerate;
-    int         i_bitspersample;
-
-    es_descriptor_t *p_es;
-
-
+    es_out_id_t *p_es;
 
     vlc_bool_t      b_inited;
     /* data to be send first */
@@ -401,6 +195,13 @@ typedef struct
 
     /* hack : it's for seek */
     vlc_bool_t      b_search_keyframe;
+
+    /* informative */
+    char         *psz_codec_name;
+    char         *psz_codec_settings;
+    char         *psz_codec_info_url;
+    char         *psz_codec_download_url;
+
 } mkv_track_t;
 
 typedef struct
@@ -433,6 +234,7 @@ struct demux_sys_t
     /* from seekhead */
     int64_t                 i_cues_position;
     int64_t                 i_chapters_position;
+    int64_t                 i_tags_position;
 
     /* current data */
     KaxSegment              *segment;
@@ -440,37 +242,44 @@ struct demux_sys_t
 
     mtime_t                 i_pts;
 
+    vlc_bool_t              b_cues;
     int                     i_index;
     int                     i_index_max;
     mkv_index_t             *index;
+
+    /* info */
+    char                    *psz_muxing_application;
+    char                    *psz_writing_application;
+    char                    *psz_segment_filename;
+    char                    *psz_title;
+    char                    *psz_date_utc;
 };
 
 #define MKVD_TIMECODESCALE 1000000
 
+static void IndexAppendCluster  ( input_thread_t *p_input, KaxCluster *cluster );
+static char *UTF8ToStr          ( const UTFstring &u );
+static void LoadCues            ( input_thread_t *);
+static void InformationsCreate  ( input_thread_t *p_input );
+
+static char *LanguageGetName    ( const char *psz_code );
+
 /*****************************************************************************
- * Activate: initializes matroska demux structures
+ * Open: initializes matroska demux structures
  *****************************************************************************/
-static int Activate( vlc_object_t * p_this )
+static int Open( vlc_object_t * p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
     demux_sys_t    *p_sys;
     uint8_t        *p_peek;
 
     int             i_track;
-    vlc_bool_t      b_audio_selected;
-    int             i_spu_channel, i_audio_channel;
 
     EbmlElement     *el = NULL, *el1 = NULL, *el2 = NULL, *el3 = NULL, *el4 = NULL;
 
-    /* Initialize access plug-in structures. */
-    if( p_input->i_mtu == 0 )
-    {
-        /* Improve speed. */
-        p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE;
-    }
-
     /* Set the demux function */
     p_input->pf_demux = Demux;
+    p_input->pf_demux_control = demux_vaControlDefault;
 
     /* peek the begining */
     if( input_Peek( p_input, &p_peek, 4 ) < 4 )
@@ -480,22 +289,19 @@ static int Activate( vlc_object_t * p_this )
     }
 
     /* is a valid file */
-    if( p_peek[0] != 0x1a || p_peek[1] != 0x45 || p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
-    {
-        msg_Warn( p_input, "matroska module discarded (invalid header)" );
-        return VLC_EGENERIC;
-    }
-
-    if( p_input->stream.i_method != INPUT_METHOD_FILE || !p_input->stream.b_seekable )
+    if( p_peek[0] != 0x1a || p_peek[1] != 0x45 ||
+        p_peek[2] != 0xdf || p_peek[3] != 0xa3 )
     {
-        msg_Err( p_input, "can only read matroska over seekable file yet" );
+        msg_Warn( p_input, "matroska module discarded "
+                           "(invalid header 0x%.2x%.2x%.2x%.2x)",
+                           p_peek[0], p_peek[1], p_peek[2], p_peek[3] );
         return VLC_EGENERIC;
     }
 
-    p_input->p_demux_data = p_sys = (demux_sys_t*)malloc( sizeof( demux_sys_t ) );
+    p_input->p_demux_data = p_sys = (demux_sys_t*)malloc(sizeof( demux_sys_t ));
     memset( p_sys, 0, sizeof( demux_sys_t ) );
 
-    p_sys->in = new vlc_stream_io_callback( p_input );
+    p_sys->in = new vlc_stream_io_callback( p_input->s );
     p_sys->es = new EbmlStream( *p_sys->in );
     p_sys->f_duration   = -1;
     p_sys->i_timescale     = MKVD_TIMECODESCALE;
@@ -504,10 +310,19 @@ static int Activate( vlc_object_t * p_this )
     p_sys->i_pts   = 0;
     p_sys->i_cues_position = -1;
     p_sys->i_chapters_position = -1;
+    p_sys->i_tags_position = -1;
 
+    p_sys->b_cues       = VLC_FALSE;
     p_sys->i_index      = 0;
     p_sys->i_index_max  = 1024;
-    p_sys->index        = (mkv_index_t*)malloc( sizeof( mkv_index_t ) * p_sys->i_index_max );
+    p_sys->index        = (mkv_index_t*)malloc( sizeof( mkv_index_t ) *
+                                                p_sys->i_index_max );
+
+    p_sys->psz_muxing_application = NULL;
+    p_sys->psz_writing_application = NULL;
+    p_sys->psz_segment_filename = NULL;
+    p_sys->psz_title = NULL;
+    p_sys->psz_date_utc = NULL;;
 
     if( p_sys->es == NULL )
     {
@@ -557,7 +372,8 @@ static int Activate( vlc_object_t * p_this )
                     tcs.ReadData( p_sys->es->I_O() );
                     p_sys->i_timescale = uint64(tcs);
 
-                    msg_Dbg( p_input, "|   |   + TimecodeScale=%lld", p_sys->i_timescale );
+                    msg_Dbg( p_input, "|   |   + TimecodeScale="I64Fd,
+                             p_sys->i_timescale );
                 }
                 else if( EbmlId( *el2 ) == KaxDuration::ClassInfos.GlobalId )
                 {
@@ -566,11 +382,76 @@ static int Activate( vlc_object_t * p_this )
                     dur.ReadData( p_sys->es->I_O() );
                     p_sys->f_duration = float(dur);
 
-                    msg_Dbg( p_input, "|   |   + Duration=%f", p_sys->f_duration );
+                    msg_Dbg( p_input, "|   |   + Duration=%f",
+                             p_sys->f_duration );
+                }
+                else if( EbmlId( *el2 ) == KaxMuxingApp::ClassInfos.GlobalId )
+                {
+                    KaxMuxingApp &mapp = *(KaxMuxingApp*)el2;
+
+                    mapp.ReadData( p_sys->es->I_O() );
+
+                    p_sys->psz_muxing_application = UTF8ToStr( UTFstring( mapp ) );
+
+                    msg_Dbg( p_input, "|   |   + Muxing Application=%s",
+                             p_sys->psz_muxing_application );
+                }
+                else if( EbmlId( *el2 ) == KaxWritingApp::ClassInfos.GlobalId )
+                {
+                    KaxWritingApp &wapp = *(KaxWritingApp*)el2;
+
+                    wapp.ReadData( p_sys->es->I_O() );
+
+                    p_sys->psz_writing_application = UTF8ToStr( UTFstring( wapp ) );
+
+                    msg_Dbg( p_input, "|   |   + Wrinting Application=%s",
+                             p_sys->psz_writing_application );
+                }
+                else if( EbmlId( *el2 ) == KaxSegmentFilename::ClassInfos.GlobalId )
+                {
+                    KaxSegmentFilename &sfn = *(KaxSegmentFilename*)el2;
+
+                    sfn.ReadData( p_sys->es->I_O() );
+
+                    p_sys->psz_segment_filename = UTF8ToStr( UTFstring( sfn ) );
+
+                    msg_Dbg( p_input, "|   |   + Segment Filename=%s",
+                             p_sys->psz_segment_filename );
+                }
+                else if( EbmlId( *el2 ) == KaxTitle::ClassInfos.GlobalId )
+                {
+                    KaxTitle &title = *(KaxTitle*)el2;
+
+                    title.ReadData( p_sys->es->I_O() );
+
+                    p_sys->psz_title = UTF8ToStr( UTFstring( title ) );
+
+                    msg_Dbg( p_input, "|   |   + Title=%s", p_sys->psz_title );
+                }
+#ifdef HAVE_GMTIME_R
+                else if( EbmlId( *el2 ) == KaxDateUTC::ClassInfos.GlobalId )
+                {
+                    KaxDateUTC &date = *(KaxDateUTC*)el2;
+                    time_t i_date;
+                    struct tm tmres;
+                    char   buffer[256];
+
+                    date.ReadData( p_sys->es->I_O() );
+
+                    i_date = date.GetEpochDate();
+                    memset( buffer, 0, 256 );
+                    if( gmtime_r( &i_date, &tmres ) &&
+                        asctime_r( &tmres, buffer ) )
+                    {
+                        buffer[strlen( buffer)-1]= '\0';
+                        p_sys->psz_date_utc = strdup( buffer );
+                        msg_Dbg( p_input, "|   |   + Date=%s", p_sys->psz_date_utc );
+                    }
                 }
+#endif
                 else
                 {
-                    msg_Dbg( p_input, "|   |   + Unknow (%s)", typeid(*el2).name() );
+                    msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid(*el2).name() );
                 }
             }
             p_sys->ep->Up();
@@ -590,20 +471,29 @@ static int Activate( vlc_object_t * p_this )
                     p_sys->track = (mkv_track_t*)realloc( p_sys->track, sizeof( mkv_track_t ) * (p_sys->i_track + 1 ) );
 #define tk  p_sys->track[p_sys->i_track - 1]
                     memset( &tk, 0, sizeof( mkv_track_t ) );
-                    tk.i_cat = UNKNOWN_ES;
-                    tk.b_default = VLC_FALSE;
+
+                    es_format_Init( &tk.fmt, UNKNOWN_ES, 0 );
+                    tk.fmt.psz_language = strdup("English");
+                    tk.fmt.psz_description = NULL;
+
+                    tk.b_default = VLC_TRUE;
+                    tk.b_enabled = VLC_TRUE;
                     tk.i_number = p_sys->i_track - 1;
                     tk.i_extra_data = 0;
                     tk.p_extra_data = NULL;
-                    tk.i_codec = 0;
                     tk.psz_codec = NULL;
-                    tk.psz_language = NULL;
                     tk.i_default_duration = 0;
+                    tk.f_timecodescale = 1.0;
 
                     tk.b_inited = VLC_FALSE;
                     tk.i_data_init = 0;
                     tk.p_data_init = NULL;
 
+                    tk.psz_codec_name = NULL;
+                    tk.psz_codec_settings = NULL;
+                    tk.psz_codec_info_url = NULL;
+                    tk.psz_codec_download_url = NULL;
+
                     p_sys->ep->Down();
 
                     while( ( el3 = p_sys->ep->Get() ) != NULL )
@@ -614,22 +504,16 @@ static int Activate( vlc_object_t * p_this )
                             tnum.ReadData( p_sys->es->I_O() );
 
                             tk.i_number = uint32( tnum );
-                            msg_Dbg( p_input, "|   |   |   + Track Number=%u", uint32( tnum ) );
+                            msg_Dbg( p_input, "|   |   |   + Track Number=%u",
+                                     uint32( tnum ) );
                         }
                         else  if( EbmlId( *el3 ) == KaxTrackUID::ClassInfos.GlobalId )
                         {
                             KaxTrackUID &tuid = *(KaxTrackUID*)el3;
                             tuid.ReadData( p_sys->es->I_O() );
 
-                            msg_Dbg( p_input, "|   |   |   + Track UID=%u", uint32( tuid ) );
-                        }
-                        else  if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
-                        {
-                            KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
-                            defd.ReadData( p_sys->es->I_O() );
-
-                            tk.i_default_duration = uint64(defd);
-                            msg_Dbg( p_input, "|   |   |   + Track Default Duration=%lld", uint64(defd) );
+                            msg_Dbg( p_input, "|   |   |   + Track UID=%u",
+                                     uint32( tuid ) );
                         }
                         else  if( EbmlId( *el3 ) == KaxTrackType::ClassInfos.GlobalId )
                         {
@@ -640,85 +524,201 @@ static int Activate( vlc_object_t * p_this )
                             {
                                 case track_audio:
                                     psz_type = "audio";
-                                    tk.i_cat = AUDIO_ES;
+                                    tk.fmt.i_cat = AUDIO_ES;
                                     break;
                                 case track_video:
                                     psz_type = "video";
-                                    tk.i_cat = VIDEO_ES;
+                                    tk.fmt.i_cat = VIDEO_ES;
                                     break;
                                 case track_subtitle:
                                     psz_type = "subtitle";
-                                    tk.i_cat = SPU_ES;
+                                    tk.fmt.i_cat = SPU_ES;
                                     break;
                                 default:
                                     psz_type = "unknown";
-                                    tk.i_cat = UNKNOWN_ES;
+                                    tk.fmt.i_cat = UNKNOWN_ES;
                                     break;
                             }
 
-                            msg_Dbg( p_input, "|   |   |   + Track Type=%s", psz_type );
+                            msg_Dbg( p_input, "|   |   |   + Track Type=%s",
+                                     psz_type );
                         }
-                        else  if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
+//                         else  if( EbmlId( *el3 ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
+//                         {
+//                             KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)el3;
+//                             fenb.ReadData( p_sys->es->I_O() );
+
+//                             tk.b_enabled = uint32( fenb );
+//                             msg_Dbg( p_input, "|   |   |   + Track Enabled=%u",
+//                                      uint32( fenb )  );
+//                         }
+                        else  if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
                         {
-                            msg_Dbg( p_input, "|   |   |   + Track Audio" );
-                            tk.i_channels = 0;
-                            tk.i_samplerate = 0;
-                            tk.i_bitspersample = 0;
+                            KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
+                            fdef.ReadData( p_sys->es->I_O() );
 
-                            p_sys->ep->Down();
+                            tk.b_default = uint32( fdef );
+                            msg_Dbg( p_input, "|   |   |   + Track Default=%u",
+                                     uint32( fdef )  );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
+                        {
+                            KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
+                            lac.ReadData( p_sys->es->I_O() );
 
-                            while( ( el4 = p_sys->ep->Get() ) != NULL )
-                            {
-                                if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
-                                {
-                                    KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
-                                    afreq.ReadData( p_sys->es->I_O() );
+                            msg_Dbg( p_input, "|   |   |   + Track Lacing=%d",
+                                     uint32( lac ) );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
+                        {
+                            KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
+                            cmin.ReadData( p_sys->es->I_O() );
 
-                                    tk.i_samplerate = (int)float( afreq );
-                                    msg_Dbg( p_input, "|   |   |   |   + afreq=%d", tk.i_samplerate );
-                                }
-                                else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
-                                {
-                                    KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
-                                    achan.ReadData( p_sys->es->I_O() );
+                            msg_Dbg( p_input, "|   |   |   + Track MinCache=%d",
+                                     uint32( cmin ) );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
+                        {
+                            KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
+                            cmax.ReadData( p_sys->es->I_O() );
 
-                                    tk.i_channels = uint8( achan );
-                                    msg_Dbg( p_input, "|   |   |   |   + achan=%u", uint8( achan ) );
-                                }
-                                else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
-                                {
-                                    KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
-                                    abits.ReadData( p_sys->es->I_O() );
+                            msg_Dbg( p_input, "|   |   |   + Track MaxCache=%d",
+                                     uint32( cmax ) );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxTrackDefaultDuration::ClassInfos.GlobalId )
+                        {
+                            KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)el3;
+                            defd.ReadData( p_sys->es->I_O() );
 
-                                    tk.i_bitspersample = uint8( abits );
-                                    msg_Dbg( p_input, "|   |   |   |   + abits=%u", uint8( abits ) );
-                                }
-                                else
-                                {
-                                    msg_Dbg( p_input, "|   |   |   |   + Unknow (%s)", typeid(*el4).name() );
-                                }
+                            tk.i_default_duration = uint64(defd);
+                            msg_Dbg( p_input, "|   |   |   + Track Default Duration="I64Fd, uint64(defd) );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxTrackTimecodeScale::ClassInfos.GlobalId )
+                        {
+                            KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)el3;
+                            ttcs.ReadData( p_sys->es->I_O() );
+
+                            tk.f_timecodescale = float( ttcs );
+                            msg_Dbg( p_input, "|   |   |   + Track TimeCodeScale=%f", tk.f_timecodescale );
+                        }
+                        else if( EbmlId( *el3 ) == KaxTrackName::ClassInfos.GlobalId )
+                        {
+                            KaxTrackName &tname = *(KaxTrackName*)el3;
+                            tname.ReadData( p_sys->es->I_O() );
+
+                            tk.fmt.psz_description = UTF8ToStr( UTFstring( tname ) );
+                            msg_Dbg( p_input, "|   |   |   + Track Name=%s",
+                                     tk.fmt.psz_description );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
+                        {
+                            KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
+                            lang.ReadData( p_sys->es->I_O() );
+
+                            tk.fmt.psz_language =
+                                LanguageGetName( string( lang ).c_str() );
+                            msg_Dbg( p_input,
+                                     "|   |   |   + Track Language=`%s'(%s) ",
+                                     tk.fmt.psz_language, string( lang ).c_str() );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
+                        {
+                            KaxCodecID &codecid = *(KaxCodecID*)el3;
+                            codecid.ReadData( p_sys->es->I_O() );
+
+                            tk.psz_codec = strdup( string( codecid ).c_str() );
+                            msg_Dbg( p_input, "|   |   |   + Track CodecId=%s",
+                                     string( codecid ).c_str() );
+                        }
+                        else  if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
+                        {
+                            KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
+                            cpriv.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
+
+                            tk.i_extra_data = cpriv.GetSize();
+                            if( tk.i_extra_data > 0 )
+                            {
+                                tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
+                                memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
                             }
-                            p_sys->ep->Up();
+                            msg_Dbg( p_input, "|   |   |   + Track CodecPrivate size="I64Fd, cpriv.GetSize() );
+                        }
+                        else if( EbmlId( *el3 ) == KaxCodecName::ClassInfos.GlobalId )
+                        {
+                            KaxCodecName &cname = *(KaxCodecName*)el3;
+                            cname.ReadData( p_sys->es->I_O() );
+
+                            tk.psz_codec_name = UTF8ToStr( UTFstring( cname ) );
+                            msg_Dbg( p_input, "|   |   |   + Track Codec Name=%s", tk.psz_codec_name );
                         }
+//                         else if( EbmlId( *el3 ) == KaxCodecSettings::ClassInfos.GlobalId )
+//                         {
+//                             KaxCodecSettings &cset = *(KaxCodecSettings*)el3;
+//                             cset.ReadData( p_sys->es->I_O() );
+
+//                             tk.psz_codec_settings = UTF8ToStr( UTFstring( cset ) );
+//                             msg_Dbg( p_input, "|   |   |   + Track Codec Settings=%s", tk.psz_codec_settings );
+//                         }
+//                         else if( EbmlId( *el3 ) == KaxCodecInfoURL::ClassInfos.GlobalId )
+//                         {
+//                             KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)el3;
+//                             ciurl.ReadData( p_sys->es->I_O() );
+
+//                             tk.psz_codec_info_url = strdup( string( ciurl ).c_str() );
+//                             msg_Dbg( p_input, "|   |   |   + Track Codec Info URL=%s", tk.psz_codec_info_url );
+//                         }
+//                         else if( EbmlId( *el3 ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
+//                         {
+//                             KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)el3;
+//                             cdurl.ReadData( p_sys->es->I_O() );
+
+//                             tk.psz_codec_download_url = strdup( string( cdurl ).c_str() );
+//                             msg_Dbg( p_input, "|   |   |   + Track Codec Info URL=%s", tk.psz_codec_download_url );
+//                         }
+//                         else if( EbmlId( *el3 ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
+//                         {
+//                             KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)el3;
+//                             cdall.ReadData( p_sys->es->I_O() );
+
+//                             msg_Dbg( p_input, "|   |   |   + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
+//                         }
+//                         else if( EbmlId( *el3 ) == KaxTrackOverlay::ClassInfos.GlobalId )
+//                         {
+//                             KaxTrackOverlay &tovr = *(KaxTrackOverlay*)el3;
+//                             tovr.ReadData( p_sys->es->I_O() );
+
+//                             msg_Dbg( p_input, "|   |   |   + Track Overlay=%u <== UNUSED", uint32( tovr ) );
+//                         }
                         else  if( EbmlId( *el3 ) == KaxTrackVideo::ClassInfos.GlobalId )
                         {
                             msg_Dbg( p_input, "|   |   |   + Track Video" );
-                            tk.i_width  = 0;
-                            tk.i_height = 0;
-                            tk.i_display_width  = 0;
-                            tk.i_display_height = 0;
                             tk.f_fps = 0.0;
 
                             p_sys->ep->Down();
 
                             while( ( el4 = p_sys->ep->Get() ) != NULL )
                             {
-                                if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
+//                                 if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
+//                                 {
+//                                     KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
+//                                     fint.ReadData( p_sys->es->I_O() );
+
+//                                     msg_Dbg( p_input, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
+//                                 }
+//                                 else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
+//                                 {
+//                                     KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
+//                                     stereo.ReadData( p_sys->es->I_O() );
+
+//                                     msg_Dbg( p_input, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
+//                                 }
+//                                 else 
+                              if( EbmlId( *el4 ) == KaxVideoPixelWidth::ClassInfos.GlobalId )
                                 {
                                     KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)el4;
                                     vwidth.ReadData( p_sys->es->I_O() );
 
-                                    tk.i_width = uint16( vwidth );
+                                    tk.fmt.video.i_width = uint16( vwidth );
                                     msg_Dbg( p_input, "|   |   |   |   + width=%d", uint16( vwidth ) );
                                 }
                                 else if( EbmlId( *el4 ) == KaxVideoPixelHeight::ClassInfos.GlobalId )
@@ -726,7 +726,7 @@ static int Activate( vlc_object_t * p_this )
                                     KaxVideoPixelWidth &vheight = *(KaxVideoPixelWidth*)el4;
                                     vheight.ReadData( p_sys->es->I_O() );
 
-                                    tk.i_height = uint16( vheight );
+                                    tk.fmt.video.i_height = uint16( vheight );
                                     msg_Dbg( p_input, "|   |   |   |   + height=%d", uint16( vheight ) );
                                 }
                                 else if( EbmlId( *el4 ) == KaxVideoDisplayWidth::ClassInfos.GlobalId )
@@ -734,7 +734,7 @@ static int Activate( vlc_object_t * p_this )
                                     KaxVideoDisplayWidth &vwidth = *(KaxVideoDisplayWidth*)el4;
                                     vwidth.ReadData( p_sys->es->I_O() );
 
-                                    tk.i_display_width = uint16( vwidth );
+                                    tk.fmt.video.i_visible_width = uint16( vwidth );
                                     msg_Dbg( p_input, "|   |   |   |   + display width=%d", uint16( vwidth ) );
                                 }
                                 else if( EbmlId( *el4 ) == KaxVideoDisplayHeight::ClassInfos.GlobalId )
@@ -742,7 +742,7 @@ static int Activate( vlc_object_t * p_this )
                                     KaxVideoDisplayWidth &vheight = *(KaxVideoDisplayWidth*)el4;
                                     vheight.ReadData( p_sys->es->I_O() );
 
-                                    tk.i_display_height = uint16( vheight );
+                                    tk.fmt.video.i_visible_height = uint16( vheight );
                                     msg_Dbg( p_input, "|   |   |   |   + display height=%d", uint16( vheight ) );
                                 }
                                 else if( EbmlId( *el4 ) == KaxVideoFrameRate::ClassInfos.GlobalId )
@@ -753,82 +753,86 @@ static int Activate( vlc_object_t * p_this )
                                     tk.f_fps = float( vfps );
                                     msg_Dbg( p_input, "   |   |   |   + fps=%f", float( vfps ) );
                                 }
+//                                 else if( EbmlId( *el4 ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
+//                                 {
+//                                      KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)el4;
+//                                     vdmode.ReadData( p_sys->es->I_O() );
+
+//                                     msg_Dbg( p_input, "|   |   |   |   + Track Video Display Unit=%s",
+//                                              uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
+//                                 }
+//                                 else if( EbmlId( *el4 ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
+//                                 {
+//                                     KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)el4;
+//                                     ratio.ReadData( p_sys->es->I_O() );
+
+//                                     msg_Dbg( p_input, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
+//                                 }
+//                                 else if( EbmlId( *el4 ) == KaxVideoGamma::ClassInfos.GlobalId )
+//                                 {
+//                                     KaxVideoGamma &gamma = *(KaxVideoGamma*)el4;
+//                                     gamma.ReadData( p_sys->es->I_O() );
+
+//                                     msg_Dbg( p_input, "   |   |   |   + fps=%f", float( gamma ) );
+//                                 }
                                 else
                                 {
-                                    msg_Dbg( p_input, "|   |   |   |   + Unknow (%s)", typeid(*el4).name() );
+                                    msg_Dbg( p_input, "|   |   |   |   + Unknown (%s)", typeid(*el4).name() );
                                 }
                             }
                             p_sys->ep->Up();
                         }
-                        else  if( EbmlId( *el3 ) == KaxCodecID::ClassInfos.GlobalId )
+                        else  if( EbmlId( *el3 ) == KaxTrackAudio::ClassInfos.GlobalId )
                         {
-                            KaxCodecID &codecid = *(KaxCodecID*)el3;
-                            codecid.ReadData( p_sys->es->I_O() );
+                            msg_Dbg( p_input, "|   |   |   + Track Audio" );
 
-                            tk.psz_codec = strdup( string( codecid ).c_str() );
-                            msg_Dbg( p_input, "|   |   |   + Track CodecId=%s", string( codecid ).c_str() );
-                        }
-                        else  if( EbmlId( *el3 ) == KaxCodecPrivate::ClassInfos.GlobalId )
-                        {
-                            KaxCodecPrivate &cpriv = *(KaxCodecPrivate*)el3;
-                            cpriv.ReadData( p_sys->es->I_O() );
+                            p_sys->ep->Down();
 
-                            tk.i_extra_data = cpriv.GetSize();
-                            if( tk.i_extra_data > 0 )
+                            while( ( el4 = p_sys->ep->Get() ) != NULL )
                             {
-                                tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
-                                memcpy( tk.p_extra_data, cpriv.GetBuffer(), tk.i_extra_data );
-                            }
-                            msg_Dbg( p_input, "|   |   |   + Track CodecPrivate size=%lld", cpriv.GetSize() );
-                        }
-                        else  if( EbmlId( *el3 ) == KaxTrackFlagDefault::ClassInfos.GlobalId )
-                        {
-                            KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)el3;
-                            fdef.ReadData( p_sys->es->I_O() );
-
-                            tk.b_default = uint32( fdef );
-                            msg_Dbg( p_input, "|   |   |   + Track Default=%u", uint32( fdef )  );
-                        }
-                        else  if( EbmlId( *el3 ) == KaxTrackLanguage::ClassInfos.GlobalId )
-                        {
-                            KaxTrackLanguage &lang = *(KaxTrackLanguage*)el3;
-
-                            lang.ReadData( p_sys->es->I_O() );
-
-                            tk.psz_language = strdup( string( lang ).c_str() );
-                            msg_Dbg( p_input, "|   |   |   + Track Language=`%s'", string( lang ).c_str() );
-                        }
-                        else  if( EbmlId( *el3 ) == KaxTrackFlagLacing::ClassInfos.GlobalId )
-                        {
-                            KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)el3;
-                            lac.ReadData( p_sys->es->I_O() );
+                                if( EbmlId( *el4 ) == KaxAudioSamplingFreq::ClassInfos.GlobalId )
+                                {
+                                    KaxAudioSamplingFreq &afreq = *(KaxAudioSamplingFreq*)el4;
+                                    afreq.ReadData( p_sys->es->I_O() );
 
-                            msg_Dbg( p_input, "|   |   |   + Track Lacing=%d", uint32( lac ) );
-                        }
-                        else  if( EbmlId( *el3 ) == KaxTrackMinCache::ClassInfos.GlobalId )
-                        {
-                            KaxTrackMinCache &cmin = *(KaxTrackMinCache*)el3;
-                            cmin.ReadData( p_sys->es->I_O() );
+                                    tk.fmt.audio.i_rate = (int)float( afreq );
+                                    msg_Dbg( p_input, "|   |   |   |   + afreq=%d", tk.fmt.audio.i_rate );
+                                }
+                                else if( EbmlId( *el4 ) == KaxAudioChannels::ClassInfos.GlobalId )
+                                {
+                                    KaxAudioChannels &achan = *(KaxAudioChannels*)el4;
+                                    achan.ReadData( p_sys->es->I_O() );
 
-                            msg_Dbg( p_input, "|   |   |   + Track MinCache=%d", uint32( cmin ) );
-                        }
-                        else  if( EbmlId( *el3 ) == KaxTrackMaxCache::ClassInfos.GlobalId )
-                        {
-                            KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)el3;
-                            cmax.ReadData( p_sys->es->I_O() );
+                                    tk.fmt.audio.i_channels = uint8( achan );
+                                    msg_Dbg( p_input, "|   |   |   |   + achan=%u", uint8( achan ) );
+                                }
+                                else if( EbmlId( *el4 ) == KaxAudioBitDepth::ClassInfos.GlobalId )
+                                {
+                                    KaxAudioBitDepth &abits = *(KaxAudioBitDepth*)el4;
+                                    abits.ReadData( p_sys->es->I_O() );
 
-                            msg_Dbg( p_input, "|   |   |   + Track MaxCache=%d", uint32( cmax ) );
+                                    tk.fmt.audio.i_bitspersample = uint8( abits );
+                                    msg_Dbg( p_input, "|   |   |   |   + abits=%u", uint8( abits ) );
+                                }
+                                else
+                                {
+                                    msg_Dbg( p_input, "|   |   |   |   + Unknown (%s)", typeid(*el4).name() );
+                                }
+                            }
+                            p_sys->ep->Up();
                         }
                         else
                         {
-                            msg_Dbg( p_input, "|   |   |   + Unknow (%s)", typeid(*el3).name() );
+                            msg_Dbg( p_input, "|   |   |   + Unknown (%s)",
+                                     typeid(*el3).name() );
                         }
                     }
                     p_sys->ep->Up();
                 }
                 else
                 {
-                    msg_Dbg( p_input, "|   |   + Unknow (%s)", typeid(*el2).name() );
+                    msg_Dbg( p_input, "|   |   + Unknown (%s)",
+                             typeid(*el2).name() );
                 }
 #undef tk
             }
@@ -853,7 +857,7 @@ static int Activate( vlc_object_t * p_this )
                         {
                             KaxSeekID &sid = *(KaxSeekID*)el;
 
-                            sid.ReadData( p_sys->es->I_O() );
+                            sid.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
 
                             id = EbmlId( sid.GetBuffer(), sid.GetSize() );
                         }
@@ -861,13 +865,14 @@ static int Activate( vlc_object_t * p_this )
                         {
                             KaxSeekPosition &spos = *(KaxSeekPosition*)el;
 
-                            spos.ReadData( p_sys->es->I_O() );
+                            spos.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
 
                             i_pos = uint64( spos );
                         }
                         else
                         {
-                            msg_Dbg( p_input, "|   |   |   + Unknow (%s)", typeid(*el).name() );
+                            msg_Dbg( p_input, "|   |   |   + Unknown (%s)",
+                                     typeid(*el).name() );
                         }
                     }
                     p_sys->ep->Up();
@@ -876,19 +881,29 @@ static int Activate( vlc_object_t * p_this )
                     {
                         if( id == KaxCues::ClassInfos.GlobalId )
                         {
-                            msg_Dbg( p_input, "|   |   |   = cues at %lld", i_pos );
+                            msg_Dbg( p_input, "|   |   |   = cues at "I64Fd,
+                                     i_pos );
                             p_sys->i_cues_position = p_sys->segment->GetGlobalPosition( i_pos );
                         }
                         else if( id == KaxChapters::ClassInfos.GlobalId )
                         {
-                            msg_Dbg( p_input, "|   |   |   = chapters at %lld", i_pos );
+                            msg_Dbg( p_input, "|   |   |   = chapters at "I64Fd,
+                                     i_pos );
                             p_sys->i_chapters_position = p_sys->segment->GetGlobalPosition( i_pos );
                         }
+                        else if( id == KaxTags::ClassInfos.GlobalId )
+                        {
+                            msg_Dbg( p_input, "|   |   |   = tags at "I64Fd,
+                                     i_pos );
+                            p_sys->i_tags_position = p_sys->segment->GetGlobalPosition( i_pos );
+                        }
+
                     }
                 }
                 else
                 {
-                    msg_Dbg( p_input, "|   |   + Unknow (%s)", typeid(*el).name() );
+                    msg_Dbg( p_input, "|   |   + Unknown (%s)",
+                             typeid(*el).name() );
                 }
             }
             p_sys->ep->Up();
@@ -900,15 +915,20 @@ static int Activate( vlc_object_t * p_this )
         else if( EbmlId( *el1 ) == KaxCluster::ClassInfos.GlobalId )
         {
             msg_Dbg( p_input, "|   + Cluster" );
+
             p_sys->cluster = (KaxCluster*)el1;
-            p_sys->ep->Down();
 
+            p_sys->ep->Down();
             /* stop parsing the stream */
             break;
         }
+#ifdef HAVE_MATROSKA_KAXATTACHMENTS_H
+        else if( EbmlId( *el1 ) == KaxAttachments::ClassInfos.GlobalId )
+#else
         else if( EbmlId( *el1 ) == KaxAttachements::ClassInfos.GlobalId )
+#endif
         {
-            msg_Dbg( p_input, "|   + Attachements FIXME TODO (but probably never supported)" );
+            msg_Dbg( p_input, "|   + Attachments FIXME TODO (but probably never supported)" );
         }
         else if( EbmlId( *el1 ) == KaxChapters::ClassInfos.GlobalId )
         {
@@ -920,7 +940,7 @@ static int Activate( vlc_object_t * p_this )
         }
         else
         {
-            msg_Dbg( p_input, "|   + Unknow (%s)", typeid(*el1).name() );
+            msg_Dbg( p_input, "|   + Unknown (%s)", typeid(*el1).name() );
         }
     }
 
@@ -935,143 +955,25 @@ static int Activate( vlc_object_t * p_this )
         msg_Warn( p_input, "chapters unsupported" );
     }
 
-#if 0
-    if( p_sys->i_cues_position == -1 && config_GetInt( p_input, "mkv-index" ) != 0 )
-    {
-        int64_t i_sav_position = p_sys->in->getFilePointer();
-        EbmlParser *ep;
-
-        /* parse to find cues gathering info on all clusters */
-        msg_Dbg( p_input, "no cues referenced, looking for one" );
-
-        /* ugly but like easier */
-        p_sys->in->setFilePointer( p_sys->cluster->GetElementPosition(), seek_beginning );
-
-        ep = new EbmlParser( p_sys->es, p_sys->segment );
-        while( ( el = ep->Get() ) != NULL )
-        {
-            if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
-            {
-                int64_t i_pos = el->GetElementPosition();
-
-                msg_Dbg( p_input, "found a cluster at %lld", i_pos );
-            }
-            else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
-            {
-                msg_Dbg( p_input, "found a Cues !" );
-            }
-        }
-        delete ep;
-        msg_Dbg( p_input, "lookind done." );
-
-        p_sys->in->setFilePointer( i_sav_position, seek_beginning );
-    }
-#endif
-
     /* *** Load the cue if found *** */
     if( p_sys->i_cues_position >= 0 )
     {
-        int64_t i_sav_position = p_sys->in->getFilePointer();
-        EbmlParser *ep;
-        EbmlElement *cues;
+        vlc_bool_t b_seekable;
 
-        msg_Dbg( p_input, "loading cues" );
-        p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
-        cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
-
-        ep = new EbmlParser( p_sys->es, cues );
-        while( ( el = ep->Get() ) != NULL )
+        stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
+        if( b_seekable )
         {
-            if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
-            {
-#define idx p_sys->index[p_sys->i_index]
-
-                idx.i_track       = -1;
-                idx.i_block_number= -1;
-                idx.i_position    = -1;
-                idx.i_time        = -1;
-                idx.b_key         = VLC_TRUE;
-
-                ep->Down();
-                while( ( el = ep->Get() ) != NULL )
-                {
-                    if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
-                    {
-                        KaxCueTime &ctime = *(KaxCueTime*)el;
-
-                        ctime.ReadData( p_sys->es->I_O() );
-
-                        idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
-                    }
-                    else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
-                    {
-                        ep->Down();
-                        while( ( el = ep->Get() ) != NULL )
-                        {
-                            if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
-                            {
-                                KaxCueTrack &ctrack = *(KaxCueTrack*)el;
-
-                                ctrack.ReadData( p_sys->es->I_O() );
-                                idx.i_track = uint16( ctrack );
-                            }
-                            else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
-                            {
-                                KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
-
-                                ccpos.ReadData( p_sys->es->I_O() );
-                                idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
-                            }
-                            else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
-                            {
-                                KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
-
-                                cbnum.ReadData( p_sys->es->I_O() );
-                                idx.i_block_number = uint32( cbnum );
-                            }
-                            else
-                            {
-                                msg_Dbg( p_input, "         * Unknow (%s)", typeid(*el).name() );
-                            }
-                        }
-                        ep->Up();
-                    }
-                    else
-                    {
-                        msg_Dbg( p_input, "     * Unknow (%s)", typeid(*el).name() );
-                    }
-                }
-                ep->Up();
-
-                msg_Dbg( p_input, " * added time=%lld pos=%lld track=%d bnum=%d",
-                         idx.i_time, idx.i_position, idx.i_track, idx.i_block_number );
-
-                p_sys->i_index++;
-                if( p_sys->i_index >= p_sys->i_index_max )
-                {
-                    p_sys->i_index_max += 1024;
-                    p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
-                }
-#undef idx
-            }
-            else
-            {
-                msg_Dbg( p_input, " * Unknow (%s)", typeid(*el).name() );
-            }
+            LoadCues( p_input );
         }
-        delete ep;
-        delete cues;
-
-        msg_Dbg( p_input, "loading cues done." );
-        p_sys->in->setFilePointer( i_sav_position, seek_beginning );
     }
 
-    if( p_sys->i_index <= 0 )
+    if( !p_sys->b_cues || p_sys->i_index <= 0 )
     {
-        /* FIXME FIXME FIXME */
-        msg_Warn( p_input, "no cues found -> unseekable stream for now FIXME" );
-        p_input->stream.b_seekable = VLC_FALSE;
-        /* FIXME FIXME FIXME */
+        msg_Warn( p_input, "no cues/empty cues found->seek won't be precise" );
+
+        IndexAppendCluster( p_input, p_sys->cluster );
+
+        p_sys->b_cues = VLC_FALSE;
     }
 
     /* Create one program */
@@ -1082,93 +984,73 @@ static int Activate( vlc_object_t * p_this )
         msg_Err( p_input, "cannot init stream" );
         goto error;
     }
-    if( input_AddProgram( p_input, 0, 0) == NULL )
-    {
-        vlc_mutex_unlock( &p_input->stream.stream_lock );
-        msg_Err( p_input, "cannot add program" );
-        goto error;
-    }
-    p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
+    p_input->stream.i_mux_rate = 0;
+    vlc_mutex_unlock( &p_input->stream.stream_lock );
+
     if( p_sys->f_duration > 1001.0 )
     {
         mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000.0 );
-        p_input->stream.i_mux_rate = p_input->stream.p_selected_area->i_size / 50 / i_duration;
+        p_input->stream.i_mux_rate = stream_Size( p_input->s )/50 / i_duration;
     }
-    else
-    {
-        p_input->stream.i_mux_rate = 0;
-    }
-    vlc_mutex_unlock( &p_input->stream.stream_lock );
 
     /* add all es */
     msg_Dbg( p_input, "found %d es", p_sys->i_track );
     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
     {
 #define tk  p_sys->track[i_track]
-        if( tk.i_cat == UNKNOWN_ES )
+        if( tk.fmt.i_cat == UNKNOWN_ES )
         {
             msg_Warn( p_input, "invalid track[%d, n=%d]", i_track, tk.i_number );
             tk.p_es = NULL;
             continue;
         }
-        tk.p_es = input_AddES( p_input,
-                               p_input->stream.p_selected_program,
-                               i_track + 1,
-                               tk.i_cat,
-                               tk.psz_language, 0 );
+
+        if( tk.fmt.i_cat == SPU_ES )
+        {
+            vlc_value_t val;
+            val.psz_string = "UTF-8";
+#if defined(HAVE_ICONV)
+            var_Create( p_input, "subsdec-encoding", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
+            var_Set( p_input, "subsdec-encoding", val );
+#endif
+        }
         if( !strcmp( tk.psz_codec, "V_MS/VFW/FOURCC" ) )
         {
             if( tk.i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
             {
                 msg_Err( p_input, "missing/invalid BITMAPINFOHEADER" );
-                tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+                tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
             }
             else
             {
                 BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
 
-                p_bih->biSize           = GetDWLE( &p_bih->biSize );
-                p_bih->biWidth          = GetDWLE( &p_bih->biWidth );
-                p_bih->biHeight         = GetDWLE( &p_bih->biHeight );
-                p_bih->biPlanes         = GetWLE( &p_bih->biPlanes );
-                p_bih->biBitCount       = GetWLE( &p_bih->biBitCount );
-                p_bih->biCompression    = GetFOURCC( &p_bih->biCompression );
-                p_bih->biSizeImage      = GetDWLE( &p_bih->biSizeImage );
-                p_bih->biXPelsPerMeter  = GetDWLE( &p_bih->biXPelsPerMeter );
-                p_bih->biYPelsPerMeter  = GetDWLE( &p_bih->biYPelsPerMeter );
-                p_bih->biClrUsed        = GetDWLE( &p_bih->biClrUsed );
-                p_bih->biClrImportant   = GetDWLE( &p_bih->biClrImportant );
-
-
-                tk.i_codec = p_bih->biCompression;
-                tk.p_es->p_bitmapinfoheader = p_bih;
+                tk.fmt.video.i_width = GetDWLE( &p_bih->biWidth );
+                tk.fmt.video.i_height= GetDWLE( &p_bih->biHeight );
+                tk.fmt.i_codec       = GetFOURCC( &p_bih->biCompression );
+
+                tk.fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
+                if( tk.fmt.i_extra > 0 )
+                {
+                    tk.fmt.p_extra = malloc( tk.fmt.i_extra );
+                    memcpy( tk.fmt.p_extra, &p_bih[1], tk.fmt.i_extra );
+                }
             }
         }
         else if( !strcmp( tk.psz_codec, "V_MPEG1" ) ||
                  !strcmp( tk.psz_codec, "V_MPEG2" ) )
         {
-            tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
+            tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'v' );
         }
         else if( !strncmp( tk.psz_codec, "V_MPEG4", 7 ) )
         {
-            BITMAPINFOHEADER *p_bih;
-
-            tk.i_extra_data = sizeof( BITMAPINFOHEADER );
-            tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
-
-            p_bih = (BITMAPINFOHEADER*)tk.p_extra_data;
-            memset( p_bih, 0, sizeof( BITMAPINFOHEADER ) );
-            p_bih->biSize  = sizeof( BITMAPINFOHEADER );
-            p_bih->biWidth = tk.i_width;
-            p_bih->biHeight= tk.i_height;
-
             if( !strcmp( tk.psz_codec, "V_MPEG4/MS/V3" ) )
             {
-                tk.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
+                tk.fmt.i_codec = VLC_FOURCC( 'D', 'I', 'V', '3' );
             }
             else
             {
-                tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
+                tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
             }
         }
         else if( !strcmp( tk.psz_codec, "A_MS/ACM" ) )
@@ -1176,78 +1058,52 @@ static int Activate( vlc_object_t * p_this )
             if( tk.i_extra_data < (int)sizeof( WAVEFORMATEX ) )
             {
                 msg_Err( p_input, "missing/invalid WAVEFORMATEX" );
-                tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+                tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
             }
             else
             {
                 WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tk.p_extra_data;
 
-                p_wf->wFormatTag        = GetWLE( &p_wf->wFormatTag );
-                p_wf->nChannels         = GetWLE( &p_wf->nChannels );
-                p_wf->nSamplesPerSec    = GetDWLE( &p_wf->nSamplesPerSec );
-                p_wf->nAvgBytesPerSec   = GetDWLE( &p_wf->nAvgBytesPerSec );
-                p_wf->nBlockAlign       = GetWLE( &p_wf->nBlockAlign );
-                p_wf->wBitsPerSample    = GetWLE( &p_wf->wBitsPerSample );
-                p_wf->cbSize            = GetWLE( &p_wf->cbSize );
+                wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tk.fmt.i_codec, NULL );
 
-                switch( p_wf->wFormatTag )
+                tk.fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
+                tk.fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
+                tk.fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
+                tk.fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
+                tk.fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
+
+                tk.fmt.i_extra            = GetWLE( &p_wf->cbSize );
+                if( tk.fmt.i_extra > 0 )
                 {
-                    case WAVE_FORMAT_PCM:
-                        tk.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
-                        break;
-                    case WAVE_FORMAT_ADPCM:
-                        tk.i_codec = VLC_FOURCC( 'm', 's', 0x00, 0x02 );
-                        break;
-                    case WAVE_FORMAT_ALAW:
-                        tk.i_codec = VLC_FOURCC( 'a', 'l', 'a', 'w' );
-                        break;
-                    case WAVE_FORMAT_MULAW:
-                        tk.i_codec = VLC_FOURCC( 'm', 'l', 'a', 'w' );
-                        break;
-                    case WAVE_FORMAT_IMA_ADPCM:
-                        tk.i_codec = VLC_FOURCC( 'm', 's', 0x00, 0x11 );
-                        break;
-                    case WAVE_FORMAT_MPEG:
-                    case WAVE_FORMAT_MPEGLAYER3:
-                        tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
-                        break;
-                    case WAVE_FORMAT_A52:
-                        tk.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
-                        break;
-                    case WAVE_FORMAT_WMA1:
-                        tk.i_codec = VLC_FOURCC( 'w', 'm', 'a', '1' );
-                        break;
-                    case WAVE_FORMAT_WMA2:
-                        tk.i_codec = VLC_FOURCC( 'w', 'm', 'a', '2' );
-                        break;
-                    case WAVE_FORMAT_WMA3:
-                        tk.i_codec = VLC_FOURCC( 'w', 'm', 'a', '3' );
-                        break;
-                    default:
-                        msg_Err( p_input, "unknown wFormatTag=0x%x", p_wf->wFormatTag );
-                        tk.i_codec = VLC_FOURCC( 'm', 's', p_wf->wFormatTag >> 8, p_wf->wFormatTag&0xff );
-                        break;
+                    tk.fmt.p_extra = malloc( tk.fmt.i_extra );
+                    memcpy( tk.fmt.p_extra, &p_wf[1], tk.fmt.i_extra );
                 }
-                tk.p_es->p_waveformatex = p_wf;
             }
         }
         else if( !strcmp( tk.psz_codec, "A_MPEG/L3" ) ||
                  !strcmp( tk.psz_codec, "A_MPEG/L2" ) ||
                  !strcmp( tk.psz_codec, "A_MPEG/L1" ) )
         {
-            tk.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
+            tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', 'g', 'a' );
         }
         else if( !strcmp( tk.psz_codec, "A_AC3" ) )
         {
-            tk.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
+            tk.fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
         }
         else if( !strcmp( tk.psz_codec, "A_DTS" ) )
         {
-            tk.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
+            tk.fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
+        }
+        else if( !strcmp( tk.psz_codec, "A_FLAC" ) )
+        {
+            tk.fmt.i_codec = VLC_FOURCC( 'f', 'l', 'a', 'c' );
+            tk.fmt.i_extra = tk.i_extra_data;
+            tk.fmt.p_extra = malloc( tk.i_extra_data );
+            memcpy( tk.fmt.p_extra,tk.p_extra_data, tk.i_extra_data );
         }
         else if( !strcmp( tk.psz_codec, "A_VORBIS" ) )
         {
-            tk.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
+            tk.fmt.i_codec = VLC_FOURCC( 'v', 'o', 'r', 'b' );
             tk.i_data_init = tk.i_extra_data;
             tk.p_data_init = tk.p_extra_data;
         }
@@ -1255,14 +1111,13 @@ static int Activate( vlc_object_t * p_this )
                  !strncmp( tk.psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
         {
             int i_profile, i_srate;
-            static int i_sample_rates[] =
+            static unsigned int i_sample_rates[] =
             {
                     96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
                         16000, 12000, 11025, 8000,  7350,  0,     0,     0
             };
-            WAVEFORMATEX *p_wf;
 
-            tk.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
+            tk.fmt.i_codec = VLC_FOURCC( 'm', 'p', '4', 'a' );
             /* create data for faad (MP4DecSpecificDescrTag)*/
 
             if( !strcmp( &tk.psz_codec[12], "MAIN" ) )
@@ -1284,125 +1139,64 @@ static int Activate( vlc_object_t * p_this )
 
             for( i_srate = 0; i_srate < 13; i_srate++ )
             {
-                if( i_sample_rates[i_srate] == tk.i_samplerate )
+                if( i_sample_rates[i_srate] == tk.fmt.audio.i_rate )
                 {
                     break;
                 }
             }
             msg_Dbg( p_input, "profile=%d srate=%d", i_profile, i_srate );
 
-            tk.i_extra_data = sizeof( WAVEFORMATEX ) + 2;
-            tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
-            p_wf = (WAVEFORMATEX*)tk.p_extra_data;
-
-            p_wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
-            p_wf->nChannels  = tk.i_channels;
-            p_wf->nSamplesPerSec = tk.i_samplerate;
-            p_wf->nAvgBytesPerSec = 0;
-            p_wf->nBlockAlign = 0;
-            p_wf->wBitsPerSample = 0;
-            p_wf->cbSize = 2;
-
-            tk.p_extra_data[sizeof( WAVEFORMATEX )+ 0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
-            tk.p_extra_data[sizeof( WAVEFORMATEX )+ 1] = ((i_srate & 0x1) << 7) | (tk.i_channels << 3);
-
-            tk.p_es->p_waveformatex = p_wf;
+            tk.fmt.i_extra = 2;
+            tk.fmt.p_extra = malloc( tk.fmt.i_extra );
+            ((uint8_t*)tk.fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
+            ((uint8_t*)tk.fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tk.fmt.audio.i_channels << 3);
         }
         else if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) ||
                  !strcmp( tk.psz_codec, "A_PCM/INT/LIT" ) ||
                  !strcmp( tk.psz_codec, "A_PCM/FLOAT/IEEE" ) )
         {
-            WAVEFORMATEX *p_wf;
-
-            tk.i_extra_data = sizeof( WAVEFORMATEX );
-            tk.p_extra_data = (uint8_t*)malloc( tk.i_extra_data );
-
-            p_wf = (WAVEFORMATEX*)tk.p_extra_data;
-
-            if( !strncmp( &tk.psz_codec[6], "INT", 3 ) )
+            if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
             {
-                p_wf->wFormatTag = WAVE_FORMAT_PCM;
+                tk.fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
             }
             else
             {
-                p_wf->wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
-            }
-            p_wf->nChannels  = tk.i_channels;
-            p_wf->nSamplesPerSec = tk.i_samplerate;
-            p_wf->nAvgBytesPerSec = 0;
-            p_wf->nBlockAlign = ( tk.i_bitspersample + 7 ) / 8 * tk.i_channels;
-            p_wf->wBitsPerSample = tk.i_bitspersample;
-            p_wf->cbSize = 0;
-
-            tk.p_es->p_waveformatex = p_wf;
-
-            if( !strcmp( tk.psz_codec, "A_PCM/INT/BIG" ) )
-            {
-                tk.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
-            }
-            else
-            {
-                tk.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
+                tk.fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
             }
+            tk.fmt.audio.i_blockalign = ( tk.fmt.audio.i_bitspersample + 7 ) / 8 * tk.fmt.audio.i_channels;
         }
         else if( !strcmp( tk.psz_codec, "S_TEXT/UTF8" ) )
         {
-            tk.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
+            tk.fmt.i_codec = VLC_FOURCC( 's', 'u', 'b', 't' );
+        }
+        else if( !strcmp( tk.psz_codec, "S_TEXT/SSA" ) ||
+                 !strcmp( tk.psz_codec, "S_SSA" ) ||
+                 !strcmp( tk.psz_codec, "S_ASS" ))
+        {
+            tk.fmt.i_codec = VLC_FOURCC( 's', 's', 'a', ' ' );
+#if 0
+            /* FIXME */
+            tk.fmt.i_extra = sizeof( subtitle_data_t );
+            tk.fmt.p_extra = malloc( tk.fmt.i_extra );
+            ((es_sys_t*)tk->fmt.p_extra)->psz_header = strdup( (char *)tk.p_extra_data );
+#endif
+        }
+        else if( !strcmp( tk.psz_codec, "S_VOBSUB" ) )
+        {
+            tk.fmt.i_codec = VLC_FOURCC( 's','p','u',' ' );
         }
         else
         {
             msg_Err( p_input, "unknow codec id=`%s'", tk.psz_codec );
-            tk.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+            tk.fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
         }
 
-        tk.p_es->i_fourcc = tk.i_codec;
-#undef tk
-    }
-
-    /* select track all video, one audio, no spu TODO : improve */
-    b_audio_selected = VLC_FALSE;
-    i_audio_channel = 0;
-    i_spu_channel = 0;
-    for( i_track = 0; i_track < p_sys->i_track; i_track++ )
-    {
-#define tk  p_sys->track[i_track]
-        switch( tk.i_cat )
-        {
-            case VIDEO_ES:
-                vlc_mutex_lock( &p_input->stream.stream_lock );
-                input_SelectES( p_input, tk.p_es );
-                vlc_mutex_unlock( &p_input->stream.stream_lock );
-                break;
-
-            case AUDIO_ES:
-                if( ( !b_audio_selected && config_GetInt( p_input, "audio-channel" ) < 0 ) ||
-                    i_audio_channel == config_GetInt( p_input, "audio-channel" ) )
-                {
-                    vlc_mutex_lock( &p_input->stream.stream_lock );
-                    input_SelectES( p_input, tk.p_es );
-                    vlc_mutex_unlock( &p_input->stream.stream_lock );
-
-                    b_audio_selected = tk.p_es->p_decoder_fifo ? VLC_TRUE : VLC_FALSE;
-                }
-                i_audio_channel++;
-                break;
-            case SPU_ES:
-                if( i_spu_channel == config_GetInt( p_input, "spu-channel" ) )
-                {
-                    vlc_mutex_lock( &p_input->stream.stream_lock );
-                    input_SelectES( p_input, tk.p_es );
-                    vlc_mutex_unlock( &p_input->stream.stream_lock );
-                }
-                i_spu_channel++;
-                break;
-        }
+        tk.p_es = es_out_Add( p_input->p_es_out, &tk.fmt );
 #undef tk
     }
 
-    if( !b_audio_selected )
-    {
-        msg_Warn( p_input, "cannot find/select audio track" );
-    }
+    /* add informations */
+    InformationsCreate( p_input );
 
     return VLC_SUCCESS;
 
@@ -1414,9 +1208,9 @@ error:
 }
 
 /*****************************************************************************
- * Deactivate: frees unused data
+ * Close: frees unused data
  *****************************************************************************/
-static void Deactivate( vlc_object_t *p_this )
+static void Close( vlc_object_t *p_this )
 {
     input_thread_t *p_input = (input_thread_t *)p_this;
     demux_sys_t    *p_sys   = p_input->p_demux_data;
@@ -1426,18 +1220,33 @@ static void Deactivate( vlc_object_t *p_this )
     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
     {
 #define tk  p_sys->track[i_track]
+        if( tk.fmt.psz_description )
+        {
+            free( tk.fmt.psz_language );
+        }
         if( tk.psz_codec )
         {
             free( tk.psz_codec );
         }
-        if( tk.psz_language )
+        if( tk.fmt.psz_language )
         {
-            free( tk.psz_language );
+            free( tk.fmt.psz_language );
         }
 #undef tk
     }
     free( p_sys->track );
 
+    if( p_sys->psz_writing_application  )
+    {
+        free( p_sys->psz_writing_application );
+    }
+    if( p_sys->psz_muxing_application  )
+    {
+        free( p_sys->psz_muxing_application );
+    }
+
+    delete p_sys->segment;
+
     delete p_sys->ep;
     delete p_sys->es;
     delete p_sys->in;
@@ -1468,6 +1277,14 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r
 
         if( el == NULL && *pp_block != NULL )
         {
+            /* update the index */
+#define idx p_sys->index[p_sys->i_index - 1]
+            if( p_sys->i_index > 0 && idx.i_time == -1 )
+            {
+                idx.i_time        = (*pp_block)->GlobalTimecode() * (mtime_t) 1000 / p_sys->i_timescale;
+                idx.b_key         = *pi_ref1 == -1 ? VLC_TRUE : VLC_FALSE;
+            }
+#undef idx
             return VLC_SUCCESS;
         }
 
@@ -1488,6 +1305,14 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r
             if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
             {
                 p_sys->cluster = (KaxCluster*)el;
+
+                /* add it to the index */
+                if( p_sys->i_index == 0 ||
+                    ( p_sys->i_index > 0 && p_sys->index[p_sys->i_index - 1].i_position < (int64_t)p_sys->cluster->GetElementPosition() ) )
+                {
+                    IndexAppendCluster( p_input, p_sys->cluster );
+                }
+
                 p_sys->ep->Down();
             }
             else if( EbmlId( *el ) == KaxCues::ClassInfos.GlobalId )
@@ -1506,7 +1331,7 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r
             {
                 KaxClusterTimecode &ctc = *(KaxClusterTimecode*)el;
 
-                ctc.ReadData( p_sys->es->I_O() );
+                ctc.ReadData( p_sys->es->I_O(), SCOPE_ALL_DATA );
                 p_sys->cluster->InitTimecode( uint64( ctc ), p_sys->i_timescale );
             }
             else if( EbmlId( *el ) == KaxBlockGroup::ClassInfos.GlobalId )
@@ -1555,26 +1380,13 @@ static int BlockGet( input_thread_t *p_input, KaxBlock **pp_block, int64_t *pi_r
     }
 }
 
-static pes_packet_t *MemToPES( input_thread_t *p_input, uint8_t *p_mem, int i_mem )
+static block_t *MemToBlock( input_thread_t *p_input, uint8_t *p_mem, int i_mem)
 {
-    pes_packet_t *p_pes;
-    data_packet_t *p_data;
-
-    if( ( p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
-    {
-        return NULL;
-    }
-
-    p_data = input_NewPacket( p_input->p_method_data, i_mem);
-
-    memcpy( p_data->p_payload_start, p_mem, i_mem );
-    p_data->p_payload_end = p_data->p_payload_start + i_mem;
-
-    p_pes->p_first = p_pes->p_last = p_data;
-    p_pes->i_nb_data = 1;
-    p_pes->i_pes_size = i_mem;
-
-    return p_pes;
+    block_t *p_block;
+    if( !(p_block = block_New( p_input, i_mem ) ) ) return NULL;
+    memcpy( p_block->p_buffer, p_mem, i_mem );
+    //p_block->i_rate = p_input->stream.control.i_rate;
+    return p_block;
 }
 
 static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts, mtime_t i_duration )
@@ -1582,8 +1394,8 @@ static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts
     demux_sys_t    *p_sys   = p_input->p_demux_data;
 
     int             i_track;
-    pes_packet_t    *p_pes;
     unsigned int    i;
+    vlc_bool_t      b;
 
 #define tk  p_sys->track[i_track]
     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
@@ -1600,74 +1412,26 @@ static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts
         return;
     }
 
-    if( tk.p_es->p_decoder_fifo == NULL )
+    es_out_Control( p_input->p_es_out, ES_OUT_GET_ES_STATE, tk.p_es, &b );
+    if( !b )
     {
+        tk.b_inited = VLC_FALSE;
         return;
     }
 
-    if( ( p_pes = input_NewPES( p_input->p_method_data ) ) == NULL )
-    {
-        msg_Err( p_input, "cannot allocate PES" );
-    }
-
-
-    p_pes->i_pts = i_pts;
-    p_pes->i_dts = i_pts;
-    if( tk.i_cat == SPU_ES )
-    {
-        /* special case : dts mean end of display */
-        if( i_duration > 0 )
-        {
-            /* FIXME not sure about that */
-            p_pes->i_dts += i_duration * 1000;// * (mtime_t) 1000 / p_sys->i_timescale;
-        }
-        else
-        {
-            /* unknown */
-            p_pes->i_dts = 0;
-        }
-    }
-
-    p_pes->p_first = p_pes->p_last = NULL;
-    p_pes->i_nb_data = 0;
-    p_pes->i_pes_size = 0;
-
-    for( i = 0; i < block->NumberFrames(); i++ )
-    {
-        data_packet_t *p_data = NULL;
-        DataBuffer &data = block->GetBuffer(i);
-
-        if( ( p_data = input_NewPacket( p_input->p_method_data, data.Size() ) ) != NULL )
-        {
-            memcpy( p_data->p_payload_start, data.Buffer(), data.Size() );
-
-            p_data->p_payload_end = p_data->p_payload_start + data.Size();
-
-            if( p_pes->p_first == NULL )
-            {
-                p_pes->p_first = p_data;
-            }
-            else
-            {
-                p_pes->p_last->p_next  = p_data;
-            }
-            p_pes->i_nb_data++;
-            p_pes->i_pes_size += data.Size();
-            p_pes->p_last  = p_data;
-        }
-    }
-    if( tk.i_cat == SPU_ES && p_pes->p_first && p_pes->i_pes_size > 0 )
+    if( tk.fmt.i_cat == AUDIO_ES && p_input->stream.control.b_mute )
     {
-        p_pes->p_first->p_payload_end[-1] = '\0';
+        return;
     }
 
+    /* First send init data */
     if( !tk.b_inited && tk.i_data_init > 0 )
     {
-        pes_packet_t *p_init;
+        block_t *p_init;
 
         msg_Dbg( p_input, "sending header (%d bytes)", tk.i_data_init );
 
-        if( tk.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
+        if( tk.fmt.i_codec == VLC_FOURCC( 'v', 'o', 'r', 'b' ) )
         {
             int i;
             int i_offset = 1;
@@ -1695,34 +1459,69 @@ static void BlockDecode( input_thread_t *p_input, KaxBlock *block, mtime_t i_pts
             i_size[1] = __MIN( i_size[1], tk.i_data_init - i_offset - i_size[0] );
             i_size[2] = tk.i_data_init - i_offset - i_size[0] - i_size[1];
 
-            p_init = MemToPES( p_input, &tk.p_data_init[i_offset], i_size[0] );
+            p_init = MemToBlock( p_input, &tk.p_data_init[i_offset], i_size[0] );
             if( p_init )
             {
-                input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
+                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
             }
-            p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
+            p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]], i_size[1] );
             if( p_init )
             {
-                input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
+                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
             }
-            p_init = MemToPES( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
+            p_init = MemToBlock( p_input, &tk.p_data_init[i_offset+i_size[0]+i_size[1]], i_size[2] );
             if( p_init )
             {
-                input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
+                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
             }
         }
         else
         {
-            p_init = MemToPES( p_input, tk.p_data_init, tk.i_data_init );
+            p_init = MemToBlock( p_input, tk.p_data_init, tk.i_data_init );
             if( p_init )
             {
-                input_DecodePES( tk.p_es->p_decoder_fifo, p_init );
+                es_out_Send( p_input->p_es_out, tk.p_es, p_init );
             }
         }
     }
     tk.b_inited = VLC_TRUE;
 
-    input_DecodePES( tk.p_es->p_decoder_fifo, p_pes );
+
+    for( i = 0; i < block->NumberFrames(); i++ )
+    {
+        block_t *p_block;
+        DataBuffer &data = block->GetBuffer(i);
+
+        p_block = MemToBlock( p_input, data.Buffer(), data.Size() );
+        if( p_block == NULL )
+        {
+            break;
+        }
+
+        if( tk.fmt.i_cat != VIDEO_ES )
+            p_block->i_dts = p_block->i_pts = i_pts;
+        else
+        {
+            p_block->i_dts = i_pts;
+            p_block->i_pts = 0;
+        }
+
+        if( tk.fmt.i_cat == SPU_ES && strcmp( tk.psz_codec, "S_VOBSUB" ) )
+        {
+            if( i_duration > 0 )
+            {
+                p_block->i_dts += i_duration * 1000;
+            }
+            else
+            {
+                p_block->i_dts = 0;
+            }
+        }
+        es_out_Send( p_input->p_es_out, tk.p_es, p_block );
+
+        /* use time stamp only for first block */
+        i_pts = 0;
+    }
 
 #undef tk
 }
@@ -1740,43 +1539,99 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
     int         i_track_skipping;
     int         i_track;
 
-    msg_Dbg( p_input, "seek request to %lld (%d%%)", i_date, i_percent );
+    msg_Dbg( p_input, "seek request to "I64Fd" (%d%%)", i_date, i_percent );
+    if( i_date < 0 && i_percent < 0 )
+    {
+        return;
+    }
+
+    delete p_sys->ep;
+    p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
+    p_sys->cluster = NULL;
 
-    for( i_index = 0; i_index < p_sys->i_index; i_index++ )
+    /* seek without index or without date */
+    if( config_GetInt( p_input, "mkv-seek-percent" ) || !p_sys->b_cues || i_date < 0 )
     {
-        if( p_sys->index[i_index].i_time >= i_date )
+        int64_t i_pos = i_percent * stream_Size( p_input->s ) / 100;
+
+        msg_Dbg( p_input, "inacurate way of seeking" );
+        for( i_index = 0; i_index < p_sys->i_index; i_index++ )
         {
-            break;
+            if( p_sys->index[i_index].i_position >= i_pos)
+            {
+                break;
+            }
+        }
+        if( i_index == p_sys->i_index )
+        {
+            i_index--;
         }
-    }
 
-    if( i_index > 0 )
-    {
-        i_index--;
-    }
+        p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
+                                   seek_beginning );
+
+        if( p_sys->index[i_index].i_position < i_pos )
+        {
+            EbmlElement *el;
 
-    msg_Dbg( p_input, "seek got %lld (%d%%)",
-             p_sys->index[i_index].i_time, (int)(100 * p_sys->index[i_index].i_position /p_input->stream.p_selected_area->i_size ) );
+            msg_Warn( p_input, "searching for cluster, could take some time" );
 
-    delete p_sys->ep;
+            /* search a cluster */
+            while( ( el = p_sys->ep->Get() ) != NULL )
+            {
+                if( EbmlId( *el ) == KaxCluster::ClassInfos.GlobalId )
+                {
+                    KaxCluster *cluster = (KaxCluster*)el;
 
-    p_sys->in->setFilePointer( p_sys->index[i_index].i_position, seek_beginning );
+                    /* add it to the index */
+                    IndexAppendCluster( p_input, cluster );
 
-    p_sys->ep = new EbmlParser( p_sys->es, p_sys->segment );
+                    if( (int64_t)cluster->GetElementPosition() >= i_pos )
+                    {
+                        p_sys->cluster = cluster;
+                        p_sys->ep->Down();
+                        break;
+                    }
+                }
+            }
+        }
+    }
+    else
+    {
+        for( i_index = 0; i_index < p_sys->i_index; i_index++ )
+        {
+            if( p_sys->index[i_index].i_time >= i_date )
+            {
+                break;
+            }
+        }
+
+        if( i_index > 0 )
+        {
+            i_index--;
+        }
+
+        msg_Dbg( p_input, "seek got "I64Fd" (%d%%)",
+                 p_sys->index[i_index].i_time,
+                 (int)( 100 * p_sys->index[i_index].i_position /
+                        stream_Size( p_input->s ) ) );
+
+        p_sys->in->setFilePointer( p_sys->index[i_index].i_position,
+                                   seek_beginning );
+    }
 
     /* now parse until key frame */
 #define tk  p_sys->track[i_track]
     i_track_skipping = 0;
     for( i_track = 0; i_track < p_sys->i_track; i_track++ )
     {
-        if( tk.i_cat == VIDEO_ES )
+        if( tk.fmt.i_cat == VIDEO_ES )
         {
             tk.b_search_keyframe = VLC_TRUE;
             i_track_skipping++;
         }
     }
 
-
     while( i_track_skipping > 0 )
     {
         if( BlockGet( p_input, &block, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
@@ -1798,12 +1653,12 @@ static void Seek( input_thread_t *p_input, mtime_t i_date, int i_percent)
 
         if( i_track < p_sys->i_track )
         {
-            if( tk.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
+            if( tk.fmt.i_cat == VIDEO_ES && i_block_ref1 == -1 && tk.b_search_keyframe )
             {
                 tk.b_search_keyframe = VLC_FALSE;
                 i_track_skipping--;
             }
-            if( tk.i_cat == VIDEO_ES && !tk.b_search_keyframe )
+            if( tk.fmt.i_cat == VIDEO_ES && !tk.b_search_keyframe )
             {
                 BlockDecode( p_input, block, 0, 0 );
             }
@@ -1823,6 +1678,8 @@ static int Demux( input_thread_t * p_input )
 {
     demux_sys_t    *p_sys   = p_input->p_demux_data;
     mtime_t        i_start_pts;
+    int            i_block_count = 0;
+
     KaxBlock *block;
     int64_t i_block_duration;
     int64_t i_block_ref1;
@@ -1831,21 +1688,26 @@ static int Demux( input_thread_t * p_input )
     if( p_input->stream.p_selected_program->i_synchro_state == SYNCHRO_REINIT )
     {
         mtime_t i_duration = (mtime_t)( p_sys->f_duration / 1000 );
-        mtime_t i_date;
-        int i_percent;
+        mtime_t i_date = -1;
+        int i_percent  = -1;
 
-        i_date = (mtime_t)1000000 *
-                 (mtime_t)i_duration*
-                 (mtime_t)p_sys->in->getFilePointer() /
-                 (mtime_t)p_input->stream.p_selected_area->i_size;
-
-        i_percent = 100 * p_sys->in->getFilePointer() /
-                        p_input->stream.p_selected_area->i_size;
+        if( i_duration > 0 )
+        {
+            i_date = (mtime_t)1000000 *
+                     (mtime_t)i_duration*
+                     (mtime_t)p_sys->in->getFilePointer() /
+                     (mtime_t)stream_Size( p_input->s );
+        }
+        if( stream_Size( p_input->s ) > 0 )
+        {
+            i_percent = 100 * p_sys->in->getFilePointer() /
+                        stream_Size( p_input->s );
+        }
 
         Seek( p_input, i_date, i_percent);
     }
 
-    i_start_pts = p_sys->i_pts;
+    i_start_pts = -1;
 
     for( ;; )
     {
@@ -1871,15 +1733,18 @@ static int Demux( input_thread_t * p_input )
                                   p_input->stream.p_selected_program,
                                   p_sys->i_pts * 9 / 100 );
 
+
+
         BlockDecode( p_input, block, i_pts, i_block_duration );
 
         delete block;
+        i_block_count++;
 
         if( i_start_pts == -1 )
         {
             i_start_pts = p_sys->i_pts;
         }
-        else if( p_sys->i_pts > i_start_pts + (mtime_t)100000)
+        else if( p_sys->i_pts > i_start_pts + (mtime_t)100000 || i_block_count > 5 )
         {
             return 1;
         }
@@ -1887,3 +1752,594 @@ static int Demux( input_thread_t * p_input )
 }
 
 
+
+/*****************************************************************************
+ * Stream managment
+ *****************************************************************************/
+vlc_stream_io_callback::vlc_stream_io_callback( stream_t *s_ )
+{
+    s = s_;
+    mb_eof = VLC_FALSE;
+}
+
+uint32_t vlc_stream_io_callback::read( void *p_buffer, size_t i_size )
+{
+    if( i_size <= 0 || mb_eof )
+    {
+        return 0;
+    }
+
+    return stream_Read( s, p_buffer, i_size );
+}
+void vlc_stream_io_callback::setFilePointer(int64_t i_offset, seek_mode mode )
+{
+    int64_t i_pos;
+
+    switch( mode )
+    {
+        case seek_beginning:
+            i_pos = i_offset;
+            break;
+        case seek_end:
+            i_pos = stream_Size( s ) - i_offset;
+            break;
+        default:
+            i_pos= stream_Tell( s ) + i_offset;
+            break;
+    }
+
+    if( i_pos < 0 || i_pos >= stream_Size( s ) )
+    {
+        mb_eof = VLC_TRUE;
+        return;
+    }
+
+    mb_eof = VLC_FALSE;
+    if( stream_Seek( s, i_pos ) )
+    {
+        mb_eof = VLC_TRUE;
+    }
+    return;
+}
+size_t vlc_stream_io_callback::write( const void *p_buffer, size_t i_size )
+{
+    return 0;
+}
+uint64_t vlc_stream_io_callback::getFilePointer( void )
+{
+    return stream_Tell( s );
+}
+void vlc_stream_io_callback::close( void )
+{
+    return;
+}
+
+
+/*****************************************************************************
+ * Ebml Stream parser
+ *****************************************************************************/
+EbmlParser::EbmlParser( EbmlStream *es, EbmlElement *el_start )
+{
+    int i;
+
+    m_es = es;
+    m_got = NULL;
+    m_el[0] = el_start;
+
+    for( i = 1; i < 6; i++ )
+    {
+        m_el[i] = NULL;
+    }
+    mi_level = 1;
+    mi_user_level = 1;
+    mb_keep = VLC_FALSE;
+}
+
+EbmlParser::~EbmlParser( void )
+{
+    int i;
+
+    for( i = 1; i < mi_level; i++ )
+    {
+        if( !mb_keep )
+        {
+            delete m_el[i];
+        }
+        mb_keep = VLC_FALSE;
+    }
+}
+
+void EbmlParser::Up( void )
+{
+    if( mi_user_level == mi_level )
+    {
+        fprintf( stderr," arrrrrrrrrrrrrg Up cannot escape itself\n" );
+    }
+
+    mi_user_level--;
+}
+
+void EbmlParser::Down( void )
+{
+    mi_user_level++;
+    mi_level++;
+}
+
+void EbmlParser::Keep( void )
+{
+    mb_keep = VLC_TRUE;
+}
+
+int EbmlParser::GetLevel( void )
+{
+    return mi_user_level;
+}
+
+EbmlElement *EbmlParser::Get( void )
+{
+    int i_ulev = 0;
+
+    if( mi_user_level != mi_level )
+    {
+        return NULL;
+    }
+    if( m_got )
+    {
+        EbmlElement *ret = m_got;
+        m_got = NULL;
+
+        return ret;
+    }
+
+    if( m_el[mi_level] )
+    {
+        m_el[mi_level]->SkipData( *m_es, m_el[mi_level]->Generic().Context );
+        if( !mb_keep )
+        {
+            delete m_el[mi_level];
+        }
+        mb_keep = VLC_FALSE;
+    }
+
+    m_el[mi_level] = m_es->FindNextElement( m_el[mi_level - 1]->Generic().Context, i_ulev, 0xFFFFFFFFL, true, 1 );
+    if( i_ulev > 0 )
+    {
+        while( i_ulev > 0 )
+        {
+            if( mi_level == 1 )
+            {
+                mi_level = 0;
+                return NULL;
+            }
+
+            delete m_el[mi_level - 1];
+            m_got = m_el[mi_level -1] = m_el[mi_level];
+            m_el[mi_level] = NULL;
+
+            mi_level--;
+            i_ulev--;
+        }
+        return NULL;
+    }
+    else if( m_el[mi_level] == NULL )
+    {
+        fprintf( stderr," m_el[mi_level] == NULL\n" );
+    }
+
+    return m_el[mi_level];
+}
+
+
+/*****************************************************************************
+ * Tools
+ *  * LoadCues : load the cues element and update index
+ *
+ *  * LoadTags : load ... the tags element
+ *
+ *  * InformationsCreate : create all informations, load tags if present
+ *
+ *****************************************************************************/
+static void LoadCues( input_thread_t *p_input )
+{
+    demux_sys_t *p_sys = p_input->p_demux_data;
+    int64_t     i_sav_position = p_sys->in->getFilePointer();
+    EbmlParser  *ep;
+    EbmlElement *el, *cues;
+
+    msg_Dbg( p_input, "loading cues" );
+    p_sys->in->setFilePointer( p_sys->i_cues_position, seek_beginning );
+    cues = p_sys->es->FindNextID( KaxCues::ClassInfos, 0xFFFFFFFFL);
+
+    if( cues == NULL )
+    {
+        msg_Err( p_input, "cannot load cues (broken seekhead or file)" );
+        return;
+    }
+
+    ep = new EbmlParser( p_sys->es, cues );
+    while( ( el = ep->Get() ) != NULL )
+    {
+        if( EbmlId( *el ) == KaxCuePoint::ClassInfos.GlobalId )
+        {
+#define idx p_sys->index[p_sys->i_index]
+
+            idx.i_track       = -1;
+            idx.i_block_number= -1;
+            idx.i_position    = -1;
+            idx.i_time        = -1;
+            idx.b_key         = VLC_TRUE;
+
+            ep->Down();
+            while( ( el = ep->Get() ) != NULL )
+            {
+                if( EbmlId( *el ) == KaxCueTime::ClassInfos.GlobalId )
+                {
+                    KaxCueTime &ctime = *(KaxCueTime*)el;
+
+                    ctime.ReadData( p_sys->es->I_O() );
+
+                    idx.i_time = uint64( ctime ) * (mtime_t)1000000000 / p_sys->i_timescale;
+                }
+                else if( EbmlId( *el ) == KaxCueTrackPositions::ClassInfos.GlobalId )
+                {
+                    ep->Down();
+                    while( ( el = ep->Get() ) != NULL )
+                    {
+                        if( EbmlId( *el ) == KaxCueTrack::ClassInfos.GlobalId )
+                        {
+                            KaxCueTrack &ctrack = *(KaxCueTrack*)el;
+
+                            ctrack.ReadData( p_sys->es->I_O() );
+                            idx.i_track = uint16( ctrack );
+                        }
+                        else if( EbmlId( *el ) == KaxCueClusterPosition::ClassInfos.GlobalId )
+                        {
+                            KaxCueClusterPosition &ccpos = *(KaxCueClusterPosition*)el;
+
+                            ccpos.ReadData( p_sys->es->I_O() );
+                            idx.i_position = p_sys->segment->GetGlobalPosition( uint64( ccpos ) );
+                        }
+                        else if( EbmlId( *el ) == KaxCueBlockNumber::ClassInfos.GlobalId )
+                        {
+                            KaxCueBlockNumber &cbnum = *(KaxCueBlockNumber*)el;
+
+                            cbnum.ReadData( p_sys->es->I_O() );
+                            idx.i_block_number = uint32( cbnum );
+                        }
+                        else
+                        {
+                            msg_Dbg( p_input, "         * Unknown (%s)", typeid(*el).name() );
+                        }
+                    }
+                    ep->Up();
+                }
+                else
+                {
+                    msg_Dbg( p_input, "     * Unknown (%s)", typeid(*el).name() );
+                }
+            }
+            ep->Up();
+
+#if 0
+            msg_Dbg( p_input, " * added time="I64Fd" pos="I64Fd
+                     " track=%d bnum=%d", idx.i_time, idx.i_position,
+                     idx.i_track, idx.i_block_number );
+#endif
+
+            p_sys->i_index++;
+            if( p_sys->i_index >= p_sys->i_index_max )
+            {
+                p_sys->i_index_max += 1024;
+                p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
+            }
+#undef idx
+        }
+        else
+        {
+            msg_Dbg( p_input, " * Unknown (%s)", typeid(*el).name() );
+        }
+    }
+    delete ep;
+    delete cues;
+
+    p_sys->b_cues = VLC_TRUE;
+
+    msg_Dbg( p_input, "loading cues done." );
+    p_sys->in->setFilePointer( i_sav_position, seek_beginning );
+}
+
+static void LoadTags( input_thread_t *p_input )
+{
+    demux_sys_t *p_sys = p_input->p_demux_data;
+    int64_t     i_sav_position = p_sys->in->getFilePointer();
+    EbmlParser  *ep;
+    EbmlElement *el, *tags;
+
+    msg_Dbg( p_input, "loading tags" );
+    p_sys->in->setFilePointer( p_sys->i_tags_position, seek_beginning );
+    tags = p_sys->es->FindNextID( KaxTags::ClassInfos, 0xFFFFFFFFL);
+
+    if( tags == NULL )
+    {
+        msg_Err( p_input, "cannot load tags (broken seekhead or file)" );
+        return;
+    }
+
+    msg_Dbg( p_input, "Tags" );
+    ep = new EbmlParser( p_sys->es, tags );
+    while( ( el = ep->Get() ) != NULL )
+    {
+        if( EbmlId( *el ) == KaxTag::ClassInfos.GlobalId )
+        {
+            msg_Dbg( p_input, "+ Tag" );
+            ep->Down();
+            while( ( el = ep->Get() ) != NULL )
+            {
+                if( EbmlId( *el ) == KaxTagTargets::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Targets" );
+                    ep->Down();
+                    while( ( el = ep->Get() ) != NULL )
+                    {
+                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
+                    }
+                    ep->Up();
+                }
+                else if( EbmlId( *el ) == KaxTagGeneral::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + General" );
+                    ep->Down();
+                    while( ( el = ep->Get() ) != NULL )
+                    {
+                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
+                    }
+                    ep->Up();
+                }
+                else if( EbmlId( *el ) == KaxTagGenres::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Genres" );
+                    ep->Down();
+                    while( ( el = ep->Get() ) != NULL )
+                    {
+                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
+                    }
+                    ep->Up();
+                }
+                else if( EbmlId( *el ) == KaxTagAudioSpecific::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Audio Specific" );
+                    ep->Down();
+                    while( ( el = ep->Get() ) != NULL )
+                    {
+                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
+                    }
+                    ep->Up();
+                }
+                else if( EbmlId( *el ) == KaxTagImageSpecific::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Images Specific" );
+                    ep->Down();
+                    while( ( el = ep->Get() ) != NULL )
+                    {
+                        msg_Dbg( p_input, "|   |   + Unknown (%s)", typeid( *el ).name() );
+                    }
+                    ep->Up();
+                }
+                else if( EbmlId( *el ) == KaxTagMultiComment::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Multi Comment" );
+                }
+                else if( EbmlId( *el ) == KaxTagMultiCommercial::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Multi Commercial" );
+                }
+                else if( EbmlId( *el ) == KaxTagMultiDate::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Multi Date" );
+                }
+                else if( EbmlId( *el ) == KaxTagMultiEntity::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Multi Entity" );
+                }
+                else if( EbmlId( *el ) == KaxTagMultiIdentifier::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Multi Identifier" );
+                }
+                else if( EbmlId( *el ) == KaxTagMultiLegal::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Multi Legal" );
+                }
+                else if( EbmlId( *el ) == KaxTagMultiTitle::ClassInfos.GlobalId )
+                {
+                    msg_Dbg( p_input, "|   + Multi Title" );
+                }
+                else
+                {
+                    msg_Dbg( p_input, "|   + Unknown (%s)", typeid( *el ).name() );
+                }
+            }
+            ep->Up();
+        }
+        else
+        {
+            msg_Dbg( p_input, "+ Unknown (%s)", typeid( *el ).name() );
+        }
+    }
+    delete ep;
+    delete tags;
+
+    msg_Dbg( p_input, "loading tags done." );
+    p_sys->in->setFilePointer( i_sav_position, seek_beginning );
+}
+
+static void InformationsCreate( input_thread_t *p_input )
+{
+    demux_sys_t           *p_sys = p_input->p_demux_data;
+    input_info_category_t *p_cat;
+    playlist_t            *p_playlist;
+    int                   i_track;
+
+    p_cat = input_InfoCategory( p_input, _("Matroska") );
+    if( p_sys->f_duration > 1000.1 )
+    {
+        char psz_buffer[MSTRTIME_MAX_SIZE];
+        input_AddInfo( p_cat, _("Duration"),
+                       msecstotimestr( psz_buffer, (int)p_sys->f_duration ) );
+    }
+
+    if( p_sys->psz_title )
+    {
+        input_AddInfo( p_cat, _("Title"), "%s" ,p_sys->psz_title );
+    }
+    if( p_sys->psz_date_utc )
+    {
+        input_AddInfo( p_cat, _("UTC date"), "%s" ,p_sys->psz_date_utc );
+    }
+    if( p_sys->psz_segment_filename )
+    {
+        input_AddInfo( p_cat, _("Segment filename"), "%s" ,p_sys->psz_segment_filename );
+    }
+    if( p_sys->psz_muxing_application )
+    {
+        input_AddInfo( p_cat, _("Muxing application"), "%s" ,p_sys->psz_muxing_application );
+    }
+    if( p_sys->psz_writing_application )
+    {
+        input_AddInfo( p_cat, _("Writing application"), "%s" ,p_sys->psz_writing_application );
+    }
+    input_AddInfo( p_cat, _("Number of streams"), "%d" , p_sys->i_track );
+
+    for( i_track = 0; i_track < p_sys->i_track; i_track++ )
+    {
+        char psz_cat[strlen( "Stream " ) + 10];
+#define tk  p_sys->track[i_track]
+
+        sprintf( psz_cat, "Stream %d", i_track );
+        p_cat = input_InfoCategory( p_input, psz_cat);
+        if( tk.fmt.psz_description )
+        {
+            input_AddInfo( p_cat, _("Name"), "%s", tk.fmt.psz_description );
+        }
+        if( tk.fmt.psz_language )
+        {
+            input_AddInfo( p_cat, _("Language"), "%s", tk.fmt.psz_language );
+        }
+        if( tk.psz_codec_name )
+        {
+            input_AddInfo( p_cat, _("Codec name"), "%s", tk.psz_codec_name );
+        }
+        if( tk.psz_codec_settings )
+        {
+            input_AddInfo( p_cat, _("Codec setting"), "%s", tk.psz_codec_settings );
+        }
+        if( tk.psz_codec_info_url )
+        {
+            input_AddInfo( p_cat, _("Codec info"), "%s", tk.psz_codec_info_url );
+        }
+        if( tk.psz_codec_download_url )
+        {
+            input_AddInfo( p_cat, _("Codec download"), "%s", tk.psz_codec_download_url );
+        }
+#undef  tk
+    }
+
+    if( p_sys->i_tags_position >= 0 )
+    {
+        vlc_bool_t b_seekable;
+
+        stream_Control( p_input->s, STREAM_CAN_FASTSEEK, &b_seekable );
+        if( b_seekable )
+        {
+            LoadTags( p_input );
+        }
+    }
+}
+
+
+/*****************************************************************************
+ * Divers
+ *****************************************************************************/
+
+static void IndexAppendCluster( input_thread_t *p_input, KaxCluster *cluster )
+{
+    demux_sys_t    *p_sys   = p_input->p_demux_data;
+
+#define idx p_sys->index[p_sys->i_index]
+    idx.i_track       = -1;
+    idx.i_block_number= -1;
+    idx.i_position    = cluster->GetElementPosition();
+    idx.i_time        = -1;
+    idx.b_key         = VLC_TRUE;
+
+    p_sys->i_index++;
+    if( p_sys->i_index >= p_sys->i_index_max )
+    {
+        p_sys->i_index_max += 1024;
+        p_sys->index = (mkv_index_t*)realloc( p_sys->index, sizeof( mkv_index_t ) * p_sys->i_index_max );
+    }
+#undef idx
+}
+
+static char * UTF8ToStr( const UTFstring &u )
+{
+    int     i_src;
+    const wchar_t *src;
+    char *dst, *p;
+
+    i_src = u.length();
+    src   = u.c_str();
+
+    p = dst = (char*)malloc( i_src + 1);
+    while( i_src > 0 )
+    {
+        if( *src < 255 )
+        {
+            *p++ = (char)*src;
+        }
+        else
+        {
+            *p++ = '?';
+        }
+        src++;
+        i_src--;
+    }
+    *p++= '\0';
+
+    return dst;
+}
+
+static char *LanguageGetName    ( const char *psz_code )
+{
+    const iso639_lang_t *pl;
+
+    if( strlen( psz_code ) == 2 )
+    {
+        pl = GetLang_1( psz_code );
+    }
+    else if( strlen( psz_code ) == 3 )
+    {
+        pl = GetLang_2B( psz_code );
+        if( !strcmp( pl->psz_iso639_1, "??" ) )
+        {
+            pl = GetLang_2T( psz_code );
+        }
+    }
+    else
+    {
+        return strdup( psz_code );
+    }
+
+    if( !strcmp( pl->psz_iso639_1, "??" ) )
+    {
+       return strdup( psz_code );
+    }
+    else
+    {
+        if( *pl->psz_native_name )
+        {
+            return strdup( pl->psz_native_name );
+        }
+        return strdup( pl->psz_eng_name );
+    }
+}
+