demux: es: fix swab usage
[vlc.git] / modules / demux / mpeg / ts_psi.c
blobe9fb6659d16263f22b44c9e2b491d3a684ab41ca
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 *****************************************************************************/
19 #ifdef HAVE_CONFIG_H
20 # include "config.h"
21 #endif
23 #include <vlc_common.h>
25 #ifndef _DVBPSI_DVBPSI_H_
26 # include <dvbpsi/dvbpsi.h>
27 #endif
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>
35 #include <vlc_bits.h>
37 #include "ts_streams.h"
38 #include "ts_psi.h"
39 #include "ts_pid.h"
40 #include "ts_streams_private.h"
41 #include "ts.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"
51 #include "sections.h"
52 #include "ts_sl.h"
53 #include "ts_scte.h"
54 #include "ts_psip.h"
55 #include "ts_si.h"
56 #include "ts_metadata.h"
57 #include "ts_descriptions.h"
59 #include "../access/dtv/en50221_capmt.h"
61 #include <assert.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 */
70 int i_prev_pid = -1;
71 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
72 p_program != NULL;
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 );
78 return VLC_EGENERIC;
80 i_prev_pid = p_program->i_pid;
82 return VLC_SUCCESS;
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" );
100 return;
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 );
111 return;
114 else msg_Warn( p_demux, "Replacing generated PAT with one received from stream" );
116 /* check content */
117 if( !p_dvbpsipat->b_current_next || p_sys->b_user_pmt ||
118 PATCheck( p_demux, p_dvbpsipat ) )
120 dvbpsi_pat_delete( p_dvbpsipat );
121 return;
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;
145 break;
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,
157 p_program->i_pid );
158 if( p_program->i_number == 0 )
159 continue;
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,
168 p_program->i_pid );
169 continue;
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 );
240 switch(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" );
247 break;
250 static const struct
252 const char rgs[4];
253 const ts_pmt_registration_type_t reg;
254 } regs[] = {
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 );
266 break;
270 break;
272 case 0x09:
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);
278 break;
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 );
282 break;
284 case 0xC1:
285 i_arib_score_flags |= 1 << 2;
286 break;
288 case 0xF6:
289 i_arib_score_flags |= 1 << 1;
290 break;
292 default:
293 break;
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 )
310 VLC_UNUSED(p_pmt);
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 )
315 continue;
317 switch( i_standard )
319 case TS_STANDARD_ARIB:
321 const char *psz_desc = ARIB_B10_Get_PMT_Descriptor_Description( p_dr->i_tag );
322 if( psz_desc )
323 msg_Dbg( p_demux, PMT_DESC_PREFIX "%s (0x%x)", psz_desc, p_dr->i_tag );
324 else
325 msg_Dbg( p_demux, PMT_DESC_PREFIX "Unknown Private (0x%x)", p_dr->i_tag );
327 break;
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" );
337 break;
340 /* fallthrough */
341 default:
342 msg_Dbg( p_demux, PMT_DESC_PREFIX "Unknown Private (0x%x)", p_dr->i_tag );
343 break;
348 /*****************************************************************************
349 * PMT callback and helpers
350 *****************************************************************************/
351 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
352 int i_tag )
354 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
355 while( p_dr && ( p_dr->i_tag != i_tag ) )
356 p_dr = p_dr->p_next;
357 return p_dr;
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 );
364 if( !p_dr )
365 return false;
367 if( p_dr->i_length < 4 )
369 msg_Warn( p_demux, "invalid Registration Descriptor" );
370 return false;
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 );
381 if( !p_dr )
382 return false;
383 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
384 if( !p_si )
385 return false;
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[] = {
425 DESC_MAIN_AUDIO,
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[] = {
463 /* "Main audio", */
464 DESC_CLEAN_EFFECTS,
465 DESC_CLEAN_AUDIO_HEARING_IMPAIRED,
466 DESC_VISUAL_IMPAIRED_COMMENTS,
469 if ( type == 0 || type >= ARRAY_SIZE(audio_type) )
470 return NULL;
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 );
479 if( !p_decoded )
481 msg_Err( p_demux, " Failed to decode a ISO 639 descriptor" );
482 return;
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 )
503 return;
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 )
533 case 0x0a:
534 if( p_es->fmt.i_cat == AUDIO_ES )
535 SetupISO639Descriptor( p_demux, p_es, p_dr );
536 break;
538 case 0x0E:
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;
544 break;
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;
555 while( p_dr )
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 */
568 if( i_length == 2 )
569 break;
570 i_length -= 3;
571 msg_Dbg( p_demux, " - found FMC_descriptor mapping es_id=%"PRIu16, p_es->i_sl_es_id );
573 break;
574 case 0x1e: /* SL Descriptor */
575 if( i_length == 2 )
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 */
582 if( !p_pes->p_proc )
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;
593 break;
594 default:
595 break;
598 p_dr = p_dr->p_next;
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 );
610 else
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" );
618 break;
619 default:
620 msg_Err( p_demux, " - SL/FMC descriptor not found/matched" );
621 break;
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 )
632 /* app format 0xFFFF
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,
642 p_dr->p_data[11] );
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 )
692 static const struct
694 uint8_t val;
695 const char * psz;
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 )
708 return;
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;
721 /* profiles */
722 uint8_t i_profiles_count = (p_dr->p_data[5] & 0x0F);
723 if( i_data <= i_profiles_count )
724 goto end;
725 p_data += i_profiles_count;
726 i_data -= i_profiles_count;
728 /* qualifier */
729 if( p_dr->p_data[5] & 0x40 )
731 if( i_data <= 4 )
732 goto end;
733 p_data += 4;
734 i_data -= 4;
737 /* font usage */
738 if( p_dr->p_data[5] & 0x80 )
740 if( i_data <= 1U || i_data <= 1U + p_data[0] )
741 goto end;
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] )
748 goto end;
749 if( !p_fmt->psz_description )
750 p_fmt->psz_description = strndup( (const char*) &p_data[1], p_data[0] );
752 end:
753 /* Apply */
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 );
760 break;
765 typedef struct
767 int i_type;
768 int i_magazine;
769 int i_page;
770 char p_iso639[3];
771 } ts_teletext_page_t;
773 static const char *const ppsz_teletext_type[] = {
775 DESC_TELETEXT,
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];
789 unsigned i_page = 0;
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 );
796 if( !p_dr )
797 continue;
799 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
800 if( !p_sub )
801 continue;
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 )
808 continue;
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 );
823 if( p_dr )
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 )
831 continue;
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 )
839 case 0x01:
840 p_dst->i_type = 0x02;
841 break;
842 default:
843 p_dst->i_type = 0x03;
844 break;
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 );
854 /* */
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 );
864 if( !p_dr )
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 );
871 if( p_fmt->p_extra )
873 p_fmt->i_extra = p_dr->i_length;
874 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
878 else
880 for( unsigned i = 0; i < i_page; i++ )
882 ts_es_t *p_page_es;
884 /* */
885 if( i == 0 )
887 p_page_es = p_pes->p_es;
889 else
891 p_page_es = ts_es_New( p_pes->p_es->p_program );
892 if( !p_page_es )
893 break;
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 );
903 /* */
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;
912 msg_Dbg( p_demux,
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 );
929 int i_page = 0;
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 ) )
936 i_page++;
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 );
948 if( p_fmt->p_extra )
950 p_fmt->i_extra = p_dr->i_length;
951 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
955 else
957 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
959 ts_es_t *p_subs_es;
961 /* */
962 if( i == 0 )
964 p_subs_es = p_pes->p_es;
966 else
968 p_subs_es = ts_es_New( p_pes->p_es->p_program );
969 if( !p_subs_es )
970 break;
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 );
981 /* */
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. */
987 case 0x11: /* 4:3 */
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") );
992 break;
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") );
999 break;
1000 default:
1001 break;
1004 /* Hack, FIXME */
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)
1015 n++;
1017 return n;
1020 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
1022 OpusHeader h;
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));
1028 uint8_t mapping;
1029 int csc;
1030 int channels = 0;
1031 int stream_count = 0;
1032 int ccc = p[1]; // channel_config_code
1033 if (ccc <= 8) {
1034 channels = ccc;
1035 if (channels)
1036 mapping = channels > 2;
1037 else {
1038 mapping = 255;
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] = {
1046 { 2,1 },
1047 { 1,2,3 },
1048 { 4,1,2,3 },
1049 { 4,1,2,3,5 },
1050 { 4,1,2,3,5,6 },
1051 { 6,1,2,3,4,5,7 },
1053 if (channels > 2)
1054 memcpy(&h.stream_map[1], maps[channels-3], channels - 1);
1055 } else if (ccc == 0x81) {
1056 if (len < 4)
1057 goto explicit_config_too_short;
1059 channels = p[2];
1060 mapping = p[3];
1061 csc = 0;
1062 if (mapping) {
1063 bs_t s;
1064 bs_init(&s, &p[4], len - 4);
1065 stream_count = 1;
1066 if (channels) {
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);
1083 if (m == silence)
1084 m = 0xff;
1085 h.stream_map[i] = m;
1088 } else if (ccc >= 0x80 && ccc <= 0x88) {
1089 channels = ccc - 0x80;
1090 if (channels)
1091 mapping = 1;
1092 else {
1093 mapping = 255;
1094 channels = 2; // dual mono
1096 csc = 0;
1097 stream_count = channels;
1098 } else {
1099 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
1102 if (!channels) {
1103 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
1104 return;
1107 opus_prepare_header(channels, 0, &h);
1108 h.preskip = 0;
1109 h.input_sample_rate = 48000;
1110 h.nb_coupled = csc;
1111 h.nb_streams = channels - csc;
1112 h.channel_mapping = mapping;
1114 if (h.channels) {
1115 uint8_t *p_extra = NULL;
1116 int i_extra = 0;
1117 opus_write_header(&p_extra, &i_extra, &h, NULL /* FIXME */);
1118 if (p_extra) {
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;
1127 return;
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 );
1137 if( p_dr )
1139 /* sample: wcbs.ts */
1140 const dvbpsi_vstream_dr_t *p_vdr = dvbpsi_DecodeVStreamDr( p_dr );
1141 if( p_vdr )
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 )
1166 case 0x03:
1167 mode = MULTIVIEW_STEREO_SBS; break;
1168 case 0x04:
1169 mode = MULTIVIEW_STEREO_TB; break;
1170 case 0x08:
1171 default:
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;
1183 VLC_UNUSED(p_es);
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 );
1188 if( p_ait_dr )
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);
1250 break;
1251 case 0x0E: /* DTS HD */
1252 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_DTS );
1253 p_fmt->i_profile = PROFILE_DTS_HD;
1254 break;
1255 case 0x0F: /* DTS Neural */
1256 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_DTS );
1257 break;
1258 case 0x15: /* AC4, unsupported for now */
1259 es_format_Change( p_fmt, AUDIO_ES, VLC_FOURCC('A', 'C', '-', '4') );
1260 break;
1261 case 0x20:
1262 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_TTML_TS );
1263 SetupTTMLExtendedDescriptor( p_demux, desc, p_fmt );
1264 break;
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") );
1292 else
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 )
1301 break;
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 );
1309 break;
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 );
1321 break;
1322 default:
1323 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
1324 p_sub->p_subtitle[i].i_subtitling_type );
1325 break;
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 */
1335 /* Teletext/VBI */
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" );
1357 return;
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" );
1376 return;
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 )
1391 msg_Warn( p_demux,
1392 "private MSCODEC (vlc) without bih private descriptor" );
1393 return;
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 ) );
1412 else
1413 p_fmt->i_extra = 0;
1415 /* For such stream we will gather them ourself and don't launch a
1416 * packetizer.
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 );
1432 else
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 )
1444 case 0x80:
1445 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_BD_LPCM );
1446 break;
1447 case 0x81:
1448 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_A52 );
1449 break;
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;
1454 break;
1455 case 0x82:
1456 case 0xA2: /* Secondary DTS audio */
1457 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_DTS );
1458 break;
1460 case 0x83: /* TrueHD AC3 */
1461 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_TRUEHD );
1462 break;
1464 case 0x84: /* E-AC3 */
1465 case 0xA1: /* Secondary E-AC3 */
1466 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_EAC3 );
1467 break;
1468 case 0x90: /* Presentation graphics */
1469 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_BD_PG );
1470 break;
1471 case 0x91: /* Interactive graphics */
1472 return false;
1473 case 0x92: /* Subtitle */
1474 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_BD_TEXT );
1475 break;
1476 case 0xEA:
1477 es_format_Change( p_fmt, VIDEO_ES, VLC_CODEC_VC1 );
1478 break;
1479 default:
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 );
1482 return false;
1484 return true;
1487 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_es_t *p_es,
1488 const dvbpsi_pmt_es_t *p_dvbpsies )
1490 static const struct
1492 char psz_tag[5];
1493 enum es_format_category_e i_cat;
1494 vlc_fourcc_t i_codec;
1495 } p_regs[] = {
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;
1518 return true;
1521 return false;
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;
1533 break;
1534 case 0x02: /* MPEG-2 video */
1535 case 0x80: /* MPEG-2 MOTO video */
1536 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_MPGV );
1537 break;
1538 case 0x03: /* MPEG-1 audio */
1539 case 0x04: /* MPEG-2 audio */
1540 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_MPGA );
1541 break;
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');
1545 break;
1546 case 0x10: /* MPEG4 (video) */
1547 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_MP4V );
1548 break;
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');
1552 break;
1553 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1554 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_H264 );
1555 break;
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 );
1559 break;
1560 case 0x24: /* HEVC */
1561 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_HEVC );
1562 break;
1563 case 0x42: /* CAVS (Chinese AVS) */
1564 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_CAVS );
1565 break;
1567 case 0x81: /* A52 (audio) */
1568 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_A52 );
1569 break;
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 );
1575 break;
1576 case 0x84: /* SDDS (audio) */
1577 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_SDDS );
1578 break;
1579 case 0x85: /* DTS (audio) FIXME: HDMV Only ? */
1580 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_DTS );
1581 break;
1582 case 0x87: /* E-AC3, ATSC */
1583 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
1584 break;
1585 case 0x8a: /* DTS (audio) */
1586 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_DTS );
1587 break;
1588 case 0x91: /* A52 vls (audio) */
1589 es_format_Change( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1590 break;
1591 case 0x92: /* DVD_SPU vls (sub) */
1592 es_format_Change( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1593 break;
1595 case 0x94: /* SDDS (audio) */
1596 es_format_Change( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1597 break;
1599 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1600 es_format_Change( fmt, UNKNOWN_ES, 0 );
1601 break;
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 */
1606 default:
1607 es_format_Change( fmt, UNKNOWN_ES, 0 );
1608 break;
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 );
1634 else
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 )
1648 case 0x02:
1649 PMTSetupEs0x02( p_pes->p_es, p_dvbpsies );
1650 break;
1651 case 0x05: /* Private data in sections */
1652 p_pes->transport = TS_TRANSPORT_SECTIONS;
1653 PMTSetupEs0x05PrivateData( p_demux, p_pes->p_es, p_dvbpsies );
1654 break;
1655 case 0x06:
1656 /* Handle PES private data */
1657 PMTSetupEs0x06( p_demux, p_pes, p_dvbpsies );
1658 break;
1659 case 0x0a: /* DSM-CC */
1660 case 0x0b:
1661 case 0x0c:
1662 case 0x0d:
1663 p_pes->transport = TS_TRANSPORT_IGNORE;
1664 break;
1665 /* All other private or reserved types */
1666 case 0x13: /* SL in sections */
1667 p_pes->transport = TS_TRANSPORT_SECTIONS;
1668 /* fallthrough */
1669 case 0x0f:
1670 case 0x10:
1671 case 0x11:
1672 case 0x12:
1673 SetupISO14496Descriptors( p_demux, p_pes, p_pmt, p_dvbpsies );
1674 break;
1675 case 0x15:
1676 SetupMetadataDescriptors( p_demux, p_pes, p_dvbpsies );
1677 break;
1678 case 0x1b:
1679 SetupAVCDescriptors( p_demux, p_pes->p_es, p_dvbpsies );
1680 break;
1681 case 0x21:
1682 SetupJ2KDescriptors( p_demux, p_pes->p_es, p_dvbpsies );
1683 break;
1684 case 0x83:
1685 /* LPCM (audio) */
1686 PMTSetupEs0x83( p_dvbpsipmt, p_pes->p_es, p_dvbpsies->i_pid );
1687 break;
1688 case 0xa0:
1689 PMTSetupEs0xA0( p_demux, p_pes->p_es, p_dvbpsies );
1690 break;
1691 case 0xd1:
1692 PMTSetupEs0xD1( p_demux, p_pes->p_es, p_dvbpsies );
1693 break;
1694 case 0xEA:
1695 PMTSetupEs0xEA( p_demux, p_pes->p_es, p_dvbpsies );
1696 default:
1697 break;
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;
1728 else
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) )
1749 return p_en;
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,
1762 p_es->i_type,
1763 p_es->i_pid );
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 );
1775 return p_en;
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);
1813 return;
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 );
1822 return;
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);
1836 if( p_pmt->iod )
1838 ODFree( p_pmt->iod );
1839 p_pmt->iod = NULL;
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, &registration_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 );
1860 break;
1861 case TS_PMT_REGISTRATION_ARIB:
1862 TsChangeStandard( p_sys, TS_STANDARD_ARIB );
1863 break;
1864 case TS_PMT_REGISTRATION_ATSC:
1865 TsChangeStandard( p_sys, TS_STANDARD_ATSC );
1866 break;
1867 default:
1868 if(SEEN(GetPID(p_sys, ATSC_BASE_PID)))
1870 TsChangeStandard( p_sys, TS_STANDARD_ATSC );
1872 else
1874 /* Probe using ES */
1875 p_sys->standard = ProbePMTStandard( p_dvbpsipmt );
1877 break;
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 );
1891 continue;
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 );
1907 if( psz_desc )
1908 msg_Dbg( p_demux, " - ES descriptor %s 0x%x", psz_desc, p_dr->i_tag );
1909 else
1910 msg_Dbg( p_demux, " - ES descriptor tag 0x%x", p_dr->i_tag );
1913 const bool b_pid_inuse = ( pespid->type == TYPE_STREAM );
1914 ts_stream_t *p_pes;
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 );
1920 continue;
1922 else
1924 if( b_pid_inuse ) /* pes will point to a temp */
1926 p_pes = ts_stream_New( p_demux, p_pmt );
1927 if( !p_pes )
1929 PIDRelease( p_demux, pespid );
1930 continue;
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 );
1955 else
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 );
1974 if( p_existing_es )
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 )
2004 if( b_canreuse )
2006 /* Just keep using previous es */
2007 ts_stream_Del( p_demux, p_pes );
2009 else
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 );
2024 assert( p_new );
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 */
2030 if( b_create_es )
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 );
2038 if( p_en )
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",
2064 p_pmt->i_number );
2065 PIDRelease( p_demux, atsc_base_pid );
2067 else
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;
2089 else
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",
2112 p_pmt->i_number );
2113 PIDRelease( p_demux, p_sdt_pid );
2115 else
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;
2161 int i_number;
2163 if( !psz_dup )
2164 return VLC_ENOMEM;
2166 /* Parse PID */
2167 unsigned long i_pid = strtoul( psz, &psz, 0 );
2168 if( i_pid < 2 || i_pid >= 8192 )
2169 goto error;
2171 /* Parse optional program number */
2172 i_number = 0;
2173 if( *psz == ':' )
2174 i_number = strtol( &psz[1], &psz, 0 );
2176 /* */
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) ) )
2181 goto error;
2183 /* Dummy PMT */
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 );
2191 goto error;
2194 ARRAY_APPEND( GetPID(p_sys, 0)->u.p_pat->programs, pmtpid );
2196 psz = strchr( psz, '=' );
2197 if( psz )
2198 psz++;
2199 while( psz && *psz )
2201 char *psz_next = strchr( psz, ',' );
2203 if( psz_next )
2204 *psz_next++ = '\0';
2206 i_pid = strtoul( psz, &psz, 0 );
2207 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
2208 goto next;
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, '=' );
2220 if( psz_arg )
2221 *psz_arg++ = '\0';
2223 if ( !PIDSetup( p_demux, TYPE_STREAM, pid, pmtpid ) )
2224 continue;
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" ) )
2240 i_cat = VIDEO_ES;
2241 else if( !strcmp( psz_opt, "audio" ) )
2242 i_cat = AUDIO_ES;
2243 else if( !strcmp( psz_opt, "spu" ) )
2244 i_cat = SPU_ES;
2246 es_format_Change( fmt, i_cat, i_codec );
2247 fmt->b_packetized = false;
2249 else
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 )
2257 fmt->i_id = i_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 );
2264 p_sys->i_pmt_es++;
2268 next:
2269 psz = psz_next;
2272 p_sys->b_user_pmt = true;
2273 free( psz_dup );
2274 return VLC_SUCCESS;
2276 error:
2277 free( psz_dup );
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) )
2284 return false;
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 );