1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004 VideoLAN
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
31 #include <vlc/input.h>
36 #include "../mux/mpeg/csa.h"
38 /* Include dvbpsi headers */
39 #ifdef HAVE_DVBPSI_DR_H
40 # include <dvbpsi/dvbpsi.h>
41 # include <dvbpsi/demux.h>
42 # include <dvbpsi/descriptor.h>
43 # include <dvbpsi/pat.h>
44 # include <dvbpsi/pmt.h>
45 # include <dvbpsi/sdt.h>
46 # include <dvbpsi/dr.h>
47 # include <dvbpsi/psi.h>
51 # include "descriptor.h"
52 # include "tables/pat.h"
53 # include "tables/pmt.h"
54 # include "tables/sdt.h"
55 # include "descriptors/dr.h"
60 #ifdef _DVBPSI_DR_4D_H_
61 # define TS_USE_DVB_SI 1
62 # ifdef HAVE_DVBPSI_DR_H
63 # include <dvbpsi/eit.h>
65 # include "tables/eit.h"
70 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Open ( vlc_object_t * );
79 static void Close ( vlc_object_t * );
81 #define PMT_TEXT N_("Extra PMT")
82 #define PMT_LONGTEXT N_( \
83 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])" )
85 #define PID_TEXT N_("Set id of ES to PID")
86 #define PID_LONGTEXT N_("set id of es to pid")
88 #define TSOUT_TEXT N_("Fast udp streaming")
89 #define TSOUT_LONGTEXT N_( \
90 "Sends TS to specific ip:port by udp (you must know what you are doing)")
92 #define MTUOUT_TEXT N_("MTU for out mode")
93 #define MTUOUT_LONGTEXT N_("MTU for out mode")
95 #define CSA_TEXT N_("CSA ck")
96 #define CSA_LONGTEXT N_("CSA ck")
98 #define SILENT_TEXT N_("Silent mode")
99 #define SILENT_LONGTEXT N_("do not complain on encrypted PES")
101 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
102 #define CAPMT_SYSID_LONGTEXT N_("only forward descriptors from this SysID to the CAM")
105 set_description( _("MPEG Transport Stream demuxer") );
106 set_shortname ( "MPEG-TS" );
107 set_category( CAT_INPUT );
108 set_subcategory( SUBCAT_INPUT_DEMUX );
110 add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
111 add_bool( "ts-es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
112 add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
113 add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
114 MTUOUT_LONGTEXT, VLC_TRUE );
115 add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
116 add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );
118 set_capability( "demux2", 10 );
119 set_callbacks( Open, Close );
120 add_shortcut( "ts" );
123 /*****************************************************************************
125 *****************************************************************************/
129 uint8_t i_objectTypeIndication;
130 uint8_t i_streamType;
131 vlc_bool_t b_upStream;
132 uint32_t i_bufferSizeDB;
133 uint32_t i_maxBitrate;
134 uint32_t i_avgBitrate;
136 int i_decoder_specific_info_len;
137 uint8_t *p_decoder_specific_info;
139 } decoder_config_descriptor_t;
143 vlc_bool_t b_useAccessUnitStartFlag;
144 vlc_bool_t b_useAccessUnitEndFlag;
145 vlc_bool_t b_useRandomAccessPointFlag;
146 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
147 vlc_bool_t b_usePaddingFlag;
148 vlc_bool_t b_useTimeStampsFlags;
149 vlc_bool_t b_useIdleFlag;
150 vlc_bool_t b_durationFlag;
151 uint32_t i_timeStampResolution;
152 uint32_t i_OCRResolution;
153 uint8_t i_timeStampLength;
156 uint8_t i_instantBitrateLength;
157 uint8_t i_degradationPriorityLength;
158 uint8_t i_AU_seqNumLength;
159 uint8_t i_packetSeqNumLength;
161 uint32_t i_timeScale;
162 uint16_t i_accessUnitDuration;
163 uint16_t i_compositionUnitDuration;
165 uint64_t i_startDecodingTimeStamp;
166 uint64_t i_startCompositionTimeStamp;
168 } sl_config_descriptor_t;
175 vlc_bool_t b_streamDependenceFlag;
176 vlc_bool_t b_OCRStreamFlag;
177 uint8_t i_streamPriority;
181 uint16_t i_dependOn_es_id;
182 uint16_t i_OCR_es_id;
184 decoder_config_descriptor_t dec_descr;
185 sl_config_descriptor_t sl_descr;
187 } es_mpeg4_descriptor_t;
191 uint8_t i_iod_label, i_iod_label_scope;
197 uint8_t i_ODProfileLevelIndication;
198 uint8_t i_sceneProfileLevelIndication;
199 uint8_t i_audioProfileLevelIndication;
200 uint8_t i_visualProfileLevelIndication;
201 uint8_t i_graphicsProfileLevelIndication;
203 es_mpeg4_descriptor_t es_descr[255];
209 dvbpsi_handle handle;
215 /* IOD stuff (mpeg4) */
216 iod_descriptor_t *iod;
222 /* for special PAT/SDT case */
223 dvbpsi_handle handle; /* PAT/SDT/EIT */
242 es_mpeg4_descriptor_t *p_mpeg4desc;
253 int i_cc; /* countinuity counter */
255 /* PSI owner (ie PMT -> PAT, ES -> PMT */
263 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
271 /* TS packet size (188, 192, 204) */
274 /* how many TS packet we read at once */
285 vlc_bool_t b_es_id_pid;
289 vlc_bool_t b_udp_out;
290 int fd; /* udp socket */
293 vlc_bool_t b_dvb_control;
295 vlc_list_t *p_programs_list;
301 static int Demux ( demux_t *p_demux );
302 static int Control( demux_t *p_demux, int i_query, va_list args );
305 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
306 static void PIDClean( es_out_t *out, ts_pid_t *pid );
307 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
309 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
310 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
312 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
313 uint8_t i_table_id, uint16_t i_extension );
317 static inline int PIDGet( block_t *p )
319 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
322 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
324 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
326 static iod_descriptor_t *IODNew( int , uint8_t * );
327 static void IODFree( iod_descriptor_t * );
329 #define TS_PACKET_SIZE_188 188
330 #define TS_PACKET_SIZE_192 192
331 #define TS_PACKET_SIZE_204 204
332 #define TS_PACKET_SIZE_MAX 204
334 /*****************************************************************************
336 *****************************************************************************/
337 static int Open( vlc_object_t *p_this )
339 demux_t *p_demux = (demux_t*)p_this;
343 int i_sync, i_peek, i;
350 if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
351 TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
353 /* Search first sync byte */
354 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
356 if( p_peek[i_sync] == 0x47 ) break;
358 if( i_sync >= TS_PACKET_SIZE_MAX )
360 if( strcmp( p_demux->psz_demux, "ts" ) ) return VLC_EGENERIC;
361 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
364 /* Check next 3 sync bytes */
365 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
366 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
368 msg_Err( p_demux, "cannot peek" );
371 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
372 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
373 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
375 i_packet_size = TS_PACKET_SIZE_188;
377 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
378 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
379 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
381 i_packet_size = TS_PACKET_SIZE_192;
383 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
384 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
385 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
387 i_packet_size = TS_PACKET_SIZE_204;
389 else if( !strcmp( p_demux->psz_demux, "ts" ) )
391 i_packet_size = TS_PACKET_SIZE_188;
395 msg_Warn( p_demux, "TS module discarded (lost sync)" );
399 /* Fill p_demux field */
400 p_demux->pf_demux = Demux;
401 p_demux->pf_control = Control;
402 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
403 memset( p_sys, 0, sizeof( demux_sys_t ) );
405 /* Init p_sys field */
406 p_sys->b_meta = VLC_TRUE;
407 p_sys->b_dvb_control = VLC_TRUE;
408 p_sys->i_dvb_program = 0;
409 for( i = 0; i < 8192; i++ )
411 ts_pid_t *pid = &p_sys->pid[i];
414 pid->b_seen = VLC_FALSE;
415 pid->b_valid = VLC_FALSE;
417 p_sys->i_packet_size = i_packet_size;
418 p_sys->b_udp_out = VLC_FALSE;
419 p_sys->i_ts_read = 50;
422 /* Init PAT handler */
423 pat = &p_sys->pid[0];
424 PIDInit( pat, VLC_TRUE, NULL );
425 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
430 ts_pid_t *sdt = &p_sys->pid[0x11];
431 ts_pid_t *eit = &p_sys->pid[0x12];
433 PIDInit( sdt, VLC_TRUE, NULL );
435 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
437 PIDInit( eit, VLC_TRUE, NULL );
439 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
441 if( p_sys->b_dvb_control )
443 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
444 ACCESS_SET_PRIVATE_ID_STATE, 0x11, VLC_TRUE );
445 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
446 ACCESS_SET_PRIVATE_ID_STATE, 0x12, VLC_TRUE );
456 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
457 var_Get( p_demux, "ts-es-id-pid", &val );
458 p_sys->b_es_id_pid = val.b_bool;
460 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
461 var_Get( p_demux, "ts-out", &val );
462 if( val.psz_string && *val.psz_string )
465 char *psz = strchr( val.psz_string, ':' );
468 p_sys->b_udp_out = VLC_TRUE;
473 i_port = atoi( psz );
475 if( i_port <= 0 ) i_port = 1234;
476 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
478 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
481 msg_Err( p_demux, "failed to open udp socket, send disabled" );
482 p_sys->b_udp_out = VLC_FALSE;
486 var_Create( p_demux, "ts-out-mtu",
487 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
488 var_Get( p_demux, "ts-out-mtu", &mtu );
489 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
490 if( p_sys->i_ts_read <= 0 )
492 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
494 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
499 free( val.psz_string );
503 /* We handle description of an extra PMT */
504 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
505 var_Get( p_demux, "ts-extra-pmt", &val );
506 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
508 char *psz = val.psz_string;
509 int i_pid = strtol( psz, &psz, 0 );
511 if( i_pid >= 2 && i_pid < 8192 )
513 ts_pid_t *pmt = &p_sys->pid[i_pid];
515 msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
516 PIDInit( pmt, VLC_TRUE, NULL );
518 pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
519 /* FIXME we should also ask for a number */
520 pmt->psi->prg[0]->handle =
521 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
523 pmt->psi->prg[0]->i_number = 0; /* special one */
525 psz = strchr( psz, '=' ) + 1; /* can't failed */
528 char *psz_next = strchr( psz, ',' );
529 int i_pid, i_stream_type;
536 i_pid = strtol( psz, &psz, 0 );
539 i_stream_type = strtol( psz + 1, &psz, 0 );
540 if( i_pid >= 2 && i_pid < 8192 &&
541 !p_sys->pid[i_pid].b_valid )
543 ts_pid_t *pid = &p_sys->pid[i_pid];
545 PIDInit( pid, VLC_FALSE, pmt->psi);
546 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
548 pmt->psi->prg[0]->i_pid_pcr = i_pid;
550 PIDFillFormat( pid, i_stream_type);
551 if( pid->es->fmt.i_cat != UNKNOWN_ES )
553 if( p_sys->b_es_id_pid )
555 pid->es->fmt.i_id = i_pid;
557 msg_Dbg( p_demux, " * es pid=%d type=%d "
558 "fcc=%4.4s", i_pid, i_stream_type,
559 (char*)&pid->es->fmt.i_codec );
560 pid->es->id = es_out_Add( p_demux->out,
571 free( val.psz_string );
574 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
575 var_Get( p_demux, "ts-csa-ck", &val );
576 if( val.psz_string && *val.psz_string )
578 char *psz = val.psz_string;
579 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
583 if( strlen( psz ) != 16 )
585 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
590 uint64_t i_ck = strtoull( psz, NULL, 16 );
592 uint64_t i_ck = strtoll( psz, NULL, 16 );
596 for( i = 0; i < 8; i++ )
598 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
601 msg_Dbg( p_demux, "using CSA scrambling with "
602 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
603 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
605 p_sys->csa = csa_New();
606 csa_SetCW( p_sys->csa, ck, ck );
611 free( val.psz_string );
614 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
615 var_Get( p_demux, "ts-silent", &val );
616 p_sys->b_silent = val.b_bool;
621 /*****************************************************************************
623 *****************************************************************************/
624 static void Close( vlc_object_t *p_this )
626 demux_t *p_demux = (demux_t*)p_this;
627 demux_sys_t *p_sys = p_demux->p_sys;
631 msg_Dbg( p_demux, "pid list:" );
632 for( i = 0; i < 8192; i++ )
634 ts_pid_t *pid = &p_sys->pid[i];
636 if( pid->b_valid && pid->psi )
641 dvbpsi_DetachPAT( pid->psi->handle );
649 dvbpsi_DetachDemux( pid->psi->handle );
653 PIDClean( p_demux->out, pid );
657 else if( pid->b_valid && pid->es )
659 PIDClean( p_demux->out, pid );
664 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
667 if( p_sys->b_dvb_control && pid->i_pid > 0 )
670 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
675 if( p_sys->b_udp_out )
677 net_Close( p_sys->fd );
678 free( p_sys->buffer );
682 csa_Delete( p_sys->csa );
685 if( p_sys->i_pmt ) free( p_sys->pmt );
687 if ( p_sys->p_programs_list )
690 val.p_list = p_sys->p_programs_list;
691 var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
697 /*****************************************************************************
699 *****************************************************************************/
700 static int Demux( demux_t *p_demux )
702 demux_sys_t *p_sys = p_demux->p_sys;
705 /* We read at most 100 TS packet or until a frame is completed */
706 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
708 vlc_bool_t b_frame = VLC_FALSE;
712 /* Get a new TS packet */
713 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
715 msg_Dbg( p_demux, "eof ?" );
719 /* Check sync byte and re-sync if needed */
720 if( p_pkt->p_buffer[0] != 0x47 )
722 msg_Warn( p_demux, "lost synchro" );
723 block_Release( p_pkt );
725 while( !p_demux->b_die )
728 int i_peek, i_skip = 0;
730 i_peek = stream_Peek( p_demux->s, &p_peek,
731 p_sys->i_packet_size * 10 );
732 if( i_peek < p_sys->i_packet_size + 1 )
734 msg_Dbg( p_demux, "eof ?" );
738 while( i_skip < i_peek - p_sys->i_packet_size )
740 if( p_peek[i_skip] == 0x47 &&
741 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
748 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
749 stream_Read( p_demux->s, NULL, i_skip );
751 if( i_skip < i_peek - p_sys->i_packet_size )
757 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
759 msg_Dbg( p_demux, "eof ?" );
764 if( p_sys->b_udp_out )
766 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
767 p_pkt->p_buffer, p_sys->i_packet_size );
770 /* Parse the TS packet */
771 p_pid = &p_sys->pid[PIDGet( p_pkt )];
777 if( p_pid->i_pid == 0 || p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 )
779 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
784 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
786 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
790 block_Release( p_pkt );
792 else if( !p_sys->b_udp_out )
794 b_frame = GatherPES( p_demux, p_pid, p_pkt );
798 PCRHandle( p_demux, p_pid, p_pkt );
799 block_Release( p_pkt );
806 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
808 /* We have to handle PCR if present */
809 PCRHandle( p_demux, p_pid, p_pkt );
810 block_Release( p_pkt );
812 p_pid->b_seen = VLC_TRUE;
820 if( p_sys->b_udp_out )
822 /* Send the complete block */
823 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
824 p_sys->i_ts_read * p_sys->i_packet_size );
830 /*****************************************************************************
832 *****************************************************************************/
833 static int Control( demux_t *p_demux, int i_query, va_list args )
835 demux_sys_t *p_sys = p_demux->p_sys;
842 case DEMUX_GET_POSITION:
843 pf = (double*) va_arg( args, double* );
844 i64 = stream_Size( p_demux->s );
847 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
854 case DEMUX_SET_POSITION:
855 f = (double) va_arg( args, double );
856 i64 = stream_Size( p_demux->s );
858 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
859 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
867 pi64 = (int64_t*)va_arg( args, int64_t * );
868 if( p_sys->i_time < 0 )
873 *pi64 = p_sys->i_time;
876 case DEMUX_GET_LENGTH:
877 pi64 = (int64_t*)va_arg( args, int64_t * );
878 if( p_sys->i_mux_rate > 0 )
880 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
887 case DEMUX_SET_GROUP:
889 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
890 ts_prg_psi_t *p_prg = NULL;
893 i_int = (int)va_arg( args, int );
894 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
895 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
897 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
902 /* Search pmt to be unselected */
903 for( i = 0; i < p_sys->i_pmt; i++ )
905 ts_pid_t *pmt = p_sys->pmt[i];
908 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
910 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
912 i_pmt_pid = p_sys->pmt[i]->i_pid;
916 if( i_pmt_pid > 0 ) break;
921 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
922 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
925 for( i = 2; i < 8192; i++ )
927 ts_pid_t *pid = &p_sys->pid[i];
930 if( !pid->b_valid || pid->psi ) continue;
932 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
934 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
936 /* We only remove es that aren't defined by extra pmt */
937 stream_Control( p_demux->s,
938 STREAM_CONTROL_ACCESS,
939 ACCESS_SET_PRIVATE_ID_STATE,
947 /* select new program */
948 p_sys->i_dvb_program = i_int;
950 for( i = 0; i < p_sys->i_pmt; i++ )
952 ts_pid_t *pmt = p_sys->pmt[i];
955 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
957 if( pmt->psi->prg[i_prg]->i_number == i_int )
959 i_pmt_pid = p_sys->pmt[i]->i_pid;
960 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
964 if( i_pmt_pid > 0 ) break;
968 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
969 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
971 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
972 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
975 for( i = 2; i < 8192; i++ )
977 ts_pid_t *pid = &p_sys->pid[i];
980 if( !pid->b_valid || pid->psi ) continue;
982 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
984 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
986 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
988 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
990 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
992 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
995 stream_Control( p_demux->s,
996 STREAM_CONTROL_ACCESS,
997 ACCESS_SET_PRIVATE_ID_STATE,
1007 p_sys->i_dvb_program = -1;
1008 p_sys->p_programs_list = p_list;
1014 case DEMUX_SET_TIME:
1016 return VLC_EGENERIC;
1020 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
1022 vlc_bool_t b_old_valid = pid->b_valid;
1024 pid->b_valid = VLC_TRUE;
1026 pid->p_owner = p_owner;
1027 pid->i_owner_number = 0;
1029 pid->extra_es = NULL;
1030 pid->i_extra_es = 0;
1038 pid->psi = malloc( sizeof( ts_psi_t ) );
1039 pid->psi->handle= NULL;
1040 pid->psi->i_prg = 0;
1041 pid->psi->prg = NULL;
1043 pid->psi->i_pat_version = -1;
1044 pid->psi->i_sdt_version = -1;
1047 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1049 prg->i_version = -1;
1051 prg->i_pid_pcr = -1;
1052 prg->i_pid_pmt = -1;
1056 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1062 pid->es = malloc( sizeof( ts_es_t ) );
1064 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1066 pid->es->p_pes = NULL;
1067 pid->es->i_pes_size= 0;
1068 pid->es->i_pes_gathered= 0;
1069 pid->es->pp_last = &pid->es->p_pes;
1070 pid->es->p_mpeg4desc = NULL;
1071 pid->es->b_gather = VLC_FALSE;
1075 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1081 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1082 for( i = 0; i < pid->psi->i_prg; i++ )
1084 if( pid->psi->prg[i]->iod )
1085 IODFree( pid->psi->prg[i]->iod );
1086 if( pid->psi->prg[i]->handle )
1087 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1088 free( pid->psi->prg[i] );
1090 if( pid->psi->prg ) free( pid->psi->prg );
1098 es_out_Del( out, pid->es->id );
1100 if( pid->es->p_pes )
1101 block_ChainRelease( pid->es->p_pes );
1103 es_format_Clean( &pid->es->fmt );
1107 for( i = 0; i < pid->i_extra_es; i++ )
1109 if( pid->extra_es[i]->id )
1110 es_out_Del( out, pid->extra_es[i]->id );
1112 if( pid->extra_es[i]->p_pes )
1113 block_ChainRelease( pid->extra_es[i]->p_pes );
1115 es_format_Clean( &pid->extra_es[i]->fmt );
1117 free( pid->extra_es[i] );
1119 if( pid->i_extra_es ) free( pid->extra_es );
1122 pid->b_valid = VLC_FALSE;
1125 /****************************************************************************
1127 ****************************************************************************/
1128 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1130 block_t *p_pes = pid->es->p_pes;
1136 mtime_t i_length = 0;
1139 /* remove the pes from pid */
1140 pid->es->p_pes = NULL;
1141 pid->es->i_pes_size= 0;
1142 pid->es->i_pes_gathered= 0;
1143 pid->es->pp_last = &pid->es->p_pes;
1145 /* FIXME find real max size */
1146 i_max = block_ChainExtract( p_pes, header, 30 );
1149 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1151 if( !p_demux->p_sys->b_silent )
1152 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1153 header[0], header[1],header[2],header[3], pid->i_pid );
1154 block_ChainRelease( p_pes );
1158 /* TODO check size */
1161 case 0xBC: /* Program stream map */
1162 case 0xBE: /* Padding */
1163 case 0xBF: /* Private stream 2 */
1164 case 0xB0: /* ECM */
1165 case 0xB1: /* EMM */
1166 case 0xFF: /* Program stream directory */
1167 case 0xF2: /* DSMCC stream */
1168 case 0xF8: /* ITU-T H.222.1 type E stream */
1172 if( ( header[6]&0xC0 ) == 0x80 )
1175 i_skip = header[8] + 9;
1177 if( header[7]&0x80 ) /* has pts */
1179 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1180 (mtime_t)(header[10] << 22)|
1181 ((mtime_t)(header[11]&0xfe) << 14)|
1182 (mtime_t)(header[12] << 7)|
1183 (mtime_t)(header[13] >> 1);
1185 if( header[7]&0x40 ) /* has dts */
1187 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1188 (mtime_t)(header[15] << 22)|
1189 ((mtime_t)(header[16]&0xfe) << 14)|
1190 (mtime_t)(header[17] << 7)|
1191 (mtime_t)(header[18] >> 1);
1198 while( i_skip < 23 && header[i_skip] == 0xff )
1204 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1205 block_ChainRelease( p_pes );
1208 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1213 if( header[i_skip]&0x20 )
1215 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1216 (mtime_t)(header[i_skip+1] << 22)|
1217 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1218 (mtime_t)(header[i_skip+3] << 7)|
1219 (mtime_t)(header[i_skip+4] >> 1);
1221 if( header[i_skip]&0x10 ) /* has dts */
1223 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1224 (mtime_t)(header[i_skip+6] << 22)|
1225 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1226 (mtime_t)(header[i_skip+8] << 7)|
1227 (mtime_t)(header[i_skip+9] >> 1);
1243 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1244 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1248 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1249 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1250 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1254 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1255 pid->es->p_mpeg4desc )
1257 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1259 if( dcd->i_decoder_specific_info_len > 2 &&
1260 dcd->p_decoder_specific_info[0] == 0x10 &&
1261 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1263 /* display length */
1264 if( p_pes->i_buffer + 2 <= i_skip )
1266 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1271 if( p_pes->i_buffer + 2 <= i_skip )
1273 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1280 while( p_pes && i_skip > 0 )
1282 if( p_pes->i_buffer <= i_skip )
1284 block_t *p_next = p_pes->p_next;
1286 i_skip -= p_pes->i_buffer;
1287 block_Release( p_pes );
1292 p_pes->i_buffer -= i_skip;
1293 p_pes->p_buffer += i_skip;
1298 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1299 if( i_pts >= 0 && i_dts < 0 )
1309 p_pes->i_dts = i_dts * 100 / 9;
1313 p_pes->i_pts = i_pts * 100 / 9;
1315 p_pes->i_length = i_length * 100 / 9;
1317 p_block = block_ChainGather( p_pes );
1318 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1320 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1322 p_block->i_buffer = i_pes_size;
1325 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1326 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1329 for( i = 0; i < pid->i_extra_es; i++ )
1331 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1332 block_Duplicate( p_block ) );
1335 es_out_Send( p_demux->out, pid->es->id, p_block );
1339 msg_Warn( p_demux, "empty pes" );
1343 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1345 demux_sys_t *p_sys = p_demux->p_sys;
1346 const uint8_t *p = p_bk->p_buffer;
1348 if( ( p[3]&0x20 ) && /* adaptation */
1353 mtime_t i_pcr; /* 33 bits */
1355 i_pcr = ( (mtime_t)p[6] << 25 ) |
1356 ( (mtime_t)p[7] << 17 ) |
1357 ( (mtime_t)p[8] << 9 ) |
1358 ( (mtime_t)p[9] << 1 ) |
1359 ( (mtime_t)p[10] >> 7 );
1361 /* Search program and set the PCR */
1362 for( i = 0; i < p_sys->i_pmt; i++ )
1365 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1367 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1369 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1370 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1371 (int64_t)(i_pcr * 100 / 9) );
1378 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1380 const uint8_t *p = p_bk->p_buffer;
1381 const vlc_bool_t b_unit_start = p[1]&0x40;
1382 const vlc_bool_t b_adaptation = p[3]&0x20;
1383 const vlc_bool_t b_payload = p[3]&0x10;
1384 const int i_cc = p[3]&0x0f; /* continuity counter */
1386 /* transport_scrambling_control is ignored */
1389 vlc_bool_t i_ret = VLC_FALSE;
1393 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1394 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1398 /* For now, ignore additional error correction
1399 * TODO: handle Reed-Solomon 204,188 error correction */
1400 p_bk->i_buffer = TS_PACKET_SIZE_188;
1404 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1406 if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
1407 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1410 if( p_demux->p_sys->csa )
1412 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
1417 /* We don't have any adaptation_field, so payload starts
1418 * immediately after the 4 byte TS header */
1423 /* p[4] is adaptation_field_length minus one */
1429 msg_Warn( p_demux, "discontinuity_indicator (pid=%d) "
1430 "ignored", pid->i_pid );
1435 /* Test continuity counter */
1436 /* continuous when (one of this):
1438 * diff == 0 and payload == 0
1439 * diff == 0 and duplicate packet (playload != 0) <- should we
1440 * test the content ?
1443 i_diff = ( i_cc - pid->i_cc )&0x0f;
1444 if( b_payload && i_diff == 1 )
1450 if( pid->i_cc == 0xff )
1452 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1456 else if( i_diff != 0 )
1458 /* FIXME what to do when discontinuity_indicator is set ? */
1459 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
1460 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
1464 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1466 /* Small video artifacts are usually better then
1467 * dropping full frames */
1468 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1473 PCRHandle( p_demux, pid, p_bk );
1475 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1477 block_Release( p_bk );
1481 /* We have to gather it */
1482 p_bk->p_buffer += i_skip;
1483 p_bk->i_buffer -= i_skip;
1487 if( pid->es->p_pes )
1489 ParsePES( p_demux, pid );
1493 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1494 if( p_bk->i_buffer > 6 )
1496 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1497 if( pid->es->i_pes_size > 0 )
1499 pid->es->i_pes_size += 6;
1502 pid->es->i_pes_gathered += p_bk->i_buffer;
1503 if( pid->es->i_pes_size > 0 &&
1504 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1506 ParsePES( p_demux, pid );
1512 if( pid->es->p_pes == NULL )
1514 /* msg_Dbg( p_demux, "broken packet" ); */
1515 block_Release( p_bk );
1519 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1520 pid->es->i_pes_gathered += p_bk->i_buffer;
1521 if( pid->es->i_pes_size > 0 &&
1522 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1524 ParsePES( p_demux, pid );
1533 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1535 es_format_t *fmt = &pid->es->fmt;
1537 switch( i_stream_type )
1539 case 0x01: /* MPEG-1 video */
1540 case 0x02: /* MPEG-2 video */
1541 case 0x80: /* MPEG-2 MOTO video */
1542 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1544 case 0x03: /* MPEG-1 audio */
1545 case 0x04: /* MPEG-2 audio */
1546 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1548 case 0x11: /* MPEG4 (audio) */
1549 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1550 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1552 case 0x10: /* MPEG4 (video) */
1553 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1554 pid->es->b_gather = VLC_TRUE;
1556 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1557 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1560 case 0x81: /* A52 (audio) */
1561 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1563 case 0x82: /* DVD_SPU (sub) */
1564 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1566 case 0x83: /* LPCM (audio) */
1567 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1569 case 0x84: /* SDDS (audio) */
1570 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1572 case 0x85: /* DTS (audio) */
1573 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1576 case 0x91: /* A52 vls (audio) */
1577 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1579 case 0x92: /* DVD_SPU vls (sub) */
1580 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1582 case 0x93: /* LPCM vls (audio) */
1583 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1585 case 0x94: /* SDDS (audio) */
1586 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1589 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1590 es_format_Init( fmt, UNKNOWN_ES, 0 );
1591 pid->es->b_gather = VLC_TRUE;
1594 case 0x06: /* PES_PRIVATE (fixed later) */
1595 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1597 es_format_Init( fmt, UNKNOWN_ES, 0 );
1601 /* PES packets usually contain truncated frames */
1602 fmt->b_packetized = VLC_FALSE;
1604 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1607 /*****************************************************************************
1608 * MP4 specific functions (IOD parser)
1609 *****************************************************************************/
1610 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1613 unsigned int i_len = 0;
1619 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1621 } while( i_b&0x80 );
1625 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1629 const int i_b = **pp_data;
1636 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1638 const int i1 = IODGetByte( pi_data, pp_data );
1639 const int i2 = IODGetByte( pi_data, pp_data );
1640 return( ( i1 << 8 ) | i2 );
1642 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1644 const int i1 = IODGetByte( pi_data, pp_data );
1645 const int i2 = IODGetByte( pi_data, pp_data );
1646 const int i3 = IODGetByte( pi_data, pp_data );
1648 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1651 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1653 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1654 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1655 return( ( i1 << 16 ) | i2 );
1658 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1663 i_url_len = IODGetByte( pi_data, pp_data );
1664 url = malloc( i_url_len + 1 );
1665 for( i = 0; i < i_url_len; i++ )
1667 url[i] = IODGetByte( pi_data, pp_data );
1669 url[i_url_len] = '\0';
1673 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1675 iod_descriptor_t *p_iod;
1678 uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
1682 p_iod = malloc( sizeof( iod_descriptor_t ) );
1683 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1685 fprintf( stderr, "\n************ IOD ************" );
1686 for( i = 0; i < 255; i++ )
1688 p_iod->es_descr[i].b_ok = 0;
1697 byte1 = IODGetByte( &i_data, &p_data );
1698 byte2 = IODGetByte( &i_data, &p_data );
1699 byte3 = IODGetByte( &i_data, &p_data );
1700 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
1702 p_iod->i_iod_label_scope = 0x11;
1703 p_iod->i_iod_label = byte1;
1706 else //correct implementation of the IOD_descriptor
1708 p_iod->i_iod_label_scope = byte1;
1709 p_iod->i_iod_label = byte2;
1712 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1713 fprintf( stderr, "\n* ===========" );
1714 fprintf( stderr, "\n* tag:0x%x", i_iod_tag );
1716 if( i_iod_tag != 0x02 )
1718 fprintf( stderr, "\n ERR: tag %02x != 0x02", i_iod_tag );
1722 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1723 fprintf( stderr, "\n* length:%d", i_iod_length );
1724 if( i_iod_length > i_data )
1726 i_iod_length = i_data;
1729 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1730 i_flags = IODGetByte( &i_data, &p_data );
1731 p_iod->i_od_id |= i_flags >> 6;
1732 b_url = ( i_flags >> 5 )&0x01;
1734 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1735 fprintf( stderr, "\n* url flag:%d", b_url );
1736 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1740 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1741 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1742 fprintf( stderr, "\n*****************************\n" );
1747 p_iod->psz_url = NULL;
1750 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1751 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1752 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1753 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1754 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1756 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1757 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1758 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1759 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1760 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1763 while( i_data > 0 && i_es_index < 255)
1765 int i_tag, i_length;
1767 uint8_t *p_data_sav;
1769 i_tag = IODGetByte( &i_data, &p_data );
1770 i_length = IODDescriptorLength( &i_data, &p_data );
1772 i_data_sav = i_data;
1773 p_data_sav = p_data;
1781 #define es_descr p_iod->es_descr[i_es_index]
1782 int i_decoderConfigDescr_length;
1783 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1786 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1787 i_flags = IODGetByte( &i_data, &p_data );
1788 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1789 b_url = ( i_flags >> 6 )&0x01;
1790 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1791 es_descr.i_streamPriority = i_flags & 0x1f;
1792 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1793 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1794 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1796 if( es_descr.b_streamDependenceFlag )
1798 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1799 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1804 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1805 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1809 es_descr.psz_url = NULL;
1812 if( es_descr.b_OCRStreamFlag )
1814 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1815 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1818 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1820 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1824 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1826 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1827 #define dec_descr es_descr.dec_descr
1828 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1829 i_flags = IODGetByte( &i_data, &p_data );
1830 dec_descr.i_streamType = i_flags >> 2;
1831 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1832 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1833 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1834 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1835 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1836 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1837 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1838 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1839 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1840 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1841 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1844 dec_descr.i_decoder_specific_info_len =
1845 IODDescriptorLength( &i_data, &p_data );
1846 if( dec_descr.i_decoder_specific_info_len > 0 )
1848 dec_descr.p_decoder_specific_info =
1849 malloc( dec_descr.i_decoder_specific_info_len );
1851 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1853 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1858 dec_descr.i_decoder_specific_info_len = 0;
1859 dec_descr.p_decoder_specific_info = NULL;
1863 #define sl_descr es_descr.sl_descr
1865 int i_SLConfigDescr_length;
1868 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1870 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1874 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1876 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1877 i_predefined = IODGetByte( &i_data, &p_data );
1878 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1879 switch( i_predefined )
1883 sl_descr.b_useAccessUnitStartFlag = 0;
1884 sl_descr.b_useAccessUnitEndFlag = 0;
1885 sl_descr.b_useRandomAccessPointFlag = 0;
1886 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1887 sl_descr.b_usePaddingFlag = 0;
1888 sl_descr.b_useTimeStampsFlags = 0;
1889 sl_descr.b_useIdleFlag = 0;
1890 sl_descr.b_durationFlag = 0; // FIXME FIXME
1891 sl_descr.i_timeStampResolution = 1000;
1892 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1893 sl_descr.i_timeStampLength = 32;
1894 sl_descr.i_OCRLength = 0; // FIXME FIXME
1895 sl_descr.i_AU_Length = 0;
1896 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1897 sl_descr.i_degradationPriorityLength= 0;
1898 sl_descr.i_AU_seqNumLength = 0;
1899 sl_descr.i_packetSeqNumLength = 0;
1900 if( sl_descr.b_durationFlag )
1902 sl_descr.i_timeScale = 0; // FIXME FIXME
1903 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1904 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1906 if( !sl_descr.b_useTimeStampsFlags )
1908 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1909 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1914 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1923 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1927 p_data = p_data_sav + i_length;
1928 i_data = i_data_sav - i_length;
1933 fprintf( stderr, "\n*****************************\n" );
1937 static void IODFree( iod_descriptor_t *p_iod )
1941 if( p_iod->psz_url )
1943 free( p_iod->psz_url );
1944 p_iod->psz_url = NULL;
1949 for( i = 0; i < 255; i++ )
1951 #define es_descr p_iod->es_descr[i]
1954 if( es_descr.psz_url )
1956 free( es_descr.psz_url );
1957 es_descr.psz_url = NULL;
1961 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1963 free( es_descr.dec_descr.p_decoder_specific_info );
1964 es_descr.dec_descr.p_decoder_specific_info = NULL;
1965 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1975 /****************************************************************************
1976 ****************************************************************************
1977 ** libdvbpsi callbacks
1978 ****************************************************************************
1979 ****************************************************************************/
1980 static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
1982 demux_sys_t *p_sys = p_demux->p_sys;
1984 if ( !p_sys->b_dvb_control ) return VLC_FALSE;
1985 if ( p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL )
1987 if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
1989 if ( p_sys->p_programs_list != NULL )
1992 for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
1994 if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
2001 #ifdef TS_USE_DVB_SI
2002 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2004 demux_sys_t *p_sys = p_demux->p_sys;
2005 ts_pid_t *sdt = &p_sys->pid[0x11];
2006 dvbpsi_sdt_service_t *p_srv;
2008 msg_Dbg( p_demux, "SDTCallBack called" );
2010 if( sdt->psi->i_sdt_version != -1 &&
2011 ( !p_sdt->b_current_next ||
2012 p_sdt->i_version == sdt->psi->i_sdt_version ) )
2014 dvbpsi_DeleteSDT( p_sdt );
2018 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2020 p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
2021 p_sdt->i_network_id );
2023 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2025 vlc_meta_t *p_meta = vlc_meta_New();
2026 dvbpsi_descriptor_t *p_dr;
2028 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
2029 "runing=%d free_ca=%d",
2030 p_srv->i_service_id, p_srv->b_eit_schedule,
2031 p_srv->b_eit_present, p_srv->i_running_status,
2034 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2036 if( p_dr->i_tag == 0x48 )
2038 static const char *psz_type[0x11] = {
2040 "Digital television service",
2041 "Digital radio sound service",
2043 "NVOD reference service",
2044 "NVOD time-shifted service",
2047 "SECAM coded signal",
2050 "NTSC coded signal",
2051 "Data broadcast service",
2052 "Reserved for Common Interface Usage",
2053 "RCS Map (see EN 301 790 [35])",
2054 "RCS FLS (see EN 301 790 [35])",
2057 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2061 memcpy( str1, pD->i_service_provider_name,
2062 pD->i_service_provider_name_length );
2063 str1[pD->i_service_provider_name_length] = '\0';
2064 memcpy( str2, pD->i_service_name, pD->i_service_name_length );
2065 str2[pD->i_service_name_length] = '\0';
2067 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
2068 pD->i_service_type, str1, str2 );
2070 vlc_meta_Add( p_meta, "Name", str2 );
2071 vlc_meta_Add( p_meta, "Provider", str1 );
2072 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2073 vlc_meta_Add( p_meta, "Type", psz_type[pD->i_service_type] );
2077 if( p_srv->i_running_status == 0x01 )
2078 vlc_meta_Add( p_meta, "Status", "Not running" );
2079 else if( p_srv->i_running_status == 0x02 )
2080 vlc_meta_Add( p_meta, "Status", "Starts in a few seconds" );
2081 else if( p_srv->i_running_status == 0x03 )
2082 vlc_meta_Add( p_meta, "Status", "Pausing" );
2083 else if( p_srv->i_running_status == 0x04 )
2084 vlc_meta_Add( p_meta, "Status", "Running" );
2086 vlc_meta_Add( p_meta, "Status", "Unknown" );
2089 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2090 p_srv->i_service_id, p_meta );
2091 vlc_meta_Delete( p_meta );
2094 sdt->psi->i_sdt_version = p_sdt->i_version;
2095 dvbpsi_DeleteSDT( p_sdt );
2098 static void DecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2100 int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2101 int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2103 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2105 if( mp == 14 || mp == 15 )
2117 static void EITEventFixString( char *psz )
2120 /* Sometimes the first char isn't a normal char but designed
2121 * caracters encoding, for now lets skip it */
2122 if( isalnum(psz[0]) )
2124 if( ( i_len = strlen( psz ) ) > 0 )
2125 memmove( &psz[0], &psz[1], i_len ); /* Copy the \0 too */
2127 static void EITCallBack( demux_t *p_demux, dvbpsi_eit_t *p_eit )
2129 dvbpsi_eit_event_t *p_evt;
2131 vlc_bool_t b_event_active = VLC_FALSE;
2133 msg_Dbg( p_demux, "EITCallBack called" );
2134 if( !p_eit->b_current_next )
2136 dvbpsi_DeleteEIT( p_eit );
2140 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
2141 "ts_id=%d network_id=%d segment_last_section_number=%d "
2143 p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
2144 p_eit->i_ts_id, p_eit->i_network_id,
2145 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2147 p_meta = vlc_meta_New();
2148 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2150 dvbpsi_descriptor_t *p_dr;
2151 char *psz_cat = malloc( strlen("Event")+10 );
2153 char psz_duration[15];
2156 char *psz_extra = strdup("");
2159 sprintf( psz_cat, "Event %d", p_evt->i_event_id );
2160 sprintf( psz_start, "%d%d:%d%d:%d%d",
2161 (int)(p_evt->i_start_time >> 20)&0xf,
2162 (int)(p_evt->i_start_time >> 16)&0xf,
2163 (int)(p_evt->i_start_time >> 12)&0xf,
2164 (int)(p_evt->i_start_time >> 8)&0xf,
2165 (int)(p_evt->i_start_time >> 4)&0xf,
2166 (int)(p_evt->i_start_time )&0xf );
2167 sprintf( psz_duration, "%d%d:%d%d:%d%d",
2168 (p_evt->i_duration >> 20)&0xf, (p_evt->i_duration >> 16)&0xf,
2169 (p_evt->i_duration >> 12)&0xf, (p_evt->i_duration >> 8)&0xf,
2170 (p_evt->i_duration >> 4)&0xf, (p_evt->i_duration )&0xf );
2171 psz_name[0] = psz_text[0] = '\0';
2173 msg_Dbg( p_demux, " * event id=%d start_time:mjd=%d %s duration=%s "
2174 "running=%d free_ca=%d",
2176 (int)(p_evt->i_start_time >> 24),
2177 psz_start, psz_duration,
2178 p_evt->i_running_status, p_evt->b_free_ca );
2180 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2182 if( p_dr->i_tag == 0x4d )
2184 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2188 memcpy( psz_name, pE->i_event_name, pE->i_event_name_length);
2189 psz_name[pE->i_event_name_length] = '\0';
2190 memcpy( psz_text, pE->i_text, pE->i_text_length );
2191 psz_text[pE->i_text_length] = '\0';
2193 EITEventFixString(psz_name);
2194 EITEventFixString(psz_text);
2195 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
2196 pE->i_iso_639_code, psz_name, psz_text );
2199 else if( p_dr->i_tag == 0x4e )
2201 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
2208 msg_Dbg( p_demux, " - extended event lang=%3.3s",
2209 pE->i_iso_639_code );
2210 for( i = 0; i < pE->i_entry_count; i++ )
2212 memcpy( str1, pE->i_item_description[i],
2213 pE->i_item_description_length[i] );
2214 str1[pE->i_item_description_length[i]] = '\0';
2215 EITEventFixString(str1);
2217 memcpy( str2, pE->i_item[i],
2218 pE->i_item_length[i] );
2219 str2[pE->i_item_length[i]] = '\0';
2220 EITEventFixString(str2);
2222 msg_Dbg( p_demux, " - desc='%s' item='%s'", str1, str2 );
2223 psz_extra = realloc( psz_extra,
2225 strlen(str1) +strlen(str2) + 1 + 3 );
2226 strcat( psz_extra, str1 );
2227 strcat( psz_extra, "(" );
2228 strcat( psz_extra, str2 );
2229 strcat( psz_extra, ") " );
2232 memcpy( str1, pE->i_text, pE->i_text_length );
2233 str1[pE->i_text_length] = '\0';
2234 EITEventFixString(str1);
2236 msg_Dbg( p_demux, " - text='%s'", str1 );
2237 psz_extra = realloc( psz_extra,
2238 strlen(psz_extra) + strlen(str1) + 2 );
2239 strcat( psz_extra, str1 );
2240 strcat( psz_extra, " " );
2245 msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
2249 asprintf( &psz_value, "%s: %s (+%s) %s (%s)",
2253 psz_text, psz_extra );
2254 vlc_meta_Add( p_meta, psz_cat, psz_value );
2257 if( p_evt->i_running_status == 0x04 )
2259 vlc_meta_Add( p_meta, VLC_META_NOW_PLAYING, psz_name );
2260 b_event_active = VLC_TRUE;
2267 if( !b_event_active )
2268 vlc_meta_Add( p_meta, VLC_META_NOW_PLAYING, "" );
2269 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2270 p_eit->i_service_id, p_meta );
2271 vlc_meta_Delete( p_meta );
2273 dvbpsi_DeleteEIT( p_eit );
2276 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
2277 uint8_t i_table_id, uint16_t i_extension )
2280 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2281 i_table_id, i_table_id, i_extension, i_extension );
2284 if( i_table_id == 0x42 )
2286 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2287 i_table_id, i_table_id, i_extension, i_extension );
2289 dvbpsi_AttachSDT( h, i_table_id, i_extension,
2290 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
2292 else if( i_table_id == 0x4e || /* Current/Following */
2293 ( i_table_id >= 0x50 && i_table_id <= 0x5f ) ) /* Schedule */
2295 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2296 i_table_id, i_table_id, i_extension, i_extension );
2298 dvbpsi_AttachEIT( h, i_table_id, i_extension,
2299 (dvbpsi_eit_callback)EITCallBack, p_demux );
2304 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
2306 demux_sys_t *p_sys = p_demux->p_sys;
2307 dvbpsi_descriptor_t *p_dr;
2308 dvbpsi_pmt_es_t *p_es;
2310 ts_pid_t *pmt = NULL;
2311 ts_prg_psi_t *prg = NULL;
2313 ts_pid_t **pp_clean = NULL;
2316 msg_Dbg( p_demux, "PMTCallBack called" );
2318 /* First find this PMT declared in PAT */
2319 for( i = 0; i < p_sys->i_pmt; i++ )
2322 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2324 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
2325 p_pmt->i_program_number )
2327 pmt = p_sys->pmt[i];
2328 prg = p_sys->pmt[i]->psi->prg[i_prg];
2337 msg_Warn( p_demux, "unreferenced program (broken stream)" );
2338 dvbpsi_DeletePMT(p_pmt);
2342 if( prg->i_version != -1 &&
2343 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
2345 dvbpsi_DeletePMT( p_pmt );
2349 /* Clean this program (remove all es) */
2350 for( i = 0; i < 8192; i++ )
2352 ts_pid_t *pid = &p_sys->pid[i];
2354 if( pid->b_valid && pid->p_owner == pmt->psi &&
2355 pid->i_owner_number == prg->i_number && pid->psi == NULL )
2357 TAB_APPEND( i_clean, pp_clean, pid );
2362 IODFree( prg->iod );
2366 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
2367 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
2368 prg->i_pid_pcr = p_pmt->i_pcr_pid;
2369 prg->i_version = p_pmt->i_version;
2371 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2373 /* Set demux filter */
2374 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2375 ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
2378 else if ( p_sys->b_dvb_control )
2380 msg_Warn( p_demux, "skipping program (not selected)" );
2381 dvbpsi_DeletePMT(p_pmt);
2385 /* Parse descriptor */
2386 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
2388 if( p_dr->i_tag == 0x1d )
2390 /* We have found an IOD descriptor */
2391 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
2393 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
2395 else if( p_dr->i_tag == 0x9 )
2397 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2399 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
2403 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2407 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2409 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
2411 /* Find out if the PID was already declared */
2412 for( i = 0; i < i_clean; i++ )
2414 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
2416 old_pid = pp_clean[i];
2421 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
2423 msg_Warn( p_demux, "pmt error: pid=%d already defined",
2428 PIDInit( pid, VLC_FALSE, pmt->psi );
2429 PIDFillFormat( pid, p_es->i_type );
2430 pid->i_owner_number = prg->i_number;
2431 pid->i_pid = p_es->i_pid;
2432 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
2434 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2435 p_es->i_type == 0x12 )
2437 /* MPEG-4 stream: search SL_DESCRIPTOR */
2438 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2440 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2442 if( p_dr && p_dr->i_length == 2 )
2444 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2446 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2448 pid->es->p_mpeg4desc = NULL;
2450 for( i = 0; i < 255; i++ )
2452 iod_descriptor_t *iod = prg->iod;
2454 if( iod->es_descr[i].b_ok &&
2455 iod->es_descr[i].i_es_id == i_es_id )
2457 pid->es->p_mpeg4desc = &iod->es_descr[i];
2463 if( pid->es->p_mpeg4desc != NULL )
2465 decoder_config_descriptor_t *dcd =
2466 &pid->es->p_mpeg4desc->dec_descr;
2468 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2470 pid->es->fmt.i_cat = VIDEO_ES;
2471 switch( dcd->i_objectTypeIndication )
2473 case 0x0B: /* mpeg4 sub */
2474 pid->es->fmt.i_cat = SPU_ES;
2475 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2478 case 0x20: /* mpeg4 */
2479 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2481 case 0x21: /* h264 */
2482 pid->es->fmt.i_codec = VLC_FOURCC('h','2','6','4');
2489 case 0x65: /* mpeg2 */
2490 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2492 case 0x6a: /* mpeg1 */
2493 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2495 case 0x6c: /* mpeg1 */
2496 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2499 pid->es->fmt.i_cat = UNKNOWN_ES;
2503 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2505 pid->es->fmt.i_cat = AUDIO_ES;
2506 switch( dcd->i_objectTypeIndication )
2508 case 0x40: /* mpeg4 */
2509 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2513 case 0x68: /* mpeg2 aac */
2514 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2516 case 0x69: /* mpeg2 */
2517 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2519 case 0x6b: /* mpeg1 */
2520 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2523 pid->es->fmt.i_cat = UNKNOWN_ES;
2529 pid->es->fmt.i_cat = UNKNOWN_ES;
2532 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2534 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2535 if( pid->es->fmt.i_extra > 0 )
2537 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2538 memcpy( pid->es->fmt.p_extra,
2539 dcd->p_decoder_specific_info,
2540 pid->es->fmt.i_extra );
2545 else if( p_es->i_type == 0x06 )
2547 dvbpsi_descriptor_t *p_dr;
2549 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2550 p_dr = p_dr->p_next )
2552 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
2553 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2555 if( p_dr->i_tag == 0x05 )
2557 /* Registration Descriptor */
2558 if( p_dr->i_length != 4 )
2560 msg_Warn( p_demux, "invalid Registration Descriptor" );
2564 if( !memcmp( p_dr->p_data, "AC-3", 4 ) )
2566 /* ATSC with stream_type 0x81 (but this descriptor
2567 * is then not mandatory */
2568 pid->es->fmt.i_cat = AUDIO_ES;
2569 pid->es->fmt.i_codec = VLC_FOURCC('a','5','2',' ');
2571 else if( !memcmp( p_dr->p_data, "DTS1", 4 ) ||
2572 !memcmp( p_dr->p_data, "DTS2", 4 ) ||
2573 !memcmp( p_dr->p_data, "DTS3", 4 ) )
2575 /*registration descriptor(ETSI TS 101 154 Annex F)*/
2576 pid->es->fmt.i_cat = AUDIO_ES;
2577 pid->es->fmt.i_codec = VLC_FOURCC('d','t','s',' ');
2579 else if( !memcmp( p_dr->p_data, "BSSD", 4 ) )
2581 pid->es->fmt.i_cat = AUDIO_ES;
2582 pid->es->fmt.i_codec = VLC_FOURCC('l','p','c','m');
2587 "unknown Registration Descriptor (%4.4s)",
2593 else if( p_dr->i_tag == 0x6a )
2595 /* DVB with stream_type 0x06 */
2596 pid->es->fmt.i_cat = AUDIO_ES;
2597 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2599 else if( p_dr->i_tag == 0x73 )
2601 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2602 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2603 pid->es->fmt.i_cat = AUDIO_ES;
2604 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2606 else if( p_dr->i_tag == 0x45 )
2608 msg_Dbg( p_demux, " * VBI Data descriptor" );
2609 /* FIXME : store the information somewhere */
2611 else if( p_dr->i_tag == 0x46 )
2613 msg_Dbg( p_demux, " * VBI Teletext descriptor" );
2614 /* FIXME : store the information somewhere */
2616 else if( p_dr->i_tag == 0x56 )
2618 msg_Dbg( p_demux, " * EBU Teletext descriptor" );
2619 pid->es->fmt.i_cat = SPU_ES;
2620 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
2621 pid->es->fmt.psz_description = strdup( "Teletext" );
2622 pid->es->fmt.i_extra = p_dr->i_length;
2623 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2624 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2627 #ifdef _DVBPSI_DR_59_H_
2628 else if( p_dr->i_tag == 0x59 )
2631 dvbpsi_subtitling_dr_t *sub;
2634 pid->es->fmt.i_cat = SPU_ES;
2635 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
2636 pid->es->fmt.i_group = p_pmt->i_program_number;
2638 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
2639 if( !sub ) continue;
2641 /* Each subtitle ES contains n languages,
2642 * We are going to create n ES for the n tracks */
2643 if( sub->i_subtitles_number > 0 )
2645 pid->es->fmt.psz_language = malloc( 4 );
2646 memcpy( pid->es->fmt.psz_language,
2647 sub->p_subtitle[0].i_iso6392_language_code, 3);
2648 pid->es->fmt.psz_language[3] = 0;
2650 pid->es->fmt.subs.dvb.i_id =
2651 sub->p_subtitle[0].i_composition_page_id;
2653 pid->es->fmt.subs.dvb.i_id |=
2654 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
2656 else pid->es->fmt.i_cat = UNKNOWN_ES;
2658 for( n = 1; n < sub->i_subtitles_number; n++ )
2660 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
2661 p_es->fmt = pid->es->fmt;
2664 p_es->i_pes_size = 0;
2665 p_es->i_pes_gathered = 0;
2666 p_es->pp_last = &p_es->p_pes;
2667 p_es->p_mpeg4desc = NULL;
2669 p_es->fmt.psz_language = malloc( 4 );
2670 memcpy( p_es->fmt.psz_language,
2671 sub->p_subtitle[n].i_iso6392_language_code, 3);
2672 p_es->fmt.psz_language[3] = 0;
2674 p_es->fmt.subs.dvb.i_id =
2675 sub->p_subtitle[n].i_composition_page_id;
2677 p_es->fmt.subs.dvb.i_id |=
2678 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
2680 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
2683 #endif /* _DVBPSI_DR_59_H_ */
2686 else if( p_es->i_type == 0xa0 )
2688 /* MSCODEC sent by vlc */
2689 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2691 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
2693 if( p_dr && p_dr->i_length >= 8 )
2695 pid->es->fmt.i_cat = VIDEO_ES;
2696 pid->es->fmt.i_codec =
2697 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
2698 p_dr->p_data[2], p_dr->p_data[3] );
2699 pid->es->fmt.video.i_width =
2700 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
2701 pid->es->fmt.video.i_height =
2702 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
2703 pid->es->fmt.i_extra =
2704 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
2706 if( pid->es->fmt.i_extra > 0 )
2708 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2709 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
2710 pid->es->fmt.i_extra );
2715 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
2718 /* For such stream we will gather them ourself and don't launch a
2720 * Yes it's ugly but it's the only way to have DIV3 working */
2721 pid->es->fmt.b_packetized = VLC_TRUE;
2724 if( pid->es->fmt.i_cat == AUDIO_ES ||
2725 ( pid->es->fmt.i_cat == SPU_ES &&
2726 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
2728 /* get language descriptor */
2729 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2730 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
2734 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
2738 pid->es->fmt.psz_language = malloc( 4 );
2739 memcpy( pid->es->fmt.psz_language,
2740 p_decoded->i_iso_639_code, 3 );
2741 pid->es->fmt.psz_language[3] = 0;
2746 pid->es->fmt.i_group = p_pmt->i_program_number;
2747 if( pid->es->fmt.i_cat == UNKNOWN_ES )
2749 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
2750 p_es->i_pid, p_es->i_type );
2752 else if( !p_sys->b_udp_out )
2754 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
2755 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
2757 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
2759 /* Check if we can avoid restarting the ES */
2761 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
2762 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
2763 pid->es->fmt.i_extra == 0 &&
2764 pid->i_extra_es == old_pid->i_extra_es &&
2765 ( ( !pid->es->fmt.psz_language &&
2766 !old_pid->es->fmt.psz_language ) ||
2767 ( pid->es->fmt.psz_language &&
2768 old_pid->es->fmt.psz_language &&
2769 !strcmp( pid->es->fmt.psz_language,
2770 old_pid->es->fmt.psz_language ) ) ) )
2772 pid->es->id = old_pid->es->id;
2773 old_pid->es->id = NULL;
2774 for( i = 0; i < pid->i_extra_es; i++ )
2776 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
2777 old_pid->extra_es[i]->id = NULL;
2784 PIDClean( p_demux->out, old_pid );
2785 TAB_REMOVE( i_clean, pp_clean, old_pid );
2789 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
2790 for( i = 0; i < pid->i_extra_es; i++ )
2792 pid->extra_es[i]->id =
2793 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
2798 /* Add ES to the list */
2801 PIDClean( p_demux->out, old_pid );
2802 TAB_REMOVE( i_clean, pp_clean, old_pid );
2804 p_sys->pid[p_es->i_pid] = *pid;
2806 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2807 p_dr = p_dr->p_next )
2809 if( p_dr->i_tag == 0x9 )
2811 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2813 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
2818 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2820 /* Set demux filter */
2821 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2822 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
2827 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2829 /* Set CAM descrambling */
2830 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2831 ACCESS_SET_PRIVATE_ID_CA, p_pmt );
2835 dvbpsi_DeletePMT( p_pmt );
2838 for ( i = 0; i < i_clean; i++ )
2840 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2842 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2843 ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
2847 PIDClean( p_demux->out, pp_clean[i] );
2849 if( i_clean ) free( pp_clean );
2852 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
2854 demux_sys_t *p_sys = p_demux->p_sys;
2855 dvbpsi_pat_program_t *p_program;
2856 ts_pid_t *pat = &p_sys->pid[0];
2859 msg_Dbg( p_demux, "PATCallBack called" );
2861 if( pat->psi->i_pat_version != -1 &&
2862 ( !p_pat->b_current_next ||
2863 p_pat->i_version == pat->psi->i_pat_version ) )
2865 dvbpsi_DeletePAT( p_pat );
2869 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
2870 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
2873 if( p_sys->i_pmt > 0 )
2876 ts_pid_t **pmt_rm = NULL;
2878 /* Search pmt to be deleted */
2879 for( i = 0; i < p_sys->i_pmt; i++ )
2881 ts_pid_t *pmt = p_sys->pmt[i];
2882 vlc_bool_t b_keep = VLC_FALSE;
2884 for( p_program = p_pat->p_first_program; p_program != NULL;
2885 p_program = p_program->p_next )
2887 if( p_program->i_pid == pmt->i_pid )
2890 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2892 if( p_program->i_number ==
2893 pmt->psi->prg[i_prg]->i_number )
2905 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
2909 /* Delete all ES attached to thoses PMT */
2910 for( i = 2; i < 8192; i++ )
2912 ts_pid_t *pid = &p_sys->pid[i];
2914 if( !pid->b_valid || pid->psi ) continue;
2916 for( j = 0; j < i_pmt_rm; j++ )
2919 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
2921 /* We only remove es that aren't defined by extra pmt */
2922 if( pid->p_owner->prg[i_prg]->i_pid_pmt !=
2923 pmt_rm[j]->i_pid ) continue;
2925 if( p_sys->b_dvb_control && pid->es->id )
2927 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2928 ACCESS_SET_PRIVATE_ID_STATE, i,
2930 p_sys->b_dvb_control = VLC_FALSE;
2933 PIDClean( p_demux->out, pid );
2937 if( !pid->b_valid ) break;
2941 /* Delete PMT pid */
2942 for( i = 0; i < i_pmt_rm; i++ )
2945 if( p_sys->b_dvb_control )
2947 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2948 ACCESS_SET_PRIVATE_ID_STATE,
2949 pmt_rm[i]->i_pid, VLC_FALSE ) )
2950 p_sys->b_dvb_control = VLC_FALSE;
2953 for( i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
2955 const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
2957 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
2960 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
2961 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
2964 if( pmt_rm ) free( pmt_rm );
2967 /* now create programs */
2968 for( p_program = p_pat->p_first_program; p_program != NULL;
2969 p_program = p_program->p_next )
2971 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
2973 if( p_program->i_number != 0 )
2975 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
2976 vlc_bool_t b_add = VLC_TRUE;
2981 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2983 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
2992 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
2997 PIDInit( pmt, VLC_TRUE, pat->psi );
2998 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
2999 dvbpsi_AttachPMT( p_program->i_number,
3000 (dvbpsi_pmt_callback)PMTCallBack,
3002 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
3003 p_program->i_number;
3004 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
3007 /* Now select PID at access level */
3008 if( p_sys->b_dvb_control )
3010 if( DVBProgramIsSelected( p_demux, p_program->i_number ) )
3012 if( p_sys->i_dvb_program == 0 )
3013 p_sys->i_dvb_program = p_program->i_number;
3015 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
3016 p_sys->b_dvb_control = VLC_FALSE;
3020 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_FALSE ) )
3021 p_sys->b_dvb_control = VLC_FALSE;
3027 pat->psi->i_pat_version = p_pat->i_version;
3029 dvbpsi_DeletePAT( p_pat );