satdump/src-core/common/mpeg_ts/ts_demux.cpp

52 lines
1.4 KiB
C++
Raw Permalink Normal View History

2022-07-11 02:17:20 +02:00
#include "ts_demux.h"
namespace mpeg_ts
{
std::vector<std::vector<uint8_t>> TSDemux::demux(uint8_t *ts, int pid)
{
std::vector<std::vector<uint8_t>> frames_out;
ts_header.parse(ts);
if (pid != -1 && ts_header.pid != pid)
return frames_out;
2022-07-19 16:14:15 +02:00
int offset = 0;
if (ts_header.afc == 0b10) // Adapation field only, no payload
return frames_out;
else if (ts_header.afc == 0b11) // Adapation field followed
offset = ts[4] + 1;
2022-07-11 02:17:20 +02:00
if (ts_header.pusi)
{
2022-07-19 16:14:15 +02:00
if (offset > 188 - 4)
return frames_out;
uint8_t ptr = ts[4 + offset];
2022-07-11 02:17:20 +02:00
2022-07-19 16:14:15 +02:00
if (offset + ptr > 188 - 5)
2022-07-12 14:40:42 +02:00
return frames_out;
2022-07-11 02:17:20 +02:00
if (in_progress)
{
2022-07-19 16:14:15 +02:00
current_payload.insert(current_payload.end(), &ts[5 + offset], &ts[5 + offset + ptr]);
2022-07-11 02:17:20 +02:00
frames_out.push_back(current_payload);
in_progress = false;
}
current_payload.clear();
2022-07-19 16:14:15 +02:00
current_payload.insert(current_payload.end(), &ts[5 + offset + ptr], &ts[188]);
2022-07-11 02:17:20 +02:00
in_progress = true;
}
else if (in_progress)
2022-07-19 16:14:15 +02:00
{
if (offset > 188 - 4)
return frames_out;
current_payload.insert(current_payload.end(), &ts[4 + offset], &ts[188]);
}
2022-07-11 02:17:20 +02:00
return frames_out;
}
};