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( _("MPEG Transport Stream demuxer") );
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 ) <
330 TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
332 /* Search first sync byte */
333 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
335 if( p_peek[i_sync] == 0x47 ) break;
337 if( i_sync >= TS_PACKET_SIZE_MAX )
339 if( strcmp( p_demux->psz_demux, "ts" ) ) return VLC_EGENERIC;
340 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
343 /* Check next 3 sync bytes */
344 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
345 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
347 msg_Err( p_demux, "cannot peek" );
350 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
351 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
352 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
354 i_packet_size = TS_PACKET_SIZE_188;
356 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
357 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
358 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
360 i_packet_size = TS_PACKET_SIZE_192;
362 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
363 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
364 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
366 i_packet_size = TS_PACKET_SIZE_204;
368 else if( !strcmp( p_demux->psz_demux, "ts" ) )
370 i_packet_size = TS_PACKET_SIZE_188;
374 msg_Warn( p_demux, "TS module discarded (lost sync)" );
378 /* Fill p_demux field */
379 p_demux->pf_demux = Demux;
380 p_demux->pf_control = Control;
381 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
382 memset( p_sys, 0, sizeof( demux_sys_t ) );
384 /* Init p_sys field */
385 p_sys->b_dvb_control = VLC_TRUE;
386 p_sys->i_dvb_program = 0;
387 for( i = 0; i < 8192; i++ )
389 ts_pid_t *pid = &p_sys->pid[i];
392 pid->b_seen = VLC_FALSE;
393 pid->b_valid = VLC_FALSE;
395 p_sys->i_packet_size = i_packet_size;
396 p_sys->b_udp_out = VLC_FALSE;
397 p_sys->i_ts_read = 50;
400 /* Init PAT handler */
401 pat = &p_sys->pid[0];
402 PIDInit( pat, VLC_TRUE, NULL );
403 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
411 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
412 var_Get( p_demux, "ts-es-id-pid", &val );
413 p_sys->b_es_id_pid = val.b_bool,
415 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
416 var_Get( p_demux, "ts-out", &val );
417 if( val.psz_string && *val.psz_string )
420 char *psz = strchr( val.psz_string, ':' );
423 p_sys->b_udp_out = VLC_TRUE;
428 i_port = atoi( psz );
430 if( i_port <= 0 ) i_port = 1234;
431 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
433 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
436 msg_Err( p_demux, "failed to open udp socket, send disabled" );
437 p_sys->b_udp_out = VLC_FALSE;
441 var_Create( p_demux, "ts-out-mtu",
442 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
443 var_Get( p_demux, "ts-out-mtu", &mtu );
444 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
445 if( p_sys->i_ts_read <= 0 )
447 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
449 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
454 free( val.psz_string );
458 /* We handle description of an extra PMT */
459 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
460 var_Get( p_demux, "ts-extra-pmt", &val );
461 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
463 char *psz = val.psz_string;
464 int i_pid = strtol( psz, &psz, 0 );
466 if( i_pid >= 2 && i_pid < 8192 )
468 ts_pid_t *pmt = &p_sys->pid[i_pid];
470 msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
471 PIDInit( pmt, VLC_TRUE, NULL );
473 pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
474 /* FIXME we should also ask for a number */
475 pmt->psi->prg[0]->handle =
476 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
478 pmt->psi->prg[0]->i_number = 0; /* special one */
480 psz = strchr( psz, '=' ) + 1; /* can't failed */
483 char *psz_next = strchr( psz, ',' );
484 int i_pid, i_stream_type;
491 i_pid = strtol( psz, &psz, 0 );
494 i_stream_type = strtol( psz + 1, &psz, 0 );
495 if( i_pid >= 2 && i_pid < 8192 &&
496 !p_sys->pid[i_pid].b_valid )
498 ts_pid_t *pid = &p_sys->pid[i_pid];
500 PIDInit( pid, VLC_FALSE, pmt->psi);
501 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
503 pmt->psi->prg[0]->i_pid_pcr = i_pid;
505 PIDFillFormat( pid, i_stream_type);
506 if( pid->es->fmt.i_cat != UNKNOWN_ES )
508 if( p_sys->b_es_id_pid )
510 pid->es->fmt.i_id = i_pid;
512 msg_Dbg( p_demux, " * es pid=%d type=%d "
513 "fcc=%4.4s", i_pid, i_stream_type,
514 (char*)&pid->es->fmt.i_codec );
515 pid->es->id = es_out_Add( p_demux->out,
526 free( val.psz_string );
529 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
530 var_Get( p_demux, "ts-csa-ck", &val );
531 if( val.psz_string && *val.psz_string )
533 char *psz = val.psz_string;
534 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
538 if( strlen( psz ) != 16 )
540 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
545 uint64_t i_ck = strtoull( psz, NULL, 16 );
547 uint64_t i_ck = strtoll( psz, NULL, 16 );
551 for( i = 0; i < 8; i++ )
553 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
556 msg_Dbg( p_demux, "using CSA scrambling with "
557 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
558 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
560 p_sys->csa = csa_New();
561 csa_SetCW( p_sys->csa, ck, ck );
566 free( val.psz_string );
569 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
570 var_Get( p_demux, "ts-silent", &val );
571 p_sys->b_silent = val.b_bool;
573 var_Create( p_demux, "ts-capmt-sysid", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
574 var_Get( p_demux, "ts-capmt-sysid", &val );
575 p_sys->i_capmt_sysid = val.i_int;
580 /*****************************************************************************
582 *****************************************************************************/
583 static void Close( vlc_object_t *p_this )
585 demux_t *p_demux = (demux_t*)p_this;
586 demux_sys_t *p_sys = p_demux->p_sys;
590 msg_Dbg( p_demux, "pid list:" );
591 for( i = 0; i < 8192; i++ )
593 ts_pid_t *pid = &p_sys->pid[i];
595 if( pid->b_valid && pid->psi )
600 dvbpsi_DetachPAT( pid->psi->handle );
607 PIDClean( p_demux->out, pid );
611 else if( pid->b_valid && pid->es )
613 PIDClean( p_demux->out, pid );
618 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
621 if( p_sys->b_dvb_control && pid->i_pid > 0 )
624 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
629 if( p_sys->b_udp_out )
631 net_Close( p_sys->fd );
632 free( p_sys->buffer );
636 csa_Delete( p_sys->csa );
639 if( p_sys->i_pmt ) free( p_sys->pmt );
641 if ( p_sys->p_programs_list )
644 val.p_list = p_sys->p_programs_list;
645 var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
651 /*****************************************************************************
653 *****************************************************************************/
654 static int Demux( demux_t *p_demux )
656 demux_sys_t *p_sys = p_demux->p_sys;
659 /* We read at most 100 TS packet or until a frame is completed */
660 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
662 vlc_bool_t b_frame = VLC_FALSE;
666 /* Get a new TS packet */
667 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
669 msg_Dbg( p_demux, "eof ?" );
673 /* Check sync byte and re-sync if needed */
674 if( p_pkt->p_buffer[0] != 0x47 )
676 msg_Warn( p_demux, "lost synchro" );
677 block_Release( p_pkt );
679 while( !p_demux->b_die )
682 int i_peek, i_skip = 0;
684 i_peek = stream_Peek( p_demux->s, &p_peek,
685 p_sys->i_packet_size * 10 );
686 if( i_peek < p_sys->i_packet_size + 1 )
688 msg_Dbg( p_demux, "eof ?" );
692 while( i_skip < i_peek - p_sys->i_packet_size )
694 if( p_peek[i_skip] == 0x47 &&
695 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
702 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
703 stream_Read( p_demux->s, NULL, i_skip );
705 if( i_skip < i_peek - p_sys->i_packet_size )
711 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
713 msg_Dbg( p_demux, "eof ?" );
718 if( p_sys->b_udp_out )
720 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
721 p_pkt->p_buffer, p_sys->i_packet_size );
724 /* Parse the TS packet */
725 p_pid = &p_sys->pid[PIDGet( p_pkt )];
731 if( p_pid->i_pid == 0 )
733 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
738 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
740 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
744 block_Release( p_pkt );
746 else if( !p_sys->b_udp_out )
748 b_frame = GatherPES( p_demux, p_pid, p_pkt );
752 PCRHandle( p_demux, p_pid, p_pkt );
753 block_Release( p_pkt );
760 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
762 /* We have to handle PCR if present */
763 PCRHandle( p_demux, p_pid, p_pkt );
764 block_Release( p_pkt );
766 p_pid->b_seen = VLC_TRUE;
774 if( p_sys->b_udp_out )
776 /* Send the complete block */
777 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
778 p_sys->i_ts_read * p_sys->i_packet_size );
784 /*****************************************************************************
786 *****************************************************************************/
787 static int Control( demux_t *p_demux, int i_query, va_list args )
789 demux_sys_t *p_sys = p_demux->p_sys;
796 case DEMUX_GET_POSITION:
797 pf = (double*) va_arg( args, double* );
798 i64 = stream_Size( p_demux->s );
801 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
808 case DEMUX_SET_POSITION:
809 f = (double) va_arg( args, double );
810 i64 = stream_Size( p_demux->s );
812 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
813 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
821 pi64 = (int64_t*)va_arg( args, int64_t * );
822 if( p_sys->i_time < 0 )
827 *pi64 = p_sys->i_time;
830 case DEMUX_GET_LENGTH:
831 pi64 = (int64_t*)va_arg( args, int64_t * );
832 if( p_sys->i_mux_rate > 0 )
834 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
841 case DEMUX_SET_GROUP:
843 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
844 ts_prg_psi_t *p_prg = NULL;
847 i_int = (int)va_arg( args, int );
848 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
849 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
851 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
856 /* Search pmt to be unselected */
857 for( i = 0; i < p_sys->i_pmt; i++ )
859 ts_pid_t *pmt = p_sys->pmt[i];
862 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
864 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
866 i_pmt_pid = p_sys->pmt[i]->i_pid;
870 if( i_pmt_pid > 0 ) break;
875 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
876 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
879 for( i = 2; i < 8192; i++ )
881 ts_pid_t *pid = &p_sys->pid[i];
884 if( !pid->b_valid || pid->psi ) continue;
886 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
888 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
890 /* We only remove es that aren't defined by extra pmt */
891 stream_Control( p_demux->s,
892 STREAM_CONTROL_ACCESS,
893 ACCESS_SET_PRIVATE_ID_STATE,
901 /* select new program */
902 p_sys->i_dvb_program = i_int;
904 for( i = 0; i < p_sys->i_pmt; i++ )
906 ts_pid_t *pmt = p_sys->pmt[i];
909 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
911 if( pmt->psi->prg[i_prg]->i_number == i_int )
913 i_pmt_pid = p_sys->pmt[i]->i_pid;
914 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
918 if( i_pmt_pid > 0 ) break;
922 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
923 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
925 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
926 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
929 for( i = 2; i < 8192; i++ )
931 ts_pid_t *pid = &p_sys->pid[i];
934 if( !pid->b_valid || pid->psi ) continue;
936 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
938 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
940 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
942 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
944 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
946 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
949 stream_Control( p_demux->s,
950 STREAM_CONTROL_ACCESS,
951 ACCESS_SET_PRIVATE_ID_STATE,
961 p_sys->i_dvb_program = -1;
962 p_sys->p_programs_list = p_list;
974 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
976 vlc_bool_t b_old_valid = pid->b_valid;
978 pid->b_valid = VLC_TRUE;
980 pid->p_owner = p_owner;
981 pid->i_owner_number = 0;
983 pid->extra_es = NULL;
992 pid->psi = malloc( sizeof( ts_psi_t ) );
994 pid->psi->prg = NULL;
995 pid->psi->handle= NULL;
997 pid->psi->i_pat_version = -1;
1000 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1002 prg->i_version = -1;
1004 prg->i_pid_pcr = -1;
1005 prg->i_pid_pmt = -1;
1009 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1015 pid->es = malloc( sizeof( ts_es_t ) );
1017 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1019 pid->es->p_pes = NULL;
1020 pid->es->i_pes_size= 0;
1021 pid->es->i_pes_gathered= 0;
1022 pid->es->pp_last = &pid->es->p_pes;
1023 pid->es->p_mpeg4desc = NULL;
1024 pid->es->b_gather = VLC_FALSE;
1028 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1034 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1035 for( i = 0; i < pid->psi->i_prg; i++ )
1037 if( pid->psi->prg[i]->iod )
1038 IODFree( pid->psi->prg[i]->iod );
1039 if( pid->psi->prg[i]->handle )
1040 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1041 free( pid->psi->prg[i] );
1043 if( pid->psi->prg ) free( pid->psi->prg );
1051 es_out_Del( out, pid->es->id );
1053 if( pid->es->p_pes )
1054 block_ChainRelease( pid->es->p_pes );
1056 es_format_Clean( &pid->es->fmt );
1060 for( i = 0; i < pid->i_extra_es; i++ )
1062 if( pid->extra_es[i]->id )
1063 es_out_Del( out, pid->extra_es[i]->id );
1065 if( pid->extra_es[i]->p_pes )
1066 block_ChainRelease( pid->extra_es[i]->p_pes );
1068 es_format_Clean( &pid->extra_es[i]->fmt );
1070 free( pid->extra_es[i] );
1072 if( pid->i_extra_es ) free( pid->extra_es );
1075 pid->b_valid = VLC_FALSE;
1078 /****************************************************************************
1080 ****************************************************************************/
1081 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1083 block_t *p_pes = pid->es->p_pes;
1089 mtime_t i_length = 0;
1092 /* remove the pes from pid */
1093 pid->es->p_pes = NULL;
1094 pid->es->i_pes_size= 0;
1095 pid->es->i_pes_gathered= 0;
1096 pid->es->pp_last = &pid->es->p_pes;
1098 /* FIXME find real max size */
1099 i_max = block_ChainExtract( p_pes, header, 30 );
1102 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1104 if( !p_demux->p_sys->b_silent )
1105 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1106 header[0], header[1],header[2],header[3], pid->i_pid );
1107 block_ChainRelease( p_pes );
1111 /* TODO check size */
1114 case 0xBC: /* Program stream map */
1115 case 0xBE: /* Padding */
1116 case 0xBF: /* Private stream 2 */
1117 case 0xB0: /* ECM */
1118 case 0xB1: /* EMM */
1119 case 0xFF: /* Program stream directory */
1120 case 0xF2: /* DSMCC stream */
1121 case 0xF8: /* ITU-T H.222.1 type E stream */
1125 if( ( header[6]&0xC0 ) == 0x80 )
1128 i_skip = header[8] + 9;
1130 if( header[7]&0x80 ) /* has pts */
1132 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1133 (mtime_t)(header[10] << 22)|
1134 ((mtime_t)(header[11]&0xfe) << 14)|
1135 (mtime_t)(header[12] << 7)|
1136 (mtime_t)(header[13] >> 1);
1138 if( header[7]&0x40 ) /* has dts */
1140 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1141 (mtime_t)(header[15] << 22)|
1142 ((mtime_t)(header[16]&0xfe) << 14)|
1143 (mtime_t)(header[17] << 7)|
1144 (mtime_t)(header[18] >> 1);
1151 while( i_skip < 23 && header[i_skip] == 0xff )
1157 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1158 block_ChainRelease( p_pes );
1161 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1166 if( header[i_skip]&0x20 )
1168 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1169 (mtime_t)(header[i_skip+1] << 22)|
1170 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1171 (mtime_t)(header[i_skip+3] << 7)|
1172 (mtime_t)(header[i_skip+4] >> 1);
1174 if( header[i_skip]&0x10 ) /* has dts */
1176 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1177 (mtime_t)(header[i_skip+6] << 22)|
1178 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1179 (mtime_t)(header[i_skip+8] << 7)|
1180 (mtime_t)(header[i_skip+9] >> 1);
1196 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1197 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1201 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1202 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1203 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1207 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1208 pid->es->p_mpeg4desc )
1210 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1212 if( dcd->i_decoder_specific_info_len > 2 &&
1213 dcd->p_decoder_specific_info[0] == 0x10 &&
1214 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1216 /* display length */
1217 if( p_pes->i_buffer + 2 <= i_skip )
1219 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1224 if( p_pes->i_buffer + 2 <= i_skip )
1226 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1233 while( p_pes && i_skip > 0 )
1235 if( p_pes->i_buffer <= i_skip )
1237 block_t *p_next = p_pes->p_next;
1239 i_skip -= p_pes->i_buffer;
1240 block_Release( p_pes );
1245 p_pes->i_buffer -= i_skip;
1246 p_pes->p_buffer += i_skip;
1251 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1252 if( i_pts >= 0 && i_dts < 0 )
1262 p_pes->i_dts = i_dts * 100 / 9;
1266 p_pes->i_pts = i_pts * 100 / 9;
1268 p_pes->i_length = i_length * 100 / 9;
1270 p_block = block_ChainGather( p_pes );
1271 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1273 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1275 p_block->i_buffer = i_pes_size;
1278 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1279 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1282 for( i = 0; i < pid->i_extra_es; i++ )
1284 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1285 block_Duplicate( p_block ) );
1288 es_out_Send( p_demux->out, pid->es->id, p_block );
1292 msg_Warn( p_demux, "empty pes" );
1296 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1298 demux_sys_t *p_sys = p_demux->p_sys;
1299 const uint8_t *p = p_bk->p_buffer;
1301 if( ( p[3]&0x20 ) && /* adaptation */
1306 mtime_t i_pcr; /* 33 bits */
1308 i_pcr = ( (mtime_t)p[6] << 25 ) |
1309 ( (mtime_t)p[7] << 17 ) |
1310 ( (mtime_t)p[8] << 9 ) |
1311 ( (mtime_t)p[9] << 1 ) |
1312 ( (mtime_t)p[10] >> 7 );
1314 /* Search program and set the PCR */
1315 for( i = 0; i < p_sys->i_pmt; i++ )
1318 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1320 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1322 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1323 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1324 (int64_t)(i_pcr * 100 / 9) );
1331 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1333 const uint8_t *p = p_bk->p_buffer;
1334 const vlc_bool_t b_unit_start = p[1]&0x40;
1335 const vlc_bool_t b_adaptation = p[3]&0x20;
1336 const vlc_bool_t b_payload = p[3]&0x10;
1337 const int i_cc = p[3]&0x0f; /* continuity counter */
1339 /* transport_scrambling_control is ignored */
1342 vlc_bool_t i_ret = VLC_FALSE;
1346 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1347 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1351 /* For now, ignore additional error correction
1352 * TODO: handle Reed-Solomon 204,188 error correction */
1353 p_bk->i_buffer = TS_PACKET_SIZE_188;
1357 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1361 if( p_demux->p_sys->csa )
1363 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
1368 /* We don't have any adaptation_field, so payload starts
1369 * immediately after the 4 byte TS header */
1374 /* p[4] is adaptation_field_length minus one */
1380 msg_Warn( p_demux, "discontinuity_indicator (pid=%d) "
1381 "ignored", pid->i_pid );
1386 /* Test continuity counter */
1387 /* continuous when (one of this):
1389 * diff == 0 and payload == 0
1390 * diff == 0 and duplicate packet (playload != 0) <- should we
1391 * test the content ?
1394 i_diff = ( i_cc - pid->i_cc )&0x0f;
1395 if( b_payload && i_diff == 1 )
1401 if( pid->i_cc == 0xff )
1403 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1407 else if( i_diff != 0 )
1409 /* FIXME what to do when discontinuity_indicator is set ? */
1410 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
1411 i_cc, ( pid->i_cc + 1 )&0x0f );
1415 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1417 /* Small video artifacts are usually better then
1418 * dropping full frames */
1419 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1424 PCRHandle( p_demux, pid, p_bk );
1426 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1428 block_Release( p_bk );
1432 /* We have to gather it */
1433 p_bk->p_buffer += i_skip;
1434 p_bk->i_buffer -= i_skip;
1438 if( pid->es->p_pes )
1440 ParsePES( p_demux, pid );
1444 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1445 if( p_bk->i_buffer > 6 )
1447 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1448 if( pid->es->i_pes_size > 0 )
1450 pid->es->i_pes_size += 6;
1453 pid->es->i_pes_gathered += p_bk->i_buffer;
1454 if( pid->es->i_pes_size > 0 &&
1455 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1457 ParsePES( p_demux, pid );
1463 if( pid->es->p_pes == NULL )
1465 /* msg_Dbg( p_demux, "broken packet" ); */
1466 block_Release( p_bk );
1470 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1471 pid->es->i_pes_gathered += p_bk->i_buffer;
1472 if( pid->es->i_pes_size > 0 &&
1473 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1475 ParsePES( p_demux, pid );
1484 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1486 es_format_t *fmt = &pid->es->fmt;
1488 switch( i_stream_type )
1490 case 0x01: /* MPEG-1 video */
1491 case 0x02: /* MPEG-2 video */
1492 case 0x80: /* MPEG-2 MOTO video */
1493 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1495 case 0x03: /* MPEG-1 audio */
1496 case 0x04: /* MPEG-2 audio */
1497 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1499 case 0x11: /* MPEG4 (audio) */
1500 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1501 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1503 case 0x10: /* MPEG4 (video) */
1504 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1505 pid->es->b_gather = VLC_TRUE;
1507 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1508 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1511 case 0x81: /* A52 (audio) */
1512 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1514 case 0x82: /* DVD_SPU (sub) */
1515 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1517 case 0x83: /* LPCM (audio) */
1518 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1520 case 0x84: /* SDDS (audio) */
1521 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1523 case 0x85: /* DTS (audio) */
1524 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1527 case 0x91: /* A52 vls (audio) */
1528 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1530 case 0x92: /* DVD_SPU vls (sub) */
1531 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1533 case 0x93: /* LPCM vls (audio) */
1534 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1536 case 0x94: /* SDDS (audio) */
1537 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1540 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1541 es_format_Init( fmt, UNKNOWN_ES, 0 );
1542 pid->es->b_gather = VLC_TRUE;
1545 case 0x06: /* PES_PRIVATE (fixed later) */
1546 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1548 es_format_Init( fmt, UNKNOWN_ES, 0 );
1552 /* PES packets usually contain truncated frames */
1553 fmt->b_packetized = VLC_FALSE;
1555 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1558 /*****************************************************************************
1559 * MP4 specific functions (IOD parser)
1560 *****************************************************************************/
1561 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1564 unsigned int i_len = 0;
1570 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1572 } while( i_b&0x80 );
1576 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1580 const int i_b = **pp_data;
1587 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1589 const int i1 = IODGetByte( pi_data, pp_data );
1590 const int i2 = IODGetByte( pi_data, pp_data );
1591 return( ( i1 << 8 ) | i2 );
1593 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1595 const int i1 = IODGetByte( pi_data, pp_data );
1596 const int i2 = IODGetByte( pi_data, pp_data );
1597 const int i3 = IODGetByte( pi_data, pp_data );
1599 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1602 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1604 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1605 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1606 return( ( i1 << 16 ) | i2 );
1609 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1614 i_url_len = IODGetByte( pi_data, pp_data );
1615 url = malloc( i_url_len + 1 );
1616 for( i = 0; i < i_url_len; i++ )
1618 url[i] = IODGetByte( pi_data, pp_data );
1620 url[i_url_len] = '\0';
1624 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1626 iod_descriptor_t *p_iod;
1633 p_iod = malloc( sizeof( iod_descriptor_t ) );
1634 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1636 fprintf( stderr, "\n************ IOD ************" );
1637 for( i = 0; i < 255; i++ )
1639 p_iod->es_descr[i].b_ok = 0;
1648 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1649 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1650 fprintf( stderr, "\n* ===========" );
1651 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1653 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1655 fprintf( stderr, "\n ERR: tag != 0x02" );
1659 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1660 fprintf( stderr, "\n* length:%d", i_iod_length );
1661 if( i_iod_length > i_data )
1663 i_iod_length = i_data;
1666 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1667 i_flags = IODGetByte( &i_data, &p_data );
1668 p_iod->i_od_id |= i_flags >> 6;
1669 b_url = ( i_flags >> 5 )&0x01;
1671 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1672 fprintf( stderr, "\n* url flag:%d", b_url );
1673 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1677 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1678 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1679 fprintf( stderr, "\n*****************************\n" );
1684 p_iod->psz_url = NULL;
1687 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1688 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1689 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1690 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1691 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1693 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1694 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1695 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1696 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1697 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1700 while( i_data > 0 && i_es_index < 255)
1702 int i_tag, i_length;
1704 uint8_t *p_data_sav;
1706 i_tag = IODGetByte( &i_data, &p_data );
1707 i_length = IODDescriptorLength( &i_data, &p_data );
1709 i_data_sav = i_data;
1710 p_data_sav = p_data;
1718 #define es_descr p_iod->es_descr[i_es_index]
1719 int i_decoderConfigDescr_length;
1720 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1723 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1724 i_flags = IODGetByte( &i_data, &p_data );
1725 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1726 b_url = ( i_flags >> 6 )&0x01;
1727 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1728 es_descr.i_streamPriority = i_flags & 0x1f;
1729 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1730 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1731 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1733 if( es_descr.b_streamDependenceFlag )
1735 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1736 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1741 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1742 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1746 es_descr.psz_url = NULL;
1749 if( es_descr.b_OCRStreamFlag )
1751 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1752 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1755 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1757 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1761 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1763 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1764 #define dec_descr es_descr.dec_descr
1765 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1766 i_flags = IODGetByte( &i_data, &p_data );
1767 dec_descr.i_streamType = i_flags >> 2;
1768 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1769 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1770 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1771 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1772 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1773 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1774 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1775 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1776 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1777 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1778 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1781 dec_descr.i_decoder_specific_info_len =
1782 IODDescriptorLength( &i_data, &p_data );
1783 if( dec_descr.i_decoder_specific_info_len > 0 )
1785 dec_descr.p_decoder_specific_info =
1786 malloc( dec_descr.i_decoder_specific_info_len );
1788 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1790 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1795 dec_descr.i_decoder_specific_info_len = 0;
1796 dec_descr.p_decoder_specific_info = NULL;
1800 #define sl_descr es_descr.sl_descr
1802 int i_SLConfigDescr_length;
1805 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1807 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1811 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1813 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1814 i_predefined = IODGetByte( &i_data, &p_data );
1815 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1816 switch( i_predefined )
1820 sl_descr.b_useAccessUnitStartFlag = 0;
1821 sl_descr.b_useAccessUnitEndFlag = 0;
1822 sl_descr.b_useRandomAccessPointFlag = 0;
1823 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1824 sl_descr.b_usePaddingFlag = 0;
1825 sl_descr.b_useTimeStampsFlags = 0;
1826 sl_descr.b_useIdleFlag = 0;
1827 sl_descr.b_durationFlag = 0; // FIXME FIXME
1828 sl_descr.i_timeStampResolution = 1000;
1829 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1830 sl_descr.i_timeStampLength = 32;
1831 sl_descr.i_OCRLength = 0; // FIXME FIXME
1832 sl_descr.i_AU_Length = 0;
1833 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1834 sl_descr.i_degradationPriorityLength= 0;
1835 sl_descr.i_AU_seqNumLength = 0;
1836 sl_descr.i_packetSeqNumLength = 0;
1837 if( sl_descr.b_durationFlag )
1839 sl_descr.i_timeScale = 0; // FIXME FIXME
1840 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1841 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1843 if( !sl_descr.b_useTimeStampsFlags )
1845 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1846 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1851 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1860 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1864 p_data = p_data_sav + i_length;
1865 i_data = i_data_sav - i_length;
1870 fprintf( stderr, "\n*****************************\n" );
1874 static void IODFree( iod_descriptor_t *p_iod )
1878 if( p_iod->psz_url )
1880 free( p_iod->psz_url );
1881 p_iod->psz_url = NULL;
1886 for( i = 0; i < 255; i++ )
1888 #define es_descr p_iod->es_descr[i]
1891 if( es_descr.psz_url )
1893 free( es_descr.psz_url );
1894 es_descr.psz_url = NULL;
1898 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1900 free( es_descr.dec_descr.p_decoder_specific_info );
1901 es_descr.dec_descr.p_decoder_specific_info = NULL;
1902 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1912 /****************************************************************************
1913 ****************************************************************************
1914 ** libdvbpsi callbacks
1915 ****************************************************************************
1916 ****************************************************************************/
1917 static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
1919 demux_sys_t *p_sys = p_demux->p_sys;
1921 if ( !p_sys->b_dvb_control ) return VLC_FALSE;
1922 if ( p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL )
1924 if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
1926 if ( p_sys->p_programs_list != NULL )
1929 for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
1931 if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
1938 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1940 demux_sys_t *p_sys = p_demux->p_sys;
1941 dvbpsi_descriptor_t *p_dr;
1942 dvbpsi_pmt_es_t *p_es;
1944 ts_pid_t *pmt = NULL;
1945 ts_prg_psi_t *prg = NULL;
1947 ts_pid_t **pp_clean = NULL;
1950 msg_Dbg( p_demux, "PMTCallBack called" );
1952 /* First find this PMT declared in PAT */
1953 for( i = 0; i < p_sys->i_pmt; i++ )
1956 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1958 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
1959 p_pmt->i_program_number )
1961 pmt = p_sys->pmt[i];
1962 prg = p_sys->pmt[i]->psi->prg[i_prg];
1971 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1972 dvbpsi_DeletePMT(p_pmt);
1976 if( prg->i_version != -1 &&
1977 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
1979 dvbpsi_DeletePMT( p_pmt );
1983 /* Clean this program (remove all es) */
1984 for( i = 0; i < 8192; i++ )
1986 ts_pid_t *pid = &p_sys->pid[i];
1988 if( pid->b_valid && pid->p_owner == pmt->psi &&
1989 pid->i_owner_number == prg->i_number && pid->psi == NULL )
1991 TAB_APPEND( i_clean, pp_clean, pid );
1996 IODFree( prg->iod );
2000 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
2001 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
2002 prg->i_pid_pcr = p_pmt->i_pcr_pid;
2003 prg->i_version = p_pmt->i_version;
2005 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2007 /* Set demux filter */
2008 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2009 ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
2012 else if ( p_sys->b_dvb_control )
2014 msg_Warn( p_demux, "skipping program (not selected)" );
2015 dvbpsi_DeletePMT(p_pmt);
2019 /* Parse descriptor */
2020 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
2022 if( p_dr->i_tag == 0x1d )
2024 /* We have found an IOD descriptor */
2025 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
2027 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
2029 else if( p_dr->i_tag == 0x9 )
2031 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2033 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
2037 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2041 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2043 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
2045 /* Find out if the PID was already declared */
2046 for( i = 0; i < i_clean; i++ )
2048 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
2050 old_pid = pp_clean[i];
2055 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
2057 msg_Warn( p_demux, "pmt error: pid=%d already defined",
2062 PIDInit( pid, VLC_FALSE, pmt->psi );
2063 PIDFillFormat( pid, p_es->i_type );
2064 pid->i_owner_number = prg->i_number;
2065 pid->i_pid = p_es->i_pid;
2066 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
2068 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2069 p_es->i_type == 0x12 )
2071 /* MPEG-4 stream: search SL_DESCRIPTOR */
2072 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2074 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2076 if( p_dr && p_dr->i_length == 2 )
2078 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2080 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2082 pid->es->p_mpeg4desc = NULL;
2084 for( i = 0; i < 255; i++ )
2086 iod_descriptor_t *iod = prg->iod;
2088 if( iod->es_descr[i].b_ok &&
2089 iod->es_descr[i].i_es_id == i_es_id )
2091 pid->es->p_mpeg4desc = &iod->es_descr[i];
2097 if( pid->es->p_mpeg4desc != NULL )
2099 decoder_config_descriptor_t *dcd =
2100 &pid->es->p_mpeg4desc->dec_descr;
2102 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2104 pid->es->fmt.i_cat = VIDEO_ES;
2105 switch( dcd->i_objectTypeIndication )
2107 case 0x0B: /* mpeg4 sub */
2108 pid->es->fmt.i_cat = SPU_ES;
2109 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2112 case 0x20: /* mpeg4 */
2113 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2120 case 0x65: /* mpeg2 */
2121 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2123 case 0x6a: /* mpeg1 */
2124 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2126 case 0x6c: /* mpeg1 */
2127 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2130 pid->es->fmt.i_cat = UNKNOWN_ES;
2134 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2136 pid->es->fmt.i_cat = AUDIO_ES;
2137 switch( dcd->i_objectTypeIndication )
2139 case 0x40: /* mpeg4 */
2140 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2144 case 0x68: /* mpeg2 aac */
2145 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2147 case 0x69: /* mpeg2 */
2148 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2150 case 0x6b: /* mpeg1 */
2151 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2154 pid->es->fmt.i_cat = UNKNOWN_ES;
2160 pid->es->fmt.i_cat = UNKNOWN_ES;
2163 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2165 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2166 if( pid->es->fmt.i_extra > 0 )
2168 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2169 memcpy( pid->es->fmt.p_extra,
2170 dcd->p_decoder_specific_info,
2171 pid->es->fmt.i_extra );
2176 else if( p_es->i_type == 0x06 )
2178 dvbpsi_descriptor_t *p_dr;
2180 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2181 p_dr = p_dr->p_next )
2183 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
2184 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2186 if( p_dr->i_tag == 0x05 )
2188 /* Registration Descriptor */
2189 if( p_dr->i_length != 4 )
2191 msg_Warn( p_demux, "invalid Registration Descriptor" );
2195 if( !memcmp( p_dr->p_data, "AC-3", 4 ) )
2197 /* ATSC with stream_type 0x81 (but this descriptor
2198 * is then not mandatory */
2199 pid->es->fmt.i_cat = AUDIO_ES;
2200 pid->es->fmt.i_codec = VLC_FOURCC('a','5','2',' ');
2202 else if( !memcmp( p_dr->p_data, "DTS1", 4 ) ||
2203 !memcmp( p_dr->p_data, "DTS2", 4 ) ||
2204 !memcmp( p_dr->p_data, "DTS3", 4 ) )
2206 /*registration descriptor(ETSI TS 101 154 Annex F)*/
2207 pid->es->fmt.i_cat = AUDIO_ES;
2208 pid->es->fmt.i_codec = VLC_FOURCC('d','t','s',' ');
2210 else if( !memcmp( p_dr->p_data, "BSSD", 4 ) )
2212 pid->es->fmt.i_cat = AUDIO_ES;
2213 pid->es->fmt.i_codec = VLC_FOURCC('l','p','c','m');
2218 "unknown Registration Descriptor (%4.4s)",
2224 else if( p_dr->i_tag == 0x6a )
2226 /* DVB with stream_type 0x06 */
2227 pid->es->fmt.i_cat = AUDIO_ES;
2228 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2230 else if( p_dr->i_tag == 0x73 )
2232 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2233 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2234 pid->es->fmt.i_cat = AUDIO_ES;
2235 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2237 else if( p_dr->i_tag == 0x45 )
2239 msg_Dbg( p_demux, " * VBI Data descriptor" );
2240 pid->es->fmt.i_cat = SPU_ES;
2241 pid->es->fmt.i_codec = VLC_FOURCC( 'v', 'b', 'i', 'd' );
2242 pid->es->fmt.psz_description = strdup( "VBI Data" );
2243 pid->es->fmt.i_extra = p_dr->i_length;
2244 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2245 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2248 else if( p_dr->i_tag == 0x46 )
2250 msg_Dbg( p_demux, " * VBI Teletext descriptor" );
2251 pid->es->fmt.i_cat = SPU_ES;
2252 pid->es->fmt.i_codec = VLC_FOURCC( 'v', 'b', 'i', 't' );
2253 pid->es->fmt.psz_description = strdup( "VBI Teletext" );
2254 pid->es->fmt.i_extra = p_dr->i_length;
2255 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2256 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2259 else if( p_dr->i_tag == 0x56 )
2261 msg_Dbg( p_demux, " * EBU Teletext descriptor" );
2262 pid->es->fmt.i_cat = SPU_ES;
2263 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
2264 pid->es->fmt.psz_description = strdup( "Teletext" );
2265 pid->es->fmt.i_extra = p_dr->i_length;
2266 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2267 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2270 #ifdef _DVBPSI_DR_59_H_
2271 else if( p_dr->i_tag == 0x59 )
2274 dvbpsi_subtitling_dr_t *sub;
2277 pid->es->fmt.i_cat = SPU_ES;
2278 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
2279 pid->es->fmt.i_group = p_pmt->i_program_number;
2281 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
2282 if( !sub ) continue;
2284 /* Each subtitle ES contains n languages,
2285 * We are going to create n ES for the n tracks */
2286 if( sub->i_subtitles_number > 0 )
2288 pid->es->fmt.psz_language = malloc( 4 );
2289 memcpy( pid->es->fmt.psz_language,
2290 sub->p_subtitle[0].i_iso6392_language_code, 3);
2291 pid->es->fmt.psz_language[3] = 0;
2293 pid->es->fmt.subs.dvb.i_id =
2294 sub->p_subtitle[0].i_composition_page_id;
2296 pid->es->fmt.subs.dvb.i_id |=
2297 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
2299 else pid->es->fmt.i_cat = UNKNOWN_ES;
2301 for( n = 1; n < sub->i_subtitles_number; n++ )
2303 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
2304 p_es->fmt = pid->es->fmt;
2307 p_es->i_pes_size = 0;
2308 p_es->i_pes_gathered = 0;
2309 p_es->pp_last = &p_es->p_pes;
2310 p_es->p_mpeg4desc = NULL;
2312 p_es->fmt.psz_language = malloc( 4 );
2313 memcpy( p_es->fmt.psz_language,
2314 sub->p_subtitle[n].i_iso6392_language_code, 3);
2315 p_es->fmt.psz_language[3] = 0;
2317 p_es->fmt.subs.dvb.i_id =
2318 sub->p_subtitle[n].i_composition_page_id;
2320 p_es->fmt.subs.dvb.i_id |=
2321 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
2323 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
2326 #endif /* _DVBPSI_DR_59_H_ */
2329 else if( p_es->i_type == 0xa0 )
2331 /* MSCODEC sent by vlc */
2332 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2334 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
2336 if( p_dr && p_dr->i_length >= 8 )
2338 pid->es->fmt.i_cat = VIDEO_ES;
2339 pid->es->fmt.i_codec =
2340 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
2341 p_dr->p_data[2], p_dr->p_data[3] );
2342 pid->es->fmt.video.i_width =
2343 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
2344 pid->es->fmt.video.i_height =
2345 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
2346 pid->es->fmt.i_extra =
2347 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
2349 if( pid->es->fmt.i_extra > 0 )
2351 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2352 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
2353 pid->es->fmt.i_extra );
2358 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
2361 /* For such stream we will gather them ourself and don't launch a
2363 * Yes it's ugly but it's the only way to have DIV3 working */
2364 pid->es->fmt.b_packetized = VLC_TRUE;
2367 if( pid->es->fmt.i_cat == AUDIO_ES ||
2368 ( pid->es->fmt.i_cat == SPU_ES &&
2369 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
2371 /* get language descriptor */
2372 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2373 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
2377 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
2381 pid->es->fmt.psz_language = malloc( 4 );
2382 memcpy( pid->es->fmt.psz_language,
2383 p_decoded->i_iso_639_code, 3 );
2384 pid->es->fmt.psz_language[3] = 0;
2389 pid->es->fmt.i_group = p_pmt->i_program_number;
2390 if( pid->es->fmt.i_cat == UNKNOWN_ES )
2392 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
2393 p_es->i_pid, p_es->i_type );
2395 else if( !p_sys->b_udp_out )
2397 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
2398 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
2400 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
2402 /* Check if we can avoid restarting the ES */
2404 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
2405 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
2406 pid->es->fmt.i_extra == 0 &&
2407 pid->i_extra_es == old_pid->i_extra_es &&
2408 ( ( !pid->es->fmt.psz_language &&
2409 !old_pid->es->fmt.psz_language ) ||
2410 ( pid->es->fmt.psz_language &&
2411 old_pid->es->fmt.psz_language &&
2412 !strcmp( pid->es->fmt.psz_language,
2413 old_pid->es->fmt.psz_language ) ) ) )
2415 pid->es->id = old_pid->es->id;
2416 old_pid->es->id = NULL;
2417 for( i = 0; i < pid->i_extra_es; i++ )
2419 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
2420 old_pid->extra_es[i]->id = NULL;
2427 PIDClean( p_demux->out, old_pid );
2428 TAB_REMOVE( i_clean, pp_clean, old_pid );
2432 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
2433 for( i = 0; i < pid->i_extra_es; i++ )
2435 pid->extra_es[i]->id =
2436 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
2441 /* Add ES to the list */
2444 PIDClean( p_demux->out, old_pid );
2445 TAB_REMOVE( i_clean, pp_clean, old_pid );
2447 p_sys->pid[p_es->i_pid] = *pid;
2449 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2450 p_dr = p_dr->p_next )
2452 if( p_dr->i_tag == 0x9 )
2454 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2456 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
2461 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2463 /* Set demux filter */
2464 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2465 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
2470 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2472 /* Set CAM descrambling */
2473 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2474 ACCESS_SET_PRIVATE_ID_CA, p_pmt );
2478 dvbpsi_DeletePMT( p_pmt );
2481 for ( i = 0; i < i_clean; i++ )
2483 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2485 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2486 ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
2490 PIDClean( p_demux->out, pp_clean[i] );
2492 if( i_clean ) free( pp_clean );
2495 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
2497 demux_sys_t *p_sys = p_demux->p_sys;
2498 dvbpsi_pat_program_t *p_program;
2499 ts_pid_t *pat = &p_sys->pid[0];
2502 msg_Dbg( p_demux, "PATCallBack called" );
2504 if( pat->psi->i_pat_version != -1 &&
2505 ( !p_pat->b_current_next ||
2506 p_pat->i_version == pat->psi->i_pat_version ) )
2508 dvbpsi_DeletePAT( p_pat );
2512 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
2513 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
2516 if( p_sys->i_pmt > 0 )
2519 ts_pid_t **pmt_rm = NULL;
2521 /* Search pmt to be deleted */
2522 for( i = 0; i < p_sys->i_pmt; i++ )
2524 ts_pid_t *pmt = p_sys->pmt[i];
2525 vlc_bool_t b_keep = VLC_FALSE;
2527 for( p_program = p_pat->p_first_program; p_program != NULL;
2528 p_program = p_program->p_next )
2530 if( p_program->i_pid == pmt->i_pid )
2533 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2535 if( p_program->i_number ==
2536 pmt->psi->prg[i_prg]->i_number )
2548 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
2552 /* Delete all ES attached to thoses PMT */
2553 for( i = 2; i < 8192; i++ )
2555 ts_pid_t *pid = &p_sys->pid[i];
2557 if( !pid->b_valid || pid->psi ) continue;
2559 for( j = 0; j < i_pmt_rm; j++ )
2562 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
2564 /* We only remove es that aren't defined by extra pmt */
2565 if( pid->p_owner->prg[i_prg]->i_pid_pmt !=
2566 pmt_rm[j]->i_pid ) continue;
2568 if( p_sys->b_dvb_control && pid->es->id )
2570 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2571 ACCESS_SET_PRIVATE_ID_STATE, i,
2573 p_sys->b_dvb_control = VLC_FALSE;
2576 PIDClean( p_demux->out, pid );
2580 if( !pid->b_valid ) break;
2584 /* Delete PMT pid */
2585 for( i = 0; i < i_pmt_rm; i++ )
2587 if( p_sys->b_dvb_control )
2589 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2590 ACCESS_SET_PRIVATE_ID_STATE,
2591 pmt_rm[i]->i_pid, VLC_FALSE ) )
2592 p_sys->b_dvb_control = VLC_FALSE;
2595 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
2596 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
2599 if( pmt_rm ) free( pmt_rm );
2602 /* now create programs */
2603 for( p_program = p_pat->p_first_program; p_program != NULL;
2604 p_program = p_program->p_next )
2606 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
2608 if( p_program->i_number != 0 )
2610 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
2611 vlc_bool_t b_add = VLC_TRUE;
2616 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2618 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
2627 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
2632 PIDInit( pmt, VLC_TRUE, pat->psi );
2633 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
2634 dvbpsi_AttachPMT( p_program->i_number,
2635 (dvbpsi_pmt_callback)PMTCallBack,
2637 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
2638 p_program->i_number;
2639 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
2642 /* Now select PID at access level */
2643 if( p_sys->b_dvb_control )
2645 if( DVBProgramIsSelected( p_demux, p_program->i_number ) )
2647 if( p_sys->i_dvb_program == 0 )
2648 p_sys->i_dvb_program = p_program->i_number;
2650 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
2651 p_sys->b_dvb_control = VLC_FALSE;
2655 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_FALSE ) )
2656 p_sys->b_dvb_control = VLC_FALSE;
2662 pat->psi->i_pat_version = p_pat->i_version;
2664 dvbpsi_DeletePAT( p_pat );