]> git.sesse.net Git - vlc/blobdiff - modules/demux/mkv/matroska_segment_parse.cpp
MKV: fix playback of AC-3 with bogus default duration
[vlc] / modules / demux / mkv / matroska_segment_parse.cpp
index 5ea638e59479675bc4fdc58267195570c232392a..d0bd4edfd04fee490418037aa20fb889a3d97f64 100644 (file)
@@ -1,32 +1,57 @@
 /*****************************************************************************
- * mkv.cpp : matroska demuxer
+ * matroska_segment_parse.cpp : matroska demuxer
  *****************************************************************************
- * Copyright (C) 2003-2004 the VideoLAN team
+ * Copyright (C) 2003-2010 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Steve Lhomme <steve.lhomme@free.fr>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
-
+#include "mkv.hpp"
 #include "matroska_segment.hpp"
-
 #include "chapters.hpp"
-
 #include "demux.hpp"
+#include "Ebml_parser.hpp"
+#include "util.hpp"
+
+extern "C" {
+#include "../vobsub.h"
+#include "../xiph.h"
+#include "../windows_audio_commons.h"
+#include "../mp4/libmp4.h"
+}
+
+#include <vlc_codecs.h>
+
+/* GetFourCC helper */
+#define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
+static vlc_fourcc_t __GetFOURCC( uint8_t *p )
+{
+    return VLC_FOURCC( p[0], p[1], p[2], p[3] );
+}
+
+static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
+{
+    if(p_tk->i_extra_data <= offset) return;
+    p_tk->fmt.i_extra = p_tk->i_extra_data - offset;
+    p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
+    if(!p_tk->fmt.p_extra) { p_tk->fmt.i_extra = 0; return; };
+    memcpy( p_tk->fmt.p_extra, p_tk->p_extra_data + offset, p_tk->fmt.i_extra );
+}
 
 /*****************************************************************************
  * Some functions to manipulate memory
@@ -57,72 +82,88 @@ void matroska_segment_c::ParseSeekHead( KaxSeekHead *seekhead )
     {
         if( MKV_IS_ID( l, KaxSeek ) )
         {
-            EbmlId id = EbmlVoid::ClassInfos.GlobalId;
+            EbmlId id = EBML_ID(EbmlVoid);
             int64_t i_pos = -1;
 
+#ifdef MKV_DEBUG
             msg_Dbg( &sys.demuxer, "|   |   + Seek" );
+#endif
             ep->Down();
-            while( ( l = ep->Get() ) != NULL )
+            try
             {
-                if( MKV_IS_ID( l, KaxSeekID ) )
-                {
-                    KaxSeekID &sid = *(KaxSeekID*)l;
-                    sid.ReadData( es.I_O() );
-                    id = EbmlId( sid.GetBuffer(), sid.GetSize() );
-                }
-                else if( MKV_IS_ID( l, KaxSeekPosition ) )
-                {
-                    KaxSeekPosition &spos = *(KaxSeekPosition*)l;
-                    spos.ReadData( es.I_O() );
-                    i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
-                }
-                else
+                while( ( l = ep->Get() ) != NULL )
                 {
-                    /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
-                    msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name()  );
+                    if( unlikely( !l->ValidateSize() ) )
+                    {
+                        msg_Err( &sys.demuxer,"%s too big... skipping it",  typeid(*l).name() );
+                        continue;
+                    }
+                    if( MKV_IS_ID( l, KaxSeekID ) )
+                    {
+                        KaxSeekID &sid = *(KaxSeekID*)l;
+                        sid.ReadData( es.I_O() );
+                        id = EbmlId( sid.GetBuffer(), sid.GetSize() );
+                    }
+                    else if( MKV_IS_ID( l, KaxSeekPosition ) )
+                    {
+                        KaxSeekPosition &spos = *(KaxSeekPosition*)l;
+                        spos.ReadData( es.I_O() );
+                        i_pos = (int64_t)segment->GetGlobalPosition( uint64( spos ) );
+                    }
+                    else
+                    {
+                        /* Many mkvmerge files hit this case. It seems to be a broken SeekHead */
+                        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
+                    }
                 }
             }
+            catch(...)
+            {
+                msg_Err( &sys.demuxer,"Error while reading %s",  typeid(*l).name() );
+            }
             ep->Up();
 
             if( i_pos >= 0 )
             {
-                if( id == KaxCues::ClassInfos.GlobalId )
+                if( id == EBML_ID(KaxCues) )
                 {
-                    msg_Dbg( &sys.demuxer, "|   - cues at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxCues::ClassInfos, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - cues at %" PRId64, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxCues), i_pos );
                 }
-                else if( id == KaxInfo::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxInfo) )
                 {
-                    msg_Dbg( &sys.demuxer, "|   - info at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxInfo::ClassInfos, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - info at %" PRId64, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxInfo), i_pos );
                 }
-                else if( id == KaxChapters::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxChapters) )
                 {
-                    msg_Dbg( &sys.demuxer, "|   - chapters at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxChapters::ClassInfos, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - chapters at %" PRId64, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxChapters), i_pos );
                 }
-                else if( id == KaxTags::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxTags) )
                 {
-                    msg_Dbg( &sys.demuxer, "|   - tags at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxTags::ClassInfos, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - tags at %" PRId64, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxTags), i_pos );
                 }
-                else if( id == KaxSeekHead::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxSeekHead) )
                 {
-                    msg_Dbg( &sys.demuxer, "|   - chained seekhead at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxSeekHead::ClassInfos, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - chained seekhead at %" PRId64, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxSeekHead), i_pos );
                 }
-                else if( id == KaxTracks::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxTracks) )
                 {
-                    msg_Dbg( &sys.demuxer, "|   - tracks at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxTracks::ClassInfos, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - tracks at %" PRId64, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxTracks), i_pos );
                 }
-                else if( id == KaxAttachments::ClassInfos.GlobalId )
+                else if( id == EBML_ID(KaxAttachments) )
                 {
-                    msg_Dbg( &sys.demuxer, "|   - attachments at %"PRId64, i_pos );
-                    LoadSeekHeadItem( KaxAttachments::ClassInfos, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - attachments at %" PRId64, i_pos );
+                    LoadSeekHeadItem( EBML_INFO(KaxAttachments), i_pos );
                 }
+#ifdef MKV_DEBUG
                 else
-                    msg_Dbg( &sys.demuxer, "|   - unknown seekhead reference at %"PRId64, i_pos );
+                    msg_Dbg( &sys.demuxer, "|   - unknown seekhead reference at %" PRId64, i_pos );
+#endif
             }
         }
         else
@@ -140,7 +181,7 @@ static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ...
     va_list args;
     if( i_level > 9 )
     {
-        msg_Err( &demuxer, "too deep tree" );
+        msg_Err( &demuxer, "MKV tree is too deep" );
         return;
     }
     va_start( args, psz_format );
@@ -150,7 +191,7 @@ static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ...
     psz_foo2[ 4 * i_level ] = '+';
     psz_foo2[ 4 * i_level + 1 ] = ' ';
     strcpy( &psz_foo2[ 4 * i_level + 2 ], psz_format );
-    __msg_GenericVa( VLC_OBJECT(&demuxer),VLC_MSG_DBG, "mkv", psz_foo2, args );
+    msg_GenericVa( &demuxer,VLC_MSG_DBG, psz_foo2, args );
     free( psz_foo2 );
     va_end( args );
 }
@@ -161,46 +202,46 @@ static void MkvTree( demux_t & demuxer, int i_level, const char *psz_format, ...
  *****************************************************************************/
 void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 {
-    size_t i, j, k, n;
     bool bSupported = true;
 
-    mkv_track_t *tk;
-
-    msg_Dbg( &sys.demuxer, "|   |   + Track Entry" );
-
-    tk = new mkv_track_t();
-
     /* Init the track */
+    mkv_track_t *tk = new mkv_track_t();
     memset( tk, 0, sizeof( mkv_track_t ) );
 
     es_format_Init( &tk->fmt, UNKNOWN_ES, 0 );
-    tk->fmt.psz_language = strdup("English");
-    tk->fmt.psz_description = NULL;
-
-    tk->b_default = true;
-    tk->b_enabled = true;
-    tk->b_silent = false;
-    tk->i_number = tracks.size() - 1;
-    tk->i_extra_data = 0;
-    tk->p_extra_data = NULL;
-    tk->psz_codec = NULL;
-    tk->b_dts_only = false;
-    tk->i_default_duration = 0;
-    tk->f_timecodescale = 1.0;
-
-    tk->b_inited = 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->p_es = NULL;
+    tk->fmt.psz_language       = strdup("English");
+    tk->fmt.psz_description    = NULL;
+
+    tk->b_default              = true;
+    tk->b_enabled              = true;
+    tk->b_forced               = false;
+    tk->b_silent               = false;
+    tk->i_number               = tracks.size() - 1;
+    tk->i_extra_data           = 0;
+    tk->p_extra_data           = NULL;
+    tk->psz_codec              = NULL;
+    tk->b_dts_only             = false;
+    tk->i_default_duration     = 0;
+    tk->b_no_duration          = false;
+    tk->f_timecodescale        = 1.0;
+
+    tk->b_inited               = 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;
-    tk->i_compression_type = MATROSKA_COMPRESSION_NONE;
-    tk->p_compression_data = NULL;
 
-    for( i = 0; i < m->ListSize(); i++ )
+    tk->i_compression_type     = MATROSKA_COMPRESSION_NONE;
+    tk->i_encoding_scope       = MATROSKA_ENCODING_SCOPE_ALL_FRAMES;
+    tk->p_compression_data     = NULL;
+
+    msg_Dbg( &sys.demuxer, "|   |   + Track Entry" );
+
+    for( size_t i = 0; i < m->ListSize(); i++ )
     {
         EbmlElement *l = (*m)[i];
 
@@ -227,6 +268,8 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                 case track_audio:
                     psz_type = "audio";
                     tk->fmt.i_cat = AUDIO_ES;
+                    tk->fmt.audio.i_channels = 1;
+                    tk->fmt.audio.i_rate = 8000;
                     break;
                 case track_video:
                     psz_type = "video";
@@ -248,34 +291,40 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Type=%s", psz_type );
         }
-//        else  if( EbmlId( *l ) == KaxTrackFlagEnabled::ClassInfos.GlobalId )
-//        {
-//            KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
+        else  if( MKV_IS_ID( l, KaxTrackFlagEnabled ) ) // UNUSED
+        {
+            KaxTrackFlagEnabled &fenb = *(KaxTrackFlagEnabled*)l;
 
-//            tk->b_enabled = uint32( fenb );
-//            msg_Dbg( &sys.demuxer, "|   |   |   + Track Enabled=%u",
-//                     uint32( fenb )  );
-//        }
+            tk->b_enabled = uint32( fenb );
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Enabled=%u", uint32( fenb ) );
+        }
         else  if( MKV_IS_ID( l, KaxTrackFlagDefault ) )
         {
             KaxTrackFlagDefault &fdef = *(KaxTrackFlagDefault*)l;
 
             tk->b_default = uint32( fdef );
-            msg_Dbg( &sys.demuxer, "|   |   |   + Track Default=%u", uint32( fdef )  );
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Default=%u", uint32( fdef ) );
+        }
+        else  if( MKV_IS_ID( l, KaxTrackFlagForced ) ) // UNUSED
+        {
+            KaxTrackFlagForced &ffor = *(KaxTrackFlagForced*)l;
+            tk->b_forced = uint32( ffor );
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Forced=%u", uint32( ffor ) );
         }
-        else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) )
+        else  if( MKV_IS_ID( l, KaxTrackFlagLacing ) ) // UNUSED
         {
             KaxTrackFlagLacing &lac = *(KaxTrackFlagLacing*)l;
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Lacing=%d", uint32( lac ) );
         }
-        else  if( MKV_IS_ID( l, KaxTrackMinCache ) )
+        else  if( MKV_IS_ID( l, KaxTrackMinCache ) ) // UNUSED
         {
             KaxTrackMinCache &cmin = *(KaxTrackMinCache*)l;
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track MinCache=%d", uint32( cmin ) );
         }
-        else  if( MKV_IS_ID( l, KaxTrackMaxCache ) )
+        else  if( MKV_IS_ID( l, KaxTrackMaxCache ) ) // UNUSED
         {
             KaxTrackMaxCache &cmax = *(KaxTrackMaxCache*)l;
 
@@ -286,15 +335,22 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
             KaxTrackDefaultDuration &defd = *(KaxTrackDefaultDuration*)l;
 
             tk->i_default_duration = uint64(defd);
-            msg_Dbg( &sys.demuxer, "|   |   |   + Track Default Duration=%"PRId64, uint64(defd) );
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Default Duration=%" PRId64, uint64(defd) );
         }
         else  if( MKV_IS_ID( l, KaxTrackTimecodeScale ) )
         {
             KaxTrackTimecodeScale &ttcs = *(KaxTrackTimecodeScale*)l;
 
             tk->f_timecodescale = float( ttcs );
+            if ( tk->f_timecodescale <= 0 ) tk->f_timecodescale = 1.0;
             msg_Dbg( &sys.demuxer, "|   |   |   + Track TimeCodeScale=%f", tk->f_timecodescale );
         }
+        else  if( MKV_IS_ID( l, KaxMaxBlockAdditionID ) ) // UNUSED
+        {
+            KaxMaxBlockAdditionID &mbl = *(KaxMaxBlockAdditionID*)l;
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Max BlockAdditionID=%d", uint32( mbl ) );
+        }
         else if( MKV_IS_ID( l, KaxTrackName ) )
         {
             KaxTrackName &tname = *(KaxTrackName*)l;
@@ -328,7 +384,7 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                 tk->p_extra_data = (uint8_t*)malloc( tk->i_extra_data );
                 memcpy( tk->p_extra_data, cpriv.GetBuffer(), tk->i_extra_data );
             }
-            msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecPrivate size=%"PRId64, cpriv.GetSize() );
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track CodecPrivate size=%" PRId64, cpriv.GetSize() );
         }
         else if( MKV_IS_ID( l, KaxCodecName ) )
         {
@@ -337,6 +393,34 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
             tk->psz_codec_name = ToUTF8( UTFstring( cname ) );
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Name=%s", tk->psz_codec_name );
         }
+        //AttachmentLink
+        else if( MKV_IS_ID( l, KaxCodecDecodeAll ) ) // UNUSED
+        {
+            KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Decode All=%u", uint8( cdall ) );
+        }
+        else if( MKV_IS_ID( l, KaxTrackOverlay ) ) // UNUSED
+        {
+            KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
+
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Overlay=%u", uint32( tovr ) );
+        }
+#if LIBMATROSKA_VERSION >= 0x010401
+        else if( MKV_IS_ID( l, KaxCodecDelay ) )
+        {
+            KaxCodecDelay &codecdelay = *(KaxCodecDelay*)l;
+            tk->i_codec_delay = uint64_t( codecdelay ) / 1000;
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Delay =%" PRIu64,
+                     tk->i_codec_delay );
+        }
+        else if( MKV_IS_ID( l, KaxSeekPreRoll ) )
+        {
+            KaxSeekPreRoll &spr = *(KaxSeekPreRoll*)l;
+            tk->i_seek_preroll = uint64_t(spr) / 1000;
+            msg_Dbg( &sys.demuxer, "|   |   |   + Track Seek Preroll =%" PRIu64, tk->i_seek_preroll );
+        }
+#endif
         else if( MKV_IS_ID( l, KaxContentEncodings ) )
         {
             EbmlMaster *cencs = static_cast<EbmlMaster*>(l);
@@ -346,14 +430,14 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                 msg_Err( &sys.demuxer, "Multiple Compression method not supported" );
                 bSupported = false;
             }
-            for( j = 0; j < cencs->ListSize(); j++ )
+            for( size_t j = 0; j < cencs->ListSize(); j++ )
             {
                 EbmlElement *l2 = (*cencs)[j];
                 if( MKV_IS_ID( l2, KaxContentEncoding ) )
                 {
                     MkvTree( sys.demuxer, 4, "Content Encoding" );
                     EbmlMaster *cenc = static_cast<EbmlMaster*>(l2);
-                    for( k = 0; k < cenc->ListSize(); k++ )
+                    for( size_t k = 0; k < cenc->ListSize(); k++ )
                     {
                         EbmlElement *l3 = (*cenc)[k];
                         if( MKV_IS_ID( l3, KaxContentEncodingOrder ) )
@@ -364,6 +448,7 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                         else if( MKV_IS_ID( l3, KaxContentEncodingScope ) )
                         {
                             KaxContentEncodingScope &encscope = *(KaxContentEncodingScope*)l3;
+                            tk->i_encoding_scope = uint32( encscope );
                             MkvTree( sys.demuxer, 5, "Scope: %i", uint32( encscope ) );
                         }
                         else if( MKV_IS_ID( l3, KaxContentEncodingType ) )
@@ -375,7 +460,9 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                         {
                             EbmlMaster *compr = static_cast<EbmlMaster*>(l3);
                             MkvTree( sys.demuxer, 5, "Content Compression" );
-                            for( n = 0; n < compr->ListSize(); n++ )
+                            //Default compression type is 0 (Zlib)
+                            tk->i_compression_type = MATROSKA_COMPRESSION_ZLIB;
+                            for( size_t n = 0; n < compr->ListSize(); n++ )
                             {
                                 EbmlElement *l4 = (*compr)[n];
                                 if( MKV_IS_ID( l4, KaxContentCompAlgo ) )
@@ -400,6 +487,7 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                                 }
                             }
                         }
+                        // ContentEncryption Unsupported
                         else
                         {
                             MkvTree( sys.demuxer, 5, "Unknown (%s)", typeid(*l3).name() );
@@ -412,43 +500,30 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                 }
             }
         }
-//        else if( EbmlId( *l ) == KaxCodecSettings::ClassInfos.GlobalId )
+//        else if( MKV_IS_ID( l, KaxCodecSettings) ) DEPRECATED by matroska
 //        {
 //            KaxCodecSettings &cset = *(KaxCodecSettings*)l;
 
 //            tk->psz_codec_settings = ToUTF8( UTFstring( cset ) );
 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Settings=%s", tk->psz_codec_settings );
 //        }
-//        else if( EbmlId( *l ) == KaxCodecInfoURL::ClassInfos.GlobalId )
+//        else if( MKV_IS_ID( l, KaxCodecInfoURL) ) DEPRECATED by matroska
 //        {
 //            KaxCodecInfoURL &ciurl = *(KaxCodecInfoURL*)l;
 
 //            tk->psz_codec_info_url = strdup( string( ciurl ).c_str() );
 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_info_url );
 //        }
-//        else if( EbmlId( *l ) == KaxCodecDownloadURL::ClassInfos.GlobalId )
+//        else if( MKV_IS_ID( l, KaxCodecDownloadURL) ) DEPRECATED by matroska
 //        {
 //            KaxCodecDownloadURL &cdurl = *(KaxCodecDownloadURL*)l;
 
 //            tk->psz_codec_download_url = strdup( string( cdurl ).c_str() );
 //            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Info URL=%s", tk->psz_codec_download_url );
-//        }
-//        else if( EbmlId( *l ) == KaxCodecDecodeAll::ClassInfos.GlobalId )
-//        {
-//            KaxCodecDecodeAll &cdall = *(KaxCodecDecodeAll*)l;
-
-//            msg_Dbg( &sys.demuxer, "|   |   |   + Track Codec Decode All=%u <== UNUSED", uint8( cdall ) );
-//        }
-//        else if( EbmlId( *l ) == KaxTrackOverlay::ClassInfos.GlobalId )
-//        {
-//            KaxTrackOverlay &tovr = *(KaxTrackOverlay*)l;
-
-//            msg_Dbg( &sys.demuxer, "|   |   |   + Track Overlay=%u <== UNUSED", uint32( tovr ) );
 //        }
         else  if( MKV_IS_ID( l, KaxTrackVideo ) )
         {
             EbmlMaster *tkv = static_cast<EbmlMaster*>(l);
-            unsigned int j;
             unsigned int i_crop_right = 0, i_crop_left = 0, i_crop_top = 0, i_crop_bottom = 0;
             unsigned int i_display_unit = 0, i_display_width = 0, i_display_height = 0;
 
@@ -457,24 +532,23 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 
             tk->fmt.video.i_frame_rate_base = (unsigned int)(tk->i_default_duration / 1000);
             tk->fmt.video.i_frame_rate = 1000000;
-            for( j = 0; j < tkv->ListSize(); j++ )
+
+            for( unsigned int j = 0; j < tkv->ListSize(); j++ )
             {
                 EbmlElement *l = (*tkv)[j];
-//                if( EbmlId( *el4 ) == KaxVideoFlagInterlaced::ClassInfos.GlobalId )
-//                {
-//                    KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)el4;
+                if( MKV_IS_ID( l, KaxVideoFlagInterlaced ) ) // UNUSED
+                {
+                    KaxVideoFlagInterlaced &fint = *(KaxVideoFlagInterlaced*)l;
 
-//                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
-//                }
-//                else if( EbmlId( *el4 ) == KaxVideoStereoMode::ClassInfos.GlobalId )
-//                {
-//                    KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)el4;
+                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Interlaced=%u", uint8( fint ) );
+                }
+                else if( MKV_IS_ID( l, KaxVideoStereoMode ) ) // UNUSED
+                {
+                    KaxVideoStereoMode &stereo = *(KaxVideoStereoMode*)l;
 
-//                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
-//                }
-//                else
-                if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
+                    msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Stereo Mode=%u", uint8( stereo ) );
+                }
+                else if( MKV_IS_ID( l, KaxVideoPixelWidth ) )
                 {
                     KaxVideoPixelWidth &vwidth = *(KaxVideoPixelWidth*)l;
 
@@ -530,28 +604,29 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                     i_crop_left = uint16( cropval );
                     msg_Dbg( &sys.demuxer, "|   |   |   |   + crop pixel left=%d", uint16( cropval ) );
                 }
-                else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
-                {
-                    KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
-
-                    tk->f_fps = float( vfps );
-                    msg_Dbg( &sys.demuxer, "   |   |   |   + fps=%f", float( vfps ) );
-                }
-                else if( EbmlId( *l ) == KaxVideoDisplayUnit::ClassInfos.GlobalId )
+                else if( MKV_IS_ID( l, KaxVideoDisplayUnit ) )
                 {
                     KaxVideoDisplayUnit &vdmode = *(KaxVideoDisplayUnit*)l;
 
                     i_display_unit = uint8( vdmode );
                     msg_Dbg( &sys.demuxer, "|   |   |   |   + Track Video Display Unit=%s",
-                             uint8( vdmode ) == 0 ? "pixels" : ( uint8( vdmode ) == 1 ? "centimeters": "inches" ) );
+                             i_display_unit == 0 ? "pixels" : ( i_display_unit == 1 ? "centimeters": "inches" ) );
                 }
-//                else if( EbmlId( *l ) == KaxVideoAspectRatio::ClassInfos.GlobalId )
-//                {
-//                    KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
+                else if( MKV_IS_ID( l, KaxVideoAspectRatio ) ) // UNUSED
+                {
+                    KaxVideoAspectRatio &ratio = *(KaxVideoAspectRatio*)l;
 
-//                    msg_Dbg( &sys.demuxer, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
-//                }
-//                else if( EbmlId( *l ) == KaxVideoGamma::ClassInfos.GlobalId )
+                    msg_Dbg( &sys.demuxer, "   |   |   |   + Track Video Aspect Ratio Type=%u", uint8( ratio ) );
+                }
+                // ColourSpace UNUSED
+                else if( MKV_IS_ID( l, KaxVideoFrameRate ) )
+                {
+                    KaxVideoFrameRate &vfps = *(KaxVideoFrameRate*)l;
+
+                    tk->f_fps = __MAX( float( vfps ), 1 );
+                    msg_Dbg( &sys.demuxer, "   |   |   |   + fps=%f", float( vfps ) );
+                }
+//                else if( MKV_IS_ID( l, KaxVideoGamma) ) //DEPRECATED by Matroska
 //                {
 //                    KaxVideoGamma &gamma = *(KaxVideoGamma*)l;
 
@@ -563,14 +638,17 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
                 }
             }
             if( i_display_height && i_display_width )
-                tk->fmt.video.i_aspect = VOUT_ASPECT_FACTOR * i_display_width / i_display_height;
+            {
+                tk->fmt.video.i_sar_num = i_display_width  * tk->fmt.video.i_height;
+                tk->fmt.video.i_sar_den = i_display_height * tk->fmt.video.i_width;
+            }
             if( i_crop_left || i_crop_right || i_crop_top || i_crop_bottom )
             {
-                tk->fmt.video.i_visible_width = tk->fmt.video.i_width;
-                tk->fmt.video.i_visible_height = tk->fmt.video.i_height;
-                tk->fmt.video.i_x_offset = i_crop_left;
-                tk->fmt.video.i_y_offset = i_crop_top;
-                tk->fmt.video.i_visible_width -= i_crop_left + i_crop_right;
+                tk->fmt.video.i_visible_width   = tk->fmt.video.i_width;
+                tk->fmt.video.i_visible_height  = tk->fmt.video.i_height;
+                tk->fmt.video.i_x_offset        = i_crop_left;
+                tk->fmt.video.i_y_offset        = i_crop_top;
+                tk->fmt.video.i_visible_width  -= i_crop_left + i_crop_right;
                 tk->fmt.video.i_visible_height -= i_crop_top + i_crop_bottom;
             }
             /* FIXME: i_display_* allows you to not only set DAR, but also a zoom factor.
@@ -579,11 +657,14 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
         else  if( MKV_IS_ID( l, KaxTrackAudio ) )
         {
             EbmlMaster *tka = static_cast<EbmlMaster*>(l);
-            unsigned int j;
+
+            /* Initialize default values */
+            tk->fmt.audio.i_channels = 1;
+            tk->fmt.audio.i_rate = 8000;
 
             msg_Dbg( &sys.demuxer, "|   |   |   + Track Audio" );
 
-            for( j = 0; j < tka->ListSize(); j++ )
+            for( unsigned int j = 0; j < tka->ListSize(); j++ )
             {
                 EbmlElement *l = (*tka)[j];
 
@@ -630,11 +711,27 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 
     if ( bSupported )
     {
+#ifdef HAVE_ZLIB_H
+        if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
+            tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_PRIVATE &&
+            tk->i_extra_data && tk->p_extra_data &&
+            zlib_decompress_extra( &sys.demuxer, tk) )
+            return;
+#endif
+        if( TrackInit( tk ) )
+        {
+            msg_Err(&sys.demuxer, "Couldn't init track %d", tk->i_number );
+            free(tk->p_extra_data);
+            delete tk;
+            return;
+        }
+
         tracks.push_back( tk );
     }
     else
     {
         msg_Err( &sys.demuxer, "Track Entry %d not supported", tk->i_number );
+        free(tk->p_extra_data);
         delete tk;
     }
 }
@@ -645,13 +742,25 @@ void matroska_segment_c::ParseTrackEntry( KaxTrackEntry *m )
 void matroska_segment_c::ParseTracks( KaxTracks *tracks )
 {
     EbmlElement *el;
-    unsigned int i;
     int i_upper_level = 0;
 
     /* Master elements */
-    tracks->Read( es, tracks->Generic().Context, i_upper_level, el, true );
+    if( unlikely( tracks->IsFiniteSize() && tracks->GetSize() >= SIZE_MAX ) )
+    {
+        msg_Err( &sys.demuxer, "Track too big, aborting" );
+        return;
+    }
+    try
+    {
+        tracks->Read( es, EBML_CONTEXT(tracks), i_upper_level, el, true );
+    }
+    catch(...)
+    {
+        msg_Err( &sys.demuxer, "Couldn't read tracks" );
+        return;
+    }
 
-    for( i = 0; i < tracks->ListSize(); i++ )
+    for( size_t i = 0; i < tracks->ListSize(); i++ )
     {
         EbmlElement *l = (*tracks)[i];
 
@@ -673,14 +782,26 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
 {
     EbmlElement *el;
     EbmlMaster  *m;
-    size_t i, j;
     int i_upper_level = 0;
 
     /* Master elements */
     m = static_cast<EbmlMaster *>(info);
-    m->Read( es, info->Generic().Context, i_upper_level, el, true );
+    if( unlikely( m->IsFiniteSize() && m->GetSize() >= SIZE_MAX ) )
+    {
+        msg_Err( &sys.demuxer, "Info too big, aborting" );
+        return;
+    }
+    try
+    {
+        m->Read( es, EBML_CONTEXT(info), i_upper_level, el, true );
+    }
+    catch(...)
+    {
+        msg_Err( &sys.demuxer, "Couldn't read info" );
+        return;
+    }   
 
-    for( i = 0; i < m->ListSize(); i++ )
+    for( size_t i = 0; i < m->ListSize(); i++ )
     {
         EbmlElement *l = (*m)[i];
 
@@ -694,14 +815,20 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
         else if( MKV_IS_ID( l, KaxPrevUID ) )
         {
             if ( p_prev_segment_uid == NULL )
+            {
                 p_prev_segment_uid = new KaxPrevUID(*static_cast<KaxPrevUID*>(l));
+                b_ref_external_segments = true;
+            }
 
             msg_Dbg( &sys.demuxer, "|   |   + PrevUID=%d", *(uint32*)p_prev_segment_uid->GetBuffer() );
         }
         else if( MKV_IS_ID( l, KaxNextUID ) )
         {
             if ( p_next_segment_uid == NULL )
+            {
                 p_next_segment_uid = new KaxNextUID(*static_cast<KaxNextUID*>(l));
+                b_ref_external_segments = true;
+            }
 
             msg_Dbg( &sys.demuxer, "|   |   + NextUID=%d", *(uint32*)p_next_segment_uid->GetBuffer() );
         }
@@ -711,7 +838,7 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
 
             i_timescale = uint64(tcs);
 
-            msg_Dbg( &sys.demuxer, "|   |   + TimecodeScale=%"PRId64,
+            msg_Dbg( &sys.demuxer, "|   |   + TimecodeScale=%" PRId64,
                      i_timescale );
         }
         else if( MKV_IS_ID( l, KaxDuration ) )
@@ -720,7 +847,7 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
 
             i_duration = mtime_t( double( dur ) );
 
-            msg_Dbg( &sys.demuxer, "|   |   + Duration=%"PRId64,
+            msg_Dbg( &sys.demuxer, "|   |   + Duration=%" PRId64,
                      i_duration );
         }
         else if( MKV_IS_ID( l, KaxMuxingApp ) )
@@ -785,28 +912,41 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
         else if( MKV_IS_ID( l, KaxChapterTranslate ) )
         {
             KaxChapterTranslate *p_trans = static_cast<KaxChapterTranslate*>( l );
-            chapter_translation_c *p_translate = new chapter_translation_c();
-
-            p_trans->Read( es, p_trans->Generic().Context, i_upper_level, el, true );
-            for( j = 0; j < p_trans->ListSize(); j++ )
+            try
             {
-                EbmlElement *l = (*p_trans)[j];
-
-                if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
+                if( unlikely( p_trans->IsFiniteSize() && p_trans->GetSize() >= SIZE_MAX ) )
                 {
-                    p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
+                    msg_Err( &sys.demuxer, "Chapter translate too big, aborting" );
+                    continue;
                 }
-                else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
-                {
-                    p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
-                }
-                else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
+
+                p_trans->Read( es, EBML_CONTEXT(p_trans), i_upper_level, el, true );
+                chapter_translation_c *p_translate = new chapter_translation_c();
+
+                for( size_t j = 0; j < p_trans->ListSize(); j++ )
                 {
-                    p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
+                    EbmlElement *l = (*p_trans)[j];
+
+                    if( MKV_IS_ID( l, KaxChapterTranslateEditionUID ) )
+                    {
+                        p_translate->editions.push_back( uint64( *static_cast<KaxChapterTranslateEditionUID*>( l ) ) );
+                    }
+                    else if( MKV_IS_ID( l, KaxChapterTranslateCodec ) )
+                    {
+                        p_translate->codec_id = uint32( *static_cast<KaxChapterTranslateCodec*>( l ) );
+                    }
+                    else if( MKV_IS_ID( l, KaxChapterTranslateID ) )
+                    {
+                        p_translate->p_translated = new KaxChapterTranslateID( *static_cast<KaxChapterTranslateID*>( l ) );
+                    }
                 }
-            }
 
-            translations.push_back( p_translate );
+                translations.push_back( p_translate );
+            }
+            catch(...)
+            {
+                msg_Err( &sys.demuxer, "Error while reading Chapter Tranlate");
+            }
         }
         else
         {
@@ -816,6 +956,7 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
 
     double f_dur = double(i_duration) * double(i_timescale) / 1000000.0;
     i_duration = mtime_t(f_dur);
+    if( !i_duration ) i_duration = -1;
 }
 
 
@@ -824,17 +965,15 @@ void matroska_segment_c::ParseInfo( KaxInfo *info )
  *****************************************************************************/
 void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chapter_item_c & chapters )
 {
-    size_t i, j;
-
     msg_Dbg( &sys.demuxer, "|   |   |   + ChapterAtom (level=%d)", i_level );
-    for( i = 0; i < ca->ListSize(); i++ )
+    for( size_t i = 0; i < ca->ListSize(); i++ )
     {
         EbmlElement *l = (*ca)[i];
 
         if( MKV_IS_ID( l, KaxChapterUID ) )
         {
             chapters.i_uid = uint64_t(*(KaxChapterUID*)l);
-            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterUID: %lld", chapters.i_uid );
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterUID: %" PRIu64, chapters.i_uid );
         }
         else if( MKV_IS_ID( l, KaxChapterFlagHidden ) )
         {
@@ -843,35 +982,50 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
 
             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterFlagHidden: %s", chapters.b_display_seekpoint ? "no":"yes" );
         }
+        else if( MKV_IS_ID( l, KaxChapterSegmentUID ) )
+        {
+            chapters.p_segment_uid = new KaxChapterSegmentUID( *static_cast<KaxChapterSegmentUID*>(l) );
+            b_ref_external_segments = true;
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterSegmentUID= %u", *(uint32*)chapters.p_segment_uid->GetBuffer() );
+        }
+        else if( MKV_IS_ID( l, KaxChapterSegmentEditionUID ) )
+        {
+            chapters.p_segment_edition_uid = new KaxChapterSegmentEditionUID( *static_cast<KaxChapterSegmentEditionUID*>(l) );
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterSegmentEditionUID= %u",
+#if LIBMATROSKA_VERSION < 0x010300
+                     *(uint32*)chapters.p_segment_edition_uid->GetBuffer()
+#else
+                     *(uint32*)chapters.p_segment_edition_uid
+#endif
+                   );
+        }
         else if( MKV_IS_ID( l, KaxChapterTimeStart ) )
         {
             KaxChapterTimeStart &start =*(KaxChapterTimeStart*)l;
             chapters.i_start_time = uint64( start ) / INT64_C(1000);
 
-            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeStart: %lld", chapters.i_start_time );
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeStart: %" PRId64, chapters.i_start_time );
         }
         else if( MKV_IS_ID( l, KaxChapterTimeEnd ) )
         {
             KaxChapterTimeEnd &end =*(KaxChapterTimeEnd*)l;
             chapters.i_end_time = uint64( end ) / INT64_C(1000);
 
-            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeEnd: %lld", chapters.i_end_time );
+            msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterTimeEnd: %" PRId64, chapters.i_end_time );
         }
         else if( MKV_IS_ID( l, KaxChapterDisplay ) )
         {
             EbmlMaster *cd = static_cast<EbmlMaster *>(l);
 
             msg_Dbg( &sys.demuxer, "|   |   |   |   + ChapterDisplay" );
-            for( j = 0; j < cd->ListSize(); j++ )
+            for( size_t j = 0; j < cd->ListSize(); j++ )
             {
                 EbmlElement *l= (*cd)[j];
 
                 if( MKV_IS_ID( l, KaxChapterString ) )
                 {
-                    int k;
-
                     KaxChapterString &name =*(KaxChapterString*)l;
-                    for (k = 0; k < i_level; k++)
+                    for ( int k = 0; k < i_level; k++)
                         chapters.psz_name += '+';
                     chapters.psz_name += ' ';
                     char *psz_tmp_utf8 = ToUTF8( UTFstring( name ) );
@@ -884,16 +1038,14 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
                 else if( MKV_IS_ID( l, KaxChapterLanguage ) )
                 {
                     KaxChapterLanguage &lang =*(KaxChapterLanguage*)l;
-                    const char *psz = string( lang ).c_str();
-
-                    msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterLanguage '%s'", psz );
+                    msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterLanguage '%s'",
+                             string( lang ).c_str() );
                 }
                 else if( MKV_IS_ID( l, KaxChapterCountry ) )
                 {
                     KaxChapterCountry &ct =*(KaxChapterCountry*)l;
-                    const char *psz = string( ct ).c_str();
-
-                    msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterCountry '%s'", psz );
+                    msg_Dbg( &sys.demuxer, "|   |   |   |   |    + ChapterCountry '%s'",
+                             string( ct ).c_str() );
                 }
             }
         }
@@ -904,7 +1056,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
             KaxChapterProcess *cp = static_cast<KaxChapterProcess *>(l);
             chapter_codec_cmds_c *p_ccodec = NULL;
 
-            for( j = 0; j < cp->ListSize(); j++ )
+            for( size_t j = 0; j < cp->ListSize(); j++ )
             {
                 EbmlElement *k= (*cp)[j];
 
@@ -921,7 +1073,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
 
             if ( p_ccodec != NULL )
             {
-                for( j = 0; j < cp->ListSize(); j++ )
+                for( size_t j = 0; j < cp->ListSize(); j++ )
                 {
                     EbmlElement *k= (*cp)[j];
 
@@ -942,7 +1094,7 @@ void matroska_segment_c::ParseChapterAtom( int i_level, KaxChapterAtom *ca, chap
         {
             chapter_item_c *new_sub_chapter = new chapter_item_c();
             ParseChapterAtom( i_level+1, static_cast<KaxChapterAtom *>(l), *new_sub_chapter );
-            new_sub_chapter->psz_parent = &chapters;
+            new_sub_chapter->p_parent = &chapters;
             chapters.sub_chapters.push_back( new_sub_chapter );
         }
     }
@@ -956,35 +1108,55 @@ void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
     EbmlElement *el;
     int i_upper_level = 0;
 
-    attachments->Read( es, attachments->Generic().Context, i_upper_level, el, true );
+    if( unlikely( attachments->IsFiniteSize() && attachments->GetSize() >= SIZE_MAX ) )
+    {
+        msg_Err( &sys.demuxer, "Attachments too big, aborting" );
+        return;
+    }
+    try
+    {
+        attachments->Read( es, EBML_CONTEXT(attachments), i_upper_level, el, true );
+    }
+    catch(...)
+    {
+        msg_Err( &sys.demuxer, "Error while reading attachments" );
+        return;
+    }
 
     KaxAttached *attachedFile = FindChild<KaxAttached>( *attachments );
 
     while( attachedFile && ( attachedFile->GetSize() > 0 ) )
     {
-        std::string psz_mime_type  = GetChild<KaxMimeType>( *attachedFile );
-        KaxFileName  &file_name    = GetChild<KaxFileName>( *attachedFile );
         KaxFileData  &img_data     = GetChild<KaxFileData>( *attachedFile );
+        char *psz_tmp_utf8 =  ToUTF8( UTFstring( GetChild<KaxFileName>( *attachedFile ) ) );
+        std::string attached_filename(psz_tmp_utf8);
+        free(psz_tmp_utf8);
+        attachment_c *new_attachment = new attachment_c( attached_filename,
+                                                         GetChild<KaxMimeType>( *attachedFile ),
+                                                         img_data.GetSize() );
 
-        attachment_c *new_attachment = new attachment_c();
+        msg_Dbg( &sys.demuxer, "|   |   - %s (%s)", new_attachment->fileName(), new_attachment->mimeType() );
 
-        if( new_attachment )
+        if( new_attachment->init() )
         {
-            new_attachment->psz_file_name  = ToUTF8( UTFstring( file_name ) );
-            new_attachment->psz_mime_type  = psz_mime_type;
-            new_attachment->i_size         = img_data.GetSize();
-            new_attachment->p_data         = malloc( img_data.GetSize() );
-
-            if( new_attachment->p_data )
-            {
-                memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
-                sys.stored_attachments.push_back( new_attachment );
-            }
-            else
+            memcpy( new_attachment->p_data, img_data.GetBuffer(), img_data.GetSize() );
+            sys.stored_attachments.push_back( new_attachment );
+            if( !strncmp( new_attachment->mimeType(), "image/", 6 ) )
             {
-                delete new_attachment;
+                char *psz_url;
+                if( asprintf( &psz_url, "attachment://%s",
+                              new_attachment->fileName() ) == -1 )
+                    continue;
+                if( !sys.meta )
+                    sys.meta = vlc_meta_New();
+                vlc_meta_SetArtURL( sys.meta, psz_url );
+                free( psz_url );
             }
         }
+        else
+        {
+            delete new_attachment;
+        }
 
         attachedFile = &GetNextChild<KaxAttached>( *attachments, *attachedFile );
     }
@@ -996,25 +1168,35 @@ void matroska_segment_c::ParseAttachments( KaxAttachments *attachments )
 void matroska_segment_c::ParseChapters( KaxChapters *chapters )
 {
     EbmlElement *el;
-    size_t i;
     int i_upper_level = 0;
-    mtime_t i_dur;
 
     /* Master elements */
-    chapters->Read( es, chapters->Generic().Context, i_upper_level, el, true );
+    if( unlikely( chapters->IsFiniteSize() && chapters->GetSize() >= SIZE_MAX ) )
+    {
+        msg_Err( &sys.demuxer, "Chapters too big, aborting" );
+        return;
+    }
+    try
+    {
+        chapters->Read( es, EBML_CONTEXT(chapters), i_upper_level, el, true );
+    }
+    catch(...)
+    {
+        msg_Err( &sys.demuxer, "Error while reading chapters" );
+        return;
+    }
 
-    for( i = 0; i < chapters->ListSize(); i++ )
+    for( size_t i = 0; i < chapters->ListSize(); i++ )
     {
         EbmlElement *l = (*chapters)[i];
 
         if( MKV_IS_ID( l, KaxEditionEntry ) )
         {
             chapter_edition_c *p_edition = new chapter_edition_c();
+
             EbmlMaster *E = static_cast<EbmlMaster *>(l );
-            size_t j;
             msg_Dbg( &sys.demuxer, "|   |   + EditionEntry" );
-            for( j = 0; j < E->ListSize(); j++ )
+            for( size_t j = 0; j < E->ListSize(); j++ )
             {
                 EbmlElement *l = (*E)[j];
 
@@ -1030,13 +1212,17 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
                 }
                 else if( MKV_IS_ID( l, KaxEditionFlagOrdered ) )
                 {
-                    p_edition->b_ordered = config_GetInt( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
+                    p_edition->b_ordered = var_InheritBool( &sys.demuxer, "mkv-use-ordered-chapters" ) ? (uint8(*static_cast<KaxEditionFlagOrdered *>( l )) != 0) : 0;
                 }
                 else if( MKV_IS_ID( l, KaxEditionFlagDefault ) )
                 {
                     if (uint8(*static_cast<KaxEditionFlagDefault *>( l )) != 0)
                         i_default_edition = stored_editions.size();
                 }
+                else if( MKV_IS_ID( l, KaxEditionFlagHidden ) )
+                {
+                    // FIXME to implement
+                }
                 else
                 {
                     msg_Dbg( &sys.demuxer, "|   |   |   + Unknown (%s)", typeid(*l).name() );
@@ -1049,33 +1235,32 @@ void matroska_segment_c::ParseChapters( KaxChapters *chapters )
             msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid(*l).name() );
         }
     }
-
-    for( i = 0; i < stored_editions.size(); i++ )
-    {
-        stored_editions[i]->RefreshChapters( );
-    }
-    if ( stored_editions.size() != 0 && stored_editions[i_default_edition]->b_ordered )
-    {
-        /* update the duration of the segment according to the sum of all sub chapters */
-        i_dur = stored_editions[i_default_edition]->Duration() / INT64_C(1000);
-        if (i_dur > 0)
-            i_duration = i_dur;
-    }
 }
 
-void matroska_segment_c::ParseCluster( )
+void matroska_segment_c::ParseCluster( KaxCluster *cluster, bool b_update_start_time, ScopeMode read_fully )
 {
     EbmlElement *el;
     EbmlMaster  *m;
-    unsigned int i;
     int i_upper_level = 0;
 
     /* Master elements */
     m = static_cast<EbmlMaster *>( cluster );
-    m->Read( es, cluster->Generic().Context, i_upper_level, el, true );
+    if( unlikely( m->IsFiniteSize() && m->GetSize() >= SIZE_MAX ) )
+    {
+        msg_Err( &sys.demuxer, "Cluster too big, aborting" );
+        return;
+    }
+    try
+    {
+        m->Read( es, EBML_CONTEXT(cluster), i_upper_level, el, true, read_fully );
+    }
+    catch(...)
+    {
+        msg_Err( &sys.demuxer, "Error while reading cluster" );
+        return;
+    }
 
-    for( i = 0; i < m->ListSize(); i++ )
+    for( unsigned int i = 0; i < m->ListSize(); i++ )
     {
         EbmlElement *l = (*m)[i];
 
@@ -1088,7 +1273,559 @@ void matroska_segment_c::ParseCluster( )
         }
     }
 
-    i_start_time = cluster->GlobalTimecode() / 1000;
+    if( b_update_start_time )
+        i_start_time = cluster->GlobalTimecode() / 1000;
 }
 
 
+int32_t matroska_segment_c::TrackInit( mkv_track_t * p_tk )
+{
+    es_format_t *p_fmt = &p_tk->fmt;
+
+    if( p_tk->psz_codec == NULL )
+    {
+        msg_Err( &sys.demuxer, "Empty codec id" );
+        p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+        return 0;
+    }
+
+    if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
+    {
+        if( p_tk->i_extra_data < (int)sizeof( VLC_BITMAPINFOHEADER ) )
+        {
+            msg_Err( &sys.demuxer, "missing/invalid VLC_BITMAPINFOHEADER" );
+            p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+        }
+        else
+        {
+            VLC_BITMAPINFOHEADER *p_bih = (VLC_BITMAPINFOHEADER*)p_tk->p_extra_data;
+
+            p_tk->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
+            p_tk->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
+            p_tk->fmt.i_codec       = GetFOURCC( &p_bih->biCompression );
+
+            p_tk->fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( VLC_BITMAPINFOHEADER );
+            if( p_tk->fmt.i_extra > 0 )
+            {
+                /* Very unlikely yet possible: bug #5659*/
+                size_t maxlen = p_tk->i_extra_data - sizeof( VLC_BITMAPINFOHEADER );
+                p_tk->fmt.i_extra = ( (unsigned)p_tk->fmt.i_extra < maxlen )?
+                    p_tk->fmt.i_extra : maxlen;
+
+                p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
+                memcpy( p_tk->fmt.p_extra, &p_bih[1], p_tk->fmt.i_extra );
+            }
+        }
+        p_tk->b_dts_only = true;
+    }
+    else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
+             !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_MPGV;
+        if( p_tk->i_extra_data )
+            fill_extra_data( p_tk, 0 );
+    }
+    else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_THEORA;
+        fill_extra_data( p_tk, 0 );
+        p_tk->b_pts_only = true;
+    }
+    else if( !strncmp( p_tk->psz_codec, "V_REAL/RV", 9 ) )
+    {
+        uint8_t *p = p_tk->p_extra_data;
+
+        if( !strcmp( p_tk->psz_codec, "V_REAL/RV10" ) )
+            p_fmt->i_codec = VLC_CODEC_RV10;
+        else if( !strcmp( p_tk->psz_codec, "V_REAL/RV20" ) )
+            p_fmt->i_codec = VLC_CODEC_RV20;
+        else if( !strcmp( p_tk->psz_codec, "V_REAL/RV30" ) )
+            p_fmt->i_codec = VLC_CODEC_RV30;
+        else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
+            p_fmt->i_codec = VLC_CODEC_RV40;
+
+        /* Extract the framerate from the header */
+        if( p_tk->i_extra_data >= 26 &&
+            p[4] == 'V' && p[5] == 'I' && p[6] == 'D' && p[7] == 'O' &&
+            p[8] == 'R' && p[9] == 'V' &&
+            (p[10] == '3' || p[10] == '4') && p[11] == '0' )
+        {
+            p_tk->fmt.video.i_frame_rate =
+                p[22] << 24 | p[23] << 16 | p[24] << 8 | p[25] << 0;
+            p_tk->fmt.video.i_frame_rate_base = 65536;
+        }
+
+        fill_extra_data( p_tk, 26 );
+        p_tk->b_dts_only = true;
+    }
+    else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
+    }
+    else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_VP8;
+        p_tk->b_pts_only = true;
+    }
+    else if( !strncmp( p_tk->psz_codec, "V_VP9", 5 ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_VP9;
+        fill_extra_data( p_tk, 0 );
+    }
+    else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
+    {
+        if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
+        {
+            p_tk->fmt.i_codec = VLC_CODEC_DIV3;
+        }
+        else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
+        {
+            /* A MPEG 4 codec, SP, ASP, AP or AVC */
+            if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
+                p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
+            else
+                p_tk->fmt.i_codec = VLC_CODEC_MP4V;
+            fill_extra_data( p_tk, 0 );
+        }
+    }
+    else if( !strncmp( p_tk->psz_codec, "V_MPEGH/ISO/HEVC", 16) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_HEVC;
+        fill_extra_data( p_tk, 0 );
+    } 
+    else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
+    {
+        MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
+        stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
+                                                   p_tk->p_extra_data,
+                                                   p_tk->i_extra_data,
+                                                   true );
+        if( MP4_ReadBoxCommon( p_mp4_stream, p_box ) &&
+            MP4_ReadBox_sample_vide( p_mp4_stream, p_box ) )
+        {
+            p_tk->fmt.i_codec = p_box->i_type;
+            uint32_t i_width = p_box->data.p_sample_vide->i_width;
+            uint32_t i_height = p_box->data.p_sample_vide->i_height;
+            if( i_width && i_height )
+            {
+                p_tk->fmt.video.i_width = i_width;
+                p_tk->fmt.video.i_height = i_height;
+            }
+            p_tk->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
+            p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
+            memcpy( p_tk->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, p_tk->fmt.i_extra );
+            MP4_FreeBox_sample_vide( p_box );
+        }
+        else
+        {
+            free( p_box );
+        }
+        stream_Delete( p_mp4_stream );
+    }
+    else if( !strcmp( p_tk->psz_codec, "V_MJPEG" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_MJPG;
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_MS/ACM" ) )
+    {
+        if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
+        {
+            msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
+            p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+        }
+        else
+        {
+            WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
+
+            p_tk->fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
+            p_tk->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
+            p_tk->fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
+            p_tk->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
+            p_tk->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
+
+            p_tk->fmt.i_extra            = GetWLE( &p_wf->cbSize );
+            if( p_tk->fmt.i_extra > 0 )
+            {
+                p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
+                if( p_tk->fmt.p_extra )
+                    memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
+                else
+                    p_tk->fmt.i_extra = 0;
+            }
+
+            if( p_wf->wFormatTag == WAVE_FORMAT_EXTENSIBLE && 
+                p_tk->i_extra_data >= sizeof(WAVEFORMATEXTENSIBLE) )
+            {
+                WAVEFORMATEXTENSIBLE * p_wext = (WAVEFORMATEXTENSIBLE*) p_wf;
+                sf_tag_to_fourcc( &p_wext->SubFormat,  &p_tk->fmt.i_codec, NULL);
+                /* FIXME should we use Samples */
+
+                if( p_tk->fmt.audio.i_channels > 2 &&
+                    ( p_tk->fmt.i_codec != VLC_FOURCC( 'u', 'n', 'd', 'f' ) ) ) 
+                {
+                    uint32_t wfextcm = GetDWLE( &p_wext->dwChannelMask );
+                    int match;
+                    unsigned i_channel_mask = getChannelMask( &wfextcm,
+                                                              p_tk->fmt.audio.i_channels,
+                                                              &match );
+                    p_tk->fmt.i_codec = vlc_fourcc_GetCodecAudio( p_tk->fmt.i_codec,
+                                                                  p_tk->fmt.audio.i_bitspersample );
+                    if( i_channel_mask )
+                    {
+                        p_tk->i_chans_to_reorder = aout_CheckChannelReorder(
+                            pi_channels_aout, NULL,
+                            i_channel_mask,
+                            p_tk->pi_chan_table );
+
+                        p_tk->fmt.audio.i_physical_channels =
+                        p_tk->fmt.audio.i_original_channels = i_channel_mask;
+                    }
+                }
+            }
+            else
+                wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
+
+            if( p_tk->fmt.i_codec == VLC_FOURCC( 'u', 'n', 'd', 'f' ) )
+                msg_Err( &sys.demuxer, "Unrecognized wf tag: 0x%x", GetWLE( &p_wf->wFormatTag ) );
+        }
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_MPEG/L3" ) ||
+             !strcmp( p_tk->psz_codec, "A_MPEG/L2" ) ||
+             !strcmp( p_tk->psz_codec, "A_MPEG/L1" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_MPGA;
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
+    {
+        // the AC-3 default duration cannot be trusted, see #8512
+        if ( p_tk->fmt.audio.i_rate == 8000 )
+        {
+            p_tk->b_no_duration = true;
+            p_tk->i_default_duration = 0;
+        }
+        p_tk->fmt.i_codec = VLC_CODEC_A52;
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_EAC3;
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_DTS;
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_MLP;
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
+    {
+        /* FIXME when more samples arrive */
+        p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
+        p_fmt->b_packetized = false;
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_FLAC;
+        fill_extra_data( p_tk, 8 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
+        fill_extra_data( p_tk, 0 );
+    }
+    else if( !strncmp( p_tk->psz_codec, "A_OPUS", 6 ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_OPUS;
+        if( !p_tk->fmt.audio.i_rate )
+        {
+            msg_Err( &sys.demuxer,"No sampling rate, defaulting to 48kHz");
+            p_tk->fmt.audio.i_rate = 48000;
+        }
+        const uint8_t tags[16] = {'O','p','u','s','T','a','g','s',
+                                   0, 0, 0, 0, 0, 0, 0, 0};
+        unsigned ps[2] = { p_tk->i_extra_data, 16 };
+        const void *pkt[2] = { (const void *)p_tk->p_extra_data,
+                              (const void *) tags };
+
+        if( xiph_PackHeaders( &p_tk->fmt.i_extra,
+                              &p_tk->fmt.p_extra,
+                              ps, pkt, 2 ) )
+            msg_Err( &sys.demuxer, "Couldn't pack OPUS headers");
+
+    }
+    else if( !strncmp( p_tk->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
+             !strncmp( p_tk->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
+    {
+        int i_profile, i_srate, sbr = 0;
+        static const unsigned int i_sample_rates[] =
+        {
+            96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050,
+            16000, 12000, 11025,  8000,  7350,     0,     0,     0
+        };
+
+        p_tk->fmt.i_codec = VLC_CODEC_MP4A;
+        /* create data for faad (MP4DecSpecificDescrTag)*/
+
+        if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
+        {
+            i_profile = 0;
+        }
+        else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
+        {
+            i_profile = 1;
+        }
+        else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
+        {
+            i_profile = 2;
+        }
+        else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
+        {
+            i_profile = 1;
+            sbr = 1;
+        }
+        else
+        {
+            i_profile = 3;
+        }
+
+        for( i_srate = 0; i_srate < 13; i_srate++ )
+        {
+            if( i_sample_rates[i_srate] == p_tk->i_original_rate )
+            {
+                break;
+            }
+        }
+        msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
+
+        p_tk->fmt.i_extra = sbr ? 5 : 2;
+        p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
+        ((uint8_t*)p_tk->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
+        ((uint8_t*)p_tk->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (p_tk->fmt.audio.i_channels << 3);
+        if (sbr != 0)
+        {
+            int syncExtensionType = 0x2B7;
+            int iDSRI;
+            for (iDSRI=0; iDSRI<13; iDSRI++)
+                if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
+                    break;
+            ((uint8_t*)p_tk->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
+            ((uint8_t*)p_tk->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
+            ((uint8_t*)p_tk->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
+        }
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_MP4A;
+        fill_extra_data( p_tk, 0 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_ALAC" ) )
+    {
+        p_tk->fmt.i_codec =  VLC_CODEC_ALAC;
+        fill_extra_data( p_tk, 0 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
+        fill_extra_data( p_tk, 0 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
+    {
+        p_fmt->i_codec = VLC_CODEC_TTA;
+        if( p_tk->i_extra_data > 0 )
+        {
+            fill_extra_data( p_tk, 0 );
+        }
+        else
+        {
+            p_fmt->i_extra = 30;
+            p_fmt->p_extra = xmalloc( p_fmt->i_extra );
+            uint8_t *p_extra = (uint8_t*)p_fmt->p_extra;
+            memcpy( &p_extra[ 0], "TTA1", 4 );
+            SetWLE( &p_extra[ 4], 1 );
+            SetWLE( &p_extra[ 6], p_fmt->audio.i_channels );
+            SetWLE( &p_extra[ 8], p_fmt->audio.i_bitspersample );
+            SetDWLE( &p_extra[10], p_fmt->audio.i_rate );
+            SetDWLE( &p_extra[14], 0xffffffff );
+            memset( &p_extra[18], 0, 30  - 18 );
+        }
+    }
+    else if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) ||
+             !strcmp( p_tk->psz_codec, "A_PCM/INT/LIT" ) ||
+             !strcmp( p_tk->psz_codec, "A_PCM/FLOAT/IEEE" ) )
+    {
+        if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
+        {
+            p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
+        }
+        else
+        {
+            p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
+        }
+        p_tk->fmt.audio.i_blockalign = ( p_tk->fmt.audio.i_bitspersample + 7 ) / 8 * p_tk->fmt.audio.i_channels;
+    }
+    else if( !strncmp( p_tk->psz_codec, "A_REAL/", 7 ) )
+    {
+        if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
+        {
+            p_fmt->i_codec = VLC_CODEC_RA_144;
+            p_fmt->audio.i_channels = 1;
+            p_fmt->audio.i_rate = 8000;
+            p_fmt->audio.i_blockalign = 0x14;
+        }
+        else if( p_tk->i_extra_data > 28 )
+        {
+            uint8_t *p = p_tk->p_extra_data;
+            if( memcmp( p, ".ra", 3 ) ) {
+                msg_Err( &sys.demuxer, "Invalid Real ExtraData 0x%4.4s", (char *)p );
+                p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+            }
+            else
+            {
+                real_audio_private * priv = (real_audio_private*) p_tk->p_extra_data;
+                if( !strcmp( p_tk->psz_codec, "A_REAL/COOK" ) )
+                {
+                    p_tk->fmt.i_codec = VLC_CODEC_COOK;
+                    p_tk->fmt.audio.i_blockalign = hton16(priv->sub_packet_size);
+                }
+                else if( !strcmp( p_tk->psz_codec, "A_REAL/ATRC" ) )
+                    p_tk->fmt.i_codec = VLC_CODEC_ATRAC3;
+                else if( !strcmp( p_tk->psz_codec, "A_REAL/28_8" ) )
+                    p_tk->fmt.i_codec = VLC_CODEC_RA_288;
+                /* FIXME RALF and SIPR */
+                uint16_t version = (uint16_t) hton16(priv->version);
+                p_tk->p_sys =
+                    new Cook_PrivateTrackData( hton16(priv->sub_packet_h),
+                                               hton16(priv->frame_size),
+                                               hton16(priv->sub_packet_size));
+                if( unlikely( !p_tk->p_sys ) )
+                    return 1;
+
+                if( unlikely( p_tk->p_sys->Init() ) )
+                    return 1;
+
+                if( version == 4 )
+                {
+                    real_audio_private_v4 * v4 = (real_audio_private_v4*) priv;
+                    p_tk->fmt.audio.i_channels = hton16(v4->channels);
+                    p_tk->fmt.audio.i_bitspersample = hton16(v4->sample_size);
+                    p_tk->fmt.audio.i_rate = hton16(v4->sample_rate);
+                }
+                else if( version == 5 )
+                {
+                    real_audio_private_v5 * v5 = (real_audio_private_v5*) priv;
+                    p_tk->fmt.audio.i_channels = hton16(v5->channels);
+                    p_tk->fmt.audio.i_bitspersample = hton16(v5->sample_size);
+                    p_tk->fmt.audio.i_rate = hton16(v5->sample_rate);
+                }
+                msg_Dbg(&sys.demuxer, "%d channels %d bits %d Hz",p_tk->fmt.audio.i_channels, p_tk->fmt.audio.i_bitspersample, p_tk->fmt.audio.i_rate);
+
+                fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 78);
+            }
+        }
+    }
+    else if( !strncmp( p_tk->psz_codec, "A_QUICKTIME", 11 ) )
+    {
+        p_tk->fmt.i_cat = AUDIO_ES;
+        if ( !strncmp( p_tk->psz_codec+11, "/QDM2", 5 ) )
+            p_tk->fmt.i_codec = VLC_CODEC_QDM2;
+        else if( !strncmp( p_tk->psz_codec+11, "/QDMC", 5 ) )
+            p_tk->fmt.i_codec = VLC_FOURCC('Q','D','M','C');
+        else if( p_tk->i_extra_data >= 8)
+            p_tk->fmt.i_codec = VLC_FOURCC(p_tk->p_extra_data[4],
+                                           p_tk->p_extra_data[5],
+                                           p_tk->p_extra_data[6],
+                                           p_tk->p_extra_data[7]);
+        fill_extra_data( p_tk, 0 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_KATE;
+        p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
+
+        fill_extra_data( p_tk, 0 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
+    {
+        p_fmt->i_codec = VLC_CODEC_SUBT;
+        p_fmt->subs.psz_encoding = strdup( "ASCII" );
+    }
+    else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_SUBT;
+        p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
+    }
+    else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
+    {
+        p_tk->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
+        p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
+        if( p_tk->i_extra_data )
+            fill_extra_data( p_tk, 0 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "S_TEXT/SSA" ) ||
+             !strcmp( p_tk->psz_codec, "S_TEXT/ASS" ) ||
+             !strcmp( p_tk->psz_codec, "S_SSA" ) ||
+             !strcmp( p_tk->psz_codec, "S_ASS" ))
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_SSA;
+        p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
+        if( p_tk->i_extra_data )
+            fill_extra_data( p_tk, 0 );
+    }
+    else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_SPU;
+        p_tk->b_no_duration = true;
+        if( p_tk->i_extra_data )
+        {
+            char *psz_start;
+            char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
+            if( psz_buf != NULL )
+            {
+                memcpy( psz_buf, p_tk->p_extra_data , p_tk->i_extra_data );
+                psz_buf[p_tk->i_extra_data] = '\0';
+
+                psz_start = strstr( psz_buf, "size:" );
+                if( psz_start &&
+                    vobsub_size_parse( psz_start,
+                                       &p_tk->fmt.subs.spu.i_original_frame_width,
+                                       &p_tk->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
+                {
+                    msg_Dbg( &sys.demuxer, "original frame size vobsubs: %dx%d",
+                             p_tk->fmt.subs.spu.i_original_frame_width,
+                             p_tk->fmt.subs.spu.i_original_frame_height );
+                }
+                else
+                {
+                    msg_Warn( &sys.demuxer, "reading original frame size for vobsub failed" );
+                }
+
+                psz_start = strstr( psz_buf, "palette:" );
+                if( psz_start &&
+                    vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
+                {
+                    p_tk->fmt.subs.spu.palette[0] =  0xBeef;
+                    msg_Dbg( &sys.demuxer, "vobsub palette read" );
+                }
+                else
+                {
+                    msg_Warn( &sys.demuxer, "reading original palette failed" );
+                }
+                free( psz_buf );
+            }
+        }
+    }
+    else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
+    {
+        p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
+    }
+    else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
+    {
+        p_tk->fmt.i_cat = NAV_ES;
+    }
+    else
+    {
+        msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
+        p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+    }
+    return 0;
+}