[vlc-commits] codec: opus: don't abuse lengh for trimming

Francois Cartegnie git at videolan.org
Tue May 29 11:19:58 CEST 2018


vlc | branch: master | Francois Cartegnie <fcvlcdev at free.fr> | Mon May 28 23:09:54 2018 +0200| [0981a08af761e52931dddeceeb7dc250fef5ba29] | committer: Francois Cartegnie

codec: opus: don't abuse lengh for trimming

can break non ogg and remuxing

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=0981a08af761e52931dddeceeb7dc250fef5ba29
---

 modules/codec/opus.c | 18 +++++++++++++++---
 modules/demux/ogg.c  | 16 ++++++----------
 modules/demux/ogg.h  |  2 +-
 3 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/modules/codec/opus.c b/modules/codec/opus.c
index fdde76ff05..d1a0fc9250 100644
--- a/modules/codec/opus.c
+++ b/modules/codec/opus.c
@@ -161,7 +161,7 @@ static int  ProcessHeaders( decoder_t * );
 static int  ProcessInitialHeader ( decoder_t *, ogg_packet * );
 static block_t *ProcessPacket( decoder_t *, ogg_packet *, block_t * );
 
-static block_t *DecodePacket( decoder_t *, ogg_packet *, int, int );
+static block_t *DecodePacket( decoder_t *, ogg_packet *, int, mtime_t );
 
 /*****************************************************************************
  * OpenDecoder: probe the decoder and return score
@@ -447,9 +447,13 @@ static block_t *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
         return NULL;
     }
 
+    /* trimming info */
+    mtime_t i_max_duration = (p_block->i_flags & BLOCK_FLAG_END_OF_SEQUENCE) ?
+                             p_block->i_length : 0;
+
     block_t *p_aout_buffer = DecodePacket( p_dec, p_oggpacket,
                                            p_block->i_nb_samples,
-                                           (int)p_block->i_length );
+                                           i_max_duration );
 
     block_Release( p_block );
     return p_aout_buffer;
@@ -459,7 +463,7 @@ static block_t *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
  * DecodePacket: decodes a Opus packet.
  *****************************************************************************/
 static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
-                              int i_nb_samples, int i_end_trim )
+                              int i_nb_samples, mtime_t i_duration )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
 
@@ -487,6 +491,14 @@ static block_t *DecodePacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
 
     spp=opus_multistream_decode_float(p_sys->p_st, p_oggpacket->packet,
          p_oggpacket->bytes, (float *)p_aout_buffer->p_buffer, spp, 0);
+
+    int i_end_trim = 0;
+    if( i_duration > 0 && spp > 0 &&
+        i_duration < i_nb_samples * CLOCK_FREQ / 48000 )
+    {
+        i_end_trim = spp - VLC_CLIP(i_duration * 48000 / CLOCK_FREQ, 0, spp);
+    }
+
     if( spp < 0 || i_nb_samples <= 0 || i_end_trim >= i_nb_samples)
     {
         block_Release(p_aout_buffer);
diff --git a/modules/demux/ogg.c b/modules/demux/ogg.c
index 86cfd8e26f..e5a0e84c70 100644
--- a/modules/demux/ogg.c
+++ b/modules/demux/ogg.c
@@ -1051,15 +1051,7 @@ static void Ogg_UpdatePCR( demux_t *p_demux, logical_stream_t *p_stream,
             ogg_int64_t sample = p_stream->i_previous_granulepos;
 
             if( p_stream->fmt.i_codec == VLC_CODEC_OPUS && p_oggpacket->e_o_s )
-            {
-                unsigned duration = Ogg_OpusPacketDuration( p_oggpacket );
-                if( duration > 0 )
-                {
-                    ogg_int64_t end_sample = p_oggpacket->granulepos;
-                    if( end_sample < ( sample + duration ) )
-                        p_stream->i_end_trim = sample + duration - end_sample;
-                }
-            }
+                p_stream->i_end_trim = p_oggpacket->granulepos - sample;
 
             if (sample >= p_stream->i_pre_skip)
                 sample -= p_stream->i_pre_skip;
@@ -1474,7 +1466,11 @@ static void Ogg_DecodePacket( demux_t *p_demux,
     else if( p_stream->fmt.i_cat == AUDIO_ES )
     {
         /* Blatant abuse of the i_length field. */
-        p_block->i_length = p_stream->i_end_trim;
+        if( p_stream->i_end_trim > 0 )
+        {
+            p_block->i_length = p_stream->i_end_trim * CLOCK_FREQ / p_stream->f_rate;
+            p_block->i_flags |= BLOCK_FLAG_END_OF_SEQUENCE;
+        }
         p_block->i_pts = p_block->i_dts = p_stream->i_pcr;
     }
     else if( p_stream->fmt.i_cat == SPU_ES )
diff --git a/modules/demux/ogg.h b/modules/demux/ogg.h
index d2139d1b6f..13c1c6d4d6 100644
--- a/modules/demux/ogg.h
+++ b/modules/demux/ogg.h
@@ -94,7 +94,7 @@ typedef struct logical_stream_s
     /* Opus has a starting offset in the headers. */
     int i_pre_skip;
     /* Vorbis and Opus can trim the end of a stream using granule positions. */
-    int i_end_trim;
+    int i_end_trim; /* number of samples to keep */
 
     /* offset of first keyframe for theora; can be 0 or 1 depending on version number */
     int8_t i_keyframe_offset;



More information about the vlc-commits mailing list