33 #define TS_SYNC_BYTE 0x47 36 #define TS_PAYLOAD_START 0x40 37 #define TS_TRANSPORT_PRIORITY 0x20 38 #define TS_PID_MASK_HI 0x1F 39 #define TS_SCRAMBLING_CONTROL 0xC0 40 #define TS_ADAPT_FIELD_EXISTS 0x20 41 #define TS_PAYLOAD_EXISTS 0x10 42 #define TS_CONT_CNT_MASK 0x0F 43 #define TS_ADAPT_DISCONT 0x80 44 #define TS_ADAPT_RANDOM_ACC 0x40 // would be perfect for detecting independent frames, but unfortunately not used by all broadcasters 45 #define TS_ADAPT_ELEM_PRIO 0x20 46 #define TS_ADAPT_PCR 0x10 47 #define TS_ADAPT_OPCR 0x08 48 #define TS_ADAPT_SPLICING 0x04 49 #define TS_ADAPT_TP_PRIVATE 0x02 50 #define TS_ADAPT_EXTENSION 0x01 52 #define PATPID 0x0000 // PAT PID (constant 0) 53 #define CATPID 0x0001 // CAT PID (constant 1) 54 #define EITPID 0x0012 // EIT PID (constant 18) 55 #define MAXPID 0x2000 // for arrays that use a PID as the index 57 #define PTSTICKS 90000 // number of PTS ticks per second 58 #define PCRFACTOR 300 // conversion from 27MHz PCR extension to 90kHz PCR base 59 #define MAX33BIT 0x00000001FFFFFFFFLL // max. possible value with 33 bit 60 #define MAX27MHZ ((MAX33BIT + 1) * PCRFACTOR - 1) // max. possible PCR value 128 return ((((int64_t)p[ 6]) << 25) |
129 (((int64_t)p[ 7]) << 17) |
130 (((int64_t)p[ 8]) << 9) |
131 (((int64_t)p[ 9]) << 1) |
133 (((((int)p[10]) & 0x01) << 8) |
149 #define TS_SYNC(Data, Length) (*Data == TS_SYNC_BYTE ? 0 : TsSync(Data, Length, __FILE__, __FUNCTION__, __LINE__)) 150 int TsSync(
const uchar *Data,
int Length,
const char *File = NULL,
const char *Function = NULL,
int Line = 0);
175 return 6 + p[4] * 256 + p[5];
185 return (p[7] & 0x80) && p[8] >= 5;
190 return (p[7] & 0x40) && p[8] >= 10;
195 return ((((int64_t)p[ 9]) & 0x0E) << 29) |
196 (( (int64_t)p[10]) << 22) |
197 ((((int64_t)p[11]) & 0xFE) << 14) |
198 (( (int64_t)p[12]) << 7) |
199 ((((int64_t)p[13]) & 0xFE) >> 1);
204 return ((((int64_t)p[14]) & 0x0E) << 29) |
205 (( (int64_t)p[15]) << 22) |
206 ((((int64_t)p[16]) & 0xFE) << 14) |
207 (( (int64_t)p[17]) << 7) |
208 ((((int64_t)p[18]) & 0xFE) >> 1);
216 inline int64_t
PtsAdd(int64_t Pts1, int64_t Pts2) {
return (Pts1 + Pts2) &
MAX33BIT; }
218 int64_t
PtsDiff(int64_t Pts1, int64_t Pts2);
241 void Setup(
uchar *Data,
int Length,
int Pid = -1);
262 bool Eof(
void)
const {
return index >= length; }
264 void Statistics(
void)
const;
270 bool SkipBytes(
int Bytes);
273 bool SkipPesHeader(
void);
275 int GetLastIndex(
void);
278 void SetByte(
uchar Byte,
int Index);
283 bool Find(uint32_t Code);
295 #define MAX_SECTION_SIZE 4096 // maximum size of an SI section 296 #define MAX_PMT_TS (MAX_SECTION_SIZE / TS_SIZE + 1) 309 void IncCounter(
int &Counter,
uchar *TsPacket);
310 void IncVersion(
int &Version);
311 void IncEsInfoLength(
int Length);
313 int MakeStream(
uchar *Target,
uchar Type,
int Pid);
314 int MakeAC3Descriptor(
uchar *Target,
uchar Type);
315 int MakeSubtitlingDescriptor(
uchar *Target,
const char *Language,
uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId);
316 int MakeLanguageDescriptor(
uchar *Target,
const char *Language);
317 int MakeCRC(
uchar *Target,
const uchar *Data,
int Length);
318 void GeneratePmtPid(
const cChannel *Channel);
321 void GeneratePat(
void);
323 void GeneratePmt(
const cChannel *Channel);
328 void SetVersions(
int PatVersion,
int PmtVersion);
337 void SetChannel(
const cChannel *Channel);
342 uchar *GetPmt(
int &Index);
351 #define MAX_PMT_PIDS 32 377 int SectionLength(
const uchar *Data,
int Length) {
return (Length >= 3) ? ((int(Data[1]) & 0x0F) << 8)| Data[2] : 0; }
383 void ParsePat(
const uchar *Data,
int Length);
386 void ParsePmt(
const uchar *Data,
int Length);
393 bool ParsePatPmt(
const uchar *Data,
int Length);
397 bool GetVersions(
int &PatVersion,
int &PmtVersion)
const;
400 bool IsPmtPid(
int Pid)
const {
for (
int i = 0; pmtPids[i]; i++)
if (pmtPids[i] == Pid)
return true;
return false; }
403 int Vpid(
void)
const {
return vpid; }
406 int Ppid(
void)
const {
return ppid; }
409 int Vtype(
void)
const {
return vtype; }
414 const int *
Apids(
void)
const {
return apids; }
415 const int *
Dpids(
void)
const {
return dpids; }
416 const int *
Spids(
void)
const {
return spids; }
417 int Apid(
int i)
const {
return (0 <= i && i <
MAXAPIDS) ? apids[i] : 0; }
418 int Dpid(
int i)
const {
return (0 <= i && i <
MAXDPIDS) ? dpids[i] : 0; }
419 int Spid(
int i)
const {
return (0 <= i && i <
MAXSPIDS) ? spids[i] : 0; }
422 const char *
Alang(
int i)
const {
return (0 <= i && i <
MAXAPIDS) ? alangs[i] :
""; }
423 const char *
Dlang(
int i)
const {
return (0 <= i && i <
MAXDPIDS) ? dlangs[i] :
""; }
424 const char *
Slang(
int i)
const {
return (0 <= i && i <
MAXSPIDS) ? slangs[i] :
""; }
437 uint16_t YMDtoMJD(
int Y,
int M,
int D);
438 uchar *AddParentalRatingDescriptor(
uchar *p,
uchar ParentalRating = 0);
441 uchar *Generate(
int Sid);
462 void PutTs(
const uchar *Data,
int Length);
472 const uchar *GetPes(
int &Length);
486 void SetRepeatLast(
void);
498 void TsDump(
const char *Name,
const u_char *Data,
int Length);
499 void PesDump(
const char *Name,
const u_char *Data,
int Length);
503 #define MIN_TS_PACKETS_FOR_FRAME_DETECTOR 100 509 enum { MaxPtsValues = 150 };
515 uint32_t ptsValues[MaxPtsValues];
530 void SetPid(
int Pid,
int Type);
532 int Analyze(
const uchar *Data,
int Length);
int64_t PtsAdd(int64_t Pts1, int64_t Pts2)
Adds the given PTS values, taking into account the 33bit wrap around.
int Used(void)
Returns the number of raw bytes that have already been used (e.g.
const char * Alang(int i) const
bool TsError(const uchar *p)
int PesPayloadOffset(const uchar *p)
bool TsHasAdaptationField(const uchar *p)
bool IsPmtPid(int Pid) const
Returns true if Pid the one of the PMT pids as defined by the current PAT.
bool TsPayloadStart(const uchar *p)
int64_t TsGetDts(const uchar *p, int l)
uchar SubtitlingType(int i) const
int64_t PesGetPts(const uchar *p)
bool TsHasPayload(const uchar *p)
void TsHidePayload(uchar *p)
#define TS_SCRAMBLING_CONTROL
#define TS_ADAPT_FIELD_EXISTS
void TsSetPid(uchar *p, int Pid)
int Vpid(void) const
Returns the video pid as defined by the current PMT, or 0 if no video pid has been detected...
void BlockDump(const char *Name, const u_char *Data, int Length)
void TsSetDts(uchar *p, int l, int64_t Dts)
bool AtPayloadStart(void)
Returns true if this payload handler is currently pointing to the first byte of a TS packet that star...
bool PesHasPts(const uchar *p)
int SectionLength(const uchar *Data, int Length)
int Available(void)
Returns the number of raw bytes (including any TS headers) still available in the TS payload handler...
bool Eof(void) const
Returns true if all available bytes of the TS payload have been processed.
bool Synced(void)
Returns true if the frame detector has synced on the data stream.
bool PesLongEnough(int Length)
int64_t TsGetPts(const uchar *p, int l)
int TsPid(const uchar *p)
const char * Slang(int i) const
#define TS_PAYLOAD_EXISTS
int PesLength(const uchar *p)
void TsSetPcr(uchar *p, int64_t Pcr)
void PesDump(const char *Name, const u_char *Data, int Length)
int Vtype(void) const
Returns the video stream type as defined by the current PMT, or 0 if no video stream type has been de...
const int * Spids(void) const
bool PesHasDts(const uchar *p)
void TsDump(const char *Name, const u_char *Data, int Length)
void PesSetDts(uchar *p, int64_t Dts)
const int * Dpids(void) const
void TsSetContinuityCounter(uchar *p, uchar Counter)
int TsGetPayload(const uchar **p)
void PesSetPts(uchar *p, int64_t Pts)
bool Completed(void)
Returns true if the PMT has been completely parsed.
const int * Apids(void) const
static void SetBrokenLink(uchar *Data, int Length)
int64_t TsGetPcr(const uchar *p)
void TsSetPts(uchar *p, int l, int64_t Pts)
bool PesHasLength(const uchar *p)
uint16_t CompositionPageId(int i) const
bool NewFrame(void)
Returns true if the data given to the last call to Analyze() started a new frame. ...
bool IndependentFrame(void)
Returns true if a new frame was detected and this is an independent frame (i.e.
bool TsIsScrambled(const uchar *p)
int64_t PesGetDts(const uchar *p)
const char * Dlang(int i) const
double FramesPerSecond(void)
Returns the number of frames per second, or 0 if this information is not available.
uchar TsContinuityCounter(const uchar *p)
uint16_t AncillaryPageId(int i) const
int TsPayloadOffset(const uchar *p)
int TsSync(const uchar *Data, int Length, const char *File=NULL, const char *Function=NULL, int Line=0)
int Ppid(void) const
Returns the PCR pid as defined by the current PMT, or 0 if no PCR pid has been detected, yet.
bool AtTsStart(void)
Returns true if this payload handler is currently pointing to first byte of a TS packet.
int64_t PtsDiff(int64_t Pts1, int64_t Pts2)
Returns the difference between two PTS values.
ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader=NULL)