ts.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:136k
源码类别:

midi

开发平台:

Unix_Linux

  1.     return( i_len );
  2. }
  3. static int IODGetByte( int *pi_data, uint8_t **pp_data )
  4. {
  5.     if( *pi_data > 0 )
  6.     {
  7.         const int i_b = **pp_data;
  8.         (*pp_data)++;
  9.         (*pi_data)--;
  10.         return( i_b );
  11.     }
  12.     return( 0 );
  13. }
  14. static int IODGetWord( int *pi_data, uint8_t **pp_data )
  15. {
  16.     const int i1 = IODGetByte( pi_data, pp_data );
  17.     const int i2 = IODGetByte( pi_data, pp_data );
  18.     return( ( i1 << 8 ) | i2 );
  19. }
  20. static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
  21. {
  22.     const int i1 = IODGetByte( pi_data, pp_data );
  23.     const int i2 = IODGetByte( pi_data, pp_data );
  24.     const int i3 = IODGetByte( pi_data, pp_data );
  25.     return( ( i1 << 16 ) | ( i2 << 8) | i3 );
  26. }
  27. static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
  28. {
  29.     const uint32_t i1 = IODGetWord( pi_data, pp_data );
  30.     const uint32_t i2 = IODGetWord( pi_data, pp_data );
  31.     return( ( i1 << 16 ) | i2 );
  32. }
  33. static char* IODGetURL( int *pi_data, uint8_t **pp_data )
  34. {
  35.     char *url;
  36.     int i_url_len, i;
  37.     i_url_len = IODGetByte( pi_data, pp_data );
  38.     url = malloc( i_url_len + 1 );
  39.     if( !url ) return NULL;
  40.     for( i = 0; i < i_url_len; i++ )
  41.     {
  42.         url[i] = IODGetByte( pi_data, pp_data );
  43.     }
  44.     url[i_url_len] = '';
  45.     return( url );
  46. }
  47. static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
  48. {
  49.     iod_descriptor_t *p_iod;
  50.     int i;
  51.     int i_es_index;
  52.     uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
  53.     bool  b_url;
  54.     int   i_iod_length;
  55.     p_iod = malloc( sizeof( iod_descriptor_t ) );
  56.     if( !p_iod ) return NULL;
  57.     memset( p_iod, 0, sizeof( iod_descriptor_t ) );
  58. #ifdef TS_DEBUG
  59.     fprintf( stderr, "n************ IOD ************" );
  60. #endif
  61.     for( i = 0; i < 255; i++ )
  62.     {
  63.         p_iod->es_descr[i].b_ok = 0;
  64.     }
  65.     i_es_index = 0;
  66.     if( i_data < 3 )
  67.     {
  68.         return p_iod;
  69.     }
  70.     byte1 = IODGetByte( &i_data, &p_data );
  71.     byte2 = IODGetByte( &i_data, &p_data );
  72.     byte3 = IODGetByte( &i_data, &p_data );
  73.     if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
  74.     {
  75.         p_iod->i_iod_label_scope = 0x11;
  76.         p_iod->i_iod_label = byte1;
  77.         i_iod_tag = byte2;
  78.     }
  79.     else  //correct implementation of the IOD_descriptor
  80.     {
  81.         p_iod->i_iod_label_scope = byte1;
  82.         p_iod->i_iod_label = byte2;
  83.         i_iod_tag = byte3;
  84.     }
  85. #ifdef TS_DEBUG
  86.     fprintf( stderr, "n* iod_label:%d", p_iod->i_iod_label );
  87.     fprintf( stderr, "n* ===========" );
  88.     fprintf( stderr, "n* tag:0x%x", i_iod_tag );
  89. #endif
  90.     if( i_iod_tag != 0x02 )
  91.     {
  92. #ifdef TS_DEBUG
  93.         fprintf( stderr, "n ERR: tag %02x != 0x02", i_iod_tag );
  94. #endif
  95.         return p_iod;
  96.     }
  97.     i_iod_length = IODDescriptorLength( &i_data, &p_data );
  98. #ifdef TS_DEBUG
  99.     fprintf( stderr, "n* length:%d", i_iod_length );
  100. #endif
  101.     if( i_iod_length > i_data )
  102.     {
  103.         i_iod_length = i_data;
  104.     }
  105.     p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
  106.     i_flags = IODGetByte( &i_data, &p_data );
  107.     p_iod->i_od_id |= i_flags >> 6;
  108.     b_url = ( i_flags >> 5  )&0x01;
  109. #ifdef TS_DEBUG
  110.     fprintf( stderr, "n* od_id:%d", p_iod->i_od_id );
  111.     fprintf( stderr, "n* url flag:%d", b_url );
  112.     fprintf( stderr, "n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
  113. #endif
  114.     if( b_url )
  115.     {
  116.         p_iod->psz_url = IODGetURL( &i_data, &p_data );
  117. #ifdef TS_DEBUG
  118.         fprintf( stderr, "n* url string:%s", p_iod->psz_url );
  119.         fprintf( stderr, "n*****************************n" );
  120. #endif
  121.         return p_iod;
  122.     }
  123.     else
  124.     {
  125.         p_iod->psz_url = NULL;
  126.     }
  127.     p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
  128.     p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
  129.     p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
  130.     p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
  131.     p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
  132. #ifdef TS_DEBUG
  133.     fprintf( stderr, "n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
  134.     fprintf( stderr, "n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
  135.     fprintf( stderr, "n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
  136.     fprintf( stderr, "n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
  137.     fprintf( stderr, "n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
  138. #endif
  139.     while( i_data > 0 && i_es_index < 255)
  140.     {
  141.         int i_tag, i_length;
  142.         int     i_data_sav;
  143.         uint8_t *p_data_sav;
  144.         i_tag = IODGetByte( &i_data, &p_data );
  145.         i_length = IODDescriptorLength( &i_data, &p_data );
  146.         i_data_sav = i_data;
  147.         p_data_sav = p_data;
  148.         i_data = i_length;
  149.         switch( i_tag )
  150.         {
  151.         case 0x03:
  152.             {
  153. #define es_descr    p_iod->es_descr[i_es_index]
  154.                 int i_decoderConfigDescr_length;
  155. #ifdef TS_DEBUG
  156.                 fprintf( stderr, "n* - ES_Descriptor length:%d", i_length );
  157. #endif
  158.                 es_descr.b_ok = 1;
  159.                 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
  160.                 i_flags = IODGetByte( &i_data, &p_data );
  161.                 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
  162.                 b_url = ( i_flags >> 6 )&0x01;
  163.                 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
  164.                 es_descr.i_streamPriority = i_flags & 0x1f;
  165. #ifdef TS_DEBUG
  166.                 fprintf( stderr, "n*   * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
  167.                 fprintf( stderr, "n*   * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
  168.                 fprintf( stderr, "n*   * streamPriority:%d", es_descr.i_streamPriority );
  169. #endif
  170.                 if( es_descr.b_streamDependenceFlag )
  171.                 {
  172.                     es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
  173. #ifdef TS_DEBUG
  174.                     fprintf( stderr, "n*   * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
  175. #endif
  176.                 }
  177.                 if( b_url )
  178.                 {
  179.                     es_descr.psz_url = IODGetURL( &i_data, &p_data );
  180. #ifdef TS_DEBUG
  181.                     fprintf( stderr, "n* url string:%s", es_descr.psz_url );
  182. #endif
  183.                 }
  184.                 else
  185.                 {
  186.                     es_descr.psz_url = NULL;
  187.                 }
  188.                 if( es_descr.b_OCRStreamFlag )
  189.                 {
  190.                     es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
  191. #ifdef TS_DEBUG
  192.                     fprintf( stderr, "n*   * OCR_es_id:%d", es_descr.i_OCR_es_id );
  193. #endif
  194.                 }
  195.                 if( IODGetByte( &i_data, &p_data ) != 0x04 )
  196.                 {
  197. #ifdef TS_DEBUG
  198.                     fprintf( stderr, "n* ERR missing DecoderConfigDescr" );
  199. #endif
  200.                     es_descr.b_ok = 0;
  201.                     break;
  202.                 }
  203.                 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
  204. #ifdef TS_DEBUG
  205.                 fprintf( stderr, "n*   - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
  206. #endif
  207. #define dec_descr   es_descr.dec_descr
  208.                 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
  209.                 i_flags = IODGetByte( &i_data, &p_data );
  210.                 dec_descr.i_streamType = i_flags >> 2;
  211.                 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
  212.                 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
  213.                 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
  214.                 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
  215. #ifdef TS_DEBUG
  216.                 fprintf( stderr, "n*     * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication  );
  217.                 fprintf( stderr, "n*     * streamType:0x%x", dec_descr.i_streamType );
  218.                 fprintf( stderr, "n*     * upStream:%d", dec_descr.b_upStream );
  219.                 fprintf( stderr, "n*     * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
  220.                 fprintf( stderr, "n*     * maxBitrate:%d", dec_descr.i_maxBitrate );
  221.                 fprintf( stderr, "n*     * avgBitrate:%d", dec_descr.i_avgBitrate );
  222. #endif
  223.                 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
  224.                 {
  225.                     int i;
  226.                     dec_descr.i_decoder_specific_info_len =
  227.                         IODDescriptorLength( &i_data, &p_data );
  228.                     if( dec_descr.i_decoder_specific_info_len > 0 )
  229.                     {
  230.                         dec_descr.p_decoder_specific_info =
  231.                             malloc( dec_descr.i_decoder_specific_info_len );
  232.                     }
  233.                     for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
  234.                     {
  235.                         dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
  236.                     }
  237.                 }
  238.                 else
  239.                 {
  240.                     dec_descr.i_decoder_specific_info_len = 0;
  241.                     dec_descr.p_decoder_specific_info = NULL;
  242.                 }
  243.             }
  244. #undef  dec_descr
  245. #define sl_descr    es_descr.sl_descr
  246.             {
  247.                 int i_SLConfigDescr_length;
  248.                 int i_predefined;
  249.                 if( IODGetByte( &i_data, &p_data ) != 0x06 )
  250.                 {
  251. #ifdef TS_DEBUG
  252.                     fprintf( stderr, "n* ERR missing SLConfigDescr" );
  253. #endif
  254.                     es_descr.b_ok = 0;
  255.                     break;
  256.                 }
  257.                 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
  258. #ifdef TS_DEBUG
  259.                 fprintf( stderr, "n*   - SLConfigDescr length:%d", i_SLConfigDescr_length );
  260. #endif
  261.                 i_predefined = IODGetByte( &i_data, &p_data );
  262. #ifdef TS_DEBUG
  263.                 fprintf( stderr, "n*     * i_predefined:0x%x", i_predefined  );
  264. #endif
  265.                 switch( i_predefined )
  266.                 {
  267.                 case 0x01:
  268.                     {
  269.                         sl_descr.b_useAccessUnitStartFlag   = 0;
  270.                         sl_descr.b_useAccessUnitEndFlag     = 0;
  271.                         sl_descr.b_useRandomAccessPointFlag = 0;
  272.                         //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
  273.                         sl_descr.b_usePaddingFlag           = 0;
  274.                         sl_descr.b_useTimeStampsFlags       = 0;
  275.                         sl_descr.b_useIdleFlag              = 0;
  276.                         sl_descr.b_durationFlag     = 0;    // FIXME FIXME
  277.                         sl_descr.i_timeStampResolution      = 1000;
  278.                         sl_descr.i_OCRResolution    = 0;    // FIXME FIXME
  279.                         sl_descr.i_timeStampLength          = 32;
  280.                         sl_descr.i_OCRLength        = 0;    // FIXME FIXME
  281.                         sl_descr.i_AU_Length                = 0;
  282.                         sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
  283.                         sl_descr.i_degradationPriorityLength= 0;
  284.                         sl_descr.i_AU_seqNumLength          = 0;
  285.                         sl_descr.i_packetSeqNumLength       = 0;
  286.                         if( sl_descr.b_durationFlag )
  287.                         {
  288.                             sl_descr.i_timeScale            = 0;    // FIXME FIXME
  289.                             sl_descr.i_accessUnitDuration   = 0;    // FIXME FIXME
  290.                             sl_descr.i_compositionUnitDuration= 0;    // FIXME FIXME
  291.                         }
  292.                         if( !sl_descr.b_useTimeStampsFlags )
  293.                         {
  294.                             sl_descr.i_startDecodingTimeStamp   = 0;    // FIXME FIXME
  295.                             sl_descr.i_startCompositionTimeStamp= 0;    // FIXME FIXME
  296.                         }
  297.                     }
  298.                     break;
  299.                 default:
  300. #ifdef TS_DEBUG
  301.                     fprintf( stderr, "n* ERR unsupported SLConfigDescr predefined" );
  302. #endif
  303.                     es_descr.b_ok = 0;
  304.                     break;
  305.                 }
  306.             }
  307.             break;
  308. #undef  sl_descr
  309. #undef  es_descr
  310.         default:
  311. #ifdef TS_DEBUG
  312.             fprintf( stderr, "n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
  313. #endif
  314.             break;
  315.         }
  316.         p_data = p_data_sav + i_length;
  317.         i_data = i_data_sav - i_length;
  318.         i_es_index++;
  319.     }
  320. #ifdef TS_DEBUG
  321.     fprintf( stderr, "n*****************************n" );
  322. #endif
  323.     return p_iod;
  324. }
  325. static void IODFree( iod_descriptor_t *p_iod )
  326. {
  327.     int i;
  328.     if( p_iod->psz_url )
  329.     {
  330.         free( p_iod->psz_url );
  331.         p_iod->psz_url = NULL;
  332.         free( p_iod );
  333.         return;
  334.     }
  335.     for( i = 0; i < 255; i++ )
  336.     {
  337. #define es_descr p_iod->es_descr[i]
  338.         if( es_descr.b_ok )
  339.         {
  340.             if( es_descr.psz_url )
  341.             {
  342.                 free( es_descr.psz_url );
  343.                 es_descr.psz_url = NULL;
  344.             }
  345.             else
  346.             {
  347.                 free( es_descr.dec_descr.p_decoder_specific_info );
  348.                 es_descr.dec_descr.p_decoder_specific_info = NULL;
  349.                 es_descr.dec_descr.i_decoder_specific_info_len = 0;
  350.             }
  351.         }
  352.         es_descr.b_ok = 0;
  353. #undef  es_descr
  354.     }
  355.     free( p_iod );
  356. }
  357. /****************************************************************************
  358.  ****************************************************************************
  359.  ** libdvbpsi callbacks
  360.  ****************************************************************************
  361.  ****************************************************************************/
  362. static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
  363. {
  364.     demux_sys_t          *p_sys = p_demux->p_sys;
  365.     if( !p_sys->b_access_control )
  366.         return false;
  367.     if( ( p_sys->i_current_program == -1 && p_sys->p_programs_list == NULL ) ||
  368.         p_sys->i_current_program == 0 )
  369.         return true;
  370.     if( p_sys->i_current_program == i_pgrm )
  371.         return true;
  372.     if( p_sys->p_programs_list != NULL )
  373.     {
  374.         for( int i = 0; i < p_sys->p_programs_list->i_count; i++ )
  375.         {
  376.             if( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
  377.                 return true;
  378.         }
  379.     }
  380.     return false;
  381. }
  382. static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
  383. {
  384.     demux_sys_t *p_sys = p_demux->p_sys;
  385.     if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 ) )
  386.         return;
  387.     msg_Warn( p_demux, "Switching to non DVB mode" );
  388.     /* This doesn't look like a DVB stream so don't try
  389.      * parsing the SDT/EDT */
  390.     for( int i = 0x11; i <= 0x12; i++ )
  391.     {
  392.         ts_pid_t *p_pid = &p_sys->pid[i];
  393.         if( p_pid->psi )
  394.         {
  395.             dvbpsi_DetachDemux( p_pid->psi->handle );
  396.             free( p_pid->psi );
  397.             p_pid->psi = NULL;
  398.             p_pid->b_valid = false;
  399.         }
  400.         if( p_sys->b_access_control )
  401.             stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  402.                             ACCESS_SET_PRIVATE_ID_STATE, i, false );
  403.     }
  404.     p_sys->b_dvb_meta = false;
  405. }
  406. #ifdef TS_USE_DVB_SI
  407. /* FIXME same than dvbsi_to_utf8 from dvb access */
  408. static char *EITConvertToUTF8( const unsigned char *psz_instring,
  409.                                size_t i_length )
  410. {
  411.     const char *psz_encoding;
  412.     char *psz_outstring;
  413.     char psz_encbuf[sizeof( "ISO_8859-123" )];
  414.     size_t i_in, i_out, offset = 1;
  415.     vlc_iconv_t iconv_handle;
  416.     if( i_length < 1 ) return NULL;
  417.     if( psz_instring[0] >= 0x20 )
  418.     {
  419.         /* According to ETSI EN 300 468 Annex A, this should be ISO6937,
  420.          * but some broadcasters use different charset... */
  421.         if ( i_broken_epg == 1 )
  422.         {
  423.            psz_encoding = "ISO_8859-1";
  424.         }
  425.         else
  426.         {
  427.            psz_encoding = "ISO_6937";
  428.         }
  429.         offset = 0;
  430.     }
  431.     else switch( psz_instring[0] )
  432.     {
  433.     case 0x01:
  434.         psz_encoding = "ISO_8859-5";
  435.         break;
  436.     case 0x02:
  437.         psz_encoding = "ISO_8859-6";
  438.         break;
  439.     case 0x03:
  440.         psz_encoding = "ISO_8859-7";
  441.         break;
  442.     case 0x04:
  443.         psz_encoding = "ISO_8859-8";
  444.         break;
  445.     case 0x05:
  446.         psz_encoding = "ISO_8859-9";
  447.         break;
  448.     case 0x06:
  449.         psz_encoding = "ISO_8859-10";
  450.         break;
  451.     case 0x07:
  452.         psz_encoding = "ISO_8859-11";
  453.         break;
  454.     case 0x08:
  455.         psz_encoding = "ISO_8859-12";
  456.         break;
  457.     case 0x09:
  458.         psz_encoding = "ISO_8859-13";
  459.         break;
  460.     case 0x0a:
  461.         psz_encoding = "ISO_8859-14";
  462.         break;
  463.     case 0x0b:
  464.         psz_encoding = "ISO_8859-15";
  465.         break;
  466.     case 0x10:
  467. #warning Is Latin-10 (psz_instring[2] == 16) really illegal?
  468.         if( i_length < 3 || psz_instring[1] != 0x00 || psz_instring[2] > 15
  469.          || psz_instring[2] == 0 )
  470.         {
  471.             psz_encoding = "UTF-8";
  472.             offset = 0;
  473.         }
  474.         else
  475.         {
  476.             sprintf( psz_encbuf, "ISO_8859-%u", psz_instring[2] );
  477.             psz_encoding = psz_encbuf;
  478.             offset = 3;
  479.         }
  480.         break;
  481.     case 0x11:
  482. #warning Is there a BOM or do we use a fixed endianess?
  483.         psz_encoding = "UTF-16";
  484.         break;
  485.     case 0x12:
  486.         psz_encoding = "KSC5601-1987";
  487.         break;
  488.     case 0x13:
  489.         psz_encoding = "GB2312"; /* GB-2312-1980 */
  490.         break;
  491.     case 0x14:
  492.         psz_encoding = "BIG-5";
  493.         break;
  494.     case 0x15:
  495.         psz_encoding = "UTF-8";
  496.         break;
  497.     default:
  498.         /* invalid */
  499.         psz_encoding = "UTF-8";
  500.         offset = 0;
  501.     }
  502.     i_in = i_length - offset;
  503.     i_out = i_in * 6 + 1;
  504.     psz_outstring = malloc( i_out );
  505.     if( !psz_outstring )
  506.     {
  507.         return NULL;
  508.     }
  509.     iconv_handle = vlc_iconv_open( "UTF-8", psz_encoding );
  510.     if( iconv_handle == (vlc_iconv_t)(-1) )
  511.     {
  512.          /* Invalid character set (e.g. ISO_8859-12) */
  513.          memcpy( psz_outstring, &psz_instring[offset], i_in );
  514.          psz_outstring[i_in] = '';
  515.          EnsureUTF8( psz_outstring );
  516.     }
  517.     else
  518.     {
  519.         const char *psz_in = (const char *)&psz_instring[offset];
  520.         char *psz_out = psz_outstring;
  521.         while( vlc_iconv( iconv_handle, &psz_in, &i_in,
  522.                           &psz_out, &i_out ) == (size_t)(-1) )
  523.         {
  524.             /* skip naughty byte. This may fail terribly for multibyte stuff,
  525.              * but what can we do anyway? */
  526.             psz_in++;
  527.             i_in--;
  528.             vlc_iconv( iconv_handle, NULL, NULL, NULL, NULL ); /* reset */
  529.         }
  530.         vlc_iconv_close( iconv_handle );
  531.         *psz_out = '';
  532.     }
  533.     return psz_outstring;
  534. }
  535. static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
  536. {
  537.     demux_sys_t          *p_sys = p_demux->p_sys;
  538.     ts_pid_t             *sdt = &p_sys->pid[0x11];
  539.     dvbpsi_sdt_service_t *p_srv;
  540.     msg_Dbg( p_demux, "SDTCallBack called" );
  541.     if( sdt->psi->i_sdt_version != -1 &&
  542.         ( !p_sdt->b_current_next ||
  543.           p_sdt->i_version == sdt->psi->i_sdt_version ) )
  544.     {
  545.         dvbpsi_DeleteSDT( p_sdt );
  546.         return;
  547.     }
  548.     msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
  549.              "network_id=%d",
  550.              p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
  551.              p_sdt->i_network_id );
  552.     i_broken_epg = 0;
  553.     for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
  554.     {
  555.         vlc_meta_t          *p_meta;
  556.         dvbpsi_descriptor_t *p_dr;
  557.         const char *psz_type = NULL;
  558.         const char *psz_status = NULL;
  559.         msg_Dbg( p_demux, "  * service id=%d eit schedule=%d present=%d "
  560.                  "running=%d free_ca=%d",
  561.                  p_srv->i_service_id, p_srv->b_eit_schedule,
  562.                  p_srv->b_eit_present, p_srv->i_running_status,
  563.                  p_srv->b_free_ca );
  564.         if( p_sys->i_current_program != -1 && p_sys->i_current_program != p_srv->i_service_id )
  565.             continue;
  566.         p_meta = vlc_meta_New();
  567.         for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
  568.         {
  569.             if( p_dr->i_tag == 0x48 )
  570.             {
  571.                 static const char *ppsz_type[17] = {
  572.                     "Reserved",
  573.                     "Digital television service",
  574.                     "Digital radio sound service",
  575.                     "Teletext service",
  576.                     "NVOD reference service",
  577.                     "NVOD time-shifted service",
  578.                     "Mosaic service",
  579.                     "PAL coded signal",
  580.                     "SECAM coded signal",
  581.                     "D/D2-MAC",
  582.                     "FM Radio",
  583.                     "NTSC coded signal",
  584.                     "Data broadcast service",
  585.                     "Reserved for Common Interface Usage",
  586.                     "RCS Map (see EN 301 790 [35])",
  587.                     "RCS FLS (see EN 301 790 [35])",
  588.                     "DVB MHP service"
  589.                 };
  590.                 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
  591.                 char *str1 = NULL;
  592.                 char *str2 = NULL;
  593.                 /* Workarounds for broadcasters with broken EPG */
  594.                 if ( p_sdt->i_network_id == 133 )
  595.                    i_broken_epg = 1;  /* SKY DE & BetaDigital use ISO8859-1 */
  596.                 if ( (pD->i_service_provider_name_length == 4) &&
  597.                      !strncmp(pD->i_service_provider_name, "CSAT", 4) )
  598.                    i_broken_epg = 1;  /* CanalSat FR uses ISO8859-1 */
  599.                 /* FIXME: Digital+ ES also uses ISO8859-1 */
  600.                 str1 = EITConvertToUTF8(pD->i_service_provider_name,
  601.                                         pD->i_service_provider_name_length);
  602.                 str2 = EITConvertToUTF8(pD->i_service_name,
  603.                                         pD->i_service_name_length);
  604.                 msg_Dbg( p_demux, "    - type=%d provider=%s name=%s",
  605.                          pD->i_service_type, str1, str2 );
  606.                 vlc_meta_SetTitle( p_meta, str2 );
  607.                 vlc_meta_SetPublisher( p_meta, str1 );
  608.                 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
  609.                     psz_type = ppsz_type[pD->i_service_type];
  610.                 free( str1 );
  611.                 free( str2 );
  612.             }
  613.         }
  614.         if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
  615.         {
  616.             static const char *ppsz_status[5] = {
  617.                 "Unknown",
  618.                 "Not running",
  619.                 "Starts in a few seconds",
  620.                 "Pausing",
  621.                 "Running"
  622.             };
  623.             psz_status = ppsz_status[p_srv->i_running_status];
  624.         }
  625.         if( psz_type )
  626.             vlc_meta_AddExtra( p_meta, "Type", psz_type );
  627.         if( psz_status )
  628.             vlc_meta_AddExtra( p_meta, "Status", psz_status );
  629.         es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
  630.                         p_srv->i_service_id, p_meta );
  631.         vlc_meta_Delete( p_meta );
  632.     }
  633.     sdt->psi->i_sdt_version = p_sdt->i_version;
  634.     dvbpsi_DeleteSDT( p_sdt );
  635. }
  636. /* i_year: year - 1900  i_month: 0-11  i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
  637. static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
  638. {
  639.     static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
  640.     int64_t i_day;
  641.     int i;
  642.     if( i_year < 70 ||
  643.         i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
  644.         i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
  645.         return -1;
  646.     /* Count the number of days */
  647.     i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
  648. #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
  649.     for( i = 70; i < i_year; i++ )
  650.         i_day += LEAP(1900+i);
  651.     if( i_month > 1 )
  652.         i_day += LEAP(1900+i_year);
  653. #undef LEAP
  654.     /**/
  655.     return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
  656. }
  657. static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
  658. {
  659.     const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
  660.     const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
  661.     const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
  662.     *p_y = 1900 + yp + c*1;
  663.     *p_m = mp - 1 - c*12;
  664.     *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
  665. }
  666. #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
  667. static int64_t EITConvertStartTime( uint64_t i_date )
  668. {
  669.     const int i_mjd = i_date >> 24;
  670.     const int i_hour   = CVT_FROM_BCD(i_date >> 16);
  671.     const int i_minute = CVT_FROM_BCD(i_date >>  8);
  672.     const int i_second = CVT_FROM_BCD(i_date      );
  673.     int i_year;
  674.     int i_month;
  675.     int i_day;
  676.     /* if all 40 bits are 1, the start is unknown */
  677.     if( i_date == UINT64_C(0xffffffffff) )
  678.         return -1;
  679.     EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
  680.     return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
  681. }
  682. static int EITConvertDuration( uint32_t i_duration )
  683. {
  684.     return CVT_FROM_BCD(i_duration >> 16) * 3600 +
  685.            CVT_FROM_BCD(i_duration >> 8 ) * 60 +
  686.            CVT_FROM_BCD(i_duration      );
  687. }
  688. #undef CVT_FROM_BCD
  689. static void EITCallBack( demux_t *p_demux,
  690.                          dvbpsi_eit_t *p_eit, bool b_current_following )
  691. {
  692.     demux_sys_t        *p_sys = p_demux->p_sys;
  693.     dvbpsi_eit_event_t *p_evt;
  694.     vlc_epg_t *p_epg;
  695.     msg_Dbg( p_demux, "EITCallBack called" );
  696.     if( !p_eit->b_current_next || ( p_sys->i_current_program != -1 && p_sys->i_current_program != p_eit->i_service_id ) )
  697.     {
  698.         dvbpsi_DeleteEIT( p_eit );
  699.         return;
  700.     }
  701.     msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
  702.              "ts_id=%d network_id=%d segment_last_section_number=%d "
  703.              "last_table_id=%d",
  704.              p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
  705.              p_eit->i_ts_id, p_eit->i_network_id,
  706.              p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
  707.     p_epg = vlc_epg_New( NULL );
  708.     for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
  709.     {
  710.         dvbpsi_descriptor_t *p_dr;
  711.         char                *psz_name = NULL;
  712.         char                *psz_text = NULL;
  713.         char                *psz_extra = strdup("");
  714.         int64_t i_start;
  715.         int i_duration;
  716.         i_start = EITConvertStartTime( p_evt->i_start_time );
  717.         i_duration = EITConvertDuration( p_evt->i_duration );
  718.         msg_Dbg( p_demux, "  * event id=%d start_time:%d duration=%d "
  719.                           "running=%d free_ca=%d",
  720.                  p_evt->i_event_id, (int)i_start, (int)i_duration,
  721.                  p_evt->i_running_status, p_evt->b_free_ca );
  722.         for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
  723.         {
  724.             if( p_dr->i_tag == 0x4d )
  725.             {
  726.                 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
  727.                 if( pE )
  728.                 {
  729.                     psz_name = EITConvertToUTF8( pE->i_event_name, pE->i_event_name_length);
  730.                     psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length );
  731.                     msg_Dbg( p_demux, "    - short event lang=%3.3s '%s' : '%s'",
  732.                              pE->i_iso_639_code, psz_name, psz_text );
  733.                 }
  734.             }
  735.             else if( p_dr->i_tag == 0x4e )
  736.             {
  737.                 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
  738.                 if( pE )
  739.                 {
  740.                     msg_Dbg( p_demux, "    - extended event lang=%3.3s [%d/%d]",
  741.                              pE->i_iso_639_code,
  742.                              pE->i_descriptor_number, pE->i_last_descriptor_number );
  743.                     if( pE->i_text_length > 0 )
  744.                     {
  745.                         char *psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length );
  746.                         if( psz_text )
  747.                         {
  748.                             msg_Dbg( p_demux, "       - text='%s'", psz_text );
  749.                             psz_extra = realloc( psz_extra, strlen(psz_extra) + strlen(psz_text) + 1 );
  750.                             strcat( psz_extra, psz_text );
  751.                             free( psz_text );
  752.                         }
  753.                     }
  754.                     for( int i = 0; i < pE->i_entry_count; i++ )
  755.                     {
  756.                         char *psz_dsc = EITConvertToUTF8( pE->i_item_description[i],
  757.                                                           pE->i_item_description_length[i] );
  758.                         char *psz_itm = EITConvertToUTF8( pE->i_item[i], pE->i_item_length[i] );
  759.                         if( psz_dsc && psz_itm )
  760.                         {
  761.                             msg_Dbg( p_demux, "       - desc='%s' item='%s'", psz_dsc, psz_itm );
  762. #if 0
  763.                             psz_extra = realloc( psz_extra, strlen(psz_extra) + strlen(psz_dsc) + strlen(psz_itm) + 3 + 1 );
  764.                             strcat( psz_extra, "(" );
  765.                             strcat( psz_extra, psz_dsc );
  766.                             strcat( psz_extra, " " );
  767.                             strcat( psz_extra, psz_itm );
  768.                             strcat( psz_extra, ")" );
  769. #endif
  770.                         }
  771.                         free( psz_dsc );
  772.                         free( psz_itm );
  773.                     }
  774.                 }
  775.             }
  776.             else
  777.             {
  778.                 msg_Dbg( p_demux, "    - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
  779.             }
  780.         }
  781.         /* */
  782.         if( i_start > 0 )
  783.             vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
  784.                               *psz_extra ? psz_extra : NULL );
  785.         /* Update "now playing" field */
  786.         if( p_evt->i_running_status == 0x04 && i_start > 0 )
  787.             vlc_epg_SetCurrent( p_epg, i_start );
  788.         free( psz_name );
  789.         free( psz_text );
  790.         free( psz_extra );
  791.     }
  792.     if( p_epg->i_event > 0 )
  793.     {
  794.         if( p_eit->i_service_id == p_sys->i_current_program && b_current_following )
  795.         {
  796.             p_sys->i_dvb_length = 0;
  797.             p_sys->i_dvb_start = 0;
  798.             if( p_epg->p_current )
  799.             {
  800.                 p_sys->i_dvb_start = p_epg->p_current->i_start;
  801.                 p_sys->i_dvb_length = p_epg->p_current->i_duration;
  802.             }
  803.         }
  804.         es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG, p_eit->i_service_id, p_epg );
  805.     }
  806.     vlc_epg_Delete( p_epg );
  807.     dvbpsi_DeleteEIT( p_eit );
  808. }
  809. static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
  810. {
  811.     EITCallBack( p_demux, p_eit, true );
  812. }
  813. static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
  814. {
  815.     EITCallBack( p_demux, p_eit, false );
  816. }
  817. static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
  818.                                  uint8_t  i_table_id, uint16_t i_extension )
  819. {
  820. #if 0
  821.     msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
  822.              i_table_id, i_table_id, i_extension, i_extension );
  823. #endif
  824.     if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
  825.     {
  826.         msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
  827.                  i_table_id, i_table_id, i_extension, i_extension );
  828.         dvbpsi_AttachSDT( h, i_table_id, i_extension,
  829.                           (dvbpsi_sdt_callback)SDTCallBack, p_demux );
  830.     }
  831.     else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
  832.              ( i_table_id == 0x4e || /* Current/Following */
  833.                (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
  834.     {
  835.         msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
  836.                  i_table_id, i_table_id, i_extension, i_extension );
  837.         dvbpsi_eit_callback cb = i_table_id == 0x4e ?
  838.                                     (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
  839.                                     (dvbpsi_eit_callback)EITCallBackSchedule;
  840.         dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
  841.     }
  842. }
  843. #endif
  844. /*****************************************************************************
  845.  * PMT callback and helpers
  846.  *****************************************************************************/
  847. static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
  848.                                                  int i_tag )
  849. {
  850.     dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
  851.     while( p_dr && ( p_dr->i_tag != i_tag ) )
  852.         p_dr = p_dr->p_next;
  853.     return p_dr;
  854. }
  855. static bool PMTEsHasRegistration( demux_t *p_demux,
  856.                                   const dvbpsi_pmt_es_t *p_es,
  857.                                   const char *psz_tag )
  858. {
  859.     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
  860.     if( !p_dr )
  861.         return false;
  862.     if( p_dr->i_length < 4 )
  863.     {
  864.         msg_Warn( p_demux, "invalid Registration Descriptor" );
  865.         return false;
  866.     }
  867.     assert( strlen(psz_tag) == 4 );
  868.     return !memcmp( p_dr->p_data, psz_tag, 4 );
  869. }
  870. static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
  871.                                 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
  872. {
  873.     es_format_t *p_fmt = &pid->es->fmt;
  874.     /* MPEG-4 stream: search SL_DESCRIPTOR */
  875.     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
  876.     if( p_dr && p_dr->i_length == 2 )
  877.     {
  878.         const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
  879.         msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
  880.         pid->es->p_mpeg4desc = NULL;
  881.         for( int i = 0; i < 255; i++ )
  882.         {
  883.             iod_descriptor_t *iod = prg->iod;
  884.             if( iod->es_descr[i].b_ok &&
  885.                 iod->es_descr[i].i_es_id == i_es_id )
  886.             {
  887.                 pid->es->p_mpeg4desc = &iod->es_descr[i];
  888.                 break;
  889.             }
  890.         }
  891.     }
  892.     if( !pid->es->p_mpeg4desc )
  893.     {
  894.         msg_Err( p_demux, "MPEG-4 descriptor not found" );
  895.         return;
  896.     }
  897.     const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
  898.     if( dcd->i_streamType == 0x04 )    /* VisualStream */
  899.     {
  900.         p_fmt->i_cat = VIDEO_ES;
  901.         switch( dcd->i_objectTypeIndication )
  902.         {
  903.         case 0x0B: /* mpeg4 sub */
  904.             p_fmt->i_cat = SPU_ES;
  905.             p_fmt->i_codec = VLC_FOURCC('s','u','b','t');
  906.             break;
  907.         case 0x20: /* mpeg4 */
  908.             p_fmt->i_codec = VLC_FOURCC('m','p','4','v');
  909.             break;
  910.         case 0x21: /* h264 */
  911.             p_fmt->i_codec = VLC_FOURCC('h','2','6','4');
  912.             break;
  913.         case 0x60:
  914.         case 0x61:
  915.         case 0x62:
  916.         case 0x63:
  917.         case 0x64:
  918.         case 0x65: /* mpeg2 */
  919.             p_fmt->i_codec = VLC_FOURCC( 'm','p','g','v' );
  920.             break;
  921.         case 0x6a: /* mpeg1 */
  922.             p_fmt->i_codec = VLC_FOURCC( 'm','p','g','v' );
  923.             break;
  924.         case 0x6c: /* mpeg1 */
  925.             p_fmt->i_codec = VLC_FOURCC( 'j','p','e','g' );
  926.             break;
  927.         default:
  928.             p_fmt->i_cat = UNKNOWN_ES;
  929.             break;
  930.         }
  931.     }
  932.     else if( dcd->i_streamType == 0x05 )    /* AudioStream */
  933.     {
  934.         p_fmt->i_cat = AUDIO_ES;
  935.         switch( dcd->i_objectTypeIndication )
  936.         {
  937.         case 0x40: /* mpeg4 */
  938.             p_fmt->i_codec = VLC_FOURCC('m','p','4','a');
  939.             break;
  940.         case 0x66:
  941.         case 0x67:
  942.         case 0x68: /* mpeg2 aac */
  943.             p_fmt->i_codec = VLC_FOURCC('m','p','4','a');
  944.             break;
  945.         case 0x69: /* mpeg2 */
  946.             p_fmt->i_codec = VLC_FOURCC('m','p','g','a');
  947.             break;
  948.         case 0x6b: /* mpeg1 */
  949.             p_fmt->i_codec = VLC_FOURCC('m','p','g','a');
  950.             break;
  951.         default:
  952.             p_fmt->i_cat = UNKNOWN_ES;
  953.             break;
  954.         }
  955.     }
  956.     else
  957.     {
  958.         p_fmt->i_cat = UNKNOWN_ES;
  959.     }
  960.     if( p_fmt->i_cat != UNKNOWN_ES )
  961.     {
  962.         p_fmt->i_extra = dcd->i_decoder_specific_info_len;
  963.         if( p_fmt->i_extra > 0 )
  964.         {
  965.             p_fmt->p_extra = malloc( p_fmt->i_extra );
  966.             if( p_fmt->p_extra )
  967.                 memcpy( p_fmt->p_extra,
  968.                         dcd->p_decoder_specific_info,
  969.                         p_fmt->i_extra );
  970.             else
  971.                 p_fmt->i_extra = 0;
  972.         }
  973.     }
  974. }
  975. typedef struct
  976. {
  977.     int  i_type;
  978.     int  i_magazine;
  979.     int  i_page;
  980.     char p_iso639[3];
  981. } ts_teletext_page_t;
  982. static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
  983.                                 const dvbpsi_pmt_es_t *p_es )
  984. {
  985.     es_format_t *p_fmt = &pid->es->fmt;
  986.     ts_teletext_page_t p_page[2 * 64 + 20];
  987.     int i_page = 0;
  988.     /* Gather pages informations */
  989. #if defined _DVBPSI_DR_56_H_ && 
  990.     defined DVBPSI_VERSION && DVBPSI_VERSION_INT > ((0<<16)+(1<<8)+5)
  991.     for( int i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
  992.     {
  993.         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
  994.         if( !p_dr )
  995.             continue;
  996.         dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
  997.         if( !p_sub )
  998.             continue;
  999.         for( int i = 0; i < p_sub->i_pages_number; i++ )
  1000.         {
  1001.             const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
  1002.             if( p_src->i_teletext_type < 0 || p_src->i_teletext_type >= 0x06 )
  1003.                 continue;
  1004.             assert( i_page < sizeof(p_page)/sizeof(*p_page) );
  1005.             ts_teletext_page_t *p_dst = &p_page[i_page++];
  1006.             p_dst->i_type = p_src->i_teletext_type;
  1007.             p_dst->i_magazine = p_src->i_teletext_magazine_number
  1008.                 ? p_src->i_teletext_magazine_number : 8;
  1009.             p_dst->i_page = p_src->i_teletext_page_number;
  1010.             memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
  1011.         }
  1012.     }
  1013. #endif
  1014. #ifdef _DVBPSI_DR_59_H_
  1015.     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
  1016.     if( p_dr )
  1017.     {
  1018.         dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
  1019.         for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
  1020.         {
  1021.             dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
  1022.             if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
  1023.                 continue;
  1024.             assert( i_page < sizeof(p_page)/sizeof(*p_page) );
  1025.             ts_teletext_page_t *p_dst = &p_page[i_page++];
  1026.             switch( p_src->i_subtitling_type )
  1027.             {
  1028.             case 0x01:
  1029.                 p_dst->i_type = 0x02;
  1030.                 break;
  1031.             default:
  1032.                 p_dst->i_type = 0x03;
  1033.                 break;
  1034.             }
  1035.             /* FIXME check if it is the right split */
  1036.             p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
  1037.                 ? (p_src->i_composition_page_id >> 8) : 8;
  1038.             p_dst->i_page = p_src->i_composition_page_id & 0xff;
  1039.             memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
  1040.         }
  1041.     }
  1042. #endif
  1043.     /* */
  1044.     es_format_Init( p_fmt, SPU_ES, VLC_FOURCC( 't', 'e', 'l', 'x' ) );
  1045.     /* In stream output mode, do not separate the stream by page */
  1046.     if( p_demux->out->b_sout || i_page <= 0 )
  1047.     {
  1048.         p_fmt->subs.teletext.i_magazine = -1;
  1049.         p_fmt->subs.teletext.i_page = 0;
  1050.         p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
  1051.         dvbpsi_descriptor_t *p_dr;
  1052.         p_dr = PMTEsFindDescriptor( p_es, 0x46 );
  1053.         if( !p_dr )
  1054.             p_dr = PMTEsFindDescriptor( p_es, 0x56 );
  1055.         if( p_demux->out->b_sout && p_dr && p_dr->i_length > 0 )
  1056.         {
  1057.             /* Descriptor pass-through for sout */
  1058.             p_fmt->p_extra = malloc( p_dr->i_length );
  1059.             if( p_fmt->p_extra )
  1060.             {
  1061.                 p_fmt->i_extra = p_dr->i_length;
  1062.                 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
  1063.             }
  1064.         }
  1065.     }
  1066.     else
  1067.     {
  1068.         for( int i = 0; i < i_page; i++ )
  1069.         {
  1070.             ts_es_t *p_es;
  1071.             /* */
  1072.             if( i == 0 )
  1073.             {
  1074.                 p_es = pid->es;
  1075.             }
  1076.             else
  1077.             {
  1078.                 p_es = malloc( sizeof(*p_es) );
  1079.                 if( !p_es )
  1080.                     break;
  1081.                 es_format_Copy( &p_es->fmt, &pid->es->fmt );
  1082.                 free( p_es->fmt.psz_language );
  1083.                 free( p_es->fmt.psz_description );
  1084.                 p_es->fmt.psz_language = NULL;
  1085.                 p_es->fmt.psz_description = NULL;
  1086.                 p_es->id      = NULL;
  1087.                 p_es->p_pes   = NULL;
  1088.                 p_es->i_pes_size = 0;
  1089.                 p_es->i_pes_gathered = 0;
  1090.                 p_es->pp_last = &p_es->p_pes;
  1091.                 p_es->p_mpeg4desc = NULL;
  1092.                 p_es->b_gather = false;
  1093.                 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
  1094.             }
  1095.             /* */
  1096.             const ts_teletext_page_t *p = &p_page[i];
  1097.             p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
  1098.             p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
  1099.             p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
  1100.             p_es->fmt.subs.teletext.i_page = p->i_page;
  1101.             msg_Dbg( p_demux,
  1102.                          "    * ttxt type=%s lan=%s page=%d%02x",
  1103.                          p_es->fmt.psz_description,
  1104.                          p_es->fmt.psz_language,
  1105.                          p->i_magazine, p->i_page );
  1106.         }
  1107.     }
  1108. }
  1109. static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
  1110.                                    const dvbpsi_pmt_es_t *p_es )
  1111. {
  1112.     es_format_t *p_fmt = &pid->es->fmt;
  1113.     es_format_Init( p_fmt, SPU_ES, VLC_FOURCC( 'd', 'v', 'b', 's' ) );
  1114.     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
  1115.     int i_page = 0;
  1116. #ifdef _DVBPSI_DR_59_H_
  1117.     dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
  1118.     for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
  1119.     {
  1120.         const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
  1121.         if( ( i_type >= 0x10 && i_type <= 0x13 ) ||
  1122.             ( i_type >= 0x20 && i_type <= 0x23 ) )
  1123.             i_page++;
  1124.     }
  1125. #endif
  1126.     /* In stream output mode, do not separate the stream by page */
  1127.     if( p_demux->out->b_sout  || i_page <= 0 )
  1128.     {
  1129.         p_fmt->subs.dvb.i_id = -1;
  1130.         p_fmt->psz_description = strdup( _("DVB subtitles") );
  1131.         if( p_demux->out->b_sout && p_dr && p_dr->i_length > 0 )
  1132.         {
  1133.             /* Descriptor pass-through for sout */
  1134.             p_fmt->p_extra = malloc( p_dr->i_length );
  1135.             if( p_fmt->p_extra )
  1136.             {
  1137.                 p_fmt->i_extra = p_dr->i_length;
  1138.                 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
  1139.             }
  1140.         }
  1141.     }
  1142.     else
  1143.     {
  1144. #ifdef _DVBPSI_DR_59_H_
  1145.         for( int i = 0; i < p_sub->i_subtitles_number; i++ )
  1146.         {
  1147.             ts_es_t *p_es;
  1148.             /* */
  1149.             if( i == 0 )
  1150.             {
  1151.                 p_es = pid->es;
  1152.             }
  1153.             else
  1154.             {
  1155.                 p_es = malloc( sizeof(*p_es) );
  1156.                 if( !p_es )
  1157.                     break;
  1158.                 es_format_Copy( &p_es->fmt, &pid->es->fmt );
  1159.                 free( p_es->fmt.psz_language );
  1160.                 free( p_es->fmt.psz_description );
  1161.                 p_es->fmt.psz_language = NULL;
  1162.                 p_es->fmt.psz_description = NULL;
  1163.                 p_es->id      = NULL;
  1164.                 p_es->p_pes   = NULL;
  1165.                 p_es->i_pes_size = 0;
  1166.                 p_es->i_pes_gathered = 0;
  1167.                 p_es->pp_last = &p_es->p_pes;
  1168.                 p_es->p_mpeg4desc = NULL;
  1169.                 p_es->b_gather = false;
  1170.                 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
  1171.             }
  1172.             /* */
  1173.             const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
  1174.             p_es->fmt.psz_language = strndup( p->i_iso6392_language_code, 3 );
  1175.             switch( p->i_subtitling_type )
  1176.             {
  1177.             case 0x10: /* unspec. */
  1178.             case 0x11: /* 4:3 */
  1179.             case 0x12: /* 16:9 */
  1180.             case 0x13: /* 2.21:1 */
  1181.                 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
  1182.                 break;
  1183.             case 0x20: /* Hearing impaired unspec. */
  1184.             case 0x21: /* h.i. 4:3 */
  1185.             case 0x22: /* h.i. 16:9 */
  1186.             case 0x23: /* h.i. 2.21:1 */
  1187.                 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
  1188.                 break;
  1189.             default:
  1190.                 break;
  1191.             }
  1192.             /* Hack, FIXME */
  1193.             p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id <<  0 ) |
  1194.                                       ( p->i_ancillary_page_id   << 16 );
  1195.         }
  1196. #endif
  1197.     }
  1198. }
  1199. static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
  1200.                             const dvbpsi_pmt_es_t *p_es )
  1201. {
  1202.     es_format_t *p_fmt = &pid->es->fmt;
  1203.     if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
  1204.         PMTEsFindDescriptor( p_es, 0x6a ) ||
  1205.         PMTEsFindDescriptor( p_es, 0x81 ) )
  1206.     {
  1207.         p_fmt->i_cat = AUDIO_ES;
  1208.         p_fmt->i_codec = VLC_FOURCC('a','5','2',' ');
  1209.     }
  1210.     else if( PMTEsFindDescriptor( p_es, 0x7a ) )
  1211.     {
  1212.         /* DVB with stream_type 0x06 (ETS EN 300 468) */
  1213.         p_fmt->i_cat = AUDIO_ES;
  1214.         p_fmt->i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
  1215.     }
  1216.     else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
  1217.              PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
  1218.              PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
  1219.              PMTEsFindDescriptor( p_es, 0x73 ) )
  1220.     {
  1221.         /*registration descriptor(ETSI TS 101 154 Annex F)*/
  1222.         p_fmt->i_cat = AUDIO_ES;
  1223.         p_fmt->i_codec = VLC_FOURCC('d','t','s',' ');
  1224.     }
  1225.     else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) )
  1226.     {
  1227.         p_fmt->i_cat = AUDIO_ES;
  1228.         p_fmt->b_packetized = true;
  1229.         p_fmt->i_codec = VLC_FOURCC('a','e','s','3');
  1230.     }
  1231.     else
  1232.     {
  1233.         /* Subtitle/Teletext/VBI fallbacks */
  1234.         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
  1235. #ifdef _DVBPSI_DR_59_H_
  1236.         dvbpsi_subtitling_dr_t *p_sub;
  1237.         if( p_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_dr ) ) )
  1238.         {
  1239.             for( int i = 0; i < p_sub->i_subtitles_number; i++ )
  1240.             {
  1241.                 if( p_fmt->i_cat != UNKNOWN_ES )
  1242.                     break;
  1243.                 switch( p_sub->p_subtitle[i].i_subtitling_type )
  1244.                 {
  1245.                 case 0x01: /* EBU Teletext subtitles */
  1246.                 case 0x02: /* Associated EBU Teletext */
  1247.                 case 0x03: /* VBI data */
  1248.                     PMTSetupEsTeletext( p_demux, pid, p_es );
  1249.                     break;
  1250.                 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
  1251.                 case 0x11: /*                 ...   on 4:3 AR monitor */
  1252.                 case 0x12: /*                 ...   on 16:9 AR monitor */
  1253.                 case 0x13: /*                 ...   on 2.21:1 AR monitor */
  1254.                 case 0x14: /*                 ...   for display on a high definition monitor */
  1255.                 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
  1256.                 case 0x21: /*                 ...   on 4:3 AR monitor */
  1257.                 case 0x22: /*                 ...   on 16:9 AR monitor */
  1258.                 case 0x23: /*                 ...   on 2.21:1 AR monitor */
  1259.                 case 0x24: /*                 ...   for display on a high definition monitor */
  1260.                     PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
  1261.                     break;
  1262.                 default:
  1263.                     msg_Err( p_demux, "Unrecognized DVB subtitle type" );
  1264.                     break;
  1265.                 }
  1266.             }
  1267.         }
  1268. #else
  1269.         if( p_fmt->i_cat == UNKNOWN_ES && p_dr )
  1270.             PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
  1271. #endif
  1272.         if( p_fmt->i_cat == UNKNOWN_ES &&
  1273.             ( PMTEsFindDescriptor( p_es, 0x45 ) ||  /* VBI Data descriptor */
  1274.               PMTEsFindDescriptor( p_es, 0x46 ) ||  /* VBI Teletext descriptor */
  1275.               PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
  1276.         {
  1277.             /* Teletext/VBI */
  1278.             PMTSetupEsTeletext( p_demux, pid, p_es );
  1279.         }
  1280.     }
  1281. #ifdef _DVBPSI_DR_52_H_
  1282.     /* FIXME is it usefull ? */
  1283.     if( PMTEsFindDescriptor( p_es, 0x52 ) )
  1284.     {
  1285.         dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
  1286.         dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
  1287.         msg_Dbg( p_demux, "    * Stream Component Identifier: %d", p_si->i_component_tag );
  1288.     }
  1289. #endif
  1290. }
  1291. static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
  1292.                            const dvbpsi_pmt_es_t *p_es )
  1293. {
  1294.     /* Registration Descriptor */
  1295.     if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
  1296.     {
  1297.         msg_Err( p_demux, "Registration descriptor not found or invalid" );
  1298.         return;
  1299.     }
  1300.     es_format_t *p_fmt = &pid->es->fmt;
  1301.     /* registration descriptor for VC-1 (SMPTE rp227) */
  1302.     p_fmt->i_cat = VIDEO_ES;
  1303.     p_fmt->i_codec = VLC_FOURCC('W','V','C','1');
  1304.     /* XXX With Simple and Main profile the SEQUENCE
  1305.      * header is modified: video width and height are
  1306.      * inserted just after the start code as 2 int16_t
  1307.      * The packetizer will take care of that. */
  1308. }
  1309. static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
  1310.                            const dvbpsi_pmt_es_t *p_es )
  1311. {
  1312.     /* Registration Descriptor */
  1313.     if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
  1314.     {
  1315.         msg_Err( p_demux, "Registration descriptor not found or invalid" );
  1316.         return;
  1317.     }
  1318.     es_format_t *p_fmt = &pid->es->fmt;
  1319.     /* registration descriptor for Dirac
  1320.      * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
  1321.     p_fmt->i_cat = VIDEO_ES;
  1322.     p_fmt->i_codec = VLC_FOURCC('d','r','a','c');
  1323. }
  1324. static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
  1325.                            const dvbpsi_pmt_es_t *p_es )
  1326. {
  1327.     /* MSCODEC sent by vlc */
  1328.     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
  1329.     if( !p_dr || p_dr->i_length < 10 )
  1330.     {
  1331.         msg_Warn( p_demux,
  1332.                   "private MSCODEC (vlc) without bih private descriptor" );
  1333.         return;
  1334.     }
  1335.     es_format_t *p_fmt = &pid->es->fmt;
  1336.     p_fmt->i_cat = VIDEO_ES;
  1337.     p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
  1338.                                  p_dr->p_data[2], p_dr->p_data[3] );
  1339.     p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
  1340.     p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
  1341.     p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
  1342.     if( p_fmt->i_extra > 0 )
  1343.     {
  1344.         p_fmt->p_extra = malloc( p_fmt->i_extra );
  1345.         if( p_fmt->p_extra )
  1346.             memcpy( p_fmt->p_extra, &p_dr->p_data[10],
  1347.                     __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
  1348.         else
  1349.             p_fmt->i_extra = 0;
  1350.     }
  1351.     /* For such stream we will gather them ourself and don't launch a
  1352.      * packetizer.
  1353.      * Yes it's ugly but it's the only way to have DIV3 working */
  1354.     p_fmt->b_packetized = true;
  1355. }
  1356. static void PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
  1357.                            const dvbpsi_pmt_es_t *p_es )
  1358. {
  1359.     es_format_t *p_fmt = &pid->es->fmt;
  1360.     /* Blu-Ray mapping */
  1361.     switch( p_es->i_type )
  1362.     {
  1363.     case 0x80:
  1364.         p_fmt->i_cat = AUDIO_ES;
  1365.         p_fmt->i_codec = VLC_FOURCC( 'b', 'p', 'c', 'm' );
  1366.         break;
  1367.     case 0x82:
  1368.     case 0x85: /* DTS-HD High resolution audio */
  1369.     case 0x86: /* DTS-HD Master audio */
  1370.     case 0xA2: /* Secondary DTS audio */
  1371.         p_fmt->i_cat = AUDIO_ES;
  1372.         p_fmt->i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
  1373.         break;
  1374.     case 0x83: /* TrueHD AC3 */
  1375.         p_fmt->i_cat = AUDIO_ES;
  1376.         p_fmt->i_codec = VLC_FOURCC( 't', 'r', 'h', 'd' );
  1377.         break;
  1378.     case 0x84: /* E-AC3 */
  1379.     case 0xA1: /* Secondary E-AC3 */
  1380.         p_fmt->i_cat = AUDIO_ES;
  1381.         p_fmt->i_codec = VLC_FOURCC( 'e', 'a', 'c', '3' );
  1382.         break;
  1383.     case 0x90: /* Presentation graphics */
  1384.     case 0x91: /* Interactive graphics */
  1385.     case 0x92: /* Subtitle */
  1386.     default:
  1387.         break;
  1388.     }
  1389. }
  1390. static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
  1391.                               const dvbpsi_pmt_es_t *p_es )
  1392. {
  1393.     /* get language descriptor */
  1394.     dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
  1395.     if( !p_dr )
  1396.         return;
  1397.     dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
  1398.     if( !p_decoded )
  1399.     {
  1400.         msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
  1401.         return;
  1402.     }
  1403. #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
  1404.     pid->es->fmt.psz_language = malloc( 4 );
  1405.     if( pid->es->fmt.psz_language )
  1406.     {
  1407.         memcpy( pid->es->fmt.psz_language,
  1408.                 p_decoded->code[0].iso_639_code, 3 );
  1409.         pid->es->fmt.psz_language[3] = 0;
  1410.         msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
  1411.     }
  1412.     switch( p_decoded->code[0].i_audio_type )
  1413.     {
  1414.     case 0:
  1415.         pid->es->fmt.psz_description = NULL;
  1416.         break;
  1417.     case 1:
  1418.         pid->es->fmt.psz_description =
  1419.             strdup(_("clean effects"));
  1420.         break;
  1421.     case 2:
  1422.         pid->es->fmt.psz_description =
  1423.             strdup(_("hearing impaired"));
  1424.         break;
  1425.     case 3:
  1426.         pid->es->fmt.psz_description =
  1427.             strdup(_("visual impaired commentary"));
  1428.         break;
  1429.     default:
  1430.         msg_Dbg( p_demux, "unknown audio type: %d",
  1431.                  p_decoded->code[0].i_audio_type);
  1432.         pid->es->fmt.psz_description = NULL;
  1433.         break;
  1434.     }
  1435.     pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
  1436.     if( pid->es->fmt.i_extra_languages > 0 )
  1437.         pid->es->fmt.p_extra_languages =
  1438.             malloc( sizeof(*pid->es->fmt.p_extra_languages) *
  1439.                     pid->es->fmt.i_extra_languages );
  1440.     if( pid->es->fmt.p_extra_languages )
  1441.     {
  1442.         for( int i = 0; i < pid->es->fmt.i_extra_languages; i++ )
  1443.         {
  1444.             msg_Dbg( p_demux, "bang" );
  1445.             pid->es->fmt.p_extra_languages[i].psz_language =
  1446.                 malloc(4);
  1447.             if( pid->es->fmt.p_extra_languages[i].psz_language )
  1448.             {
  1449.                 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
  1450.                     p_decoded->code[i+1].iso_639_code, 3 );
  1451.                 pid->es->fmt.p_extra_languages[i].psz_language[3] = '';
  1452.             }
  1453.             switch( p_decoded->code[i].i_audio_type )
  1454.             {
  1455.             case 0:
  1456.                 pid->es->fmt.p_extra_languages[i].psz_description =
  1457.                     NULL;
  1458.                 break;
  1459.             case 1:
  1460.                 pid->es->fmt.p_extra_languages[i].psz_description =
  1461.                     strdup(_("clean effects"));
  1462.                 break;
  1463.             case 2:
  1464.                 pid->es->fmt.p_extra_languages[i].psz_description =
  1465.                     strdup(_("hearing impaired"));
  1466.                 break;
  1467.             case 3:
  1468.                 pid->es->fmt.p_extra_languages[i].psz_description =
  1469.                     strdup(_("visual impaired commentary"));
  1470.                 break;
  1471.             default:
  1472.                 msg_Dbg( p_demux, "unknown audio type: %d",
  1473.                         p_decoded->code[i].i_audio_type);
  1474.                 pid->es->fmt.psz_description = NULL;
  1475.                 break;
  1476.             }
  1477.         }
  1478.     }
  1479. #else
  1480.     pid->es->fmt.psz_language = malloc( 4 );
  1481.     if( pid->es->fmt.psz_language )
  1482.     {
  1483.         memcpy( pid->es->fmt.psz_language,
  1484.                 p_decoded->i_iso_639_code, 3 );
  1485.         pid->es->fmt.psz_language[3] = 0;
  1486.     }
  1487. #endif
  1488. }
  1489. static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
  1490. {
  1491.     demux_sys_t          *p_sys = p_demux->p_sys;
  1492.     dvbpsi_descriptor_t  *p_dr;
  1493.     dvbpsi_pmt_es_t      *p_es;
  1494.     ts_pid_t             *pmt = NULL;
  1495.     ts_prg_psi_t         *prg = NULL;
  1496.     ts_pid_t             **pp_clean = NULL;
  1497.     int                  i_clean = 0;
  1498.     bool                 b_hdmv = false;
  1499.     msg_Dbg( p_demux, "PMTCallBack called" );
  1500.     /* First find this PMT declared in PAT */
  1501.     for( int i = 0; i < p_sys->i_pmt; i++ )
  1502.     {
  1503.         int i_prg;
  1504.         for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
  1505.         {
  1506.             const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
  1507.             if( i_pmt_number != TS_USER_PMT_NUMBER && i_pmt_number == p_pmt->i_program_number )
  1508.             {
  1509.                 pmt = p_sys->pmt[i];
  1510.                 prg = p_sys->pmt[i]->psi->prg[i_prg];
  1511.                 break;
  1512.             }
  1513.         }
  1514.         if( pmt )
  1515.             break;
  1516.     }
  1517.     if( pmt == NULL )
  1518.     {
  1519.         msg_Warn( p_demux, "unreferenced program (broken stream)" );
  1520.         dvbpsi_DeletePMT(p_pmt);
  1521.         return;
  1522.     }
  1523.     if( prg->i_version != -1 &&
  1524.         ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
  1525.     {
  1526.         dvbpsi_DeletePMT( p_pmt );
  1527.         return;
  1528.     }
  1529.     /* Clean this program (remove all es) */
  1530.     for( int i = 0; i < 8192; i++ )
  1531.     {
  1532.         ts_pid_t *pid = &p_sys->pid[i];
  1533.         if( pid->b_valid && pid->p_owner == pmt->psi &&
  1534.             pid->i_owner_number == prg->i_number && pid->psi == NULL )
  1535.         {
  1536.             TAB_APPEND( i_clean, pp_clean, pid );
  1537.         }
  1538.     }
  1539.     if( prg->iod )
  1540.     {
  1541.         IODFree( prg->iod );
  1542.         prg->iod = NULL;
  1543.     }
  1544.     msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
  1545.              p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
  1546.     prg->i_pid_pcr = p_pmt->i_pcr_pid;
  1547.     prg->i_version = p_pmt->i_version;
  1548.     ValidateDVBMeta( p_demux, prg->i_pid_pcr );
  1549.     if( ProgramIsSelected( p_demux, prg->i_number ) )
  1550.     {
  1551.         /* Set demux filter */
  1552.         stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  1553.                         ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
  1554.                         true );
  1555.     }
  1556.     else if ( p_sys->b_access_control )
  1557.     {
  1558.         msg_Warn( p_demux, "skipping program (not selected)" );
  1559.         dvbpsi_DeletePMT(p_pmt);
  1560.         return;
  1561.     }
  1562.     /* Parse descriptor */
  1563.     for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
  1564.     {
  1565.         if( p_dr->i_tag == 0x1d )
  1566.         {
  1567.             /* We have found an IOD descriptor */
  1568.             msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
  1569.             prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
  1570.         }
  1571.         else if( p_dr->i_tag == 0x9 )
  1572.         {
  1573.             uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
  1574.                                 | p_dr->p_data[1];
  1575.             msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
  1576.         }
  1577.         else if( p_dr->i_tag == 0x05 )
  1578.         {
  1579.             if( p_dr->i_tag == 0x05 )
  1580.             {
  1581.                 /* Registration Descriptor */
  1582.                 if( p_dr->i_length != 4 )
  1583.                 {
  1584.                     msg_Warn( p_demux, "invalid Registration Descriptor" );
  1585.                 }
  1586.                 else
  1587.                 {
  1588.                     msg_Dbg( p_demux, " * descriptor : registration %4.4s", p_dr->p_data );
  1589.                     if( !memcmp( p_dr->p_data, "HDMV", 4 ) )
  1590.                     {
  1591.                         /* Blu-Ray */
  1592.                         b_hdmv = true;
  1593.                     }
  1594.                 }
  1595.             }
  1596.         }
  1597.         else
  1598.         {
  1599.             msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
  1600.         }
  1601.     }
  1602.     for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
  1603.     {
  1604.         ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
  1605.         /* Find out if the PID was already declared */
  1606.         for( int i = 0; i < i_clean; i++ )
  1607.         {
  1608.             if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
  1609.             {
  1610.                 old_pid = pp_clean[i];
  1611.                 break;
  1612.             }
  1613.         }
  1614.         ValidateDVBMeta( p_demux, p_es->i_pid );
  1615.         if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
  1616.         {
  1617.             msg_Warn( p_demux, "pmt error: pid=%d already defined",
  1618.                       p_es->i_pid );
  1619.             continue;
  1620.         }
  1621.         for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
  1622.              p_dr = p_dr->p_next )
  1623.         {
  1624.             msg_Dbg( p_demux, "  * es pid=%d type=%d dr->i_tag=0x%x",
  1625.                      p_es->i_pid, p_es->i_type, p_dr->i_tag );
  1626.         }
  1627.         PIDInit( pid, false, pmt->psi );
  1628.         PIDFillFormat( pid, p_es->i_type );
  1629.         pid->i_owner_number = prg->i_number;
  1630.         pid->i_pid          = p_es->i_pid;
  1631.         pid->b_seen         = p_sys->pid[p_es->i_pid].b_seen;
  1632.         if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
  1633.             p_es->i_type == 0x12 || p_es->i_type == 0x0f )
  1634.         {
  1635.             PMTSetupEsISO14496( p_demux, pid, prg, p_es );
  1636.         }
  1637.         else if( p_es->i_type == 0x06 )
  1638.         {
  1639.             PMTSetupEs0x06(  p_demux, pid, p_es );
  1640.         }
  1641.         else if( p_es->i_type == 0xEA )
  1642.         {
  1643.             PMTSetupEs0xEA( p_demux, pid, p_es );
  1644.         }
  1645.         else if( p_es->i_type == 0xd1 )
  1646.         {
  1647.             PMTSetupEs0xD1( p_demux, pid, p_es );
  1648.         }
  1649.         else if( p_es->i_type == 0xa0 )
  1650.         {
  1651.             PMTSetupEs0xA0( p_demux, pid, p_es );
  1652.         }
  1653.         else if( b_hdmv )
  1654.         {
  1655.             PMTSetupEsHDMV( p_demux, pid, p_es );
  1656.         }
  1657.         if( pid->es->fmt.i_cat == AUDIO_ES ||
  1658.             ( pid->es->fmt.i_cat == SPU_ES &&
  1659.               pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') &&
  1660.               pid->es->fmt.i_codec != VLC_FOURCC('t','e','l','x') ) )
  1661.         {
  1662.             PMTParseEsIso639( p_demux, pid, p_es );
  1663.         }
  1664.         pid->es->fmt.i_group = p_pmt->i_program_number;
  1665.         for( int i = 0; i < pid->i_extra_es; i++ )
  1666.             pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
  1667.         if( pid->es->fmt.i_cat == UNKNOWN_ES )
  1668.         {
  1669.             msg_Dbg( p_demux, "  * es pid=%d type=%d *unknown*",
  1670.                      p_es->i_pid, p_es->i_type );
  1671.         }
  1672.         else if( !p_sys->b_udp_out )
  1673.         {
  1674.             msg_Dbg( p_demux, "  * es pid=%d type=%d fcc=%4.4s",
  1675.                      p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
  1676.             if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
  1677.             /* Check if we can avoid restarting the ES */
  1678.             if( old_pid &&
  1679.                 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
  1680.                 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
  1681.                 pid->es->fmt.i_extra == 0 &&
  1682.                 pid->i_extra_es == old_pid->i_extra_es &&
  1683.                 ( ( !pid->es->fmt.psz_language &&
  1684.                     !old_pid->es->fmt.psz_language ) ||
  1685.                   ( pid->es->fmt.psz_language &&
  1686.                     old_pid->es->fmt.psz_language &&
  1687.                     !strcmp( pid->es->fmt.psz_language,
  1688.                              old_pid->es->fmt.psz_language ) ) ) )
  1689.             {
  1690.                 pid->es->id = old_pid->es->id;
  1691.                 old_pid->es->id = NULL;
  1692.                 for( int i = 0; i < pid->i_extra_es; i++ )
  1693.                 {
  1694.                     pid->extra_es[i]->id = old_pid->extra_es[i]->id;
  1695.                     old_pid->extra_es[i]->id = NULL;
  1696.                 }
  1697.             }
  1698.             else
  1699.             {
  1700.                 if( old_pid )
  1701.                 {
  1702.                     PIDClean( p_demux->out, old_pid );
  1703.                     TAB_REMOVE( i_clean, pp_clean, old_pid );
  1704.                     old_pid = 0;
  1705.                 }
  1706.                 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
  1707.                 for( int i = 0; i < pid->i_extra_es; i++ )
  1708.                 {
  1709.                     pid->extra_es[i]->id =
  1710.                         es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
  1711.                 }
  1712.             }
  1713.         }
  1714.         /* Add ES to the list */
  1715.         if( old_pid )
  1716.         {
  1717.             PIDClean( p_demux->out, old_pid );
  1718.             TAB_REMOVE( i_clean, pp_clean, old_pid );
  1719.         }
  1720.         p_sys->pid[p_es->i_pid] = *pid;
  1721.         p_dr = PMTEsFindDescriptor( p_es, 0x09 );
  1722.         if( p_dr && p_dr->i_length >= 2 )
  1723.         {
  1724.             uint16_t i_sysid = (p_dr->p_data[0] << 8) | p_dr->p_data[1];
  1725.             msg_Dbg( p_demux, "   * descriptor : CA (0x9) SysID 0x%x",
  1726.                      i_sysid );
  1727.         }
  1728.         if( ProgramIsSelected( p_demux, prg->i_number ) &&
  1729.             ( pid->es->id != NULL || p_sys->b_udp_out ) )
  1730.         {
  1731.             /* Set demux filter */
  1732.             stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  1733.                             ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
  1734.                             true );
  1735.         }
  1736.     }
  1737.     if( ProgramIsSelected( p_demux, prg->i_number ) )
  1738.     {
  1739.         /* Set CAM descrambling */
  1740.         stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  1741.                         ACCESS_SET_PRIVATE_ID_CA, p_pmt );
  1742.     }
  1743.     else
  1744.     {
  1745.         dvbpsi_DeletePMT( p_pmt );
  1746.     }
  1747.     for( int i = 0; i < i_clean; i++ )
  1748.     {
  1749.         if( ProgramIsSelected( p_demux, prg->i_number ) )
  1750.         {
  1751.             stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  1752.                             ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
  1753.                             false );
  1754.         }
  1755.         PIDClean( p_demux->out, pp_clean[i] );
  1756.     }
  1757.     if( i_clean )
  1758.         free( pp_clean );
  1759. }
  1760. static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
  1761. {
  1762.     demux_sys_t          *p_sys = p_demux->p_sys;
  1763.     dvbpsi_pat_program_t *p_program;
  1764.     ts_pid_t             *pat = &p_sys->pid[0];
  1765.     msg_Dbg( p_demux, "PATCallBack called" );
  1766.     if( ( pat->psi->i_pat_version != -1 &&
  1767.             ( !p_pat->b_current_next ||
  1768.               p_pat->i_version == pat->psi->i_pat_version ) ) ||
  1769.         p_sys->b_user_pmt )
  1770.     {
  1771.         dvbpsi_DeletePAT( p_pat );
  1772.         return;
  1773.     }
  1774.     msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
  1775.              p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
  1776.     /* Clean old */
  1777.     if( p_sys->i_pmt > 0 )
  1778.     {
  1779.         int      i_pmt_rm = 0;
  1780.         ts_pid_t **pmt_rm = NULL;
  1781.         /* Search pmt to be deleted */
  1782.         for( int i = 0; i < p_sys->i_pmt; i++ )
  1783.         {
  1784.             ts_pid_t *pmt = p_sys->pmt[i];
  1785.             bool b_keep = false;
  1786.             for( p_program = p_pat->p_first_program; p_program != NULL;
  1787.                  p_program = p_program->p_next )
  1788.             {
  1789.                 if( p_program->i_pid == pmt->i_pid )
  1790.                 {
  1791.                     for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
  1792.                     {
  1793.                         if( p_program->i_number ==
  1794.                             pmt->psi->prg[i_prg]->i_number )
  1795.                         {
  1796.                             b_keep = true;
  1797.                             break;
  1798.                         }
  1799.                     }
  1800.                     if( b_keep )
  1801.                         break;
  1802.                 }
  1803.             }
  1804.             if( !b_keep )
  1805.             {
  1806.                 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
  1807.             }
  1808.         }
  1809.         /* Delete all ES attached to thoses PMT */
  1810.         for( int i = 2; i < 8192; i++ )
  1811.         {
  1812.             ts_pid_t *pid = &p_sys->pid[i];
  1813.             if( !pid->b_valid || pid->psi )
  1814.                 continue;
  1815.             for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
  1816.             {
  1817.                 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
  1818.                 {
  1819.                     /* We only remove es that aren't defined by extra pmt */
  1820.                     if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
  1821.                         continue;
  1822.                     if( p_sys->b_access_control && pid->es->id )
  1823.                     {
  1824.                         if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  1825.                                             ACCESS_SET_PRIVATE_ID_STATE, i,
  1826.                                             false ) )
  1827.                             p_sys->b_access_control = false;
  1828.                     }
  1829.                     PIDClean( p_demux->out, pid );
  1830.                     break;
  1831.                 }
  1832.             }
  1833.         }
  1834.         /* Delete PMT pid */
  1835.         for( int i = 0; i < i_pmt_rm; i++ )
  1836.         {
  1837.             if( p_sys->b_access_control )
  1838.             {
  1839.                 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  1840.                                     ACCESS_SET_PRIVATE_ID_STATE,
  1841.                                     pmt_rm[i]->i_pid, false ) )
  1842.                     p_sys->b_access_control = false;
  1843.             }
  1844.             for( int i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
  1845.             {
  1846.                 const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
  1847.                 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
  1848.             }
  1849.             PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
  1850.             TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
  1851.         }
  1852.         free( pmt_rm );
  1853.     }
  1854.     /* now create programs */
  1855.     for( p_program = p_pat->p_first_program; p_program != NULL;
  1856.          p_program = p_program->p_next )
  1857.     {
  1858.         msg_Dbg( p_demux, "  * number=%d pid=%d", p_program->i_number,
  1859.                  p_program->i_pid );
  1860.         if( p_program->i_number != 0 )
  1861.         {
  1862.             ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
  1863.             bool b_add = true;
  1864.             ValidateDVBMeta( p_demux, p_program->i_pid );
  1865.             if( pmt->b_valid )
  1866.             {
  1867.                 int i_prg;
  1868.                 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
  1869.                 {
  1870.                     if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
  1871.                     {
  1872.                         b_add = false;
  1873.                         break;
  1874.                     }
  1875.                 }
  1876.             }
  1877.             else
  1878.             {
  1879.                 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
  1880.             }
  1881.             if( b_add )
  1882.             {
  1883.                 PIDInit( pmt, true, pat->psi );
  1884.                 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
  1885.                     dvbpsi_AttachPMT( p_program->i_number,
  1886.                                       (dvbpsi_pmt_callback)PMTCallBack,
  1887.                                       p_demux );
  1888.                 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
  1889.                     p_program->i_number;
  1890.                 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
  1891.                     p_program->i_pid;
  1892.                 /* Now select PID at access level */
  1893.                 if( p_sys->b_access_control )
  1894.                 {
  1895.                     if( ProgramIsSelected( p_demux, p_program->i_number ) )
  1896.                     {
  1897.                         if( p_sys->i_current_program == 0 )
  1898.                             p_sys->i_current_program = p_program->i_number;
  1899.                         if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
  1900.                                             ACCESS_SET_PRIVATE_ID_STATE,
  1901.                                             p_program->i_pid, true ) )
  1902.                             p_sys->b_access_control = false;
  1903.                     }
  1904.                 }
  1905.             }
  1906.         }
  1907.     }
  1908.     pat->psi->i_pat_version = p_pat->i_version;
  1909.     dvbpsi_DeletePAT( p_pat );
  1910. }