vdr-plugin-softhddevice-drm-gles 1.6.2
pes.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2
10#ifndef __SOFTHDDEVICE_PES_H
11#define __SOFTHDDEVICE_PES_H
12
13#include <cstdint>
14#include <map>
15#include <vector>
16
17extern "C"
18{
19#include <libavcodec/avcodec.h>
20}
21
33class cPes {
34public:
35 cPes(const uint8_t *, int);
36 bool IsValid(void);
37 bool HasPts(void);
38 int64_t GetPts(void);
39 const uint8_t *GetPayload(void);
40 int GetPayloadSize(void);
41 int GetPacketLength(void);
42 uint8_t GetStreamId(void) { return m_data[3]; }
43
44protected:
45 virtual bool IsStreamIdValid(void) = 0;
46 void Init(void);
47 bool IsHeaderValid(void);
48
49 bool m_valid = false;
50 const uint8_t *m_data;
51 int m_size;
52
53 // According to H.222.0 03/2017 Table 2-21 ("PES packet") packet_start_code_prefix
54 // And also according to H.264/HEVC payload
55 static constexpr uint32_t PES_PACKET_START_CODE_PREFIX = 0x00'0001;
57};
58
64class cPesVideo : public cPes {
65public:
66 cPesVideo(const uint8_t *data, int size) : cPes(data, size) { cPes::Init(); }
67private:
68 bool IsStreamIdValid(void) override { return (GetStreamId() & 0xF0) == 0xE0; } // Video stream IDs are in the range 0xE0-0xEF
69};
70
77class cPesAudio : public cPes {
78public:
79 cPesAudio(const uint8_t *data, int size) : cPes(data, size) { cPes::Init(); }
80 bool IsAudioStreamId(void) { return (GetStreamId() & 0xF0) == 0xC0; } // Audio stream IDs are in the range 0xC0-0xCF
81private:
82 bool IsStreamIdValid(void) override { return IsAudioStreamId() || IsPrivateStreamId(); }
83 bool IsPrivateStreamId(void) { return GetStreamId() == 0xBD; }
84};
85
94public:
96 void Push(const uint8_t *, int, int64_t);
97 void Erase(size_t);
98 int64_t GetPts(void);
99 const uint8_t *Peek(void) { return &m_data[0]; }
100 void Reset(void) { m_data.clear(); m_pts.clear(); }
101 int GetSize(void) { return m_data.size(); }
102 const char *GetIdentifier(void) { return m_identifier; }
103private:
104 const char *m_identifier;
105 std::map<size_t, int64_t> m_pts;
106 std::vector<uint8_t> m_data;
107};
108
116public:
117 virtual void Push(const uint8_t *data, int size, int64_t pts) { m_buffer.Push(data, size, pts); }
118 virtual AVPacket *PopAvPacket(void) = 0;
119 bool IsEmpty(void) { return m_buffer.GetSize() == 0; }
120 size_t GetSize(void) { return m_buffer.GetSize(); }
121 void Reset(void);
122 AVCodecID GetCodec(void) { return m_codec; }
123protected:
125 AVPacket *PopAvPacket(int);
129};
130
138public:
141 bool ParseCodecHeader(const uint8_t *, int);
142 bool HasLeadingZero(const uint8_t *, int);
143private:
144 static constexpr uint32_t VIDEO_FRAME_START_CODE = 0x00'0001;
145 static constexpr int VIDEO_FRAME_START_CODE_LEN = 3;
146
147 static constexpr uint8_t MPEG2_STREAM_TYPE = 0xB3;
148 static constexpr uint8_t H264_STREAM_TYPE = 0x09;
149 static constexpr uint8_t HEVC_STREAM_TYPE = 0x46;
150};
151
159
179
182#endif
Audio PES Packet Parser.
Definition pes.h:77
cPesAudio(const uint8_t *data, int size)
Definition pes.h:79
bool IsPrivateStreamId(void)
Definition pes.h:83
bool IsAudioStreamId(void)
Definition pes.h:80
bool IsStreamIdValid(void) override
Definition pes.h:82
Video PES Packet Parser.
Definition pes.h:64
bool IsStreamIdValid(void) override
Definition pes.h:68
cPesVideo(const uint8_t *data, int size)
Definition pes.h:66
PES Packet Parser.
Definition pes.h:33
static constexpr uint32_t PES_PACKET_START_CODE_PREFIX
Definition pes.h:55
bool m_valid
flag indicating if the PES packet is valid
Definition pes.h:49
virtual bool IsStreamIdValid(void)=0
int m_size
size of the PES packet
Definition pes.h:51
const uint8_t * m_data
pointer to the raw PES packet data
Definition pes.h:50
uint8_t GetStreamId(void)
Definition pes.h:42
static constexpr uint32_t PES_PACKET_START_CODE_PREFIX_LEN
Definition pes.h:56
PTS Tracking Buffer.
Definition pes.h:93
const char * GetIdentifier(void)
Definition pes.h:102
int GetSize(void)
Definition pes.h:101
std::vector< uint8_t > m_data
Byte buffer.
Definition pes.h:106
cPtsTrackingBuffer(const char *identifier)
Definition pes.h:95
std::map< size_t, int64_t > m_pts
Map of buffer positions to PTS values.
Definition pes.h:105
const uint8_t * Peek(void)
Definition pes.h:99
void Reset(void)
Definition pes.h:100
const char * m_identifier
Definition pes.h:104
Audio Stream Reassembly Buffer.
Definition pes.h:166
static constexpr int MAX_HEADER_SIZE
Definition pes.h:176
cReassemblyBufferAudio(void)
Definition pes.h:168
bool m_ptsInvalid
flag indicating if PTS is invalid for current buffer, because it was truncated
Definition pes.h:177
Video Stream Reassembly Buffer.
Definition pes.h:137
AVPacket * PopAvPacket(void) override
Definition pes.h:140
static constexpr uint32_t VIDEO_FRAME_START_CODE
Definition pes.h:144
static constexpr int VIDEO_FRAME_START_CODE_LEN
Definition pes.h:145
static constexpr uint8_t H264_STREAM_TYPE
Definition pes.h:148
static constexpr uint8_t MPEG2_STREAM_TYPE
Definition pes.h:147
static constexpr uint8_t HEVC_STREAM_TYPE
Definition pes.h:149
cReassemblyBufferVideo(void)
Definition pes.h:139
Base Class for Stream Reassembly Buffers.
Definition pes.h:115
AVCodecID m_codec
detected codec ID
Definition pes.h:126
AVCodecID GetCodec(void)
Definition pes.h:122
int64_t m_lastPoppedPts
PTS of the last popped AVPacket.
Definition pes.h:128
virtual void Push(const uint8_t *data, int size, int64_t pts)
Definition pes.h:117
virtual AVPacket * PopAvPacket(void)=0
cPtsTrackingBuffer m_buffer
fragmentation buffer
Definition pes.h:127
bool IsEmpty(void)
Definition pes.h:119
cReassemblyBuffer(const char *identifier)
Definition pes.h:124
size_t GetSize(void)
Definition pes.h:120
int64_t GetPts(void)
Get the PTS value for the current buffer position.
Definition pes.cpp:704
#define AV_NOPTS_VALUE
Definition misc.h:74
AVCodecID TruncateBufferUntilFirstValidData(void)
Truncate buffer until the first valid audio frame.
Definition pes.cpp:493
void Push(const uint8_t *, int, int64_t)
Push data into the PTS tracking buffer.
Definition pes.cpp:645
void Reset(void)
Reset the reassembly buffer.
Definition pes.cpp:624
const uint8_t * GetPayload(void)
Get a pointer to the PES payload data.
Definition pes.cpp:301
AVCodecID DetectCodecFromSyncWord(const uint8_t *, int)
Detect audio codec from sync word pattern.
Definition pes.cpp:590
int GetPayloadSize(void)
Get the size of the PES payload.
Definition pes.cpp:314
bool HasLeadingZero(const uint8_t *, int)
Check if video data has a leading zero byte before the start code.
Definition pes.cpp:438
SyncWordInfo FindSyncWord(const uint8_t *, int)
Find the first audio sync word in data.
Definition pes.cpp:568
bool HasPts(void)
Check if the PES packet contains a Presentation Time Stamp (PTS)
Definition pes.cpp:272
bool IsHeaderValid(void)
Check if the PES header is valid.
Definition pes.cpp:259
void Erase(size_t)
Erase data from the beginning of the buffer.
Definition pes.cpp:666
int GetPacketLength(void)
Get the total length of the PES packet.
Definition pes.cpp:336
bool IsValid(void)
Check if the PES packet is valid.
Definition pes.cpp:245
SyncWordInfo FindTwoConsecutiveFramesWithSameSyncWord()
Find two consecutive audio frames with the same sync word.
Definition pes.cpp:521
AVPacket * PopAvPacket(void) override
Pop an audio AVPacket from the reassembly buffer.
Definition pes.cpp:455
bool ParseCodecHeader(const uint8_t *, int)
Parse video codec header to detect codec type.
Definition pes.cpp:404
int64_t GetPts(void)
Get the Presentation Time Stamp (PTS) from the PES header.
Definition pes.cpp:285
int GetFrameSizeForCodec(AVCodecID, const uint8_t *)
Get the frame size for a given codec and frame header.
Definition pes.cpp:614
void Init(void)
Initialize and validate the PES packet.
Definition pes.cpp:219
Information about a detected audio sync word.
Definition pes.h:155
AVCodecID codecId
Detected codec ID.
Definition pes.h:156
int pos
Position of sync word in buffer.
Definition pes.h:157