]> git.sesse.net Git - vlc/blobdiff - modules/demux/ts.c
mpeg: implement title/seekpoint controls (refs #8455)
[vlc] / modules / demux / ts.c
index 0c83422f9ecbf0da8a4109f625d2377958f43463..b958408c2e4f0765685fa127e2c0bf90f2ab87af 100644 (file)
@@ -61,6 +61,8 @@
 /* TDT support */
 # include <dvbpsi/tot.h>
 
+#include "../mux/mpeg/dvbpsi_compat.h"
+
 #undef TS_DEBUG
 VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
 {
@@ -114,9 +116,6 @@ static void Close ( vlc_object_t * );
 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
   "16 char string (8 hexadecimal bytes).")
 
-#define SILENT_TEXT N_("Silent mode")
-#define SILENT_LONGTEXT N_("Do not complain on encrypted PES.")
-
 
 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
@@ -133,6 +132,8 @@ static void Close ( vlc_object_t * );
     "Seek and position based on a percent byte position, not a PCR generated " \
     "time position. If seeking doesn't work property, turn on this option." )
 
+#define PCR_TEXT N_("Trust in-stream PCR")
+#define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
 
 vlc_module_begin ()
     set_description( N_("MPEG Transport Stream demuxer") )
@@ -141,6 +142,8 @@ vlc_module_begin ()
     set_subcategory( SUBCAT_INPUT_DEMUX )
 
     add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
+    add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
+        change_safe()
     add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
         change_safe()
     add_string( "ts-out", NULL, TSOUT_TEXT, TSOUT_LONGTEXT, true )
@@ -154,11 +157,11 @@ vlc_module_begin ()
     add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
         change_safe()
 
-    add_bool( "ts-silent", false, SILENT_TEXT, SILENT_LONGTEXT, true )
-
     add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
     add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
 
+    add_obsolete_bool( "ts-silent" );
+
     set_capability( "demux", 10 )
     set_callbacks( Open, Close )
     add_shortcut( "ts" )
@@ -209,7 +212,6 @@ typedef struct
 typedef struct
 {
     dvbpsi_handle   handle;
-
     int             i_version;
     int             i_number;
     int             i_pid_pcr;
@@ -309,12 +311,12 @@ struct demux_sys_t
     bool        b_es_id_pid;
     csa_t       *csa;
     int         i_csa_pkt_size;
-    bool        b_silent;
     bool        b_split_es;
 
     bool        b_udp_out;
     int         fd; /* udp socket */
     uint8_t     *buffer;
+    bool        b_trust_pcr;
 
     /* */
     bool        b_access_control;
@@ -343,8 +345,14 @@ static void PIDFillFormat( ts_es_t *es, int i_stream_type );
 
 static void PATCallBack( void*, dvbpsi_pat_t * );
 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t  i_table_id,
+                                 uint16_t i_extension, demux_t * );
+#else
 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
                                  uint8_t  i_table_id, uint16_t i_extension );
+#endif
+
 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
 
 static inline int PIDGet( block_t *p )
@@ -494,6 +502,50 @@ static int DetectPacketSize( demux_t *p_demux )
     return -1;
 }
 
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+static void vlc_dvbpsi_reset( demux_t *p_demux )
+{
+    demux_sys_t *p_sys = p_demux->p_sys;
+
+    ts_pid_t *pat = &p_sys->pid[0];
+    ts_pid_t *sdt = &p_sys->pid[0x11];
+    ts_pid_t *eit = &p_sys->pid[0x12];
+    ts_pid_t *tdt = &p_sys->pid[0x14];
+
+    if( pat->psi->handle )
+    {
+        if( dvbpsi_decoder_present( pat->psi->handle ) )
+            dvbpsi_pat_detach( pat->psi->handle );
+        dvbpsi_delete( pat->psi->handle );
+        pat->psi->handle = NULL;
+    }
+
+    if( sdt->psi->handle )
+    {
+        if( dvbpsi_decoder_present( sdt->psi->handle ) )
+            dvbpsi_DetachDemux( sdt->psi->handle );
+        dvbpsi_delete( sdt->psi->handle );
+        sdt->psi->handle = NULL;
+    }
+
+    if( eit->psi->handle )
+    {
+        if( dvbpsi_decoder_present( eit->psi->handle ) )
+            dvbpsi_DetachDemux( eit->psi->handle );
+        dvbpsi_delete( eit->psi->handle );
+        eit->psi->handle = NULL;
+    }
+
+    if( tdt->psi->handle )
+    {
+        if( dvbpsi_decoder_present( tdt->psi->handle ) )
+            dvbpsi_DetachDemux( tdt->psi->handle );
+        dvbpsi_delete( tdt->psi->handle );
+        tdt->psi->handle = NULL;
+    }
+}
+#endif
+
 /*****************************************************************************
  * Open
  *****************************************************************************/
@@ -537,7 +589,6 @@ static int Open( vlc_object_t *p_this )
     for( int i = 0; i < 8192; i++ )
     {
         ts_pid_t *pid = &p_sys->pid[i];
-
         pid->i_pid      = i;
         pid->b_seen     = false;
         pid->b_valid    = false;
@@ -551,29 +602,80 @@ static int Open( vlc_object_t *p_this )
     p_sys->csa = NULL;
     p_sys->b_start_record = false;
 
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+# define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
+    do { \
+        (table)->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG ); \
+        if( ! (table)->psi->handle ) \
+        { \
+            vlc_mutex_destroy( &p_sys->csa_lock ); \
+            free( p_sys ); \
+            return VLC_ENOMEM; \
+        } \
+        (table)->psi->handle->p_sys = (void *) VLC_OBJECT(obj); \
+        if( !dvbpsi_AttachDemux( (table)->psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
+        { \
+            vlc_dvbpsi_reset( obj ); \
+            vlc_mutex_destroy( &p_sys->csa_lock ); \
+            free( p_sys ); \
+            return VLC_EGENERIC; \
+        } \
+    } while (0);
+#endif
+
     /* Init PAT handler */
     pat = &p_sys->pid[0];
     PIDInit( pat, true, NULL );
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+    pat->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
+    if( !pat->psi->handle )
+    {
+        vlc_mutex_destroy( &p_sys->csa_lock );
+        free( p_sys );
+        return VLC_ENOMEM;
+    }
+    pat->psi->handle->p_sys = (void *) p_demux;
+    if( !dvbpsi_pat_attach( pat->psi->handle, PATCallBack, p_demux ) )
+    {
+        vlc_dvbpsi_reset( p_demux );
+        vlc_mutex_destroy( &p_sys->csa_lock );
+        free( p_sys );
+        return VLC_EGENERIC;
+    }
+#else
     pat->psi->handle = dvbpsi_AttachPAT( PATCallBack, p_demux );
+#endif
     if( p_sys->b_dvb_meta )
     {
         ts_pid_t *sdt = &p_sys->pid[0x11];
         ts_pid_t *eit = &p_sys->pid[0x12];
 
         PIDInit( sdt, true, NULL );
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+        VLC_DVBPSI_DEMUX_TABLE_INIT( sdt, p_demux )
+#else
         sdt->psi->handle =
             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
                                 p_demux );
+#endif
         PIDInit( eit, true, NULL );
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+        VLC_DVBPSI_DEMUX_TABLE_INIT( eit, p_demux )
+#else
         eit->psi->handle =
             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
                                 p_demux );
-
+#endif
         ts_pid_t *tdt = &p_sys->pid[0x14];
         PIDInit( tdt, true, NULL );
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+        VLC_DVBPSI_DEMUX_TABLE_INIT( tdt, p_demux )
+#else
         tdt->psi->handle =
             dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
                                 p_demux );
+#endif
+
         if( p_sys->b_access_control )
         {
             if( SetPIDFilter( p_demux, 0x11, true ) ||
@@ -583,6 +685,10 @@ static int Open( vlc_object_t *p_this )
         }
     }
 
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+# undef VLC_DVBPSI_DEMUX_TABLE_INIT
+#endif
+
     /* Init PMT array */
     TAB_INIT( p_sys->i_pmt, p_sys->pmt );
     p_sys->i_pmt_es = 0;
@@ -590,6 +696,8 @@ static int Open( vlc_object_t *p_this )
     /* Read config */
     p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
 
+    p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
+
     char* psz_string = var_CreateGetString( p_demux, "ts-out" );
     if( psz_string && *psz_string )
     {
@@ -679,7 +787,6 @@ static int Open( vlc_object_t *p_this )
     }
     free( psz_string );
 
-    p_sys->b_silent = var_CreateGetBool( p_demux, "ts-silent" );
     p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
 
     p_sys->i_pid_ref_pcr = -1;
@@ -731,7 +838,14 @@ static void Close( vlc_object_t *p_this )
             switch( pid->i_pid )
             {
             case 0: /* PAT */
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+                if( dvbpsi_decoder_present( pid->psi->handle ) )
+                    dvbpsi_pat_detach( pid->psi->handle );
+                dvbpsi_delete( pid->psi->handle );
+                pid->psi->handle = NULL;
+#else
                 dvbpsi_DetachPAT( pid->psi->handle );
+#endif
                 free( pid->psi );
                 break;
             case 1: /* CAT */
@@ -742,6 +856,10 @@ static void Close( vlc_object_t *p_this )
                 {
                     /* SDT or EIT or TDT */
                     dvbpsi_DetachDemux( pid->psi->handle );
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+                    dvbpsi_delete( pid->psi->handle );
+                    pid->psi->handle = NULL;
+#endif
                     free( pid->psi );
                 }
                 else
@@ -901,6 +1019,7 @@ static int Demux( demux_t *p_demux )
                    p_sys->i_ts_read * p_sys->i_packet_size );
     }
 
+    demux_UpdateTitleFromStream( p_demux );
     return 1;
 }
 
@@ -1071,6 +1190,25 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         return VLC_SUCCESS;
     }
 
+    case DEMUX_GET_TITLE_INFO:
+    {
+        struct input_title_t ***v = va_arg( args, struct input_title_t*** );
+        int *c = va_arg( args, int * );
+
+        *va_arg( args, int* ) = 0; /* Title offset */
+        *va_arg( args, int* ) = 0; /* Chapter offset */
+        return stream_Control( p_demux->s, STREAM_GET_TITLE_INFO, v, c );
+    }
+
+    case DEMUX_SET_TITLE:
+        return stream_Control( p_demux->s, STREAM_SET_TITLE, args );
+
+    case DEMUX_SET_SEEKPOINT:
+        return stream_Control( p_demux->s, STREAM_SET_SEEKPOINT, args );
+
+    case DEMUX_GET_META:
+        return stream_Control( p_demux->s, STREAM_GET_META, args );
+
     case DEMUX_CAN_RECORD:
         pb_bool = (bool*)va_arg( args, bool * );
         *pb_bool = true;
@@ -1084,8 +1222,9 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
         p_sys->b_start_record = b_bool;
         return VLC_SUCCESS;
 
-    case DEMUX_GET_FPS:
-    case DEMUX_SET_TIME:
+    case DEMUX_GET_SIGNAL:
+        return stream_Control( p_demux->s, STREAM_GET_SIGNAL, args );
+
     default:
         return VLC_EGENERIC;
     }
@@ -1123,18 +1262,32 @@ static int UserPmt( demux_t *p_demux, const char *psz_fmt )
     PIDInit( pmt, true, NULL );
 
     /* Dummy PMT */
-    prg = malloc( sizeof( ts_prg_psi_t ) );
+    prg = calloc( 1, sizeof( ts_prg_psi_t ) );
     if( !prg )
         goto error;
 
-    memset( prg, 0, sizeof( ts_prg_psi_t ) );
     prg->i_pid_pcr  = -1;
     prg->i_pid_pmt  = -1;
     prg->i_version  = -1;
     prg->i_number   = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+    prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
+    if( !prg->handle )
+        goto error;
+    prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
+    if( !dvbpsi_pmt_attach( prg->handle,
+                            ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
+                            PMTCallBack, p_demux ) )
+    {
+        dvbpsi_delete( prg->handle );
+        prg->handle = NULL;
+        goto error;
+    }
+#else
     prg->handle     = dvbpsi_AttachPMT(
-        i_number != TS_USER_PMT_NUMBER ? i_number : 1,
+        ((i_number != TS_USER_PMT_NUMBER) ? i_number : 1),
         PMTCallBack, p_demux );
+#endif
     TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
 
     psz = strchr( psz, '=' );
@@ -1215,6 +1368,7 @@ static int UserPmt( demux_t *p_demux, const char *psz_fmt )
     return VLC_SUCCESS;
 
 error:
+    free( prg );
     free( psz_dup );
     return VLC_EGENERIC;
 }
@@ -1226,8 +1380,8 @@ static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
     if( !p_sys->b_access_control )
         return VLC_EGENERIC;
 
-    return stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
-                           ACCESS_SET_PRIVATE_ID_STATE, i_pid, b_selected );
+    return stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE,
+                           i_pid, b_selected );
 }
 
 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
@@ -1345,13 +1499,30 @@ static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
     if( pid->psi )
     {
         if( pid->psi->handle )
+        {
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+            if( dvbpsi_decoder_present( pid->psi->handle ) )
+                dvbpsi_pmt_detach( pid->psi->handle );
+            dvbpsi_delete( pid->psi->handle );
+            pid->psi->handle = NULL;
+#else
             dvbpsi_DetachPMT( pid->psi->handle );
+#endif
+        }
         for( int i = 0; i < pid->psi->i_prg; i++ )
         {
             if( pid->psi->prg[i]->iod )
                 IODFree( pid->psi->prg[i]->iod );
             if( pid->psi->prg[i]->handle )
+            {
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+                if( dvbpsi_decoder_present( pid->psi->prg[i]->handle ) )
+                    dvbpsi_pmt_detach( pid->psi->prg[i]->handle );
+                dvbpsi_delete( pid->psi->prg[i]->handle );
+#else
                 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
+#endif
+            }
             free( pid->psi->prg[i] );
         }
         free( pid->psi->prg );
@@ -1399,6 +1570,7 @@ static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
  ****************************************************************************/
 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
 {
+    demux_sys_t *p_sys = p_demux->p_sys;
     uint8_t header[34];
     unsigned i_pes_size = 0;
     unsigned i_skip = 0;
@@ -1411,9 +1583,8 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
 
     if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
     {
-        if( !p_demux->p_sys->b_silent )
-            msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
-                      header[0], header[1],header[2],header[3], pid->i_pid );
+        msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
+                    header[0], header[1],header[2],header[3], pid->i_pid );
         block_ChainRelease( p_pes );
         return;
     }
@@ -1608,6 +1779,9 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
                          block_Duplicate( p_block ) );
         }
 
+        if (!p_sys->b_trust_pcr)
+            es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts);
+
         es_out_Send( p_demux->out, pid->es->id, p_block );
     }
     else
@@ -2026,9 +2200,10 @@ static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
             if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
             {
                 p_sys->pmt[i]->psi->prg[i_prg]->i_pcr_value = i_pcr;
-                es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
-                                (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
-                                (int64_t)(VLC_TS_0 + i_pcr * 100 / 9) );
+                if (p_sys->b_trust_pcr)
+                    es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
+                      (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
+                      (int64_t)(VLC_TS_0 + i_pcr * 100 / 9) );
             }
 }
 
@@ -2251,6 +2426,8 @@ static void PIDFillFormat( ts_es_t *es, int i_stream_type )
         break;
     case 0x11:  /* MPEG4 (audio) LATM */
     case 0x0f:  /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
+    case 0x1c:  /* ISO/IEC 14496-3 Audio, without using any additional
+                   transport syntax, such as DST, ALS and SLS */
         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
         break;
     case 0x10:  /* MPEG4 (video) */
@@ -2575,7 +2752,14 @@ static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
         ts_pid_t *p_pid = &p_sys->pid[i];
         if( p_pid->psi )
         {
+
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+            if( dvbpsi_decoder_present( p_pid->psi->handle ))
+                dvbpsi_DetachDemux( p_pid->psi->handle );
+            dvbpsi_delete( p_pid->psi->handle );
+#else
             dvbpsi_DetachDemux( p_pid->psi->handle );
+#endif
             free( p_pid->psi );
             p_pid->psi = NULL;
             p_pid->b_valid = false;
@@ -2621,7 +2805,12 @@ static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
 
     msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
              "network_id=%d",
-             p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+             p_sdt->i_extension,
+#else
+             p_sdt->i_ts_id,
+#endif
+             p_sdt->i_version, p_sdt->b_current_next,
              p_sdt->i_network_id );
 
     p_sys->b_broken_charset = false;
@@ -2822,7 +3011,12 @@ static void EITCallBack( demux_t *p_demux,
     msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
              "ts_id=%d network_id=%d segment_last_section_number=%d "
              "last_table_id=%d",
-             p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+             p_eit->i_extension,
+#else
+             p_eit->i_service_id,
+#endif
+             p_eit->i_version, p_eit->b_current_next,
              p_eit->i_ts_id, p_eit->i_network_id,
              p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
 
@@ -2956,7 +3150,13 @@ static void EITCallBack( demux_t *p_demux,
     {
         if( b_current_following &&
             (  p_sys->i_current_program == -1 ||
-               p_sys->i_current_program == p_eit->i_service_id ) )
+               p_sys->i_current_program ==
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+                    p_eit->i_extension
+#else
+                    p_eit->i_service_id
+#endif
+                ) )
         {
             p_sys->i_dvb_length = 0;
             p_sys->i_dvb_start = 0;
@@ -2967,7 +3167,13 @@ static void EITCallBack( demux_t *p_demux,
                 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
             }
         }
-        es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG, p_eit->i_service_id, p_epg );
+        es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+                        p_eit->i_extension,
+#else
+                        p_eit->i_service_id,
+#endif
+                        p_epg );
     }
     vlc_epg_Delete( p_epg );
 
@@ -2982,9 +3188,15 @@ static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
     EITCallBack( p_demux, p_eit, false );
 }
 
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
+                                 uint16_t i_extension, demux_t *p_demux )
+#else
 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
                                  uint8_t  i_table_id, uint16_t i_extension )
+#endif
 {
+    assert( h );
 #if 0
     msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
              i_table_id, i_table_id, i_extension, i_extension );
@@ -2993,9 +3205,13 @@ static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
     {
         msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
                  i_table_id, i_table_id, i_extension, i_extension );
-
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+        if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
+            msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
+#else
         dvbpsi_AttachSDT( h, i_table_id, i_extension,
                           (dvbpsi_sdt_callback)SDTCallBack, p_demux );
+#endif
     }
     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
              ( i_table_id == 0x4e || /* Current/Following */
@@ -3007,15 +3223,25 @@ static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
         dvbpsi_eit_callback cb = i_table_id == 0x4e ?
                                     (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
                                     (dvbpsi_eit_callback)EITCallBackSchedule;
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+        if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
+            msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
+#else
         dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
+#endif
     }
     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
               i_table_id == 0x70 )  /* TDT */
     {
          msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
                  i_table_id, i_table_id, i_extension, i_extension );
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+        if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
+            msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
+#else
          dvbpsi_AttachTOT( h, i_table_id, i_extension,
                            (dvbpsi_tot_callback)TDTCallBack, p_demux);
+#endif
     }
 }
 
@@ -3177,7 +3403,7 @@ static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
 
     /* Gather pages information */
 #if defined _DVBPSI_DR_56_H_ && \
-    defined DVBPSI_VERSION && DVBPSI_VERSION_INT > ((0<<16)+(1<<8)+5)
+    defined DVBPSI_VERSION && DVBPSI_VERSION_INT > DVBPSI_VERSION_WANTED(0,1,5)
     for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
     {
         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
@@ -3646,6 +3872,8 @@ static void PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
         {
             p_fmt->i_cat   = p_regs[i].i_cat;
             p_fmt->i_codec = p_regs[i].i_codec;
+            if (p_es->i_type == 0x87)
+                p_fmt->i_codec = VLC_CODEC_EAC3;
             return;
         }
     }
@@ -3713,7 +3941,7 @@ static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
                 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
             }
             int type = p_decoded->code[i].i_audio_type;
-            pid->es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
+            pid->es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
         }
     }
 #else
@@ -3869,7 +4097,7 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
         }
         else if( p_es->i_type == 0x06 )
         {
-            PMTSetupEs0x06(  p_demux, pid, p_es );
+            PMTSetupEs0x06( p_demux, pid, p_es );
         }
         else if( p_es->i_type == 0xEA )
         {
@@ -3929,23 +4157,19 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
                     !strcmp( pid->es->fmt.psz_language,
                              old_pid->es->fmt.psz_language ) ) ) )
             {
-                pid->es->id = old_pid->es->id;
-                old_pid->es->id = NULL;
+                pid->i_cc = old_pid->i_cc;
+                ts_es_t *e = pid->es;
+                pid->es = old_pid->es;
+                old_pid->es = e;
                 for( int i = 0; i < pid->i_extra_es; i++ )
                 {
-                    pid->extra_es[i]->id = old_pid->extra_es[i]->id;
-                    old_pid->extra_es[i]->id = NULL;
+                    e = pid->extra_es[i];
+                    pid->extra_es[i] = old_pid->extra_es[i];
+                    old_pid->extra_es[i] = e;
                 }
             }
             else
             {
-                if( old_pid )
-                {
-                    PIDClean( p_demux, old_pid );
-                    TAB_REMOVE( i_clean, pp_clean, old_pid );
-                    old_pid = 0;
-                }
-
                 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
                 for( int i = 0; i < pid->i_extra_es; i++ )
                 {
@@ -3978,8 +4202,8 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
 
     /* Set CAM descrambling */
     if( !ProgramIsSelected( p_demux, prg->i_number )
-     || stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
-                        ACCESS_SET_PRIVATE_ID_CA, p_pmt ) != VLC_SUCCESS )
+     || stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_CA,
+                        p_pmt ) != VLC_SUCCESS )
         dvbpsi_DeletePMT( p_pmt );
 
     for( int i = 0; i < i_clean; i++ )
@@ -4117,7 +4341,20 @@ static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
 
         PIDInit( pmt, true, pat->psi );
         ts_prg_psi_t *prg = pmt->psi->prg[pmt->psi->i_prg-1];
-        prg->handle = dvbpsi_AttachPMT(p_program->i_number, PMTCallBack, p_demux);
+#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
+        prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
+        if( !prg->handle )
+        {
+            dvbpsi_DeletePAT( p_pat );
+            return;
+        }
+        prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
+        if( !dvbpsi_pmt_attach( prg->handle, p_program->i_number, PMTCallBack, p_demux ) )
+            msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
+                     p_program->i_number );
+#else
+        prg->handle = dvbpsi_AttachPMT( p_program->i_number, PMTCallBack, p_demux );
+#endif
         prg->i_number = p_program->i_number;
         prg->i_pid_pmt = p_program->i_pid;