2019-12-01 09:13:35 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2019 Edouard Griffiths, F4EXB //
|
|
|
|
// //
|
|
|
|
// 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 //
|
|
|
|
// (at your option) any later version. //
|
|
|
|
// //
|
|
|
|
// 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/>. //
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef INCLUDE_ATVDEMODSINK_H
|
|
|
|
#define INCLUDE_ATVDEMODSINK_H
|
|
|
|
|
|
|
|
#include <QElapsedTimer>
|
|
|
|
#include <vector>
|
2020-08-04 10:02:06 -04:00
|
|
|
#include <memory>
|
2019-12-01 09:13:35 -05:00
|
|
|
|
|
|
|
#include "dsp/channelsamplesink.h"
|
|
|
|
#include "dsp/nco.h"
|
|
|
|
#include "dsp/interpolator.h"
|
|
|
|
#include "dsp/fftfilt.h"
|
|
|
|
#include "dsp/agc.h"
|
|
|
|
#include "dsp/phaselock.h"
|
|
|
|
#include "dsp/recursivefilters.h"
|
|
|
|
#include "dsp/phasediscri.h"
|
|
|
|
#include "util/movingaverage.h"
|
2020-08-03 12:31:47 -04:00
|
|
|
#include "gui/tvscreenanalog.h"
|
2019-12-01 09:13:35 -05:00
|
|
|
|
|
|
|
#include "atvdemodsettings.h"
|
|
|
|
|
2021-05-29 14:09:28 -04:00
|
|
|
class ScopeVis;
|
|
|
|
|
2019-12-01 09:13:35 -05:00
|
|
|
class ATVDemodSink : public ChannelSampleSink {
|
|
|
|
public:
|
|
|
|
ATVDemodSink();
|
|
|
|
~ATVDemodSink();
|
|
|
|
|
|
|
|
virtual void feed(const SampleVector::const_iterator& begin, const SampleVector::const_iterator& end);
|
|
|
|
|
2021-05-29 14:09:28 -04:00
|
|
|
void setScopeSink(ScopeVis* scopeSink) { m_scopeSink = scopeSink; }
|
2020-12-10 00:50:15 -05:00
|
|
|
void setTVScreen(TVScreenAnalog *tvScreen) //!< set by the GUI
|
|
|
|
{
|
|
|
|
m_registeredTVScreen = tvScreen;
|
|
|
|
m_tvScreenBuffer = m_registeredTVScreen->getBackBuffer();
|
|
|
|
}
|
2020-06-29 02:46:09 -04:00
|
|
|
double getMagSq() const { return m_magSqAverage; } //!< Beware this is scaled to 2^30
|
2019-12-01 09:13:35 -05:00
|
|
|
bool getBFOLocked();
|
|
|
|
void setVideoTabIndex(int videoTabIndex) { m_videoTabIndex = videoTabIndex; }
|
|
|
|
|
|
|
|
void applyChannelSettings(int channelSampleRate, int channelFrequencyOffset, bool force = false);
|
|
|
|
void applySettings(const ATVDemodSettings& settings, bool force = false);
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct ATVConfigPrivate
|
|
|
|
{
|
|
|
|
int m_intTVSampleRate;
|
|
|
|
int m_intNumberSamplePerLine;
|
|
|
|
|
|
|
|
ATVConfigPrivate() :
|
|
|
|
m_intTVSampleRate(0),
|
|
|
|
m_intNumberSamplePerLine(0)
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exponential average using integers and alpha as the inverse of a power of two
|
|
|
|
*/
|
|
|
|
class AvgExpInt
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AvgExpInt(int log2Alpha) : m_log2Alpha(log2Alpha), m_m1(0), m_start(true) {}
|
|
|
|
void reset() { m_start = true; }
|
|
|
|
|
|
|
|
int run(int m0)
|
|
|
|
{
|
|
|
|
if (m_start)
|
|
|
|
{
|
|
|
|
m_m1 = m0;
|
|
|
|
m_start = false;
|
|
|
|
return m0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
m_m1 = m0 + m_m1 - (m_m1>>m_log2Alpha);
|
|
|
|
return m_m1>>m_log2Alpha;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int m_log2Alpha;
|
|
|
|
int m_m1;
|
|
|
|
bool m_start;
|
|
|
|
};
|
|
|
|
|
|
|
|
int m_channelSampleRate;
|
|
|
|
int m_channelFrequencyOffset;
|
2020-08-18 10:23:40 -04:00
|
|
|
int m_samplesPerLine; //!< number of samples per complete line (includes sync signals) - adusted value
|
|
|
|
float m_samplesPerLineFrac; //!< number of samples per complete line (includes sync signals), fractional part
|
|
|
|
ATVDemodSettings m_settings;
|
2019-12-01 09:13:35 -05:00
|
|
|
int m_videoTabIndex;
|
|
|
|
|
|
|
|
//*************** SCOPE ***************
|
|
|
|
|
2021-05-29 14:09:28 -04:00
|
|
|
ScopeVis* m_scopeSink;
|
2019-12-01 09:13:35 -05:00
|
|
|
SampleVector m_scopeSampleBuffer;
|
|
|
|
|
|
|
|
//*************** ATV PARAMETERS ***************
|
2020-08-03 12:31:47 -04:00
|
|
|
TVScreenAnalog *m_registeredTVScreen;
|
2020-12-10 01:28:55 -05:00
|
|
|
TVScreenAnalogBuffer *m_tvScreenBuffer;
|
2019-12-01 09:13:35 -05:00
|
|
|
|
|
|
|
//int m_intNumberSamplePerLine;
|
2020-08-18 10:23:40 -04:00
|
|
|
int m_numberSamplesPerHTop; //!< number of samples per horizontal synchronization pulse (pulse in ultra-black) - integer value
|
2019-12-01 09:13:35 -05:00
|
|
|
int m_numberOfBlackLines; //!< this is the total number of lines not part of the image and is used for vertical screen size
|
2020-07-19 07:07:04 -04:00
|
|
|
int m_firstVisibleLine;
|
|
|
|
|
|
|
|
int m_fieldDetectStartPos;
|
|
|
|
int m_fieldDetectEndPos;
|
|
|
|
int m_vSyncDetectStartPos;
|
|
|
|
int m_vSyncDetectEndPos;
|
|
|
|
|
|
|
|
int m_vSyncDetectThreshold;
|
|
|
|
int m_fieldDetectThreshold1;
|
|
|
|
int m_fieldDetectThreshold2;
|
|
|
|
|
|
|
|
int m_numberOfVSyncLines;
|
2019-12-01 09:13:35 -05:00
|
|
|
int m_numberSamplesPerLineSignals; //!< number of samples in the non image part of the line (signals = front porch + pulse + back porch)
|
|
|
|
int m_numberSamplesPerHSync; //!< number of samples per horizontal synchronization pattern (pulse + back porch)
|
|
|
|
int m_numberSamplesHSyncCrop; //!< number of samples to crop from start of horizontal synchronization
|
|
|
|
bool m_interleaved; //!< interleaved image
|
|
|
|
|
|
|
|
//*************** PROCESSING ***************
|
|
|
|
|
2020-07-19 07:07:04 -04:00
|
|
|
int m_fieldIndex;
|
2019-12-01 09:13:35 -05:00
|
|
|
int m_synchroSamples;
|
|
|
|
|
2020-07-19 07:07:04 -04:00
|
|
|
int m_fieldDetectSampleCount;
|
|
|
|
int m_vSyncDetectSampleCount;
|
2019-12-01 09:13:35 -05:00
|
|
|
|
|
|
|
float m_effMin;
|
|
|
|
float m_effMax;
|
|
|
|
|
|
|
|
float m_ampMin;
|
|
|
|
float m_ampMax;
|
|
|
|
float m_ampDelta; //!< calculated amplitude of HSync pulse (should be ~0.3f)
|
|
|
|
|
|
|
|
float m_fltBufferI[6];
|
|
|
|
float m_fltBufferQ[6];
|
|
|
|
|
2020-08-05 12:36:07 -04:00
|
|
|
int m_amSampleIndex;
|
|
|
|
|
|
|
|
int m_sampleOffset; // assumed (averaged) sample offset from the start of horizontal sync pulse
|
|
|
|
float m_sampleOffsetFrac; // sample offset, fractional part
|
|
|
|
int m_sampleOffsetDetected; // detected sample offset from the start of horizontal sync pulse
|
2019-12-01 09:13:35 -05:00
|
|
|
int m_lineIndex;
|
|
|
|
|
2020-08-05 12:36:07 -04:00
|
|
|
float m_hSyncShift;
|
2020-07-19 07:07:04 -04:00
|
|
|
int m_hSyncErrorCount;
|
|
|
|
|
|
|
|
float prevSample;
|
2019-12-01 09:13:35 -05:00
|
|
|
int m_avgColIndex;
|
|
|
|
|
|
|
|
SampleVector m_sampleBuffer;
|
|
|
|
|
2020-07-20 02:34:11 -04:00
|
|
|
float m_sampleRangeCorrection;
|
|
|
|
|
2019-12-01 09:13:35 -05:00
|
|
|
//*************** RF ***************
|
|
|
|
|
2020-06-29 02:46:09 -04:00
|
|
|
MovingAverageUtil<double, double, 32> m_magSqAverage;
|
2020-06-29 19:15:12 -04:00
|
|
|
MovingAverageUtilVar<double, double> m_ampAverage;
|
2019-12-01 09:13:35 -05:00
|
|
|
|
|
|
|
NCO m_nco;
|
|
|
|
SimplePhaseLock m_bfoPLL;
|
|
|
|
SecondOrderRecursiveFilter m_bfoFilter;
|
|
|
|
|
|
|
|
// Used for vestigial SSB with asymmetrical filtering (needs double sideband scheme)
|
|
|
|
fftfilt* m_DSBFilter;
|
|
|
|
Complex* m_DSBFilterBuffer;
|
|
|
|
int m_DSBFilterBufferIndex;
|
|
|
|
static const int m_ssbFftLen;
|
|
|
|
|
|
|
|
// Used for FM
|
|
|
|
PhaseDiscriminators m_objPhaseDiscri;
|
|
|
|
|
|
|
|
void demod(Complex& c);
|
2020-08-18 10:23:40 -04:00
|
|
|
void applyStandard(int sampleRate, ATVDemodSettings::ATVStd atvStd, float lineDuration);
|
2019-12-01 09:13:35 -05:00
|
|
|
|
2020-07-22 02:48:23 -04:00
|
|
|
inline void processSample(float& sample, int& sampleVideo)
|
2019-12-01 09:13:35 -05:00
|
|
|
{
|
|
|
|
// Filling pixel on the current line - reference index 0 at start of sync pulse
|
2020-08-10 08:54:34 -04:00
|
|
|
m_tvScreenBuffer->setSampleValue(m_sampleOffset - m_numberSamplesPerHSync, sampleVideo);
|
2019-12-01 09:13:35 -05:00
|
|
|
|
2020-07-19 07:07:04 -04:00
|
|
|
if (m_settings.m_hSync)
|
2019-12-01 09:13:35 -05:00
|
|
|
{
|
2020-07-19 07:07:04 -04:00
|
|
|
// Horizontal Synchro detection
|
|
|
|
if ((prevSample >= m_settings.m_levelSynchroTop &&
|
|
|
|
sample < m_settings.m_levelSynchroTop) // horizontal synchro detected
|
2020-08-18 10:23:40 -04:00
|
|
|
&& (m_sampleOffsetDetected > m_samplesPerLine - m_numberSamplesPerHTop))
|
2020-07-19 07:07:04 -04:00
|
|
|
{
|
2020-08-05 12:36:07 -04:00
|
|
|
float sampleOffsetDetectedFrac =
|
2020-07-19 07:07:04 -04:00
|
|
|
(sample - m_settings.m_levelSynchroTop) / (prevSample - sample);
|
2020-08-05 12:36:07 -04:00
|
|
|
float hSyncShift = -m_sampleOffset - m_sampleOffsetFrac - sampleOffsetDetectedFrac;
|
2020-07-19 07:07:04 -04:00
|
|
|
if (hSyncShift > m_samplesPerLine / 2)
|
2020-08-05 12:36:07 -04:00
|
|
|
hSyncShift -= m_samplesPerLine;
|
2020-07-19 07:07:04 -04:00
|
|
|
else if (hSyncShift < -m_samplesPerLine / 2)
|
2020-08-05 12:36:07 -04:00
|
|
|
hSyncShift += m_samplesPerLine;
|
2020-07-19 07:07:04 -04:00
|
|
|
|
2020-08-18 10:23:40 -04:00
|
|
|
if (fabs(hSyncShift) > m_numberSamplesPerHTop)
|
2020-07-19 07:07:04 -04:00
|
|
|
{
|
|
|
|
m_hSyncErrorCount++;
|
2020-08-05 12:36:07 -04:00
|
|
|
if (m_hSyncErrorCount >= 4)
|
2020-07-19 07:07:04 -04:00
|
|
|
{
|
|
|
|
// Fast sync: shift is too large, needs to be fixed ASAP
|
2020-08-05 12:36:07 -04:00
|
|
|
m_hSyncShift = hSyncShift;
|
2020-07-19 07:07:04 -04:00
|
|
|
m_hSyncErrorCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-08-05 12:36:07 -04:00
|
|
|
// Slow sync: slight adjustment is needed
|
|
|
|
m_hSyncShift = hSyncShift * 0.2f;
|
2020-07-19 07:07:04 -04:00
|
|
|
m_hSyncErrorCount = 0;
|
|
|
|
}
|
2020-08-05 12:36:07 -04:00
|
|
|
m_sampleOffsetDetected = 0;
|
2020-07-19 07:07:04 -04:00
|
|
|
}
|
|
|
|
else
|
2020-08-05 12:36:07 -04:00
|
|
|
m_sampleOffsetDetected++;
|
2019-12-01 09:13:35 -05:00
|
|
|
}
|
2020-08-05 12:36:07 -04:00
|
|
|
m_sampleOffset++;
|
2019-12-01 09:13:35 -05:00
|
|
|
|
2020-07-19 07:07:04 -04:00
|
|
|
if (m_settings.m_vSync)
|
2019-12-01 09:13:35 -05:00
|
|
|
{
|
2020-08-05 12:36:07 -04:00
|
|
|
if (m_sampleOffset > m_fieldDetectStartPos && m_sampleOffset < m_fieldDetectEndPos)
|
2020-07-19 07:07:04 -04:00
|
|
|
m_fieldDetectSampleCount += sample < m_settings.m_levelSynchroTop;
|
2020-08-05 12:36:07 -04:00
|
|
|
if (m_sampleOffset > m_vSyncDetectStartPos && m_sampleOffset < m_vSyncDetectEndPos)
|
2020-07-19 07:07:04 -04:00
|
|
|
m_vSyncDetectSampleCount += sample < m_settings.m_levelSynchroTop;
|
2019-12-01 09:13:35 -05:00
|
|
|
}
|
|
|
|
|
2020-07-19 07:07:04 -04:00
|
|
|
// end of line
|
2020-08-05 12:36:07 -04:00
|
|
|
if (m_sampleOffset >= m_samplesPerLine)
|
2020-07-19 07:07:04 -04:00
|
|
|
{
|
2020-08-18 14:08:46 -04:00
|
|
|
float sampleOffsetFloat = m_hSyncShift + m_sampleOffsetFrac - m_samplesPerLineFrac;
|
|
|
|
m_sampleOffset = sampleOffsetFloat;
|
|
|
|
m_sampleOffsetFrac = sampleOffsetFloat - m_sampleOffset;
|
2020-08-05 12:36:07 -04:00
|
|
|
m_hSyncShift = 0.0f;
|
|
|
|
|
|
|
|
m_lineIndex++;
|
|
|
|
if (m_settings.m_atvStd == ATVDemodSettings::ATVStdHSkip) {
|
2020-07-22 02:48:23 -04:00
|
|
|
processEOLHSkip();
|
|
|
|
} else {
|
|
|
|
processEOLClassic();
|
2019-12-01 09:13:35 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 07:07:04 -04:00
|
|
|
|
|
|
|
prevSample = sample;
|
2019-12-01 09:13:35 -05:00
|
|
|
}
|
2020-07-22 01:52:21 -04:00
|
|
|
|
2020-07-22 02:48:23 -04:00
|
|
|
// Standard vertical sync
|
|
|
|
inline void processEOLClassic()
|
2020-07-22 01:52:21 -04:00
|
|
|
{
|
2020-07-22 02:48:23 -04:00
|
|
|
if (m_lineIndex == m_numberOfVSyncLines + 3 && m_fieldIndex == 0)
|
2020-07-22 01:52:21 -04:00
|
|
|
{
|
2020-08-10 08:54:34 -04:00
|
|
|
m_tvScreenBuffer = m_registeredTVScreen->swapBuffers();
|
2020-07-22 02:48:23 -04:00
|
|
|
}
|
2020-07-22 01:52:21 -04:00
|
|
|
|
2020-07-22 02:48:23 -04:00
|
|
|
if (m_vSyncDetectSampleCount > m_vSyncDetectThreshold &&
|
|
|
|
(m_lineIndex < 3 || m_lineIndex > m_numberOfVSyncLines + 1) && m_settings.m_vSync)
|
|
|
|
{
|
|
|
|
if (m_interleaved)
|
|
|
|
{
|
|
|
|
if (m_fieldDetectSampleCount > m_fieldDetectThreshold1)
|
|
|
|
m_fieldIndex = 0;
|
|
|
|
else if (m_fieldDetectSampleCount < m_fieldDetectThreshold2)
|
|
|
|
m_fieldIndex = 1;
|
2020-07-22 01:52:21 -04:00
|
|
|
}
|
2020-07-22 02:48:23 -04:00
|
|
|
m_lineIndex = 2;
|
2020-07-22 01:52:21 -04:00
|
|
|
}
|
2020-07-22 02:48:23 -04:00
|
|
|
m_fieldDetectSampleCount = 0;
|
|
|
|
m_vSyncDetectSampleCount = 0;
|
|
|
|
|
|
|
|
if (m_lineIndex > m_settings.m_nbLines / 2 + m_fieldIndex && m_interleaved)
|
2020-07-22 01:52:21 -04:00
|
|
|
{
|
2020-07-22 02:48:23 -04:00
|
|
|
m_lineIndex = 1;
|
|
|
|
m_fieldIndex = 1 - m_fieldIndex;
|
|
|
|
}
|
|
|
|
else if (m_lineIndex > m_settings.m_nbLines && !m_interleaved)
|
|
|
|
{
|
|
|
|
m_lineIndex = 1;
|
|
|
|
m_fieldIndex = 0;
|
2020-07-22 01:52:21 -04:00
|
|
|
}
|
|
|
|
|
2020-07-22 02:48:23 -04:00
|
|
|
int rowIndex = m_lineIndex - m_firstVisibleLine;
|
|
|
|
if (m_interleaved)
|
|
|
|
rowIndex = rowIndex * 2 - m_fieldIndex;
|
2020-08-03 12:31:47 -04:00
|
|
|
|
2020-08-10 08:54:34 -04:00
|
|
|
m_tvScreenBuffer->selectRow(rowIndex, m_sampleOffsetFrac);
|
2020-08-03 12:31:47 -04:00
|
|
|
}
|
2020-07-22 01:52:21 -04:00
|
|
|
|
2020-07-22 02:48:23 -04:00
|
|
|
// Vertical sync is obtained by skipping horizontal sync on the line that triggers vertical sync (new frame)
|
|
|
|
inline void processEOLHSkip()
|
|
|
|
{
|
2020-08-05 12:36:07 -04:00
|
|
|
if ((m_sampleOffsetDetected > (3 * m_samplesPerLine) / 2) // Vertical sync is first horizontal sync after skip (count at least 1.5 line length)
|
2020-07-22 02:48:23 -04:00
|
|
|
|| (!m_settings.m_vSync && (m_lineIndex >= m_settings.m_nbLines))) // Vsync ignored and reached nominal number of lines per frame
|
2020-07-22 01:52:21 -04:00
|
|
|
{
|
2020-08-10 08:54:34 -04:00
|
|
|
m_tvScreenBuffer = m_registeredTVScreen->swapBuffers();
|
2020-08-05 12:36:07 -04:00
|
|
|
m_lineIndex = 0;
|
2020-07-22 01:52:21 -04:00
|
|
|
}
|
|
|
|
|
2020-08-10 08:54:34 -04:00
|
|
|
m_tvScreenBuffer->selectRow(m_lineIndex, m_sampleOffsetFrac);
|
2020-07-22 01:52:21 -04:00
|
|
|
}
|
2019-12-01 09:13:35 -05:00
|
|
|
};
|
|
|
|
|
2020-12-10 00:50:15 -05:00
|
|
|
#endif // INCLUDE_ATVDEMODSINK_H
|