]> git.sesse.net Git - vlc/blobdiff - modules/demux/ts.c
demux: mp4: add restriction for frma atom
[vlc] / modules / demux / ts.c
index 9125c85b242db5ad088a4dcbc221beb08c1b2601..10c08faa38747dd463529838e31fb11faeb95c13 100644 (file)
 #include <vlc_plugin.h>
 
 #include <assert.h>
+#include <time.h>
 
 #include <vlc_access.h>    /* DVB-specific things */
 #include <vlc_demux.h>
 #include <vlc_meta.h>
 #include <vlc_epg.h>
 #include <vlc_charset.h>   /* FromCharset, for EIT */
+#include <vlc_bits.h>
 
 #include "../mux/mpeg/csa.h"
 
 
 #include "../mux/mpeg/dvbpsi_compat.h"
 
+#include "../codec/opus_header.h"
+
+#include "opus.h"
+
 #undef TS_DEBUG
 VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
 {
@@ -74,6 +80,18 @@ VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
 #endif
 }
 
+#ifdef HAVE_ARIBB24
+ #include <aribb24/aribb24.h>
+ #include <aribb24/decoder.h>
+#endif
+
+typedef enum arib_modes_e
+{
+    ARIBMODE_AUTO = -1,
+    ARIBMODE_DISABLED = 0,
+    ARIBMODE_ENABLED = 1
+} arib_modes_e;
+
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
@@ -99,13 +117,6 @@ static void Close ( vlc_object_t * );
                        " the TS stream, instead of 1, 2, 3, etc. Useful to" \
                        " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
 
-#define TSOUT_TEXT N_("Fast udp streaming")
-#define TSOUT_LONGTEXT N_( \
-  "Sends TS to specific ip:port by udp (you must know what you are doing).")
-
-#define MTUOUT_TEXT N_("MTU for out mode")
-#define MTUOUT_LONGTEXT N_("MTU for out mode.")
-
 #define CSA_TEXT N_("CSA Key")
 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
   "16 char string (8 hexadecimal bytes).")
@@ -133,6 +144,16 @@ static void Close ( vlc_object_t * );
 #define PCR_TEXT N_("Trust in-stream PCR")
 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
 
+static const int const arib_mode_list[] =
+  { ARIBMODE_AUTO, ARIBMODE_ENABLED, ARIBMODE_DISABLED };
+static const char *const arib_mode_list_text[] =
+  { N_("Auto"), N_("Enabled"), N_("Disabled") };
+
+#define SUPPORT_ARIB_TEXT N_("ARIB STD-B24 mode")
+#define SUPPORT_ARIB_LONGTEXT N_( \
+    "Forces ARIB STD-B24 mode for decoding characters." \
+    "This feature affects EPG information and subtitles." )
+
 vlc_module_begin ()
     set_description( N_("MPEG Transport Stream demuxer") )
     set_shortname ( "MPEG-TS" )
@@ -156,6 +177,9 @@ vlc_module_begin ()
     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_integer( "ts-arib", ARIBMODE_AUTO, SUPPORT_ARIB_TEXT, SUPPORT_ARIB_LONGTEXT, false )
+        change_integer_list( arib_mode_list, arib_mode_list_text )
+
     add_obsolete_bool( "ts-silent" );
 
     set_capability( "demux", 10 )
@@ -240,9 +264,9 @@ typedef enum
 
 typedef enum
 {
-    TS_REGISTRATION_OTHER = 0,
-    TS_REGISTRATION_HDMV
-} ts_registration_type_t;
+    TS_PMT_REGISTRATION_NONE = 0,
+    TS_PMT_REGISTRATION_HDMV
+} ts_pmt_registration_type_t;
 
 typedef struct
 {
@@ -304,6 +328,14 @@ struct demux_sys_t
     mtime_t     *p_pcrs;
     int64_t     *p_pos;
 
+    struct
+    {
+        arib_modes_e e_mode;
+#ifdef HAVE_ARIBB24
+        arib_instance_t *p_instance;
+#endif
+    } arib;
+
     /* All pid */
     ts_pid_t    pid[8192];
 
@@ -384,7 +416,6 @@ static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
 #define TS_PACKET_SIZE_192 192
 #define TS_PACKET_SIZE_204 204
 #define TS_PACKET_SIZE_MAX 204
-#define TS_TOPFIELD_HEADER 1320
 
 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size )
 {
@@ -767,6 +798,8 @@ static int Open( vlc_object_t *p_this )
     p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
     p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
 
+    p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
+
     if( !p_sys->p_pcrs || !p_sys->p_pos )
     {
         Close( p_this );
@@ -875,6 +908,11 @@ static void Close( vlc_object_t *p_this )
     free( p_sys->p_pcrs );
     free( p_sys->p_pos );
 
+#ifdef HAVE_ARIBB24
+    if ( p_sys->arib.p_instance )
+        arib_instance_destroy( p_sys->arib.p_instance );
+#endif
+
     vlc_mutex_destroy( &p_sys->csa_lock );
     free( p_sys );
 }
@@ -1518,6 +1556,107 @@ static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
     pid->b_valid = false;
 }
 
+static int16_t read_opus_flag(uint8_t **buf, size_t *len)
+{
+    if (*len < 2)
+        return -1;
+
+    int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
+
+    *len -= 2;
+    *buf += 2;
+
+    if (ret & (3<<13))
+        ret = -1;
+
+    return ret;
+}
+
+static block_t *Opus_Parse(demux_t *demux, block_t *block)
+{
+    block_t *out = NULL;
+    block_t **last = NULL;
+
+    uint8_t *buf = block->p_buffer;
+    size_t len = block->i_buffer;
+
+    while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
+        int16_t start_trim = 0, end_trim = 0;
+        int start_trim_flag        = (buf[1] >> 4) & 1;
+        int end_trim_flag          = (buf[1] >> 3) & 1;
+        int control_extension_flag = (buf[1] >> 2) & 1;
+
+        len -= 2;
+        buf += 2;
+
+        unsigned au_size = 0;
+        while (len--) {
+            int c = *buf++;
+            au_size += c;
+            if (c != 0xff)
+                break;
+        }
+
+        if (start_trim_flag) {
+            start_trim = read_opus_flag(&buf, &len);
+            if (start_trim < 0) {
+                msg_Err(demux, "Invalid start trimming flag");
+            }
+        }
+        if (end_trim_flag) {
+            end_trim = read_opus_flag(&buf, &len);
+            if (end_trim < 0) {
+                msg_Err(demux, "Invalid end trimming flag");
+            }
+        }
+        if (control_extension_flag && len) {
+            unsigned l = *buf++; len--;
+            if (l > len) {
+                msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
+                break;
+            }
+            buf += l;
+            len -= l;
+        }
+
+        if (!au_size || au_size > len) {
+            msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
+            break;
+        }
+
+        block_t *au = block_Alloc(au_size);
+        if (!au)
+            break;
+        memcpy(au->p_buffer, buf, au_size);
+        block_CopyProperties(au, block);
+        au->p_next = NULL;
+
+        if (!out)
+            out = au;
+        else
+            *last = au;
+        last = &au->p_next;
+
+        au->i_nb_samples = opus_frame_duration(buf, au_size);
+        if (end_trim && end_trim <= au->i_nb_samples)
+            au->i_length = end_trim; /* Blatant abuse of the i_length field. */
+        else
+            au->i_length = 0;
+
+        if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
+            au->i_nb_samples -= start_trim;
+            if (au->i_nb_samples == 0)
+                au->i_flags |= BLOCK_FLAG_PREROLL;
+        }
+
+        buf += au_size;
+        len -= au_size;
+    }
+
+    block_Release(block);
+    return out;
+}
+
 /****************************************************************************
  * gathering stuff
  ****************************************************************************/
@@ -1534,10 +1673,11 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *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( pid->b_scrambled || header[0] != 0 || header[1] != 0 || header[2] != 1 )
     {
-        msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
-                    header[0], header[1],header[2],header[3], pid->i_pid );
+        if ( !pid->b_scrambled )
+            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;
     }
@@ -1704,7 +1844,7 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
             /* Append a \0 */
             p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
             if( !p_block )
-                abort();
+                return;
             p_block->p_buffer[p_block->i_buffer -1] = '\0';
         }
         else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
@@ -1725,17 +1865,44 @@ static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
                 }
             }
         }
-
-        for( int i = 0; i < pid->i_extra_es; i++ )
+        else if( pid->es->fmt.i_codec == VLC_CODEC_ARIB_A ||
+                 pid->es->fmt.i_codec == VLC_CODEC_ARIB_C )
         {
-            es_out_Send( p_demux->out, pid->extra_es[i]->id,
-                         block_Duplicate( p_block ) );
+            if( p_block->i_pts <= VLC_TS_INVALID )
+            {
+                if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
+                {
+                    p_block->i_buffer = i_pes_size;
+                }
+                /* Append a \0 */
+                p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
+                if( !p_block )
+                    return;
+                p_block->p_buffer[p_block->i_buffer -1] = '\0';
+            }
         }
+        else if( pid->es->fmt.i_codec == VLC_CODEC_OPUS)
+        {
+            p_block = Opus_Parse(p_demux, p_block);
+        }
+
+        while (p_block) {
+            block_t *p_next = p_block->p_next;
+            p_block->p_next = NULL;
+            for( int i = 0; i < pid->i_extra_es; i++ )
+            {
+                es_out_Send( p_demux->out, pid->extra_es[i]->id,
+                        block_Duplicate( p_block ) );
+            }
+
+            if (!p_sys->b_trust_pcr)
+                es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
+                        pid->i_owner_number, p_block->i_pts);
 
-        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 );
 
-        es_out_Send( p_demux->out, pid->es->id, p_block );
+            p_block = p_next;
+        }
     }
     else
     {
@@ -2759,10 +2926,41 @@ static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
 
 #include "dvb-text.h"
 
-static char *EITConvertToUTF8( const unsigned char *psz_instring,
+static char *EITConvertToUTF8( demux_t *p_demux,
+                               const unsigned char *psz_instring,
                                size_t i_length,
                                bool b_broken )
 {
+    demux_sys_t *p_sys = p_demux->p_sys;
+#ifdef HAVE_ARIBB24
+    if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
+    {
+        if ( !p_sys->arib.p_instance )
+            p_sys->arib.p_instance = arib_instance_new( p_demux );
+        if ( !p_sys->arib.p_instance )
+            return NULL;
+        arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
+        if ( !p_decoder )
+            return NULL;
+
+        char *psz_outstring = NULL;
+        size_t i_out;
+
+        i_out = i_length * 4;
+        psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
+        if( !psz_outstring )
+            return NULL;
+
+        arib_initialize_decoder( p_decoder );
+        i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
+                                    psz_outstring, i_out );
+        arib_finalize_decoder( p_decoder );
+
+        return psz_outstring;
+    }
+#else
+    VLC_UNUSED(p_sys);
+#endif
     /* 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
@@ -2868,10 +3066,12 @@ static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
 
                 /* FIXME: Digital+ ES also uses ISO8859-1 */
 
-                str1 = EITConvertToUTF8(pD->i_service_provider_name,
+                str1 = EITConvertToUTF8(p_demux,
+                                        pD->i_service_provider_name,
                                         pD->i_service_provider_name_length,
                                         p_sys->b_broken_charset );
-                str2 = EITConvertToUTF8(pD->i_service_name,
+                str2 = EITConvertToUTF8(p_demux,
+                                        pD->i_service_name,
                                         pD->i_service_name_length,
                                         p_sys->b_broken_charset );
 
@@ -3018,10 +3218,31 @@ static void EITCallBack( demux_t *p_demux,
         int64_t i_start;
         int i_duration;
         int i_min_age = 0;
+        int64_t i_tot_time = 0;
 
         i_start = EITConvertStartTime( p_evt->i_start_time );
         i_duration = EITConvertDuration( p_evt->i_duration );
 
+        if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
+        {
+            if( p_sys->i_tdt_delta == 0 )
+                p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
+
+            i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
+
+            tzset(); // JST -> UTC
+            i_start += timezone; // FIXME: what about DST?
+            i_tot_time += timezone;
+
+            if( p_evt->i_running_status == 0x00 &&
+                (i_start - 5 < i_tot_time &&
+                 i_tot_time < i_start + i_duration + 5) )
+            {
+                p_evt->i_running_status = 0x04;
+                msg_Dbg( p_demux, "  EIT running status 0x00 -> 0x04" );
+            }
+        }
+
         msg_Dbg( p_demux, "  * event id=%d start_time:%d duration=%d "
                           "running=%d free_ca=%d",
                  p_evt->i_event_id, (int)i_start, (int)i_duration,
@@ -3029,23 +3250,30 @@ static void EITCallBack( demux_t *p_demux,
 
         for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
         {
-            if( p_dr->i_tag == 0x4d )
+            switch(p_dr->i_tag)
+            {
+            case 0x4d:
             {
                 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
 
                 /* Only take first description, as we don't handle language-info
                    for epg atm*/
-                if( pE && psz_name == NULL)
+                if( pE && psz_name == NULL )
                 {
-                    psz_name = EITConvertToUTF8( pE->i_event_name, pE->i_event_name_length,
+                    psz_name = EITConvertToUTF8( p_demux,
+                                                 pE->i_event_name, pE->i_event_name_length,
                                                  p_sys->b_broken_charset );
-                    psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
+                    free( psz_text );
+                    psz_text = EITConvertToUTF8( p_demux,
+                                                 pE->i_text, pE->i_text_length,
                                                  p_sys->b_broken_charset );
                     msg_Dbg( p_demux, "    - short event lang=%3.3s '%s' : '%s'",
                              pE->i_iso_639_code, psz_name, psz_text );
                 }
             }
-            else if( p_dr->i_tag == 0x4e )
+                break;
+
+            case 0x4e:
             {
                 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
                 if( pE )
@@ -3056,7 +3284,8 @@ static void EITCallBack( demux_t *p_demux,
 
                     if( pE->i_text_length > 0 )
                     {
-                        char *psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
+                        char *psz_text = EITConvertToUTF8( p_demux,
+                                                           pE->i_text, pE->i_text_length,
                                                            p_sys->b_broken_charset );
                         if( psz_text )
                         {
@@ -3071,10 +3300,12 @@ static void EITCallBack( demux_t *p_demux,
 
                     for( int i = 0; i < pE->i_entry_count; i++ )
                     {
-                        char *psz_dsc = EITConvertToUTF8( pE->i_item_description[i],
+                        char *psz_dsc = EITConvertToUTF8( p_demux,
+                                                          pE->i_item_description[i],
                                                           pE->i_item_description_length[i],
                                                           p_sys->b_broken_charset );
-                        char *psz_itm = EITConvertToUTF8( pE->i_item[i], pE->i_item_length[i],
+                        char *psz_itm = EITConvertToUTF8( p_demux,
+                                                          pE->i_item[i], pE->i_item_length[i],
                                                           p_sys->b_broken_charset );
 
                         if( psz_dsc && psz_itm )
@@ -3096,7 +3327,9 @@ static void EITCallBack( demux_t *p_demux,
                     }
                 }
             }
-            else if( p_dr->i_tag == 0x55 )
+                break;
+
+            case 0x55:
             {
                 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
                 if ( pR )
@@ -3108,25 +3341,27 @@ static void EITCallBack( demux_t *p_demux,
                         {
                             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",
+                            msg_Dbg( p_demux, "    - 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 );
+                break;
+
+            default:
+                msg_Dbg( p_demux, "    - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
+                break;
             }
         }
 
         /* */
-        if( i_start > 0 )
+        if( i_start > 0 && psz_name && psz_text)
             vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
                               *psz_extra ? psz_extra : NULL, i_min_age );
 
         /* Update "now playing" field */
-        if( p_evt->i_running_status == 0x04 && i_start > 0 )
+        if( p_evt->i_running_status == 0x04 && i_start > 0  && psz_name && psz_text )
             vlc_epg_SetCurrent( p_epg, i_start );
 
         free( psz_name );
@@ -3219,7 +3454,7 @@ static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
 #endif
     }
     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
-              i_table_id == 0x70 )  /* TDT */
+            (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
     {
          msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
                  i_table_id, i_table_id, i_extension, i_extension );
@@ -3261,6 +3496,20 @@ static bool PMTEsHasRegistration( demux_t *p_demux,
     assert( strlen(psz_tag) == 4 );
     return !memcmp( p_dr->p_data, psz_tag, 4 );
 }
+
+static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
+                                  int i_component_tag )
+{
+    dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
+    if( !p_dr )
+        return false;
+    dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
+    if( !p_si )
+        return false;
+
+    return p_si->i_component_tag == i_component_tag;
+}
+
 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
                                 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
 {
@@ -3292,7 +3541,19 @@ static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
     }
     if( !pid->es->p_mpeg4desc )
     {
-        msg_Err( p_demux, "MPEG-4 descriptor not found" );
+        switch( p_es->i_type )
+        {
+        /* non fatal, set by packetizer */
+        case 0x0f: /* ADTS */
+        case 0x11: /* LOAS */
+            msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
+                      pid->i_pid, p_es->i_type );
+            break;
+        default:
+            msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
+                     pid->i_pid, p_es->i_type );
+            break;
+        }
         return;
     }
 
@@ -3629,10 +3890,131 @@ static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
         }
     }
 }
+
+static int vlc_ceil_log2( const unsigned int val )
+{
+    int n = 31 - clz(val);
+    if ((1U << n) != val)
+        n++;
+
+    return n;
+}
+
+static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
+{
+    OpusHeader h;
+
+    /* default mapping */
+    static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
+    memcpy(h.stream_map, map, sizeof(map));
+
+    int csc, mapping;
+    int channels = 0;
+    int stream_count = 0;
+    int ccc = p[1]; // channel_config_code
+    if (ccc <= 8) {
+        channels = ccc;
+        if (channels)
+            mapping = channels > 2;
+        else {
+            mapping = 255;
+            channels = 2; // dual mono
+        }
+        static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
+        csc = p_csc[channels - 1];
+        stream_count = channels - csc;
+
+        static const uint8_t map[6][7] = {
+            { 2,1 },
+            { 1,2,3 },
+            { 4,1,2,3 },
+            { 4,1,2,3,5 },
+            { 4,1,2,3,5,6 },
+            { 6,1,2,3,4,5,7 },
+        };
+        if (channels > 2)
+            memcpy(&h.stream_map[1], map[channels-3], channels - 1);
+    } else if (ccc == 0x81) {
+        if (len < 4)
+            goto explicit_config_too_short;
+
+        channels = p[2];
+        mapping = p[3];
+        csc = 0;
+        if (mapping) {
+            bs_t s;
+            bs_init(&s, &p[4], len - 4);
+            stream_count = 1;
+            if (channels) {
+                int bits = vlc_ceil_log2(channels);
+                if (s.i_left < bits)
+                    goto explicit_config_too_short;
+                stream_count = bs_read(&s, bits) + 1;
+                bits = vlc_ceil_log2(stream_count + 1);
+                if (s.i_left < bits)
+                    goto explicit_config_too_short;
+                csc = bs_read(&s, bits);
+            }
+            int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
+            if (s.i_left < channels * channel_bits)
+                goto explicit_config_too_short;
+
+            unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
+            for (int i = 0; i < channels; i++) {
+                unsigned char m = bs_read(&s, channel_bits);
+                if (m == silence)
+                    m = 0xff;
+                h.stream_map[i] = m;
+            }
+        }
+    } else if (ccc >= 0x80 && ccc <= 0x88) {
+        channels = ccc - 0x80;
+        if (channels)
+            mapping = 1;
+        else {
+            mapping = 255;
+            channels = 2; // dual mono
+        }
+        csc = 0;
+        stream_count = channels;
+    } else {
+        msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
+    }
+
+    if (!channels) {
+        msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
+        return;
+    }
+
+    opus_prepare_header(channels, 0, &h);
+    h.preskip = 0;
+    h.input_sample_rate = 48000;
+    h.nb_coupled = csc;
+    h.nb_streams = channels - csc;
+    h.channel_mapping = mapping;
+
+    if (h.channels) {
+        opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
+        if (p_fmt->p_extra) {
+            p_fmt->i_cat = AUDIO_ES;
+            p_fmt->i_codec = VLC_CODEC_OPUS;
+            p_fmt->audio.i_channels = h.channels;
+            p_fmt->audio.i_rate = 48000;
+        }
+    }
+
+    return;
+
+explicit_config_too_short:
+    msg_Err(demux, "Opus descriptor too short");
+}
+
 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
                             const dvbpsi_pmt_es_t *p_es )
 {
     es_format_t *p_fmt = &pid->es->fmt;
+    dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_es, 0x59 );
+    dvbpsi_descriptor_t *desc;
 
     if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
         PMTEsFindDescriptor( p_es, 0x6a ) ||
@@ -3641,6 +4023,11 @@ static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
         p_fmt->i_cat = AUDIO_ES;
         p_fmt->i_codec = VLC_CODEC_A52;
     }
+    else if( (desc = PMTEsFindDescriptor( p_es, 0x7f ) ) && desc->i_length >= 2 &&
+              desc->p_data[0] == 0x80)
+    {
+        OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
+    }
     else if( PMTEsFindDescriptor( p_es, 0x7a ) )
     {
         /* DVB with stream_type 0x06 (ETS EN 300 468) */
@@ -3656,8 +4043,10 @@ static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
         p_fmt->i_cat = AUDIO_ES;
         p_fmt->i_codec = VLC_CODEC_DTS;
     }
-    else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) )
+    else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) && !p_subs_dr )
     {
+        /* BSSD is AES3 DATA, but could also be subtitles
+         * we need to check for secondary descriptor then s*/
         p_fmt->i_cat = AUDIO_ES;
         p_fmt->b_packetized = true;
         p_fmt->i_codec = VLC_CODEC_302M;
@@ -3667,13 +4056,42 @@ static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
         p_fmt->i_cat = VIDEO_ES;
         p_fmt->i_codec = VLC_CODEC_HEVC;
     }
+    else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
+    {
+        /* Lookup our data component descriptor first ARIB STD B10 6.4 */
+        dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xFD );
+        /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
+        if ( p_dr && p_dr->i_length >= 2 )
+        {
+            if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) &&  (
+                    PMTEsHasComponentTag( p_es, 0x30 ) ||
+                    PMTEsHasComponentTag( p_es, 0x31 ) ||
+                    PMTEsHasComponentTag( p_es, 0x32 ) ||
+                    PMTEsHasComponentTag( p_es, 0x33 ) ||
+                    PMTEsHasComponentTag( p_es, 0x34 ) ||
+                    PMTEsHasComponentTag( p_es, 0x35 ) ||
+                    PMTEsHasComponentTag( p_es, 0x36 ) ||
+                    PMTEsHasComponentTag( p_es, 0x37 ) ) )
+            {
+                es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_A );
+                p_fmt->psz_language = strndup ( "jpn", 3 );
+                p_fmt->psz_description = strdup( _("ARIB subtitles") );
+            }
+            else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
+                     PMTEsHasComponentTag( p_es, 0x87 ) ||
+                     PMTEsHasComponentTag( p_es, 0x88 ) ) )
+            {
+                es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_C );
+                p_fmt->psz_language = strndup ( "jpn", 3 );
+                p_fmt->psz_description = strdup( _("ARIB subtitles") );
+            }
+        }
+    }
     else
     {
         /* Subtitle/Teletext/VBI fallbacks */
-        dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
-
         dvbpsi_subtitling_dr_t *p_sub;
-        if( p_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_dr ) ) )
+        if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
         {
             for( int i = 0; i < p_sub->i_subtitles_number; i++ )
             {
@@ -3817,7 +4235,8 @@ static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pid_t *pid )
         es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
 }
 
-static void PMTSetupEsHDMV( ts_pid_t *pid, const dvbpsi_pmt_es_t *p_es )
+static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
+                            const dvbpsi_pmt_es_t *p_es )
 {
     es_format_t *p_fmt = &pid->es->fmt;
 
@@ -3852,12 +4271,17 @@ static void PMTSetupEsHDMV( ts_pid_t *pid, const dvbpsi_pmt_es_t *p_es )
         break;
     case 0x91: /* Interactive graphics */
     case 0x92: /* Subtitle */
+        return false;
     default:
+        msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
+                  p_es->i_pid, p_es->i_type );
+        return false;
         break;
     }
+    return true;
 }
 
-static void PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
+static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
                                     const dvbpsi_pmt_es_t *p_es )
 {
     static const struct
@@ -3885,9 +4309,10 @@ static void PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
             p_fmt->i_codec = p_regs[i].i_codec;
             if (p_es->i_type == 0x87)
                 p_fmt->i_codec = VLC_CODEC_EAC3;
-            return;
+            return true;
         }
     }
+    return false;
 }
 
 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
@@ -4032,44 +4457,83 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
     if( ProgramIsSelected( p_demux, prg->i_number ) )
         SetPIDFilter( p_demux, prg->i_pid_pcr, true ); /* Set demux filter */
 
-    /* Parse descriptor */
-    ts_registration_type_t registration_type = TS_REGISTRATION_OTHER;
+    /* Parse PMT descriptors */
+    ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
     dvbpsi_descriptor_t  *p_dr;
+
+    /* First pass for standard detection */
+    if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
+    {
+        int i_arib_flags = 0; /* Descriptors can be repeated */
+        for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
+        {
+            switch(p_dr->i_tag)
+            {
+            case 0x09:
+            {
+                dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
+                i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
+            }
+                break;
+            case 0xF6:
+                i_arib_flags |= 1 << 1;
+                break;
+            case 0xC1:
+                i_arib_flags |= 1 << 2;
+                break;
+            default:
+                break;
+            }
+        }
+        if ( i_arib_flags == 0b111 )
+            p_sys->arib.e_mode = ARIBMODE_ENABLED;
+    }
+
     for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
+    {
+        /* special descriptors handling */
         switch(p_dr->i_tag)
         {
         case 0x1d: /* We have found an IOD descriptor */
-            msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
+            msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
             prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
             break;
 
         case 0x9:
-            msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
+            msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
                     (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
             break;
 
         case 0x5: /* Registration Descriptor */
             if( p_dr->i_length != 4 )
             {
-                msg_Warn( p_demux, "invalid Registration Descriptor" );
+                msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
             }
             else
             {
-                msg_Dbg( p_demux, " * descriptor : registration %4.4s", p_dr->p_data );
+                msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
                 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
-                    registration_type = TS_REGISTRATION_HDMV; /* Blu-Ray */
+                    registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
             }
             break;
 
+        case 0x0f:
+            msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
+            break;
+
+        case 0xC1:
+            msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
+            break;
+
         case 0x88: /* EACEM Simulcast HD Logical channels ordering */
             msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
             /* TODO: apply visibility flags */
             break;
 
         default:
-            msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
+            msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
         }
-
+    }
     dvbpsi_pmt_es_t      *p_es;
     for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
     {
@@ -4088,16 +4552,74 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
 
         if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
         {
-            msg_Warn( p_demux, "pmt error: pid=%d already defined",
+            msg_Warn( p_demux, " * PMT error: pid=%d already defined",
                       p_es->i_pid );
             continue;
         }
 
+        char const * psz_typedesc = "";
+        switch(p_es->i_type)
+        {
+        case 0x00:
+            psz_typedesc = "ISO/IEC Reserved";
+            break;
+        case 0x01:
+            psz_typedesc = "ISO/IEC 11172 Video";
+            break;
+        case 0x02:
+            psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
+            break;
+        case 0x03:
+            psz_typedesc = "ISO/IEC 11172 Audio";
+            break;
+        case 0x04:
+            psz_typedesc = "ISO/IEC 13818-3 Audio";
+            break;
+        case 0x05:
+            psz_typedesc = "ISO/IEC 13818-1 private_sections";
+            break;
+        case 0x06:
+            psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
+            break;
+        case 0x07:
+            psz_typedesc = "ISO/IEC 13522 MHEG";
+            break;
+        case 0x08:
+            psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
+            break;
+        case 0x09:
+            psz_typedesc = "ITU-T Rec. H.222.1";
+            break;
+        case 0x0A:
+            psz_typedesc = "ISO/IEC 13818-6 type A";
+            break;
+        case 0x0B:
+            psz_typedesc = "ISO/IEC 13818-6 type B";
+            break;
+        case 0x0C:
+            psz_typedesc = "ISO/IEC 13818-6 type C";
+            break;
+        case 0x0D:
+            psz_typedesc = "ISO/IEC 13818-6 type D";
+            break;
+        case 0x0E:
+            psz_typedesc = "ISO/IEC 13818-1 auxiliary";
+            break;
+        default:
+            if (p_es->i_type >= 0x0F && p_es->i_type <=0x7F)
+                psz_typedesc = "ISO/IEC 13818-1 Reserved";
+            else
+                psz_typedesc = "User Private";
+        }
+
+        msg_Dbg( p_demux, "  * pid=%d type=0x%x %s",
+                 p_es->i_pid, p_es->i_type, psz_typedesc );
+
         for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
              p_dr = p_dr->p_next )
         {
-            msg_Dbg( p_demux, "  * es pid=%d type=%d dr->i_tag=0x%x",
-                     p_es->i_pid, p_es->i_type, p_dr->i_tag );
+            msg_Dbg( p_demux, "    - descriptor tag 0x%x",
+                     p_dr->i_tag );
         }
 
         PIDInit( pid, false, pmt->psi );
@@ -4106,36 +4628,54 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
         pid->i_pid          = p_es->i_pid;
         pid->b_seen         = p_sys->pid[p_es->i_pid].b_seen;
 
-        switch( p_es->i_type )
+
+        bool b_registration_applied = false;
+        if ( p_es->i_type >= 0x80 ) /* non standard, extensions */
         {
-        case 0x10:
-        case 0x11:
-        case 0x12:
-        case 0x0f:
-            PMTSetupEsISO14496( p_demux, pid, prg, p_es );
-            break;
-        case 0x06:
-            PMTSetupEs0x06( p_demux, pid, p_es );
-            break;
-        case 0x83:
-            /* LPCM (audio) */
-            PMTSetupEs0x83( p_pmt, pid );
-            break;
-        case 0xa0:
-            PMTSetupEs0xA0( p_demux, pid, p_es );
-            break;
-        case 0xd1:
-            PMTSetupEs0xD1( p_demux, pid, p_es );
-            break;
-        case 0xEA:
-            PMTSetupEs0xEA( p_demux, pid, p_es );
-            break;
-        default:
-            if( registration_type == TS_REGISTRATION_HDMV )
-                PMTSetupEsHDMV( pid, p_es );
-            else if( p_es->i_type >= 0x80 )
-                PMTSetupEsRegistration( p_demux, pid, p_es );
-            break;
+            if ( registration_type == TS_PMT_REGISTRATION_HDMV )
+            {
+                if (( b_registration_applied = PMTSetupEsHDMV( p_demux, pid, p_es ) ))
+                    msg_Dbg( p_demux, "    + HDMV registration applied to pid %d type 0x%x",
+                             p_es->i_pid, p_es->i_type );
+            }
+            else
+            {
+                if (( b_registration_applied = PMTSetupEsRegistration( p_demux, pid, p_es ) ))
+                    msg_Dbg( p_demux, "    + registration applied to pid %d type 0x%x",
+                        p_es->i_pid, p_es->i_type );
+            }
+        }
+
+        if ( !b_registration_applied )
+        {
+            switch( p_es->i_type )
+            {
+            case 0x06:
+                /* Handle PES private data */
+                PMTSetupEs0x06( p_demux, pid, p_es );
+                break;
+            /* All other private or reserved types */
+            case 0x0f:
+            case 0x10:
+            case 0x11:
+            case 0x12:
+                PMTSetupEsISO14496( p_demux, pid, prg, p_es );
+                break;
+            case 0x83:
+                /* LPCM (audio) */
+                PMTSetupEs0x83( p_pmt, pid );
+                break;
+            case 0xa0:
+                PMTSetupEs0xA0( p_demux, pid, p_es );
+                break;
+            case 0xd1:
+                PMTSetupEs0xD1( p_demux, pid, p_es );
+                break;
+            case 0xEA:
+                PMTSetupEs0xEA( p_demux, pid, p_es );
+            default:
+                break;
+            }
         }
 
         if( pid->es->fmt.i_cat == AUDIO_ES ||
@@ -4162,13 +4702,13 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
 
         if( pid->es->fmt.i_cat == UNKNOWN_ES )
         {
-            msg_Dbg( p_demux, "  * es pid=%d type=%d *unknown*",
-                     p_es->i_pid, p_es->i_type );
+            msg_Dbg( p_demux, "   => pid %d content is *unknown*",
+                     p_es->i_pid );
         }
         else
         {
-            msg_Dbg( p_demux, "  * es pid=%d type=%d fcc=%4.4s",
-                     p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
+            msg_Dbg( p_demux, "   => pid %d has now es fcc=%4.4s",
+                     p_es->i_pid, (char*)&pid->es->fmt.i_codec );
 
             if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
 
@@ -4219,7 +4759,7 @@ static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
         p_dr = PMTEsFindDescriptor( p_es, 0x09 );
         if( p_dr && p_dr->i_length >= 2 )
         {
-            msg_Dbg( p_demux, "   * descriptor : CA (0x9) SysID 0x%x",
+            msg_Dbg( p_demux, "   * PMT descriptor : CA (0x9) SysID 0x%x",
                      (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
         }