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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <stdlib.h> /* malloc(), free() */
35 #include <vlc/input.h>
41 #include "../mux/mpeg/csa.h"
43 /* Include dvbpsi headers */
44 #ifdef HAVE_DVBPSI_DR_H
45 # include <dvbpsi/dvbpsi.h>
46 # include <dvbpsi/demux.h>
47 # include <dvbpsi/descriptor.h>
48 # include <dvbpsi/pat.h>
49 # include <dvbpsi/pmt.h>
50 # include <dvbpsi/sdt.h>
51 # include <dvbpsi/dr.h>
52 # include <dvbpsi/psi.h>
56 # include "descriptor.h"
57 # include "tables/pat.h"
58 # include "tables/pmt.h"
59 # include "tables/sdt.h"
60 # include "descriptors/dr.h"
65 #ifdef _DVBPSI_DR_4D_H_
66 # define TS_USE_DVB_SI 1
67 # ifdef HAVE_DVBPSI_DR_H
68 # include <dvbpsi/eit.h>
70 # include "tables/eit.h"
77 * - XXX: do not mark options message to be translated, they are too osbcure for now ...
82 #define vlc_meta_Add(a,b,c) fprintf(stderr, "FIXME: TS demuxer meta is broken\n" )
83 /*****************************************************************************
85 *****************************************************************************/
86 static int Open ( vlc_object_t * );
87 static void Close ( vlc_object_t * );
89 #define PMT_TEXT N_("Extra PMT")
90 #define PMT_LONGTEXT N_( \
91 "Allows a user to specify an extra pmt (pmt_pid=pid:stream_type[,...])." )
93 #define PID_TEXT N_("Set id of ES to PID")
94 #define PID_LONGTEXT N_("Set the internal ID of each elementary stream" \
95 " handled by VLC to the same value as the PID in" \
96 " the TS stream, instead of 1, 2, 3, etc. Useful to" \
97 " do \'#duplicate{..., select=\"es=<pid>\"}\'.")
99 #define TSOUT_TEXT N_("Fast udp streaming")
100 #define TSOUT_LONGTEXT N_( \
101 "Sends TS to specific ip:port by udp (you must know what you are doing).")
103 #define MTUOUT_TEXT N_("MTU for out mode")
104 #define MTUOUT_LONGTEXT N_("MTU for out mode.")
106 #define CSA_TEXT N_("CSA ck")
107 #define CSA_LONGTEXT N_("Control word for the CSA encryption algorithm")
109 #define SILENT_TEXT N_("Silent mode")
110 #define SILENT_LONGTEXT N_("Do not complain on encrypted PES.")
112 #define CAPMT_SYSID_TEXT N_("CAPMT System ID")
113 #define CAPMT_SYSID_LONGTEXT N_("Only forward descriptors from this SysID to the CAM.")
115 #define CPKT_TEXT N_("Packet size in bytes to decrypt")
116 #define CPKT_LONGTEXT N_("Specify the size of the TS packet to decrypt. " \
117 "The decryption routines subtract the TS-header from the value before " \
120 #define TSDUMP_TEXT N_("Filename of dump")
121 #define TSDUMP_LONGTEXT N_("Specify a filename where to dump the TS in.")
123 #define APPEND_TEXT N_("Append")
124 #define APPEND_LONGTEXT N_( \
125 "If the file exists and this option is selected, the existing file " \
126 "will not be overwritten." )
128 #define DUMPSIZE_TEXT N_("Dump buffer size")
129 #define DUMPSIZE_LONGTEXT N_( \
130 "Tweak the buffer size for reading and writing an integer number of packets." \
131 "Specify the size of the buffer here and not the number of packets." )
134 set_description( _("MPEG Transport Stream demuxer") );
135 set_shortname ( "MPEG-TS" );
136 set_category( CAT_INPUT );
137 set_subcategory( SUBCAT_INPUT_DEMUX );
139 add_string( "ts-extra-pmt", NULL, NULL, PMT_TEXT, PMT_LONGTEXT, VLC_TRUE );
140 add_bool( "ts-es-id-pid", 1, NULL, PID_TEXT, PID_LONGTEXT, VLC_TRUE );
141 add_string( "ts-out", NULL, NULL, TSOUT_TEXT, TSOUT_LONGTEXT, VLC_TRUE );
142 add_integer( "ts-out-mtu", 1500, NULL, MTUOUT_TEXT,
143 MTUOUT_LONGTEXT, VLC_TRUE );
144 add_string( "ts-csa-ck", NULL, NULL, CSA_TEXT, CSA_LONGTEXT, VLC_TRUE );
145 add_integer( "ts-csa-pkt", 188, NULL, CPKT_TEXT, CPKT_LONGTEXT, VLC_TRUE );
146 add_bool( "ts-silent", 0, NULL, SILENT_TEXT, SILENT_LONGTEXT, VLC_TRUE );
148 add_file( "ts-dump-file", NULL, NULL, TSDUMP_TEXT, TSDUMP_LONGTEXT, VLC_FALSE );
149 add_bool( "ts-dump-append", 0, NULL, APPEND_TEXT, APPEND_LONGTEXT, VLC_FALSE );
150 add_integer( "ts-dump-size", 16384, NULL, DUMPSIZE_TEXT,
151 DUMPSIZE_LONGTEXT, VLC_TRUE );
153 set_capability( "demux2", 10 );
154 set_callbacks( Open, Close );
155 add_shortcut( "ts" );
158 /*****************************************************************************
160 *****************************************************************************/
164 uint8_t i_objectTypeIndication;
165 uint8_t i_streamType;
166 vlc_bool_t b_upStream;
167 uint32_t i_bufferSizeDB;
168 uint32_t i_maxBitrate;
169 uint32_t i_avgBitrate;
171 int i_decoder_specific_info_len;
172 uint8_t *p_decoder_specific_info;
174 } decoder_config_descriptor_t;
178 vlc_bool_t b_useAccessUnitStartFlag;
179 vlc_bool_t b_useAccessUnitEndFlag;
180 vlc_bool_t b_useRandomAccessPointFlag;
181 vlc_bool_t b_useRandomAccessUnitsOnlyFlag;
182 vlc_bool_t b_usePaddingFlag;
183 vlc_bool_t b_useTimeStampsFlags;
184 vlc_bool_t b_useIdleFlag;
185 vlc_bool_t b_durationFlag;
186 uint32_t i_timeStampResolution;
187 uint32_t i_OCRResolution;
188 uint8_t i_timeStampLength;
191 uint8_t i_instantBitrateLength;
192 uint8_t i_degradationPriorityLength;
193 uint8_t i_AU_seqNumLength;
194 uint8_t i_packetSeqNumLength;
196 uint32_t i_timeScale;
197 uint16_t i_accessUnitDuration;
198 uint16_t i_compositionUnitDuration;
200 uint64_t i_startDecodingTimeStamp;
201 uint64_t i_startCompositionTimeStamp;
203 } sl_config_descriptor_t;
210 vlc_bool_t b_streamDependenceFlag;
211 vlc_bool_t b_OCRStreamFlag;
212 uint8_t i_streamPriority;
216 uint16_t i_dependOn_es_id;
217 uint16_t i_OCR_es_id;
219 decoder_config_descriptor_t dec_descr;
220 sl_config_descriptor_t sl_descr;
222 } es_mpeg4_descriptor_t;
226 uint8_t i_iod_label, i_iod_label_scope;
232 uint8_t i_ODProfileLevelIndication;
233 uint8_t i_sceneProfileLevelIndication;
234 uint8_t i_audioProfileLevelIndication;
235 uint8_t i_visualProfileLevelIndication;
236 uint8_t i_graphicsProfileLevelIndication;
238 es_mpeg4_descriptor_t es_descr[255];
244 dvbpsi_handle handle;
250 /* IOD stuff (mpeg4) */
251 iod_descriptor_t *iod;
257 /* for special PAT/SDT case */
258 dvbpsi_handle handle; /* PAT/SDT/EIT */
277 es_mpeg4_descriptor_t *p_mpeg4desc;
288 int i_cc; /* countinuity counter */
290 /* PSI owner (ie PMT -> PAT, ES -> PMT */
298 /* Some private streams encapsulate several ES (eg. DVB subtitles)*/
306 /* TS packet size (188, 192, 204) */
309 /* how many TS packet we read at once */
320 vlc_bool_t b_es_id_pid;
325 vlc_bool_t b_udp_out;
326 int fd; /* udp socket */
329 vlc_bool_t b_dvb_control;
331 vlc_list_t *p_programs_list;
334 char *psz_file; /* file to dump data in */
335 FILE *p_file; /* filehandle */
336 uint64_t i_write; /* bytes written */
337 vlc_bool_t b_file_out; /* dump mode enabled */
343 static int Demux ( demux_t *p_demux );
344 static int DemuxFile( demux_t *p_demux );
345 static int Control( demux_t *p_demux, int i_query, va_list args );
347 static void PIDInit ( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner );
348 static void PIDClean( es_out_t *out, ts_pid_t *pid );
349 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type );
351 static void PATCallBack( demux_t *, dvbpsi_pat_t * );
352 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt );
354 static void PSINewTableCallBack( demux_t *, dvbpsi_handle,
355 uint8_t i_table_id, uint16_t i_extension );
358 static inline int PIDGet( block_t *p )
360 return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
363 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
365 static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
367 static iod_descriptor_t *IODNew( int , uint8_t * );
368 static void IODFree( iod_descriptor_t * );
370 #define TS_PACKET_SIZE_188 188
371 #define TS_PACKET_SIZE_192 192
372 #define TS_PACKET_SIZE_204 204
373 #define TS_PACKET_SIZE_MAX 204
374 #define TS_TOPFIELD_HEADER 1320
376 /*****************************************************************************
378 *****************************************************************************/
379 static int Open( vlc_object_t *p_this )
381 demux_t *p_demux = (demux_t*)p_this;
385 int i_sync, i_peek, i;
391 vlc_bool_t b_topfield = VLC_FALSE;
395 if( stream_Peek( p_demux->s, &p_peek, TS_PACKET_SIZE_MAX ) <
396 TS_PACKET_SIZE_MAX ) return VLC_EGENERIC;
398 if( p_peek[0] == 'T' && p_peek[1] == 'F' &&
399 p_peek[2] == 'r' && p_peek[3] == 'c' )
401 b_topfield = VLC_TRUE;
402 msg_Dbg( p_demux, "this is a topfield file" );
405 /* Search first sync byte */
406 for( i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
408 if( p_peek[i_sync] == 0x47 ) break;
410 if( i_sync >= TS_PACKET_SIZE_MAX && !b_topfield )
412 if( strcmp( p_demux->psz_demux, "ts" ) ) return VLC_EGENERIC;
413 msg_Warn( p_demux, "this does not look like a TS stream, continuing" );
418 /* Read the entire Topfield header */
419 i_peek = TS_TOPFIELD_HEADER;
423 /* Check next 3 sync bytes */
424 i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
427 if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
429 msg_Err( p_demux, "cannot peek" );
432 if( p_peek[i_sync + TS_PACKET_SIZE_188] == 0x47 &&
433 p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
434 p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
436 i_packet_size = TS_PACKET_SIZE_188;
438 else if( p_peek[i_sync + TS_PACKET_SIZE_192] == 0x47 &&
439 p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
440 p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
442 i_packet_size = TS_PACKET_SIZE_192;
444 else if( p_peek[i_sync + TS_PACKET_SIZE_204] == 0x47 &&
445 p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
446 p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
448 i_packet_size = TS_PACKET_SIZE_204;
450 else if( !strcmp( p_demux->psz_demux, "ts" ) )
452 i_packet_size = TS_PACKET_SIZE_188;
454 else if( b_topfield )
456 i_packet_size = TS_PACKET_SIZE_188;
458 /* I used the TF5000PVR 2004 Firmware .doc header documentation,
459 * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
460 * but after the filename the offsets seem to be incorrect. - DJ */
461 int i_duration, i_name;
462 char *psz_name = malloc(25);
463 char *psz_event_name;
464 char *psz_event_text = malloc(130);
465 char *psz_ext_text = malloc(1025);
467 // 2 bytes version Uimsbf (4,5)
468 // 2 bytes reserved (6,7)
469 // 2 bytes duration in minutes Uimsbf (8,9(
470 i_duration = (int) (p_peek[8] << 8) | p_peek[9];
471 msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
472 // 2 bytes service number in channel list (10, 11)
473 // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
474 // 4 bytes of reserved + tuner info (14,15,16,17)
475 // 2 bytes of Service ID Bslbf (18,19)
476 // 2 bytes of PMT PID Uimsbf (20,21)
477 // 2 bytes of PCR PID Uimsbf (22,23)
478 // 2 bytes of Video PID Uimsbf (24,25)
479 // 2 bytes of Audio PID Uimsbf (26,27)
480 // 24 bytes filename Bslbf
481 memcpy( psz_name, &p_peek[28], 24 );
483 msg_Dbg( p_demux, "recordingname=%s", psz_name );
484 // 1 byte of sat index Uimsbf (52)
485 // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
486 // 4 bytes of freq. Uimsbf (56,57,58,59)
487 // 2 bytes of symbol rate Uimsbf (60,61)
488 // 2 bytes of TS stream ID Uimsbf (62,63)
491 // 2 bytes duration Uimsbf (70,71)
492 //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
493 //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
494 // 4 bytes EventID Uimsbf (72-75)
495 // 8 bytes of Start and End time info (76-83)
496 // 1 byte reserved (84)
497 // 1 byte event name length Uimsbf (89)
498 i_name = (int)(p_peek[89]&~0x81);
499 msg_Dbg( p_demux, "event name length = %d", i_name);
500 psz_event_name = malloc( i_name+1 );
501 // 1 byte parental rating (90)
502 // 129 bytes of event text
503 memcpy( psz_event_name, &p_peek[91], i_name );
504 psz_event_name[i_name] = '\0';
505 memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
506 psz_event_text[129-i_name] = '\0';
507 msg_Dbg( p_demux, "event name=%s", psz_event_name );
508 msg_Dbg( p_demux, "event text=%s", psz_event_text );
509 // 12 bytes reserved (220)
511 // 2 bytes Event Text Length Uimsbf
512 // 4 bytes EventID Uimsbf
513 // FIXME We just have 613 bytes. not enough for this entire text
514 // 1024 bytes Extended Event Text Bslbf
515 memcpy( psz_ext_text, p_peek+372, 1024 );
516 psz_ext_text[1024] = '\0';
517 msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
518 // 52 bytes reserved Bslbf
523 msg_Warn( p_demux, "TS module discarded (lost sync)" );
527 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
528 memset( p_sys, 0, sizeof( demux_sys_t ) );
529 p_sys->i_packet_size = i_packet_size;
531 /* Fill dump mode fields */
533 p_sys->p_file = NULL;
534 p_sys->b_file_out = VLC_FALSE;
535 p_sys->psz_file = var_CreateGetString( p_demux, "ts-dump-file" );
536 if( *p_sys->psz_file != '\0' )
538 p_sys->b_file_out = VLC_TRUE;
540 var_Create( p_demux, "ts-dump-append", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
541 var_Get( p_demux, "ts-dump-append", &val );
542 b_append = val.b_bool;
548 if( !strcmp( p_sys->psz_file, "-" ) )
550 msg_Info( p_demux, "dumping raw stream to standard output" );
551 p_sys->p_file = stdout;
553 else if( ( p_sys->p_file = utf8_fopen( p_sys->psz_file, psz_mode ) ) == NULL )
555 msg_Err( p_demux, "cannot create `%s' for writing", p_sys->psz_file );
556 p_sys->b_file_out = VLC_FALSE;
559 if( p_sys->b_file_out )
563 /* Determine how many packets to read. */
564 var_Create( p_demux, "ts-dump-size",
565 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
566 var_Get( p_demux, "ts-dump-size", &bufsize );
567 p_sys->i_ts_read = (int) (bufsize.i_int / p_sys->i_packet_size);
568 if( p_sys->i_ts_read <= 0 )
570 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
572 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
573 msg_Info( p_demux, "%s raw stream to file `%s' reading packets %d",
574 b_append ? "appending" : "dumping", p_sys->psz_file,
579 /* Fill p_demux field */
580 if( p_sys->b_file_out )
581 p_demux->pf_demux = DemuxFile;
583 p_demux->pf_demux = Demux;
584 p_demux->pf_control = Control;
586 /* Init p_sys field */
587 p_sys->b_meta = VLC_TRUE;
588 p_sys->b_dvb_control = VLC_TRUE;
589 p_sys->i_dvb_program = 0;
590 for( i = 0; i < 8192; i++ )
592 ts_pid_t *pid = &p_sys->pid[i];
595 pid->b_seen = VLC_FALSE;
596 pid->b_valid = VLC_FALSE;
598 /* PID 8191 is padding */
599 p_sys->pid[8191].b_seen = VLC_TRUE;
600 p_sys->i_packet_size = i_packet_size;
601 p_sys->b_udp_out = VLC_FALSE;
602 p_sys->i_ts_read = 50;
605 /* Init PAT handler */
606 pat = &p_sys->pid[0];
607 PIDInit( pat, VLC_TRUE, NULL );
608 pat->psi->handle = dvbpsi_AttachPAT( (dvbpsi_pat_callback)PATCallBack,
613 ts_pid_t *sdt = &p_sys->pid[0x11];
614 ts_pid_t *eit = &p_sys->pid[0x12];
616 PIDInit( sdt, VLC_TRUE, NULL );
618 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
620 PIDInit( eit, VLC_TRUE, NULL );
622 dvbpsi_AttachDemux( (dvbpsi_demux_new_cb_t)PSINewTableCallBack,
624 if( p_sys->b_dvb_control )
626 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
627 ACCESS_SET_PRIVATE_ID_STATE, 0x11, VLC_TRUE );
628 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
629 ACCESS_SET_PRIVATE_ID_STATE, 0x12, VLC_TRUE );
639 var_Create( p_demux, "ts-es-id-pid", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
640 var_Get( p_demux, "ts-es-id-pid", &val );
641 p_sys->b_es_id_pid = val.b_bool;
643 var_Create( p_demux, "ts-out", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
644 var_Get( p_demux, "ts-out", &val );
645 if( val.psz_string && *val.psz_string && !p_sys->b_file_out )
648 char *psz = strchr( val.psz_string, ':' );
651 p_sys->b_udp_out = VLC_TRUE;
656 i_port = atoi( psz );
658 if( i_port <= 0 ) i_port = 1234;
659 msg_Dbg( p_demux, "resend ts to '%s:%d'", val.psz_string, i_port );
661 p_sys->fd = net_ConnectUDP( p_demux, val.psz_string, i_port, 0 );
664 msg_Err( p_demux, "failed to open udp socket, send disabled" );
665 p_sys->b_udp_out = VLC_FALSE;
669 var_Create( p_demux, "ts-out-mtu",
670 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
671 var_Get( p_demux, "ts-out-mtu", &mtu );
672 p_sys->i_ts_read = mtu.i_int / p_sys->i_packet_size;
673 if( p_sys->i_ts_read <= 0 )
675 p_sys->i_ts_read = 1500 / p_sys->i_packet_size;
677 p_sys->buffer = malloc( p_sys->i_packet_size * p_sys->i_ts_read );
682 free( val.psz_string );
685 /* We handle description of an extra PMT */
686 var_Create( p_demux, "ts-extra-pmt", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
687 var_Get( p_demux, "ts-extra-pmt", &val );
688 if( val.psz_string && strchr( val.psz_string, '=' ) != NULL )
690 char *psz = val.psz_string;
691 int i_pid = strtol( psz, &psz, 0 );
693 if( i_pid >= 2 && i_pid < 8192 )
695 ts_pid_t *pmt = &p_sys->pid[i_pid];
697 msg_Dbg( p_demux, "extra pmt specified (pid=%d)", i_pid );
698 PIDInit( pmt, VLC_TRUE, NULL );
700 pmt->psi->prg = malloc( sizeof(ts_prg_psi_t) );
701 /* FIXME we should also ask for a number */
702 pmt->psi->prg[0]->handle =
703 dvbpsi_AttachPMT( 1, (dvbpsi_pmt_callback)PMTCallBack,
705 pmt->psi->prg[0]->i_number = 0; /* special one */
707 psz = strchr( psz, '=' ) + 1; /* can't failed */
710 char *psz_next = strchr( psz, ',' );
711 int i_pid, i_stream_type;
718 i_pid = strtol( psz, &psz, 0 );
721 i_stream_type = strtol( psz + 1, &psz, 0 );
722 if( i_pid >= 2 && i_pid < 8192 &&
723 !p_sys->pid[i_pid].b_valid )
725 ts_pid_t *pid = &p_sys->pid[i_pid];
727 PIDInit( pid, VLC_FALSE, pmt->psi);
728 if( pmt->psi->prg[0]->i_pid_pcr <= 0 )
730 pmt->psi->prg[0]->i_pid_pcr = i_pid;
732 PIDFillFormat( pid, i_stream_type);
733 if( pid->es->fmt.i_cat != UNKNOWN_ES )
735 if( p_sys->b_es_id_pid )
737 pid->es->fmt.i_id = i_pid;
739 msg_Dbg( p_demux, " * es pid=%d type=%d "
740 "fcc=%4.4s", i_pid, i_stream_type,
741 (char*)&pid->es->fmt.i_codec );
742 pid->es->id = es_out_Add( p_demux->out,
753 free( val.psz_string );
756 var_Create( p_demux, "ts-csa-ck", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
757 var_Get( p_demux, "ts-csa-ck", &val );
758 if( val.psz_string && *val.psz_string )
760 char *psz = val.psz_string;
761 if( psz[0] == '0' && ( psz[1] == 'x' || psz[1] == 'X' ) )
765 if( strlen( psz ) != 16 )
767 msg_Warn( p_demux, "invalid csa ck (it must be 16 chars long)" );
772 uint64_t i_ck = strtoull( psz, NULL, 16 );
774 uint64_t i_ck = strtoll( psz, NULL, 16 );
778 for( i = 0; i < 8; i++ )
780 ck[i] = ( i_ck >> ( 56 - 8*i) )&0xff;
782 #ifndef TS_NO_CSA_CK_MSG
783 msg_Dbg( p_demux, "using CSA scrambling with "
784 "ck=%x:%x:%x:%x:%x:%x:%x:%x",
785 ck[0], ck[1], ck[2], ck[3], ck[4], ck[5], ck[6], ck[7] );
787 p_sys->csa = csa_New();
793 csa_SetCW( p_sys->csa, ck, ck );
795 var_Create( p_demux, "ts-csa-pkt", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
796 var_Get( p_demux, "ts-csa-pkt", &pkt_val );
797 if( pkt_val.i_int < 4 || pkt_val.i_int > 188 )
799 msg_Err( p_demux, "wrong packet size %d specified.", pkt_val.i_int );
800 msg_Warn( p_demux, "using default packet size of 188 bytes" );
801 p_sys->i_csa_pkt_size = 188;
803 else p_sys->i_csa_pkt_size = pkt_val.i_int;
804 msg_Dbg( p_demux, "decrypting %d bytes of packet", p_sys->i_csa_pkt_size );
810 free( val.psz_string );
813 var_Create( p_demux, "ts-silent", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
814 var_Get( p_demux, "ts-silent", &val );
815 p_sys->b_silent = val.b_bool;
820 /*****************************************************************************
822 *****************************************************************************/
823 static void Close( vlc_object_t *p_this )
825 demux_t *p_demux = (demux_t*)p_this;
826 demux_sys_t *p_sys = p_demux->p_sys;
830 msg_Dbg( p_demux, "pid list:" );
831 for( i = 0; i < 8192; i++ )
833 ts_pid_t *pid = &p_sys->pid[i];
835 if( pid->b_valid && pid->psi )
840 dvbpsi_DetachPAT( pid->psi->handle );
848 dvbpsi_DetachDemux( pid->psi->handle );
852 PIDClean( p_demux->out, pid );
856 else if( pid->b_valid && pid->es )
858 PIDClean( p_demux->out, pid );
863 msg_Dbg( p_demux, " - pid[%d] seen", pid->i_pid );
866 if( p_sys->b_dvb_control && pid->i_pid > 0 )
869 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, pid->i_pid, VLC_FALSE );
874 if( p_sys->b_udp_out )
876 net_Close( p_sys->fd );
877 free( p_sys->buffer );
881 csa_Delete( p_sys->csa );
884 if( p_sys->i_pmt ) free( p_sys->pmt );
886 if ( p_sys->p_programs_list )
889 val.p_list = p_sys->p_programs_list;
890 var_Change( p_demux, "programs", VLC_VAR_FREELIST, &val, NULL );
893 /* If in dump mode, then close the file */
894 if( p_sys->b_file_out )
896 msg_Info( p_demux ,"closing %s ("I64Fd" Kbytes dumped)", p_sys->psz_file,
897 p_sys->i_write / 1024 );
899 if( p_sys->p_file != stdout )
901 fclose( p_sys->p_file );
902 p_sys->p_file = NULL;
905 free( p_sys->buffer );
908 free( p_sys->psz_file );
909 p_sys->psz_file = NULL;
914 /*****************************************************************************
916 *****************************************************************************/
917 static int DemuxFile( demux_t *p_demux )
919 demux_sys_t *p_sys = p_demux->p_sys;
920 uint8_t *p_buffer = p_sys->buffer; /* Put first on sync byte */
924 int i_bufsize = p_sys->i_packet_size * p_sys->i_ts_read;
926 i_data = stream_Read( p_demux->s, p_sys->buffer, i_bufsize );
927 if( (i_data <= 0) && (i_data < p_sys->i_packet_size) )
929 msg_Dbg( p_demux, "error reading malformed packets" );
933 /* Test continuity counter */
934 while( i_pos < i_data )
936 ts_pid_t *p_pid; /* point to a PID structure */
937 vlc_bool_t b_payload; /* indicates a packet with payload */
938 vlc_bool_t b_adaptation; /* adaptation field */
939 int i_cc = 0; /* continuity counter */
941 if( p_sys->buffer[i_pos] != 0x47 )
943 msg_Warn( p_demux, "lost sync" );
944 while( !p_demux->b_die && (i_pos < i_data) )
947 if( p_sys->buffer[i_pos] == 0x47 )
950 if( !p_demux->b_die )
951 msg_Warn( p_demux, "sync found" );
954 /* continuous when (one of this):
956 * diff == 0 and payload == 0
957 * diff == 0 and duplicate packet (playload != 0) <- should we
960 i_cc = p_buffer[i_pos+3]&0x0f;
961 b_payload = p_buffer[i_pos+3]&0x10;
962 b_adaptation = p_buffer[i_pos+3]&0x20;
965 p_pid = &p_sys->pid[ ((p_buffer[i_pos+1]&0x1f)<<8)|p_buffer[i_pos+2] ];
967 /* Detect discontinuity indicator in adaptation field */
970 if( p_buffer[i_pos+5]&0x80 )
971 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ", p_pid->i_pid );
972 if( p_buffer[i_pos+5]&0x40 )
973 msg_Warn( p_demux, "random access indicator (pid=%d) ", p_pid->i_pid );
976 i_diff = ( i_cc - p_pid->i_cc )&0x0f;
977 if( b_payload && i_diff == 1 )
979 p_pid->i_cc = ( p_pid->i_cc + 1 ) & 0xf;
983 if( p_pid->i_cc == 0xff )
985 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
986 p_pid->i_pid, i_cc );
989 else if( i_diff != 0 )
991 /* FIXME what to do when discontinuity_indicator is set ? */
992 msg_Warn( p_demux, "transport error detected 0x%x instead of 0x%x",
993 i_cc, ( p_pid->i_cc + 1 )&0x0f );
996 /* Mark transport error in the TS packet. */
997 p_buffer[i_pos+1] |= 0x80;
1001 /* Test if user wants to decrypt it first */
1003 csa_Decrypt( p_demux->p_sys->csa, &p_buffer[i_pos], p_demux->p_sys->i_csa_pkt_size );
1005 i_pos += p_sys->i_packet_size;
1009 i_data = fwrite( p_sys->buffer, 1, i_data, p_sys->p_file );
1012 msg_Err( p_demux, "failed to write data" );
1016 msg_Dbg( p_demux, "dumped %d bytes", i_data );
1019 p_sys->i_write += i_data;
1023 /*****************************************************************************
1025 *****************************************************************************/
1026 static int Demux( demux_t *p_demux )
1028 demux_sys_t *p_sys = p_demux->p_sys;
1031 /* We read at most 100 TS packet or until a frame is completed */
1032 for( i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
1034 vlc_bool_t b_frame = VLC_FALSE;
1038 /* Get a new TS packet */
1039 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1041 msg_Dbg( p_demux, "eof ?" );
1045 /* Check sync byte and re-sync if needed */
1046 if( p_pkt->p_buffer[0] != 0x47 )
1048 msg_Warn( p_demux, "lost synchro" );
1049 block_Release( p_pkt );
1051 while( !p_demux->b_die )
1054 int i_peek, i_skip = 0;
1056 i_peek = stream_Peek( p_demux->s, &p_peek,
1057 p_sys->i_packet_size * 10 );
1058 if( i_peek < p_sys->i_packet_size + 1 )
1060 msg_Dbg( p_demux, "eof ?" );
1064 while( i_skip < i_peek - p_sys->i_packet_size )
1066 if( p_peek[i_skip] == 0x47 &&
1067 p_peek[i_skip + p_sys->i_packet_size] == 0x47 )
1074 msg_Dbg( p_demux, "skipping %d bytes of garbage", i_skip );
1075 stream_Read( p_demux->s, NULL, i_skip );
1077 if( i_skip < i_peek - p_sys->i_packet_size )
1083 if( !( p_pkt = stream_Block( p_demux->s, p_sys->i_packet_size ) ) )
1085 msg_Dbg( p_demux, "eof ?" );
1090 if( p_sys->b_udp_out )
1092 memcpy( &p_sys->buffer[i_pkt * p_sys->i_packet_size],
1093 p_pkt->p_buffer, p_sys->i_packet_size );
1096 /* Parse the TS packet */
1097 p_pid = &p_sys->pid[PIDGet( p_pkt )];
1099 if( p_pid->b_valid )
1103 if( p_pid->i_pid == 0 || p_pid->i_pid == 0x11 || p_pid->i_pid == 0x12 )
1105 dvbpsi_PushPacket( p_pid->psi->handle, p_pkt->p_buffer );
1110 for( i_prg = 0; i_prg < p_pid->psi->i_prg; i_prg++ )
1112 dvbpsi_PushPacket( p_pid->psi->prg[i_prg]->handle,
1116 block_Release( p_pkt );
1118 else if( !p_sys->b_udp_out )
1120 b_frame = GatherPES( p_demux, p_pid, p_pkt );
1124 PCRHandle( p_demux, p_pid, p_pkt );
1125 block_Release( p_pkt );
1130 if( !p_pid->b_seen )
1132 msg_Dbg( p_demux, "pid[%d] unknown", p_pid->i_pid );
1134 /* We have to handle PCR if present */
1135 PCRHandle( p_demux, p_pid, p_pkt );
1136 block_Release( p_pkt );
1138 p_pid->b_seen = VLC_TRUE;
1146 if( p_sys->b_udp_out )
1148 /* Send the complete block */
1149 net_Write( p_demux, p_sys->fd, NULL, p_sys->buffer,
1150 p_sys->i_ts_read * p_sys->i_packet_size );
1156 /*****************************************************************************
1158 *****************************************************************************/
1159 static int Control( demux_t *p_demux, int i_query, va_list args )
1161 demux_sys_t *p_sys = p_demux->p_sys;
1167 if( p_sys->b_file_out )
1168 return demux2_vaControlHelper( p_demux->s, 0, -1, 0, 1, i_query, args );
1172 case DEMUX_GET_POSITION:
1173 pf = (double*) va_arg( args, double* );
1174 i64 = stream_Size( p_demux->s );
1177 *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
1184 case DEMUX_SET_POSITION:
1185 f = (double) va_arg( args, double );
1186 i64 = stream_Size( p_demux->s );
1188 es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
1189 if( stream_Seek( p_demux->s, (int64_t)(i64 * f) ) )
1191 return VLC_EGENERIC;
1196 case DEMUX_GET_TIME:
1197 pi64 = (int64_t*)va_arg( args, int64_t * );
1198 if( p_sys->i_time < 0 )
1201 return VLC_EGENERIC;
1203 *pi64 = p_sys->i_time;
1206 case DEMUX_GET_LENGTH:
1207 pi64 = (int64_t*)va_arg( args, int64_t * );
1208 if( p_sys->i_mux_rate > 0 )
1210 *pi64 = I64C(1000000) * ( stream_Size( p_demux->s ) / 50 ) /
1215 return VLC_EGENERIC;
1217 case DEMUX_GET_TIME:
1218 case DEMUX_GET_LENGTH:
1219 pi64 = (int64_t*)va_arg( args, int64_t * );
1223 case DEMUX_SET_GROUP:
1225 uint16_t i_vpid = 0, i_apid1 = 0, i_apid2 = 0, i_apid3 = 0;
1226 ts_prg_psi_t *p_prg = NULL;
1229 i_int = (int)va_arg( args, int );
1230 p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
1231 msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
1233 if( p_sys->b_dvb_control && i_int > 0 && i_int != p_sys->i_dvb_program )
1238 /* Search pmt to be unselected */
1239 for( i = 0; i < p_sys->i_pmt; i++ )
1241 ts_pid_t *pmt = p_sys->pmt[i];
1244 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1246 if( pmt->psi->prg[i_prg]->i_number == p_sys->i_dvb_program )
1248 i_pmt_pid = p_sys->pmt[i]->i_pid;
1252 if( i_pmt_pid > 0 ) break;
1257 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1258 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1261 for( i = 2; i < 8192; i++ )
1263 ts_pid_t *pid = &p_sys->pid[i];
1266 if( !pid->b_valid || pid->psi ) continue;
1268 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1270 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1272 /* We only remove es that aren't defined by extra pmt */
1273 stream_Control( p_demux->s,
1274 STREAM_CONTROL_ACCESS,
1275 ACCESS_SET_PRIVATE_ID_STATE,
1283 /* select new program */
1284 p_sys->i_dvb_program = i_int;
1286 for( i = 0; i < p_sys->i_pmt; i++ )
1288 ts_pid_t *pmt = p_sys->pmt[i];
1291 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
1293 if( pmt->psi->prg[i_prg]->i_number == i_int )
1295 i_pmt_pid = p_sys->pmt[i]->i_pid;
1296 p_prg = p_sys->pmt[i]->psi->prg[i_prg];
1300 if( i_pmt_pid > 0 ) break;
1304 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1305 ACCESS_SET_PRIVATE_ID_STATE, i_pmt_pid,
1307 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
1308 ACCESS_SET_PRIVATE_ID_STATE, p_prg->i_pid_pcr,
1311 for( i = 2; i < 8192; i++ )
1313 ts_pid_t *pid = &p_sys->pid[i];
1316 if( !pid->b_valid || pid->psi ) continue;
1318 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
1320 if( pid->p_owner->prg[i_prg]->i_pid_pmt == i_pmt_pid && pid->es->id )
1322 if ( pid->es->fmt.i_cat == VIDEO_ES && !i_vpid )
1324 if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid1 )
1326 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid2 )
1328 else if ( pid->es->fmt.i_cat == AUDIO_ES && !i_apid3 )
1331 stream_Control( p_demux->s,
1332 STREAM_CONTROL_ACCESS,
1333 ACCESS_SET_PRIVATE_ID_STATE,
1343 p_sys->i_dvb_program = -1;
1344 p_sys->p_programs_list = p_list;
1350 case DEMUX_SET_TIME:
1352 return VLC_EGENERIC;
1356 static void PIDInit( ts_pid_t *pid, vlc_bool_t b_psi, ts_psi_t *p_owner )
1358 vlc_bool_t b_old_valid = pid->b_valid;
1360 pid->b_valid = VLC_TRUE;
1362 pid->p_owner = p_owner;
1363 pid->i_owner_number = 0;
1365 pid->extra_es = NULL;
1366 pid->i_extra_es = 0;
1374 pid->psi = malloc( sizeof( ts_psi_t ) );
1375 pid->psi->handle= NULL;
1376 pid->psi->i_prg = 0;
1377 pid->psi->prg = NULL;
1379 pid->psi->i_pat_version = -1;
1380 pid->psi->i_sdt_version = -1;
1383 ts_prg_psi_t *prg = malloc( sizeof( ts_prg_psi_t ) );
1385 prg->i_version = -1;
1387 prg->i_pid_pcr = -1;
1388 prg->i_pid_pmt = -1;
1392 TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
1398 pid->es = malloc( sizeof( ts_es_t ) );
1400 es_format_Init( &pid->es->fmt, UNKNOWN_ES, 0 );
1402 pid->es->p_pes = NULL;
1403 pid->es->i_pes_size= 0;
1404 pid->es->i_pes_gathered= 0;
1405 pid->es->pp_last = &pid->es->p_pes;
1406 pid->es->p_mpeg4desc = NULL;
1407 pid->es->b_gather = VLC_FALSE;
1411 static void PIDClean( es_out_t *out, ts_pid_t *pid )
1417 if( pid->psi->handle ) dvbpsi_DetachPMT( pid->psi->handle );
1418 for( i = 0; i < pid->psi->i_prg; i++ )
1420 if( pid->psi->prg[i]->iod )
1421 IODFree( pid->psi->prg[i]->iod );
1422 if( pid->psi->prg[i]->handle )
1423 dvbpsi_DetachPMT( pid->psi->prg[i]->handle );
1424 free( pid->psi->prg[i] );
1426 if( pid->psi->prg ) free( pid->psi->prg );
1434 es_out_Del( out, pid->es->id );
1436 if( pid->es->p_pes )
1437 block_ChainRelease( pid->es->p_pes );
1439 es_format_Clean( &pid->es->fmt );
1443 for( i = 0; i < pid->i_extra_es; i++ )
1445 if( pid->extra_es[i]->id )
1446 es_out_Del( out, pid->extra_es[i]->id );
1448 if( pid->extra_es[i]->p_pes )
1449 block_ChainRelease( pid->extra_es[i]->p_pes );
1451 es_format_Clean( &pid->extra_es[i]->fmt );
1453 free( pid->extra_es[i] );
1455 if( pid->i_extra_es ) free( pid->extra_es );
1458 pid->b_valid = VLC_FALSE;
1461 /****************************************************************************
1463 ****************************************************************************/
1464 static void ParsePES( demux_t *p_demux, ts_pid_t *pid )
1466 block_t *p_pes = pid->es->p_pes;
1472 mtime_t i_length = 0;
1475 /* remove the pes from pid */
1476 pid->es->p_pes = NULL;
1477 pid->es->i_pes_size= 0;
1478 pid->es->i_pes_gathered= 0;
1479 pid->es->pp_last = &pid->es->p_pes;
1481 /* FIXME find real max size */
1482 i_max = block_ChainExtract( p_pes, header, 30 );
1485 if( header[0] != 0 || header[1] != 0 || header[2] != 1 )
1487 if( !p_demux->p_sys->b_silent )
1488 msg_Warn( p_demux, "invalid header [0x%x:%x:%x:%x] (pid: %d)",
1489 header[0], header[1],header[2],header[3], pid->i_pid );
1490 block_ChainRelease( p_pes );
1494 /* TODO check size */
1497 case 0xBC: /* Program stream map */
1498 case 0xBE: /* Padding */
1499 case 0xBF: /* Private stream 2 */
1500 case 0xB0: /* ECM */
1501 case 0xB1: /* EMM */
1502 case 0xFF: /* Program stream directory */
1503 case 0xF2: /* DSMCC stream */
1504 case 0xF8: /* ITU-T H.222.1 type E stream */
1508 if( ( header[6]&0xC0 ) == 0x80 )
1511 i_skip = header[8] + 9;
1513 if( header[7]&0x80 ) /* has pts */
1515 i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
1516 (mtime_t)(header[10] << 22)|
1517 ((mtime_t)(header[11]&0xfe) << 14)|
1518 (mtime_t)(header[12] << 7)|
1519 (mtime_t)(header[13] >> 1);
1521 if( header[7]&0x40 ) /* has dts */
1523 i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
1524 (mtime_t)(header[15] << 22)|
1525 ((mtime_t)(header[16]&0xfe) << 14)|
1526 (mtime_t)(header[17] << 7)|
1527 (mtime_t)(header[18] >> 1);
1534 while( i_skip < 23 && header[i_skip] == 0xff )
1540 msg_Err( p_demux, "too much MPEG-1 stuffing" );
1541 block_ChainRelease( p_pes );
1544 if( ( header[i_skip] & 0xC0 ) == 0x40 )
1549 if( header[i_skip]&0x20 )
1551 i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
1552 (mtime_t)(header[i_skip+1] << 22)|
1553 ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
1554 (mtime_t)(header[i_skip+3] << 7)|
1555 (mtime_t)(header[i_skip+4] >> 1);
1557 if( header[i_skip]&0x10 ) /* has dts */
1559 i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
1560 (mtime_t)(header[i_skip+6] << 22)|
1561 ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
1562 (mtime_t)(header[i_skip+8] << 7)|
1563 (mtime_t)(header[i_skip+9] >> 1);
1579 if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
1580 pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
1584 else if( pid->es->fmt.i_codec == VLC_FOURCC( 'l', 'p', 'c', 'b' ) ||
1585 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'p', 'u', 'b' ) ||
1586 pid->es->fmt.i_codec == VLC_FOURCC( 's', 'd', 'd', 'b' ) )
1590 else if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) &&
1591 pid->es->p_mpeg4desc )
1593 decoder_config_descriptor_t *dcd = &pid->es->p_mpeg4desc->dec_descr;
1595 if( dcd->i_decoder_specific_info_len > 2 &&
1596 dcd->p_decoder_specific_info[0] == 0x10 &&
1597 ( dcd->p_decoder_specific_info[1]&0x10 ) )
1599 /* display length */
1600 if( p_pes->i_buffer + 2 <= i_skip )
1602 i_length = GetWBE( &p_pes->p_buffer[i_skip] );
1607 if( p_pes->i_buffer + 2 <= i_skip )
1609 i_pes_size = GetWBE( &p_pes->p_buffer[i_skip] );
1616 while( p_pes && i_skip > 0 )
1618 if( p_pes->i_buffer <= i_skip )
1620 block_t *p_next = p_pes->p_next;
1622 i_skip -= p_pes->i_buffer;
1623 block_Release( p_pes );
1628 p_pes->i_buffer -= i_skip;
1629 p_pes->p_buffer += i_skip;
1634 /* ISO/IEC 13818-1 2.7.5: if no pts and no dts, then dts == pts */
1635 if( i_pts >= 0 && i_dts < 0 )
1645 p_pes->i_dts = i_dts * 100 / 9;
1649 p_pes->i_pts = i_pts * 100 / 9;
1651 p_pes->i_length = i_length * 100 / 9;
1653 p_block = block_ChainGather( p_pes );
1654 if( pid->es->fmt.i_codec == VLC_FOURCC( 's', 'u', 'b', 't' ) )
1656 if( i_pes_size > 0 && p_block->i_buffer > i_pes_size )
1658 p_block->i_buffer = i_pes_size;
1661 p_block = block_Realloc( p_block, 0, p_block->i_buffer + 1 );
1662 p_block->p_buffer[p_block->i_buffer -1] = '\0';
1665 for( i = 0; i < pid->i_extra_es; i++ )
1667 es_out_Send( p_demux->out, pid->extra_es[i]->id,
1668 block_Duplicate( p_block ) );
1671 es_out_Send( p_demux->out, pid->es->id, p_block );
1675 msg_Warn( p_demux, "empty pes" );
1679 static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1681 demux_sys_t *p_sys = p_demux->p_sys;
1682 const uint8_t *p = p_bk->p_buffer;
1684 if( ( p[3]&0x20 ) && /* adaptation */
1689 mtime_t i_pcr; /* 33 bits */
1691 i_pcr = ( (mtime_t)p[6] << 25 ) |
1692 ( (mtime_t)p[7] << 17 ) |
1693 ( (mtime_t)p[8] << 9 ) |
1694 ( (mtime_t)p[9] << 1 ) |
1695 ( (mtime_t)p[10] >> 7 );
1697 /* Search program and set the PCR */
1698 for( i = 0; i < p_sys->i_pmt; i++ )
1701 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
1703 if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
1705 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
1706 (int)p_sys->pmt[i]->psi->prg[i_prg]->i_number,
1707 (int64_t)(i_pcr * 100 / 9) );
1714 static vlc_bool_t GatherPES( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
1716 const uint8_t *p = p_bk->p_buffer;
1717 const vlc_bool_t b_unit_start = p[1]&0x40;
1718 const vlc_bool_t b_adaptation = p[3]&0x20;
1719 const vlc_bool_t b_payload = p[3]&0x10;
1720 const int i_cc = p[3]&0x0f; /* continuity counter */
1721 vlc_bool_t b_discontinuity = VLC_FALSE;/* discontinuity */
1723 /* transport_scrambling_control is ignored */
1725 vlc_bool_t i_ret = VLC_FALSE;
1729 msg_Dbg( p_demux, "pid=%d unit_start=%d adaptation=%d payload=%d "
1730 "cc=0x%x", pid->i_pid, b_unit_start, b_adaptation,
1734 /* For now, ignore additional error correction
1735 * TODO: handle Reed-Solomon 204,188 error correction */
1736 p_bk->i_buffer = TS_PACKET_SIZE_188;
1740 msg_Dbg( p_demux, "transport_error_indicator set (pid=%d)",
1742 if( pid->es->p_pes ) //&& pid->es->fmt.i_cat == VIDEO_ES )
1743 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1746 if( p_demux->p_sys->csa )
1748 csa_Decrypt( p_demux->p_sys->csa, p_bk->p_buffer, p_demux->p_sys->i_csa_pkt_size );
1753 /* We don't have any adaptation_field, so payload starts
1754 * immediately after the 4 byte TS header */
1759 /* p[4] is adaptation_field_length minus one */
1763 /* discontinuity indicator found in stream */
1764 b_discontinuity = (p[5]&0x80) ? VLC_TRUE : VLC_FALSE;
1765 if( b_discontinuity && pid->es->p_pes )
1767 msg_Warn( p_demux, "discontinuity indicator (pid=%d) ",
1769 /* pid->es->p_pes->i_flags |= BLOCK_FLAG_DISCONTINUITY; */
1773 msg_Dbg( p_demux, "random access indicator (pid=%d) ", pid->i_pid );
1778 /* Test continuity counter */
1779 /* continuous when (one of this):
1781 * diff == 0 and payload == 0
1782 * diff == 0 and duplicate packet (playload != 0) <- should we
1783 * test the content ?
1785 i_diff = ( i_cc - pid->i_cc )&0x0f;
1786 if( b_payload && i_diff == 1 )
1788 pid->i_cc = ( pid->i_cc + 1 ) & 0xf;
1792 if( pid->i_cc == 0xff )
1794 msg_Warn( p_demux, "first packet for pid=%d cc=0x%x",
1798 else if( i_diff != 0 && !b_discontinuity )
1800 msg_Warn( p_demux, "discontinuity received 0x%x instead of 0x%x (pid=%d)",
1801 i_cc, ( pid->i_cc + 1 )&0x0f, pid->i_pid );
1804 if( pid->es->p_pes && pid->es->fmt.i_cat != VIDEO_ES )
1806 /* Small video artifacts are usually better then
1807 * dropping full frames */
1808 pid->es->p_pes->i_flags |= BLOCK_FLAG_CORRUPTED;
1813 PCRHandle( p_demux, pid, p_bk );
1815 if( i_skip >= 188 || pid->es->id == NULL || p_demux->p_sys->b_udp_out )
1817 block_Release( p_bk );
1821 /* We have to gather it */
1822 p_bk->p_buffer += i_skip;
1823 p_bk->i_buffer -= i_skip;
1827 if( pid->es->p_pes )
1829 ParsePES( p_demux, pid );
1833 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1834 if( p_bk->i_buffer > 6 )
1836 pid->es->i_pes_size = GetWBE( &p_bk->p_buffer[4] );
1837 if( pid->es->i_pes_size > 0 )
1839 pid->es->i_pes_size += 6;
1842 pid->es->i_pes_gathered += p_bk->i_buffer;
1843 if( pid->es->i_pes_size > 0 &&
1844 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1846 ParsePES( p_demux, pid );
1852 if( pid->es->p_pes == NULL )
1854 /* msg_Dbg( p_demux, "broken packet" ); */
1855 block_Release( p_bk );
1859 block_ChainLastAppend( &pid->es->pp_last, p_bk );
1860 pid->es->i_pes_gathered += p_bk->i_buffer;
1861 if( pid->es->i_pes_size > 0 &&
1862 pid->es->i_pes_gathered >= pid->es->i_pes_size )
1864 ParsePES( p_demux, pid );
1873 static int PIDFillFormat( ts_pid_t *pid, int i_stream_type )
1875 es_format_t *fmt = &pid->es->fmt;
1877 switch( i_stream_type )
1879 case 0x01: /* MPEG-1 video */
1880 case 0x02: /* MPEG-2 video */
1881 case 0x80: /* MPEG-2 MOTO video */
1882 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', 'g', 'v' ) );
1884 case 0x03: /* MPEG-1 audio */
1885 case 0x04: /* MPEG-2 audio */
1886 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', 'g', 'a' ) );
1888 case 0x11: /* MPEG4 (audio) */
1889 case 0x0f: /* ISO/IEC 13818-7 Audio with ADTS transport syntax */
1890 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'm', 'p', '4', 'a' ) );
1892 case 0x10: /* MPEG4 (video) */
1893 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'm', 'p', '4', 'v' ) );
1894 pid->es->b_gather = VLC_TRUE;
1896 case 0x1B: /* H264 <- check transport syntax/needed descriptor */
1897 es_format_Init( fmt, VIDEO_ES, VLC_FOURCC( 'h', '2', '6', '4' ) );
1900 case 0x81: /* A52 (audio) */
1901 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', ' ' ) );
1903 case 0x82: /* DVD_SPU (sub) */
1904 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', ' ' ) );
1906 case 0x83: /* LPCM (audio) */
1907 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'l', 'p', 'c', 'm' ) );
1909 case 0x84: /* SDDS (audio) */
1910 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 's' ) );
1912 case 0x85: /* DTS (audio) */
1913 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'd', 't', 's', ' ' ) );
1916 case 0x91: /* A52 vls (audio) */
1917 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 'a', '5', '2', 'b' ) );
1919 case 0x92: /* DVD_SPU vls (sub) */
1920 es_format_Init( fmt, SPU_ES, VLC_FOURCC( 's', 'p', 'u', 'b' ) );
1923 case 0x94: /* SDDS (audio) */
1924 es_format_Init( fmt, AUDIO_ES, VLC_FOURCC( 's', 'd', 'd', 'b' ) );
1927 case 0xa0: /* MSCODEC vlc (video) (fixed later) */
1928 es_format_Init( fmt, UNKNOWN_ES, 0 );
1929 pid->es->b_gather = VLC_TRUE;
1932 case 0x06: /* PES_PRIVATE (fixed later) */
1933 case 0x12: /* MPEG-4 generic (sub/scene/...) (fixed later) */
1935 es_format_Init( fmt, UNKNOWN_ES, 0 );
1939 /* PES packets usually contain truncated frames */
1940 fmt->b_packetized = VLC_FALSE;
1942 return fmt->i_cat == UNKNOWN_ES ? VLC_EGENERIC : VLC_SUCCESS ;
1945 /*****************************************************************************
1946 * MP4 specific functions (IOD parser)
1947 *****************************************************************************/
1948 static int IODDescriptorLength( int *pi_data, uint8_t **pp_data )
1951 unsigned int i_len = 0;
1957 i_len = ( i_len << 7 ) + ( i_b&0x7f );
1959 } while( i_b&0x80 );
1963 static int IODGetByte( int *pi_data, uint8_t **pp_data )
1967 const int i_b = **pp_data;
1974 static int IODGetWord( int *pi_data, uint8_t **pp_data )
1976 const int i1 = IODGetByte( pi_data, pp_data );
1977 const int i2 = IODGetByte( pi_data, pp_data );
1978 return( ( i1 << 8 ) | i2 );
1980 static int IODGet3Bytes( int *pi_data, uint8_t **pp_data )
1982 const int i1 = IODGetByte( pi_data, pp_data );
1983 const int i2 = IODGetByte( pi_data, pp_data );
1984 const int i3 = IODGetByte( pi_data, pp_data );
1986 return( ( i1 << 16 ) | ( i2 << 8) | i3 );
1989 static uint32_t IODGetDWord( int *pi_data, uint8_t **pp_data )
1991 const uint32_t i1 = IODGetWord( pi_data, pp_data );
1992 const uint32_t i2 = IODGetWord( pi_data, pp_data );
1993 return( ( i1 << 16 ) | i2 );
1996 static char* IODGetURL( int *pi_data, uint8_t **pp_data )
2001 i_url_len = IODGetByte( pi_data, pp_data );
2002 url = malloc( i_url_len + 1 );
2003 for( i = 0; i < i_url_len; i++ )
2005 url[i] = IODGetByte( pi_data, pp_data );
2007 url[i_url_len] = '\0';
2011 static iod_descriptor_t *IODNew( int i_data, uint8_t *p_data )
2013 iod_descriptor_t *p_iod;
2016 uint8_t i_flags, i_iod_tag, byte1, byte2, byte3;
2020 p_iod = malloc( sizeof( iod_descriptor_t ) );
2021 memset( p_iod, 0, sizeof( iod_descriptor_t ) );
2024 fprintf( stderr, "\n************ IOD ************" );
2026 for( i = 0; i < 255; i++ )
2028 p_iod->es_descr[i].b_ok = 0;
2037 byte1 = IODGetByte( &i_data, &p_data );
2038 byte2 = IODGetByte( &i_data, &p_data );
2039 byte3 = IODGetByte( &i_data, &p_data );
2040 if( byte2 == 0x02 ) //old vlc's buggy implementation of the IOD_descriptor
2042 p_iod->i_iod_label_scope = 0x11;
2043 p_iod->i_iod_label = byte1;
2046 else //correct implementation of the IOD_descriptor
2048 p_iod->i_iod_label_scope = byte1;
2049 p_iod->i_iod_label = byte2;
2053 fprintf( stderr, "\n* iod_label:%d", p_iod->i_iod_label );
2054 fprintf( stderr, "\n* ===========" );
2055 fprintf( stderr, "\n* tag:0x%x", i_iod_tag );
2057 if( i_iod_tag != 0x02 )
2060 fprintf( stderr, "\n ERR: tag %02x != 0x02", i_iod_tag );
2065 i_iod_length = IODDescriptorLength( &i_data, &p_data );
2067 fprintf( stderr, "\n* length:%d", i_iod_length );
2069 if( i_iod_length > i_data )
2071 i_iod_length = i_data;
2074 p_iod->i_od_id = ( IODGetByte( &i_data, &p_data ) << 2 );
2075 i_flags = IODGetByte( &i_data, &p_data );
2076 p_iod->i_od_id |= i_flags >> 6;
2077 b_url = ( i_flags >> 5 )&0x01;
2079 fprintf( stderr, "\n* od_id:%d", p_iod->i_od_id );
2080 fprintf( stderr, "\n* url flag:%d", b_url );
2081 fprintf( stderr, "\n* includeInlineProfileLevel flag:%d", ( i_flags >> 4 )&0x01 );
2085 p_iod->psz_url = IODGetURL( &i_data, &p_data );
2087 fprintf( stderr, "\n* url string:%s", p_iod->psz_url );
2088 fprintf( stderr, "\n*****************************\n" );
2094 p_iod->psz_url = NULL;
2097 p_iod->i_ODProfileLevelIndication = IODGetByte( &i_data, &p_data );
2098 p_iod->i_sceneProfileLevelIndication = IODGetByte( &i_data, &p_data );
2099 p_iod->i_audioProfileLevelIndication = IODGetByte( &i_data, &p_data );
2100 p_iod->i_visualProfileLevelIndication = IODGetByte( &i_data, &p_data );
2101 p_iod->i_graphicsProfileLevelIndication = IODGetByte( &i_data, &p_data );
2103 fprintf( stderr, "\n* ODProfileLevelIndication:%d", p_iod->i_ODProfileLevelIndication );
2104 fprintf( stderr, "\n* sceneProfileLevelIndication:%d", p_iod->i_sceneProfileLevelIndication );
2105 fprintf( stderr, "\n* audioProfileLevelIndication:%d", p_iod->i_audioProfileLevelIndication );
2106 fprintf( stderr, "\n* visualProfileLevelIndication:%d", p_iod->i_visualProfileLevelIndication );
2107 fprintf( stderr, "\n* graphicsProfileLevelIndication:%d", p_iod->i_graphicsProfileLevelIndication );
2110 while( i_data > 0 && i_es_index < 255)
2112 int i_tag, i_length;
2114 uint8_t *p_data_sav;
2116 i_tag = IODGetByte( &i_data, &p_data );
2117 i_length = IODDescriptorLength( &i_data, &p_data );
2119 i_data_sav = i_data;
2120 p_data_sav = p_data;
2128 #define es_descr p_iod->es_descr[i_es_index]
2129 int i_decoderConfigDescr_length;
2131 fprintf( stderr, "\n* - ES_Descriptor length:%d", i_length );
2135 es_descr.i_es_id = IODGetWord( &i_data, &p_data );
2136 i_flags = IODGetByte( &i_data, &p_data );
2137 es_descr.b_streamDependenceFlag = ( i_flags >> 7 )&0x01;
2138 b_url = ( i_flags >> 6 )&0x01;
2139 es_descr.b_OCRStreamFlag = ( i_flags >> 5 )&0x01;
2140 es_descr.i_streamPriority = i_flags & 0x1f;
2142 fprintf( stderr, "\n* * streamDependenceFlag:%d", es_descr.b_streamDependenceFlag );
2143 fprintf( stderr, "\n* * OCRStreamFlag:%d", es_descr.b_OCRStreamFlag );
2144 fprintf( stderr, "\n* * streamPriority:%d", es_descr.i_streamPriority );
2146 if( es_descr.b_streamDependenceFlag )
2148 es_descr.i_dependOn_es_id = IODGetWord( &i_data, &p_data );
2150 fprintf( stderr, "\n* * dependOn_es_id:%d", es_descr.i_dependOn_es_id );
2156 es_descr.psz_url = IODGetURL( &i_data, &p_data );
2158 fprintf( stderr, "\n* url string:%s", es_descr.psz_url );
2163 es_descr.psz_url = NULL;
2166 if( es_descr.b_OCRStreamFlag )
2168 es_descr.i_OCR_es_id = IODGetWord( &i_data, &p_data );
2170 fprintf( stderr, "\n* * OCR_es_id:%d", es_descr.i_OCR_es_id );
2174 if( IODGetByte( &i_data, &p_data ) != 0x04 )
2177 fprintf( stderr, "\n* ERR missing DecoderConfigDescr" );
2182 i_decoderConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2184 fprintf( stderr, "\n* - DecoderConfigDesc length:%d", i_decoderConfigDescr_length );
2186 #define dec_descr es_descr.dec_descr
2187 dec_descr.i_objectTypeIndication = IODGetByte( &i_data, &p_data );
2188 i_flags = IODGetByte( &i_data, &p_data );
2189 dec_descr.i_streamType = i_flags >> 2;
2190 dec_descr.b_upStream = ( i_flags >> 1 )&0x01;
2191 dec_descr.i_bufferSizeDB = IODGet3Bytes( &i_data, &p_data );
2192 dec_descr.i_maxBitrate = IODGetDWord( &i_data, &p_data );
2193 dec_descr.i_avgBitrate = IODGetDWord( &i_data, &p_data );
2195 fprintf( stderr, "\n* * objectTypeIndication:0x%x", dec_descr.i_objectTypeIndication );
2196 fprintf( stderr, "\n* * streamType:0x%x", dec_descr.i_streamType );
2197 fprintf( stderr, "\n* * upStream:%d", dec_descr.b_upStream );
2198 fprintf( stderr, "\n* * bufferSizeDB:%d", dec_descr.i_bufferSizeDB );
2199 fprintf( stderr, "\n* * maxBitrate:%d", dec_descr.i_maxBitrate );
2200 fprintf( stderr, "\n* * avgBitrate:%d", dec_descr.i_avgBitrate );
2202 if( i_decoderConfigDescr_length > 13 && IODGetByte( &i_data, &p_data ) == 0x05 )
2205 dec_descr.i_decoder_specific_info_len =
2206 IODDescriptorLength( &i_data, &p_data );
2207 if( dec_descr.i_decoder_specific_info_len > 0 )
2209 dec_descr.p_decoder_specific_info =
2210 malloc( dec_descr.i_decoder_specific_info_len );
2212 for( i = 0; i < dec_descr.i_decoder_specific_info_len; i++ )
2214 dec_descr.p_decoder_specific_info[i] = IODGetByte( &i_data, &p_data );
2219 dec_descr.i_decoder_specific_info_len = 0;
2220 dec_descr.p_decoder_specific_info = NULL;
2224 #define sl_descr es_descr.sl_descr
2226 int i_SLConfigDescr_length;
2229 if( IODGetByte( &i_data, &p_data ) != 0x06 )
2232 fprintf( stderr, "\n* ERR missing SLConfigDescr" );
2237 i_SLConfigDescr_length = IODDescriptorLength( &i_data, &p_data );
2239 fprintf( stderr, "\n* - SLConfigDescr length:%d", i_SLConfigDescr_length );
2241 i_predefined = IODGetByte( &i_data, &p_data );
2243 fprintf( stderr, "\n* * i_predefined:0x%x", i_predefined );
2245 switch( i_predefined )
2249 sl_descr.b_useAccessUnitStartFlag = 0;
2250 sl_descr.b_useAccessUnitEndFlag = 0;
2251 sl_descr.b_useRandomAccessPointFlag = 0;
2252 //sl_descr.b_useRandomAccessUnitsOnlyFlag = 0;
2253 sl_descr.b_usePaddingFlag = 0;
2254 sl_descr.b_useTimeStampsFlags = 0;
2255 sl_descr.b_useIdleFlag = 0;
2256 sl_descr.b_durationFlag = 0; // FIXME FIXME
2257 sl_descr.i_timeStampResolution = 1000;
2258 sl_descr.i_OCRResolution = 0; // FIXME FIXME
2259 sl_descr.i_timeStampLength = 32;
2260 sl_descr.i_OCRLength = 0; // FIXME FIXME
2261 sl_descr.i_AU_Length = 0;
2262 sl_descr.i_instantBitrateLength= 0; // FIXME FIXME
2263 sl_descr.i_degradationPriorityLength= 0;
2264 sl_descr.i_AU_seqNumLength = 0;
2265 sl_descr.i_packetSeqNumLength = 0;
2266 if( sl_descr.b_durationFlag )
2268 sl_descr.i_timeScale = 0; // FIXME FIXME
2269 sl_descr.i_accessUnitDuration = 0; // FIXME FIXME
2270 sl_descr.i_compositionUnitDuration= 0; // FIXME FIXME
2272 if( !sl_descr.b_useTimeStampsFlags )
2274 sl_descr.i_startDecodingTimeStamp = 0; // FIXME FIXME
2275 sl_descr.i_startCompositionTimeStamp= 0; // FIXME FIXME
2281 fprintf( stderr, "\n* ERR unsupported SLConfigDescr predefined" );
2292 fprintf( stderr, "\n* - OD tag:0x%x length:%d (Unsupported)", i_tag, i_length );
2297 p_data = p_data_sav + i_length;
2298 i_data = i_data_sav - i_length;
2302 fprintf( stderr, "\n*****************************\n" );
2307 static void IODFree( iod_descriptor_t *p_iod )
2311 if( p_iod->psz_url )
2313 free( p_iod->psz_url );
2314 p_iod->psz_url = NULL;
2319 for( i = 0; i < 255; i++ )
2321 #define es_descr p_iod->es_descr[i]
2324 if( es_descr.psz_url )
2326 free( es_descr.psz_url );
2327 es_descr.psz_url = NULL;
2331 if( es_descr.dec_descr.p_decoder_specific_info != NULL )
2333 free( es_descr.dec_descr.p_decoder_specific_info );
2334 es_descr.dec_descr.p_decoder_specific_info = NULL;
2335 es_descr.dec_descr.i_decoder_specific_info_len = 0;
2345 /****************************************************************************
2346 ****************************************************************************
2347 ** libdvbpsi callbacks
2348 ****************************************************************************
2349 ****************************************************************************/
2350 static vlc_bool_t DVBProgramIsSelected( demux_t *p_demux, uint16_t i_pgrm )
2352 demux_sys_t *p_sys = p_demux->p_sys;
2354 if ( !p_sys->b_dvb_control ) return VLC_FALSE;
2355 if ( (p_sys->i_dvb_program == -1 && p_sys->p_programs_list == NULL)
2356 || p_sys->i_dvb_program == 0 )
2358 if ( p_sys->i_dvb_program == i_pgrm ) return VLC_TRUE;
2360 if ( p_sys->p_programs_list != NULL )
2363 for ( i = 0; i < p_sys->p_programs_list->i_count; i++ )
2365 if ( i_pgrm == p_sys->p_programs_list->p_values[i].i_int )
2372 #ifdef TS_USE_DVB_SI
2373 static void SDTCallBack( demux_t *p_demux, dvbpsi_sdt_t *p_sdt )
2375 demux_sys_t *p_sys = p_demux->p_sys;
2376 ts_pid_t *sdt = &p_sys->pid[0x11];
2377 dvbpsi_sdt_service_t *p_srv;
2379 msg_Dbg( p_demux, "SDTCallBack called" );
2381 if( sdt->psi->i_sdt_version != -1 &&
2382 ( !p_sdt->b_current_next ||
2383 p_sdt->i_version == sdt->psi->i_sdt_version ) )
2385 dvbpsi_DeleteSDT( p_sdt );
2389 msg_Dbg( p_demux, "new SDT ts_id=%d version=%d current_next=%d "
2391 p_sdt->i_ts_id, p_sdt->i_version, p_sdt->b_current_next,
2392 p_sdt->i_network_id );
2394 for( p_srv = p_sdt->p_first_service; p_srv; p_srv = p_srv->p_next )
2396 vlc_meta_t *p_meta = vlc_meta_New();
2397 dvbpsi_descriptor_t *p_dr;
2399 msg_Dbg( p_demux, " * service id=%d eit schedule=%d present=%d "
2400 "running=%d free_ca=%d",
2401 p_srv->i_service_id, p_srv->b_eit_schedule,
2402 p_srv->b_eit_present, p_srv->i_running_status,
2405 for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2407 if( p_dr->i_tag == 0x48 )
2410 static const char *psz_type[0x11] = {
2412 "Digital television service",
2413 "Digital radio sound service",
2415 "NVOD reference service",
2416 "NVOD time-shifted service",
2419 "SECAM coded signal",
2422 "NTSC coded signal",
2423 "Data broadcast service",
2424 "Reserved for Common Interface Usage",
2425 "RCS Map (see EN 301 790 [35])",
2426 "RCS FLS (see EN 301 790 [35])",
2430 dvbpsi_service_dr_t *pD = dvbpsi_DecodeServiceDr( p_dr );
2434 memcpy( str1, pD->i_service_provider_name,
2435 pD->i_service_provider_name_length );
2436 str1[pD->i_service_provider_name_length] = '\0';
2437 memcpy( str2, pD->i_service_name, pD->i_service_name_length );
2438 str2[pD->i_service_name_length] = '\0';
2440 msg_Dbg( p_demux, " - type=%d provider=%s name=%s",
2441 pD->i_service_type, str1, str2 );
2443 vlc_meta_SetTitle( p_meta, str2 );
2444 vlc_meta_SetPublisher( p_meta, str1 );
2445 if( pD->i_service_type >= 0x01 && pD->i_service_type <= 0x10 )
2446 vlc_meta_Add( p_meta, "Type", psz_type[pD->i_service_type] );
2450 if( p_srv->i_running_status == 0x01 )
2451 vlc_meta_Add( p_meta, "Status", "Not running" );
2452 else if( p_srv->i_running_status == 0x02 )
2453 vlc_meta_Add( p_meta, "Status", "Starts in a few seconds" );
2454 else if( p_srv->i_running_status == 0x03 )
2455 vlc_meta_Add( p_meta, "Status", "Pausing" );
2456 else if( p_srv->i_running_status == 0x04 )
2457 vlc_meta_Add( p_meta, "Status", "Running" );
2459 vlc_meta_Add( p_meta, "Status", "Unknown" );
2461 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2462 p_srv->i_service_id, p_meta );
2463 vlc_meta_Delete( p_meta );
2466 sdt->psi->i_sdt_version = p_sdt->i_version;
2467 dvbpsi_DeleteSDT( p_sdt );
2470 static void DecodeMjd( int i_mjd, int *p_y, int *p_m, int *p_d )
2472 int yp = (int)( ( (double)i_mjd - 15078.2)/365.25 );
2473 int mp = (int)( ((double)i_mjd - 14956.1 - (int)(yp * 365.25)) / 30.6001 );
2475 *p_d = i_mjd - 14956 - (int)(yp*365.25) - (int)(mp*30.6001);
2477 if( mp == 14 || mp == 15 )
2489 static void EITEventFixString( unsigned char *psz )
2492 /* Sometimes the first char isn't a normal char but designed
2493 * caracters encoding, for now lets skip it */
2494 if( psz[0] >= 0x20 )
2496 if( ( i_len = strlen( (char *) psz ) ) > 0 )
2497 memmove( &psz[0], &psz[1], i_len ); /* Copy the \0 too */
2499 static void EITCallBack( demux_t *p_demux, dvbpsi_eit_t *p_eit )
2501 dvbpsi_eit_event_t *p_evt;
2503 vlc_bool_t b_event_active = VLC_FALSE;
2505 msg_Dbg( p_demux, "EITCallBack called" );
2506 if( !p_eit->b_current_next )
2508 dvbpsi_DeleteEIT( p_eit );
2512 msg_Dbg( p_demux, "new EIT service_id=%d version=%d current_next=%d "
2513 "ts_id=%d network_id=%d segment_last_section_number=%d "
2515 p_eit->i_service_id, p_eit->i_version, p_eit->b_current_next,
2516 p_eit->i_ts_id, p_eit->i_network_id,
2517 p_eit->i_segment_last_section_number, p_eit->i_last_table_id );
2519 p_meta = vlc_meta_New();
2520 for( p_evt = p_eit->p_first_event; p_evt; p_evt = p_evt->p_next )
2522 dvbpsi_descriptor_t *p_dr;
2523 char *psz_cat = malloc( strlen("Event")+10 );
2525 char psz_duration[15];
2528 char *psz_extra = strdup("");
2531 sprintf( psz_cat, "Event %d", p_evt->i_event_id );
2532 sprintf( psz_start, "%d%d:%d%d:%d%d",
2533 (int)(p_evt->i_start_time >> 20)&0xf,
2534 (int)(p_evt->i_start_time >> 16)&0xf,
2535 (int)(p_evt->i_start_time >> 12)&0xf,
2536 (int)(p_evt->i_start_time >> 8)&0xf,
2537 (int)(p_evt->i_start_time >> 4)&0xf,
2538 (int)(p_evt->i_start_time )&0xf );
2539 sprintf( psz_duration, "%d%d:%d%d:%d%d",
2540 (p_evt->i_duration >> 20)&0xf, (p_evt->i_duration >> 16)&0xf,
2541 (p_evt->i_duration >> 12)&0xf, (p_evt->i_duration >> 8)&0xf,
2542 (p_evt->i_duration >> 4)&0xf, (p_evt->i_duration )&0xf );
2543 psz_name[0] = psz_text[0] = '\0';
2545 msg_Dbg( p_demux, " * event id=%d start_time:mjd=%d %s duration=%s "
2546 "running=%d free_ca=%d",
2548 (int)(p_evt->i_start_time >> 24),
2549 psz_start, psz_duration,
2550 p_evt->i_running_status, p_evt->b_free_ca );
2552 for( p_dr = p_evt->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
2554 if( p_dr->i_tag == 0x4d )
2556 dvbpsi_short_event_dr_t *pE = dvbpsi_DecodeShortEventDr( p_dr );
2560 memcpy( psz_name, pE->i_event_name, pE->i_event_name_length);
2561 psz_name[pE->i_event_name_length] = '\0';
2562 memcpy( psz_text, pE->i_text, pE->i_text_length );
2563 psz_text[pE->i_text_length] = '\0';
2565 EITEventFixString((unsigned char *)&psz_name);
2566 EITEventFixString((unsigned char *)&psz_text);
2567 msg_Dbg( p_demux, " - short event lang=%3.3s '%s' : '%s'",
2568 pE->i_iso_639_code, psz_name, psz_text );
2571 else if( p_dr->i_tag == 0x4e )
2573 dvbpsi_extended_event_dr_t *pE = dvbpsi_DecodeExtendedEventDr( p_dr );
2580 msg_Dbg( p_demux, " - extended event lang=%3.3s",
2581 pE->i_iso_639_code );
2582 for( i = 0; i < pE->i_entry_count; i++ )
2584 memcpy( str1, pE->i_item_description[i],
2585 pE->i_item_description_length[i] );
2586 str1[pE->i_item_description_length[i]] = '\0';
2587 EITEventFixString((unsigned char *)&str1);
2589 memcpy( str2, pE->i_item[i],
2590 pE->i_item_length[i] );
2591 str2[pE->i_item_length[i]] = '\0';
2592 EITEventFixString((unsigned char *)&str2);
2594 msg_Dbg( p_demux, " - desc='%s' item='%s'", str1, str2 );
2595 psz_extra = realloc( psz_extra,
2597 strlen(str1) +strlen(str2) + 1 + 3 );
2598 strcat( psz_extra, str1 );
2599 strcat( psz_extra, "(" );
2600 strcat( psz_extra, str2 );
2601 strcat( psz_extra, ") " );
2604 memcpy( str1, pE->i_text, pE->i_text_length );
2605 str1[pE->i_text_length] = '\0';
2606 EITEventFixString((unsigned char *)&str1);
2608 msg_Dbg( p_demux, " - text='%s'", str1 );
2609 psz_extra = realloc( psz_extra,
2610 strlen(psz_extra) + strlen(str1) + 2 );
2611 strcat( psz_extra, str1 );
2612 strcat( psz_extra, " " );
2617 msg_Dbg( p_demux, " - tag=0x%x(%d)", p_dr->i_tag, p_dr->i_tag );
2621 asprintf( &psz_value, "%s: %s (+%s) %s (%s)",
2625 psz_text, psz_extra );
2626 vlc_meta_Add( p_meta, psz_cat, psz_value );
2629 if( p_evt->i_running_status == 0x04 )
2631 vlc_meta_SetNowPlaying( p_meta, psz_name );
2632 b_event_active = VLC_TRUE;
2639 if( !b_event_active )
2640 vlc_meta_SetNowPlaying( p_meta, "" );
2641 es_out_Control( p_demux->out, ES_OUT_SET_GROUP_META,
2642 p_eit->i_service_id, p_meta );
2643 vlc_meta_Delete( p_meta );
2645 dvbpsi_DeleteEIT( p_eit );
2648 static void PSINewTableCallBack( demux_t *p_demux, dvbpsi_handle h,
2649 uint8_t i_table_id, uint16_t i_extension )
2652 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2653 i_table_id, i_table_id, i_extension, i_extension );
2656 if( i_table_id == 0x42 )
2658 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2659 i_table_id, i_table_id, i_extension, i_extension );
2661 dvbpsi_AttachSDT( h, i_table_id, i_extension,
2662 (dvbpsi_sdt_callback)SDTCallBack, p_demux );
2664 else if( i_table_id == 0x4e || /* Current/Following */
2665 ( i_table_id >= 0x50 && i_table_id <= 0x5f ) ) /* Schedule */
2667 msg_Dbg( p_demux, "PSINewTableCallBack: table 0x%x(%d) ext=0x%x(%d)",
2668 i_table_id, i_table_id, i_extension, i_extension );
2670 dvbpsi_AttachEIT( h, i_table_id, i_extension,
2671 (dvbpsi_eit_callback)EITCallBack, p_demux );
2676 static void PMTCallBack( demux_t *p_demux, dvbpsi_pmt_t *p_pmt )
2678 demux_sys_t *p_sys = p_demux->p_sys;
2679 dvbpsi_descriptor_t *p_dr;
2680 dvbpsi_pmt_es_t *p_es;
2682 ts_pid_t *pmt = NULL;
2683 ts_prg_psi_t *prg = NULL;
2685 ts_pid_t **pp_clean = NULL;
2688 msg_Dbg( p_demux, "PMTCallBack called" );
2690 /* First find this PMT declared in PAT */
2691 for( i = 0; i < p_sys->i_pmt; i++ )
2694 for( i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
2696 if( p_sys->pmt[i]->psi->prg[i_prg]->i_number ==
2697 p_pmt->i_program_number )
2699 pmt = p_sys->pmt[i];
2700 prg = p_sys->pmt[i]->psi->prg[i_prg];
2709 msg_Warn( p_demux, "unreferenced program (broken stream)" );
2710 dvbpsi_DeletePMT(p_pmt);
2714 if( prg->i_version != -1 &&
2715 ( !p_pmt->b_current_next || prg->i_version == p_pmt->i_version ) )
2717 dvbpsi_DeletePMT( p_pmt );
2721 /* Clean this program (remove all es) */
2722 for( i = 0; i < 8192; i++ )
2724 ts_pid_t *pid = &p_sys->pid[i];
2726 if( pid->b_valid && pid->p_owner == pmt->psi &&
2727 pid->i_owner_number == prg->i_number && pid->psi == NULL )
2729 TAB_APPEND( i_clean, pp_clean, pid );
2734 IODFree( prg->iod );
2738 msg_Dbg( p_demux, "new PMT program number=%d version=%d pid_pcr=%d",
2739 p_pmt->i_program_number, p_pmt->i_version, p_pmt->i_pcr_pid );
2740 prg->i_pid_pcr = p_pmt->i_pcr_pid;
2741 prg->i_version = p_pmt->i_version;
2743 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
2745 /* Set demux filter */
2746 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
2747 ACCESS_SET_PRIVATE_ID_STATE, prg->i_pid_pcr,
2750 else if ( p_sys->b_dvb_control )
2752 msg_Warn( p_demux, "skipping program (not selected)" );
2753 dvbpsi_DeletePMT(p_pmt);
2757 /* Parse descriptor */
2758 for( p_dr = p_pmt->p_first_descriptor; p_dr != NULL; p_dr = p_dr->p_next )
2760 if( p_dr->i_tag == 0x1d )
2762 /* We have found an IOD descriptor */
2763 msg_Dbg( p_demux, " * descriptor : IOD (0x1d)" );
2765 prg->iod = IODNew( p_dr->i_length, p_dr->p_data );
2767 else if( p_dr->i_tag == 0x9 )
2769 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
2771 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x", i_sysid );
2775 msg_Dbg( p_demux, " * descriptor : unknown (0x%x)", p_dr->i_tag );
2779 for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
2781 ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
2783 /* Find out if the PID was already declared */
2784 for( i = 0; i < i_clean; i++ )
2786 if( pp_clean[i] == &p_sys->pid[p_es->i_pid] )
2788 old_pid = pp_clean[i];
2793 if( !old_pid && p_sys->pid[p_es->i_pid].b_valid )
2795 ts_pid_t *pid = &p_sys->pid[p_es->i_pid];
2796 if( ( pid->i_pid == 0x11 /* SDT */ ||
2797 pid->i_pid == 0x12 /* EDT */ ) && pid->psi )
2799 /* This doesn't look like a DVB stream so don't try
2800 * parsing the SDT/EDT */
2801 dvbpsi_DetachDemux( pid->psi->handle );
2807 msg_Warn( p_demux, "pmt error: pid=%d already defined",
2813 PIDInit( pid, VLC_FALSE, pmt->psi );
2814 PIDFillFormat( pid, p_es->i_type );
2815 pid->i_owner_number = prg->i_number;
2816 pid->i_pid = p_es->i_pid;
2817 pid->b_seen = p_sys->pid[p_es->i_pid].b_seen;
2819 if( p_es->i_type == 0x10 || p_es->i_type == 0x11 ||
2820 p_es->i_type == 0x12 )
2822 /* MPEG-4 stream: search SL_DESCRIPTOR */
2823 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;;
2825 while( p_dr && ( p_dr->i_tag != 0x1f ) ) p_dr = p_dr->p_next;
2827 if( p_dr && p_dr->i_length == 2 )
2829 int i_es_id = ( p_dr->p_data[0] << 8 ) | p_dr->p_data[1];
2831 msg_Warn( p_demux, "found SL_descriptor es_id=%d", i_es_id );
2833 pid->es->p_mpeg4desc = NULL;
2835 for( i = 0; i < 255; i++ )
2837 iod_descriptor_t *iod = prg->iod;
2839 if( iod->es_descr[i].b_ok &&
2840 iod->es_descr[i].i_es_id == i_es_id )
2842 pid->es->p_mpeg4desc = &iod->es_descr[i];
2848 if( pid->es->p_mpeg4desc != NULL )
2850 decoder_config_descriptor_t *dcd =
2851 &pid->es->p_mpeg4desc->dec_descr;
2853 if( dcd->i_streamType == 0x04 ) /* VisualStream */
2855 pid->es->fmt.i_cat = VIDEO_ES;
2856 switch( dcd->i_objectTypeIndication )
2858 case 0x0B: /* mpeg4 sub */
2859 pid->es->fmt.i_cat = SPU_ES;
2860 pid->es->fmt.i_codec = VLC_FOURCC('s','u','b','t');
2863 case 0x20: /* mpeg4 */
2864 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','v');
2866 case 0x21: /* h264 */
2867 pid->es->fmt.i_codec = VLC_FOURCC('h','2','6','4');
2874 case 0x65: /* mpeg2 */
2875 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2877 case 0x6a: /* mpeg1 */
2878 pid->es->fmt.i_codec = VLC_FOURCC( 'm','p','g','v' );
2880 case 0x6c: /* mpeg1 */
2881 pid->es->fmt.i_codec = VLC_FOURCC( 'j','p','e','g' );
2884 pid->es->fmt.i_cat = UNKNOWN_ES;
2888 else if( dcd->i_streamType == 0x05 ) /* AudioStream */
2890 pid->es->fmt.i_cat = AUDIO_ES;
2891 switch( dcd->i_objectTypeIndication )
2893 case 0x40: /* mpeg4 */
2894 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2898 case 0x68: /* mpeg2 aac */
2899 pid->es->fmt.i_codec = VLC_FOURCC('m','p','4','a');
2901 case 0x69: /* mpeg2 */
2902 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2904 case 0x6b: /* mpeg1 */
2905 pid->es->fmt.i_codec = VLC_FOURCC('m','p','g','a');
2908 pid->es->fmt.i_cat = UNKNOWN_ES;
2914 pid->es->fmt.i_cat = UNKNOWN_ES;
2917 if( pid->es->fmt.i_cat != UNKNOWN_ES )
2919 pid->es->fmt.i_extra = dcd->i_decoder_specific_info_len;
2920 if( pid->es->fmt.i_extra > 0 )
2922 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
2923 memcpy( pid->es->fmt.p_extra,
2924 dcd->p_decoder_specific_info,
2925 pid->es->fmt.i_extra );
2930 else if( p_es->i_type == 0x06 )
2932 dvbpsi_descriptor_t *p_dr;
2934 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
2935 p_dr = p_dr->p_next )
2937 msg_Dbg( p_demux, " * es pid=%d type=%d dr->i_tag=0x%x",
2938 p_es->i_pid, p_es->i_type, p_dr->i_tag );
2940 if( p_dr->i_tag == 0x05 )
2942 /* Registration Descriptor */
2943 if( p_dr->i_length != 4 )
2945 msg_Warn( p_demux, "invalid Registration Descriptor" );
2949 if( !memcmp( p_dr->p_data, "AC-3", 4 ) )
2951 /* ATSC with stream_type 0x81 (but this descriptor
2952 * is then not mandatory */
2953 pid->es->fmt.i_cat = AUDIO_ES;
2954 pid->es->fmt.i_codec = VLC_FOURCC('a','5','2',' ');
2956 else if( !memcmp( p_dr->p_data, "DTS1", 4 ) ||
2957 !memcmp( p_dr->p_data, "DTS2", 4 ) ||
2958 !memcmp( p_dr->p_data, "DTS3", 4 ) )
2960 /*registration descriptor(ETSI TS 101 154 Annex F)*/
2961 pid->es->fmt.i_cat = AUDIO_ES;
2962 pid->es->fmt.i_codec = VLC_FOURCC('d','t','s',' ');
2964 else if( !memcmp( p_dr->p_data, "BSSD", 4 ) )
2966 pid->es->fmt.i_cat = AUDIO_ES;
2967 pid->es->fmt.i_codec = VLC_FOURCC('l','p','c','m');
2972 "unknown Registration Descriptor (%4.4s)",
2978 else if( p_dr->i_tag == 0x6a )
2980 /* DVB with stream_type 0x06 */
2981 pid->es->fmt.i_cat = AUDIO_ES;
2982 pid->es->fmt.i_codec = VLC_FOURCC( 'a', '5', '2', ' ' );
2984 else if( p_dr->i_tag == 0x73 )
2986 /* DTS audio descriptor (ETSI TS 101 154 Annex F) */
2987 msg_Dbg( p_demux, " * DTS audio descriptor not decoded" );
2988 pid->es->fmt.i_cat = AUDIO_ES;
2989 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 't', 's', ' ' );
2991 else if( p_dr->i_tag == 0x45 )
2993 msg_Dbg( p_demux, " * VBI Data descriptor" );
2994 /* FIXME : store the information somewhere */
2996 else if( p_dr->i_tag == 0x46 )
2998 msg_Dbg( p_demux, " * VBI Teletext descriptor" );
2999 /* FIXME : store the information somewhere */
3001 #ifdef _DVBPSI_DR_52_H_
3002 else if( p_dr->i_tag == 0x52 )
3004 dvbpsi_stream_identifier_dr_t *si;
3005 si = dvbpsi_DecodeStreamIdentifierDr( p_dr );
3007 msg_Dbg( p_demux, " * Stream Component Identifier: %d", si->i_component_tag );
3010 else if( p_dr->i_tag == 0x56 )
3012 msg_Dbg( p_demux, " * EBU Teletext descriptor" );
3013 pid->es->fmt.i_cat = SPU_ES;
3014 pid->es->fmt.i_codec = VLC_FOURCC( 't', 'e', 'l', 'x' );
3015 pid->es->fmt.psz_description = strdup( "Teletext" );
3016 pid->es->fmt.i_extra = p_dr->i_length;
3017 pid->es->fmt.p_extra = malloc( p_dr->i_length );
3018 memcpy( pid->es->fmt.p_extra, p_dr->p_data,
3021 #ifdef _DVBPSI_DR_59_H_
3022 else if( p_dr->i_tag == 0x59 )
3025 dvbpsi_subtitling_dr_t *sub;
3028 pid->es->fmt.i_cat = SPU_ES;
3029 pid->es->fmt.i_codec = VLC_FOURCC( 'd', 'v', 'b', 's' );
3030 pid->es->fmt.i_group = p_pmt->i_program_number;
3032 sub = dvbpsi_DecodeSubtitlingDr( p_dr );
3033 if( !sub ) continue;
3035 /* Each subtitle ES contains n languages,
3036 * We are going to create n ES for the n tracks */
3037 if( sub->i_subtitles_number > 0 )
3039 pid->es->fmt.psz_language = malloc( 4 );
3040 memcpy( pid->es->fmt.psz_language,
3041 sub->p_subtitle[0].i_iso6392_language_code, 3);
3042 pid->es->fmt.psz_language[3] = 0;
3044 pid->es->fmt.subs.dvb.i_id =
3045 sub->p_subtitle[0].i_composition_page_id;
3047 pid->es->fmt.subs.dvb.i_id |=
3048 ((int)sub->p_subtitle[0].i_ancillary_page_id << 16);
3050 else pid->es->fmt.i_cat = UNKNOWN_ES;
3052 for( n = 1; n < sub->i_subtitles_number; n++ )
3054 ts_es_t *p_es = malloc( sizeof( ts_es_t ) );
3055 p_es->fmt = pid->es->fmt;
3058 p_es->i_pes_size = 0;
3059 p_es->i_pes_gathered = 0;
3060 p_es->pp_last = &p_es->p_pes;
3061 p_es->p_mpeg4desc = NULL;
3063 p_es->fmt.psz_language = malloc( 4 );
3064 memcpy( p_es->fmt.psz_language,
3065 sub->p_subtitle[n].i_iso6392_language_code, 3);
3066 p_es->fmt.psz_language[3] = 0;
3068 p_es->fmt.subs.dvb.i_id =
3069 sub->p_subtitle[n].i_composition_page_id;
3071 p_es->fmt.subs.dvb.i_id |=
3072 ((int)sub->p_subtitle[n].i_ancillary_page_id << 16);
3074 TAB_APPEND( pid->i_extra_es, pid->extra_es, p_es );
3077 #endif /* _DVBPSI_DR_59_H_ */
3080 else if( p_es->i_type == 0xa0 )
3082 /* MSCODEC sent by vlc */
3083 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
3085 while( p_dr && ( p_dr->i_tag != 0xa0 ) ) p_dr = p_dr->p_next;
3087 if( p_dr && p_dr->i_length >= 8 )
3089 pid->es->fmt.i_cat = VIDEO_ES;
3090 pid->es->fmt.i_codec =
3091 VLC_FOURCC( p_dr->p_data[0], p_dr->p_data[1],
3092 p_dr->p_data[2], p_dr->p_data[3] );
3093 pid->es->fmt.video.i_width =
3094 ( p_dr->p_data[4] << 8 ) | p_dr->p_data[5];
3095 pid->es->fmt.video.i_height =
3096 ( p_dr->p_data[6] << 8 ) | p_dr->p_data[7];
3097 pid->es->fmt.i_extra =
3098 (p_dr->p_data[8] << 8) | p_dr->p_data[9];
3100 if( pid->es->fmt.i_extra > 0 )
3102 pid->es->fmt.p_extra = malloc( pid->es->fmt.i_extra );
3103 memcpy( pid->es->fmt.p_extra, &p_dr->p_data[10],
3104 pid->es->fmt.i_extra );
3109 msg_Warn( p_demux, "private MSCODEC (vlc) without bih private "
3112 /* For such stream we will gather them ourself and don't launch a
3114 * Yes it's ugly but it's the only way to have DIV3 working */
3115 pid->es->fmt.b_packetized = VLC_TRUE;
3118 if( pid->es->fmt.i_cat == AUDIO_ES ||
3119 ( pid->es->fmt.i_cat == SPU_ES &&
3120 pid->es->fmt.i_codec != VLC_FOURCC('d','v','b','s') ) )
3122 /* get language descriptor */
3123 dvbpsi_descriptor_t *p_dr = p_es->p_first_descriptor;
3124 while( p_dr && ( p_dr->i_tag != 0x0a ) ) p_dr = p_dr->p_next;
3128 dvbpsi_iso639_dr_t *p_decoded = dvbpsi_DecodeISO639Dr( p_dr );
3132 #if DR_0A_API_VER >= 2
3133 pid->es->fmt.psz_language = malloc( 4 );
3134 memcpy( pid->es->fmt.psz_language,
3135 p_decoded->code[0].iso_639_code, 3 );
3136 pid->es->fmt.psz_language[3] = 0;
3137 msg_Dbg( p_demux, "found language: %s", pid->es->fmt.psz_language);
3138 switch( p_decoded->code[0].i_audio_type ) {
3140 pid->es->fmt.psz_description = NULL;
3143 pid->es->fmt.psz_description =
3144 strdup(_("clean effects"));
3147 pid->es->fmt.psz_description =
3148 strdup(_("hearing impaired"));
3151 pid->es->fmt.psz_description =
3152 strdup(_("visual impaired commentary"));
3155 msg_Dbg( p_demux, "unknown audio type: %d",
3156 p_decoded->code[0].i_audio_type);
3157 pid->es->fmt.psz_description = NULL;
3160 pid->es->fmt.i_extra_languages = p_decoded->i_code_count-1;
3161 pid->es->fmt.p_extra_languages =
3162 malloc( sizeof(*pid->es->fmt.p_extra_languages) *
3163 pid->es->fmt.i_extra_languages );
3164 for( i = 0; i < pid->es->fmt.i_extra_languages; i++ ) {
3165 msg_Dbg( p_demux, "bang" );
3166 pid->es->fmt.p_extra_languages[i].psz_language =
3168 memcpy(pid->es->fmt.p_extra_languages[i].psz_language,
3169 p_decoded->code[i+1].iso_639_code, 3 );
3170 pid->es->fmt.p_extra_languages[i].psz_language[3] = '\0';
3171 switch( p_decoded->code[i].i_audio_type ) {
3173 pid->es->fmt.p_extra_languages[i].psz_description =
3177 pid->es->fmt.p_extra_languages[i].psz_description =
3178 strdup(_("clean effects"));
3181 pid->es->fmt.p_extra_languages[i].psz_description =
3182 strdup(_("hearing impaired"));
3185 pid->es->fmt.p_extra_languages[i].psz_description =
3186 strdup(_("visual impaired commentary"));
3189 msg_Dbg( p_demux, "unknown audio type: %d",
3190 p_decoded->code[i].i_audio_type);
3191 pid->es->fmt.psz_description = NULL;
3197 pid->es->fmt.psz_language = malloc( 4 );
3198 memcpy( pid->es->fmt.psz_language,
3199 p_decoded->i_iso_639_code, 3 );
3200 pid->es->fmt.psz_language[3] = 0;
3206 pid->es->fmt.i_group = p_pmt->i_program_number;
3207 if( pid->es->fmt.i_cat == UNKNOWN_ES )
3209 msg_Dbg( p_demux, " * es pid=%d type=%d *unknown*",
3210 p_es->i_pid, p_es->i_type );
3212 else if( !p_sys->b_udp_out )
3214 msg_Dbg( p_demux, " * es pid=%d type=%d fcc=%4.4s",
3215 p_es->i_pid, p_es->i_type, (char*)&pid->es->fmt.i_codec );
3217 if( p_sys->b_es_id_pid ) pid->es->fmt.i_id = p_es->i_pid;
3219 /* Check if we can avoid restarting the ES */
3221 pid->es->fmt.i_codec == old_pid->es->fmt.i_codec &&
3222 pid->es->fmt.i_extra == old_pid->es->fmt.i_extra &&
3223 pid->es->fmt.i_extra == 0 &&
3224 pid->i_extra_es == old_pid->i_extra_es &&
3225 ( ( !pid->es->fmt.psz_language &&
3226 !old_pid->es->fmt.psz_language ) ||
3227 ( pid->es->fmt.psz_language &&
3228 old_pid->es->fmt.psz_language &&
3229 !strcmp( pid->es->fmt.psz_language,
3230 old_pid->es->fmt.psz_language ) ) ) )
3232 pid->es->id = old_pid->es->id;
3233 old_pid->es->id = NULL;
3234 for( i = 0; i < pid->i_extra_es; i++ )
3236 pid->extra_es[i]->id = old_pid->extra_es[i]->id;
3237 old_pid->extra_es[i]->id = NULL;
3244 PIDClean( p_demux->out, old_pid );
3245 TAB_REMOVE( i_clean, pp_clean, old_pid );
3249 pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
3250 for( i = 0; i < pid->i_extra_es; i++ )
3252 pid->extra_es[i]->id =
3253 es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
3258 /* Add ES to the list */
3261 PIDClean( p_demux->out, old_pid );
3262 TAB_REMOVE( i_clean, pp_clean, old_pid );
3264 p_sys->pid[p_es->i_pid] = *pid;
3266 for( p_dr = p_es->p_first_descriptor; p_dr != NULL;
3267 p_dr = p_dr->p_next )
3269 if( p_dr->i_tag == 0x9 )
3271 uint16_t i_sysid = ((uint16_t)p_dr->p_data[0] << 8)
3273 msg_Dbg( p_demux, " * descriptor : CA (0x9) SysID 0x%x",
3278 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3280 /* Set demux filter */
3281 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3282 ACCESS_SET_PRIVATE_ID_STATE, p_es->i_pid,
3287 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3289 /* Set CAM descrambling */
3290 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3291 ACCESS_SET_PRIVATE_ID_CA, p_pmt );
3295 dvbpsi_DeletePMT( p_pmt );
3298 for ( i = 0; i < i_clean; i++ )
3300 if( DVBProgramIsSelected( p_demux, prg->i_number ) )
3302 stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3303 ACCESS_SET_PRIVATE_ID_STATE, pp_clean[i]->i_pid,
3307 PIDClean( p_demux->out, pp_clean[i] );
3309 if( i_clean ) free( pp_clean );
3312 static void PATCallBack( demux_t *p_demux, dvbpsi_pat_t *p_pat )
3314 demux_sys_t *p_sys = p_demux->p_sys;
3315 dvbpsi_pat_program_t *p_program;
3316 ts_pid_t *pat = &p_sys->pid[0];
3319 msg_Dbg( p_demux, "PATCallBack called" );
3321 if( pat->psi->i_pat_version != -1 &&
3322 ( !p_pat->b_current_next ||
3323 p_pat->i_version == pat->psi->i_pat_version ) )
3325 dvbpsi_DeletePAT( p_pat );
3329 msg_Dbg( p_demux, "new PAT ts_id=%d version=%d current_next=%d",
3330 p_pat->i_ts_id, p_pat->i_version, p_pat->b_current_next );
3333 if( p_sys->i_pmt > 0 )
3336 ts_pid_t **pmt_rm = NULL;
3338 /* Search pmt to be deleted */
3339 for( i = 0; i < p_sys->i_pmt; i++ )
3341 ts_pid_t *pmt = p_sys->pmt[i];
3342 vlc_bool_t b_keep = VLC_FALSE;
3344 for( p_program = p_pat->p_first_program; p_program != NULL;
3345 p_program = p_program->p_next )
3347 if( p_program->i_pid == pmt->i_pid )
3350 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
3352 if( p_program->i_number ==
3353 pmt->psi->prg[i_prg]->i_number )
3365 TAB_APPEND( i_pmt_rm, pmt_rm, pmt );
3369 /* Delete all ES attached to thoses PMT */
3370 for( i = 2; i < 8192; i++ )
3372 ts_pid_t *pid = &p_sys->pid[i];
3374 if( !pid->b_valid || pid->psi ) continue;
3376 for( j = 0; j < i_pmt_rm; j++ )
3379 for( i_prg = 0; i_prg < pid->p_owner->i_prg; i_prg++ )
3381 /* We only remove es that aren't defined by extra pmt */
3382 if( pid->p_owner->prg[i_prg]->i_pid_pmt !=
3383 pmt_rm[j]->i_pid ) continue;
3385 if( p_sys->b_dvb_control && pid->es->id )
3387 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3388 ACCESS_SET_PRIVATE_ID_STATE, i,
3390 p_sys->b_dvb_control = VLC_FALSE;
3393 PIDClean( p_demux->out, pid );
3397 if( !pid->b_valid ) break;
3401 /* Delete PMT pid */
3402 for( i = 0; i < i_pmt_rm; i++ )
3405 if( p_sys->b_dvb_control )
3407 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS,
3408 ACCESS_SET_PRIVATE_ID_STATE,
3409 pmt_rm[i]->i_pid, VLC_FALSE ) )
3410 p_sys->b_dvb_control = VLC_FALSE;
3413 for( i_prg = 0; i_prg < pmt_rm[i]->psi->i_prg; i_prg++ )
3415 const int i_number = pmt_rm[i]->psi->prg[i_prg]->i_number;
3417 es_out_Control( p_demux->out, ES_OUT_DEL_GROUP, i_number );
3420 PIDClean( p_demux->out, &p_sys->pid[pmt_rm[i]->i_pid] );
3421 TAB_REMOVE( p_sys->i_pmt, p_sys->pmt, pmt_rm[i] );
3424 if( pmt_rm ) free( pmt_rm );
3427 /* now create programs */
3428 for( p_program = p_pat->p_first_program; p_program != NULL;
3429 p_program = p_program->p_next )
3431 msg_Dbg( p_demux, " * number=%d pid=%d", p_program->i_number,
3433 if( p_program->i_number != 0 )
3435 ts_pid_t *pmt = &p_sys->pid[p_program->i_pid];
3436 vlc_bool_t b_add = VLC_TRUE;
3441 for( i_prg = 0; i_prg < pmt->psi->i_prg; i_prg++ )
3443 if( pmt->psi->prg[i_prg]->i_number == p_program->i_number )
3452 TAB_APPEND( p_sys->i_pmt, p_sys->pmt, pmt );
3457 PIDInit( pmt, VLC_TRUE, pat->psi );
3458 pmt->psi->prg[pmt->psi->i_prg-1]->handle =
3459 dvbpsi_AttachPMT( p_program->i_number,
3460 (dvbpsi_pmt_callback)PMTCallBack,
3462 pmt->psi->prg[pmt->psi->i_prg-1]->i_number =
3463 p_program->i_number;
3464 pmt->psi->prg[pmt->psi->i_prg-1]->i_pid_pmt =
3467 /* Now select PID at access level */
3468 if( p_sys->b_dvb_control )
3470 if( DVBProgramIsSelected( p_demux, p_program->i_number ) )
3472 if( p_sys->i_dvb_program == 0 )
3473 p_sys->i_dvb_program = p_program->i_number;
3475 if( stream_Control( p_demux->s, STREAM_CONTROL_ACCESS, ACCESS_SET_PRIVATE_ID_STATE, p_program->i_pid, VLC_TRUE ) )
3476 p_sys->b_dvb_control = VLC_FALSE;
3482 pat->psi->i_pat_version = p_pat->i_version;
3484 dvbpsi_DeletePAT( p_pat );