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() */
30 #include <vlc/input.h>
35 #include "../mux/mpeg/csa.h"
37 /* Include dvbpsi headers */
38 #ifdef HAVE_DVBPSI_DR_H
39 # include <dvbpsi/dvbpsi.h>
40 # include <dvbpsi/demux.h>
41 # include <dvbpsi/descriptor.h>
42 # include <dvbpsi/pat.h>
43 # include <dvbpsi/pmt.h>
44 # include <dvbpsi/sdt.h>
45 # include <dvbpsi/dr.h>
46 # include <dvbpsi/psi.h>
50 # include "descriptor.h"
51 # include "tables/pat.h"
52 # include "tables/pmt.h"
53 # include "tables/sdt.h"
54 # include "descriptors/dr.h"
59 #ifdef _DVBPSI_DR_4D_H_
60 # define TS_USE_DVB_SI 1
61 # ifdef HAVE_DVBPSI_DR_H
62 # include <dvbpsi/eit.h>
64 # include "tables/eit.h"
69 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
74 /*****************************************************************************
76 *****************************************************************************/
77 static int Open ( vlc_object_t * );
78 static void Close ( vlc_object_t * );
80 #define PMT_TEXT N_("Extra PMT")
81 #define PMT_LONGTEXT N_( \
82 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])" )
84 #define PID_TEXT N_("Set id of ES to PID")
85 #define PID_LONGTEXT N_("set id of es to pid")
87 #define TSOUT_TEXT N_("Fast udp streaming")
88 #define TSOUT_LONGTEXT N_( \
89 "Sends TS to specific ip:port by udp (you must know what you are doing)")
91 #define MTUOUT_TEXT N_("MTU for out mode")
92 #define MTUOUT_LONGTEXT N_("MTU for out mode")
94 #define CSA_TEXT N_("CSA ck")
95 #define CSA_LONGTEXT N_("CSA ck")
97 #define SILENT_TEXT N_("Silent mode")
98 #define SILENT_LONGTEXT N_("do not complain on encrypted PES")
100 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
101 #define CAPMT_SYSID_LONGTEXT N_("only forward descriptors from this SysID to the CAM")
104 set_description( _("MPEG Transport Stream demuxer") );
105 set_shortname ( "MPEG-TS" );
106 set_category( CAT_INPUT );
107 set_subcategory( SUBCAT_INPUT_DEMUX );
109 add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
110 add_bool( "ts-es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
111 add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
112 add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
113 MTUOUT_LONGTEXT, VLC_TRUE );
114 add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
115 add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );
117 set_capability( "demux2", 10 );
118 set_callbacks( Open, Close );
119 add_shortcut( "ts" );
122 /*****************************************************************************
124 *****************************************************************************/
128 uint8_t i_objectTypeIndication;
129 uint8_t i_streamType;
130 vlc_bool_t b_upStream;
131 uint32_t i_bufferSizeDB;
132 uint32_t i_maxBitrate;
133 uint32_t i_avgBitrate;
135 int i_decoder_specific_info_len;
136 uint8_t *p_decoder_specific_info;
138 } decoder_config_descriptor_t;
142 vlc_bool_t b_useAccessUnitStartFlag;
143 vlc_bool_t b_useAccessUnitEndFlag;
144 vlc_bool_t b_useRandomAccessPointFlag;
145 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
146 vlc_bool_t b_usePaddingFlag;
147 vlc_bool_t b_useTimeStampsFlags;
148 vlc_bool_t b_useIdleFlag;
149 vlc_bool_t b_durationFlag;
150 uint32_t i_timeStampResolution;
151 uint32_t i_OCRResolution;
152 uint8_t i_timeStampLength;
155 uint8_t i_instantBitrateLength;
156 uint8_t i_degradationPriorityLength;
157 uint8_t i_AU_seqNumLength;
158 uint8_t i_packetSeqNumLength;
160 uint32_t i_timeScale;
161 uint16_t i_accessUnitDuration;
162 uint16_t i_compositionUnitDuration;
164 uint64_t i_startDecodingTimeStamp;
165 uint64_t i_startCompositionTimeStamp;
167 } sl_config_descriptor_t;
174 vlc_bool_t b_streamDependenceFlag;
175 vlc_bool_t b_OCRStreamFlag;
176 uint8_t i_streamPriority;
180 uint16_t i_dependOn_es_id;
181 uint16_t i_OCR_es_id;
183 decoder_config_descriptor_t dec_descr;
184 sl_config_descriptor_t sl_descr;
186 } es_mpeg4_descriptor_t;
190 uint8_t i_iod_label, i_iod_label_scope;
196 uint8_t i_ODProfileLevelIndication;
197 uint8_t i_sceneProfileLevelIndication;
198 uint8_t i_audioProfileLevelIndication;
199 uint8_t i_visualProfileLevelIndication;
200 uint8_t i_graphicsProfileLevelIndication;
202 es_mpeg4_descriptor_t es_descr[255];
208 dvbpsi_handle handle;
214 /* IOD stuff (mpeg4) */
215 iod_descriptor_t *iod;
221 demux_t *p_demux; /* Hack */
229 /* for special PAT/SDT case */
230 dvbpsi_handle handle; /* PAT/SDT/EIT */
253 es_mpeg4_descriptor_t *p_mpeg4desc;
264 int i_cc; /* countinuity counter */
266 /* PSI owner (ie PMT -> PAT, ES -> PMT */
274 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
282 /* TS packet size (188, 192, 204) */
285 /* how many TS packet we read at once */
296 vlc_bool_t b_es_id_pid;
300 vlc_bool_t b_udp_out;
301 int fd; /* udp socket */
304 vlc_bool_t b_dvb_control;
306 vlc_list_t *p_programs_list;
312 static int Demux ( demux_t *p_demux );
313 static int Control( demux_t *p_demux, int i_query, va_list args );
316 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
317 static void PIDClean( es_out_t *out, ts_pid_t *pid );
318 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
320 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
321 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
323 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
324 uint8_t i_table_id, uint16_t i_extension );
328 static inline int PIDGet( block_t *p )
330 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
333 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
335 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
337 static iod_descriptor_t *IODNew( int , uint8_t * );
338 static void IODFree( iod_descriptor_t * );
340 #define TS_PACKET_SIZE_188 188
341 #define TS_PACKET_SIZE_192 192
342 #define TS_PACKET_SIZE_204 204
343 #define TS_PACKET_SIZE_MAX 204
345 /*****************************************************************************
347 *****************************************************************************/
348 static int Open( vlc_object_t *p_this )
350 demux_t *p_demux = (demux_t*)p_this;
354 int i_sync, i_peek, i;
361 if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
362 TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
364 /* Search first sync byte */
365 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
367 if( p_peek[i_sync] == 0x47 ) break;
369 if( i_sync >= TS_PACKET_SIZE_MAX )
371 if( strcmp( p_demux->psz_demux, "ts" ) ) return VLC_EGENERIC;
372 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
375 /* Check next 3 sync bytes */
376 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
377 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
379 msg_Err( p_demux, "cannot peek" );
382 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
383 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
384 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
386 i_packet_size = TS_PACKET_SIZE_188;
388 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
389 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
390 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
392 i_packet_size = TS_PACKET_SIZE_192;
394 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
395 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
396 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
398 i_packet_size = TS_PACKET_SIZE_204;
400 else if( !strcmp( p_demux->psz_demux, "ts" ) )
402 i_packet_size = TS_PACKET_SIZE_188;
406 msg_Warn( p_demux, "TS module discarded (lost sync)" );
410 /* Fill p_demux field */
411 p_demux->pf_demux = Demux;
412 p_demux->pf_control = Control;
413 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
414 memset( p_sys, 0, sizeof( demux_sys_t ) );
416 /* Init p_sys field */
417 p_sys->b_meta = VLC_TRUE;
418 p_sys->b_dvb_control = VLC_TRUE;
419 p_sys->i_dvb_program = 0;
420 for( i = 0; i < 8192; i++ )
422 ts_pid_t *pid = &p_sys->pid[i];
425 pid->b_seen = VLC_FALSE;
426 pid->b_valid = VLC_FALSE;
428 p_sys->i_packet_size = i_packet_size;
429 p_sys->b_udp_out = VLC_FALSE;
430 p_sys->i_ts_read = 50;
433 /* Init PAT handler */
434 pat = &p_sys->pid[0];
435 PIDInit( pat, VLC_TRUE, NULL );
436 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
441 ts_pid_t *sdt = &p_sys->pid[0x11];
442 ts_pid_t *eit = &p_sys->pid[0x12];
444 PIDInit( sdt, VLC_TRUE, NULL );
445 sdt->psi->handle = dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
447 PIDInit( eit, VLC_TRUE, NULL );
448 eit->psi->handle = dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
450 if( p_sys->b_dvb_control )
452 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
453 ACCESS_SET_PRIVATE_ID_STATE, 0x11, VLC_TRUE );
454 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
455 ACCESS_SET_PRIVATE_ID_STATE, 0x12, VLC_TRUE );
465 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
466 var_Get( p_demux, "ts-es-id-pid", &val );
467 p_sys->b_es_id_pid = val.b_bool;
469 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
470 var_Get( p_demux, "ts-out", &val );
471 if( val.psz_string && *val.psz_string )
474 char *psz = strchr( val.psz_string, ':' );
477 p_sys->b_udp_out = VLC_TRUE;
482 i_port = atoi( psz );
484 if( i_port <= 0 ) i_port = 1234;
485 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
487 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
490 msg_Err( p_demux, "failed to open udp socket, send disabled" );
491 p_sys->b_udp_out = VLC_FALSE;
495 var_Create( p_demux, "ts-out-mtu",
496 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
497 var_Get( p_demux, "ts-out-mtu", &mtu );
498 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
499 if( p_sys->i_ts_read <= 0 )
501 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
503 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
508 free( val.psz_string );
512 /* We handle description of an extra PMT */
513 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
514 var_Get( p_demux, "ts-extra-pmt", &val );
515 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
517 char *psz = val.psz_string;
518 int i_pid = strtol( psz, &psz, 0 );
520 if( i_pid >= 2 && i_pid < 8192 )
522 ts_pid_t *pmt = &p_sys->pid[i_pid];
524 msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
525 PIDInit( pmt, VLC_TRUE, NULL );
527 pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
528 /* FIXME we should also ask for a number */
529 pmt->psi->prg[0]->handle =
530 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
532 pmt->psi->prg[0]->i_number = 0; /* special one */
534 psz = strchr( psz, '=' ) + 1; /* can't failed */
537 char *psz_next = strchr( psz, ',' );
538 int i_pid, i_stream_type;
545 i_pid = strtol( psz, &psz, 0 );
548 i_stream_type = strtol( psz + 1, &psz, 0 );
549 if( i_pid >= 2 && i_pid < 8192 &&
550 !p_sys->pid[i_pid].b_valid )
552 ts_pid_t *pid = &p_sys->pid[i_pid];
554 PIDInit( pid, VLC_FALSE, pmt->psi);
555 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
557 pmt->psi->prg[0]->i_pid_pcr = i_pid;
559 PIDFillFormat( pid, i_stream_type);
560 if( pid->es->fmt.i_cat != UNKNOWN_ES )
562 if( p_sys->b_es_id_pid )
564 pid->es->fmt.i_id = i_pid;
566 msg_Dbg( p_demux, " * es pid=%d type=%d "
567 "fcc=%4.4s", i_pid, i_stream_type,
568 (char*)&pid->es->fmt.i_codec );
569 pid->es->id = es_out_Add( p_demux->out,
580 free( val.psz_string );
583 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
584 var_Get( p_demux, "ts-csa-ck", &val );
585 if( val.psz_string && *val.psz_string )
587 char *psz = val.psz_string;
588 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
592 if( strlen( psz ) != 16 )
594 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
599 uint64_t i_ck = strtoull( psz, NULL, 16 );
601 uint64_t i_ck = strtoll( psz, NULL, 16 );
605 for( i = 0; i < 8; i++ )
607 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
610 msg_Dbg( p_demux, "using CSA scrambling with "
611 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
612 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
614 p_sys->csa = csa_New();
615 csa_SetCW( p_sys->csa, ck, ck );
620 free( val.psz_string );
623 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
624 var_Get( p_demux, "ts-silent", &val );
625 p_sys->b_silent = val.b_bool;
630 /*****************************************************************************
632 *****************************************************************************/
633 static void Close( vlc_object_t *p_this )
635 demux_t *p_demux = (demux_t*)p_this;
636 demux_sys_t *p_sys = p_demux->p_sys;
640 msg_Dbg( p_demux, "pid list:" );
641 for( i = 0; i < 8192; i++ )
643 ts_pid_t *pid = &p_sys->pid[i];
645 if( pid->b_valid && pid->psi )
651 dvbpsi_DetachPAT( pid->psi->handle );
658 dvbpsi_DetachDemux( pid->psi->handle );
662 dvbpsi_DetachDemux( pid->psi->handle );
663 for( j = 0; j < pid->psi->i_eit; j++ )
665 free( pid->psi->eit[j] );
667 if( pid->psi->i_eit )
668 free( pid->psi->eit );
672 PIDClean( p_demux->out, pid );
676 else if( pid->b_valid && pid->es )
678 PIDClean( p_demux->out, pid );
683 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
686 if( p_sys->b_dvb_control && pid->i_pid > 0 )
689 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
694 if( p_sys->b_udp_out )
696 net_Close( p_sys->fd );
697 free( p_sys->buffer );
701 csa_Delete( p_sys->csa );
704 if( p_sys->i_pmt ) free( p_sys->pmt );
706 if ( p_sys->p_programs_list )
709 val.p_list = p_sys->p_programs_list;
710 var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
716 /*****************************************************************************
718 *****************************************************************************/
719 static int Demux( demux_t *p_demux )
721 demux_sys_t *p_sys = p_demux->p_sys;
724 /* We read at most 100 TS packet or until a frame is completed */
725 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
727 vlc_bool_t b_frame = VLC_FALSE;
731 /* Get a new TS packet */
732 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
734 msg_Dbg( p_demux, "eof ?" );
738 /* Check sync byte and re-sync if needed */
739 if( p_pkt->p_buffer[0] != 0x47 )
741 msg_Warn( p_demux, "lost synchro" );
742 block_Release( p_pkt );
744 while( !p_demux->b_die )
747 int i_peek, i_skip = 0;
749 i_peek = stream_Peek( p_demux->s, &p_peek,
750 p_sys->i_packet_size * 10 );
751 if( i_peek < p_sys->i_packet_size + 1 )
753 msg_Dbg( p_demux, "eof ?" );
757 while( i_skip < i_peek - p_sys->i_packet_size )
759 if( p_peek[i_skip] == 0x47 &&
760 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
767 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
768 stream_Read( p_demux->s, NULL, i_skip );
770 if( i_skip < i_peek - p_sys->i_packet_size )
776 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
778 msg_Dbg( p_demux, "eof ?" );
783 if( p_sys->b_udp_out )
785 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
786 p_pkt->p_buffer, p_sys->i_packet_size );
789 /* Parse the TS packet */
790 p_pid = &p_sys->pid[PIDGet( p_pkt )];
796 if( p_pid->i_pid == 0 || p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 )
798 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
803 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
805 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
809 block_Release( p_pkt );
811 else if( !p_sys->b_udp_out )
813 b_frame = GatherPES( p_demux, p_pid, p_pkt );
817 PCRHandle( p_demux, p_pid, p_pkt );
818 block_Release( p_pkt );
825 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
827 /* We have to handle PCR if present */
828 PCRHandle( p_demux, p_pid, p_pkt );
829 block_Release( p_pkt );
831 p_pid->b_seen = VLC_TRUE;
839 if( p_sys->b_udp_out )
841 /* Send the complete block */
842 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
843 p_sys->i_ts_read * p_sys->i_packet_size );
849 /*****************************************************************************
851 *****************************************************************************/
852 static int Control( demux_t *p_demux, int i_query, va_list args )
854 demux_sys_t *p_sys = p_demux->p_sys;
861 case DEMUX_GET_POSITION:
862 pf = (double*) va_arg( args, double* );
863 i64 = stream_Size( p_demux->s );
866 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
873 case DEMUX_SET_POSITION:
874 f = (double) va_arg( args, double );
875 i64 = stream_Size( p_demux->s );
877 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
878 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
886 pi64 = (int64_t*)va_arg( args, int64_t * );
887 if( p_sys->i_time < 0 )
892 *pi64 = p_sys->i_time;
895 case DEMUX_GET_LENGTH:
896 pi64 = (int64_t*)va_arg( args, int64_t * );
897 if( p_sys->i_mux_rate > 0 )
899 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
906 case DEMUX_SET_GROUP:
908 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
909 ts_prg_psi_t *p_prg = NULL;
912 i_int = (int)va_arg( args, int );
913 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
914 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
916 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
921 /* Search pmt to be unselected */
922 for( i = 0; i < p_sys->i_pmt; i++ )
924 ts_pid_t *pmt = p_sys->pmt[i];
927 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
929 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
931 i_pmt_pid = p_sys->pmt[i]->i_pid;
935 if( i_pmt_pid > 0 ) break;
940 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
941 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
944 for( i = 2; i < 8192; i++ )
946 ts_pid_t *pid = &p_sys->pid[i];
949 if( !pid->b_valid || pid->psi ) continue;
951 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
953 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
955 /* We only remove es that aren't defined by extra pmt */
956 stream_Control( p_demux->s,
957 STREAM_CONTROL_ACCESS,
958 ACCESS_SET_PRIVATE_ID_STATE,
966 /* select new program */
967 p_sys->i_dvb_program = i_int;
969 for( i = 0; i < p_sys->i_pmt; i++ )
971 ts_pid_t *pmt = p_sys->pmt[i];
974 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
976 if( pmt->psi->prg[i_prg]->i_number == i_int )
978 i_pmt_pid = p_sys->pmt[i]->i_pid;
979 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
983 if( i_pmt_pid > 0 ) break;
987 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
988 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
990 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
991 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
994 for( i = 2; i < 8192; i++ )
996 ts_pid_t *pid = &p_sys->pid[i];
999 if( !pid->b_valid || pid->psi ) continue;
1001 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1003 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1005 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
1007 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
1009 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
1011 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
1014 stream_Control( p_demux->s,
1015 STREAM_CONTROL_ACCESS,
1016 ACCESS_SET_PRIVATE_ID_STATE,
1026 p_sys->i_dvb_program = -1;
1027 p_sys->p_programs_list = p_list;
1033 case DEMUX_SET_TIME:
1035 return VLC_EGENERIC;
1039 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
1041 vlc_bool_t b_old_valid = pid->b_valid;
1043 pid->b_valid = VLC_TRUE;
1045 pid->p_owner = p_owner;
1046 pid->i_owner_number = 0;
1048 pid->extra_es = NULL;
1049 pid->i_extra_es = 0;
1057 pid->psi = malloc( sizeof( ts_psi_t ) );
1058 pid->psi->handle= NULL;
1059 pid->psi->i_prg = 0;
1060 pid->psi->prg = NULL;
1061 pid->psi->i_eit = 0;
1062 pid->psi->eit = NULL;
1064 pid->psi->i_pat_version = -1;
1065 pid->psi->i_sdt_version = -1;
1068 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1070 prg->i_version = -1;
1072 prg->i_pid_pcr = -1;
1073 prg->i_pid_pmt = -1;
1077 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1083 pid->es = malloc( sizeof( ts_es_t ) );
1085 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1087 pid->es->p_pes = NULL;
1088 pid->es->i_pes_size= 0;
1089 pid->es->i_pes_gathered= 0;
1090 pid->es->pp_last = &pid->es->p_pes;
1091 pid->es->p_mpeg4desc = NULL;
1092 pid->es->b_gather = VLC_FALSE;
1096 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1102 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1103 for( i = 0; i < pid->psi->i_prg; i++ )
1105 if( pid->psi->prg[i]->iod )
1106 IODFree( pid->psi->prg[i]->iod );
1107 if( pid->psi->prg[i]->handle )
1108 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1109 free( pid->psi->prg[i] );
1111 if( pid->psi->prg ) free( pid->psi->prg );
1119 es_out_Del( out, pid->es->id );
1121 if( pid->es->p_pes )
1122 block_ChainRelease( pid->es->p_pes );
1124 es_format_Clean( &pid->es->fmt );
1128 for( i = 0; i < pid->i_extra_es; i++ )
1130 if( pid->extra_es[i]->id )
1131 es_out_Del( out, pid->extra_es[i]->id );
1133 if( pid->extra_es[i]->p_pes )
1134 block_ChainRelease( pid->extra_es[i]->p_pes );
1136 es_format_Clean( &pid->extra_es[i]->fmt );
1138 free( pid->extra_es[i] );
1140 if( pid->i_extra_es ) free( pid->extra_es );
1143 pid->b_valid = VLC_FALSE;
1146 /****************************************************************************
1148 ****************************************************************************/
1149 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1151 block_t *p_pes = pid->es->p_pes;
1157 mtime_t i_length = 0;
1160 /* remove the pes from pid */
1161 pid->es->p_pes = NULL;
1162 pid->es->i_pes_size= 0;
1163 pid->es->i_pes_gathered= 0;
1164 pid->es->pp_last = &pid->es->p_pes;
1166 /* FIXME find real max size */
1167 i_max = block_ChainExtract( p_pes, header, 30 );
1170 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1172 if( !p_demux->p_sys->b_silent )
1173 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1174 header[0], header[1],header[2],header[3], pid->i_pid );
1175 block_ChainRelease( p_pes );
1179 /* TODO check size */
1182 case 0xBC: /* Program stream map */
1183 case 0xBE: /* Padding */
1184 case 0xBF: /* Private stream 2 */
1185 case 0xB0: /* ECM */
1186 case 0xB1: /* EMM */
1187 case 0xFF: /* Program stream directory */
1188 case 0xF2: /* DSMCC stream */
1189 case 0xF8: /* ITU-T H.222.1 type E stream */
1193 if( ( header[6]&0xC0 ) == 0x80 )
1196 i_skip = header[8] + 9;
1198 if( header[7]&0x80 ) /* has pts */
1200 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1201 (mtime_t)(header[10] << 22)|
1202 ((mtime_t)(header[11]&0xfe) << 14)|
1203 (mtime_t)(header[12] << 7)|
1204 (mtime_t)(header[13] >> 1);
1206 if( header[7]&0x40 ) /* has dts */
1208 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1209 (mtime_t)(header[15] << 22)|
1210 ((mtime_t)(header[16]&0xfe) << 14)|
1211 (mtime_t)(header[17] << 7)|
1212 (mtime_t)(header[18] >> 1);
1219 while( i_skip < 23 && header[i_skip] == 0xff )
1225 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1226 block_ChainRelease( p_pes );
1229 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1234 if( header[i_skip]&0x20 )
1236 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1237 (mtime_t)(header[i_skip+1] << 22)|
1238 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1239 (mtime_t)(header[i_skip+3] << 7)|
1240 (mtime_t)(header[i_skip+4] >> 1);
1242 if( header[i_skip]&0x10 ) /* has dts */
1244 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1245 (mtime_t)(header[i_skip+6] << 22)|
1246 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1247 (mtime_t)(header[i_skip+8] << 7)|
1248 (mtime_t)(header[i_skip+9] >> 1);
1264 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1265 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1269 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1270 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1271 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1275 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1276 pid->es->p_mpeg4desc )
1278 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1280 if( dcd->i_decoder_specific_info_len > 2 &&
1281 dcd->p_decoder_specific_info[0] == 0x10 &&
1282 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1284 /* display length */
1285 if( p_pes->i_buffer + 2 <= i_skip )
1287 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1292 if( p_pes->i_buffer + 2 <= i_skip )
1294 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1301 while( p_pes && i_skip > 0 )
1303 if( p_pes->i_buffer <= i_skip )
1305 block_t *p_next = p_pes->p_next;
1307 i_skip -= p_pes->i_buffer;
1308 block_Release( p_pes );
1313 p_pes->i_buffer -= i_skip;
1314 p_pes->p_buffer += i_skip;
1319 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1320 if( i_pts >= 0 && i_dts < 0 )
1330 p_pes->i_dts = i_dts * 100 / 9;
1334 p_pes->i_pts = i_pts * 100 / 9;
1336 p_pes->i_length = i_length * 100 / 9;
1338 p_block = block_ChainGather( p_pes );
1339 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1341 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1343 p_block->i_buffer = i_pes_size;
1346 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1347 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1350 for( i = 0; i < pid->i_extra_es; i++ )
1352 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1353 block_Duplicate( p_block ) );
1356 es_out_Send( p_demux->out, pid->es->id, p_block );
1360 msg_Warn( p_demux, "empty pes" );
1364 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1366 demux_sys_t *p_sys = p_demux->p_sys;
1367 const uint8_t *p = p_bk->p_buffer;
1369 if( ( p[3]&0x20 ) && /* adaptation */
1374 mtime_t i_pcr; /* 33 bits */
1376 i_pcr = ( (mtime_t)p[6] << 25 ) |
1377 ( (mtime_t)p[7] << 17 ) |
1378 ( (mtime_t)p[8] << 9 ) |
1379 ( (mtime_t)p[9] << 1 ) |
1380 ( (mtime_t)p[10] >> 7 );
1382 /* Search program and set the PCR */
1383 for( i = 0; i < p_sys->i_pmt; i++ )
1386 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1388 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1390 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1391 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1392 (int64_t)(i_pcr * 100 / 9) );
1399 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1401 const uint8_t *p = p_bk->p_buffer;
1402 const vlc_bool_t b_unit_start = p[1]&0x40;
1403 const vlc_bool_t b_adaptation = p[3]&0x20;
1404 const vlc_bool_t b_payload = p[3]&0x10;
1405 const int i_cc = p[3]&0x0f; /* continuity counter */
1407 /* transport_scrambling_control is ignored */
1410 vlc_bool_t i_ret = VLC_FALSE;
1414 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1415 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1419 /* For now, ignore additional error correction
1420 * TODO: handle Reed-Solomon 204,188 error correction */
1421 p_bk->i_buffer = TS_PACKET_SIZE_188;
1425 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1429 if( p_demux->p_sys->csa )
1431 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
1436 /* We don't have any adaptation_field, so payload starts
1437 * immediately after the 4 byte TS header */
1442 /* p[4] is adaptation_field_length minus one */
1448 msg_Warn( p_demux, "discontinuity_indicator (pid=%d) "
1449 "ignored", pid->i_pid );
1454 /* Test continuity counter */
1455 /* continuous when (one of this):
1457 * diff == 0 and payload == 0
1458 * diff == 0 and duplicate packet (playload != 0) <- should we
1459 * test the content ?
1462 i_diff = ( i_cc - pid->i_cc )&0x0f;
1463 if( b_payload && i_diff == 1 )
1469 if( pid->i_cc == 0xff )
1471 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1475 else if( i_diff != 0 )
1477 /* FIXME what to do when discontinuity_indicator is set ? */
1478 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
1479 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
1483 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1485 /* Small video artifacts are usually better then
1486 * dropping full frames */
1487 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1492 PCRHandle( p_demux, pid, p_bk );
1494 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1496 block_Release( p_bk );
1500 /* We have to gather it */
1501 p_bk->p_buffer += i_skip;
1502 p_bk->i_buffer -= i_skip;
1506 if( pid->es->p_pes )
1508 ParsePES( p_demux, pid );
1512 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1513 if( p_bk->i_buffer > 6 )
1515 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1516 if( pid->es->i_pes_size > 0 )
1518 pid->es->i_pes_size += 6;
1521 pid->es->i_pes_gathered += p_bk->i_buffer;
1522 if( pid->es->i_pes_size > 0 &&
1523 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1525 ParsePES( p_demux, pid );
1531 if( pid->es->p_pes == NULL )
1533 /* msg_Dbg( p_demux, "broken packet" ); */
1534 block_Release( p_bk );
1538 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1539 pid->es->i_pes_gathered += p_bk->i_buffer;
1540 if( pid->es->i_pes_size > 0 &&
1541 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1543 ParsePES( p_demux, pid );
1552 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1554 es_format_t *fmt = &pid->es->fmt;
1556 switch( i_stream_type )
1558 case 0x01: /* MPEG-1 video */
1559 case 0x02: /* MPEG-2 video */
1560 case 0x80: /* MPEG-2 MOTO video */
1561 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1563 case 0x03: /* MPEG-1 audio */
1564 case 0x04: /* MPEG-2 audio */
1565 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1567 case 0x11: /* MPEG4 (audio) */
1568 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1569 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1571 case 0x10: /* MPEG4 (video) */
1572 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1573 pid->es->b_gather = VLC_TRUE;
1575 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1576 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1579 case 0x81: /* A52 (audio) */
1580 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1582 case 0x82: /* DVD_SPU (sub) */
1583 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1585 case 0x83: /* LPCM (audio) */
1586 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1588 case 0x84: /* SDDS (audio) */
1589 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1591 case 0x85: /* DTS (audio) */
1592 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1595 case 0x91: /* A52 vls (audio) */
1596 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1598 case 0x92: /* DVD_SPU vls (sub) */
1599 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1601 case 0x93: /* LPCM vls (audio) */
1602 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1604 case 0x94: /* SDDS (audio) */
1605 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1608 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1609 es_format_Init( fmt, UNKNOWN_ES, 0 );
1610 pid->es->b_gather = VLC_TRUE;
1613 case 0x06: /* PES_PRIVATE (fixed later) */
1614 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1616 es_format_Init( fmt, UNKNOWN_ES, 0 );
1620 /* PES packets usually contain truncated frames */
1621 fmt->b_packetized = VLC_FALSE;
1623 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1626 /*****************************************************************************
1627 * MP4 specific functions (IOD parser)
1628 *****************************************************************************/
1629 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1632 unsigned int i_len = 0;
1638 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1640 } while( i_b&0x80 );
1644 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1648 const int i_b = **pp_data;
1655 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1657 const int i1 = IODGetByte( pi_data, pp_data );
1658 const int i2 = IODGetByte( pi_data, pp_data );
1659 return( ( i1 << 8 ) | i2 );
1661 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1663 const int i1 = IODGetByte( pi_data, pp_data );
1664 const int i2 = IODGetByte( pi_data, pp_data );
1665 const int i3 = IODGetByte( pi_data, pp_data );
1667 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1670 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1672 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1673 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1674 return( ( i1 << 16 ) | i2 );
1677 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1682 i_url_len = IODGetByte( pi_data, pp_data );
1683 url = malloc( i_url_len + 1 );
1684 for( i = 0; i < i_url_len; i++ )
1686 url[i] = IODGetByte( pi_data, pp_data );
1688 url[i_url_len] = '\0';
1692 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1694 iod_descriptor_t *p_iod;
1697 uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
1701 p_iod = malloc( sizeof( iod_descriptor_t ) );
1702 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1704 fprintf( stderr, "\n************ IOD ************" );
1705 for( i = 0; i < 255; i++ )
1707 p_iod->es_descr[i].b_ok = 0;
1716 byte1 = IODGetByte( &i_data, &p_data );
1717 byte2 = IODGetByte( &i_data, &p_data );
1718 byte3 = IODGetByte( &i_data, &p_data );
1719 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
1721 p_iod->i_iod_label_scope = 0x11;
1722 p_iod->i_iod_label = byte1;
1725 else //correct implementation of the IOD_descriptor
1727 p_iod->i_iod_label_scope = byte1;
1728 p_iod->i_iod_label = byte2;
1731 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1732 fprintf( stderr, "\n* ===========" );
1733 fprintf( stderr, "\n* tag:0x%x", i_iod_tag );
1735 if( i_iod_tag != 0x02 )
1737 fprintf( stderr, "\n ERR: tag %02x != 0x02", i_iod_tag );
1741 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1742 fprintf( stderr, "\n* length:%d", i_iod_length );
1743 if( i_iod_length > i_data )
1745 i_iod_length = i_data;
1748 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1749 i_flags = IODGetByte( &i_data, &p_data );
1750 p_iod->i_od_id |= i_flags >> 6;
1751 b_url = ( i_flags >> 5 )&0x01;
1753 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1754 fprintf( stderr, "\n* url flag:%d", b_url );
1755 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1759 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1760 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1761 fprintf( stderr, "\n*****************************\n" );
1766 p_iod->psz_url = NULL;
1769 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1770 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1771 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1772 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1773 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1775 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1776 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1777 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1778 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1779 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1782 while( i_data > 0 && i_es_index < 255)
1784 int i_tag, i_length;
1786 uint8_t *p_data_sav;
1788 i_tag = IODGetByte( &i_data, &p_data );
1789 i_length = IODDescriptorLength( &i_data, &p_data );
1791 i_data_sav = i_data;
1792 p_data_sav = p_data;
1800 #define es_descr p_iod->es_descr[i_es_index]
1801 int i_decoderConfigDescr_length;
1802 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1805 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1806 i_flags = IODGetByte( &i_data, &p_data );
1807 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1808 b_url = ( i_flags >> 6 )&0x01;
1809 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1810 es_descr.i_streamPriority = i_flags & 0x1f;
1811 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1812 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1813 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1815 if( es_descr.b_streamDependenceFlag )
1817 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1818 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1823 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1824 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1828 es_descr.psz_url = NULL;
1831 if( es_descr.b_OCRStreamFlag )
1833 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1834 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1837 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1839 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1843 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1845 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1846 #define dec_descr es_descr.dec_descr
1847 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1848 i_flags = IODGetByte( &i_data, &p_data );
1849 dec_descr.i_streamType = i_flags >> 2;
1850 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1851 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1852 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1853 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1854 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1855 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1856 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1857 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1858 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1859 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1860 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1863 dec_descr.i_decoder_specific_info_len =
1864 IODDescriptorLength( &i_data, &p_data );
1865 if( dec_descr.i_decoder_specific_info_len > 0 )
1867 dec_descr.p_decoder_specific_info =
1868 malloc( dec_descr.i_decoder_specific_info_len );
1870 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1872 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1877 dec_descr.i_decoder_specific_info_len = 0;
1878 dec_descr.p_decoder_specific_info = NULL;
1882 #define sl_descr es_descr.sl_descr
1884 int i_SLConfigDescr_length;
1887 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1889 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1893 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1895 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1896 i_predefined = IODGetByte( &i_data, &p_data );
1897 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1898 switch( i_predefined )
1902 sl_descr.b_useAccessUnitStartFlag = 0;
1903 sl_descr.b_useAccessUnitEndFlag = 0;
1904 sl_descr.b_useRandomAccessPointFlag = 0;
1905 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1906 sl_descr.b_usePaddingFlag = 0;
1907 sl_descr.b_useTimeStampsFlags = 0;
1908 sl_descr.b_useIdleFlag = 0;
1909 sl_descr.b_durationFlag = 0; // FIXME FIXME
1910 sl_descr.i_timeStampResolution = 1000;
1911 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1912 sl_descr.i_timeStampLength = 32;
1913 sl_descr.i_OCRLength = 0; // FIXME FIXME
1914 sl_descr.i_AU_Length = 0;
1915 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1916 sl_descr.i_degradationPriorityLength= 0;
1917 sl_descr.i_AU_seqNumLength = 0;
1918 sl_descr.i_packetSeqNumLength = 0;
1919 if( sl_descr.b_durationFlag )
1921 sl_descr.i_timeScale = 0; // FIXME FIXME
1922 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1923 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1925 if( !sl_descr.b_useTimeStampsFlags )
1927 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1928 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1933 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1942 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1946 p_data = p_data_sav + i_length;
1947 i_data = i_data_sav - i_length;
1952 fprintf( stderr, "\n*****************************\n" );
1956 static void IODFree( iod_descriptor_t *p_iod )
1960 if( p_iod->psz_url )
1962 free( p_iod->psz_url );
1963 p_iod->psz_url = NULL;
1968 for( i = 0; i < 255; i++ )
1970 #define es_descr p_iod->es_descr[i]
1973 if( es_descr.psz_url )
1975 free( es_descr.psz_url );
1976 es_descr.psz_url = NULL;
1980 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1982 free( es_descr.dec_descr.p_decoder_specific_info );
1983 es_descr.dec_descr.p_decoder_specific_info = NULL;
1984 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1994 /****************************************************************************
1995 ****************************************************************************
1996 ** libdvbpsi callbacks
1997 ****************************************************************************
1998 ****************************************************************************/
1999 static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2001 demux_sys_t *p_sys = p_demux->p_sys;
2003 if ( !p_sys->b_dvb_control ) return VLC_FALSE;
2004 if ( p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL )
2006 if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
2008 if ( p_sys->p_programs_list != NULL )
2011 for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
2013 if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
2020 #ifdef TS_USE_DVB_SI
2021 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2023 demux_sys_t *p_sys = p_demux->p_sys;
2024 ts_pid_t *sdt = &p_sys->pid[0x11];
2025 dvbpsi_sdt_service_t *p_srv;
2027 msg_Dbg( p_demux, "SDTCallBack called" );
2029 if( sdt->psi->i_sdt_version != -1 &&
2030 ( !p_sdt->b_current_next ||
2031 p_sdt->i_version == sdt->psi->i_sdt_version ) )
2033 dvbpsi_DeleteSDT( p_sdt );
2037 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d network_id=%d",
2038 p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next, p_sdt->i_network_id );
2040 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2042 vlc_meta_t *p_meta = vlc_meta_New();
2043 dvbpsi_descriptor_t *p_dr;
2045 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d runing=%d free_ca=%d",
2046 p_srv->i_service_id,
2047 p_srv->b_eit_schedule, p_srv->b_eit_present, p_srv->i_running_status,
2050 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2052 if( p_dr->i_tag == 0x48 )
2054 static const char *psz_type[0x11] = {
2056 "Digital television service",
2057 "Digital radio sound service",
2059 "NVOD reference service",
2060 "NVOD time-shifted service",
2063 "SECAM coded signal",
2066 "NTSC coded signal",
2067 "Data broadcast service",
2068 "Reserved for Common Interface Usage",
2069 "RCS Map (see EN 301 790 [35])",
2070 "RCS FLS (see EN 301 790 [35])",
2073 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2077 memcpy( str1, pD->i_service_provider_name, pD->i_service_provider_name_length );
2078 str1[pD->i_service_provider_name_length] = '\0';
2079 memcpy( str2, pD->i_service_name, pD->i_service_name_length );
2080 str2[pD->i_service_name_length] = '\0';
2082 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
2083 pD->i_service_type, str1, str2 );
2085 vlc_meta_Add( p_meta, "Name", str2 );
2086 vlc_meta_Add( p_meta, "Provider", str1 );
2087 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2088 vlc_meta_Add( p_meta, "Type", psz_type[pD->i_service_type] );
2092 if( p_srv->i_running_status == 0x01 )
2093 vlc_meta_Add( p_meta, "Status", "Not running" );
2094 else if( p_srv->i_running_status == 0x02 )
2095 vlc_meta_Add( p_meta, "Status", "Starts in a few seconds" );
2096 else if( p_srv->i_running_status == 0x03 )
2097 vlc_meta_Add( p_meta, "Status", "Pausing" );
2098 else if( p_srv->i_running_status == 0x04 )
2099 vlc_meta_Add( p_meta, "Status", "Running" );
2101 vlc_meta_Add( p_meta, "Status", "Unknown" );
2104 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2105 p_srv->i_service_id, p_meta );
2106 vlc_meta_Delete( p_meta );
2109 sdt->psi->i_sdt_version = p_sdt->i_version;
2110 dvbpsi_DeleteSDT( p_sdt );
2113 static void DecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2115 int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2116 int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2118 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2120 if( mp == 14 || mp == 15 )
2133 static void EITCallBack( ts_eit_psi_t *psi, dvbpsi_eit_t *p_eit )
2135 demux_t *p_demux = psi->p_demux;
2137 dvbpsi_eit_event_t *p_evt;
2139 msg_Dbg( p_demux, "EITCallBack called" );
2140 if( psi->i_version != -1 &&
2141 ( !p_eit->b_current_next || psi->i_version == p_eit->i_version ) )
2143 dvbpsi_DeleteEIT( p_eit );
2147 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d ts_id=%d network_id=%d"
2148 "segment_last_section_number=%d last_table_id=%d",
2149 p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next, p_eit->i_ts_id,
2150 p_eit->i_network_id, p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2152 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2154 vlc_meta_t *p_meta = vlc_meta_New();
2155 dvbpsi_descriptor_t *p_dr;
2156 char *psz_cat = malloc( strlen("Event")+10 );
2158 char psz_duration[15];
2161 char *psz_extra = strdup("");
2164 sprintf( psz_cat, "Event %d", p_evt->i_event_id );
2165 sprintf( psz_start, "%d%d:%d%d:%d%d",
2166 (int)(p_evt->i_start_time >> 20)&0xf,
2167 (int)(p_evt->i_start_time >> 16)&0xf,
2168 (int)(p_evt->i_start_time >> 12)&0xf,
2169 (int)(p_evt->i_start_time >> 8)&0xf,
2170 (int)(p_evt->i_start_time >> 4)&0xf,
2171 (int)(p_evt->i_start_time )&0xf );
2172 sprintf( psz_duration, "%d%d:%d%d:%d%d",
2173 (p_evt->i_duration >> 20)&0xf, (p_evt->i_duration >> 16)&0xf,
2174 (p_evt->i_duration >> 12)&0xf, (p_evt->i_duration >> 8)&0xf,
2175 (p_evt->i_duration >> 4)&0xf, (p_evt->i_duration )&0xf );
2176 psz_name[0] = psz_text[0] = '\0';
2178 msg_Dbg( p_demux, " * event id=%d start_time:mjd=%d %s duration=%s "
2179 "running=%d free_ca=%d",
2181 (int)(p_evt->i_start_time >> 24),
2182 psz_start, psz_duration,
2183 p_evt->i_running_status, p_evt->b_free_ca );
2185 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2187 if( p_dr->i_tag == 0x4d )
2189 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2193 memcpy( psz_name, pE->i_event_name, pE->i_event_name_length);
2194 psz_name[pE->i_event_name_length] = '\0';
2195 memcpy( psz_text, pE->i_text, pE->i_text_length );
2196 psz_text[pE->i_text_length] = '\0';
2198 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
2199 pE->i_iso_639_code, psz_name, psz_text );
2202 else if( p_dr->i_tag == 0x4e )
2204 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
2211 msg_Dbg( p_demux, " - extended event lang=%3.3s",
2212 pE->i_iso_639_code );
2213 for( i = 0; i < pE->i_entry_count; i++ )
2215 memcpy( str1, pE->i_item_description[i],
2216 pE->i_item_description_length[i] );
2217 str1[pE->i_item_description_length[i]] = '\0';
2219 memcpy( str1, pE->i_item[i],
2220 pE->i_item_length[i] );
2221 str2[pE->i_item_length[i]] = '\0';
2223 msg_Dbg( p_demux, " - desc='%s' item='%s'", str1, str2 );
2224 psz_extra = realloc( psz_extra,
2226 strlen(str1) +strlen(str2) + 1 + 3 );
2227 strcat( psz_extra, str1 );
2228 strcat( psz_extra, "(" );
2229 strcat( psz_extra, str2 );
2230 strcat( psz_extra, ") " );
2233 memcpy( str1, pE->i_text, pE->i_text_length );
2234 str1[pE->i_text_length] = '\0';
2235 msg_Dbg( p_demux, " - text='%s'", str1 );
2236 psz_extra = realloc( psz_extra,
2237 strlen(psz_extra) + strlen(str1) + 2 );
2238 strcat( psz_extra, str1 );
2239 strcat( psz_extra, " " );
2244 msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
2248 asprintf( &psz_value, "%s: %s (+%s) %s (%s)",
2252 psz_text, psz_extra );
2253 vlc_meta_Add( p_meta, psz_cat, psz_value );
2256 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2257 p_eit->i_service_id, p_meta );
2258 vlc_meta_Delete( p_meta );
2263 psi->i_version = p_eit->i_version;
2264 dvbpsi_DeleteEIT( p_eit );
2267 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
2268 uint8_t i_table_id, uint16_t i_extension )
2271 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2272 i_table_id, i_table_id, i_extension, i_extension );
2274 if( i_table_id == 0x42 )
2276 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2277 i_table_id, i_table_id, i_extension, i_extension );
2279 dvbpsi_AttachSDT( h, i_table_id, i_extension, (dvbpsi_sdt_callback)SDTCallBack, p_demux );
2281 else if( i_table_id == 0x4e || /* Current/Following */
2282 ( i_table_id >= 0x50 && i_table_id <= 0x5f ) ) /* Schedule */
2284 ts_eit_psi_t *psi = malloc( sizeof(ts_eit_psi_t) );
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 psi->p_demux = p_demux;
2290 psi->i_table_id = i_table_id;
2291 psi->i_version = -1;
2292 psi->i_service_id = i_extension;
2294 dvbpsi_AttachEIT( h, i_table_id, i_extension, (dvbpsi_eit_callback)EITCallBack, psi );
2299 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
2301 demux_sys_t *p_sys = p_demux->p_sys;
2302 dvbpsi_descriptor_t *p_dr;
2303 dvbpsi_pmt_es_t *p_es;
2305 ts_pid_t *pmt = NULL;
2306 ts_prg_psi_t *prg = NULL;
2308 ts_pid_t **pp_clean = NULL;
2311 msg_Dbg( p_demux, "PMTCallBack called" );
2313 /* First find this PMT declared in PAT */
2314 for( i = 0; i < p_sys->i_pmt; i++ )
2317 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2319 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
2320 p_pmt->i_program_number )
2322 pmt = p_sys->pmt[i];
2323 prg = p_sys->pmt[i]->psi->prg[i_prg];
2332 msg_Warn( p_demux, "unreferenced program (broken stream)" );
2333 dvbpsi_DeletePMT(p_pmt);
2337 if( prg->i_version != -1 &&
2338 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
2340 dvbpsi_DeletePMT( p_pmt );
2344 /* Clean this program (remove all es) */
2345 for( i = 0; i < 8192; i++ )
2347 ts_pid_t *pid = &p_sys->pid[i];
2349 if( pid->b_valid && pid->p_owner == pmt->psi &&
2350 pid->i_owner_number == prg->i_number && pid->psi == NULL )
2352 TAB_APPEND( i_clean, pp_clean, pid );
2357 IODFree( prg->iod );
2361 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
2362 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
2363 prg->i_pid_pcr = p_pmt->i_pcr_pid;
2364 prg->i_version = p_pmt->i_version;
2366 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2368 /* Set demux filter */
2369 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2370 ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
2373 else if ( p_sys->b_dvb_control )
2375 msg_Warn( p_demux, "skipping program (not selected)" );
2376 dvbpsi_DeletePMT(p_pmt);
2380 /* Parse descriptor */
2381 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
2383 if( p_dr->i_tag == 0x1d )
2385 /* We have found an IOD descriptor */
2386 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
2388 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
2390 else if( p_dr->i_tag == 0x9 )
2392 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2394 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
2398 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2402 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2404 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
2406 /* Find out if the PID was already declared */
2407 for( i = 0; i < i_clean; i++ )
2409 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
2411 old_pid = pp_clean[i];
2416 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
2418 msg_Warn( p_demux, "pmt error: pid=%d already defined",
2423 PIDInit( pid, VLC_FALSE, pmt->psi );
2424 PIDFillFormat( pid, p_es->i_type );
2425 pid->i_owner_number = prg->i_number;
2426 pid->i_pid = p_es->i_pid;
2427 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
2429 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2430 p_es->i_type == 0x12 )
2432 /* MPEG-4 stream: search SL_DESCRIPTOR */
2433 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2435 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2437 if( p_dr && p_dr->i_length == 2 )
2439 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2441 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2443 pid->es->p_mpeg4desc = NULL;
2445 for( i = 0; i < 255; i++ )
2447 iod_descriptor_t *iod = prg->iod;
2449 if( iod->es_descr[i].b_ok &&
2450 iod->es_descr[i].i_es_id == i_es_id )
2452 pid->es->p_mpeg4desc = &iod->es_descr[i];
2458 if( pid->es->p_mpeg4desc != NULL )
2460 decoder_config_descriptor_t *dcd =
2461 &pid->es->p_mpeg4desc->dec_descr;
2463 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2465 pid->es->fmt.i_cat = VIDEO_ES;
2466 switch( dcd->i_objectTypeIndication )
2468 case 0x0B: /* mpeg4 sub */
2469 pid->es->fmt.i_cat = SPU_ES;
2470 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2473 case 0x20: /* mpeg4 */
2474 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2476 case 0x21: /* h264 */
2477 pid->es->fmt.i_codec = VLC_FOURCC('h','2','6','4');
2484 case 0x65: /* mpeg2 */
2485 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2487 case 0x6a: /* mpeg1 */
2488 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2490 case 0x6c: /* mpeg1 */
2491 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2494 pid->es->fmt.i_cat = UNKNOWN_ES;
2498 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2500 pid->es->fmt.i_cat = AUDIO_ES;
2501 switch( dcd->i_objectTypeIndication )
2503 case 0x40: /* mpeg4 */
2504 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2508 case 0x68: /* mpeg2 aac */
2509 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2511 case 0x69: /* mpeg2 */
2512 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2514 case 0x6b: /* mpeg1 */
2515 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2518 pid->es->fmt.i_cat = UNKNOWN_ES;
2524 pid->es->fmt.i_cat = UNKNOWN_ES;
2527 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2529 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2530 if( pid->es->fmt.i_extra > 0 )
2532 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2533 memcpy( pid->es->fmt.p_extra,
2534 dcd->p_decoder_specific_info,
2535 pid->es->fmt.i_extra );
2540 else if( p_es->i_type == 0x06 )
2542 dvbpsi_descriptor_t *p_dr;
2544 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2545 p_dr = p_dr->p_next )
2547 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
2548 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2550 if( p_dr->i_tag == 0x05 )
2552 /* Registration Descriptor */
2553 if( p_dr->i_length != 4 )
2555 msg_Warn( p_demux, "invalid Registration Descriptor" );
2559 if( !memcmp( p_dr->p_data, "AC-3", 4 ) )
2561 /* ATSC with stream_type 0x81 (but this descriptor
2562 * is then not mandatory */
2563 pid->es->fmt.i_cat = AUDIO_ES;
2564 pid->es->fmt.i_codec = VLC_FOURCC('a','5','2',' ');
2566 else if( !memcmp( p_dr->p_data, "DTS1", 4 ) ||
2567 !memcmp( p_dr->p_data, "DTS2", 4 ) ||
2568 !memcmp( p_dr->p_data, "DTS3", 4 ) )
2570 /*registration descriptor(ETSI TS 101 154 Annex F)*/
2571 pid->es->fmt.i_cat = AUDIO_ES;
2572 pid->es->fmt.i_codec = VLC_FOURCC('d','t','s',' ');
2574 else if( !memcmp( p_dr->p_data, "BSSD", 4 ) )
2576 pid->es->fmt.i_cat = AUDIO_ES;
2577 pid->es->fmt.i_codec = VLC_FOURCC('l','p','c','m');
2582 "unknown Registration Descriptor (%4.4s)",
2588 else if( p_dr->i_tag == 0x6a )
2590 /* DVB with stream_type 0x06 */
2591 pid->es->fmt.i_cat = AUDIO_ES;
2592 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2594 else if( p_dr->i_tag == 0x73 )
2596 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2597 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2598 pid->es->fmt.i_cat = AUDIO_ES;
2599 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2601 else if( p_dr->i_tag == 0x45 )
2603 msg_Dbg( p_demux, " * VBI Data descriptor" );
2604 /* FIXME : store the information somewhere */
2606 else if( p_dr->i_tag == 0x46 )
2608 msg_Dbg( p_demux, " * VBI Teletext descriptor" );
2609 /* FIXME : store the information somewhere */
2611 else if( p_dr->i_tag == 0x56 )
2613 msg_Dbg( p_demux, " * EBU Teletext descriptor" );
2614 pid->es->fmt.i_cat = SPU_ES;
2615 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
2616 pid->es->fmt.psz_description = strdup( "Teletext" );
2617 pid->es->fmt.i_extra = p_dr->i_length;
2618 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2619 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2622 #ifdef _DVBPSI_DR_59_H_
2623 else if( p_dr->i_tag == 0x59 )
2626 dvbpsi_subtitling_dr_t *sub;
2629 pid->es->fmt.i_cat = SPU_ES;
2630 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
2631 pid->es->fmt.i_group = p_pmt->i_program_number;
2633 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
2634 if( !sub ) continue;
2636 /* Each subtitle ES contains n languages,
2637 * We are going to create n ES for the n tracks */
2638 if( sub->i_subtitles_number > 0 )
2640 pid->es->fmt.psz_language = malloc( 4 );
2641 memcpy( pid->es->fmt.psz_language,
2642 sub->p_subtitle[0].i_iso6392_language_code, 3);
2643 pid->es->fmt.psz_language[3] = 0;
2645 pid->es->fmt.subs.dvb.i_id =
2646 sub->p_subtitle[0].i_composition_page_id;
2648 pid->es->fmt.subs.dvb.i_id |=
2649 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
2651 else pid->es->fmt.i_cat = UNKNOWN_ES;
2653 for( n = 1; n < sub->i_subtitles_number; n++ )
2655 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
2656 p_es->fmt = pid->es->fmt;
2659 p_es->i_pes_size = 0;
2660 p_es->i_pes_gathered = 0;
2661 p_es->pp_last = &p_es->p_pes;
2662 p_es->p_mpeg4desc = NULL;
2664 p_es->fmt.psz_language = malloc( 4 );
2665 memcpy( p_es->fmt.psz_language,
2666 sub->p_subtitle[n].i_iso6392_language_code, 3);
2667 p_es->fmt.psz_language[3] = 0;
2669 p_es->fmt.subs.dvb.i_id =
2670 sub->p_subtitle[n].i_composition_page_id;
2672 p_es->fmt.subs.dvb.i_id |=
2673 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
2675 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
2678 #endif /* _DVBPSI_DR_59_H_ */
2681 else if( p_es->i_type == 0xa0 )
2683 /* MSCODEC sent by vlc */
2684 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2686 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
2688 if( p_dr && p_dr->i_length >= 8 )
2690 pid->es->fmt.i_cat = VIDEO_ES;
2691 pid->es->fmt.i_codec =
2692 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
2693 p_dr->p_data[2], p_dr->p_data[3] );
2694 pid->es->fmt.video.i_width =
2695 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
2696 pid->es->fmt.video.i_height =
2697 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
2698 pid->es->fmt.i_extra =
2699 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
2701 if( pid->es->fmt.i_extra > 0 )
2703 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2704 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
2705 pid->es->fmt.i_extra );
2710 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
2713 /* For such stream we will gather them ourself and don't launch a
2715 * Yes it's ugly but it's the only way to have DIV3 working */
2716 pid->es->fmt.b_packetized = VLC_TRUE;
2719 if( pid->es->fmt.i_cat == AUDIO_ES ||
2720 ( pid->es->fmt.i_cat == SPU_ES &&
2721 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
2723 /* get language descriptor */
2724 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2725 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
2729 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
2733 pid->es->fmt.psz_language = malloc( 4 );
2734 memcpy( pid->es->fmt.psz_language,
2735 p_decoded->i_iso_639_code, 3 );
2736 pid->es->fmt.psz_language[3] = 0;
2741 pid->es->fmt.i_group = p_pmt->i_program_number;
2742 if( pid->es->fmt.i_cat == UNKNOWN_ES )
2744 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
2745 p_es->i_pid, p_es->i_type );
2747 else if( !p_sys->b_udp_out )
2749 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
2750 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
2752 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
2754 /* Check if we can avoid restarting the ES */
2756 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
2757 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
2758 pid->es->fmt.i_extra == 0 &&
2759 pid->i_extra_es == old_pid->i_extra_es &&
2760 ( ( !pid->es->fmt.psz_language &&
2761 !old_pid->es->fmt.psz_language ) ||
2762 ( pid->es->fmt.psz_language &&
2763 old_pid->es->fmt.psz_language &&
2764 !strcmp( pid->es->fmt.psz_language,
2765 old_pid->es->fmt.psz_language ) ) ) )
2767 pid->es->id = old_pid->es->id;
2768 old_pid->es->id = NULL;
2769 for( i = 0; i < pid->i_extra_es; i++ )
2771 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
2772 old_pid->extra_es[i]->id = NULL;
2779 PIDClean( p_demux->out, old_pid );
2780 TAB_REMOVE( i_clean, pp_clean, old_pid );
2784 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
2785 for( i = 0; i < pid->i_extra_es; i++ )
2787 pid->extra_es[i]->id =
2788 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
2793 /* Add ES to the list */
2796 PIDClean( p_demux->out, old_pid );
2797 TAB_REMOVE( i_clean, pp_clean, old_pid );
2799 p_sys->pid[p_es->i_pid] = *pid;
2801 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2802 p_dr = p_dr->p_next )
2804 if( p_dr->i_tag == 0x9 )
2806 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2808 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
2813 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2815 /* Set demux filter */
2816 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2817 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
2822 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2824 /* Set CAM descrambling */
2825 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2826 ACCESS_SET_PRIVATE_ID_CA, p_pmt );
2830 dvbpsi_DeletePMT( p_pmt );
2833 for ( i = 0; i < i_clean; i++ )
2835 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2837 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2838 ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
2842 PIDClean( p_demux->out, pp_clean[i] );
2844 if( i_clean ) free( pp_clean );
2847 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
2849 demux_sys_t *p_sys = p_demux->p_sys;
2850 dvbpsi_pat_program_t *p_program;
2851 ts_pid_t *pat = &p_sys->pid[0];
2854 msg_Dbg( p_demux, "PATCallBack called" );
2856 if( pat->psi->i_pat_version != -1 &&
2857 ( !p_pat->b_current_next ||
2858 p_pat->i_version == pat->psi->i_pat_version ) )
2860 dvbpsi_DeletePAT( p_pat );
2864 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
2865 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
2868 if( p_sys->i_pmt > 0 )
2871 ts_pid_t **pmt_rm = NULL;
2873 /* Search pmt to be deleted */
2874 for( i = 0; i < p_sys->i_pmt; i++ )
2876 ts_pid_t *pmt = p_sys->pmt[i];
2877 vlc_bool_t b_keep = VLC_FALSE;
2879 for( p_program = p_pat->p_first_program; p_program != NULL;
2880 p_program = p_program->p_next )
2882 if( p_program->i_pid == pmt->i_pid )
2885 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2887 if( p_program->i_number ==
2888 pmt->psi->prg[i_prg]->i_number )
2900 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
2904 /* Delete all ES attached to thoses PMT */
2905 for( i = 2; i < 8192; i++ )
2907 ts_pid_t *pid = &p_sys->pid[i];
2909 if( !pid->b_valid || pid->psi ) continue;
2911 for( j = 0; j < i_pmt_rm; j++ )
2914 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
2916 /* We only remove es that aren't defined by extra pmt */
2917 if( pid->p_owner->prg[i_prg]->i_pid_pmt !=
2918 pmt_rm[j]->i_pid ) continue;
2920 if( p_sys->b_dvb_control && pid->es->id )
2922 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2923 ACCESS_SET_PRIVATE_ID_STATE, i,
2925 p_sys->b_dvb_control = VLC_FALSE;
2928 PIDClean( p_demux->out, pid );
2932 if( !pid->b_valid ) break;
2936 /* Delete PMT pid */
2937 for( i = 0; i < i_pmt_rm; i++ )
2939 if( p_sys->b_dvb_control )
2941 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2942 ACCESS_SET_PRIVATE_ID_STATE,
2943 pmt_rm[i]->i_pid, VLC_FALSE ) )
2944 p_sys->b_dvb_control = VLC_FALSE;
2947 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
2948 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
2951 if( pmt_rm ) free( pmt_rm );
2954 /* now create programs */
2955 for( p_program = p_pat->p_first_program; p_program != NULL;
2956 p_program = p_program->p_next )
2958 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
2960 if( p_program->i_number != 0 )
2962 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
2963 vlc_bool_t b_add = VLC_TRUE;
2968 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2970 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
2979 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
2984 PIDInit( pmt, VLC_TRUE, pat->psi );
2985 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
2986 dvbpsi_AttachPMT( p_program->i_number,
2987 (dvbpsi_pmt_callback)PMTCallBack,
2989 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
2990 p_program->i_number;
2991 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
2994 /* Now select PID at access level */
2995 if( p_sys->b_dvb_control )
2997 if( DVBProgramIsSelected( p_demux, p_program->i_number ) )
2999 if( p_sys->i_dvb_program == 0 )
3000 p_sys->i_dvb_program = p_program->i_number;
3002 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
3003 p_sys->b_dvb_control = VLC_FALSE;
3007 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_FALSE ) )
3008 p_sys->b_dvb_control = VLC_FALSE;
3014 pat->psi->i_pat_version = p_pat->i_version;
3016 dvbpsi_DeletePAT( p_pat );