vdr-plugin-softhddevice-drm-gles 1.6.2
pes.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-or-later
2
10#include <functional>
11#include <map>
12#include <stdexcept>
13
14#include "pes.h"
15#include "logger.h"
16#include "misc.h"
17
18#include <vdr/remux.h>
19
20extern "C" {
21#include <libavutil/avutil.h>
22}
23
35{
37 std::function<bool(const uint8_t*)> MatchSyncWord;
38 std::function<int(const uint8_t*)> GetFrameSize;
39};
40
47static const std::map<AVCodecID, CodecInfo> AudioCodecMap = {
49 .minSize = 3,
50 .MatchSyncWord = [](const uint8_t* data) -> bool {
51 constexpr uint32_t MPEG_AUDIO_SYNC_WORD = 0xFF'E000;
52 constexpr uint32_t MPEG_AUDIO_VERSION_FORBIDDEN_VALUE = 0x00'0800;
55
56 uint32_t syncWord = ReadBytes(data, 3);
57 return (syncWord & 0b1111'1111'1110'0000'0000'0000) == MPEG_AUDIO_SYNC_WORD &&
58 (syncWord & 0b0000'0000'0001'1000'0000'0000) != MPEG_AUDIO_VERSION_FORBIDDEN_VALUE &&
59 (syncWord & 0b0000'0000'0000'0110'0000'0000) != MPEG_AUDIO_LAYER_DESCRIPTION_FORBIDDEN_VALUE &&
60 (syncWord & 0b0000'0000'0000'0000'1111'0000) != MPEG_AUDIO_BITRATE_INDEX_FORBIDDEN_VALUE;
61 },
62 .GetFrameSize = [](const uint8_t* data) -> int {
63 constexpr uint16_t BitRateTable[2][4][16] = {
64 // MPEG Version 1
65 {{},
66 {0, 32, 64, 96, 128, 160, 192, 224, 256, 288, 320, 352, 384, 416, 448, 0},
67 {0, 32, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 384, 0},
68 {0, 32, 40, 48, 56, 64, 80, 96, 112, 128, 160, 192, 224, 256, 320, 0}},
69 // MPEG Version 2 & 2.5
70 {{},
71 {0, 32, 48, 56, 64, 80, 96, 112, 128, 144, 160, 176, 192, 224, 256, 0},
72 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0},
73 {0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96, 112, 128, 144, 160, 0}
74 }
75 };
76 constexpr uint16_t SampleRateTable[4] = {44100, 48000, 32000, 0};
77
78 int mpeg2 = !(data[1] & 0x08) && (data[1] & 0x10);
79 int mpeg25 = !(data[1] & 0x08) && !(data[1] & 0x10);
80 int layer = 4 - ((data[1] >> 1) & 0x03);
81 int bitRateIndex = (data[2] >> 4) & 0x0F;
82 int sampleRateIndex = (data[2] >> 2) & 0x03;
83 int padding = (data[2] >> 1) & 0x01;
84
86 if (!sampleRate)
87 throw std::invalid_argument("MPEG: invalid sample rate");
88
89 sampleRate >>= mpeg2;
91
93 if (!bitRate)
94 throw std::invalid_argument("MPEG: invalid bit rate");
95
96 bitRate *= 1000;
97 int frameSize;
98 switch (layer) {
99 case 1:
100 frameSize = (12 * bitRate) / sampleRate;
101 frameSize = (frameSize + padding) * 4;
102 break;
103 case 2:
104 case 3:
105 default:
106 frameSize = (144 * bitRate) / sampleRate;
108 break;
109 }
110 return frameSize;
111 }
112 }},
114 .minSize = 3,
115 .MatchSyncWord = [](const uint8_t* data) -> bool {
116 constexpr uint32_t LAOS_SYNC_WORD_MASK = 0xFFE000;
117 constexpr uint32_t LAOS_SYNC_WORD = 0x2B7 << (24-11);
118
119 uint32_t syncWord = ReadBytes(data, 3);
121 },
122 .GetFrameSize = [](const uint8_t* data) -> int {
123 return ((data[1] & 0x1F) << 8) + data[2] + 3;
124 }
125 }},
127 .minSize = 6,
128 .MatchSyncWord = [](const uint8_t* data) -> bool {
129 constexpr uint32_t AC3_SYNC_WORD_MASK = 0xFFFF00;
130 constexpr uint32_t AC3_SYNC_WORD = 0x0B77 << (24-16);
131
132 uint32_t syncWord = ReadBytes(data, 3);
134 data[5] <= (10 << 3);
135 },
136 .GetFrameSize = [](const uint8_t* data) -> int {
137 constexpr uint16_t Ac3FrameSizeTable[38][3] = {
138 {64, 69, 96}, {64, 70, 96}, {80, 87, 120}, {80, 88, 120},
139 {96, 104, 144}, {96, 105, 144}, {112, 121, 168}, {112, 122, 168},
140 {128, 139, 192}, {128, 140, 192}, {160, 174, 240}, {160, 175, 240},
141 {192, 208, 288}, {192, 209, 288}, {224, 243, 336}, {224, 244, 336},
142 {256, 278, 384}, {256, 279, 384}, {320, 348, 480}, {320, 349, 480},
143 {384, 417, 576}, {384, 418, 576}, {448, 487, 672}, {448, 488, 672},
144 {512, 557, 768}, {512, 558, 768}, {640, 696, 960}, {640, 697, 960},
145 {768, 835, 1152}, {768, 836, 1152}, {896, 975, 1344}, {896, 976, 1344},
146 {1024, 1114, 1536}, {1024, 1115, 1536}, {1152, 1253, 1728},
147 {1152, 1254, 1728}, {1280, 1393, 1920}, {1280, 1394, 1920},
148 };
149
150 int fscod = data[4] >> 6;
151 if (fscod == 0x03)
152 throw std::invalid_argument("AC3: invalid sample rate");
153
154 int frmsizcod = data[4] & 0x3F;
155 if (frmsizcod > 37)
156 throw std::invalid_argument("AC3: invalid frame size");
157
158 return Ac3FrameSizeTable[frmsizcod][fscod] * 2;
159 }
160 }},
162 .minSize = 6,
163 .MatchSyncWord = [](const uint8_t* data) -> bool {
164 constexpr uint32_t AC3_SYNC_WORD = 0x0B77 << (24-16);
165
166 uint32_t syncWord = ReadBytes(data, 3);
167 return (syncWord & 0xFFFF00) == AC3_SYNC_WORD && data[5] > (10 << 3);
168 },
169 .GetFrameSize = [](const uint8_t* data) -> int {
170 if ((data[4] & 0xF0) == 0xF0)
171 throw std::invalid_argument("AC3: invalid fscod fscod2");
172
173 return (((data[2] & 0x07) << 8) + data[3] + 1) * 2;
174 }
175 }},
177 .minSize = 3,
178 .MatchSyncWord = [](const uint8_t* data) -> bool {
179 constexpr uint32_t ADTS_SYNC_WORD = 0xFFF000;
180 constexpr uint32_t ADTS_LAYER = 0x000000;
182
183 uint32_t syncWord = ReadBytes(data, 3);
184 return (syncWord & 0b1111'1111'1111'0000'0000'0000) == ADTS_SYNC_WORD &&
185 (syncWord & 0b0000'0000'0000'0110'0000'0000) == ADTS_LAYER &&
186 (syncWord & 0b0000'0000'0000'0011'1100'0000) != ADTS_SAMPLING_FREQUENCY_FORBIDDEN_VALUE;
187 },
188 .GetFrameSize = [](const uint8_t* data) -> int {
189 return ((data[3] & 0x03) << 11) | ((data[4] & 0xFF) << 3) | ((data[5] & 0xE0) >> 5);
190 }
191 }}
192};
193
203cPes::cPes(const uint8_t *data, int size)
204 : m_data(data), m_size(size)
205{
206}
207
219void cPes::Init(void)
220{
221 if (IsHeaderValid() && IsStreamIdValid()) {
222 if (m_size <= 8 || PesPayloadOffset(m_data) > m_size) // header length field is at position 8 when the PES extension is present
223 LOGWARNING("pes: %s: packet too short: %d %02X", __FUNCTION__, m_size, GetStreamId());
224 else
225 m_valid = true;
226 } else
227 LOGDEBUG("pes: %s: invalid packet: %d %02X", __FUNCTION__, m_size, GetStreamId());
228}
229
246{
247 return m_valid;
248}
249
263
272bool cPes::HasPts(void)
273{
274 return PesHasPts(m_data);
275}
276
286{
287 if (!HasPts())
288 return AV_NOPTS_VALUE;
289
290 return PesGetPts(m_data);
291}
292
302{
304}
305
315{
317}
318
337{
338 if (!PesHasLength(m_data))
339 return m_size; // Length field is 0, meaning unbounded/unspecified. Return raw data size.
340
341 return PesLength(m_data);
342}
343
344/********************************************************************************
345 * Reassembly buffer
346 *******************************************************************************/
347
358{
359 if (size == 0)
360 return nullptr;
361
363
364 if (!avpkt)
365 LOGFATAL("pes: %s: out of memory while allocating AVPacket", __FUNCTION__);
366
367 if (av_new_packet(avpkt, size)) // allocates size + AV_INPUT_BUFFER_PADDING_SIZE
368 LOGFATAL("pes: %s: out of memory while allocating AVPacket payload", __FUNCTION__);
369
370 memcpy(avpkt->data, m_buffer.Peek(), size);
371 memset(&avpkt->data[size], 0, AV_INPUT_BUFFER_PADDING_SIZE);
372
373 // Only audio:
374 // If a PES packet contains multiple frames, only the AVPacket with the first frame of that PES packet shall have a PTS value, when sending it to the decoder.
375 // The following AVPackets created from this PES packet shall have no PTS value.
376 // When retrieving PTS values from the same PES packet, they will be identical.
378 avpkt->pts = m_buffer.GetPts();
379 else
380 avpkt->pts = AV_NOPTS_VALUE;
381
383
384 m_buffer.Erase(size);
385
386 return avpkt;
387}
388
389/********************************************************************************
390 * Video specific implementation
391 *******************************************************************************/
392
405{
408
409 // Looking for the MPEG2 start code and stream type in the PES payload
412 else if (HasLeadingZero(fragment, size)) // Looking for a leading zero byte in front of the start code. Can be present in H.264/HEVC streams.
413 codecPayload++;
415 return false; // No start code: PES packet carries fragmented payload, or unknown codec.
416
417 if (size > &codecPayload[7] - fragment) {
418 if ( codecPayload[0] == H264_STREAM_TYPE && (codecPayload[1] == 0x10 || codecPayload[1] == 0xF0 || codecPayload[7] == 0x64))
420 else if (codecPayload[0] == HEVC_STREAM_TYPE && (codecPayload[1] == 0x10 || codecPayload[1] == 0x50 || codecPayload[7] == 0x40))
422 }
423
424 return m_codec != AV_CODEC_ID_NONE;
425}
426
439{
440 return size > VIDEO_FRAME_START_CODE_LEN + 1 && data[0] == 0 && ReadBytes(&data[1], VIDEO_FRAME_START_CODE_LEN) == VIDEO_FRAME_START_CODE;
441}
442
443/********************************************************************************
444 * Audio specific implementation
445 *******************************************************************************/
446
456{
458
460 return nullptr; // No sync word found in the buffer. Wait for more data.
462 LOGERROR("pes: %s: audio codec changed unexpectedly from %d to %d", __FUNCTION__, avcodec_get_name(m_codec), avcodec_get_name(detectedCodec));
463
465
466 try {
468
469 if (m_ptsInvalid) { // the PTS is invalid for this packet because the buffer was truncated before
470 packet->pts = AV_NOPTS_VALUE;
471
472 m_ptsInvalid = false;
473 }
474
475 return packet;
476 } catch (const std::invalid_argument &e) {
477 LOGWARNING("pes: %s: garbage in audio stream received: %s", __FUNCTION__, e.what());
478 // the garbage will be removed in the next call to TruncateBufferUntilFirstValidData()
479 }
480
481 return nullptr;
482}
483
494{
496
498
500
502 LOGDEBUG("pes: %s: truncated %d of %d bytes while searching for sync word", __FUNCTION__, sizeBeforeTruncation - m_buffer.GetSize(), sizeBeforeTruncation);
503 m_ptsInvalid = true;
504 }
505
506 return firstFrame.codecId;
507}
508
522{
523 while (true) {
525
526 if (firstFrame.codecId == AV_CODEC_ID_NONE) // No sync word found in the entire buffer. Keep only the last few bytes that could contain a partial sync word.
527 return SyncWordInfo{AV_CODEC_ID_NONE, std::max(0, (int)m_buffer.GetSize() - MAX_HEADER_SIZE)};
528
529 try {
530 // determine the length of the first found potential frame by reading the frame's header
531 int sizeOfFirstFrame = AudioCodecMap.at(firstFrame.codecId).GetFrameSize(&m_buffer.Peek()[firstFrame.pos]);
533
534 // check if another sync word follows immediately after the first frame to validate the header of the first frame is a real header and no random data
536 // Could not find the second sync word, because there might not be enough data in the buffer to contain a complete second sync word. Wait for more data.
537 // In case we have a false positive and the header's frame size field is invalid, we buffer the following amount of data in worst-case:
538 // - MP2: 6913 bytes (Layer 2/3: 384kbps @ 8kHz + padding)
539 // - AAC LATM: 8194 bytes (13-bit length field max: 0x1FFF + 3)
540 // - AC3: 2788 bytes (frmsizcod=37, fscod=1: 1394 * 2)
541 // - EAC3: 4096 bytes (11-bit field max: 2048 * 2)
542 // - AAC ADTS: 8191 bytes (13-bit length field max: 0x1FFF)
545 // two consecutive frames with the same sync word found, and the first frame's header length field is valid
546 return SyncWordInfo{firstFrame.codecId, firstFrame.pos};
547 } catch (const std::invalid_argument &e) {
548 // Failed to read the frame size from the first frame's header. The found sync word is a false positive.
549 }
550
551 // If we found one sync word, but did not find a second one at the expected position, the first one was a false positive in the middle of random data.
552 // In this case, continue the search one position after the start of the first found sync word.
553 m_buffer.Erase(firstFrame.pos + 1);
554 }
555}
556
569{
570 for (int i = 0; i < size; i++) {
574 }
575
576 return SyncWordInfo{AV_CODEC_ID_NONE, -1};
577}
578
591{
592 for (const auto& [codecId, codecInfo] : AudioCodecMap) {
593 if (size >= codecInfo.minSize && codecInfo.MatchSyncWord(syncWord)) {
594 return codecId;
595 }
596 }
597
598 return AV_CODEC_ID_NONE;
599}
600
615{
616 return AudioCodecMap.at(codec).GetFrameSize(data);
617}
618
630
631/********************************************************************************
632 * PTS tracking buffer
633 *******************************************************************************/
634
645void cPtsTrackingBuffer::Push(const uint8_t *data, int size, int64_t pts)
646{
647 if (pts != AV_NOPTS_VALUE) // PES packets not starting with a new frame (fragmented data) have no PTS
648 m_pts[m_data.size()] = pts;
649
650 m_data.insert(m_data.end(), data, data + size);
651}
652
667{
668 if (m_data.empty() || amount == 0)
669 return;
670
671 // Only PES packets have PTS values, but not the (fragmented) frames inside.
672 // The reassembled frame's PTS value will become the PTS value of the PES packet where the frame starts.
673 // Therefore, always keep the PTS value for position 0 in the buffer, which is the PTS value of the PES packet where the frame starts.
674 // This is normally the largest PTS value to be removed, or, if future position 0 already has a PTS value, that value will be used.
676 auto it = m_pts.upper_bound(amount);
677 if (it == m_pts.begin())
678 LOGFATAL("pes: %s: %s: no PTS value found for position 0 after erasing %zu bytes", __FUNCTION__, m_identifier, amount);
679 else {
680 --it; // Move to the last entry before 'amount'
681 smallestPts = it->second;
682 }
683
684 std::map<size_t, int64_t> adjusted_pts;
685 for (const auto& [pos, pts] : m_pts) {
686 if (pos >= amount) // erase all PTS entries for data that will be removed
687 adjusted_pts[pos - amount] = pts; // adjust remaining PTS entries to the new data indices
688 }
689 m_pts = std::move(adjusted_pts);
690
691 m_pts[0] = smallestPts;
692
693 m_data.erase(m_data.begin(), m_data.begin() + amount);
694}
695
705{
706 if (m_pts.empty())
707 return AV_NOPTS_VALUE;
708
709 return m_pts.begin()->second;
710}
711
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
int GetSize(void)
Definition pes.h:101
std::vector< uint8_t > m_data
Byte buffer.
Definition pes.h:106
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
static constexpr int MAX_HEADER_SIZE
Definition pes.h:176
bool m_ptsInvalid
flag indicating if PTS is invalid for current buffer, because it was truncated
Definition pes.h:177
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
AVCodecID m_codec
detected codec ID
Definition pes.h:126
int64_t m_lastPoppedPts
PTS of the last popped AVPacket.
Definition pes.h:128
virtual AVPacket * PopAvPacket(void)=0
cPtsTrackingBuffer m_buffer
fragmentation buffer
Definition pes.h:127
int64_t GetPts(void)
Get the PTS value for the current buffer position.
Definition pes.cpp:704
#define LOGDEBUG
log to LOG_DEBUG
Definition logger.h:40
#define LOGERROR
log to LOG_ERR
Definition logger.h:34
#define AV_NOPTS_VALUE
Definition misc.h:74
AVCodecID TruncateBufferUntilFirstValidData(void)
Truncate buffer until the first valid audio frame.
Definition pes.cpp:493
cPes(const uint8_t *, int)
Create a PES packet parser.
Definition pes.cpp:203
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
#define LOGWARNING
log to LOG_WARN
Definition logger.h:36
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
#define LOGFATAL
log to LOG_ERR and abort
Definition logger.h:32
static uint32_t ReadBytes(const uint8_t *data, int count)
Return count amount of bytes from data
Definition misc.h:149
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
static const std::map< AVCodecID, CodecInfo > AudioCodecMap
Map of Audio Codec Information.
Definition pes.cpp:47
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
Logger Header File.
Misc Functions.
PES Packet Parser Header File.
Codec Information Structure.
Definition pes.cpp:35
std::function< int(const uint8_t *)> GetFrameSize
Definition pes.cpp:38
int minSize
Definition pes.cpp:36
std::function< bool(const uint8_t *)> MatchSyncWord
Definition pes.cpp:37
Information about a detected audio sync word.
Definition pes.h:155