satdump/plugins/cubesat_support/lucky7/module_lucky7_decoder.cpp

116 lines
3.9 KiB
C++
Raw Normal View History

2023-02-14 00:40:46 +01:00
#include "module_lucky7_decoder.h"
#include "logger.h"
#include "imgui/imgui.h"
#include "common/utils.h"
2023-02-15 12:07:14 +01:00
#include "common/image/image.h"
2023-02-14 00:40:46 +01:00
namespace lucky7
{
2023-02-15 12:07:14 +01:00
Lucky7DecoderModule::Lucky7DecoderModule(std::string input_file, std::string output_file_hint, nlohmann::json parameters) : ProcessingModule(input_file, output_file_hint, parameters)
2023-02-14 00:40:46 +01:00
{
2023-02-15 12:07:14 +01:00
frame_buffer = new uint8_t[35];
2023-02-14 00:40:46 +01:00
}
Lucky7DecoderModule::~Lucky7DecoderModule()
{
2023-02-15 12:07:14 +01:00
delete[] frame_buffer;
2023-02-14 00:40:46 +01:00
}
void Lucky7DecoderModule::process()
{
2023-02-15 12:07:14 +01:00
filesize = getFilesize(d_input_file);
std::ifstream data_in(d_input_file, std::ios::binary);
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
std::string directory = d_output_file_hint.substr(0, d_output_file_hint.rfind('/')) + "/";
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
logger->info("Using input frames " + d_input_file);
logger->info("Decoding to " + directory);
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
std::map<int, ImagePayload> wip_image_payloads;
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
time_t lastTime = 0;
while (input_data_type == DATA_FILE ? !data_in.eof() : input_active.load())
{
// Read buffer
data_in.read((char *)frame_buffer, 35);
2023-02-14 00:40:46 +01:00
2023-02-15 17:01:06 +01:00
if (frame_buffer[1] == 0x00) // TLM
2023-02-14 00:40:46 +01:00
{
2023-02-15 12:07:14 +01:00
std::string callsign(&frame_buffer[6], &frame_buffer[12]);
logger->info("Telemetry " + callsign);
}
2023-02-15 17:01:06 +01:00
else if ((frame_buffer[1] & 0xC0) == 0xC0) // Imagery
2023-02-15 12:07:14 +01:00
{
uint16_t current_chunk = (frame_buffer[1] & 0xF) << 8 | frame_buffer[2];
uint16_t total_chunks = frame_buffer[5] << 8 | frame_buffer[6];
2023-05-08 23:08:34 +02:00
// logger->info("%d/%d", current_chunk, total_chunks);
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
if (wip_image_payloads.count(total_chunks) == 0)
2023-02-14 00:40:46 +01:00
{
2023-02-15 12:07:14 +01:00
ImagePayload newp;
newp.total_chunks = total_chunks + 1; // Need to account for 0
newp.has_chunks = std::vector<bool>(newp.total_chunks, false);
newp.payload.resize(newp.total_chunks * 28);
wip_image_payloads.insert({total_chunks, newp});
2023-02-14 00:40:46 +01:00
}
2023-02-15 12:07:14 +01:00
ImagePayload &payload = wip_image_payloads[total_chunks];
memcpy(&payload.payload[current_chunk * 28], &frame_buffer[7], 28);
payload.has_chunks[current_chunk] = true;
2023-02-14 00:40:46 +01:00
}
2023-02-15 17:01:06 +01:00
else if ((frame_buffer[1] & 0x20) == 0x20) // Dosimetry?
{
}
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
progress = data_in.tellg();
2023-02-14 00:40:46 +01:00
if (time(NULL) % 10 == 0 && lastTime != time(NULL))
{
lastTime = time(NULL);
logger->info("Progress " + std::to_string(round(((double)progress / (double)filesize) * 1000.0) / 10.0) + "%%");
2023-02-14 00:40:46 +01:00
}
}
logger->info("Decoding finished");
2023-02-15 12:07:14 +01:00
data_in.close();
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
for (auto imgp : wip_image_payloads)
{
2023-05-08 23:08:34 +02:00
logger->info("Image Payload %d. Got %d/%d", imgp.first, imgp.second.get_present(), imgp.second.total_chunks);
2023-02-14 00:40:46 +01:00
2023-02-15 12:07:14 +01:00
image::Image<uint8_t> img;
img.load_jpeg(imgp.second.payload.data(), imgp.second.payload.size());
2023-02-14 00:40:46 +01:00
std::string name = "Img_" + std::to_string(imgp.first);
img.save_img(directory + "/" + name);
2023-02-14 00:40:46 +01:00
}
2023-02-15 12:07:14 +01:00
}
void Lucky7DecoderModule::drawUI(bool window)
{
ImGui::Begin("Lucky-7 Decoder", NULL, window ? 0 : NOWINDOW_FLAGS);
2023-02-14 00:40:46 +01:00
if (!streamingInput)
ImGui::ProgressBar((double)progress / (double)filesize, ImVec2(ImGui::GetWindowWidth() - 10, 20 * ui_scale));
2023-02-14 00:40:46 +01:00
ImGui::End();
}
std::string Lucky7DecoderModule::getID()
{
return "lucky7_decoder";
}
std::vector<std::string> Lucky7DecoderModule::getParameters()
{
return {};
}
std::shared_ptr<ProcessingModule> Lucky7DecoderModule::getInstance(std::string input_file, std::string output_file_hint, nlohmann::json parameters)
{
return std::make_shared<Lucky7DecoderModule>(input_file, output_file_hint, parameters);
}
} // namespace noaa