1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-19 06:41:47 -05:00
sdrangel/plugins/channelrx/demoddatv/datvdemod.cpp

1030 lines
30 KiB
C++
Raw Normal View History

2018-02-22 16:52:49 -05:00
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2018 F4HKW //
// for F4EXB / SDRAngel //
// using LeanSDR Framework (C) 2016 F4DAV //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation as version 3 of the License, or //
2019-04-11 00:39:30 -04:00
// (at your option) any later version. //
2018-02-22 16:52:49 -05:00
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "datvdemod.h"
#include <QTime>
#include <QDebug>
#include <stdio.h>
#include <complex.h>
#include "audio/audiooutput.h"
#include "dsp/dspengine.h"
#include "dsp/downchannelizer.h"
#include "dsp/threadedbasebandsamplesink.h"
#include "device/devicesourceapi.h"
const QString DATVDemod::m_channelIdURI = "sdrangel.channel.demoddatv";
const QString DATVDemod::m_channelId = "DATVDemod";
MESSAGE_CLASS_DEFINITION(DATVDemod::MsgConfigureDATVDemod, Message)
MESSAGE_CLASS_DEFINITION(DATVDemod::MsgConfigureChannelizer, Message)
DATVDemod::DATVDemod(DeviceSourceAPI *deviceAPI) :
2018-02-25 19:04:45 -05:00
ChannelSinkAPI(m_channelIdURI),
m_blnNeedConfigUpdate(false),
m_deviceAPI(deviceAPI),
2018-03-11 11:39:02 -04:00
m_objRegisteredTVScreen(0),
m_objRegisteredVideoRender(0),
2019-03-16 20:36:44 -04:00
m_objVideoStream(nullptr),
m_objRenderThread(nullptr),
2019-03-19 18:12:54 -04:00
m_audioFifo(48000),
2018-02-25 19:04:45 -05:00
m_blnRenderingVideo(false),
m_blnStartStopVideo(false),
m_enmModulation(DATVDemodSettings::BPSK /*DATV_FM1*/),
m_sampleRate(1024000),
2018-02-25 19:04:45 -05:00
m_objSettingsMutex(QMutex::NonRecursive)
2018-02-22 16:52:49 -05:00
{
setObjectName("DATVDemod");
2019-03-19 18:12:54 -04:00
DSPEngine::instance()->getAudioDeviceManager()->addAudioSink(&m_audioFifo, getInputMessageQueue());
//m_audioSampleRate = DSPEngine::instance()->getAudioDeviceManager()->getOutputSampleRate();
2018-02-22 16:52:49 -05:00
//*************** DATV PARAMETERS ***************
2018-02-25 19:04:45 -05:00
m_blnInitialized=false;
CleanUpDATVFramework(false);
2018-02-22 16:52:49 -05:00
m_objVideoStream = new DATVideostream();
m_objRFFilter = new fftfilt(-256000.0 / 1024000.0, 256000.0 / 1024000.0, rfFilterFftLength);
m_channelizer = new DownChannelizer(this);
2018-02-22 16:52:49 -05:00
m_threadedChannelizer = new ThreadedBasebandSampleSink(m_channelizer, this);
m_deviceAPI->addThreadedSink(m_threadedChannelizer);
m_deviceAPI->addChannelAPI(this);
}
DATVDemod::~DATVDemod()
{
2018-02-25 19:04:45 -05:00
m_blnInitialized=false;
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
if(m_objVideoStream!=nullptr)
2018-02-22 16:52:49 -05:00
{
2018-02-25 19:04:45 -05:00
//Immediately exit from DATVideoStream if waiting for data before killing thread
m_objVideoStream->ThreadTimeOut=0;
2018-02-22 16:52:49 -05:00
}
2019-03-19 18:12:54 -04:00
DSPEngine::instance()->getAudioDeviceManager()->removeAudioSink(&m_audioFifo);
2019-03-16 20:36:44 -04:00
if(m_objRenderThread!=nullptr)
2018-02-22 16:52:49 -05:00
{
2018-02-25 19:04:45 -05:00
if(m_objRenderThread->isRunning())
{
m_objRenderThread->stopRendering();
m_objRenderThread->quit();
}
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
m_objRenderThread->wait(2000);
2018-02-22 16:52:49 -05:00
}
2018-02-25 19:04:45 -05:00
CleanUpDATVFramework(true);
2018-02-22 16:52:49 -05:00
m_deviceAPI->removeChannelAPI(this);
m_deviceAPI->removeThreadedSink(m_threadedChannelizer);
delete m_threadedChannelizer;
delete m_channelizer;
delete m_objRFFilter;
2018-02-22 16:52:49 -05:00
}
2018-03-11 11:39:02 -04:00
bool DATVDemod::SetTVScreen(TVScreen *objScreen)
2018-02-22 16:52:49 -05:00
{
2018-03-11 11:39:02 -04:00
m_objRegisteredTVScreen = objScreen;
2018-02-24 18:07:08 -05:00
return true;
2018-02-22 16:52:49 -05:00
}
2019-03-19 18:12:54 -04:00
DATVideostream *DATVDemod::SetVideoRender(DATVideoRender *objScreen)
2018-02-22 16:52:49 -05:00
{
m_objRegisteredVideoRender = objScreen;
2019-03-19 18:12:54 -04:00
m_objRegisteredVideoRender->setAudioFIFO(&m_audioFifo);
m_objRenderThread = new DATVideoRenderThread(m_objRegisteredVideoRender, m_objVideoStream);
2018-02-22 16:52:49 -05:00
return m_objVideoStream;
}
2019-03-19 18:12:54 -04:00
bool DATVDemod::audioActive()
{
if (m_objRegisteredVideoRender) {
return m_objRegisteredVideoRender->getAudioStreamIndex() >= 0;
} else {
return false;
}
}
2018-02-25 19:04:45 -05:00
bool DATVDemod::videoActive()
{
if (m_objRegisteredVideoRender) {
return m_objRegisteredVideoRender->getVideoStreamIndex() >= 0;
} else {
return false;
}
}
bool DATVDemod::audioDecodeOK()
{
if (m_objRegisteredVideoRender) {
return m_objRegisteredVideoRender->getAudioDecodeOK();
} else {
return false;
}
}
bool DATVDemod::videoDecodeOK()
{
if (m_objRegisteredVideoRender) {
return m_objRegisteredVideoRender->getVideoDecodeOK();
} else {
return false;
}
}
2018-02-22 16:52:49 -05:00
bool DATVDemod::PlayVideo(bool blnStartStop)
{
2019-03-16 20:36:44 -04:00
if (m_objVideoStream == nullptr) {
2018-02-22 16:52:49 -05:00
return false;
}
2019-03-16 20:36:44 -04:00
if (m_objRegisteredVideoRender == nullptr) {
2018-02-22 16:52:49 -05:00
return false;
}
2019-03-16 20:36:44 -04:00
if (m_objRenderThread == nullptr) {
2018-02-22 16:52:49 -05:00
return false;
}
2019-03-16 20:36:44 -04:00
if (m_blnStartStopVideo && !blnStartStop) {
return true;
2018-02-22 16:52:49 -05:00
}
2019-03-16 20:36:44 -04:00
if (blnStartStop == true) {
m_blnStartStopVideo = true;
2018-02-25 19:04:45 -05:00
}
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
if (m_objRenderThread->isRunning())
2018-02-25 19:04:45 -05:00
{
2019-03-16 20:36:44 -04:00
if (blnStartStop == true) {
m_objRenderThread->stopRendering();
}
2018-02-25 19:04:45 -05:00
2019-03-16 20:36:44 -04:00
return true;
2018-02-25 19:04:45 -05:00
}
2019-03-16 20:36:44 -04:00
if (m_objVideoStream->bytesAvailable() > 0)
{
2019-03-16 20:36:44 -04:00
m_objRenderThread->setStreamAndRenderer(m_objRegisteredVideoRender, m_objVideoStream);
m_objVideoStream->MultiThreaded = true;
m_objVideoStream->ThreadTimeOut = 5000; //5000 ms
m_objRenderThread->start();
}
2018-02-22 16:52:49 -05:00
return true;
}
2018-02-25 19:04:45 -05:00
void DATVDemod::CleanUpDATVFramework(bool blnRelease)
2018-02-22 16:52:49 -05:00
{
2019-03-16 20:36:44 -04:00
if (blnRelease == true)
2018-02-25 19:04:45 -05:00
{
2019-03-16 20:36:44 -04:00
if (m_objScheduler != nullptr)
2018-02-25 19:04:45 -05:00
{
m_objScheduler->shutdown();
delete m_objScheduler;
}
// NOTCH FILTER
2019-03-16 20:36:44 -04:00
if (r_auto_notch != nullptr) {
delete r_auto_notch;
}
if (p_autonotched != nullptr) {
delete p_autonotched;
}
2018-02-25 19:04:45 -05:00
// FREQUENCY CORRECTION : DEROTATOR
2019-03-16 20:36:44 -04:00
if (p_derot != nullptr) {
delete p_derot;
}
if (r_derot != nullptr) {
delete r_derot;
}
2018-02-25 19:04:45 -05:00
// CNR ESTIMATION
2019-03-16 20:36:44 -04:00
if (p_cnr != nullptr) {
delete p_cnr;
}
if (r_cnr != nullptr) {
delete r_cnr;
}
2018-02-25 19:04:45 -05:00
//FILTERING
2019-03-16 20:36:44 -04:00
if (r_resample != nullptr) {
delete r_resample;
}
if (p_resampled != nullptr) {
delete p_resampled;
}
if (coeffs != nullptr) {
delete coeffs;
}
2018-02-25 19:04:45 -05:00
// OUTPUT PREPROCESSED DATA
2019-03-16 20:36:44 -04:00
if (sampler != nullptr) {
delete sampler;
}
if (coeffs_sampler != nullptr) {
delete coeffs_sampler;
}
if (p_symbols != nullptr) {
delete p_symbols;
}
if (p_freq != nullptr) {
delete p_freq;
}
if (p_ss != nullptr) {
delete p_ss;
}
if (p_mer != nullptr) {
delete p_mer;
}
if (p_sampled != nullptr) {
delete p_sampled;
}
2018-02-25 19:04:45 -05:00
//DECIMATION
2019-03-16 20:36:44 -04:00
if (p_decimated != nullptr) {
delete p_decimated;
}
if (p_decim != nullptr) {
delete p_decim;
}
if (r_ppout != nullptr) {
delete r_ppout;
}
2018-02-25 19:04:45 -05:00
//GENERIC CONSTELLATION RECEIVER
2019-03-16 20:36:44 -04:00
if (m_objDemodulator != nullptr) {
delete m_objDemodulator;
}
2018-02-25 19:04:45 -05:00
//DECONVOLUTION AND SYNCHRONIZATION
2019-03-16 20:36:44 -04:00
if (p_bytes != nullptr) {
delete p_bytes;
}
if (r_deconv != nullptr) {
delete r_deconv;
}
if (r != nullptr) {
delete r;
}
if (p_descrambled != nullptr) {
delete p_descrambled;
}
if (p_frames != nullptr) {
delete p_frames;
}
if (r_etr192_descrambler != nullptr) {
delete r_etr192_descrambler;
}
if (r_sync != nullptr) {
delete r_sync;
}
if (p_mpegbytes != nullptr) {
delete p_mpegbytes;
}
if (p_lock != nullptr) {
delete p_lock;
}
if (p_locktime != nullptr) {
delete p_locktime;
}
if (r_sync_mpeg != nullptr) {
delete r_sync_mpeg;
}
2018-02-25 19:04:45 -05:00
// DEINTERLEAVING
2019-03-16 20:36:44 -04:00
if (p_rspackets != nullptr) {
delete p_rspackets;
}
if (r_deinter != nullptr) {
delete r_deinter;
}
if (p_vbitcount != nullptr) {
delete p_vbitcount;
}
if (p_verrcount != nullptr) {
delete p_verrcount;
}
if (p_rtspackets != nullptr) {
delete p_rtspackets;
}
if (r_rsdec != nullptr) {
delete r_rsdec;
}
2018-02-25 19:04:45 -05:00
//BER ESTIMATION
2019-03-16 20:36:44 -04:00
if (p_vber != nullptr) {
delete p_vber;
}
if (r_vber != nullptr) {
delete r_vber;
}
2018-02-25 19:04:45 -05:00
// DERANDOMIZATION
2019-03-16 20:36:44 -04:00
if (p_tspackets != nullptr) {
delete p_tspackets;
}
if (r_derand != nullptr) {
delete r_derand;
}
2018-02-25 19:04:45 -05:00
//OUTPUT : To remove
2019-03-16 20:36:44 -04:00
if (r_stdout != nullptr) {
delete r_stdout;
}
if (r_videoplayer != nullptr) {
delete r_videoplayer;
}
2018-02-25 19:04:45 -05:00
//CONSTELLATION
2019-03-16 20:36:44 -04:00
if (r_scope_symbols != nullptr) {
delete r_scope_symbols;
}
2018-02-25 19:04:45 -05:00
// INPUT
2019-03-16 20:36:44 -04:00
//if(p_rawiq!=nullptr) delete p_rawiq;
//if(p_rawiq_writer!=nullptr) delete p_rawiq_writer;
//if(p_preprocessed!=nullptr) delete p_preprocessed;
2018-02-25 19:04:45 -05:00
}
2019-03-16 20:36:44 -04:00
m_objScheduler=nullptr;
2018-02-22 16:52:49 -05:00
// INPUT
2019-03-16 20:36:44 -04:00
p_rawiq = nullptr;
p_rawiq_writer = nullptr;
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
p_preprocessed = nullptr;
2018-02-22 16:52:49 -05:00
// NOTCH FILTER
2019-03-16 20:36:44 -04:00
r_auto_notch = nullptr;
p_autonotched = nullptr;
2018-02-22 16:52:49 -05:00
// FREQUENCY CORRECTION : DEROTATOR
2019-03-16 20:36:44 -04:00
p_derot = nullptr;
r_derot=nullptr;
2018-02-22 16:52:49 -05:00
// CNR ESTIMATION
2019-03-16 20:36:44 -04:00
p_cnr = nullptr;
r_cnr = nullptr;
2018-02-22 16:52:49 -05:00
//FILTERING
2019-03-16 20:36:44 -04:00
r_resample = nullptr;
p_resampled = nullptr;
coeffs = nullptr;
2018-02-25 19:04:45 -05:00
ncoeffs=0;
2018-02-22 16:52:49 -05:00
// OUTPUT PREPROCESSED DATA
2019-03-16 20:36:44 -04:00
sampler = nullptr;
coeffs_sampler=nullptr;
2018-02-25 19:04:45 -05:00
ncoeffs_sampler=0;
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
p_symbols = nullptr;
p_freq = nullptr;
p_ss = nullptr;
p_mer = nullptr;
p_sampled = nullptr;
2018-02-22 16:52:49 -05:00
//DECIMATION
2019-03-16 20:36:44 -04:00
p_decimated = nullptr;
p_decim = nullptr;
r_ppout = nullptr;
2018-02-22 16:52:49 -05:00
//GENERIC CONSTELLATION RECEIVER
2019-03-16 20:36:44 -04:00
m_objDemodulator = nullptr;
2018-02-22 16:52:49 -05:00
//DECONVOLUTION AND SYNCHRONIZATION
2019-03-16 20:36:44 -04:00
p_bytes=nullptr;
r_deconv=nullptr;
r = nullptr;
2018-02-25 19:04:45 -05:00
2019-03-16 20:36:44 -04:00
p_descrambled = nullptr;
p_frames = nullptr;
r_etr192_descrambler = nullptr;
r_sync = nullptr;
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
p_mpegbytes = nullptr;
p_lock = nullptr;
p_locktime = nullptr;
r_sync_mpeg = nullptr;
2018-02-22 16:52:49 -05:00
// DEINTERLEAVING
2019-03-16 20:36:44 -04:00
p_rspackets = nullptr;
r_deinter = nullptr;
2018-02-25 19:04:45 -05:00
2019-03-16 20:36:44 -04:00
p_vbitcount = nullptr;
p_verrcount = nullptr;
p_rtspackets = nullptr;
r_rsdec = nullptr;
2018-02-22 16:52:49 -05:00
//BER ESTIMATION
2019-03-16 20:36:44 -04:00
p_vber = nullptr;
r_vber = nullptr;
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
// DERANDOMIZATION
2019-03-16 20:36:44 -04:00
p_tspackets = nullptr;
r_derand = nullptr;
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
//OUTPUT : To remove
2019-03-16 20:36:44 -04:00
r_stdout = nullptr;
r_videoplayer = nullptr;
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
//CONSTELLATION
2019-03-16 20:36:44 -04:00
r_scope_symbols = nullptr;
2018-02-22 16:52:49 -05:00
}
void DATVDemod::InitDATVFramework()
{
2019-03-16 20:36:44 -04:00
m_blnDVBInitialized = false;
m_lngReadIQ = 0;
2018-02-25 19:04:45 -05:00
CleanUpDATVFramework(false);
qDebug() << "DATVDemod::InitDATVFramework:"
<< " Symbol Rate: " << m_settings.m_symbolRate
<< " Modulation: " << m_settings.m_modulation
<< " Notch Filters: " << m_settings.m_notchFilters
<< " Allow Drift: " << m_settings.m_allowDrift
<< " Fast Lock: " << m_settings.m_fastLock
<< " Filter: " << m_settings.m_filter
<< " HARD METRIC: " << m_settings.m_hardMetric
<< " RollOff: " << m_settings.m_rollOff
<< " Viterbi: " << m_settings.m_viterbi
<< " Excursion: " << m_settings.m_excursion;
m_objCfg.standard = m_settings.m_standard;
m_objCfg.fec = m_settings.m_fec;
m_objCfg.Fs = (float) m_sampleRate;
m_objCfg.Fm = (float) m_settings.m_symbolRate;
m_objCfg.fastlock = m_settings.m_fastLock;
m_objCfg.sampler = m_settings.m_filter;
m_objCfg.rolloff = m_settings.m_rollOff; //0...1
m_objCfg.rrc_rej = (float) m_settings.m_excursion; //dB
2019-03-16 20:36:44 -04:00
m_objCfg.rrc_steps = 0; //auto
2018-02-25 19:04:45 -05:00
switch(m_settings.m_modulation)
2018-02-22 16:52:49 -05:00
{
case DATVDemodSettings::BPSK:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::BPSK;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::QPSK:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::QPSK;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::PSK8:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::PSK8;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::APSK16:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::APSK16;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::APSK32:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::APSK32;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::APSK64E:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::APSK64E;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::QAM16:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::QAM16;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::QAM64:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::QAM64;
2019-03-16 20:36:44 -04:00
break;
case DATVDemodSettings::QAM256:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::QAM256;
2019-03-16 20:36:44 -04:00
break;
2018-02-25 19:04:45 -05:00
default:
m_objCfg.constellation = leansdr::cstln_lut<leansdr::eucl_ss, 256>::BPSK;
2019-03-16 20:36:44 -04:00
break;
2018-02-22 16:52:49 -05:00
}
m_objCfg.allow_drift = m_settings.m_allowDrift;
m_objCfg.anf = m_settings.m_notchFilters;
m_objCfg.hard_metric = m_settings.m_hardMetric;
m_objCfg.sampler = m_settings.m_filter;
m_objCfg.viterbi = m_settings.m_viterbi;
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
// Min buffer size for baseband data
// scopes: 1024
// ss_estimator: 1024
// anf: 4096
// cstln_receiver: reads in chunks of 128+1
BUF_BASEBAND = 4096 * m_objCfg.buf_factor;
// Min buffer size for IQ symbols
// cstln_receiver: writes in chunks of 128/omega symbols (margin 128)
// deconv_sync: reads at least 64+32
// A larger buffer improves performance significantly.
BUF_SYMBOLS = 1024 * m_objCfg.buf_factor;
// Min buffer size for unsynchronized bytes
// deconv_sync: writes 32 bytes
// mpeg_sync: reads up to 204*scan_syncs = 1632 bytes
BUF_BYTES = 2048 * m_objCfg.buf_factor;
// Min buffer size for synchronized (but interleaved) bytes
// mpeg_sync: writes 1 rspacket
// deinterleaver: reads 17*11*12+204 = 2448 bytes
BUF_MPEGBYTES = 2448 * m_objCfg.buf_factor;
// Min buffer size for packets: 1
BUF_PACKETS = m_objCfg.buf_factor;
// Min buffer size for misc measurements: 1
BUF_SLOW = m_objCfg.buf_factor;
2018-02-25 19:04:45 -05:00
m_lngExpectedReadIQ = BUF_BASEBAND;
2018-02-22 16:52:49 -05:00
m_objScheduler = new leansdr::scheduler();
2018-02-22 16:52:49 -05:00
//***************
p_rawiq = new leansdr::pipebuf<leansdr::cf32>(m_objScheduler, "rawiq", BUF_BASEBAND);
p_rawiq_writer = new leansdr::pipewriter<leansdr::cf32>(*p_rawiq);
2018-02-22 16:52:49 -05:00
p_preprocessed = p_rawiq;
// NOTCH FILTER
2019-03-16 20:36:44 -04:00
if (m_objCfg.anf>0)
2018-02-22 16:52:49 -05:00
{
p_autonotched = new leansdr::pipebuf<leansdr::cf32>(m_objScheduler, "autonotched", BUF_BASEBAND);
r_auto_notch = new leansdr::auto_notch<leansdr::f32>(m_objScheduler, *p_preprocessed, *p_autonotched, m_objCfg.anf, 0);
2018-02-22 16:52:49 -05:00
p_preprocessed = p_autonotched;
}
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
// FREQUENCY CORRECTION
2018-02-25 19:04:45 -05:00
//******** -> if ( m_objCfg.Fderot>0 )
2018-02-22 16:52:49 -05:00
// CNR ESTIMATION
p_cnr = new leansdr::pipebuf<leansdr::f32>(m_objScheduler, "cnr", BUF_SLOW);
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
if (m_objCfg.cnr == true)
{
r_cnr = new leansdr::cnr_fft<leansdr::f32>(m_objScheduler, *p_preprocessed, *p_cnr, m_objCfg.Fm/m_objCfg.Fs);
2018-02-22 16:52:49 -05:00
r_cnr->decimation = decimation(m_objCfg.Fs, 1); // 1 Hz
}
// FILTERING
int decim = 1;
2018-02-25 19:04:45 -05:00
//******** -> if ( m_objCfg.resample )
2018-02-22 16:52:49 -05:00
// DECIMATION
// (Unless already done in resampler)
2018-02-25 19:04:45 -05:00
//******** -> if ( !m_objCfg.resample && m_objCfg.decim>1 )
2018-02-22 16:52:49 -05:00
//Resampling FS
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
// Generic constellation receiver
p_symbols = new leansdr::pipebuf<leansdr::eucl_ss>(m_objScheduler, "PSK soft-symbols", BUF_SYMBOLS);
p_freq = new leansdr::pipebuf<leansdr::f32> (m_objScheduler, "freq", BUF_SLOW);
p_ss = new leansdr::pipebuf<leansdr::f32> (m_objScheduler, "SS", BUF_SLOW);
p_mer = new leansdr::pipebuf<leansdr::f32> (m_objScheduler, "MER", BUF_SLOW);
p_sampled = new leansdr::pipebuf<leansdr::cf32> (m_objScheduler, "PSK symbols", BUF_BASEBAND);
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
switch (m_objCfg.sampler)
2018-02-22 16:52:49 -05:00
{
case DATVDemodSettings::SAMP_NEAREST:
sampler = new leansdr::nearest_sampler<float>();
2018-02-25 19:04:45 -05:00
break;
case DATVDemodSettings::SAMP_LINEAR:
sampler = new leansdr::linear_sampler<float>();
2018-02-25 19:04:45 -05:00
break;
case DATVDemodSettings::SAMP_RRC:
{
2019-03-16 20:36:44 -04:00
if (m_objCfg.rrc_steps == 0)
2018-02-25 19:04:45 -05:00
{
2018-02-22 16:52:49 -05:00
// At least 64 discrete sampling points between symbols
m_objCfg.rrc_steps = std::max(1, (int)(64*m_objCfg.Fm / m_objCfg.Fs));
2018-02-25 19:04:45 -05:00
}
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
float Frrc = m_objCfg.Fs * m_objCfg.rrc_steps; // Sample freq of the RRC filter
float transition = (m_objCfg.Fm/2) * m_objCfg.rolloff;
int order = m_objCfg.rrc_rej * Frrc / (22*transition);
ncoeffs_sampler = leansdr::filtergen::root_raised_cosine(order, m_objCfg.Fm/Frrc, m_objCfg.rolloff, &coeffs_sampler);
sampler = new leansdr::fir_sampler<float,float>(ncoeffs_sampler, coeffs_sampler, m_objCfg.rrc_steps);
2018-02-25 19:04:45 -05:00
break;
}
default:
qCritical("DATVDemod::InitDATVFramework: Interpolator not implemented");
return;
2018-02-22 16:52:49 -05:00
}
m_objDemodulator = new leansdr::cstln_receiver<leansdr::f32, leansdr::eucl_ss>(
2019-03-16 20:36:44 -04:00
m_objScheduler,
sampler,
*p_preprocessed,
*p_symbols,
p_freq,
p_ss,
p_mer,
p_sampled);
if (m_objCfg.standard == DATVDemodSettings::DVB_S)
2018-02-22 16:52:49 -05:00
{
if ( m_objCfg.constellation != leansdr::cstln_lut<leansdr::eucl_ss, 256>::QPSK
&& m_objCfg.constellation != leansdr::cstln_lut<leansdr::eucl_ss, 256>::BPSK )
2019-03-16 20:36:44 -04:00
{
qWarning("DATVDemod::InitDATVFramework: non-standard constellation for DVB-S");
}
2018-02-22 16:52:49 -05:00
}
if (m_objCfg.standard == DATVDemodSettings::DVB_S2)
2018-02-22 16:52:49 -05:00
{
2019-03-16 20:36:44 -04:00
// For DVB-S2 testing only.
// Constellation should be determined from PL signalling.
qDebug("DATVDemod::InitDATVFramework: DVB-S2: Testing symbol sampler only.");
2018-02-22 16:52:49 -05:00
}
m_objDemodulator->cstln = make_dvbs2_constellation(m_objCfg.constellation, m_objCfg.fec);
2019-03-16 20:36:44 -04:00
if (m_objCfg.hard_metric) {
m_objDemodulator->cstln->harden();
2018-02-22 16:52:49 -05:00
}
2018-02-25 19:04:45 -05:00
m_objDemodulator->set_omega(m_objCfg.Fs/m_objCfg.Fm);
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
//******** if ( m_objCfg.Ftune )
//{
// m_objDemodulator->set_freq(m_objCfg.Ftune/m_objCfg.Fs);
//}
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
if (m_objCfg.allow_drift) {
m_objDemodulator->set_allow_drift(true);
2018-02-22 16:52:49 -05:00
}
2018-02-25 19:04:45 -05:00
//******** -> if ( m_objCfg.viterbi )
2019-03-16 20:36:44 -04:00
if (m_objCfg.viterbi) {
m_objDemodulator->pll_adjustment /= 6;
2018-02-22 16:52:49 -05:00
}
m_objDemodulator->meas_decimation = decimation(m_objCfg.Fs, m_objCfg.Finfo);
// TRACKING FILTERS
2019-03-16 20:36:44 -04:00
if (r_cnr)
2018-02-22 16:52:49 -05:00
{
2019-03-16 20:36:44 -04:00
r_cnr->freq_tap = &m_objDemodulator->freq_tap;
r_cnr->tap_multiplier = 1.0 / decim;
2018-02-22 16:52:49 -05:00
}
//constellation
2018-03-11 11:39:02 -04:00
if (m_objRegisteredTVScreen)
{
2018-03-11 11:39:02 -04:00
m_objRegisteredTVScreen->resizeTVScreen(256,256);
2019-03-16 20:36:44 -04:00
r_scope_symbols = new leansdr::datvconstellation<leansdr::f32>(m_objScheduler, *p_sampled, -128,128, nullptr, m_objRegisteredTVScreen);
r_scope_symbols->decimation = 1;
r_scope_symbols->cstln = &m_objDemodulator->cstln;
r_scope_symbols->calculate_cstln_points();
}
2018-02-22 16:52:49 -05:00
// DECONVOLUTION AND SYNCHRONIZATION
p_bytes = new leansdr::pipebuf<leansdr::u8>(m_objScheduler, "bytes", BUF_BYTES);
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
r_deconv = nullptr;
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
//******** -> if ( m_objCfg.viterbi )
2019-03-16 20:36:44 -04:00
if (m_objCfg.viterbi)
2018-02-22 16:52:49 -05:00
{
2019-03-16 20:36:44 -04:00
if (m_objCfg.fec == leansdr::FEC23 && (m_objDemodulator->cstln->nsymbols == 4 || m_objDemodulator->cstln->nsymbols == 64)) {
m_objCfg.fec = leansdr::FEC46;
}
//To uncomment -> Linking Problem : undefined symbol: _ZN7leansdr21viterbi_dec_interfaceIhhiiE6updateEPiS2_
r = new leansdr::viterbi_sync(m_objScheduler, (*p_symbols), (*p_bytes), m_objDemodulator->cstln, m_objCfg.fec);
if (m_objCfg.fastlock) {
r->resync_period = 1;
}
2018-02-22 16:52:49 -05:00
}
else
{
r_deconv = make_deconvol_sync_simple(m_objScheduler, (*p_symbols), (*p_bytes), m_objCfg.fec);
r_deconv->fastlock = m_objCfg.fastlock;
2018-02-22 16:52:49 -05:00
}
2018-02-25 19:04:45 -05:00
//******* -> if ( m_objCfg.hdlc )
2018-02-22 16:52:49 -05:00
p_mpegbytes = new leansdr::pipebuf<leansdr::u8> (m_objScheduler, "mpegbytes", BUF_MPEGBYTES);
p_lock = new leansdr::pipebuf<int> (m_objScheduler, "lock", BUF_SLOW);
p_locktime = new leansdr::pipebuf<leansdr::u32> (m_objScheduler, "locktime", BUF_PACKETS);
2018-02-22 16:52:49 -05:00
r_sync_mpeg = new leansdr::mpeg_sync<leansdr::u8, 0>(m_objScheduler, *p_bytes, *p_mpegbytes, r_deconv, p_lock, p_locktime);
2018-02-25 19:04:45 -05:00
r_sync_mpeg->fastlock = m_objCfg.fastlock;
2018-02-22 16:52:49 -05:00
// DEINTERLEAVING
2019-03-16 20:36:44 -04:00
p_rspackets = new leansdr::pipebuf<leansdr::rspacket<leansdr::u8> >(m_objScheduler, "RS-enc packets", BUF_PACKETS);
r_deinter = new leansdr::deinterleaver<leansdr::u8>(m_objScheduler, *p_mpegbytes, *p_rspackets);
2018-02-22 16:52:49 -05:00
// REED-SOLOMON
p_vbitcount = new leansdr::pipebuf<int>(m_objScheduler, "Bits processed", BUF_PACKETS);
p_verrcount = new leansdr::pipebuf<int>(m_objScheduler, "Bits corrected", BUF_PACKETS);
p_rtspackets = new leansdr::pipebuf<leansdr::tspacket>(m_objScheduler, "rand TS packets", BUF_PACKETS);
2019-03-16 20:36:44 -04:00
r_rsdec = new leansdr::rs_decoder<leansdr::u8, 0>(m_objScheduler, *p_rspackets, *p_rtspackets, p_vbitcount, p_verrcount);
2018-02-22 16:52:49 -05:00
// BER ESTIMATION
2018-02-25 19:04:45 -05:00
/*
2019-03-16 20:36:44 -04:00
p_vber = new pipebuf<float> (m_objScheduler, "VBER", BUF_SLOW);
r_vber = new rate_estimator<float> (m_objScheduler, *p_verrcount, *p_vbitcount, *p_vber);
r_vber->sample_size = m_objCfg.Fm/2; // About twice per second, depending on CR
// Require resolution better than 2E-5
if ( r_vber->sample_size < 50000 )
{
r_vber->sample_size = 50000;
}
*/
2018-02-22 16:52:49 -05:00
// DERANDOMIZATION
p_tspackets = new leansdr::pipebuf<leansdr::tspacket>(m_objScheduler, "TS packets", BUF_PACKETS);
r_derand = new leansdr::derandomizer(m_objScheduler, *p_rtspackets, *p_tspackets);
2018-02-22 16:52:49 -05:00
// OUTPUT
2019-03-16 20:36:44 -04:00
r_videoplayer = new leansdr::datvvideoplayer<leansdr::tspacket>(m_objScheduler, *p_tspackets, m_objVideoStream);
2018-02-22 16:52:49 -05:00
2019-03-16 20:36:44 -04:00
m_blnDVBInitialized = true;
2018-02-22 16:52:49 -05:00
}
void DATVDemod::feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end, bool firstOfBurst)
2018-02-22 16:52:49 -05:00
{
(void) firstOfBurst;
2018-02-22 16:52:49 -05:00
float fltI;
float fltQ;
leansdr::cf32 objIQ;
2018-02-22 16:52:49 -05:00
//Complex objC;
fftfilt::cmplx *objRF;
int intRFOut;
2018-03-10 17:01:03 -05:00
double magSq;
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
//********** Bis repetita : Let's rock and roll buddy ! **********
2018-02-22 16:52:49 -05:00
#ifdef EXTENDED_DIRECT_SAMPLE
qint16 * ptrBuffer;
qint32 intLen;
//********** Reading direct samples **********
SampleVector::const_iterator it = begin;
intLen = it->intLen;
ptrBuffer = it->ptrBuffer;
ptrBufferToRelease = ptrBuffer;
++it;
for(qint32 intInd=0; intInd<intLen-1; intInd +=2)
{
fltI= ((qint32) (*ptrBuffer)) << 4;
ptrBuffer ++;
fltQ= ((qint32) (*ptrBuffer)) << 4;
ptrBuffer ++;
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
#else
for (SampleVector::const_iterator it = begin; it != end; ++it /* ++it **/)
{
fltI = it->real();
fltQ = it->imag();
#endif
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
//********** demodulation **********
2018-02-25 19:04:45 -05:00
if (m_blnNeedConfigUpdate)
2018-02-22 16:52:49 -05:00
{
2018-02-25 19:04:45 -05:00
m_objSettingsMutex.lock();
m_blnNeedConfigUpdate=false;
InitDATVFramework();
m_objSettingsMutex.unlock();
2018-02-22 16:52:49 -05:00
}
2018-02-25 19:04:45 -05:00
//********** iq stream ****************
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
Complex objC(fltI,fltQ);
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
objC *= m_objNCO.nextIQ();
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
intRFOut = m_objRFFilter->runFilt(objC, &objRF); // filter RF before demod
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
for (int intI = 0 ; intI < intRFOut; intI++)
{
objIQ.re = objRF->real();
objIQ.im = objRF->imag();
2018-03-10 17:01:03 -05:00
magSq = objIQ.re*objIQ.re + objIQ.im*objIQ.im;
m_objMagSqAverage(magSq);
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
objRF ++;
2018-02-22 16:52:49 -05:00
2018-02-25 19:04:45 -05:00
if (m_blnDVBInitialized
2019-03-16 20:36:44 -04:00
&& (p_rawiq_writer!=nullptr)
&& (m_objScheduler!=nullptr))
2018-02-25 19:04:45 -05:00
{
2018-02-22 16:52:49 -05:00
p_rawiq_writer->write(objIQ);
m_lngReadIQ++;
2018-02-25 19:04:45 -05:00
//Leave +1 by safety
if((m_lngReadIQ+1)>=p_rawiq_writer->writable())
{
m_objScheduler->step();
m_lngReadIQ=0;
delete p_rawiq_writer;
p_rawiq_writer = new leansdr::pipewriter<leansdr::cf32>(*p_rawiq);
2018-02-25 19:04:45 -05:00
}
2018-02-22 16:52:49 -05:00
}
2018-02-25 19:04:45 -05:00
2018-02-22 16:52:49 -05:00
}
}
}
void DATVDemod::start()
{
2019-03-19 18:12:54 -04:00
m_audioFifo.clear();
2018-02-22 16:52:49 -05:00
}
void DATVDemod::stop()
{
}
bool DATVDemod::handleMessage(const Message& cmd)
{
2018-03-29 04:03:52 -04:00
if (DownChannelizer::MsgChannelizerNotification::match(cmd))
{
DownChannelizer::MsgChannelizerNotification& objNotif = (DownChannelizer::MsgChannelizerNotification&) cmd;
qDebug() << "DATVDemod::handleMessage: MsgChannelizerNotification:"
2019-03-16 20:36:44 -04:00
<< " m_intSampleRate: " << objNotif.getSampleRate()
<< " m_intFrequencyOffset: " << objNotif.getFrequencyOffset();
applyChannelSettings(objNotif.getSampleRate(), objNotif.getFrequencyOffset());
2018-03-29 04:03:52 -04:00
return true;
}
else if (MsgConfigureChannelizer::match(cmd))
{
MsgConfigureChannelizer& cfg = (MsgConfigureChannelizer&) cmd;
m_channelizer->configure(m_channelizer->getInputMessageQueue(),
m_channelizer->getInputSampleRate(), // do not change sample rate
2018-03-29 04:03:52 -04:00
cfg.getCenterFrequency());
2018-02-22 16:52:49 -05:00
2018-03-29 04:03:52 -04:00
qDebug() << "DATVDemod::handleMessage: MsgConfigureChannelizer: sampleRate: " << m_channelizer->getInputSampleRate()
2019-03-16 20:36:44 -04:00
<< " centerFrequency: " << cfg.getCenterFrequency();
2018-02-22 16:52:49 -05:00
2018-03-29 04:03:52 -04:00
return true;
}
else if (MsgConfigureDATVDemod::match(cmd))
2018-02-25 19:04:45 -05:00
{
MsgConfigureDATVDemod& objCfg = (MsgConfigureDATVDemod&) cmd;
qDebug() << "DATVDemod::handleMessage: MsgConfigureDATVDemod";
applySettings(objCfg.getSettings(), objCfg.getForce());
2018-02-25 19:04:45 -05:00
2019-03-16 20:36:44 -04:00
return true;
2018-02-25 19:04:45 -05:00
}
else
{
return false;
}
2018-02-22 16:52:49 -05:00
}
void DATVDemod::applyChannelSettings(int inputSampleRate, int inputFrequencyOffset, bool force)
{
qDebug() << "DATVDemod::applyChannelSettings:"
<< " inputSampleRate: " << inputSampleRate
<< " inputFrequencyOffset: " << inputFrequencyOffset;
if ((m_settings.m_centerFrequency != inputFrequencyOffset) ||
(m_sampleRate != inputSampleRate) || force)
{
m_objNCO.setFreq(-(float) inputFrequencyOffset, (float) inputSampleRate);
}
if ((m_sampleRate != inputSampleRate) || force)
{
m_objSettingsMutex.lock();
//Bandpass filter shaping
Real fltLowCut = -((float) m_settings.m_rfBandwidth / 2.0) / (float) inputSampleRate;
Real fltHiCut = ((float) m_settings.m_rfBandwidth / 2.0) / (float) inputSampleRate;
m_objRFFilter->create_filter(fltLowCut, fltHiCut);
m_blnNeedConfigUpdate = true;
m_objSettingsMutex.unlock();
}
m_sampleRate = inputSampleRate;
m_settings.m_centerFrequency = inputFrequencyOffset;
}
void DATVDemod::applySettings(const DATVDemodSettings& settings, bool force)
2018-02-22 16:52:49 -05:00
{
QString msg = tr("DATVDemod::applySettings: force: %1").arg(force);
settings.debug(msg);
if (m_sampleRate == 0) {
2018-02-22 16:52:49 -05:00
return;
}
2019-03-19 18:12:54 -04:00
if ((settings.m_audioDeviceName != m_settings.m_audioDeviceName) || force)
{
AudioDeviceManager *audioDeviceManager = DSPEngine::instance()->getAudioDeviceManager();
int audioDeviceIndex = audioDeviceManager->getOutputDeviceIndex(settings.m_audioDeviceName);
2019-03-19 19:07:05 -04:00
audioDeviceManager->addAudioSink(&m_audioFifo, getInputMessageQueue(), audioDeviceIndex); // removes from current if necessary
2019-03-19 18:12:54 -04:00
// uint32_t audioSampleRate = audioDeviceManager->getOutputSampleRate(audioDeviceIndex);
// if (m_audioSampleRate != audioSampleRate) {
// applyAudioSampleRate(audioSampleRate);
// }
}
2019-03-22 03:05:01 -04:00
if ((settings.m_audioVolume) != (m_settings.m_audioVolume) || force)
{
if (m_objRegisteredVideoRender) {
m_objRegisteredVideoRender->setAudioVolume(settings.m_audioVolume);
}
}
if ((settings.m_audioMute) != (m_settings.m_audioMute) || force)
{
if (m_objRegisteredVideoRender) {
m_objRegisteredVideoRender->setAudioMute(settings.m_audioMute);
}
}
if ((settings.m_videoMute) != (m_settings.m_videoMute) || force)
{
if (m_objRegisteredVideoRender) {
m_objRegisteredVideoRender->setVideoMute(settings.m_videoMute);
}
}
if (m_settings.isDifferent(settings) || force)
{
m_objSettingsMutex.lock();
if ((m_settings.m_rfBandwidth != settings.m_rfBandwidth)
|| force)
{
//Bandpass filter shaping
Real fltLowCut = -((float) settings.m_rfBandwidth / 2.0) / (float) m_sampleRate;
Real fltHiCut = ((float) settings.m_rfBandwidth / 2.0) / (float) m_sampleRate;
m_objRFFilter->create_filter(fltLowCut, fltHiCut);
}
if ((m_settings.m_centerFrequency != settings.m_centerFrequency)
|| force)
{
m_objNCO.setFreq(-(float) settings.m_centerFrequency, (float) m_sampleRate);
}
m_objSettingsMutex.unlock();
m_blnNeedConfigUpdate = true;
}
m_settings = settings;
2018-02-22 16:52:49 -05:00
}
int DATVDemod::GetSampleRate()
{
return m_sampleRate;
2018-02-22 16:52:49 -05:00
}