1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x.nl>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
38 #include <vlc_access.h> /* DVB-specific things */
39 #include <vlc_demux.h>
42 #include <vlc_charset.h> /* FromCharset, for EIT */
44 #include "../mux/mpeg/csa.h"
46 /* Include dvbpsi headers */
47 # include <dvbpsi/dvbpsi.h>
48 # include <dvbpsi/demux.h>
49 # include <dvbpsi/descriptor.h>
50 # include <dvbpsi/pat.h>
51 # include <dvbpsi/pmt.h>
52 # include <dvbpsi/sdt.h>
53 # include <dvbpsi/dr.h>
54 # include <dvbpsi/psi.h>
57 # include <dvbpsi/eit.h>
60 # include <dvbpsi/tot.h>
62 #include "../mux/mpeg/dvbpsi_compat.h"
65 VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
70 vfprintf(stderr, format, ap);
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open ( vlc_object_t * );
81 static void Close ( vlc_object_t * );
84 * - Rename "extra pmt" to "user pmt"
85 * - Update extra pmt description
86 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
87 * where pid_description could take 3 forms:
88 * 1. pid:pcr (to force the pcr pid)
90 * 3. pid:type=fourcc where type=(video|audio|spu)
92 #define PMT_TEXT N_("Extra PMT")
93 #define PMT_LONGTEXT N_( \
94 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
96 #define PID_TEXT N_("Set id of ES to PID")
97 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
98 " handled by VLC to the same value as the PID in" \
99 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
100 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
102 #define TSOUT_TEXT N_("Fast udp streaming")
103 #define TSOUT_LONGTEXT N_( \
104 "Sends TS to specific ip:port by udp (you must know what you are doing).")
106 #define MTUOUT_TEXT N_("MTU for out mode")
107 #define MTUOUT_LONGTEXT N_("MTU for out mode.")
109 #define CSA_TEXT N_("CSA Key")
110 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
111 "16 char string (8 hexadecimal bytes).")
113 #define CSA2_TEXT N_("Second CSA Key")
114 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
115 "16 char string (8 hexadecimal bytes).")
118 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
119 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
120 "The decryption routines subtract the TS-header from the value before " \
123 #define SPLIT_ES_TEXT N_("Separate sub-streams")
124 #define SPLIT_ES_LONGTEXT N_( \
125 "Separate teletex/dvbs pages into independent ES. " \
126 "It can be useful to turn off this option when using stream output." )
128 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
129 #define SEEK_PERCENT_LONGTEXT N_( \
130 "Seek and position based on a percent byte position, not a PCR generated " \
131 "time position. If seeking doesn't work property, turn on this option." )
133 #define PCR_TEXT N_("Trust in-stream PCR")
134 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
137 set_description( N_("MPEG Transport Stream demuxer") )
138 set_shortname ( "MPEG-TS" )
139 set_category( CAT_INPUT )
140 set_subcategory( SUBCAT_INPUT_DEMUX )
142 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
143 add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
145 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
147 add_obsolete_string( "ts-out" ) /* since 2.2.0 */
148 add_obsolete_integer( "ts-out-mtu" ) /* since 2.2.0 */
149 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
151 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
153 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
156 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
157 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
159 add_obsolete_bool( "ts-silent" );
161 set_capability( "demux", 10 )
162 set_callbacks( Open, Close )
166 /*****************************************************************************
168 *****************************************************************************/
169 static const char *const ppsz_teletext_type[] = {
172 N_("Teletext subtitles"),
173 N_("Teletext: additional information"),
174 N_("Teletext: program schedule"),
175 N_("Teletext subtitles: hearing impaired")
180 uint8_t i_objectTypeIndication;
181 uint8_t i_streamType;
186 } decoder_config_descriptor_t;
195 decoder_config_descriptor_t dec_descr;
197 } es_mpeg4_descriptor_t;
199 #define ES_DESCRIPTOR_COUNT 255
205 es_mpeg4_descriptor_t es_descr[ES_DESCRIPTOR_COUNT];
211 dvbpsi_handle handle;
217 /* IOD stuff (mpeg4) */
218 iod_descriptor_t *iod;
224 /* for special PAT/SDT case */
225 dvbpsi_handle handle; /* PAT/SDT/EIT */
238 TS_ES_DATA_TABLE_SECTION
243 TS_PMT_REGISTRATION_NONE = 0,
244 TS_PMT_REGISTRATION_HDMV
245 } ts_pmt_registration_type_t;
251 ts_es_data_type_t data_type;
257 es_mpeg4_descriptor_t *p_mpeg4desc;
267 int i_cc; /* countinuity counter */
270 /* PSI owner (ie PMT -> PAT, ES -> PMT */
278 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
286 vlc_mutex_t csa_lock;
288 /* TS packet size (188, 192, 204) */
291 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
292 int i_packet_header_size;
294 /* how many TS packet we read at once */
297 /* to determine length and time */
300 mtime_t i_current_pcr;
302 bool b_force_seek_per_percent;
325 bool b_access_control;
331 int64_t i_dvb_length;
332 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
335 int i_current_program;
336 vlc_list_t programs_list;
342 static int Demux ( demux_t *p_demux );
343 static int Control( demux_t *p_demux, int i_query, va_list args );
345 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
346 static void PIDClean( demux_t *, ts_pid_t *pid );
347 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
349 static void PATCallBack( void*, dvbpsi_pat_t * );
350 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
351 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
352 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
353 uint16_t i_extension, demux_t * );
355 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
356 uint8_t i_table_id, uint16_t i_extension );
359 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
361 static inline int PIDGet( block_t *p )
363 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
366 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
368 static block_t* ReadTSPacket( demux_t *p_demux );
369 static int Seek( demux_t *p_demux, double f_percent );
370 static void GetFirstPCR( demux_t *p_demux );
371 static void GetLastPCR( demux_t *p_demux );
372 static void CheckPCR( demux_t *p_demux );
373 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
375 static void IODFree( iod_descriptor_t * );
377 #define TS_USER_PMT_NUMBER (0)
378 static int UserPmt( demux_t *p_demux, const char * );
380 static int SetPIDFilter( demux_t *, int i_pid, bool b_selected );
381 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
383 #define TS_PACKET_SIZE_188 188
384 #define TS_PACKET_SIZE_192 192
385 #define TS_PACKET_SIZE_204 204
386 #define TS_PACKET_SIZE_MAX 204
387 #define TS_TOPFIELD_HEADER 1320
389 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size )
391 const uint8_t *p_peek;
392 if( stream_Peek( p_demux->s,
393 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
398 if( memcmp( p_peek, "TFrc", 4 ) == 0 )
401 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
402 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
403 * but after the filename the offsets seem to be incorrect. - DJ */
404 int i_duration, i_name;
405 char *psz_name = xmalloc(25);
406 char *psz_event_name;
407 char *psz_event_text = xmalloc(130);
408 char *psz_ext_text = xmalloc(1025);
410 // 2 bytes version Uimsbf (4,5)
411 // 2 bytes reserved (6,7)
412 // 2 bytes duration in minutes Uimsbf (8,9(
413 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
414 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
415 // 2 bytes service number in channel list (10, 11)
416 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
417 // 4 bytes of reserved + tuner info (14,15,16,17)
418 // 2 bytes of Service ID Bslbf (18,19)
419 // 2 bytes of PMT PID Uimsbf (20,21)
420 // 2 bytes of PCR PID Uimsbf (22,23)
421 // 2 bytes of Video PID Uimsbf (24,25)
422 // 2 bytes of Audio PID Uimsbf (26,27)
423 // 24 bytes filename Bslbf
424 memcpy( psz_name, &p_peek[28], 24 );
426 msg_Dbg( p_demux, "recordingname=%s", psz_name );
427 // 1 byte of sat index Uimsbf (52)
428 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
429 // 4 bytes of freq. Uimsbf (56,57,58,59)
430 // 2 bytes of symbol rate Uimsbf (60,61)
431 // 2 bytes of TS stream ID Uimsbf (62,63)
434 // 2 bytes duration Uimsbf (70,71)
435 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
436 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
437 // 4 bytes EventID Uimsbf (72-75)
438 // 8 bytes of Start and End time info (76-83)
439 // 1 byte reserved (84)
440 // 1 byte event name length Uimsbf (89)
441 i_name = (int)(p_peek[89]&~0x81);
442 msg_Dbg( p_demux, "event name length = %d", i_name);
443 psz_event_name = xmalloc( i_name+1 );
444 // 1 byte parental rating (90)
445 // 129 bytes of event text
446 memcpy( psz_event_name, &p_peek[91], i_name );
447 psz_event_name[i_name] = '\0';
448 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
449 psz_event_text[129-i_name] = '\0';
450 msg_Dbg( p_demux, "event name=%s", psz_event_name );
451 msg_Dbg( p_demux, "event text=%s", psz_event_text );
452 // 12 bytes reserved (220)
454 // 2 bytes Event Text Length Uimsbf
455 // 4 bytes EventID Uimsbf
456 // FIXME We just have 613 bytes. not enough for this entire text
457 // 1024 bytes Extended Event Text Bslbf
458 memcpy( psz_ext_text, p_peek+372, 1024 );
459 psz_ext_text[1024] = '\0';
460 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
461 // 52 bytes reserved Bslbf
463 msg_Dbg( p_demux, "this is a topfield file" );
464 return TS_PACKET_SIZE_188;
467 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
469 if( p_peek[i_sync] != 0x47 )
472 /* Check next 3 sync bytes */
473 int i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
474 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
476 msg_Err( p_demux, "cannot peek" );
479 if( p_peek[i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
480 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
481 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
483 return TS_PACKET_SIZE_188;
485 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
486 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
487 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
491 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
493 return TS_PACKET_SIZE_192;
495 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
496 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
497 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
499 return TS_PACKET_SIZE_204;
503 if( p_demux->b_force )
505 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
506 return TS_PACKET_SIZE_188;
508 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
512 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
513 static void vlc_dvbpsi_reset( demux_t *p_demux )
515 demux_sys_t *p_sys = p_demux->p_sys;
517 ts_pid_t *pat = &p_sys->pid[0];
518 ts_pid_t *sdt = &p_sys->pid[0x11];
519 ts_pid_t *eit = &p_sys->pid[0x12];
520 ts_pid_t *tdt = &p_sys->pid[0x14];
522 if( pat->psi->handle )
524 if( dvbpsi_decoder_present( pat->psi->handle ) )
525 dvbpsi_pat_detach( pat->psi->handle );
526 dvbpsi_delete( pat->psi->handle );
527 pat->psi->handle = NULL;
530 if( sdt->psi->handle )
532 if( dvbpsi_decoder_present( sdt->psi->handle ) )
533 dvbpsi_DetachDemux( sdt->psi->handle );
534 dvbpsi_delete( sdt->psi->handle );
535 sdt->psi->handle = NULL;
538 if( eit->psi->handle )
540 if( dvbpsi_decoder_present( eit->psi->handle ) )
541 dvbpsi_DetachDemux( eit->psi->handle );
542 dvbpsi_delete( eit->psi->handle );
543 eit->psi->handle = NULL;
546 if( tdt->psi->handle )
548 if( dvbpsi_decoder_present( tdt->psi->handle ) )
549 dvbpsi_DetachDemux( tdt->psi->handle );
550 dvbpsi_delete( tdt->psi->handle );
551 tdt->psi->handle = NULL;
556 /*****************************************************************************
558 *****************************************************************************/
559 static int Open( vlc_object_t *p_this )
561 demux_t *p_demux = (demux_t*)p_this;
564 int i_packet_size, i_packet_header_size = 0;
568 /* Search first sync byte */
569 i_packet_size = DetectPacketSize( p_demux, &i_packet_header_size );
570 if( i_packet_size < 0 )
573 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
576 memset( p_sys, 0, sizeof( demux_sys_t ) );
577 vlc_mutex_init( &p_sys->csa_lock );
579 p_demux->pf_demux = Demux;
580 p_demux->pf_control = Control;
582 /* Init p_sys field */
583 p_sys->b_dvb_meta = true;
584 p_sys->b_access_control = true;
585 p_sys->i_current_program = 0;
586 p_sys->programs_list.i_count = 0;
587 p_sys->programs_list.p_values = NULL;
588 p_sys->i_tdt_delta = 0;
589 p_sys->i_dvb_start = 0;
590 p_sys->i_dvb_length = 0;
592 p_sys->b_broken_charset = false;
594 for( int i = 0; i < 8192; i++ )
596 ts_pid_t *pid = &p_sys->pid[i];
599 pid->b_valid = false;
601 /* PID 8191 is padding */
602 p_sys->pid[8191].b_seen = true;
603 p_sys->i_packet_size = i_packet_size;
604 p_sys->i_packet_header_size = i_packet_header_size;
605 p_sys->i_ts_read = 50;
607 p_sys->b_start_record = false;
609 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
610 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
612 (table)->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG ); \
613 if( ! (table)->psi->handle ) \
615 vlc_mutex_destroy( &p_sys->csa_lock ); \
619 (table)->psi->handle->p_sys = (void *) VLC_OBJECT(obj); \
620 if( !dvbpsi_AttachDemux( (table)->psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
622 vlc_dvbpsi_reset( obj ); \
623 vlc_mutex_destroy( &p_sys->csa_lock ); \
625 return VLC_EGENERIC; \
630 /* Init PAT handler */
631 pat = &p_sys->pid[0];
632 PIDInit( pat, true, NULL );
633 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
634 pat->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
635 if( !pat->psi->handle )
637 vlc_mutex_destroy( &p_sys->csa_lock );
641 pat->psi->handle->p_sys = (void *) p_demux;
642 if( !dvbpsi_pat_attach( pat->psi->handle, PATCallBack, p_demux ) )
644 vlc_dvbpsi_reset( p_demux );
645 vlc_mutex_destroy( &p_sys->csa_lock );
650 pat->psi->handle = dvbpsi_AttachPAT( PATCallBack, p_demux );
652 if( p_sys->b_dvb_meta )
654 ts_pid_t *sdt = &p_sys->pid[0x11];
655 ts_pid_t *eit = &p_sys->pid[0x12];
657 PIDInit( sdt, true, NULL );
658 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
659 VLC_DVBPSI_DEMUX_TABLE_INIT( sdt, p_demux )
662 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
665 PIDInit( eit, true, NULL );
666 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
667 VLC_DVBPSI_DEMUX_TABLE_INIT( eit, p_demux )
670 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
673 ts_pid_t *tdt = &p_sys->pid[0x14];
674 PIDInit( tdt, true, NULL );
675 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
676 VLC_DVBPSI_DEMUX_TABLE_INIT( tdt, p_demux )
679 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
683 if( p_sys->b_access_control )
685 if( SetPIDFilter( p_demux, 0x11, true ) ||
686 SetPIDFilter( p_demux, 0x14, true ) ||
687 SetPIDFilter( p_demux, 0x12, true ) )
688 p_sys->b_access_control = false;
692 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
693 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
697 TAB_INIT( p_sys->i_pmt, p_sys->pmt );
701 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
703 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
705 /* We handle description of an extra PMT */
706 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
707 p_sys->b_user_pmt = false;
708 if( psz_string && *psz_string )
709 UserPmt( p_demux, psz_string );
712 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
713 if( psz_string && *psz_string )
718 p_sys->csa = csa_New();
720 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
721 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
722 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
724 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
726 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
729 else if ( i_res == VLC_SUCCESS )
731 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
735 csa_Delete( p_sys->csa );
741 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
742 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
744 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
745 if( i_pkt < 4 || i_pkt > 188 )
747 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
748 msg_Warn( p_demux, "using default packet size of 188 bytes" );
749 p_sys->i_csa_pkt_size = 188;
752 p_sys->i_csa_pkt_size = i_pkt;
753 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
759 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
761 p_sys->i_pid_ref_pcr = -1;
762 p_sys->i_first_pcr = -1;
763 p_sys->i_current_pcr = -1;
764 p_sys->i_last_pcr = -1;
765 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
766 p_sys->i_pcrs_num = 10;
767 p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
768 p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
770 if( !p_sys->p_pcrs || !p_sys->p_pos )
776 bool can_seek = false;
777 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &can_seek );
780 GetFirstPCR( p_demux );
782 GetLastPCR( p_demux );
784 if( p_sys->i_first_pcr < 0 || p_sys->i_last_pcr < 0 )
786 msg_Dbg( p_demux, "Force Seek Per Percent: PCR's not found,");
787 p_sys->b_force_seek_per_percent = true;
790 while( p_sys->i_pmt_es <= 0 && vlc_object_alive( p_demux ) )
792 if( Demux( p_demux ) != 1 )
798 /*****************************************************************************
800 *****************************************************************************/
801 static void Close( vlc_object_t *p_this )
803 demux_t *p_demux = (demux_t*)p_this;
804 demux_sys_t *p_sys = p_demux->p_sys;
806 msg_Dbg( p_demux, "pid list:" );
807 for( int i = 0; i < 8192; i++ )
809 ts_pid_t *pid = &p_sys->pid[i];
811 if( pid->b_valid && pid->psi )
816 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
817 if( dvbpsi_decoder_present( pid->psi->handle ) )
818 dvbpsi_pat_detach( pid->psi->handle );
819 dvbpsi_delete( pid->psi->handle );
820 pid->psi->handle = NULL;
822 dvbpsi_DetachPAT( pid->psi->handle );
830 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
832 /* SDT or EIT or TDT */
833 dvbpsi_DetachDemux( pid->psi->handle );
834 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
835 dvbpsi_delete( pid->psi->handle );
836 pid->psi->handle = NULL;
842 PIDClean( p_demux, pid );
847 else if( pid->b_valid && pid->es )
849 PIDClean( p_demux, pid );
854 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
859 SetPIDFilter( p_demux, pid->i_pid, false );
862 vlc_mutex_lock( &p_sys->csa_lock );
865 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
866 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
867 csa_Delete( p_sys->csa );
869 vlc_mutex_unlock( &p_sys->csa_lock );
871 TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
873 free( p_sys->programs_list.p_values );
875 free( p_sys->p_pcrs );
876 free( p_sys->p_pos );
878 vlc_mutex_destroy( &p_sys->csa_lock );
882 /*****************************************************************************
883 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
884 *****************************************************************************/
885 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
886 vlc_value_t oldval, vlc_value_t newval,
889 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
890 demux_t *p_demux = (demux_t*)p_this;
891 demux_sys_t *p_sys = p_demux->p_sys;
892 int i_tmp = (intptr_t)p_data;
894 vlc_mutex_lock( &p_sys->csa_lock );
896 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
898 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
900 vlc_mutex_unlock( &p_sys->csa_lock );
904 /*****************************************************************************
906 *****************************************************************************/
907 static int Demux( demux_t *p_demux )
909 demux_sys_t *p_sys = p_demux->p_sys;
910 bool b_wait_es = p_sys->i_pmt_es <= 0;
912 /* We read at most 100 TS packet or until a frame is completed */
913 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
915 bool b_frame = false;
917 if( !(p_pkt = ReadTSPacket( p_demux )) )
922 if( p_sys->b_start_record )
924 /* Enable recording once synchronized */
925 stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, true, "ts" );
926 p_sys->b_start_record = false;
929 /* Parse the TS packet */
930 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
936 if( p_pid->i_pid == 0 || ( p_sys->b_dvb_meta && ( p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 || p_pid->i_pid == 0x14 ) ) )
938 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
942 for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
944 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
948 block_Release( p_pkt );
952 b_frame = GatherData( p_demux, p_pid, p_pkt );
959 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
961 /* We have to handle PCR if present */
962 PCRHandle( p_demux, p_pid, p_pkt );
963 block_Release( p_pkt );
965 p_pid->b_seen = true;
967 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
971 demux_UpdateTitleFromStream( p_demux );
975 /*****************************************************************************
977 *****************************************************************************/
978 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
980 demux_sys_t *p_sys = p_demux->p_sys;
986 if( p_sys->i_dvb_length > 0 )
988 const int64_t t = mdate() + p_sys->i_tdt_delta;
990 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
993 *pi_length = p_sys->i_dvb_length;
995 *pi_time = t - p_sys->i_dvb_start;
1002 static int Control( demux_t *p_demux, int i_query, va_list args )
1004 demux_sys_t *p_sys = p_demux->p_sys;
1006 bool b_bool, *pb_bool;
1013 case DEMUX_GET_POSITION:
1014 pf = (double*) va_arg( args, double* );
1016 if( p_sys->b_force_seek_per_percent ||
1017 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1018 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 ||
1019 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1021 int64_t i_time, i_length;
1022 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1023 *pf = (double)i_time/(double)i_length;
1024 else if( (i64 = stream_Size( p_demux->s) ) > 0 )
1026 int64_t offset = stream_Tell( p_demux->s );
1028 *pf = (double)offset / (double)i64;
1035 *pf = (double)(p_sys->i_current_pcr - p_sys->i_first_pcr) / (double)(p_sys->i_last_pcr - p_sys->i_first_pcr);
1039 case DEMUX_SET_POSITION:
1040 f = (double) va_arg( args, double );
1042 if( p_sys->b_force_seek_per_percent ||
1043 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1044 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1046 i64 = stream_Size( p_demux->s );
1047 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
1048 return VLC_EGENERIC;
1052 if( Seek( p_demux, f ) )
1054 p_sys->b_force_seek_per_percent = true;
1055 return VLC_EGENERIC;
1060 case DEMUX_GET_TIME:
1061 pi64 = (int64_t*)va_arg( args, int64_t * );
1062 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1063 p_sys->b_force_seek_per_percent ||
1064 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 )
1066 if( DVBEventInformation( p_demux, pi64, NULL ) )
1073 *pi64 = (p_sys->i_current_pcr - p_sys->i_first_pcr) * 100 / 9;
1077 case DEMUX_GET_LENGTH:
1078 pi64 = (int64_t*)va_arg( args, int64_t * );
1079 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1080 p_sys->b_force_seek_per_percent ||
1081 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1083 if( DVBEventInformation( p_demux, NULL, pi64 ) )
1090 *pi64 = (p_sys->i_last_pcr - p_sys->i_first_pcr) * 100 / 9;
1094 case DEMUX_SET_GROUP:
1098 i_int = (int)va_arg( args, int );
1099 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1100 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1102 if( i_int == 0 && p_sys->i_current_program > 0 )
1103 i_int = p_sys->i_current_program;
1105 if( p_sys->i_current_program > 0 )
1107 if( p_sys->i_current_program != i_int )
1108 SetPrgFilter( p_demux, p_sys->i_current_program, false );
1110 else if( p_sys->i_current_program < 0 )
1112 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
1113 SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
1118 p_sys->i_current_program = i_int;
1119 SetPrgFilter( p_demux, p_sys->i_current_program, true );
1121 else if( i_int < 0 )
1123 p_sys->i_current_program = -1;
1124 p_sys->programs_list.i_count = 0;
1127 vlc_list_t *p_dst = &p_sys->programs_list;
1128 free( p_dst->p_values );
1130 p_dst->p_values = calloc( p_list->i_count,
1131 sizeof(*p_dst->p_values) );
1132 if( p_dst->p_values )
1134 p_dst->i_count = p_list->i_count;
1135 for( int i = 0; i < p_list->i_count; i++ )
1137 p_dst->p_values[i] = p_list->p_values[i];
1138 SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
1146 case DEMUX_GET_TITLE_INFO:
1148 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1149 int *c = va_arg( args, int * );
1151 *va_arg( args, int* ) = 0; /* Title offset */
1152 *va_arg( args, int* ) = 0; /* Chapter offset */
1153 return stream_Control( p_demux->s, STREAM_GET_TITLE_INFO, v, c );
1156 case DEMUX_SET_TITLE:
1157 return stream_vaControl( p_demux->s, STREAM_SET_TITLE, args );
1159 case DEMUX_SET_SEEKPOINT:
1160 return stream_vaControl( p_demux->s, STREAM_SET_SEEKPOINT, args );
1162 case DEMUX_GET_META:
1163 return stream_vaControl( p_demux->s, STREAM_GET_META, args );
1165 case DEMUX_CAN_RECORD:
1166 pb_bool = (bool*)va_arg( args, bool * );
1170 case DEMUX_SET_RECORD_STATE:
1171 b_bool = (bool)va_arg( args, int );
1174 stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
1175 p_sys->b_start_record = b_bool;
1178 case DEMUX_GET_SIGNAL:
1179 return stream_vaControl( p_demux->s, STREAM_GET_SIGNAL, args );
1182 return VLC_EGENERIC;
1186 /*****************************************************************************
1188 *****************************************************************************/
1189 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1191 demux_sys_t *p_sys = p_demux->p_sys;
1192 char *psz_dup = strdup( psz_fmt );
1193 char *psz = psz_dup;
1196 ts_prg_psi_t *prg = NULL;
1202 i_pid = strtol( psz, &psz, 0 );
1203 if( i_pid < 2 || i_pid >= 8192 )
1206 /* Parse optional program number */
1209 i_number = strtol( &psz[1], &psz, 0 );
1212 ts_pid_t *pmt = &p_sys->pid[i_pid];
1214 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1215 PIDInit( pmt, true, NULL );
1218 prg = calloc( 1, sizeof( ts_prg_psi_t ) );
1222 prg->i_pid_pcr = -1;
1223 prg->i_pid_pmt = -1;
1224 prg->i_version = -1;
1225 prg->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1226 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1227 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
1230 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
1231 if( !dvbpsi_pmt_attach( prg->handle,
1232 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1233 PMTCallBack, p_demux ) )
1235 dvbpsi_delete( prg->handle );
1240 prg->handle = dvbpsi_AttachPMT(
1241 ((i_number != TS_USER_PMT_NUMBER) ? i_number : 1),
1242 PMTCallBack, p_demux );
1244 TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1246 psz = strchr( psz, '=' );
1249 while( psz && *psz )
1251 char *psz_next = strchr( psz, ',' );
1257 i_pid = strtol( psz, &psz, 0 );
1258 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1261 char *psz_opt = &psz[1];
1262 if( !strcmp( psz_opt, "pcr" ) )
1264 prg->i_pid_pcr = i_pid;
1266 else if( !p_sys->pid[i_pid].b_valid )
1268 ts_pid_t *pid = &p_sys->pid[i_pid];
1270 char *psz_arg = strchr( psz_opt, '=' );
1274 PIDInit( pid, false, pmt->psi);
1275 if( prg->i_pid_pcr <= 0 )
1276 prg->i_pid_pcr = i_pid;
1278 if( psz_arg && strlen( psz_arg ) == 4 )
1280 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1281 psz_arg[2], psz_arg[3] );
1282 int i_cat = UNKNOWN_ES;
1283 es_format_t *fmt = &pid->es->fmt;
1285 if( !strcmp( psz_opt, "video" ) )
1287 else if( !strcmp( psz_opt, "audio" ) )
1289 else if( !strcmp( psz_opt, "spu" ) )
1292 es_format_Init( fmt, i_cat, i_codec );
1293 fmt->b_packetized = false;
1297 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1298 PIDFillFormat( &pid->es->fmt, i_stream_type );
1300 pid->es->fmt.i_group = i_number;
1301 if( p_sys->b_es_id_pid )
1302 pid->es->fmt.i_id = i_pid;
1304 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1306 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1307 (char*)&pid->es->fmt.i_codec );
1308 pid->es->id = es_out_Add( p_demux->out,
1318 p_sys->b_user_pmt = true;
1319 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1326 return VLC_EGENERIC;
1329 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1331 demux_sys_t *p_sys = p_demux->p_sys;
1333 if( !p_sys->b_access_control )
1334 return VLC_EGENERIC;
1336 return stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_STATE,
1337 i_pid, b_selected );
1340 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1342 demux_sys_t *p_sys = p_demux->p_sys;
1343 ts_prg_psi_t *p_prg = NULL;
1346 /* Search pmt to be unselected */
1347 for( int i = 0; i < p_sys->i_pmt; i++ )
1349 ts_pid_t *pmt = p_sys->pmt[i];
1351 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1353 if( pmt->psi->prg[i_prg]->i_number == i_prg_id )
1355 i_pmt_pid = p_sys->pmt[i]->i_pid;
1356 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1363 if( i_pmt_pid <= 0 )
1367 SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1368 if( p_prg->i_pid_pcr > 0 )
1369 SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
1372 for( int i = 2; i < 8192; i++ )
1374 ts_pid_t *pid = &p_sys->pid[i];
1376 if( !pid->b_valid || pid->psi )
1379 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1381 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1383 /* We only remove/select es that aren't defined by extra pmt */
1384 SetPIDFilter( p_demux, i, b_selected );
1391 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1393 bool b_old_valid = pid->b_valid;
1395 pid->b_valid = true;
1397 pid->b_scrambled = false;
1398 pid->p_owner = p_owner;
1399 pid->i_owner_number = 0;
1401 TAB_INIT( pid->i_extra_es, pid->extra_es );
1409 pid->psi = xmalloc( sizeof( ts_psi_t ) );
1410 pid->psi->handle = NULL;
1411 TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1415 pid->psi->i_pat_version = -1;
1416 pid->psi->i_sdt_version = -1;
1419 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1423 prg->i_version = -1;
1425 prg->i_pid_pcr = -1;
1426 prg->i_pid_pmt = -1;
1427 prg->i_pcr_value= -1;
1431 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1437 pid->es = calloc( 1, sizeof( ts_es_t ) );
1441 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1442 pid->es->data_type = TS_ES_DATA_PES;
1443 pid->es->pp_last = &pid->es->p_data;
1447 static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
1449 demux_sys_t *p_sys = p_demux->p_sys;
1450 es_out_t *out = p_demux->out;
1454 if( pid->psi->handle )
1456 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1457 if( dvbpsi_decoder_present( pid->psi->handle ) )
1458 dvbpsi_pmt_detach( pid->psi->handle );
1459 dvbpsi_delete( pid->psi->handle );
1460 pid->psi->handle = NULL;
1462 dvbpsi_DetachPMT( pid->psi->handle );
1465 for( int i = 0; i < pid->psi->i_prg; i++ )
1467 if( pid->psi->prg[i]->iod )
1468 IODFree( pid->psi->prg[i]->iod );
1469 if( pid->psi->prg[i]->handle )
1471 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1472 if( dvbpsi_decoder_present( pid->psi->prg[i]->handle ) )
1473 dvbpsi_pmt_detach( pid->psi->prg[i]->handle );
1474 dvbpsi_delete( pid->psi->prg[i]->handle );
1476 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1479 free( pid->psi->prg[i] );
1481 free( pid->psi->prg );
1488 es_out_Del( out, pid->es->id );
1492 if( pid->es->p_data )
1493 block_ChainRelease( pid->es->p_data );
1495 es_format_Clean( &pid->es->fmt );
1499 for( int i = 0; i < pid->i_extra_es; i++ )
1501 if( pid->extra_es[i]->id )
1503 es_out_Del( out, pid->extra_es[i]->id );
1507 if( pid->extra_es[i]->p_data )
1508 block_ChainRelease( pid->extra_es[i]->p_data );
1510 es_format_Clean( &pid->extra_es[i]->fmt );
1512 free( pid->extra_es[i] );
1514 if( pid->i_extra_es )
1515 free( pid->extra_es );
1518 pid->b_valid = false;
1521 /****************************************************************************
1523 ****************************************************************************/
1524 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
1526 demux_sys_t *p_sys = p_demux->p_sys;
1528 unsigned i_pes_size = 0;
1529 unsigned i_skip = 0;
1532 mtime_t i_length = 0;
1534 /* FIXME find real max size */
1535 /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
1537 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1539 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
1540 header[0], header[1],header[2],header[3], pid->i_pid );
1541 block_ChainRelease( p_pes );
1545 /* TODO check size */
1548 case 0xBC: /* Program stream map */
1549 case 0xBE: /* Padding */
1550 case 0xBF: /* Private stream 2 */
1551 case 0xF0: /* ECM */
1552 case 0xF1: /* EMM */
1553 case 0xFF: /* Program stream directory */
1554 case 0xF2: /* DSMCC stream */
1555 case 0xF8: /* ITU-T H.222.1 type E stream */
1559 if( ( header[6]&0xC0 ) == 0x80 )
1562 i_skip = header[8] + 9;
1564 if( header[7]&0x80 ) /* has pts */
1566 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1567 (mtime_t)(header[10] << 22)|
1568 ((mtime_t)(header[11]&0xfe) << 14)|
1569 (mtime_t)(header[12] << 7)|
1570 (mtime_t)(header[13] >> 1);
1572 if( header[7]&0x40 ) /* has dts */
1574 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1575 (mtime_t)(header[15] << 22)|
1576 ((mtime_t)(header[16]&0xfe) << 14)|
1577 (mtime_t)(header[17] << 7)|
1578 (mtime_t)(header[18] >> 1);
1585 while( i_skip < 23 && header[i_skip] == 0xff )
1591 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1592 block_ChainRelease( p_pes );
1595 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1600 if( header[i_skip]&0x20 )
1602 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1603 (mtime_t)(header[i_skip+1] << 22)|
1604 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1605 (mtime_t)(header[i_skip+3] << 7)|
1606 (mtime_t)(header[i_skip+4] >> 1);
1608 if( header[i_skip]&0x10 ) /* has dts */
1610 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1611 (mtime_t)(header[i_skip+6] << 22)|
1612 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1613 (mtime_t)(header[i_skip+8] << 7)|
1614 (mtime_t)(header[i_skip+9] >> 1);
1630 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1631 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1635 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1636 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1637 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1641 else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
1642 pid->es->p_mpeg4desc )
1644 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1646 if( dcd->i_extra > 2 &&
1647 dcd->p_extra[0] == 0x10 &&
1648 ( dcd->p_extra[1]&0x10 ) )
1650 /* display length */
1651 if( p_pes->i_buffer + 2 <= i_skip )
1652 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1656 if( p_pes->i_buffer + 2 <= i_skip )
1657 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1663 while( p_pes && i_skip > 0 )
1665 if( p_pes->i_buffer <= i_skip )
1667 block_t *p_next = p_pes->p_next;
1669 i_skip -= p_pes->i_buffer;
1670 block_Release( p_pes );
1675 p_pes->i_buffer -= i_skip;
1676 p_pes->p_buffer += i_skip;
1681 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1682 if( i_pts >= 0 && i_dts < 0 )
1690 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
1693 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
1695 p_pes->i_length = i_length * 100 / 9;
1697 p_block = block_ChainGather( p_pes );
1698 if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
1700 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1702 p_block->i_buffer = i_pes_size;
1705 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1708 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1710 else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
1712 if( p_block->i_pts <= VLC_TS_INVALID )
1714 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
1715 * In this case use the last PCR + 40ms */
1716 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
1718 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
1720 mtime_t i_pcr = pid->p_owner->prg[i]->i_pcr_value;
1721 if( i_pcr > VLC_TS_INVALID )
1722 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
1729 for( int i = 0; i < pid->i_extra_es; i++ )
1731 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1732 block_Duplicate( p_block ) );
1735 if (!p_sys->b_trust_pcr)
1736 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1737 pid->i_owner_number, p_block->i_pts);
1739 es_out_Send( p_demux->out, pid->es->id, p_block );
1743 msg_Warn( p_demux, "empty pes" );
1747 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
1749 block_t *p_content = block_ChainGather( p_data );
1750 mtime_t i_date = -1;
1751 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
1753 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
1755 i_date = pid->p_owner->prg[i]->i_pcr_value;
1762 if( pid->es->fmt.i_codec == VLC_CODEC_SCTE_27 )
1764 /* We need to extract the truncated pts stored inside the payload */
1765 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
1768 size_t i_offset = 4;
1769 if( p_content->p_buffer[3] & 0x40 )
1771 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
1772 p_content->p_buffer[8];
1775 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
1777 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
1780 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
1781 if( i_display_in < i_date )
1782 i_date = i_display_in + (1ll << 32);
1784 i_date = i_display_in;
1791 p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
1793 es_out_Send( p_demux->out, pid->es->id, p_content );
1795 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
1797 block_t *p_data = pid->es->p_data;
1799 /* remove the pes from pid */
1800 pid->es->p_data = NULL;
1801 pid->es->i_data_size = 0;
1802 pid->es->i_data_gathered = 0;
1803 pid->es->pp_last = &pid->es->p_data;
1805 if( pid->es->data_type == TS_ES_DATA_PES )
1807 ParsePES( p_demux, pid, p_data );
1809 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
1811 ParseTableSection( p_demux, pid, p_data );
1815 block_ChainRelease( p_data );
1819 static block_t* ReadTSPacket( demux_t *p_demux )
1821 demux_sys_t *p_sys = p_demux->p_sys;
1825 /* Get a new TS packet */
1826 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1828 msg_Dbg( p_demux, "eof ?" );
1832 /* Skip header (BluRay streams).
1833 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
1834 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
1836 p_pkt->p_buffer += p_sys->i_packet_header_size;
1837 p_pkt->i_buffer -= p_sys->i_packet_header_size;
1839 /* Check sync byte and re-sync if needed */
1840 if( p_pkt->p_buffer[0] != 0x47 )
1842 msg_Warn( p_demux, "lost synchro" );
1843 block_Release( p_pkt );
1844 while( vlc_object_alive (p_demux) )
1846 const uint8_t *p_peek;
1847 int i_peek, i_skip = 0;
1849 i_peek = stream_Peek( p_demux->s, &p_peek,
1850 p_sys->i_packet_size * 10 );
1851 if( i_peek < p_sys->i_packet_size + 1 )
1853 msg_Dbg( p_demux, "eof ?" );
1857 while( i_skip < i_peek - p_sys->i_packet_size )
1859 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
1860 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
1866 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
1867 stream_Read( p_demux->s, NULL, i_skip );
1869 if( i_skip < i_peek - p_sys->i_packet_size )
1874 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1876 msg_Dbg( p_demux, "eof ?" );
1883 static mtime_t AdjustPCRWrapAround( demux_t *p_demux, mtime_t i_pcr )
1885 demux_sys_t *p_sys = p_demux->p_sys;
1887 * PCR is 33bit. If PCR reaches to 0x1FFFFFFFF (26:30:43.717), ressets from 0.
1888 * So, need to add 0x1FFFFFFFF, for calculating duration or current position.
1890 mtime_t i_adjust = 0;
1891 int64_t i_pos = stream_Tell( p_demux->s );
1893 for( i = 1; i < p_sys->i_pcrs_num && p_sys->p_pos[i] <= i_pos; ++i )
1895 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
1896 i_adjust += 0x1FFFFFFFF;
1898 if( p_sys->p_pcrs[i-1] > i_pcr )
1899 i_adjust += 0x1FFFFFFFF;
1901 return i_pcr + i_adjust;
1904 static mtime_t GetPCR( block_t *p_pkt )
1906 const uint8_t *p = p_pkt->p_buffer;
1910 if( ( p[3]&0x20 ) && /* adaptation */
1914 /* PCR is 33 bits */
1915 i_pcr = ( (mtime_t)p[6] << 25 ) |
1916 ( (mtime_t)p[7] << 17 ) |
1917 ( (mtime_t)p[8] << 9 ) |
1918 ( (mtime_t)p[9] << 1 ) |
1919 ( (mtime_t)p[10] >> 7 );
1924 static int SeekToPCR( demux_t *p_demux, int64_t i_pos )
1926 demux_sys_t *p_sys = p_demux->p_sys;
1929 const int64_t i_initial_pos = stream_Tell( p_demux->s );
1932 return VLC_EGENERIC;
1934 int64_t i_last_pos = stream_Size( p_demux->s ) - p_sys->i_packet_size;
1935 if( i_pos > i_last_pos )
1938 if( stream_Seek( p_demux->s, i_pos ) )
1939 return VLC_EGENERIC;
1941 while( vlc_object_alive( p_demux ) )
1945 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
1949 if( PIDGet( p_pkt ) == p_sys->i_pid_ref_pcr )
1951 i_pcr = GetPCR( p_pkt );
1953 block_Release( p_pkt );
1956 if( stream_Tell( p_demux->s ) >= i_last_pos )
1961 stream_Seek( p_demux->s, i_initial_pos );
1962 assert( i_initial_pos == stream_Tell( p_demux->s ) );
1963 return VLC_EGENERIC;
1966 p_sys->i_current_pcr = i_pcr;
1970 static int Seek( demux_t *p_demux, double f_percent )
1972 demux_sys_t *p_sys = p_demux->p_sys;
1974 int64_t i_initial_pos = stream_Tell( p_demux->s );
1975 mtime_t i_initial_pcr = p_sys->i_current_pcr;
1978 * Find the time position by using binary search algorithm.
1980 mtime_t i_target_pcr = (p_sys->i_last_pcr - p_sys->i_first_pcr) * f_percent + p_sys->i_first_pcr;
1982 int64_t i_head_pos = 0;
1985 mtime_t i_adjust = 0;
1987 for( i = 1; i < p_sys->i_pcrs_num; ++i )
1989 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
1990 i_adjust += 0x1FFFFFFFF;
1991 if( p_sys->p_pcrs[i] + i_adjust > i_target_pcr )
1994 i_head_pos = p_sys->p_pos[i-1];
1995 i_tail_pos = ( i < p_sys->i_pcrs_num ) ? p_sys->p_pos[i] : stream_Size( p_demux->s );
1997 msg_Dbg( p_demux, "Seek():i_head_pos:%"PRId64", i_tail_pos:%"PRId64, i_head_pos, i_tail_pos);
1999 bool b_found = false;
2001 while( i_head_pos <= i_tail_pos )
2003 /* Round i_pos to a multiple of p_sys->i_packet_size */
2004 int64_t i_pos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2005 int64_t i_div = i_pos % p_sys->i_packet_size;
2007 if( SeekToPCR( p_demux, i_pos ) )
2009 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2010 int64_t i_diff_msec = (p_sys->i_current_pcr - i_target_pcr) * 100 / 9 / 1000;
2011 if( i_diff_msec > 500 )
2013 i_tail_pos = i_pos - p_sys->i_packet_size;
2015 else if( i_diff_msec < -500 )
2017 i_head_pos = i_pos + p_sys->i_packet_size;
2021 // diff time <= 500msec
2029 msg_Dbg( p_demux, "Seek():cannot find a time position. i_cnt:%d", i_cnt );
2030 stream_Seek( p_demux->s, i_initial_pos );
2031 p_sys->i_current_pcr = i_initial_pcr;
2032 return VLC_EGENERIC;
2036 msg_Dbg( p_demux, "Seek():can find a time position. i_cnt:%d", i_cnt );
2041 static void GetFirstPCR( demux_t *p_demux )
2043 demux_sys_t *p_sys = p_demux->p_sys;
2045 int64_t i_initial_pos = stream_Tell( p_demux->s );
2047 if( stream_Seek( p_demux->s, 0 ) )
2050 while( vlc_object_alive (p_demux) )
2054 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2058 mtime_t i_pcr = GetPCR( p_pkt );
2061 p_sys->i_pid_ref_pcr = PIDGet( p_pkt );
2062 p_sys->i_first_pcr = i_pcr;
2063 p_sys->i_current_pcr = i_pcr;
2065 block_Release( p_pkt );
2066 if( p_sys->i_first_pcr >= 0 )
2069 stream_Seek( p_demux->s, i_initial_pos );
2072 static void GetLastPCR( demux_t *p_demux )
2074 demux_sys_t *p_sys = p_demux->p_sys;
2076 const int64_t i_initial_pos = stream_Tell( p_demux->s );
2077 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2079 int64_t i_stream_size = stream_Size( p_demux->s );
2080 int64_t i_last_pos = i_stream_size - p_sys->i_packet_size;
2081 /* Round i_pos to a multiple of p_sys->i_packet_size */
2082 int64_t i_pos = i_last_pos - p_sys->i_packet_size * 4500; /* FIXME if the value is not reasonable, please change it. */
2083 int64_t i_div = i_pos % p_sys->i_packet_size;
2086 if( i_pos <= i_initial_pos && i_pos >= i_stream_size )
2087 i_pos = i_initial_pos + p_sys->i_packet_size;
2088 if( i_pos < 0 && i_pos >= i_stream_size )
2091 while( vlc_object_alive( p_demux ) )
2093 if( SeekToPCR( p_demux, i_pos ) )
2095 p_sys->i_last_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2096 if( ( i_pos = stream_Tell( p_demux->s ) ) >= i_last_pos )
2099 if( p_sys->i_last_pcr >= 0 )
2101 int64_t i_size = stream_Size( p_demux->s );
2102 mtime_t i_duration_msec = ( p_sys->i_last_pcr - p_sys->i_first_pcr ) * 100 / 9 / 1000;
2103 int64_t i_rate = ( i_size < 0 || i_duration_msec <= 0 ) ? 0 : i_size * 1000 * 8 / i_duration_msec;
2104 const int64_t TS_SUPPOSED_MAXRATE = 55 * 1000 * 1000; //FIXME I think it's enough.
2105 const int64_t TS_SUPPOSED_MINRATE = 0.5 * 1000 * 1000; //FIXME
2106 if( i_rate < TS_SUPPOSED_MINRATE || i_rate > TS_SUPPOSED_MAXRATE )
2108 msg_Dbg( p_demux, "calculated bitrate (%"PRId64"bit/s) is too low or too high. min bitrate (%"PRId64"bit/s) max bitrate (%"PRId64"bit/s)",
2109 i_rate, TS_SUPPOSED_MINRATE, TS_SUPPOSED_MAXRATE );
2110 p_sys->i_last_pcr = -1;
2113 stream_Seek( p_demux->s, i_initial_pos );
2114 assert( i_initial_pos == stream_Tell( p_demux->s ) );
2115 p_sys->i_current_pcr = i_initial_pcr;
2118 static void CheckPCR( demux_t *p_demux )
2120 demux_sys_t *p_sys = p_demux->p_sys;
2122 int64_t i_initial_pos = stream_Tell( p_demux->s );
2123 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2125 int64_t i_size = stream_Size( p_demux->s );
2128 p_sys->p_pcrs[0] = p_sys->i_first_pcr;
2129 p_sys->p_pos[0] = i_initial_pos;
2131 for( i = 1; i < p_sys->i_pcrs_num && vlc_object_alive( p_demux ); ++i )
2133 /* Round i_pos to a multiple of p_sys->i_packet_size */
2134 int64_t i_pos = i_size / p_sys->i_pcrs_num * i;
2135 int64_t i_div = i_pos % p_sys->i_packet_size;
2137 if( SeekToPCR( p_demux, i_pos ) )
2139 p_sys->p_pcrs[i] = p_sys->i_current_pcr;
2140 p_sys->p_pos[i] = stream_Tell( p_demux->s );
2141 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2143 msg_Dbg( p_demux, "PCR Wrap Around found between %d%% and %d%% (pcr:%"PRId64"(0x%09"PRIx64") pcr:%"PRId64"(0x%09"PRIx64"))",
2144 (int)((i-1)*100/p_sys->i_pcrs_num), (int)(i*100/p_sys->i_pcrs_num), p_sys->p_pcrs[i-1], p_sys->p_pcrs[i-1], p_sys->p_pcrs[i], p_sys->p_pcrs[i] );
2147 if( i < p_sys->i_pcrs_num )
2149 msg_Dbg( p_demux, "Force Seek Per Percent: Seeking failed at %d%%.", (int)(i*100/p_sys->i_pcrs_num) );
2150 p_sys->b_force_seek_per_percent = true;
2153 stream_Seek( p_demux->s, i_initial_pos );
2154 p_sys->i_current_pcr = i_initial_pcr;
2157 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2159 demux_sys_t *p_sys = p_demux->p_sys;
2161 if( p_sys->i_pmt_es <= 0 )
2164 mtime_t i_pcr = GetPCR( p_bk );
2168 if( p_sys->i_pid_ref_pcr == pid->i_pid )
2169 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
2171 /* Search program and set the PCR */
2173 for( int i = 0; i < p_sys->i_pmt && i_group < 0 ; i++ )
2175 bool b_pmt_has_es = false;
2177 for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2179 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
2181 /* We've found our target group */
2182 p_sys->pmt[i]->psi->prg[i_prg]->i_pcr_value = i_pcr;
2183 i_group = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
2184 for( int j = 0; j < 8192; j++ )
2186 const ts_pid_t *pid = &p_sys->pid[j];
2187 if( pid->b_valid && pid->p_owner == p_sys->pmt[i]->psi && pid->es )
2189 b_pmt_has_es = true;
2196 if ( p_sys->b_trust_pcr && i_group > 0 && b_pmt_has_es )
2197 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2198 i_group, VLC_TS_0 + i_pcr * 100 / 9 );
2202 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2204 const uint8_t *p = p_bk->p_buffer;
2205 const bool b_unit_start = p[1]&0x40;
2206 const bool b_scrambled = p[3]&0x80;
2207 const bool b_adaptation = p[3]&0x20;
2208 const bool b_payload = p[3]&0x10;
2209 const int i_cc = p[3]&0x0f; /* continuity counter */
2210 bool b_discontinuity = false; /* discontinuity */
2212 /* transport_scrambling_control is ignored */
2217 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
2218 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
2222 /* For now, ignore additional error correction
2223 * TODO: handle Reed-Solomon 204,188 error correction */
2224 p_bk->i_buffer = TS_PACKET_SIZE_188;
2228 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
2230 if( pid->es->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
2231 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2234 if( p_demux->p_sys->csa )
2236 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
2237 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
2238 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
2243 /* We don't have any adaptation_field, so payload starts
2244 * immediately after the 4 byte TS header */
2249 /* p[4] is adaptation_field_length minus one */
2253 /* discontinuity indicator found in stream */
2254 b_discontinuity = (p[5]&0x80) ? true : false;
2255 if( b_discontinuity && pid->es->p_data )
2257 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
2259 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
2263 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
2268 /* Test continuity counter */
2269 /* continuous when (one of this):
2271 * diff == 0 and payload == 0
2272 * diff == 0 and duplicate packet (playload != 0) <- should we
2273 * test the content ?
2275 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
2276 if( b_payload && i_diff == 1 )
2278 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
2282 if( pid->i_cc == 0xff )
2284 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
2288 else if( i_diff != 0 && !b_discontinuity )
2290 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
2291 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
2294 if( pid->es->p_data && pid->es->fmt.i_cat != VIDEO_ES )
2296 /* Small video artifacts are usually better than
2297 * dropping full frames */
2298 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2303 PCRHandle( p_demux, pid, p_bk );
2305 if( i_skip >= 188 || pid->es->id == NULL )
2307 block_Release( p_bk );
2312 if( !pid->b_scrambled != !b_scrambled )
2314 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2315 pid->i_pid, pid->b_scrambled, b_scrambled );
2317 pid->b_scrambled = b_scrambled;
2319 for( int i = 0; i < pid->i_extra_es; i++ )
2321 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2322 pid->extra_es[i]->id, b_scrambled );
2324 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2325 pid->es->id, b_scrambled );
2328 /* We have to gather it */
2329 p_bk->p_buffer += i_skip;
2330 p_bk->i_buffer -= i_skip;
2334 if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
2336 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
2337 block_t *p = block_Duplicate( p_bk );
2340 p->i_buffer = i_pointer_field;
2342 block_ChainLastAppend( &pid->es->pp_last, p );
2344 p_bk->i_buffer -= 1 + i_pointer_field;
2345 p_bk->p_buffer += 1 + i_pointer_field;
2347 if( pid->es->p_data )
2349 ParseData( p_demux, pid );
2353 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2354 if( pid->es->data_type == TS_ES_DATA_PES )
2356 if( p_bk->i_buffer > 6 )
2358 pid->es->i_data_size = GetWBE( &p_bk->p_buffer[4] );
2359 if( pid->es->i_data_size > 0 )
2361 pid->es->i_data_size += 6;
2365 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
2367 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
2369 pid->es->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
2372 pid->es->i_data_gathered += p_bk->i_buffer;
2373 if( pid->es->i_data_size > 0 &&
2374 pid->es->i_data_gathered >= pid->es->i_data_size )
2376 ParseData( p_demux, pid );
2382 if( pid->es->p_data == NULL )
2384 /* msg_Dbg( p_demux, "broken packet" ); */
2385 block_Release( p_bk );
2389 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2390 pid->es->i_data_gathered += p_bk->i_buffer;
2392 if( pid->es->i_data_size > 0 &&
2393 pid->es->i_data_gathered >= pid->es->i_data_size )
2395 ParseData( p_demux, pid );
2404 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
2406 switch( i_stream_type )
2408 case 0x01: /* MPEG-1 video */
2409 case 0x02: /* MPEG-2 video */
2410 case 0x80: /* MPEG-2 MOTO video */
2411 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
2413 case 0x03: /* MPEG-1 audio */
2414 case 0x04: /* MPEG-2 audio */
2415 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
2417 case 0x11: /* MPEG4 (audio) LATM */
2418 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
2419 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
2420 transport syntax, such as DST, ALS and SLS */
2421 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
2423 case 0x10: /* MPEG4 (video) */
2424 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
2426 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
2427 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
2429 case 0x24: /* HEVC */
2430 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
2432 case 0x42: /* CAVS (Chinese AVS) */
2433 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
2436 case 0x81: /* A52 (audio) */
2437 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
2439 case 0x82: /* SCTE-27 (sub) */
2440 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
2442 case 0x84: /* SDDS (audio) */
2443 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
2445 case 0x85: /* DTS (audio) */
2446 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
2448 case 0x87: /* E-AC3 */
2449 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
2452 case 0x91: /* A52 vls (audio) */
2453 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
2455 case 0x92: /* DVD_SPU vls (sub) */
2456 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
2459 case 0x94: /* SDDS (audio) */
2460 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
2463 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
2464 es_format_Init( fmt, UNKNOWN_ES, 0 );
2467 case 0x06: /* PES_PRIVATE (fixed later) */
2468 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
2469 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
2471 es_format_Init( fmt, UNKNOWN_ES, 0 );
2475 /* PES packets usually contain truncated frames */
2476 fmt->b_packetized = false;
2479 /*****************************************************************************
2480 * MP4 specific functions (IOD parser)
2481 *****************************************************************************/
2482 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
2485 unsigned int i_len = 0;
2491 i_len = ( i_len << 7 ) + ( i_b&0x7f );
2493 } while( i_b&0x80 && *pi_data > 0 );
2495 if (i_len > *pi_data)
2501 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
2504 while( *pi_data > 0 && bytes-- )
2515 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
2517 int len = IODGetBytes( pi_data, pp_data, 1 );
2520 char *url = strndup( (char*)*pp_data, len );
2526 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2528 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
2530 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
2539 byte1 = IODGetBytes( &i_data, &p_data, 1 );
2540 byte2 = IODGetBytes( &i_data, &p_data, 1 );
2541 byte3 = IODGetBytes( &i_data, &p_data, 1 );
2542 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2544 i_iod_label = byte1;
2547 else //correct implementation of the IOD_descriptor
2549 i_iod_label = byte2;
2553 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
2555 if( i_iod_tag != 0x02 )
2557 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
2561 IODDescriptorLength( &i_data, &p_data );
2563 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
2564 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
2565 i_od_id |= i_flags >> 6;
2566 ts_debug( "\n* od_id:%d", i_od_id );
2567 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2568 if ((i_flags >> 5) & 0x01)
2570 p_iod->psz_url = IODGetURL( &i_data, &p_data );
2571 ts_debug( "\n* url string:%s", p_iod->psz_url );
2572 ts_debug( "\n*****************************\n" );
2577 p_iod->psz_url = NULL;
2580 /* Profile Level Indication */
2581 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
2582 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
2583 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
2584 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
2585 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
2588 int i_data_sav = i_data;
2589 uint8_t *p_data_sav = p_data;
2590 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
2592 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
2594 p_data = p_data_sav + i_length;
2595 i_data = i_data_sav - i_length;
2597 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
2598 i_length = IODDescriptorLength( &i_data, &p_data );
2600 i_data_sav = i_data;
2601 p_data_sav = p_data;
2605 if ( i_tag != 0x03 )
2607 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
2611 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
2612 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
2613 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2614 if( b_streamDependenceFlag )
2615 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
2617 if( (i_flags >> 6) & 0x01 )
2618 es_descr->psz_url = IODGetURL( &i_data, &p_data );
2620 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2621 if( b_OCRStreamFlag )
2622 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
2624 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
2626 ts_debug( "\n* ERR missing DecoderConfigDescr" );
2629 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
2630 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
2631 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
2632 i_flags = IODGetBytes( &i_data, &p_data, 1 );
2633 dec_descr->i_streamType = i_flags >> 2;
2635 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
2636 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
2637 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
2639 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
2641 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
2642 if( dec_descr->i_extra > 0 )
2644 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
2645 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
2646 p_data += dec_descr->i_extra;
2647 i_data -= dec_descr->i_extra;
2652 dec_descr->i_extra = 0;
2653 dec_descr->p_extra = NULL;
2656 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
2658 ts_debug( "\n* ERR missing SLConfigDescr" );
2661 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
2662 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
2665 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
2670 es_descr->b_ok = true;
2676 static void IODFree( iod_descriptor_t *p_iod )
2678 if( p_iod->psz_url )
2680 free( p_iod->psz_url );
2685 for( int i = 0; i < 255; i++ )
2687 #define es_descr p_iod->es_descr[i]
2690 if( es_descr.psz_url )
2691 free( es_descr.psz_url );
2693 free( es_descr.dec_descr.p_extra );
2700 /****************************************************************************
2701 ****************************************************************************
2702 ** libdvbpsi callbacks
2703 ****************************************************************************
2704 ****************************************************************************/
2705 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2707 demux_sys_t *p_sys = p_demux->p_sys;
2709 if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
2710 p_sys->i_current_program == 0 )
2712 if( p_sys->i_current_program == i_pgrm )
2715 if( p_sys->programs_list.i_count != 0 )
2717 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
2719 if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
2726 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
2728 demux_sys_t *p_sys = p_demux->p_sys;
2730 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
2733 msg_Warn( p_demux, "Switching to non DVB mode" );
2735 /* This doesn't look like a DVB stream so don't try
2736 * parsing the SDT/EDT/TDT */
2738 for( int i = 0x11; i <= 0x14; i++ )
2740 if( i == 0x13 ) continue;
2741 ts_pid_t *p_pid = &p_sys->pid[i];
2745 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2746 if( dvbpsi_decoder_present( p_pid->psi->handle ))
2747 dvbpsi_DetachDemux( p_pid->psi->handle );
2748 dvbpsi_delete( p_pid->psi->handle );
2750 dvbpsi_DetachDemux( p_pid->psi->handle );
2754 p_pid->b_valid = false;
2756 SetPIDFilter( p_demux, i, false );
2758 p_sys->b_dvb_meta = false;
2761 #include "dvb-text.h"
2763 static char *EITConvertToUTF8( const unsigned char *psz_instring,
2767 /* Deal with no longer broken providers (no switch byte
2768 but sending ISO_8859-1 instead of ISO_6937) without
2769 removing them from the broken providers table
2770 (keep the entry for correctly handling recorded TS).
2772 b_broken = b_broken && i_length && *psz_instring > 0x20;
2775 return FromCharset( "ISO_8859-1", psz_instring, i_length );
2776 return vlc_from_EIT( psz_instring, i_length );
2779 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2781 demux_sys_t *p_sys = p_demux->p_sys;
2782 ts_pid_t *sdt = &p_sys->pid[0x11];
2783 dvbpsi_sdt_service_t *p_srv;
2785 msg_Dbg( p_demux, "SDTCallBack called" );
2787 if( sdt->psi->i_sdt_version != -1 &&
2788 ( !p_sdt->b_current_next ||
2789 p_sdt->i_version == sdt->psi->i_sdt_version ) )
2791 dvbpsi_DeleteSDT( p_sdt );
2795 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2797 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2802 p_sdt->i_version, p_sdt->b_current_next,
2803 p_sdt->i_network_id );
2805 p_sys->b_broken_charset = false;
2807 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2810 dvbpsi_descriptor_t *p_dr;
2812 const char *psz_type = NULL;
2813 const char *psz_status = NULL;
2815 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
2816 "running=%d free_ca=%d",
2817 p_srv->i_service_id, p_srv->b_eit_schedule,
2818 p_srv->b_eit_present, p_srv->i_running_status,
2821 p_meta = vlc_meta_New();
2822 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2824 if( p_dr->i_tag == 0x48 )
2826 static const char *ppsz_type[17] = {
2828 "Digital television service",
2829 "Digital radio sound service",
2831 "NVOD reference service",
2832 "NVOD time-shifted service",
2835 "SECAM coded signal",
2838 "NTSC coded signal",
2839 "Data broadcast service",
2840 "Reserved for Common Interface Usage",
2841 "RCS Map (see EN 301 790 [35])",
2842 "RCS FLS (see EN 301 790 [35])",
2845 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2849 /* Workarounds for broadcasters with broken EPG */
2851 if( p_sdt->i_network_id == 133 )
2852 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
2854 /* List of providers using ISO8859-1 */
2855 static const char ppsz_broken_providers[][8] = {
2856 "CSAT", /* CanalSat FR */
2857 "GR1", /* France televisions */
2859 "MR5", /* France 2/M6 HD */
2862 for( int i = 0; *ppsz_broken_providers[i]; i++ )
2864 const size_t i_length = strlen(ppsz_broken_providers[i]);
2865 if( pD->i_service_provider_name_length == i_length &&
2866 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
2867 p_sys->b_broken_charset = true;
2870 /* FIXME: Digital+ ES also uses ISO8859-1 */
2872 str1 = EITConvertToUTF8(pD->i_service_provider_name,
2873 pD->i_service_provider_name_length,
2874 p_sys->b_broken_charset );
2875 str2 = EITConvertToUTF8(pD->i_service_name,
2876 pD->i_service_name_length,
2877 p_sys->b_broken_charset );
2879 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
2880 pD->i_service_type, str1, str2 );
2882 vlc_meta_SetTitle( p_meta, str2 );
2883 vlc_meta_SetPublisher( p_meta, str1 );
2884 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2885 psz_type = ppsz_type[pD->i_service_type];
2891 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
2893 static const char *ppsz_status[5] = {
2896 "Starts in a few seconds",
2900 psz_status = ppsz_status[p_srv->i_running_status];
2904 vlc_meta_AddExtra( p_meta, "Type", psz_type );
2906 vlc_meta_AddExtra( p_meta, "Status", psz_status );
2908 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2909 p_srv->i_service_id, p_meta );
2910 vlc_meta_Delete( p_meta );
2913 sdt->psi->i_sdt_version = p_sdt->i_version;
2914 dvbpsi_DeleteSDT( p_sdt );
2917 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
2918 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
2920 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
2924 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
2925 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
2928 /* Count the number of days */
2929 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
2930 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
2931 for( int i = 70; i < i_year; i++ )
2932 i_day += LEAP(1900+i);
2934 i_day += LEAP(1900+i_year);
2937 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
2940 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2942 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2943 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2944 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
2946 *p_y = 1900 + yp + c*1;
2947 *p_m = mp - 1 - c*12;
2948 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2950 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
2951 static int64_t EITConvertStartTime( uint64_t i_date )
2953 const int i_mjd = i_date >> 24;
2954 const int i_hour = CVT_FROM_BCD(i_date >> 16);
2955 const int i_minute = CVT_FROM_BCD(i_date >> 8);
2956 const int i_second = CVT_FROM_BCD(i_date );
2961 /* if all 40 bits are 1, the start is unknown */
2962 if( i_date == UINT64_C(0xffffffffff) )
2965 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
2966 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
2968 static int EITConvertDuration( uint32_t i_duration )
2970 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
2971 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
2972 CVT_FROM_BCD(i_duration );
2976 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
2978 demux_sys_t *p_sys = p_demux->p_sys;
2980 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
2982 dvbpsi_DeleteTOT(p_tdt);
2986 static void EITCallBack( demux_t *p_demux,
2987 dvbpsi_eit_t *p_eit, bool b_current_following )
2989 demux_sys_t *p_sys = p_demux->p_sys;
2990 dvbpsi_eit_event_t *p_evt;
2993 msg_Dbg( p_demux, "EITCallBack called" );
2994 if( !p_eit->b_current_next )
2996 dvbpsi_DeleteEIT( p_eit );
3000 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3001 "ts_id=%d network_id=%d segment_last_section_number=%d "
3003 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3006 p_eit->i_service_id,
3008 p_eit->i_version, p_eit->b_current_next,
3009 p_eit->i_ts_id, p_eit->i_network_id,
3010 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3012 p_epg = vlc_epg_New( NULL );
3013 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3015 dvbpsi_descriptor_t *p_dr;
3016 char *psz_name = NULL;
3017 char *psz_text = NULL;
3018 char *psz_extra = strdup("");
3023 i_start = EITConvertStartTime( p_evt->i_start_time );
3024 i_duration = EITConvertDuration( p_evt->i_duration );
3026 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3027 "running=%d free_ca=%d",
3028 p_evt->i_event_id, (int)i_start, (int)i_duration,
3029 p_evt->i_running_status, p_evt->b_free_ca );
3031 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3033 if( p_dr->i_tag == 0x4d )
3035 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3037 /* Only take first description, as we don't handle language-info
3039 if( pE && psz_name == NULL)
3041 psz_name = EITConvertToUTF8( pE->i_event_name, pE->i_event_name_length,
3042 p_sys->b_broken_charset );
3043 psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
3044 p_sys->b_broken_charset );
3045 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3046 pE->i_iso_639_code, psz_name, psz_text );
3049 else if( p_dr->i_tag == 0x4e )
3051 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3054 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3056 pE->i_descriptor_number, pE->i_last_descriptor_number );
3058 if( pE->i_text_length > 0 )
3060 char *psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
3061 p_sys->b_broken_charset );
3064 msg_Dbg( p_demux, " - text='%s'", psz_text );
3066 psz_extra = xrealloc( psz_extra,
3067 strlen(psz_extra) + strlen(psz_text) + 1 );
3068 strcat( psz_extra, psz_text );
3073 for( int i = 0; i < pE->i_entry_count; i++ )
3075 char *psz_dsc = EITConvertToUTF8( pE->i_item_description[i],
3076 pE->i_item_description_length[i],
3077 p_sys->b_broken_charset );
3078 char *psz_itm = EITConvertToUTF8( pE->i_item[i], pE->i_item_length[i],
3079 p_sys->b_broken_charset );
3081 if( psz_dsc && psz_itm )
3083 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3085 psz_extra = xrealloc( psz_extra,
3086 strlen(psz_extra) + strlen(psz_dsc) +
3087 strlen(psz_itm) + 3 + 1 );
3088 strcat( psz_extra, "(" );
3089 strcat( psz_extra, psz_dsc );
3090 strcat( psz_extra, " " );
3091 strcat( psz_extra, psz_itm );
3092 strcat( psz_extra, ")" );
3100 else if( p_dr->i_tag == 0x55 )
3102 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
3105 for ( int i = 0; i < pR->i_ratings_number; i++ )
3107 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
3108 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
3110 if ( p_rating->i_rating + 3 > i_min_age )
3111 i_min_age = p_rating->i_rating + 3;
3112 msg_Dbg( p_demux, "..* event parental control set to %d years",
3120 msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3126 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3127 *psz_extra ? psz_extra : NULL, i_min_age );
3129 /* Update "now playing" field */
3130 if( p_evt->i_running_status == 0x04 && i_start > 0 )
3131 vlc_epg_SetCurrent( p_epg, i_start );
3138 if( p_epg->i_event > 0 )
3140 if( b_current_following &&
3141 ( p_sys->i_current_program == -1 ||
3142 p_sys->i_current_program ==
3143 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3150 p_sys->i_dvb_length = 0;
3151 p_sys->i_dvb_start = 0;
3153 if( p_epg->p_current )
3155 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
3156 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
3159 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
3160 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3163 p_eit->i_service_id,
3167 vlc_epg_Delete( p_epg );
3169 dvbpsi_DeleteEIT( p_eit );
3171 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3173 EITCallBack( p_demux, p_eit, true );
3175 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3177 EITCallBack( p_demux, p_eit, false );
3180 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3181 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
3182 uint16_t i_extension, demux_t *p_demux )
3184 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
3185 uint8_t i_table_id, uint16_t i_extension )
3190 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3191 i_table_id, i_table_id, i_extension, i_extension );
3193 if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
3195 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3196 i_table_id, i_table_id, i_extension, i_extension );
3197 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3198 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
3199 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
3201 dvbpsi_AttachSDT( h, i_table_id, i_extension,
3202 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
3205 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3206 ( i_table_id == 0x4e || /* Current/Following */
3207 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3209 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3210 i_table_id, i_table_id, i_extension, i_extension );
3212 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3213 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3214 (dvbpsi_eit_callback)EITCallBackSchedule;
3215 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3216 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
3217 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
3219 dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
3222 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3223 i_table_id == 0x70 ) /* TDT */
3225 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3226 i_table_id, i_table_id, i_extension, i_extension );
3227 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3228 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
3229 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
3231 dvbpsi_AttachTOT( h, i_table_id, i_extension,
3232 (dvbpsi_tot_callback)TDTCallBack, p_demux);
3237 /*****************************************************************************
3238 * PMT callback and helpers
3239 *****************************************************************************/
3240 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3243 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3244 while( p_dr && ( p_dr->i_tag != i_tag ) )
3245 p_dr = p_dr->p_next;
3248 static bool PMTEsHasRegistration( demux_t *p_demux,
3249 const dvbpsi_pmt_es_t *p_es,
3250 const char *psz_tag )
3252 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3256 if( p_dr->i_length < 4 )
3258 msg_Warn( p_demux, "invalid Registration Descriptor" );
3262 assert( strlen(psz_tag) == 4 );
3263 return !memcmp( p_dr->p_data, psz_tag, 4 );
3265 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3266 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3268 es_format_t *p_fmt = &pid->es->fmt;
3270 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
3271 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3273 if( p_dr && p_dr->i_length == 2 )
3275 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3277 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
3279 pid->es->p_mpeg4desc = NULL;
3281 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
3283 iod_descriptor_t *iod = prg->iod;
3284 if( iod->es_descr[i].i_es_id == i_es_id )
3286 if ( iod->es_descr[i].b_ok )
3287 pid->es->p_mpeg4desc = &iod->es_descr[i];
3289 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
3294 if( !pid->es->p_mpeg4desc )
3296 switch( p_es->i_type )
3298 /* non fatal, set by packetizer */
3299 case 0x0f: /* ADTS */
3300 case 0x11: /* LOAS */
3301 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
3302 pid->i_pid, p_es->i_type );
3305 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
3306 pid->i_pid, p_es->i_type );
3312 const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
3313 if( dcd->i_streamType == 0x04 ) /* VisualStream */
3315 p_fmt->i_cat = VIDEO_ES;
3316 switch( dcd->i_objectTypeIndication )
3318 case 0x0B: /* mpeg4 sub */
3319 p_fmt->i_cat = SPU_ES;
3320 p_fmt->i_codec = VLC_CODEC_SUBT;
3323 case 0x20: /* mpeg4 */
3324 p_fmt->i_codec = VLC_CODEC_MP4V;
3326 case 0x21: /* h264 */
3327 p_fmt->i_codec = VLC_CODEC_H264;
3334 case 0x65: /* mpeg2 */
3335 p_fmt->i_codec = VLC_CODEC_MPGV;
3337 case 0x6a: /* mpeg1 */
3338 p_fmt->i_codec = VLC_CODEC_MPGV;
3340 case 0x6c: /* mpeg1 */
3341 p_fmt->i_codec = VLC_CODEC_JPEG;
3344 p_fmt->i_cat = UNKNOWN_ES;
3348 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
3350 p_fmt->i_cat = AUDIO_ES;
3351 switch( dcd->i_objectTypeIndication )
3353 case 0x40: /* mpeg4 */
3354 p_fmt->i_codec = VLC_CODEC_MP4A;
3358 case 0x68: /* mpeg2 aac */
3359 p_fmt->i_codec = VLC_CODEC_MP4A;
3361 case 0x69: /* mpeg2 */
3362 p_fmt->i_codec = VLC_CODEC_MPGA;
3364 case 0x6b: /* mpeg1 */
3365 p_fmt->i_codec = VLC_CODEC_MPGA;
3368 p_fmt->i_cat = UNKNOWN_ES;
3374 p_fmt->i_cat = UNKNOWN_ES;
3377 if( p_fmt->i_cat != UNKNOWN_ES )
3379 p_fmt->i_extra = dcd->i_extra;
3380 if( p_fmt->i_extra > 0 )
3382 p_fmt->p_extra = malloc( p_fmt->i_extra );
3383 if( p_fmt->p_extra )
3384 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
3397 } ts_teletext_page_t;
3399 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
3400 const dvbpsi_pmt_es_t *p_es )
3402 es_format_t *p_fmt = &pid->es->fmt;
3404 ts_teletext_page_t p_page[2 * 64 + 20];
3405 unsigned i_page = 0;
3407 /* Gather pages information */
3408 #if defined _DVBPSI_DR_56_H_ && \
3409 defined DVBPSI_VERSION && DVBPSI_VERSION_INT > DVBPSI_VERSION_WANTED(0,1,5)
3410 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
3412 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
3416 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
3420 for( int i = 0; i < p_sub->i_pages_number; i++ )
3422 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
3424 if( p_src->i_teletext_type >= 0x06 )
3427 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3429 ts_teletext_page_t *p_dst = &p_page[i_page++];
3431 p_dst->i_type = p_src->i_teletext_type;
3432 p_dst->i_magazine = p_src->i_teletext_magazine_number
3433 ? p_src->i_teletext_magazine_number : 8;
3434 p_dst->i_page = p_src->i_teletext_page_number;
3435 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3440 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3443 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3444 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3446 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
3448 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
3451 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3453 ts_teletext_page_t *p_dst = &p_page[i_page++];
3455 switch( p_src->i_subtitling_type )
3458 p_dst->i_type = 0x02;
3461 p_dst->i_type = 0x03;
3464 /* FIXME check if it is the right split */
3465 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
3466 ? (p_src->i_composition_page_id >> 8) : 8;
3467 p_dst->i_page = p_src->i_composition_page_id & 0xff;
3468 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3473 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
3475 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3477 p_fmt->subs.teletext.i_magazine = -1;
3478 p_fmt->subs.teletext.i_page = 0;
3479 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
3481 dvbpsi_descriptor_t *p_dr;
3482 p_dr = PMTEsFindDescriptor( p_es, 0x46 );
3484 p_dr = PMTEsFindDescriptor( p_es, 0x56 );
3486 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3488 /* Descriptor pass-through */
3489 p_fmt->p_extra = malloc( p_dr->i_length );
3490 if( p_fmt->p_extra )
3492 p_fmt->i_extra = p_dr->i_length;
3493 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3499 for( unsigned i = 0; i < i_page; i++ )
3510 p_es = malloc( sizeof(*p_es) );
3514 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3515 free( p_es->fmt.psz_language );
3516 free( p_es->fmt.psz_description );
3517 p_es->fmt.psz_language = NULL;
3518 p_es->fmt.psz_description = NULL;
3521 p_es->p_data = NULL;
3522 p_es->i_data_size = 0;
3523 p_es->i_data_gathered = 0;
3524 p_es->pp_last = &p_es->p_data;
3525 p_es->data_type = TS_ES_DATA_PES;
3526 p_es->p_mpeg4desc = NULL;
3528 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3532 const ts_teletext_page_t *p = &p_page[i];
3533 p_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
3534 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
3535 p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
3536 p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
3537 p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
3538 p_es->fmt.subs.teletext.i_page = p->i_page;
3541 " * ttxt type=%s lan=%s page=%d%02x",
3542 p_es->fmt.psz_description,
3543 p_es->fmt.psz_language,
3544 p->i_magazine, p->i_page );
3548 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
3549 const dvbpsi_pmt_es_t *p_es )
3551 es_format_t *p_fmt = &pid->es->fmt;
3553 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
3555 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3557 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3558 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3560 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
3561 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
3562 ( i_type >= 0x20 && i_type <= 0x24 ) )
3566 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3568 p_fmt->subs.dvb.i_id = -1;
3569 p_fmt->psz_description = strdup( _("DVB subtitles") );
3571 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3573 /* Descriptor pass-through */
3574 p_fmt->p_extra = malloc( p_dr->i_length );
3575 if( p_fmt->p_extra )
3577 p_fmt->i_extra = p_dr->i_length;
3578 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3584 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3595 p_es = malloc( sizeof(*p_es) );
3599 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3600 free( p_es->fmt.psz_language );
3601 free( p_es->fmt.psz_description );
3602 p_es->fmt.psz_language = NULL;
3603 p_es->fmt.psz_description = NULL;
3606 p_es->p_data = NULL;
3607 p_es->i_data_size = 0;
3608 p_es->i_data_gathered = 0;
3609 p_es->pp_last = &p_es->p_data;
3610 p_es->data_type = TS_ES_DATA_PES;
3611 p_es->p_mpeg4desc = NULL;
3613 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3617 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
3618 p_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
3619 switch( p->i_subtitling_type )
3621 case 0x10: /* unspec. */
3622 case 0x11: /* 4:3 */
3623 case 0x12: /* 16:9 */
3624 case 0x13: /* 2.21:1 */
3625 case 0x14: /* HD monitor */
3626 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
3628 case 0x20: /* Hearing impaired unspec. */
3629 case 0x21: /* h.i. 4:3 */
3630 case 0x22: /* h.i. 16:9 */
3631 case 0x23: /* h.i. 2.21:1 */
3632 case 0x24: /* h.i. HD monitor */
3633 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
3640 p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
3641 ( p->i_ancillary_page_id << 16 );
3645 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
3646 const dvbpsi_pmt_es_t *p_es )
3648 es_format_t *p_fmt = &pid->es->fmt;
3649 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_es, 0x59 );
3651 if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
3652 PMTEsFindDescriptor( p_es, 0x6a ) ||
3653 PMTEsFindDescriptor( p_es, 0x81 ) )
3655 p_fmt->i_cat = AUDIO_ES;
3656 p_fmt->i_codec = VLC_CODEC_A52;
3658 else if( PMTEsFindDescriptor( p_es, 0x7a ) )
3660 /* DVB with stream_type 0x06 (ETS EN 300 468) */
3661 p_fmt->i_cat = AUDIO_ES;
3662 p_fmt->i_codec = VLC_CODEC_EAC3;
3664 else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
3665 PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
3666 PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
3667 PMTEsFindDescriptor( p_es, 0x73 ) )
3669 /*registration descriptor(ETSI TS 101 154 Annex F)*/
3670 p_fmt->i_cat = AUDIO_ES;
3671 p_fmt->i_codec = VLC_CODEC_DTS;
3673 else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) && !p_subs_dr )
3675 /* BSSD is AES3 DATA, but could also be subtitles
3676 * we need to check for secondary descriptor then s*/
3677 p_fmt->i_cat = AUDIO_ES;
3678 p_fmt->b_packetized = true;
3679 p_fmt->i_codec = VLC_CODEC_302M;
3681 else if( PMTEsHasRegistration( p_demux, p_es, "HEVC" ) )
3683 p_fmt->i_cat = VIDEO_ES;
3684 p_fmt->i_codec = VLC_CODEC_HEVC;
3688 /* Subtitle/Teletext/VBI fallbacks */
3689 dvbpsi_subtitling_dr_t *p_sub;
3690 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
3692 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3694 if( p_fmt->i_cat != UNKNOWN_ES )
3697 switch( p_sub->p_subtitle[i].i_subtitling_type )
3699 case 0x01: /* EBU Teletext subtitles */
3700 case 0x02: /* Associated EBU Teletext */
3701 case 0x03: /* VBI data */
3702 PMTSetupEsTeletext( p_demux, pid, p_es );
3704 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
3705 case 0x11: /* ... on 4:3 AR monitor */
3706 case 0x12: /* ... on 16:9 AR monitor */
3707 case 0x13: /* ... on 2.21:1 AR monitor */
3708 case 0x14: /* ... for display on a high definition monitor */
3709 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
3710 case 0x21: /* ... on 4:3 AR monitor */
3711 case 0x22: /* ... on 16:9 AR monitor */
3712 case 0x23: /* ... on 2.21:1 AR monitor */
3713 case 0x24: /* ... for display on a high definition monitor */
3714 PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
3717 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
3718 p_sub->p_subtitle[i].i_subtitling_type );
3724 if( p_fmt->i_cat == UNKNOWN_ES &&
3725 ( PMTEsFindDescriptor( p_es, 0x45 ) || /* VBI Data descriptor */
3726 PMTEsFindDescriptor( p_es, 0x46 ) || /* VBI Teletext descriptor */
3727 PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
3730 PMTSetupEsTeletext( p_demux, pid, p_es );
3734 /* FIXME is it useful ? */
3735 if( PMTEsFindDescriptor( p_es, 0x52 ) )
3737 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3738 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3740 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
3744 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
3745 const dvbpsi_pmt_es_t *p_es )
3747 /* Registration Descriptor */
3748 if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
3750 msg_Err( p_demux, "Registration descriptor not found or invalid" );
3754 es_format_t *p_fmt = &pid->es->fmt;
3756 /* registration descriptor for VC-1 (SMPTE rp227) */
3757 p_fmt->i_cat = VIDEO_ES;
3758 p_fmt->i_codec = VLC_CODEC_VC1;
3760 /* XXX With Simple and Main profile the SEQUENCE
3761 * header is modified: video width and height are
3762 * inserted just after the start code as 2 int16_t
3763 * The packetizer will take care of that. */
3766 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
3767 const dvbpsi_pmt_es_t *p_es )
3769 /* Registration Descriptor */
3770 if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
3772 msg_Err( p_demux, "Registration descriptor not found or invalid" );
3776 es_format_t *p_fmt = &pid->es->fmt;
3778 /* registration descriptor for Dirac
3779 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
3780 p_fmt->i_cat = VIDEO_ES;
3781 p_fmt->i_codec = VLC_CODEC_DIRAC;
3784 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
3785 const dvbpsi_pmt_es_t *p_es )
3787 /* MSCODEC sent by vlc */
3788 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
3789 if( !p_dr || p_dr->i_length < 10 )
3792 "private MSCODEC (vlc) without bih private descriptor" );
3796 es_format_t *p_fmt = &pid->es->fmt;
3797 p_fmt->i_cat = VIDEO_ES;
3798 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
3799 p_dr->p_data[2], p_dr->p_data[3] );
3800 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
3801 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
3802 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
3804 if( p_fmt->i_extra > 0 )
3806 p_fmt->p_extra = malloc( p_fmt->i_extra );
3807 if( p_fmt->p_extra )
3808 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
3809 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
3813 /* For such stream we will gather them ourself and don't launch a
3815 * Yes it's ugly but it's the only way to have DIV3 working */
3816 p_fmt->b_packetized = true;
3819 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pid_t *pid )
3821 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
3822 * with audio track pid being 0x1100..0x11FF */
3823 if ( p_pmt->i_program_number == 0x1 &&
3824 p_pmt->i_pcr_pid == 0x1000 &&
3825 ( pid->i_pid >> 8 ) == 0x11 )
3827 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
3828 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
3831 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
3834 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
3835 const dvbpsi_pmt_es_t *p_es )
3837 es_format_t *p_fmt = &pid->es->fmt;
3839 /* Blu-Ray mapping */
3840 switch( p_es->i_type )
3843 p_fmt->i_cat = AUDIO_ES;
3844 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
3847 case 0x85: /* DTS-HD High resolution audio */
3848 case 0x86: /* DTS-HD Master audio */
3849 case 0xA2: /* Secondary DTS audio */
3850 p_fmt->i_cat = AUDIO_ES;
3851 p_fmt->i_codec = VLC_CODEC_DTS;
3854 case 0x83: /* TrueHD AC3 */
3855 p_fmt->i_cat = AUDIO_ES;
3856 p_fmt->i_codec = VLC_CODEC_TRUEHD;
3859 case 0x84: /* E-AC3 */
3860 case 0xA1: /* Secondary E-AC3 */
3861 p_fmt->i_cat = AUDIO_ES;
3862 p_fmt->i_codec = VLC_CODEC_EAC3;
3864 case 0x90: /* Presentation graphics */
3865 p_fmt->i_cat = SPU_ES;
3866 p_fmt->i_codec = VLC_CODEC_BD_PG;
3868 case 0x91: /* Interactive graphics */
3869 case 0x92: /* Subtitle */
3872 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
3873 p_es->i_pid, p_es->i_type );
3880 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
3881 const dvbpsi_pmt_es_t *p_es )
3887 vlc_fourcc_t i_codec;
3889 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
3890 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
3891 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
3892 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
3893 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
3894 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
3895 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
3896 { "", UNKNOWN_ES, 0 }
3898 es_format_t *p_fmt = &pid->es->fmt;
3900 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
3902 if( PMTEsHasRegistration( p_demux, p_es, p_regs[i].psz_tag ) )
3904 p_fmt->i_cat = p_regs[i].i_cat;
3905 p_fmt->i_codec = p_regs[i].i_codec;
3906 if (p_es->i_type == 0x87)
3907 p_fmt->i_codec = VLC_CODEC_EAC3;
3914 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
3916 static const char *audio_type[] = {
3918 N_("clean effects"),
3919 N_("hearing impaired"),
3920 N_("visual impaired commentary"),
3923 if (type < 0 || type > 3)
3924 msg_Dbg( p_demux, "unknown audio type: %d", type);
3926 return strdup(audio_type[type]);
3930 static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
3931 const dvbpsi_pmt_es_t *p_es )
3933 /* get language descriptor */
3934 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
3939 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
3942 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
3946 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
3947 pid->es->fmt.psz_language = malloc( 4 );
3948 if( pid->es->fmt.psz_language )
3950 memcpy( pid->es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
3951 pid->es->fmt.psz_language[3] = 0;
3952 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
3954 int type = p_decoded->code[0].i_audio_type;
3955 pid->es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
3957 pid->es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
3959 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
3960 if( pid->es->fmt.i_extra_languages > 0 )
3961 pid->es->fmt.p_extra_languages =
3962 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
3963 pid->es->fmt.i_extra_languages );
3964 if( pid->es->fmt.p_extra_languages )
3966 for( int i = 0; i < pid->es->fmt.i_extra_languages; i++ )
3968 pid->es->fmt.p_extra_languages[i].psz_language = malloc(4);
3969 if( pid->es->fmt.p_extra_languages[i].psz_language )
3971 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
3972 p_decoded->code[i+1].iso_639_code, 3 );
3973 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
3975 int type = p_decoded->code[i].i_audio_type;
3976 pid->es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
3980 pid->es->fmt.psz_language = malloc( 4 );
3981 if( pid->es->fmt.psz_language )
3983 memcpy( pid->es->fmt.psz_language,
3984 p_decoded->i_iso_639_code, 3 );
3985 pid->es->fmt.psz_language[3] = 0;
3990 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
3992 demux_t *p_demux = data;
3993 demux_sys_t *p_sys = p_demux->p_sys;
3995 ts_pid_t *pmt = NULL;
3998 msg_Dbg( p_demux, "PMTCallBack called" );
4000 /* First find this PMT declared in PAT */
4001 for( int i = 0; !pmt && i < p_sys->i_pmt; i++ )
4002 for( int i_prg = 0; !pmt && i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
4004 const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
4005 if( i_pmt_number != TS_USER_PMT_NUMBER &&
4006 i_pmt_number == p_pmt->i_program_number )
4008 pmt = p_sys->pmt[i];
4009 prg = p_sys->pmt[i]->psi->prg[i_prg];
4015 msg_Warn( p_demux, "unreferenced program (broken stream)" );
4016 dvbpsi_DeletePMT(p_pmt);
4021 if( prg->i_version != -1 &&
4022 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
4024 dvbpsi_DeletePMT( p_pmt );
4028 ts_pid_t **pp_clean = NULL;
4030 /* Clean this program (remove all es) */
4031 for( int i = 0; i < 8192; i++ )
4033 ts_pid_t *pid = &p_sys->pid[i];
4035 if( pid->b_valid && pid->p_owner == pmt->psi &&
4036 pid->i_owner_number == prg->i_number && pid->psi == NULL )
4038 TAB_APPEND( i_clean, pp_clean, pid );
4043 IODFree( prg->iod );
4047 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
4048 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
4049 prg->i_pid_pcr = p_pmt->i_pcr_pid;
4050 prg->i_version = p_pmt->i_version;
4052 ValidateDVBMeta( p_demux, prg->i_pid_pcr );
4053 if( ProgramIsSelected( p_demux, prg->i_number ) )
4054 SetPIDFilter( p_demux, prg->i_pid_pcr, true ); /* Set demux filter */
4056 /* Parse PMT descriptors */
4057 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
4058 dvbpsi_descriptor_t *p_dr;
4059 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
4062 case 0x1d: /* We have found an IOD descriptor */
4063 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
4064 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
4068 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
4069 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
4072 case 0x5: /* Registration Descriptor */
4073 if( p_dr->i_length != 4 )
4075 msg_Warn( p_demux, "invalid Registration Descriptor" );
4079 msg_Dbg( p_demux, " * descriptor : registration %4.4s", p_dr->p_data );
4080 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
4081 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
4086 msg_Dbg( p_demux, " * descriptor : Private Data (0x0f)" );
4089 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
4090 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
4091 /* TODO: apply visibility flags */
4095 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
4098 dvbpsi_pmt_es_t *p_es;
4099 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
4101 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
4103 /* Find out if the PID was already declared */
4104 for( int i = 0; i < i_clean; i++ )
4106 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
4108 old_pid = pp_clean[i];
4112 ValidateDVBMeta( p_demux, p_es->i_pid );
4114 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
4116 msg_Warn( p_demux, "pmt error: pid=%d already defined",
4121 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
4122 p_dr = p_dr->p_next )
4124 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
4125 p_es->i_pid, p_es->i_type, p_dr->i_tag );
4128 PIDInit( pid, false, pmt->psi );
4129 PIDFillFormat( &pid->es->fmt, p_es->i_type );
4130 pid->i_owner_number = prg->i_number;
4131 pid->i_pid = p_es->i_pid;
4132 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
4135 bool b_registration_applied = false;
4136 if ( p_es->i_type >= 0x80 ) /* non standard, extensions */
4138 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
4140 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, pid, p_es ) ))
4141 msg_Dbg( p_demux, "es HDMV registration applied to pid 0x%x type 0x%x",
4142 p_es->i_pid, p_es->i_type );
4146 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, pid, p_es ) ))
4147 msg_Dbg( p_demux, "es registration applied to pid 0x%x type 0x%x",
4148 p_es->i_pid, p_es->i_type );
4152 if ( !b_registration_applied )
4154 switch( p_es->i_type )
4160 PMTSetupEsISO14496( p_demux, pid, prg, p_es );
4163 PMTSetupEs0x06( p_demux, pid, p_es );
4167 PMTSetupEs0x83( p_pmt, pid );
4170 PMTSetupEs0xA0( p_demux, pid, p_es );
4173 PMTSetupEs0xD1( p_demux, pid, p_es );
4176 PMTSetupEs0xEA( p_demux, pid, p_es );
4182 if( pid->es->fmt.i_cat == AUDIO_ES ||
4183 ( pid->es->fmt.i_cat == SPU_ES &&
4184 pid->es->fmt.i_codec != VLC_CODEC_DVBS &&
4185 pid->es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
4187 PMTParseEsIso639( p_demux, pid, p_es );
4190 switch( pid->es->fmt.i_codec )
4192 case VLC_CODEC_SCTE_27:
4193 pid->es->data_type = TS_ES_DATA_TABLE_SECTION;
4196 //pid->es->data_type = TS_ES_DATA_PES;
4200 pid->es->fmt.i_group = p_pmt->i_program_number;
4201 for( int i = 0; i < pid->i_extra_es; i++ )
4202 pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
4204 if( pid->es->fmt.i_cat == UNKNOWN_ES )
4206 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
4207 p_es->i_pid, p_es->i_type );
4211 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
4212 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
4214 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
4216 /* Check if we can avoid restarting the ES */
4218 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
4219 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
4220 pid->es->fmt.i_extra == 0 &&
4221 pid->i_extra_es == old_pid->i_extra_es &&
4222 ( ( !pid->es->fmt.psz_language &&
4223 !old_pid->es->fmt.psz_language ) ||
4224 ( pid->es->fmt.psz_language &&
4225 old_pid->es->fmt.psz_language &&
4226 !strcmp( pid->es->fmt.psz_language,
4227 old_pid->es->fmt.psz_language ) ) ) )
4229 pid->i_cc = old_pid->i_cc;
4230 ts_es_t *e = pid->es;
4231 pid->es = old_pid->es;
4233 for( int i = 0; i < pid->i_extra_es; i++ )
4235 e = pid->extra_es[i];
4236 pid->extra_es[i] = old_pid->extra_es[i];
4237 old_pid->extra_es[i] = e;
4242 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4243 for( int i = 0; i < pid->i_extra_es; i++ )
4245 pid->extra_es[i]->id =
4246 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
4248 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4252 /* Add ES to the list */
4255 PIDClean( p_demux, old_pid );
4256 TAB_REMOVE( i_clean, pp_clean, old_pid );
4258 p_sys->pid[p_es->i_pid] = *pid;
4260 p_dr = PMTEsFindDescriptor( p_es, 0x09 );
4261 if( p_dr && p_dr->i_length >= 2 )
4263 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
4264 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
4267 if( ProgramIsSelected( p_demux, prg->i_number ) && pid->es->id != NULL )
4268 SetPIDFilter( p_demux, p_es->i_pid, true ); /* Set demux filter */
4271 /* Set CAM descrambling */
4272 if( !ProgramIsSelected( p_demux, prg->i_number )
4273 || stream_Control( p_demux->s, STREAM_SET_PRIVATE_ID_CA,
4274 p_pmt ) != VLC_SUCCESS )
4275 dvbpsi_DeletePMT( p_pmt );
4277 for( int i = 0; i < i_clean; i++ )
4279 if( ProgramIsSelected( p_demux, prg->i_number ) )
4280 SetPIDFilter( p_demux, pp_clean[i]->i_pid, false );
4282 PIDClean( p_demux, pp_clean[i] );
4288 static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
4290 demux_t *p_demux = data;
4291 demux_sys_t *p_sys = p_demux->p_sys;
4292 dvbpsi_pat_program_t *p_program;
4293 ts_pid_t *pat = &p_sys->pid[0];
4295 msg_Dbg( p_demux, "PATCallBack called" );
4297 if( ( pat->psi->i_pat_version != -1 &&
4298 ( !p_pat->b_current_next ||
4299 p_pat->i_version == pat->psi->i_pat_version ) ) ||
4302 dvbpsi_DeletePAT( p_pat );
4306 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
4307 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
4310 if( p_sys->i_pmt > 0 )
4313 ts_pid_t **pmt_rm = NULL;
4315 /* Search pmt to be deleted */
4316 for( int i = 0; i < p_sys->i_pmt; i++ )
4318 ts_pid_t *pmt = p_sys->pmt[i];
4319 bool b_keep = false;
4321 for( p_program = p_pat->p_first_program; !b_keep && p_program;
4322 p_program = p_program->p_next )
4324 if( p_program->i_pid != pmt->i_pid )
4327 for( int i_prg = 0; !b_keep && i_prg < pmt->psi->i_prg; i_prg++ )
4328 if( p_program->i_number == pmt->psi->prg[i_prg]->i_number )
4335 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
4338 /* Delete all ES attached to thoses PMT */
4339 for( int i = 2; i < 8192; i++ )
4341 ts_pid_t *pid = &p_sys->pid[i];
4343 if( !pid->b_valid || pid->psi )
4346 for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
4348 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
4350 /* We only remove es that aren't defined by extra pmt */
4351 if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
4355 SetPIDFilter( p_demux, i, false );
4357 PIDClean( p_demux, pid );
4363 /* Delete PMT pid */
4364 for( int i = 0; i < i_pmt_rm; i++ )
4366 ts_pid_t *pid = pmt_rm[i];
4367 SetPIDFilter( p_demux, pid->i_pid, false );
4369 for( int i_prg = 0; i_prg < pid->psi->i_prg; i_prg++ )
4371 const int i_number = pid->psi->prg[i_prg]->i_number;
4372 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
4375 PIDClean( p_demux, &p_sys->pid[pid->i_pid] );
4376 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
4382 /* now create programs */
4383 for( p_program = p_pat->p_first_program; p_program != NULL;
4384 p_program = p_program->p_next )
4386 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
4388 if( p_program->i_number == 0 )
4391 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
4393 ValidateDVBMeta( p_demux, p_program->i_pid );
4398 for( int i_prg = 0; b_add && i_prg < pmt->psi->i_prg; i_prg++ )
4399 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
4407 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
4410 PIDInit( pmt, true, pat->psi );
4411 ts_prg_psi_t *prg = pmt->psi->prg[pmt->psi->i_prg-1];
4412 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
4413 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
4416 dvbpsi_DeletePAT( p_pat );
4419 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
4420 if( !dvbpsi_pmt_attach( prg->handle, p_program->i_number, PMTCallBack, p_demux ) )
4421 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
4422 p_program->i_number );
4424 prg->handle = dvbpsi_AttachPMT( p_program->i_number, PMTCallBack, p_demux );
4426 prg->i_number = p_program->i_number;
4427 prg->i_pid_pmt = p_program->i_pid;
4429 /* Now select PID at access level */
4430 if( ProgramIsSelected( p_demux, p_program->i_number ) )
4432 if( p_sys->i_current_program == 0 )
4433 p_sys->i_current_program = p_program->i_number;
4435 if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
4436 p_sys->b_access_control = false;
4439 pat->psi->i_pat_version = p_pat->i_version;
4441 dvbpsi_DeletePAT( p_pat );