1
0
mirror of https://github.com/f4exb/sdrangel.git synced 2024-11-14 04:11:48 -05:00
sdrangel/sdrbase/dsp/samplemififo.cpp

381 lines
11 KiB
C++
Raw Normal View History

2019-09-24 18:35:02 -04: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/>. //
///////////////////////////////////////////////////////////////////////////////////
#include "samplemififo.h"
void SampleMIFifo::init(unsigned int nbStreams, unsigned int size)
2019-09-24 18:35:02 -04:00
{
2021-04-26 13:33:02 -04:00
QMutexLocker mutexLocker(&m_mutex);
m_nbStreams = nbStreams;
m_size = size;
m_fill = 0;
m_head = 0;
2019-10-02 19:19:43 -04:00
m_data.resize(nbStreams);
m_vFill.clear();
m_vHead.clear();
for (unsigned int stream = 0; stream < nbStreams; stream++)
2019-09-24 18:35:02 -04:00
{
m_data[stream].resize(size);
2019-10-02 19:19:43 -04:00
m_vFill.push_back(0);
m_vHead.push_back(0);
2019-09-24 18:35:02 -04:00
}
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::reset()
2019-09-24 18:35:02 -04:00
{
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
m_fill = 0;
m_head = 0;
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_nbStreams; stream++)
{
m_vFill[stream] = 0;
m_vHead[stream] = 0;
}
}
2019-09-24 18:35:02 -04:00
2019-10-02 19:19:43 -04:00
SampleMIFifo::SampleMIFifo(QObject *parent) :
QObject(parent),
m_nbStreams(0),
m_size(0),
m_fill(0),
m_head(0)
{
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
SampleMIFifo::SampleMIFifo(unsigned int nbStreams, unsigned int size, QObject *parent) :
QObject(parent)
2019-09-24 18:35:02 -04:00
{
2019-10-02 19:19:43 -04:00
init(nbStreams, size);
}
2019-09-24 18:35:02 -04:00
2019-10-06 03:53:32 -04:00
SampleMIFifo::~SampleMIFifo()
{
qDebug("SampleMIFifo::~SampleMIFifo: m_fill: %u", m_fill);
qDebug("SampleMIFifo::~SampleMIFifo: m_head: %u", m_head);
for (unsigned int stream = 0; stream < m_data.size(); stream++)
{
qDebug("SampleMIFifo::~SampleMIFifo: m_data[%u] size: %lu", stream, m_data[stream].size());
qDebug("SampleMIFifo::~SampleMIFifo: m_vFill[%u] %u", stream, m_vFill[stream]);
qDebug("SampleMIFifo::~SampleMIFifo: m_vHead[%u] %u", stream, m_vHead[stream]);
}
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::writeSync(const quint8* data, unsigned int count)
{
QMutexLocker mutexLocker(&m_mutex);
unsigned int spaceLeft = m_size - m_fill;
unsigned int size = count / sizeof(Sample);
2019-09-24 18:35:02 -04:00
2019-10-06 03:53:32 -04:00
if (size > m_size)
{
qWarning("SampleMIFifo::writeSync: input size %u greater that FIFO size %u: truncating input", size, m_size);
size = m_size;
count = m_size * sizeof(Sample);
}
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_data.size(); stream++)
2019-09-24 18:35:02 -04:00
{
2019-10-06 03:53:32 -04:00
if (size <= spaceLeft)
2019-09-24 18:35:02 -04:00
{
2019-10-02 19:19:43 -04:00
std::copy(&data[stream*count], &data[stream*count] + count, m_data[stream].begin() + m_fill);
m_fill += size;
}
2019-09-24 18:35:02 -04:00
else
{
2019-10-02 19:19:43 -04:00
unsigned int remaining = size - spaceLeft;
unsigned int bytesLeft = spaceLeft*sizeof(Sample);
std::copy(&data[stream*count], &data[stream*count] + bytesLeft, m_data[stream].begin() + m_fill);
std::copy(&data[stream*count] + bytesLeft, &data[stream*count] + count, m_data[stream].begin());
m_fill = remaining;
2019-09-24 18:35:02 -04:00
}
}
2019-10-02 19:19:43 -04:00
emit dataSyncReady();
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::writeSync(const std::vector<SampleVector::const_iterator>& vbegin, unsigned int size)
2019-09-24 18:35:02 -04:00
{
2019-10-02 19:19:43 -04:00
if ((m_data.size() == 0) || (m_data.size() != vbegin.size())) {
return;
}
2019-09-24 18:35:02 -04:00
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
unsigned int spaceLeft = m_size - m_fill;
2019-10-06 03:53:32 -04:00
if (size > m_size)
{
qWarning("SampleMIFifo::writeSync: input size %u greater that FIFO size %u: truncating input", size, m_size);
size = m_size;
}
if (size <= spaceLeft)
{
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_data.size(); stream++) {
std::copy(vbegin[stream], vbegin[stream] + size, m_data[stream].begin() + m_fill);
}
m_fill += size;
}
else
{
2019-10-02 19:19:43 -04:00
unsigned int remaining = size - spaceLeft;
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_data.size(); stream++)
{
2019-10-02 19:19:43 -04:00
std::copy(vbegin[stream], vbegin[stream] + spaceLeft, m_data[stream].begin() + m_fill);
std::copy(vbegin[stream] + spaceLeft, vbegin[stream] + size, m_data[stream].begin());
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
m_fill = remaining;
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
emit dataSyncReady();
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::readSync(
std::vector<SampleVector::const_iterator*> vpart1Begin, std::vector<SampleVector::const_iterator*> vpart1End,
2019-10-02 19:19:43 -04:00
std::vector<SampleVector::const_iterator*> vpart2Begin, std::vector<SampleVector::const_iterator*> vpart2End
)
2019-09-24 18:35:02 -04:00
{
2019-10-02 19:19:43 -04:00
if (m_data.size() == 0) {
return;
}
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
vpart1Begin.resize(m_nbStreams);
vpart1End.resize(m_nbStreams);
vpart2Begin.resize(m_nbStreams);
vpart2End.resize(m_nbStreams);
2019-09-24 18:35:02 -04:00
2019-10-02 19:19:43 -04:00
if (m_head < m_fill)
{
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_data.size(); stream++)
{
2019-10-02 19:19:43 -04:00
*vpart1Begin[stream] = m_data[stream].begin() + m_head;
*vpart1End[stream] = m_data[stream].begin() + m_fill;
*vpart2Begin[stream] = m_data[stream].end();
*vpart2End[stream] = m_data[stream].end();
}
2019-10-02 19:19:43 -04:00
}
else
{
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_data.size(); stream++)
{
2019-10-02 19:19:43 -04:00
*vpart1Begin[stream] = m_data[stream].begin() + m_head;
*vpart1End[stream] = m_data[stream].end();
*vpart2Begin[stream] = m_data[stream].begin();
*vpart2End[stream] = m_data[stream].begin() + m_fill;
}
2019-10-02 19:19:43 -04:00
}
2019-10-02 19:19:43 -04:00
m_head = m_fill;
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::readSync(
std::vector<unsigned int>& vpart1Begin, std::vector<unsigned int>& vpart1End,
std::vector<unsigned int>& vpart2Begin, std::vector<unsigned int>& vpart2End
)
{
2019-10-02 19:19:43 -04:00
if (m_data.size() == 0) {
return;
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
vpart1Begin.resize(m_nbStreams);
vpart1End.resize(m_nbStreams);
vpart2Begin.resize(m_nbStreams);
vpart2End.resize(m_nbStreams);
2019-10-02 19:19:43 -04:00
if (m_head < m_fill)
{
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_data.size(); stream++)
{
vpart1Begin[stream] = m_head;
vpart1End[stream] = m_fill;
vpart2Begin[stream] = 0;
vpart2End[stream] = 0;
}
}
else
{
2019-10-02 19:19:43 -04:00
for (unsigned int stream = 0; stream < m_data.size(); stream++)
{
vpart1Begin[stream] = m_head;
vpart1End[stream] = m_size;
vpart2Begin[stream] = 0;
vpart2End[stream] = m_fill;
}
}
2019-10-02 19:19:43 -04:00
m_head = m_fill;
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::readSync(
unsigned int& ipart1Begin, unsigned int& ipart1End,
unsigned int& ipart2Begin, unsigned int& ipart2End
)
2019-09-24 18:35:02 -04:00
{
2019-10-02 19:19:43 -04:00
if (m_data.size() == 0) {
return;
}
2021-04-26 13:33:02 -04:00
QMutexLocker mutexLocker(&m_mutex);
2019-10-02 19:19:43 -04:00
if (m_head < m_fill)
{
2019-10-02 19:19:43 -04:00
ipart1Begin = m_head;
ipart1End = m_fill;
ipart2Begin = 0;
ipart2End = 0;
}
else
{
ipart1Begin = m_head;
ipart1End = m_size;
ipart2Begin = 0;
ipart2End = m_fill;
}
2019-10-02 19:19:43 -04:00
m_head = m_fill;
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::writeAsync(const quint8* data, unsigned int count, unsigned int stream)
{
if (stream >= m_nbStreams) {
2019-10-02 19:19:43 -04:00
return;
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
unsigned int spaceLeft = m_size - m_vFill[stream];
unsigned int size = count / sizeof(Sample);
2019-09-24 18:35:02 -04:00
2019-10-06 03:53:32 -04:00
if (size > m_size)
{
qWarning("SampleMIFifo::writeAsync: input size %u greater that FIFO size %u: truncating input", size, m_size);
size = m_size;
count = m_size * sizeof(Sample);
}
if (size <= spaceLeft)
2019-09-24 18:35:02 -04:00
{
2019-10-02 19:19:43 -04:00
std::copy(&data[stream*count], &data[stream*count] + count, m_data[stream].begin() + m_vFill[stream]);
m_vFill[stream] += size;
}
else
{
2019-10-02 19:19:43 -04:00
unsigned int remaining = size - spaceLeft;
unsigned int bytesLeft = spaceLeft * sizeof(Sample);
std::copy(&data[stream*count], &data[stream*count] + bytesLeft, m_data[stream].begin() + m_vFill[stream]);
std::copy(&data[stream*count] + bytesLeft, &data[stream*count] + count, m_data[stream].begin());
m_vFill[stream] = remaining;
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
emit dataAsyncReady(stream);
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::writeAsync(const SampleVector::const_iterator& begin, unsigned int size, unsigned int stream)
2019-09-24 18:35:02 -04:00
{
if (stream >= m_nbStreams) {
2019-10-02 19:19:43 -04:00
return;
}
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
2020-11-14 16:08:06 -05:00
unsigned int spaceLeft = m_size < m_vFill[stream] ? 0 : m_size - m_vFill[stream];
2019-09-24 18:35:02 -04:00
2019-10-06 03:53:32 -04:00
if (size > m_size)
{
qWarning("SampleMIFifo::writeAsync: input size %u greater that FIFO size %u: truncating input", size, m_size);
size = m_size;
}
if (size <= spaceLeft)
{
2019-10-02 19:19:43 -04:00
std::copy(begin, begin + size, m_data[stream].begin() + m_vFill[stream]);
m_vFill[stream] += size;
}
else
{
2019-10-06 03:53:32 -04:00
unsigned int remaining = size - spaceLeft;
2019-10-02 19:19:43 -04:00
std::copy(begin, begin + spaceLeft, m_data[stream].begin() + m_vFill[stream]);
std::copy(begin + spaceLeft, begin + size, m_data[stream].begin());
m_vFill[stream] = remaining;
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
emit dataAsyncReady(stream);
2019-09-24 18:35:02 -04:00
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::readAsync(
SampleVector::const_iterator* part1Begin, SampleVector::const_iterator* part1End,
SampleVector::const_iterator* part2Begin, SampleVector::const_iterator* part2End,
unsigned int stream)
{
2019-10-06 03:53:32 -04:00
if (stream >= m_nbStreams) {
2019-10-02 19:19:43 -04:00
return;
}
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
2019-10-02 19:19:43 -04:00
if (m_vHead[stream] < m_vFill[stream])
{
2019-10-02 19:19:43 -04:00
*part1Begin = m_data[stream].begin() + m_vHead[stream];
*part1End = m_data[stream].begin() + m_vFill[stream];
*part2Begin = m_data[stream].begin();
*part2End = m_data[stream].begin();
}
else
{
2019-10-02 19:19:43 -04:00
*part1Begin = m_data[stream].begin() + m_vHead[stream];
*part1End = m_data[stream].end();
*part2Begin = m_data[stream].begin();
*part2End = m_data[stream].begin() + m_vFill[stream];
}
2019-10-02 19:19:43 -04:00
m_vHead[stream] = m_vFill[stream];
}
2019-10-02 19:19:43 -04:00
void SampleMIFifo::readAsync(
unsigned int& ipart1Begin, unsigned int& ipart1End,
unsigned int& ipart2Begin, unsigned int& ipart2End,
unsigned int stream)
{
2019-10-02 19:19:43 -04:00
if (stream >= m_data.size()) {
return;
}
2019-10-02 19:19:43 -04:00
QMutexLocker mutexLocker(&m_mutex);
2019-10-02 19:19:43 -04:00
if (m_vHead[stream] < m_vFill[stream])
{
2019-10-02 19:19:43 -04:00
ipart1Begin = m_vHead[stream];
ipart1End = m_vFill[stream];
ipart2Begin = m_size;
ipart2End = m_size;
}
else
{
ipart1Begin = m_vHead[stream];
ipart1End = m_size;
ipart2Begin = 0;
ipart2End = m_vFill[stream];
}
2019-10-02 19:19:43 -04:00
m_vHead[stream] = m_vFill[stream];
2020-11-14 16:08:06 -05:00
}