sdrplusplus/decoder_modules/atv_decoder/src/main.cpp

243 lines
7.4 KiB
C++
Raw Normal View History

#include <config.h>
#include <core.h>
#include <filesystem>
#include <gui/gui.h>
#include <gui/style.h>
#include <gui/widgets/image.h>
#include <imgui.h>
#include <module.h>
#include <signal_path/signal_path.h>
#include <dsp/demod/quadrature.h>
#include <dsp/sink/handler_sink.h>
2023-12-13 23:25:18 +01:00
#include "linesync.h"
#include <dsp/loop/pll.h>
#include <dsp/convert/real_to_complex.h>
#include <dsp/filter/fir.h>
#include <dsp/taps/from_array.h>
#include "chrominance_filter.h"
#include "chroma_pll.h"
2025-03-10 17:47:56 +01:00
#include "linesync_old.h"
#include "amplitude.h"
#include <dsp/demod/am.h>
#include <dsp/loop/fast_agc.h>
#define CONCAT(a, b) ((std::string(a) + b).c_str())
SDRPP_MOD_INFO{/* Name: */ "atv_decoder",
/* Description: */ "ATV decoder for SDR++",
/* Author: */ "Ryzerth",
/* Version: */ 0, 1, 0,
2023-12-13 23:25:18 +01:00
/* Max instances */ -1
};
2025-03-10 17:47:56 +01:00
#define SAMPLE_RATE (625.0f * (float)LINE_SIZE * 25.0f)
class ATVDecoderModule : public ModuleManager::Instance {
public:
2025-03-10 17:47:56 +01:00
ATVDecoderModule(std::string name) : img(768, 576) {
this->name = name;
2025-03-10 17:47:56 +01:00
vfo = sigpath::vfoManager.createVFO(name, ImGui::WaterfallVFO::REF_CENTER, 0, 7000000.0f, SAMPLE_RATE, SAMPLE_RATE, SAMPLE_RATE, true);
2025-03-10 17:47:56 +01:00
agc.init(vfo->output, 1.0f, 1e6, 0.001f, 1.0f);
demod.init(&agc.out, SAMPLE_RATE, SAMPLE_RATE / 2.0f);
//demod.init(vfo->output, dsp::demod::AM<float>::CARRIER, 8000000.0f, 50.0 / SAMPLE_RATE, 50.0 / SAMPLE_RATE, 0.0f, SAMPLE_RATE);
2023-12-13 23:25:18 +01:00
sync.init(&demod.out, 1.0f, 1e-6, 1.0, 0.05);
sink.init(&sync.out, handler, this);
r2c.init(NULL);
2025-03-10 17:47:56 +01:00
agc.start();
demod.start();
2023-12-13 23:25:18 +01:00
sync.start();
sink.start();
gui::menu.registerEntry(name, menuHandler, this, this);
}
~ATVDecoderModule() {
if (vfo) {
sigpath::vfoManager.deleteVFO(vfo);
}
2025-03-10 17:47:56 +01:00
agc.stop();
demod.stop();
2025-03-10 17:47:56 +01:00
sync.stop();
sink.stop();
gui::menu.removeEntry(name);
}
void postInit() {}
2023-12-13 23:25:18 +01:00
void enable() {
enabled = true;
}
2023-12-13 23:25:18 +01:00
void disable() {
enabled = false;
}
bool isEnabled() { return enabled; }
private:
static void menuHandler(void *ctx) {
ATVDecoderModule *_this = (ATVDecoderModule *)ctx;
if (!_this->enabled) {
style::beginDisabled();
}
ImGui::FillWidth();
_this->img.draw();
2025-03-10 17:47:56 +01:00
ImGui::TextUnformatted("Horizontal Sync:");
ImGui::SameLine();
if (_this->sync.locked > 750) {
ImGui::TextColored(ImVec4(0, 1, 0, 1), "Locked");
2023-12-13 23:25:18 +01:00
}
2025-03-10 17:47:56 +01:00
else {
ImGui::TextUnformatted("Not locked");
2023-12-13 23:25:18 +01:00
}
2025-03-10 17:47:56 +01:00
ImGui::TextUnformatted("Vertical Sync:");
ImGui::SameLine();
if (_this->vlock > 15) {
2023-12-13 23:25:18 +01:00
ImGui::TextColored(ImVec4(0, 1, 0, 1), "Locked");
}
else {
ImGui::TextUnformatted("Not locked");
}
2025-03-10 17:47:56 +01:00
ImGui::Checkbox("Fast Lock", &_this->sync.fastLock);
ImGui::Checkbox("Color Mode", &_this->colorMode);
2023-12-13 23:25:18 +01:00
if (!_this->enabled) {
style::endDisabled();
}
2025-03-10 17:47:56 +01:00
ImGui::Text("Gain: %f", _this->gain);
ImGui::Text("Offset: %f", _this->offset);
}
static void handler(float *data, int count, void *ctx) {
ATVDecoderModule *_this = (ATVDecoderModule *)ctx;
2025-03-10 17:47:56 +01:00
// Correct the offset
volk_32f_s32f_add_32f(data, data, _this->offset, count);
// Correct the gain
volk_32f_s32f_multiply_32f(data, data, _this->gain, count);
// Compute the sync levels
float syncLLevel = 0.0f;
float syncRLevel = 0.0f;
volk_32f_accumulator_s32f(&syncLLevel, data, EQUAL_LEN);
volk_32f_accumulator_s32f(&syncRLevel, &data[EQUAL_LEN], SYNC_LEN - EQUAL_LEN);
syncLLevel *= 1.0f / EQUAL_LEN;
syncRLevel *= 1.0f / (SYNC_LEN - EQUAL_LEN);
float syncLevel = (syncLLevel + syncRLevel) * 0.5f; // TODO: It's technically correct but if the sizes were different it wouldn't be
// Compute the blanking level
float blankLevel = 0.0f;
volk_32f_accumulator_s32f(&blankLevel, &data[HBLANK_START], HBLANK_LEN);
blankLevel /= (float)HBLANK_LEN;
// Run the offset control loop
_this->offset -= (blankLevel / _this->gain)*0.001;
_this->offset = std::clamp<float>(_this->offset, -1.0f, 1.0f);
_this->gain -= (blankLevel - syncLevel + SYNC_LEVEL)*0.01f;
_this->gain = std::clamp<float>(_this->gain, 0.1f, 10.0f);
// Detect the sync type
uint16_t shortSync = (syncLLevel < 0.5f*SYNC_LEVEL) && (syncRLevel > 0.5f*SYNC_LEVEL) && (blankLevel > 0.5f*SYNC_LEVEL);
uint16_t longSync = (syncLLevel < 0.5f*SYNC_LEVEL) && (syncRLevel < 0.5f*SYNC_LEVEL) && (blankLevel < 0.5f*SYNC_LEVEL);
// Save sync type to history
_this->syncHistory = (_this->syncHistory << 2) | (longSync << 1) | shortSync;
// Render the line if it's visible
if (_this->ypos >= 34 && _this->ypos <= 34+576-1) {
uint32_t* currentLine = &((uint32_t *)_this->img.buffer)[(_this->ypos - 34)*768];
for (int i = 155; i < (155+768); i++) {
int imval = std::clamp<float>(data[i] * 255.0f, 0, 255);
currentLine[i-155] = 0xFF000000 | (imval << 16) | (imval << 8) | imval;
}
}
2025-03-10 17:47:56 +01:00
// Compute whether to rollover
bool rollToOdd = (_this->ypos == 624);
bool rollToEven = (_this->ypos == 623);
2025-03-10 17:47:56 +01:00
// Compute the field sync
bool syncToOdd = (_this->syncHistory == 0b0101011010010101);
bool syncToEven = (_this->syncHistory == 0b0001011010100101);
2023-12-13 23:25:18 +01:00
2025-03-10 17:47:56 +01:00
// Process the sync (NOTE: should start with 0b01, but for some reason I don't see a sync?)
if (rollToOdd || syncToOdd) {
// Update the vertical lock state
bool disagree = (rollToOdd ^ syncToOdd);
if (disagree && _this->vlock > 0) {
_this->vlock--;
}
else if (!disagree && _this->vlock < 20) {
_this->vlock++;
}
2023-12-13 23:25:18 +01:00
2025-03-10 17:47:56 +01:00
// Start the odd field
_this->ypos = 1;
2023-12-13 23:25:18 +01:00
}
2025-03-10 17:47:56 +01:00
else if (rollToEven || syncToEven) {
// Update the vertical lock state
bool disagree = (rollToEven ^ syncToEven);
if (disagree && _this->vlock > 0) {
_this->vlock--;
}
else if (!disagree && _this->vlock < 20) {
_this->vlock++;
}
2025-03-10 17:47:56 +01:00
// Start the even field
2023-12-13 23:25:18 +01:00
_this->ypos = 0;
2025-03-10 17:47:56 +01:00
// Swap the video buffer
2023-12-13 23:25:18 +01:00
_this->img.swap();
}
2025-03-10 17:47:56 +01:00
else {
_this->ypos += 2;
}
}
2025-03-10 17:47:56 +01:00
// NEW SYNC:
float offset = 0.0f;
float gain = 1.0f;
uint16_t syncHistory = 0;
int ypos = 0;
int vlock = 0;
std::string name;
bool enabled = true;
VFOManager::VFO *vfo = NULL;
2025-03-10 17:47:56 +01:00
//dsp::demod::Quadrature demod;
dsp::loop::FastAGC<dsp::complex_t> agc;
dsp::demod::Amplitude demod;
//dsp::demod::AM<float> demod;
2023-12-13 23:25:18 +01:00
LineSync sync;
dsp::sink::Handler<float> sink;
2023-12-13 23:25:18 +01:00
dsp::convert::RealToComplex r2c;
2025-03-10 17:47:56 +01:00
bool colorMode = false;
2023-12-13 23:25:18 +01:00
ImGui::ImageDisplay img;
};
MOD_EXPORT void _INIT_() {}
MOD_EXPORT ModuleManager::Instance *_CREATE_INSTANCE_(std::string name) { return new ATVDecoderModule(name); }
MOD_EXPORT void _DELETE_INSTANCE_(void *instance) { delete (ATVDecoderModule *)instance; }
MOD_EXPORT void _END_() {}