1 /*****************************************************************************
2 * ts.c: Transport Stream input module for VLC.
3 *****************************************************************************
4 * Copyright (C) 2004-2005 VideoLAN (Centrale Réseaux) and its contributors
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
28 #include <stdlib.h> /* malloc(), free() */
32 #include <vlc/input.h>
38 #include "../mux/mpeg/csa.h"
40 /* Include dvbpsi headers */
41 #ifdef HAVE_DVBPSI_DR_H
42 # include <dvbpsi/dvbpsi.h>
43 # include <dvbpsi/demux.h>
44 # include <dvbpsi/descriptor.h>
45 # include <dvbpsi/pat.h>
46 # include <dvbpsi/pmt.h>
47 # include <dvbpsi/sdt.h>
48 # include <dvbpsi/dr.h>
49 # include <dvbpsi/psi.h>
53 # include "descriptor.h"
54 # include "tables/pat.h"
55 # include "tables/pmt.h"
56 # include "tables/sdt.h"
57 # include "descriptors/dr.h"
62 #ifdef _DVBPSI_DR_4D_H_
63 # define TS_USE_DVB_SI 1
64 # ifdef HAVE_DVBPSI_DR_H
65 # include <dvbpsi/eit.h>
67 # include "tables/eit.h"
72 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
77 #define vlc_meta_Add(a,b,c) fprintf(stderr, "FIXME: TS demuxer meta is broken\n" )
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open ( vlc_object_t * );
82 static void Close ( vlc_object_t * );
84 #define PMT_TEXT N_("Extra PMT")
85 #define PMT_LONGTEXT N_( \
86 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
88 #define PID_TEXT N_("Set id of ES to PID")
89 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
90 " handled by VLC to the same value as the PID in" \
91 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
92 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
94 #define TSOUT_TEXT N_("Fast udp streaming")
95 #define TSOUT_LONGTEXT N_( \
96 "Sends TS to specific ip:port by udp (you must know what you are doing).")
98 #define MTUOUT_TEXT N_("MTU for out mode")
99 #define MTUOUT_LONGTEXT N_("MTU for out mode.")
101 #define CSA_TEXT N_("CSA ck")
102 #define CSA_LONGTEXT N_("Control word for the CSA encryption algorithm")
104 #define SILENT_TEXT N_("Silent mode")
105 #define SILENT_LONGTEXT N_("Do not complain on encrypted PES.")
107 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
108 #define CAPMT_SYSID_LONGTEXT N_("Only forward descriptors from this SysID to the CAM.")
110 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
111 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
112 "The decryption routines subtract the TS-header from the value before " \
115 #define TSDUMP_TEXT N_("Filename of dump")
116 #define TSDUMP_LONGTEXT N_("Specify a filename where to dump the TS in.")
118 #define APPEND_TEXT N_("Append")
119 #define APPEND_LONGTEXT N_( \
120 "If the file exists and this option is selected, the existing file " \
121 "will not be overwritten." )
123 #define DUMPSIZE_TEXT N_("Dump buffer size")
124 #define DUMPSIZE_LONGTEXT N_( \
125 "Tweak the buffer size for reading and writing an integer number of packets." \
126 "Specify the size of the buffer here and not the number of packets." )
129 set_description( _("MPEG Transport Stream demuxer") );
130 set_shortname ( "MPEG-TS" );
131 set_category( CAT_INPUT );
132 set_subcategory( SUBCAT_INPUT_DEMUX );
134 add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
135 add_bool( "ts-es-id-pid", 1, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
136 add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
137 add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
138 MTUOUT_LONGTEXT, VLC_TRUE );
139 add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
140 add_integer( "ts-csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, VLC_TRUE );
141 add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );
143 add_file( "ts-dump-file", NULL, NULL, TSDUMP_TEXT, TSDUMP_LONGTEXT, VLC_FALSE );
144 add_bool( "ts-dump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT, VLC_FALSE );
145 add_integer( "ts-dump-size", 16384, NULL, DUMPSIZE_TEXT,
146 DUMPSIZE_LONGTEXT, VLC_TRUE );
148 set_capability( "demux2", 10 );
149 set_callbacks( Open, Close );
150 add_shortcut( "ts" );
153 /*****************************************************************************
155 *****************************************************************************/
159 uint8_t i_objectTypeIndication;
160 uint8_t i_streamType;
161 vlc_bool_t b_upStream;
162 uint32_t i_bufferSizeDB;
163 uint32_t i_maxBitrate;
164 uint32_t i_avgBitrate;
166 int i_decoder_specific_info_len;
167 uint8_t *p_decoder_specific_info;
169 } decoder_config_descriptor_t;
173 vlc_bool_t b_useAccessUnitStartFlag;
174 vlc_bool_t b_useAccessUnitEndFlag;
175 vlc_bool_t b_useRandomAccessPointFlag;
176 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
177 vlc_bool_t b_usePaddingFlag;
178 vlc_bool_t b_useTimeStampsFlags;
179 vlc_bool_t b_useIdleFlag;
180 vlc_bool_t b_durationFlag;
181 uint32_t i_timeStampResolution;
182 uint32_t i_OCRResolution;
183 uint8_t i_timeStampLength;
186 uint8_t i_instantBitrateLength;
187 uint8_t i_degradationPriorityLength;
188 uint8_t i_AU_seqNumLength;
189 uint8_t i_packetSeqNumLength;
191 uint32_t i_timeScale;
192 uint16_t i_accessUnitDuration;
193 uint16_t i_compositionUnitDuration;
195 uint64_t i_startDecodingTimeStamp;
196 uint64_t i_startCompositionTimeStamp;
198 } sl_config_descriptor_t;
205 vlc_bool_t b_streamDependenceFlag;
206 vlc_bool_t b_OCRStreamFlag;
207 uint8_t i_streamPriority;
211 uint16_t i_dependOn_es_id;
212 uint16_t i_OCR_es_id;
214 decoder_config_descriptor_t dec_descr;
215 sl_config_descriptor_t sl_descr;
217 } es_mpeg4_descriptor_t;
221 uint8_t i_iod_label, i_iod_label_scope;
227 uint8_t i_ODProfileLevelIndication;
228 uint8_t i_sceneProfileLevelIndication;
229 uint8_t i_audioProfileLevelIndication;
230 uint8_t i_visualProfileLevelIndication;
231 uint8_t i_graphicsProfileLevelIndication;
233 es_mpeg4_descriptor_t es_descr[255];
239 dvbpsi_handle handle;
245 /* IOD stuff (mpeg4) */
246 iod_descriptor_t *iod;
252 /* for special PAT/SDT case */
253 dvbpsi_handle handle; /* PAT/SDT/EIT */
272 es_mpeg4_descriptor_t *p_mpeg4desc;
283 int i_cc; /* countinuity counter */
285 /* PSI owner (ie PMT -> PAT, ES -> PMT */
293 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
301 /* TS packet size (188, 192, 204) */
304 /* how many TS packet we read at once */
315 vlc_bool_t b_es_id_pid;
320 vlc_bool_t b_udp_out;
321 int fd; /* udp socket */
324 vlc_bool_t b_dvb_control;
326 vlc_list_t *p_programs_list;
329 char *psz_file; /* file to dump data in */
330 FILE *p_file; /* filehandle */
331 uint64_t i_write; /* bytes written */
332 vlc_bool_t b_file_out; /* dump mode enabled */
338 static int Demux ( demux_t *p_demux );
339 static int DemuxFile( demux_t *p_demux );
340 static int Control( demux_t *p_demux, int i_query, va_list args );
342 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
343 static void PIDClean( es_out_t *out, ts_pid_t *pid );
344 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
346 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
347 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
349 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
350 uint8_t i_table_id, uint16_t i_extension );
353 static inline int PIDGet( block_t *p )
355 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
358 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
360 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
362 static iod_descriptor_t *IODNew( int , uint8_t * );
363 static void IODFree( iod_descriptor_t * );
365 #define TS_PACKET_SIZE_188 188
366 #define TS_PACKET_SIZE_192 192
367 #define TS_PACKET_SIZE_204 204
368 #define TS_PACKET_SIZE_MAX 204
369 #define TS_TOPFIELD_HEADER 1320
371 /*****************************************************************************
373 *****************************************************************************/
374 static int Open( vlc_object_t *p_this )
376 demux_t *p_demux = (demux_t*)p_this;
380 int i_sync, i_peek, i;
386 vlc_bool_t b_topfield = VLC_FALSE;
390 if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
391 TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
393 if( p_peek[0] == 'T' && p_peek[1] == 'F' &&
394 p_peek[2] == 'r' && p_peek[3] == 'c' )
396 b_topfield = VLC_TRUE;
397 msg_Dbg( p_demux, "this is a topfield file" );
400 /* Search first sync byte */
401 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
403 if( p_peek[i_sync] == 0x47 ) break;
405 if( i_sync >= TS_PACKET_SIZE_MAX && !b_topfield )
407 if( strcmp( p_demux->psz_demux, "ts" ) ) return VLC_EGENERIC;
408 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
413 /* Read the entire Topfield header */
414 i_peek = TS_TOPFIELD_HEADER;
418 /* Check next 3 sync bytes */
419 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
422 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
424 msg_Err( p_demux, "cannot peek" );
427 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
428 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
429 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
431 i_packet_size = TS_PACKET_SIZE_188;
433 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
434 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
435 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
437 i_packet_size = TS_PACKET_SIZE_192;
439 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
440 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
441 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
443 i_packet_size = TS_PACKET_SIZE_204;
445 else if( !strcmp( p_demux->psz_demux, "ts" ) )
447 i_packet_size = TS_PACKET_SIZE_188;
449 else if( b_topfield )
451 i_packet_size = TS_PACKET_SIZE_188;
453 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
454 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
455 * but after the filename the offsets seem to be incorrect. - DJ */
456 int i_duration, i_name;
457 char *psz_name = malloc(25);
458 char *psz_event_name;
459 char *psz_event_text = malloc(130);
460 char *psz_ext_text = malloc(1025);
462 // 2 bytes version Uimsbf (4,5)
463 // 2 bytes reserved (6,7)
464 // 2 bytes duration in minutes Uimsbf (8,9(
465 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
466 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
467 // 2 bytes service number in channel list (10, 11)
468 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
469 // 4 bytes of reserved + tuner info (14,15,16,17)
470 // 2 bytes of Service ID Bslbf (18,19)
471 // 2 bytes of PMT PID Uimsbf (20,21)
472 // 2 bytes of PCR PID Uimsbf (22,23)
473 // 2 bytes of Video PID Uimsbf (24,25)
474 // 2 bytes of Audio PID Uimsbf (26,27)
475 // 24 bytes filename Bslbf
476 memcpy( psz_name, &p_peek[28], 24 );
478 msg_Dbg( p_demux, "recordingname=%s", psz_name );
479 // 1 byte of sat index Uimsbf (52)
480 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
481 // 4 bytes of freq. Uimsbf (56,57,58,59)
482 // 2 bytes of symbol rate Uimsbf (60,61)
483 // 2 bytes of TS stream ID Uimsbf (62,63)
486 // 2 bytes duration Uimsbf (70,71)
487 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
488 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
489 // 4 bytes EventID Uimsbf (72-75)
490 // 8 bytes of Start and End time info (76-83)
491 // 1 byte reserved (84)
492 // 1 byte event name length Uimsbf (89)
493 i_name = (int)(p_peek[89]&~0x81);
494 msg_Dbg( p_demux, "event name length = %d", i_name);
495 psz_event_name = malloc( i_name+1 );
496 // 1 byte parental rating (90)
497 // 129 bytes of event text
498 memcpy( psz_event_name, &p_peek[91], i_name );
499 psz_event_name[i_name] = '\0';
500 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
501 psz_event_text[129-i_name] = '\0';
502 msg_Dbg( p_demux, "event name=%s", psz_event_name );
503 msg_Dbg( p_demux, "event text=%s", psz_event_text );
504 // 12 bytes reserved (220)
506 // 2 bytes Event Text Length Uimsbf
507 // 4 bytes EventID Uimsbf
508 // FIXME We just have 613 bytes. not enough for this entire text
509 // 1024 bytes Extended Event Text Bslbf
510 memcpy( psz_ext_text, p_peek+372, 1024 );
511 psz_ext_text[1024] = '\0';
512 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
513 // 52 bytes reserved Bslbf
518 msg_Warn( p_demux, "TS module discarded (lost sync)" );
522 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
523 memset( p_sys, 0, sizeof( demux_sys_t ) );
524 p_sys->i_packet_size = i_packet_size;
526 /* Fill dump mode fields */
528 p_sys->p_file = NULL;
529 p_sys->b_file_out = VLC_FALSE;
530 p_sys->psz_file = var_CreateGetString( p_demux, "ts-dump-file" );
531 if( *p_sys->psz_file != '\0' )
533 p_sys->b_file_out = VLC_TRUE;
535 var_Create( p_demux, "ts-dump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
536 var_Get( p_demux, "ts-dump-append", &val );
537 b_append = val.b_bool;
543 if( !strcmp( p_sys->psz_file, "-" ) )
545 msg_Info( p_demux, "dumping raw stream to standard output" );
546 p_sys->p_file = stdout;
548 else if( ( p_sys->p_file = utf8_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
550 msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
551 p_sys->b_file_out = VLC_FALSE;
554 if( p_sys->b_file_out )
558 /* Determine how many packets to read. */
559 var_Create( p_demux, "ts-dump-size",
560 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
561 var_Get( p_demux, "ts-dump-size", &bufsize );
562 p_sys->i_ts_read = (int) (bufsize.i_int / p_sys->i_packet_size);
563 if( p_sys->i_ts_read <= 0 )
565 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
567 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
568 msg_Info( p_demux, "%s raw stream to file `%s' reading packets %d",
569 b_append ? "appending" : "dumping", p_sys->psz_file,
574 /* Fill p_demux field */
575 if( p_sys->b_file_out )
576 p_demux->pf_demux = DemuxFile;
578 p_demux->pf_demux = Demux;
579 p_demux->pf_control = Control;
581 /* Init p_sys field */
582 p_sys->b_meta = VLC_TRUE;
583 p_sys->b_dvb_control = VLC_TRUE;
584 p_sys->i_dvb_program = 0;
585 for( i = 0; i < 8192; i++ )
587 ts_pid_t *pid = &p_sys->pid[i];
590 pid->b_seen = VLC_FALSE;
591 pid->b_valid = VLC_FALSE;
593 /* PID 8191 is padding */
594 p_sys->pid[8191].b_seen = VLC_TRUE;
595 p_sys->i_packet_size = i_packet_size;
596 p_sys->b_udp_out = VLC_FALSE;
597 p_sys->i_ts_read = 50;
600 /* Init PAT handler */
601 pat = &p_sys->pid[0];
602 PIDInit( pat, VLC_TRUE, NULL );
603 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
608 ts_pid_t *sdt = &p_sys->pid[0x11];
609 ts_pid_t *eit = &p_sys->pid[0x12];
611 PIDInit( sdt, VLC_TRUE, NULL );
613 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
615 PIDInit( eit, VLC_TRUE, NULL );
617 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
619 if( p_sys->b_dvb_control )
621 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
622 ACCESS_SET_PRIVATE_ID_STATE, 0x11, VLC_TRUE );
623 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
624 ACCESS_SET_PRIVATE_ID_STATE, 0x12, VLC_TRUE );
634 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
635 var_Get( p_demux, "ts-es-id-pid", &val );
636 p_sys->b_es_id_pid = val.b_bool;
638 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
639 var_Get( p_demux, "ts-out", &val );
640 if( val.psz_string && *val.psz_string && !p_sys->b_file_out )
643 char *psz = strchr( val.psz_string, ':' );
646 p_sys->b_udp_out = VLC_TRUE;
651 i_port = atoi( psz );
653 if( i_port <= 0 ) i_port = 1234;
654 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
656 p_sys->fd = net_ConnectUDP( p_demux, val.psz_string, i_port, 0 );
659 msg_Err( p_demux, "failed to open udp socket, send disabled" );
660 p_sys->b_udp_out = VLC_FALSE;
664 var_Create( p_demux, "ts-out-mtu",
665 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
666 var_Get( p_demux, "ts-out-mtu", &mtu );
667 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
668 if( p_sys->i_ts_read <= 0 )
670 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
672 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
677 free( val.psz_string );
680 /* We handle description of an extra PMT */
681 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
682 var_Get( p_demux, "ts-extra-pmt", &val );
683 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
685 char *psz = val.psz_string;
686 int i_pid = strtol( psz, &psz, 0 );
688 if( i_pid >= 2 && i_pid < 8192 )
690 ts_pid_t *pmt = &p_sys->pid[i_pid];
692 msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
693 PIDInit( pmt, VLC_TRUE, NULL );
695 pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
696 /* FIXME we should also ask for a number */
697 pmt->psi->prg[0]->handle =
698 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
700 pmt->psi->prg[0]->i_number = 0; /* special one */
702 psz = strchr( psz, '=' ) + 1; /* can't failed */
705 char *psz_next = strchr( psz, ',' );
706 int i_pid, i_stream_type;
713 i_pid = strtol( psz, &psz, 0 );
716 i_stream_type = strtol( psz + 1, &psz, 0 );
717 if( i_pid >= 2 && i_pid < 8192 &&
718 !p_sys->pid[i_pid].b_valid )
720 ts_pid_t *pid = &p_sys->pid[i_pid];
722 PIDInit( pid, VLC_FALSE, pmt->psi);
723 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
725 pmt->psi->prg[0]->i_pid_pcr = i_pid;
727 PIDFillFormat( pid, i_stream_type);
728 if( pid->es->fmt.i_cat != UNKNOWN_ES )
730 if( p_sys->b_es_id_pid )
732 pid->es->fmt.i_id = i_pid;
734 msg_Dbg( p_demux, " * es pid=%d type=%d "
735 "fcc=%4.4s", i_pid, i_stream_type,
736 (char*)&pid->es->fmt.i_codec );
737 pid->es->id = es_out_Add( p_demux->out,
748 free( val.psz_string );
751 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
752 var_Get( p_demux, "ts-csa-ck", &val );
753 if( val.psz_string && *val.psz_string )
755 char *psz = val.psz_string;
756 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
760 if( strlen( psz ) != 16 )
762 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
767 uint64_t i_ck = strtoull( psz, NULL, 16 );
769 uint64_t i_ck = strtoll( psz, NULL, 16 );
773 for( i = 0; i < 8; i++ )
775 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
777 #ifndef TS_NO_CSA_CK_MSG
778 msg_Dbg( p_demux, "using CSA scrambling with "
779 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
780 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
782 p_sys->csa = csa_New();
788 csa_SetCW( p_sys->csa, ck, ck );
790 var_Create( p_demux, "ts-csa-pkt", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
791 var_Get( p_demux, "ts-csa-pkt", &pkt_val );
792 if( pkt_val.i_int < 4 || pkt_val.i_int > 188 )
794 msg_Err( p_demux, "wrong packet size %d specified.", pkt_val.i_int );
795 msg_Warn( p_demux, "using default packet size of 188 bytes" );
796 p_sys->i_csa_pkt_size = 188;
798 else p_sys->i_csa_pkt_size = pkt_val.i_int;
799 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
805 free( val.psz_string );
808 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
809 var_Get( p_demux, "ts-silent", &val );
810 p_sys->b_silent = val.b_bool;
815 /*****************************************************************************
817 *****************************************************************************/
818 static void Close( vlc_object_t *p_this )
820 demux_t *p_demux = (demux_t*)p_this;
821 demux_sys_t *p_sys = p_demux->p_sys;
825 msg_Dbg( p_demux, "pid list:" );
826 for( i = 0; i < 8192; i++ )
828 ts_pid_t *pid = &p_sys->pid[i];
830 if( pid->b_valid && pid->psi )
835 dvbpsi_DetachPAT( pid->psi->handle );
843 dvbpsi_DetachDemux( pid->psi->handle );
847 PIDClean( p_demux->out, pid );
851 else if( pid->b_valid && pid->es )
853 PIDClean( p_demux->out, pid );
858 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
861 if( p_sys->b_dvb_control && pid->i_pid > 0 )
864 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
869 if( p_sys->b_udp_out )
871 net_Close( p_sys->fd );
872 free( p_sys->buffer );
876 csa_Delete( p_sys->csa );
879 if( p_sys->i_pmt ) free( p_sys->pmt );
881 if ( p_sys->p_programs_list )
884 val.p_list = p_sys->p_programs_list;
885 var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
888 /* If in dump mode, then close the file */
889 if( p_sys->b_file_out )
891 msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
892 p_sys->i_write / 1024 );
894 if( p_sys->p_file != stdout )
896 fclose( p_sys->p_file );
897 p_sys->p_file = NULL;
900 free( p_sys->buffer );
903 free( p_sys->psz_file );
904 p_sys->psz_file = NULL;
909 /*****************************************************************************
911 *****************************************************************************/
912 static int DemuxFile( demux_t *p_demux )
914 demux_sys_t *p_sys = p_demux->p_sys;
915 uint8_t *p_buffer = p_sys->buffer; /* Put first on sync byte */
919 int i_bufsize = p_sys->i_packet_size * p_sys->i_ts_read;
921 i_data = stream_Read( p_demux->s, p_sys->buffer, i_bufsize );
922 if( (i_data <= 0) && (i_data < p_sys->i_packet_size) )
924 msg_Dbg( p_demux, "error reading malformed packets" );
928 /* Test continuity counter */
929 while( i_pos < i_data )
931 ts_pid_t *p_pid; /* point to a PID structure */
932 vlc_bool_t b_payload; /* indicates a packet with payload */
933 vlc_bool_t b_adaptation; /* adaptation field */
934 int i_cc = 0; /* continuity counter */
936 if( p_sys->buffer[i_pos] != 0x47 )
938 msg_Warn( p_demux, "lost sync" );
939 while( !p_demux->b_die && (i_pos < i_data) )
942 if( p_sys->buffer[i_pos] == 0x47 )
945 if( !p_demux->b_die )
946 msg_Warn( p_demux, "sync found" );
949 /* continuous when (one of this):
951 * diff == 0 and payload == 0
952 * diff == 0 and duplicate packet (playload != 0) <- should we
955 i_cc = p_buffer[i_pos+3]&0x0f;
956 b_payload = p_buffer[i_pos+3]&0x10;
957 b_adaptation = p_buffer[i_pos+3]&0x20;
960 p_pid = &p_sys->pid[ ((p_buffer[i_pos+1]&0x1f)<<8)|p_buffer[i_pos+2] ];
962 /* Detect discontinuity indicator in adaptation field */
965 if( p_buffer[i_pos+5]&0x80 )
966 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", p_pid->i_pid );
967 if( p_buffer[i_pos+5]&0x40 )
968 msg_Warn( p_demux, "random access indicator (pid=%d) ", p_pid->i_pid );
971 i_diff = ( i_cc - p_pid->i_cc )&0x0f;
972 if( b_payload && i_diff == 1 )
978 if( p_pid->i_cc == 0xff )
980 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
981 p_pid->i_pid, i_cc );
984 else if( i_diff != 0 )
986 /* FIXME what to do when discontinuity_indicator is set ? */
987 msg_Warn( p_demux, "transport error detected 0x%x instead of 0x%x",
988 i_cc, ( p_pid->i_cc + 1 )&0x0f );
991 /* Mark transport error in the TS packet. */
992 p_buffer[i_pos+1] |= 0x80;
996 /* Test if user wants to decrypt it first */
998 csa_Decrypt( p_demux->p_sys->csa, &p_buffer[i_pos], p_demux->p_sys->i_csa_pkt_size );
1000 i_pos += p_sys->i_packet_size;
1004 i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
1007 msg_Err( p_demux, "failed to write data" );
1011 msg_Dbg( p_demux, "dumped %d bytes", i_data );
1014 p_sys->i_write += i_data;
1018 /*****************************************************************************
1020 *****************************************************************************/
1021 static int Demux( demux_t *p_demux )
1023 demux_sys_t *p_sys = p_demux->p_sys;
1026 /* We read at most 100 TS packet or until a frame is completed */
1027 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1029 vlc_bool_t b_frame = VLC_FALSE;
1033 /* Get a new TS packet */
1034 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1036 msg_Dbg( p_demux, "eof ?" );
1040 /* Check sync byte and re-sync if needed */
1041 if( p_pkt->p_buffer[0] != 0x47 )
1043 msg_Warn( p_demux, "lost synchro" );
1044 block_Release( p_pkt );
1046 while( !p_demux->b_die )
1049 int i_peek, i_skip = 0;
1051 i_peek = stream_Peek( p_demux->s, &p_peek,
1052 p_sys->i_packet_size * 10 );
1053 if( i_peek < p_sys->i_packet_size + 1 )
1055 msg_Dbg( p_demux, "eof ?" );
1059 while( i_skip < i_peek - p_sys->i_packet_size )
1061 if( p_peek[i_skip] == 0x47 &&
1062 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
1069 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
1070 stream_Read( p_demux->s, NULL, i_skip );
1072 if( i_skip < i_peek - p_sys->i_packet_size )
1078 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1080 msg_Dbg( p_demux, "eof ?" );
1085 if( p_sys->b_udp_out )
1087 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
1088 p_pkt->p_buffer, p_sys->i_packet_size );
1091 /* Parse the TS packet */
1092 p_pid = &p_sys->pid[PIDGet( p_pkt )];
1094 if( p_pid->b_valid )
1098 if( p_pid->i_pid == 0 || p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 )
1100 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1105 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1107 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1111 block_Release( p_pkt );
1113 else if( !p_sys->b_udp_out )
1115 b_frame = GatherPES( p_demux, p_pid, p_pkt );
1119 PCRHandle( p_demux, p_pid, p_pkt );
1120 block_Release( p_pkt );
1125 if( !p_pid->b_seen )
1127 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1129 /* We have to handle PCR if present */
1130 PCRHandle( p_demux, p_pid, p_pkt );
1131 block_Release( p_pkt );
1133 p_pid->b_seen = VLC_TRUE;
1141 if( p_sys->b_udp_out )
1143 /* Send the complete block */
1144 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
1145 p_sys->i_ts_read * p_sys->i_packet_size );
1151 /*****************************************************************************
1153 *****************************************************************************/
1154 static int Control( demux_t *p_demux, int i_query, va_list args )
1156 demux_sys_t *p_sys = p_demux->p_sys;
1162 if( p_sys->b_file_out )
1163 return demux2_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
1167 case DEMUX_GET_POSITION:
1168 pf = (double*) va_arg( args, double* );
1169 i64 = stream_Size( p_demux->s );
1172 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
1179 case DEMUX_SET_POSITION:
1180 f = (double) va_arg( args, double );
1181 i64 = stream_Size( p_demux->s );
1183 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1184 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
1186 return VLC_EGENERIC;
1191 case DEMUX_GET_TIME:
1192 pi64 = (int64_t*)va_arg( args, int64_t * );
1193 if( p_sys->i_time < 0 )
1196 return VLC_EGENERIC;
1198 *pi64 = p_sys->i_time;
1201 case DEMUX_GET_LENGTH:
1202 pi64 = (int64_t*)va_arg( args, int64_t * );
1203 if( p_sys->i_mux_rate > 0 )
1205 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
1210 return VLC_EGENERIC;
1212 case DEMUX_GET_TIME:
1213 case DEMUX_GET_LENGTH:
1214 pi64 = (int64_t*)va_arg( args, int64_t * );
1218 case DEMUX_SET_GROUP:
1220 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
1221 ts_prg_psi_t *p_prg = NULL;
1224 i_int = (int)va_arg( args, int );
1225 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1226 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1228 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
1233 /* Search pmt to be unselected */
1234 for( i = 0; i < p_sys->i_pmt; i++ )
1236 ts_pid_t *pmt = p_sys->pmt[i];
1239 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1241 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
1243 i_pmt_pid = p_sys->pmt[i]->i_pid;
1247 if( i_pmt_pid > 0 ) break;
1252 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1253 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1256 for( i = 2; i < 8192; i++ )
1258 ts_pid_t *pid = &p_sys->pid[i];
1261 if( !pid->b_valid || pid->psi ) continue;
1263 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1265 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1267 /* We only remove es that aren't defined by extra pmt */
1268 stream_Control( p_demux->s,
1269 STREAM_CONTROL_ACCESS,
1270 ACCESS_SET_PRIVATE_ID_STATE,
1278 /* select new program */
1279 p_sys->i_dvb_program = i_int;
1281 for( i = 0; i < p_sys->i_pmt; i++ )
1283 ts_pid_t *pmt = p_sys->pmt[i];
1286 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1288 if( pmt->psi->prg[i_prg]->i_number == i_int )
1290 i_pmt_pid = p_sys->pmt[i]->i_pid;
1291 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1295 if( i_pmt_pid > 0 ) break;
1299 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1300 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1302 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1303 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
1306 for( i = 2; i < 8192; i++ )
1308 ts_pid_t *pid = &p_sys->pid[i];
1311 if( !pid->b_valid || pid->psi ) continue;
1313 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1315 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1317 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
1319 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
1321 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
1323 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
1326 stream_Control( p_demux->s,
1327 STREAM_CONTROL_ACCESS,
1328 ACCESS_SET_PRIVATE_ID_STATE,
1338 p_sys->i_dvb_program = -1;
1339 p_sys->p_programs_list = p_list;
1345 case DEMUX_SET_TIME:
1347 return VLC_EGENERIC;
1351 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
1353 vlc_bool_t b_old_valid = pid->b_valid;
1355 pid->b_valid = VLC_TRUE;
1357 pid->p_owner = p_owner;
1358 pid->i_owner_number = 0;
1360 pid->extra_es = NULL;
1361 pid->i_extra_es = 0;
1369 pid->psi = malloc( sizeof( ts_psi_t ) );
1370 pid->psi->handle= NULL;
1371 pid->psi->i_prg = 0;
1372 pid->psi->prg = NULL;
1374 pid->psi->i_pat_version = -1;
1375 pid->psi->i_sdt_version = -1;
1378 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1380 prg->i_version = -1;
1382 prg->i_pid_pcr = -1;
1383 prg->i_pid_pmt = -1;
1387 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1393 pid->es = malloc( sizeof( ts_es_t ) );
1395 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1397 pid->es->p_pes = NULL;
1398 pid->es->i_pes_size= 0;
1399 pid->es->i_pes_gathered= 0;
1400 pid->es->pp_last = &pid->es->p_pes;
1401 pid->es->p_mpeg4desc = NULL;
1402 pid->es->b_gather = VLC_FALSE;
1406 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1412 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1413 for( i = 0; i < pid->psi->i_prg; i++ )
1415 if( pid->psi->prg[i]->iod )
1416 IODFree( pid->psi->prg[i]->iod );
1417 if( pid->psi->prg[i]->handle )
1418 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1419 free( pid->psi->prg[i] );
1421 if( pid->psi->prg ) free( pid->psi->prg );
1429 es_out_Del( out, pid->es->id );
1431 if( pid->es->p_pes )
1432 block_ChainRelease( pid->es->p_pes );
1434 es_format_Clean( &pid->es->fmt );
1438 for( i = 0; i < pid->i_extra_es; i++ )
1440 if( pid->extra_es[i]->id )
1441 es_out_Del( out, pid->extra_es[i]->id );
1443 if( pid->extra_es[i]->p_pes )
1444 block_ChainRelease( pid->extra_es[i]->p_pes );
1446 es_format_Clean( &pid->extra_es[i]->fmt );
1448 free( pid->extra_es[i] );
1450 if( pid->i_extra_es ) free( pid->extra_es );
1453 pid->b_valid = VLC_FALSE;
1456 /****************************************************************************
1458 ****************************************************************************/
1459 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1461 block_t *p_pes = pid->es->p_pes;
1467 mtime_t i_length = 0;
1470 /* remove the pes from pid */
1471 pid->es->p_pes = NULL;
1472 pid->es->i_pes_size= 0;
1473 pid->es->i_pes_gathered= 0;
1474 pid->es->pp_last = &pid->es->p_pes;
1476 /* FIXME find real max size */
1477 i_max = block_ChainExtract( p_pes, header, 30 );
1480 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1482 if( !p_demux->p_sys->b_silent )
1483 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1484 header[0], header[1],header[2],header[3], pid->i_pid );
1485 block_ChainRelease( p_pes );
1489 /* TODO check size */
1492 case 0xBC: /* Program stream map */
1493 case 0xBE: /* Padding */
1494 case 0xBF: /* Private stream 2 */
1495 case 0xB0: /* ECM */
1496 case 0xB1: /* EMM */
1497 case 0xFF: /* Program stream directory */
1498 case 0xF2: /* DSMCC stream */
1499 case 0xF8: /* ITU-T H.222.1 type E stream */
1503 if( ( header[6]&0xC0 ) == 0x80 )
1506 i_skip = header[8] + 9;
1508 if( header[7]&0x80 ) /* has pts */
1510 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1511 (mtime_t)(header[10] << 22)|
1512 ((mtime_t)(header[11]&0xfe) << 14)|
1513 (mtime_t)(header[12] << 7)|
1514 (mtime_t)(header[13] >> 1);
1516 if( header[7]&0x40 ) /* has dts */
1518 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1519 (mtime_t)(header[15] << 22)|
1520 ((mtime_t)(header[16]&0xfe) << 14)|
1521 (mtime_t)(header[17] << 7)|
1522 (mtime_t)(header[18] >> 1);
1529 while( i_skip < 23 && header[i_skip] == 0xff )
1535 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1536 block_ChainRelease( p_pes );
1539 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1544 if( header[i_skip]&0x20 )
1546 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1547 (mtime_t)(header[i_skip+1] << 22)|
1548 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1549 (mtime_t)(header[i_skip+3] << 7)|
1550 (mtime_t)(header[i_skip+4] >> 1);
1552 if( header[i_skip]&0x10 ) /* has dts */
1554 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1555 (mtime_t)(header[i_skip+6] << 22)|
1556 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1557 (mtime_t)(header[i_skip+8] << 7)|
1558 (mtime_t)(header[i_skip+9] >> 1);
1574 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1575 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1579 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1580 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1581 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1585 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1586 pid->es->p_mpeg4desc )
1588 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1590 if( dcd->i_decoder_specific_info_len > 2 &&
1591 dcd->p_decoder_specific_info[0] == 0x10 &&
1592 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1594 /* display length */
1595 if( p_pes->i_buffer + 2 <= i_skip )
1597 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1602 if( p_pes->i_buffer + 2 <= i_skip )
1604 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1611 while( p_pes && i_skip > 0 )
1613 if( p_pes->i_buffer <= i_skip )
1615 block_t *p_next = p_pes->p_next;
1617 i_skip -= p_pes->i_buffer;
1618 block_Release( p_pes );
1623 p_pes->i_buffer -= i_skip;
1624 p_pes->p_buffer += i_skip;
1629 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1630 if( i_pts >= 0 && i_dts < 0 )
1640 p_pes->i_dts = i_dts * 100 / 9;
1644 p_pes->i_pts = i_pts * 100 / 9;
1646 p_pes->i_length = i_length * 100 / 9;
1648 p_block = block_ChainGather( p_pes );
1649 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1651 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1653 p_block->i_buffer = i_pes_size;
1656 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1657 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1660 for( i = 0; i < pid->i_extra_es; i++ )
1662 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1663 block_Duplicate( p_block ) );
1666 es_out_Send( p_demux->out, pid->es->id, p_block );
1670 msg_Warn( p_demux, "empty pes" );
1674 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1676 demux_sys_t *p_sys = p_demux->p_sys;
1677 const uint8_t *p = p_bk->p_buffer;
1679 if( ( p[3]&0x20 ) && /* adaptation */
1684 mtime_t i_pcr; /* 33 bits */
1686 i_pcr = ( (mtime_t)p[6] << 25 ) |
1687 ( (mtime_t)p[7] << 17 ) |
1688 ( (mtime_t)p[8] << 9 ) |
1689 ( (mtime_t)p[9] << 1 ) |
1690 ( (mtime_t)p[10] >> 7 );
1692 /* Search program and set the PCR */
1693 for( i = 0; i < p_sys->i_pmt; i++ )
1696 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1698 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1700 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1701 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1702 (int64_t)(i_pcr * 100 / 9) );
1709 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1711 const uint8_t *p = p_bk->p_buffer;
1712 const vlc_bool_t b_unit_start = p[1]&0x40;
1713 const vlc_bool_t b_adaptation = p[3]&0x20;
1714 const vlc_bool_t b_payload = p[3]&0x10;
1715 const int i_cc = p[3]&0x0f; /* continuity counter */
1716 vlc_bool_t b_discontinuity = VLC_FALSE;/* discontinuity */
1718 /* transport_scrambling_control is ignored */
1720 vlc_bool_t i_ret = VLC_FALSE;
1724 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1725 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1729 /* For now, ignore additional error correction
1730 * TODO: handle Reed-Solomon 204,188 error correction */
1731 p_bk->i_buffer = TS_PACKET_SIZE_188;
1735 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1737 if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
1738 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1741 if( p_demux->p_sys->csa )
1743 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
1748 /* We don't have any adaptation_field, so payload starts
1749 * immediately after the 4 byte TS header */
1754 /* p[4] is adaptation_field_length minus one */
1758 /* discontinuity indicator found in stream */
1759 b_discontinuity = (p[5]&0x80) ? VLC_TRUE : VLC_FALSE;
1760 if( b_discontinuity && pid->es->p_pes )
1762 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
1764 /* pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
1768 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
1773 /* Test continuity counter */
1774 /* continuous when (one of this):
1776 * diff == 0 and payload == 0
1777 * diff == 0 and duplicate packet (playload != 0) <- should we
1778 * test the content ?
1780 i_diff = ( i_cc - pid->i_cc )&0x0f;
1781 if( b_payload && i_diff == 1 )
1787 if( pid->i_cc == 0xff )
1789 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1793 else if( i_diff != 0 && !b_discontinuity )
1795 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
1796 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
1799 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1801 /* Small video artifacts are usually better then
1802 * dropping full frames */
1803 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1808 PCRHandle( p_demux, pid, p_bk );
1810 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1812 block_Release( p_bk );
1816 /* We have to gather it */
1817 p_bk->p_buffer += i_skip;
1818 p_bk->i_buffer -= i_skip;
1822 if( pid->es->p_pes )
1824 ParsePES( p_demux, pid );
1828 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1829 if( p_bk->i_buffer > 6 )
1831 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1832 if( pid->es->i_pes_size > 0 )
1834 pid->es->i_pes_size += 6;
1837 pid->es->i_pes_gathered += p_bk->i_buffer;
1838 if( pid->es->i_pes_size > 0 &&
1839 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1841 ParsePES( p_demux, pid );
1847 if( pid->es->p_pes == NULL )
1849 /* msg_Dbg( p_demux, "broken packet" ); */
1850 block_Release( p_bk );
1854 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1855 pid->es->i_pes_gathered += p_bk->i_buffer;
1856 if( pid->es->i_pes_size > 0 &&
1857 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1859 ParsePES( p_demux, pid );
1868 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1870 es_format_t *fmt = &pid->es->fmt;
1872 switch( i_stream_type )
1874 case 0x01: /* MPEG-1 video */
1875 case 0x02: /* MPEG-2 video */
1876 case 0x80: /* MPEG-2 MOTO video */
1877 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1879 case 0x03: /* MPEG-1 audio */
1880 case 0x04: /* MPEG-2 audio */
1881 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1883 case 0x11: /* MPEG4 (audio) */
1884 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1885 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1887 case 0x10: /* MPEG4 (video) */
1888 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1889 pid->es->b_gather = VLC_TRUE;
1891 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1892 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1895 case 0x81: /* A52 (audio) */
1896 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1898 case 0x82: /* DVD_SPU (sub) */
1899 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1901 case 0x83: /* LPCM (audio) */
1902 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1904 case 0x84: /* SDDS (audio) */
1905 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1907 case 0x85: /* DTS (audio) */
1908 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1911 case 0x91: /* A52 vls (audio) */
1912 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1914 case 0x92: /* DVD_SPU vls (sub) */
1915 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1918 case 0x94: /* SDDS (audio) */
1919 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1922 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1923 es_format_Init( fmt, UNKNOWN_ES, 0 );
1924 pid->es->b_gather = VLC_TRUE;
1927 case 0x06: /* PES_PRIVATE (fixed later) */
1928 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1930 es_format_Init( fmt, UNKNOWN_ES, 0 );
1934 /* PES packets usually contain truncated frames */
1935 fmt->b_packetized = VLC_FALSE;
1937 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1940 /*****************************************************************************
1941 * MP4 specific functions (IOD parser)
1942 *****************************************************************************/
1943 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1946 unsigned int i_len = 0;
1952 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1954 } while( i_b&0x80 );
1958 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1962 const int i_b = **pp_data;
1969 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1971 const int i1 = IODGetByte( pi_data, pp_data );
1972 const int i2 = IODGetByte( pi_data, pp_data );
1973 return( ( i1 << 8 ) | i2 );
1975 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1977 const int i1 = IODGetByte( pi_data, pp_data );
1978 const int i2 = IODGetByte( pi_data, pp_data );
1979 const int i3 = IODGetByte( pi_data, pp_data );
1981 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1984 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1986 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1987 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1988 return( ( i1 << 16 ) | i2 );
1991 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
1996 i_url_len = IODGetByte( pi_data, pp_data );
1997 url = malloc( i_url_len + 1 );
1998 for( i = 0; i < i_url_len; i++ )
2000 url[i] = IODGetByte( pi_data, pp_data );
2002 url[i_url_len] = '\0';
2006 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2008 iod_descriptor_t *p_iod;
2011 uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
2015 p_iod = malloc( sizeof( iod_descriptor_t ) );
2016 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
2019 fprintf( stderr, "\n************ IOD ************" );
2021 for( i = 0; i < 255; i++ )
2023 p_iod->es_descr[i].b_ok = 0;
2032 byte1 = IODGetByte( &i_data, &p_data );
2033 byte2 = IODGetByte( &i_data, &p_data );
2034 byte3 = IODGetByte( &i_data, &p_data );
2035 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2037 p_iod->i_iod_label_scope = 0x11;
2038 p_iod->i_iod_label = byte1;
2041 else //correct implementation of the IOD_descriptor
2043 p_iod->i_iod_label_scope = byte1;
2044 p_iod->i_iod_label = byte2;
2048 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
2049 fprintf( stderr, "\n* ===========" );
2050 fprintf( stderr, "\n* tag:0x%x", i_iod_tag );
2052 if( i_iod_tag != 0x02 )
2055 fprintf( stderr, "\n ERR: tag %02x != 0x02", i_iod_tag );
2060 i_iod_length = IODDescriptorLength( &i_data, &p_data );
2062 fprintf( stderr, "\n* length:%d", i_iod_length );
2064 if( i_iod_length > i_data )
2066 i_iod_length = i_data;
2069 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
2070 i_flags = IODGetByte( &i_data, &p_data );
2071 p_iod->i_od_id |= i_flags >> 6;
2072 b_url = ( i_flags >> 5 )&0x01;
2074 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
2075 fprintf( stderr, "\n* url flag:%d", b_url );
2076 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2080 p_iod->psz_url = IODGetURL( &i_data, &p_data );
2082 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
2083 fprintf( stderr, "\n*****************************\n" );
2089 p_iod->psz_url = NULL;
2092 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
2093 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
2094 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
2095 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
2096 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
2098 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
2099 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
2100 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
2101 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
2102 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
2105 while( i_data > 0 && i_es_index < 255)
2107 int i_tag, i_length;
2109 uint8_t *p_data_sav;
2111 i_tag = IODGetByte( &i_data, &p_data );
2112 i_length = IODDescriptorLength( &i_data, &p_data );
2114 i_data_sav = i_data;
2115 p_data_sav = p_data;
2123 #define es_descr p_iod->es_descr[i_es_index]
2124 int i_decoderConfigDescr_length;
2126 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
2130 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
2131 i_flags = IODGetByte( &i_data, &p_data );
2132 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2133 b_url = ( i_flags >> 6 )&0x01;
2134 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2135 es_descr.i_streamPriority = i_flags & 0x1f;
2137 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
2138 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
2139 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
2141 if( es_descr.b_streamDependenceFlag )
2143 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
2145 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
2151 es_descr.psz_url = IODGetURL( &i_data, &p_data );
2153 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
2158 es_descr.psz_url = NULL;
2161 if( es_descr.b_OCRStreamFlag )
2163 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
2165 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
2169 if( IODGetByte( &i_data, &p_data ) != 0x04 )
2172 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
2177 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2179 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
2181 #define dec_descr es_descr.dec_descr
2182 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
2183 i_flags = IODGetByte( &i_data, &p_data );
2184 dec_descr.i_streamType = i_flags >> 2;
2185 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
2186 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
2187 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
2188 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
2190 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
2191 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
2192 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
2193 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
2194 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
2195 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
2197 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
2200 dec_descr.i_decoder_specific_info_len =
2201 IODDescriptorLength( &i_data, &p_data );
2202 if( dec_descr.i_decoder_specific_info_len > 0 )
2204 dec_descr.p_decoder_specific_info =
2205 malloc( dec_descr.i_decoder_specific_info_len );
2207 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
2209 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
2214 dec_descr.i_decoder_specific_info_len = 0;
2215 dec_descr.p_decoder_specific_info = NULL;
2219 #define sl_descr es_descr.sl_descr
2221 int i_SLConfigDescr_length;
2224 if( IODGetByte( &i_data, &p_data ) != 0x06 )
2227 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
2232 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2234 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
2236 i_predefined = IODGetByte( &i_data, &p_data );
2238 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
2240 switch( i_predefined )
2244 sl_descr.b_useAccessUnitStartFlag = 0;
2245 sl_descr.b_useAccessUnitEndFlag = 0;
2246 sl_descr.b_useRandomAccessPointFlag = 0;
2247 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
2248 sl_descr.b_usePaddingFlag = 0;
2249 sl_descr.b_useTimeStampsFlags = 0;
2250 sl_descr.b_useIdleFlag = 0;
2251 sl_descr.b_durationFlag = 0; // FIXME FIXME
2252 sl_descr.i_timeStampResolution = 1000;
2253 sl_descr.i_OCRResolution = 0; // FIXME FIXME
2254 sl_descr.i_timeStampLength = 32;
2255 sl_descr.i_OCRLength = 0; // FIXME FIXME
2256 sl_descr.i_AU_Length = 0;
2257 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
2258 sl_descr.i_degradationPriorityLength= 0;
2259 sl_descr.i_AU_seqNumLength = 0;
2260 sl_descr.i_packetSeqNumLength = 0;
2261 if( sl_descr.b_durationFlag )
2263 sl_descr.i_timeScale = 0; // FIXME FIXME
2264 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
2265 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
2267 if( !sl_descr.b_useTimeStampsFlags )
2269 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
2270 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
2276 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
2287 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
2292 p_data = p_data_sav + i_length;
2293 i_data = i_data_sav - i_length;
2297 fprintf( stderr, "\n*****************************\n" );
2302 static void IODFree( iod_descriptor_t *p_iod )
2306 if( p_iod->psz_url )
2308 free( p_iod->psz_url );
2309 p_iod->psz_url = NULL;
2314 for( i = 0; i < 255; i++ )
2316 #define es_descr p_iod->es_descr[i]
2319 if( es_descr.psz_url )
2321 free( es_descr.psz_url );
2322 es_descr.psz_url = NULL;
2326 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
2328 free( es_descr.dec_descr.p_decoder_specific_info );
2329 es_descr.dec_descr.p_decoder_specific_info = NULL;
2330 es_descr.dec_descr.i_decoder_specific_info_len = 0;
2340 /****************************************************************************
2341 ****************************************************************************
2342 ** libdvbpsi callbacks
2343 ****************************************************************************
2344 ****************************************************************************/
2345 static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2347 demux_sys_t *p_sys = p_demux->p_sys;
2349 if ( !p_sys->b_dvb_control ) return VLC_FALSE;
2350 if ( (p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL)
2351 || p_sys->i_dvb_program == 0 )
2353 if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
2355 if ( p_sys->p_programs_list != NULL )
2358 for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
2360 if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
2367 #ifdef TS_USE_DVB_SI
2368 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2370 demux_sys_t *p_sys = p_demux->p_sys;
2371 ts_pid_t *sdt = &p_sys->pid[0x11];
2372 dvbpsi_sdt_service_t *p_srv;
2374 msg_Dbg( p_demux, "SDTCallBack called" );
2376 if( sdt->psi->i_sdt_version != -1 &&
2377 ( !p_sdt->b_current_next ||
2378 p_sdt->i_version == sdt->psi->i_sdt_version ) )
2380 dvbpsi_DeleteSDT( p_sdt );
2384 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2386 p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
2387 p_sdt->i_network_id );
2389 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2391 vlc_meta_t *p_meta = vlc_meta_New();
2392 dvbpsi_descriptor_t *p_dr;
2394 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
2395 "running=%d free_ca=%d",
2396 p_srv->i_service_id, p_srv->b_eit_schedule,
2397 p_srv->b_eit_present, p_srv->i_running_status,
2400 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2402 if( p_dr->i_tag == 0x48 )
2404 static const char *psz_type[0x11] = {
2406 "Digital television service",
2407 "Digital radio sound service",
2409 "NVOD reference service",
2410 "NVOD time-shifted service",
2413 "SECAM coded signal",
2416 "NTSC coded signal",
2417 "Data broadcast service",
2418 "Reserved for Common Interface Usage",
2419 "RCS Map (see EN 301 790 [35])",
2420 "RCS FLS (see EN 301 790 [35])",
2423 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2427 memcpy( str1, pD->i_service_provider_name,
2428 pD->i_service_provider_name_length );
2429 str1[pD->i_service_provider_name_length] = '\0';
2430 memcpy( str2, pD->i_service_name, pD->i_service_name_length );
2431 str2[pD->i_service_name_length] = '\0';
2433 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
2434 pD->i_service_type, str1, str2 );
2436 vlc_meta_SetTitle( p_meta, str2 );
2437 vlc_meta_SetPublisher( p_meta, str1 );
2438 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2439 vlc_meta_Add( p_meta, "Type", psz_type[pD->i_service_type] );
2443 if( p_srv->i_running_status == 0x01 )
2444 vlc_meta_Add( p_meta, "Status", "Not running" );
2445 else if( p_srv->i_running_status == 0x02 )
2446 vlc_meta_Add( p_meta, "Status", "Starts in a few seconds" );
2447 else if( p_srv->i_running_status == 0x03 )
2448 vlc_meta_Add( p_meta, "Status", "Pausing" );
2449 else if( p_srv->i_running_status == 0x04 )
2450 vlc_meta_Add( p_meta, "Status", "Running" );
2452 vlc_meta_Add( p_meta, "Status", "Unknown" );
2454 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2455 p_srv->i_service_id, p_meta );
2456 vlc_meta_Delete( p_meta );
2459 sdt->psi->i_sdt_version = p_sdt->i_version;
2460 dvbpsi_DeleteSDT( p_sdt );
2463 static void DecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2465 int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2466 int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2468 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2470 if( mp == 14 || mp == 15 )
2482 static void EITEventFixString( unsigned char *psz )
2485 /* Sometimes the first char isn't a normal char but designed
2486 * caracters encoding, for now lets skip it */
2487 if( psz[0] >= 0x20 )
2489 if( ( i_len = strlen( (char *) psz ) ) > 0 )
2490 memmove( &psz[0], &psz[1], i_len ); /* Copy the \0 too */
2492 static void EITCallBack( demux_t *p_demux, dvbpsi_eit_t *p_eit )
2494 dvbpsi_eit_event_t *p_evt;
2496 vlc_bool_t b_event_active = VLC_FALSE;
2498 msg_Dbg( p_demux, "EITCallBack called" );
2499 if( !p_eit->b_current_next )
2501 dvbpsi_DeleteEIT( p_eit );
2505 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
2506 "ts_id=%d network_id=%d segment_last_section_number=%d "
2508 p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
2509 p_eit->i_ts_id, p_eit->i_network_id,
2510 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2512 p_meta = vlc_meta_New();
2513 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2515 dvbpsi_descriptor_t *p_dr;
2516 char *psz_cat = malloc( strlen("Event")+10 );
2518 char psz_duration[15];
2521 char *psz_extra = strdup("");
2524 sprintf( psz_cat, "Event %d", p_evt->i_event_id );
2525 sprintf( psz_start, "%d%d:%d%d:%d%d",
2526 (int)(p_evt->i_start_time >> 20)&0xf,
2527 (int)(p_evt->i_start_time >> 16)&0xf,
2528 (int)(p_evt->i_start_time >> 12)&0xf,
2529 (int)(p_evt->i_start_time >> 8)&0xf,
2530 (int)(p_evt->i_start_time >> 4)&0xf,
2531 (int)(p_evt->i_start_time )&0xf );
2532 sprintf( psz_duration, "%d%d:%d%d:%d%d",
2533 (p_evt->i_duration >> 20)&0xf, (p_evt->i_duration >> 16)&0xf,
2534 (p_evt->i_duration >> 12)&0xf, (p_evt->i_duration >> 8)&0xf,
2535 (p_evt->i_duration >> 4)&0xf, (p_evt->i_duration )&0xf );
2536 psz_name[0] = psz_text[0] = '\0';
2538 msg_Dbg( p_demux, " * event id=%d start_time:mjd=%d %s duration=%s "
2539 "running=%d free_ca=%d",
2541 (int)(p_evt->i_start_time >> 24),
2542 psz_start, psz_duration,
2543 p_evt->i_running_status, p_evt->b_free_ca );
2545 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2547 if( p_dr->i_tag == 0x4d )
2549 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2553 memcpy( psz_name, pE->i_event_name, pE->i_event_name_length);
2554 psz_name[pE->i_event_name_length] = '\0';
2555 memcpy( psz_text, pE->i_text, pE->i_text_length );
2556 psz_text[pE->i_text_length] = '\0';
2558 EITEventFixString((unsigned char *)&psz_name);
2559 EITEventFixString((unsigned char *)&psz_text);
2560 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
2561 pE->i_iso_639_code, psz_name, psz_text );
2564 else if( p_dr->i_tag == 0x4e )
2566 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
2573 msg_Dbg( p_demux, " - extended event lang=%3.3s",
2574 pE->i_iso_639_code );
2575 for( i = 0; i < pE->i_entry_count; i++ )
2577 memcpy( str1, pE->i_item_description[i],
2578 pE->i_item_description_length[i] );
2579 str1[pE->i_item_description_length[i]] = '\0';
2580 EITEventFixString((unsigned char *)&str1);
2582 memcpy( str2, pE->i_item[i],
2583 pE->i_item_length[i] );
2584 str2[pE->i_item_length[i]] = '\0';
2585 EITEventFixString((unsigned char *)&str2);
2587 msg_Dbg( p_demux, " - desc='%s' item='%s'", str1, str2 );
2588 psz_extra = realloc( psz_extra,
2590 strlen(str1) +strlen(str2) + 1 + 3 );
2591 strcat( psz_extra, str1 );
2592 strcat( psz_extra, "(" );
2593 strcat( psz_extra, str2 );
2594 strcat( psz_extra, ") " );
2597 memcpy( str1, pE->i_text, pE->i_text_length );
2598 str1[pE->i_text_length] = '\0';
2599 EITEventFixString((unsigned char *)&str1);
2601 msg_Dbg( p_demux, " - text='%s'", str1 );
2602 psz_extra = realloc( psz_extra,
2603 strlen(psz_extra) + strlen(str1) + 2 );
2604 strcat( psz_extra, str1 );
2605 strcat( psz_extra, " " );
2610 msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
2614 asprintf( &psz_value, "%s: %s (+%s) %s (%s)",
2618 psz_text, psz_extra );
2619 vlc_meta_Add( p_meta, psz_cat, psz_value );
2622 if( p_evt->i_running_status == 0x04 )
2624 vlc_meta_SetNowPlaying( p_meta, psz_name );
2625 b_event_active = VLC_TRUE;
2632 if( !b_event_active )
2633 vlc_meta_SetNowPlaying( p_meta, "" );
2634 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2635 p_eit->i_service_id, p_meta );
2636 vlc_meta_Delete( p_meta );
2638 dvbpsi_DeleteEIT( p_eit );
2641 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
2642 uint8_t i_table_id, uint16_t i_extension )
2645 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2646 i_table_id, i_table_id, i_extension, i_extension );
2649 if( i_table_id == 0x42 )
2651 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2652 i_table_id, i_table_id, i_extension, i_extension );
2654 dvbpsi_AttachSDT( h, i_table_id, i_extension,
2655 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
2657 else if( i_table_id == 0x4e || /* Current/Following */
2658 ( i_table_id >= 0x50 && i_table_id <= 0x5f ) ) /* Schedule */
2660 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2661 i_table_id, i_table_id, i_extension, i_extension );
2663 dvbpsi_AttachEIT( h, i_table_id, i_extension,
2664 (dvbpsi_eit_callback)EITCallBack, p_demux );
2669 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
2671 demux_sys_t *p_sys = p_demux->p_sys;
2672 dvbpsi_descriptor_t *p_dr;
2673 dvbpsi_pmt_es_t *p_es;
2675 ts_pid_t *pmt = NULL;
2676 ts_prg_psi_t *prg = NULL;
2678 ts_pid_t **pp_clean = NULL;
2681 msg_Dbg( p_demux, "PMTCallBack called" );
2683 /* First find this PMT declared in PAT */
2684 for( i = 0; i < p_sys->i_pmt; i++ )
2687 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2689 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
2690 p_pmt->i_program_number )
2692 pmt = p_sys->pmt[i];
2693 prg = p_sys->pmt[i]->psi->prg[i_prg];
2702 msg_Warn( p_demux, "unreferenced program (broken stream)" );
2703 dvbpsi_DeletePMT(p_pmt);
2707 if( prg->i_version != -1 &&
2708 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
2710 dvbpsi_DeletePMT( p_pmt );
2714 /* Clean this program (remove all es) */
2715 for( i = 0; i < 8192; i++ )
2717 ts_pid_t *pid = &p_sys->pid[i];
2719 if( pid->b_valid && pid->p_owner == pmt->psi &&
2720 pid->i_owner_number == prg->i_number && pid->psi == NULL )
2722 TAB_APPEND( i_clean, pp_clean, pid );
2727 IODFree( prg->iod );
2731 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
2732 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
2733 prg->i_pid_pcr = p_pmt->i_pcr_pid;
2734 prg->i_version = p_pmt->i_version;
2736 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2738 /* Set demux filter */
2739 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2740 ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
2743 else if ( p_sys->b_dvb_control )
2745 msg_Warn( p_demux, "skipping program (not selected)" );
2746 dvbpsi_DeletePMT(p_pmt);
2750 /* Parse descriptor */
2751 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
2753 if( p_dr->i_tag == 0x1d )
2755 /* We have found an IOD descriptor */
2756 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
2758 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
2760 else if( p_dr->i_tag == 0x9 )
2762 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2764 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
2768 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2772 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2774 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
2776 /* Find out if the PID was already declared */
2777 for( i = 0; i < i_clean; i++ )
2779 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
2781 old_pid = pp_clean[i];
2786 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
2788 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
2789 if( ( pid->i_pid == 0x11 /* SDT */ ||
2790 pid->i_pid == 0x12 /* EDT */ ) && pid->psi )
2792 /* This doesn't look like a DVB stream so don't try
2793 * parsing the SDT/EDT */
2794 dvbpsi_DetachDemux( pid->psi->handle );
2800 msg_Warn( p_demux, "pmt error: pid=%d already defined",
2806 PIDInit( pid, VLC_FALSE, pmt->psi );
2807 PIDFillFormat( pid, p_es->i_type );
2808 pid->i_owner_number = prg->i_number;
2809 pid->i_pid = p_es->i_pid;
2810 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
2812 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2813 p_es->i_type == 0x12 )
2815 /* MPEG-4 stream: search SL_DESCRIPTOR */
2816 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2818 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2820 if( p_dr && p_dr->i_length == 2 )
2822 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2824 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2826 pid->es->p_mpeg4desc = NULL;
2828 for( i = 0; i < 255; i++ )
2830 iod_descriptor_t *iod = prg->iod;
2832 if( iod->es_descr[i].b_ok &&
2833 iod->es_descr[i].i_es_id == i_es_id )
2835 pid->es->p_mpeg4desc = &iod->es_descr[i];
2841 if( pid->es->p_mpeg4desc != NULL )
2843 decoder_config_descriptor_t *dcd =
2844 &pid->es->p_mpeg4desc->dec_descr;
2846 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2848 pid->es->fmt.i_cat = VIDEO_ES;
2849 switch( dcd->i_objectTypeIndication )
2851 case 0x0B: /* mpeg4 sub */
2852 pid->es->fmt.i_cat = SPU_ES;
2853 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2856 case 0x20: /* mpeg4 */
2857 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2859 case 0x21: /* h264 */
2860 pid->es->fmt.i_codec = VLC_FOURCC('h','2','6','4');
2867 case 0x65: /* mpeg2 */
2868 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2870 case 0x6a: /* mpeg1 */
2871 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2873 case 0x6c: /* mpeg1 */
2874 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2877 pid->es->fmt.i_cat = UNKNOWN_ES;
2881 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2883 pid->es->fmt.i_cat = AUDIO_ES;
2884 switch( dcd->i_objectTypeIndication )
2886 case 0x40: /* mpeg4 */
2887 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2891 case 0x68: /* mpeg2 aac */
2892 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2894 case 0x69: /* mpeg2 */
2895 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2897 case 0x6b: /* mpeg1 */
2898 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2901 pid->es->fmt.i_cat = UNKNOWN_ES;
2907 pid->es->fmt.i_cat = UNKNOWN_ES;
2910 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2912 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2913 if( pid->es->fmt.i_extra > 0 )
2915 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2916 memcpy( pid->es->fmt.p_extra,
2917 dcd->p_decoder_specific_info,
2918 pid->es->fmt.i_extra );
2923 else if( p_es->i_type == 0x06 )
2925 dvbpsi_descriptor_t *p_dr;
2927 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2928 p_dr = p_dr->p_next )
2930 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
2931 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2933 if( p_dr->i_tag == 0x05 )
2935 /* Registration Descriptor */
2936 if( p_dr->i_length != 4 )
2938 msg_Warn( p_demux, "invalid Registration Descriptor" );
2942 if( !memcmp( p_dr->p_data, "AC-3", 4 ) )
2944 /* ATSC with stream_type 0x81 (but this descriptor
2945 * is then not mandatory */
2946 pid->es->fmt.i_cat = AUDIO_ES;
2947 pid->es->fmt.i_codec = VLC_FOURCC('a','5','2',' ');
2949 else if( !memcmp( p_dr->p_data, "DTS1", 4 ) ||
2950 !memcmp( p_dr->p_data, "DTS2", 4 ) ||
2951 !memcmp( p_dr->p_data, "DTS3", 4 ) )
2953 /*registration descriptor(ETSI TS 101 154 Annex F)*/
2954 pid->es->fmt.i_cat = AUDIO_ES;
2955 pid->es->fmt.i_codec = VLC_FOURCC('d','t','s',' ');
2957 else if( !memcmp( p_dr->p_data, "BSSD", 4 ) )
2959 pid->es->fmt.i_cat = AUDIO_ES;
2960 pid->es->fmt.i_codec = VLC_FOURCC('l','p','c','m');
2965 "unknown Registration Descriptor (%4.4s)",
2971 else if( p_dr->i_tag == 0x6a )
2973 /* DVB with stream_type 0x06 */
2974 pid->es->fmt.i_cat = AUDIO_ES;
2975 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2977 else if( p_dr->i_tag == 0x73 )
2979 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2980 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2981 pid->es->fmt.i_cat = AUDIO_ES;
2982 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2984 else if( p_dr->i_tag == 0x45 )
2986 msg_Dbg( p_demux, " * VBI Data descriptor" );
2987 /* FIXME : store the information somewhere */
2989 else if( p_dr->i_tag == 0x46 )
2991 msg_Dbg( p_demux, " * VBI Teletext descriptor" );
2992 /* FIXME : store the information somewhere */
2994 #ifdef _DVBPSI_DR_52_H_
2995 else if( p_dr->i_tag == 0x52 )
2997 dvbpsi_stream_identifier_dr_t *si;
2998 si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3000 msg_Dbg( p_demux, " * Stream Component Identifier: %d", si->i_component_tag );
3003 else if( p_dr->i_tag == 0x56 )
3005 msg_Dbg( p_demux, " * EBU Teletext descriptor" );
3006 pid->es->fmt.i_cat = SPU_ES;
3007 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
3008 pid->es->fmt.psz_description = strdup( "Teletext" );
3009 pid->es->fmt.i_extra = p_dr->i_length;
3010 pid->es->fmt.p_extra = malloc( p_dr->i_length );
3011 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
3014 #ifdef _DVBPSI_DR_59_H_
3015 else if( p_dr->i_tag == 0x59 )
3018 dvbpsi_subtitling_dr_t *sub;
3021 pid->es->fmt.i_cat = SPU_ES;
3022 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
3023 pid->es->fmt.i_group = p_pmt->i_program_number;
3025 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3026 if( !sub ) continue;
3028 /* Each subtitle ES contains n languages,
3029 * We are going to create n ES for the n tracks */
3030 if( sub->i_subtitles_number > 0 )
3032 pid->es->fmt.psz_language = malloc( 4 );
3033 memcpy( pid->es->fmt.psz_language,
3034 sub->p_subtitle[0].i_iso6392_language_code, 3);
3035 pid->es->fmt.psz_language[3] = 0;
3037 pid->es->fmt.subs.dvb.i_id =
3038 sub->p_subtitle[0].i_composition_page_id;
3040 pid->es->fmt.subs.dvb.i_id |=
3041 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
3043 else pid->es->fmt.i_cat = UNKNOWN_ES;
3045 for( n = 1; n < sub->i_subtitles_number; n++ )
3047 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
3048 p_es->fmt = pid->es->fmt;
3051 p_es->i_pes_size = 0;
3052 p_es->i_pes_gathered = 0;
3053 p_es->pp_last = &p_es->p_pes;
3054 p_es->p_mpeg4desc = NULL;
3056 p_es->fmt.psz_language = malloc( 4 );
3057 memcpy( p_es->fmt.psz_language,
3058 sub->p_subtitle[n].i_iso6392_language_code, 3);
3059 p_es->fmt.psz_language[3] = 0;
3061 p_es->fmt.subs.dvb.i_id =
3062 sub->p_subtitle[n].i_composition_page_id;
3064 p_es->fmt.subs.dvb.i_id |=
3065 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
3067 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3070 #endif /* _DVBPSI_DR_59_H_ */
3073 else if( p_es->i_type == 0xa0 )
3075 /* MSCODEC sent by vlc */
3076 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
3078 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
3080 if( p_dr && p_dr->i_length >= 8 )
3082 pid->es->fmt.i_cat = VIDEO_ES;
3083 pid->es->fmt.i_codec =
3084 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
3085 p_dr->p_data[2], p_dr->p_data[3] );
3086 pid->es->fmt.video.i_width =
3087 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
3088 pid->es->fmt.video.i_height =
3089 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
3090 pid->es->fmt.i_extra =
3091 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
3093 if( pid->es->fmt.i_extra > 0 )
3095 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
3096 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
3097 pid->es->fmt.i_extra );
3102 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
3105 /* For such stream we will gather them ourself and don't launch a
3107 * Yes it's ugly but it's the only way to have DIV3 working */
3108 pid->es->fmt.b_packetized = VLC_TRUE;
3111 if( pid->es->fmt.i_cat == AUDIO_ES ||
3112 ( pid->es->fmt.i_cat == SPU_ES &&
3113 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
3115 /* get language descriptor */
3116 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
3117 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
3121 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
3125 #if DR_0A_API_VER >= 2
3126 pid->es->fmt.psz_language = malloc( 4 );
3127 memcpy( pid->es->fmt.psz_language,
3128 p_decoded->code[0].iso_639_code, 3 );
3129 pid->es->fmt.psz_language[3] = 0;
3130 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
3131 switch( p_decoded->code[0].i_audio_type ) {
3133 pid->es->fmt.psz_description = NULL;
3136 pid->es->fmt.psz_description =
3137 strdup(_("clean effects"));
3140 pid->es->fmt.psz_description =
3141 strdup(_("hearing impaired"));
3144 pid->es->fmt.psz_description =
3145 strdup(_("visual impaired commentary"));
3148 msg_Dbg( p_demux, "unknown audio type: %d",
3149 p_decoded->code[0].i_audio_type);
3150 pid->es->fmt.psz_description = NULL;
3153 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
3154 pid->es->fmt.p_extra_languages =
3155 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
3156 pid->es->fmt.i_extra_languages );
3157 for( i = 0; i < pid->es->fmt.i_extra_languages; i++ ) {
3158 msg_Dbg( p_demux, "bang" );
3159 pid->es->fmt.p_extra_languages[i].psz_language =
3161 memcpy(pid->es->fmt.p_extra_languages[i].psz_language,
3162 p_decoded->code[i+1].iso_639_code, 3 );
3163 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
3164 switch( p_decoded->code[i].i_audio_type ) {
3166 pid->es->fmt.p_extra_languages[i].psz_description =
3170 pid->es->fmt.p_extra_languages[i].psz_description =
3171 strdup(_("clean effects"));
3174 pid->es->fmt.p_extra_languages[i].psz_description =
3175 strdup(_("hearing impaired"));
3178 pid->es->fmt.p_extra_languages[i].psz_description =
3179 strdup(_("visual impaired commentary"));
3182 msg_Dbg( p_demux, "unknown audio type: %d",
3183 p_decoded->code[i].i_audio_type);
3184 pid->es->fmt.psz_description = NULL;
3190 pid->es->fmt.psz_language = malloc( 4 );
3191 memcpy( pid->es->fmt.psz_language,
3192 p_decoded->i_iso_639_code, 3 );
3193 pid->es->fmt.psz_language[3] = 0;
3199 pid->es->fmt.i_group = p_pmt->i_program_number;
3200 if( pid->es->fmt.i_cat == UNKNOWN_ES )
3202 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
3203 p_es->i_pid, p_es->i_type );
3205 else if( !p_sys->b_udp_out )
3207 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
3208 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
3210 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
3212 /* Check if we can avoid restarting the ES */
3214 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
3215 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
3216 pid->es->fmt.i_extra == 0 &&
3217 pid->i_extra_es == old_pid->i_extra_es &&
3218 ( ( !pid->es->fmt.psz_language &&
3219 !old_pid->es->fmt.psz_language ) ||
3220 ( pid->es->fmt.psz_language &&
3221 old_pid->es->fmt.psz_language &&
3222 !strcmp( pid->es->fmt.psz_language,
3223 old_pid->es->fmt.psz_language ) ) ) )
3225 pid->es->id = old_pid->es->id;
3226 old_pid->es->id = NULL;
3227 for( i = 0; i < pid->i_extra_es; i++ )
3229 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
3230 old_pid->extra_es[i]->id = NULL;
3237 PIDClean( p_demux->out, old_pid );
3238 TAB_REMOVE( i_clean, pp_clean, old_pid );
3242 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
3243 for( i = 0; i < pid->i_extra_es; i++ )
3245 pid->extra_es[i]->id =
3246 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
3251 /* Add ES to the list */
3254 PIDClean( p_demux->out, old_pid );
3255 TAB_REMOVE( i_clean, pp_clean, old_pid );
3257 p_sys->pid[p_es->i_pid] = *pid;
3259 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
3260 p_dr = p_dr->p_next )
3262 if( p_dr->i_tag == 0x9 )
3264 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
3266 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
3271 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3273 /* Set demux filter */
3274 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3275 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
3280 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3282 /* Set CAM descrambling */
3283 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3284 ACCESS_SET_PRIVATE_ID_CA, p_pmt );
3288 dvbpsi_DeletePMT( p_pmt );
3291 for ( i = 0; i < i_clean; i++ )
3293 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3295 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3296 ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
3300 PIDClean( p_demux->out, pp_clean[i] );
3302 if( i_clean ) free( pp_clean );
3305 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
3307 demux_sys_t *p_sys = p_demux->p_sys;
3308 dvbpsi_pat_program_t *p_program;
3309 ts_pid_t *pat = &p_sys->pid[0];
3312 msg_Dbg( p_demux, "PATCallBack called" );
3314 if( pat->psi->i_pat_version != -1 &&
3315 ( !p_pat->b_current_next ||
3316 p_pat->i_version == pat->psi->i_pat_version ) )
3318 dvbpsi_DeletePAT( p_pat );
3322 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
3323 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
3326 if( p_sys->i_pmt > 0 )
3329 ts_pid_t **pmt_rm = NULL;
3331 /* Search pmt to be deleted */
3332 for( i = 0; i < p_sys->i_pmt; i++ )
3334 ts_pid_t *pmt = p_sys->pmt[i];
3335 vlc_bool_t b_keep = VLC_FALSE;
3337 for( p_program = p_pat->p_first_program; p_program != NULL;
3338 p_program = p_program->p_next )
3340 if( p_program->i_pid == pmt->i_pid )
3343 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
3345 if( p_program->i_number ==
3346 pmt->psi->prg[i_prg]->i_number )
3358 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
3362 /* Delete all ES attached to thoses PMT */
3363 for( i = 2; i < 8192; i++ )
3365 ts_pid_t *pid = &p_sys->pid[i];
3367 if( !pid->b_valid || pid->psi ) continue;
3369 for( j = 0; j < i_pmt_rm; j++ )
3372 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
3374 /* We only remove es that aren't defined by extra pmt */
3375 if( pid->p_owner->prg[i_prg]->i_pid_pmt !=
3376 pmt_rm[j]->i_pid ) continue;
3378 if( p_sys->b_dvb_control && pid->es->id )
3380 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3381 ACCESS_SET_PRIVATE_ID_STATE, i,
3383 p_sys->b_dvb_control = VLC_FALSE;
3386 PIDClean( p_demux->out, pid );
3390 if( !pid->b_valid ) break;
3394 /* Delete PMT pid */
3395 for( i = 0; i < i_pmt_rm; i++ )
3398 if( p_sys->b_dvb_control )
3400 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3401 ACCESS_SET_PRIVATE_ID_STATE,
3402 pmt_rm[i]->i_pid, VLC_FALSE ) )
3403 p_sys->b_dvb_control = VLC_FALSE;
3406 for( i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
3408 const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
3410 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
3413 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
3414 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
3417 if( pmt_rm ) free( pmt_rm );
3420 /* now create programs */
3421 for( p_program = p_pat->p_first_program; p_program != NULL;
3422 p_program = p_program->p_next )
3424 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
3426 if( p_program->i_number != 0 )
3428 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
3429 vlc_bool_t b_add = VLC_TRUE;
3434 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
3436 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
3445 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
3450 PIDInit( pmt, VLC_TRUE, pat->psi );
3451 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
3452 dvbpsi_AttachPMT( p_program->i_number,
3453 (dvbpsi_pmt_callback)PMTCallBack,
3455 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
3456 p_program->i_number;
3457 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
3460 /* Now select PID at access level */
3461 if( p_sys->b_dvb_control )
3463 if( DVBProgramIsSelected( p_demux, p_program->i_number ) )
3465 if( p_sys->i_dvb_program == 0 )
3466 p_sys->i_dvb_program = p_program->i_number;
3468 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
3469 p_sys->b_dvb_control = VLC_FALSE;
3475 pat->psi->i_pat_version = p_pat->i_version;
3477 dvbpsi_DeletePAT( p_pat );