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")
87 set_description( _("ISO 13818-1 MPEG Transport Stream input - new" ) );
89 add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
90 add_bool( "ts-es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
91 add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
92 add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
93 MTUOUT_LONGTEXT, VLC_TRUE );
94 add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
95 add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );
97 set_capability( "demux2", 10 );
98 set_callbacks( Open, Close );
102 /*****************************************************************************
104 *****************************************************************************/
108 uint8_t i_objectTypeIndication;
109 uint8_t i_streamType;
110 vlc_bool_t b_upStream;
111 uint32_t i_bufferSizeDB;
112 uint32_t i_maxBitrate;
113 uint32_t i_avgBitrate;
115 int i_decoder_specific_info_len;
116 uint8_t *p_decoder_specific_info;
118 } decoder_config_descriptor_t;
122 vlc_bool_t b_useAccessUnitStartFlag;
123 vlc_bool_t b_useAccessUnitEndFlag;
124 vlc_bool_t b_useRandomAccessPointFlag;
125 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
126 vlc_bool_t b_usePaddingFlag;
127 vlc_bool_t b_useTimeStampsFlags;
128 vlc_bool_t b_useIdleFlag;
129 vlc_bool_t b_durationFlag;
130 uint32_t i_timeStampResolution;
131 uint32_t i_OCRResolution;
132 uint8_t i_timeStampLength;
135 uint8_t i_instantBitrateLength;
136 uint8_t i_degradationPriorityLength;
137 uint8_t i_AU_seqNumLength;
138 uint8_t i_packetSeqNumLength;
140 uint32_t i_timeScale;
141 uint16_t i_accessUnitDuration;
142 uint16_t i_compositionUnitDuration;
144 uint64_t i_startDecodingTimeStamp;
145 uint64_t i_startCompositionTimeStamp;
147 } sl_config_descriptor_t;
154 vlc_bool_t b_streamDependenceFlag;
155 vlc_bool_t b_OCRStreamFlag;
156 uint8_t i_streamPriority;
160 uint16_t i_dependOn_es_id;
161 uint16_t i_OCR_es_id;
163 decoder_config_descriptor_t dec_descr;
164 sl_config_descriptor_t sl_descr;
166 } es_mpeg4_descriptor_t;
176 uint8_t i_ODProfileLevelIndication;
177 uint8_t i_sceneProfileLevelIndication;
178 uint8_t i_audioProfileLevelIndication;
179 uint8_t i_visualProfileLevelIndication;
180 uint8_t i_graphicsProfileLevelIndication;
182 es_mpeg4_descriptor_t es_descr[255];
190 dvbpsi_handle handle;
196 /* IOD stuff (mpeg4) */
197 iod_descriptor_t *iod;
199 /* Conditional Access descriptor */
201 uint8_t *cad[MAX_CAD];
202 uint8_t i_cad_length[MAX_CAD];
208 /* for special PAT case */
209 dvbpsi_handle handle;
227 es_mpeg4_descriptor_t *p_mpeg4desc;
238 int i_cc; /* countinuity counter */
240 /* PSI owner (ie PMT -> PAT, ES -> PMT */
248 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
256 /* TS packet size (188, 192, 204) */
259 /* how many TS packet we read at once */
270 vlc_bool_t b_es_id_pid;
274 vlc_bool_t b_udp_out;
275 int fd; /* udp socket */
278 vlc_bool_t b_dvb_control;
282 static int Demux ( demux_t *p_demux );
283 static int Control( demux_t *p_demux, int i_query, va_list args );
286 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
287 static void PIDClean( es_out_t *out, ts_pid_t *pid );
288 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
290 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
291 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
293 static inline int PIDGet( block_t *p )
295 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
298 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
300 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
302 static iod_descriptor_t *IODNew( int , uint8_t * );
303 static void IODFree( iod_descriptor_t * );
305 #define TS_PACKET_SIZE_188 188
306 #define TS_PACKET_SIZE_192 192
307 #define TS_PACKET_SIZE_204 204
308 #define TS_PACKET_SIZE_MAX 204
310 /*****************************************************************************
312 *****************************************************************************/
313 static int Open( vlc_object_t *p_this )
315 demux_t *p_demux = (demux_t*)p_this;
319 int i_sync, i_peek, i;
326 if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
329 msg_Err( p_demux, "cannot peek" );
333 /* Search first sync byte */
334 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
336 if( p_peek[i_sync] == 0x47 ) break;
338 if( i_sync >= TS_PACKET_SIZE_MAX )
340 if( strcmp( p_demux->psz_demux, "ts" ) )
342 msg_Warn( p_demux, "TS module discarded" );
345 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
348 /* Check next 3 sync bytes */
349 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
350 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
352 msg_Err( p_demux, "cannot peek" );
355 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
356 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
357 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
359 i_packet_size = TS_PACKET_SIZE_188;
361 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
362 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
363 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
365 i_packet_size = TS_PACKET_SIZE_192;
367 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
368 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
369 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
371 i_packet_size = TS_PACKET_SIZE_204;
373 else if( !strcmp( p_demux->psz_demux, "ts" ) )
375 i_packet_size = TS_PACKET_SIZE_188;
379 msg_Warn( p_demux, "TS module discarded (lost sync)" );
383 /* Fill p_demux field */
384 p_demux->pf_demux = Demux;
385 p_demux->pf_control = Control;
386 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
387 memset( p_sys, 0, sizeof( demux_sys_t ) );
389 /* Init p_sys field */
390 p_sys->b_dvb_control = VLC_TRUE;
391 p_sys->i_dvb_program = 0;
392 for( i = 0; i < 8192; i++ )
394 ts_pid_t *pid = &p_sys->pid[i];
397 pid->b_seen = VLC_FALSE;
398 pid->b_valid = VLC_FALSE;
400 p_sys->i_packet_size = i_packet_size;
401 p_sys->b_udp_out = VLC_FALSE;
402 p_sys->i_ts_read = 50;
405 /* Init PAT handler */
406 pat = &p_sys->pid[0];
407 PIDInit( pat, VLC_TRUE, NULL );
408 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
416 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
417 var_Get( p_demux, "ts-es-id-pid", &val );
418 p_sys->b_es_id_pid = val.b_bool,
420 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
421 var_Get( p_demux, "ts-out", &val );
422 if( val.psz_string && *val.psz_string )
425 char *psz = strchr( val.psz_string, ':' );
428 p_sys->b_udp_out = VLC_TRUE;
433 i_port = atoi( psz );
435 if( i_port <= 0 ) i_port = 1234;
436 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
438 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
441 msg_Err( p_demux, "failed to open udp socket, send disabled" );
442 p_sys->b_udp_out = VLC_FALSE;
446 var_Create( p_demux, "ts-out-mtu",
447 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
448 var_Get( p_demux, "ts-out-mtu", &mtu );
449 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
450 if( p_sys->i_ts_read <= 0 )
452 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
454 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
459 free( val.psz_string );
463 /* We handle description of an extra PMT */
464 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
465 var_Get( p_demux, "ts-extra-pmt", &val );
466 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
468 char *psz = val.psz_string;
469 int i_pid = strtol( psz, &psz, 0 );
471 if( i_pid >= 2 && i_pid < 8192 )
473 ts_pid_t *pmt = &p_sys->pid[i_pid];
475 msg_Dbg( p_demux, "extra pmt specified (pid=0x%x)", i_pid );
476 PIDInit( pmt, VLC_TRUE, NULL );
477 /* FIXME we should also ask for a number */
478 pmt->psi->prg[0]->handle =
479 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
481 pmt->psi->prg[0]->i_number = 0; /* special one */
483 psz = strchr( psz, '=' ) + 1; /* can't failed */
486 char *psz_next = strchr( psz, ',' );
487 int i_pid, i_stream_type;
494 i_pid = strtol( psz, &psz, 0 );
497 i_stream_type = strtol( psz + 1, &psz, 0 );
498 if( i_pid >= 2 && i_pid < 8192 &&
499 !p_sys->pid[i_pid].b_valid )
501 ts_pid_t *pid = &p_sys->pid[i_pid];
503 PIDInit( pid, VLC_FALSE, pmt->psi);
504 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
506 pmt->psi->prg[0]->i_pid_pcr = i_pid;
508 PIDFillFormat( pid, i_stream_type);
509 if( pid->es->fmt.i_cat != UNKNOWN_ES )
511 if( p_sys->b_es_id_pid )
513 pid->es->fmt.i_id = i_pid;
515 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x "
516 "fcc=%4.4s", i_pid, i_stream_type,
517 (char*)&pid->es->fmt.i_codec );
518 pid->es->id = es_out_Add( p_demux->out,
529 free( val.psz_string );
532 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
533 var_Get( p_demux, "ts-csa-ck", &val );
534 if( val.psz_string && *val.psz_string )
536 char *psz = val.psz_string;
537 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
541 if( strlen( psz ) != 16 )
543 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
547 uint64_t i_ck = strtoull( psz, NULL, 16 );
550 for( i = 0; i < 8; i++ )
552 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
555 msg_Dbg( p_demux, "using CSA scrambling with "
556 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
557 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
559 p_sys->csa = csa_New();
560 csa_SetCW( p_sys->csa, ck, ck );
565 free( val.psz_string );
568 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
569 var_Get( p_demux, "ts-silent", &val );
570 p_sys->b_silent = val.b_bool;
575 /*****************************************************************************
577 *****************************************************************************/
578 static void Close( vlc_object_t *p_this )
580 demux_t *p_demux = (demux_t*)p_this;
581 demux_sys_t *p_sys = p_demux->p_sys;
585 msg_Dbg( p_demux, "pid list:" );
586 for( i = 0; i < 8192; i++ )
588 ts_pid_t *pid = &p_sys->pid[i];
590 if( pid->b_valid && pid->psi )
595 dvbpsi_DetachPAT( pid->psi->handle );
602 PIDClean( p_demux->out, pid );
606 else if( pid->b_valid && pid->es )
608 PIDClean( p_demux->out, pid );
613 msg_Dbg( p_demux, " - pid[0x%x] seen", pid->i_pid );
616 if( p_sys->b_dvb_control && pid->i_pid > 0 )
619 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
624 if( p_sys->b_udp_out )
626 net_Close( p_sys->fd );
627 free( p_sys->buffer );
631 csa_Delete( p_sys->csa );
634 if( p_sys->i_pmt ) free( p_sys->pmt );
638 /*****************************************************************************
640 *****************************************************************************/
641 static int Demux( demux_t *p_demux )
643 demux_sys_t *p_sys = p_demux->p_sys;
646 /* We read at most 100 TS packet or until a frame is completed */
647 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
649 vlc_bool_t b_frame = VLC_FALSE;
653 /* Get a new TS packet */
654 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
656 msg_Dbg( p_demux, "eof ?" );
660 /* Check sync byte and re-sync if needed */
661 if( p_pkt->p_buffer[0] != 0x47 )
663 msg_Warn( p_demux, "lost synchro" );
664 block_Release( p_pkt );
666 while( !p_demux->b_die )
669 int i_peek, i_skip = 0;
671 i_peek = stream_Peek( p_demux->s, &p_peek,
672 p_sys->i_packet_size * 10 );
673 if( i_peek < p_sys->i_packet_size + 1 )
675 msg_Dbg( p_demux, "eof ?" );
679 while( i_skip < i_peek - p_sys->i_packet_size )
681 if( p_peek[i_skip] == 0x47 &&
682 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
689 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
690 stream_Read( p_demux->s, NULL, i_skip );
692 if( i_skip < i_peek - p_sys->i_packet_size )
698 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
700 msg_Dbg( p_demux, "eof ?" );
705 if( p_sys->b_udp_out )
707 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
708 p_pkt->p_buffer, p_sys->i_packet_size );
711 /* Parse the TS packet */
712 p_pid = &p_sys->pid[PIDGet( p_pkt )];
718 if( p_pid->i_pid == 0 )
720 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
725 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
727 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
731 block_Release( p_pkt );
733 else if( !p_sys->b_udp_out )
735 b_frame = GatherPES( p_demux, p_pid, p_pkt );
739 PCRHandle( p_demux, p_pid, p_pkt );
740 block_Release( p_pkt );
747 msg_Dbg( p_demux, "pid[0x%x] unknown", p_pid->i_pid );
749 /* We have to handle PCR if present */
750 PCRHandle( p_demux, p_pid, p_pkt );
751 block_Release( p_pkt );
753 p_pid->b_seen = VLC_TRUE;
761 if( p_sys->b_udp_out )
763 /* Send the complete block */
764 net_Write( p_demux, p_sys->fd, p_sys->buffer,
765 p_sys->i_ts_read * p_sys->i_packet_size );
771 /*****************************************************************************
773 *****************************************************************************/
774 static int Control( demux_t *p_demux, int i_query, va_list args )
776 demux_sys_t *p_sys = p_demux->p_sys;
783 case DEMUX_GET_POSITION:
784 pf = (double*) va_arg( args, double* );
785 i64 = stream_Size( p_demux->s );
788 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
795 case DEMUX_SET_POSITION:
796 f = (double) va_arg( args, double );
797 i64 = stream_Size( p_demux->s );
799 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
800 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
808 pi64 = (int64_t*)va_arg( args, int64_t * );
809 if( p_sys->i_time < 0 )
814 *pi64 = p_sys->i_time;
817 case DEMUX_GET_LENGTH:
818 pi64 = (int64_t*)va_arg( args, int64_t * );
819 if( p_sys->i_mux_rate > 0 )
821 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
828 case DEMUX_SET_GROUP:
830 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
831 ts_prg_psi_t *p_prg = NULL;
833 i_int = (int)va_arg( args, int );
834 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d", i_int );
836 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
841 /* Search pmt to be unselected */
842 for( i = 0; i < p_sys->i_pmt; i++ )
844 ts_pid_t *pmt = p_sys->pmt[i];
847 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
849 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
851 i_pmt_pid = p_sys->pmt[i]->i_pid;
855 if( i_pmt_pid > 0 ) break;
860 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
861 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
864 for( i = 2; i < 8192; i++ )
866 ts_pid_t *pid = &p_sys->pid[i];
869 if( !pid->b_valid || pid->psi ) continue;
871 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
873 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
875 /* We only remove es that aren't defined by extra pmt */
876 stream_Control( p_demux->s,
877 STREAM_CONTROL_ACCESS,
878 ACCESS_SET_PRIVATE_ID_STATE,
886 /* select new program */
887 p_sys->i_dvb_program = i_int;
889 for( i = 0; i < p_sys->i_pmt; i++ )
891 ts_pid_t *pmt = p_sys->pmt[i];
894 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
896 if( pmt->psi->prg[i_prg]->i_number == i_int )
898 i_pmt_pid = p_sys->pmt[i]->i_pid;
899 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
903 if( i_pmt_pid > 0 ) break;
907 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
908 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
910 for( i = 2; i < 8192; i++ )
912 ts_pid_t *pid = &p_sys->pid[i];
915 if( !pid->b_valid || pid->psi ) continue;
917 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
919 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
921 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
923 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
925 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
927 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
930 stream_Control( p_demux->s,
931 STREAM_CONTROL_ACCESS,
932 ACCESS_SET_PRIVATE_ID_STATE,
939 /* Set CAM descrambling */
940 for ( i = 0; i < p_prg->i_nb_cad; i++ )
942 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
943 ACCESS_SET_PRIVATE_ID_CA, p_prg->i_number,
944 i_vpid, i_apid1, i_apid2, i_apid3,
945 p_prg->i_cad_length[i],
948 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
949 ACCESS_SET_PRIVATE_ID_CA, p_prg->i_number,
950 0, 0, 0, 0, 0, NULL );
951 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
952 ACCESS_SET_PRIVATE_ID_CA, 0,
953 0, 0, 0, 0, 0, NULL );
966 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
968 vlc_bool_t b_old_valid = pid->b_valid;
970 pid->b_valid = VLC_TRUE;
972 pid->p_owner = p_owner;
973 pid->i_owner_number = 0;
975 pid->extra_es = NULL;
984 pid->psi = malloc( sizeof( ts_psi_t ) );
986 pid->psi->prg = NULL;
987 pid->psi->handle= NULL;
989 pid->psi->i_pat_version = -1;
992 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1002 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1008 pid->es = malloc( sizeof( ts_es_t ) );
1010 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1012 pid->es->p_pes = NULL;
1013 pid->es->i_pes_size= 0;
1014 pid->es->i_pes_gathered= 0;
1015 pid->es->pp_last = &pid->es->p_pes;
1016 pid->es->p_mpeg4desc = NULL;
1017 pid->es->b_gather = VLC_FALSE;
1021 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1027 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1028 for( i = 0; i < pid->psi->i_prg; i++ )
1031 if( pid->psi->prg[i]->iod )
1032 IODFree( pid->psi->prg[i]->iod );
1033 for ( j = 0; j < pid->psi->prg[i]->i_nb_cad; j++ )
1034 free( pid->psi->prg[i]->cad[j] );
1035 if( pid->psi->prg[i]->handle )
1036 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1037 free( pid->psi->prg[i] );
1039 if( pid->psi->prg ) free( pid->psi->prg );
1047 es_out_Del( out, pid->es->id );
1049 if( pid->es->p_pes )
1050 block_ChainRelease( pid->es->p_pes );
1052 es_format_Clean( &pid->es->fmt );
1056 for( i = 0; i < pid->i_extra_es; i++ )
1058 if( pid->extra_es[i]->id )
1059 es_out_Del( out, pid->extra_es[i]->id );
1061 if( pid->extra_es[i]->p_pes )
1062 block_ChainRelease( pid->extra_es[i]->p_pes );
1064 es_format_Clean( &pid->extra_es[i]->fmt );
1066 free( pid->extra_es[i] );
1068 if( pid->i_extra_es ) free( pid->extra_es );
1071 pid->b_valid = VLC_FALSE;
1074 /****************************************************************************
1076 ****************************************************************************/
1077 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1079 block_t *p_pes = pid->es->p_pes;
1085 mtime_t i_length = 0;
1088 /* remove the pes from pid */
1089 pid->es->p_pes = NULL;
1090 pid->es->i_pes_size= 0;
1091 pid->es->i_pes_gathered= 0;
1092 pid->es->pp_last = &pid->es->p_pes;
1094 /* FIXME find real max size */
1095 i_max = block_ChainExtract( p_pes, header, 30 );
1098 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1100 if( !p_demux->p_sys->b_silent )
1101 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: 0x%x)",
1102 header[0], header[1],header[2],header[3], pid->i_pid );
1103 block_ChainRelease( p_pes );
1107 /* TODO check size */
1110 case 0xBC: /* Program stream map */
1111 case 0xBE: /* Padding */
1112 case 0xBF: /* Private stream 2 */
1113 case 0xB0: /* ECM */
1114 case 0xB1: /* EMM */
1115 case 0xFF: /* Program stream directory */
1116 case 0xF2: /* DSMCC stream */
1117 case 0xF8: /* ITU-T H.222.1 type E stream */
1121 if( ( header[6]&0xC0 ) == 0x80 )
1124 i_skip = header[8] + 9;
1126 if( header[7]&0x80 ) /* has pts */
1128 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1129 (mtime_t)(header[10] << 22)|
1130 ((mtime_t)(header[11]&0xfe) << 14)|
1131 (mtime_t)(header[12] << 7)|
1132 (mtime_t)(header[13] >> 1);
1134 if( header[7]&0x40 ) /* has dts */
1136 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1137 (mtime_t)(header[15] << 22)|
1138 ((mtime_t)(header[16]&0xfe) << 14)|
1139 (mtime_t)(header[17] << 7)|
1140 (mtime_t)(header[18] >> 1);
1147 while( i_skip < 23 && header[i_skip] == 0xff )
1153 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1154 block_ChainRelease( p_pes );
1157 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1162 if( header[i_skip]&0x20 )
1164 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1165 (mtime_t)(header[i_skip+1] << 22)|
1166 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1167 (mtime_t)(header[i_skip+3] << 7)|
1168 (mtime_t)(header[i_skip+4] >> 1);
1170 if( header[i_skip]&0x10 ) /* has dts */
1172 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1173 (mtime_t)(header[i_skip+6] << 22)|
1174 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1175 (mtime_t)(header[i_skip+8] << 7)|
1176 (mtime_t)(header[i_skip+9] >> 1);
1192 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1193 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1197 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1198 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1199 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1203 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1204 pid->es->p_mpeg4desc )
1206 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1208 if( dcd->i_decoder_specific_info_len > 2 &&
1209 dcd->p_decoder_specific_info[0] == 0x10 &&
1210 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1212 /* display length */
1213 if( p_pes->i_buffer + 2 <= i_skip )
1215 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1220 if( p_pes->i_buffer + 2 <= i_skip )
1222 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1229 while( p_pes && i_skip > 0 )
1231 if( p_pes->i_buffer <= i_skip )
1233 block_t *p_next = p_pes->p_next;
1235 i_skip -= p_pes->i_buffer;
1236 block_Release( p_pes );
1241 p_pes->i_buffer -= i_skip;
1242 p_pes->p_buffer += i_skip;
1254 p_pes->i_dts = i_dts * 100 / 9;
1258 p_pes->i_pts = i_pts * 100 / 9;
1260 p_pes->i_length = i_length * 100 / 9;
1262 p_block = block_ChainGather( p_pes );
1263 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1265 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1267 p_block->i_buffer = i_pes_size;
1270 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1271 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1274 for( i = 0; i < pid->i_extra_es; i++ )
1276 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1277 block_Duplicate( p_block ) );
1280 es_out_Send( p_demux->out, pid->es->id, p_block );
1284 msg_Warn( p_demux, "empty pes" );
1288 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1290 demux_sys_t *p_sys = p_demux->p_sys;
1291 const uint8_t *p = p_bk->p_buffer;
1293 if( ( p[3]&0x20 ) && /* adaptation */
1298 mtime_t i_pcr; /* 33 bits */
1300 i_pcr = ( (mtime_t)p[6] << 25 ) |
1301 ( (mtime_t)p[7] << 17 ) |
1302 ( (mtime_t)p[8] << 9 ) |
1303 ( (mtime_t)p[9] << 1 ) |
1304 ( (mtime_t)p[10] >> 7 );
1306 /* Search program and set the PCR */
1307 for( i = 0; i < p_sys->i_pmt; i++ )
1310 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1312 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1314 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1315 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1316 (int64_t)(i_pcr * 100 / 9) );
1323 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1325 const uint8_t *p = p_bk->p_buffer;
1326 const vlc_bool_t b_unit_start = p[1]&0x40;
1327 const vlc_bool_t b_adaptation = p[3]&0x20;
1328 const vlc_bool_t b_payload = p[3]&0x10;
1329 const int i_cc = p[3]&0x0f; /* continuity counter */
1331 /* transport_scrambling_control is ignored */
1334 vlc_bool_t i_ret = VLC_FALSE;
1338 msg_Dbg( p_demux, "pid=0x%x unit_start=%d adaptation=%d payload=%d "
1339 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1343 /* For now, ignore additional error correction
1344 * TODO: handle Reed-Solomon 204,188 error correction */
1345 p_bk->i_buffer = TS_PACKET_SIZE_188;
1349 msg_Dbg( p_demux, "transport_error_indicator set (pid=0x%x)",
1353 if( p_demux->p_sys->csa )
1355 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer );
1360 /* We don't have any adaptation_field, so payload starts
1361 * immediately after the 4 byte TS header */
1366 /* p[4] is adaptation_field_length minus one */
1372 msg_Warn( p_demux, "discontinuity_indicator (pid=0x%x) "
1373 "ignored", pid->i_pid );
1378 /* Test continuity counter */
1379 /* continuous when (one of this):
1381 * diff == 0 and payload == 0
1382 * diff == 0 and duplicate packet (playload != 0) <- should we
1383 * test the content ?
1386 i_diff = ( i_cc - pid->i_cc )&0x0f;
1387 if( b_payload && i_diff == 1 )
1393 if( pid->i_cc == 0xff )
1395 msg_Warn( p_demux, "first packet for pid=0x%x cc=0x%x",
1399 else if( i_diff != 0 )
1401 /* FIXME what to do when discontinuity_indicator is set ? */
1402 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x",
1403 i_cc, ( pid->i_cc + 1 )&0x0f );
1407 if( pid->es->p_pes )
1409 pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY;
1414 PCRHandle( p_demux, pid, p_bk );
1416 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1418 block_Release( p_bk );
1422 /* We have to gather it */
1423 p_bk->p_buffer += i_skip;
1424 p_bk->i_buffer -= i_skip;
1428 if( pid->es->p_pes )
1430 ParsePES( p_demux, pid );
1434 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1435 if( p_bk->i_buffer > 6 )
1437 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1438 if( pid->es->i_pes_size > 0 )
1440 pid->es->i_pes_size += 6;
1443 pid->es->i_pes_gathered += p_bk->i_buffer;
1444 if( pid->es->i_pes_size > 0 &&
1445 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1447 ParsePES( p_demux, pid );
1453 if( pid->es->p_pes == NULL )
1455 /* msg_Dbg( p_demux, "broken packet" ); */
1456 block_Release( p_bk );
1460 block_ChainLastAppend( &pid->es->pp_last, p_bk );
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 );
1474 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1476 es_format_t *fmt = &pid->es->fmt;
1478 switch( i_stream_type )
1480 case 0x01: /* MPEG-1 video */
1481 case 0x02: /* MPEG-2 video */
1482 case 0x80: /* MPEG-2 MOTO video */
1483 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1485 case 0x03: /* MPEG-1 audio */
1486 case 0x04: /* MPEG-2 audio */
1487 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1489 case 0x11: /* MPEG4 (audio) */
1490 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1491 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1493 case 0x10: /* MPEG4 (video) */
1494 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1495 pid->es->b_gather = VLC_TRUE;
1497 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1498 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1501 case 0x81: /* A52 (audio) */
1502 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1504 case 0x82: /* DVD_SPU (sub) */
1505 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1507 case 0x83: /* LPCM (audio) */
1508 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1510 case 0x84: /* SDDS (audio) */
1511 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1513 case 0x85: /* DTS (audio) */
1514 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1517 case 0x91: /* A52 vls (audio) */
1518 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1520 case 0x92: /* DVD_SPU vls (sub) */
1521 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1523 case 0x93: /* LPCM vls (audio) */
1524 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1526 case 0x94: /* SDDS (audio) */
1527 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1530 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1531 es_format_Init( fmt, UNKNOWN_ES, 0 );
1532 pid->es->b_gather = VLC_TRUE;
1535 case 0x06: /* PES_PRIVATE (fixed later) */
1536 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1538 es_format_Init( fmt, UNKNOWN_ES, 0 );
1542 /* PES packets usually contain truncated frames */
1543 fmt->b_packetized = VLC_FALSE;
1545 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1548 /*****************************************************************************
1549 * MP4 specific functions (IOD parser)
1550 *****************************************************************************/
1551 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1554 unsigned int i_len = 0;
1560 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1562 } while( i_b&0x80 );
1566 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1570 const int i_b = **pp_data;
1577 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1579 const int i1 = IODGetByte( pi_data, pp_data );
1580 const int i2 = IODGetByte( pi_data, pp_data );
1581 return( ( i1 << 8 ) | i2 );
1583 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1585 const int i1 = IODGetByte( pi_data, pp_data );
1586 const int i2 = IODGetByte( pi_data, pp_data );
1587 const int i3 = IODGetByte( pi_data, pp_data );
1589 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1592 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1594 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1595 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1596 return( ( i1 << 16 ) | i2 );
1599 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1604 i_url_len = IODGetByte( pi_data, pp_data );
1605 url = malloc( i_url_len + 1 );
1606 for( i = 0; i < i_url_len; i++ )
1608 url[i] = IODGetByte( pi_data, pp_data );
1610 url[i_url_len] = '\0';
1614 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1616 iod_descriptor_t *p_iod;
1623 p_iod = malloc( sizeof( iod_descriptor_t ) );
1624 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1626 fprintf( stderr, "\n************ IOD ************" );
1627 for( i = 0; i < 255; i++ )
1629 p_iod->es_descr[i].b_ok = 0;
1638 p_iod->i_iod_label = IODGetByte( &i_data, &p_data );
1639 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1640 fprintf( stderr, "\n* ===========" );
1641 fprintf( stderr, "\n* tag:0x%x", p_data[0] );
1643 if( IODGetByte( &i_data, &p_data ) != 0x02 )
1645 fprintf( stderr, "\n ERR: tag != 0x02" );
1649 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1650 fprintf( stderr, "\n* length:%d", i_iod_length );
1651 if( i_iod_length > i_data )
1653 i_iod_length = i_data;
1656 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1657 i_flags = IODGetByte( &i_data, &p_data );
1658 p_iod->i_od_id |= i_flags >> 6;
1659 b_url = ( i_flags >> 5 )&0x01;
1661 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1662 fprintf( stderr, "\n* url flag:%d", b_url );
1663 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1667 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1668 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1669 fprintf( stderr, "\n*****************************\n" );
1674 p_iod->psz_url = NULL;
1677 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1678 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1679 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1680 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1681 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1683 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1684 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1685 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1686 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1687 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1690 while( i_data > 0 && i_es_index < 255)
1692 int i_tag, i_length;
1694 uint8_t *p_data_sav;
1696 i_tag = IODGetByte( &i_data, &p_data );
1697 i_length = IODDescriptorLength( &i_data, &p_data );
1699 i_data_sav = i_data;
1700 p_data_sav = p_data;
1708 #define es_descr p_iod->es_descr[i_es_index]
1709 int i_decoderConfigDescr_length;
1710 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
1713 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
1714 i_flags = IODGetByte( &i_data, &p_data );
1715 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
1716 b_url = ( i_flags >> 6 )&0x01;
1717 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
1718 es_descr.i_streamPriority = i_flags & 0x1f;
1719 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
1720 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
1721 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
1723 if( es_descr.b_streamDependenceFlag )
1725 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
1726 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
1731 es_descr.psz_url = IODGetURL( &i_data, &p_data );
1732 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
1736 es_descr.psz_url = NULL;
1739 if( es_descr.b_OCRStreamFlag )
1741 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
1742 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
1745 if( IODGetByte( &i_data, &p_data ) != 0x04 )
1747 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
1751 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1753 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
1754 #define dec_descr es_descr.dec_descr
1755 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
1756 i_flags = IODGetByte( &i_data, &p_data );
1757 dec_descr.i_streamType = i_flags >> 2;
1758 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
1759 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
1760 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
1761 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
1762 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
1763 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
1764 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
1765 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
1766 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
1767 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
1768 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
1771 dec_descr.i_decoder_specific_info_len =
1772 IODDescriptorLength( &i_data, &p_data );
1773 if( dec_descr.i_decoder_specific_info_len > 0 )
1775 dec_descr.p_decoder_specific_info =
1776 malloc( dec_descr.i_decoder_specific_info_len );
1778 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
1780 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
1785 dec_descr.i_decoder_specific_info_len = 0;
1786 dec_descr.p_decoder_specific_info = NULL;
1790 #define sl_descr es_descr.sl_descr
1792 int i_SLConfigDescr_length;
1795 if( IODGetByte( &i_data, &p_data ) != 0x06 )
1797 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
1801 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
1803 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
1804 i_predefined = IODGetByte( &i_data, &p_data );
1805 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
1806 switch( i_predefined )
1810 sl_descr.b_useAccessUnitStartFlag = 0;
1811 sl_descr.b_useAccessUnitEndFlag = 0;
1812 sl_descr.b_useRandomAccessPointFlag = 0;
1813 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
1814 sl_descr.b_usePaddingFlag = 0;
1815 sl_descr.b_useTimeStampsFlags = 0;
1816 sl_descr.b_useIdleFlag = 0;
1817 sl_descr.b_durationFlag = 0; // FIXME FIXME
1818 sl_descr.i_timeStampResolution = 1000;
1819 sl_descr.i_OCRResolution = 0; // FIXME FIXME
1820 sl_descr.i_timeStampLength = 32;
1821 sl_descr.i_OCRLength = 0; // FIXME FIXME
1822 sl_descr.i_AU_Length = 0;
1823 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
1824 sl_descr.i_degradationPriorityLength= 0;
1825 sl_descr.i_AU_seqNumLength = 0;
1826 sl_descr.i_packetSeqNumLength = 0;
1827 if( sl_descr.b_durationFlag )
1829 sl_descr.i_timeScale = 0; // FIXME FIXME
1830 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
1831 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
1833 if( !sl_descr.b_useTimeStampsFlags )
1835 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
1836 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
1841 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
1850 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
1854 p_data = p_data_sav + i_length;
1855 i_data = i_data_sav - i_length;
1860 fprintf( stderr, "\n*****************************\n" );
1864 static void IODFree( iod_descriptor_t *p_iod )
1868 if( p_iod->psz_url )
1870 free( p_iod->psz_url );
1871 p_iod->psz_url = NULL;
1876 for( i = 0; i < 255; i++ )
1878 #define es_descr p_iod->es_descr[i]
1881 if( es_descr.psz_url )
1883 free( es_descr.psz_url );
1884 es_descr.psz_url = NULL;
1888 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
1890 free( es_descr.dec_descr.p_decoder_specific_info );
1891 es_descr.dec_descr.p_decoder_specific_info = NULL;
1892 es_descr.dec_descr.i_decoder_specific_info_len = 0;
1902 /****************************************************************************
1903 ****************************************************************************
1904 ** libdvbpsi callbacks
1905 ****************************************************************************
1906 ****************************************************************************/
1907 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
1909 demux_sys_t *p_sys = p_demux->p_sys;
1910 dvbpsi_descriptor_t *p_dr;
1911 dvbpsi_pmt_es_t *p_es;
1913 ts_pid_t *pmt = NULL;
1914 ts_prg_psi_t *prg = NULL;
1918 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
1920 msg_Dbg( p_demux, "PMTCallBack called" );
1922 /* First find this PMT declared in PAT */
1923 for( i = 0; i < p_sys->i_pmt; i++ )
1926 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1928 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
1929 p_pmt->i_program_number )
1931 pmt = p_sys->pmt[i];
1932 prg = p_sys->pmt[i]->psi->prg[i_prg];
1942 msg_Warn( p_demux, "unreferenced program (broken stream)" );
1943 dvbpsi_DeletePMT(p_pmt);
1947 if( prg->i_version != -1 &&
1948 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
1950 dvbpsi_DeletePMT( p_pmt );
1954 /* Clean this program (remove all es) */
1955 for( i = 0; i < 8192; i++ )
1957 ts_pid_t *pid = &p_sys->pid[i];
1959 if( pid->b_valid && pid->p_owner == pmt->psi &&
1960 pid->i_owner_number == prg->i_number && pid->psi == NULL )
1962 if( p_sys->b_dvb_control &&
1963 ( p_sys->i_dvb_program < 0 || p_sys->i_dvb_program == prg->i_number ) )
1965 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, i, VLC_FALSE );
1968 PIDClean( p_demux->out, pid );
1973 IODFree( prg->iod );
1976 for ( i = 0; i < prg->i_nb_cad; i++ )
1977 free( prg->cad[i] );
1980 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=0x%x",
1981 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
1982 prg->i_pid_pcr = p_pmt->i_pcr_pid;
1983 prg->i_version = p_pmt->i_version;
1985 /* Parse descriptor */
1986 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
1988 if( p_dr->i_tag == 0x1d )
1990 /* We have found an IOD descriptor */
1991 msg_Warn( p_demux, " * descriptor : IOD (0x1d)" );
1993 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
1995 else if( p_dr->i_tag == 0x9 )
1997 msg_Dbg( p_demux, " * descriptor : CA (0x9)" );
1999 prg->cad[prg->i_nb_cad] = malloc( p_dr->i_length + 2 );
2000 prg->cad[prg->i_nb_cad][0] = 0x9;
2001 prg->cad[prg->i_nb_cad][1] = p_dr->i_length;
2002 memcpy( prg->cad[prg->i_nb_cad] + 2, p_dr->p_data, p_dr->i_length );
2003 prg->i_cad_length[prg->i_nb_cad] = p_dr->i_length + 2;
2008 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2012 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2014 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
2018 msg_Warn( p_demux, "pmt error: pid=0x%x already defined",
2023 PIDInit( pid, VLC_FALSE, pmt->psi );
2024 PIDFillFormat( pid, p_es->i_type );
2025 pid->i_owner_number = prg->i_number;
2027 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
2028 i_vpid = p_es->i_pid;
2029 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
2030 i_apid1 = p_es->i_pid;
2031 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
2032 i_apid2 = p_es->i_pid;
2033 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
2034 i_apid3 = p_es->i_pid;
2036 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2037 p_es->i_type == 0x12 )
2039 /* MPEG-4 stream: search SL_DESCRIPTOR */
2040 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2042 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2044 if( p_dr && p_dr->i_length == 2 )
2047 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2049 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2051 pid->es->p_mpeg4desc = NULL;
2053 for( i = 0; i < 255; i++ )
2055 iod_descriptor_t *iod = prg->iod;
2057 if( iod->es_descr[i].b_ok &&
2058 iod->es_descr[i].i_es_id == i_es_id )
2060 pid->es->p_mpeg4desc = &iod->es_descr[i];
2066 if( pid->es->p_mpeg4desc != NULL )
2068 decoder_config_descriptor_t *dcd =
2069 &pid->es->p_mpeg4desc->dec_descr;
2071 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2073 pid->es->fmt.i_cat = VIDEO_ES;
2074 switch( dcd->i_objectTypeIndication )
2076 case 0x0B: /* mpeg4 sub */
2077 pid->es->fmt.i_cat = SPU_ES;
2078 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2081 case 0x20: /* mpeg4 */
2082 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2089 case 0x65: /* mpeg2 */
2090 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2092 case 0x6a: /* mpeg1 */
2093 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2095 case 0x6c: /* mpeg1 */
2096 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2099 pid->es->fmt.i_cat = UNKNOWN_ES;
2103 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2105 pid->es->fmt.i_cat = AUDIO_ES;
2106 switch( dcd->i_objectTypeIndication )
2108 case 0x40: /* mpeg4 */
2109 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2113 case 0x68: /* mpeg2 aac */
2114 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2116 case 0x69: /* mpeg2 */
2117 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2119 case 0x6b: /* mpeg1 */
2120 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2123 pid->es->fmt.i_cat = UNKNOWN_ES;
2129 pid->es->fmt.i_cat = UNKNOWN_ES;
2132 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2134 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2135 if( pid->es->fmt.i_extra > 0 )
2137 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2138 memcpy( pid->es->fmt.p_extra,
2139 dcd->p_decoder_specific_info,
2140 pid->es->fmt.i_extra );
2145 else if( p_es->i_type == 0x06 )
2147 dvbpsi_descriptor_t *p_dr;
2149 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2150 p_dr = p_dr->p_next )
2152 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x dr->i_tag=0x%x",
2153 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2155 if( p_dr->i_tag == 0x6a )
2157 pid->es->fmt.i_cat = AUDIO_ES;
2158 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2160 else if( p_dr->i_tag == 0x05 )
2163 /* DTS registration descriptor (ETSI TS 101 154 Annex F) */
2164 pid->es->fmt.i_cat = AUDIO_ES;
2165 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2167 else if( p_dr->i_tag == 0x73 )
2169 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2170 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2171 pid->es->fmt.i_cat = AUDIO_ES;
2172 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2174 else if( p_dr->i_tag == 0x56 )
2176 msg_Dbg( p_demux, " * Teletext descriptor" );
2177 pid->es->fmt.i_cat = SPU_ES;
2178 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
2180 #ifdef _DVBPSI_DR_59_H_
2181 else if( p_dr->i_tag == 0x59 )
2184 dvbpsi_subtitling_dr_t *sub;
2187 pid->es->fmt.i_cat = SPU_ES;
2188 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
2189 pid->es->fmt.i_group = p_pmt->i_program_number;
2191 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
2192 if( !sub ) continue;
2194 /* Each subtitle ES contains n languages,
2195 * We are going to create n ES for the n tracks */
2196 if( sub->i_subtitles_number > 0 )
2198 pid->es->fmt.psz_language = malloc( 4 );
2199 memcpy( pid->es->fmt.psz_language,
2200 sub->p_subtitle[0].i_iso6392_language_code, 3);
2201 pid->es->fmt.psz_language[3] = 0;
2203 pid->es->fmt.subs.dvb.i_id =
2204 sub->p_subtitle[0].i_composition_page_id;
2206 pid->es->fmt.subs.dvb.i_id |=
2207 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
2209 else pid->es->fmt.i_cat = UNKNOWN_ES;
2211 for( n = 1; n < sub->i_subtitles_number; n++ )
2213 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
2214 p_es->fmt = pid->es->fmt;
2217 p_es->i_pes_size = 0;
2218 p_es->i_pes_gathered = 0;
2219 p_es->pp_last = &p_es->p_pes;
2220 p_es->p_mpeg4desc = NULL;
2222 p_es->fmt.psz_language = malloc( 4 );
2223 memcpy( p_es->fmt.psz_language,
2224 sub->p_subtitle[n].i_iso6392_language_code, 3);
2225 p_es->fmt.psz_language[3] = 0;
2227 p_es->fmt.subs.dvb.i_id =
2228 sub->p_subtitle[n].i_composition_page_id;
2230 pid->es->fmt.subs.dvb.i_id |=
2231 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
2233 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
2236 #endif /* _DVBPSI_DR_59_H_ */
2239 else if( p_es->i_type == 0xa0 )
2241 /* MSCODEC sent by vlc */
2242 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2244 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
2246 if( p_dr && p_dr->i_length >= 8 )
2248 pid->es->fmt.i_cat = VIDEO_ES;
2249 pid->es->fmt.i_codec =
2250 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
2251 p_dr->p_data[2], p_dr->p_data[3] );
2252 pid->es->fmt.video.i_width =
2253 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
2254 pid->es->fmt.video.i_height =
2255 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
2256 pid->es->fmt.i_extra =
2257 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
2259 if( pid->es->fmt.i_extra > 0 )
2261 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2262 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
2263 pid->es->fmt.i_extra );
2268 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
2271 /* For such stream we will gather them ourself and don't launch a
2273 * Yes it's ugly but it's the only way to have DIV3 working */
2274 pid->es->fmt.b_packetized = VLC_TRUE;
2277 if( pid->es->fmt.i_cat == AUDIO_ES ||
2278 ( pid->es->fmt.i_cat == SPU_ES &&
2279 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
2281 /* get language descriptor */
2282 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2283 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
2287 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
2291 pid->es->fmt.psz_language = malloc( 4 );
2292 memcpy( pid->es->fmt.psz_language,
2293 p_decoded->i_iso_639_code, 3 );
2294 pid->es->fmt.psz_language[3] = 0;
2299 pid->es->fmt.i_group = p_pmt->i_program_number;
2300 if( pid->es->fmt.i_cat == UNKNOWN_ES )
2302 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x *unknown*",
2303 p_es->i_pid, p_es->i_type );
2305 else if( !p_sys->b_udp_out )
2309 msg_Dbg( p_demux, " * es pid=0x%x type=0x%x fcc=%4.4s",
2310 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
2312 if( p_sys->b_es_id_pid )
2314 pid->es->fmt.i_id = p_es->i_pid;
2317 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
2319 for( i = 0; i < pid->i_extra_es; i++ )
2321 pid->extra_es[i]->id =
2322 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt);
2326 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2327 p_dr = p_dr->p_next )
2329 if( p_dr->i_tag == 0x9 )
2331 msg_Dbg( p_demux, " * descriptor : CA (0x9)" );
2333 prg->cad[prg->i_nb_cad] = malloc( p_dr->i_length + 2 );
2334 prg->cad[prg->i_nb_cad][0] = 0x9;
2335 prg->cad[prg->i_nb_cad][1] = p_dr->i_length;
2336 memcpy( prg->cad[prg->i_nb_cad] + 2, p_dr->p_data, p_dr->i_length );
2337 prg->i_cad_length[prg->i_nb_cad] = p_dr->i_length + 2;
2342 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)",
2347 if( p_sys->b_dvb_control &&
2348 ( p_sys->i_dvb_program < 0 || p_sys->i_dvb_program == prg->i_number ) )
2350 /* Set demux filter */
2351 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2352 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
2356 dvbpsi_DeletePMT(p_pmt);
2358 if( p_sys->b_dvb_control &&
2359 ( p_sys->i_dvb_program < 0 || p_sys->i_dvb_program == prg->i_number ) )
2361 /* Set CAM descrambling */
2362 for ( i = 0; i < prg->i_nb_cad; i++ )
2364 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2365 ACCESS_SET_PRIVATE_ID_CA, prg->i_number,
2366 i_vpid, i_apid1, i_apid2, i_apid3,
2367 prg->i_cad_length[i], prg->cad[i] );
2369 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2370 ACCESS_SET_PRIVATE_ID_CA, prg->i_number,
2371 0, 0, 0, 0, 0, NULL );
2372 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2373 ACCESS_SET_PRIVATE_ID_CA, 0,
2374 0, 0, 0, 0, 0, NULL );
2378 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
2380 demux_sys_t *p_sys = p_demux->p_sys;
2381 dvbpsi_pat_program_t *p_program;
2382 ts_pid_t *pat = &p_sys->pid[0];
2385 msg_Dbg( p_demux, "PATCallBack called" );
2387 if( pat->psi->i_pat_version != -1 &&
2388 ( !p_pat->b_current_next ||
2389 p_pat->i_version == pat->psi->i_pat_version ) )
2391 dvbpsi_DeletePAT( p_pat );
2395 msg_Dbg( p_demux, "new PAT ts_id=0x%x version=%d current_next=%d",
2396 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
2399 if( p_sys->i_pmt > 0 )
2402 ts_pid_t **pmt_rm = NULL;
2404 /* Search pmt to be deleted */
2405 for( i = 0; i < p_sys->i_pmt; i++ )
2407 ts_pid_t *pmt = p_sys->pmt[i];
2408 vlc_bool_t b_keep = VLC_FALSE;
2410 for( p_program = p_pat->p_first_program; p_program != NULL;
2411 p_program = p_program->p_next )
2413 if( p_program->i_pid == pmt->i_pid )
2416 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2418 if( p_program->i_number ==
2419 pmt->psi->prg[i_prg]->i_number )
2431 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
2435 /* Delete all ES attached to thoses PMT */
2436 for( i = 2; i < 8192; i++ )
2438 ts_pid_t *pid = &p_sys->pid[i];
2440 if( !pid->b_valid || pid->psi ) continue;
2442 for( j = 0; j < i_pmt_rm; j++ )
2445 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
2447 if( pid->p_owner->prg[i_prg]->i_pid_pmt ==
2448 pmt_rm[j]->i_pid && pid->es->id )
2450 /* We only remove es that aren't defined by extra pmt */
2451 if( p_sys->b_dvb_control )
2453 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, i, VLC_FALSE ) )
2454 p_sys->b_dvb_control = VLC_FALSE;
2457 PIDClean( p_demux->out, pid );
2466 /* Delete PMT pid */
2467 for( i = 0; i < i_pmt_rm; i++ )
2469 if( p_sys->b_dvb_control )
2471 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pmt_rm[i]->i_pid, VLC_FALSE ) )
2472 p_sys->b_dvb_control = VLC_FALSE;
2475 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
2476 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
2484 /* now create programs */
2485 for( p_program = p_pat->p_first_program; p_program != NULL;
2486 p_program = p_program->p_next )
2488 msg_Dbg( p_demux, " * number=%d pid=0x%x", p_program->i_number,
2490 if( p_program->i_number != 0 )
2492 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
2493 vlc_bool_t b_add = VLC_TRUE;
2498 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
2500 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
2509 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
2514 PIDInit( pmt, VLC_TRUE, pat->psi );
2515 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
2516 dvbpsi_AttachPMT( p_program->i_number,
2517 (dvbpsi_pmt_callback)PMTCallBack,
2519 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
2520 p_program->i_number;
2521 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
2524 /* Now select PID at access level */
2525 if( p_sys->b_dvb_control )
2527 if( p_sys->i_dvb_program <= 0 || p_sys->i_dvb_program == p_program->i_number )
2529 if( p_sys->i_dvb_program == 0 )
2530 p_sys->i_dvb_program = p_program->i_number;
2532 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
2533 p_sys->b_dvb_control = VLC_FALSE;
2537 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_FALSE ) )
2538 p_sys->b_dvb_control = VLC_FALSE;
2544 pat->psi->i_pat_version = p_pat->i_version;
2546 dvbpsi_DeletePAT( p_pat );