satdump/src-core/modules/xrit/module_xrit_decoder.cpp

373 lines
16 KiB
C++
Raw Normal View History

2021-07-16 01:06:49 +10:00
#include "module_xrit_decoder.h"
#include "logger.h"
#include "common/codings/rotation.h"
#include "common/codings/randomization.h"
2021-07-16 01:06:49 +10:00
#include "common/codings/differential/nrzm.h"
#include "imgui/imgui.h"
#include "common/codings/viterbi/viterbi27.h"
#include "common/codings/correlator.h"
#include "common/codings/reedsolomon/reedsolomon.h"
2021-08-08 00:10:32 +02:00
#define BUFFER_SIZE 8192 * 2
2021-07-16 01:06:49 +10:00
#define FRAME_SIZE 1024
#define ENCODED_FRAME_SIZE 1024 * 8 * 2
// Return filesize
size_t getFilesize(std::string filepath);
namespace xrit
{
XRITDecoderModule::XRITDecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters) : ProcessingModule(input_file, output_file_hint, parameters),
is_bpsk(parameters["is_bpsk"].get<bool>()),
diff_decode(parameters["diff_decode"].get<bool>()),
viterbi(ENCODED_FRAME_SIZE / 2, viterbi::CCSDS_R2_K7_POLYS),
d_viterbi_outsync_after(parameters.count("viterbi_outsync_after") > 0 ? parameters["viterbi_outsync_after"].get<int>() : 0),
d_viterbi_ber_threasold(parameters.count("viterbi_ber_thresold") > 0 ? parameters["viterbi_ber_thresold"].get<float>() : 0),
stream_viterbi(d_viterbi_ber_threasold, d_viterbi_outsync_after, BUFFER_SIZE)
2021-07-16 01:06:49 +10:00
{
buffer = new uint8_t[ENCODED_FRAME_SIZE];
2021-08-08 00:10:32 +02:00
viterbi_out = new uint8_t[BUFFER_SIZE * 2];
2021-07-16 01:06:49 +10:00
}
std::vector<ModuleDataType> XRITDecoderModule::getInputTypes()
{
return {DATA_FILE, DATA_STREAM};
}
std::vector<ModuleDataType> XRITDecoderModule::getOutputTypes()
{
return {DATA_FILE};
}
XRITDecoderModule::~XRITDecoderModule()
{
delete[] buffer;
2021-08-08 00:10:32 +02:00
delete[] viterbi_out;
2021-07-16 01:06:49 +10:00
}
void XRITDecoderModule::process()
{
if (input_data_type == DATA_FILE)
filesize = getFilesize(d_input_file);
else
filesize = 0;
if (input_data_type == DATA_FILE)
data_in = std::ifstream(d_input_file, std::ios::binary);
if (output_data_type == DATA_FILE)
{
data_out = std::ofstream(d_output_file_hint + ".cadu", std::ios::binary);
d_output_files.push_back(d_output_file_hint + ".cadu");
}
logger->info("Using input symbols " + d_input_file);
logger->info("Decoding to " + d_output_file_hint + ".cadu");
time_t lastTime = 0;
2021-08-08 00:10:32 +02:00
// Common objects
2021-07-16 01:06:49 +10:00
reedsolomon::ReedSolomon rs(reedsolomon::RS223);
diff::NRZMDiff diff;
2021-08-08 00:10:32 +02:00
if (is_bpsk) // BPSK Variant using a correlator
2021-07-16 01:06:49 +10:00
{
2021-08-08 00:10:32 +02:00
// Correlator and shifter
Correlator correlator(BPSK, diff_decode ? 0xfc4ef4fd0cc2df89 : 0xfca2b63db00d9794);
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// Other buffers
uint8_t frameBuffer[FRAME_SIZE];
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// Phase utils
phase_t phase;
bool swap;
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
while (input_data_type == DATA_FILE ? !data_in.eof() : input_active.load())
2021-07-16 01:06:49 +10:00
{
2021-08-08 00:10:32 +02:00
// Read a buffer
2021-07-16 01:06:49 +10:00
if (input_data_type == DATA_FILE)
2021-08-08 00:10:32 +02:00
data_in.read((char *)buffer, ENCODED_FRAME_SIZE);
2021-07-16 01:06:49 +10:00
else
2021-08-08 00:10:32 +02:00
input_fifo->read((uint8_t *)buffer, ENCODED_FRAME_SIZE);
int pos = correlator.correlate((int8_t *)buffer, phase, swap, cor, ENCODED_FRAME_SIZE);
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
locked = pos == 0; // Update locking state
if (pos != 0 && pos < ENCODED_FRAME_SIZE) // Safety
{
std::memmove(buffer, &buffer[pos], pos);
if (input_data_type == DATA_FILE)
data_in.read((char *)&buffer[ENCODED_FRAME_SIZE - pos], pos);
else
input_fifo->read((uint8_t *)&buffer[ENCODED_FRAME_SIZE - pos], pos);
}
// Correct phase ambiguity
rotate_soft((int8_t *)buffer, ENCODED_FRAME_SIZE, (phase_t)(phase + 2), swap);
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// Viterbi
viterbi.work((int8_t *)buffer, frameBuffer);
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// NRZ-M Decoding
if (diff_decode)
diff.decode(frameBuffer, FRAME_SIZE);
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// Derandomize that frame
derand_ccsds(&frameBuffer[4], FRAME_SIZE - 4);
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// RS Correction
rs.decode_interlaved(&frameBuffer[4], true, 4, errors);
// Write it out if it's not garbage
if (errors[0] >= 0 && errors[1] >= 0 && errors[2] >= 0 && errors[3] >= 0)
{
frameBuffer[0] = 0x1a;
frameBuffer[1] = 0xcf;
frameBuffer[2] = 0xfc;
frameBuffer[3] = 0x1d;
2021-08-03 21:48:02 +02:00
2021-08-08 00:10:32 +02:00
if (output_data_type == DATA_FILE)
data_out.write((char *)frameBuffer, FRAME_SIZE);
else
output_fifo->write(frameBuffer, FRAME_SIZE);
}
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
if (input_data_type == DATA_FILE)
progress = data_in.tellg();
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// Update module stats
module_stats["lock_state"] = locked;
module_stats["viterbi_ber"] = viterbi.ber() * 100;
module_stats["rs_avg"] = (errors[0] + errors[1] + errors[2] + errors[3]) / 4;
if (time(NULL) % 10 == 0 && lastTime != time(NULL))
{
lastTime = time(NULL);
std::string lock_state = locked ? "SYNCED" : "NOSYNC";
logger->info("Progress " + std::to_string(round(((float)progress / (float)filesize) * 1000.0f) / 10.0f) + "%, Viterbi BER : " + std::to_string(viterbi.ber() * 100) + "%, Lock : " + lock_state);
}
2021-07-16 01:06:49 +10:00
}
2021-08-08 00:10:32 +02:00
}
else
{
int vout;
2021-07-16 01:06:49 +10:00
2021-08-08 00:29:53 +02:00
while (input_data_type == DATA_FILE ? !data_in.eof() : input_active.load())
2021-08-08 00:10:32 +02:00
{
// Read a buffer
if (input_data_type == DATA_FILE)
data_in.read((char *)buffer, BUFFER_SIZE);
else
input_fifo->read((uint8_t *)buffer, BUFFER_SIZE);
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
// Perform Viterbi decoding
vout = stream_viterbi.work((uint8_t *)buffer, BUFFER_SIZE, viterbi_out);
2021-07-21 00:54:48 +02:00
2021-08-08 00:10:32 +02:00
// NRZ-M Decoding
if (diff_decode)
diff.decode(viterbi_out, vout);
if (vout > 0)
{
// Deframe (and derand)
std::vector<std::array<uint8_t, ccsds::ccsds_1_0_1024::CADU_SIZE>> frames = deframer.work(viterbi_out, vout);
// if we found frame, write them
if (frames.size() > 0)
{
for (std::array<uint8_t, ccsds::ccsds_1_0_1024::CADU_SIZE> cadu : frames)
{
// RS Correction
rs.decode_interlaved(&cadu[4], true, 4, errors);
// Write it out
if (errors[0] >= 0 && errors[1] >= 0 && errors[2] >= 0 && errors[3] >= 0)
2021-08-08 00:17:46 +02:00
{
if (output_data_type == DATA_FILE)
data_out.write((char *)&cadu, FRAME_SIZE);
else
2021-08-08 00:18:33 +02:00
output_fifo->write((uint8_t *)&cadu, FRAME_SIZE);
2021-08-08 00:17:46 +02:00
}
2021-08-08 00:10:32 +02:00
}
}
}
if (input_data_type == DATA_FILE)
progress = data_in.tellg();
// Update module stats
module_stats["viterbi_lock"] = stream_viterbi.getState();
module_stats["viterbi_ber"] = stream_viterbi.ber() * 100;
module_stats["deframer_lock"] = deframer.getState();
module_stats["rs_avg"] = (errors[0] + errors[1] + errors[2] + errors[3]) / 4;
if (time(NULL) % 10 == 0 && lastTime != time(NULL))
{
lastTime = time(NULL);
std::string viterbi_state = stream_viterbi.getState() == 0 ? "NOSYNC" : "SYNCED";
std::string deframer_state = deframer.getState() == 0 ? "NOSYNC" : (deframer.getState() == 2 || deframer.getState() == 6 ? "SYNCING" : "SYNCED");
2021-08-08 00:29:53 +02:00
logger->info("Progress " + std::to_string(round(((float)progress / (float)filesize) * 1000.0f) / 10.0f) + "%, Viterbi : " + viterbi_state + " BER : " + std::to_string(stream_viterbi.ber()) + ", Deframer : " + deframer_state);
2021-08-08 00:10:32 +02:00
}
2021-07-16 01:06:49 +10:00
}
}
if (output_data_type == DATA_FILE)
data_out.close();
if (input_data_type == DATA_FILE)
data_in.close();
}
void XRITDecoderModule::drawUI(bool window)
{
ImGui::Begin("xRIT Decoder", NULL, window ? NULL : NOWINDOW_FLAGS);
2021-08-08 00:10:32 +02:00
float ber = is_bpsk ? viterbi.ber() : stream_viterbi.ber();
2021-07-16 01:06:49 +10:00
ImGui::BeginGroup();
{
// Constellation
{
ImDrawList *draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(ImGui::GetCursorScreenPos(),
2021-07-17 01:47:41 +02:00
ImVec2(ImGui::GetCursorScreenPos().x + 200 * ui_scale, ImGui::GetCursorScreenPos().y + 200 * ui_scale),
ImColor::HSV(0, 0, 0));
2021-07-16 01:06:49 +10:00
2021-07-16 12:11:19 +10:00
if (is_bpsk)
2021-07-16 01:06:49 +10:00
{
2021-07-16 12:11:19 +10:00
for (int i = 0; i < 2048; i++)
{
draw_list->AddCircleFilled(ImVec2(ImGui::GetCursorScreenPos().x + (int)(100 * ui_scale + (((int8_t *)buffer)[i] / 127.0) * 130 * ui_scale) % int(200 * ui_scale),
2021-07-17 01:47:41 +02:00
ImGui::GetCursorScreenPos().y + (int)(100 * ui_scale + rng.gasdev() * 14 * ui_scale) % int(200 * ui_scale)),
2 * ui_scale,
ImColor::HSV(113.0 / 360.0, 1, 1, 1.0));
2021-07-16 12:11:19 +10:00
}
}
else
{
for (int i = 0; i < 2048; i++)
{
draw_list->AddCircleFilled(ImVec2(ImGui::GetCursorScreenPos().x + (int)(100 * ui_scale + (((int8_t *)buffer)[i * 2 + 0] / 127.0) * 100 * ui_scale) % int(200 * ui_scale),
2021-07-17 01:47:41 +02:00
ImGui::GetCursorScreenPos().y + (int)(100 * ui_scale + (((int8_t *)buffer)[i * 2 + 1] / 127.0) * 100 * ui_scale) % int(200 * ui_scale)),
2 * ui_scale,
ImColor::HSV(113.0 / 360.0, 1, 1, 1.0));
2021-07-16 12:11:19 +10:00
}
2021-07-16 01:06:49 +10:00
}
ImGui::Dummy(ImVec2(200 * ui_scale + 3, 200 * ui_scale + 3));
}
}
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
{
2021-08-08 00:10:32 +02:00
if (is_bpsk)
2021-07-16 01:06:49 +10:00
{
2021-08-08 00:10:32 +02:00
ImGui::Button("Correlator", {200 * ui_scale, 20 * ui_scale});
{
ImGui::Text("Corr : ");
ImGui::SameLine();
ImGui::TextColored(locked ? IMCOLOR_SYNCED : IMCOLOR_SYNCING, UITO_C_STR(cor));
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
std::memmove(&cor_history[0], &cor_history[1], (200 - 1) * sizeof(float));
cor_history[200 - 1] = cor;
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
ImGui::PlotLines("", cor_history, IM_ARRAYSIZE(cor_history), 0, "", 40.0f, 64.0f, ImVec2(200 * ui_scale, 50 * ui_scale));
}
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
ImGui::Spacing();
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
ImGui::Button("Viterbi", {200 * ui_scale, 20 * ui_scale});
{
ImGui::Text("BER : ");
ImGui::SameLine();
ImGui::TextColored(ber < 0.22 ? IMCOLOR_SYNCED : IMCOLOR_NOSYNC, UITO_C_STR(ber));
std::memmove(&ber_history[0], &ber_history[1], (200 - 1) * sizeof(float));
ber_history[200 - 1] = ber;
ImGui::PlotLines("", ber_history, IM_ARRAYSIZE(ber_history), 0, "", 0.0f, 1.0f, ImVec2(200 * ui_scale, 50 * ui_scale));
}
}
else
2021-07-16 01:06:49 +10:00
{
2021-08-08 00:10:32 +02:00
ImGui::Button("Viterbi", {200 * ui_scale, 20 * ui_scale});
{
ImGui::Text("State : ");
ImGui::SameLine();
if (stream_viterbi.getState() == 0)
ImGui::TextColored(IMCOLOR_NOSYNC, "NOSYNC");
else
ImGui::TextColored(IMCOLOR_SYNCED, "SYNCED");
ImGui::Text("BER : ");
ImGui::SameLine();
ImGui::TextColored(stream_viterbi.getState() == 0 ? IMCOLOR_NOSYNC : IMCOLOR_SYNCED, UITO_C_STR(ber));
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
std::memmove(&ber_history[0], &ber_history[1], (200 - 1) * sizeof(float));
ber_history[200 - 1] = ber;
2021-07-16 01:06:49 +10:00
2021-08-08 00:10:32 +02:00
ImGui::PlotLines("", ber_history, IM_ARRAYSIZE(ber_history), 0, "", 0.0f, 1.0f, ImVec2(200 * ui_scale, 50 * ui_scale));
}
ImGui::Spacing();
ImGui::Button("Deframer", {200 * ui_scale, 20 * ui_scale});
{
ImGui::Text("State : ");
ImGui::SameLine();
if (deframer.getState() == 0)
ImGui::TextColored(IMCOLOR_NOSYNC, "NOSYNC");
else if (deframer.getState() == 2 || deframer.getState() == 6)
ImGui::TextColored(IMCOLOR_SYNCING, "SYNCING");
else
ImGui::TextColored(IMCOLOR_SYNCED, "SYNCED");
}
2021-07-16 01:06:49 +10:00
}
ImGui::Spacing();
ImGui::Button("Reed-Solomon", {200 * ui_scale, 20 * ui_scale});
{
ImGui::Text("RS : ");
for (int i = 0; i < 4; i++)
{
ImGui::SameLine();
if (errors[i] == -1)
ImGui::TextColored(IMCOLOR_NOSYNC, "%i ", i);
else if (errors[i] > 0)
ImGui::TextColored(IMCOLOR_SYNCING, "%i ", i);
else
ImGui::TextColored(IMCOLOR_SYNCED, "%i ", i);
}
}
}
ImGui::EndGroup();
if (!streamingInput)
ImGui::ProgressBar((float)progress / (float)filesize, ImVec2(ImGui::GetWindowWidth() - 10, 20 * ui_scale));
ImGui::End();
}
std::string XRITDecoderModule::getID()
{
return "xrit_decoder";
}
std::vector<std::string> XRITDecoderModule::getParameters()
{
2021-08-08 00:10:32 +02:00
return {"is_bpsk", "diff_decode", "viterbi_outsync_after", "viterbi_ber_thresold"};
2021-07-16 01:06:49 +10:00
}
std::shared_ptr<ProcessingModule> XRITDecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
2021-07-16 01:06:49 +10:00
{
return std::make_shared<XRITDecoderModule>(input_file, output_file_hint, parameters);
}
}