vdr-plugin-softhddevice-drm-gles 1.5.9-20e15de
pes.h
Go to the documentation of this file.
1
18#ifndef __SOFTHDDEVICE_PES_H
19#define __SOFTHDDEVICE_PES_H
20
21#include <cstdint>
22#include <map>
23#include <vector>
24
25extern "C"
26{
27#include <libavcodec/avcodec.h>
28}
29
36class cPes
37{
38public:
39 cPes(const uint8_t *, int);
40 bool IsValid();
41 bool HasPts();
42 int64_t GetPts();
43 const uint8_t *GetPayload();
44 int GetPayloadSize();
45 int GetPacketLength();
46 uint8_t GetStreamId() { return m_data[3]; }
47
48protected:
49 virtual bool IsStreamIdValid() = 0;
50 void Init();
51 bool IsHeaderValid();
52
53 bool m_valid = false;
54 const uint8_t *m_data;
55 int m_size;
56
57 // According to H.222.0 03/2017 Table 2-21 ("PES packet") packet_start_code_prefix
58 // And also according to H.264/HEVC payload
59 static constexpr uint32_t PES_PACKET_START_CODE_PREFIX = 0x00'0001;
60 static constexpr uint32_t PES_PACKET_START_CODE_PREFIX_LEN = 3;
61};
62
68class cPesVideo : public cPes {
69public:
70 cPesVideo(const uint8_t *data, int size) : cPes(data, size) { cPes::Init(); }
71private:
72 bool IsStreamIdValid() override { return (GetStreamId() & 0xF0) == 0xE0; } // Video stream IDs are in the range 0xE0-0xEF
73};
74
81class cPesAudio : public cPes {
82public:
83 cPesAudio(const uint8_t *data, int size) : cPes(data, size) { cPes::Init(); }
84 bool IsAudioStreamId() { return (GetStreamId() & 0xF0) == 0xC0; } // Audio stream IDs are in the range 0xC0-0xCF
85private:
86 bool IsStreamIdValid() override { return IsAudioStreamId() || IsPrivateStreamId(); }
87 bool IsPrivateStreamId() { return GetStreamId() == 0xBD; }
88};
89
98public:
99 cPtsTrackingBuffer(const char *identifier) : m_identifier(identifier) {}
100 void Push(const uint8_t *, int, int64_t);
101 void Erase(size_t);
102 int64_t GetPts();
103 const uint8_t *Peek() { return &m_data[0]; }
104 void Reset() { m_data.clear(); m_pts.clear(); }
105 int GetSize() { return m_data.size(); }
106 const char *GetIdentifier() { return m_identifier; }
107private:
108 const char *m_identifier;
109 std::map<size_t, int64_t> m_pts;
110 std::vector<uint8_t> m_data;
111};
112
120public:
121 virtual void Push(const uint8_t *data, int size, int64_t pts) { m_buffer.Push(data, size, pts); }
122 virtual AVPacket *PopAvPacket() = 0;
123 bool IsEmpty() { return m_buffer.GetSize() == 0; }
124 size_t GetSize() { return m_buffer.GetSize(); }
125 void Reset();
126 AVCodecID GetCodec() { return m_codec; }
127protected:
128 cReassemblyBuffer(const char *identifier) : m_buffer(identifier) {}
129 AVPacket *PopAvPacket(int);
130 AVCodecID m_codec = AV_CODEC_ID_NONE;
133};
134
142public:
144 AVPacket *PopAvPacket() override { return cReassemblyBuffer::PopAvPacket(m_buffer.GetSize()); }
145 bool ParseCodecHeader(const uint8_t *, int);
146 bool HasLeadingZero(const uint8_t *, int);
147private:
148 static constexpr uint32_t VIDEO_FRAME_START_CODE = 0x00'0001;
149 static constexpr int VIDEO_FRAME_START_CODE_LEN = 3;
150
151 static constexpr uint8_t MPEG2_STREAM_TYPE = 0xB3;
152 static constexpr uint8_t H264_STREAM_TYPE = 0x09;
153 static constexpr uint8_t HEVC_STREAM_TYPE = 0x46;
154};
155
160 AVCodecID codecId;
161 int pos;
162};
163
171public:
173 AVPacket *PopAvPacket() override;
175 SyncWordInfo FindSyncWord(const uint8_t *, int );
176 AVCodecID DetectCodecFromSyncWord(const uint8_t *, int);
177 int GetFrameSizeForCodec(AVCodecID, const uint8_t *);
178private:
180 static constexpr int MAX_HEADER_SIZE = 6;
181 bool m_ptsInvalid = false;
182};
183
184#endif
Audio PES packet parser.
Definition pes.h:81
cPesAudio(const uint8_t *data, int size)
Definition pes.h:83
bool IsStreamIdValid() override
Definition pes.h:86
bool IsPrivateStreamId()
Definition pes.h:87
bool IsAudioStreamId()
Definition pes.h:84
Video PES packet parser.
Definition pes.h:68
bool IsStreamIdValid() override
Definition pes.h:72
cPesVideo(const uint8_t *data, int size)
Definition pes.h:70
PES packet parser class.
Definition pes.h:37
bool IsValid()
Check if the PES packet is valid.
Definition pes.cpp:244
static constexpr uint32_t PES_PACKET_START_CODE_PREFIX
Definition pes.h:59
bool m_valid
flag indicating if the PES packet is valid
Definition pes.h:53
uint8_t GetStreamId()
Definition pes.h:46
int GetPayloadSize()
Get the size of the PES payload.
Definition pes.cpp:312
int m_size
size of the PES packet
Definition pes.h:55
int GetPacketLength()
Get the total length of the PES packet.
Definition pes.cpp:334
const uint8_t * GetPayload()
Get a pointer to the PES payload data.
Definition pes.cpp:299
const uint8_t * m_data
pointer to the raw PES packet data
Definition pes.h:54
virtual bool IsStreamIdValid()=0
void Init()
Initialize and validate the PES packet.
Definition pes.cpp:219
bool IsHeaderValid()
Check if the PES header is valid.
Definition pes.cpp:257
bool HasPts()
Check if the PES packet contains a Presentation Time Stamp (PTS)
Definition pes.cpp:270
int64_t GetPts()
Get the Presentation Time Stamp (PTS) from the PES header.
Definition pes.cpp:283
static constexpr uint32_t PES_PACKET_START_CODE_PREFIX_LEN
Definition pes.h:60
Buffer that tracks PTS values at specific byte positions.
Definition pes.h:97
const char * GetIdentifier()
Definition pes.h:106
int GetSize()
Definition pes.h:105
void Push(const uint8_t *, int, int64_t)
Push data into the PTS tracking buffer.
Definition pes.cpp:637
std::vector< uint8_t > m_data
Byte buffer.
Definition pes.h:110
cPtsTrackingBuffer(const char *identifier)
Definition pes.h:99
std::map< size_t, int64_t > m_pts
Map of buffer positions to PTS values.
Definition pes.h:109
void Reset()
Definition pes.h:104
const uint8_t * Peek()
Definition pes.h:103
void Erase(size_t)
Erase data from the beginning of the buffer.
Definition pes.cpp:657
const char * m_identifier
Definition pes.h:108
int64_t GetPts()
Get the PTS value for the current buffer position.
Definition pes.cpp:694
Audio stream reassembly buffer.
Definition pes.h:170
AVPacket * PopAvPacket() override
Pop an audio AVPacket from the reassembly buffer.
Definition pes.cpp:448
static constexpr int MAX_HEADER_SIZE
Definition pes.h:180
AVCodecID TruncateBufferUntilFirstValidData()
Truncate buffer until the first valid audio frame.
Definition pes.cpp:486
AVCodecID DetectCodecFromSyncWord(const uint8_t *, int)
Detect audio codec from sync word pattern.
Definition pes.cpp:582
SyncWordInfo FindSyncWord(const uint8_t *, int)
Find the first audio sync word in data.
Definition pes.cpp:560
SyncWordInfo FindTwoConsecutiveFramesWithSameSyncWord()
Find two consecutive audio frames with the same sync word.
Definition pes.cpp:513
bool m_ptsInvalid
flag indicating if PTS is invalid for current buffer, because it was truncated
Definition pes.h:181
int GetFrameSizeForCodec(AVCodecID, const uint8_t *)
Get the frame size for a given codec and frame header.
Definition pes.cpp:606
Video stream reassembly buffer.
Definition pes.h:141
static constexpr uint32_t VIDEO_FRAME_START_CODE
Definition pes.h:148
static constexpr int VIDEO_FRAME_START_CODE_LEN
Definition pes.h:149
static constexpr uint8_t H264_STREAM_TYPE
Definition pes.h:152
static constexpr uint8_t MPEG2_STREAM_TYPE
Definition pes.h:151
bool HasLeadingZero(const uint8_t *, int)
Check if video data has a leading zero byte before the start code.
Definition pes.cpp:431
static constexpr uint8_t HEVC_STREAM_TYPE
Definition pes.h:153
bool ParseCodecHeader(const uint8_t *, int)
Parse video codec header to detect codec type.
Definition pes.cpp:398
AVPacket * PopAvPacket() override
Definition pes.h:144
Base class for stream reassembly buffers.
Definition pes.h:119
AVCodecID m_codec
detected codec ID
Definition pes.h:130
size_t GetSize()
Definition pes.h:124
virtual AVPacket * PopAvPacket()=0
int64_t m_lastPoppedPts
PTS of the last popped AVPacket.
Definition pes.h:132
virtual void Push(const uint8_t *data, int size, int64_t pts)
Definition pes.h:121
cPtsTrackingBuffer m_buffer
fragmentation buffer
Definition pes.h:131
AVCodecID GetCodec()
Definition pes.h:126
cReassemblyBuffer(const char *identifier)
Definition pes.h:128
void Reset()
Reset the reassembly buffer.
Definition pes.cpp:616
bool IsEmpty()
Definition pes.h:123
#define AV_NOPTS_VALUE
Definition misc.h:69
Information about a detected audio sync word.
Definition pes.h:159
AVCodecID codecId
Detected codec ID.
Definition pes.h:160
int pos
Position of sync word in buffer.
Definition pes.h:161