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"
73 #include "mpeg4_iod.h"
76 #include <aribb24/aribb24.h>
77 #include <aribb24/decoder.h>
80 typedef enum arib_modes_e
83 ARIBMODE_DISABLED = 0,
87 /*****************************************************************************
89 *****************************************************************************/
90 static int Open ( vlc_object_t * );
91 static void Close ( vlc_object_t * );
94 * - Rename "extra pmt" to "user pmt"
95 * - Update extra pmt description
96 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
97 * where pid_description could take 3 forms:
98 * 1. pid:pcr (to force the pcr pid)
100 * 3. pid:type=fourcc where type=(video|audio|spu)
102 #define PMT_TEXT N_("Extra PMT")
103 #define PMT_LONGTEXT N_( \
104 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
106 #define PID_TEXT N_("Set id of ES to PID")
107 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
108 " handled by VLC to the same value as the PID in" \
109 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
110 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
112 #define CSA_TEXT N_("CSA Key")
113 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
114 "16 char string (8 hexadecimal bytes).")
116 #define CSA2_TEXT N_("Second CSA Key")
117 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
118 "16 char string (8 hexadecimal bytes).")
121 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
122 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
123 "The decryption routines subtract the TS-header from the value before " \
126 #define SPLIT_ES_TEXT N_("Separate sub-streams")
127 #define SPLIT_ES_LONGTEXT N_( \
128 "Separate teletex/dvbs pages into independent ES. " \
129 "It can be useful to turn off this option when using stream output." )
131 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
132 #define SEEK_PERCENT_LONGTEXT N_( \
133 "Seek and position based on a percent byte position, not a PCR generated " \
134 "time position. If seeking doesn't work property, turn on this option." )
136 #define PCR_TEXT N_("Trust in-stream PCR")
137 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
139 static const int const arib_mode_list[] =
140 { ARIBMODE_AUTO, ARIBMODE_ENABLED, ARIBMODE_DISABLED };
141 static const char *const arib_mode_list_text[] =
142 { N_("Auto"), N_("Enabled"), N_("Disabled") };
144 #define SUPPORT_ARIB_TEXT N_("ARIB STD-B24 mode")
145 #define SUPPORT_ARIB_LONGTEXT N_( \
146 "Forces ARIB STD-B24 mode for decoding characters." \
147 "This feature affects EPG information and subtitles." )
150 set_description( N_("MPEG Transport Stream demuxer") )
151 set_shortname ( "MPEG-TS" )
152 set_category( CAT_INPUT )
153 set_subcategory( SUBCAT_INPUT_DEMUX )
155 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
156 add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
158 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
160 add_obsolete_string( "ts-out" ) /* since 2.2.0 */
161 add_obsolete_integer( "ts-out-mtu" ) /* since 2.2.0 */
162 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
164 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
166 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
169 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
170 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
172 add_integer( "ts-arib", ARIBMODE_AUTO, SUPPORT_ARIB_TEXT, SUPPORT_ARIB_LONGTEXT, false )
173 change_integer_list( arib_mode_list, arib_mode_list_text )
175 add_obsolete_bool( "ts-silent" );
177 set_capability( "demux", 10 )
178 set_callbacks( Open, Close )
182 /*****************************************************************************
184 *****************************************************************************/
185 static const char *const ppsz_teletext_type[] = {
188 N_("Teletext subtitles"),
189 N_("Teletext: additional information"),
190 N_("Teletext: program schedule"),
191 N_("Teletext subtitles: hearing impaired")
194 typedef struct ts_pid_t ts_pid_t;
201 DECL_ARRAY(ts_pid_t *) programs;
211 /* IOD stuff (mpeg4) */
212 iod_descriptor_t *iod;
214 DECL_ARRAY(ts_pid_t *) e_streams;
219 mtime_t i_first; // seen <> != -1
220 /* broken PCR handling */
223 bool b_disable; /* ignore PCR field, use dts */
235 const es_mpeg4_descriptor_t *p_mpeg4desc;
241 TS_ES_DATA_TABLE_SECTION
247 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
248 DECL_ARRAY( ts_pes_es_t * ) extra_es;
250 ts_es_data_type_t data_type;
256 block_t * p_prepcr_outqueue;
263 /* for special PAT/SDT case */
264 dvbpsi_t *handle; /* PAT/SDT/EIT */
271 TS_PMT_REGISTRATION_NONE = 0,
272 TS_PMT_REGISTRATION_HDMV
273 } ts_pmt_registration_type_t;
294 #define SEEN(x) ((x)->i_flags & FLAG_SEEN)
295 #define SCRAMBLED(x) ((x).i_flags & FLAG_SCRAMBLED)
302 uint8_t i_cc; /* countinuity counter */
305 /* PSI owner (ie PMT -> PAT, ES -> PMT */
320 vlc_fourcc_t i_fourcc;
332 #define MIN_ES_PID 4 /* Should be 32.. broken muxers */
333 #define MAX_ES_PID 8190
334 #define MIN_PAT_INTERVAL CLOCK_FREQ // DVB is 500ms
336 #define FROM_SCALE(x) (VLC_TS_0 + ((x) * 100 / 9))
337 #define TO_SCALE(x) (((x) - VLC_TS_0) * 9 / 100)
339 #define PID_ALLOC_CHUNK 16
346 vlc_mutex_t csa_lock;
348 /* TS packet size (188, 192, 204) */
349 unsigned i_packet_size;
351 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
352 unsigned i_packet_header_size;
354 /* how many TS packet we read at once */
357 bool b_force_seek_per_percent;
363 arib_instance_t *p_instance;
373 /* all non commons ones, dynamically allocated */
377 /* last recently used */
384 bool b_es_all; /* If we need to return all es/programs */
388 NO_ES, /* for preparse */
392 #define PREPARSING p_sys->es_creation == NO_ES
396 uint16_t i_next_extraid;
405 bool b_access_control;
412 int64_t i_dvb_length;
413 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
415 /* Selected programs */
416 DECL_ARRAY( int ) programs; /* List of selected/access-filtered programs */
417 bool b_default_selection; /* True if set by default to first pmt seen (to get data from filtered access) */
421 mtime_t i_first_dts; /* first dts encountered for the stream */
422 int i_timesourcepid; /* which pid we saved the dts from */
423 bool b_pat_deadline; /* set if we haven't seen PAT within MIN_PAT_INTERVAL */
432 static int Demux ( demux_t *p_demux );
433 static int Control( demux_t *p_demux, int i_query, va_list args );
435 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
437 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent );
438 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid );
440 static void PATCallBack( void*, dvbpsi_pat_t * );
441 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
442 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
443 uint16_t i_extension, demux_t * );
445 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
448 static ts_pat_t *ts_pat_New( demux_t * );
449 static void ts_pat_Del( demux_t *, ts_pat_t * );
450 static ts_pmt_t *ts_pmt_New( demux_t * );
451 static void ts_pmt_Del( demux_t *, ts_pmt_t * );
452 static ts_pes_t *ts_pes_New( demux_t * );
453 static void ts_pes_Del( demux_t *, ts_pes_t * );
454 static ts_psi_t *ts_psi_New( demux_t * );
455 static void ts_psi_Del( demux_t *, ts_psi_t * );
458 static ts_pid_t *PID( demux_sys_t *, uint16_t i_pid );
459 static ts_pmt_t * GetProgramByID( demux_sys_t *, int i_program );
460 static bool ProgramIsSelected( demux_sys_t *, uint16_t i_pgrm );
461 static void UpdatePESFilters( demux_t *p_demux, bool b_all );
462 static inline void FlushESBuffer( ts_pes_t *p_pes );
463 static void UpdateScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool );
464 static inline int PIDGet( block_t *p )
466 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
469 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
470 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid, bool );
471 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_prg, mtime_t i_pcr );
473 static block_t* ReadTSPacket( demux_t *p_demux );
474 static int ProbeStart( demux_t *p_demux, int i_program );
475 static int ProbeEnd( demux_t *p_demux, int i_program );
476 static int SeekToTime( demux_t *p_demux, ts_pmt_t *, int64_t time );
477 static void ReadyQueuesPostSeek( demux_t *p_demux );
478 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
479 static void PCRFixHandle( demux_t *, ts_pmt_t *, block_t * );
480 static int64_t TimeStampWrapAround( ts_pmt_t *, int64_t );
482 #define TS_USER_PMT_NUMBER (0)
483 static int UserPmt( demux_t *p_demux, const char * );
485 static int SetPIDFilter( demux_sys_t *, ts_pid_t *, bool b_selected );
487 #define TS_PACKET_SIZE_188 188
488 #define TS_PACKET_SIZE_192 192
489 #define TS_PACKET_SIZE_204 204
490 #define TS_PACKET_SIZE_MAX 204
491 #define TS_HEADER_SIZE 4
493 static int DetectPacketSize( demux_t *p_demux, unsigned *pi_header_size, int i_offset )
495 const uint8_t *p_peek;
497 if( stream_Peek( p_demux->s,
498 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
501 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
503 if( p_peek[i_offset + i_sync] != 0x47 )
506 /* Check next 3 sync bytes */
507 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
508 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
510 msg_Err( p_demux, "cannot peek" );
513 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
514 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
515 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
517 return TS_PACKET_SIZE_188;
519 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
520 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
521 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
525 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
527 return TS_PACKET_SIZE_192;
529 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
530 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
531 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
533 return TS_PACKET_SIZE_204;
537 if( p_demux->b_force )
539 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
540 return TS_PACKET_SIZE_188;
542 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
546 #define TOPFIELD_HEADER_SIZE 3712
548 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, unsigned *pi_header_size, vdr_info_t *p_vdr )
550 const uint8_t *p_peek;
552 int i_packet_size = -1;
554 if( stream_Peek( p_demux->s,
555 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
558 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
559 p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
560 stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
561 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
563 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
564 if( i_packet_size != -1 )
566 msg_Dbg( p_demux, "this is a topfield file" );
568 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
569 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
570 * but after the filename the offsets seem to be incorrect. - DJ */
571 int i_duration, i_name;
572 char *psz_name = xmalloc(25);
573 char *psz_event_name;
574 char *psz_event_text = xmalloc(130);
575 char *psz_ext_text = xmalloc(1025);
577 // 2 bytes version Uimsbf (4,5)
578 // 2 bytes reserved (6,7)
579 // 2 bytes duration in minutes Uimsbf (8,9(
580 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
581 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
582 // 2 bytes service number in channel list (10, 11)
583 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
584 // 4 bytes of reserved + tuner info (14,15,16,17)
585 // 2 bytes of Service ID Bslbf (18,19)
586 // 2 bytes of PMT PID Uimsbf (20,21)
587 // 2 bytes of PCR PID Uimsbf (22,23)
588 // 2 bytes of Video PID Uimsbf (24,25)
589 // 2 bytes of Audio PID Uimsbf (26,27)
590 // 24 bytes filename Bslbf
591 memcpy( psz_name, &p_peek[28], 24 );
593 msg_Dbg( p_demux, "recordingname=%s", psz_name );
594 // 1 byte of sat index Uimsbf (52)
595 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
596 // 4 bytes of freq. Uimsbf (56,57,58,59)
597 // 2 bytes of symbol rate Uimsbf (60,61)
598 // 2 bytes of TS stream ID Uimsbf (62,63)
601 // 2 bytes duration Uimsbf (70,71)
602 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
603 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
604 // 4 bytes EventID Uimsbf (72-75)
605 // 8 bytes of Start and End time info (76-83)
606 // 1 byte reserved (84)
607 // 1 byte event name length Uimsbf (89)
608 i_name = (int)(p_peek[89]&~0x81);
609 msg_Dbg( p_demux, "event name length = %d", i_name);
610 psz_event_name = xmalloc( i_name+1 );
611 // 1 byte parental rating (90)
612 // 129 bytes of event text
613 memcpy( psz_event_name, &p_peek[91], i_name );
614 psz_event_name[i_name] = '\0';
615 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
616 psz_event_text[129-i_name] = '\0';
617 msg_Dbg( p_demux, "event name=%s", psz_event_name );
618 msg_Dbg( p_demux, "event text=%s", psz_event_text );
619 // 12 bytes reserved (220)
621 // 2 bytes Event Text Length Uimsbf
622 // 4 bytes EventID Uimsbf
623 // FIXME We just have 613 bytes. not enough for this entire text
624 // 1024 bytes Extended Event Text Bslbf
625 memcpy( psz_ext_text, p_peek+372, 1024 );
626 psz_ext_text[1024] = '\0';
627 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
628 // 52 bytes reserved Bslbf
630 p_vdr->i_service = GetWBE(&p_peek[18]);
632 return i_packet_size;
633 //return TS_PACKET_SIZE_188;
637 return DetectPacketSize( p_demux, pi_header_size, 0 );
640 static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
642 return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
643 (mtime_t)(p_data[1] << 22)|
644 ((mtime_t)(p_data[2]&0xfe) << 14)|
645 (mtime_t)(p_data[3] << 7)|
646 (mtime_t)(p_data[4] >> 1);
649 static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, size_t i_data, bool b_adaptfield )
651 demux_sys_t *p_sys = p_demux->p_sys;
652 const uint8_t *p_pes = p_pesstart;
653 pid->probed.i_type = -1;
660 uint8_t len = *p_pes;
665 p_pes++; i_data--;/* stuffing */
671 if( len >= 7 && (p_pes[1] & 0x10) )
672 pid->probed.i_pcr_count++;
681 if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
684 size_t i_pesextoffset = 8;
686 if( p_pes[7] & 0x80 ) // PTS
689 if ( i_data < i_pesextoffset )
691 i_dts = ExtractPESTimestamp( &p_pes[9] );
693 if( p_pes[7] & 0x40 ) // DTS
696 if ( i_data < i_pesextoffset )
698 i_dts = ExtractPESTimestamp( &p_pes[14] );
700 if( p_pes[7] & 0x20 ) // ESCR
702 if( p_pes[7] & 0x10 ) // ESrate
704 if( p_pes[7] & 0x08 ) // DSM
706 if( p_pes[7] & 0x04 ) // CopyInfo
708 if( p_pes[7] & 0x02 ) // PESCRC
711 if ( i_data < i_pesextoffset )
714 /* HeaderdataLength */
715 const size_t i_payloadoffset = 8 + 1 + p_pes[8];
718 if ( i_data < i_pesextoffset || i_data < i_payloadoffset )
721 i_data -= 8 + 1 + p_pes[8];
723 if( p_pes[7] & 0x01 ) // PESExt
725 size_t i_extension2_offset = 1;
726 if ( p_pes[i_pesextoffset] & 0x80 ) // private data
727 i_extension2_offset += 16;
728 if ( p_pes[i_pesextoffset] & 0x40 ) // pack
729 i_extension2_offset += 1;
730 if ( p_pes[i_pesextoffset] & 0x20 ) // seq
731 i_extension2_offset += 2;
732 if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
733 i_extension2_offset += 2;
734 if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
736 uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
737 i_extension2_offset += i_len;
739 if( i_data < i_extension2_offset )
742 i_data -= i_extension2_offset;
744 /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
749 const uint8_t *p_data = &p_pes[i_payloadoffset];
751 if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
753 if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
755 pid->probed.i_type = 0x06;
756 pid->probed.i_fourcc = VLC_CODEC_DTS;
758 else if( !memcmp( p_data, "\x0B\x77", 2 ) )
760 pid->probed.i_type = 0x06;
761 pid->probed.i_fourcc = VLC_CODEC_EAC3;
763 else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
765 switch(p_data[1] & 18)
767 /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
768 11 - MPEG Version 1 (ISO/IEC 11172-3) */
770 pid->probed.i_type = 0x04;
773 pid->probed.i_type = 0x03;
778 switch(p_data[1] & 6)
784 pid->probed.i_type = 0x04;
785 pid->probed.i_fourcc = VLC_CODEC_MPGA;
788 pid->probed.i_type = 0x04;
789 pid->probed.i_fourcc = VLC_CODEC_MP2;
792 pid->probed.i_type = 0x04;
793 pid->probed.i_fourcc = VLC_CODEC_MP3;
800 else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
802 if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
804 pid->probed.i_type = 0x1b;
805 pid->probed.i_fourcc = VLC_CODEC_H264;
807 else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
809 pid->probed.i_type = 0x02;
810 pid->probed.i_fourcc = VLC_CODEC_MPGV;
814 /* Track timestamps and flag missing PAT */
815 if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
817 p_sys->patfix.i_first_dts = i_dts;
818 p_sys->patfix.i_timesourcepid = pid->i_pid;
820 else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
821 !p_sys->patfix.b_pat_deadline )
823 if( i_dts - p_sys->patfix.i_first_dts > TO_SCALE(MIN_PAT_INTERVAL) )
824 p_sys->patfix.b_pat_deadline = true;
829 static void BuildPATCallback( void *p_opaque, block_t *p_block )
831 ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
832 dvbpsi_packet_push( pat_pid->u.p_pat->handle, p_block->p_buffer );
835 static void BuildPMTCallback( void *p_opaque, block_t *p_block )
837 ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
838 assert(program_pid->type == TYPE_PMT);
841 dvbpsi_packet_push( program_pid->u.p_pmt->handle,
843 p_block = p_block->p_next;
847 static void MissingPATPMTFixup( demux_t *p_demux )
849 demux_sys_t *p_sys = p_demux->p_sys;
850 int i_program_number = 1234;
851 int i_program_pid = 1337;
852 int i_pcr_pid = 0x1FFF;
855 ts_pid_t *p_program_pid = PID( p_sys, i_program_pid );
856 if( SEEN(p_program_pid) )
858 /* Find a free one */
859 for( i_program_pid = MIN_ES_PID;
860 i_program_pid <= MAX_ES_PID && SEEN(p_program_pid);
863 p_program_pid = PID( p_sys, i_program_pid );
867 for( int i=0; i<p_sys->pids.i_all; i++ )
869 const ts_pid_t *p_pid = p_sys->pids.pp_all[i];
871 p_pid->probed.i_type == -1 )
874 if( i_pcr_pid == 0x1FFF && ( p_pid->probed.i_type == 0x03 ||
875 p_pid->probed.i_pcr_count ) )
876 i_pcr_pid = p_pid->i_pid;
884 ts_stream_t patstream =
887 .i_continuity_counter = 0x10,
888 .b_discontinuity = false
891 ts_stream_t pmtprogramstream =
893 .i_pid = i_program_pid,
894 .i_continuity_counter = 0x0,
895 .b_discontinuity = false
898 BuildPAT( PID(p_sys, 0)->u.p_pat->handle,
899 &p_sys->pids.pat, BuildPATCallback,
902 1, &pmtprogramstream, &i_program_number );
904 /* PAT callback should have been triggered */
905 if( p_program_pid->type != TYPE_PMT )
907 msg_Err( p_demux, "PAT creation failed" );
916 es_format_t esfmt = {0};
917 struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
918 pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
919 if( esstreams && mapped )
922 for( int i=0; i<p_sys->pids.i_all; i++ )
924 const ts_pid_t *p_pid = p_sys->pids.pp_all[i];
927 p_pid->probed.i_type == -1 )
930 esstreams[j].pes.i_stream_type = p_pid->probed.i_type;
931 esstreams[j].pes.i_stream_type = p_pid->probed.i_type;
932 esstreams[j].ts.i_pid = p_pid->i_pid;
933 mapped[j].pes = &esstreams[j].pes;
934 mapped[j].ts = &esstreams[j].ts;
935 mapped[j].fmt = &esfmt;
939 BuildPMT( PID(p_sys, 0)->u.p_pat->handle, VLC_OBJECT(p_demux),
940 p_program_pid, BuildPMTCallback,
944 1, &pmtprogramstream, &i_program_number,
951 /*****************************************************************************
953 *****************************************************************************/
954 static int Open( vlc_object_t *p_this )
956 demux_t *p_demux = (demux_t*)p_this;
960 unsigned i_packet_header_size = 0;
963 vdr_info_t vdr = {0};
965 /* Search first sync byte */
966 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
967 if( i_packet_size < 0 )
970 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
973 memset( p_sys, 0, sizeof( demux_sys_t ) );
974 vlc_mutex_init( &p_sys->csa_lock );
976 p_demux->pf_demux = Demux;
977 p_demux->pf_control = Control;
979 /* Init p_sys field */
980 p_sys->b_dvb_meta = true;
981 p_sys->b_access_control = true;
982 p_sys->b_end_preparse = false;
983 ARRAY_INIT( p_sys->programs );
984 p_sys->b_default_selection = false;
985 p_sys->i_tdt_delta = 0;
986 p_sys->i_dvb_start = 0;
987 p_sys->i_dvb_length = 0;
991 p_sys->arib.b25stream = NULL;
992 p_sys->stream = p_demux->s;
994 p_sys->b_broken_charset = false;
996 p_sys->pids.dummy.i_pid = 8191;
997 p_sys->pids.dummy.i_flags = FLAG_SEEN;
999 p_sys->i_packet_size = i_packet_size;
1000 p_sys->i_packet_header_size = i_packet_header_size;
1001 p_sys->i_ts_read = 50;
1003 p_sys->b_start_record = false;
1005 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
1007 if( !dvbpsi_AttachDemux( (table)->u.p_psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
1009 msg_Warn( obj, "Can't dvbpsi_AttachDemux on pid %d", (table)->i_pid );\
1013 /* Init PAT handler */
1014 patpid = PID(p_sys, 0);
1015 if ( !PIDSetup( p_demux, TYPE_PAT, patpid, NULL ) )
1017 vlc_mutex_destroy( &p_sys->csa_lock );
1021 if( !dvbpsi_pat_attach( patpid->u.p_pat->handle, PATCallBack, p_demux ) )
1023 PIDRelease( p_demux, patpid );
1024 vlc_mutex_destroy( &p_sys->csa_lock );
1026 return VLC_EGENERIC;
1029 if( p_sys->b_dvb_meta )
1031 if( !PIDSetup( p_demux, TYPE_SDT, PID(p_sys, 0x11), NULL ) ||
1032 !PIDSetup( p_demux, TYPE_EIT, PID(p_sys, 0x12), NULL ) ||
1033 !PIDSetup( p_demux, TYPE_TDT, PID(p_sys, 0x14), NULL ) )
1035 PIDRelease( p_demux, PID(p_sys, 0x11) );
1036 PIDRelease( p_demux, PID(p_sys, 0x12) );
1037 PIDRelease( p_demux, PID(p_sys, 0x14) );
1038 p_sys->b_dvb_meta = false;
1042 VLC_DVBPSI_DEMUX_TABLE_INIT(PID(p_sys, 0x11), p_demux);
1043 VLC_DVBPSI_DEMUX_TABLE_INIT(PID(p_sys, 0x12), p_demux);
1044 VLC_DVBPSI_DEMUX_TABLE_INIT(PID(p_sys, 0x14), p_demux);
1045 if( p_sys->b_access_control &&
1046 ( SetPIDFilter( p_sys, PID(p_sys, 0x11), true ) ||
1047 SetPIDFilter( p_sys, PID(p_sys, 0x14), true ) ||
1048 SetPIDFilter( p_sys, PID(p_sys, 0x12), true ) )
1050 p_sys->b_access_control = false;
1054 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
1056 p_sys->i_pmt_es = 0;
1057 p_sys->b_es_all = false;
1060 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
1061 p_sys->i_next_extraid = 1;
1063 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
1065 /* We handle description of an extra PMT */
1066 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
1067 p_sys->b_user_pmt = false;
1068 if( psz_string && *psz_string )
1069 UserPmt( p_demux, psz_string );
1072 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
1073 if( psz_string && *psz_string )
1078 p_sys->csa = csa_New();
1080 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
1081 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
1082 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
1084 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
1086 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1089 else if ( i_res == VLC_SUCCESS )
1091 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1095 csa_Delete( p_sys->csa );
1101 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
1102 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1104 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
1105 if( i_pkt < 4 || i_pkt > 188 )
1107 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
1108 msg_Warn( p_demux, "using default packet size of 188 bytes" );
1109 p_sys->i_csa_pkt_size = 188;
1112 p_sys->i_csa_pkt_size = i_pkt;
1113 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
1119 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
1121 p_sys->b_canseek = false;
1122 p_sys->b_canfastseek = false;
1123 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
1125 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
1127 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
1128 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
1131 if( p_sys->b_canseek )
1133 p_sys->es_creation = NO_ES;
1134 while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
1135 if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
1137 p_sys->es_creation = DELAY_ES;
1140 p_sys->es_creation = ( p_sys->b_access_control ? CREATE_ES : DELAY_ES );
1145 /*****************************************************************************
1147 *****************************************************************************/
1148 static void Close( vlc_object_t *p_this )
1150 demux_t *p_demux = (demux_t*)p_this;
1151 demux_sys_t *p_sys = p_demux->p_sys;
1153 PIDRelease( p_demux, PID(p_sys, 0) );
1155 if( p_sys->b_dvb_meta )
1157 PIDRelease( p_demux, PID(p_sys, 0x11) );
1158 PIDRelease( p_demux, PID(p_sys, 0x12) );
1159 PIDRelease( p_demux, PID(p_sys, 0x14) );
1162 vlc_mutex_lock( &p_sys->csa_lock );
1165 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
1166 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1167 csa_Delete( p_sys->csa );
1169 vlc_mutex_unlock( &p_sys->csa_lock );
1171 ARRAY_RESET( p_sys->programs );
1174 if ( p_sys->arib.p_instance )
1175 arib_instance_destroy( p_sys->arib.p_instance );
1178 if ( p_sys->arib.b25stream )
1180 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
1181 stream_Delete( p_sys->arib.b25stream );
1184 vlc_mutex_destroy( &p_sys->csa_lock );
1186 /* Release all non default pids */
1187 for( int i = 0; i < p_sys->pids.i_all; i++ )
1189 ts_pid_t *pid = p_sys->pids.pp_all[i];
1191 if( pid->type != TYPE_FREE )
1192 msg_Err( p_demux, "PID %d type %d not freed", pid->i_pid, pid->type );
1196 free( p_sys->pids.pp_all );
1201 /*****************************************************************************
1202 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
1203 *****************************************************************************/
1204 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
1205 vlc_value_t oldval, vlc_value_t newval,
1208 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1209 demux_t *p_demux = (demux_t*)p_this;
1210 demux_sys_t *p_sys = p_demux->p_sys;
1211 int i_tmp = (intptr_t)p_data;
1213 vlc_mutex_lock( &p_sys->csa_lock );
1215 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
1217 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
1219 vlc_mutex_unlock( &p_sys->csa_lock );
1223 /*****************************************************************************
1225 *****************************************************************************/
1226 static int Demux( demux_t *p_demux )
1228 demux_sys_t *p_sys = p_demux->p_sys;
1229 bool b_wait_es = p_sys->i_pmt_es <= 0;
1231 /* If we had no PAT within MIN_PAT_INTERVAL, create PAT/PMT from probed streams */
1232 if( p_sys->i_pmt_es == 0 && !SEEN(PID(p_sys, 0)) && p_sys->patfix.b_pat_deadline )
1233 MissingPATPMTFixup( p_demux );
1235 /* We read at most 100 TS packet or until a frame is completed */
1236 for( unsigned i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1238 bool b_frame = false;
1240 if( !(p_pkt = ReadTSPacket( p_demux )) )
1242 return VLC_DEMUXER_EOF;
1245 if( p_sys->b_start_record )
1247 /* Enable recording once synchronized */
1248 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
1249 p_sys->b_start_record = false;
1252 /* Parse the TS packet */
1253 ts_pid_t *p_pid = PID( p_sys, PIDGet( p_pkt ) );
1255 if( SCRAMBLED(*p_pid) != !!(p_pkt->p_buffer[3] & 0x80) )
1256 UpdateScrambledState( p_demux, p_pid, p_pkt->p_buffer[3] & 0x80 );
1260 if( p_pid->type == TYPE_FREE )
1261 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1262 p_pid->i_flags |= FLAG_SEEN;
1265 if ( SCRAMBLED(*p_pid) && !p_demux->p_sys->csa )
1267 block_Release( p_pkt );
1271 /* Probe streams to build PAT/PMT after MIN_PAT_INTERVAL in case we don't see any PAT */
1272 if( !SEEN( PID( p_sys, 0 ) ) &&
1273 (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
1274 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1275 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
1277 ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
1278 p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
1281 switch( p_pid->type )
1284 dvbpsi_packet_push( p_pid->u.p_pat->handle, p_pkt->p_buffer );
1285 block_Release( p_pkt );
1289 dvbpsi_packet_push( p_pid->u.p_pmt->handle, p_pkt->p_buffer );
1290 block_Release( p_pkt );
1294 p_sys->b_end_preparse = true;
1296 if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
1298 msg_Dbg( p_demux, "Creating delayed ES" );
1299 AddAndCreateES( p_demux, p_pid, true );
1302 if( !p_sys->b_access_control && !(p_pid->i_flags & FLAG_FILTERED) )
1304 /* That packet is for an unselected ES, don't waste time/memory gathering its data */
1305 block_Release( p_pkt );
1309 b_frame = GatherData( p_demux, p_pid, p_pkt );
1315 if( p_sys->b_dvb_meta )
1316 dvbpsi_packet_push( p_pid->u.p_psi->handle, p_pkt->p_buffer );
1317 block_Release( p_pkt );
1321 /* We have to handle PCR if present */
1322 PCRHandle( p_demux, p_pid, p_pkt );
1323 block_Release( p_pkt );
1327 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1331 demux_UpdateTitleFromStream( p_demux );
1332 return VLC_DEMUXER_SUCCESS;
1335 /*****************************************************************************
1337 *****************************************************************************/
1338 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1340 demux_sys_t *p_sys = p_demux->p_sys;
1346 if( p_sys->i_dvb_length > 0 )
1348 const int64_t t = mdate() + p_sys->i_tdt_delta;
1350 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1353 *pi_length = p_sys->i_dvb_length;
1355 *pi_time = t - p_sys->i_dvb_start;
1359 return VLC_EGENERIC;
1362 static void UpdatePESFilters( demux_t *p_demux, bool b_all )
1364 demux_sys_t *p_sys = p_demux->p_sys;
1365 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
1366 for( int i=0; i< p_pat->programs.i_size; i++ )
1368 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1369 bool b_program_selected;
1370 if( (p_sys->b_default_selection && !p_sys->b_access_control) || b_all )
1371 b_program_selected = true;
1373 b_program_selected = ProgramIsSelected( p_sys, p_pmt->i_number );
1375 SetPIDFilter( p_sys, p_pat->programs.p_elems[i], b_program_selected );
1377 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
1379 ts_pid_t *espid = p_pmt->e_streams.p_elems[j];
1380 bool b_stream_selected = b_program_selected;
1381 if( b_program_selected && !b_all && espid->u.p_pes->es.id )
1382 es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
1383 espid->u.p_pes->es.id, &b_stream_selected );
1385 if( !p_sys->b_es_all && espid->u.p_pes->es.fmt.i_cat == UNKNOWN_ES )
1386 b_stream_selected = false;
1388 if( b_stream_selected )
1389 msg_Dbg( p_demux, "enabling pid %d from program %d", espid->i_pid, p_pmt->i_number );
1391 SetPIDFilter( p_sys, espid, b_stream_selected );
1392 if( !b_stream_selected )
1393 FlushESBuffer( espid->u.p_pes );
1396 /* Select pcr last in case it is handled by unselected ES */
1397 if( p_pmt->i_pid_pcr > 0 )
1399 SetPIDFilter( p_sys, PID(p_sys, p_pmt->i_pid_pcr), b_program_selected );
1400 if( b_program_selected )
1401 msg_Dbg( p_demux, "enabling pcr pid %d from program %d", p_pmt->i_pid_pcr, p_pmt->i_number );
1406 static int Control( demux_t *p_demux, int i_query, va_list args )
1408 demux_sys_t *p_sys = p_demux->p_sys;
1410 bool b_bool, *pb_bool;
1415 int i_first_program = ( p_sys->programs.i_size ) ? p_sys->programs.p_elems[0] : 0;
1417 if( PREPARSING || !i_first_program || p_sys->b_default_selection )
1419 if( likely(PID(p_sys, 0)->type == TYPE_PAT) )
1421 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
1422 /* Set default program for preparse time (no program has been selected) */
1423 for( int i = 0; i < p_pat->programs.i_size; i++ )
1425 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
1426 p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
1427 if( ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) && p_pmt->i_last_dts > 0 )
1429 i_first_program = p_pmt->i_number;
1438 case DEMUX_GET_POSITION:
1439 pf = (double*) va_arg( args, double* );
1441 /* Access control test is because EPG for recordings is not relevant */
1442 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1444 int64_t i_time, i_length;
1445 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1447 *pf = (double)i_time/(double)i_length;
1452 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1453 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1454 p_pmt->pcr.i_current > -1 )
1456 double i_length = TimeStampWrapAround( p_pmt,
1457 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1458 i_length += p_pmt->pcr.i_pcroffset;
1459 double i_pos = TimeStampWrapAround( p_pmt,
1460 p_pmt->pcr.i_current ) - p_pmt->pcr.i_first;
1461 *pf = i_pos / i_length;
1465 if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1467 int64_t offset = stream_Tell( p_sys->stream );
1468 *pf = (double)offset / (double)i64;
1473 case DEMUX_SET_POSITION:
1474 f = (double) va_arg( args, double );
1476 if(!p_sys->b_canseek)
1479 if( p_sys->b_dvb_meta && p_sys->b_access_control &&
1480 !p_sys->b_force_seek_per_percent &&
1481 (p_pmt = GetProgramByID( p_sys, i_first_program )) )
1483 int64_t i_time, i_length;
1484 if( !DVBEventInformation( p_demux, &i_time, &i_length ) &&
1485 i_length > 0 && !SeekToTime( p_demux, p_pmt, TO_SCALE(i_length) * f ) )
1487 ReadyQueuesPostSeek( p_demux );
1488 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1489 TO_SCALE(i_length) * f );
1494 if( !p_sys->b_force_seek_per_percent &&
1495 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1496 p_pmt->pcr.i_first > -1 && p_pmt->i_last_dts > VLC_TS_INVALID &&
1497 p_pmt->pcr.i_current > -1 )
1499 double i_length = TimeStampWrapAround( p_pmt,
1500 p_pmt->i_last_dts ) - p_pmt->pcr.i_first;
1501 if( !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + i_length * f ) )
1503 ReadyQueuesPostSeek( p_demux );
1504 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1505 FROM_SCALE(p_pmt->pcr.i_first + i_length * f) );
1510 i64 = stream_Size( p_sys->stream );
1512 stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) == VLC_SUCCESS )
1514 ReadyQueuesPostSeek( p_demux );
1519 case DEMUX_SET_TIME:
1520 i64 = (int64_t)va_arg( args, int64_t );
1522 if( p_sys->b_canseek &&
1523 (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1524 p_pmt->pcr.i_first > -1 &&
1525 !SeekToTime( p_demux, p_pmt, p_pmt->pcr.i_first + TO_SCALE(i64) ) )
1527 ReadyQueuesPostSeek( p_demux );
1528 es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
1529 FROM_SCALE(p_pmt->pcr.i_first) + i64 - VLC_TS_0 );
1534 case DEMUX_GET_TIME:
1535 pi64 = (int64_t*)va_arg( args, int64_t * );
1537 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1539 if( !DVBEventInformation( p_demux, pi64, NULL ) )
1543 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1544 p_pmt->pcr.i_current > -1 && p_pmt->pcr.i_first > -1 )
1546 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
1547 *pi64 = FROM_SCALE(i_pcr - p_pmt->pcr.i_first);
1552 case DEMUX_GET_LENGTH:
1553 pi64 = (int64_t*)va_arg( args, int64_t * );
1555 if( p_sys->b_dvb_meta && p_sys->b_access_control )
1557 if( !DVBEventInformation( p_demux, NULL, pi64 ) )
1561 if( (p_pmt = GetProgramByID( p_sys, i_first_program )) &&
1562 ( p_pmt->pcr.i_first > -1 || p_pmt->pcr.i_first_dts > VLC_TS_INVALID ) &&
1563 p_pmt->i_last_dts > 0 )
1565 int64_t i_start = (p_pmt->pcr.i_first > -1) ? p_pmt->pcr.i_first :
1566 TO_SCALE(p_pmt->pcr.i_first_dts);
1567 int64_t i_last = TimeStampWrapAround( p_pmt, p_pmt->i_last_dts );
1568 i_last += p_pmt->pcr.i_pcroffset;
1569 *pi64 = FROM_SCALE(i_last - i_start);
1574 case DEMUX_SET_GROUP:
1578 i_int = (int)va_arg( args, int );
1579 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1580 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1582 if( i_int != 0 ) /* If not default program */
1584 /* Deselect/filter current ones */
1588 p_sys->b_es_all = false;
1589 ARRAY_APPEND( p_sys->programs, i_int );
1590 UpdatePESFilters( p_demux, false );
1592 else if( likely( p_list != NULL ) )
1594 p_sys->b_es_all = false;
1595 for( int i = 0; i < p_list->i_count; i++ )
1596 ARRAY_APPEND( p_sys->programs, p_list->p_values[i].i_int );
1597 UpdatePESFilters( p_demux, false );
1601 p_sys->b_es_all = true;
1602 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
1603 for( int i = 0; i < p_pat->programs.i_size; i++ )
1604 ARRAY_APPEND( p_sys->programs, p_pat->programs.p_elems[i]->i_pid );
1605 UpdatePESFilters( p_demux, true );
1608 p_sys->b_default_selection = false;
1616 i_int = (int)va_arg( args, int );
1617 msg_Dbg( p_demux, "DEMUX_SET_ES %d", i_int );
1619 if( !p_sys->b_es_all ) /* Won't change anything */
1620 UpdatePESFilters( p_demux, false );
1625 case DEMUX_GET_TITLE_INFO:
1627 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1628 int *c = va_arg( args, int * );
1630 *va_arg( args, int* ) = 0; /* Title offset */
1631 *va_arg( args, int* ) = 0; /* Chapter offset */
1632 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1635 case DEMUX_SET_TITLE:
1636 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1638 case DEMUX_SET_SEEKPOINT:
1639 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1641 case DEMUX_GET_META:
1642 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1644 case DEMUX_CAN_RECORD:
1645 pb_bool = (bool*)va_arg( args, bool * );
1649 case DEMUX_SET_RECORD_STATE:
1650 b_bool = (bool)va_arg( args, int );
1653 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1654 p_sys->b_start_record = b_bool;
1657 case DEMUX_GET_SIGNAL:
1658 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1664 return VLC_EGENERIC;
1667 /*****************************************************************************
1669 *****************************************************************************/
1670 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1672 demux_sys_t *p_sys = p_demux->p_sys;
1673 char *psz_dup = strdup( psz_fmt );
1674 char *psz = psz_dup;
1682 i_pid = strtol( psz, &psz, 0 );
1683 if( i_pid < 2 || i_pid >= 8192 )
1686 /* Parse optional program number */
1689 i_number = strtol( &psz[1], &psz, 0 );
1692 ts_pid_t *pmtpid = PID(p_sys, i_pid);
1694 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1695 if ( !PIDSetup( p_demux, TYPE_PMT, pmtpid, PID(p_sys, 0) ) )
1699 ts_pmt_t *p_pmt = pmtpid->u.p_pmt;
1700 p_pmt->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1701 if( !dvbpsi_pmt_attach( p_pmt->handle,
1702 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1703 PMTCallBack, p_demux ) )
1705 PIDRelease( p_demux, pmtpid );
1709 ARRAY_APPEND( PID(p_sys, 0)->u.p_pat->programs, pmtpid );
1711 psz = strchr( psz, '=' );
1714 while( psz && *psz )
1716 char *psz_next = strchr( psz, ',' );
1722 i_pid = strtol( psz, &psz, 0 );
1723 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1726 char *psz_opt = &psz[1];
1727 if( !strcmp( psz_opt, "pcr" ) )
1729 p_pmt->i_pid_pcr = i_pid;
1731 else if( PID(p_sys, i_pid)->type == TYPE_FREE )
1733 ts_pid_t *pid = PID(p_sys, i_pid);
1735 char *psz_arg = strchr( psz_opt, '=' );
1739 if ( !PIDSetup( p_demux, TYPE_PES, pid, pmtpid ) )
1742 ARRAY_APPEND( p_pmt->e_streams, pid );
1744 if( p_pmt->i_pid_pcr <= 0 )
1745 p_pmt->i_pid_pcr = i_pid;
1747 es_format_t *fmt = &pid->u.p_pes->es.fmt;
1749 if( psz_arg && strlen( psz_arg ) == 4 )
1751 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1752 psz_arg[2], psz_arg[3] );
1753 int i_cat = UNKNOWN_ES;
1755 if( !strcmp( psz_opt, "video" ) )
1757 else if( !strcmp( psz_opt, "audio" ) )
1759 else if( !strcmp( psz_opt, "spu" ) )
1762 es_format_Init( fmt, i_cat, i_codec );
1763 fmt->b_packetized = false;
1767 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1768 PIDFillFormat( fmt, i_stream_type );
1771 fmt->i_group = i_number;
1772 if( p_sys->b_es_id_pid )
1775 if( fmt->i_cat != UNKNOWN_ES )
1777 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1778 (char*)&fmt->i_codec );
1779 pid->u.p_pes->es.id = es_out_Add( p_demux->out, fmt );
1788 p_sys->b_user_pmt = true;
1794 return VLC_EGENERIC;
1797 static int SetPIDFilter( demux_sys_t *p_sys, ts_pid_t *p_pid, bool b_selected )
1800 p_pid->i_flags |= FLAG_FILTERED;
1802 p_pid->i_flags &= ~FLAG_FILTERED;
1804 if( !p_sys->b_access_control )
1805 return VLC_EGENERIC;
1807 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1808 p_pid->i_pid, b_selected );
1811 static void PIDReset( ts_pid_t *pid )
1813 assert(pid->i_refcount == 0);
1815 pid->i_flags &= ~FLAG_SCRAMBLED;
1816 pid->p_parent = NULL;
1817 pid->type = TYPE_FREE;
1820 static bool PIDSetup( demux_t *p_demux, ts_pid_type_t i_type, ts_pid_t *pid, ts_pid_t *p_parent )
1822 if( pid == p_parent || pid->i_pid == 0x1FFF )
1825 if( pid->i_refcount == 0 )
1827 assert( pid->type == TYPE_FREE );
1830 case TYPE_FREE: /* nonsense ?*/
1836 pid->u.p_pat = ts_pat_New( p_demux );
1843 pid->u.p_pmt = ts_pmt_New( p_demux );
1850 pid->u.p_pes = ts_pes_New( p_demux );
1859 pid->u.p_psi = ts_psi_New( p_demux );
1871 pid->p_parent = p_parent;
1873 else if( pid->type == i_type && pid->i_refcount < UINT8_MAX )
1879 if( pid->type != TYPE_FREE )
1880 msg_Warn( p_demux, "Tried to redeclare pid %d with another type", pid->i_pid );
1887 static void PIDRelease( demux_t *p_demux, ts_pid_t *pid )
1889 if( pid->i_refcount == 0 )
1891 assert( pid->type == TYPE_FREE );
1894 else if( pid->i_refcount == 1 )
1898 else if( pid->i_refcount > 1 )
1900 assert( pid->type != TYPE_FREE && pid->type != TYPE_PAT );
1904 if( pid->i_refcount == 0 )
1909 case TYPE_FREE: /* nonsense ?*/
1910 assert( pid->type != TYPE_FREE );
1914 ts_pat_Del( p_demux, pid->u.p_pat );
1915 pid->u.p_pat = NULL;
1919 ts_pmt_Del( p_demux, pid->u.p_pmt );
1920 pid->u.p_pmt = NULL;
1924 ts_pes_Del( p_demux, pid->u.p_pes );
1925 pid->u.p_pes = NULL;
1931 ts_psi_Del( p_demux, pid->u.p_psi );
1932 pid->u.p_psi = NULL;
1937 SetPIDFilter( p_demux->p_sys, pid, false );
1942 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1947 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1958 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1960 block_t *out = NULL;
1961 block_t **last = NULL;
1963 uint8_t *buf = block->p_buffer;
1964 size_t len = block->i_buffer;
1966 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
1967 int16_t start_trim = 0, end_trim = 0;
1968 int start_trim_flag = (buf[1] >> 4) & 1;
1969 int end_trim_flag = (buf[1] >> 3) & 1;
1970 int control_extension_flag = (buf[1] >> 2) & 1;
1975 unsigned au_size = 0;
1983 if (start_trim_flag) {
1984 start_trim = read_opus_flag(&buf, &len);
1985 if (start_trim < 0) {
1986 msg_Err(demux, "Invalid start trimming flag");
1989 if (end_trim_flag) {
1990 end_trim = read_opus_flag(&buf, &len);
1992 msg_Err(demux, "Invalid end trimming flag");
1995 if (control_extension_flag && len) {
1996 unsigned l = *buf++; len--;
1998 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
2005 if (!au_size || au_size > len) {
2006 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
2010 block_t *au = block_Alloc(au_size);
2013 memcpy(au->p_buffer, buf, au_size);
2014 block_CopyProperties(au, block);
2023 au->i_nb_samples = opus_frame_duration(buf, au_size);
2024 if (end_trim && (uint16_t) end_trim <= au->i_nb_samples)
2025 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
2029 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
2030 au->i_nb_samples -= start_trim;
2031 if (au->i_nb_samples == 0)
2032 au->i_flags |= BLOCK_FLAG_PREROLL;
2039 block_Release(block);
2043 /****************************************************************************
2045 ****************************************************************************/
2046 static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header, size_t i_header,
2047 unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
2052 return VLC_EGENERIC;
2054 switch( p_header[3] )
2056 case 0xBC: /* Program stream map */
2057 case 0xBE: /* Padding */
2058 case 0xBF: /* Private stream 2 */
2059 case 0xF0: /* ECM */
2060 case 0xF1: /* EMM */
2061 case 0xFF: /* Program stream directory */
2062 case 0xF2: /* DSMCC stream */
2063 case 0xF8: /* ITU-T H.222.1 type E stream */
2067 if( ( p_header[6]&0xC0 ) == 0x80 )
2070 i_skip = p_header[8] + 9;
2072 if( p_header[7]&0x80 ) /* has pts */
2074 if( i_header < 9 + 5 )
2075 return VLC_EGENERIC;
2076 *pi_pts = ExtractPESTimestamp( &p_header[9] );
2078 if( p_header[7]&0x40 ) /* has dts */
2080 if( i_header < 14 + 5 )
2081 return VLC_EGENERIC;
2082 *pi_dts = ExtractPESTimestamp( &p_header[14] );
2089 if( i_header < i_skip + 1 )
2090 return VLC_EGENERIC;
2091 while( i_skip < 23 && p_header[i_skip] == 0xff )
2094 if( i_header < i_skip + 1 )
2095 return VLC_EGENERIC;
2099 msg_Err( p_demux, "too much MPEG-1 stuffing" );
2100 return VLC_EGENERIC;
2102 if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
2107 if( i_header < i_skip + 1 )
2108 return VLC_EGENERIC;
2110 if( p_header[i_skip]&0x20 )
2112 if( i_header < i_skip + 5 )
2113 return VLC_EGENERIC;
2114 *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
2116 if( p_header[i_skip]&0x10 ) /* has dts */
2118 if( i_header < i_skip + 10 )
2119 return VLC_EGENERIC;
2120 *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
2140 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
2143 unsigned i_pes_size = 0;
2144 unsigned i_skip = 0;
2147 mtime_t i_length = 0;
2149 assert(pid->type == TYPE_PES);
2150 assert(pid->p_parent && pid->p_parent->type == TYPE_PMT);
2152 const int i_max = block_ChainExtract( p_pes, header, 34 );
2155 block_ChainRelease( p_pes );
2159 if( SCRAMBLED(*pid) || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2161 if ( !SCRAMBLED(*pid) )
2162 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
2163 header[0], header[1],header[2],header[3], pid->i_pid );
2164 block_ChainRelease( p_pes );
2168 if( ParsePESHeader( p_demux, (uint8_t*)&header, i_max, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
2170 block_ChainRelease( p_pes );
2176 i_pts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_pts );
2178 i_dts = TimeStampWrapAround( pid->p_parent->u.p_pmt, i_dts );
2181 if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
2182 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
2186 else if( pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
2187 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
2188 pid->u.p_pes->es.fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
2192 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT &&
2193 pid->u.p_pes->es.p_mpeg4desc )
2195 const decoder_config_descriptor_t *dcd = &pid->u.p_pes->es.p_mpeg4desc->dec_descr;
2197 if( dcd->i_extra > 2 &&
2198 dcd->p_extra[0] == 0x10 &&
2199 ( dcd->p_extra[1]&0x10 ) )
2201 /* display length */
2202 if( p_pes->i_buffer + 2 <= i_skip )
2203 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
2207 if( p_pes->i_buffer + 2 <= i_skip )
2208 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
2214 while( p_pes && i_skip > 0 )
2216 if( p_pes->i_buffer <= i_skip )
2218 block_t *p_next = p_pes->p_next;
2220 i_skip -= p_pes->i_buffer;
2221 block_Release( p_pes );
2226 p_pes->i_buffer -= i_skip;
2227 p_pes->p_buffer += i_skip;
2232 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
2233 if( i_pts >= 0 && i_dts < 0 )
2241 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
2244 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
2246 p_pes->i_length = i_length * 100 / 9;
2248 p_block = block_ChainGather( p_pes );
2249 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SUBT )
2251 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2253 p_block->i_buffer = i_pes_size;
2256 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2259 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2261 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_TELETEXT )
2263 if( p_block->i_pts <= VLC_TS_INVALID && pid->p_parent )
2265 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
2266 * In this case use the last PCR + 40ms */
2267 assert( pid->p_parent->type == TYPE_PMT );
2268 if( likely(pid->p_parent->type == TYPE_PMT) )
2270 mtime_t i_pcr = pid->p_parent->u.p_pmt->pcr.i_current;
2271 if( i_pcr > VLC_TS_INVALID )
2272 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
2276 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_A ||
2277 pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_ARIB_C )
2279 if( p_block->i_pts <= VLC_TS_INVALID )
2281 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2283 p_block->i_buffer = i_pes_size;
2286 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2289 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2292 else if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_OPUS)
2294 p_block = Opus_Parse(p_demux, p_block);
2297 if( !pid->p_parent || pid->p_parent->type != TYPE_PMT )
2299 block_Release( p_block );
2303 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2306 block_t *p_next = p_block->p_next;
2307 p_block->p_next = NULL;
2309 if( !p_pmt->pcr.b_fix_done ) /* Not seen yet */
2310 PCRFixHandle( p_demux, p_pmt, p_block );
2312 if( pid->u.p_pes->es.id && (p_pmt->pcr.i_current > -1 || p_pmt->pcr.b_disable) )
2314 if( pid->u.p_pes->p_prepcr_outqueue )
2316 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2317 p_block = pid->u.p_pes->p_prepcr_outqueue;
2318 p_next = p_block->p_next;
2319 p_block->p_next = NULL;
2320 pid->u.p_pes->p_prepcr_outqueue = NULL;
2323 if ( p_pmt->pcr.b_disable && p_block->i_dts > VLC_TS_INVALID &&
2324 ( p_pmt->i_pid_pcr == pid->i_pid || p_pmt->i_pid_pcr == 0x1FFF ) )
2326 ProgramSetPCR( p_demux, p_pmt, (p_block->i_dts - VLC_TS_0) * 9 / 100 - 120000 );
2329 /* Compute PCR/DTS offset if any */
2330 if( p_pmt->pcr.i_pcroffset == -1 && p_block->i_dts > VLC_TS_INVALID &&
2331 p_pmt->pcr.i_current > VLC_TS_INVALID )
2333 int64_t i_dts27 = (p_block->i_dts - VLC_TS_0) * 9 / 100;
2334 int64_t i_pcr = TimeStampWrapAround( p_pmt, p_pmt->pcr.i_current );
2335 if( i_dts27 < i_pcr )
2337 p_pmt->pcr.i_pcroffset = i_pcr - i_dts27 + 80000;
2338 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64
2339 "us later than pcr, applying delay",
2340 pid->i_pid, p_pmt->pcr.i_pcroffset * 100 / 9 );
2342 else p_pmt->pcr.i_pcroffset = 0;
2345 if( p_pmt->pcr.i_pcroffset != -1 )
2347 if( p_block->i_dts > VLC_TS_INVALID )
2348 p_block->i_dts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2349 if( p_block->i_pts > VLC_TS_INVALID )
2350 p_block->i_pts += (p_pmt->pcr.i_pcroffset * 100 / 9);
2353 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
2355 es_out_Send( p_demux->out, pid->u.p_pes->extra_es.p_elems[i]->id,
2356 block_Duplicate( p_block ) );
2359 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2363 if( !p_pmt->pcr.b_fix_done ) /* Not seen yet */
2364 PCRFixHandle( p_demux, p_pmt, p_block );
2366 block_ChainAppend( &pid->u.p_pes->p_prepcr_outqueue, p_block );
2374 msg_Warn( p_demux, "empty pes" );
2378 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
2380 block_t *p_content = block_ChainGather( p_data );
2382 if ( pid->p_parent && pid->p_parent->type == TYPE_PMT )
2384 ts_pmt_t *p_pmt = pid->p_parent->u.p_pmt;
2385 mtime_t i_date = p_pmt->pcr.i_current;
2387 if( pid->u.p_pes->es.fmt.i_codec == VLC_CODEC_SCTE_27 )
2389 /* We need to extract the truncated pts stored inside the payload */
2390 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
2393 size_t i_offset = 4;
2394 if( p_content->p_buffer[3] & 0x40 )
2396 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
2397 p_content->p_buffer[8];
2400 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
2402 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
2405 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
2406 if( i_display_in < i_date )
2407 i_date = i_display_in + (1ll << 32);
2409 i_date = i_display_in;
2416 p_content->i_dts = p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
2417 PCRFixHandle( p_demux, p_pmt, p_content );
2420 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_content );
2423 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
2425 block_t *p_data = pid->u.p_pes->p_data;
2430 /* remove the pes from pid */
2431 pid->u.p_pes->p_data = NULL;
2432 pid->u.p_pes->i_data_size = 0;
2433 pid->u.p_pes->i_data_gathered = 0;
2434 pid->u.p_pes->pp_last = &pid->u.p_pes->p_data;
2436 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
2438 ParsePES( p_demux, pid, p_data );
2440 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
2442 ParseTableSection( p_demux, pid, p_data );
2446 block_ChainRelease( p_data );
2450 static block_t* ReadTSPacket( demux_t *p_demux )
2452 demux_sys_t *p_sys = p_demux->p_sys;
2456 /* Get a new TS packet */
2457 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2459 if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
2460 msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
2462 msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
2466 if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
2468 block_Release( p_pkt );
2472 /* Skip header (BluRay streams).
2473 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2474 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2476 p_pkt->p_buffer += p_sys->i_packet_header_size;
2477 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2479 /* Check sync byte and re-sync if needed */
2480 if( p_pkt->p_buffer[0] != 0x47 )
2482 msg_Warn( p_demux, "lost synchro" );
2483 block_Release( p_pkt );
2486 const uint8_t *p_peek;
2488 unsigned i_skip = 0;
2490 i_peek = stream_Peek( p_sys->stream, &p_peek,
2491 p_sys->i_packet_size * 10 );
2492 if( i_peek < 0 || (unsigned)i_peek < p_sys->i_packet_size + 1 )
2494 msg_Dbg( p_demux, "eof ?" );
2498 while( i_skip < i_peek - p_sys->i_packet_size )
2500 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2501 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2507 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2508 stream_Read( p_sys->stream, NULL, i_skip );
2510 if( i_skip < i_peek - p_sys->i_packet_size )
2515 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2517 msg_Dbg( p_demux, "eof ?" );
2524 static int64_t TimeStampWrapAround( ts_pmt_t *p_pmt, int64_t i_time )
2526 int64_t i_adjust = 0;
2527 if( p_pmt && p_pmt->pcr.i_first > 0x0FFFFFFFF && i_time < 0x0FFFFFFFF )
2528 i_adjust = 0x1FFFFFFFF;
2530 return i_time + i_adjust;
2533 static mtime_t GetPCR( block_t *p_pkt )
2535 const uint8_t *p = p_pkt->p_buffer;
2539 if( ( p[3]&0x20 ) && /* adaptation */
2543 /* PCR is 33 bits */
2544 i_pcr = ( (mtime_t)p[6] << 25 ) |
2545 ( (mtime_t)p[7] << 17 ) |
2546 ( (mtime_t)p[8] << 9 ) |
2547 ( (mtime_t)p[9] << 1 ) |
2548 ( (mtime_t)p[10] >> 7 );
2553 static void UpdateScrambledState( demux_t *p_demux, ts_pid_t *p_pid, bool b_scrambled )
2555 if( !SCRAMBLED(*p_pid) == !b_scrambled )
2558 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2559 p_pid->i_pid, SCRAMBLED(*p_pid), b_scrambled );
2561 p_pid->i_flags |= (b_scrambled) ? FLAG_SCRAMBLED : FLAGS_NONE;
2563 if( p_pid->type == TYPE_PES && p_pid->u.p_pes->es.id )
2565 for( int i = 0; i < p_pid->u.p_pes->extra_es.i_size; i++ )
2567 if( p_pid->u.p_pes->extra_es.p_elems[i]->id )
2568 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2569 p_pid->u.p_pes->extra_es.p_elems[i]->id, b_scrambled );
2571 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2572 p_pid->u.p_pes->es.id, b_scrambled );
2576 static inline void FlushESBuffer( ts_pes_t *p_pes )
2580 p_pes->i_data_gathered = p_pes->i_data_size = 0;
2581 block_ChainRelease( p_pes->p_data );
2582 p_pes->p_data = NULL;
2583 p_pes->pp_last = &p_pes->p_data;
2587 static void ReadyQueuesPostSeek( demux_t *p_demux )
2589 demux_sys_t *p_sys = p_demux->p_sys;
2591 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
2592 for( int i=0; i< p_pat->programs.i_size; i++ )
2594 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2595 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2597 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
2599 if( pid->type != TYPE_PES )
2602 if( pid->u.p_pes->es.id )
2604 block_t *p_block = block_Alloc(1);
2607 p_block->i_buffer = 0;
2608 p_block->i_flags = BLOCK_FLAG_DISCONTINUITY | BLOCK_FLAG_CORRUPTED;
2609 es_out_Send( p_demux->out, pid->u.p_pes->es.id, p_block );
2615 if( pid->u.p_pes->p_prepcr_outqueue )
2617 block_ChainRelease( pid->u.p_pes->p_prepcr_outqueue );
2618 pid->u.p_pes->p_prepcr_outqueue = NULL;
2621 FlushESBuffer( pid->u.p_pes );
2623 p_pmt->pcr.i_current = -1;
2627 static int SeekToTime( demux_t *p_demux, ts_pmt_t *p_pmt, int64_t i_scaledtime )
2629 demux_sys_t *p_sys = p_demux->p_sys;
2631 /* Deal with common but worst binary search case */
2632 if( p_pmt->pcr.i_first == i_scaledtime && p_sys->b_canseek )
2633 return stream_Seek( p_sys->stream, 0 );
2635 if( !p_sys->b_canfastseek )
2636 return VLC_EGENERIC;
2638 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2640 /* Find the time position by using binary search algorithm. */
2641 int64_t i_head_pos = 0;
2642 int64_t i_tail_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2643 if( i_head_pos >= i_tail_pos )
2644 return VLC_EGENERIC;
2646 bool b_found = false;
2647 while( (i_head_pos + p_sys->i_packet_size) <= i_tail_pos && !b_found )
2649 /* Round i_pos to a multiple of p_sys->i_packet_size */
2650 int64_t i_splitpos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2651 int64_t i_div = i_splitpos % p_sys->i_packet_size;
2652 i_splitpos -= i_div;
2654 if ( stream_Seek( p_sys->stream, i_splitpos ) != VLC_SUCCESS )
2657 int64_t i_pos = i_splitpos;
2658 while( i_pos > -1 && i_pos < i_tail_pos )
2661 block_t *p_pkt = ReadTSPacket( p_demux );
2664 i_head_pos = i_tail_pos;
2668 i_pos = stream_Tell( p_sys->stream );
2670 int i_pid = PIDGet( p_pkt );
2671 if( i_pid != 0x1FFF && PID(p_sys, i_pid)->type == TYPE_PES &&
2672 PID(p_sys, i_pid)->p_parent->u.p_pmt == p_pmt &&
2673 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
2674 (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
2677 unsigned i_skip = 4;
2678 if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
2680 if( p_pkt->i_buffer >= 4 + 2 + 5 )
2682 i_pcr = GetPCR( p_pkt );
2683 i_skip += 1 + p_pkt->p_buffer[4];
2690 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2691 p_pkt->i_buffer - i_skip, &i_skip, &i_dts, &i_pts ) )
2698 block_Release( p_pkt );
2702 int64_t i_diff = i_scaledtime - TimeStampWrapAround( p_pmt, i_pcr );
2704 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2705 else if( i_diff < TO_SCALE(VLC_TS_0 + CLOCK_FREQ / 2) ) // 500ms
2713 if ( !b_found && i_pos > i_tail_pos - p_sys->i_packet_size )
2714 i_tail_pos = i_splitpos - p_sys->i_packet_size;
2719 msg_Dbg( p_demux, "Seek():cannot find a time position." );
2720 stream_Seek( p_sys->stream, i_initial_pos );
2721 return VLC_EGENERIC;
2726 static ts_pid_t *PID( demux_sys_t *p_sys, uint16_t i_pid )
2731 return &p_sys->pids.pat;
2733 return &p_sys->pids.dummy;
2735 if( p_sys->pids.i_last_pid == i_pid )
2736 return p_sys->pids.p_last;
2740 for( int i=0; i < p_sys->pids.i_all; i++ )
2742 if( p_sys->pids.pp_all[i]->i_pid == i_pid )
2744 p_sys->pids.p_last = p_sys->pids.pp_all[i];
2745 p_sys->pids.i_last_pid = i_pid;
2746 return p_sys->pids.p_last;
2750 if( p_sys->pids.i_all >= p_sys->pids.i_all_alloc )
2752 ts_pid_t **p_realloc = realloc( p_sys->pids.pp_all,
2753 (p_sys->pids.i_all_alloc + PID_ALLOC_CHUNK) * sizeof(ts_pid_t *) );
2756 p_sys->pids.pp_all = p_realloc;
2757 p_sys->pids.i_all_alloc += PID_ALLOC_CHUNK;
2760 ts_pid_t *p_pid = calloc( 1, sizeof(*p_pid) );
2767 p_pid->i_pid = i_pid;
2768 p_sys->pids.pp_all[p_sys->pids.i_all++] = p_pid;
2770 p_sys->pids.p_last = p_pid;
2771 p_sys->pids.i_last_pid = i_pid;
2776 static ts_pmt_t * GetProgramByID( demux_sys_t *p_sys, int i_program )
2778 if(unlikely(PID(p_sys, 0)->type != TYPE_PAT))
2781 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
2782 for( int i = 0; i < p_pat->programs.i_size; i++ )
2784 assert(p_pat->programs.p_elems[i]->type == TYPE_PMT);
2785 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2786 if( p_pmt->i_number == i_program )
2792 #define PROBE_CHUNK_COUNT 250
2794 static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_pcr, bool *pb_found )
2796 demux_sys_t *p_sys = p_demux->p_sys;
2798 block_t *p_pkt = NULL;
2804 if( i_count++ > PROBE_CHUNK_COUNT || !( p_pkt = ReadTSPacket( p_demux ) ) )
2809 const int i_pid = PIDGet( p_pkt );
2810 ts_pid_t *p_pid = PID(p_sys, i_pid);
2812 p_pid->i_flags |= FLAG_SEEN;
2814 if( i_pid != 0x1FFF && (p_pkt->p_buffer[1] & 0x80) == 0 ) /* not corrupt */
2816 bool b_pcrresult = true;
2817 bool b_adaptfield = p_pkt->p_buffer[3] & 0x20;
2819 if( b_adaptfield && p_pkt->i_buffer >= 4 + 2 + 5 )
2820 *pi_pcr = GetPCR( p_pkt );
2822 if( *pi_pcr == -1 &&
2823 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* payload start */
2824 (p_pkt->p_buffer[3] & 0xD0) == 0x10 && /* Has payload but is not encrypted */
2825 p_pid->type == TYPE_PES &&
2826 p_pid->u.p_pes->es.fmt.i_cat != UNKNOWN_ES
2829 b_pcrresult = false;
2832 unsigned i_skip = 4;
2833 if ( b_adaptfield ) // adaptation field
2834 i_skip += 1 + p_pkt->p_buffer[4];
2836 if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
2837 p_pkt->i_buffer - i_skip,
2838 &i_skip, &i_dts, &i_pts ) )
2842 else if( i_pts != -1 )
2849 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
2850 for( int i=0; i<p_pat->programs.i_size; i++ )
2852 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2853 if( ( p_pmt->i_pid_pcr == p_pid->i_pid ||
2854 ( p_pmt->i_pid_pcr == 0x1FFF && p_pid->p_parent == p_pat->programs.p_elems[i] ) ) )
2858 p_pmt->i_last_dts = *pi_pcr;
2860 /* Start, only keep first */
2861 else if( b_pcrresult && p_pmt->pcr.i_first == -1 )
2863 p_pmt->pcr.i_first = *pi_pcr;
2865 else if( p_pmt->pcr.i_first_dts < VLC_TS_0 )
2867 p_pmt->pcr.i_first_dts = FROM_SCALE(*pi_pcr);
2870 if( i_program == 0 || i_program == p_pmt->i_number )
2877 block_Release( p_pkt );
2883 static int ProbeStart( demux_t *p_demux, int i_program )
2885 demux_sys_t *p_sys = p_demux->p_sys;
2886 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2887 int64_t i_stream_size = stream_Size( p_sys->stream );
2889 int i_probe_count = 0;
2892 bool b_found = false;
2896 i_pos = p_sys->i_packet_size * i_probe_count;
2897 i_pos = __MIN( i_pos, i_stream_size );
2899 if( stream_Seek( p_sys->stream, i_pos ) )
2900 return VLC_EGENERIC;
2902 ProbeChunk( p_demux, i_program, false, &i_pcr, &b_found );
2904 /* Go ahead one more chunk if end of file contained only stuffing packets */
2905 i_probe_count += PROBE_CHUNK_COUNT;
2906 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (2 * PROBE_CHUNK_COUNT) );
2908 stream_Seek( p_sys->stream, i_initial_pos );
2910 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2913 static int ProbeEnd( demux_t *p_demux, int i_program )
2915 demux_sys_t *p_sys = p_demux->p_sys;
2916 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2917 int64_t i_stream_size = stream_Size( p_sys->stream );
2919 int i_probe_count = PROBE_CHUNK_COUNT;
2922 bool b_found = false;
2926 i_pos = i_stream_size - (p_sys->i_packet_size * i_probe_count);
2927 i_pos = __MAX( i_pos, 0 );
2929 if( stream_Seek( p_sys->stream, i_pos ) )
2930 return VLC_EGENERIC;
2932 ProbeChunk( p_demux, i_program, true, &i_pcr, &b_found );
2934 /* Go ahead one more chunk if end of file contained only stuffing packets */
2935 i_probe_count += PROBE_CHUNK_COUNT;
2936 } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (6 * PROBE_CHUNK_COUNT) );
2938 stream_Seek( p_sys->stream, i_initial_pos );
2940 return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
2943 static void ProgramSetPCR( demux_t *p_demux, ts_pmt_t *p_pmt, mtime_t i_pcr )
2945 demux_sys_t *p_sys = p_demux->p_sys;
2947 /* Check if we have enqueued blocks waiting the/before the
2948 PCR barrier, and then adapt pcr so they have valid PCR when dequeuing */
2949 if( p_pmt->pcr.i_current == -1 && p_pmt->pcr.b_fix_done )
2951 mtime_t i_mindts = -1;
2953 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
2954 for( int i=0; i< p_pat->programs.i_size; i++ )
2956 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
2957 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
2959 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[j];
2960 block_t *p_block = p_pid->u.p_pes->p_prepcr_outqueue;
2961 while( p_block && p_block->i_dts == VLC_TS_INVALID )
2962 p_block = p_block->p_next;
2964 if( p_block && ( i_mindts == -1 || p_block->i_dts < i_mindts ) )
2965 i_mindts = p_block->i_dts;
2969 if( i_mindts > VLC_TS_INVALID )
2971 msg_Dbg( p_demux, "Program %d PCR prequeue fixup %"PRId64"->%"PRId64,
2972 p_pmt->i_number, TO_SCALE(i_mindts), i_pcr );
2973 i_pcr = TO_SCALE(i_mindts);
2977 p_pmt->pcr.i_current = i_pcr;
2978 if( p_pmt->pcr.i_first == -1 )
2980 p_pmt->pcr.i_first = i_pcr; // now seen
2983 if ( p_sys->i_pmt_es )
2985 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2986 p_pmt->i_number, VLC_TS_0 + i_pcr * 100 / 9 );
2990 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2992 demux_sys_t *p_sys = p_demux->p_sys;
2994 if( p_sys->i_pmt_es <= 0 )
2997 mtime_t i_pcr = GetPCR( p_bk );
3001 pid->probed.i_pcr_count++;
3003 if(unlikely(PID(p_sys, 0)->type != TYPE_PAT))
3006 /* Search program and set the PCR */
3007 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
3008 for( int i = 0; i < p_pat->programs.i_size; i++ )
3010 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
3011 mtime_t i_program_pcr = TimeStampWrapAround( p_pmt, i_pcr );
3013 if( p_pmt->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
3015 if( pid->p_parent == p_pat->programs.p_elems[i] ) /* PCR shall be on pid itself */
3017 /* ? update PCR for the whole group program ? */
3018 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3021 else /* set PCR provided by current pid to program(s) referencing it */
3023 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
3024 if( p_pmt->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
3026 /* We've found a target group for update */
3027 ProgramSetPCR( p_demux, p_pmt, i_program_pcr );
3034 static int FindPCRCandidate( ts_pmt_t *p_pmt )
3036 ts_pid_t *p_cand = NULL;
3037 int i_previous = p_pmt->i_pid_pcr;
3039 for( int i=0; i<p_pmt->e_streams.i_size; i++ )
3041 ts_pid_t *p_pid = p_pmt->e_streams.p_elems[i];
3043 (!p_cand || p_cand->i_pid != i_previous) )
3045 if( p_pid->probed.i_pcr_count ) /* check PCR frequency first */
3047 if( !p_cand || p_pid->probed.i_pcr_count > p_cand->probed.i_pcr_count )
3054 if( p_pid->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3059 else if ( p_pid->u.p_pes->es.fmt.i_cat == VIDEO_ES ) /* Otherwise prioritize video dts */
3061 if( !p_cand || p_cand->u.p_pes->es.fmt.i_cat == AUDIO_ES )
3068 return p_cand->i_pid;
3073 /* Tries to reselect a new PCR when none has been received */
3074 static void PCRFixHandle( demux_t *p_demux, ts_pmt_t *p_pmt, block_t *p_block )
3076 if ( p_pmt->pcr.b_disable || p_pmt->pcr.b_fix_done )
3080 /* Record the first data packet timestamp in case there wont be any PCR */
3081 else if( !p_pmt->pcr.i_first_dts )
3083 p_pmt->pcr.i_first_dts = p_block->i_dts;
3085 else if( p_block->i_dts - p_pmt->pcr.i_first_dts > CLOCK_FREQ / 2 ) /* "PCR repeat rate shall not exceed 100ms" */
3087 if( p_pmt->pcr.i_current < 0 )
3089 int i_cand = FindPCRCandidate( p_pmt );
3090 p_pmt->i_pid_pcr = i_cand;
3091 msg_Warn( p_demux, "No PCR received for program %d, set up workaround using pid %d",
3092 p_pmt->i_number, i_cand );
3093 UpdatePESFilters( p_demux, p_demux->p_sys->b_es_all );
3094 p_pmt->pcr.b_fix_done = true;
3099 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
3101 const uint8_t *p = p_bk->p_buffer;
3102 const bool b_unit_start = p[1]&0x40;
3103 const bool b_adaptation = p[3]&0x20;
3104 const bool b_payload = p[3]&0x10;
3105 const int i_cc = p[3]&0x0f; /* continuity counter */
3106 bool b_discontinuity = false; /* discontinuity */
3108 /* transport_scrambling_control is ignored */
3113 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
3114 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
3118 /* For now, ignore additional error correction
3119 * TODO: handle Reed-Solomon 204,188 error correction */
3120 p_bk->i_buffer = TS_PACKET_SIZE_188;
3124 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
3126 if( pid->u.p_pes->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
3127 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3130 if( p_demux->p_sys->csa )
3132 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
3133 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
3134 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
3139 /* We don't have any adaptation_field, so payload starts
3140 * immediately after the 4 byte TS header */
3145 /* p[4] is adaptation_field_length minus one */
3149 /* discontinuity indicator found in stream */
3150 b_discontinuity = (p[5]&0x80) ? true : false;
3151 if( b_discontinuity && pid->u.p_pes->p_data )
3153 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
3155 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
3159 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
3164 /* Test continuity counter */
3165 /* continuous when (one of this):
3167 * diff == 0 and payload == 0
3168 * diff == 0 and duplicate packet (playload != 0) <- should we
3169 * test the content ?
3171 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
3172 if( b_payload && i_diff == 1 )
3174 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
3178 if( pid->i_cc == 0xff )
3180 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
3184 else if( i_diff != 0 && !b_discontinuity )
3186 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
3187 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
3190 if( pid->u.p_pes->p_data && pid->u.p_pes->es.fmt.i_cat != VIDEO_ES &&
3191 pid->u.p_pes->es.fmt.i_cat != AUDIO_ES )
3193 /* Small audio/video artifacts are usually better than
3194 * dropping full frames */
3195 pid->u.p_pes->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
3200 PCRHandle( p_demux, pid, p_bk );
3204 block_Release( p_bk );
3208 /* We have to gather it */
3209 p_bk->p_buffer += i_skip;
3210 p_bk->i_buffer -= i_skip;
3214 if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
3216 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
3217 block_t *p = block_Duplicate( p_bk );
3220 p->i_buffer = i_pointer_field;
3222 block_ChainLastAppend( &pid->u.p_pes->pp_last, p );
3224 p_bk->i_buffer -= 1 + i_pointer_field;
3225 p_bk->p_buffer += 1 + i_pointer_field;
3227 if( pid->u.p_pes->p_data )
3229 ParseData( p_demux, pid );
3233 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3234 if( pid->u.p_pes->data_type == TS_ES_DATA_PES )
3236 if( p_bk->i_buffer > 6 )
3238 pid->u.p_pes->i_data_size = GetWBE( &p_bk->p_buffer[4] );
3239 if( pid->u.p_pes->i_data_size > 0 )
3241 pid->u.p_pes->i_data_size += 6;
3245 else if( pid->u.p_pes->data_type == TS_ES_DATA_TABLE_SECTION )
3247 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
3249 pid->u.p_pes->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
3252 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3253 if( pid->u.p_pes->i_data_size > 0 &&
3254 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3256 ParseData( p_demux, pid );
3262 if( pid->u.p_pes->p_data == NULL )
3264 /* msg_Dbg( p_demux, "broken packet" ); */
3265 block_Release( p_bk );
3269 block_ChainLastAppend( &pid->u.p_pes->pp_last, p_bk );
3270 pid->u.p_pes->i_data_gathered += p_bk->i_buffer;
3272 if( pid->u.p_pes->i_data_size > 0 &&
3273 pid->u.p_pes->i_data_gathered >= pid->u.p_pes->i_data_size )
3275 ParseData( p_demux, pid );
3284 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
3286 switch( i_stream_type )
3288 case 0x01: /* MPEG-1 video */
3289 case 0x02: /* MPEG-2 video */
3290 case 0x80: /* MPEG-2 MOTO video */
3291 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
3293 case 0x03: /* MPEG-1 audio */
3294 case 0x04: /* MPEG-2 audio */
3295 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
3297 case 0x11: /* MPEG4 (audio) LATM */
3298 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
3299 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
3300 transport syntax, such as DST, ALS and SLS */
3301 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
3303 case 0x10: /* MPEG4 (video) */
3304 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
3306 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
3307 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
3309 case 0x24: /* HEVC */
3310 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
3312 case 0x42: /* CAVS (Chinese AVS) */
3313 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
3316 case 0x81: /* A52 (audio) */
3317 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
3319 case 0x82: /* SCTE-27 (sub) */
3320 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
3322 case 0x84: /* SDDS (audio) */
3323 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
3325 case 0x85: /* DTS (audio) */
3326 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
3328 case 0x87: /* E-AC3 */
3329 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
3332 case 0x91: /* A52 vls (audio) */
3333 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
3335 case 0x92: /* DVD_SPU vls (sub) */
3336 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
3339 case 0x94: /* SDDS (audio) */
3340 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
3343 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
3344 es_format_Init( fmt, UNKNOWN_ES, 0 );
3347 case 0x06: /* PES_PRIVATE (fixed later) */
3348 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
3349 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
3351 es_format_Init( fmt, UNKNOWN_ES, 0 );
3355 /* PES packets usually contain truncated frames */
3356 fmt->b_packetized = false;
3359 /****************************************************************************
3360 ****************************************************************************
3361 ** libdvbpsi callbacks
3362 ****************************************************************************
3363 ****************************************************************************/
3364 static bool ProgramIsSelected( demux_sys_t *p_sys, uint16_t i_pgrm )
3366 for(int i=0; i<p_sys->programs.i_size; i++)
3367 if( p_sys->programs.p_elems[i] == i_pgrm )
3373 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
3375 demux_sys_t *p_sys = p_demux->p_sys;
3377 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
3380 msg_Warn( p_demux, "Switching to non DVB mode" );
3382 /* This doesn't look like a DVB stream so don't try
3383 * parsing the SDT/EDT/TDT */
3385 PIDRelease( p_demux, PID(p_sys, 0x11) );
3386 PIDRelease( p_demux, PID(p_sys, 0x12) );
3387 PIDRelease( p_demux, PID(p_sys, 0x14) );
3388 p_sys->b_dvb_meta = false;
3391 #include "dvb-text.h"
3393 static char *EITConvertToUTF8( demux_t *p_demux,
3394 const unsigned char *psz_instring,
3398 demux_sys_t *p_sys = p_demux->p_sys;
3400 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3402 if ( !p_sys->arib.p_instance )
3403 p_sys->arib.p_instance = arib_instance_new( p_demux );
3404 if ( !p_sys->arib.p_instance )
3406 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3410 char *psz_outstring = NULL;
3413 i_out = i_length * 4;
3414 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3415 if( !psz_outstring )
3418 arib_initialize_decoder( p_decoder );
3419 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3420 psz_outstring, i_out );
3421 arib_finalize_decoder( p_decoder );
3423 return psz_outstring;
3428 /* Deal with no longer broken providers (no switch byte
3429 but sending ISO_8859-1 instead of ISO_6937) without
3430 removing them from the broken providers table
3431 (keep the entry for correctly handling recorded TS).
3433 b_broken = b_broken && i_length && *psz_instring > 0x20;
3436 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3437 return vlc_from_EIT( psz_instring, i_length );
3440 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3442 demux_sys_t *p_sys = p_demux->p_sys;
3443 ts_pid_t *sdt = PID(p_sys, 0x11);
3444 dvbpsi_sdt_service_t *p_srv;
3446 msg_Dbg( p_demux, "SDTCallBack called" );
3448 if( p_sys->es_creation != CREATE_ES ||
3449 !p_sdt->b_current_next ||
3450 p_sdt->i_version == sdt->u.p_psi->i_version )
3452 dvbpsi_sdt_delete( p_sdt );
3456 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3459 p_sdt->i_version, p_sdt->b_current_next,
3460 p_sdt->i_network_id );
3462 p_sys->b_broken_charset = false;
3464 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3467 dvbpsi_descriptor_t *p_dr;
3469 const char *psz_type = NULL;
3470 const char *psz_status = NULL;
3472 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3473 "running=%d free_ca=%d",
3474 p_srv->i_service_id, p_srv->b_eit_schedule,
3475 p_srv->b_eit_present, p_srv->i_running_status,
3478 if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
3480 msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
3481 p_sys->vdr.i_service );
3485 p_meta = vlc_meta_New();
3486 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3488 if( p_dr->i_tag == 0x48 )
3490 static const char *ppsz_type[17] = {
3492 "Digital television service",
3493 "Digital radio sound service",
3495 "NVOD reference service",
3496 "NVOD time-shifted service",
3499 "SECAM coded signal",
3502 "NTSC coded signal",
3503 "Data broadcast service",
3504 "Reserved for Common Interface Usage",
3505 "RCS Map (see EN 301 790 [35])",
3506 "RCS FLS (see EN 301 790 [35])",
3509 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3513 /* Workarounds for broadcasters with broken EPG */
3515 if( p_sdt->i_network_id == 133 )
3516 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3518 /* List of providers using ISO8859-1 */
3519 static const char ppsz_broken_providers[][8] = {
3520 "CSAT", /* CanalSat FR */
3521 "GR1", /* France televisions */
3523 "MR5", /* France 2/M6 HD */
3526 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3528 const size_t i_length = strlen(ppsz_broken_providers[i]);
3529 if( pD->i_service_provider_name_length == i_length &&
3530 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3531 p_sys->b_broken_charset = true;
3534 /* FIXME: Digital+ ES also uses ISO8859-1 */
3536 str1 = EITConvertToUTF8(p_demux,
3537 pD->i_service_provider_name,
3538 pD->i_service_provider_name_length,
3539 p_sys->b_broken_charset );
3540 str2 = EITConvertToUTF8(p_demux,
3542 pD->i_service_name_length,
3543 p_sys->b_broken_charset );
3545 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3546 pD->i_service_type, str1, str2 );
3548 vlc_meta_SetTitle( p_meta, str2 );
3549 vlc_meta_SetPublisher( p_meta, str1 );
3550 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3551 psz_type = ppsz_type[pD->i_service_type];
3557 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3559 static const char *ppsz_status[5] = {
3562 "Starts in a few seconds",
3566 psz_status = ppsz_status[p_srv->i_running_status];
3570 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3572 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3574 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3575 p_srv->i_service_id, p_meta );
3576 vlc_meta_Delete( p_meta );
3579 sdt->u.p_psi->i_version = p_sdt->i_version;
3580 dvbpsi_sdt_delete( p_sdt );
3583 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3584 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3586 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3590 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3591 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3594 /* Count the number of days */
3595 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3596 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3597 for( int i = 70; i < i_year; i++ )
3598 i_day += LEAP(1900+i);
3600 i_day += LEAP(1900+i_year);
3603 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3606 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3608 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3609 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3610 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3612 *p_y = 1900 + yp + c*1;
3613 *p_m = mp - 1 - c*12;
3614 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3616 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3617 static int64_t EITConvertStartTime( uint64_t i_date )
3619 const int i_mjd = i_date >> 24;
3620 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3621 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3622 const int i_second = CVT_FROM_BCD(i_date );
3627 /* if all 40 bits are 1, the start is unknown */
3628 if( i_date == UINT64_C(0xffffffffff) )
3631 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3632 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3634 static int EITConvertDuration( uint32_t i_duration )
3636 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3637 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3638 CVT_FROM_BCD(i_duration );
3642 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3644 demux_sys_t *p_sys = p_demux->p_sys;
3646 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3648 dvbpsi_tot_delete(p_tdt);
3652 static void EITCallBack( demux_t *p_demux,
3653 dvbpsi_eit_t *p_eit, bool b_current_following )
3655 demux_sys_t *p_sys = p_demux->p_sys;
3656 dvbpsi_eit_event_t *p_evt;
3659 msg_Dbg( p_demux, "EITCallBack called" );
3660 if( !p_eit->b_current_next )
3662 dvbpsi_eit_delete( p_eit );
3666 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3667 "ts_id=%d network_id=%d segment_last_section_number=%d "
3670 p_eit->i_version, p_eit->b_current_next,
3671 p_eit->i_ts_id, p_eit->i_network_id,
3672 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3674 p_epg = vlc_epg_New( NULL );
3675 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3677 dvbpsi_descriptor_t *p_dr;
3678 char *psz_name = NULL;
3679 char *psz_text = NULL;
3680 char *psz_extra = strdup("");
3684 int64_t i_tot_time = 0;
3686 i_start = EITConvertStartTime( p_evt->i_start_time );
3687 i_duration = EITConvertDuration( p_evt->i_duration );
3689 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3691 if( p_sys->i_tdt_delta == 0 )
3692 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3694 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3696 tzset(); // JST -> UTC
3697 i_start += timezone; // FIXME: what about DST?
3698 i_tot_time += timezone;
3700 if( p_evt->i_running_status == 0x00 &&
3701 (i_start - 5 < i_tot_time &&
3702 i_tot_time < i_start + i_duration + 5) )
3704 p_evt->i_running_status = 0x04;
3705 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3709 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3710 "running=%d free_ca=%d",
3711 p_evt->i_event_id, (int)i_start, (int)i_duration,
3712 p_evt->i_running_status, p_evt->b_free_ca );
3714 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3720 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3722 /* Only take first description, as we don't handle language-info
3724 if( pE && psz_name == NULL )
3726 psz_name = EITConvertToUTF8( p_demux,
3727 pE->i_event_name, pE->i_event_name_length,
3728 p_sys->b_broken_charset );
3730 psz_text = EITConvertToUTF8( p_demux,
3731 pE->i_text, pE->i_text_length,
3732 p_sys->b_broken_charset );
3733 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3734 pE->i_iso_639_code, psz_name, psz_text );
3741 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3744 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3746 pE->i_descriptor_number, pE->i_last_descriptor_number );
3748 if( pE->i_text_length > 0 )
3750 char *psz_text = EITConvertToUTF8( p_demux,
3751 pE->i_text, pE->i_text_length,
3752 p_sys->b_broken_charset );
3755 msg_Dbg( p_demux, " - text='%s'", psz_text );
3757 psz_extra = xrealloc( psz_extra,
3758 strlen(psz_extra) + strlen(psz_text) + 1 );
3759 strcat( psz_extra, psz_text );
3764 for( int i = 0; i < pE->i_entry_count; i++ )
3766 char *psz_dsc = EITConvertToUTF8( p_demux,
3767 pE->i_item_description[i],
3768 pE->i_item_description_length[i],
3769 p_sys->b_broken_charset );
3770 char *psz_itm = EITConvertToUTF8( p_demux,
3771 pE->i_item[i], pE->i_item_length[i],
3772 p_sys->b_broken_charset );
3774 if( psz_dsc && psz_itm )
3776 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3778 psz_extra = xrealloc( psz_extra,
3779 strlen(psz_extra) + strlen(psz_dsc) +
3780 strlen(psz_itm) + 3 + 1 );
3781 strcat( psz_extra, "(" );
3782 strcat( psz_extra, psz_dsc );
3783 strcat( psz_extra, " " );
3784 strcat( psz_extra, psz_itm );
3785 strcat( psz_extra, ")" );
3797 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
3800 for ( int i = 0; i < pR->i_ratings_number; i++ )
3802 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
3803 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
3805 if ( p_rating->i_rating + 3 > i_min_age )
3806 i_min_age = p_rating->i_rating + 3;
3807 msg_Dbg( p_demux, " - parental control set to %d years",
3816 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3822 if( i_start > 0 && psz_name && psz_text)
3823 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3824 *psz_extra ? psz_extra : NULL, i_min_age );
3826 /* Update "now playing" field */
3827 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
3828 vlc_epg_SetCurrent( p_epg, i_start );
3835 if( p_epg->i_event > 0 )
3837 if( b_current_following &&
3838 ( p_sys->programs.i_size == 0 ||
3839 p_sys->programs.p_elems[0] ==
3843 p_sys->i_dvb_length = 0;
3844 p_sys->i_dvb_start = 0;
3846 if( p_epg->p_current )
3848 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
3849 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
3852 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
3856 vlc_epg_Delete( p_epg );
3858 dvbpsi_eit_delete( p_eit );
3860 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3862 EITCallBack( p_demux, p_eit, true );
3864 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3866 EITCallBack( p_demux, p_eit, false );
3869 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
3870 uint16_t i_extension, demux_t *p_demux )
3872 demux_sys_t *p_sys = p_demux->p_sys;
3875 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3876 i_table_id, i_table_id, i_extension, i_extension );
3878 if( PID(p_sys, 0)->u.p_pat->i_version != -1 && i_table_id == 0x42 )
3880 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3881 i_table_id, i_table_id, i_extension, i_extension );
3883 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
3884 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
3886 else if( PID(p_sys, 0x11)->u.p_psi->i_version != -1 &&
3887 ( i_table_id == 0x4e || /* Current/Following */
3888 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3890 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3891 i_table_id, i_table_id, i_extension, i_extension );
3893 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3894 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3895 (dvbpsi_eit_callback)EITCallBackSchedule;
3897 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
3898 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
3900 else if( PID(p_sys, 0x11)->u.p_psi->i_version != -1 &&
3901 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
3903 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3904 i_table_id, i_table_id, i_extension, i_extension );
3906 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
3907 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
3911 /*****************************************************************************
3912 * PMT callback and helpers
3913 *****************************************************************************/
3914 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3917 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3918 while( p_dr && ( p_dr->i_tag != i_tag ) )
3919 p_dr = p_dr->p_next;
3922 static bool PMTEsHasRegistration( demux_t *p_demux,
3923 const dvbpsi_pmt_es_t *p_es,
3924 const char *psz_tag )
3926 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3930 if( p_dr->i_length < 4 )
3932 msg_Warn( p_demux, "invalid Registration Descriptor" );
3936 assert( strlen(psz_tag) == 4 );
3937 return !memcmp( p_dr->p_data, psz_tag, 4 );
3940 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
3941 int i_component_tag )
3943 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3946 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3950 return p_si->i_component_tag == i_component_tag;
3953 static const es_mpeg4_descriptor_t * GetMPEG4DescByEsId( const iod_descriptor_t *iod,
3957 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
3959 if( iod->es_descr[i].i_es_id == i_es_id )
3961 if ( iod->es_descr[i].b_ok )
3962 return &iod->es_descr[i];
3968 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pes_es_t *p_es,
3969 const ts_pmt_t *p_pmt, const dvbpsi_pmt_es_t *p_dvbpsies )
3971 es_format_t *p_fmt = &p_es->fmt;
3973 const dvbpsi_descriptor_t *p_dr = p_dvbpsies->p_first_descriptor;
3976 const es_mpeg4_descriptor_t *p_esdescr = NULL;
3978 uint8_t i_length = p_dr->i_length;
3980 switch( p_dr->i_tag )
3982 case 0x1f: /* FMC Descriptor */
3983 while( i_length >= 3 && !p_esdescr )
3985 i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3986 p_esdescr = GetMPEG4DescByEsId( p_pmt->iod, i_es_id );
3987 /* FIXME: add flexmux channel */
3990 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%"PRIu16, i_es_id );
3993 case 0x1e: /* SL Descriptor */
3996 i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3997 p_esdescr = GetMPEG4DescByEsId( p_pmt->iod, i_es_id );
3999 msg_Dbg( p_demux, "found SL_descriptor declaring sl packetization on es_id=%"PRIu16, i_es_id );
4008 if( !p_esdescr->b_ok )
4010 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%"PRIu16, i_es_id );
4014 p_es->p_mpeg4desc = p_esdescr;
4019 p_dr = p_dr->p_next;
4022 if( !p_es->p_mpeg4desc )
4024 switch( p_dvbpsies->i_type )
4026 /* non fatal, set by packetizer */
4027 case 0x0f: /* ADTS */
4028 case 0x11: /* LOAS */
4029 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4030 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4033 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4034 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4040 const decoder_config_descriptor_t *dcd = &p_es->p_mpeg4desc->dec_descr;
4041 if( dcd->i_streamType == 0x04 ) /* VisualStream */
4043 p_fmt->i_cat = VIDEO_ES;
4044 switch( dcd->i_objectTypeIndication )
4046 case 0x0B: /* mpeg4 sub */
4047 p_fmt->i_cat = SPU_ES;
4048 p_fmt->i_codec = VLC_CODEC_SUBT;
4051 case 0x20: /* mpeg4 */
4052 p_fmt->i_codec = VLC_CODEC_MP4V;
4054 case 0x21: /* h264 */
4055 p_fmt->i_codec = VLC_CODEC_H264;
4062 case 0x65: /* mpeg2 */
4063 p_fmt->i_codec = VLC_CODEC_MPGV;
4065 case 0x6a: /* mpeg1 */
4066 p_fmt->i_codec = VLC_CODEC_MPGV;
4068 case 0x6c: /* mpeg1 */
4069 p_fmt->i_codec = VLC_CODEC_JPEG;
4072 p_fmt->i_cat = UNKNOWN_ES;
4076 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
4078 p_fmt->i_cat = AUDIO_ES;
4079 switch( dcd->i_objectTypeIndication )
4081 case 0x40: /* mpeg4 */
4082 p_fmt->i_codec = VLC_CODEC_MP4A;
4086 case 0x68: /* mpeg2 aac */
4087 p_fmt->i_codec = VLC_CODEC_MP4A;
4089 case 0x69: /* mpeg2 */
4090 p_fmt->i_codec = VLC_CODEC_MPGA;
4092 case 0x6b: /* mpeg1 */
4093 p_fmt->i_codec = VLC_CODEC_MPGA;
4096 p_fmt->i_cat = UNKNOWN_ES;
4102 p_fmt->i_cat = UNKNOWN_ES;
4105 if( p_fmt->i_cat != UNKNOWN_ES )
4107 p_fmt->i_extra = __MIN(dcd->i_extra, INT32_MAX);
4108 if( p_fmt->i_extra > 0 )
4110 p_fmt->p_extra = malloc( p_fmt->i_extra );
4111 if( p_fmt->p_extra )
4112 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
4125 } ts_teletext_page_t;
4127 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pes_t *p_pes,
4128 const dvbpsi_pmt_es_t *p_dvbpsies )
4130 es_format_t *p_fmt = &p_pes->es.fmt;
4132 ts_teletext_page_t p_page[2 * 64 + 20];
4133 unsigned i_page = 0;
4135 /* Gather pages information */
4136 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
4138 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, i_tag_idx == 0 ? 0x46 : 0x56 );
4142 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
4146 for( int i = 0; i < p_sub->i_pages_number; i++ )
4148 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
4150 if( p_src->i_teletext_type >= 0x06 )
4153 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4155 ts_teletext_page_t *p_dst = &p_page[i_page++];
4157 p_dst->i_type = p_src->i_teletext_type;
4158 p_dst->i_magazine = p_src->i_teletext_magazine_number
4159 ? p_src->i_teletext_magazine_number : 8;
4160 p_dst->i_page = p_src->i_teletext_page_number;
4161 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4165 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4168 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4169 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4171 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
4173 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
4176 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4178 ts_teletext_page_t *p_dst = &p_page[i_page++];
4180 switch( p_src->i_subtitling_type )
4183 p_dst->i_type = 0x02;
4186 p_dst->i_type = 0x03;
4189 /* FIXME check if it is the right split */
4190 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
4191 ? (p_src->i_composition_page_id >> 8) : 8;
4192 p_dst->i_page = p_src->i_composition_page_id & 0xff;
4193 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4198 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
4200 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4202 p_fmt->subs.teletext.i_magazine = -1;
4203 p_fmt->subs.teletext.i_page = 0;
4204 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
4206 dvbpsi_descriptor_t *p_dr;
4207 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x46 );
4209 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x56 );
4211 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4213 /* Descriptor pass-through */
4214 p_fmt->p_extra = malloc( p_dr->i_length );
4215 if( p_fmt->p_extra )
4217 p_fmt->i_extra = p_dr->i_length;
4218 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4224 for( unsigned i = 0; i < i_page; i++ )
4226 ts_pes_es_t *p_page_es;
4231 p_page_es = &p_pes->es;
4235 p_page_es = calloc( 1, sizeof(*p_page_es) );
4239 es_format_Copy( &p_page_es->fmt, p_fmt );
4240 free( p_page_es->fmt.psz_language );
4241 free( p_page_es->fmt.psz_description );
4242 p_page_es->fmt.psz_language = NULL;
4243 p_page_es->fmt.psz_description = NULL;
4245 ARRAY_APPEND( p_pes->extra_es, p_page_es );
4249 const ts_teletext_page_t *p = &p_page[i];
4250 p_page_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
4251 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
4252 p_page_es->fmt.psz_language = strndup( p->p_iso639, 3 );
4253 p_page_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
4254 p_page_es->fmt.subs.teletext.i_magazine = p->i_magazine;
4255 p_page_es->fmt.subs.teletext.i_page = p->i_page;
4258 " * ttxt type=%s lan=%s page=%d%02x",
4259 p_page_es->fmt.psz_description,
4260 p_page_es->fmt.psz_language,
4261 p->i_magazine, p->i_page );
4265 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pes_t *p_pes,
4266 const dvbpsi_pmt_es_t *p_dvbpsies )
4268 es_format_t *p_fmt = &p_pes->es.fmt;
4270 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
4272 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4274 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4275 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4277 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
4278 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
4279 ( i_type >= 0x20 && i_type <= 0x24 ) )
4283 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4285 p_fmt->subs.dvb.i_id = -1;
4286 p_fmt->psz_description = strdup( _("DVB subtitles") );
4288 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4290 /* Descriptor pass-through */
4291 p_fmt->p_extra = malloc( p_dr->i_length );
4292 if( p_fmt->p_extra )
4294 p_fmt->i_extra = p_dr->i_length;
4295 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4301 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4303 ts_pes_es_t *p_subs_es;
4308 p_subs_es = &p_pes->es;
4312 p_subs_es = malloc( sizeof(*p_subs_es) );
4316 es_format_Copy( &p_subs_es->fmt, p_fmt );
4317 free( p_subs_es->fmt.psz_language );
4318 free( p_subs_es->fmt.psz_description );
4319 p_subs_es->fmt.psz_language = NULL;
4320 p_subs_es->fmt.psz_description = NULL;
4322 ARRAY_APPEND( p_pes->extra_es, p_subs_es );
4326 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
4327 p_subs_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
4328 switch( p->i_subtitling_type )
4330 case 0x10: /* unspec. */
4331 case 0x11: /* 4:3 */
4332 case 0x12: /* 16:9 */
4333 case 0x13: /* 2.21:1 */
4334 case 0x14: /* HD monitor */
4335 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles") );
4337 case 0x20: /* Hearing impaired unspec. */
4338 case 0x21: /* h.i. 4:3 */
4339 case 0x22: /* h.i. 16:9 */
4340 case 0x23: /* h.i. 2.21:1 */
4341 case 0x24: /* h.i. HD monitor */
4342 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
4349 p_subs_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
4350 ( p->i_ancillary_page_id << 16 );
4355 static int vlc_ceil_log2( const unsigned int val )
4357 int n = 31 - clz(val);
4358 if ((1U << n) != val)
4364 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
4368 /* default mapping */
4369 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
4370 memcpy(h.stream_map, map, sizeof(map));
4374 int stream_count = 0;
4375 int ccc = p[1]; // channel_config_code
4379 mapping = channels > 2;
4382 channels = 2; // dual mono
4384 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
4385 csc = p_csc[channels - 1];
4386 stream_count = channels - csc;
4388 static const uint8_t map[6][7] = {
4397 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4398 } else if (ccc == 0x81) {
4400 goto explicit_config_too_short;
4407 bs_init(&s, &p[4], len - 4);
4410 int bits = vlc_ceil_log2(channels);
4411 if (s.i_left < bits)
4412 goto explicit_config_too_short;
4413 stream_count = bs_read(&s, bits) + 1;
4414 bits = vlc_ceil_log2(stream_count + 1);
4415 if (s.i_left < bits)
4416 goto explicit_config_too_short;
4417 csc = bs_read(&s, bits);
4419 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4420 if (s.i_left < channels * channel_bits)
4421 goto explicit_config_too_short;
4423 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4424 for (int i = 0; i < channels; i++) {
4425 unsigned char m = bs_read(&s, channel_bits);
4428 h.stream_map[i] = m;
4431 } else if (ccc >= 0x80 && ccc <= 0x88) {
4432 channels = ccc - 0x80;
4437 channels = 2; // dual mono
4440 stream_count = channels;
4442 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4446 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4450 opus_prepare_header(channels, 0, &h);
4452 h.input_sample_rate = 48000;
4454 h.nb_streams = channels - csc;
4455 h.channel_mapping = mapping;
4458 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4459 if (p_fmt->p_extra) {
4460 p_fmt->i_cat = AUDIO_ES;
4461 p_fmt->i_codec = VLC_CODEC_OPUS;
4462 p_fmt->audio.i_channels = h.channels;
4463 p_fmt->audio.i_rate = 48000;
4469 explicit_config_too_short:
4470 msg_Err(demux, "Opus descriptor too short");
4473 static void PMTSetupEs0x06( demux_t *p_demux, ts_pes_t *p_pes,
4474 const dvbpsi_pmt_es_t *p_dvbpsies )
4476 es_format_t *p_fmt = &p_pes->es.fmt;
4477 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
4478 dvbpsi_descriptor_t *desc;
4480 if( PMTEsHasRegistration( p_demux, p_dvbpsies, "AC-3" ) ||
4481 PMTEsFindDescriptor( p_dvbpsies, 0x6a ) ||
4482 PMTEsFindDescriptor( p_dvbpsies, 0x81 ) )
4484 p_fmt->i_cat = AUDIO_ES;
4485 p_fmt->i_codec = VLC_CODEC_A52;
4487 else if( (desc = PMTEsFindDescriptor( p_dvbpsies, 0x7f ) ) && desc->i_length >= 2 &&
4488 PMTEsHasRegistration(p_demux, p_dvbpsies, "Opus"))
4490 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4492 else if( PMTEsFindDescriptor( p_dvbpsies, 0x7a ) )
4494 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4495 p_fmt->i_cat = AUDIO_ES;
4496 p_fmt->i_codec = VLC_CODEC_EAC3;
4498 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS1" ) ||
4499 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS2" ) ||
4500 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS3" ) ||
4501 PMTEsFindDescriptor( p_dvbpsies, 0x73 ) )
4503 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4504 p_fmt->i_cat = AUDIO_ES;
4505 p_fmt->i_codec = VLC_CODEC_DTS;
4507 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "BSSD" ) && !p_subs_dr )
4509 /* BSSD is AES3 DATA, but could also be subtitles
4510 * we need to check for secondary descriptor then s*/
4511 p_fmt->i_cat = AUDIO_ES;
4512 p_fmt->b_packetized = true;
4513 p_fmt->i_codec = VLC_CODEC_302M;
4515 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "HEVC" ) )
4517 p_fmt->i_cat = VIDEO_ES;
4518 p_fmt->i_codec = VLC_CODEC_HEVC;
4520 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4522 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4523 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xFD );
4524 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4525 if ( p_dr && p_dr->i_length >= 2 )
4527 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4528 PMTEsHasComponentTag( p_dvbpsies, 0x30 ) ||
4529 PMTEsHasComponentTag( p_dvbpsies, 0x31 ) ||
4530 PMTEsHasComponentTag( p_dvbpsies, 0x32 ) ||
4531 PMTEsHasComponentTag( p_dvbpsies, 0x33 ) ||
4532 PMTEsHasComponentTag( p_dvbpsies, 0x34 ) ||
4533 PMTEsHasComponentTag( p_dvbpsies, 0x35 ) ||
4534 PMTEsHasComponentTag( p_dvbpsies, 0x36 ) ||
4535 PMTEsHasComponentTag( p_dvbpsies, 0x37 ) ) )
4537 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_A );
4538 p_fmt->psz_language = strndup ( "jpn", 3 );
4539 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4541 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4542 PMTEsHasComponentTag( p_dvbpsies, 0x87 ) ||
4543 PMTEsHasComponentTag( p_dvbpsies, 0x88 ) ) )
4545 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_ARIB_C );
4546 p_fmt->psz_language = strndup ( "jpn", 3 );
4547 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4553 /* Subtitle/Teletext/VBI fallbacks */
4554 dvbpsi_subtitling_dr_t *p_sub;
4555 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4557 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4559 if( p_fmt->i_cat != UNKNOWN_ES )
4562 switch( p_sub->p_subtitle[i].i_subtitling_type )
4564 case 0x01: /* EBU Teletext subtitles */
4565 case 0x02: /* Associated EBU Teletext */
4566 case 0x03: /* VBI data */
4567 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4569 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4570 case 0x11: /* ... on 4:3 AR monitor */
4571 case 0x12: /* ... on 16:9 AR monitor */
4572 case 0x13: /* ... on 2.21:1 AR monitor */
4573 case 0x14: /* ... for display on a high definition monitor */
4574 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4575 case 0x21: /* ... on 4:3 AR monitor */
4576 case 0x22: /* ... on 16:9 AR monitor */
4577 case 0x23: /* ... on 2.21:1 AR monitor */
4578 case 0x24: /* ... for display on a high definition monitor */
4579 PMTSetupEsDvbSubtitle( p_demux, p_pes, p_dvbpsies );
4582 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4583 p_sub->p_subtitle[i].i_subtitling_type );
4589 if( p_fmt->i_cat == UNKNOWN_ES &&
4590 ( PMTEsFindDescriptor( p_dvbpsies, 0x45 ) || /* VBI Data descriptor */
4591 PMTEsFindDescriptor( p_dvbpsies, 0x46 ) || /* VBI Teletext descriptor */
4592 PMTEsFindDescriptor( p_dvbpsies, 0x56 ) ) ) /* EBU Teletext descriptor */
4595 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
4599 /* FIXME is it useful ? */
4600 if( PMTEsFindDescriptor( p_dvbpsies, 0x52 ) )
4602 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x52 );
4603 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4605 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4609 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pes_es_t *p_es,
4610 const dvbpsi_pmt_es_t *p_dvbpsies )
4612 /* Registration Descriptor */
4613 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "VC-1" ) )
4615 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4619 es_format_t *p_fmt = &p_es->fmt;
4621 /* registration descriptor for VC-1 (SMPTE rp227) */
4622 p_fmt->i_cat = VIDEO_ES;
4623 p_fmt->i_codec = VLC_CODEC_VC1;
4625 /* XXX With Simple and Main profile the SEQUENCE
4626 * header is modified: video width and height are
4627 * inserted just after the start code as 2 int16_t
4628 * The packetizer will take care of that. */
4631 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pes_es_t *p_es,
4632 const dvbpsi_pmt_es_t *p_dvbpsies )
4634 /* Registration Descriptor */
4635 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "drac" ) )
4637 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4641 es_format_t *p_fmt = &p_es->fmt;
4643 /* registration descriptor for Dirac
4644 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4645 p_fmt->i_cat = VIDEO_ES;
4646 p_fmt->i_codec = VLC_CODEC_DIRAC;
4649 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pes_es_t *p_es,
4650 const dvbpsi_pmt_es_t *p_dvbpsies )
4652 /* MSCODEC sent by vlc */
4653 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xa0 );
4654 if( !p_dr || p_dr->i_length < 10 )
4657 "private MSCODEC (vlc) without bih private descriptor" );
4661 es_format_t *p_fmt = &p_es->fmt;
4662 p_fmt->i_cat = VIDEO_ES;
4663 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4664 p_dr->p_data[2], p_dr->p_data[3] );
4665 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4666 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4667 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4669 if( p_fmt->i_extra > 0 )
4671 p_fmt->p_extra = malloc( p_fmt->i_extra );
4672 if( p_fmt->p_extra )
4673 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4674 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4678 /* For such stream we will gather them ourself and don't launch a
4680 * Yes it's ugly but it's the only way to have DIV3 working */
4681 p_fmt->b_packetized = true;
4684 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pes_es_t *p_es, int i_pid )
4686 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4687 * with audio track pid being 0x1100..0x11FF */
4688 if ( p_pmt->i_program_number == 0x1 &&
4689 p_pmt->i_pcr_pid == 0x1000 &&
4690 ( i_pid >> 8 ) == 0x11 )
4692 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4693 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4696 es_format_Init( &p_es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4699 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pes_es_t *p_es,
4700 const dvbpsi_pmt_es_t *p_dvbpsies )
4702 es_format_t *p_fmt = &p_es->fmt;
4704 /* Blu-Ray mapping */
4705 switch( p_dvbpsies->i_type )
4708 p_fmt->i_cat = AUDIO_ES;
4709 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4712 p_fmt->i_cat = AUDIO_ES;
4713 p_fmt->i_codec = VLC_CODEC_A52;
4716 case 0x85: /* DTS-HD High resolution audio */
4717 case 0x86: /* DTS-HD Master audio */
4718 case 0xA2: /* Secondary DTS audio */
4719 p_fmt->i_cat = AUDIO_ES;
4720 p_fmt->i_codec = VLC_CODEC_DTS;
4723 case 0x83: /* TrueHD AC3 */
4724 p_fmt->i_cat = AUDIO_ES;
4725 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4728 case 0x84: /* E-AC3 */
4729 case 0xA1: /* Secondary E-AC3 */
4730 p_fmt->i_cat = AUDIO_ES;
4731 p_fmt->i_codec = VLC_CODEC_EAC3;
4733 case 0x90: /* Presentation graphics */
4734 p_fmt->i_cat = SPU_ES;
4735 p_fmt->i_codec = VLC_CODEC_BD_PG;
4737 case 0x91: /* Interactive graphics */
4738 case 0x92: /* Subtitle */
4741 p_fmt->i_cat = VIDEO_ES;
4742 p_fmt->i_codec = VLC_CODEC_VC1;
4745 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4746 p_dvbpsies->i_pid, p_dvbpsies->i_type );
4753 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pes_es_t *p_es,
4754 const dvbpsi_pmt_es_t *p_dvbpsies )
4760 vlc_fourcc_t i_codec;
4762 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4763 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4764 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4765 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4766 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4767 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4768 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4769 { "", UNKNOWN_ES, 0 }
4771 es_format_t *p_fmt = &p_es->fmt;
4773 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4775 if( PMTEsHasRegistration( p_demux, p_dvbpsies, p_regs[i].psz_tag ) )
4777 p_fmt->i_cat = p_regs[i].i_cat;
4778 p_fmt->i_codec = p_regs[i].i_codec;
4779 if (p_dvbpsies->i_type == 0x87)
4780 p_fmt->i_codec = VLC_CODEC_EAC3;
4787 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4789 static const char *audio_type[] = {
4791 N_("clean effects"),
4792 N_("hearing impaired"),
4793 N_("visual impaired commentary"),
4796 if (type < 0 || type > 3)
4797 msg_Dbg( p_demux, "unknown audio type: %d", type);
4799 return strdup(audio_type[type]);
4803 static void PMTParseEsIso639( demux_t *p_demux, ts_pes_es_t *p_es,
4804 const dvbpsi_pmt_es_t *p_dvbpsies )
4806 /* get language descriptor */
4807 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x0a );
4812 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4815 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4819 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4820 p_es->fmt.psz_language = malloc( 4 );
4821 if( p_es->fmt.psz_language )
4823 memcpy( p_es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
4824 p_es->fmt.psz_language[3] = 0;
4825 msg_Dbg( p_demux, "found language: %s", p_es->fmt.psz_language);
4827 int type = p_decoded->code[0].i_audio_type;
4828 p_es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
4830 p_es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
4832 p_es->fmt.i_extra_languages = p_decoded->i_code_count-1;
4833 if( p_es->fmt.i_extra_languages > 0 )
4834 p_es->fmt.p_extra_languages =
4835 malloc( sizeof(*p_es->fmt.p_extra_languages) *
4836 p_es->fmt.i_extra_languages );
4837 if( p_es->fmt.p_extra_languages )
4839 for( unsigned i = 0; i < p_es->fmt.i_extra_languages; i++ )
4841 p_es->fmt.p_extra_languages[i].psz_language = malloc(4);
4842 if( p_es->fmt.p_extra_languages[i].psz_language )
4844 memcpy( p_es->fmt.p_extra_languages[i].psz_language,
4845 p_decoded->code[i+1].iso_639_code, 3 );
4846 p_es->fmt.p_extra_languages[i].psz_language[3] = '\0';
4848 int type = p_decoded->code[i].i_audio_type;
4849 p_es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
4853 p_es->fmt.psz_language = malloc( 4 );
4854 if( p_es->fmt.psz_language )
4856 memcpy( p_es->fmt.psz_language,
4857 p_decoded->i_iso_639_code, 3 );
4858 p_es->fmt.psz_language[3] = 0;
4863 static inline void SetExtraESGroupAndID( demux_sys_t *p_sys, es_format_t *p_fmt,
4864 const es_format_t *p_parent_fmt )
4866 if ( p_sys->b_es_id_pid ) /* pid is 13 bits */
4867 p_fmt->i_id = (p_sys->i_next_extraid++ << 13) | p_parent_fmt->i_id;
4868 p_fmt->i_group = p_parent_fmt->i_group;
4871 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid, bool b_create_delayed )
4873 demux_sys_t *p_sys = p_demux->p_sys;
4875 if( b_create_delayed )
4876 p_sys->es_creation = CREATE_ES;
4878 if( pid && p_sys->es_creation == CREATE_ES )
4880 /* FIXME: other owners / shared pid */
4881 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
4882 for( int i = 0; i < pid->u.p_pes->extra_es.i_size; i++ )
4884 es_format_t *p_fmt = &pid->u.p_pes->extra_es.p_elems[i]->fmt;
4885 SetExtraESGroupAndID( p_sys, p_fmt, &pid->u.p_pes->es.fmt );
4886 pid->u.p_pes->extra_es.p_elems[i]->id = es_out_Add( p_demux->out, p_fmt );
4888 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
4890 /* Update the default program == first created ES group */
4891 if( p_sys->b_default_selection )
4893 p_sys->b_default_selection = false;
4894 assert(p_sys->programs.i_size == 1);
4895 if( p_sys->programs.p_elems[0] != pid->p_parent->u.p_pmt->i_number )
4896 p_sys->programs.p_elems[0] = pid->p_parent->u.p_pmt->i_number;
4897 msg_Dbg( p_demux, "Default program is %d", pid->p_parent->u.p_pmt->i_number );
4901 if( b_create_delayed )
4903 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
4904 for( int i=0; i< p_pat->programs.i_size; i++ )
4906 ts_pmt_t *p_pmt = p_pat->programs.p_elems[i]->u.p_pmt;
4907 for( int j=0; j<p_pmt->e_streams.i_size; j++ )
4909 ts_pid_t *pid = p_pmt->e_streams.p_elems[j];
4910 if( pid->u.p_pes->es.id )
4913 pid->u.p_pes->es.id = es_out_Add( p_demux->out, &pid->u.p_pes->es.fmt );
4914 for( int k = 0; k < pid->u.p_pes->extra_es.i_size; k++ )
4916 es_format_t *p_fmt = &pid->u.p_pes->extra_es.p_elems[k]->fmt;
4917 SetExtraESGroupAndID( p_sys, p_fmt, &pid->u.p_pes->es.fmt );
4918 pid->u.p_pes->extra_es.p_elems[k]->id = es_out_Add( p_demux->out, p_fmt );
4920 p_sys->i_pmt_es += 1 + pid->u.p_pes->extra_es.i_size;
4925 UpdatePESFilters( p_demux, p_sys->b_es_all );
4928 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_dvbpsipmt )
4930 demux_t *p_demux = data;
4931 demux_sys_t *p_sys = p_demux->p_sys;
4933 ts_pid_t *pmtpid = NULL;
4934 ts_pmt_t *p_pmt = NULL;
4936 msg_Dbg( p_demux, "PMTCallBack called" );
4938 if (unlikely(PID(p_sys, 0)->type != TYPE_PAT))
4940 assert(PID(p_sys, 0)->type == TYPE_PAT);
4941 dvbpsi_pmt_delete(p_dvbpsipmt);
4944 const ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
4946 /* First find this PMT declared in PAT */
4947 for( int i = 0; !pmtpid && i < p_pat->programs.i_size; i++ )
4949 const int i_pmt_prgnumber = p_pat->programs.p_elems[i]->u.p_pmt->i_number;
4950 if( i_pmt_prgnumber != TS_USER_PMT_NUMBER &&
4951 i_pmt_prgnumber == p_dvbpsipmt->i_program_number )
4953 pmtpid = p_pat->programs.p_elems[i];
4954 assert(pmtpid->type == TYPE_PMT);
4955 p_pmt = pmtpid->u.p_pmt;
4959 if( pmtpid == NULL )
4961 msg_Warn( p_demux, "unreferenced program (broken stream)" );
4962 dvbpsi_pmt_delete(p_dvbpsipmt);
4966 pmtpid->i_flags |= FLAG_SEEN;
4968 if( p_pmt->i_version != -1 &&
4969 ( !p_dvbpsipmt->b_current_next || p_pmt->i_version == p_dvbpsipmt->i_version ) )
4971 dvbpsi_pmt_delete( p_dvbpsipmt );
4975 /* Save old es array */
4976 DECL_ARRAY(ts_pid_t *) old_es_rm;
4977 old_es_rm.i_alloc = p_pmt->e_streams.i_alloc;
4978 old_es_rm.i_size = p_pmt->e_streams.i_size;
4979 old_es_rm.p_elems = p_pmt->e_streams.p_elems;
4980 ARRAY_INIT(p_pmt->e_streams);
4984 IODFree( p_pmt->iod );
4986 for( int i=0; i<old_es_rm.i_size; i++ )
4987 old_es_rm.p_elems[i]->u.p_pes->es.p_mpeg4desc = NULL;
4990 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
4991 p_dvbpsipmt->i_program_number, p_dvbpsipmt->i_version, p_dvbpsipmt->i_pcr_pid );
4992 p_pmt->i_pid_pcr = p_dvbpsipmt->i_pcr_pid;
4993 p_pmt->i_version = p_dvbpsipmt->i_version;
4995 ValidateDVBMeta( p_demux, p_pmt->i_pid_pcr );
4997 if( ProgramIsSelected( p_sys, p_pmt->i_number ) )
4998 SetPIDFilter( p_sys, PID(p_sys, p_pmt->i_pid_pcr), true ); /* Set demux filter */
5000 /* Parse PMT descriptors */
5001 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
5002 dvbpsi_descriptor_t *p_dr;
5004 /* First pass for standard detection */
5005 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
5007 int i_arib_flags = 0; /* Descriptors can be repeated */
5008 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5014 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
5015 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
5019 i_arib_flags |= 1 << 1;
5022 i_arib_flags |= 1 << 2;
5028 if ( i_arib_flags == 0x07 ) //0b111
5029 p_sys->arib.e_mode = ARIBMODE_ENABLED;
5032 for( p_dr = p_dvbpsipmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5034 /* special descriptors handling */
5037 case 0x1d: /* We have found an IOD descriptor */
5038 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
5039 p_pmt->iod = IODNew( VLC_OBJECT(p_demux), p_dr->i_length, p_dr->p_data );
5043 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5044 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5047 case 0x5: /* Registration Descriptor */
5048 if( p_dr->i_length != 4 )
5050 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
5054 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
5055 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
5056 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
5061 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
5065 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
5068 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
5069 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
5070 /* TODO: apply visibility flags */
5074 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
5078 dvbpsi_pmt_es_t *p_dvbpsies;
5079 for( p_dvbpsies = p_dvbpsipmt->p_first_es; p_dvbpsies != NULL; p_dvbpsies = p_dvbpsies->p_next )
5081 bool b_reusing_pid = false;
5084 ts_pid_t *pespid = PID(p_sys, p_dvbpsies->i_pid);
5085 if ( pespid->type == TYPE_PES && pespid->p_parent->u.p_pmt->i_number != p_pmt->i_number )
5087 msg_Warn( p_demux, " * PMT wants to get a share or pid %d (unsupported)", pespid->i_pid );
5091 /* Find out if the PID was already declared */
5092 for( int i = 0; i < old_es_rm.i_size; i++ )
5094 if( old_es_rm.p_elems[i]->i_pid == p_dvbpsies->i_pid )
5096 b_reusing_pid = true;
5100 ValidateDVBMeta( p_demux, p_dvbpsies->i_pid );
5102 char const * psz_typedesc = "";
5103 switch(p_dvbpsies->i_type)
5106 psz_typedesc = "ISO/IEC Reserved";
5109 psz_typedesc = "ISO/IEC 11172 Video";
5112 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
5115 psz_typedesc = "ISO/IEC 11172 Audio";
5118 psz_typedesc = "ISO/IEC 13818-3 Audio";
5121 psz_typedesc = "ISO/IEC 13818-1 private_sections";
5124 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
5127 psz_typedesc = "ISO/IEC 13522 MHEG";
5130 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
5133 psz_typedesc = "ITU-T Rec. H.222.1";
5136 psz_typedesc = "ISO/IEC 13818-6 type A";
5139 psz_typedesc = "ISO/IEC 13818-6 type B";
5142 psz_typedesc = "ISO/IEC 13818-6 type C";
5145 psz_typedesc = "ISO/IEC 13818-6 type D";
5148 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
5151 if (p_dvbpsies->i_type >= 0x0F && p_dvbpsies->i_type <=0x7F)
5152 psz_typedesc = "ISO/IEC 13818-1 Reserved";
5154 psz_typedesc = "User Private";
5157 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
5158 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5160 for( p_dr = p_dvbpsies->p_first_descriptor; p_dr != NULL;
5161 p_dr = p_dr->p_next )
5163 msg_Dbg( p_demux, " - descriptor tag 0x%x",
5167 if ( !PIDSetup( p_demux, TYPE_PES, pespid, pmtpid ) )
5169 msg_Warn( p_demux, " * pid=%d type=0x%x %s (skipped)",
5170 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
5177 p_pes = ts_pes_New( p_demux );
5183 p_pes = pespid->u.p_pes;
5187 ARRAY_APPEND( p_pmt->e_streams, pespid );
5189 PIDFillFormat( &p_pes->es.fmt, p_dvbpsies->i_type );
5191 pespid->i_flags |= SEEN(PID(p_sys, p_dvbpsies->i_pid));
5193 bool b_registration_applied = false;
5194 if ( p_dvbpsies->i_type >= 0x80 ) /* non standard, extensions */
5196 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
5198 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, &p_pes->es, p_dvbpsies ) ))
5199 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
5200 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5204 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, &p_pes->es, p_dvbpsies ) ))
5205 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
5206 p_dvbpsies->i_pid, p_dvbpsies->i_type );
5210 if ( !b_registration_applied )
5212 switch( p_dvbpsies->i_type )
5215 /* Handle PES private data */
5216 PMTSetupEs0x06( p_demux, p_pes, p_dvbpsies );
5218 /* All other private or reserved types */
5223 PMTSetupEsISO14496( p_demux, &p_pes->es, p_pmt, p_dvbpsies );
5227 PMTSetupEs0x83( p_dvbpsipmt, &p_pes->es, p_dvbpsies->i_pid );
5230 PMTSetupEs0xA0( p_demux, &p_pes->es, p_dvbpsies );
5233 PMTSetupEs0xD1( p_demux, &p_pes->es, p_dvbpsies );
5236 PMTSetupEs0xEA( p_demux, &p_pes->es, p_dvbpsies );
5242 if( p_pes->es.fmt.i_cat == AUDIO_ES ||
5243 ( p_pes->es.fmt.i_cat == SPU_ES &&
5244 p_pes->es.fmt.i_codec != VLC_CODEC_DVBS &&
5245 p_pes->es.fmt.i_codec != VLC_CODEC_TELETEXT ) )
5247 PMTParseEsIso639( p_demux, &p_pes->es, p_dvbpsies );
5250 switch( p_pes->es.fmt.i_codec )
5252 case VLC_CODEC_SCTE_27:
5253 p_pes->data_type = TS_ES_DATA_TABLE_SECTION;
5256 //pid->es->data_type = TS_ES_DATA_PES;
5260 /* Set Groups / ID */
5261 p_pes->es.fmt.i_group = p_dvbpsipmt->i_program_number;
5262 if( p_sys->b_es_id_pid )
5263 p_pes->es.fmt.i_id = p_dvbpsies->i_pid;
5265 if( p_pes->es.fmt.i_cat == UNKNOWN_ES )
5267 msg_Dbg( p_demux, " => pid %d content is *unknown*",
5268 p_dvbpsies->i_pid );
5269 p_pes->es.fmt.psz_description = strdup( psz_typedesc );
5273 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
5274 p_dvbpsies->i_pid, (char*)&p_pes->es.fmt.i_codec );
5276 /* Check if we can avoid restarting the ES */
5279 /* p_pes points to a tmp pes */
5280 if( pespid->u.p_pes->es.fmt.i_codec != p_pes->es.fmt.i_codec ||
5281 pespid->u.p_pes->es.fmt.i_extra != p_pes->es.fmt.i_extra ||
5282 pespid->u.p_pes->es.fmt.i_extra != 0 ||
5283 pespid->u.p_pes->extra_es.i_size != p_pes->extra_es.i_size ||
5284 !( ( !pespid->u.p_pes->es.fmt.psz_language &&
5285 !p_pes->es.fmt.psz_language ) ||
5286 ( pespid->u.p_pes->es.fmt.psz_language &&
5287 p_pes->es.fmt.psz_language &&
5288 !strcmp( pespid->u.p_pes->es.fmt.psz_language,
5289 p_pes->es.fmt.psz_language ) ) ) )
5291 /* Differs, swap then */
5292 ts_pes_t *e = pespid->u.p_pes;
5293 pespid->u.p_pes = p_pes;
5296 /* p_pes still tmp, but now contains old config */
5297 pespid->u.p_pes->es.id = p_pes->es.id;
5298 if( pespid->u.p_pes->es.id )
5300 p_pes->es.id = NULL;
5301 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5302 pespid->u.p_pes->es.id, pespid->u.p_pes->es.fmt );
5305 for( int i=0; i<pespid->u.p_pes->extra_es.i_size &&
5306 i<p_pes->extra_es.i_size; i++ )
5308 pespid->u.p_pes->extra_es.p_elems[i]->id = p_pes->extra_es.p_elems[i]->id;
5309 if( pespid->u.p_pes->extra_es.p_elems[i]->id )
5311 es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
5312 pespid->u.p_pes->extra_es.p_elems[i]->id,
5313 pespid->u.p_pes->extra_es.p_elems[i]->fmt );
5314 p_pes->extra_es.p_elems[i]->id = NULL;
5319 ts_pes_Del( p_demux, p_pes ); // delete temp
5323 AddAndCreateES( p_demux, pespid, false );
5327 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x09 );
5328 if( p_dr && p_dr->i_length >= 2 )
5330 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5331 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5335 /* Set CAM descrambling */
5336 if( !ProgramIsSelected( p_sys, p_pmt->i_number ) )
5338 dvbpsi_pmt_delete( p_dvbpsipmt );
5340 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
5341 p_dvbpsipmt ) != VLC_SUCCESS )
5343 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED && !p_sys->arib.b25stream )
5345 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
5346 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
5347 if (!p_sys->arib.b25stream)
5348 dvbpsi_pmt_delete( p_dvbpsipmt );
5349 } else dvbpsi_pmt_delete( p_dvbpsipmt );
5352 /* Decref or clean now unused es */
5353 for( int i = 0; i < old_es_rm.i_size; i++ )
5354 PIDRelease( p_demux, old_es_rm.p_elems[i] );
5355 ARRAY_RESET( old_es_rm );
5357 UpdatePESFilters( p_demux, p_sys->b_es_all );
5359 if( !p_sys->b_trust_pcr )
5361 int i_cand = FindPCRCandidate( p_pmt );
5362 p_pmt->i_pid_pcr = i_cand;
5363 p_pmt->pcr.b_disable = true;
5364 msg_Warn( p_demux, "PCR not trusted for program %d, set up workaround using pid %d",
5365 p_pmt->i_number, i_cand );
5368 /* Probe Boundaries */
5369 if( p_sys->b_canfastseek && p_pmt->i_last_dts == -1 )
5371 p_pmt->i_last_dts = 0;
5372 ProbeStart( p_demux, p_pmt->i_number );
5373 ProbeEnd( p_demux, p_pmt->i_number );
5377 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
5379 /* Some Dreambox streams have all PMT set to same pid */
5380 int i_prev_pid = -1;
5381 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
5383 p_program = p_program->p_next )
5385 if( p_program->i_pid == i_prev_pid )
5387 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
5388 return VLC_EGENERIC;
5390 i_prev_pid = p_program->i_pid;
5395 static void PATCallBack( void *data, dvbpsi_pat_t *p_dvbpsipat )
5397 demux_t *p_demux = data;
5398 demux_sys_t *p_sys = p_demux->p_sys;
5399 dvbpsi_pat_program_t *p_program;
5400 ts_pid_t *patpid = PID(p_sys, 0);
5401 ts_pat_t *p_pat = PID(p_sys, 0)->u.p_pat;
5403 patpid->i_flags |= FLAG_SEEN;
5405 msg_Dbg( p_demux, "PATCallBack called" );
5407 if(unlikely( PID(p_sys, 0)->type != TYPE_PAT ))
5409 msg_Warn( p_demux, "PATCallBack called on invalid pid" );
5413 if( ( p_pat->i_version != -1 &&
5414 ( !p_dvbpsipat->b_current_next ||
5415 p_dvbpsipat->i_version == p_pat->i_version ) ) ||
5416 ( p_pat->i_ts_id != -1 && p_dvbpsipat->i_ts_id != p_pat->i_ts_id ) ||
5417 p_sys->b_user_pmt || PATCheck( p_demux, p_dvbpsipat ) )
5419 dvbpsi_pat_delete( p_dvbpsipat );
5423 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
5424 p_dvbpsipat->i_ts_id, p_dvbpsipat->i_version, p_dvbpsipat->b_current_next );
5426 /* Save old programs array */
5427 DECL_ARRAY(ts_pid_t *) old_pmt_rm;
5428 old_pmt_rm.i_alloc = p_pat->programs.i_alloc;
5429 old_pmt_rm.i_size = p_pat->programs.i_size;
5430 old_pmt_rm.p_elems = p_pat->programs.p_elems;
5431 ARRAY_INIT(p_pat->programs);
5433 /* now create programs */
5434 for( p_program = p_dvbpsipat->p_first_program; p_program != NULL;
5435 p_program = p_program->p_next )
5437 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
5439 if( p_program->i_number == 0 )
5442 ts_pid_t *pmtpid = PID(p_sys, p_program->i_pid);
5444 ValidateDVBMeta( p_demux, p_program->i_pid );
5446 /* create or temporary incref pid */
5447 if( !PIDSetup( p_demux, TYPE_PMT, pmtpid, patpid ) )
5449 msg_Warn( p_demux, " * number=%d pid=%d (ignored)", p_program->i_number,
5454 pmtpid->u.p_pmt->i_number = p_program->i_number;
5456 if( !dvbpsi_pmt_attach( pmtpid->u.p_pmt->handle, p_program->i_number, PMTCallBack, p_demux ) )
5457 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5458 p_program->i_number );
5460 ARRAY_APPEND( p_pat->programs, pmtpid );
5462 /* Now select PID at access level */
5463 if( p_sys->programs.i_size == 0 ||
5464 ProgramIsSelected( p_sys, p_program->i_number ) )
5466 if( p_sys->programs.i_size == 0 )
5468 msg_Dbg( p_demux, "temporary receiving program %d", p_program->i_number );
5469 p_sys->b_default_selection = true;
5470 ARRAY_APPEND( p_sys->programs, p_program->i_number );
5473 if( SetPIDFilter( p_sys, pmtpid, true ) )
5474 p_sys->b_access_control = false;
5475 else if ( p_sys->es_creation == DELAY_ES )
5476 p_sys->es_creation = CREATE_ES;
5479 p_pat->i_version = p_dvbpsipat->i_version;
5480 p_pat->i_ts_id = p_dvbpsipat->i_ts_id;
5482 for(int i=0; i<old_pmt_rm.i_size; i++)
5484 /* decref current or release now unreferenced */
5485 PIDRelease( p_demux, old_pmt_rm.p_elems[i] );
5487 ARRAY_RESET(old_pmt_rm);
5489 dvbpsi_pat_delete( p_dvbpsipat );
5492 static inline bool handle_Init( demux_t *p_demux, dvbpsi_t **handle )
5494 *handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
5497 (*handle)->p_sys = (void *) p_demux;
5501 static ts_pat_t *ts_pat_New( demux_t *p_demux )
5503 ts_pat_t *pat = malloc( sizeof( ts_pat_t ) );
5507 if( !handle_Init( p_demux, &pat->handle ) )
5513 pat->i_version = -1;
5515 ARRAY_INIT( pat->programs );
5520 static void ts_pat_Del( demux_t *p_demux, ts_pat_t *pat )
5522 if( dvbpsi_decoder_present( pat->handle ) )
5523 dvbpsi_pat_detach( pat->handle );
5524 dvbpsi_delete( pat->handle );
5525 for( int i=0; i<pat->programs.i_size; i++ )
5526 PIDRelease( p_demux, pat->programs.p_elems[i] );
5527 ARRAY_RESET( pat->programs );
5531 static ts_pmt_t *ts_pmt_New( demux_t *p_demux )
5533 ts_pmt_t *pmt = malloc( sizeof( ts_pmt_t ) );
5537 if( !handle_Init( p_demux, &pmt->handle ) )
5543 ARRAY_INIT( pmt->e_streams );
5545 pmt->i_version = -1;
5547 pmt->i_pid_pcr = 0x1FFF;
5550 pmt->i_last_dts = -1;
5552 pmt->pcr.i_current = -1;
5553 pmt->pcr.i_first = -1;
5554 pmt->pcr.b_disable = false;
5555 pmt->pcr.i_first_dts = VLC_TS_INVALID;
5556 pmt->pcr.i_pcroffset = -1;
5558 pmt->pcr.b_fix_done = false;
5563 static void ts_pmt_Del( demux_t *p_demux, ts_pmt_t *pmt )
5565 if( dvbpsi_decoder_present( pmt->handle ) )
5566 dvbpsi_pmt_detach( pmt->handle );
5567 dvbpsi_delete( pmt->handle );
5568 for( int i=0; i<pmt->e_streams.i_size; i++ )
5569 PIDRelease( p_demux, pmt->e_streams.p_elems[i] );
5570 ARRAY_RESET( pmt->e_streams );
5572 IODFree( pmt->iod );
5573 if( pmt->i_number > -1 )
5574 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, pmt->i_number );
5578 static ts_pes_t *ts_pes_New( demux_t *p_demux )
5580 VLC_UNUSED(p_demux);
5581 ts_pes_t *pes = malloc( sizeof( ts_pes_t ) );
5586 pes->es.p_mpeg4desc = NULL;
5587 es_format_Init( &pes->es.fmt, UNKNOWN_ES, 0 );
5588 ARRAY_INIT( pes->extra_es );
5589 pes->data_type = TS_ES_DATA_PES;
5590 pes->i_data_size = 0;
5591 pes->i_data_gathered = 0;
5593 pes->pp_last = &pes->p_data;
5594 pes->p_prepcr_outqueue = NULL;
5599 static void ts_pes_Del( demux_t *p_demux, ts_pes_t *pes )
5603 es_out_Del( p_demux->out, pes->es.id );
5604 p_demux->p_sys->i_pmt_es--;
5608 block_ChainRelease( pes->p_data );
5610 if( pes->p_prepcr_outqueue )
5611 block_ChainRelease( pes->p_prepcr_outqueue );
5613 es_format_Clean( &pes->es.fmt );
5615 for( int i = 0; i < pes->extra_es.i_size; i++ )
5617 if( pes->extra_es.p_elems[i]->id )
5619 es_out_Del( p_demux->out, pes->extra_es.p_elems[i]->id );
5620 p_demux->p_sys->i_pmt_es--;
5622 es_format_Clean( &pes->extra_es.p_elems[i]->fmt );
5623 free( pes->extra_es.p_elems[i] );
5625 ARRAY_RESET( pes->extra_es );
5630 static ts_psi_t *ts_psi_New( demux_t *p_demux )
5632 ts_psi_t *psi = malloc( sizeof( ts_psi_t ) );
5636 if( !handle_Init( p_demux, &psi->handle ) )
5642 psi->i_version = -1;
5647 static void ts_psi_Del( demux_t *p_demux, ts_psi_t *psi )
5649 VLC_UNUSED(p_demux);
5650 if( dvbpsi_decoder_present( psi->handle ) )
5651 dvbpsi_DetachDemux( psi->handle );
5652 dvbpsi_delete( psi->handle );