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/descriptor.h>
41 # include <dvbpsi/pat.h>
42 # include <dvbpsi/pmt.h>
43 # include <dvbpsi/dr.h>
44 # include <dvbpsi/psi.h>
47 # include "descriptor.h"
48 # include "tables/pat.h"
49 # include "tables/pmt.h"
50 # include "descriptors/dr.h"
55 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close ( vlc_object_t * );
66 #define PMT_TEXT N_("Extra PMT")
67 #define PMT_LONGTEXT N_( \
68 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])" )
70 #define PID_TEXT N_("Set id of ES to PID")
71 #define PID_LONGTEXT N_("set id of es to pid")
73 #define TSOUT_TEXT N_("Fast udp streaming")
74 #define TSOUT_LONGTEXT N_( \
75 "Sends TS to specific ip:port by udp (you must know what you are doing)")
77 #define MTUOUT_TEXT N_("MTU for out mode")
78 #define MTUOUT_LONGTEXT N_("MTU for out mode")
80 #define CSA_TEXT N_("CSA ck")
81 #define CSA_LONGTEXT N_("CSA ck")
83 #define SILENT_TEXT N_("Silent mode")
84 #define SILENT_LONGTEXT N_("do not complain on encrypted PES")
86 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
87 #define CAPMT_SYSID_LONGTEXT N_("only forward descriptors from this SysID to the CAM")
90 set_description( _("ISO 13818-1 MPEG Transport Stream input - new" ) );
91 set_shortname ( _("MPEG-TS") );
92 set_category( CAT_INPUT );
93 set_subcategory( SUBCAT_INPUT_DEMUX );
95 add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
96 add_bool( "ts-es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
97 add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
98 add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
99 MTUOUT_LONGTEXT, VLC_TRUE );
100 add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
101 add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );
102 add_integer( "ts-capmt-sysid", 0, NULL, CAPMT_SYSID_TEXT,
103 CAPMT_SYSID_LONGTEXT, VLC_TRUE );
105 set_capability( "demux2", 10 );
106 set_callbacks( Open, Close );
107 add_shortcut( "ts" );
110 /*****************************************************************************
112 *****************************************************************************/
116 uint8_t i_objectTypeIndication;
117 uint8_t i_streamType;
118 vlc_bool_t b_upStream;
119 uint32_t i_bufferSizeDB;
120 uint32_t i_maxBitrate;
121 uint32_t i_avgBitrate;
123 int i_decoder_specific_info_len;
124 uint8_t *p_decoder_specific_info;
126 } decoder_config_descriptor_t;
130 vlc_bool_t b_useAccessUnitStartFlag;
131 vlc_bool_t b_useAccessUnitEndFlag;
132 vlc_bool_t b_useRandomAccessPointFlag;
133 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
134 vlc_bool_t b_usePaddingFlag;
135 vlc_bool_t b_useTimeStampsFlags;
136 vlc_bool_t b_useIdleFlag;
137 vlc_bool_t b_durationFlag;
138 uint32_t i_timeStampResolution;
139 uint32_t i_OCRResolution;
140 uint8_t i_timeStampLength;
143 uint8_t i_instantBitrateLength;
144 uint8_t i_degradationPriorityLength;
145 uint8_t i_AU_seqNumLength;
146 uint8_t i_packetSeqNumLength;
148 uint32_t i_timeScale;
149 uint16_t i_accessUnitDuration;
150 uint16_t i_compositionUnitDuration;
152 uint64_t i_startDecodingTimeStamp;
153 uint64_t i_startCompositionTimeStamp;
155 } sl_config_descriptor_t;
162 vlc_bool_t b_streamDependenceFlag;
163 vlc_bool_t b_OCRStreamFlag;
164 uint8_t i_streamPriority;
168 uint16_t i_dependOn_es_id;
169 uint16_t i_OCR_es_id;
171 decoder_config_descriptor_t dec_descr;
172 sl_config_descriptor_t sl_descr;
174 } es_mpeg4_descriptor_t;
184 uint8_t i_ODProfileLevelIndication;
185 uint8_t i_sceneProfileLevelIndication;
186 uint8_t i_audioProfileLevelIndication;
187 uint8_t i_visualProfileLevelIndication;
188 uint8_t i_graphicsProfileLevelIndication;
190 es_mpeg4_descriptor_t es_descr[255];
196 dvbpsi_handle handle;
202 /* IOD stuff (mpeg4) */
203 iod_descriptor_t *iod;
209 /* for special PAT case */
210 dvbpsi_handle handle;
228 es_mpeg4_descriptor_t *p_mpeg4desc;
239 int i_cc; /* countinuity counter */
241 /* PSI owner (ie PMT -> PAT, ES -> PMT */
249 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
257 /* TS packet size (188, 192, 204) */
260 /* how many TS packet we read at once */
271 vlc_bool_t b_es_id_pid;
274 uint16_t i_capmt_sysid;
276 vlc_bool_t b_udp_out;
277 int fd; /* udp socket */
280 vlc_bool_t b_dvb_control;
282 vlc_list_t *p_programs_list;
285 static int Demux ( demux_t *p_demux );
286 static int Control( demux_t *p_demux, int i_query, va_list args );
289 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
290 static void PIDClean( es_out_t *out, ts_pid_t *pid );
291 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
293 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
294 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
296 static inline int PIDGet( block_t *p )
298 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
301 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
303 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
305 static iod_descriptor_t *IODNew( int , uint8_t * );
306 static void IODFree( iod_descriptor_t * );
308 #define TS_PACKET_SIZE_188 188
309 #define TS_PACKET_SIZE_192 192
310 #define TS_PACKET_SIZE_204 204
311 #define TS_PACKET_SIZE_MAX 204
313 /*****************************************************************************
315 *****************************************************************************/
316 static int Open( vlc_object_t *p_this )
318 demux_t *p_demux = (demux_t*)p_this;
322 int i_sync, i_peek, i;
329 if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
332 msg_Err( p_demux, "cannot peek" );
336 /* Search first sync byte */
337 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
339 if( p_peek[i_sync] == 0x47 ) break;
341 if( i_sync >= TS_PACKET_SIZE_MAX )
343 if( strcmp( p_demux->psz_demux, "ts" ) )
345 msg_Warn( p_demux, "TS module discarded" );
348 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
351 /* Check next 3 sync bytes */
352 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
353 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
355 msg_Err( p_demux, "cannot peek" );
358 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
359 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
360 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
362 i_packet_size = TS_PACKET_SIZE_188;
364 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
365 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
366 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
368 i_packet_size = TS_PACKET_SIZE_192;
370 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
371 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
372 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
374 i_packet_size = TS_PACKET_SIZE_204;
376 else if( !strcmp( p_demux->psz_demux, "ts" ) )
378 i_packet_size = TS_PACKET_SIZE_188;
382 msg_Warn( p_demux, "TS module discarded (lost sync)" );
386 /* Fill p_demux field */
387 p_demux->pf_demux = Demux;
388 p_demux->pf_control = Control;
389 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
390 memset( p_sys, 0, sizeof( demux_sys_t ) );
392 /* Init p_sys field */
393 p_sys->b_dvb_control = VLC_TRUE;
394 p_sys->i_dvb_program = 0;
395 for( i = 0; i < 8192; i++ )
397 ts_pid_t *pid = &p_sys->pid[i];
400 pid->b_seen = VLC_FALSE;
401 pid->b_valid = VLC_FALSE;
403 p_sys->i_packet_size = i_packet_size;
404 p_sys->b_udp_out = VLC_FALSE;
405 p_sys->i_ts_read = 50;
408 /* Init PAT handler */
409 pat = &p_sys->pid[0];
410 PIDInit( pat, VLC_TRUE, NULL );
411 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
419 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
420 var_Get( p_demux, "ts-es-id-pid", &val );
421 p_sys->b_es_id_pid = val.b_bool,
423 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
424 var_Get( p_demux, "ts-out", &val );
425 if( val.psz_string && *val.psz_string )
428 char *psz = strchr( val.psz_string, ':' );
431 p_sys->b_udp_out = VLC_TRUE;
436 i_port = atoi( psz );
438 if( i_port <= 0 ) i_port = 1234;
439 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
441 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
444 msg_Err( p_demux, "failed to open udp socket, send disabled" );
445 p_sys->b_udp_out = VLC_FALSE;
449 var_Create( p_demux, "ts-out-mtu",
450 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
451 var_Get( p_demux, "ts-out-mtu", &mtu );
452 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
453 if( p_sys->i_ts_read <= 0 )
455 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
457 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
462 free( val.psz_string );
466 /* We handle description of an extra PMT */
467 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
468 var_Get( p_demux, "ts-extra-pmt", &val );
469 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
471 char *psz = val.psz_string;
472 int i_pid = strtol( psz, &psz, 0 );
474 if( i_pid >= 2 && i_pid < 8192 )
476 ts_pid_t *pmt = &p_sys->pid[i_pid];
478 msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
479 PIDInit( pmt, VLC_TRUE, NULL );
481 pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
482 /* FIXME we should also ask for a number */
483 pmt->psi->prg[0]->handle =
484 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
486 pmt->psi->prg[0]->i_number = 0; /* special one */
488 psz = strchr( psz, '=' ) + 1; /* can't failed */
491 char *psz_next = strchr( psz, ',' );
492 int i_pid, i_stream_type;
499 i_pid = strtol( psz, &psz, 0 );
502 i_stream_type = strtol( psz + 1, &psz, 0 );
503 if( i_pid >= 2 && i_pid < 8192 &&
504 !p_sys->pid[i_pid].b_valid )
506 ts_pid_t *pid = &p_sys->pid[i_pid];
508 PIDInit( pid, VLC_FALSE, pmt->psi);
509 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
511 pmt->psi->prg[0]->i_pid_pcr = i_pid;
513 PIDFillFormat( pid, i_stream_type);
514 if( pid->es->fmt.i_cat != UNKNOWN_ES )
516 if( p_sys->b_es_id_pid )
518 pid->es->fmt.i_id = i_pid;
520 msg_Dbg( p_demux, " * es pid=%d type=%d "
521 "fcc=%4.4s", i_pid, i_stream_type,
522 (char*)&pid->es->fmt.i_codec );
523 pid->es->id = es_out_Add( p_demux->out,
534 free( val.psz_string );
537 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
538 var_Get( p_demux, "ts-csa-ck", &val );
539 if( val.psz_string && *val.psz_string )
541 char *psz = val.psz_string;
542 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
546 if( strlen( psz ) != 16 )
548 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
553 uint64_t i_ck = strtoull( psz, NULL, 16 );
555 uint64_t i_ck = strtoll( psz, NULL, 16 );
559 for( i = 0; i < 8; i++ )
561 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
564 msg_Dbg( p_demux, "using CSA scrambling with "
565 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
566 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
568 p_sys->csa = csa_New();
569 csa_SetCW( p_sys->csa, ck, ck );
574 free( val.psz_string );
577 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
578 var_Get( p_demux, "ts-silent", &val );
579 p_sys->b_silent = val.b_bool;
581 var_Create( p_demux, "ts-capmt-sysid", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
582 var_Get( p_demux, "ts-capmt-sysid", &val );
583 p_sys->i_capmt_sysid = val.i_int;
588 /*****************************************************************************
590 *****************************************************************************/
591 static void Close( vlc_object_t *p_this )
593 demux_t *p_demux = (demux_t*)p_this;
594 demux_sys_t *p_sys = p_demux->p_sys;
598 msg_Dbg( p_demux, "pid list:" );
599 for( i = 0; i < 8192; i++ )
601 ts_pid_t *pid = &p_sys->pid[i];
603 if( pid->b_valid && pid->psi )
608 dvbpsi_DetachPAT( pid->psi->handle );
615 PIDClean( p_demux->out, pid );
619 else if( pid->b_valid && pid->es )
621 PIDClean( p_demux->out, pid );
626 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
629 if( p_sys->b_dvb_control && pid->i_pid > 0 )
632 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
637 if( p_sys->b_udp_out )
639 net_Close( p_sys->fd );
640 free( p_sys->buffer );
644 csa_Delete( p_sys->csa );
647 if( p_sys->i_pmt ) free( p_sys->pmt );
649 if ( p_sys->p_programs_list )
652 val.p_list = p_sys->p_programs_list;
653 var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
659 /*****************************************************************************
661 *****************************************************************************/
662 static int Demux( demux_t *p_demux )
664 demux_sys_t *p_sys = p_demux->p_sys;
667 /* We read at most 100 TS packet or until a frame is completed */
668 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
670 vlc_bool_t b_frame = VLC_FALSE;
674 /* Get a new TS packet */
675 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
677 msg_Dbg( p_demux, "eof ?" );
681 /* Check sync byte and re-sync if needed */
682 if( p_pkt->p_buffer[0] != 0x47 )
684 msg_Warn( p_demux, "lost synchro" );
685 block_Release( p_pkt );
687 while( !p_demux->b_die )
690 int i_peek, i_skip = 0;
692 i_peek = stream_Peek( p_demux->s, &p_peek,
693 p_sys->i_packet_size * 10 );
694 if( i_peek < p_sys->i_packet_size + 1 )
696 msg_Dbg( p_demux, "eof ?" );
700 while( i_skip < i_peek - p_sys->i_packet_size )
702 if( p_peek[i_skip] == 0x47 &&
703 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
710 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
711 stream_Read( p_demux->s, NULL, i_skip );
713 if( i_skip < i_peek - p_sys->i_packet_size )
719 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
721 msg_Dbg( p_demux, "eof ?" );
726 if( p_sys->b_udp_out )
728 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
729 p_pkt->p_buffer, p_sys->i_packet_size );
732 /* Parse the TS packet */
733 p_pid = &p_sys->pid[PIDGet( p_pkt )];
739 if( p_pid->i_pid == 0 )
741 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
746 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
748 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
752 block_Release( p_pkt );
754 else if( !p_sys->b_udp_out )
756 b_frame = GatherPES( p_demux, p_pid, p_pkt );
760 PCRHandle( p_demux, p_pid, p_pkt );
761 block_Release( p_pkt );
768 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
770 /* We have to handle PCR if present */
771 PCRHandle( p_demux, p_pid, p_pkt );
772 block_Release( p_pkt );
774 p_pid->b_seen = VLC_TRUE;
782 if( p_sys->b_udp_out )
784 /* Send the complete block */
785 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
786 p_sys->i_ts_read * p_sys->i_packet_size );
792 /*****************************************************************************
794 *****************************************************************************/
795 static int Control( demux_t *p_demux, int i_query, va_list args )
797 demux_sys_t *p_sys = p_demux->p_sys;
804 case DEMUX_GET_POSITION:
805 pf = (double*) va_arg( args, double* );
806 i64 = stream_Size( p_demux->s );
809 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
816 case DEMUX_SET_POSITION:
817 f = (double) va_arg( args, double );
818 i64 = stream_Size( p_demux->s );
820 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
821 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
829 pi64 = (int64_t*)va_arg( args, int64_t * );
830 if( p_sys->i_time < 0 )
835 *pi64 = p_sys->i_time;
838 case DEMUX_GET_LENGTH:
839 pi64 = (int64_t*)va_arg( args, int64_t * );
840 if( p_sys->i_mux_rate > 0 )
842 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
849 case DEMUX_SET_GROUP:
851 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
852 ts_prg_psi_t *p_prg = NULL;
855 i_int = (int)va_arg( args, int );
856 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
857 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
859 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
864 /* Search pmt to be unselected */
865 for( i = 0; i < p_sys->i_pmt; i++ )
867 ts_pid_t *pmt = p_sys->pmt[i];
870 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
872 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
874 i_pmt_pid = p_sys->pmt[i]->i_pid;
878 if( i_pmt_pid > 0 ) break;
883 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
884 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
887 for( i = 2; i < 8192; i++ )
889 ts_pid_t *pid = &p_sys->pid[i];
892 if( !pid->b_valid || pid->psi ) continue;
894 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
896 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
898 /* We only remove es that aren't defined by extra pmt */
899 stream_Control( p_demux->s,
900 STREAM_CONTROL_ACCESS,
901 ACCESS_SET_PRIVATE_ID_STATE,
909 /* select new program */
910 p_sys->i_dvb_program = i_int;
912 for( i = 0; i < p_sys->i_pmt; i++ )
914 ts_pid_t *pmt = p_sys->pmt[i];
917 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
919 if( pmt->psi->prg[i_prg]->i_number == i_int )
921 i_pmt_pid = p_sys->pmt[i]->i_pid;
922 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
926 if( i_pmt_pid > 0 ) break;
930 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
931 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
933 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
934 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
937 for( i = 2; i < 8192; i++ )
939 ts_pid_t *pid = &p_sys->pid[i];
942 if( !pid->b_valid || pid->psi ) continue;
944 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
946 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
948 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
950 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
952 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
954 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
957 stream_Control( p_demux->s,
958 STREAM_CONTROL_ACCESS,
959 ACCESS_SET_PRIVATE_ID_STATE,
969 p_sys->i_dvb_program = -1;
970 p_sys->p_programs_list = p_list;
982 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
984 vlc_bool_t b_old_valid = pid->b_valid;
986 pid->b_valid = VLC_TRUE;
988 pid->p_owner = p_owner;
989 pid->i_owner_number = 0;
991 pid->extra_es = NULL;
1000 pid->psi = malloc( sizeof( ts_psi_t ) );
1001 pid->psi->i_prg = 0;
1002 pid->psi->prg = NULL;
1003 pid->psi->handle= NULL;
1005 pid->psi->i_pat_version = -1;
1008 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1010 prg->i_version = -1;
1012 prg->i_pid_pcr = -1;
1013 prg->i_pid_pmt = -1;
1017 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1023 pid->es = malloc( sizeof( ts_es_t ) );
1025 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1027 pid->es->p_pes = NULL;
1028 pid->es->i_pes_size= 0;
1029 pid->es->i_pes_gathered= 0;
1030 pid->es->pp_last = &pid->es->p_pes;
1031 pid->es->p_mpeg4desc = NULL;
1032 pid->es->b_gather = VLC_FALSE;
1036 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1042 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1043 for( i = 0; i < pid->psi->i_prg; i++ )
1045 if( pid->psi->prg[i]->iod )
1046 IODFree( pid->psi->prg[i]->iod );
1047 if( pid->psi->prg[i]->handle )
1048 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1049 free( pid->psi->prg[i] );
1051 if( pid->psi->prg ) free( pid->psi->prg );
1059 es_out_Del( out, pid->es->id );
1061 if( pid->es->p_pes )
1062 block_ChainRelease( pid->es->p_pes );
1064 es_format_Clean( &pid->es->fmt );
1068 for( i = 0; i < pid->i_extra_es; i++ )
1070 if( pid->extra_es[i]->id )
1071 es_out_Del( out, pid->extra_es[i]->id );
1073 if( pid->extra_es[i]->p_pes )
1074 block_ChainRelease( pid->extra_es[i]->p_pes );
1076 es_format_Clean( &pid->extra_es[i]->fmt );
1078 free( pid->extra_es[i] );
1080 if( pid->i_extra_es ) free( pid->extra_es );
1083 pid->b_valid = VLC_FALSE;
1086 /****************************************************************************
1088 ****************************************************************************/
1089 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1091 block_t *p_pes = pid->es->p_pes;
1097 mtime_t i_length = 0;
1100 /* remove the pes from pid */
1101 pid->es->p_pes = NULL;
1102 pid->es->i_pes_size= 0;
1103 pid->es->i_pes_gathered= 0;
1104 pid->es->pp_last = &pid->es->p_pes;
1106 /* FIXME find real max size */
1107 i_max = block_ChainExtract( p_pes, header, 30 );
1110 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1112 if( !p_demux->p_sys->b_silent )
1113 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1114 header[0], header[1],header[2],header[3], pid->i_pid );
1115 block_ChainRelease( p_pes );
1119 /* TODO check size */
1122 case 0xBC: /* Program stream map */
1123 case 0xBE: /* Padding */
1124 case 0xBF: /* Private stream 2 */
1125 case 0xB0: /* ECM */
1126 case 0xB1: /* EMM */
1127 case 0xFF: /* Program stream directory */
1128 case 0xF2: /* DSMCC stream */
1129 case 0xF8: /* ITU-T H.222.1 type E stream */
1133 if( ( header[6]&0xC0 ) == 0x80 )
1136 i_skip = header[8] + 9;
1138 if( header[7]&0x80 ) /* has pts */
1140 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1141 (mtime_t)(header[10] << 22)|
1142 ((mtime_t)(header[11]&0xfe) << 14)|
1143 (mtime_t)(header[12] << 7)|
1144 (mtime_t)(header[13] >> 1);
1146 if( header[7]&0x40 ) /* has dts */
1148 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1149 (mtime_t)(header[15] << 22)|
1150 ((mtime_t)(header[16]&0xfe) << 14)|
1151 (mtime_t)(header[17] << 7)|
1152 (mtime_t)(header[18] >> 1);
1159 while( i_skip < 23 && header[i_skip] == 0xff )
1165 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1166 block_ChainRelease( p_pes );
1169 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1174 if( header[i_skip]&0x20 )
1176 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1177 (mtime_t)(header[i_skip+1] << 22)|
1178 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1179 (mtime_t)(header[i_skip+3] << 7)|
1180 (mtime_t)(header[i_skip+4] >> 1);
1182 if( header[i_skip]&0x10 ) /* has dts */
1184 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1185 (mtime_t)(header[i_skip+6] << 22)|
1186 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1187 (mtime_t)(header[i_skip+8] << 7)|
1188 (mtime_t)(header[i_skip+9] >> 1);
1204 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1205 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1209 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1210 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1211 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1215 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1216 pid->es->p_mpeg4desc )
1218 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1220 if( dcd->i_decoder_specific_info_len > 2 &&
1221 dcd->p_decoder_specific_info[0] == 0x10 &&
1222 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1224 /* display length */
1225 if( p_pes->i_buffer + 2 <= i_skip )
1227 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1232 if( p_pes->i_buffer + 2 <= i_skip )
1234 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1241 while( p_pes && i_skip > 0 )
1243 if( p_pes->i_buffer <= i_skip )
1245 block_t *p_next = p_pes->p_next;
1247 i_skip -= p_pes->i_buffer;
1248 block_Release( p_pes );
1253 p_pes->i_buffer -= i_skip;
1254 p_pes->p_buffer += i_skip;
1259 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1260 if( i_pts >= 0 && i_dts < 0 )
1270 p_pes->i_dts = i_dts * 100 / 9;
1274 p_pes->i_pts = i_pts * 100 / 9;
1276 p_pes->i_length = i_length * 100 / 9;
1278 p_block = block_ChainGather( p_pes );
1279 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1281 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1283 p_block->i_buffer = i_pes_size;
1286 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1287 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1290 for( i = 0; i < pid->i_extra_es; i++ )
1292 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1293 block_Duplicate( p_block ) );
1296 es_out_Send( p_demux->out, pid->es->id, p_block );
1300 msg_Warn( p_demux, "empty pes" );
1304 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1306 demux_sys_t *p_sys = p_demux->p_sys;
1307 const uint8_t *p = p_bk->p_buffer;
1309 if( ( p[3]&0x20 ) && /* adaptation */
1314 mtime_t i_pcr; /* 33 bits */
1316 i_pcr = ( (mtime_t)p[6] << 25 ) |
1317 ( (mtime_t)p[7] << 17 ) |
1318 ( (mtime_t)p[8] << 9 ) |
1319 ( (mtime_t)p[9] << 1 ) |
1320 ( (mtime_t)p[10] >> 7 );
1322 /* Search program and set the PCR */
1323 for( i = 0; i < p_sys->i_pmt; i++ )
1326 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1328 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1330 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1331 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1332 (int64_t)(i_pcr * 100 / 9) );
1339 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1341 const uint8_t *p = p_bk->p_buffer;
1342 const vlc_bool_t b_unit_start = p[1]&0x40;
1343 const vlc_bool_t b_adaptation = p[3]&0x20;
1344 const vlc_bool_t b_payload = p[3]&0x10;
1345 const int i_cc = p[3]&0x0f; /* continuity counter */
1347 /* transport_scrambling_control is ignored */
1350 vlc_bool_t i_ret = VLC_FALSE;
1354 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1355 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1359 /* For now, ignore additional error correction
1360 * TODO: handle Reed-Solomon 204,188 error correction */
1361 p_bk->i_buffer = TS_PACKET_SIZE_188;
1365 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1369 if( p_demux->p_sys->csa )
1371 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
1376 /* We don't have any adaptation_field, so payload starts
1377 * immediately after the 4 byte TS header */
1382 /* p[4] is adaptation_field_length minus one */
1388 msg_Warn( p_demux, "discontinuity_indicator (pid=%d) "
1389 "ignored", pid->i_pid );
1394 /* Test continuity counter */
1395 /* continuous when (one of this):
1397 * diff == 0 and payload == 0
1398 * diff == 0 and duplicate packet (playload != 0) <- should we
1399 * test the content ?
1402 i_diff = ( i_cc - pid->i_cc )&0x0f;
1403 if( b_payload && i_diff == 1 )
1409 if( pid->i_cc == 0xff )
1411 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1415 else if( i_diff != 0 )
1417 /* FIXME what to do when discontinuity_indicator is set ? */
1418 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
1419 i_cc, ( pid->i_cc + 1 )&0x0f );
1423 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1425 /* Small video artifacts are usually better then
1426 * dropping full frames */
1427 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1432 PCRHandle( p_demux, pid, p_bk );
1434 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1436 block_Release( p_bk );
1440 /* We have to gather it */
1441 p_bk->p_buffer += i_skip;
1442 p_bk->i_buffer -= i_skip;
1446 if( pid->es->p_pes )
1448 ParsePES( p_demux, pid );
1452 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1453 if( p_bk->i_buffer > 6 )
1455 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1456 if( pid->es->i_pes_size > 0 )
1458 pid->es->i_pes_size += 6;
1461 pid->es->i_pes_gathered += p_bk->i_buffer;
1462 if( pid->es->i_pes_size > 0 &&
1463 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1465 ParsePES( p_demux, pid );
1471 if( pid->es->p_pes == NULL )
1473 /* msg_Dbg( p_demux, "broken packet" ); */
1474 block_Release( p_bk );
1478 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1479 pid->es->i_pes_gathered += p_bk->i_buffer;
1480 if( pid->es->i_pes_size > 0 &&
1481 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1483 ParsePES( p_demux, pid );
1492 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1494 es_format_t *fmt = &pid->es->fmt;
1496 switch( i_stream_type )
1498 case 0x01: /* MPEG-1 video */
1499 case 0x02: /* MPEG-2 video */
1500 case 0x80: /* MPEG-2 MOTO video */
1501 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1503 case 0x03: /* MPEG-1 audio */
1504 case 0x04: /* MPEG-2 audio */
1505 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1507 case 0x11: /* MPEG4 (audio) */
1508 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1509 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1511 case 0x10: /* MPEG4 (video) */
1512 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1513 pid->es->b_gather = VLC_TRUE;
1515 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1516 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1519 case 0x81: /* A52 (audio) */
1520 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1522 case 0x82: /* DVD_SPU (sub) */
1523 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1525 case 0x83: /* LPCM (audio) */
1526 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1528 case 0x84: /* SDDS (audio) */
1529 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1531 case 0x85: /* DTS (audio) */
1532 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1535 case 0x91: /* A52 vls (audio) */
1536 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1538 case 0x92: /* DVD_SPU vls (sub) */
1539 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1541 case 0x93: /* LPCM vls (audio) */
1542 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1544 case 0x94: /* SDDS (audio) */
1545 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1548 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1549 es_format_Init( fmt, UNKNOWN_ES, 0 );
1550 pid->es->b_gather = VLC_TRUE;
1553 case 0x06: /* PES_PRIVATE (fixed later) */
1554 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1556 es_format_Init( fmt, UNKNOWN_ES, 0 );
1560 /* PES packets usually contain truncated frames */
1561 fmt->b_packetized = VLC_FALSE;
1563 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1566 /*****************************************************************************
1567 * MP4 specific functions (IOD parser)
1568 *****************************************************************************/
1569 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1572 unsigned int i_len = 0;
1578 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1580 } while( i_b&0x80 );
1584 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1588 const int i_b = **pp_data;
1595 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1597 const int i1 = IODGetByte( pi_data, pp_data );
1598 const int i2 = IODGetByte( pi_data, pp_data );
1599 return( ( i1 << 8 ) | i2 );
1601 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1603 const int i1 = IODGetByte( pi_data, pp_data );
1604 const int i2 = IODGetByte( pi_data, pp_data );
1605 const int i3 = IODGetByte( pi_data, pp_data );
1607 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1610 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1612 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1613 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1614 return( ( i1 << 16 ) | i2 );
1617 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1622 i_url_len = IODGetByte( pi_data, pp_data );
1623 url = malloc( i_url_len + 1 );
1624 for( i = 0; i < i_url_len; i++ )
1626 url[i] = IODGetByte( pi_data, pp_data );
1628 url[i_url_len] = '\0';
1632 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1634 iod_descriptor_t *p_iod;
1641 p_iod = malloc( sizeof( iod_descriptor_t ) );
1642 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1644 fprintf( stderr, "\n************ IOD ************" );
1645 for( i = 0; i < 255; i++ )
1647 p_iod->es_descr[i].b_ok = 0;
1656 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1657 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1658 fprintf( stderr, "\n* ===========" );
1659 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1661 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1663 fprintf( stderr, "\n ERR: tag != 0x02" );
1667 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1668 fprintf( stderr, "\n* length:%d", i_iod_length );
1669 if( i_iod_length > i_data )
1671 i_iod_length = i_data;
1674 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1675 i_flags = IODGetByte( &i_data, &p_data );
1676 p_iod->i_od_id |= i_flags >> 6;
1677 b_url = ( i_flags >> 5 )&0x01;
1679 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1680 fprintf( stderr, "\n* url flag:%d", b_url );
1681 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1685 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1686 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1687 fprintf( stderr, "\n*****************************\n" );
1692 p_iod->psz_url = NULL;
1695 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1696 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1697 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1698 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1699 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1701 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1702 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1703 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1704 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1705 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1708 while( i_data > 0 && i_es_index < 255)
1710 int i_tag, i_length;
1712 uint8_t *p_data_sav;
1714 i_tag = IODGetByte( &i_data, &p_data );
1715 i_length = IODDescriptorLength( &i_data, &p_data );
1717 i_data_sav = i_data;
1718 p_data_sav = p_data;
1726 #define es_descr p_iod->es_descr[i_es_index]
1727 int i_decoderConfigDescr_length;
1728 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1731 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1732 i_flags = IODGetByte( &i_data, &p_data );
1733 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1734 b_url = ( i_flags >> 6 )&0x01;
1735 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1736 es_descr.i_streamPriority = i_flags & 0x1f;
1737 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1738 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1739 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1741 if( es_descr.b_streamDependenceFlag )
1743 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1744 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1749 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1750 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1754 es_descr.psz_url = NULL;
1757 if( es_descr.b_OCRStreamFlag )
1759 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1760 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1763 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1765 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1769 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1771 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1772 #define dec_descr es_descr.dec_descr
1773 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1774 i_flags = IODGetByte( &i_data, &p_data );
1775 dec_descr.i_streamType = i_flags >> 2;
1776 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1777 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1778 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1779 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1780 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1781 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1782 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1783 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1784 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1785 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1786 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1789 dec_descr.i_decoder_specific_info_len =
1790 IODDescriptorLength( &i_data, &p_data );
1791 if( dec_descr.i_decoder_specific_info_len > 0 )
1793 dec_descr.p_decoder_specific_info =
1794 malloc( dec_descr.i_decoder_specific_info_len );
1796 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1798 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1803 dec_descr.i_decoder_specific_info_len = 0;
1804 dec_descr.p_decoder_specific_info = NULL;
1808 #define sl_descr es_descr.sl_descr
1810 int i_SLConfigDescr_length;
1813 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1815 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1819 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1821 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1822 i_predefined = IODGetByte( &i_data, &p_data );
1823 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1824 switch( i_predefined )
1828 sl_descr.b_useAccessUnitStartFlag = 0;
1829 sl_descr.b_useAccessUnitEndFlag = 0;
1830 sl_descr.b_useRandomAccessPointFlag = 0;
1831 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1832 sl_descr.b_usePaddingFlag = 0;
1833 sl_descr.b_useTimeStampsFlags = 0;
1834 sl_descr.b_useIdleFlag = 0;
1835 sl_descr.b_durationFlag = 0; // FIXME FIXME
1836 sl_descr.i_timeStampResolution = 1000;
1837 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1838 sl_descr.i_timeStampLength = 32;
1839 sl_descr.i_OCRLength = 0; // FIXME FIXME
1840 sl_descr.i_AU_Length = 0;
1841 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1842 sl_descr.i_degradationPriorityLength= 0;
1843 sl_descr.i_AU_seqNumLength = 0;
1844 sl_descr.i_packetSeqNumLength = 0;
1845 if( sl_descr.b_durationFlag )
1847 sl_descr.i_timeScale = 0; // FIXME FIXME
1848 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1849 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1851 if( !sl_descr.b_useTimeStampsFlags )
1853 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1854 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1859 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1868 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1872 p_data = p_data_sav + i_length;
1873 i_data = i_data_sav - i_length;
1878 fprintf( stderr, "\n*****************************\n" );
1882 static void IODFree( iod_descriptor_t *p_iod )
1886 if( p_iod->psz_url )
1888 free( p_iod->psz_url );
1889 p_iod->psz_url = NULL;
1894 for( i = 0; i < 255; i++ )
1896 #define es_descr p_iod->es_descr[i]
1899 if( es_descr.psz_url )
1901 free( es_descr.psz_url );
1902 es_descr.psz_url = NULL;
1906 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1908 free( es_descr.dec_descr.p_decoder_specific_info );
1909 es_descr.dec_descr.p_decoder_specific_info = NULL;
1910 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1920 /****************************************************************************
1921 ****************************************************************************
1922 ** libdvbpsi callbacks
1923 ****************************************************************************
1924 ****************************************************************************/
1925 static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
1927 demux_sys_t *p_sys = p_demux->p_sys;
1929 if ( !p_sys->b_dvb_control ) return VLC_FALSE;
1930 if ( p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL )
1932 if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
1934 if ( p_sys->p_programs_list != NULL )
1937 for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
1939 if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
1946 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1948 demux_sys_t *p_sys = p_demux->p_sys;
1949 dvbpsi_descriptor_t *p_dr;
1950 dvbpsi_pmt_es_t *p_es;
1952 ts_pid_t *pmt = NULL;
1953 ts_prg_psi_t *prg = NULL;
1955 ts_pid_t **pp_clean = NULL;
1958 msg_Dbg( p_demux, "PMTCallBack called" );
1960 /* First find this PMT declared in PAT */
1961 for( i = 0; i < p_sys->i_pmt; i++ )
1964 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1966 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
1967 p_pmt->i_program_number )
1969 pmt = p_sys->pmt[i];
1970 prg = p_sys->pmt[i]->psi->prg[i_prg];
1979 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1980 dvbpsi_DeletePMT(p_pmt);
1984 if( prg->i_version != -1 &&
1985 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
1987 dvbpsi_DeletePMT( p_pmt );
1991 /* Clean this program (remove all es) */
1992 for( i = 0; i < 8192; i++ )
1994 ts_pid_t *pid = &p_sys->pid[i];
1996 if( pid->b_valid && pid->p_owner == pmt->psi &&
1997 pid->i_owner_number == prg->i_number && pid->psi == NULL )
1999 TAB_APPEND( i_clean, pp_clean, pid );
2004 IODFree( prg->iod );
2008 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
2009 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
2010 prg->i_pid_pcr = p_pmt->i_pcr_pid;
2011 prg->i_version = p_pmt->i_version;
2013 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2015 /* Set demux filter */
2016 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2017 ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
2020 else if ( p_sys->b_dvb_control )
2022 msg_Warn( p_demux, "skipping program (not selected)" );
2023 dvbpsi_DeletePMT(p_pmt);
2027 /* Parse descriptor */
2028 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
2030 if( p_dr->i_tag == 0x1d )
2032 /* We have found an IOD descriptor */
2033 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
2035 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
2037 else if( p_dr->i_tag == 0x9 )
2039 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2041 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
2045 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2049 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2051 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
2053 /* Find out if the PID was already declared */
2054 for( i = 0; i < i_clean; i++ )
2056 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
2058 old_pid = pp_clean[i];
2063 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
2065 msg_Warn( p_demux, "pmt error: pid=%d already defined",
2070 PIDInit( pid, VLC_FALSE, pmt->psi );
2071 PIDFillFormat( pid, p_es->i_type );
2072 pid->i_owner_number = prg->i_number;
2073 pid->i_pid = p_es->i_pid;
2074 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
2076 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2077 p_es->i_type == 0x12 )
2079 /* MPEG-4 stream: search SL_DESCRIPTOR */
2080 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2082 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2084 if( p_dr && p_dr->i_length == 2 )
2086 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2088 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2090 pid->es->p_mpeg4desc = NULL;
2092 for( i = 0; i < 255; i++ )
2094 iod_descriptor_t *iod = prg->iod;
2096 if( iod->es_descr[i].b_ok &&
2097 iod->es_descr[i].i_es_id == i_es_id )
2099 pid->es->p_mpeg4desc = &iod->es_descr[i];
2105 if( pid->es->p_mpeg4desc != NULL )
2107 decoder_config_descriptor_t *dcd =
2108 &pid->es->p_mpeg4desc->dec_descr;
2110 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2112 pid->es->fmt.i_cat = VIDEO_ES;
2113 switch( dcd->i_objectTypeIndication )
2115 case 0x0B: /* mpeg4 sub */
2116 pid->es->fmt.i_cat = SPU_ES;
2117 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2120 case 0x20: /* mpeg4 */
2121 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2128 case 0x65: /* mpeg2 */
2129 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2131 case 0x6a: /* mpeg1 */
2132 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2134 case 0x6c: /* mpeg1 */
2135 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2138 pid->es->fmt.i_cat = UNKNOWN_ES;
2142 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2144 pid->es->fmt.i_cat = AUDIO_ES;
2145 switch( dcd->i_objectTypeIndication )
2147 case 0x40: /* mpeg4 */
2148 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2152 case 0x68: /* mpeg2 aac */
2153 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2155 case 0x69: /* mpeg2 */
2156 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2158 case 0x6b: /* mpeg1 */
2159 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2162 pid->es->fmt.i_cat = UNKNOWN_ES;
2168 pid->es->fmt.i_cat = UNKNOWN_ES;
2171 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2173 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2174 if( pid->es->fmt.i_extra > 0 )
2176 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2177 memcpy( pid->es->fmt.p_extra,
2178 dcd->p_decoder_specific_info,
2179 pid->es->fmt.i_extra );
2184 else if( p_es->i_type == 0x06 )
2186 dvbpsi_descriptor_t *p_dr;
2188 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2189 p_dr = p_dr->p_next )
2191 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
2192 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2194 if( p_dr->i_tag == 0x05 )
2196 /* Registration Descriptor */
2197 if( p_dr->i_length != 4 )
2199 msg_Warn( p_demux, "invalid Registration Descriptor" );
2203 if( !memcmp( p_dr->p_data, "AC-3", 4 ) )
2205 /* ATSC with stream_type 0x81 (but this descriptor
2206 * is then not mandatory */
2207 pid->es->fmt.i_cat = AUDIO_ES;
2208 pid->es->fmt.i_codec = VLC_FOURCC('a','5','2',' ');
2210 else if( !memcmp( p_dr->p_data, "DTS1", 4 ) ||
2211 !memcmp( p_dr->p_data, "DTS2", 4 ) ||
2212 !memcmp( p_dr->p_data, "DTS3", 4 ) )
2214 /*registration descriptor(ETSI TS 101 154 Annex F)*/
2215 pid->es->fmt.i_cat = AUDIO_ES;
2216 pid->es->fmt.i_codec = VLC_FOURCC('d','t','s',' ');
2218 else if( !memcmp( p_dr->p_data, "BSSD", 4 ) )
2220 pid->es->fmt.i_cat = AUDIO_ES;
2221 pid->es->fmt.i_codec = VLC_FOURCC('l','p','c','m');
2226 "unknown Registration Descriptor (%4.4s)",
2232 else if( p_dr->i_tag == 0x6a )
2234 /* DVB with stream_type 0x06 */
2235 pid->es->fmt.i_cat = AUDIO_ES;
2236 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2238 else if( p_dr->i_tag == 0x73 )
2240 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2241 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2242 pid->es->fmt.i_cat = AUDIO_ES;
2243 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2245 else if( p_dr->i_tag == 0x45 )
2247 msg_Dbg( p_demux, " * VBI Data descriptor" );
2248 pid->es->fmt.i_cat = SPU_ES;
2249 pid->es->fmt.i_codec = VLC_FOURCC( 'v', 'b', 'i', 'd' );
2250 pid->es->fmt.psz_description = strdup( "VBI Data" );
2251 pid->es->fmt.i_extra = p_dr->i_length;
2252 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2253 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2256 else if( p_dr->i_tag == 0x46 )
2258 msg_Dbg( p_demux, " * VBI Teletext descriptor" );
2259 pid->es->fmt.i_cat = SPU_ES;
2260 pid->es->fmt.i_codec = VLC_FOURCC( 'v', 'b', 'i', 't' );
2261 pid->es->fmt.psz_description = strdup( "VBI Teletext" );
2262 pid->es->fmt.i_extra = p_dr->i_length;
2263 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2264 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2267 else if( p_dr->i_tag == 0x56 )
2269 msg_Dbg( p_demux, " * EBU Teletext descriptor" );
2270 pid->es->fmt.i_cat = SPU_ES;
2271 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
2272 pid->es->fmt.psz_description = strdup( "Teletext" );
2273 pid->es->fmt.i_extra = p_dr->i_length;
2274 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2275 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2278 #ifdef _DVBPSI_DR_59_H_
2279 else if( p_dr->i_tag == 0x59 )
2282 dvbpsi_subtitling_dr_t *sub;
2285 pid->es->fmt.i_cat = SPU_ES;
2286 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
2287 pid->es->fmt.i_group = p_pmt->i_program_number;
2289 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
2290 if( !sub ) continue;
2292 /* Each subtitle ES contains n languages,
2293 * We are going to create n ES for the n tracks */
2294 if( sub->i_subtitles_number > 0 )
2296 pid->es->fmt.psz_language = malloc( 4 );
2297 memcpy( pid->es->fmt.psz_language,
2298 sub->p_subtitle[0].i_iso6392_language_code, 3);
2299 pid->es->fmt.psz_language[3] = 0;
2301 pid->es->fmt.subs.dvb.i_id =
2302 sub->p_subtitle[0].i_composition_page_id;
2304 pid->es->fmt.subs.dvb.i_id |=
2305 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
2307 else pid->es->fmt.i_cat = UNKNOWN_ES;
2309 for( n = 1; n < sub->i_subtitles_number; n++ )
2311 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
2312 p_es->fmt = pid->es->fmt;
2315 p_es->i_pes_size = 0;
2316 p_es->i_pes_gathered = 0;
2317 p_es->pp_last = &p_es->p_pes;
2318 p_es->p_mpeg4desc = NULL;
2320 p_es->fmt.psz_language = malloc( 4 );
2321 memcpy( p_es->fmt.psz_language,
2322 sub->p_subtitle[n].i_iso6392_language_code, 3);
2323 p_es->fmt.psz_language[3] = 0;
2325 p_es->fmt.subs.dvb.i_id =
2326 sub->p_subtitle[n].i_composition_page_id;
2328 p_es->fmt.subs.dvb.i_id |=
2329 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
2331 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
2334 #endif /* _DVBPSI_DR_59_H_ */
2337 else if( p_es->i_type == 0xa0 )
2339 /* MSCODEC sent by vlc */
2340 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2342 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
2344 if( p_dr && p_dr->i_length >= 8 )
2346 pid->es->fmt.i_cat = VIDEO_ES;
2347 pid->es->fmt.i_codec =
2348 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
2349 p_dr->p_data[2], p_dr->p_data[3] );
2350 pid->es->fmt.video.i_width =
2351 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
2352 pid->es->fmt.video.i_height =
2353 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
2354 pid->es->fmt.i_extra =
2355 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
2357 if( pid->es->fmt.i_extra > 0 )
2359 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2360 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
2361 pid->es->fmt.i_extra );
2366 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
2369 /* For such stream we will gather them ourself and don't launch a
2371 * Yes it's ugly but it's the only way to have DIV3 working */
2372 pid->es->fmt.b_packetized = VLC_TRUE;
2375 if( pid->es->fmt.i_cat == AUDIO_ES ||
2376 ( pid->es->fmt.i_cat == SPU_ES &&
2377 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
2379 /* get language descriptor */
2380 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2381 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
2385 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
2389 pid->es->fmt.psz_language = malloc( 4 );
2390 memcpy( pid->es->fmt.psz_language,
2391 p_decoded->i_iso_639_code, 3 );
2392 pid->es->fmt.psz_language[3] = 0;
2397 pid->es->fmt.i_group = p_pmt->i_program_number;
2398 if( pid->es->fmt.i_cat == UNKNOWN_ES )
2400 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
2401 p_es->i_pid, p_es->i_type );
2403 else if( !p_sys->b_udp_out )
2405 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
2406 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
2408 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
2410 /* Check if we can avoid restarting the ES */
2412 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
2413 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
2414 pid->es->fmt.i_extra == 0 &&
2415 pid->i_extra_es == old_pid->i_extra_es &&
2416 ( ( !pid->es->fmt.psz_language &&
2417 !old_pid->es->fmt.psz_language ) ||
2418 ( pid->es->fmt.psz_language &&
2419 old_pid->es->fmt.psz_language &&
2420 !strcmp( pid->es->fmt.psz_language,
2421 old_pid->es->fmt.psz_language ) ) ) )
2423 pid->es->id = old_pid->es->id;
2424 old_pid->es->id = NULL;
2425 for( i = 0; i < pid->i_extra_es; i++ )
2427 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
2428 old_pid->extra_es[i]->id = NULL;
2435 PIDClean( p_demux->out, old_pid );
2436 TAB_REMOVE( i_clean, pp_clean, old_pid );
2440 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
2441 for( i = 0; i < pid->i_extra_es; i++ )
2443 pid->extra_es[i]->id =
2444 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
2449 /* Add ES to the list */
2452 PIDClean( p_demux->out, old_pid );
2453 TAB_REMOVE( i_clean, pp_clean, old_pid );
2455 p_sys->pid[p_es->i_pid] = *pid;
2457 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2458 p_dr = p_dr->p_next )
2460 if( p_dr->i_tag == 0x9 )
2462 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2464 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
2469 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2471 /* Set demux filter */
2472 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2473 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
2478 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2480 /* Set CAM descrambling */
2481 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2482 ACCESS_SET_PRIVATE_ID_CA, p_pmt );
2486 dvbpsi_DeletePMT( p_pmt );
2489 for ( i = 0; i < i_clean; i++ )
2491 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2493 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2494 ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
2498 PIDClean( p_demux->out, pp_clean[i] );
2500 if( i_clean ) free( pp_clean );
2503 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
2505 demux_sys_t *p_sys = p_demux->p_sys;
2506 dvbpsi_pat_program_t *p_program;
2507 ts_pid_t *pat = &p_sys->pid[0];
2510 msg_Dbg( p_demux, "PATCallBack called" );
2512 if( pat->psi->i_pat_version != -1 &&
2513 ( !p_pat->b_current_next ||
2514 p_pat->i_version == pat->psi->i_pat_version ) )
2516 dvbpsi_DeletePAT( p_pat );
2520 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
2521 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
2524 if( p_sys->i_pmt > 0 )
2527 ts_pid_t **pmt_rm = NULL;
2529 /* Search pmt to be deleted */
2530 for( i = 0; i < p_sys->i_pmt; i++ )
2532 ts_pid_t *pmt = p_sys->pmt[i];
2533 vlc_bool_t b_keep = VLC_FALSE;
2535 for( p_program = p_pat->p_first_program; p_program != NULL;
2536 p_program = p_program->p_next )
2538 if( p_program->i_pid == pmt->i_pid )
2541 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2543 if( p_program->i_number ==
2544 pmt->psi->prg[i_prg]->i_number )
2556 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
2560 /* Delete all ES attached to thoses PMT */
2561 for( i = 2; i < 8192; i++ )
2563 ts_pid_t *pid = &p_sys->pid[i];
2565 if( !pid->b_valid || pid->psi ) continue;
2567 for( j = 0; j < i_pmt_rm; j++ )
2570 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
2572 /* We only remove es that aren't defined by extra pmt */
2573 if( pid->p_owner->prg[i_prg]->i_pid_pmt !=
2574 pmt_rm[j]->i_pid ) continue;
2576 if( p_sys->b_dvb_control && pid->es->id )
2578 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2579 ACCESS_SET_PRIVATE_ID_STATE, i,
2581 p_sys->b_dvb_control = VLC_FALSE;
2584 PIDClean( p_demux->out, pid );
2588 if( !pid->b_valid ) break;
2592 /* Delete PMT pid */
2593 for( i = 0; i < i_pmt_rm; i++ )
2595 if( p_sys->b_dvb_control )
2597 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2598 ACCESS_SET_PRIVATE_ID_STATE,
2599 pmt_rm[i]->i_pid, VLC_FALSE ) )
2600 p_sys->b_dvb_control = VLC_FALSE;
2603 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
2604 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
2607 if( pmt_rm ) free( pmt_rm );
2610 /* now create programs */
2611 for( p_program = p_pat->p_first_program; p_program != NULL;
2612 p_program = p_program->p_next )
2614 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
2616 if( p_program->i_number != 0 )
2618 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
2619 vlc_bool_t b_add = VLC_TRUE;
2624 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2626 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
2635 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
2640 PIDInit( pmt, VLC_TRUE, pat->psi );
2641 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
2642 dvbpsi_AttachPMT( p_program->i_number,
2643 (dvbpsi_pmt_callback)PMTCallBack,
2645 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
2646 p_program->i_number;
2647 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
2650 /* Now select PID at access level */
2651 if( p_sys->b_dvb_control )
2653 if( DVBProgramIsSelected( p_demux, p_program->i_number ) )
2655 if( p_sys->i_dvb_program == 0 )
2656 p_sys->i_dvb_program = p_program->i_number;
2658 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
2659 p_sys->b_dvb_control = VLC_FALSE;
2663 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_FALSE ) )
2664 p_sys->b_dvb_control = VLC_FALSE;
2670 pat->psi->i_pat_version = p_pat->i_version;
2672 dvbpsi_DeletePAT( p_pat );