]> 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 b2b85bb0ab724ee36e06cfd5f4a0992806302ec5..5e183b9912f6c857ac1b60bb2608f9d56f19d8ed 100644 (file)
 
 #include "stream_io_callback.hpp"
 
+extern "C" {
+#include "../../modules/codec/dts_header.h"
+}
+
 #include <vlc_fs.h>
 #include <vlc_url.h>
 
@@ -73,7 +77,7 @@ vlc_module_begin ()
     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 );
@@ -411,7 +415,7 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 
         case DEMUX_SET_TIME:
             i64 = (int64_t) va_arg( args, int64_t );
-            msg_Dbg(p_demux,"SET_TIME to %"PRId64, i64 );
+            msg_Dbg(p_demux,"SET_TIME to %" PRId64, i64 );
             Seek( p_demux, i64, -1, NULL );
             return VLC_SUCCESS;
         default:
@@ -425,12 +429,11 @@ static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, virtual_ch
     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->CurrentSegment();
-    mtime_t            i_time_offset = 0;
     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!" );
@@ -460,11 +463,11 @@ static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, virtual_ch
         {
             int64_t i_pos = int64_t( f_percent * stream_Size( p_demux->s ) );
 
-            msg_Dbg( p_demux, "lengthy 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->p_indexes[i_index].i_position >= i_pos &&
-                    p_segment->p_indexes[i_index].i_time > 0 )
+                    p_segment->p_indexes[i_index].i_time != -1 )
                     break;
             }
             if( i_index == p_segment->i_index )
@@ -472,17 +475,18 @@ static void Seek( demux_t *p_demux, mtime_t i_date, double f_percent, virtual_ch
 
             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, p_chapter, i_global_position );
+    p_vsegment->Seek( *p_demux, i_date, p_chapter, i_global_position );
 }
 
 /* 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 f_mandatory )
+                  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->CurrentSegment();
@@ -515,7 +519,7 @@ void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock
         {
             tk->b_inited = false;
             if( tk->fmt.i_cat == VIDEO_ES || tk->fmt.i_cat == AUDIO_ES )
-                tk->i_last_dts = i_pts;
+                tk->i_last_dts = VLC_TS_INVALID;
             return;
         }
     }
@@ -541,22 +545,19 @@ void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock
     else
         block_size = block->GetSize();
  
-    for( unsigned int i = 0;
-         ( block != NULL && i < block->NumberFrames()) || ( simpleblock != NULL && i < simpleblock->NumberFrames() );
-         i++ )
+    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);
-            // condition when the DTS is correct (keyframe or B frame == NOT P frame)
+            data = &block->GetBuffer(i_frame);
         }
         frame_size += data->Size();
         if( !data->Buffer() || data->Size() > frame_size || frame_size > block_size  )
@@ -602,10 +603,24 @@ void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock
             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 / 1000 ):
+                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 )
+            {
+                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);
+            }
+            break;
+
          case VLC_CODEC_OPUS:
             mtime_t i_length = i_duration * tk-> f_timecodescale *
                     (double) p_segment->i_timescale / 1000.0;
@@ -615,6 +630,8 @@ void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock
             break;
         }
 
+        if ( b_key_picture )
+            p_block->i_flags |= BLOCK_FLAG_TYPE_I;
         
         if( tk->fmt.i_cat != VIDEO_ES )
         {
@@ -651,10 +668,11 @@ void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock
             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 / 1000 ) );
+                    p_block->i_dts = min( i_pts, tk->i_last_dts + ( mtime_t )tk->i_default_duration );
             }
         }
         if( p_block->i_dts > VLC_TS_INVALID &&
@@ -664,25 +682,25 @@ void BlockDecode( demux_t *p_demux, KaxBlock *block, KaxSimpleBlock *simpleblock
         }
 
 #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( !tk->b_no_duration )
         {
             p_block->i_length = i_duration * tk-> f_timecodescale *
-                (double) p_segment->i_timescale / 1000.0;
+                (double) p_segment->i_timescale / ( 1000.0 * i_number_frames );
         }
 
         /* FIXME remove when VLC_TS_INVALID work is done */
-        if( i == 0 || p_block->i_dts > VLC_TS_INVALID )
+        if( i_frame == 0 || p_block->i_dts > VLC_TS_INVALID )
             p_block->i_dts += VLC_TS_0;
-        if( !tk->b_dts_only && ( i == 0 || p_block->i_pts > VLC_TS_INVALID ) )
+        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 = ( tk->i_default_duration )?
-                 i_pts + ( mtime_t )( tk->i_default_duration / 1000 ):
+                 i_pts + ( mtime_t )tk->i_default_duration:
                  VLC_TS_INVALID;
     }
 }
@@ -788,7 +806,7 @@ static int Demux( demux_t *p_demux)
             break;
         }
 
-        BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, b_key_picture || b_discardable_picture );
+        BlockDecode( p_demux, block, simpleblock, p_sys->i_pts, i_block_duration, b_key_picture, b_discardable_picture );
 
         delete block;