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 */
276 es_mpeg4_descriptor_t *p_mpeg4desc;
282 TS_ES_DATA_TABLE_SECTION
288 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
289 DECL_ARRAY( ts_pes_es_t * ) extra_es;
291 ts_es_data_type_t data_type;
297 block_t * p_prepcr_outqueue;
304 /* for special PAT/SDT case */
305 dvbpsi_t *handle; /* PAT/SDT/EIT */
312 TS_PMT_REGISTRATION_NONE = 0,
313 TS_PMT_REGISTRATION_HDMV
314 } ts_pmt_registration_type_t;
320 ts_es_data_type_t data_type;
326 es_mpeg4_descriptor_t *p_mpeg4desc;
328 block_t * p_prepcr_outqueue;
350 #define SEEN(x) ((x).i_flags & FLAG_SEEN)
351 #define SCRAMBLED(x) ((x).i_flags & FLAG_SCRAMBLED)
358 uint8_t i_cc; /* countinuity counter */
361 /* PSI owner (ie PMT -> PAT, ES -> PMT */
376 vlc_fourcc_t i_fourcc;
388 #define MIN_ES_PID 4 /* Should be 32.. broken muxers */
389 #define MAX_ES_PID 8190
390 #define MIN_PAT_INTERVAL CLOCK_FREQ // DVB is 500ms
392 #define FROM_SCALE(x) (VLC_TS_0 + ((x) * 100 / 9))
393 #define TO_SCALE(x) (((x) - VLC_TS_0) * 9 / 100)
400 vlc_mutex_t csa_lock;
402 /* TS packet size (188, 192, 204) */
405 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
406 int i_packet_header_size;
408 /* how many TS packet we read at once */
411 bool b_force_seek_per_percent;
417 arib_instance_t *p_instance;
427 bool b_es_all; /* If we need to return all es/programs */
431 NO_ES, /* for preparse */
435 #define PREPARSING p_sys->es_creation == NO_ES
439 uint16_t i_next_extraid;
448 bool b_access_control;
455 int64_t i_dvb_length;
456 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
458 /* Selected programs */
459 DECL_ARRAY( int ) programs; /* List of selected/access-filtered programs */
460 bool b_default_selection; /* True if set by default to first pmt seen (to get data from filtered access) */
464 mtime_t i_first_dts; /* first dts encountered for the stream */
465 int i_timesourcepid; /* which pid we saved the dts from */
466 bool b_pat_deadline; /* set if we haven't seen PAT within MIN_PAT_INTERVAL */
475 static int Demux ( demux_t *p_demux );
476 static int Control( demux_t *p_demux, int i_query, va_list args );
478 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
480 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent );
481 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid );
483 static void PATCallBack( void*, dvbpsi_pat_t * );
484 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
485 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
486 uint16_t i_extension, demux_t * );
488 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
491 static ts_pat_t *ts_pat_New( demux_t * );
492 static void ts_pat_Del( demux_t *, ts_pat_t * );
493 static ts_pmt_t *ts_pmt_New( demux_t * );
494 static void ts_pmt_Del( demux_t *, ts_pmt_t * );
495 static ts_pes_t *ts_pes_New( demux_t * );
496 static void ts_pes_Del( demux_t *, ts_pes_t * );
497 static ts_psi_t *ts_psi_New( demux_t * );
498 static void ts_psi_Del( demux_t *, ts_psi_t * );
501 static ts_pmt_t * GetProgramByID( demux_sys_t *, int i_program );
502 static bool ProgramIsSelected( demux_sys_t *, uint16_t i_pgrm );
503 static void UpdatePESFilters( demux_t *p_demux, bool b_all );
504 static inline void FlushESBuffer( ts_pes_t *p_pes );
505 static void UpdateScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool );
506 static inline int PIDGet( block_t *p )
508 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
511 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
512 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid, bool );
513 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_prg, mtime_t i_pcr );
515 static block_t* ReadTSPacket( demux_t *p_demux );
516 static int ProbeStart( demux_t *p_demux, int i_program );
517 static int ProbeEnd( demux_t *p_demux, int i_program );
518 static int SeekToTime( demux_t *p_demux, ts_pmt_t *, int64_t time );
519 static void ReadyQueuesPostSeek( demux_t *p_demux );
520 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
521 static void PCRFixHandle( demux_t *, ts_pmt_t *, block_t * );
522 static int64_t TimeStampWrapAround( ts_pmt_t *, int64_t );
524 static void IODFree( iod_descriptor_t * );
526 #define TS_USER_PMT_NUMBER (0)
527 static int UserPmt( demux_t *p_demux, const char * );
529 static int SetPIDFilter( demux_sys_t *, ts_pid_t *, bool b_selected );
531 #define TS_PACKET_SIZE_188 188
532 #define TS_PACKET_SIZE_192 192
533 #define TS_PACKET_SIZE_204 204
534 #define TS_PACKET_SIZE_MAX 204
535 #define TS_HEADER_SIZE 4
537 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size, int i_offset )
539 const uint8_t *p_peek;
541 if( stream_Peek( p_demux->s,
542 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
545 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
547 if( p_peek[i_offset + i_sync] != 0x47 )
550 /* Check next 3 sync bytes */
551 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
552 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
554 msg_Err( p_demux, "cannot peek" );
557 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
558 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
559 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
561 return TS_PACKET_SIZE_188;
563 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
564 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
565 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
569 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
571 return TS_PACKET_SIZE_192;
573 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
574 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
575 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
577 return TS_PACKET_SIZE_204;
581 if( p_demux->b_force )
583 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
584 return TS_PACKET_SIZE_188;
586 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
590 #define TOPFIELD_HEADER_SIZE 3712
592 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, int *pi_header_size, vdr_info_t *p_vdr )
594 const uint8_t *p_peek;
596 int i_packet_size = -1;
598 if( stream_Peek( p_demux->s,
599 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
602 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
603 p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
604 stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
605 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
607 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
608 if( i_packet_size != -1 )
610 msg_Dbg( p_demux, "this is a topfield file" );
612 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
613 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
614 * but after the filename the offsets seem to be incorrect. - DJ */
615 int i_duration, i_name;
616 char *psz_name = xmalloc(25);
617 char *psz_event_name;
618 char *psz_event_text = xmalloc(130);
619 char *psz_ext_text = xmalloc(1025);
621 // 2 bytes version Uimsbf (4,5)
622 // 2 bytes reserved (6,7)
623 // 2 bytes duration in minutes Uimsbf (8,9(
624 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
625 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
626 // 2 bytes service number in channel list (10, 11)
627 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
628 // 4 bytes of reserved + tuner info (14,15,16,17)
629 // 2 bytes of Service ID Bslbf (18,19)
630 // 2 bytes of PMT PID Uimsbf (20,21)
631 // 2 bytes of PCR PID Uimsbf (22,23)
632 // 2 bytes of Video PID Uimsbf (24,25)
633 // 2 bytes of Audio PID Uimsbf (26,27)
634 // 24 bytes filename Bslbf
635 memcpy( psz_name, &p_peek[28], 24 );
637 msg_Dbg( p_demux, "recordingname=%s", psz_name );
638 // 1 byte of sat index Uimsbf (52)
639 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
640 // 4 bytes of freq. Uimsbf (56,57,58,59)
641 // 2 bytes of symbol rate Uimsbf (60,61)
642 // 2 bytes of TS stream ID Uimsbf (62,63)
645 // 2 bytes duration Uimsbf (70,71)
646 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
647 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
648 // 4 bytes EventID Uimsbf (72-75)
649 // 8 bytes of Start and End time info (76-83)
650 // 1 byte reserved (84)
651 // 1 byte event name length Uimsbf (89)
652 i_name = (int)(p_peek[89]&~0x81);
653 msg_Dbg( p_demux, "event name length = %d", i_name);
654 psz_event_name = xmalloc( i_name+1 );
655 // 1 byte parental rating (90)
656 // 129 bytes of event text
657 memcpy( psz_event_name, &p_peek[91], i_name );
658 psz_event_name[i_name] = '\0';
659 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
660 psz_event_text[129-i_name] = '\0';
661 msg_Dbg( p_demux, "event name=%s", psz_event_name );
662 msg_Dbg( p_demux, "event text=%s", psz_event_text );
663 // 12 bytes reserved (220)
665 // 2 bytes Event Text Length Uimsbf
666 // 4 bytes EventID Uimsbf
667 // FIXME We just have 613 bytes. not enough for this entire text
668 // 1024 bytes Extended Event Text Bslbf
669 memcpy( psz_ext_text, p_peek+372, 1024 );
670 psz_ext_text[1024] = '\0';
671 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
672 // 52 bytes reserved Bslbf
674 p_vdr->i_service = GetWBE(&p_peek[18]);
676 return i_packet_size;
677 //return TS_PACKET_SIZE_188;
681 return DetectPacketSize( p_demux, pi_header_size, 0 );
684 static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
686 return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
687 (mtime_t)(p_data[1] << 22)|
688 ((mtime_t)(p_data[2]&0xfe) << 14)|
689 (mtime_t)(p_data[3] << 7)|
690 (mtime_t)(p_data[4] >> 1);
693 static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, size_t i_data, bool b_adaptfield )
695 demux_sys_t *p_sys = p_demux->p_sys;
696 const uint8_t *p_pes = p_pesstart;
697 pid->probed.i_type = -1;
704 uint8_t len = *p_pes;
709 p_pes++; i_data--;/* stuffing */
715 if( len >= 7 && (p_pes[1] & 0x10) )
716 pid->probed.i_pcr_count++;
725 if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
728 size_t i_pesextoffset = 8;
730 if( p_pes[7] & 0x80 ) // PTS
733 if ( i_data < i_pesextoffset )
735 i_dts = ExtractPESTimestamp( &p_pes[9] );
737 if( p_pes[7] & 0x40 ) // DTS
740 if ( i_data < i_pesextoffset )
742 i_dts = ExtractPESTimestamp( &p_pes[14] );
744 if( p_pes[7] & 0x20 ) // ESCR
746 if( p_pes[7] & 0x10 ) // ESrate
748 if( p_pes[7] & 0x08 ) // DSM
750 if( p_pes[7] & 0x04 ) // CopyInfo
752 if( p_pes[7] & 0x02 ) // PESCRC
755 if ( i_data < i_pesextoffset )
758 /* HeaderdataLength */
759 const size_t i_payloadoffset = 8 + 1 + p_pes[8];
762 if ( i_data < i_pesextoffset || i_data < i_payloadoffset )
765 i_data -= 8 + 1 + p_pes[8];
767 if( p_pes[7] & 0x01 ) // PESExt
769 size_t i_extension2_offset = 1;
770 if ( p_pes[i_pesextoffset] & 0x80 ) // private data
771 i_extension2_offset += 16;
772 if ( p_pes[i_pesextoffset] & 0x40 ) // pack
773 i_extension2_offset += 1;
774 if ( p_pes[i_pesextoffset] & 0x20 ) // seq
775 i_extension2_offset += 2;
776 if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
777 i_extension2_offset += 2;
778 if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
780 uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
781 i_extension2_offset += i_len;
783 if( i_data < i_extension2_offset )
786 i_data -= i_extension2_offset;
788 /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
793 const uint8_t *p_data = &p_pes[i_payloadoffset];
795 if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
797 if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
799 pid->probed.i_type = 0x06;
800 pid->probed.i_fourcc = VLC_CODEC_DTS;
802 else if( !memcmp( p_data, "\x0B\x77", 2 ) )
804 pid->probed.i_type = 0x06;
805 pid->probed.i_fourcc = VLC_CODEC_EAC3;
807 else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
809 switch(p_data[1] & 18)
811 /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
812 11 - MPEG Version 1 (ISO/IEC 11172-3) */
814 pid->probed.i_type = 0x04;
817 pid->probed.i_type = 0x03;
822 switch(p_data[1] & 6)
828 pid->probed.i_type = 0x04;
829 pid->probed.i_fourcc = VLC_CODEC_MPGA;
832 pid->probed.i_type = 0x04;
833 pid->probed.i_fourcc = VLC_CODEC_MP2;
836 pid->probed.i_type = 0x04;
837 pid->probed.i_fourcc = VLC_CODEC_MP3;
844 else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
846 if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
848 pid->probed.i_type = 0x1b;
849 pid->probed.i_fourcc = VLC_CODEC_H264;
851 else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
853 pid->probed.i_type = 0x02;
854 pid->probed.i_fourcc = VLC_CODEC_MPGV;
858 /* Track timestamps and flag missing PAT */
859 if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
861 p_sys->patfix.i_first_dts = i_dts;
862 p_sys->patfix.i_timesourcepid = pid->i_pid;
864 else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
865 !p_sys->patfix.b_pat_deadline )
867 if( i_dts - p_sys->patfix.i_first_dts > TO_SCALE(MIN_PAT_INTERVAL) )
868 p_sys->patfix.b_pat_deadline = true;
873 static void BuildPATCallback( void *p_opaque, block_t *p_block )
875 ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
876 dvbpsi_packet_push( pat_pid->u.p_pat->handle, p_block->p_buffer );
879 static void BuildPMTCallback( void *p_opaque, block_t *p_block )
881 ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
882 assert(program_pid->type == TYPE_PMT);
885 dvbpsi_packet_push( program_pid->u.p_pmt->handle,
887 p_block = p_block->p_next;
891 static void MissingPATPMTFixup( demux_t *p_demux )
893 demux_sys_t *p_sys = p_demux->p_sys;
894 int i_program_number = 1234;
895 int i_program_pid = 1337;
896 int i_pcr_pid = 0x1FFF;
899 if( SEEN(p_sys->pid[i_program_pid]) )
901 /* Find a free one */
902 for( i_program_pid = MIN_ES_PID;
903 i_program_pid <= MAX_ES_PID && SEEN(p_sys->pid[i_program_pid]);
907 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
909 if( !SEEN(p_sys->pid[i]) ||
910 p_sys->pid[i].probed.i_type == -1 )
913 if( i_pcr_pid == 0x1FFF && ( p_sys->pid[i].probed.i_type == 0x03 ||
914 p_sys->pid[i].probed.i_pcr_count ) )
915 i_pcr_pid = p_sys->pid[i].i_pid;
923 ts_stream_t patstream =
926 .i_continuity_counter = 0x10,
927 .b_discontinuity = false
930 ts_stream_t pmtprogramstream =
932 .i_pid = i_program_pid,
933 .i_continuity_counter = 0x0,
934 .b_discontinuity = false
937 BuildPAT( p_sys->pid[0].u.p_pat->handle,
938 &p_sys->pid[0], BuildPATCallback,
941 1, &pmtprogramstream, &i_program_number );
943 if( p_sys->pid[i_program_pid].type != TYPE_PMT )
945 msg_Err( p_demux, "PAT creation failed" );
954 es_format_t esfmt = {0};
955 struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
956 pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
957 if( esstreams && mapped )
960 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
962 if( !SEEN(p_sys->pid[i]) ||
963 p_sys->pid[i].probed.i_type == -1 )
966 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
967 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
968 esstreams[j].ts.i_pid = p_sys->pid[i].i_pid;
969 mapped[j].pes = &esstreams[j].pes;
970 mapped[j].ts = &esstreams[j].ts;
971 mapped[j].fmt = &esfmt;
975 BuildPMT( p_sys->pid[0].u.p_pat->handle, VLC_OBJECT(p_demux),
976 &p_sys->pid[i_program_pid], BuildPMTCallback,
980 1, &pmtprogramstream, &i_program_number,
987 /*****************************************************************************
989 *****************************************************************************/
990 static int Open( vlc_object_t *p_this )
992 demux_t *p_demux = (demux_t*)p_this;
995 int i_packet_size, i_packet_header_size = 0;
998 vdr_info_t vdr = {0};
1000 /* Search first sync byte */
1001 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
1002 if( i_packet_size < 0 )
1003 return VLC_EGENERIC;
1005 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
1008 memset( p_sys, 0, sizeof( demux_sys_t ) );
1009 vlc_mutex_init( &p_sys->csa_lock );
1011 p_demux->pf_demux = Demux;
1012 p_demux->pf_control = Control;
1014 /* Init p_sys field */
1015 p_sys->b_dvb_meta = true;
1016 p_sys->b_access_control = true;
1017 p_sys->b_end_preparse = false;
1018 ARRAY_INIT( p_sys->programs );
1019 p_sys->b_default_selection = false;
1020 p_sys->i_tdt_delta = 0;
1021 p_sys->i_dvb_start = 0;
1022 p_sys->i_dvb_length = 0;
1026 p_sys->arib.b25stream = NULL;
1027 p_sys->stream = p_demux->s;
1029 p_sys->b_broken_charset = false;
1031 for( int i = 0; i < 8192; i++ )
1033 ts_pid_t *pid = &p_sys->pid[i];
1035 pid->i_flags = FLAGS_NONE;
1036 pid->probed.i_fourcc = 0;
1037 pid->probed.i_type = 0;
1039 /* PID 8191 is padding */
1040 p_sys->pid[8191].i_flags = FLAG_SEEN;
1041 p_sys->i_packet_size = i_packet_size;
1042 p_sys->i_packet_header_size = i_packet_header_size;
1043 p_sys->i_ts_read = 50;
1045 p_sys->b_start_record = false;
1047 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
1049 if( !dvbpsi_AttachDemux( (table)->u.p_psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
1051 msg_Warn( obj, "Can't dvbpsi_AttachDemux on pid %d", (table)->i_pid );\
1055 /* Init PAT handler */
1056 patpid = &p_sys->pid[0];
1057 if ( !PIDSetup( p_demux, TYPE_PAT, patpid, NULL ) )
1059 vlc_mutex_destroy( &p_sys->csa_lock );
1063 if( !dvbpsi_pat_attach( patpid->u.p_pat->handle, PATCallBack, p_demux ) )
1065 PIDRelease( p_demux, patpid );
1066 vlc_mutex_destroy( &p_sys->csa_lock );
1068 return VLC_EGENERIC;
1071 if( p_sys->b_dvb_meta )
1073 if( !PIDSetup( p_demux, TYPE_SDT, &p_sys->pid[0x11], NULL ) ||
1074 !PIDSetup( p_demux, TYPE_EIT, &p_sys->pid[0x12], NULL ) ||
1075 !PIDSetup( p_demux, TYPE_TDT, &p_sys->pid[0x14], NULL ) )
1077 PIDRelease( p_demux, &p_sys->pid[0x11] );
1078 PIDRelease( p_demux, &p_sys->pid[0x12] );
1079 PIDRelease( p_demux, &p_sys->pid[0x14] );
1080 p_sys->b_dvb_meta = false;
1084 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x11], p_demux);
1085 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x12], p_demux);
1086 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x14], p_demux);
1087 if( p_sys->b_access_control &&
1088 ( SetPIDFilter( p_sys, &p_sys->pid[0x11], true ) ||
1089 SetPIDFilter( p_sys, &p_sys->pid[0x14], true ) ||
1090 SetPIDFilter( p_sys, &p_sys->pid[0x12], true ) )
1092 p_sys->b_access_control = false;
1096 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
1098 p_sys->i_pmt_es = 0;
1099 p_sys->b_es_all = false;
1102 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
1103 p_sys->i_next_extraid = 1;
1105 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
1107 /* We handle description of an extra PMT */
1108 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
1109 p_sys->b_user_pmt = false;
1110 if( psz_string && *psz_string )
1111 UserPmt( p_demux, psz_string );
1114 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
1115 if( psz_string && *psz_string )
1120 p_sys->csa = csa_New();
1122 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
1123 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
1124 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
1126 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
1128 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1131 else if ( i_res == VLC_SUCCESS )
1133 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1137 csa_Delete( p_sys->csa );
1143 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
1144 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1146 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
1147 if( i_pkt < 4 || i_pkt > 188 )
1149 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
1150 msg_Warn( p_demux, "using default packet size of 188 bytes" );
1151 p_sys->i_csa_pkt_size = 188;
1154 p_sys->i_csa_pkt_size = i_pkt;
1155 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
1161 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
1163 p_sys->b_canseek = false;
1164 p_sys->b_canfastseek = false;
1165 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
1167 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
1169 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
1170 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1173 if( p_sys->b_canseek )
1175 p_sys->es_creation = NO_ES;
1176 while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
1177 if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
1179 p_sys->es_creation = DELAY_ES;
1182 p_sys->es_creation = ( p_sys->b_access_control ? CREATE_ES : DELAY_ES );
1187 /*****************************************************************************
1189 *****************************************************************************/
1190 static void Close( vlc_object_t *p_this )
1192 demux_t *p_demux = (demux_t*)p_this;
1193 demux_sys_t *p_sys = p_demux->p_sys;
1195 PIDRelease( p_demux, &p_sys->pid[0] );
1197 if( p_sys->b_dvb_meta )
1199 PIDRelease( p_demux, &p_sys->pid[0x11] );
1200 PIDRelease( p_demux, &p_sys->pid[0x12] );
1201 PIDRelease( p_demux, &p_sys->pid[0x14] );
1205 for( int i = 0; i < 8192; i++ )
1207 ts_pid_t *pid = &p_sys->pid[i];
1208 if( pid->type != TYPE_FREE )
1209 msg_Err( p_demux, "PID %d type %d not freed", pid->i_pid, pid->type );
1213 vlc_mutex_lock( &p_sys->csa_lock );
1216 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
1217 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1218 csa_Delete( p_sys->csa );
1220 vlc_mutex_unlock( &p_sys->csa_lock );
1222 ARRAY_RESET( p_sys->programs );
1225 if ( p_sys->arib.p_instance )
1226 arib_instance_destroy( p_sys->arib.p_instance );
1229 if ( p_sys->arib.b25stream )
1231 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
1232 stream_Delete( p_sys->arib.b25stream );
1235 vlc_mutex_destroy( &p_sys->csa_lock );
1239 /*****************************************************************************
1240 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
1241 *****************************************************************************/
1242 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
1243 vlc_value_t oldval, vlc_value_t newval,
1246 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1247 demux_t *p_demux = (demux_t*)p_this;
1248 demux_sys_t *p_sys = p_demux->p_sys;
1249 int i_tmp = (intptr_t)p_data;
1251 vlc_mutex_lock( &p_sys->csa_lock );
1253 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
1255 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
1257 vlc_mutex_unlock( &p_sys->csa_lock );
1261 /*****************************************************************************
1263 *****************************************************************************/
1264 static int Demux( demux_t *p_demux )
1266 demux_sys_t *p_sys = p_demux->p_sys;
1267 bool b_wait_es = p_sys->i_pmt_es <= 0;
1269 /* If we had no PAT within MIN_PAT_INTERVAL, create PAT/PMT from probed streams */
1270 if( p_sys->i_pmt_es == 0 && !SEEN(p_sys->pid[0]) && p_sys->patfix.b_pat_deadline )
1271 MissingPATPMTFixup( p_demux );
1273 /* We read at most 100 TS packet or until a frame is completed */
1274 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1276 bool b_frame = false;
1278 if( !(p_pkt = ReadTSPacket( p_demux )) )
1280 return VLC_DEMUXER_EOF;
1283 if( p_sys->b_start_record )
1285 /* Enable recording once synchronized */
1286 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
1287 p_sys->b_start_record = false;
1290 /* Parse the TS packet */
1291 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1293 if( SCRAMBLED(*p_pid) != !!(p_pkt->p_buffer[3] & 0x80) )
1294 UpdateScrambledState( p_demux, p_pid, p_pkt->p_buffer[3] & 0x80 );
1298 if( p_pid->type == TYPE_FREE )
1299 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1300 p_pid->i_flags |= FLAG_SEEN;
1303 if ( SCRAMBLED(*p_pid) && !p_demux->p_sys->csa )
1305 block_Release( p_pkt );
1309 /* Probe streams to build PAT/PMT after MIN_PAT_INTERVAL in case we don't see any PAT */
1310 if( !SEEN(p_sys->pid[0]) &&
1311 (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
1312 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1313 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
1315 ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
1316 p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
1319 switch( p_pid->type )
1322 dvbpsi_packet_push( p_pid->u.p_pat->handle, p_pkt->p_buffer );
1323 block_Release( p_pkt );
1327 dvbpsi_packet_push( p_pid->u.p_pmt->handle, p_pkt->p_buffer );
1328 block_Release( p_pkt );
1332 p_sys->b_end_preparse = true;
1334 if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
1336 msg_Dbg( p_demux, "Creating delayed ES" );
1337 AddAndCreateES( p_demux, p_pid, true );
1340 if( !p_sys->b_access_control && !(p_pid->i_flags & FLAG_FILTERED) )
1342 /* That packet is for an unselected ES, don't waste time/memory gathering its data */
1343 block_Release( p_pkt );
1347 b_frame = GatherData( p_demux, p_pid, p_pkt );
1353 if( p_sys->b_dvb_meta )
1354 dvbpsi_packet_push( p_pid->u.p_psi->handle, p_pkt->p_buffer );
1355 block_Release( p_pkt );
1359 /* We have to handle PCR if present */
1360 PCRHandle( p_demux, p_pid, p_pkt );
1361 block_Release( p_pkt );
1365 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1369 demux_UpdateTitleFromStream( p_demux );
1370 return VLC_DEMUXER_SUCCESS;
1373 /*****************************************************************************
1375 *****************************************************************************/
1376 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1378 demux_sys_t *p_sys = p_demux->p_sys;
1384 if( p_sys->i_dvb_length > 0 )
1386 const int64_t t = mdate() + p_sys->i_tdt_delta;
1388 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1391 *pi_length = p_sys->i_dvb_length;
1393 *pi_time = t - p_sys->i_dvb_start;
1397 return VLC_EGENERIC;
1400 static void UpdatePESFilters( demux_t *p_demux, bool b_all )
1402 demux_sys_t *p_sys = p_demux->p_sys;
1403 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1404 for( int i=0; i< p_pat->programs.i_size; i++ )
1406 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1407 bool b_program_selected;
1408 if( (p_sys->b_default_selection && !p_sys->b_access_control) || b_all )
1409 b_program_selected = true;
1411 b_program_selected = ProgramIsSelected( p_sys, p_pmt->i_number );
1413 SetPIDFilter( p_sys, p_pat->programs.p_elems[i], b_program_selected );
1415 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
1417 ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
1418 bool b_stream_selected = b_program_selected;
1419 if( b_program_selected && !b_all && espid->u.p_pes->es.id )
1420 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1421 espid->u.p_pes->es.id, &b_stream_selected );
1423 if( !p_sys->b_es_all && espid->u.p_pes->es.fmt.i_cat == UNKNOWN_ES )
1424 b_stream_selected = false;
1426 if( b_stream_selected )
1427 msg_Dbg( p_demux, "enabling pid %d from program %d", espid->i_pid, p_pmt->i_number );
1429 SetPIDFilter( p_sys, espid, b_stream_selected );
1430 if( !b_stream_selected )
1431 FlushESBuffer( espid->u.p_pes );
1434 /* Select pcr last in case it is handled by unselected ES */
1435 if( p_pmt->i_pid_pcr > 0 )
1437 SetPIDFilter( p_sys, &p_sys->pid[p_pmt->i_pid_pcr], b_program_selected );
1438 if( b_program_selected )
1439 msg_Dbg( p_demux, "enabling pcr pid %d from program %d", p_pmt->i_pid_pcr, p_pmt->i_number );
1444 static int Control( demux_t *p_demux, int i_query, va_list args )
1446 demux_sys_t *p_sys = p_demux->p_sys;
1448 bool b_bool, *pb_bool;
1453 int i_first_program = ( p_sys->programs.i_size ) ? p_sys->programs.p_elems[0] : 0;
1455 if( PREPARSING || !i_first_program || p_sys->b_default_selection )
1457 if( likely(p_sys->pid[0].type == TYPE_PAT) )
1459 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1460 /* Set default program for preparse time (no program has been selected) */
1461 for( int i = 0; i < p_pat->programs.i_size; i++ )
1463 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
1464 p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1465 if( ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) && p_pmt->i_last_dts > 0 )
1467 i_first_program = p_pmt->i_number;
1476 case DEMUX_GET_POSITION:
1477 pf = (double*) va_arg( args, double* );
1479 /* Access control test is because EPG for recordings is not relevant */
1480 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1482 int64_t i_time, i_length;
1483 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1485 *pf = (double)i_time/(double)i_length;
1490 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1491 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1492 p_pmt->pcr.i_current > -1 )
1494 double i_length = TimeStampWrapAround( p_pmt,
1495 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1496 i_length += p_pmt->pcr.i_pcroffset;
1497 double i_pos = TimeStampWrapAround( p_pmt,
1498 p_pmt->pcr.i_current ) - p_pmt->pcr.i_first;
1499 *pf = i_pos / i_length;
1503 if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1505 int64_t offset = stream_Tell( p_sys->stream );
1506 *pf = (double)offset / (double)i64;
1511 case DEMUX_SET_POSITION:
1512 f = (double) va_arg( args, double );
1514 if(!p_sys->b_canseek)
1517 if( p_sys->b_dvb_meta && p_sys->b_access_control &&
1518 !p_sys->b_force_seek_per_percent &&
1519 (p_pmt = GetProgramByID( p_sys, i_first_program )) )
1521 int64_t i_time, i_length;
1522 if( !DVBEventInformation( p_demux, &i_time, &i_length ) &&
1523 i_length > 0 && !SeekToTime( p_demux, p_pmt, TO_SCALE(i_length) * f ) )
1525 ReadyQueuesPostSeek( p_demux );
1526 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1527 TO_SCALE(i_length) * f );
1532 if( !p_sys->b_force_seek_per_percent &&
1533 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1534 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1535 p_pmt->pcr.i_current > -1 )
1537 double i_length = TimeStampWrapAround( p_pmt,
1538 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1539 if( !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + i_length * f ) )
1541 ReadyQueuesPostSeek( p_demux );
1542 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1543 FROM_SCALE(p_pmt->pcr.i_first + i_length * f) );
1548 i64 = stream_Size( p_sys->stream );
1550 stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) == VLC_SUCCESS )
1552 ReadyQueuesPostSeek( p_demux );
1557 case DEMUX_SET_TIME:
1558 i64 = (int64_t)va_arg( args, int64_t );
1560 if( p_sys->b_canseek &&
1561 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1562 p_pmt->pcr.i_first > -1 &&
1563 !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + TO_SCALE(i64) ) )
1565 ReadyQueuesPostSeek( p_demux );
1566 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1567 FROM_SCALE(p_pmt->pcr.i_first) + i64 - VLC_TS_0 );
1572 case DEMUX_GET_TIME:
1573 pi64 = (int64_t*)va_arg( args, int64_t * );
1575 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1577 if( !DVBEventInformation( p_demux, pi64, NULL ) )
1581 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1582 p_pmt->pcr.i_current > -1 && p_pmt->pcr.i_first > -1 )
1584 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
1585 *pi64 = FROM_SCALE(i_pcr - p_pmt->pcr.i_first);
1590 case DEMUX_GET_LENGTH:
1591 pi64 = (int64_t*)va_arg( args, int64_t * );
1593 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1595 if( !DVBEventInformation( p_demux, NULL, pi64 ) )
1599 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1600 ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) &&
1601 p_pmt->i_last_dts > 0 )
1603 int64_t i_start = (p_pmt->pcr.i_first > -1) ? p_pmt->pcr.i_first :
1604 TO_SCALE(p_pmt->pcr.i_first_dts);
1605 int64_t i_last = TimeStampWrapAround( p_pmt, p_pmt->i_last_dts );
1606 i_last += p_pmt->pcr.i_pcroffset;
1607 *pi64 = FROM_SCALE(i_last - i_start);
1612 case DEMUX_SET_GROUP:
1616 i_int = (int)va_arg( args, int );
1617 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1618 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1620 if( i_int != 0 ) /* If not default program */
1622 /* Deselect/filter current ones */
1626 p_sys->b_es_all = false;
1627 ARRAY_APPEND( p_sys->programs, i_int );
1628 UpdatePESFilters( p_demux, false );
1630 else if( likely( p_list != NULL ) )
1632 p_sys->b_es_all = false;
1633 for( int i = 0; i < p_list->i_count; i++ )
1634 ARRAY_APPEND( p_sys->programs, p_list->p_values[i].i_int );
1635 UpdatePESFilters( p_demux, false );
1639 p_sys->b_es_all = true;
1640 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1641 for( int i = 0; i < p_pat->programs.i_size; i++ )
1642 ARRAY_APPEND( p_sys->programs, p_pat->programs.p_elems[i]->i_pid );
1643 UpdatePESFilters( p_demux, true );
1646 p_sys->b_default_selection = false;
1654 i_int = (int)va_arg( args, int );
1655 msg_Dbg( p_demux, "DEMUX_SET_ES %d", i_int );
1657 if( !p_sys->b_es_all ) /* Won't change anything */
1658 UpdatePESFilters( p_demux, false );
1663 case DEMUX_GET_TITLE_INFO:
1665 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1666 int *c = va_arg( args, int * );
1668 *va_arg( args, int* ) = 0; /* Title offset */
1669 *va_arg( args, int* ) = 0; /* Chapter offset */
1670 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1673 case DEMUX_SET_TITLE:
1674 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1676 case DEMUX_SET_SEEKPOINT:
1677 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1679 case DEMUX_GET_META:
1680 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1682 case DEMUX_CAN_RECORD:
1683 pb_bool = (bool*)va_arg( args, bool * );
1687 case DEMUX_SET_RECORD_STATE:
1688 b_bool = (bool)va_arg( args, int );
1691 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1692 p_sys->b_start_record = b_bool;
1695 case DEMUX_GET_SIGNAL:
1696 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1702 return VLC_EGENERIC;
1705 /*****************************************************************************
1707 *****************************************************************************/
1708 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1710 demux_sys_t *p_sys = p_demux->p_sys;
1711 char *psz_dup = strdup( psz_fmt );
1712 char *psz = psz_dup;
1720 i_pid = strtol( psz, &psz, 0 );
1721 if( i_pid < 2 || i_pid >= 8192 )
1724 /* Parse optional program number */
1727 i_number = strtol( &psz[1], &psz, 0 );
1730 ts_pid_t *pmtpid = &p_sys->pid[i_pid];
1732 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1733 if ( !PIDSetup( p_demux, TYPE_PMT, pmtpid, &p_sys->pid[0] ) )
1737 ts_pmt_t *p_pmt = pmtpid->u.p_pmt;
1738 p_pmt->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1739 if( !dvbpsi_pmt_attach( p_pmt->handle,
1740 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1741 PMTCallBack, p_demux ) )
1743 PIDRelease( p_demux, pmtpid );
1747 ARRAY_APPEND( p_sys->pid[0].u.p_pat->programs, pmtpid );
1749 psz = strchr( psz, '=' );
1752 while( psz && *psz )
1754 char *psz_next = strchr( psz, ',' );
1760 i_pid = strtol( psz, &psz, 0 );
1761 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1764 char *psz_opt = &psz[1];
1765 if( !strcmp( psz_opt, "pcr" ) )
1767 p_pmt->i_pid_pcr = i_pid;
1769 else if( p_sys->pid[i_pid].type == TYPE_FREE )
1771 ts_pid_t *pid = &p_sys->pid[i_pid];
1773 char *psz_arg = strchr( psz_opt, '=' );
1777 if ( !PIDSetup( p_demux, TYPE_PES, pid, pmtpid ) )
1780 ARRAY_APPEND( p_pmt->e_streams, pid );
1782 if( p_pmt->i_pid_pcr <= 0 )
1783 p_pmt->i_pid_pcr = i_pid;
1785 es_format_t *fmt = &pid->u.p_pes->es.fmt;
1787 if( psz_arg && strlen( psz_arg ) == 4 )
1789 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1790 psz_arg[2], psz_arg[3] );
1791 int i_cat = UNKNOWN_ES;
1793 if( !strcmp( psz_opt, "video" ) )
1795 else if( !strcmp( psz_opt, "audio" ) )
1797 else if( !strcmp( psz_opt, "spu" ) )
1800 es_format_Init( fmt, i_cat, i_codec );
1801 fmt->b_packetized = false;
1805 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1806 PIDFillFormat( fmt, i_stream_type );
1809 fmt->i_group = i_number;
1810 if( p_sys->b_es_id_pid )
1813 if( fmt->i_cat != UNKNOWN_ES )
1815 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1816 (char*)&fmt->i_codec );
1817 pid->u.p_pes->es.id = es_out_Add( p_demux->out, fmt );
1826 p_sys->b_user_pmt = true;
1832 return VLC_EGENERIC;
1835 static int SetPIDFilter( demux_sys_t *p_sys, ts_pid_t *p_pid, bool b_selected )
1838 p_pid->i_flags |= FLAG_FILTERED;
1840 p_pid->i_flags &= ~FLAG_FILTERED;
1842 if( !p_sys->b_access_control )
1843 return VLC_EGENERIC;
1845 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1846 p_pid->i_pid, b_selected );
1849 static void PIDReset( ts_pid_t *pid )
1851 assert(pid->i_refcount == 0);
1853 pid->i_flags &= ~FLAG_SCRAMBLED;
1854 pid->p_parent = NULL;
1855 pid->type = TYPE_FREE;
1858 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent )
1860 if( pid == p_parent || pid->i_pid == 0x1FFF )
1863 if( pid->i_refcount == 0 )
1865 assert( pid->type == TYPE_FREE );
1868 case TYPE_FREE: /* nonsense ?*/
1874 pid->u.p_pat = ts_pat_New( p_demux );
1881 pid->u.p_pmt = ts_pmt_New( p_demux );
1888 pid->u.p_pes = ts_pes_New( p_demux );
1897 pid->u.p_psi = ts_psi_New( p_demux );
1909 pid->p_parent = p_parent;
1911 else if( pid->type == i_type && pid->i_refcount < UINT8_MAX )
1917 if( pid->type != TYPE_FREE )
1918 msg_Warn( p_demux, "Tried to redeclare pid %d with another type", pid->i_pid );
1925 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid )
1927 if( pid->i_refcount == 0 )
1929 assert( pid->type == TYPE_FREE );
1932 else if( pid->i_refcount == 1 )
1936 else if( pid->i_refcount > 1 )
1938 assert( pid->type != TYPE_FREE && pid->type != TYPE_PAT );
1942 if( pid->i_refcount == 0 )
1947 case TYPE_FREE: /* nonsense ?*/
1948 assert( pid->type != TYPE_FREE );
1952 ts_pat_Del( p_demux, pid->u.p_pat );
1953 pid->u.p_pat = NULL;
1957 ts_pmt_Del( p_demux, pid->u.p_pmt );
1958 pid->u.p_pmt = NULL;
1962 ts_pes_Del( p_demux, pid->u.p_pes );
1963 pid->u.p_pes = NULL;
1969 ts_psi_Del( p_demux, pid->u.p_psi );
1970 pid->u.p_psi = NULL;
1975 SetPIDFilter( p_demux->p_sys, pid, false );
1980 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1985 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1996 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1998 block_t *out = NULL;
1999 block_t **last = NULL;
2001 uint8_t *buf = block->p_buffer;
2002 size_t len = block->i_buffer;
2004 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
2005 int16_t start_trim = 0, end_trim = 0;
2006 int start_trim_flag = (buf[1] >> 4) & 1;
2007 int end_trim_flag = (buf[1] >> 3) & 1;
2008 int control_extension_flag = (buf[1] >> 2) & 1;
2013 unsigned au_size = 0;
2021 if (start_trim_flag) {
2022 start_trim = read_opus_flag(&buf, &len);
2023 if (start_trim < 0) {
2024 msg_Err(demux, "Invalid start trimming flag");
2027 if (end_trim_flag) {
2028 end_trim = read_opus_flag(&buf, &len);
2030 msg_Err(demux, "Invalid end trimming flag");
2033 if (control_extension_flag && len) {
2034 unsigned l = *buf++; len--;
2036 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
2043 if (!au_size || au_size > len) {
2044 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
2048 block_t *au = block_Alloc(au_size);
2051 memcpy(au->p_buffer, buf, au_size);
2052 block_CopyProperties(au, block);
2061 au->i_nb_samples = opus_frame_duration(buf, au_size);
2062 if (end_trim && end_trim <= au->i_nb_samples)
2063 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
2067 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
2068 au->i_nb_samples -= start_trim;
2069 if (au->i_nb_samples == 0)
2070 au->i_flags |= BLOCK_FLAG_PREROLL;
2077 block_Release(block);
2081 /****************************************************************************
2083 ****************************************************************************/
2084 static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header, size_t i_header,
2085 unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
2090 return VLC_EGENERIC;
2092 switch( p_header[3] )
2094 case 0xBC: /* Program stream map */
2095 case 0xBE: /* Padding */
2096 case 0xBF: /* Private stream 2 */
2097 case 0xF0: /* ECM */
2098 case 0xF1: /* EMM */
2099 case 0xFF: /* Program stream directory */
2100 case 0xF2: /* DSMCC stream */
2101 case 0xF8: /* ITU-T H.222.1 type E stream */
2105 if( ( p_header[6]&0xC0 ) == 0x80 )
2108 i_skip = p_header[8] + 9;
2110 if( p_header[7]&0x80 ) /* has pts */
2112 if( i_header < 9 + 5 )
2113 return VLC_EGENERIC;
2114 *pi_pts = ExtractPESTimestamp( &p_header[9] );
2116 if( p_header[7]&0x40 ) /* has dts */
2118 if( i_header < 14 + 5 )
2119 return VLC_EGENERIC;
2120 *pi_dts = ExtractPESTimestamp( &p_header[14] );
2127 if( i_header < i_skip + 1 )
2128 return VLC_EGENERIC;
2129 while( i_skip < 23 && p_header[i_skip] == 0xff )
2132 if( i_header < i_skip + 1 )
2133 return VLC_EGENERIC;
2137 msg_Err( p_demux, "too much MPEG-1 stuffing" );
2138 return VLC_EGENERIC;
2140 if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
2145 if( i_header < i_skip + 1 )
2146 return VLC_EGENERIC;
2148 if( p_header[i_skip]&0x20 )
2150 if( i_header < i_skip + 5 )
2151 return VLC_EGENERIC;
2152 *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
2154 if( p_header[i_skip]&0x10 ) /* has dts */
2156 if( i_header < i_skip + 10 )
2157 return VLC_EGENERIC;
2158 *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
2178 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
2181 unsigned i_pes_size = 0;
2182 unsigned i_skip = 0;
2185 mtime_t i_length = 0;
2187 assert(pid->type == TYPE_PES);
2188 assert(pid->p_parent && pid->p_parent->type == TYPE_PMT);
2190 const int i_max = block_ChainExtract( p_pes, header, 34 );
2193 block_ChainRelease( p_pes );
2197 if( SCRAMBLED(*pid) || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2199 if ( !SCRAMBLED(*pid) )
2200 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
2201 header[0], header[1],header[2],header[3], pid->i_pid );
2202 block_ChainRelease( p_pes );
2206 if( ParsePESHeader( p_demux, (uint8_t*)&header, i_max, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
2208 block_ChainRelease( p_pes );
2214 i_pts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_pts );
2216 i_dts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_dts );
2219 if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
2220 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
2224 else if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
2225 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
2226 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
2230 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT &&
2231 pid->u.p_pes->es.p_mpeg4desc )
2233 decoder_config_descriptor_t *dcd = &pid->u.p_pes->es.p_mpeg4desc->dec_descr;
2235 if( dcd->i_extra > 2 &&
2236 dcd->p_extra[0] == 0x10 &&
2237 ( dcd->p_extra[1]&0x10 ) )
2239 /* display length */
2240 if( p_pes->i_buffer + 2 <= i_skip )
2241 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
2245 if( p_pes->i_buffer + 2 <= i_skip )
2246 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
2252 while( p_pes && i_skip > 0 )
2254 if( p_pes->i_buffer <= i_skip )
2256 block_t *p_next = p_pes->p_next;
2258 i_skip -= p_pes->i_buffer;
2259 block_Release( p_pes );
2264 p_pes->i_buffer -= i_skip;
2265 p_pes->p_buffer += i_skip;
2270 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
2271 if( i_pts >= 0 && i_dts < 0 )
2279 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
2282 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
2284 p_pes->i_length = i_length * 100 / 9;
2286 p_block = block_ChainGather( p_pes );
2287 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT )
2289 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2291 p_block->i_buffer = i_pes_size;
2294 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2297 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2299 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_TELETEXT )
2301 if( p_block->i_pts <= VLC_TS_INVALID && pid->p_parent )
2303 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
2304 * In this case use the last PCR + 40ms */
2305 assert( pid->p_parent->type == TYPE_PMT );
2306 if( likely(pid->p_parent->type == TYPE_PMT) )
2308 mtime_t i_pcr = pid->p_parent->u.p_pmt->pcr.i_current;
2309 if( i_pcr > VLC_TS_INVALID )
2310 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
2314 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_A ||
2315 pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_C )
2317 if( p_block->i_pts <= VLC_TS_INVALID )
2319 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2321 p_block->i_buffer = i_pes_size;
2324 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2327 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2330 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_OPUS)
2332 p_block = Opus_Parse(p_demux, p_block);
2335 if( !pid->p_parent || pid->p_parent->type != TYPE_PMT )
2337 block_Release( p_block );
2341 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2344 block_t *p_next = p_block->p_next;
2345 p_block->p_next = NULL;
2347 if( p_pmt->pcr.i_first == -1 ) /* Not seen yet */
2348 PCRFixHandle( p_demux, p_pmt, p_block );
2350 if( pid->u.p_pes->es.id && (p_pmt->pcr.i_current > -1 || p_pmt->pcr.b_disable) )
2352 if( pid->u.p_pes->p_prepcr_outqueue )
2354 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2355 p_block = pid->u.p_pes->p_prepcr_outqueue;
2356 pid->u.p_pes->p_prepcr_outqueue = NULL;
2359 if ( p_pmt->pcr.b_disable && p_block->i_dts > VLC_TS_INVALID &&
2360 ( p_pmt->i_pid_pcr == pid->i_pid || p_pmt->i_pid_pcr == 0x1FFF ) )
2362 ProgramSetPCR( p_demux, p_pmt, (p_block->i_dts - VLC_TS_0) * 9 / 100 - 120000 );
2365 /* Compute PCR/DTS offset if any */
2366 if( p_pmt->pcr.i_pcroffset == -1 && p_block->i_dts > VLC_TS_INVALID &&
2367 p_pmt->pcr.i_current > VLC_TS_INVALID )
2369 int64_t i_dts27 = (p_block->i_dts - VLC_TS_0) * 9 / 100;
2370 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
2371 if( i_dts27 < i_pcr )
2373 p_pmt->pcr.i_pcroffset = i_pcr - i_dts27 + 80000;
2374 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64
2375 "us later than pcr, applying delay",
2376 pid->i_pid, p_pmt->pcr.i_pcroffset * 100 / 9 );
2378 else p_pmt->pcr.i_pcroffset = 0;
2381 if( p_pmt->pcr.i_pcroffset != -1 )
2383 if( p_block->i_dts > VLC_TS_INVALID )
2384 p_block->i_dts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2385 if( p_block->i_pts > VLC_TS_INVALID )
2386 p_block->i_pts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2389 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
2391 es_out_Send( p_demux->out, pid->u.p_pes->extra_es.p_elems[i]->id,
2392 block_Duplicate( p_block ) );
2395 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2399 if( p_pmt->pcr.i_first == -1 ) /* Not seen yet */
2400 PCRFixHandle( p_demux, p_pmt, p_block );
2402 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2410 msg_Warn( p_demux, "empty pes" );
2414 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
2416 block_t *p_content = block_ChainGather( p_data );
2418 if ( pid->p_parent && pid->p_parent->type == TYPE_PMT )
2420 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2421 mtime_t i_date = p_pmt->pcr.i_current;
2423 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SCTE_27 )
2425 /* We need to extract the truncated pts stored inside the payload */
2426 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
2429 size_t i_offset = 4;
2430 if( p_content->p_buffer[3] & 0x40 )
2432 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
2433 p_content->p_buffer[8];
2436 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
2438 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
2441 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
2442 if( i_display_in < i_date )
2443 i_date = i_display_in + (1ll << 32);
2445 i_date = i_display_in;
2452 p_content->i_dts = p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
2453 PCRFixHandle( p_demux, p_pmt, p_content );
2456 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_content );
2459 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
2461 block_t *p_data = pid->u.p_pes->p_data;
2466 /* remove the pes from pid */
2467 pid->u.p_pes->p_data = NULL;
2468 pid->u.p_pes->i_data_size = 0;
2469 pid->u.p_pes->i_data_gathered = 0;
2470 pid->u.p_pes->pp_last = &pid->u.p_pes->p_data;
2472 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
2474 ParsePES( p_demux, pid, p_data );
2476 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
2478 ParseTableSection( p_demux, pid, p_data );
2482 block_ChainRelease( p_data );
2486 static block_t* ReadTSPacket( demux_t *p_demux )
2488 demux_sys_t *p_sys = p_demux->p_sys;
2492 /* Get a new TS packet */
2493 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2495 if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
2496 msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
2498 msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
2502 if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
2504 block_Release( p_pkt );
2508 /* Skip header (BluRay streams).
2509 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2510 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2512 p_pkt->p_buffer += p_sys->i_packet_header_size;
2513 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2515 /* Check sync byte and re-sync if needed */
2516 if( p_pkt->p_buffer[0] != 0x47 )
2518 msg_Warn( p_demux, "lost synchro" );
2519 block_Release( p_pkt );
2522 const uint8_t *p_peek;
2523 int i_peek, i_skip = 0;
2525 i_peek = stream_Peek( p_sys->stream, &p_peek,
2526 p_sys->i_packet_size * 10 );
2527 if( i_peek < p_sys->i_packet_size + 1 )
2529 msg_Dbg( p_demux, "eof ?" );
2533 while( i_skip < i_peek - p_sys->i_packet_size )
2535 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2536 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2542 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2543 stream_Read( p_sys->stream, NULL, i_skip );
2545 if( i_skip < i_peek - p_sys->i_packet_size )
2550 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2552 msg_Dbg( p_demux, "eof ?" );
2559 static int64_t TimeStampWrapAround( ts_pmt_t *p_pmt, int64_t i_time )
2561 int64_t i_adjust = 0;
2562 if( p_pmt && p_pmt->pcr.i_first > 0x0FFFFFFFF && i_time < 0x0FFFFFFFF )
2563 i_adjust = 0x1FFFFFFFF;
2565 return i_time + i_adjust;
2568 static mtime_t GetPCR( block_t *p_pkt )
2570 const uint8_t *p = p_pkt->p_buffer;
2574 if( ( p[3]&0x20 ) && /* adaptation */
2578 /* PCR is 33 bits */
2579 i_pcr = ( (mtime_t)p[6] << 25 ) |
2580 ( (mtime_t)p[7] << 17 ) |
2581 ( (mtime_t)p[8] << 9 ) |
2582 ( (mtime_t)p[9] << 1 ) |
2583 ( (mtime_t)p[10] >> 7 );
2588 static void UpdateScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool b_scrambled )
2590 if( !SCRAMBLED(*p_pid) == !b_scrambled )
2593 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2594 p_pid->i_pid, SCRAMBLED(*p_pid), b_scrambled );
2596 p_pid->i_flags |= (b_scrambled) ? FLAG_SCRAMBLED : FLAGS_NONE;
2598 if( p_pid->type == TYPE_PES && p_pid->u.p_pes->es.id )
2600 for( int i = 0; i < p_pid->u.p_pes->extra_es.i_size; i++ )
2602 if( p_pid->u.p_pes->extra_es.p_elems[i]->id )
2603 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2604 p_pid->u.p_pes->extra_es.p_elems[i]->id, b_scrambled );
2606 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2607 p_pid->u.p_pes->es.id, b_scrambled );
2611 static inline void FlushESBuffer( ts_pes_t *p_pes )
2615 p_pes->i_data_gathered = p_pes->i_data_size = 0;
2616 block_ChainRelease( p_pes->p_data );
2617 p_pes->p_data = NULL;
2618 p_pes->pp_last = &p_pes->p_data;
2622 static void ReadyQueuesPostSeek( demux_t *p_demux )
2624 demux_sys_t *p_sys = p_demux->p_sys;
2626 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2627 for( int i=0; i< p_pat->programs.i_size; i++ )
2629 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2630 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2632 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
2634 if( pid->type != TYPE_PES )
2637 if( pid->u.p_pes->es.id )
2639 block_t *p_block = block_Alloc(1);
2642 p_block->i_buffer = 0;
2643 p_block->i_flags = BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED;
2644 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2650 if( pid->u.p_pes->p_prepcr_outqueue )
2652 block_ChainRelease( pid->u.p_pes->p_prepcr_outqueue );
2653 pid->u.p_pes->p_prepcr_outqueue = NULL;
2656 FlushESBuffer( pid->u.p_pes );
2658 p_pmt->pcr.i_current = -1;
2662 static int SeekToTime( demux_t *p_demux, ts_pmt_t *p_pmt, int64_t i_scaledtime )
2664 demux_sys_t *p_sys = p_demux->p_sys;
2666 /* Deal with common but worst binary search case */
2667 if( p_pmt->pcr.i_first == i_scaledtime && p_sys->b_canseek )
2668 return stream_Seek( p_sys->stream, 0 );
2670 if( !p_sys->b_canfastseek )
2671 return VLC_EGENERIC;
2673 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2675 /* Find the time position by using binary search algorithm. */
2676 int64_t i_head_pos = 0;
2677 int64_t i_tail_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2678 if( i_head_pos >= i_tail_pos )
2679 return VLC_EGENERIC;
2681 bool b_found = false;
2682 while( (i_head_pos + p_sys->i_packet_size) <= i_tail_pos && !b_found )
2684 /* Round i_pos to a multiple of p_sys->i_packet_size */
2685 int64_t i_splitpos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2686 int64_t i_div = i_splitpos % p_sys->i_packet_size;
2687 i_splitpos -= i_div;
2689 if ( stream_Seek( p_sys->stream, i_splitpos ) != VLC_SUCCESS )
2692 int64_t i_pos = i_splitpos;
2693 while( i_pos > -1 && i_pos < i_tail_pos )
2696 block_t *p_pkt = ReadTSPacket( p_demux );
2699 i_head_pos = i_tail_pos;
2703 i_pos = stream_Tell( p_sys->stream );
2705 int i_pid = PIDGet( p_pkt );
2706 if( i_pid != 0x1FFF && p_sys->pid[i_pid].type == TYPE_PES &&
2707 p_sys->pid[i_pid].p_parent->u.p_pmt == p_pmt &&
2708 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
2709 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
2712 unsigned i_skip = 4;
2713 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
2715 if( p_pkt->i_buffer >= 4 + 2 + 5 )
2717 i_pcr = GetPCR( p_pkt );
2718 i_skip += 1 + p_pkt->p_buffer[4];
2725 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2726 p_pkt->i_buffer - i_skip, &i_skip, &i_dts, &i_pts ) )
2733 block_Release( p_pkt );
2737 int64_t i_diff = i_scaledtime - TimeStampWrapAround( p_pmt, i_pcr );
2739 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2740 else if( i_diff < TO_SCALE(VLC_TS_0 + CLOCK_FREQ / 2) ) // 500ms
2748 if ( !b_found && i_pos > i_tail_pos - p_sys->i_packet_size )
2749 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2754 msg_Dbg( p_demux, "Seek():cannot find a time position." );
2755 stream_Seek( p_sys->stream, i_initial_pos );
2756 return VLC_EGENERIC;
2761 static ts_pmt_t * GetProgramByID( demux_sys_t *p_sys, int i_program )
2763 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
2766 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2767 for( int i = 0; i < p_pat->programs.i_size; i++ )
2769 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
2770 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2771 if( p_pmt->i_number == i_program )
2777 #define PROBE_CHUNK_COUNT 250
2779 static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_pcr, bool *pb_found )
2781 demux_sys_t *p_sys = p_demux->p_sys;
2783 block_t *p_pkt = NULL;
2789 if( i_count++ > PROBE_CHUNK_COUNT || !( p_pkt = ReadTSPacket( p_demux ) ) )
2794 const int i_pid = PIDGet( p_pkt );
2795 ts_pid_t *p_pid = &p_sys->pid[i_pid];
2797 p_pid->i_flags |= FLAG_SEEN;
2799 if( i_pid != 0x1FFF && (p_pkt->p_buffer[1] & 0x80) == 0 ) /* not corrupt */
2801 bool b_pcrresult = true;
2802 bool b_adaptfield = p_pkt->p_buffer[3] & 0x20;
2804 if( b_adaptfield && p_pkt->i_buffer >= 4 + 2 + 5 )
2805 *pi_pcr = GetPCR( p_pkt );
2807 if( *pi_pcr == -1 &&
2808 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* payload start */
2809 (p_pkt->p_buffer[3] & 0xD0) == 0x10 && /* Has payload but is not encrypted */
2810 p_pid->type == TYPE_PES &&
2811 p_pid->u.p_pes->es.fmt.i_cat != UNKNOWN_ES
2814 b_pcrresult = false;
2817 unsigned i_skip = 4;
2818 if ( b_adaptfield ) // adaptation field
2819 i_skip += 1 + p_pkt->p_buffer[4];
2821 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2822 p_pkt->i_buffer - i_skip,
2823 &i_skip, &i_dts, &i_pts ) )
2827 else if( i_pts != -1 )
2834 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2835 for( int i=0; i<p_pat->programs.i_size; i++ )
2837 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2838 if( ( p_pmt->i_pid_pcr == p_pid->i_pid ||
2839 ( p_pmt->i_pid_pcr == 0x1FFF && p_pid->p_parent == p_pat->programs.p_elems[i] ) ) )
2843 p_pmt->i_last_dts = *pi_pcr;
2845 /* Start, only keep first */
2846 else if( b_pcrresult && p_pmt->pcr.i_first == -1 )
2848 p_pmt->pcr.i_first = *pi_pcr;
2850 else if( p_pmt->pcr.i_first_dts < VLC_TS_0 )
2852 p_pmt->pcr.i_first_dts = FROM_SCALE(*pi_pcr);
2855 if( i_program == 0 || i_program == p_pmt->i_number )
2862 block_Release( p_pkt );
2868 static int ProbeStart( demux_t *p_demux, int i_program )
2870 demux_sys_t *p_sys = p_demux->p_sys;
2871 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2872 int64_t i_stream_size = stream_Size( p_sys->stream );
2874 int i_probe_count = 0;
2877 bool b_found = false;
2881 i_pos = p_sys->i_packet_size * i_probe_count;
2882 i_pos = __MIN( i_pos, i_stream_size );
2884 if( stream_Seek( p_sys->stream, i_pos ) )
2885 return VLC_EGENERIC;
2887 ProbeChunk( p_demux, i_program, false, &i_pcr, &b_found );
2889 /* Go ahead one more chunk if end of file contained only stuffing packets */
2890 i_probe_count += PROBE_CHUNK_COUNT;
2891 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (2 * PROBE_CHUNK_COUNT) );
2893 stream_Seek( p_sys->stream, i_initial_pos );
2895 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2898 static int ProbeEnd( demux_t *p_demux, int i_program )
2900 demux_sys_t *p_sys = p_demux->p_sys;
2901 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2902 int64_t i_stream_size = stream_Size( p_sys->stream );
2904 int i_probe_count = PROBE_CHUNK_COUNT;
2907 bool b_found = false;
2911 i_pos = i_stream_size - (p_sys->i_packet_size * i_probe_count);
2912 i_pos = __MAX( i_pos, 0 );
2914 if( stream_Seek( p_sys->stream, i_pos ) )
2915 return VLC_EGENERIC;
2917 ProbeChunk( p_demux, i_program, true, &i_pcr, &b_found );
2919 /* Go ahead one more chunk if end of file contained only stuffing packets */
2920 i_probe_count += PROBE_CHUNK_COUNT;
2921 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (6 * PROBE_CHUNK_COUNT) );
2923 stream_Seek( p_sys->stream, i_initial_pos );
2925 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2928 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_pmt, mtime_t i_pcr )
2930 demux_sys_t *p_sys = p_demux->p_sys;
2932 /* Check if we have enqueued blocks waiting the/before the
2933 PCR barrier, and then adapt pcr so they have valid PCR when dequeuing */
2934 if( p_pmt->pcr.i_current == -1 )
2936 mtime_t i_mindts = -1;
2938 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2939 for( int i=0; i< p_pat->programs.i_size; i++ )
2941 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2942 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2944 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[j];
2945 block_t *p_block = p_pid->u.p_pes->p_prepcr_outqueue;
2946 while( p_block && p_block->i_dts == VLC_TS_INVALID )
2947 p_block = p_block->p_next;
2949 if( p_block && ( i_mindts == -1 || p_block->i_dts < i_mindts ) )
2950 i_mindts = p_block->i_dts;
2954 if( i_mindts > VLC_TS_INVALID )
2956 msg_Dbg( p_demux, "Program %d PCR prequeue fixup %"PRId64"->%"PRId64,
2957 p_pmt->i_number, TO_SCALE(i_mindts), i_pcr );
2958 i_pcr = TO_SCALE(i_mindts);
2962 p_pmt->pcr.i_current = i_pcr;
2963 if( p_pmt->pcr.i_first == -1 )
2965 p_pmt->pcr.i_first = i_pcr; // now seen
2968 if ( p_sys->i_pmt_es )
2970 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2971 p_pmt->i_number, VLC_TS_0 + i_pcr * 100 / 9 );
2975 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2977 demux_sys_t *p_sys = p_demux->p_sys;
2979 if( p_sys->i_pmt_es <= 0 )
2982 mtime_t i_pcr = GetPCR( p_bk );
2986 pid->probed.i_pcr_count++;
2988 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
2991 /* Search program and set the PCR */
2992 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2993 for( int i = 0; i < p_pat->programs.i_size; i++ )
2995 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2996 mtime_t i_program_pcr = TimeStampWrapAround( p_pmt, i_pcr );
2998 if( p_pmt->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
3000 if( pid->p_parent == p_pat->programs.p_elems[i] ) /* PCR shall be on pid itself */
3002 /* ? update PCR for the whole group program ? */
3003 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3006 else /* set PCR provided by current pid to program(s) referencing it */
3008 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
3009 if( p_pmt->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
3011 /* We've found a target group for update */
3012 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3019 static int FindPCRCandidate( ts_pmt_t *p_pmt )
3021 ts_pid_t *p_cand = NULL;
3022 int i_previous = p_pmt->i_pid_pcr;
3024 for( int i=0; i<p_pmt->e_streams.i_size; i++ )
3026 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[i];
3028 (!p_cand || p_cand->i_pid != i_previous) )
3030 if( p_pid->probed.i_pcr_count ) /* check PCR frequency first */
3032 if( !p_cand || p_pid->probed.i_pcr_count > p_cand->probed.i_pcr_count )
3039 if( p_pid->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3044 else if ( p_pid->u.p_pes->es.fmt.i_cat == VIDEO_ES ) /* Otherwise prioritize video dts */
3046 if( !p_cand || p_cand->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3053 return p_cand->i_pid;
3058 static void PCRFixHandle( demux_t *p_demux, ts_pmt_t *p_pmt, block_t *p_block )
3060 if( p_pmt->pcr.i_first > -1 || p_pmt->pcr.b_disable )
3063 /* Record the first data packet timestamp in case there wont be any PCR */
3064 if( !p_pmt->pcr.i_first_dts )
3066 p_pmt->pcr.i_first_dts = p_block->i_dts;
3068 else if( p_block->i_dts - p_pmt->pcr.i_first_dts > CLOCK_FREQ / 2 ) /* "shall not exceed 100ms" */
3070 int i_cand = FindPCRCandidate( p_pmt );
3071 p_pmt->i_pid_pcr = i_cand;
3072 p_pmt->pcr.b_disable = true; /* So we do not wait packet PCR flag as there might be none on the pid */
3073 msg_Warn( p_demux, "No PCR received for program %d, set up workaround using pid %d",
3074 p_pmt->i_number, i_cand );
3075 UpdatePESFilters( p_demux, p_demux->p_sys->b_es_all );
3079 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
3081 const uint8_t *p = p_bk->p_buffer;
3082 const bool b_unit_start = p[1]&0x40;
3083 const bool b_adaptation = p[3]&0x20;
3084 const bool b_payload = p[3]&0x10;
3085 const int i_cc = p[3]&0x0f; /* continuity counter */
3086 bool b_discontinuity = false; /* discontinuity */
3088 /* transport_scrambling_control is ignored */
3093 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
3094 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
3098 /* For now, ignore additional error correction
3099 * TODO: handle Reed-Solomon 204,188 error correction */
3100 p_bk->i_buffer = TS_PACKET_SIZE_188;
3104 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
3106 if( pid->u.p_pes->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
3107 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3110 if( p_demux->p_sys->csa )
3112 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
3113 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
3114 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
3119 /* We don't have any adaptation_field, so payload starts
3120 * immediately after the 4 byte TS header */
3125 /* p[4] is adaptation_field_length minus one */
3129 /* discontinuity indicator found in stream */
3130 b_discontinuity = (p[5]&0x80) ? true : false;
3131 if( b_discontinuity && pid->u.p_pes->p_data )
3133 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
3135 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
3139 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
3144 /* Test continuity counter */
3145 /* continuous when (one of this):
3147 * diff == 0 and payload == 0
3148 * diff == 0 and duplicate packet (playload != 0) <- should we
3149 * test the content ?
3151 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
3152 if( b_payload && i_diff == 1 )
3154 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
3158 if( pid->i_cc == 0xff )
3160 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
3164 else if( i_diff != 0 && !b_discontinuity )
3166 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
3167 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
3170 if( pid->u.p_pes->p_data && pid->u.p_pes->es.fmt.i_cat != VIDEO_ES &&
3171 pid->u.p_pes->es.fmt.i_cat != AUDIO_ES )
3173 /* Small audio/video artifacts are usually better than
3174 * dropping full frames */
3175 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3180 PCRHandle( p_demux, pid, p_bk );
3184 block_Release( p_bk );
3188 /* We have to gather it */
3189 p_bk->p_buffer += i_skip;
3190 p_bk->i_buffer -= i_skip;
3194 if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
3196 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
3197 block_t *p = block_Duplicate( p_bk );
3200 p->i_buffer = i_pointer_field;
3202 block_ChainLastAppend( &pid->u.p_pes->pp_last, p );
3204 p_bk->i_buffer -= 1 + i_pointer_field;
3205 p_bk->p_buffer += 1 + i_pointer_field;
3207 if( pid->u.p_pes->p_data )
3209 ParseData( p_demux, pid );
3213 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3214 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
3216 if( p_bk->i_buffer > 6 )
3218 pid->u.p_pes->i_data_size = GetWBE( &p_bk->p_buffer[4] );
3219 if( pid->u.p_pes->i_data_size > 0 )
3221 pid->u.p_pes->i_data_size += 6;
3225 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
3227 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
3229 pid->u.p_pes->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
3232 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3233 if( pid->u.p_pes->i_data_size > 0 &&
3234 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3236 ParseData( p_demux, pid );
3242 if( pid->u.p_pes->p_data == NULL )
3244 /* msg_Dbg( p_demux, "broken packet" ); */
3245 block_Release( p_bk );
3249 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3250 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3252 if( pid->u.p_pes->i_data_size > 0 &&
3253 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3255 ParseData( p_demux, pid );
3264 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
3266 switch( i_stream_type )
3268 case 0x01: /* MPEG-1 video */
3269 case 0x02: /* MPEG-2 video */
3270 case 0x80: /* MPEG-2 MOTO video */
3271 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
3273 case 0x03: /* MPEG-1 audio */
3274 case 0x04: /* MPEG-2 audio */
3275 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
3277 case 0x11: /* MPEG4 (audio) LATM */
3278 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
3279 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
3280 transport syntax, such as DST, ALS and SLS */
3281 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
3283 case 0x10: /* MPEG4 (video) */
3284 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
3286 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
3287 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
3289 case 0x24: /* HEVC */
3290 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
3292 case 0x42: /* CAVS (Chinese AVS) */
3293 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
3296 case 0x81: /* A52 (audio) */
3297 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
3299 case 0x82: /* SCTE-27 (sub) */
3300 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
3302 case 0x84: /* SDDS (audio) */
3303 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
3305 case 0x85: /* DTS (audio) */
3306 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
3308 case 0x87: /* E-AC3 */
3309 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
3312 case 0x91: /* A52 vls (audio) */
3313 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
3315 case 0x92: /* DVD_SPU vls (sub) */
3316 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
3319 case 0x94: /* SDDS (audio) */
3320 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
3323 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
3324 es_format_Init( fmt, UNKNOWN_ES, 0 );
3327 case 0x06: /* PES_PRIVATE (fixed later) */
3328 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
3329 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
3331 es_format_Init( fmt, UNKNOWN_ES, 0 );
3335 /* PES packets usually contain truncated frames */
3336 fmt->b_packetized = false;
3339 /*****************************************************************************
3340 * MP4 specific functions (IOD parser)
3341 *****************************************************************************/
3342 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
3345 unsigned int i_len = 0;
3351 i_len = ( i_len << 7 ) + ( i_b&0x7f );
3353 } while( i_b&0x80 && *pi_data > 0 );
3355 if (i_len > *pi_data)
3361 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
3364 while( *pi_data > 0 && bytes-- )
3375 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
3377 int len = IODGetBytes( pi_data, pp_data, 1 );
3380 char *url = strndup( (char*)*pp_data, len );
3386 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
3388 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
3390 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
3399 byte1 = IODGetBytes( &i_data, &p_data, 1 );
3400 byte2 = IODGetBytes( &i_data, &p_data, 1 );
3401 byte3 = IODGetBytes( &i_data, &p_data, 1 );
3402 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
3404 i_iod_label = byte1;
3407 else //correct implementation of the IOD_descriptor
3409 i_iod_label = byte2;
3413 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
3415 if( i_iod_tag != 0x02 )
3417 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
3421 IODDescriptorLength( &i_data, &p_data );
3423 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
3424 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
3425 i_od_id |= i_flags >> 6;
3426 ts_debug( "\n* od_id:%d", i_od_id );
3427 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
3428 if ((i_flags >> 5) & 0x01)
3430 p_iod->psz_url = IODGetURL( &i_data, &p_data );
3431 ts_debug( "\n* url string:%s", p_iod->psz_url );
3432 ts_debug( "\n*****************************\n" );
3437 p_iod->psz_url = NULL;
3440 /* Profile Level Indication */
3441 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
3442 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
3443 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
3444 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
3445 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
3448 int i_data_sav = i_data;
3449 uint8_t *p_data_sav = p_data;
3450 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
3452 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
3454 p_data = p_data_sav + i_length;
3455 i_data = i_data_sav - i_length;
3457 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
3458 i_length = IODDescriptorLength( &i_data, &p_data );
3460 i_data_sav = i_data;
3461 p_data_sav = p_data;
3465 if ( i_tag != 0x03 )
3467 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
3471 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
3472 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
3473 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
3474 if( b_streamDependenceFlag )
3475 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
3477 if( (i_flags >> 6) & 0x01 )
3478 es_descr->psz_url = IODGetURL( &i_data, &p_data );
3480 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
3481 if( b_OCRStreamFlag )
3482 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
3484 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
3486 ts_debug( "\n* ERR missing DecoderConfigDescr" );
3489 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
3490 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
3491 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
3492 i_flags = IODGetBytes( &i_data, &p_data, 1 );
3493 dec_descr->i_streamType = i_flags >> 2;
3495 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
3496 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
3497 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
3499 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
3501 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
3502 if( dec_descr->i_extra > 0 )
3504 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
3505 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
3506 p_data += dec_descr->i_extra;
3507 i_data -= dec_descr->i_extra;
3512 dec_descr->i_extra = 0;
3513 dec_descr->p_extra = NULL;
3516 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
3518 ts_debug( "\n* ERR missing SLConfigDescr" );
3521 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
3522 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
3525 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
3530 es_descr->b_ok = true;
3536 static void IODFree( iod_descriptor_t *p_iod )
3538 if( p_iod->psz_url )
3540 free( p_iod->psz_url );
3545 for( int i = 0; i < 255; i++ )
3547 #define es_descr p_iod->es_descr[i]
3550 if( es_descr.psz_url )
3551 free( es_descr.psz_url );
3553 free( es_descr.dec_descr.p_extra );
3560 /****************************************************************************
3561 ****************************************************************************
3562 ** libdvbpsi callbacks
3563 ****************************************************************************
3564 ****************************************************************************/
3565 static bool ProgramIsSelected( demux_sys_t *p_sys, uint16_t i_pgrm )
3567 for(int i=0; i<p_sys->programs.i_size; i++)
3568 if( p_sys->programs.p_elems[i] == i_pgrm )
3574 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
3576 demux_sys_t *p_sys = p_demux->p_sys;
3578 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
3581 msg_Warn( p_demux, "Switching to non DVB mode" );
3583 /* This doesn't look like a DVB stream so don't try
3584 * parsing the SDT/EDT/TDT */
3586 PIDRelease( p_demux, &p_sys->pid[0x11] );
3587 PIDRelease( p_demux, &p_sys->pid[0x12] );
3588 PIDRelease( p_demux, &p_sys->pid[0x14] );
3589 p_sys->b_dvb_meta = false;
3592 #include "dvb-text.h"
3594 static char *EITConvertToUTF8( demux_t *p_demux,
3595 const unsigned char *psz_instring,
3599 demux_sys_t *p_sys = p_demux->p_sys;
3601 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3603 if ( !p_sys->arib.p_instance )
3604 p_sys->arib.p_instance = arib_instance_new( p_demux );
3605 if ( !p_sys->arib.p_instance )
3607 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3611 char *psz_outstring = NULL;
3614 i_out = i_length * 4;
3615 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3616 if( !psz_outstring )
3619 arib_initialize_decoder( p_decoder );
3620 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3621 psz_outstring, i_out );
3622 arib_finalize_decoder( p_decoder );
3624 return psz_outstring;
3629 /* Deal with no longer broken providers (no switch byte
3630 but sending ISO_8859-1 instead of ISO_6937) without
3631 removing them from the broken providers table
3632 (keep the entry for correctly handling recorded TS).
3634 b_broken = b_broken && i_length && *psz_instring > 0x20;
3637 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3638 return vlc_from_EIT( psz_instring, i_length );
3641 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3643 demux_sys_t *p_sys = p_demux->p_sys;
3644 ts_pid_t *sdt = &p_sys->pid[0x11];
3645 dvbpsi_sdt_service_t *p_srv;
3647 msg_Dbg( p_demux, "SDTCallBack called" );
3649 if( p_sys->es_creation != CREATE_ES ||
3650 !p_sdt->b_current_next ||
3651 p_sdt->i_version == sdt->u.p_psi->i_version )
3653 dvbpsi_sdt_delete( p_sdt );
3657 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3660 p_sdt->i_version, p_sdt->b_current_next,
3661 p_sdt->i_network_id );
3663 p_sys->b_broken_charset = false;
3665 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3668 dvbpsi_descriptor_t *p_dr;
3670 const char *psz_type = NULL;
3671 const char *psz_status = NULL;
3673 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3674 "running=%d free_ca=%d",
3675 p_srv->i_service_id, p_srv->b_eit_schedule,
3676 p_srv->b_eit_present, p_srv->i_running_status,
3679 if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
3681 msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
3682 p_sys->vdr.i_service );
3686 p_meta = vlc_meta_New();
3687 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3689 if( p_dr->i_tag == 0x48 )
3691 static const char *ppsz_type[17] = {
3693 "Digital television service",
3694 "Digital radio sound service",
3696 "NVOD reference service",
3697 "NVOD time-shifted service",
3700 "SECAM coded signal",
3703 "NTSC coded signal",
3704 "Data broadcast service",
3705 "Reserved for Common Interface Usage",
3706 "RCS Map (see EN 301 790 [35])",
3707 "RCS FLS (see EN 301 790 [35])",
3710 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3714 /* Workarounds for broadcasters with broken EPG */
3716 if( p_sdt->i_network_id == 133 )
3717 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3719 /* List of providers using ISO8859-1 */
3720 static const char ppsz_broken_providers[][8] = {
3721 "CSAT", /* CanalSat FR */
3722 "GR1", /* France televisions */
3724 "MR5", /* France 2/M6 HD */
3727 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3729 const size_t i_length = strlen(ppsz_broken_providers[i]);
3730 if( pD->i_service_provider_name_length == i_length &&
3731 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3732 p_sys->b_broken_charset = true;
3735 /* FIXME: Digital+ ES also uses ISO8859-1 */
3737 str1 = EITConvertToUTF8(p_demux,
3738 pD->i_service_provider_name,
3739 pD->i_service_provider_name_length,
3740 p_sys->b_broken_charset );
3741 str2 = EITConvertToUTF8(p_demux,
3743 pD->i_service_name_length,
3744 p_sys->b_broken_charset );
3746 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3747 pD->i_service_type, str1, str2 );
3749 vlc_meta_SetTitle( p_meta, str2 );
3750 vlc_meta_SetPublisher( p_meta, str1 );
3751 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3752 psz_type = ppsz_type[pD->i_service_type];
3758 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3760 static const char *ppsz_status[5] = {
3763 "Starts in a few seconds",
3767 psz_status = ppsz_status[p_srv->i_running_status];
3771 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3773 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3775 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3776 p_srv->i_service_id, p_meta );
3777 vlc_meta_Delete( p_meta );
3780 sdt->u.p_psi->i_version = p_sdt->i_version;
3781 dvbpsi_sdt_delete( p_sdt );
3784 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3785 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3787 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3791 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3792 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3795 /* Count the number of days */
3796 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3797 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3798 for( int i = 70; i < i_year; i++ )
3799 i_day += LEAP(1900+i);
3801 i_day += LEAP(1900+i_year);
3804 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3807 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3809 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3810 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3811 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3813 *p_y = 1900 + yp + c*1;
3814 *p_m = mp - 1 - c*12;
3815 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3817 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3818 static int64_t EITConvertStartTime( uint64_t i_date )
3820 const int i_mjd = i_date >> 24;
3821 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3822 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3823 const int i_second = CVT_FROM_BCD(i_date );
3828 /* if all 40 bits are 1, the start is unknown */
3829 if( i_date == UINT64_C(0xffffffffff) )
3832 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3833 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3835 static int EITConvertDuration( uint32_t i_duration )
3837 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3838 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3839 CVT_FROM_BCD(i_duration );
3843 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3845 demux_sys_t *p_sys = p_demux->p_sys;
3847 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3849 dvbpsi_tot_delete(p_tdt);
3853 static void EITCallBack( demux_t *p_demux,
3854 dvbpsi_eit_t *p_eit, bool b_current_following )
3856 demux_sys_t *p_sys = p_demux->p_sys;
3857 dvbpsi_eit_event_t *p_evt;
3860 msg_Dbg( p_demux, "EITCallBack called" );
3861 if( !p_eit->b_current_next )
3863 dvbpsi_eit_delete( p_eit );
3867 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3868 "ts_id=%d network_id=%d segment_last_section_number=%d "
3871 p_eit->i_version, p_eit->b_current_next,
3872 p_eit->i_ts_id, p_eit->i_network_id,
3873 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3875 p_epg = vlc_epg_New( NULL );
3876 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3878 dvbpsi_descriptor_t *p_dr;
3879 char *psz_name = NULL;
3880 char *psz_text = NULL;
3881 char *psz_extra = strdup("");
3885 int64_t i_tot_time = 0;
3887 i_start = EITConvertStartTime( p_evt->i_start_time );
3888 i_duration = EITConvertDuration( p_evt->i_duration );
3890 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3892 if( p_sys->i_tdt_delta == 0 )
3893 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3895 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3897 tzset(); // JST -> UTC
3898 i_start += timezone; // FIXME: what about DST?
3899 i_tot_time += timezone;
3901 if( p_evt->i_running_status == 0x00 &&
3902 (i_start - 5 < i_tot_time &&
3903 i_tot_time < i_start + i_duration + 5) )
3905 p_evt->i_running_status = 0x04;
3906 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3910 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3911 "running=%d free_ca=%d",
3912 p_evt->i_event_id, (int)i_start, (int)i_duration,
3913 p_evt->i_running_status, p_evt->b_free_ca );
3915 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3921 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3923 /* Only take first description, as we don't handle language-info
3925 if( pE && psz_name == NULL )
3927 psz_name = EITConvertToUTF8( p_demux,
3928 pE->i_event_name, pE->i_event_name_length,
3929 p_sys->b_broken_charset );
3931 psz_text = EITConvertToUTF8( p_demux,
3932 pE->i_text, pE->i_text_length,
3933 p_sys->b_broken_charset );
3934 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3935 pE->i_iso_639_code, psz_name, psz_text );
3942 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3945 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3947 pE->i_descriptor_number, pE->i_last_descriptor_number );
3949 if( pE->i_text_length > 0 )
3951 char *psz_text = EITConvertToUTF8( p_demux,
3952 pE->i_text, pE->i_text_length,
3953 p_sys->b_broken_charset );
3956 msg_Dbg( p_demux, " - text='%s'", psz_text );
3958 psz_extra = xrealloc( psz_extra,
3959 strlen(psz_extra) + strlen(psz_text) + 1 );
3960 strcat( psz_extra, psz_text );
3965 for( int i = 0; i < pE->i_entry_count; i++ )
3967 char *psz_dsc = EITConvertToUTF8( p_demux,
3968 pE->i_item_description[i],
3969 pE->i_item_description_length[i],
3970 p_sys->b_broken_charset );
3971 char *psz_itm = EITConvertToUTF8( p_demux,
3972 pE->i_item[i], pE->i_item_length[i],
3973 p_sys->b_broken_charset );
3975 if( psz_dsc && psz_itm )
3977 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3979 psz_extra = xrealloc( psz_extra,
3980 strlen(psz_extra) + strlen(psz_dsc) +
3981 strlen(psz_itm) + 3 + 1 );
3982 strcat( psz_extra, "(" );
3983 strcat( psz_extra, psz_dsc );
3984 strcat( psz_extra, " " );
3985 strcat( psz_extra, psz_itm );
3986 strcat( psz_extra, ")" );
3998 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
4001 for ( int i = 0; i < pR->i_ratings_number; i++ )
4003 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
4004 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
4006 if ( p_rating->i_rating + 3 > i_min_age )
4007 i_min_age = p_rating->i_rating + 3;
4008 msg_Dbg( p_demux, " - parental control set to %d years",
4017 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
4023 if( i_start > 0 && psz_name && psz_text)
4024 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
4025 *psz_extra ? psz_extra : NULL, i_min_age );
4027 /* Update "now playing" field */
4028 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
4029 vlc_epg_SetCurrent( p_epg, i_start );
4036 if( p_epg->i_event > 0 )
4038 if( b_current_following &&
4039 ( p_sys->programs.i_size == 0 ||
4040 p_sys->programs.p_elems[0] ==
4044 p_sys->i_dvb_length = 0;
4045 p_sys->i_dvb_start = 0;
4047 if( p_epg->p_current )
4049 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
4050 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
4053 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
4057 vlc_epg_Delete( p_epg );
4059 dvbpsi_eit_delete( p_eit );
4061 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4063 EITCallBack( p_demux, p_eit, true );
4065 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4067 EITCallBack( p_demux, p_eit, false );
4070 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
4071 uint16_t i_extension, demux_t *p_demux )
4073 demux_sys_t *p_sys = p_demux->p_sys;
4076 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4077 i_table_id, i_table_id, i_extension, i_extension );
4079 if( p_sys->pid[0].u.p_pat->i_version != -1 && i_table_id == 0x42 )
4081 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4082 i_table_id, i_table_id, i_extension, i_extension );
4084 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
4085 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
4087 else if( p_sys->pid[0x11].u.p_psi->i_version != -1 &&
4088 ( i_table_id == 0x4e || /* Current/Following */
4089 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
4091 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4092 i_table_id, i_table_id, i_extension, i_extension );
4094 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
4095 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
4096 (dvbpsi_eit_callback)EITCallBackSchedule;
4098 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
4099 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
4101 else if( p_demux->p_sys->pid[0x11].u.p_psi->i_version != -1 &&
4102 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
4104 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4105 i_table_id, i_table_id, i_extension, i_extension );
4107 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
4108 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
4112 /*****************************************************************************
4113 * PMT callback and helpers
4114 *****************************************************************************/
4115 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
4118 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
4119 while( p_dr && ( p_dr->i_tag != i_tag ) )
4120 p_dr = p_dr->p_next;
4123 static bool PMTEsHasRegistration( demux_t *p_demux,
4124 const dvbpsi_pmt_es_t *p_es,
4125 const char *psz_tag )
4127 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
4131 if( p_dr->i_length < 4 )
4133 msg_Warn( p_demux, "invalid Registration Descriptor" );
4137 assert( strlen(psz_tag) == 4 );
4138 return !memcmp( p_dr->p_data, psz_tag, 4 );
4141 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
4142 int i_component_tag )
4144 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4147 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4151 return p_si->i_component_tag == i_component_tag;
4154 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pes_es_t *p_es,
4155 const ts_pmt_t *p_pmt, const dvbpsi_pmt_es_t *p_dvbpsies )
4157 es_format_t *p_fmt = &p_es->fmt;
4159 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
4160 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x1f );
4162 if( p_dr && p_dr->i_length == 2 )
4164 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
4166 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
4168 p_es->p_mpeg4desc = NULL;
4170 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
4172 iod_descriptor_t *iod = p_pmt->iod;
4173 if( iod->es_descr[i].i_es_id == i_es_id )
4175 if ( iod->es_descr[i].b_ok )
4176 p_es->p_mpeg4desc = &iod->es_descr[i];
4178 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
4183 if( !p_es->p_mpeg4desc )
4185 switch( p_dvbpsies->i_type )
4187 /* non fatal, set by packetizer */
4188 case 0x0f: /* ADTS */
4189 case 0x11: /* LOAS */
4190 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4191 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4194 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4195 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4201 const decoder_config_descriptor_t *dcd = &p_es->p_mpeg4desc->dec_descr;
4202 if( dcd->i_streamType == 0x04 ) /* VisualStream */
4204 p_fmt->i_cat = VIDEO_ES;
4205 switch( dcd->i_objectTypeIndication )
4207 case 0x0B: /* mpeg4 sub */
4208 p_fmt->i_cat = SPU_ES;
4209 p_fmt->i_codec = VLC_CODEC_SUBT;
4212 case 0x20: /* mpeg4 */
4213 p_fmt->i_codec = VLC_CODEC_MP4V;
4215 case 0x21: /* h264 */
4216 p_fmt->i_codec = VLC_CODEC_H264;
4223 case 0x65: /* mpeg2 */
4224 p_fmt->i_codec = VLC_CODEC_MPGV;
4226 case 0x6a: /* mpeg1 */
4227 p_fmt->i_codec = VLC_CODEC_MPGV;
4229 case 0x6c: /* mpeg1 */
4230 p_fmt->i_codec = VLC_CODEC_JPEG;
4233 p_fmt->i_cat = UNKNOWN_ES;
4237 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
4239 p_fmt->i_cat = AUDIO_ES;
4240 switch( dcd->i_objectTypeIndication )
4242 case 0x40: /* mpeg4 */
4243 p_fmt->i_codec = VLC_CODEC_MP4A;
4247 case 0x68: /* mpeg2 aac */
4248 p_fmt->i_codec = VLC_CODEC_MP4A;
4250 case 0x69: /* mpeg2 */
4251 p_fmt->i_codec = VLC_CODEC_MPGA;
4253 case 0x6b: /* mpeg1 */
4254 p_fmt->i_codec = VLC_CODEC_MPGA;
4257 p_fmt->i_cat = UNKNOWN_ES;
4263 p_fmt->i_cat = UNKNOWN_ES;
4266 if( p_fmt->i_cat != UNKNOWN_ES )
4268 p_fmt->i_extra = dcd->i_extra;
4269 if( p_fmt->i_extra > 0 )
4271 p_fmt->p_extra = malloc( p_fmt->i_extra );
4272 if( p_fmt->p_extra )
4273 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
4286 } ts_teletext_page_t;
4288 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pes_t *p_pes,
4289 const dvbpsi_pmt_es_t *p_dvbpsies )
4291 es_format_t *p_fmt = &p_pes->es.fmt;
4293 ts_teletext_page_t p_page[2 * 64 + 20];
4294 unsigned i_page = 0;
4296 /* Gather pages information */
4297 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
4299 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, i_tag_idx == 0 ? 0x46 : 0x56 );
4303 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
4307 for( int i = 0; i < p_sub->i_pages_number; i++ )
4309 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
4311 if( p_src->i_teletext_type >= 0x06 )
4314 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4316 ts_teletext_page_t *p_dst = &p_page[i_page++];
4318 p_dst->i_type = p_src->i_teletext_type;
4319 p_dst->i_magazine = p_src->i_teletext_magazine_number
4320 ? p_src->i_teletext_magazine_number : 8;
4321 p_dst->i_page = p_src->i_teletext_page_number;
4322 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4326 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4329 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4330 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4332 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
4334 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
4337 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4339 ts_teletext_page_t *p_dst = &p_page[i_page++];
4341 switch( p_src->i_subtitling_type )
4344 p_dst->i_type = 0x02;
4347 p_dst->i_type = 0x03;
4350 /* FIXME check if it is the right split */
4351 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
4352 ? (p_src->i_composition_page_id >> 8) : 8;
4353 p_dst->i_page = p_src->i_composition_page_id & 0xff;
4354 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4359 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
4361 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4363 p_fmt->subs.teletext.i_magazine = -1;
4364 p_fmt->subs.teletext.i_page = 0;
4365 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
4367 dvbpsi_descriptor_t *p_dr;
4368 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x46 );
4370 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x56 );
4372 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4374 /* Descriptor pass-through */
4375 p_fmt->p_extra = malloc( p_dr->i_length );
4376 if( p_fmt->p_extra )
4378 p_fmt->i_extra = p_dr->i_length;
4379 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4385 for( unsigned i = 0; i < i_page; i++ )
4387 ts_pes_es_t *p_page_es;
4392 p_page_es = &p_pes->es;
4396 p_page_es = calloc( 1, sizeof(*p_page_es) );
4400 es_format_Copy( &p_page_es->fmt, p_fmt );
4401 free( p_page_es->fmt.psz_language );
4402 free( p_page_es->fmt.psz_description );
4403 p_page_es->fmt.psz_language = NULL;
4404 p_page_es->fmt.psz_description = NULL;
4406 ARRAY_APPEND( p_pes->extra_es, p_page_es );
4410 const ts_teletext_page_t *p = &p_page[i];
4411 p_page_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
4412 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
4413 p_page_es->fmt.psz_language = strndup( p->p_iso639, 3 );
4414 p_page_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
4415 p_page_es->fmt.subs.teletext.i_magazine = p->i_magazine;
4416 p_page_es->fmt.subs.teletext.i_page = p->i_page;
4419 " * ttxt type=%s lan=%s page=%d%02x",
4420 p_page_es->fmt.psz_description,
4421 p_page_es->fmt.psz_language,
4422 p->i_magazine, p->i_page );
4426 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pes_t *p_pes,
4427 const dvbpsi_pmt_es_t *p_dvbpsies )
4429 es_format_t *p_fmt = &p_pes->es.fmt;
4431 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
4433 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4435 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4436 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4438 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
4439 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
4440 ( i_type >= 0x20 && i_type <= 0x24 ) )
4444 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4446 p_fmt->subs.dvb.i_id = -1;
4447 p_fmt->psz_description = strdup( _("DVB subtitles") );
4449 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4451 /* Descriptor pass-through */
4452 p_fmt->p_extra = malloc( p_dr->i_length );
4453 if( p_fmt->p_extra )
4455 p_fmt->i_extra = p_dr->i_length;
4456 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4462 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4464 ts_pes_es_t *p_subs_es;
4469 p_subs_es = &p_pes->es;
4473 p_subs_es = malloc( sizeof(*p_subs_es) );
4477 es_format_Copy( &p_subs_es->fmt, p_fmt );
4478 free( p_subs_es->fmt.psz_language );
4479 free( p_subs_es->fmt.psz_description );
4480 p_subs_es->fmt.psz_language = NULL;
4481 p_subs_es->fmt.psz_description = NULL;
4483 ARRAY_APPEND( p_pes->extra_es, p_subs_es );
4487 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
4488 p_subs_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
4489 switch( p->i_subtitling_type )
4491 case 0x10: /* unspec. */
4492 case 0x11: /* 4:3 */
4493 case 0x12: /* 16:9 */
4494 case 0x13: /* 2.21:1 */
4495 case 0x14: /* HD monitor */
4496 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles") );
4498 case 0x20: /* Hearing impaired unspec. */
4499 case 0x21: /* h.i. 4:3 */
4500 case 0x22: /* h.i. 16:9 */
4501 case 0x23: /* h.i. 2.21:1 */
4502 case 0x24: /* h.i. HD monitor */
4503 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
4510 p_subs_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
4511 ( p->i_ancillary_page_id << 16 );
4516 static int vlc_ceil_log2( const unsigned int val )
4518 int n = 31 - clz(val);
4519 if ((1U << n) != val)
4525 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
4529 /* default mapping */
4530 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
4531 memcpy(h.stream_map, map, sizeof(map));
4535 int stream_count = 0;
4536 int ccc = p[1]; // channel_config_code
4540 mapping = channels > 2;
4543 channels = 2; // dual mono
4545 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
4546 csc = p_csc[channels - 1];
4547 stream_count = channels - csc;
4549 static const uint8_t map[6][7] = {
4558 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4559 } else if (ccc == 0x81) {
4561 goto explicit_config_too_short;
4568 bs_init(&s, &p[4], len - 4);
4571 int bits = vlc_ceil_log2(channels);
4572 if (s.i_left < bits)
4573 goto explicit_config_too_short;
4574 stream_count = bs_read(&s, bits) + 1;
4575 bits = vlc_ceil_log2(stream_count + 1);
4576 if (s.i_left < bits)
4577 goto explicit_config_too_short;
4578 csc = bs_read(&s, bits);
4580 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4581 if (s.i_left < channels * channel_bits)
4582 goto explicit_config_too_short;
4584 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4585 for (int i = 0; i < channels; i++) {
4586 unsigned char m = bs_read(&s, channel_bits);
4589 h.stream_map[i] = m;
4592 } else if (ccc >= 0x80 && ccc <= 0x88) {
4593 channels = ccc - 0x80;
4598 channels = 2; // dual mono
4601 stream_count = channels;
4603 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4607 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4611 opus_prepare_header(channels, 0, &h);
4613 h.input_sample_rate = 48000;
4615 h.nb_streams = channels - csc;
4616 h.channel_mapping = mapping;
4619 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4620 if (p_fmt->p_extra) {
4621 p_fmt->i_cat = AUDIO_ES;
4622 p_fmt->i_codec = VLC_CODEC_OPUS;
4623 p_fmt->audio.i_channels = h.channels;
4624 p_fmt->audio.i_rate = 48000;
4630 explicit_config_too_short:
4631 msg_Err(demux, "Opus descriptor too short");
4634 static void PMTSetupEs0x06( demux_t *p_demux, ts_pes_t *p_pes,
4635 const dvbpsi_pmt_es_t *p_dvbpsies )
4637 es_format_t *p_fmt = &p_pes->es.fmt;
4638 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4639 dvbpsi_descriptor_t *desc;
4641 if( PMTEsHasRegistration( p_demux, p_dvbpsies, "AC-3" ) ||
4642 PMTEsFindDescriptor( p_dvbpsies, 0x6a ) ||
4643 PMTEsFindDescriptor( p_dvbpsies, 0x81 ) )
4645 p_fmt->i_cat = AUDIO_ES;
4646 p_fmt->i_codec = VLC_CODEC_A52;
4648 else if( (desc = PMTEsFindDescriptor( p_dvbpsies, 0x7f ) ) && desc->i_length >= 2 &&
4649 PMTEsHasRegistration(p_demux, p_dvbpsies, "Opus"))
4651 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4653 else if( PMTEsFindDescriptor( p_dvbpsies, 0x7a ) )
4655 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4656 p_fmt->i_cat = AUDIO_ES;
4657 p_fmt->i_codec = VLC_CODEC_EAC3;
4659 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS1" ) ||
4660 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS2" ) ||
4661 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS3" ) ||
4662 PMTEsFindDescriptor( p_dvbpsies, 0x73 ) )
4664 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4665 p_fmt->i_cat = AUDIO_ES;
4666 p_fmt->i_codec = VLC_CODEC_DTS;
4668 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "BSSD" ) && !p_subs_dr )
4670 /* BSSD is AES3 DATA, but could also be subtitles
4671 * we need to check for secondary descriptor then s*/
4672 p_fmt->i_cat = AUDIO_ES;
4673 p_fmt->b_packetized = true;
4674 p_fmt->i_codec = VLC_CODEC_302M;
4676 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "HEVC" ) )
4678 p_fmt->i_cat = VIDEO_ES;
4679 p_fmt->i_codec = VLC_CODEC_HEVC;
4681 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4683 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4684 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xFD );
4685 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4686 if ( p_dr && p_dr->i_length >= 2 )
4688 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4689 PMTEsHasComponentTag( p_dvbpsies, 0x30 ) ||
4690 PMTEsHasComponentTag( p_dvbpsies, 0x31 ) ||
4691 PMTEsHasComponentTag( p_dvbpsies, 0x32 ) ||
4692 PMTEsHasComponentTag( p_dvbpsies, 0x33 ) ||
4693 PMTEsHasComponentTag( p_dvbpsies, 0x34 ) ||
4694 PMTEsHasComponentTag( p_dvbpsies, 0x35 ) ||
4695 PMTEsHasComponentTag( p_dvbpsies, 0x36 ) ||
4696 PMTEsHasComponentTag( p_dvbpsies, 0x37 ) ) )
4698 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_A );
4699 p_fmt->psz_language = strndup ( "jpn", 3 );
4700 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4702 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4703 PMTEsHasComponentTag( p_dvbpsies, 0x87 ) ||
4704 PMTEsHasComponentTag( p_dvbpsies, 0x88 ) ) )
4706 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_C );
4707 p_fmt->psz_language = strndup ( "jpn", 3 );
4708 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4714 /* Subtitle/Teletext/VBI fallbacks */
4715 dvbpsi_subtitling_dr_t *p_sub;
4716 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4718 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4720 if( p_fmt->i_cat != UNKNOWN_ES )
4723 switch( p_sub->p_subtitle[i].i_subtitling_type )
4725 case 0x01: /* EBU Teletext subtitles */
4726 case 0x02: /* Associated EBU Teletext */
4727 case 0x03: /* VBI data */
4728 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4730 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4731 case 0x11: /* ... on 4:3 AR monitor */
4732 case 0x12: /* ... on 16:9 AR monitor */
4733 case 0x13: /* ... on 2.21:1 AR monitor */
4734 case 0x14: /* ... for display on a high definition monitor */
4735 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4736 case 0x21: /* ... on 4:3 AR monitor */
4737 case 0x22: /* ... on 16:9 AR monitor */
4738 case 0x23: /* ... on 2.21:1 AR monitor */
4739 case 0x24: /* ... for display on a high definition monitor */
4740 PMTSetupEsDvbSubtitle( p_demux, p_pes, p_dvbpsies );
4743 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4744 p_sub->p_subtitle[i].i_subtitling_type );
4750 if( p_fmt->i_cat == UNKNOWN_ES &&
4751 ( PMTEsFindDescriptor( p_dvbpsies, 0x45 ) || /* VBI Data descriptor */
4752 PMTEsFindDescriptor( p_dvbpsies, 0x46 ) || /* VBI Teletext descriptor */
4753 PMTEsFindDescriptor( p_dvbpsies, 0x56 ) ) ) /* EBU Teletext descriptor */
4756 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4760 /* FIXME is it useful ? */
4761 if( PMTEsFindDescriptor( p_dvbpsies, 0x52 ) )
4763 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x52 );
4764 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4766 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4770 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pes_es_t *p_es,
4771 const dvbpsi_pmt_es_t *p_dvbpsies )
4773 /* Registration Descriptor */
4774 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "VC-1" ) )
4776 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4780 es_format_t *p_fmt = &p_es->fmt;
4782 /* registration descriptor for VC-1 (SMPTE rp227) */
4783 p_fmt->i_cat = VIDEO_ES;
4784 p_fmt->i_codec = VLC_CODEC_VC1;
4786 /* XXX With Simple and Main profile the SEQUENCE
4787 * header is modified: video width and height are
4788 * inserted just after the start code as 2 int16_t
4789 * The packetizer will take care of that. */
4792 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pes_es_t *p_es,
4793 const dvbpsi_pmt_es_t *p_dvbpsies )
4795 /* Registration Descriptor */
4796 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "drac" ) )
4798 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4802 es_format_t *p_fmt = &p_es->fmt;
4804 /* registration descriptor for Dirac
4805 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4806 p_fmt->i_cat = VIDEO_ES;
4807 p_fmt->i_codec = VLC_CODEC_DIRAC;
4810 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pes_es_t *p_es,
4811 const dvbpsi_pmt_es_t *p_dvbpsies )
4813 /* MSCODEC sent by vlc */
4814 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xa0 );
4815 if( !p_dr || p_dr->i_length < 10 )
4818 "private MSCODEC (vlc) without bih private descriptor" );
4822 es_format_t *p_fmt = &p_es->fmt;
4823 p_fmt->i_cat = VIDEO_ES;
4824 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4825 p_dr->p_data[2], p_dr->p_data[3] );
4826 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4827 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4828 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4830 if( p_fmt->i_extra > 0 )
4832 p_fmt->p_extra = malloc( p_fmt->i_extra );
4833 if( p_fmt->p_extra )
4834 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4835 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4839 /* For such stream we will gather them ourself and don't launch a
4841 * Yes it's ugly but it's the only way to have DIV3 working */
4842 p_fmt->b_packetized = true;
4845 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pes_es_t *p_es, int i_pid )
4847 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4848 * with audio track pid being 0x1100..0x11FF */
4849 if ( p_pmt->i_program_number == 0x1 &&
4850 p_pmt->i_pcr_pid == 0x1000 &&
4851 ( i_pid >> 8 ) == 0x11 )
4853 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4854 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4857 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4860 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pes_es_t *p_es,
4861 const dvbpsi_pmt_es_t *p_dvbpsies )
4863 es_format_t *p_fmt = &p_es->fmt;
4865 /* Blu-Ray mapping */
4866 switch( p_dvbpsies->i_type )
4869 p_fmt->i_cat = AUDIO_ES;
4870 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4873 p_fmt->i_cat = AUDIO_ES;
4874 p_fmt->i_codec = VLC_CODEC_A52;
4877 case 0x85: /* DTS-HD High resolution audio */
4878 case 0x86: /* DTS-HD Master audio */
4879 case 0xA2: /* Secondary DTS audio */
4880 p_fmt->i_cat = AUDIO_ES;
4881 p_fmt->i_codec = VLC_CODEC_DTS;
4884 case 0x83: /* TrueHD AC3 */
4885 p_fmt->i_cat = AUDIO_ES;
4886 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4889 case 0x84: /* E-AC3 */
4890 case 0xA1: /* Secondary E-AC3 */
4891 p_fmt->i_cat = AUDIO_ES;
4892 p_fmt->i_codec = VLC_CODEC_EAC3;
4894 case 0x90: /* Presentation graphics */
4895 p_fmt->i_cat = SPU_ES;
4896 p_fmt->i_codec = VLC_CODEC_BD_PG;
4898 case 0x91: /* Interactive graphics */
4899 case 0x92: /* Subtitle */
4902 p_fmt->i_cat = VIDEO_ES;
4903 p_fmt->i_codec = VLC_CODEC_VC1;
4906 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4907 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4914 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pes_es_t *p_es,
4915 const dvbpsi_pmt_es_t *p_dvbpsies )
4921 vlc_fourcc_t i_codec;
4923 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4924 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4925 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4926 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4927 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4928 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4929 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4930 { "", UNKNOWN_ES, 0 }
4932 es_format_t *p_fmt = &p_es->fmt;
4934 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4936 if( PMTEsHasRegistration( p_demux, p_dvbpsies, p_regs[i].psz_tag ) )
4938 p_fmt->i_cat = p_regs[i].i_cat;
4939 p_fmt->i_codec = p_regs[i].i_codec;
4940 if (p_dvbpsies->i_type == 0x87)
4941 p_fmt->i_codec = VLC_CODEC_EAC3;
4948 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4950 static const char *audio_type[] = {
4952 N_("clean effects"),
4953 N_("hearing impaired"),
4954 N_("visual impaired commentary"),
4957 if (type < 0 || type > 3)
4958 msg_Dbg( p_demux, "unknown audio type: %d", type);
4960 return strdup(audio_type[type]);
4964 static void PMTParseEsIso639( demux_t *p_demux, ts_pes_es_t *p_es,
4965 const dvbpsi_pmt_es_t *p_dvbpsies )
4967 /* get language descriptor */
4968 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x0a );
4973 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4976 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4980 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4981 p_es->fmt.psz_language = malloc( 4 );
4982 if( p_es->fmt.psz_language )
4984 memcpy( p_es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
4985 p_es->fmt.psz_language[3] = 0;
4986 msg_Dbg( p_demux, "found language: %s", p_es->fmt.psz_language);
4988 int type = p_decoded->code[0].i_audio_type;
4989 p_es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
4991 p_es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
4993 p_es->fmt.i_extra_languages = p_decoded->i_code_count-1;
4994 if( p_es->fmt.i_extra_languages > 0 )
4995 p_es->fmt.p_extra_languages =
4996 malloc( sizeof(*p_es->fmt.p_extra_languages) *
4997 p_es->fmt.i_extra_languages );
4998 if( p_es->fmt.p_extra_languages )
5000 for( unsigned i = 0; i < p_es->fmt.i_extra_languages; i++ )
5002 p_es->fmt.p_extra_languages[i].psz_language = malloc(4);
5003 if( p_es->fmt.p_extra_languages[i].psz_language )
5005 memcpy( p_es->fmt.p_extra_languages[i].psz_language,
5006 p_decoded->code[i+1].iso_639_code, 3 );
5007 p_es->fmt.p_extra_languages[i].psz_language[3] = '\0';
5009 int type = p_decoded->code[i].i_audio_type;
5010 p_es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
5014 p_es->fmt.psz_language = malloc( 4 );
5015 if( p_es->fmt.psz_language )
5017 memcpy( p_es->fmt.psz_language,
5018 p_decoded->i_iso_639_code, 3 );
5019 p_es->fmt.psz_language[3] = 0;
5024 static inline void SetExtraESGroupAndID( demux_sys_t *p_sys, es_format_t *p_fmt,
5025 const es_format_t *p_parent_fmt )
5027 if ( p_sys->b_es_id_pid ) /* pid is 13 bits */
5028 p_fmt->i_id = (p_sys->i_next_extraid++ << 13) | p_parent_fmt->i_id;
5029 p_fmt->i_group = p_parent_fmt->i_group;
5032 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid, bool b_create_delayed )
5034 demux_sys_t *p_sys = p_demux->p_sys;
5036 if( b_create_delayed )
5037 p_sys->es_creation = CREATE_ES;
5039 if( pid && p_sys->es_creation == CREATE_ES )
5041 /* FIXME: other owners / shared pid */
5042 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
5043 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
5045 es_format_t *p_fmt = &pid->u.p_pes->extra_es.p_elems[i]->fmt;
5046 SetExtraESGroupAndID( p_sys, p_fmt, &pid->u.p_pes->es.fmt );
5047 pid->u.p_pes->extra_es.p_elems[i]->id = es_out_Add( p_demux->out, p_fmt );
5049 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
5051 /* Update the default program == first created ES group */
5052 if( p_sys->b_default_selection )
5054 p_sys->b_default_selection = false;
5055 assert(p_sys->programs.i_size == 1);
5056 if( p_sys->programs.p_elems[0] != pid->p_parent->u.p_pmt->i_number )
5057 p_sys->programs.p_elems[0] = pid->p_parent->u.p_pmt->i_number;
5058 msg_Dbg( p_demux, "Default program is %d", pid->p_parent->u.p_pmt->i_number );
5062 if( b_create_delayed )
5064 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5065 for( int i=0; i< p_pat->programs.i_size; i++ )
5067 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
5068 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
5070 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
5071 if( pid->u.p_pes->es.id )
5074 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
5075 for( int k = 0; k < pid->u.p_pes->extra_es.i_size; k++ )
5077 es_format_t *p_fmt = &pid->u.p_pes->extra_es.p_elems[k]->fmt;
5078 SetExtraESGroupAndID( p_sys, p_fmt, &pid->u.p_pes->es.fmt );
5079 pid->u.p_pes->extra_es.p_elems[k]->id = es_out_Add( p_demux->out, p_fmt );
5081 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
5086 UpdatePESFilters( p_demux, p_sys->b_es_all );
5089 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_dvbpsipmt )
5091 demux_t *p_demux = data;
5092 demux_sys_t *p_sys = p_demux->p_sys;
5094 ts_pid_t *pmtpid = NULL;
5095 ts_pmt_t *p_pmt = NULL;
5097 msg_Dbg( p_demux, "PMTCallBack called" );
5099 if (unlikely(p_sys->pid[0].type != TYPE_PAT))
5101 assert(p_sys->pid[0].type == TYPE_PAT);
5102 dvbpsi_pmt_delete(p_dvbpsipmt);
5105 const ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5107 /* First find this PMT declared in PAT */
5108 for( int i = 0; !pmtpid && i < p_pat->programs.i_size; i++ )
5110 const int i_pmt_prgnumber = p_pat->programs.p_elems[i]->u.p_pmt->i_number;
5111 if( i_pmt_prgnumber != TS_USER_PMT_NUMBER &&
5112 i_pmt_prgnumber == p_dvbpsipmt->i_program_number )
5114 pmtpid = p_pat->programs.p_elems[i];
5115 assert(pmtpid->type == TYPE_PMT);
5116 p_pmt = pmtpid->u.p_pmt;
5120 if( pmtpid == NULL )
5122 msg_Warn( p_demux, "unreferenced program (broken stream)" );
5123 dvbpsi_pmt_delete(p_dvbpsipmt);
5127 pmtpid->i_flags |= FLAG_SEEN;
5129 if( p_pmt->i_version != -1 &&
5130 ( !p_dvbpsipmt->b_current_next || p_pmt->i_version == p_dvbpsipmt->i_version ) )
5132 dvbpsi_pmt_delete( p_dvbpsipmt );
5136 /* Save old es array */
5137 DECL_ARRAY(ts_pid_t *) old_es_rm;
5138 old_es_rm.i_alloc = p_pmt->e_streams.i_alloc;
5139 old_es_rm.i_size = p_pmt->e_streams.i_size;
5140 old_es_rm.p_elems = p_pmt->e_streams.p_elems;
5141 ARRAY_INIT(p_pmt->e_streams);
5145 IODFree( p_pmt->iod );
5149 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
5150 p_dvbpsipmt->i_program_number, p_dvbpsipmt->i_version, p_dvbpsipmt->i_pcr_pid );
5151 p_pmt->i_pid_pcr = p_dvbpsipmt->i_pcr_pid;
5152 p_pmt->i_version = p_dvbpsipmt->i_version;
5154 ValidateDVBMeta( p_demux, p_pmt->i_pid_pcr );
5156 if( ProgramIsSelected( p_sys, p_pmt->i_number ) )
5157 SetPIDFilter( p_sys, &p_sys->pid[p_pmt->i_pid_pcr], true ); /* Set demux filter */
5159 /* Parse PMT descriptors */
5160 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
5161 dvbpsi_descriptor_t *p_dr;
5163 /* First pass for standard detection */
5164 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
5166 int i_arib_flags = 0; /* Descriptors can be repeated */
5167 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5173 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
5174 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
5178 i_arib_flags |= 1 << 1;
5181 i_arib_flags |= 1 << 2;
5187 if ( i_arib_flags == 0x07 ) //0b111
5188 p_sys->arib.e_mode = ARIBMODE_ENABLED;
5191 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5193 /* special descriptors handling */
5196 case 0x1d: /* We have found an IOD descriptor */
5197 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
5198 p_pmt->iod = IODNew( p_dr->i_length, p_dr->p_data );
5202 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5203 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5206 case 0x5: /* Registration Descriptor */
5207 if( p_dr->i_length != 4 )
5209 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
5213 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
5214 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
5215 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
5220 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
5224 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
5227 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
5228 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
5229 /* TODO: apply visibility flags */
5233 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
5237 dvbpsi_pmt_es_t *p_dvbpsies;
5238 for( p_dvbpsies = p_dvbpsipmt->p_first_es; p_dvbpsies != NULL; p_dvbpsies = p_dvbpsies->p_next )
5240 bool b_reusing_pid = false;
5243 ts_pid_t *pespid = &p_sys->pid[p_dvbpsies->i_pid];
5244 if ( pespid->type == TYPE_PES && pespid->p_parent->u.p_pmt->i_number != p_pmt->i_number )
5246 msg_Warn( p_demux, " * PMT wants to get a share or pid %d (unsupported)", pespid->i_pid );
5250 /* Find out if the PID was already declared */
5251 for( int i = 0; i < old_es_rm.i_size; i++ )
5253 if( old_es_rm.p_elems[i]->i_pid == p_dvbpsies->i_pid )
5255 b_reusing_pid = true;
5259 ValidateDVBMeta( p_demux, p_dvbpsies->i_pid );
5261 char const * psz_typedesc = "";
5262 switch(p_dvbpsies->i_type)
5265 psz_typedesc = "ISO/IEC Reserved";
5268 psz_typedesc = "ISO/IEC 11172 Video";
5271 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
5274 psz_typedesc = "ISO/IEC 11172 Audio";
5277 psz_typedesc = "ISO/IEC 13818-3 Audio";
5280 psz_typedesc = "ISO/IEC 13818-1 private_sections";
5283 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
5286 psz_typedesc = "ISO/IEC 13522 MHEG";
5289 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
5292 psz_typedesc = "ITU-T Rec. H.222.1";
5295 psz_typedesc = "ISO/IEC 13818-6 type A";
5298 psz_typedesc = "ISO/IEC 13818-6 type B";
5301 psz_typedesc = "ISO/IEC 13818-6 type C";
5304 psz_typedesc = "ISO/IEC 13818-6 type D";
5307 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
5310 if (p_dvbpsies->i_type >= 0x0F && p_dvbpsies->i_type <=0x7F)
5311 psz_typedesc = "ISO/IEC 13818-1 Reserved";
5313 psz_typedesc = "User Private";
5316 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
5317 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5319 for( p_dr = p_dvbpsies->p_first_descriptor; p_dr != NULL;
5320 p_dr = p_dr->p_next )
5322 msg_Dbg( p_demux, " - descriptor tag 0x%x",
5326 if ( !PIDSetup( p_demux, TYPE_PES, pespid, pmtpid ) )
5328 msg_Warn( p_demux, " * pid=%d type=0x%x %s (skipped)",
5329 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5336 p_pes = ts_pes_New( p_demux );
5342 p_pes = pespid->u.p_pes;
5346 ARRAY_APPEND( p_pmt->e_streams, pespid );
5348 PIDFillFormat( &p_pes->es.fmt, p_dvbpsies->i_type );
5350 pespid->i_flags |= SEEN(p_sys->pid[p_dvbpsies->i_pid]);
5352 bool b_registration_applied = false;
5353 if ( p_dvbpsies->i_type >= 0x80 ) /* non standard, extensions */
5355 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
5357 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, &p_pes->es, p_dvbpsies ) ))
5358 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
5359 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5363 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, &p_pes->es, p_dvbpsies ) ))
5364 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
5365 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5369 if ( !b_registration_applied )
5371 switch( p_dvbpsies->i_type )
5374 /* Handle PES private data */
5375 PMTSetupEs0x06( p_demux, p_pes, p_dvbpsies );
5377 /* All other private or reserved types */
5382 PMTSetupEsISO14496( p_demux, &p_pes->es, p_pmt, p_dvbpsies );
5386 PMTSetupEs0x83( p_dvbpsipmt, &p_pes->es, p_dvbpsies->i_pid );
5389 PMTSetupEs0xA0( p_demux, &p_pes->es, p_dvbpsies );
5392 PMTSetupEs0xD1( p_demux, &p_pes->es, p_dvbpsies );
5395 PMTSetupEs0xEA( p_demux, &p_pes->es, p_dvbpsies );
5401 if( p_pes->es.fmt.i_cat == AUDIO_ES ||
5402 ( p_pes->es.fmt.i_cat == SPU_ES &&
5403 p_pes->es.fmt.i_codec != VLC_CODEC_DVBS &&
5404 p_pes->es.fmt.i_codec != VLC_CODEC_TELETEXT ) )
5406 PMTParseEsIso639( p_demux, &p_pes->es, p_dvbpsies );
5409 switch( p_pes->es.fmt.i_codec )
5411 case VLC_CODEC_SCTE_27:
5412 p_pes->data_type = TS_ES_DATA_TABLE_SECTION;
5415 //pid->es->data_type = TS_ES_DATA_PES;
5419 /* Set Groups / ID */
5420 p_pes->es.fmt.i_group = p_dvbpsipmt->i_program_number;
5421 if( p_sys->b_es_id_pid )
5422 p_pes->es.fmt.i_id = p_dvbpsies->i_pid;
5424 if( p_pes->es.fmt.i_cat == UNKNOWN_ES )
5426 msg_Dbg( p_demux, " => pid %d content is *unknown*",
5427 p_dvbpsies->i_pid );
5428 p_pes->es.fmt.psz_description = strdup( psz_typedesc );
5432 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
5433 p_dvbpsies->i_pid, (char*)&p_pes->es.fmt.i_codec );
5435 /* Check if we can avoid restarting the ES */
5438 /* p_pes points to a tmp pes */
5439 if( pespid->u.p_pes->es.fmt.i_codec != p_pes->es.fmt.i_codec ||
5440 pespid->u.p_pes->es.fmt.i_extra != p_pes->es.fmt.i_extra ||
5441 pespid->u.p_pes->es.fmt.i_extra != 0 ||
5442 pespid->u.p_pes->extra_es.i_size != p_pes->extra_es.i_size ||
5443 !( ( !pespid->u.p_pes->es.fmt.psz_language &&
5444 !p_pes->es.fmt.psz_language ) ||
5445 ( pespid->u.p_pes->es.fmt.psz_language &&
5446 p_pes->es.fmt.psz_language &&
5447 !strcmp( pespid->u.p_pes->es.fmt.psz_language,
5448 p_pes->es.fmt.psz_language ) ) ) )
5450 /* Differs, swap then */
5451 ts_pes_t *e = pespid->u.p_pes;
5452 pespid->u.p_pes = p_pes;
5455 /* p_pes still tmp, but now contains old config */
5456 pespid->u.p_pes->es.id = p_pes->es.id;
5457 if( pespid->u.p_pes->es.id )
5459 p_pes->es.id = NULL;
5460 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5461 pespid->u.p_pes->es.id, pespid->u.p_pes->es.fmt );
5464 for( int i=0; i<pespid->u.p_pes->extra_es.i_size &&
5465 i<p_pes->extra_es.i_size; i++ )
5467 pespid->u.p_pes->extra_es.p_elems[i]->id = p_pes->extra_es.p_elems[i]->id;
5468 if( pespid->u.p_pes->extra_es.p_elems[i]->id )
5470 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5471 pespid->u.p_pes->extra_es.p_elems[i]->id,
5472 pespid->u.p_pes->extra_es.p_elems[i]->fmt );
5473 p_pes->extra_es.p_elems[i]->id = NULL;
5478 ts_pes_Del( p_demux, p_pes ); // delete temp
5482 AddAndCreateES( p_demux, pespid, false );
5486 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x09 );
5487 if( p_dr && p_dr->i_length >= 2 )
5489 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5490 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5494 /* Set CAM descrambling */
5495 if( !ProgramIsSelected( p_sys, p_pmt->i_number ) )
5497 dvbpsi_pmt_delete( p_dvbpsipmt );
5499 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
5500 p_dvbpsipmt ) != VLC_SUCCESS )
5502 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED && !p_sys->arib.b25stream )
5504 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
5505 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
5506 if (!p_sys->arib.b25stream)
5507 dvbpsi_pmt_delete( p_dvbpsipmt );
5508 } else dvbpsi_pmt_delete( p_dvbpsipmt );
5511 /* Decref or clean now unused es */
5512 for( int i = 0; i < old_es_rm.i_size; i++ )
5513 PIDRelease( p_demux, old_es_rm.p_elems[i] );
5514 ARRAY_RESET( old_es_rm );
5516 UpdatePESFilters( p_demux, p_sys->b_es_all );
5518 if( !p_sys->b_trust_pcr )
5520 int i_cand = FindPCRCandidate( p_pmt );
5521 p_pmt->i_pid_pcr = i_cand;
5522 p_pmt->pcr.b_disable = true;
5523 msg_Warn( p_demux, "PCR not trusted for program %d, set up workaround using pid %d",
5524 p_pmt->i_number, i_cand );
5527 /* Probe Boundaries */
5528 if( p_sys->b_canfastseek && p_pmt->i_last_dts == -1 )
5530 p_pmt->i_last_dts = 0;
5531 ProbeStart( p_demux, p_pmt->i_number );
5532 ProbeEnd( p_demux, p_pmt->i_number );
5536 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
5538 /* Some Dreambox streams have all PMT set to same pid */
5539 int i_prev_pid = -1;
5540 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
5542 p_program = p_program->p_next )
5544 if( p_program->i_pid == i_prev_pid )
5546 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
5547 return VLC_EGENERIC;
5549 i_prev_pid = p_program->i_pid;
5554 static void PATCallBack( void *data, dvbpsi_pat_t *p_dvbpsipat )
5556 demux_t *p_demux = data;
5557 demux_sys_t *p_sys = p_demux->p_sys;
5558 dvbpsi_pat_program_t *p_program;
5559 ts_pid_t *patpid = &p_sys->pid[0];
5560 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5562 patpid->i_flags |= FLAG_SEEN;
5564 msg_Dbg( p_demux, "PATCallBack called" );
5566 if(unlikely( p_sys->pid[0].type != TYPE_PAT ))
5568 msg_Warn( p_demux, "PATCallBack called on invalid pid" );
5572 if( ( p_pat->i_version != -1 &&
5573 ( !p_dvbpsipat->b_current_next ||
5574 p_dvbpsipat->i_version == p_pat->i_version ) ) ||
5575 ( p_pat->i_ts_id != -1 && p_dvbpsipat->i_ts_id != p_pat->i_ts_id ) ||
5576 p_sys->b_user_pmt || PATCheck( p_demux, p_dvbpsipat ) )
5578 dvbpsi_pat_delete( p_dvbpsipat );
5582 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
5583 p_dvbpsipat->i_ts_id, p_dvbpsipat->i_version, p_dvbpsipat->b_current_next );
5585 /* Save old programs array */
5586 DECL_ARRAY(ts_pid_t *) old_pmt_rm;
5587 old_pmt_rm.i_alloc = p_pat->programs.i_alloc;
5588 old_pmt_rm.i_size = p_pat->programs.i_size;
5589 old_pmt_rm.p_elems = p_pat->programs.p_elems;
5590 ARRAY_INIT(p_pat->programs);
5592 /* now create programs */
5593 for( p_program = p_dvbpsipat->p_first_program; p_program != NULL;
5594 p_program = p_program->p_next )
5596 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
5598 if( p_program->i_number == 0 )
5601 ts_pid_t *pmtpid = &p_sys->pid[p_program->i_pid];
5603 ValidateDVBMeta( p_demux, p_program->i_pid );
5605 /* create or temporary incref pid */
5606 if( !PIDSetup( p_demux, TYPE_PMT, pmtpid, patpid ) )
5608 msg_Warn( p_demux, " * number=%d pid=%d (ignored)", p_program->i_number,
5613 pmtpid->u.p_pmt->i_number = p_program->i_number;
5615 if( !dvbpsi_pmt_attach( pmtpid->u.p_pmt->handle, p_program->i_number, PMTCallBack, p_demux ) )
5616 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5617 p_program->i_number );
5619 ARRAY_APPEND( p_pat->programs, pmtpid );
5621 /* Now select PID at access level */
5622 if( p_sys->programs.i_size == 0 ||
5623 ProgramIsSelected( p_sys, p_program->i_number ) )
5625 if( p_sys->programs.i_size == 0 )
5627 msg_Dbg( p_demux, "temporary receiving program %d", p_program->i_number );
5628 p_sys->b_default_selection = true;
5629 ARRAY_APPEND( p_sys->programs, p_program->i_number );
5632 if( SetPIDFilter( p_sys, pmtpid, true ) )
5633 p_sys->b_access_control = false;
5634 else if ( p_sys->es_creation == DELAY_ES )
5635 p_sys->es_creation = CREATE_ES;
5638 p_pat->i_version = p_dvbpsipat->i_version;
5639 p_pat->i_ts_id = p_dvbpsipat->i_ts_id;
5641 for(int i=0; i<old_pmt_rm.i_size; i++)
5643 /* decref current or release now unreferenced */
5644 PIDRelease( p_demux, old_pmt_rm.p_elems[i] );
5646 ARRAY_RESET(old_pmt_rm);
5648 dvbpsi_pat_delete( p_dvbpsipat );
5651 static inline bool handle_Init( demux_t *p_demux, dvbpsi_t **handle )
5653 *handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
5656 (*handle)->p_sys = (void *) p_demux;
5660 static ts_pat_t *ts_pat_New( demux_t *p_demux )
5662 ts_pat_t *pat = malloc( sizeof( ts_pat_t ) );
5666 if( !handle_Init( p_demux, &pat->handle ) )
5672 pat->i_version = -1;
5674 ARRAY_INIT( pat->programs );
5679 static void ts_pat_Del( demux_t *p_demux, ts_pat_t *pat )
5681 if( dvbpsi_decoder_present( pat->handle ) )
5682 dvbpsi_pat_detach( pat->handle );
5683 dvbpsi_delete( pat->handle );
5684 for( int i=0; i<pat->programs.i_size; i++ )
5685 PIDRelease( p_demux, pat->programs.p_elems[i] );
5686 ARRAY_RESET( pat->programs );
5690 static ts_pmt_t *ts_pmt_New( demux_t *p_demux )
5692 ts_pmt_t *pmt = malloc( sizeof( ts_pmt_t ) );
5696 if( !handle_Init( p_demux, &pmt->handle ) )
5702 ARRAY_INIT( pmt->e_streams );
5704 pmt->i_version = -1;
5706 pmt->i_pid_pcr = 0x1FFF;
5709 pmt->i_last_dts = -1;
5711 pmt->pcr.i_current = -1;
5712 pmt->pcr.i_first = -1;
5713 pmt->pcr.b_disable = false;
5714 pmt->pcr.i_first_dts = VLC_TS_INVALID;
5715 pmt->pcr.i_pcroffset = -1;
5720 static void ts_pmt_Del( demux_t *p_demux, ts_pmt_t *pmt )
5722 if( dvbpsi_decoder_present( pmt->handle ) )
5723 dvbpsi_pmt_detach( pmt->handle );
5724 dvbpsi_delete( pmt->handle );
5725 for( int i=0; i<pmt->e_streams.i_size; i++ )
5726 PIDRelease( p_demux, pmt->e_streams.p_elems[i] );
5727 ARRAY_RESET( pmt->e_streams );
5729 IODFree( pmt->iod );
5730 if( pmt->i_number > -1 )
5731 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, pmt->i_number );
5735 static ts_pes_t *ts_pes_New( demux_t *p_demux )
5737 VLC_UNUSED(p_demux);
5738 ts_pes_t *pes = malloc( sizeof( ts_pes_t ) );
5743 pes->es.p_mpeg4desc = NULL;
5744 es_format_Init( &pes->es.fmt, UNKNOWN_ES, 0 );
5745 ARRAY_INIT( pes->extra_es );
5746 pes->data_type = TS_ES_DATA_PES;
5747 pes->i_data_size = 0;
5748 pes->i_data_gathered = 0;
5750 pes->pp_last = &pes->p_data;
5751 pes->p_prepcr_outqueue = NULL;
5756 static void ts_pes_Del( demux_t *p_demux, ts_pes_t *pes )
5760 es_out_Del( p_demux->out, pes->es.id );
5761 p_demux->p_sys->i_pmt_es--;
5765 block_ChainRelease( pes->p_data );
5767 if( pes->p_prepcr_outqueue )
5768 block_ChainRelease( pes->p_prepcr_outqueue );
5770 es_format_Clean( &pes->es.fmt );
5771 free( pes->es.p_mpeg4desc );
5773 for( int i = 0; i < pes->extra_es.i_size; i++ )
5775 if( pes->extra_es.p_elems[i]->id )
5777 es_out_Del( p_demux->out, pes->extra_es.p_elems[i]->id );
5778 p_demux->p_sys->i_pmt_es--;
5780 es_format_Clean( &pes->extra_es.p_elems[i]->fmt );
5781 free( pes->extra_es.p_elems[i] );
5783 ARRAY_RESET( pes->extra_es );
5788 static ts_psi_t *ts_psi_New( demux_t *p_demux )
5790 ts_psi_t *psi = malloc( sizeof( ts_psi_t ) );
5794 if( !handle_Init( p_demux, &psi->handle ) )
5800 psi->i_version = -1;
5805 static void ts_psi_Del( demux_t *p_demux, ts_psi_t *psi )
5807 VLC_UNUSED(p_demux);
5808 if( dvbpsi_decoder_present( psi->handle ) )
5809 dvbpsi_DetachDemux( psi->handle );
5810 dvbpsi_delete( psi->handle );