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
446 bool b_access_control;
453 int64_t i_dvb_length;
454 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
456 /* Selected programs */
457 DECL_ARRAY( int ) programs; /* List of selected/access-filtered programs */
458 bool b_default_selection; /* True if set by default to first pmt seen (to get data from filtered access) */
462 mtime_t i_first_dts; /* first dts encountered for the stream */
463 int i_timesourcepid; /* which pid we saved the dts from */
464 bool b_pat_deadline; /* set if we haven't seen PAT within MIN_PAT_INTERVAL */
473 static int Demux ( demux_t *p_demux );
474 static int Control( demux_t *p_demux, int i_query, va_list args );
476 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
478 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent );
479 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid );
481 static void PATCallBack( void*, dvbpsi_pat_t * );
482 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
483 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
484 uint16_t i_extension, demux_t * );
486 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
489 static ts_pat_t *ts_pat_New( demux_t * );
490 static void ts_pat_Del( demux_t *, ts_pat_t * );
491 static ts_pmt_t *ts_pmt_New( demux_t * );
492 static void ts_pmt_Del( demux_t *, ts_pmt_t * );
493 static ts_pes_t *ts_pes_New( demux_t * );
494 static void ts_pes_Del( demux_t *, ts_pes_t * );
495 static ts_psi_t *ts_psi_New( demux_t * );
496 static void ts_psi_Del( demux_t *, ts_psi_t * );
499 static ts_pmt_t * GetProgramByID( demux_sys_t *, int i_program );
500 static bool ProgramIsSelected( demux_sys_t *, uint16_t i_pgrm );
501 static void UpdatePESFilters( demux_t *p_demux, bool b_all );
502 static inline void FlushESBuffer( ts_pes_t *p_pes );
503 static inline int PIDGet( block_t *p )
505 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
508 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
509 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid );
510 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_prg, mtime_t i_pcr );
512 static block_t* ReadTSPacket( demux_t *p_demux );
513 static int ProbeStart( demux_t *p_demux, int i_program );
514 static int ProbeEnd( demux_t *p_demux, int i_program );
515 static int SeekToTime( demux_t *p_demux, ts_pmt_t *, int64_t time );
516 static void ReadyQueuesPostSeek( demux_t *p_demux );
517 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
518 static void PCRFixHandle( demux_t *, ts_pmt_t *, block_t * );
519 static int64_t TimeStampWrapAround( ts_pmt_t *, int64_t );
521 static void IODFree( iod_descriptor_t * );
523 #define TS_USER_PMT_NUMBER (0)
524 static int UserPmt( demux_t *p_demux, const char * );
526 static int SetPIDFilter( demux_sys_t *, ts_pid_t *, bool b_selected );
527 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
529 #define TS_PACKET_SIZE_188 188
530 #define TS_PACKET_SIZE_192 192
531 #define TS_PACKET_SIZE_204 204
532 #define TS_PACKET_SIZE_MAX 204
533 #define TS_HEADER_SIZE 4
535 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size, int i_offset )
537 const uint8_t *p_peek;
539 if( stream_Peek( p_demux->s,
540 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
543 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
545 if( p_peek[i_offset + i_sync] != 0x47 )
548 /* Check next 3 sync bytes */
549 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
550 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
552 msg_Err( p_demux, "cannot peek" );
555 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
556 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
557 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
559 return TS_PACKET_SIZE_188;
561 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
562 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
563 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
567 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
569 return TS_PACKET_SIZE_192;
571 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
572 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
573 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
575 return TS_PACKET_SIZE_204;
579 if( p_demux->b_force )
581 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
582 return TS_PACKET_SIZE_188;
584 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
588 #define TOPFIELD_HEADER_SIZE 3712
590 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, int *pi_header_size, vdr_info_t *p_vdr )
592 const uint8_t *p_peek;
594 int i_packet_size = -1;
596 if( stream_Peek( p_demux->s,
597 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
600 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
601 p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
602 stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
603 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
605 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
606 if( i_packet_size != -1 )
608 msg_Dbg( p_demux, "this is a topfield file" );
610 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
611 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
612 * but after the filename the offsets seem to be incorrect. - DJ */
613 int i_duration, i_name;
614 char *psz_name = xmalloc(25);
615 char *psz_event_name;
616 char *psz_event_text = xmalloc(130);
617 char *psz_ext_text = xmalloc(1025);
619 // 2 bytes version Uimsbf (4,5)
620 // 2 bytes reserved (6,7)
621 // 2 bytes duration in minutes Uimsbf (8,9(
622 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
623 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
624 // 2 bytes service number in channel list (10, 11)
625 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
626 // 4 bytes of reserved + tuner info (14,15,16,17)
627 // 2 bytes of Service ID Bslbf (18,19)
628 // 2 bytes of PMT PID Uimsbf (20,21)
629 // 2 bytes of PCR PID Uimsbf (22,23)
630 // 2 bytes of Video PID Uimsbf (24,25)
631 // 2 bytes of Audio PID Uimsbf (26,27)
632 // 24 bytes filename Bslbf
633 memcpy( psz_name, &p_peek[28], 24 );
635 msg_Dbg( p_demux, "recordingname=%s", psz_name );
636 // 1 byte of sat index Uimsbf (52)
637 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
638 // 4 bytes of freq. Uimsbf (56,57,58,59)
639 // 2 bytes of symbol rate Uimsbf (60,61)
640 // 2 bytes of TS stream ID Uimsbf (62,63)
643 // 2 bytes duration Uimsbf (70,71)
644 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
645 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
646 // 4 bytes EventID Uimsbf (72-75)
647 // 8 bytes of Start and End time info (76-83)
648 // 1 byte reserved (84)
649 // 1 byte event name length Uimsbf (89)
650 i_name = (int)(p_peek[89]&~0x81);
651 msg_Dbg( p_demux, "event name length = %d", i_name);
652 psz_event_name = xmalloc( i_name+1 );
653 // 1 byte parental rating (90)
654 // 129 bytes of event text
655 memcpy( psz_event_name, &p_peek[91], i_name );
656 psz_event_name[i_name] = '\0';
657 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
658 psz_event_text[129-i_name] = '\0';
659 msg_Dbg( p_demux, "event name=%s", psz_event_name );
660 msg_Dbg( p_demux, "event text=%s", psz_event_text );
661 // 12 bytes reserved (220)
663 // 2 bytes Event Text Length Uimsbf
664 // 4 bytes EventID Uimsbf
665 // FIXME We just have 613 bytes. not enough for this entire text
666 // 1024 bytes Extended Event Text Bslbf
667 memcpy( psz_ext_text, p_peek+372, 1024 );
668 psz_ext_text[1024] = '\0';
669 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
670 // 52 bytes reserved Bslbf
672 p_vdr->i_service = GetWBE(&p_peek[18]);
674 return i_packet_size;
675 //return TS_PACKET_SIZE_188;
679 return DetectPacketSize( p_demux, pi_header_size, 0 );
682 static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
684 return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
685 (mtime_t)(p_data[1] << 22)|
686 ((mtime_t)(p_data[2]&0xfe) << 14)|
687 (mtime_t)(p_data[3] << 7)|
688 (mtime_t)(p_data[4] >> 1);
691 static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, size_t i_data, bool b_adaptfield )
693 demux_sys_t *p_sys = p_demux->p_sys;
694 const uint8_t *p_pes = p_pesstart;
695 pid->probed.i_type = -1;
702 uint8_t len = *p_pes;
707 p_pes++; i_data--;/* stuffing */
713 if( len >= 7 && (p_pes[1] & 0x10) )
714 pid->probed.i_pcr_count++;
723 if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
726 size_t i_pesextoffset = 8;
728 if( p_pes[7] & 0x80 ) // PTS
731 if ( i_data < i_pesextoffset )
733 i_dts = ExtractPESTimestamp( &p_pes[9] );
735 if( p_pes[7] & 0x40 ) // DTS
738 if ( i_data < i_pesextoffset )
740 i_dts = ExtractPESTimestamp( &p_pes[14] );
742 if( p_pes[7] & 0x20 ) // ESCR
744 if( p_pes[7] & 0x10 ) // ESrate
746 if( p_pes[7] & 0x08 ) // DSM
748 if( p_pes[7] & 0x04 ) // CopyInfo
750 if( p_pes[7] & 0x02 ) // PESCRC
753 if ( i_data < i_pesextoffset )
756 /* HeaderdataLength */
757 const size_t i_payloadoffset = 8 + 1 + p_pes[8];
760 if ( i_data < i_pesextoffset || i_data < i_payloadoffset )
763 i_data -= 8 + 1 + p_pes[8];
765 if( p_pes[7] & 0x01 ) // PESExt
767 size_t i_extension2_offset = 1;
768 if ( p_pes[i_pesextoffset] & 0x80 ) // private data
769 i_extension2_offset += 16;
770 if ( p_pes[i_pesextoffset] & 0x40 ) // pack
771 i_extension2_offset += 1;
772 if ( p_pes[i_pesextoffset] & 0x20 ) // seq
773 i_extension2_offset += 2;
774 if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
775 i_extension2_offset += 2;
776 if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
778 uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
779 i_extension2_offset += i_len;
781 if( i_data < i_extension2_offset )
784 i_data -= i_extension2_offset;
786 /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
791 const uint8_t *p_data = &p_pes[i_payloadoffset];
793 if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
795 if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
797 pid->probed.i_type = 0x06;
798 pid->probed.i_fourcc = VLC_CODEC_DTS;
800 else if( !memcmp( p_data, "\x0B\x77", 2 ) )
802 pid->probed.i_type = 0x06;
803 pid->probed.i_fourcc = VLC_CODEC_EAC3;
805 else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
807 switch(p_data[1] & 18)
809 /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
810 11 - MPEG Version 1 (ISO/IEC 11172-3) */
812 pid->probed.i_type = 0x04;
815 pid->probed.i_type = 0x03;
820 switch(p_data[1] & 6)
826 pid->probed.i_type = 0x04;
827 pid->probed.i_fourcc = VLC_CODEC_MPGA;
830 pid->probed.i_type = 0x04;
831 pid->probed.i_fourcc = VLC_CODEC_MP2;
834 pid->probed.i_type = 0x04;
835 pid->probed.i_fourcc = VLC_CODEC_MP3;
842 else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
844 if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
846 pid->probed.i_type = 0x1b;
847 pid->probed.i_fourcc = VLC_CODEC_H264;
849 else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
851 pid->probed.i_type = 0x02;
852 pid->probed.i_fourcc = VLC_CODEC_MPGV;
856 /* Track timestamps and flag missing PAT */
857 if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
859 p_sys->patfix.i_first_dts = i_dts;
860 p_sys->patfix.i_timesourcepid = pid->i_pid;
862 else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
863 !p_sys->patfix.b_pat_deadline )
865 if( i_dts - p_sys->patfix.i_first_dts > TO_SCALE(MIN_PAT_INTERVAL) )
866 p_sys->patfix.b_pat_deadline = true;
871 static void BuildPATCallback( void *p_opaque, block_t *p_block )
873 ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
874 dvbpsi_packet_push( pat_pid->u.p_pat->handle, p_block->p_buffer );
877 static void BuildPMTCallback( void *p_opaque, block_t *p_block )
879 ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
880 assert(program_pid->type == TYPE_PMT);
883 dvbpsi_packet_push( program_pid->u.p_pmt->handle,
885 p_block = p_block->p_next;
889 static void MissingPATPMTFixup( demux_t *p_demux )
891 demux_sys_t *p_sys = p_demux->p_sys;
892 int i_program_number = 1234;
893 int i_program_pid = 1337;
894 int i_pcr_pid = 0x1FFF;
897 if( SEEN(p_sys->pid[i_program_pid]) )
899 /* Find a free one */
900 for( i_program_pid = MIN_ES_PID;
901 i_program_pid <= MAX_ES_PID && SEEN(p_sys->pid[i_program_pid]);
905 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
907 if( !SEEN(p_sys->pid[i]) ||
908 p_sys->pid[i].probed.i_type == -1 )
911 if( i_pcr_pid == 0x1FFF && ( p_sys->pid[i].probed.i_type == 0x03 ||
912 p_sys->pid[i].probed.i_pcr_count ) )
913 i_pcr_pid = p_sys->pid[i].i_pid;
921 ts_stream_t patstream =
924 .i_continuity_counter = 0x10,
925 .b_discontinuity = false
928 ts_stream_t pmtprogramstream =
930 .i_pid = i_program_pid,
931 .i_continuity_counter = 0x0,
932 .b_discontinuity = false
935 BuildPAT( p_sys->pid[0].u.p_pat->handle,
936 &p_sys->pid[0], BuildPATCallback,
939 1, &pmtprogramstream, &i_program_number );
941 if( p_sys->pid[i_program_pid].type != TYPE_PMT )
943 msg_Err( p_demux, "PAT creation failed" );
952 es_format_t esfmt = {0};
953 struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
954 pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
955 if( esstreams && mapped )
958 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
960 if( !SEEN(p_sys->pid[i]) ||
961 p_sys->pid[i].probed.i_type == -1 )
964 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
965 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
966 esstreams[j].ts.i_pid = p_sys->pid[i].i_pid;
967 mapped[j].pes = &esstreams[j].pes;
968 mapped[j].ts = &esstreams[j].ts;
969 mapped[j].fmt = &esfmt;
973 BuildPMT( p_sys->pid[0].u.p_pat->handle, VLC_OBJECT(p_demux),
974 &p_sys->pid[i_program_pid], BuildPMTCallback,
978 1, &pmtprogramstream, &i_program_number,
985 /*****************************************************************************
987 *****************************************************************************/
988 static int Open( vlc_object_t *p_this )
990 demux_t *p_demux = (demux_t*)p_this;
993 int i_packet_size, i_packet_header_size = 0;
996 vdr_info_t vdr = {0};
998 /* Search first sync byte */
999 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
1000 if( i_packet_size < 0 )
1001 return VLC_EGENERIC;
1003 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
1006 memset( p_sys, 0, sizeof( demux_sys_t ) );
1007 vlc_mutex_init( &p_sys->csa_lock );
1009 p_demux->pf_demux = Demux;
1010 p_demux->pf_control = Control;
1012 /* Init p_sys field */
1013 p_sys->b_dvb_meta = true;
1014 p_sys->b_access_control = true;
1015 p_sys->b_end_preparse = false;
1016 ARRAY_INIT( p_sys->programs );
1017 p_sys->b_default_selection = false;
1018 p_sys->i_tdt_delta = 0;
1019 p_sys->i_dvb_start = 0;
1020 p_sys->i_dvb_length = 0;
1024 p_sys->arib.b25stream = NULL;
1025 p_sys->stream = p_demux->s;
1027 p_sys->b_broken_charset = false;
1029 for( int i = 0; i < 8192; i++ )
1031 ts_pid_t *pid = &p_sys->pid[i];
1033 pid->i_flags = FLAGS_NONE;
1034 pid->probed.i_fourcc = 0;
1035 pid->probed.i_type = 0;
1037 /* PID 8191 is padding */
1038 p_sys->pid[8191].i_flags = FLAG_SEEN;
1039 p_sys->i_packet_size = i_packet_size;
1040 p_sys->i_packet_header_size = i_packet_header_size;
1041 p_sys->i_ts_read = 50;
1043 p_sys->b_start_record = false;
1045 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
1047 if( !dvbpsi_AttachDemux( (table)->u.p_psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
1049 msg_Warn( obj, "Can't dvbpsi_AttachDemux on pid %d", (table)->i_pid );\
1053 /* Init PAT handler */
1054 patpid = &p_sys->pid[0];
1055 if ( !PIDSetup( p_demux, TYPE_PAT, patpid, NULL ) )
1057 vlc_mutex_destroy( &p_sys->csa_lock );
1061 if( !dvbpsi_pat_attach( patpid->u.p_pat->handle, PATCallBack, p_demux ) )
1063 PIDRelease( p_demux, patpid );
1064 vlc_mutex_destroy( &p_sys->csa_lock );
1066 return VLC_EGENERIC;
1069 if( p_sys->b_dvb_meta )
1071 if( !PIDSetup( p_demux, TYPE_SDT, &p_sys->pid[0x11], NULL ) ||
1072 !PIDSetup( p_demux, TYPE_EIT, &p_sys->pid[0x12], NULL ) ||
1073 !PIDSetup( p_demux, TYPE_TDT, &p_sys->pid[0x14], NULL ) )
1075 PIDRelease( p_demux, &p_sys->pid[0x11] );
1076 PIDRelease( p_demux, &p_sys->pid[0x12] );
1077 PIDRelease( p_demux, &p_sys->pid[0x14] );
1078 p_sys->b_dvb_meta = false;
1082 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x11], p_demux);
1083 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x12], p_demux);
1084 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x14], p_demux);
1085 if( p_sys->b_access_control &&
1086 ( SetPIDFilter( p_sys, &p_sys->pid[0x11], true ) ||
1087 SetPIDFilter( p_sys, &p_sys->pid[0x14], true ) ||
1088 SetPIDFilter( p_sys, &p_sys->pid[0x12], true ) )
1090 p_sys->b_access_control = false;
1094 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
1096 p_sys->i_pmt_es = 0;
1097 p_sys->b_es_all = false;
1100 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
1102 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
1104 /* We handle description of an extra PMT */
1105 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
1106 p_sys->b_user_pmt = false;
1107 if( psz_string && *psz_string )
1108 UserPmt( p_demux, psz_string );
1111 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
1112 if( psz_string && *psz_string )
1117 p_sys->csa = csa_New();
1119 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
1120 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
1121 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
1123 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
1125 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1128 else if ( i_res == VLC_SUCCESS )
1130 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1134 csa_Delete( p_sys->csa );
1140 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
1141 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1143 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
1144 if( i_pkt < 4 || i_pkt > 188 )
1146 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
1147 msg_Warn( p_demux, "using default packet size of 188 bytes" );
1148 p_sys->i_csa_pkt_size = 188;
1151 p_sys->i_csa_pkt_size = i_pkt;
1152 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
1158 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
1160 p_sys->b_canseek = false;
1161 p_sys->b_canfastseek = false;
1162 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
1164 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
1166 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
1167 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1170 if( p_sys->b_canseek )
1172 p_sys->es_creation = NO_ES;
1173 while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
1174 if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
1176 p_sys->es_creation = DELAY_ES;
1179 p_sys->es_creation = ( p_sys->b_access_control ? CREATE_ES : DELAY_ES );
1184 /*****************************************************************************
1186 *****************************************************************************/
1187 static void Close( vlc_object_t *p_this )
1189 demux_t *p_demux = (demux_t*)p_this;
1190 demux_sys_t *p_sys = p_demux->p_sys;
1192 PIDRelease( p_demux, &p_sys->pid[0] );
1194 if( p_sys->b_dvb_meta )
1196 PIDRelease( p_demux, &p_sys->pid[0x11] );
1197 PIDRelease( p_demux, &p_sys->pid[0x12] );
1198 PIDRelease( p_demux, &p_sys->pid[0x14] );
1202 for( int i = 0; i < 8192; i++ )
1204 ts_pid_t *pid = &p_sys->pid[i];
1205 if( pid->type != TYPE_FREE )
1206 msg_Err( p_demux, "PID %d type %d not freed", pid->i_pid, pid->type );
1210 vlc_mutex_lock( &p_sys->csa_lock );
1213 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
1214 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1215 csa_Delete( p_sys->csa );
1217 vlc_mutex_unlock( &p_sys->csa_lock );
1219 ARRAY_RESET( p_sys->programs );
1222 if ( p_sys->arib.p_instance )
1223 arib_instance_destroy( p_sys->arib.p_instance );
1226 if ( p_sys->arib.b25stream )
1228 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
1229 stream_Delete( p_sys->arib.b25stream );
1232 vlc_mutex_destroy( &p_sys->csa_lock );
1236 /*****************************************************************************
1237 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
1238 *****************************************************************************/
1239 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
1240 vlc_value_t oldval, vlc_value_t newval,
1243 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1244 demux_t *p_demux = (demux_t*)p_this;
1245 demux_sys_t *p_sys = p_demux->p_sys;
1246 int i_tmp = (intptr_t)p_data;
1248 vlc_mutex_lock( &p_sys->csa_lock );
1250 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
1252 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
1254 vlc_mutex_unlock( &p_sys->csa_lock );
1258 /*****************************************************************************
1260 *****************************************************************************/
1261 static int Demux( demux_t *p_demux )
1263 demux_sys_t *p_sys = p_demux->p_sys;
1264 bool b_wait_es = p_sys->i_pmt_es <= 0;
1266 /* If we had no PAT within MIN_PAT_INTERVAL, create PAT/PMT from probed streams */
1267 if( p_sys->i_pmt_es == 0 && !SEEN(p_sys->pid[0]) && p_sys->patfix.b_pat_deadline )
1268 MissingPATPMTFixup( p_demux );
1270 /* We read at most 100 TS packet or until a frame is completed */
1271 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1273 bool b_frame = false;
1275 if( !(p_pkt = ReadTSPacket( p_demux )) )
1277 return VLC_DEMUXER_EOF;
1280 if( p_sys->b_start_record )
1282 /* Enable recording once synchronized */
1283 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
1284 p_sys->b_start_record = false;
1287 /* Parse the TS packet */
1288 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1290 /* Probe streams to build PAT/PMT after MIN_PAT_INTERVAL in case we don't see any PAT */
1291 if( !SEEN(p_sys->pid[0]) &&
1292 (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
1293 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1294 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
1296 ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
1297 p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
1300 switch( p_pid->type )
1303 dvbpsi_packet_push( p_pid->u.p_pat->handle, p_pkt->p_buffer );
1307 dvbpsi_packet_push( p_pid->u.p_pmt->handle, p_pkt->p_buffer );
1311 if( !p_sys->b_access_control && !(p_pid->i_flags & FLAG_FILTERED) )
1313 /* That packet is for an unselected ES, don't waste time/memory gathering its data */
1314 block_Release( p_pkt );
1318 p_sys->b_end_preparse = true;
1319 if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
1321 AddAndCreateES( p_demux, NULL );
1323 b_frame = GatherData( p_demux, p_pid, p_pkt );
1325 if( p_sys->b_default_selection )
1327 p_sys->b_default_selection = false;
1328 assert(p_sys->programs.i_size == 1);
1329 if( p_sys->programs.p_elems[0] != p_pid->p_parent->u.p_pmt->i_number )
1331 SetPrgFilter( p_demux, p_sys->programs.p_elems[0], false );
1332 SetPrgFilter( p_demux, p_pid->p_parent->u.p_pmt->i_number, true );
1333 p_sys->programs.p_elems[0] = p_pid->p_parent->u.p_pmt->i_number;
1341 if( p_sys->b_dvb_meta )
1342 dvbpsi_packet_push( p_pid->u.p_psi->handle, p_pkt->p_buffer );
1347 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1349 /* We have to handle PCR if present */
1350 PCRHandle( p_demux, p_pid, p_pkt );
1351 block_Release( p_pkt );
1355 p_pid->i_flags |= FLAG_SEEN;
1357 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1361 demux_UpdateTitleFromStream( p_demux );
1362 return VLC_DEMUXER_SUCCESS;
1365 /*****************************************************************************
1367 *****************************************************************************/
1368 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1370 demux_sys_t *p_sys = p_demux->p_sys;
1376 if( p_sys->i_dvb_length > 0 )
1378 const int64_t t = mdate() + p_sys->i_tdt_delta;
1380 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1383 *pi_length = p_sys->i_dvb_length;
1385 *pi_time = t - p_sys->i_dvb_start;
1389 return VLC_EGENERIC;
1392 static void UpdatePESFilters( demux_t *p_demux, bool b_all )
1394 demux_sys_t *p_sys = p_demux->p_sys;
1395 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1396 for( int i=0; i< p_pat->programs.i_size; i++ )
1398 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1399 bool b_program_selected;
1400 if( (p_sys->b_default_selection && !p_sys->b_access_control) || b_all )
1401 b_program_selected = true;
1403 b_program_selected = ProgramIsSelected( p_sys, p_pmt->i_number );
1405 SetPIDFilter( p_sys, p_pat->programs.p_elems[i], b_program_selected );
1406 if( p_pmt->i_pid_pcr > 0 )
1407 SetPIDFilter( p_sys, &p_sys->pid[p_pmt->i_pid_pcr], b_program_selected );
1409 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
1411 ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
1412 bool b_stream_selected = b_program_selected;
1413 if( b_program_selected && !b_all && espid->u.p_pes->es.id )
1414 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1415 espid->u.p_pes->es.id, &b_stream_selected );
1417 if( !p_sys->b_es_all && espid->u.p_pes->es.fmt.i_cat == UNKNOWN_ES )
1418 b_stream_selected = false;
1420 if( b_stream_selected )
1421 msg_Dbg( p_demux, "enablind pid %d from program %d", espid->i_pid, p_pmt->i_number );
1423 SetPIDFilter( p_sys, espid, b_stream_selected );
1424 if( !b_stream_selected )
1425 FlushESBuffer( espid->u.p_pes );
1431 static int Control( demux_t *p_demux, int i_query, va_list args )
1433 demux_sys_t *p_sys = p_demux->p_sys;
1435 bool b_bool, *pb_bool;
1440 int i_first_program = ( p_sys->programs.i_size ) ? p_sys->programs.p_elems[0] : 0;
1442 if( PREPARSING || !i_first_program || p_sys->b_default_selection )
1444 if( likely(p_sys->pid[0].type == TYPE_PAT) )
1446 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1447 /* Set default program for preparse time (no program has been selected) */
1448 for( int i = 0; i < p_pat->programs.i_size; i++ )
1450 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
1451 p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1452 if( ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) && p_pmt->i_last_dts > 0 )
1454 i_first_program = p_pmt->i_number;
1463 case DEMUX_GET_POSITION:
1464 pf = (double*) va_arg( args, double* );
1466 /* Access control test is because EPG for recordings is not relevant */
1467 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1469 int64_t i_time, i_length;
1470 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1472 *pf = (double)i_time/(double)i_length;
1477 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1478 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1479 p_pmt->pcr.i_current > -1 )
1481 double i_length = TimeStampWrapAround( p_pmt,
1482 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1483 i_length += p_pmt->pcr.i_pcroffset;
1484 double i_pos = TimeStampWrapAround( p_pmt,
1485 p_pmt->pcr.i_current ) - p_pmt->pcr.i_first;
1486 *pf = i_pos / i_length;
1490 if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1492 int64_t offset = stream_Tell( p_sys->stream );
1493 *pf = (double)offset / (double)i64;
1498 case DEMUX_SET_POSITION:
1499 f = (double) va_arg( args, double );
1501 if(!p_sys->b_canseek)
1504 if( p_sys->b_dvb_meta && p_sys->b_access_control &&
1505 !p_sys->b_force_seek_per_percent &&
1506 (p_pmt = GetProgramByID( p_sys, i_first_program )) )
1508 int64_t i_time, i_length;
1509 if( !DVBEventInformation( p_demux, &i_time, &i_length ) &&
1510 i_length > 0 && !SeekToTime( p_demux, p_pmt, TO_SCALE(i_length) * f ) )
1512 ReadyQueuesPostSeek( p_demux );
1513 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1514 TO_SCALE(i_length) * f );
1519 if( !p_sys->b_force_seek_per_percent &&
1520 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1521 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1522 p_pmt->pcr.i_current > -1 )
1524 double i_length = TimeStampWrapAround( p_pmt,
1525 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1526 if( !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + i_length * f ) )
1528 ReadyQueuesPostSeek( p_demux );
1529 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1530 FROM_SCALE(p_pmt->pcr.i_first + i_length * f) );
1535 i64 = stream_Size( p_sys->stream );
1537 stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) == VLC_SUCCESS )
1539 ReadyQueuesPostSeek( p_demux );
1544 case DEMUX_SET_TIME:
1545 i64 = (int64_t)va_arg( args, int64_t );
1547 if( p_sys->b_canseek &&
1548 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1549 p_pmt->pcr.i_first > -1 &&
1550 !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + TO_SCALE(i64) ) )
1552 ReadyQueuesPostSeek( p_demux );
1553 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1554 FROM_SCALE(p_pmt->pcr.i_first) + i64 - VLC_TS_0 );
1559 case DEMUX_GET_TIME:
1560 pi64 = (int64_t*)va_arg( args, int64_t * );
1562 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1564 if( !DVBEventInformation( p_demux, pi64, NULL ) )
1568 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1569 p_pmt->pcr.i_current > -1 && p_pmt->pcr.i_first > -1 )
1571 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
1572 *pi64 = FROM_SCALE(i_pcr - p_pmt->pcr.i_first);
1577 case DEMUX_GET_LENGTH:
1578 pi64 = (int64_t*)va_arg( args, int64_t * );
1580 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1582 if( !DVBEventInformation( p_demux, NULL, pi64 ) )
1586 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1587 ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) &&
1588 p_pmt->i_last_dts > 0 )
1590 int64_t i_start = (p_pmt->pcr.i_first > -1) ? p_pmt->pcr.i_first :
1591 TO_SCALE(p_pmt->pcr.i_first_dts);
1592 int64_t i_last = TimeStampWrapAround( p_pmt, p_pmt->i_last_dts );
1593 i_last += p_pmt->pcr.i_pcroffset;
1594 *pi64 = FROM_SCALE(i_last - i_start);
1599 case DEMUX_SET_GROUP:
1603 i_int = (int)va_arg( args, int );
1604 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1605 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1607 if( i_int != 0 ) /* If not default program */
1609 /* Deselect/filter current ones */
1613 p_sys->b_es_all = false;
1614 ARRAY_APPEND( p_sys->programs, i_int );
1615 UpdatePESFilters( p_demux, false );
1617 else if( likely( p_list != NULL ) )
1619 p_sys->b_es_all = false;
1620 for( int i = 0; i < p_list->i_count; i++ )
1621 ARRAY_APPEND( p_sys->programs, p_list->p_values[i].i_int );
1622 UpdatePESFilters( p_demux, false );
1626 p_sys->b_es_all = true;
1627 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1628 for( int i = 0; i < p_pat->programs.i_size; i++ )
1629 ARRAY_APPEND( p_sys->programs, p_pat->programs.p_elems[i]->i_pid );
1630 UpdatePESFilters( p_demux, true );
1633 p_sys->b_default_selection = false;
1641 i_int = (int)va_arg( args, int );
1642 msg_Dbg( p_demux, "DEMUX_SET_ES %d", i_int );
1644 if( !p_sys->b_es_all ) /* Won't change anything */
1645 UpdatePESFilters( p_demux, false );
1650 case DEMUX_GET_TITLE_INFO:
1652 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1653 int *c = va_arg( args, int * );
1655 *va_arg( args, int* ) = 0; /* Title offset */
1656 *va_arg( args, int* ) = 0; /* Chapter offset */
1657 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1660 case DEMUX_SET_TITLE:
1661 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1663 case DEMUX_SET_SEEKPOINT:
1664 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1666 case DEMUX_GET_META:
1667 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1669 case DEMUX_CAN_RECORD:
1670 pb_bool = (bool*)va_arg( args, bool * );
1674 case DEMUX_SET_RECORD_STATE:
1675 b_bool = (bool)va_arg( args, int );
1678 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1679 p_sys->b_start_record = b_bool;
1682 case DEMUX_GET_SIGNAL:
1683 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1689 return VLC_EGENERIC;
1692 /*****************************************************************************
1694 *****************************************************************************/
1695 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1697 demux_sys_t *p_sys = p_demux->p_sys;
1698 char *psz_dup = strdup( psz_fmt );
1699 char *psz = psz_dup;
1707 i_pid = strtol( psz, &psz, 0 );
1708 if( i_pid < 2 || i_pid >= 8192 )
1711 /* Parse optional program number */
1714 i_number = strtol( &psz[1], &psz, 0 );
1717 ts_pid_t *pmtpid = &p_sys->pid[i_pid];
1719 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1720 if ( !PIDSetup( p_demux, TYPE_PMT, pmtpid, &p_sys->pid[0] ) )
1724 ts_pmt_t *p_pmt = pmtpid->u.p_pmt;
1725 p_pmt->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1726 if( !dvbpsi_pmt_attach( p_pmt->handle,
1727 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1728 PMTCallBack, p_demux ) )
1730 PIDRelease( p_demux, pmtpid );
1734 ARRAY_APPEND( p_sys->pid[0].u.p_pat->programs, pmtpid );
1736 psz = strchr( psz, '=' );
1739 while( psz && *psz )
1741 char *psz_next = strchr( psz, ',' );
1747 i_pid = strtol( psz, &psz, 0 );
1748 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1751 char *psz_opt = &psz[1];
1752 if( !strcmp( psz_opt, "pcr" ) )
1754 p_pmt->i_pid_pcr = i_pid;
1756 else if( p_sys->pid[i_pid].type == TYPE_FREE )
1758 ts_pid_t *pid = &p_sys->pid[i_pid];
1760 char *psz_arg = strchr( psz_opt, '=' );
1764 if ( !PIDSetup( p_demux, TYPE_PES, pid, pmtpid ) )
1767 ARRAY_APPEND( p_pmt->e_streams, pid );
1769 if( p_pmt->i_pid_pcr <= 0 )
1770 p_pmt->i_pid_pcr = i_pid;
1772 es_format_t *fmt = &pid->u.p_pes->es.fmt;
1774 if( psz_arg && strlen( psz_arg ) == 4 )
1776 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1777 psz_arg[2], psz_arg[3] );
1778 int i_cat = UNKNOWN_ES;
1780 if( !strcmp( psz_opt, "video" ) )
1782 else if( !strcmp( psz_opt, "audio" ) )
1784 else if( !strcmp( psz_opt, "spu" ) )
1787 es_format_Init( fmt, i_cat, i_codec );
1788 fmt->b_packetized = false;
1792 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1793 PIDFillFormat( fmt, i_stream_type );
1796 fmt->i_group = i_number;
1797 if( p_sys->b_es_id_pid )
1800 if( fmt->i_cat != UNKNOWN_ES )
1802 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1803 (char*)&fmt->i_codec );
1804 pid->u.p_pes->es.id = es_out_Add( p_demux->out, fmt );
1813 p_sys->b_user_pmt = true;
1819 return VLC_EGENERIC;
1822 static int SetPIDFilter( demux_sys_t *p_sys, ts_pid_t *p_pid, bool b_selected )
1825 p_pid->i_flags |= FLAG_FILTERED;
1827 p_pid->i_flags &= ~FLAG_FILTERED;
1829 if( !p_sys->b_access_control )
1830 return VLC_EGENERIC;
1832 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1833 p_pid->i_pid, b_selected );
1836 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1838 demux_sys_t *p_sys = p_demux->p_sys;
1839 ts_pmt_t *p_pmt = NULL;
1840 ts_pid_t *pid = NULL;
1842 /* Search pmt to be unselected */
1843 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
1846 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1847 for( int i = 0; i < p_pat->programs.i_size; i++ )
1849 ts_pid_t *pmtpid = p_pat->programs.p_elems[i];
1850 assert(pmtpid->type == TYPE_PMT);
1852 if( pmtpid->u.p_pmt->i_number == i_prg_id )
1855 p_pmt = pid->u.p_pmt;
1862 p_pmt->pcr.b_disable = !p_sys->b_trust_pcr;
1864 SetPIDFilter( p_sys, pid, b_selected );
1865 if( p_pmt->i_pid_pcr > 0 )
1866 SetPIDFilter( p_sys, &p_sys->pid[p_pmt->i_pid_pcr], b_selected );
1869 for( int i = 0; i < p_pmt->e_streams.i_size; i++ )
1871 ts_pid_t *pespid = p_pmt->e_streams.p_elems[i];
1872 assert( pespid->type == TYPE_PES );
1873 /* We only remove/select es that aren't defined by extra pmt */
1874 if( pespid->u.p_pes->es.id || !b_selected )
1875 SetPIDFilter( p_sys, pespid, b_selected );
1879 static void PIDReset( ts_pid_t *pid )
1881 assert(pid->i_refcount == 0);
1883 pid->i_flags &= ~FLAG_SCRAMBLED;
1884 pid->p_parent = NULL;
1885 pid->type = TYPE_FREE;
1888 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent )
1890 if( pid == p_parent || pid->i_pid == 0x1FFF )
1893 if( pid->i_refcount == 0 )
1895 assert( pid->type == TYPE_FREE );
1898 case TYPE_FREE: /* nonsense ?*/
1904 pid->u.p_pat = ts_pat_New( p_demux );
1911 pid->u.p_pmt = ts_pmt_New( p_demux );
1918 pid->u.p_pes = ts_pes_New( p_demux );
1927 pid->u.p_psi = ts_psi_New( p_demux );
1939 pid->p_parent = p_parent;
1941 else if( pid->type == i_type && pid->i_refcount < UINT8_MAX )
1947 if( pid->type != TYPE_FREE )
1948 msg_Warn( p_demux, "Tried to redeclare pid %d with another type", pid->i_pid );
1955 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid )
1957 if( pid->i_refcount == 0 )
1959 assert( pid->type == TYPE_FREE );
1962 else if( pid->i_refcount == 1 )
1966 else if( pid->i_refcount > 1 )
1968 assert( pid->type != TYPE_FREE && pid->type != TYPE_PAT );
1972 if( pid->i_refcount == 0 )
1977 case TYPE_FREE: /* nonsense ?*/
1978 assert( pid->type != TYPE_FREE );
1982 ts_pat_Del( p_demux, pid->u.p_pat );
1983 pid->u.p_pat = NULL;
1987 ts_pmt_Del( p_demux, pid->u.p_pmt );
1988 pid->u.p_pmt = NULL;
1992 ts_pes_Del( p_demux, pid->u.p_pes );
1993 pid->u.p_pes = NULL;
1999 ts_psi_Del( p_demux, pid->u.p_psi );
2000 pid->u.p_psi = NULL;
2005 SetPIDFilter( p_demux->p_sys, pid, false );
2010 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
2015 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
2026 static block_t *Opus_Parse(demux_t *demux, block_t *block)
2028 block_t *out = NULL;
2029 block_t **last = NULL;
2031 uint8_t *buf = block->p_buffer;
2032 size_t len = block->i_buffer;
2034 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
2035 int16_t start_trim = 0, end_trim = 0;
2036 int start_trim_flag = (buf[1] >> 4) & 1;
2037 int end_trim_flag = (buf[1] >> 3) & 1;
2038 int control_extension_flag = (buf[1] >> 2) & 1;
2043 unsigned au_size = 0;
2051 if (start_trim_flag) {
2052 start_trim = read_opus_flag(&buf, &len);
2053 if (start_trim < 0) {
2054 msg_Err(demux, "Invalid start trimming flag");
2057 if (end_trim_flag) {
2058 end_trim = read_opus_flag(&buf, &len);
2060 msg_Err(demux, "Invalid end trimming flag");
2063 if (control_extension_flag && len) {
2064 unsigned l = *buf++; len--;
2066 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
2073 if (!au_size || au_size > len) {
2074 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
2078 block_t *au = block_Alloc(au_size);
2081 memcpy(au->p_buffer, buf, au_size);
2082 block_CopyProperties(au, block);
2091 au->i_nb_samples = opus_frame_duration(buf, au_size);
2092 if (end_trim && end_trim <= au->i_nb_samples)
2093 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
2097 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
2098 au->i_nb_samples -= start_trim;
2099 if (au->i_nb_samples == 0)
2100 au->i_flags |= BLOCK_FLAG_PREROLL;
2107 block_Release(block);
2111 /****************************************************************************
2113 ****************************************************************************/
2114 static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header, size_t i_header,
2115 unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
2120 return VLC_EGENERIC;
2122 switch( p_header[3] )
2124 case 0xBC: /* Program stream map */
2125 case 0xBE: /* Padding */
2126 case 0xBF: /* Private stream 2 */
2127 case 0xF0: /* ECM */
2128 case 0xF1: /* EMM */
2129 case 0xFF: /* Program stream directory */
2130 case 0xF2: /* DSMCC stream */
2131 case 0xF8: /* ITU-T H.222.1 type E stream */
2135 if( ( p_header[6]&0xC0 ) == 0x80 )
2138 i_skip = p_header[8] + 9;
2140 if( p_header[7]&0x80 ) /* has pts */
2142 if( i_header < 9 + 5 )
2143 return VLC_EGENERIC;
2144 *pi_pts = ExtractPESTimestamp( &p_header[9] );
2146 if( p_header[7]&0x40 ) /* has dts */
2148 if( i_header < 14 + 5 )
2149 return VLC_EGENERIC;
2150 *pi_dts = ExtractPESTimestamp( &p_header[14] );
2157 if( i_header < i_skip + 1 )
2158 return VLC_EGENERIC;
2159 while( i_skip < 23 && p_header[i_skip] == 0xff )
2162 if( i_header < i_skip + 1 )
2163 return VLC_EGENERIC;
2167 msg_Err( p_demux, "too much MPEG-1 stuffing" );
2168 return VLC_EGENERIC;
2170 if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
2175 if( i_header < i_skip + 1 )
2176 return VLC_EGENERIC;
2178 if( p_header[i_skip]&0x20 )
2180 if( i_header < i_skip + 5 )
2181 return VLC_EGENERIC;
2182 *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
2184 if( p_header[i_skip]&0x10 ) /* has dts */
2186 if( i_header < i_skip + 10 )
2187 return VLC_EGENERIC;
2188 *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
2208 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
2211 unsigned i_pes_size = 0;
2212 unsigned i_skip = 0;
2215 mtime_t i_length = 0;
2217 assert(pid->type == TYPE_PES);
2218 assert(pid->p_parent && pid->p_parent->type == TYPE_PMT);
2220 const int i_max = block_ChainExtract( p_pes, header, 34 );
2223 block_ChainRelease( p_pes );
2227 if( SCRAMBLED(*pid) || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2229 if ( !SCRAMBLED(*pid) )
2230 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
2231 header[0], header[1],header[2],header[3], pid->i_pid );
2232 block_ChainRelease( p_pes );
2236 if( ParsePESHeader( p_demux, (uint8_t*)&header, i_max, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
2238 block_ChainRelease( p_pes );
2244 i_pts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_pts );
2246 i_dts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_dts );
2249 if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
2250 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
2254 else if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
2255 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
2256 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
2260 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT &&
2261 pid->u.p_pes->es.p_mpeg4desc )
2263 decoder_config_descriptor_t *dcd = &pid->u.p_pes->es.p_mpeg4desc->dec_descr;
2265 if( dcd->i_extra > 2 &&
2266 dcd->p_extra[0] == 0x10 &&
2267 ( dcd->p_extra[1]&0x10 ) )
2269 /* display length */
2270 if( p_pes->i_buffer + 2 <= i_skip )
2271 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
2275 if( p_pes->i_buffer + 2 <= i_skip )
2276 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
2282 while( p_pes && i_skip > 0 )
2284 if( p_pes->i_buffer <= i_skip )
2286 block_t *p_next = p_pes->p_next;
2288 i_skip -= p_pes->i_buffer;
2289 block_Release( p_pes );
2294 p_pes->i_buffer -= i_skip;
2295 p_pes->p_buffer += i_skip;
2300 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
2301 if( i_pts >= 0 && i_dts < 0 )
2309 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
2312 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
2314 p_pes->i_length = i_length * 100 / 9;
2316 p_block = block_ChainGather( p_pes );
2317 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT )
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';
2329 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_TELETEXT )
2331 if( p_block->i_pts <= VLC_TS_INVALID && pid->p_parent )
2333 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
2334 * In this case use the last PCR + 40ms */
2335 assert( pid->p_parent->type == TYPE_PMT );
2336 if( likely(pid->p_parent->type == TYPE_PMT) )
2338 mtime_t i_pcr = pid->p_parent->u.p_pmt->pcr.i_current;
2339 if( i_pcr > VLC_TS_INVALID )
2340 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
2344 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_A ||
2345 pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_C )
2347 if( p_block->i_pts <= VLC_TS_INVALID )
2349 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2351 p_block->i_buffer = i_pes_size;
2354 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2357 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2360 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_OPUS)
2362 p_block = Opus_Parse(p_demux, p_block);
2365 if( !pid->p_parent || pid->p_parent->type != TYPE_PMT )
2367 block_Release( p_block );
2371 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2374 block_t *p_next = p_block->p_next;
2375 p_block->p_next = NULL;
2377 if( p_pmt->pcr.i_first == -1 ) /* Not seen yet */
2378 PCRFixHandle( p_demux, p_pmt, p_block );
2380 if( p_pmt->pcr.i_current > -1 || p_pmt->pcr.b_disable )
2382 if( pid->u.p_pes->p_prepcr_outqueue )
2384 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2385 p_block = pid->u.p_pes->p_prepcr_outqueue;
2386 pid->u.p_pes->p_prepcr_outqueue = NULL;
2389 if ( p_pmt->pcr.b_disable && p_block->i_dts > VLC_TS_INVALID &&
2390 ( p_pmt->i_pid_pcr == pid->i_pid || p_pmt->i_pid_pcr == 0x1FFF ) )
2392 ProgramSetPCR( p_demux, p_pmt, (p_block->i_dts - VLC_TS_0) * 9 / 100 - 120000 );
2395 /* Compute PCR/DTS offset if any */
2396 if( p_pmt->pcr.i_pcroffset == -1 && p_block->i_dts > VLC_TS_INVALID &&
2397 p_pmt->pcr.i_current > VLC_TS_INVALID )
2399 int64_t i_dts27 = (p_block->i_dts - VLC_TS_0) * 9 / 100;
2400 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
2401 if( i_dts27 < i_pcr )
2403 p_pmt->pcr.i_pcroffset = i_pcr - i_dts27 + 80000;
2404 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64
2405 "us later than pcr, applying delay",
2406 pid->i_pid, p_pmt->pcr.i_pcroffset * 100 / 9 );
2408 else p_pmt->pcr.i_pcroffset = 0;
2411 if( p_pmt->pcr.i_pcroffset != -1 )
2413 if( p_block->i_dts > VLC_TS_INVALID )
2414 p_block->i_dts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2415 if( p_block->i_pts > VLC_TS_INVALID )
2416 p_block->i_pts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2419 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
2421 es_out_Send( p_demux->out, pid->u.p_pes->extra_es.p_elems[i]->id,
2422 block_Duplicate( p_block ) );
2425 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2429 if( p_pmt->pcr.i_first == -1 ) /* Not seen yet */
2430 PCRFixHandle( p_demux, p_pmt, p_block );
2432 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2440 msg_Warn( p_demux, "empty pes" );
2444 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
2446 block_t *p_content = block_ChainGather( p_data );
2448 if ( pid->p_parent && pid->p_parent->type == TYPE_PMT )
2450 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2451 mtime_t i_date = p_pmt->pcr.i_current;
2453 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SCTE_27 )
2455 /* We need to extract the truncated pts stored inside the payload */
2456 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
2459 size_t i_offset = 4;
2460 if( p_content->p_buffer[3] & 0x40 )
2462 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
2463 p_content->p_buffer[8];
2466 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
2468 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
2471 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
2472 if( i_display_in < i_date )
2473 i_date = i_display_in + (1ll << 32);
2475 i_date = i_display_in;
2482 p_content->i_dts = p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
2483 PCRFixHandle( p_demux, p_pmt, p_content );
2486 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_content );
2489 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
2491 block_t *p_data = pid->u.p_pes->p_data;
2496 /* remove the pes from pid */
2497 pid->u.p_pes->p_data = NULL;
2498 pid->u.p_pes->i_data_size = 0;
2499 pid->u.p_pes->i_data_gathered = 0;
2500 pid->u.p_pes->pp_last = &pid->u.p_pes->p_data;
2502 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
2504 ParsePES( p_demux, pid, p_data );
2506 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
2508 ParseTableSection( p_demux, pid, p_data );
2512 block_ChainRelease( p_data );
2516 static block_t* ReadTSPacket( demux_t *p_demux )
2518 demux_sys_t *p_sys = p_demux->p_sys;
2522 /* Get a new TS packet */
2523 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2525 if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
2526 msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
2528 msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
2532 if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
2534 block_Release( p_pkt );
2538 /* Skip header (BluRay streams).
2539 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2540 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2542 p_pkt->p_buffer += p_sys->i_packet_header_size;
2543 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2545 /* Check sync byte and re-sync if needed */
2546 if( p_pkt->p_buffer[0] != 0x47 )
2548 msg_Warn( p_demux, "lost synchro" );
2549 block_Release( p_pkt );
2552 const uint8_t *p_peek;
2553 int i_peek, i_skip = 0;
2555 i_peek = stream_Peek( p_sys->stream, &p_peek,
2556 p_sys->i_packet_size * 10 );
2557 if( i_peek < p_sys->i_packet_size + 1 )
2559 msg_Dbg( p_demux, "eof ?" );
2563 while( i_skip < i_peek - p_sys->i_packet_size )
2565 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2566 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2572 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2573 stream_Read( p_sys->stream, NULL, i_skip );
2575 if( i_skip < i_peek - p_sys->i_packet_size )
2580 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2582 msg_Dbg( p_demux, "eof ?" );
2589 static int64_t TimeStampWrapAround( ts_pmt_t *p_pmt, int64_t i_time )
2591 int64_t i_adjust = 0;
2592 if( p_pmt && p_pmt->pcr.i_first > 0x0FFFFFFFF && i_time < 0x0FFFFFFFF )
2593 i_adjust = 0x1FFFFFFFF;
2595 return i_time + i_adjust;
2598 static mtime_t GetPCR( block_t *p_pkt )
2600 const uint8_t *p = p_pkt->p_buffer;
2604 if( ( p[3]&0x20 ) && /* adaptation */
2608 /* PCR is 33 bits */
2609 i_pcr = ( (mtime_t)p[6] << 25 ) |
2610 ( (mtime_t)p[7] << 17 ) |
2611 ( (mtime_t)p[8] << 9 ) |
2612 ( (mtime_t)p[9] << 1 ) |
2613 ( (mtime_t)p[10] >> 7 );
2618 static inline void FlushESBuffer( ts_pes_t *p_pes )
2622 p_pes->i_data_gathered = p_pes->i_data_size = 0;
2623 block_ChainRelease( p_pes->p_data );
2624 p_pes->p_data = NULL;
2625 p_pes->pp_last = &p_pes->p_data;
2629 static void ReadyQueuesPostSeek( demux_t *p_demux )
2631 demux_sys_t *p_sys = p_demux->p_sys;
2633 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2634 for( int i=0; i< p_pat->programs.i_size; i++ )
2636 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2637 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2639 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
2641 if( pid->type != TYPE_PES )
2644 if( !pid->u.p_pes->es.id )
2646 block_t *p_block = block_Alloc(1);
2649 p_block->i_buffer = 0;
2650 p_block->i_flags = BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED;
2651 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2657 if( pid->u.p_pes->p_prepcr_outqueue )
2659 block_ChainRelease( pid->u.p_pes->p_prepcr_outqueue );
2660 pid->u.p_pes->p_prepcr_outqueue = NULL;
2663 FlushESBuffer( pid->u.p_pes );
2665 p_pmt->pcr.i_current = -1;
2669 static int SeekToTime( demux_t *p_demux, ts_pmt_t *p_pmt, int64_t i_scaledtime )
2671 demux_sys_t *p_sys = p_demux->p_sys;
2673 /* Deal with common but worst binary search case */
2674 if( p_pmt->pcr.i_first == i_scaledtime && p_sys->b_canseek )
2675 return stream_Seek( p_sys->stream, 0 );
2677 if( !p_sys->b_canfastseek )
2678 return VLC_EGENERIC;
2680 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2682 /* Find the time position by using binary search algorithm. */
2683 int64_t i_head_pos = 0;
2684 int64_t i_tail_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2685 if( i_head_pos >= i_tail_pos )
2686 return VLC_EGENERIC;
2688 bool b_found = false;
2689 while( (i_head_pos + p_sys->i_packet_size) <= i_tail_pos && !b_found )
2691 /* Round i_pos to a multiple of p_sys->i_packet_size */
2692 int64_t i_splitpos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2693 int64_t i_div = i_splitpos % p_sys->i_packet_size;
2694 i_splitpos -= i_div;
2696 if ( stream_Seek( p_sys->stream, i_splitpos ) != VLC_SUCCESS )
2699 int64_t i_pos = i_splitpos;
2700 while( i_pos > -1 && i_pos < i_tail_pos )
2703 block_t *p_pkt = ReadTSPacket( p_demux );
2706 i_head_pos = i_tail_pos;
2710 i_pos = stream_Tell( p_sys->stream );
2712 int i_pid = PIDGet( p_pkt );
2713 if( i_pid != 0x1FFF && p_sys->pid[i_pid].type == TYPE_PES &&
2714 p_sys->pid[i_pid].p_parent->u.p_pmt == p_pmt &&
2715 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
2716 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
2719 unsigned i_skip = 4;
2720 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
2722 if( p_pkt->i_buffer >= 4 + 2 + 5 )
2724 i_pcr = GetPCR( p_pkt );
2725 i_skip += 1 + p_pkt->p_buffer[4];
2732 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2733 p_pkt->i_buffer - i_skip, &i_skip, &i_dts, &i_pts ) )
2740 block_Release( p_pkt );
2744 int64_t i_diff = i_scaledtime - TimeStampWrapAround( p_pmt, i_pcr );
2746 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2747 else if( i_diff < TO_SCALE(VLC_TS_0 + CLOCK_FREQ / 2) ) // 500ms
2755 if ( !b_found && i_pos > i_tail_pos - p_sys->i_packet_size )
2756 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2761 msg_Dbg( p_demux, "Seek():cannot find a time position." );
2762 stream_Seek( p_sys->stream, i_initial_pos );
2763 return VLC_EGENERIC;
2768 static ts_pmt_t * GetProgramByID( demux_sys_t *p_sys, int i_program )
2770 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
2773 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2774 for( int i = 0; i < p_pat->programs.i_size; i++ )
2776 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
2777 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2778 if( p_pmt->i_number == i_program )
2784 #define PROBE_CHUNK_COUNT 250
2786 static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_pcr, bool *pb_found )
2788 demux_sys_t *p_sys = p_demux->p_sys;
2790 block_t *p_pkt = NULL;
2795 if( i_count++ > PROBE_CHUNK_COUNT || !( p_pkt = ReadTSPacket( p_demux ) ) )
2800 int i_pid = PIDGet( p_pkt );
2801 p_sys->pid[i_pid].i_flags |= FLAG_SEEN;
2803 if( i_pid != 0x1FFF && p_sys->pid[i_pid].type == TYPE_PES &&
2804 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
2805 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
2808 bool b_pcrresult = true;
2810 if( p_pkt->i_buffer >= 4 + 2 + 5 )
2811 *pi_pcr = GetPCR( p_pkt );
2815 b_pcrresult = false;
2818 unsigned i_skip = 4;
2819 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
2820 i_skip += 1 + p_pkt->p_buffer[4];
2822 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2823 p_pkt->i_buffer - i_skip,
2824 &i_skip, &i_dts, &i_pts ) )
2828 else if( i_pts != -1 )
2833 if( *pi_pcr != -1 ) // TODO: non ES PCR
2835 ts_pid_t *pmtpid = p_sys->pid[i_pid].p_parent;
2836 assert(pmtpid->type == TYPE_PMT);
2838 if( i_program == 0 || i_program == pmtpid->u.p_pmt->i_number )
2842 pmtpid->u.p_pmt->i_last_dts = *pi_pcr;
2844 /* Start, only keep first */
2845 else if( b_pcrresult && pmtpid->u.p_pmt->pcr.i_first == -1 )
2847 pmtpid->u.p_pmt->pcr.i_first = *pi_pcr;
2849 else if( pmtpid->u.p_pmt->pcr.i_first_dts < VLC_TS_0 )
2851 pmtpid->u.p_pmt->pcr.i_first_dts = VLC_TS_0 + *pi_pcr * 100 / 9;
2858 block_Release( p_pkt );
2864 static int ProbeStart( demux_t *p_demux, int i_program )
2866 demux_sys_t *p_sys = p_demux->p_sys;
2867 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2868 int64_t i_stream_size = stream_Size( p_sys->stream );
2870 int i_probe_count = 0;
2873 bool b_found = false;
2877 i_pos = p_sys->i_packet_size * i_probe_count;
2878 i_pos = __MIN( i_pos, i_stream_size );
2880 if( stream_Seek( p_sys->stream, i_pos ) )
2881 return VLC_EGENERIC;
2883 ProbeChunk( p_demux, i_program, false, &i_pcr, &b_found );
2885 /* Go ahead one more chunk if end of file contained only stuffing packets */
2886 i_probe_count += PROBE_CHUNK_COUNT;
2887 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (2 * PROBE_CHUNK_COUNT) );
2889 stream_Seek( p_sys->stream, i_initial_pos );
2891 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2894 static int ProbeEnd( demux_t *p_demux, int i_program )
2896 demux_sys_t *p_sys = p_demux->p_sys;
2897 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2898 int64_t i_stream_size = stream_Size( p_sys->stream );
2900 int i_probe_count = PROBE_CHUNK_COUNT;
2903 bool b_found = false;
2907 i_pos = i_stream_size - (p_sys->i_packet_size * i_probe_count);
2908 i_pos = __MAX( i_pos, 0 );
2910 if( stream_Seek( p_sys->stream, i_pos ) )
2911 return VLC_EGENERIC;
2913 ProbeChunk( p_demux, i_program, true, &i_pcr, &b_found );
2915 /* Go ahead one more chunk if end of file contained only stuffing packets */
2916 i_probe_count += PROBE_CHUNK_COUNT;
2917 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (6 * PROBE_CHUNK_COUNT) );
2919 stream_Seek( p_sys->stream, i_initial_pos );
2921 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2924 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_pmt, mtime_t i_pcr )
2926 demux_sys_t *p_sys = p_demux->p_sys;
2928 /* Check if we have enqueued blocks waiting the/before the
2929 PCR barrier, and then adapt pcr so they have valid PCR when dequeuing */
2930 if( p_pmt->pcr.i_current == -1 )
2932 mtime_t i_mindts = -1;
2934 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2935 for( int i=0; i< p_pat->programs.i_size; i++ )
2937 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2938 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2940 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[j];
2941 block_t *p_block = p_pid->u.p_pes->p_prepcr_outqueue;
2942 while( p_block && p_block->i_dts == VLC_TS_INVALID )
2943 p_block = p_block->p_next;
2945 if( p_block && ( i_mindts == -1 || p_block->i_dts < i_mindts ) )
2946 i_mindts = p_block->i_dts;
2950 if( i_mindts > VLC_TS_INVALID )
2952 msg_Dbg( p_demux, "Program %d PCR prequeue fixup %"PRId64"->%"PRId64,
2953 p_pmt->i_number, TO_SCALE(i_mindts), i_pcr );
2954 i_pcr = TO_SCALE(i_mindts);
2958 p_pmt->pcr.i_current = i_pcr;
2959 if( p_pmt->pcr.i_first == -1 )
2961 p_pmt->pcr.i_first = i_pcr; // now seen
2964 if ( p_sys->i_pmt_es )
2966 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2967 p_pmt->i_number, VLC_TS_0 + i_pcr * 100 / 9 );
2971 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2973 demux_sys_t *p_sys = p_demux->p_sys;
2975 if( p_sys->i_pmt_es <= 0 )
2978 mtime_t i_pcr = GetPCR( p_bk );
2982 pid->probed.i_pcr_count++;
2984 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
2987 /* Search program and set the PCR */
2988 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2989 for( int i = 0; i < p_pat->programs.i_size; i++ )
2991 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2992 mtime_t i_program_pcr = TimeStampWrapAround( p_pmt, i_pcr );
2994 if( p_pmt->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
2996 if( pid->p_parent ) /* PCR shall be on pid itself */
2998 /* ? update PCR for the whole group program ? */
2999 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3003 msg_Warn(p_demux, "discarding PCR update from pid %d which has no owner", pid->i_pid);
3007 else /* set PCR provided by current pid to program(s) referencing it */
3009 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
3010 if( p_pmt->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
3012 /* We've found a target group for update */
3013 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3020 static int FindPCRCandidate( ts_pmt_t *p_pmt )
3022 ts_pid_t *p_cand = NULL;
3023 int i_previous = p_pmt->i_pid_pcr;
3025 for( int i=0; i<p_pmt->e_streams.i_size; i++ )
3027 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[i];
3029 (!p_cand || p_cand->i_pid != i_previous) )
3031 if( p_pid->probed.i_pcr_count ) /* check PCR frequency first */
3033 if( !p_cand || p_pid->probed.i_pcr_count > p_cand->probed.i_pcr_count )
3040 if( p_pid->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3045 else if ( p_pid->u.p_pes->es.fmt.i_cat == VIDEO_ES ) /* Otherwise prioritize video dts */
3047 if( !p_cand || p_cand->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3054 return p_cand->i_pid;
3059 static void PCRFixHandle( demux_t *p_demux, ts_pmt_t *p_pmt, block_t *p_block )
3061 if( p_pmt->pcr.i_first > -1 || p_pmt->pcr.b_disable )
3064 /* Record the first data packet timestamp in case there wont be any PCR */
3065 if( !p_pmt->pcr.i_first_dts )
3067 p_pmt->pcr.i_first_dts = p_block->i_dts;
3069 else if( p_block->i_dts - p_pmt->pcr.i_first_dts > CLOCK_FREQ / 2 ) /* "shall not exceed 100ms" */
3071 int i_cand = FindPCRCandidate( p_pmt );
3072 p_pmt->i_pid_pcr = i_cand;
3073 p_pmt->pcr.b_disable = true; /* So we do not wait packet PCR flag as there might be none on the pid */
3074 msg_Warn( p_demux, "No PCR received for program %d, set up workaround using pid %d",
3075 p_pmt->i_number, i_cand );
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_scrambled = p[3]&0x80;
3084 const bool b_adaptation = p[3]&0x20;
3085 const bool b_payload = p[3]&0x10;
3086 const int i_cc = p[3]&0x0f; /* continuity counter */
3087 bool b_discontinuity = false; /* discontinuity */
3089 /* transport_scrambling_control is ignored */
3094 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
3095 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
3099 /* For now, ignore additional error correction
3100 * TODO: handle Reed-Solomon 204,188 error correction */
3101 p_bk->i_buffer = TS_PACKET_SIZE_188;
3105 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
3107 if( pid->u.p_pes->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
3108 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3111 if( p_demux->p_sys->csa )
3113 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
3114 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
3115 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
3120 /* We don't have any adaptation_field, so payload starts
3121 * immediately after the 4 byte TS header */
3126 /* p[4] is adaptation_field_length minus one */
3130 /* discontinuity indicator found in stream */
3131 b_discontinuity = (p[5]&0x80) ? true : false;
3132 if( b_discontinuity && pid->u.p_pes->p_data )
3134 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
3136 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
3140 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
3145 /* Test continuity counter */
3146 /* continuous when (one of this):
3148 * diff == 0 and payload == 0
3149 * diff == 0 and duplicate packet (playload != 0) <- should we
3150 * test the content ?
3152 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
3153 if( b_payload && i_diff == 1 )
3155 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
3159 if( pid->i_cc == 0xff )
3161 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
3165 else if( i_diff != 0 && !b_discontinuity )
3167 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
3168 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
3171 if( pid->u.p_pes->p_data && pid->u.p_pes->es.fmt.i_cat != VIDEO_ES &&
3172 pid->u.p_pes->es.fmt.i_cat != AUDIO_ES )
3174 /* Small audio/video artifacts are usually better than
3175 * dropping full frames */
3176 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3181 PCRHandle( p_demux, pid, p_bk );
3183 if( i_skip >= 188 || pid->u.p_pes->es.id == NULL )
3185 block_Release( p_bk );
3190 if( !SCRAMBLED(*pid) != !b_scrambled )
3192 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
3193 pid->i_pid, SCRAMBLED(*pid), b_scrambled );
3195 pid->i_flags |= (b_scrambled) ? FLAG_SCRAMBLED : FLAGS_NONE;
3197 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
3199 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
3200 pid->u.p_pes->extra_es.p_elems[i]->id, b_scrambled );
3202 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
3203 pid->u.p_pes->es.id, b_scrambled );
3206 /* We have to gather it */
3207 p_bk->p_buffer += i_skip;
3208 p_bk->i_buffer -= i_skip;
3212 if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
3214 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
3215 block_t *p = block_Duplicate( p_bk );
3218 p->i_buffer = i_pointer_field;
3220 block_ChainLastAppend( &pid->u.p_pes->pp_last, p );
3222 p_bk->i_buffer -= 1 + i_pointer_field;
3223 p_bk->p_buffer += 1 + i_pointer_field;
3225 if( pid->u.p_pes->p_data )
3227 ParseData( p_demux, pid );
3231 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3232 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
3234 if( p_bk->i_buffer > 6 )
3236 pid->u.p_pes->i_data_size = GetWBE( &p_bk->p_buffer[4] );
3237 if( pid->u.p_pes->i_data_size > 0 )
3239 pid->u.p_pes->i_data_size += 6;
3243 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
3245 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
3247 pid->u.p_pes->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
3250 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3251 if( pid->u.p_pes->i_data_size > 0 &&
3252 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3254 ParseData( p_demux, pid );
3260 if( pid->u.p_pes->p_data == NULL )
3262 /* msg_Dbg( p_demux, "broken packet" ); */
3263 block_Release( p_bk );
3267 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3268 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3270 if( pid->u.p_pes->i_data_size > 0 &&
3271 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3273 ParseData( p_demux, pid );
3282 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
3284 switch( i_stream_type )
3286 case 0x01: /* MPEG-1 video */
3287 case 0x02: /* MPEG-2 video */
3288 case 0x80: /* MPEG-2 MOTO video */
3289 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
3291 case 0x03: /* MPEG-1 audio */
3292 case 0x04: /* MPEG-2 audio */
3293 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
3295 case 0x11: /* MPEG4 (audio) LATM */
3296 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
3297 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
3298 transport syntax, such as DST, ALS and SLS */
3299 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
3301 case 0x10: /* MPEG4 (video) */
3302 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
3304 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
3305 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
3307 case 0x24: /* HEVC */
3308 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
3310 case 0x42: /* CAVS (Chinese AVS) */
3311 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
3314 case 0x81: /* A52 (audio) */
3315 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
3317 case 0x82: /* SCTE-27 (sub) */
3318 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
3320 case 0x84: /* SDDS (audio) */
3321 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
3323 case 0x85: /* DTS (audio) */
3324 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
3326 case 0x87: /* E-AC3 */
3327 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
3330 case 0x91: /* A52 vls (audio) */
3331 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
3333 case 0x92: /* DVD_SPU vls (sub) */
3334 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
3337 case 0x94: /* SDDS (audio) */
3338 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
3341 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
3342 es_format_Init( fmt, UNKNOWN_ES, 0 );
3345 case 0x06: /* PES_PRIVATE (fixed later) */
3346 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
3347 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
3349 es_format_Init( fmt, UNKNOWN_ES, 0 );
3353 /* PES packets usually contain truncated frames */
3354 fmt->b_packetized = false;
3357 /*****************************************************************************
3358 * MP4 specific functions (IOD parser)
3359 *****************************************************************************/
3360 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
3363 unsigned int i_len = 0;
3369 i_len = ( i_len << 7 ) + ( i_b&0x7f );
3371 } while( i_b&0x80 && *pi_data > 0 );
3373 if (i_len > *pi_data)
3379 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
3382 while( *pi_data > 0 && bytes-- )
3393 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
3395 int len = IODGetBytes( pi_data, pp_data, 1 );
3398 char *url = strndup( (char*)*pp_data, len );
3404 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
3406 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
3408 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
3417 byte1 = IODGetBytes( &i_data, &p_data, 1 );
3418 byte2 = IODGetBytes( &i_data, &p_data, 1 );
3419 byte3 = IODGetBytes( &i_data, &p_data, 1 );
3420 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
3422 i_iod_label = byte1;
3425 else //correct implementation of the IOD_descriptor
3427 i_iod_label = byte2;
3431 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
3433 if( i_iod_tag != 0x02 )
3435 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
3439 IODDescriptorLength( &i_data, &p_data );
3441 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
3442 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
3443 i_od_id |= i_flags >> 6;
3444 ts_debug( "\n* od_id:%d", i_od_id );
3445 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
3446 if ((i_flags >> 5) & 0x01)
3448 p_iod->psz_url = IODGetURL( &i_data, &p_data );
3449 ts_debug( "\n* url string:%s", p_iod->psz_url );
3450 ts_debug( "\n*****************************\n" );
3455 p_iod->psz_url = NULL;
3458 /* Profile Level Indication */
3459 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
3460 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
3461 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
3462 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
3463 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
3466 int i_data_sav = i_data;
3467 uint8_t *p_data_sav = p_data;
3468 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
3470 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
3472 p_data = p_data_sav + i_length;
3473 i_data = i_data_sav - i_length;
3475 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
3476 i_length = IODDescriptorLength( &i_data, &p_data );
3478 i_data_sav = i_data;
3479 p_data_sav = p_data;
3483 if ( i_tag != 0x03 )
3485 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
3489 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
3490 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
3491 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
3492 if( b_streamDependenceFlag )
3493 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
3495 if( (i_flags >> 6) & 0x01 )
3496 es_descr->psz_url = IODGetURL( &i_data, &p_data );
3498 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
3499 if( b_OCRStreamFlag )
3500 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
3502 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
3504 ts_debug( "\n* ERR missing DecoderConfigDescr" );
3507 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
3508 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
3509 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
3510 i_flags = IODGetBytes( &i_data, &p_data, 1 );
3511 dec_descr->i_streamType = i_flags >> 2;
3513 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
3514 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
3515 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
3517 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
3519 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
3520 if( dec_descr->i_extra > 0 )
3522 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
3523 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
3524 p_data += dec_descr->i_extra;
3525 i_data -= dec_descr->i_extra;
3530 dec_descr->i_extra = 0;
3531 dec_descr->p_extra = NULL;
3534 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
3536 ts_debug( "\n* ERR missing SLConfigDescr" );
3539 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
3540 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
3543 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
3548 es_descr->b_ok = true;
3554 static void IODFree( iod_descriptor_t *p_iod )
3556 if( p_iod->psz_url )
3558 free( p_iod->psz_url );
3563 for( int i = 0; i < 255; i++ )
3565 #define es_descr p_iod->es_descr[i]
3568 if( es_descr.psz_url )
3569 free( es_descr.psz_url );
3571 free( es_descr.dec_descr.p_extra );
3578 /****************************************************************************
3579 ****************************************************************************
3580 ** libdvbpsi callbacks
3581 ****************************************************************************
3582 ****************************************************************************/
3583 static bool ProgramIsSelected( demux_sys_t *p_sys, uint16_t i_pgrm )
3585 for(int i=0; i<p_sys->programs.i_size; i++)
3586 if( p_sys->programs.p_elems[i] == i_pgrm )
3592 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
3594 demux_sys_t *p_sys = p_demux->p_sys;
3596 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
3599 msg_Warn( p_demux, "Switching to non DVB mode" );
3601 /* This doesn't look like a DVB stream so don't try
3602 * parsing the SDT/EDT/TDT */
3604 PIDRelease( p_demux, &p_sys->pid[0x11] );
3605 PIDRelease( p_demux, &p_sys->pid[0x12] );
3606 PIDRelease( p_demux, &p_sys->pid[0x14] );
3607 p_sys->b_dvb_meta = false;
3610 #include "dvb-text.h"
3612 static char *EITConvertToUTF8( demux_t *p_demux,
3613 const unsigned char *psz_instring,
3617 demux_sys_t *p_sys = p_demux->p_sys;
3619 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3621 if ( !p_sys->arib.p_instance )
3622 p_sys->arib.p_instance = arib_instance_new( p_demux );
3623 if ( !p_sys->arib.p_instance )
3625 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3629 char *psz_outstring = NULL;
3632 i_out = i_length * 4;
3633 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3634 if( !psz_outstring )
3637 arib_initialize_decoder( p_decoder );
3638 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3639 psz_outstring, i_out );
3640 arib_finalize_decoder( p_decoder );
3642 return psz_outstring;
3647 /* Deal with no longer broken providers (no switch byte
3648 but sending ISO_8859-1 instead of ISO_6937) without
3649 removing them from the broken providers table
3650 (keep the entry for correctly handling recorded TS).
3652 b_broken = b_broken && i_length && *psz_instring > 0x20;
3655 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3656 return vlc_from_EIT( psz_instring, i_length );
3659 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3661 demux_sys_t *p_sys = p_demux->p_sys;
3662 ts_pid_t *sdt = &p_sys->pid[0x11];
3663 dvbpsi_sdt_service_t *p_srv;
3665 msg_Dbg( p_demux, "SDTCallBack called" );
3667 if( p_sys->es_creation != CREATE_ES ||
3668 !p_sdt->b_current_next ||
3669 p_sdt->i_version == sdt->u.p_psi->i_version )
3671 dvbpsi_sdt_delete( p_sdt );
3675 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3678 p_sdt->i_version, p_sdt->b_current_next,
3679 p_sdt->i_network_id );
3681 p_sys->b_broken_charset = false;
3683 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3686 dvbpsi_descriptor_t *p_dr;
3688 const char *psz_type = NULL;
3689 const char *psz_status = NULL;
3691 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3692 "running=%d free_ca=%d",
3693 p_srv->i_service_id, p_srv->b_eit_schedule,
3694 p_srv->b_eit_present, p_srv->i_running_status,
3697 if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
3699 msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
3700 p_sys->vdr.i_service );
3704 p_meta = vlc_meta_New();
3705 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3707 if( p_dr->i_tag == 0x48 )
3709 static const char *ppsz_type[17] = {
3711 "Digital television service",
3712 "Digital radio sound service",
3714 "NVOD reference service",
3715 "NVOD time-shifted service",
3718 "SECAM coded signal",
3721 "NTSC coded signal",
3722 "Data broadcast service",
3723 "Reserved for Common Interface Usage",
3724 "RCS Map (see EN 301 790 [35])",
3725 "RCS FLS (see EN 301 790 [35])",
3728 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3732 /* Workarounds for broadcasters with broken EPG */
3734 if( p_sdt->i_network_id == 133 )
3735 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3737 /* List of providers using ISO8859-1 */
3738 static const char ppsz_broken_providers[][8] = {
3739 "CSAT", /* CanalSat FR */
3740 "GR1", /* France televisions */
3742 "MR5", /* France 2/M6 HD */
3745 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3747 const size_t i_length = strlen(ppsz_broken_providers[i]);
3748 if( pD->i_service_provider_name_length == i_length &&
3749 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3750 p_sys->b_broken_charset = true;
3753 /* FIXME: Digital+ ES also uses ISO8859-1 */
3755 str1 = EITConvertToUTF8(p_demux,
3756 pD->i_service_provider_name,
3757 pD->i_service_provider_name_length,
3758 p_sys->b_broken_charset );
3759 str2 = EITConvertToUTF8(p_demux,
3761 pD->i_service_name_length,
3762 p_sys->b_broken_charset );
3764 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3765 pD->i_service_type, str1, str2 );
3767 vlc_meta_SetTitle( p_meta, str2 );
3768 vlc_meta_SetPublisher( p_meta, str1 );
3769 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3770 psz_type = ppsz_type[pD->i_service_type];
3776 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3778 static const char *ppsz_status[5] = {
3781 "Starts in a few seconds",
3785 psz_status = ppsz_status[p_srv->i_running_status];
3789 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3791 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3793 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3794 p_srv->i_service_id, p_meta );
3795 vlc_meta_Delete( p_meta );
3798 sdt->u.p_psi->i_version = p_sdt->i_version;
3799 dvbpsi_sdt_delete( p_sdt );
3802 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3803 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3805 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3809 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3810 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3813 /* Count the number of days */
3814 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3815 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3816 for( int i = 70; i < i_year; i++ )
3817 i_day += LEAP(1900+i);
3819 i_day += LEAP(1900+i_year);
3822 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3825 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3827 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3828 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3829 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3831 *p_y = 1900 + yp + c*1;
3832 *p_m = mp - 1 - c*12;
3833 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3835 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3836 static int64_t EITConvertStartTime( uint64_t i_date )
3838 const int i_mjd = i_date >> 24;
3839 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3840 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3841 const int i_second = CVT_FROM_BCD(i_date );
3846 /* if all 40 bits are 1, the start is unknown */
3847 if( i_date == UINT64_C(0xffffffffff) )
3850 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3851 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3853 static int EITConvertDuration( uint32_t i_duration )
3855 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3856 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3857 CVT_FROM_BCD(i_duration );
3861 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3863 demux_sys_t *p_sys = p_demux->p_sys;
3865 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3867 dvbpsi_tot_delete(p_tdt);
3871 static void EITCallBack( demux_t *p_demux,
3872 dvbpsi_eit_t *p_eit, bool b_current_following )
3874 demux_sys_t *p_sys = p_demux->p_sys;
3875 dvbpsi_eit_event_t *p_evt;
3878 msg_Dbg( p_demux, "EITCallBack called" );
3879 if( !p_eit->b_current_next )
3881 dvbpsi_eit_delete( p_eit );
3885 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3886 "ts_id=%d network_id=%d segment_last_section_number=%d "
3889 p_eit->i_version, p_eit->b_current_next,
3890 p_eit->i_ts_id, p_eit->i_network_id,
3891 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3893 p_epg = vlc_epg_New( NULL );
3894 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3896 dvbpsi_descriptor_t *p_dr;
3897 char *psz_name = NULL;
3898 char *psz_text = NULL;
3899 char *psz_extra = strdup("");
3903 int64_t i_tot_time = 0;
3905 i_start = EITConvertStartTime( p_evt->i_start_time );
3906 i_duration = EITConvertDuration( p_evt->i_duration );
3908 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3910 if( p_sys->i_tdt_delta == 0 )
3911 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3913 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3915 tzset(); // JST -> UTC
3916 i_start += timezone; // FIXME: what about DST?
3917 i_tot_time += timezone;
3919 if( p_evt->i_running_status == 0x00 &&
3920 (i_start - 5 < i_tot_time &&
3921 i_tot_time < i_start + i_duration + 5) )
3923 p_evt->i_running_status = 0x04;
3924 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3928 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3929 "running=%d free_ca=%d",
3930 p_evt->i_event_id, (int)i_start, (int)i_duration,
3931 p_evt->i_running_status, p_evt->b_free_ca );
3933 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3939 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3941 /* Only take first description, as we don't handle language-info
3943 if( pE && psz_name == NULL )
3945 psz_name = EITConvertToUTF8( p_demux,
3946 pE->i_event_name, pE->i_event_name_length,
3947 p_sys->b_broken_charset );
3949 psz_text = EITConvertToUTF8( p_demux,
3950 pE->i_text, pE->i_text_length,
3951 p_sys->b_broken_charset );
3952 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3953 pE->i_iso_639_code, psz_name, psz_text );
3960 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3963 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3965 pE->i_descriptor_number, pE->i_last_descriptor_number );
3967 if( pE->i_text_length > 0 )
3969 char *psz_text = EITConvertToUTF8( p_demux,
3970 pE->i_text, pE->i_text_length,
3971 p_sys->b_broken_charset );
3974 msg_Dbg( p_demux, " - text='%s'", psz_text );
3976 psz_extra = xrealloc( psz_extra,
3977 strlen(psz_extra) + strlen(psz_text) + 1 );
3978 strcat( psz_extra, psz_text );
3983 for( int i = 0; i < pE->i_entry_count; i++ )
3985 char *psz_dsc = EITConvertToUTF8( p_demux,
3986 pE->i_item_description[i],
3987 pE->i_item_description_length[i],
3988 p_sys->b_broken_charset );
3989 char *psz_itm = EITConvertToUTF8( p_demux,
3990 pE->i_item[i], pE->i_item_length[i],
3991 p_sys->b_broken_charset );
3993 if( psz_dsc && psz_itm )
3995 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3997 psz_extra = xrealloc( psz_extra,
3998 strlen(psz_extra) + strlen(psz_dsc) +
3999 strlen(psz_itm) + 3 + 1 );
4000 strcat( psz_extra, "(" );
4001 strcat( psz_extra, psz_dsc );
4002 strcat( psz_extra, " " );
4003 strcat( psz_extra, psz_itm );
4004 strcat( psz_extra, ")" );
4016 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
4019 for ( int i = 0; i < pR->i_ratings_number; i++ )
4021 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
4022 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
4024 if ( p_rating->i_rating + 3 > i_min_age )
4025 i_min_age = p_rating->i_rating + 3;
4026 msg_Dbg( p_demux, " - parental control set to %d years",
4035 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
4041 if( i_start > 0 && psz_name && psz_text)
4042 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
4043 *psz_extra ? psz_extra : NULL, i_min_age );
4045 /* Update "now playing" field */
4046 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
4047 vlc_epg_SetCurrent( p_epg, i_start );
4054 if( p_epg->i_event > 0 )
4056 if( b_current_following &&
4057 ( p_sys->programs.i_size == 0 ||
4058 p_sys->programs.p_elems[0] ==
4062 p_sys->i_dvb_length = 0;
4063 p_sys->i_dvb_start = 0;
4065 if( p_epg->p_current )
4067 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
4068 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
4071 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
4075 vlc_epg_Delete( p_epg );
4077 dvbpsi_eit_delete( p_eit );
4079 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4081 EITCallBack( p_demux, p_eit, true );
4083 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4085 EITCallBack( p_demux, p_eit, false );
4088 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
4089 uint16_t i_extension, demux_t *p_demux )
4091 demux_sys_t *p_sys = p_demux->p_sys;
4094 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4095 i_table_id, i_table_id, i_extension, i_extension );
4097 if( p_sys->pid[0].u.p_pat->i_version != -1 && i_table_id == 0x42 )
4099 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4100 i_table_id, i_table_id, i_extension, i_extension );
4102 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
4103 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
4105 else if( p_sys->pid[0x11].u.p_psi->i_version != -1 &&
4106 ( i_table_id == 0x4e || /* Current/Following */
4107 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
4109 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4110 i_table_id, i_table_id, i_extension, i_extension );
4112 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
4113 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
4114 (dvbpsi_eit_callback)EITCallBackSchedule;
4116 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
4117 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
4119 else if( p_demux->p_sys->pid[0x11].u.p_psi->i_version != -1 &&
4120 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
4122 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4123 i_table_id, i_table_id, i_extension, i_extension );
4125 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
4126 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
4130 /*****************************************************************************
4131 * PMT callback and helpers
4132 *****************************************************************************/
4133 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
4136 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
4137 while( p_dr && ( p_dr->i_tag != i_tag ) )
4138 p_dr = p_dr->p_next;
4141 static bool PMTEsHasRegistration( demux_t *p_demux,
4142 const dvbpsi_pmt_es_t *p_es,
4143 const char *psz_tag )
4145 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
4149 if( p_dr->i_length < 4 )
4151 msg_Warn( p_demux, "invalid Registration Descriptor" );
4155 assert( strlen(psz_tag) == 4 );
4156 return !memcmp( p_dr->p_data, psz_tag, 4 );
4159 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
4160 int i_component_tag )
4162 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4165 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4169 return p_si->i_component_tag == i_component_tag;
4172 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pes_es_t *p_es,
4173 const ts_pmt_t *p_pmt, const dvbpsi_pmt_es_t *p_dvbpsies )
4175 es_format_t *p_fmt = &p_es->fmt;
4177 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
4178 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x1f );
4180 if( p_dr && p_dr->i_length == 2 )
4182 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
4184 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
4186 p_es->p_mpeg4desc = NULL;
4188 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
4190 iod_descriptor_t *iod = p_pmt->iod;
4191 if( iod->es_descr[i].i_es_id == i_es_id )
4193 if ( iod->es_descr[i].b_ok )
4194 p_es->p_mpeg4desc = &iod->es_descr[i];
4196 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
4201 if( !p_es->p_mpeg4desc )
4203 switch( p_dvbpsies->i_type )
4205 /* non fatal, set by packetizer */
4206 case 0x0f: /* ADTS */
4207 case 0x11: /* LOAS */
4208 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4209 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4212 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4213 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4219 const decoder_config_descriptor_t *dcd = &p_es->p_mpeg4desc->dec_descr;
4220 if( dcd->i_streamType == 0x04 ) /* VisualStream */
4222 p_fmt->i_cat = VIDEO_ES;
4223 switch( dcd->i_objectTypeIndication )
4225 case 0x0B: /* mpeg4 sub */
4226 p_fmt->i_cat = SPU_ES;
4227 p_fmt->i_codec = VLC_CODEC_SUBT;
4230 case 0x20: /* mpeg4 */
4231 p_fmt->i_codec = VLC_CODEC_MP4V;
4233 case 0x21: /* h264 */
4234 p_fmt->i_codec = VLC_CODEC_H264;
4241 case 0x65: /* mpeg2 */
4242 p_fmt->i_codec = VLC_CODEC_MPGV;
4244 case 0x6a: /* mpeg1 */
4245 p_fmt->i_codec = VLC_CODEC_MPGV;
4247 case 0x6c: /* mpeg1 */
4248 p_fmt->i_codec = VLC_CODEC_JPEG;
4251 p_fmt->i_cat = UNKNOWN_ES;
4255 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
4257 p_fmt->i_cat = AUDIO_ES;
4258 switch( dcd->i_objectTypeIndication )
4260 case 0x40: /* mpeg4 */
4261 p_fmt->i_codec = VLC_CODEC_MP4A;
4265 case 0x68: /* mpeg2 aac */
4266 p_fmt->i_codec = VLC_CODEC_MP4A;
4268 case 0x69: /* mpeg2 */
4269 p_fmt->i_codec = VLC_CODEC_MPGA;
4271 case 0x6b: /* mpeg1 */
4272 p_fmt->i_codec = VLC_CODEC_MPGA;
4275 p_fmt->i_cat = UNKNOWN_ES;
4281 p_fmt->i_cat = UNKNOWN_ES;
4284 if( p_fmt->i_cat != UNKNOWN_ES )
4286 p_fmt->i_extra = dcd->i_extra;
4287 if( p_fmt->i_extra > 0 )
4289 p_fmt->p_extra = malloc( p_fmt->i_extra );
4290 if( p_fmt->p_extra )
4291 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
4304 } ts_teletext_page_t;
4306 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pes_t *p_pes,
4307 const dvbpsi_pmt_es_t *p_dvbpsies )
4309 es_format_t *p_fmt = &p_pes->es.fmt;
4311 ts_teletext_page_t p_page[2 * 64 + 20];
4312 unsigned i_page = 0;
4314 /* Gather pages information */
4315 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
4317 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, i_tag_idx == 0 ? 0x46 : 0x56 );
4321 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
4325 for( int i = 0; i < p_sub->i_pages_number; i++ )
4327 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
4329 if( p_src->i_teletext_type >= 0x06 )
4332 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4334 ts_teletext_page_t *p_dst = &p_page[i_page++];
4336 p_dst->i_type = p_src->i_teletext_type;
4337 p_dst->i_magazine = p_src->i_teletext_magazine_number
4338 ? p_src->i_teletext_magazine_number : 8;
4339 p_dst->i_page = p_src->i_teletext_page_number;
4340 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4344 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4347 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4348 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4350 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
4352 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
4355 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4357 ts_teletext_page_t *p_dst = &p_page[i_page++];
4359 switch( p_src->i_subtitling_type )
4362 p_dst->i_type = 0x02;
4365 p_dst->i_type = 0x03;
4368 /* FIXME check if it is the right split */
4369 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
4370 ? (p_src->i_composition_page_id >> 8) : 8;
4371 p_dst->i_page = p_src->i_composition_page_id & 0xff;
4372 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4377 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
4379 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4381 p_fmt->subs.teletext.i_magazine = -1;
4382 p_fmt->subs.teletext.i_page = 0;
4383 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
4385 dvbpsi_descriptor_t *p_dr;
4386 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x46 );
4388 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x56 );
4390 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4392 /* Descriptor pass-through */
4393 p_fmt->p_extra = malloc( p_dr->i_length );
4394 if( p_fmt->p_extra )
4396 p_fmt->i_extra = p_dr->i_length;
4397 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4403 for( unsigned i = 0; i < i_page; i++ )
4405 ts_pes_es_t *p_page_es;
4410 p_page_es = &p_pes->es;
4414 p_page_es = malloc( sizeof(*p_page_es) );
4418 es_format_Copy( &p_page_es->fmt, p_fmt );
4419 free( p_page_es->fmt.psz_language );
4420 free( p_page_es->fmt.psz_description );
4421 p_page_es->fmt.psz_language = NULL;
4422 p_page_es->fmt.psz_description = NULL;
4424 ARRAY_APPEND( p_pes->extra_es, p_page_es );
4428 const ts_teletext_page_t *p = &p_page[i];
4429 p_page_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
4430 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
4431 p_page_es->fmt.psz_language = strndup( p->p_iso639, 3 );
4432 p_page_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
4433 p_page_es->fmt.subs.teletext.i_magazine = p->i_magazine;
4434 p_page_es->fmt.subs.teletext.i_page = p->i_page;
4437 " * ttxt type=%s lan=%s page=%d%02x",
4438 p_page_es->fmt.psz_description,
4439 p_page_es->fmt.psz_language,
4440 p->i_magazine, p->i_page );
4444 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pes_t *p_pes,
4445 const dvbpsi_pmt_es_t *p_dvbpsies )
4447 es_format_t *p_fmt = &p_pes->es.fmt;
4449 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
4451 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4453 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4454 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4456 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
4457 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
4458 ( i_type >= 0x20 && i_type <= 0x24 ) )
4462 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4464 p_fmt->subs.dvb.i_id = -1;
4465 p_fmt->psz_description = strdup( _("DVB subtitles") );
4467 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4469 /* Descriptor pass-through */
4470 p_fmt->p_extra = malloc( p_dr->i_length );
4471 if( p_fmt->p_extra )
4473 p_fmt->i_extra = p_dr->i_length;
4474 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4480 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4482 ts_pes_es_t *p_subs_es;
4487 p_subs_es = &p_pes->es;
4491 p_subs_es = malloc( sizeof(*p_subs_es) );
4495 es_format_Copy( &p_subs_es->fmt, p_fmt );
4496 free( p_subs_es->fmt.psz_language );
4497 free( p_subs_es->fmt.psz_description );
4498 p_subs_es->fmt.psz_language = NULL;
4499 p_subs_es->fmt.psz_description = NULL;
4501 ARRAY_APPEND( p_pes->extra_es, p_subs_es );
4505 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
4506 p_subs_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
4507 switch( p->i_subtitling_type )
4509 case 0x10: /* unspec. */
4510 case 0x11: /* 4:3 */
4511 case 0x12: /* 16:9 */
4512 case 0x13: /* 2.21:1 */
4513 case 0x14: /* HD monitor */
4514 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles") );
4516 case 0x20: /* Hearing impaired unspec. */
4517 case 0x21: /* h.i. 4:3 */
4518 case 0x22: /* h.i. 16:9 */
4519 case 0x23: /* h.i. 2.21:1 */
4520 case 0x24: /* h.i. HD monitor */
4521 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
4528 p_subs_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
4529 ( p->i_ancillary_page_id << 16 );
4534 static int vlc_ceil_log2( const unsigned int val )
4536 int n = 31 - clz(val);
4537 if ((1U << n) != val)
4543 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
4547 /* default mapping */
4548 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
4549 memcpy(h.stream_map, map, sizeof(map));
4553 int stream_count = 0;
4554 int ccc = p[1]; // channel_config_code
4558 mapping = channels > 2;
4561 channels = 2; // dual mono
4563 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
4564 csc = p_csc[channels - 1];
4565 stream_count = channels - csc;
4567 static const uint8_t map[6][7] = {
4576 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4577 } else if (ccc == 0x81) {
4579 goto explicit_config_too_short;
4586 bs_init(&s, &p[4], len - 4);
4589 int bits = vlc_ceil_log2(channels);
4590 if (s.i_left < bits)
4591 goto explicit_config_too_short;
4592 stream_count = bs_read(&s, bits) + 1;
4593 bits = vlc_ceil_log2(stream_count + 1);
4594 if (s.i_left < bits)
4595 goto explicit_config_too_short;
4596 csc = bs_read(&s, bits);
4598 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4599 if (s.i_left < channels * channel_bits)
4600 goto explicit_config_too_short;
4602 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4603 for (int i = 0; i < channels; i++) {
4604 unsigned char m = bs_read(&s, channel_bits);
4607 h.stream_map[i] = m;
4610 } else if (ccc >= 0x80 && ccc <= 0x88) {
4611 channels = ccc - 0x80;
4616 channels = 2; // dual mono
4619 stream_count = channels;
4621 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4625 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4629 opus_prepare_header(channels, 0, &h);
4631 h.input_sample_rate = 48000;
4633 h.nb_streams = channels - csc;
4634 h.channel_mapping = mapping;
4637 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4638 if (p_fmt->p_extra) {
4639 p_fmt->i_cat = AUDIO_ES;
4640 p_fmt->i_codec = VLC_CODEC_OPUS;
4641 p_fmt->audio.i_channels = h.channels;
4642 p_fmt->audio.i_rate = 48000;
4648 explicit_config_too_short:
4649 msg_Err(demux, "Opus descriptor too short");
4652 static void PMTSetupEs0x06( demux_t *p_demux, ts_pes_t *p_pes,
4653 const dvbpsi_pmt_es_t *p_dvbpsies )
4655 es_format_t *p_fmt = &p_pes->es.fmt;
4656 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4657 dvbpsi_descriptor_t *desc;
4659 if( PMTEsHasRegistration( p_demux, p_dvbpsies, "AC-3" ) ||
4660 PMTEsFindDescriptor( p_dvbpsies, 0x6a ) ||
4661 PMTEsFindDescriptor( p_dvbpsies, 0x81 ) )
4663 p_fmt->i_cat = AUDIO_ES;
4664 p_fmt->i_codec = VLC_CODEC_A52;
4666 else if( (desc = PMTEsFindDescriptor( p_dvbpsies, 0x7f ) ) && desc->i_length >= 2 &&
4667 PMTEsHasRegistration(p_demux, p_dvbpsies, "Opus"))
4669 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4671 else if( PMTEsFindDescriptor( p_dvbpsies, 0x7a ) )
4673 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4674 p_fmt->i_cat = AUDIO_ES;
4675 p_fmt->i_codec = VLC_CODEC_EAC3;
4677 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS1" ) ||
4678 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS2" ) ||
4679 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS3" ) ||
4680 PMTEsFindDescriptor( p_dvbpsies, 0x73 ) )
4682 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4683 p_fmt->i_cat = AUDIO_ES;
4684 p_fmt->i_codec = VLC_CODEC_DTS;
4686 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "BSSD" ) && !p_subs_dr )
4688 /* BSSD is AES3 DATA, but could also be subtitles
4689 * we need to check for secondary descriptor then s*/
4690 p_fmt->i_cat = AUDIO_ES;
4691 p_fmt->b_packetized = true;
4692 p_fmt->i_codec = VLC_CODEC_302M;
4694 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "HEVC" ) )
4696 p_fmt->i_cat = VIDEO_ES;
4697 p_fmt->i_codec = VLC_CODEC_HEVC;
4699 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4701 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4702 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xFD );
4703 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4704 if ( p_dr && p_dr->i_length >= 2 )
4706 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4707 PMTEsHasComponentTag( p_dvbpsies, 0x30 ) ||
4708 PMTEsHasComponentTag( p_dvbpsies, 0x31 ) ||
4709 PMTEsHasComponentTag( p_dvbpsies, 0x32 ) ||
4710 PMTEsHasComponentTag( p_dvbpsies, 0x33 ) ||
4711 PMTEsHasComponentTag( p_dvbpsies, 0x34 ) ||
4712 PMTEsHasComponentTag( p_dvbpsies, 0x35 ) ||
4713 PMTEsHasComponentTag( p_dvbpsies, 0x36 ) ||
4714 PMTEsHasComponentTag( p_dvbpsies, 0x37 ) ) )
4716 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_A );
4717 p_fmt->psz_language = strndup ( "jpn", 3 );
4718 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4720 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4721 PMTEsHasComponentTag( p_dvbpsies, 0x87 ) ||
4722 PMTEsHasComponentTag( p_dvbpsies, 0x88 ) ) )
4724 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_C );
4725 p_fmt->psz_language = strndup ( "jpn", 3 );
4726 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4732 /* Subtitle/Teletext/VBI fallbacks */
4733 dvbpsi_subtitling_dr_t *p_sub;
4734 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4736 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4738 if( p_fmt->i_cat != UNKNOWN_ES )
4741 switch( p_sub->p_subtitle[i].i_subtitling_type )
4743 case 0x01: /* EBU Teletext subtitles */
4744 case 0x02: /* Associated EBU Teletext */
4745 case 0x03: /* VBI data */
4746 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4748 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4749 case 0x11: /* ... on 4:3 AR monitor */
4750 case 0x12: /* ... on 16:9 AR monitor */
4751 case 0x13: /* ... on 2.21:1 AR monitor */
4752 case 0x14: /* ... for display on a high definition monitor */
4753 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4754 case 0x21: /* ... on 4:3 AR monitor */
4755 case 0x22: /* ... on 16:9 AR monitor */
4756 case 0x23: /* ... on 2.21:1 AR monitor */
4757 case 0x24: /* ... for display on a high definition monitor */
4758 PMTSetupEsDvbSubtitle( p_demux, p_pes, p_dvbpsies );
4761 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4762 p_sub->p_subtitle[i].i_subtitling_type );
4768 if( p_fmt->i_cat == UNKNOWN_ES &&
4769 ( PMTEsFindDescriptor( p_dvbpsies, 0x45 ) || /* VBI Data descriptor */
4770 PMTEsFindDescriptor( p_dvbpsies, 0x46 ) || /* VBI Teletext descriptor */
4771 PMTEsFindDescriptor( p_dvbpsies, 0x56 ) ) ) /* EBU Teletext descriptor */
4774 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4778 /* FIXME is it useful ? */
4779 if( PMTEsFindDescriptor( p_dvbpsies, 0x52 ) )
4781 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x52 );
4782 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4784 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4788 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pes_es_t *p_es,
4789 const dvbpsi_pmt_es_t *p_dvbpsies )
4791 /* Registration Descriptor */
4792 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "VC-1" ) )
4794 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4798 es_format_t *p_fmt = &p_es->fmt;
4800 /* registration descriptor for VC-1 (SMPTE rp227) */
4801 p_fmt->i_cat = VIDEO_ES;
4802 p_fmt->i_codec = VLC_CODEC_VC1;
4804 /* XXX With Simple and Main profile the SEQUENCE
4805 * header is modified: video width and height are
4806 * inserted just after the start code as 2 int16_t
4807 * The packetizer will take care of that. */
4810 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pes_es_t *p_es,
4811 const dvbpsi_pmt_es_t *p_dvbpsies )
4813 /* Registration Descriptor */
4814 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "drac" ) )
4816 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4820 es_format_t *p_fmt = &p_es->fmt;
4822 /* registration descriptor for Dirac
4823 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4824 p_fmt->i_cat = VIDEO_ES;
4825 p_fmt->i_codec = VLC_CODEC_DIRAC;
4828 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pes_es_t *p_es,
4829 const dvbpsi_pmt_es_t *p_dvbpsies )
4831 /* MSCODEC sent by vlc */
4832 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xa0 );
4833 if( !p_dr || p_dr->i_length < 10 )
4836 "private MSCODEC (vlc) without bih private descriptor" );
4840 es_format_t *p_fmt = &p_es->fmt;
4841 p_fmt->i_cat = VIDEO_ES;
4842 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4843 p_dr->p_data[2], p_dr->p_data[3] );
4844 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4845 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4846 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4848 if( p_fmt->i_extra > 0 )
4850 p_fmt->p_extra = malloc( p_fmt->i_extra );
4851 if( p_fmt->p_extra )
4852 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4853 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4857 /* For such stream we will gather them ourself and don't launch a
4859 * Yes it's ugly but it's the only way to have DIV3 working */
4860 p_fmt->b_packetized = true;
4863 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pes_es_t *p_es, int i_pid )
4865 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4866 * with audio track pid being 0x1100..0x11FF */
4867 if ( p_pmt->i_program_number == 0x1 &&
4868 p_pmt->i_pcr_pid == 0x1000 &&
4869 ( i_pid >> 8 ) == 0x11 )
4871 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4872 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4875 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4878 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pes_es_t *p_es,
4879 const dvbpsi_pmt_es_t *p_dvbpsies )
4881 es_format_t *p_fmt = &p_es->fmt;
4883 /* Blu-Ray mapping */
4884 switch( p_dvbpsies->i_type )
4887 p_fmt->i_cat = AUDIO_ES;
4888 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4891 p_fmt->i_cat = AUDIO_ES;
4892 p_fmt->i_codec = VLC_CODEC_A52;
4895 case 0x85: /* DTS-HD High resolution audio */
4896 case 0x86: /* DTS-HD Master audio */
4897 case 0xA2: /* Secondary DTS audio */
4898 p_fmt->i_cat = AUDIO_ES;
4899 p_fmt->i_codec = VLC_CODEC_DTS;
4902 case 0x83: /* TrueHD AC3 */
4903 p_fmt->i_cat = AUDIO_ES;
4904 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4907 case 0x84: /* E-AC3 */
4908 case 0xA1: /* Secondary E-AC3 */
4909 p_fmt->i_cat = AUDIO_ES;
4910 p_fmt->i_codec = VLC_CODEC_EAC3;
4912 case 0x90: /* Presentation graphics */
4913 p_fmt->i_cat = SPU_ES;
4914 p_fmt->i_codec = VLC_CODEC_BD_PG;
4916 case 0x91: /* Interactive graphics */
4917 case 0x92: /* Subtitle */
4920 p_fmt->i_cat = VIDEO_ES;
4921 p_fmt->i_codec = VLC_CODEC_VC1;
4924 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4925 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4932 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pes_es_t *p_es,
4933 const dvbpsi_pmt_es_t *p_dvbpsies )
4939 vlc_fourcc_t i_codec;
4941 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4942 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4943 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4944 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4945 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4946 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4947 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4948 { "", UNKNOWN_ES, 0 }
4950 es_format_t *p_fmt = &p_es->fmt;
4952 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4954 if( PMTEsHasRegistration( p_demux, p_dvbpsies, p_regs[i].psz_tag ) )
4956 p_fmt->i_cat = p_regs[i].i_cat;
4957 p_fmt->i_codec = p_regs[i].i_codec;
4958 if (p_dvbpsies->i_type == 0x87)
4959 p_fmt->i_codec = VLC_CODEC_EAC3;
4966 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4968 static const char *audio_type[] = {
4970 N_("clean effects"),
4971 N_("hearing impaired"),
4972 N_("visual impaired commentary"),
4975 if (type < 0 || type > 3)
4976 msg_Dbg( p_demux, "unknown audio type: %d", type);
4978 return strdup(audio_type[type]);
4982 static void PMTParseEsIso639( demux_t *p_demux, ts_pes_es_t *p_es,
4983 const dvbpsi_pmt_es_t *p_dvbpsies )
4985 /* get language descriptor */
4986 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x0a );
4991 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4994 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4998 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4999 p_es->fmt.psz_language = malloc( 4 );
5000 if( p_es->fmt.psz_language )
5002 memcpy( p_es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
5003 p_es->fmt.psz_language[3] = 0;
5004 msg_Dbg( p_demux, "found language: %s", p_es->fmt.psz_language);
5006 int type = p_decoded->code[0].i_audio_type;
5007 p_es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
5009 p_es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
5011 p_es->fmt.i_extra_languages = p_decoded->i_code_count-1;
5012 if( p_es->fmt.i_extra_languages > 0 )
5013 p_es->fmt.p_extra_languages =
5014 malloc( sizeof(*p_es->fmt.p_extra_languages) *
5015 p_es->fmt.i_extra_languages );
5016 if( p_es->fmt.p_extra_languages )
5018 for( unsigned i = 0; i < p_es->fmt.i_extra_languages; i++ )
5020 p_es->fmt.p_extra_languages[i].psz_language = malloc(4);
5021 if( p_es->fmt.p_extra_languages[i].psz_language )
5023 memcpy( p_es->fmt.p_extra_languages[i].psz_language,
5024 p_decoded->code[i+1].iso_639_code, 3 );
5025 p_es->fmt.p_extra_languages[i].psz_language[3] = '\0';
5027 int type = p_decoded->code[i].i_audio_type;
5028 p_es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
5032 p_es->fmt.psz_language = malloc( 4 );
5033 if( p_es->fmt.psz_language )
5035 memcpy( p_es->fmt.psz_language,
5036 p_decoded->i_iso_639_code, 3 );
5037 p_es->fmt.psz_language[3] = 0;
5042 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid )
5044 demux_sys_t *p_sys = p_demux->p_sys;
5045 bool b_create_delayed = false;
5049 if( SEEN(*pid) && p_sys->es_creation == DELAY_ES )
5051 p_sys->es_creation = CREATE_ES;
5052 b_create_delayed = true;
5055 if( p_sys->es_creation == CREATE_ES )
5057 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
5058 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
5060 pid->u.p_pes->extra_es.p_elems[i]->id =
5061 es_out_Add( p_demux->out, &pid->u.p_pes->extra_es.p_elems[i]->fmt );
5063 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
5064 UpdatePESFilters( p_demux, p_sys->b_es_all );
5067 else if( p_sys->es_creation == DELAY_ES )
5069 p_sys->es_creation = CREATE_ES;
5070 b_create_delayed = true;
5073 if( b_create_delayed )
5075 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5076 for( int i=0; i< p_pat->programs.i_size; i++ )
5078 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
5079 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
5081 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
5082 if( pid->u.p_pes->es.id )
5085 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
5086 for( int k = 0; k < pid->u.p_pes->extra_es.i_size; k++ )
5088 pid->u.p_pes->extra_es.p_elems[k]->id =
5089 es_out_Add( p_demux->out, &pid->u.p_pes->extra_es.p_elems[k]->fmt );
5091 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
5093 if( pid->u.p_pes->es.id != NULL && ProgramIsSelected( p_sys, pid->p_parent->u.p_pmt->i_number ) )
5094 SetPIDFilter( p_sys, pid, true );
5097 UpdatePESFilters( p_demux, p_sys->b_es_all );
5101 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_dvbpsipmt )
5103 demux_t *p_demux = data;
5104 demux_sys_t *p_sys = p_demux->p_sys;
5106 ts_pid_t *pmtpid = NULL;
5107 ts_pmt_t *p_pmt = NULL;
5109 msg_Dbg( p_demux, "PMTCallBack called" );
5111 if (unlikely(p_sys->pid[0].type != TYPE_PAT))
5113 assert(p_sys->pid[0].type == TYPE_PAT);
5114 dvbpsi_pmt_delete(p_dvbpsipmt);
5117 const ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5119 /* First find this PMT declared in PAT */
5120 for( int i = 0; !pmtpid && i < p_pat->programs.i_size; i++ )
5122 const int i_pmt_prgnumber = p_pat->programs.p_elems[i]->u.p_pmt->i_number;
5123 if( i_pmt_prgnumber != TS_USER_PMT_NUMBER &&
5124 i_pmt_prgnumber == p_dvbpsipmt->i_program_number )
5126 pmtpid = p_pat->programs.p_elems[i];
5127 assert(pmtpid->type == TYPE_PMT);
5128 p_pmt = pmtpid->u.p_pmt;
5132 if( pmtpid == NULL )
5134 msg_Warn( p_demux, "unreferenced program (broken stream)" );
5135 dvbpsi_pmt_delete(p_dvbpsipmt);
5139 pmtpid->i_flags |= FLAG_SEEN;
5141 if( p_pmt->i_version != -1 &&
5142 ( !p_dvbpsipmt->b_current_next || p_pmt->i_version == p_dvbpsipmt->i_version ) )
5144 dvbpsi_pmt_delete( p_dvbpsipmt );
5148 /* Save old es array */
5149 DECL_ARRAY(ts_pid_t *) old_es_rm;
5150 old_es_rm.i_alloc = p_pmt->e_streams.i_alloc;
5151 old_es_rm.i_size = p_pmt->e_streams.i_size;
5152 old_es_rm.p_elems = p_pmt->e_streams.p_elems;
5153 ARRAY_INIT(p_pmt->e_streams);
5157 IODFree( p_pmt->iod );
5161 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
5162 p_dvbpsipmt->i_program_number, p_dvbpsipmt->i_version, p_dvbpsipmt->i_pcr_pid );
5163 p_pmt->i_pid_pcr = p_dvbpsipmt->i_pcr_pid;
5164 p_pmt->i_version = p_dvbpsipmt->i_version;
5166 ValidateDVBMeta( p_demux, p_pmt->i_pid_pcr );
5168 if( ProgramIsSelected( p_sys, p_pmt->i_number ) )
5169 SetPIDFilter( p_sys, &p_sys->pid[p_pmt->i_pid_pcr], true ); /* Set demux filter */
5171 /* Parse PMT descriptors */
5172 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
5173 dvbpsi_descriptor_t *p_dr;
5175 /* First pass for standard detection */
5176 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
5178 int i_arib_flags = 0; /* Descriptors can be repeated */
5179 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5185 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
5186 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
5190 i_arib_flags |= 1 << 1;
5193 i_arib_flags |= 1 << 2;
5199 if ( i_arib_flags == 0x07 ) //0b111
5200 p_sys->arib.e_mode = ARIBMODE_ENABLED;
5203 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5205 /* special descriptors handling */
5208 case 0x1d: /* We have found an IOD descriptor */
5209 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
5210 p_pmt->iod = IODNew( p_dr->i_length, p_dr->p_data );
5214 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5215 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5218 case 0x5: /* Registration Descriptor */
5219 if( p_dr->i_length != 4 )
5221 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
5225 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
5226 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
5227 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
5232 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
5236 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
5239 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
5240 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
5241 /* TODO: apply visibility flags */
5245 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
5249 dvbpsi_pmt_es_t *p_dvbpsies;
5250 for( p_dvbpsies = p_dvbpsipmt->p_first_es; p_dvbpsies != NULL; p_dvbpsies = p_dvbpsies->p_next )
5252 bool b_reusing_pid = false;
5255 ts_pid_t *pespid = &p_sys->pid[p_dvbpsies->i_pid];
5257 /* Find out if the PID was already declared */
5258 for( int i = 0; i < old_es_rm.i_size; i++ )
5260 if( old_es_rm.p_elems[i]->i_pid == p_dvbpsies->i_pid )
5262 b_reusing_pid = true;
5266 ValidateDVBMeta( p_demux, p_dvbpsies->i_pid );
5268 char const * psz_typedesc = "";
5269 switch(p_dvbpsies->i_type)
5272 psz_typedesc = "ISO/IEC Reserved";
5275 psz_typedesc = "ISO/IEC 11172 Video";
5278 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
5281 psz_typedesc = "ISO/IEC 11172 Audio";
5284 psz_typedesc = "ISO/IEC 13818-3 Audio";
5287 psz_typedesc = "ISO/IEC 13818-1 private_sections";
5290 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
5293 psz_typedesc = "ISO/IEC 13522 MHEG";
5296 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
5299 psz_typedesc = "ITU-T Rec. H.222.1";
5302 psz_typedesc = "ISO/IEC 13818-6 type A";
5305 psz_typedesc = "ISO/IEC 13818-6 type B";
5308 psz_typedesc = "ISO/IEC 13818-6 type C";
5311 psz_typedesc = "ISO/IEC 13818-6 type D";
5314 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
5317 if (p_dvbpsies->i_type >= 0x0F && p_dvbpsies->i_type <=0x7F)
5318 psz_typedesc = "ISO/IEC 13818-1 Reserved";
5320 psz_typedesc = "User Private";
5323 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
5324 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5326 for( p_dr = p_dvbpsies->p_first_descriptor; p_dr != NULL;
5327 p_dr = p_dr->p_next )
5329 msg_Dbg( p_demux, " - descriptor tag 0x%x",
5333 if ( !PIDSetup( p_demux, TYPE_PES, pespid, pmtpid ) )
5335 msg_Warn( p_demux, " * pid=%d type=0x%x %s (skipped)",
5336 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5343 p_pes = ts_pes_New( p_demux );
5349 p_pes = pespid->u.p_pes;
5353 ARRAY_APPEND( p_pmt->e_streams, pespid );
5355 PIDFillFormat( &p_pes->es.fmt, p_dvbpsies->i_type );
5357 pespid->i_flags |= SEEN(p_sys->pid[p_dvbpsies->i_pid]);
5359 bool b_registration_applied = false;
5360 if ( p_dvbpsies->i_type >= 0x80 ) /* non standard, extensions */
5362 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
5364 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, &p_pes->es, p_dvbpsies ) ))
5365 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
5366 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5370 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, &p_pes->es, p_dvbpsies ) ))
5371 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
5372 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5376 if ( !b_registration_applied )
5378 switch( p_dvbpsies->i_type )
5381 /* Handle PES private data */
5382 PMTSetupEs0x06( p_demux, p_pes, p_dvbpsies );
5384 /* All other private or reserved types */
5389 PMTSetupEsISO14496( p_demux, &p_pes->es, p_pmt, p_dvbpsies );
5393 PMTSetupEs0x83( p_dvbpsipmt, &p_pes->es, p_dvbpsies->i_pid );
5396 PMTSetupEs0xA0( p_demux, &p_pes->es, p_dvbpsies );
5399 PMTSetupEs0xD1( p_demux, &p_pes->es, p_dvbpsies );
5402 PMTSetupEs0xEA( p_demux, &p_pes->es, p_dvbpsies );
5408 if( p_pes->es.fmt.i_cat == AUDIO_ES ||
5409 ( p_pes->es.fmt.i_cat == SPU_ES &&
5410 p_pes->es.fmt.i_codec != VLC_CODEC_DVBS &&
5411 p_pes->es.fmt.i_codec != VLC_CODEC_TELETEXT ) )
5413 PMTParseEsIso639( p_demux, &p_pes->es, p_dvbpsies );
5416 switch( p_pes->es.fmt.i_codec )
5418 case VLC_CODEC_SCTE_27:
5419 p_pes->data_type = TS_ES_DATA_TABLE_SECTION;
5422 //pid->es->data_type = TS_ES_DATA_PES;
5426 p_pes->es.fmt.i_group = p_dvbpsipmt->i_program_number;
5427 for( int i = 0; i < p_pes->extra_es.i_size; i++ )
5428 p_pes->extra_es.p_elems[i]->fmt.i_group = p_dvbpsipmt->i_program_number;
5430 if( p_pes->es.fmt.i_cat == UNKNOWN_ES )
5432 msg_Dbg( p_demux, " => pid %d content is *unknown*",
5433 p_dvbpsies->i_pid );
5434 p_pes->es.fmt.psz_description = strdup( psz_typedesc );
5438 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
5439 p_dvbpsies->i_pid, (char*)&p_pes->es.fmt.i_codec );
5441 if( p_sys->b_es_id_pid ) p_pes->es.fmt.i_id = p_dvbpsies->i_pid;
5443 /* Check if we can avoid restarting the ES */
5446 /* p_pes points to a tmp pes */
5447 if( pespid->u.p_pes->es.fmt.i_codec != p_pes->es.fmt.i_codec ||
5448 pespid->u.p_pes->es.fmt.i_extra != p_pes->es.fmt.i_extra ||
5449 pespid->u.p_pes->es.fmt.i_extra != 0 ||
5450 pespid->u.p_pes->extra_es.i_size != p_pes->extra_es.i_size ||
5451 !( ( !pespid->u.p_pes->es.fmt.psz_language &&
5452 !p_pes->es.fmt.psz_language ) ||
5453 ( pespid->u.p_pes->es.fmt.psz_language &&
5454 p_pes->es.fmt.psz_language &&
5455 !strcmp( pespid->u.p_pes->es.fmt.psz_language,
5456 p_pes->es.fmt.psz_language ) ) ) )
5458 /* Differs, swap then */
5459 ts_pes_t *e = pespid->u.p_pes;
5460 pespid->u.p_pes = p_pes;
5463 /* p_pes still tmp, but now contains old config */
5464 pespid->u.p_pes->es.id = p_pes->es.id;
5465 if( pespid->u.p_pes->es.id )
5467 p_pes->es.id = NULL;
5468 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5469 pespid->u.p_pes->es.id, pespid->u.p_pes->es.fmt );
5472 for( int i=0; i<pespid->u.p_pes->extra_es.i_size &&
5473 i<p_pes->extra_es.i_size; i++ )
5475 pespid->u.p_pes->extra_es.p_elems[i]->id = p_pes->extra_es.p_elems[i]->id;
5476 if( pespid->u.p_pes->extra_es.p_elems[i]->id )
5478 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5479 pespid->u.p_pes->extra_es.p_elems[i]->id,
5480 pespid->u.p_pes->extra_es.p_elems[i]->fmt );
5481 p_pes->extra_es.p_elems[i]->id = NULL;
5486 ts_pes_Del( p_demux, p_pes ); // delete temp
5490 AddAndCreateES( p_demux, pespid );
5494 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x09 );
5495 if( p_dr && p_dr->i_length >= 2 )
5497 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5498 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5502 /* Set CAM descrambling */
5503 if( !ProgramIsSelected( p_sys, p_pmt->i_number ) )
5505 dvbpsi_pmt_delete( p_dvbpsipmt );
5507 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
5508 p_dvbpsipmt ) != VLC_SUCCESS )
5510 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED )
5512 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
5513 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
5514 if (!p_sys->arib.b25stream)
5515 dvbpsi_pmt_delete( p_dvbpsipmt );
5516 } else dvbpsi_pmt_delete( p_dvbpsipmt );
5519 /* Decref or clean now unused es */
5520 for( int i = 0; i < old_es_rm.i_size; i++ )
5521 PIDRelease( p_demux, old_es_rm.p_elems[i] );
5522 ARRAY_RESET( old_es_rm );
5524 UpdatePESFilters( p_demux, p_sys->b_es_all );
5526 if( !p_sys->b_trust_pcr )
5528 int i_cand = FindPCRCandidate( p_pmt );
5529 p_pmt->i_pid_pcr = i_cand;
5530 p_pmt->pcr.b_disable = true;
5531 msg_Warn( p_demux, "PCR not trusted for program %d, set up workaround using pid %d",
5532 p_pmt->i_number, i_cand );
5535 /* Probe Boundaries */
5536 if( p_sys->b_canfastseek && p_pmt->i_last_dts == -1 )
5538 p_pmt->i_last_dts = 0;
5539 ProbeStart( p_demux, p_pmt->i_number );
5540 ProbeEnd( p_demux, p_pmt->i_number );
5544 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
5546 /* Some Dreambox streams have all PMT set to same pid */
5547 int i_prev_pid = -1;
5548 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
5550 p_program = p_program->p_next )
5552 if( p_program->i_pid == i_prev_pid )
5554 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
5555 return VLC_EGENERIC;
5557 i_prev_pid = p_program->i_pid;
5562 static void PATCallBack( void *data, dvbpsi_pat_t *p_dvbpsipat )
5564 demux_t *p_demux = data;
5565 demux_sys_t *p_sys = p_demux->p_sys;
5566 dvbpsi_pat_program_t *p_program;
5567 ts_pid_t *patpid = &p_sys->pid[0];
5568 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5570 patpid->i_flags |= FLAG_SEEN;
5572 msg_Dbg( p_demux, "PATCallBack called" );
5574 if(unlikely( p_sys->pid[0].type != TYPE_PAT ))
5576 msg_Warn( p_demux, "PATCallBack called on invalid pid" );
5580 if( ( p_pat->i_version != -1 &&
5581 ( !p_dvbpsipat->b_current_next ||
5582 p_dvbpsipat->i_version == p_pat->i_version ) ) ||
5583 ( p_pat->i_ts_id != -1 && p_dvbpsipat->i_ts_id != p_pat->i_ts_id ) ||
5584 p_sys->b_user_pmt || PATCheck( p_demux, p_dvbpsipat ) )
5586 dvbpsi_pat_delete( p_dvbpsipat );
5590 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
5591 p_dvbpsipat->i_ts_id, p_dvbpsipat->i_version, p_dvbpsipat->b_current_next );
5593 /* Save old programs array */
5594 DECL_ARRAY(ts_pid_t *) old_pmt_rm;
5595 old_pmt_rm.i_alloc = p_pat->programs.i_alloc;
5596 old_pmt_rm.i_size = p_pat->programs.i_size;
5597 old_pmt_rm.p_elems = p_pat->programs.p_elems;
5598 ARRAY_INIT(p_pat->programs);
5600 /* now create programs */
5601 for( p_program = p_dvbpsipat->p_first_program; p_program != NULL;
5602 p_program = p_program->p_next )
5604 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
5606 if( p_program->i_number == 0 )
5609 ts_pid_t *pmtpid = &p_sys->pid[p_program->i_pid];
5611 ValidateDVBMeta( p_demux, p_program->i_pid );
5613 /* create or temporary incref pid */
5614 if( !PIDSetup( p_demux, TYPE_PMT, pmtpid, patpid ) )
5616 msg_Warn( p_demux, " * number=%d pid=%d (ignored)", p_program->i_number,
5621 pmtpid->u.p_pmt->i_number = p_program->i_number;
5623 if( !dvbpsi_pmt_attach( pmtpid->u.p_pmt->handle, p_program->i_number, PMTCallBack, p_demux ) )
5624 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5625 p_program->i_number );
5627 ARRAY_APPEND( p_pat->programs, pmtpid );
5629 /* Now select PID at access level */
5630 if( p_sys->programs.i_size == 0 ||
5631 ProgramIsSelected( p_sys, p_program->i_number ) )
5633 if( p_sys->programs.i_size == 0 )
5635 p_sys->b_default_selection = true;
5636 ARRAY_APPEND( p_sys->programs, p_program->i_number );
5639 if( SetPIDFilter( p_sys, pmtpid, true ) )
5640 p_sys->b_access_control = false;
5641 else if ( p_sys->es_creation == DELAY_ES )
5642 p_sys->es_creation = CREATE_ES;
5645 p_pat->i_version = p_dvbpsipat->i_version;
5646 p_pat->i_ts_id = p_dvbpsipat->i_ts_id;
5648 for(int i=0; i<old_pmt_rm.i_size; i++)
5650 /* decref current or release now unreferenced */
5651 PIDRelease( p_demux, old_pmt_rm.p_elems[i] );
5653 ARRAY_RESET(old_pmt_rm);
5655 dvbpsi_pat_delete( p_dvbpsipat );
5658 static inline bool handle_Init( demux_t *p_demux, dvbpsi_t **handle )
5660 *handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
5663 (*handle)->p_sys = (void *) p_demux;
5667 static ts_pat_t *ts_pat_New( demux_t *p_demux )
5669 ts_pat_t *pat = malloc( sizeof( ts_pat_t ) );
5673 if( !handle_Init( p_demux, &pat->handle ) )
5679 pat->i_version = -1;
5681 ARRAY_INIT( pat->programs );
5686 static void ts_pat_Del( demux_t *p_demux, ts_pat_t *pat )
5688 if( dvbpsi_decoder_present( pat->handle ) )
5689 dvbpsi_pat_detach( pat->handle );
5690 dvbpsi_delete( pat->handle );
5691 for( int i=0; i<pat->programs.i_size; i++ )
5692 PIDRelease( p_demux, pat->programs.p_elems[i] );
5693 ARRAY_RESET( pat->programs );
5697 static ts_pmt_t *ts_pmt_New( demux_t *p_demux )
5699 ts_pmt_t *pmt = malloc( sizeof( ts_pmt_t ) );
5703 if( !handle_Init( p_demux, &pmt->handle ) )
5709 ARRAY_INIT( pmt->e_streams );
5711 pmt->i_version = -1;
5713 pmt->i_pid_pcr = -1;
5716 pmt->i_last_dts = -1;
5718 pmt->pcr.i_current = -1;
5719 pmt->pcr.i_first = -1;
5720 pmt->pcr.b_disable = false;
5721 pmt->pcr.i_first_dts = VLC_TS_INVALID;
5722 pmt->pcr.i_pcroffset = -1;
5727 static void ts_pmt_Del( demux_t *p_demux, ts_pmt_t *pmt )
5729 if( dvbpsi_decoder_present( pmt->handle ) )
5730 dvbpsi_pmt_detach( pmt->handle );
5731 dvbpsi_delete( pmt->handle );
5732 for( int i=0; i<pmt->e_streams.i_size; i++ )
5733 PIDRelease( p_demux, pmt->e_streams.p_elems[i] );
5734 ARRAY_RESET( pmt->e_streams );
5736 IODFree( pmt->iod );
5737 if( pmt->i_number > -1 )
5738 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, pmt->i_number );
5742 static ts_pes_t *ts_pes_New( demux_t *p_demux )
5744 VLC_UNUSED(p_demux);
5745 ts_pes_t *pes = malloc( sizeof( ts_pes_t ) );
5750 pes->es.p_mpeg4desc = NULL;
5751 es_format_Init( &pes->es.fmt, UNKNOWN_ES, 0 );
5752 ARRAY_INIT( pes->extra_es );
5753 pes->data_type = TS_ES_DATA_PES;
5754 pes->i_data_size = 0;
5755 pes->i_data_gathered = 0;
5757 pes->pp_last = &pes->p_data;
5758 pes->p_prepcr_outqueue = NULL;
5763 static void ts_pes_Del( demux_t *p_demux, ts_pes_t *pes )
5767 es_out_Del( p_demux->out, pes->es.id );
5768 p_demux->p_sys->i_pmt_es--;
5772 block_ChainRelease( pes->p_data );
5774 if( pes->p_prepcr_outqueue )
5775 block_ChainRelease( pes->p_prepcr_outqueue );
5777 es_format_Clean( &pes->es.fmt );
5778 free( pes->es.p_mpeg4desc );
5780 for( int i = 0; i < pes->extra_es.i_size; i++ )
5782 if( pes->extra_es.p_elems[i]->id )
5784 es_out_Del( p_demux->out, pes->extra_es.p_elems[i]->id );
5785 p_demux->p_sys->i_pmt_es--;
5787 es_format_Clean( &pes->extra_es.p_elems[i]->fmt );
5788 free( pes->extra_es.p_elems[i] );
5790 ARRAY_RESET( pes->extra_es );
5795 static ts_psi_t *ts_psi_New( demux_t *p_demux )
5797 ts_psi_t *psi = malloc( sizeof( ts_psi_t ) );
5801 if( !handle_Init( p_demux, &psi->handle ) )
5807 psi->i_version = -1;
5812 static void ts_psi_Del( demux_t *p_demux, ts_psi_t *psi )
5814 VLC_UNUSED(p_demux);
5815 if( dvbpsi_decoder_present( psi->handle ) )
5816 dvbpsi_DetachDemux( psi->handle );
5817 dvbpsi_delete( psi->handle );