]> git.sesse.net Git - vlc/blobdiff - modules/demux/mpeg/mpga.c
A bit of headers cleanup
[vlc] / modules / demux / mpeg / mpga.c
index 1deab3928711b936374d262ef532b4b904f3c59f..ec5cb291eba88ae1f351ec36892b1aa2591b948f 100644 (file)
@@ -1,10 +1,11 @@
 /*****************************************************************************
  * mpga.c : MPEG-I/II Audio input module for vlc
  *****************************************************************************
- * Copyright (C) 2001-2004 VideoLAN
+ * Copyright (C) 2001-2004 the VideoLAN team
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
+ *          Gildas Bazin <gbazin@videolan.org>
  *
  * 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
@@ -18,7 +19,7 @@
  *
  * 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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 #include <stdlib.h>                                      /* malloc(), free() */
 
 #include <vlc/vlc.h>
-#include <vlc/input.h>
+#include <vlc_demux.h>
+#include <vlc_codec.h>
+#include <vlc_meta.h>
 
-#include "vlc_meta.h"
+#define MPGA_PACKET_SIZE 1024
 
 /*****************************************************************************
  * Module descriptor
@@ -38,17 +41,15 @@ static int  Open ( vlc_object_t * );
 static void Close( vlc_object_t * );
 
 vlc_module_begin();
-    set_description( _("MPEG-I/II audio demuxer" ) );
+    set_category( CAT_INPUT );
+    set_subcategory( SUBCAT_INPUT_DEMUX );
+    set_description( _("MPEG audio / MP3 demuxer" ) );
     set_capability( "demux2", 100 );
     set_callbacks( Open, Close );
     add_shortcut( "mpga" );
     add_shortcut( "mp3" );
 vlc_module_end();
 
-/* TODO:
- * - free bitrate
- */
-
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
@@ -57,14 +58,23 @@ static int Control( demux_t *, int, va_list );
 
 struct demux_sys_t
 {
-    date_t          pts;
-    mtime_t         i_time_offset;
+    es_out_id_t *p_es;
+    vlc_meta_t  *meta;
+
+    vlc_bool_t  b_start;
+    decoder_t   *p_packetizer;
 
-    int             i_bitrate_avg;  /* extracted from Xing header */
+    mtime_t     i_pts;
+    mtime_t     i_time_offset;
+    int         i_bitrate_avg;  /* extracted from Xing header */
 
-    vlc_meta_t      *meta;
+    vlc_bool_t b_initial_sync_failed;
 
-    es_out_id_t     *p_es;
+    int i_xing_frames;
+    int i_xing_bytes;
+    int i_xing_bitrate_avg;
+    int i_xing_frame_samples;
+    block_t *p_block_in, *p_block_out;
 };
 
 static int HeaderCheck( uint32_t h )
@@ -76,56 +86,15 @@ static int HeaderCheck( uint32_t h )
         || (((h >> 10) & 0x03) == 0x03 )    /* valide sampling freq ? */
         || ((h & 0x03) == 0x02 ))           /* valid emphasis ? */
     {
-        return( VLC_FALSE );
+        return VLC_FALSE;
     }
-    return( VLC_TRUE );
+    return VLC_TRUE;
 }
 
-static int mpga_sample_rate[2][4] =
-{
-    { 44100, 48000, 32000, 0 },
-    { 22050, 24000, 16000, 0 }
-};
-
-static int mpga_bitrate[2][3][16] =
-{
-  {
-    { 0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
-    { 0, 32, 48, 56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
-    { 0, 32, 40, 48,  56,  64,  80,  96, 112, 128, 160, 192, 224, 256, 320, 0}
-  },
-  {
-    { 0, 32, 48, 56,  64,  80,  96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
-    { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0},
-    { 0,  8, 16, 24,  32,  40,  48,  56,  64,  80,  96, 112, 128, 144, 160, 0}
-  }
-};
-
-
 #define MPGA_VERSION( h )   ( 1 - (((h)>>19)&0x01) )
 #define MPGA_LAYER( h )     ( 3 - (((h)>>17)&0x03) )
-#define MPGA_SAMPLE_RATE(h) \
-    ( mpga_sample_rate[MPGA_VERSION(h)][((h)>>10)&0x03] / ( ((h>>20)&0x01) ? 1 : 2) )
-#define MPGA_CHANNELS(h)    ( (((h)>>6)&0x03) == 3 ? 1 : 2)
-#define MPGA_BITRATE(h)     mpga_bitrate[MPGA_VERSION(h)][MPGA_LAYER(h)][((h)>>12)&0x0f]
-#define MPGA_PADDING(h)     ( ((h)>>9)&0x01 )
 #define MPGA_MODE(h)        (((h)>> 6)&0x03)
 
-static int mpga_frame_size( uint32_t h )
-{
-    switch( MPGA_LAYER(h) )
-    {
-        case 0:
-            return ( ( 12000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h) ) * 4;
-        case 1:
-            return ( 144000 * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
-        case 2:
-            return ( ( MPGA_VERSION(h) ? 72000 : 144000 ) * MPGA_BITRATE(h) ) / MPGA_SAMPLE_RATE(h) + MPGA_PADDING(h);
-        default:
-            return 0;
-    }
-}
-
 static int mpga_frame_samples( uint32_t h )
 {
     switch( MPGA_LAYER(h) )
@@ -141,6 +110,7 @@ static int mpga_frame_samples( uint32_t h )
     }
 }
 
+
 /*****************************************************************************
  * Open: initializes demux structures
  *****************************************************************************/
@@ -149,62 +119,31 @@ static int Open( vlc_object_t * p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
     vlc_bool_t   b_forced = VLC_FALSE;
-    vlc_bool_t   b_extention = VLC_FALSE;
 
     uint32_t     header;
     uint8_t     *p_peek;
     module_t    *p_id3;
-    es_format_t   fmt;
+    block_t     *p_block_in, *p_block_out;
 
-    if( !strncmp( p_demux->psz_demux, "mpga", 4 ) ||
-        !strncmp( p_demux->psz_demux, "mp3", 3 ) )
-    {
-        b_forced = VLC_TRUE;
-    }
     if( p_demux->psz_path )
     {
         int  i_len = strlen( p_demux->psz_path );
         if( i_len > 4 && !strcasecmp( &p_demux->psz_path[i_len - 4], ".mp3" ) )
         {
-            b_extention = VLC_TRUE;
+            b_forced = VLC_TRUE;
         }
     }
 
-    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
-    p_sys->i_time_offset = 0;
-    p_sys->i_bitrate_avg = 0;
-    p_sys->meta = NULL;
-
-    /* skip/parse possible id3 header */
-    if( ( p_id3 = module_Need( p_demux, "id3", NULL, 0 ) ) )
-    {
-        p_sys->meta = (vlc_meta_t *)p_demux->p_private;
-        p_demux->p_private = NULL;
-
-        module_Unneed( p_demux, p_id3 );
-    }
-
-    if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
-    {
-        msg_Err( p_demux, "cannot peek" );
-        Close( VLC_OBJECT(p_demux ) );
-        return VLC_EGENERIC;
-    }
+    if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 ) return VLC_EGENERIC;
 
     if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
     {
         vlc_bool_t b_ok = VLC_FALSE;
         int i_peek;
 
-        if( !b_forced && !b_extention )
-        {
-            msg_Warn( p_demux, "mpga module discarded" );
-            Close( VLC_OBJECT(p_demux) );
-            return VLC_EGENERIC;
-        }
+        if( !p_demux->b_force && !b_forced ) return VLC_EGENERIC;
 
         i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
-
         while( i_peek > 4 )
         {
             if( HeaderCheck( header = GetDWBE( p_peek ) ) )
@@ -212,111 +151,127 @@ static int Open( vlc_object_t * p_this )
                 b_ok = VLC_TRUE;
                 break;
             }
-            p_peek += 4;
-            i_peek -= 4;
-        }
-        if( !b_ok && !b_forced )
-        {
-            msg_Warn( p_demux, "mpga module discarded" );
-            Close( VLC_OBJECT(p_demux) );
-            return VLC_EGENERIC;
+            p_peek += 1;
+            i_peek -= 1;
         }
+        if( !b_ok && !p_demux->b_force ) return VLC_EGENERIC;
     }
 
-    p_demux->pf_demux   = Demux;
-    p_demux->pf_control = Control;
+    STANDARD_DEMUX_INIT; p_sys = p_demux->p_sys;
+    memset( p_sys, 0, sizeof( demux_sys_t ) );
+    p_sys->p_es = 0;
+    p_sys->b_start = VLC_TRUE;
+    p_sys->meta = 0;
 
-    es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
+    /* Load the mpeg audio packetizer */
+    INIT_APACKETIZER( p_sys->p_packetizer, 'm', 'p', 'g', 'a' );
+    es_format_Init( &p_sys->p_packetizer->fmt_out, UNKNOWN_ES, 0 );
+    LOAD_PACKETIZER_OR_FAIL( p_sys->p_packetizer, "mpga" );
 
+    /* Xing header */
     if( HeaderCheck( header ) )
     {
-        int     i_xing;
+        int i_xing, i_skip;
         uint8_t *p_xing;
-        char psz_description[50];
 
-        p_sys->i_bitrate_avg = MPGA_BITRATE( header ) * 1000;
-        if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) >= 21 )
+        if( ( i_xing = stream_Peek( p_demux->s, &p_xing, 1024 ) ) < 21 )
+            return VLC_SUCCESS; /* No header */
+
+        if( MPGA_VERSION( header ) == 0 )
+        {
+            i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
+        }
+        else
+        {
+            i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
+        }
+
+        if( i_skip + 8 < i_xing && !strncmp( (char *)&p_xing[i_skip], "Xing", 4 ) )
         {
-            int i_skip;
+            unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
 
-            if( MPGA_VERSION( header) == 0 )
+            p_xing += i_skip + 8;
+            i_xing -= i_skip + 8;
+
+            i_skip = 0;
+            if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
+            {
+                p_sys->i_xing_frames = GetDWBE( &p_xing[i_skip] );
+                i_skip += 4;
+            }
+            if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
             {
-                i_skip = MPGA_MODE( header ) != 3 ? 36 : 21;
+                p_sys->i_xing_bytes = GetDWBE( &p_xing[i_skip] );
+                i_skip += 4;
             }
-            else
+            if( i_flags&0x04 )   /* XING_TOC */
             {
-                i_skip = MPGA_MODE( header ) != 3 ? 21 : 13;
+                i_skip += 100;
             }
-            if( i_skip + 8 < i_xing &&
-                !strncmp( &p_xing[i_skip], "Xing", 4 ) )
+
+            // FIXME: doesn't return the right bitrage average, at least
+            // with some MP3's
+            if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
             {
-                unsigned int i_flags = GetDWBE( &p_xing[i_skip+4] );
-                unsigned int i_bytes = 0, i_frames = 0;
-
-                p_xing += i_skip + 8;
-                i_xing -= i_skip + 8;
-
-                i_skip = 0;
-                if( i_flags&0x01 && i_skip + 4 <= i_xing )   /* XING_FRAMES */
-                {
-                    i_frames = GetDWBE( &p_xing[i_skip] );
-                    i_skip += 4;
-                }
-                if( i_flags&0x02 && i_skip + 4 <= i_xing )   /* XING_BYTES */
-                {
-                    i_bytes = GetDWBE( &p_xing[i_skip] );
-                    i_skip += 4;
-                }
-                if( i_flags&0x04 )   /* XING_TOC */
-                {
-                    i_skip += 100;
-                }
-#if 0
-// FIXME: doesn't return the right bitrage average, at least with some MP3's
-                if( i_flags&0x08 && i_skip + 4 <= i_xing )   /* XING_VBR */
-                {
-                    p_sys->i_bitrate_avg = GetDWBE( &p_xing[i_skip] );
-    fprintf(stderr,"rate2 %d\n", p_sys->i_bitrate_avg);
-                    msg_Dbg( p_input, "xing vbr value present (%d)", p_sys->i_bitrate_avg );
-                }
-                else
-#endif
-                if( i_frames > 0 && i_bytes > 0 )
-                {
-                    p_sys->i_bitrate_avg = (int64_t)i_bytes *
-                                           (int64_t)8 *
-                                           (int64_t)MPGA_SAMPLE_RATE( header ) /
-                                           (int64_t)i_frames /
-                                           (int64_t)mpga_frame_samples( header );
-                    msg_Dbg( p_demux, "xing frames&bytes value present (%db/s)", p_sys->i_bitrate_avg );
-                }
+                p_sys->i_xing_bitrate_avg = GetDWBE( &p_xing[i_skip] );
+                msg_Dbg( p_demux, "xing vbr value present (%d)",
+                         p_sys->i_xing_bitrate_avg );
+            }
+
+            if( p_sys->i_xing_frames > 0 && p_sys->i_xing_bytes > 0 )
+            {
+                p_sys->i_xing_frame_samples = mpga_frame_samples( header );
+                msg_Dbg( p_demux, "xing frames&bytes value present "
+                         "(%d bytes, %d frames, %d samples/frame)",
+                         p_sys->i_xing_bytes, p_sys->i_xing_frames,
+                         p_sys->i_xing_frame_samples );
             }
         }
+    }
 
-        msg_Dbg( p_demux, "version=%d layer=%d channels=%d samplerate=%d",
-                 MPGA_VERSION( header ) + 1,
-                 MPGA_LAYER( header ) + 1,
-                 MPGA_CHANNELS( header ),
-                 MPGA_SAMPLE_RATE( header ) );
-
-        fmt.audio.i_channels = MPGA_CHANNELS( header );
-        fmt.audio.i_rate = MPGA_SAMPLE_RATE( header );
-        fmt.i_bitrate = p_sys->i_bitrate_avg;
-        sprintf( psz_description, "MPEG Audio Layer %d, version %d",
-                 MPGA_LAYER ( header ) + 1, MPGA_VERSION ( header ) + 1 );
-        fmt.psz_description = strdup( psz_description );
-
-        date_Init( &p_sys->pts, fmt.audio.i_rate, 1 );
-        date_Set( &p_sys->pts, 1 );
+    if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) ) == NULL )
+    {
+        return VLC_EGENERIC;
+    }
+    p_block_in->i_pts = p_block_in->i_dts = 1;
+    p_block_out = p_sys->p_packetizer->pf_packetize(
+        p_sys->p_packetizer, &p_block_in );
+
+    if( p_block_out == NULL )
+    {
+        msg_Dbg( p_demux, "did not sync on first block" );
+        p_sys->b_initial_sync_failed = VLC_TRUE;
     }
+    else
+        p_sys->b_initial_sync_failed = VLC_FALSE;
 
-    p_sys->p_es = es_out_Add( p_demux->out, &fmt );
-    if( fmt.psz_description ) free( fmt.psz_description );
+    p_sys->p_packetizer->fmt_out.b_packetized = VLC_TRUE;
+    p_sys->p_es = es_out_Add( p_demux->out,
+                              &p_sys->p_packetizer->fmt_out);
+    p_sys->i_bitrate_avg = p_sys->p_packetizer->fmt_out.i_bitrate;
+
+    if( p_sys->i_xing_bytes && p_sys->i_xing_frames &&
+        p_sys->i_xing_frame_samples )
+    {
+        p_sys->i_bitrate_avg = p_sys->i_xing_bytes * I64C(8) *
+            p_sys->p_packetizer->fmt_out.audio.i_rate /
+            p_sys->i_xing_frames / p_sys->i_xing_frame_samples;
+    }
+
+    p_sys->p_block_in = p_block_in;
+    p_sys->p_block_out = p_block_out;
+
+    /* Parse possible id3 header */
+    if( ( p_id3 = module_Need( p_demux, "meta reader", NULL, 0 ) ) )
+    {
+        p_sys->meta = (vlc_meta_t *)p_demux->p_private;
+        p_demux->p_private = NULL;
+        module_Unneed( p_demux, p_id3 );
+    }
 
     return VLC_SUCCESS;
 }
 
-
 /*****************************************************************************
  * Demux: reads and demuxes data packets
  *****************************************************************************
@@ -325,64 +280,52 @@ static int Open( vlc_object_t * p_this )
 static int Demux( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
-    block_t     *p_frame;
-
-    uint32_t     header;
-    uint8_t     *p_peek;
-
-    if( stream_Peek( p_demux->s, &p_peek, 4 ) < 4 )
+    block_t *p_block_in, *p_block_out;
+    if( p_sys->b_start )
     {
-        msg_Warn( p_demux, "cannot peek" );
-        return 0;
+        p_sys->b_start = VLC_FALSE;
+        p_block_in = p_sys->p_block_in;
+        p_sys->p_block_in = NULL;
+        p_block_out = p_sys->p_block_out;
+        p_sys->p_block_out = NULL;
     }
-
-    if( !HeaderCheck( header = GetDWBE( p_peek ) ) )
+    else
     {
-        /* we need to resynch */
-        vlc_bool_t  b_ok = VLC_FALSE;
-        int         i_skip = 0;
-        int         i_peek;
-
-        i_peek = stream_Peek( p_demux->s, &p_peek, 8096 );
-        if( i_peek < 4 )
+        if( ( p_block_in = stream_Block( p_demux->s, MPGA_PACKET_SIZE ) )
+            == NULL )
         {
-            msg_Warn( p_demux, "cannot peek" );
             return 0;
         }
-
-        while( i_peek >= 4 )
+        if( p_demux->p_sys->b_initial_sync_failed == VLC_TRUE )
         {
-            if( HeaderCheck( header = GetDWBE( p_peek ) ) )
-            {
-                b_ok = VLC_TRUE;
-                break;
-            }
-
-            p_peek++;
-            i_peek--;
-            i_skip++;
+            p_block_in->i_pts = p_block_in->i_dts = 1;
+            /* Only try to resync once */
+            p_demux->p_sys->b_initial_sync_failed = 0;
         }
-
-        msg_Warn( p_demux, "garbage=%d bytes", i_skip );
-        stream_Read( p_demux->s, NULL, i_skip );
-        return 1;
+        else
+            p_block_in->i_pts = p_block_in->i_dts = 0;
+        p_block_out = p_sys->p_packetizer->pf_packetize(
+            p_sys->p_packetizer, &p_block_in );
     }
 
-    if( ( p_frame = stream_Block( p_demux->s,
-                                  mpga_frame_size( header ) ) ) == NULL )
-    {
-        msg_Warn( p_demux, "cannot read data" );
-        return 0;
-    }
 
-    p_frame->i_dts = p_frame->i_pts =
-        date_Increment( &p_sys->pts, mpga_frame_samples( header ) );
+    while( p_block_out )
+    {
+        while( p_block_out )
+        {
+            block_t *p_next = p_block_out->p_next;
 
-    /* set PCR */
-    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_frame->i_pts );
+            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block_out->i_dts );
 
-    es_out_Send( p_demux->out, p_sys->p_es, p_frame );
+            p_block_out->p_next = NULL;
+            p_sys->i_pts = p_block_out->i_pts;
+            es_out_Send( p_demux->out, p_sys->p_es, p_block_out );
 
+            p_block_out = p_next;
+        }
+        p_block_out = p_sys->p_packetizer->pf_packetize(
+            p_sys->p_packetizer, &p_block_in );
+    }
     return 1;
 }
 
@@ -393,10 +336,10 @@ 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;
-    if( p_sys->meta )
-    {
-        vlc_meta_Delete( p_sys->meta );
-    }
+
+    DESTROY_PACKETIZER( p_sys->p_packetizer );
+    if( p_sys->meta ) vlc_meta_Delete( p_sys->meta );
+    if( p_sys->p_block_out ) block_Release( p_sys->p_block_out );
 
     free( p_sys );
 }
@@ -408,44 +351,53 @@ 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;
-    vlc_meta_t **pp_meta;
+    vlc_meta_t *p_meta;
     int i_ret;
 
     switch( i_query )
     {
         case DEMUX_GET_META:
-            pp_meta = (vlc_meta_t **)va_arg( args, vlc_meta_t** );
-            if( p_sys->meta )
-            {
-                *pp_meta = vlc_meta_Duplicate( p_sys->meta );
-            }
-            else
-            {
-                *pp_meta = NULL;
-            }
+            p_meta = (vlc_meta_t *)va_arg( args, vlc_meta_t* );
+            vlc_meta_Merge( p_meta, p_sys->meta );
             return VLC_SUCCESS;
 
         case DEMUX_GET_TIME:
             pi64 = (int64_t*)va_arg( args, int64_t * );
-            *pi64 = date_Get( &p_sys->pts ) + p_sys->i_time_offset;
+            *pi64 = p_sys->i_pts + p_sys->i_time_offset;
             return VLC_SUCCESS;
 
         case DEMUX_SET_TIME:
             /* FIXME TODO: implement a high precision seek (with mp3 parsing)
              * needed for multi-input */
+
         default:
             i_ret = demux2_vaControlHelper( p_demux->s, 0, -1,
                                             p_sys->i_bitrate_avg, 1, i_query,
                                             args );
+            /* No bitrate, we can't have it precisely, but we can compute
+             * a raw approximation with time/position */
+            if( i_ret && i_query == DEMUX_GET_LENGTH &&!p_sys->i_bitrate_avg )
+            {
+                float f_pos = (double)( stream_Tell( p_demux->s ) ) /
+                              (double)( stream_Size( p_demux->s ) );
+                /* The first few seconds are guaranteed to be very whacky,
+                 * don't bother trying ... Too bad */
+                if( f_pos < 0.01 ||
+                    (p_sys->i_pts + p_sys->i_time_offset) < 8000000 )
+                    return VLC_EGENERIC;
+
+                pi64 = (int64_t *)va_arg( args, int64_t * );
+                *pi64 = (p_sys->i_pts + p_sys->i_time_offset) / f_pos;
+                return VLC_SUCCESS;
+            }
             if( !i_ret && p_sys->i_bitrate_avg > 0 &&
                 (i_query == DEMUX_SET_POSITION || i_query == DEMUX_SET_TIME) )
             {
                 int64_t i_time = I64C(8000000) * stream_Tell(p_demux->s) /
                     p_sys->i_bitrate_avg;
 
-                /* fix time_offset */
-                if( i_time >= 0 )
-                    p_sys->i_time_offset = i_time - date_Get( &p_sys->pts );
+                /* Fix time_offset */
+                if( i_time >= 0 ) p_sys->i_time_offset = i_time - p_sys->i_pts;
             }
             return i_ret;
     }