demux: ts: add support for multiview descriptor
[vlc.git] / modules / demux / mpeg / ts_psi.c
blob37ebab855503210174e30e5cb2c5713b279479c7
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"
50 #include "sections.h"
51 #include "ts_sl.h"
52 #include "ts_scte.h"
53 #include "ts_psip.h"
54 #include "ts_si.h"
55 #include "ts_metadata.h"
57 #include "../access/dtv/en50221_capmt.h"
59 #include <assert.h>
61 static void PIDFillFormat( demux_t *, ts_stream_t *p_pes, int i_stream_type, ts_transport_type_t * );
62 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_dvbpsipmt );
63 static ts_standards_e ProbePMTStandard( const dvbpsi_pmt_t *p_dvbpsipmt );
65 static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
67 /* Some Dreambox streams have all PMT set to same pid */
68 int i_prev_pid = -1;
69 for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
70 p_program != NULL;
71 p_program = p_program->p_next )
73 if( p_program->i_pid == i_prev_pid )
75 msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
76 return VLC_EGENERIC;
78 i_prev_pid = p_program->i_pid;
80 return VLC_SUCCESS;
83 static void PATCallBack( void *data, dvbpsi_pat_t *p_dvbpsipat )
85 demux_t *p_demux = data;
86 demux_sys_t *p_sys = p_demux->p_sys;
87 dvbpsi_pat_program_t *p_program;
88 ts_pid_t *patpid = GetPID(p_sys, 0);
89 ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
91 patpid->i_flags |= FLAG_SEEN;
93 msg_Dbg( p_demux, "PATCallBack called" );
95 if(unlikely( GetPID(p_sys, 0)->type != TYPE_PAT ))
97 msg_Warn( p_demux, "PATCallBack called on invalid pid" );
98 return;
101 if( ( p_pat->i_version != -1 &&
102 ( !p_dvbpsipat->b_current_next ||
103 p_dvbpsipat->i_version == p_pat->i_version ) ) ||
104 ( p_pat->i_ts_id != -1 && p_dvbpsipat->i_ts_id != p_pat->i_ts_id ) ||
105 p_sys->b_user_pmt || PATCheck( p_demux, p_dvbpsipat ) )
107 dvbpsi_pat_delete( p_dvbpsipat );
108 return;
111 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
112 p_dvbpsipat->i_ts_id, p_dvbpsipat->i_version, p_dvbpsipat->b_current_next );
114 /* Save old programs array */
115 DECL_ARRAY(ts_pid_t *) old_pmt_rm;
116 old_pmt_rm.i_alloc = p_pat->programs.i_alloc;
117 old_pmt_rm.i_size = p_pat->programs.i_size;
118 old_pmt_rm.p_elems = p_pat->programs.p_elems;
119 ARRAY_INIT(p_pat->programs);
121 /* now create programs */
122 for( p_program = p_dvbpsipat->p_first_program; p_program != NULL;
123 p_program = p_program->p_next )
125 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
126 p_program->i_pid );
127 if( p_program->i_number == 0 )
128 continue;
130 ts_pid_t *pmtpid = GetPID(p_sys, p_program->i_pid);
132 bool b_existing = (pmtpid->type == TYPE_PMT);
133 /* create or temporary incref pid */
134 if( !PIDSetup( p_demux, TYPE_PMT, pmtpid, patpid ) )
136 msg_Warn( p_demux, " * number=%d pid=%d (ignored)", p_program->i_number,
137 p_program->i_pid );
138 continue;
141 if( !b_existing || pmtpid->u.p_pmt->i_number != p_program->i_number )
143 if( b_existing && pmtpid->u.p_pmt->i_number != p_program->i_number )
144 dvbpsi_pmt_detach(pmtpid->u.p_pmt->handle);
146 if( !dvbpsi_pmt_attach( pmtpid->u.p_pmt->handle, p_program->i_number, PMTCallBack, p_demux ) )
147 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
148 p_program->i_number );
151 pmtpid->u.p_pmt->i_number = p_program->i_number;
153 ARRAY_APPEND( p_pat->programs, pmtpid );
155 /* Now select PID at access level */
156 if( p_sys->programs.i_size == 0 ||
157 ProgramIsSelected( p_sys, p_program->i_number ) )
159 if( p_sys->programs.i_size == 0 )
161 msg_Dbg( p_demux, "temporary receiving program %d", p_program->i_number );
162 p_sys->b_default_selection = true;
163 ARRAY_APPEND( p_sys->programs, p_program->i_number );
166 SetPIDFilter( p_sys, pmtpid, true );
168 if ( p_sys->es_creation == DELAY_ES )
169 p_sys->es_creation = CREATE_ES;
172 p_pat->i_version = p_dvbpsipat->i_version;
173 p_pat->i_ts_id = p_dvbpsipat->i_ts_id;
175 for(int i=0; i<old_pmt_rm.i_size; i++)
177 /* decref current or release now unreferenced */
178 PIDRelease( p_demux, old_pmt_rm.p_elems[i] );
180 ARRAY_RESET(old_pmt_rm);
182 dvbpsi_pat_delete( p_dvbpsipat );
185 #define PMT_DESC_PREFIX " * PMT descriptor: "
186 #define PMT_DESC_INDENT " : "
187 static void ParsePMTRegistrations( demux_t *p_demux, const dvbpsi_descriptor_t *p_firstdr,
188 ts_pmt_t *p_pmt, ts_pmt_registration_type_t *p_registration_type )
190 demux_sys_t *p_sys = p_demux->p_sys;
191 ts_pmt_registration_type_t registration_type = *p_registration_type;
192 int i_arib_score_flags = 0; /* Descriptors can be repeated */
194 for( const dvbpsi_descriptor_t *p_dr = p_firstdr; p_dr != NULL; p_dr = p_dr->p_next )
196 /* general descriptors handling < 0x40 and scoring */
197 if( p_dr->i_tag < 0x40 )
199 msg_Dbg( p_demux, PMT_DESC_PREFIX "%s (0x%x)",
200 ISO13818_1_Get_Descriptor_Description(p_dr->i_tag), p_dr->i_tag );
203 switch(p_dr->i_tag)
205 case 0x05: /* Registration Descriptor */
207 if( p_dr->i_length != 4 )
209 msg_Warn( p_demux, PMT_DESC_INDENT "invalid registration descriptor" );
210 break;
213 static const struct
215 const char rgs[4];
216 const ts_pmt_registration_type_t reg;
217 } regs[] = {
218 { { 'H', 'D', 'M', 'V' }, TS_PMT_REGISTRATION_BLURAY },
219 { { 'H', 'D', 'P', 'R' }, TS_PMT_REGISTRATION_BLURAY },
220 { { 'G', 'A', '9', '4' }, TS_PMT_REGISTRATION_ATSC },
223 for( unsigned i=0; i<ARRAY_SIZE(regs); i++ )
225 if( !memcmp( regs[i].rgs, p_dr->p_data, 4 ) )
227 registration_type = regs[i].reg;
228 msg_Dbg( p_demux, PMT_DESC_INDENT "%4.4s registration", p_dr->p_data );
229 break;
233 break;
235 case 0x09:
237 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( (dvbpsi_descriptor_t *) p_dr );
238 msg_Dbg( p_demux, PMT_DESC_INDENT "CA System ID 0x%x", p_cadr->i_ca_system_id );
239 i_arib_score_flags |= (p_cadr->i_ca_system_id == 0x05);
241 break;
243 case 0x1d: /* We have found an IOD descriptor */
244 p_pmt->iod = IODNew( VLC_OBJECT(p_demux), p_dr->i_length, p_dr->p_data );
245 break;
247 case 0xC1:
248 i_arib_score_flags |= 1 << 2;
249 break;
251 case 0xF6:
252 i_arib_score_flags |= 1 << 1;
253 break;
255 default:
256 break;
260 if ( p_sys->standard == TS_STANDARD_AUTO &&
261 registration_type == TS_PMT_REGISTRATION_NONE &&
262 i_arib_score_flags == 0x07 ) //0b111
264 registration_type = TS_PMT_REGISTRATION_ARIB;
267 *p_registration_type = registration_type;
270 static void ParsePMTPrivateRegistrations( demux_t *p_demux, const dvbpsi_descriptor_t *p_firstdr,
271 ts_pmt_t *p_pmt, ts_standards_e i_standard )
273 VLC_UNUSED(p_pmt);
274 /* Now process private descriptors >= 0x40 */
275 for( const dvbpsi_descriptor_t *p_dr = p_firstdr; p_dr != NULL; p_dr = p_dr->p_next )
277 if( p_dr->i_tag < 0x40 )
278 continue;
280 switch( i_standard )
282 case TS_STANDARD_ARIB:
284 const char *psz_desc = ARIB_B10_Get_PMT_Descriptor_Description( p_dr->i_tag );
285 if( psz_desc )
286 msg_Dbg( p_demux, PMT_DESC_PREFIX "%s (0x%x)", psz_desc, p_dr->i_tag );
287 else
288 msg_Dbg( p_demux, PMT_DESC_PREFIX "Unknown Private (0x%x)", p_dr->i_tag );
291 case TS_STANDARD_DVB:
292 case TS_STANDARD_AUTO:
294 if( p_dr->i_tag == 0x88 )
296 /* EACEM Simulcast HD Logical channels ordering */
297 /* TODO: apply visibility flags */
298 msg_Dbg( p_demux, PMT_DESC_PREFIX "EACEM Simulcast HD" );
299 break;
301 //ft
303 default:
304 msg_Dbg( p_demux, PMT_DESC_PREFIX "Unknown Private (0x%x)", p_dr->i_tag );
305 break;
310 /*****************************************************************************
311 * PMT callback and helpers
312 *****************************************************************************/
313 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
314 int i_tag )
316 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
317 while( p_dr && ( p_dr->i_tag != i_tag ) )
318 p_dr = p_dr->p_next;
319 return p_dr;
321 static bool PMTEsHasRegistration( demux_t *p_demux,
322 const dvbpsi_pmt_es_t *p_es,
323 const char *psz_tag )
325 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
326 if( !p_dr )
327 return false;
329 if( p_dr->i_length < 4 )
331 msg_Warn( p_demux, "invalid Registration Descriptor" );
332 return false;
335 assert( strlen(psz_tag) == 4 );
336 return !memcmp( p_dr->p_data, psz_tag, 4 );
339 static bool PMTEsHasComponentTagBetween( const dvbpsi_pmt_es_t *p_es,
340 uint8_t i_low, uint8_t i_high )
342 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
343 if( !p_dr )
344 return false;
345 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
346 if( !p_si )
347 return false;
349 return p_si->i_component_tag >= i_low && p_si->i_component_tag <= i_high;
352 static ts_standards_e ProbePMTStandard( const dvbpsi_pmt_t *p_dvbpsipmt )
354 dvbpsi_pmt_es_t *p_dvbpsies;
355 for( p_dvbpsies = p_dvbpsipmt->p_first_es; p_dvbpsies; p_dvbpsies = p_dvbpsies->p_next )
357 if( p_dvbpsies->i_type == 0x06 )
359 /* Probe for ARIB subtitles */
360 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xFD );
361 if( p_dr && p_dr->i_length >= 2 )
363 const uint16_t i_data_component_id = GetWBE(p_dr->p_data);
364 if( ( i_data_component_id == 0x0008 &&
365 PMTEsHasComponentTagBetween( p_dvbpsies, 0x30, 0x37 ) ) ||
366 ( i_data_component_id == 0x0012 &&
367 PMTEsHasComponentTagBetween( p_dvbpsies, 0x87, 0x88 ) ) )
368 return TS_STANDARD_ARIB;
372 return TS_STANDARD_AUTO;
375 static void SetupAudioExtendedDescriptors( demux_t *p_demux, ts_es_t *p_es,
376 const dvbpsi_pmt_es_t *p_dvbpsies )
378 if( p_demux->p_sys->standard == TS_STANDARD_AUTO ||
379 p_demux->p_sys->standard == TS_STANDARD_DVB )
381 const dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x7F );
382 if( p_dr && p_dr->i_length > 1 && p_dr->p_data[0] == 0x06 /* Tag extension */ )
384 static const char *editorial_classification_coding[] = {
385 N_("Main audio"),
386 N_("Audio description for the visually impaired"),
387 N_("Clean audio for the hearing impaired"),
388 N_("Spoken subtitles for the visually impaired"),
391 uint8_t i_audio_type = (p_dr->p_data[1] & 0x7F) >> 2;
393 if( i_audio_type < ARRAY_SIZE(editorial_classification_coding) )
395 free( p_es->fmt.psz_description );
396 p_es->fmt.psz_description = strdup(editorial_classification_coding[i_audio_type]);
399 if( i_audio_type == 0x00 /* Main Audio */ )
400 p_es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1;
402 if( (p_dr->p_data[1] & 0x80) == 0x00 ) /* Split mixed */
403 p_es->fmt.i_priority = ES_PRIORITY_NOT_DEFAULTABLE;
405 if( (p_dr->p_data[1] & 0x01) && p_dr->i_length >= 5 )
407 free( p_es->fmt.psz_language );
408 p_es->fmt.psz_language = malloc( 4 );
409 if( p_es->fmt.psz_language )
411 memcpy( p_es->fmt.psz_language, &p_dr->p_data[2], 3 );
412 p_es->fmt.psz_language[3] = 0;
413 msg_Dbg( p_demux, " found language: %s", p_es->fmt.psz_language );
420 static void SetupISO14496Descriptors( demux_t *p_demux, ts_stream_t *p_pes,
421 const ts_pmt_t *p_pmt, const dvbpsi_pmt_es_t *p_dvbpsies )
423 const dvbpsi_descriptor_t *p_dr = p_dvbpsies->p_first_descriptor;
424 ts_es_t *p_es = p_pes->p_es;
426 while( p_dr )
428 uint8_t i_length = p_dr->i_length;
430 switch( p_dr->i_tag )
432 case 0x1f: /* FMC Descriptor */
433 while( i_length >= 2 /* see below */ && !p_es->i_sl_es_id )
435 p_es->i_sl_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
436 /* FIXME: map all ids and flexmux channels */
437 /* Handle broken streams with 2 byte 0x1F descriptors
438 * see samples/A-codecs/AAC/freetv_aac_latm.txt */
439 if( i_length == 2 )
440 break;
441 i_length -= 3;
442 msg_Dbg( p_demux, " - found FMC_descriptor mapping es_id=%"PRIu16, p_es->i_sl_es_id );
444 break;
445 case 0x1e: /* SL Descriptor */
446 if( i_length == 2 )
448 p_es->i_sl_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
449 msg_Dbg( p_demux, " - found SL_descriptor mapping es_id=%"PRIu16, p_es->i_sl_es_id );
451 if( p_dvbpsies->i_type == 0x12 ) /* SL AU pes stream */
453 if( !p_pes->p_proc )
454 p_pes->p_proc = SL_stream_processor_New( p_pes );
456 else if( p_dvbpsies->i_type == 0x13 ) /* IOD / SL sections */
458 ts_sections_processor_Add( p_demux,
459 &p_pes->p_sections_proc, 0x05, 0x00,
460 SLPackets_Section_Handler, p_pes );
462 p_pes->b_always_receive = true;
464 break;
465 default:
466 break;
469 p_dr = p_dr->p_next;
472 if( p_es->i_sl_es_id )
474 const es_mpeg4_descriptor_t *p_mpeg4desc = GetMPEG4DescByEsId( p_pmt, p_es->i_sl_es_id );
475 if( p_mpeg4desc && p_mpeg4desc->b_ok )
477 if( !SetupISO14496LogicalStream( p_demux, &p_mpeg4desc->dec_descr, &p_es->fmt ) )
478 msg_Dbg( p_demux, " - IOD not yet available for es_id=%"PRIu16, p_es->i_sl_es_id );
481 else
483 switch( p_dvbpsies->i_type )
485 /* non fatal, set by packetizer */
486 case 0x0f: /* ADTS */
487 case 0x11: /* LOAS */
488 msg_Dbg( p_demux, " - SL/FMC descriptor not found/matched" );
489 break;
490 default:
491 msg_Err( p_demux, " - SL/FMC descriptor not found/matched" );
492 break;
497 static void SetupMetadataDescriptors( demux_t *p_demux, ts_stream_t *p_stream, const dvbpsi_pmt_es_t *p_dvbpsies )
499 ts_es_t *p_es = p_stream->p_es;
500 const dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x26 );
501 if( p_dr && p_dr->i_length >= 13 )
503 /* app format 0xFFFF
504 * metadata_application_format_identifier ID3\x20
505 * i_metadata_format 0xFF
506 * metadata_format_identifier ID3\x20 */
507 if( !memcmp( p_dr->p_data, "\xFF\xFFID3 \xFFID3 ", 11 ) &&
508 (p_dr->p_data[12] & 0xF0) == 0x00 )
510 p_es->metadata.i_format = VLC_FOURCC('I', 'D', '3', ' ');
511 p_es->metadata.i_service_id = p_dr->p_data[11];
512 msg_Dbg( p_demux, " - found Metadata_descriptor type ID3 with service_id=0x%"PRIx8,
513 p_dr->p_data[11] );
514 if( !p_stream->p_proc )
515 p_stream->p_proc = Metadata_stream_processor_New( p_stream, p_demux->out );
520 static void SetupAVCDescriptors( demux_t *p_demux, ts_es_t *p_es, const dvbpsi_pmt_es_t *p_dvbpsies )
522 const dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x28 );
523 if( p_dr && p_dr->i_length >= 4 )
525 p_es->fmt.i_profile = p_dr->p_data[0];
526 p_es->fmt.i_level = p_dr->p_data[2];
527 msg_Dbg( p_demux, " - found AVC_video_descriptor profile=0x%"PRIx8" level=0x%"PRIx8,
528 p_es->fmt.i_profile, p_es->fmt.i_level );
532 static void SetupJ2KDescriptors( demux_t *p_demux, ts_es_t *p_es, const dvbpsi_pmt_es_t *p_dvbpsies )
534 const dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x32 );
535 if( p_dr && p_dr->i_length >= 24 )
537 es_format_Change( &p_es->fmt, VIDEO_ES, VLC_CODEC_JPEG2000 );
538 p_es->fmt.i_profile = p_dr->p_data[0];
539 p_es->fmt.i_level = p_dr->p_data[1];
540 p_es->fmt.video.i_width = GetDWBE(&p_dr->p_data[2]);
541 p_es->fmt.video.i_height = GetDWBE(&p_dr->p_data[6]);
542 p_es->fmt.video.i_frame_rate_base = GetWBE(&p_dr->p_data[18]);
543 p_es->fmt.video.i_frame_rate = GetWBE(&p_dr->p_data[20]);
544 j2k_fill_color_profile( p_dr->p_data[21],
545 &p_es->fmt.video.primaries,
546 &p_es->fmt.video.transfer,
547 &p_es->fmt.video.space );
548 p_es->b_interlaced = p_dr->p_data[23] & 0x40;
549 if( p_dr->i_length > 24 )
551 p_es->fmt.p_extra = malloc(p_dr->i_length - 24);
552 if( p_es->fmt.p_extra )
553 p_es->fmt.i_extra = p_dr->i_length - 24;
555 msg_Dbg( p_demux, " - found J2K_video_descriptor profile=0x%"PRIx8" level=0x%"PRIx8,
556 p_es->fmt.i_profile, p_es->fmt.i_level );
560 typedef struct
562 int i_type;
563 int i_magazine;
564 int i_page;
565 char p_iso639[3];
566 } ts_teletext_page_t;
568 static const char *const ppsz_teletext_type[] = {
570 N_("Teletext"),
571 N_("Teletext subtitles"),
572 N_("Teletext: additional information"),
573 N_("Teletext: program schedule"),
574 N_("Teletext subtitles: hearing impaired")
577 static void PMTSetupEsTeletext( demux_t *p_demux, ts_stream_t *p_pes,
578 const dvbpsi_pmt_es_t *p_dvbpsies )
580 es_format_t *p_fmt = &p_pes->p_es->fmt;
582 ts_teletext_page_t p_page[2 * 64 + 20];
583 unsigned i_page = 0;
585 /* Gather pages information */
586 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
588 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, i_tag_idx == 0 ? 0x46 : 0x56 );
589 if( !p_dr )
590 continue;
592 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
593 if( !p_sub )
594 continue;
596 for( int i = 0; i < p_sub->i_pages_number; i++ )
598 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
600 if( p_src->i_teletext_type >= 0x06 )
601 continue;
603 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
605 ts_teletext_page_t *p_dst = &p_page[i_page++];
607 p_dst->i_type = p_src->i_teletext_type;
608 p_dst->i_magazine = p_src->i_teletext_magazine_number
609 ? p_src->i_teletext_magazine_number : 8;
610 p_dst->i_page = p_src->i_teletext_page_number;
611 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
615 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
616 if( p_dr )
618 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
619 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
621 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
623 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
624 continue;
626 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
628 ts_teletext_page_t *p_dst = &p_page[i_page++];
630 switch( p_src->i_subtitling_type )
632 case 0x01:
633 p_dst->i_type = 0x02;
634 break;
635 default:
636 p_dst->i_type = 0x03;
637 break;
639 /* FIXME check if it is the right split */
640 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
641 ? (p_src->i_composition_page_id >> 8) : 8;
642 p_dst->i_page = p_src->i_composition_page_id & 0xff;
643 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
647 /* */
648 es_format_Change(p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
650 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
652 p_fmt->subs.teletext.i_magazine = -1;
653 p_fmt->subs.teletext.i_page = 0;
654 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
656 dvbpsi_descriptor_t *p_dr;
657 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x46 );
658 if( !p_dr )
659 p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x56 );
661 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
663 /* Descriptor pass-through */
664 p_fmt->p_extra = malloc( p_dr->i_length );
665 if( p_fmt->p_extra )
667 p_fmt->i_extra = p_dr->i_length;
668 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
672 else
674 for( unsigned i = 0; i < i_page; i++ )
676 ts_es_t *p_page_es;
678 /* */
679 if( i == 0 )
681 p_page_es = p_pes->p_es;
683 else
685 p_page_es = ts_es_New( p_pes->p_es->p_program );
686 if( !p_page_es )
687 break;
689 es_format_Copy( &p_page_es->fmt, p_fmt );
690 free( p_page_es->fmt.psz_language );
691 free( p_page_es->fmt.psz_description );
692 p_page_es->fmt.psz_language = NULL;
693 p_page_es->fmt.psz_description = NULL;
694 ts_stream_Add_es( p_pes, p_page_es, true );
697 /* */
698 const ts_teletext_page_t *p = &p_page[i];
699 p_page_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
700 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
701 p_page_es->fmt.psz_language = strndup( p->p_iso639, 3 );
702 p_page_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
703 p_page_es->fmt.subs.teletext.i_magazine = p->i_magazine;
704 p_page_es->fmt.subs.teletext.i_page = p->i_page;
706 msg_Dbg( p_demux,
707 " * ttxt type=%s lan=%s page=%d%02x",
708 p_page_es->fmt.psz_description,
709 p_page_es->fmt.psz_language,
710 p->i_magazine, p->i_page );
714 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_stream_t *p_pes,
715 const dvbpsi_pmt_es_t *p_dvbpsies )
717 es_format_t *p_fmt = &p_pes->p_es->fmt;
719 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_DVBS );
721 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
722 int i_page = 0;
723 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
724 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
726 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
727 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
728 ( i_type >= 0x20 && i_type <= 0x24 ) )
729 i_page++;
732 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
734 p_fmt->subs.dvb.i_id = -1;
735 p_fmt->psz_description = strdup( _("DVB subtitles") );
737 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
739 /* Descriptor pass-through */
740 p_fmt->p_extra = malloc( p_dr->i_length );
741 if( p_fmt->p_extra )
743 p_fmt->i_extra = p_dr->i_length;
744 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
748 else
750 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
752 ts_es_t *p_subs_es;
754 /* */
755 if( i == 0 )
757 p_subs_es = p_pes->p_es;
759 else
761 p_subs_es = ts_es_New( p_pes->p_es->p_program );
762 if( !p_subs_es )
763 break;
765 es_format_Copy( &p_subs_es->fmt, p_fmt );
766 free( p_subs_es->fmt.psz_language );
767 free( p_subs_es->fmt.psz_description );
768 p_subs_es->fmt.psz_language = NULL;
769 p_subs_es->fmt.psz_description = NULL;
771 ts_stream_Add_es( p_pes, p_subs_es, true );
774 /* */
775 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
776 p_subs_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
777 switch( p->i_subtitling_type )
779 case 0x10: /* unspec. */
780 case 0x11: /* 4:3 */
781 case 0x12: /* 16:9 */
782 case 0x13: /* 2.21:1 */
783 case 0x14: /* HD monitor */
784 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles") );
785 break;
786 case 0x20: /* Hearing impaired unspec. */
787 case 0x21: /* h.i. 4:3 */
788 case 0x22: /* h.i. 16:9 */
789 case 0x23: /* h.i. 2.21:1 */
790 case 0x24: /* h.i. HD monitor */
791 p_subs_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
792 break;
793 default:
794 break;
797 /* Hack, FIXME */
798 p_subs_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
799 ( p->i_ancillary_page_id << 16 );
804 static int vlc_ceil_log2( const unsigned int val )
806 int n = 31 - clz(val);
807 if ((1U << n) != val)
808 n++;
810 return n;
813 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
815 OpusHeader h;
817 /* default mapping */
818 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
819 memcpy(h.stream_map, map, sizeof(map));
821 int csc, mapping;
822 int channels = 0;
823 int stream_count = 0;
824 int ccc = p[1]; // channel_config_code
825 if (ccc <= 8) {
826 channels = ccc;
827 if (channels)
828 mapping = channels > 2;
829 else {
830 mapping = 255;
831 channels = 2; // dual mono
833 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
834 csc = p_csc[channels - 1];
835 stream_count = channels - csc;
837 static const uint8_t maps[6][7] = {
838 { 2,1 },
839 { 1,2,3 },
840 { 4,1,2,3 },
841 { 4,1,2,3,5 },
842 { 4,1,2,3,5,6 },
843 { 6,1,2,3,4,5,7 },
845 if (channels > 2)
846 memcpy(&h.stream_map[1], maps[channels-3], channels - 1);
847 } else if (ccc == 0x81) {
848 if (len < 4)
849 goto explicit_config_too_short;
851 channels = p[2];
852 mapping = p[3];
853 csc = 0;
854 if (mapping) {
855 bs_t s;
856 bs_init(&s, &p[4], len - 4);
857 stream_count = 1;
858 if (channels) {
859 int bits = vlc_ceil_log2(channels);
860 if (s.i_left < bits)
861 goto explicit_config_too_short;
862 stream_count = bs_read(&s, bits) + 1;
863 bits = vlc_ceil_log2(stream_count + 1);
864 if (s.i_left < bits)
865 goto explicit_config_too_short;
866 csc = bs_read(&s, bits);
868 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
869 if (s.i_left < channels * channel_bits)
870 goto explicit_config_too_short;
872 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
873 for (int i = 0; i < channels; i++) {
874 unsigned char m = bs_read(&s, channel_bits);
875 if (m == silence)
876 m = 0xff;
877 h.stream_map[i] = m;
880 } else if (ccc >= 0x80 && ccc <= 0x88) {
881 channels = ccc - 0x80;
882 if (channels)
883 mapping = 1;
884 else {
885 mapping = 255;
886 channels = 2; // dual mono
888 csc = 0;
889 stream_count = channels;
890 } else {
891 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
894 if (!channels) {
895 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
896 return;
899 opus_prepare_header(channels, 0, &h);
900 h.preskip = 0;
901 h.input_sample_rate = 48000;
902 h.nb_coupled = csc;
903 h.nb_streams = channels - csc;
904 h.channel_mapping = mapping;
906 if (h.channels) {
907 uint8_t *p_extra = NULL;
908 int i_extra = 0;
909 opus_write_header(&p_extra, &i_extra, &h, NULL /* FIXME */);
910 if (p_extra) {
911 es_format_Change(p_fmt, AUDIO_ES, VLC_CODEC_OPUS);
912 p_fmt->p_extra = p_extra;
913 p_fmt->i_extra = i_extra;
914 p_fmt->audio.i_channels = h.channels;
915 p_fmt->audio.i_rate = 48000;
919 return;
921 explicit_config_too_short:
922 msg_Err(demux, "Opus descriptor too short");
925 static void PMTSetupEs0x02( ts_es_t *p_es,
926 const dvbpsi_pmt_es_t *p_dvbpsies )
928 /* MPEG2_stereoscopic_video_format_descriptor */
929 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x34 );
930 if( p_dr && p_dr->i_length > 0 && (p_dr->p_data[0] & 0x80) )
932 video_multiview_mode_t mode;
933 switch( p_dr->p_data[0] & 0x7F )
935 case 0x03:
936 mode = MULTIVIEW_STEREO_SBS; break;
937 case 0x04:
938 mode = MULTIVIEW_STEREO_TB; break;
939 case 0x08:
940 default:
941 mode = MULTIVIEW_2D; break;
943 p_es->fmt.video.multiview_mode = mode;
947 static void PMTSetupEs0x05PrivateData( demux_t *p_demux, ts_es_t *p_es,
948 const dvbpsi_pmt_es_t *p_dvbpsies )
950 VLC_UNUSED(p_es);
951 if( p_demux->p_sys->standard == TS_STANDARD_DVB ||
952 p_demux->p_sys->standard == TS_STANDARD_AUTO )
954 dvbpsi_descriptor_t *p_ait_dr = PMTEsFindDescriptor( p_dvbpsies, 0x6F );
955 if( p_ait_dr )
957 uint8_t *p_data = p_ait_dr->p_data;
958 for( uint8_t i_data = p_ait_dr->i_length; i_data >= 3; i_data -= 3, p_data += 3 )
960 uint16_t i_app_type = ((p_data[0] & 0x7F) << 8) | p_data[1];
961 msg_Dbg( p_demux, " - Application type 0x%"PRIx16" version %"PRIu8,
962 i_app_type, p_data[2] & 0x1F);
968 static void PMTSetupEs0x06( demux_t *p_demux, ts_stream_t *p_pes,
969 const dvbpsi_pmt_es_t *p_dvbpsies )
971 es_format_t *p_fmt = &p_pes->p_es->fmt;
972 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_dvbpsies, 0x59 );
973 dvbpsi_descriptor_t *desc;
974 if( PMTEsHasRegistration( p_demux, p_dvbpsies, "EAC3" ) ||
975 PMTEsFindDescriptor( p_dvbpsies, 0x7a ) )
977 /* DVB with stream_type 0x06 (ETS EN 300 468) */
978 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_EAC3 );
980 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "AC-3" ) ||
981 PMTEsFindDescriptor( p_dvbpsies, 0x6a ) ||
982 PMTEsFindDescriptor( p_dvbpsies, 0x81 ) ) /* AC-3 channel (also in EAC3) */
984 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_A52 );
986 else if( (desc = PMTEsFindDescriptor( p_dvbpsies, 0x7f ) ) &&
987 desc->i_length >= 2 && desc->p_data[0] == 0x80 &&
988 PMTEsHasRegistration(p_demux, p_dvbpsies, "Opus"))
990 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
992 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS1" ) || /* 512 Bpf */
993 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS2" ) || /* 1024 Bpf */
994 PMTEsHasRegistration( p_demux, p_dvbpsies, "DTS3" ) || /* 2048 Bpf */
995 PMTEsFindDescriptor( p_dvbpsies, 0x73 ) )
997 /*registration descriptor(ETSI TS 101 154 Annex F)*/
998 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_DTS );
1000 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "BSSD" ) && !p_subs_dr )
1002 /* BSSD is AES3 DATA, but could also be subtitles
1003 * we need to check for secondary descriptor then s*/
1004 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_302M );
1005 p_fmt->b_packetized = true;
1007 else if( PMTEsHasRegistration( p_demux, p_dvbpsies, "HEVC" ) )
1009 es_format_Change( p_fmt, VIDEO_ES, VLC_CODEC_HEVC );
1011 else if ( p_demux->p_sys->standard == TS_STANDARD_ARIB )
1013 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
1014 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xFD );
1015 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
1016 if ( p_dr && p_dr->i_length >= 2 )
1018 /* See STD-B10 Annex J, table J-1 mappings */
1019 const uint16_t i_data_component_id = GetWBE(p_dr->p_data);
1020 if( i_data_component_id == 0x0008 &&
1021 PMTEsHasComponentTagBetween( p_dvbpsies, 0x30, 0x37 ) )
1023 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_ARIB_A );
1024 p_fmt->psz_language = strndup ( "jpn", 3 );
1025 p_fmt->psz_description = strdup( _("ARIB subtitles") );
1027 else if( i_data_component_id == 0x0012 &&
1028 PMTEsHasComponentTagBetween( p_dvbpsies, 0x87, 0x88 ) )
1030 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_ARIB_C );
1031 p_fmt->psz_language = strndup ( "jpn", 3 );
1032 p_fmt->psz_description = strdup( _("ARIB subtitles") );
1036 else
1038 /* Subtitle/Teletext/VBI fallbacks */
1039 dvbpsi_subtitling_dr_t *p_sub;
1040 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
1042 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
1044 if( p_fmt->i_cat != UNKNOWN_ES )
1045 break;
1047 switch( p_sub->p_subtitle[i].i_subtitling_type )
1049 case 0x01: /* EBU Teletext subtitles */
1050 case 0x02: /* Associated EBU Teletext */
1051 case 0x03: /* VBI data */
1052 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
1053 break;
1054 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
1055 case 0x11: /* ... on 4:3 AR monitor */
1056 case 0x12: /* ... on 16:9 AR monitor */
1057 case 0x13: /* ... on 2.21:1 AR monitor */
1058 case 0x14: /* ... for display on a high definition monitor */
1059 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
1060 case 0x21: /* ... on 4:3 AR monitor */
1061 case 0x22: /* ... on 16:9 AR monitor */
1062 case 0x23: /* ... on 2.21:1 AR monitor */
1063 case 0x24: /* ... for display on a high definition monitor */
1064 PMTSetupEsDvbSubtitle( p_demux, p_pes, p_dvbpsies );
1065 break;
1066 default:
1067 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
1068 p_sub->p_subtitle[i].i_subtitling_type );
1069 break;
1074 if( p_fmt->i_cat == UNKNOWN_ES &&
1075 ( PMTEsFindDescriptor( p_dvbpsies, 0x45 ) || /* VBI Data descriptor */
1076 PMTEsFindDescriptor( p_dvbpsies, 0x46 ) || /* VBI Teletext descriptor */
1077 PMTEsFindDescriptor( p_dvbpsies, 0x56 ) ) ) /* EBU Teletext descriptor */
1079 /* Teletext/VBI */
1080 PMTSetupEsTeletext( p_demux, p_pes, p_dvbpsies );
1084 /* FIXME is it useful ? */
1085 if( PMTEsFindDescriptor( p_dvbpsies, 0x52 ) )
1087 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x52 );
1088 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
1090 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
1094 static void PMTSetupEs0xEA( demux_t *p_demux, ts_es_t *p_es,
1095 const dvbpsi_pmt_es_t *p_dvbpsies )
1097 /* Registration Descriptor */
1098 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "VC-1" ) )
1100 msg_Err( p_demux, "Registration descriptor not found or invalid" );
1101 return;
1104 /* registration descriptor for VC-1 (SMPTE rp227) */
1105 es_format_Change( &p_es->fmt, VIDEO_ES, VLC_CODEC_VC1 );
1107 /* XXX With Simple and Main profile the SEQUENCE
1108 * header is modified: video width and height are
1109 * inserted just after the start code as 2 int16_t
1110 * The packetizer will take care of that. */
1113 static void PMTSetupEs0xD1( demux_t *p_demux, ts_es_t *p_es,
1114 const dvbpsi_pmt_es_t *p_dvbpsies )
1116 /* Registration Descriptor */
1117 if( !PMTEsHasRegistration( p_demux, p_dvbpsies, "drac" ) )
1119 msg_Err( p_demux, "Registration descriptor not found or invalid" );
1120 return;
1123 /* registration descriptor for Dirac
1124 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
1125 es_format_Change( &p_es->fmt, VIDEO_ES, VLC_CODEC_DIRAC );
1128 static void PMTSetupEs0xA0( demux_t *p_demux, ts_es_t *p_es,
1129 const dvbpsi_pmt_es_t *p_dvbpsies )
1131 /* MSCODEC sent by vlc */
1132 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0xa0 );
1133 if( !p_dr || p_dr->i_length < 10 )
1135 msg_Warn( p_demux,
1136 "private MSCODEC (vlc) without bih private descriptor" );
1137 return;
1140 es_format_t *p_fmt = &p_es->fmt;
1141 es_format_Change( &p_es->fmt, VIDEO_ES,
1142 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
1143 p_dr->p_data[2], p_dr->p_data[3] ) );
1144 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
1145 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
1146 p_fmt->video.i_visible_width = p_fmt->video.i_width;
1147 p_fmt->video.i_visible_height = p_fmt->video.i_height;
1148 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
1150 if( p_fmt->i_extra > 0 )
1152 p_fmt->p_extra = malloc( p_fmt->i_extra );
1153 if( p_fmt->p_extra )
1154 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
1155 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
1156 else
1157 p_fmt->i_extra = 0;
1159 /* For such stream we will gather them ourself and don't launch a
1160 * packetizer.
1161 * Yes it's ugly but it's the only way to have DIV3 working */
1162 p_fmt->b_packetized = true;
1165 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_es_t *p_es, int i_pid )
1167 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
1168 * with audio track pid being 0x1100..0x11FF */
1169 if ( p_pmt->i_program_number == 0x1 &&
1170 p_pmt->i_pcr_pid == 0x1000 &&
1171 ( i_pid >> 8 ) == 0x11 )
1173 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
1174 es_format_Change( &p_es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
1176 else
1177 es_format_Change( &p_es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
1180 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_es_t *p_es,
1181 const dvbpsi_pmt_es_t *p_dvbpsies )
1183 es_format_t *p_fmt = &p_es->fmt;
1185 /* Blu-Ray mapping */
1186 switch( p_dvbpsies->i_type )
1188 case 0x80:
1189 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_BD_LPCM );
1190 break;
1191 case 0x81:
1192 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_A52 );
1193 break;
1194 case 0x82:
1195 case 0x85: /* DTS-HD High resolution audio */
1196 case 0x86: /* DTS-HD Master audio */
1197 case 0xA2: /* Secondary DTS audio */
1198 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_DTS );
1199 break;
1201 case 0x83: /* TrueHD AC3 */
1202 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_TRUEHD );
1203 break;
1205 case 0x84: /* E-AC3 */
1206 case 0xA1: /* Secondary E-AC3 */
1207 es_format_Change( p_fmt, AUDIO_ES, VLC_CODEC_EAC3 );
1208 break;
1209 case 0x90: /* Presentation graphics */
1210 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_BD_PG );
1211 break;
1212 case 0x91: /* Interactive graphics */
1213 return false;
1214 case 0x92: /* Subtitle */
1215 es_format_Change( p_fmt, SPU_ES, VLC_CODEC_BD_TEXT );
1216 break;
1217 case 0xEA:
1218 es_format_Change( p_fmt, VIDEO_ES, VLC_CODEC_VC1 );
1219 break;
1220 default:
1221 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
1222 p_dvbpsies->i_pid, p_dvbpsies->i_type );
1223 return false;
1225 return true;
1228 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_es_t *p_es,
1229 const dvbpsi_pmt_es_t *p_dvbpsies )
1231 static const struct
1233 char psz_tag[5];
1234 enum es_format_category_e i_cat;
1235 vlc_fourcc_t i_codec;
1236 } p_regs[] = {
1237 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
1238 { "EAC3", AUDIO_ES, VLC_CODEC_EAC3 },
1239 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
1240 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
1241 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
1242 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
1243 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
1244 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
1245 { "", UNKNOWN_ES, 0 }
1247 es_format_t *p_fmt = &p_es->fmt;
1249 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
1251 if( PMTEsHasRegistration( p_demux, p_dvbpsies, p_regs[i].psz_tag ) )
1253 es_format_Change( p_fmt, p_regs[i].i_cat, p_regs[i].i_codec );
1255 /* System A AC3 extension, see ATSC A/52 Annex G.2 */
1256 if ( p_regs[i].i_codec == VLC_CODEC_A52 && p_dvbpsies->i_type == 0x87 )
1257 p_fmt->i_codec = VLC_CODEC_EAC3;
1259 return true;
1262 return false;
1265 static char *GetIso639AudioTypeDesc( uint8_t type )
1267 static const char *audio_type[] = {
1268 /* "Main audio", */
1269 N_("clean effects"),
1270 N_("hearing impaired"),
1271 N_("visual impaired commentary"),
1274 if ( type == 0 || type >= ARRAY_SIZE(audio_type) )
1275 return NULL;
1277 return strdup( audio_type[ --type ] );
1280 static void PMTParseEsIso639( demux_t *p_demux, ts_es_t *p_es,
1281 const dvbpsi_pmt_es_t *p_dvbpsies )
1283 /* get language descriptor */
1284 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x0a );
1286 if( !p_dr )
1287 return;
1289 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
1290 if( !p_decoded )
1292 msg_Err( p_demux, " Failed to decode a ISO 639 descriptor" );
1293 return;
1296 p_es->fmt.psz_language = malloc( 4 );
1297 if( p_es->fmt.psz_language )
1299 memcpy( p_es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
1300 p_es->fmt.psz_language[3] = 0;
1301 msg_Dbg( p_demux, " found language: %s", p_es->fmt.psz_language);
1304 uint8_t type = p_decoded->code[0].i_audio_type;
1305 p_es->fmt.psz_description = GetIso639AudioTypeDesc( type );
1306 if (type == 0x00) /* Undefined */
1307 p_es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
1309 p_es->fmt.i_extra_languages = p_decoded->i_code_count-1;
1310 if( p_es->fmt.i_extra_languages > 0 )
1311 p_es->fmt.p_extra_languages =
1312 malloc( sizeof(*p_es->fmt.p_extra_languages) *
1313 p_es->fmt.i_extra_languages );
1314 if( p_es->fmt.p_extra_languages )
1316 for( unsigned i = 0; i < p_es->fmt.i_extra_languages; i++ )
1318 extra_languages_t *p_lang = &p_es->fmt.p_extra_languages[i];
1319 if( (p_lang->psz_language = malloc(4)) )
1321 memcpy( p_lang->psz_language, p_decoded->code[i+1].iso_639_code, 3 );
1322 p_lang->psz_language[3] = '\0';
1324 p_lang->psz_description = GetIso639AudioTypeDesc( p_decoded->code[i].i_audio_type );
1329 static void PIDFillFormat( demux_t *p_demux, ts_stream_t *p_pes,
1330 int i_stream_type, ts_transport_type_t *p_datatype )
1332 es_format_t *fmt = &p_pes->p_es->fmt;
1333 switch( i_stream_type )
1335 case 0x01: /* MPEG-1 video */
1336 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_MPGV );
1337 fmt->i_original_fourcc = VLC_CODEC_MP1V;
1338 break;
1339 case 0x02: /* MPEG-2 video */
1340 case 0x80: /* MPEG-2 MOTO video */
1341 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_MPGV );
1342 break;
1343 case 0x03: /* MPEG-1 audio */
1344 case 0x04: /* MPEG-2 audio */
1345 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_MPGA );
1346 break;
1347 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1348 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_MP4A );
1349 fmt->i_original_fourcc = VLC_FOURCC('A','D','T','S');
1350 break;
1351 case 0x10: /* MPEG4 (video) */
1352 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_MP4V );
1353 break;
1354 case 0x11: /* MPEG4 (audio) LATM */
1355 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_MP4A );
1356 fmt->i_original_fourcc = VLC_FOURCC('L','A','T','M');
1357 break;
1358 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1359 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_H264 );
1360 break;
1361 case 0x1C: /* ISO/IEC 14496-3 Audio, without using any additional
1362 transport syntax, such as DST, ALS and SLS */
1363 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_MP4A );
1364 break;
1365 case 0x24: /* HEVC */
1366 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_HEVC );
1367 break;
1368 case 0x42: /* CAVS (Chinese AVS) */
1369 es_format_Change( fmt, VIDEO_ES, VLC_CODEC_CAVS );
1370 break;
1372 case 0x81: /* A52 (audio) */
1373 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_A52 );
1374 break;
1375 case 0x82: /* SCTE-27 (sub) */
1376 es_format_Change( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
1377 *p_datatype = TS_TRANSPORT_SECTIONS;
1378 ts_sections_processor_Add( p_demux, &p_pes->p_sections_proc, 0xC6, 0x00,
1379 SCTE27_Section_Callback, p_pes );
1380 break;
1381 case 0x84: /* SDDS (audio) */
1382 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_SDDS );
1383 break;
1384 case 0x85: /* DTS (audio) FIXME: HDMV Only ? */
1385 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_DTS );
1386 break;
1387 case 0x87: /* E-AC3, ATSC */
1388 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
1389 break;
1390 case 0x8a: /* DTS (audio) */
1391 es_format_Change( fmt, AUDIO_ES, VLC_CODEC_DTS );
1392 break;
1393 case 0x91: /* A52 vls (audio) */
1394 es_format_Change( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1395 break;
1396 case 0x92: /* DVD_SPU vls (sub) */
1397 es_format_Change( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1398 break;
1400 case 0x94: /* SDDS (audio) */
1401 es_format_Change( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1402 break;
1404 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1405 es_format_Change( fmt, UNKNOWN_ES, 0 );
1406 break;
1408 case 0x06: /* PES_PRIVATE (fixed later) */
1409 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1410 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
1411 default:
1412 es_format_Change( fmt, UNKNOWN_ES, 0 );
1413 break;
1417 static void FillPESFromDvbpsiES( demux_t *p_demux,
1418 const dvbpsi_pmt_t *p_dvbpsipmt,
1419 const dvbpsi_pmt_es_t *p_dvbpsies,
1420 ts_pmt_registration_type_t registration_type,
1421 const ts_pmt_t *p_pmt,
1422 ts_stream_t *p_pes )
1424 ts_transport_type_t type_change = TS_TRANSPORT_PES;
1425 PIDFillFormat( p_demux, p_pes, p_dvbpsies->i_type, &type_change );
1427 p_pes->i_stream_type = p_dvbpsies->i_type;
1429 bool b_registration_applied = false;
1430 if ( p_dvbpsies->i_type >= 0x80 ) /* non standard, extensions */
1432 if ( registration_type == TS_PMT_REGISTRATION_BLURAY )
1434 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, p_pes->p_es, p_dvbpsies ) ))
1435 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
1436 p_dvbpsies->i_pid, p_dvbpsies->i_type );
1438 else
1440 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, p_pes->p_es, p_dvbpsies ) ))
1441 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
1442 p_dvbpsies->i_pid, p_dvbpsies->i_type );
1446 if ( !b_registration_applied )
1448 p_pes->transport = type_change; /* Only change type if registration has not changed meaning */
1450 switch( p_dvbpsies->i_type )
1452 case 0x02:
1453 PMTSetupEs0x02( p_pes->p_es, p_dvbpsies );
1454 break;
1455 case 0x05: /* Private data in sections */
1456 p_pes->transport = TS_TRANSPORT_SECTIONS;
1457 PMTSetupEs0x05PrivateData( p_demux, p_pes->p_es, p_dvbpsies );
1458 break;
1459 case 0x06:
1460 /* Handle PES private data */
1461 PMTSetupEs0x06( p_demux, p_pes, p_dvbpsies );
1462 break;
1463 case 0x0a: /* DSM-CC */
1464 case 0x0b:
1465 case 0x0c:
1466 case 0x0d:
1467 p_pes->transport = TS_TRANSPORT_IGNORE;
1468 break;
1469 /* All other private or reserved types */
1470 case 0x13: /* SL in sections */
1471 p_pes->transport = TS_TRANSPORT_SECTIONS;
1472 //ft
1473 case 0x0f:
1474 case 0x10:
1475 case 0x11:
1476 case 0x12:
1477 SetupISO14496Descriptors( p_demux, p_pes, p_pmt, p_dvbpsies );
1478 break;
1479 case 0x15:
1480 SetupMetadataDescriptors( p_demux, p_pes, p_dvbpsies );
1481 break;
1482 case 0x1b:
1483 SetupAVCDescriptors( p_demux, p_pes->p_es, p_dvbpsies );
1484 break;
1485 case 0x21:
1486 SetupJ2KDescriptors( p_demux, p_pes->p_es, p_dvbpsies );
1487 break;
1488 case 0x83:
1489 /* LPCM (audio) */
1490 PMTSetupEs0x83( p_dvbpsipmt, p_pes->p_es, p_dvbpsies->i_pid );
1491 break;
1492 case 0xa0:
1493 PMTSetupEs0xA0( p_demux, p_pes->p_es, p_dvbpsies );
1494 break;
1495 case 0xd1:
1496 PMTSetupEs0xD1( p_demux, p_pes->p_es, p_dvbpsies );
1497 break;
1498 case 0xEA:
1499 PMTSetupEs0xEA( p_demux, p_pes->p_es, p_dvbpsies );
1500 default:
1501 break;
1505 if( p_pes->p_es->fmt.i_cat == AUDIO_ES ||
1506 ( p_pes->p_es->fmt.i_cat == SPU_ES &&
1507 p_pes->p_es->fmt.i_codec != VLC_CODEC_DVBS &&
1508 p_pes->p_es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
1510 PMTParseEsIso639( p_demux, p_pes->p_es, p_dvbpsies );
1513 if( p_pes->p_es->fmt.i_cat == AUDIO_ES )
1515 SetupAudioExtendedDescriptors( p_demux, p_pes->p_es, p_dvbpsies );
1518 /* PES packets usually contain truncated frames */
1519 p_pes->p_es->fmt.b_packetized = false;
1521 /* Set Groups / ID */
1522 p_pes->p_es->fmt.i_group = p_dvbpsipmt->i_program_number;
1523 if( p_demux->p_sys->b_es_id_pid )
1524 p_pes->p_es->fmt.i_id = p_dvbpsies->i_pid;
1527 static en50221_capmt_info_t * CreateCAPMTInfo( const dvbpsi_pmt_t *p_pmt )
1529 en50221_capmt_info_t *p_en = en50221_capmt_New( p_pmt->i_version,
1530 p_pmt->i_program_number );
1531 if( unlikely(p_en == NULL) )
1532 return p_en;
1534 for( const dvbpsi_descriptor_t *p_dr = p_pmt->p_first_descriptor;
1535 p_dr; p_dr = p_dr->p_next )
1537 if( p_dr->i_tag == 0x09 )
1538 en50221_capmt_AddCADescriptor( p_en, p_dr->p_data, p_dr->i_length );
1541 for( const dvbpsi_pmt_es_t *p_es = p_pmt->p_first_es;
1542 p_es; p_es = p_es->p_next )
1544 en50221_capmt_es_info_t *p_enes = en50221_capmt_EsAdd( p_en,
1545 p_es->i_type,
1546 p_es->i_pid );
1547 if( likely(p_enes) )
1549 for( const dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
1550 p_dr; p_dr = p_dr->p_next )
1552 if( p_dr->i_tag == 0x09 )
1553 en50221_capmt_AddESCADescriptor( p_enes, p_dr->p_data, p_dr->i_length );
1558 return p_en;
1561 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_dvbpsipmt )
1563 demux_t *p_demux = data;
1564 demux_sys_t *p_sys = p_demux->p_sys;
1566 ts_pid_t *pmtpid = NULL;
1567 ts_pmt_t *p_pmt = NULL;
1569 msg_Dbg( p_demux, "PMTCallBack called for program %d", p_dvbpsipmt->i_program_number );
1571 if (unlikely(GetPID(p_sys, 0)->type != TYPE_PAT))
1573 assert(GetPID(p_sys, 0)->type == TYPE_PAT);
1574 dvbpsi_pmt_delete(p_dvbpsipmt);
1577 const ts_pat_t *p_pat = GetPID(p_sys, 0)->u.p_pat;
1579 /* First find this PMT declared in PAT */
1580 for( int i = 0; !pmtpid && i < p_pat->programs.i_size; i++ )
1582 const int i_pmt_prgnumber = p_pat->programs.p_elems[i]->u.p_pmt->i_number;
1583 if( i_pmt_prgnumber != TS_USER_PMT_NUMBER &&
1584 i_pmt_prgnumber == p_dvbpsipmt->i_program_number )
1586 pmtpid = p_pat->programs.p_elems[i];
1587 assert(pmtpid->type == TYPE_PMT);
1588 p_pmt = pmtpid->u.p_pmt;
1592 if( pmtpid == NULL )
1594 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1595 dvbpsi_pmt_delete(p_dvbpsipmt);
1596 return;
1599 pmtpid->i_flags |= FLAG_SEEN;
1601 if( p_pmt->i_version != -1 &&
1602 ( !p_dvbpsipmt->b_current_next || p_pmt->i_version == p_dvbpsipmt->i_version ) )
1604 dvbpsi_pmt_delete( p_dvbpsipmt );
1605 return;
1608 /* Save old es array */
1609 DECL_ARRAY(ts_pid_t *) pid_to_decref;
1610 pid_to_decref.i_alloc = p_pmt->e_streams.i_alloc;
1611 pid_to_decref.i_size = p_pmt->e_streams.i_size;
1612 pid_to_decref.p_elems = p_pmt->e_streams.p_elems;
1613 if( p_pmt->p_atsc_si_basepid )
1614 ARRAY_APPEND( pid_to_decref, p_pmt->p_atsc_si_basepid );
1615 if( p_pmt->p_si_sdt_pid )
1616 ARRAY_APPEND( pid_to_decref, p_pmt->p_si_sdt_pid );
1617 ARRAY_INIT(p_pmt->e_streams);
1619 if( p_pmt->iod )
1621 ODFree( p_pmt->iod );
1622 p_pmt->iod = NULL;
1625 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
1626 p_dvbpsipmt->i_program_number, p_dvbpsipmt->i_version, p_dvbpsipmt->i_pcr_pid );
1627 p_pmt->i_pid_pcr = p_dvbpsipmt->i_pcr_pid;
1628 p_pmt->i_version = p_dvbpsipmt->i_version;
1630 if( ProgramIsSelected( p_sys, p_pmt->i_number ) )
1631 SetPIDFilter( p_sys, GetPID(p_sys, p_pmt->i_pid_pcr), true ); /* Set demux filter */
1633 /* Parse PMT descriptors */
1634 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
1635 ParsePMTRegistrations( p_demux, p_dvbpsipmt->p_first_descriptor, p_pmt, &registration_type );
1637 if( p_sys->standard == TS_STANDARD_AUTO )
1639 switch( registration_type )
1641 case TS_PMT_REGISTRATION_BLURAY:
1642 TsChangeStandard( p_sys, TS_STANDARD_MPEG );
1643 break;
1644 case TS_PMT_REGISTRATION_ARIB:
1645 TsChangeStandard( p_sys, TS_STANDARD_ARIB );
1646 break;
1647 case TS_PMT_REGISTRATION_ATSC:
1648 TsChangeStandard( p_sys, TS_STANDARD_ATSC );
1649 break;
1650 default:
1651 if(SEEN(GetPID(p_sys, ATSC_BASE_PID)))
1653 TsChangeStandard( p_sys, TS_STANDARD_ATSC );
1655 else
1657 /* Probe using ES */
1658 p_sys->standard = ProbePMTStandard( p_dvbpsipmt );
1660 break;
1664 /* Private descriptors depends on standard */
1665 ParsePMTPrivateRegistrations( p_demux, p_dvbpsipmt->p_first_descriptor, p_pmt, p_sys->standard );
1667 dvbpsi_pmt_es_t *p_dvbpsies;
1668 for( p_dvbpsies = p_dvbpsipmt->p_first_es; p_dvbpsies != NULL; p_dvbpsies = p_dvbpsies->p_next )
1670 ts_pid_t *pespid = GetPID(p_sys, p_dvbpsies->i_pid);
1671 if ( pespid->type != TYPE_STREAM && pespid->type != TYPE_FREE )
1673 msg_Warn( p_demux, " * PMT wants to create PES on pid %d used by non PES", pespid->i_pid );
1674 continue;
1677 char const * psz_typedesc = ISO13818_1_Get_StreamType_Description( p_dvbpsies->i_type );
1679 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
1680 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
1682 /* PMT element/component descriptors */
1683 for( dvbpsi_descriptor_t *p_dr = p_dvbpsies->p_first_descriptor;
1684 p_dr != NULL; p_dr = p_dr->p_next )
1686 const char *psz_desc = NULL;
1687 if( registration_type == TS_PMT_REGISTRATION_ARIB )
1688 psz_desc = ARIB_B10_Get_PMT_Descriptor_Description( p_dr->i_tag );
1690 if( psz_desc )
1691 msg_Dbg( p_demux, " - ES descriptor %s 0x%x", psz_desc, p_dr->i_tag );
1692 else
1693 msg_Dbg( p_demux, " - ES descriptor tag 0x%x", p_dr->i_tag );
1696 const bool b_pid_inuse = ( pespid->type == TYPE_STREAM );
1697 ts_stream_t *p_pes;
1699 if ( !PIDSetup( p_demux, TYPE_STREAM, pespid, pmtpid ) )
1701 msg_Warn( p_demux, " * pid=%d type=0x%x %s (skipped)",
1702 p_dvbpsies->i_pid, p_dvbpsies->i_type, psz_typedesc );
1703 continue;
1705 else
1707 if( b_pid_inuse ) /* pes will point to a temp */
1709 p_pes = ts_stream_New( p_demux, p_pmt );
1710 if( !p_pes )
1712 PIDRelease( p_demux, pespid );
1713 continue;
1716 else /* pes will point to the new one allocated from PIDSetup */
1718 p_pes = pespid->u.p_stream;
1722 /* Add pid to the list of used ones in pmt */
1723 ARRAY_APPEND( p_pmt->e_streams, pespid );
1724 pespid->i_flags |= SEEN(GetPID(p_sys, p_dvbpsies->i_pid));
1726 /* Fill p_pes es and add extra es if any */
1727 FillPESFromDvbpsiES( p_demux, p_dvbpsipmt, p_dvbpsies,
1728 registration_type, p_pmt, p_pes );
1730 /* Set description and debug */
1731 if( p_pes->p_es->fmt.i_cat == UNKNOWN_ES )
1733 msg_Dbg( p_demux, " => pid %d content is *unknown*",
1734 p_dvbpsies->i_pid );
1735 p_pes->p_es->fmt.psz_description = strdup( psz_typedesc );
1737 else
1739 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
1740 p_dvbpsies->i_pid, (char*)&p_pes->p_es->fmt.i_codec );
1743 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_dvbpsies, 0x09 );
1744 if( p_dr && p_dr->i_length >= 2 )
1746 msg_Dbg( p_demux, " - ES descriptor : CA (0x9) SysID 0x%x",
1747 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
1750 const bool b_create_es = (p_pes->p_es->fmt.i_cat != UNKNOWN_ES);
1752 /* Now check and merge */
1753 if( b_pid_inuse ) /* We need to compare to the existing pes/es */
1755 ts_es_t *p_existing_es = ts_stream_Find_es( pespid->u.p_stream, p_pmt );
1756 if( p_existing_es )
1758 const es_format_t *ofmt = &p_existing_es->fmt;
1759 const es_format_t *nfmt = &p_pes->p_es->fmt;
1761 /* Check if we can avoid restarting that ES */
1762 bool b_canreuse = es_format_IsSimilar( ofmt, nfmt );
1764 /* Check codecs extra */
1765 b_canreuse = b_canreuse &&
1767 ofmt->i_extra == nfmt->i_extra &&
1768 ( ofmt->i_extra == 0 ||
1769 memcmp( ofmt->p_extra, nfmt->p_extra, nfmt->i_extra ) == 0 )
1772 /* Tracks must have same language */
1773 b_canreuse = b_canreuse &&
1775 ( !!ofmt->psz_language == !!nfmt->psz_language ) &&
1776 ( ofmt->psz_language == NULL ||
1777 !strcmp( ofmt->psz_language, nfmt->psz_language ) )
1780 /* Check is we have any subtitles */
1781 b_canreuse = b_canreuse &&
1782 ( ts_Count_es( p_pes->p_es->p_extraes, false, NULL ) ==
1783 ts_Count_es( p_existing_es->p_extraes, false, NULL )
1786 if( b_canreuse )
1788 /* Just keep using previous es */
1789 ts_stream_Del( p_demux, p_pes );
1791 else
1793 ts_es_t *p_new = ts_stream_Extract_es( p_pes, p_pmt );
1794 ts_es_t *p_old = ts_stream_Extract_es( pespid->u.p_stream, p_pmt );
1795 ts_stream_Add_es( pespid->u.p_stream, p_new, false );
1796 assert(p_old == p_existing_es);
1797 assert(ts_Count_es(p_pes->p_es, false, NULL) == 0);
1798 ts_stream_Add_es( p_pes, p_old, false );
1799 ts_stream_Del( p_demux, p_pes );
1802 else /* There was no es for that program on that pid, merge in */
1804 assert(ts_Count_es(pespid->u.p_stream->p_es, false, NULL)); /* Used by another program */
1805 ts_es_t *p_new = ts_stream_Extract_es( p_pes, p_pmt );
1806 assert( p_new );
1807 ts_stream_Add_es( pespid->u.p_stream, p_new, false );
1808 ts_stream_Del( p_demux, p_pes );
1811 /* Nothing to do, pes is now just set */
1812 if( b_create_es )
1813 AddAndCreateES( p_demux, pespid, false );
1816 /* Set CAM descrambling */
1817 if( ProgramIsSelected( p_sys, p_pmt->i_number ) )
1819 en50221_capmt_info_t *p_en = CreateCAPMTInfo( p_dvbpsipmt );
1820 if( p_en )
1822 /* DTV/CAM takes ownership of en50221_capmt_info_t on success */
1823 if( vlc_stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA, p_en ) != VLC_SUCCESS )
1825 en50221_capmt_Delete( p_en );
1826 if ( p_sys->standard == TS_STANDARD_ARIB && !p_sys->arib.b25stream )
1828 p_sys->arib.b25stream = vlc_stream_FilterNew( p_demux->s, "aribcam" );
1829 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
1835 /* Add arbitrary PID from here */
1836 if ( p_sys->standard == TS_STANDARD_ATSC )
1838 ts_pid_t *atsc_base_pid = GetPID(p_sys, ATSC_BASE_PID);
1839 if ( PIDSetup( p_demux, TYPE_PSIP, atsc_base_pid, pmtpid ) )
1841 ts_psip_t *p_psip = atsc_base_pid->u.p_psip;
1842 if( !ATSC_Attach_Dvbpsi_Base_Decoders( p_psip->handle, atsc_base_pid ) )
1844 msg_Err( p_demux, "dvbpsi_atsc_AttachMGT/STT failed for program %d",
1845 p_pmt->i_number );
1846 PIDRelease( p_demux, atsc_base_pid );
1848 else
1850 p_pmt->p_atsc_si_basepid = atsc_base_pid;
1851 SetPIDFilter( p_demux->p_sys, atsc_base_pid, true );
1852 msg_Dbg( p_demux, " * pid=%d listening for MGT/STT", atsc_base_pid->i_pid );
1854 /* Set up EAS spu es */
1855 if( p_pmt->e_streams.i_size )
1857 ts_es_t *p_eas_es = ts_es_New( p_pmt );
1858 if( likely(p_eas_es) )
1860 es_format_Change( &p_eas_es->fmt, SPU_ES, VLC_CODEC_SCTE_18 );
1861 p_eas_es->fmt.i_id = ATSC_BASE_PID;
1862 p_eas_es->fmt.i_group = p_pmt->i_number;
1863 p_eas_es->fmt.psz_description = strdup(SCTE18_DESCRIPTION);
1864 if( p_psip->p_eas_es )
1866 ts_es_t *p_next = p_psip->p_eas_es->p_next;
1867 p_psip->p_eas_es->p_next = p_eas_es;
1868 p_eas_es->p_next = p_next;
1870 else
1872 p_psip->p_eas_es = p_eas_es;
1874 msg_Dbg( p_demux, " * pid=%d listening for EAS events", ATSC_BASE_PID );
1879 else if( atsc_base_pid->type != TYPE_FREE )
1881 msg_Err( p_demux, "can't attach PSIP table handlers"
1882 "on already in use ATSC base pid %d", ATSC_BASE_PID );
1885 else if( p_sys->standard != TS_STANDARD_MPEG && p_sys->standard != TS_STANDARD_TDMB )
1887 ts_pid_t *p_sdt_pid = ts_pid_Get( &p_sys->pids, TS_SI_SDT_PID );
1888 if ( PIDSetup( p_demux, TYPE_SI, p_sdt_pid, pmtpid ) ) /* Create or incref SDT */
1890 if( !ts_attach_SI_Tables_Decoders( p_sdt_pid ) )
1892 msg_Err( p_demux, "Can't attach SI table decoders from program %d",
1893 p_pmt->i_number );
1894 PIDRelease( p_demux, p_sdt_pid );
1896 else
1898 p_pmt->p_si_sdt_pid = p_sdt_pid;
1899 SetPIDFilter( p_demux->p_sys, p_sdt_pid, true );
1900 msg_Dbg( p_demux, " * pid=%d listening for SDT", p_sdt_pid->i_pid );
1903 else if( p_sdt_pid->type != TYPE_FREE )
1905 msg_Err( p_demux, "can't attach SI SDT table handler"
1906 "on already in used pid %d (Not DVB ?)", p_sdt_pid->i_pid );
1910 /* Decref or clean now unused es */
1911 for( int i = 0; i < pid_to_decref.i_size; i++ )
1912 PIDRelease( p_demux, pid_to_decref.p_elems[i] );
1913 ARRAY_RESET( pid_to_decref );
1915 if( !p_sys->b_trust_pcr )
1917 int i_cand = FindPCRCandidate( p_pmt );
1918 p_pmt->i_pid_pcr = i_cand;
1919 p_pmt->pcr.b_disable = true;
1920 msg_Warn( p_demux, "PCR not trusted for program %d, set up workaround using pid %d",
1921 p_pmt->i_number, i_cand );
1924 UpdatePESFilters( p_demux, p_sys->b_es_all );
1926 /* Probe Boundaries */
1927 if( p_sys->b_canfastseek && p_pmt->i_last_dts == -1 )
1929 p_pmt->i_last_dts = 0;
1930 ProbeStart( p_demux, p_pmt->i_number );
1931 ProbeEnd( p_demux, p_pmt->i_number );
1934 dvbpsi_pmt_delete( p_dvbpsipmt );
1937 int UserPmt( demux_t *p_demux, const char *psz_fmt )
1939 demux_sys_t *p_sys = p_demux->p_sys;
1940 char *psz_dup = strdup( psz_fmt );
1941 char *psz = psz_dup;
1942 int i_number;
1944 if( !psz_dup )
1945 return VLC_ENOMEM;
1947 /* Parse PID */
1948 unsigned long i_pid = strtoul( psz, &psz, 0 );
1949 if( i_pid < 2 || i_pid >= 8192 )
1950 goto error;
1952 /* Parse optional program number */
1953 i_number = 0;
1954 if( *psz == ':' )
1955 i_number = strtol( &psz[1], &psz, 0 );
1957 /* */
1958 ts_pid_t *pmtpid = GetPID(p_sys, i_pid);
1960 msg_Dbg( p_demux, "user pmt specified (pid=%lu,number=%d)", i_pid, i_number );
1961 if ( !PIDSetup( p_demux, TYPE_PMT, pmtpid, GetPID(p_sys, 0) ) )
1962 goto error;
1964 /* Dummy PMT */
1965 ts_pmt_t *p_pmt = pmtpid->u.p_pmt;
1966 p_pmt->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1967 if( !dvbpsi_pmt_attach( p_pmt->handle,
1968 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1969 PMTCallBack, p_demux ) )
1971 PIDRelease( p_demux, pmtpid );
1972 goto error;
1975 ARRAY_APPEND( GetPID(p_sys, 0)->u.p_pat->programs, pmtpid );
1977 psz = strchr( psz, '=' );
1978 if( psz )
1979 psz++;
1980 while( psz && *psz )
1982 char *psz_next = strchr( psz, ',' );
1984 if( psz_next )
1985 *psz_next++ = '\0';
1987 i_pid = strtoul( psz, &psz, 0 );
1988 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1989 goto next;
1991 char *psz_opt = &psz[1];
1992 if( !strcmp( psz_opt, "pcr" ) )
1994 p_pmt->i_pid_pcr = i_pid;
1996 else if( GetPID(p_sys, i_pid)->type == TYPE_FREE )
1998 ts_pid_t *pid = GetPID(p_sys, i_pid);
2000 char *psz_arg = strchr( psz_opt, '=' );
2001 if( psz_arg )
2002 *psz_arg++ = '\0';
2004 if ( !PIDSetup( p_demux, TYPE_STREAM, pid, pmtpid ) )
2005 continue;
2007 ARRAY_APPEND( p_pmt->e_streams, pid );
2009 if( p_pmt->i_pid_pcr <= 0 )
2010 p_pmt->i_pid_pcr = i_pid;
2012 es_format_t *fmt = &pid->u.p_stream->p_es->fmt;
2014 if( psz_arg && strlen( psz_arg ) == 4 )
2016 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
2017 psz_arg[2], psz_arg[3] );
2018 int i_cat = UNKNOWN_ES;
2020 if( !strcmp( psz_opt, "video" ) )
2021 i_cat = VIDEO_ES;
2022 else if( !strcmp( psz_opt, "audio" ) )
2023 i_cat = AUDIO_ES;
2024 else if( !strcmp( psz_opt, "spu" ) )
2025 i_cat = SPU_ES;
2027 es_format_Change( fmt, i_cat, i_codec );
2028 fmt->b_packetized = false;
2030 else
2032 const int i_stream_type = strtol( psz_opt, NULL, 0 );
2033 PIDFillFormat( p_demux, pid->u.p_stream, i_stream_type, &pid->u.p_stream->transport );
2036 fmt->i_group = i_number;
2037 if( p_sys->b_es_id_pid )
2038 fmt->i_id = i_pid;
2040 if( fmt->i_cat != UNKNOWN_ES )
2042 msg_Dbg( p_demux, " * es pid=%lu fcc=%4.4s", i_pid,
2043 (char*)&fmt->i_codec );
2044 pid->u.p_stream->p_es->id = es_out_Add( p_demux->out, fmt );
2045 p_sys->i_pmt_es++;
2049 next:
2050 psz = psz_next;
2053 p_sys->b_user_pmt = true;
2054 free( psz_dup );
2055 return VLC_SUCCESS;
2057 error:
2058 free( psz_dup );
2059 return VLC_EGENERIC;
2062 bool ts_psi_PAT_Attach( ts_pid_t *patpid, void *cbdata )
2064 if( unlikely(patpid->type != TYPE_PAT || patpid->i_pid != TS_PSI_PAT_PID) )
2065 return false;
2066 return dvbpsi_pat_attach( patpid->u.p_pat->handle, PATCallBack, cbdata );
2069 void ts_psi_Packet_Push( ts_pid_t *p_pid, const uint8_t *p_pktbuffer )
2071 if( p_pid->type == TYPE_PAT )
2072 dvbpsi_packet_push( p_pid->u.p_pat->handle, (uint8_t *) p_pktbuffer );
2073 else if( p_pid->type == TYPE_PMT )
2074 dvbpsi_packet_push( p_pid->u.p_pmt->handle, (uint8_t *) p_pktbuffer );