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"
65 #include "../mux/mpeg/streams.h"
66 #include "../mux/mpeg/tsutil.h"
67 #include "../mux/mpeg/tables.h"
69 #include "../codec/opus_header.h"
74 VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
79 vfprintf(stderr, format, ap);
87 #include <aribb24/aribb24.h>
88 #include <aribb24/decoder.h>
91 typedef enum arib_modes_e
94 ARIBMODE_DISABLED = 0,
98 /*****************************************************************************
100 *****************************************************************************/
101 static int Open ( vlc_object_t * );
102 static void Close ( vlc_object_t * );
105 * - Rename "extra pmt" to "user pmt"
106 * - Update extra pmt description
107 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
108 * where pid_description could take 3 forms:
109 * 1. pid:pcr (to force the pcr pid)
111 * 3. pid:type=fourcc where type=(video|audio|spu)
113 #define PMT_TEXT N_("Extra PMT")
114 #define PMT_LONGTEXT N_( \
115 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
117 #define PID_TEXT N_("Set id of ES to PID")
118 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
119 " handled by VLC to the same value as the PID in" \
120 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
121 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
123 #define CSA_TEXT N_("CSA Key")
124 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
125 "16 char string (8 hexadecimal bytes).")
127 #define CSA2_TEXT N_("Second CSA Key")
128 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
129 "16 char string (8 hexadecimal bytes).")
132 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
133 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
134 "The decryption routines subtract the TS-header from the value before " \
137 #define SPLIT_ES_TEXT N_("Separate sub-streams")
138 #define SPLIT_ES_LONGTEXT N_( \
139 "Separate teletex/dvbs pages into independent ES. " \
140 "It can be useful to turn off this option when using stream output." )
142 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
143 #define SEEK_PERCENT_LONGTEXT N_( \
144 "Seek and position based on a percent byte position, not a PCR generated " \
145 "time position. If seeking doesn't work property, turn on this option." )
147 #define PCR_TEXT N_("Trust in-stream PCR")
148 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
150 static const int const arib_mode_list[] =
151 { ARIBMODE_AUTO, ARIBMODE_ENABLED, ARIBMODE_DISABLED };
152 static const char *const arib_mode_list_text[] =
153 { N_("Auto"), N_("Enabled"), N_("Disabled") };
155 #define SUPPORT_ARIB_TEXT N_("ARIB STD-B24 mode")
156 #define SUPPORT_ARIB_LONGTEXT N_( \
157 "Forces ARIB STD-B24 mode for decoding characters." \
158 "This feature affects EPG information and subtitles." )
161 set_description( N_("MPEG Transport Stream demuxer") )
162 set_shortname ( "MPEG-TS" )
163 set_category( CAT_INPUT )
164 set_subcategory( SUBCAT_INPUT_DEMUX )
166 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
167 add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
169 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
171 add_obsolete_string( "ts-out" ) /* since 2.2.0 */
172 add_obsolete_integer( "ts-out-mtu" ) /* since 2.2.0 */
173 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
175 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
177 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
180 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
181 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
183 add_integer( "ts-arib", ARIBMODE_AUTO, SUPPORT_ARIB_TEXT, SUPPORT_ARIB_LONGTEXT, false )
184 change_integer_list( arib_mode_list, arib_mode_list_text )
186 add_obsolete_bool( "ts-silent" );
188 set_capability( "demux", 10 )
189 set_callbacks( Open, Close )
193 /*****************************************************************************
195 *****************************************************************************/
196 static const char *const ppsz_teletext_type[] = {
199 N_("Teletext subtitles"),
200 N_("Teletext: additional information"),
201 N_("Teletext: program schedule"),
202 N_("Teletext subtitles: hearing impaired")
207 uint8_t i_objectTypeIndication;
208 uint8_t i_streamType;
213 } decoder_config_descriptor_t;
222 decoder_config_descriptor_t dec_descr;
224 } es_mpeg4_descriptor_t;
226 #define ES_DESCRIPTOR_COUNT 255
232 es_mpeg4_descriptor_t es_descr[ES_DESCRIPTOR_COUNT];
238 dvbpsi_handle handle;
244 /* IOD stuff (mpeg4) */
245 iod_descriptor_t *iod;
251 /* for special PAT/SDT case */
252 dvbpsi_handle handle; /* PAT/SDT/EIT */
265 TS_ES_DATA_TABLE_SECTION
270 TS_PMT_REGISTRATION_NONE = 0,
271 TS_PMT_REGISTRATION_HDMV
272 } ts_pmt_registration_type_t;
278 ts_es_data_type_t data_type;
284 es_mpeg4_descriptor_t *p_mpeg4desc;
294 int i_cc; /* countinuity counter */
297 /* PSI owner (ie PMT -> PAT, ES -> PMT */
305 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
311 vlc_fourcc_t i_fourcc;
322 #define MIN_ES_PID 4 /* Should be 32.. broken muxers */
323 #define MAX_ES_PID 8190
329 vlc_mutex_t csa_lock;
331 /* TS packet size (188, 192, 204) */
334 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
335 int i_packet_header_size;
337 /* how many TS packet we read at once */
340 /* to determine length and time */
343 mtime_t i_current_pcr;
345 bool b_force_seek_per_percent;
354 arib_instance_t *p_instance;
367 bool b_delay_es_creation;
379 bool b_access_control;
385 int64_t i_dvb_length;
386 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
389 int i_current_program;
390 vlc_list_t programs_list;
394 /* broken PCR handling */
395 bool b_program_pcr_seen;
401 mtime_t i_first_dts; /* first dts encountered for the stream */
402 int i_timesourcepid; /* which pid we saved the dts from */
403 bool b_pat_deadline; /* set if we haven't seen PAT within 140ms */
412 static int Demux ( demux_t *p_demux );
413 static int Control( demux_t *p_demux, int i_query, va_list args );
415 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
416 static void PIDClean( demux_t *, ts_pid_t *pid );
417 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
419 static void PATCallBack( void*, dvbpsi_pat_t * );
420 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
421 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
422 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
423 uint16_t i_extension, demux_t * );
425 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
426 uint8_t i_table_id, uint16_t i_extension );
429 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
431 static inline int PIDGet( block_t *p )
433 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
436 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
437 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid );
439 static block_t* ReadTSPacket( demux_t *p_demux );
440 static int Seek( demux_t *p_demux, double f_percent );
441 static void GetFirstPCR( demux_t *p_demux );
442 static void GetLastPCR( demux_t *p_demux );
443 static void CheckPCR( demux_t *p_demux );
444 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
445 static void PCRFixHandle( demux_t *, block_t * );
446 static mtime_t AdjustPTSWrapAround( demux_t *, mtime_t );
448 static void IODFree( iod_descriptor_t * );
450 #define TS_USER_PMT_NUMBER (0)
451 static int UserPmt( demux_t *p_demux, const char * );
453 static int SetPIDFilter( demux_t *, int i_pid, bool b_selected );
454 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
456 #define TS_PACKET_SIZE_188 188
457 #define TS_PACKET_SIZE_192 192
458 #define TS_PACKET_SIZE_204 204
459 #define TS_PACKET_SIZE_MAX 204
461 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size, int i_offset )
463 const uint8_t *p_peek;
465 if( stream_Peek( p_demux->s,
466 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
469 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
471 if( p_peek[i_offset + i_sync] != 0x47 )
474 /* Check next 3 sync bytes */
475 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
476 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
478 msg_Err( p_demux, "cannot peek" );
481 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
482 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
483 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
485 return TS_PACKET_SIZE_188;
487 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
488 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
489 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
493 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
495 return TS_PACKET_SIZE_192;
497 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
498 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
499 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
501 return TS_PACKET_SIZE_204;
505 if( p_demux->b_force )
507 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
508 return TS_PACKET_SIZE_188;
510 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
514 #define TOPFIELD_HEADER_SIZE 3712
516 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, int *pi_header_size, vdr_info_t *p_vdr )
518 const uint8_t *p_peek;
520 int i_packet_size = -1;
522 if( stream_Peek( p_demux->s,
523 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
526 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
527 p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
528 stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
529 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
531 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
532 if( i_packet_size != -1 )
534 msg_Dbg( p_demux, "this is a topfield file" );
536 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
537 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
538 * but after the filename the offsets seem to be incorrect. - DJ */
539 int i_duration, i_name;
540 char *psz_name = xmalloc(25);
541 char *psz_event_name;
542 char *psz_event_text = xmalloc(130);
543 char *psz_ext_text = xmalloc(1025);
545 // 2 bytes version Uimsbf (4,5)
546 // 2 bytes reserved (6,7)
547 // 2 bytes duration in minutes Uimsbf (8,9(
548 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
549 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
550 // 2 bytes service number in channel list (10, 11)
551 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
552 // 4 bytes of reserved + tuner info (14,15,16,17)
553 // 2 bytes of Service ID Bslbf (18,19)
554 // 2 bytes of PMT PID Uimsbf (20,21)
555 // 2 bytes of PCR PID Uimsbf (22,23)
556 // 2 bytes of Video PID Uimsbf (24,25)
557 // 2 bytes of Audio PID Uimsbf (26,27)
558 // 24 bytes filename Bslbf
559 memcpy( psz_name, &p_peek[28], 24 );
561 msg_Dbg( p_demux, "recordingname=%s", psz_name );
562 // 1 byte of sat index Uimsbf (52)
563 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
564 // 4 bytes of freq. Uimsbf (56,57,58,59)
565 // 2 bytes of symbol rate Uimsbf (60,61)
566 // 2 bytes of TS stream ID Uimsbf (62,63)
569 // 2 bytes duration Uimsbf (70,71)
570 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
571 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
572 // 4 bytes EventID Uimsbf (72-75)
573 // 8 bytes of Start and End time info (76-83)
574 // 1 byte reserved (84)
575 // 1 byte event name length Uimsbf (89)
576 i_name = (int)(p_peek[89]&~0x81);
577 msg_Dbg( p_demux, "event name length = %d", i_name);
578 psz_event_name = xmalloc( i_name+1 );
579 // 1 byte parental rating (90)
580 // 129 bytes of event text
581 memcpy( psz_event_name, &p_peek[91], i_name );
582 psz_event_name[i_name] = '\0';
583 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
584 psz_event_text[129-i_name] = '\0';
585 msg_Dbg( p_demux, "event name=%s", psz_event_name );
586 msg_Dbg( p_demux, "event text=%s", psz_event_text );
587 // 12 bytes reserved (220)
589 // 2 bytes Event Text Length Uimsbf
590 // 4 bytes EventID Uimsbf
591 // FIXME We just have 613 bytes. not enough for this entire text
592 // 1024 bytes Extended Event Text Bslbf
593 memcpy( psz_ext_text, p_peek+372, 1024 );
594 psz_ext_text[1024] = '\0';
595 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
596 // 52 bytes reserved Bslbf
598 p_vdr->i_service = GetWBE(&p_peek[18]);
600 return i_packet_size;
601 //return TS_PACKET_SIZE_188;
605 return DetectPacketSize( p_demux, pi_header_size, 0 );
608 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
609 static void vlc_dvbpsi_reset( demux_t *p_demux )
611 demux_sys_t *p_sys = p_demux->p_sys;
613 ts_pid_t *pat = &p_sys->pid[0];
614 ts_pid_t *sdt = &p_sys->pid[0x11];
615 ts_pid_t *eit = &p_sys->pid[0x12];
616 ts_pid_t *tdt = &p_sys->pid[0x14];
618 if( pat->psi->handle )
620 if( dvbpsi_decoder_present( pat->psi->handle ) )
621 dvbpsi_pat_detach( pat->psi->handle );
622 dvbpsi_delete( pat->psi->handle );
623 pat->psi->handle = NULL;
626 if( sdt->psi->handle )
628 if( dvbpsi_decoder_present( sdt->psi->handle ) )
629 dvbpsi_DetachDemux( sdt->psi->handle );
630 dvbpsi_delete( sdt->psi->handle );
631 sdt->psi->handle = NULL;
634 if( eit->psi->handle )
636 if( dvbpsi_decoder_present( eit->psi->handle ) )
637 dvbpsi_DetachDemux( eit->psi->handle );
638 dvbpsi_delete( eit->psi->handle );
639 eit->psi->handle = NULL;
642 if( tdt->psi->handle )
644 if( dvbpsi_decoder_present( tdt->psi->handle ) )
645 dvbpsi_DetachDemux( tdt->psi->handle );
646 dvbpsi_delete( tdt->psi->handle );
647 tdt->psi->handle = NULL;
652 static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
654 return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
655 (mtime_t)(p_data[1] << 22)|
656 ((mtime_t)(p_data[2]&0xfe) << 14)|
657 (mtime_t)(p_data[3] << 7)|
658 (mtime_t)(p_data[4] >> 1);
661 static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, bool b_adaptfield )
663 demux_sys_t *p_sys = p_demux->p_sys;
664 const uint8_t *p_pes = p_pesstart;
665 pid->probed.i_type = -1;
669 uint8_t len = *p_pes;
674 p_pes++; /* stuffing */
678 pid->probed.b_haspcr = ( *p_pes >= 7 && (p_pes[1] & 0x10) );
683 if( p_pes - p_pesstart >= TS_PACKET_SIZE_188 - 9)
686 if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
689 size_t i_pesextoffset = 8;
691 if( p_pes[7] & 0x80 ) // PTS
694 i_dts = ExtractPESTimestamp( &p_pes[9] );
696 if( p_pes[7] & 0x40 ) // DTS
699 i_dts = ExtractPESTimestamp( &p_pes[14] );
701 if( p_pes[7] & 0x20 ) // ESCR
703 if( p_pes[7] & 0x10 ) // ESrate
705 if( p_pes[7] & 0x08 ) // DSM
707 if( p_pes[7] & 0x04 ) // CopyInfo
709 if( p_pes[7] & 0x02 ) // PESCRC
712 /* HeaderdataLength */
713 const size_t i_payloadoffset = 8 + 1 + p_pes[8];
716 if( p_pes[7] & 0x01 ) // PESExt
718 size_t i_extension2_offset = 1;
719 if ( p_pes[i_pesextoffset] & 0x80 ) // private data
720 i_extension2_offset += 16;
721 if ( p_pes[i_pesextoffset] & 0x40 ) // pack
722 i_extension2_offset += 1;
723 if ( p_pes[i_pesextoffset] & 0x20 ) // seq
724 i_extension2_offset += 2;
725 if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
726 i_extension2_offset += 2;
727 if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
729 uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
730 i_extension2_offset += i_len;
733 /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
735 if( &p_pes[i_payloadoffset] - p_pesstart >= TS_PACKET_SIZE_188 - 4)
738 const uint8_t *p_data = &p_pes[i_payloadoffset];
740 if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
742 if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
744 pid->probed.i_type = 0x06;
745 pid->probed.i_fourcc = VLC_CODEC_DTS;
747 else if( !memcmp( p_data, "\x0B\x77", 2 ) )
749 pid->probed.i_type = 0x06;
750 pid->probed.i_fourcc = VLC_CODEC_EAC3;
752 else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
754 switch(p_data[1] & 18)
756 /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
757 11 - MPEG Version 1 (ISO/IEC 11172-3) */
759 pid->probed.i_type = 0x04;
762 pid->probed.i_type = 0x03;
767 switch(p_data[1] & 6)
773 pid->probed.i_type = 0x04;
774 pid->probed.i_fourcc = VLC_CODEC_MPGA;
777 pid->probed.i_type = 0x04;
778 pid->probed.i_fourcc = VLC_CODEC_MP2;
781 pid->probed.i_type = 0x04;
782 pid->probed.i_fourcc = VLC_CODEC_MP3;
789 else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
791 if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
793 pid->probed.i_type = 0x1b;
794 pid->probed.i_fourcc = VLC_CODEC_H264;
796 else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
798 pid->probed.i_type = 0x02;
799 pid->probed.i_fourcc = VLC_CODEC_MPGV;
803 /* Track timestamps and flag missing PAT */
804 if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
806 p_sys->patfix.i_first_dts = i_dts;
807 p_sys->patfix.i_timesourcepid = pid->i_pid;
809 else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
810 !p_sys->patfix.b_pat_deadline )
812 if( i_dts - p_sys->patfix.i_first_dts > 9 * 140000 / 100 )
813 p_sys->patfix.b_pat_deadline = true;
818 static void BuildPATCallback( void *p_opaque, block_t *p_block )
820 ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
821 dvbpsi_PushPacket( pat_pid->psi->handle, p_block->p_buffer );
824 static void BuildPMTCallback( void *p_opaque, block_t *p_block )
826 ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
829 for( int i_prg = 0; i_prg < program_pid->psi->i_prg; i_prg++ )
831 dvbpsi_PushPacket( program_pid->psi->prg[i_prg]->handle,
834 p_block = p_block->p_next;
838 static void MissingPATPMTFixup( demux_t *p_demux )
840 demux_sys_t *p_sys = p_demux->p_sys;
841 int i_program_number = 1234;
842 int i_program_pid = 1337;
843 int i_pcr_pid = 0x1FFF;
846 if( p_sys->pid[i_program_pid].b_seen )
848 /* Find a free one */
849 for( i_program_pid = MIN_ES_PID;
850 i_program_pid <= MAX_ES_PID && p_sys->pid[i_program_pid].b_seen;
854 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
856 if( !p_sys->pid[i].b_seen ||
857 p_sys->pid[i].probed.i_type == -1 )
860 if( i_pcr_pid == 0x1FFF && ( p_sys->pid[i].probed.i_type == 0x03 ||
861 p_sys->pid[i].probed.b_haspcr ) )
862 i_pcr_pid = p_sys->pid[i].i_pid;
870 ts_stream_t patstream =
873 .i_continuity_counter = 0x10,
874 .b_discontinuity = false
877 ts_stream_t pmtprogramstream =
879 .i_pid = i_program_pid,
880 .i_continuity_counter = 0x0,
881 .b_discontinuity = false
884 BuildPAT( DVBPSI_HANDLE_PARAM(p_sys->pid[0].psi->handle)
885 &p_sys->pid[0], BuildPATCallback,
888 1, &pmtprogramstream, &i_program_number );
890 if( !p_sys->pid[i_program_pid].psi )
892 msg_Err( p_demux, "PAT creation failed" );
901 es_format_t esfmt = {0};
902 struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
903 pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
904 if( esstreams && mapped )
907 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
909 if( !p_sys->pid[i].b_seen ||
910 p_sys->pid[i].probed.i_type == -1 )
913 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
914 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
915 esstreams[j].ts.i_pid = p_sys->pid[i].i_pid;
916 mapped[j].pes = &esstreams[j].pes;
917 mapped[j].ts = &esstreams[j].ts;
918 mapped[j].fmt = &esfmt;
922 BuildPMT( DVBPSI_HANDLE_PARAM(p_sys->pid[0].psi->handle) VLC_OBJECT(p_demux),
923 &p_sys->pid[i_program_pid], BuildPMTCallback,
927 1, &pmtprogramstream, &i_program_number,
934 /*****************************************************************************
936 *****************************************************************************/
937 static int Open( vlc_object_t *p_this )
939 demux_t *p_demux = (demux_t*)p_this;
942 int i_packet_size, i_packet_header_size = 0;
945 vdr_info_t vdr = {0};
947 /* Search first sync byte */
948 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
949 if( i_packet_size < 0 )
952 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
955 memset( p_sys, 0, sizeof( demux_sys_t ) );
956 vlc_mutex_init( &p_sys->csa_lock );
958 p_demux->pf_demux = Demux;
959 p_demux->pf_control = Control;
961 /* Init p_sys field */
962 p_sys->b_dvb_meta = true;
963 p_sys->b_access_control = true;
964 p_sys->i_current_program = 0;
965 p_sys->programs_list.i_count = 0;
966 p_sys->programs_list.p_values = NULL;
967 p_sys->i_tdt_delta = 0;
968 p_sys->i_dvb_start = 0;
969 p_sys->i_dvb_length = 0;
973 p_sys->arib.b25stream = NULL;
974 p_sys->stream = p_demux->s;
976 p_sys->b_broken_charset = false;
978 for( int i = 0; i < 8192; i++ )
980 ts_pid_t *pid = &p_sys->pid[i];
983 pid->b_valid = false;
984 pid->probed.i_fourcc = 0;
985 pid->probed.i_type = 0;
987 /* PID 8191 is padding */
988 p_sys->pid[8191].b_seen = true;
989 p_sys->i_packet_size = i_packet_size;
990 p_sys->i_packet_header_size = i_packet_header_size;
991 p_sys->i_ts_read = 50;
993 p_sys->b_start_record = false;
995 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
996 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
998 (table)->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG ); \
999 if( ! (table)->psi->handle ) \
1001 vlc_mutex_destroy( &p_sys->csa_lock ); \
1003 return VLC_ENOMEM; \
1005 (table)->psi->handle->p_sys = (void *) VLC_OBJECT(obj); \
1006 if( !dvbpsi_AttachDemux( (table)->psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
1008 vlc_dvbpsi_reset( obj ); \
1009 vlc_mutex_destroy( &p_sys->csa_lock ); \
1011 return VLC_EGENERIC; \
1016 /* Init PAT handler */
1017 pat = &p_sys->pid[0];
1018 PIDInit( pat, true, NULL );
1019 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1020 pat->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
1021 if( !pat->psi->handle )
1023 vlc_mutex_destroy( &p_sys->csa_lock );
1027 pat->psi->handle->p_sys = (void *) p_demux;
1028 if( !dvbpsi_pat_attach( pat->psi->handle, PATCallBack, p_demux ) )
1030 vlc_dvbpsi_reset( p_demux );
1031 vlc_mutex_destroy( &p_sys->csa_lock );
1033 return VLC_EGENERIC;
1036 pat->psi->handle = dvbpsi_AttachPAT( PATCallBack, p_demux );
1038 if( p_sys->b_dvb_meta )
1040 ts_pid_t *sdt = &p_sys->pid[0x11];
1041 ts_pid_t *eit = &p_sys->pid[0x12];
1043 PIDInit( sdt, true, NULL );
1044 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1045 VLC_DVBPSI_DEMUX_TABLE_INIT( sdt, p_demux )
1048 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
1051 PIDInit( eit, true, NULL );
1052 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1053 VLC_DVBPSI_DEMUX_TABLE_INIT( eit, p_demux )
1056 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
1059 ts_pid_t *tdt = &p_sys->pid[0x14];
1060 PIDInit( tdt, true, NULL );
1061 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1062 VLC_DVBPSI_DEMUX_TABLE_INIT( tdt, p_demux )
1065 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
1069 if( p_sys->b_access_control )
1071 if( SetPIDFilter( p_demux, 0x11, true ) ||
1072 SetPIDFilter( p_demux, 0x14, true ) ||
1073 SetPIDFilter( p_demux, 0x12, true ) )
1074 p_sys->b_access_control = false;
1078 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1079 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
1082 /* Init PMT array */
1083 TAB_INIT( p_sys->i_pmt, p_sys->pmt );
1084 p_sys->i_pmt_es = 0;
1085 p_sys->b_delay_es_creation = !p_sys->b_access_control;
1088 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
1090 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
1092 /* We handle description of an extra PMT */
1093 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
1094 p_sys->b_user_pmt = false;
1095 if( psz_string && *psz_string )
1096 UserPmt( p_demux, psz_string );
1099 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
1100 if( psz_string && *psz_string )
1105 p_sys->csa = csa_New();
1107 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
1108 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
1109 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
1111 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
1113 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1116 else if ( i_res == VLC_SUCCESS )
1118 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1122 csa_Delete( p_sys->csa );
1128 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
1129 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1131 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
1132 if( i_pkt < 4 || i_pkt > 188 )
1134 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
1135 msg_Warn( p_demux, "using default packet size of 188 bytes" );
1136 p_sys->i_csa_pkt_size = 188;
1139 p_sys->i_csa_pkt_size = i_pkt;
1140 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
1146 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
1148 p_sys->b_canseek = false;
1149 p_sys->i_pid_ref_pcr = -1;
1150 p_sys->i_first_pcr = -1;
1151 p_sys->i_current_pcr = -1;
1152 p_sys->i_last_pcr = -1;
1153 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
1154 p_sys->i_pcrs_num = 10;
1155 p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
1156 p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
1158 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
1160 if( !p_sys->p_pcrs || !p_sys->p_pos )
1166 bool b_can_fastseek = false;
1167 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
1168 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &b_can_fastseek );
1169 if ( p_sys->b_canseek )
1171 if( b_can_fastseek )
1173 GetFirstPCR( p_demux );
1174 CheckPCR( p_demux );
1175 GetLastPCR( p_demux );
1178 if( p_sys->i_first_pcr < 0 || p_sys->i_last_pcr < 0 )
1180 msg_Dbg( p_demux, "Force Seek Per Percent: PCR's not found,");
1181 p_sys->b_force_seek_per_percent = true;
1185 while( p_sys->i_pmt_es <= 0
1186 && (!p_sys->pid[0].b_seen && !p_sys->patfix.b_pat_deadline) )
1188 if( Demux( p_demux ) != 1 )
1192 /* If we had no PAT within 140ms, create PAT/PMT from probed streams */
1193 if( p_sys->i_pmt_es == 0 && !p_sys->pid[0].b_seen )
1194 MissingPATPMTFixup( p_demux );
1199 /*****************************************************************************
1201 *****************************************************************************/
1202 static void Close( vlc_object_t *p_this )
1204 demux_t *p_demux = (demux_t*)p_this;
1205 demux_sys_t *p_sys = p_demux->p_sys;
1207 msg_Dbg( p_demux, "pid list:" );
1208 for( int i = 0; i < 8192; i++ )
1210 ts_pid_t *pid = &p_sys->pid[i];
1212 if( pid->b_valid && pid->psi )
1214 switch( pid->i_pid )
1217 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1218 if( dvbpsi_decoder_present( pid->psi->handle ) )
1219 dvbpsi_pat_detach( pid->psi->handle );
1220 dvbpsi_delete( pid->psi->handle );
1221 pid->psi->handle = NULL;
1223 dvbpsi_DetachPAT( pid->psi->handle );
1231 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
1233 /* SDT or EIT or TDT */
1234 dvbpsi_DetachDemux( pid->psi->handle );
1235 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1236 dvbpsi_delete( pid->psi->handle );
1237 pid->psi->handle = NULL;
1243 PIDClean( p_demux, pid );
1248 else if( pid->b_valid && pid->es )
1250 PIDClean( p_demux, pid );
1255 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
1259 if( pid->i_pid > 0 )
1260 SetPIDFilter( p_demux, pid->i_pid, false );
1263 vlc_mutex_lock( &p_sys->csa_lock );
1266 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
1267 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1268 csa_Delete( p_sys->csa );
1270 vlc_mutex_unlock( &p_sys->csa_lock );
1272 TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
1274 free( p_sys->programs_list.p_values );
1276 free( p_sys->p_pcrs );
1277 free( p_sys->p_pos );
1280 if ( p_sys->arib.p_instance )
1281 arib_instance_destroy( p_sys->arib.p_instance );
1284 if ( p_sys->arib.b25stream )
1286 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
1287 stream_Delete( p_sys->arib.b25stream );
1290 vlc_mutex_destroy( &p_sys->csa_lock );
1294 /*****************************************************************************
1295 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
1296 *****************************************************************************/
1297 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
1298 vlc_value_t oldval, vlc_value_t newval,
1301 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1302 demux_t *p_demux = (demux_t*)p_this;
1303 demux_sys_t *p_sys = p_demux->p_sys;
1304 int i_tmp = (intptr_t)p_data;
1306 vlc_mutex_lock( &p_sys->csa_lock );
1308 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
1310 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
1312 vlc_mutex_unlock( &p_sys->csa_lock );
1316 /*****************************************************************************
1318 *****************************************************************************/
1319 static int Demux( demux_t *p_demux )
1321 demux_sys_t *p_sys = p_demux->p_sys;
1322 bool b_wait_es = p_sys->i_pmt_es <= 0;
1324 /* We read at most 100 TS packet or until a frame is completed */
1325 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1327 bool b_frame = false;
1329 if( !(p_pkt = ReadTSPacket( p_demux )) )
1334 if( p_sys->b_start_record )
1336 /* Enable recording once synchronized */
1337 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
1338 p_sys->b_start_record = false;
1341 /* Parse the TS packet */
1342 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1344 /* Probe streams to build PAT/PMT after 140ms in case we don't see any PAT */
1345 if( !p_sys->pid[0].b_seen &&
1346 (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
1347 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1348 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
1350 ProbePES( p_demux, p_pid, p_pkt->p_buffer + 4, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
1353 if( p_pid->b_valid )
1357 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 ) ) )
1359 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1363 for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1365 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1369 block_Release( p_pkt );
1373 if(p_sys->b_delay_es_creation) /* No longer delay ES since that pid's program sends data */
1374 AddAndCreateES( p_demux, NULL );
1375 b_frame = GatherData( p_demux, p_pid, p_pkt );
1380 if( !p_pid->b_seen )
1382 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1384 /* We have to handle PCR if present */
1385 PCRHandle( p_demux, p_pid, p_pkt );
1386 block_Release( p_pkt );
1388 p_pid->b_seen = true;
1390 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1394 demux_UpdateTitleFromStream( p_demux );
1398 /*****************************************************************************
1400 *****************************************************************************/
1401 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1403 demux_sys_t *p_sys = p_demux->p_sys;
1409 if( p_sys->i_dvb_length > 0 )
1411 const int64_t t = mdate() + p_sys->i_tdt_delta;
1413 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1416 *pi_length = p_sys->i_dvb_length;
1418 *pi_time = t - p_sys->i_dvb_start;
1422 return VLC_EGENERIC;
1425 static int Control( demux_t *p_demux, int i_query, va_list args )
1427 demux_sys_t *p_sys = p_demux->p_sys;
1429 bool b_bool, *pb_bool;
1436 case DEMUX_GET_POSITION:
1437 pf = (double*) va_arg( args, double* );
1439 if( p_sys->b_force_seek_per_percent ||
1440 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1441 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 ||
1442 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1444 int64_t i_time, i_length;
1445 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1446 *pf = (double)i_time/(double)i_length;
1447 else if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1449 int64_t offset = stream_Tell( p_sys->stream );
1451 *pf = (double)offset / (double)i64;
1458 *pf = (double)(p_sys->i_current_pcr - p_sys->i_first_pcr) / (double)(p_sys->i_last_pcr - p_sys->i_first_pcr);
1462 case DEMUX_SET_POSITION:
1463 f = (double) va_arg( args, double );
1465 if(!p_sys->b_canseek)
1466 return VLC_EGENERIC;
1468 if( p_sys->b_force_seek_per_percent ||
1469 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1470 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1472 i64 = stream_Size( p_sys->stream );
1473 if( stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) )
1474 return VLC_EGENERIC;
1478 if( Seek( p_demux, f ) )
1480 p_sys->b_force_seek_per_percent = true;
1481 return VLC_EGENERIC;
1486 case DEMUX_GET_TIME:
1487 pi64 = (int64_t*)va_arg( args, int64_t * );
1488 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1489 p_sys->b_force_seek_per_percent ||
1490 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 )
1492 if( DVBEventInformation( p_demux, pi64, NULL ) )
1499 *pi64 = (p_sys->i_current_pcr - p_sys->i_first_pcr) * 100 / 9;
1503 case DEMUX_GET_LENGTH:
1504 pi64 = (int64_t*)va_arg( args, int64_t * );
1505 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1506 p_sys->b_force_seek_per_percent ||
1507 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1509 if( DVBEventInformation( p_demux, NULL, pi64 ) )
1516 *pi64 = (p_sys->i_last_pcr - p_sys->i_first_pcr) * 100 / 9;
1520 case DEMUX_SET_GROUP:
1524 i_int = (int)va_arg( args, int );
1525 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1526 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1528 if( i_int == 0 && p_sys->i_current_program > 0 )
1529 i_int = p_sys->i_current_program;
1531 if( p_sys->i_current_program > 0 )
1533 if( p_sys->i_current_program != i_int )
1534 SetPrgFilter( p_demux, p_sys->i_current_program, false );
1536 else if( p_sys->i_current_program < 0 )
1538 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
1539 SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
1544 p_sys->i_current_program = i_int;
1545 SetPrgFilter( p_demux, p_sys->i_current_program, true );
1547 else if( i_int < 0 )
1549 p_sys->i_current_program = -1;
1550 p_sys->programs_list.i_count = 0;
1553 vlc_list_t *p_dst = &p_sys->programs_list;
1554 free( p_dst->p_values );
1556 p_dst->p_values = calloc( p_list->i_count,
1557 sizeof(*p_dst->p_values) );
1558 if( p_dst->p_values )
1560 p_dst->i_count = p_list->i_count;
1561 for( int i = 0; i < p_list->i_count; i++ )
1563 p_dst->p_values[i] = p_list->p_values[i];
1564 SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
1572 case DEMUX_GET_TITLE_INFO:
1574 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1575 int *c = va_arg( args, int * );
1577 *va_arg( args, int* ) = 0; /* Title offset */
1578 *va_arg( args, int* ) = 0; /* Chapter offset */
1579 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1582 case DEMUX_SET_TITLE:
1583 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1585 case DEMUX_SET_SEEKPOINT:
1586 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1588 case DEMUX_GET_META:
1589 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1591 case DEMUX_CAN_RECORD:
1592 pb_bool = (bool*)va_arg( args, bool * );
1596 case DEMUX_SET_RECORD_STATE:
1597 b_bool = (bool)va_arg( args, int );
1600 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1601 p_sys->b_start_record = b_bool;
1604 case DEMUX_GET_SIGNAL:
1605 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1608 return VLC_EGENERIC;
1612 /*****************************************************************************
1614 *****************************************************************************/
1615 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1617 demux_sys_t *p_sys = p_demux->p_sys;
1618 char *psz_dup = strdup( psz_fmt );
1619 char *psz = psz_dup;
1622 ts_prg_psi_t *prg = NULL;
1628 i_pid = strtol( psz, &psz, 0 );
1629 if( i_pid < 2 || i_pid >= 8192 )
1632 /* Parse optional program number */
1635 i_number = strtol( &psz[1], &psz, 0 );
1638 ts_pid_t *pmt = &p_sys->pid[i_pid];
1640 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1641 PIDInit( pmt, true, NULL );
1644 prg = calloc( 1, sizeof( ts_prg_psi_t ) );
1648 prg->i_pid_pcr = -1;
1649 prg->i_pid_pmt = -1;
1650 prg->i_version = -1;
1651 prg->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1652 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1653 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
1656 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
1657 if( !dvbpsi_pmt_attach( prg->handle,
1658 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1659 PMTCallBack, p_demux ) )
1661 dvbpsi_delete( prg->handle );
1666 prg->handle = dvbpsi_AttachPMT(
1667 ((i_number != TS_USER_PMT_NUMBER) ? i_number : 1),
1668 PMTCallBack, p_demux );
1670 TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1672 psz = strchr( psz, '=' );
1675 while( psz && *psz )
1677 char *psz_next = strchr( psz, ',' );
1683 i_pid = strtol( psz, &psz, 0 );
1684 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1687 char *psz_opt = &psz[1];
1688 if( !strcmp( psz_opt, "pcr" ) )
1690 prg->i_pid_pcr = i_pid;
1692 else if( !p_sys->pid[i_pid].b_valid )
1694 ts_pid_t *pid = &p_sys->pid[i_pid];
1696 char *psz_arg = strchr( psz_opt, '=' );
1700 PIDInit( pid, false, pmt->psi);
1701 if( prg->i_pid_pcr <= 0 )
1702 prg->i_pid_pcr = i_pid;
1704 if( psz_arg && strlen( psz_arg ) == 4 )
1706 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1707 psz_arg[2], psz_arg[3] );
1708 int i_cat = UNKNOWN_ES;
1709 es_format_t *fmt = &pid->es->fmt;
1711 if( !strcmp( psz_opt, "video" ) )
1713 else if( !strcmp( psz_opt, "audio" ) )
1715 else if( !strcmp( psz_opt, "spu" ) )
1718 es_format_Init( fmt, i_cat, i_codec );
1719 fmt->b_packetized = false;
1723 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1724 PIDFillFormat( &pid->es->fmt, i_stream_type );
1726 pid->es->fmt.i_group = i_number;
1727 if( p_sys->b_es_id_pid )
1728 pid->es->fmt.i_id = i_pid;
1730 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1732 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1733 (char*)&pid->es->fmt.i_codec );
1734 pid->es->id = es_out_Add( p_demux->out,
1744 p_sys->b_user_pmt = true;
1745 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1752 return VLC_EGENERIC;
1755 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1757 demux_sys_t *p_sys = p_demux->p_sys;
1759 if( !p_sys->b_access_control )
1760 return VLC_EGENERIC;
1762 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1763 i_pid, b_selected );
1766 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1768 demux_sys_t *p_sys = p_demux->p_sys;
1769 ts_prg_psi_t *p_prg = NULL;
1772 p_sys->b_disable_pcr = !p_sys->b_trust_pcr;
1774 /* Search pmt to be unselected */
1775 for( int i = 0; i < p_sys->i_pmt; i++ )
1777 ts_pid_t *pmt = p_sys->pmt[i];
1779 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1781 if( pmt->psi->prg[i_prg]->i_number == i_prg_id )
1783 i_pmt_pid = p_sys->pmt[i]->i_pid;
1784 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1791 if( i_pmt_pid <= 0 )
1795 SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1796 if( p_prg->i_pid_pcr > 0 )
1797 SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
1800 for( int i = 2; i < 8192; i++ )
1802 ts_pid_t *pid = &p_sys->pid[i];
1804 if( !pid->b_valid || pid->psi )
1807 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1809 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1811 /* We only remove/select es that aren't defined by extra pmt */
1812 SetPIDFilter( p_demux, i, b_selected );
1819 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1821 bool b_old_valid = pid->b_valid;
1823 pid->b_valid = true;
1825 pid->b_scrambled = false;
1826 pid->p_owner = p_owner;
1827 pid->i_owner_number = 0;
1829 TAB_INIT( pid->i_extra_es, pid->extra_es );
1837 pid->psi = xmalloc( sizeof( ts_psi_t ) );
1838 pid->psi->handle = NULL;
1839 TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1843 pid->psi->i_pat_version = -1;
1844 pid->psi->i_sdt_version = -1;
1847 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1851 prg->i_version = -1;
1853 prg->i_pid_pcr = -1;
1854 prg->i_pid_pmt = -1;
1855 prg->i_pcr_value= -1;
1859 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1865 pid->es = calloc( 1, sizeof( ts_es_t ) );
1869 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1870 pid->es->data_type = TS_ES_DATA_PES;
1871 pid->es->pp_last = &pid->es->p_data;
1875 static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
1877 demux_sys_t *p_sys = p_demux->p_sys;
1878 es_out_t *out = p_demux->out;
1882 if( pid->psi->handle )
1884 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1885 if( dvbpsi_decoder_present( pid->psi->handle ) )
1886 dvbpsi_pmt_detach( pid->psi->handle );
1887 dvbpsi_delete( pid->psi->handle );
1888 pid->psi->handle = NULL;
1890 dvbpsi_DetachPMT( pid->psi->handle );
1893 for( int i = 0; i < pid->psi->i_prg; i++ )
1895 if( pid->psi->prg[i]->iod )
1896 IODFree( pid->psi->prg[i]->iod );
1897 if( pid->psi->prg[i]->handle )
1899 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1900 if( dvbpsi_decoder_present( pid->psi->prg[i]->handle ) )
1901 dvbpsi_pmt_detach( pid->psi->prg[i]->handle );
1902 dvbpsi_delete( pid->psi->prg[i]->handle );
1904 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1907 free( pid->psi->prg[i] );
1909 free( pid->psi->prg );
1916 es_out_Del( out, pid->es->id );
1920 if( pid->es->p_data )
1921 block_ChainRelease( pid->es->p_data );
1923 es_format_Clean( &pid->es->fmt );
1927 for( int i = 0; i < pid->i_extra_es; i++ )
1929 if( pid->extra_es[i]->id )
1931 es_out_Del( out, pid->extra_es[i]->id );
1935 if( pid->extra_es[i]->p_data )
1936 block_ChainRelease( pid->extra_es[i]->p_data );
1938 es_format_Clean( &pid->extra_es[i]->fmt );
1940 free( pid->extra_es[i] );
1942 if( pid->i_extra_es )
1943 free( pid->extra_es );
1946 pid->b_valid = false;
1949 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1954 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1965 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1967 block_t *out = NULL;
1968 block_t **last = NULL;
1970 uint8_t *buf = block->p_buffer;
1971 size_t len = block->i_buffer;
1973 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
1974 int16_t start_trim = 0, end_trim = 0;
1975 int start_trim_flag = (buf[1] >> 4) & 1;
1976 int end_trim_flag = (buf[1] >> 3) & 1;
1977 int control_extension_flag = (buf[1] >> 2) & 1;
1982 unsigned au_size = 0;
1990 if (start_trim_flag) {
1991 start_trim = read_opus_flag(&buf, &len);
1992 if (start_trim < 0) {
1993 msg_Err(demux, "Invalid start trimming flag");
1996 if (end_trim_flag) {
1997 end_trim = read_opus_flag(&buf, &len);
1999 msg_Err(demux, "Invalid end trimming flag");
2002 if (control_extension_flag && len) {
2003 unsigned l = *buf++; len--;
2005 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
2012 if (!au_size || au_size > len) {
2013 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
2017 block_t *au = block_Alloc(au_size);
2020 memcpy(au->p_buffer, buf, au_size);
2021 block_CopyProperties(au, block);
2030 au->i_nb_samples = opus_frame_duration(buf, au_size);
2031 if (end_trim && end_trim <= au->i_nb_samples)
2032 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
2036 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
2037 au->i_nb_samples -= start_trim;
2038 if (au->i_nb_samples == 0)
2039 au->i_flags |= BLOCK_FLAG_PREROLL;
2046 block_Release(block);
2050 /****************************************************************************
2052 ****************************************************************************/
2053 static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header,
2054 unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
2058 switch( p_header[3] )
2060 case 0xBC: /* Program stream map */
2061 case 0xBE: /* Padding */
2062 case 0xBF: /* Private stream 2 */
2063 case 0xF0: /* ECM */
2064 case 0xF1: /* EMM */
2065 case 0xFF: /* Program stream directory */
2066 case 0xF2: /* DSMCC stream */
2067 case 0xF8: /* ITU-T H.222.1 type E stream */
2071 if( ( p_header[6]&0xC0 ) == 0x80 )
2074 i_skip = p_header[8] + 9;
2076 if( p_header[7]&0x80 ) /* has pts */
2078 *pi_pts = ExtractPESTimestamp( &p_header[9] );
2080 if( p_header[7]&0x40 ) /* has dts */
2081 *pi_dts = ExtractPESTimestamp( &p_header[14] );
2087 while( i_skip < 23 && p_header[i_skip] == 0xff )
2093 msg_Err( p_demux, "too much MPEG-1 stuffing" );
2094 return VLC_EGENERIC;
2096 if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
2101 if( p_header[i_skip]&0x20 )
2103 *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
2105 if( p_header[i_skip]&0x10 ) /* has dts */
2107 *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
2127 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
2129 demux_sys_t *p_sys = p_demux->p_sys;
2131 unsigned i_pes_size = 0;
2132 unsigned i_skip = 0;
2135 mtime_t i_length = 0;
2137 /* FIXME find real max size */
2138 const int i_max = block_ChainExtract( p_pes, header, 34 );
2139 assert(i_max >= 34);
2140 if (unlikely(i_max < 34))
2142 block_ChainRelease( p_pes );
2146 if( pid->b_scrambled || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2148 if ( !pid->b_scrambled )
2149 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
2150 header[0], header[1],header[2],header[3], pid->i_pid );
2151 block_ChainRelease( p_pes );
2155 /* TODO check size */
2156 if( ParsePESHeader( p_demux, (uint8_t*)&header, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
2158 block_ChainRelease( p_pes );
2164 i_pts = AdjustPTSWrapAround( p_demux, i_pts );
2166 i_dts = AdjustPTSWrapAround( p_demux, i_dts );
2169 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
2170 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
2174 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
2175 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
2176 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
2180 else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
2181 pid->es->p_mpeg4desc )
2183 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
2185 if( dcd->i_extra > 2 &&
2186 dcd->p_extra[0] == 0x10 &&
2187 ( dcd->p_extra[1]&0x10 ) )
2189 /* display length */
2190 if( p_pes->i_buffer + 2 <= i_skip )
2191 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
2195 if( p_pes->i_buffer + 2 <= i_skip )
2196 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
2202 while( p_pes && i_skip > 0 )
2204 if( p_pes->i_buffer <= i_skip )
2206 block_t *p_next = p_pes->p_next;
2208 i_skip -= p_pes->i_buffer;
2209 block_Release( p_pes );
2214 p_pes->i_buffer -= i_skip;
2215 p_pes->p_buffer += i_skip;
2220 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
2221 if( i_pts >= 0 && i_dts < 0 )
2229 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
2232 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
2234 p_pes->i_length = i_length * 100 / 9;
2236 p_block = block_ChainGather( p_pes );
2237 if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
2239 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2241 p_block->i_buffer = i_pes_size;
2244 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2247 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2249 else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
2251 if( p_block->i_pts <= VLC_TS_INVALID )
2253 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
2254 * In this case use the last PCR + 40ms */
2255 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
2257 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
2259 mtime_t i_pcr = pid->p_owner->prg[i]->i_pcr_value;
2260 if( i_pcr > VLC_TS_INVALID )
2261 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
2267 else if( pid->es->fmt.i_codec == VLC_CODEC_ARIB_A ||
2268 pid->es->fmt.i_codec == VLC_CODEC_ARIB_C )
2270 if( p_block->i_pts <= VLC_TS_INVALID )
2272 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2274 p_block->i_buffer = i_pes_size;
2277 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2280 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2283 else if( pid->es->fmt.i_codec == VLC_CODEC_OPUS)
2285 p_block = Opus_Parse(p_demux, p_block);
2289 block_t *p_next = p_block->p_next;
2290 p_block->p_next = NULL;
2291 for( int i = 0; i < pid->i_extra_es; i++ )
2293 es_out_Send( p_demux->out, pid->extra_es[i]->id,
2294 block_Duplicate( p_block ) );
2297 PCRFixHandle( p_demux, p_block );
2299 if ( p_sys->b_disable_pcr && p_block->i_dts > VLC_TS_INVALID )
2300 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2301 pid->i_owner_number, p_block->i_dts);
2303 if( !p_sys->b_disable_pcr && p_block->i_dts > VLC_TS_INVALID &&
2304 p_block->i_dts < (VLC_TS_0 + p_sys->i_current_pcr * 100 / 9) )
2305 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64"us later than pcr",
2306 pid->i_pid, (p_sys->i_current_pcr * 100 / 9) - p_block->i_dts + VLC_TS_0 );
2308 es_out_Send( p_demux->out, pid->es->id, p_block );
2315 msg_Warn( p_demux, "empty pes" );
2319 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
2321 block_t *p_content = block_ChainGather( p_data );
2322 mtime_t i_date = -1;
2323 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
2325 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
2327 i_date = pid->p_owner->prg[i]->i_pcr_value;
2334 if( pid->es->fmt.i_codec == VLC_CODEC_SCTE_27 )
2336 /* We need to extract the truncated pts stored inside the payload */
2337 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
2340 size_t i_offset = 4;
2341 if( p_content->p_buffer[3] & 0x40 )
2343 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
2344 p_content->p_buffer[8];
2347 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
2349 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
2352 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
2353 if( i_display_in < i_date )
2354 i_date = i_display_in + (1ll << 32);
2356 i_date = i_display_in;
2363 p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
2365 es_out_Send( p_demux->out, pid->es->id, p_content );
2367 PCRFixHandle( p_demux, p_content );
2369 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
2371 block_t *p_data = pid->es->p_data;
2373 /* remove the pes from pid */
2374 pid->es->p_data = NULL;
2375 pid->es->i_data_size = 0;
2376 pid->es->i_data_gathered = 0;
2377 pid->es->pp_last = &pid->es->p_data;
2379 if( pid->es->data_type == TS_ES_DATA_PES )
2381 ParsePES( p_demux, pid, p_data );
2383 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
2385 ParseTableSection( p_demux, pid, p_data );
2389 block_ChainRelease( p_data );
2393 static block_t* ReadTSPacket( demux_t *p_demux )
2395 demux_sys_t *p_sys = p_demux->p_sys;
2399 /* Get a new TS packet */
2400 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2402 if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
2403 msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
2405 msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
2409 /* Skip header (BluRay streams).
2410 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2411 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2413 p_pkt->p_buffer += p_sys->i_packet_header_size;
2414 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2416 /* Check sync byte and re-sync if needed */
2417 if( p_pkt->p_buffer[0] != 0x47 )
2419 msg_Warn( p_demux, "lost synchro" );
2420 block_Release( p_pkt );
2423 const uint8_t *p_peek;
2424 int i_peek, i_skip = 0;
2426 i_peek = stream_Peek( p_sys->stream, &p_peek,
2427 p_sys->i_packet_size * 10 );
2428 if( i_peek < p_sys->i_packet_size + 1 )
2430 msg_Dbg( p_demux, "eof ?" );
2434 while( i_skip < i_peek - p_sys->i_packet_size )
2436 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2437 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2443 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2444 stream_Read( p_sys->stream, NULL, i_skip );
2446 if( i_skip < i_peek - p_sys->i_packet_size )
2451 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2453 msg_Dbg( p_demux, "eof ?" );
2460 static mtime_t AdjustPCRWrapAround( demux_t *p_demux, mtime_t i_pcr )
2462 demux_sys_t *p_sys = p_demux->p_sys;
2464 * PCR is 33bit. If PCR reaches to 0x1FFFFFFFF (26:30:43.717), ressets from 0.
2465 * So, need to add 0x1FFFFFFFF, for calculating duration or current position.
2467 mtime_t i_adjust = 0;
2468 int64_t i_pos = stream_Tell( p_sys->stream );
2470 for( i = 1; i < p_sys->i_pcrs_num && p_sys->p_pos[i] <= i_pos; ++i )
2472 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2473 i_adjust += 0x1FFFFFFFF;
2475 if( p_sys->p_pcrs[i-1] > i_pcr )
2476 i_adjust += 0x1FFFFFFFF;
2478 return i_pcr + i_adjust;
2481 static mtime_t AdjustPTSWrapAround( demux_t *p_demux, mtime_t i_pts )
2483 demux_sys_t *p_sys = p_demux->p_sys;
2484 mtime_t i_pcr = p_sys->i_current_pcr;
2486 mtime_t i_adjustremain = i_pcr % 0x1FFFFFFFF;
2487 mtime_t i_adjustbase = i_pcr - i_adjustremain;
2489 mtime_t i_pts_adjust = i_adjustbase;
2491 /* PTS has rolled first */
2492 if( i_adjustremain >= 0xFFFFFFFF && i_pts < 0xFFFFFFFF )
2494 i_pts_adjust += 0x1FFFFFFFF;
2496 /* PCR has rolled first (PTS is late!) */
2497 else if( i_adjustremain < 0xFFFFFFFF && i_pts >= 0xFFFFFFFF )
2499 if( i_adjustbase >= 0x1FFFFFFFF ) /* we need to remove current roll */
2500 i_pts_adjust -= 0x1FFFFFFFF;
2503 i_pts += i_pts_adjust;
2507 static mtime_t GetPCR( block_t *p_pkt )
2509 const uint8_t *p = p_pkt->p_buffer;
2513 if( ( p[3]&0x20 ) && /* adaptation */
2517 /* PCR is 33 bits */
2518 i_pcr = ( (mtime_t)p[6] << 25 ) |
2519 ( (mtime_t)p[7] << 17 ) |
2520 ( (mtime_t)p[8] << 9 ) |
2521 ( (mtime_t)p[9] << 1 ) |
2522 ( (mtime_t)p[10] >> 7 );
2527 static int SeekToPCR( demux_t *p_demux, int64_t i_pos )
2529 demux_sys_t *p_sys = p_demux->p_sys;
2532 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2535 return VLC_EGENERIC;
2537 int64_t i_last_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2538 if( i_pos > i_last_pos )
2541 if( stream_Seek( p_sys->stream, i_pos ) )
2542 return VLC_EGENERIC;
2548 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2552 if( PIDGet( p_pkt ) == p_sys->i_pid_ref_pcr )
2554 i_pcr = GetPCR( p_pkt );
2556 block_Release( p_pkt );
2559 if( stream_Tell( p_sys->stream ) >= i_last_pos )
2564 stream_Seek( p_sys->stream, i_initial_pos );
2565 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2566 return VLC_EGENERIC;
2569 p_sys->i_current_pcr = i_pcr;
2573 static int Seek( demux_t *p_demux, double f_percent )
2575 demux_sys_t *p_sys = p_demux->p_sys;
2577 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2578 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2581 * Find the time position by using binary search algorithm.
2583 mtime_t i_target_pcr = (p_sys->i_last_pcr - p_sys->i_first_pcr) * f_percent + p_sys->i_first_pcr;
2585 int64_t i_head_pos = 0;
2588 mtime_t i_adjust = 0;
2590 for( i = 1; i < p_sys->i_pcrs_num; ++i )
2592 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2593 i_adjust += 0x1FFFFFFFF;
2594 if( p_sys->p_pcrs[i] + i_adjust > i_target_pcr )
2597 i_head_pos = p_sys->p_pos[i-1];
2598 i_tail_pos = ( i < p_sys->i_pcrs_num ) ? p_sys->p_pos[i] : stream_Size( p_sys->stream );
2600 msg_Dbg( p_demux, "Seek():i_head_pos:%"PRId64", i_tail_pos:%"PRId64, i_head_pos, i_tail_pos);
2602 bool b_found = false;
2604 while( i_head_pos <= i_tail_pos )
2606 /* Round i_pos to a multiple of p_sys->i_packet_size */
2607 int64_t i_pos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2608 int64_t i_div = i_pos % p_sys->i_packet_size;
2610 if( SeekToPCR( p_demux, i_pos ) )
2612 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2613 int64_t i_diff_msec = (p_sys->i_current_pcr - i_target_pcr) * 100 / 9 / 1000;
2614 if( i_diff_msec > 500 )
2616 i_tail_pos = i_pos - p_sys->i_packet_size;
2618 else if( i_diff_msec < -500 )
2620 i_head_pos = i_pos + p_sys->i_packet_size;
2624 // diff time <= 500msec
2632 msg_Dbg( p_demux, "Seek():cannot find a time position. i_cnt:%d", i_cnt );
2633 stream_Seek( p_sys->stream, i_initial_pos );
2634 p_sys->i_current_pcr = i_initial_pcr;
2635 return VLC_EGENERIC;
2639 msg_Dbg( p_demux, "Seek():can find a time position. i_cnt:%d", i_cnt );
2640 p_demux->p_sys->pcrfix.i_first_dts = 0;
2645 static void GetFirstPCR( demux_t *p_demux )
2647 demux_sys_t *p_sys = p_demux->p_sys;
2649 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2651 if( stream_Seek( p_sys->stream, 0 ) )
2658 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2662 mtime_t i_pcr = GetPCR( p_pkt );
2665 p_sys->i_pid_ref_pcr = PIDGet( p_pkt );
2666 p_sys->i_first_pcr = i_pcr;
2667 p_sys->i_current_pcr = i_pcr;
2669 block_Release( p_pkt );
2670 if( p_sys->i_first_pcr >= 0 )
2673 stream_Seek( p_sys->stream, i_initial_pos );
2676 static void GetLastPCR( demux_t *p_demux )
2678 demux_sys_t *p_sys = p_demux->p_sys;
2680 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2681 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2683 int64_t i_stream_size = stream_Size( p_sys->stream );
2684 int64_t i_last_pos = i_stream_size - p_sys->i_packet_size;
2685 /* Round i_pos to a multiple of p_sys->i_packet_size */
2686 int64_t i_pos = i_last_pos - p_sys->i_packet_size * 4500; /* FIXME if the value is not reasonable, please change it. */
2687 int64_t i_div = i_pos % p_sys->i_packet_size;
2690 if( i_pos <= i_initial_pos && i_pos >= i_stream_size )
2691 i_pos = i_initial_pos + p_sys->i_packet_size;
2692 if( i_pos < 0 && i_pos >= i_stream_size )
2697 if( SeekToPCR( p_demux, i_pos ) )
2699 p_sys->i_last_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2700 if( ( i_pos = stream_Tell( p_sys->stream ) ) >= i_last_pos )
2704 stream_Seek( p_sys->stream, i_initial_pos );
2705 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2706 p_sys->i_current_pcr = i_initial_pcr;
2709 static void CheckPCR( demux_t *p_demux )
2711 demux_sys_t *p_sys = p_demux->p_sys;
2713 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2714 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2716 int64_t i_size = stream_Size( p_sys->stream );
2719 p_sys->p_pcrs[0] = p_sys->i_first_pcr;
2720 p_sys->p_pos[0] = i_initial_pos;
2722 for( i = 1; i < p_sys->i_pcrs_num; ++i )
2724 /* Round i_pos to a multiple of p_sys->i_packet_size */
2725 int64_t i_pos = i_size / p_sys->i_pcrs_num * i;
2726 int64_t i_div = i_pos % p_sys->i_packet_size;
2728 if( SeekToPCR( p_demux, i_pos ) )
2730 p_sys->p_pcrs[i] = p_sys->i_current_pcr;
2731 p_sys->p_pos[i] = stream_Tell( p_sys->stream );
2732 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2734 msg_Dbg( p_demux, "PCR Wrap Around found between %d%% and %d%% (pcr:%"PRId64"(0x%09"PRIx64") pcr:%"PRId64"(0x%09"PRIx64"))",
2735 (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] );
2738 if( i < p_sys->i_pcrs_num )
2740 msg_Dbg( p_demux, "Force Seek Per Percent: Seeking failed at %d%%.", (int)(i*100/p_sys->i_pcrs_num) );
2741 p_sys->b_force_seek_per_percent = true;
2744 stream_Seek( p_sys->stream, i_initial_pos );
2745 p_sys->i_current_pcr = i_initial_pcr;
2748 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2750 demux_sys_t *p_sys = p_demux->p_sys;
2752 if( p_sys->i_pmt_es <= 0 )
2755 mtime_t i_pcr = GetPCR( p_bk );
2759 i_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
2761 if( p_sys->i_pid_ref_pcr == pid->i_pid )
2762 p_sys->i_current_pcr = i_pcr;
2764 /* Search program and set the PCR */
2765 for( int i = 0; i < p_sys->i_pmt; i++ )
2768 for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2770 ts_prg_psi_t *p_prg = p_sys->pmt[i]->psi->prg[i_prg];
2772 if( p_prg->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
2774 if( pid->p_owner ) /* PCR shall be on pid itself */
2776 p_prg->i_pcr_value = i_pcr; /* ? update PCR for the whole group program */
2777 i_group = p_prg->i_number;
2778 p_sys->pcrfix.b_program_pcr_seen = true;
2779 p_sys->pcrfix.i_first_dts = 0;
2780 p_sys->b_disable_pcr = !p_sys->b_trust_pcr;
2784 msg_Warn(p_demux, "discarding PCR update from pid %d which has no owner", pid->i_pid);
2788 else /* set PCR provided by current pid to program(s) referencing it */
2790 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
2791 if( p_prg->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
2793 p_prg->i_pcr_value = i_pcr;
2794 i_group = p_prg->i_number; /* We've found a target group for update */
2795 p_sys->pcrfix.b_program_pcr_seen = true;
2796 p_sys->pcrfix.i_first_dts = 0;
2797 p_sys->b_disable_pcr = !p_sys->b_trust_pcr;
2801 if ( !p_sys->b_disable_pcr && i_group > 0 && p_sys->i_pmt_es )
2803 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2804 i_group, VLC_TS_0 + i_pcr * 100 / 9 );
2809 static void PCRFixHandle( demux_t *p_demux, block_t *p_block )
2811 demux_sys_t *p_sys = p_demux->p_sys;
2812 /* Record the first data packet timestamp in case there wont be any PCR */
2813 if( !p_sys->pcrfix.b_program_pcr_seen && !p_sys->b_disable_pcr )
2815 if( !p_sys->pcrfix.i_first_dts )
2817 p_sys->pcrfix.i_first_dts = p_block->i_dts;
2819 else if( p_block->i_dts - p_sys->pcrfix.i_first_dts > CLOCK_FREQ / 10 ) /* "shall not exceed 100ms" */
2821 p_sys->b_disable_pcr = true;
2822 msg_Warn( p_demux, "No PCR received for program %d, set up workaround",
2823 p_sys->i_current_program );
2828 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2830 const uint8_t *p = p_bk->p_buffer;
2831 const bool b_unit_start = p[1]&0x40;
2832 const bool b_scrambled = p[3]&0x80;
2833 const bool b_adaptation = p[3]&0x20;
2834 const bool b_payload = p[3]&0x10;
2835 const int i_cc = p[3]&0x0f; /* continuity counter */
2836 bool b_discontinuity = false; /* discontinuity */
2838 /* transport_scrambling_control is ignored */
2843 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
2844 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
2848 /* For now, ignore additional error correction
2849 * TODO: handle Reed-Solomon 204,188 error correction */
2850 p_bk->i_buffer = TS_PACKET_SIZE_188;
2854 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
2856 if( pid->es->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
2857 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2860 if( p_demux->p_sys->csa )
2862 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
2863 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
2864 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
2869 /* We don't have any adaptation_field, so payload starts
2870 * immediately after the 4 byte TS header */
2875 /* p[4] is adaptation_field_length minus one */
2879 /* discontinuity indicator found in stream */
2880 b_discontinuity = (p[5]&0x80) ? true : false;
2881 if( b_discontinuity && pid->es->p_data )
2883 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
2885 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
2889 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
2894 /* Test continuity counter */
2895 /* continuous when (one of this):
2897 * diff == 0 and payload == 0
2898 * diff == 0 and duplicate packet (playload != 0) <- should we
2899 * test the content ?
2901 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
2902 if( b_payload && i_diff == 1 )
2904 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
2908 if( pid->i_cc == 0xff )
2910 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
2914 else if( i_diff != 0 && !b_discontinuity )
2916 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
2917 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
2920 if( pid->es->p_data && pid->es->fmt.i_cat != VIDEO_ES &&
2921 pid->es->fmt.i_cat != AUDIO_ES )
2923 /* Small audio/video artifacts are usually better than
2924 * dropping full frames */
2925 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2930 PCRHandle( p_demux, pid, p_bk );
2932 if( i_skip >= 188 || pid->es->id == NULL )
2934 block_Release( p_bk );
2939 if( !pid->b_scrambled != !b_scrambled )
2941 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2942 pid->i_pid, pid->b_scrambled, b_scrambled );
2944 pid->b_scrambled = b_scrambled;
2946 for( int i = 0; i < pid->i_extra_es; i++ )
2948 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2949 pid->extra_es[i]->id, b_scrambled );
2951 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2952 pid->es->id, b_scrambled );
2955 /* We have to gather it */
2956 p_bk->p_buffer += i_skip;
2957 p_bk->i_buffer -= i_skip;
2961 if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
2963 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
2964 block_t *p = block_Duplicate( p_bk );
2967 p->i_buffer = i_pointer_field;
2969 block_ChainLastAppend( &pid->es->pp_last, p );
2971 p_bk->i_buffer -= 1 + i_pointer_field;
2972 p_bk->p_buffer += 1 + i_pointer_field;
2974 if( pid->es->p_data )
2976 ParseData( p_demux, pid );
2980 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2981 if( pid->es->data_type == TS_ES_DATA_PES )
2983 if( p_bk->i_buffer > 6 )
2985 pid->es->i_data_size = GetWBE( &p_bk->p_buffer[4] );
2986 if( pid->es->i_data_size > 0 )
2988 pid->es->i_data_size += 6;
2992 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
2994 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
2996 pid->es->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
2999 pid->es->i_data_gathered += p_bk->i_buffer;
3000 if( pid->es->i_data_size > 0 &&
3001 pid->es->i_data_gathered >= pid->es->i_data_size )
3003 ParseData( p_demux, pid );
3009 if( pid->es->p_data == NULL )
3011 /* msg_Dbg( p_demux, "broken packet" ); */
3012 block_Release( p_bk );
3016 block_ChainLastAppend( &pid->es->pp_last, p_bk );
3017 pid->es->i_data_gathered += p_bk->i_buffer;
3019 if( pid->es->i_data_size > 0 &&
3020 pid->es->i_data_gathered >= pid->es->i_data_size )
3022 ParseData( p_demux, pid );
3031 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
3033 switch( i_stream_type )
3035 case 0x01: /* MPEG-1 video */
3036 case 0x02: /* MPEG-2 video */
3037 case 0x80: /* MPEG-2 MOTO video */
3038 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
3040 case 0x03: /* MPEG-1 audio */
3041 case 0x04: /* MPEG-2 audio */
3042 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
3044 case 0x11: /* MPEG4 (audio) LATM */
3045 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
3046 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
3047 transport syntax, such as DST, ALS and SLS */
3048 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
3050 case 0x10: /* MPEG4 (video) */
3051 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
3053 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
3054 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
3056 case 0x24: /* HEVC */
3057 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
3059 case 0x42: /* CAVS (Chinese AVS) */
3060 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
3063 case 0x81: /* A52 (audio) */
3064 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
3066 case 0x82: /* SCTE-27 (sub) */
3067 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
3069 case 0x84: /* SDDS (audio) */
3070 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
3072 case 0x85: /* DTS (audio) */
3073 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
3075 case 0x87: /* E-AC3 */
3076 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
3079 case 0x91: /* A52 vls (audio) */
3080 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
3082 case 0x92: /* DVD_SPU vls (sub) */
3083 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
3086 case 0x94: /* SDDS (audio) */
3087 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
3090 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
3091 es_format_Init( fmt, UNKNOWN_ES, 0 );
3094 case 0x06: /* PES_PRIVATE (fixed later) */
3095 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
3096 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
3098 es_format_Init( fmt, UNKNOWN_ES, 0 );
3102 /* PES packets usually contain truncated frames */
3103 fmt->b_packetized = false;
3106 /*****************************************************************************
3107 * MP4 specific functions (IOD parser)
3108 *****************************************************************************/
3109 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
3112 unsigned int i_len = 0;
3118 i_len = ( i_len << 7 ) + ( i_b&0x7f );
3120 } while( i_b&0x80 && *pi_data > 0 );
3122 if (i_len > *pi_data)
3128 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
3131 while( *pi_data > 0 && bytes-- )
3142 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
3144 int len = IODGetBytes( pi_data, pp_data, 1 );
3147 char *url = strndup( (char*)*pp_data, len );
3153 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
3155 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
3157 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
3166 byte1 = IODGetBytes( &i_data, &p_data, 1 );
3167 byte2 = IODGetBytes( &i_data, &p_data, 1 );
3168 byte3 = IODGetBytes( &i_data, &p_data, 1 );
3169 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
3171 i_iod_label = byte1;
3174 else //correct implementation of the IOD_descriptor
3176 i_iod_label = byte2;
3180 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
3182 if( i_iod_tag != 0x02 )
3184 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
3188 IODDescriptorLength( &i_data, &p_data );
3190 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
3191 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
3192 i_od_id |= i_flags >> 6;
3193 ts_debug( "\n* od_id:%d", i_od_id );
3194 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
3195 if ((i_flags >> 5) & 0x01)
3197 p_iod->psz_url = IODGetURL( &i_data, &p_data );
3198 ts_debug( "\n* url string:%s", p_iod->psz_url );
3199 ts_debug( "\n*****************************\n" );
3204 p_iod->psz_url = NULL;
3207 /* Profile Level Indication */
3208 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
3209 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
3210 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
3211 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
3212 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
3215 int i_data_sav = i_data;
3216 uint8_t *p_data_sav = p_data;
3217 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
3219 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
3221 p_data = p_data_sav + i_length;
3222 i_data = i_data_sav - i_length;
3224 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
3225 i_length = IODDescriptorLength( &i_data, &p_data );
3227 i_data_sav = i_data;
3228 p_data_sav = p_data;
3232 if ( i_tag != 0x03 )
3234 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
3238 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
3239 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
3240 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
3241 if( b_streamDependenceFlag )
3242 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
3244 if( (i_flags >> 6) & 0x01 )
3245 es_descr->psz_url = IODGetURL( &i_data, &p_data );
3247 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
3248 if( b_OCRStreamFlag )
3249 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
3251 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
3253 ts_debug( "\n* ERR missing DecoderConfigDescr" );
3256 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
3257 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
3258 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
3259 i_flags = IODGetBytes( &i_data, &p_data, 1 );
3260 dec_descr->i_streamType = i_flags >> 2;
3262 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
3263 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
3264 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
3266 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
3268 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
3269 if( dec_descr->i_extra > 0 )
3271 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
3272 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
3273 p_data += dec_descr->i_extra;
3274 i_data -= dec_descr->i_extra;
3279 dec_descr->i_extra = 0;
3280 dec_descr->p_extra = NULL;
3283 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
3285 ts_debug( "\n* ERR missing SLConfigDescr" );
3288 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
3289 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
3292 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
3297 es_descr->b_ok = true;
3303 static void IODFree( iod_descriptor_t *p_iod )
3305 if( p_iod->psz_url )
3307 free( p_iod->psz_url );
3312 for( int i = 0; i < 255; i++ )
3314 #define es_descr p_iod->es_descr[i]
3317 if( es_descr.psz_url )
3318 free( es_descr.psz_url );
3320 free( es_descr.dec_descr.p_extra );
3327 /****************************************************************************
3328 ****************************************************************************
3329 ** libdvbpsi callbacks
3330 ****************************************************************************
3331 ****************************************************************************/
3332 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
3334 demux_sys_t *p_sys = p_demux->p_sys;
3336 if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
3337 p_sys->i_current_program == 0 )
3339 if( p_sys->i_current_program == i_pgrm )
3342 if( p_sys->programs_list.i_count != 0 )
3344 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
3346 if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
3353 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
3355 demux_sys_t *p_sys = p_demux->p_sys;
3357 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
3360 msg_Warn( p_demux, "Switching to non DVB mode" );
3362 /* This doesn't look like a DVB stream so don't try
3363 * parsing the SDT/EDT/TDT */
3365 for( int i = 0x11; i <= 0x14; i++ )
3367 if( i == 0x13 ) continue;
3368 ts_pid_t *p_pid = &p_sys->pid[i];
3372 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3373 if( dvbpsi_decoder_present( p_pid->psi->handle ))
3374 dvbpsi_DetachDemux( p_pid->psi->handle );
3375 dvbpsi_delete( p_pid->psi->handle );
3377 dvbpsi_DetachDemux( p_pid->psi->handle );
3381 p_pid->b_valid = false;
3383 SetPIDFilter( p_demux, i, false );
3385 p_sys->b_dvb_meta = false;
3388 #include "dvb-text.h"
3390 static char *EITConvertToUTF8( demux_t *p_demux,
3391 const unsigned char *psz_instring,
3395 demux_sys_t *p_sys = p_demux->p_sys;
3397 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3399 if ( !p_sys->arib.p_instance )
3400 p_sys->arib.p_instance = arib_instance_new( p_demux );
3401 if ( !p_sys->arib.p_instance )
3403 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3407 char *psz_outstring = NULL;
3410 i_out = i_length * 4;
3411 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3412 if( !psz_outstring )
3415 arib_initialize_decoder( p_decoder );
3416 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3417 psz_outstring, i_out );
3418 arib_finalize_decoder( p_decoder );
3420 return psz_outstring;
3425 /* Deal with no longer broken providers (no switch byte
3426 but sending ISO_8859-1 instead of ISO_6937) without
3427 removing them from the broken providers table
3428 (keep the entry for correctly handling recorded TS).
3430 b_broken = b_broken && i_length && *psz_instring > 0x20;
3433 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3434 return vlc_from_EIT( psz_instring, i_length );
3437 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3439 demux_sys_t *p_sys = p_demux->p_sys;
3440 ts_pid_t *sdt = &p_sys->pid[0x11];
3441 dvbpsi_sdt_service_t *p_srv;
3443 msg_Dbg( p_demux, "SDTCallBack called" );
3445 if( p_sys->b_delay_es_creation ||
3446 !p_sdt->b_current_next ||
3447 p_sdt->i_version == sdt->psi->i_sdt_version )
3449 dvbpsi_DeleteSDT( p_sdt );
3453 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3455 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3460 p_sdt->i_version, p_sdt->b_current_next,
3461 p_sdt->i_network_id );
3463 p_sys->b_broken_charset = false;
3465 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3468 dvbpsi_descriptor_t *p_dr;
3470 const char *psz_type = NULL;
3471 const char *psz_status = NULL;
3473 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3474 "running=%d free_ca=%d",
3475 p_srv->i_service_id, p_srv->b_eit_schedule,
3476 p_srv->b_eit_present, p_srv->i_running_status,
3479 if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
3481 msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
3482 p_sys->vdr.i_service );
3486 p_meta = vlc_meta_New();
3487 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3489 if( p_dr->i_tag == 0x48 )
3491 static const char *ppsz_type[17] = {
3493 "Digital television service",
3494 "Digital radio sound service",
3496 "NVOD reference service",
3497 "NVOD time-shifted service",
3500 "SECAM coded signal",
3503 "NTSC coded signal",
3504 "Data broadcast service",
3505 "Reserved for Common Interface Usage",
3506 "RCS Map (see EN 301 790 [35])",
3507 "RCS FLS (see EN 301 790 [35])",
3510 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3514 /* Workarounds for broadcasters with broken EPG */
3516 if( p_sdt->i_network_id == 133 )
3517 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3519 /* List of providers using ISO8859-1 */
3520 static const char ppsz_broken_providers[][8] = {
3521 "CSAT", /* CanalSat FR */
3522 "GR1", /* France televisions */
3524 "MR5", /* France 2/M6 HD */
3527 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3529 const size_t i_length = strlen(ppsz_broken_providers[i]);
3530 if( pD->i_service_provider_name_length == i_length &&
3531 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3532 p_sys->b_broken_charset = true;
3535 /* FIXME: Digital+ ES also uses ISO8859-1 */
3537 str1 = EITConvertToUTF8(p_demux,
3538 pD->i_service_provider_name,
3539 pD->i_service_provider_name_length,
3540 p_sys->b_broken_charset );
3541 str2 = EITConvertToUTF8(p_demux,
3543 pD->i_service_name_length,
3544 p_sys->b_broken_charset );
3546 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3547 pD->i_service_type, str1, str2 );
3549 vlc_meta_SetTitle( p_meta, str2 );
3550 vlc_meta_SetPublisher( p_meta, str1 );
3551 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3552 psz_type = ppsz_type[pD->i_service_type];
3558 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3560 static const char *ppsz_status[5] = {
3563 "Starts in a few seconds",
3567 psz_status = ppsz_status[p_srv->i_running_status];
3571 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3573 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3575 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3576 p_srv->i_service_id, p_meta );
3577 vlc_meta_Delete( p_meta );
3580 sdt->psi->i_sdt_version = p_sdt->i_version;
3581 dvbpsi_DeleteSDT( p_sdt );
3584 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3585 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3587 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3591 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3592 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3595 /* Count the number of days */
3596 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3597 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3598 for( int i = 70; i < i_year; i++ )
3599 i_day += LEAP(1900+i);
3601 i_day += LEAP(1900+i_year);
3604 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3607 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3609 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3610 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3611 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3613 *p_y = 1900 + yp + c*1;
3614 *p_m = mp - 1 - c*12;
3615 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3617 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3618 static int64_t EITConvertStartTime( uint64_t i_date )
3620 const int i_mjd = i_date >> 24;
3621 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3622 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3623 const int i_second = CVT_FROM_BCD(i_date );
3628 /* if all 40 bits are 1, the start is unknown */
3629 if( i_date == UINT64_C(0xffffffffff) )
3632 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3633 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3635 static int EITConvertDuration( uint32_t i_duration )
3637 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3638 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3639 CVT_FROM_BCD(i_duration );
3643 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3645 demux_sys_t *p_sys = p_demux->p_sys;
3647 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3649 dvbpsi_DeleteTOT(p_tdt);
3653 static void EITCallBack( demux_t *p_demux,
3654 dvbpsi_eit_t *p_eit, bool b_current_following )
3656 demux_sys_t *p_sys = p_demux->p_sys;
3657 dvbpsi_eit_event_t *p_evt;
3660 msg_Dbg( p_demux, "EITCallBack called" );
3661 if( !p_eit->b_current_next )
3663 dvbpsi_DeleteEIT( p_eit );
3667 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3668 "ts_id=%d network_id=%d segment_last_section_number=%d "
3670 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3673 p_eit->i_service_id,
3675 p_eit->i_version, p_eit->b_current_next,
3676 p_eit->i_ts_id, p_eit->i_network_id,
3677 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3679 p_epg = vlc_epg_New( NULL );
3680 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3682 dvbpsi_descriptor_t *p_dr;
3683 char *psz_name = NULL;
3684 char *psz_text = NULL;
3685 char *psz_extra = strdup("");
3689 int64_t i_tot_time = 0;
3691 i_start = EITConvertStartTime( p_evt->i_start_time );
3692 i_duration = EITConvertDuration( p_evt->i_duration );
3694 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3696 if( p_sys->i_tdt_delta == 0 )
3697 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3699 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3701 tzset(); // JST -> UTC
3702 i_start += timezone; // FIXME: what about DST?
3703 i_tot_time += timezone;
3705 if( p_evt->i_running_status == 0x00 &&
3706 (i_start - 5 < i_tot_time &&
3707 i_tot_time < i_start + i_duration + 5) )
3709 p_evt->i_running_status = 0x04;
3710 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3714 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3715 "running=%d free_ca=%d",
3716 p_evt->i_event_id, (int)i_start, (int)i_duration,
3717 p_evt->i_running_status, p_evt->b_free_ca );
3719 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3725 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3727 /* Only take first description, as we don't handle language-info
3729 if( pE && psz_name == NULL )
3731 psz_name = EITConvertToUTF8( p_demux,
3732 pE->i_event_name, pE->i_event_name_length,
3733 p_sys->b_broken_charset );
3735 psz_text = EITConvertToUTF8( p_demux,
3736 pE->i_text, pE->i_text_length,
3737 p_sys->b_broken_charset );
3738 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3739 pE->i_iso_639_code, psz_name, psz_text );
3746 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3749 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3751 pE->i_descriptor_number, pE->i_last_descriptor_number );
3753 if( pE->i_text_length > 0 )
3755 char *psz_text = EITConvertToUTF8( p_demux,
3756 pE->i_text, pE->i_text_length,
3757 p_sys->b_broken_charset );
3760 msg_Dbg( p_demux, " - text='%s'", psz_text );
3762 psz_extra = xrealloc( psz_extra,
3763 strlen(psz_extra) + strlen(psz_text) + 1 );
3764 strcat( psz_extra, psz_text );
3769 for( int i = 0; i < pE->i_entry_count; i++ )
3771 char *psz_dsc = EITConvertToUTF8( p_demux,
3772 pE->i_item_description[i],
3773 pE->i_item_description_length[i],
3774 p_sys->b_broken_charset );
3775 char *psz_itm = EITConvertToUTF8( p_demux,
3776 pE->i_item[i], pE->i_item_length[i],
3777 p_sys->b_broken_charset );
3779 if( psz_dsc && psz_itm )
3781 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3783 psz_extra = xrealloc( psz_extra,
3784 strlen(psz_extra) + strlen(psz_dsc) +
3785 strlen(psz_itm) + 3 + 1 );
3786 strcat( psz_extra, "(" );
3787 strcat( psz_extra, psz_dsc );
3788 strcat( psz_extra, " " );
3789 strcat( psz_extra, psz_itm );
3790 strcat( psz_extra, ")" );
3802 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
3805 for ( int i = 0; i < pR->i_ratings_number; i++ )
3807 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
3808 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
3810 if ( p_rating->i_rating + 3 > i_min_age )
3811 i_min_age = p_rating->i_rating + 3;
3812 msg_Dbg( p_demux, " - parental control set to %d years",
3821 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3827 if( i_start > 0 && psz_name && psz_text)
3828 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3829 *psz_extra ? psz_extra : NULL, i_min_age );
3831 /* Update "now playing" field */
3832 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
3833 vlc_epg_SetCurrent( p_epg, i_start );
3840 if( p_epg->i_event > 0 )
3842 if( b_current_following &&
3843 ( p_sys->i_current_program == -1 ||
3844 p_sys->i_current_program ==
3845 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3852 p_sys->i_dvb_length = 0;
3853 p_sys->i_dvb_start = 0;
3855 if( p_epg->p_current )
3857 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
3858 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
3861 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
3862 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3865 p_eit->i_service_id,
3869 vlc_epg_Delete( p_epg );
3871 dvbpsi_DeleteEIT( p_eit );
3873 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3875 EITCallBack( p_demux, p_eit, true );
3877 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3879 EITCallBack( p_demux, p_eit, false );
3882 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3883 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
3884 uint16_t i_extension, demux_t *p_demux )
3886 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
3887 uint8_t i_table_id, uint16_t i_extension )
3892 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3893 i_table_id, i_table_id, i_extension, i_extension );
3895 if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
3897 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3898 i_table_id, i_table_id, i_extension, i_extension );
3899 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3900 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
3901 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
3903 dvbpsi_AttachSDT( h, i_table_id, i_extension,
3904 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
3907 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3908 ( i_table_id == 0x4e || /* Current/Following */
3909 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3911 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3912 i_table_id, i_table_id, i_extension, i_extension );
3914 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3915 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3916 (dvbpsi_eit_callback)EITCallBackSchedule;
3917 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3918 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
3919 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
3921 dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
3924 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3925 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
3927 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3928 i_table_id, i_table_id, i_extension, i_extension );
3929 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3930 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
3931 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
3933 dvbpsi_AttachTOT( h, i_table_id, i_extension,
3934 (dvbpsi_tot_callback)TDTCallBack, p_demux);
3939 /*****************************************************************************
3940 * PMT callback and helpers
3941 *****************************************************************************/
3942 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3945 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3946 while( p_dr && ( p_dr->i_tag != i_tag ) )
3947 p_dr = p_dr->p_next;
3950 static bool PMTEsHasRegistration( demux_t *p_demux,
3951 const dvbpsi_pmt_es_t *p_es,
3952 const char *psz_tag )
3954 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3958 if( p_dr->i_length < 4 )
3960 msg_Warn( p_demux, "invalid Registration Descriptor" );
3964 assert( strlen(psz_tag) == 4 );
3965 return !memcmp( p_dr->p_data, psz_tag, 4 );
3968 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
3969 int i_component_tag )
3971 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3974 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3978 return p_si->i_component_tag == i_component_tag;
3981 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3982 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3984 es_format_t *p_fmt = &pid->es->fmt;
3986 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
3987 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3989 if( p_dr && p_dr->i_length == 2 )
3991 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3993 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
3995 pid->es->p_mpeg4desc = NULL;
3997 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
3999 iod_descriptor_t *iod = prg->iod;
4000 if( iod->es_descr[i].i_es_id == i_es_id )
4002 if ( iod->es_descr[i].b_ok )
4003 pid->es->p_mpeg4desc = &iod->es_descr[i];
4005 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
4010 if( !pid->es->p_mpeg4desc )
4012 switch( p_es->i_type )
4014 /* non fatal, set by packetizer */
4015 case 0x0f: /* ADTS */
4016 case 0x11: /* LOAS */
4017 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4018 pid->i_pid, p_es->i_type );
4021 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4022 pid->i_pid, p_es->i_type );
4028 const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
4029 if( dcd->i_streamType == 0x04 ) /* VisualStream */
4031 p_fmt->i_cat = VIDEO_ES;
4032 switch( dcd->i_objectTypeIndication )
4034 case 0x0B: /* mpeg4 sub */
4035 p_fmt->i_cat = SPU_ES;
4036 p_fmt->i_codec = VLC_CODEC_SUBT;
4039 case 0x20: /* mpeg4 */
4040 p_fmt->i_codec = VLC_CODEC_MP4V;
4042 case 0x21: /* h264 */
4043 p_fmt->i_codec = VLC_CODEC_H264;
4050 case 0x65: /* mpeg2 */
4051 p_fmt->i_codec = VLC_CODEC_MPGV;
4053 case 0x6a: /* mpeg1 */
4054 p_fmt->i_codec = VLC_CODEC_MPGV;
4056 case 0x6c: /* mpeg1 */
4057 p_fmt->i_codec = VLC_CODEC_JPEG;
4060 p_fmt->i_cat = UNKNOWN_ES;
4064 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
4066 p_fmt->i_cat = AUDIO_ES;
4067 switch( dcd->i_objectTypeIndication )
4069 case 0x40: /* mpeg4 */
4070 p_fmt->i_codec = VLC_CODEC_MP4A;
4074 case 0x68: /* mpeg2 aac */
4075 p_fmt->i_codec = VLC_CODEC_MP4A;
4077 case 0x69: /* mpeg2 */
4078 p_fmt->i_codec = VLC_CODEC_MPGA;
4080 case 0x6b: /* mpeg1 */
4081 p_fmt->i_codec = VLC_CODEC_MPGA;
4084 p_fmt->i_cat = UNKNOWN_ES;
4090 p_fmt->i_cat = UNKNOWN_ES;
4093 if( p_fmt->i_cat != UNKNOWN_ES )
4095 p_fmt->i_extra = dcd->i_extra;
4096 if( p_fmt->i_extra > 0 )
4098 p_fmt->p_extra = malloc( p_fmt->i_extra );
4099 if( p_fmt->p_extra )
4100 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
4113 } ts_teletext_page_t;
4115 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
4116 const dvbpsi_pmt_es_t *p_es )
4118 es_format_t *p_fmt = &pid->es->fmt;
4120 ts_teletext_page_t p_page[2 * 64 + 20];
4121 unsigned i_page = 0;
4123 /* Gather pages information */
4124 #if defined _DVBPSI_DR_56_H_ && \
4125 defined DVBPSI_VERSION && DVBPSI_VERSION_INT > DVBPSI_VERSION_WANTED(0,1,5)
4126 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
4128 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
4132 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
4136 for( int i = 0; i < p_sub->i_pages_number; i++ )
4138 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
4140 if( p_src->i_teletext_type >= 0x06 )
4143 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4145 ts_teletext_page_t *p_dst = &p_page[i_page++];
4147 p_dst->i_type = p_src->i_teletext_type;
4148 p_dst->i_magazine = p_src->i_teletext_magazine_number
4149 ? p_src->i_teletext_magazine_number : 8;
4150 p_dst->i_page = p_src->i_teletext_page_number;
4151 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4156 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
4159 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4160 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4162 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
4164 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
4167 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4169 ts_teletext_page_t *p_dst = &p_page[i_page++];
4171 switch( p_src->i_subtitling_type )
4174 p_dst->i_type = 0x02;
4177 p_dst->i_type = 0x03;
4180 /* FIXME check if it is the right split */
4181 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
4182 ? (p_src->i_composition_page_id >> 8) : 8;
4183 p_dst->i_page = p_src->i_composition_page_id & 0xff;
4184 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4189 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
4191 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4193 p_fmt->subs.teletext.i_magazine = -1;
4194 p_fmt->subs.teletext.i_page = 0;
4195 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
4197 dvbpsi_descriptor_t *p_dr;
4198 p_dr = PMTEsFindDescriptor( p_es, 0x46 );
4200 p_dr = PMTEsFindDescriptor( p_es, 0x56 );
4202 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4204 /* Descriptor pass-through */
4205 p_fmt->p_extra = malloc( p_dr->i_length );
4206 if( p_fmt->p_extra )
4208 p_fmt->i_extra = p_dr->i_length;
4209 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4215 for( unsigned i = 0; i < i_page; i++ )
4226 p_es = malloc( sizeof(*p_es) );
4230 es_format_Copy( &p_es->fmt, &pid->es->fmt );
4231 free( p_es->fmt.psz_language );
4232 free( p_es->fmt.psz_description );
4233 p_es->fmt.psz_language = NULL;
4234 p_es->fmt.psz_description = NULL;
4237 p_es->p_data = NULL;
4238 p_es->i_data_size = 0;
4239 p_es->i_data_gathered = 0;
4240 p_es->pp_last = &p_es->p_data;
4241 p_es->data_type = TS_ES_DATA_PES;
4242 p_es->p_mpeg4desc = NULL;
4244 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
4248 const ts_teletext_page_t *p = &p_page[i];
4249 p_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
4250 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
4251 p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
4252 p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
4253 p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
4254 p_es->fmt.subs.teletext.i_page = p->i_page;
4257 " * ttxt type=%s lan=%s page=%d%02x",
4258 p_es->fmt.psz_description,
4259 p_es->fmt.psz_language,
4260 p->i_magazine, p->i_page );
4264 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
4265 const dvbpsi_pmt_es_t *p_es )
4267 es_format_t *p_fmt = &pid->es->fmt;
4269 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
4271 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
4273 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4274 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4276 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
4277 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
4278 ( i_type >= 0x20 && i_type <= 0x24 ) )
4282 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4284 p_fmt->subs.dvb.i_id = -1;
4285 p_fmt->psz_description = strdup( _("DVB subtitles") );
4287 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4289 /* Descriptor pass-through */
4290 p_fmt->p_extra = malloc( p_dr->i_length );
4291 if( p_fmt->p_extra )
4293 p_fmt->i_extra = p_dr->i_length;
4294 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4300 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4311 p_es = malloc( sizeof(*p_es) );
4315 es_format_Copy( &p_es->fmt, &pid->es->fmt );
4316 free( p_es->fmt.psz_language );
4317 free( p_es->fmt.psz_description );
4318 p_es->fmt.psz_language = NULL;
4319 p_es->fmt.psz_description = NULL;
4322 p_es->p_data = NULL;
4323 p_es->i_data_size = 0;
4324 p_es->i_data_gathered = 0;
4325 p_es->pp_last = &p_es->p_data;
4326 p_es->data_type = TS_ES_DATA_PES;
4327 p_es->p_mpeg4desc = NULL;
4329 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
4333 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
4334 p_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
4335 switch( p->i_subtitling_type )
4337 case 0x10: /* unspec. */
4338 case 0x11: /* 4:3 */
4339 case 0x12: /* 16:9 */
4340 case 0x13: /* 2.21:1 */
4341 case 0x14: /* HD monitor */
4342 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
4344 case 0x20: /* Hearing impaired unspec. */
4345 case 0x21: /* h.i. 4:3 */
4346 case 0x22: /* h.i. 16:9 */
4347 case 0x23: /* h.i. 2.21:1 */
4348 case 0x24: /* h.i. HD monitor */
4349 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
4356 p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
4357 ( p->i_ancillary_page_id << 16 );
4362 static int vlc_ceil_log2( const unsigned int val )
4364 int n = 31 - clz(val);
4365 if ((1U << n) != val)
4371 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
4375 /* default mapping */
4376 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
4377 memcpy(h.stream_map, map, sizeof(map));
4381 int stream_count = 0;
4382 int ccc = p[1]; // channel_config_code
4386 mapping = channels > 2;
4389 channels = 2; // dual mono
4391 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
4392 csc = p_csc[channels - 1];
4393 stream_count = channels - csc;
4395 static const uint8_t map[6][7] = {
4404 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4405 } else if (ccc == 0x81) {
4407 goto explicit_config_too_short;
4414 bs_init(&s, &p[4], len - 4);
4417 int bits = vlc_ceil_log2(channels);
4418 if (s.i_left < bits)
4419 goto explicit_config_too_short;
4420 stream_count = bs_read(&s, bits) + 1;
4421 bits = vlc_ceil_log2(stream_count + 1);
4422 if (s.i_left < bits)
4423 goto explicit_config_too_short;
4424 csc = bs_read(&s, bits);
4426 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4427 if (s.i_left < channels * channel_bits)
4428 goto explicit_config_too_short;
4430 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4431 for (int i = 0; i < channels; i++) {
4432 unsigned char m = bs_read(&s, channel_bits);
4435 h.stream_map[i] = m;
4438 } else if (ccc >= 0x80 && ccc <= 0x88) {
4439 channels = ccc - 0x80;
4444 channels = 2; // dual mono
4447 stream_count = channels;
4449 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4453 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4457 opus_prepare_header(channels, 0, &h);
4459 h.input_sample_rate = 48000;
4461 h.nb_streams = channels - csc;
4462 h.channel_mapping = mapping;
4465 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4466 if (p_fmt->p_extra) {
4467 p_fmt->i_cat = AUDIO_ES;
4468 p_fmt->i_codec = VLC_CODEC_OPUS;
4469 p_fmt->audio.i_channels = h.channels;
4470 p_fmt->audio.i_rate = 48000;
4476 explicit_config_too_short:
4477 msg_Err(demux, "Opus descriptor too short");
4480 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
4481 const dvbpsi_pmt_es_t *p_es )
4483 es_format_t *p_fmt = &pid->es->fmt;
4484 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_es, 0x59 );
4485 dvbpsi_descriptor_t *desc;
4487 if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
4488 PMTEsFindDescriptor( p_es, 0x6a ) ||
4489 PMTEsFindDescriptor( p_es, 0x81 ) )
4491 p_fmt->i_cat = AUDIO_ES;
4492 p_fmt->i_codec = VLC_CODEC_A52;
4494 else if( (desc = PMTEsFindDescriptor( p_es, 0x7f ) ) && desc->i_length >= 2 &&
4495 PMTEsHasRegistration(p_demux, p_es, "Opus"))
4497 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4499 else if( PMTEsFindDescriptor( p_es, 0x7a ) )
4501 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4502 p_fmt->i_cat = AUDIO_ES;
4503 p_fmt->i_codec = VLC_CODEC_EAC3;
4505 else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
4506 PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
4507 PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
4508 PMTEsFindDescriptor( p_es, 0x73 ) )
4510 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4511 p_fmt->i_cat = AUDIO_ES;
4512 p_fmt->i_codec = VLC_CODEC_DTS;
4514 else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) && !p_subs_dr )
4516 /* BSSD is AES3 DATA, but could also be subtitles
4517 * we need to check for secondary descriptor then s*/
4518 p_fmt->i_cat = AUDIO_ES;
4519 p_fmt->b_packetized = true;
4520 p_fmt->i_codec = VLC_CODEC_302M;
4522 else if( PMTEsHasRegistration( p_demux, p_es, "HEVC" ) )
4524 p_fmt->i_cat = VIDEO_ES;
4525 p_fmt->i_codec = VLC_CODEC_HEVC;
4527 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4529 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4530 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xFD );
4531 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4532 if ( p_dr && p_dr->i_length >= 2 )
4534 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4535 PMTEsHasComponentTag( p_es, 0x30 ) ||
4536 PMTEsHasComponentTag( p_es, 0x31 ) ||
4537 PMTEsHasComponentTag( p_es, 0x32 ) ||
4538 PMTEsHasComponentTag( p_es, 0x33 ) ||
4539 PMTEsHasComponentTag( p_es, 0x34 ) ||
4540 PMTEsHasComponentTag( p_es, 0x35 ) ||
4541 PMTEsHasComponentTag( p_es, 0x36 ) ||
4542 PMTEsHasComponentTag( p_es, 0x37 ) ) )
4544 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_A );
4545 p_fmt->psz_language = strndup ( "jpn", 3 );
4546 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4548 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4549 PMTEsHasComponentTag( p_es, 0x87 ) ||
4550 PMTEsHasComponentTag( p_es, 0x88 ) ) )
4552 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_C );
4553 p_fmt->psz_language = strndup ( "jpn", 3 );
4554 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4560 /* Subtitle/Teletext/VBI fallbacks */
4561 dvbpsi_subtitling_dr_t *p_sub;
4562 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4564 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4566 if( p_fmt->i_cat != UNKNOWN_ES )
4569 switch( p_sub->p_subtitle[i].i_subtitling_type )
4571 case 0x01: /* EBU Teletext subtitles */
4572 case 0x02: /* Associated EBU Teletext */
4573 case 0x03: /* VBI data */
4574 PMTSetupEsTeletext( p_demux, pid, p_es );
4576 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4577 case 0x11: /* ... on 4:3 AR monitor */
4578 case 0x12: /* ... on 16:9 AR monitor */
4579 case 0x13: /* ... on 2.21:1 AR monitor */
4580 case 0x14: /* ... for display on a high definition monitor */
4581 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4582 case 0x21: /* ... on 4:3 AR monitor */
4583 case 0x22: /* ... on 16:9 AR monitor */
4584 case 0x23: /* ... on 2.21:1 AR monitor */
4585 case 0x24: /* ... for display on a high definition monitor */
4586 PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
4589 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4590 p_sub->p_subtitle[i].i_subtitling_type );
4596 if( p_fmt->i_cat == UNKNOWN_ES &&
4597 ( PMTEsFindDescriptor( p_es, 0x45 ) || /* VBI Data descriptor */
4598 PMTEsFindDescriptor( p_es, 0x46 ) || /* VBI Teletext descriptor */
4599 PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
4602 PMTSetupEsTeletext( p_demux, pid, p_es );
4606 /* FIXME is it useful ? */
4607 if( PMTEsFindDescriptor( p_es, 0x52 ) )
4609 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4610 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4612 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4616 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
4617 const dvbpsi_pmt_es_t *p_es )
4619 /* Registration Descriptor */
4620 if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
4622 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4626 es_format_t *p_fmt = &pid->es->fmt;
4628 /* registration descriptor for VC-1 (SMPTE rp227) */
4629 p_fmt->i_cat = VIDEO_ES;
4630 p_fmt->i_codec = VLC_CODEC_VC1;
4632 /* XXX With Simple and Main profile the SEQUENCE
4633 * header is modified: video width and height are
4634 * inserted just after the start code as 2 int16_t
4635 * The packetizer will take care of that. */
4638 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
4639 const dvbpsi_pmt_es_t *p_es )
4641 /* Registration Descriptor */
4642 if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
4644 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4648 es_format_t *p_fmt = &pid->es->fmt;
4650 /* registration descriptor for Dirac
4651 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4652 p_fmt->i_cat = VIDEO_ES;
4653 p_fmt->i_codec = VLC_CODEC_DIRAC;
4656 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
4657 const dvbpsi_pmt_es_t *p_es )
4659 /* MSCODEC sent by vlc */
4660 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
4661 if( !p_dr || p_dr->i_length < 10 )
4664 "private MSCODEC (vlc) without bih private descriptor" );
4668 es_format_t *p_fmt = &pid->es->fmt;
4669 p_fmt->i_cat = VIDEO_ES;
4670 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4671 p_dr->p_data[2], p_dr->p_data[3] );
4672 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4673 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4674 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4676 if( p_fmt->i_extra > 0 )
4678 p_fmt->p_extra = malloc( p_fmt->i_extra );
4679 if( p_fmt->p_extra )
4680 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4681 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4685 /* For such stream we will gather them ourself and don't launch a
4687 * Yes it's ugly but it's the only way to have DIV3 working */
4688 p_fmt->b_packetized = true;
4691 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pid_t *pid )
4693 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4694 * with audio track pid being 0x1100..0x11FF */
4695 if ( p_pmt->i_program_number == 0x1 &&
4696 p_pmt->i_pcr_pid == 0x1000 &&
4697 ( pid->i_pid >> 8 ) == 0x11 )
4699 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4700 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4703 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4706 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
4707 const dvbpsi_pmt_es_t *p_es )
4709 es_format_t *p_fmt = &pid->es->fmt;
4711 /* Blu-Ray mapping */
4712 switch( p_es->i_type )
4715 p_fmt->i_cat = AUDIO_ES;
4716 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4719 p_fmt->i_cat = AUDIO_ES;
4720 p_fmt->i_codec = VLC_CODEC_A52;
4723 case 0x85: /* DTS-HD High resolution audio */
4724 case 0x86: /* DTS-HD Master audio */
4725 case 0xA2: /* Secondary DTS audio */
4726 p_fmt->i_cat = AUDIO_ES;
4727 p_fmt->i_codec = VLC_CODEC_DTS;
4730 case 0x83: /* TrueHD AC3 */
4731 p_fmt->i_cat = AUDIO_ES;
4732 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4735 case 0x84: /* E-AC3 */
4736 case 0xA1: /* Secondary E-AC3 */
4737 p_fmt->i_cat = AUDIO_ES;
4738 p_fmt->i_codec = VLC_CODEC_EAC3;
4740 case 0x90: /* Presentation graphics */
4741 p_fmt->i_cat = SPU_ES;
4742 p_fmt->i_codec = VLC_CODEC_BD_PG;
4744 case 0x91: /* Interactive graphics */
4745 case 0x92: /* Subtitle */
4748 p_fmt->i_cat = VIDEO_ES;
4749 p_fmt->i_codec = VLC_CODEC_VC1;
4752 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4753 p_es->i_pid, p_es->i_type );
4760 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
4761 const dvbpsi_pmt_es_t *p_es )
4767 vlc_fourcc_t i_codec;
4769 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4770 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4771 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4772 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4773 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4774 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4775 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4776 { "", UNKNOWN_ES, 0 }
4778 es_format_t *p_fmt = &pid->es->fmt;
4780 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4782 if( PMTEsHasRegistration( p_demux, p_es, p_regs[i].psz_tag ) )
4784 p_fmt->i_cat = p_regs[i].i_cat;
4785 p_fmt->i_codec = p_regs[i].i_codec;
4786 if (p_es->i_type == 0x87)
4787 p_fmt->i_codec = VLC_CODEC_EAC3;
4794 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4796 static const char *audio_type[] = {
4798 N_("clean effects"),
4799 N_("hearing impaired"),
4800 N_("visual impaired commentary"),
4803 if (type < 0 || type > 3)
4804 msg_Dbg( p_demux, "unknown audio type: %d", type);
4806 return strdup(audio_type[type]);
4810 static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
4811 const dvbpsi_pmt_es_t *p_es )
4813 /* get language descriptor */
4814 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
4819 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4822 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4826 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4827 pid->es->fmt.psz_language = malloc( 4 );
4828 if( pid->es->fmt.psz_language )
4830 memcpy( pid->es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
4831 pid->es->fmt.psz_language[3] = 0;
4832 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
4834 int type = p_decoded->code[0].i_audio_type;
4835 pid->es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
4837 pid->es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
4839 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
4840 if( pid->es->fmt.i_extra_languages > 0 )
4841 pid->es->fmt.p_extra_languages =
4842 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
4843 pid->es->fmt.i_extra_languages );
4844 if( pid->es->fmt.p_extra_languages )
4846 for( unsigned i = 0; i < pid->es->fmt.i_extra_languages; i++ )
4848 pid->es->fmt.p_extra_languages[i].psz_language = malloc(4);
4849 if( pid->es->fmt.p_extra_languages[i].psz_language )
4851 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
4852 p_decoded->code[i+1].iso_639_code, 3 );
4853 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
4855 int type = p_decoded->code[i].i_audio_type;
4856 pid->es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
4860 pid->es->fmt.psz_language = malloc( 4 );
4861 if( pid->es->fmt.psz_language )
4863 memcpy( pid->es->fmt.psz_language,
4864 p_decoded->i_iso_639_code, 3 );
4865 pid->es->fmt.psz_language[3] = 0;
4870 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid )
4872 demux_sys_t *p_sys = p_demux->p_sys;
4873 bool b_create_delayed = false;
4877 if( pid->b_seen && p_sys->b_delay_es_creation )
4879 p_sys->b_delay_es_creation = false;
4880 b_create_delayed = true;
4883 if( !p_sys->b_delay_es_creation )
4885 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4886 for( int i = 0; i < pid->i_extra_es; i++ )
4888 pid->extra_es[i]->id =
4889 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
4891 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4894 else if( p_sys->b_delay_es_creation )
4896 p_sys->b_delay_es_creation = false;
4897 b_create_delayed = true;
4900 if( b_create_delayed )
4902 for(int i=MIN_ES_PID; i<=MAX_ES_PID; i++)
4904 pid = &p_sys->pid[i];
4905 if(!pid->es || pid->es->id)
4907 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4908 for( int j = 0; j < pid->i_extra_es; j++ )
4910 pid->extra_es[j]->id =
4911 es_out_Add( p_demux->out, &pid->extra_es[j]->fmt );
4913 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4918 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
4920 demux_t *p_demux = data;
4921 demux_sys_t *p_sys = p_demux->p_sys;
4923 ts_pid_t *pmt = NULL;
4926 msg_Dbg( p_demux, "PMTCallBack called" );
4928 /* First find this PMT declared in PAT */
4929 for( int i = 0; !pmt && i < p_sys->i_pmt; i++ )
4930 for( int i_prg = 0; !pmt && i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
4932 const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
4933 if( i_pmt_number != TS_USER_PMT_NUMBER &&
4934 i_pmt_number == p_pmt->i_program_number )
4936 pmt = p_sys->pmt[i];
4937 prg = p_sys->pmt[i]->psi->prg[i_prg];
4943 msg_Warn( p_demux, "unreferenced program (broken stream)" );
4944 dvbpsi_DeletePMT(p_pmt);
4949 if( prg->i_version != -1 &&
4950 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
4952 dvbpsi_DeletePMT( p_pmt );
4956 ts_pid_t **pp_clean = NULL;
4958 /* Clean this program (remove all es) */
4959 for( int i = 0; i < 8192; i++ )
4961 ts_pid_t *pid = &p_sys->pid[i];
4963 if( pid->b_valid && pid->p_owner == pmt->psi &&
4964 pid->i_owner_number == prg->i_number && pid->psi == NULL )
4966 TAB_APPEND( i_clean, pp_clean, pid );
4971 IODFree( prg->iod );
4975 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
4976 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
4977 prg->i_pid_pcr = p_pmt->i_pcr_pid;
4978 prg->i_version = p_pmt->i_version;
4980 ValidateDVBMeta( p_demux, prg->i_pid_pcr );
4981 if( ProgramIsSelected( p_demux, prg->i_number ) )
4982 SetPIDFilter( p_demux, prg->i_pid_pcr, true ); /* Set demux filter */
4984 /* Parse PMT descriptors */
4985 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
4986 dvbpsi_descriptor_t *p_dr;
4988 /* First pass for standard detection */
4989 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
4991 int i_arib_flags = 0; /* Descriptors can be repeated */
4992 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
4998 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
4999 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
5003 i_arib_flags |= 1 << 1;
5006 i_arib_flags |= 1 << 2;
5012 if ( i_arib_flags == 0x07 ) //0b111
5013 p_sys->arib.e_mode = ARIBMODE_ENABLED;
5016 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5018 /* special descriptors handling */
5021 case 0x1d: /* We have found an IOD descriptor */
5022 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
5023 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
5027 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5028 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5031 case 0x5: /* Registration Descriptor */
5032 if( p_dr->i_length != 4 )
5034 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
5038 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
5039 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
5040 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
5045 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
5049 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
5052 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
5053 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
5054 /* TODO: apply visibility flags */
5058 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
5061 dvbpsi_pmt_es_t *p_es;
5062 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
5064 ts_pid_t tmp_pid = {0}, *old_pid = {0}, *pid = &tmp_pid;
5066 /* Find out if the PID was already declared */
5067 for( int i = 0; i < i_clean; i++ )
5069 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
5071 old_pid = pp_clean[i];
5075 ValidateDVBMeta( p_demux, p_es->i_pid );
5077 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
5079 msg_Warn( p_demux, " * PMT error: pid=%d already defined",
5084 char const * psz_typedesc = "";
5085 switch(p_es->i_type)
5088 psz_typedesc = "ISO/IEC Reserved";
5091 psz_typedesc = "ISO/IEC 11172 Video";
5094 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
5097 psz_typedesc = "ISO/IEC 11172 Audio";
5100 psz_typedesc = "ISO/IEC 13818-3 Audio";
5103 psz_typedesc = "ISO/IEC 13818-1 private_sections";
5106 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
5109 psz_typedesc = "ISO/IEC 13522 MHEG";
5112 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
5115 psz_typedesc = "ITU-T Rec. H.222.1";
5118 psz_typedesc = "ISO/IEC 13818-6 type A";
5121 psz_typedesc = "ISO/IEC 13818-6 type B";
5124 psz_typedesc = "ISO/IEC 13818-6 type C";
5127 psz_typedesc = "ISO/IEC 13818-6 type D";
5130 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
5133 if (p_es->i_type >= 0x0F && p_es->i_type <=0x7F)
5134 psz_typedesc = "ISO/IEC 13818-1 Reserved";
5136 psz_typedesc = "User Private";
5139 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
5140 p_es->i_pid, p_es->i_type, psz_typedesc );
5142 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
5143 p_dr = p_dr->p_next )
5145 msg_Dbg( p_demux, " - descriptor tag 0x%x",
5149 PIDInit( pid, false, pmt->psi );
5150 PIDFillFormat( &pid->es->fmt, p_es->i_type );
5151 pid->i_owner_number = prg->i_number;
5152 pid->i_pid = p_es->i_pid;
5153 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
5156 bool b_registration_applied = false;
5157 if ( p_es->i_type >= 0x80 ) /* non standard, extensions */
5159 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
5161 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, pid, p_es ) ))
5162 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
5163 p_es->i_pid, p_es->i_type );
5167 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, pid, p_es ) ))
5168 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
5169 p_es->i_pid, p_es->i_type );
5173 if ( !b_registration_applied )
5175 switch( p_es->i_type )
5178 /* Handle PES private data */
5179 PMTSetupEs0x06( p_demux, pid, p_es );
5181 /* All other private or reserved types */
5186 PMTSetupEsISO14496( p_demux, pid, prg, p_es );
5190 PMTSetupEs0x83( p_pmt, pid );
5193 PMTSetupEs0xA0( p_demux, pid, p_es );
5196 PMTSetupEs0xD1( p_demux, pid, p_es );
5199 PMTSetupEs0xEA( p_demux, pid, p_es );
5205 if( pid->es->fmt.i_cat == AUDIO_ES ||
5206 ( pid->es->fmt.i_cat == SPU_ES &&
5207 pid->es->fmt.i_codec != VLC_CODEC_DVBS &&
5208 pid->es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
5210 PMTParseEsIso639( p_demux, pid, p_es );
5213 switch( pid->es->fmt.i_codec )
5215 case VLC_CODEC_SCTE_27:
5216 pid->es->data_type = TS_ES_DATA_TABLE_SECTION;
5219 //pid->es->data_type = TS_ES_DATA_PES;
5223 pid->es->fmt.i_group = p_pmt->i_program_number;
5224 for( int i = 0; i < pid->i_extra_es; i++ )
5225 pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
5227 if( pid->es->fmt.i_cat == UNKNOWN_ES )
5229 msg_Dbg( p_demux, " => pid %d content is *unknown*",
5234 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
5235 p_es->i_pid, (char*)&pid->es->fmt.i_codec );
5237 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
5239 /* Check if we can avoid restarting the ES */
5241 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
5242 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
5243 pid->es->fmt.i_extra == 0 &&
5244 pid->i_extra_es == old_pid->i_extra_es &&
5245 ( ( !pid->es->fmt.psz_language &&
5246 !old_pid->es->fmt.psz_language ) ||
5247 ( pid->es->fmt.psz_language &&
5248 old_pid->es->fmt.psz_language &&
5249 !strcmp( pid->es->fmt.psz_language,
5250 old_pid->es->fmt.psz_language ) ) ) )
5252 pid->i_cc = old_pid->i_cc;
5253 ts_es_t *e = pid->es;
5254 pid->es = old_pid->es;
5256 for( int i = 0; i < pid->i_extra_es; i++ )
5258 e = pid->extra_es[i];
5259 pid->extra_es[i] = old_pid->extra_es[i];
5260 old_pid->extra_es[i] = e;
5265 AddAndCreateES( p_demux, pid );
5269 /* Add ES to the list */
5272 PIDClean( p_demux, old_pid );
5273 TAB_REMOVE( i_clean, pp_clean, old_pid );
5275 p_sys->pid[p_es->i_pid] = *pid;
5277 p_dr = PMTEsFindDescriptor( p_es, 0x09 );
5278 if( p_dr && p_dr->i_length >= 2 )
5280 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5281 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5284 if( ProgramIsSelected( p_demux, prg->i_number ) && pid->es->id != NULL )
5285 SetPIDFilter( p_demux, p_es->i_pid, true ); /* Set demux filter */
5288 /* Set CAM descrambling */
5289 if( !ProgramIsSelected( p_demux, prg->i_number ) )
5291 dvbpsi_DeletePMT( p_pmt );
5293 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
5294 p_pmt ) != VLC_SUCCESS )
5296 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED )
5298 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
5299 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
5300 if (!p_sys->arib.b25stream)
5301 dvbpsi_DeletePMT( p_pmt );
5302 } else dvbpsi_DeletePMT( p_pmt );
5305 for( int i = 0; i < i_clean; i++ )
5307 if( ProgramIsSelected( p_demux, prg->i_number ) )
5308 SetPIDFilter( p_demux, pp_clean[i]->i_pid, false );
5310 PIDClean( p_demux, pp_clean[i] );
5316 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
5318 /* Some Dreambox streams have all PMT set to same pid */
5319 int i_prev_pid = -1;
5320 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
5322 p_program = p_program->p_next )
5324 if( p_program->i_pid == i_prev_pid )
5326 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
5327 return VLC_EGENERIC;
5329 i_prev_pid = p_program->i_pid;
5334 static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
5336 demux_t *p_demux = data;
5337 demux_sys_t *p_sys = p_demux->p_sys;
5338 dvbpsi_pat_program_t *p_program;
5339 ts_pid_t *pat = &p_sys->pid[0];
5341 msg_Dbg( p_demux, "PATCallBack called" );
5343 if( ( pat->psi->i_pat_version != -1 &&
5344 ( !p_pat->b_current_next ||
5345 p_pat->i_version == pat->psi->i_pat_version ) ) ||
5346 p_sys->b_user_pmt || PATCheck( p_demux, p_pat ) )
5348 dvbpsi_DeletePAT( p_pat );
5352 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
5353 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
5356 if( p_sys->i_pmt > 0 )
5359 ts_pid_t **pmt_rm = NULL;
5361 /* Search pmt to be deleted */
5362 for( int i = 0; i < p_sys->i_pmt; i++ )
5364 ts_pid_t *pmt = p_sys->pmt[i];
5365 bool b_keep = false;
5367 for( p_program = p_pat->p_first_program; !b_keep && p_program;
5368 p_program = p_program->p_next )
5370 if( p_program->i_pid != pmt->i_pid )
5373 for( int i_prg = 0; !b_keep && i_prg < pmt->psi->i_prg; i_prg++ )
5374 if( p_program->i_number == pmt->psi->prg[i_prg]->i_number )
5381 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
5384 /* Delete all ES attached to thoses PMT */
5385 for( int i = 2; i < 8192; i++ )
5387 ts_pid_t *pid = &p_sys->pid[i];
5389 if( !pid->b_valid || pid->psi )
5392 for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
5394 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
5396 /* We only remove es that aren't defined by extra pmt */
5397 if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
5401 SetPIDFilter( p_demux, i, false );
5403 PIDClean( p_demux, pid );
5409 /* Delete PMT pid */
5410 for( int i = 0; i < i_pmt_rm; i++ )
5412 ts_pid_t *pid = pmt_rm[i];
5413 SetPIDFilter( p_demux, pid->i_pid, false );
5415 for( int i_prg = 0; i_prg < pid->psi->i_prg; i_prg++ )
5417 const int i_number = pid->psi->prg[i_prg]->i_number;
5418 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
5421 PIDClean( p_demux, &p_sys->pid[pid->i_pid] );
5422 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
5428 /* now create programs */
5429 for( p_program = p_pat->p_first_program; p_program != NULL;
5430 p_program = p_program->p_next )
5432 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
5434 if( p_program->i_number == 0 )
5437 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
5439 ValidateDVBMeta( p_demux, p_program->i_pid );
5444 for( int i_prg = 0; b_add && i_prg < pmt->psi->i_prg; i_prg++ )
5445 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
5453 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
5456 PIDInit( pmt, true, pat->psi );
5457 ts_prg_psi_t *prg = pmt->psi->prg[pmt->psi->i_prg-1];
5458 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
5459 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
5462 dvbpsi_DeletePAT( p_pat );
5465 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
5466 if( !dvbpsi_pmt_attach( prg->handle, p_program->i_number, PMTCallBack, p_demux ) )
5467 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5468 p_program->i_number );
5470 prg->handle = dvbpsi_AttachPMT( p_program->i_number, PMTCallBack, p_demux );
5472 prg->i_number = p_program->i_number;
5473 prg->i_pid_pmt = p_program->i_pid;
5475 /* Now select PID at access level */
5476 if( ProgramIsSelected( p_demux, p_program->i_number ) )
5478 if( p_sys->i_current_program == 0 )
5479 p_sys->i_current_program = p_program->i_number;
5481 if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
5482 p_sys->b_access_control = false;
5485 pat->psi->i_pat_version = p_pat->i_version;
5487 dvbpsi_DeletePAT( p_pat );