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>
39 #include <vlc_access.h> /* DVB-specific things */
40 #include <vlc_demux.h>
43 #include <vlc_charset.h> /* FromCharset, for EIT */
46 #include "../mux/mpeg/csa.h"
48 /* Include dvbpsi headers */
49 # include <dvbpsi/dvbpsi.h>
50 # include <dvbpsi/demux.h>
51 # include <dvbpsi/descriptor.h>
52 # include <dvbpsi/pat.h>
53 # include <dvbpsi/pmt.h>
54 # include <dvbpsi/sdt.h>
55 # include <dvbpsi/dr.h>
56 # include <dvbpsi/psi.h>
59 # include <dvbpsi/eit.h>
62 # include <dvbpsi/tot.h>
64 #include "../mux/mpeg/dvbpsi_compat.h"
66 #include "../codec/opus_header.h"
71 VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
76 vfprintf(stderr, format, ap);
84 #include <aribb24/aribb24.h>
85 #include <aribb24/decoder.h>
88 typedef enum arib_modes_e
91 ARIBMODE_DISABLED = 0,
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Open ( vlc_object_t * );
99 static void Close ( vlc_object_t * );
102 * - Rename "extra pmt" to "user pmt"
103 * - Update extra pmt description
104 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
105 * where pid_description could take 3 forms:
106 * 1. pid:pcr (to force the pcr pid)
108 * 3. pid:type=fourcc where type=(video|audio|spu)
110 #define PMT_TEXT N_("Extra PMT")
111 #define PMT_LONGTEXT N_( \
112 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
114 #define PID_TEXT N_("Set id of ES to PID")
115 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
116 " handled by VLC to the same value as the PID in" \
117 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
118 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
120 #define CSA_TEXT N_("CSA Key")
121 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
122 "16 char string (8 hexadecimal bytes).")
124 #define CSA2_TEXT N_("Second CSA Key")
125 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
126 "16 char string (8 hexadecimal bytes).")
129 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
130 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
131 "The decryption routines subtract the TS-header from the value before " \
134 #define SPLIT_ES_TEXT N_("Separate sub-streams")
135 #define SPLIT_ES_LONGTEXT N_( \
136 "Separate teletex/dvbs pages into independent ES. " \
137 "It can be useful to turn off this option when using stream output." )
139 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
140 #define SEEK_PERCENT_LONGTEXT N_( \
141 "Seek and position based on a percent byte position, not a PCR generated " \
142 "time position. If seeking doesn't work property, turn on this option." )
144 #define PCR_TEXT N_("Trust in-stream PCR")
145 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
147 static const int const arib_mode_list[] =
148 { ARIBMODE_AUTO, ARIBMODE_ENABLED, ARIBMODE_DISABLED };
149 static const char *const arib_mode_list_text[] =
150 { N_("Auto"), N_("Enabled"), N_("Disabled") };
152 #define SUPPORT_ARIB_TEXT N_("ARIB STD-B24 mode")
153 #define SUPPORT_ARIB_LONGTEXT N_( \
154 "Forces ARIB STD-B24 mode for decoding characters." \
155 "This feature affects EPG information and subtitles." )
158 set_description( N_("MPEG Transport Stream demuxer") )
159 set_shortname ( "MPEG-TS" )
160 set_category( CAT_INPUT )
161 set_subcategory( SUBCAT_INPUT_DEMUX )
163 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
164 add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
166 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
168 add_obsolete_string( "ts-out" ) /* since 2.2.0 */
169 add_obsolete_integer( "ts-out-mtu" ) /* since 2.2.0 */
170 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
172 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
174 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
177 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
178 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
180 add_integer( "ts-arib", ARIBMODE_AUTO, SUPPORT_ARIB_TEXT, SUPPORT_ARIB_LONGTEXT, false )
181 change_integer_list( arib_mode_list, arib_mode_list_text )
183 add_obsolete_bool( "ts-silent" );
185 set_capability( "demux", 10 )
186 set_callbacks( Open, Close )
190 /*****************************************************************************
192 *****************************************************************************/
193 static const char *const ppsz_teletext_type[] = {
196 N_("Teletext subtitles"),
197 N_("Teletext: additional information"),
198 N_("Teletext: program schedule"),
199 N_("Teletext subtitles: hearing impaired")
204 uint8_t i_objectTypeIndication;
205 uint8_t i_streamType;
210 } decoder_config_descriptor_t;
219 decoder_config_descriptor_t dec_descr;
221 } es_mpeg4_descriptor_t;
223 #define ES_DESCRIPTOR_COUNT 255
229 es_mpeg4_descriptor_t es_descr[ES_DESCRIPTOR_COUNT];
235 dvbpsi_handle handle;
241 /* IOD stuff (mpeg4) */
242 iod_descriptor_t *iod;
248 /* for special PAT/SDT case */
249 dvbpsi_handle handle; /* PAT/SDT/EIT */
262 TS_ES_DATA_TABLE_SECTION
267 TS_PMT_REGISTRATION_NONE = 0,
268 TS_PMT_REGISTRATION_HDMV
269 } ts_pmt_registration_type_t;
275 ts_es_data_type_t data_type;
281 es_mpeg4_descriptor_t *p_mpeg4desc;
291 int i_cc; /* countinuity counter */
294 /* PSI owner (ie PMT -> PAT, ES -> PMT */
302 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
312 vlc_mutex_t csa_lock;
314 /* TS packet size (188, 192, 204) */
317 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
318 int i_packet_header_size;
320 /* how many TS packet we read at once */
323 /* to determine length and time */
326 mtime_t i_current_pcr;
328 bool b_force_seek_per_percent;
337 arib_instance_t *p_instance;
361 bool b_access_control;
367 int64_t i_dvb_length;
368 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
371 int i_current_program;
372 vlc_list_t programs_list;
376 /* broken PCR handling */
377 bool b_program_pcr_seen;
385 static int Demux ( demux_t *p_demux );
386 static int Control( demux_t *p_demux, int i_query, va_list args );
388 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
389 static void PIDClean( demux_t *, ts_pid_t *pid );
390 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
392 static void PATCallBack( void*, dvbpsi_pat_t * );
393 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
394 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
395 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
396 uint16_t i_extension, demux_t * );
398 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
399 uint8_t i_table_id, uint16_t i_extension );
402 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
404 static inline int PIDGet( block_t *p )
406 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
409 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
411 static block_t* ReadTSPacket( demux_t *p_demux );
412 static int Seek( demux_t *p_demux, double f_percent );
413 static void GetFirstPCR( demux_t *p_demux );
414 static void GetLastPCR( demux_t *p_demux );
415 static void CheckPCR( demux_t *p_demux );
416 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
417 static void PCRFixHandle( demux_t *, block_t * );
419 static void IODFree( iod_descriptor_t * );
421 #define TS_USER_PMT_NUMBER (0)
422 static int UserPmt( demux_t *p_demux, const char * );
424 static int SetPIDFilter( demux_t *, int i_pid, bool b_selected );
425 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
427 #define TS_PACKET_SIZE_188 188
428 #define TS_PACKET_SIZE_192 192
429 #define TS_PACKET_SIZE_204 204
430 #define TS_PACKET_SIZE_MAX 204
432 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size )
434 const uint8_t *p_peek;
435 if( stream_Peek( p_demux->s,
436 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
441 if( memcmp( p_peek, "TFrc", 4 ) == 0 )
444 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
445 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
446 * but after the filename the offsets seem to be incorrect. - DJ */
447 int i_duration, i_name;
448 char *psz_name = xmalloc(25);
449 char *psz_event_name;
450 char *psz_event_text = xmalloc(130);
451 char *psz_ext_text = xmalloc(1025);
453 // 2 bytes version Uimsbf (4,5)
454 // 2 bytes reserved (6,7)
455 // 2 bytes duration in minutes Uimsbf (8,9(
456 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
457 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
458 // 2 bytes service number in channel list (10, 11)
459 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
460 // 4 bytes of reserved + tuner info (14,15,16,17)
461 // 2 bytes of Service ID Bslbf (18,19)
462 // 2 bytes of PMT PID Uimsbf (20,21)
463 // 2 bytes of PCR PID Uimsbf (22,23)
464 // 2 bytes of Video PID Uimsbf (24,25)
465 // 2 bytes of Audio PID Uimsbf (26,27)
466 // 24 bytes filename Bslbf
467 memcpy( psz_name, &p_peek[28], 24 );
469 msg_Dbg( p_demux, "recordingname=%s", psz_name );
470 // 1 byte of sat index Uimsbf (52)
471 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
472 // 4 bytes of freq. Uimsbf (56,57,58,59)
473 // 2 bytes of symbol rate Uimsbf (60,61)
474 // 2 bytes of TS stream ID Uimsbf (62,63)
477 // 2 bytes duration Uimsbf (70,71)
478 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
479 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
480 // 4 bytes EventID Uimsbf (72-75)
481 // 8 bytes of Start and End time info (76-83)
482 // 1 byte reserved (84)
483 // 1 byte event name length Uimsbf (89)
484 i_name = (int)(p_peek[89]&~0x81);
485 msg_Dbg( p_demux, "event name length = %d", i_name);
486 psz_event_name = xmalloc( i_name+1 );
487 // 1 byte parental rating (90)
488 // 129 bytes of event text
489 memcpy( psz_event_name, &p_peek[91], i_name );
490 psz_event_name[i_name] = '\0';
491 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
492 psz_event_text[129-i_name] = '\0';
493 msg_Dbg( p_demux, "event name=%s", psz_event_name );
494 msg_Dbg( p_demux, "event text=%s", psz_event_text );
495 // 12 bytes reserved (220)
497 // 2 bytes Event Text Length Uimsbf
498 // 4 bytes EventID Uimsbf
499 // FIXME We just have 613 bytes. not enough for this entire text
500 // 1024 bytes Extended Event Text Bslbf
501 memcpy( psz_ext_text, p_peek+372, 1024 );
502 psz_ext_text[1024] = '\0';
503 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
504 // 52 bytes reserved Bslbf
506 msg_Dbg( p_demux, "this is a topfield file" );
507 return TS_PACKET_SIZE_188;
510 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
512 if( p_peek[i_sync] != 0x47 )
515 /* Check next 3 sync bytes */
516 int i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
517 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
519 msg_Err( p_demux, "cannot peek" );
522 if( p_peek[i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
523 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
524 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
526 return TS_PACKET_SIZE_188;
528 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
529 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
530 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
534 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
536 return TS_PACKET_SIZE_192;
538 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
539 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
540 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
542 return TS_PACKET_SIZE_204;
546 if( p_demux->b_force )
548 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
549 return TS_PACKET_SIZE_188;
551 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
555 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
556 static void vlc_dvbpsi_reset( demux_t *p_demux )
558 demux_sys_t *p_sys = p_demux->p_sys;
560 ts_pid_t *pat = &p_sys->pid[0];
561 ts_pid_t *sdt = &p_sys->pid[0x11];
562 ts_pid_t *eit = &p_sys->pid[0x12];
563 ts_pid_t *tdt = &p_sys->pid[0x14];
565 if( pat->psi->handle )
567 if( dvbpsi_decoder_present( pat->psi->handle ) )
568 dvbpsi_pat_detach( pat->psi->handle );
569 dvbpsi_delete( pat->psi->handle );
570 pat->psi->handle = NULL;
573 if( sdt->psi->handle )
575 if( dvbpsi_decoder_present( sdt->psi->handle ) )
576 dvbpsi_DetachDemux( sdt->psi->handle );
577 dvbpsi_delete( sdt->psi->handle );
578 sdt->psi->handle = NULL;
581 if( eit->psi->handle )
583 if( dvbpsi_decoder_present( eit->psi->handle ) )
584 dvbpsi_DetachDemux( eit->psi->handle );
585 dvbpsi_delete( eit->psi->handle );
586 eit->psi->handle = NULL;
589 if( tdt->psi->handle )
591 if( dvbpsi_decoder_present( tdt->psi->handle ) )
592 dvbpsi_DetachDemux( tdt->psi->handle );
593 dvbpsi_delete( tdt->psi->handle );
594 tdt->psi->handle = NULL;
599 /*****************************************************************************
601 *****************************************************************************/
602 static int Open( vlc_object_t *p_this )
604 demux_t *p_demux = (demux_t*)p_this;
607 int i_packet_size, i_packet_header_size = 0;
611 /* Search first sync byte */
612 i_packet_size = DetectPacketSize( p_demux, &i_packet_header_size );
613 if( i_packet_size < 0 )
616 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
619 memset( p_sys, 0, sizeof( demux_sys_t ) );
620 vlc_mutex_init( &p_sys->csa_lock );
622 p_demux->pf_demux = Demux;
623 p_demux->pf_control = Control;
625 /* Init p_sys field */
626 p_sys->b_dvb_meta = true;
627 p_sys->b_access_control = true;
628 p_sys->i_current_program = 0;
629 p_sys->programs_list.i_count = 0;
630 p_sys->programs_list.p_values = NULL;
631 p_sys->i_tdt_delta = 0;
632 p_sys->i_dvb_start = 0;
633 p_sys->i_dvb_length = 0;
635 p_sys->arib.b25stream = NULL;
636 p_sys->stream = p_demux->s;
638 p_sys->b_broken_charset = false;
640 for( int i = 0; i < 8192; i++ )
642 ts_pid_t *pid = &p_sys->pid[i];
645 pid->b_valid = false;
647 /* PID 8191 is padding */
648 p_sys->pid[8191].b_seen = true;
649 p_sys->i_packet_size = i_packet_size;
650 p_sys->i_packet_header_size = i_packet_header_size;
651 p_sys->i_ts_read = 50;
653 p_sys->b_start_record = false;
655 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
656 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
658 (table)->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG ); \
659 if( ! (table)->psi->handle ) \
661 vlc_mutex_destroy( &p_sys->csa_lock ); \
665 (table)->psi->handle->p_sys = (void *) VLC_OBJECT(obj); \
666 if( !dvbpsi_AttachDemux( (table)->psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
668 vlc_dvbpsi_reset( obj ); \
669 vlc_mutex_destroy( &p_sys->csa_lock ); \
671 return VLC_EGENERIC; \
676 /* Init PAT handler */
677 pat = &p_sys->pid[0];
678 PIDInit( pat, true, NULL );
679 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
680 pat->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
681 if( !pat->psi->handle )
683 vlc_mutex_destroy( &p_sys->csa_lock );
687 pat->psi->handle->p_sys = (void *) p_demux;
688 if( !dvbpsi_pat_attach( pat->psi->handle, PATCallBack, p_demux ) )
690 vlc_dvbpsi_reset( p_demux );
691 vlc_mutex_destroy( &p_sys->csa_lock );
696 pat->psi->handle = dvbpsi_AttachPAT( PATCallBack, p_demux );
698 if( p_sys->b_dvb_meta )
700 ts_pid_t *sdt = &p_sys->pid[0x11];
701 ts_pid_t *eit = &p_sys->pid[0x12];
703 PIDInit( sdt, true, NULL );
704 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
705 VLC_DVBPSI_DEMUX_TABLE_INIT( sdt, p_demux )
708 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
711 PIDInit( eit, true, NULL );
712 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
713 VLC_DVBPSI_DEMUX_TABLE_INIT( eit, p_demux )
716 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
719 ts_pid_t *tdt = &p_sys->pid[0x14];
720 PIDInit( tdt, true, NULL );
721 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
722 VLC_DVBPSI_DEMUX_TABLE_INIT( tdt, p_demux )
725 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
729 if( p_sys->b_access_control )
731 if( SetPIDFilter( p_demux, 0x11, true ) ||
732 SetPIDFilter( p_demux, 0x14, true ) ||
733 SetPIDFilter( p_demux, 0x12, true ) )
734 p_sys->b_access_control = false;
738 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
739 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
743 TAB_INIT( p_sys->i_pmt, p_sys->pmt );
747 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
749 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
751 /* We handle description of an extra PMT */
752 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
753 p_sys->b_user_pmt = false;
754 if( psz_string && *psz_string )
755 UserPmt( p_demux, psz_string );
758 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
759 if( psz_string && *psz_string )
764 p_sys->csa = csa_New();
766 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
767 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
768 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
770 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
772 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
775 else if ( i_res == VLC_SUCCESS )
777 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
781 csa_Delete( p_sys->csa );
787 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
788 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
790 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
791 if( i_pkt < 4 || i_pkt > 188 )
793 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
794 msg_Warn( p_demux, "using default packet size of 188 bytes" );
795 p_sys->i_csa_pkt_size = 188;
798 p_sys->i_csa_pkt_size = i_pkt;
799 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
805 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
807 p_sys->b_canseek = false;
808 p_sys->i_pid_ref_pcr = -1;
809 p_sys->i_first_pcr = -1;
810 p_sys->i_current_pcr = -1;
811 p_sys->i_last_pcr = -1;
812 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
813 p_sys->i_pcrs_num = 10;
814 p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
815 p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
817 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
819 if( !p_sys->p_pcrs || !p_sys->p_pos )
825 bool b_can_fastseek = false;
826 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
827 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &b_can_fastseek );
828 if ( p_sys->b_canseek )
832 GetFirstPCR( p_demux );
834 GetLastPCR( p_demux );
837 if( p_sys->i_first_pcr < 0 || p_sys->i_last_pcr < 0 )
839 msg_Dbg( p_demux, "Force Seek Per Percent: PCR's not found,");
840 p_sys->b_force_seek_per_percent = true;
844 while( p_sys->i_pmt_es <= 0 && vlc_object_alive( p_demux ) )
846 if( Demux( p_demux ) != 1 )
852 /*****************************************************************************
854 *****************************************************************************/
855 static void Close( vlc_object_t *p_this )
857 demux_t *p_demux = (demux_t*)p_this;
858 demux_sys_t *p_sys = p_demux->p_sys;
860 msg_Dbg( p_demux, "pid list:" );
861 for( int i = 0; i < 8192; i++ )
863 ts_pid_t *pid = &p_sys->pid[i];
865 if( pid->b_valid && pid->psi )
870 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
871 if( dvbpsi_decoder_present( pid->psi->handle ) )
872 dvbpsi_pat_detach( pid->psi->handle );
873 dvbpsi_delete( pid->psi->handle );
874 pid->psi->handle = NULL;
876 dvbpsi_DetachPAT( pid->psi->handle );
884 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
886 /* SDT or EIT or TDT */
887 dvbpsi_DetachDemux( pid->psi->handle );
888 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
889 dvbpsi_delete( pid->psi->handle );
890 pid->psi->handle = NULL;
896 PIDClean( p_demux, pid );
901 else if( pid->b_valid && pid->es )
903 PIDClean( p_demux, pid );
908 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
913 SetPIDFilter( p_demux, pid->i_pid, false );
916 vlc_mutex_lock( &p_sys->csa_lock );
919 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
920 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
921 csa_Delete( p_sys->csa );
923 vlc_mutex_unlock( &p_sys->csa_lock );
925 TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
927 free( p_sys->programs_list.p_values );
929 free( p_sys->p_pcrs );
930 free( p_sys->p_pos );
933 if ( p_sys->arib.p_instance )
934 arib_instance_destroy( p_sys->arib.p_instance );
937 if ( p_sys->arib.b25stream )
939 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
940 stream_Delete( p_sys->arib.b25stream );
943 vlc_mutex_destroy( &p_sys->csa_lock );
947 /*****************************************************************************
948 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
949 *****************************************************************************/
950 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
951 vlc_value_t oldval, vlc_value_t newval,
954 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
955 demux_t *p_demux = (demux_t*)p_this;
956 demux_sys_t *p_sys = p_demux->p_sys;
957 int i_tmp = (intptr_t)p_data;
959 vlc_mutex_lock( &p_sys->csa_lock );
961 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
963 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
965 vlc_mutex_unlock( &p_sys->csa_lock );
969 /*****************************************************************************
971 *****************************************************************************/
972 static int Demux( demux_t *p_demux )
974 demux_sys_t *p_sys = p_demux->p_sys;
975 bool b_wait_es = p_sys->i_pmt_es <= 0;
977 /* We read at most 100 TS packet or until a frame is completed */
978 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
980 bool b_frame = false;
982 if( !(p_pkt = ReadTSPacket( p_demux )) )
987 if( p_sys->b_start_record )
989 /* Enable recording once synchronized */
990 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
991 p_sys->b_start_record = false;
994 /* Parse the TS packet */
995 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1001 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 ) ) )
1003 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1007 for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1009 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1013 block_Release( p_pkt );
1017 b_frame = GatherData( p_demux, p_pid, p_pkt );
1022 if( !p_pid->b_seen )
1024 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1026 /* We have to handle PCR if present */
1027 PCRHandle( p_demux, p_pid, p_pkt );
1028 block_Release( p_pkt );
1030 p_pid->b_seen = true;
1032 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1036 demux_UpdateTitleFromStream( p_demux );
1040 /*****************************************************************************
1042 *****************************************************************************/
1043 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1045 demux_sys_t *p_sys = p_demux->p_sys;
1051 if( p_sys->i_dvb_length > 0 )
1053 const int64_t t = mdate() + p_sys->i_tdt_delta;
1055 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1058 *pi_length = p_sys->i_dvb_length;
1060 *pi_time = t - p_sys->i_dvb_start;
1064 return VLC_EGENERIC;
1067 static int Control( demux_t *p_demux, int i_query, va_list args )
1069 demux_sys_t *p_sys = p_demux->p_sys;
1071 bool b_bool, *pb_bool;
1078 case DEMUX_GET_POSITION:
1079 pf = (double*) va_arg( args, double* );
1081 if( p_sys->b_force_seek_per_percent ||
1082 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1083 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 ||
1084 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1086 int64_t i_time, i_length;
1087 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1088 *pf = (double)i_time/(double)i_length;
1089 else if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1091 int64_t offset = stream_Tell( p_sys->stream );
1093 *pf = (double)offset / (double)i64;
1100 *pf = (double)(p_sys->i_current_pcr - p_sys->i_first_pcr) / (double)(p_sys->i_last_pcr - p_sys->i_first_pcr);
1104 case DEMUX_SET_POSITION:
1105 f = (double) va_arg( args, double );
1107 if(!p_sys->b_canseek)
1108 return VLC_EGENERIC;
1110 if( p_sys->b_force_seek_per_percent ||
1111 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1112 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1114 i64 = stream_Size( p_sys->stream );
1115 if( stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) )
1116 return VLC_EGENERIC;
1120 if( Seek( p_demux, f ) )
1122 p_sys->b_force_seek_per_percent = true;
1123 return VLC_EGENERIC;
1128 case DEMUX_GET_TIME:
1129 pi64 = (int64_t*)va_arg( args, int64_t * );
1130 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1131 p_sys->b_force_seek_per_percent ||
1132 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 )
1134 if( DVBEventInformation( p_demux, pi64, NULL ) )
1141 *pi64 = (p_sys->i_current_pcr - p_sys->i_first_pcr) * 100 / 9;
1145 case DEMUX_GET_LENGTH:
1146 pi64 = (int64_t*)va_arg( args, int64_t * );
1147 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1148 p_sys->b_force_seek_per_percent ||
1149 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1151 if( DVBEventInformation( p_demux, NULL, pi64 ) )
1158 *pi64 = (p_sys->i_last_pcr - p_sys->i_first_pcr) * 100 / 9;
1162 case DEMUX_SET_GROUP:
1166 i_int = (int)va_arg( args, int );
1167 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1168 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1170 if( i_int == 0 && p_sys->i_current_program > 0 )
1171 i_int = p_sys->i_current_program;
1173 if( p_sys->i_current_program > 0 )
1175 if( p_sys->i_current_program != i_int )
1176 SetPrgFilter( p_demux, p_sys->i_current_program, false );
1178 else if( p_sys->i_current_program < 0 )
1180 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
1181 SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
1186 p_sys->i_current_program = i_int;
1187 SetPrgFilter( p_demux, p_sys->i_current_program, true );
1189 else if( i_int < 0 )
1191 p_sys->i_current_program = -1;
1192 p_sys->programs_list.i_count = 0;
1195 vlc_list_t *p_dst = &p_sys->programs_list;
1196 free( p_dst->p_values );
1198 p_dst->p_values = calloc( p_list->i_count,
1199 sizeof(*p_dst->p_values) );
1200 if( p_dst->p_values )
1202 p_dst->i_count = p_list->i_count;
1203 for( int i = 0; i < p_list->i_count; i++ )
1205 p_dst->p_values[i] = p_list->p_values[i];
1206 SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
1214 case DEMUX_GET_TITLE_INFO:
1216 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1217 int *c = va_arg( args, int * );
1219 *va_arg( args, int* ) = 0; /* Title offset */
1220 *va_arg( args, int* ) = 0; /* Chapter offset */
1221 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1224 case DEMUX_SET_TITLE:
1225 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1227 case DEMUX_SET_SEEKPOINT:
1228 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1230 case DEMUX_GET_META:
1231 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1233 case DEMUX_CAN_RECORD:
1234 pb_bool = (bool*)va_arg( args, bool * );
1238 case DEMUX_SET_RECORD_STATE:
1239 b_bool = (bool)va_arg( args, int );
1242 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1243 p_sys->b_start_record = b_bool;
1246 case DEMUX_GET_SIGNAL:
1247 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1250 return VLC_EGENERIC;
1254 /*****************************************************************************
1256 *****************************************************************************/
1257 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1259 demux_sys_t *p_sys = p_demux->p_sys;
1260 char *psz_dup = strdup( psz_fmt );
1261 char *psz = psz_dup;
1264 ts_prg_psi_t *prg = NULL;
1270 i_pid = strtol( psz, &psz, 0 );
1271 if( i_pid < 2 || i_pid >= 8192 )
1274 /* Parse optional program number */
1277 i_number = strtol( &psz[1], &psz, 0 );
1280 ts_pid_t *pmt = &p_sys->pid[i_pid];
1282 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1283 PIDInit( pmt, true, NULL );
1286 prg = calloc( 1, sizeof( ts_prg_psi_t ) );
1290 prg->i_pid_pcr = -1;
1291 prg->i_pid_pmt = -1;
1292 prg->i_version = -1;
1293 prg->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1294 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1295 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
1298 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
1299 if( !dvbpsi_pmt_attach( prg->handle,
1300 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1301 PMTCallBack, p_demux ) )
1303 dvbpsi_delete( prg->handle );
1308 prg->handle = dvbpsi_AttachPMT(
1309 ((i_number != TS_USER_PMT_NUMBER) ? i_number : 1),
1310 PMTCallBack, p_demux );
1312 TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1314 psz = strchr( psz, '=' );
1317 while( psz && *psz )
1319 char *psz_next = strchr( psz, ',' );
1325 i_pid = strtol( psz, &psz, 0 );
1326 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1329 char *psz_opt = &psz[1];
1330 if( !strcmp( psz_opt, "pcr" ) )
1332 prg->i_pid_pcr = i_pid;
1334 else if( !p_sys->pid[i_pid].b_valid )
1336 ts_pid_t *pid = &p_sys->pid[i_pid];
1338 char *psz_arg = strchr( psz_opt, '=' );
1342 PIDInit( pid, false, pmt->psi);
1343 if( prg->i_pid_pcr <= 0 )
1344 prg->i_pid_pcr = i_pid;
1346 if( psz_arg && strlen( psz_arg ) == 4 )
1348 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1349 psz_arg[2], psz_arg[3] );
1350 int i_cat = UNKNOWN_ES;
1351 es_format_t *fmt = &pid->es->fmt;
1353 if( !strcmp( psz_opt, "video" ) )
1355 else if( !strcmp( psz_opt, "audio" ) )
1357 else if( !strcmp( psz_opt, "spu" ) )
1360 es_format_Init( fmt, i_cat, i_codec );
1361 fmt->b_packetized = false;
1365 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1366 PIDFillFormat( &pid->es->fmt, i_stream_type );
1368 pid->es->fmt.i_group = i_number;
1369 if( p_sys->b_es_id_pid )
1370 pid->es->fmt.i_id = i_pid;
1372 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1374 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1375 (char*)&pid->es->fmt.i_codec );
1376 pid->es->id = es_out_Add( p_demux->out,
1386 p_sys->b_user_pmt = true;
1387 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1394 return VLC_EGENERIC;
1397 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1399 demux_sys_t *p_sys = p_demux->p_sys;
1401 if( !p_sys->b_access_control )
1402 return VLC_EGENERIC;
1404 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1405 i_pid, b_selected );
1408 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1410 demux_sys_t *p_sys = p_demux->p_sys;
1411 ts_prg_psi_t *p_prg = NULL;
1414 p_sys->b_disable_pcr = !p_sys->b_trust_pcr;
1416 /* Search pmt to be unselected */
1417 for( int i = 0; i < p_sys->i_pmt; i++ )
1419 ts_pid_t *pmt = p_sys->pmt[i];
1421 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1423 if( pmt->psi->prg[i_prg]->i_number == i_prg_id )
1425 i_pmt_pid = p_sys->pmt[i]->i_pid;
1426 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1433 if( i_pmt_pid <= 0 )
1437 SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1438 if( p_prg->i_pid_pcr > 0 )
1439 SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
1442 for( int i = 2; i < 8192; i++ )
1444 ts_pid_t *pid = &p_sys->pid[i];
1446 if( !pid->b_valid || pid->psi )
1449 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1451 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1453 /* We only remove/select es that aren't defined by extra pmt */
1454 SetPIDFilter( p_demux, i, b_selected );
1461 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1463 bool b_old_valid = pid->b_valid;
1465 pid->b_valid = true;
1467 pid->b_scrambled = false;
1468 pid->p_owner = p_owner;
1469 pid->i_owner_number = 0;
1471 TAB_INIT( pid->i_extra_es, pid->extra_es );
1479 pid->psi = xmalloc( sizeof( ts_psi_t ) );
1480 pid->psi->handle = NULL;
1481 TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1485 pid->psi->i_pat_version = -1;
1486 pid->psi->i_sdt_version = -1;
1489 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1493 prg->i_version = -1;
1495 prg->i_pid_pcr = -1;
1496 prg->i_pid_pmt = -1;
1497 prg->i_pcr_value= -1;
1501 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1507 pid->es = calloc( 1, sizeof( ts_es_t ) );
1511 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1512 pid->es->data_type = TS_ES_DATA_PES;
1513 pid->es->pp_last = &pid->es->p_data;
1517 static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
1519 demux_sys_t *p_sys = p_demux->p_sys;
1520 es_out_t *out = p_demux->out;
1524 if( pid->psi->handle )
1526 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1527 if( dvbpsi_decoder_present( pid->psi->handle ) )
1528 dvbpsi_pmt_detach( pid->psi->handle );
1529 dvbpsi_delete( pid->psi->handle );
1530 pid->psi->handle = NULL;
1532 dvbpsi_DetachPMT( pid->psi->handle );
1535 for( int i = 0; i < pid->psi->i_prg; i++ )
1537 if( pid->psi->prg[i]->iod )
1538 IODFree( pid->psi->prg[i]->iod );
1539 if( pid->psi->prg[i]->handle )
1541 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1542 if( dvbpsi_decoder_present( pid->psi->prg[i]->handle ) )
1543 dvbpsi_pmt_detach( pid->psi->prg[i]->handle );
1544 dvbpsi_delete( pid->psi->prg[i]->handle );
1546 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1549 free( pid->psi->prg[i] );
1551 free( pid->psi->prg );
1558 es_out_Del( out, pid->es->id );
1562 if( pid->es->p_data )
1563 block_ChainRelease( pid->es->p_data );
1565 es_format_Clean( &pid->es->fmt );
1569 for( int i = 0; i < pid->i_extra_es; i++ )
1571 if( pid->extra_es[i]->id )
1573 es_out_Del( out, pid->extra_es[i]->id );
1577 if( pid->extra_es[i]->p_data )
1578 block_ChainRelease( pid->extra_es[i]->p_data );
1580 es_format_Clean( &pid->extra_es[i]->fmt );
1582 free( pid->extra_es[i] );
1584 if( pid->i_extra_es )
1585 free( pid->extra_es );
1588 pid->b_valid = false;
1591 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1596 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1607 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1609 block_t *out = NULL;
1610 block_t **last = NULL;
1612 uint8_t *buf = block->p_buffer;
1613 size_t len = block->i_buffer;
1615 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
1616 int16_t start_trim = 0, end_trim = 0;
1617 int start_trim_flag = (buf[1] >> 4) & 1;
1618 int end_trim_flag = (buf[1] >> 3) & 1;
1619 int control_extension_flag = (buf[1] >> 2) & 1;
1624 unsigned au_size = 0;
1632 if (start_trim_flag) {
1633 start_trim = read_opus_flag(&buf, &len);
1634 if (start_trim < 0) {
1635 msg_Err(demux, "Invalid start trimming flag");
1638 if (end_trim_flag) {
1639 end_trim = read_opus_flag(&buf, &len);
1641 msg_Err(demux, "Invalid end trimming flag");
1644 if (control_extension_flag && len) {
1645 unsigned l = *buf++; len--;
1647 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
1654 if (!au_size || au_size > len) {
1655 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
1659 block_t *au = block_Alloc(au_size);
1662 memcpy(au->p_buffer, buf, au_size);
1663 block_CopyProperties(au, block);
1672 au->i_nb_samples = opus_frame_duration(buf, au_size);
1673 if (end_trim && end_trim <= au->i_nb_samples)
1674 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
1678 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
1679 au->i_nb_samples -= start_trim;
1680 if (au->i_nb_samples == 0)
1681 au->i_flags |= BLOCK_FLAG_PREROLL;
1688 block_Release(block);
1692 /****************************************************************************
1694 ****************************************************************************/
1695 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
1697 demux_sys_t *p_sys = p_demux->p_sys;
1699 unsigned i_pes_size = 0;
1700 unsigned i_skip = 0;
1703 mtime_t i_length = 0;
1705 /* FIXME find real max size */
1706 /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
1708 if( pid->b_scrambled || header[0] != 0 || header[1] != 0 || header[2] != 1 )
1710 if ( !pid->b_scrambled )
1711 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
1712 header[0], header[1],header[2],header[3], pid->i_pid );
1713 block_ChainRelease( p_pes );
1717 /* TODO check size */
1720 case 0xBC: /* Program stream map */
1721 case 0xBE: /* Padding */
1722 case 0xBF: /* Private stream 2 */
1723 case 0xF0: /* ECM */
1724 case 0xF1: /* EMM */
1725 case 0xFF: /* Program stream directory */
1726 case 0xF2: /* DSMCC stream */
1727 case 0xF8: /* ITU-T H.222.1 type E stream */
1731 if( ( header[6]&0xC0 ) == 0x80 )
1734 i_skip = header[8] + 9;
1736 if( header[7]&0x80 ) /* has pts */
1738 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1739 (mtime_t)(header[10] << 22)|
1740 ((mtime_t)(header[11]&0xfe) << 14)|
1741 (mtime_t)(header[12] << 7)|
1742 (mtime_t)(header[13] >> 1);
1744 if( header[7]&0x40 ) /* has dts */
1746 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1747 (mtime_t)(header[15] << 22)|
1748 ((mtime_t)(header[16]&0xfe) << 14)|
1749 (mtime_t)(header[17] << 7)|
1750 (mtime_t)(header[18] >> 1);
1757 while( i_skip < 23 && header[i_skip] == 0xff )
1763 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1764 block_ChainRelease( p_pes );
1767 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1772 if( header[i_skip]&0x20 )
1774 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1775 (mtime_t)(header[i_skip+1] << 22)|
1776 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1777 (mtime_t)(header[i_skip+3] << 7)|
1778 (mtime_t)(header[i_skip+4] >> 1);
1780 if( header[i_skip]&0x10 ) /* has dts */
1782 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1783 (mtime_t)(header[i_skip+6] << 22)|
1784 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1785 (mtime_t)(header[i_skip+8] << 7)|
1786 (mtime_t)(header[i_skip+9] >> 1);
1802 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1803 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1807 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1808 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1809 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1813 else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
1814 pid->es->p_mpeg4desc )
1816 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1818 if( dcd->i_extra > 2 &&
1819 dcd->p_extra[0] == 0x10 &&
1820 ( dcd->p_extra[1]&0x10 ) )
1822 /* display length */
1823 if( p_pes->i_buffer + 2 <= i_skip )
1824 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1828 if( p_pes->i_buffer + 2 <= i_skip )
1829 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1835 while( p_pes && i_skip > 0 )
1837 if( p_pes->i_buffer <= i_skip )
1839 block_t *p_next = p_pes->p_next;
1841 i_skip -= p_pes->i_buffer;
1842 block_Release( p_pes );
1847 p_pes->i_buffer -= i_skip;
1848 p_pes->p_buffer += i_skip;
1853 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1854 if( i_pts >= 0 && i_dts < 0 )
1862 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
1865 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
1867 p_pes->i_length = i_length * 100 / 9;
1869 p_block = block_ChainGather( p_pes );
1870 if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
1872 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1874 p_block->i_buffer = i_pes_size;
1877 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1880 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1882 else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
1884 if( p_block->i_pts <= VLC_TS_INVALID )
1886 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
1887 * In this case use the last PCR + 40ms */
1888 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
1890 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
1892 mtime_t i_pcr = pid->p_owner->prg[i]->i_pcr_value;
1893 if( i_pcr > VLC_TS_INVALID )
1894 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
1900 else if( pid->es->fmt.i_codec == VLC_CODEC_ARIB_A ||
1901 pid->es->fmt.i_codec == VLC_CODEC_ARIB_C )
1903 if( p_block->i_pts <= VLC_TS_INVALID )
1905 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1907 p_block->i_buffer = i_pes_size;
1910 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1913 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1916 else if( pid->es->fmt.i_codec == VLC_CODEC_OPUS)
1918 p_block = Opus_Parse(p_demux, p_block);
1922 block_t *p_next = p_block->p_next;
1923 p_block->p_next = NULL;
1924 for( int i = 0; i < pid->i_extra_es; i++ )
1926 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1927 block_Duplicate( p_block ) );
1930 PCRFixHandle( p_demux, p_block );
1932 if ( p_sys->b_disable_pcr && p_block->i_dts > VLC_TS_INVALID )
1933 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1934 pid->i_owner_number, p_block->i_dts);
1936 es_out_Send( p_demux->out, pid->es->id, p_block );
1943 msg_Warn( p_demux, "empty pes" );
1947 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
1949 block_t *p_content = block_ChainGather( p_data );
1950 mtime_t i_date = -1;
1951 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
1953 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
1955 i_date = pid->p_owner->prg[i]->i_pcr_value;
1962 if( pid->es->fmt.i_codec == VLC_CODEC_SCTE_27 )
1964 /* We need to extract the truncated pts stored inside the payload */
1965 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
1968 size_t i_offset = 4;
1969 if( p_content->p_buffer[3] & 0x40 )
1971 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
1972 p_content->p_buffer[8];
1975 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
1977 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
1980 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
1981 if( i_display_in < i_date )
1982 i_date = i_display_in + (1ll << 32);
1984 i_date = i_display_in;
1991 p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
1993 es_out_Send( p_demux->out, pid->es->id, p_content );
1995 PCRFixHandle( p_demux, p_content );
1997 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
1999 block_t *p_data = pid->es->p_data;
2001 /* remove the pes from pid */
2002 pid->es->p_data = NULL;
2003 pid->es->i_data_size = 0;
2004 pid->es->i_data_gathered = 0;
2005 pid->es->pp_last = &pid->es->p_data;
2007 if( pid->es->data_type == TS_ES_DATA_PES )
2009 ParsePES( p_demux, pid, p_data );
2011 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
2013 ParseTableSection( p_demux, pid, p_data );
2017 block_ChainRelease( p_data );
2021 static block_t* ReadTSPacket( demux_t *p_demux )
2023 demux_sys_t *p_sys = p_demux->p_sys;
2027 /* Get a new TS packet */
2028 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2030 msg_Dbg( p_demux, "eof ?" );
2034 /* Skip header (BluRay streams).
2035 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2036 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2038 p_pkt->p_buffer += p_sys->i_packet_header_size;
2039 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2041 /* Check sync byte and re-sync if needed */
2042 if( p_pkt->p_buffer[0] != 0x47 )
2044 msg_Warn( p_demux, "lost synchro" );
2045 block_Release( p_pkt );
2046 while( vlc_object_alive (p_demux) )
2048 const uint8_t *p_peek;
2049 int i_peek, i_skip = 0;
2051 i_peek = stream_Peek( p_sys->stream, &p_peek,
2052 p_sys->i_packet_size * 10 );
2053 if( i_peek < p_sys->i_packet_size + 1 )
2055 msg_Dbg( p_demux, "eof ?" );
2059 while( i_skip < i_peek - p_sys->i_packet_size )
2061 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2062 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2068 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2069 stream_Read( p_sys->stream, NULL, i_skip );
2071 if( i_skip < i_peek - p_sys->i_packet_size )
2076 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2078 msg_Dbg( p_demux, "eof ?" );
2085 static mtime_t AdjustPCRWrapAround( demux_t *p_demux, mtime_t i_pcr )
2087 demux_sys_t *p_sys = p_demux->p_sys;
2089 * PCR is 33bit. If PCR reaches to 0x1FFFFFFFF (26:30:43.717), ressets from 0.
2090 * So, need to add 0x1FFFFFFFF, for calculating duration or current position.
2092 mtime_t i_adjust = 0;
2093 int64_t i_pos = stream_Tell( p_sys->stream );
2095 for( i = 1; i < p_sys->i_pcrs_num && p_sys->p_pos[i] <= i_pos; ++i )
2097 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2098 i_adjust += 0x1FFFFFFFF;
2100 if( p_sys->p_pcrs[i-1] > i_pcr )
2101 i_adjust += 0x1FFFFFFFF;
2103 return i_pcr + i_adjust;
2106 static mtime_t GetPCR( block_t *p_pkt )
2108 const uint8_t *p = p_pkt->p_buffer;
2112 if( ( p[3]&0x20 ) && /* adaptation */
2116 /* PCR is 33 bits */
2117 i_pcr = ( (mtime_t)p[6] << 25 ) |
2118 ( (mtime_t)p[7] << 17 ) |
2119 ( (mtime_t)p[8] << 9 ) |
2120 ( (mtime_t)p[9] << 1 ) |
2121 ( (mtime_t)p[10] >> 7 );
2126 static int SeekToPCR( demux_t *p_demux, int64_t i_pos )
2128 demux_sys_t *p_sys = p_demux->p_sys;
2131 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2134 return VLC_EGENERIC;
2136 int64_t i_last_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2137 if( i_pos > i_last_pos )
2140 if( stream_Seek( p_sys->stream, i_pos ) )
2141 return VLC_EGENERIC;
2143 while( vlc_object_alive( p_demux ) )
2147 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2151 if( PIDGet( p_pkt ) == p_sys->i_pid_ref_pcr )
2153 i_pcr = GetPCR( p_pkt );
2155 block_Release( p_pkt );
2158 if( stream_Tell( p_sys->stream ) >= i_last_pos )
2163 stream_Seek( p_sys->stream, i_initial_pos );
2164 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2165 return VLC_EGENERIC;
2168 p_sys->i_current_pcr = i_pcr;
2172 static int Seek( demux_t *p_demux, double f_percent )
2174 demux_sys_t *p_sys = p_demux->p_sys;
2176 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2177 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2180 * Find the time position by using binary search algorithm.
2182 mtime_t i_target_pcr = (p_sys->i_last_pcr - p_sys->i_first_pcr) * f_percent + p_sys->i_first_pcr;
2184 int64_t i_head_pos = 0;
2187 mtime_t i_adjust = 0;
2189 for( i = 1; i < p_sys->i_pcrs_num; ++i )
2191 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2192 i_adjust += 0x1FFFFFFFF;
2193 if( p_sys->p_pcrs[i] + i_adjust > i_target_pcr )
2196 i_head_pos = p_sys->p_pos[i-1];
2197 i_tail_pos = ( i < p_sys->i_pcrs_num ) ? p_sys->p_pos[i] : stream_Size( p_sys->stream );
2199 msg_Dbg( p_demux, "Seek():i_head_pos:%"PRId64", i_tail_pos:%"PRId64, i_head_pos, i_tail_pos);
2201 bool b_found = false;
2203 while( i_head_pos <= i_tail_pos )
2205 /* Round i_pos to a multiple of p_sys->i_packet_size */
2206 int64_t i_pos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2207 int64_t i_div = i_pos % p_sys->i_packet_size;
2209 if( SeekToPCR( p_demux, i_pos ) )
2211 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2212 int64_t i_diff_msec = (p_sys->i_current_pcr - i_target_pcr) * 100 / 9 / 1000;
2213 if( i_diff_msec > 500 )
2215 i_tail_pos = i_pos - p_sys->i_packet_size;
2217 else if( i_diff_msec < -500 )
2219 i_head_pos = i_pos + p_sys->i_packet_size;
2223 // diff time <= 500msec
2231 msg_Dbg( p_demux, "Seek():cannot find a time position. i_cnt:%d", i_cnt );
2232 stream_Seek( p_sys->stream, i_initial_pos );
2233 p_sys->i_current_pcr = i_initial_pcr;
2234 return VLC_EGENERIC;
2238 msg_Dbg( p_demux, "Seek():can find a time position. i_cnt:%d", i_cnt );
2239 p_demux->p_sys->pcrfix.i_first_dts = 0;
2244 static void GetFirstPCR( demux_t *p_demux )
2246 demux_sys_t *p_sys = p_demux->p_sys;
2248 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2250 if( stream_Seek( p_sys->stream, 0 ) )
2253 while( vlc_object_alive (p_demux) )
2257 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2261 mtime_t i_pcr = GetPCR( p_pkt );
2264 p_sys->i_pid_ref_pcr = PIDGet( p_pkt );
2265 p_sys->i_first_pcr = i_pcr;
2266 p_sys->i_current_pcr = i_pcr;
2268 block_Release( p_pkt );
2269 if( p_sys->i_first_pcr >= 0 )
2272 stream_Seek( p_sys->stream, i_initial_pos );
2275 static void GetLastPCR( demux_t *p_demux )
2277 demux_sys_t *p_sys = p_demux->p_sys;
2279 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2280 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2282 int64_t i_stream_size = stream_Size( p_sys->stream );
2283 int64_t i_last_pos = i_stream_size - p_sys->i_packet_size;
2284 /* Round i_pos to a multiple of p_sys->i_packet_size */
2285 int64_t i_pos = i_last_pos - p_sys->i_packet_size * 4500; /* FIXME if the value is not reasonable, please change it. */
2286 int64_t i_div = i_pos % p_sys->i_packet_size;
2289 if( i_pos <= i_initial_pos && i_pos >= i_stream_size )
2290 i_pos = i_initial_pos + p_sys->i_packet_size;
2291 if( i_pos < 0 && i_pos >= i_stream_size )
2294 while( vlc_object_alive( p_demux ) )
2296 if( SeekToPCR( p_demux, i_pos ) )
2298 p_sys->i_last_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2299 if( ( i_pos = stream_Tell( p_sys->stream ) ) >= i_last_pos )
2302 if( p_sys->i_last_pcr >= 0 )
2304 int64_t i_size = stream_Size( p_sys->stream );
2305 mtime_t i_duration_msec = ( p_sys->i_last_pcr - p_sys->i_first_pcr ) * 100 / 9 / 1000;
2306 int64_t i_rate = ( i_size < 0 || i_duration_msec <= 0 ) ? 0 : i_size * 1000 * 8 / i_duration_msec;
2307 const int64_t TS_SUPPOSED_MAXRATE = 55 * 1000 * 1000; //FIXME I think it's enough.
2308 const int64_t TS_SUPPOSED_MINRATE = 0.5 * 1000 * 1000; //FIXME
2309 if( i_rate < TS_SUPPOSED_MINRATE || i_rate > TS_SUPPOSED_MAXRATE )
2311 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)",
2312 i_rate, TS_SUPPOSED_MINRATE, TS_SUPPOSED_MAXRATE );
2313 p_sys->i_last_pcr = -1;
2316 stream_Seek( p_sys->stream, i_initial_pos );
2317 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2318 p_sys->i_current_pcr = i_initial_pcr;
2321 static void CheckPCR( demux_t *p_demux )
2323 demux_sys_t *p_sys = p_demux->p_sys;
2325 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2326 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2328 int64_t i_size = stream_Size( p_sys->stream );
2331 p_sys->p_pcrs[0] = p_sys->i_first_pcr;
2332 p_sys->p_pos[0] = i_initial_pos;
2334 for( i = 1; i < p_sys->i_pcrs_num && vlc_object_alive( p_demux ); ++i )
2336 /* Round i_pos to a multiple of p_sys->i_packet_size */
2337 int64_t i_pos = i_size / p_sys->i_pcrs_num * i;
2338 int64_t i_div = i_pos % p_sys->i_packet_size;
2340 if( SeekToPCR( p_demux, i_pos ) )
2342 p_sys->p_pcrs[i] = p_sys->i_current_pcr;
2343 p_sys->p_pos[i] = stream_Tell( p_sys->stream );
2344 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2346 msg_Dbg( p_demux, "PCR Wrap Around found between %d%% and %d%% (pcr:%"PRId64"(0x%09"PRIx64") pcr:%"PRId64"(0x%09"PRIx64"))",
2347 (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] );
2350 if( i < p_sys->i_pcrs_num )
2352 msg_Dbg( p_demux, "Force Seek Per Percent: Seeking failed at %d%%.", (int)(i*100/p_sys->i_pcrs_num) );
2353 p_sys->b_force_seek_per_percent = true;
2356 stream_Seek( p_sys->stream, i_initial_pos );
2357 p_sys->i_current_pcr = i_initial_pcr;
2360 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2362 demux_sys_t *p_sys = p_demux->p_sys;
2364 if( p_sys->i_pmt_es <= 0 )
2367 mtime_t i_pcr = GetPCR( p_bk );
2371 if( p_sys->i_pid_ref_pcr == pid->i_pid )
2372 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
2374 /* Search program and set the PCR */
2376 for( int i = 0; i < p_sys->i_pmt && i_group < 0 ; i++ )
2378 for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2380 ts_prg_psi_t *p_prg = p_sys->pmt[i]->psi->prg[i_prg];
2382 if( p_prg->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
2384 if( pid->p_owner ) /* PCR shall be on pid itself */
2386 p_prg->i_pcr_value = i_pcr; /* ? update PCR for the whole group program */
2387 i_group = p_prg->i_number;
2388 p_sys->pcrfix.b_program_pcr_seen = true;
2389 p_sys->pcrfix.i_first_dts = 0;
2390 p_sys->b_disable_pcr = false;
2394 msg_Warn(p_demux, "discarding PCR update from pid %d which has no owner", pid->i_pid);
2398 else /* set PCR provided by current pid to program(s) referencing it */
2400 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
2401 if( p_prg->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
2403 p_prg->i_pcr_value = i_pcr;
2404 i_group = p_prg->i_number; /* We've found a target group for update */
2405 p_sys->pcrfix.b_program_pcr_seen = true;
2406 p_sys->pcrfix.i_first_dts = 0;
2407 p_sys->b_disable_pcr = false;
2411 if ( !p_sys->b_disable_pcr && i_group > 0 && p_sys->i_pmt_es )
2413 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2414 i_group, VLC_TS_0 + i_pcr * 100 / 9 );
2419 static void PCRFixHandle( demux_t *p_demux, block_t *p_block )
2421 demux_sys_t *p_sys = p_demux->p_sys;
2422 /* Record the first data packet timestamp in case there wont be any PCR */
2423 if( !p_sys->pcrfix.b_program_pcr_seen && !p_sys->b_disable_pcr )
2425 if( !p_sys->pcrfix.i_first_dts )
2427 p_sys->pcrfix.i_first_dts = p_block->i_dts;
2429 else if( p_block->i_dts - p_sys->pcrfix.i_first_dts > CLOCK_FREQ / 10 ) /* "shall not exceed 100ms" */
2431 p_sys->b_disable_pcr = true;
2432 msg_Warn( p_demux, "No PCR received for program %d, set up workaround",
2433 p_sys->i_current_program );
2438 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2440 const uint8_t *p = p_bk->p_buffer;
2441 const bool b_unit_start = p[1]&0x40;
2442 const bool b_scrambled = p[3]&0x80;
2443 const bool b_adaptation = p[3]&0x20;
2444 const bool b_payload = p[3]&0x10;
2445 const int i_cc = p[3]&0x0f; /* continuity counter */
2446 bool b_discontinuity = false; /* discontinuity */
2448 /* transport_scrambling_control is ignored */
2453 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
2454 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
2458 /* For now, ignore additional error correction
2459 * TODO: handle Reed-Solomon 204,188 error correction */
2460 p_bk->i_buffer = TS_PACKET_SIZE_188;
2464 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
2466 if( pid->es->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
2467 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2470 if( p_demux->p_sys->csa )
2472 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
2473 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
2474 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
2479 /* We don't have any adaptation_field, so payload starts
2480 * immediately after the 4 byte TS header */
2485 /* p[4] is adaptation_field_length minus one */
2489 /* discontinuity indicator found in stream */
2490 b_discontinuity = (p[5]&0x80) ? true : false;
2491 if( b_discontinuity && pid->es->p_data )
2493 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
2495 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
2499 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
2504 /* Test continuity counter */
2505 /* continuous when (one of this):
2507 * diff == 0 and payload == 0
2508 * diff == 0 and duplicate packet (playload != 0) <- should we
2509 * test the content ?
2511 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
2512 if( b_payload && i_diff == 1 )
2514 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
2518 if( pid->i_cc == 0xff )
2520 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
2524 else if( i_diff != 0 && !b_discontinuity )
2526 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
2527 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
2530 if( pid->es->p_data && pid->es->fmt.i_cat != VIDEO_ES &&
2531 pid->es->fmt.i_cat != AUDIO_ES )
2533 /* Small audio/video artifacts are usually better than
2534 * dropping full frames */
2535 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2540 PCRHandle( p_demux, pid, p_bk );
2542 if( i_skip >= 188 || pid->es->id == NULL )
2544 block_Release( p_bk );
2549 if( !pid->b_scrambled != !b_scrambled )
2551 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2552 pid->i_pid, pid->b_scrambled, b_scrambled );
2554 pid->b_scrambled = b_scrambled;
2556 for( int i = 0; i < pid->i_extra_es; i++ )
2558 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2559 pid->extra_es[i]->id, b_scrambled );
2561 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2562 pid->es->id, b_scrambled );
2565 /* We have to gather it */
2566 p_bk->p_buffer += i_skip;
2567 p_bk->i_buffer -= i_skip;
2571 if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
2573 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
2574 block_t *p = block_Duplicate( p_bk );
2577 p->i_buffer = i_pointer_field;
2579 block_ChainLastAppend( &pid->es->pp_last, p );
2581 p_bk->i_buffer -= 1 + i_pointer_field;
2582 p_bk->p_buffer += 1 + i_pointer_field;
2584 if( pid->es->p_data )
2586 ParseData( p_demux, pid );
2590 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2591 if( pid->es->data_type == TS_ES_DATA_PES )
2593 if( p_bk->i_buffer > 6 )
2595 pid->es->i_data_size = GetWBE( &p_bk->p_buffer[4] );
2596 if( pid->es->i_data_size > 0 )
2598 pid->es->i_data_size += 6;
2602 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
2604 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
2606 pid->es->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
2609 pid->es->i_data_gathered += p_bk->i_buffer;
2610 if( pid->es->i_data_size > 0 &&
2611 pid->es->i_data_gathered >= pid->es->i_data_size )
2613 ParseData( p_demux, pid );
2619 if( pid->es->p_data == NULL )
2621 /* msg_Dbg( p_demux, "broken packet" ); */
2622 block_Release( p_bk );
2626 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2627 pid->es->i_data_gathered += p_bk->i_buffer;
2629 if( pid->es->i_data_size > 0 &&
2630 pid->es->i_data_gathered >= pid->es->i_data_size )
2632 ParseData( p_demux, pid );
2641 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
2643 switch( i_stream_type )
2645 case 0x01: /* MPEG-1 video */
2646 case 0x02: /* MPEG-2 video */
2647 case 0x80: /* MPEG-2 MOTO video */
2648 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
2650 case 0x03: /* MPEG-1 audio */
2651 case 0x04: /* MPEG-2 audio */
2652 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
2654 case 0x11: /* MPEG4 (audio) LATM */
2655 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
2656 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
2657 transport syntax, such as DST, ALS and SLS */
2658 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
2660 case 0x10: /* MPEG4 (video) */
2661 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
2663 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
2664 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
2666 case 0x24: /* HEVC */
2667 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
2669 case 0x42: /* CAVS (Chinese AVS) */
2670 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
2673 case 0x81: /* A52 (audio) */
2674 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
2676 case 0x82: /* SCTE-27 (sub) */
2677 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
2679 case 0x84: /* SDDS (audio) */
2680 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
2682 case 0x85: /* DTS (audio) */
2683 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
2685 case 0x87: /* E-AC3 */
2686 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
2689 case 0x91: /* A52 vls (audio) */
2690 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
2692 case 0x92: /* DVD_SPU vls (sub) */
2693 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
2696 case 0x94: /* SDDS (audio) */
2697 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
2700 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
2701 es_format_Init( fmt, UNKNOWN_ES, 0 );
2704 case 0x06: /* PES_PRIVATE (fixed later) */
2705 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
2706 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
2708 es_format_Init( fmt, UNKNOWN_ES, 0 );
2712 /* PES packets usually contain truncated frames */
2713 fmt->b_packetized = false;
2716 /*****************************************************************************
2717 * MP4 specific functions (IOD parser)
2718 *****************************************************************************/
2719 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
2722 unsigned int i_len = 0;
2728 i_len = ( i_len << 7 ) + ( i_b&0x7f );
2730 } while( i_b&0x80 && *pi_data > 0 );
2732 if (i_len > *pi_data)
2738 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
2741 while( *pi_data > 0 && bytes-- )
2752 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
2754 int len = IODGetBytes( pi_data, pp_data, 1 );
2757 char *url = strndup( (char*)*pp_data, len );
2763 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2765 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
2767 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
2776 byte1 = IODGetBytes( &i_data, &p_data, 1 );
2777 byte2 = IODGetBytes( &i_data, &p_data, 1 );
2778 byte3 = IODGetBytes( &i_data, &p_data, 1 );
2779 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2781 i_iod_label = byte1;
2784 else //correct implementation of the IOD_descriptor
2786 i_iod_label = byte2;
2790 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
2792 if( i_iod_tag != 0x02 )
2794 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
2798 IODDescriptorLength( &i_data, &p_data );
2800 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
2801 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
2802 i_od_id |= i_flags >> 6;
2803 ts_debug( "\n* od_id:%d", i_od_id );
2804 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2805 if ((i_flags >> 5) & 0x01)
2807 p_iod->psz_url = IODGetURL( &i_data, &p_data );
2808 ts_debug( "\n* url string:%s", p_iod->psz_url );
2809 ts_debug( "\n*****************************\n" );
2814 p_iod->psz_url = NULL;
2817 /* Profile Level Indication */
2818 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
2819 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
2820 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
2821 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
2822 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
2825 int i_data_sav = i_data;
2826 uint8_t *p_data_sav = p_data;
2827 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
2829 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
2831 p_data = p_data_sav + i_length;
2832 i_data = i_data_sav - i_length;
2834 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
2835 i_length = IODDescriptorLength( &i_data, &p_data );
2837 i_data_sav = i_data;
2838 p_data_sav = p_data;
2842 if ( i_tag != 0x03 )
2844 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
2848 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
2849 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
2850 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2851 if( b_streamDependenceFlag )
2852 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
2854 if( (i_flags >> 6) & 0x01 )
2855 es_descr->psz_url = IODGetURL( &i_data, &p_data );
2857 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2858 if( b_OCRStreamFlag )
2859 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
2861 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
2863 ts_debug( "\n* ERR missing DecoderConfigDescr" );
2866 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
2867 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
2868 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
2869 i_flags = IODGetBytes( &i_data, &p_data, 1 );
2870 dec_descr->i_streamType = i_flags >> 2;
2872 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
2873 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
2874 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
2876 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
2878 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
2879 if( dec_descr->i_extra > 0 )
2881 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
2882 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
2883 p_data += dec_descr->i_extra;
2884 i_data -= dec_descr->i_extra;
2889 dec_descr->i_extra = 0;
2890 dec_descr->p_extra = NULL;
2893 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
2895 ts_debug( "\n* ERR missing SLConfigDescr" );
2898 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
2899 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
2902 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
2907 es_descr->b_ok = true;
2913 static void IODFree( iod_descriptor_t *p_iod )
2915 if( p_iod->psz_url )
2917 free( p_iod->psz_url );
2922 for( int i = 0; i < 255; i++ )
2924 #define es_descr p_iod->es_descr[i]
2927 if( es_descr.psz_url )
2928 free( es_descr.psz_url );
2930 free( es_descr.dec_descr.p_extra );
2937 /****************************************************************************
2938 ****************************************************************************
2939 ** libdvbpsi callbacks
2940 ****************************************************************************
2941 ****************************************************************************/
2942 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2944 demux_sys_t *p_sys = p_demux->p_sys;
2946 if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
2947 p_sys->i_current_program == 0 )
2949 if( p_sys->i_current_program == i_pgrm )
2952 if( p_sys->programs_list.i_count != 0 )
2954 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
2956 if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
2963 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
2965 demux_sys_t *p_sys = p_demux->p_sys;
2967 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
2970 msg_Warn( p_demux, "Switching to non DVB mode" );
2972 /* This doesn't look like a DVB stream so don't try
2973 * parsing the SDT/EDT/TDT */
2975 for( int i = 0x11; i <= 0x14; i++ )
2977 if( i == 0x13 ) continue;
2978 ts_pid_t *p_pid = &p_sys->pid[i];
2982 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2983 if( dvbpsi_decoder_present( p_pid->psi->handle ))
2984 dvbpsi_DetachDemux( p_pid->psi->handle );
2985 dvbpsi_delete( p_pid->psi->handle );
2987 dvbpsi_DetachDemux( p_pid->psi->handle );
2991 p_pid->b_valid = false;
2993 SetPIDFilter( p_demux, i, false );
2995 p_sys->b_dvb_meta = false;
2998 #include "dvb-text.h"
3000 static char *EITConvertToUTF8( demux_t *p_demux,
3001 const unsigned char *psz_instring,
3005 demux_sys_t *p_sys = p_demux->p_sys;
3007 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3009 if ( !p_sys->arib.p_instance )
3010 p_sys->arib.p_instance = arib_instance_new( p_demux );
3011 if ( !p_sys->arib.p_instance )
3013 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3017 char *psz_outstring = NULL;
3020 i_out = i_length * 4;
3021 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3022 if( !psz_outstring )
3025 arib_initialize_decoder( p_decoder );
3026 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3027 psz_outstring, i_out );
3028 arib_finalize_decoder( p_decoder );
3030 return psz_outstring;
3035 /* Deal with no longer broken providers (no switch byte
3036 but sending ISO_8859-1 instead of ISO_6937) without
3037 removing them from the broken providers table
3038 (keep the entry for correctly handling recorded TS).
3040 b_broken = b_broken && i_length && *psz_instring > 0x20;
3043 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3044 return vlc_from_EIT( psz_instring, i_length );
3047 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3049 demux_sys_t *p_sys = p_demux->p_sys;
3050 ts_pid_t *sdt = &p_sys->pid[0x11];
3051 dvbpsi_sdt_service_t *p_srv;
3053 msg_Dbg( p_demux, "SDTCallBack called" );
3055 if( sdt->psi->i_sdt_version != -1 &&
3056 ( !p_sdt->b_current_next ||
3057 p_sdt->i_version == sdt->psi->i_sdt_version ) )
3059 dvbpsi_DeleteSDT( p_sdt );
3063 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3065 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3070 p_sdt->i_version, p_sdt->b_current_next,
3071 p_sdt->i_network_id );
3073 p_sys->b_broken_charset = false;
3075 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3078 dvbpsi_descriptor_t *p_dr;
3080 const char *psz_type = NULL;
3081 const char *psz_status = NULL;
3083 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3084 "running=%d free_ca=%d",
3085 p_srv->i_service_id, p_srv->b_eit_schedule,
3086 p_srv->b_eit_present, p_srv->i_running_status,
3089 p_meta = vlc_meta_New();
3090 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3092 if( p_dr->i_tag == 0x48 )
3094 static const char *ppsz_type[17] = {
3096 "Digital television service",
3097 "Digital radio sound service",
3099 "NVOD reference service",
3100 "NVOD time-shifted service",
3103 "SECAM coded signal",
3106 "NTSC coded signal",
3107 "Data broadcast service",
3108 "Reserved for Common Interface Usage",
3109 "RCS Map (see EN 301 790 [35])",
3110 "RCS FLS (see EN 301 790 [35])",
3113 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3117 /* Workarounds for broadcasters with broken EPG */
3119 if( p_sdt->i_network_id == 133 )
3120 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3122 /* List of providers using ISO8859-1 */
3123 static const char ppsz_broken_providers[][8] = {
3124 "CSAT", /* CanalSat FR */
3125 "GR1", /* France televisions */
3127 "MR5", /* France 2/M6 HD */
3130 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3132 const size_t i_length = strlen(ppsz_broken_providers[i]);
3133 if( pD->i_service_provider_name_length == i_length &&
3134 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3135 p_sys->b_broken_charset = true;
3138 /* FIXME: Digital+ ES also uses ISO8859-1 */
3140 str1 = EITConvertToUTF8(p_demux,
3141 pD->i_service_provider_name,
3142 pD->i_service_provider_name_length,
3143 p_sys->b_broken_charset );
3144 str2 = EITConvertToUTF8(p_demux,
3146 pD->i_service_name_length,
3147 p_sys->b_broken_charset );
3149 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3150 pD->i_service_type, str1, str2 );
3152 vlc_meta_SetTitle( p_meta, str2 );
3153 vlc_meta_SetPublisher( p_meta, str1 );
3154 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3155 psz_type = ppsz_type[pD->i_service_type];
3161 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3163 static const char *ppsz_status[5] = {
3166 "Starts in a few seconds",
3170 psz_status = ppsz_status[p_srv->i_running_status];
3174 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3176 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3178 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3179 p_srv->i_service_id, p_meta );
3180 vlc_meta_Delete( p_meta );
3183 sdt->psi->i_sdt_version = p_sdt->i_version;
3184 dvbpsi_DeleteSDT( p_sdt );
3187 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3188 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3190 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3194 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3195 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3198 /* Count the number of days */
3199 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3200 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3201 for( int i = 70; i < i_year; i++ )
3202 i_day += LEAP(1900+i);
3204 i_day += LEAP(1900+i_year);
3207 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3210 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3212 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3213 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3214 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3216 *p_y = 1900 + yp + c*1;
3217 *p_m = mp - 1 - c*12;
3218 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3220 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3221 static int64_t EITConvertStartTime( uint64_t i_date )
3223 const int i_mjd = i_date >> 24;
3224 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3225 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3226 const int i_second = CVT_FROM_BCD(i_date );
3231 /* if all 40 bits are 1, the start is unknown */
3232 if( i_date == UINT64_C(0xffffffffff) )
3235 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3236 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3238 static int EITConvertDuration( uint32_t i_duration )
3240 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3241 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3242 CVT_FROM_BCD(i_duration );
3246 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3248 demux_sys_t *p_sys = p_demux->p_sys;
3250 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3252 dvbpsi_DeleteTOT(p_tdt);
3256 static void EITCallBack( demux_t *p_demux,
3257 dvbpsi_eit_t *p_eit, bool b_current_following )
3259 demux_sys_t *p_sys = p_demux->p_sys;
3260 dvbpsi_eit_event_t *p_evt;
3263 msg_Dbg( p_demux, "EITCallBack called" );
3264 if( !p_eit->b_current_next )
3266 dvbpsi_DeleteEIT( p_eit );
3270 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3271 "ts_id=%d network_id=%d segment_last_section_number=%d "
3273 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3276 p_eit->i_service_id,
3278 p_eit->i_version, p_eit->b_current_next,
3279 p_eit->i_ts_id, p_eit->i_network_id,
3280 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3282 p_epg = vlc_epg_New( NULL );
3283 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3285 dvbpsi_descriptor_t *p_dr;
3286 char *psz_name = NULL;
3287 char *psz_text = NULL;
3288 char *psz_extra = strdup("");
3292 int64_t i_tot_time = 0;
3294 i_start = EITConvertStartTime( p_evt->i_start_time );
3295 i_duration = EITConvertDuration( p_evt->i_duration );
3297 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3299 if( p_sys->i_tdt_delta == 0 )
3300 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3302 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3304 tzset(); // JST -> UTC
3305 i_start += timezone; // FIXME: what about DST?
3306 i_tot_time += timezone;
3308 if( p_evt->i_running_status == 0x00 &&
3309 (i_start - 5 < i_tot_time &&
3310 i_tot_time < i_start + i_duration + 5) )
3312 p_evt->i_running_status = 0x04;
3313 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3317 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3318 "running=%d free_ca=%d",
3319 p_evt->i_event_id, (int)i_start, (int)i_duration,
3320 p_evt->i_running_status, p_evt->b_free_ca );
3322 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3328 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3330 /* Only take first description, as we don't handle language-info
3332 if( pE && psz_name == NULL )
3334 psz_name = EITConvertToUTF8( p_demux,
3335 pE->i_event_name, pE->i_event_name_length,
3336 p_sys->b_broken_charset );
3338 psz_text = EITConvertToUTF8( p_demux,
3339 pE->i_text, pE->i_text_length,
3340 p_sys->b_broken_charset );
3341 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3342 pE->i_iso_639_code, psz_name, psz_text );
3349 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3352 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3354 pE->i_descriptor_number, pE->i_last_descriptor_number );
3356 if( pE->i_text_length > 0 )
3358 char *psz_text = EITConvertToUTF8( p_demux,
3359 pE->i_text, pE->i_text_length,
3360 p_sys->b_broken_charset );
3363 msg_Dbg( p_demux, " - text='%s'", psz_text );
3365 psz_extra = xrealloc( psz_extra,
3366 strlen(psz_extra) + strlen(psz_text) + 1 );
3367 strcat( psz_extra, psz_text );
3372 for( int i = 0; i < pE->i_entry_count; i++ )
3374 char *psz_dsc = EITConvertToUTF8( p_demux,
3375 pE->i_item_description[i],
3376 pE->i_item_description_length[i],
3377 p_sys->b_broken_charset );
3378 char *psz_itm = EITConvertToUTF8( p_demux,
3379 pE->i_item[i], pE->i_item_length[i],
3380 p_sys->b_broken_charset );
3382 if( psz_dsc && psz_itm )
3384 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3386 psz_extra = xrealloc( psz_extra,
3387 strlen(psz_extra) + strlen(psz_dsc) +
3388 strlen(psz_itm) + 3 + 1 );
3389 strcat( psz_extra, "(" );
3390 strcat( psz_extra, psz_dsc );
3391 strcat( psz_extra, " " );
3392 strcat( psz_extra, psz_itm );
3393 strcat( psz_extra, ")" );
3405 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
3408 for ( int i = 0; i < pR->i_ratings_number; i++ )
3410 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
3411 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
3413 if ( p_rating->i_rating + 3 > i_min_age )
3414 i_min_age = p_rating->i_rating + 3;
3415 msg_Dbg( p_demux, " - parental control set to %d years",
3424 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3430 if( i_start > 0 && psz_name && psz_text)
3431 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3432 *psz_extra ? psz_extra : NULL, i_min_age );
3434 /* Update "now playing" field */
3435 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
3436 vlc_epg_SetCurrent( p_epg, i_start );
3443 if( p_epg->i_event > 0 )
3445 if( b_current_following &&
3446 ( p_sys->i_current_program == -1 ||
3447 p_sys->i_current_program ==
3448 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3455 p_sys->i_dvb_length = 0;
3456 p_sys->i_dvb_start = 0;
3458 if( p_epg->p_current )
3460 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
3461 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
3464 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
3465 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3468 p_eit->i_service_id,
3472 vlc_epg_Delete( p_epg );
3474 dvbpsi_DeleteEIT( p_eit );
3476 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3478 EITCallBack( p_demux, p_eit, true );
3480 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3482 EITCallBack( p_demux, p_eit, false );
3485 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3486 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
3487 uint16_t i_extension, demux_t *p_demux )
3489 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
3490 uint8_t i_table_id, uint16_t i_extension )
3495 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3496 i_table_id, i_table_id, i_extension, i_extension );
3498 if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
3500 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3501 i_table_id, i_table_id, i_extension, i_extension );
3502 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3503 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
3504 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
3506 dvbpsi_AttachSDT( h, i_table_id, i_extension,
3507 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
3510 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3511 ( i_table_id == 0x4e || /* Current/Following */
3512 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3514 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3515 i_table_id, i_table_id, i_extension, i_extension );
3517 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3518 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3519 (dvbpsi_eit_callback)EITCallBackSchedule;
3520 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3521 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
3522 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
3524 dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
3527 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3528 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
3530 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3531 i_table_id, i_table_id, i_extension, i_extension );
3532 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3533 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
3534 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
3536 dvbpsi_AttachTOT( h, i_table_id, i_extension,
3537 (dvbpsi_tot_callback)TDTCallBack, p_demux);
3542 /*****************************************************************************
3543 * PMT callback and helpers
3544 *****************************************************************************/
3545 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3548 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3549 while( p_dr && ( p_dr->i_tag != i_tag ) )
3550 p_dr = p_dr->p_next;
3553 static bool PMTEsHasRegistration( demux_t *p_demux,
3554 const dvbpsi_pmt_es_t *p_es,
3555 const char *psz_tag )
3557 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3561 if( p_dr->i_length < 4 )
3563 msg_Warn( p_demux, "invalid Registration Descriptor" );
3567 assert( strlen(psz_tag) == 4 );
3568 return !memcmp( p_dr->p_data, psz_tag, 4 );
3571 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
3572 int i_component_tag )
3574 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3577 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3581 return p_si->i_component_tag == i_component_tag;
3584 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3585 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3587 es_format_t *p_fmt = &pid->es->fmt;
3589 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
3590 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3592 if( p_dr && p_dr->i_length == 2 )
3594 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3596 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
3598 pid->es->p_mpeg4desc = NULL;
3600 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
3602 iod_descriptor_t *iod = prg->iod;
3603 if( iod->es_descr[i].i_es_id == i_es_id )
3605 if ( iod->es_descr[i].b_ok )
3606 pid->es->p_mpeg4desc = &iod->es_descr[i];
3608 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
3613 if( !pid->es->p_mpeg4desc )
3615 switch( p_es->i_type )
3617 /* non fatal, set by packetizer */
3618 case 0x0f: /* ADTS */
3619 case 0x11: /* LOAS */
3620 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
3621 pid->i_pid, p_es->i_type );
3624 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
3625 pid->i_pid, p_es->i_type );
3631 const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
3632 if( dcd->i_streamType == 0x04 ) /* VisualStream */
3634 p_fmt->i_cat = VIDEO_ES;
3635 switch( dcd->i_objectTypeIndication )
3637 case 0x0B: /* mpeg4 sub */
3638 p_fmt->i_cat = SPU_ES;
3639 p_fmt->i_codec = VLC_CODEC_SUBT;
3642 case 0x20: /* mpeg4 */
3643 p_fmt->i_codec = VLC_CODEC_MP4V;
3645 case 0x21: /* h264 */
3646 p_fmt->i_codec = VLC_CODEC_H264;
3653 case 0x65: /* mpeg2 */
3654 p_fmt->i_codec = VLC_CODEC_MPGV;
3656 case 0x6a: /* mpeg1 */
3657 p_fmt->i_codec = VLC_CODEC_MPGV;
3659 case 0x6c: /* mpeg1 */
3660 p_fmt->i_codec = VLC_CODEC_JPEG;
3663 p_fmt->i_cat = UNKNOWN_ES;
3667 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
3669 p_fmt->i_cat = AUDIO_ES;
3670 switch( dcd->i_objectTypeIndication )
3672 case 0x40: /* mpeg4 */
3673 p_fmt->i_codec = VLC_CODEC_MP4A;
3677 case 0x68: /* mpeg2 aac */
3678 p_fmt->i_codec = VLC_CODEC_MP4A;
3680 case 0x69: /* mpeg2 */
3681 p_fmt->i_codec = VLC_CODEC_MPGA;
3683 case 0x6b: /* mpeg1 */
3684 p_fmt->i_codec = VLC_CODEC_MPGA;
3687 p_fmt->i_cat = UNKNOWN_ES;
3693 p_fmt->i_cat = UNKNOWN_ES;
3696 if( p_fmt->i_cat != UNKNOWN_ES )
3698 p_fmt->i_extra = dcd->i_extra;
3699 if( p_fmt->i_extra > 0 )
3701 p_fmt->p_extra = malloc( p_fmt->i_extra );
3702 if( p_fmt->p_extra )
3703 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
3716 } ts_teletext_page_t;
3718 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
3719 const dvbpsi_pmt_es_t *p_es )
3721 es_format_t *p_fmt = &pid->es->fmt;
3723 ts_teletext_page_t p_page[2 * 64 + 20];
3724 unsigned i_page = 0;
3726 /* Gather pages information */
3727 #if defined _DVBPSI_DR_56_H_ && \
3728 defined DVBPSI_VERSION && DVBPSI_VERSION_INT > DVBPSI_VERSION_WANTED(0,1,5)
3729 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
3731 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
3735 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
3739 for( int i = 0; i < p_sub->i_pages_number; i++ )
3741 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
3743 if( p_src->i_teletext_type >= 0x06 )
3746 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3748 ts_teletext_page_t *p_dst = &p_page[i_page++];
3750 p_dst->i_type = p_src->i_teletext_type;
3751 p_dst->i_magazine = p_src->i_teletext_magazine_number
3752 ? p_src->i_teletext_magazine_number : 8;
3753 p_dst->i_page = p_src->i_teletext_page_number;
3754 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3759 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3762 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3763 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3765 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
3767 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
3770 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3772 ts_teletext_page_t *p_dst = &p_page[i_page++];
3774 switch( p_src->i_subtitling_type )
3777 p_dst->i_type = 0x02;
3780 p_dst->i_type = 0x03;
3783 /* FIXME check if it is the right split */
3784 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
3785 ? (p_src->i_composition_page_id >> 8) : 8;
3786 p_dst->i_page = p_src->i_composition_page_id & 0xff;
3787 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3792 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
3794 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3796 p_fmt->subs.teletext.i_magazine = -1;
3797 p_fmt->subs.teletext.i_page = 0;
3798 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
3800 dvbpsi_descriptor_t *p_dr;
3801 p_dr = PMTEsFindDescriptor( p_es, 0x46 );
3803 p_dr = PMTEsFindDescriptor( p_es, 0x56 );
3805 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3807 /* Descriptor pass-through */
3808 p_fmt->p_extra = malloc( p_dr->i_length );
3809 if( p_fmt->p_extra )
3811 p_fmt->i_extra = p_dr->i_length;
3812 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3818 for( unsigned i = 0; i < i_page; i++ )
3829 p_es = malloc( sizeof(*p_es) );
3833 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3834 free( p_es->fmt.psz_language );
3835 free( p_es->fmt.psz_description );
3836 p_es->fmt.psz_language = NULL;
3837 p_es->fmt.psz_description = NULL;
3840 p_es->p_data = NULL;
3841 p_es->i_data_size = 0;
3842 p_es->i_data_gathered = 0;
3843 p_es->pp_last = &p_es->p_data;
3844 p_es->data_type = TS_ES_DATA_PES;
3845 p_es->p_mpeg4desc = NULL;
3847 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3851 const ts_teletext_page_t *p = &p_page[i];
3852 p_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
3853 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
3854 p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
3855 p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
3856 p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
3857 p_es->fmt.subs.teletext.i_page = p->i_page;
3860 " * ttxt type=%s lan=%s page=%d%02x",
3861 p_es->fmt.psz_description,
3862 p_es->fmt.psz_language,
3863 p->i_magazine, p->i_page );
3867 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
3868 const dvbpsi_pmt_es_t *p_es )
3870 es_format_t *p_fmt = &pid->es->fmt;
3872 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
3874 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3876 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3877 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3879 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
3880 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
3881 ( i_type >= 0x20 && i_type <= 0x24 ) )
3885 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3887 p_fmt->subs.dvb.i_id = -1;
3888 p_fmt->psz_description = strdup( _("DVB subtitles") );
3890 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3892 /* Descriptor pass-through */
3893 p_fmt->p_extra = malloc( p_dr->i_length );
3894 if( p_fmt->p_extra )
3896 p_fmt->i_extra = p_dr->i_length;
3897 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3903 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3914 p_es = malloc( sizeof(*p_es) );
3918 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3919 free( p_es->fmt.psz_language );
3920 free( p_es->fmt.psz_description );
3921 p_es->fmt.psz_language = NULL;
3922 p_es->fmt.psz_description = NULL;
3925 p_es->p_data = NULL;
3926 p_es->i_data_size = 0;
3927 p_es->i_data_gathered = 0;
3928 p_es->pp_last = &p_es->p_data;
3929 p_es->data_type = TS_ES_DATA_PES;
3930 p_es->p_mpeg4desc = NULL;
3932 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3936 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
3937 p_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
3938 switch( p->i_subtitling_type )
3940 case 0x10: /* unspec. */
3941 case 0x11: /* 4:3 */
3942 case 0x12: /* 16:9 */
3943 case 0x13: /* 2.21:1 */
3944 case 0x14: /* HD monitor */
3945 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
3947 case 0x20: /* Hearing impaired unspec. */
3948 case 0x21: /* h.i. 4:3 */
3949 case 0x22: /* h.i. 16:9 */
3950 case 0x23: /* h.i. 2.21:1 */
3951 case 0x24: /* h.i. HD monitor */
3952 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
3959 p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
3960 ( p->i_ancillary_page_id << 16 );
3965 static int vlc_ceil_log2( const unsigned int val )
3967 int n = 31 - clz(val);
3968 if ((1U << n) != val)
3974 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
3978 /* default mapping */
3979 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
3980 memcpy(h.stream_map, map, sizeof(map));
3984 int stream_count = 0;
3985 int ccc = p[1]; // channel_config_code
3989 mapping = channels > 2;
3992 channels = 2; // dual mono
3994 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
3995 csc = p_csc[channels - 1];
3996 stream_count = channels - csc;
3998 static const uint8_t map[6][7] = {
4007 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4008 } else if (ccc == 0x81) {
4010 goto explicit_config_too_short;
4017 bs_init(&s, &p[4], len - 4);
4020 int bits = vlc_ceil_log2(channels);
4021 if (s.i_left < bits)
4022 goto explicit_config_too_short;
4023 stream_count = bs_read(&s, bits) + 1;
4024 bits = vlc_ceil_log2(stream_count + 1);
4025 if (s.i_left < bits)
4026 goto explicit_config_too_short;
4027 csc = bs_read(&s, bits);
4029 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4030 if (s.i_left < channels * channel_bits)
4031 goto explicit_config_too_short;
4033 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4034 for (int i = 0; i < channels; i++) {
4035 unsigned char m = bs_read(&s, channel_bits);
4038 h.stream_map[i] = m;
4041 } else if (ccc >= 0x80 && ccc <= 0x88) {
4042 channels = ccc - 0x80;
4047 channels = 2; // dual mono
4050 stream_count = channels;
4052 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4056 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4060 opus_prepare_header(channels, 0, &h);
4062 h.input_sample_rate = 48000;
4064 h.nb_streams = channels - csc;
4065 h.channel_mapping = mapping;
4068 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4069 if (p_fmt->p_extra) {
4070 p_fmt->i_cat = AUDIO_ES;
4071 p_fmt->i_codec = VLC_CODEC_OPUS;
4072 p_fmt->audio.i_channels = h.channels;
4073 p_fmt->audio.i_rate = 48000;
4079 explicit_config_too_short:
4080 msg_Err(demux, "Opus descriptor too short");
4083 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
4084 const dvbpsi_pmt_es_t *p_es )
4086 es_format_t *p_fmt = &pid->es->fmt;
4087 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_es, 0x59 );
4088 dvbpsi_descriptor_t *desc;
4090 if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
4091 PMTEsFindDescriptor( p_es, 0x6a ) ||
4092 PMTEsFindDescriptor( p_es, 0x81 ) )
4094 p_fmt->i_cat = AUDIO_ES;
4095 p_fmt->i_codec = VLC_CODEC_A52;
4097 else if( (desc = PMTEsFindDescriptor( p_es, 0x7f ) ) && desc->i_length >= 2 &&
4098 PMTEsHasRegistration(p_demux, p_es, "Opus"))
4100 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4102 else if( PMTEsFindDescriptor( p_es, 0x7a ) )
4104 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4105 p_fmt->i_cat = AUDIO_ES;
4106 p_fmt->i_codec = VLC_CODEC_EAC3;
4108 else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
4109 PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
4110 PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
4111 PMTEsFindDescriptor( p_es, 0x73 ) )
4113 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4114 p_fmt->i_cat = AUDIO_ES;
4115 p_fmt->i_codec = VLC_CODEC_DTS;
4117 else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) && !p_subs_dr )
4119 /* BSSD is AES3 DATA, but could also be subtitles
4120 * we need to check for secondary descriptor then s*/
4121 p_fmt->i_cat = AUDIO_ES;
4122 p_fmt->b_packetized = true;
4123 p_fmt->i_codec = VLC_CODEC_302M;
4125 else if( PMTEsHasRegistration( p_demux, p_es, "HEVC" ) )
4127 p_fmt->i_cat = VIDEO_ES;
4128 p_fmt->i_codec = VLC_CODEC_HEVC;
4130 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4132 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4133 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xFD );
4134 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4135 if ( p_dr && p_dr->i_length >= 2 )
4137 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4138 PMTEsHasComponentTag( p_es, 0x30 ) ||
4139 PMTEsHasComponentTag( p_es, 0x31 ) ||
4140 PMTEsHasComponentTag( p_es, 0x32 ) ||
4141 PMTEsHasComponentTag( p_es, 0x33 ) ||
4142 PMTEsHasComponentTag( p_es, 0x34 ) ||
4143 PMTEsHasComponentTag( p_es, 0x35 ) ||
4144 PMTEsHasComponentTag( p_es, 0x36 ) ||
4145 PMTEsHasComponentTag( p_es, 0x37 ) ) )
4147 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_A );
4148 p_fmt->psz_language = strndup ( "jpn", 3 );
4149 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4151 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4152 PMTEsHasComponentTag( p_es, 0x87 ) ||
4153 PMTEsHasComponentTag( p_es, 0x88 ) ) )
4155 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_C );
4156 p_fmt->psz_language = strndup ( "jpn", 3 );
4157 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4163 /* Subtitle/Teletext/VBI fallbacks */
4164 dvbpsi_subtitling_dr_t *p_sub;
4165 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4167 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4169 if( p_fmt->i_cat != UNKNOWN_ES )
4172 switch( p_sub->p_subtitle[i].i_subtitling_type )
4174 case 0x01: /* EBU Teletext subtitles */
4175 case 0x02: /* Associated EBU Teletext */
4176 case 0x03: /* VBI data */
4177 PMTSetupEsTeletext( p_demux, pid, p_es );
4179 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4180 case 0x11: /* ... on 4:3 AR monitor */
4181 case 0x12: /* ... on 16:9 AR monitor */
4182 case 0x13: /* ... on 2.21:1 AR monitor */
4183 case 0x14: /* ... for display on a high definition monitor */
4184 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4185 case 0x21: /* ... on 4:3 AR monitor */
4186 case 0x22: /* ... on 16:9 AR monitor */
4187 case 0x23: /* ... on 2.21:1 AR monitor */
4188 case 0x24: /* ... for display on a high definition monitor */
4189 PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
4192 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4193 p_sub->p_subtitle[i].i_subtitling_type );
4199 if( p_fmt->i_cat == UNKNOWN_ES &&
4200 ( PMTEsFindDescriptor( p_es, 0x45 ) || /* VBI Data descriptor */
4201 PMTEsFindDescriptor( p_es, 0x46 ) || /* VBI Teletext descriptor */
4202 PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
4205 PMTSetupEsTeletext( p_demux, pid, p_es );
4209 /* FIXME is it useful ? */
4210 if( PMTEsFindDescriptor( p_es, 0x52 ) )
4212 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4213 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4215 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4219 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
4220 const dvbpsi_pmt_es_t *p_es )
4222 /* Registration Descriptor */
4223 if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
4225 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4229 es_format_t *p_fmt = &pid->es->fmt;
4231 /* registration descriptor for VC-1 (SMPTE rp227) */
4232 p_fmt->i_cat = VIDEO_ES;
4233 p_fmt->i_codec = VLC_CODEC_VC1;
4235 /* XXX With Simple and Main profile the SEQUENCE
4236 * header is modified: video width and height are
4237 * inserted just after the start code as 2 int16_t
4238 * The packetizer will take care of that. */
4241 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
4242 const dvbpsi_pmt_es_t *p_es )
4244 /* Registration Descriptor */
4245 if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
4247 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4251 es_format_t *p_fmt = &pid->es->fmt;
4253 /* registration descriptor for Dirac
4254 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4255 p_fmt->i_cat = VIDEO_ES;
4256 p_fmt->i_codec = VLC_CODEC_DIRAC;
4259 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
4260 const dvbpsi_pmt_es_t *p_es )
4262 /* MSCODEC sent by vlc */
4263 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
4264 if( !p_dr || p_dr->i_length < 10 )
4267 "private MSCODEC (vlc) without bih private descriptor" );
4271 es_format_t *p_fmt = &pid->es->fmt;
4272 p_fmt->i_cat = VIDEO_ES;
4273 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4274 p_dr->p_data[2], p_dr->p_data[3] );
4275 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4276 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4277 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4279 if( p_fmt->i_extra > 0 )
4281 p_fmt->p_extra = malloc( p_fmt->i_extra );
4282 if( p_fmt->p_extra )
4283 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4284 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4288 /* For such stream we will gather them ourself and don't launch a
4290 * Yes it's ugly but it's the only way to have DIV3 working */
4291 p_fmt->b_packetized = true;
4294 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pid_t *pid )
4296 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4297 * with audio track pid being 0x1100..0x11FF */
4298 if ( p_pmt->i_program_number == 0x1 &&
4299 p_pmt->i_pcr_pid == 0x1000 &&
4300 ( pid->i_pid >> 8 ) == 0x11 )
4302 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4303 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4306 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4309 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
4310 const dvbpsi_pmt_es_t *p_es )
4312 es_format_t *p_fmt = &pid->es->fmt;
4314 /* Blu-Ray mapping */
4315 switch( p_es->i_type )
4318 p_fmt->i_cat = AUDIO_ES;
4319 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4322 case 0x85: /* DTS-HD High resolution audio */
4323 case 0x86: /* DTS-HD Master audio */
4324 case 0xA2: /* Secondary DTS audio */
4325 p_fmt->i_cat = AUDIO_ES;
4326 p_fmt->i_codec = VLC_CODEC_DTS;
4329 case 0x83: /* TrueHD AC3 */
4330 p_fmt->i_cat = AUDIO_ES;
4331 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4334 case 0x84: /* E-AC3 */
4335 case 0xA1: /* Secondary E-AC3 */
4336 p_fmt->i_cat = AUDIO_ES;
4337 p_fmt->i_codec = VLC_CODEC_EAC3;
4339 case 0x90: /* Presentation graphics */
4340 p_fmt->i_cat = SPU_ES;
4341 p_fmt->i_codec = VLC_CODEC_BD_PG;
4343 case 0x91: /* Interactive graphics */
4344 case 0x92: /* Subtitle */
4347 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4348 p_es->i_pid, p_es->i_type );
4355 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
4356 const dvbpsi_pmt_es_t *p_es )
4362 vlc_fourcc_t i_codec;
4364 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4365 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4366 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4367 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4368 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4369 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4370 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4371 { "", UNKNOWN_ES, 0 }
4373 es_format_t *p_fmt = &pid->es->fmt;
4375 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4377 if( PMTEsHasRegistration( p_demux, p_es, p_regs[i].psz_tag ) )
4379 p_fmt->i_cat = p_regs[i].i_cat;
4380 p_fmt->i_codec = p_regs[i].i_codec;
4381 if (p_es->i_type == 0x87)
4382 p_fmt->i_codec = VLC_CODEC_EAC3;
4389 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4391 static const char *audio_type[] = {
4393 N_("clean effects"),
4394 N_("hearing impaired"),
4395 N_("visual impaired commentary"),
4398 if (type < 0 || type > 3)
4399 msg_Dbg( p_demux, "unknown audio type: %d", type);
4401 return strdup(audio_type[type]);
4405 static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
4406 const dvbpsi_pmt_es_t *p_es )
4408 /* get language descriptor */
4409 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
4414 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4417 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4421 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4422 pid->es->fmt.psz_language = malloc( 4 );
4423 if( pid->es->fmt.psz_language )
4425 memcpy( pid->es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
4426 pid->es->fmt.psz_language[3] = 0;
4427 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
4429 int type = p_decoded->code[0].i_audio_type;
4430 pid->es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
4432 pid->es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
4434 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
4435 if( pid->es->fmt.i_extra_languages > 0 )
4436 pid->es->fmt.p_extra_languages =
4437 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
4438 pid->es->fmt.i_extra_languages );
4439 if( pid->es->fmt.p_extra_languages )
4441 for( int i = 0; i < pid->es->fmt.i_extra_languages; i++ )
4443 pid->es->fmt.p_extra_languages[i].psz_language = malloc(4);
4444 if( pid->es->fmt.p_extra_languages[i].psz_language )
4446 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
4447 p_decoded->code[i+1].iso_639_code, 3 );
4448 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
4450 int type = p_decoded->code[i].i_audio_type;
4451 pid->es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
4455 pid->es->fmt.psz_language = malloc( 4 );
4456 if( pid->es->fmt.psz_language )
4458 memcpy( pid->es->fmt.psz_language,
4459 p_decoded->i_iso_639_code, 3 );
4460 pid->es->fmt.psz_language[3] = 0;
4465 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
4467 demux_t *p_demux = data;
4468 demux_sys_t *p_sys = p_demux->p_sys;
4470 ts_pid_t *pmt = NULL;
4473 msg_Dbg( p_demux, "PMTCallBack called" );
4475 /* First find this PMT declared in PAT */
4476 for( int i = 0; !pmt && i < p_sys->i_pmt; i++ )
4477 for( int i_prg = 0; !pmt && i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
4479 const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
4480 if( i_pmt_number != TS_USER_PMT_NUMBER &&
4481 i_pmt_number == p_pmt->i_program_number )
4483 pmt = p_sys->pmt[i];
4484 prg = p_sys->pmt[i]->psi->prg[i_prg];
4490 msg_Warn( p_demux, "unreferenced program (broken stream)" );
4491 dvbpsi_DeletePMT(p_pmt);
4496 if( prg->i_version != -1 &&
4497 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
4499 dvbpsi_DeletePMT( p_pmt );
4503 ts_pid_t **pp_clean = NULL;
4505 /* Clean this program (remove all es) */
4506 for( int i = 0; i < 8192; i++ )
4508 ts_pid_t *pid = &p_sys->pid[i];
4510 if( pid->b_valid && pid->p_owner == pmt->psi &&
4511 pid->i_owner_number == prg->i_number && pid->psi == NULL )
4513 TAB_APPEND( i_clean, pp_clean, pid );
4518 IODFree( prg->iod );
4522 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
4523 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
4524 prg->i_pid_pcr = p_pmt->i_pcr_pid;
4525 prg->i_version = p_pmt->i_version;
4527 ValidateDVBMeta( p_demux, prg->i_pid_pcr );
4528 if( ProgramIsSelected( p_demux, prg->i_number ) )
4529 SetPIDFilter( p_demux, prg->i_pid_pcr, true ); /* Set demux filter */
4531 /* Parse PMT descriptors */
4532 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
4533 dvbpsi_descriptor_t *p_dr;
4535 /* First pass for standard detection */
4536 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
4538 int i_arib_flags = 0; /* Descriptors can be repeated */
4539 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
4545 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
4546 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
4550 i_arib_flags |= 1 << 1;
4553 i_arib_flags |= 1 << 2;
4559 if ( i_arib_flags == 0x07 ) //0b111
4560 p_sys->arib.e_mode = ARIBMODE_ENABLED;
4563 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
4565 /* special descriptors handling */
4568 case 0x1d: /* We have found an IOD descriptor */
4569 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
4570 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
4574 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
4575 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
4578 case 0x5: /* Registration Descriptor */
4579 if( p_dr->i_length != 4 )
4581 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
4585 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
4586 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
4587 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
4592 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
4596 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
4599 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
4600 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
4601 /* TODO: apply visibility flags */
4605 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
4608 dvbpsi_pmt_es_t *p_es;
4609 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
4611 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
4613 /* Find out if the PID was already declared */
4614 for( int i = 0; i < i_clean; i++ )
4616 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
4618 old_pid = pp_clean[i];
4622 ValidateDVBMeta( p_demux, p_es->i_pid );
4624 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
4626 msg_Warn( p_demux, " * PMT error: pid=%d already defined",
4631 char const * psz_typedesc = "";
4632 switch(p_es->i_type)
4635 psz_typedesc = "ISO/IEC Reserved";
4638 psz_typedesc = "ISO/IEC 11172 Video";
4641 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
4644 psz_typedesc = "ISO/IEC 11172 Audio";
4647 psz_typedesc = "ISO/IEC 13818-3 Audio";
4650 psz_typedesc = "ISO/IEC 13818-1 private_sections";
4653 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
4656 psz_typedesc = "ISO/IEC 13522 MHEG";
4659 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
4662 psz_typedesc = "ITU-T Rec. H.222.1";
4665 psz_typedesc = "ISO/IEC 13818-6 type A";
4668 psz_typedesc = "ISO/IEC 13818-6 type B";
4671 psz_typedesc = "ISO/IEC 13818-6 type C";
4674 psz_typedesc = "ISO/IEC 13818-6 type D";
4677 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
4680 if (p_es->i_type >= 0x0F && p_es->i_type <=0x7F)
4681 psz_typedesc = "ISO/IEC 13818-1 Reserved";
4683 psz_typedesc = "User Private";
4686 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
4687 p_es->i_pid, p_es->i_type, psz_typedesc );
4689 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
4690 p_dr = p_dr->p_next )
4692 msg_Dbg( p_demux, " - descriptor tag 0x%x",
4696 PIDInit( pid, false, pmt->psi );
4697 PIDFillFormat( &pid->es->fmt, p_es->i_type );
4698 pid->i_owner_number = prg->i_number;
4699 pid->i_pid = p_es->i_pid;
4700 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
4703 bool b_registration_applied = false;
4704 if ( p_es->i_type >= 0x80 ) /* non standard, extensions */
4706 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
4708 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, pid, p_es ) ))
4709 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
4710 p_es->i_pid, p_es->i_type );
4714 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, pid, p_es ) ))
4715 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
4716 p_es->i_pid, p_es->i_type );
4720 if ( !b_registration_applied )
4722 switch( p_es->i_type )
4725 /* Handle PES private data */
4726 PMTSetupEs0x06( p_demux, pid, p_es );
4728 /* All other private or reserved types */
4733 PMTSetupEsISO14496( p_demux, pid, prg, p_es );
4737 PMTSetupEs0x83( p_pmt, pid );
4740 PMTSetupEs0xA0( p_demux, pid, p_es );
4743 PMTSetupEs0xD1( p_demux, pid, p_es );
4746 PMTSetupEs0xEA( p_demux, pid, p_es );
4752 if( pid->es->fmt.i_cat == AUDIO_ES ||
4753 ( pid->es->fmt.i_cat == SPU_ES &&
4754 pid->es->fmt.i_codec != VLC_CODEC_DVBS &&
4755 pid->es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
4757 PMTParseEsIso639( p_demux, pid, p_es );
4760 switch( pid->es->fmt.i_codec )
4762 case VLC_CODEC_SCTE_27:
4763 pid->es->data_type = TS_ES_DATA_TABLE_SECTION;
4766 //pid->es->data_type = TS_ES_DATA_PES;
4770 pid->es->fmt.i_group = p_pmt->i_program_number;
4771 for( int i = 0; i < pid->i_extra_es; i++ )
4772 pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
4774 if( pid->es->fmt.i_cat == UNKNOWN_ES )
4776 msg_Dbg( p_demux, " => pid %d content is *unknown*",
4781 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
4782 p_es->i_pid, (char*)&pid->es->fmt.i_codec );
4784 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
4786 /* Check if we can avoid restarting the ES */
4788 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
4789 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
4790 pid->es->fmt.i_extra == 0 &&
4791 pid->i_extra_es == old_pid->i_extra_es &&
4792 ( ( !pid->es->fmt.psz_language &&
4793 !old_pid->es->fmt.psz_language ) ||
4794 ( pid->es->fmt.psz_language &&
4795 old_pid->es->fmt.psz_language &&
4796 !strcmp( pid->es->fmt.psz_language,
4797 old_pid->es->fmt.psz_language ) ) ) )
4799 pid->i_cc = old_pid->i_cc;
4800 ts_es_t *e = pid->es;
4801 pid->es = old_pid->es;
4803 for( int i = 0; i < pid->i_extra_es; i++ )
4805 e = pid->extra_es[i];
4806 pid->extra_es[i] = old_pid->extra_es[i];
4807 old_pid->extra_es[i] = e;
4812 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4813 for( int i = 0; i < pid->i_extra_es; i++ )
4815 pid->extra_es[i]->id =
4816 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
4818 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4822 /* Add ES to the list */
4825 PIDClean( p_demux, old_pid );
4826 TAB_REMOVE( i_clean, pp_clean, old_pid );
4828 p_sys->pid[p_es->i_pid] = *pid;
4830 p_dr = PMTEsFindDescriptor( p_es, 0x09 );
4831 if( p_dr && p_dr->i_length >= 2 )
4833 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
4834 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
4837 if( ProgramIsSelected( p_demux, prg->i_number ) && pid->es->id != NULL )
4838 SetPIDFilter( p_demux, p_es->i_pid, true ); /* Set demux filter */
4841 /* Set CAM descrambling */
4842 if( !ProgramIsSelected( p_demux, prg->i_number ) )
4844 dvbpsi_DeletePMT( p_pmt );
4846 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
4847 p_pmt ) != VLC_SUCCESS )
4849 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED )
4851 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
4852 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
4853 if (!p_sys->arib.b25stream)
4854 dvbpsi_DeletePMT( p_pmt );
4855 } else dvbpsi_DeletePMT( p_pmt );
4858 for( int i = 0; i < i_clean; i++ )
4860 if( ProgramIsSelected( p_demux, prg->i_number ) )
4861 SetPIDFilter( p_demux, pp_clean[i]->i_pid, false );
4863 PIDClean( p_demux, pp_clean[i] );
4869 static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
4871 demux_t *p_demux = data;
4872 demux_sys_t *p_sys = p_demux->p_sys;
4873 dvbpsi_pat_program_t *p_program;
4874 ts_pid_t *pat = &p_sys->pid[0];
4876 msg_Dbg( p_demux, "PATCallBack called" );
4878 if( ( pat->psi->i_pat_version != -1 &&
4879 ( !p_pat->b_current_next ||
4880 p_pat->i_version == pat->psi->i_pat_version ) ) ||
4883 dvbpsi_DeletePAT( p_pat );
4887 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
4888 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
4891 if( p_sys->i_pmt > 0 )
4894 ts_pid_t **pmt_rm = NULL;
4896 /* Search pmt to be deleted */
4897 for( int i = 0; i < p_sys->i_pmt; i++ )
4899 ts_pid_t *pmt = p_sys->pmt[i];
4900 bool b_keep = false;
4902 for( p_program = p_pat->p_first_program; !b_keep && p_program;
4903 p_program = p_program->p_next )
4905 if( p_program->i_pid != pmt->i_pid )
4908 for( int i_prg = 0; !b_keep && i_prg < pmt->psi->i_prg; i_prg++ )
4909 if( p_program->i_number == pmt->psi->prg[i_prg]->i_number )
4916 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
4919 /* Delete all ES attached to thoses PMT */
4920 for( int i = 2; i < 8192; i++ )
4922 ts_pid_t *pid = &p_sys->pid[i];
4924 if( !pid->b_valid || pid->psi )
4927 for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
4929 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
4931 /* We only remove es that aren't defined by extra pmt */
4932 if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
4936 SetPIDFilter( p_demux, i, false );
4938 PIDClean( p_demux, pid );
4944 /* Delete PMT pid */
4945 for( int i = 0; i < i_pmt_rm; i++ )
4947 ts_pid_t *pid = pmt_rm[i];
4948 SetPIDFilter( p_demux, pid->i_pid, false );
4950 for( int i_prg = 0; i_prg < pid->psi->i_prg; i_prg++ )
4952 const int i_number = pid->psi->prg[i_prg]->i_number;
4953 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
4956 PIDClean( p_demux, &p_sys->pid[pid->i_pid] );
4957 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
4963 /* now create programs */
4964 for( p_program = p_pat->p_first_program; p_program != NULL;
4965 p_program = p_program->p_next )
4967 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
4969 if( p_program->i_number == 0 )
4972 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
4974 ValidateDVBMeta( p_demux, p_program->i_pid );
4979 for( int i_prg = 0; b_add && i_prg < pmt->psi->i_prg; i_prg++ )
4980 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
4988 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
4991 PIDInit( pmt, true, pat->psi );
4992 ts_prg_psi_t *prg = pmt->psi->prg[pmt->psi->i_prg-1];
4993 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
4994 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
4997 dvbpsi_DeletePAT( p_pat );
5000 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
5001 if( !dvbpsi_pmt_attach( prg->handle, p_program->i_number, PMTCallBack, p_demux ) )
5002 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5003 p_program->i_number );
5005 prg->handle = dvbpsi_AttachPMT( p_program->i_number, PMTCallBack, p_demux );
5007 prg->i_number = p_program->i_number;
5008 prg->i_pid_pmt = p_program->i_pid;
5010 /* Now select PID at access level */
5011 if( ProgramIsSelected( p_demux, p_program->i_number ) )
5013 if( p_sys->i_current_program == 0 )
5014 p_sys->i_current_program = p_program->i_number;
5016 if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
5017 p_sys->b_access_control = false;
5020 pat->psi->i_pat_version = p_pat->i_version;
5022 dvbpsi_DeletePAT( p_pat );