1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
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
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 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 General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc_common.h>
34 #include <vlc_plugin.h>
38 #include <vlc_access.h> /* DVB-specific things */
39 #include <vlc_demux.h>
42 #include <vlc_charset.h> /* FromCharset, for EIT */
44 #include <vlc_network.h> /* net_ for ts-out mode */
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>
65 static void ts_debug(const char *format, ...)
70 vfprintf(stderr, format, ap);
77 /*****************************************************************************
79 *****************************************************************************/
80 static int Open ( vlc_object_t * );
81 static void Close ( vlc_object_t * );
84 * - Rename "extra pmt" to "user pmt"
85 * - Update extra pmt description
86 * pmt_pid[:pmt_number][=pid_description[,pid_description]]
87 * where pid_description could take 3 forms:
88 * 1. pid:pcr (to force the pcr pid)
90 * 3. pid:type=fourcc where type=(video|audio|spu)
92 #define PMT_TEXT N_("Extra PMT")
93 #define PMT_LONGTEXT N_( \
94 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
96 #define PID_TEXT N_("Set id of ES to PID")
97 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
98 " handled by VLC to the same value as the PID in" \
99 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
100 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
102 #define TSOUT_TEXT N_("Fast udp streaming")
103 #define TSOUT_LONGTEXT N_( \
104 "Sends TS to specific ip:port by udp (you must know what you are doing).")
106 #define MTUOUT_TEXT N_("MTU for out mode")
107 #define MTUOUT_LONGTEXT N_("MTU for out mode.")
109 #define CSA_TEXT N_("CSA Key")
110 #define CSA_LONGTEXT N_("CSA encryption key. This must be a " \
111 "16 char string (8 hexadecimal bytes).")
113 #define CSA2_TEXT N_("Second CSA Key")
114 #define CSA2_LONGTEXT N_("The even CSA encryption key. This must be a " \
115 "16 char string (8 hexadecimal bytes).")
117 #define SILENT_TEXT N_("Silent mode")
118 #define SILENT_LONGTEXT N_("Do not complain on encrypted PES.")
120 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
121 #define CAPMT_SYSID_LONGTEXT N_("Only forward descriptors from this SysID to the CAM.")
123 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
124 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
125 "The decryption routines subtract the TS-header from the value before " \
128 #define SPLIT_ES_TEXT N_("Separate sub-streams")
129 #define SPLIT_ES_LONGTEXT N_( \
130 "Separate teletex/dvbs pages into independent ES. " \
131 "It can be useful to turn off this option when using stream output." )
133 #define SEEK_PERCENT_TEXT N_("Seek based on percent not time")
134 #define SEEK_PERCENT_LONGTEXT N_( \
135 "Seek and position based on a percent byte position, not a PCR generated " \
136 "time position. If seeking doesn't work property, turn on this option." )
140 set_description( N_("MPEG Transport Stream demuxer") )
141 set_shortname ( "MPEG-TS" )
142 set_category( CAT_INPUT )
143 set_subcategory( SUBCAT_INPUT_DEMUX )
145 add_string( "ts-extra-pmt", NULL, PMT_TEXT, PMT_LONGTEXT, true )
146 add_bool( "ts-es-id-pid", true, PID_TEXT, PID_LONGTEXT, true )
148 add_string( "ts-out", NULL, TSOUT_TEXT, TSOUT_LONGTEXT, true )
149 add_integer( "ts-out-mtu", 1400, MTUOUT_TEXT,
150 MTUOUT_LONGTEXT, true )
152 add_string( "ts-csa-ck", NULL, CSA_TEXT, CSA_LONGTEXT, true )
154 add_string( "ts-csa2-ck", NULL, CSA2_TEXT, CSA2_LONGTEXT, true )
156 add_integer( "ts-csa-pkt", 188, CPKT_TEXT, CPKT_LONGTEXT, true )
159 add_bool( "ts-silent", false, SILENT_TEXT, SILENT_LONGTEXT, true )
161 add_bool( "ts-split-es", true, SPLIT_ES_TEXT, SPLIT_ES_LONGTEXT, false )
162 add_bool( "ts-seek-percent", false, SEEK_PERCENT_TEXT, SEEK_PERCENT_LONGTEXT, true )
164 set_capability( "demux", 10 )
165 set_callbacks( Open, Close )
169 /*****************************************************************************
171 *****************************************************************************/
172 static const char *const ppsz_teletext_type[] = {
175 N_("Teletext subtitles"),
176 N_("Teletext: additional information"),
177 N_("Teletext: program schedule"),
178 N_("Teletext subtitles: hearing impaired")
183 uint8_t i_objectTypeIndication;
184 uint8_t i_streamType;
186 uint32_t i_bufferSizeDB;
187 uint32_t i_maxBitrate;
188 uint32_t i_avgBitrate;
193 } decoder_config_descriptor_t;
200 bool b_streamDependenceFlag;
201 bool b_OCRStreamFlag;
202 uint8_t i_streamPriority;
206 uint16_t i_dependOn_es_id;
207 uint16_t i_OCR_es_id;
209 decoder_config_descriptor_t dec_descr;
211 } es_mpeg4_descriptor_t;
215 uint8_t i_iod_label, i_iod_label_scope;
221 uint8_t i_ODProfileLevelIndication;
222 uint8_t i_sceneProfileLevelIndication;
223 uint8_t i_audioProfileLevelIndication;
224 uint8_t i_visualProfileLevelIndication;
225 uint8_t i_graphicsProfileLevelIndication;
227 es_mpeg4_descriptor_t es_descr[255];
233 dvbpsi_handle handle;
240 /* IOD stuff (mpeg4) */
241 iod_descriptor_t *iod;
247 /* for special PAT/SDT case */
248 dvbpsi_handle handle; /* PAT/SDT/EIT */
267 es_mpeg4_descriptor_t *p_mpeg4desc;
278 int i_cc; /* countinuity counter */
281 /* PSI owner (ie PMT -> PAT, ES -> PMT */
289 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
297 vlc_mutex_t csa_lock;
299 /* TS packet size (188, 192, 204) */
302 /* how many TS packet we read at once */
305 /* to determine length and time */
308 mtime_t i_current_pcr;
310 bool b_force_seek_per_percent;
332 int fd; /* udp socket */
336 bool b_access_control;
342 int64_t i_dvb_length;
343 bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
346 int i_current_program;
347 vlc_list_t programs_list;
353 static int Demux ( demux_t *p_demux );
354 static int Control( demux_t *p_demux, int i_query, va_list args );
356 static void PIDInit ( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner );
357 static void PIDClean( demux_t *, ts_pid_t *pid );
358 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
360 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
361 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
362 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
363 uint8_t i_table_id, uint16_t i_extension );
364 static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
366 static inline int PIDGet( block_t *p )
368 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
371 static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
373 static block_t* ReadTSPacket( demux_t *p_demux );
374 static mtime_t GetPCR( block_t *p_pkt );
375 static int SeekToPCR( demux_t *p_demux, int64_t i_pos );
376 static int Seek( demux_t *p_demux, double f_percent );
377 static void GetFirstPCR( demux_t *p_demux );
378 static void GetLastPCR( demux_t *p_demux );
379 static void CheckPCR( demux_t *p_demux );
380 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
382 static iod_descriptor_t *IODNew( int , uint8_t * );
383 static void IODFree( iod_descriptor_t * );
385 #define TS_USER_PMT_NUMBER (0)
386 static int UserPmt( demux_t *p_demux, const char * );
388 static int SetPIDFilter( demux_t *, int i_pid, bool b_selected );
389 static void SetPrgFilter( demux_t *, int i_prg, bool b_selected );
391 #define TS_PACKET_SIZE_188 188
392 #define TS_PACKET_SIZE_192 192
393 #define TS_PACKET_SIZE_204 204
394 #define TS_PACKET_SIZE_MAX 204
395 #define TS_TOPFIELD_HEADER 1320
397 static int DetectPacketSize( demux_t *p_demux )
399 const uint8_t *p_peek;
400 if( stream_Peek( p_demux->s,
401 &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
404 if( memcmp( p_peek, "TFrc", 4 ) == 0 )
407 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
408 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
409 * but after the filename the offsets seem to be incorrect. - DJ */
410 int i_duration, i_name;
411 char *psz_name = xmalloc(25);
412 char *psz_event_name;
413 char *psz_event_text = xmalloc(130);
414 char *psz_ext_text = xmalloc(1025);
416 // 2 bytes version Uimsbf (4,5)
417 // 2 bytes reserved (6,7)
418 // 2 bytes duration in minutes Uimsbf (8,9(
419 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
420 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
421 // 2 bytes service number in channel list (10, 11)
422 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
423 // 4 bytes of reserved + tuner info (14,15,16,17)
424 // 2 bytes of Service ID Bslbf (18,19)
425 // 2 bytes of PMT PID Uimsbf (20,21)
426 // 2 bytes of PCR PID Uimsbf (22,23)
427 // 2 bytes of Video PID Uimsbf (24,25)
428 // 2 bytes of Audio PID Uimsbf (26,27)
429 // 24 bytes filename Bslbf
430 memcpy( psz_name, &p_peek[28], 24 );
432 msg_Dbg( p_demux, "recordingname=%s", psz_name );
433 // 1 byte of sat index Uimsbf (52)
434 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
435 // 4 bytes of freq. Uimsbf (56,57,58,59)
436 // 2 bytes of symbol rate Uimsbf (60,61)
437 // 2 bytes of TS stream ID Uimsbf (62,63)
440 // 2 bytes duration Uimsbf (70,71)
441 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
442 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
443 // 4 bytes EventID Uimsbf (72-75)
444 // 8 bytes of Start and End time info (76-83)
445 // 1 byte reserved (84)
446 // 1 byte event name length Uimsbf (89)
447 i_name = (int)(p_peek[89]&~0x81);
448 msg_Dbg( p_demux, "event name length = %d", i_name);
449 psz_event_name = xmalloc( i_name+1 );
450 // 1 byte parental rating (90)
451 // 129 bytes of event text
452 memcpy( psz_event_name, &p_peek[91], i_name );
453 psz_event_name[i_name] = '\0';
454 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
455 psz_event_text[129-i_name] = '\0';
456 msg_Dbg( p_demux, "event name=%s", psz_event_name );
457 msg_Dbg( p_demux, "event text=%s", psz_event_text );
458 // 12 bytes reserved (220)
460 // 2 bytes Event Text Length Uimsbf
461 // 4 bytes EventID Uimsbf
462 // FIXME We just have 613 bytes. not enough for this entire text
463 // 1024 bytes Extended Event Text Bslbf
464 memcpy( psz_ext_text, p_peek+372, 1024 );
465 psz_ext_text[1024] = '\0';
466 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
467 // 52 bytes reserved Bslbf
469 msg_Dbg( p_demux, "this is a topfield file" );
470 return TS_PACKET_SIZE_188;
473 for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
475 if( p_peek[i_sync] != 0x47 )
478 /* Check next 3 sync bytes */
479 int i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
480 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
482 msg_Err( p_demux, "cannot peek" );
485 if( p_peek[i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
486 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
487 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
489 return TS_PACKET_SIZE_188;
491 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
492 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
493 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
495 return TS_PACKET_SIZE_192;
497 else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
498 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
499 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
501 return TS_PACKET_SIZE_204;
505 if( p_demux->b_force )
507 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
508 return TS_PACKET_SIZE_188;
510 msg_Dbg( p_demux, "TS module discarded (lost sync)" );
514 /*****************************************************************************
516 *****************************************************************************/
517 static int Open( vlc_object_t *p_this )
519 demux_t *p_demux = (demux_t*)p_this;
526 /* Search first sync byte */
527 i_packet_size = DetectPacketSize( p_demux );
528 if( i_packet_size < 0 )
531 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
534 memset( p_sys, 0, sizeof( demux_sys_t ) );
535 p_sys->i_packet_size = i_packet_size;
536 vlc_mutex_init( &p_sys->csa_lock );
538 p_sys->buffer = NULL;
539 p_demux->pf_demux = Demux;
540 p_demux->pf_control = Control;
542 /* Init p_sys field */
543 p_sys->b_dvb_meta = true;
544 p_sys->b_access_control = true;
545 p_sys->i_current_program = 0;
546 p_sys->programs_list.i_count = 0;
547 p_sys->programs_list.p_values = NULL;
548 p_sys->i_tdt_delta = 0;
549 p_sys->i_dvb_start = 0;
550 p_sys->i_dvb_length = 0;
552 p_sys->b_broken_charset = false;
554 for( int i = 0; i < 8192; i++ )
556 ts_pid_t *pid = &p_sys->pid[i];
560 pid->b_valid = false;
562 /* PID 8191 is padding */
563 p_sys->pid[8191].b_seen = true;
564 p_sys->i_packet_size = i_packet_size;
565 p_sys->b_udp_out = false;
567 p_sys->i_ts_read = 50;
569 p_sys->b_start_record = false;
571 /* Init PAT handler */
572 pat = &p_sys->pid[0];
573 PIDInit( pat, true, NULL );
574 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
576 if( p_sys->b_dvb_meta )
578 ts_pid_t *sdt = &p_sys->pid[0x11];
579 ts_pid_t *eit = &p_sys->pid[0x12];
581 PIDInit( sdt, true, NULL );
583 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
585 PIDInit( eit, true, NULL );
587 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
590 ts_pid_t *tdt = &p_sys->pid[0x14];
591 PIDInit( tdt, true, NULL );
593 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
595 if( p_sys->b_access_control )
597 if( SetPIDFilter( p_demux, 0x11, true ) ||
598 SetPIDFilter( p_demux, 0x14, true ) ||
599 SetPIDFilter( p_demux, 0x12, true ) )
600 p_sys->b_access_control = false;
605 TAB_INIT( p_sys->i_pmt, p_sys->pmt );
609 p_sys->b_es_id_pid = var_CreateGetBool( p_demux, "ts-es-id-pid" );
611 char* psz_string = var_CreateGetString( p_demux, "ts-out" );
612 if( psz_string && *psz_string )
614 char *psz = strchr( psz_string, ':' );
617 p_sys->b_udp_out = true;
622 i_port = atoi( psz );
624 if( i_port <= 0 ) i_port = 1234;
625 msg_Dbg( p_demux, "resend ts to '%s:%d'", psz_string, i_port );
627 p_sys->fd = net_ConnectUDP( VLC_OBJECT(p_demux), psz_string, i_port, -1 );
630 msg_Err( p_demux, "failed to open udp socket, send disabled" );
631 p_sys->b_udp_out = false;
635 int i_mtu = var_CreateGetInteger( p_demux, "ts-out-mtu" );
636 p_sys->i_ts_read = i_mtu / p_sys->i_packet_size;
637 if( p_sys->i_ts_read <= 0 )
639 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
641 p_sys->buffer = xmalloc( p_sys->i_packet_size * p_sys->i_ts_read );
646 /* We handle description of an extra PMT */
647 psz_string = var_CreateGetString( p_demux, "ts-extra-pmt" );
648 p_sys->b_user_pmt = false;
649 if( psz_string && *psz_string )
650 UserPmt( p_demux, psz_string );
653 psz_string = var_CreateGetStringCommand( p_demux, "ts-csa-ck" );
654 if( psz_string && *psz_string )
659 p_sys->csa = csa_New();
661 psz_csa2 = var_CreateGetStringCommand( p_demux, "ts-csa2-ck" );
662 i_res = csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, true );
663 if( i_res == VLC_SUCCESS && psz_csa2 && *psz_csa2 )
665 if( csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_csa2, false ) != VLC_SUCCESS )
667 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
670 else if ( i_res == VLC_SUCCESS )
672 csa_SetCW( (vlc_object_t*)p_demux, p_sys->csa, psz_string, false );
676 csa_Delete( p_sys->csa );
682 var_AddCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, (void *)1 );
683 var_AddCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
685 int i_pkt = var_CreateGetInteger( p_demux, "ts-csa-pkt" );
686 if( i_pkt < 4 || i_pkt > 188 )
688 msg_Err( p_demux, "wrong packet size %d specified.", i_pkt );
689 msg_Warn( p_demux, "using default packet size of 188 bytes" );
690 p_sys->i_csa_pkt_size = 188;
693 p_sys->i_csa_pkt_size = i_pkt;
694 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
700 p_sys->b_silent = var_CreateGetBool( p_demux, "ts-silent" );
701 p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
703 p_sys->i_pid_ref_pcr = -1;
704 p_sys->i_first_pcr = -1;
705 p_sys->i_current_pcr = -1;
706 p_sys->i_last_pcr = -1;
707 p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
708 p_sys->i_pcrs_num = 10;
709 p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
710 p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
712 bool can_seek = false;
713 stream_Control( p_demux->s, STREAM_CAN_FASTSEEK, &can_seek );
716 GetFirstPCR( p_demux );
718 GetLastPCR( p_demux );
720 if( p_sys->i_first_pcr < 0 || p_sys->i_last_pcr < 0 )
722 p_sys->b_force_seek_per_percent = true;
725 while( p_sys->i_pmt_es <= 0 && vlc_object_alive( p_demux ) )
727 if( p_demux->pf_demux( p_demux ) != 1 )
734 /*****************************************************************************
736 *****************************************************************************/
737 static void Close( vlc_object_t *p_this )
739 demux_t *p_demux = (demux_t*)p_this;
740 demux_sys_t *p_sys = p_demux->p_sys;
742 msg_Dbg( p_demux, "pid list:" );
743 for( int i = 0; i < 8192; i++ )
745 ts_pid_t *pid = &p_sys->pid[i];
747 if( pid->b_valid && pid->psi )
752 dvbpsi_DetachPAT( pid->psi->handle );
759 if( p_sys->b_dvb_meta && ( pid->i_pid == 0x11 || pid->i_pid == 0x12 || pid->i_pid == 0x14 ) )
761 /* SDT or EIT or TDT */
762 dvbpsi_DetachDemux( pid->psi->handle );
767 PIDClean( p_demux, pid );
772 else if( pid->b_valid && pid->es )
774 PIDClean( p_demux, pid );
779 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
784 SetPIDFilter( p_demux, pid->i_pid, false );
787 vlc_mutex_lock( &p_sys->csa_lock );
790 var_DelCallback( p_demux, "ts-csa-ck", ChangeKeyCallback, NULL );
791 var_DelCallback( p_demux, "ts-csa2-ck", ChangeKeyCallback, NULL );
792 csa_Delete( p_sys->csa );
794 vlc_mutex_unlock( &p_sys->csa_lock );
796 TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
798 free( p_sys->programs_list.p_values );
800 /* When streaming, close the port */
803 net_Close( p_sys->fd );
806 free( p_sys->buffer );
808 free( p_sys->p_pcrs );
809 free( p_sys->p_pos );
811 vlc_mutex_destroy( &p_sys->csa_lock );
815 /*****************************************************************************
816 * ChangeKeyCallback: called when changing the odd encryption key on the fly.
817 *****************************************************************************/
818 static int ChangeKeyCallback( vlc_object_t *p_this, char const *psz_cmd,
819 vlc_value_t oldval, vlc_value_t newval,
822 VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
823 demux_t *p_demux = (demux_t*)p_this;
824 demux_sys_t *p_sys = p_demux->p_sys;
825 int i_tmp = (intptr_t)p_data;
827 vlc_mutex_lock( &p_sys->csa_lock );
829 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, true );
831 i_tmp = csa_SetCW( p_this, p_sys->csa, newval.psz_string, false );
833 vlc_mutex_unlock( &p_sys->csa_lock );
837 /*****************************************************************************
839 *****************************************************************************/
840 static int Demux( demux_t *p_demux )
842 demux_sys_t *p_sys = p_demux->p_sys;
843 bool b_wait_es = p_sys->i_pmt_es <= 0;
845 /* We read at most 100 TS packet or until a frame is completed */
846 for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
848 bool b_frame = false;
850 if( !(p_pkt = ReadTSPacket( p_demux )) )
855 if( p_sys->b_start_record )
857 /* Enable recording once synchronized */
858 stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, true, "ts" );
859 p_sys->b_start_record = false;
862 if( p_sys->b_udp_out )
864 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
865 p_pkt->p_buffer, p_sys->i_packet_size );
868 /* Parse the TS packet */
869 ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
875 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 ) ) )
877 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
881 for( int i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
883 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
887 block_Release( p_pkt );
889 else if( !p_sys->b_udp_out )
891 b_frame = GatherPES( p_demux, p_pid, p_pkt );
895 PCRHandle( p_demux, p_pid, p_pkt );
896 block_Release( p_pkt );
903 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
905 /* We have to handle PCR if present */
906 PCRHandle( p_demux, p_pid, p_pkt );
907 block_Release( p_pkt );
909 p_pid->b_seen = true;
911 if( b_frame || ( b_wait_es && p_sys->i_pmt_es > 0 ) )
915 if( p_sys->b_udp_out )
917 /* Send the complete block */
918 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
919 p_sys->i_ts_read * p_sys->i_packet_size );
925 /*****************************************************************************
927 *****************************************************************************/
928 static int DVBEventInformation( demux_t *p_demux, int64_t *pi_time, int64_t *pi_length )
930 demux_sys_t *p_sys = p_demux->p_sys;
936 if( p_sys->i_dvb_length > 0 )
938 const int64_t t = mdate() + p_sys->i_tdt_delta;
940 if( p_sys->i_dvb_start <= t && t < p_sys->i_dvb_start + p_sys->i_dvb_length )
943 *pi_length = p_sys->i_dvb_length;
945 *pi_time = t - p_sys->i_dvb_start;
952 static int Control( demux_t *p_demux, int i_query, va_list args )
954 demux_sys_t *p_sys = p_demux->p_sys;
956 bool b_bool, *pb_bool;
963 case DEMUX_GET_POSITION:
964 pf = (double*) va_arg( args, double* );
966 if( p_sys->b_force_seek_per_percent ||
967 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
968 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 ||
969 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
971 int64_t i_time, i_length;
972 if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
973 *pf = (double)i_time/(double)i_length;
974 else if( (i64 = stream_Size( p_demux->s) ) > 0 )
975 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
981 *pf = (double)(p_sys->i_current_pcr - p_sys->i_first_pcr) / (double)(p_sys->i_last_pcr - p_sys->i_first_pcr);
985 case DEMUX_SET_POSITION:
986 f = (double) va_arg( args, double );
988 if( p_sys->b_force_seek_per_percent ||
989 (p_sys->b_dvb_meta && p_sys->b_access_control) ||
990 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
992 i64 = stream_Size( p_demux->s );
993 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
998 if( Seek( p_demux, f ) )
1000 p_sys->b_force_seek_per_percent = true;
1001 return VLC_EGENERIC;
1006 case DEMUX_GET_TIME:
1007 pi64 = (int64_t*)va_arg( args, int64_t * );
1008 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1009 p_sys->b_force_seek_per_percent ||
1010 p_sys->i_current_pcr - p_sys->i_first_pcr < 0 )
1012 if( DVBEventInformation( p_demux, pi64, NULL ) )
1019 *pi64 = (p_sys->i_current_pcr - p_sys->i_first_pcr) * 100 / 9;
1023 case DEMUX_GET_LENGTH:
1024 pi64 = (int64_t*)va_arg( args, int64_t * );
1025 if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
1026 p_sys->b_force_seek_per_percent ||
1027 p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
1029 if( DVBEventInformation( p_demux, NULL, pi64 ) )
1036 *pi64 = (p_sys->i_last_pcr - p_sys->i_first_pcr) * 100 / 9;
1040 case DEMUX_SET_GROUP:
1044 i_int = (int)va_arg( args, int );
1045 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1046 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1048 if( i_int == 0 && p_sys->i_current_program > 0 )
1049 i_int = p_sys->i_current_program;
1051 if( p_sys->i_current_program > 0 )
1053 if( p_sys->i_current_program != i_int )
1054 SetPrgFilter( p_demux, p_sys->i_current_program, false );
1056 else if( p_sys->i_current_program < 0 )
1058 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
1059 SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
1064 p_sys->i_current_program = i_int;
1065 SetPrgFilter( p_demux, p_sys->i_current_program, true );
1067 else if( i_int < 0 )
1069 p_sys->i_current_program = -1;
1070 p_sys->programs_list.i_count = 0;
1073 vlc_list_t *p_dst = &p_sys->programs_list;
1074 free( p_dst->p_values );
1076 p_dst->p_values = calloc( p_list->i_count,
1077 sizeof(*p_dst->p_values) );
1078 if( p_dst->p_values )
1080 p_dst->i_count = p_list->i_count;
1081 for( int i = 0; i < p_list->i_count; i++ )
1083 p_dst->p_values[i] = p_list->p_values[i];
1084 SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
1092 case DEMUX_CAN_RECORD:
1093 pb_bool = (bool*)va_arg( args, bool * );
1097 case DEMUX_SET_RECORD_STATE:
1098 b_bool = (bool)va_arg( args, int );
1101 stream_Control( p_demux->s, STREAM_SET_RECORD_STATE, false );
1102 p_sys->b_start_record = b_bool;
1106 case DEMUX_SET_TIME:
1108 return VLC_EGENERIC;
1112 /*****************************************************************************
1114 *****************************************************************************/
1115 static int UserPmt( demux_t *p_demux, const char *psz_fmt )
1117 demux_sys_t *p_sys = p_demux->p_sys;
1118 char *psz_dup = strdup( psz_fmt );
1119 char *psz = psz_dup;
1127 i_pid = strtol( psz, &psz, 0 );
1128 if( i_pid < 2 || i_pid >= 8192 )
1131 /* Parse optional program number */
1134 i_number = strtol( &psz[1], &psz, 0 );
1137 ts_pid_t *pmt = &p_sys->pid[i_pid];
1140 msg_Dbg( p_demux, "user pmt specified (pid=%d,number=%d)", i_pid, i_number );
1141 PIDInit( pmt, true, NULL );
1144 prg = malloc( sizeof( ts_prg_psi_t ) );
1148 memset( prg, 0, sizeof( ts_prg_psi_t ) );
1149 prg->i_pid_pcr = -1;
1150 prg->i_pid_pmt = -1;
1151 prg->i_version = -1;
1152 prg->i_number = i_number != 0 ? i_number : TS_USER_PMT_NUMBER;
1153 prg->handle = dvbpsi_AttachPMT( i_number != TS_USER_PMT_NUMBER ? i_number : 1, (dvbpsi_pmt_callback)PMTCallBack, p_demux );
1154 TAB_APPEND( pmt->psi->i_prg, pmt->psi->prg, prg );
1156 psz = strchr( psz, '=' );
1159 while( psz && *psz )
1161 char *psz_next = strchr( psz, ',' );
1167 i_pid = strtol( psz, &psz, 0 );
1168 if( *psz != ':' || i_pid < 2 || i_pid >= 8192 )
1171 char *psz_opt = &psz[1];
1172 if( !strcmp( psz_opt, "pcr" ) )
1174 prg->i_pid_pcr = i_pid;
1176 else if( !p_sys->pid[i_pid].b_valid )
1178 ts_pid_t *pid = &p_sys->pid[i_pid];
1180 char *psz_arg = strchr( psz_opt, '=' );
1184 PIDInit( pid, false, pmt->psi);
1185 if( prg->i_pid_pcr <= 0 )
1186 prg->i_pid_pcr = i_pid;
1188 if( psz_arg && strlen( psz_arg ) == 4 )
1190 const vlc_fourcc_t i_codec = VLC_FOURCC( psz_arg[0], psz_arg[1],
1191 psz_arg[2], psz_arg[3] );
1192 int i_cat = UNKNOWN_ES;
1193 es_format_t *fmt = &pid->es->fmt;
1195 if( !strcmp( psz_opt, "video" ) )
1197 else if( !strcmp( psz_opt, "audio" ) )
1199 else if( !strcmp( psz_opt, "spu" ) )
1202 es_format_Init( fmt, i_cat, i_codec );
1203 fmt->b_packetized = false;
1207 const int i_stream_type = strtol( psz_opt, NULL, 0 );
1208 PIDFillFormat( pid, i_stream_type );
1210 pid->es->fmt.i_group = i_number;
1211 if( p_sys->b_es_id_pid )
1212 pid->es->fmt.i_id = i_pid;
1214 if( pid->es->fmt.i_cat != UNKNOWN_ES )
1216 msg_Dbg( p_demux, " * es pid=%d fcc=%4.4s", i_pid,
1217 (char*)&pid->es->fmt.i_codec );
1218 pid->es->id = es_out_Add( p_demux->out,
1228 p_sys->b_user_pmt = true;
1229 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
1235 return VLC_EGENERIC;
1238 static int SetPIDFilter( demux_t *p_demux, int i_pid, bool b_selected )
1240 demux_sys_t *p_sys = p_demux->p_sys;
1242 if( !p_sys->b_access_control )
1243 return VLC_EGENERIC;
1245 return stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1246 ACCESS_SET_PRIVATE_ID_STATE, i_pid, b_selected );
1249 static void SetPrgFilter( demux_t *p_demux, int i_prg_id, bool b_selected )
1251 demux_sys_t *p_sys = p_demux->p_sys;
1252 ts_prg_psi_t *p_prg = NULL;
1255 /* Search pmt to be unselected */
1256 for( int i = 0; i < p_sys->i_pmt; i++ )
1258 ts_pid_t *pmt = p_sys->pmt[i];
1260 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1262 if( pmt->psi->prg[i_prg]->i_number == i_prg_id )
1264 i_pmt_pid = p_sys->pmt[i]->i_pid;
1265 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1272 if( i_pmt_pid <= 0 )
1276 SetPIDFilter( p_demux, i_pmt_pid, b_selected );
1277 if( p_prg->i_pid_pcr > 0 )
1278 SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
1281 for( int i = 2; i < 8192; i++ )
1283 ts_pid_t *pid = &p_sys->pid[i];
1285 if( !pid->b_valid || pid->psi )
1288 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1290 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1292 /* We only remove/select es that aren't defined by extra pmt */
1293 SetPIDFilter( p_demux, i, b_selected );
1300 static void PIDInit( ts_pid_t *pid, bool b_psi, ts_psi_t *p_owner )
1302 bool b_old_valid = pid->b_valid;
1304 pid->b_valid = true;
1306 pid->b_scrambled = false;
1307 pid->p_owner = p_owner;
1308 pid->i_owner_number = 0;
1310 TAB_INIT( pid->i_extra_es, pid->extra_es );
1318 pid->psi = xmalloc( sizeof( ts_psi_t ) );
1319 pid->psi->handle = NULL;
1320 TAB_INIT( pid->psi->i_prg, pid->psi->prg );
1324 pid->psi->i_pat_version = -1;
1325 pid->psi->i_sdt_version = -1;
1328 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1332 prg->i_version = -1;
1334 prg->i_pid_pcr = -1;
1335 prg->i_pid_pmt = -1;
1336 prg->i_pcr_value= -1;
1340 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1347 pid->es = malloc( sizeof( ts_es_t ) );
1350 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1352 pid->es->p_pes = NULL;
1353 pid->es->i_pes_size= 0;
1354 pid->es->i_pes_gathered= 0;
1355 pid->es->pp_last = &pid->es->p_pes;
1356 pid->es->p_mpeg4desc = NULL;
1357 pid->es->b_gather = false;
1362 static void PIDClean( demux_t *p_demux, ts_pid_t *pid )
1364 demux_sys_t *p_sys = p_demux->p_sys;
1365 es_out_t *out = p_demux->out;
1369 if( pid->psi->handle )
1370 dvbpsi_DetachPMT( pid->psi->handle );
1371 for( int i = 0; i < pid->psi->i_prg; i++ )
1373 if( pid->psi->prg[i]->iod )
1374 IODFree( pid->psi->prg[i]->iod );
1375 if( pid->psi->prg[i]->handle )
1376 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1377 free( pid->psi->prg[i] );
1379 free( pid->psi->prg );
1386 es_out_Del( out, pid->es->id );
1390 if( pid->es->p_pes )
1391 block_ChainRelease( pid->es->p_pes );
1393 es_format_Clean( &pid->es->fmt );
1397 for( int i = 0; i < pid->i_extra_es; i++ )
1399 if( pid->extra_es[i]->id )
1401 es_out_Del( out, pid->extra_es[i]->id );
1405 if( pid->extra_es[i]->p_pes )
1406 block_ChainRelease( pid->extra_es[i]->p_pes );
1408 es_format_Clean( &pid->extra_es[i]->fmt );
1410 free( pid->extra_es[i] );
1412 if( pid->i_extra_es )
1413 free( pid->extra_es );
1416 pid->b_valid = false;
1419 /****************************************************************************
1421 ****************************************************************************/
1422 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1424 block_t *p_pes = pid->es->p_pes;
1426 unsigned i_pes_size = 0;
1427 unsigned i_skip = 0;
1430 mtime_t i_length = 0;
1432 /* remove the pes from pid */
1433 pid->es->p_pes = NULL;
1434 pid->es->i_pes_size= 0;
1435 pid->es->i_pes_gathered= 0;
1436 pid->es->pp_last = &pid->es->p_pes;
1438 /* FIXME find real max size */
1439 /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
1441 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1443 if( !p_demux->p_sys->b_silent )
1444 msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
1445 header[0], header[1],header[2],header[3], pid->i_pid );
1446 block_ChainRelease( p_pes );
1450 /* TODO check size */
1453 case 0xBC: /* Program stream map */
1454 case 0xBE: /* Padding */
1455 case 0xBF: /* Private stream 2 */
1456 case 0xF0: /* ECM */
1457 case 0xF1: /* EMM */
1458 case 0xFF: /* Program stream directory */
1459 case 0xF2: /* DSMCC stream */
1460 case 0xF8: /* ITU-T H.222.1 type E stream */
1464 if( ( header[6]&0xC0 ) == 0x80 )
1467 i_skip = header[8] + 9;
1469 if( header[7]&0x80 ) /* has pts */
1471 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1472 (mtime_t)(header[10] << 22)|
1473 ((mtime_t)(header[11]&0xfe) << 14)|
1474 (mtime_t)(header[12] << 7)|
1475 (mtime_t)(header[13] >> 1);
1477 if( header[7]&0x40 ) /* has dts */
1479 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1480 (mtime_t)(header[15] << 22)|
1481 ((mtime_t)(header[16]&0xfe) << 14)|
1482 (mtime_t)(header[17] << 7)|
1483 (mtime_t)(header[18] >> 1);
1490 while( i_skip < 23 && header[i_skip] == 0xff )
1496 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1497 block_ChainRelease( p_pes );
1500 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1505 if( header[i_skip]&0x20 )
1507 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1508 (mtime_t)(header[i_skip+1] << 22)|
1509 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1510 (mtime_t)(header[i_skip+3] << 7)|
1511 (mtime_t)(header[i_skip+4] >> 1);
1513 if( header[i_skip]&0x10 ) /* has dts */
1515 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1516 (mtime_t)(header[i_skip+6] << 22)|
1517 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1518 (mtime_t)(header[i_skip+8] << 7)|
1519 (mtime_t)(header[i_skip+9] >> 1);
1535 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1536 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1540 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1541 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1542 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1546 else if( pid->es->fmt.i_codec == VLC_CODEC_SUBT &&
1547 pid->es->p_mpeg4desc )
1549 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1551 if( dcd->i_extra > 2 &&
1552 dcd->p_extra[0] == 0x10 &&
1553 ( dcd->p_extra[1]&0x10 ) )
1555 /* display length */
1556 if( p_pes->i_buffer + 2 <= i_skip )
1557 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1561 if( p_pes->i_buffer + 2 <= i_skip )
1562 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1568 while( p_pes && i_skip > 0 )
1570 if( p_pes->i_buffer <= i_skip )
1572 block_t *p_next = p_pes->p_next;
1574 i_skip -= p_pes->i_buffer;
1575 block_Release( p_pes );
1580 p_pes->i_buffer -= i_skip;
1581 p_pes->p_buffer += i_skip;
1586 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1587 if( i_pts >= 0 && i_dts < 0 )
1595 p_pes->i_dts = VLC_TS_0 + i_dts * 100 / 9;
1598 p_pes->i_pts = VLC_TS_0 + i_pts * 100 / 9;
1600 p_pes->i_length = i_length * 100 / 9;
1602 p_block = block_ChainGather( p_pes );
1603 if( pid->es->fmt.i_codec == VLC_CODEC_SUBT )
1605 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1607 p_block->i_buffer = i_pes_size;
1610 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1613 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1615 else if( pid->es->fmt.i_codec == VLC_CODEC_TELETEXT )
1617 if( p_block->i_pts <= VLC_TS_INVALID )
1619 /* Teletext may have missing PTS (ETSI EN 300 472 Annexe A)
1620 * In this case use the last PCR + 40ms */
1621 for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
1623 if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
1625 mtime_t i_pcr = pid->p_owner->prg[i]->i_pcr_value;
1626 if( i_pcr > VLC_TS_INVALID )
1627 p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
1634 for( int i = 0; i < pid->i_extra_es; i++ )
1636 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1637 block_Duplicate( p_block ) );
1640 es_out_Send( p_demux->out, pid->es->id, p_block );
1644 msg_Warn( p_demux, "empty pes" );
1648 static block_t* ReadTSPacket( demux_t *p_demux )
1650 demux_sys_t *p_sys = p_demux->p_sys;
1654 /* Get a new TS packet */
1655 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1657 msg_Dbg( p_demux, "eof ?" );
1661 /* Check sync byte and re-sync if needed */
1662 if( p_pkt->p_buffer[0] != 0x47 )
1664 msg_Warn( p_demux, "lost synchro" );
1665 block_Release( p_pkt );
1666 while( vlc_object_alive (p_demux) )
1668 const uint8_t *p_peek;
1669 int i_peek, i_skip = 0;
1671 i_peek = stream_Peek( p_demux->s, &p_peek,
1672 p_sys->i_packet_size * 10 );
1673 if( i_peek < p_sys->i_packet_size + 1 )
1675 msg_Dbg( p_demux, "eof ?" );
1679 while( i_skip < i_peek - p_sys->i_packet_size )
1681 if( p_peek[i_skip] == 0x47 &&
1682 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
1688 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
1689 stream_Read( p_demux->s, NULL, i_skip );
1691 if( i_skip < i_peek - p_sys->i_packet_size )
1696 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1698 msg_Dbg( p_demux, "eof ?" );
1705 static mtime_t AdjustPCRWrapAround( demux_t *p_demux, mtime_t i_pcr )
1707 demux_sys_t *p_sys = p_demux->p_sys;
1709 * PCR is 33bit. If PCR reaches to 0x1FFFFFFFF (26:30:43.717), ressets from 0.
1710 * So, need to add 0x1FFFFFFFF, for calculating duration or current position.
1712 mtime_t i_adjust = 0;
1713 int64_t i_pos = stream_Tell( p_demux->s );
1715 for( i = 1; i < p_sys->i_pcrs_num && p_sys->p_pos[i] <= i_pos; ++i )
1717 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
1718 i_adjust += 0x1FFFFFFFF;
1720 if( p_sys->p_pcrs[i-1] > i_pcr )
1721 i_adjust += 0x1FFFFFFFF;
1723 return i_pcr + i_adjust;
1726 static mtime_t GetPCR( block_t *p_pkt )
1728 const uint8_t *p = p_pkt->p_buffer;
1732 if( ( p[3]&0x20 ) && /* adaptation */
1736 /* PCR is 33 bits */
1737 i_pcr = ( (mtime_t)p[6] << 25 ) |
1738 ( (mtime_t)p[7] << 17 ) |
1739 ( (mtime_t)p[8] << 9 ) |
1740 ( (mtime_t)p[9] << 1 ) |
1741 ( (mtime_t)p[10] >> 7 );
1746 static int SeekToPCR( demux_t *p_demux, int64_t i_pos )
1748 demux_sys_t *p_sys = p_demux->p_sys;
1751 int64_t i_initial_pos = stream_Tell( p_demux->s );
1754 return VLC_EGENERIC;
1756 int64_t i_last_pos = i_pos + p_sys->i_packet_size * 4500; //XXX
1757 if( i_last_pos > stream_Size( p_demux->s ) - p_sys->i_packet_size )
1759 i_last_pos = stream_Size( p_demux->s ) - p_sys->i_packet_size;
1762 if( stream_Seek( p_demux->s, i_pos ) )
1763 return VLC_EGENERIC;
1765 while( vlc_object_alive( p_demux ) )
1768 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
1772 if( PIDGet( p_pkt ) == p_sys->i_pid_ref_pcr )
1774 i_pcr = GetPCR( p_pkt );
1776 block_Release( p_pkt );
1779 if( stream_Tell( p_demux->s ) >= i_last_pos )
1784 stream_Seek( p_demux->s, i_initial_pos );
1785 return VLC_EGENERIC;
1789 p_sys->i_current_pcr = i_pcr;
1794 static int Seek( demux_t *p_demux, double f_percent )
1796 demux_sys_t *p_sys = p_demux->p_sys;
1798 int64_t i_initial_pos = stream_Tell( p_demux->s );
1799 mtime_t i_initial_pcr = p_sys->i_current_pcr;
1802 * Find the time position by using binary search algorithm.
1804 mtime_t i_target_pcr = (p_sys->i_last_pcr - p_sys->i_first_pcr) * f_percent + p_sys->i_first_pcr;
1806 int64_t i_head_pos = 0;
1807 int64_t i_tail_pos = stream_Size( p_demux->s );
1809 mtime_t i_adjust = 0;
1811 for( i = 1; i < p_sys->i_pcrs_num; ++i )
1813 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
1814 i_adjust += 0x1FFFFFFFF;
1815 if( p_sys->p_pcrs[i] + i_adjust > i_target_pcr )
1818 i_head_pos = p_sys->p_pos[i-1];
1819 i_tail_pos = ( i < p_sys->i_pcrs_num ) ? p_sys->p_pos[i] : stream_Size( p_demux->s );
1821 msg_Dbg( p_demux, "Seek():i_head_pos:%"PRId64", i_tail_pos:%"PRId64, i_head_pos, i_tail_pos);
1823 bool b_found = false;
1825 while( i_head_pos <= i_tail_pos )
1827 int64_t i_pos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
1828 if( SeekToPCR( p_demux, i_pos ) )
1830 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
1831 int64_t i_diff_msec = (p_sys->i_current_pcr - i_target_pcr) * 100 / 9 / 1000;
1832 if( i_diff_msec > 500 )
1834 i_tail_pos = i_pos - p_sys->i_packet_size;
1836 else if( i_diff_msec < -500 )
1838 i_head_pos = i_pos + p_sys->i_packet_size;
1842 // diff time <= 500msec
1850 msg_Dbg( p_demux, "Seek():cannot find a time position. i_cnt:%d", i_cnt );
1851 stream_Seek( p_demux->s, i_initial_pos );
1852 p_sys->i_current_pcr = i_initial_pcr;
1853 return VLC_EGENERIC;
1857 msg_Dbg( p_demux, "Seek():can find a time position. i_cnt:%d", i_cnt );
1862 static void GetFirstPCR( demux_t *p_demux )
1864 demux_sys_t *p_sys = p_demux->p_sys;
1866 int64_t i_initial_pos = stream_Tell( p_demux->s );
1868 if( stream_Seek( p_demux->s, 0 ) )
1871 while( vlc_object_alive (p_demux) )
1874 if( !( p_pkt = ReadTSPacket( p_demux ) ) )
1878 mtime_t i_pcr = GetPCR( p_pkt );
1881 p_sys->i_pid_ref_pcr = PIDGet( p_pkt );
1882 p_sys->i_first_pcr = i_pcr;
1883 p_sys->i_current_pcr = i_pcr;
1885 block_Release( p_pkt );
1886 if( p_sys->i_first_pcr >= 0 )
1889 stream_Seek( p_demux->s, i_initial_pos );
1892 static void GetLastPCR( demux_t *p_demux )
1894 demux_sys_t *p_sys = p_demux->p_sys;
1896 int64_t i_initial_pos = stream_Tell( p_demux->s );
1897 mtime_t i_initial_pcr = p_sys->i_current_pcr;
1899 int64_t i_last_pos = stream_Size( p_demux->s ) - p_sys->i_packet_size;
1900 int64_t i_pos = i_last_pos - p_sys->i_packet_size * 4500; /* FIXME if the value is not reasonable, please change it. */
1904 while( vlc_object_alive( p_demux ) )
1906 if( SeekToPCR( p_demux, i_pos ) )
1908 p_sys->i_last_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
1909 if( ( i_pos = stream_Tell( p_demux->s ) ) >= i_last_pos )
1912 if( p_sys->i_last_pcr >= 0 )
1914 int64_t i_size = stream_Size( p_demux->s );
1915 mtime_t i_duration_msec = ( p_sys->i_last_pcr - p_sys->i_first_pcr ) * 100 / 9 / 1000;
1916 int64_t i_rate = ( i_size < 0 || i_duration_msec <= 0 ) ? 0 : i_size * 1000 * 8 / i_duration_msec;
1917 const int64_t TS_SUPPOSED_MAXRATE = 55 * 1000 * 1000; //FIXME I think it's enough.
1918 const int64_t TS_SUPPOSED_MINRATE = 0.5 * 1000 * 1000; //FIXME
1919 if( i_rate < TS_SUPPOSED_MINRATE || i_rate > TS_SUPPOSED_MAXRATE )
1921 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)",
1922 i_rate, TS_SUPPOSED_MINRATE, TS_SUPPOSED_MAXRATE );
1923 p_sys->i_last_pcr = -1;
1926 stream_Seek( p_demux->s, i_initial_pos );
1927 p_sys->i_current_pcr = i_initial_pcr;
1930 static void CheckPCR( demux_t *p_demux )
1932 demux_sys_t *p_sys = p_demux->p_sys;
1934 int64_t i_initial_pos = stream_Tell( p_demux->s );
1935 mtime_t i_initial_pcr = p_sys->i_current_pcr;
1937 int64_t i_size = stream_Size( p_demux->s );
1940 p_sys->p_pcrs[0] = p_sys->i_first_pcr;
1941 p_sys->p_pos[0] = i_initial_pos;
1943 for( i = 1; i < p_sys->i_pcrs_num && vlc_object_alive( p_demux ); ++i )
1945 int64_t i_pos = i_size / p_sys->i_pcrs_num * i;
1946 if( SeekToPCR( p_demux, i_pos ) )
1948 p_sys->p_pcrs[i] = p_sys->i_current_pcr;
1949 p_sys->p_pos[i] = stream_Tell( p_demux->s );
1950 if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
1952 msg_Dbg( p_demux, "PCR Wrap Around found between %d%% and %d%% (pcr:%"PRId64"(0x%09"PRIx64") pcr:%"PRId64"(0x%09"PRIx64"))",
1953 (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] );
1956 if( i < p_sys->i_pcrs_num )
1958 msg_Dbg( p_demux, "Force Seek Per Percent: Seeking failed at %d%%.", (int)(i*100/p_sys->i_pcrs_num) );
1959 p_sys->b_force_seek_per_percent = true;
1962 stream_Seek( p_demux->s, i_initial_pos );
1963 p_sys->i_current_pcr = i_initial_pcr;
1966 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1968 demux_sys_t *p_sys = p_demux->p_sys;
1970 if( p_sys->i_pmt_es <= 0 )
1973 mtime_t i_pcr = GetPCR( p_bk );
1976 if( p_sys->i_pid_ref_pcr == pid->i_pid )
1978 p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
1981 /* Search program and set the PCR */
1982 for( int i = 0; i < p_sys->i_pmt; i++ )
1984 for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1986 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1988 p_sys->pmt[i]->psi->prg[i_prg]->i_pcr_value = i_pcr;
1989 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1990 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1991 (int64_t)(VLC_TS_0 + i_pcr * 100 / 9) );
1998 static bool GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
2000 const uint8_t *p = p_bk->p_buffer;
2001 const bool b_unit_start = p[1]&0x40;
2002 const bool b_scrambled = p[3]&0x80;
2003 const bool b_adaptation = p[3]&0x20;
2004 const bool b_payload = p[3]&0x10;
2005 const int i_cc = p[3]&0x0f; /* continuity counter */
2006 bool b_discontinuity = false; /* discontinuity */
2008 /* transport_scrambling_control is ignored */
2013 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
2014 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
2018 /* For now, ignore additional error correction
2019 * TODO: handle Reed-Solomon 204,188 error correction */
2020 p_bk->i_buffer = TS_PACKET_SIZE_188;
2024 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
2026 if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
2027 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
2030 if( p_demux->p_sys->csa )
2032 vlc_mutex_lock( &p_demux->p_sys->csa_lock );
2033 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
2034 vlc_mutex_unlock( &p_demux->p_sys->csa_lock );
2039 /* We don't have any adaptation_field, so payload starts
2040 * immediately after the 4 byte TS header */
2045 /* p[4] is adaptation_field_length minus one */
2049 /* discontinuity indicator found in stream */
2050 b_discontinuity = (p[5]&0x80) ? true : false;
2051 if( b_discontinuity && pid->es->p_pes )
2053 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
2055 /* pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
2059 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
2064 /* Test continuity counter */
2065 /* continuous when (one of this):
2067 * diff == 0 and payload == 0
2068 * diff == 0 and duplicate packet (playload != 0) <- should we
2069 * test the content ?
2071 const int i_diff = ( i_cc - pid->i_cc )&0x0f;
2072 if( b_payload && i_diff == 1 )
2074 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
2078 if( pid->i_cc == 0xff )
2080 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
2084 else if( i_diff != 0 && !b_discontinuity )
2086 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
2087 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
2090 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
2092 /* Small video artifacts are usually better than
2093 * dropping full frames */
2094 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
2099 PCRHandle( p_demux, pid, p_bk );
2101 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
2103 block_Release( p_bk );
2108 if( !pid->b_scrambled != !b_scrambled )
2110 msg_Warn( p_demux, "scrambled state changed on pid %d (%d->%d)",
2111 pid->i_pid, pid->b_scrambled, b_scrambled );
2113 pid->b_scrambled = b_scrambled;
2115 for( int i = 0; i < pid->i_extra_es; i++ )
2117 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2118 pid->extra_es[i]->id, b_scrambled );
2120 es_out_Control( p_demux->out, ES_OUT_SET_ES_SCRAMBLED_STATE,
2121 pid->es->id, b_scrambled );
2124 /* We have to gather it */
2125 p_bk->p_buffer += i_skip;
2126 p_bk->i_buffer -= i_skip;
2130 if( pid->es->p_pes )
2132 ParsePES( p_demux, pid );
2136 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2137 if( p_bk->i_buffer > 6 )
2139 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
2140 if( pid->es->i_pes_size > 0 )
2142 pid->es->i_pes_size += 6;
2145 pid->es->i_pes_gathered += p_bk->i_buffer;
2146 if( pid->es->i_pes_size > 0 &&
2147 pid->es->i_pes_gathered >= pid->es->i_pes_size )
2149 ParsePES( p_demux, pid );
2155 if( pid->es->p_pes == NULL )
2157 /* msg_Dbg( p_demux, "broken packet" ); */
2158 block_Release( p_bk );
2162 block_ChainLastAppend( &pid->es->pp_last, p_bk );
2163 pid->es->i_pes_gathered += p_bk->i_buffer;
2164 if( pid->es->i_pes_size > 0 &&
2165 pid->es->i_pes_gathered >= pid->es->i_pes_size )
2167 ParsePES( p_demux, pid );
2176 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
2178 es_format_t *fmt = &pid->es->fmt;
2180 switch( i_stream_type )
2182 case 0x01: /* MPEG-1 video */
2183 case 0x02: /* MPEG-2 video */
2184 case 0x80: /* MPEG-2 MOTO video */
2185 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MPGV );
2187 case 0x03: /* MPEG-1 audio */
2188 case 0x04: /* MPEG-2 audio */
2189 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MPGA );
2191 case 0x11: /* MPEG4 (audio) LATM */
2192 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
2193 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_MP4A );
2195 case 0x10: /* MPEG4 (video) */
2196 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_MP4V );
2197 pid->es->b_gather = true;
2199 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
2200 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_H264 );
2202 case 0x42: /* CAVS (Chinese AVS) */
2203 es_format_Init( fmt, VIDEO_ES, VLC_CODEC_CAVS );
2206 case 0x81: /* A52 (audio) */
2207 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_A52 );
2209 case 0x82: /* DVD_SPU (sub) */
2210 es_format_Init( fmt, SPU_ES, VLC_CODEC_SPU );
2212 case 0x83: /* LPCM (audio) */
2213 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DVD_LPCM );
2215 case 0x84: /* SDDS (audio) */
2216 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_SDDS );
2218 case 0x85: /* DTS (audio) */
2219 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_DTS );
2221 case 0x87: /* E-AC3 */
2222 es_format_Init( fmt, AUDIO_ES, VLC_CODEC_EAC3 );
2225 case 0x91: /* A52 vls (audio) */
2226 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
2228 case 0x92: /* DVD_SPU vls (sub) */
2229 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
2232 case 0x94: /* SDDS (audio) */
2233 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
2236 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
2237 es_format_Init( fmt, UNKNOWN_ES, 0 );
2238 pid->es->b_gather = true;
2241 case 0x06: /* PES_PRIVATE (fixed later) */
2242 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
2243 case 0xEA: /* Privately managed ES (VC-1) (fixed later */
2245 es_format_Init( fmt, UNKNOWN_ES, 0 );
2249 /* PES packets usually contain truncated frames */
2250 fmt->b_packetized = false;
2252 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
2255 /*****************************************************************************
2256 * MP4 specific functions (IOD parser)
2257 *****************************************************************************/
2258 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
2261 unsigned int i_len = 0;
2267 i_len = ( i_len << 7 ) + ( i_b&0x7f );
2269 } while( i_b&0x80 );
2274 static int IODGetByte( int *pi_data, uint8_t **pp_data )
2278 const int i_b = **pp_data;
2286 static int IODGetWord( int *pi_data, uint8_t **pp_data )
2288 const int i1 = IODGetByte( pi_data, pp_data );
2289 const int i2 = IODGetByte( pi_data, pp_data );
2290 return( ( i1 << 8 ) | i2 );
2293 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
2295 const int i1 = IODGetByte( pi_data, pp_data );
2296 const int i2 = IODGetByte( pi_data, pp_data );
2297 const int i3 = IODGetByte( pi_data, pp_data );
2299 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
2302 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
2304 const uint32_t i1 = IODGetWord( pi_data, pp_data );
2305 const uint32_t i2 = IODGetWord( pi_data, pp_data );
2306 return( ( i1 << 16 ) | i2 );
2309 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
2314 i_url_len = IODGetByte( pi_data, pp_data );
2315 url = malloc( i_url_len + 1 );
2316 if( !url ) return NULL;
2317 for( int i = 0; i < i_url_len; i++ )
2319 url[i] = IODGetByte( pi_data, pp_data );
2321 url[i_url_len] = '\0';
2325 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2327 iod_descriptor_t *p_iod;
2329 uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
2333 p_iod = malloc( sizeof( iod_descriptor_t ) );
2334 if( !p_iod ) return NULL;
2335 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
2337 ts_debug( "\n************ IOD ************" );
2339 for( int i = 0; i < 255; i++ )
2341 p_iod->es_descr[i].b_ok = 0;
2350 byte1 = IODGetByte( &i_data, &p_data );
2351 byte2 = IODGetByte( &i_data, &p_data );
2352 byte3 = IODGetByte( &i_data, &p_data );
2353 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2355 p_iod->i_iod_label_scope = 0x11;
2356 p_iod->i_iod_label = byte1;
2359 else //correct implementation of the IOD_descriptor
2361 p_iod->i_iod_label_scope = byte1;
2362 p_iod->i_iod_label = byte2;
2366 ts_debug( "\n* iod_label:%d", p_iod->i_iod_label );
2367 ts_debug( "\n* ===========" );
2368 ts_debug( "\n* tag:0x%x", i_iod_tag );
2370 if( i_iod_tag != 0x02 )
2372 ts_debug( "\n ERR: tag %02x != 0x02", i_iod_tag );
2376 i_iod_length = IODDescriptorLength( &i_data, &p_data );
2377 ts_debug( "\n* length:%d", i_iod_length );
2378 if( i_iod_length > i_data )
2380 i_iod_length = i_data;
2383 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
2384 i_flags = IODGetByte( &i_data, &p_data );
2385 p_iod->i_od_id |= i_flags >> 6;
2386 b_url = ( i_flags >> 5 )&0x01;
2387 ts_debug( "\n* od_id:%d", p_iod->i_od_id );
2388 ts_debug( "\n* url flag:%d", b_url );
2389 ts_debug( "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2392 p_iod->psz_url = IODGetURL( &i_data, &p_data );
2393 ts_debug( "\n* url string:%s", p_iod->psz_url );
2394 ts_debug( "\n*****************************\n" );
2399 p_iod->psz_url = NULL;
2402 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
2403 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
2404 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
2405 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
2406 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
2407 ts_debug( "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
2408 ts_debug( "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
2409 ts_debug( "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
2410 ts_debug( "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
2411 ts_debug( "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
2413 while( i_data > 0 && i_es_index < 255)
2415 int i_tag, i_length;
2417 uint8_t *p_data_sav;
2419 i_tag = IODGetByte( &i_data, &p_data );
2420 i_length = IODDescriptorLength( &i_data, &p_data );
2422 i_data_sav = i_data;
2423 p_data_sav = p_data;
2431 #define es_descr p_iod->es_descr[i_es_index]
2432 int i_decoderConfigDescr_length;
2433 ts_debug( "\n* - ES_Descriptor length:%d", i_length );
2436 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
2437 i_flags = IODGetByte( &i_data, &p_data );
2438 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2439 b_url = ( i_flags >> 6 )&0x01;
2440 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2441 es_descr.i_streamPriority = i_flags & 0x1f;
2442 ts_debug( "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
2443 ts_debug( "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
2444 ts_debug( "\n* * streamPriority:%d", es_descr.i_streamPriority );
2445 if( es_descr.b_streamDependenceFlag )
2447 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
2448 ts_debug( "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
2453 es_descr.psz_url = IODGetURL( &i_data, &p_data );
2454 ts_debug( "\n* url string:%s", es_descr.psz_url );
2458 es_descr.psz_url = NULL;
2461 if( es_descr.b_OCRStreamFlag )
2463 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
2464 ts_debug( "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
2467 if( IODGetByte( &i_data, &p_data ) != 0x04 )
2469 ts_debug( "\n* ERR missing DecoderConfigDescr" );
2473 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2474 ts_debug( "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
2475 #define dec_descr es_descr.dec_descr
2476 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
2477 i_flags = IODGetByte( &i_data, &p_data );
2478 dec_descr.i_streamType = i_flags >> 2;
2479 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
2480 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
2481 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
2482 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
2483 ts_debug( "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
2484 ts_debug( "\n* * streamType:0x%x", dec_descr.i_streamType );
2485 ts_debug( "\n* * upStream:%d", dec_descr.b_upStream );
2486 ts_debug( "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
2487 ts_debug( "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
2488 ts_debug( "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
2489 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
2491 dec_descr.i_extra = IODDescriptorLength( &i_data, &p_data );
2492 if( dec_descr.i_extra > 0 )
2494 dec_descr.p_extra = xmalloc( dec_descr.i_extra );
2496 for( int i = 0; i < dec_descr.i_extra; i++ )
2498 dec_descr.p_extra[i] = IODGetByte( &i_data, &p_data );
2503 dec_descr.i_extra = 0;
2504 dec_descr.p_extra = NULL;
2509 int i_SLConfigDescr_length;
2512 if( IODGetByte( &i_data, &p_data ) != 0x06 )
2514 ts_debug( "\n* ERR missing SLConfigDescr" );
2518 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2519 ts_debug( "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
2520 i_predefined = IODGetByte( &i_data, &p_data );
2521 ts_debug( "\n* * i_predefined:0x%x", i_predefined );
2522 switch( i_predefined )
2528 ts_debug( "\n* ERR unsupported SLConfigDescr predefined" );
2536 ts_debug( "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
2540 p_data = p_data_sav + i_length;
2541 i_data = i_data_sav - i_length;
2544 ts_debug( "\n*****************************\n" );
2548 static void IODFree( iod_descriptor_t *p_iod )
2550 if( p_iod->psz_url )
2552 free( p_iod->psz_url );
2553 p_iod->psz_url = NULL;
2558 for( int i = 0; i < 255; i++ )
2560 #define es_descr p_iod->es_descr[i]
2563 if( es_descr.psz_url )
2565 free( es_descr.psz_url );
2566 es_descr.psz_url = NULL;
2570 free( es_descr.dec_descr.p_extra );
2571 es_descr.dec_descr.p_extra = NULL;
2572 es_descr.dec_descr.i_extra = 0;
2581 /****************************************************************************
2582 ****************************************************************************
2583 ** libdvbpsi callbacks
2584 ****************************************************************************
2585 ****************************************************************************/
2586 static bool ProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2588 demux_sys_t *p_sys = p_demux->p_sys;
2590 if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
2591 p_sys->i_current_program == 0 )
2593 if( p_sys->i_current_program == i_pgrm )
2596 if( p_sys->programs_list.i_count != 0 )
2598 for( int i = 0; i < p_sys->programs_list.i_count; i++ )
2600 if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
2607 static void ValidateDVBMeta( demux_t *p_demux, int i_pid )
2609 demux_sys_t *p_sys = p_demux->p_sys;
2611 if( !p_sys->b_dvb_meta || ( i_pid != 0x11 && i_pid != 0x12 && i_pid != 0x14 ) )
2614 msg_Warn( p_demux, "Switching to non DVB mode" );
2616 /* This doesn't look like a DVB stream so don't try
2617 * parsing the SDT/EDT/TDT */
2619 for( int i = 0x11; i <= 0x14; i++ )
2621 if( i == 0x13 ) continue;
2622 ts_pid_t *p_pid = &p_sys->pid[i];
2625 dvbpsi_DetachDemux( p_pid->psi->handle );
2628 p_pid->b_valid = false;
2630 SetPIDFilter( p_demux, i, false );
2632 p_sys->b_dvb_meta = false;
2635 #include "dvb-text.h"
2637 static char *EITConvertToUTF8( const unsigned char *psz_instring,
2642 return FromCharset( "ISO_8859-1", psz_instring, i_length );
2643 return vlc_from_EIT( psz_instring, i_length );
2646 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2648 demux_sys_t *p_sys = p_demux->p_sys;
2649 ts_pid_t *sdt = &p_sys->pid[0x11];
2650 dvbpsi_sdt_service_t *p_srv;
2652 msg_Dbg( p_demux, "SDTCallBack called" );
2654 if( sdt->psi->i_sdt_version != -1 &&
2655 ( !p_sdt->b_current_next ||
2656 p_sdt->i_version == sdt->psi->i_sdt_version ) )
2658 dvbpsi_DeleteSDT( p_sdt );
2662 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2664 p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
2665 p_sdt->i_network_id );
2667 p_sys->b_broken_charset = false;
2669 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2672 dvbpsi_descriptor_t *p_dr;
2674 const char *psz_type = NULL;
2675 const char *psz_status = NULL;
2677 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
2678 "running=%d free_ca=%d",
2679 p_srv->i_service_id, p_srv->b_eit_schedule,
2680 p_srv->b_eit_present, p_srv->i_running_status,
2683 p_meta = vlc_meta_New();
2684 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2686 if( p_dr->i_tag == 0x48 )
2688 static const char *ppsz_type[17] = {
2690 "Digital television service",
2691 "Digital radio sound service",
2693 "NVOD reference service",
2694 "NVOD time-shifted service",
2697 "SECAM coded signal",
2700 "NTSC coded signal",
2701 "Data broadcast service",
2702 "Reserved for Common Interface Usage",
2703 "RCS Map (see EN 301 790 [35])",
2704 "RCS FLS (see EN 301 790 [35])",
2707 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2711 /* Workarounds for broadcasters with broken EPG */
2713 if( p_sdt->i_network_id == 133 )
2714 p_sys->b_broken_charset = true; /* SKY DE & BetaDigital use ISO8859-1 */
2716 /* List of providers using ISO8859-1 */
2717 static const char ppsz_broken_providers[][8] = {
2718 "CSAT", /* CanalSat FR */
2719 "GR1", /* France televisions */
2721 "MR5", /* France 2/M6 HD */
2724 for( int i = 0; *ppsz_broken_providers[i]; i++ )
2726 const size_t i_length = strlen(ppsz_broken_providers[i]);
2727 if( pD->i_service_provider_name_length == i_length &&
2728 !strncmp( (char *)pD->i_service_provider_name, ppsz_broken_providers[i], i_length ) )
2729 p_sys->b_broken_charset = true;
2732 /* FIXME: Digital+ ES also uses ISO8859-1 */
2734 str1 = EITConvertToUTF8(pD->i_service_provider_name,
2735 pD->i_service_provider_name_length,
2736 p_sys->b_broken_charset );
2737 str2 = EITConvertToUTF8(pD->i_service_name,
2738 pD->i_service_name_length,
2739 p_sys->b_broken_charset );
2741 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
2742 pD->i_service_type, str1, str2 );
2744 vlc_meta_SetTitle( p_meta, str2 );
2745 vlc_meta_SetPublisher( p_meta, str1 );
2746 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2747 psz_type = ppsz_type[pD->i_service_type];
2753 if( p_srv->i_running_status >= 0x01 && p_srv->i_running_status <= 0x04 )
2755 static const char *ppsz_status[5] = {
2758 "Starts in a few seconds",
2762 psz_status = ppsz_status[p_srv->i_running_status];
2766 vlc_meta_AddExtra( p_meta, "Type", psz_type );
2768 vlc_meta_AddExtra( p_meta, "Status", psz_status );
2770 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2771 p_srv->i_service_id, p_meta );
2772 vlc_meta_Delete( p_meta );
2775 sdt->psi->i_sdt_version = p_sdt->i_version;
2776 dvbpsi_DeleteSDT( p_sdt );
2779 /* i_year: year - 1900 i_month: 0-11 i_mday: 1-31 i_hour: 0-23 i_minute: 0-59 i_second: 0-59 */
2780 static int64_t vlc_timegm( int i_year, int i_month, int i_mday, int i_hour, int i_minute, int i_second )
2782 static const int pn_day[12+1] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 };
2786 i_month < 0 || i_month > 11 || i_mday < 1 || i_mday > 31 ||
2787 i_hour < 0 || i_hour > 23 || i_minute < 0 || i_minute > 59 || i_second < 0 || i_second > 59 )
2790 /* Count the number of days */
2791 i_day = 365 * (i_year-70) + pn_day[i_month] + i_mday - 1;
2792 #define LEAP(y) ( ((y)%4) == 0 && (((y)%100) != 0 || ((y)%400) == 0) ? 1 : 0)
2793 for( int i = 70; i < i_year; i++ )
2794 i_day += LEAP(1900+i);
2796 i_day += LEAP(1900+i_year);
2799 return ((24*i_day + i_hour)*60 + i_minute)*60 + i_second;
2802 static void EITDecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2804 const int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2805 const int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2806 const int c = ( mp == 14 || mp == 15 ) ? 1 : 0;
2808 *p_y = 1900 + yp + c*1;
2809 *p_m = mp - 1 - c*12;
2810 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2812 #define CVT_FROM_BCD(v) ((((v) >> 4)&0xf)*10 + ((v)&0xf))
2813 static int64_t EITConvertStartTime( uint64_t i_date )
2815 const int i_mjd = i_date >> 24;
2816 const int i_hour = CVT_FROM_BCD(i_date >> 16);
2817 const int i_minute = CVT_FROM_BCD(i_date >> 8);
2818 const int i_second = CVT_FROM_BCD(i_date );
2823 /* if all 40 bits are 1, the start is unknown */
2824 if( i_date == UINT64_C(0xffffffffff) )
2827 EITDecodeMjd( i_mjd, &i_year, &i_month, &i_day );
2828 return vlc_timegm( i_year - 1900, i_month - 1, i_day, i_hour, i_minute, i_second );
2830 static int EITConvertDuration( uint32_t i_duration )
2832 return CVT_FROM_BCD(i_duration >> 16) * 3600 +
2833 CVT_FROM_BCD(i_duration >> 8 ) * 60 +
2834 CVT_FROM_BCD(i_duration );
2838 static void TDTCallBack( demux_t *p_demux, dvbpsi_tot_t *p_tdt )
2840 demux_sys_t *p_sys = p_demux->p_sys;
2842 p_sys->i_tdt_delta = CLOCK_FREQ * EITConvertStartTime( p_tdt->i_utc_time )
2844 dvbpsi_DeleteTOT(p_tdt);
2848 static void EITCallBack( demux_t *p_demux,
2849 dvbpsi_eit_t *p_eit, bool b_current_following )
2851 demux_sys_t *p_sys = p_demux->p_sys;
2852 dvbpsi_eit_event_t *p_evt;
2855 msg_Dbg( p_demux, "EITCallBack called" );
2856 if( !p_eit->b_current_next )
2858 dvbpsi_DeleteEIT( p_eit );
2862 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
2863 "ts_id=%d network_id=%d segment_last_section_number=%d "
2865 p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
2866 p_eit->i_ts_id, p_eit->i_network_id,
2867 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2869 p_epg = vlc_epg_New( NULL );
2870 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2872 dvbpsi_descriptor_t *p_dr;
2873 char *psz_name = NULL;
2874 char *psz_text = NULL;
2875 char *psz_extra = strdup("");
2879 i_start = EITConvertStartTime( p_evt->i_start_time );
2880 i_duration = EITConvertDuration( p_evt->i_duration );
2882 msg_Dbg( p_demux, " * event id=%d start_time:%d duration=%d "
2883 "running=%d free_ca=%d",
2884 p_evt->i_event_id, (int)i_start, (int)i_duration,
2885 p_evt->i_running_status, p_evt->b_free_ca );
2887 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2889 if( p_dr->i_tag == 0x4d )
2891 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2893 /* Only take first description, as we don't handle language-info
2895 if( pE && psz_name == NULL)
2897 psz_name = EITConvertToUTF8( pE->i_event_name, pE->i_event_name_length,
2898 p_sys->b_broken_charset );
2899 psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
2900 p_sys->b_broken_charset );
2901 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
2902 pE->i_iso_639_code, psz_name, psz_text );
2905 else if( p_dr->i_tag == 0x4e )
2907 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
2910 msg_Dbg( p_demux, " - extended event lang=%3.3s [%d/%d]",
2912 pE->i_descriptor_number, pE->i_last_descriptor_number );
2914 if( pE->i_text_length > 0 )
2916 char *psz_text = EITConvertToUTF8( pE->i_text, pE->i_text_length,
2917 p_sys->b_broken_charset );
2920 msg_Dbg( p_demux, " - text='%s'", psz_text );
2922 psz_extra = xrealloc( psz_extra,
2923 strlen(psz_extra) + strlen(psz_text) + 1 );
2924 strcat( psz_extra, psz_text );
2929 for( int i = 0; i < pE->i_entry_count; i++ )
2931 char *psz_dsc = EITConvertToUTF8( pE->i_item_description[i],
2932 pE->i_item_description_length[i],
2933 p_sys->b_broken_charset );
2934 char *psz_itm = EITConvertToUTF8( pE->i_item[i], pE->i_item_length[i],
2935 p_sys->b_broken_charset );
2937 if( psz_dsc && psz_itm )
2939 msg_Dbg( p_demux, " - desc='%s' item='%s'", psz_dsc, psz_itm );
2941 psz_extra = xrealloc( psz_extra,
2942 strlen(psz_extra) + strlen(psz_dsc) +
2943 strlen(psz_itm) + 3 + 1 );
2944 strcat( psz_extra, "(" );
2945 strcat( psz_extra, psz_dsc );
2946 strcat( psz_extra, " " );
2947 strcat( psz_extra, psz_itm );
2948 strcat( psz_extra, ")" );
2958 msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
2964 vlc_epg_AddEvent( p_epg, i_start, i_duration, psz_name, psz_text,
2965 *psz_extra ? psz_extra : NULL );
2967 /* Update "now playing" field */
2968 if( p_evt->i_running_status == 0x04 && i_start > 0 )
2969 vlc_epg_SetCurrent( p_epg, i_start );
2976 if( p_epg->i_event > 0 )
2978 if( b_current_following &&
2979 ( p_sys->i_current_program == -1 ||
2980 p_sys->i_current_program == p_eit->i_service_id ) )
2982 p_sys->i_dvb_length = 0;
2983 p_sys->i_dvb_start = 0;
2985 if( p_epg->p_current )
2987 p_sys->i_dvb_start = CLOCK_FREQ * p_epg->p_current->i_start;
2988 p_sys->i_dvb_length = CLOCK_FREQ * p_epg->p_current->i_duration;
2991 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_EPG, p_eit->i_service_id, p_epg );
2993 vlc_epg_Delete( p_epg );
2995 dvbpsi_DeleteEIT( p_eit );
2997 static void EITCallBackCurrentFollowing( demux_t *p_demux, dvbpsi_eit_t *p_eit )
2999 EITCallBack( p_demux, p_eit, true );
3001 static void EITCallBackSchedule( demux_t *p_demux, dvbpsi_eit_t *p_eit )
3003 EITCallBack( p_demux, p_eit, false );
3006 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
3007 uint8_t i_table_id, uint16_t i_extension )
3010 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3011 i_table_id, i_table_id, i_extension, i_extension );
3013 if( p_demux->p_sys->pid[0].psi->i_pat_version != -1 && i_table_id == 0x42 )
3015 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3016 i_table_id, i_table_id, i_extension, i_extension );
3018 dvbpsi_AttachSDT( h, i_table_id, i_extension,
3019 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
3021 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3022 ( i_table_id == 0x4e || /* Current/Following */
3023 (i_table_id >= 0x50 && i_table_id <= 0x5f) ) ) /* Schedule */
3025 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3026 i_table_id, i_table_id, i_extension, i_extension );
3028 dvbpsi_eit_callback cb = i_table_id == 0x4e ?
3029 (dvbpsi_eit_callback)EITCallBackCurrentFollowing :
3030 (dvbpsi_eit_callback)EITCallBackSchedule;
3031 dvbpsi_AttachEIT( h, i_table_id, i_extension, cb, p_demux );
3033 else if( p_demux->p_sys->pid[0x11].psi->i_sdt_version != -1 &&
3034 i_table_id == 0x70 ) /* TDT */
3036 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
3037 i_table_id, i_table_id, i_extension, i_extension );
3038 dvbpsi_AttachTOT( h, i_table_id, i_extension,
3039 (dvbpsi_tot_callback)TDTCallBack, p_demux);
3043 /*****************************************************************************
3044 * PMT callback and helpers
3045 *****************************************************************************/
3046 static dvbpsi_descriptor_t *PMTEsFindDescriptor( const dvbpsi_pmt_es_t *p_es,
3049 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
3050 while( p_dr && ( p_dr->i_tag != i_tag ) )
3051 p_dr = p_dr->p_next;
3054 static bool PMTEsHasRegistration( demux_t *p_demux,
3055 const dvbpsi_pmt_es_t *p_es,
3056 const char *psz_tag )
3058 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x05 );
3062 if( p_dr->i_length < 4 )
3064 msg_Warn( p_demux, "invalid Registration Descriptor" );
3068 assert( strlen(psz_tag) == 4 );
3069 return !memcmp( p_dr->p_data, psz_tag, 4 );
3071 static void PMTSetupEsISO14496( demux_t *p_demux, ts_pid_t *pid,
3072 const ts_prg_psi_t *prg, const dvbpsi_pmt_es_t *p_es )
3074 es_format_t *p_fmt = &pid->es->fmt;
3076 /* MPEG-4 stream: search SL_DESCRIPTOR */
3077 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x1f );
3079 if( p_dr && p_dr->i_length == 2 )
3081 const int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
3083 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
3085 pid->es->p_mpeg4desc = NULL;
3087 for( int i = 0; i < 255; i++ )
3089 iod_descriptor_t *iod = prg->iod;
3091 if( iod->es_descr[i].b_ok && iod->es_descr[i].i_es_id == i_es_id )
3093 pid->es->p_mpeg4desc = &iod->es_descr[i];
3098 if( !pid->es->p_mpeg4desc )
3100 msg_Err( p_demux, "MPEG-4 descriptor not found" );
3104 const decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
3105 if( dcd->i_streamType == 0x04 ) /* VisualStream */
3107 p_fmt->i_cat = VIDEO_ES;
3108 switch( dcd->i_objectTypeIndication )
3110 case 0x0B: /* mpeg4 sub */
3111 p_fmt->i_cat = SPU_ES;
3112 p_fmt->i_codec = VLC_CODEC_SUBT;
3115 case 0x20: /* mpeg4 */
3116 p_fmt->i_codec = VLC_CODEC_MP4V;
3118 case 0x21: /* h264 */
3119 p_fmt->i_codec = VLC_CODEC_H264;
3126 case 0x65: /* mpeg2 */
3127 p_fmt->i_codec = VLC_CODEC_MPGV;
3129 case 0x6a: /* mpeg1 */
3130 p_fmt->i_codec = VLC_CODEC_MPGV;
3132 case 0x6c: /* mpeg1 */
3133 p_fmt->i_codec = VLC_CODEC_JPEG;
3136 p_fmt->i_cat = UNKNOWN_ES;
3140 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
3142 p_fmt->i_cat = AUDIO_ES;
3143 switch( dcd->i_objectTypeIndication )
3145 case 0x40: /* mpeg4 */
3146 p_fmt->i_codec = VLC_CODEC_MP4A;
3150 case 0x68: /* mpeg2 aac */
3151 p_fmt->i_codec = VLC_CODEC_MP4A;
3153 case 0x69: /* mpeg2 */
3154 p_fmt->i_codec = VLC_CODEC_MPGA;
3156 case 0x6b: /* mpeg1 */
3157 p_fmt->i_codec = VLC_CODEC_MPGA;
3160 p_fmt->i_cat = UNKNOWN_ES;
3166 p_fmt->i_cat = UNKNOWN_ES;
3169 if( p_fmt->i_cat != UNKNOWN_ES )
3171 p_fmt->i_extra = dcd->i_extra;
3172 if( p_fmt->i_extra > 0 )
3174 p_fmt->p_extra = malloc( p_fmt->i_extra );
3175 if( p_fmt->p_extra )
3176 memcpy( p_fmt->p_extra, dcd->p_extra, p_fmt->i_extra );
3189 } ts_teletext_page_t;
3191 static void PMTSetupEsTeletext( demux_t *p_demux, ts_pid_t *pid,
3192 const dvbpsi_pmt_es_t *p_es )
3194 es_format_t *p_fmt = &pid->es->fmt;
3196 ts_teletext_page_t p_page[2 * 64 + 20];
3197 unsigned i_page = 0;
3199 /* Gather pages information */
3200 #if defined _DVBPSI_DR_56_H_ && \
3201 defined DVBPSI_VERSION && DVBPSI_VERSION_INT > ((0<<16)+(1<<8)+5)
3202 for( unsigned i_tag_idx = 0; i_tag_idx < 2; i_tag_idx++ )
3204 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, i_tag_idx == 0 ? 0x46 : 0x56 );
3208 dvbpsi_teletext_dr_t *p_sub = dvbpsi_DecodeTeletextDr( p_dr );
3212 for( int i = 0; i < p_sub->i_pages_number; i++ )
3214 const dvbpsi_teletextpage_t *p_src = &p_sub->p_pages[i];
3216 if( p_src->i_teletext_type >= 0x06 )
3219 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3221 ts_teletext_page_t *p_dst = &p_page[i_page++];
3223 p_dst->i_type = p_src->i_teletext_type;
3224 p_dst->i_magazine = p_src->i_teletext_magazine_number
3225 ? p_src->i_teletext_magazine_number : 8;
3226 p_dst->i_page = p_src->i_teletext_page_number;
3227 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3232 #ifdef _DVBPSI_DR_59_H_
3233 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3236 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3237 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3239 dvbpsi_subtitle_t *p_src = &p_sub->p_subtitle[i];
3241 if( p_src->i_subtitling_type < 0x01 || p_src->i_subtitling_type > 0x03 )
3244 assert( i_page < sizeof(p_page)/sizeof(*p_page) );
3246 ts_teletext_page_t *p_dst = &p_page[i_page++];
3248 switch( p_src->i_subtitling_type )
3251 p_dst->i_type = 0x02;
3254 p_dst->i_type = 0x03;
3257 /* FIXME check if it is the right split */
3258 p_dst->i_magazine = (p_src->i_composition_page_id >> 8)
3259 ? (p_src->i_composition_page_id >> 8) : 8;
3260 p_dst->i_page = p_src->i_composition_page_id & 0xff;
3261 memcpy( p_dst->p_iso639, p_src->i_iso6392_language_code, 3 );
3267 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_TELETEXT );
3269 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3271 p_fmt->subs.teletext.i_magazine = -1;
3272 p_fmt->subs.teletext.i_page = 0;
3273 p_fmt->psz_description = strdup( vlc_gettext(ppsz_teletext_type[1]) );
3275 dvbpsi_descriptor_t *p_dr;
3276 p_dr = PMTEsFindDescriptor( p_es, 0x46 );
3278 p_dr = PMTEsFindDescriptor( p_es, 0x56 );
3280 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3282 /* Descriptor pass-through */
3283 p_fmt->p_extra = malloc( p_dr->i_length );
3284 if( p_fmt->p_extra )
3286 p_fmt->i_extra = p_dr->i_length;
3287 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3293 for( unsigned i = 0; i < i_page; i++ )
3304 p_es = malloc( sizeof(*p_es) );
3308 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3309 free( p_es->fmt.psz_language );
3310 free( p_es->fmt.psz_description );
3311 p_es->fmt.psz_language = NULL;
3312 p_es->fmt.psz_description = NULL;
3316 p_es->i_pes_size = 0;
3317 p_es->i_pes_gathered = 0;
3318 p_es->pp_last = &p_es->p_pes;
3319 p_es->p_mpeg4desc = NULL;
3320 p_es->b_gather = false;
3322 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3326 const ts_teletext_page_t *p = &p_page[i];
3327 p_es->fmt.i_priority = (p->i_type == 0x02 || p->i_type == 0x05) ? 0 : -1;
3328 p_es->fmt.psz_language = strndup( p->p_iso639, 3 );
3329 p_es->fmt.psz_description = strdup(vlc_gettext(ppsz_teletext_type[p->i_type]));
3330 p_es->fmt.subs.teletext.i_magazine = p->i_magazine;
3331 p_es->fmt.subs.teletext.i_page = p->i_page;
3334 " * ttxt type=%s lan=%s page=%d%02x",
3335 p_es->fmt.psz_description,
3336 p_es->fmt.psz_language,
3337 p->i_magazine, p->i_page );
3341 static void PMTSetupEsDvbSubtitle( demux_t *p_demux, ts_pid_t *pid,
3342 const dvbpsi_pmt_es_t *p_es )
3344 es_format_t *p_fmt = &pid->es->fmt;
3346 es_format_Init( p_fmt, SPU_ES, VLC_CODEC_DVBS );
3348 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3350 #ifdef _DVBPSI_DR_59_H_
3351 dvbpsi_subtitling_dr_t *p_sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3352 for( int i = 0; p_sub && i < p_sub->i_subtitles_number; i++ )
3354 const int i_type = p_sub->p_subtitle[i].i_subtitling_type;
3355 if( ( i_type >= 0x10 && i_type <= 0x14 ) ||
3356 ( i_type >= 0x20 && i_type <= 0x24 ) )
3361 if( !p_demux->p_sys->b_split_es || i_page <= 0 )
3363 p_fmt->subs.dvb.i_id = -1;
3364 p_fmt->psz_description = strdup( _("DVB subtitles") );
3366 if( !p_demux->p_sys->b_split_es && p_dr && p_dr->i_length > 0 )
3368 /* Descriptor pass-through */
3369 p_fmt->p_extra = malloc( p_dr->i_length );
3370 if( p_fmt->p_extra )
3372 p_fmt->i_extra = p_dr->i_length;
3373 memcpy( p_fmt->p_extra, p_dr->p_data, p_dr->i_length );
3379 #ifdef _DVBPSI_DR_59_H_
3380 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3391 p_es = malloc( sizeof(*p_es) );
3395 es_format_Copy( &p_es->fmt, &pid->es->fmt );
3396 free( p_es->fmt.psz_language );
3397 free( p_es->fmt.psz_description );
3398 p_es->fmt.psz_language = NULL;
3399 p_es->fmt.psz_description = NULL;
3403 p_es->i_pes_size = 0;
3404 p_es->i_pes_gathered = 0;
3405 p_es->pp_last = &p_es->p_pes;
3406 p_es->p_mpeg4desc = NULL;
3407 p_es->b_gather = false;
3409 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3413 const dvbpsi_subtitle_t *p = &p_sub->p_subtitle[i];
3414 p_es->fmt.psz_language = strndup( (char *)p->i_iso6392_language_code, 3 );
3415 switch( p->i_subtitling_type )
3417 case 0x10: /* unspec. */
3418 case 0x11: /* 4:3 */
3419 case 0x12: /* 16:9 */
3420 case 0x13: /* 2.21:1 */
3421 case 0x14: /* HD monitor */
3422 p_es->fmt.psz_description = strdup( _("DVB subtitles") );
3424 case 0x20: /* Hearing impaired unspec. */
3425 case 0x21: /* h.i. 4:3 */
3426 case 0x22: /* h.i. 16:9 */
3427 case 0x23: /* h.i. 2.21:1 */
3428 case 0x24: /* h.i. HD monitor */
3429 p_es->fmt.psz_description = strdup( _("DVB subtitles: hearing impaired") );
3436 p_es->fmt.subs.dvb.i_id = ( p->i_composition_page_id << 0 ) |
3437 ( p->i_ancillary_page_id << 16 );
3442 static void PMTSetupEs0x06( demux_t *p_demux, ts_pid_t *pid,
3443 const dvbpsi_pmt_es_t *p_es )
3445 es_format_t *p_fmt = &pid->es->fmt;
3447 if( PMTEsHasRegistration( p_demux, p_es, "AC-3" ) ||
3448 PMTEsFindDescriptor( p_es, 0x6a ) ||
3449 PMTEsFindDescriptor( p_es, 0x81 ) )
3451 p_fmt->i_cat = AUDIO_ES;
3452 p_fmt->i_codec = VLC_CODEC_A52;
3454 else if( PMTEsFindDescriptor( p_es, 0x7a ) )
3456 /* DVB with stream_type 0x06 (ETS EN 300 468) */
3457 p_fmt->i_cat = AUDIO_ES;
3458 p_fmt->i_codec = VLC_CODEC_EAC3;
3460 else if( PMTEsHasRegistration( p_demux, p_es, "DTS1" ) ||
3461 PMTEsHasRegistration( p_demux, p_es, "DTS2" ) ||
3462 PMTEsHasRegistration( p_demux, p_es, "DTS3" ) ||
3463 PMTEsFindDescriptor( p_es, 0x73 ) )
3465 /*registration descriptor(ETSI TS 101 154 Annex F)*/
3466 p_fmt->i_cat = AUDIO_ES;
3467 p_fmt->i_codec = VLC_CODEC_DTS;
3469 else if( PMTEsHasRegistration( p_demux, p_es, "BSSD" ) )
3471 p_fmt->i_cat = AUDIO_ES;
3472 p_fmt->b_packetized = true;
3473 p_fmt->i_codec = VLC_CODEC_302M;
3477 /* Subtitle/Teletext/VBI fallbacks */
3478 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x59 );
3480 #ifdef _DVBPSI_DR_59_H_
3481 dvbpsi_subtitling_dr_t *p_sub;
3482 if( p_dr && ( p_sub = dvbpsi_DecodeSubtitlingDr( p_dr ) ) )
3484 for( int i = 0; i < p_sub->i_subtitles_number; i++ )
3486 if( p_fmt->i_cat != UNKNOWN_ES )
3489 switch( p_sub->p_subtitle[i].i_subtitling_type )
3491 case 0x01: /* EBU Teletext subtitles */
3492 case 0x02: /* Associated EBU Teletext */
3493 case 0x03: /* VBI data */
3494 PMTSetupEsTeletext( p_demux, pid, p_es );
3496 case 0x10: /* DVB Subtitle (normal) with no monitor AR critical */
3497 case 0x11: /* ... on 4:3 AR monitor */
3498 case 0x12: /* ... on 16:9 AR monitor */
3499 case 0x13: /* ... on 2.21:1 AR monitor */
3500 case 0x14: /* ... for display on a high definition monitor */
3501 case 0x20: /* DVB Subtitle (impaired) with no monitor AR critical */
3502 case 0x21: /* ... on 4:3 AR monitor */
3503 case 0x22: /* ... on 16:9 AR monitor */
3504 case 0x23: /* ... on 2.21:1 AR monitor */
3505 case 0x24: /* ... for display on a high definition monitor */
3506 PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
3509 msg_Err( p_demux, "Unrecognized DVB subtitle type (0x%x)",
3510 p_sub->p_subtitle[i].i_subtitling_type );
3516 if( p_fmt->i_cat == UNKNOWN_ES && p_dr )
3517 PMTSetupEsDvbSubtitle( p_demux, pid, p_es );
3519 if( p_fmt->i_cat == UNKNOWN_ES &&
3520 ( PMTEsFindDescriptor( p_es, 0x45 ) || /* VBI Data descriptor */
3521 PMTEsFindDescriptor( p_es, 0x46 ) || /* VBI Teletext descriptor */
3522 PMTEsFindDescriptor( p_es, 0x56 ) ) ) /* EBU Teletext descriptor */
3525 PMTSetupEsTeletext( p_demux, pid, p_es );
3529 #ifdef _DVBPSI_DR_52_H_
3530 /* FIXME is it useful ? */
3531 if( PMTEsFindDescriptor( p_es, 0x52 ) )
3533 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x52 );
3534 dvbpsi_stream_identifier_dr_t *p_si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3536 msg_Dbg( p_demux, " * Stream Component Identifier: %d", p_si->i_component_tag );
3541 static void PMTSetupEs0xEA( demux_t *p_demux, ts_pid_t *pid,
3542 const dvbpsi_pmt_es_t *p_es )
3544 /* Registration Descriptor */
3545 if( !PMTEsHasRegistration( p_demux, p_es, "VC-1" ) )
3547 msg_Err( p_demux, "Registration descriptor not found or invalid" );
3551 es_format_t *p_fmt = &pid->es->fmt;
3553 /* registration descriptor for VC-1 (SMPTE rp227) */
3554 p_fmt->i_cat = VIDEO_ES;
3555 p_fmt->i_codec = VLC_CODEC_VC1;
3557 /* XXX With Simple and Main profile the SEQUENCE
3558 * header is modified: video width and height are
3559 * inserted just after the start code as 2 int16_t
3560 * The packetizer will take care of that. */
3563 static void PMTSetupEs0xD1( demux_t *p_demux, ts_pid_t *pid,
3564 const dvbpsi_pmt_es_t *p_es )
3566 /* Registration Descriptor */
3567 if( !PMTEsHasRegistration( p_demux, p_es, "drac" ) )
3569 msg_Err( p_demux, "Registration descriptor not found or invalid" );
3573 es_format_t *p_fmt = &pid->es->fmt;
3575 /* registration descriptor for Dirac
3576 * (backwards compatable with VC-2 (SMPTE Sxxxx:2008)) */
3577 p_fmt->i_cat = VIDEO_ES;
3578 p_fmt->i_codec = VLC_CODEC_DIRAC;
3581 static void PMTSetupEs0xA0( demux_t *p_demux, ts_pid_t *pid,
3582 const dvbpsi_pmt_es_t *p_es )
3584 /* MSCODEC sent by vlc */
3585 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0xa0 );
3586 if( !p_dr || p_dr->i_length < 10 )
3589 "private MSCODEC (vlc) without bih private descriptor" );
3593 es_format_t *p_fmt = &pid->es->fmt;
3594 p_fmt->i_cat = VIDEO_ES;
3595 p_fmt->i_codec = VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
3596 p_dr->p_data[2], p_dr->p_data[3] );
3597 p_fmt->video.i_width = GetWBE( &p_dr->p_data[4] );
3598 p_fmt->video.i_height = GetWBE( &p_dr->p_data[6] );
3599 p_fmt->i_extra = GetWBE( &p_dr->p_data[8] );
3601 if( p_fmt->i_extra > 0 )
3603 p_fmt->p_extra = malloc( p_fmt->i_extra );
3604 if( p_fmt->p_extra )
3605 memcpy( p_fmt->p_extra, &p_dr->p_data[10],
3606 __MIN( p_fmt->i_extra, p_dr->i_length - 10 ) );
3610 /* For such stream we will gather them ourself and don't launch a
3612 * Yes it's ugly but it's the only way to have DIV3 working */
3613 p_fmt->b_packetized = true;
3616 static void PMTSetupEsHDMV( demux_t *p_demux, ts_pid_t *pid,
3617 const dvbpsi_pmt_es_t *p_es )
3619 VLC_UNUSED(p_demux);
3620 es_format_t *p_fmt = &pid->es->fmt;
3622 /* Blu-Ray mapping */
3623 switch( p_es->i_type )
3626 p_fmt->i_cat = AUDIO_ES;
3627 p_fmt->i_codec = VLC_CODEC_BD_LPCM;
3630 case 0x85: /* DTS-HD High resolution audio */
3631 case 0x86: /* DTS-HD Master audio */
3632 case 0xA2: /* Secondary DTS audio */
3633 p_fmt->i_cat = AUDIO_ES;
3634 p_fmt->i_codec = VLC_CODEC_DTS;
3637 case 0x83: /* TrueHD AC3 */
3638 p_fmt->i_cat = AUDIO_ES;
3639 p_fmt->i_codec = VLC_CODEC_TRUEHD;
3642 case 0x84: /* E-AC3 */
3643 case 0xA1: /* Secondary E-AC3 */
3644 p_fmt->i_cat = AUDIO_ES;
3645 p_fmt->i_codec = VLC_CODEC_EAC3;
3647 case 0x90: /* Presentation graphics */
3648 p_fmt->i_cat = SPU_ES;
3649 p_fmt->i_codec = VLC_CODEC_BD_PG;
3651 case 0x91: /* Interactive graphics */
3652 case 0x92: /* Subtitle */
3658 static void PMTSetupEsRegistration( demux_t *p_demux, ts_pid_t *pid,
3659 const dvbpsi_pmt_es_t *p_es )
3665 vlc_fourcc_t i_codec;
3667 { "AC-3", AUDIO_ES, VLC_CODEC_A52 },
3668 { "DTS1", AUDIO_ES, VLC_CODEC_DTS },
3669 { "DTS2", AUDIO_ES, VLC_CODEC_DTS },
3670 { "DTS3", AUDIO_ES, VLC_CODEC_DTS },
3671 { "BSSD", AUDIO_ES, VLC_CODEC_302M },
3672 { "VC-1", VIDEO_ES, VLC_CODEC_VC1 },
3673 { "drac", VIDEO_ES, VLC_CODEC_DIRAC },
3674 { "", UNKNOWN_ES, 0 }
3676 es_format_t *p_fmt = &pid->es->fmt;
3678 for( int i = 0; p_regs[i].i_cat != UNKNOWN_ES; i++ )
3680 if( PMTEsHasRegistration( p_demux, p_es, p_regs[i].psz_tag ) )
3682 p_fmt->i_cat = p_regs[i].i_cat;
3683 p_fmt->i_codec = p_regs[i].i_codec;
3689 static void PMTParseEsIso639( demux_t *p_demux, ts_pid_t *pid,
3690 const dvbpsi_pmt_es_t *p_es )
3692 /* get language descriptor */
3693 dvbpsi_descriptor_t *p_dr = PMTEsFindDescriptor( p_es, 0x0a );
3698 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
3701 msg_Err( p_demux, "Failed to decode a ISO 639 descriptor" );
3705 #if defined(DR_0A_API_VER) && (DR_0A_API_VER >= 2)
3706 pid->es->fmt.psz_language = malloc( 4 );
3707 if( pid->es->fmt.psz_language )
3709 memcpy( pid->es->fmt.psz_language,
3710 p_decoded->code[0].iso_639_code, 3 );
3711 pid->es->fmt.psz_language[3] = 0;
3712 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
3714 switch( p_decoded->code[0].i_audio_type )
3717 pid->es->fmt.i_priority = 1; // prioritize normal audio tracks
3718 pid->es->fmt.psz_description = NULL;
3721 pid->es->fmt.psz_description =
3722 strdup(_("clean effects"));
3725 pid->es->fmt.psz_description =
3726 strdup(_("hearing impaired"));
3729 pid->es->fmt.psz_description =
3730 strdup(_("visual impaired commentary"));
3733 msg_Dbg( p_demux, "unknown audio type: %d",
3734 p_decoded->code[0].i_audio_type);
3735 pid->es->fmt.psz_description = NULL;
3738 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
3739 if( pid->es->fmt.i_extra_languages > 0 )
3740 pid->es->fmt.p_extra_languages =
3741 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
3742 pid->es->fmt.i_extra_languages );
3743 if( pid->es->fmt.p_extra_languages )
3745 for( int i = 0; i < pid->es->fmt.i_extra_languages; i++ )
3747 msg_Dbg( p_demux, "bang" );
3748 pid->es->fmt.p_extra_languages[i].psz_language =
3750 if( pid->es->fmt.p_extra_languages[i].psz_language )
3752 memcpy( pid->es->fmt.p_extra_languages[i].psz_language,
3753 p_decoded->code[i+1].iso_639_code, 3 );
3754 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
3756 switch( p_decoded->code[i].i_audio_type )
3759 pid->es->fmt.p_extra_languages[i].psz_description =
3763 pid->es->fmt.p_extra_languages[i].psz_description =
3764 strdup(_("clean effects"));
3767 pid->es->fmt.p_extra_languages[i].psz_description =
3768 strdup(_("hearing impaired"));
3771 pid->es->fmt.p_extra_languages[i].psz_description =
3772 strdup(_("visual impaired commentary"));
3775 msg_Dbg( p_demux, "unknown audio type: %d",
3776 p_decoded->code[i].i_audio_type);
3777 pid->es->fmt.psz_description = NULL;
3784 pid->es->fmt.psz_language = malloc( 4 );
3785 if( pid->es->fmt.psz_language )
3787 memcpy( pid->es->fmt.psz_language,
3788 p_decoded->i_iso_639_code, 3 );
3789 pid->es->fmt.psz_language[3] = 0;
3794 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
3796 demux_sys_t *p_sys = p_demux->p_sys;
3797 dvbpsi_descriptor_t *p_dr;
3798 dvbpsi_pmt_es_t *p_es;
3800 ts_pid_t *pmt = NULL;
3801 ts_prg_psi_t *prg = NULL;
3803 ts_pid_t **pp_clean = NULL;
3805 bool b_hdmv = false;
3807 msg_Dbg( p_demux, "PMTCallBack called" );
3809 /* First find this PMT declared in PAT */
3810 for( int i = 0; i < p_sys->i_pmt; i++ )
3812 for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
3814 const int i_pmt_number = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
3815 if( i_pmt_number != TS_USER_PMT_NUMBER && i_pmt_number == p_pmt->i_program_number )
3817 pmt = p_sys->pmt[i];
3818 prg = p_sys->pmt[i]->psi->prg[i_prg];
3828 msg_Warn( p_demux, "unreferenced program (broken stream)" );
3829 dvbpsi_DeletePMT(p_pmt);
3833 if( prg->i_version != -1 &&
3834 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
3836 dvbpsi_DeletePMT( p_pmt );
3840 /* Clean this program (remove all es) */
3841 for( int i = 0; i < 8192; i++ )
3843 ts_pid_t *pid = &p_sys->pid[i];
3845 if( pid->b_valid && pid->p_owner == pmt->psi &&
3846 pid->i_owner_number == prg->i_number && pid->psi == NULL )
3848 TAB_APPEND( i_clean, pp_clean, pid );
3853 IODFree( prg->iod );
3857 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
3858 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
3859 prg->i_pid_pcr = p_pmt->i_pcr_pid;
3860 prg->i_version = p_pmt->i_version;
3862 ValidateDVBMeta( p_demux, prg->i_pid_pcr );
3863 if( ProgramIsSelected( p_demux, prg->i_number ) )
3865 /* Set demux filter */
3866 SetPIDFilter( p_demux, prg->i_pid_pcr, true );
3869 /* Parse descriptor */
3870 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
3872 if( p_dr->i_tag == 0x1d )
3874 /* We have found an IOD descriptor */
3875 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
3877 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
3879 else if( p_dr->i_tag == 0x9 )
3881 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
3883 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
3885 else if( p_dr->i_tag == 0x05 )
3887 /* Registration Descriptor */
3888 if( p_dr->i_length != 4 )
3890 msg_Warn( p_demux, "invalid Registration Descriptor" );
3894 msg_Dbg( p_demux, " * descriptor : registration %4.4s", p_dr->p_data );
3895 if( !memcmp( p_dr->p_data, "HDMV", 4 ) )
3904 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
3908 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
3910 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
3912 /* Find out if the PID was already declared */
3913 for( int i = 0; i < i_clean; i++ )
3915 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
3917 old_pid = pp_clean[i];
3921 ValidateDVBMeta( p_demux, p_es->i_pid );
3923 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
3925 msg_Warn( p_demux, "pmt error: pid=%d already defined",
3930 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
3931 p_dr = p_dr->p_next )
3933 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
3934 p_es->i_pid, p_es->i_type, p_dr->i_tag );
3937 PIDInit( pid, false, pmt->psi );
3938 PIDFillFormat( pid, p_es->i_type );
3939 pid->i_owner_number = prg->i_number;
3940 pid->i_pid = p_es->i_pid;
3941 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
3943 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
3944 p_es->i_type == 0x12 || p_es->i_type == 0x0f )
3946 PMTSetupEsISO14496( p_demux, pid, prg, p_es );
3948 else if( p_es->i_type == 0x06 )
3950 PMTSetupEs0x06( p_demux, pid, p_es );
3952 else if( p_es->i_type == 0xEA )
3954 PMTSetupEs0xEA( p_demux, pid, p_es );
3956 else if( p_es->i_type == 0xd1 )
3958 PMTSetupEs0xD1( p_demux, pid, p_es );
3960 else if( p_es->i_type == 0xa0 )
3962 PMTSetupEs0xA0( p_demux, pid, p_es );
3966 PMTSetupEsHDMV( p_demux, pid, p_es );
3968 else if( p_es->i_type >= 0x80 )
3970 PMTSetupEsRegistration( p_demux, pid, p_es );
3973 if( pid->es->fmt.i_cat == AUDIO_ES ||
3974 ( pid->es->fmt.i_cat == SPU_ES &&
3975 pid->es->fmt.i_codec != VLC_CODEC_DVBS &&
3976 pid->es->fmt.i_codec != VLC_CODEC_TELETEXT ) )
3978 PMTParseEsIso639( p_demux, pid, p_es );
3981 pid->es->fmt.i_group = p_pmt->i_program_number;
3982 for( int i = 0; i < pid->i_extra_es; i++ )
3983 pid->extra_es[i]->fmt.i_group = p_pmt->i_program_number;
3985 if( pid->es->fmt.i_cat == UNKNOWN_ES )
3987 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
3988 p_es->i_pid, p_es->i_type );
3990 else if( !p_sys->b_udp_out )
3992 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
3993 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
3995 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
3997 /* Check if we can avoid restarting the ES */
3999 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
4000 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
4001 pid->es->fmt.i_extra == 0 &&
4002 pid->i_extra_es == old_pid->i_extra_es &&
4003 ( ( !pid->es->fmt.psz_language &&
4004 !old_pid->es->fmt.psz_language ) ||
4005 ( pid->es->fmt.psz_language &&
4006 old_pid->es->fmt.psz_language &&
4007 !strcmp( pid->es->fmt.psz_language,
4008 old_pid->es->fmt.psz_language ) ) ) )
4010 pid->es->id = old_pid->es->id;
4011 old_pid->es->id = NULL;
4012 for( int i = 0; i < pid->i_extra_es; i++ )
4014 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
4015 old_pid->extra_es[i]->id = NULL;
4022 PIDClean( p_demux, old_pid );
4023 TAB_REMOVE( i_clean, pp_clean, old_pid );
4027 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
4028 for( int i = 0; i < pid->i_extra_es; i++ )
4030 pid->extra_es[i]->id =
4031 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
4033 p_sys->i_pmt_es += 1 + pid->i_extra_es;
4037 /* Add ES to the list */
4040 PIDClean( p_demux, old_pid );
4041 TAB_REMOVE( i_clean, pp_clean, old_pid );
4043 p_sys->pid[p_es->i_pid] = *pid;
4045 p_dr = PMTEsFindDescriptor( p_es, 0x09 );
4046 if( p_dr && p_dr->i_length >= 2 )
4048 uint16_t i_sysid = (p_dr->p_data[0] << 8) | p_dr->p_data[1];
4049 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
4053 if( ProgramIsSelected( p_demux, prg->i_number ) &&
4054 ( pid->es->id != NULL || p_sys->b_udp_out ) )
4056 /* Set demux filter */
4057 SetPIDFilter( p_demux, p_es->i_pid, true );
4061 /* Set CAM descrambling */
4062 if( !ProgramIsSelected( p_demux, prg->i_number )
4063 || stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
4064 ACCESS_SET_PRIVATE_ID_CA, p_pmt ) != VLC_SUCCESS )
4065 dvbpsi_DeletePMT( p_pmt );
4067 for( int i = 0; i < i_clean; i++ )
4069 if( ProgramIsSelected( p_demux, prg->i_number ) )
4071 SetPIDFilter( p_demux, pp_clean[i]->i_pid, false );
4074 PIDClean( p_demux, pp_clean[i] );
4080 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
4082 demux_sys_t *p_sys = p_demux->p_sys;
4083 dvbpsi_pat_program_t *p_program;
4084 ts_pid_t *pat = &p_sys->pid[0];
4086 msg_Dbg( p_demux, "PATCallBack called" );
4088 if( ( pat->psi->i_pat_version != -1 &&
4089 ( !p_pat->b_current_next ||
4090 p_pat->i_version == pat->psi->i_pat_version ) ) ||
4093 dvbpsi_DeletePAT( p_pat );
4097 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
4098 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
4101 if( p_sys->i_pmt > 0 )
4104 ts_pid_t **pmt_rm = NULL;
4106 /* Search pmt to be deleted */
4107 for( int i = 0; i < p_sys->i_pmt; i++ )
4109 ts_pid_t *pmt = p_sys->pmt[i];
4110 bool b_keep = false;
4112 for( p_program = p_pat->p_first_program; p_program != NULL;
4113 p_program = p_program->p_next )
4115 if( p_program->i_pid == pmt->i_pid )
4117 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
4119 if( p_program->i_number ==
4120 pmt->psi->prg[i_prg]->i_number )
4133 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
4137 /* Delete all ES attached to thoses PMT */
4138 for( int i = 2; i < 8192; i++ )
4140 ts_pid_t *pid = &p_sys->pid[i];
4142 if( !pid->b_valid || pid->psi )
4145 for( int j = 0; j < i_pmt_rm && pid->b_valid; j++ )
4147 for( int i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
4149 /* We only remove es that aren't defined by extra pmt */
4150 if( pid->p_owner->prg[i_prg]->i_pid_pmt != pmt_rm[j]->i_pid )
4154 SetPIDFilter( p_demux, i, false );
4156 PIDClean( p_demux, pid );
4162 /* Delete PMT pid */
4163 for( int i = 0; i < i_pmt_rm; i++ )
4165 SetPIDFilter( p_demux, pmt_rm[i]->i_pid, false );
4167 for( int i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
4169 const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
4170 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
4173 PIDClean( p_demux, &p_sys->pid[pmt_rm[i]->i_pid] );
4174 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
4180 /* now create programs */
4181 for( p_program = p_pat->p_first_program; p_program != NULL;
4182 p_program = p_program->p_next )
4184 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
4186 if( p_program->i_number != 0 )
4188 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
4191 ValidateDVBMeta( p_demux, p_program->i_pid );
4195 for( int i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
4197 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
4206 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
4211 PIDInit( pmt, true, pat->psi );
4212 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
4213 dvbpsi_AttachPMT( p_program->i_number,
4214 (dvbpsi_pmt_callback)PMTCallBack,
4216 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
4217 p_program->i_number;
4218 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
4221 /* Now select PID at access level */
4222 if( ProgramIsSelected( p_demux, p_program->i_number ) )
4224 if( p_sys->i_current_program == 0 )
4225 p_sys->i_current_program = p_program->i_number;
4227 if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
4228 p_sys->b_access_control = false;
4233 pat->psi->i_pat_version = p_pat->i_version;
4235 dvbpsi_DeletePAT( p_pat );