satdump/plugins/eos_support/aqua/module_aqua_db_decoder.cpp

208 lines
7.1 KiB
C++
Raw Normal View History

2021-02-14 12:05:44 +01:00
#include "module_aqua_db_decoder.h"
#include "logger.h"
2021-05-22 01:24:05 +02:00
#include "common/codings/reedsolomon/reedsolomon.h"
2021-05-21 23:07:07 +02:00
#include "common/codings/differential/nrzm.h"
2021-02-17 20:07:49 +01:00
#include "imgui/imgui.h"
2022-01-06 11:05:38 +01:00
#include "common/codings/randomization.h"
#include "common/dsp/constellation.h"
2022-02-28 15:09:54 +01:00
#include "common/utils.h"
2021-02-14 12:05:44 +01:00
#define BUFFER_SIZE (1024 * 8 * 8)
2022-01-06 11:05:38 +01:00
#define FRAME_SIZE 1024
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
namespace aqua
2021-02-14 12:05:44 +01:00
{
AquaDBDecoderModule::AquaDBDecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters) : ProcessingModule(input_file, output_file_hint, parameters)
2021-02-14 13:23:54 +01:00
{
2021-02-17 20:07:49 +01:00
buffer = new uint8_t[BUFFER_SIZE];
2022-01-06 11:05:38 +01:00
deframer.STATE_SYNCED = 10;
2021-02-17 20:07:49 +01:00
}
2021-10-27 15:48:15 +02:00
std::vector<ModuleDataType> AquaDBDecoderModule::getInputTypes()
{
return {DATA_FILE, DATA_STREAM};
}
std::vector<ModuleDataType> AquaDBDecoderModule::getOutputTypes()
{
return {DATA_FILE};
}
2021-02-17 20:07:49 +01:00
AquaDBDecoderModule::~AquaDBDecoderModule()
{
delete[] buffer;
2021-02-14 13:23:54 +01:00
}
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
void AquaDBDecoderModule::process()
{
2021-10-27 15:48:15 +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-02-17 20:07:49 +01:00
data_out = std::ofstream(d_output_file_hint + ".cadu", std::ios::binary);
2021-02-14 13:23:54 +01:00
d_output_files.push_back(d_output_file_hint + ".cadu");
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
logger->info("Using input symbols " + d_input_file);
logger->info("Decoding to " + d_output_file_hint + ".cadu");
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
time_t lastTime = 0;
2021-02-14 12:05:44 +01:00
dsp::constellation_t constellation(dsp::QPSK);
2021-05-22 01:24:05 +02:00
reedsolomon::ReedSolomon rs(reedsolomon::RS223);
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
// I/Q Buffers
uint8_t decodedBufI[BUFFER_SIZE / 2];
uint8_t decodedBufQ[BUFFER_SIZE / 2];
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
// Final buffer after decoding
2022-01-06 11:05:38 +01:00
uint8_t finalBuffer[BUFFER_SIZE];
2021-02-14 12:05:44 +01:00
2021-05-21 21:16:54 +02:00
// 2 NRZ-M Decoders
diff::NRZMDiff diff1, diff2;
2022-01-06 11:05:38 +01:00
uint8_t frame_buffer[FRAME_SIZE * 100];
2021-10-27 15:48:15 +02:00
while (input_data_type == DATA_FILE ? !data_in.eof() : input_active.load())
2021-02-14 12:05:44 +01:00
{
2021-02-14 13:23:54 +01:00
// Read buffer
2021-10-27 15:48:15 +02:00
if (input_data_type == DATA_FILE)
data_in.read((char *)buffer, BUFFER_SIZE);
else
input_fifo->read((uint8_t *)buffer, BUFFER_SIZE);
2021-02-14 12:05:44 +01:00
// Demodulate QPSK Constellation
2021-02-14 13:23:54 +01:00
for (int i = 0; i < BUFFER_SIZE / 2; i++)
2021-02-14 12:05:44 +01:00
{
uint8_t demod = constellation.soft_demod((int8_t *)&buffer[i * 2]);
decodedBufI[i] = demod >> 1;
decodedBufQ[i] = demod & 1;
2021-02-14 12:05:44 +01:00
}
2021-02-14 13:23:54 +01:00
2022-01-06 11:05:38 +01:00
diff1.decode_bits(decodedBufI, BUFFER_SIZE / 2);
diff2.decode_bits(decodedBufQ, BUFFER_SIZE / 2);
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
for (int i = 0; i < BUFFER_SIZE / 2; i++)
2021-02-14 12:05:44 +01:00
{
2022-01-06 11:05:38 +01:00
finalBuffer[i * 2 + 0] = decodedBufI[i];
finalBuffer[i * 2 + 1] = decodedBufQ[i];
2021-02-14 12:05:44 +01:00
}
2022-01-06 11:05:38 +01:00
int frames = deframer.work(finalBuffer, BUFFER_SIZE, frame_buffer);
2021-02-14 12:05:44 +01:00
2022-01-06 11:05:38 +01:00
for (int i = 0; i < frames; i++)
2021-02-14 12:05:44 +01:00
{
2022-01-06 11:05:38 +01:00
uint8_t *cadu = &frame_buffer[i * FRAME_SIZE];
2021-02-14 12:05:44 +01:00
2022-01-06 11:05:38 +01:00
// Derand
derand_ccsds(&cadu[4], FRAME_SIZE - 4);
2021-02-14 12:05:44 +01:00
2022-01-06 11:05:38 +01:00
// RS Correction
rs.decode_interlaved(&cadu[4], true, 4, errors);
2021-02-14 13:23:54 +01:00
2022-01-06 11:05:38 +01:00
// Write it out
data_out.write((char *)cadu, FRAME_SIZE);
2021-02-14 13:23:54 +01:00
}
2021-02-14 12:05:44 +01:00
2021-10-27 15:48:15 +02:00
if (input_data_type == DATA_FILE)
progress = data_in.tellg();
2021-02-18 00:31:24 +01:00
2021-02-14 13:23:54 +01:00
if (time(NULL) % 10 == 0 && lastTime != time(NULL))
{
lastTime = time(NULL);
2022-01-06 19:05:23 +01:00
std::string deframer_state = deframer.getState() == deframer.STATE_NOSYNC ? "NOSYNC" : (deframer.getState() == deframer.STATE_SYNCING ? "SYNCING" : "SYNCED");
2021-02-18 00:31:24 +01:00
logger->info("Progress " + std::to_string(round(((float)progress / (float)filesize) * 1000.0f) / 10.0f) + "%, Deframer : " + deframer_state);
2021-02-14 12:05:44 +01:00
}
}
2021-02-14 13:23:54 +01:00
data_out.close();
2021-10-27 15:48:15 +02:00
if (output_data_type == DATA_FILE)
data_in.close();
2021-02-14 12:05:44 +01:00
}
2021-03-31 18:15:26 +02:00
void AquaDBDecoderModule::drawUI(bool window)
2021-02-17 20:07:49 +01:00
{
ImGui::Begin("Aqua DB Decoder", NULL, window ? NULL : NOWINDOW_FLAGS);
2021-02-17 20:07:49 +01: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-02-17 20:07:49 +01: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-02-17 20:07:49 +01:00
ImColor::HSV(113.0 / 360.0, 1, 1, 1.0));
}
ImGui::Dummy(ImVec2(200 * ui_scale + 3, 200 * ui_scale + 3));
2021-02-17 20:07:49 +01:00
}
}
ImGui::EndGroup();
ImGui::SameLine();
ImGui::BeginGroup();
{
ImGui::Button("Deframer", {200 * ui_scale, 20 * ui_scale});
2021-02-17 20:07:49 +01:00
{
ImGui::Text("State : ");
ImGui::SameLine();
2022-01-06 11:38:52 +01:00
if (deframer.getState() == deframer.STATE_NOSYNC)
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_NOSYNC, "NOSYNC");
2022-01-06 11:38:52 +01:00
else if (deframer.getState() == deframer.STATE_SYNCING)
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_SYNCING, "SYNCING");
2021-02-17 20:07:49 +01:00
else
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_SYNCED, "SYNCED");
2021-02-17 20:07:49 +01:00
}
ImGui::Spacing();
ImGui::Button("Reed-Solomon", {200 * ui_scale, 20 * ui_scale});
2021-02-17 20:07:49 +01: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-02-17 20:07:49 +01:00
else if (errors[i] > 0)
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_SYNCING, "%i ", i);
2021-02-17 20:07:49 +01:00
else
2021-07-13 15:52:15 +02:00
ImGui::TextColored(IMCOLOR_SYNCED, "%i ", i);
2021-02-17 20:07:49 +01:00
}
}
}
ImGui::EndGroup();
2021-10-27 15:48:15 +02:00
if (!streamingInput)
ImGui::ProgressBar((float)progress / (float)filesize, ImVec2(ImGui::GetWindowWidth() - 10, 20 * ui_scale));
2021-02-17 20:07:49 +01:00
ImGui::End();
}
2021-02-14 13:23:54 +01:00
std::string AquaDBDecoderModule::getID()
{
return "aqua_db_decoder";
}
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
std::vector<std::string> AquaDBDecoderModule::getParameters()
{
return {};
}
2021-02-14 12:05:44 +01:00
std::shared_ptr<ProcessingModule> AquaDBDecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
2021-02-14 13:23:54 +01:00
{
return std::make_shared<AquaDBDecoderModule>(input_file, output_file_hint, parameters);
}
} // namespace aqua