vdr-plugin-softhddevice-drm-gles 1.4.0
pes.h
Go to the documentation of this file.
1
18#ifndef __SOFTHDDEVICE_PES_H
19#define __SOFTHDDEVICE_PES_H
20
21#include <vector>
22#include <map>
23
24#include <stdint.h>
25#include <stddef.h>
26
27extern "C"
28{
29#include <libavcodec/avcodec.h>
30}
31
38class cPes
39{
40public:
41 cPes(const uint8_t *, int);
42 bool IsValid();
43 bool HasPts();
44 int64_t GetPts();
45 const uint8_t *GetPayload();
46 int GetPayloadSize();
47 int GetPacketLength();
48 uint8_t GetStreamId() { return m_data[3]; }
49
50protected:
51 virtual bool IsStreamIdValid() = 0;
52 void Init();
53 bool IsHeaderValid();
54
55 bool m_valid = false;
56 const uint8_t *m_data;
57 int m_size;
58
59 // According to H.222.0 03/2017 Table 2-21 ("PES packet") packet_start_code_prefix
60 // And also according to H.264/HEVC payload
61 static constexpr uint32_t PES_PACKET_START_CODE_PREFIX = 0x00'0001;
62 static constexpr uint32_t PES_PACKET_START_CODE_PREFIX_LEN = 3;
63};
64
70class cPesVideo : public cPes {
71public:
72 cPesVideo(const uint8_t *data, int size) : cPes(data, size) { cPes::Init(); }
73private:
74 bool IsStreamIdValid() override { return (GetStreamId() & 0xF0) == 0xE0; } // Video stream IDs are in the range 0xE0-0xEF
75};
76
83class cPesAudio : public cPes {
84public:
85 cPesAudio(const uint8_t *data, int size) : cPes(data, size) { cPes::Init(); }
86 bool IsAudioStreamId() { return (GetStreamId() & 0xF0) == 0xC0; } // Audio stream IDs are in the range 0xC0-0xCF
87private:
88 bool IsStreamIdValid() override { return IsAudioStreamId() || IsPrivateStreamId(); }
89 bool IsPrivateStreamId() { return GetStreamId() == 0xBD; }
90};
91
100public:
101 cPtsTrackingBuffer(const char *identifier) : m_identifier(identifier) {}
102 void Push(const uint8_t *, int, int64_t);
103 void Erase(size_t);
104 int64_t GetPts();
105 const uint8_t *Peek() { return &m_data[0]; }
106 void Reset() { m_data.clear(); m_pts.clear(); }
107 int GetSize() { return m_data.size(); }
108 const char *GetIdentifier() { return m_identifier; }
109private:
110 const char *m_identifier;
111 std::map<size_t, int64_t> m_pts;
112 std::vector<uint8_t> m_data;
113};
114
122public:
123 virtual void Push(const uint8_t *data, int size, int64_t pts) { m_buffer.Push(data, size, pts); }
124 virtual AVPacket *PopAvPacket() = 0;
125 bool IsEmpty() { return m_buffer.GetSize() == 0; }
126 size_t GetSize() { return m_buffer.GetSize(); }
127 void Reset();
128 AVCodecID GetCodec() { return m_codec; }
129protected:
130 cReassemblyBuffer(const char *identifier) : m_buffer(identifier) {}
131 AVPacket *PopAvPacket(int);
132 AVCodecID m_codec = AV_CODEC_ID_NONE;
135};
136
144public:
146 AVPacket *PopAvPacket() override { return cReassemblyBuffer::PopAvPacket(m_buffer.GetSize()); }
147 bool ParseCodecHeader(const uint8_t *, int);
148 bool HasLeadingZero(const uint8_t *, int);
149private:
150 static constexpr uint32_t VIDEO_FRAME_START_CODE = 0x00'0001;
151 static constexpr int VIDEO_FRAME_START_CODE_LEN = 3;
152
153 static constexpr uint8_t MPEG2_STREAM_TYPE = 0xB3;
154 static constexpr uint8_t H264_STREAM_TYPE = 0x09;
155 static constexpr uint8_t HEVC_STREAM_TYPE = 0x46;
156};
157
162 AVCodecID codecId;
163 int pos;
164};
165
173public:
175 AVPacket *PopAvPacket() override;
177 SyncWordInfo FindSyncWord(const uint8_t *, int );
178 AVCodecID DetectCodecFromSyncWord(const uint8_t *, int);
179 int GetFrameSizeForCodec(AVCodecID, const uint8_t *);
180private:
182 static constexpr int MAX_HEADER_SIZE = 6;
183 bool m_ptsInvalid = false;
184};
185
186#endif
Audio PES packet parser.
Definition: pes.h:83
cPesAudio(const uint8_t *data, int size)
Definition: pes.h:85
bool IsStreamIdValid() override
Definition: pes.h:88
bool IsPrivateStreamId()
Definition: pes.h:89
bool IsAudioStreamId()
Definition: pes.h:86
Video PES packet parser.
Definition: pes.h:70
bool IsStreamIdValid() override
Definition: pes.h:74
cPesVideo(const uint8_t *data, int size)
Definition: pes.h:72
PES packet parser class.
Definition: pes.h:39
bool IsValid()
Check if the PES packet is valid.
Definition: pes.cpp:256
static constexpr uint32_t PES_PACKET_START_CODE_PREFIX
Definition: pes.h:61
bool m_valid
flag indicating if the PES packet is valid
Definition: pes.h:55
uint8_t GetStreamId()
Definition: pes.h:48
int GetPayloadSize()
Get the size of the PES payload.
Definition: pes.cpp:324
int m_size
size of the PES packet
Definition: pes.h:57
cPes(const uint8_t *, int)
Construct a PES packet parser.
Definition: pes.cpp:215
int GetPacketLength()
Get the total length of the PES packet.
Definition: pes.cpp:346
const uint8_t * GetPayload()
Get a pointer to the PES payload data.
Definition: pes.cpp:311
const uint8_t * m_data
pointer to the raw PES packet data
Definition: pes.h:56
virtual bool IsStreamIdValid()=0
void Init()
Initialize and validate the PES packet.
Definition: pes.cpp:231
bool IsHeaderValid()
Check if the PES header is valid.
Definition: pes.cpp:269
bool HasPts()
Check if the PES packet contains a Presentation Time Stamp (PTS)
Definition: pes.cpp:282
int64_t GetPts()
Get the Presentation Time Stamp (PTS) from the PES header.
Definition: pes.cpp:295
static constexpr uint32_t PES_PACKET_START_CODE_PREFIX_LEN
Definition: pes.h:62
Buffer that tracks PTS values at specific byte positions.
Definition: pes.h:99
const char * GetIdentifier()
Definition: pes.h:108
int GetSize()
Definition: pes.h:107
void Push(const uint8_t *, int, int64_t)
Push data into the PTS tracking buffer.
Definition: pes.cpp:650
std::vector< uint8_t > m_data
Byte buffer.
Definition: pes.h:112
cPtsTrackingBuffer(const char *identifier)
Definition: pes.h:101
std::map< size_t, int64_t > m_pts
Map of buffer positions to PTS values.
Definition: pes.h:111
void Reset()
Definition: pes.h:106
const uint8_t * Peek()
Definition: pes.h:105
void Erase(size_t)
Erase data from the beginning of the buffer.
Definition: pes.cpp:670
const char * m_identifier
Definition: pes.h:110
int64_t GetPts()
Get the PTS value for the current buffer position.
Definition: pes.cpp:707
Audio stream reassembly buffer.
Definition: pes.h:172
AVPacket * PopAvPacket() override
Pop an audio AVPacket from the reassembly buffer.
Definition: pes.cpp:461
static constexpr int MAX_HEADER_SIZE
Definition: pes.h:182
AVCodecID TruncateBufferUntilFirstValidData()
Truncate buffer until the first valid audio frame.
Definition: pes.cpp:499
AVCodecID DetectCodecFromSyncWord(const uint8_t *, int)
Detect audio codec from sync word pattern.
Definition: pes.cpp:595
SyncWordInfo FindSyncWord(const uint8_t *, int)
Find the first audio sync word in data.
Definition: pes.cpp:573
SyncWordInfo FindTwoConsecutiveFramesWithSameSyncWord()
Find two consecutive audio frames with the same sync word.
Definition: pes.cpp:526
bool m_ptsInvalid
flag indicating if PTS is invalid for current buffer, because it was truncated
Definition: pes.h:183
int GetFrameSizeForCodec(AVCodecID, const uint8_t *)
Get the frame size for a given codec and frame header.
Definition: pes.cpp:619
Video stream reassembly buffer.
Definition: pes.h:143
static constexpr uint32_t VIDEO_FRAME_START_CODE
Definition: pes.h:150
static constexpr int VIDEO_FRAME_START_CODE_LEN
Definition: pes.h:151
static constexpr uint8_t H264_STREAM_TYPE
Definition: pes.h:154
static constexpr uint8_t MPEG2_STREAM_TYPE
Definition: pes.h:153
bool HasLeadingZero(const uint8_t *, int)
Check if video data has a leading zero byte before the start code.
Definition: pes.cpp:444
static constexpr uint8_t HEVC_STREAM_TYPE
Definition: pes.h:155
bool ParseCodecHeader(const uint8_t *, int)
Parse video codec header to detect codec type.
Definition: pes.cpp:411
AVPacket * PopAvPacket() override
Definition: pes.h:146
Base class for stream reassembly buffers.
Definition: pes.h:121
AVCodecID m_codec
detected codec ID
Definition: pes.h:132
size_t GetSize()
Definition: pes.h:126
virtual AVPacket * PopAvPacket()=0
int64_t m_lastPoppedPts
PTS of the last popped AVPacket.
Definition: pes.h:134
virtual void Push(const uint8_t *data, int size, int64_t pts)
Definition: pes.h:123
cPtsTrackingBuffer m_buffer
fragmentation buffer
Definition: pes.h:133
AVCodecID GetCodec()
Definition: pes.h:128
cReassemblyBuffer(const char *identifier)
Definition: pes.h:130
void Reset()
Reset the reassembly buffer.
Definition: pes.cpp:629
bool IsEmpty()
Definition: pes.h:125
#define AV_NOPTS_VALUE
Definition: misc.h:35
Information about a detected audio sync word.
Definition: pes.h:161
AVCodecID codecId
Detected codec ID.
Definition: pes.h:162
int pos
Position of sync word in buffer.
Definition: pes.h:163