1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x.nl>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
39 #include <vlc_access.h> /* DVB-specific things */
40 #include <vlc_demux.h>
43 #include <vlc_charset.h> /* FromCharset, for EIT */
46 #include "../mux/mpeg/csa.h"
48 /* Include dvbpsi headers */
49 # include <dvbpsi/dvbpsi.h>
50 # include <dvbpsi/demux.h>
51 # include <dvbpsi/descriptor.h>
52 # include <dvbpsi/pat.h>
53 # include <dvbpsi/pmt.h>
54 # include <dvbpsi/sdt.h>
55 # include <dvbpsi/dr.h>
56 # include <dvbpsi/psi.h>
59 # include <dvbpsi/eit.h>
62 # include <dvbpsi/tot.h>
64 #include "../mux/mpeg/dvbpsi_compat.h"
65 #include "../mux/mpeg/streams.h"
66 #include "../mux/mpeg/tsutil.h"
67 #include "../mux/mpeg/tables.h"
69 #include "../codec/opus_header.h"
74 VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
79 vfprintf(stderr, format, ap);
87 #include <aribb24/aribb24.h>
88 #include <aribb24/decoder.h>
91 typedef enum arib_modes_e
94 ARIBMODE_DISABLED = 0,
98 /*****************************************************************************
100 *****************************************************************************/
101 static int Open ( vlc_object_t * );
102 static void Close ( vlc_object_t * );
105 * - Rename "extra pmt" to "user pmt"
106 * - Update extra pmt description
107 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
108 * where pid_description could take 3 forms:
109 * 1. pid:pcr (to force the pcr pid)
111 * 3. pid:type=fourcc where type=(video|audio|spu)
113 #define PMT_TEXT N_("Extra PMT")
114 #define PMT_LONGTEXT N_( \
115 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
117 #define PID_TEXT N_("Set id of ES to PID")
118 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
119 " handled by VLC to the same value as the PID in" \
120 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
121 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
123 #define CSA_TEXT N_("CSA Key")
124 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
125 "16 char string (8 hexadecimal bytes).")
127 #define CSA2_TEXT N_("Second CSA Key")
128 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
129 "16 char string (8 hexadecimal bytes).")
132 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
133 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
134 "The decryption routines subtract the TS-header from the value before " \
137 #define SPLIT_ES_TEXT N_("Separate sub-streams")
138 #define SPLIT_ES_LONGTEXT N_( \
139 "Separate teletex/dvbs pages into independent ES. " \
140 "It can be useful to turn off this option when using stream output." )
142 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
143 #define SEEK_PERCENT_LONGTEXT N_( \
144 "Seek and position based on a percent byte position, not a PCR generated " \
145 "time position. If seeking doesn't work property, turn on this option." )
147 #define PCR_TEXT N_("Trust in-stream PCR")
148 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
150 static const int const arib_mode_list[] =
151 { ARIBMODE_AUTO, ARIBMODE_ENABLED, ARIBMODE_DISABLED };
152 static const char *const arib_mode_list_text[] =
153 { N_("Auto"), N_("Enabled"), N_("Disabled") };
155 #define SUPPORT_ARIB_TEXT N_("ARIB STD-B24 mode")
156 #define SUPPORT_ARIB_LONGTEXT N_( \
157 "Forces ARIB STD-B24 mode for decoding characters." \
158 "This feature affects EPG information and subtitles." )
161 set_description( N_("MPEG Transport Stream demuxer") )
162 set_shortname ( "MPEG-TS" )
163 set_category( CAT_INPUT )
164 set_subcategory( SUBCAT_INPUT_DEMUX )
166 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
167 add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
169 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
171 add_obsolete_string( "ts-out" ) /* since 2.2.0 */
172 add_obsolete_integer( "ts-out-mtu" ) /* since 2.2.0 */
173 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
175 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
177 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
180 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
181 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
183 add_integer( "ts-arib", ARIBMODE_AUTO, SUPPORT_ARIB_TEXT, SUPPORT_ARIB_LONGTEXT, false )
184 change_integer_list( arib_mode_list, arib_mode_list_text )
186 add_obsolete_bool( "ts-silent" );
188 set_capability( "demux", 10 )
189 set_callbacks( Open, Close )
193 /*****************************************************************************
195 *****************************************************************************/
196 static const char *const ppsz_teletext_type[] = {
199 N_("Teletext subtitles"),
200 N_("Teletext: additional information"),
201 N_("Teletext: program schedule"),
202 N_("Teletext subtitles: hearing impaired")
207 uint8_t i_objectTypeIndication;
208 uint8_t i_streamType;
213 } decoder_config_descriptor_t;
222 decoder_config_descriptor_t dec_descr;
224 } es_mpeg4_descriptor_t;
226 #define ES_DESCRIPTOR_COUNT 255
232 es_mpeg4_descriptor_t es_descr[ES_DESCRIPTOR_COUNT];
238 dvbpsi_handle handle;
244 /* IOD stuff (mpeg4) */
245 iod_descriptor_t *iod;
251 /* for special PAT/SDT case */
252 dvbpsi_handle handle; /* PAT/SDT/EIT */
265 TS_ES_DATA_TABLE_SECTION
270 TS_PMT_REGISTRATION_NONE = 0,
271 TS_PMT_REGISTRATION_HDMV
272 } ts_pmt_registration_type_t;
278 ts_es_data_type_t data_type;
284 es_mpeg4_descriptor_t *p_mpeg4desc;
294 int i_cc; /* countinuity counter */
297 /* PSI owner (ie PMT -> PAT, ES -> PMT */
305 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
311 vlc_fourcc_t i_fourcc;
322 #define MIN_ES_PID 4 /* Should be 32.. broken muxers */
323 #define MAX_ES_PID 8190
325 #define FROM_SCALE(x) (VLC_TS_0 + ((x) * 100 / 9))
326 #define TO_SCALE(x) (((x) - VLC_TS_0) * 9 / 100)
332 vlc_mutex_t csa_lock;
334 /* TS packet size (188, 192, 204) */
337 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
338 int i_packet_header_size;
340 /* how many TS packet we read at once */
343 /* to determine length and time */
346 mtime_t i_current_pcr;
348 bool b_force_seek_per_percent;
357 arib_instance_t *p_instance;
370 bool b_delay_es_creation;
382 bool b_access_control;
388 int64_t i_dvb_length;
389 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
392 int i_current_program;
393 vlc_list_t programs_list;
397 /* broken PCR handling */
398 bool b_program_pcr_seen;
404 mtime_t i_first_dts; /* first dts encountered for the stream */
405 int i_timesourcepid; /* which pid we saved the dts from */
406 bool b_pat_deadline; /* set if we haven't seen PAT within 140ms */
415 static int Demux ( demux_t *p_demux );
416 static int Control( demux_t *p_demux, int i_query, va_list args );
418 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
419 static void PIDClean( demux_t *, ts_pid_t *pid );
420 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
422 static void PATCallBack( void*, dvbpsi_pat_t * );
423 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
424 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
425 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
426 uint16_t i_extension, demux_t * );
428 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
429 uint8_t i_table_id, uint16_t i_extension );
432 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
434 static inline int PIDGet( block_t *p )
436 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
439 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
440 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid );
442 static block_t* ReadTSPacket( demux_t *p_demux );
443 static int Seek( demux_t *p_demux, double f_percent );
444 static void GetFirstPCR( demux_t *p_demux );
445 static void GetLastPCR( demux_t *p_demux );
446 static void CheckPCR( demux_t *p_demux );
447 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
448 static void PCRFixHandle( demux_t *, block_t * );
449 static mtime_t AdjustPTSWrapAround( demux_t *, mtime_t );
451 static void IODFree( iod_descriptor_t * );
453 #define TS_USER_PMT_NUMBER (0)
454 static int UserPmt( demux_t *p_demux, const char * );
456 static int SetPIDFilter( demux_t *, int i_pid, bool b_selected );
457 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
459 #define TS_PACKET_SIZE_188 188
460 #define TS_PACKET_SIZE_192 192
461 #define TS_PACKET_SIZE_204 204
462 #define TS_PACKET_SIZE_MAX 204
463 #define TS_HEADER_SIZE 4
465 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size, int i_offset )
467 const uint8_t *p_peek;
469 if( stream_Peek( p_demux->s,
470 &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
473 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
475 if( p_peek[i_offset + i_sync] != 0x47 )
478 /* Check next 3 sync bytes */
479 int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
480 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
482 msg_Err( p_demux, "cannot peek" );
485 if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
486 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
487 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
489 return TS_PACKET_SIZE_188;
491 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
492 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
493 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
497 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
499 return TS_PACKET_SIZE_192;
501 else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
502 p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
503 p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
505 return TS_PACKET_SIZE_204;
509 if( p_demux->b_force )
511 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
512 return TS_PACKET_SIZE_188;
514 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
518 #define TOPFIELD_HEADER_SIZE 3712
520 static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, int *pi_header_size, vdr_info_t *p_vdr )
522 const uint8_t *p_peek;
524 int i_packet_size = -1;
526 if( stream_Peek( p_demux->s,
527 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
530 if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
531 p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
532 stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
533 == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
535 i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
536 if( i_packet_size != -1 )
538 msg_Dbg( p_demux, "this is a topfield file" );
540 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
541 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
542 * but after the filename the offsets seem to be incorrect. - DJ */
543 int i_duration, i_name;
544 char *psz_name = xmalloc(25);
545 char *psz_event_name;
546 char *psz_event_text = xmalloc(130);
547 char *psz_ext_text = xmalloc(1025);
549 // 2 bytes version Uimsbf (4,5)
550 // 2 bytes reserved (6,7)
551 // 2 bytes duration in minutes Uimsbf (8,9(
552 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
553 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
554 // 2 bytes service number in channel list (10, 11)
555 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
556 // 4 bytes of reserved + tuner info (14,15,16,17)
557 // 2 bytes of Service ID Bslbf (18,19)
558 // 2 bytes of PMT PID Uimsbf (20,21)
559 // 2 bytes of PCR PID Uimsbf (22,23)
560 // 2 bytes of Video PID Uimsbf (24,25)
561 // 2 bytes of Audio PID Uimsbf (26,27)
562 // 24 bytes filename Bslbf
563 memcpy( psz_name, &p_peek[28], 24 );
565 msg_Dbg( p_demux, "recordingname=%s", psz_name );
566 // 1 byte of sat index Uimsbf (52)
567 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
568 // 4 bytes of freq. Uimsbf (56,57,58,59)
569 // 2 bytes of symbol rate Uimsbf (60,61)
570 // 2 bytes of TS stream ID Uimsbf (62,63)
573 // 2 bytes duration Uimsbf (70,71)
574 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
575 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
576 // 4 bytes EventID Uimsbf (72-75)
577 // 8 bytes of Start and End time info (76-83)
578 // 1 byte reserved (84)
579 // 1 byte event name length Uimsbf (89)
580 i_name = (int)(p_peek[89]&~0x81);
581 msg_Dbg( p_demux, "event name length = %d", i_name);
582 psz_event_name = xmalloc( i_name+1 );
583 // 1 byte parental rating (90)
584 // 129 bytes of event text
585 memcpy( psz_event_name, &p_peek[91], i_name );
586 psz_event_name[i_name] = '\0';
587 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
588 psz_event_text[129-i_name] = '\0';
589 msg_Dbg( p_demux, "event name=%s", psz_event_name );
590 msg_Dbg( p_demux, "event text=%s", psz_event_text );
591 // 12 bytes reserved (220)
593 // 2 bytes Event Text Length Uimsbf
594 // 4 bytes EventID Uimsbf
595 // FIXME We just have 613 bytes. not enough for this entire text
596 // 1024 bytes Extended Event Text Bslbf
597 memcpy( psz_ext_text, p_peek+372, 1024 );
598 psz_ext_text[1024] = '\0';
599 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
600 // 52 bytes reserved Bslbf
602 p_vdr->i_service = GetWBE(&p_peek[18]);
604 return i_packet_size;
605 //return TS_PACKET_SIZE_188;
609 return DetectPacketSize( p_demux, pi_header_size, 0 );
612 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
613 static void vlc_dvbpsi_reset( demux_t *p_demux )
615 demux_sys_t *p_sys = p_demux->p_sys;
617 ts_pid_t *pat = &p_sys->pid[0];
618 ts_pid_t *sdt = &p_sys->pid[0x11];
619 ts_pid_t *eit = &p_sys->pid[0x12];
620 ts_pid_t *tdt = &p_sys->pid[0x14];
622 if( pat->psi->handle )
624 if( dvbpsi_decoder_present( pat->psi->handle ) )
625 dvbpsi_pat_detach( pat->psi->handle );
626 dvbpsi_delete( pat->psi->handle );
627 pat->psi->handle = NULL;
630 if( sdt->psi->handle )
632 if( dvbpsi_decoder_present( sdt->psi->handle ) )
633 dvbpsi_DetachDemux( sdt->psi->handle );
634 dvbpsi_delete( sdt->psi->handle );
635 sdt->psi->handle = NULL;
638 if( eit->psi->handle )
640 if( dvbpsi_decoder_present( eit->psi->handle ) )
641 dvbpsi_DetachDemux( eit->psi->handle );
642 dvbpsi_delete( eit->psi->handle );
643 eit->psi->handle = NULL;
646 if( tdt->psi->handle )
648 if( dvbpsi_decoder_present( tdt->psi->handle ) )
649 dvbpsi_DetachDemux( tdt->psi->handle );
650 dvbpsi_delete( tdt->psi->handle );
651 tdt->psi->handle = NULL;
656 static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
658 return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
659 (mtime_t)(p_data[1] << 22)|
660 ((mtime_t)(p_data[2]&0xfe) << 14)|
661 (mtime_t)(p_data[3] << 7)|
662 (mtime_t)(p_data[4] >> 1);
665 static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, bool b_adaptfield )
667 demux_sys_t *p_sys = p_demux->p_sys;
668 const uint8_t *p_pes = p_pesstart;
669 pid->probed.i_type = -1;
673 uint8_t len = *p_pes;
678 p_pes++; /* stuffing */
682 pid->probed.b_haspcr = ( *p_pes >= 7 && (p_pes[1] & 0x10) );
687 if( p_pes - p_pesstart >= TS_PACKET_SIZE_188 - 9)
690 if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
693 size_t i_pesextoffset = 8;
695 if( p_pes[7] & 0x80 ) // PTS
698 i_dts = ExtractPESTimestamp( &p_pes[9] );
700 if( p_pes[7] & 0x40 ) // DTS
703 i_dts = ExtractPESTimestamp( &p_pes[14] );
705 if( p_pes[7] & 0x20 ) // ESCR
707 if( p_pes[7] & 0x10 ) // ESrate
709 if( p_pes[7] & 0x08 ) // DSM
711 if( p_pes[7] & 0x04 ) // CopyInfo
713 if( p_pes[7] & 0x02 ) // PESCRC
716 /* HeaderdataLength */
717 const size_t i_payloadoffset = 8 + 1 + p_pes[8];
720 if( p_pes[7] & 0x01 ) // PESExt
722 size_t i_extension2_offset = 1;
723 if ( p_pes[i_pesextoffset] & 0x80 ) // private data
724 i_extension2_offset += 16;
725 if ( p_pes[i_pesextoffset] & 0x40 ) // pack
726 i_extension2_offset += 1;
727 if ( p_pes[i_pesextoffset] & 0x20 ) // seq
728 i_extension2_offset += 2;
729 if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
730 i_extension2_offset += 2;
731 if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
733 uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
734 i_extension2_offset += i_len;
737 /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
739 if( &p_pes[i_payloadoffset] - p_pesstart >= TS_PACKET_SIZE_188 - 4)
742 const uint8_t *p_data = &p_pes[i_payloadoffset];
744 if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
746 if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
748 pid->probed.i_type = 0x06;
749 pid->probed.i_fourcc = VLC_CODEC_DTS;
751 else if( !memcmp( p_data, "\x0B\x77", 2 ) )
753 pid->probed.i_type = 0x06;
754 pid->probed.i_fourcc = VLC_CODEC_EAC3;
756 else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
758 switch(p_data[1] & 18)
760 /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
761 11 - MPEG Version 1 (ISO/IEC 11172-3) */
763 pid->probed.i_type = 0x04;
766 pid->probed.i_type = 0x03;
771 switch(p_data[1] & 6)
777 pid->probed.i_type = 0x04;
778 pid->probed.i_fourcc = VLC_CODEC_MPGA;
781 pid->probed.i_type = 0x04;
782 pid->probed.i_fourcc = VLC_CODEC_MP2;
785 pid->probed.i_type = 0x04;
786 pid->probed.i_fourcc = VLC_CODEC_MP3;
793 else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
795 if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
797 pid->probed.i_type = 0x1b;
798 pid->probed.i_fourcc = VLC_CODEC_H264;
800 else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
802 pid->probed.i_type = 0x02;
803 pid->probed.i_fourcc = VLC_CODEC_MPGV;
807 /* Track timestamps and flag missing PAT */
808 if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
810 p_sys->patfix.i_first_dts = i_dts;
811 p_sys->patfix.i_timesourcepid = pid->i_pid;
813 else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
814 !p_sys->patfix.b_pat_deadline )
816 if( i_dts - p_sys->patfix.i_first_dts > 9 * 140000 / 100 )
817 p_sys->patfix.b_pat_deadline = true;
822 static void BuildPATCallback( void *p_opaque, block_t *p_block )
824 ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
825 dvbpsi_PushPacket( pat_pid->psi->handle, p_block->p_buffer );
828 static void BuildPMTCallback( void *p_opaque, block_t *p_block )
830 ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
833 for( int i_prg = 0; i_prg < program_pid->psi->i_prg; i_prg++ )
835 dvbpsi_PushPacket( program_pid->psi->prg[i_prg]->handle,
838 p_block = p_block->p_next;
842 static void MissingPATPMTFixup( demux_t *p_demux )
844 demux_sys_t *p_sys = p_demux->p_sys;
845 int i_program_number = 1234;
846 int i_program_pid = 1337;
847 int i_pcr_pid = 0x1FFF;
850 if( p_sys->pid[i_program_pid].b_seen )
852 /* Find a free one */
853 for( i_program_pid = MIN_ES_PID;
854 i_program_pid <= MAX_ES_PID && p_sys->pid[i_program_pid].b_seen;
858 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
860 if( !p_sys->pid[i].b_seen ||
861 p_sys->pid[i].probed.i_type == -1 )
864 if( i_pcr_pid == 0x1FFF && ( p_sys->pid[i].probed.i_type == 0x03 ||
865 p_sys->pid[i].probed.b_haspcr ) )
866 i_pcr_pid = p_sys->pid[i].i_pid;
874 ts_stream_t patstream =
877 .i_continuity_counter = 0x10,
878 .b_discontinuity = false
881 ts_stream_t pmtprogramstream =
883 .i_pid = i_program_pid,
884 .i_continuity_counter = 0x0,
885 .b_discontinuity = false
888 BuildPAT( DVBPSI_HANDLE_PARAM(p_sys->pid[0].psi->handle)
889 &p_sys->pid[0], BuildPATCallback,
892 1, &pmtprogramstream, &i_program_number );
894 if( !p_sys->pid[i_program_pid].psi )
896 msg_Err( p_demux, "PAT creation failed" );
905 es_format_t esfmt = {0};
906 struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
907 pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
908 if( esstreams && mapped )
911 for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
913 if( !p_sys->pid[i].b_seen ||
914 p_sys->pid[i].probed.i_type == -1 )
917 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
918 esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
919 esstreams[j].ts.i_pid = p_sys->pid[i].i_pid;
920 mapped[j].pes = &esstreams[j].pes;
921 mapped[j].ts = &esstreams[j].ts;
922 mapped[j].fmt = &esfmt;
926 BuildPMT( DVBPSI_HANDLE_PARAM(p_sys->pid[0].psi->handle) VLC_OBJECT(p_demux),
927 &p_sys->pid[i_program_pid], BuildPMTCallback,
931 1, &pmtprogramstream, &i_program_number,
938 /*****************************************************************************
940 *****************************************************************************/
941 static int Open( vlc_object_t *p_this )
943 demux_t *p_demux = (demux_t*)p_this;
946 int i_packet_size, i_packet_header_size = 0;
949 vdr_info_t vdr = {0};
951 /* Search first sync byte */
952 i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
953 if( i_packet_size < 0 )
956 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
959 memset( p_sys, 0, sizeof( demux_sys_t ) );
960 vlc_mutex_init( &p_sys->csa_lock );
962 p_demux->pf_demux = Demux;
963 p_demux->pf_control = Control;
965 /* Init p_sys field */
966 p_sys->b_dvb_meta = true;
967 p_sys->b_access_control = true;
968 p_sys->i_current_program = 0;
969 p_sys->programs_list.i_count = 0;
970 p_sys->programs_list.p_values = NULL;
971 p_sys->i_tdt_delta = 0;
972 p_sys->i_dvb_start = 0;
973 p_sys->i_dvb_length = 0;
977 p_sys->arib.b25stream = NULL;
978 p_sys->stream = p_demux->s;
980 p_sys->b_broken_charset = false;
982 for( int i = 0; i < 8192; i++ )
984 ts_pid_t *pid = &p_sys->pid[i];
987 pid->b_valid = false;
988 pid->probed.i_fourcc = 0;
989 pid->probed.i_type = 0;
991 /* PID 8191 is padding */
992 p_sys->pid[8191].b_seen = true;
993 p_sys->i_packet_size = i_packet_size;
994 p_sys->i_packet_header_size = i_packet_header_size;
995 p_sys->i_ts_read = 50;
997 p_sys->b_start_record = false;
999 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1000 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
1002 (table)->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG ); \
1003 if( ! (table)->psi->handle ) \
1005 vlc_mutex_destroy( &p_sys->csa_lock ); \
1007 return VLC_ENOMEM; \
1009 (table)->psi->handle->p_sys = (void *) VLC_OBJECT(obj); \
1010 if( !dvbpsi_AttachDemux( (table)->psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
1012 vlc_dvbpsi_reset( obj ); \
1013 vlc_mutex_destroy( &p_sys->csa_lock ); \
1015 return VLC_EGENERIC; \
1020 /* Init PAT handler */
1021 pat = &p_sys->pid[0];
1022 PIDInit( pat, true, NULL );
1023 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1024 pat->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
1025 if( !pat->psi->handle )
1027 vlc_mutex_destroy( &p_sys->csa_lock );
1031 pat->psi->handle->p_sys = (void *) p_demux;
1032 if( !dvbpsi_pat_attach( pat->psi->handle, PATCallBack, p_demux ) )
1034 vlc_dvbpsi_reset( p_demux );
1035 vlc_mutex_destroy( &p_sys->csa_lock );
1037 return VLC_EGENERIC;
1040 pat->psi->handle = dvbpsi_AttachPAT( PATCallBack, p_demux );
1042 if( p_sys->b_dvb_meta )
1044 ts_pid_t *sdt = &p_sys->pid[0x11];
1045 ts_pid_t *eit = &p_sys->pid[0x12];
1047 PIDInit( sdt, true, NULL );
1048 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1049 VLC_DVBPSI_DEMUX_TABLE_INIT( sdt, p_demux )
1052 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
1055 PIDInit( eit, true, NULL );
1056 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1057 VLC_DVBPSI_DEMUX_TABLE_INIT( eit, p_demux )
1060 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
1063 ts_pid_t *tdt = &p_sys->pid[0x14];
1064 PIDInit( tdt, true, NULL );
1065 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1066 VLC_DVBPSI_DEMUX_TABLE_INIT( tdt, p_demux )
1069 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
1073 if( p_sys->b_access_control )
1075 if( SetPIDFilter( p_demux, 0x11, true ) ||
1076 SetPIDFilter( p_demux, 0x14, true ) ||
1077 SetPIDFilter( p_demux, 0x12, true ) )
1078 p_sys->b_access_control = false;
1082 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1083 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
1086 /* Init PMT array */
1087 TAB_INIT( p_sys->i_pmt, p_sys->pmt );
1088 p_sys->i_pmt_es = 0;
1089 p_sys->b_delay_es_creation = !p_sys->b_access_control;
1092 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
1094 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
1096 /* We handle description of an extra PMT */
1097 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
1098 p_sys->b_user_pmt = false;
1099 if( psz_string && *psz_string )
1100 UserPmt( p_demux, psz_string );
1103 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
1104 if( psz_string && *psz_string )
1109 p_sys->csa = csa_New();
1111 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
1112 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
1113 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
1115 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
1117 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1120 else if ( i_res == VLC_SUCCESS )
1122 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
1126 csa_Delete( p_sys->csa );
1132 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
1133 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1135 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
1136 if( i_pkt < 4 || i_pkt > 188 )
1138 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
1139 msg_Warn( p_demux, "using default packet size of 188 bytes" );
1140 p_sys->i_csa_pkt_size = 188;
1143 p_sys->i_csa_pkt_size = i_pkt;
1144 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
1150 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
1152 p_sys->b_canseek = false;
1153 p_sys->i_pid_ref_pcr = -1;
1154 p_sys->i_first_pcr = -1;
1155 p_sys->i_current_pcr = -1;
1156 p_sys->i_last_pcr = -1;
1157 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
1158 p_sys->i_pcrs_num = 10;
1159 p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
1160 p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
1162 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
1164 if( !p_sys->p_pcrs || !p_sys->p_pos )
1170 bool b_can_fastseek = false;
1171 stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
1172 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &b_can_fastseek );
1173 if ( p_sys->b_canseek )
1175 if( b_can_fastseek )
1177 GetFirstPCR( p_demux );
1178 CheckPCR( p_demux );
1179 GetLastPCR( p_demux );
1182 if( p_sys->i_first_pcr < 0 || p_sys->i_last_pcr < 0 )
1184 msg_Dbg( p_demux, "Force Seek Per Percent: PCR's not found,");
1185 p_sys->b_force_seek_per_percent = true;
1189 while( p_sys->i_pmt_es <= 0
1190 && (!p_sys->pid[0].b_seen && !p_sys->patfix.b_pat_deadline) )
1192 if( Demux( p_demux ) != 1 )
1196 /* If we had no PAT within 140ms, create PAT/PMT from probed streams */
1197 if( p_sys->i_pmt_es == 0 && !p_sys->pid[0].b_seen )
1198 MissingPATPMTFixup( p_demux );
1203 /*****************************************************************************
1205 *****************************************************************************/
1206 static void Close( vlc_object_t *p_this )
1208 demux_t *p_demux = (demux_t*)p_this;
1209 demux_sys_t *p_sys = p_demux->p_sys;
1211 msg_Dbg( p_demux, "pid list:" );
1212 for( int i = 0; i < 8192; i++ )
1214 ts_pid_t *pid = &p_sys->pid[i];
1216 if( pid->b_valid && pid->psi )
1218 switch( pid->i_pid )
1221 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1222 if( dvbpsi_decoder_present( pid->psi->handle ) )
1223 dvbpsi_pat_detach( pid->psi->handle );
1224 dvbpsi_delete( pid->psi->handle );
1225 pid->psi->handle = NULL;
1227 dvbpsi_DetachPAT( pid->psi->handle );
1235 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
1237 /* SDT or EIT or TDT */
1238 dvbpsi_DetachDemux( pid->psi->handle );
1239 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1240 dvbpsi_delete( pid->psi->handle );
1241 pid->psi->handle = NULL;
1247 PIDClean( p_demux, pid );
1252 else if( pid->b_valid && pid->es )
1254 PIDClean( p_demux, pid );
1259 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
1263 if( pid->i_pid > 0 )
1264 SetPIDFilter( p_demux, pid->i_pid, false );
1267 vlc_mutex_lock( &p_sys->csa_lock );
1270 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
1271 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
1272 csa_Delete( p_sys->csa );
1274 vlc_mutex_unlock( &p_sys->csa_lock );
1276 TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
1278 free( p_sys->programs_list.p_values );
1280 free( p_sys->p_pcrs );
1281 free( p_sys->p_pos );
1284 if ( p_sys->arib.p_instance )
1285 arib_instance_destroy( p_sys->arib.p_instance );
1288 if ( p_sys->arib.b25stream )
1290 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
1291 stream_Delete( p_sys->arib.b25stream );
1294 vlc_mutex_destroy( &p_sys->csa_lock );
1298 /*****************************************************************************
1299 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
1300 *****************************************************************************/
1301 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
1302 vlc_value_t oldval, vlc_value_t newval,
1305 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1306 demux_t *p_demux = (demux_t*)p_this;
1307 demux_sys_t *p_sys = p_demux->p_sys;
1308 int i_tmp = (intptr_t)p_data;
1310 vlc_mutex_lock( &p_sys->csa_lock );
1312 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
1314 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
1316 vlc_mutex_unlock( &p_sys->csa_lock );
1320 /*****************************************************************************
1322 *****************************************************************************/
1323 static int Demux( demux_t *p_demux )
1325 demux_sys_t *p_sys = p_demux->p_sys;
1326 bool b_wait_es = p_sys->i_pmt_es <= 0;
1328 /* We read at most 100 TS packet or until a frame is completed */
1329 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1331 bool b_frame = false;
1333 if( !(p_pkt = ReadTSPacket( p_demux )) )
1338 if( p_sys->b_start_record )
1340 /* Enable recording once synchronized */
1341 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
1342 p_sys->b_start_record = false;
1345 /* Parse the TS packet */
1346 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
1348 /* Probe streams to build PAT/PMT after 140ms in case we don't see any PAT */
1349 if( !p_sys->pid[0].b_seen &&
1350 (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
1351 (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
1352 (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
1354 ProbePES( p_demux, p_pid, p_pkt->p_buffer + 4, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
1357 if( p_pid->b_valid )
1361 if( p_pid->i_pid == 0 || ( p_sys->b_dvb_meta && ( p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 || p_pid->i_pid == 0x14 ) ) )
1363 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1367 for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1369 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1373 block_Release( p_pkt );
1377 if(p_sys->b_delay_es_creation) /* No longer delay ES since that pid's program sends data */
1378 AddAndCreateES( p_demux, NULL );
1379 b_frame = GatherData( p_demux, p_pid, p_pkt );
1384 if( !p_pid->b_seen )
1386 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1388 /* We have to handle PCR if present */
1389 PCRHandle( p_demux, p_pid, p_pkt );
1390 block_Release( p_pkt );
1392 p_pid->b_seen = true;
1394 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1398 demux_UpdateTitleFromStream( p_demux );
1402 /*****************************************************************************
1404 *****************************************************************************/
1405 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1407 demux_sys_t *p_sys = p_demux->p_sys;
1413 if( p_sys->i_dvb_length > 0 )
1415 const int64_t t = mdate() + p_sys->i_tdt_delta;
1417 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1420 *pi_length = p_sys->i_dvb_length;
1422 *pi_time = t - p_sys->i_dvb_start;
1426 return VLC_EGENERIC;
1429 static int Control( demux_t *p_demux, int i_query, va_list args )
1431 demux_sys_t *p_sys = p_demux->p_sys;
1433 bool b_bool, *pb_bool;
1440 case DEMUX_GET_POSITION:
1441 pf = (double*) va_arg( args, double* );
1443 if( p_sys->b_force_seek_per_percent ||
1444 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1445 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 ||
1446 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1448 int64_t i_time, i_length;
1449 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1450 *pf = (double)i_time/(double)i_length;
1451 else if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1453 int64_t offset = stream_Tell( p_sys->stream );
1455 *pf = (double)offset / (double)i64;
1462 *pf = (double)(p_sys->i_current_pcr - p_sys->i_first_pcr) / (double)(p_sys->i_last_pcr - p_sys->i_first_pcr);
1466 case DEMUX_SET_POSITION:
1467 f = (double) va_arg( args, double );
1469 if(!p_sys->b_canseek)
1470 return VLC_EGENERIC;
1472 if( p_sys->b_force_seek_per_percent ||
1473 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1474 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1476 i64 = stream_Size( p_sys->stream );
1477 if( stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) )
1478 return VLC_EGENERIC;
1482 if( Seek( p_demux, f ) )
1484 p_sys->b_force_seek_per_percent = true;
1485 return VLC_EGENERIC;
1490 case DEMUX_GET_TIME:
1491 pi64 = (int64_t*)va_arg( args, int64_t * );
1492 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1493 p_sys->b_force_seek_per_percent ||
1494 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 )
1496 if( DVBEventInformation( p_demux, pi64, NULL ) )
1503 *pi64 = (p_sys->i_current_pcr - p_sys->i_first_pcr) * 100 / 9;
1507 case DEMUX_GET_LENGTH:
1508 pi64 = (int64_t*)va_arg( args, int64_t * );
1509 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1510 p_sys->b_force_seek_per_percent ||
1511 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1513 if( DVBEventInformation( p_demux, NULL, pi64 ) )
1520 *pi64 = (p_sys->i_last_pcr - p_sys->i_first_pcr) * 100 / 9;
1524 case DEMUX_SET_GROUP:
1528 i_int = (int)va_arg( args, int );
1529 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1530 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1532 if( i_int == 0 && p_sys->i_current_program > 0 )
1533 i_int = p_sys->i_current_program;
1535 if( p_sys->i_current_program > 0 )
1537 if( p_sys->i_current_program != i_int )
1538 SetPrgFilter( p_demux, p_sys->i_current_program, false );
1540 else if( p_sys->i_current_program < 0 )
1542 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
1543 SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
1548 p_sys->i_current_program = i_int;
1549 SetPrgFilter( p_demux, p_sys->i_current_program, true );
1551 else if( i_int < 0 )
1553 p_sys->i_current_program = -1;
1554 p_sys->programs_list.i_count = 0;
1557 vlc_list_t *p_dst = &p_sys->programs_list;
1558 free( p_dst->p_values );
1560 p_dst->p_values = calloc( p_list->i_count,
1561 sizeof(*p_dst->p_values) );
1562 if( p_dst->p_values )
1564 p_dst->i_count = p_list->i_count;
1565 for( int i = 0; i < p_list->i_count; i++ )
1567 p_dst->p_values[i] = p_list->p_values[i];
1568 SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
1576 case DEMUX_GET_TITLE_INFO:
1578 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1579 int *c = va_arg( args, int * );
1581 *va_arg( args, int* ) = 0; /* Title offset */
1582 *va_arg( args, int* ) = 0; /* Chapter offset */
1583 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1586 case DEMUX_SET_TITLE:
1587 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1589 case DEMUX_SET_SEEKPOINT:
1590 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1592 case DEMUX_GET_META:
1593 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1595 case DEMUX_CAN_RECORD:
1596 pb_bool = (bool*)va_arg( args, bool * );
1600 case DEMUX_SET_RECORD_STATE:
1601 b_bool = (bool)va_arg( args, int );
1604 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1605 p_sys->b_start_record = b_bool;
1608 case DEMUX_GET_SIGNAL:
1609 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1612 return VLC_EGENERIC;
1616 /*****************************************************************************
1618 *****************************************************************************/
1619 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1621 demux_sys_t *p_sys = p_demux->p_sys;
1622 char *psz_dup = strdup( psz_fmt );
1623 char *psz = psz_dup;
1626 ts_prg_psi_t *prg = NULL;
1632 i_pid = strtol( psz, &psz, 0 );
1633 if( i_pid < 2 || i_pid >= 8192 )
1636 /* Parse optional program number */
1639 i_number = strtol( &psz[1], &psz, 0 );
1642 ts_pid_t *pmt = &p_sys->pid[i_pid];
1644 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1645 PIDInit( pmt, true, NULL );
1648 prg = calloc( 1, sizeof( ts_prg_psi_t ) );
1652 prg->i_pid_pcr = -1;
1653 prg->i_pid_pmt = -1;
1654 prg->i_version = -1;
1655 prg->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1656 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1657 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
1660 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
1661 if( !dvbpsi_pmt_attach( prg->handle,
1662 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1663 PMTCallBack, p_demux ) )
1665 dvbpsi_delete( prg->handle );
1670 prg->handle = dvbpsi_AttachPMT(
1671 ((i_number != TS_USER_PMT_NUMBER) ? i_number : 1),
1672 PMTCallBack, p_demux );
1674 TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1676 psz = strchr( psz, '=' );
1679 while( psz && *psz )
1681 char *psz_next = strchr( psz, ',' );
1687 i_pid = strtol( psz, &psz, 0 );
1688 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1691 char *psz_opt = &psz[1];
1692 if( !strcmp( psz_opt, "pcr" ) )
1694 prg->i_pid_pcr = i_pid;
1696 else if( !p_sys->pid[i_pid].b_valid )
1698 ts_pid_t *pid = &p_sys->pid[i_pid];
1700 char *psz_arg = strchr( psz_opt, '=' );
1704 PIDInit( pid, false, pmt->psi);
1705 if( prg->i_pid_pcr <= 0 )
1706 prg->i_pid_pcr = i_pid;
1708 if( psz_arg && strlen( psz_arg ) == 4 )
1710 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1711 psz_arg[2], psz_arg[3] );
1712 int i_cat = UNKNOWN_ES;
1713 es_format_t *fmt = &pid->es->fmt;
1715 if( !strcmp( psz_opt, "video" ) )
1717 else if( !strcmp( psz_opt, "audio" ) )
1719 else if( !strcmp( psz_opt, "spu" ) )
1722 es_format_Init( fmt, i_cat, i_codec );
1723 fmt->b_packetized = false;
1727 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1728 PIDFillFormat( &pid->es->fmt, i_stream_type );
1730 pid->es->fmt.i_group = i_number;
1731 if( p_sys->b_es_id_pid )
1732 pid->es->fmt.i_id = i_pid;
1734 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1736 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1737 (char*)&pid->es->fmt.i_codec );
1738 pid->es->id = es_out_Add( p_demux->out,
1748 p_sys->b_user_pmt = true;
1749 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1756 return VLC_EGENERIC;
1759 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1761 demux_sys_t *p_sys = p_demux->p_sys;
1763 if( !p_sys->b_access_control )
1764 return VLC_EGENERIC;
1766 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1767 i_pid, b_selected );
1770 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1772 demux_sys_t *p_sys = p_demux->p_sys;
1773 ts_prg_psi_t *p_prg = NULL;
1776 p_sys->b_disable_pcr = !p_sys->b_trust_pcr;
1778 /* Search pmt to be unselected */
1779 for( int i = 0; i < p_sys->i_pmt; i++ )
1781 ts_pid_t *pmt = p_sys->pmt[i];
1783 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1785 if( pmt->psi->prg[i_prg]->i_number == i_prg_id )
1787 i_pmt_pid = p_sys->pmt[i]->i_pid;
1788 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1795 if( i_pmt_pid <= 0 )
1799 SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1800 if( p_prg->i_pid_pcr > 0 )
1801 SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
1804 for( int i = 2; i < 8192; i++ )
1806 ts_pid_t *pid = &p_sys->pid[i];
1808 if( !pid->b_valid || pid->psi )
1811 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1813 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1815 /* We only remove/select es that aren't defined by extra pmt */
1816 SetPIDFilter( p_demux, i, b_selected );
1823 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1825 bool b_old_valid = pid->b_valid;
1827 pid->b_valid = true;
1829 pid->b_scrambled = false;
1830 pid->p_owner = p_owner;
1831 pid->i_owner_number = 0;
1833 TAB_INIT( pid->i_extra_es, pid->extra_es );
1841 pid->psi = xmalloc( sizeof( ts_psi_t ) );
1842 pid->psi->handle = NULL;
1843 TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1847 pid->psi->i_pat_version = -1;
1848 pid->psi->i_sdt_version = -1;
1851 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1855 prg->i_version = -1;
1857 prg->i_pid_pcr = -1;
1858 prg->i_pid_pmt = -1;
1859 prg->i_pcr_value= -1;
1863 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1869 pid->es = calloc( 1, sizeof( ts_es_t ) );
1873 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1874 pid->es->data_type = TS_ES_DATA_PES;
1875 pid->es->pp_last = &pid->es->p_data;
1879 static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
1881 demux_sys_t *p_sys = p_demux->p_sys;
1882 es_out_t *out = p_demux->out;
1886 if( pid->psi->handle )
1888 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1889 if( dvbpsi_decoder_present( pid->psi->handle ) )
1890 dvbpsi_pmt_detach( pid->psi->handle );
1891 dvbpsi_delete( pid->psi->handle );
1892 pid->psi->handle = NULL;
1894 dvbpsi_DetachPMT( pid->psi->handle );
1897 for( int i = 0; i < pid->psi->i_prg; i++ )
1899 if( pid->psi->prg[i]->iod )
1900 IODFree( pid->psi->prg[i]->iod );
1901 if( pid->psi->prg[i]->handle )
1903 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1904 if( dvbpsi_decoder_present( pid->psi->prg[i]->handle ) )
1905 dvbpsi_pmt_detach( pid->psi->prg[i]->handle );
1906 dvbpsi_delete( pid->psi->prg[i]->handle );
1908 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1911 free( pid->psi->prg[i] );
1913 free( pid->psi->prg );
1920 es_out_Del( out, pid->es->id );
1924 if( pid->es->p_data )
1925 block_ChainRelease( pid->es->p_data );
1927 es_format_Clean( &pid->es->fmt );
1931 for( int i = 0; i < pid->i_extra_es; i++ )
1933 if( pid->extra_es[i]->id )
1935 es_out_Del( out, pid->extra_es[i]->id );
1939 if( pid->extra_es[i]->p_data )
1940 block_ChainRelease( pid->extra_es[i]->p_data );
1942 es_format_Clean( &pid->extra_es[i]->fmt );
1944 free( pid->extra_es[i] );
1946 if( pid->i_extra_es )
1947 free( pid->extra_es );
1950 pid->b_valid = false;
1953 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1958 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1969 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1971 block_t *out = NULL;
1972 block_t **last = NULL;
1974 uint8_t *buf = block->p_buffer;
1975 size_t len = block->i_buffer;
1977 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
1978 int16_t start_trim = 0, end_trim = 0;
1979 int start_trim_flag = (buf[1] >> 4) & 1;
1980 int end_trim_flag = (buf[1] >> 3) & 1;
1981 int control_extension_flag = (buf[1] >> 2) & 1;
1986 unsigned au_size = 0;
1994 if (start_trim_flag) {
1995 start_trim = read_opus_flag(&buf, &len);
1996 if (start_trim < 0) {
1997 msg_Err(demux, "Invalid start trimming flag");
2000 if (end_trim_flag) {
2001 end_trim = read_opus_flag(&buf, &len);
2003 msg_Err(demux, "Invalid end trimming flag");
2006 if (control_extension_flag && len) {
2007 unsigned l = *buf++; len--;
2009 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
2016 if (!au_size || au_size > len) {
2017 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
2021 block_t *au = block_Alloc(au_size);
2024 memcpy(au->p_buffer, buf, au_size);
2025 block_CopyProperties(au, block);
2034 au->i_nb_samples = opus_frame_duration(buf, au_size);
2035 if (end_trim && end_trim <= au->i_nb_samples)
2036 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
2040 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
2041 au->i_nb_samples -= start_trim;
2042 if (au->i_nb_samples == 0)
2043 au->i_flags |= BLOCK_FLAG_PREROLL;
2050 block_Release(block);
2054 /****************************************************************************
2056 ****************************************************************************/
2057 static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header,
2058 unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
2062 switch( p_header[3] )
2064 case 0xBC: /* Program stream map */
2065 case 0xBE: /* Padding */
2066 case 0xBF: /* Private stream 2 */
2067 case 0xF0: /* ECM */
2068 case 0xF1: /* EMM */
2069 case 0xFF: /* Program stream directory */
2070 case 0xF2: /* DSMCC stream */
2071 case 0xF8: /* ITU-T H.222.1 type E stream */
2075 if( ( p_header[6]&0xC0 ) == 0x80 )
2078 i_skip = p_header[8] + 9;
2080 if( p_header[7]&0x80 ) /* has pts */
2082 *pi_pts = ExtractPESTimestamp( &p_header[9] );
2084 if( p_header[7]&0x40 ) /* has dts */
2085 *pi_dts = ExtractPESTimestamp( &p_header[14] );
2091 while( i_skip < 23 && p_header[i_skip] == 0xff )
2097 msg_Err( p_demux, "too much MPEG-1 stuffing" );
2098 return VLC_EGENERIC;
2100 if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
2105 if( p_header[i_skip]&0x20 )
2107 *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
2109 if( p_header[i_skip]&0x10 ) /* has dts */
2111 *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
2131 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
2133 demux_sys_t *p_sys = p_demux->p_sys;
2135 unsigned i_pes_size = 0;
2136 unsigned i_skip = 0;
2139 mtime_t i_length = 0;
2141 /* FIXME find real max size */
2142 const int i_max = block_ChainExtract( p_pes, header, 34 );
2143 assert(i_max >= 34);
2144 if (unlikely(i_max < 34))
2146 block_ChainRelease( p_pes );
2150 if( pid->b_scrambled || header[0] != 0 || header[1] != 0 || header[2] != 1 )
2152 if ( !pid->b_scrambled )
2153 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
2154 header[0], header[1],header[2],header[3], pid->i_pid );
2155 block_ChainRelease( p_pes );
2159 /* TODO check size */
2160 if( ParsePESHeader( p_demux, (uint8_t*)&header, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
2162 block_ChainRelease( p_pes );
2168 i_pts = AdjustPTSWrapAround( p_demux, i_pts );
2170 i_dts = AdjustPTSWrapAround( p_demux, i_dts );
2173 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
2174 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
2178 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
2179 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
2180 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
2184 else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
2185 pid->es->p_mpeg4desc )
2187 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
2189 if( dcd->i_extra > 2 &&
2190 dcd->p_extra[0] == 0x10 &&
2191 ( dcd->p_extra[1]&0x10 ) )
2193 /* display length */
2194 if( p_pes->i_buffer + 2 <= i_skip )
2195 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
2199 if( p_pes->i_buffer + 2 <= i_skip )
2200 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
2206 while( p_pes && i_skip > 0 )
2208 if( p_pes->i_buffer <= i_skip )
2210 block_t *p_next = p_pes->p_next;
2212 i_skip -= p_pes->i_buffer;
2213 block_Release( p_pes );
2218 p_pes->i_buffer -= i_skip;
2219 p_pes->p_buffer += i_skip;
2224 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
2225 if( i_pts >= 0 && i_dts < 0 )
2233 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
2236 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
2238 p_pes->i_length = i_length * 100 / 9;
2240 p_block = block_ChainGather( p_pes );
2241 if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
2243 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2245 p_block->i_buffer = i_pes_size;
2248 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2251 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2253 else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
2255 if( p_block->i_pts <= VLC_TS_INVALID )
2257 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
2258 * In this case use the last PCR + 40ms */
2259 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
2261 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
2263 mtime_t i_pcr = pid->p_owner->prg[i]->i_pcr_value;
2264 if( i_pcr > VLC_TS_INVALID )
2265 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
2271 else if( pid->es->fmt.i_codec == VLC_CODEC_ARIB_A ||
2272 pid->es->fmt.i_codec == VLC_CODEC_ARIB_C )
2274 if( p_block->i_pts <= VLC_TS_INVALID )
2276 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
2278 p_block->i_buffer = i_pes_size;
2281 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
2284 p_block->p_buffer[p_block->i_buffer -1] = '\0';
2287 else if( pid->es->fmt.i_codec == VLC_CODEC_OPUS)
2289 p_block = Opus_Parse(p_demux, p_block);
2293 block_t *p_next = p_block->p_next;
2294 p_block->p_next = NULL;
2295 for( int i = 0; i < pid->i_extra_es; i++ )
2297 es_out_Send( p_demux->out, pid->extra_es[i]->id,
2298 block_Duplicate( p_block ) );
2301 PCRFixHandle( p_demux, p_block );
2303 if ( p_sys->b_disable_pcr && p_block->i_dts > VLC_TS_INVALID )
2304 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2305 pid->i_owner_number, p_block->i_dts);
2307 if( !p_sys->b_disable_pcr && p_block->i_dts > VLC_TS_INVALID &&
2308 p_block->i_dts < (VLC_TS_0 + p_sys->i_current_pcr * 100 / 9) )
2309 msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64"us later than pcr",
2310 pid->i_pid, (p_sys->i_current_pcr * 100 / 9) - p_block->i_dts + VLC_TS_0 );
2312 es_out_Send( p_demux->out, pid->es->id, p_block );
2319 msg_Warn( p_demux, "empty pes" );
2323 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
2325 block_t *p_content = block_ChainGather( p_data );
2326 mtime_t i_date = -1;
2327 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
2329 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
2331 i_date = pid->p_owner->prg[i]->i_pcr_value;
2338 if( pid->es->fmt.i_codec == VLC_CODEC_SCTE_27 )
2340 /* We need to extract the truncated pts stored inside the payload */
2341 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
2344 size_t i_offset = 4;
2345 if( p_content->p_buffer[3] & 0x40 )
2347 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
2348 p_content->p_buffer[8];
2351 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
2353 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
2356 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
2357 if( i_display_in < i_date )
2358 i_date = i_display_in + (1ll << 32);
2360 i_date = i_display_in;
2367 p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
2369 es_out_Send( p_demux->out, pid->es->id, p_content );
2371 PCRFixHandle( p_demux, p_content );
2373 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
2375 block_t *p_data = pid->es->p_data;
2377 /* remove the pes from pid */
2378 pid->es->p_data = NULL;
2379 pid->es->i_data_size = 0;
2380 pid->es->i_data_gathered = 0;
2381 pid->es->pp_last = &pid->es->p_data;
2383 if( pid->es->data_type == TS_ES_DATA_PES )
2385 ParsePES( p_demux, pid, p_data );
2387 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
2389 ParseTableSection( p_demux, pid, p_data );
2393 block_ChainRelease( p_data );
2397 static block_t* ReadTSPacket( demux_t *p_demux )
2399 demux_sys_t *p_sys = p_demux->p_sys;
2403 /* Get a new TS packet */
2404 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2406 if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
2407 msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
2409 msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
2413 if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
2415 block_Release( p_pkt );
2419 /* Skip header (BluRay streams).
2420 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2421 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2423 p_pkt->p_buffer += p_sys->i_packet_header_size;
2424 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2426 /* Check sync byte and re-sync if needed */
2427 if( p_pkt->p_buffer[0] != 0x47 )
2429 msg_Warn( p_demux, "lost synchro" );
2430 block_Release( p_pkt );
2433 const uint8_t *p_peek;
2434 int i_peek, i_skip = 0;
2436 i_peek = stream_Peek( p_sys->stream, &p_peek,
2437 p_sys->i_packet_size * 10 );
2438 if( i_peek < p_sys->i_packet_size + 1 )
2440 msg_Dbg( p_demux, "eof ?" );
2444 while( i_skip < i_peek - p_sys->i_packet_size )
2446 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2447 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2453 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2454 stream_Read( p_sys->stream, NULL, i_skip );
2456 if( i_skip < i_peek - p_sys->i_packet_size )
2461 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2463 msg_Dbg( p_demux, "eof ?" );
2470 static mtime_t AdjustPCRWrapAround( demux_t *p_demux, mtime_t i_pcr )
2472 demux_sys_t *p_sys = p_demux->p_sys;
2474 * PCR is 33bit. If PCR reaches to 0x1FFFFFFFF (26:30:43.717), ressets from 0.
2475 * So, need to add 0x1FFFFFFFF, for calculating duration or current position.
2477 mtime_t i_adjust = 0;
2478 int64_t i_pos = stream_Tell( p_sys->stream );
2480 for( i = 1; i < p_sys->i_pcrs_num && p_sys->p_pos[i] <= i_pos; ++i )
2482 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2483 i_adjust += 0x1FFFFFFFF;
2485 if( p_sys->p_pcrs[i-1] > i_pcr )
2486 i_adjust += 0x1FFFFFFFF;
2488 return i_pcr + i_adjust;
2491 static mtime_t AdjustPTSWrapAround( demux_t *p_demux, mtime_t i_pts )
2493 demux_sys_t *p_sys = p_demux->p_sys;
2494 mtime_t i_pcr = p_sys->i_current_pcr;
2496 mtime_t i_adjustremain = i_pcr % 0x1FFFFFFFF;
2497 mtime_t i_adjustbase = i_pcr - i_adjustremain;
2499 mtime_t i_pts_adjust = i_adjustbase;
2501 /* PTS has rolled first */
2502 if( i_adjustremain >= 0xFFFFFFFF && i_pts < 0xFFFFFFFF )
2504 i_pts_adjust += 0x1FFFFFFFF;
2506 /* PCR has rolled first (PTS is late!) */
2507 else if( i_adjustremain < 0xFFFFFFFF && i_pts >= 0xFFFFFFFF )
2509 if( i_adjustbase >= 0x1FFFFFFFF ) /* we need to remove current roll */
2510 i_pts_adjust -= 0x1FFFFFFFF;
2513 i_pts += i_pts_adjust;
2517 static mtime_t GetPCR( block_t *p_pkt )
2519 const uint8_t *p = p_pkt->p_buffer;
2523 if( ( p[3]&0x20 ) && /* adaptation */
2527 /* PCR is 33 bits */
2528 i_pcr = ( (mtime_t)p[6] << 25 ) |
2529 ( (mtime_t)p[7] << 17 ) |
2530 ( (mtime_t)p[8] << 9 ) |
2531 ( (mtime_t)p[9] << 1 ) |
2532 ( (mtime_t)p[10] >> 7 );
2537 static int SeekToPCR( demux_t *p_demux, int64_t i_pos )
2539 demux_sys_t *p_sys = p_demux->p_sys;
2542 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2545 return VLC_EGENERIC;
2547 int64_t i_last_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2548 if( i_pos > i_last_pos )
2551 if( stream_Seek( p_sys->stream, i_pos ) )
2552 return VLC_EGENERIC;
2558 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2562 if( PIDGet( p_pkt ) == p_sys->i_pid_ref_pcr )
2564 i_pcr = GetPCR( p_pkt );
2566 block_Release( p_pkt );
2569 if( stream_Tell( p_sys->stream ) >= i_last_pos )
2574 stream_Seek( p_sys->stream, i_initial_pos );
2575 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2576 return VLC_EGENERIC;
2579 p_sys->i_current_pcr = i_pcr;
2583 static int Seek( demux_t *p_demux, double f_percent )
2585 demux_sys_t *p_sys = p_demux->p_sys;
2587 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2588 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2591 * Find the time position by using binary search algorithm.
2593 mtime_t i_target_pcr = (p_sys->i_last_pcr - p_sys->i_first_pcr) * f_percent + p_sys->i_first_pcr;
2595 int64_t i_head_pos = 0;
2598 mtime_t i_adjust = 0;
2600 for( i = 1; i < p_sys->i_pcrs_num; ++i )
2602 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2603 i_adjust += 0x1FFFFFFFF;
2604 if( p_sys->p_pcrs[i] + i_adjust > i_target_pcr )
2607 i_head_pos = p_sys->p_pos[i-1];
2608 i_tail_pos = ( i < p_sys->i_pcrs_num ) ? p_sys->p_pos[i] : stream_Size( p_sys->stream );
2610 msg_Dbg( p_demux, "Seek():i_head_pos:%"PRId64", i_tail_pos:%"PRId64, i_head_pos, i_tail_pos);
2612 bool b_found = false;
2614 while( i_head_pos <= i_tail_pos )
2616 /* Round i_pos to a multiple of p_sys->i_packet_size */
2617 int64_t i_pos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2618 int64_t i_div = i_pos % p_sys->i_packet_size;
2620 if( SeekToPCR( p_demux, i_pos ) )
2622 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2623 int64_t i_diff_msec = (p_sys->i_current_pcr - i_target_pcr) * 100 / 9 / 1000;
2624 if( i_diff_msec > 500 )
2626 i_tail_pos = i_pos - p_sys->i_packet_size;
2628 else if( i_diff_msec < -500 )
2630 i_head_pos = i_pos + p_sys->i_packet_size;
2634 // diff time <= 500msec
2642 msg_Dbg( p_demux, "Seek():cannot find a time position. i_cnt:%d", i_cnt );
2643 stream_Seek( p_sys->stream, i_initial_pos );
2644 p_sys->i_current_pcr = i_initial_pcr;
2645 return VLC_EGENERIC;
2649 msg_Dbg( p_demux, "Seek():can find a time position. i_cnt:%d", i_cnt );
2650 p_demux->p_sys->pcrfix.i_first_dts = 0;
2655 static void GetFirstPCR( demux_t *p_demux )
2657 demux_sys_t *p_sys = p_demux->p_sys;
2659 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2661 if( stream_Seek( p_sys->stream, 0 ) )
2668 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2672 mtime_t i_pcr = GetPCR( p_pkt );
2675 p_sys->i_pid_ref_pcr = PIDGet( p_pkt );
2676 p_sys->i_first_pcr = i_pcr;
2677 p_sys->i_current_pcr = i_pcr;
2679 block_Release( p_pkt );
2680 if( p_sys->i_first_pcr >= 0 )
2683 stream_Seek( p_sys->stream, i_initial_pos );
2686 static void GetLastPCR( demux_t *p_demux )
2688 demux_sys_t *p_sys = p_demux->p_sys;
2690 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2691 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2693 int64_t i_stream_size = stream_Size( p_sys->stream );
2694 int64_t i_last_pos = i_stream_size - p_sys->i_packet_size;
2695 /* Round i_pos to a multiple of p_sys->i_packet_size */
2696 int64_t i_pos = i_last_pos - p_sys->i_packet_size * 4500; /* FIXME if the value is not reasonable, please change it. */
2697 int64_t i_div = i_pos % p_sys->i_packet_size;
2700 if( i_pos <= i_initial_pos && i_pos >= i_stream_size )
2701 i_pos = i_initial_pos + p_sys->i_packet_size;
2702 if( i_pos < 0 && i_pos >= i_stream_size )
2707 if( SeekToPCR( p_demux, i_pos ) )
2709 p_sys->i_last_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2710 if( ( i_pos = stream_Tell( p_sys->stream ) ) >= i_last_pos )
2714 stream_Seek( p_sys->stream, i_initial_pos );
2715 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2716 p_sys->i_current_pcr = i_initial_pcr;
2719 static void CheckPCR( demux_t *p_demux )
2721 demux_sys_t *p_sys = p_demux->p_sys;
2723 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2724 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2726 int64_t i_size = stream_Size( p_sys->stream );
2729 p_sys->p_pcrs[0] = p_sys->i_first_pcr;
2730 p_sys->p_pos[0] = i_initial_pos;
2732 for( i = 1; i < p_sys->i_pcrs_num; ++i )
2734 /* Round i_pos to a multiple of p_sys->i_packet_size */
2735 int64_t i_pos = i_size / p_sys->i_pcrs_num * i;
2736 int64_t i_div = i_pos % p_sys->i_packet_size;
2738 if( SeekToPCR( p_demux, i_pos ) )
2740 p_sys->p_pcrs[i] = p_sys->i_current_pcr;
2741 p_sys->p_pos[i] = stream_Tell( p_sys->stream );
2742 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2744 msg_Dbg( p_demux, "PCR Wrap Around found between %d%% and %d%% (pcr:%"PRId64"(0x%09"PRIx64") pcr:%"PRId64"(0x%09"PRIx64"))",
2745 (int)((i-1)*100/p_sys->i_pcrs_num), (int)(i*100/p_sys->i_pcrs_num), p_sys->p_pcrs[i-1], p_sys->p_pcrs[i-1], p_sys->p_pcrs[i], p_sys->p_pcrs[i] );
2748 if( i < p_sys->i_pcrs_num )
2750 msg_Dbg( p_demux, "Force Seek Per Percent: Seeking failed at %d%%.", (int)(i*100/p_sys->i_pcrs_num) );
2751 p_sys->b_force_seek_per_percent = true;
2754 stream_Seek( p_sys->stream, i_initial_pos );
2755 p_sys->i_current_pcr = i_initial_pcr;
2758 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2760 demux_sys_t *p_sys = p_demux->p_sys;
2762 if( p_sys->i_pmt_es <= 0 )
2765 mtime_t i_pcr = GetPCR( p_bk );
2769 i_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
2771 if( p_sys->i_pid_ref_pcr == pid->i_pid )
2772 p_sys->i_current_pcr = i_pcr;
2774 /* Search program and set the PCR */
2775 for( int i = 0; i < p_sys->i_pmt; i++ )
2778 for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2780 ts_prg_psi_t *p_prg = p_sys->pmt[i]->psi->prg[i_prg];
2782 if( p_prg->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
2784 if( pid->p_owner ) /* PCR shall be on pid itself */
2786 p_prg->i_pcr_value = i_pcr; /* ? update PCR for the whole group program */
2787 i_group = p_prg->i_number;
2788 p_sys->pcrfix.b_program_pcr_seen = true;
2789 p_sys->pcrfix.i_first_dts = 0;
2790 p_sys->b_disable_pcr = !p_sys->b_trust_pcr;
2794 msg_Warn(p_demux, "discarding PCR update from pid %d which has no owner", pid->i_pid);
2798 else /* set PCR provided by current pid to program(s) referencing it */
2800 /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
2801 if( p_prg->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
2803 p_prg->i_pcr_value = i_pcr;
2804 i_group = p_prg->i_number; /* We've found a target group for update */
2805 p_sys->pcrfix.b_program_pcr_seen = true;
2806 p_sys->pcrfix.i_first_dts = 0;
2807 p_sys->b_disable_pcr = !p_sys->b_trust_pcr;
2811 if ( !p_sys->b_disable_pcr && i_group > 0 && p_sys->i_pmt_es )
2813 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2814 i_group, VLC_TS_0 + i_pcr * 100 / 9 );
2819 static void PCRFixHandle( demux_t *p_demux, block_t *p_block )
2821 demux_sys_t *p_sys = p_demux->p_sys;
2822 /* Record the first data packet timestamp in case there wont be any PCR */
2823 if( !p_sys->pcrfix.b_program_pcr_seen && !p_sys->b_disable_pcr )
2825 if( !p_sys->pcrfix.i_first_dts )
2827 p_sys->pcrfix.i_first_dts = p_block->i_dts;
2829 else if( p_block->i_dts - p_sys->pcrfix.i_first_dts > CLOCK_FREQ / 10 ) /* "shall not exceed 100ms" */
2831 p_sys->b_disable_pcr = true;
2832 msg_Warn( p_demux, "No PCR received for program %d, set up workaround",
2833 p_sys->i_current_program );
2838 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2840 const uint8_t *p = p_bk->p_buffer;
2841 const bool b_unit_start = p[1]&0x40;
2842 const bool b_scrambled = p[3]&0x80;
2843 const bool b_adaptation = p[3]&0x20;
2844 const bool b_payload = p[3]&0x10;
2845 const int i_cc = p[3]&0x0f; /* continuity counter */
2846 bool b_discontinuity = false; /* discontinuity */
2848 /* transport_scrambling_control is ignored */
2853 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
2854 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
2858 /* For now, ignore additional error correction
2859 * TODO: handle Reed-Solomon 204,188 error correction */
2860 p_bk->i_buffer = TS_PACKET_SIZE_188;
2864 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
2866 if( pid->es->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
2867 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2870 if( p_demux->p_sys->csa )
2872 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
2873 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
2874 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
2879 /* We don't have any adaptation_field, so payload starts
2880 * immediately after the 4 byte TS header */
2885 /* p[4] is adaptation_field_length minus one */
2889 /* discontinuity indicator found in stream */
2890 b_discontinuity = (p[5]&0x80) ? true : false;
2891 if( b_discontinuity && pid->es->p_data )
2893 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
2895 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
2899 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
2904 /* Test continuity counter */
2905 /* continuous when (one of this):
2907 * diff == 0 and payload == 0
2908 * diff == 0 and duplicate packet (playload != 0) <- should we
2909 * test the content ?
2911 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
2912 if( b_payload && i_diff == 1 )
2914 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
2918 if( pid->i_cc == 0xff )
2920 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
2924 else if( i_diff != 0 && !b_discontinuity )
2926 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
2927 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
2930 if( pid->es->p_data && pid->es->fmt.i_cat != VIDEO_ES &&
2931 pid->es->fmt.i_cat != AUDIO_ES )
2933 /* Small audio/video artifacts are usually better than
2934 * dropping full frames */
2935 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2940 PCRHandle( p_demux, pid, p_bk );
2942 if( i_skip >= 188 || pid->es->id == NULL )
2944 block_Release( p_bk );
2949 if( !pid->b_scrambled != !b_scrambled )
2951 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2952 pid->i_pid, pid->b_scrambled, b_scrambled );
2954 pid->b_scrambled = b_scrambled;
2956 for( int i = 0; i < pid->i_extra_es; i++ )
2958 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2959 pid->extra_es[i]->id, b_scrambled );
2961 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2962 pid->es->id, b_scrambled );
2965 /* We have to gather it */
2966 p_bk->p_buffer += i_skip;
2967 p_bk->i_buffer -= i_skip;
2971 if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
2973 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
2974 block_t *p = block_Duplicate( p_bk );
2977 p->i_buffer = i_pointer_field;
2979 block_ChainLastAppend( &pid->es->pp_last, p );
2981 p_bk->i_buffer -= 1 + i_pointer_field;
2982 p_bk->p_buffer += 1 + i_pointer_field;
2984 if( pid->es->p_data )
2986 ParseData( p_demux, pid );
2990 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2991 if( pid->es->data_type == TS_ES_DATA_PES )
2993 if( p_bk->i_buffer > 6 )
2995 pid->es->i_data_size = GetWBE( &p_bk->p_buffer[4] );
2996 if( pid->es->i_data_size > 0 )
2998 pid->es->i_data_size += 6;
3002 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
3004 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
3006 pid->es->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
3009 pid->es->i_data_gathered += p_bk->i_buffer;
3010 if( pid->es->i_data_size > 0 &&
3011 pid->es->i_data_gathered >= pid->es->i_data_size )
3013 ParseData( p_demux, pid );
3019 if( pid->es->p_data == NULL )
3021 /* msg_Dbg( p_demux, "broken packet" ); */
3022 block_Release( p_bk );
3026 block_ChainLastAppend( &pid->es->pp_last, p_bk );
3027 pid->es->i_data_gathered += p_bk->i_buffer;
3029 if( pid->es->i_data_size > 0 &&
3030 pid->es->i_data_gathered >= pid->es->i_data_size )
3032 ParseData( p_demux, pid );
3041 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
3043 switch( i_stream_type )
3045 case 0x01: /* MPEG-1 video */
3046 case 0x02: /* MPEG-2 video */
3047 case 0x80: /* MPEG-2 MOTO video */
3048 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
3050 case 0x03: /* MPEG-1 audio */
3051 case 0x04: /* MPEG-2 audio */
3052 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
3054 case 0x11: /* MPEG4 (audio) LATM */
3055 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
3056 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
3057 transport syntax, such as DST, ALS and SLS */
3058 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
3060 case 0x10: /* MPEG4 (video) */
3061 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
3063 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
3064 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
3066 case 0x24: /* HEVC */
3067 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
3069 case 0x42: /* CAVS (Chinese AVS) */
3070 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
3073 case 0x81: /* A52 (audio) */
3074 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
3076 case 0x82: /* SCTE-27 (sub) */
3077 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
3079 case 0x84: /* SDDS (audio) */
3080 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
3082 case 0x85: /* DTS (audio) */
3083 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
3085 case 0x87: /* E-AC3 */
3086 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
3089 case 0x91: /* A52 vls (audio) */
3090 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
3092 case 0x92: /* DVD_SPU vls (sub) */
3093 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
3096 case 0x94: /* SDDS (audio) */
3097 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
3100 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
3101 es_format_Init( fmt, UNKNOWN_ES, 0 );
3104 case 0x06: /* PES_PRIVATE (fixed later) */
3105 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
3106 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
3108 es_format_Init( fmt, UNKNOWN_ES, 0 );
3112 /* PES packets usually contain truncated frames */
3113 fmt->b_packetized = false;
3116 /*****************************************************************************
3117 * MP4 specific functions (IOD parser)
3118 *****************************************************************************/
3119 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
3122 unsigned int i_len = 0;
3128 i_len = ( i_len << 7 ) + ( i_b&0x7f );
3130 } while( i_b&0x80 && *pi_data > 0 );
3132 if (i_len > *pi_data)
3138 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
3141 while( *pi_data > 0 && bytes-- )
3152 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
3154 int len = IODGetBytes( pi_data, pp_data, 1 );
3157 char *url = strndup( (char*)*pp_data, len );
3163 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
3165 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
3167 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
3176 byte1 = IODGetBytes( &i_data, &p_data, 1 );
3177 byte2 = IODGetBytes( &i_data, &p_data, 1 );
3178 byte3 = IODGetBytes( &i_data, &p_data, 1 );
3179 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
3181 i_iod_label = byte1;
3184 else //correct implementation of the IOD_descriptor
3186 i_iod_label = byte2;
3190 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
3192 if( i_iod_tag != 0x02 )
3194 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
3198 IODDescriptorLength( &i_data, &p_data );
3200 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
3201 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
3202 i_od_id |= i_flags >> 6;
3203 ts_debug( "\n* od_id:%d", i_od_id );
3204 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
3205 if ((i_flags >> 5) & 0x01)
3207 p_iod->psz_url = IODGetURL( &i_data, &p_data );
3208 ts_debug( "\n* url string:%s", p_iod->psz_url );
3209 ts_debug( "\n*****************************\n" );
3214 p_iod->psz_url = NULL;
3217 /* Profile Level Indication */
3218 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
3219 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
3220 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
3221 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
3222 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
3225 int i_data_sav = i_data;
3226 uint8_t *p_data_sav = p_data;
3227 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
3229 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
3231 p_data = p_data_sav + i_length;
3232 i_data = i_data_sav - i_length;
3234 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
3235 i_length = IODDescriptorLength( &i_data, &p_data );
3237 i_data_sav = i_data;
3238 p_data_sav = p_data;
3242 if ( i_tag != 0x03 )
3244 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
3248 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
3249 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
3250 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
3251 if( b_streamDependenceFlag )
3252 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
3254 if( (i_flags >> 6) & 0x01 )
3255 es_descr->psz_url = IODGetURL( &i_data, &p_data );
3257 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
3258 if( b_OCRStreamFlag )
3259 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
3261 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
3263 ts_debug( "\n* ERR missing DecoderConfigDescr" );
3266 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
3267 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
3268 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
3269 i_flags = IODGetBytes( &i_data, &p_data, 1 );
3270 dec_descr->i_streamType = i_flags >> 2;
3272 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
3273 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
3274 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
3276 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
3278 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
3279 if( dec_descr->i_extra > 0 )
3281 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
3282 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
3283 p_data += dec_descr->i_extra;
3284 i_data -= dec_descr->i_extra;
3289 dec_descr->i_extra = 0;
3290 dec_descr->p_extra = NULL;
3293 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
3295 ts_debug( "\n* ERR missing SLConfigDescr" );
3298 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
3299 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
3302 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
3307 es_descr->b_ok = true;
3313 static void IODFree( iod_descriptor_t *p_iod )
3315 if( p_iod->psz_url )
3317 free( p_iod->psz_url );
3322 for( int i = 0; i < 255; i++ )
3324 #define es_descr p_iod->es_descr[i]
3327 if( es_descr.psz_url )
3328 free( es_descr.psz_url );
3330 free( es_descr.dec_descr.p_extra );
3337 /****************************************************************************
3338 ****************************************************************************
3339 ** libdvbpsi callbacks
3340 ****************************************************************************
3341 ****************************************************************************/
3342 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
3344 demux_sys_t *p_sys = p_demux->p_sys;
3346 if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
3347 p_sys->i_current_program == 0 )
3349 if( p_sys->i_current_program == i_pgrm )
3352 if( p_sys->programs_list.i_count != 0 )
3354 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
3356 if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
3363 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
3365 demux_sys_t *p_sys = p_demux->p_sys;
3367 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
3370 msg_Warn( p_demux, "Switching to non DVB mode" );
3372 /* This doesn't look like a DVB stream so don't try
3373 * parsing the SDT/EDT/TDT */
3375 for( int i = 0x11; i <= 0x14; i++ )
3377 if( i == 0x13 ) continue;
3378 ts_pid_t *p_pid = &p_sys->pid[i];
3382 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3383 if( dvbpsi_decoder_present( p_pid->psi->handle ))
3384 dvbpsi_DetachDemux( p_pid->psi->handle );
3385 dvbpsi_delete( p_pid->psi->handle );
3387 dvbpsi_DetachDemux( p_pid->psi->handle );
3391 p_pid->b_valid = false;
3393 SetPIDFilter( p_demux, i, false );
3395 p_sys->b_dvb_meta = false;
3398 #include "dvb-text.h"
3400 static char *EITConvertToUTF8( demux_t *p_demux,
3401 const unsigned char *psz_instring,
3405 demux_sys_t *p_sys = p_demux->p_sys;
3407 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3409 if ( !p_sys->arib.p_instance )
3410 p_sys->arib.p_instance = arib_instance_new( p_demux );
3411 if ( !p_sys->arib.p_instance )
3413 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
3417 char *psz_outstring = NULL;
3420 i_out = i_length * 4;
3421 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
3422 if( !psz_outstring )
3425 arib_initialize_decoder( p_decoder );
3426 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
3427 psz_outstring, i_out );
3428 arib_finalize_decoder( p_decoder );
3430 return psz_outstring;
3435 /* Deal with no longer broken providers (no switch byte
3436 but sending ISO_8859-1 instead of ISO_6937) without
3437 removing them from the broken providers table
3438 (keep the entry for correctly handling recorded TS).
3440 b_broken = b_broken && i_length && *psz_instring > 0x20;
3443 return FromCharset( "ISO_8859-1", psz_instring, i_length );
3444 return vlc_from_EIT( psz_instring, i_length );
3447 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
3449 demux_sys_t *p_sys = p_demux->p_sys;
3450 ts_pid_t *sdt = &p_sys->pid[0x11];
3451 dvbpsi_sdt_service_t *p_srv;
3453 msg_Dbg( p_demux, "SDTCallBack called" );
3455 if( p_sys->b_delay_es_creation ||
3456 !p_sdt->b_current_next ||
3457 p_sdt->i_version == sdt->psi->i_sdt_version )
3459 dvbpsi_DeleteSDT( p_sdt );
3463 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3465 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3470 p_sdt->i_version, p_sdt->b_current_next,
3471 p_sdt->i_network_id );
3473 p_sys->b_broken_charset = false;
3475 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3478 dvbpsi_descriptor_t *p_dr;
3480 const char *psz_type = NULL;
3481 const char *psz_status = NULL;
3483 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3484 "running=%d free_ca=%d",
3485 p_srv->i_service_id, p_srv->b_eit_schedule,
3486 p_srv->b_eit_present, p_srv->i_running_status,
3489 if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
3491 msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
3492 p_sys->vdr.i_service );
3496 p_meta = vlc_meta_New();
3497 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3499 if( p_dr->i_tag == 0x48 )
3501 static const char *ppsz_type[17] = {
3503 "Digital television service",
3504 "Digital radio sound service",
3506 "NVOD reference service",
3507 "NVOD time-shifted service",
3510 "SECAM coded signal",
3513 "NTSC coded signal",
3514 "Data broadcast service",
3515 "Reserved for Common Interface Usage",
3516 "RCS Map (see EN 301 790 [35])",
3517 "RCS FLS (see EN 301 790 [35])",
3520 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3524 /* Workarounds for broadcasters with broken EPG */
3526 if( p_sdt->i_network_id == 133 )
3527 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3529 /* List of providers using ISO8859-1 */
3530 static const char ppsz_broken_providers[][8] = {
3531 "CSAT", /* CanalSat FR */
3532 "GR1", /* France televisions */
3534 "MR5", /* France 2/M6 HD */
3537 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3539 const size_t i_length = strlen(ppsz_broken_providers[i]);
3540 if( pD->i_service_provider_name_length == i_length &&
3541 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3542 p_sys->b_broken_charset = true;
3545 /* FIXME: Digital+ ES also uses ISO8859-1 */
3547 str1 = EITConvertToUTF8(p_demux,
3548 pD->i_service_provider_name,
3549 pD->i_service_provider_name_length,
3550 p_sys->b_broken_charset );
3551 str2 = EITConvertToUTF8(p_demux,
3553 pD->i_service_name_length,
3554 p_sys->b_broken_charset );
3556 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3557 pD->i_service_type, str1, str2 );
3559 vlc_meta_SetTitle( p_meta, str2 );
3560 vlc_meta_SetPublisher( p_meta, str1 );
3561 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3562 psz_type = ppsz_type[pD->i_service_type];
3568 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3570 static const char *ppsz_status[5] = {
3573 "Starts in a few seconds",
3577 psz_status = ppsz_status[p_srv->i_running_status];
3581 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3583 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3585 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3586 p_srv->i_service_id, p_meta );
3587 vlc_meta_Delete( p_meta );
3590 sdt->psi->i_sdt_version = p_sdt->i_version;
3591 dvbpsi_DeleteSDT( p_sdt );
3594 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3595 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3597 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3601 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3602 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3605 /* Count the number of days */
3606 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3607 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3608 for( int i = 70; i < i_year; i++ )
3609 i_day += LEAP(1900+i);
3611 i_day += LEAP(1900+i_year);
3614 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3617 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3619 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3620 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3621 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3623 *p_y = 1900 + yp + c*1;
3624 *p_m = mp - 1 - c*12;
3625 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3627 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3628 static int64_t EITConvertStartTime( uint64_t i_date )
3630 const int i_mjd = i_date >> 24;
3631 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3632 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3633 const int i_second = CVT_FROM_BCD(i_date );
3638 /* if all 40 bits are 1, the start is unknown */
3639 if( i_date == UINT64_C(0xffffffffff) )
3642 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3643 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3645 static int EITConvertDuration( uint32_t i_duration )
3647 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3648 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3649 CVT_FROM_BCD(i_duration );
3653 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3655 demux_sys_t *p_sys = p_demux->p_sys;
3657 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3659 dvbpsi_DeleteTOT(p_tdt);
3663 static void EITCallBack( demux_t *p_demux,
3664 dvbpsi_eit_t *p_eit, bool b_current_following )
3666 demux_sys_t *p_sys = p_demux->p_sys;
3667 dvbpsi_eit_event_t *p_evt;
3670 msg_Dbg( p_demux, "EITCallBack called" );
3671 if( !p_eit->b_current_next )
3673 dvbpsi_DeleteEIT( p_eit );
3677 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3678 "ts_id=%d network_id=%d segment_last_section_number=%d "
3680 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3683 p_eit->i_service_id,
3685 p_eit->i_version, p_eit->b_current_next,
3686 p_eit->i_ts_id, p_eit->i_network_id,
3687 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3689 p_epg = vlc_epg_New( NULL );
3690 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3692 dvbpsi_descriptor_t *p_dr;
3693 char *psz_name = NULL;
3694 char *psz_text = NULL;
3695 char *psz_extra = strdup("");
3699 int64_t i_tot_time = 0;
3701 i_start = EITConvertStartTime( p_evt->i_start_time );
3702 i_duration = EITConvertDuration( p_evt->i_duration );
3704 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3706 if( p_sys->i_tdt_delta == 0 )
3707 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3709 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3711 tzset(); // JST -> UTC
3712 i_start += timezone; // FIXME: what about DST?
3713 i_tot_time += timezone;
3715 if( p_evt->i_running_status == 0x00 &&
3716 (i_start - 5 < i_tot_time &&
3717 i_tot_time < i_start + i_duration + 5) )
3719 p_evt->i_running_status = 0x04;
3720 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3724 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3725 "running=%d free_ca=%d",
3726 p_evt->i_event_id, (int)i_start, (int)i_duration,
3727 p_evt->i_running_status, p_evt->b_free_ca );
3729 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3735 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3737 /* Only take first description, as we don't handle language-info
3739 if( pE && psz_name == NULL )
3741 psz_name = EITConvertToUTF8( p_demux,
3742 pE->i_event_name, pE->i_event_name_length,
3743 p_sys->b_broken_charset );
3745 psz_text = EITConvertToUTF8( p_demux,
3746 pE->i_text, pE->i_text_length,
3747 p_sys->b_broken_charset );
3748 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3749 pE->i_iso_639_code, psz_name, psz_text );
3756 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3759 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3761 pE->i_descriptor_number, pE->i_last_descriptor_number );
3763 if( pE->i_text_length > 0 )
3765 char *psz_text = EITConvertToUTF8( p_demux,
3766 pE->i_text, pE->i_text_length,
3767 p_sys->b_broken_charset );
3770 msg_Dbg( p_demux, " - text='%s'", psz_text );
3772 psz_extra = xrealloc( psz_extra,
3773 strlen(psz_extra) + strlen(psz_text) + 1 );
3774 strcat( psz_extra, psz_text );
3779 for( int i = 0; i < pE->i_entry_count; i++ )
3781 char *psz_dsc = EITConvertToUTF8( p_demux,
3782 pE->i_item_description[i],
3783 pE->i_item_description_length[i],
3784 p_sys->b_broken_charset );
3785 char *psz_itm = EITConvertToUTF8( p_demux,
3786 pE->i_item[i], pE->i_item_length[i],
3787 p_sys->b_broken_charset );
3789 if( psz_dsc && psz_itm )
3791 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3793 psz_extra = xrealloc( psz_extra,
3794 strlen(psz_extra) + strlen(psz_dsc) +
3795 strlen(psz_itm) + 3 + 1 );
3796 strcat( psz_extra, "(" );
3797 strcat( psz_extra, psz_dsc );
3798 strcat( psz_extra, " " );
3799 strcat( psz_extra, psz_itm );
3800 strcat( psz_extra, ")" );
3812 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
3815 for ( int i = 0; i < pR->i_ratings_number; i++ )
3817 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
3818 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
3820 if ( p_rating->i_rating + 3 > i_min_age )
3821 i_min_age = p_rating->i_rating + 3;
3822 msg_Dbg( p_demux, " - parental control set to %d years",
3831 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3837 if( i_start > 0 && psz_name && psz_text)
3838 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3839 *psz_extra ? psz_extra : NULL, i_min_age );
3841 /* Update "now playing" field */
3842 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
3843 vlc_epg_SetCurrent( p_epg, i_start );
3850 if( p_epg->i_event > 0 )
3852 if( b_current_following &&
3853 ( p_sys->i_current_program == -1 ||
3854 p_sys->i_current_program ==
3855 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3862 p_sys->i_dvb_length = 0;
3863 p_sys->i_dvb_start = 0;
3865 if( p_epg->p_current )
3867 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
3868 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
3871 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
3872 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3875 p_eit->i_service_id,
3879 vlc_epg_Delete( p_epg );
3881 dvbpsi_DeleteEIT( p_eit );
3883 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3885 EITCallBack( p_demux, p_eit, true );
3887 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3889 EITCallBack( p_demux, p_eit, false );
3892 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3893 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
3894 uint16_t i_extension, demux_t *p_demux )
3896 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
3897 uint8_t i_table_id, uint16_t i_extension )
3902 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3903 i_table_id, i_table_id, i_extension, i_extension );
3905 if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
3907 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3908 i_table_id, i_table_id, i_extension, i_extension );
3909 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3910 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
3911 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
3913 dvbpsi_AttachSDT( h, i_table_id, i_extension,
3914 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
3917 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3918 ( i_table_id == 0x4e || /* Current/Following */
3919 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3921 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3922 i_table_id, i_table_id, i_extension, i_extension );
3924 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3925 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3926 (dvbpsi_eit_callback)EITCallBackSchedule;
3927 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3928 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
3929 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
3931 dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
3934 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3935 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
3937 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3938 i_table_id, i_table_id, i_extension, i_extension );
3939 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3940 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
3941 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
3943 dvbpsi_AttachTOT( h, i_table_id, i_extension,
3944 (dvbpsi_tot_callback)TDTCallBack, p_demux);
3949 /*****************************************************************************
3950 * PMT callback and helpers
3951 *****************************************************************************/
3952 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3955 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3956 while( p_dr && ( p_dr->i_tag != i_tag ) )
3957 p_dr = p_dr->p_next;
3960 static bool PMTEsHasRegistration( demux_t *p_demux,
3961 const dvbpsi_pmt_es_t *p_es,
3962 const char *psz_tag )
3964 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3968 if( p_dr->i_length < 4 )
3970 msg_Warn( p_demux, "invalid Registration Descriptor" );
3974 assert( strlen(psz_tag) == 4 );
3975 return !memcmp( p_dr->p_data, psz_tag, 4 );
3978 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
3979 int i_component_tag )
3981 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3984 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3988 return p_si->i_component_tag == i_component_tag;
3991 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3992 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3994 es_format_t *p_fmt = &pid->es->fmt;
3996 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
3997 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3999 if( p_dr && p_dr->i_length == 2 )
4001 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
4003 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
4005 pid->es->p_mpeg4desc = NULL;
4007 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
4009 iod_descriptor_t *iod = prg->iod;
4010 if( iod->es_descr[i].i_es_id == i_es_id )
4012 if ( iod->es_descr[i].b_ok )
4013 pid->es->p_mpeg4desc = &iod->es_descr[i];
4015 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
4020 if( !pid->es->p_mpeg4desc )
4022 switch( p_es->i_type )
4024 /* non fatal, set by packetizer */
4025 case 0x0f: /* ADTS */
4026 case 0x11: /* LOAS */
4027 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4028 pid->i_pid, p_es->i_type );
4031 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
4032 pid->i_pid, p_es->i_type );
4038 const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
4039 if( dcd->i_streamType == 0x04 ) /* VisualStream */
4041 p_fmt->i_cat = VIDEO_ES;
4042 switch( dcd->i_objectTypeIndication )
4044 case 0x0B: /* mpeg4 sub */
4045 p_fmt->i_cat = SPU_ES;
4046 p_fmt->i_codec = VLC_CODEC_SUBT;
4049 case 0x20: /* mpeg4 */
4050 p_fmt->i_codec = VLC_CODEC_MP4V;
4052 case 0x21: /* h264 */
4053 p_fmt->i_codec = VLC_CODEC_H264;
4060 case 0x65: /* mpeg2 */
4061 p_fmt->i_codec = VLC_CODEC_MPGV;
4063 case 0x6a: /* mpeg1 */
4064 p_fmt->i_codec = VLC_CODEC_MPGV;
4066 case 0x6c: /* mpeg1 */
4067 p_fmt->i_codec = VLC_CODEC_JPEG;
4070 p_fmt->i_cat = UNKNOWN_ES;
4074 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
4076 p_fmt->i_cat = AUDIO_ES;
4077 switch( dcd->i_objectTypeIndication )
4079 case 0x40: /* mpeg4 */
4080 p_fmt->i_codec = VLC_CODEC_MP4A;
4084 case 0x68: /* mpeg2 aac */
4085 p_fmt->i_codec = VLC_CODEC_MP4A;
4087 case 0x69: /* mpeg2 */
4088 p_fmt->i_codec = VLC_CODEC_MPGA;
4090 case 0x6b: /* mpeg1 */
4091 p_fmt->i_codec = VLC_CODEC_MPGA;
4094 p_fmt->i_cat = UNKNOWN_ES;
4100 p_fmt->i_cat = UNKNOWN_ES;
4103 if( p_fmt->i_cat != UNKNOWN_ES )
4105 p_fmt->i_extra = dcd->i_extra;
4106 if( p_fmt->i_extra > 0 )
4108 p_fmt->p_extra = malloc( p_fmt->i_extra );
4109 if( p_fmt->p_extra )
4110 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
4123 } ts_teletext_page_t;
4125 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
4126 const dvbpsi_pmt_es_t *p_es )
4128 es_format_t *p_fmt = &pid->es->fmt;
4130 ts_teletext_page_t p_page[2 * 64 + 20];
4131 unsigned i_page = 0;
4133 /* Gather pages information */
4134 #if defined _DVBPSI_DR_56_H_ && \
4135 defined DVBPSI_VERSION && DVBPSI_VERSION_INT > DVBPSI_VERSION_WANTED(0,1,5)
4136 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
4138 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 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 );
4166 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
4169 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4170 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4172 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
4174 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
4177 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
4179 ts_teletext_page_t *p_dst = &p_page[i_page++];
4181 switch( p_src->i_subtitling_type )
4184 p_dst->i_type = 0x02;
4187 p_dst->i_type = 0x03;
4190 /* FIXME check if it is the right split */
4191 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
4192 ? (p_src->i_composition_page_id >> 8) : 8;
4193 p_dst->i_page = p_src->i_composition_page_id & 0xff;
4194 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
4199 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
4201 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4203 p_fmt->subs.teletext.i_magazine = -1;
4204 p_fmt->subs.teletext.i_page = 0;
4205 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
4207 dvbpsi_descriptor_t *p_dr;
4208 p_dr = PMTEsFindDescriptor( p_es, 0x46 );
4210 p_dr = PMTEsFindDescriptor( p_es, 0x56 );
4212 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4214 /* Descriptor pass-through */
4215 p_fmt->p_extra = malloc( p_dr->i_length );
4216 if( p_fmt->p_extra )
4218 p_fmt->i_extra = p_dr->i_length;
4219 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4225 for( unsigned i = 0; i < i_page; i++ )
4236 p_es = malloc( sizeof(*p_es) );
4240 es_format_Copy( &p_es->fmt, &pid->es->fmt );
4241 free( p_es->fmt.psz_language );
4242 free( p_es->fmt.psz_description );
4243 p_es->fmt.psz_language = NULL;
4244 p_es->fmt.psz_description = NULL;
4247 p_es->p_data = NULL;
4248 p_es->i_data_size = 0;
4249 p_es->i_data_gathered = 0;
4250 p_es->pp_last = &p_es->p_data;
4251 p_es->data_type = TS_ES_DATA_PES;
4252 p_es->p_mpeg4desc = NULL;
4254 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
4258 const ts_teletext_page_t *p = &p_page[i];
4259 p_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
4260 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
4261 p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
4262 p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
4263 p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
4264 p_es->fmt.subs.teletext.i_page = p->i_page;
4267 " * ttxt type=%s lan=%s page=%d%02x",
4268 p_es->fmt.psz_description,
4269 p_es->fmt.psz_language,
4270 p->i_magazine, p->i_page );
4274 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
4275 const dvbpsi_pmt_es_t *p_es )
4277 es_format_t *p_fmt = &pid->es->fmt;
4279 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
4281 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
4283 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
4284 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
4286 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
4287 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
4288 ( i_type >= 0x20 && i_type <= 0x24 ) )
4292 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
4294 p_fmt->subs.dvb.i_id = -1;
4295 p_fmt->psz_description = strdup( _("DVB subtitles") );
4297 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
4299 /* Descriptor pass-through */
4300 p_fmt->p_extra = malloc( p_dr->i_length );
4301 if( p_fmt->p_extra )
4303 p_fmt->i_extra = p_dr->i_length;
4304 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
4310 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4321 p_es = malloc( sizeof(*p_es) );
4325 es_format_Copy( &p_es->fmt, &pid->es->fmt );
4326 free( p_es->fmt.psz_language );
4327 free( p_es->fmt.psz_description );
4328 p_es->fmt.psz_language = NULL;
4329 p_es->fmt.psz_description = NULL;
4332 p_es->p_data = NULL;
4333 p_es->i_data_size = 0;
4334 p_es->i_data_gathered = 0;
4335 p_es->pp_last = &p_es->p_data;
4336 p_es->data_type = TS_ES_DATA_PES;
4337 p_es->p_mpeg4desc = NULL;
4339 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
4343 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
4344 p_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
4345 switch( p->i_subtitling_type )
4347 case 0x10: /* unspec. */
4348 case 0x11: /* 4:3 */
4349 case 0x12: /* 16:9 */
4350 case 0x13: /* 2.21:1 */
4351 case 0x14: /* HD monitor */
4352 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
4354 case 0x20: /* Hearing impaired unspec. */
4355 case 0x21: /* h.i. 4:3 */
4356 case 0x22: /* h.i. 16:9 */
4357 case 0x23: /* h.i. 2.21:1 */
4358 case 0x24: /* h.i. HD monitor */
4359 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
4366 p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
4367 ( p->i_ancillary_page_id << 16 );
4372 static int vlc_ceil_log2( const unsigned int val )
4374 int n = 31 - clz(val);
4375 if ((1U << n) != val)
4381 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
4385 /* default mapping */
4386 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
4387 memcpy(h.stream_map, map, sizeof(map));
4391 int stream_count = 0;
4392 int ccc = p[1]; // channel_config_code
4396 mapping = channels > 2;
4399 channels = 2; // dual mono
4401 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
4402 csc = p_csc[channels - 1];
4403 stream_count = channels - csc;
4405 static const uint8_t map[6][7] = {
4414 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
4415 } else if (ccc == 0x81) {
4417 goto explicit_config_too_short;
4424 bs_init(&s, &p[4], len - 4);
4427 int bits = vlc_ceil_log2(channels);
4428 if (s.i_left < bits)
4429 goto explicit_config_too_short;
4430 stream_count = bs_read(&s, bits) + 1;
4431 bits = vlc_ceil_log2(stream_count + 1);
4432 if (s.i_left < bits)
4433 goto explicit_config_too_short;
4434 csc = bs_read(&s, bits);
4436 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
4437 if (s.i_left < channels * channel_bits)
4438 goto explicit_config_too_short;
4440 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
4441 for (int i = 0; i < channels; i++) {
4442 unsigned char m = bs_read(&s, channel_bits);
4445 h.stream_map[i] = m;
4448 } else if (ccc >= 0x80 && ccc <= 0x88) {
4449 channels = ccc - 0x80;
4454 channels = 2; // dual mono
4457 stream_count = channels;
4459 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
4463 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4467 opus_prepare_header(channels, 0, &h);
4469 h.input_sample_rate = 48000;
4471 h.nb_streams = channels - csc;
4472 h.channel_mapping = mapping;
4475 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4476 if (p_fmt->p_extra) {
4477 p_fmt->i_cat = AUDIO_ES;
4478 p_fmt->i_codec = VLC_CODEC_OPUS;
4479 p_fmt->audio.i_channels = h.channels;
4480 p_fmt->audio.i_rate = 48000;
4486 explicit_config_too_short:
4487 msg_Err(demux, "Opus descriptor too short");
4490 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
4491 const dvbpsi_pmt_es_t *p_es )
4493 es_format_t *p_fmt = &pid->es->fmt;
4494 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_es, 0x59 );
4495 dvbpsi_descriptor_t *desc;
4497 if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
4498 PMTEsFindDescriptor( p_es, 0x6a ) ||
4499 PMTEsFindDescriptor( p_es, 0x81 ) )
4501 p_fmt->i_cat = AUDIO_ES;
4502 p_fmt->i_codec = VLC_CODEC_A52;
4504 else if( (desc = PMTEsFindDescriptor( p_es, 0x7f ) ) && desc->i_length >= 2 &&
4505 PMTEsHasRegistration(p_demux, p_es, "Opus"))
4507 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4509 else if( PMTEsFindDescriptor( p_es, 0x7a ) )
4511 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4512 p_fmt->i_cat = AUDIO_ES;
4513 p_fmt->i_codec = VLC_CODEC_EAC3;
4515 else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
4516 PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
4517 PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
4518 PMTEsFindDescriptor( p_es, 0x73 ) )
4520 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4521 p_fmt->i_cat = AUDIO_ES;
4522 p_fmt->i_codec = VLC_CODEC_DTS;
4524 else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) && !p_subs_dr )
4526 /* BSSD is AES3 DATA, but could also be subtitles
4527 * we need to check for secondary descriptor then s*/
4528 p_fmt->i_cat = AUDIO_ES;
4529 p_fmt->b_packetized = true;
4530 p_fmt->i_codec = VLC_CODEC_302M;
4532 else if( PMTEsHasRegistration( p_demux, p_es, "HEVC" ) )
4534 p_fmt->i_cat = VIDEO_ES;
4535 p_fmt->i_codec = VLC_CODEC_HEVC;
4537 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4539 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4540 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xFD );
4541 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4542 if ( p_dr && p_dr->i_length >= 2 )
4544 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4545 PMTEsHasComponentTag( p_es, 0x30 ) ||
4546 PMTEsHasComponentTag( p_es, 0x31 ) ||
4547 PMTEsHasComponentTag( p_es, 0x32 ) ||
4548 PMTEsHasComponentTag( p_es, 0x33 ) ||
4549 PMTEsHasComponentTag( p_es, 0x34 ) ||
4550 PMTEsHasComponentTag( p_es, 0x35 ) ||
4551 PMTEsHasComponentTag( p_es, 0x36 ) ||
4552 PMTEsHasComponentTag( p_es, 0x37 ) ) )
4554 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_A );
4555 p_fmt->psz_language = strndup ( "jpn", 3 );
4556 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4558 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4559 PMTEsHasComponentTag( p_es, 0x87 ) ||
4560 PMTEsHasComponentTag( p_es, 0x88 ) ) )
4562 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_C );
4563 p_fmt->psz_language = strndup ( "jpn", 3 );
4564 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4570 /* Subtitle/Teletext/VBI fallbacks */
4571 dvbpsi_subtitling_dr_t *p_sub;
4572 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4574 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4576 if( p_fmt->i_cat != UNKNOWN_ES )
4579 switch( p_sub->p_subtitle[i].i_subtitling_type )
4581 case 0x01: /* EBU Teletext subtitles */
4582 case 0x02: /* Associated EBU Teletext */
4583 case 0x03: /* VBI data */
4584 PMTSetupEsTeletext( p_demux, pid, p_es );
4586 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4587 case 0x11: /* ... on 4:3 AR monitor */
4588 case 0x12: /* ... on 16:9 AR monitor */
4589 case 0x13: /* ... on 2.21:1 AR monitor */
4590 case 0x14: /* ... for display on a high definition monitor */
4591 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4592 case 0x21: /* ... on 4:3 AR monitor */
4593 case 0x22: /* ... on 16:9 AR monitor */
4594 case 0x23: /* ... on 2.21:1 AR monitor */
4595 case 0x24: /* ... for display on a high definition monitor */
4596 PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
4599 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4600 p_sub->p_subtitle[i].i_subtitling_type );
4606 if( p_fmt->i_cat == UNKNOWN_ES &&
4607 ( PMTEsFindDescriptor( p_es, 0x45 ) || /* VBI Data descriptor */
4608 PMTEsFindDescriptor( p_es, 0x46 ) || /* VBI Teletext descriptor */
4609 PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
4612 PMTSetupEsTeletext( p_demux, pid, p_es );
4616 /* FIXME is it useful ? */
4617 if( PMTEsFindDescriptor( p_es, 0x52 ) )
4619 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4620 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4622 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4626 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
4627 const dvbpsi_pmt_es_t *p_es )
4629 /* Registration Descriptor */
4630 if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
4632 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4636 es_format_t *p_fmt = &pid->es->fmt;
4638 /* registration descriptor for VC-1 (SMPTE rp227) */
4639 p_fmt->i_cat = VIDEO_ES;
4640 p_fmt->i_codec = VLC_CODEC_VC1;
4642 /* XXX With Simple and Main profile the SEQUENCE
4643 * header is modified: video width and height are
4644 * inserted just after the start code as 2 int16_t
4645 * The packetizer will take care of that. */
4648 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
4649 const dvbpsi_pmt_es_t *p_es )
4651 /* Registration Descriptor */
4652 if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
4654 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4658 es_format_t *p_fmt = &pid->es->fmt;
4660 /* registration descriptor for Dirac
4661 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4662 p_fmt->i_cat = VIDEO_ES;
4663 p_fmt->i_codec = VLC_CODEC_DIRAC;
4666 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
4667 const dvbpsi_pmt_es_t *p_es )
4669 /* MSCODEC sent by vlc */
4670 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
4671 if( !p_dr || p_dr->i_length < 10 )
4674 "private MSCODEC (vlc) without bih private descriptor" );
4678 es_format_t *p_fmt = &pid->es->fmt;
4679 p_fmt->i_cat = VIDEO_ES;
4680 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4681 p_dr->p_data[2], p_dr->p_data[3] );
4682 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4683 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4684 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4686 if( p_fmt->i_extra > 0 )
4688 p_fmt->p_extra = malloc( p_fmt->i_extra );
4689 if( p_fmt->p_extra )
4690 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4691 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4695 /* For such stream we will gather them ourself and don't launch a
4697 * Yes it's ugly but it's the only way to have DIV3 working */
4698 p_fmt->b_packetized = true;
4701 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pid_t *pid )
4703 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4704 * with audio track pid being 0x1100..0x11FF */
4705 if ( p_pmt->i_program_number == 0x1 &&
4706 p_pmt->i_pcr_pid == 0x1000 &&
4707 ( pid->i_pid >> 8 ) == 0x11 )
4709 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4710 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4713 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4716 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
4717 const dvbpsi_pmt_es_t *p_es )
4719 es_format_t *p_fmt = &pid->es->fmt;
4721 /* Blu-Ray mapping */
4722 switch( p_es->i_type )
4725 p_fmt->i_cat = AUDIO_ES;
4726 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4729 p_fmt->i_cat = AUDIO_ES;
4730 p_fmt->i_codec = VLC_CODEC_A52;
4733 case 0x85: /* DTS-HD High resolution audio */
4734 case 0x86: /* DTS-HD Master audio */
4735 case 0xA2: /* Secondary DTS audio */
4736 p_fmt->i_cat = AUDIO_ES;
4737 p_fmt->i_codec = VLC_CODEC_DTS;
4740 case 0x83: /* TrueHD AC3 */
4741 p_fmt->i_cat = AUDIO_ES;
4742 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4745 case 0x84: /* E-AC3 */
4746 case 0xA1: /* Secondary E-AC3 */
4747 p_fmt->i_cat = AUDIO_ES;
4748 p_fmt->i_codec = VLC_CODEC_EAC3;
4750 case 0x90: /* Presentation graphics */
4751 p_fmt->i_cat = SPU_ES;
4752 p_fmt->i_codec = VLC_CODEC_BD_PG;
4754 case 0x91: /* Interactive graphics */
4755 case 0x92: /* Subtitle */
4758 p_fmt->i_cat = VIDEO_ES;
4759 p_fmt->i_codec = VLC_CODEC_VC1;
4762 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4763 p_es->i_pid, p_es->i_type );
4770 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
4771 const dvbpsi_pmt_es_t *p_es )
4777 vlc_fourcc_t i_codec;
4779 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4780 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4781 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4782 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4783 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4784 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4785 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4786 { "", UNKNOWN_ES, 0 }
4788 es_format_t *p_fmt = &pid->es->fmt;
4790 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4792 if( PMTEsHasRegistration( p_demux, p_es, p_regs[i].psz_tag ) )
4794 p_fmt->i_cat = p_regs[i].i_cat;
4795 p_fmt->i_codec = p_regs[i].i_codec;
4796 if (p_es->i_type == 0x87)
4797 p_fmt->i_codec = VLC_CODEC_EAC3;
4804 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4806 static const char *audio_type[] = {
4808 N_("clean effects"),
4809 N_("hearing impaired"),
4810 N_("visual impaired commentary"),
4813 if (type < 0 || type > 3)
4814 msg_Dbg( p_demux, "unknown audio type: %d", type);
4816 return strdup(audio_type[type]);
4820 static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
4821 const dvbpsi_pmt_es_t *p_es )
4823 /* get language descriptor */
4824 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
4829 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4832 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4836 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4837 pid->es->fmt.psz_language = malloc( 4 );
4838 if( pid->es->fmt.psz_language )
4840 memcpy( pid->es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
4841 pid->es->fmt.psz_language[3] = 0;
4842 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
4844 int type = p_decoded->code[0].i_audio_type;
4845 pid->es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
4847 pid->es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
4849 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
4850 if( pid->es->fmt.i_extra_languages > 0 )
4851 pid->es->fmt.p_extra_languages =
4852 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
4853 pid->es->fmt.i_extra_languages );
4854 if( pid->es->fmt.p_extra_languages )
4856 for( unsigned i = 0; i < pid->es->fmt.i_extra_languages; i++ )
4858 pid->es->fmt.p_extra_languages[i].psz_language = malloc(4);
4859 if( pid->es->fmt.p_extra_languages[i].psz_language )
4861 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
4862 p_decoded->code[i+1].iso_639_code, 3 );
4863 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
4865 int type = p_decoded->code[i].i_audio_type;
4866 pid->es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
4870 pid->es->fmt.psz_language = malloc( 4 );
4871 if( pid->es->fmt.psz_language )
4873 memcpy( pid->es->fmt.psz_language,
4874 p_decoded->i_iso_639_code, 3 );
4875 pid->es->fmt.psz_language[3] = 0;
4880 static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid )
4882 demux_sys_t *p_sys = p_demux->p_sys;
4883 bool b_create_delayed = false;
4887 if( pid->b_seen && p_sys->b_delay_es_creation )
4889 p_sys->b_delay_es_creation = false;
4890 b_create_delayed = true;
4893 if( !p_sys->b_delay_es_creation )
4895 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4896 for( int i = 0; i < pid->i_extra_es; i++ )
4898 pid->extra_es[i]->id =
4899 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
4901 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4904 else if( p_sys->b_delay_es_creation )
4906 p_sys->b_delay_es_creation = false;
4907 b_create_delayed = true;
4910 if( b_create_delayed )
4912 for(int i=MIN_ES_PID; i<=MAX_ES_PID; i++)
4914 pid = &p_sys->pid[i];
4915 if(!pid->es || pid->es->id)
4917 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4918 for( int j = 0; j < pid->i_extra_es; j++ )
4920 pid->extra_es[j]->id =
4921 es_out_Add( p_demux->out, &pid->extra_es[j]->fmt );
4923 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4928 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
4930 demux_t *p_demux = data;
4931 demux_sys_t *p_sys = p_demux->p_sys;
4933 ts_pid_t *pmt = NULL;
4936 msg_Dbg( p_demux, "PMTCallBack called" );
4938 /* First find this PMT declared in PAT */
4939 for( int i = 0; !pmt && i < p_sys->i_pmt; i++ )
4940 for( int i_prg = 0; !pmt && i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
4942 const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
4943 if( i_pmt_number != TS_USER_PMT_NUMBER &&
4944 i_pmt_number == p_pmt->i_program_number )
4946 pmt = p_sys->pmt[i];
4947 prg = p_sys->pmt[i]->psi->prg[i_prg];
4953 msg_Warn( p_demux, "unreferenced program (broken stream)" );
4954 dvbpsi_DeletePMT(p_pmt);
4959 if( prg->i_version != -1 &&
4960 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
4962 dvbpsi_DeletePMT( p_pmt );
4966 ts_pid_t **pp_clean = NULL;
4968 /* Clean this program (remove all es) */
4969 for( int i = 0; i < 8192; i++ )
4971 ts_pid_t *pid = &p_sys->pid[i];
4973 if( pid->b_valid && pid->p_owner == pmt->psi &&
4974 pid->i_owner_number == prg->i_number && pid->psi == NULL )
4976 TAB_APPEND( i_clean, pp_clean, pid );
4981 IODFree( prg->iod );
4985 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
4986 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
4987 prg->i_pid_pcr = p_pmt->i_pcr_pid;
4988 prg->i_version = p_pmt->i_version;
4990 ValidateDVBMeta( p_demux, prg->i_pid_pcr );
4991 if( ProgramIsSelected( p_demux, prg->i_number ) )
4992 SetPIDFilter( p_demux, prg->i_pid_pcr, true ); /* Set demux filter */
4994 /* Parse PMT descriptors */
4995 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
4996 dvbpsi_descriptor_t *p_dr;
4998 /* First pass for standard detection */
4999 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
5001 int i_arib_flags = 0; /* Descriptors can be repeated */
5002 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5008 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
5009 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
5013 i_arib_flags |= 1 << 1;
5016 i_arib_flags |= 1 << 2;
5022 if ( i_arib_flags == 0x07 ) //0b111
5023 p_sys->arib.e_mode = ARIBMODE_ENABLED;
5026 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
5028 /* special descriptors handling */
5031 case 0x1d: /* We have found an IOD descriptor */
5032 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
5033 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
5037 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5038 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5041 case 0x5: /* Registration Descriptor */
5042 if( p_dr->i_length != 4 )
5044 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
5048 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
5049 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
5050 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
5055 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
5059 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
5062 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
5063 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
5064 /* TODO: apply visibility flags */
5068 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
5071 dvbpsi_pmt_es_t *p_es;
5072 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
5074 ts_pid_t tmp_pid = {0}, *old_pid = {0}, *pid = &tmp_pid;
5076 /* Find out if the PID was already declared */
5077 for( int i = 0; i < i_clean; i++ )
5079 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
5081 old_pid = pp_clean[i];
5085 ValidateDVBMeta( p_demux, p_es->i_pid );
5087 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
5089 msg_Warn( p_demux, " * PMT error: pid=%d already defined",
5094 char const * psz_typedesc = "";
5095 switch(p_es->i_type)
5098 psz_typedesc = "ISO/IEC Reserved";
5101 psz_typedesc = "ISO/IEC 11172 Video";
5104 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
5107 psz_typedesc = "ISO/IEC 11172 Audio";
5110 psz_typedesc = "ISO/IEC 13818-3 Audio";
5113 psz_typedesc = "ISO/IEC 13818-1 private_sections";
5116 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
5119 psz_typedesc = "ISO/IEC 13522 MHEG";
5122 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
5125 psz_typedesc = "ITU-T Rec. H.222.1";
5128 psz_typedesc = "ISO/IEC 13818-6 type A";
5131 psz_typedesc = "ISO/IEC 13818-6 type B";
5134 psz_typedesc = "ISO/IEC 13818-6 type C";
5137 psz_typedesc = "ISO/IEC 13818-6 type D";
5140 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
5143 if (p_es->i_type >= 0x0F && p_es->i_type <=0x7F)
5144 psz_typedesc = "ISO/IEC 13818-1 Reserved";
5146 psz_typedesc = "User Private";
5149 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
5150 p_es->i_pid, p_es->i_type, psz_typedesc );
5152 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
5153 p_dr = p_dr->p_next )
5155 msg_Dbg( p_demux, " - descriptor tag 0x%x",
5159 PIDInit( pid, false, pmt->psi );
5160 PIDFillFormat( &pid->es->fmt, p_es->i_type );
5161 pid->i_owner_number = prg->i_number;
5162 pid->i_pid = p_es->i_pid;
5163 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
5166 bool b_registration_applied = false;
5167 if ( p_es->i_type >= 0x80 ) /* non standard, extensions */
5169 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
5171 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, pid, p_es ) ))
5172 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
5173 p_es->i_pid, p_es->i_type );
5177 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, pid, p_es ) ))
5178 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
5179 p_es->i_pid, p_es->i_type );
5183 if ( !b_registration_applied )
5185 switch( p_es->i_type )
5188 /* Handle PES private data */
5189 PMTSetupEs0x06( p_demux, pid, p_es );
5191 /* All other private or reserved types */
5196 PMTSetupEsISO14496( p_demux, pid, prg, p_es );
5200 PMTSetupEs0x83( p_pmt, pid );
5203 PMTSetupEs0xA0( p_demux, pid, p_es );
5206 PMTSetupEs0xD1( p_demux, pid, p_es );
5209 PMTSetupEs0xEA( p_demux, pid, p_es );
5215 if( pid->es->fmt.i_cat == AUDIO_ES ||
5216 ( pid->es->fmt.i_cat == SPU_ES &&
5217 pid->es->fmt.i_codec != VLC_CODEC_DVBS &&
5218 pid->es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
5220 PMTParseEsIso639( p_demux, pid, p_es );
5223 switch( pid->es->fmt.i_codec )
5225 case VLC_CODEC_SCTE_27:
5226 pid->es->data_type = TS_ES_DATA_TABLE_SECTION;
5229 //pid->es->data_type = TS_ES_DATA_PES;
5233 pid->es->fmt.i_group = p_pmt->i_program_number;
5234 for( int i = 0; i < pid->i_extra_es; i++ )
5235 pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
5237 if( pid->es->fmt.i_cat == UNKNOWN_ES )
5239 msg_Dbg( p_demux, " => pid %d content is *unknown*",
5244 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
5245 p_es->i_pid, (char*)&pid->es->fmt.i_codec );
5247 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
5249 /* Check if we can avoid restarting the ES */
5251 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
5252 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
5253 pid->es->fmt.i_extra == 0 &&
5254 pid->i_extra_es == old_pid->i_extra_es &&
5255 ( ( !pid->es->fmt.psz_language &&
5256 !old_pid->es->fmt.psz_language ) ||
5257 ( pid->es->fmt.psz_language &&
5258 old_pid->es->fmt.psz_language &&
5259 !strcmp( pid->es->fmt.psz_language,
5260 old_pid->es->fmt.psz_language ) ) ) )
5262 pid->i_cc = old_pid->i_cc;
5263 ts_es_t *e = pid->es;
5264 pid->es = old_pid->es;
5266 for( int i = 0; i < pid->i_extra_es; i++ )
5268 e = pid->extra_es[i];
5269 pid->extra_es[i] = old_pid->extra_es[i];
5270 old_pid->extra_es[i] = e;
5275 AddAndCreateES( p_demux, pid );
5279 /* Add ES to the list */
5282 PIDClean( p_demux, old_pid );
5283 TAB_REMOVE( i_clean, pp_clean, old_pid );
5285 p_sys->pid[p_es->i_pid] = *pid;
5287 p_dr = PMTEsFindDescriptor( p_es, 0x09 );
5288 if( p_dr && p_dr->i_length >= 2 )
5290 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
5291 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
5294 if( ProgramIsSelected( p_demux, prg->i_number ) && pid->es->id != NULL )
5295 SetPIDFilter( p_demux, p_es->i_pid, true ); /* Set demux filter */
5298 /* Set CAM descrambling */
5299 if( !ProgramIsSelected( p_demux, prg->i_number ) )
5301 dvbpsi_DeletePMT( p_pmt );
5303 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
5304 p_pmt ) != VLC_SUCCESS )
5306 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED )
5308 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
5309 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
5310 if (!p_sys->arib.b25stream)
5311 dvbpsi_DeletePMT( p_pmt );
5312 } else dvbpsi_DeletePMT( p_pmt );
5315 for( int i = 0; i < i_clean; i++ )
5317 if( ProgramIsSelected( p_demux, prg->i_number ) )
5318 SetPIDFilter( p_demux, pp_clean[i]->i_pid, false );
5320 PIDClean( p_demux, pp_clean[i] );
5326 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
5328 /* Some Dreambox streams have all PMT set to same pid */
5329 int i_prev_pid = -1;
5330 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
5332 p_program = p_program->p_next )
5334 if( p_program->i_pid == i_prev_pid )
5336 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
5337 return VLC_EGENERIC;
5339 i_prev_pid = p_program->i_pid;
5344 static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
5346 demux_t *p_demux = data;
5347 demux_sys_t *p_sys = p_demux->p_sys;
5348 dvbpsi_pat_program_t *p_program;
5349 ts_pid_t *pat = &p_sys->pid[0];
5351 msg_Dbg( p_demux, "PATCallBack called" );
5353 if( ( pat->psi->i_pat_version != -1 &&
5354 ( !p_pat->b_current_next ||
5355 p_pat->i_version == pat->psi->i_pat_version ) ) ||
5356 p_sys->b_user_pmt || PATCheck( p_demux, p_pat ) )
5358 dvbpsi_DeletePAT( p_pat );
5362 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
5363 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
5366 if( p_sys->i_pmt > 0 )
5369 ts_pid_t **pmt_rm = NULL;
5371 /* Search pmt to be deleted */
5372 for( int i = 0; i < p_sys->i_pmt; i++ )
5374 ts_pid_t *pmt = p_sys->pmt[i];
5375 bool b_keep = false;
5377 for( p_program = p_pat->p_first_program; !b_keep && p_program;
5378 p_program = p_program->p_next )
5380 if( p_program->i_pid != pmt->i_pid )
5383 for( int i_prg = 0; !b_keep && i_prg < pmt->psi->i_prg; i_prg++ )
5384 if( p_program->i_number == pmt->psi->prg[i_prg]->i_number )
5391 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
5394 /* Delete all ES attached to thoses PMT */
5395 for( int i = 2; i < 8192; i++ )
5397 ts_pid_t *pid = &p_sys->pid[i];
5399 if( !pid->b_valid || pid->psi )
5402 for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
5404 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
5406 /* We only remove es that aren't defined by extra pmt */
5407 if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
5411 SetPIDFilter( p_demux, i, false );
5413 PIDClean( p_demux, pid );
5419 /* Delete PMT pid */
5420 for( int i = 0; i < i_pmt_rm; i++ )
5422 ts_pid_t *pid = pmt_rm[i];
5423 SetPIDFilter( p_demux, pid->i_pid, false );
5425 for( int i_prg = 0; i_prg < pid->psi->i_prg; i_prg++ )
5427 const int i_number = pid->psi->prg[i_prg]->i_number;
5428 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
5431 PIDClean( p_demux, &p_sys->pid[pid->i_pid] );
5432 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
5438 /* now create programs */
5439 for( p_program = p_pat->p_first_program; p_program != NULL;
5440 p_program = p_program->p_next )
5442 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
5444 if( p_program->i_number == 0 )
5447 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
5449 ValidateDVBMeta( p_demux, p_program->i_pid );
5454 for( int i_prg = 0; b_add && i_prg < pmt->psi->i_prg; i_prg++ )
5455 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
5463 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
5466 PIDInit( pmt, true, pat->psi );
5467 ts_prg_psi_t *prg = pmt->psi->prg[pmt->psi->i_prg-1];
5468 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
5469 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
5472 dvbpsi_DeletePAT( p_pat );
5475 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
5476 if( !dvbpsi_pmt_attach( prg->handle, p_program->i_number, PMTCallBack, p_demux ) )
5477 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
5478 p_program->i_number );
5480 prg->handle = dvbpsi_AttachPMT( p_program->i_number, PMTCallBack, p_demux );
5482 prg->i_number = p_program->i_number;
5483 prg->i_pid_pmt = p_program->i_pid;
5485 /* Now select PID at access level */
5486 if( ProgramIsSelected( p_demux, p_program->i_number ) )
5488 if( p_sys->i_current_program == 0 )
5489 p_sys->i_current_program = p_program->i_number;
5491 if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
5492 p_sys->b_access_control = false;
5495 pat->psi->i_pat_version = p_pat->i_version;
5497 dvbpsi_DeletePAT( p_pat );