1 /*****************************************************************************
2 * ts_psi.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2016 VLC authors and VideoLAN
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by
8 * the Free Software Foundation; either version 2.1 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *****************************************************************************/
23 #include <vlc_common.h>
25 #ifndef _DVBPSI_DVBPSI_H_
26 # include <dvbpsi/dvbpsi.h>
29 #include <dvbpsi/descriptor.h>
30 #include <dvbpsi/pat.h>
31 #include <dvbpsi/pmt.h>
32 #include <dvbpsi/dr.h>
34 #include <vlc_demux.h>
37 #include "ts_streams.h"
40 #include "ts_streams_private.h"
43 #include "ts_strings.h"
45 #include "timestamps.h"
47 #include "../../codec/jpeg2000.h"
48 #include "../../codec/opus_header.h"
49 #include "../../packetizer/dts_header.h"
56 #include "ts_metadata.h"
57 #include "ts_descriptions.h"
59 #include "../access/dtv/en50221_capmt.h"
63 static void PIDFillFormat( demux_t
*, ts_stream_t
*p_pes
, int i_stream_type
, ts_transport_type_t
* );
64 static void PMTCallBack( void *data
, dvbpsi_pmt_t
*p_dvbpsipmt
);
65 static ts_standards_e
ProbePMTStandard( const dvbpsi_pmt_t
*p_dvbpsipmt
);
67 static int PATCheck( demux_t
*p_demux
, dvbpsi_pat_t
*p_pat
)
69 /* Some Dreambox streams have all PMT set to same pid */
71 for( dvbpsi_pat_program_t
* p_program
= p_pat
->p_first_program
;
73 p_program
= p_program
->p_next
)
75 if( p_program
->i_pid
== i_prev_pid
)
77 msg_Warn( p_demux
, "PAT check failed: duplicate program pid %d", i_prev_pid
);
80 i_prev_pid
= p_program
->i_pid
;
85 static void PATCallBack( void *data
, dvbpsi_pat_t
*p_dvbpsipat
)
87 demux_t
*p_demux
= data
;
88 demux_sys_t
*p_sys
= p_demux
->p_sys
;
89 dvbpsi_pat_program_t
*p_program
;
90 ts_pid_t
*patpid
= GetPID(p_sys
, 0);
91 ts_pat_t
*p_pat
= GetPID(p_sys
, 0)->u
.p_pat
;
93 patpid
->i_flags
|= FLAG_SEEN
;
95 msg_Dbg( p_demux
, "PATCallBack called" );
97 if(unlikely( GetPID(p_sys
, 0)->type
!= TYPE_PAT
))
99 msg_Warn( p_demux
, "PATCallBack called on invalid pid" );
103 /* check versioning changes */
104 if( !p_pat
->b_generated
)
106 /* override hotfixes */
107 if( ( p_pat
->i_version
!= -1 && p_dvbpsipat
->i_version
== p_pat
->i_version
) ||
108 ( p_pat
->i_ts_id
!= -1 && p_dvbpsipat
->i_ts_id
!= p_pat
->i_ts_id
) )
110 dvbpsi_pat_delete( p_dvbpsipat
);
114 else msg_Warn( p_demux
, "Replacing generated PAT with one received from stream" );
117 if( !p_dvbpsipat
->b_current_next
|| p_sys
->b_user_pmt
||
118 PATCheck( p_demux
, p_dvbpsipat
) )
120 dvbpsi_pat_delete( p_dvbpsipat
);
124 msg_Dbg( p_demux
, "new PAT ts_id=%d version=%d current_next=%d",
125 p_dvbpsipat
->i_ts_id
, p_dvbpsipat
->i_version
, p_dvbpsipat
->b_current_next
);
127 /* Save old programs array */
128 DECL_ARRAY(ts_pid_t
*) old_pmt_rm
;
129 old_pmt_rm
.i_alloc
= p_pat
->programs
.i_alloc
;
130 old_pmt_rm
.i_size
= p_pat
->programs
.i_size
;
131 old_pmt_rm
.p_elems
= p_pat
->programs
.p_elems
;
132 ARRAY_INIT(p_pat
->programs
);
134 bool b_force_reselect
= false;
135 if( p_sys
->programs
.i_size
&& p_sys
->seltype
== PROGRAM_AUTO_DEFAULT
)
137 /* If the program was set by default selection, we'll need to repick */
138 b_force_reselect
= true;
139 for( p_program
= p_dvbpsipat
->p_first_program
; p_program
!= NULL
;
140 p_program
= p_program
->p_next
)
142 if( p_sys
->programs
.p_elems
[0] == p_program
->i_number
)
144 b_force_reselect
= false;
148 if( b_force_reselect
)
149 ARRAY_RESET( p_sys
->programs
);
152 /* now create programs */
153 for( p_program
= p_dvbpsipat
->p_first_program
; p_program
!= NULL
;
154 p_program
= p_program
->p_next
)
156 msg_Dbg( p_demux
, " * number=%d pid=%d", p_program
->i_number
,
158 if( p_program
->i_number
== 0 )
161 ts_pid_t
*pmtpid
= GetPID(p_sys
, p_program
->i_pid
);
163 bool b_existing
= (pmtpid
->type
== TYPE_PMT
);
164 /* create or temporary incref pid */
165 if( !PIDSetup( p_demux
, TYPE_PMT
, pmtpid
, patpid
) )
167 msg_Warn( p_demux
, " * number=%d pid=%d (ignored)", p_program
->i_number
,
172 if( !b_existing
|| pmtpid
->u
.p_pmt
->i_number
!= p_program
->i_number
)
174 if( b_existing
&& pmtpid
->u
.p_pmt
->i_number
!= p_program
->i_number
)
175 dvbpsi_pmt_detach(pmtpid
->u
.p_pmt
->handle
);
177 if( !dvbpsi_pmt_attach( pmtpid
->u
.p_pmt
->handle
, p_program
->i_number
, PMTCallBack
, p_demux
) )
178 msg_Err( p_demux
, "PATCallback failed attaching PMTCallback to program %d",
179 p_program
->i_number
);
182 pmtpid
->u
.p_pmt
->i_number
= p_program
->i_number
;
184 ARRAY_APPEND( p_pat
->programs
, pmtpid
);
186 /* Now select PID at access level */
187 if( p_sys
->programs
.i_size
== 0 ||
188 ProgramIsSelected( p_sys
, p_program
->i_number
) )
190 if( p_sys
->programs
.i_size
== 0 )
192 msg_Dbg( p_demux
, "temporary receiving program %d", p_program
->i_number
);
193 p_sys
->b_default_selection
= true;
194 ARRAY_APPEND( p_sys
->programs
, p_program
->i_number
);
197 SetPIDFilter( p_sys
, pmtpid
, true );
199 if ( p_sys
->es_creation
== DELAY_ES
)
200 p_sys
->es_creation
= CREATE_ES
;
203 p_pat
->i_version
= p_dvbpsipat
->i_version
;
204 p_pat
->i_ts_id
= p_dvbpsipat
->i_ts_id
;
205 p_pat
->b_generated
= false;
207 for(int i
=0; i
<old_pmt_rm
.i_size
; i
++)
209 /* decref current or release now unreferenced */
210 PIDRelease( p_demux
, old_pmt_rm
.p_elems
[i
] );
212 ARRAY_RESET(old_pmt_rm
);
214 if( b_force_reselect
&& p_sys
->programs
.i_size
)
216 es_out_Control( p_demux
->out
, ES_OUT_SET_GROUP
, p_sys
->programs
.p_elems
[0] );
219 dvbpsi_pat_delete( p_dvbpsipat
);
222 #define PMT_DESC_PREFIX " * PMT descriptor: "
223 #define PMT_DESC_INDENT " : "
224 static void ParsePMTRegistrations( demux_t
*p_demux
, const dvbpsi_descriptor_t
*p_firstdr
,
225 ts_pmt_t
*p_pmt
, ts_pmt_registration_type_t
*p_registration_type
)
227 demux_sys_t
*p_sys
= p_demux
->p_sys
;
228 ts_pmt_registration_type_t registration_type
= *p_registration_type
;
229 int i_arib_score_flags
= 0; /* Descriptors can be repeated */
231 for( const dvbpsi_descriptor_t
*p_dr
= p_firstdr
; p_dr
!= NULL
; p_dr
= p_dr
->p_next
)
233 /* general descriptors handling < 0x40 and scoring */
234 if( p_dr
->i_tag
< 0x40 )
236 msg_Dbg( p_demux
, PMT_DESC_PREFIX
"%s (0x%x)",
237 ISO13818_1_Get_Descriptor_Description(p_dr
->i_tag
), p_dr
->i_tag
);
242 case 0x05: /* Registration Descriptor */
244 if( p_dr
->i_length
!= 4 )
246 msg_Warn( p_demux
, PMT_DESC_INDENT
"invalid registration descriptor" );
253 const ts_pmt_registration_type_t reg
;
255 { { 'H', 'D', 'M', 'V' }, TS_PMT_REGISTRATION_BLURAY
},
256 { { 'H', 'D', 'P', 'R' }, TS_PMT_REGISTRATION_BLURAY
},
257 { { 'G', 'A', '9', '4' }, TS_PMT_REGISTRATION_ATSC
},
260 for( unsigned i
=0; i
<ARRAY_SIZE(regs
); i
++ )
262 if( !memcmp( regs
[i
].rgs
, p_dr
->p_data
, 4 ) )
264 registration_type
= regs
[i
].reg
;
265 msg_Dbg( p_demux
, PMT_DESC_INDENT
"%4.4s registration", p_dr
->p_data
);
274 dvbpsi_ca_dr_t
*p_cadr
= dvbpsi_DecodeCADr( (dvbpsi_descriptor_t
*) p_dr
);
275 msg_Dbg( p_demux
, PMT_DESC_INDENT
"CA System ID 0x%x", p_cadr
->i_ca_system_id
);
276 i_arib_score_flags
|= (p_cadr
->i_ca_system_id
== 0x05);
280 case 0x1d: /* We have found an IOD descriptor */
281 p_pmt
->iod
= IODNew( VLC_OBJECT(p_demux
), p_dr
->i_length
, p_dr
->p_data
);
285 i_arib_score_flags
|= 1 << 2;
289 i_arib_score_flags
|= 1 << 1;
297 if ( p_sys
->standard
== TS_STANDARD_AUTO
&&
298 registration_type
== TS_PMT_REGISTRATION_NONE
&&
299 i_arib_score_flags
== 0x07 ) //0b111
301 registration_type
= TS_PMT_REGISTRATION_ARIB
;
304 *p_registration_type
= registration_type
;
307 static void ParsePMTPrivateRegistrations( demux_t
*p_demux
, const dvbpsi_descriptor_t
*p_firstdr
,
308 ts_pmt_t
*p_pmt
, ts_standards_e i_standard
)
311 /* Now process private descriptors >= 0x40 */
312 for( const dvbpsi_descriptor_t
*p_dr
= p_firstdr
; p_dr
!= NULL
; p_dr
= p_dr
->p_next
)
314 if( p_dr
->i_tag
< 0x40 )
319 case TS_STANDARD_ARIB
:
321 const char *psz_desc
= ARIB_B10_Get_PMT_Descriptor_Description( p_dr
->i_tag
);
323 msg_Dbg( p_demux
, PMT_DESC_PREFIX
"%s (0x%x)", psz_desc
, p_dr
->i_tag
);
325 msg_Dbg( p_demux
, PMT_DESC_PREFIX
"Unknown Private (0x%x)", p_dr
->i_tag
);
329 case TS_STANDARD_DVB
:
330 case TS_STANDARD_AUTO
:
332 if( p_dr
->i_tag
== 0x88 )
334 /* EACEM Simulcast HD Logical channels ordering */
335 /* TODO: apply visibility flags */
336 msg_Dbg( p_demux
, PMT_DESC_PREFIX
"EACEM Simulcast HD" );
342 msg_Dbg( p_demux
, PMT_DESC_PREFIX
"Unknown Private (0x%x)", p_dr
->i_tag
);
348 /*****************************************************************************
349 * PMT callback and helpers
350 *****************************************************************************/
351 static dvbpsi_descriptor_t
*PMTEsFindDescriptor( const dvbpsi_pmt_es_t
*p_es
,
354 dvbpsi_descriptor_t
*p_dr
= p_es
->p_first_descriptor
;;
355 while( p_dr
&& ( p_dr
->i_tag
!= i_tag
) )
359 static bool PMTEsHasRegistration( demux_t
*p_demux
,
360 const dvbpsi_pmt_es_t
*p_es
,
361 const char *psz_tag
)
363 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_es
, 0x05 );
367 if( p_dr
->i_length
< 4 )
369 msg_Warn( p_demux
, "invalid Registration Descriptor" );
373 assert( strlen(psz_tag
) == 4 );
374 return !memcmp( p_dr
->p_data
, psz_tag
, 4 );
377 static bool PMTEsHasComponentTagBetween( const dvbpsi_pmt_es_t
*p_es
,
378 uint8_t i_low
, uint8_t i_high
)
380 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_es
, 0x52 );
383 dvbpsi_stream_identifier_dr_t
*p_si
= dvbpsi_DecodeStreamIdentifierDr( p_dr
);
387 return p_si
->i_component_tag
>= i_low
&& p_si
->i_component_tag
<= i_high
;
390 static ts_standards_e
ProbePMTStandard( const dvbpsi_pmt_t
*p_dvbpsipmt
)
392 dvbpsi_pmt_es_t
*p_dvbpsies
;
393 for( p_dvbpsies
= p_dvbpsipmt
->p_first_es
; p_dvbpsies
; p_dvbpsies
= p_dvbpsies
->p_next
)
395 if( p_dvbpsies
->i_type
== 0x06 )
397 /* Probe for ARIB subtitles */
398 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0xFD );
399 if( p_dr
&& p_dr
->i_length
>= 2 )
401 const uint16_t i_data_component_id
= GetWBE(p_dr
->p_data
);
402 if( ( i_data_component_id
== 0x0008 &&
403 PMTEsHasComponentTagBetween( p_dvbpsies
, 0x30, 0x37 ) ) ||
404 ( i_data_component_id
== 0x0012 &&
405 PMTEsHasComponentTagBetween( p_dvbpsies
, 0x87, 0x88 ) ) )
406 return TS_STANDARD_ARIB
;
410 return TS_STANDARD_AUTO
;
413 static void SetupAudioExtendedDescriptors( demux_t
*p_demux
, ts_es_t
*p_es
,
414 const dvbpsi_pmt_es_t
*p_dvbpsies
)
416 demux_sys_t
*p_sys
= p_demux
->p_sys
;
418 if( p_sys
->standard
== TS_STANDARD_AUTO
||
419 p_sys
->standard
== TS_STANDARD_DVB
)
421 const dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x7F );
422 if( p_dr
&& p_dr
->i_length
> 1 && p_dr
->p_data
[0] == 0x06 /* Tag extension */ )
424 static const char *editorial_classification_coding
[] = {
426 DESC_AUDIO_DESC_VISUALLY_IMPAIRED
,
427 DESC_CLEAN_AUDIO_HEARING_IMPAIRED
,
428 DESC_SPOKEN_SUBTITLES_VISUAL_IMP
,
431 uint8_t i_audio_type
= (p_dr
->p_data
[1] & 0x7F) >> 2;
433 if( i_audio_type
< ARRAY_SIZE(editorial_classification_coding
) )
435 free( p_es
->fmt
.psz_description
);
436 p_es
->fmt
.psz_description
= strdup(editorial_classification_coding
[i_audio_type
]);
439 if( i_audio_type
== 0x00 /* Main Audio */ )
440 p_es
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
+ 1;
442 if( (p_dr
->p_data
[1] & 0x80) == 0x00 ) /* Split mixed */
443 p_es
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
445 if( (p_dr
->p_data
[1] & 0x01) && p_dr
->i_length
>= 5 )
447 free( p_es
->fmt
.psz_language
);
448 p_es
->fmt
.psz_language
= malloc( 4 );
449 if( p_es
->fmt
.psz_language
)
451 memcpy( p_es
->fmt
.psz_language
, &p_dr
->p_data
[2], 3 );
452 p_es
->fmt
.psz_language
[3] = 0;
453 msg_Dbg( p_demux
, " found language: %s", p_es
->fmt
.psz_language
);
460 static char *GetIso639AudioTypeDesc( uint8_t type
)
462 static const char *audio_type
[] = {
465 DESC_CLEAN_AUDIO_HEARING_IMPAIRED
,
466 DESC_VISUAL_IMPAIRED_COMMENTS
,
469 if ( type
== 0 || type
>= ARRAY_SIZE(audio_type
) )
472 return strdup( audio_type
[ --type
] );
475 static void SetupISO639Descriptor( demux_t
*p_demux
, ts_es_t
*p_es
,
476 dvbpsi_descriptor_t
*p_dr
)
478 dvbpsi_iso639_dr_t
*p_decoded
= dvbpsi_DecodeISO639Dr( p_dr
);
481 msg_Err( p_demux
, " Failed to decode a ISO 639 descriptor" );
485 if( !p_es
->fmt
.psz_language
)
487 p_es
->fmt
.psz_language
= malloc( 4 );
488 if( p_es
->fmt
.psz_language
)
490 memcpy( p_es
->fmt
.psz_language
, p_decoded
->code
[0].iso_639_code
, 3 );
491 p_es
->fmt
.psz_language
[3] = 0;
492 msg_Dbg( p_demux
, " found language: %s", p_es
->fmt
.psz_language
);
496 uint8_t type
= p_decoded
->code
[0].i_audio_type
;
497 if( !p_es
->fmt
.psz_description
)
498 p_es
->fmt
.psz_description
= GetIso639AudioTypeDesc( type
);
499 if (type
== 0x00) /* Undefined */
500 p_es
->fmt
.i_priority
= ES_PRIORITY_SELECTABLE_MIN
+ 1; // prioritize normal audio tracks
502 if( p_es
->fmt
.p_extra_languages
)
505 p_es
->fmt
.i_extra_languages
= p_decoded
->i_code_count
-1;
506 if( p_es
->fmt
.i_extra_languages
> 0 )
507 p_es
->fmt
.p_extra_languages
=
508 malloc( sizeof(*p_es
->fmt
.p_extra_languages
) *
509 p_es
->fmt
.i_extra_languages
);
510 if( p_es
->fmt
.p_extra_languages
)
512 for( unsigned i
= 0; i
< p_es
->fmt
.i_extra_languages
; i
++ )
514 extra_languages_t
*p_lang
= &p_es
->fmt
.p_extra_languages
[i
];
515 if( (p_lang
->psz_language
= malloc(4)) )
517 memcpy( p_lang
->psz_language
, p_decoded
->code
[i
+1].iso_639_code
, 3 );
518 p_lang
->psz_language
[3] = '\0';
520 p_lang
->psz_description
= GetIso639AudioTypeDesc( p_decoded
->code
[i
].i_audio_type
);
525 static void SetupStandardESDescriptors( demux_t
*p_demux
, ts_es_t
*p_es
,
526 const dvbpsi_pmt_es_t
*p_dvbpsies
)
528 for( dvbpsi_descriptor_t
*p_dr
= p_dvbpsies
->p_first_descriptor
;
529 p_dr
; p_dr
= p_dr
->p_next
)
531 switch( p_dr
->i_tag
)
534 if( p_es
->fmt
.i_cat
== AUDIO_ES
)
535 SetupISO639Descriptor( p_demux
, p_es
, p_dr
);
540 dvbpsi_max_bitrate_dr_t
*p_btdr
= dvbpsi_DecodeMaxBitrateDr( p_dr
);
541 if( p_btdr
&& !p_es
->fmt
.i_bitrate
)
542 p_es
->fmt
.i_bitrate
= p_btdr
->i_max_bitrate
* 50 * 8;
549 static void SetupISO14496Descriptors( demux_t
*p_demux
, ts_stream_t
*p_pes
,
550 const ts_pmt_t
*p_pmt
, const dvbpsi_pmt_es_t
*p_dvbpsies
)
552 const dvbpsi_descriptor_t
*p_dr
= p_dvbpsies
->p_first_descriptor
;
553 ts_es_t
*p_es
= p_pes
->p_es
;
557 uint8_t i_length
= p_dr
->i_length
;
559 switch( p_dr
->i_tag
)
561 case 0x1f: /* FMC Descriptor */
562 while( i_length
>= 2 /* see below */ && !p_es
->i_sl_es_id
)
564 p_es
->i_sl_es_id
= ( p_dr
->p_data
[0] << 8 ) | p_dr
->p_data
[1];
565 /* FIXME: map all ids and flexmux channels */
566 /* Handle broken streams with 2 byte 0x1F descriptors
567 * see samples/A-codecs/AAC/freetv_aac_latm.txt */
571 msg_Dbg( p_demux
, " - found FMC_descriptor mapping es_id=%"PRIu16
, p_es
->i_sl_es_id
);
574 case 0x1e: /* SL Descriptor */
577 p_es
->i_sl_es_id
= ( p_dr
->p_data
[0] << 8 ) | p_dr
->p_data
[1];
578 msg_Dbg( p_demux
, " - found SL_descriptor mapping es_id=%"PRIu16
, p_es
->i_sl_es_id
);
580 if( p_dvbpsies
->i_type
== 0x12 ) /* SL AU pes stream */
583 p_pes
->p_proc
= SL_stream_processor_New( p_pes
);
585 else if( p_dvbpsies
->i_type
== 0x13 ) /* IOD / SL sections */
587 ts_sections_processor_Add( p_demux
,
588 &p_pes
->p_sections_proc
, 0x05, 0x00,
589 SLPackets_Section_Handler
, p_pes
);
591 p_pes
->b_always_receive
= true;
601 if( p_es
->i_sl_es_id
)
603 const es_mpeg4_descriptor_t
*p_mpeg4desc
= GetMPEG4DescByEsId( p_pmt
, p_es
->i_sl_es_id
);
604 if( p_mpeg4desc
&& p_mpeg4desc
->b_ok
)
606 if( !SetupISO14496LogicalStream( p_demux
, &p_mpeg4desc
->dec_descr
, &p_es
->fmt
) )
607 msg_Dbg( p_demux
, " - IOD not yet available for es_id=%"PRIu16
, p_es
->i_sl_es_id
);
612 switch( p_dvbpsies
->i_type
)
614 /* non fatal, set by packetizer */
615 case 0x0f: /* ADTS */
616 case 0x11: /* LOAS */
617 msg_Dbg( p_demux
, " - SL/FMC descriptor not found/matched" );
620 msg_Err( p_demux
, " - SL/FMC descriptor not found/matched" );
626 static void SetupMetadataDescriptors( demux_t
*p_demux
, ts_stream_t
*p_stream
, const dvbpsi_pmt_es_t
*p_dvbpsies
)
628 ts_es_t
*p_es
= p_stream
->p_es
;
629 const dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x26 );
630 if( p_dr
&& p_dr
->i_length
>= 13 )
633 * metadata_application_format_identifier ID3\x20
634 * i_metadata_format 0xFF
635 * metadata_format_identifier ID3\x20 */
636 if( !memcmp( p_dr
->p_data
, "\xFF\xFFID3 \xFFID3 ", 11 ) &&
637 (p_dr
->p_data
[12] & 0xF0) == 0x00 )
639 p_es
->metadata
.i_format
= VLC_FOURCC('I', 'D', '3', ' ');
640 p_es
->metadata
.i_service_id
= p_dr
->p_data
[11];
641 msg_Dbg( p_demux
, " - found Metadata_descriptor type ID3 with service_id=0x%"PRIx8
,
643 if( !p_stream
->p_proc
)
644 p_stream
->p_proc
= Metadata_stream_processor_New( p_stream
, p_demux
->out
);
649 static void SetupAVCDescriptors( demux_t
*p_demux
, ts_es_t
*p_es
, const dvbpsi_pmt_es_t
*p_dvbpsies
)
651 const dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x28 );
652 if( p_dr
&& p_dr
->i_length
>= 4 )
654 p_es
->fmt
.i_profile
= p_dr
->p_data
[0];
655 p_es
->fmt
.i_level
= p_dr
->p_data
[2];
656 msg_Dbg( p_demux
, " - found AVC_video_descriptor profile=0x%"PRIx8
" level=0x%"PRIx8
,
657 p_es
->fmt
.i_profile
, p_es
->fmt
.i_level
);
661 static void SetupJ2KDescriptors( demux_t
*p_demux
, ts_es_t
*p_es
, const dvbpsi_pmt_es_t
*p_dvbpsies
)
663 const dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x32 );
664 if( p_dr
&& p_dr
->i_length
>= 24 )
666 es_format_Change( &p_es
->fmt
, VIDEO_ES
, VLC_CODEC_JPEG2000
);
667 p_es
->fmt
.i_profile
= p_dr
->p_data
[0];
668 p_es
->fmt
.i_level
= p_dr
->p_data
[1];
669 p_es
->fmt
.video
.i_width
= GetDWBE(&p_dr
->p_data
[2]);
670 p_es
->fmt
.video
.i_height
= GetDWBE(&p_dr
->p_data
[6]);
671 p_es
->fmt
.video
.i_frame_rate_base
= GetWBE(&p_dr
->p_data
[18]);
672 p_es
->fmt
.video
.i_frame_rate
= GetWBE(&p_dr
->p_data
[20]);
673 j2k_fill_color_profile( p_dr
->p_data
[21],
674 &p_es
->fmt
.video
.primaries
,
675 &p_es
->fmt
.video
.transfer
,
676 &p_es
->fmt
.video
.space
);
677 p_es
->b_interlaced
= p_dr
->p_data
[23] & 0x40;
678 if( p_dr
->i_length
> 24 )
680 p_es
->fmt
.p_extra
= malloc(p_dr
->i_length
- 24);
681 if( p_es
->fmt
.p_extra
)
682 p_es
->fmt
.i_extra
= p_dr
->i_length
- 24;
684 msg_Dbg( p_demux
, " - found J2K_video_descriptor profile=0x%"PRIx8
" level=0x%"PRIx8
,
685 p_es
->fmt
.i_profile
, p_es
->fmt
.i_level
);
689 static void SetupTTMLExtendedDescriptor( demux_t
*p_demux
,
690 const dvbpsi_descriptor_t
*p_dr
, es_format_t
*p_fmt
)
696 } subtitle_purpose
[] = {
697 { 0x00, DESC_SUBS_SAME_LANG
},
698 { 0x01, DESC_SUBS_FOREIGN_LANG
},
699 { 0x02, DESC_SUBS_ALL_LANG
},
700 { 0x10, DESC_SUBS_HEARING_IMPAIRED
},
701 { 0x11, DESC_SUBS_HEARING_IMPAIRED
},
702 { 0x12, DESC_SUBS_HEARING_IMPAIRED
},
703 { 0x30, DESC_SUBS_AUDIO_DESCRIPTION_TRANS
},
704 { 0x31, DESC_SUBS_COMMENTARY
},
707 if( p_dr
->i_length
< 9 )
710 msg_Dbg( p_demux
, " - found TTML_descriptor" );
711 if( !p_fmt
->psz_language
)
713 p_fmt
->psz_language
= strndup( (const char *)&p_dr
->p_data
[1], 3 );
714 msg_Dbg( p_demux
, " language: %s", p_fmt
->psz_language
);
717 /* variable members */
718 const uint8_t *p_data
= &p_dr
->p_data
[6];
719 size_t i_data
= p_dr
->i_length
- 6;
722 uint8_t i_profiles_count
= (p_dr
->p_data
[5] & 0x0F);
723 if( i_data
<= i_profiles_count
)
725 p_data
+= i_profiles_count
;
726 i_data
-= i_profiles_count
;
729 if( p_dr
->p_data
[5] & 0x40 )
738 if( p_dr
->p_data
[5] & 0x80 )
740 if( i_data
<= 1U || i_data
<= 1U + p_data
[0] )
742 p_data
+= 1 + p_data
[0];
743 i_data
-= 1 + p_data
[0];
746 /* text... finally */
747 if( i_data
< 1U || i_data
< 1U + p_data
[0] )
749 if( !p_fmt
->psz_description
)
750 p_fmt
->psz_description
= strndup( (const char*) &p_data
[1], p_data
[0] );
754 for( size_t i
=0; i
<ARRAY_SIZE(subtitle_purpose
); i
++ )
756 if( subtitle_purpose
[i
].val
== (p_dr
->p_data
[4] >> 2) )
758 if( !p_fmt
->psz_description
)
759 p_fmt
->psz_description
= strdup( subtitle_purpose
[i
].psz
);
771 } ts_teletext_page_t
;
773 static const char *const ppsz_teletext_type
[] = {
776 DESC_TELETEXT_SUBTITLES
,
777 DESC_TELETEXT_ADDTNL_INFO
,
778 DESC_TELETEXT_SCHEDULE
,
779 DESC_TELETEXT_SUBS_HEARING_IMPAIRED
782 static void PMTSetupEsTeletext( demux_t
*p_demux
, ts_stream_t
*p_pes
,
783 const dvbpsi_pmt_es_t
*p_dvbpsies
)
785 demux_sys_t
*p_sys
= p_demux
->p_sys
;
786 es_format_t
*p_fmt
= &p_pes
->p_es
->fmt
;
788 ts_teletext_page_t p_page
[2 * 64 + 20];
790 dvbpsi_descriptor_t
*p_dr
;
792 /* Gather pages information */
793 for( unsigned i_tag_idx
= 0; i_tag_idx
< 2; i_tag_idx
++ )
795 p_dr
= PMTEsFindDescriptor( p_dvbpsies
, i_tag_idx
== 0 ? 0x46 : 0x56 );
799 dvbpsi_teletext_dr_t
*p_sub
= dvbpsi_DecodeTeletextDr( p_dr
);
803 for( int i
= 0; i
< p_sub
->i_pages_number
; i
++ )
805 const dvbpsi_teletextpage_t
*p_src
= &p_sub
->p_pages
[i
];
807 if( p_src
->i_teletext_type
>= 0x06 )
810 assert( i_page
< sizeof(p_page
)/sizeof(*p_page
) );
812 ts_teletext_page_t
*p_dst
= &p_page
[i_page
++];
814 p_dst
->i_type
= p_src
->i_teletext_type
;
815 p_dst
->i_magazine
= p_src
->i_teletext_magazine_number
816 ? p_src
->i_teletext_magazine_number
: 8;
817 p_dst
->i_page
= p_src
->i_teletext_page_number
;
818 memcpy( p_dst
->p_iso639
, p_src
->i_iso6392_language_code
, 3 );
822 p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x59 );
825 dvbpsi_subtitling_dr_t
*p_sub
= dvbpsi_DecodeSubtitlingDr( p_dr
);
826 for( int i
= 0; p_sub
&& i
< p_sub
->i_subtitles_number
; i
++ )
828 dvbpsi_subtitle_t
*p_src
= &p_sub
->p_subtitle
[i
];
830 if( p_src
->i_subtitling_type
< 0x01 || p_src
->i_subtitling_type
> 0x03 )
833 assert( i_page
< sizeof(p_page
)/sizeof(*p_page
) );
835 ts_teletext_page_t
*p_dst
= &p_page
[i_page
++];
837 switch( p_src
->i_subtitling_type
)
840 p_dst
->i_type
= 0x02;
843 p_dst
->i_type
= 0x03;
846 /* FIXME check if it is the right split */
847 p_dst
->i_magazine
= (p_src
->i_composition_page_id
>> 8)
848 ? (p_src
->i_composition_page_id
>> 8) : 8;
849 p_dst
->i_page
= p_src
->i_composition_page_id
& 0xff;
850 memcpy( p_dst
->p_iso639
, p_src
->i_iso6392_language_code
, 3 );
855 es_format_Change(p_fmt
, SPU_ES
, VLC_CODEC_TELETEXT
);
857 if( !p_sys
->b_split_es
|| i_page
<= 0 )
859 p_fmt
->subs
.teletext
.i_magazine
= -1;
860 p_fmt
->subs
.teletext
.i_page
= 0;
861 p_fmt
->psz_description
= strdup( vlc_gettext(ppsz_teletext_type
[1]) );
863 p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x46 );
865 p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x56 );
867 if( !p_sys
->b_split_es
&& p_dr
&& p_dr
->i_length
> 0 )
869 /* Descriptor pass-through */
870 p_fmt
->p_extra
= malloc( p_dr
->i_length
);
873 p_fmt
->i_extra
= p_dr
->i_length
;
874 memcpy( p_fmt
->p_extra
, p_dr
->p_data
, p_dr
->i_length
);
880 for( unsigned i
= 0; i
< i_page
; i
++ )
887 p_page_es
= p_pes
->p_es
;
891 p_page_es
= ts_es_New( p_pes
->p_es
->p_program
);
895 es_format_Copy( &p_page_es
->fmt
, p_fmt
);
896 free( p_page_es
->fmt
.psz_language
);
897 free( p_page_es
->fmt
.psz_description
);
898 p_page_es
->fmt
.psz_language
= NULL
;
899 p_page_es
->fmt
.psz_description
= NULL
;
900 ts_stream_Add_es( p_pes
, p_page_es
, true );
904 const ts_teletext_page_t
*p
= &p_page
[i
];
905 p_page_es
->fmt
.i_priority
= (p
->i_type
== 0x02 || p
->i_type
== 0x05) ?
906 ES_PRIORITY_SELECTABLE_MIN
: ES_PRIORITY_NOT_DEFAULTABLE
;
907 p_page_es
->fmt
.psz_language
= strndup( p
->p_iso639
, 3 );
908 p_page_es
->fmt
.psz_description
= strdup(vlc_gettext(ppsz_teletext_type
[p
->i_type
]));
909 p_page_es
->fmt
.subs
.teletext
.i_magazine
= p
->i_magazine
;
910 p_page_es
->fmt
.subs
.teletext
.i_page
= p
->i_page
;
913 " * ttxt type=%s lan=%s page=%d%02x",
914 p_page_es
->fmt
.psz_description
,
915 p_page_es
->fmt
.psz_language
,
916 p
->i_magazine
, p
->i_page
);
920 static void PMTSetupEsDvbSubtitle( demux_t
*p_demux
, ts_stream_t
*p_pes
,
921 const dvbpsi_pmt_es_t
*p_dvbpsies
)
923 demux_sys_t
*p_sys
= p_demux
->p_sys
;
924 es_format_t
*p_fmt
= &p_pes
->p_es
->fmt
;
926 es_format_Change( p_fmt
, SPU_ES
, VLC_CODEC_DVBS
);
928 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x59 );
930 dvbpsi_subtitling_dr_t
*p_sub
= dvbpsi_DecodeSubtitlingDr( p_dr
);
931 for( int i
= 0; p_sub
&& i
< p_sub
->i_subtitles_number
; i
++ )
933 const int i_type
= p_sub
->p_subtitle
[i
].i_subtitling_type
;
934 if( ( i_type
>= 0x10 && i_type
<= 0x14 ) ||
935 ( i_type
>= 0x20 && i_type
<= 0x24 ) )
939 if( !p_sys
->b_split_es
|| i_page
<= 0 )
941 p_fmt
->subs
.dvb
.i_id
= -1;
942 p_fmt
->psz_description
= strdup( _("DVB subtitles") );
944 if( !p_sys
->b_split_es
&& p_dr
&& p_dr
->i_length
> 0 )
946 /* Descriptor pass-through */
947 p_fmt
->p_extra
= malloc( p_dr
->i_length
);
950 p_fmt
->i_extra
= p_dr
->i_length
;
951 memcpy( p_fmt
->p_extra
, p_dr
->p_data
, p_dr
->i_length
);
957 for( int i
= 0; i
< p_sub
->i_subtitles_number
; i
++ )
964 p_subs_es
= p_pes
->p_es
;
968 p_subs_es
= ts_es_New( p_pes
->p_es
->p_program
);
972 es_format_Copy( &p_subs_es
->fmt
, p_fmt
);
973 free( p_subs_es
->fmt
.psz_language
);
974 free( p_subs_es
->fmt
.psz_description
);
975 p_subs_es
->fmt
.psz_language
= NULL
;
976 p_subs_es
->fmt
.psz_description
= NULL
;
978 ts_stream_Add_es( p_pes
, p_subs_es
, true );
982 const dvbpsi_subtitle_t
*p
= &p_sub
->p_subtitle
[i
];
983 p_subs_es
->fmt
.psz_language
= strndup( (char *)p
->i_iso6392_language_code
, 3 );
984 switch( p
->i_subtitling_type
)
986 case 0x10: /* unspec. */
988 case 0x12: /* 16:9 */
989 case 0x13: /* 2.21:1 */
990 case 0x14: /* HD monitor */
991 p_subs_es
->fmt
.psz_description
= strdup( _("DVB subtitles") );
993 case 0x20: /* Hearing impaired unspec. */
994 case 0x21: /* h.i. 4:3 */
995 case 0x22: /* h.i. 16:9 */
996 case 0x23: /* h.i. 2.21:1 */
997 case 0x24: /* h.i. HD monitor */
998 p_subs_es
->fmt
.psz_description
= strdup( _("DVB subtitles: hearing impaired") );
1005 p_subs_es
->fmt
.subs
.dvb
.i_id
= ( p
->i_composition_page_id
<< 0 ) |
1006 ( p
->i_ancillary_page_id
<< 16 );
1011 static int vlc_ceil_log2( const unsigned int val
)
1013 int n
= 31 - clz(val
);
1014 if ((1U << n
) != val
)
1020 static void OpusSetup(demux_t
*demux
, uint8_t *p
, size_t len
, es_format_t
*p_fmt
)
1024 /* default mapping */
1025 static const unsigned char map
[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
1026 memcpy(h
.stream_map
, map
, sizeof(map
));
1031 int stream_count
= 0;
1032 int ccc
= p
[1]; // channel_config_code
1036 mapping
= channels
> 2;
1039 channels
= 2; // dual mono
1041 static const uint8_t p_csc
[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
1042 csc
= p_csc
[channels
- 1];
1043 stream_count
= channels
- csc
;
1045 static const uint8_t maps
[6][7] = {
1054 memcpy(&h
.stream_map
[1], maps
[channels
-3], channels
- 1);
1055 } else if (ccc
== 0x81) {
1057 goto explicit_config_too_short
;
1064 bs_init(&s
, &p
[4], len
- 4);
1067 int bits
= vlc_ceil_log2(channels
);
1068 if (s
.i_left
< bits
)
1069 goto explicit_config_too_short
;
1070 stream_count
= bs_read(&s
, bits
) + 1;
1071 bits
= vlc_ceil_log2(stream_count
+ 1);
1072 if (s
.i_left
< bits
)
1073 goto explicit_config_too_short
;
1074 csc
= bs_read(&s
, bits
);
1076 int channel_bits
= vlc_ceil_log2(stream_count
+ csc
+ 1);
1077 if (s
.i_left
< channels
* channel_bits
)
1078 goto explicit_config_too_short
;
1080 unsigned char silence
= (1U << (stream_count
+ csc
+ 1)) - 1;
1081 for (int i
= 0; i
< channels
; i
++) {
1082 unsigned char m
= bs_read(&s
, channel_bits
);
1085 h
.stream_map
[i
] = m
;
1088 } else if (ccc
>= 0x80 && ccc
<= 0x88) {
1089 channels
= ccc
- 0x80;
1094 channels
= 2; // dual mono
1097 stream_count
= channels
;
1099 msg_Err(demux
, "Opus channel configuration 0x%.2x is reserved", ccc
);
1103 msg_Err(demux
, "Opus channel configuration 0x%.2x not supported yet", p
[1]);
1107 opus_prepare_header(channels
, 0, &h
);
1109 h
.input_sample_rate
= 48000;
1111 h
.nb_streams
= channels
- csc
;
1112 h
.channel_mapping
= mapping
;
1115 uint8_t *p_extra
= NULL
;
1117 opus_write_header(&p_extra
, &i_extra
, &h
, NULL
/* FIXME */);
1119 es_format_Change(p_fmt
, AUDIO_ES
, VLC_CODEC_OPUS
);
1120 p_fmt
->p_extra
= p_extra
;
1121 p_fmt
->i_extra
= i_extra
;
1122 p_fmt
->audio
.i_channels
= h
.channels
;
1123 p_fmt
->audio
.i_rate
= 48000;
1129 explicit_config_too_short
:
1130 msg_Err(demux
, "Opus descriptor too short");
1133 static void PMTSetupEs0x02( ts_es_t
*p_es
,
1134 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1136 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x02 );
1139 /* sample: wcbs.ts */
1140 const dvbpsi_vstream_dr_t
*p_vdr
= dvbpsi_DecodeVStreamDr( p_dr
);
1143 if( p_vdr
->i_frame_rate_code
> 1 && p_vdr
->i_frame_rate_code
< 9 &&
1144 !p_vdr
->b_multiple_frame_rate
)
1146 static const int code_to_frame_rate
[8][2] =
1148 { 24000, 1001 }, { 24, 1 }, { 25, 1 }, { 30000, 1001 },
1149 { 30, 1 }, { 50, 1 }, { 60000, 1001 }, { 60, 1 },
1151 p_es
->fmt
.video
.i_frame_rate
= code_to_frame_rate
[p_vdr
->i_frame_rate_code
- 1][0];
1152 p_es
->fmt
.video
.i_frame_rate_base
= code_to_frame_rate
[p_vdr
->i_frame_rate_code
- 1][1];
1154 if( !p_vdr
->b_mpeg2
&& p_es
->fmt
.i_codec
== VLC_CODEC_MPGV
)
1155 p_es
->fmt
.i_original_fourcc
= VLC_CODEC_MP1V
;
1159 /* MPEG2_stereoscopic_video_format_descriptor */
1160 p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x34 );
1161 if( p_dr
&& p_dr
->i_length
> 0 && (p_dr
->p_data
[0] & 0x80) )
1163 video_multiview_mode_t mode
;
1164 switch( p_dr
->p_data
[0] & 0x7F )
1167 mode
= MULTIVIEW_STEREO_SBS
; break;
1169 mode
= MULTIVIEW_STEREO_TB
; break;
1172 mode
= MULTIVIEW_2D
; break;
1174 p_es
->fmt
.video
.multiview_mode
= mode
;
1178 static void PMTSetupEs0x05PrivateData( demux_t
*p_demux
, ts_es_t
*p_es
,
1179 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1181 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1184 if( p_sys
->standard
== TS_STANDARD_DVB
||
1185 p_sys
->standard
== TS_STANDARD_AUTO
)
1187 dvbpsi_descriptor_t
*p_ait_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x6F );
1190 uint8_t *p_data
= p_ait_dr
->p_data
;
1191 for( uint8_t i_data
= p_ait_dr
->i_length
; i_data
>= 3; i_data
-= 3, p_data
+= 3 )
1193 uint16_t i_app_type
= ((p_data
[0] & 0x7F) << 8) | p_data
[1];
1194 msg_Dbg( p_demux
, " - Application type 0x%"PRIx16
" version %"PRIu8
,
1195 i_app_type
, p_data
[2] & 0x1F);
1201 static void PMTSetupEs0x06( demux_t
*p_demux
, ts_stream_t
*p_pes
,
1202 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1204 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1205 es_format_t
*p_fmt
= &p_pes
->p_es
->fmt
;
1206 dvbpsi_descriptor_t
*p_subs_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x59 );
1207 dvbpsi_descriptor_t
*desc
;
1208 if( PMTEsHasRegistration( p_demux
, p_dvbpsies
, "EAC3" ) ||
1209 PMTEsFindDescriptor( p_dvbpsies
, 0x7a ) )
1211 /* DVB with stream_type 0x06 (ETS EN 300 468) */
1212 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_EAC3
);
1214 else if( PMTEsHasRegistration( p_demux
, p_dvbpsies
, "AC-3" ) ||
1215 PMTEsFindDescriptor( p_dvbpsies
, 0x6a ) ||
1216 PMTEsFindDescriptor( p_dvbpsies
, 0x81 ) ) /* AC-3 channel (also in EAC3) */
1218 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_A52
);
1220 else if( PMTEsHasRegistration( p_demux
, p_dvbpsies
, "DTS1" ) || /* 512 Bpf */
1221 PMTEsHasRegistration( p_demux
, p_dvbpsies
, "DTS2" ) || /* 1024 Bpf */
1222 PMTEsHasRegistration( p_demux
, p_dvbpsies
, "DTS3" ) || /* 2048 Bpf */
1223 PMTEsFindDescriptor( p_dvbpsies
, 0x73 ) )
1225 /*registration descriptor(ETSI TS 101 154 Annex F)*/
1226 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_DTS
);
1228 else if( PMTEsHasRegistration( p_demux
, p_dvbpsies
, "BSSD" ) && !p_subs_dr
)
1230 /* BSSD is AES3 DATA, but could also be subtitles
1231 * we need to check for secondary descriptor then s*/
1232 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_302M
);
1233 p_fmt
->b_packetized
= true;
1235 else if( PMTEsHasRegistration( p_demux
, p_dvbpsies
, "HEVC" ) )
1237 es_format_Change( p_fmt
, VIDEO_ES
, VLC_CODEC_HEVC
);
1239 else if( (desc
= PMTEsFindDescriptor( p_dvbpsies
, 0x7f )) &&
1240 desc
->i_length
>= 2 )
1242 /* extended_descriptor on PMT (DVB Bluebook A038) */
1243 switch( desc
->p_data
[0] )
1245 case 0x80: /* User Defined */
1246 /* non finalized Opus in TS Draft. Can't really tell...
1247 * So ffmpeg produced mixes with System-A reg */
1248 if( PMTEsHasRegistration(p_demux
, p_dvbpsies
, "Opus") )
1249 OpusSetup(p_demux
, desc
->p_data
, desc
->i_length
, p_fmt
);
1251 case 0x0E: /* DTS HD */
1252 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_DTS
);
1253 p_fmt
->i_profile
= PROFILE_DTS_HD
;
1255 case 0x0F: /* DTS Neural */
1256 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_DTS
);
1258 case 0x15: /* AC4, unsupported for now */
1259 es_format_Change( p_fmt
, AUDIO_ES
, VLC_FOURCC('A', 'C', '-', '4') );
1262 es_format_Change( p_fmt
, SPU_ES
, VLC_CODEC_TTML_TS
);
1263 SetupTTMLExtendedDescriptor( p_demux
, desc
, p_fmt
);
1267 else if( p_sys
->standard
== TS_STANDARD_ARIB
)
1269 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
1270 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0xFD );
1271 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
1272 if ( p_dr
&& p_dr
->i_length
>= 2 )
1274 /* See STD-B10 Annex J, table J-1 mappings */
1275 const uint16_t i_data_component_id
= GetWBE(p_dr
->p_data
);
1276 if( i_data_component_id
== 0x0008 &&
1277 PMTEsHasComponentTagBetween( p_dvbpsies
, 0x30, 0x37 ) )
1279 es_format_Change( p_fmt
, SPU_ES
, VLC_CODEC_ARIB_A
);
1280 p_fmt
->psz_language
= strndup ( "jpn", 3 );
1281 p_fmt
->psz_description
= strdup( _("ARIB subtitles") );
1283 else if( i_data_component_id
== 0x0012 &&
1284 PMTEsHasComponentTagBetween( p_dvbpsies
, 0x87, 0x88 ) )
1286 es_format_Change( p_fmt
, SPU_ES
, VLC_CODEC_ARIB_C
);
1287 p_fmt
->psz_language
= strndup ( "jpn", 3 );
1288 p_fmt
->psz_description
= strdup( _("ARIB subtitles") );
1294 /* Subtitle/Teletext/VBI fallbacks */
1295 dvbpsi_subtitling_dr_t
*p_sub
;
1296 if( p_subs_dr
&& ( p_sub
= dvbpsi_DecodeSubtitlingDr( p_subs_dr
) ) )
1298 for( int i
= 0; i
< p_sub
->i_subtitles_number
; i
++ )
1300 if( p_fmt
->i_cat
!= UNKNOWN_ES
)
1303 switch( p_sub
->p_subtitle
[i
].i_subtitling_type
)
1305 case 0x01: /* EBU Teletext subtitles */
1306 case 0x02: /* Associated EBU Teletext */
1307 case 0x03: /* VBI data */
1308 PMTSetupEsTeletext( p_demux
, p_pes
, p_dvbpsies
);
1310 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
1311 case 0x11: /* ... on 4:3 AR monitor */
1312 case 0x12: /* ... on 16:9 AR monitor */
1313 case 0x13: /* ... on 2.21:1 AR monitor */
1314 case 0x14: /* ... for display on a high definition monitor */
1315 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
1316 case 0x21: /* ... on 4:3 AR monitor */
1317 case 0x22: /* ... on 16:9 AR monitor */
1318 case 0x23: /* ... on 2.21:1 AR monitor */
1319 case 0x24: /* ... for display on a high definition monitor */
1320 PMTSetupEsDvbSubtitle( p_demux
, p_pes
, p_dvbpsies
);
1323 msg_Err( p_demux
, "Unrecognized DVB subtitle type (0x%x)",
1324 p_sub
->p_subtitle
[i
].i_subtitling_type
);
1330 if( p_fmt
->i_cat
== UNKNOWN_ES
&&
1331 ( PMTEsFindDescriptor( p_dvbpsies
, 0x45 ) || /* VBI Data descriptor */
1332 PMTEsFindDescriptor( p_dvbpsies
, 0x46 ) || /* VBI Teletext descriptor */
1333 PMTEsFindDescriptor( p_dvbpsies
, 0x56 ) ) ) /* EBU Teletext descriptor */
1336 PMTSetupEsTeletext( p_demux
, p_pes
, p_dvbpsies
);
1340 /* FIXME is it useful ? */
1341 if( PMTEsFindDescriptor( p_dvbpsies
, 0x52 ) )
1343 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x52 );
1344 dvbpsi_stream_identifier_dr_t
*p_si
= dvbpsi_DecodeStreamIdentifierDr( p_dr
);
1346 msg_Dbg( p_demux
, " * Stream Component Identifier: %d", p_si
->i_component_tag
);
1350 static void PMTSetupEs0xEA( demux_t
*p_demux
, ts_es_t
*p_es
,
1351 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1353 /* Registration Descriptor */
1354 if( !PMTEsHasRegistration( p_demux
, p_dvbpsies
, "VC-1" ) )
1356 msg_Err( p_demux
, "Registration descriptor not found or invalid" );
1360 /* registration descriptor for VC-1 (SMPTE rp227) */
1361 es_format_Change( &p_es
->fmt
, VIDEO_ES
, VLC_CODEC_VC1
);
1363 /* XXX With Simple and Main profile the SEQUENCE
1364 * header is modified: video width and height are
1365 * inserted just after the start code as 2 int16_t
1366 * The packetizer will take care of that. */
1369 static void PMTSetupEs0xD1( demux_t
*p_demux
, ts_es_t
*p_es
,
1370 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1372 /* Registration Descriptor */
1373 if( !PMTEsHasRegistration( p_demux
, p_dvbpsies
, "drac" ) )
1375 msg_Err( p_demux
, "Registration descriptor not found or invalid" );
1379 /* registration descriptor for Dirac
1380 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
1381 es_format_Change( &p_es
->fmt
, VIDEO_ES
, VLC_CODEC_DIRAC
);
1384 static void PMTSetupEs0xA0( demux_t
*p_demux
, ts_es_t
*p_es
,
1385 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1387 /* MSCODEC sent by vlc */
1388 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0xa0 );
1389 if( !p_dr
|| p_dr
->i_length
< 10 )
1392 "private MSCODEC (vlc) without bih private descriptor" );
1396 es_format_t
*p_fmt
= &p_es
->fmt
;
1397 es_format_Change( &p_es
->fmt
, VIDEO_ES
,
1398 VLC_FOURCC( p_dr
->p_data
[0], p_dr
->p_data
[1],
1399 p_dr
->p_data
[2], p_dr
->p_data
[3] ) );
1400 p_fmt
->video
.i_width
= GetWBE( &p_dr
->p_data
[4] );
1401 p_fmt
->video
.i_height
= GetWBE( &p_dr
->p_data
[6] );
1402 p_fmt
->video
.i_visible_width
= p_fmt
->video
.i_width
;
1403 p_fmt
->video
.i_visible_height
= p_fmt
->video
.i_height
;
1404 p_fmt
->i_extra
= GetWBE( &p_dr
->p_data
[8] );
1406 if( p_fmt
->i_extra
> 0 )
1408 p_fmt
->p_extra
= malloc( p_fmt
->i_extra
);
1409 if( p_fmt
->p_extra
)
1410 memcpy( p_fmt
->p_extra
, &p_dr
->p_data
[10],
1411 __MIN( p_fmt
->i_extra
, p_dr
->i_length
- 10 ) );
1415 /* For such stream we will gather them ourself and don't launch a
1417 * Yes it's ugly but it's the only way to have DIV3 working */
1418 p_fmt
->b_packetized
= true;
1421 static void PMTSetupEs0x83( const dvbpsi_pmt_t
*p_pmt
, ts_es_t
*p_es
, int i_pid
)
1423 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
1424 * with audio track pid being 0x1100..0x11FF */
1425 if ( p_pmt
->i_program_number
== 0x1 &&
1426 p_pmt
->i_pcr_pid
== 0x1000 &&
1427 ( i_pid
>> 8 ) == 0x11 )
1429 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
1430 es_format_Change( &p_es
->fmt
, AUDIO_ES
, VLC_CODEC_WIDI_LPCM
);
1433 es_format_Change( &p_es
->fmt
, AUDIO_ES
, VLC_CODEC_DVD_LPCM
);
1436 static bool PMTSetupEsHDMV( demux_t
*p_demux
, ts_es_t
*p_es
,
1437 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1439 es_format_t
*p_fmt
= &p_es
->fmt
;
1441 /* Blu-Ray mapping */
1442 switch( p_dvbpsies
->i_type
)
1445 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_BD_LPCM
);
1448 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_A52
);
1450 case 0x85: /* DTS-HD High resolution audio */
1451 case 0x86: /* DTS-HD Master audio */
1452 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_DTS
);
1453 p_fmt
->i_profile
= PROFILE_DTS_HD
;
1456 case 0xA2: /* Secondary DTS audio */
1457 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_DTS
);
1460 case 0x83: /* TrueHD AC3 */
1461 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_TRUEHD
);
1464 case 0x84: /* E-AC3 */
1465 case 0xA1: /* Secondary E-AC3 */
1466 es_format_Change( p_fmt
, AUDIO_ES
, VLC_CODEC_EAC3
);
1468 case 0x90: /* Presentation graphics */
1469 es_format_Change( p_fmt
, SPU_ES
, VLC_CODEC_BD_PG
);
1471 case 0x91: /* Interactive graphics */
1473 case 0x92: /* Subtitle */
1474 es_format_Change( p_fmt
, SPU_ES
, VLC_CODEC_BD_TEXT
);
1477 es_format_Change( p_fmt
, VIDEO_ES
, VLC_CODEC_VC1
);
1480 msg_Info( p_demux
, "HDMV registration not implemented for pid 0x%x type 0x%x",
1481 p_dvbpsies
->i_pid
, p_dvbpsies
->i_type
);
1487 static bool PMTSetupEsRegistration( demux_t
*p_demux
, ts_es_t
*p_es
,
1488 const dvbpsi_pmt_es_t
*p_dvbpsies
)
1493 enum es_format_category_e i_cat
;
1494 vlc_fourcc_t i_codec
;
1496 { "AC-3", AUDIO_ES
, VLC_CODEC_A52
},
1497 { "EAC3", AUDIO_ES
, VLC_CODEC_EAC3
},
1498 { "DTS1", AUDIO_ES
, VLC_CODEC_DTS
},
1499 { "DTS2", AUDIO_ES
, VLC_CODEC_DTS
},
1500 { "DTS3", AUDIO_ES
, VLC_CODEC_DTS
},
1501 { "BSSD", AUDIO_ES
, VLC_CODEC_302M
},
1502 { "VC-1", VIDEO_ES
, VLC_CODEC_VC1
},
1503 { "drac", VIDEO_ES
, VLC_CODEC_DIRAC
},
1504 { "", UNKNOWN_ES
, 0 }
1506 es_format_t
*p_fmt
= &p_es
->fmt
;
1508 for( int i
= 0; p_regs
[i
].i_cat
!= UNKNOWN_ES
; i
++ )
1510 if( PMTEsHasRegistration( p_demux
, p_dvbpsies
, p_regs
[i
].psz_tag
) )
1512 es_format_Change( p_fmt
, p_regs
[i
].i_cat
, p_regs
[i
].i_codec
);
1514 /* System A AC3 extension, see ATSC A/52 Annex G.2 */
1515 if ( p_regs
[i
].i_codec
== VLC_CODEC_A52
&& p_dvbpsies
->i_type
== 0x87 )
1516 p_fmt
->i_codec
= VLC_CODEC_EAC3
;
1524 static void PIDFillFormat( demux_t
*p_demux
, ts_stream_t
*p_pes
,
1525 int i_stream_type
, ts_transport_type_t
*p_datatype
)
1527 es_format_t
*fmt
= &p_pes
->p_es
->fmt
;
1528 switch( i_stream_type
)
1530 case 0x01: /* MPEG-1 video */
1531 es_format_Change( fmt
, VIDEO_ES
, VLC_CODEC_MPGV
);
1532 fmt
->i_original_fourcc
= VLC_CODEC_MP1V
;
1534 case 0x02: /* MPEG-2 video */
1535 case 0x80: /* MPEG-2 MOTO video */
1536 es_format_Change( fmt
, VIDEO_ES
, VLC_CODEC_MPGV
);
1538 case 0x03: /* MPEG-1 audio */
1539 case 0x04: /* MPEG-2 audio */
1540 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_MPGA
);
1542 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1543 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_MP4A
);
1544 fmt
->i_original_fourcc
= VLC_FOURCC('A','D','T','S');
1546 case 0x10: /* MPEG4 (video) */
1547 es_format_Change( fmt
, VIDEO_ES
, VLC_CODEC_MP4V
);
1549 case 0x11: /* MPEG4 (audio) LATM */
1550 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_MP4A
);
1551 fmt
->i_original_fourcc
= VLC_FOURCC('L','A','T','M');
1553 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1554 es_format_Change( fmt
, VIDEO_ES
, VLC_CODEC_H264
);
1556 case 0x1C: /* ISO/IEC 14496-3 Audio, without using any additional
1557 transport syntax, such as DST, ALS and SLS */
1558 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_MP4A
);
1560 case 0x24: /* HEVC */
1561 es_format_Change( fmt
, VIDEO_ES
, VLC_CODEC_HEVC
);
1563 case 0x42: /* CAVS (Chinese AVS) */
1564 es_format_Change( fmt
, VIDEO_ES
, VLC_CODEC_CAVS
);
1567 case 0x81: /* A52 (audio) */
1568 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_A52
);
1570 case 0x82: /* SCTE-27 (sub) */
1571 es_format_Change( fmt
, SPU_ES
, VLC_CODEC_SCTE_27
);
1572 *p_datatype
= TS_TRANSPORT_SECTIONS
;
1573 ts_sections_processor_Add( p_demux
, &p_pes
->p_sections_proc
, 0xC6, 0x00,
1574 SCTE27_Section_Callback
, p_pes
);
1576 case 0x84: /* SDDS (audio) */
1577 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_SDDS
);
1579 case 0x85: /* DTS (audio) FIXME: HDMV Only ? */
1580 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_DTS
);
1582 case 0x87: /* E-AC3, ATSC */
1583 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_EAC3
);
1585 case 0x8a: /* DTS (audio) */
1586 es_format_Change( fmt
, AUDIO_ES
, VLC_CODEC_DTS
);
1588 case 0x91: /* A52 vls (audio) */
1589 es_format_Change( fmt
, AUDIO_ES
, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1591 case 0x92: /* DVD_SPU vls (sub) */
1592 es_format_Change( fmt
, SPU_ES
, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1595 case 0x94: /* SDDS (audio) */
1596 es_format_Change( fmt
, AUDIO_ES
, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1599 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1600 es_format_Change( fmt
, UNKNOWN_ES
, 0 );
1603 case 0x06: /* PES_PRIVATE (fixed later) */
1604 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1605 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
1607 es_format_Change( fmt
, UNKNOWN_ES
, 0 );
1612 static void FillPESFromDvbpsiES( demux_t
*p_demux
,
1613 const dvbpsi_pmt_t
*p_dvbpsipmt
,
1614 const dvbpsi_pmt_es_t
*p_dvbpsies
,
1615 ts_pmt_registration_type_t registration_type
,
1616 const ts_pmt_t
*p_pmt
,
1617 ts_stream_t
*p_pes
)
1619 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1620 ts_transport_type_t type_change
= TS_TRANSPORT_PES
;
1621 PIDFillFormat( p_demux
, p_pes
, p_dvbpsies
->i_type
, &type_change
);
1623 p_pes
->i_stream_type
= p_dvbpsies
->i_type
;
1625 bool b_registration_applied
= false;
1626 if ( p_dvbpsies
->i_type
>= 0x80 ) /* non standard, extensions */
1628 if ( registration_type
== TS_PMT_REGISTRATION_BLURAY
)
1630 if (( b_registration_applied
= PMTSetupEsHDMV( p_demux
, p_pes
->p_es
, p_dvbpsies
) ))
1631 msg_Dbg( p_demux
, " + HDMV registration applied to pid %d type 0x%x",
1632 p_dvbpsies
->i_pid
, p_dvbpsies
->i_type
);
1636 if (( b_registration_applied
= PMTSetupEsRegistration( p_demux
, p_pes
->p_es
, p_dvbpsies
) ))
1637 msg_Dbg( p_demux
, " + registration applied to pid %d type 0x%x",
1638 p_dvbpsies
->i_pid
, p_dvbpsies
->i_type
);
1642 if ( !b_registration_applied
)
1644 p_pes
->transport
= type_change
; /* Only change type if registration has not changed meaning */
1646 switch( p_dvbpsies
->i_type
)
1649 PMTSetupEs0x02( p_pes
->p_es
, p_dvbpsies
);
1651 case 0x05: /* Private data in sections */
1652 p_pes
->transport
= TS_TRANSPORT_SECTIONS
;
1653 PMTSetupEs0x05PrivateData( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1656 /* Handle PES private data */
1657 PMTSetupEs0x06( p_demux
, p_pes
, p_dvbpsies
);
1659 case 0x0a: /* DSM-CC */
1663 p_pes
->transport
= TS_TRANSPORT_IGNORE
;
1665 /* All other private or reserved types */
1666 case 0x13: /* SL in sections */
1667 p_pes
->transport
= TS_TRANSPORT_SECTIONS
;
1673 SetupISO14496Descriptors( p_demux
, p_pes
, p_pmt
, p_dvbpsies
);
1676 SetupMetadataDescriptors( p_demux
, p_pes
, p_dvbpsies
);
1679 SetupAVCDescriptors( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1682 SetupJ2KDescriptors( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1686 PMTSetupEs0x83( p_dvbpsipmt
, p_pes
->p_es
, p_dvbpsies
->i_pid
);
1689 PMTSetupEs0xA0( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1692 PMTSetupEs0xD1( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1695 PMTSetupEs0xEA( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1701 if( p_pes
->p_es
->fmt
.i_cat
== AUDIO_ES
)
1703 SetupAudioExtendedDescriptors( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1706 SetupStandardESDescriptors( p_demux
, p_pes
->p_es
, p_dvbpsies
);
1708 if ( registration_type
== TS_PMT_REGISTRATION_BLURAY
)
1711 * 0x1011 primary video
1712 * 0x1100- 0x111f primary audio
1713 * 0x1a00- 0x1a1f secondary audio
1714 * 0x1b00- 0x1b1f secondary video */
1716 /* Disable dolbyvision */
1717 if ( p_dvbpsies
->i_pid
== 0x1015 &&
1718 PMTEsHasRegistration( p_demux
, p_dvbpsies
, "HDMV" ) )
1720 p_pes
->p_es
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
1722 else if( (p_dvbpsies
->i_pid
>= 0x1a00 && p_dvbpsies
->i_pid
<= 0x1a1f) ||
1723 (p_dvbpsies
->i_pid
>= 0x1b00 && p_dvbpsies
->i_pid
<= 0x1b1f) )
1725 /* We might just want to prio, but it will trigger multiple videos es */
1726 p_pes
->p_es
->fmt
.i_priority
= ES_PRIORITY_NOT_DEFAULTABLE
;
1730 p_pes
->p_es
->fmt
.i_priority
= 0xFFFF - (p_dvbpsies
->i_pid
& 0xFFFF) +
1731 ES_PRIORITY_SELECTABLE_MIN
;
1735 /* PES packets usually contain truncated frames */
1736 p_pes
->p_es
->fmt
.b_packetized
= false;
1738 /* Set Groups / ID */
1739 p_pes
->p_es
->fmt
.i_group
= p_dvbpsipmt
->i_program_number
;
1740 if( p_sys
->b_es_id_pid
)
1741 p_pes
->p_es
->fmt
.i_id
= p_dvbpsies
->i_pid
;
1744 static en50221_capmt_info_t
* CreateCAPMTInfo( const dvbpsi_pmt_t
*p_pmt
)
1746 en50221_capmt_info_t
*p_en
= en50221_capmt_New( p_pmt
->i_version
,
1747 p_pmt
->i_program_number
);
1748 if( unlikely(p_en
== NULL
) )
1751 for( const dvbpsi_descriptor_t
*p_dr
= p_pmt
->p_first_descriptor
;
1752 p_dr
; p_dr
= p_dr
->p_next
)
1754 if( p_dr
->i_tag
== 0x09 )
1755 en50221_capmt_AddCADescriptor( p_en
, p_dr
->p_data
, p_dr
->i_length
);
1758 for( const dvbpsi_pmt_es_t
*p_es
= p_pmt
->p_first_es
;
1759 p_es
; p_es
= p_es
->p_next
)
1761 en50221_capmt_es_info_t
*p_enes
= en50221_capmt_EsAdd( p_en
,
1764 if( likely(p_enes
) )
1766 for( const dvbpsi_descriptor_t
*p_dr
= p_es
->p_first_descriptor
;
1767 p_dr
; p_dr
= p_dr
->p_next
)
1769 if( p_dr
->i_tag
== 0x09 )
1770 en50221_capmt_AddESCADescriptor( p_enes
, p_dr
->p_data
, p_dr
->i_length
);
1778 static void PMTCallBack( void *data
, dvbpsi_pmt_t
*p_dvbpsipmt
)
1780 demux_t
*p_demux
= data
;
1781 demux_sys_t
*p_sys
= p_demux
->p_sys
;
1783 ts_pid_t
*pmtpid
= NULL
;
1784 ts_pmt_t
*p_pmt
= NULL
;
1786 msg_Dbg( p_demux
, "PMTCallBack called for program %d", p_dvbpsipmt
->i_program_number
);
1788 if (unlikely(GetPID(p_sys
, 0)->type
!= TYPE_PAT
))
1790 assert(GetPID(p_sys
, 0)->type
== TYPE_PAT
);
1791 dvbpsi_pmt_delete(p_dvbpsipmt
);
1794 const ts_pat_t
*p_pat
= GetPID(p_sys
, 0)->u
.p_pat
;
1796 /* First find this PMT declared in PAT */
1797 for( int i
= 0; !pmtpid
&& i
< p_pat
->programs
.i_size
; i
++ )
1799 const int i_pmt_prgnumber
= p_pat
->programs
.p_elems
[i
]->u
.p_pmt
->i_number
;
1800 if( i_pmt_prgnumber
!= TS_USER_PMT_NUMBER
&&
1801 i_pmt_prgnumber
== p_dvbpsipmt
->i_program_number
)
1803 pmtpid
= p_pat
->programs
.p_elems
[i
];
1804 assert(pmtpid
->type
== TYPE_PMT
);
1805 p_pmt
= pmtpid
->u
.p_pmt
;
1809 if( pmtpid
== NULL
)
1811 msg_Warn( p_demux
, "unreferenced program (broken stream)" );
1812 dvbpsi_pmt_delete(p_dvbpsipmt
);
1816 pmtpid
->i_flags
|= FLAG_SEEN
;
1818 if( p_pmt
->i_version
!= -1 &&
1819 ( !p_dvbpsipmt
->b_current_next
|| p_pmt
->i_version
== p_dvbpsipmt
->i_version
) )
1821 dvbpsi_pmt_delete( p_dvbpsipmt
);
1825 /* Save old es array */
1826 DECL_ARRAY(ts_pid_t
*) pid_to_decref
;
1827 pid_to_decref
.i_alloc
= p_pmt
->e_streams
.i_alloc
;
1828 pid_to_decref
.i_size
= p_pmt
->e_streams
.i_size
;
1829 pid_to_decref
.p_elems
= p_pmt
->e_streams
.p_elems
;
1830 if( p_pmt
->p_atsc_si_basepid
)
1831 ARRAY_APPEND( pid_to_decref
, p_pmt
->p_atsc_si_basepid
);
1832 if( p_pmt
->p_si_sdt_pid
)
1833 ARRAY_APPEND( pid_to_decref
, p_pmt
->p_si_sdt_pid
);
1834 ARRAY_INIT(p_pmt
->e_streams
);
1838 ODFree( p_pmt
->iod
);
1842 msg_Dbg( p_demux
, "new PMT program number=%d version=%d pid_pcr=%d",
1843 p_dvbpsipmt
->i_program_number
, p_dvbpsipmt
->i_version
, p_dvbpsipmt
->i_pcr_pid
);
1844 p_pmt
->i_pid_pcr
= p_dvbpsipmt
->i_pcr_pid
;
1845 p_pmt
->i_version
= p_dvbpsipmt
->i_version
;
1847 if( ProgramIsSelected( p_sys
, p_pmt
->i_number
) )
1848 SetPIDFilter( p_sys
, GetPID(p_sys
, p_pmt
->i_pid_pcr
), true ); /* Set demux filter */
1850 /* Parse PMT descriptors */
1851 ts_pmt_registration_type_t registration_type
= TS_PMT_REGISTRATION_NONE
;
1852 ParsePMTRegistrations( p_demux
, p_dvbpsipmt
->p_first_descriptor
, p_pmt
, ®istration_type
);
1854 if( p_sys
->standard
== TS_STANDARD_AUTO
)
1856 switch( registration_type
)
1858 case TS_PMT_REGISTRATION_BLURAY
:
1859 TsChangeStandard( p_sys
, TS_STANDARD_MPEG
);
1861 case TS_PMT_REGISTRATION_ARIB
:
1862 TsChangeStandard( p_sys
, TS_STANDARD_ARIB
);
1864 case TS_PMT_REGISTRATION_ATSC
:
1865 TsChangeStandard( p_sys
, TS_STANDARD_ATSC
);
1868 if(SEEN(GetPID(p_sys
, ATSC_BASE_PID
)))
1870 TsChangeStandard( p_sys
, TS_STANDARD_ATSC
);
1874 /* Probe using ES */
1875 p_sys
->standard
= ProbePMTStandard( p_dvbpsipmt
);
1881 /* Private descriptors depends on standard */
1882 ParsePMTPrivateRegistrations( p_demux
, p_dvbpsipmt
->p_first_descriptor
, p_pmt
, p_sys
->standard
);
1884 dvbpsi_pmt_es_t
*p_dvbpsies
;
1885 for( p_dvbpsies
= p_dvbpsipmt
->p_first_es
; p_dvbpsies
!= NULL
; p_dvbpsies
= p_dvbpsies
->p_next
)
1887 ts_pid_t
*pespid
= GetPID(p_sys
, p_dvbpsies
->i_pid
);
1888 if ( pespid
->type
!= TYPE_STREAM
&& pespid
->type
!= TYPE_FREE
)
1890 msg_Warn( p_demux
, " * PMT wants to create PES on pid %d used by non PES", pespid
->i_pid
);
1894 char const * psz_typedesc
= ISO13818_1_Get_StreamType_Description( p_dvbpsies
->i_type
);
1896 msg_Dbg( p_demux
, " * pid=%d type=0x%x %s",
1897 p_dvbpsies
->i_pid
, p_dvbpsies
->i_type
, psz_typedesc
);
1899 /* PMT element/component descriptors */
1900 for( dvbpsi_descriptor_t
*p_dr
= p_dvbpsies
->p_first_descriptor
;
1901 p_dr
!= NULL
; p_dr
= p_dr
->p_next
)
1903 const char *psz_desc
= NULL
;
1904 if( registration_type
== TS_PMT_REGISTRATION_ARIB
)
1905 psz_desc
= ARIB_B10_Get_PMT_Descriptor_Description( p_dr
->i_tag
);
1908 msg_Dbg( p_demux
, " - ES descriptor %s 0x%x", psz_desc
, p_dr
->i_tag
);
1910 msg_Dbg( p_demux
, " - ES descriptor tag 0x%x", p_dr
->i_tag
);
1913 const bool b_pid_inuse
= ( pespid
->type
== TYPE_STREAM
);
1916 if ( !PIDSetup( p_demux
, TYPE_STREAM
, pespid
, pmtpid
) )
1918 msg_Warn( p_demux
, " * pid=%d type=0x%x %s (skipped)",
1919 p_dvbpsies
->i_pid
, p_dvbpsies
->i_type
, psz_typedesc
);
1924 if( b_pid_inuse
) /* pes will point to a temp */
1926 p_pes
= ts_stream_New( p_demux
, p_pmt
);
1929 PIDRelease( p_demux
, pespid
);
1933 else /* pes will point to the new one allocated from PIDSetup */
1935 p_pes
= pespid
->u
.p_stream
;
1939 /* Add pid to the list of used ones in pmt */
1940 ARRAY_APPEND( p_pmt
->e_streams
, pespid
);
1941 pespid
->i_flags
|= SEEN(GetPID(p_sys
, p_dvbpsies
->i_pid
));
1943 /* Fill p_pes es and add extra es if any */
1944 FillPESFromDvbpsiES( p_demux
, p_dvbpsipmt
, p_dvbpsies
,
1945 registration_type
, p_pmt
, p_pes
);
1947 /* Set description and debug */
1948 if( p_pes
->p_es
->fmt
.i_cat
== UNKNOWN_ES
)
1950 msg_Dbg( p_demux
, " => pid %d content is *unknown*",
1951 p_dvbpsies
->i_pid
);
1952 if( !p_pes
->p_es
->fmt
.psz_description
)
1953 p_pes
->p_es
->fmt
.psz_description
= strdup( psz_typedesc
);
1957 msg_Dbg( p_demux
, " => pid %d has now es fcc=%4.4s",
1958 p_dvbpsies
->i_pid
, (char*)&p_pes
->p_es
->fmt
.i_codec
);
1961 dvbpsi_descriptor_t
*p_dr
= PMTEsFindDescriptor( p_dvbpsies
, 0x09 );
1962 if( p_dr
&& p_dr
->i_length
>= 2 )
1964 msg_Dbg( p_demux
, " - ES descriptor : CA (0x9) SysID 0x%x",
1965 (p_dr
->p_data
[0] << 8) | p_dr
->p_data
[1] );
1968 const bool b_create_es
= (p_pes
->p_es
->fmt
.i_cat
!= UNKNOWN_ES
);
1970 /* Now check and merge */
1971 if( b_pid_inuse
) /* We need to compare to the existing pes/es */
1973 ts_es_t
*p_existing_es
= ts_stream_Find_es( pespid
->u
.p_stream
, p_pmt
);
1976 const es_format_t
*ofmt
= &p_existing_es
->fmt
;
1977 const es_format_t
*nfmt
= &p_pes
->p_es
->fmt
;
1979 /* Check if we can avoid restarting that ES */
1980 bool b_canreuse
= es_format_IsSimilar( ofmt
, nfmt
);
1982 /* Check codecs extra */
1983 b_canreuse
= b_canreuse
&&
1985 ofmt
->i_extra
== nfmt
->i_extra
&&
1986 ( ofmt
->i_extra
== 0 ||
1987 memcmp( ofmt
->p_extra
, nfmt
->p_extra
, nfmt
->i_extra
) == 0 )
1990 /* Tracks must have same language */
1991 b_canreuse
= b_canreuse
&&
1993 ( !!ofmt
->psz_language
== !!nfmt
->psz_language
) &&
1994 ( ofmt
->psz_language
== NULL
||
1995 !strcmp( ofmt
->psz_language
, nfmt
->psz_language
) )
1998 /* Check is we have any subtitles */
1999 b_canreuse
= b_canreuse
&&
2000 ( ts_Count_es( p_pes
->p_es
->p_extraes
, false, NULL
) ==
2001 ts_Count_es( p_existing_es
->p_extraes
, false, NULL
)
2006 /* Just keep using previous es */
2007 ts_stream_Del( p_demux
, p_pes
);
2011 ts_es_t
*p_new
= ts_stream_Extract_es( p_pes
, p_pmt
);
2012 ts_es_t
*p_old
= ts_stream_Extract_es( pespid
->u
.p_stream
, p_pmt
);
2013 ts_stream_Add_es( pespid
->u
.p_stream
, p_new
, false );
2014 assert(p_old
== p_existing_es
);
2015 assert(ts_Count_es(p_pes
->p_es
, false, NULL
) == 0);
2016 ts_stream_Add_es( p_pes
, p_old
, false );
2017 ts_stream_Del( p_demux
, p_pes
);
2020 else /* There was no es for that program on that pid, merge in */
2022 assert(ts_Count_es(pespid
->u
.p_stream
->p_es
, false, NULL
)); /* Used by another program */
2023 ts_es_t
*p_new
= ts_stream_Extract_es( p_pes
, p_pmt
);
2025 ts_stream_Add_es( pespid
->u
.p_stream
, p_new
, false );
2026 ts_stream_Del( p_demux
, p_pes
);
2029 /* Nothing to do, pes is now just set */
2031 AddAndCreateES( p_demux
, pespid
, false );
2034 /* Set CAM descrambling */
2035 if( ProgramIsSelected( p_sys
, p_pmt
->i_number
) )
2037 en50221_capmt_info_t
*p_en
= CreateCAPMTInfo( p_dvbpsipmt
);
2040 /* DTV/CAM takes ownership of en50221_capmt_info_t on success */
2041 if( vlc_stream_Control( p_sys
->stream
, STREAM_SET_PRIVATE_ID_CA
,
2042 (void *)p_en
) != VLC_SUCCESS
)
2044 en50221_capmt_Delete( p_en
);
2045 if ( p_sys
->standard
== TS_STANDARD_ARIB
&& !p_sys
->arib
.b25stream
)
2047 p_sys
->arib
.b25stream
= vlc_stream_FilterNew( p_demux
->s
, "aribcam" );
2048 p_sys
->stream
= ( p_sys
->arib
.b25stream
) ? p_sys
->arib
.b25stream
: p_demux
->s
;
2054 /* Add arbitrary PID from here */
2055 if ( p_sys
->standard
== TS_STANDARD_ATSC
)
2057 ts_pid_t
*atsc_base_pid
= GetPID(p_sys
, ATSC_BASE_PID
);
2058 if ( PIDSetup( p_demux
, TYPE_PSIP
, atsc_base_pid
, pmtpid
) )
2060 ts_psip_t
*p_psip
= atsc_base_pid
->u
.p_psip
;
2061 if( !ATSC_Attach_Dvbpsi_Base_Decoders( p_psip
->handle
, atsc_base_pid
) )
2063 msg_Err( p_demux
, "dvbpsi_atsc_AttachMGT/STT failed for program %d",
2065 PIDRelease( p_demux
, atsc_base_pid
);
2069 p_pmt
->p_atsc_si_basepid
= atsc_base_pid
;
2070 SetPIDFilter( p_demux
->p_sys
, atsc_base_pid
, true );
2071 msg_Dbg( p_demux
, " * pid=%d listening for MGT/STT", atsc_base_pid
->i_pid
);
2073 /* Set up EAS spu es */
2074 if( p_pmt
->e_streams
.i_size
)
2076 ts_es_t
*p_eas_es
= ts_es_New( p_pmt
);
2077 if( likely(p_eas_es
) )
2079 es_format_Change( &p_eas_es
->fmt
, SPU_ES
, VLC_CODEC_SCTE_18
);
2080 p_eas_es
->fmt
.i_id
= ATSC_BASE_PID
;
2081 p_eas_es
->fmt
.i_group
= p_pmt
->i_number
;
2082 p_eas_es
->fmt
.psz_description
= strdup(SCTE18_DESCRIPTION
);
2083 if( p_psip
->p_eas_es
)
2085 ts_es_t
*p_next
= p_psip
->p_eas_es
->p_next
;
2086 p_psip
->p_eas_es
->p_next
= p_eas_es
;
2087 p_eas_es
->p_next
= p_next
;
2091 p_psip
->p_eas_es
= p_eas_es
;
2093 msg_Dbg( p_demux
, " * pid=%d listening for EAS events", ATSC_BASE_PID
);
2098 else if( atsc_base_pid
->type
!= TYPE_FREE
)
2100 msg_Err( p_demux
, "can't attach PSIP table handlers"
2101 "on already in use ATSC base pid %d", ATSC_BASE_PID
);
2104 else if( p_sys
->standard
!= TS_STANDARD_MPEG
&& p_sys
->standard
!= TS_STANDARD_TDMB
)
2106 ts_pid_t
*p_sdt_pid
= ts_pid_Get( &p_sys
->pids
, TS_SI_SDT_PID
);
2107 if ( PIDSetup( p_demux
, TYPE_SI
, p_sdt_pid
, pmtpid
) ) /* Create or incref SDT */
2109 if( !ts_attach_SI_Tables_Decoders( p_sdt_pid
) )
2111 msg_Err( p_demux
, "Can't attach SI table decoders from program %d",
2113 PIDRelease( p_demux
, p_sdt_pid
);
2117 p_pmt
->p_si_sdt_pid
= p_sdt_pid
;
2118 SetPIDFilter( p_demux
->p_sys
, p_sdt_pid
, true );
2119 msg_Dbg( p_demux
, " * pid=%d listening for SDT", p_sdt_pid
->i_pid
);
2122 else if( p_sdt_pid
->type
!= TYPE_FREE
)
2124 msg_Err( p_demux
, "can't attach SI SDT table handler"
2125 "on already in used pid %d (Not DVB ?)", p_sdt_pid
->i_pid
);
2129 /* Decref or clean now unused es */
2130 for( int i
= 0; i
< pid_to_decref
.i_size
; i
++ )
2131 PIDRelease( p_demux
, pid_to_decref
.p_elems
[i
] );
2132 ARRAY_RESET( pid_to_decref
);
2134 if( !p_sys
->b_trust_pcr
)
2136 int i_cand
= FindPCRCandidate( p_pmt
);
2137 p_pmt
->i_pid_pcr
= i_cand
;
2138 p_pmt
->pcr
.b_disable
= true;
2139 msg_Warn( p_demux
, "PCR not trusted for program %d, set up workaround using pid %d",
2140 p_pmt
->i_number
, i_cand
);
2143 UpdatePESFilters( p_demux
, p_sys
->seltype
== PROGRAM_ALL
);
2145 /* Probe Boundaries */
2146 if( p_sys
->b_canfastseek
&& p_pmt
->i_last_dts
== TS_TICK_UNKNOWN
)
2148 p_pmt
->i_last_dts
= 0;
2149 ProbeStart( p_demux
, p_pmt
->i_number
);
2150 ProbeEnd( p_demux
, p_pmt
->i_number
);
2153 dvbpsi_pmt_delete( p_dvbpsipmt
);
2156 int UserPmt( demux_t
*p_demux
, const char *psz_fmt
)
2158 demux_sys_t
*p_sys
= p_demux
->p_sys
;
2159 char *psz_dup
= strdup( psz_fmt
);
2160 char *psz
= psz_dup
;
2167 unsigned long i_pid
= strtoul( psz
, &psz
, 0 );
2168 if( i_pid
< 2 || i_pid
>= 8192 )
2171 /* Parse optional program number */
2174 i_number
= strtol( &psz
[1], &psz
, 0 );
2177 ts_pid_t
*pmtpid
= GetPID(p_sys
, i_pid
);
2179 msg_Dbg( p_demux
, "user pmt specified (pid=%lu,number=%d)", i_pid
, i_number
);
2180 if ( !PIDSetup( p_demux
, TYPE_PMT
, pmtpid
, GetPID(p_sys
, 0) ) )
2184 ts_pmt_t
*p_pmt
= pmtpid
->u
.p_pmt
;
2185 p_pmt
->i_number
= i_number
!= 0 ? i_number
: TS_USER_PMT_NUMBER
;
2186 if( !dvbpsi_pmt_attach( p_pmt
->handle
,
2187 ((i_number
!= TS_USER_PMT_NUMBER
? i_number
: 1)),
2188 PMTCallBack
, p_demux
) )
2190 PIDRelease( p_demux
, pmtpid
);
2194 ARRAY_APPEND( GetPID(p_sys
, 0)->u
.p_pat
->programs
, pmtpid
);
2196 psz
= strchr( psz
, '=' );
2199 while( psz
&& *psz
)
2201 char *psz_next
= strchr( psz
, ',' );
2206 i_pid
= strtoul( psz
, &psz
, 0 );
2207 if( *psz
!= ':' || i_pid
< 2 || i_pid
>= 8192 )
2210 char *psz_opt
= &psz
[1];
2211 if( !strcmp( psz_opt
, "pcr" ) )
2213 p_pmt
->i_pid_pcr
= i_pid
;
2215 else if( GetPID(p_sys
, i_pid
)->type
== TYPE_FREE
)
2217 ts_pid_t
*pid
= GetPID(p_sys
, i_pid
);
2219 char *psz_arg
= strchr( psz_opt
, '=' );
2223 if ( !PIDSetup( p_demux
, TYPE_STREAM
, pid
, pmtpid
) )
2226 ARRAY_APPEND( p_pmt
->e_streams
, pid
);
2228 if( p_pmt
->i_pid_pcr
<= 0 )
2229 p_pmt
->i_pid_pcr
= i_pid
;
2231 es_format_t
*fmt
= &pid
->u
.p_stream
->p_es
->fmt
;
2233 if( psz_arg
&& strlen( psz_arg
) == 4 )
2235 const vlc_fourcc_t i_codec
= VLC_FOURCC( psz_arg
[0], psz_arg
[1],
2236 psz_arg
[2], psz_arg
[3] );
2237 int i_cat
= UNKNOWN_ES
;
2239 if( !strcmp( psz_opt
, "video" ) )
2241 else if( !strcmp( psz_opt
, "audio" ) )
2243 else if( !strcmp( psz_opt
, "spu" ) )
2246 es_format_Change( fmt
, i_cat
, i_codec
);
2247 fmt
->b_packetized
= false;
2251 const int i_stream_type
= strtol( psz_opt
, NULL
, 0 );
2252 PIDFillFormat( p_demux
, pid
->u
.p_stream
, i_stream_type
, &pid
->u
.p_stream
->transport
);
2255 fmt
->i_group
= i_number
;
2256 if( p_sys
->b_es_id_pid
)
2259 if( fmt
->i_cat
!= UNKNOWN_ES
)
2261 msg_Dbg( p_demux
, " * es pid=%lu fcc=%4.4s", i_pid
,
2262 (char*)&fmt
->i_codec
);
2263 pid
->u
.p_stream
->p_es
->id
= es_out_Add( p_demux
->out
, fmt
);
2272 p_sys
->b_user_pmt
= true;
2278 return VLC_EGENERIC
;
2281 bool ts_psi_PAT_Attach( ts_pid_t
*patpid
, void *cbdata
)
2283 if( unlikely(patpid
->type
!= TYPE_PAT
|| patpid
->i_pid
!= TS_PSI_PAT_PID
) )
2285 return dvbpsi_pat_attach( patpid
->u
.p_pat
->handle
, PATCallBack
, cbdata
);
2288 void ts_psi_Packet_Push( ts_pid_t
*p_pid
, const uint8_t *p_pktbuffer
)
2290 if( p_pid
->type
== TYPE_PAT
)
2291 dvbpsi_packet_push( p_pid
->u
.p_pat
->handle
, (uint8_t *) p_pktbuffer
);
2292 else if( p_pid
->type
== TYPE_PMT
)
2293 dvbpsi_packet_push( p_pid
->u
.p_pmt
->handle
, (uint8_t *) p_pktbuffer
);