+static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+
+ if ( !p_sys->b_dvb_control ) return VLC_FALSE;
+ if ( (p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL)
+ || p_sys->i_dvb_program == 0 )
+ return VLC_TRUE;
+ if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
+
+ if ( p_sys->p_programs_list != NULL )
+ {
+ int i;
+ for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
+ {
+ if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
+ return VLC_TRUE;
+ }
+ }
+ return VLC_FALSE;
+}
+
+#ifdef TS_USE_DVB_SI
+static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ ts_pid_t *sdt = &p_sys->pid[0x11];
+ dvbpsi_sdt_service_t *p_srv;
+
+ msg_Dbg( p_demux, "SDTCallBack called" );
+
+ if( sdt->psi->i_sdt_version != -1 &&
+ ( !p_sdt->b_current_next ||
+ p_sdt->i_version == sdt->psi->i_sdt_version ) )
+ {
+ dvbpsi_DeleteSDT( p_sdt );
+ return;
+ }
+
+ msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
+ "network_id=%d",
+ p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
+ p_sdt->i_network_id );
+
+ for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
+ {
+ vlc_meta_t *p_meta;
+ dvbpsi_descriptor_t *p_dr;
+
+ const char *psz_type = NULL;
+ const char *psz_status = NULL;
+
+ msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
+ "running=%d free_ca=%d",
+ p_srv->i_service_id, p_srv->b_eit_schedule,
+ p_srv->b_eit_present, p_srv->i_running_status,
+ p_srv->b_free_ca );
+
+ if( p_sys->i_dvb_program != -1 && p_sys->i_dvb_program != p_srv->i_service_id )
+ continue;
+
+ p_meta = vlc_meta_New();
+ for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
+ {
+ if( p_dr->i_tag == 0x48 )
+ {
+ static const char *ppsz_type[17] = {
+ "Reserved",
+ "Digital television service",
+ "Digital radio sound service",
+ "Teletext service",
+ "NVOD reference service",
+ "NVOD time-shifted service",
+ "Mosaic service",
+ "PAL coded signal",
+ "SECAM coded signal",
+ "D/D2-MAC",
+ "FM Radio",
+ "NTSC coded signal",
+ "Data broadcast service",
+ "Reserved for Common Interface Usage",
+ "RCS Map (see EN 301 790 [35])",
+ "RCS FLS (see EN 301 790 [35])",
+ "DVB MHP service"
+ };
+ dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
+ char str1[257];
+ char str2[257];
+
+ memcpy( str1, pD->i_service_provider_name,
+ pD->i_service_provider_name_length );
+ str1[pD->i_service_provider_name_length] = '\0';
+ memcpy( str2, pD->i_service_name, pD->i_service_name_length );
+ str2[pD->i_service_name_length] = '\0';
+
+ msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
+ pD->i_service_type, str1, str2 );
+
+ vlc_meta_SetTitle( p_meta, str2 );
+ vlc_meta_SetPublisher( p_meta, str1 );
+ if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
+ psz_type = ppsz_type[pD->i_service_type];
+ }
+ }
+
+ if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
+ {
+ static const char *ppsz_status[5] = {
+ "Unknown",
+ "Not running",
+ "Starts in a few seconds",
+ "Pausing",
+ "Running"
+ };
+ psz_status = ppsz_status[p_srv->i_running_status];
+ }
+
+ if( psz_type )
+ vlc_meta_AddExtra( p_meta, "Type", psz_type );
+ if( psz_status )
+ vlc_meta_AddExtra( p_meta, "Status", psz_status );
+
+ es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
+ p_srv->i_service_id, p_meta );
+ vlc_meta_Delete( p_meta );
+ }
+
+ sdt->psi->i_sdt_version = p_sdt->i_version;
+ dvbpsi_DeleteSDT( p_sdt );
+}
+
+/* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
+static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
+{
+ static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
+ int64_t i_day;
+ int i;
+
+ if( i_year < 70 ||
+ i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
+ i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
+ return -1;
+
+ /* Count the number of days */
+ i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
+#define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
+ for( i = 70; i < i_year; i++ )
+ i_day += LEAP(1900+i);
+ if( i_month > 1 )
+ i_day += LEAP(1900+i_year);
+#undef LEAP
+ /**/
+ return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
+}
+
+static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
+{
+ const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
+ const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
+ const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
+
+ *p_y = 1900 + yp + c*1;
+ *p_m = mp - 1 + c*12;
+ *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
+}
+#define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
+static int64_t EITConvertStartTime( uint64_t i_date )
+{
+ const int i_mjd = i_date >> 24;
+ const int i_hour = CVT_FROM_BCD(i_date >> 16);
+ const int i_minute = CVT_FROM_BCD(i_date >> 8);
+ const int i_second = CVT_FROM_BCD(i_date );
+ int i_year;
+ int i_month;
+ int i_day;
+
+ /* if all 40 bits are 1, the start is unknown */
+ if( i_date == UINT64_C(0xffffffffff) )
+ return -1;
+
+ EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
+ return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
+}
+static int EITConvertDuration( uint32_t i_duration )
+{
+ return CVT_FROM_BCD(i_duration >> 16) * 3600 +
+ CVT_FROM_BCD(i_duration >> 8 ) * 60 +
+ CVT_FROM_BCD(i_duration );
+}
+#undef CVT_FROM_BCD
+
+/* FIXME same than dvbsi_to_utf8 from dvb access */
+static char *
+EITConvertToUTF8( const unsigned char *psz_instring, size_t i_length )
+{
+ const char *psz_encoding;
+ char *psz_outstring;
+ char psz_encbuf[sizeof( "ISO_8859-123" )];
+ size_t i_in, i_out, offset = 1;
+ vlc_iconv_t iconv_handle;
+
+ if( i_length < 1 ) return NULL;
+ if( psz_instring[0] >= 0x20 )
+ {
+ psz_encoding = "ISO_8859-1";
+ /* According to the specification, this should be ISO6937,
+ * but it seems Latin-1 is used instead. */
+ offset = 0;
+ }
+ else switch( psz_instring[0] )
+ {
+ case 0x01:
+ psz_encoding = "ISO_8859-5";
+ break;
+ case 0x02:
+ psz_encoding = "ISO_8859-6";
+ break;
+ case 0x03:
+ psz_encoding = "ISO_8859-7";
+ break;
+ case 0x04:
+ psz_encoding = "ISO_8859-8";
+ break;
+ case 0x05:
+ psz_encoding = "ISO_8859-9";
+ break;
+ case 0x06:
+ psz_encoding = "ISO_8859-10";
+ break;
+ case 0x07:
+ psz_encoding = "ISO_8859-11";
+ break;
+ case 0x08:
+ psz_encoding = "ISO_8859-12";
+ break;
+ case 0x09:
+ psz_encoding = "ISO_8859-13";
+ break;
+ case 0x0a:
+ psz_encoding = "ISO_8859-14";
+ break;
+ case 0x0b:
+ psz_encoding = "ISO_8859-15";
+ break;
+ case 0x10:
+#warning Is Latin-10 (psz_instring[2] == 16) really illegal?
+ if( i_length < 3 || psz_instring[1] != 0x00 || psz_instring[2] > 15
+ || psz_instring[2] == 0 )
+ {
+ psz_encoding = "UTF-8";
+ offset = 0;
+ }
+ else
+ {
+ sprintf( psz_encbuf, "ISO_8859-%u", psz_instring[2] );
+ psz_encoding = psz_encbuf;
+ offset = 3;
+ }
+ break;
+ case 0x11:
+#warning Is there a BOM or do we use a fixed endianess?
+ psz_encoding = "UTF-16";
+ break;
+ case 0x12:
+ psz_encoding = "KSC5601-1987";
+ break;
+ case 0x13:
+ psz_encoding = "GB2312"; /* GB-2312-1980 */
+ break;
+ case 0x14:
+ psz_encoding = "BIG-5";
+ break;
+ case 0x15:
+ psz_encoding = "UTF-8";
+ break;
+ default:
+ /* invalid */
+ psz_encoding = "UTF-8";
+ offset = 0;
+ }
+
+ i_in = i_length - offset;
+ i_out = i_in * 6 + 1;
+
+ psz_outstring = malloc( i_out );
+
+ iconv_handle = vlc_iconv_open( "UTF-8", psz_encoding );
+ if( iconv_handle == (vlc_iconv_t)(-1) )
+ {
+ /* Invalid character set (e.g. ISO_8859-12) */
+ memcpy( psz_outstring, psz_instring, i_in );
+ psz_outstring[i_in] = '\0';
+ EnsureUTF8( psz_outstring );
+ }
+ else
+ {
+ const char *psz_in = (const char *)psz_instring;
+ char *psz_out = psz_outstring;
+
+ while( vlc_iconv( iconv_handle, &psz_in, &i_in,
+ &psz_out, &i_out ) == (size_t)(-1) )
+ {
+ /* skip naughty byte. This may fail terribly for multibyte stuff,
+ * but what can we do anyway? */
+ psz_in++;
+ i_in--;
+ vlc_iconv( iconv_handle, NULL, NULL, NULL, NULL ); /* reset */
+ }
+ vlc_iconv_close( iconv_handle );
+
+ *psz_out = '\0';
+ }
+ return psz_outstring;
+}
+
+static void EITCallBack( demux_t *p_demux, dvbpsi_eit_t *p_eit )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ dvbpsi_eit_event_t *p_evt;
+ vlc_epg_t *p_epg;
+
+ msg_Dbg( p_demux, "EITCallBack called" );
+ if( !p_eit->b_current_next || ( p_sys->i_dvb_program != -1 && p_sys->i_dvb_program != p_eit->i_service_id ) )
+ {
+ dvbpsi_DeleteEIT( p_eit );
+ return;
+ }
+
+ msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
+ "ts_id=%d network_id=%d segment_last_section_number=%d "
+ "last_table_id=%d",
+ p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
+ p_eit->i_ts_id, p_eit->i_network_id,
+ p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
+
+ p_epg = vlc_epg_New( NULL );
+ for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
+ {
+ dvbpsi_descriptor_t *p_dr;
+ char *psz_name = NULL;
+ char *psz_text = NULL;
+ char *psz_extra = strdup("");
+ int64_t i_start;
+ int i_duration;
+
+ i_start = EITConvertStartTime( p_evt->i_start_time );
+ i_duration = EITConvertDuration( p_evt->i_duration );
+
+ 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,
+ p_evt->i_running_status, p_evt->b_free_ca );
+
+ for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
+ {
+ if( p_dr->i_tag == 0x4d )
+ {
+ dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
+
+ if( pE )
+ {
+ psz_name = EITConvertToUTF8( pE->i_event_name, pE->i_event_name_length);
+ psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length );
+ 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 )
+ {
+ dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
+ if( pE )
+ {
+ int i;
+ msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
+ pE->i_iso_639_code,
+ pE->i_descriptor_number, pE->i_last_descriptor_number );
+
+ if( pE->i_text_length > 0 )
+ {
+ char *psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length );
+ msg_Dbg( p_demux, " - text='%s'", psz_text );
+
+ psz_extra = realloc( psz_extra, strlen(psz_extra) + strlen(psz_text) + 1 );
+ strcat( psz_extra, psz_text );
+ free( psz_text );
+ }
+
+ for( i = 0; i < pE->i_entry_count; i++ )
+ {
+ char *psz_dsc = EITConvertToUTF8( pE->i_item_description[i],
+ pE->i_item_description_length[i] );
+ char *psz_itm = EITConvertToUTF8( pE->i_item[i], pE->i_item_length[i] );
+
+ if( psz_dsc && psz_itm )
+ {
+ msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
+
+ psz_extra = realloc( psz_extra, strlen(psz_extra) + strlen(psz_dsc) + strlen(psz_itm) + 3 + 1 );
+ strcat( psz_extra, "(" );
+ strcat( psz_extra, psz_dsc );
+ strcat( psz_extra, " " );
+ strcat( psz_extra, psz_itm );
+ strcat( psz_extra, ")" );
+ }
+ if( psz_dsc )
+ free( psz_dsc );
+ if( psz_itm )
+ free( psz_itm );
+ }
+ }
+ }
+ else
+ {
+ msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
+ }
+ }
+
+ /* */
+ if( i_start > 0 )
+ vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text, psz_extra );
+
+ /* Update "now playing" field */
+ if( p_evt->i_running_status == 0x04 && i_start > 0 )
+ vlc_epg_SetCurrent( p_epg, i_start );
+
+ if( psz_name )
+ free( psz_name );
+ if( psz_text )
+ free( psz_text );
+
+ free( psz_extra );
+ }
+ if( p_epg->i_event > 0 )
+ {
+ if( p_eit->i_service_id == p_sys->i_dvb_program )
+ {
+ p_sys->i_dvb_length = 0;
+ p_sys->i_dvb_start = 0;
+
+ if( p_epg->p_current )
+ {
+ p_sys->i_dvb_start = p_epg->p_current->i_start;
+ p_sys->i_dvb_length = p_epg->p_current->i_duration;
+ }
+ }
+ es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG, p_eit->i_service_id, p_epg );
+ }
+ vlc_epg_Delete( p_epg );
+
+ dvbpsi_DeleteEIT( p_eit );
+}
+
+static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
+ uint8_t i_table_id, uint16_t i_extension )
+{
+#if 0
+ msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
+ i_table_id, i_table_id, i_extension, i_extension );
+#endif
+ if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
+ {
+ msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
+ i_table_id, i_table_id, i_extension, i_extension );
+
+ dvbpsi_AttachSDT( h, i_table_id, i_extension,
+ (dvbpsi_sdt_callback)SDTCallBack, p_demux );
+ }
+ else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
+ ( i_table_id == 0x4e || /* Current/Following */
+ (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
+ {
+ msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
+ i_table_id, i_table_id, i_extension, i_extension );
+
+ dvbpsi_AttachEIT( h, i_table_id, i_extension,
+ (dvbpsi_eit_callback)EITCallBack, p_demux );
+ }
+}
+#endif
+