]> git.sesse.net Git - vlc/blobdiff - modules/mux/mpeg/ts.c
* modules/stream_out/transcode.c: new options
[vlc] / modules / mux / mpeg / ts.c
index d6756d4c0d775dca59b758505e972c122625b449..1a3fb6c6a5e7a81e5a8f4941f299038b5a864009 100644 (file)
@@ -2,7 +2,7 @@
  * ts.c: MPEG-II TS Muxer
  *****************************************************************************
  * Copyright (C) 2001, 2002 VideoLAN
- * $Id: ts.c,v 1.27 2003/08/14 11:47:32 gbazin Exp $
+ * $Id: ts.c,v 1.41 2003/11/27 22:44:50 massiot Exp $
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Eric Petit <titer@videolan.org>
@@ -59,6 +59,8 @@
  *  - check PCR frequency requirement
  *  - check PAT/PMT  "        "
  *  - check PCR/PCR "soft"
+ *  - check if "registration" descriptor : "AC-3" should be a program
+ *    descriptor or an es one. (xine want an es one)
  *
  *  - remove creation of PAT/PMT without dvbpsi
  *  - ?
@@ -99,7 +101,7 @@ static int     Mux      ( sout_mux_t * );
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
-#define SOUT_BUFFER_FLAGS_PRIVATE_PCR_SOFT  ( 1 << SOUT_BUFFER_FLAGS_PRIVATE_SHIFT )
+#define SOUT_BUFFER_FLAGS_PRIVATE_PCR  ( 1 << SOUT_BUFFER_FLAGS_PRIVATE_SHIFT )
 typedef struct
 {
     int           i_depth;
@@ -143,6 +145,16 @@ static inline sout_buffer_t *BufferChainGet( sout_buffer_chain_t *c )
     }
     return b;
 }
+static inline void BufferChainClean( sout_instance_t *p_sout, sout_buffer_chain_t *c )
+{
+    sout_buffer_t *b;
+
+    while( ( b = BufferChainGet( c ) ) )
+    {
+        sout_BufferDelete( p_sout, b );
+    }
+    BufferChainInit( c );
+}
 
 typedef struct ts_stream_s
 {
@@ -161,8 +173,10 @@ typedef struct ts_stream_s
     int             i_decoder_specific_info;
     uint8_t         *p_decoder_specific_info;
 
-    /* for TS building */
-    sout_buffer_chain_t chain_ts;
+    sout_buffer_chain_t chain_pes;
+    mtime_t             i_pes_dts;
+    mtime_t             i_pes_length;
+    int                 i_pes_used;
 
 } ts_stream_t;
 
@@ -171,13 +185,11 @@ struct sout_mux_sys_t
     int             i_pcr_pid;
     sout_input_t    *p_pcr_input;
 
-    int             i_stream_id_mpga;
-    int             i_stream_id_mpgv;
-    int             i_stream_id_a52;
-
     int             i_audio_bound;
     int             i_video_bound;
 
+    int             i_pid_video;
+    int             i_pid_audio;
     int             i_pid_free; // first usable pid
 
     int             i_pat_version_number;
@@ -193,30 +205,44 @@ struct sout_mux_sys_t
     /* for TS building */
     int64_t             i_bitrate_min;
     int64_t             i_bitrate_max;
+
+    int64_t             i_caching_delay;
     int64_t             i_pcr_delay;
-    int64_t             i_pcr_soft_delay;
 
-    mtime_t             i_pcr;  /* last PCR emited (for pcr-soft) */
-    mtime_t             i_dts;
-    mtime_t             i_length;
-    sout_buffer_chain_t chain_ts;
+    int64_t             i_dts_delay;
+
+    mtime_t             i_pcr;  /* last PCR emited */
 };
 
 
 /* Reserve a pid and return it */
-static int  AllocatePID( sout_mux_sys_t *p_sys )
+static int  AllocatePID( sout_mux_sys_t *p_sys, int i_cat )
 {
-    return( ++p_sys->i_pid_free );
+    int i_pid;
+    if ( i_cat == VIDEO_ES && p_sys->i_pid_video )
+    {
+        i_pid = p_sys->i_pid_video;
+        p_sys->i_pid_video = 0;
+    }
+    else if ( i_cat == AUDIO_ES && p_sys->i_pid_audio )
+    {
+        i_pid = p_sys->i_pid_audio;
+        p_sys->i_pid_audio = 0;
+    }
+    else
+    {
+        i_pid = ++p_sys->i_pid_free;
+    }
+    return i_pid;
 }
 
 static void GetPAT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
 static void GetPMT( sout_mux_t *p_mux, sout_buffer_chain_t *c );
 
-static int  TSFill   ( sout_mux_t *, sout_input_t * );
-static void PEStoTS  ( sout_instance_t *, sout_buffer_chain_t *, sout_buffer_t *, ts_stream_t *, vlc_bool_t );
-static void TSSetDate( sout_buffer_chain_t *, mtime_t, mtime_t );
-static void TSSetConstraints( sout_mux_t*, sout_buffer_chain_t *,
-                              mtime_t i_length, int i_bitrate_min, int i_bitrate_max );
+static sout_buffer_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream, vlc_bool_t b_pcr );
+static void TSSetPCR( sout_buffer_t *p_ts, mtime_t i_dts );
+
+static void PEStoTS  ( sout_instance_t *, sout_buffer_chain_t *, sout_buffer_t *, ts_stream_t * );
 
 /*****************************************************************************
  * Open:
@@ -240,10 +266,6 @@ static int Open( vlc_object_t *p_this )
 
     srand( (uint32_t)mdate() );
 
-    p_sys->i_stream_id_mpga = 0xc0;
-    p_sys->i_stream_id_a52  = 0x80;
-    p_sys->i_stream_id_mpgv = 0xe0;
-
     p_sys->i_audio_bound = 0;
     p_sys->i_video_bound = 0;
 
@@ -257,6 +279,25 @@ static int Open( vlc_object_t *p_this )
 
     p_sys->i_pid_free = 0x43;
 
+    p_sys->i_pid_video = 0;
+    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "pid-video" ) ) )
+    {
+        p_sys->i_pid_video = strtol( val, NULL, 0 );
+        if ( p_sys->i_pid_video > p_sys->i_pid_free )
+        {
+            p_sys->i_pid_free = p_sys->i_pid_video + 1;
+        }
+    }
+    p_sys->i_pid_audio = 0;
+    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "pid-audio" ) ) )
+    {
+        p_sys->i_pid_audio = strtol( val, NULL, 0 );
+        if ( p_sys->i_pid_audio > p_sys->i_pid_free )
+        {
+            p_sys->i_pid_free = p_sys->i_pid_audio + 1;
+        }
+    }
+
     p_sys->i_pcr_pid = 0x1fff;
     p_sys->p_pcr_input = NULL;
 
@@ -283,40 +324,48 @@ static int Open( vlc_object_t *p_this )
         p_sys->i_bitrate_min = 0;
         p_sys->i_bitrate_max = 0;
     }
-    p_sys->i_pcr_delay = 100000;
-    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "pcr" ) ) )
+    if( p_sys->i_bitrate_min > 0 || p_sys->i_bitrate_max > 0 )
     {
-        p_sys->i_pcr_delay = (int64_t)atoi( val ) * 1000;
-        if( p_sys->i_pcr_delay <= 0 )
+        msg_Err( p_mux, "bmin and bmax no more supported (if you need them report it)" );
+    }
+
+    p_sys->i_caching_delay = 200000;
+    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "caching" ) ) )
+    {
+        p_sys->i_caching_delay = (int64_t)atoi( val ) * 1000;
+        if( p_sys->i_caching_delay <= 0 )
         {
             msg_Err( p_mux,
-                     "invalid pcr delay ("I64Fd"ms) reseting to 100ms",
-                     p_sys->i_pcr_delay / 1000 );
-            p_sys->i_pcr_delay = 100000;
+                     "invalid caching ("I64Fd"ms) reseting to 200ms",
+                     p_sys->i_caching_delay / 1000 );
+            p_sys->i_caching_delay = 200000;
         }
     }
-    p_sys->i_pcr_soft_delay = 0;
-    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "pcr-soft" ) ) )
+    p_sys->i_pcr_delay = 30000;
+    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "pcr" ) ) )
     {
-        p_sys->i_pcr_soft_delay = (int64_t)atoi( val ) * 1000;
-        if( p_sys->i_pcr_soft_delay <= 0 ||
-            p_sys->i_pcr_soft_delay >= p_sys->i_pcr_delay )
+        p_sys->i_pcr_delay = (int64_t)atoi( val ) * 1000;
+        if( p_sys->i_pcr_delay <= 0 ||
+            p_sys->i_pcr_delay >= p_sys->i_caching_delay )
         {
             msg_Err( p_mux,
-                     "invalid pcr-soft delay ("I64Fd"ms) disabled",
-                     p_sys->i_pcr_soft_delay / 1000 );
-            p_sys->i_pcr_soft_delay = 0;
+                     "invalid pcr delay ("I64Fd"ms) reseting to 30ms",
+                     p_sys->i_pcr_delay / 1000 );
+            p_sys->i_pcr_delay = 30000;
         }
     }
 
-    msg_Dbg( p_mux, "pcr_delay="I64Fd" pcr_soft_delay="I64Fd,
-             p_sys->i_pcr_delay, p_sys->i_pcr_soft_delay );
+    msg_Dbg( p_mux, "caching="I64Fd" pcr="I64Fd,
+             p_sys->i_caching_delay, p_sys->i_pcr_delay );
+
+    p_sys->i_dts_delay = 200000;
+    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "dts-delay" ) ) )
+    {
+        p_sys->i_dts_delay = (int64_t)atoi( val ) * 1000;
+    }
 
-    /* for TS génération */
+    /* for TS generation */
     p_sys->i_pcr    = 0;
-    p_sys->i_dts    = 0;
-    p_sys->i_length = 0;
-    BufferChainInit( &p_sys->chain_ts );
     return VLC_SUCCESS;
 }
 
@@ -327,16 +376,9 @@ static void Close( vlc_object_t * p_this )
 {
     sout_mux_t          *p_mux = (sout_mux_t*)p_this;
     sout_mux_sys_t      *p_sys = p_mux->p_sys;
-    sout_buffer_t       *p_data;
 
     msg_Dbg( p_mux, "Close" );
 
-    /* Empty TS buffer */
-    while( ( p_data = BufferChainGet( &p_sys->chain_ts ) ) )
-    {
-        sout_BufferDelete( p_mux->p_sout, p_data );
-    }
-
     free( p_sys );
 }
 
@@ -363,27 +405,26 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
     sout_mux_sys_t      *p_sys = p_mux->p_sys;
     ts_stream_t         *p_stream;
 
-    msg_Dbg( p_mux, "adding input codec=%4.4s", (char*)&p_input->p_fmt->i_fourcc );
-
     p_input->p_sys = (void*)p_stream = malloc( sizeof( ts_stream_t ) );
 
     /* Init this new stream */
-    p_stream->i_pid = AllocatePID( p_sys );
+    p_stream->i_pid = AllocatePID( p_sys, p_input->p_fmt->i_cat );
     p_stream->i_continuity_counter    = 0;
     p_stream->i_decoder_specific_info = 0;
     p_stream->p_decoder_specific_info = NULL;
 
+    msg_Dbg( p_mux, "adding input codec=%4.4s pid=%d", (char*)&p_input->p_fmt->i_codec, p_stream->i_pid );
+
     /* All others fields depand on codec */
     switch( p_input->p_fmt->i_cat )
     {
         case VIDEO_ES:
-            switch( p_input->p_fmt->i_fourcc )
+            switch( p_input->p_fmt->i_codec )
             {
                 case VLC_FOURCC( 'm', 'p','g', 'v' ):
                     /* TODO: do we need to check MPEG-I/II ? */
                     p_stream->i_stream_type = 0x02;
-                    p_stream->i_stream_id = p_sys->i_stream_id_mpgv;
-                    p_sys->i_stream_id_mpgv++;
+                    p_stream->i_stream_id = 0xe0;
                     break;
                 case VLC_FOURCC( 'm', 'p','4', 'v' ):
                     p_stream->i_stream_type = 0x10;
@@ -403,9 +444,9 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
                 case VLC_FOURCC( 'M', 'J', 'P', 'G' ):
                     p_stream->i_stream_type = 0xa0; // private
                     p_stream->i_stream_id = 0xa0;   // beurk
-                    p_stream->i_bih_codec  = p_input->p_fmt->i_fourcc;
-                    p_stream->i_bih_width  = p_input->p_fmt->i_width;
-                    p_stream->i_bih_height = p_input->p_fmt->i_height;
+                    p_stream->i_bih_codec  = p_input->p_fmt->i_codec;
+                    p_stream->i_bih_width  = p_input->p_fmt->video.i_width;
+                    p_stream->i_bih_height = p_input->p_fmt->video.i_height;
                     break;
                 default:
                     free( p_stream );
@@ -415,18 +456,25 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             break;
 
         case AUDIO_ES:
-            switch( p_input->p_fmt->i_fourcc )
+            switch( p_input->p_fmt->i_codec )
             {
                 case VLC_FOURCC( 'm', 'p','g', 'a' ):
-                    p_stream->i_stream_type = p_input->p_fmt->i_sample_rate >= 32000 ? 0x03 : 0x04;
-                    p_stream->i_stream_id = p_sys->i_stream_id_mpga;
-                    p_sys->i_stream_id_mpga++;
+                    p_stream->i_stream_type = p_input->p_fmt->audio.i_rate >= 32000 ? 0x03 : 0x04;
+                    p_stream->i_stream_id = 0xc0;
                     break;
                 case VLC_FOURCC( 'a', '5','2', ' ' ):
                     p_stream->i_stream_type = 0x81;
-                    p_stream->i_stream_id = p_sys->i_stream_id_a52;
-                    p_sys->i_stream_id_a52++;
+                    p_stream->i_stream_id = 0xbd;
+                    break;
+                case VLC_FOURCC( 'l', 'p','c', 'm' ):
+                    p_stream->i_stream_type = 0x83;
+                    p_stream->i_stream_id = 0xbd;
                     break;
+                case VLC_FOURCC( 'd', 't','s', ' ' ):
+                    p_stream->i_stream_type = 0x85;
+                    p_stream->i_stream_id = 0xbd;
+                    break;
+
                 case VLC_FOURCC( 'm', 'p','4', 'a' ):
                     p_stream->i_stream_type = 0x11;
                     p_stream->i_stream_id = 0xfa;
@@ -441,11 +489,11 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
             break;
 
         case SPU_ES:
-            switch( p_input->p_fmt->i_fourcc )
+            switch( p_input->p_fmt->i_codec )
             {
                 case VLC_FOURCC( 's', 'p','u', ' ' ):
                     p_stream->i_stream_type = 0x82;
-                    p_stream->i_stream_id = 0x82;
+                    p_stream->i_stream_id = 0xbd;
                     break;
                 default:
                     free( p_stream );
@@ -459,17 +507,21 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
     }
 
     /* Copy extra data (VOL for MPEG-4 and extra BitMapInfoHeader for VFW */
-    p_stream->i_decoder_specific_info = p_input->p_fmt->i_extra_data;
+    p_stream->i_decoder_specific_info = p_input->p_fmt->i_extra;
     if( p_stream->i_decoder_specific_info > 0 )
     {
         p_stream->p_decoder_specific_info =
             malloc( p_stream->i_decoder_specific_info );
         memcpy( p_stream->p_decoder_specific_info,
-                p_input->p_fmt->p_extra_data,
-                p_input->p_fmt->i_extra_data );
+                p_input->p_fmt->p_extra,
+                p_input->p_fmt->i_extra );
     }
-    /* Init chain for TS building */
-    BufferChainInit( &p_stream->chain_ts );
+
+    /* Init pes chain */
+    BufferChainInit( &p_stream->chain_pes );
+    p_stream->i_pes_dts    = 0;
+    p_stream->i_pes_length = 0;
+    p_stream->i_pes_used   = 0;
 
     /* We only change PMT version (PAT isn't changed) */
     p_sys->i_pmt_version_number = ( p_sys->i_pmt_version_number + 1 )%32;
@@ -478,26 +530,15 @@ static int AddStream( sout_mux_t *p_mux, sout_input_t *p_input )
     if( p_input->p_fmt->i_cat != SPU_ES &&
         ( p_sys->i_pcr_pid == 0x1fff || p_input->p_fmt->i_cat == VIDEO_ES ) )
     {
-        sout_buffer_t *p_data;
-
         if( p_sys->p_pcr_input )
         {
             /* There was already a PCR stream, so clean context */
-            ts_stream_t   *p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
-
-            while( ( p_data = BufferChainGet( &p_pcr_stream->chain_ts ) ) )
-            {
-                sout_BufferDelete( p_mux->p_sout, p_data );
-            }
+            /* FIXME */
         }
         p_sys->i_pcr_pid   = p_stream->i_pid;
         p_sys->p_pcr_input = p_input;
 
-        /* Empty TS buffer (avoid broken data/problems with pcr stream changement ) */
-        while( ( p_data = BufferChainGet( &p_sys->chain_ts ) ) )
-        {
-            sout_BufferDelete( p_mux->p_sout, p_data );
-        }
+        msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
     }
 
     return VLC_SUCCESS;
@@ -510,10 +551,10 @@ static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
 {
     sout_mux_sys_t  *p_sys = p_mux->p_sys;
     ts_stream_t     *p_stream;
-    sout_buffer_t   *p_data;
+    char            *val;
 
-    msg_Dbg( p_mux, "removing input" );
     p_stream = (ts_stream_t*)p_input->p_sys;
+    msg_Dbg( p_mux, "removing input pid=%d", p_stream->i_pid );
 
     if( p_sys->i_pcr_pid == p_stream->i_pid )
     {
@@ -547,18 +588,14 @@ static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
         if( p_sys->p_pcr_input )
         {
             /* Empty TS buffer */
-            while( ( p_data = BufferChainGet( &((ts_stream_t*)p_sys->p_pcr_input->p_sys)->chain_ts ) ) )
-            {
-                sout_BufferDelete( p_mux->p_sout, p_data );
-            }
+            /* FIXME */
         }
+        msg_Dbg( p_mux, "new PCR PID is %d", p_sys->i_pcr_pid );
     }
 
-    /* Empty all data in chain_ts */
-    while( ( p_data = BufferChainGet( &p_stream->chain_ts ) ) )
-    {
-        sout_BufferDelete( p_mux->p_sout, p_data );
-    }
+    /* Empty all data in chain_pes */
+    BufferChainClean( p_mux->p_sout, &p_stream->chain_pes );
+
     if( p_stream->p_decoder_specific_info )
     {
         free( p_stream->p_decoder_specific_info );
@@ -567,17 +604,29 @@ static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
     {
         p_sys->i_mpeg4_streams--;
     }
+    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "pid-video" ) ) )
+    {
+        int i_pid_video = strtol( val, NULL, 0 );
+        if ( i_pid_video == p_stream->i_pid )
+        {
+            p_sys->i_pid_video = i_pid_video;
+            msg_Dbg( p_mux, "freeing video PID %d", i_pid_video );
+        }
+    }
+    if( ( val = sout_cfg_find_value( p_mux->p_cfg, "pid-audio" ) ) )
+    {
+        int i_pid_audio = strtol( val, NULL, 0 );
+        if ( i_pid_audio == p_stream->i_pid )
+        {
+            p_sys->i_pid_audio = i_pid_audio;
+            msg_Dbg( p_mux, "freeing audio PID %d", i_pid_audio );
+        }
+    }
     free( p_stream );
 
     /* We only change PMT version (PAT isn't changed) */
     p_sys->i_pmt_version_number++; p_sys->i_pmt_version_number %= 32;
 
-    /*Empty TS buffer (avoid broken data/problems with pcr stream changement) */
-    while( ( p_data = BufferChainGet( &p_sys->chain_ts ) ) )
-    {
-        sout_BufferDelete( p_mux->p_sout, p_data );
-    }
-
     return VLC_SUCCESS;
 }
 
@@ -589,9 +638,7 @@ static int DelStream( sout_mux_t *p_mux, sout_input_t *p_input )
 static int Mux( sout_mux_t *p_mux )
 {
     sout_mux_sys_t  *p_sys = p_mux->p_sys;
-
-    sout_input_t    *p_pcr_input = p_sys->p_pcr_input;
-    ts_stream_t     *p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
+    ts_stream_t     *p_pcr_stream;
 
     if( p_sys->i_pcr_pid == 0x1fff )
     {
@@ -599,196 +646,334 @@ static int Mux( sout_mux_t *p_mux )
         msleep( 1000 );
         return VLC_SUCCESS;
     }
+    p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
 
     for( ;; )
     {
-        ts_stream_t   *p_stream = NULL;
-        sout_buffer_t *p_data;
-
-        int     i_stream, i;
-        mtime_t i_dts;
+        sout_buffer_chain_t chain_ts;
+        int                 i_packet_count;
+        int                 i_packet_pos;
+        mtime_t             i_pcr_dts;
+        mtime_t             i_pcr_length;
+        int i;
 
-        /* fill ts packets for pcr XXX before GetPAT/GetPMT */
-        if( p_pcr_stream->chain_ts.p_first == NULL && TSFill( p_mux, p_pcr_input ) )
+        /* 1: get enough PES packet for all input */
+        for( ;; )
         {
-            /* We need more data */
-            return VLC_SUCCESS;
+            vlc_bool_t b_ok = VLC_TRUE;
+            sout_buffer_t *p_data;
+
+            /* Accumulate enough data in the pcr stream (>i_caching_delay) */
+            /* Accumulate enough data in all other stream ( >= length of pcr) */
+            for( i = 0; i < p_mux->i_nb_inputs; i++ )
+            {
+                sout_input_t *p_input = p_mux->pp_inputs[i];
+                ts_stream_t *p_stream = (ts_stream_t*)p_input->p_sys;
+
+                if( ( p_stream == p_pcr_stream && p_stream->i_pes_length <= p_sys->i_caching_delay ) ||
+                    p_stream->i_pes_dts + p_stream->i_pes_length < p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
+                {
+                    /* Need more data */
+                    if( p_input->p_fifo->i_depth <= 1 )
+                    {
+                        if( p_input->p_fmt->i_cat == AUDIO_ES ||
+                            p_input->p_fmt->i_cat == VIDEO_ES )
+                        {
+                            /* We need more data */
+                            return VLC_SUCCESS;
+                        }
+                        else if( p_input->p_fifo->i_depth <= 0 )
+                        {
+                            /* spu, only one packet is needed */
+                            continue;
+                        }
+                    }
+                    b_ok = VLC_FALSE;
+
+                    p_data = sout_FifoGet( p_input->p_fifo );
+                    if( p_input->p_fifo->i_depth > 0 )
+                    {
+                        sout_buffer_t *p_next = sout_FifoShow( p_input->p_fifo );
+
+                        p_data->i_length = p_next->i_dts - p_data->i_dts;
+                    }
+
+                    if( ( p_pcr_stream->i_pes_dts > 0 && p_data->i_dts - 2000000 > p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length ) ||
+                        p_data->i_dts < p_stream->i_pes_dts ||
+                        ( p_stream->i_pes_dts > 0 && p_data->i_dts - 2000000 > p_stream->i_pes_dts + p_stream->i_pes_length ) )
+                    {
+                        msg_Warn( p_mux, "packet with too strange dts (dts=%lld,old=%lld,pcr=%lld)",
+                                  p_data->i_dts,
+                                  p_stream->i_pes_dts,
+                                  p_pcr_stream->i_pes_dts );
+                        sout_BufferDelete( p_mux->p_sout, p_data );
+
+                        BufferChainClean( p_mux->p_sout, &p_stream->chain_pes );
+                        p_stream->i_pes_dts = 0;
+                        p_stream->i_pes_used = 0;
+                        p_stream->i_pes_length = 0;
+
+                        BufferChainClean( p_mux->p_sout, &p_pcr_stream->chain_pes );
+                        p_pcr_stream->i_pes_dts = 0;
+                        p_pcr_stream->i_pes_used = 0;
+                        p_pcr_stream->i_pes_length = 0;
+
+                    }
+                    else
+                    {
+                        p_stream->i_pes_length += p_data->i_length;
+                        if( p_stream->i_pes_dts == 0 )
+                        {
+                            p_stream->i_pes_dts = p_data->i_dts;
+                        }
+
+                        /* Convert to pes */
+                        E_( EStoPES )( p_mux->p_sout, &p_data, p_data, p_stream->i_stream_id, 1 );
+
+                        BufferChainAppend( &p_stream->chain_pes, p_data );
+                    }
+                }
+            }
+
+            if( b_ok )
+            {
+                break;
+            }
         }
 
-        if( p_sys->chain_ts.p_first == NULL  )
+        /* save */
+        i_pcr_dts      = p_pcr_stream->i_pes_dts;
+        i_pcr_length   = p_pcr_stream->i_pes_length;
+
+        /* msg_Dbg( p_mux, "starting muxing %lldms", i_pcr_length / 1000 ); */
+        /* 2: calculate non accurate total size of muxed ts */
+        i_packet_count = 0;
+        for( i = 0; i < p_mux->i_nb_inputs; i++ )
         {
-            /* Every pcr packet send PAT/PMT */
-            GetPAT( p_mux, &p_sys->chain_ts);
-            GetPMT( p_mux, &p_sys->chain_ts );
+            ts_stream_t *p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
+            sout_buffer_t *p_pes;
+
+            /* False for pcr stream but it will be eough to do PCR algo */
+            for( p_pes = p_stream->chain_pes.p_first; p_pes != NULL; p_pes = p_pes->p_next )
+            {
+                int i_size = p_pes->i_size;
+                if( p_pes->i_dts + p_pes->i_length > p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length )
+                {
+                    mtime_t i_frag = p_pcr_stream->i_pes_dts + p_pcr_stream->i_pes_length - p_pes->i_dts;
+                    if( i_frag < 0 )
+                    {
+                        /* Next stream */
+                        break;
+                    }
+                    i_size = p_pes->i_size * i_frag / p_pes->i_length;
+                }
+                i_packet_count += ( i_size + 183 ) / 184;
+            }
         }
+        /* add overhead for PCR (not really exact) */
+        i_packet_count += ( 8 * i_pcr_length / p_sys->i_pcr_delay + 175 ) / 176;
 
-        /* search stream with lowest dts */
-        for( i = 0, i_stream = -1, i_dts = 0; i < p_mux->i_nb_inputs; i++ )
+
+        /* 3: mux PES into TS */
+        BufferChainInit( &chain_ts );
+        /* append PAT/PMT  -> FIXME with big pcr delay it won't have enough pat/pmt */
+        GetPAT( p_mux, &chain_ts);
+        GetPMT( p_mux, &chain_ts );
+        i_packet_pos = 0;
+        i_packet_count += chain_ts.i_depth;
+        /* msg_Dbg( p_mux, "estimated pck=%d", i_packet_count ); */
+
+        for( ;; )
         {
-            p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
+            int         i_stream;
+            mtime_t     i_dts;
+            ts_stream_t *p_stream;
+            sout_buffer_t *p_ts;
+            vlc_bool_t   b_pcr;
 
-            if( p_stream->chain_ts.p_first == NULL )
+            /* Select stream (lowest dts)*/
+            for( i = 0, i_stream = -1, i_dts = 0; i < p_mux->i_nb_inputs; i++ )
             {
-                if( TSFill( p_mux, p_mux->pp_inputs[i] ) )
+                p_stream = (ts_stream_t*)p_mux->pp_inputs[i]->p_sys;
+
+                if( p_stream->i_pes_dts == 0 )
                 {
-                    /* We need more data */
-                    return VLC_SUCCESS;
+                    continue;
                 }
-                if( p_stream->chain_ts.p_first == NULL )
+
+                if( i_stream == -1 ||
+                    p_stream->i_pes_dts < i_dts )
                 {
-                    continue; /* SPU_ES */
+                    i_stream = i;
+                    i_dts = p_stream->i_pes_dts;
                 }
             }
+            if( i_stream == -1 )
+            {
+                break;
+            }
+            p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
 
-            if( i_stream == -1 ||
-                p_stream->chain_ts.p_first->i_dts < i_dts )
+            /* do we need to issue pcr */
+            b_pcr = VLC_FALSE;
+            if( p_stream == p_pcr_stream &&
+                i_pcr_dts + i_packet_pos * i_pcr_length / i_packet_count >= p_sys->i_pcr + p_sys->i_pcr_delay )
             {
-                i_stream = i;
-                i_dts = p_stream->chain_ts.p_first->i_dts;
+                b_pcr = VLC_TRUE;
+                p_sys->i_pcr = i_pcr_dts + i_packet_pos * i_pcr_length / i_packet_count;
             }
-        }
 
-        p_stream = (ts_stream_t*)p_mux->pp_inputs[i_stream]->p_sys;
+            /* Build the TS packet */
+            p_ts = TSNew( p_mux, p_stream, b_pcr );
+            i_packet_pos++;
 
-        p_data = BufferChainGet( &p_stream->chain_ts );
-        BufferChainAppend( &p_sys->chain_ts, p_data );
+            /* */
+            BufferChainAppend( &chain_ts, p_ts );
+        }
 
-        if( p_stream->i_pid == p_pcr_stream->i_pid && p_stream->chain_ts.p_first == NULL )
+        /* 4: date and send */
+        i_packet_count = chain_ts.i_depth;
+        /* msg_Dbg( p_mux, "real pck=%d", i_packet_count ); */
+        for( i = 0; i < i_packet_count; i++ )
         {
-            sout_buffer_t *p_ts = p_sys->chain_ts.p_first;
+            sout_buffer_t *p_ts = BufferChainGet( &chain_ts );
 
-            /* We have consume all TS packets from the PCR stream */
+            p_ts->i_dts    = i_pcr_dts + i_pcr_length * i / i_packet_count;
+            p_ts->i_length = i_pcr_length / i_packet_count;
 
-            if( p_sys->i_length > p_sys->i_pcr_delay )
+            if( p_ts->i_flags&SOUT_BUFFER_FLAGS_PRIVATE_PCR )
             {
-                /* Send TS data if last PCR was i_pcr_delay ago */
-
-                if( p_sys->i_bitrate_min > 0 ||
-                    p_sys->i_bitrate_max > 0 )
-                {
-                    TSSetConstraints( p_mux, &p_sys->chain_ts, p_sys->i_length,
-                                      p_sys->i_bitrate_min, p_sys->i_bitrate_max );
-                }
-
-                /* Send all data */
-                TSSetDate( &p_sys->chain_ts,
-                           p_sys->i_dts + 3  * p_sys->i_pcr_delay / 2,    /* latency is equal to i_pcr_delay, 3/2 is for security */
-                           p_sys->i_length );
-                sout_AccessOutWrite( p_mux->p_access, p_ts );
+                /* msg_Dbg( p_mux, "pcr=%lld ms", p_ts->i_dts / 1000 ); */
+                TSSetPCR( p_ts, p_ts->i_dts - p_sys->i_dts_delay );
+            }
 
-                /* Reset the ts chain */
-                BufferChainInit( &p_sys->chain_ts );
+            /* latency */
+            p_ts->i_dts += 3*p_sys->i_caching_delay/2;
 
-                p_sys->i_length = 0;
-            }
+            sout_AccessOutWrite( p_mux->p_access, p_ts );
         }
     }
-
-    return VLC_SUCCESS;
 }
 
+static sout_buffer_t *TSNew( sout_mux_t *p_mux, ts_stream_t *p_stream, vlc_bool_t b_pcr )
+{
+    sout_buffer_t *p_pes = p_stream->chain_pes.p_first;
+    sout_buffer_t *p_ts;
 
+    vlc_bool_t b_new_pes = VLC_FALSE;
+    vlc_bool_t b_adaptation_field = VLC_FALSE;
 
-/*****************************************************************************
- *
- *
- *****************************************************************************/
-static int TSFill( sout_mux_t *p_mux, sout_input_t *p_input )
-{
-    sout_mux_sys_t  *p_sys = p_mux->p_sys;
+    int        i_payload_max = 184 - ( b_pcr ? 8 : 0 );
+    int        i_payload;
 
-    ts_stream_t     *p_pcr_stream = (ts_stream_t*)p_sys->p_pcr_input->p_sys;
-    ts_stream_t     *p_stream = (ts_stream_t*)p_input->p_sys;
-    mtime_t         i_dts, i_length;
-    sout_buffer_t   *p_data;
-    vlc_bool_t      b_pcr = VLC_FALSE;
-    vlc_bool_t      b_pcr_soft = VLC_FALSE;
+    if( p_stream->i_pes_used <= 0 )
+    {
+        b_new_pes = VLC_TRUE;
+    }
+    i_payload = __MIN( (int)p_pes->i_size - p_stream->i_pes_used, i_payload_max );
 
+    if( b_pcr || i_payload < i_payload_max )
+    {
+        b_adaptation_field = VLC_TRUE;
+    }
 
-    for( ;; )
+    p_ts = sout_BufferNew( p_mux->p_sout, 188 );
+
+    p_ts->p_buffer[0] = 0x47;
+    p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|( ( p_stream->i_pid >> 8 )&0x1f );
+    p_ts->p_buffer[2] = p_stream->i_pid & 0xff;
+    p_ts->p_buffer[3] = ( b_adaptation_field ? 0x30 : 0x10 )|p_stream->i_continuity_counter;
+
+    p_stream->i_continuity_counter = (p_stream->i_continuity_counter+1)%16;
+
+    if( b_adaptation_field )
     {
-        if( p_input->p_fifo->i_depth <= 0 )
+        int i;
+
+        if( b_pcr )
         {
-            if( p_input->p_fmt->i_cat == AUDIO_ES ||
-                p_input->p_fmt->i_cat == VIDEO_ES )
-            {
-                /* We need more data */
-                return VLC_EGENERIC;
-            }
-            else
+            int     i_stuffing = i_payload_max - i_payload;
+
+            p_ts->i_flags |= SOUT_BUFFER_FLAGS_PRIVATE_PCR;
+
+            p_ts->p_buffer[4] = 7 + i_stuffing;
+            p_ts->p_buffer[5] = 0x10;   /* flags */
+            p_ts->p_buffer[6] = ( 0 )&0xff;
+            p_ts->p_buffer[7] = ( 0 )&0xff;
+            p_ts->p_buffer[8] = ( 0 )&0xff;
+            p_ts->p_buffer[9] = ( 0 )&0xff;
+            p_ts->p_buffer[10]= ( 0 )&0x80;
+            p_ts->p_buffer[11]= 0;
+
+            for( i = 12; i < 12 + i_stuffing; i++ )
             {
-                return VLC_SUCCESS;
+                p_ts->p_buffer[i] = 0xff;
             }
         }
-        p_data = sout_FifoGet( p_input->p_fifo );
-        i_dts    = p_data->i_dts;
-        i_length = p_data->i_length;
-
-        if(  p_stream->i_pid == p_pcr_stream->i_pid &&
-             p_stream->chain_ts.p_first == NULL )
+        else
         {
-            p_sys->i_length+= i_length;
-            if( p_sys->chain_ts.p_first == NULL )
-            {
-                p_sys->i_dts    = i_dts;
-                p_sys->i_pcr    = i_dts;
-                b_pcr = VLC_TRUE;
-            }
-            else if( p_sys->i_pcr_soft_delay > 0 &&
-                     p_sys->i_pcr + p_sys->i_pcr_soft_delay < i_dts )
+            int i_stuffing = i_payload_max - i_payload;
+
+            p_ts->p_buffer[4] = i_stuffing - 1;
+            if( i_stuffing > 1 )
             {
-                p_sys->i_pcr    = i_dts;
-                b_pcr = VLC_TRUE;
-                b_pcr_soft = VLC_TRUE;
+                p_ts->p_buffer[5] = 0x00;
+                for( i = 6; i < 6 + i_stuffing - 2; i++ )
+                {
+                    p_ts->p_buffer[i] = 0xff;
+                }
             }
-            break;
         }
+    }
 
-        if( ( p_sys->i_dts + p_sys->i_length ) - i_dts > 2000000 ||
-            ( p_sys->i_dts + p_sys->i_length ) - i_dts < -2000000 )
-        {
-            msg_Err( p_mux, "| buffer_dts - pcr_pts | > 2s ("I64Fd") empting "
-                     "pcr TS buffers", p_sys->i_dts + p_sys->i_length - i_dts);
+    /* copy payload */
+    memcpy( &p_ts->p_buffer[188 - i_payload], &p_pes->p_buffer[p_stream->i_pes_used], i_payload );
 
-            sout_BufferDelete( p_mux->p_sout, p_data );
+    p_stream->i_pes_used += i_payload;
+    p_stream->i_pes_dts      = p_pes->i_dts + p_pes->i_length * p_stream->i_pes_used / p_pes->i_size;
+    p_stream->i_pes_length  -= p_pes->i_length * i_payload / p_pes->i_size;
 
-            while( ( p_data = BufferChainGet( &p_pcr_stream->chain_ts ) ) )
-            {
-                sout_BufferDelete( p_mux->p_sout, p_data );
-            }
-            while( ( p_data = BufferChainGet( &p_sys->chain_ts ) ) )
+    if( p_stream->i_pes_used >= (int)p_pes->i_size )
+    {
+        p_pes = BufferChainGet( &p_stream->chain_pes );
+        sout_BufferDelete( p_mux->p_sout, p_pes );
+
+        p_pes = p_stream->chain_pes.p_first;
+        if( p_pes )
+        {
+            p_stream->i_pes_dts    = p_pes->i_dts;
+            p_stream->i_pes_length = 0;
+            while( p_pes )
             {
-                sout_BufferDelete( p_mux->p_sout, p_data );
+                p_stream->i_pes_length += p_pes->i_length;
+
+                p_pes = p_pes->p_next;
             }
-            return VLC_EGENERIC;
         }
-
-        if( i_dts >= p_sys->i_dts )
+        else
         {
-            break;
+            p_stream->i_pes_dts = 0;
+            p_stream->i_pes_length = 0;
         }
-
-        msg_Dbg( p_mux, "dropping buffer size=%d dts="I64Fd" pcr_dts="I64Fd
-                 " diff="I64Fd, p_data->i_size, i_dts, p_sys->i_dts,
-                 p_sys->i_dts + p_sys->i_length - i_dts );
-        sout_BufferDelete( p_mux->p_sout, p_data );
+        p_stream->i_pes_used = 0;
     }
 
-    E_( EStoPES )( p_mux->p_sout, &p_data, p_data, p_stream->i_stream_id, 1);
-
-    BufferChainInit( &p_stream->chain_ts );
-    PEStoTS( p_mux->p_sout, &p_stream->chain_ts, p_data, p_stream, b_pcr );
+    return p_ts;
+}
 
-    TSSetDate( &p_stream->chain_ts, i_dts, i_length );
 
-    if( b_pcr_soft && p_stream->chain_ts.p_first )
-    {
-        p_stream->chain_ts.p_first->i_flags=SOUT_BUFFER_FLAGS_PRIVATE_PCR_SOFT;
-    }
+static void TSSetPCR( sout_buffer_t *p_ts, mtime_t i_dts )
+{
+    mtime_t i_pcr = 9 * i_dts / 100;
 
-    return VLC_SUCCESS;
+    p_ts->p_buffer[6]  = ( i_pcr >> 25 )&0xff;
+    p_ts->p_buffer[7]  = ( i_pcr >> 17 )&0xff;
+    p_ts->p_buffer[8]  = ( i_pcr >> 9  )&0xff;
+    p_ts->p_buffer[9]  = ( i_pcr >> 1  )&0xff;
+    p_ts->p_buffer[10]|= ( i_pcr << 7  )&0x80;
 }
 
+#if 0
 static void TSSetConstraints( sout_mux_t *p_mux, sout_buffer_chain_t *c, mtime_t i_length, int i_bitrate_min, int i_bitrate_max )
 {
     sout_mux_sys_t  *p_sys = p_mux->p_sys;
@@ -872,37 +1057,11 @@ static void TSSetConstraints( sout_mux_t *p_mux, sout_buffer_chain_t *c, mtime_t
         }
     }
 }
-
-static void TSSetDate( sout_buffer_chain_t *c, mtime_t i_dts, mtime_t i_length )
-{
-    sout_buffer_t *p_ts;
-    mtime_t       i_delta = i_length / c->i_depth;
-    int           i_packet = 0;
-
-    for( p_ts = c->p_first; p_ts != NULL; p_ts = p_ts->p_next )
-    {
-        p_ts->i_dts    = i_dts + i_packet * i_length / c->i_depth;  /* avoid rounding error */
-        p_ts->i_length = i_delta;
-
-        if( p_ts->i_flags&SOUT_BUFFER_FLAGS_PRIVATE_PCR_SOFT )
-        {
-            mtime_t i_pcr = 9 * p_ts->i_dts / 100;
-
-            p_ts->p_buffer[6] = ( i_pcr >> 25 )&0xff;
-            p_ts->p_buffer[7] = ( i_pcr >> 17 )&0xff;
-            p_ts->p_buffer[8] = ( i_pcr >> 9  )&0xff;
-            p_ts->p_buffer[9] = ( i_pcr >> 1  )&0xff;
-            p_ts->p_buffer[10]= ( i_pcr << 7  )&0x80;
-        }
-
-        i_packet++;
-    }
-}
+#endif
 
 static void PEStoTS( sout_instance_t *p_sout,
                      sout_buffer_chain_t *c, sout_buffer_t *p_pes,
-                     ts_stream_t *p_stream,
-                     vlc_bool_t b_pcr )
+                     ts_stream_t *p_stream )
 {
     uint8_t *p_data;
     int     i_size;
@@ -912,18 +1071,11 @@ static void PEStoTS( sout_instance_t *p_sout,
     i_size = p_pes->i_size;
     p_data = p_pes->p_buffer;
 
-    /* Set pcr only with valid DTS */
-    if( p_pes->i_dts <= 0 )
-    {
-        b_pcr = VLC_FALSE;
-    }
-
     b_new_pes = VLC_TRUE;
 
     for( ;; )
     {
         int           b_adaptation_field;
-        int           i_payload;
         int           i_copy;
         sout_buffer_t *p_ts;
 
@@ -939,9 +1091,8 @@ static void PEStoTS( sout_instance_t *p_sout,
          * 4b           continuity_counter
          */
 
-        i_payload = 184 - ( b_pcr ? 8 : 0 );
-        i_copy    = __MIN( i_size, i_payload );
-        b_adaptation_field = (b_pcr||i_size<i_payload) ? VLC_TRUE : VLC_FALSE;
+        i_copy    = __MIN( i_size, 184 );
+        b_adaptation_field = i_size < 184 ? VLC_TRUE : VLC_FALSE;
 
         p_ts->p_buffer[0] = 0x47;
         p_ts->p_buffer[1] = ( b_new_pes ? 0x40 : 0x00 )|
@@ -955,42 +1106,18 @@ static void PEStoTS( sout_instance_t *p_sout,
 
         if( b_adaptation_field )
         {
+            int i_stuffing = 184 - i_copy;
             int i;
 
-            if( b_pcr )
+            p_ts->p_buffer[4] = i_stuffing - 1;
+            if( i_stuffing > 1 )
             {
-                mtime_t i_pcr = p_pes->i_dts * 9 / 100;
-                int     i_stuffing = i_payload - i_copy;
-
-                p_ts->p_buffer[4] = 7 + i_stuffing;
-                p_ts->p_buffer[5] = 0x10;   /* flags */
-                p_ts->p_buffer[6] = ( i_pcr >> 25 )&0xff;
-                p_ts->p_buffer[7] = ( i_pcr >> 17 )&0xff;
-                p_ts->p_buffer[8] = ( i_pcr >> 9  )&0xff;
-                p_ts->p_buffer[9] = ( i_pcr >> 1  )&0xff;
-                p_ts->p_buffer[10]= ( i_pcr << 7  )&0x80;
-                p_ts->p_buffer[11]= 0;
-
-                b_pcr = VLC_FALSE;
-                for( i = 12; i < 12 + i_stuffing; i++ )
+                p_ts->p_buffer[5] = 0x00;
+                for( i = 6; i < 6 + i_stuffing - 2; i++ )
                 {
                     p_ts->p_buffer[i] = 0xff;
                 }
             }
-            else
-            {
-                int i_stuffing = i_payload - i_copy;
-
-                p_ts->p_buffer[4] = i_stuffing - 1;
-                if( i_stuffing > 1 )
-                {
-                    p_ts->p_buffer[5] = 0x00;
-                    for( i = 6; i < 6 + i_stuffing - 2; i++ )
-                    {
-                        p_ts->p_buffer[i] = 0xff;
-                    }
-                }
-            }
         }
         /* copy payload */
         memcpy( &p_ts->p_buffer[188 - i_copy], p_data, i_copy );
@@ -1139,7 +1266,7 @@ static void GetPAT( sout_mux_t *p_mux,
 
     p_pat->i_size = bits.i_data;
 
-    PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat, VLC_FALSE );
+    PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat );
 }
 
 static void GetPMT( sout_mux_t *p_mux,
@@ -1195,7 +1322,7 @@ static void GetPMT( sout_mux_t *p_mux,
 
     p_pmt->i_size = bits.i_data;
 
-    PEStoTS( p_mux->p_sout, c, p_pmt, &p_sys->pmt, VLC_FALSE );
+    PEStoTS( p_mux->p_sout, c, p_pmt, &p_sys->pmt );
 }
 #elif defined MODULE_NAME_IS_mux_ts_dvbpsi
 
@@ -1253,7 +1380,7 @@ static void GetPAT( sout_mux_t *p_mux,
 
     p_pat = WritePSISection( p_mux->p_sout, p_section );
 
-    PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat, VLC_FALSE );
+    PEStoTS( p_mux->p_sout, c, p_pat, &p_sys->pat );
 
     dvbpsi_DeletePSISections( p_section );
     dvbpsi_EmptyPAT( &pat );
@@ -1294,6 +1421,11 @@ static void GetPMT( sout_mux_t *p_mux,
         bits_buffer_t bits;
         bits_buffer_t bits_fix_IOD;
 
+        /* Make valgrind happy : it works at byte level not bit one so
+         * bit_write confuse it (but DON'T CHANGE the way that bit_write is
+         * working (needed when fixing some bits) */
+        memset( iod, 0, 4096 );
+
         bits_initwrite( &bits, 4096, iod );
         // IOD_label
         bits_write( &bits, 8,   0x01 );
@@ -1412,47 +1544,41 @@ static void GetPMT( sout_mux_t *p_mux,
                                 p_stream->i_pid );
         if( p_stream->i_stream_id == 0xfa || p_stream->i_stream_id == 0xfb )
         {
-            uint8_t     data[512];
-            bits_buffer_t bits;
+            uint8_t     es_id[2];
 
             /* SL descriptor */
-            bits_initwrite( &bits, 512, data );
-            bits_write( &bits, 16, p_stream->i_es_id );
-
-            dvbpsi_PMTESAddDescriptor( p_es,
-                                       0x1f,
-                                       bits.i_data,
-                                       bits.p_data );
+            es_id[0] = (p_stream->i_es_id >> 8)&0xff;
+            es_id[1] = (p_stream->i_es_id)&0xff;
+            dvbpsi_PMTESAddDescriptor( p_es, 0x1f, 2, es_id );
         }
-        else if( p_stream->i_stream_id == 0xa0 )
+        else if( p_stream->i_stream_type == 0xa0 )
         {
             uint8_t     data[512];
-            uint8_t     fcc[4];
-            bits_buffer_t bits;
-
-            memcpy( fcc, &p_stream->i_bih_codec, 4 );
+            int         i_extra = __MIN( p_stream->i_decoder_specific_info,
+                                        502 );
 
             /* private DIV3 descripor */
-            bits_initwrite( &bits, 512, data );
-            bits_write( &bits, 8,  fcc[0]);
-            bits_write( &bits, 8,  fcc[1]);
-            bits_write( &bits, 8,  fcc[2]);
-            bits_write( &bits, 8,  fcc[3]);
-            bits_write( &bits, 16, p_stream->i_bih_width );
-            bits_write( &bits, 16, p_stream->i_bih_height );
-            bits_write( &bits, 16, p_stream->i_decoder_specific_info );
-            if( p_stream->i_decoder_specific_info > 0 )
+            memcpy( &data[0], &p_stream->i_bih_codec, 4 );
+            data[4] = ( p_stream->i_bih_width >> 8 )&0xff;
+            data[5] = ( p_stream->i_bih_width      )&0xff;
+            data[6] = ( p_stream->i_bih_height>> 8 )&0xff;
+            data[7] = ( p_stream->i_bih_height     )&0xff;
+            data[8] = ( i_extra >> 8 )&0xff;
+            data[9] = ( i_extra      )&0xff;
+            if( i_extra > 0 )
             {
-                int i;
-                for( i = 0; i < p_stream->i_decoder_specific_info; i++ )
-                {
-                    bits_write( &bits, 8, p_stream->p_decoder_specific_info[i] );
-                }
+                memcpy( &data[10], p_stream->p_decoder_specific_info, i_extra );
             }
-            dvbpsi_PMTESAddDescriptor( p_es,
-                                       0xa0,    // private
-                                       bits.i_data,
-                                       bits.p_data );
+
+            /* 0xa0 is private */
+            dvbpsi_PMTESAddDescriptor( p_es, 0xa0, i_extra  + 10, data );
+        }
+        else if( p_stream->i_stream_type == 0x81 )
+        {
+            uint8_t format[4] = { 0x41, 0x43, 0x2d, 0x33 };
+
+            /* "registration" descriptor : "AC-3" */
+            dvbpsi_PMTESAddDescriptor( p_es, 0x05, 4, format );
         }
     }
 
@@ -1460,7 +1586,7 @@ static void GetPMT( sout_mux_t *p_mux,
 
     p_pmt = WritePSISection( p_mux->p_sout, p_section );
 
-    PEStoTS( p_mux->p_sout, c, p_pmt, &p_sys->pmt, VLC_FALSE );
+    PEStoTS( p_mux->p_sout, c, p_pmt, &p_sys->pmt );
 
     dvbpsi_DeletePSISections( p_section );
     dvbpsi_EmptyPMT( &pmt );