1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VLC authors and VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x.nl>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
39 #include <vlc_access.h> /* DVB-specific things */
40 #include <vlc_demux.h>
43 #include <vlc_charset.h> /* FromCharset, for EIT */
46 #include "../mux/mpeg/csa.h"
48 /* Include dvbpsi headers */
49 # include <dvbpsi/dvbpsi.h>
50 # include <dvbpsi/demux.h>
51 # include <dvbpsi/descriptor.h>
52 # include <dvbpsi/pat.h>
53 # include <dvbpsi/pmt.h>
54 # include <dvbpsi/sdt.h>
55 # include <dvbpsi/dr.h>
56 # include <dvbpsi/psi.h>
59 # include <dvbpsi/eit.h>
62 # include <dvbpsi/tot.h>
64 #include "../mux/mpeg/dvbpsi_compat.h"
66 #include "../codec/opus_header.h"
71 VLC_FORMAT(1, 2) static void ts_debug(const char *format, ...)
76 vfprintf(stderr, format, ap);
84 #include <aribb24/aribb24.h>
85 #include <aribb24/decoder.h>
88 typedef enum arib_modes_e
91 ARIBMODE_DISABLED = 0,
95 /*****************************************************************************
97 *****************************************************************************/
98 static int Open ( vlc_object_t * );
99 static void Close ( vlc_object_t * );
102 * - Rename "extra pmt" to "user pmt"
103 * - Update extra pmt description
104 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
105 * where pid_description could take 3 forms:
106 * 1. pid:pcr (to force the pcr pid)
108 * 3. pid:type=fourcc where type=(video|audio|spu)
110 #define PMT_TEXT N_("Extra PMT")
111 #define PMT_LONGTEXT N_( \
112 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
114 #define PID_TEXT N_("Set id of ES to PID")
115 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
116 " handled by VLC to the same value as the PID in" \
117 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
118 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
120 #define CSA_TEXT N_("CSA Key")
121 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
122 "16 char string (8 hexadecimal bytes).")
124 #define CSA2_TEXT N_("Second CSA Key")
125 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
126 "16 char string (8 hexadecimal bytes).")
129 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
130 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
131 "The decryption routines subtract the TS-header from the value before " \
134 #define SPLIT_ES_TEXT N_("Separate sub-streams")
135 #define SPLIT_ES_LONGTEXT N_( \
136 "Separate teletex/dvbs pages into independent ES. " \
137 "It can be useful to turn off this option when using stream output." )
139 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
140 #define SEEK_PERCENT_LONGTEXT N_( \
141 "Seek and position based on a percent byte position, not a PCR generated " \
142 "time position. If seeking doesn't work property, turn on this option." )
144 #define PCR_TEXT N_("Trust in-stream PCR")
145 #define PCR_LONGTEXT N_("Use the stream PCR as a reference.")
147 static const int const arib_mode_list[] =
148 { ARIBMODE_AUTO, ARIBMODE_ENABLED, ARIBMODE_DISABLED };
149 static const char *const arib_mode_list_text[] =
150 { N_("Auto"), N_("Enabled"), N_("Disabled") };
152 #define SUPPORT_ARIB_TEXT N_("ARIB STD-B24 mode")
153 #define SUPPORT_ARIB_LONGTEXT N_( \
154 "Forces ARIB STD-B24 mode for decoding characters." \
155 "This feature affects EPG information and subtitles." )
158 set_description( N_("MPEG Transport Stream demuxer") )
159 set_shortname ( "MPEG-TS" )
160 set_category( CAT_INPUT )
161 set_subcategory( SUBCAT_INPUT_DEMUX )
163 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
164 add_bool( "ts-trust-pcr", true, PCR_TEXT, PCR_LONGTEXT, true )
166 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
168 add_obsolete_string( "ts-out" ) /* since 2.2.0 */
169 add_obsolete_integer( "ts-out-mtu" ) /* since 2.2.0 */
170 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
172 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
174 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
177 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
178 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
180 add_integer( "ts-arib", ARIBMODE_AUTO, SUPPORT_ARIB_TEXT, SUPPORT_ARIB_LONGTEXT, false )
181 change_integer_list( arib_mode_list, arib_mode_list_text )
183 add_obsolete_bool( "ts-silent" );
185 set_capability( "demux", 10 )
186 set_callbacks( Open, Close )
190 /*****************************************************************************
192 *****************************************************************************/
193 static const char *const ppsz_teletext_type[] = {
196 N_("Teletext subtitles"),
197 N_("Teletext: additional information"),
198 N_("Teletext: program schedule"),
199 N_("Teletext subtitles: hearing impaired")
204 uint8_t i_objectTypeIndication;
205 uint8_t i_streamType;
210 } decoder_config_descriptor_t;
219 decoder_config_descriptor_t dec_descr;
221 } es_mpeg4_descriptor_t;
223 #define ES_DESCRIPTOR_COUNT 255
229 es_mpeg4_descriptor_t es_descr[ES_DESCRIPTOR_COUNT];
235 dvbpsi_handle handle;
241 /* IOD stuff (mpeg4) */
242 iod_descriptor_t *iod;
248 /* for special PAT/SDT case */
249 dvbpsi_handle handle; /* PAT/SDT/EIT */
262 TS_ES_DATA_TABLE_SECTION
267 TS_PMT_REGISTRATION_NONE = 0,
268 TS_PMT_REGISTRATION_HDMV
269 } ts_pmt_registration_type_t;
275 ts_es_data_type_t data_type;
281 es_mpeg4_descriptor_t *p_mpeg4desc;
291 int i_cc; /* countinuity counter */
294 /* PSI owner (ie PMT -> PAT, ES -> PMT */
302 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
311 vlc_mutex_t csa_lock;
313 /* TS packet size (188, 192, 204) */
316 /* Additional TS packet header size (BluRay TS packets have 4-byte header before sync byte) */
317 int i_packet_header_size;
319 /* how many TS packet we read at once */
322 /* to determine length and time */
325 mtime_t i_current_pcr;
327 bool b_force_seek_per_percent;
336 arib_instance_t *p_instance;
359 bool b_access_control;
365 int64_t i_dvb_length;
366 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
369 int i_current_program;
370 vlc_list_t programs_list;
376 static int Demux ( demux_t *p_demux );
377 static int Control( demux_t *p_demux, int i_query, va_list args );
379 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
380 static void PIDClean( demux_t *, ts_pid_t *pid );
381 static void PIDFillFormat( es_format_t *fmt, int i_stream_type );
383 static void PATCallBack( void*, dvbpsi_pat_t * );
384 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt );
385 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
386 static void PSINewTableCallBack( dvbpsi_t *handle, uint8_t i_table_id,
387 uint16_t i_extension, demux_t * );
389 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
390 uint8_t i_table_id, uint16_t i_extension );
393 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
395 static inline int PIDGet( block_t *p )
397 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
400 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
402 static block_t* ReadTSPacket( demux_t *p_demux );
403 static int Seek( demux_t *p_demux, double f_percent );
404 static void GetFirstPCR( demux_t *p_demux );
405 static void GetLastPCR( demux_t *p_demux );
406 static void CheckPCR( demux_t *p_demux );
407 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
409 static void IODFree( iod_descriptor_t * );
411 #define TS_USER_PMT_NUMBER (0)
412 static int UserPmt( demux_t *p_demux, const char * );
414 static int SetPIDFilter( demux_t *, int i_pid, bool b_selected );
415 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
417 #define TS_PACKET_SIZE_188 188
418 #define TS_PACKET_SIZE_192 192
419 #define TS_PACKET_SIZE_204 204
420 #define TS_PACKET_SIZE_MAX 204
422 static int DetectPacketSize( demux_t *p_demux, int *pi_header_size )
424 const uint8_t *p_peek;
425 if( stream_Peek( p_demux->s,
426 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
431 if( memcmp( p_peek, "TFrc", 4 ) == 0 )
434 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
435 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
436 * but after the filename the offsets seem to be incorrect. - DJ */
437 int i_duration, i_name;
438 char *psz_name = xmalloc(25);
439 char *psz_event_name;
440 char *psz_event_text = xmalloc(130);
441 char *psz_ext_text = xmalloc(1025);
443 // 2 bytes version Uimsbf (4,5)
444 // 2 bytes reserved (6,7)
445 // 2 bytes duration in minutes Uimsbf (8,9(
446 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
447 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
448 // 2 bytes service number in channel list (10, 11)
449 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
450 // 4 bytes of reserved + tuner info (14,15,16,17)
451 // 2 bytes of Service ID Bslbf (18,19)
452 // 2 bytes of PMT PID Uimsbf (20,21)
453 // 2 bytes of PCR PID Uimsbf (22,23)
454 // 2 bytes of Video PID Uimsbf (24,25)
455 // 2 bytes of Audio PID Uimsbf (26,27)
456 // 24 bytes filename Bslbf
457 memcpy( psz_name, &p_peek[28], 24 );
459 msg_Dbg( p_demux, "recordingname=%s", psz_name );
460 // 1 byte of sat index Uimsbf (52)
461 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
462 // 4 bytes of freq. Uimsbf (56,57,58,59)
463 // 2 bytes of symbol rate Uimsbf (60,61)
464 // 2 bytes of TS stream ID Uimsbf (62,63)
467 // 2 bytes duration Uimsbf (70,71)
468 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
469 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
470 // 4 bytes EventID Uimsbf (72-75)
471 // 8 bytes of Start and End time info (76-83)
472 // 1 byte reserved (84)
473 // 1 byte event name length Uimsbf (89)
474 i_name = (int)(p_peek[89]&~0x81);
475 msg_Dbg( p_demux, "event name length = %d", i_name);
476 psz_event_name = xmalloc( i_name+1 );
477 // 1 byte parental rating (90)
478 // 129 bytes of event text
479 memcpy( psz_event_name, &p_peek[91], i_name );
480 psz_event_name[i_name] = '\0';
481 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
482 psz_event_text[129-i_name] = '\0';
483 msg_Dbg( p_demux, "event name=%s", psz_event_name );
484 msg_Dbg( p_demux, "event text=%s", psz_event_text );
485 // 12 bytes reserved (220)
487 // 2 bytes Event Text Length Uimsbf
488 // 4 bytes EventID Uimsbf
489 // FIXME We just have 613 bytes. not enough for this entire text
490 // 1024 bytes Extended Event Text Bslbf
491 memcpy( psz_ext_text, p_peek+372, 1024 );
492 psz_ext_text[1024] = '\0';
493 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
494 // 52 bytes reserved Bslbf
496 msg_Dbg( p_demux, "this is a topfield file" );
497 return TS_PACKET_SIZE_188;
500 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
502 if( p_peek[i_sync] != 0x47 )
505 /* Check next 3 sync bytes */
506 int i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
507 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
509 msg_Err( p_demux, "cannot peek" );
512 if( p_peek[i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
513 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
514 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
516 return TS_PACKET_SIZE_188;
518 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
519 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
520 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
524 *pi_header_size = 4; /* BluRay TS packets have 4-byte header */
526 return TS_PACKET_SIZE_192;
528 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
529 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
530 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
532 return TS_PACKET_SIZE_204;
536 if( p_demux->b_force )
538 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
539 return TS_PACKET_SIZE_188;
541 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
545 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
546 static void vlc_dvbpsi_reset( demux_t *p_demux )
548 demux_sys_t *p_sys = p_demux->p_sys;
550 ts_pid_t *pat = &p_sys->pid[0];
551 ts_pid_t *sdt = &p_sys->pid[0x11];
552 ts_pid_t *eit = &p_sys->pid[0x12];
553 ts_pid_t *tdt = &p_sys->pid[0x14];
555 if( pat->psi->handle )
557 if( dvbpsi_decoder_present( pat->psi->handle ) )
558 dvbpsi_pat_detach( pat->psi->handle );
559 dvbpsi_delete( pat->psi->handle );
560 pat->psi->handle = NULL;
563 if( sdt->psi->handle )
565 if( dvbpsi_decoder_present( sdt->psi->handle ) )
566 dvbpsi_DetachDemux( sdt->psi->handle );
567 dvbpsi_delete( sdt->psi->handle );
568 sdt->psi->handle = NULL;
571 if( eit->psi->handle )
573 if( dvbpsi_decoder_present( eit->psi->handle ) )
574 dvbpsi_DetachDemux( eit->psi->handle );
575 dvbpsi_delete( eit->psi->handle );
576 eit->psi->handle = NULL;
579 if( tdt->psi->handle )
581 if( dvbpsi_decoder_present( tdt->psi->handle ) )
582 dvbpsi_DetachDemux( tdt->psi->handle );
583 dvbpsi_delete( tdt->psi->handle );
584 tdt->psi->handle = NULL;
589 /*****************************************************************************
591 *****************************************************************************/
592 static int Open( vlc_object_t *p_this )
594 demux_t *p_demux = (demux_t*)p_this;
597 int i_packet_size, i_packet_header_size = 0;
601 /* Search first sync byte */
602 i_packet_size = DetectPacketSize( p_demux, &i_packet_header_size );
603 if( i_packet_size < 0 )
606 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
609 memset( p_sys, 0, sizeof( demux_sys_t ) );
610 vlc_mutex_init( &p_sys->csa_lock );
612 p_demux->pf_demux = Demux;
613 p_demux->pf_control = Control;
615 /* Init p_sys field */
616 p_sys->b_dvb_meta = true;
617 p_sys->b_access_control = true;
618 p_sys->i_current_program = 0;
619 p_sys->programs_list.i_count = 0;
620 p_sys->programs_list.p_values = NULL;
621 p_sys->i_tdt_delta = 0;
622 p_sys->i_dvb_start = 0;
623 p_sys->i_dvb_length = 0;
625 p_sys->arib.b25stream = NULL;
626 p_sys->stream = p_demux->s;
628 p_sys->b_broken_charset = false;
630 for( int i = 0; i < 8192; i++ )
632 ts_pid_t *pid = &p_sys->pid[i];
635 pid->b_valid = false;
637 /* PID 8191 is padding */
638 p_sys->pid[8191].b_seen = true;
639 p_sys->i_packet_size = i_packet_size;
640 p_sys->i_packet_header_size = i_packet_header_size;
641 p_sys->i_ts_read = 50;
643 p_sys->b_start_record = false;
645 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
646 # define VLC_DVBPSI_DEMUX_TABLE_INIT(table,obj) \
648 (table)->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG ); \
649 if( ! (table)->psi->handle ) \
651 vlc_mutex_destroy( &p_sys->csa_lock ); \
655 (table)->psi->handle->p_sys = (void *) VLC_OBJECT(obj); \
656 if( !dvbpsi_AttachDemux( (table)->psi->handle, (dvbpsi_demux_new_cb_t)PSINewTableCallBack, (obj) ) ) \
658 vlc_dvbpsi_reset( obj ); \
659 vlc_mutex_destroy( &p_sys->csa_lock ); \
661 return VLC_EGENERIC; \
666 /* Init PAT handler */
667 pat = &p_sys->pid[0];
668 PIDInit( pat, true, NULL );
669 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
670 pat->psi->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
671 if( !pat->psi->handle )
673 vlc_mutex_destroy( &p_sys->csa_lock );
677 pat->psi->handle->p_sys = (void *) p_demux;
678 if( !dvbpsi_pat_attach( pat->psi->handle, PATCallBack, p_demux ) )
680 vlc_dvbpsi_reset( p_demux );
681 vlc_mutex_destroy( &p_sys->csa_lock );
686 pat->psi->handle = dvbpsi_AttachPAT( PATCallBack, p_demux );
688 if( p_sys->b_dvb_meta )
690 ts_pid_t *sdt = &p_sys->pid[0x11];
691 ts_pid_t *eit = &p_sys->pid[0x12];
693 PIDInit( sdt, true, NULL );
694 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
695 VLC_DVBPSI_DEMUX_TABLE_INIT( sdt, p_demux )
698 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
701 PIDInit( eit, true, NULL );
702 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
703 VLC_DVBPSI_DEMUX_TABLE_INIT( eit, p_demux )
706 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
709 ts_pid_t *tdt = &p_sys->pid[0x14];
710 PIDInit( tdt, true, NULL );
711 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
712 VLC_DVBPSI_DEMUX_TABLE_INIT( tdt, p_demux )
715 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
719 if( p_sys->b_access_control )
721 if( SetPIDFilter( p_demux, 0x11, true ) ||
722 SetPIDFilter( p_demux, 0x14, true ) ||
723 SetPIDFilter( p_demux, 0x12, true ) )
724 p_sys->b_access_control = false;
728 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
729 # undef VLC_DVBPSI_DEMUX_TABLE_INIT
733 TAB_INIT( p_sys->i_pmt, p_sys->pmt );
737 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
739 p_sys->b_trust_pcr = var_CreateGetBool( p_demux, "ts-trust-pcr" );
741 /* We handle description of an extra PMT */
742 char* psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
743 p_sys->b_user_pmt = false;
744 if( psz_string && *psz_string )
745 UserPmt( p_demux, psz_string );
748 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
749 if( psz_string && *psz_string )
754 p_sys->csa = csa_New();
756 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
757 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
758 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
760 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
762 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
765 else if ( i_res == VLC_SUCCESS )
767 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
771 csa_Delete( p_sys->csa );
777 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
778 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
780 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
781 if( i_pkt < 4 || i_pkt > 188 )
783 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
784 msg_Warn( p_demux, "using default packet size of 188 bytes" );
785 p_sys->i_csa_pkt_size = 188;
788 p_sys->i_csa_pkt_size = i_pkt;
789 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
795 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
797 p_sys->i_pid_ref_pcr = -1;
798 p_sys->i_first_pcr = -1;
799 p_sys->i_current_pcr = -1;
800 p_sys->i_last_pcr = -1;
801 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
802 p_sys->i_pcrs_num = 10;
803 p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
804 p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
806 p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
808 if( !p_sys->p_pcrs || !p_sys->p_pos )
814 bool can_seek = false;
815 stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &can_seek );
818 GetFirstPCR( p_demux );
820 GetLastPCR( p_demux );
822 if( p_sys->i_first_pcr < 0 || p_sys->i_last_pcr < 0 )
824 msg_Dbg( p_demux, "Force Seek Per Percent: PCR's not found,");
825 p_sys->b_force_seek_per_percent = true;
828 while( p_sys->i_pmt_es <= 0 && vlc_object_alive( p_demux ) )
830 if( Demux( p_demux ) != 1 )
836 /*****************************************************************************
838 *****************************************************************************/
839 static void Close( vlc_object_t *p_this )
841 demux_t *p_demux = (demux_t*)p_this;
842 demux_sys_t *p_sys = p_demux->p_sys;
844 msg_Dbg( p_demux, "pid list:" );
845 for( int i = 0; i < 8192; i++ )
847 ts_pid_t *pid = &p_sys->pid[i];
849 if( pid->b_valid && pid->psi )
854 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
855 if( dvbpsi_decoder_present( pid->psi->handle ) )
856 dvbpsi_pat_detach( pid->psi->handle );
857 dvbpsi_delete( pid->psi->handle );
858 pid->psi->handle = NULL;
860 dvbpsi_DetachPAT( pid->psi->handle );
868 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
870 /* SDT or EIT or TDT */
871 dvbpsi_DetachDemux( pid->psi->handle );
872 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
873 dvbpsi_delete( pid->psi->handle );
874 pid->psi->handle = NULL;
880 PIDClean( p_demux, pid );
885 else if( pid->b_valid && pid->es )
887 PIDClean( p_demux, pid );
892 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
897 SetPIDFilter( p_demux, pid->i_pid, false );
900 vlc_mutex_lock( &p_sys->csa_lock );
903 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
904 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
905 csa_Delete( p_sys->csa );
907 vlc_mutex_unlock( &p_sys->csa_lock );
909 TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
911 free( p_sys->programs_list.p_values );
913 free( p_sys->p_pcrs );
914 free( p_sys->p_pos );
917 if ( p_sys->arib.p_instance )
918 arib_instance_destroy( p_sys->arib.p_instance );
921 if ( p_sys->arib.b25stream )
923 p_sys->arib.b25stream->p_source = NULL; /* don't chain kill demuxer's source */
924 stream_Delete( p_sys->arib.b25stream );
927 vlc_mutex_destroy( &p_sys->csa_lock );
931 /*****************************************************************************
932 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
933 *****************************************************************************/
934 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
935 vlc_value_t oldval, vlc_value_t newval,
938 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
939 demux_t *p_demux = (demux_t*)p_this;
940 demux_sys_t *p_sys = p_demux->p_sys;
941 int i_tmp = (intptr_t)p_data;
943 vlc_mutex_lock( &p_sys->csa_lock );
945 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
947 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
949 vlc_mutex_unlock( &p_sys->csa_lock );
953 /*****************************************************************************
955 *****************************************************************************/
956 static int Demux( demux_t *p_demux )
958 demux_sys_t *p_sys = p_demux->p_sys;
959 bool b_wait_es = p_sys->i_pmt_es <= 0;
961 /* We read at most 100 TS packet or until a frame is completed */
962 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
964 bool b_frame = false;
966 if( !(p_pkt = ReadTSPacket( p_demux )) )
971 if( p_sys->b_start_record )
973 /* Enable recording once synchronized */
974 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, true, "ts" );
975 p_sys->b_start_record = false;
978 /* Parse the TS packet */
979 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
985 if( p_pid->i_pid == 0 || ( p_sys->b_dvb_meta && ( p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 || p_pid->i_pid == 0x14 ) ) )
987 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
991 for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
993 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
997 block_Release( p_pkt );
1001 b_frame = GatherData( p_demux, p_pid, p_pkt );
1006 if( !p_pid->b_seen )
1008 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1010 /* We have to handle PCR if present */
1011 PCRHandle( p_demux, p_pid, p_pkt );
1012 block_Release( p_pkt );
1014 p_pid->b_seen = true;
1016 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
1020 demux_UpdateTitleFromStream( p_demux );
1024 /*****************************************************************************
1026 *****************************************************************************/
1027 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
1029 demux_sys_t *p_sys = p_demux->p_sys;
1035 if( p_sys->i_dvb_length > 0 )
1037 const int64_t t = mdate() + p_sys->i_tdt_delta;
1039 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
1042 *pi_length = p_sys->i_dvb_length;
1044 *pi_time = t - p_sys->i_dvb_start;
1048 return VLC_EGENERIC;
1051 static int Control( demux_t *p_demux, int i_query, va_list args )
1053 demux_sys_t *p_sys = p_demux->p_sys;
1055 bool b_bool, *pb_bool;
1062 case DEMUX_GET_POSITION:
1063 pf = (double*) va_arg( args, double* );
1065 if( p_sys->b_force_seek_per_percent ||
1066 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1067 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 ||
1068 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1070 int64_t i_time, i_length;
1071 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
1072 *pf = (double)i_time/(double)i_length;
1073 else if( (i64 = stream_Size( p_sys->stream) ) > 0 )
1075 int64_t offset = stream_Tell( p_sys->stream );
1077 *pf = (double)offset / (double)i64;
1084 *pf = (double)(p_sys->i_current_pcr - p_sys->i_first_pcr) / (double)(p_sys->i_last_pcr - p_sys->i_first_pcr);
1088 case DEMUX_SET_POSITION:
1089 f = (double) va_arg( args, double );
1091 if( p_sys->b_force_seek_per_percent ||
1092 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1093 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1095 i64 = stream_Size( p_sys->stream );
1096 if( stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) )
1097 return VLC_EGENERIC;
1101 if( Seek( p_demux, f ) )
1103 p_sys->b_force_seek_per_percent = true;
1104 return VLC_EGENERIC;
1109 case DEMUX_GET_TIME:
1110 pi64 = (int64_t*)va_arg( args, int64_t * );
1111 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1112 p_sys->b_force_seek_per_percent ||
1113 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 )
1115 if( DVBEventInformation( p_demux, pi64, NULL ) )
1122 *pi64 = (p_sys->i_current_pcr - p_sys->i_first_pcr) * 100 / 9;
1126 case DEMUX_GET_LENGTH:
1127 pi64 = (int64_t*)va_arg( args, int64_t * );
1128 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1129 p_sys->b_force_seek_per_percent ||
1130 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1132 if( DVBEventInformation( p_demux, NULL, pi64 ) )
1139 *pi64 = (p_sys->i_last_pcr - p_sys->i_first_pcr) * 100 / 9;
1143 case DEMUX_SET_GROUP:
1147 i_int = (int)va_arg( args, int );
1148 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1149 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1151 if( i_int == 0 && p_sys->i_current_program > 0 )
1152 i_int = p_sys->i_current_program;
1154 if( p_sys->i_current_program > 0 )
1156 if( p_sys->i_current_program != i_int )
1157 SetPrgFilter( p_demux, p_sys->i_current_program, false );
1159 else if( p_sys->i_current_program < 0 )
1161 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
1162 SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
1167 p_sys->i_current_program = i_int;
1168 SetPrgFilter( p_demux, p_sys->i_current_program, true );
1170 else if( i_int < 0 )
1172 p_sys->i_current_program = -1;
1173 p_sys->programs_list.i_count = 0;
1176 vlc_list_t *p_dst = &p_sys->programs_list;
1177 free( p_dst->p_values );
1179 p_dst->p_values = calloc( p_list->i_count,
1180 sizeof(*p_dst->p_values) );
1181 if( p_dst->p_values )
1183 p_dst->i_count = p_list->i_count;
1184 for( int i = 0; i < p_list->i_count; i++ )
1186 p_dst->p_values[i] = p_list->p_values[i];
1187 SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
1195 case DEMUX_GET_TITLE_INFO:
1197 struct input_title_t ***v = va_arg( args, struct input_title_t*** );
1198 int *c = va_arg( args, int * );
1200 *va_arg( args, int* ) = 0; /* Title offset */
1201 *va_arg( args, int* ) = 0; /* Chapter offset */
1202 return stream_Control( p_sys->stream, STREAM_GET_TITLE_INFO, v, c );
1205 case DEMUX_SET_TITLE:
1206 return stream_vaControl( p_sys->stream, STREAM_SET_TITLE, args );
1208 case DEMUX_SET_SEEKPOINT:
1209 return stream_vaControl( p_sys->stream, STREAM_SET_SEEKPOINT, args );
1211 case DEMUX_GET_META:
1212 return stream_vaControl( p_sys->stream, STREAM_GET_META, args );
1214 case DEMUX_CAN_RECORD:
1215 pb_bool = (bool*)va_arg( args, bool * );
1219 case DEMUX_SET_RECORD_STATE:
1220 b_bool = (bool)va_arg( args, int );
1223 stream_Control( p_sys->stream, STREAM_SET_RECORD_STATE, false );
1224 p_sys->b_start_record = b_bool;
1227 case DEMUX_GET_SIGNAL:
1228 return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
1231 return VLC_EGENERIC;
1235 /*****************************************************************************
1237 *****************************************************************************/
1238 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1240 demux_sys_t *p_sys = p_demux->p_sys;
1241 char *psz_dup = strdup( psz_fmt );
1242 char *psz = psz_dup;
1245 ts_prg_psi_t *prg = NULL;
1251 i_pid = strtol( psz, &psz, 0 );
1252 if( i_pid < 2 || i_pid >= 8192 )
1255 /* Parse optional program number */
1258 i_number = strtol( &psz[1], &psz, 0 );
1261 ts_pid_t *pmt = &p_sys->pid[i_pid];
1263 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1264 PIDInit( pmt, true, NULL );
1267 prg = calloc( 1, sizeof( ts_prg_psi_t ) );
1271 prg->i_pid_pcr = -1;
1272 prg->i_pid_pmt = -1;
1273 prg->i_version = -1;
1274 prg->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1275 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1276 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
1279 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
1280 if( !dvbpsi_pmt_attach( prg->handle,
1281 ((i_number != TS_USER_PMT_NUMBER ? i_number : 1)),
1282 PMTCallBack, p_demux ) )
1284 dvbpsi_delete( prg->handle );
1289 prg->handle = dvbpsi_AttachPMT(
1290 ((i_number != TS_USER_PMT_NUMBER) ? i_number : 1),
1291 PMTCallBack, p_demux );
1293 TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1295 psz = strchr( psz, '=' );
1298 while( psz && *psz )
1300 char *psz_next = strchr( psz, ',' );
1306 i_pid = strtol( psz, &psz, 0 );
1307 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1310 char *psz_opt = &psz[1];
1311 if( !strcmp( psz_opt, "pcr" ) )
1313 prg->i_pid_pcr = i_pid;
1315 else if( !p_sys->pid[i_pid].b_valid )
1317 ts_pid_t *pid = &p_sys->pid[i_pid];
1319 char *psz_arg = strchr( psz_opt, '=' );
1323 PIDInit( pid, false, pmt->psi);
1324 if( prg->i_pid_pcr <= 0 )
1325 prg->i_pid_pcr = i_pid;
1327 if( psz_arg && strlen( psz_arg ) == 4 )
1329 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1330 psz_arg[2], psz_arg[3] );
1331 int i_cat = UNKNOWN_ES;
1332 es_format_t *fmt = &pid->es->fmt;
1334 if( !strcmp( psz_opt, "video" ) )
1336 else if( !strcmp( psz_opt, "audio" ) )
1338 else if( !strcmp( psz_opt, "spu" ) )
1341 es_format_Init( fmt, i_cat, i_codec );
1342 fmt->b_packetized = false;
1346 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1347 PIDFillFormat( &pid->es->fmt, i_stream_type );
1349 pid->es->fmt.i_group = i_number;
1350 if( p_sys->b_es_id_pid )
1351 pid->es->fmt.i_id = i_pid;
1353 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1355 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1356 (char*)&pid->es->fmt.i_codec );
1357 pid->es->id = es_out_Add( p_demux->out,
1367 p_sys->b_user_pmt = true;
1368 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1375 return VLC_EGENERIC;
1378 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1380 demux_sys_t *p_sys = p_demux->p_sys;
1382 if( !p_sys->b_access_control )
1383 return VLC_EGENERIC;
1385 return stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_STATE,
1386 i_pid, b_selected );
1389 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1391 demux_sys_t *p_sys = p_demux->p_sys;
1392 ts_prg_psi_t *p_prg = NULL;
1395 /* Search pmt to be unselected */
1396 for( int i = 0; i < p_sys->i_pmt; i++ )
1398 ts_pid_t *pmt = p_sys->pmt[i];
1400 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1402 if( pmt->psi->prg[i_prg]->i_number == i_prg_id )
1404 i_pmt_pid = p_sys->pmt[i]->i_pid;
1405 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1412 if( i_pmt_pid <= 0 )
1416 SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1417 if( p_prg->i_pid_pcr > 0 )
1418 SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
1421 for( int i = 2; i < 8192; i++ )
1423 ts_pid_t *pid = &p_sys->pid[i];
1425 if( !pid->b_valid || pid->psi )
1428 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1430 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1432 /* We only remove/select es that aren't defined by extra pmt */
1433 SetPIDFilter( p_demux, i, b_selected );
1440 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1442 bool b_old_valid = pid->b_valid;
1444 pid->b_valid = true;
1446 pid->b_scrambled = false;
1447 pid->p_owner = p_owner;
1448 pid->i_owner_number = 0;
1450 TAB_INIT( pid->i_extra_es, pid->extra_es );
1458 pid->psi = xmalloc( sizeof( ts_psi_t ) );
1459 pid->psi->handle = NULL;
1460 TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1464 pid->psi->i_pat_version = -1;
1465 pid->psi->i_sdt_version = -1;
1468 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1472 prg->i_version = -1;
1474 prg->i_pid_pcr = -1;
1475 prg->i_pid_pmt = -1;
1476 prg->i_pcr_value= -1;
1480 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1486 pid->es = calloc( 1, sizeof( ts_es_t ) );
1490 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1491 pid->es->data_type = TS_ES_DATA_PES;
1492 pid->es->pp_last = &pid->es->p_data;
1496 static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
1498 demux_sys_t *p_sys = p_demux->p_sys;
1499 es_out_t *out = p_demux->out;
1503 if( pid->psi->handle )
1505 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1506 if( dvbpsi_decoder_present( pid->psi->handle ) )
1507 dvbpsi_pmt_detach( pid->psi->handle );
1508 dvbpsi_delete( pid->psi->handle );
1509 pid->psi->handle = NULL;
1511 dvbpsi_DetachPMT( pid->psi->handle );
1514 for( int i = 0; i < pid->psi->i_prg; i++ )
1516 if( pid->psi->prg[i]->iod )
1517 IODFree( pid->psi->prg[i]->iod );
1518 if( pid->psi->prg[i]->handle )
1520 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
1521 if( dvbpsi_decoder_present( pid->psi->prg[i]->handle ) )
1522 dvbpsi_pmt_detach( pid->psi->prg[i]->handle );
1523 dvbpsi_delete( pid->psi->prg[i]->handle );
1525 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1528 free( pid->psi->prg[i] );
1530 free( pid->psi->prg );
1537 es_out_Del( out, pid->es->id );
1541 if( pid->es->p_data )
1542 block_ChainRelease( pid->es->p_data );
1544 es_format_Clean( &pid->es->fmt );
1548 for( int i = 0; i < pid->i_extra_es; i++ )
1550 if( pid->extra_es[i]->id )
1552 es_out_Del( out, pid->extra_es[i]->id );
1556 if( pid->extra_es[i]->p_data )
1557 block_ChainRelease( pid->extra_es[i]->p_data );
1559 es_format_Clean( &pid->extra_es[i]->fmt );
1561 free( pid->extra_es[i] );
1563 if( pid->i_extra_es )
1564 free( pid->extra_es );
1567 pid->b_valid = false;
1570 static int16_t read_opus_flag(uint8_t **buf, size_t *len)
1575 int16_t ret = ((*buf)[0] << 8) | (*buf)[1];
1586 static block_t *Opus_Parse(demux_t *demux, block_t *block)
1588 block_t *out = NULL;
1589 block_t **last = NULL;
1591 uint8_t *buf = block->p_buffer;
1592 size_t len = block->i_buffer;
1594 while (len > 3 && ((buf[0] << 3) | (buf[1] >> 5)) == 0x3ff) {
1595 int16_t start_trim = 0, end_trim = 0;
1596 int start_trim_flag = (buf[1] >> 4) & 1;
1597 int end_trim_flag = (buf[1] >> 3) & 1;
1598 int control_extension_flag = (buf[1] >> 2) & 1;
1603 unsigned au_size = 0;
1611 if (start_trim_flag) {
1612 start_trim = read_opus_flag(&buf, &len);
1613 if (start_trim < 0) {
1614 msg_Err(demux, "Invalid start trimming flag");
1617 if (end_trim_flag) {
1618 end_trim = read_opus_flag(&buf, &len);
1620 msg_Err(demux, "Invalid end trimming flag");
1623 if (control_extension_flag && len) {
1624 unsigned l = *buf++; len--;
1626 msg_Err(demux, "Invalid control extension length %d > %zu", l, len);
1633 if (!au_size || au_size > len) {
1634 msg_Err(demux, "Invalid Opus AU size %d (PES %zu)", au_size, len);
1638 block_t *au = block_Alloc(au_size);
1641 memcpy(au->p_buffer, buf, au_size);
1642 block_CopyProperties(au, block);
1651 au->i_nb_samples = opus_frame_duration(buf, au_size);
1652 if (end_trim && end_trim <= au->i_nb_samples)
1653 au->i_length = end_trim; /* Blatant abuse of the i_length field. */
1657 if (start_trim && start_trim < (au->i_nb_samples - au->i_length)) {
1658 au->i_nb_samples -= start_trim;
1659 if (au->i_nb_samples == 0)
1660 au->i_flags |= BLOCK_FLAG_PREROLL;
1667 block_Release(block);
1671 /****************************************************************************
1673 ****************************************************************************/
1674 static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
1676 demux_sys_t *p_sys = p_demux->p_sys;
1678 unsigned i_pes_size = 0;
1679 unsigned i_skip = 0;
1682 mtime_t i_length = 0;
1684 /* FIXME find real max size */
1685 /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
1687 if( pid->b_scrambled || header[0] != 0 || header[1] != 0 || header[2] != 1 )
1689 if ( !pid->b_scrambled )
1690 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
1691 header[0], header[1],header[2],header[3], pid->i_pid );
1692 block_ChainRelease( p_pes );
1696 /* TODO check size */
1699 case 0xBC: /* Program stream map */
1700 case 0xBE: /* Padding */
1701 case 0xBF: /* Private stream 2 */
1702 case 0xF0: /* ECM */
1703 case 0xF1: /* EMM */
1704 case 0xFF: /* Program stream directory */
1705 case 0xF2: /* DSMCC stream */
1706 case 0xF8: /* ITU-T H.222.1 type E stream */
1710 if( ( header[6]&0xC0 ) == 0x80 )
1713 i_skip = header[8] + 9;
1715 if( header[7]&0x80 ) /* has pts */
1717 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1718 (mtime_t)(header[10] << 22)|
1719 ((mtime_t)(header[11]&0xfe) << 14)|
1720 (mtime_t)(header[12] << 7)|
1721 (mtime_t)(header[13] >> 1);
1723 if( header[7]&0x40 ) /* has dts */
1725 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1726 (mtime_t)(header[15] << 22)|
1727 ((mtime_t)(header[16]&0xfe) << 14)|
1728 (mtime_t)(header[17] << 7)|
1729 (mtime_t)(header[18] >> 1);
1736 while( i_skip < 23 && header[i_skip] == 0xff )
1742 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1743 block_ChainRelease( p_pes );
1746 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1751 if( header[i_skip]&0x20 )
1753 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1754 (mtime_t)(header[i_skip+1] << 22)|
1755 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1756 (mtime_t)(header[i_skip+3] << 7)|
1757 (mtime_t)(header[i_skip+4] >> 1);
1759 if( header[i_skip]&0x10 ) /* has dts */
1761 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1762 (mtime_t)(header[i_skip+6] << 22)|
1763 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1764 (mtime_t)(header[i_skip+8] << 7)|
1765 (mtime_t)(header[i_skip+9] >> 1);
1781 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1782 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1786 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1787 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1788 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1792 else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
1793 pid->es->p_mpeg4desc )
1795 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1797 if( dcd->i_extra > 2 &&
1798 dcd->p_extra[0] == 0x10 &&
1799 ( dcd->p_extra[1]&0x10 ) )
1801 /* display length */
1802 if( p_pes->i_buffer + 2 <= i_skip )
1803 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1807 if( p_pes->i_buffer + 2 <= i_skip )
1808 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1814 while( p_pes && i_skip > 0 )
1816 if( p_pes->i_buffer <= i_skip )
1818 block_t *p_next = p_pes->p_next;
1820 i_skip -= p_pes->i_buffer;
1821 block_Release( p_pes );
1826 p_pes->i_buffer -= i_skip;
1827 p_pes->p_buffer += i_skip;
1832 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1833 if( i_pts >= 0 && i_dts < 0 )
1841 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
1844 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
1846 p_pes->i_length = i_length * 100 / 9;
1848 p_block = block_ChainGather( p_pes );
1849 if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
1851 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1853 p_block->i_buffer = i_pes_size;
1856 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1859 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1861 else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
1863 if( p_block->i_pts <= VLC_TS_INVALID )
1865 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
1866 * In this case use the last PCR + 40ms */
1867 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
1869 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
1871 mtime_t i_pcr = pid->p_owner->prg[i]->i_pcr_value;
1872 if( i_pcr > VLC_TS_INVALID )
1873 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
1879 else if( pid->es->fmt.i_codec == VLC_CODEC_ARIB_A ||
1880 pid->es->fmt.i_codec == VLC_CODEC_ARIB_C )
1882 if( p_block->i_pts <= VLC_TS_INVALID )
1884 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1886 p_block->i_buffer = i_pes_size;
1889 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1892 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1895 else if( pid->es->fmt.i_codec == VLC_CODEC_OPUS)
1897 p_block = Opus_Parse(p_demux, p_block);
1901 block_t *p_next = p_block->p_next;
1902 p_block->p_next = NULL;
1903 for( int i = 0; i < pid->i_extra_es; i++ )
1905 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1906 block_Duplicate( p_block ) );
1909 if (!p_sys->b_trust_pcr)
1910 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1911 pid->i_owner_number, p_block->i_pts);
1913 es_out_Send( p_demux->out, pid->es->id, p_block );
1920 msg_Warn( p_demux, "empty pes" );
1924 static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
1926 block_t *p_content = block_ChainGather( p_data );
1927 mtime_t i_date = -1;
1928 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
1930 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
1932 i_date = pid->p_owner->prg[i]->i_pcr_value;
1939 if( pid->es->fmt.i_codec == VLC_CODEC_SCTE_27 )
1941 /* We need to extract the truncated pts stored inside the payload */
1942 if( p_content->i_buffer > 9 && p_content->p_buffer[0] == 0xc6 )
1945 size_t i_offset = 4;
1946 if( p_content->p_buffer[3] & 0x40 )
1948 i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
1949 p_content->p_buffer[8];
1952 if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
1954 bool is_immediate = p_content->p_buffer[i_offset + 3] & 0x40;
1957 mtime_t i_display_in = GetDWBE( &p_content->p_buffer[i_offset + 4] );
1958 if( i_display_in < i_date )
1959 i_date = i_display_in + (1ll << 32);
1961 i_date = i_display_in;
1968 p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
1970 es_out_Send( p_demux->out, pid->es->id, p_content );
1972 static void ParseData( demux_t *p_demux, ts_pid_t *pid )
1974 block_t *p_data = pid->es->p_data;
1976 /* remove the pes from pid */
1977 pid->es->p_data = NULL;
1978 pid->es->i_data_size = 0;
1979 pid->es->i_data_gathered = 0;
1980 pid->es->pp_last = &pid->es->p_data;
1982 if( pid->es->data_type == TS_ES_DATA_PES )
1984 ParsePES( p_demux, pid, p_data );
1986 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
1988 ParseTableSection( p_demux, pid, p_data );
1992 block_ChainRelease( p_data );
1996 static block_t* ReadTSPacket( demux_t *p_demux )
1998 demux_sys_t *p_sys = p_demux->p_sys;
2002 /* Get a new TS packet */
2003 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2005 msg_Dbg( p_demux, "eof ?" );
2009 /* Skip header (BluRay streams).
2010 * re-sync logic would do this (by adjusting packet start), but this would result in losing first and last ts packets.
2011 * First packet is usually PAT, and losing it means losing whole first GOP. This is fatal with still-image based menus.
2013 p_pkt->p_buffer += p_sys->i_packet_header_size;
2014 p_pkt->i_buffer -= p_sys->i_packet_header_size;
2016 /* Check sync byte and re-sync if needed */
2017 if( p_pkt->p_buffer[0] != 0x47 )
2019 msg_Warn( p_demux, "lost synchro" );
2020 block_Release( p_pkt );
2021 while( vlc_object_alive (p_demux) )
2023 const uint8_t *p_peek;
2024 int i_peek, i_skip = 0;
2026 i_peek = stream_Peek( p_sys->stream, &p_peek,
2027 p_sys->i_packet_size * 10 );
2028 if( i_peek < p_sys->i_packet_size + 1 )
2030 msg_Dbg( p_demux, "eof ?" );
2034 while( i_skip < i_peek - p_sys->i_packet_size )
2036 if( p_peek[i_skip + p_sys->i_packet_header_size] == 0x47 &&
2037 p_peek[i_skip + p_sys->i_packet_header_size + p_sys->i_packet_size] == 0x47 )
2043 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
2044 stream_Read( p_sys->stream, NULL, i_skip );
2046 if( i_skip < i_peek - p_sys->i_packet_size )
2051 if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
2053 msg_Dbg( p_demux, "eof ?" );
2060 static mtime_t AdjustPCRWrapAround( demux_t *p_demux, mtime_t i_pcr )
2062 demux_sys_t *p_sys = p_demux->p_sys;
2064 * PCR is 33bit. If PCR reaches to 0x1FFFFFFFF (26:30:43.717), ressets from 0.
2065 * So, need to add 0x1FFFFFFFF, for calculating duration or current position.
2067 mtime_t i_adjust = 0;
2068 int64_t i_pos = stream_Tell( p_sys->stream );
2070 for( i = 1; i < p_sys->i_pcrs_num && p_sys->p_pos[i] <= i_pos; ++i )
2072 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2073 i_adjust += 0x1FFFFFFFF;
2075 if( p_sys->p_pcrs[i-1] > i_pcr )
2076 i_adjust += 0x1FFFFFFFF;
2078 return i_pcr + i_adjust;
2081 static mtime_t GetPCR( block_t *p_pkt )
2083 const uint8_t *p = p_pkt->p_buffer;
2087 if( ( p[3]&0x20 ) && /* adaptation */
2091 /* PCR is 33 bits */
2092 i_pcr = ( (mtime_t)p[6] << 25 ) |
2093 ( (mtime_t)p[7] << 17 ) |
2094 ( (mtime_t)p[8] << 9 ) |
2095 ( (mtime_t)p[9] << 1 ) |
2096 ( (mtime_t)p[10] >> 7 );
2101 static int SeekToPCR( demux_t *p_demux, int64_t i_pos )
2103 demux_sys_t *p_sys = p_demux->p_sys;
2106 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2109 return VLC_EGENERIC;
2111 int64_t i_last_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
2112 if( i_pos > i_last_pos )
2115 if( stream_Seek( p_sys->stream, i_pos ) )
2116 return VLC_EGENERIC;
2118 while( vlc_object_alive( p_demux ) )
2122 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2126 if( PIDGet( p_pkt ) == p_sys->i_pid_ref_pcr )
2128 i_pcr = GetPCR( p_pkt );
2130 block_Release( p_pkt );
2133 if( stream_Tell( p_sys->stream ) >= i_last_pos )
2138 stream_Seek( p_sys->stream, i_initial_pos );
2139 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2140 return VLC_EGENERIC;
2143 p_sys->i_current_pcr = i_pcr;
2147 static int Seek( demux_t *p_demux, double f_percent )
2149 demux_sys_t *p_sys = p_demux->p_sys;
2151 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2152 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2155 * Find the time position by using binary search algorithm.
2157 mtime_t i_target_pcr = (p_sys->i_last_pcr - p_sys->i_first_pcr) * f_percent + p_sys->i_first_pcr;
2159 int64_t i_head_pos = 0;
2162 mtime_t i_adjust = 0;
2164 for( i = 1; i < p_sys->i_pcrs_num; ++i )
2166 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2167 i_adjust += 0x1FFFFFFFF;
2168 if( p_sys->p_pcrs[i] + i_adjust > i_target_pcr )
2171 i_head_pos = p_sys->p_pos[i-1];
2172 i_tail_pos = ( i < p_sys->i_pcrs_num ) ? p_sys->p_pos[i] : stream_Size( p_sys->stream );
2174 msg_Dbg( p_demux, "Seek():i_head_pos:%"PRId64", i_tail_pos:%"PRId64, i_head_pos, i_tail_pos);
2176 bool b_found = false;
2178 while( i_head_pos <= i_tail_pos )
2180 /* Round i_pos to a multiple of p_sys->i_packet_size */
2181 int64_t i_pos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
2182 int64_t i_div = i_pos % p_sys->i_packet_size;
2184 if( SeekToPCR( p_demux, i_pos ) )
2186 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2187 int64_t i_diff_msec = (p_sys->i_current_pcr - i_target_pcr) * 100 / 9 / 1000;
2188 if( i_diff_msec > 500 )
2190 i_tail_pos = i_pos - p_sys->i_packet_size;
2192 else if( i_diff_msec < -500 )
2194 i_head_pos = i_pos + p_sys->i_packet_size;
2198 // diff time <= 500msec
2206 msg_Dbg( p_demux, "Seek():cannot find a time position. i_cnt:%d", i_cnt );
2207 stream_Seek( p_sys->stream, i_initial_pos );
2208 p_sys->i_current_pcr = i_initial_pcr;
2209 return VLC_EGENERIC;
2213 msg_Dbg( p_demux, "Seek():can find a time position. i_cnt:%d", i_cnt );
2218 static void GetFirstPCR( demux_t *p_demux )
2220 demux_sys_t *p_sys = p_demux->p_sys;
2222 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2224 if( stream_Seek( p_sys->stream, 0 ) )
2227 while( vlc_object_alive (p_demux) )
2231 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
2235 mtime_t i_pcr = GetPCR( p_pkt );
2238 p_sys->i_pid_ref_pcr = PIDGet( p_pkt );
2239 p_sys->i_first_pcr = i_pcr;
2240 p_sys->i_current_pcr = i_pcr;
2242 block_Release( p_pkt );
2243 if( p_sys->i_first_pcr >= 0 )
2246 stream_Seek( p_sys->stream, i_initial_pos );
2249 static void GetLastPCR( demux_t *p_demux )
2251 demux_sys_t *p_sys = p_demux->p_sys;
2253 const int64_t i_initial_pos = stream_Tell( p_sys->stream );
2254 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2256 int64_t i_stream_size = stream_Size( p_sys->stream );
2257 int64_t i_last_pos = i_stream_size - p_sys->i_packet_size;
2258 /* Round i_pos to a multiple of p_sys->i_packet_size */
2259 int64_t i_pos = i_last_pos - p_sys->i_packet_size * 4500; /* FIXME if the value is not reasonable, please change it. */
2260 int64_t i_div = i_pos % p_sys->i_packet_size;
2263 if( i_pos <= i_initial_pos && i_pos >= i_stream_size )
2264 i_pos = i_initial_pos + p_sys->i_packet_size;
2265 if( i_pos < 0 && i_pos >= i_stream_size )
2268 while( vlc_object_alive( p_demux ) )
2270 if( SeekToPCR( p_demux, i_pos ) )
2272 p_sys->i_last_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
2273 if( ( i_pos = stream_Tell( p_sys->stream ) ) >= i_last_pos )
2276 if( p_sys->i_last_pcr >= 0 )
2278 int64_t i_size = stream_Size( p_sys->stream );
2279 mtime_t i_duration_msec = ( p_sys->i_last_pcr - p_sys->i_first_pcr ) * 100 / 9 / 1000;
2280 int64_t i_rate = ( i_size < 0 || i_duration_msec <= 0 ) ? 0 : i_size * 1000 * 8 / i_duration_msec;
2281 const int64_t TS_SUPPOSED_MAXRATE = 55 * 1000 * 1000; //FIXME I think it's enough.
2282 const int64_t TS_SUPPOSED_MINRATE = 0.5 * 1000 * 1000; //FIXME
2283 if( i_rate < TS_SUPPOSED_MINRATE || i_rate > TS_SUPPOSED_MAXRATE )
2285 msg_Dbg( p_demux, "calculated bitrate (%"PRId64"bit/s) is too low or too high. min bitrate (%"PRId64"bit/s) max bitrate (%"PRId64"bit/s)",
2286 i_rate, TS_SUPPOSED_MINRATE, TS_SUPPOSED_MAXRATE );
2287 p_sys->i_last_pcr = -1;
2290 stream_Seek( p_sys->stream, i_initial_pos );
2291 assert( i_initial_pos == stream_Tell( p_sys->stream ) );
2292 p_sys->i_current_pcr = i_initial_pcr;
2295 static void CheckPCR( demux_t *p_demux )
2297 demux_sys_t *p_sys = p_demux->p_sys;
2299 int64_t i_initial_pos = stream_Tell( p_sys->stream );
2300 mtime_t i_initial_pcr = p_sys->i_current_pcr;
2302 int64_t i_size = stream_Size( p_sys->stream );
2305 p_sys->p_pcrs[0] = p_sys->i_first_pcr;
2306 p_sys->p_pos[0] = i_initial_pos;
2308 for( i = 1; i < p_sys->i_pcrs_num && vlc_object_alive( p_demux ); ++i )
2310 /* Round i_pos to a multiple of p_sys->i_packet_size */
2311 int64_t i_pos = i_size / p_sys->i_pcrs_num * i;
2312 int64_t i_div = i_pos % p_sys->i_packet_size;
2314 if( SeekToPCR( p_demux, i_pos ) )
2316 p_sys->p_pcrs[i] = p_sys->i_current_pcr;
2317 p_sys->p_pos[i] = stream_Tell( p_sys->stream );
2318 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
2320 msg_Dbg( p_demux, "PCR Wrap Around found between %d%% and %d%% (pcr:%"PRId64"(0x%09"PRIx64") pcr:%"PRId64"(0x%09"PRIx64"))",
2321 (int)((i-1)*100/p_sys->i_pcrs_num), (int)(i*100/p_sys->i_pcrs_num), p_sys->p_pcrs[i-1], p_sys->p_pcrs[i-1], p_sys->p_pcrs[i], p_sys->p_pcrs[i] );
2324 if( i < p_sys->i_pcrs_num )
2326 msg_Dbg( p_demux, "Force Seek Per Percent: Seeking failed at %d%%.", (int)(i*100/p_sys->i_pcrs_num) );
2327 p_sys->b_force_seek_per_percent = true;
2330 stream_Seek( p_sys->stream, i_initial_pos );
2331 p_sys->i_current_pcr = i_initial_pcr;
2334 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2336 demux_sys_t *p_sys = p_demux->p_sys;
2338 if( p_sys->i_pmt_es <= 0 )
2341 mtime_t i_pcr = GetPCR( p_bk );
2345 if( p_sys->i_pid_ref_pcr == pid->i_pid )
2346 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
2348 /* Search program and set the PCR */
2350 for( int i = 0; i < p_sys->i_pmt && i_group < 0 ; i++ )
2352 bool b_pmt_has_es = false;
2354 for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2356 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
2358 /* We've found our target group */
2359 p_sys->pmt[i]->psi->prg[i_prg]->i_pcr_value = i_pcr;
2360 i_group = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
2361 for( int j = 0; j < 8192; j++ )
2363 const ts_pid_t *pid = &p_sys->pid[j];
2364 if( pid->b_valid && pid->p_owner == p_sys->pmt[i]->psi && pid->es )
2366 b_pmt_has_es = true;
2373 if ( p_sys->b_trust_pcr && i_group > 0 && b_pmt_has_es )
2374 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
2375 i_group, VLC_TS_0 + i_pcr * 100 / 9 );
2379 static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2381 const uint8_t *p = p_bk->p_buffer;
2382 const bool b_unit_start = p[1]&0x40;
2383 const bool b_scrambled = p[3]&0x80;
2384 const bool b_adaptation = p[3]&0x20;
2385 const bool b_payload = p[3]&0x10;
2386 const int i_cc = p[3]&0x0f; /* continuity counter */
2387 bool b_discontinuity = false; /* discontinuity */
2389 /* transport_scrambling_control is ignored */
2394 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
2395 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
2399 /* For now, ignore additional error correction
2400 * TODO: handle Reed-Solomon 204,188 error correction */
2401 p_bk->i_buffer = TS_PACKET_SIZE_188;
2405 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
2407 if( pid->es->p_data ) //&& pid->es->fmt.i_cat == VIDEO_ES )
2408 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2411 if( p_demux->p_sys->csa )
2413 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
2414 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
2415 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
2420 /* We don't have any adaptation_field, so payload starts
2421 * immediately after the 4 byte TS header */
2426 /* p[4] is adaptation_field_length minus one */
2430 /* discontinuity indicator found in stream */
2431 b_discontinuity = (p[5]&0x80) ? true : false;
2432 if( b_discontinuity && pid->es->p_data )
2434 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
2436 /* pid->es->p_data->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
2440 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
2445 /* Test continuity counter */
2446 /* continuous when (one of this):
2448 * diff == 0 and payload == 0
2449 * diff == 0 and duplicate packet (playload != 0) <- should we
2450 * test the content ?
2452 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
2453 if( b_payload && i_diff == 1 )
2455 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
2459 if( pid->i_cc == 0xff )
2461 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
2465 else if( i_diff != 0 && !b_discontinuity )
2467 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
2468 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
2471 if( pid->es->p_data && pid->es->fmt.i_cat != VIDEO_ES &&
2472 pid->es->fmt.i_cat != AUDIO_ES )
2474 /* Small audio/video artifacts are usually better than
2475 * dropping full frames */
2476 pid->es->p_data->i_flags |= BLOCK_FLAG_CORRUPTED;
2481 PCRHandle( p_demux, pid, p_bk );
2483 if( i_skip >= 188 || pid->es->id == NULL )
2485 block_Release( p_bk );
2490 if( !pid->b_scrambled != !b_scrambled )
2492 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2493 pid->i_pid, pid->b_scrambled, b_scrambled );
2495 pid->b_scrambled = b_scrambled;
2497 for( int i = 0; i < pid->i_extra_es; i++ )
2499 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2500 pid->extra_es[i]->id, b_scrambled );
2502 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2503 pid->es->id, b_scrambled );
2506 /* We have to gather it */
2507 p_bk->p_buffer += i_skip;
2508 p_bk->i_buffer -= i_skip;
2512 if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION && p_bk->i_buffer > 0 )
2514 int i_pointer_field = __MIN( p_bk->p_buffer[0], p_bk->i_buffer - 1 );
2515 block_t *p = block_Duplicate( p_bk );
2518 p->i_buffer = i_pointer_field;
2520 block_ChainLastAppend( &pid->es->pp_last, p );
2522 p_bk->i_buffer -= 1 + i_pointer_field;
2523 p_bk->p_buffer += 1 + i_pointer_field;
2525 if( pid->es->p_data )
2527 ParseData( p_demux, pid );
2531 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2532 if( pid->es->data_type == TS_ES_DATA_PES )
2534 if( p_bk->i_buffer > 6 )
2536 pid->es->i_data_size = GetWBE( &p_bk->p_buffer[4] );
2537 if( pid->es->i_data_size > 0 )
2539 pid->es->i_data_size += 6;
2543 else if( pid->es->data_type == TS_ES_DATA_TABLE_SECTION )
2545 if( p_bk->i_buffer > 3 && p_bk->p_buffer[0] != 0xff )
2547 pid->es->i_data_size = 3 + (((p_bk->p_buffer[1] & 0xf) << 8) | p_bk->p_buffer[2]);
2550 pid->es->i_data_gathered += p_bk->i_buffer;
2551 if( pid->es->i_data_size > 0 &&
2552 pid->es->i_data_gathered >= pid->es->i_data_size )
2554 ParseData( p_demux, pid );
2560 if( pid->es->p_data == NULL )
2562 /* msg_Dbg( p_demux, "broken packet" ); */
2563 block_Release( p_bk );
2567 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2568 pid->es->i_data_gathered += p_bk->i_buffer;
2570 if( pid->es->i_data_size > 0 &&
2571 pid->es->i_data_gathered >= pid->es->i_data_size )
2573 ParseData( p_demux, pid );
2582 static void PIDFillFormat( es_format_t *fmt, int i_stream_type )
2584 switch( i_stream_type )
2586 case 0x01: /* MPEG-1 video */
2587 case 0x02: /* MPEG-2 video */
2588 case 0x80: /* MPEG-2 MOTO video */
2589 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
2591 case 0x03: /* MPEG-1 audio */
2592 case 0x04: /* MPEG-2 audio */
2593 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
2595 case 0x11: /* MPEG4 (audio) LATM */
2596 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
2597 case 0x1c: /* ISO/IEC 14496-3 Audio, without using any additional
2598 transport syntax, such as DST, ALS and SLS */
2599 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
2601 case 0x10: /* MPEG4 (video) */
2602 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
2604 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
2605 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
2607 case 0x24: /* HEVC */
2608 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_HEVC );
2610 case 0x42: /* CAVS (Chinese AVS) */
2611 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
2614 case 0x81: /* A52 (audio) */
2615 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
2617 case 0x82: /* SCTE-27 (sub) */
2618 es_format_Init( fmt, SPU_ES, VLC_CODEC_SCTE_27 );
2620 case 0x84: /* SDDS (audio) */
2621 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
2623 case 0x85: /* DTS (audio) */
2624 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
2626 case 0x87: /* E-AC3 */
2627 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
2630 case 0x91: /* A52 vls (audio) */
2631 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
2633 case 0x92: /* DVD_SPU vls (sub) */
2634 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
2637 case 0x94: /* SDDS (audio) */
2638 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
2641 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
2642 es_format_Init( fmt, UNKNOWN_ES, 0 );
2645 case 0x06: /* PES_PRIVATE (fixed later) */
2646 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
2647 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
2649 es_format_Init( fmt, UNKNOWN_ES, 0 );
2653 /* PES packets usually contain truncated frames */
2654 fmt->b_packetized = false;
2657 /*****************************************************************************
2658 * MP4 specific functions (IOD parser)
2659 *****************************************************************************/
2660 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
2663 unsigned int i_len = 0;
2669 i_len = ( i_len << 7 ) + ( i_b&0x7f );
2671 } while( i_b&0x80 && *pi_data > 0 );
2673 if (i_len > *pi_data)
2679 static int IODGetBytes( int *pi_data, uint8_t **pp_data, size_t bytes )
2682 while( *pi_data > 0 && bytes-- )
2693 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
2695 int len = IODGetBytes( pi_data, pp_data, 1 );
2698 char *url = strndup( (char*)*pp_data, len );
2704 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2706 uint8_t i_iod_tag, i_iod_label, byte1, byte2, byte3;
2708 iod_descriptor_t *p_iod = calloc( 1, sizeof( iod_descriptor_t ) );
2717 byte1 = IODGetBytes( &i_data, &p_data, 1 );
2718 byte2 = IODGetBytes( &i_data, &p_data, 1 );
2719 byte3 = IODGetBytes( &i_data, &p_data, 1 );
2720 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2722 i_iod_label = byte1;
2725 else //correct implementation of the IOD_descriptor
2727 i_iod_label = byte2;
2731 ts_debug( "\n* iod label:%d tag:0x%x", i_iod_label, i_iod_tag );
2733 if( i_iod_tag != 0x02 )
2735 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
2739 IODDescriptorLength( &i_data, &p_data );
2741 uint16_t i_od_id = ( IODGetBytes( &i_data, &p_data, 1 ) << 2 );
2742 uint8_t i_flags = IODGetBytes( &i_data, &p_data, 1 );
2743 i_od_id |= i_flags >> 6;
2744 ts_debug( "\n* od_id:%d", i_od_id );
2745 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2746 if ((i_flags >> 5) & 0x01)
2748 p_iod->psz_url = IODGetURL( &i_data, &p_data );
2749 ts_debug( "\n* url string:%s", p_iod->psz_url );
2750 ts_debug( "\n*****************************\n" );
2755 p_iod->psz_url = NULL;
2758 /* Profile Level Indication */
2759 IODGetBytes( &i_data, &p_data, 1 ); /* OD */
2760 IODGetBytes( &i_data, &p_data, 1 ); /* scene */
2761 IODGetBytes( &i_data, &p_data, 1 ); /* audio */
2762 IODGetBytes( &i_data, &p_data, 1 ); /* visual */
2763 IODGetBytes( &i_data, &p_data, 1 ); /* graphics */
2766 int i_data_sav = i_data;
2767 uint8_t *p_data_sav = p_data;
2768 for (int i = 0; i_data > 0 && i < ES_DESCRIPTOR_COUNT; i++)
2770 es_mpeg4_descriptor_t *es_descr = &p_iod->es_descr[i];
2772 p_data = p_data_sav + i_length;
2773 i_data = i_data_sav - i_length;
2775 int i_tag = IODGetBytes( &i_data, &p_data, 1 );
2776 i_length = IODDescriptorLength( &i_data, &p_data );
2778 i_data_sav = i_data;
2779 p_data_sav = p_data;
2783 if ( i_tag != 0x03 )
2785 ts_debug( "\n* - OD tag:0x%x Unsupported", i_tag );
2789 es_descr->i_es_id = IODGetBytes( &i_data, &p_data, 2 );
2790 int i_flags = IODGetBytes( &i_data, &p_data, 1 );
2791 bool b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2792 if( b_streamDependenceFlag )
2793 IODGetBytes( &i_data, &p_data, 2 ); /* dependOn_es_id */
2795 if( (i_flags >> 6) & 0x01 )
2796 es_descr->psz_url = IODGetURL( &i_data, &p_data );
2798 bool b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2799 if( b_OCRStreamFlag )
2800 IODGetBytes( &i_data, &p_data, 2 ); /* OCR_es_id */
2802 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x04 )
2804 ts_debug( "\n* ERR missing DecoderConfigDescr" );
2807 int i_config_desc_length = IODDescriptorLength( &i_data, &p_data ); /* DecoderConfigDescr_length */
2808 decoder_config_descriptor_t *dec_descr = &es_descr->dec_descr;
2809 dec_descr->i_objectTypeIndication = IODGetBytes( &i_data, &p_data, 1 );
2810 i_flags = IODGetBytes( &i_data, &p_data, 1 );
2811 dec_descr->i_streamType = i_flags >> 2;
2813 IODGetBytes( &i_data, &p_data, 3); /* bufferSizeDB */
2814 IODGetBytes( &i_data, &p_data, 4); /* maxBitrate */
2815 IODGetBytes( &i_data, &p_data, 4 ); /* avgBitrate */
2817 if( i_config_desc_length > 13 && IODGetBytes( &i_data, &p_data, 1 ) == 0x05 )
2819 dec_descr->i_extra = IODDescriptorLength( &i_data, &p_data );
2820 if( dec_descr->i_extra > 0 )
2822 dec_descr->p_extra = xmalloc( dec_descr->i_extra );
2823 memcpy(dec_descr->p_extra, p_data, dec_descr->i_extra);
2824 p_data += dec_descr->i_extra;
2825 i_data -= dec_descr->i_extra;
2830 dec_descr->i_extra = 0;
2831 dec_descr->p_extra = NULL;
2834 if( IODGetBytes( &i_data, &p_data, 1 ) != 0x06 )
2836 ts_debug( "\n* ERR missing SLConfigDescr" );
2839 IODDescriptorLength( &i_data, &p_data ); /* SLConfigDescr_length */
2840 switch( IODGetBytes( &i_data, &p_data, 1 ) /* predefined */ )
2843 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
2848 es_descr->b_ok = true;
2854 static void IODFree( iod_descriptor_t *p_iod )
2856 if( p_iod->psz_url )
2858 free( p_iod->psz_url );
2863 for( int i = 0; i < 255; i++ )
2865 #define es_descr p_iod->es_descr[i]
2868 if( es_descr.psz_url )
2869 free( es_descr.psz_url );
2871 free( es_descr.dec_descr.p_extra );
2878 /****************************************************************************
2879 ****************************************************************************
2880 ** libdvbpsi callbacks
2881 ****************************************************************************
2882 ****************************************************************************/
2883 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2885 demux_sys_t *p_sys = p_demux->p_sys;
2887 if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
2888 p_sys->i_current_program == 0 )
2890 if( p_sys->i_current_program == i_pgrm )
2893 if( p_sys->programs_list.i_count != 0 )
2895 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
2897 if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
2904 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
2906 demux_sys_t *p_sys = p_demux->p_sys;
2908 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
2911 msg_Warn( p_demux, "Switching to non DVB mode" );
2913 /* This doesn't look like a DVB stream so don't try
2914 * parsing the SDT/EDT/TDT */
2916 for( int i = 0x11; i <= 0x14; i++ )
2918 if( i == 0x13 ) continue;
2919 ts_pid_t *p_pid = &p_sys->pid[i];
2923 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
2924 if( dvbpsi_decoder_present( p_pid->psi->handle ))
2925 dvbpsi_DetachDemux( p_pid->psi->handle );
2926 dvbpsi_delete( p_pid->psi->handle );
2928 dvbpsi_DetachDemux( p_pid->psi->handle );
2932 p_pid->b_valid = false;
2934 SetPIDFilter( p_demux, i, false );
2936 p_sys->b_dvb_meta = false;
2939 #include "dvb-text.h"
2941 static char *EITConvertToUTF8( demux_t *p_demux,
2942 const unsigned char *psz_instring,
2946 demux_sys_t *p_sys = p_demux->p_sys;
2948 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
2950 if ( !p_sys->arib.p_instance )
2951 p_sys->arib.p_instance = arib_instance_new( p_demux );
2952 if ( !p_sys->arib.p_instance )
2954 arib_decoder_t *p_decoder = arib_get_decoder( p_sys->arib.p_instance );
2958 char *psz_outstring = NULL;
2961 i_out = i_length * 4;
2962 psz_outstring = (char*) calloc( i_out + 1, sizeof(char) );
2963 if( !psz_outstring )
2966 arib_initialize_decoder( p_decoder );
2967 i_out = arib_decode_buffer( p_decoder, psz_instring, i_length,
2968 psz_outstring, i_out );
2969 arib_finalize_decoder( p_decoder );
2971 return psz_outstring;
2976 /* Deal with no longer broken providers (no switch byte
2977 but sending ISO_8859-1 instead of ISO_6937) without
2978 removing them from the broken providers table
2979 (keep the entry for correctly handling recorded TS).
2981 b_broken = b_broken && i_length && *psz_instring > 0x20;
2984 return FromCharset( "ISO_8859-1", psz_instring, i_length );
2985 return vlc_from_EIT( psz_instring, i_length );
2988 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2990 demux_sys_t *p_sys = p_demux->p_sys;
2991 ts_pid_t *sdt = &p_sys->pid[0x11];
2992 dvbpsi_sdt_service_t *p_srv;
2994 msg_Dbg( p_demux, "SDTCallBack called" );
2996 if( sdt->psi->i_sdt_version != -1 &&
2997 ( !p_sdt->b_current_next ||
2998 p_sdt->i_version == sdt->psi->i_sdt_version ) )
3000 dvbpsi_DeleteSDT( p_sdt );
3004 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
3006 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3011 p_sdt->i_version, p_sdt->b_current_next,
3012 p_sdt->i_network_id );
3014 p_sys->b_broken_charset = false;
3016 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
3019 dvbpsi_descriptor_t *p_dr;
3021 const char *psz_type = NULL;
3022 const char *psz_status = NULL;
3024 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
3025 "running=%d free_ca=%d",
3026 p_srv->i_service_id, p_srv->b_eit_schedule,
3027 p_srv->b_eit_present, p_srv->i_running_status,
3030 p_meta = vlc_meta_New();
3031 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3033 if( p_dr->i_tag == 0x48 )
3035 static const char *ppsz_type[17] = {
3037 "Digital television service",
3038 "Digital radio sound service",
3040 "NVOD reference service",
3041 "NVOD time-shifted service",
3044 "SECAM coded signal",
3047 "NTSC coded signal",
3048 "Data broadcast service",
3049 "Reserved for Common Interface Usage",
3050 "RCS Map (see EN 301 790 [35])",
3051 "RCS FLS (see EN 301 790 [35])",
3054 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
3058 /* Workarounds for broadcasters with broken EPG */
3060 if( p_sdt->i_network_id == 133 )
3061 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
3063 /* List of providers using ISO8859-1 */
3064 static const char ppsz_broken_providers[][8] = {
3065 "CSAT", /* CanalSat FR */
3066 "GR1", /* France televisions */
3068 "MR5", /* France 2/M6 HD */
3071 for( int i = 0; *ppsz_broken_providers[i]; i++ )
3073 const size_t i_length = strlen(ppsz_broken_providers[i]);
3074 if( pD->i_service_provider_name_length == i_length &&
3075 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
3076 p_sys->b_broken_charset = true;
3079 /* FIXME: Digital+ ES also uses ISO8859-1 */
3081 str1 = EITConvertToUTF8(p_demux,
3082 pD->i_service_provider_name,
3083 pD->i_service_provider_name_length,
3084 p_sys->b_broken_charset );
3085 str2 = EITConvertToUTF8(p_demux,
3087 pD->i_service_name_length,
3088 p_sys->b_broken_charset );
3090 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
3091 pD->i_service_type, str1, str2 );
3093 vlc_meta_SetTitle( p_meta, str2 );
3094 vlc_meta_SetPublisher( p_meta, str1 );
3095 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
3096 psz_type = ppsz_type[pD->i_service_type];
3102 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
3104 static const char *ppsz_status[5] = {
3107 "Starts in a few seconds",
3111 psz_status = ppsz_status[p_srv->i_running_status];
3115 vlc_meta_AddExtra( p_meta, "Type", psz_type );
3117 vlc_meta_AddExtra( p_meta, "Status", psz_status );
3119 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
3120 p_srv->i_service_id, p_meta );
3121 vlc_meta_Delete( p_meta );
3124 sdt->psi->i_sdt_version = p_sdt->i_version;
3125 dvbpsi_DeleteSDT( p_sdt );
3128 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
3129 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
3131 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
3135 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
3136 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
3139 /* Count the number of days */
3140 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
3141 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
3142 for( int i = 70; i < i_year; i++ )
3143 i_day += LEAP(1900+i);
3145 i_day += LEAP(1900+i_year);
3148 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
3151 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
3153 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
3154 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
3155 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
3157 *p_y = 1900 + yp + c*1;
3158 *p_m = mp - 1 - c*12;
3159 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
3161 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
3162 static int64_t EITConvertStartTime( uint64_t i_date )
3164 const int i_mjd = i_date >> 24;
3165 const int i_hour = CVT_FROM_BCD(i_date >> 16);
3166 const int i_minute = CVT_FROM_BCD(i_date >> 8);
3167 const int i_second = CVT_FROM_BCD(i_date );
3172 /* if all 40 bits are 1, the start is unknown */
3173 if( i_date == UINT64_C(0xffffffffff) )
3176 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
3177 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
3179 static int EITConvertDuration( uint32_t i_duration )
3181 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
3182 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
3183 CVT_FROM_BCD(i_duration );
3187 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
3189 demux_sys_t *p_sys = p_demux->p_sys;
3191 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
3193 dvbpsi_DeleteTOT(p_tdt);
3197 static void EITCallBack( demux_t *p_demux,
3198 dvbpsi_eit_t *p_eit, bool b_current_following )
3200 demux_sys_t *p_sys = p_demux->p_sys;
3201 dvbpsi_eit_event_t *p_evt;
3204 msg_Dbg( p_demux, "EITCallBack called" );
3205 if( !p_eit->b_current_next )
3207 dvbpsi_DeleteEIT( p_eit );
3211 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
3212 "ts_id=%d network_id=%d segment_last_section_number=%d "
3214 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3217 p_eit->i_service_id,
3219 p_eit->i_version, p_eit->b_current_next,
3220 p_eit->i_ts_id, p_eit->i_network_id,
3221 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
3223 p_epg = vlc_epg_New( NULL );
3224 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
3226 dvbpsi_descriptor_t *p_dr;
3227 char *psz_name = NULL;
3228 char *psz_text = NULL;
3229 char *psz_extra = strdup("");
3233 int64_t i_tot_time = 0;
3235 i_start = EITConvertStartTime( p_evt->i_start_time );
3236 i_duration = EITConvertDuration( p_evt->i_duration );
3238 if( p_sys->arib.e_mode == ARIBMODE_ENABLED )
3240 if( p_sys->i_tdt_delta == 0 )
3241 p_sys->i_tdt_delta = CLOCK_FREQ * (i_start + i_duration - 5) - mdate();
3243 i_tot_time = (mdate() + p_sys->i_tdt_delta) / CLOCK_FREQ;
3245 tzset(); // JST -> UTC
3246 i_start += timezone; // FIXME: what about DST?
3247 i_tot_time += timezone;
3249 if( p_evt->i_running_status == 0x00 &&
3250 (i_start - 5 < i_tot_time &&
3251 i_tot_time < i_start + i_duration + 5) )
3253 p_evt->i_running_status = 0x04;
3254 msg_Dbg( p_demux, " EIT running status 0x00 -> 0x04" );
3258 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
3259 "running=%d free_ca=%d",
3260 p_evt->i_event_id, (int)i_start, (int)i_duration,
3261 p_evt->i_running_status, p_evt->b_free_ca );
3263 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
3269 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
3271 /* Only take first description, as we don't handle language-info
3273 if( pE && psz_name == NULL )
3275 psz_name = EITConvertToUTF8( p_demux,
3276 pE->i_event_name, pE->i_event_name_length,
3277 p_sys->b_broken_charset );
3279 psz_text = EITConvertToUTF8( p_demux,
3280 pE->i_text, pE->i_text_length,
3281 p_sys->b_broken_charset );
3282 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
3283 pE->i_iso_639_code, psz_name, psz_text );
3290 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
3293 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
3295 pE->i_descriptor_number, pE->i_last_descriptor_number );
3297 if( pE->i_text_length > 0 )
3299 char *psz_text = EITConvertToUTF8( p_demux,
3300 pE->i_text, pE->i_text_length,
3301 p_sys->b_broken_charset );
3304 msg_Dbg( p_demux, " - text='%s'", psz_text );
3306 psz_extra = xrealloc( psz_extra,
3307 strlen(psz_extra) + strlen(psz_text) + 1 );
3308 strcat( psz_extra, psz_text );
3313 for( int i = 0; i < pE->i_entry_count; i++ )
3315 char *psz_dsc = EITConvertToUTF8( p_demux,
3316 pE->i_item_description[i],
3317 pE->i_item_description_length[i],
3318 p_sys->b_broken_charset );
3319 char *psz_itm = EITConvertToUTF8( p_demux,
3320 pE->i_item[i], pE->i_item_length[i],
3321 p_sys->b_broken_charset );
3323 if( psz_dsc && psz_itm )
3325 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
3327 psz_extra = xrealloc( psz_extra,
3328 strlen(psz_extra) + strlen(psz_dsc) +
3329 strlen(psz_itm) + 3 + 1 );
3330 strcat( psz_extra, "(" );
3331 strcat( psz_extra, psz_dsc );
3332 strcat( psz_extra, " " );
3333 strcat( psz_extra, psz_itm );
3334 strcat( psz_extra, ")" );
3346 dvbpsi_parental_rating_dr_t *pR = dvbpsi_DecodeParentalRatingDr( p_dr );
3349 for ( int i = 0; i < pR->i_ratings_number; i++ )
3351 const dvbpsi_parental_rating_t *p_rating = & pR->p_parental_rating[ i ];
3352 if ( p_rating->i_rating > 0x00 && p_rating->i_rating <= 0x0F )
3354 if ( p_rating->i_rating + 3 > i_min_age )
3355 i_min_age = p_rating->i_rating + 3;
3356 msg_Dbg( p_demux, " - parental control set to %d years",
3365 msg_Dbg( p_demux, " - event unknown dr 0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
3371 if( i_start > 0 && psz_name && psz_text)
3372 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
3373 *psz_extra ? psz_extra : NULL, i_min_age );
3375 /* Update "now playing" field */
3376 if( p_evt->i_running_status == 0x04 && i_start > 0 && psz_name && psz_text )
3377 vlc_epg_SetCurrent( p_epg, i_start );
3384 if( p_epg->i_event > 0 )
3386 if( b_current_following &&
3387 ( p_sys->i_current_program == -1 ||
3388 p_sys->i_current_program ==
3389 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3396 p_sys->i_dvb_length = 0;
3397 p_sys->i_dvb_start = 0;
3399 if( p_epg->p_current )
3401 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
3402 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
3405 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG,
3406 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3409 p_eit->i_service_id,
3413 vlc_epg_Delete( p_epg );
3415 dvbpsi_DeleteEIT( p_eit );
3417 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3419 EITCallBack( p_demux, p_eit, true );
3421 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3423 EITCallBack( p_demux, p_eit, false );
3426 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3427 static void PSINewTableCallBack( dvbpsi_t *h, uint8_t i_table_id,
3428 uint16_t i_extension, demux_t *p_demux )
3430 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
3431 uint8_t i_table_id, uint16_t i_extension )
3436 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3437 i_table_id, i_table_id, i_extension, i_extension );
3439 if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
3441 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3442 i_table_id, i_table_id, i_extension, i_extension );
3443 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3444 if( !dvbpsi_sdt_attach( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux ) )
3445 msg_Err( p_demux, "PSINewTableCallback: failed attaching SDTCallback" );
3447 dvbpsi_AttachSDT( h, i_table_id, i_extension,
3448 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
3451 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3452 ( i_table_id == 0x4e || /* Current/Following */
3453 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3455 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3456 i_table_id, i_table_id, i_extension, i_extension );
3458 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3459 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3460 (dvbpsi_eit_callback)EITCallBackSchedule;
3461 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3462 if( !dvbpsi_eit_attach( h, i_table_id, i_extension, cb, p_demux ) )
3463 msg_Err( p_demux, "PSINewTableCallback: failed attaching EITCallback" );
3465 dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
3468 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3469 (i_table_id == 0x70 /* TDT */ || i_table_id == 0x73 /* TOT */) )
3471 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3472 i_table_id, i_table_id, i_extension, i_extension );
3473 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
3474 if( !dvbpsi_tot_attach( h, i_table_id, i_extension, (dvbpsi_tot_callback)TDTCallBack, p_demux ) )
3475 msg_Err( p_demux, "PSINewTableCallback: failed attaching TDTCallback" );
3477 dvbpsi_AttachTOT( h, i_table_id, i_extension,
3478 (dvbpsi_tot_callback)TDTCallBack, p_demux);
3483 /*****************************************************************************
3484 * PMT callback and helpers
3485 *****************************************************************************/
3486 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3489 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3490 while( p_dr && ( p_dr->i_tag != i_tag ) )
3491 p_dr = p_dr->p_next;
3494 static bool PMTEsHasRegistration( demux_t *p_demux,
3495 const dvbpsi_pmt_es_t *p_es,
3496 const char *psz_tag )
3498 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3502 if( p_dr->i_length < 4 )
3504 msg_Warn( p_demux, "invalid Registration Descriptor" );
3508 assert( strlen(psz_tag) == 4 );
3509 return !memcmp( p_dr->p_data, psz_tag, 4 );
3512 static bool PMTEsHasComponentTag( const dvbpsi_pmt_es_t *p_es,
3513 int i_component_tag )
3515 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3518 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3522 return p_si->i_component_tag == i_component_tag;
3525 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3526 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3528 es_format_t *p_fmt = &pid->es->fmt;
3530 /* MPEG-4 stream: search FMC_DESCRIPTOR (SL Packetized stream) */
3531 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3533 if( p_dr && p_dr->i_length == 2 )
3535 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3537 msg_Dbg( p_demux, "found FMC_descriptor declaring sl packetization on es_id=%d", i_es_id );
3539 pid->es->p_mpeg4desc = NULL;
3541 for( int i = 0; i < ES_DESCRIPTOR_COUNT; i++ )
3543 iod_descriptor_t *iod = prg->iod;
3544 if( iod->es_descr[i].i_es_id == i_es_id )
3546 if ( iod->es_descr[i].b_ok )
3547 pid->es->p_mpeg4desc = &iod->es_descr[i];
3549 msg_Dbg( p_demux, "MPEG-4 descriptor not yet available on es_id=%d", i_es_id );
3554 if( !pid->es->p_mpeg4desc )
3556 switch( p_es->i_type )
3558 /* non fatal, set by packetizer */
3559 case 0x0f: /* ADTS */
3560 case 0x11: /* LOAS */
3561 msg_Info( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
3562 pid->i_pid, p_es->i_type );
3565 msg_Err( p_demux, "MPEG-4 descriptor not found for pid 0x%x type 0x%x",
3566 pid->i_pid, p_es->i_type );
3572 const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
3573 if( dcd->i_streamType == 0x04 ) /* VisualStream */
3575 p_fmt->i_cat = VIDEO_ES;
3576 switch( dcd->i_objectTypeIndication )
3578 case 0x0B: /* mpeg4 sub */
3579 p_fmt->i_cat = SPU_ES;
3580 p_fmt->i_codec = VLC_CODEC_SUBT;
3583 case 0x20: /* mpeg4 */
3584 p_fmt->i_codec = VLC_CODEC_MP4V;
3586 case 0x21: /* h264 */
3587 p_fmt->i_codec = VLC_CODEC_H264;
3594 case 0x65: /* mpeg2 */
3595 p_fmt->i_codec = VLC_CODEC_MPGV;
3597 case 0x6a: /* mpeg1 */
3598 p_fmt->i_codec = VLC_CODEC_MPGV;
3600 case 0x6c: /* mpeg1 */
3601 p_fmt->i_codec = VLC_CODEC_JPEG;
3604 p_fmt->i_cat = UNKNOWN_ES;
3608 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
3610 p_fmt->i_cat = AUDIO_ES;
3611 switch( dcd->i_objectTypeIndication )
3613 case 0x40: /* mpeg4 */
3614 p_fmt->i_codec = VLC_CODEC_MP4A;
3618 case 0x68: /* mpeg2 aac */
3619 p_fmt->i_codec = VLC_CODEC_MP4A;
3621 case 0x69: /* mpeg2 */
3622 p_fmt->i_codec = VLC_CODEC_MPGA;
3624 case 0x6b: /* mpeg1 */
3625 p_fmt->i_codec = VLC_CODEC_MPGA;
3628 p_fmt->i_cat = UNKNOWN_ES;
3634 p_fmt->i_cat = UNKNOWN_ES;
3637 if( p_fmt->i_cat != UNKNOWN_ES )
3639 p_fmt->i_extra = dcd->i_extra;
3640 if( p_fmt->i_extra > 0 )
3642 p_fmt->p_extra = malloc( p_fmt->i_extra );
3643 if( p_fmt->p_extra )
3644 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
3657 } ts_teletext_page_t;
3659 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
3660 const dvbpsi_pmt_es_t *p_es )
3662 es_format_t *p_fmt = &pid->es->fmt;
3664 ts_teletext_page_t p_page[2 * 64 + 20];
3665 unsigned i_page = 0;
3667 /* Gather pages information */
3668 #if defined _DVBPSI_DR_56_H_ && \
3669 defined DVBPSI_VERSION && DVBPSI_VERSION_INT > DVBPSI_VERSION_WANTED(0,1,5)
3670 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
3672 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
3676 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
3680 for( int i = 0; i < p_sub->i_pages_number; i++ )
3682 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
3684 if( p_src->i_teletext_type >= 0x06 )
3687 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3689 ts_teletext_page_t *p_dst = &p_page[i_page++];
3691 p_dst->i_type = p_src->i_teletext_type;
3692 p_dst->i_magazine = p_src->i_teletext_magazine_number
3693 ? p_src->i_teletext_magazine_number : 8;
3694 p_dst->i_page = p_src->i_teletext_page_number;
3695 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3700 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3703 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3704 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3706 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
3708 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
3711 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3713 ts_teletext_page_t *p_dst = &p_page[i_page++];
3715 switch( p_src->i_subtitling_type )
3718 p_dst->i_type = 0x02;
3721 p_dst->i_type = 0x03;
3724 /* FIXME check if it is the right split */
3725 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
3726 ? (p_src->i_composition_page_id >> 8) : 8;
3727 p_dst->i_page = p_src->i_composition_page_id & 0xff;
3728 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3733 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
3735 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3737 p_fmt->subs.teletext.i_magazine = -1;
3738 p_fmt->subs.teletext.i_page = 0;
3739 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
3741 dvbpsi_descriptor_t *p_dr;
3742 p_dr = PMTEsFindDescriptor( p_es, 0x46 );
3744 p_dr = PMTEsFindDescriptor( p_es, 0x56 );
3746 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3748 /* Descriptor pass-through */
3749 p_fmt->p_extra = malloc( p_dr->i_length );
3750 if( p_fmt->p_extra )
3752 p_fmt->i_extra = p_dr->i_length;
3753 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3759 for( unsigned i = 0; i < i_page; i++ )
3770 p_es = malloc( sizeof(*p_es) );
3774 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3775 free( p_es->fmt.psz_language );
3776 free( p_es->fmt.psz_description );
3777 p_es->fmt.psz_language = NULL;
3778 p_es->fmt.psz_description = NULL;
3781 p_es->p_data = NULL;
3782 p_es->i_data_size = 0;
3783 p_es->i_data_gathered = 0;
3784 p_es->pp_last = &p_es->p_data;
3785 p_es->data_type = TS_ES_DATA_PES;
3786 p_es->p_mpeg4desc = NULL;
3788 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3792 const ts_teletext_page_t *p = &p_page[i];
3793 p_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ?
3794 ES_PRIORITY_SELECTABLE_MIN : ES_PRIORITY_NOT_DEFAULTABLE;
3795 p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
3796 p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
3797 p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
3798 p_es->fmt.subs.teletext.i_page = p->i_page;
3801 " * ttxt type=%s lan=%s page=%d%02x",
3802 p_es->fmt.psz_description,
3803 p_es->fmt.psz_language,
3804 p->i_magazine, p->i_page );
3808 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
3809 const dvbpsi_pmt_es_t *p_es )
3811 es_format_t *p_fmt = &pid->es->fmt;
3813 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
3815 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3817 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3818 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3820 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
3821 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
3822 ( i_type >= 0x20 && i_type <= 0x24 ) )
3826 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3828 p_fmt->subs.dvb.i_id = -1;
3829 p_fmt->psz_description = strdup( _("DVB subtitles") );
3831 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3833 /* Descriptor pass-through */
3834 p_fmt->p_extra = malloc( p_dr->i_length );
3835 if( p_fmt->p_extra )
3837 p_fmt->i_extra = p_dr->i_length;
3838 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3844 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3855 p_es = malloc( sizeof(*p_es) );
3859 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3860 free( p_es->fmt.psz_language );
3861 free( p_es->fmt.psz_description );
3862 p_es->fmt.psz_language = NULL;
3863 p_es->fmt.psz_description = NULL;
3866 p_es->p_data = NULL;
3867 p_es->i_data_size = 0;
3868 p_es->i_data_gathered = 0;
3869 p_es->pp_last = &p_es->p_data;
3870 p_es->data_type = TS_ES_DATA_PES;
3871 p_es->p_mpeg4desc = NULL;
3873 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3877 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
3878 p_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
3879 switch( p->i_subtitling_type )
3881 case 0x10: /* unspec. */
3882 case 0x11: /* 4:3 */
3883 case 0x12: /* 16:9 */
3884 case 0x13: /* 2.21:1 */
3885 case 0x14: /* HD monitor */
3886 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
3888 case 0x20: /* Hearing impaired unspec. */
3889 case 0x21: /* h.i. 4:3 */
3890 case 0x22: /* h.i. 16:9 */
3891 case 0x23: /* h.i. 2.21:1 */
3892 case 0x24: /* h.i. HD monitor */
3893 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
3900 p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
3901 ( p->i_ancillary_page_id << 16 );
3906 static int vlc_ceil_log2( const unsigned int val )
3908 int n = 31 - clz(val);
3909 if ((1U << n) != val)
3915 static void OpusSetup(demux_t *demux, uint8_t *p, size_t len, es_format_t *p_fmt)
3919 /* default mapping */
3920 static const unsigned char map[8] = { 0, 1, 2, 3, 4, 5, 6, 7 };
3921 memcpy(h.stream_map, map, sizeof(map));
3925 int stream_count = 0;
3926 int ccc = p[1]; // channel_config_code
3930 mapping = channels > 2;
3933 channels = 2; // dual mono
3935 static const uint8_t p_csc[8] = { 0, 1, 1, 2, 2, 2, 3, 3 };
3936 csc = p_csc[channels - 1];
3937 stream_count = channels - csc;
3939 static const uint8_t map[6][7] = {
3948 memcpy(&h.stream_map[1], map[channels-3], channels - 1);
3949 } else if (ccc == 0x81) {
3951 goto explicit_config_too_short;
3958 bs_init(&s, &p[4], len - 4);
3961 int bits = vlc_ceil_log2(channels);
3962 if (s.i_left < bits)
3963 goto explicit_config_too_short;
3964 stream_count = bs_read(&s, bits) + 1;
3965 bits = vlc_ceil_log2(stream_count + 1);
3966 if (s.i_left < bits)
3967 goto explicit_config_too_short;
3968 csc = bs_read(&s, bits);
3970 int channel_bits = vlc_ceil_log2(stream_count + csc + 1);
3971 if (s.i_left < channels * channel_bits)
3972 goto explicit_config_too_short;
3974 unsigned char silence = (1U << (stream_count + csc + 1)) - 1;
3975 for (int i = 0; i < channels; i++) {
3976 unsigned char m = bs_read(&s, channel_bits);
3979 h.stream_map[i] = m;
3982 } else if (ccc >= 0x80 && ccc <= 0x88) {
3983 channels = ccc - 0x80;
3988 channels = 2; // dual mono
3991 stream_count = channels;
3993 msg_Err(demux, "Opus channel configuration 0x%.2x is reserved", ccc);
3997 msg_Err(demux, "Opus channel configuration 0x%.2x not supported yet", p[1]);
4001 opus_prepare_header(channels, 0, &h);
4003 h.input_sample_rate = 48000;
4005 h.nb_streams = channels - csc;
4006 h.channel_mapping = mapping;
4009 opus_write_header((uint8_t**)&p_fmt->p_extra, &p_fmt->i_extra, &h, NULL /* FIXME */);
4010 if (p_fmt->p_extra) {
4011 p_fmt->i_cat = AUDIO_ES;
4012 p_fmt->i_codec = VLC_CODEC_OPUS;
4013 p_fmt->audio.i_channels = h.channels;
4014 p_fmt->audio.i_rate = 48000;
4020 explicit_config_too_short:
4021 msg_Err(demux, "Opus descriptor too short");
4024 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
4025 const dvbpsi_pmt_es_t *p_es )
4027 es_format_t *p_fmt = &pid->es->fmt;
4028 dvbpsi_descriptor_t *p_subs_dr = PMTEsFindDescriptor( p_es, 0x59 );
4029 dvbpsi_descriptor_t *desc;
4031 if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
4032 PMTEsFindDescriptor( p_es, 0x6a ) ||
4033 PMTEsFindDescriptor( p_es, 0x81 ) )
4035 p_fmt->i_cat = AUDIO_ES;
4036 p_fmt->i_codec = VLC_CODEC_A52;
4038 else if( (desc = PMTEsFindDescriptor( p_es, 0x7f ) ) && desc->i_length >= 2 &&
4039 PMTEsHasRegistration(p_demux, p_es, "Opus"))
4041 OpusSetup(p_demux, desc->p_data, desc->i_length, p_fmt);
4043 else if( PMTEsFindDescriptor( p_es, 0x7a ) )
4045 /* DVB with stream_type 0x06 (ETS EN 300 468) */
4046 p_fmt->i_cat = AUDIO_ES;
4047 p_fmt->i_codec = VLC_CODEC_EAC3;
4049 else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
4050 PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
4051 PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
4052 PMTEsFindDescriptor( p_es, 0x73 ) )
4054 /*registration descriptor(ETSI TS 101 154 Annex F)*/
4055 p_fmt->i_cat = AUDIO_ES;
4056 p_fmt->i_codec = VLC_CODEC_DTS;
4058 else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) && !p_subs_dr )
4060 /* BSSD is AES3 DATA, but could also be subtitles
4061 * we need to check for secondary descriptor then s*/
4062 p_fmt->i_cat = AUDIO_ES;
4063 p_fmt->b_packetized = true;
4064 p_fmt->i_codec = VLC_CODEC_302M;
4066 else if( PMTEsHasRegistration( p_demux, p_es, "HEVC" ) )
4068 p_fmt->i_cat = VIDEO_ES;
4069 p_fmt->i_codec = VLC_CODEC_HEVC;
4071 else if ( p_demux->p_sys->arib.e_mode == ARIBMODE_ENABLED )
4073 /* Lookup our data component descriptor first ARIB STD B10 6.4 */
4074 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xFD );
4075 /* and check that it maps to something ARIB STD B14 Table 5.1/5.2 */
4076 if ( p_dr && p_dr->i_length >= 2 )
4078 if( !memcmp( p_dr->p_data, "\x00\x08", 2 ) && (
4079 PMTEsHasComponentTag( p_es, 0x30 ) ||
4080 PMTEsHasComponentTag( p_es, 0x31 ) ||
4081 PMTEsHasComponentTag( p_es, 0x32 ) ||
4082 PMTEsHasComponentTag( p_es, 0x33 ) ||
4083 PMTEsHasComponentTag( p_es, 0x34 ) ||
4084 PMTEsHasComponentTag( p_es, 0x35 ) ||
4085 PMTEsHasComponentTag( p_es, 0x36 ) ||
4086 PMTEsHasComponentTag( p_es, 0x37 ) ) )
4088 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_A );
4089 p_fmt->psz_language = strndup ( "jpn", 3 );
4090 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4092 else if( !memcmp( p_dr->p_data, "\x00\x12", 2 ) && (
4093 PMTEsHasComponentTag( p_es, 0x87 ) ||
4094 PMTEsHasComponentTag( p_es, 0x88 ) ) )
4096 es_format_Init( &pid->es->fmt, SPU_ES, VLC_CODEC_ARIB_C );
4097 p_fmt->psz_language = strndup ( "jpn", 3 );
4098 p_fmt->psz_description = strdup( _("ARIB subtitles") );
4104 /* Subtitle/Teletext/VBI fallbacks */
4105 dvbpsi_subtitling_dr_t *p_sub;
4106 if( p_subs_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_subs_dr ) ) )
4108 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
4110 if( p_fmt->i_cat != UNKNOWN_ES )
4113 switch( p_sub->p_subtitle[i].i_subtitling_type )
4115 case 0x01: /* EBU Teletext subtitles */
4116 case 0x02: /* Associated EBU Teletext */
4117 case 0x03: /* VBI data */
4118 PMTSetupEsTeletext( p_demux, pid, p_es );
4120 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
4121 case 0x11: /* ... on 4:3 AR monitor */
4122 case 0x12: /* ... on 16:9 AR monitor */
4123 case 0x13: /* ... on 2.21:1 AR monitor */
4124 case 0x14: /* ... for display on a high definition monitor */
4125 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
4126 case 0x21: /* ... on 4:3 AR monitor */
4127 case 0x22: /* ... on 16:9 AR monitor */
4128 case 0x23: /* ... on 2.21:1 AR monitor */
4129 case 0x24: /* ... for display on a high definition monitor */
4130 PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
4133 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
4134 p_sub->p_subtitle[i].i_subtitling_type );
4140 if( p_fmt->i_cat == UNKNOWN_ES &&
4141 ( PMTEsFindDescriptor( p_es, 0x45 ) || /* VBI Data descriptor */
4142 PMTEsFindDescriptor( p_es, 0x46 ) || /* VBI Teletext descriptor */
4143 PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
4146 PMTSetupEsTeletext( p_demux, pid, p_es );
4150 /* FIXME is it useful ? */
4151 if( PMTEsFindDescriptor( p_es, 0x52 ) )
4153 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
4154 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
4156 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
4160 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
4161 const dvbpsi_pmt_es_t *p_es )
4163 /* Registration Descriptor */
4164 if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
4166 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4170 es_format_t *p_fmt = &pid->es->fmt;
4172 /* registration descriptor for VC-1 (SMPTE rp227) */
4173 p_fmt->i_cat = VIDEO_ES;
4174 p_fmt->i_codec = VLC_CODEC_VC1;
4176 /* XXX With Simple and Main profile the SEQUENCE
4177 * header is modified: video width and height are
4178 * inserted just after the start code as 2 int16_t
4179 * The packetizer will take care of that. */
4182 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
4183 const dvbpsi_pmt_es_t *p_es )
4185 /* Registration Descriptor */
4186 if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
4188 msg_Err( p_demux, "Registration descriptor not found or invalid" );
4192 es_format_t *p_fmt = &pid->es->fmt;
4194 /* registration descriptor for Dirac
4195 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
4196 p_fmt->i_cat = VIDEO_ES;
4197 p_fmt->i_codec = VLC_CODEC_DIRAC;
4200 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
4201 const dvbpsi_pmt_es_t *p_es )
4203 /* MSCODEC sent by vlc */
4204 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
4205 if( !p_dr || p_dr->i_length < 10 )
4208 "private MSCODEC (vlc) without bih private descriptor" );
4212 es_format_t *p_fmt = &pid->es->fmt;
4213 p_fmt->i_cat = VIDEO_ES;
4214 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
4215 p_dr->p_data[2], p_dr->p_data[3] );
4216 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
4217 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
4218 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
4220 if( p_fmt->i_extra > 0 )
4222 p_fmt->p_extra = malloc( p_fmt->i_extra );
4223 if( p_fmt->p_extra )
4224 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
4225 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
4229 /* For such stream we will gather them ourself and don't launch a
4231 * Yes it's ugly but it's the only way to have DIV3 working */
4232 p_fmt->b_packetized = true;
4235 static void PMTSetupEs0x83( const dvbpsi_pmt_t *p_pmt, ts_pid_t *pid )
4237 /* WiDi broadcasts without registration on PMT 0x1, PCR 0x1000 and
4238 * with audio track pid being 0x1100..0x11FF */
4239 if ( p_pmt->i_program_number == 0x1 &&
4240 p_pmt->i_pcr_pid == 0x1000 &&
4241 ( pid->i_pid >> 8 ) == 0x11 )
4243 /* Not enough ? might contain 0x83 private descriptor, 2 bytes 0x473F */
4244 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_WIDI_LPCM );
4247 es_format_Init( &pid->es->fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
4250 static bool PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
4251 const dvbpsi_pmt_es_t *p_es )
4253 es_format_t *p_fmt = &pid->es->fmt;
4255 /* Blu-Ray mapping */
4256 switch( p_es->i_type )
4259 p_fmt->i_cat = AUDIO_ES;
4260 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
4263 case 0x85: /* DTS-HD High resolution audio */
4264 case 0x86: /* DTS-HD Master audio */
4265 case 0xA2: /* Secondary DTS audio */
4266 p_fmt->i_cat = AUDIO_ES;
4267 p_fmt->i_codec = VLC_CODEC_DTS;
4270 case 0x83: /* TrueHD AC3 */
4271 p_fmt->i_cat = AUDIO_ES;
4272 p_fmt->i_codec = VLC_CODEC_TRUEHD;
4275 case 0x84: /* E-AC3 */
4276 case 0xA1: /* Secondary E-AC3 */
4277 p_fmt->i_cat = AUDIO_ES;
4278 p_fmt->i_codec = VLC_CODEC_EAC3;
4280 case 0x90: /* Presentation graphics */
4281 p_fmt->i_cat = SPU_ES;
4282 p_fmt->i_codec = VLC_CODEC_BD_PG;
4284 case 0x91: /* Interactive graphics */
4285 case 0x92: /* Subtitle */
4288 msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
4289 p_es->i_pid, p_es->i_type );
4296 static bool PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
4297 const dvbpsi_pmt_es_t *p_es )
4303 vlc_fourcc_t i_codec;
4305 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
4306 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
4307 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
4308 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
4309 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
4310 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
4311 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
4312 { "", UNKNOWN_ES, 0 }
4314 es_format_t *p_fmt = &pid->es->fmt;
4316 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
4318 if( PMTEsHasRegistration( p_demux, p_es, p_regs[i].psz_tag ) )
4320 p_fmt->i_cat = p_regs[i].i_cat;
4321 p_fmt->i_codec = p_regs[i].i_codec;
4322 if (p_es->i_type == 0x87)
4323 p_fmt->i_codec = VLC_CODEC_EAC3;
4330 static char *GetAudioTypeDesc(demux_t *p_demux, int type)
4332 static const char *audio_type[] = {
4334 N_("clean effects"),
4335 N_("hearing impaired"),
4336 N_("visual impaired commentary"),
4339 if (type < 0 || type > 3)
4340 msg_Dbg( p_demux, "unknown audio type: %d", type);
4342 return strdup(audio_type[type]);
4346 static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
4347 const dvbpsi_pmt_es_t *p_es )
4349 /* get language descriptor */
4350 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
4355 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
4358 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
4362 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
4363 pid->es->fmt.psz_language = malloc( 4 );
4364 if( pid->es->fmt.psz_language )
4366 memcpy( pid->es->fmt.psz_language, p_decoded->code[0].iso_639_code, 3 );
4367 pid->es->fmt.psz_language[3] = 0;
4368 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
4370 int type = p_decoded->code[0].i_audio_type;
4371 pid->es->fmt.psz_description = GetAudioTypeDesc(p_demux, type);
4373 pid->es->fmt.i_priority = ES_PRIORITY_SELECTABLE_MIN + 1; // prioritize normal audio tracks
4375 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
4376 if( pid->es->fmt.i_extra_languages > 0 )
4377 pid->es->fmt.p_extra_languages =
4378 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
4379 pid->es->fmt.i_extra_languages );
4380 if( pid->es->fmt.p_extra_languages )
4382 for( int i = 0; i < pid->es->fmt.i_extra_languages; i++ )
4384 pid->es->fmt.p_extra_languages[i].psz_language = malloc(4);
4385 if( pid->es->fmt.p_extra_languages[i].psz_language )
4387 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
4388 p_decoded->code[i+1].iso_639_code, 3 );
4389 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
4391 int type = p_decoded->code[i].i_audio_type;
4392 pid->es->fmt.p_extra_languages[i].psz_description = GetAudioTypeDesc(p_demux, type);
4396 pid->es->fmt.psz_language = malloc( 4 );
4397 if( pid->es->fmt.psz_language )
4399 memcpy( pid->es->fmt.psz_language,
4400 p_decoded->i_iso_639_code, 3 );
4401 pid->es->fmt.psz_language[3] = 0;
4406 static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
4408 demux_t *p_demux = data;
4409 demux_sys_t *p_sys = p_demux->p_sys;
4411 ts_pid_t *pmt = NULL;
4414 msg_Dbg( p_demux, "PMTCallBack called" );
4416 /* First find this PMT declared in PAT */
4417 for( int i = 0; !pmt && i < p_sys->i_pmt; i++ )
4418 for( int i_prg = 0; !pmt && i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
4420 const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
4421 if( i_pmt_number != TS_USER_PMT_NUMBER &&
4422 i_pmt_number == p_pmt->i_program_number )
4424 pmt = p_sys->pmt[i];
4425 prg = p_sys->pmt[i]->psi->prg[i_prg];
4431 msg_Warn( p_demux, "unreferenced program (broken stream)" );
4432 dvbpsi_DeletePMT(p_pmt);
4437 if( prg->i_version != -1 &&
4438 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
4440 dvbpsi_DeletePMT( p_pmt );
4444 ts_pid_t **pp_clean = NULL;
4446 /* Clean this program (remove all es) */
4447 for( int i = 0; i < 8192; i++ )
4449 ts_pid_t *pid = &p_sys->pid[i];
4451 if( pid->b_valid && pid->p_owner == pmt->psi &&
4452 pid->i_owner_number == prg->i_number && pid->psi == NULL )
4454 TAB_APPEND( i_clean, pp_clean, pid );
4459 IODFree( prg->iod );
4463 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
4464 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
4465 prg->i_pid_pcr = p_pmt->i_pcr_pid;
4466 prg->i_version = p_pmt->i_version;
4468 ValidateDVBMeta( p_demux, prg->i_pid_pcr );
4469 if( ProgramIsSelected( p_demux, prg->i_number ) )
4470 SetPIDFilter( p_demux, prg->i_pid_pcr, true ); /* Set demux filter */
4472 /* Parse PMT descriptors */
4473 ts_pmt_registration_type_t registration_type = TS_PMT_REGISTRATION_NONE;
4474 dvbpsi_descriptor_t *p_dr;
4476 /* First pass for standard detection */
4477 if ( p_sys->arib.e_mode == ARIBMODE_AUTO )
4479 int i_arib_flags = 0; /* Descriptors can be repeated */
4480 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
4486 dvbpsi_ca_dr_t *p_cadr = dvbpsi_DecodeCADr( p_dr );
4487 i_arib_flags |= (p_cadr->i_ca_system_id == 0x05);
4491 i_arib_flags |= 1 << 1;
4494 i_arib_flags |= 1 << 2;
4500 if ( i_arib_flags == 0b111 )
4501 p_sys->arib.e_mode = ARIBMODE_ENABLED;
4504 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
4506 /* special descriptors handling */
4509 case 0x1d: /* We have found an IOD descriptor */
4510 msg_Dbg( p_demux, " * PMT descriptor : IOD (0x1d)" );
4511 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
4515 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
4516 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
4519 case 0x5: /* Registration Descriptor */
4520 if( p_dr->i_length != 4 )
4522 msg_Warn( p_demux, " * PMT invalid Registration Descriptor" );
4526 msg_Dbg( p_demux, " * PMT descriptor : registration %4.4s", p_dr->p_data );
4527 if( !memcmp( p_dr->p_data, "HDMV", 4 ) || !memcmp( p_dr->p_data, "HDPR", 4 ) )
4528 registration_type = TS_PMT_REGISTRATION_HDMV; /* Blu-Ray */
4533 msg_Dbg( p_demux, " * PMT descriptor : Private Data (0x0f)" );
4537 msg_Dbg( p_demux, " * PMT descriptor : Digital copy control (0xC1)" );
4540 case 0x88: /* EACEM Simulcast HD Logical channels ordering */
4541 msg_Dbg( p_demux, " * descriptor : EACEM Simulcast HD" );
4542 /* TODO: apply visibility flags */
4546 msg_Dbg( p_demux, " * PMT descriptor : unknown (0x%x)", p_dr->i_tag );
4549 dvbpsi_pmt_es_t *p_es;
4550 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
4552 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
4554 /* Find out if the PID was already declared */
4555 for( int i = 0; i < i_clean; i++ )
4557 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
4559 old_pid = pp_clean[i];
4563 ValidateDVBMeta( p_demux, p_es->i_pid );
4565 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
4567 msg_Warn( p_demux, " * PMT error: pid=%d already defined",
4572 char const * psz_typedesc = "";
4573 switch(p_es->i_type)
4576 psz_typedesc = "ISO/IEC Reserved";
4579 psz_typedesc = "ISO/IEC 11172 Video";
4582 psz_typedesc = "ISO/IEC 13818-2 Video or ISO/IEC 11172-2 constrained parameter video stream";
4585 psz_typedesc = "ISO/IEC 11172 Audio";
4588 psz_typedesc = "ISO/IEC 13818-3 Audio";
4591 psz_typedesc = "ISO/IEC 13818-1 private_sections";
4594 psz_typedesc = "ISO/IEC 13818-1 PES packets containing private data";
4597 psz_typedesc = "ISO/IEC 13522 MHEG";
4600 psz_typedesc = "ISO/IEC 13818-1 Annex A DSM CC";
4603 psz_typedesc = "ITU-T Rec. H.222.1";
4606 psz_typedesc = "ISO/IEC 13818-6 type A";
4609 psz_typedesc = "ISO/IEC 13818-6 type B";
4612 psz_typedesc = "ISO/IEC 13818-6 type C";
4615 psz_typedesc = "ISO/IEC 13818-6 type D";
4618 psz_typedesc = "ISO/IEC 13818-1 auxiliary";
4621 if (p_es->i_type >= 0x0F && p_es->i_type <=0x7F)
4622 psz_typedesc = "ISO/IEC 13818-1 Reserved";
4624 psz_typedesc = "User Private";
4627 msg_Dbg( p_demux, " * pid=%d type=0x%x %s",
4628 p_es->i_pid, p_es->i_type, psz_typedesc );
4630 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
4631 p_dr = p_dr->p_next )
4633 msg_Dbg( p_demux, " - descriptor tag 0x%x",
4637 PIDInit( pid, false, pmt->psi );
4638 PIDFillFormat( &pid->es->fmt, p_es->i_type );
4639 pid->i_owner_number = prg->i_number;
4640 pid->i_pid = p_es->i_pid;
4641 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
4644 bool b_registration_applied = false;
4645 if ( p_es->i_type >= 0x80 ) /* non standard, extensions */
4647 if ( registration_type == TS_PMT_REGISTRATION_HDMV )
4649 if (( b_registration_applied = PMTSetupEsHDMV( p_demux, pid, p_es ) ))
4650 msg_Dbg( p_demux, " + HDMV registration applied to pid %d type 0x%x",
4651 p_es->i_pid, p_es->i_type );
4655 if (( b_registration_applied = PMTSetupEsRegistration( p_demux, pid, p_es ) ))
4656 msg_Dbg( p_demux, " + registration applied to pid %d type 0x%x",
4657 p_es->i_pid, p_es->i_type );
4661 if ( !b_registration_applied )
4663 switch( p_es->i_type )
4666 /* Handle PES private data */
4667 PMTSetupEs0x06( p_demux, pid, p_es );
4669 /* All other private or reserved types */
4674 PMTSetupEsISO14496( p_demux, pid, prg, p_es );
4678 PMTSetupEs0x83( p_pmt, pid );
4681 PMTSetupEs0xA0( p_demux, pid, p_es );
4684 PMTSetupEs0xD1( p_demux, pid, p_es );
4687 PMTSetupEs0xEA( p_demux, pid, p_es );
4693 if( pid->es->fmt.i_cat == AUDIO_ES ||
4694 ( pid->es->fmt.i_cat == SPU_ES &&
4695 pid->es->fmt.i_codec != VLC_CODEC_DVBS &&
4696 pid->es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
4698 PMTParseEsIso639( p_demux, pid, p_es );
4701 switch( pid->es->fmt.i_codec )
4703 case VLC_CODEC_SCTE_27:
4704 pid->es->data_type = TS_ES_DATA_TABLE_SECTION;
4707 //pid->es->data_type = TS_ES_DATA_PES;
4711 pid->es->fmt.i_group = p_pmt->i_program_number;
4712 for( int i = 0; i < pid->i_extra_es; i++ )
4713 pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
4715 if( pid->es->fmt.i_cat == UNKNOWN_ES )
4717 msg_Dbg( p_demux, " => pid %d content is *unknown*",
4722 msg_Dbg( p_demux, " => pid %d has now es fcc=%4.4s",
4723 p_es->i_pid, (char*)&pid->es->fmt.i_codec );
4725 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
4727 /* Check if we can avoid restarting the ES */
4729 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
4730 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
4731 pid->es->fmt.i_extra == 0 &&
4732 pid->i_extra_es == old_pid->i_extra_es &&
4733 ( ( !pid->es->fmt.psz_language &&
4734 !old_pid->es->fmt.psz_language ) ||
4735 ( pid->es->fmt.psz_language &&
4736 old_pid->es->fmt.psz_language &&
4737 !strcmp( pid->es->fmt.psz_language,
4738 old_pid->es->fmt.psz_language ) ) ) )
4740 pid->i_cc = old_pid->i_cc;
4741 ts_es_t *e = pid->es;
4742 pid->es = old_pid->es;
4744 for( int i = 0; i < pid->i_extra_es; i++ )
4746 e = pid->extra_es[i];
4747 pid->extra_es[i] = old_pid->extra_es[i];
4748 old_pid->extra_es[i] = e;
4753 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4754 for( int i = 0; i < pid->i_extra_es; i++ )
4756 pid->extra_es[i]->id =
4757 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
4759 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4763 /* Add ES to the list */
4766 PIDClean( p_demux, old_pid );
4767 TAB_REMOVE( i_clean, pp_clean, old_pid );
4769 p_sys->pid[p_es->i_pid] = *pid;
4771 p_dr = PMTEsFindDescriptor( p_es, 0x09 );
4772 if( p_dr && p_dr->i_length >= 2 )
4774 msg_Dbg( p_demux, " * PMT descriptor : CA (0x9) SysID 0x%x",
4775 (p_dr->p_data[0] << 8) | p_dr->p_data[1] );
4778 if( ProgramIsSelected( p_demux, prg->i_number ) && pid->es->id != NULL )
4779 SetPIDFilter( p_demux, p_es->i_pid, true ); /* Set demux filter */
4782 /* Set CAM descrambling */
4783 if( !ProgramIsSelected( p_demux, prg->i_number ) )
4785 dvbpsi_DeletePMT( p_pmt );
4787 else if( stream_Control( p_sys->stream, STREAM_SET_PRIVATE_ID_CA,
4788 p_pmt ) != VLC_SUCCESS )
4790 if ( p_sys->arib.e_mode == ARIBMODE_ENABLED )
4792 p_sys->arib.b25stream = stream_FilterNew( p_demux->s, "aribcam" );
4793 p_sys->stream = ( p_sys->arib.b25stream ) ? p_sys->arib.b25stream : p_demux->s;
4794 if (!p_sys->arib.b25stream)
4795 dvbpsi_DeletePMT( p_pmt );
4796 } else dvbpsi_DeletePMT( p_pmt );
4799 for( int i = 0; i < i_clean; i++ )
4801 if( ProgramIsSelected( p_demux, prg->i_number ) )
4802 SetPIDFilter( p_demux, pp_clean[i]->i_pid, false );
4804 PIDClean( p_demux, pp_clean[i] );
4810 static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
4812 demux_t *p_demux = data;
4813 demux_sys_t *p_sys = p_demux->p_sys;
4814 dvbpsi_pat_program_t *p_program;
4815 ts_pid_t *pat = &p_sys->pid[0];
4817 msg_Dbg( p_demux, "PATCallBack called" );
4819 if( ( pat->psi->i_pat_version != -1 &&
4820 ( !p_pat->b_current_next ||
4821 p_pat->i_version == pat->psi->i_pat_version ) ) ||
4824 dvbpsi_DeletePAT( p_pat );
4828 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
4829 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
4832 if( p_sys->i_pmt > 0 )
4835 ts_pid_t **pmt_rm = NULL;
4837 /* Search pmt to be deleted */
4838 for( int i = 0; i < p_sys->i_pmt; i++ )
4840 ts_pid_t *pmt = p_sys->pmt[i];
4841 bool b_keep = false;
4843 for( p_program = p_pat->p_first_program; !b_keep && p_program;
4844 p_program = p_program->p_next )
4846 if( p_program->i_pid != pmt->i_pid )
4849 for( int i_prg = 0; !b_keep && i_prg < pmt->psi->i_prg; i_prg++ )
4850 if( p_program->i_number == pmt->psi->prg[i_prg]->i_number )
4857 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
4860 /* Delete all ES attached to thoses PMT */
4861 for( int i = 2; i < 8192; i++ )
4863 ts_pid_t *pid = &p_sys->pid[i];
4865 if( !pid->b_valid || pid->psi )
4868 for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
4870 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
4872 /* We only remove es that aren't defined by extra pmt */
4873 if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
4877 SetPIDFilter( p_demux, i, false );
4879 PIDClean( p_demux, pid );
4885 /* Delete PMT pid */
4886 for( int i = 0; i < i_pmt_rm; i++ )
4888 ts_pid_t *pid = pmt_rm[i];
4889 SetPIDFilter( p_demux, pid->i_pid, false );
4891 for( int i_prg = 0; i_prg < pid->psi->i_prg; i_prg++ )
4893 const int i_number = pid->psi->prg[i_prg]->i_number;
4894 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
4897 PIDClean( p_demux, &p_sys->pid[pid->i_pid] );
4898 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pid );
4904 /* now create programs */
4905 for( p_program = p_pat->p_first_program; p_program != NULL;
4906 p_program = p_program->p_next )
4908 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
4910 if( p_program->i_number == 0 )
4913 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
4915 ValidateDVBMeta( p_demux, p_program->i_pid );
4920 for( int i_prg = 0; b_add && i_prg < pmt->psi->i_prg; i_prg++ )
4921 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
4929 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
4932 PIDInit( pmt, true, pat->psi );
4933 ts_prg_psi_t *prg = pmt->psi->prg[pmt->psi->i_prg-1];
4934 #if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
4935 prg->handle = dvbpsi_new( &dvbpsi_messages, DVBPSI_MSG_DEBUG );
4938 dvbpsi_DeletePAT( p_pat );
4941 prg->handle->p_sys = (void *) VLC_OBJECT(p_demux);
4942 if( !dvbpsi_pmt_attach( prg->handle, p_program->i_number, PMTCallBack, p_demux ) )
4943 msg_Err( p_demux, "PATCallback failed attaching PMTCallback to program %d",
4944 p_program->i_number );
4946 prg->handle = dvbpsi_AttachPMT( p_program->i_number, PMTCallBack, p_demux );
4948 prg->i_number = p_program->i_number;
4949 prg->i_pid_pmt = p_program->i_pid;
4951 /* Now select PID at access level */
4952 if( ProgramIsSelected( p_demux, p_program->i_number ) )
4954 if( p_sys->i_current_program == 0 )
4955 p_sys->i_current_program = p_program->i_number;
4957 if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
4958 p_sys->b_access_control = false;
4961 pat->psi->i_pat_version = p_pat->i_version;
4963 dvbpsi_DeletePAT( p_pat );