satdump/plugins/eos_support/aqua/module_aqua_db_decoder.cpp

177 lines
6.2 KiB
C++
Raw Permalink Normal View History

2021-02-14 12:05:44 +01:00
#include "module_aqua_db_decoder.h"
2021-05-21 23:07:07 +02:00
#include "common/codings/differential/nrzm.h"
2022-01-06 11:05:38 +01:00
#include "common/codings/randomization.h"
#include "common/codings/reedsolomon/reedsolomon.h"
2023-01-31 01:12:10 +01:00
#include "common/dsp/demod/constellation.h"
2022-02-28 15:09:54 +01:00
#include "common/utils.h"
#include "imgui/imgui.h"
#include "logger.h"
#include <cstdint>
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)
: satdump::pipeline::base::FileStreamToFileStreamModule(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-09-21 14:22:53 +02:00
deframer.STATE_SYNCING = 6;
2022-01-06 11:05:38 +01:00
deframer.STATE_SYNCED = 10;
fsfsm_file_ext = ".cadu";
2021-02-17 20:07:49 +01:00
}
AquaDBDecoderModule::~AquaDBDecoderModule() { delete[] buffer; }
2021-02-14 12:05:44 +01:00
2021-02-14 13:23:54 +01:00
void AquaDBDecoderModule::process()
{
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];
while (should_run())
2021-02-14 12:05:44 +01:00
{
2021-02-14 13:23:54 +01:00
// Read buffer
read_data((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
write_data((uint8_t *)cadu, FRAME_SIZE);
2021-02-14 12:05:44 +01:00
}
}
cleanup();
}
nlohmann::json AquaDBDecoderModule::getModuleStats()
{
auto v = satdump::pipeline::base::FileStreamToFileStreamModule::getModuleStats();
std::string deframer_state = deframer.getState() == deframer.STATE_NOSYNC ? "NOSYNC" : (deframer.getState() == deframer.STATE_SYNCING ? "SYNCING" : "SYNCED");
v["deframer_state"] = deframer_state;
return v;
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
{
2022-09-02 01:59:13 +02:00
ImGui::Begin("Aqua DB Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);
2021-02-17 20:07:49 +01:00
ImGui::BeginGroup();
{
// Constellation
{
ImDrawList *draw_list = ImGui::GetWindowDrawList();
ImVec2 rect_min = ImGui::GetCursorScreenPos();
ImVec2 rect_max = {rect_min.x + 200 * ui_scale, rect_min.y + 200 * ui_scale};
draw_list->AddRectFilled(rect_min, rect_max, style::theme.widget_bg);
draw_list->PushClipRect(rect_min, rect_max);
2021-02-17 20:07:49 +01:00
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, style::theme.constellation);
2021-02-17 20:07:49 +01:00
}
draw_list->PopClipRect();
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)
2024-01-21 14:57:41 -05:00
ImGui::TextColored(style::theme.red, "NOSYNC");
2022-01-06 11:38:52 +01:00
else if (deframer.getState() == deframer.STATE_SYNCING)
2024-01-21 14:57:41 -05:00
ImGui::TextColored(style::theme.orange, "SYNCING");
2021-02-17 20:07:49 +01:00
else
2024-01-21 14:57:41 -05:00
ImGui::TextColored(style::theme.green, "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 (deframer.getState() == deframer.STATE_NOSYNC)
{
ImGui::TextColored(ImGui::GetStyleColorVec4(ImGuiCol_TextDisabled), "%i ", i);
}
2021-02-17 20:07:49 +01:00
else
{
if (errors[i] == -1)
ImGui::TextColored(style::theme.red, "%i ", i);
else if (errors[i] > 0)
ImGui::TextColored(style::theme.orange, "%i ", i);
else
ImGui::TextColored(style::theme.green, "%i ", i);
}
2021-02-17 20:07:49 +01:00
}
}
}
ImGui::EndGroup();
drawProgressBar();
2021-02-17 20:07:49 +01:00
ImGui::End();
}
std::string AquaDBDecoderModule::getID() { return "aqua_db_decoder"; }
2021-02-14 12:05:44 +01:00
std::shared_ptr<satdump::pipeline::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