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")
205 typedef struct ts_pid_t ts_pid_t;
209 uint8_t i_objectTypeIndication;
210 uint8_t i_streamType;
215 } decoder_config_descriptor_t;
224 decoder_config_descriptor_t dec_descr;
226 } es_mpeg4_descriptor_t;
228 #define ES_DESCRIPTOR_COUNT 255
234 es_mpeg4_descriptor_t es_descr[ES_DESCRIPTOR_COUNT];
243 DECL_ARRAY(ts_pid_t *) programs;
253 /* IOD stuff (mpeg4) */
254 iod_descriptor_t *iod;
256 DECL_ARRAY(ts_pid_t *) e_streams;
261 mtime_t i_first; // seen <> != -1
262 /* broken PCR handling */
265 bool b_disable; /* ignore PCR field, use dts */
277 es_mpeg4_descriptor_t *p_mpeg4desc;
283 TS_ES_DATA_TABLE_SECTION
289 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
290 DECL_ARRAY( ts_pes_es_t * ) extra_es;
292 ts_es_data_type_t data_type;
298 block_t * p_prepcr_outqueue;
305 /* for special PAT/SDT case */
306 dvbpsi_t *handle; /* PAT/SDT/EIT */
313 TS_PMT_REGISTRATION_NONE = 0,
314 TS_PMT_REGISTRATION_HDMV
315 } ts_pmt_registration_type_t;
321 ts_es_data_type_t data_type;
327 es_mpeg4_descriptor_t *p_mpeg4desc;
329 block_t * p_prepcr_outqueue;
351 #define SEEN(x) ((x)->i_flags & FLAG_SEEN)
352 #define SCRAMBLED(x) ((x).i_flags & FLAG_SCRAMBLED)
359 uint8_t i_cc; /* countinuity counter */
362 /* PSI owner (ie PMT -> PAT, ES -> PMT */
377 vlc_fourcc_t i_fourcc;
389 #define MIN_ES_PID 4 /* Should be 32.. broken muxers */
390 #define MAX_ES_PID 8190
391 #define MIN_PAT_INTERVAL CLOCK_FREQ // DVB is 500ms
393 #define FROM_SCALE(x) (VLC_TS_0 + ((x) * 100 / 9))
394 #define TO_SCALE(x) (((x) - VLC_TS_0) * 9 / 100)
396 #define PID_ALLOC_CHUNK 16
403 vlc_mutex_t csa_lock;
405 /* TS packet size (188, 192, 204) */
406 unsigned i_packet_size;
408 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
409 unsigned i_packet_header_size;
411 /* how many TS packet we read at once */
414 bool b_force_seek_per_percent;
420 arib_instance_t *p_instance;
430 /* all non commons ones, dynamically allocated */
434 /* last recently used */
441 bool b_es_all; /* If we need to return all es/programs */
445 NO_ES, /* for preparse */
449 #define PREPARSING p_sys->es_creation == NO_ES
453 uint16_t i_next_extraid;
462 bool b_access_control;
469 int64_t i_dvb_length;
470 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
472 /* Selected programs */
473 DECL_ARRAY( int ) programs; /* List of selected/access-filtered programs */
474 bool b_default_selection; /* True if set by default to first pmt seen (to get data from filtered access) */
478 mtime_t i_first_dts; /* first dts encountered for the stream */
479 int i_timesourcepid; /* which pid we saved the dts from */
480 bool b_pat_deadline; /* set if we haven't seen PAT within MIN_PAT_INTERVAL */
489 static int Demux ( demux_t *p_demux );
490 static int Control( demux_t *p_demux, int i_query, va_list args );
492 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
494 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent );
495 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid );
497 static void PATCallBack( void*, dvbpsi_pat_t * );
498 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
499 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
500 uint16_t i_extension, demux_t * );
502 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
505 static ts_pat_t *ts_pat_New( demux_t * );
506 static void ts_pat_Del( demux_t *, ts_pat_t * );
507 static ts_pmt_t *ts_pmt_New( demux_t * );
508 static void ts_pmt_Del( demux_t *, ts_pmt_t * );
509 static ts_pes_t *ts_pes_New( demux_t * );
510 static void ts_pes_Del( demux_t *, ts_pes_t * );
511 static ts_psi_t *ts_psi_New( demux_t * );
512 static void ts_psi_Del( demux_t *, ts_psi_t * );
515 static ts_pid_t *PID( demux_sys_t *, uint16_t i_pid );
516 static ts_pmt_t * GetProgramByID( demux_sys_t *, int i_program );
517 static bool ProgramIsSelected( demux_sys_t *, uint16_t i_pgrm );
518 static void UpdatePESFilters( demux_t *p_demux, bool b_all );
519 static inline void FlushESBuffer( ts_pes_t *p_pes );
520 static void UpdateScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool );
521 static inline int PIDGet( block_t *p )
523 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
526 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
527 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid, bool );
528 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_prg, mtime_t i_pcr );
530 static block_t* ReadTSPacket( demux_t *p_demux );
531 static int ProbeStart( demux_t *p_demux, int i_program );
532 static int ProbeEnd( demux_t *p_demux, int i_program );
533 static int SeekToTime( demux_t *p_demux, ts_pmt_t *, int64_t time );
534 static void ReadyQueuesPostSeek( demux_t *p_demux );
535 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
536 static void PCRFixHandle( demux_t *, ts_pmt_t *, block_t * );
537 static int64_t TimeStampWrapAround( ts_pmt_t *, int64_t );
539 static void IODFree( iod_descriptor_t * );
541 #define TS_USER_PMT_NUMBER (0)
542 static int UserPmt( demux_t *p_demux, const char * );
544 static int SetPIDFilter( demux_sys_t *, ts_pid_t *, bool b_selected );
546 #define TS_PACKET_SIZE_188 188
547 #define TS_PACKET_SIZE_192 192
548 #define TS_PACKET_SIZE_204 204
549 #define TS_PACKET_SIZE_MAX 204
550 #define TS_HEADER_SIZE 4
552 static int DetectPacketSize( demux_t *p_demux, unsigned *pi_header_size, int i_offset )
554 const uint8_t *p_peek;
556 if( stream_Peek( p_demux->s,
557 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
560 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
562 if( p_peek[i_offset + i_sync] != 0x47 )
565 /* Check next 3 sync bytes */
566 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
567 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
569 msg_Err( p_demux, "cannot peek" );
572 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
573 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
574 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
576 return TS_PACKET_SIZE_188;
578 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
579 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
580 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
584 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
586 return TS_PACKET_SIZE_192;
588 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
589 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
590 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
592 return TS_PACKET_SIZE_204;
596 if( p_demux->b_force )
598 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
599 return TS_PACKET_SIZE_188;
601 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
605 #define TOPFIELD_HEADER_SIZE 3712
607 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, unsigned *pi_header_size, vdr_info_t *p_vdr )
609 const uint8_t *p_peek;
611 int i_packet_size = -1;
613 if( stream_Peek( p_demux->s,
614 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
617 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
618 p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
619 stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
620 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
622 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
623 if( i_packet_size != -1 )
625 msg_Dbg( p_demux, "this is a topfield file" );
627 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
628 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
629 * but after the filename the offsets seem to be incorrect. - DJ */
630 int i_duration, i_name;
631 char *psz_name = xmalloc(25);
632 char *psz_event_name;
633 char *psz_event_text = xmalloc(130);
634 char *psz_ext_text = xmalloc(1025);
636 // 2 bytes version Uimsbf (4,5)
637 // 2 bytes reserved (6,7)
638 // 2 bytes duration in minutes Uimsbf (8,9(
639 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
640 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
641 // 2 bytes service number in channel list (10, 11)
642 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
643 // 4 bytes of reserved + tuner info (14,15,16,17)
644 // 2 bytes of Service ID Bslbf (18,19)
645 // 2 bytes of PMT PID Uimsbf (20,21)
646 // 2 bytes of PCR PID Uimsbf (22,23)
647 // 2 bytes of Video PID Uimsbf (24,25)
648 // 2 bytes of Audio PID Uimsbf (26,27)
649 // 24 bytes filename Bslbf
650 memcpy( psz_name, &p_peek[28], 24 );
652 msg_Dbg( p_demux, "recordingname=%s", psz_name );
653 // 1 byte of sat index Uimsbf (52)
654 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
655 // 4 bytes of freq. Uimsbf (56,57,58,59)
656 // 2 bytes of symbol rate Uimsbf (60,61)
657 // 2 bytes of TS stream ID Uimsbf (62,63)
660 // 2 bytes duration Uimsbf (70,71)
661 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
662 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
663 // 4 bytes EventID Uimsbf (72-75)
664 // 8 bytes of Start and End time info (76-83)
665 // 1 byte reserved (84)
666 // 1 byte event name length Uimsbf (89)
667 i_name = (int)(p_peek[89]&~0x81);
668 msg_Dbg( p_demux, "event name length = %d", i_name);
669 psz_event_name = xmalloc( i_name+1 );
670 // 1 byte parental rating (90)
671 // 129 bytes of event text
672 memcpy( psz_event_name, &p_peek[91], i_name );
673 psz_event_name[i_name] = '\0';
674 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
675 psz_event_text[129-i_name] = '\0';
676 msg_Dbg( p_demux, "event name=%s", psz_event_name );
677 msg_Dbg( p_demux, "event text=%s", psz_event_text );
678 // 12 bytes reserved (220)
680 // 2 bytes Event Text Length Uimsbf
681 // 4 bytes EventID Uimsbf
682 // FIXME We just have 613 bytes. not enough for this entire text
683 // 1024 bytes Extended Event Text Bslbf
684 memcpy( psz_ext_text, p_peek+372, 1024 );
685 psz_ext_text[1024] = '\0';
686 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
687 // 52 bytes reserved Bslbf
689 p_vdr->i_service = GetWBE(&p_peek[18]);
691 return i_packet_size;
692 //return TS_PACKET_SIZE_188;
696 return DetectPacketSize( p_demux, pi_header_size, 0 );
699 static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
701 return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
702 (mtime_t)(p_data[1] << 22)|
703 ((mtime_t)(p_data[2]&0xfe) << 14)|
704 (mtime_t)(p_data[3] << 7)|
705 (mtime_t)(p_data[4] >> 1);
708 static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, size_t i_data, bool b_adaptfield )
710 demux_sys_t *p_sys = p_demux->p_sys;
711 const uint8_t *p_pes = p_pesstart;
712 pid->probed.i_type = -1;
719 uint8_t len = *p_pes;
724 p_pes++; i_data--;/* stuffing */
730 if( len >= 7 && (p_pes[1] & 0x10) )
731 pid->probed.i_pcr_count++;
740 if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
743 size_t i_pesextoffset = 8;
745 if( p_pes[7] & 0x80 ) // PTS
748 if ( i_data < i_pesextoffset )
750 i_dts = ExtractPESTimestamp( &p_pes[9] );
752 if( p_pes[7] & 0x40 ) // DTS
755 if ( i_data < i_pesextoffset )
757 i_dts = ExtractPESTimestamp( &p_pes[14] );
759 if( p_pes[7] & 0x20 ) // ESCR
761 if( p_pes[7] & 0x10 ) // ESrate
763 if( p_pes[7] & 0x08 ) // DSM
765 if( p_pes[7] & 0x04 ) // CopyInfo
767 if( p_pes[7] & 0x02 ) // PESCRC
770 if ( i_data < i_pesextoffset )
773 /* HeaderdataLength */
774 const size_t i_payloadoffset = 8 + 1 + p_pes[8];
777 if ( i_data < i_pesextoffset || i_data < i_payloadoffset )
780 i_data -= 8 + 1 + p_pes[8];
782 if( p_pes[7] & 0x01 ) // PESExt
784 size_t i_extension2_offset = 1;
785 if ( p_pes[i_pesextoffset] & 0x80 ) // private data
786 i_extension2_offset += 16;
787 if ( p_pes[i_pesextoffset] & 0x40 ) // pack
788 i_extension2_offset += 1;
789 if ( p_pes[i_pesextoffset] & 0x20 ) // seq
790 i_extension2_offset += 2;
791 if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
792 i_extension2_offset += 2;
793 if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
795 uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
796 i_extension2_offset += i_len;
798 if( i_data < i_extension2_offset )
801 i_data -= i_extension2_offset;
803 /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
808 const uint8_t *p_data = &p_pes[i_payloadoffset];
810 if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
812 if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
814 pid->probed.i_type = 0x06;
815 pid->probed.i_fourcc = VLC_CODEC_DTS;
817 else if( !memcmp( p_data, "\x0B\x77", 2 ) )
819 pid->probed.i_type = 0x06;
820 pid->probed.i_fourcc = VLC_CODEC_EAC3;
822 else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
824 switch(p_data[1] & 18)
826 /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
827 11 - MPEG Version 1 (ISO/IEC 11172-3) */
829 pid->probed.i_type = 0x04;
832 pid->probed.i_type = 0x03;
837 switch(p_data[1] & 6)
843 pid->probed.i_type = 0x04;
844 pid->probed.i_fourcc = VLC_CODEC_MPGA;
847 pid->probed.i_type = 0x04;
848 pid->probed.i_fourcc = VLC_CODEC_MP2;
851 pid->probed.i_type = 0x04;
852 pid->probed.i_fourcc = VLC_CODEC_MP3;
859 else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
861 if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
863 pid->probed.i_type = 0x1b;
864 pid->probed.i_fourcc = VLC_CODEC_H264;
866 else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
868 pid->probed.i_type = 0x02;
869 pid->probed.i_fourcc = VLC_CODEC_MPGV;
873 /* Track timestamps and flag missing PAT */
874 if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
876 p_sys->patfix.i_first_dts = i_dts;
877 p_sys->patfix.i_timesourcepid = pid->i_pid;
879 else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
880 !p_sys->patfix.b_pat_deadline )
882 if( i_dts - p_sys->patfix.i_first_dts > TO_SCALE(MIN_PAT_INTERVAL) )
883 p_sys->patfix.b_pat_deadline = true;
888 static void BuildPATCallback( void *p_opaque, block_t *p_block )
890 ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
891 dvbpsi_packet_push( pat_pid->u.p_pat->handle, p_block->p_buffer );
894 static void BuildPMTCallback( void *p_opaque, block_t *p_block )
896 ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
897 assert(program_pid->type == TYPE_PMT);
900 dvbpsi_packet_push( program_pid->u.p_pmt->handle,
902 p_block = p_block->p_next;
906 static void MissingPATPMTFixup( demux_t *p_demux )
908 demux_sys_t *p_sys = p_demux->p_sys;
909 int i_program_number = 1234;
910 int i_program_pid = 1337;
911 int i_pcr_pid = 0x1FFF;
914 ts_pid_t *p_program_pid = PID( p_sys, i_program_pid );
915 if( SEEN(p_program_pid) )
917 /* Find a free one */
918 for( i_program_pid = MIN_ES_PID;
919 i_program_pid <= MAX_ES_PID && SEEN(p_program_pid);
922 p_program_pid = PID( p_sys, i_program_pid );
926 for( int i=0; i<p_sys->pids.i_all; i++ )
928 const ts_pid_t *p_pid = p_sys->pids.pp_all[i];
930 p_pid->probed.i_type == -1 )
933 if( i_pcr_pid == 0x1FFF && ( p_pid->probed.i_type == 0x03 ||
934 p_pid->probed.i_pcr_count ) )
935 i_pcr_pid = p_pid->i_pid;
943 ts_stream_t patstream =
946 .i_continuity_counter = 0x10,
947 .b_discontinuity = false
950 ts_stream_t pmtprogramstream =
952 .i_pid = i_program_pid,
953 .i_continuity_counter = 0x0,
954 .b_discontinuity = false
957 BuildPAT( PID(p_sys, 0)->u.p_pat->handle,
958 &p_sys->pids.pat, BuildPATCallback,
961 1, &pmtprogramstream, &i_program_number );
963 /* PAT callback should have been triggered */
964 if( p_program_pid->type != TYPE_PMT )
966 msg_Err( p_demux, "PAT creation failed" );
975 es_format_t esfmt = {0};
976 struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
977 pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
978 if( esstreams && mapped )
981 for( int i=0; i<p_sys->pids.i_all; i++ )
983 const ts_pid_t *p_pid = p_sys->pids.pp_all[i];
986 p_pid->probed.i_type == -1 )
989 esstreams[j].pes.i_stream_type = p_pid->probed.i_type;
990 esstreams[j].pes.i_stream_type = p_pid->probed.i_type;
991 esstreams[j].ts.i_pid = p_pid->i_pid;
992 mapped[j].pes = &esstreams[j].pes;
993 mapped[j].ts = &esstreams[j].ts;
994 mapped[j].fmt = &esfmt;
998 BuildPMT( PID(p_sys, 0)->u.p_pat->handle, VLC_OBJECT(p_demux),
999 p_program_pid, BuildPMTCallback,
1003 1, &pmtprogramstream, &i_program_number,
1004 i_num_pes, mapped );
1010 /*****************************************************************************
1012 *****************************************************************************/
1013 static int Open( vlc_object_t *p_this )
1015 demux_t *p_demux = (demux_t*)p_this;
1019 unsigned i_packet_header_size = 0;
1022 vdr_info_t vdr = {0};
1024 /* Search first sync byte */
1025 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
1026 if( i_packet_size < 0 )
1027 return VLC_EGENERIC;
1029 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
1032 memset( p_sys, 0, sizeof( demux_sys_t ) );
1033 vlc_mutex_init( &p_sys->csa_lock );
1035 p_demux->pf_demux = Demux;
1036 p_demux->pf_control = Control;
1038 /* Init p_sys field */
1039 p_sys->b_dvb_meta = true;
1040 p_sys->b_access_control = true;
1041 p_sys->b_end_preparse = false;
1042 ARRAY_INIT( p_sys->programs );
1043 p_sys->b_default_selection = false;
1044 p_sys->i_tdt_delta = 0;
1045 p_sys->i_dvb_start = 0;
1046 p_sys->i_dvb_length = 0;
1050 p_sys->arib.b25stream = NULL;
1051 p_sys->stream = p_demux->s;
1053 p_sys->b_broken_charset = false;
1055 p_sys->pids.dummy.i_pid = 8191;
1056 p_sys->pids.dummy.i_flags = FLAG_SEEN;
1058 p_sys->i_packet_size = i_packet_size;
1059 p_sys->i_packet_header_size = i_packet_header_size;
1060 p_sys->i_ts_read = 50;
1062 p_sys->b_start_record = false;
1064 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
1066 if( !dvbpsi_AttachDemux( (table)->u.p_psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
1068 msg_Warn( obj, "Can't dvbpsi_AttachDemux on pid %d", (table)->i_pid );\
1072 /* Init PAT handler */
1073 patpid = PID(p_sys, 0);
1074 if ( !PIDSetup( p_demux, TYPE_PAT, patpid, NULL ) )
1076 vlc_mutex_destroy( &p_sys->csa_lock );
1080 if( !dvbpsi_pat_attach( patpid->u.p_pat->handle, PATCallBack, p_demux ) )
1082 PIDRelease( p_demux, patpid );
1083 vlc_mutex_destroy( &p_sys->csa_lock );
1085 return VLC_EGENERIC;
1088 if( p_sys->b_dvb_meta )
1090 if( !PIDSetup( p_demux, TYPE_SDT, PID(p_sys, 0x11), NULL ) ||
1091 !PIDSetup( p_demux, TYPE_EIT, PID(p_sys, 0x12), NULL ) ||
1092 !PIDSetup( p_demux, TYPE_TDT, PID(p_sys, 0x14), NULL ) )
1094 PIDRelease( p_demux, PID(p_sys, 0x11) );
1095 PIDRelease( p_demux, PID(p_sys, 0x12) );
1096 PIDRelease( p_demux, PID(p_sys, 0x14) );
1097 p_sys->b_dvb_meta = false;
1101 VLC_DVBPSI_DEMUX_TABLE_INIT(PID(p_sys, 0x11), p_demux);
1102 VLC_DVBPSI_DEMUX_TABLE_INIT(PID(p_sys, 0x12), p_demux);
1103 VLC_DVBPSI_DEMUX_TABLE_INIT(PID(p_sys, 0x14), p_demux);
1104 if( p_sys->b_access_control &&
1105 ( SetPIDFilter( p_sys, PID(p_sys, 0x11), true ) ||
1106 SetPIDFilter( p_sys, PID(p_sys, 0x14), true ) ||
1107 SetPIDFilter( p_sys, PID(p_sys, 0x12), true ) )
1109 p_sys->b_access_control = false;
1113 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
1115 p_sys->i_pmt_es = 0;
1116 p_sys->b_es_all = false;
1119 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
1120 p_sys->i_next_extraid = 1;
1122 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
1124 /* We handle description of an extra PMT */
1125 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
1126 p_sys->b_user_pmt = false;
1127 if( psz_string && *psz_string )
1128 UserPmt( p_demux, psz_string );
1131 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
1132 if( psz_string && *psz_string )
1137 p_sys->csa = csa_New();
1139 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
1140 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
1141 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
1143 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
1145 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1148 else if ( i_res == VLC_SUCCESS )
1150 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1154 csa_Delete( p_sys->csa );
1160 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
1161 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1163 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
1164 if( i_pkt < 4 || i_pkt > 188 )
1166 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
1167 msg_Warn( p_demux, "using default packet size of 188 bytes" );
1168 p_sys->i_csa_pkt_size = 188;
1171 p_sys->i_csa_pkt_size = i_pkt;
1172 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
1178 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
1180 p_sys->b_canseek = false;
1181 p_sys->b_canfastseek = false;
1182 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
1184 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
1186 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
1187 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1190 if( p_sys->b_canseek )
1192 p_sys->es_creation = NO_ES;
1193 while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
1194 if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
1196 p_sys->es_creation = DELAY_ES;
1199 p_sys->es_creation = ( p_sys->b_access_control ? CREATE_ES : DELAY_ES );
1204 /*****************************************************************************
1206 *****************************************************************************/
1207 static void Close( vlc_object_t *p_this )
1209 demux_t *p_demux = (demux_t*)p_this;
1210 demux_sys_t *p_sys = p_demux->p_sys;
1212 PIDRelease( p_demux, PID(p_sys, 0) );
1214 if( p_sys->b_dvb_meta )
1216 PIDRelease( p_demux, PID(p_sys, 0x11) );
1217 PIDRelease( p_demux, PID(p_sys, 0x12) );
1218 PIDRelease( p_demux, PID(p_sys, 0x14) );
1221 vlc_mutex_lock( &p_sys->csa_lock );
1224 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
1225 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1226 csa_Delete( p_sys->csa );
1228 vlc_mutex_unlock( &p_sys->csa_lock );
1230 ARRAY_RESET( p_sys->programs );
1233 if ( p_sys->arib.p_instance )
1234 arib_instance_destroy( p_sys->arib.p_instance );
1237 if ( p_sys->arib.b25stream )
1239 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
1240 stream_Delete( p_sys->arib.b25stream );
1243 vlc_mutex_destroy( &p_sys->csa_lock );
1245 /* Release all non default pids */
1246 for( int i = 0; i < p_sys->pids.i_all; i++ )
1248 ts_pid_t *pid = p_sys->pids.pp_all[i];
1250 if( pid->type != TYPE_FREE )
1251 msg_Err( p_demux, "PID %d type %d not freed", pid->i_pid, pid->type );
1255 free( p_sys->pids.pp_all );
1260 /*****************************************************************************
1261 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
1262 *****************************************************************************/
1263 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
1264 vlc_value_t oldval, vlc_value_t newval,
1267 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1268 demux_t *p_demux = (demux_t*)p_this;
1269 demux_sys_t *p_sys = p_demux->p_sys;
1270 int i_tmp = (intptr_t)p_data;
1272 vlc_mutex_lock( &p_sys->csa_lock );
1274 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
1276 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
1278 vlc_mutex_unlock( &p_sys->csa_lock );
1282 /*****************************************************************************
1284 *****************************************************************************/
1285 static int Demux( demux_t *p_demux )
1287 demux_sys_t *p_sys = p_demux->p_sys;
1288 bool b_wait_es = p_sys->i_pmt_es <= 0;
1290 /* If we had no PAT within MIN_PAT_INTERVAL, create PAT/PMT from probed streams */
1291 if( p_sys->i_pmt_es == 0 && !SEEN(PID(p_sys, 0)) && p_sys->patfix.b_pat_deadline )
1292 MissingPATPMTFixup( p_demux );
1294 /* We read at most 100 TS packet or until a frame is completed */
1295 for( unsigned i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1297 bool b_frame = false;
1299 if( !(p_pkt = ReadTSPacket( p_demux )) )
1301 return VLC_DEMUXER_EOF;
1304 if( p_sys->b_start_record )
1306 /* Enable recording once synchronized */
1307 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
1308 p_sys->b_start_record = false;
1311 /* Parse the TS packet */
1312 ts_pid_t *p_pid = PID( p_sys, PIDGet( p_pkt ) );
1314 if( SCRAMBLED(*p_pid) != !!(p_pkt->p_buffer[3] & 0x80) )
1315 UpdateScrambledState( p_demux, p_pid, p_pkt->p_buffer[3] & 0x80 );
1319 if( p_pid->type == TYPE_FREE )
1320 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1321 p_pid->i_flags |= FLAG_SEEN;
1324 if ( SCRAMBLED(*p_pid) && !p_demux->p_sys->csa )
1326 block_Release( p_pkt );
1330 /* Probe streams to build PAT/PMT after MIN_PAT_INTERVAL in case we don't see any PAT */
1331 if( !SEEN( PID( p_sys, 0 ) ) &&
1332 (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
1333 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1334 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
1336 ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
1337 p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
1340 switch( p_pid->type )
1343 dvbpsi_packet_push( p_pid->u.p_pat->handle, p_pkt->p_buffer );
1344 block_Release( p_pkt );
1348 dvbpsi_packet_push( p_pid->u.p_pmt->handle, p_pkt->p_buffer );
1349 block_Release( p_pkt );
1353 p_sys->b_end_preparse = true;
1355 if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
1357 msg_Dbg( p_demux, "Creating delayed ES" );
1358 AddAndCreateES( p_demux, p_pid, true );
1361 if( !p_sys->b_access_control && !(p_pid->i_flags & FLAG_FILTERED) )
1363 /* That packet is for an unselected ES, don't waste time/memory gathering its data */
1364 block_Release( p_pkt );
1368 b_frame = GatherData( p_demux, p_pid, p_pkt );
1374 if( p_sys->b_dvb_meta )
1375 dvbpsi_packet_push( p_pid->u.p_psi->handle, p_pkt->p_buffer );
1376 block_Release( p_pkt );
1380 /* We have to handle PCR if present */
1381 PCRHandle( p_demux, p_pid, p_pkt );
1382 block_Release( p_pkt );
1386 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1390 demux_UpdateTitleFromStream( p_demux );
1391 return VLC_DEMUXER_SUCCESS;
1394 /*****************************************************************************
1396 *****************************************************************************/
1397 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1399 demux_sys_t *p_sys = p_demux->p_sys;
1405 if( p_sys->i_dvb_length > 0 )
1407 const int64_t t = mdate() + p_sys->i_tdt_delta;
1409 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1412 *pi_length = p_sys->i_dvb_length;
1414 *pi_time = t - p_sys->i_dvb_start;
1418 return VLC_EGENERIC;
1421 static void UpdatePESFilters( demux_t *p_demux, bool b_all )
1423 demux_sys_t *p_sys = p_demux->p_sys;
1424 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
1425 for( int i=0; i< p_pat->programs.i_size; i++ )
1427 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1428 bool b_program_selected;
1429 if( (p_sys->b_default_selection && !p_sys->b_access_control) || b_all )
1430 b_program_selected = true;
1432 b_program_selected = ProgramIsSelected( p_sys, p_pmt->i_number );
1434 SetPIDFilter( p_sys, p_pat->programs.p_elems[i], b_program_selected );
1436 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
1438 ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
1439 bool b_stream_selected = b_program_selected;
1440 if( b_program_selected && !b_all && espid->u.p_pes->es.id )
1441 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1442 espid->u.p_pes->es.id, &b_stream_selected );
1444 if( !p_sys->b_es_all && espid->u.p_pes->es.fmt.i_cat == UNKNOWN_ES )
1445 b_stream_selected = false;
1447 if( b_stream_selected )
1448 msg_Dbg( p_demux, "enabling pid %d from program %d", espid->i_pid, p_pmt->i_number );
1450 SetPIDFilter( p_sys, espid, b_stream_selected );
1451 if( !b_stream_selected )
1452 FlushESBuffer( espid->u.p_pes );
1455 /* Select pcr last in case it is handled by unselected ES */
1456 if( p_pmt->i_pid_pcr > 0 )
1458 SetPIDFilter( p_sys, PID(p_sys, p_pmt->i_pid_pcr), b_program_selected );
1459 if( b_program_selected )
1460 msg_Dbg( p_demux, "enabling pcr pid %d from program %d", p_pmt->i_pid_pcr, p_pmt->i_number );
1465 static int Control( demux_t *p_demux, int i_query, va_list args )
1467 demux_sys_t *p_sys = p_demux->p_sys;
1469 bool b_bool, *pb_bool;
1474 int i_first_program = ( p_sys->programs.i_size ) ? p_sys->programs.p_elems[0] : 0;
1476 if( PREPARSING || !i_first_program || p_sys->b_default_selection )
1478 if( likely(PID(p_sys, 0)->type == TYPE_PAT) )
1480 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
1481 /* Set default program for preparse time (no program has been selected) */
1482 for( int i = 0; i < p_pat->programs.i_size; i++ )
1484 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
1485 p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1486 if( ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) && p_pmt->i_last_dts > 0 )
1488 i_first_program = p_pmt->i_number;
1497 case DEMUX_GET_POSITION:
1498 pf = (double*) va_arg( args, double* );
1500 /* Access control test is because EPG for recordings is not relevant */
1501 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1503 int64_t i_time, i_length;
1504 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1506 *pf = (double)i_time/(double)i_length;
1511 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1512 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1513 p_pmt->pcr.i_current > -1 )
1515 double i_length = TimeStampWrapAround( p_pmt,
1516 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1517 i_length += p_pmt->pcr.i_pcroffset;
1518 double i_pos = TimeStampWrapAround( p_pmt,
1519 p_pmt->pcr.i_current ) - p_pmt->pcr.i_first;
1520 *pf = i_pos / i_length;
1524 if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1526 int64_t offset = stream_Tell( p_sys->stream );
1527 *pf = (double)offset / (double)i64;
1532 case DEMUX_SET_POSITION:
1533 f = (double) va_arg( args, double );
1535 if(!p_sys->b_canseek)
1538 if( p_sys->b_dvb_meta && p_sys->b_access_control &&
1539 !p_sys->b_force_seek_per_percent &&
1540 (p_pmt = GetProgramByID( p_sys, i_first_program )) )
1542 int64_t i_time, i_length;
1543 if( !DVBEventInformation( p_demux, &i_time, &i_length ) &&
1544 i_length > 0 && !SeekToTime( p_demux, p_pmt, TO_SCALE(i_length) * f ) )
1546 ReadyQueuesPostSeek( p_demux );
1547 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1548 TO_SCALE(i_length) * f );
1553 if( !p_sys->b_force_seek_per_percent &&
1554 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1555 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1556 p_pmt->pcr.i_current > -1 )
1558 double i_length = TimeStampWrapAround( p_pmt,
1559 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1560 if( !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + i_length * f ) )
1562 ReadyQueuesPostSeek( p_demux );
1563 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1564 FROM_SCALE(p_pmt->pcr.i_first + i_length * f) );
1569 i64 = stream_Size( p_sys->stream );
1571 stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) == VLC_SUCCESS )
1573 ReadyQueuesPostSeek( p_demux );
1578 case DEMUX_SET_TIME:
1579 i64 = (int64_t)va_arg( args, int64_t );
1581 if( p_sys->b_canseek &&
1582 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1583 p_pmt->pcr.i_first > -1 &&
1584 !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + TO_SCALE(i64) ) )
1586 ReadyQueuesPostSeek( p_demux );
1587 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1588 FROM_SCALE(p_pmt->pcr.i_first) + i64 - VLC_TS_0 );
1593 case DEMUX_GET_TIME:
1594 pi64 = (int64_t*)va_arg( args, int64_t * );
1596 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1598 if( !DVBEventInformation( p_demux, pi64, NULL ) )
1602 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1603 p_pmt->pcr.i_current > -1 && p_pmt->pcr.i_first > -1 )
1605 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
1606 *pi64 = FROM_SCALE(i_pcr - p_pmt->pcr.i_first);
1611 case DEMUX_GET_LENGTH:
1612 pi64 = (int64_t*)va_arg( args, int64_t * );
1614 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1616 if( !DVBEventInformation( p_demux, NULL, pi64 ) )
1620 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1621 ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) &&
1622 p_pmt->i_last_dts > 0 )
1624 int64_t i_start = (p_pmt->pcr.i_first > -1) ? p_pmt->pcr.i_first :
1625 TO_SCALE(p_pmt->pcr.i_first_dts);
1626 int64_t i_last = TimeStampWrapAround( p_pmt, p_pmt->i_last_dts );
1627 i_last += p_pmt->pcr.i_pcroffset;
1628 *pi64 = FROM_SCALE(i_last - i_start);
1633 case DEMUX_SET_GROUP:
1637 i_int = (int)va_arg( args, int );
1638 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1639 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1641 if( i_int != 0 ) /* If not default program */
1643 /* Deselect/filter current ones */
1647 p_sys->b_es_all = false;
1648 ARRAY_APPEND( p_sys->programs, i_int );
1649 UpdatePESFilters( p_demux, false );
1651 else if( likely( p_list != NULL ) )
1653 p_sys->b_es_all = false;
1654 for( int i = 0; i < p_list->i_count; i++ )
1655 ARRAY_APPEND( p_sys->programs, p_list->p_values[i].i_int );
1656 UpdatePESFilters( p_demux, false );
1660 p_sys->b_es_all = true;
1661 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
1662 for( int i = 0; i < p_pat->programs.i_size; i++ )
1663 ARRAY_APPEND( p_sys->programs, p_pat->programs.p_elems[i]->i_pid );
1664 UpdatePESFilters( p_demux, true );
1667 p_sys->b_default_selection = false;
1675 i_int = (int)va_arg( args, int );
1676 msg_Dbg( p_demux, "DEMUX_SET_ES %d", i_int );
1678 if( !p_sys->b_es_all ) /* Won't change anything */
1679 UpdatePESFilters( p_demux, false );
1684 case DEMUX_GET_TITLE_INFO:
1686 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1687 int *c = va_arg( args, int * );
1689 *va_arg( args, int* ) = 0; /* Title offset */
1690 *va_arg( args, int* ) = 0; /* Chapter offset */
1691 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1694 case DEMUX_SET_TITLE:
1695 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1697 case DEMUX_SET_SEEKPOINT:
1698 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1700 case DEMUX_GET_META:
1701 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1703 case DEMUX_CAN_RECORD:
1704 pb_bool = (bool*)va_arg( args, bool * );
1708 case DEMUX_SET_RECORD_STATE:
1709 b_bool = (bool)va_arg( args, int );
1712 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1713 p_sys->b_start_record = b_bool;
1716 case DEMUX_GET_SIGNAL:
1717 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1723 return VLC_EGENERIC;
1726 /*****************************************************************************
1728 *****************************************************************************/
1729 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1731 demux_sys_t *p_sys = p_demux->p_sys;
1732 char *psz_dup = strdup( psz_fmt );
1733 char *psz = psz_dup;
1741 i_pid = strtol( psz, &psz, 0 );
1742 if( i_pid < 2 || i_pid >= 8192 )
1745 /* Parse optional program number */
1748 i_number = strtol( &psz[1], &psz, 0 );
1751 ts_pid_t *pmtpid = PID(p_sys, i_pid);
1753 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1754 if ( !PIDSetup( p_demux, TYPE_PMT, pmtpid, PID(p_sys, 0) ) )
1758 ts_pmt_t *p_pmt = pmtpid->u.p_pmt;
1759 p_pmt->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1760 if( !dvbpsi_pmt_attach( p_pmt->handle,
1761 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1762 PMTCallBack, p_demux ) )
1764 PIDRelease( p_demux, pmtpid );
1768 ARRAY_APPEND( PID(p_sys, 0)->u.p_pat->programs, pmtpid );
1770 psz = strchr( psz, '=' );
1773 while( psz && *psz )
1775 char *psz_next = strchr( psz, ',' );
1781 i_pid = strtol( psz, &psz, 0 );
1782 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1785 char *psz_opt = &psz[1];
1786 if( !strcmp( psz_opt, "pcr" ) )
1788 p_pmt->i_pid_pcr = i_pid;
1790 else if( PID(p_sys, i_pid)->type == TYPE_FREE )
1792 ts_pid_t *pid = PID(p_sys, i_pid);
1794 char *psz_arg = strchr( psz_opt, '=' );
1798 if ( !PIDSetup( p_demux, TYPE_PES, pid, pmtpid ) )
1801 ARRAY_APPEND( p_pmt->e_streams, pid );
1803 if( p_pmt->i_pid_pcr <= 0 )
1804 p_pmt->i_pid_pcr = i_pid;
1806 es_format_t *fmt = &pid->u.p_pes->es.fmt;
1808 if( psz_arg && strlen( psz_arg ) == 4 )
1810 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1811 psz_arg[2], psz_arg[3] );
1812 int i_cat = UNKNOWN_ES;
1814 if( !strcmp( psz_opt, "video" ) )
1816 else if( !strcmp( psz_opt, "audio" ) )
1818 else if( !strcmp( psz_opt, "spu" ) )
1821 es_format_Init( fmt, i_cat, i_codec );
1822 fmt->b_packetized = false;
1826 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1827 PIDFillFormat( fmt, i_stream_type );
1830 fmt->i_group = i_number;
1831 if( p_sys->b_es_id_pid )
1834 if( fmt->i_cat != UNKNOWN_ES )
1836 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1837 (char*)&fmt->i_codec );
1838 pid->u.p_pes->es.id = es_out_Add( p_demux->out, fmt );
1847 p_sys->b_user_pmt = true;
1853 return VLC_EGENERIC;
1856 static int SetPIDFilter( demux_sys_t *p_sys, ts_pid_t *p_pid, bool b_selected )
1859 p_pid->i_flags |= FLAG_FILTERED;
1861 p_pid->i_flags &= ~FLAG_FILTERED;
1863 if( !p_sys->b_access_control )
1864 return VLC_EGENERIC;
1866 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1867 p_pid->i_pid, b_selected );
1870 static void PIDReset( ts_pid_t *pid )
1872 assert(pid->i_refcount == 0);
1874 pid->i_flags &= ~FLAG_SCRAMBLED;
1875 pid->p_parent = NULL;
1876 pid->type = TYPE_FREE;
1879 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent )
1881 if( pid == p_parent || pid->i_pid == 0x1FFF )
1884 if( pid->i_refcount == 0 )
1886 assert( pid->type == TYPE_FREE );
1889 case TYPE_FREE: /* nonsense ?*/
1895 pid->u.p_pat = ts_pat_New( p_demux );
1902 pid->u.p_pmt = ts_pmt_New( p_demux );
1909 pid->u.p_pes = ts_pes_New( p_demux );
1918 pid->u.p_psi = ts_psi_New( p_demux );
1930 pid->p_parent = p_parent;
1932 else if( pid->type == i_type && pid->i_refcount < UINT8_MAX )
1938 if( pid->type != TYPE_FREE )
1939 msg_Warn( p_demux, "Tried to redeclare pid %d with another type", pid->i_pid );
1946 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid )
1948 if( pid->i_refcount == 0 )
1950 assert( pid->type == TYPE_FREE );
1953 else if( pid->i_refcount == 1 )
1957 else if( pid->i_refcount > 1 )
1959 assert( pid->type != TYPE_FREE && pid->type != TYPE_PAT );
1963 if( pid->i_refcount == 0 )
1968 case TYPE_FREE: /* nonsense ?*/
1969 assert( pid->type != TYPE_FREE );
1973 ts_pat_Del( p_demux, pid->u.p_pat );
1974 pid->u.p_pat = NULL;
1978 ts_pmt_Del( p_demux, pid->u.p_pmt );
1979 pid->u.p_pmt = NULL;
1983 ts_pes_Del( p_demux, pid->u.p_pes );
1984 pid->u.p_pes = NULL;
1990 ts_psi_Del( p_demux, pid->u.p_psi );
1991 pid->u.p_psi = NULL;
1996 SetPIDFilter( p_demux->p_sys, pid, false );
2001 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
2006 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
2017 static block_t *Opus_Parse(demux_t *demux, block_t *block)
2019 block_t *out = NULL;
2020 block_t **last = NULL;
2022 uint8_t *buf = block->p_buffer;
2023 size_t len = block->i_buffer;
2025 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
2026 int16_t start_trim = 0, end_trim = 0;
2027 int start_trim_flag = (buf[1] >> 4) & 1;
2028 int end_trim_flag = (buf[1] >> 3) & 1;
2029 int control_extension_flag = (buf[1] >> 2) & 1;
2034 unsigned au_size = 0;
2042 if (start_trim_flag) {
2043 start_trim = read_opus_flag(&buf, &len);
2044 if (start_trim < 0) {
2045 msg_Err(demux, "Invalid start trimming flag");
2048 if (end_trim_flag) {
2049 end_trim = read_opus_flag(&buf, &len);
2051 msg_Err(demux, "Invalid end trimming flag");
2054 if (control_extension_flag && len) {
2055 unsigned l = *buf++; len--;
2057 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
2064 if (!au_size || au_size > len) {
2065 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
2069 block_t *au = block_Alloc(au_size);
2072 memcpy(au->p_buffer, buf, au_size);
2073 block_CopyProperties(au, block);
2082 au->i_nb_samples = opus_frame_duration(buf, au_size);
2083 if (end_trim && (uint16_t) end_trim <= au->i_nb_samples)
2084 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
2088 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
2089 au->i_nb_samples -= start_trim;
2090 if (au->i_nb_samples == 0)
2091 au->i_flags |= BLOCK_FLAG_PREROLL;
2098 block_Release(block);
2102 /****************************************************************************
2104 ****************************************************************************/
2105 static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header, size_t i_header,
2106 unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
2111 return VLC_EGENERIC;
2113 switch( p_header[3] )
2115 case 0xBC: /* Program stream map */
2116 case 0xBE: /* Padding */
2117 case 0xBF: /* Private stream 2 */
2118 case 0xF0: /* ECM */
2119 case 0xF1: /* EMM */
2120 case 0xFF: /* Program stream directory */
2121 case 0xF2: /* DSMCC stream */
2122 case 0xF8: /* ITU-T H.222.1 type E stream */
2126 if( ( p_header[6]&0xC0 ) == 0x80 )
2129 i_skip = p_header[8] + 9;
2131 if( p_header[7]&0x80 ) /* has pts */
2133 if( i_header < 9 + 5 )
2134 return VLC_EGENERIC;
2135 *pi_pts = ExtractPESTimestamp( &p_header[9] );
2137 if( p_header[7]&0x40 ) /* has dts */
2139 if( i_header < 14 + 5 )
2140 return VLC_EGENERIC;
2141 *pi_dts = ExtractPESTimestamp( &p_header[14] );
2148 if( i_header < i_skip + 1 )
2149 return VLC_EGENERIC;
2150 while( i_skip < 23 && p_header[i_skip] == 0xff )
2153 if( i_header < i_skip + 1 )
2154 return VLC_EGENERIC;
2158 msg_Err( p_demux, "too much MPEG-1 stuffing" );
2159 return VLC_EGENERIC;
2161 if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
2166 if( i_header < i_skip + 1 )
2167 return VLC_EGENERIC;
2169 if( p_header[i_skip]&0x20 )
2171 if( i_header < i_skip + 5 )
2172 return VLC_EGENERIC;
2173 *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
2175 if( p_header[i_skip]&0x10 ) /* has dts */
2177 if( i_header < i_skip + 10 )
2178 return VLC_EGENERIC;
2179 *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
2199 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
2202 unsigned i_pes_size = 0;
2203 unsigned i_skip = 0;
2206 mtime_t i_length = 0;
2208 assert(pid->type == TYPE_PES);
2209 assert(pid->p_parent && pid->p_parent->type == TYPE_PMT);
2211 const int i_max = block_ChainExtract( p_pes, header, 34 );
2214 block_ChainRelease( p_pes );
2218 if( SCRAMBLED(*pid) || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2220 if ( !SCRAMBLED(*pid) )
2221 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
2222 header[0], header[1],header[2],header[3], pid->i_pid );
2223 block_ChainRelease( p_pes );
2227 if( ParsePESHeader( p_demux, (uint8_t*)&header, i_max, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
2229 block_ChainRelease( p_pes );
2235 i_pts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_pts );
2237 i_dts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_dts );
2240 if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
2241 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
2245 else if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
2246 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
2247 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
2251 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT &&
2252 pid->u.p_pes->es.p_mpeg4desc )
2254 decoder_config_descriptor_t *dcd = &pid->u.p_pes->es.p_mpeg4desc->dec_descr;
2256 if( dcd->i_extra > 2 &&
2257 dcd->p_extra[0] == 0x10 &&
2258 ( dcd->p_extra[1]&0x10 ) )
2260 /* display length */
2261 if( p_pes->i_buffer + 2 <= i_skip )
2262 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
2266 if( p_pes->i_buffer + 2 <= i_skip )
2267 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
2273 while( p_pes && i_skip > 0 )
2275 if( p_pes->i_buffer <= i_skip )
2277 block_t *p_next = p_pes->p_next;
2279 i_skip -= p_pes->i_buffer;
2280 block_Release( p_pes );
2285 p_pes->i_buffer -= i_skip;
2286 p_pes->p_buffer += i_skip;
2291 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
2292 if( i_pts >= 0 && i_dts < 0 )
2300 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
2303 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
2305 p_pes->i_length = i_length * 100 / 9;
2307 p_block = block_ChainGather( p_pes );
2308 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT )
2310 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2312 p_block->i_buffer = i_pes_size;
2315 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2318 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2320 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_TELETEXT )
2322 if( p_block->i_pts <= VLC_TS_INVALID && pid->p_parent )
2324 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
2325 * In this case use the last PCR + 40ms */
2326 assert( pid->p_parent->type == TYPE_PMT );
2327 if( likely(pid->p_parent->type == TYPE_PMT) )
2329 mtime_t i_pcr = pid->p_parent->u.p_pmt->pcr.i_current;
2330 if( i_pcr > VLC_TS_INVALID )
2331 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
2335 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_A ||
2336 pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_C )
2338 if( p_block->i_pts <= VLC_TS_INVALID )
2340 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2342 p_block->i_buffer = i_pes_size;
2345 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2348 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2351 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_OPUS)
2353 p_block = Opus_Parse(p_demux, p_block);
2356 if( !pid->p_parent || pid->p_parent->type != TYPE_PMT )
2358 block_Release( p_block );
2362 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2365 block_t *p_next = p_block->p_next;
2366 p_block->p_next = NULL;
2368 if( !p_pmt->pcr.b_fix_done ) /* Not seen yet */
2369 PCRFixHandle( p_demux, p_pmt, p_block );
2371 if( pid->u.p_pes->es.id && (p_pmt->pcr.i_current > -1 || p_pmt->pcr.b_disable) )
2373 if( pid->u.p_pes->p_prepcr_outqueue )
2375 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2376 p_block = pid->u.p_pes->p_prepcr_outqueue;
2377 p_next = p_block->p_next;
2378 p_block->p_next = NULL;
2379 pid->u.p_pes->p_prepcr_outqueue = NULL;
2382 if ( p_pmt->pcr.b_disable && p_block->i_dts > VLC_TS_INVALID &&
2383 ( p_pmt->i_pid_pcr == pid->i_pid || p_pmt->i_pid_pcr == 0x1FFF ) )
2385 ProgramSetPCR( p_demux, p_pmt, (p_block->i_dts - VLC_TS_0) * 9 / 100 - 120000 );
2388 /* Compute PCR/DTS offset if any */
2389 if( p_pmt->pcr.i_pcroffset == -1 && p_block->i_dts > VLC_TS_INVALID &&
2390 p_pmt->pcr.i_current > VLC_TS_INVALID )
2392 int64_t i_dts27 = (p_block->i_dts - VLC_TS_0) * 9 / 100;
2393 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
2394 if( i_dts27 < i_pcr )
2396 p_pmt->pcr.i_pcroffset = i_pcr - i_dts27 + 80000;
2397 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64
2398 "us later than pcr, applying delay",
2399 pid->i_pid, p_pmt->pcr.i_pcroffset * 100 / 9 );
2401 else p_pmt->pcr.i_pcroffset = 0;
2404 if( p_pmt->pcr.i_pcroffset != -1 )
2406 if( p_block->i_dts > VLC_TS_INVALID )
2407 p_block->i_dts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2408 if( p_block->i_pts > VLC_TS_INVALID )
2409 p_block->i_pts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2412 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
2414 es_out_Send( p_demux->out, pid->u.p_pes->extra_es.p_elems[i]->id,
2415 block_Duplicate( p_block ) );
2418 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2422 if( !p_pmt->pcr.b_fix_done ) /* Not seen yet */
2423 PCRFixHandle( p_demux, p_pmt, p_block );
2425 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2433 msg_Warn( p_demux, "empty pes" );
2437 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
2439 block_t *p_content = block_ChainGather( p_data );
2441 if ( pid->p_parent && pid->p_parent->type == TYPE_PMT )
2443 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2444 mtime_t i_date = p_pmt->pcr.i_current;
2446 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SCTE_27 )
2448 /* We need to extract the truncated pts stored inside the payload */
2449 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
2452 size_t i_offset = 4;
2453 if( p_content->p_buffer[3] & 0x40 )
2455 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
2456 p_content->p_buffer[8];
2459 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
2461 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
2464 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
2465 if( i_display_in < i_date )
2466 i_date = i_display_in + (1ll << 32);
2468 i_date = i_display_in;
2475 p_content->i_dts = p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
2476 PCRFixHandle( p_demux, p_pmt, p_content );
2479 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_content );
2482 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
2484 block_t *p_data = pid->u.p_pes->p_data;
2489 /* remove the pes from pid */
2490 pid->u.p_pes->p_data = NULL;
2491 pid->u.p_pes->i_data_size = 0;
2492 pid->u.p_pes->i_data_gathered = 0;
2493 pid->u.p_pes->pp_last = &pid->u.p_pes->p_data;
2495 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
2497 ParsePES( p_demux, pid, p_data );
2499 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
2501 ParseTableSection( p_demux, pid, p_data );
2505 block_ChainRelease( p_data );
2509 static block_t* ReadTSPacket( demux_t *p_demux )
2511 demux_sys_t *p_sys = p_demux->p_sys;
2515 /* Get a new TS packet */
2516 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2518 if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
2519 msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
2521 msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
2525 if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
2527 block_Release( p_pkt );
2531 /* Skip header (BluRay streams).
2532 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2533 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2535 p_pkt->p_buffer += p_sys->i_packet_header_size;
2536 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2538 /* Check sync byte and re-sync if needed */
2539 if( p_pkt->p_buffer[0] != 0x47 )
2541 msg_Warn( p_demux, "lost synchro" );
2542 block_Release( p_pkt );
2545 const uint8_t *p_peek;
2547 unsigned i_skip = 0;
2549 i_peek = stream_Peek( p_sys->stream, &p_peek,
2550 p_sys->i_packet_size * 10 );
2551 if( i_peek < 0 || (unsigned)i_peek < p_sys->i_packet_size + 1 )
2553 msg_Dbg( p_demux, "eof ?" );
2557 while( i_skip < i_peek - p_sys->i_packet_size )
2559 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2560 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2566 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2567 stream_Read( p_sys->stream, NULL, i_skip );
2569 if( i_skip < i_peek - p_sys->i_packet_size )
2574 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2576 msg_Dbg( p_demux, "eof ?" );
2583 static int64_t TimeStampWrapAround( ts_pmt_t *p_pmt, int64_t i_time )
2585 int64_t i_adjust = 0;
2586 if( p_pmt && p_pmt->pcr.i_first > 0x0FFFFFFFF && i_time < 0x0FFFFFFFF )
2587 i_adjust = 0x1FFFFFFFF;
2589 return i_time + i_adjust;
2592 static mtime_t GetPCR( block_t *p_pkt )
2594 const uint8_t *p = p_pkt->p_buffer;
2598 if( ( p[3]&0x20 ) && /* adaptation */
2602 /* PCR is 33 bits */
2603 i_pcr = ( (mtime_t)p[6] << 25 ) |
2604 ( (mtime_t)p[7] << 17 ) |
2605 ( (mtime_t)p[8] << 9 ) |
2606 ( (mtime_t)p[9] << 1 ) |
2607 ( (mtime_t)p[10] >> 7 );
2612 static void UpdateScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool b_scrambled )
2614 if( !SCRAMBLED(*p_pid) == !b_scrambled )
2617 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2618 p_pid->i_pid, SCRAMBLED(*p_pid), b_scrambled );
2620 p_pid->i_flags |= (b_scrambled) ? FLAG_SCRAMBLED : FLAGS_NONE;
2622 if( p_pid->type == TYPE_PES && p_pid->u.p_pes->es.id )
2624 for( int i = 0; i < p_pid->u.p_pes->extra_es.i_size; i++ )
2626 if( p_pid->u.p_pes->extra_es.p_elems[i]->id )
2627 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2628 p_pid->u.p_pes->extra_es.p_elems[i]->id, b_scrambled );
2630 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2631 p_pid->u.p_pes->es.id, b_scrambled );
2635 static inline void FlushESBuffer( ts_pes_t *p_pes )
2639 p_pes->i_data_gathered = p_pes->i_data_size = 0;
2640 block_ChainRelease( p_pes->p_data );
2641 p_pes->p_data = NULL;
2642 p_pes->pp_last = &p_pes->p_data;
2646 static void ReadyQueuesPostSeek( demux_t *p_demux )
2648 demux_sys_t *p_sys = p_demux->p_sys;
2650 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
2651 for( int i=0; i< p_pat->programs.i_size; i++ )
2653 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2654 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2656 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
2658 if( pid->type != TYPE_PES )
2661 if( pid->u.p_pes->es.id )
2663 block_t *p_block = block_Alloc(1);
2666 p_block->i_buffer = 0;
2667 p_block->i_flags = BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED;
2668 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2674 if( pid->u.p_pes->p_prepcr_outqueue )
2676 block_ChainRelease( pid->u.p_pes->p_prepcr_outqueue );
2677 pid->u.p_pes->p_prepcr_outqueue = NULL;
2680 FlushESBuffer( pid->u.p_pes );
2682 p_pmt->pcr.i_current = -1;
2686 static int SeekToTime( demux_t *p_demux, ts_pmt_t *p_pmt, int64_t i_scaledtime )
2688 demux_sys_t *p_sys = p_demux->p_sys;
2690 /* Deal with common but worst binary search case */
2691 if( p_pmt->pcr.i_first == i_scaledtime && p_sys->b_canseek )
2692 return stream_Seek( p_sys->stream, 0 );
2694 if( !p_sys->b_canfastseek )
2695 return VLC_EGENERIC;
2697 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2699 /* Find the time position by using binary search algorithm. */
2700 int64_t i_head_pos = 0;
2701 int64_t i_tail_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2702 if( i_head_pos >= i_tail_pos )
2703 return VLC_EGENERIC;
2705 bool b_found = false;
2706 while( (i_head_pos + p_sys->i_packet_size) <= i_tail_pos && !b_found )
2708 /* Round i_pos to a multiple of p_sys->i_packet_size */
2709 int64_t i_splitpos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2710 int64_t i_div = i_splitpos % p_sys->i_packet_size;
2711 i_splitpos -= i_div;
2713 if ( stream_Seek( p_sys->stream, i_splitpos ) != VLC_SUCCESS )
2716 int64_t i_pos = i_splitpos;
2717 while( i_pos > -1 && i_pos < i_tail_pos )
2720 block_t *p_pkt = ReadTSPacket( p_demux );
2723 i_head_pos = i_tail_pos;
2727 i_pos = stream_Tell( p_sys->stream );
2729 int i_pid = PIDGet( p_pkt );
2730 if( i_pid != 0x1FFF && PID(p_sys, i_pid)->type == TYPE_PES &&
2731 PID(p_sys, i_pid)->p_parent->u.p_pmt == p_pmt &&
2732 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
2733 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
2736 unsigned i_skip = 4;
2737 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
2739 if( p_pkt->i_buffer >= 4 + 2 + 5 )
2741 i_pcr = GetPCR( p_pkt );
2742 i_skip += 1 + p_pkt->p_buffer[4];
2749 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2750 p_pkt->i_buffer - i_skip, &i_skip, &i_dts, &i_pts ) )
2757 block_Release( p_pkt );
2761 int64_t i_diff = i_scaledtime - TimeStampWrapAround( p_pmt, i_pcr );
2763 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2764 else if( i_diff < TO_SCALE(VLC_TS_0 + CLOCK_FREQ / 2) ) // 500ms
2772 if ( !b_found && i_pos > i_tail_pos - p_sys->i_packet_size )
2773 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2778 msg_Dbg( p_demux, "Seek():cannot find a time position." );
2779 stream_Seek( p_sys->stream, i_initial_pos );
2780 return VLC_EGENERIC;
2785 static ts_pid_t *PID( demux_sys_t *p_sys, uint16_t i_pid )
2790 return &p_sys->pids.pat;
2792 return &p_sys->pids.dummy;
2794 if( p_sys->pids.i_last_pid == i_pid )
2795 return p_sys->pids.p_last;
2799 for( int i=0; i < p_sys->pids.i_all; i++ )
2801 if( p_sys->pids.pp_all[i]->i_pid == i_pid )
2803 p_sys->pids.p_last = p_sys->pids.pp_all[i];
2804 p_sys->pids.i_last_pid = i_pid;
2805 return p_sys->pids.p_last;
2809 if( p_sys->pids.i_all >= p_sys->pids.i_all_alloc )
2811 ts_pid_t **p_realloc = realloc( p_sys->pids.pp_all,
2812 (p_sys->pids.i_all_alloc + PID_ALLOC_CHUNK) * sizeof(ts_pid_t *) );
2815 p_sys->pids.pp_all = p_realloc;
2816 p_sys->pids.i_all_alloc += PID_ALLOC_CHUNK;
2819 ts_pid_t *p_pid = calloc( 1, sizeof(*p_pid) );
2826 p_pid->i_pid = i_pid;
2827 p_sys->pids.pp_all[p_sys->pids.i_all++] = p_pid;
2829 p_sys->pids.p_last = p_pid;
2830 p_sys->pids.i_last_pid = i_pid;
2835 static ts_pmt_t * GetProgramByID( demux_sys_t *p_sys, int i_program )
2837 if(unlikely(PID(p_sys, 0)->type != TYPE_PAT))
2840 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
2841 for( int i = 0; i < p_pat->programs.i_size; i++ )
2843 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
2844 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2845 if( p_pmt->i_number == i_program )
2851 #define PROBE_CHUNK_COUNT 250
2853 static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_pcr, bool *pb_found )
2855 demux_sys_t *p_sys = p_demux->p_sys;
2857 block_t *p_pkt = NULL;
2863 if( i_count++ > PROBE_CHUNK_COUNT || !( p_pkt = ReadTSPacket( p_demux ) ) )
2868 const int i_pid = PIDGet( p_pkt );
2869 ts_pid_t *p_pid = PID(p_sys, i_pid);
2871 p_pid->i_flags |= FLAG_SEEN;
2873 if( i_pid != 0x1FFF && (p_pkt->p_buffer[1] & 0x80) == 0 ) /* not corrupt */
2875 bool b_pcrresult = true;
2876 bool b_adaptfield = p_pkt->p_buffer[3] & 0x20;
2878 if( b_adaptfield && p_pkt->i_buffer >= 4 + 2 + 5 )
2879 *pi_pcr = GetPCR( p_pkt );
2881 if( *pi_pcr == -1 &&
2882 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* payload start */
2883 (p_pkt->p_buffer[3] & 0xD0) == 0x10 && /* Has payload but is not encrypted */
2884 p_pid->type == TYPE_PES &&
2885 p_pid->u.p_pes->es.fmt.i_cat != UNKNOWN_ES
2888 b_pcrresult = false;
2891 unsigned i_skip = 4;
2892 if ( b_adaptfield ) // adaptation field
2893 i_skip += 1 + p_pkt->p_buffer[4];
2895 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2896 p_pkt->i_buffer - i_skip,
2897 &i_skip, &i_dts, &i_pts ) )
2901 else if( i_pts != -1 )
2908 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
2909 for( int i=0; i<p_pat->programs.i_size; i++ )
2911 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2912 if( ( p_pmt->i_pid_pcr == p_pid->i_pid ||
2913 ( p_pmt->i_pid_pcr == 0x1FFF && p_pid->p_parent == p_pat->programs.p_elems[i] ) ) )
2917 p_pmt->i_last_dts = *pi_pcr;
2919 /* Start, only keep first */
2920 else if( b_pcrresult && p_pmt->pcr.i_first == -1 )
2922 p_pmt->pcr.i_first = *pi_pcr;
2924 else if( p_pmt->pcr.i_first_dts < VLC_TS_0 )
2926 p_pmt->pcr.i_first_dts = FROM_SCALE(*pi_pcr);
2929 if( i_program == 0 || i_program == p_pmt->i_number )
2936 block_Release( p_pkt );
2942 static int ProbeStart( demux_t *p_demux, int i_program )
2944 demux_sys_t *p_sys = p_demux->p_sys;
2945 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2946 int64_t i_stream_size = stream_Size( p_sys->stream );
2948 int i_probe_count = 0;
2951 bool b_found = false;
2955 i_pos = p_sys->i_packet_size * i_probe_count;
2956 i_pos = __MIN( i_pos, i_stream_size );
2958 if( stream_Seek( p_sys->stream, i_pos ) )
2959 return VLC_EGENERIC;
2961 ProbeChunk( p_demux, i_program, false, &i_pcr, &b_found );
2963 /* Go ahead one more chunk if end of file contained only stuffing packets */
2964 i_probe_count += PROBE_CHUNK_COUNT;
2965 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (2 * PROBE_CHUNK_COUNT) );
2967 stream_Seek( p_sys->stream, i_initial_pos );
2969 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2972 static int ProbeEnd( demux_t *p_demux, int i_program )
2974 demux_sys_t *p_sys = p_demux->p_sys;
2975 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2976 int64_t i_stream_size = stream_Size( p_sys->stream );
2978 int i_probe_count = PROBE_CHUNK_COUNT;
2981 bool b_found = false;
2985 i_pos = i_stream_size - (p_sys->i_packet_size * i_probe_count);
2986 i_pos = __MAX( i_pos, 0 );
2988 if( stream_Seek( p_sys->stream, i_pos ) )
2989 return VLC_EGENERIC;
2991 ProbeChunk( p_demux, i_program, true, &i_pcr, &b_found );
2993 /* Go ahead one more chunk if end of file contained only stuffing packets */
2994 i_probe_count += PROBE_CHUNK_COUNT;
2995 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (6 * PROBE_CHUNK_COUNT) );
2997 stream_Seek( p_sys->stream, i_initial_pos );
2999 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
3002 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_pmt, mtime_t i_pcr )
3004 demux_sys_t *p_sys = p_demux->p_sys;
3006 /* Check if we have enqueued blocks waiting the/before the
3007 PCR barrier, and then adapt pcr so they have valid PCR when dequeuing */
3008 if( p_pmt->pcr.i_current == -1 && p_pmt->pcr.b_fix_done )
3010 mtime_t i_mindts = -1;
3012 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
3013 for( int i=0; i< p_pat->programs.i_size; i++ )
3015 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
3016 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
3018 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[j];
3019 block_t *p_block = p_pid->u.p_pes->p_prepcr_outqueue;
3020 while( p_block && p_block->i_dts == VLC_TS_INVALID )
3021 p_block = p_block->p_next;
3023 if( p_block && ( i_mindts == -1 || p_block->i_dts < i_mindts ) )
3024 i_mindts = p_block->i_dts;
3028 if( i_mindts > VLC_TS_INVALID )
3030 msg_Dbg( p_demux, "Program %d PCR prequeue fixup %"PRId64"->%"PRId64,
3031 p_pmt->i_number, TO_SCALE(i_mindts), i_pcr );
3032 i_pcr = TO_SCALE(i_mindts);
3036 p_pmt->pcr.i_current = i_pcr;
3037 if( p_pmt->pcr.i_first == -1 )
3039 p_pmt->pcr.i_first = i_pcr; // now seen
3042 if ( p_sys->i_pmt_es )
3044 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
3045 p_pmt->i_number, VLC_TS_0 + i_pcr * 100 / 9 );
3049 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
3051 demux_sys_t *p_sys = p_demux->p_sys;
3053 if( p_sys->i_pmt_es <= 0 )
3056 mtime_t i_pcr = GetPCR( p_bk );
3060 pid->probed.i_pcr_count++;
3062 if(unlikely(PID(p_sys, 0)->type != TYPE_PAT))
3065 /* Search program and set the PCR */
3066 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
3067 for( int i = 0; i < p_pat->programs.i_size; i++ )
3069 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
3070 mtime_t i_program_pcr = TimeStampWrapAround( p_pmt, i_pcr );
3072 if( p_pmt->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
3074 if( pid->p_parent == p_pat->programs.p_elems[i] ) /* PCR shall be on pid itself */
3076 /* ? update PCR for the whole group program ? */
3077 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3080 else /* set PCR provided by current pid to program(s) referencing it */
3082 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
3083 if( p_pmt->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
3085 /* We've found a target group for update */
3086 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3093 static int FindPCRCandidate( ts_pmt_t *p_pmt )
3095 ts_pid_t *p_cand = NULL;
3096 int i_previous = p_pmt->i_pid_pcr;
3098 for( int i=0; i<p_pmt->e_streams.i_size; i++ )
3100 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[i];
3102 (!p_cand || p_cand->i_pid != i_previous) )
3104 if( p_pid->probed.i_pcr_count ) /* check PCR frequency first */
3106 if( !p_cand || p_pid->probed.i_pcr_count > p_cand->probed.i_pcr_count )
3113 if( p_pid->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3118 else if ( p_pid->u.p_pes->es.fmt.i_cat == VIDEO_ES ) /* Otherwise prioritize video dts */
3120 if( !p_cand || p_cand->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3127 return p_cand->i_pid;
3132 /* Tries to reselect a new PCR when none has been received */
3133 static void PCRFixHandle( demux_t *p_demux, ts_pmt_t *p_pmt, block_t *p_block )
3135 if ( p_pmt->pcr.b_disable || p_pmt->pcr.b_fix_done )
3139 /* Record the first data packet timestamp in case there wont be any PCR */
3140 else if( !p_pmt->pcr.i_first_dts )
3142 p_pmt->pcr.i_first_dts = p_block->i_dts;
3144 else if( p_block->i_dts - p_pmt->pcr.i_first_dts > CLOCK_FREQ / 2 ) /* "PCR repeat rate shall not exceed 100ms" */
3146 if( p_pmt->pcr.i_current < 0 )
3148 int i_cand = FindPCRCandidate( p_pmt );
3149 p_pmt->i_pid_pcr = i_cand;
3150 msg_Warn( p_demux, "No PCR received for program %d, set up workaround using pid %d",
3151 p_pmt->i_number, i_cand );
3152 UpdatePESFilters( p_demux, p_demux->p_sys->b_es_all );
3153 p_pmt->pcr.b_fix_done = true;
3158 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
3160 const uint8_t *p = p_bk->p_buffer;
3161 const bool b_unit_start = p[1]&0x40;
3162 const bool b_adaptation = p[3]&0x20;
3163 const bool b_payload = p[3]&0x10;
3164 const int i_cc = p[3]&0x0f; /* continuity counter */
3165 bool b_discontinuity = false; /* discontinuity */
3167 /* transport_scrambling_control is ignored */
3172 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
3173 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
3177 /* For now, ignore additional error correction
3178 * TODO: handle Reed-Solomon 204,188 error correction */
3179 p_bk->i_buffer = TS_PACKET_SIZE_188;
3183 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
3185 if( pid->u.p_pes->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
3186 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3189 if( p_demux->p_sys->csa )
3191 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
3192 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
3193 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
3198 /* We don't have any adaptation_field, so payload starts
3199 * immediately after the 4 byte TS header */
3204 /* p[4] is adaptation_field_length minus one */
3208 /* discontinuity indicator found in stream */
3209 b_discontinuity = (p[5]&0x80) ? true : false;
3210 if( b_discontinuity && pid->u.p_pes->p_data )
3212 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
3214 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
3218 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
3223 /* Test continuity counter */
3224 /* continuous when (one of this):
3226 * diff == 0 and payload == 0
3227 * diff == 0 and duplicate packet (playload != 0) <- should we
3228 * test the content ?
3230 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
3231 if( b_payload && i_diff == 1 )
3233 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
3237 if( pid->i_cc == 0xff )
3239 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
3243 else if( i_diff != 0 && !b_discontinuity )
3245 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
3246 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
3249 if( pid->u.p_pes->p_data && pid->u.p_pes->es.fmt.i_cat != VIDEO_ES &&
3250 pid->u.p_pes->es.fmt.i_cat != AUDIO_ES )
3252 /* Small audio/video artifacts are usually better than
3253 * dropping full frames */
3254 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3259 PCRHandle( p_demux, pid, p_bk );
3263 block_Release( p_bk );
3267 /* We have to gather it */
3268 p_bk->p_buffer += i_skip;
3269 p_bk->i_buffer -= i_skip;
3273 if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
3275 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
3276 block_t *p = block_Duplicate( p_bk );
3279 p->i_buffer = i_pointer_field;
3281 block_ChainLastAppend( &pid->u.p_pes->pp_last, p );
3283 p_bk->i_buffer -= 1 + i_pointer_field;
3284 p_bk->p_buffer += 1 + i_pointer_field;
3286 if( pid->u.p_pes->p_data )
3288 ParseData( p_demux, pid );
3292 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3293 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
3295 if( p_bk->i_buffer > 6 )
3297 pid->u.p_pes->i_data_size = GetWBE( &p_bk->p_buffer[4] );
3298 if( pid->u.p_pes->i_data_size > 0 )
3300 pid->u.p_pes->i_data_size += 6;
3304 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
3306 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
3308 pid->u.p_pes->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
3311 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3312 if( pid->u.p_pes->i_data_size > 0 &&
3313 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3315 ParseData( p_demux, pid );
3321 if( pid->u.p_pes->p_data == NULL )
3323 /* msg_Dbg( p_demux, "broken packet" ); */
3324 block_Release( p_bk );
3328 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3329 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3331 if( pid->u.p_pes->i_data_size > 0 &&
3332 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3334 ParseData( p_demux, pid );
3343 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
3345 switch( i_stream_type )
3347 case 0x01: /* MPEG-1 video */
3348 case 0x02: /* MPEG-2 video */
3349 case 0x80: /* MPEG-2 MOTO video */
3350 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
3352 case 0x03: /* MPEG-1 audio */
3353 case 0x04: /* MPEG-2 audio */
3354 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
3356 case 0x11: /* MPEG4 (audio) LATM */
3357 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
3358 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
3359 transport syntax, such as DST, ALS and SLS */
3360 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
3362 case 0x10: /* MPEG4 (video) */
3363 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
3365 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
3366 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
3368 case 0x24: /* HEVC */
3369 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
3371 case 0x42: /* CAVS (Chinese AVS) */
3372 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
3375 case 0x81: /* A52 (audio) */
3376 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
3378 case 0x82: /* SCTE-27 (sub) */
3379 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
3381 case 0x84: /* SDDS (audio) */
3382 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
3384 case 0x85: /* DTS (audio) */
3385 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
3387 case 0x87: /* E-AC3 */
3388 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
3391 case 0x91: /* A52 vls (audio) */
3392 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
3394 case 0x92: /* DVD_SPU vls (sub) */
3395 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
3398 case 0x94: /* SDDS (audio) */
3399 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
3402 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
3403 es_format_Init( fmt, UNKNOWN_ES, 0 );
3406 case 0x06: /* PES_PRIVATE (fixed later) */
3407 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
3408 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
3410 es_format_Init( fmt, UNKNOWN_ES, 0 );
3414 /* PES packets usually contain truncated frames */
3415 fmt->b_packetized = false;
3418 /*****************************************************************************
3419 * MP4 specific functions (IOD parser)
3420 *****************************************************************************/
3421 static unsigned IODDescriptorLength( unsigned *pi_data, uint8_t **pp_data )
3424 unsigned int i_len = 0;
3430 i_len = ( i_len << 7 ) + ( i_b&0x7f );
3432 } while( i_b&0x80 && *pi_data > 0 );
3434 if (i_len > *pi_data)
3440 static unsigned IODGetBytes( unsigned *pi_data, uint8_t **pp_data, size_t bytes )
3443 while( *pi_data > 0 && bytes-- )
3454 static char* IODGetURL( unsigned *pi_data, uint8_t **pp_data )
3456 unsigned len = IODGetBytes( pi_data, pp_data, 1 );
3459 char *url = strndup( (char*)*pp_data, len );
3465 static iod_descriptor_t *IODNew( unsigned i_data, uint8_t *p_data )
3467 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
3469 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
3478 byte1 = IODGetBytes( &i_data, &p_data, 1 );
3479 byte2 = IODGetBytes( &i_data, &p_data, 1 );
3480 byte3 = IODGetBytes( &i_data, &p_data, 1 );
3481 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
3483 i_iod_label = byte1;
3486 else //correct implementation of the IOD_descriptor
3488 i_iod_label = byte2;
3492 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
3494 if( i_iod_tag != 0x02 )
3496 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
3500 IODDescriptorLength( &i_data, &p_data );
3502 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
3503 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
3504 i_od_id |= i_flags >> 6;
3505 ts_debug( "\n* od_id:%d", i_od_id );
3506 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
3507 if ((i_flags >> 5) & 0x01)
3509 p_iod->psz_url = IODGetURL( &i_data, &p_data );
3510 ts_debug( "\n* url string:%s", p_iod->psz_url );
3511 ts_debug( "\n*****************************\n" );
3516 p_iod->psz_url = NULL;
3519 /* Profile Level Indication */
3520 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
3521 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
3522 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
3523 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
3524 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
3526 unsigned i_length = 0;
3527 unsigned i_data_sav = i_data;
3528 uint8_t *p_data_sav = p_data;
3529 for (unsigned i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
3531 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
3533 p_data = p_data_sav + i_length;
3534 i_data = i_data_sav - i_length;
3536 uint8_t i_tag = IODGetBytes( &i_data, &p_data, 1 );
3537 i_length = IODDescriptorLength( &i_data, &p_data );
3539 i_data_sav = i_data;
3540 p_data_sav = p_data;
3544 if ( i_tag != 0x03 )
3546 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
3550 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
3551 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
3552 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
3553 if( b_streamDependenceFlag )
3554 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
3556 if( (i_flags >> 6) & 0x01 )
3557 es_descr->psz_url = IODGetURL( &i_data, &p_data );
3559 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
3560 if( b_OCRStreamFlag )
3561 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
3563 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
3565 ts_debug( "\n* ERR missing DecoderConfigDescr" );
3568 unsigned i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
3569 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
3570 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
3571 i_flags = IODGetBytes( &i_data, &p_data, 1 );
3572 dec_descr->i_streamType = i_flags >> 2;
3574 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
3575 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
3576 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
3578 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
3580 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
3581 if( dec_descr->i_extra > 0 )
3583 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
3584 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
3585 p_data += dec_descr->i_extra;
3586 i_data -= dec_descr->i_extra;
3591 dec_descr->i_extra = 0;
3592 dec_descr->p_extra = NULL;
3595 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
3597 ts_debug( "\n* ERR missing SLConfigDescr" );
3600 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
3601 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
3604 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
3609 es_descr->b_ok = true;
3615 static void IODFree( iod_descriptor_t *p_iod )
3617 if( p_iod->psz_url )
3619 free( p_iod->psz_url );
3624 for( int i = 0; i < 255; i++ )
3626 #define es_descr p_iod->es_descr[i]
3629 if( es_descr.psz_url )
3630 free( es_descr.psz_url );
3632 free( es_descr.dec_descr.p_extra );
3639 /****************************************************************************
3640 ****************************************************************************
3641 ** libdvbpsi callbacks
3642 ****************************************************************************
3643 ****************************************************************************/
3644 static bool ProgramIsSelected( demux_sys_t *p_sys, uint16_t i_pgrm )
3646 for(int i=0; i<p_sys->programs.i_size; i++)
3647 if( p_sys->programs.p_elems[i] == i_pgrm )
3653 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
3655 demux_sys_t *p_sys = p_demux->p_sys;
3657 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
3660 msg_Warn( p_demux, "Switching to non DVB mode" );
3662 /* This doesn't look like a DVB stream so don't try
3663 * parsing the SDT/EDT/TDT */
3665 PIDRelease( p_demux, PID(p_sys, 0x11) );
3666 PIDRelease( p_demux, PID(p_sys, 0x12) );
3667 PIDRelease( p_demux, PID(p_sys, 0x14) );
3668 p_sys->b_dvb_meta = false;
3671 #include "dvb-text.h"
3673 static char *EITConvertToUTF8( demux_t *p_demux,
3674 const unsigned char *psz_instring,
3678 demux_sys_t *p_sys = p_demux->p_sys;
3680 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3682 if ( !p_sys->arib.p_instance )
3683 p_sys->arib.p_instance = arib_instance_new( p_demux );
3684 if ( !p_sys->arib.p_instance )
3686 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3690 char *psz_outstring = NULL;
3693 i_out = i_length * 4;
3694 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3695 if( !psz_outstring )
3698 arib_initialize_decoder( p_decoder );
3699 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3700 psz_outstring, i_out );
3701 arib_finalize_decoder( p_decoder );
3703 return psz_outstring;
3708 /* Deal with no longer broken providers (no switch byte
3709 but sending ISO_8859-1 instead of ISO_6937) without
3710 removing them from the broken providers table
3711 (keep the entry for correctly handling recorded TS).
3713 b_broken = b_broken && i_length && *psz_instring > 0x20;
3716 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3717 return vlc_from_EIT( psz_instring, i_length );
3720 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3722 demux_sys_t *p_sys = p_demux->p_sys;
3723 ts_pid_t *sdt = PID(p_sys, 0x11);
3724 dvbpsi_sdt_service_t *p_srv;
3726 msg_Dbg( p_demux, "SDTCallBack called" );
3728 if( p_sys->es_creation != CREATE_ES ||
3729 !p_sdt->b_current_next ||
3730 p_sdt->i_version == sdt->u.p_psi->i_version )
3732 dvbpsi_sdt_delete( p_sdt );
3736 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3739 p_sdt->i_version, p_sdt->b_current_next,
3740 p_sdt->i_network_id );
3742 p_sys->b_broken_charset = false;
3744 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3747 dvbpsi_descriptor_t *p_dr;
3749 const char *psz_type = NULL;
3750 const char *psz_status = NULL;
3752 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3753 "running=%d free_ca=%d",
3754 p_srv->i_service_id, p_srv->b_eit_schedule,
3755 p_srv->b_eit_present, p_srv->i_running_status,
3758 if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
3760 msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
3761 p_sys->vdr.i_service );
3765 p_meta = vlc_meta_New();
3766 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3768 if( p_dr->i_tag == 0x48 )
3770 static const char *ppsz_type[17] = {
3772 "Digital television service",
3773 "Digital radio sound service",
3775 "NVOD reference service",
3776 "NVOD time-shifted service",
3779 "SECAM coded signal",
3782 "NTSC coded signal",
3783 "Data broadcast service",
3784 "Reserved for Common Interface Usage",
3785 "RCS Map (see EN 301 790 [35])",
3786 "RCS FLS (see EN 301 790 [35])",
3789 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3793 /* Workarounds for broadcasters with broken EPG */
3795 if( p_sdt->i_network_id == 133 )
3796 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3798 /* List of providers using ISO8859-1 */
3799 static const char ppsz_broken_providers[][8] = {
3800 "CSAT", /* CanalSat FR */
3801 "GR1", /* France televisions */
3803 "MR5", /* France 2/M6 HD */
3806 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3808 const size_t i_length = strlen(ppsz_broken_providers[i]);
3809 if( pD->i_service_provider_name_length == i_length &&
3810 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3811 p_sys->b_broken_charset = true;
3814 /* FIXME: Digital+ ES also uses ISO8859-1 */
3816 str1 = EITConvertToUTF8(p_demux,
3817 pD->i_service_provider_name,
3818 pD->i_service_provider_name_length,
3819 p_sys->b_broken_charset );
3820 str2 = EITConvertToUTF8(p_demux,
3822 pD->i_service_name_length,
3823 p_sys->b_broken_charset );
3825 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3826 pD->i_service_type, str1, str2 );
3828 vlc_meta_SetTitle( p_meta, str2 );
3829 vlc_meta_SetPublisher( p_meta, str1 );
3830 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3831 psz_type = ppsz_type[pD->i_service_type];
3837 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3839 static const char *ppsz_status[5] = {
3842 "Starts in a few seconds",
3846 psz_status = ppsz_status[p_srv->i_running_status];
3850 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3852 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3854 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3855 p_srv->i_service_id, p_meta );
3856 vlc_meta_Delete( p_meta );
3859 sdt->u.p_psi->i_version = p_sdt->i_version;
3860 dvbpsi_sdt_delete( p_sdt );
3863 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3864 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3866 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3870 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3871 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3874 /* Count the number of days */
3875 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3876 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3877 for( int i = 70; i < i_year; i++ )
3878 i_day += LEAP(1900+i);
3880 i_day += LEAP(1900+i_year);
3883 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3886 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3888 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3889 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3890 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3892 *p_y = 1900 + yp + c*1;
3893 *p_m = mp - 1 - c*12;
3894 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3896 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3897 static int64_t EITConvertStartTime( uint64_t i_date )
3899 const int i_mjd = i_date >> 24;
3900 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3901 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3902 const int i_second = CVT_FROM_BCD(i_date );
3907 /* if all 40 bits are 1, the start is unknown */
3908 if( i_date == UINT64_C(0xffffffffff) )
3911 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3912 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3914 static int EITConvertDuration( uint32_t i_duration )
3916 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3917 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3918 CVT_FROM_BCD(i_duration );
3922 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3924 demux_sys_t *p_sys = p_demux->p_sys;
3926 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3928 dvbpsi_tot_delete(p_tdt);
3932 static void EITCallBack( demux_t *p_demux,
3933 dvbpsi_eit_t *p_eit, bool b_current_following )
3935 demux_sys_t *p_sys = p_demux->p_sys;
3936 dvbpsi_eit_event_t *p_evt;
3939 msg_Dbg( p_demux, "EITCallBack called" );
3940 if( !p_eit->b_current_next )
3942 dvbpsi_eit_delete( p_eit );
3946 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3947 "ts_id=%d network_id=%d segment_last_section_number=%d "
3950 p_eit->i_version, p_eit->b_current_next,
3951 p_eit->i_ts_id, p_eit->i_network_id,
3952 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3954 p_epg = vlc_epg_New( NULL );
3955 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3957 dvbpsi_descriptor_t *p_dr;
3958 char *psz_name = NULL;
3959 char *psz_text = NULL;
3960 char *psz_extra = strdup("");
3964 int64_t i_tot_time = 0;
3966 i_start = EITConvertStartTime( p_evt->i_start_time );
3967 i_duration = EITConvertDuration( p_evt->i_duration );
3969 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3971 if( p_sys->i_tdt_delta == 0 )
3972 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3974 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3976 tzset(); // JST -> UTC
3977 i_start += timezone; // FIXME: what about DST?
3978 i_tot_time += timezone;
3980 if( p_evt->i_running_status == 0x00 &&
3981 (i_start - 5 < i_tot_time &&
3982 i_tot_time < i_start + i_duration + 5) )
3984 p_evt->i_running_status = 0x04;
3985 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3989 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3990 "running=%d free_ca=%d",
3991 p_evt->i_event_id, (int)i_start, (int)i_duration,
3992 p_evt->i_running_status, p_evt->b_free_ca );
3994 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
4000 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
4002 /* Only take first description, as we don't handle language-info
4004 if( pE && psz_name == NULL )
4006 psz_name = EITConvertToUTF8( p_demux,
4007 pE->i_event_name, pE->i_event_name_length,
4008 p_sys->b_broken_charset );
4010 psz_text = EITConvertToUTF8( p_demux,
4011 pE->i_text, pE->i_text_length,
4012 p_sys->b_broken_charset );
4013 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
4014 pE->i_iso_639_code, psz_name, psz_text );
4021 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
4024 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
4026 pE->i_descriptor_number, pE->i_last_descriptor_number );
4028 if( pE->i_text_length > 0 )
4030 char *psz_text = EITConvertToUTF8( p_demux,
4031 pE->i_text, pE->i_text_length,
4032 p_sys->b_broken_charset );
4035 msg_Dbg( p_demux, " - text='%s'", psz_text );
4037 psz_extra = xrealloc( psz_extra,
4038 strlen(psz_extra) + strlen(psz_text) + 1 );
4039 strcat( psz_extra, psz_text );
4044 for( int i = 0; i < pE->i_entry_count; i++ )
4046 char *psz_dsc = EITConvertToUTF8( p_demux,
4047 pE->i_item_description[i],
4048 pE->i_item_description_length[i],
4049 p_sys->b_broken_charset );
4050 char *psz_itm = EITConvertToUTF8( p_demux,
4051 pE->i_item[i], pE->i_item_length[i],
4052 p_sys->b_broken_charset );
4054 if( psz_dsc && psz_itm )
4056 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
4058 psz_extra = xrealloc( psz_extra,
4059 strlen(psz_extra) + strlen(psz_dsc) +
4060 strlen(psz_itm) + 3 + 1 );
4061 strcat( psz_extra, "(" );
4062 strcat( psz_extra, psz_dsc );
4063 strcat( psz_extra, " " );
4064 strcat( psz_extra, psz_itm );
4065 strcat( psz_extra, ")" );
4077 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
4080 for ( int i = 0; i < pR->i_ratings_number; i++ )
4082 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
4083 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
4085 if ( p_rating->i_rating + 3 > i_min_age )
4086 i_min_age = p_rating->i_rating + 3;
4087 msg_Dbg( p_demux, " - parental control set to %d years",
4096 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
4102 if( i_start > 0 && psz_name && psz_text)
4103 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
4104 *psz_extra ? psz_extra : NULL, i_min_age );
4106 /* Update "now playing" field */
4107 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
4108 vlc_epg_SetCurrent( p_epg, i_start );
4115 if( p_epg->i_event > 0 )
4117 if( b_current_following &&
4118 ( p_sys->programs.i_size == 0 ||
4119 p_sys->programs.p_elems[0] ==
4123 p_sys->i_dvb_length = 0;
4124 p_sys->i_dvb_start = 0;
4126 if( p_epg->p_current )
4128 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
4129 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
4132 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
4136 vlc_epg_Delete( p_epg );
4138 dvbpsi_eit_delete( p_eit );
4140 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4142 EITCallBack( p_demux, p_eit, true );
4144 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4146 EITCallBack( p_demux, p_eit, false );
4149 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
4150 uint16_t i_extension, demux_t *p_demux )
4152 demux_sys_t *p_sys = p_demux->p_sys;
4155 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4156 i_table_id, i_table_id, i_extension, i_extension );
4158 if( PID(p_sys, 0)->u.p_pat->i_version != -1 && i_table_id == 0x42 )
4160 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4161 i_table_id, i_table_id, i_extension, i_extension );
4163 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
4164 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
4166 else if( PID(p_sys, 0x11)->u.p_psi->i_version != -1 &&
4167 ( i_table_id == 0x4e || /* Current/Following */
4168 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
4170 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4171 i_table_id, i_table_id, i_extension, i_extension );
4173 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
4174 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
4175 (dvbpsi_eit_callback)EITCallBackSchedule;
4177 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
4178 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
4180 else if( PID(p_sys, 0x11)->u.p_psi->i_version != -1 &&
4181 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
4183 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4184 i_table_id, i_table_id, i_extension, i_extension );
4186 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
4187 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
4191 /*****************************************************************************
4192 * PMT callback and helpers
4193 *****************************************************************************/
4194 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
4197 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
4198 while( p_dr && ( p_dr->i_tag != i_tag ) )
4199 p_dr = p_dr->p_next;
4202 static bool PMTEsHasRegistration( demux_t *p_demux,
4203 const dvbpsi_pmt_es_t *p_es,
4204 const char *psz_tag )
4206 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
4210 if( p_dr->i_length < 4 )
4212 msg_Warn( p_demux, "invalid Registration Descriptor" );
4216 assert( strlen(psz_tag) == 4 );
4217 return !memcmp( p_dr->p_data, psz_tag, 4 );
4220 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
4221 int i_component_tag )
4223 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4226 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4230 return p_si->i_component_tag == i_component_tag;
4233 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pes_es_t *p_es,
4234 const ts_pmt_t *p_pmt, const dvbpsi_pmt_es_t *p_dvbpsies )
4236 es_format_t *p_fmt = &p_es->fmt;
4238 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
4239 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x1f );
4241 if( p_dr && p_dr->i_length == 2 && p_pmt->iod )
4243 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
4245 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
4247 p_es->p_mpeg4desc = NULL;
4249 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
4251 iod_descriptor_t *iod = p_pmt->iod;
4252 if( iod->es_descr[i].i_es_id == i_es_id )
4254 if ( iod->es_descr[i].b_ok )
4255 p_es->p_mpeg4desc = &iod->es_descr[i];
4257 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
4262 if( !p_es->p_mpeg4desc )
4264 switch( p_dvbpsies->i_type )
4266 /* non fatal, set by packetizer */
4267 case 0x0f: /* ADTS */
4268 case 0x11: /* LOAS */
4269 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4270 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4273 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4274 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4280 const decoder_config_descriptor_t *dcd = &p_es->p_mpeg4desc->dec_descr;
4281 if( dcd->i_streamType == 0x04 ) /* VisualStream */
4283 p_fmt->i_cat = VIDEO_ES;
4284 switch( dcd->i_objectTypeIndication )
4286 case 0x0B: /* mpeg4 sub */
4287 p_fmt->i_cat = SPU_ES;
4288 p_fmt->i_codec = VLC_CODEC_SUBT;
4291 case 0x20: /* mpeg4 */
4292 p_fmt->i_codec = VLC_CODEC_MP4V;
4294 case 0x21: /* h264 */
4295 p_fmt->i_codec = VLC_CODEC_H264;
4302 case 0x65: /* mpeg2 */
4303 p_fmt->i_codec = VLC_CODEC_MPGV;
4305 case 0x6a: /* mpeg1 */
4306 p_fmt->i_codec = VLC_CODEC_MPGV;
4308 case 0x6c: /* mpeg1 */
4309 p_fmt->i_codec = VLC_CODEC_JPEG;
4312 p_fmt->i_cat = UNKNOWN_ES;
4316 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
4318 p_fmt->i_cat = AUDIO_ES;
4319 switch( dcd->i_objectTypeIndication )
4321 case 0x40: /* mpeg4 */
4322 p_fmt->i_codec = VLC_CODEC_MP4A;
4326 case 0x68: /* mpeg2 aac */
4327 p_fmt->i_codec = VLC_CODEC_MP4A;
4329 case 0x69: /* mpeg2 */
4330 p_fmt->i_codec = VLC_CODEC_MPGA;
4332 case 0x6b: /* mpeg1 */
4333 p_fmt->i_codec = VLC_CODEC_MPGA;
4336 p_fmt->i_cat = UNKNOWN_ES;
4342 p_fmt->i_cat = UNKNOWN_ES;
4345 if( p_fmt->i_cat != UNKNOWN_ES )
4347 p_fmt->i_extra = __MIN(dcd->i_extra, INT32_MAX);
4348 if( p_fmt->i_extra > 0 )
4350 p_fmt->p_extra = malloc( p_fmt->i_extra );
4351 if( p_fmt->p_extra )
4352 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
4365 } ts_teletext_page_t;
4367 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pes_t *p_pes,
4368 const dvbpsi_pmt_es_t *p_dvbpsies )
4370 es_format_t *p_fmt = &p_pes->es.fmt;
4372 ts_teletext_page_t p_page[2 * 64 + 20];
4373 unsigned i_page = 0;
4375 /* Gather pages information */
4376 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
4378 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, i_tag_idx == 0 ? 0x46 : 0x56 );
4382 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
4386 for( int i = 0; i < p_sub->i_pages_number; i++ )
4388 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
4390 if( p_src->i_teletext_type >= 0x06 )
4393 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4395 ts_teletext_page_t *p_dst = &p_page[i_page++];
4397 p_dst->i_type = p_src->i_teletext_type;
4398 p_dst->i_magazine = p_src->i_teletext_magazine_number
4399 ? p_src->i_teletext_magazine_number : 8;
4400 p_dst->i_page = p_src->i_teletext_page_number;
4401 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4405 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4408 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4409 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4411 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
4413 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
4416 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4418 ts_teletext_page_t *p_dst = &p_page[i_page++];
4420 switch( p_src->i_subtitling_type )
4423 p_dst->i_type = 0x02;
4426 p_dst->i_type = 0x03;
4429 /* FIXME check if it is the right split */
4430 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
4431 ? (p_src->i_composition_page_id >> 8) : 8;
4432 p_dst->i_page = p_src->i_composition_page_id & 0xff;
4433 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4438 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
4440 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4442 p_fmt->subs.teletext.i_magazine = -1;
4443 p_fmt->subs.teletext.i_page = 0;
4444 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
4446 dvbpsi_descriptor_t *p_dr;
4447 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x46 );
4449 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x56 );
4451 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4453 /* Descriptor pass-through */
4454 p_fmt->p_extra = malloc( p_dr->i_length );
4455 if( p_fmt->p_extra )
4457 p_fmt->i_extra = p_dr->i_length;
4458 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4464 for( unsigned i = 0; i < i_page; i++ )
4466 ts_pes_es_t *p_page_es;
4471 p_page_es = &p_pes->es;
4475 p_page_es = calloc( 1, sizeof(*p_page_es) );
4479 es_format_Copy( &p_page_es->fmt, p_fmt );
4480 free( p_page_es->fmt.psz_language );
4481 free( p_page_es->fmt.psz_description );
4482 p_page_es->fmt.psz_language = NULL;
4483 p_page_es->fmt.psz_description = NULL;
4485 ARRAY_APPEND( p_pes->extra_es, p_page_es );
4489 const ts_teletext_page_t *p = &p_page[i];
4490 p_page_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
4491 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
4492 p_page_es->fmt.psz_language = strndup( p->p_iso639, 3 );
4493 p_page_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
4494 p_page_es->fmt.subs.teletext.i_magazine = p->i_magazine;
4495 p_page_es->fmt.subs.teletext.i_page = p->i_page;
4498 " * ttxt type=%s lan=%s page=%d%02x",
4499 p_page_es->fmt.psz_description,
4500 p_page_es->fmt.psz_language,
4501 p->i_magazine, p->i_page );
4505 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pes_t *p_pes,
4506 const dvbpsi_pmt_es_t *p_dvbpsies )
4508 es_format_t *p_fmt = &p_pes->es.fmt;
4510 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
4512 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4514 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4515 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4517 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
4518 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
4519 ( i_type >= 0x20 && i_type <= 0x24 ) )
4523 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4525 p_fmt->subs.dvb.i_id = -1;
4526 p_fmt->psz_description = strdup( _("DVB subtitles") );
4528 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4530 /* Descriptor pass-through */
4531 p_fmt->p_extra = malloc( p_dr->i_length );
4532 if( p_fmt->p_extra )
4534 p_fmt->i_extra = p_dr->i_length;
4535 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4541 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4543 ts_pes_es_t *p_subs_es;
4548 p_subs_es = &p_pes->es;
4552 p_subs_es = malloc( sizeof(*p_subs_es) );
4556 es_format_Copy( &p_subs_es->fmt, p_fmt );
4557 free( p_subs_es->fmt.psz_language );
4558 free( p_subs_es->fmt.psz_description );
4559 p_subs_es->fmt.psz_language = NULL;
4560 p_subs_es->fmt.psz_description = NULL;
4562 ARRAY_APPEND( p_pes->extra_es, p_subs_es );
4566 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
4567 p_subs_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
4568 switch( p->i_subtitling_type )
4570 case 0x10: /* unspec. */
4571 case 0x11: /* 4:3 */
4572 case 0x12: /* 16:9 */
4573 case 0x13: /* 2.21:1 */
4574 case 0x14: /* HD monitor */
4575 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles") );
4577 case 0x20: /* Hearing impaired unspec. */
4578 case 0x21: /* h.i. 4:3 */
4579 case 0x22: /* h.i. 16:9 */
4580 case 0x23: /* h.i. 2.21:1 */
4581 case 0x24: /* h.i. HD monitor */
4582 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
4589 p_subs_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
4590 ( p->i_ancillary_page_id << 16 );
4595 static int vlc_ceil_log2( const unsigned int val )
4597 int n = 31 - clz(val);
4598 if ((1U << n) != val)
4604 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
4608 /* default mapping */
4609 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
4610 memcpy(h.stream_map, map, sizeof(map));
4614 int stream_count = 0;
4615 int ccc = p[1]; // channel_config_code
4619 mapping = channels > 2;
4622 channels = 2; // dual mono
4624 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
4625 csc = p_csc[channels - 1];
4626 stream_count = channels - csc;
4628 static const uint8_t map[6][7] = {
4637 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4638 } else if (ccc == 0x81) {
4640 goto explicit_config_too_short;
4647 bs_init(&s, &p[4], len - 4);
4650 int bits = vlc_ceil_log2(channels);
4651 if (s.i_left < bits)
4652 goto explicit_config_too_short;
4653 stream_count = bs_read(&s, bits) + 1;
4654 bits = vlc_ceil_log2(stream_count + 1);
4655 if (s.i_left < bits)
4656 goto explicit_config_too_short;
4657 csc = bs_read(&s, bits);
4659 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4660 if (s.i_left < channels * channel_bits)
4661 goto explicit_config_too_short;
4663 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4664 for (int i = 0; i < channels; i++) {
4665 unsigned char m = bs_read(&s, channel_bits);
4668 h.stream_map[i] = m;
4671 } else if (ccc >= 0x80 && ccc <= 0x88) {
4672 channels = ccc - 0x80;
4677 channels = 2; // dual mono
4680 stream_count = channels;
4682 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4686 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4690 opus_prepare_header(channels, 0, &h);
4692 h.input_sample_rate = 48000;
4694 h.nb_streams = channels - csc;
4695 h.channel_mapping = mapping;
4698 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4699 if (p_fmt->p_extra) {
4700 p_fmt->i_cat = AUDIO_ES;
4701 p_fmt->i_codec = VLC_CODEC_OPUS;
4702 p_fmt->audio.i_channels = h.channels;
4703 p_fmt->audio.i_rate = 48000;
4709 explicit_config_too_short:
4710 msg_Err(demux, "Opus descriptor too short");
4713 static void PMTSetupEs0x06( demux_t *p_demux, ts_pes_t *p_pes,
4714 const dvbpsi_pmt_es_t *p_dvbpsies )
4716 es_format_t *p_fmt = &p_pes->es.fmt;
4717 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4718 dvbpsi_descriptor_t *desc;
4720 if( PMTEsHasRegistration( p_demux, p_dvbpsies, "AC-3" ) ||
4721 PMTEsFindDescriptor( p_dvbpsies, 0x6a ) ||
4722 PMTEsFindDescriptor( p_dvbpsies, 0x81 ) )
4724 p_fmt->i_cat = AUDIO_ES;
4725 p_fmt->i_codec = VLC_CODEC_A52;
4727 else if( (desc = PMTEsFindDescriptor( p_dvbpsies, 0x7f ) ) && desc->i_length >= 2 &&
4728 PMTEsHasRegistration(p_demux, p_dvbpsies, "Opus"))
4730 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4732 else if( PMTEsFindDescriptor( p_dvbpsies, 0x7a ) )
4734 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4735 p_fmt->i_cat = AUDIO_ES;
4736 p_fmt->i_codec = VLC_CODEC_EAC3;
4738 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS1" ) ||
4739 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS2" ) ||
4740 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS3" ) ||
4741 PMTEsFindDescriptor( p_dvbpsies, 0x73 ) )
4743 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4744 p_fmt->i_cat = AUDIO_ES;
4745 p_fmt->i_codec = VLC_CODEC_DTS;
4747 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "BSSD" ) && !p_subs_dr )
4749 /* BSSD is AES3 DATA, but could also be subtitles
4750 * we need to check for secondary descriptor then s*/
4751 p_fmt->i_cat = AUDIO_ES;
4752 p_fmt->b_packetized = true;
4753 p_fmt->i_codec = VLC_CODEC_302M;
4755 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "HEVC" ) )
4757 p_fmt->i_cat = VIDEO_ES;
4758 p_fmt->i_codec = VLC_CODEC_HEVC;
4760 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4762 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4763 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xFD );
4764 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4765 if ( p_dr && p_dr->i_length >= 2 )
4767 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4768 PMTEsHasComponentTag( p_dvbpsies, 0x30 ) ||
4769 PMTEsHasComponentTag( p_dvbpsies, 0x31 ) ||
4770 PMTEsHasComponentTag( p_dvbpsies, 0x32 ) ||
4771 PMTEsHasComponentTag( p_dvbpsies, 0x33 ) ||
4772 PMTEsHasComponentTag( p_dvbpsies, 0x34 ) ||
4773 PMTEsHasComponentTag( p_dvbpsies, 0x35 ) ||
4774 PMTEsHasComponentTag( p_dvbpsies, 0x36 ) ||
4775 PMTEsHasComponentTag( p_dvbpsies, 0x37 ) ) )
4777 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_A );
4778 p_fmt->psz_language = strndup ( "jpn", 3 );
4779 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4781 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4782 PMTEsHasComponentTag( p_dvbpsies, 0x87 ) ||
4783 PMTEsHasComponentTag( p_dvbpsies, 0x88 ) ) )
4785 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_C );
4786 p_fmt->psz_language = strndup ( "jpn", 3 );
4787 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4793 /* Subtitle/Teletext/VBI fallbacks */
4794 dvbpsi_subtitling_dr_t *p_sub;
4795 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4797 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4799 if( p_fmt->i_cat != UNKNOWN_ES )
4802 switch( p_sub->p_subtitle[i].i_subtitling_type )
4804 case 0x01: /* EBU Teletext subtitles */
4805 case 0x02: /* Associated EBU Teletext */
4806 case 0x03: /* VBI data */
4807 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4809 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4810 case 0x11: /* ... on 4:3 AR monitor */
4811 case 0x12: /* ... on 16:9 AR monitor */
4812 case 0x13: /* ... on 2.21:1 AR monitor */
4813 case 0x14: /* ... for display on a high definition monitor */
4814 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4815 case 0x21: /* ... on 4:3 AR monitor */
4816 case 0x22: /* ... on 16:9 AR monitor */
4817 case 0x23: /* ... on 2.21:1 AR monitor */
4818 case 0x24: /* ... for display on a high definition monitor */
4819 PMTSetupEsDvbSubtitle( p_demux, p_pes, p_dvbpsies );
4822 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4823 p_sub->p_subtitle[i].i_subtitling_type );
4829 if( p_fmt->i_cat == UNKNOWN_ES &&
4830 ( PMTEsFindDescriptor( p_dvbpsies, 0x45 ) || /* VBI Data descriptor */
4831 PMTEsFindDescriptor( p_dvbpsies, 0x46 ) || /* VBI Teletext descriptor */
4832 PMTEsFindDescriptor( p_dvbpsies, 0x56 ) ) ) /* EBU Teletext descriptor */
4835 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4839 /* FIXME is it useful ? */
4840 if( PMTEsFindDescriptor( p_dvbpsies, 0x52 ) )
4842 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x52 );
4843 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4845 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4849 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pes_es_t *p_es,
4850 const dvbpsi_pmt_es_t *p_dvbpsies )
4852 /* Registration Descriptor */
4853 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "VC-1" ) )
4855 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4859 es_format_t *p_fmt = &p_es->fmt;
4861 /* registration descriptor for VC-1 (SMPTE rp227) */
4862 p_fmt->i_cat = VIDEO_ES;
4863 p_fmt->i_codec = VLC_CODEC_VC1;
4865 /* XXX With Simple and Main profile the SEQUENCE
4866 * header is modified: video width and height are
4867 * inserted just after the start code as 2 int16_t
4868 * The packetizer will take care of that. */
4871 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pes_es_t *p_es,
4872 const dvbpsi_pmt_es_t *p_dvbpsies )
4874 /* Registration Descriptor */
4875 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "drac" ) )
4877 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4881 es_format_t *p_fmt = &p_es->fmt;
4883 /* registration descriptor for Dirac
4884 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4885 p_fmt->i_cat = VIDEO_ES;
4886 p_fmt->i_codec = VLC_CODEC_DIRAC;
4889 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pes_es_t *p_es,
4890 const dvbpsi_pmt_es_t *p_dvbpsies )
4892 /* MSCODEC sent by vlc */
4893 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xa0 );
4894 if( !p_dr || p_dr->i_length < 10 )
4897 "private MSCODEC (vlc) without bih private descriptor" );
4901 es_format_t *p_fmt = &p_es->fmt;
4902 p_fmt->i_cat = VIDEO_ES;
4903 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4904 p_dr->p_data[2], p_dr->p_data[3] );
4905 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4906 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4907 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4909 if( p_fmt->i_extra > 0 )
4911 p_fmt->p_extra = malloc( p_fmt->i_extra );
4912 if( p_fmt->p_extra )
4913 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4914 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4918 /* For such stream we will gather them ourself and don't launch a
4920 * Yes it's ugly but it's the only way to have DIV3 working */
4921 p_fmt->b_packetized = true;
4924 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pes_es_t *p_es, int i_pid )
4926 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4927 * with audio track pid being 0x1100..0x11FF */
4928 if ( p_pmt->i_program_number == 0x1 &&
4929 p_pmt->i_pcr_pid == 0x1000 &&
4930 ( i_pid >> 8 ) == 0x11 )
4932 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4933 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4936 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4939 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pes_es_t *p_es,
4940 const dvbpsi_pmt_es_t *p_dvbpsies )
4942 es_format_t *p_fmt = &p_es->fmt;
4944 /* Blu-Ray mapping */
4945 switch( p_dvbpsies->i_type )
4948 p_fmt->i_cat = AUDIO_ES;
4949 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4952 p_fmt->i_cat = AUDIO_ES;
4953 p_fmt->i_codec = VLC_CODEC_A52;
4956 case 0x85: /* DTS-HD High resolution audio */
4957 case 0x86: /* DTS-HD Master audio */
4958 case 0xA2: /* Secondary DTS audio */
4959 p_fmt->i_cat = AUDIO_ES;
4960 p_fmt->i_codec = VLC_CODEC_DTS;
4963 case 0x83: /* TrueHD AC3 */
4964 p_fmt->i_cat = AUDIO_ES;
4965 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4968 case 0x84: /* E-AC3 */
4969 case 0xA1: /* Secondary E-AC3 */
4970 p_fmt->i_cat = AUDIO_ES;
4971 p_fmt->i_codec = VLC_CODEC_EAC3;
4973 case 0x90: /* Presentation graphics */
4974 p_fmt->i_cat = SPU_ES;
4975 p_fmt->i_codec = VLC_CODEC_BD_PG;
4977 case 0x91: /* Interactive graphics */
4978 case 0x92: /* Subtitle */
4981 p_fmt->i_cat = VIDEO_ES;
4982 p_fmt->i_codec = VLC_CODEC_VC1;
4985 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4986 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4993 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pes_es_t *p_es,
4994 const dvbpsi_pmt_es_t *p_dvbpsies )
5000 vlc_fourcc_t i_codec;
5002 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
5003 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
5004 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
5005 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
5006 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
5007 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
5008 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
5009 { "", UNKNOWN_ES, 0 }
5011 es_format_t *p_fmt = &p_es->fmt;
5013 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
5015 if( PMTEsHasRegistration( p_demux, p_dvbpsies, p_regs[i].psz_tag ) )
5017 p_fmt->i_cat = p_regs[i].i_cat;
5018 p_fmt->i_codec = p_regs[i].i_codec;
5019 if (p_dvbpsies->i_type == 0x87)
5020 p_fmt->i_codec = VLC_CODEC_EAC3;
5027 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
5029 static const char *audio_type[] = {
5031 N_("clean effects"),
5032 N_("hearing impaired"),
5033 N_("visual impaired commentary"),
5036 if (type < 0 || type > 3)
5037 msg_Dbg( p_demux, "unknown audio type: %d", type);
5039 return strdup(audio_type[type]);
5043 static void PMTParseEsIso639( demux_t *p_demux, ts_pes_es_t *p_es,
5044 const dvbpsi_pmt_es_t *p_dvbpsies )
5046 /* get language descriptor */
5047 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x0a );
5052 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
5055 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
5059 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
5060 p_es->fmt.psz_language = malloc( 4 );
5061 if( p_es->fmt.psz_language )
5063 memcpy( p_es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
5064 p_es->fmt.psz_language[3] = 0;
5065 msg_Dbg( p_demux, "found language: %s", p_es->fmt.psz_language);
5067 int type = p_decoded->code[0].i_audio_type;
5068 p_es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
5070 p_es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
5072 p_es->fmt.i_extra_languages = p_decoded->i_code_count-1;
5073 if( p_es->fmt.i_extra_languages > 0 )
5074 p_es->fmt.p_extra_languages =
5075 malloc( sizeof(*p_es->fmt.p_extra_languages) *
5076 p_es->fmt.i_extra_languages );
5077 if( p_es->fmt.p_extra_languages )
5079 for( unsigned i = 0; i < p_es->fmt.i_extra_languages; i++ )
5081 p_es->fmt.p_extra_languages[i].psz_language = malloc(4);
5082 if( p_es->fmt.p_extra_languages[i].psz_language )
5084 memcpy( p_es->fmt.p_extra_languages[i].psz_language,
5085 p_decoded->code[i+1].iso_639_code, 3 );
5086 p_es->fmt.p_extra_languages[i].psz_language[3] = '\0';
5088 int type = p_decoded->code[i].i_audio_type;
5089 p_es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
5093 p_es->fmt.psz_language = malloc( 4 );
5094 if( p_es->fmt.psz_language )
5096 memcpy( p_es->fmt.psz_language,
5097 p_decoded->i_iso_639_code, 3 );
5098 p_es->fmt.psz_language[3] = 0;
5103 static inline void SetExtraESGroupAndID( demux_sys_t *p_sys, es_format_t *p_fmt,
5104 const es_format_t *p_parent_fmt )
5106 if ( p_sys->b_es_id_pid ) /* pid is 13 bits */
5107 p_fmt->i_id = (p_sys->i_next_extraid++ << 13) | p_parent_fmt->i_id;
5108 p_fmt->i_group = p_parent_fmt->i_group;
5111 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid, bool b_create_delayed )
5113 demux_sys_t *p_sys = p_demux->p_sys;
5115 if( b_create_delayed )
5116 p_sys->es_creation = CREATE_ES;
5118 if( pid && p_sys->es_creation == CREATE_ES )
5120 /* FIXME: other owners / shared pid */
5121 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
5122 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
5124 es_format_t *p_fmt = &pid->u.p_pes->extra_es.p_elems[i]->fmt;
5125 SetExtraESGroupAndID( p_sys, p_fmt, &pid->u.p_pes->es.fmt );
5126 pid->u.p_pes->extra_es.p_elems[i]->id = es_out_Add( p_demux->out, p_fmt );
5128 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
5130 /* Update the default program == first created ES group */
5131 if( p_sys->b_default_selection )
5133 p_sys->b_default_selection = false;
5134 assert(p_sys->programs.i_size == 1);
5135 if( p_sys->programs.p_elems[0] != pid->p_parent->u.p_pmt->i_number )
5136 p_sys->programs.p_elems[0] = pid->p_parent->u.p_pmt->i_number;
5137 msg_Dbg( p_demux, "Default program is %d", pid->p_parent->u.p_pmt->i_number );
5141 if( b_create_delayed )
5143 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
5144 for( int i=0; i< p_pat->programs.i_size; i++ )
5146 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
5147 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
5149 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
5150 if( pid->u.p_pes->es.id )
5153 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
5154 for( int k = 0; k < pid->u.p_pes->extra_es.i_size; k++ )
5156 es_format_t *p_fmt = &pid->u.p_pes->extra_es.p_elems[k]->fmt;
5157 SetExtraESGroupAndID( p_sys, p_fmt, &pid->u.p_pes->es.fmt );
5158 pid->u.p_pes->extra_es.p_elems[k]->id = es_out_Add( p_demux->out, p_fmt );
5160 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
5165 UpdatePESFilters( p_demux, p_sys->b_es_all );
5168 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_dvbpsipmt )
5170 demux_t *p_demux = data;
5171 demux_sys_t *p_sys = p_demux->p_sys;
5173 ts_pid_t *pmtpid = NULL;
5174 ts_pmt_t *p_pmt = NULL;
5176 msg_Dbg( p_demux, "PMTCallBack called" );
5178 if (unlikely(PID(p_sys, 0)->type != TYPE_PAT))
5180 assert(PID(p_sys, 0)->type == TYPE_PAT);
5181 dvbpsi_pmt_delete(p_dvbpsipmt);
5184 const ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
5186 /* First find this PMT declared in PAT */
5187 for( int i = 0; !pmtpid && i < p_pat->programs.i_size; i++ )
5189 const int i_pmt_prgnumber = p_pat->programs.p_elems[i]->u.p_pmt->i_number;
5190 if( i_pmt_prgnumber != TS_USER_PMT_NUMBER &&
5191 i_pmt_prgnumber == p_dvbpsipmt->i_program_number )
5193 pmtpid = p_pat->programs.p_elems[i];
5194 assert(pmtpid->type == TYPE_PMT);
5195 p_pmt = pmtpid->u.p_pmt;
5199 if( pmtpid == NULL )
5201 msg_Warn( p_demux, "unreferenced program (broken stream)" );
5202 dvbpsi_pmt_delete(p_dvbpsipmt);
5206 pmtpid->i_flags |= FLAG_SEEN;
5208 if( p_pmt->i_version != -1 &&
5209 ( !p_dvbpsipmt->b_current_next || p_pmt->i_version == p_dvbpsipmt->i_version ) )
5211 dvbpsi_pmt_delete( p_dvbpsipmt );
5215 /* Save old es array */
5216 DECL_ARRAY(ts_pid_t *) old_es_rm;
5217 old_es_rm.i_alloc = p_pmt->e_streams.i_alloc;
5218 old_es_rm.i_size = p_pmt->e_streams.i_size;
5219 old_es_rm.p_elems = p_pmt->e_streams.p_elems;
5220 ARRAY_INIT(p_pmt->e_streams);
5224 IODFree( p_pmt->iod );
5228 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
5229 p_dvbpsipmt->i_program_number, p_dvbpsipmt->i_version, p_dvbpsipmt->i_pcr_pid );
5230 p_pmt->i_pid_pcr = p_dvbpsipmt->i_pcr_pid;
5231 p_pmt->i_version = p_dvbpsipmt->i_version;
5233 ValidateDVBMeta( p_demux, p_pmt->i_pid_pcr );
5235 if( ProgramIsSelected( p_sys, p_pmt->i_number ) )
5236 SetPIDFilter( p_sys, PID(p_sys, p_pmt->i_pid_pcr), true ); /* Set demux filter */
5238 /* Parse PMT descriptors */
5239 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
5240 dvbpsi_descriptor_t *p_dr;
5242 /* First pass for standard detection */
5243 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
5245 int i_arib_flags = 0; /* Descriptors can be repeated */
5246 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5252 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
5253 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
5257 i_arib_flags |= 1 << 1;
5260 i_arib_flags |= 1 << 2;
5266 if ( i_arib_flags == 0x07 ) //0b111
5267 p_sys->arib.e_mode = ARIBMODE_ENABLED;
5270 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5272 /* special descriptors handling */
5275 case 0x1d: /* We have found an IOD descriptor */
5276 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
5277 p_pmt->iod = IODNew( p_dr->i_length, p_dr->p_data );
5281 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5282 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5285 case 0x5: /* Registration Descriptor */
5286 if( p_dr->i_length != 4 )
5288 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
5292 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
5293 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
5294 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
5299 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
5303 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
5306 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
5307 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
5308 /* TODO: apply visibility flags */
5312 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
5316 dvbpsi_pmt_es_t *p_dvbpsies;
5317 for( p_dvbpsies = p_dvbpsipmt->p_first_es; p_dvbpsies != NULL; p_dvbpsies = p_dvbpsies->p_next )
5319 bool b_reusing_pid = false;
5322 ts_pid_t *pespid = PID(p_sys, p_dvbpsies->i_pid);
5323 if ( pespid->type == TYPE_PES && pespid->p_parent->u.p_pmt->i_number != p_pmt->i_number )
5325 msg_Warn( p_demux, " * PMT wants to get a share or pid %d (unsupported)", pespid->i_pid );
5329 /* Find out if the PID was already declared */
5330 for( int i = 0; i < old_es_rm.i_size; i++ )
5332 if( old_es_rm.p_elems[i]->i_pid == p_dvbpsies->i_pid )
5334 b_reusing_pid = true;
5338 ValidateDVBMeta( p_demux, p_dvbpsies->i_pid );
5340 char const * psz_typedesc = "";
5341 switch(p_dvbpsies->i_type)
5344 psz_typedesc = "ISO/IEC Reserved";
5347 psz_typedesc = "ISO/IEC 11172 Video";
5350 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
5353 psz_typedesc = "ISO/IEC 11172 Audio";
5356 psz_typedesc = "ISO/IEC 13818-3 Audio";
5359 psz_typedesc = "ISO/IEC 13818-1 private_sections";
5362 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
5365 psz_typedesc = "ISO/IEC 13522 MHEG";
5368 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
5371 psz_typedesc = "ITU-T Rec. H.222.1";
5374 psz_typedesc = "ISO/IEC 13818-6 type A";
5377 psz_typedesc = "ISO/IEC 13818-6 type B";
5380 psz_typedesc = "ISO/IEC 13818-6 type C";
5383 psz_typedesc = "ISO/IEC 13818-6 type D";
5386 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
5389 if (p_dvbpsies->i_type >= 0x0F && p_dvbpsies->i_type <=0x7F)
5390 psz_typedesc = "ISO/IEC 13818-1 Reserved";
5392 psz_typedesc = "User Private";
5395 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
5396 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5398 for( p_dr = p_dvbpsies->p_first_descriptor; p_dr != NULL;
5399 p_dr = p_dr->p_next )
5401 msg_Dbg( p_demux, " - descriptor tag 0x%x",
5405 if ( !PIDSetup( p_demux, TYPE_PES, pespid, pmtpid ) )
5407 msg_Warn( p_demux, " * pid=%d type=0x%x %s (skipped)",
5408 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5415 p_pes = ts_pes_New( p_demux );
5421 p_pes = pespid->u.p_pes;
5425 ARRAY_APPEND( p_pmt->e_streams, pespid );
5427 PIDFillFormat( &p_pes->es.fmt, p_dvbpsies->i_type );
5429 pespid->i_flags |= SEEN(PID(p_sys, p_dvbpsies->i_pid));
5431 bool b_registration_applied = false;
5432 if ( p_dvbpsies->i_type >= 0x80 ) /* non standard, extensions */
5434 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
5436 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, &p_pes->es, p_dvbpsies ) ))
5437 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
5438 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5442 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, &p_pes->es, p_dvbpsies ) ))
5443 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
5444 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5448 if ( !b_registration_applied )
5450 switch( p_dvbpsies->i_type )
5453 /* Handle PES private data */
5454 PMTSetupEs0x06( p_demux, p_pes, p_dvbpsies );
5456 /* All other private or reserved types */
5461 PMTSetupEsISO14496( p_demux, &p_pes->es, p_pmt, p_dvbpsies );
5465 PMTSetupEs0x83( p_dvbpsipmt, &p_pes->es, p_dvbpsies->i_pid );
5468 PMTSetupEs0xA0( p_demux, &p_pes->es, p_dvbpsies );
5471 PMTSetupEs0xD1( p_demux, &p_pes->es, p_dvbpsies );
5474 PMTSetupEs0xEA( p_demux, &p_pes->es, p_dvbpsies );
5480 if( p_pes->es.fmt.i_cat == AUDIO_ES ||
5481 ( p_pes->es.fmt.i_cat == SPU_ES &&
5482 p_pes->es.fmt.i_codec != VLC_CODEC_DVBS &&
5483 p_pes->es.fmt.i_codec != VLC_CODEC_TELETEXT ) )
5485 PMTParseEsIso639( p_demux, &p_pes->es, p_dvbpsies );
5488 switch( p_pes->es.fmt.i_codec )
5490 case VLC_CODEC_SCTE_27:
5491 p_pes->data_type = TS_ES_DATA_TABLE_SECTION;
5494 //pid->es->data_type = TS_ES_DATA_PES;
5498 /* Set Groups / ID */
5499 p_pes->es.fmt.i_group = p_dvbpsipmt->i_program_number;
5500 if( p_sys->b_es_id_pid )
5501 p_pes->es.fmt.i_id = p_dvbpsies->i_pid;
5503 if( p_pes->es.fmt.i_cat == UNKNOWN_ES )
5505 msg_Dbg( p_demux, " => pid %d content is *unknown*",
5506 p_dvbpsies->i_pid );
5507 p_pes->es.fmt.psz_description = strdup( psz_typedesc );
5511 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
5512 p_dvbpsies->i_pid, (char*)&p_pes->es.fmt.i_codec );
5514 /* Check if we can avoid restarting the ES */
5517 /* p_pes points to a tmp pes */
5518 if( pespid->u.p_pes->es.fmt.i_codec != p_pes->es.fmt.i_codec ||
5519 pespid->u.p_pes->es.fmt.i_extra != p_pes->es.fmt.i_extra ||
5520 pespid->u.p_pes->es.fmt.i_extra != 0 ||
5521 pespid->u.p_pes->extra_es.i_size != p_pes->extra_es.i_size ||
5522 !( ( !pespid->u.p_pes->es.fmt.psz_language &&
5523 !p_pes->es.fmt.psz_language ) ||
5524 ( pespid->u.p_pes->es.fmt.psz_language &&
5525 p_pes->es.fmt.psz_language &&
5526 !strcmp( pespid->u.p_pes->es.fmt.psz_language,
5527 p_pes->es.fmt.psz_language ) ) ) )
5529 /* Differs, swap then */
5530 ts_pes_t *e = pespid->u.p_pes;
5531 pespid->u.p_pes = p_pes;
5534 /* p_pes still tmp, but now contains old config */
5535 pespid->u.p_pes->es.id = p_pes->es.id;
5536 if( pespid->u.p_pes->es.id )
5538 p_pes->es.id = NULL;
5539 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5540 pespid->u.p_pes->es.id, pespid->u.p_pes->es.fmt );
5543 for( int i=0; i<pespid->u.p_pes->extra_es.i_size &&
5544 i<p_pes->extra_es.i_size; i++ )
5546 pespid->u.p_pes->extra_es.p_elems[i]->id = p_pes->extra_es.p_elems[i]->id;
5547 if( pespid->u.p_pes->extra_es.p_elems[i]->id )
5549 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5550 pespid->u.p_pes->extra_es.p_elems[i]->id,
5551 pespid->u.p_pes->extra_es.p_elems[i]->fmt );
5552 p_pes->extra_es.p_elems[i]->id = NULL;
5557 ts_pes_Del( p_demux, p_pes ); // delete temp
5561 AddAndCreateES( p_demux, pespid, false );
5565 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x09 );
5566 if( p_dr && p_dr->i_length >= 2 )
5568 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5569 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5573 /* Set CAM descrambling */
5574 if( !ProgramIsSelected( p_sys, p_pmt->i_number ) )
5576 dvbpsi_pmt_delete( p_dvbpsipmt );
5578 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
5579 p_dvbpsipmt ) != VLC_SUCCESS )
5581 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED && !p_sys->arib.b25stream )
5583 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
5584 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
5585 if (!p_sys->arib.b25stream)
5586 dvbpsi_pmt_delete( p_dvbpsipmt );
5587 } else dvbpsi_pmt_delete( p_dvbpsipmt );
5590 /* Decref or clean now unused es */
5591 for( int i = 0; i < old_es_rm.i_size; i++ )
5592 PIDRelease( p_demux, old_es_rm.p_elems[i] );
5593 ARRAY_RESET( old_es_rm );
5595 UpdatePESFilters( p_demux, p_sys->b_es_all );
5597 if( !p_sys->b_trust_pcr )
5599 int i_cand = FindPCRCandidate( p_pmt );
5600 p_pmt->i_pid_pcr = i_cand;
5601 p_pmt->pcr.b_disable = true;
5602 msg_Warn( p_demux, "PCR not trusted for program %d, set up workaround using pid %d",
5603 p_pmt->i_number, i_cand );
5606 /* Probe Boundaries */
5607 if( p_sys->b_canfastseek && p_pmt->i_last_dts == -1 )
5609 p_pmt->i_last_dts = 0;
5610 ProbeStart( p_demux, p_pmt->i_number );
5611 ProbeEnd( p_demux, p_pmt->i_number );
5615 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
5617 /* Some Dreambox streams have all PMT set to same pid */
5618 int i_prev_pid = -1;
5619 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
5621 p_program = p_program->p_next )
5623 if( p_program->i_pid == i_prev_pid )
5625 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
5626 return VLC_EGENERIC;
5628 i_prev_pid = p_program->i_pid;
5633 static void PATCallBack( void *data, dvbpsi_pat_t *p_dvbpsipat )
5635 demux_t *p_demux = data;
5636 demux_sys_t *p_sys = p_demux->p_sys;
5637 dvbpsi_pat_program_t *p_program;
5638 ts_pid_t *patpid = PID(p_sys, 0);
5639 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
5641 patpid->i_flags |= FLAG_SEEN;
5643 msg_Dbg( p_demux, "PATCallBack called" );
5645 if(unlikely( PID(p_sys, 0)->type != TYPE_PAT ))
5647 msg_Warn( p_demux, "PATCallBack called on invalid pid" );
5651 if( ( p_pat->i_version != -1 &&
5652 ( !p_dvbpsipat->b_current_next ||
5653 p_dvbpsipat->i_version == p_pat->i_version ) ) ||
5654 ( p_pat->i_ts_id != -1 && p_dvbpsipat->i_ts_id != p_pat->i_ts_id ) ||
5655 p_sys->b_user_pmt || PATCheck( p_demux, p_dvbpsipat ) )
5657 dvbpsi_pat_delete( p_dvbpsipat );
5661 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
5662 p_dvbpsipat->i_ts_id, p_dvbpsipat->i_version, p_dvbpsipat->b_current_next );
5664 /* Save old programs array */
5665 DECL_ARRAY(ts_pid_t *) old_pmt_rm;
5666 old_pmt_rm.i_alloc = p_pat->programs.i_alloc;
5667 old_pmt_rm.i_size = p_pat->programs.i_size;
5668 old_pmt_rm.p_elems = p_pat->programs.p_elems;
5669 ARRAY_INIT(p_pat->programs);
5671 /* now create programs */
5672 for( p_program = p_dvbpsipat->p_first_program; p_program != NULL;
5673 p_program = p_program->p_next )
5675 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
5677 if( p_program->i_number == 0 )
5680 ts_pid_t *pmtpid = PID(p_sys, p_program->i_pid);
5682 ValidateDVBMeta( p_demux, p_program->i_pid );
5684 /* create or temporary incref pid */
5685 if( !PIDSetup( p_demux, TYPE_PMT, pmtpid, patpid ) )
5687 msg_Warn( p_demux, " * number=%d pid=%d (ignored)", p_program->i_number,
5692 pmtpid->u.p_pmt->i_number = p_program->i_number;
5694 if( !dvbpsi_pmt_attach( pmtpid->u.p_pmt->handle, p_program->i_number, PMTCallBack, p_demux ) )
5695 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5696 p_program->i_number );
5698 ARRAY_APPEND( p_pat->programs, pmtpid );
5700 /* Now select PID at access level */
5701 if( p_sys->programs.i_size == 0 ||
5702 ProgramIsSelected( p_sys, p_program->i_number ) )
5704 if( p_sys->programs.i_size == 0 )
5706 msg_Dbg( p_demux, "temporary receiving program %d", p_program->i_number );
5707 p_sys->b_default_selection = true;
5708 ARRAY_APPEND( p_sys->programs, p_program->i_number );
5711 if( SetPIDFilter( p_sys, pmtpid, true ) )
5712 p_sys->b_access_control = false;
5713 else if ( p_sys->es_creation == DELAY_ES )
5714 p_sys->es_creation = CREATE_ES;
5717 p_pat->i_version = p_dvbpsipat->i_version;
5718 p_pat->i_ts_id = p_dvbpsipat->i_ts_id;
5720 for(int i=0; i<old_pmt_rm.i_size; i++)
5722 /* decref current or release now unreferenced */
5723 PIDRelease( p_demux, old_pmt_rm.p_elems[i] );
5725 ARRAY_RESET(old_pmt_rm);
5727 dvbpsi_pat_delete( p_dvbpsipat );
5730 static inline bool handle_Init( demux_t *p_demux, dvbpsi_t **handle )
5732 *handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
5735 (*handle)->p_sys = (void *) p_demux;
5739 static ts_pat_t *ts_pat_New( demux_t *p_demux )
5741 ts_pat_t *pat = malloc( sizeof( ts_pat_t ) );
5745 if( !handle_Init( p_demux, &pat->handle ) )
5751 pat->i_version = -1;
5753 ARRAY_INIT( pat->programs );
5758 static void ts_pat_Del( demux_t *p_demux, ts_pat_t *pat )
5760 if( dvbpsi_decoder_present( pat->handle ) )
5761 dvbpsi_pat_detach( pat->handle );
5762 dvbpsi_delete( pat->handle );
5763 for( int i=0; i<pat->programs.i_size; i++ )
5764 PIDRelease( p_demux, pat->programs.p_elems[i] );
5765 ARRAY_RESET( pat->programs );
5769 static ts_pmt_t *ts_pmt_New( demux_t *p_demux )
5771 ts_pmt_t *pmt = malloc( sizeof( ts_pmt_t ) );
5775 if( !handle_Init( p_demux, &pmt->handle ) )
5781 ARRAY_INIT( pmt->e_streams );
5783 pmt->i_version = -1;
5785 pmt->i_pid_pcr = 0x1FFF;
5788 pmt->i_last_dts = -1;
5790 pmt->pcr.i_current = -1;
5791 pmt->pcr.i_first = -1;
5792 pmt->pcr.b_disable = false;
5793 pmt->pcr.i_first_dts = VLC_TS_INVALID;
5794 pmt->pcr.i_pcroffset = -1;
5796 pmt->pcr.b_fix_done = false;
5801 static void ts_pmt_Del( demux_t *p_demux, ts_pmt_t *pmt )
5803 if( dvbpsi_decoder_present( pmt->handle ) )
5804 dvbpsi_pmt_detach( pmt->handle );
5805 dvbpsi_delete( pmt->handle );
5806 for( int i=0; i<pmt->e_streams.i_size; i++ )
5807 PIDRelease( p_demux, pmt->e_streams.p_elems[i] );
5808 ARRAY_RESET( pmt->e_streams );
5810 IODFree( pmt->iod );
5811 if( pmt->i_number > -1 )
5812 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, pmt->i_number );
5816 static ts_pes_t *ts_pes_New( demux_t *p_demux )
5818 VLC_UNUSED(p_demux);
5819 ts_pes_t *pes = malloc( sizeof( ts_pes_t ) );
5824 pes->es.p_mpeg4desc = NULL;
5825 es_format_Init( &pes->es.fmt, UNKNOWN_ES, 0 );
5826 ARRAY_INIT( pes->extra_es );
5827 pes->data_type = TS_ES_DATA_PES;
5828 pes->i_data_size = 0;
5829 pes->i_data_gathered = 0;
5831 pes->pp_last = &pes->p_data;
5832 pes->p_prepcr_outqueue = NULL;
5837 static void ts_pes_Del( demux_t *p_demux, ts_pes_t *pes )
5841 es_out_Del( p_demux->out, pes->es.id );
5842 p_demux->p_sys->i_pmt_es--;
5846 block_ChainRelease( pes->p_data );
5848 if( pes->p_prepcr_outqueue )
5849 block_ChainRelease( pes->p_prepcr_outqueue );
5851 es_format_Clean( &pes->es.fmt );
5852 free( pes->es.p_mpeg4desc );
5854 for( int i = 0; i < pes->extra_es.i_size; i++ )
5856 if( pes->extra_es.p_elems[i]->id )
5858 es_out_Del( p_demux->out, pes->extra_es.p_elems[i]->id );
5859 p_demux->p_sys->i_pmt_es--;
5861 es_format_Clean( &pes->extra_es.p_elems[i]->fmt );
5862 free( pes->extra_es.p_elems[i] );
5864 ARRAY_RESET( pes->extra_es );
5869 static ts_psi_t *ts_psi_New( demux_t *p_demux )
5871 ts_psi_t *psi = malloc( sizeof( ts_psi_t ) );
5875 if( !handle_Init( p_demux, &psi->handle ) )
5881 psi->i_version = -1;
5886 static void ts_psi_Del( demux_t *p_demux, ts_psi_t *psi )
5888 VLC_UNUSED(p_demux);
5889 if( dvbpsi_decoder_present( psi->handle ) )
5890 dvbpsi_DetachDemux( psi->handle );
5891 dvbpsi_delete( psi->handle );