]> git.sesse.net Git - vlc/blobdiff - modules/demux/ts.c
mpeg: implement title/seekpoint controls (refs #8455)
[vlc] / modules / demux / ts.c
index 234a523b3f565c7d673332b4ff6b7a0743a96198..b958408c2e4f0765685fa127e2c0bf90f2ab87af 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * ts.c: Transport Stream input module for VLC.
  *****************************************************************************
- * Copyright (C) 2004-2005 the VideoLAN team
+ * Copyright (C) 2004-2005 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Jean-Paul Saman <jpsaman #_at_# m2x.nl>
  *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
 /* TDT support */
 # include <dvbpsi/tot.h>
 
+#include "../mux/mpeg/dvbpsi_compat.h"
+
 #undef TS_DEBUG
-static void ts_debug(const char *format, ...)
+VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
 {
 #ifdef TS_DEBUG
     va_list ap;
@@ -114,11 +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 CAPMT_SYSID_TEXT N_("CAPMT System ID")
-#define CAPMT_SYSID_LONGTEXT N_("Only forward descriptors from this SysID to the CAM.")
 
 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
@@ -135,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") )
@@ -143,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 )
@@ -156,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" )
@@ -211,7 +212,6 @@ typedef struct
 typedef struct
 {
     dvbpsi_handle   handle;
-
     int             i_version;
     int             i_number;
     int             i_pid_pcr;
@@ -235,17 +235,23 @@ typedef struct
 
 } ts_psi_t;
 
+typedef enum
+{
+    TS_ES_DATA_PES,
+    TS_ES_DATA_TABLE_SECTION
+} ts_es_data_type_t;
+
 typedef struct
 {
     es_format_t  fmt;
     es_out_id_t *id;
-    int         i_pes_size;
-    int         i_pes_gathered;
-    block_t     *p_pes;
+    ts_es_data_type_t data_type;
+    int         i_data_size;
+    int         i_data_gathered;
+    block_t     *p_data;
     block_t     **pp_last;
 
     es_mpeg4_descriptor_t *p_mpeg4desc;
-    int         b_gather;
 
 } ts_es_t;
 
@@ -305,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;
@@ -339,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 )
@@ -348,7 +360,7 @@ static inline int PIDGet( block_t *p )
     return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
 }
 
-static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
+static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
 
 static block_t* ReadTSPacket( demux_t *p_demux );
 static mtime_t GetPCR( block_t *p_pkt );
@@ -490,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
  *****************************************************************************/
@@ -533,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;
@@ -547,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 ) ||
@@ -579,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;
@@ -586,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 )
     {
@@ -675,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;
@@ -727,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 */
@@ -738,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
@@ -866,7 +988,7 @@ static int Demux( demux_t *p_demux )
             }
             else if( !p_sys->b_udp_out )
             {
-                b_frame = GatherPES( p_demux, p_pid, p_pkt );
+                b_frame = GatherData( p_demux, p_pid, p_pkt );
             }
             else
             {
@@ -897,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;
 }
 
@@ -1067,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;
@@ -1080,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;
     }
@@ -1119,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, '=' );
@@ -1211,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;
 }
@@ -1222,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 )
@@ -1328,7 +1486,8 @@ static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
             return;
 
         es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
-        pid->es->pp_last = &pid->es->p_pes;
+        pid->es->data_type = TS_ES_DATA_PES;
+        pid->es->pp_last = &pid->es->p_data;
     }
 }
 
@@ -1340,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 );
@@ -1360,8 +1536,8 @@ static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
             p_sys->i_pmt_es--;
         }
 
-        if( pid->es->p_pes )
-            block_ChainRelease( pid->es->p_pes );
+        if( pid->es->p_data )
+            block_ChainRelease( pid->es->p_data );
 
         es_format_Clean( &pid->es->fmt );
 
@@ -1375,8 +1551,8 @@ static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
                 p_sys->i_pmt_es--;
             }
 
-            if( pid->extra_es[i]->p_pes )
-                block_ChainRelease( pid->extra_es[i]->p_pes );
+            if( pid->extra_es[i]->p_data )
+                block_ChainRelease( pid->extra_es[i]->p_data );
 
             es_format_Clean( &pid->extra_es[i]->fmt );
 
@@ -1392,9 +1568,9 @@ static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
 /****************************************************************************
  * gathering stuff
  ****************************************************************************/
-static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
+static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
 {
-    block_t *p_pes = pid->es->p_pes;
+    demux_sys_t *p_sys = p_demux->p_sys;
     uint8_t header[34];
     unsigned i_pes_size = 0;
     unsigned i_skip = 0;
@@ -1402,20 +1578,13 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
     mtime_t i_pts = -1;
     mtime_t i_length = 0;
 
-    /* remove the pes from pid */
-    pid->es->p_pes = NULL;
-    pid->es->i_pes_size= 0;
-    pid->es->i_pes_gathered= 0;
-    pid->es->pp_last = &pid->es->p_pes;
-
     /* FIXME find real max size */
     /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
 
     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;
     }
@@ -1610,6 +1779,9 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
                          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
@@ -1618,6 +1790,78 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
     }
 }
 
+static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
+{
+    block_t *p_content = block_ChainGather( p_data );
+    mtime_t i_date = -1;
+    for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
+    {
+        if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
+        {
+            i_date = pid->p_owner->prg[i]->i_pcr_value;
+            if( i_date >= 0 )
+                break;
+        }
+    }
+    if( i_date >= 0 )
+    {
+        if( pid->es->fmt.i_codec == VLC_CODEC_SCTE_27 )
+        {
+            /* We need to extract the truncated pts stored inside the payload */
+            if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
+            {
+                int i_index = 0;
+                int i_offset = 4;
+                if( p_content->p_buffer[3] & 0x40 )
+                {
+                    i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
+                              p_content->p_buffer[8];
+                    i_offset = 9;
+                }
+                if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
+                {
+                    bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
+                    if( !is_immediate )
+                    {
+                        mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
+                        if( i_display_in < i_date )
+                            i_date = i_display_in + (1ll << 32);
+                        else
+                            i_date = i_display_in;
+                    }
+
+                }
+            }
+        }
+        p_content->i_dts =
+        p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
+    }
+    es_out_Send( p_demux->out, pid->es->id, p_content );
+}
+static void ParseData( demux_t *p_demux, ts_pid_t *pid )
+{
+    block_t *p_data = pid->es->p_data;
+
+    /* remove the pes from pid */
+    pid->es->p_data = NULL;
+    pid->es->i_data_size = 0;
+    pid->es->i_data_gathered = 0;
+    pid->es->pp_last = &pid->es->p_data;
+
+    if( pid->es->data_type == TS_ES_DATA_PES )
+    {
+        ParsePES( p_demux, pid, p_data );
+    }
+    else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
+    {
+        ParseTableSection( p_demux, pid, p_data );
+    }
+    else
+    {
+        block_ChainRelease( p_data );
+    }
+}
+
 static block_t* ReadTSPacket( demux_t *p_demux )
 {
     demux_sys_t *p_sys = p_demux->p_sys;
@@ -1944,31 +2188,26 @@ static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
         return;
 
     mtime_t i_pcr = GetPCR( p_bk );
-    if( i_pcr >= 0 )
-    {
-        if( p_sys->i_pid_ref_pcr == pid->i_pid )
-        {
-            p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
-        }
+    if( i_pcr < 0 )
+        return;
 
-        /* Search program and set the PCR */
-        for( int i = 0; i < p_sys->i_pmt; i++ )
-        {
-            for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
+    if( p_sys->i_pid_ref_pcr == pid->i_pid )
+        p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
+
+    /* Search program and set the PCR */
+    for( int i = 0; i < p_sys->i_pmt; i++ )
+        for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
+            if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
             {
-                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;
+                p_sys->pmt[i]->psi->prg[i_prg]->i_pcr_value = i_pcr;
+                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) );
-                }
+                      (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
+                      (int64_t)(VLC_TS_0 + i_pcr * 100 / 9) );
             }
-        }
-    }
 }
 
-static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
+static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
 {
     const uint8_t *p = p_bk->p_buffer;
     const bool b_unit_start = p[1]&0x40;
@@ -1996,8 +2235,8 @@ static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
     {
         msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
                  pid->i_pid );
-        if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
-            pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
+        if( pid->es->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
+            pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
     }
 
     if( p_demux->p_sys->csa )
@@ -2021,11 +2260,11 @@ static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
         {
             /* discontinuity indicator found in stream */
             b_discontinuity = (p[5]&0x80) ? true : false;
-            if( b_discontinuity && pid->es->p_pes )
+            if( b_discontinuity && pid->es->p_data )
             {
                 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
                             pid->i_pid );
-                /* pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
+                /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
             }
 #if 0
             if( p[5]&0x40 )
@@ -2060,11 +2299,11 @@ static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
                       i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
 
             pid->i_cc = i_cc;
-            if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
+            if( pid->es->p_data && pid->es->fmt.i_cat != VIDEO_ES )
             {
                 /* Small video artifacts are usually better than
                  * dropping full frames */
-                pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
+                pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
             }
         }
     }
@@ -2100,32 +2339,55 @@ static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
 
     if( b_unit_start )
     {
-        if( pid->es->p_pes )
+        if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
         {
-            ParsePES( p_demux, pid );
+            int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
+            block_t *p = block_Duplicate( p_bk );
+            if( p )
+            {
+                p->i_buffer = i_pointer_field;
+                p->p_buffer++;
+                block_ChainLastAppend( &pid->es->pp_last, p );
+            }
+            p_bk->i_buffer -= 1 + i_pointer_field;
+            p_bk->p_buffer += 1 + i_pointer_field;
+        }
+        if( pid->es->p_data )
+        {
+            ParseData( p_demux, pid );
             i_ret = true;
         }
 
         block_ChainLastAppend( &pid->es->pp_last, p_bk );
-        if( p_bk->i_buffer > 6 )
+        if( pid->es->data_type == TS_ES_DATA_PES )
         {
-            pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
-            if( pid->es->i_pes_size > 0 )
+            if( p_bk->i_buffer > 6 )
             {
-                pid->es->i_pes_size += 6;
+                pid->es->i_data_size = GetWBE( &p_bk->p_buffer[4] );
+                if( pid->es->i_data_size > 0 )
+                {
+                    pid->es->i_data_size += 6;
+                }
             }
         }
-        pid->es->i_pes_gathered += p_bk->i_buffer;
-        if( pid->es->i_pes_size > 0 &&
-            pid->es->i_pes_gathered >= pid->es->i_pes_size )
+        else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
         {
-            ParsePES( p_demux, pid );
+            if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
+            {
+                pid->es->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
+            }
+        }
+        pid->es->i_data_gathered += p_bk->i_buffer;
+        if( pid->es->i_data_size > 0 &&
+            pid->es->i_data_gathered >= pid->es->i_data_size )
+        {
+            ParseData( p_demux, pid );
             i_ret = true;
         }
     }
     else
     {
-        if( pid->es->p_pes == NULL )
+        if( pid->es->p_data == NULL )
         {
             /* msg_Dbg( p_demux, "broken packet" ); */
             block_Release( p_bk );
@@ -2133,11 +2395,12 @@ static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
         else
         {
             block_ChainLastAppend( &pid->es->pp_last, p_bk );
-            pid->es->i_pes_gathered += p_bk->i_buffer;
-            if( pid->es->i_pes_size > 0 &&
-                pid->es->i_pes_gathered >= pid->es->i_pes_size )
+            pid->es->i_data_gathered += p_bk->i_buffer;
+
+            if( pid->es->i_data_size > 0 &&
+                pid->es->i_data_gathered >= pid->es->i_data_size )
             {
-                ParsePES( p_demux, pid );
+                ParseData( p_demux, pid );
                 i_ret = true;
             }
         }
@@ -2163,11 +2426,12 @@ 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) */
         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
-        es->b_gather = true;
         break;
     case 0x1B:  /* H264 <- check transport syntax/needed descriptor */
         es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
@@ -2179,8 +2443,9 @@ static void PIDFillFormat( ts_es_t *es, int i_stream_type )
     case 0x81:  /* A52 (audio) */
         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
         break;
-    case 0x82:  /* DVD_SPU (sub) */
-        es_format_Init( fmt, SPU_ES, VLC_CODEC_SPU );
+    case 0x82:  /* SCTE-27 (sub) */
+        es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
+        es->data_type = TS_ES_DATA_TABLE_SECTION;
         break;
     case 0x83:  /* LPCM (audio) */
         es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
@@ -2208,7 +2473,6 @@ static void PIDFillFormat( ts_es_t *es, int i_stream_type )
 
     case 0xa0:  /* MSCODEC vlc (video) (fixed later) */
         es_format_Init( fmt, UNKNOWN_ES, 0 );
-        es->b_gather = true;
         break;
 
     case 0x06:  /* PES_PRIVATE  (fixed later) */
@@ -2488,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;
@@ -2504,6 +2775,13 @@ static char *EITConvertToUTF8( const unsigned char *psz_instring,
                                size_t i_length,
                                bool b_broken )
 {
+    /* Deal with no longer broken providers (no switch byte
+      but sending ISO_8859-1 instead of ISO_6937) without
+      removing them from the broken providers table
+      (keep the entry for correctly handling recorded TS).
+    */
+    b_broken = b_broken && i_length && *psz_instring > 0x20;
+
     if( b_broken )
         return FromCharset( "ISO_8859-1", psz_instring, i_length );
     return vlc_from_EIT( psz_instring, i_length );
@@ -2527,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;
@@ -2728,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 );
 
@@ -2741,6 +3029,7 @@ static void EITCallBack( demux_t *p_demux,
         char                *psz_extra = strdup("");
         int64_t i_start;
         int i_duration;
+        int i_min_age = 0;
 
         i_start = EITConvertStartTime( p_evt->i_start_time );
         i_duration = EITConvertDuration( p_evt->i_duration );
@@ -2819,6 +3108,24 @@ static void EITCallBack( demux_t *p_demux,
                     }
                 }
             }
+            else if( p_dr->i_tag == 0x55 )
+            {
+                dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
+                if ( pR )
+                {
+                    for ( int i = 0; i < pR->i_ratings_number; i++ )
+                    {
+                        const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
+                        if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
+                        {
+                            if ( p_rating->i_rating + 3 > i_min_age )
+                                i_min_age = p_rating->i_rating + 3;
+                            msg_Dbg( p_demux, "..* event parental control set to %d years",
+                                     i_min_age );
+                        }
+                    }
+                }
+            }
             else
             {
                 msg_Dbg( p_demux, "    - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
@@ -2828,7 +3135,7 @@ static void EITCallBack( demux_t *p_demux,
         /* */
         if( i_start > 0 )
             vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
-                              *psz_extra ? psz_extra : NULL );
+                              *psz_extra ? psz_extra : NULL, i_min_age );
 
         /* Update "now playing" field */
         if( p_evt->i_running_status == 0x04 && i_start > 0 )
@@ -2843,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;
@@ -2854,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 );
 
@@ -2869,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 );
@@ -2880,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 */
@@ -2894,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
     }
 }
 
@@ -3064,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 );
@@ -3095,7 +3434,6 @@ static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
     }
 #endif
 
-#ifdef _DVBPSI_DR_59_H_
     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
     if( p_dr )
     {
@@ -3127,7 +3465,6 @@ static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
             memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
         }
     }
-#endif
 
     /* */
     es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
@@ -3178,12 +3515,12 @@ static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
                 p_es->fmt.psz_description = NULL;
 
                 p_es->id      = NULL;
-                p_es->p_pes   = NULL;
-                p_es->i_pes_size = 0;
-                p_es->i_pes_gathered = 0;
-                p_es->pp_last = &p_es->p_pes;
+                p_es->p_data  = NULL;
+                p_es->i_data_size = 0;
+                p_es->i_data_gathered = 0;
+                p_es->pp_last = &p_es->p_data;
+                p_es->data_type = TS_ES_DATA_PES;
                 p_es->p_mpeg4desc = NULL;
-                p_es->b_gather = false;
 
                 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
             }
@@ -3213,7 +3550,6 @@ static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
 
     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
     int i_page = 0;
-#ifdef _DVBPSI_DR_59_H_
     dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
     for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
     {
@@ -3222,7 +3558,6 @@ static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
             ( i_type >= 0x20 && i_type <= 0x24 ) )
             i_page++;
     }
-#endif
 
     if( !p_demux->p_sys->b_split_es  || i_page <= 0 )
     {
@@ -3242,7 +3577,6 @@ static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
     }
     else
     {
-#ifdef _DVBPSI_DR_59_H_
         for( int i = 0; i < p_sub->i_subtitles_number; i++ )
         {
             ts_es_t *p_es;
@@ -3265,12 +3599,12 @@ static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
                 p_es->fmt.psz_description = NULL;
 
                 p_es->id      = NULL;
-                p_es->p_pes   = NULL;
-                p_es->i_pes_size = 0;
-                p_es->i_pes_gathered = 0;
-                p_es->pp_last = &p_es->p_pes;
+                p_es->p_data   = NULL;
+                p_es->i_data_size = 0;
+                p_es->i_data_gathered = 0;
+                p_es->pp_last = &p_es->p_data;
+                p_es->data_type = TS_ES_DATA_PES;
                 p_es->p_mpeg4desc = NULL;
-                p_es->b_gather = false;
 
                 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
             }
@@ -3302,7 +3636,6 @@ static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
             p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id <<  0 ) |
                                       ( p->i_ancillary_page_id   << 16 );
         }
-#endif
     }
 }
 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
@@ -3343,7 +3676,6 @@ static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
         /* Subtitle/Teletext/VBI fallbacks */
         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
 
-#ifdef _DVBPSI_DR_59_H_
         dvbpsi_subtitling_dr_t *p_sub;
         if( p_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_dr ) ) )
         {
@@ -3378,10 +3710,7 @@ static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
                 }
             }
         }
-#else
-        if( p_fmt->i_cat == UNKNOWN_ES && p_dr )
-            PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
-#endif
+
         if( p_fmt->i_cat == UNKNOWN_ES &&
             ( PMTEsFindDescriptor( p_es, 0x45 ) ||  /* VBI Data descriptor */
               PMTEsFindDescriptor( p_es, 0x46 ) ||  /* VBI Teletext descriptor */
@@ -3392,7 +3721,6 @@ static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
         }
     }
 
-#ifdef _DVBPSI_DR_52_H_
     /* FIXME is it useful ? */
     if( PMTEsFindDescriptor( p_es, 0x52 ) )
     {
@@ -3401,7 +3729,6 @@ static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
 
         msg_Dbg( p_demux, "    * Stream Component Identifier: %d", p_si->i_component_tag );
     }
-#endif
 }
 
 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
@@ -3545,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;
         }
     }
@@ -3562,7 +3891,7 @@ static char *GetAudioTypeDesc(demux_t *p_demux, int type)
     if (type < 0 || type > 3)
         msg_Dbg( p_demux, "unknown audio type: %d", type);
     else if (type > 0)
-        return strdup(audio_type[type - 1]);
+        return strdup(audio_type[type]);
 
     return NULL;
 }
@@ -3612,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
@@ -3768,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 )
         {
@@ -3828,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++ )
                 {
@@ -3877,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++ )
@@ -3970,16 +4295,17 @@ static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
         /* Delete PMT pid */
         for( int i = 0; i < i_pmt_rm; i++ )
         {
-            SetPIDFilter( p_demux, pmt_rm[i]->i_pid, false );
+            ts_pid_t *pid = pmt_rm[i];
+            SetPIDFilter( p_demux, pid->i_pid, false );
 
-            for( int i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
+            for( int i_prg = 0; i_prg < pid->psi->i_prg; i_prg++ )
             {
-                const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
+                const int i_number = pid->psi->prg[i_prg]->i_number;
                 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
             }
 
-            PIDClean( p_demux, &p_sys->pid[pmt_rm[i]->i_pid] );
-            TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
+            PIDClean( p_demux, &p_sys->pid[pid->i_pid] );
+            TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
         }
 
         free( pmt_rm );
@@ -4015,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;