1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2005 the VideoLAN team
7 * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8 * Jean-Paul Saman <jpsaman #_at_# m2x.nl>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/input.h>
37 #include "../mux/mpeg/csa.h"
39 /* Include dvbpsi headers */
40 #ifdef HAVE_DVBPSI_DR_H
41 # include <dvbpsi/dvbpsi.h>
42 # include <dvbpsi/demux.h>
43 # include <dvbpsi/descriptor.h>
44 # include <dvbpsi/pat.h>
45 # include <dvbpsi/pmt.h>
46 # include <dvbpsi/sdt.h>
47 # include <dvbpsi/dr.h>
48 # include <dvbpsi/psi.h>
52 # include "descriptor.h"
53 # include "tables/pat.h"
54 # include "tables/pmt.h"
55 # include "tables/sdt.h"
56 # include "descriptors/dr.h"
61 #ifdef _DVBPSI_DR_4D_H_
62 # define TS_USE_DVB_SI 1
63 # ifdef HAVE_DVBPSI_DR_H
64 # include <dvbpsi/eit.h>
66 # include "tables/eit.h"
71 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
76 /*****************************************************************************
78 *****************************************************************************/
79 static int Open ( vlc_object_t * );
80 static void Close ( vlc_object_t * );
82 #define PMT_TEXT N_("Extra PMT")
83 #define PMT_LONGTEXT N_( \
84 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])" )
86 #define PID_TEXT N_("Set id of ES to PID")
87 #define PID_LONGTEXT N_("set id of es to pid")
89 #define TSOUT_TEXT N_("Fast udp streaming")
90 #define TSOUT_LONGTEXT N_( \
91 "Sends TS to specific ip:port by udp (you must know what you are doing)")
93 #define MTUOUT_TEXT N_("MTU for out mode")
94 #define MTUOUT_LONGTEXT N_("MTU for out mode")
96 #define CSA_TEXT N_("CSA ck")
97 #define CSA_LONGTEXT N_("CSA ck")
99 #define SILENT_TEXT N_("Silent mode")
100 #define SILENT_LONGTEXT N_("do not complain on encrypted PES")
102 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
103 #define CAPMT_SYSID_LONGTEXT N_("only forward descriptors from this SysID to the CAM")
105 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
106 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
107 "The decryption routines subtract the TS-header from the value before " \
110 #define TSDUMP_TEXT N_("Filename of dump")
111 #define TSDUMP_LONGTEXT N_("Specify a filename where to dump the TS in")
113 #define APPEND_TEXT N_("Append")
114 #define APPEND_LONGTEXT N_( \
115 "If the file exists and this option is selected, the existing file " \
116 "will not be overwritten." )
118 #define DUMPSIZE_TEXT N_("Dump buffer size")
119 #define DUMPSIZE_LONGTEXT N_( \
120 "Tweak the buffer size for reading and writing an integer number of packets." \
121 "Specify the size of the buffer here and not the number of packets." )
124 set_description( _("MPEG Transport Stream demuxer") );
125 set_shortname ( "MPEG-TS" );
126 set_category( CAT_INPUT );
127 set_subcategory( SUBCAT_INPUT_DEMUX );
129 add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
130 add_bool( "ts-es-id-pid", 0, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
131 add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
132 add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
133 MTUOUT_LONGTEXT, VLC_TRUE );
134 add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
135 add_integer( "ts-csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, VLC_TRUE );
136 add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );
138 add_file( "ts-dump-file", NULL, NULL, TSDUMP_TEXT, TSDUMP_LONGTEXT, VLC_FALSE );
139 add_bool( "ts-dump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT, VLC_FALSE );
140 add_integer( "ts-dump-size", 16384, NULL, DUMPSIZE_TEXT,
141 DUMPSIZE_LONGTEXT, VLC_TRUE );
143 set_capability( "demux2", 10 );
144 set_callbacks( Open, Close );
145 add_shortcut( "ts" );
148 /*****************************************************************************
150 *****************************************************************************/
154 uint8_t i_objectTypeIndication;
155 uint8_t i_streamType;
156 vlc_bool_t b_upStream;
157 uint32_t i_bufferSizeDB;
158 uint32_t i_maxBitrate;
159 uint32_t i_avgBitrate;
161 int i_decoder_specific_info_len;
162 uint8_t *p_decoder_specific_info;
164 } decoder_config_descriptor_t;
168 vlc_bool_t b_useAccessUnitStartFlag;
169 vlc_bool_t b_useAccessUnitEndFlag;
170 vlc_bool_t b_useRandomAccessPointFlag;
171 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
172 vlc_bool_t b_usePaddingFlag;
173 vlc_bool_t b_useTimeStampsFlags;
174 vlc_bool_t b_useIdleFlag;
175 vlc_bool_t b_durationFlag;
176 uint32_t i_timeStampResolution;
177 uint32_t i_OCRResolution;
178 uint8_t i_timeStampLength;
181 uint8_t i_instantBitrateLength;
182 uint8_t i_degradationPriorityLength;
183 uint8_t i_AU_seqNumLength;
184 uint8_t i_packetSeqNumLength;
186 uint32_t i_timeScale;
187 uint16_t i_accessUnitDuration;
188 uint16_t i_compositionUnitDuration;
190 uint64_t i_startDecodingTimeStamp;
191 uint64_t i_startCompositionTimeStamp;
193 } sl_config_descriptor_t;
200 vlc_bool_t b_streamDependenceFlag;
201 vlc_bool_t b_OCRStreamFlag;
202 uint8_t i_streamPriority;
206 uint16_t i_dependOn_es_id;
207 uint16_t i_OCR_es_id;
209 decoder_config_descriptor_t dec_descr;
210 sl_config_descriptor_t sl_descr;
212 } es_mpeg4_descriptor_t;
216 uint8_t i_iod_label, i_iod_label_scope;
222 uint8_t i_ODProfileLevelIndication;
223 uint8_t i_sceneProfileLevelIndication;
224 uint8_t i_audioProfileLevelIndication;
225 uint8_t i_visualProfileLevelIndication;
226 uint8_t i_graphicsProfileLevelIndication;
228 es_mpeg4_descriptor_t es_descr[255];
234 dvbpsi_handle handle;
240 /* IOD stuff (mpeg4) */
241 iod_descriptor_t *iod;
247 /* for special PAT/SDT case */
248 dvbpsi_handle handle; /* PAT/SDT/EIT */
267 es_mpeg4_descriptor_t *p_mpeg4desc;
278 int i_cc; /* countinuity counter */
280 /* PSI owner (ie PMT -> PAT, ES -> PMT */
288 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
296 /* TS packet size (188, 192, 204) */
299 /* how many TS packet we read at once */
310 vlc_bool_t b_es_id_pid;
315 vlc_bool_t b_udp_out;
316 int fd; /* udp socket */
319 vlc_bool_t b_dvb_control;
321 vlc_list_t *p_programs_list;
324 char *psz_file; /* file to dump data in */
325 FILE *p_file; /* filehandle */
326 uint64_t i_write; /* bytes written */
327 vlc_bool_t b_file_out; /* dump mode enabled */
333 static int Demux ( demux_t *p_demux );
334 static int DemuxFile( demux_t *p_demux );
335 static int Control( demux_t *p_demux, int i_query, va_list args );
337 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
338 static void PIDClean( es_out_t *out, ts_pid_t *pid );
339 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
341 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
342 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
344 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
345 uint8_t i_table_id, uint16_t i_extension );
348 static inline int PIDGet( block_t *p )
350 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
353 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
355 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
357 static iod_descriptor_t *IODNew( int , uint8_t * );
358 static void IODFree( iod_descriptor_t * );
360 #define TS_PACKET_SIZE_188 188
361 #define TS_PACKET_SIZE_192 192
362 #define TS_PACKET_SIZE_204 204
363 #define TS_PACKET_SIZE_MAX 204
365 /*****************************************************************************
367 *****************************************************************************/
368 static int Open( vlc_object_t *p_this )
370 demux_t *p_demux = (demux_t*)p_this;
374 int i_sync, i_peek, i;
383 if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
384 TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
386 /* Search first sync byte */
387 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
389 if( p_peek[i_sync] == 0x47 ) break;
391 if( i_sync >= TS_PACKET_SIZE_MAX )
393 if( strcmp( p_demux->psz_demux, "ts" ) ) return VLC_EGENERIC;
394 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
397 /* Check next 3 sync bytes */
398 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
399 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
401 msg_Err( p_demux, "cannot peek" );
404 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
405 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
406 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
408 i_packet_size = TS_PACKET_SIZE_188;
410 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
411 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
412 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
414 i_packet_size = TS_PACKET_SIZE_192;
416 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
417 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
418 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
420 i_packet_size = TS_PACKET_SIZE_204;
422 else if( !strcmp( p_demux->psz_demux, "ts" ) )
424 i_packet_size = TS_PACKET_SIZE_188;
428 msg_Warn( p_demux, "TS module discarded (lost sync)" );
432 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
433 memset( p_sys, 0, sizeof( demux_sys_t ) );
434 p_sys->i_packet_size = i_packet_size;
436 /* Fill dump mode fields */
438 p_sys->p_file = NULL;
439 p_sys->b_file_out = VLC_FALSE;
440 p_sys->psz_file = var_CreateGetString( p_demux, "ts-dump-file" );
441 if( *p_sys->psz_file != '\0' )
443 p_sys->b_file_out = VLC_TRUE;
445 var_Create( p_demux, "ts-dump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
446 var_Get( p_demux, "ts-dump-append", &val );
447 b_append = val.b_bool;
453 if( !strcmp( p_sys->psz_file, "-" ) )
455 msg_Info( p_demux, "dumping raw stream to standard output" );
456 p_sys->p_file = stdout;
458 else if( ( p_sys->p_file = fopen( p_sys->psz_file, psz_mode ) ) == NULL )
460 msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
461 p_sys->b_file_out = VLC_FALSE;
464 if( p_sys->b_file_out )
468 /* Determine how many packets to read. */
469 var_Create( p_demux, "ts-dump-size",
470 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
471 var_Get( p_demux, "ts-dump-size", &bufsize );
472 p_sys->i_ts_read = (int) (bufsize.i_int / p_sys->i_packet_size);
473 if( p_sys->i_ts_read <= 0 )
475 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
477 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
478 msg_Info( p_demux, "%s raw stream to file `%s' reading packets %d",
479 b_append ? "appending" : "dumping", p_sys->psz_file,
484 /* Fill p_demux field */
485 if( p_sys->b_file_out )
486 p_demux->pf_demux = DemuxFile;
488 p_demux->pf_demux = Demux;
489 p_demux->pf_control = Control;
491 /* Init p_sys field */
492 p_sys->b_meta = VLC_TRUE;
493 p_sys->b_dvb_control = VLC_TRUE;
494 p_sys->i_dvb_program = 0;
495 for( i = 0; i < 8192; i++ )
497 ts_pid_t *pid = &p_sys->pid[i];
500 pid->b_seen = VLC_FALSE;
501 pid->b_valid = VLC_FALSE;
503 p_sys->i_packet_size = i_packet_size;
504 p_sys->b_udp_out = VLC_FALSE;
505 p_sys->i_ts_read = 50;
508 /* Init PAT handler */
509 pat = &p_sys->pid[0];
510 PIDInit( pat, VLC_TRUE, NULL );
511 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
516 ts_pid_t *sdt = &p_sys->pid[0x11];
517 ts_pid_t *eit = &p_sys->pid[0x12];
519 PIDInit( sdt, VLC_TRUE, NULL );
521 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
523 PIDInit( eit, VLC_TRUE, NULL );
525 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
527 if( p_sys->b_dvb_control )
529 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
530 ACCESS_SET_PRIVATE_ID_STATE, 0x11, VLC_TRUE );
531 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
532 ACCESS_SET_PRIVATE_ID_STATE, 0x12, VLC_TRUE );
542 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
543 var_Get( p_demux, "ts-es-id-pid", &val );
544 p_sys->b_es_id_pid = val.b_bool;
546 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
547 var_Get( p_demux, "ts-out", &val );
548 if( val.psz_string && *val.psz_string && !p_sys->b_file_out )
551 char *psz = strchr( val.psz_string, ':' );
554 p_sys->b_udp_out = VLC_TRUE;
559 i_port = atoi( psz );
561 if( i_port <= 0 ) i_port = 1234;
562 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
564 p_sys->fd = net_OpenUDP( p_demux, "", 0, val.psz_string, i_port );
567 msg_Err( p_demux, "failed to open udp socket, send disabled" );
568 p_sys->b_udp_out = VLC_FALSE;
572 var_Create( p_demux, "ts-out-mtu",
573 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
574 var_Get( p_demux, "ts-out-mtu", &mtu );
575 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
576 if( p_sys->i_ts_read <= 0 )
578 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
580 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
585 free( val.psz_string );
588 /* We handle description of an extra PMT */
589 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
590 var_Get( p_demux, "ts-extra-pmt", &val );
591 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
593 char *psz = val.psz_string;
594 int i_pid = strtol( psz, &psz, 0 );
596 if( i_pid >= 2 && i_pid < 8192 )
598 ts_pid_t *pmt = &p_sys->pid[i_pid];
600 msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
601 PIDInit( pmt, VLC_TRUE, NULL );
603 pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
604 /* FIXME we should also ask for a number */
605 pmt->psi->prg[0]->handle =
606 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
608 pmt->psi->prg[0]->i_number = 0; /* special one */
610 psz = strchr( psz, '=' ) + 1; /* can't failed */
613 char *psz_next = strchr( psz, ',' );
614 int i_pid, i_stream_type;
621 i_pid = strtol( psz, &psz, 0 );
624 i_stream_type = strtol( psz + 1, &psz, 0 );
625 if( i_pid >= 2 && i_pid < 8192 &&
626 !p_sys->pid[i_pid].b_valid )
628 ts_pid_t *pid = &p_sys->pid[i_pid];
630 PIDInit( pid, VLC_FALSE, pmt->psi);
631 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
633 pmt->psi->prg[0]->i_pid_pcr = i_pid;
635 PIDFillFormat( pid, i_stream_type);
636 if( pid->es->fmt.i_cat != UNKNOWN_ES )
638 if( p_sys->b_es_id_pid )
640 pid->es->fmt.i_id = i_pid;
642 msg_Dbg( p_demux, " * es pid=%d type=%d "
643 "fcc=%4.4s", i_pid, i_stream_type,
644 (char*)&pid->es->fmt.i_codec );
645 pid->es->id = es_out_Add( p_demux->out,
656 free( val.psz_string );
659 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
660 var_Get( p_demux, "ts-csa-ck", &val );
661 if( val.psz_string && *val.psz_string )
663 char *psz = val.psz_string;
664 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
668 if( strlen( psz ) != 16 )
670 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
675 uint64_t i_ck = strtoull( psz, NULL, 16 );
677 uint64_t i_ck = strtoll( psz, NULL, 16 );
681 for( i = 0; i < 8; i++ )
683 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
685 #ifndef TS_NO_CSA_CK_MSG
686 msg_Dbg( p_demux, "using CSA scrambling with "
687 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
688 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
690 p_sys->csa = csa_New();
694 csa_SetCW( p_sys->csa, ck, ck );
696 var_Create( p_demux, "ts-csa-pkt", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
697 var_Get( p_demux, "ts-csa-pkt", &val );
698 if( val.i_int < 4 || val.i_int > 188 )
700 msg_Err( p_demux, "wrong packet size %d specified.", val.i_int );
701 msg_Warn( p_demux, "using default packet size of 188 bytes" );
702 p_sys->i_csa_pkt_size = 188;
704 else p_sys->i_csa_pkt_size = val.i_int;
705 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
711 free( val.psz_string );
714 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
715 var_Get( p_demux, "ts-silent", &val );
716 p_sys->b_silent = val.b_bool;
721 /*****************************************************************************
723 *****************************************************************************/
724 static void Close( vlc_object_t *p_this )
726 demux_t *p_demux = (demux_t*)p_this;
727 demux_sys_t *p_sys = p_demux->p_sys;
731 msg_Dbg( p_demux, "pid list:" );
732 for( i = 0; i < 8192; i++ )
734 ts_pid_t *pid = &p_sys->pid[i];
736 if( pid->b_valid && pid->psi )
741 dvbpsi_DetachPAT( pid->psi->handle );
749 dvbpsi_DetachDemux( pid->psi->handle );
753 PIDClean( p_demux->out, pid );
757 else if( pid->b_valid && pid->es )
759 PIDClean( p_demux->out, pid );
764 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
767 if( p_sys->b_dvb_control && pid->i_pid > 0 )
770 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
775 if( p_sys->b_udp_out )
777 net_Close( p_sys->fd );
778 free( p_sys->buffer );
782 csa_Delete( p_sys->csa );
785 if( p_sys->i_pmt ) free( p_sys->pmt );
787 if ( p_sys->p_programs_list )
790 val.p_list = p_sys->p_programs_list;
791 var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
794 /* If in dump mode, then close the file */
795 if( p_sys->b_file_out )
797 msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
798 p_sys->i_write / 1024 );
800 if( p_sys->p_file != stdout )
802 fclose( p_sys->p_file );
803 p_sys->p_file = NULL;
805 free( p_sys->psz_file );
806 p_sys->psz_file = NULL;
808 free( p_sys->buffer );
814 /*****************************************************************************
816 *****************************************************************************/
817 static int DemuxFile( demux_t *p_demux )
819 demux_sys_t *p_sys = p_demux->p_sys;
820 uint8_t *p_buffer = p_sys->buffer; /* Put first on sync byte */
824 int i_bufsize = p_sys->i_packet_size * p_sys->i_ts_read;
826 i_data = stream_Read( p_demux->s, p_sys->buffer, i_bufsize );
827 if( (i_data <= 0) && (i_data < p_sys->i_packet_size) )
829 msg_Dbg( p_demux, "Error reading malformed packets" );
833 /* Test continuity counter */
834 while( i_pos < i_data )
836 ts_pid_t *p_pid; /* point to a PID structure */
837 vlc_bool_t b_payload; /* indicates a packet with payload */
838 vlc_bool_t b_adaptation; /* adaptation field */
839 int i_cc = 0; /* continuity counter */
841 if( p_sys->buffer[i_pos] != 0x47 )
843 msg_Warn( p_demux, "lost sync" );
844 while( !p_demux->b_die && (i_pos < i_data) )
847 if( p_sys->buffer[i_pos] == 0x47 )
850 if( !p_demux->b_die )
851 msg_Warn( p_demux, "sync found" );
854 /* continuous when (one of this):
856 * diff == 0 and payload == 0
857 * diff == 0 and duplicate packet (playload != 0) <- should we
860 i_cc = p_buffer[i_pos+3]&0x0f;
861 b_payload = p_buffer[i_pos+3]&0x10;
862 b_adaptation = p_buffer[i_pos+3]&0x20;
865 p_pid = &p_sys->pid[ ((p_buffer[i_pos+1]&0x1f)<<8)|p_buffer[i_pos+2] ];
867 /* Detect discontinuity indicator in adaptation field */
870 if( p_buffer[i_pos+5]&0x80 )
871 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", p_pid->i_pid );
872 if( p_buffer[i_pos+5]&0x40 )
873 msg_Warn( p_demux, "random access indicator (pid=%d) ", p_pid->i_pid );
876 i_diff = ( i_cc - p_pid->i_cc )&0x0f;
877 if( b_payload && i_diff == 1 )
883 if( p_pid->i_cc == 0xff )
885 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
886 p_pid->i_pid, i_cc );
889 else if( i_diff != 0 )
891 /* FIXME what to do when discontinuity_indicator is set ? */
892 msg_Warn( p_demux, "transport error detected 0x%x instead of 0x%x",
893 i_cc, ( p_pid->i_cc + 1 )&0x0f );
896 /* Mark transport error in the TS packet. */
897 p_buffer[i_pos+1] |= 0x80;
901 /* Test if user wants to decrypt it first */
903 csa_Decrypt( p_demux->p_sys->csa, &p_buffer[i_pos], p_demux->p_sys->i_csa_pkt_size );
905 i_pos += p_sys->i_packet_size;
909 i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
912 msg_Err( p_demux, "failed to write data" );
916 msg_Dbg( p_demux, "dumped %d bytes", i_data );
919 p_sys->i_write += i_data;
923 /*****************************************************************************
925 *****************************************************************************/
926 static int Demux( demux_t *p_demux )
928 demux_sys_t *p_sys = p_demux->p_sys;
931 /* We read at most 100 TS packet or until a frame is completed */
932 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
934 vlc_bool_t b_frame = VLC_FALSE;
938 /* Get a new TS packet */
939 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
941 msg_Dbg( p_demux, "eof ?" );
945 /* Check sync byte and re-sync if needed */
946 if( p_pkt->p_buffer[0] != 0x47 )
948 msg_Warn( p_demux, "lost synchro" );
949 block_Release( p_pkt );
951 while( !p_demux->b_die )
954 int i_peek, i_skip = 0;
956 i_peek = stream_Peek( p_demux->s, &p_peek,
957 p_sys->i_packet_size * 10 );
958 if( i_peek < p_sys->i_packet_size + 1 )
960 msg_Dbg( p_demux, "eof ?" );
964 while( i_skip < i_peek - p_sys->i_packet_size )
966 if( p_peek[i_skip] == 0x47 &&
967 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
974 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
975 stream_Read( p_demux->s, NULL, i_skip );
977 if( i_skip < i_peek - p_sys->i_packet_size )
983 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
985 msg_Dbg( p_demux, "eof ?" );
990 if( p_sys->b_udp_out )
992 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
993 p_pkt->p_buffer, p_sys->i_packet_size );
996 /* Parse the TS packet */
997 p_pid = &p_sys->pid[PIDGet( p_pkt )];
1003 if( p_pid->i_pid == 0 || p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 )
1005 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1010 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1012 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1016 block_Release( p_pkt );
1018 else if( !p_sys->b_udp_out )
1020 b_frame = GatherPES( p_demux, p_pid, p_pkt );
1024 PCRHandle( p_demux, p_pid, p_pkt );
1025 block_Release( p_pkt );
1030 if( !p_pid->b_seen )
1032 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1034 /* We have to handle PCR if present */
1035 PCRHandle( p_demux, p_pid, p_pkt );
1036 block_Release( p_pkt );
1038 p_pid->b_seen = VLC_TRUE;
1046 if( p_sys->b_udp_out )
1048 /* Send the complete block */
1049 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
1050 p_sys->i_ts_read * p_sys->i_packet_size );
1056 /*****************************************************************************
1058 *****************************************************************************/
1059 static int Control( demux_t *p_demux, int i_query, va_list args )
1061 demux_sys_t *p_sys = p_demux->p_sys;
1066 if( p_sys->b_file_out )
1067 return demux2_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
1071 case DEMUX_GET_POSITION:
1072 pf = (double*) va_arg( args, double* );
1073 i64 = stream_Size( p_demux->s );
1076 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
1083 case DEMUX_SET_POSITION:
1084 f = (double) va_arg( args, double );
1085 i64 = stream_Size( p_demux->s );
1087 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1088 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
1090 return VLC_EGENERIC;
1095 case DEMUX_GET_TIME:
1096 pi64 = (int64_t*)va_arg( args, int64_t * );
1097 if( p_sys->i_time < 0 )
1100 return VLC_EGENERIC;
1102 *pi64 = p_sys->i_time;
1105 case DEMUX_GET_LENGTH:
1106 pi64 = (int64_t*)va_arg( args, int64_t * );
1107 if( p_sys->i_mux_rate > 0 )
1109 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
1114 return VLC_EGENERIC;
1116 case DEMUX_SET_GROUP:
1118 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
1119 ts_prg_psi_t *p_prg = NULL;
1122 i_int = (int)va_arg( args, int );
1123 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1124 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1126 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
1131 /* Search pmt to be unselected */
1132 for( i = 0; i < p_sys->i_pmt; i++ )
1134 ts_pid_t *pmt = p_sys->pmt[i];
1137 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1139 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
1141 i_pmt_pid = p_sys->pmt[i]->i_pid;
1145 if( i_pmt_pid > 0 ) break;
1150 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1151 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1154 for( i = 2; i < 8192; i++ )
1156 ts_pid_t *pid = &p_sys->pid[i];
1159 if( !pid->b_valid || pid->psi ) continue;
1161 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1163 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1165 /* We only remove es that aren't defined by extra pmt */
1166 stream_Control( p_demux->s,
1167 STREAM_CONTROL_ACCESS,
1168 ACCESS_SET_PRIVATE_ID_STATE,
1176 /* select new program */
1177 p_sys->i_dvb_program = i_int;
1179 for( i = 0; i < p_sys->i_pmt; i++ )
1181 ts_pid_t *pmt = p_sys->pmt[i];
1184 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1186 if( pmt->psi->prg[i_prg]->i_number == i_int )
1188 i_pmt_pid = p_sys->pmt[i]->i_pid;
1189 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1193 if( i_pmt_pid > 0 ) break;
1197 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1198 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1200 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1201 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
1204 for( i = 2; i < 8192; i++ )
1206 ts_pid_t *pid = &p_sys->pid[i];
1209 if( !pid->b_valid || pid->psi ) continue;
1211 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1213 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1215 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
1217 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
1219 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
1221 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
1224 stream_Control( p_demux->s,
1225 STREAM_CONTROL_ACCESS,
1226 ACCESS_SET_PRIVATE_ID_STATE,
1236 p_sys->i_dvb_program = -1;
1237 p_sys->p_programs_list = p_list;
1243 case DEMUX_SET_TIME:
1245 return VLC_EGENERIC;
1249 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
1251 vlc_bool_t b_old_valid = pid->b_valid;
1253 pid->b_valid = VLC_TRUE;
1255 pid->p_owner = p_owner;
1256 pid->i_owner_number = 0;
1258 pid->extra_es = NULL;
1259 pid->i_extra_es = 0;
1267 pid->psi = malloc( sizeof( ts_psi_t ) );
1268 pid->psi->handle= NULL;
1269 pid->psi->i_prg = 0;
1270 pid->psi->prg = NULL;
1272 pid->psi->i_pat_version = -1;
1273 pid->psi->i_sdt_version = -1;
1276 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1278 prg->i_version = -1;
1280 prg->i_pid_pcr = -1;
1281 prg->i_pid_pmt = -1;
1285 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1291 pid->es = malloc( sizeof( ts_es_t ) );
1293 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1295 pid->es->p_pes = NULL;
1296 pid->es->i_pes_size= 0;
1297 pid->es->i_pes_gathered= 0;
1298 pid->es->pp_last = &pid->es->p_pes;
1299 pid->es->p_mpeg4desc = NULL;
1300 pid->es->b_gather = VLC_FALSE;
1304 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1310 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1311 for( i = 0; i < pid->psi->i_prg; i++ )
1313 if( pid->psi->prg[i]->iod )
1314 IODFree( pid->psi->prg[i]->iod );
1315 if( pid->psi->prg[i]->handle )
1316 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1317 free( pid->psi->prg[i] );
1319 if( pid->psi->prg ) free( pid->psi->prg );
1327 es_out_Del( out, pid->es->id );
1329 if( pid->es->p_pes )
1330 block_ChainRelease( pid->es->p_pes );
1332 es_format_Clean( &pid->es->fmt );
1336 for( i = 0; i < pid->i_extra_es; i++ )
1338 if( pid->extra_es[i]->id )
1339 es_out_Del( out, pid->extra_es[i]->id );
1341 if( pid->extra_es[i]->p_pes )
1342 block_ChainRelease( pid->extra_es[i]->p_pes );
1344 es_format_Clean( &pid->extra_es[i]->fmt );
1346 free( pid->extra_es[i] );
1348 if( pid->i_extra_es ) free( pid->extra_es );
1351 pid->b_valid = VLC_FALSE;
1354 /****************************************************************************
1356 ****************************************************************************/
1357 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1359 block_t *p_pes = pid->es->p_pes;
1365 mtime_t i_length = 0;
1368 /* remove the pes from pid */
1369 pid->es->p_pes = NULL;
1370 pid->es->i_pes_size= 0;
1371 pid->es->i_pes_gathered= 0;
1372 pid->es->pp_last = &pid->es->p_pes;
1374 /* FIXME find real max size */
1375 i_max = block_ChainExtract( p_pes, header, 30 );
1378 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1380 if( !p_demux->p_sys->b_silent )
1381 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1382 header[0], header[1],header[2],header[3], pid->i_pid );
1383 block_ChainRelease( p_pes );
1387 /* TODO check size */
1390 case 0xBC: /* Program stream map */
1391 case 0xBE: /* Padding */
1392 case 0xBF: /* Private stream 2 */
1393 case 0xB0: /* ECM */
1394 case 0xB1: /* EMM */
1395 case 0xFF: /* Program stream directory */
1396 case 0xF2: /* DSMCC stream */
1397 case 0xF8: /* ITU-T H.222.1 type E stream */
1401 if( ( header[6]&0xC0 ) == 0x80 )
1404 i_skip = header[8] + 9;
1406 if( header[7]&0x80 ) /* has pts */
1408 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1409 (mtime_t)(header[10] << 22)|
1410 ((mtime_t)(header[11]&0xfe) << 14)|
1411 (mtime_t)(header[12] << 7)|
1412 (mtime_t)(header[13] >> 1);
1414 if( header[7]&0x40 ) /* has dts */
1416 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1417 (mtime_t)(header[15] << 22)|
1418 ((mtime_t)(header[16]&0xfe) << 14)|
1419 (mtime_t)(header[17] << 7)|
1420 (mtime_t)(header[18] >> 1);
1427 while( i_skip < 23 && header[i_skip] == 0xff )
1433 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1434 block_ChainRelease( p_pes );
1437 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1442 if( header[i_skip]&0x20 )
1444 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1445 (mtime_t)(header[i_skip+1] << 22)|
1446 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1447 (mtime_t)(header[i_skip+3] << 7)|
1448 (mtime_t)(header[i_skip+4] >> 1);
1450 if( header[i_skip]&0x10 ) /* has dts */
1452 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1453 (mtime_t)(header[i_skip+6] << 22)|
1454 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1455 (mtime_t)(header[i_skip+8] << 7)|
1456 (mtime_t)(header[i_skip+9] >> 1);
1472 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1473 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1477 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1478 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1479 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1483 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1484 pid->es->p_mpeg4desc )
1486 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1488 if( dcd->i_decoder_specific_info_len > 2 &&
1489 dcd->p_decoder_specific_info[0] == 0x10 &&
1490 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1492 /* display length */
1493 if( p_pes->i_buffer + 2 <= i_skip )
1495 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1500 if( p_pes->i_buffer + 2 <= i_skip )
1502 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1509 while( p_pes && i_skip > 0 )
1511 if( p_pes->i_buffer <= i_skip )
1513 block_t *p_next = p_pes->p_next;
1515 i_skip -= p_pes->i_buffer;
1516 block_Release( p_pes );
1521 p_pes->i_buffer -= i_skip;
1522 p_pes->p_buffer += i_skip;
1527 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1528 if( i_pts >= 0 && i_dts < 0 )
1538 p_pes->i_dts = i_dts * 100 / 9;
1542 p_pes->i_pts = i_pts * 100 / 9;
1544 p_pes->i_length = i_length * 100 / 9;
1546 p_block = block_ChainGather( p_pes );
1547 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1549 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1551 p_block->i_buffer = i_pes_size;
1554 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1555 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1558 for( i = 0; i < pid->i_extra_es; i++ )
1560 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1561 block_Duplicate( p_block ) );
1564 es_out_Send( p_demux->out, pid->es->id, p_block );
1568 msg_Warn( p_demux, "empty pes" );
1572 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1574 demux_sys_t *p_sys = p_demux->p_sys;
1575 const uint8_t *p = p_bk->p_buffer;
1577 if( ( p[3]&0x20 ) && /* adaptation */
1582 mtime_t i_pcr; /* 33 bits */
1584 i_pcr = ( (mtime_t)p[6] << 25 ) |
1585 ( (mtime_t)p[7] << 17 ) |
1586 ( (mtime_t)p[8] << 9 ) |
1587 ( (mtime_t)p[9] << 1 ) |
1588 ( (mtime_t)p[10] >> 7 );
1590 /* Search program and set the PCR */
1591 for( i = 0; i < p_sys->i_pmt; i++ )
1594 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1596 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1598 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1599 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1600 (int64_t)(i_pcr * 100 / 9) );
1607 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1609 const uint8_t *p = p_bk->p_buffer;
1610 const vlc_bool_t b_unit_start = p[1]&0x40;
1611 const vlc_bool_t b_adaptation = p[3]&0x20;
1612 const vlc_bool_t b_payload = p[3]&0x10;
1613 const int i_cc = p[3]&0x0f; /* continuity counter */
1615 /* transport_scrambling_control is ignored */
1618 vlc_bool_t i_ret = VLC_FALSE;
1622 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1623 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1627 /* For now, ignore additional error correction
1628 * TODO: handle Reed-Solomon 204,188 error correction */
1629 p_bk->i_buffer = TS_PACKET_SIZE_188;
1633 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1635 if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
1636 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1639 if( p_demux->p_sys->csa )
1641 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
1646 /* We don't have any adaptation_field, so payload starts
1647 * immediately after the 4 byte TS header */
1652 /* p[4] is adaptation_field_length minus one */
1658 msg_Warn( p_demux, "discontinuity_indicator (pid=%d) "
1659 "ignored", pid->i_pid );
1664 /* Test continuity counter */
1665 /* continuous when (one of this):
1667 * diff == 0 and payload == 0
1668 * diff == 0 and duplicate packet (playload != 0) <- should we
1669 * test the content ?
1672 i_diff = ( i_cc - pid->i_cc )&0x0f;
1673 if( b_payload && i_diff == 1 )
1679 if( pid->i_cc == 0xff )
1681 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1685 else if( i_diff != 0 )
1687 /* FIXME what to do when discontinuity_indicator is set ? */
1688 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
1689 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
1693 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1695 /* Small video artifacts are usually better then
1696 * dropping full frames */
1697 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1702 PCRHandle( p_demux, pid, p_bk );
1704 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1706 block_Release( p_bk );
1710 /* We have to gather it */
1711 p_bk->p_buffer += i_skip;
1712 p_bk->i_buffer -= i_skip;
1716 if( pid->es->p_pes )
1718 ParsePES( p_demux, pid );
1722 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1723 if( p_bk->i_buffer > 6 )
1725 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1726 if( pid->es->i_pes_size > 0 )
1728 pid->es->i_pes_size += 6;
1731 pid->es->i_pes_gathered += p_bk->i_buffer;
1732 if( pid->es->i_pes_size > 0 &&
1733 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1735 ParsePES( p_demux, pid );
1741 if( pid->es->p_pes == NULL )
1743 /* msg_Dbg( p_demux, "broken packet" ); */
1744 block_Release( p_bk );
1748 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1749 pid->es->i_pes_gathered += p_bk->i_buffer;
1750 if( pid->es->i_pes_size > 0 &&
1751 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1753 ParsePES( p_demux, pid );
1762 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1764 es_format_t *fmt = &pid->es->fmt;
1766 switch( i_stream_type )
1768 case 0x01: /* MPEG-1 video */
1769 case 0x02: /* MPEG-2 video */
1770 case 0x80: /* MPEG-2 MOTO video */
1771 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1773 case 0x03: /* MPEG-1 audio */
1774 case 0x04: /* MPEG-2 audio */
1775 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1777 case 0x11: /* MPEG4 (audio) */
1778 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1779 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1781 case 0x10: /* MPEG4 (video) */
1782 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1783 pid->es->b_gather = VLC_TRUE;
1785 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1786 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1789 case 0x81: /* A52 (audio) */
1790 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1792 case 0x82: /* DVD_SPU (sub) */
1793 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1795 case 0x83: /* LPCM (audio) */
1796 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1798 case 0x84: /* SDDS (audio) */
1799 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1801 case 0x85: /* DTS (audio) */
1802 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1805 case 0x91: /* A52 vls (audio) */
1806 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1808 case 0x92: /* DVD_SPU vls (sub) */
1809 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1811 case 0x93: /* LPCM vls (audio) */
1812 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'b' ) );
1814 case 0x94: /* SDDS (audio) */
1815 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1818 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1819 es_format_Init( fmt, UNKNOWN_ES, 0 );
1820 pid->es->b_gather = VLC_TRUE;
1823 case 0x06: /* PES_PRIVATE (fixed later) */
1824 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1826 es_format_Init( fmt, UNKNOWN_ES, 0 );
1830 /* PES packets usually contain truncated frames */
1831 fmt->b_packetized = VLC_FALSE;
1833 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1836 /*****************************************************************************
1837 * MP4 specific functions (IOD parser)
1838 *****************************************************************************/
1839 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1842 unsigned int i_len = 0;
1848 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1850 } while( i_b&0x80 );
1854 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1858 const int i_b = **pp_data;
1865 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1867 const int i1 = IODGetByte( pi_data, pp_data );
1868 const int i2 = IODGetByte( pi_data, pp_data );
1869 return( ( i1 << 8 ) | i2 );
1871 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1873 const int i1 = IODGetByte( pi_data, pp_data );
1874 const int i2 = IODGetByte( pi_data, pp_data );
1875 const int i3 = IODGetByte( pi_data, pp_data );
1877 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1880 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1882 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1883 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1884 return( ( i1 << 16 ) | i2 );
1887 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1892 i_url_len = IODGetByte( pi_data, pp_data );
1893 url = malloc( i_url_len + 1 );
1894 for( i = 0; i < i_url_len; i++ )
1896 url[i] = IODGetByte( pi_data, pp_data );
1898 url[i_url_len] = '\0';
1902 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
1904 iod_descriptor_t *p_iod;
1907 uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
1911 p_iod = malloc( sizeof( iod_descriptor_t ) );
1912 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
1915 fprintf( stderr, "\n************ IOD ************" );
1917 for( i = 0; i < 255; i++ )
1919 p_iod->es_descr[i].b_ok = 0;
1928 byte1 = IODGetByte( &i_data, &p_data );
1929 byte2 = IODGetByte( &i_data, &p_data );
1930 byte3 = IODGetByte( &i_data, &p_data );
1931 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
1933 p_iod->i_iod_label_scope = 0x11;
1934 p_iod->i_iod_label = byte1;
1937 else //correct implementation of the IOD_descriptor
1939 p_iod->i_iod_label_scope = byte1;
1940 p_iod->i_iod_label = byte2;
1944 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
1945 fprintf( stderr, "\n* ===========" );
1946 fprintf( stderr, "\n* tag:0x%x", i_iod_tag );
1948 if( i_iod_tag != 0x02 )
1950 fprintf( stderr, "\n ERR: tag %02x != 0x02", i_iod_tag );
1954 i_iod_length = IODDescriptorLength( &i_data, &p_data );
1956 fprintf( stderr, "\n* length:%d", i_iod_length );
1958 if( i_iod_length > i_data )
1960 i_iod_length = i_data;
1963 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
1964 i_flags = IODGetByte( &i_data, &p_data );
1965 p_iod->i_od_id |= i_flags >> 6;
1966 b_url = ( i_flags >> 5 )&0x01;
1968 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
1969 fprintf( stderr, "\n* url flag:%d", b_url );
1970 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
1974 p_iod->psz_url = IODGetURL( &i_data, &p_data );
1976 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
1977 fprintf( stderr, "\n*****************************\n" );
1983 p_iod->psz_url = NULL;
1986 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
1987 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
1988 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
1989 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
1990 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
1992 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
1993 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
1994 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
1995 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
1996 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
1999 while( i_data > 0 && i_es_index < 255)
2001 int i_tag, i_length;
2003 uint8_t *p_data_sav;
2005 i_tag = IODGetByte( &i_data, &p_data );
2006 i_length = IODDescriptorLength( &i_data, &p_data );
2008 i_data_sav = i_data;
2009 p_data_sav = p_data;
2017 #define es_descr p_iod->es_descr[i_es_index]
2018 int i_decoderConfigDescr_length;
2020 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
2024 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
2025 i_flags = IODGetByte( &i_data, &p_data );
2026 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2027 b_url = ( i_flags >> 6 )&0x01;
2028 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2029 es_descr.i_streamPriority = i_flags & 0x1f;
2031 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
2032 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
2033 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
2035 if( es_descr.b_streamDependenceFlag )
2037 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
2039 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
2045 es_descr.psz_url = IODGetURL( &i_data, &p_data );
2047 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
2052 es_descr.psz_url = NULL;
2055 if( es_descr.b_OCRStreamFlag )
2057 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
2059 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
2063 if( IODGetByte( &i_data, &p_data ) != 0x04 )
2066 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
2071 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2073 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
2075 #define dec_descr es_descr.dec_descr
2076 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
2077 i_flags = IODGetByte( &i_data, &p_data );
2078 dec_descr.i_streamType = i_flags >> 2;
2079 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
2080 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
2081 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
2082 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
2084 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
2085 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
2086 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
2087 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
2088 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
2089 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
2091 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
2094 dec_descr.i_decoder_specific_info_len =
2095 IODDescriptorLength( &i_data, &p_data );
2096 if( dec_descr.i_decoder_specific_info_len > 0 )
2098 dec_descr.p_decoder_specific_info =
2099 malloc( dec_descr.i_decoder_specific_info_len );
2101 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
2103 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
2108 dec_descr.i_decoder_specific_info_len = 0;
2109 dec_descr.p_decoder_specific_info = NULL;
2113 #define sl_descr es_descr.sl_descr
2115 int i_SLConfigDescr_length;
2118 if( IODGetByte( &i_data, &p_data ) != 0x06 )
2121 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
2126 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2128 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
2130 i_predefined = IODGetByte( &i_data, &p_data );
2132 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
2134 switch( i_predefined )
2138 sl_descr.b_useAccessUnitStartFlag = 0;
2139 sl_descr.b_useAccessUnitEndFlag = 0;
2140 sl_descr.b_useRandomAccessPointFlag = 0;
2141 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
2142 sl_descr.b_usePaddingFlag = 0;
2143 sl_descr.b_useTimeStampsFlags = 0;
2144 sl_descr.b_useIdleFlag = 0;
2145 sl_descr.b_durationFlag = 0; // FIXME FIXME
2146 sl_descr.i_timeStampResolution = 1000;
2147 sl_descr.i_OCRResolution = 0; // FIXME FIXME
2148 sl_descr.i_timeStampLength = 32;
2149 sl_descr.i_OCRLength = 0; // FIXME FIXME
2150 sl_descr.i_AU_Length = 0;
2151 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
2152 sl_descr.i_degradationPriorityLength= 0;
2153 sl_descr.i_AU_seqNumLength = 0;
2154 sl_descr.i_packetSeqNumLength = 0;
2155 if( sl_descr.b_durationFlag )
2157 sl_descr.i_timeScale = 0; // FIXME FIXME
2158 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
2159 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
2161 if( !sl_descr.b_useTimeStampsFlags )
2163 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
2164 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
2170 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
2181 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
2186 p_data = p_data_sav + i_length;
2187 i_data = i_data_sav - i_length;
2191 fprintf( stderr, "\n*****************************\n" );
2196 static void IODFree( iod_descriptor_t *p_iod )
2200 if( p_iod->psz_url )
2202 free( p_iod->psz_url );
2203 p_iod->psz_url = NULL;
2208 for( i = 0; i < 255; i++ )
2210 #define es_descr p_iod->es_descr[i]
2213 if( es_descr.psz_url )
2215 free( es_descr.psz_url );
2216 es_descr.psz_url = NULL;
2220 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
2222 free( es_descr.dec_descr.p_decoder_specific_info );
2223 es_descr.dec_descr.p_decoder_specific_info = NULL;
2224 es_descr.dec_descr.i_decoder_specific_info_len = 0;
2234 /****************************************************************************
2235 ****************************************************************************
2236 ** libdvbpsi callbacks
2237 ****************************************************************************
2238 ****************************************************************************/
2239 static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2241 demux_sys_t *p_sys = p_demux->p_sys;
2243 if ( !p_sys->b_dvb_control ) return VLC_FALSE;
2244 if ( p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL )
2246 if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
2248 if ( p_sys->p_programs_list != NULL )
2251 for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
2253 if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
2260 #ifdef TS_USE_DVB_SI
2261 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2263 demux_sys_t *p_sys = p_demux->p_sys;
2264 ts_pid_t *sdt = &p_sys->pid[0x11];
2265 dvbpsi_sdt_service_t *p_srv;
2267 msg_Dbg( p_demux, "SDTCallBack called" );
2269 if( sdt->psi->i_sdt_version != -1 &&
2270 ( !p_sdt->b_current_next ||
2271 p_sdt->i_version == sdt->psi->i_sdt_version ) )
2273 dvbpsi_DeleteSDT( p_sdt );
2277 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2279 p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
2280 p_sdt->i_network_id );
2282 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2284 vlc_meta_t *p_meta = vlc_meta_New();
2285 dvbpsi_descriptor_t *p_dr;
2287 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
2288 "runing=%d free_ca=%d",
2289 p_srv->i_service_id, p_srv->b_eit_schedule,
2290 p_srv->b_eit_present, p_srv->i_running_status,
2293 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2295 if( p_dr->i_tag == 0x48 )
2297 static const char *psz_type[0x11] = {
2299 "Digital television service",
2300 "Digital radio sound service",
2302 "NVOD reference service",
2303 "NVOD time-shifted service",
2306 "SECAM coded signal",
2309 "NTSC coded signal",
2310 "Data broadcast service",
2311 "Reserved for Common Interface Usage",
2312 "RCS Map (see EN 301 790 [35])",
2313 "RCS FLS (see EN 301 790 [35])",
2316 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2320 memcpy( str1, pD->i_service_provider_name,
2321 pD->i_service_provider_name_length );
2322 str1[pD->i_service_provider_name_length] = '\0';
2323 memcpy( str2, pD->i_service_name, pD->i_service_name_length );
2324 str2[pD->i_service_name_length] = '\0';
2326 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
2327 pD->i_service_type, str1, str2 );
2329 vlc_meta_Add( p_meta, "Name", str2 );
2330 vlc_meta_Add( p_meta, "Provider", str1 );
2331 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2332 vlc_meta_Add( p_meta, "Type", psz_type[pD->i_service_type] );
2336 if( p_srv->i_running_status == 0x01 )
2337 vlc_meta_Add( p_meta, "Status", "Not running" );
2338 else if( p_srv->i_running_status == 0x02 )
2339 vlc_meta_Add( p_meta, "Status", "Starts in a few seconds" );
2340 else if( p_srv->i_running_status == 0x03 )
2341 vlc_meta_Add( p_meta, "Status", "Pausing" );
2342 else if( p_srv->i_running_status == 0x04 )
2343 vlc_meta_Add( p_meta, "Status", "Running" );
2345 vlc_meta_Add( p_meta, "Status", "Unknown" );
2348 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2349 p_srv->i_service_id, p_meta );
2350 vlc_meta_Delete( p_meta );
2353 sdt->psi->i_sdt_version = p_sdt->i_version;
2354 dvbpsi_DeleteSDT( p_sdt );
2357 static void DecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2359 int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2360 int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2362 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2364 if( mp == 14 || mp == 15 )
2376 static void EITEventFixString( unsigned char *psz )
2379 /* Sometimes the first char isn't a normal char but designed
2380 * caracters encoding, for now lets skip it */
2381 if( psz[0] >= 0x20 )
2383 if( ( i_len = strlen( psz ) ) > 0 )
2384 memmove( &psz[0], &psz[1], i_len ); /* Copy the \0 too */
2386 static void EITCallBack( demux_t *p_demux, dvbpsi_eit_t *p_eit )
2388 dvbpsi_eit_event_t *p_evt;
2390 vlc_bool_t b_event_active = VLC_FALSE;
2392 msg_Dbg( p_demux, "EITCallBack called" );
2393 if( !p_eit->b_current_next )
2395 dvbpsi_DeleteEIT( p_eit );
2399 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
2400 "ts_id=%d network_id=%d segment_last_section_number=%d "
2402 p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
2403 p_eit->i_ts_id, p_eit->i_network_id,
2404 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2406 p_meta = vlc_meta_New();
2407 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2409 dvbpsi_descriptor_t *p_dr;
2410 char *psz_cat = malloc( strlen("Event")+10 );
2412 char psz_duration[15];
2415 char *psz_extra = strdup("");
2418 sprintf( psz_cat, "Event %d", p_evt->i_event_id );
2419 sprintf( psz_start, "%d%d:%d%d:%d%d",
2420 (int)(p_evt->i_start_time >> 20)&0xf,
2421 (int)(p_evt->i_start_time >> 16)&0xf,
2422 (int)(p_evt->i_start_time >> 12)&0xf,
2423 (int)(p_evt->i_start_time >> 8)&0xf,
2424 (int)(p_evt->i_start_time >> 4)&0xf,
2425 (int)(p_evt->i_start_time )&0xf );
2426 sprintf( psz_duration, "%d%d:%d%d:%d%d",
2427 (p_evt->i_duration >> 20)&0xf, (p_evt->i_duration >> 16)&0xf,
2428 (p_evt->i_duration >> 12)&0xf, (p_evt->i_duration >> 8)&0xf,
2429 (p_evt->i_duration >> 4)&0xf, (p_evt->i_duration )&0xf );
2430 psz_name[0] = psz_text[0] = '\0';
2432 msg_Dbg( p_demux, " * event id=%d start_time:mjd=%d %s duration=%s "
2433 "running=%d free_ca=%d",
2435 (int)(p_evt->i_start_time >> 24),
2436 psz_start, psz_duration,
2437 p_evt->i_running_status, p_evt->b_free_ca );
2439 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2441 if( p_dr->i_tag == 0x4d )
2443 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2447 memcpy( psz_name, pE->i_event_name, pE->i_event_name_length);
2448 psz_name[pE->i_event_name_length] = '\0';
2449 memcpy( psz_text, pE->i_text, pE->i_text_length );
2450 psz_text[pE->i_text_length] = '\0';
2452 EITEventFixString(psz_name);
2453 EITEventFixString(psz_text);
2454 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
2455 pE->i_iso_639_code, psz_name, psz_text );
2458 else if( p_dr->i_tag == 0x4e )
2460 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
2467 msg_Dbg( p_demux, " - extended event lang=%3.3s",
2468 pE->i_iso_639_code );
2469 for( i = 0; i < pE->i_entry_count; i++ )
2471 memcpy( str1, pE->i_item_description[i],
2472 pE->i_item_description_length[i] );
2473 str1[pE->i_item_description_length[i]] = '\0';
2474 EITEventFixString(str1);
2476 memcpy( str2, pE->i_item[i],
2477 pE->i_item_length[i] );
2478 str2[pE->i_item_length[i]] = '\0';
2479 EITEventFixString(str2);
2481 msg_Dbg( p_demux, " - desc='%s' item='%s'", str1, str2 );
2482 psz_extra = realloc( psz_extra,
2484 strlen(str1) +strlen(str2) + 1 + 3 );
2485 strcat( psz_extra, str1 );
2486 strcat( psz_extra, "(" );
2487 strcat( psz_extra, str2 );
2488 strcat( psz_extra, ") " );
2491 memcpy( str1, pE->i_text, pE->i_text_length );
2492 str1[pE->i_text_length] = '\0';
2493 EITEventFixString(str1);
2495 msg_Dbg( p_demux, " - text='%s'", str1 );
2496 psz_extra = realloc( psz_extra,
2497 strlen(psz_extra) + strlen(str1) + 2 );
2498 strcat( psz_extra, str1 );
2499 strcat( psz_extra, " " );
2504 msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
2508 asprintf( &psz_value, "%s: %s (+%s) %s (%s)",
2512 psz_text, psz_extra );
2513 vlc_meta_Add( p_meta, psz_cat, psz_value );
2516 if( p_evt->i_running_status == 0x04 )
2518 vlc_meta_Add( p_meta, VLC_META_NOW_PLAYING, psz_name );
2519 b_event_active = VLC_TRUE;
2526 if( !b_event_active )
2527 vlc_meta_Add( p_meta, VLC_META_NOW_PLAYING, "" );
2528 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2529 p_eit->i_service_id, p_meta );
2530 vlc_meta_Delete( p_meta );
2532 dvbpsi_DeleteEIT( p_eit );
2535 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
2536 uint8_t i_table_id, uint16_t i_extension )
2539 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2540 i_table_id, i_table_id, i_extension, i_extension );
2543 if( i_table_id == 0x42 )
2545 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2546 i_table_id, i_table_id, i_extension, i_extension );
2548 dvbpsi_AttachSDT( h, i_table_id, i_extension,
2549 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
2551 else if( i_table_id == 0x4e || /* Current/Following */
2552 ( i_table_id >= 0x50 && i_table_id <= 0x5f ) ) /* Schedule */
2554 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2555 i_table_id, i_table_id, i_extension, i_extension );
2557 dvbpsi_AttachEIT( h, i_table_id, i_extension,
2558 (dvbpsi_eit_callback)EITCallBack, p_demux );
2563 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
2565 demux_sys_t *p_sys = p_demux->p_sys;
2566 dvbpsi_descriptor_t *p_dr;
2567 dvbpsi_pmt_es_t *p_es;
2569 ts_pid_t *pmt = NULL;
2570 ts_prg_psi_t *prg = NULL;
2572 ts_pid_t **pp_clean = NULL;
2575 msg_Dbg( p_demux, "PMTCallBack called" );
2577 /* First find this PMT declared in PAT */
2578 for( i = 0; i < p_sys->i_pmt; i++ )
2581 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2583 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
2584 p_pmt->i_program_number )
2586 pmt = p_sys->pmt[i];
2587 prg = p_sys->pmt[i]->psi->prg[i_prg];
2596 msg_Warn( p_demux, "unreferenced program (broken stream)" );
2597 dvbpsi_DeletePMT(p_pmt);
2601 if( prg->i_version != -1 &&
2602 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
2604 dvbpsi_DeletePMT( p_pmt );
2608 /* Clean this program (remove all es) */
2609 for( i = 0; i < 8192; i++ )
2611 ts_pid_t *pid = &p_sys->pid[i];
2613 if( pid->b_valid && pid->p_owner == pmt->psi &&
2614 pid->i_owner_number == prg->i_number && pid->psi == NULL )
2616 TAB_APPEND( i_clean, pp_clean, pid );
2621 IODFree( prg->iod );
2625 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
2626 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
2627 prg->i_pid_pcr = p_pmt->i_pcr_pid;
2628 prg->i_version = p_pmt->i_version;
2630 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2632 /* Set demux filter */
2633 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2634 ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
2637 else if ( p_sys->b_dvb_control )
2639 msg_Warn( p_demux, "skipping program (not selected)" );
2640 dvbpsi_DeletePMT(p_pmt);
2644 /* Parse descriptor */
2645 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
2647 if( p_dr->i_tag == 0x1d )
2649 /* We have found an IOD descriptor */
2650 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
2652 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
2654 else if( p_dr->i_tag == 0x9 )
2656 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2658 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
2662 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2666 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2668 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
2670 /* Find out if the PID was already declared */
2671 for( i = 0; i < i_clean; i++ )
2673 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
2675 old_pid = pp_clean[i];
2680 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
2682 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
2683 if( ( pid->i_pid == 0x11 /* SDT */ ||
2684 pid->i_pid == 0x12 /* EDT */ ) && pid->psi )
2686 /* This doesn't look like a DVB stream so don't try
2687 * parsing the SDT/EDT */
2688 dvbpsi_DetachDemux( pid->psi->handle );
2694 msg_Warn( p_demux, "pmt error: pid=%d already defined",
2700 PIDInit( pid, VLC_FALSE, pmt->psi );
2701 PIDFillFormat( pid, p_es->i_type );
2702 pid->i_owner_number = prg->i_number;
2703 pid->i_pid = p_es->i_pid;
2704 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
2706 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2707 p_es->i_type == 0x12 )
2709 /* MPEG-4 stream: search SL_DESCRIPTOR */
2710 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2712 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2714 if( p_dr && p_dr->i_length == 2 )
2716 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2718 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2720 pid->es->p_mpeg4desc = NULL;
2722 for( i = 0; i < 255; i++ )
2724 iod_descriptor_t *iod = prg->iod;
2726 if( iod->es_descr[i].b_ok &&
2727 iod->es_descr[i].i_es_id == i_es_id )
2729 pid->es->p_mpeg4desc = &iod->es_descr[i];
2735 if( pid->es->p_mpeg4desc != NULL )
2737 decoder_config_descriptor_t *dcd =
2738 &pid->es->p_mpeg4desc->dec_descr;
2740 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2742 pid->es->fmt.i_cat = VIDEO_ES;
2743 switch( dcd->i_objectTypeIndication )
2745 case 0x0B: /* mpeg4 sub */
2746 pid->es->fmt.i_cat = SPU_ES;
2747 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2750 case 0x20: /* mpeg4 */
2751 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2753 case 0x21: /* h264 */
2754 pid->es->fmt.i_codec = VLC_FOURCC('h','2','6','4');
2761 case 0x65: /* mpeg2 */
2762 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2764 case 0x6a: /* mpeg1 */
2765 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2767 case 0x6c: /* mpeg1 */
2768 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2771 pid->es->fmt.i_cat = UNKNOWN_ES;
2775 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2777 pid->es->fmt.i_cat = AUDIO_ES;
2778 switch( dcd->i_objectTypeIndication )
2780 case 0x40: /* mpeg4 */
2781 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2785 case 0x68: /* mpeg2 aac */
2786 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2788 case 0x69: /* mpeg2 */
2789 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2791 case 0x6b: /* mpeg1 */
2792 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2795 pid->es->fmt.i_cat = UNKNOWN_ES;
2801 pid->es->fmt.i_cat = UNKNOWN_ES;
2804 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2806 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2807 if( pid->es->fmt.i_extra > 0 )
2809 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2810 memcpy( pid->es->fmt.p_extra,
2811 dcd->p_decoder_specific_info,
2812 pid->es->fmt.i_extra );
2817 else if( p_es->i_type == 0x06 )
2819 dvbpsi_descriptor_t *p_dr;
2821 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2822 p_dr = p_dr->p_next )
2824 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
2825 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2827 if( p_dr->i_tag == 0x05 )
2829 /* Registration Descriptor */
2830 if( p_dr->i_length != 4 )
2832 msg_Warn( p_demux, "invalid Registration Descriptor" );
2836 if( !memcmp( p_dr->p_data, "AC-3", 4 ) )
2838 /* ATSC with stream_type 0x81 (but this descriptor
2839 * is then not mandatory */
2840 pid->es->fmt.i_cat = AUDIO_ES;
2841 pid->es->fmt.i_codec = VLC_FOURCC('a','5','2',' ');
2843 else if( !memcmp( p_dr->p_data, "DTS1", 4 ) ||
2844 !memcmp( p_dr->p_data, "DTS2", 4 ) ||
2845 !memcmp( p_dr->p_data, "DTS3", 4 ) )
2847 /*registration descriptor(ETSI TS 101 154 Annex F)*/
2848 pid->es->fmt.i_cat = AUDIO_ES;
2849 pid->es->fmt.i_codec = VLC_FOURCC('d','t','s',' ');
2851 else if( !memcmp( p_dr->p_data, "BSSD", 4 ) )
2853 pid->es->fmt.i_cat = AUDIO_ES;
2854 pid->es->fmt.i_codec = VLC_FOURCC('l','p','c','m');
2859 "unknown Registration Descriptor (%4.4s)",
2865 else if( p_dr->i_tag == 0x6a )
2867 /* DVB with stream_type 0x06 */
2868 pid->es->fmt.i_cat = AUDIO_ES;
2869 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2871 else if( p_dr->i_tag == 0x73 )
2873 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2874 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2875 pid->es->fmt.i_cat = AUDIO_ES;
2876 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2878 else if( p_dr->i_tag == 0x45 )
2880 msg_Dbg( p_demux, " * VBI Data descriptor" );
2881 /* FIXME : store the information somewhere */
2883 else if( p_dr->i_tag == 0x46 )
2885 msg_Dbg( p_demux, " * VBI Teletext descriptor" );
2886 /* FIXME : store the information somewhere */
2888 else if( p_dr->i_tag == 0x56 )
2890 msg_Dbg( p_demux, " * EBU Teletext descriptor" );
2891 pid->es->fmt.i_cat = SPU_ES;
2892 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
2893 pid->es->fmt.psz_description = strdup( "Teletext" );
2894 pid->es->fmt.i_extra = p_dr->i_length;
2895 pid->es->fmt.p_extra = malloc( p_dr->i_length );
2896 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
2899 #ifdef _DVBPSI_DR_59_H_
2900 else if( p_dr->i_tag == 0x59 )
2903 dvbpsi_subtitling_dr_t *sub;
2906 pid->es->fmt.i_cat = SPU_ES;
2907 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
2908 pid->es->fmt.i_group = p_pmt->i_program_number;
2910 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
2911 if( !sub ) continue;
2913 /* Each subtitle ES contains n languages,
2914 * We are going to create n ES for the n tracks */
2915 if( sub->i_subtitles_number > 0 )
2917 pid->es->fmt.psz_language = malloc( 4 );
2918 memcpy( pid->es->fmt.psz_language,
2919 sub->p_subtitle[0].i_iso6392_language_code, 3);
2920 pid->es->fmt.psz_language[3] = 0;
2922 pid->es->fmt.subs.dvb.i_id =
2923 sub->p_subtitle[0].i_composition_page_id;
2925 pid->es->fmt.subs.dvb.i_id |=
2926 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
2928 else pid->es->fmt.i_cat = UNKNOWN_ES;
2930 for( n = 1; n < sub->i_subtitles_number; n++ )
2932 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
2933 p_es->fmt = pid->es->fmt;
2936 p_es->i_pes_size = 0;
2937 p_es->i_pes_gathered = 0;
2938 p_es->pp_last = &p_es->p_pes;
2939 p_es->p_mpeg4desc = NULL;
2941 p_es->fmt.psz_language = malloc( 4 );
2942 memcpy( p_es->fmt.psz_language,
2943 sub->p_subtitle[n].i_iso6392_language_code, 3);
2944 p_es->fmt.psz_language[3] = 0;
2946 p_es->fmt.subs.dvb.i_id =
2947 sub->p_subtitle[n].i_composition_page_id;
2949 p_es->fmt.subs.dvb.i_id |=
2950 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
2952 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
2955 #endif /* _DVBPSI_DR_59_H_ */
2958 else if( p_es->i_type == 0xa0 )
2960 /* MSCODEC sent by vlc */
2961 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
2963 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
2965 if( p_dr && p_dr->i_length >= 8 )
2967 pid->es->fmt.i_cat = VIDEO_ES;
2968 pid->es->fmt.i_codec =
2969 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
2970 p_dr->p_data[2], p_dr->p_data[3] );
2971 pid->es->fmt.video.i_width =
2972 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
2973 pid->es->fmt.video.i_height =
2974 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
2975 pid->es->fmt.i_extra =
2976 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
2978 if( pid->es->fmt.i_extra > 0 )
2980 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2981 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
2982 pid->es->fmt.i_extra );
2987 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
2990 /* For such stream we will gather them ourself and don't launch a
2992 * Yes it's ugly but it's the only way to have DIV3 working */
2993 pid->es->fmt.b_packetized = VLC_TRUE;
2996 if( pid->es->fmt.i_cat == AUDIO_ES ||
2997 ( pid->es->fmt.i_cat == SPU_ES &&
2998 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
3000 /* get language descriptor */
3001 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
3002 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
3006 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
3010 pid->es->fmt.psz_language = malloc( 4 );
3011 memcpy( pid->es->fmt.psz_language,
3012 p_decoded->i_iso_639_code, 3 );
3013 pid->es->fmt.psz_language[3] = 0;
3018 pid->es->fmt.i_group = p_pmt->i_program_number;
3019 if( pid->es->fmt.i_cat == UNKNOWN_ES )
3021 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
3022 p_es->i_pid, p_es->i_type );
3024 else if( !p_sys->b_udp_out )
3026 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
3027 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
3029 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
3031 /* Check if we can avoid restarting the ES */
3033 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
3034 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
3035 pid->es->fmt.i_extra == 0 &&
3036 pid->i_extra_es == old_pid->i_extra_es &&
3037 ( ( !pid->es->fmt.psz_language &&
3038 !old_pid->es->fmt.psz_language ) ||
3039 ( pid->es->fmt.psz_language &&
3040 old_pid->es->fmt.psz_language &&
3041 !strcmp( pid->es->fmt.psz_language,
3042 old_pid->es->fmt.psz_language ) ) ) )
3044 pid->es->id = old_pid->es->id;
3045 old_pid->es->id = NULL;
3046 for( i = 0; i < pid->i_extra_es; i++ )
3048 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
3049 old_pid->extra_es[i]->id = NULL;
3056 PIDClean( p_demux->out, old_pid );
3057 TAB_REMOVE( i_clean, pp_clean, old_pid );
3061 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
3062 for( i = 0; i < pid->i_extra_es; i++ )
3064 pid->extra_es[i]->id =
3065 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
3070 /* Add ES to the list */
3073 PIDClean( p_demux->out, old_pid );
3074 TAB_REMOVE( i_clean, pp_clean, old_pid );
3076 p_sys->pid[p_es->i_pid] = *pid;
3078 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
3079 p_dr = p_dr->p_next )
3081 if( p_dr->i_tag == 0x9 )
3083 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
3085 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
3090 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3092 /* Set demux filter */
3093 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3094 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
3099 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3101 /* Set CAM descrambling */
3102 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3103 ACCESS_SET_PRIVATE_ID_CA, p_pmt );
3107 dvbpsi_DeletePMT( p_pmt );
3110 for ( i = 0; i < i_clean; i++ )
3112 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3114 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3115 ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
3119 PIDClean( p_demux->out, pp_clean[i] );
3121 if( i_clean ) free( pp_clean );
3124 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
3126 demux_sys_t *p_sys = p_demux->p_sys;
3127 dvbpsi_pat_program_t *p_program;
3128 ts_pid_t *pat = &p_sys->pid[0];
3131 msg_Dbg( p_demux, "PATCallBack called" );
3133 if( pat->psi->i_pat_version != -1 &&
3134 ( !p_pat->b_current_next ||
3135 p_pat->i_version == pat->psi->i_pat_version ) )
3137 dvbpsi_DeletePAT( p_pat );
3141 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
3142 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
3145 if( p_sys->i_pmt > 0 )
3148 ts_pid_t **pmt_rm = NULL;
3150 /* Search pmt to be deleted */
3151 for( i = 0; i < p_sys->i_pmt; i++ )
3153 ts_pid_t *pmt = p_sys->pmt[i];
3154 vlc_bool_t b_keep = VLC_FALSE;
3156 for( p_program = p_pat->p_first_program; p_program != NULL;
3157 p_program = p_program->p_next )
3159 if( p_program->i_pid == pmt->i_pid )
3162 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
3164 if( p_program->i_number ==
3165 pmt->psi->prg[i_prg]->i_number )
3177 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
3181 /* Delete all ES attached to thoses PMT */
3182 for( i = 2; i < 8192; i++ )
3184 ts_pid_t *pid = &p_sys->pid[i];
3186 if( !pid->b_valid || pid->psi ) continue;
3188 for( j = 0; j < i_pmt_rm; j++ )
3191 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
3193 /* We only remove es that aren't defined by extra pmt */
3194 if( pid->p_owner->prg[i_prg]->i_pid_pmt !=
3195 pmt_rm[j]->i_pid ) continue;
3197 if( p_sys->b_dvb_control && pid->es->id )
3199 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3200 ACCESS_SET_PRIVATE_ID_STATE, i,
3202 p_sys->b_dvb_control = VLC_FALSE;
3205 PIDClean( p_demux->out, pid );
3209 if( !pid->b_valid ) break;
3213 /* Delete PMT pid */
3214 for( i = 0; i < i_pmt_rm; i++ )
3217 if( p_sys->b_dvb_control )
3219 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3220 ACCESS_SET_PRIVATE_ID_STATE,
3221 pmt_rm[i]->i_pid, VLC_FALSE ) )
3222 p_sys->b_dvb_control = VLC_FALSE;
3225 for( i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
3227 const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
3229 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
3232 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
3233 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
3236 if( pmt_rm ) free( pmt_rm );
3239 /* now create programs */
3240 for( p_program = p_pat->p_first_program; p_program != NULL;
3241 p_program = p_program->p_next )
3243 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
3245 if( p_program->i_number != 0 )
3247 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
3248 vlc_bool_t b_add = VLC_TRUE;
3253 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
3255 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
3264 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
3269 PIDInit( pmt, VLC_TRUE, pat->psi );
3270 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
3271 dvbpsi_AttachPMT( p_program->i_number,
3272 (dvbpsi_pmt_callback)PMTCallBack,
3274 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
3275 p_program->i_number;
3276 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
3279 /* Now select PID at access level */
3280 if( p_sys->b_dvb_control )
3282 if( DVBProgramIsSelected( p_demux, p_program->i_number ) )
3284 if( p_sys->i_dvb_program == 0 )
3285 p_sys->i_dvb_program = p_program->i_number;
3287 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
3288 p_sys->b_dvb_control = VLC_FALSE;
3294 pat->psi->i_pat_version = p_pat->i_version;
3296 dvbpsi_DeletePAT( p_pat );