# include <dvbpsi/tot.h>
#include "../mux/mpeg/dvbpsi_compat.h"
+#include "../mux/mpeg/streams.h"
+#include "../mux/mpeg/tsutil.h"
+#include "../mux/mpeg/tables.h"
#include "../codec/opus_header.h"
int i_number;
int i_pid_pcr;
int i_pid_pmt;
- mtime_t i_pcr_value;
/* IOD stuff (mpeg4) */
iod_descriptor_t *iod;
+ struct
+ {
+ mtime_t i_current;
+ mtime_t i_first; // seen <> != -1
+ /* broken PCR handling */
+ mtime_t i_first_dts;
+ mtime_t i_pcroffset;
+ bool b_disable; /* ignore PCR field, use dts */
+ } pcr;
+
+ mtime_t i_last_dts;
+
} ts_prg_psi_t;
typedef struct
es_mpeg4_descriptor_t *p_mpeg4desc;
+ block_t * p_prepcr_outqueue;
} ts_es_t;
typedef struct
ts_es_t **extra_es;
int i_extra_es;
+ struct
+ {
+ vlc_fourcc_t i_fourcc;
+ int i_type;
+ int i_pcr_count;
+ } probed;
+
} ts_pid_t;
+typedef struct
+{
+ int i_service;
+} vdr_info_t;
+
+#define MIN_ES_PID 4 /* Should be 32.. broken muxers */
+#define MAX_ES_PID 8190
+
+#define FROM_SCALE(x) (VLC_TS_0 + ((x) * 100 / 9))
+#define TO_SCALE(x) (((x) - VLC_TS_0) * 9 / 100)
+
struct demux_sys_t
{
stream_t *stream;
bool b_canseek;
+ bool b_canfastseek;
vlc_mutex_t csa_lock;
/* TS packet size (188, 192, 204) */
/* how many TS packet we read at once */
int i_ts_read;
- /* to determine length and time */
- int i_pid_ref_pcr;
- mtime_t i_first_pcr;
- mtime_t i_current_pcr;
- mtime_t i_last_pcr;
bool b_force_seek_per_percent;
- int i_pcrs_num;
- mtime_t *p_pcrs;
- int64_t *p_pos;
struct
{
ts_pid_t **pmt;
int i_pmt_es;
+ enum
+ {
+ NO_ES, /* for preparse */
+ DELAY_ES,
+ CREATE_ES
+ } es_creation;
+ #define PREPARSING p_sys->es_creation == NO_ES
+
/* */
bool b_es_id_pid;
csa_t *csa;
/* */
bool b_access_control;
+ bool b_end_preparse;
/* */
bool b_dvb_meta;
int64_t i_dvb_length;
bool b_broken_charset; /* True if broken encoding is used in EPG/SDT */
- /* */
- int i_current_program;
- vlc_list_t programs_list;
+ /* Selected programs */
+ DECL_ARRAY( int ) programs; /* List of selected/access-filtered programs */
+ bool b_default_selection; /* True if set by default to first pmt seen (to get data from filtered access) */
+
+ struct
+ {
+ mtime_t i_first_dts; /* first dts encountered for the stream */
+ int i_timesourcepid; /* which pid we saved the dts from */
+ bool b_pat_deadline; /* set if we haven't seen PAT within 140ms */
+ } patfix;
+
+ vdr_info_t vdr;
/* */
bool b_start_record;
static int ChangeKeyCallback( vlc_object_t *, char const *, vlc_value_t, vlc_value_t, void * );
+
+/* Helpers */
+static ts_prg_psi_t * GetProgramByID( demux_sys_t *, int i_program );
static inline int PIDGet( block_t *p )
{
return ( (p->p_buffer[1]&0x1f)<<8 )|p->p_buffer[2];
}
static bool GatherData( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk );
+static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid );
+static void ProgramSetPCR( demux_t *p_demux, ts_prg_psi_t *p_prg, mtime_t i_pcr );
static block_t* ReadTSPacket( demux_t *p_demux );
-static int Seek( demux_t *p_demux, double f_percent );
-static void GetFirstPCR( demux_t *p_demux );
-static void GetLastPCR( demux_t *p_demux );
-static void CheckPCR( demux_t *p_demux );
+static int ProbeStart( demux_t *p_demux, int i_program );
+static int ProbeEnd( demux_t *p_demux, int i_program );
+static int SeekToTime( demux_t *p_demux, ts_prg_psi_t *, int64_t time );
+static void ReadyQueuesPostSeek( demux_sys_t *p_sys );
static void PCRHandle( demux_t *p_demux, ts_pid_t *, block_t * );
+static void PCRFixHandle( demux_t *, ts_prg_psi_t *, block_t * );
+static int64_t TimeStampWrapAround( ts_prg_psi_t *, int64_t );
static void IODFree( iod_descriptor_t * );
#define TS_PACKET_SIZE_192 192
#define TS_PACKET_SIZE_204 204
#define TS_PACKET_SIZE_MAX 204
+#define TS_HEADER_SIZE 4
-static int DetectPacketSize( demux_t *p_demux, int *pi_header_size )
+static int DetectPacketSize( demux_t *p_demux, int *pi_header_size, int i_offset )
{
const uint8_t *p_peek;
+
if( stream_Peek( p_demux->s,
- &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
+ &p_peek, i_offset + TS_PACKET_SIZE_MAX ) < i_offset + TS_PACKET_SIZE_MAX )
return -1;
- *pi_header_size = 0;
-
- if( memcmp( p_peek, "TFrc", 4 ) == 0 )
- {
-#if 0
- /* I used the TF5000PVR 2004 Firmware .doc header documentation,
- * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
- * but after the filename the offsets seem to be incorrect. - DJ */
- int i_duration, i_name;
- char *psz_name = xmalloc(25);
- char *psz_event_name;
- char *psz_event_text = xmalloc(130);
- char *psz_ext_text = xmalloc(1025);
-
- // 2 bytes version Uimsbf (4,5)
- // 2 bytes reserved (6,7)
- // 2 bytes duration in minutes Uimsbf (8,9(
- i_duration = (int) (p_peek[8] << 8) | p_peek[9];
- msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
- // 2 bytes service number in channel list (10, 11)
- // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
- // 4 bytes of reserved + tuner info (14,15,16,17)
- // 2 bytes of Service ID Bslbf (18,19)
- // 2 bytes of PMT PID Uimsbf (20,21)
- // 2 bytes of PCR PID Uimsbf (22,23)
- // 2 bytes of Video PID Uimsbf (24,25)
- // 2 bytes of Audio PID Uimsbf (26,27)
- // 24 bytes filename Bslbf
- memcpy( psz_name, &p_peek[28], 24 );
- psz_name[24] = '\0';
- msg_Dbg( p_demux, "recordingname=%s", psz_name );
- // 1 byte of sat index Uimsbf (52)
- // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
- // 4 bytes of freq. Uimsbf (56,57,58,59)
- // 2 bytes of symbol rate Uimsbf (60,61)
- // 2 bytes of TS stream ID Uimsbf (62,63)
- // 4 bytes reserved
- // 2 bytes reserved
- // 2 bytes duration Uimsbf (70,71)
- //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
- //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
- // 4 bytes EventID Uimsbf (72-75)
- // 8 bytes of Start and End time info (76-83)
- // 1 byte reserved (84)
- // 1 byte event name length Uimsbf (89)
- i_name = (int)(p_peek[89]&~0x81);
- msg_Dbg( p_demux, "event name length = %d", i_name);
- psz_event_name = xmalloc( i_name+1 );
- // 1 byte parental rating (90)
- // 129 bytes of event text
- memcpy( psz_event_name, &p_peek[91], i_name );
- psz_event_name[i_name] = '\0';
- memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
- psz_event_text[129-i_name] = '\0';
- msg_Dbg( p_demux, "event name=%s", psz_event_name );
- msg_Dbg( p_demux, "event text=%s", psz_event_text );
- // 12 bytes reserved (220)
- // 6 bytes reserved
- // 2 bytes Event Text Length Uimsbf
- // 4 bytes EventID Uimsbf
- // FIXME We just have 613 bytes. not enough for this entire text
- // 1024 bytes Extended Event Text Bslbf
- memcpy( psz_ext_text, p_peek+372, 1024 );
- psz_ext_text[1024] = '\0';
- msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
- // 52 bytes reserved Bslbf
-#endif
- msg_Dbg( p_demux, "this is a topfield file" );
- return TS_PACKET_SIZE_188;
- }
-
for( int i_sync = 0; i_sync < TS_PACKET_SIZE_MAX; i_sync++ )
{
- if( p_peek[i_sync] != 0x47 )
+ if( p_peek[i_offset + i_sync] != 0x47 )
continue;
/* Check next 3 sync bytes */
- int i_peek = TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
+ int i_peek = i_offset + TS_PACKET_SIZE_MAX * 3 + i_sync + 1;
if( ( stream_Peek( p_demux->s, &p_peek, i_peek ) ) < i_peek )
{
msg_Err( p_demux, "cannot peek" );
return -1;
}
- if( p_peek[i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
- p_peek[i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
- p_peek[i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
+ if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_188] == 0x47 &&
+ p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_188] == 0x47 &&
+ p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_188] == 0x47 )
{
return TS_PACKET_SIZE_188;
}
- else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
- p_peek[i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
- p_peek[i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
+ else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_192] == 0x47 &&
+ p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_192] == 0x47 &&
+ p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_192] == 0x47 )
{
if( i_sync == 4 )
{
}
return TS_PACKET_SIZE_192;
}
- else if( p_peek[i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
- p_peek[i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
- p_peek[i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
+ else if( p_peek[i_offset + i_sync + 1 * TS_PACKET_SIZE_204] == 0x47 &&
+ p_peek[i_offset + i_sync + 2 * TS_PACKET_SIZE_204] == 0x47 &&
+ p_peek[i_offset + i_sync + 3 * TS_PACKET_SIZE_204] == 0x47 )
{
return TS_PACKET_SIZE_204;
}
return -1;
}
+#define TOPFIELD_HEADER_SIZE 3712
+
+static int DetectPVRHeadersAndHeaderSize( demux_t *p_demux, int *pi_header_size, vdr_info_t *p_vdr )
+{
+ const uint8_t *p_peek;
+ *pi_header_size = 0;
+ int i_packet_size = -1;
+
+ if( stream_Peek( p_demux->s,
+ &p_peek, TS_PACKET_SIZE_MAX ) < TS_PACKET_SIZE_MAX )
+ return -1;
+
+ if( memcmp( p_peek, "TFrc", 4 ) == 0 &&
+ p_peek[6] == 0 && memcmp( &p_peek[53], "\x80\x00\x00", 4 ) == 0 &&
+ stream_Peek( p_demux->s, &p_peek, TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
+ == TOPFIELD_HEADER_SIZE + TS_PACKET_SIZE_MAX )
+ {
+ i_packet_size = DetectPacketSize( p_demux, pi_header_size, TOPFIELD_HEADER_SIZE );
+ if( i_packet_size != -1 )
+ {
+ msg_Dbg( p_demux, "this is a topfield file" );
+#if 0
+ /* I used the TF5000PVR 2004 Firmware .doc header documentation,
+ * http://www.i-topfield.com/data/product/firmware/Structure%20of%20Recorded%20File%20in%20TF5000PVR%20(Feb%2021%202004).doc
+ * but after the filename the offsets seem to be incorrect. - DJ */
+ int i_duration, i_name;
+ char *psz_name = xmalloc(25);
+ char *psz_event_name;
+ char *psz_event_text = xmalloc(130);
+ char *psz_ext_text = xmalloc(1025);
+
+ // 2 bytes version Uimsbf (4,5)
+ // 2 bytes reserved (6,7)
+ // 2 bytes duration in minutes Uimsbf (8,9(
+ i_duration = (int) (p_peek[8] << 8) | p_peek[9];
+ msg_Dbg( p_demux, "Topfield recording length: +/- %d minutes", i_duration);
+ // 2 bytes service number in channel list (10, 11)
+ // 2 bytes service type Bslbf 0=TV 1=Radio Bslb (12, 13)
+ // 4 bytes of reserved + tuner info (14,15,16,17)
+ // 2 bytes of Service ID Bslbf (18,19)
+ // 2 bytes of PMT PID Uimsbf (20,21)
+ // 2 bytes of PCR PID Uimsbf (22,23)
+ // 2 bytes of Video PID Uimsbf (24,25)
+ // 2 bytes of Audio PID Uimsbf (26,27)
+ // 24 bytes filename Bslbf
+ memcpy( psz_name, &p_peek[28], 24 );
+ psz_name[24] = '\0';
+ msg_Dbg( p_demux, "recordingname=%s", psz_name );
+ // 1 byte of sat index Uimsbf (52)
+ // 3 bytes (1 bit of polarity Bslbf +23 bits reserved)
+ // 4 bytes of freq. Uimsbf (56,57,58,59)
+ // 2 bytes of symbol rate Uimsbf (60,61)
+ // 2 bytes of TS stream ID Uimsbf (62,63)
+ // 4 bytes reserved
+ // 2 bytes reserved
+ // 2 bytes duration Uimsbf (70,71)
+ //i_duration = (int) (p_peek[70] << 8) | p_peek[71];
+ //msg_Dbg( p_demux, "Topfield 2nd duration field: +/- %d minutes", i_duration);
+ // 4 bytes EventID Uimsbf (72-75)
+ // 8 bytes of Start and End time info (76-83)
+ // 1 byte reserved (84)
+ // 1 byte event name length Uimsbf (89)
+ i_name = (int)(p_peek[89]&~0x81);
+ msg_Dbg( p_demux, "event name length = %d", i_name);
+ psz_event_name = xmalloc( i_name+1 );
+ // 1 byte parental rating (90)
+ // 129 bytes of event text
+ memcpy( psz_event_name, &p_peek[91], i_name );
+ psz_event_name[i_name] = '\0';
+ memcpy( psz_event_text, &p_peek[91+i_name], 129-i_name );
+ psz_event_text[129-i_name] = '\0';
+ msg_Dbg( p_demux, "event name=%s", psz_event_name );
+ msg_Dbg( p_demux, "event text=%s", psz_event_text );
+ // 12 bytes reserved (220)
+ // 6 bytes reserved
+ // 2 bytes Event Text Length Uimsbf
+ // 4 bytes EventID Uimsbf
+ // FIXME We just have 613 bytes. not enough for this entire text
+ // 1024 bytes Extended Event Text Bslbf
+ memcpy( psz_ext_text, p_peek+372, 1024 );
+ psz_ext_text[1024] = '\0';
+ msg_Dbg( p_demux, "extended event text=%s", psz_ext_text );
+ // 52 bytes reserved Bslbf
+#endif
+ p_vdr->i_service = GetWBE(&p_peek[18]);
+
+ return i_packet_size;
+ //return TS_PACKET_SIZE_188;
+ }
+ }
+
+ return DetectPacketSize( p_demux, pi_header_size, 0 );
+}
+
#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
static void vlc_dvbpsi_reset( demux_t *p_demux )
{
}
#endif
+static inline mtime_t ExtractPESTimestamp( const uint8_t *p_data )
+{
+ return ((mtime_t)(p_data[ 0]&0x0e ) << 29)|
+ (mtime_t)(p_data[1] << 22)|
+ ((mtime_t)(p_data[2]&0xfe) << 14)|
+ (mtime_t)(p_data[3] << 7)|
+ (mtime_t)(p_data[4] >> 1);
+}
+
+static void ProbePES( demux_t *p_demux, ts_pid_t *pid, const uint8_t *p_pesstart, size_t i_data, bool b_adaptfield )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ const uint8_t *p_pes = p_pesstart;
+ pid->probed.i_type = -1;
+
+ if( b_adaptfield )
+ {
+ if ( i_data < 2 )
+ return;
+
+ uint8_t len = *p_pes;
+ p_pes++; i_data--;
+
+ if(len == 0)
+ {
+ p_pes++; i_data--;/* stuffing */
+ }
+ else
+ {
+ if( i_data < len )
+ return;
+ if( len >= 7 && (p_pes[1] & 0x10) )
+ pid->probed.i_pcr_count++;
+ p_pes += len;
+ i_data -= len;
+ }
+ }
+
+ if( i_data < 9 )
+ return;
+
+ if( p_pes[0] != 0 || p_pes[1] != 0 || p_pes[2] != 1 )
+ return;
+
+ size_t i_pesextoffset = 8;
+ mtime_t i_dts = -1;
+ if( p_pes[7] & 0x80 ) // PTS
+ {
+ i_pesextoffset += 5;
+ if ( i_data < i_pesextoffset )
+ return;
+ i_dts = ExtractPESTimestamp( &p_pes[9] );
+ }
+ if( p_pes[7] & 0x40 ) // DTS
+ {
+ i_pesextoffset += 5;
+ if ( i_data < i_pesextoffset )
+ return;
+ i_dts = ExtractPESTimestamp( &p_pes[14] );
+ }
+ if( p_pes[7] & 0x20 ) // ESCR
+ i_pesextoffset += 6;
+ if( p_pes[7] & 0x10 ) // ESrate
+ i_pesextoffset += 3;
+ if( p_pes[7] & 0x08 ) // DSM
+ i_pesextoffset += 1;
+ if( p_pes[7] & 0x04 ) // CopyInfo
+ i_pesextoffset += 1;
+ if( p_pes[7] & 0x02 ) // PESCRC
+ i_pesextoffset += 2;
+
+ if ( i_data < i_pesextoffset )
+ return;
+
+ /* HeaderdataLength */
+ const size_t i_payloadoffset = 8 + 1 + p_pes[8];
+ i_pesextoffset += 1;
+
+ if ( i_data < i_pesextoffset || i_data < i_payloadoffset )
+ return;
+
+ i_data -= 8 + 1 + p_pes[8];
+
+ if( p_pes[7] & 0x01 ) // PESExt
+ {
+ size_t i_extension2_offset = 1;
+ if ( p_pes[i_pesextoffset] & 0x80 ) // private data
+ i_extension2_offset += 16;
+ if ( p_pes[i_pesextoffset] & 0x40 ) // pack
+ i_extension2_offset += 1;
+ if ( p_pes[i_pesextoffset] & 0x20 ) // seq
+ i_extension2_offset += 2;
+ if ( p_pes[i_pesextoffset] & 0x10 ) // P-STD
+ i_extension2_offset += 2;
+ if ( p_pes[i_pesextoffset] & 0x01 ) // Extension 2
+ {
+ uint8_t i_len = p_pes[i_pesextoffset + i_extension2_offset] & 0x7F;
+ i_extension2_offset += i_len;
+ }
+ if( i_data < i_extension2_offset )
+ return;
+
+ i_data -= i_extension2_offset;
+ }
+ /* (i_payloadoffset - i_pesextoffset) 0xFF stuffing */
+
+ if ( i_data < 4 )
+ return;
+
+ const uint8_t *p_data = &p_pes[i_payloadoffset];
+ /* AUDIO STREAM */
+ if(p_pes[3] >= 0xC0 && p_pes[3] <= 0xDF)
+ {
+ if( !memcmp( p_data, "\x7F\xFE\x80\x01", 4 ) )
+ {
+ pid->probed.i_type = 0x06;
+ pid->probed.i_fourcc = VLC_CODEC_DTS;
+ }
+ else if( !memcmp( p_data, "\x0B\x77", 2 ) )
+ {
+ pid->probed.i_type = 0x06;
+ pid->probed.i_fourcc = VLC_CODEC_EAC3;
+ }
+ else if( p_data[0] == 0xFF && (p_data[1] & 0xE0) == 0xE0 )
+ {
+ switch(p_data[1] & 18)
+ {
+ /* 10 - MPEG Version 2 (ISO/IEC 13818-3)
+ 11 - MPEG Version 1 (ISO/IEC 11172-3) */
+ case 0x10:
+ pid->probed.i_type = 0x04;
+ break;
+ case 0x18:
+ pid->probed.i_type = 0x03;
+ default:
+ break;
+ }
+
+ switch(p_data[1] & 6)
+ {
+ /* 01 - Layer III
+ 10 - Layer II
+ 11 - Layer I */
+ case 0x06:
+ pid->probed.i_type = 0x04;
+ pid->probed.i_fourcc = VLC_CODEC_MPGA;
+ break;
+ case 0x04:
+ pid->probed.i_type = 0x04;
+ pid->probed.i_fourcc = VLC_CODEC_MP2;
+ break;
+ case 0x02:
+ pid->probed.i_type = 0x04;
+ pid->probed.i_fourcc = VLC_CODEC_MP3;
+ default:
+ break;
+ }
+ }
+ }
+ /* VIDEO STREAM */
+ else if( p_pes[3] >= 0xE0 && p_pes[3] <= 0xEF )
+ {
+ if( !memcmp( p_data, "\x00\x00\x00", 4 ) )
+ {
+ pid->probed.i_type = 0x1b;
+ pid->probed.i_fourcc = VLC_CODEC_H264;
+ }
+ else if( !memcmp( p_data, "\x00\x00\x01", 4 ) )
+ {
+ pid->probed.i_type = 0x02;
+ pid->probed.i_fourcc = VLC_CODEC_MPGV;
+ }
+ }
+
+ /* Track timestamps and flag missing PAT */
+ if( !p_sys->patfix.i_timesourcepid && i_dts > -1 )
+ {
+ p_sys->patfix.i_first_dts = i_dts;
+ p_sys->patfix.i_timesourcepid = pid->i_pid;
+ }
+ else if( p_sys->patfix.i_timesourcepid == pid->i_pid && i_dts > -1 &&
+ !p_sys->patfix.b_pat_deadline )
+ {
+ if( i_dts - p_sys->patfix.i_first_dts > 9 * 140000 / 100 )
+ p_sys->patfix.b_pat_deadline = true;
+ }
+
+}
+
+static void BuildPATCallback( void *p_opaque, block_t *p_block )
+{
+ ts_pid_t *pat_pid = (ts_pid_t *) p_opaque;
+ dvbpsi_PushPacket( pat_pid->psi->handle, p_block->p_buffer );
+}
+
+static void BuildPMTCallback( void *p_opaque, block_t *p_block )
+{
+ ts_pid_t *program_pid = (ts_pid_t *) p_opaque;
+ while( p_block )
+ {
+ for( int i_prg = 0; i_prg < program_pid->psi->i_prg; i_prg++ )
+ {
+ dvbpsi_PushPacket( program_pid->psi->prg[i_prg]->handle,
+ p_block->p_buffer );
+ }
+ p_block = p_block->p_next;
+ }
+}
+
+static void MissingPATPMTFixup( demux_t *p_demux )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ int i_program_number = 1234;
+ int i_program_pid = 1337;
+ int i_pcr_pid = 0x1FFF;
+ int i_num_pes = 0;
+
+ if( p_sys->pid[i_program_pid].b_seen )
+ {
+ /* Find a free one */
+ for( i_program_pid = MIN_ES_PID;
+ i_program_pid <= MAX_ES_PID && p_sys->pid[i_program_pid].b_seen;
+ i_program_pid++ );
+ }
+
+ for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
+ {
+ if( !p_sys->pid[i].b_seen ||
+ p_sys->pid[i].probed.i_type == -1 )
+ continue;
+
+ if( i_pcr_pid == 0x1FFF && ( p_sys->pid[i].probed.i_type == 0x03 ||
+ p_sys->pid[i].probed.i_pcr_count ) )
+ i_pcr_pid = p_sys->pid[i].i_pid;
+
+ i_num_pes++;
+ }
+
+ if( i_num_pes == 0 )
+ return;
+
+ ts_stream_t patstream =
+ {
+ .i_pid = 0,
+ .i_continuity_counter = 0x10,
+ .b_discontinuity = false
+ };
+
+ ts_stream_t pmtprogramstream =
+ {
+ .i_pid = i_program_pid,
+ .i_continuity_counter = 0x0,
+ .b_discontinuity = false
+ };
+
+ BuildPAT( DVBPSI_HANDLE_PARAM(p_sys->pid[0].psi->handle)
+ &p_sys->pid[0], BuildPATCallback,
+ 0, 1,
+ &patstream,
+ 1, &pmtprogramstream, &i_program_number );
+
+ if( !p_sys->pid[i_program_pid].psi )
+ {
+ msg_Err( p_demux, "PAT creation failed" );
+ return;
+ }
+
+ struct esstreams_t
+ {
+ pes_stream_t pes;
+ ts_stream_t ts;
+ };
+ es_format_t esfmt = {0};
+ struct esstreams_t *esstreams = calloc( i_num_pes, sizeof(struct esstreams_t) );
+ pes_mapped_stream_t *mapped = calloc( i_num_pes, sizeof(pes_mapped_stream_t) );
+ if( esstreams && mapped )
+ {
+ int j=0;
+ for( int i = MIN_ES_PID; i <= MAX_ES_PID; i++ )
+ {
+ if( !p_sys->pid[i].b_seen ||
+ p_sys->pid[i].probed.i_type == -1 )
+ continue;
+
+ esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
+ esstreams[j].pes.i_stream_type = p_sys->pid[i].probed.i_type;
+ esstreams[j].ts.i_pid = p_sys->pid[i].i_pid;
+ mapped[j].pes = &esstreams[j].pes;
+ mapped[j].ts = &esstreams[j].ts;
+ mapped[j].fmt = &esfmt;
+ j++;
+ }
+
+ BuildPMT( DVBPSI_HANDLE_PARAM(p_sys->pid[0].psi->handle) VLC_OBJECT(p_demux),
+ &p_sys->pid[i_program_pid], BuildPMTCallback,
+ 0, 1,
+ i_pcr_pid,
+ NULL,
+ 1, &pmtprogramstream, &i_program_number,
+ i_num_pes, mapped );
+ }
+ free(esstreams);
+ free(mapped);
+}
+
/*****************************************************************************
* Open
*****************************************************************************/
int i_packet_size, i_packet_header_size = 0;
ts_pid_t *pat;
+ vdr_info_t vdr = {0};
/* Search first sync byte */
- i_packet_size = DetectPacketSize( p_demux, &i_packet_header_size );
+ i_packet_size = DetectPVRHeadersAndHeaderSize( p_demux, &i_packet_header_size, &vdr );
if( i_packet_size < 0 )
return VLC_EGENERIC;
/* Init p_sys field */
p_sys->b_dvb_meta = true;
p_sys->b_access_control = true;
- p_sys->i_current_program = 0;
- p_sys->programs_list.i_count = 0;
- p_sys->programs_list.p_values = NULL;
+ p_sys->b_end_preparse = false;
+ ARRAY_INIT( p_sys->programs );
+ p_sys->b_default_selection = false;
p_sys->i_tdt_delta = 0;
p_sys->i_dvb_start = 0;
p_sys->i_dvb_length = 0;
+ p_sys->vdr = vdr;
+
p_sys->arib.b25stream = NULL;
p_sys->stream = p_demux->s;
pid->i_pid = i;
pid->b_seen = false;
pid->b_valid = false;
+ pid->probed.i_fourcc = 0;
+ pid->probed.i_type = 0;
}
/* PID 8191 is padding */
p_sys->pid[8191].b_seen = true;
p_sys->b_split_es = var_InheritBool( p_demux, "ts-split-es" );
p_sys->b_canseek = false;
- p_sys->i_pid_ref_pcr = -1;
- p_sys->i_first_pcr = -1;
- p_sys->i_current_pcr = -1;
- p_sys->i_last_pcr = -1;
+ p_sys->b_canfastseek = false;
p_sys->b_force_seek_per_percent = var_InheritBool( p_demux, "ts-seek-percent" );
- p_sys->i_pcrs_num = 10;
- p_sys->p_pcrs = (mtime_t *)calloc( p_sys->i_pcrs_num, sizeof( mtime_t ) );
- p_sys->p_pos = (int64_t *)calloc( p_sys->i_pcrs_num, sizeof( int64_t ) );
p_sys->arib.e_mode = var_InheritInteger( p_demux, "ts-arib" );
- if( !p_sys->p_pcrs || !p_sys->p_pos )
- {
- Close( p_this );
- return VLC_ENOMEM;
- }
-
- bool b_can_fastseek = false;
stream_Control( p_sys->stream, STREAM_CAN_SEEK, &p_sys->b_canseek );
- stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &b_can_fastseek );
- if ( p_sys->b_canseek )
- {
- if( b_can_fastseek )
- {
- GetFirstPCR( p_demux );
- CheckPCR( p_demux );
- GetLastPCR( p_demux );
- }
+ stream_Control( p_sys->stream, STREAM_CAN_FASTSEEK, &p_sys->b_canfastseek );
- if( p_sys->i_first_pcr < 0 || p_sys->i_last_pcr < 0 )
- {
- msg_Dbg( p_demux, "Force Seek Per Percent: PCR's not found,");
- p_sys->b_force_seek_per_percent = true;
- }
- }
-
- while( p_sys->i_pmt_es <= 0 && vlc_object_alive( p_demux ) )
+ /* Preparse time */
+ if( p_sys->b_canseek )
{
- if( Demux( p_demux ) != 1 )
- break;
+ p_sys->es_creation = NO_ES;
+ while( !p_sys->i_pmt_es && !p_sys->b_end_preparse )
+ if( Demux( p_demux ) != VLC_DEMUXER_SUCCESS )
+ break;
}
+
+ p_sys->es_creation = ( p_sys->b_access_control ? CREATE_ES : DELAY_ES );
+
return VLC_SUCCESS;
}
TAB_CLEAN( p_sys->i_pmt, p_sys->pmt );
- free( p_sys->programs_list.p_values );
-
- free( p_sys->p_pcrs );
- free( p_sys->p_pos );
+ ARRAY_RESET( p_sys->programs );
#ifdef HAVE_ARIBB24
if ( p_sys->arib.p_instance )
demux_sys_t *p_sys = p_demux->p_sys;
bool b_wait_es = p_sys->i_pmt_es <= 0;
+ /* If we had no PAT within 140ms, create PAT/PMT from probed streams */
+ if( p_sys->i_pmt_es == 0 && !p_sys->pid[0].b_seen && p_sys->patfix.b_pat_deadline )
+ MissingPATPMTFixup( p_demux );
+
/* We read at most 100 TS packet or until a frame is completed */
for( int i_pkt = 0; i_pkt < p_sys->i_ts_read; i_pkt++ )
{
block_t *p_pkt;
if( !(p_pkt = ReadTSPacket( p_demux )) )
{
- return 0;
+ return VLC_DEMUXER_EOF;
}
if( p_sys->b_start_record )
/* Parse the TS packet */
ts_pid_t *p_pid = &p_sys->pid[PIDGet( p_pkt )];
+ /* Probe streams to build PAT/PMT after 140ms in case we don't see any PAT */
+ if( !p_sys->pid[0].b_seen &&
+ (p_pid->probed.i_type == 0 || p_pid->i_pid == p_sys->patfix.i_timesourcepid) &&
+ (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
+ (p_pkt->p_buffer[3] & 0xD0) == 0x10 ) /* Has payload but is not encrypted */
+ {
+ ProbePES( p_demux, p_pid, p_pkt->p_buffer + TS_HEADER_SIZE,
+ p_pkt->i_buffer - TS_HEADER_SIZE, p_pkt->p_buffer[3] & 0x20 /* Adaptation field */);
+ }
+
if( p_pid->b_valid )
{
if( p_pid->psi )
}
else
{
+ p_sys->b_end_preparse = true;
+ if( p_sys->es_creation == DELAY_ES ) /* No longer delay ES since that pid's program sends data */
+ {
+ AddAndCreateES( p_demux, NULL );
+ }
b_frame = GatherData( p_demux, p_pid, p_pkt );
+
+ if( p_sys->b_default_selection )
+ {
+ p_sys->b_default_selection = false;
+ assert(p_sys->programs.i_size == 1);
+ if( p_sys->programs.p_elems[0] != p_pid->i_owner_number )
+ {
+ SetPrgFilter( p_demux, p_sys->programs.p_elems[0], false );
+ SetPrgFilter( p_demux, p_pid->i_owner_number, true );
+ p_sys->programs.p_elems[0] = p_pid->i_owner_number;
+ }
+ }
}
}
else
}
demux_UpdateTitleFromStream( p_demux );
- return 1;
+ return VLC_DEMUXER_SUCCESS;
}
/*****************************************************************************
int64_t i64;
int64_t *pi64;
int i_int;
+ ts_prg_psi_t *p_prg;
+ int i_first_program = ( p_sys->programs.i_size ) ? p_sys->programs.p_elems[0] : 0;
+
+ if( PREPARSING || !i_first_program || p_sys->b_default_selection )
+ {
+ /* Set default program for preparse time (no program has been selected) */
+ for( int i=0; i<p_sys->i_pmt; i++ )
+ {
+ for( int j=0; j<p_sys->pmt[i]->psi->i_prg; j++ )
+ {
+ p_prg = p_sys->pmt[i]->psi->prg[j];
+ if( ( p_prg->pcr.i_first > -1 || p_prg->pcr.i_first_dts > VLC_TS_INVALID ) && p_prg->i_last_dts > 0 )
+ {
+ i_first_program = p_prg->i_number;
+ break;
+ }
+ }
+ }
+ }
switch( i_query )
{
case DEMUX_GET_POSITION:
pf = (double*) va_arg( args, double* );
- if( p_sys->b_force_seek_per_percent ||
- (p_sys->b_dvb_meta && p_sys->b_access_control) ||
- p_sys->i_current_pcr - p_sys->i_first_pcr < 0 ||
- p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
+ /* Access control test is because EPG for recordings is not relevant */
+ if( p_sys->b_dvb_meta && p_sys->b_access_control )
{
int64_t i_time, i_length;
if( !DVBEventInformation( p_demux, &i_time, &i_length ) && i_length > 0 )
- *pf = (double)i_time/(double)i_length;
- else if( (i64 = stream_Size( p_sys->stream) ) > 0 )
{
- int64_t offset = stream_Tell( p_sys->stream );
-
- *pf = (double)offset / (double)i64;
+ *pf = (double)i_time/(double)i_length;
+ return VLC_SUCCESS;
}
- else
- *pf = 0.0;
}
- else
+
+ if( (p_prg = GetProgramByID( p_sys, i_first_program )) &&
+ p_prg->pcr.i_first > -1 && p_prg->i_last_dts > VLC_TS_INVALID &&
+ p_prg->pcr.i_current > -1 )
{
- *pf = (double)(p_sys->i_current_pcr - p_sys->i_first_pcr) / (double)(p_sys->i_last_pcr - p_sys->i_first_pcr);
+ double i_length = TimeStampWrapAround( p_prg,
+ p_prg->i_last_dts ) - p_prg->pcr.i_first;
+ i_length += p_prg->pcr.i_pcroffset;
+ double i_pos = TimeStampWrapAround( p_prg,
+ p_prg->pcr.i_current ) - p_prg->pcr.i_first;
+ *pf = i_pos / i_length;
+ return VLC_SUCCESS;
}
- return VLC_SUCCESS;
+
+ if( (i64 = stream_Size( p_sys->stream) ) > 0 )
+ {
+ int64_t offset = stream_Tell( p_sys->stream );
+ *pf = (double)offset / (double)i64;
+ return VLC_SUCCESS;
+ }
+ break;
case DEMUX_SET_POSITION:
f = (double) va_arg( args, double );
if(!p_sys->b_canseek)
- return VLC_EGENERIC;
+ break;
- if( p_sys->b_force_seek_per_percent ||
- (p_sys->b_dvb_meta && p_sys->b_access_control) ||
- p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
+ if( p_sys->b_dvb_meta && p_sys->b_access_control &&
+ !p_sys->b_force_seek_per_percent &&
+ (p_prg = GetProgramByID( p_sys, i_first_program )) )
{
- i64 = stream_Size( p_sys->stream );
- if( stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) )
- return VLC_EGENERIC;
+ int64_t i_time, i_length;
+ if( !DVBEventInformation( p_demux, &i_time, &i_length ) &&
+ i_length > 0 && !SeekToTime( p_demux, p_prg, TO_SCALE(i_length) * f ) )
+ {
+ ReadyQueuesPostSeek( p_sys );
+ es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
+ TO_SCALE(i_length) * f );
+ return VLC_SUCCESS;
+ }
}
- else
+
+ if( !p_sys->b_force_seek_per_percent &&
+ (p_prg = GetProgramByID( p_sys, i_first_program )) &&
+ p_prg->pcr.i_first > -1 && p_prg->i_last_dts > VLC_TS_INVALID &&
+ p_prg->pcr.i_current > -1 )
{
- if( Seek( p_demux, f ) )
+ double i_length = TimeStampWrapAround( p_prg,
+ p_prg->i_last_dts ) - p_prg->pcr.i_first;
+ if( !SeekToTime( p_demux, p_prg, p_prg->pcr.i_first + i_length * f ) )
{
- p_sys->b_force_seek_per_percent = true;
- return VLC_EGENERIC;
+ ReadyQueuesPostSeek( p_sys );
+ es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
+ FROM_SCALE(p_prg->pcr.i_first + i_length * f) );
+ return VLC_SUCCESS;
}
}
- return VLC_SUCCESS;
+
+ i64 = stream_Size( p_sys->stream );
+ if( i64 > 0 &&
+ stream_Seek( p_sys->stream, (int64_t)(i64 * f) ) == VLC_SUCCESS )
+ {
+ ReadyQueuesPostSeek( p_sys );
+ return VLC_SUCCESS;
+ }
+ break;
+
+ case DEMUX_SET_TIME:
+ i64 = (int64_t)va_arg( args, int64_t );
+
+ if( p_sys->b_canseek &&
+ (p_prg = GetProgramByID( p_sys, i_first_program )) &&
+ p_prg->pcr.i_first > -1 &&
+ !SeekToTime( p_demux, p_prg, p_prg->pcr.i_first + TO_SCALE(i64) ) )
+ {
+ ReadyQueuesPostSeek( p_sys );
+ es_out_Control( p_demux->out, ES_OUT_SET_NEXT_DISPLAY_TIME,
+ FROM_SCALE(p_prg->pcr.i_first) + i64 - VLC_TS_0 );
+ return VLC_SUCCESS;
+ }
+ break;
case DEMUX_GET_TIME:
pi64 = (int64_t*)va_arg( args, int64_t * );
- if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
- p_sys->b_force_seek_per_percent ||
- p_sys->i_current_pcr - p_sys->i_first_pcr < 0 )
+
+ if( p_sys->b_dvb_meta && p_sys->b_access_control )
{
- if( DVBEventInformation( p_demux, pi64, NULL ) )
- {
- *pi64 = 0;
- }
+ if( !DVBEventInformation( p_demux, pi64, NULL ) )
+ return VLC_SUCCESS;
}
- else
+
+ if( (p_prg = GetProgramByID( p_sys, i_first_program )) &&
+ p_prg->pcr.i_current > -1 && p_prg->pcr.i_first > -1 )
{
- *pi64 = (p_sys->i_current_pcr - p_sys->i_first_pcr) * 100 / 9;
+ int64_t i_pcr = TimeStampWrapAround( p_prg, p_prg->pcr.i_current );
+ *pi64 = FROM_SCALE(i_pcr - p_prg->pcr.i_first);
+ return VLC_SUCCESS;
}
- return VLC_SUCCESS;
+ break;
case DEMUX_GET_LENGTH:
pi64 = (int64_t*)va_arg( args, int64_t * );
- if( (p_sys->b_dvb_meta && p_sys->b_access_control) ||
- p_sys->b_force_seek_per_percent ||
- p_sys->i_last_pcr - p_sys->i_first_pcr <= 0 )
+
+ if( p_sys->b_dvb_meta && p_sys->b_access_control )
{
- if( DVBEventInformation( p_demux, NULL, pi64 ) )
- {
- *pi64 = 0;
- }
+ if( !DVBEventInformation( p_demux, NULL, pi64 ) )
+ return VLC_SUCCESS;
}
- else
+
+ if( (p_prg = GetProgramByID( p_sys, i_first_program )) &&
+ ( p_prg->pcr.i_first > -1 || p_prg->pcr.i_first_dts > VLC_TS_INVALID ) &&
+ p_prg->i_last_dts > 0 )
{
- *pi64 = (p_sys->i_last_pcr - p_sys->i_first_pcr) * 100 / 9;
+ int64_t i_start = (p_prg->pcr.i_first > -1) ? p_prg->pcr.i_first :
+ TO_SCALE(p_prg->pcr.i_first_dts);
+ int64_t i_last = TimeStampWrapAround( p_prg, p_prg->i_last_dts );
+ i_last += p_prg->pcr.i_pcroffset;
+ *pi64 = FROM_SCALE(i_last - i_start);
+ return VLC_SUCCESS;
}
- return VLC_SUCCESS;
+ break;
case DEMUX_SET_GROUP:
{
p_list = (vlc_list_t *)va_arg( args, vlc_list_t * );
msg_Dbg( p_demux, "DEMUX_SET_GROUP %d %p", i_int, p_list );
- if( i_int == 0 && p_sys->i_current_program > 0 )
- i_int = p_sys->i_current_program;
-
- if( p_sys->i_current_program > 0 )
- {
- if( p_sys->i_current_program != i_int )
- SetPrgFilter( p_demux, p_sys->i_current_program, false );
- }
- else if( p_sys->i_current_program < 0 )
+ if( i_int != 0 ) /* If not default program */
{
- for( int i = 0; i < p_sys->programs_list.i_count; i++ )
- SetPrgFilter( p_demux, p_sys->programs_list.p_values[i].i_int, false );
- }
+ /* Deselect/filter current ones */
+ for( int i=0; i<p_sys->programs.i_size; i++ )
+ SetPrgFilter( p_demux, p_sys->programs.p_elems[i], false );
+ ARRAY_RESET( p_sys->programs );
- if( i_int > 0 )
- {
- p_sys->i_current_program = i_int;
- SetPrgFilter( p_demux, p_sys->i_current_program, true );
- }
- else if( i_int < 0 )
- {
- p_sys->i_current_program = -1;
- p_sys->programs_list.i_count = 0;
- if( p_list )
+ if( i_int != -1 )
+ {
+ ARRAY_APPEND( p_sys->programs, i_int );
+ }
+ else if( likely( p_list != NULL ) )
{
- vlc_list_t *p_dst = &p_sys->programs_list;
- free( p_dst->p_values );
+ for( int i = 0; i < p_list->i_count; i++ )
+ ARRAY_APPEND( p_sys->programs, p_list->p_values[i].i_int );
+ }
- p_dst->p_values = calloc( p_list->i_count,
- sizeof(*p_dst->p_values) );
- if( p_dst->p_values )
- {
- p_dst->i_count = p_list->i_count;
- for( int i = 0; i < p_list->i_count; i++ )
- {
- p_dst->p_values[i] = p_list->p_values[i];
- SetPrgFilter( p_demux, p_dst->p_values[i].i_int, true );
- }
- }
+ /* Select/filter current ones */
+ for( int i=0; i<p_sys->programs.i_size; i++ )
+ {
+ msg_Dbg( p_demux, "Program %d in new selection", p_sys->programs.p_elems[i] );
+ SetPrgFilter( p_demux, p_sys->programs.p_elems[i], true );
}
+
+ p_sys->b_default_selection = false;
}
+
return VLC_SUCCESS;
}
return stream_vaControl( p_sys->stream, STREAM_GET_SIGNAL, args );
default:
- return VLC_EGENERIC;
+ break;
}
+
+ return VLC_EGENERIC;
}
/*****************************************************************************
return;
assert( p_prg );
+ p_prg->pcr.b_disable = !p_sys->b_trust_pcr;
+
SetPIDFilter( p_demux, i_pmt_pid, b_selected );
if( p_prg->i_pid_pcr > 0 )
SetPIDFilter( p_demux, p_prg->i_pid_pcr, b_selected );
prg->i_number = -1;
prg->i_pid_pcr = -1;
prg->i_pid_pmt = -1;
- prg->i_pcr_value= -1;
prg->iod = NULL;
prg->handle = NULL;
+ prg->i_last_dts = -1;
+
+ prg->pcr.i_current = -1;
+ prg->pcr.i_first = -1;
+ prg->pcr.b_disable = false;
+ prg->pcr.i_first_dts = VLC_TS_INVALID;
+ prg->pcr.i_pcroffset = -1;
TAB_APPEND( pid->psi->i_prg, pid->psi->prg, prg );
}
}
if( pid->es->p_data )
block_ChainRelease( pid->es->p_data );
+ if( pid->es->p_prepcr_outqueue )
+ block_ChainRelease( pid->es->p_prepcr_outqueue );
+
es_format_Clean( &pid->es->fmt );
free( pid->es );
/****************************************************************************
* gathering stuff
****************************************************************************/
-static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
+static int ParsePESHeader( demux_t *p_demux, const uint8_t *p_header, size_t i_header,
+ unsigned *pi_skip, mtime_t *pi_dts, mtime_t *pi_pts )
{
- demux_sys_t *p_sys = p_demux->p_sys;
- uint8_t header[34];
- unsigned i_pes_size = 0;
- unsigned i_skip = 0;
- mtime_t i_dts = -1;
- mtime_t i_pts = -1;
- mtime_t i_length = 0;
+ unsigned i_skip;
- /* FIXME find real max size */
- /* const int i_max = */ block_ChainExtract( p_pes, header, 34 );
-
- if( pid->b_scrambled || header[0] != 0 || header[1] != 0 || header[2] != 1 )
- {
- if ( !pid->b_scrambled )
- msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
- header[0], header[1],header[2],header[3], pid->i_pid );
- block_ChainRelease( p_pes );
- return;
- }
+ if ( i_header < 9 )
+ return VLC_EGENERIC;
- /* TODO check size */
- switch( header[3] )
+ switch( p_header[3] )
{
case 0xBC: /* Program stream map */
case 0xBE: /* Padding */
i_skip = 6;
break;
default:
- if( ( header[6]&0xC0 ) == 0x80 )
+ if( ( p_header[6]&0xC0 ) == 0x80 )
{
/* mpeg2 PES */
- i_skip = header[8] + 9;
+ i_skip = p_header[8] + 9;
+ if( i_header < i_skip )
+ return VLC_EGENERIC;
- if( header[7]&0x80 ) /* has pts */
+ if( p_header[7]&0x80 ) /* has pts */
{
- i_pts = ((mtime_t)(header[ 9]&0x0e ) << 29)|
- (mtime_t)(header[10] << 22)|
- ((mtime_t)(header[11]&0xfe) << 14)|
- (mtime_t)(header[12] << 7)|
- (mtime_t)(header[13] >> 1);
+ if( i_header < 9 + 5 )
+ return VLC_EGENERIC;
+ *pi_pts = ExtractPESTimestamp( &p_header[9] );
- if( header[7]&0x40 ) /* has dts */
+ if( p_header[7]&0x40 ) /* has dts */
{
- i_dts = ((mtime_t)(header[14]&0x0e ) << 29)|
- (mtime_t)(header[15] << 22)|
- ((mtime_t)(header[16]&0xfe) << 14)|
- (mtime_t)(header[17] << 7)|
- (mtime_t)(header[18] >> 1);
+ if( i_header < 14 + 5 )
+ return VLC_EGENERIC;
+ *pi_dts = ExtractPESTimestamp( &p_header[14] );
}
}
}
else
{
i_skip = 6;
- while( i_skip < 23 && header[i_skip] == 0xff )
+ if( i_header < i_skip + 1 )
+ return VLC_EGENERIC;
+ while( i_skip < 23 && p_header[i_skip] == 0xff )
{
i_skip++;
+ if( i_header < i_skip + 1 )
+ return VLC_EGENERIC;
}
if( i_skip == 23 )
{
msg_Err( p_demux, "too much MPEG-1 stuffing" );
- block_ChainRelease( p_pes );
- return;
+ return VLC_EGENERIC;
}
- if( ( header[i_skip] & 0xC0 ) == 0x40 )
+ if( ( p_header[i_skip] & 0xC0 ) == 0x40 )
{
i_skip += 2;
}
- if( header[i_skip]&0x20 )
+ if( i_header < i_skip + 1 )
+ return VLC_EGENERIC;
+
+ if( p_header[i_skip]&0x20 )
{
- i_pts = ((mtime_t)(header[i_skip]&0x0e ) << 29)|
- (mtime_t)(header[i_skip+1] << 22)|
- ((mtime_t)(header[i_skip+2]&0xfe) << 14)|
- (mtime_t)(header[i_skip+3] << 7)|
- (mtime_t)(header[i_skip+4] >> 1);
+ if( i_header < i_skip + 5 )
+ return VLC_EGENERIC;
+ *pi_pts = ExtractPESTimestamp( &p_header[i_skip] );
- if( header[i_skip]&0x10 ) /* has dts */
+ if( p_header[i_skip]&0x10 ) /* has dts */
{
- i_dts = ((mtime_t)(header[i_skip+5]&0x0e ) << 29)|
- (mtime_t)(header[i_skip+6] << 22)|
- ((mtime_t)(header[i_skip+7]&0xfe) << 14)|
- (mtime_t)(header[i_skip+8] << 7)|
- (mtime_t)(header[i_skip+9] >> 1);
- i_skip += 10;
+ if( i_header < i_skip + 10 )
+ return VLC_EGENERIC;
+ *pi_dts = ExtractPESTimestamp( &p_header[i_skip+5] );
+ i_skip += 10;
}
else
{
break;
}
+ if( i_header < i_skip )
+ return VLC_EGENERIC;
+
+ *pi_skip = i_skip;
+ return VLC_SUCCESS;
+}
+
+static void ParsePES( demux_t *p_demux, ts_pid_t *pid, block_t *p_pes )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ uint8_t header[34];
+ unsigned i_pes_size = 0;
+ unsigned i_skip = 0;
+ mtime_t i_dts = -1;
+ mtime_t i_pts = -1;
+ mtime_t i_length = 0;
+
+ const int i_max = block_ChainExtract( p_pes, header, 34 );
+ if ( i_max < 4 )
+ {
+ block_ChainRelease( p_pes );
+ return;
+ }
+
+ if( pid->b_scrambled || header[0] != 0 || header[1] != 0 || header[2] != 1 )
+ {
+ if ( !pid->b_scrambled )
+ msg_Warn( p_demux, "invalid header [0x%02x:%02x:%02x:%02x] (pid: %d)",
+ header[0], header[1],header[2],header[3], pid->i_pid );
+ block_ChainRelease( p_pes );
+ return;
+ }
+
+ if( ParsePESHeader( p_demux, (uint8_t*)&header, i_max, &i_skip, &i_dts, &i_pts ) == VLC_EGENERIC )
+ {
+ block_ChainRelease( p_pes );
+ return;
+ }
+ else
+ {
+ if( i_pts != -1 )
+ i_pts = TimeStampWrapAround( GetProgramByID(p_sys, pid->i_owner_number), i_pts );
+ if( i_dts != -1 )
+ i_dts = TimeStampWrapAround( GetProgramByID(p_sys, pid->i_owner_number), i_dts );
+ }
+
if( pid->es->fmt.i_codec == VLC_FOURCC( 'a', '5', '2', 'b' ) ||
pid->es->fmt.i_codec == VLC_FOURCC( 'd', 't', 's', 'b' ) )
{
{
if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
{
- mtime_t i_pcr = pid->p_owner->prg[i]->i_pcr_value;
+ mtime_t i_pcr = pid->p_owner->prg[i]->pcr.i_current;
if( i_pcr > VLC_TS_INVALID )
p_block->i_pts = VLC_TS_0 + i_pcr * 100 / 9 + 40000;
break;
p_block = Opus_Parse(p_demux, p_block);
}
+ ts_prg_psi_t *p_program = GetProgramByID( p_sys, pid->i_owner_number );
+
while (p_block) {
block_t *p_next = p_block->p_next;
p_block->p_next = NULL;
- for( int i = 0; i < pid->i_extra_es; i++ )
+
+ if( p_program->pcr.i_first == -1 ) /* Not seen yet */
+ PCRFixHandle( p_demux, p_program, p_block );
+
+ if( p_program->pcr.i_current > -1 || p_program->pcr.b_disable )
{
- es_out_Send( p_demux->out, pid->extra_es[i]->id,
- block_Duplicate( p_block ) );
- }
+ if( pid->es->p_prepcr_outqueue )
+ {
+ block_ChainAppend( &pid->es->p_prepcr_outqueue, p_block );
+ p_block = pid->es->p_prepcr_outqueue;
+ pid->es->p_prepcr_outqueue = NULL;
+ }
+
+ if ( p_program->pcr.b_disable && p_block->i_dts > VLC_TS_INVALID &&
+ ( p_program->i_pid_pcr == pid->i_pid || p_program->i_pid_pcr == 0x1FFF ) )
+ {
+ ProgramSetPCR( p_demux, p_program, (p_block->i_dts - VLC_TS_0) * 9 / 100 - 120000 );
+ }
+
+ /* Compute PCR/DTS offset if any */
+ if( p_program->pcr.i_pcroffset == -1 && p_block->i_dts > VLC_TS_INVALID &&
+ p_program->pcr.i_current > VLC_TS_INVALID )
+ {
+ int64_t i_dts27 = (p_block->i_dts - VLC_TS_0) * 9 / 100;
+ int64_t i_pcr = TimeStampWrapAround( p_program, p_program->pcr.i_current );
+ if( i_dts27 < i_pcr )
+ {
+ p_program->pcr.i_pcroffset = i_pcr - i_dts27 + 80000;
+ msg_Warn( p_demux, "Broken stream: pid %d sends packets with dts %"PRId64
+ "us later than pcr, applying delay",
+ pid->i_pid, p_program->pcr.i_pcroffset * 100 / 9 );
+ }
+ else p_program->pcr.i_pcroffset = 0;
+ }
+
+ if( p_program->pcr.i_pcroffset != -1 )
+ {
+ if( p_block->i_dts > VLC_TS_INVALID )
+ p_block->i_dts += (p_program->pcr.i_pcroffset * 100 / 9);
+ if( p_block->i_pts > VLC_TS_INVALID )
+ p_block->i_pts += (p_program->pcr.i_pcroffset * 100 / 9);
+ }
- if (!p_sys->b_trust_pcr)
- es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
- pid->i_owner_number, p_block->i_pts);
+ for( int i = 0; i < pid->i_extra_es; i++ )
+ {
+ es_out_Send( p_demux->out, pid->extra_es[i]->id,
+ block_Duplicate( p_block ) );
+ }
- es_out_Send( p_demux->out, pid->es->id, p_block );
+ es_out_Send( p_demux->out, pid->es->id, p_block );
+ }
+ else
+ {
+ if( p_program->pcr.i_first == -1 ) /* Not seen yet */
+ PCRFixHandle( p_demux, p_program, p_block );
+
+ block_ChainAppend( &pid->es->p_prepcr_outqueue, p_block );
+ }
p_block = p_next;
}
static void ParseTableSection( demux_t *p_demux, ts_pid_t *pid, block_t *p_data )
{
block_t *p_content = block_ChainGather( p_data );
- mtime_t i_date = -1;
- for( int i = 0; pid->p_owner && i < pid->p_owner->i_prg; i++ )
- {
- if( pid->i_owner_number == pid->p_owner->prg[i]->i_number )
- {
- i_date = pid->p_owner->prg[i]->i_pcr_value;
- if( i_date >= 0 )
- break;
- }
- }
- if( i_date >= 0 )
+ ts_prg_psi_t *p_program = NULL;
+
+ if ( pid->p_owner &&
+ (p_program = GetProgramByID( p_demux->p_sys, pid->i_owner_number )) )
{
+ mtime_t i_date = p_program->pcr.i_current;
+
if( pid->es->fmt.i_codec == VLC_CODEC_SCTE_27 )
{
/* We need to extract the truncated pts stored inside the payload */
if( p_content->p_buffer[3] & 0x40 )
{
i_index = ((p_content->p_buffer[7] & 0x0f) << 8) |
- p_content->p_buffer[8];
+ p_content->p_buffer[8];
i_offset = 9;
}
if( i_index == 0 && p_content->i_buffer > i_offset + 8 )
}
}
}
- p_content->i_dts =
- p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
+
+ p_content->i_dts = p_content->i_pts = VLC_TS_0 + i_date * 100 / 9;
+ PCRFixHandle( p_demux, p_program, p_content );
}
+
es_out_Send( p_demux->out, pid->es->id, p_content );
}
+
static void ParseData( demux_t *p_demux, ts_pid_t *pid )
{
block_t *p_data = pid->es->p_data;
/* Get a new TS packet */
if( !( p_pkt = stream_Block( p_sys->stream, p_sys->i_packet_size ) ) )
{
- msg_Dbg( p_demux, "eof ?" );
+ if( stream_Tell( p_sys->stream ) == stream_Size( p_sys->stream ) )
+ msg_Dbg( p_demux, "EOF at %"PRId64, stream_Tell( p_sys->stream ) );
+ else
+ msg_Dbg( p_demux, "Can't read TS packet at %"PRId64, stream_Tell(p_sys->stream) );
+ return NULL;
+ }
+
+ if( p_pkt->i_buffer < TS_HEADER_SIZE + p_sys->i_packet_header_size )
+ {
+ block_Release( p_pkt );
return NULL;
}
{
msg_Warn( p_demux, "lost synchro" );
block_Release( p_pkt );
- while( vlc_object_alive (p_demux) )
+ for( ;; )
{
const uint8_t *p_peek;
int i_peek, i_skip = 0;
return p_pkt;
}
-static mtime_t AdjustPCRWrapAround( demux_t *p_demux, mtime_t i_pcr )
+static int64_t TimeStampWrapAround( ts_prg_psi_t *p_prg, int64_t i_time )
{
- demux_sys_t *p_sys = p_demux->p_sys;
- /*
- * PCR is 33bit. If PCR reaches to 0x1FFFFFFFF (26:30:43.717), ressets from 0.
- * So, need to add 0x1FFFFFFFF, for calculating duration or current position.
- */
- mtime_t i_adjust = 0;
- int64_t i_pos = stream_Tell( p_sys->stream );
- int i;
- for( i = 1; i < p_sys->i_pcrs_num && p_sys->p_pos[i] <= i_pos; ++i )
- {
- if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
- i_adjust += 0x1FFFFFFFF;
- }
- if( p_sys->p_pcrs[i-1] > i_pcr )
- i_adjust += 0x1FFFFFFFF;
+ int64_t i_adjust = 0;
+ if( p_prg && p_prg->pcr.i_first > 0x0FFFFFFFF && i_time < 0x0FFFFFFFF )
+ i_adjust = 0x1FFFFFFFF;
- return i_pcr + i_adjust;
+ return i_time + i_adjust;
}
static mtime_t GetPCR( block_t *p_pkt )
return i_pcr;
}
-static int SeekToPCR( demux_t *p_demux, int64_t i_pos )
+static inline void FlushESBuffer( ts_es_t *p_es )
+{
+ if( p_es->p_data )
+ {
+ p_es->i_data_gathered = p_es->i_data_size = 0;
+ block_ChainRelease( p_es->p_data );
+ p_es->p_data = NULL;
+ p_es->pp_last = &p_es->p_data;
+ }
+}
+
+static void ReadyQueuesPostSeek( demux_sys_t *p_sys )
+{
+ for( int i=MIN_ES_PID; i<=MAX_ES_PID; i++ )
+ {
+ ts_pid_t *pid = &p_sys->pid[i];
+
+ if( !pid->es )
+ continue;
+
+ pid->i_cc = 0xff;
+
+ if( pid->es->p_prepcr_outqueue )
+ {
+ block_ChainRelease( pid->es->p_prepcr_outqueue );
+ pid->es->p_prepcr_outqueue = NULL;
+ }
+
+ FlushESBuffer( pid->es );
+
+ for( int j = 0; j < pid->i_extra_es; j++ )
+ FlushESBuffer( pid->extra_es[j] );
+ }
+
+ for( int i=0; i<p_sys->i_pmt; i++ )
+ for( int j=0; j<p_sys->pmt[i]->psi->i_prg; j++ )
+ p_sys->pmt[i]->psi->prg[j]->pcr.i_current = -1;
+}
+
+static int SeekToTime( demux_t *p_demux, ts_prg_psi_t *p_prg, int64_t i_scaledtime )
{
demux_sys_t *p_sys = p_demux->p_sys;
- mtime_t i_pcr = -1;
- const int64_t i_initial_pos = stream_Tell( p_sys->stream );
+ /* Deal with common but worst binary search case */
+ if( p_prg->pcr.i_first == i_scaledtime && p_sys->b_canseek )
+ return stream_Seek( p_sys->stream, 0 );
- if( i_pos < 0 )
+ if( !p_sys->b_canfastseek )
return VLC_EGENERIC;
- int64_t i_last_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
- if( i_pos > i_last_pos )
- i_pos = i_last_pos;
+ int64_t i_initial_pos = stream_Tell( p_sys->stream );
- if( stream_Seek( p_sys->stream, i_pos ) )
+ /* Find the time position by using binary search algorithm. */
+ int64_t i_head_pos = 0;
+ int64_t i_tail_pos = stream_Size( p_sys->stream ) - p_sys->i_packet_size;
+ if( i_head_pos >= i_tail_pos )
return VLC_EGENERIC;
- while( vlc_object_alive( p_demux ) )
+ bool b_found = false;
+ while( (i_head_pos + p_sys->i_packet_size) <= i_tail_pos && !b_found )
{
- block_t *p_pkt;
+ /* Round i_pos to a multiple of p_sys->i_packet_size */
+ int64_t i_splitpos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
+ int64_t i_div = i_splitpos % p_sys->i_packet_size;
+ i_splitpos -= i_div;
- if( !( p_pkt = ReadTSPacket( p_demux ) ) )
- {
+ if ( stream_Seek( p_sys->stream, i_splitpos ) != VLC_SUCCESS )
break;
- }
- if( PIDGet( p_pkt ) == p_sys->i_pid_ref_pcr )
+
+ int64_t i_pos = i_splitpos;
+ while( i_pos > -1 && i_pos < i_tail_pos )
{
- i_pcr = GetPCR( p_pkt );
+ int64_t i_pcr = -1;
+ block_t *p_pkt = ReadTSPacket( p_demux );
+ if( !p_pkt )
+ {
+ i_head_pos = i_tail_pos;
+ break;
+ }
+ else
+ i_pos = stream_Tell( p_sys->stream );
+
+ int i_pid = PIDGet( p_pkt );
+ if( i_pid != 0x1FFF && p_sys->pid[i_pid].b_valid &&
+ p_sys->pid[i_pid].i_owner_number == p_prg->i_number &&
+ (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
+ (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
+ )
+ {
+ unsigned i_skip = 4;
+ if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
+ {
+ if( p_pkt->i_buffer >= 4 + 2 + 5 )
+ {
+ i_pcr = GetPCR( p_pkt );
+ i_skip += 1 + p_pkt->p_buffer[4];
+ }
+ }
+ else
+ {
+ mtime_t i_dts = -1;
+ mtime_t i_pts = -1;
+ if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
+ p_pkt->i_buffer - i_skip, &i_skip, &i_dts, &i_pts ) )
+ {
+ if( i_dts > -1 )
+ i_pcr = i_dts;
+ }
+ }
+ }
+ block_Release( p_pkt );
+
+ if( i_pcr != -1 )
+ {
+ int64_t i_diff = i_scaledtime - TimeStampWrapAround( p_prg, i_pcr );
+ if ( i_diff < 0 )
+ i_tail_pos = i_splitpos - p_sys->i_packet_size;
+ else if( i_diff < TO_SCALE(VLC_TS_0 + CLOCK_FREQ / 2) ) // 500ms
+ b_found = true;
+ else
+ i_head_pos = i_pos;
+ break;
+ }
}
- block_Release( p_pkt );
- if( i_pcr >= 0 )
- break;
- if( stream_Tell( p_sys->stream ) >= i_last_pos )
- break;
+
}
- if( i_pcr < 0 )
+
+ if( !b_found )
{
+ msg_Dbg( p_demux, "Seek():cannot find a time position." );
stream_Seek( p_sys->stream, i_initial_pos );
- assert( i_initial_pos == stream_Tell( p_sys->stream ) );
return VLC_EGENERIC;
}
-
- p_sys->i_current_pcr = i_pcr;
return VLC_SUCCESS;
}
-static int Seek( demux_t *p_demux, double f_percent )
+static ts_prg_psi_t * GetProgramByID( demux_sys_t *p_sys, int i_program )
{
- demux_sys_t *p_sys = p_demux->p_sys;
-
- int64_t i_initial_pos = stream_Tell( p_sys->stream );
- mtime_t i_initial_pcr = p_sys->i_current_pcr;
-
- /*
- * Find the time position by using binary search algorithm.
- */
- mtime_t i_target_pcr = (p_sys->i_last_pcr - p_sys->i_first_pcr) * f_percent + p_sys->i_first_pcr;
-
- int64_t i_head_pos = 0;
- int64_t i_tail_pos;
+ for( int i = 0; i < p_sys->i_pmt; i++ )
{
- mtime_t i_adjust = 0;
- int i;
- for( i = 1; i < p_sys->i_pcrs_num; ++i )
+ for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
{
- if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
- i_adjust += 0x1FFFFFFFF;
- if( p_sys->p_pcrs[i] + i_adjust > i_target_pcr )
- break;
+ ts_prg_psi_t *p_prg = p_sys->pmt[i]->psi->prg[i_prg];
+ if( p_prg->i_number == i_program )
+ return p_prg;
}
- i_head_pos = p_sys->p_pos[i-1];
- i_tail_pos = ( i < p_sys->i_pcrs_num ) ? p_sys->p_pos[i] : stream_Size( p_sys->stream );
}
- msg_Dbg( p_demux, "Seek():i_head_pos:%"PRId64", i_tail_pos:%"PRId64, i_head_pos, i_tail_pos);
+ return NULL;
+}
- bool b_found = false;
- int i_cnt = 0;
- while( i_head_pos <= i_tail_pos )
+#define PROBE_CHUNK_COUNT 250
+
+static int ProbeChunk( demux_t *p_demux, int i_program, bool b_end, int64_t *pi_pcr, bool *pb_found )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ int i_count = 0;
+ block_t *p_pkt = NULL;
+ *pi_pcr = -1;
+
+ for( ;; )
{
- /* Round i_pos to a multiple of p_sys->i_packet_size */
- int64_t i_pos = i_head_pos + (i_tail_pos - i_head_pos) / 2;
- int64_t i_div = i_pos % p_sys->i_packet_size;
- i_pos -= i_div;
- if( SeekToPCR( p_demux, i_pos ) )
- break;
- p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
- int64_t i_diff_msec = (p_sys->i_current_pcr - i_target_pcr) * 100 / 9 / 1000;
- if( i_diff_msec > 500 )
- {
- i_tail_pos = i_pos - p_sys->i_packet_size;
- }
- else if( i_diff_msec < -500 )
+ if( i_count++ > PROBE_CHUNK_COUNT || !( p_pkt = ReadTSPacket( p_demux ) ) )
{
- i_head_pos = i_pos + p_sys->i_packet_size;
+ break;
}
- else
+
+ int i_pid = PIDGet( p_pkt );
+ p_sys->pid[i_pid].b_seen = true;
+
+ if( i_pid != 0x1FFF && p_sys->pid[i_pid].b_valid && p_sys->pid[i_pid].p_owner &&
+ (p_pkt->p_buffer[1] & 0xC0) == 0x40 && /* Payload start but not corrupt */
+ (p_pkt->p_buffer[3] & 0xD0) == 0x10 /* Has payload but is not encrypted */
+ )
{
- // diff time <= 500msec
- b_found = true;
- break;
+ bool b_pcrresult = true;
+
+ if( p_pkt->i_buffer >= 4 + 2 + 5 )
+ *pi_pcr = GetPCR( p_pkt );
+
+ if( *pi_pcr == -1 )
+ {
+ b_pcrresult = false;
+ mtime_t i_dts = -1;
+ mtime_t i_pts = -1;
+ unsigned i_skip = 4;
+ if ( p_pkt->p_buffer[3] & 0x20 ) // adaptation field
+ i_skip += 1 + p_pkt->p_buffer[4];
+
+ if ( VLC_SUCCESS == ParsePESHeader( p_demux, &p_pkt->p_buffer[i_skip],
+ p_pkt->i_buffer - i_skip,
+ &i_skip, &i_dts, &i_pts ) )
+ {
+ if( i_dts != -1 )
+ *pi_pcr = i_dts;
+ else if( i_pts != -1 )
+ *pi_pcr = i_pts;
+ }
+ }
+
+ if( *pi_pcr != -1 )
+ {
+ ts_psi_t *p_prg = p_sys->pid[i_pid].p_owner;
+ for(int i=0; i<p_prg->i_prg; i++)
+ {
+ if( i_program == 0 || i_program == p_prg->prg[i]->i_number )
+ *pb_found = true;
+
+ if( b_end )
+ {
+ p_prg->prg[i]->i_last_dts = *pi_pcr;
+ }
+ /* Start, only keep first */
+ else if( b_pcrresult && p_prg->prg[i]->pcr.i_first == -1 )
+ {
+ p_prg->prg[i]->pcr.i_first = *pi_pcr;
+ }
+ else if( p_prg->prg[i]->pcr.i_first_dts < VLC_TS_0 )
+ {
+ p_prg->prg[i]->pcr.i_first_dts = VLC_TS_0 + *pi_pcr * 100 / 9;
+ }
+ }
+ }
}
- ++i_cnt;
- }
- if( !b_found )
- {
- msg_Dbg( p_demux, "Seek():cannot find a time position. i_cnt:%d", i_cnt );
- stream_Seek( p_sys->stream, i_initial_pos );
- p_sys->i_current_pcr = i_initial_pcr;
- return VLC_EGENERIC;
- }
- else
- {
- msg_Dbg( p_demux, "Seek():can find a time position. i_cnt:%d", i_cnt );
- return VLC_SUCCESS;
+
+ block_Release( p_pkt );
}
+
+ return i_count;
}
-static void GetFirstPCR( demux_t *p_demux )
+static int ProbeStart( demux_t *p_demux, int i_program )
{
demux_sys_t *p_sys = p_demux->p_sys;
+ const int64_t i_initial_pos = stream_Tell( p_sys->stream );
+ int64_t i_stream_size = stream_Size( p_sys->stream );
- int64_t i_initial_pos = stream_Tell( p_sys->stream );
-
- if( stream_Seek( p_sys->stream, 0 ) )
- return;
+ int i_probe_count = 0;
+ int64_t i_pos;
+ mtime_t i_pcr = -1;
+ bool b_found = false;
- while( vlc_object_alive (p_demux) )
+ do
{
- block_t *p_pkt;
+ i_pos = p_sys->i_packet_size * i_probe_count;
+ i_pos = __MIN( i_pos, i_stream_size );
+
+ if( stream_Seek( p_sys->stream, i_pos ) )
+ return VLC_EGENERIC;
+
+ ProbeChunk( p_demux, i_program, false, &i_pcr, &b_found );
+
+ /* Go ahead one more chunk if end of file contained only stuffing packets */
+ i_probe_count += PROBE_CHUNK_COUNT;
+ } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (2 * PROBE_CHUNK_COUNT) );
- if( !( p_pkt = ReadTSPacket( p_demux ) ) )
- {
- break;
- }
- mtime_t i_pcr = GetPCR( p_pkt );
- if( i_pcr >= 0 )
- {
- p_sys->i_pid_ref_pcr = PIDGet( p_pkt );
- p_sys->i_first_pcr = i_pcr;
- p_sys->i_current_pcr = i_pcr;
- }
- block_Release( p_pkt );
- if( p_sys->i_first_pcr >= 0 )
- break;
- }
stream_Seek( p_sys->stream, i_initial_pos );
+
+ return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
}
-static void GetLastPCR( demux_t *p_demux )
+static int ProbeEnd( demux_t *p_demux, int i_program )
{
demux_sys_t *p_sys = p_demux->p_sys;
-
const int64_t i_initial_pos = stream_Tell( p_sys->stream );
- mtime_t i_initial_pcr = p_sys->i_current_pcr;
-
int64_t i_stream_size = stream_Size( p_sys->stream );
- int64_t i_last_pos = i_stream_size - p_sys->i_packet_size;
- /* Round i_pos to a multiple of p_sys->i_packet_size */
- int64_t i_pos = i_last_pos - p_sys->i_packet_size * 4500; /* FIXME if the value is not reasonable, please change it. */
- int64_t i_div = i_pos % p_sys->i_packet_size;
- i_pos -= i_div;
-
- if( i_pos <= i_initial_pos && i_pos >= i_stream_size )
- i_pos = i_initial_pos + p_sys->i_packet_size;
- if( i_pos < 0 && i_pos >= i_stream_size )
- return;
- while( vlc_object_alive( p_demux ) )
- {
- if( SeekToPCR( p_demux, i_pos ) )
- break;
- p_sys->i_last_pcr = AdjustPCRWrapAround( p_demux, p_sys->i_current_pcr );
- if( ( i_pos = stream_Tell( p_sys->stream ) ) >= i_last_pos )
- break;
- }
- if( p_sys->i_last_pcr >= 0 )
+ int i_probe_count = PROBE_CHUNK_COUNT;
+ int64_t i_pos;
+ mtime_t i_pcr = -1;
+ bool b_found = false;
+
+ do
{
- int64_t i_size = stream_Size( p_sys->stream );
- mtime_t i_duration_msec = ( p_sys->i_last_pcr - p_sys->i_first_pcr ) * 100 / 9 / 1000;
- int64_t i_rate = ( i_size < 0 || i_duration_msec <= 0 ) ? 0 : i_size * 1000 * 8 / i_duration_msec;
- const int64_t TS_SUPPOSED_MAXRATE = 55 * 1000 * 1000; //FIXME I think it's enough.
- const int64_t TS_SUPPOSED_MINRATE = 0.5 * 1000 * 1000; //FIXME
- if( i_rate < TS_SUPPOSED_MINRATE || i_rate > TS_SUPPOSED_MAXRATE )
- {
- msg_Dbg( p_demux, "calculated bitrate (%"PRId64"bit/s) is too low or too high. min bitrate (%"PRId64"bit/s) max bitrate (%"PRId64"bit/s)",
- i_rate, TS_SUPPOSED_MINRATE, TS_SUPPOSED_MAXRATE );
- p_sys->i_last_pcr = -1;
- }
- }
+ i_pos = i_stream_size - (p_sys->i_packet_size * i_probe_count);
+ i_pos = __MAX( i_pos, 0 );
+
+ if( stream_Seek( p_sys->stream, i_pos ) )
+ return VLC_EGENERIC;
+
+ ProbeChunk( p_demux, i_program, true, &i_pcr, &b_found );
+
+ /* Go ahead one more chunk if end of file contained only stuffing packets */
+ i_probe_count += PROBE_CHUNK_COUNT;
+ } while( i_pos > 0 && (i_pcr == -1 || !b_found) && i_probe_count < (6 * PROBE_CHUNK_COUNT) );
+
stream_Seek( p_sys->stream, i_initial_pos );
- assert( i_initial_pos == stream_Tell( p_sys->stream ) );
- p_sys->i_current_pcr = i_initial_pcr;
+
+ return (b_found) ? VLC_SUCCESS : VLC_EGENERIC;
}
-static void CheckPCR( demux_t *p_demux )
+static void ProgramSetPCR( demux_t *p_demux, ts_prg_psi_t *p_prg, mtime_t i_pcr )
{
- demux_sys_t *p_sys = p_demux->p_sys;
+ demux_sys_t *p_sys = p_demux->p_sys;
- int64_t i_initial_pos = stream_Tell( p_sys->stream );
- mtime_t i_initial_pcr = p_sys->i_current_pcr;
+ /* Check if we have enqueued blocks waiting the/before the
+ PCR barrier, and then adapt pcr so they have valid PCR when dequeuing */
+ if( p_prg->pcr.i_current == -1 )
+ {
+ mtime_t i_mindts = -1;
+ for( int i=MIN_ES_PID; i<=MAX_ES_PID; i++ )
+ {
+ ts_pid_t *p_pid = &p_sys->pid[i];
+ if( p_pid->b_valid && p_pid->i_owner_number == p_prg->i_number && p_pid->es )
+ {
+ block_t *p_block = p_pid->es->p_prepcr_outqueue;
+ while( p_block && p_block->i_dts == VLC_TS_INVALID )
+ p_block = p_block->p_next;
- int64_t i_size = stream_Size( p_sys->stream );
+ if( p_block )
+ msg_Warn( p_demux, "Program %ld %ld *******", i_mindts, p_block->i_dts );
- int i = 0;
- p_sys->p_pcrs[0] = p_sys->i_first_pcr;
- p_sys->p_pos[0] = i_initial_pos;
+ if( p_block && ( i_mindts == -1 || p_block->i_dts < i_mindts ) )
+ {
+ i_mindts = p_block->i_dts;
+ msg_Warn( p_demux, "Program %ld %ld *******", i_mindts, p_block->i_dts );
+ }
+ }
+ }
- for( i = 1; i < p_sys->i_pcrs_num && vlc_object_alive( p_demux ); ++i )
- {
- /* Round i_pos to a multiple of p_sys->i_packet_size */
- int64_t i_pos = i_size / p_sys->i_pcrs_num * i;
- int64_t i_div = i_pos % p_sys->i_packet_size;
- i_pos -= i_div;
- if( SeekToPCR( p_demux, i_pos ) )
- break;
- p_sys->p_pcrs[i] = p_sys->i_current_pcr;
- p_sys->p_pos[i] = stream_Tell( p_sys->stream );
- if( p_sys->p_pcrs[i-1] > p_sys->p_pcrs[i] )
+ if( i_mindts > VLC_TS_INVALID )
{
- msg_Dbg( p_demux, "PCR Wrap Around found between %d%% and %d%% (pcr:%"PRId64"(0x%09"PRIx64") pcr:%"PRId64"(0x%09"PRIx64"))",
- (int)((i-1)*100/p_sys->i_pcrs_num), (int)(i*100/p_sys->i_pcrs_num), p_sys->p_pcrs[i-1], p_sys->p_pcrs[i-1], p_sys->p_pcrs[i], p_sys->p_pcrs[i] );
+ msg_Dbg( p_demux, "Program %d PCR prequeue fixup %"PRId64"->%"PRId64,
+ p_prg->i_number, TO_SCALE(i_mindts), i_pcr );
+ i_pcr = TO_SCALE(i_mindts);
}
}
- if( i < p_sys->i_pcrs_num )
+
+ p_prg->pcr.i_current = i_pcr;
+ if( p_prg->pcr.i_first == -1 )
{
- msg_Dbg( p_demux, "Force Seek Per Percent: Seeking failed at %d%%.", (int)(i*100/p_sys->i_pcrs_num) );
- p_sys->b_force_seek_per_percent = true;
+ p_prg->pcr.i_first = i_pcr; // now seen
}
- stream_Seek( p_sys->stream, i_initial_pos );
- p_sys->i_current_pcr = i_initial_pcr;
+ if ( p_sys->i_pmt_es )
+ {
+ es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
+ p_prg->i_number, VLC_TS_0 + i_pcr * 100 / 9 );
+ }
}
static void PCRHandle( demux_t *p_demux, ts_pid_t *pid, block_t *p_bk )
if( i_pcr < 0 )
return;
- if( p_sys->i_pid_ref_pcr == pid->i_pid )
- p_sys->i_current_pcr = AdjustPCRWrapAround( p_demux, i_pcr );
+ pid->probed.i_pcr_count++;
/* Search program and set the PCR */
- int i_group = -1;
- for( int i = 0; i < p_sys->i_pmt && i_group < 0 ; i++ )
+ for( int i = 0; i < p_sys->i_pmt; i++ )
{
- bool b_pmt_has_es = false;
-
for( int i_prg = 0; i_prg < p_sys->pmt[i]->psi->i_prg; i_prg++ )
{
- if( pid->i_pid == p_sys->pmt[i]->psi->prg[i_prg]->i_pid_pcr )
+ ts_prg_psi_t *p_prg = p_sys->pmt[i]->psi->prg[i_prg];
+ mtime_t i_program_pcr = TimeStampWrapAround( p_prg, i_pcr );
+
+ if( p_prg->i_pid_pcr == 0x1FFF ) /* That program has no dedicated PCR pid ISO/IEC 13818-1 2.4.4.9 */
{
- /* We've found our target group */
- p_sys->pmt[i]->psi->prg[i_prg]->i_pcr_value = i_pcr;
- i_group = p_sys->pmt[i]->psi->prg[i_prg]->i_number;
- for( int j = 0; j < 8192; j++ )
+ if( pid->p_owner ) /* PCR shall be on pid itself */
{
- const ts_pid_t *pid = &p_sys->pid[j];
- if( pid->b_valid && pid->p_owner == p_sys->pmt[i]->psi && pid->es )
- {
- b_pmt_has_es = true;
- break;
- }
+ /* ? update PCR for the whole group program ? */
+ ProgramSetPCR( p_demux, p_prg, i_program_pcr );
+ }
+ else
+ {
+ msg_Warn(p_demux, "discarding PCR update from pid %d which has no owner", pid->i_pid);
+ }
+ break;
+ }
+ else /* set PCR provided by current pid to program(s) referencing it */
+ {
+ /* Can be dedicated PCR pid (no owned then) or another pid (owner == pmt) */
+ if( p_prg->i_pid_pcr == pid->i_pid ) /* If that program references current pid as PCR */
+ {
+ /* We've found a target group for update */
+ ProgramSetPCR( p_demux, p_prg, i_program_pcr );
+ }
+ }
+ }
+ }
+}
+
+static int FindPCRCandidate( demux_sys_t *p_sys, ts_prg_psi_t *p_prg )
+{
+ ts_pid_t *p_cand = NULL;
+ int i_previous = p_prg->i_pid_pcr;
+ for( int i=MIN_ES_PID; i<=MAX_ES_PID; i++ )
+ {
+ ts_pid_t *p_pid = &p_sys->pid[i];
+ if( p_pid->b_seen && p_pid->es && p_pid->es->id &&
+ p_pid->i_owner_number == p_prg->i_number &&
+ p_cand->i_pid != i_previous )
+ {
+ if( p_pid->probed.i_pcr_count ) /* check PCR frequency first */
+ {
+ if( !p_cand || p_pid->probed.i_pcr_count > p_cand->probed.i_pcr_count )
+ {
+ p_cand = p_pid;
+ continue;
}
}
+
+ if( p_pid->es->fmt.i_cat == AUDIO_ES )
+ {
+ if( !p_cand )
+ p_cand = p_pid;
+ }
+ else if ( p_pid->es->fmt.i_cat == VIDEO_ES ) /* Otherwise prioritize video dts */
+ {
+ if( !p_cand || p_cand->es->fmt.i_cat == AUDIO_ES )
+ p_cand = p_pid;
+ }
}
+ }
+
+ if( p_cand )
+ return p_cand->i_pid;
+ else
+ return 0x1FFF;
+}
+
+static void PCRFixHandle( demux_t *p_demux, ts_prg_psi_t *p_prg, block_t *p_block )
+{
+ if( p_prg->pcr.i_first > -1 || p_prg->pcr.b_disable )
+ return;
- if ( p_sys->b_trust_pcr && i_group > 0 && b_pmt_has_es )
- es_out_Control( p_demux->out, ES_OUT_SET_GROUP_PCR,
- i_group, VLC_TS_0 + i_pcr * 100 / 9 );
+ /* Record the first data packet timestamp in case there wont be any PCR */
+ if( !p_prg->pcr.i_first_dts )
+ {
+ p_prg->pcr.i_first_dts = p_block->i_dts;
+ }
+ else if( p_block->i_dts - p_prg->pcr.i_first_dts > CLOCK_FREQ / 2 ) /* "shall not exceed 100ms" */
+ {
+ int i_cand = FindPCRCandidate( p_demux->p_sys, p_prg );
+ p_prg->i_pid_pcr = i_cand;
+ p_prg->pcr.b_disable = true; /* So we do not wait packet PCR flag as there might be none on the pid */
+ msg_Warn( p_demux, "No PCR received for program %d, set up workaround using pid %d",
+ p_prg->i_number, i_cand );
}
}
{
demux_sys_t *p_sys = p_demux->p_sys;
- if( ( p_sys->i_current_program == -1 && p_sys->programs_list.i_count == 0 ) ||
- p_sys->i_current_program == 0 )
- return true;
- if( p_sys->i_current_program == i_pgrm )
- return true;
+ for(int i=0; i<p_sys->programs.i_size; i++)
+ if( p_sys->programs.p_elems[i] == i_pgrm )
+ return true;
- if( p_sys->programs_list.i_count != 0 )
- {
- for( int i = 0; i < p_sys->programs_list.i_count; i++ )
- {
- if( i_pgrm == p_sys->programs_list.p_values[i].i_int )
- return true;
- }
- }
return false;
}
msg_Dbg( p_demux, "SDTCallBack called" );
- if( sdt->psi->i_sdt_version != -1 &&
- ( !p_sdt->b_current_next ||
- p_sdt->i_version == sdt->psi->i_sdt_version ) )
+ if( p_sys->es_creation != CREATE_ES ||
+ !p_sdt->b_current_next ||
+ p_sdt->i_version == sdt->psi->i_sdt_version )
{
dvbpsi_DeleteSDT( p_sdt );
return;
p_srv->b_eit_present, p_srv->i_running_status,
p_srv->b_free_ca );
+ if( p_sys->vdr.i_service && p_srv->i_service_id != p_sys->vdr.i_service )
+ {
+ msg_Dbg( p_demux, " * service id=%d skipped (not declared in vdr header)",
+ p_sys->vdr.i_service );
+ continue;
+ }
+
p_meta = vlc_meta_New();
for( p_dr = p_srv->p_first_descriptor; p_dr; p_dr = p_dr->p_next )
{
if( p_epg->i_event > 0 )
{
if( b_current_following &&
- ( p_sys->i_current_program == -1 ||
- p_sys->i_current_program ==
+ ( p_sys->programs.i_size == 0 ||
+ p_sys->programs.p_elems[0] ==
#if (DVBPSI_VERSION_INT >= DVBPSI_VERSION_WANTED(1,0,0))
p_eit->i_extension
#else
p_fmt->i_cat = AUDIO_ES;
p_fmt->i_codec = VLC_CODEC_BD_LPCM;
break;
+ case 0x81:
+ p_fmt->i_cat = AUDIO_ES;
+ p_fmt->i_codec = VLC_CODEC_A52;
+ break;
case 0x82:
case 0x85: /* DTS-HD High resolution audio */
case 0x86: /* DTS-HD Master audio */
case 0x91: /* Interactive graphics */
case 0x92: /* Subtitle */
return false;
+ case 0xEA:
+ p_fmt->i_cat = VIDEO_ES;
+ p_fmt->i_codec = VLC_CODEC_VC1;
+ break;
default:
msg_Info( p_demux, "HDMV registration not implemented for pid 0x%x type 0x%x",
p_es->i_pid, p_es->i_type );
pid->es->fmt.i_extra_languages );
if( pid->es->fmt.p_extra_languages )
{
- for( int i = 0; i < pid->es->fmt.i_extra_languages; i++ )
+ for( unsigned i = 0; i < pid->es->fmt.i_extra_languages; i++ )
{
pid->es->fmt.p_extra_languages[i].psz_language = malloc(4);
if( pid->es->fmt.p_extra_languages[i].psz_language )
#endif
}
+static void AddAndCreateES( demux_t *p_demux, ts_pid_t *pid )
+{
+ demux_sys_t *p_sys = p_demux->p_sys;
+ bool b_create_delayed = false;
+
+ if( pid )
+ {
+ if( pid->b_seen && p_sys->es_creation == DELAY_ES )
+ {
+ p_sys->es_creation = CREATE_ES;
+ b_create_delayed = true;
+ }
+
+ if( p_sys->es_creation == CREATE_ES )
+ {
+ pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
+ for( int i = 0; i < pid->i_extra_es; i++ )
+ {
+ pid->extra_es[i]->id =
+ es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
+ }
+ p_sys->i_pmt_es += 1 + pid->i_extra_es;
+ }
+ }
+ else if( p_sys->es_creation == DELAY_ES )
+ {
+ p_sys->es_creation = CREATE_ES;
+ b_create_delayed = true;
+ }
+
+ if( b_create_delayed )
+ {
+ for(int i=MIN_ES_PID; i<=MAX_ES_PID; i++)
+ {
+ pid = &p_sys->pid[i];
+ if(!pid->es || pid->es->id)
+ continue;
+ pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
+ for( int j = 0; j < pid->i_extra_es; j++ )
+ {
+ pid->extra_es[j]->id =
+ es_out_Add( p_demux->out, &pid->extra_es[j]->fmt );
+ }
+ p_sys->i_pmt_es += 1 + pid->i_extra_es;
+
+ if( pid->es->id != NULL && ProgramIsSelected( p_demux, pid->i_owner_number ) )
+ SetPIDFilter( p_demux, pid->i_pid, true );
+ }
+ }
+}
+
static void PMTCallBack( void *data, dvbpsi_pmt_t *p_pmt )
{
demux_t *p_demux = data;
dvbpsi_pmt_es_t *p_es;
for( p_es = p_pmt->p_first_es; p_es != NULL; p_es = p_es->p_next )
{
- ts_pid_t tmp_pid, *old_pid = 0, *pid = &tmp_pid;
+ ts_pid_t tmp_pid = {0}, *old_pid = {0}, *pid = &tmp_pid;
/* Find out if the PID was already declared */
for( int i = 0; i < i_clean; i++ )
}
else
{
- pid->es->id = es_out_Add( p_demux->out, &pid->es->fmt );
- for( int i = 0; i < pid->i_extra_es; i++ )
- {
- pid->extra_es[i]->id =
- es_out_Add( p_demux->out, &pid->extra_es[i]->fmt );
- }
- p_sys->i_pmt_es += 1 + pid->i_extra_es;
+ AddAndCreateES( p_demux, pid );
}
}
}
if( i_clean )
free( pp_clean );
+
+ if( !p_sys->b_trust_pcr )
+ {
+ int i_cand = FindPCRCandidate( p_demux->p_sys, prg );
+ prg->i_pid_pcr = i_cand;
+ prg->pcr.b_disable = true;
+ msg_Warn( p_demux, "PCR not trusted for program %d, set up workaround using pid %d",
+ prg->i_number, i_cand );
+ }
+
+ /* Probe Boundaries */
+ if( p_sys->b_canfastseek && prg->i_last_dts == -1 )
+ {
+ prg->i_last_dts = 0;
+ ProbeStart( p_demux, prg->i_number );
+ ProbeEnd( p_demux, prg->i_number );
+ }
+}
+
+static int PATCheck( demux_t *p_demux, dvbpsi_pat_t *p_pat )
+{
+ /* Some Dreambox streams have all PMT set to same pid */
+ int i_prev_pid = -1;
+ for( dvbpsi_pat_program_t * p_program = p_pat->p_first_program;
+ p_program != NULL;
+ p_program = p_program->p_next )
+ {
+ if( p_program->i_pid == i_prev_pid )
+ {
+ msg_Warn( p_demux, "PAT check failed: duplicate program pid %d", i_prev_pid );
+ return VLC_EGENERIC;
+ }
+ i_prev_pid = p_program->i_pid;
+ }
+ return VLC_SUCCESS;
}
static void PATCallBack( void *data, dvbpsi_pat_t *p_pat )
if( ( pat->psi->i_pat_version != -1 &&
( !p_pat->b_current_next ||
p_pat->i_version == pat->psi->i_pat_version ) ) ||
- p_sys->b_user_pmt )
+ p_sys->b_user_pmt || PATCheck( p_demux, p_pat ) )
{
dvbpsi_DeletePAT( p_pat );
return;
prg->i_pid_pmt = p_program->i_pid;
/* Now select PID at access level */
- if( ProgramIsSelected( p_demux, p_program->i_number ) )
+ if( p_sys->programs.i_size == 0 ||
+ ProgramIsSelected( p_demux, p_program->i_number ) )
{
- if( p_sys->i_current_program == 0 )
- p_sys->i_current_program = p_program->i_number;
+ if( p_sys->programs.i_size == 0 )
+ {
+ p_sys->b_default_selection = true;
+ ARRAY_APPEND( p_sys->programs, p_program->i_number );
+ }
if( SetPIDFilter( p_demux, p_program->i_pid, true ) )
p_sys->b_access_control = false;
+ else if ( p_sys->es_creation == DELAY_ES )
+ p_sys->es_creation = CREATE_ES;
}
}
pat->psi->i_pat_version = p_pat->i_version;