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 MAXPID 0x2000 // for arrays that use a PID as the index
55 #define PTSTICKS 90000 // number of PTS ticks per second
56 #define PCRFACTOR 300 // conversion from 27MHz PCR extension to 90kHz PCR base
57 #define MAX33BIT 0x00000001FFFFFFFFLL // max. possible value with 33 bit
58 #define MAX27MHZ ((MAX33BIT + 1) * PCRFACTOR - 1) // max. possible PCR value
130 return ((((int64_t)p[ 6]) << 25) |
131 (((int64_t)p[ 7]) << 17) |
132 (((int64_t)p[ 8]) << 9) |
133 (((int64_t)p[ 9]) << 1) |
135 (((((int)p[10]) & 0x01) << 8) |
169 return 6 + p[4] * 256 + p[5];
179 return (p[7] & 0x80) && p[8] >= 5;
184 return (p[7] & 0x40) && p[8] >= 10;
189 return ((((int64_t)p[ 9]) & 0x0E) << 29) |
190 (( (int64_t)p[10]) << 22) |
191 ((((int64_t)p[11]) & 0xFE) << 14) |
192 (( (int64_t)p[12]) << 7) |
193 ((((int64_t)p[13]) & 0xFE) >> 1);
198 return ((((int64_t)p[14]) & 0x0E) << 29) |
199 (( (int64_t)p[15]) << 22) |
200 ((((int64_t)p[16]) & 0xFE) << 14) |
201 (( (int64_t)p[17]) << 7) |
202 ((((int64_t)p[18]) & 0xFE) >> 1);
210 inline int64_t
PtsAdd(int64_t Pts1, int64_t Pts2) {
return (Pts1 + Pts2) &
MAX33BIT; }
212 int64_t
PtsDiff(int64_t Pts1, int64_t Pts2);
235 void Setup(
uchar *Data,
int Length,
int Pid = -1);
277 bool Find(uint32_t Code);
289 #define MAX_SECTION_SIZE 4096 // maximum size of an SI section
290 #define MAX_PMT_TS (MAX_SECTION_SIZE / TS_SIZE + 1)
346 #define MAX_PMT_PIDS 32
374 int SectionLength(
const uchar *Data,
int Length) {
return (Length >= 3) ? ((int(Data[1]) & 0x0F) << 8)| Data[2] : 0; }
394 bool GetVersions(
int &PatVersion,
int &PmtVersion)
const;
482 void TsDump(
const char *Name,
const u_char *Data,
int Length);
483 void PesDump(
const char *Name,
const u_char *Data,
int Length);
487 #define MIN_TS_PACKETS_FOR_FRAME_DETECTOR 100
515 void SetPid(
int Pid,
int Type);
538 #define PATCH_NALUDUMP 100
bool ParsePatPmt(const uchar *Data, int Length)
Parses the given Data (which may consist of several TS packets, typically an entire frame) and extrac...
void ParsePat(const uchar *Data, int Length)
Parses the PAT data from the single TS packet in Data.
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.
uchar GetByte(void)
Gets the next byte of the TS payload, skipping any intermediate TS header data.
uchar subtitlingTypes[MAXSPIDS]
void SetVersions(int PatVersion, int PmtVersion)
Sets the version numbers for the generated PAT and PMT, in case this generator is used to...
const char * Alang(int i) const
bool TsError(const uchar *p)
void SetPid(int Pid, int Type)
Sets the Pid and stream Type to detect frames for.
int PesPayloadOffset(const uchar *p)
void IncCounter(int &Counter, uchar *TsPacket)
bool SkipBytes(int Bytes)
Skips the given number of bytes in the payload and returns true if there is still data left to read...
bool TsHasAdaptationField(const uchar *p)
void ParsePmt(const uchar *Data, int Length)
Parses the PMT data from the single TS packet in Data.
uint16_t ancillaryPageIds[MAXSPIDS]
bool IsPmtPid(int Pid) const
Returns true if Pid the one of the PMT pids as defined by the current PAT.
char alangs[MAXAPIDS][MAXLANGCODE2]
bool TsPayloadStart(const uchar *p)
int64_t TsGetDts(const uchar *p, int l)
void TsExtendAdaptionField(unsigned char *Packet, int ToLength)
int MakeLanguageDescriptor(uchar *Target, const char *Language)
uchar SubtitlingType(int i) const
void GeneratePmtPid(const cChannel *Channel)
Generates a PMT pid that doesn't collide with any of the actual pids of the Channel.
int64_t PesGetPts(const uchar *p)
int Analyze(const uchar *Data, int Length)
Analyzes the TS packets pointed to by Data.
long long int GetDroppedPackets()
bool TsHasPayload(const uchar *p)
void TsHidePayload(uchar *p)
uint32_t ptsValues[MaxPtsValues]
char slangs[MAXSPIDS][MAXLANGCODE2]
#define TS_SCRAMBLING_CONTROL
#define TS_ADAPT_FIELD_EXISTS
void IncEsInfoLength(int Length)
int MakeCRC(uchar *Target, const uchar *Data, int Length)
int Vpid(void) const
Returns the video pid as defined by the current PMT, or 0 if no video pid has been detected...
int MakeTeletextDescriptor(uchar *Target, const tTeletextSubtitlePage *pages, int pageCount)
int Tpid(void)
Returns the teletext pid as defined by the current PMT, or 0 if no teletext pid has been detected...
void BlockDump(const char *Name, const u_char *Data, int Length)
void SetChannel(const cChannel *Channel)
Sets the Channel for which the PAT/PMT shall be generated.
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)
cPatPmtGenerator(const cChannel *Channel=NULL)
int MakeSubtitlingDescriptor(uchar *Target, const char *Language, uchar SubtitlingType, uint16_t CompositionPageId, uint16_t AncillaryPageId)
void Setup(uchar *Data, int Length, int Pid=-1)
Sets up this TS payload handler with the given Data, which points to a sequence of Length bytes of co...
cPatPmtParser * pPatPmtParser
int MakeAC3Descriptor(uchar *Target, uchar Type)
tTeletextSubtitlePage teletextSubtitlePages[MAXTXTPAGES]
void GeneratePat(void)
Generates a PAT section for later use with GetPat().
bool Find(uint32_t Code)
Searches for the four byte sequence given in Code and returns true if it was found within the payload...
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...
int MakeStream(uchar *Target, uchar Type, int Pid)
bool Eof(void) const
Returns true if all available bytes of the TS payload have been processed.
uchar pmt[MAX_PMT_TS][TS_SIZE]
bool TsSetPayload(const uchar *p)
bool Synced(void)
Returns true if the frame detector has synced on the data stream.
bool PesLongEnough(int Length)
long long int GetTotalPackets()
int64_t TsGetPts(const uchar *p, int l)
int TsPid(const uchar *p)
const char * Slang(int i) const
long long int DroppedPackets
cFrameDetector(int Pid=0, int Type=0)
Sets up a frame detector for the given Pid and stream Type.
int TotalTeletextSubtitlePages() 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...
int TsContinuityCounter(const uchar *p)
const int * Spids(void) const
char dlangs[MAXDPIDS][MAXLANGCODE2]
void Reset(void)
Resets the converter.
uchar TsGetContinuityCounter(const uchar *p)
bool PesHasDts(const uchar *p)
void TsDump(const char *Name, const u_char *Data, int Length)
cPatPmtParser(bool UpdatePrimaryDevice=false)
int GetLastIndex(void)
Returns the index into the TS data of the payload byte that has most recently been read...
void PesSetDts(uchar *p, int64_t Dts)
const int * Dpids(void) const
void PutTs(const uchar *Data, int Length)
Puts the payload data of the single TS packet at Data into the converter.
uchar * GetPmt(int &Index)
Returns a pointer to the Index'th TS packet of the PMT section.
void TsSetContinuityCounter(uchar *p, uchar Counter)
void ProcessPayload(unsigned char *Payload, int size, bool PayloadStart, sPayloadInfo &Info)
int TsGetPayload(const uchar **p)
long long int TotalPackets
void GeneratePmt(const cChannel *Channel)
Generates a PMT section for the given Channel, for later use with GetPmt().
uchar pmt[MAX_SECTION_SIZE]
uint16_t compositionPageIds[MAXSPIDS]
void PesSetPts(uchar *p, int64_t Pts)
int pmtPids[MAX_PMT_PIDS+1]
const int * Apids(void) const
static void SetBrokenLink(uchar *Data, int Length)
void Statistics(void) const
May be called after a new frame has been detected, and will log a warning if the number of TS packets...
int64_t TsGetPcr(const uchar *p)
void TsSetPts(uchar *p, int l, int64_t Pts)
bool ProcessTSPacket(unsigned char *Packet)
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.
int totalTtxtSubtitlePages
bool TsIsScrambled(const uchar *p)
bool GetVersions(int &PatVersion, int &PmtVersion) const
Returns true if a valid PAT/PMT has been parsed and stores the current version numbers in the given v...
int64_t PesGetDts(const uchar *p)
void SetByte(uchar Byte, int Index)
Sets the TS data byte at the given Index to the value Byte.
const char * Dlang(int i) const
int DropPayloadStartBytes
double FramesPerSecond(void)
Returns the number of frames per second, or 0 if this information is not available.
eNaluFillState NaluFillState
void IncVersion(int &Version)
const uchar * GetPes(int &Length)
Gets a pointer to the complete PES packet, or NULL if the packet is not complete yet.
uint16_t AncillaryPageId(int i) const
int TsPayloadOffset(const uchar *p)
void SetRepeatLast(void)
Makes the next call to GetPes() return exactly the same data as the last one (provided there was no c...
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.
void SetPatPmtParser(cPatPmtParser *_pPatPmtParser)
void PutBuffer(uchar *Data, int Length)
void Reset(void)
Resets the parser.
ePesHeader AnalyzePesHeader(const uchar *Data, int Count, int &PesPayloadOffset, bool *ContinuationHeader=NULL)
uchar * GetPat(void)
Returns a pointer to the PAT section, which consists of exactly one TS packet.
uchar * GetBuffer(int &OutLength)
const tTeletextSubtitlePage * TeletextSubtitlePages() const
bool SkipPesHeader(void)
Skips all bytes belonging to the PES header of the payload.
uchar tempBuffer[TS_SIZE]