]> git.sesse.net Git - vlc/blobdiff - modules/demux/mkv/mkv.cpp
MKV: avoid using the last_dts of a track we know is not used anymore
[vlc] / modules / demux / mkv / mkv.cpp
index c56fe0bcbd8bc14aadab0c28ca43f40cec020898..5e183b9912f6c857ac1b60bb2608f9d56f19d8ed 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * mkv.cpp : matroska demuxer
  *****************************************************************************
- * Copyright (C) 2003-2004 the VideoLAN team
+ * Copyright (C) 2003-2005, 2008, 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 "stream_io_callback.hpp"
 
+extern "C" {
+#include "../../modules/codec/dts_header.h"
+}
+
+#include <vlc_fs.h>
+#include <vlc_url.h>
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -42,40 +49,39 @@ static void Close( vlc_object_t * );
 vlc_module_begin ()
     set_shortname( "Matroska" )
     set_description( N_("Matroska stream demuxer" ) )
-    set_capability( "demux", 0 )
+    set_capability( "demux", 50 )
     set_callbacks( Open, Close )
     set_category( CAT_INPUT )
     set_subcategory( SUBCAT_INPUT_DEMUX )
 
-    add_bool( "mkv-use-ordered-chapters", 1, NULL,
-            N_("Ordered chapters"),
-            N_("Play ordered chapters as specified in the segment."), true );
+    add_bool( "mkv-use-ordered-chapters", true,
+            N_("Respect ordered chapters"),
+            N_("Play chapters in the order specified in the segment."), false );
 
-    add_bool( "mkv-use-chapter-codec", 1, NULL,
+    add_bool( "mkv-use-chapter-codec", true,
             N_("Chapter codecs"),
             N_("Use chapter codecs found in the segment."), true );
 
-    add_bool( "mkv-preload-local-dir", 0, NULL,
-            N_("Preload Directory"),
-            N_("Preload matroska files from the same family in the same directory (not good for broken files)."), true );
+    add_bool( "mkv-preload-local-dir", true,
+            N_("Preload MKV files in the same directory"),
+            N_("Preload matroska files in the same directory to find linked segments (not good for broken files)."), false );
 
-    add_bool( "mkv-seek-percent", 0, NULL,
+    add_bool( "mkv-seek-percent", false,
             N_("Seek based on percent not time"),
             N_("Seek based on percent not time."), true );
 
-    add_bool( "mkv-use-dummy", 0, NULL,
+    add_bool( "mkv-use-dummy", false,
             N_("Dummy Elements"),
             N_("Read and discard unknown EBML elements (not good for broken files)."), true );
 
-    add_shortcut( "mka" )
-    add_shortcut( "mkv" )
+    add_shortcut( "mka", "mkv" )
 vlc_module_end ()
 
-class demux_sys_t;
+struct demux_sys_t;
 
 static int  Demux  ( demux_t * );
 static int  Control( demux_t *, int, va_list );
-static void Seek   ( demux_t *, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter );
+static void Seek   ( demux_t *, mtime_t i_date, double f_percent, virtual_chapter_c *p_chapter );
 
 /*****************************************************************************
  * Open: initializes matroska demux structures
@@ -90,6 +96,7 @@ static int Open( vlc_object_t * p_this )
     std::string         s_path, s_filename;
     vlc_stream_io_callback *p_io_callback;
     EbmlStream         *p_io_stream;
+    bool                b_need_preload = false;
 
     /* peek the begining */
     if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
@@ -117,17 +124,18 @@ static int Open( vlc_object_t * p_this )
     p_stream = p_sys->AnalyseAllSegmentsFound( p_demux, p_io_stream, true );
     if( p_stream == NULL )
     {
-        msg_Err( p_demux, "cannot find KaxSegment" );
+        msg_Err( p_demux, "cannot find KaxSegment or missing mandatory KaxInfo" );
         goto error;
     }
     p_sys->streams.push_back( p_stream );
 
-    p_stream->p_in = p_io_callback;
-    p_stream->p_es = p_io_stream;
+    p_stream->p_io_callback = p_io_callback;
+    p_stream->p_estream = p_io_stream;
 
     for (size_t i=0; i<p_stream->segments.size(); i++)
     {
         p_stream->segments[i]->Preload();
+        b_need_preload |= p_stream->segments[i]->b_ref_external_segments;
     }
 
     p_segment = p_stream->segments[0];
@@ -137,14 +145,15 @@ static int Open( vlc_object_t * p_this )
         goto error;
     }
 
-    if (config_GetInt( p_demux, "mkv-preload-local-dir" ))
+    if (b_need_preload && var_InheritBool( p_demux, "mkv-preload-local-dir" ))
     {
+        msg_Dbg( p_demux, "Preloading local dir" );
         /* get the files from the same dir from the same family (based on p_demux->psz_path) */
-        if (p_demux->psz_path[0] != '\0' && !strcmp(p_demux->psz_access, ""))
+        if ( p_demux->psz_file && !strcmp( p_demux->psz_access, "file" ) )
         {
             // assume it's a regular file
             // get the directory path
-            s_path = p_demux->psz_path;
+            s_path = p_demux->psz_file;
             if (s_path.at(s_path.length() - 1) == DIR_SEP_CHAR)
             {
                 s_path = s_path.substr(0,s_path.length()-1);
@@ -157,41 +166,37 @@ static int Open( vlc_object_t * p_this )
                 }
             }
 
-            DIR *p_src_dir = utf8_opendir(s_path.c_str());
+            DIR *p_src_dir = vlc_opendir(s_path.c_str());
 
             if (p_src_dir != NULL)
             {
-                char *psz_file;
-                while ((psz_file = utf8_readdir(p_src_dir)) != NULL)
+                const char *psz_file;
+                while ((psz_file = vlc_readdir(p_src_dir)) != NULL)
                 {
                     if (strlen(psz_file) > 4)
                     {
                         s_filename = s_path + DIR_SEP_CHAR + psz_file;
 
-#ifdef WIN32
-                        if (!strcasecmp(s_filename.c_str(), p_demux->psz_path))
+#if defined(_WIN32) || defined(__OS2__)
+                        if (!strcasecmp(s_filename.c_str(), p_demux->psz_file))
 #else
-                        if (!s_filename.compare(p_demux->psz_path))
+                        if (!s_filename.compare(p_demux->psz_file))
 #endif
                         {
-                            free (psz_file);
                             continue; // don't reuse the original opened file
                         }
 
-#if defined(__GNUC__) && (__GNUC__ < 3)
-                        if (!s_filename.compare("mkv", s_filename.length() - 3, 3) ||
-                            !s_filename.compare("mka", s_filename.length() - 3, 3))
-#else
                         if (!s_filename.compare(s_filename.length() - 3, 3, "mkv") ||
                             !s_filename.compare(s_filename.length() - 3, 3, "mka"))
-#endif
                         {
-                            // test wether this file belongs to our family
+                            // test whether this file belongs to our family
                             const uint8_t *p_peek;
                             bool          file_ok = false;
+#warning Memory leak!
+                            std::string   s_url = vlc_path2uri( s_filename.c_str(), "file" );
                             stream_t      *p_file_stream = stream_UrlNew(
                                                             p_demux,
-                                                            s_filename.c_str());
+                                                            s_url.c_str() );
                             /* peek the begining */
                             if( p_file_stream &&
                                 stream_Peek( p_file_stream, &p_peek, 4 ) >= 4
@@ -213,8 +218,8 @@ static int Open( vlc_object_t * p_this )
                                 }
                                 else
                                 {
-                                    p_stream->p_in = p_file_io;
-                                    p_stream->p_es = p_estream;
+                                    p_stream->p_io_callback = p_file_io;
+                                    p_stream->p_estream = p_estream;
                                     p_sys->streams.push_back( p_stream );
                                 }
                             }
@@ -227,7 +232,6 @@ static int Open( vlc_object_t * p_this )
                             }
                         }
                     }
-                    free (psz_file);
                 }
                 closedir( p_src_dir );
             }
@@ -235,17 +239,20 @@ static int Open( vlc_object_t * p_this )
 
         p_sys->PreloadFamily( *p_segment );
     }
+    else if (b_need_preload)
+        msg_Warn( p_demux, "This file references other files, you may want to enable the preload of local directory");
 
-    p_sys->PreloadLinked( p_segment );
-
-    if ( !p_sys->PreparePlayback( NULL ) )
+    if ( !p_sys->PreloadLinked() ||
+         !p_sys->PreparePlayback( NULL ) )
     {
         msg_Err( p_demux, "cannot use the segment" );
         goto error;
     }
 
-    p_sys->StartUiThread();
+    p_sys->FreeUnused();
+
+    p_sys->InitUi();
+
     return VLC_SUCCESS;
 
 error:
@@ -260,6 +267,13 @@ static void Close( vlc_object_t *p_this )
 {
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys   = p_demux->p_sys;
+    virtual_segment_c *p_vsegment = p_sys->p_current_segment;
+    if( p_vsegment )
+    {
+        matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
+        if( p_segment )
+            p_segment->UnSelect();
+    }
 
     delete p_sys;
 }
@@ -270,7 +284,7 @@ static void Close( vlc_object_t *p_this )
 static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t        *p_sys = p_demux->p_sys;
-    int64_t     *pi64;
+    int64_t     *pi64, i64;
     double      *pf, f;
     int         i_skp;
     size_t      i_idx;
@@ -289,15 +303,15 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                 return VLC_EGENERIC;
 
             *pi_int = p_sys->stored_attachments.size();
-            *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t**) *
+            *ppp_attach = (input_attachment_t**)malloc( sizeof(input_attachment_t*) *
                                                         p_sys->stored_attachments.size() );
             if( !(*ppp_attach) )
                 return VLC_ENOMEM;
             for( size_t i = 0; i < p_sys->stored_attachments.size(); i++ )
             {
                 attachment_c *a = p_sys->stored_attachments[i];
-                (*ppp_attach)[i] = vlc_input_attachment_New( a->psz_file_name.c_str(), a->psz_mime_type.c_str(), NULL,
-                                                             a->p_data, a->i_size );
+                (*ppp_attach)[i] = vlc_input_attachment_New( a->fileName(), a->mimeType(), NULL,
+                                                             a->p_data, a->size() );
             }
             return VLC_SUCCESS;
 
@@ -322,9 +336,13 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             return VLC_SUCCESS;
 
         case DEMUX_SET_POSITION:
-            f = (double)va_arg( args, double );
-            Seek( p_demux, -1, f, NULL );
-            return VLC_SUCCESS;
+            if( p_sys->f_duration > 0.0 )
+            {
+                f = (double)va_arg( args, double );
+                Seek( p_demux, -1, f, NULL );
+                return VLC_SUCCESS;
+            }
+            return VLC_EGENERIC;
 
         case DEMUX_GET_TIME:
             pi64 = (int64_t*)va_arg( args, int64_t * );
@@ -338,22 +356,28 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
                 int *pi_int    = (int*)va_arg( args, int* );
 
                 *pi_int = p_sys->titles.size();
-                *ppp_title = (input_title_t**)malloc( sizeof( input_title_t**) * p_sys->titles.size() );
+                *ppp_title = (input_title_t**)malloc( sizeof( input_title_t* ) * p_sys->titles.size() );
 
                 for( size_t i = 0; i < p_sys->titles.size(); i++ )
-                {
                     (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->titles[i] );
-                }
                 return VLC_SUCCESS;
             }
             return VLC_EGENERIC;
 
         case DEMUX_SET_TITLE:
-            /* TODO handle editions as titles */
+            /* handle editions as titles */
             i_idx = (int)va_arg( args, int );
-            if( i_idx < p_sys->used_segments.size() )
+            if(i_idx <  p_sys->titles.size() && p_sys->titles[i_idx]->i_seekpoint)
             {
-                p_sys->JumpTo( *p_sys->used_segments[i_idx], NULL );
+                p_sys->p_current_segment->i_current_edition = i_idx;
+                p_sys->i_current_title = i_idx;
+                p_sys->p_current_segment->p_current_chapter = p_sys->p_current_segment->editions[p_sys->p_current_segment->i_current_edition]->getChapterbyTimecode(0);
+
+                Seek( p_demux, (int64_t)p_sys->titles[i_idx]->seekpoint[0]->i_time_offset, -1, NULL);
+                p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_TITLE;
+                p_demux->info.i_seekpoint = 0;
+                p_demux->info.i_title = i_idx;
+                p_sys->f_duration = (float) p_sys->titles[i_idx]->i_length / 1000.f;
                 return VLC_SUCCESS;
             }
             return VLC_EGENERIC;
@@ -365,7 +389,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             if( p_sys->titles.size() && i_skp < p_sys->titles[p_sys->i_current_title]->i_seekpoint)
             {
                 Seek( p_demux, (int64_t)p_sys->titles[p_sys->i_current_title]->seekpoint[i_skp]->i_time_offset, -1, NULL);
-                p_demux->info.i_seekpoint |= INPUT_UPDATE_SEEKPOINT;
+                p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
                 p_demux->info.i_seekpoint = i_skp;
                 return VLC_SUCCESS;
             }
@@ -374,9 +398,9 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         case DEMUX_GET_FPS:
             pf = (double *)va_arg( args, double * );
             *pf = 0.0;
-            if( p_sys->p_current_segment && p_sys->p_current_segment->Segment() )
+            if( p_sys->p_current_segment && p_sys->p_current_segment->CurrentSegment() )
             {
-                const matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
+                const matroska_segment_c *p_segment = p_sys->p_current_segment->CurrentSegment();
                 for( size_t i = 0; i < p_segment->tracks.size(); i++ )
                 {
                     mkv_track_t *tk = p_segment->tracks[i];
@@ -390,92 +414,86 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             return VLC_SUCCESS;
 
         case DEMUX_SET_TIME:
+            i64 = (int64_t) va_arg( args, int64_t );
+            msg_Dbg(p_demux,"SET_TIME to %" PRId64, i64 );
+            Seek( p_demux, i64, -1, NULL );
+            return VLC_SUCCESS;
         default:
             return VLC_EGENERIC;
     }
 }
 
 /* Seek */
-static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, chapter_item_c *psz_chapter )
+static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, virtual_chapter_c *p_chapter )
 {
     demux_sys_t        *p_sys = p_demux->p_sys;
     virtual_segment_c  *p_vsegment = p_sys->p_current_segment;
-    matroska_segment_c *p_segment = p_vsegment->Segment();
-    mtime_t            i_time_offset = 0;
+    matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
     int64_t            i_global_position = -1;
 
     int         i_index;
 
-    msg_Dbg( p_demux, "seek request to %"PRId64" (%f%%)", i_date, f_percent );
+    msg_Dbg( p_demux, "seek request to %" PRId64 " (%f%%)", i_date, f_percent );
     if( i_date < 0 && f_percent < 0 )
     {
-        msg_Warn( p_demux, "cannot seek nowhere !" );
+        msg_Warn( p_demux, "cannot seek nowhere!" );
         return;
     }
     if( f_percent > 1.0 )
     {
-        msg_Warn( p_demux, "cannot seek so far !" );
+        msg_Warn( p_demux, "cannot seek so far!" );
+        return;
+    }
+    if( p_sys->f_duration < 0 )
+    {
+        msg_Warn( p_demux, "cannot seek without duration!");
+        return;
+    }
+    if( !p_segment )
+    {
+        msg_Warn( p_demux, "cannot seek without valid segment position");
         return;
     }
 
     /* seek without index or without date */
-    if( f_percent >= 0 && (config_GetInt( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
+    if( f_percent >= 0 && (var_InheritBool( p_demux, "mkv-seek-percent" ) || !p_segment->b_cues || i_date < 0 ))
     {
-        if( p_sys->f_duration >= 0 && p_segment->b_cues )
-        {
-            i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
-        }
-        else
+        i_date = int64_t( f_percent * p_sys->f_duration * 1000.0 );
+        if( !p_segment->b_cues )
         {
             int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
 
-            msg_Dbg( p_demux, "inaccurate way of seeking for pos:%"PRId64, i_pos );
+            msg_Dbg( p_demux, "lengthy way of seeking for pos:%" PRId64, i_pos );
             for( i_index = 0; i_index < p_segment->i_index; i_index++ )
             {
-                if( p_segment->b_cues && p_segment->p_indexes[i_index].i_position < i_pos )
-                    break;
-                if( !p_segment->b_cues && p_segment->p_indexes[i_index].i_position >= i_pos && p_segment->p_indexes[i_index].i_time > 0 )
+                if( p_segment->p_indexes[i_index].i_position >= i_pos &&
+                    p_segment->p_indexes[i_index].i_time != -1 )
                     break;
             }
             if( i_index == p_segment->i_index )
-            {
                 i_index--;
-            }
-
-            i_date = p_segment->p_indexes[i_index].i_time;
 
-            if( !p_segment->b_cues && ( p_segment->p_indexes[i_index].i_position < i_pos || p_segment->p_indexes[i_index].i_position - i_pos > 2000000 ))
+            if( p_segment->p_indexes[i_index].i_position < i_pos )
             {
-                msg_Dbg( p_demux, "no cues, seek request to global pos: %"PRId64, i_pos );
+                msg_Dbg( p_demux, "no cues, seek request to global pos: %" PRId64, i_pos );
                 i_global_position = i_pos;
             }
         }
     }
-
-    p_vsegment->Seek( *p_demux, i_date, i_time_offset, psz_chapter, i_global_position );
+    p_vsegment->Seek( *p_demux, i_date, p_chapter, i_global_position );
 }
 
-/* Utility function for BlockDecode */
-static block_t *MemToBlock( demux_t *p_demux, uint8_t *p_mem, int i_mem, size_t offset)
-{
-    block_t *p_block;
-    if( !(p_block = block_New( p_demux, i_mem + offset ) ) ) return NULL;
-    memcpy( p_block->p_buffer + offset, p_mem, i_mem );
-    //p_block->i_rate = p_input->stream.control.i_rate;
-    return p_block;
-}
-
-/* Needed by matroska_segment::Seek() */
-static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
-                         mtime_t i_pts, mtime_t i_duration, bool f_mandatory )
+/* Needed by matroska_segment::Seek() and Seek */
+void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock,
+                  mtime_t i_pts, mtime_t i_duration, bool b_key_picture,
+                  bool b_discardable_picture )
 {
     demux_sys_t        *p_sys = p_demux->p_sys;
-    matroska_segment_c *p_segment = p_sys->p_current_segment->Segment();
+    matroska_segment_c *p_segment = p_sys->p_current_segment->CurrentSegment();
 
-    size_t          i_track;
-    unsigned int    i;
-    bool            b;
+    if( !p_segment ) return;
 
+    size_t          i_track;
     if( p_segment->BlockFindTrackIndex( &i_track, block, simpleblock ) )
     {
         msg_Err( p_demux, "invalid track number" );
@@ -489,18 +507,19 @@ static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simp
         msg_Err( p_demux, "unknown track number" );
         return;
     }
-    if( i_pts + i_duration < p_sys->i_start_pts && tk->fmt.i_cat == AUDIO_ES )
-    {
-        return; /* discard audio packets that shouldn't be rendered */
-    }
+
+    i_pts -= tk->i_codec_delay;
 
     if ( tk->fmt.i_cat != NAV_ES )
     {
+        bool b;
         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE, tk->p_es, &b );
 
         if( !b )
         {
             tk->b_inited = false;
+            if( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES )
+                tk->i_last_dts = VLC_TS_INVALID;
             return;
         }
     }
@@ -512,33 +531,49 @@ static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simp
         block_t *p_init;
 
         msg_Dbg( p_demux, "sending header (%d bytes)", tk->i_data_init );
-        p_init = MemToBlock( p_demux, tk->p_data_init, tk->i_data_init, 0 );
+        p_init = MemToBlock( tk->p_data_init, tk->i_data_init, 0 );
         if( p_init ) es_out_Send( p_demux->out, tk->p_es, p_init );
     }
     tk->b_inited = true;
 
 
-    for( i = 0;
-         (block != NULL && i < block->NumberFrames()) || (simpleblock != NULL && i < simpleblock->NumberFrames());
-         i++ )
+    size_t frame_size = 0;
+    size_t block_size = 0;
+
+    if( simpleblock != NULL )
+        block_size = simpleblock->GetSize();
+    else
+        block_size = block->GetSize();
+    const unsigned int i_number_frames = block != NULL ? block->NumberFrames() :
+            ( simpleblock != NULL ? simpleblock->NumberFrames() : 0 );
+    for( unsigned int i_frame = 0; i_frame < i_number_frames; i_frame++ )
     {
         block_t *p_block;
         DataBuffer *data;
         if( simpleblock != NULL )
         {
-            data = &simpleblock->GetBuffer(i);
-            // condition when the DTS is correct (keyframe or B frame == NOT P frame)
-            f_mandatory = simpleblock->IsDiscardable() || simpleblock->IsKeyframe();
+            data = &simpleblock->GetBuffer(i_frame);
         }
         else
         {
-            data = &block->GetBuffer(i);
+            data = &block->GetBuffer(i_frame);
+        }
+        frame_size += data->Size();
+        if( !data->Buffer() || data->Size() > frame_size || frame_size > block_size  )
+        {
+            msg_Warn( p_demux, "Cannot read frame (too long or no frame)" );
+            break;
         }
 
-        if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER && tk->p_compression_data != NULL )
-            p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
+        if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
+            tk->p_compression_data != NULL &&
+            tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
+            p_block = MemToBlock( data->Buffer(), data->Size(), tk->p_compression_data->GetSize() );
+        else if( unlikely( tk->fmt.i_codec == VLC_CODEC_WAVPACK ) )
+            p_block = packetize_wavpack(tk, data->Buffer(), data->Size());
         else
-            p_block = MemToBlock( p_demux, data->Buffer(), data->Size(), 0 );
+            p_block = MemToBlock( data->Buffer(), data->Size(), 0 );
 
         if( p_block == NULL )
         {
@@ -546,218 +581,130 @@ static void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simp
         }
 
 #if defined(HAVE_ZLIB_H)
-        if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB )
+        if( tk->i_compression_type == MATROSKA_COMPRESSION_ZLIB &&
+            tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
         {
             p_block = block_zlib_decompress( VLC_OBJECT(p_demux), p_block );
+            if( p_block == NULL )
+                break;
         }
         else
 #endif
-        if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER )
+        if( tk->i_compression_type == MATROSKA_COMPRESSION_HEADER &&
+            tk->i_encoding_scope & MATROSKA_ENCODING_SCOPE_ALL_FRAMES )
         {
             memcpy( p_block->p_buffer, tk->p_compression_data->GetBuffer(), tk->p_compression_data->GetSize() );
         }
-
-        if ( tk->fmt.i_cat == NAV_ES )
+        switch( tk->fmt.i_codec )
+        {
+        case VLC_CODEC_COOK:
+        case VLC_CODEC_ATRAC3:
         {
-            // TODO handle the start/stop times of this packet
-            if ( p_sys->b_ui_hooked )
+            handle_real_audio(p_demux, tk, p_block, i_pts);
+            block_Release(p_block);
+            i_pts = ( tk->i_default_duration )?
+                i_pts + ( mtime_t )tk->i_default_duration:
+                VLC_TS_INVALID;
+            continue;
+         }
+
+        case VLC_CODEC_DTS:
+            /* Check if packetization is correct and without padding.
+             * example: Test_mkv_div3_DTS_1920x1080_1785Kbps_23,97fps.mkv */
+            if( p_block->i_buffer > 6 )
             {
-                vlc_mutex_lock( &p_sys->p_ev->lock );
-                memcpy( &p_sys->pci_packet, &p_block->p_buffer[1], sizeof(pci_t) );
-                p_sys->SwapButtons();
-                p_sys->b_pci_packet_set = true;
-                vlc_mutex_unlock( &p_sys->p_ev->lock );
-                block_Release( p_block );
+                unsigned int a, b, c, d;
+                bool e;
+                int i_frame_size = GetSyncInfo( p_block->p_buffer, &e, &a, &b, &c, &d );
+                if( i_frame_size > 0 )
+                    p_block->i_buffer = __MIN(p_block->i_buffer, (size_t)i_frame_size);
             }
-            return;
+            break;
+
+         case VLC_CODEC_OPUS:
+            mtime_t i_length = i_duration * tk-> f_timecodescale *
+                    (double) p_segment->i_timescale / 1000.0;
+            if ( i_length < 0 ) i_length = 0;
+            p_block->i_nb_samples = i_length * tk->fmt.audio.i_rate
+                    / CLOCK_FREQ;
+            break;
         }
-        // correct timestamping when B frames are used
+
+        if ( b_key_picture )
+            p_block->i_flags |= BLOCK_FLAG_TYPE_I;
+        
         if( tk->fmt.i_cat != VIDEO_ES )
         {
+            if ( tk->fmt.i_cat == NAV_ES )
+            {
+                // TODO handle the start/stop times of this packet
+                p_sys->p_ev->SetPci( (const pci_t *)&p_block->p_buffer[1]);
+                block_Release( p_block );
+                return;
+            }
+            else if( tk->fmt.i_cat == AUDIO_ES )
+            {
+                if( tk->i_chans_to_reorder )
+                    aout_ChannelReorder( p_block->p_buffer, p_block->i_buffer,
+                                         tk->fmt.audio.i_channels,
+                                         tk->pi_chan_table, tk->fmt.i_codec );
+
+            }
             p_block->i_dts = p_block->i_pts = i_pts;
         }
         else
         {
+            // correct timestamping when B frames are used
             if( tk->b_dts_only )
             {
-                p_block->i_pts = 0;
+                p_block->i_pts = VLC_TS_INVALID;
+                p_block->i_dts = i_pts;
+            }
+            else if( tk->b_pts_only )
+            {
+                p_block->i_pts = i_pts;
                 p_block->i_dts = i_pts;
             }
             else
             {
                 p_block->i_pts = i_pts;
-                if ( f_mandatory )
+                // condition when the DTS is correct (keyframe or B frame == NOT P frame)
+                if ( b_key_picture || b_discardable_picture )
                     p_block->i_dts = p_block->i_pts;
                 else
-                    p_block->i_dts = min( i_pts, tk->i_last_dts + (mtime_t)(tk->i_default_duration >> 10));
-                p_sys->i_pts = p_block->i_dts;
+                    p_block->i_dts = min( i_pts, tk->i_last_dts + ( mtime_t )tk->i_default_duration );
             }
         }
-        tk->i_last_dts = p_block->i_dts;
+        if( p_block->i_dts > VLC_TS_INVALID &&
+            ( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES ) )
+        {
+            tk->i_last_dts = p_block->i_dts;
+        }
 
 #if 0
-msg_Dbg( p_demux, "block i_dts: %"PRId64" / i_pts: %"PRId64, p_block->i_dts, p_block->i_pts);
+msg_Dbg( p_demux, "block (track=%d) i_dts: %"PRId64" / i_pts: %"PRId64, tk->i_number, p_block->i_dts, p_block->i_pts);
 #endif
-        if( strcmp( tk->psz_codec, "S_VOBSUB" ) )
+        if( !tk->b_no_duration )
         {
-            p_block->i_length = i_duration * 1000;
+            p_block->i_length = i_duration * tk-> f_timecodescale *
+                (double) p_segment->i_timescale / ( 1000.0 * i_number_frames );
         }
 
+        /* FIXME remove when VLC_TS_INVALID work is done */
+        if( i_frame == 0 || p_block->i_dts > VLC_TS_INVALID )
+            p_block->i_dts += VLC_TS_0;
+        if( !tk->b_dts_only && ( i_frame == 0 || p_block->i_pts > VLC_TS_INVALID ) )
+            p_block->i_pts += VLC_TS_0;
+
         es_out_Send( p_demux->out, tk->p_es, p_block );
 
         /* use time stamp only for first block */
-        i_pts = 0;
-    }
-}
-
-
-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;
-    int64_t     i_block_ref1;
-    int64_t     i_block_ref2;
-    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 )
-    {
-        if( BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
-        {
-            msg_Warn( &sys.demuxer, "cannot get block EOF?" );
-
-            return;
-        }
-        ep->Down();
-
-        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( i_block_ref1 == 0 && 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, i_block_ref1 >= 0 || i_block_ref2 > 0 );
-                }
-            }
-        }
-
-        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 );
+        i_pts = ( tk->i_default_duration )?
+                 i_pts + ( mtime_t )tk->i_default_duration:
+                 VLC_TS_INVALID;
     }
 }
 
-
 /*****************************************************************************
  * Demux: reads and demuxes data packets
  *****************************************************************************
@@ -770,61 +717,45 @@ static int Demux( demux_t *p_demux)
     vlc_mutex_lock( &p_sys->lock_demuxer );
 
     virtual_segment_c  *p_vsegment = p_sys->p_current_segment;
-    matroska_segment_c *p_segment = p_vsegment->Segment();
-    if ( p_segment == NULL ) return 0;
-    int                i_block_count = 0;
-    int                i_return = 0;
-
-    for( ;; )
+    matroska_segment_c *p_segment = p_vsegment->CurrentSegment();
+    if ( p_segment == NULL )
     {
-        if ( p_sys->demuxer.b_die )
-            break;
+        vlc_mutex_unlock( &p_sys->lock_demuxer );
+        return 0;
+    }
+    int i_return = 0;
 
+    do
+    {
         if( p_sys->i_pts >= p_sys->i_start_pts  )
             if ( p_vsegment->UpdateCurrentToChapter( *p_demux ) )
             {
                 i_return = 1;
                 break;
             }
-        if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
-        {
-            /* nothing left to read in this ordered edition */
-            if ( !p_vsegment->SelectNext() )
-                break;
-            p_segment->UnSelect( );
-            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
 
-            /* switch to the next segment */
-            p_segment = p_vsegment->Segment();
-            if ( !p_segment->Select( 0 ) )
-            {
-                msg_Err( p_demux, "Failed to select new segment" );
-                break;
-            }
-            continue;
-        }
+        if ( p_vsegment->CurrentEdition() &&
+             p_vsegment->CurrentEdition()->b_ordered &&
+             p_vsegment->CurrentChapter() == NULL )
+            /* nothing left to read in this ordered edition */
+            break;
 
         KaxBlock *block;
         KaxSimpleBlock *simpleblock;
         int64_t i_block_duration = 0;
-        int64_t i_block_ref1;
-        int64_t i_block_ref2;
-
-        if( p_segment->BlockGet( block, simpleblock, &i_block_ref1, &i_block_ref2, &i_block_duration ) )
+        bool b_key_picture;
+        bool b_discardable_picture;
+        if( p_segment->BlockGet( block, simpleblock, &b_key_picture, &b_discardable_picture, &i_block_duration ) )
         {
-            if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered )
+            if ( p_vsegment->CurrentEdition() && p_vsegment->CurrentEdition()->b_ordered )
             {
-                const chapter_item_c *p_chap = p_vsegment->CurrentChapter();
+                const virtual_chapter_c *p_chap = p_vsegment->CurrentChapter();
                 // check if there are more chapters to read
                 if ( p_chap != NULL )
                 {
                     /* TODO handle successive chapters with the same user_start_time/user_end_time
-                    if ( p_chap->i_user_start_time == p_chap->i_user_start_time )
-                        p_vsegment->SelectNext();
                     */
-                    p_sys->i_pts = p_chap->i_user_end_time;
+                    p_sys->i_pts = p_chap->i_virtual_stop_time;
                     p_sys->i_pts++; // trick to avoid staying on segments with no duration and no content
 
                     i_return = 1;
@@ -835,31 +766,26 @@ static int Demux( demux_t *p_demux)
             else
             {
                 msg_Warn( p_demux, "cannot get block EOF?" );
-                p_segment->UnSelect( );
-                es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
-
-                /* switch to the next segment */
-                if ( !p_vsegment->SelectNext() )
-                    // no more segments in this stream
-                    break;
-                p_segment = p_vsegment->Segment();
-                if ( !p_segment->Select( 0 ) )
-                {
-                    msg_Err( p_demux, "Failed to select new segment" );
-                    break;
-                }
-
-                continue;
+                break;
             }
         }
 
         if( simpleblock != NULL )
-            p_sys->i_pts = (p_sys->i_chapter_time + simpleblock->GlobalTimecode()) / (mtime_t) 1000;
+            p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)simpleblock->GlobalTimecode() / INT64_C(1000) );
         else
-            p_sys->i_pts = (p_sys->i_chapter_time + block->GlobalTimecode()) / (mtime_t) 1000;
+            p_sys->i_pts = p_sys->i_chapter_time + ( (mtime_t)block->GlobalTimecode() / INT64_C(1000) );
 
-        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pts );
+        mtime_t i_pcr = VLC_TS_INVALID;
+        for( size_t i = 0; i < p_segment->tracks.size(); i++)
+            if( p_segment->tracks[i]->i_last_dts > VLC_TS_INVALID &&
+                ( p_segment->tracks[i]->i_last_dts < i_pcr || i_pcr == VLC_TS_INVALID ))
+                i_pcr = p_segment->tracks[i]->i_last_dts;
+
+        if( i_pcr > p_sys->i_pcr + 300000 )
+        {
+            es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
+            p_sys->i_pcr = i_pcr;
+        }
 
         if( p_sys->i_pts >= p_sys->i_start_pts  )
         {
@@ -870,43 +796,24 @@ static int Demux( demux_t *p_demux)
                 break;
             }
         }
-        if ( p_vsegment->Edition() && p_vsegment->Edition()->b_ordered && p_vsegment->CurrentChapter() == NULL )
+
+        if ( p_vsegment->CurrentEdition() &&
+             p_vsegment->CurrentEdition()->b_ordered &&
+             p_vsegment->CurrentChapter() == NULL )
         {
             /* nothing left to read in this ordered edition */
-            if ( !p_vsegment->SelectNext() )
-            {
-                delete block;
-                break;
-            }
-            p_segment->UnSelect( );
-            es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
-
-            /* switch to the next segment */
-            p_segment = p_vsegment->Segment();
-            if ( !p_segment->Select( 0 ) )
-            {
-                msg_Err( p_demux, "Failed to select new segment" );
-                delete block;
-                break;
-            }
             delete block;
-            continue;
+            break;
         }
 
-        BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, i_block_ref1 >= 0 || i_block_ref2 > 0 );
+        BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, b_key_picture, b_discardable_picture );
 
         delete block;
-        i_block_count++;
 
-        // TODO optimize when there is need to leave or when seeking has been called
-        if( i_block_count > 5 )
-        {
-            i_return = 1;
-            break;
-        }
+        vlc_mutex_unlock( &p_sys->lock_demuxer );
+        return 1;
     }
+    while (0);
 
     vlc_mutex_unlock( &p_sys->lock_demuxer );