satdump/src-core/modules/saral/module_saral_decoder.cpp

235 lines
8.3 KiB
C++
Raw Normal View History

2021-04-02 13:25:45 +02:00
#include "module_saral_decoder.h"
#include "logger.h"
#include "common/sathelper/correlator.h"
#include "common/sathelper/packetfixer.h"
#include "common/sathelper/derandomizer.h"
2021-05-21 23:07:07 +02:00
#include "common/codings/differential/nrzm.h"
2021-04-02 13:25:45 +02:00
#include "imgui/imgui.h"
2021-06-30 17:51:50 +02:00
#include "common/codings/viterbi/viterbi27.h"
#include "common/codings/correlator.h"
#include "common/codings/reedsolomon/reedsolomon.h"
2021-04-02 13:25:45 +02:00
#define FRAME_SIZE 1024
#define ENCODED_FRAME_SIZE 1024 * 8 * 2
// Return filesize
size_t getFilesize(std::string filepath);
namespace saral
{
SaralDecoderModule::SaralDecoderModule(std::string input_file, std::string output_file_hint, std::map<std::string, std::string> parameters) : ProcessingModule(input_file, output_file_hint, parameters),
2021-06-30 17:51:50 +02:00
viterbi(ENCODED_FRAME_SIZE / 2, viterbi::CCSDS_R2_K7_POLYS)
2021-04-02 13:25:45 +02:00
{
buffer = new uint8_t[ENCODED_FRAME_SIZE];
2021-06-30 17:51:50 +02:00
}
std::vector<ModuleDataType> SaralDecoderModule::getInputTypes()
{
return {DATA_FILE, DATA_STREAM};
}
std::vector<ModuleDataType> SaralDecoderModule::getOutputTypes()
{
return {DATA_FILE};
2021-04-02 13:25:45 +02:00
}
SaralDecoderModule::~SaralDecoderModule()
{
delete[] buffer;
}
void SaralDecoderModule::process()
{
2021-06-30 17:51:50 +02:00
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);
2021-04-02 13:25:45 +02:00
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;
// Correlator
2021-06-30 17:51:50 +02:00
Correlator correlator(QPSK, 0xfc4ef4fd0cc2df89);
2021-04-02 13:25:45 +02:00
// Viterbi, rs, etc
sathelper::PacketFixer packetFixer;
sathelper::Derandomizer derand;
2021-05-22 01:24:05 +02:00
reedsolomon::ReedSolomon rs(reedsolomon::RS223);
2021-04-02 13:25:45 +02:00
// Other buffers
uint8_t frameBuffer[FRAME_SIZE];
2021-06-30 17:51:50 +02:00
phase_t phase;
bool swap;
// Vectors are inverted
diff::NRZMDiff diff;
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
while (input_data_type == DATA_FILE ? !data_in.eof() : input_active.load())
2021-04-02 13:25:45 +02:00
{
2021-06-30 17:51:50 +02:00
// Read a buffer
if (input_data_type == DATA_FILE)
data_in.read((char *)buffer, ENCODED_FRAME_SIZE);
else
input_fifo->read((uint8_t *)buffer, ENCODED_FRAME_SIZE);
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
int pos = correlator.correlate((int8_t *)buffer, phase, swap, cor, ENCODED_FRAME_SIZE);
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
locked = pos == 0; // Update locking state
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
if (pos != 0 && pos < ENCODED_FRAME_SIZE) // Safety
2021-04-02 13:25:45 +02:00
{
2021-06-30 17:51:50 +02:00
std::memmove(buffer, &buffer[pos], pos);
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
if (input_data_type == DATA_FILE)
data_in.read((char *)&buffer[ENCODED_FRAME_SIZE - pos], pos);
2021-04-02 13:25:45 +02:00
else
2021-06-30 17:51:50 +02:00
input_fifo->read((uint8_t *)&buffer[ENCODED_FRAME_SIZE - pos], pos);
}
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
// Correct phase ambiguity
packetFixer.fixPacket(buffer, ENCODED_FRAME_SIZE, (sathelper::PhaseShift)phase, swap);
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
// Viterbi
viterbi.work((int8_t *)buffer, frameBuffer);
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
// Differential decoding
diff.decode(frameBuffer, FRAME_SIZE);
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
// Derandomize that frame
derand.work(&frameBuffer[4], FRAME_SIZE - 4);
2021-04-02 13:25:45 +02:00
2021-06-30 17:51:50 +02:00
// RS Correction
rs.decode_interlaved(&frameBuffer[4], true, 4, errors);
2021-04-02 13:25:45 +02:00
2021-06-30 20:46:03 +02:00
// Write it out
if (cor > 50 || pos == 0)
2021-04-02 13:25:45 +02:00
{
2021-06-30 17:51:50 +02:00
data_out.put(0x1a);
data_out.put(0xcf);
data_out.put(0xfc);
data_out.put(0x1d);
data_out.write((char *)&frameBuffer[4], FRAME_SIZE - 4);
2021-04-02 13:25:45 +02:00
}
2021-06-30 17:51:50 +02:00
if (input_data_type == DATA_FILE)
progress = data_in.tellg();
2021-04-02 13:25:45 +02:00
if (time(NULL) % 10 == 0 && lastTime != time(NULL))
{
lastTime = time(NULL);
std::string lock_state = locked ? "SYNCED" : "NOSYNC";
2021-06-30 17:51:50 +02:00
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-04-02 13:25:45 +02:00
}
}
data_out.close();
2021-06-30 17:51:50 +02:00
if (input_data_type == DATA_FILE)
data_in.close();
2021-04-02 13:25:45 +02:00
}
void SaralDecoderModule::drawUI(bool window)
{
ImGui::Begin("Saral Decoder", NULL, window ? NULL : NOWINDOW_FLAGS);
2021-06-30 17:51:50 +02:00
float ber = viterbi.ber();
2021-04-02 13:25:45 +02:00
ImGui::BeginGroup();
{
// Constellation
{
ImDrawList *draw_list = ImGui::GetWindowDrawList();
draw_list->AddRectFilled(ImGui::GetCursorScreenPos(),
ImVec2(ImGui::GetCursorScreenPos().x + 200 * ui_scale, ImGui::GetCursorScreenPos().y + 200 * ui_scale),
2021-04-02 13:25:45 +02:00
ImColor::HSV(0, 0, 0));
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),
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,
2021-04-02 13:25:45 +02:00
ImColor::HSV(113.0 / 360.0, 1, 1, 1.0));
}
ImGui::Dummy(ImVec2(200 * ui_scale + 3, 200 * ui_scale + 3));
2021-04-02 13:25:45 +02:00
}
}
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
{
ImGui::Button("Correlator", {200 * ui_scale, 20 * ui_scale});
2021-04-02 13:25:45 +02:00
{
ImGui::Text("Corr : ");
ImGui::SameLine();
2021-07-13 15:52:15 +02:00
ImGui::TextColored(locked ? IMCOLOR_SYNCED : IMCOLOR_SYNCING, UITO_C_STR(cor));
2021-04-02 13:25:45 +02:00
std::memmove(&cor_history[0], &cor_history[1], (200 - 1) * sizeof(float));
cor_history[200 - 1] = cor;
2021-06-30 17:51:50 +02:00
ImGui::PlotLines("", cor_history, IM_ARRAYSIZE(cor_history), 0, "", 40.0f, 64.0f, ImVec2(200 * ui_scale, 50 * ui_scale));
2021-04-02 13:25:45 +02:00
}
ImGui::Spacing();
ImGui::Button("Viterbi", {200 * ui_scale, 20 * ui_scale});
2021-04-02 13:25:45 +02:00
{
ImGui::Text("BER : ");
ImGui::SameLine();
2021-07-13 15:52:15 +02:00
ImGui::TextColored(ber < 0.22 ? IMCOLOR_SYNCED : IMCOLOR_NOSYNC, UITO_C_STR(ber));
2021-04-02 13:25:45 +02:00
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));
2021-04-02 13:25:45 +02:00
}
ImGui::Spacing();
ImGui::Button("Reed-Solomon", {200 * ui_scale, 20 * ui_scale});
2021-04-02 13:25:45 +02:00
{
ImGui::Text("RS : ");
for (int i = 0; i < 4; i++)
{
ImGui::SameLine();
if (errors[i] == -1)
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_NOSYNC, "%i ", i);
2021-04-02 13:25:45 +02:00
else if (errors[i] > 0)
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_SYNCING, "%i ", i);
2021-04-02 13:25:45 +02:00
else
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_SYNCED, "%i ", i);
2021-04-02 13:25:45 +02:00
}
}
}
ImGui::EndGroup();
if (!streamingInput)
ImGui::ProgressBar((float)progress / (float)filesize, ImVec2(ImGui::GetWindowWidth() - 10, 20 * ui_scale));
2021-04-02 13:25:45 +02:00
ImGui::End();
}
std::string SaralDecoderModule::getID()
{
return "saral_decoder";
}
std::vector<std::string> SaralDecoderModule::getParameters()
{
return {};
}
std::shared_ptr<ProcessingModule> SaralDecoderModule::getInstance(std::string input_file, std::string output_file_hint, std::map<std::string, std::string> parameters)
{
return std::make_shared<SaralDecoderModule>(input_file, output_file_hint, parameters);
}
} // namespace proba