]> git.sesse.net Git - vlc/blobdiff - modules/demux/mkv/matroska_segment.cpp
Use var_InheritString for --decklink-video-connection.
[vlc] / modules / demux / mkv / matroska_segment.cpp
index 08846787b7e9965e4f246ee0d229b6e24b11d156..977bf8474d4d8073355db292b4895b0d25943338 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * mkv.cpp : matroska demuxer
  *****************************************************************************
- * Copyright (C) 2003-2004 the VideoLAN team
+ * Copyright (C) 2003-2010 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
@@ -32,6 +32,8 @@ extern "C" {
 #include "../vobsub.h"
 }
 
+#include <vlc_codecs.h>
+
 /* GetFourCC helper */
 #define GetFOURCC( p )  __GetFOURCC( (uint8_t*)p )
 static vlc_fourcc_t __GetFOURCC( uint8_t *p )
@@ -86,13 +88,11 @@ matroska_segment_c::~matroska_segment_c()
 
 
 /*****************************************************************************
- * Tools
+ * Tools                                                                     *
+ *****************************************************************************
  *  * LoadCues : load the cues element and update index
- *
  *  * LoadTags : load ... the tags element
- *
  *  * InformationCreate : create all information, load tags if present
- *
  *****************************************************************************/
 void matroska_segment_c::LoadCues( KaxCues *cues )
 {
@@ -179,7 +179,8 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
             if( i_index >= i_index_max )
             {
                 i_index_max += 1024;
-                p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
+                p_indexes = (mkv_index_t*)xrealloc( p_indexes,
+                                        sizeof( mkv_index_t ) * i_index_max );
             }
 #undef idx
         }
@@ -193,13 +194,83 @@ void matroska_segment_c::LoadCues( KaxCues *cues )
     msg_Dbg( &sys.demuxer, "|   - loading cues done." );
 }
 
-void matroska_segment_c::LoadTags( KaxTags *tags )
+
+#define PARSE_TAG( type ) \
+    do { \
+    msg_Dbg( &sys.demuxer, "|   + " type ); \
+    ep->Down();                             \
+    while( ( el = ep->Get() ) != NULL )     \
+    {                                       \
+        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() ); \
+    }                                      \
+    ep->Up(); } while( 0 )
+
+static const struct {
+    vlc_meta_type_t type;
+    const char *key;
+} metadata_map[] = { {vlc_meta_Title,       "TITLE"},
+                     {vlc_meta_Artist,      "ARTIST"},
+                     {vlc_meta_Genre,       "GENRE"},
+                     {vlc_meta_Copyright,   "COPYRIGHT"},
+                     {vlc_meta_Description, "DESCRIPTION"},
+                     {vlc_meta_Publisher,   "PUBLISHER"},
+                     {vlc_meta_URL,         "URL"},
+                     {vlc_meta_Title,       NULL},
+};
+
+void matroska_segment_c::ParseSimpleTags( KaxTagSimple *tag )
 {
-    EbmlParser  *ep;
     EbmlElement *el;
+    EbmlParser *ep = new EbmlParser( &es, tag, &sys.demuxer );
+    char *k = NULL, *v = NULL;
+
+    if( !sys.meta )
+        sys.meta = vlc_meta_New();
+
+    msg_Dbg( &sys.demuxer, "|   + Simple Tag ");
+    while( ( el = ep->Get() ) != NULL )
+    {
+        if( MKV_IS_ID( el, KaxTagName ) )
+        {
+            KaxTagName &key = *(KaxTagName*)el;
+            key.ReadData( es.I_O(), SCOPE_ALL_DATA );
+            k = strdup( UTFstring( key ).GetUTF8().c_str() );
+        }
+        if( MKV_IS_ID( el, KaxTagString ) )
+        {
+            KaxTagString &value = *(KaxTagString*)el;
+            value.ReadData( es.I_O(), SCOPE_ALL_DATA );
+            v = strdup( UTFstring( value ).GetUTF8().c_str() );
+        }
+    }
+    delete ep;
+
+    if( !k || !v )
+    {
+        msg_Warn( &sys.demuxer, "Invalid MKV SimpleTag found.");
+        return;
+    }
 
+    for( int i = 0; metadata_map[i].key; i++ )
+    {
+        if( !strcmp( k, metadata_map[i].key ) )
+        {
+            vlc_meta_Set( sys.meta, metadata_map[i].type, v );
+            goto done;
+        }
+    }
+    vlc_meta_AddExtra( sys.meta, k, v );
+done:
+    free( k );
+    free( v );
+    return;
+}
+
+void matroska_segment_c::LoadTags( KaxTags *tags )
+{
     /* Master elements */
-    ep = new EbmlParser( &es, tags, &sys.demuxer );
+    EbmlParser *ep = new EbmlParser( &es, tags, &sys.demuxer );
+    EbmlElement *el;
 
     while( ( el = ep->Get() ) != NULL )
     {
@@ -210,55 +281,15 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
             while( ( el = ep->Get() ) != NULL )
             {
                 if( MKV_IS_ID( el, KaxTagTargets ) )
-                {
-                    msg_Dbg( &sys.demuxer, "|   + Targets" );
-                    ep->Down();
-                    while( ( el = ep->Get() ) != NULL )
-                    {
-                        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
-                    }
-                    ep->Up();
-                }
+                    PARSE_TAG( "Targets" );
                 else if( MKV_IS_ID( el, KaxTagGeneral ) )
-                {
-                    msg_Dbg( &sys.demuxer, "|   + General" );
-                    ep->Down();
-                    while( ( el = ep->Get() ) != NULL )
-                    {
-                        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
-                    }
-                    ep->Up();
-                }
+                    PARSE_TAG( "General" );
                 else if( MKV_IS_ID( el, KaxTagGenres ) )
-                {
-                    msg_Dbg( &sys.demuxer, "|   + Genres" );
-                    ep->Down();
-                    while( ( el = ep->Get() ) != NULL )
-                    {
-                        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
-                    }
-                    ep->Up();
-                }
+                    PARSE_TAG( "Genres" );
                 else if( MKV_IS_ID( el, KaxTagAudioSpecific ) )
-                {
-                    msg_Dbg( &sys.demuxer, "|   + Audio Specific" );
-                    ep->Down();
-                    while( ( el = ep->Get() ) != NULL )
-                    {
-                        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
-                    }
-                    ep->Up();
-                }
+                    PARSE_TAG( "Audio Specific" );
                 else if( MKV_IS_ID( el, KaxTagImageSpecific ) )
-                {
-                    msg_Dbg( &sys.demuxer, "|   + Images Specific" );
-                    ep->Down();
-                    while( ( el = ep->Get() ) != NULL )
-                    {
-                        msg_Dbg( &sys.demuxer, "|   |   + Unknown (%s)", typeid( *el ).name() );
-                    }
-                    ep->Up();
-                }
+                    PARSE_TAG( "Images Specific" );
                 else if( MKV_IS_ID( el, KaxTagMultiComment ) )
                 {
                     msg_Dbg( &sys.demuxer, "|   + Multi Comment" );
@@ -287,6 +318,8 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
                 {
                     msg_Dbg( &sys.demuxer, "|   + Multi Title" );
                 }
+                else if( MKV_IS_ID( el, KaxTagSimple ) )
+                    ParseSimpleTags( static_cast<KaxTagSimple*>( el ) );
                 else
                 {
                     msg_Dbg( &sys.demuxer, "|   + LoadTag Unknown (%s)", typeid( *el ).name() );
@@ -303,12 +336,14 @@ void matroska_segment_c::LoadTags( KaxTags *tags )
 
     msg_Dbg( &sys.demuxer, "loading tags done." );
 }
+#undef PARSE_TAG
 
 /*****************************************************************************
  * InformationCreate:
  *****************************************************************************/
 void matroska_segment_c::InformationCreate( )
 {
+#if 0
     sys.meta = vlc_meta_New();
 
     if( psz_title )
@@ -319,7 +354,7 @@ void matroska_segment_c::InformationCreate( )
     {
         vlc_meta_SetDate( sys.meta, psz_date_utc );
     }
-#if 0
+
     if( psz_segment_filename )
     {
         fprintf( stderr, "***** WARNING: Unhandled meta - Use custom\n" );
@@ -372,12 +407,12 @@ void matroska_segment_c::IndexAppendCluster( KaxCluster *cluster )
     if( i_index >= i_index_max )
     {
         i_index_max += 1024;
-        p_indexes = (mkv_index_t*)realloc( p_indexes, sizeof( mkv_index_t ) * i_index_max );
+        p_indexes = (mkv_index_t*)xrealloc( p_indexes,
+                                        sizeof( mkv_index_t ) * i_index_max );
     }
 #undef idx
 }
 
-
 bool matroska_segment_c::PreloadFamily( const matroska_segment_c & of_segment )
 {
     if ( b_preloaded )
@@ -410,7 +445,7 @@ bool matroska_segment_c::CompareSegmentUIDs( const matroska_segment_c * p_item_a
     p_tmp = (EbmlBinary *)p_item_a->p_next_segment_uid;
     if ( !p_tmp )
         return false;
+
     if ( p_item_b->p_segment_uid != NULL
           && *p_tmp == *p_item_b->p_segment_uid )
         return true;
@@ -506,6 +541,8 @@ bool matroska_segment_c::Preload( )
                 ;//LoadTags( static_cast<KaxTags*>( el ) );
             i_tags_position = (int64_t) es.I_O().getFilePointer();
         }
+        else if( MKV_IS_ID( el, EbmlVoid ) )
+            msg_Dbg( &sys.demuxer, "|   + Void" );
         else
             msg_Dbg( &sys.demuxer, "|   + Preload Unknown (%s)", typeid(*el).name() );
     }
@@ -585,11 +622,11 @@ bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int
             ParseChapters( static_cast<KaxChapters*>( el ) );
         i_chapters_position = i_element_position;
     }
-    else if( MKV_IS_ID( el, KaxTag ) ) // FIXME
+    else if( MKV_IS_ID( el, KaxTags ) )
     {
         msg_Dbg( &sys.demuxer, "|   + Tags" );
         if( i_tags_position < 0 )
-            ;//LoadTags( static_cast<KaxTags*>( el ) );
+            LoadTags( static_cast<KaxTags*>( el ) );
         i_tags_position = i_element_position;
     }
     else
@@ -602,11 +639,155 @@ bool matroska_segment_c::LoadSeekHeadItem( const EbmlCallbacks & ClassInfos, int
     return true;
 }
 
+void matroska_segment_c::Seek( mtime_t i_date, mtime_t i_time_offset, int64_t i_global_position )
+{
+    KaxBlock    *block;
+    KaxSimpleBlock *simpleblock;
+    int         i_track_skipping;
+    int64_t     i_block_duration;
+    size_t      i_track;
+    int64_t     i_seek_position = i_start_pos;
+    int64_t     i_seek_time = i_start_time;
+
+    if( i_global_position >= 0 )
+    {
+        /* Special case for seeking in files with no cues */
+        EbmlElement *el = NULL;
+        es.I_O().setFilePointer( i_start_pos, seek_beginning );
+        delete ep;
+        ep = new EbmlParser( &es, segment, &sys.demuxer );
+        cluster = NULL;
+
+        while( ( el = ep->Get() ) != NULL )
+        {
+            if( MKV_IS_ID( el, KaxCluster ) )
+            {
+                cluster = (KaxCluster *)el;
+                i_cluster_pos = cluster->GetElementPosition();
+                if( i_index == 0 ||
+                        ( i_index > 0 && p_indexes[i_index - 1].i_position < (int64_t)cluster->GetElementPosition() ) )
+                {
+                    IndexAppendCluster( cluster );
+                }
+                if( es.I_O().getFilePointer() >= i_global_position )
+                {
+                    ParseCluster();
+                    msg_Dbg( &sys.demuxer, "we found a cluster that is in the neighbourhood" );
+                    return;
+                }
+            }
+        }
+        msg_Err( &sys.demuxer, "This file has no cues, and we were unable to seek to the requested position by parsing." );
+        return;
+    }
+
+    if ( i_index > 0 )
+    {
+        int i_idx = 0;
+
+        for( ; i_idx < i_index; i_idx++ )
+        {
+            if( p_indexes[i_idx].i_time + i_time_offset > i_date )
+            {
+                break;
+            }
+        }
+
+        if( i_idx > 0 )
+        {
+            i_idx--;
+        }
+
+        i_seek_position = p_indexes[i_idx].i_position;
+        i_seek_time = p_indexes[i_idx].i_time;
+    }
+
+    msg_Dbg( &sys.demuxer, "seek got %"PRId64" (%d%%)",
+                i_seek_time, (int)( 100 * i_seek_position / stream_Size( sys.demuxer.s ) ) );
+
+    es.I_O().setFilePointer( i_seek_position, seek_beginning );
+
+    delete ep;
+    ep = new EbmlParser( &es, segment, &sys.demuxer );
+    cluster = NULL;
+
+    sys.i_start_pts = i_date;
+
+    /* now parse until key frame */
+    i_track_skipping = 0;
+    for( i_track = 0; i_track < tracks.size(); i_track++ )
+    {
+        if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
+        {
+            tracks[i_track]->b_search_keyframe = true;
+            i_track_skipping++;
+        }
+    }
+    es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_date );
+
+    while( i_track_skipping > 0 )
+    {
+        bool b_key_picture;
+        bool b_discardable_picture;
+        if( BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
+        {
+            msg_Warn( &sys.demuxer, "cannot get block EOF?" );
+
+            return;
+        }
+
+        for( i_track = 0; i_track < tracks.size(); i_track++ )
+        {
+            if( (simpleblock && tracks[i_track]->i_number == simpleblock->TrackNum()) ||
+                (block && tracks[i_track]->i_number == block->TrackNum()) )
+            {
+                break;
+            }
+        }
+
+        if( simpleblock )
+            sys.i_pts = (sys.i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
+        else
+            sys.i_pts = (sys.i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
+
+        if( i_track < tracks.size() )
+        {
+            if( sys.i_pts > sys.i_start_pts )
+            {
+                cluster = static_cast<KaxCluster*>(ep->UnGet( i_block_pos, i_cluster_pos ));
+                i_track_skipping = 0;
+            }
+            else if( tracks[i_track]->fmt.i_cat == VIDEO_ES )
+            {
+                if( b_key_picture && tracks[i_track]->b_search_keyframe )
+                {
+                    tracks[i_track]->b_search_keyframe = false;
+                    i_track_skipping--;
+                }
+                if( !tracks[i_track]->b_search_keyframe )
+                {
+                    BlockDecode( &sys.demuxer, block, simpleblock, sys.i_pts, 0, b_key_picture || b_discardable_picture );
+                }
+            }
+        }
+
+        delete block;
+    }
+
+    /* FIXME current ES_OUT_SET_NEXT_DISPLAY_TIME does not work that well if
+     * the delay is too high. */
+    if( sys.i_pts + 500*1000 < sys.i_start_pts )
+    {
+        sys.i_start_pts = sys.i_pts;
+
+        es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, sys.i_start_pts );
+    }
+}
+
 int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
                                              const KaxBlock *p_block, const KaxSimpleBlock *p_simpleblock )
 {
-    size_t          i_track;
-
+    size_t i_track;
     for( i_track = 0; i_track < tracks.size(); i_track++ )
     {
         const mkv_track_t *tk = tracks[i_track];
@@ -626,127 +807,74 @@ int matroska_segment_c::BlockFindTrackIndex( size_t *pi_track,
     return VLC_SUCCESS;
 }
 
-bool matroska_segment_c::Select( mtime_t i_start_time )
+static inline void fill_extra_data( mkv_track_t *p_tk, unsigned int offset )
 {
-    size_t i_track;
+    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 );
+}
 
+bool matroska_segment_c::Select( mtime_t i_start_time )
+{
     /* add all es */
     msg_Dbg( &sys.demuxer, "found %d es", (int)tracks.size() );
     sys.b_pci_packet_set = false;
 
-    for( i_track = 0; i_track < tracks.size(); i_track++ )
+    for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
     {
         mkv_track_t *p_tk = tracks[i_track];
         es_format_t *p_fmt = &p_tk->fmt;
 
-        if( tracks[i_track]->fmt.i_cat == UNKNOWN_ES )
+        if( p_fmt->i_cat == UNKNOWN_ES || !p_tk->psz_codec )
         {
-            msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, tracks[i_track]->i_number );
-            tracks[i_track]->p_es = NULL;
+            msg_Warn( &sys.demuxer, "invalid track[%d, n=%d]", (int)i_track, p_tk->i_number );
+            p_tk->p_es = NULL;
             continue;
         }
 
-        if( !strcmp( tracks[i_track]->psz_codec, "V_MS/VFW/FOURCC" ) )
+        if( !strcmp( p_tk->psz_codec, "V_MS/VFW/FOURCC" ) )
         {
-            if( tracks[i_track]->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
+            if( p_tk->i_extra_data < (int)sizeof( BITMAPINFOHEADER ) )
             {
                 msg_Err( &sys.demuxer, "missing/invalid BITMAPINFOHEADER" );
-                tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+                p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
             }
             else
             {
-                BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)tracks[i_track]->p_extra_data;
+                BITMAPINFOHEADER *p_bih = (BITMAPINFOHEADER*)p_tk->p_extra_data;
 
-                tracks[i_track]->fmt.video.i_width = GetDWLE( &p_bih->biWidth );
-                tracks[i_track]->fmt.video.i_height= GetDWLE( &p_bih->biHeight );
-                tracks[i_track]->fmt.i_codec       = GetFOURCC( &p_bih->biCompression );
+                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 );
 
-                tracks[i_track]->fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
-                if( tracks[i_track]->fmt.i_extra > 0 )
+                p_tk->fmt.i_extra       = GetDWLE( &p_bih->biSize ) - sizeof( BITMAPINFOHEADER );
+                if( p_tk->fmt.i_extra > 0 )
                 {
-                    tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
-                    memcpy( tracks[i_track]->fmt.p_extra, &p_bih[1], tracks[i_track]->fmt.i_extra );
+                    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( tracks[i_track]->psz_codec, "V_MPEG1" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "V_MPEG2" ) )
+        else if( !strcmp( p_tk->psz_codec, "V_MPEG1" ) ||
+                 !strcmp( p_tk->psz_codec, "V_MPEG2" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_MPGV;
+            p_tk->fmt.i_codec = VLC_CODEC_MPGV;
+            if( p_tk->i_extra_data )
+                fill_extra_data( p_tk, 0 );
         }
-        else if( !strncmp( tracks[i_track]->psz_codec, "V_THEORA", 8 ) )
+        else if( !strncmp( p_tk->psz_codec, "V_THEORA", 8 ) )
         {
-            uint8_t *p_data = tracks[i_track]->p_extra_data;
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_THEORA;
-            if( tracks[i_track]->i_extra_data >= 4 ) {
-                if( p_data[0] == 2 ) {
-                    int i = 1;
-                    int i_size1 = 0, i_size2 = 0;
-                    p_data++;
-                    /* read size of first header packet */
-                    while( *p_data == 0xFF &&
-                           i < tracks[i_track]->i_extra_data )
-                    {
-                        i_size1 += *p_data;
-                        p_data++;
-                        i++;
-                    }
-                    i_size1 += *p_data;
-                    p_data++;
-                    i++;
-                    msg_Dbg( &sys.demuxer, "first theora header size %d", i_size1 );
-                    /* read size of second header packet */
-                    while( *p_data == 0xFF &&
-                           i < tracks[i_track]->i_extra_data )
-                    {
-                        i_size2 += *p_data;
-                        p_data++;
-                        i++;
-                    }
-                    i_size2 += *p_data;
-                    p_data++;
-                    i++;
-                    int i_size3 = tracks[i_track]->i_extra_data - i - i_size1
-                        - i_size2;
-                    msg_Dbg( &sys.demuxer, "second theora header size %d", i_size2 );
-                    msg_Dbg( &sys.demuxer, "third theora header size %d", i_size3 );
-                    tracks[i_track]->fmt.i_extra = i_size1 + i_size2 + i_size3
-                        + 6;
-                    if( i_size1 > 0 && i_size2 > 0 && i_size3 > 0  ) {
-                        tracks[i_track]->fmt.p_extra =
-                            malloc( tracks[i_track]->fmt.i_extra );
-                        uint8_t *p_out = (uint8_t*)tracks[i_track]->fmt.p_extra;
-                        *p_out++ = (i_size1>>8) & 0xFF;
-                        *p_out++ = i_size1 & 0xFF;
-                        memcpy( p_out, p_data, i_size1 );
-                        p_data += i_size1;
-                        p_out += i_size1;
-                        *p_out++ = (i_size2>>8) & 0xFF;
-                        *p_out++ = i_size2 & 0xFF;
-                        memcpy( p_out, p_data, i_size2 );
-                        p_data += i_size2;
-                        p_out += i_size2;
-
-                        *p_out++ = (i_size3>>8) & 0xFF;
-                        *p_out++ = i_size3 & 0xFF;
-                        memcpy( p_out, p_data, i_size3 );
-                        p_data += i_size3;
-                        p_out += i_size3;
-                    }
-                    else
-                    {
-                        msg_Err( &sys.demuxer, "inconsistant theora extradata" );
-                    }
-                }
-                else {
-                    msg_Err( &sys.demuxer, "Wrong number of ogg packets with theora headers (%d)", p_data[0] + 1 );
-                }
-            }
+            p_tk->fmt.i_codec = VLC_CODEC_THEORA;
+            fill_extra_data( p_tk, 0 );
+            p_tk->b_pts_only = true;
         }
-        else if( !strncmp( tracks[i_track]->psz_codec, "V_REAL/RV", 9 ) )
+        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" ) )
@@ -756,55 +884,61 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
             else if( !strcmp( p_tk->psz_codec, "V_REAL/RV40" ) )
                 p_fmt->i_codec = VLC_CODEC_RV40;
 
-            if( p_tk->i_extra_data > 26 )
+            /* 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_fmt->p_extra = malloc( p_tk->i_extra_data - 26 );
-                if( p_fmt->p_extra )
-                {
-                    p_fmt->i_extra = p_tk->i_extra_data - 26;
-                    memcpy( p_fmt->p_extra, &p_tk->p_extra_data[26], p_fmt->i_extra );
-                }
+                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( tracks[i_track]->psz_codec, "V_DIRAC", 7 ) )
+        else if( !strncmp( p_tk->psz_codec, "V_DIRAC", 7 ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_DIRAC;
+            p_tk->fmt.i_codec = VLC_CODEC_DIRAC;
         }
-        else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4", 7 ) )
+        else if( !strncmp( p_tk->psz_codec, "V_VP8", 5 ) )
         {
-            if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/MS/V3" ) )
+            p_tk->fmt.i_codec = VLC_CODEC_VP8;
+            p_tk->b_pts_only = true;
+        }
+        else if( !strncmp( p_tk->psz_codec, "V_MPEG4", 7 ) )
+        {
+            if( !strcmp( p_tk->psz_codec, "V_MPEG4/MS/V3" ) )
             {
-                tracks[i_track]->fmt.i_codec = VLC_CODEC_DIV3;
+                p_tk->fmt.i_codec = VLC_CODEC_DIV3;
             }
-            else if( !strncmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO", 11 ) )
+            else if( !strncmp( p_tk->psz_codec, "V_MPEG4/ISO", 11 ) )
             {
                 /* A MPEG 4 codec, SP, ASP, AP or AVC */
-                if( !strcmp( tracks[i_track]->psz_codec, "V_MPEG4/ISO/AVC" ) )
-                    tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
+                if( !strcmp( p_tk->psz_codec, "V_MPEG4/ISO/AVC" ) )
+                    p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'v', 'c', '1' );
                 else
-                    tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4V;
-                tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
-                tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
-                memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
+                    p_tk->fmt.i_codec = VLC_CODEC_MP4V;
+                fill_extra_data( p_tk, 0 );
             }
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "V_QUICKTIME" ) )
+        else if( !strcmp( p_tk->psz_codec, "V_QUICKTIME" ) )
         {
-            MP4_Box_t *p_box = (MP4_Box_t*)malloc( sizeof( MP4_Box_t ) );
+            MP4_Box_t *p_box = (MP4_Box_t*)xmalloc( sizeof( MP4_Box_t ) );
             stream_t *p_mp4_stream = stream_MemoryNew( VLC_OBJECT(&sys.demuxer),
-                                                       tracks[i_track]->p_extra_data,
-                                                       tracks[i_track]->i_extra_data,
+                                                       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 ) )
             {
-                tracks[i_track]->fmt.i_codec = p_box->i_type;
-                tracks[i_track]->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
-                tracks[i_track]->fmt.video.i_height = p_box->data.p_sample_vide->i_height;
-                tracks[i_track]->fmt.i_extra = p_box->data.p_sample_vide->i_qt_image_description;
-                tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
-                memcpy( tracks[i_track]->fmt.p_extra, p_box->data.p_sample_vide->p_qt_image_description, tracks[i_track]->fmt.i_extra );
+                p_tk->fmt.i_codec = p_box->i_type;
+                p_tk->fmt.video.i_width = p_box->data.p_sample_vide->i_width;
+                p_tk->fmt.video.i_height = p_box->data.p_sample_vide->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
@@ -813,132 +947,101 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
             }
             stream_Delete( p_mp4_stream );
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_MS/ACM" ) )
+        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( tracks[i_track]->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
+            if( p_tk->i_extra_data < (int)sizeof( WAVEFORMATEX ) )
             {
                 msg_Err( &sys.demuxer, "missing/invalid WAVEFORMATEX" );
-                tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+                p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
             }
             else
             {
-                WAVEFORMATEX *p_wf = (WAVEFORMATEX*)tracks[i_track]->p_extra_data;
+                WAVEFORMATEX *p_wf = (WAVEFORMATEX*)p_tk->p_extra_data;
 
-                wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &tracks[i_track]->fmt.i_codec, NULL );
+                wf_tag_to_fourcc( GetWLE( &p_wf->wFormatTag ), &p_tk->fmt.i_codec, NULL );
 
-                tracks[i_track]->fmt.audio.i_channels   = GetWLE( &p_wf->nChannels );
-                tracks[i_track]->fmt.audio.i_rate = GetDWLE( &p_wf->nSamplesPerSec );
-                tracks[i_track]->fmt.i_bitrate    = GetDWLE( &p_wf->nAvgBytesPerSec ) * 8;
-                tracks[i_track]->fmt.audio.i_blockalign = GetWLE( &p_wf->nBlockAlign );;
-                tracks[i_track]->fmt.audio.i_bitspersample = GetWLE( &p_wf->wBitsPerSample );
+                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 );
 
-                tracks[i_track]->fmt.i_extra            = GetWLE( &p_wf->cbSize );
-                if( tracks[i_track]->fmt.i_extra > 0 )
+                p_tk->fmt.i_extra            = GetWLE( &p_wf->cbSize );
+                if( p_tk->fmt.i_extra > 0 )
                 {
-                    tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
-                    memcpy( tracks[i_track]->fmt.p_extra, &p_wf[1], tracks[i_track]->fmt.i_extra );
+                    p_tk->fmt.p_extra = xmalloc( p_tk->fmt.i_extra );
+                    memcpy( p_tk->fmt.p_extra, &p_wf[1], p_tk->fmt.i_extra );
                 }
             }
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L3" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L2" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "A_MPEG/L1" ) )
+        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" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_MPGA;
+            p_tk->fmt.i_codec = VLC_CODEC_MPGA;
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_AC3" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_AC3" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_A52;
+            p_tk->fmt.i_codec = VLC_CODEC_A52;
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_EAC3" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_EAC3" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_EAC3;
+            p_tk->fmt.i_codec = VLC_CODEC_EAC3;
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_DTS" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_DTS" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_DTS;
+            p_tk->fmt.i_codec = VLC_CODEC_DTS;
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_MLP" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_MLP" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_MLP;
+            p_tk->fmt.i_codec = VLC_CODEC_MLP;
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_TRUEHD" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_TRUEHD" ) )
         {
             /* FIXME when more samples arrive */
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_TRUEHD;
+            p_tk->fmt.i_codec = VLC_CODEC_TRUEHD;
             p_fmt->b_packetized = false;
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_FLAC" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_FLAC" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_FLAC;
-            tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
-            tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
-            memcpy( tracks[i_track]->fmt.p_extra,tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
+            p_tk->fmt.i_codec = VLC_CODEC_FLAC;
+            fill_extra_data( p_tk, 0 );
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_VORBIS" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_VORBIS" ) )
         {
-            int i, i_offset = 1, i_size[3], i_extra;
-            uint8_t *p_extra;
-
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_VORBIS;
-
-            /* Split the 3 headers */
-            if( tracks[i_track]->p_extra_data[0] != 0x02 )
-                msg_Err( &sys.demuxer, "invalid vorbis header" );
-
-            for( i = 0; i < 2; i++ )
-            {
-                i_size[i] = 0;
-                while( i_offset < tracks[i_track]->i_extra_data )
-                {
-                    i_size[i] += tracks[i_track]->p_extra_data[i_offset];
-                    if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
-                }
-            }
-
-            i_size[0] = __MIN(i_size[0], tracks[i_track]->i_extra_data - i_offset);
-            i_size[1] = __MIN(i_size[1], tracks[i_track]->i_extra_data -i_offset -i_size[0]);
-            i_size[2] = tracks[i_track]->i_extra_data - i_offset - i_size[0] - i_size[1];
-
-            tracks[i_track]->fmt.i_extra = 3 * 2 + i_size[0] + i_size[1] + i_size[2];
-            tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
-            p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra; i_extra = 0;
-            for( i = 0; i < 3; i++ )
-            {
-                *(p_extra++) = i_size[i] >> 8;
-                *(p_extra++) = i_size[i] & 0xFF;
-                memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra,
-                        i_size[i] );
-                p_extra += i_size[i];
-                i_extra += i_size[i];
-            }
+            p_tk->fmt.i_codec = VLC_CODEC_VORBIS;
+            fill_extra_data( p_tk, 0 );
         }
-        else if( !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG2/", strlen( "A_AAC/MPEG2/" ) ) ||
-                 !strncmp( tracks[i_track]->psz_codec, "A_AAC/MPEG4/", strlen( "A_AAC/MPEG4/" ) ) )
+        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
+                    16000, 12000, 11025,  8000,  7350,     0,     0,     0
             };
 
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4A;
+            p_tk->fmt.i_codec = VLC_CODEC_MP4A;
             /* create data for faad (MP4DecSpecificDescrTag)*/
 
-            if( !strcmp( &tracks[i_track]->psz_codec[12], "MAIN" ) )
+            if( !strcmp( &p_tk->psz_codec[12], "MAIN" ) )
             {
                 i_profile = 0;
             }
-            else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC" ) )
+            else if( !strcmp( &p_tk->psz_codec[12], "LC" ) )
             {
                 i_profile = 1;
             }
-            else if( !strcmp( &tracks[i_track]->psz_codec[12], "SSR" ) )
+            else if( !strcmp( &p_tk->psz_codec[12], "SSR" ) )
             {
                 i_profile = 2;
             }
-            else if( !strcmp( &tracks[i_track]->psz_codec[12], "LC/SBR" ) )
+            else if( !strcmp( &p_tk->psz_codec[12], "LC/SBR" ) )
             {
                 i_profile = 1;
                 sbr = 1;
@@ -950,171 +1053,151 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
 
             for( i_srate = 0; i_srate < 13; i_srate++ )
             {
-                if( i_sample_rates[i_srate] == tracks[i_track]->i_original_rate )
+                if( i_sample_rates[i_srate] == p_tk->i_original_rate )
                 {
                     break;
                 }
             }
             msg_Dbg( &sys.demuxer, "profile=%d srate=%d", i_profile, i_srate );
 
-            tracks[i_track]->fmt.i_extra = sbr ? 5 : 2;
-            tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
-            ((uint8_t*)tracks[i_track]->fmt.p_extra)[0] = ((i_profile + 1) << 3) | ((i_srate&0xe) >> 1);
-            ((uint8_t*)tracks[i_track]->fmt.p_extra)[1] = ((i_srate & 0x1) << 7) | (tracks[i_track]->fmt.audio.i_channels << 3);
+            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] == tracks[i_track]->fmt.audio.i_rate )
+                    if( i_sample_rates[iDSRI] == p_tk->fmt.audio.i_rate )
                         break;
-                ((uint8_t*)tracks[i_track]->fmt.p_extra)[2] = (syncExtensionType >> 3) & 0xFF;
-                ((uint8_t*)tracks[i_track]->fmt.p_extra)[3] = ((syncExtensionType & 0x7) << 5) | 5;
-                ((uint8_t*)tracks[i_track]->fmt.p_extra)[4] = ((1 & 0x1) << 7) | (iDSRI << 3);
+                ((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( tracks[i_track]->psz_codec, "A_AAC" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_AAC" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_MP4A;
-            tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
-            tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
-            memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
+            p_tk->fmt.i_codec = VLC_CODEC_MP4A;
+            fill_extra_data( p_tk, 0 );
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_WAVPACK4" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_WAVPACK4" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_WAVPACK;
-            tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
-            tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
-            memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
+            p_tk->fmt.i_codec = VLC_CODEC_WAVPACK;
+            fill_extra_data( p_tk, 0 );
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "A_TTA1" ) )
+        else if( !strcmp( p_tk->psz_codec, "A_TTA1" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_TTA;
-            tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
-            tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
-            memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
+            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( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "A_PCM/INT/LIT" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "A_PCM/FLOAT/IEEE" ) )
+        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( tracks[i_track]->psz_codec, "A_PCM/INT/BIG" ) )
+            if( !strcmp( p_tk->psz_codec, "A_PCM/INT/BIG" ) )
             {
-                tracks[i_track]->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
+                p_tk->fmt.i_codec = VLC_FOURCC( 't', 'w', 'o', 's' );
             }
             else
             {
-                tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
+                p_tk->fmt.i_codec = VLC_FOURCC( 'a', 'r', 'a', 'w' );
             }
-            tracks[i_track]->fmt.audio.i_blockalign = ( tracks[i_track]->fmt.audio.i_bitspersample + 7 ) / 8 * tracks[i_track]->fmt.audio.i_channels;
+            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/COOK" ) )
+                p_tk->fmt.i_codec = VLC_CODEC_COOK;
+            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 14_4, RALF and SIPR */
+            fill_extra_data( p_tk, p_tk->fmt.i_codec == VLC_CODEC_RA_288 ? 0 : 0 /*78 - FIXME need to implement reading support for cook */ );
+        }
+        else 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;
         }
         /* disabled due to the potential "S_KATE" namespace issue */
-        else if( !strcmp( tracks[i_track]->psz_codec, "S_KATE" ) )
+        else if( !strcmp( p_tk->psz_codec, "S_KATE" ) )
         {
             int i, i_offset = 1, i_extra, num_headers, size_so_far;
             uint8_t *p_extra;
 
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_KATE;
-            tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
-
-            /* Recover the number of headers to expect */
-            num_headers = tracks[i_track]->p_extra_data[0]+1;
-            msg_Dbg( &sys.demuxer, "kate in mkv detected: %d headers in %u bytes",
-                num_headers, tracks[i_track]->i_extra_data);
-
-            /* this won't overflow the stack as is can allocate only 1020 bytes max */
-            uint16_t pi_size[num_headers];
-
-            /* Split the headers */
-            size_so_far = 0;
-            for( i = 0; i < num_headers-1; i++ )
-            {
-                pi_size[i] = 0;
-                while( i_offset < tracks[i_track]->i_extra_data )
-                {
-                    pi_size[i] += tracks[i_track]->p_extra_data[i_offset];
-                    if( tracks[i_track]->p_extra_data[i_offset++] != 0xff ) break;
-                }
-                msg_Dbg( &sys.demuxer, "kate header %d is %d bytes", i, pi_size[i]);
-                size_so_far += pi_size[i];
-            }
-            pi_size[num_headers-1] = tracks[i_track]->i_extra_data - (size_so_far+i_offset);
-            msg_Dbg( &sys.demuxer, "kate last header (%d) is %d bytes", num_headers-1, pi_size[num_headers-1]);
-
-            tracks[i_track]->fmt.i_extra = 1 + num_headers * 2 + size_so_far + pi_size[num_headers-1];
-            tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->fmt.i_extra );
-
-            p_extra = (uint8_t *)tracks[i_track]->fmt.p_extra;
-            i_extra = 0;
-            *(p_extra++) = num_headers;
-            ++i_extra;
-            for( i = 0; i < num_headers; i++ )
-            {
-                *(p_extra++) = pi_size[i] >> 8;
-                *(p_extra++) = pi_size[i] & 0xFF;
-                memcpy( p_extra, tracks[i_track]->p_extra_data + i_offset + i_extra-1,
-                        pi_size[i] );
+            p_tk->fmt.i_codec = VLC_CODEC_KATE;
+            p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
 
-                p_extra += pi_size[i];
-                i_extra += pi_size[i];
-            }
+            fill_extra_data( p_tk, 0 );
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASCII" ) )
+        else if( !strcmp( p_tk->psz_codec, "S_TEXT/ASCII" ) )
         {
             p_fmt->i_codec = VLC_CODEC_SUBT;
             p_fmt->subs.psz_encoding = NULL; /* Is there a place where it is stored ? */
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/UTF8" ) )
+        else if( !strcmp( p_tk->psz_codec, "S_TEXT/UTF8" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_SUBT;
-            tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
+            p_tk->fmt.i_codec = VLC_CODEC_SUBT;
+            p_tk->fmt.subs.psz_encoding = strdup( "UTF-8" );
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "S_TEXT/USF" ) )
+        else if( !strcmp( p_tk->psz_codec, "S_TEXT/USF" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 's', 'f', ' ' );
-            tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
-            if( tracks[i_track]->i_extra_data )
-            {
-                tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
-                tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
-                memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
-            }
+            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( tracks[i_track]->psz_codec, "S_TEXT/SSA" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "S_TEXT/ASS" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "S_SSA" ) ||
-                 !strcmp( tracks[i_track]->psz_codec, "S_ASS" ))
+        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" ))
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_SSA;
-            tracks[i_track]->fmt.subs.psz_encoding = strdup( "UTF-8" );
-            if( tracks[i_track]->i_extra_data )
-            {
-                tracks[i_track]->fmt.i_extra = tracks[i_track]->i_extra_data;
-                tracks[i_track]->fmt.p_extra = malloc( tracks[i_track]->i_extra_data );
-                memcpy( tracks[i_track]->fmt.p_extra, tracks[i_track]->p_extra_data, tracks[i_track]->i_extra_data );
-            }
+            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( tracks[i_track]->psz_codec, "S_VOBSUB" ) )
+        else if( !strcmp( p_tk->psz_codec, "S_VOBSUB" ) )
         {
-            tracks[i_track]->fmt.i_codec = VLC_CODEC_SPU;
-            if( tracks[i_track]->i_extra_data )
+            p_tk->fmt.i_codec = VLC_CODEC_SPU;
+            if( p_tk->i_extra_data )
             {
                 char *psz_start;
-                char *psz_buf = (char *)malloc( tracks[i_track]->i_extra_data + 1);
+                char *psz_buf = (char *)malloc( p_tk->i_extra_data + 1);
                 if( psz_buf != NULL )
                 {
-                    memcpy( psz_buf, tracks[i_track]->p_extra_data , tracks[i_track]->i_extra_data );
-                    psz_buf[tracks[i_track]->i_extra_data] = '\0';
+                    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,
-                                           &tracks[i_track]->fmt.subs.spu.i_original_frame_width,
-                                           &tracks[i_track]->fmt.subs.spu.i_original_frame_height ) == VLC_SUCCESS )
+                                           &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",
-                                 tracks[i_track]->fmt.subs.spu.i_original_frame_width,
-                                 tracks[i_track]->fmt.subs.spu.i_original_frame_height );
+                                 p_tk->fmt.subs.spu.i_original_frame_width,
+                                 p_tk->fmt.subs.spu.i_original_frame_height );
                     }
                     else
                     {
@@ -1123,9 +1206,9 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
 
                     psz_start = strstr( psz_buf, "palette:" );
                     if( psz_start &&
-                        vobsub_palette_parse( psz_start, &tracks[i_track]->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
+                        vobsub_palette_parse( psz_start, &p_tk->fmt.subs.spu.palette[1] ) == VLC_SUCCESS )
                     {
-                        tracks[i_track]->fmt.subs.spu.palette[0] =  0xBeef;
+                        p_tk->fmt.subs.spu.palette[0] =  0xBeef;
                         msg_Dbg( &sys.demuxer, "vobsub palette read" );
                     }
                     else
@@ -1136,44 +1219,41 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
                 }
             }
         }
-        else if( !strcmp( tracks[i_track]->psz_codec, "B_VOBBTN" ) )
+        else if( !strcmp( p_tk->psz_codec, "S_HDMV/PGS" ) )
         {
-            tracks[i_track]->fmt.i_cat = NAV_ES;
-            continue;
+            p_tk->fmt.i_codec = VLC_CODEC_BD_PG;
         }
-        else if( !strcmp( p_tk->psz_codec, "A_REAL/14_4" ) )
+        else if( !strcmp( p_tk->psz_codec, "B_VOBBTN" ) )
         {
-            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;
+            p_tk->fmt.i_cat = NAV_ES;
+            continue;
         }
         else
         {
-            msg_Err( &sys.demuxer, "unknown codec id=`%s'", tracks[i_track]->psz_codec );
-            tracks[i_track]->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
+            msg_Err( &sys.demuxer, "unknown codec id=`%s'", p_tk->psz_codec );
+            p_tk->fmt.i_codec = VLC_FOURCC( 'u', 'n', 'd', 'f' );
         }
-        if( tracks[i_track]->b_default )
+        if( p_tk->b_default )
         {
-            tracks[i_track]->fmt.i_priority = 1000;
+            p_tk->fmt.i_priority = 1000;
         }
 
-        tracks[i_track]->p_es = es_out_Add( sys.demuxer.out, &tracks[i_track]->fmt );
+        p_tk->p_es = es_out_Add( sys.demuxer.out, &p_tk->fmt );
 
         /* Turn on a subtitles track if it has been flagged as default -
          * but only do this if no subtitles track has already been engaged,
          * either by an earlier 'default track' (??) or by default
          * language choice behaviour.
          */
-        if( tracks[i_track]->b_default )
+        if( p_tk->b_default )
         {
             es_out_Control( sys.demuxer.out,
                             ES_OUT_SET_ES_DEFAULT,
-                            tracks[i_track]->p_es );
+                            p_tk->p_es );
         }
     }
     es_out_Control( sys.demuxer.out, ES_OUT_SET_NEXT_DISPLAY_TIME, i_start_time );
+
     sys.i_start_pts = i_start_time;
     // reset the stream reading to the first cluster of the segment used
     es.I_O().setFilePointer( i_start_pos );
@@ -1186,9 +1266,7 @@ bool matroska_segment_c::Select( mtime_t i_start_time )
 
 void matroska_segment_c::UnSelect( )
 {
-    size_t i_track;
-
-    for( i_track = 0; i_track < tracks.size(); i_track++ )
+    for( size_t i_track = 0; i_track < tracks.size(); i_track++ )
     {
         if ( tracks[i_track]->p_es != NULL )
         {
@@ -1201,12 +1279,13 @@ void matroska_segment_c::UnSelect( )
     ep = NULL;
 }
 
-int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, int64_t *pi_ref1, int64_t *pi_ref2, int64_t *pi_duration )
+int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_simpleblock, bool *pb_key_picture, bool *pb_discardable_picture, int64_t *pi_duration )
 {
     pp_simpleblock = NULL;
     pp_block = NULL;
-    *pi_ref1  = 0;
-    *pi_ref2  = 0;
+
+    *pb_key_picture         = true;
+    *pb_discardable_picture = false;
 
     for( ;; )
     {
@@ -1226,6 +1305,11 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
                 pp_block = NULL;
                 continue;
             }
+            if( pp_simpleblock != NULL )
+            {
+                *pb_key_picture         = pp_simpleblock->IsKeyframe();
+                *pb_discardable_picture = pp_simpleblock->IsDiscardable();
+            }
 
             /* update the index */
 #define idx p_indexes[i_index - 1]
@@ -1235,7 +1319,7 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
                     idx.i_time        = pp_simpleblock->GlobalTimecode() / (mtime_t)1000;
                 else
                     idx.i_time        = (*pp_block).GlobalTimecode() / (mtime_t)1000;
-                idx.b_key         = *pi_ref1 == 0 ? true : false;
+                idx.b_key         = *pb_key_picture;
             }
 #undef idx
             return VLC_SUCCESS;
@@ -1349,14 +1433,11 @@ int matroska_segment_c::BlockGet( KaxBlock * & pp_block, KaxSimpleBlock * & pp_s
                 KaxReferenceBlock &ref = *(KaxReferenceBlock*)el;
 
                 ref.ReadData( es.I_O() );
-                if( *pi_ref1 == 0 )
-                {
-                    *pi_ref1 = int64( ref ) * cluster->GlobalTimecodeScale();
-                }
-                else if( *pi_ref2 == 0 )
-                {
-                    *pi_ref2 = int64( ref ) * cluster->GlobalTimecodeScale();
-                }
+
+                if( *pb_key_picture )
+                    *pb_key_picture = false;
+                else if( int64( ref ) > 0 )
+                    *pb_discardable_picture = true;
             }
             else if( MKV_IS_ID( el, KaxClusterSilentTrackNumber ) )
             {