2016-06-19 03:56:49 -04:00
|
|
|
///////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Copyright (C) 2016 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 //
|
|
|
|
// //
|
|
|
|
// 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 PLUGINS_SAMPLESOURCE_SDRDAEMONFEC_SDRDAEMONFECBUFFER_H_
|
|
|
|
#define PLUGINS_SAMPLESOURCE_SDRDAEMONFEC_SDRDAEMONFECBUFFER_H_
|
|
|
|
|
|
|
|
#include <QString>
|
2016-06-22 18:58:07 -04:00
|
|
|
#include <QDebug>
|
2016-06-19 03:56:49 -04:00
|
|
|
#include <cstdlib>
|
2016-06-19 18:45:24 -04:00
|
|
|
#include "cm256.h"
|
|
|
|
#include "util/movingaverage.h"
|
2016-06-19 03:56:49 -04:00
|
|
|
|
2016-06-19 18:45:24 -04:00
|
|
|
|
|
|
|
#define SDRDAEMONFEC_UDPSIZE 512 // UDP payload size
|
|
|
|
#define SDRDAEMONFEC_NBORIGINALBLOCKS 128 // number of sample blocks per frame excluding FEC blocks
|
2016-06-21 15:49:27 -04:00
|
|
|
#define SDRDAEMONFEC_NBDECODERSLOTS 16 // power of two sub multiple of uint16_t size. A too large one is superfluous.
|
2016-06-19 03:56:49 -04:00
|
|
|
|
|
|
|
class SDRdaemonFECBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
#pragma pack(push, 1)
|
2016-06-19 18:45:24 -04:00
|
|
|
struct MetaDataFEC
|
|
|
|
{
|
|
|
|
uint32_t m_centerFrequency; //!< 4 center frequency in kHz
|
|
|
|
uint32_t m_sampleRate; //!< 8 sample rate in Hz
|
|
|
|
uint8_t m_sampleBytes; //!< 9 MSB(4): indicators, LSB(4) number of bytes per sample
|
|
|
|
uint8_t m_sampleBits; //!< 10 number of effective bits per sample
|
|
|
|
uint8_t m_nbOriginalBlocks; //!< 11 number of blocks with original (protected) data
|
|
|
|
uint8_t m_nbFECBlocks; //!< 12 number of blocks carrying FEC
|
|
|
|
uint32_t m_tv_sec; //!< 16 seconds of timestamp at start time of super-frame processing
|
|
|
|
uint32_t m_tv_usec; //!< 20 microseconds of timestamp at start time of super-frame processing
|
|
|
|
|
|
|
|
bool operator==(const MetaDataFEC& rhs)
|
|
|
|
{
|
|
|
|
return (memcmp((const void *) this, (const void *) &rhs, 12) == 0); // Only the 12 first bytes are relevant
|
|
|
|
}
|
|
|
|
|
|
|
|
void init()
|
|
|
|
{
|
|
|
|
memset((void *) this, 0, sizeof(MetaDataFEC));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Sample
|
|
|
|
{
|
|
|
|
uint16_t i;
|
|
|
|
uint16_t q;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Header
|
|
|
|
{
|
|
|
|
uint16_t frameIndex;
|
|
|
|
uint8_t blockIndex;
|
|
|
|
uint8_t filler;
|
|
|
|
};
|
|
|
|
|
|
|
|
static const int samplesPerBlock = (SDRDAEMONFEC_UDPSIZE - sizeof(Header)) / sizeof(Sample);
|
|
|
|
|
|
|
|
struct ProtectedBlock
|
|
|
|
{
|
|
|
|
Sample samples[samplesPerBlock];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct SuperBlock
|
|
|
|
{
|
|
|
|
Header header;
|
|
|
|
ProtectedBlock protectedBlock;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ProtectedBlockZero
|
|
|
|
{
|
|
|
|
MetaDataFEC m_metaData;
|
2016-07-05 18:43:38 -04:00
|
|
|
uint8_t m_filler[SDRDAEMONFEC_UDPSIZE - sizeof(Header) - sizeof(MetaDataFEC)]; // complete for a 512 byte block
|
2016-06-19 18:45:24 -04:00
|
|
|
};
|
2016-06-19 03:56:49 -04:00
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
SDRdaemonFECBuffer(uint32_t throttlems);
|
|
|
|
~SDRdaemonFECBuffer();
|
|
|
|
|
2016-06-19 18:45:24 -04:00
|
|
|
// R/W operations
|
2016-06-19 03:56:49 -04:00
|
|
|
void writeData(char *array, uint32_t length); //!< Write data into buffer.
|
2016-07-06 20:36:44 -04:00
|
|
|
void writeData0(char *array, uint32_t length); //!< Write data into buffer.
|
2016-06-19 18:45:24 -04:00
|
|
|
uint8_t *readData(int32_t length); //!< Read data from buffer
|
|
|
|
|
|
|
|
// meta data
|
|
|
|
const MetaDataFEC& getCurrentMeta() const { return m_currentMeta; }
|
|
|
|
|
|
|
|
// stats
|
|
|
|
int getCurNbBlocks() const { return m_curNbBlocks; }
|
|
|
|
int getCurNbRecovery() const { return m_curNbRecovery; }
|
|
|
|
float getAvgNbBlocks() const { return m_avgNbBlocks; }
|
|
|
|
float getAvgNbRecovery() const { return m_avgNbRecovery; }
|
|
|
|
|
|
|
|
|
|
|
|
float getBufferLengthInSecs() const { return m_bufferLenSec; }
|
2016-06-19 03:56:49 -04:00
|
|
|
|
|
|
|
/** Get buffer gauge value in % of buffer size ([-50:50])
|
|
|
|
* [-50:0] : write leads or read lags
|
|
|
|
* [0:50] : read leads or write lags
|
|
|
|
*/
|
|
|
|
inline int32_t getBufferGauge() const
|
|
|
|
{
|
2016-06-19 18:45:24 -04:00
|
|
|
if (m_framesNbBytes)
|
2016-06-19 03:56:49 -04:00
|
|
|
{
|
2016-06-19 18:45:24 -04:00
|
|
|
int32_t val = (m_wrDeltaEstimate * 100) / (int32_t) m_framesNbBytes;
|
2016-06-22 18:58:07 -04:00
|
|
|
int32_t ret = val < 0 ? -val - 50 : 50 -val;
|
|
|
|
int32_t rp = (m_readIndex * 100) / (int32_t) m_framesNbBytes;
|
|
|
|
//qDebug() << "getBufferLengthInSecs: " << val << ":" << ret << ":" << rp;
|
2016-06-21 19:10:58 -04:00
|
|
|
// conversion: [-100:-50[ : read leads (+) / [-50:0[ : read lags (-) / [0:50[ : read leads (+) / [50:100{ : read lags (-)
|
2016-06-22 18:58:07 -04:00
|
|
|
return ret;
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-06-21 19:10:58 -04:00
|
|
|
return 0; // default position
|
2016-06-19 03:56:49 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 19:32:32 -04:00
|
|
|
static const int m_udpPayloadSize = SDRDAEMONFEC_UDPSIZE;
|
|
|
|
static const int m_nbOriginalBlocks = SDRDAEMONFEC_NBORIGINALBLOCKS;
|
2016-06-19 03:56:49 -04:00
|
|
|
static const int m_sampleSize;
|
|
|
|
static const int m_iqSampleSize;
|
|
|
|
|
|
|
|
private:
|
2016-06-19 18:45:24 -04:00
|
|
|
static const int nbDecoderSlots = SDRDAEMONFEC_NBDECODERSLOTS;
|
2016-06-19 03:56:49 -04:00
|
|
|
|
2016-06-19 18:45:24 -04:00
|
|
|
#pragma pack(push, 1)
|
|
|
|
struct BufferFrame
|
|
|
|
{
|
2016-06-19 19:32:32 -04:00
|
|
|
ProtectedBlock m_blocks[m_nbOriginalBlocks - 1];
|
2016-06-19 18:45:24 -04:00
|
|
|
};
|
|
|
|
#pragma pack(pop)
|
|
|
|
|
|
|
|
struct DecoderSlot
|
|
|
|
{
|
|
|
|
ProtectedBlockZero m_blockZero;
|
2016-06-19 19:32:32 -04:00
|
|
|
ProtectedBlock* m_originalBlockPtrs[m_nbOriginalBlocks];
|
|
|
|
ProtectedBlock m_recoveryBlocks[m_nbOriginalBlocks]; // max size
|
|
|
|
cm256_block m_cm256DescriptorBlocks[m_nbOriginalBlocks];
|
2016-06-19 18:45:24 -04:00
|
|
|
int m_blockCount; //!< total number of blocks received for this frame
|
|
|
|
int m_recoveryCount; //!< number of recovery blocks received
|
|
|
|
bool m_decoded; //!< true if decoded
|
2016-06-22 18:58:07 -04:00
|
|
|
bool m_metaRetrieved;
|
2016-06-19 18:45:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
MetaDataFEC m_currentMeta; //!< Stored current meta data
|
|
|
|
cm256_encoder_params m_paramsCM256; //!< CM256 decoder parameters block
|
|
|
|
DecoderSlot m_decoderSlots[nbDecoderSlots]; //!< CM256 decoding control/buffer slots
|
|
|
|
BufferFrame m_frames[nbDecoderSlots]; //!< Samples buffer
|
|
|
|
int m_framesNbBytes; //!< Number of bytes in samples buffer
|
2016-07-05 18:43:38 -04:00
|
|
|
int m_decoderIndexHead; //!< index of the current head frame slot in decoding slots
|
2016-06-19 18:45:24 -04:00
|
|
|
int m_frameHead; //!< index of the current head frame sent
|
|
|
|
int m_curNbBlocks; //!< (stats) instantaneous number of blocks received
|
|
|
|
int m_curNbRecovery; //!< (stats) instantaneous number of recovery blocks used
|
|
|
|
MovingAverage<int, int, 10> m_avgNbBlocks; //!< (stats) average number of blocks received
|
|
|
|
MovingAverage<int, int, 10> m_avgNbRecovery; //!< (stats) average number of recovery blocks used
|
|
|
|
int m_readIndex; //!< current byte read index in frames buffer
|
|
|
|
int m_wrDeltaEstimate; //!< Sampled estimate of write to read indexes difference
|
2016-06-19 03:56:49 -04:00
|
|
|
|
|
|
|
uint32_t m_throttlemsNominal; //!< Initial throttle in ms
|
2016-06-19 18:45:24 -04:00
|
|
|
uint8_t* m_readBuffer; //!< Read buffer to hold samples when looping back to beginning of raw buffer
|
|
|
|
uint32_t m_readSize; //!< Read buffer size
|
|
|
|
|
|
|
|
float m_bufferLenSec;
|
|
|
|
|
|
|
|
void initDecoderSlotsAddresses();
|
|
|
|
void initDecodeAllSlots();
|
|
|
|
void initReadIndex();
|
2016-07-05 20:54:01 -04:00
|
|
|
void checkSlotData(int slotIndex);
|
2016-06-19 18:45:24 -04:00
|
|
|
void initDecodeSlot(int slotIndex);
|
|
|
|
|
|
|
|
static void printMeta(const QString& header, MetaDataFEC *metaData);
|
2016-06-19 03:56:49 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif /* PLUGINS_SAMPLESOURCE_SDRDAEMONFEC_SDRDAEMONFECBUFFER_H_ */
|