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;
349 #define SEEN(x) ((x).i_flags & FLAG_SEEN)
350 #define SCRAMBLED(x) ((x).i_flags & FLAG_SCRAMBLED)
357 uint8_t i_cc; /* countinuity counter */
360 /* PSI owner (ie PMT -> PAT, ES -> PMT */
375 vlc_fourcc_t i_fourcc;
387 #define MIN_ES_PID 4 /* Should be 32.. broken muxers */
388 #define MAX_ES_PID 8190
389 #define MIN_PAT_INTERVAL CLOCK_FREQ // DVB is 500ms
391 #define FROM_SCALE(x) (VLC_TS_0 + ((x) * 100 / 9))
392 #define TO_SCALE(x) (((x) - VLC_TS_0) * 9 / 100)
399 vlc_mutex_t csa_lock;
401 /* TS packet size (188, 192, 204) */
404 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
405 int i_packet_header_size;
407 /* how many TS packet we read at once */
410 bool b_force_seek_per_percent;
416 arib_instance_t *p_instance;
429 NO_ES, /* for preparse */
433 #define PREPARSING p_sys->es_creation == NO_ES
444 bool b_access_control;
451 int64_t i_dvb_length;
452 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
454 /* Selected programs */
455 DECL_ARRAY( int ) programs; /* List of selected/access-filtered programs */
456 bool b_default_selection; /* True if set by default to first pmt seen (to get data from filtered access) */
460 mtime_t i_first_dts; /* first dts encountered for the stream */
461 int i_timesourcepid; /* which pid we saved the dts from */
462 bool b_pat_deadline; /* set if we haven't seen PAT within MIN_PAT_INTERVAL */
471 static int Demux ( demux_t *p_demux );
472 static int Control( demux_t *p_demux, int i_query, va_list args );
474 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
476 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent );
477 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid );
479 static void PATCallBack( void*, dvbpsi_pat_t * );
480 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
481 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
482 uint16_t i_extension, demux_t * );
484 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
487 static ts_pat_t *ts_pat_New( demux_t * );
488 static void ts_pat_Del( demux_t *, ts_pat_t * );
489 static ts_pmt_t *ts_pmt_New( demux_t * );
490 static void ts_pmt_Del( demux_t *, ts_pmt_t * );
491 static ts_pes_t *ts_pes_New( demux_t * );
492 static void ts_pes_Del( demux_t *, ts_pes_t * );
493 static ts_psi_t *ts_psi_New( demux_t * );
494 static void ts_psi_Del( demux_t *, ts_psi_t * );
497 static ts_pmt_t * GetProgramByID( demux_sys_t *, int i_program );
498 static inline int PIDGet( block_t *p )
500 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
503 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
504 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid );
505 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_prg, mtime_t i_pcr );
507 static block_t* ReadTSPacket( demux_t *p_demux );
508 static int ProbeStart( demux_t *p_demux, int i_program );
509 static int ProbeEnd( demux_t *p_demux, int i_program );
510 static int SeekToTime( demux_t *p_demux, ts_pmt_t *, int64_t time );
511 static void ReadyQueuesPostSeek( demux_sys_t *p_sys );
512 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
513 static void PCRFixHandle( demux_t *, ts_pmt_t *, block_t * );
514 static int64_t TimeStampWrapAround( ts_pmt_t *, int64_t );
516 static void IODFree( iod_descriptor_t * );
518 #define TS_USER_PMT_NUMBER (0)
519 static int UserPmt( demux_t *p_demux, const char * );
521 static int SetPIDFilter( demux_t *, int i_pid, bool b_selected );
522 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
524 #define TS_PACKET_SIZE_188 188
525 #define TS_PACKET_SIZE_192 192
526 #define TS_PACKET_SIZE_204 204
527 #define TS_PACKET_SIZE_MAX 204
528 #define TS_HEADER_SIZE 4
530 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size, int i_offset )
532 const uint8_t *p_peek;
534 if( stream_Peek( p_demux->s,
535 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
538 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
540 if( p_peek[i_offset + i_sync] != 0x47 )
543 /* Check next 3 sync bytes */
544 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
545 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
547 msg_Err( p_demux, "cannot peek" );
550 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
551 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
552 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
554 return TS_PACKET_SIZE_188;
556 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
557 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
558 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
562 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
564 return TS_PACKET_SIZE_192;
566 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
567 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
568 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
570 return TS_PACKET_SIZE_204;
574 if( p_demux->b_force )
576 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
577 return TS_PACKET_SIZE_188;
579 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
583 #define TOPFIELD_HEADER_SIZE 3712
585 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, int *pi_header_size, vdr_info_t *p_vdr )
587 const uint8_t *p_peek;
589 int i_packet_size = -1;
591 if( stream_Peek( p_demux->s,
592 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
595 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
596 p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
597 stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
598 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
600 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
601 if( i_packet_size != -1 )
603 msg_Dbg( p_demux, "this is a topfield file" );
605 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
606 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
607 * but after the filename the offsets seem to be incorrect. - DJ */
608 int i_duration, i_name;
609 char *psz_name = xmalloc(25);
610 char *psz_event_name;
611 char *psz_event_text = xmalloc(130);
612 char *psz_ext_text = xmalloc(1025);
614 // 2 bytes version Uimsbf (4,5)
615 // 2 bytes reserved (6,7)
616 // 2 bytes duration in minutes Uimsbf (8,9(
617 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
618 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
619 // 2 bytes service number in channel list (10, 11)
620 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
621 // 4 bytes of reserved + tuner info (14,15,16,17)
622 // 2 bytes of Service ID Bslbf (18,19)
623 // 2 bytes of PMT PID Uimsbf (20,21)
624 // 2 bytes of PCR PID Uimsbf (22,23)
625 // 2 bytes of Video PID Uimsbf (24,25)
626 // 2 bytes of Audio PID Uimsbf (26,27)
627 // 24 bytes filename Bslbf
628 memcpy( psz_name, &p_peek[28], 24 );
630 msg_Dbg( p_demux, "recordingname=%s", psz_name );
631 // 1 byte of sat index Uimsbf (52)
632 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
633 // 4 bytes of freq. Uimsbf (56,57,58,59)
634 // 2 bytes of symbol rate Uimsbf (60,61)
635 // 2 bytes of TS stream ID Uimsbf (62,63)
638 // 2 bytes duration Uimsbf (70,71)
639 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
640 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
641 // 4 bytes EventID Uimsbf (72-75)
642 // 8 bytes of Start and End time info (76-83)
643 // 1 byte reserved (84)
644 // 1 byte event name length Uimsbf (89)
645 i_name = (int)(p_peek[89]&~0x81);
646 msg_Dbg( p_demux, "event name length = %d", i_name);
647 psz_event_name = xmalloc( i_name+1 );
648 // 1 byte parental rating (90)
649 // 129 bytes of event text
650 memcpy( psz_event_name, &p_peek[91], i_name );
651 psz_event_name[i_name] = '\0';
652 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
653 psz_event_text[129-i_name] = '\0';
654 msg_Dbg( p_demux, "event name=%s", psz_event_name );
655 msg_Dbg( p_demux, "event text=%s", psz_event_text );
656 // 12 bytes reserved (220)
658 // 2 bytes Event Text Length Uimsbf
659 // 4 bytes EventID Uimsbf
660 // FIXME We just have 613 bytes. not enough for this entire text
661 // 1024 bytes Extended Event Text Bslbf
662 memcpy( psz_ext_text, p_peek+372, 1024 );
663 psz_ext_text[1024] = '\0';
664 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
665 // 52 bytes reserved Bslbf
667 p_vdr->i_service = GetWBE(&p_peek[18]);
669 return i_packet_size;
670 //return TS_PACKET_SIZE_188;
674 return DetectPacketSize( p_demux, pi_header_size, 0 );
677 static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
679 return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
680 (mtime_t)(p_data[1] << 22)|
681 ((mtime_t)(p_data[2]&0xfe) << 14)|
682 (mtime_t)(p_data[3] << 7)|
683 (mtime_t)(p_data[4] >> 1);
686 static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, size_t i_data, bool b_adaptfield )
688 demux_sys_t *p_sys = p_demux->p_sys;
689 const uint8_t *p_pes = p_pesstart;
690 pid->probed.i_type = -1;
697 uint8_t len = *p_pes;
702 p_pes++; i_data--;/* stuffing */
708 if( len >= 7 && (p_pes[1] & 0x10) )
709 pid->probed.i_pcr_count++;
718 if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
721 size_t i_pesextoffset = 8;
723 if( p_pes[7] & 0x80 ) // PTS
726 if ( i_data < i_pesextoffset )
728 i_dts = ExtractPESTimestamp( &p_pes[9] );
730 if( p_pes[7] & 0x40 ) // DTS
733 if ( i_data < i_pesextoffset )
735 i_dts = ExtractPESTimestamp( &p_pes[14] );
737 if( p_pes[7] & 0x20 ) // ESCR
739 if( p_pes[7] & 0x10 ) // ESrate
741 if( p_pes[7] & 0x08 ) // DSM
743 if( p_pes[7] & 0x04 ) // CopyInfo
745 if( p_pes[7] & 0x02 ) // PESCRC
748 if ( i_data < i_pesextoffset )
751 /* HeaderdataLength */
752 const size_t i_payloadoffset = 8 + 1 + p_pes[8];
755 if ( i_data < i_pesextoffset || i_data < i_payloadoffset )
758 i_data -= 8 + 1 + p_pes[8];
760 if( p_pes[7] & 0x01 ) // PESExt
762 size_t i_extension2_offset = 1;
763 if ( p_pes[i_pesextoffset] & 0x80 ) // private data
764 i_extension2_offset += 16;
765 if ( p_pes[i_pesextoffset] & 0x40 ) // pack
766 i_extension2_offset += 1;
767 if ( p_pes[i_pesextoffset] & 0x20 ) // seq
768 i_extension2_offset += 2;
769 if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
770 i_extension2_offset += 2;
771 if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
773 uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
774 i_extension2_offset += i_len;
776 if( i_data < i_extension2_offset )
779 i_data -= i_extension2_offset;
781 /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
786 const uint8_t *p_data = &p_pes[i_payloadoffset];
788 if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
790 if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
792 pid->probed.i_type = 0x06;
793 pid->probed.i_fourcc = VLC_CODEC_DTS;
795 else if( !memcmp( p_data, "\x0B\x77", 2 ) )
797 pid->probed.i_type = 0x06;
798 pid->probed.i_fourcc = VLC_CODEC_EAC3;
800 else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
802 switch(p_data[1] & 18)
804 /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
805 11 - MPEG Version 1 (ISO/IEC 11172-3) */
807 pid->probed.i_type = 0x04;
810 pid->probed.i_type = 0x03;
815 switch(p_data[1] & 6)
821 pid->probed.i_type = 0x04;
822 pid->probed.i_fourcc = VLC_CODEC_MPGA;
825 pid->probed.i_type = 0x04;
826 pid->probed.i_fourcc = VLC_CODEC_MP2;
829 pid->probed.i_type = 0x04;
830 pid->probed.i_fourcc = VLC_CODEC_MP3;
837 else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
839 if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
841 pid->probed.i_type = 0x1b;
842 pid->probed.i_fourcc = VLC_CODEC_H264;
844 else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
846 pid->probed.i_type = 0x02;
847 pid->probed.i_fourcc = VLC_CODEC_MPGV;
851 /* Track timestamps and flag missing PAT */
852 if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
854 p_sys->patfix.i_first_dts = i_dts;
855 p_sys->patfix.i_timesourcepid = pid->i_pid;
857 else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
858 !p_sys->patfix.b_pat_deadline )
860 if( i_dts - p_sys->patfix.i_first_dts > TO_SCALE(MIN_PAT_INTERVAL) )
861 p_sys->patfix.b_pat_deadline = true;
866 static void BuildPATCallback( void *p_opaque, block_t *p_block )
868 ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
869 dvbpsi_packet_push( pat_pid->u.p_pat->handle, p_block->p_buffer );
872 static void BuildPMTCallback( void *p_opaque, block_t *p_block )
874 ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
875 assert(program_pid->type == TYPE_PMT);
878 dvbpsi_packet_push( program_pid->u.p_pmt->handle,
880 p_block = p_block->p_next;
884 static void MissingPATPMTFixup( demux_t *p_demux )
886 demux_sys_t *p_sys = p_demux->p_sys;
887 int i_program_number = 1234;
888 int i_program_pid = 1337;
889 int i_pcr_pid = 0x1FFF;
892 if( SEEN(p_sys->pid[i_program_pid]) )
894 /* Find a free one */
895 for( i_program_pid = MIN_ES_PID;
896 i_program_pid <= MAX_ES_PID && SEEN(p_sys->pid[i_program_pid]);
900 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
902 if( !SEEN(p_sys->pid[i]) ||
903 p_sys->pid[i].probed.i_type == -1 )
906 if( i_pcr_pid == 0x1FFF && ( p_sys->pid[i].probed.i_type == 0x03 ||
907 p_sys->pid[i].probed.i_pcr_count ) )
908 i_pcr_pid = p_sys->pid[i].i_pid;
916 ts_stream_t patstream =
919 .i_continuity_counter = 0x10,
920 .b_discontinuity = false
923 ts_stream_t pmtprogramstream =
925 .i_pid = i_program_pid,
926 .i_continuity_counter = 0x0,
927 .b_discontinuity = false
930 BuildPAT( p_sys->pid[0].u.p_pat->handle,
931 &p_sys->pid[0], BuildPATCallback,
934 1, &pmtprogramstream, &i_program_number );
936 if( p_sys->pid[i_program_pid].type != TYPE_PMT )
938 msg_Err( p_demux, "PAT creation failed" );
947 es_format_t esfmt = {0};
948 struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
949 pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
950 if( esstreams && mapped )
953 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
955 if( !SEEN(p_sys->pid[i]) ||
956 p_sys->pid[i].probed.i_type == -1 )
959 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
960 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
961 esstreams[j].ts.i_pid = p_sys->pid[i].i_pid;
962 mapped[j].pes = &esstreams[j].pes;
963 mapped[j].ts = &esstreams[j].ts;
964 mapped[j].fmt = &esfmt;
968 BuildPMT( p_sys->pid[0].u.p_pat->handle, VLC_OBJECT(p_demux),
969 &p_sys->pid[i_program_pid], BuildPMTCallback,
973 1, &pmtprogramstream, &i_program_number,
980 /*****************************************************************************
982 *****************************************************************************/
983 static int Open( vlc_object_t *p_this )
985 demux_t *p_demux = (demux_t*)p_this;
988 int i_packet_size, i_packet_header_size = 0;
991 vdr_info_t vdr = {0};
993 /* Search first sync byte */
994 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
995 if( i_packet_size < 0 )
998 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
1001 memset( p_sys, 0, sizeof( demux_sys_t ) );
1002 vlc_mutex_init( &p_sys->csa_lock );
1004 p_demux->pf_demux = Demux;
1005 p_demux->pf_control = Control;
1007 /* Init p_sys field */
1008 p_sys->b_dvb_meta = true;
1009 p_sys->b_access_control = true;
1010 p_sys->b_end_preparse = false;
1011 ARRAY_INIT( p_sys->programs );
1012 p_sys->b_default_selection = false;
1013 p_sys->i_tdt_delta = 0;
1014 p_sys->i_dvb_start = 0;
1015 p_sys->i_dvb_length = 0;
1019 p_sys->arib.b25stream = NULL;
1020 p_sys->stream = p_demux->s;
1022 p_sys->b_broken_charset = false;
1024 for( int i = 0; i < 8192; i++ )
1026 ts_pid_t *pid = &p_sys->pid[i];
1028 pid->i_flags = FLAGS_NONE;
1029 pid->probed.i_fourcc = 0;
1030 pid->probed.i_type = 0;
1032 /* PID 8191 is padding */
1033 p_sys->pid[8191].i_flags = FLAG_SEEN;
1034 p_sys->i_packet_size = i_packet_size;
1035 p_sys->i_packet_header_size = i_packet_header_size;
1036 p_sys->i_ts_read = 50;
1038 p_sys->b_start_record = false;
1040 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
1042 if( !dvbpsi_AttachDemux( (table)->u.p_psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
1044 msg_Warn( obj, "Can't dvbpsi_AttachDemux on pid %d", (table)->i_pid );\
1048 /* Init PAT handler */
1049 patpid = &p_sys->pid[0];
1050 if ( !PIDSetup( p_demux, TYPE_PAT, patpid, NULL ) )
1052 vlc_mutex_destroy( &p_sys->csa_lock );
1056 if( !dvbpsi_pat_attach( patpid->u.p_pat->handle, PATCallBack, p_demux ) )
1058 PIDRelease( p_demux, patpid );
1059 vlc_mutex_destroy( &p_sys->csa_lock );
1061 return VLC_EGENERIC;
1064 if( p_sys->b_dvb_meta )
1066 if( !PIDSetup( p_demux, TYPE_SDT, &p_sys->pid[0x11], NULL ) ||
1067 !PIDSetup( p_demux, TYPE_EIT, &p_sys->pid[0x12], NULL ) ||
1068 !PIDSetup( p_demux, TYPE_TDT, &p_sys->pid[0x14], NULL ) )
1070 PIDRelease( p_demux, &p_sys->pid[0x11] );
1071 PIDRelease( p_demux, &p_sys->pid[0x12] );
1072 PIDRelease( p_demux, &p_sys->pid[0x14] );
1073 p_sys->b_dvb_meta = false;
1077 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x11], p_demux);
1078 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x12], p_demux);
1079 VLC_DVBPSI_DEMUX_TABLE_INIT(&p_sys->pid[0x14], p_demux);
1080 if( p_sys->b_access_control &&
1081 ( SetPIDFilter( p_demux, 0x11, true ) ||
1082 SetPIDFilter( p_demux, 0x14, true ) ||
1083 SetPIDFilter( p_demux, 0x12, true ) )
1085 p_sys->b_access_control = false;
1089 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
1091 p_sys->i_pmt_es = 0;
1094 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
1096 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
1098 /* We handle description of an extra PMT */
1099 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
1100 p_sys->b_user_pmt = false;
1101 if( psz_string && *psz_string )
1102 UserPmt( p_demux, psz_string );
1105 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
1106 if( psz_string && *psz_string )
1111 p_sys->csa = csa_New();
1113 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
1114 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
1115 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
1117 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
1119 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1122 else if ( i_res == VLC_SUCCESS )
1124 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1128 csa_Delete( p_sys->csa );
1134 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
1135 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1137 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
1138 if( i_pkt < 4 || i_pkt > 188 )
1140 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
1141 msg_Warn( p_demux, "using default packet size of 188 bytes" );
1142 p_sys->i_csa_pkt_size = 188;
1145 p_sys->i_csa_pkt_size = i_pkt;
1146 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
1152 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
1154 p_sys->b_canseek = false;
1155 p_sys->b_canfastseek = false;
1156 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
1158 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
1160 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
1161 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1164 if( p_sys->b_canseek )
1166 p_sys->es_creation = NO_ES;
1167 while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
1168 if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
1170 p_sys->es_creation = DELAY_ES;
1173 p_sys->es_creation = ( p_sys->b_access_control ? CREATE_ES : DELAY_ES );
1178 /*****************************************************************************
1180 *****************************************************************************/
1181 static void Close( vlc_object_t *p_this )
1183 demux_t *p_demux = (demux_t*)p_this;
1184 demux_sys_t *p_sys = p_demux->p_sys;
1186 PIDRelease( p_demux, &p_sys->pid[0] );
1188 if( p_sys->b_dvb_meta )
1190 PIDRelease( p_demux, &p_sys->pid[0x11] );
1191 PIDRelease( p_demux, &p_sys->pid[0x12] );
1192 PIDRelease( p_demux, &p_sys->pid[0x14] );
1196 for( int i = 0; i < 8192; i++ )
1198 ts_pid_t *pid = &p_sys->pid[i];
1199 if( pid->type != TYPE_FREE )
1200 msg_Err( p_demux, "PID %d type %d not freed", pid->i_pid, pid->type );
1204 vlc_mutex_lock( &p_sys->csa_lock );
1207 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
1208 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1209 csa_Delete( p_sys->csa );
1211 vlc_mutex_unlock( &p_sys->csa_lock );
1213 ARRAY_RESET( p_sys->programs );
1216 if ( p_sys->arib.p_instance )
1217 arib_instance_destroy( p_sys->arib.p_instance );
1220 if ( p_sys->arib.b25stream )
1222 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
1223 stream_Delete( p_sys->arib.b25stream );
1226 vlc_mutex_destroy( &p_sys->csa_lock );
1230 /*****************************************************************************
1231 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
1232 *****************************************************************************/
1233 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
1234 vlc_value_t oldval, vlc_value_t newval,
1237 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1238 demux_t *p_demux = (demux_t*)p_this;
1239 demux_sys_t *p_sys = p_demux->p_sys;
1240 int i_tmp = (intptr_t)p_data;
1242 vlc_mutex_lock( &p_sys->csa_lock );
1244 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
1246 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
1248 vlc_mutex_unlock( &p_sys->csa_lock );
1252 /*****************************************************************************
1254 *****************************************************************************/
1255 static int Demux( demux_t *p_demux )
1257 demux_sys_t *p_sys = p_demux->p_sys;
1258 bool b_wait_es = p_sys->i_pmt_es <= 0;
1260 /* If we had no PAT within MIN_PAT_INTERVAL, create PAT/PMT from probed streams */
1261 if( p_sys->i_pmt_es == 0 && !SEEN(p_sys->pid[0]) && p_sys->patfix.b_pat_deadline )
1262 MissingPATPMTFixup( p_demux );
1264 /* We read at most 100 TS packet or until a frame is completed */
1265 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1267 bool b_frame = false;
1269 if( !(p_pkt = ReadTSPacket( p_demux )) )
1271 return VLC_DEMUXER_EOF;
1274 if( p_sys->b_start_record )
1276 /* Enable recording once synchronized */
1277 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
1278 p_sys->b_start_record = false;
1281 /* Parse the TS packet */
1282 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1284 /* Probe streams to build PAT/PMT after MIN_PAT_INTERVAL in case we don't see any PAT */
1285 if( !SEEN(p_sys->pid[0]) &&
1286 (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
1287 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1288 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
1290 ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
1291 p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
1294 switch( p_pid->type )
1297 dvbpsi_packet_push( p_pid->u.p_pat->handle, p_pkt->p_buffer );
1301 dvbpsi_packet_push( p_pid->u.p_pmt->handle, p_pkt->p_buffer );
1305 p_sys->b_end_preparse = true;
1306 if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
1308 AddAndCreateES( p_demux, NULL );
1310 b_frame = GatherData( p_demux, p_pid, p_pkt );
1312 if( p_sys->b_default_selection )
1314 p_sys->b_default_selection = false;
1315 assert(p_sys->programs.i_size == 1);
1316 if( p_sys->programs.p_elems[0] != p_pid->p_parent->u.p_pmt->i_number )
1318 SetPrgFilter( p_demux, p_sys->programs.p_elems[0], false );
1319 SetPrgFilter( p_demux, p_pid->p_parent->u.p_pmt->i_number, true );
1320 p_sys->programs.p_elems[0] = p_pid->p_parent->u.p_pmt->i_number;
1328 if( p_sys->b_dvb_meta )
1329 dvbpsi_packet_push( p_pid->u.p_psi->handle, p_pkt->p_buffer );
1334 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1336 /* We have to handle PCR if present */
1337 PCRHandle( p_demux, p_pid, p_pkt );
1338 block_Release( p_pkt );
1342 p_pid->i_flags |= FLAG_SEEN;
1344 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1348 demux_UpdateTitleFromStream( p_demux );
1349 return VLC_DEMUXER_SUCCESS;
1352 /*****************************************************************************
1354 *****************************************************************************/
1355 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1357 demux_sys_t *p_sys = p_demux->p_sys;
1363 if( p_sys->i_dvb_length > 0 )
1365 const int64_t t = mdate() + p_sys->i_tdt_delta;
1367 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1370 *pi_length = p_sys->i_dvb_length;
1372 *pi_time = t - p_sys->i_dvb_start;
1376 return VLC_EGENERIC;
1379 static int Control( demux_t *p_demux, int i_query, va_list args )
1381 demux_sys_t *p_sys = p_demux->p_sys;
1383 bool b_bool, *pb_bool;
1388 int i_first_program = ( p_sys->programs.i_size ) ? p_sys->programs.p_elems[0] : 0;
1390 if( PREPARSING || !i_first_program || p_sys->b_default_selection )
1392 if( likely(p_sys->pid[0].type == TYPE_PAT) )
1394 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1395 /* Set default program for preparse time (no program has been selected) */
1396 for( int i = 0; i < p_pat->programs.i_size; i++ )
1398 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
1399 p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1400 if( ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) && p_pmt->i_last_dts > 0 )
1402 i_first_program = p_pmt->i_number;
1411 case DEMUX_GET_POSITION:
1412 pf = (double*) va_arg( args, double* );
1414 /* Access control test is because EPG for recordings is not relevant */
1415 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1417 int64_t i_time, i_length;
1418 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1420 *pf = (double)i_time/(double)i_length;
1425 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1426 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1427 p_pmt->pcr.i_current > -1 )
1429 double i_length = TimeStampWrapAround( p_pmt,
1430 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1431 i_length += p_pmt->pcr.i_pcroffset;
1432 double i_pos = TimeStampWrapAround( p_pmt,
1433 p_pmt->pcr.i_current ) - p_pmt->pcr.i_first;
1434 *pf = i_pos / i_length;
1438 if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1440 int64_t offset = stream_Tell( p_sys->stream );
1441 *pf = (double)offset / (double)i64;
1446 case DEMUX_SET_POSITION:
1447 f = (double) va_arg( args, double );
1449 if(!p_sys->b_canseek)
1452 if( p_sys->b_dvb_meta && p_sys->b_access_control &&
1453 !p_sys->b_force_seek_per_percent &&
1454 (p_pmt = GetProgramByID( p_sys, i_first_program )) )
1456 int64_t i_time, i_length;
1457 if( !DVBEventInformation( p_demux, &i_time, &i_length ) &&
1458 i_length > 0 && !SeekToTime( p_demux, p_pmt, TO_SCALE(i_length) * f ) )
1460 ReadyQueuesPostSeek( p_sys );
1461 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1462 TO_SCALE(i_length) * f );
1467 if( !p_sys->b_force_seek_per_percent &&
1468 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1469 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1470 p_pmt->pcr.i_current > -1 )
1472 double i_length = TimeStampWrapAround( p_pmt,
1473 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1474 if( !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + i_length * f ) )
1476 ReadyQueuesPostSeek( p_sys );
1477 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1478 FROM_SCALE(p_pmt->pcr.i_first + i_length * f) );
1483 i64 = stream_Size( p_sys->stream );
1485 stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) == VLC_SUCCESS )
1487 ReadyQueuesPostSeek( p_sys );
1492 case DEMUX_SET_TIME:
1493 i64 = (int64_t)va_arg( args, int64_t );
1495 if( p_sys->b_canseek &&
1496 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1497 p_pmt->pcr.i_first > -1 &&
1498 !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + TO_SCALE(i64) ) )
1500 ReadyQueuesPostSeek( p_sys );
1501 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1502 FROM_SCALE(p_pmt->pcr.i_first) + i64 - VLC_TS_0 );
1507 case DEMUX_GET_TIME:
1508 pi64 = (int64_t*)va_arg( args, int64_t * );
1510 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1512 if( !DVBEventInformation( p_demux, pi64, NULL ) )
1516 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1517 p_pmt->pcr.i_current > -1 && p_pmt->pcr.i_first > -1 )
1519 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
1520 *pi64 = FROM_SCALE(i_pcr - p_pmt->pcr.i_first);
1525 case DEMUX_GET_LENGTH:
1526 pi64 = (int64_t*)va_arg( args, int64_t * );
1528 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1530 if( !DVBEventInformation( p_demux, NULL, pi64 ) )
1534 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1535 ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) &&
1536 p_pmt->i_last_dts > 0 )
1538 int64_t i_start = (p_pmt->pcr.i_first > -1) ? p_pmt->pcr.i_first :
1539 TO_SCALE(p_pmt->pcr.i_first_dts);
1540 int64_t i_last = TimeStampWrapAround( p_pmt, p_pmt->i_last_dts );
1541 i_last += p_pmt->pcr.i_pcroffset;
1542 *pi64 = FROM_SCALE(i_last - i_start);
1547 case DEMUX_SET_GROUP:
1551 i_int = (int)va_arg( args, int );
1552 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1553 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1555 if( i_int != 0 ) /* If not default program */
1557 /* Deselect/filter current ones */
1558 for( int i=0; i<p_sys->programs.i_size; i++ )
1559 SetPrgFilter( p_demux, p_sys->programs.p_elems[i], false );
1560 ARRAY_RESET( p_sys->programs );
1564 ARRAY_APPEND( p_sys->programs, i_int );
1566 else if( likely( p_list != NULL ) )
1568 for( int i = 0; i < p_list->i_count; i++ )
1569 ARRAY_APPEND( p_sys->programs, p_list->p_values[i].i_int );
1572 /* Select/filter current ones */
1573 for( int i=0; i<p_sys->programs.i_size; i++ )
1575 msg_Dbg( p_demux, "Program %d in new selection", p_sys->programs.p_elems[i] );
1576 SetPrgFilter( p_demux, p_sys->programs.p_elems[i], true );
1579 p_sys->b_default_selection = false;
1585 case DEMUX_GET_TITLE_INFO:
1587 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1588 int *c = va_arg( args, int * );
1590 *va_arg( args, int* ) = 0; /* Title offset */
1591 *va_arg( args, int* ) = 0; /* Chapter offset */
1592 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1595 case DEMUX_SET_TITLE:
1596 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1598 case DEMUX_SET_SEEKPOINT:
1599 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1601 case DEMUX_GET_META:
1602 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1604 case DEMUX_CAN_RECORD:
1605 pb_bool = (bool*)va_arg( args, bool * );
1609 case DEMUX_SET_RECORD_STATE:
1610 b_bool = (bool)va_arg( args, int );
1613 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1614 p_sys->b_start_record = b_bool;
1617 case DEMUX_GET_SIGNAL:
1618 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1624 return VLC_EGENERIC;
1627 /*****************************************************************************
1629 *****************************************************************************/
1630 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1632 demux_sys_t *p_sys = p_demux->p_sys;
1633 char *psz_dup = strdup( psz_fmt );
1634 char *psz = psz_dup;
1642 i_pid = strtol( psz, &psz, 0 );
1643 if( i_pid < 2 || i_pid >= 8192 )
1646 /* Parse optional program number */
1649 i_number = strtol( &psz[1], &psz, 0 );
1652 ts_pid_t *pmtpid = &p_sys->pid[i_pid];
1654 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1655 if ( !PIDSetup( p_demux, TYPE_PMT, pmtpid, &p_sys->pid[0] ) )
1659 ts_pmt_t *p_pmt = pmtpid->u.p_pmt;
1660 p_pmt->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1661 if( !dvbpsi_pmt_attach( p_pmt->handle,
1662 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1663 PMTCallBack, p_demux ) )
1665 PIDRelease( p_demux, pmtpid );
1669 ARRAY_APPEND( p_sys->pid[0].u.p_pat->programs, pmtpid );
1671 psz = strchr( psz, '=' );
1674 while( psz && *psz )
1676 char *psz_next = strchr( psz, ',' );
1682 i_pid = strtol( psz, &psz, 0 );
1683 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1686 char *psz_opt = &psz[1];
1687 if( !strcmp( psz_opt, "pcr" ) )
1689 p_pmt->i_pid_pcr = i_pid;
1691 else if( p_sys->pid[i_pid].type == TYPE_FREE )
1693 ts_pid_t *pid = &p_sys->pid[i_pid];
1695 char *psz_arg = strchr( psz_opt, '=' );
1699 if ( !PIDSetup( p_demux, TYPE_PES, pid, pmtpid ) )
1702 ARRAY_APPEND( p_pmt->e_streams, pid );
1704 if( p_pmt->i_pid_pcr <= 0 )
1705 p_pmt->i_pid_pcr = i_pid;
1707 es_format_t *fmt = &pid->u.p_pes->es.fmt;
1709 if( psz_arg && strlen( psz_arg ) == 4 )
1711 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1712 psz_arg[2], psz_arg[3] );
1713 int i_cat = UNKNOWN_ES;
1715 if( !strcmp( psz_opt, "video" ) )
1717 else if( !strcmp( psz_opt, "audio" ) )
1719 else if( !strcmp( psz_opt, "spu" ) )
1722 es_format_Init( fmt, i_cat, i_codec );
1723 fmt->b_packetized = false;
1727 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1728 PIDFillFormat( fmt, i_stream_type );
1731 fmt->i_group = i_number;
1732 if( p_sys->b_es_id_pid )
1735 if( fmt->i_cat != UNKNOWN_ES )
1737 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1738 (char*)&fmt->i_codec );
1739 pid->u.p_pes->es.id = es_out_Add( p_demux->out, fmt );
1748 p_sys->b_user_pmt = true;
1754 return VLC_EGENERIC;
1757 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1759 demux_sys_t *p_sys = p_demux->p_sys;
1761 if( !p_sys->b_access_control )
1762 return VLC_EGENERIC;
1764 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1765 i_pid, b_selected );
1768 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1770 demux_sys_t *p_sys = p_demux->p_sys;
1771 ts_pmt_t *p_pmt = NULL;
1774 /* Search pmt to be unselected */
1775 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
1778 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
1779 for( int i = 0; i < p_pat->programs.i_size; i++ )
1781 ts_pid_t *pmtpid = p_pat->programs.p_elems[i];
1782 assert(pmtpid->type == TYPE_PMT);
1784 if( pmtpid->u.p_pmt->i_number == i_prg_id )
1786 i_pmt_pid = pmtpid->i_pid;
1787 p_pmt = pmtpid->u.p_pmt;
1794 if( i_pmt_pid <= 0 )
1798 p_pmt->pcr.b_disable = !p_sys->b_trust_pcr;
1800 SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1801 if( p_pmt->i_pid_pcr > 0 )
1802 SetPIDFilter( p_demux, p_pmt->i_pid_pcr, b_selected );
1805 for( int i = 0; i < p_pmt->e_streams.i_size; i++ )
1807 ts_pid_t *pespid = p_pmt->e_streams.p_elems[i];
1808 assert( pespid->type == TYPE_PES );
1809 /* We only remove/select es that aren't defined by extra pmt */
1810 if( pespid->u.p_pes->es.id || !b_selected )
1811 SetPIDFilter( p_demux, i, b_selected );
1815 static void PIDReset( ts_pid_t *pid )
1817 assert(pid->i_refcount == 0);
1819 pid->i_flags &= ~FLAG_SCRAMBLED;
1820 pid->p_parent = NULL;
1821 pid->type = TYPE_FREE;
1824 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent )
1826 if( pid == p_parent || pid->i_pid == 0x1FFF )
1829 if( pid->i_refcount == 0 )
1831 assert( pid->type == TYPE_FREE );
1834 case TYPE_FREE: /* nonsense ?*/
1840 pid->u.p_pat = ts_pat_New( p_demux );
1847 pid->u.p_pmt = ts_pmt_New( p_demux );
1854 pid->u.p_pes = ts_pes_New( p_demux );
1863 pid->u.p_psi = ts_psi_New( p_demux );
1875 pid->p_parent = p_parent;
1877 else if( pid->type == i_type && pid->i_refcount < UINT8_MAX )
1883 if( pid->type != TYPE_FREE )
1884 msg_Warn( p_demux, "Tried to redeclare pid %d with another type", pid->i_pid );
1891 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid )
1893 if( pid->i_refcount == 0 )
1895 assert( pid->type == TYPE_FREE );
1898 else if( pid->i_refcount == 1 )
1902 else if( pid->i_refcount > 1 )
1904 assert( pid->type != TYPE_FREE && pid->type != TYPE_PAT );
1908 if( pid->i_refcount == 0 )
1913 case TYPE_FREE: /* nonsense ?*/
1914 assert( pid->type != TYPE_FREE );
1918 ts_pat_Del( p_demux, pid->u.p_pat );
1919 pid->u.p_pat = NULL;
1923 ts_pmt_Del( p_demux, pid->u.p_pmt );
1924 pid->u.p_pmt = NULL;
1928 ts_pes_Del( p_demux, pid->u.p_pes );
1929 pid->u.p_pes = NULL;
1935 ts_psi_Del( p_demux, pid->u.p_psi );
1936 pid->u.p_psi = NULL;
1941 SetPIDFilter( p_demux, pid->i_pid, false );
1946 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1951 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1962 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1964 block_t *out = NULL;
1965 block_t **last = NULL;
1967 uint8_t *buf = block->p_buffer;
1968 size_t len = block->i_buffer;
1970 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
1971 int16_t start_trim = 0, end_trim = 0;
1972 int start_trim_flag = (buf[1] >> 4) & 1;
1973 int end_trim_flag = (buf[1] >> 3) & 1;
1974 int control_extension_flag = (buf[1] >> 2) & 1;
1979 unsigned au_size = 0;
1987 if (start_trim_flag) {
1988 start_trim = read_opus_flag(&buf, &len);
1989 if (start_trim < 0) {
1990 msg_Err(demux, "Invalid start trimming flag");
1993 if (end_trim_flag) {
1994 end_trim = read_opus_flag(&buf, &len);
1996 msg_Err(demux, "Invalid end trimming flag");
1999 if (control_extension_flag && len) {
2000 unsigned l = *buf++; len--;
2002 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
2009 if (!au_size || au_size > len) {
2010 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
2014 block_t *au = block_Alloc(au_size);
2017 memcpy(au->p_buffer, buf, au_size);
2018 block_CopyProperties(au, block);
2027 au->i_nb_samples = opus_frame_duration(buf, au_size);
2028 if (end_trim && end_trim <= au->i_nb_samples)
2029 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
2033 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
2034 au->i_nb_samples -= start_trim;
2035 if (au->i_nb_samples == 0)
2036 au->i_flags |= BLOCK_FLAG_PREROLL;
2043 block_Release(block);
2047 /****************************************************************************
2049 ****************************************************************************/
2050 static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header, size_t i_header,
2051 unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
2056 return VLC_EGENERIC;
2058 switch( p_header[3] )
2060 case 0xBC: /* Program stream map */
2061 case 0xBE: /* Padding */
2062 case 0xBF: /* Private stream 2 */
2063 case 0xF0: /* ECM */
2064 case 0xF1: /* EMM */
2065 case 0xFF: /* Program stream directory */
2066 case 0xF2: /* DSMCC stream */
2067 case 0xF8: /* ITU-T H.222.1 type E stream */
2071 if( ( p_header[6]&0xC0 ) == 0x80 )
2074 i_skip = p_header[8] + 9;
2075 if( i_header < i_skip )
2076 return VLC_EGENERIC;
2078 if( p_header[7]&0x80 ) /* has pts */
2080 if( i_header < 9 + 5 )
2081 return VLC_EGENERIC;
2082 *pi_pts = ExtractPESTimestamp( &p_header[9] );
2084 if( p_header[7]&0x40 ) /* has dts */
2086 if( i_header < 14 + 5 )
2087 return VLC_EGENERIC;
2088 *pi_dts = ExtractPESTimestamp( &p_header[14] );
2095 if( i_header < i_skip + 1 )
2096 return VLC_EGENERIC;
2097 while( i_skip < 23 && p_header[i_skip] == 0xff )
2100 if( i_header < i_skip + 1 )
2101 return VLC_EGENERIC;
2105 msg_Err( p_demux, "too much MPEG-1 stuffing" );
2106 return VLC_EGENERIC;
2108 if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
2113 if( i_header < i_skip + 1 )
2114 return VLC_EGENERIC;
2116 if( p_header[i_skip]&0x20 )
2118 if( i_header < i_skip + 5 )
2119 return VLC_EGENERIC;
2120 *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
2122 if( p_header[i_skip]&0x10 ) /* has dts */
2124 if( i_header < i_skip + 10 )
2125 return VLC_EGENERIC;
2126 *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
2142 if( i_header < i_skip )
2143 return VLC_EGENERIC;
2149 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
2152 unsigned i_pes_size = 0;
2153 unsigned i_skip = 0;
2156 mtime_t i_length = 0;
2158 assert(pid->type == TYPE_PES);
2159 assert(pid->p_parent && pid->p_parent->type == TYPE_PMT);
2161 const int i_max = block_ChainExtract( p_pes, header, 34 );
2164 block_ChainRelease( p_pes );
2168 if( SCRAMBLED(*pid) || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2170 if ( !SCRAMBLED(*pid) )
2171 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
2172 header[0], header[1],header[2],header[3], pid->i_pid );
2173 block_ChainRelease( p_pes );
2177 if( ParsePESHeader( p_demux, (uint8_t*)&header, i_max, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
2179 block_ChainRelease( p_pes );
2185 i_pts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_pts );
2187 i_dts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_dts );
2190 if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
2191 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
2195 else if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
2196 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
2197 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
2201 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT &&
2202 pid->u.p_pes->es.p_mpeg4desc )
2204 decoder_config_descriptor_t *dcd = &pid->u.p_pes->es.p_mpeg4desc->dec_descr;
2206 if( dcd->i_extra > 2 &&
2207 dcd->p_extra[0] == 0x10 &&
2208 ( dcd->p_extra[1]&0x10 ) )
2210 /* display length */
2211 if( p_pes->i_buffer + 2 <= i_skip )
2212 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
2216 if( p_pes->i_buffer + 2 <= i_skip )
2217 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
2223 while( p_pes && i_skip > 0 )
2225 if( p_pes->i_buffer <= i_skip )
2227 block_t *p_next = p_pes->p_next;
2229 i_skip -= p_pes->i_buffer;
2230 block_Release( p_pes );
2235 p_pes->i_buffer -= i_skip;
2236 p_pes->p_buffer += i_skip;
2241 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
2242 if( i_pts >= 0 && i_dts < 0 )
2250 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
2253 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
2255 p_pes->i_length = i_length * 100 / 9;
2257 p_block = block_ChainGather( p_pes );
2258 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT )
2260 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2262 p_block->i_buffer = i_pes_size;
2265 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2268 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2270 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_TELETEXT )
2272 if( p_block->i_pts <= VLC_TS_INVALID && pid->p_parent )
2274 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
2275 * In this case use the last PCR + 40ms */
2276 assert( pid->p_parent->type == TYPE_PMT );
2277 if( likely(pid->p_parent->type == TYPE_PMT) )
2279 mtime_t i_pcr = pid->p_parent->u.p_pmt->pcr.i_current;
2280 if( i_pcr > VLC_TS_INVALID )
2281 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
2285 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_A ||
2286 pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_C )
2288 if( p_block->i_pts <= VLC_TS_INVALID )
2290 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2292 p_block->i_buffer = i_pes_size;
2295 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2298 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2301 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_OPUS)
2303 p_block = Opus_Parse(p_demux, p_block);
2306 if( !pid->p_parent || pid->p_parent->type != TYPE_PMT )
2308 block_Release( p_block );
2312 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2315 block_t *p_next = p_block->p_next;
2316 p_block->p_next = NULL;
2318 if( p_pmt->pcr.i_first == -1 ) /* Not seen yet */
2319 PCRFixHandle( p_demux, p_pmt, p_block );
2321 if( p_pmt->pcr.i_current > -1 || p_pmt->pcr.b_disable )
2323 if( pid->u.p_pes->p_prepcr_outqueue )
2325 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2326 p_block = pid->u.p_pes->p_prepcr_outqueue;
2327 pid->u.p_pes->p_prepcr_outqueue = NULL;
2330 if ( p_pmt->pcr.b_disable && p_block->i_dts > VLC_TS_INVALID &&
2331 ( p_pmt->i_pid_pcr == pid->i_pid || p_pmt->i_pid_pcr == 0x1FFF ) )
2333 ProgramSetPCR( p_demux, p_pmt, (p_block->i_dts - VLC_TS_0) * 9 / 100 - 120000 );
2336 /* Compute PCR/DTS offset if any */
2337 if( p_pmt->pcr.i_pcroffset == -1 && p_block->i_dts > VLC_TS_INVALID &&
2338 p_pmt->pcr.i_current > VLC_TS_INVALID )
2340 int64_t i_dts27 = (p_block->i_dts - VLC_TS_0) * 9 / 100;
2341 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
2342 if( i_dts27 < i_pcr )
2344 p_pmt->pcr.i_pcroffset = i_pcr - i_dts27 + 80000;
2345 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64
2346 "us later than pcr, applying delay",
2347 pid->i_pid, p_pmt->pcr.i_pcroffset * 100 / 9 );
2349 else p_pmt->pcr.i_pcroffset = 0;
2352 if( p_pmt->pcr.i_pcroffset != -1 )
2354 if( p_block->i_dts > VLC_TS_INVALID )
2355 p_block->i_dts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2356 if( p_block->i_pts > VLC_TS_INVALID )
2357 p_block->i_pts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2360 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
2362 es_out_Send( p_demux->out, pid->u.p_pes->extra_es.p_elems[i]->id,
2363 block_Duplicate( p_block ) );
2366 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2370 if( p_pmt->pcr.i_first == -1 ) /* Not seen yet */
2371 PCRFixHandle( p_demux, p_pmt, p_block );
2373 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2381 msg_Warn( p_demux, "empty pes" );
2385 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
2387 block_t *p_content = block_ChainGather( p_data );
2389 if ( pid->p_parent && pid->p_parent->type == TYPE_PMT )
2391 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2392 mtime_t i_date = p_pmt->pcr.i_current;
2394 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SCTE_27 )
2396 /* We need to extract the truncated pts stored inside the payload */
2397 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
2400 size_t i_offset = 4;
2401 if( p_content->p_buffer[3] & 0x40 )
2403 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
2404 p_content->p_buffer[8];
2407 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
2409 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
2412 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
2413 if( i_display_in < i_date )
2414 i_date = i_display_in + (1ll << 32);
2416 i_date = i_display_in;
2423 p_content->i_dts = p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
2424 PCRFixHandle( p_demux, p_pmt, p_content );
2427 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_content );
2430 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
2432 block_t *p_data = pid->u.p_pes->p_data;
2437 /* remove the pes from pid */
2438 pid->u.p_pes->p_data = NULL;
2439 pid->u.p_pes->i_data_size = 0;
2440 pid->u.p_pes->i_data_gathered = 0;
2441 pid->u.p_pes->pp_last = &pid->u.p_pes->p_data;
2443 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
2445 ParsePES( p_demux, pid, p_data );
2447 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
2449 ParseTableSection( p_demux, pid, p_data );
2453 block_ChainRelease( p_data );
2457 static block_t* ReadTSPacket( demux_t *p_demux )
2459 demux_sys_t *p_sys = p_demux->p_sys;
2463 /* Get a new TS packet */
2464 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2466 if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
2467 msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
2469 msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
2473 if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
2475 block_Release( p_pkt );
2479 /* Skip header (BluRay streams).
2480 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2481 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2483 p_pkt->p_buffer += p_sys->i_packet_header_size;
2484 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2486 /* Check sync byte and re-sync if needed */
2487 if( p_pkt->p_buffer[0] != 0x47 )
2489 msg_Warn( p_demux, "lost synchro" );
2490 block_Release( p_pkt );
2493 const uint8_t *p_peek;
2494 int i_peek, i_skip = 0;
2496 i_peek = stream_Peek( p_sys->stream, &p_peek,
2497 p_sys->i_packet_size * 10 );
2498 if( i_peek < p_sys->i_packet_size + 1 )
2500 msg_Dbg( p_demux, "eof ?" );
2504 while( i_skip < i_peek - p_sys->i_packet_size )
2506 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2507 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2513 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2514 stream_Read( p_sys->stream, NULL, i_skip );
2516 if( i_skip < i_peek - p_sys->i_packet_size )
2521 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2523 msg_Dbg( p_demux, "eof ?" );
2530 static int64_t TimeStampWrapAround( ts_pmt_t *p_pmt, int64_t i_time )
2532 int64_t i_adjust = 0;
2533 if( p_pmt && p_pmt->pcr.i_first > 0x0FFFFFFFF && i_time < 0x0FFFFFFFF )
2534 i_adjust = 0x1FFFFFFFF;
2536 return i_time + i_adjust;
2539 static mtime_t GetPCR( block_t *p_pkt )
2541 const uint8_t *p = p_pkt->p_buffer;
2545 if( ( p[3]&0x20 ) && /* adaptation */
2549 /* PCR is 33 bits */
2550 i_pcr = ( (mtime_t)p[6] << 25 ) |
2551 ( (mtime_t)p[7] << 17 ) |
2552 ( (mtime_t)p[8] << 9 ) |
2553 ( (mtime_t)p[9] << 1 ) |
2554 ( (mtime_t)p[10] >> 7 );
2559 static inline void FlushESBuffer( ts_pes_t *p_pes )
2563 p_pes->i_data_gathered = p_pes->i_data_size = 0;
2564 block_ChainRelease( p_pes->p_data );
2565 p_pes->p_data = NULL;
2566 p_pes->pp_last = &p_pes->p_data;
2570 static void ReadyQueuesPostSeek( demux_sys_t *p_sys )
2572 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2573 for( int i=0; i< p_pat->programs.i_size; i++ )
2575 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2576 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2578 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
2580 if( pid->type != TYPE_PES )
2585 if( pid->u.p_pes->p_prepcr_outqueue )
2587 block_ChainRelease( pid->u.p_pes->p_prepcr_outqueue );
2588 pid->u.p_pes->p_prepcr_outqueue = NULL;
2591 FlushESBuffer( pid->u.p_pes );
2593 p_pmt->pcr.i_current = -1;
2597 static int SeekToTime( demux_t *p_demux, ts_pmt_t *p_pmt, int64_t i_scaledtime )
2599 demux_sys_t *p_sys = p_demux->p_sys;
2601 /* Deal with common but worst binary search case */
2602 if( p_pmt->pcr.i_first == i_scaledtime && p_sys->b_canseek )
2603 return stream_Seek( p_sys->stream, 0 );
2605 if( !p_sys->b_canfastseek )
2606 return VLC_EGENERIC;
2608 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2610 /* Find the time position by using binary search algorithm. */
2611 int64_t i_head_pos = 0;
2612 int64_t i_tail_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2613 if( i_head_pos >= i_tail_pos )
2614 return VLC_EGENERIC;
2616 bool b_found = false;
2617 while( (i_head_pos + p_sys->i_packet_size) <= i_tail_pos && !b_found )
2619 /* Round i_pos to a multiple of p_sys->i_packet_size */
2620 int64_t i_splitpos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2621 int64_t i_div = i_splitpos % p_sys->i_packet_size;
2622 i_splitpos -= i_div;
2624 if ( stream_Seek( p_sys->stream, i_splitpos ) != VLC_SUCCESS )
2627 int64_t i_pos = i_splitpos;
2628 while( i_pos > -1 && i_pos < i_tail_pos )
2631 block_t *p_pkt = ReadTSPacket( p_demux );
2634 i_head_pos = i_tail_pos;
2638 i_pos = stream_Tell( p_sys->stream );
2640 int i_pid = PIDGet( p_pkt );
2641 if( i_pid != 0x1FFF && p_sys->pid[i_pid].type == TYPE_PES &&
2642 p_sys->pid[i_pid].p_parent->u.p_pmt == p_pmt &&
2643 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
2644 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
2647 unsigned i_skip = 4;
2648 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
2650 if( p_pkt->i_buffer >= 4 + 2 + 5 )
2652 i_pcr = GetPCR( p_pkt );
2653 i_skip += 1 + p_pkt->p_buffer[4];
2660 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2661 p_pkt->i_buffer - i_skip, &i_skip, &i_dts, &i_pts ) )
2668 block_Release( p_pkt );
2672 int64_t i_diff = i_scaledtime - TimeStampWrapAround( p_pmt, i_pcr );
2674 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2675 else if( i_diff < TO_SCALE(VLC_TS_0 + CLOCK_FREQ / 2) ) // 500ms
2683 if ( !b_found && i_pos > i_tail_pos - p_sys->i_packet_size )
2684 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2689 msg_Dbg( p_demux, "Seek():cannot find a time position." );
2690 stream_Seek( p_sys->stream, i_initial_pos );
2691 return VLC_EGENERIC;
2696 static ts_pmt_t * GetProgramByID( demux_sys_t *p_sys, int i_program )
2698 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
2701 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2702 for( int i = 0; i < p_pat->programs.i_size; i++ )
2704 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
2705 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2706 if( p_pmt->i_number == i_program )
2712 #define PROBE_CHUNK_COUNT 250
2714 static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_pcr, bool *pb_found )
2716 demux_sys_t *p_sys = p_demux->p_sys;
2718 block_t *p_pkt = NULL;
2723 if( i_count++ > PROBE_CHUNK_COUNT || !( p_pkt = ReadTSPacket( p_demux ) ) )
2728 int i_pid = PIDGet( p_pkt );
2729 p_sys->pid[i_pid].i_flags = FLAG_SEEN;
2731 if( i_pid != 0x1FFF && p_sys->pid[i_pid].type == TYPE_PES &&
2732 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
2733 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
2736 bool b_pcrresult = true;
2738 if( p_pkt->i_buffer >= 4 + 2 + 5 )
2739 *pi_pcr = GetPCR( p_pkt );
2743 b_pcrresult = false;
2746 unsigned i_skip = 4;
2747 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
2748 i_skip += 1 + p_pkt->p_buffer[4];
2750 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2751 p_pkt->i_buffer - i_skip,
2752 &i_skip, &i_dts, &i_pts ) )
2756 else if( i_pts != -1 )
2761 if( *pi_pcr != -1 ) // TODO: non ES PCR
2763 ts_pid_t *pmtpid = p_sys->pid[i_pid].p_parent;
2764 assert(pmtpid->type == TYPE_PMT);
2766 if( i_program == 0 || i_program == pmtpid->u.p_pmt->i_number )
2770 pmtpid->u.p_pmt->i_last_dts = *pi_pcr;
2772 /* Start, only keep first */
2773 else if( b_pcrresult && pmtpid->u.p_pmt->pcr.i_first == -1 )
2775 pmtpid->u.p_pmt->pcr.i_first = *pi_pcr;
2777 else if( pmtpid->u.p_pmt->pcr.i_first_dts < VLC_TS_0 )
2779 pmtpid->u.p_pmt->pcr.i_first_dts = VLC_TS_0 + *pi_pcr * 100 / 9;
2786 block_Release( p_pkt );
2792 static int ProbeStart( demux_t *p_demux, int i_program )
2794 demux_sys_t *p_sys = p_demux->p_sys;
2795 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2796 int64_t i_stream_size = stream_Size( p_sys->stream );
2798 int i_probe_count = 0;
2801 bool b_found = false;
2805 i_pos = p_sys->i_packet_size * i_probe_count;
2806 i_pos = __MIN( i_pos, i_stream_size );
2808 if( stream_Seek( p_sys->stream, i_pos ) )
2809 return VLC_EGENERIC;
2811 ProbeChunk( p_demux, i_program, false, &i_pcr, &b_found );
2813 /* Go ahead one more chunk if end of file contained only stuffing packets */
2814 i_probe_count += PROBE_CHUNK_COUNT;
2815 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (2 * PROBE_CHUNK_COUNT) );
2817 stream_Seek( p_sys->stream, i_initial_pos );
2819 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2822 static int ProbeEnd( demux_t *p_demux, int i_program )
2824 demux_sys_t *p_sys = p_demux->p_sys;
2825 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2826 int64_t i_stream_size = stream_Size( p_sys->stream );
2828 int i_probe_count = PROBE_CHUNK_COUNT;
2831 bool b_found = false;
2835 i_pos = i_stream_size - (p_sys->i_packet_size * i_probe_count);
2836 i_pos = __MAX( i_pos, 0 );
2838 if( stream_Seek( p_sys->stream, i_pos ) )
2839 return VLC_EGENERIC;
2841 ProbeChunk( p_demux, i_program, true, &i_pcr, &b_found );
2843 /* Go ahead one more chunk if end of file contained only stuffing packets */
2844 i_probe_count += PROBE_CHUNK_COUNT;
2845 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (6 * PROBE_CHUNK_COUNT) );
2847 stream_Seek( p_sys->stream, i_initial_pos );
2849 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2852 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_pmt, mtime_t i_pcr )
2854 demux_sys_t *p_sys = p_demux->p_sys;
2856 /* Check if we have enqueued blocks waiting the/before the
2857 PCR barrier, and then adapt pcr so they have valid PCR when dequeuing */
2858 if( p_pmt->pcr.i_current == -1 )
2860 mtime_t i_mindts = -1;
2862 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2863 for( int i=0; i< p_pat->programs.i_size; i++ )
2865 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2866 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2868 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[j];
2869 block_t *p_block = p_pid->u.p_pes->p_prepcr_outqueue;
2870 while( p_block && p_block->i_dts == VLC_TS_INVALID )
2871 p_block = p_block->p_next;
2873 if( p_block && ( i_mindts == -1 || p_block->i_dts < i_mindts ) )
2874 i_mindts = p_block->i_dts;
2878 if( i_mindts > VLC_TS_INVALID )
2880 msg_Dbg( p_demux, "Program %d PCR prequeue fixup %"PRId64"->%"PRId64,
2881 p_pmt->i_number, TO_SCALE(i_mindts), i_pcr );
2882 i_pcr = TO_SCALE(i_mindts);
2886 p_pmt->pcr.i_current = i_pcr;
2887 if( p_pmt->pcr.i_first == -1 )
2889 p_pmt->pcr.i_first = i_pcr; // now seen
2892 if ( p_sys->i_pmt_es )
2894 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2895 p_pmt->i_number, VLC_TS_0 + i_pcr * 100 / 9 );
2899 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2901 demux_sys_t *p_sys = p_demux->p_sys;
2903 if( p_sys->i_pmt_es <= 0 )
2906 mtime_t i_pcr = GetPCR( p_bk );
2910 pid->probed.i_pcr_count++;
2912 if(unlikely(p_sys->pid[0].type != TYPE_PAT))
2915 /* Search program and set the PCR */
2916 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
2917 for( int i = 0; i < p_pat->programs.i_size; i++ )
2919 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2920 mtime_t i_program_pcr = TimeStampWrapAround( p_pmt, i_pcr );
2922 if( p_pmt->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
2924 if( pid->p_parent ) /* PCR shall be on pid itself */
2926 /* ? update PCR for the whole group program ? */
2927 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
2931 msg_Warn(p_demux, "discarding PCR update from pid %d which has no owner", pid->i_pid);
2935 else /* set PCR provided by current pid to program(s) referencing it */
2937 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
2938 if( p_pmt->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
2940 /* We've found a target group for update */
2941 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
2948 static int FindPCRCandidate( ts_pmt_t *p_pmt )
2950 ts_pid_t *p_cand = NULL;
2951 int i_previous = p_pmt->i_pid_pcr;
2953 for( int i=0; i<p_pmt->e_streams.i_size; i++ )
2955 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[i];
2957 (!p_cand || p_cand->i_pid != i_previous) )
2959 if( p_pid->probed.i_pcr_count ) /* check PCR frequency first */
2961 if( !p_cand || p_pid->probed.i_pcr_count > p_cand->probed.i_pcr_count )
2968 if( p_pid->u.p_pes->es.fmt.i_cat == AUDIO_ES )
2973 else if ( p_pid->u.p_pes->es.fmt.i_cat == VIDEO_ES ) /* Otherwise prioritize video dts */
2975 if( !p_cand || p_cand->u.p_pes->es.fmt.i_cat == AUDIO_ES )
2982 return p_cand->i_pid;
2987 static void PCRFixHandle( demux_t *p_demux, ts_pmt_t *p_pmt, block_t *p_block )
2989 if( p_pmt->pcr.i_first > -1 || p_pmt->pcr.b_disable )
2992 /* Record the first data packet timestamp in case there wont be any PCR */
2993 if( !p_pmt->pcr.i_first_dts )
2995 p_pmt->pcr.i_first_dts = p_block->i_dts;
2997 else if( p_block->i_dts - p_pmt->pcr.i_first_dts > CLOCK_FREQ / 2 ) /* "shall not exceed 100ms" */
2999 int i_cand = FindPCRCandidate( p_pmt );
3000 p_pmt->i_pid_pcr = i_cand;
3001 p_pmt->pcr.b_disable = true; /* So we do not wait packet PCR flag as there might be none on the pid */
3002 msg_Warn( p_demux, "No PCR received for program %d, set up workaround using pid %d",
3003 p_pmt->i_number, i_cand );
3007 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
3009 const uint8_t *p = p_bk->p_buffer;
3010 const bool b_unit_start = p[1]&0x40;
3011 const bool b_scrambled = p[3]&0x80;
3012 const bool b_adaptation = p[3]&0x20;
3013 const bool b_payload = p[3]&0x10;
3014 const int i_cc = p[3]&0x0f; /* continuity counter */
3015 bool b_discontinuity = false; /* discontinuity */
3017 /* transport_scrambling_control is ignored */
3022 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
3023 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
3027 /* For now, ignore additional error correction
3028 * TODO: handle Reed-Solomon 204,188 error correction */
3029 p_bk->i_buffer = TS_PACKET_SIZE_188;
3033 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
3035 if( pid->u.p_pes->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
3036 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3039 if( p_demux->p_sys->csa )
3041 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
3042 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
3043 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
3048 /* We don't have any adaptation_field, so payload starts
3049 * immediately after the 4 byte TS header */
3054 /* p[4] is adaptation_field_length minus one */
3058 /* discontinuity indicator found in stream */
3059 b_discontinuity = (p[5]&0x80) ? true : false;
3060 if( b_discontinuity && pid->u.p_pes->p_data )
3062 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
3064 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
3068 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
3073 /* Test continuity counter */
3074 /* continuous when (one of this):
3076 * diff == 0 and payload == 0
3077 * diff == 0 and duplicate packet (playload != 0) <- should we
3078 * test the content ?
3080 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
3081 if( b_payload && i_diff == 1 )
3083 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
3087 if( pid->i_cc == 0xff )
3089 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
3093 else if( i_diff != 0 && !b_discontinuity )
3095 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
3096 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
3099 if( pid->u.p_pes->p_data && pid->u.p_pes->es.fmt.i_cat != VIDEO_ES &&
3100 pid->u.p_pes->es.fmt.i_cat != AUDIO_ES )
3102 /* Small audio/video artifacts are usually better than
3103 * dropping full frames */
3104 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3109 PCRHandle( p_demux, pid, p_bk );
3111 if( i_skip >= 188 || pid->u.p_pes->es.id == NULL )
3113 block_Release( p_bk );
3118 if( !SCRAMBLED(*pid) != !b_scrambled )
3120 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
3121 pid->i_pid, SCRAMBLED(*pid), b_scrambled );
3123 pid->i_flags |= (b_scrambled) ? FLAG_SCRAMBLED : FLAGS_NONE;
3125 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
3127 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
3128 pid->u.p_pes->extra_es.p_elems[i]->id, b_scrambled );
3130 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
3131 pid->u.p_pes->es.id, b_scrambled );
3134 /* We have to gather it */
3135 p_bk->p_buffer += i_skip;
3136 p_bk->i_buffer -= i_skip;
3140 if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
3142 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
3143 block_t *p = block_Duplicate( p_bk );
3146 p->i_buffer = i_pointer_field;
3148 block_ChainLastAppend( &pid->u.p_pes->pp_last, p );
3150 p_bk->i_buffer -= 1 + i_pointer_field;
3151 p_bk->p_buffer += 1 + i_pointer_field;
3153 if( pid->u.p_pes->p_data )
3155 ParseData( p_demux, pid );
3159 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3160 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
3162 if( p_bk->i_buffer > 6 )
3164 pid->u.p_pes->i_data_size = GetWBE( &p_bk->p_buffer[4] );
3165 if( pid->u.p_pes->i_data_size > 0 )
3167 pid->u.p_pes->i_data_size += 6;
3171 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
3173 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
3175 pid->u.p_pes->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
3178 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3179 if( pid->u.p_pes->i_data_size > 0 &&
3180 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3182 ParseData( p_demux, pid );
3188 if( pid->u.p_pes->p_data == NULL )
3190 /* msg_Dbg( p_demux, "broken packet" ); */
3191 block_Release( p_bk );
3195 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3196 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3198 if( pid->u.p_pes->i_data_size > 0 &&
3199 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3201 ParseData( p_demux, pid );
3210 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
3212 switch( i_stream_type )
3214 case 0x01: /* MPEG-1 video */
3215 case 0x02: /* MPEG-2 video */
3216 case 0x80: /* MPEG-2 MOTO video */
3217 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
3219 case 0x03: /* MPEG-1 audio */
3220 case 0x04: /* MPEG-2 audio */
3221 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
3223 case 0x11: /* MPEG4 (audio) LATM */
3224 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
3225 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
3226 transport syntax, such as DST, ALS and SLS */
3227 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
3229 case 0x10: /* MPEG4 (video) */
3230 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
3232 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
3233 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
3235 case 0x24: /* HEVC */
3236 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
3238 case 0x42: /* CAVS (Chinese AVS) */
3239 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
3242 case 0x81: /* A52 (audio) */
3243 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
3245 case 0x82: /* SCTE-27 (sub) */
3246 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
3248 case 0x84: /* SDDS (audio) */
3249 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
3251 case 0x85: /* DTS (audio) */
3252 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
3254 case 0x87: /* E-AC3 */
3255 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
3258 case 0x91: /* A52 vls (audio) */
3259 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
3261 case 0x92: /* DVD_SPU vls (sub) */
3262 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
3265 case 0x94: /* SDDS (audio) */
3266 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
3269 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
3270 es_format_Init( fmt, UNKNOWN_ES, 0 );
3273 case 0x06: /* PES_PRIVATE (fixed later) */
3274 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
3275 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
3277 es_format_Init( fmt, UNKNOWN_ES, 0 );
3281 /* PES packets usually contain truncated frames */
3282 fmt->b_packetized = false;
3285 /*****************************************************************************
3286 * MP4 specific functions (IOD parser)
3287 *****************************************************************************/
3288 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
3291 unsigned int i_len = 0;
3297 i_len = ( i_len << 7 ) + ( i_b&0x7f );
3299 } while( i_b&0x80 && *pi_data > 0 );
3301 if (i_len > *pi_data)
3307 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
3310 while( *pi_data > 0 && bytes-- )
3321 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
3323 int len = IODGetBytes( pi_data, pp_data, 1 );
3326 char *url = strndup( (char*)*pp_data, len );
3332 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
3334 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
3336 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
3345 byte1 = IODGetBytes( &i_data, &p_data, 1 );
3346 byte2 = IODGetBytes( &i_data, &p_data, 1 );
3347 byte3 = IODGetBytes( &i_data, &p_data, 1 );
3348 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
3350 i_iod_label = byte1;
3353 else //correct implementation of the IOD_descriptor
3355 i_iod_label = byte2;
3359 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
3361 if( i_iod_tag != 0x02 )
3363 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
3367 IODDescriptorLength( &i_data, &p_data );
3369 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
3370 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
3371 i_od_id |= i_flags >> 6;
3372 ts_debug( "\n* od_id:%d", i_od_id );
3373 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
3374 if ((i_flags >> 5) & 0x01)
3376 p_iod->psz_url = IODGetURL( &i_data, &p_data );
3377 ts_debug( "\n* url string:%s", p_iod->psz_url );
3378 ts_debug( "\n*****************************\n" );
3383 p_iod->psz_url = NULL;
3386 /* Profile Level Indication */
3387 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
3388 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
3389 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
3390 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
3391 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
3394 int i_data_sav = i_data;
3395 uint8_t *p_data_sav = p_data;
3396 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
3398 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
3400 p_data = p_data_sav + i_length;
3401 i_data = i_data_sav - i_length;
3403 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
3404 i_length = IODDescriptorLength( &i_data, &p_data );
3406 i_data_sav = i_data;
3407 p_data_sav = p_data;
3411 if ( i_tag != 0x03 )
3413 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
3417 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
3418 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
3419 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
3420 if( b_streamDependenceFlag )
3421 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
3423 if( (i_flags >> 6) & 0x01 )
3424 es_descr->psz_url = IODGetURL( &i_data, &p_data );
3426 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
3427 if( b_OCRStreamFlag )
3428 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
3430 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
3432 ts_debug( "\n* ERR missing DecoderConfigDescr" );
3435 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
3436 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
3437 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
3438 i_flags = IODGetBytes( &i_data, &p_data, 1 );
3439 dec_descr->i_streamType = i_flags >> 2;
3441 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
3442 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
3443 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
3445 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
3447 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
3448 if( dec_descr->i_extra > 0 )
3450 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
3451 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
3452 p_data += dec_descr->i_extra;
3453 i_data -= dec_descr->i_extra;
3458 dec_descr->i_extra = 0;
3459 dec_descr->p_extra = NULL;
3462 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
3464 ts_debug( "\n* ERR missing SLConfigDescr" );
3467 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
3468 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
3471 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
3476 es_descr->b_ok = true;
3482 static void IODFree( iod_descriptor_t *p_iod )
3484 if( p_iod->psz_url )
3486 free( p_iod->psz_url );
3491 for( int i = 0; i < 255; i++ )
3493 #define es_descr p_iod->es_descr[i]
3496 if( es_descr.psz_url )
3497 free( es_descr.psz_url );
3499 free( es_descr.dec_descr.p_extra );
3506 /****************************************************************************
3507 ****************************************************************************
3508 ** libdvbpsi callbacks
3509 ****************************************************************************
3510 ****************************************************************************/
3511 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
3513 demux_sys_t *p_sys = p_demux->p_sys;
3515 for(int i=0; i<p_sys->programs.i_size; i++)
3516 if( p_sys->programs.p_elems[i] == i_pgrm )
3522 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
3524 demux_sys_t *p_sys = p_demux->p_sys;
3526 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
3529 msg_Warn( p_demux, "Switching to non DVB mode" );
3531 /* This doesn't look like a DVB stream so don't try
3532 * parsing the SDT/EDT/TDT */
3534 PIDRelease( p_demux, &p_sys->pid[0x11] );
3535 PIDRelease( p_demux, &p_sys->pid[0x12] );
3536 PIDRelease( p_demux, &p_sys->pid[0x14] );
3537 p_sys->b_dvb_meta = false;
3540 #include "dvb-text.h"
3542 static char *EITConvertToUTF8( demux_t *p_demux,
3543 const unsigned char *psz_instring,
3547 demux_sys_t *p_sys = p_demux->p_sys;
3549 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3551 if ( !p_sys->arib.p_instance )
3552 p_sys->arib.p_instance = arib_instance_new( p_demux );
3553 if ( !p_sys->arib.p_instance )
3555 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3559 char *psz_outstring = NULL;
3562 i_out = i_length * 4;
3563 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3564 if( !psz_outstring )
3567 arib_initialize_decoder( p_decoder );
3568 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3569 psz_outstring, i_out );
3570 arib_finalize_decoder( p_decoder );
3572 return psz_outstring;
3577 /* Deal with no longer broken providers (no switch byte
3578 but sending ISO_8859-1 instead of ISO_6937) without
3579 removing them from the broken providers table
3580 (keep the entry for correctly handling recorded TS).
3582 b_broken = b_broken && i_length && *psz_instring > 0x20;
3585 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3586 return vlc_from_EIT( psz_instring, i_length );
3589 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3591 demux_sys_t *p_sys = p_demux->p_sys;
3592 ts_pid_t *sdt = &p_sys->pid[0x11];
3593 dvbpsi_sdt_service_t *p_srv;
3595 msg_Dbg( p_demux, "SDTCallBack called" );
3597 if( p_sys->es_creation != CREATE_ES ||
3598 !p_sdt->b_current_next ||
3599 p_sdt->i_version == sdt->u.p_psi->i_version )
3601 dvbpsi_sdt_delete( p_sdt );
3605 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3608 p_sdt->i_version, p_sdt->b_current_next,
3609 p_sdt->i_network_id );
3611 p_sys->b_broken_charset = false;
3613 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3616 dvbpsi_descriptor_t *p_dr;
3618 const char *psz_type = NULL;
3619 const char *psz_status = NULL;
3621 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3622 "running=%d free_ca=%d",
3623 p_srv->i_service_id, p_srv->b_eit_schedule,
3624 p_srv->b_eit_present, p_srv->i_running_status,
3627 if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
3629 msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
3630 p_sys->vdr.i_service );
3634 p_meta = vlc_meta_New();
3635 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3637 if( p_dr->i_tag == 0x48 )
3639 static const char *ppsz_type[17] = {
3641 "Digital television service",
3642 "Digital radio sound service",
3644 "NVOD reference service",
3645 "NVOD time-shifted service",
3648 "SECAM coded signal",
3651 "NTSC coded signal",
3652 "Data broadcast service",
3653 "Reserved for Common Interface Usage",
3654 "RCS Map (see EN 301 790 [35])",
3655 "RCS FLS (see EN 301 790 [35])",
3658 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3662 /* Workarounds for broadcasters with broken EPG */
3664 if( p_sdt->i_network_id == 133 )
3665 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3667 /* List of providers using ISO8859-1 */
3668 static const char ppsz_broken_providers[][8] = {
3669 "CSAT", /* CanalSat FR */
3670 "GR1", /* France televisions */
3672 "MR5", /* France 2/M6 HD */
3675 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3677 const size_t i_length = strlen(ppsz_broken_providers[i]);
3678 if( pD->i_service_provider_name_length == i_length &&
3679 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3680 p_sys->b_broken_charset = true;
3683 /* FIXME: Digital+ ES also uses ISO8859-1 */
3685 str1 = EITConvertToUTF8(p_demux,
3686 pD->i_service_provider_name,
3687 pD->i_service_provider_name_length,
3688 p_sys->b_broken_charset );
3689 str2 = EITConvertToUTF8(p_demux,
3691 pD->i_service_name_length,
3692 p_sys->b_broken_charset );
3694 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3695 pD->i_service_type, str1, str2 );
3697 vlc_meta_SetTitle( p_meta, str2 );
3698 vlc_meta_SetPublisher( p_meta, str1 );
3699 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3700 psz_type = ppsz_type[pD->i_service_type];
3706 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3708 static const char *ppsz_status[5] = {
3711 "Starts in a few seconds",
3715 psz_status = ppsz_status[p_srv->i_running_status];
3719 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3721 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3723 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3724 p_srv->i_service_id, p_meta );
3725 vlc_meta_Delete( p_meta );
3728 sdt->u.p_psi->i_version = p_sdt->i_version;
3729 dvbpsi_sdt_delete( p_sdt );
3732 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3733 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3735 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3739 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3740 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3743 /* Count the number of days */
3744 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3745 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3746 for( int i = 70; i < i_year; i++ )
3747 i_day += LEAP(1900+i);
3749 i_day += LEAP(1900+i_year);
3752 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3755 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3757 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3758 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3759 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3761 *p_y = 1900 + yp + c*1;
3762 *p_m = mp - 1 - c*12;
3763 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3765 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3766 static int64_t EITConvertStartTime( uint64_t i_date )
3768 const int i_mjd = i_date >> 24;
3769 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3770 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3771 const int i_second = CVT_FROM_BCD(i_date );
3776 /* if all 40 bits are 1, the start is unknown */
3777 if( i_date == UINT64_C(0xffffffffff) )
3780 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3781 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3783 static int EITConvertDuration( uint32_t i_duration )
3785 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3786 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3787 CVT_FROM_BCD(i_duration );
3791 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3793 demux_sys_t *p_sys = p_demux->p_sys;
3795 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3797 dvbpsi_tot_delete(p_tdt);
3801 static void EITCallBack( demux_t *p_demux,
3802 dvbpsi_eit_t *p_eit, bool b_current_following )
3804 demux_sys_t *p_sys = p_demux->p_sys;
3805 dvbpsi_eit_event_t *p_evt;
3808 msg_Dbg( p_demux, "EITCallBack called" );
3809 if( !p_eit->b_current_next )
3811 dvbpsi_eit_delete( p_eit );
3815 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3816 "ts_id=%d network_id=%d segment_last_section_number=%d "
3819 p_eit->i_version, p_eit->b_current_next,
3820 p_eit->i_ts_id, p_eit->i_network_id,
3821 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3823 p_epg = vlc_epg_New( NULL );
3824 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3826 dvbpsi_descriptor_t *p_dr;
3827 char *psz_name = NULL;
3828 char *psz_text = NULL;
3829 char *psz_extra = strdup("");
3833 int64_t i_tot_time = 0;
3835 i_start = EITConvertStartTime( p_evt->i_start_time );
3836 i_duration = EITConvertDuration( p_evt->i_duration );
3838 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3840 if( p_sys->i_tdt_delta == 0 )
3841 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3843 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3845 tzset(); // JST -> UTC
3846 i_start += timezone; // FIXME: what about DST?
3847 i_tot_time += timezone;
3849 if( p_evt->i_running_status == 0x00 &&
3850 (i_start - 5 < i_tot_time &&
3851 i_tot_time < i_start + i_duration + 5) )
3853 p_evt->i_running_status = 0x04;
3854 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3858 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3859 "running=%d free_ca=%d",
3860 p_evt->i_event_id, (int)i_start, (int)i_duration,
3861 p_evt->i_running_status, p_evt->b_free_ca );
3863 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3869 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3871 /* Only take first description, as we don't handle language-info
3873 if( pE && psz_name == NULL )
3875 psz_name = EITConvertToUTF8( p_demux,
3876 pE->i_event_name, pE->i_event_name_length,
3877 p_sys->b_broken_charset );
3879 psz_text = EITConvertToUTF8( p_demux,
3880 pE->i_text, pE->i_text_length,
3881 p_sys->b_broken_charset );
3882 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3883 pE->i_iso_639_code, psz_name, psz_text );
3890 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3893 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3895 pE->i_descriptor_number, pE->i_last_descriptor_number );
3897 if( pE->i_text_length > 0 )
3899 char *psz_text = EITConvertToUTF8( p_demux,
3900 pE->i_text, pE->i_text_length,
3901 p_sys->b_broken_charset );
3904 msg_Dbg( p_demux, " - text='%s'", psz_text );
3906 psz_extra = xrealloc( psz_extra,
3907 strlen(psz_extra) + strlen(psz_text) + 1 );
3908 strcat( psz_extra, psz_text );
3913 for( int i = 0; i < pE->i_entry_count; i++ )
3915 char *psz_dsc = EITConvertToUTF8( p_demux,
3916 pE->i_item_description[i],
3917 pE->i_item_description_length[i],
3918 p_sys->b_broken_charset );
3919 char *psz_itm = EITConvertToUTF8( p_demux,
3920 pE->i_item[i], pE->i_item_length[i],
3921 p_sys->b_broken_charset );
3923 if( psz_dsc && psz_itm )
3925 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3927 psz_extra = xrealloc( psz_extra,
3928 strlen(psz_extra) + strlen(psz_dsc) +
3929 strlen(psz_itm) + 3 + 1 );
3930 strcat( psz_extra, "(" );
3931 strcat( psz_extra, psz_dsc );
3932 strcat( psz_extra, " " );
3933 strcat( psz_extra, psz_itm );
3934 strcat( psz_extra, ")" );
3946 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
3949 for ( int i = 0; i < pR->i_ratings_number; i++ )
3951 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
3952 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
3954 if ( p_rating->i_rating + 3 > i_min_age )
3955 i_min_age = p_rating->i_rating + 3;
3956 msg_Dbg( p_demux, " - parental control set to %d years",
3965 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3971 if( i_start > 0 && psz_name && psz_text)
3972 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3973 *psz_extra ? psz_extra : NULL, i_min_age );
3975 /* Update "now playing" field */
3976 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
3977 vlc_epg_SetCurrent( p_epg, i_start );
3984 if( p_epg->i_event > 0 )
3986 if( b_current_following &&
3987 ( p_sys->programs.i_size == 0 ||
3988 p_sys->programs.p_elems[0] ==
3992 p_sys->i_dvb_length = 0;
3993 p_sys->i_dvb_start = 0;
3995 if( p_epg->p_current )
3997 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
3998 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
4001 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
4005 vlc_epg_Delete( p_epg );
4007 dvbpsi_eit_delete( p_eit );
4009 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4011 EITCallBack( p_demux, p_eit, true );
4013 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
4015 EITCallBack( p_demux, p_eit, false );
4018 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
4019 uint16_t i_extension, demux_t *p_demux )
4021 demux_sys_t *p_sys = p_demux->p_sys;
4024 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4025 i_table_id, i_table_id, i_extension, i_extension );
4027 if( p_sys->pid[0].u.p_pat->i_version != -1 && i_table_id == 0x42 )
4029 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4030 i_table_id, i_table_id, i_extension, i_extension );
4032 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
4033 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
4035 else if( p_sys->pid[0x11].u.p_psi->i_version != -1 &&
4036 ( i_table_id == 0x4e || /* Current/Following */
4037 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
4039 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4040 i_table_id, i_table_id, i_extension, i_extension );
4042 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
4043 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
4044 (dvbpsi_eit_callback)EITCallBackSchedule;
4046 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
4047 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
4049 else if( p_demux->p_sys->pid[0x11].u.p_psi->i_version != -1 &&
4050 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
4052 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
4053 i_table_id, i_table_id, i_extension, i_extension );
4055 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
4056 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
4060 /*****************************************************************************
4061 * PMT callback and helpers
4062 *****************************************************************************/
4063 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
4066 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
4067 while( p_dr && ( p_dr->i_tag != i_tag ) )
4068 p_dr = p_dr->p_next;
4071 static bool PMTEsHasRegistration( demux_t *p_demux,
4072 const dvbpsi_pmt_es_t *p_es,
4073 const char *psz_tag )
4075 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
4079 if( p_dr->i_length < 4 )
4081 msg_Warn( p_demux, "invalid Registration Descriptor" );
4085 assert( strlen(psz_tag) == 4 );
4086 return !memcmp( p_dr->p_data, psz_tag, 4 );
4089 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
4090 int i_component_tag )
4092 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4095 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4099 return p_si->i_component_tag == i_component_tag;
4102 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pes_es_t *p_es,
4103 const ts_pmt_t *p_pmt, const dvbpsi_pmt_es_t *p_dvbpsies )
4105 es_format_t *p_fmt = &p_es->fmt;
4107 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
4108 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x1f );
4110 if( p_dr && p_dr->i_length == 2 )
4112 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
4114 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
4116 p_es->p_mpeg4desc = NULL;
4118 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
4120 iod_descriptor_t *iod = p_pmt->iod;
4121 if( iod->es_descr[i].i_es_id == i_es_id )
4123 if ( iod->es_descr[i].b_ok )
4124 p_es->p_mpeg4desc = &iod->es_descr[i];
4126 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
4131 if( !p_es->p_mpeg4desc )
4133 switch( p_dvbpsies->i_type )
4135 /* non fatal, set by packetizer */
4136 case 0x0f: /* ADTS */
4137 case 0x11: /* LOAS */
4138 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4139 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4142 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4143 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4149 const decoder_config_descriptor_t *dcd = &p_es->p_mpeg4desc->dec_descr;
4150 if( dcd->i_streamType == 0x04 ) /* VisualStream */
4152 p_fmt->i_cat = VIDEO_ES;
4153 switch( dcd->i_objectTypeIndication )
4155 case 0x0B: /* mpeg4 sub */
4156 p_fmt->i_cat = SPU_ES;
4157 p_fmt->i_codec = VLC_CODEC_SUBT;
4160 case 0x20: /* mpeg4 */
4161 p_fmt->i_codec = VLC_CODEC_MP4V;
4163 case 0x21: /* h264 */
4164 p_fmt->i_codec = VLC_CODEC_H264;
4171 case 0x65: /* mpeg2 */
4172 p_fmt->i_codec = VLC_CODEC_MPGV;
4174 case 0x6a: /* mpeg1 */
4175 p_fmt->i_codec = VLC_CODEC_MPGV;
4177 case 0x6c: /* mpeg1 */
4178 p_fmt->i_codec = VLC_CODEC_JPEG;
4181 p_fmt->i_cat = UNKNOWN_ES;
4185 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
4187 p_fmt->i_cat = AUDIO_ES;
4188 switch( dcd->i_objectTypeIndication )
4190 case 0x40: /* mpeg4 */
4191 p_fmt->i_codec = VLC_CODEC_MP4A;
4195 case 0x68: /* mpeg2 aac */
4196 p_fmt->i_codec = VLC_CODEC_MP4A;
4198 case 0x69: /* mpeg2 */
4199 p_fmt->i_codec = VLC_CODEC_MPGA;
4201 case 0x6b: /* mpeg1 */
4202 p_fmt->i_codec = VLC_CODEC_MPGA;
4205 p_fmt->i_cat = UNKNOWN_ES;
4211 p_fmt->i_cat = UNKNOWN_ES;
4214 if( p_fmt->i_cat != UNKNOWN_ES )
4216 p_fmt->i_extra = dcd->i_extra;
4217 if( p_fmt->i_extra > 0 )
4219 p_fmt->p_extra = malloc( p_fmt->i_extra );
4220 if( p_fmt->p_extra )
4221 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
4234 } ts_teletext_page_t;
4236 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pes_t *p_pes,
4237 const dvbpsi_pmt_es_t *p_dvbpsies )
4239 es_format_t *p_fmt = &p_pes->es.fmt;
4241 ts_teletext_page_t p_page[2 * 64 + 20];
4242 unsigned i_page = 0;
4244 /* Gather pages information */
4245 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
4247 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, i_tag_idx == 0 ? 0x46 : 0x56 );
4251 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
4255 for( int i = 0; i < p_sub->i_pages_number; i++ )
4257 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
4259 if( p_src->i_teletext_type >= 0x06 )
4262 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4264 ts_teletext_page_t *p_dst = &p_page[i_page++];
4266 p_dst->i_type = p_src->i_teletext_type;
4267 p_dst->i_magazine = p_src->i_teletext_magazine_number
4268 ? p_src->i_teletext_magazine_number : 8;
4269 p_dst->i_page = p_src->i_teletext_page_number;
4270 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4274 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4277 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4278 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4280 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
4282 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
4285 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4287 ts_teletext_page_t *p_dst = &p_page[i_page++];
4289 switch( p_src->i_subtitling_type )
4292 p_dst->i_type = 0x02;
4295 p_dst->i_type = 0x03;
4298 /* FIXME check if it is the right split */
4299 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
4300 ? (p_src->i_composition_page_id >> 8) : 8;
4301 p_dst->i_page = p_src->i_composition_page_id & 0xff;
4302 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4307 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
4309 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4311 p_fmt->subs.teletext.i_magazine = -1;
4312 p_fmt->subs.teletext.i_page = 0;
4313 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
4315 dvbpsi_descriptor_t *p_dr;
4316 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x46 );
4318 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x56 );
4320 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4322 /* Descriptor pass-through */
4323 p_fmt->p_extra = malloc( p_dr->i_length );
4324 if( p_fmt->p_extra )
4326 p_fmt->i_extra = p_dr->i_length;
4327 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4333 for( unsigned i = 0; i < i_page; i++ )
4335 ts_pes_es_t *p_page_es;
4340 p_page_es = &p_pes->es;
4344 p_page_es = malloc( sizeof(*p_page_es) );
4348 es_format_Copy( &p_page_es->fmt, p_fmt );
4349 free( p_page_es->fmt.psz_language );
4350 free( p_page_es->fmt.psz_description );
4351 p_page_es->fmt.psz_language = NULL;
4352 p_page_es->fmt.psz_description = NULL;
4354 ARRAY_APPEND( p_pes->extra_es, p_page_es );
4358 const ts_teletext_page_t *p = &p_page[i];
4359 p_page_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
4360 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
4361 p_page_es->fmt.psz_language = strndup( p->p_iso639, 3 );
4362 p_page_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
4363 p_page_es->fmt.subs.teletext.i_magazine = p->i_magazine;
4364 p_page_es->fmt.subs.teletext.i_page = p->i_page;
4367 " * ttxt type=%s lan=%s page=%d%02x",
4368 p_page_es->fmt.psz_description,
4369 p_page_es->fmt.psz_language,
4370 p->i_magazine, p->i_page );
4374 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pes_t *p_pes,
4375 const dvbpsi_pmt_es_t *p_dvbpsies )
4377 es_format_t *p_fmt = &p_pes->es.fmt;
4379 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
4381 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4383 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4384 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4386 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
4387 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
4388 ( i_type >= 0x20 && i_type <= 0x24 ) )
4392 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4394 p_fmt->subs.dvb.i_id = -1;
4395 p_fmt->psz_description = strdup( _("DVB subtitles") );
4397 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4399 /* Descriptor pass-through */
4400 p_fmt->p_extra = malloc( p_dr->i_length );
4401 if( p_fmt->p_extra )
4403 p_fmt->i_extra = p_dr->i_length;
4404 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4410 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4412 ts_pes_es_t *p_subs_es;
4417 p_subs_es = &p_pes->es;
4421 p_subs_es = malloc( sizeof(*p_subs_es) );
4425 es_format_Copy( &p_subs_es->fmt, p_fmt );
4426 free( p_subs_es->fmt.psz_language );
4427 free( p_subs_es->fmt.psz_description );
4428 p_subs_es->fmt.psz_language = NULL;
4429 p_subs_es->fmt.psz_description = NULL;
4431 ARRAY_APPEND( p_pes->extra_es, p_subs_es );
4435 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
4436 p_subs_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
4437 switch( p->i_subtitling_type )
4439 case 0x10: /* unspec. */
4440 case 0x11: /* 4:3 */
4441 case 0x12: /* 16:9 */
4442 case 0x13: /* 2.21:1 */
4443 case 0x14: /* HD monitor */
4444 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles") );
4446 case 0x20: /* Hearing impaired unspec. */
4447 case 0x21: /* h.i. 4:3 */
4448 case 0x22: /* h.i. 16:9 */
4449 case 0x23: /* h.i. 2.21:1 */
4450 case 0x24: /* h.i. HD monitor */
4451 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
4458 p_subs_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
4459 ( p->i_ancillary_page_id << 16 );
4464 static int vlc_ceil_log2( const unsigned int val )
4466 int n = 31 - clz(val);
4467 if ((1U << n) != val)
4473 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
4477 /* default mapping */
4478 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
4479 memcpy(h.stream_map, map, sizeof(map));
4483 int stream_count = 0;
4484 int ccc = p[1]; // channel_config_code
4488 mapping = channels > 2;
4491 channels = 2; // dual mono
4493 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
4494 csc = p_csc[channels - 1];
4495 stream_count = channels - csc;
4497 static const uint8_t map[6][7] = {
4506 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4507 } else if (ccc == 0x81) {
4509 goto explicit_config_too_short;
4516 bs_init(&s, &p[4], len - 4);
4519 int bits = vlc_ceil_log2(channels);
4520 if (s.i_left < bits)
4521 goto explicit_config_too_short;
4522 stream_count = bs_read(&s, bits) + 1;
4523 bits = vlc_ceil_log2(stream_count + 1);
4524 if (s.i_left < bits)
4525 goto explicit_config_too_short;
4526 csc = bs_read(&s, bits);
4528 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4529 if (s.i_left < channels * channel_bits)
4530 goto explicit_config_too_short;
4532 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4533 for (int i = 0; i < channels; i++) {
4534 unsigned char m = bs_read(&s, channel_bits);
4537 h.stream_map[i] = m;
4540 } else if (ccc >= 0x80 && ccc <= 0x88) {
4541 channels = ccc - 0x80;
4546 channels = 2; // dual mono
4549 stream_count = channels;
4551 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4555 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4559 opus_prepare_header(channels, 0, &h);
4561 h.input_sample_rate = 48000;
4563 h.nb_streams = channels - csc;
4564 h.channel_mapping = mapping;
4567 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4568 if (p_fmt->p_extra) {
4569 p_fmt->i_cat = AUDIO_ES;
4570 p_fmt->i_codec = VLC_CODEC_OPUS;
4571 p_fmt->audio.i_channels = h.channels;
4572 p_fmt->audio.i_rate = 48000;
4578 explicit_config_too_short:
4579 msg_Err(demux, "Opus descriptor too short");
4582 static void PMTSetupEs0x06( demux_t *p_demux, ts_pes_t *p_pes,
4583 const dvbpsi_pmt_es_t *p_dvbpsies )
4585 es_format_t *p_fmt = &p_pes->es.fmt;
4586 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4587 dvbpsi_descriptor_t *desc;
4589 if( PMTEsHasRegistration( p_demux, p_dvbpsies, "AC-3" ) ||
4590 PMTEsFindDescriptor( p_dvbpsies, 0x6a ) ||
4591 PMTEsFindDescriptor( p_dvbpsies, 0x81 ) )
4593 p_fmt->i_cat = AUDIO_ES;
4594 p_fmt->i_codec = VLC_CODEC_A52;
4596 else if( (desc = PMTEsFindDescriptor( p_dvbpsies, 0x7f ) ) && desc->i_length >= 2 &&
4597 PMTEsHasRegistration(p_demux, p_dvbpsies, "Opus"))
4599 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4601 else if( PMTEsFindDescriptor( p_dvbpsies, 0x7a ) )
4603 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4604 p_fmt->i_cat = AUDIO_ES;
4605 p_fmt->i_codec = VLC_CODEC_EAC3;
4607 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS1" ) ||
4608 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS2" ) ||
4609 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS3" ) ||
4610 PMTEsFindDescriptor( p_dvbpsies, 0x73 ) )
4612 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4613 p_fmt->i_cat = AUDIO_ES;
4614 p_fmt->i_codec = VLC_CODEC_DTS;
4616 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "BSSD" ) && !p_subs_dr )
4618 /* BSSD is AES3 DATA, but could also be subtitles
4619 * we need to check for secondary descriptor then s*/
4620 p_fmt->i_cat = AUDIO_ES;
4621 p_fmt->b_packetized = true;
4622 p_fmt->i_codec = VLC_CODEC_302M;
4624 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "HEVC" ) )
4626 p_fmt->i_cat = VIDEO_ES;
4627 p_fmt->i_codec = VLC_CODEC_HEVC;
4629 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4631 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4632 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xFD );
4633 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4634 if ( p_dr && p_dr->i_length >= 2 )
4636 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4637 PMTEsHasComponentTag( p_dvbpsies, 0x30 ) ||
4638 PMTEsHasComponentTag( p_dvbpsies, 0x31 ) ||
4639 PMTEsHasComponentTag( p_dvbpsies, 0x32 ) ||
4640 PMTEsHasComponentTag( p_dvbpsies, 0x33 ) ||
4641 PMTEsHasComponentTag( p_dvbpsies, 0x34 ) ||
4642 PMTEsHasComponentTag( p_dvbpsies, 0x35 ) ||
4643 PMTEsHasComponentTag( p_dvbpsies, 0x36 ) ||
4644 PMTEsHasComponentTag( p_dvbpsies, 0x37 ) ) )
4646 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_A );
4647 p_fmt->psz_language = strndup ( "jpn", 3 );
4648 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4650 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4651 PMTEsHasComponentTag( p_dvbpsies, 0x87 ) ||
4652 PMTEsHasComponentTag( p_dvbpsies, 0x88 ) ) )
4654 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_C );
4655 p_fmt->psz_language = strndup ( "jpn", 3 );
4656 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4662 /* Subtitle/Teletext/VBI fallbacks */
4663 dvbpsi_subtitling_dr_t *p_sub;
4664 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4666 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4668 if( p_fmt->i_cat != UNKNOWN_ES )
4671 switch( p_sub->p_subtitle[i].i_subtitling_type )
4673 case 0x01: /* EBU Teletext subtitles */
4674 case 0x02: /* Associated EBU Teletext */
4675 case 0x03: /* VBI data */
4676 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4678 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4679 case 0x11: /* ... on 4:3 AR monitor */
4680 case 0x12: /* ... on 16:9 AR monitor */
4681 case 0x13: /* ... on 2.21:1 AR monitor */
4682 case 0x14: /* ... for display on a high definition monitor */
4683 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4684 case 0x21: /* ... on 4:3 AR monitor */
4685 case 0x22: /* ... on 16:9 AR monitor */
4686 case 0x23: /* ... on 2.21:1 AR monitor */
4687 case 0x24: /* ... for display on a high definition monitor */
4688 PMTSetupEsDvbSubtitle( p_demux, p_pes, p_dvbpsies );
4691 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4692 p_sub->p_subtitle[i].i_subtitling_type );
4698 if( p_fmt->i_cat == UNKNOWN_ES &&
4699 ( PMTEsFindDescriptor( p_dvbpsies, 0x45 ) || /* VBI Data descriptor */
4700 PMTEsFindDescriptor( p_dvbpsies, 0x46 ) || /* VBI Teletext descriptor */
4701 PMTEsFindDescriptor( p_dvbpsies, 0x56 ) ) ) /* EBU Teletext descriptor */
4704 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4708 /* FIXME is it useful ? */
4709 if( PMTEsFindDescriptor( p_dvbpsies, 0x52 ) )
4711 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x52 );
4712 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4714 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4718 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pes_es_t *p_es,
4719 const dvbpsi_pmt_es_t *p_dvbpsies )
4721 /* Registration Descriptor */
4722 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "VC-1" ) )
4724 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4728 es_format_t *p_fmt = &p_es->fmt;
4730 /* registration descriptor for VC-1 (SMPTE rp227) */
4731 p_fmt->i_cat = VIDEO_ES;
4732 p_fmt->i_codec = VLC_CODEC_VC1;
4734 /* XXX With Simple and Main profile the SEQUENCE
4735 * header is modified: video width and height are
4736 * inserted just after the start code as 2 int16_t
4737 * The packetizer will take care of that. */
4740 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pes_es_t *p_es,
4741 const dvbpsi_pmt_es_t *p_dvbpsies )
4743 /* Registration Descriptor */
4744 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "drac" ) )
4746 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4750 es_format_t *p_fmt = &p_es->fmt;
4752 /* registration descriptor for Dirac
4753 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4754 p_fmt->i_cat = VIDEO_ES;
4755 p_fmt->i_codec = VLC_CODEC_DIRAC;
4758 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pes_es_t *p_es,
4759 const dvbpsi_pmt_es_t *p_dvbpsies )
4761 /* MSCODEC sent by vlc */
4762 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xa0 );
4763 if( !p_dr || p_dr->i_length < 10 )
4766 "private MSCODEC (vlc) without bih private descriptor" );
4770 es_format_t *p_fmt = &p_es->fmt;
4771 p_fmt->i_cat = VIDEO_ES;
4772 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4773 p_dr->p_data[2], p_dr->p_data[3] );
4774 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4775 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4776 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4778 if( p_fmt->i_extra > 0 )
4780 p_fmt->p_extra = malloc( p_fmt->i_extra );
4781 if( p_fmt->p_extra )
4782 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4783 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4787 /* For such stream we will gather them ourself and don't launch a
4789 * Yes it's ugly but it's the only way to have DIV3 working */
4790 p_fmt->b_packetized = true;
4793 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pes_es_t *p_es, int i_pid )
4795 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4796 * with audio track pid being 0x1100..0x11FF */
4797 if ( p_pmt->i_program_number == 0x1 &&
4798 p_pmt->i_pcr_pid == 0x1000 &&
4799 ( i_pid >> 8 ) == 0x11 )
4801 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4802 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4805 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4808 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pes_es_t *p_es,
4809 const dvbpsi_pmt_es_t *p_dvbpsies )
4811 es_format_t *p_fmt = &p_es->fmt;
4813 /* Blu-Ray mapping */
4814 switch( p_dvbpsies->i_type )
4817 p_fmt->i_cat = AUDIO_ES;
4818 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4821 p_fmt->i_cat = AUDIO_ES;
4822 p_fmt->i_codec = VLC_CODEC_A52;
4825 case 0x85: /* DTS-HD High resolution audio */
4826 case 0x86: /* DTS-HD Master audio */
4827 case 0xA2: /* Secondary DTS audio */
4828 p_fmt->i_cat = AUDIO_ES;
4829 p_fmt->i_codec = VLC_CODEC_DTS;
4832 case 0x83: /* TrueHD AC3 */
4833 p_fmt->i_cat = AUDIO_ES;
4834 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4837 case 0x84: /* E-AC3 */
4838 case 0xA1: /* Secondary E-AC3 */
4839 p_fmt->i_cat = AUDIO_ES;
4840 p_fmt->i_codec = VLC_CODEC_EAC3;
4842 case 0x90: /* Presentation graphics */
4843 p_fmt->i_cat = SPU_ES;
4844 p_fmt->i_codec = VLC_CODEC_BD_PG;
4846 case 0x91: /* Interactive graphics */
4847 case 0x92: /* Subtitle */
4850 p_fmt->i_cat = VIDEO_ES;
4851 p_fmt->i_codec = VLC_CODEC_VC1;
4854 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4855 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4862 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pes_es_t *p_es,
4863 const dvbpsi_pmt_es_t *p_dvbpsies )
4869 vlc_fourcc_t i_codec;
4871 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4872 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4873 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4874 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4875 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4876 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4877 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4878 { "", UNKNOWN_ES, 0 }
4880 es_format_t *p_fmt = &p_es->fmt;
4882 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4884 if( PMTEsHasRegistration( p_demux, p_dvbpsies, p_regs[i].psz_tag ) )
4886 p_fmt->i_cat = p_regs[i].i_cat;
4887 p_fmt->i_codec = p_regs[i].i_codec;
4888 if (p_dvbpsies->i_type == 0x87)
4889 p_fmt->i_codec = VLC_CODEC_EAC3;
4896 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4898 static const char *audio_type[] = {
4900 N_("clean effects"),
4901 N_("hearing impaired"),
4902 N_("visual impaired commentary"),
4905 if (type < 0 || type > 3)
4906 msg_Dbg( p_demux, "unknown audio type: %d", type);
4908 return strdup(audio_type[type]);
4912 static void PMTParseEsIso639( demux_t *p_demux, ts_pes_es_t *p_es,
4913 const dvbpsi_pmt_es_t *p_dvbpsies )
4915 /* get language descriptor */
4916 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x0a );
4921 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4924 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4928 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4929 p_es->fmt.psz_language = malloc( 4 );
4930 if( p_es->fmt.psz_language )
4932 memcpy( p_es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
4933 p_es->fmt.psz_language[3] = 0;
4934 msg_Dbg( p_demux, "found language: %s", p_es->fmt.psz_language);
4936 int type = p_decoded->code[0].i_audio_type;
4937 p_es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
4939 p_es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
4941 p_es->fmt.i_extra_languages = p_decoded->i_code_count-1;
4942 if( p_es->fmt.i_extra_languages > 0 )
4943 p_es->fmt.p_extra_languages =
4944 malloc( sizeof(*p_es->fmt.p_extra_languages) *
4945 p_es->fmt.i_extra_languages );
4946 if( p_es->fmt.p_extra_languages )
4948 for( unsigned i = 0; i < p_es->fmt.i_extra_languages; i++ )
4950 p_es->fmt.p_extra_languages[i].psz_language = malloc(4);
4951 if( p_es->fmt.p_extra_languages[i].psz_language )
4953 memcpy( p_es->fmt.p_extra_languages[i].psz_language,
4954 p_decoded->code[i+1].iso_639_code, 3 );
4955 p_es->fmt.p_extra_languages[i].psz_language[3] = '\0';
4957 int type = p_decoded->code[i].i_audio_type;
4958 p_es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
4962 p_es->fmt.psz_language = malloc( 4 );
4963 if( p_es->fmt.psz_language )
4965 memcpy( p_es->fmt.psz_language,
4966 p_decoded->i_iso_639_code, 3 );
4967 p_es->fmt.psz_language[3] = 0;
4972 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid )
4974 demux_sys_t *p_sys = p_demux->p_sys;
4975 bool b_create_delayed = false;
4979 if( SEEN(*pid) && p_sys->es_creation == DELAY_ES )
4981 p_sys->es_creation = CREATE_ES;
4982 b_create_delayed = true;
4985 if( p_sys->es_creation == CREATE_ES )
4987 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
4988 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
4990 pid->u.p_pes->extra_es.p_elems[i]->id =
4991 es_out_Add( p_demux->out, &pid->u.p_pes->extra_es.p_elems[i]->fmt );
4993 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
4996 else if( p_sys->es_creation == DELAY_ES )
4998 p_sys->es_creation = CREATE_ES;
4999 b_create_delayed = true;
5002 if( b_create_delayed )
5004 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5005 for( int i=0; i< p_pat->programs.i_size; i++ )
5007 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
5008 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
5010 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
5011 if( pid->u.p_pes->es.id )
5014 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
5015 for( int k = 0; k < pid->u.p_pes->extra_es.i_size; k++ )
5017 pid->u.p_pes->extra_es.p_elems[k]->id =
5018 es_out_Add( p_demux->out, &pid->u.p_pes->extra_es.p_elems[k]->fmt );
5020 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
5022 if( pid->u.p_pes->es.id != NULL && ProgramIsSelected( p_demux, pid->p_parent->u.p_pmt->i_number ) )
5023 SetPIDFilter( p_demux, pid->i_pid, true );
5029 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_dvbpsipmt )
5031 demux_t *p_demux = data;
5032 demux_sys_t *p_sys = p_demux->p_sys;
5034 ts_pid_t *pmtpid = NULL;
5035 ts_pmt_t *p_pmt = NULL;
5037 msg_Dbg( p_demux, "PMTCallBack called" );
5039 if (unlikely(p_sys->pid[0].type != TYPE_PAT))
5041 assert(p_sys->pid[0].type == TYPE_PAT);
5042 dvbpsi_pmt_delete(p_dvbpsipmt);
5045 const ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5047 /* First find this PMT declared in PAT */
5048 for( int i = 0; !pmtpid && i < p_pat->programs.i_size; i++ )
5050 const int i_pmt_prgnumber = p_pat->programs.p_elems[i]->u.p_pmt->i_number;
5051 if( i_pmt_prgnumber != TS_USER_PMT_NUMBER &&
5052 i_pmt_prgnumber == p_dvbpsipmt->i_program_number )
5054 pmtpid = p_pat->programs.p_elems[i];
5055 assert(pmtpid->type == TYPE_PMT);
5056 p_pmt = pmtpid->u.p_pmt;
5060 if( pmtpid == NULL )
5062 msg_Warn( p_demux, "unreferenced program (broken stream)" );
5063 dvbpsi_pmt_delete(p_dvbpsipmt);
5067 pmtpid->i_flags |= FLAG_SEEN;
5069 if( p_pmt->i_version != -1 &&
5070 ( !p_dvbpsipmt->b_current_next || p_pmt->i_version == p_dvbpsipmt->i_version ) )
5072 dvbpsi_pmt_delete( p_dvbpsipmt );
5076 /* Save old es array */
5077 DECL_ARRAY(ts_pid_t *) old_es_rm;
5078 old_es_rm.i_alloc = p_pmt->e_streams.i_alloc;
5079 old_es_rm.i_size = p_pmt->e_streams.i_size;
5080 old_es_rm.p_elems = p_pmt->e_streams.p_elems;
5081 ARRAY_INIT(p_pmt->e_streams);
5085 IODFree( p_pmt->iod );
5089 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
5090 p_dvbpsipmt->i_program_number, p_dvbpsipmt->i_version, p_dvbpsipmt->i_pcr_pid );
5091 p_pmt->i_pid_pcr = p_dvbpsipmt->i_pcr_pid;
5092 p_pmt->i_version = p_dvbpsipmt->i_version;
5094 ValidateDVBMeta( p_demux, p_pmt->i_pid_pcr );
5096 if( ProgramIsSelected( p_demux, p_pmt->i_number ) )
5097 SetPIDFilter( p_demux, p_pmt->i_pid_pcr, true ); /* Set demux filter */
5099 /* Parse PMT descriptors */
5100 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
5101 dvbpsi_descriptor_t *p_dr;
5103 /* First pass for standard detection */
5104 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
5106 int i_arib_flags = 0; /* Descriptors can be repeated */
5107 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5113 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
5114 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
5118 i_arib_flags |= 1 << 1;
5121 i_arib_flags |= 1 << 2;
5127 if ( i_arib_flags == 0x07 ) //0b111
5128 p_sys->arib.e_mode = ARIBMODE_ENABLED;
5131 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5133 /* special descriptors handling */
5136 case 0x1d: /* We have found an IOD descriptor */
5137 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
5138 p_pmt->iod = IODNew( p_dr->i_length, p_dr->p_data );
5142 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5143 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5146 case 0x5: /* Registration Descriptor */
5147 if( p_dr->i_length != 4 )
5149 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
5153 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
5154 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
5155 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
5160 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
5164 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
5167 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
5168 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
5169 /* TODO: apply visibility flags */
5173 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
5177 dvbpsi_pmt_es_t *p_dvbpsies;
5178 for( p_dvbpsies = p_dvbpsipmt->p_first_es; p_dvbpsies != NULL; p_dvbpsies = p_dvbpsies->p_next )
5180 bool b_reusing_pid = false;
5183 ts_pid_t *pespid = &p_sys->pid[p_dvbpsies->i_pid];
5185 /* Find out if the PID was already declared */
5186 for( int i = 0; i < old_es_rm.i_size; i++ )
5188 if( old_es_rm.p_elems[i]->i_pid == p_dvbpsies->i_pid )
5190 b_reusing_pid = true;
5194 ValidateDVBMeta( p_demux, p_dvbpsies->i_pid );
5196 char const * psz_typedesc = "";
5197 switch(p_dvbpsies->i_type)
5200 psz_typedesc = "ISO/IEC Reserved";
5203 psz_typedesc = "ISO/IEC 11172 Video";
5206 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
5209 psz_typedesc = "ISO/IEC 11172 Audio";
5212 psz_typedesc = "ISO/IEC 13818-3 Audio";
5215 psz_typedesc = "ISO/IEC 13818-1 private_sections";
5218 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
5221 psz_typedesc = "ISO/IEC 13522 MHEG";
5224 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
5227 psz_typedesc = "ITU-T Rec. H.222.1";
5230 psz_typedesc = "ISO/IEC 13818-6 type A";
5233 psz_typedesc = "ISO/IEC 13818-6 type B";
5236 psz_typedesc = "ISO/IEC 13818-6 type C";
5239 psz_typedesc = "ISO/IEC 13818-6 type D";
5242 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
5245 if (p_dvbpsies->i_type >= 0x0F && p_dvbpsies->i_type <=0x7F)
5246 psz_typedesc = "ISO/IEC 13818-1 Reserved";
5248 psz_typedesc = "User Private";
5251 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
5252 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5254 for( p_dr = p_dvbpsies->p_first_descriptor; p_dr != NULL;
5255 p_dr = p_dr->p_next )
5257 msg_Dbg( p_demux, " - descriptor tag 0x%x",
5261 if ( !PIDSetup( p_demux, TYPE_PES, pespid, pmtpid ) )
5263 msg_Warn( p_demux, " * pid=%d type=0x%x %s (skipped)",
5264 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5271 p_pes = ts_pes_New( p_demux );
5277 p_pes = pespid->u.p_pes;
5281 ARRAY_APPEND( p_pmt->e_streams, pespid );
5283 PIDFillFormat( &p_pes->es.fmt, p_dvbpsies->i_type );
5285 pespid->i_flags |= SEEN(p_sys->pid[p_dvbpsies->i_pid]);
5287 bool b_registration_applied = false;
5288 if ( p_dvbpsies->i_type >= 0x80 ) /* non standard, extensions */
5290 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
5292 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, &p_pes->es, p_dvbpsies ) ))
5293 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
5294 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5298 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, &p_pes->es, p_dvbpsies ) ))
5299 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
5300 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5304 if ( !b_registration_applied )
5306 switch( p_dvbpsies->i_type )
5309 /* Handle PES private data */
5310 PMTSetupEs0x06( p_demux, p_pes, p_dvbpsies );
5312 /* All other private or reserved types */
5317 PMTSetupEsISO14496( p_demux, &p_pes->es, p_pmt, p_dvbpsies );
5321 PMTSetupEs0x83( p_dvbpsipmt, &p_pes->es, p_dvbpsies->i_pid );
5324 PMTSetupEs0xA0( p_demux, &p_pes->es, p_dvbpsies );
5327 PMTSetupEs0xD1( p_demux, &p_pes->es, p_dvbpsies );
5330 PMTSetupEs0xEA( p_demux, &p_pes->es, p_dvbpsies );
5336 if( p_pes->es.fmt.i_cat == AUDIO_ES ||
5337 ( p_pes->es.fmt.i_cat == SPU_ES &&
5338 p_pes->es.fmt.i_codec != VLC_CODEC_DVBS &&
5339 p_pes->es.fmt.i_codec != VLC_CODEC_TELETEXT ) )
5341 PMTParseEsIso639( p_demux, &p_pes->es, p_dvbpsies );
5344 switch( p_pes->es.fmt.i_codec )
5346 case VLC_CODEC_SCTE_27:
5347 p_pes->data_type = TS_ES_DATA_TABLE_SECTION;
5350 //pid->es->data_type = TS_ES_DATA_PES;
5354 p_pes->es.fmt.i_group = p_dvbpsipmt->i_program_number;
5355 for( int i = 0; i < p_pes->extra_es.i_size; i++ )
5356 p_pes->extra_es.p_elems[i]->fmt.i_group = p_dvbpsipmt->i_program_number;
5358 if( p_pes->es.fmt.i_cat == UNKNOWN_ES )
5360 msg_Dbg( p_demux, " => pid %d content is *unknown*",
5361 p_dvbpsies->i_pid );
5365 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
5366 p_dvbpsies->i_pid, (char*)&p_pes->es.fmt.i_codec );
5368 if( p_sys->b_es_id_pid ) p_pes->es.fmt.i_id = p_dvbpsies->i_pid;
5370 /* Check if we can avoid restarting the ES */
5373 /* p_pes points to a tmp pes */
5374 if( pespid->u.p_pes->es.fmt.i_codec != p_pes->es.fmt.i_codec ||
5375 pespid->u.p_pes->es.fmt.i_extra != p_pes->es.fmt.i_extra ||
5376 pespid->u.p_pes->es.fmt.i_extra != 0 ||
5377 pespid->u.p_pes->extra_es.i_size != p_pes->extra_es.i_size ||
5378 !( ( !pespid->u.p_pes->es.fmt.psz_language &&
5379 !p_pes->es.fmt.psz_language ) ||
5380 ( pespid->u.p_pes->es.fmt.psz_language &&
5381 p_pes->es.fmt.psz_language &&
5382 !strcmp( pespid->u.p_pes->es.fmt.psz_language,
5383 p_pes->es.fmt.psz_language ) ) ) )
5385 /* Differs, swap then */
5386 ts_pes_t *e = pespid->u.p_pes;
5387 pespid->u.p_pes = p_pes;
5390 /* p_pes still tmp, but now contains old config */
5391 pespid->u.p_pes->es.id = p_pes->es.id;
5392 if( pespid->u.p_pes->es.id )
5394 p_pes->es.id = NULL;
5395 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5396 pespid->u.p_pes->es.id, pespid->u.p_pes->es.fmt );
5399 for( int i=0; i<pespid->u.p_pes->extra_es.i_size &&
5400 i<p_pes->extra_es.i_size; i++ )
5402 pespid->u.p_pes->extra_es.p_elems[i]->id = p_pes->extra_es.p_elems[i]->id;
5403 if( pespid->u.p_pes->extra_es.p_elems[i]->id )
5405 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5406 pespid->u.p_pes->extra_es.p_elems[i]->id,
5407 pespid->u.p_pes->extra_es.p_elems[i]->fmt );
5408 p_pes->extra_es.p_elems[i]->id = NULL;
5413 ts_pes_Del( p_demux, p_pes ); // delete temp
5417 AddAndCreateES( p_demux, pespid );
5421 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x09 );
5422 if( p_dr && p_dr->i_length >= 2 )
5424 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5425 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5428 if( ProgramIsSelected( p_demux, p_pmt->i_number ) && pespid->u.p_pes->es.id != NULL )
5429 SetPIDFilter( p_demux, p_dvbpsies->i_pid, true ); /* Set demux filter */
5432 /* Set CAM descrambling */
5433 if( !ProgramIsSelected( p_demux, p_pmt->i_number ) )
5435 dvbpsi_pmt_delete( p_dvbpsipmt );
5437 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
5438 p_dvbpsipmt ) != VLC_SUCCESS )
5440 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED )
5442 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
5443 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
5444 if (!p_sys->arib.b25stream)
5445 dvbpsi_pmt_delete( p_dvbpsipmt );
5446 } else dvbpsi_pmt_delete( p_dvbpsipmt );
5449 /* Decref or clean now unused es */
5450 for( int i = 0; i < old_es_rm.i_size; i++ )
5451 PIDRelease( p_demux, old_es_rm.p_elems[i] );
5452 ARRAY_RESET( old_es_rm );
5454 if( !p_sys->b_trust_pcr )
5456 int i_cand = FindPCRCandidate( p_pmt );
5457 p_pmt->i_pid_pcr = i_cand;
5458 p_pmt->pcr.b_disable = true;
5459 msg_Warn( p_demux, "PCR not trusted for program %d, set up workaround using pid %d",
5460 p_pmt->i_number, i_cand );
5463 /* Probe Boundaries */
5464 if( p_sys->b_canfastseek && p_pmt->i_last_dts == -1 )
5466 p_pmt->i_last_dts = 0;
5467 ProbeStart( p_demux, p_pmt->i_number );
5468 ProbeEnd( p_demux, p_pmt->i_number );
5472 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
5474 /* Some Dreambox streams have all PMT set to same pid */
5475 int i_prev_pid = -1;
5476 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
5478 p_program = p_program->p_next )
5480 if( p_program->i_pid == i_prev_pid )
5482 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
5483 return VLC_EGENERIC;
5485 i_prev_pid = p_program->i_pid;
5490 static void PATCallBack( void *data, dvbpsi_pat_t *p_dvbpsipat )
5492 demux_t *p_demux = data;
5493 demux_sys_t *p_sys = p_demux->p_sys;
5494 dvbpsi_pat_program_t *p_program;
5495 ts_pid_t *patpid = &p_sys->pid[0];
5496 ts_pat_t *p_pat = p_sys->pid[0].u.p_pat;
5498 patpid->i_flags |= FLAG_SEEN;
5500 msg_Dbg( p_demux, "PATCallBack called" );
5502 if(unlikely( p_sys->pid[0].type != TYPE_PAT ))
5504 msg_Warn( p_demux, "PATCallBack called on invalid pid" );
5508 if( ( p_pat->i_version != -1 &&
5509 ( !p_dvbpsipat->b_current_next ||
5510 p_dvbpsipat->i_version == p_pat->i_version ) ) ||
5511 ( p_pat->i_ts_id != -1 && p_dvbpsipat->i_ts_id != p_pat->i_ts_id ) ||
5512 p_sys->b_user_pmt || PATCheck( p_demux, p_dvbpsipat ) )
5514 dvbpsi_pat_delete( p_dvbpsipat );
5518 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
5519 p_dvbpsipat->i_ts_id, p_dvbpsipat->i_version, p_dvbpsipat->b_current_next );
5521 /* Save old programs array */
5522 DECL_ARRAY(ts_pid_t *) old_pmt_rm;
5523 old_pmt_rm.i_alloc = p_pat->programs.i_alloc;
5524 old_pmt_rm.i_size = p_pat->programs.i_size;
5525 old_pmt_rm.p_elems = p_pat->programs.p_elems;
5526 ARRAY_INIT(p_pat->programs);
5528 /* now create programs */
5529 for( p_program = p_dvbpsipat->p_first_program; p_program != NULL;
5530 p_program = p_program->p_next )
5532 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
5534 if( p_program->i_number == 0 )
5537 ts_pid_t *pmtpid = &p_sys->pid[p_program->i_pid];
5539 ValidateDVBMeta( p_demux, p_program->i_pid );
5541 /* create or temporary incref pid */
5542 if( !PIDSetup( p_demux, TYPE_PMT, pmtpid, patpid ) )
5544 msg_Warn( p_demux, " * number=%d pid=%d (ignored)", p_program->i_number,
5549 pmtpid->u.p_pmt->i_number = p_program->i_number;
5551 if( !dvbpsi_pmt_attach( pmtpid->u.p_pmt->handle, p_program->i_number, PMTCallBack, p_demux ) )
5552 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5553 p_program->i_number );
5555 ARRAY_APPEND( p_pat->programs, pmtpid );
5557 /* Now select PID at access level */
5558 if( p_sys->programs.i_size == 0 ||
5559 ProgramIsSelected( p_demux, p_program->i_number ) )
5561 if( p_sys->programs.i_size == 0 )
5563 p_sys->b_default_selection = true;
5564 ARRAY_APPEND( p_sys->programs, p_program->i_number );
5567 if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
5568 p_sys->b_access_control = false;
5569 else if ( p_sys->es_creation == DELAY_ES )
5570 p_sys->es_creation = CREATE_ES;
5573 p_pat->i_version = p_dvbpsipat->i_version;
5574 p_pat->i_ts_id = p_dvbpsipat->i_ts_id;
5576 for(int i=0; i<old_pmt_rm.i_size; i++)
5578 /* decref current or release now unreferenced */
5579 PIDRelease( p_demux, old_pmt_rm.p_elems[i] );
5581 ARRAY_RESET(old_pmt_rm);
5583 dvbpsi_pat_delete( p_dvbpsipat );
5586 static inline bool handle_Init( demux_t *p_demux, dvbpsi_t **handle )
5588 *handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
5591 (*handle)->p_sys = (void *) p_demux;
5595 static ts_pat_t *ts_pat_New( demux_t *p_demux )
5597 ts_pat_t *pat = malloc( sizeof( ts_pat_t ) );
5601 if( !handle_Init( p_demux, &pat->handle ) )
5607 pat->i_version = -1;
5609 ARRAY_INIT( pat->programs );
5614 static void ts_pat_Del( demux_t *p_demux, ts_pat_t *pat )
5616 if( dvbpsi_decoder_present( pat->handle ) )
5617 dvbpsi_pat_detach( pat->handle );
5618 dvbpsi_delete( pat->handle );
5619 for( int i=0; i<pat->programs.i_size; i++ )
5620 PIDRelease( p_demux, pat->programs.p_elems[i] );
5621 ARRAY_RESET( pat->programs );
5625 static ts_pmt_t *ts_pmt_New( demux_t *p_demux )
5627 ts_pmt_t *pmt = malloc( sizeof( ts_pmt_t ) );
5631 if( !handle_Init( p_demux, &pmt->handle ) )
5637 ARRAY_INIT( pmt->e_streams );
5639 pmt->i_version = -1;
5641 pmt->i_pid_pcr = -1;
5644 pmt->i_last_dts = -1;
5646 pmt->pcr.i_current = -1;
5647 pmt->pcr.i_first = -1;
5648 pmt->pcr.b_disable = false;
5649 pmt->pcr.i_first_dts = VLC_TS_INVALID;
5650 pmt->pcr.i_pcroffset = -1;
5655 static void ts_pmt_Del( demux_t *p_demux, ts_pmt_t *pmt )
5657 if( dvbpsi_decoder_present( pmt->handle ) )
5658 dvbpsi_pmt_detach( pmt->handle );
5659 dvbpsi_delete( pmt->handle );
5660 for( int i=0; i<pmt->e_streams.i_size; i++ )
5661 PIDRelease( p_demux, pmt->e_streams.p_elems[i] );
5662 ARRAY_RESET( pmt->e_streams );
5664 IODFree( pmt->iod );
5665 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, pmt->i_number );
5669 static ts_pes_t *ts_pes_New( demux_t *p_demux )
5671 VLC_UNUSED(p_demux);
5672 ts_pes_t *pes = malloc( sizeof( ts_pes_t ) );
5677 pes->es.p_mpeg4desc = NULL;
5678 es_format_Init( &pes->es.fmt, UNKNOWN_ES, 0 );
5679 ARRAY_INIT( pes->extra_es );
5680 pes->data_type = TS_ES_DATA_PES;
5681 pes->i_data_size = 0;
5682 pes->i_data_gathered = 0;
5684 pes->pp_last = &pes->p_data;
5685 pes->p_prepcr_outqueue = NULL;
5690 static void ts_pes_Del( demux_t *p_demux, ts_pes_t *pes )
5694 es_out_Del( p_demux->out, pes->es.id );
5695 p_demux->p_sys->i_pmt_es--;
5699 block_ChainRelease( pes->p_data );
5701 if( pes->p_prepcr_outqueue )
5702 block_ChainRelease( pes->p_prepcr_outqueue );
5704 es_format_Clean( &pes->es.fmt );
5705 free( pes->es.p_mpeg4desc );
5707 for( int i = 0; i < pes->extra_es.i_size; i++ )
5709 if( pes->extra_es.p_elems[i]->id )
5711 es_out_Del( p_demux->out, pes->extra_es.p_elems[i]->id );
5712 p_demux->p_sys->i_pmt_es--;
5714 es_format_Clean( &pes->extra_es.p_elems[i]->fmt );
5715 free( pes->extra_es.p_elems[i] );
5717 ARRAY_RESET( pes->extra_es );
5722 static ts_psi_t *ts_psi_New( demux_t *p_demux )
5724 ts_psi_t *psi = malloc( sizeof( ts_psi_t ) );
5728 if( !handle_Init( p_demux, &psi->handle ) )
5734 psi->i_version = -1;
5739 static void ts_psi_Del( demux_t *p_demux, ts_psi_t *psi )
5741 VLC_UNUSED(p_demux);
5742 if( dvbpsi_decoder_present( psi->handle ) )
5743 dvbpsi_DetachDemux( psi->handle );
5744 dvbpsi_delete( psi->handle );