1
0
mirror of https://github.com/JvanKatwijk/qt-dab.git synced 2025-10-06 08:12:40 +02:00
Files
SDR-DAB_Qt-DAB/sources/backend/backend.cpp

178 lines
5.3 KiB
C++
Raw Normal View History

2017-02-06 14:09:08 +01:00
#
/*
2025-01-06 20:47:14 +01:00
* Copyright (C) 2014 .. 2024
2017-02-06 14:09:08 +01:00
* Jan van Katwijk (J.vanKatwijk@gmail.com)
2017-05-31 17:46:42 +02:00
* Lazy Chair Computing
2017-02-06 14:09:08 +01:00
*
2017-11-30 18:52:51 +01:00
* This file is part of Qt-DAB
2020-05-11 16:09:46 +02:00
*
* Qt-DAB is free software; you can redistribute it and/or modify
2017-02-06 14:09:08 +01:00
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Qt-DAB is distributed in the hope that it will be useful,
2017-02-06 14:09:08 +01:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Qt-DAB; if not, write to the Free Software
2017-02-06 14:09:08 +01:00
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#
#include "dab-constants.h"
#include "radio.h"
2018-09-24 19:38:07 +02:00
#include "backend.h"
2024-11-02 09:05:53 +01:00
#include "logger.h"
2017-02-06 14:09:08 +01:00
//
// Interleaving is - for reasons of simplicity - done
// inline rather than through a special class-object
2018-09-24 19:38:07 +02:00
#define CUSize (4 * 16)
2017-02-06 14:09:08 +01:00
// fragmentsize == Length * CUSize
2018-09-24 19:38:07 +02:00
Backend::Backend (RadioInterface *mr,
2024-11-02 09:05:53 +01:00
logger *theLogger,
2018-09-24 19:38:07 +02:00
descriptorType *d,
2023-11-05 14:11:28 +01:00
RingBuffer<std::complex<int16_t>> *audiobuffer,
RingBuffer<uint8_t> *databuffer,
2022-03-12 15:09:46 +01:00
RingBuffer<uint8_t> *frameBuffer,
FILE *dump, int flag,
uint8_t cpuSupport):
deconvolver (d, cpuSupport),
2018-09-24 19:38:07 +02:00
outV (d -> bitRate * 24),
2024-11-02 09:05:53 +01:00
driver (mr,
theLogger,
2018-09-24 19:38:07 +02:00
d,
2023-09-23 11:50:09 +02:00
flag == BACK_GROUND,
2018-09-24 19:38:07 +02:00
audiobuffer,
databuffer,
2022-03-12 15:09:46 +01:00
frameBuffer, dump)
2024-02-25 14:18:27 +01:00
#ifdef __THREADED_BACKEND__
,freeSlots (NUMBER_SLOTS)
2017-10-01 19:07:22 +02:00
#endif
{
2020-02-26 13:12:21 +01:00
this -> radioInterface = mr;
2018-09-24 19:38:07 +02:00
this -> startAddr = d -> startAddr;
this -> Length = d -> length;
this -> fragmentSize = d -> length * CUSize;
2018-09-24 19:38:07 +02:00
this -> bitRate = d -> bitRate;
2020-03-02 15:13:34 +01:00
this -> serviceId = d -> SId;
2020-02-26 13:12:21 +01:00
this -> serviceName = d -> serviceName;
this -> shortForm = d -> shortForm;
this -> protLevel = d -> protLevel;
2020-08-28 21:10:25 +02:00
this -> subChId = d -> subchId;
2022-03-12 15:09:46 +01:00
this -> borf = flag;
2020-11-04 09:21:00 +01:00
2022-01-27 10:41:10 +01:00
// fprintf (stderr, "starting a backend for %s (%X) %d\n",
// serviceName. toUtf8 (). data (),
// serviceId, startAddr);
interleaveData. resize (16);
2025-01-06 20:47:14 +01:00
for (int i = 0; i < 16; i ++) {
interleaveData [i]. resize (fragmentSize);
2019-07-08 23:30:45 +02:00
memset (interleaveData [i]. data(), 0,
fragmentSize * sizeof (int16_t));
2017-02-06 14:09:08 +01:00
}
2018-02-09 20:21:44 +01:00
2017-10-01 19:07:22 +02:00
countforInterleaver = 0;
interleaverIndex = 0;
2017-02-06 14:09:08 +01:00
2018-01-17 16:25:29 +01:00
tempX. resize (fragmentSize);
2018-02-09 20:21:44 +01:00
uint8_t shiftRegister [9];
disperseVector. resize (24 * bitRate);
memset (shiftRegister, 1, 9);
2025-01-06 20:47:14 +01:00
for (int i = 0; i < bitRate * 24; i ++) {
2018-02-09 20:21:44 +01:00
uint8_t b = shiftRegister [8] ^ shiftRegister [4];
2025-01-06 20:47:14 +01:00
for (int j = 8; j > 0; j--)
2018-02-09 20:21:44 +01:00
shiftRegister [j] = shiftRegister [j - 1];
shiftRegister [0] = b;
disperseVector [i] = b;
}
2024-02-25 14:18:27 +01:00
#ifdef __THREADED_BACKEND__
2017-10-01 19:07:22 +02:00
// for local buffering the input, we have
nextIn = 0;
nextOut = 0;
2025-01-06 20:47:14 +01:00
for (int i = 0; i < NUMBER_SLOTS; i ++)
theData [i]. resize (fragmentSize);
2017-10-01 19:07:22 +02:00
running. store (true);
2025-01-06 20:47:14 +01:00
start ();
2017-10-01 19:07:22 +02:00
#endif
2017-02-06 14:09:08 +01:00
}
2023-10-04 12:53:19 +02:00
Backend::~Backend () {
2024-02-25 14:18:27 +01:00
#ifdef __THREADED_BACKEND__
2017-10-01 19:07:22 +02:00
running. store (false);
2019-07-08 23:30:45 +02:00
while (this -> isRunning())
usleep (1000);
2017-10-01 19:07:22 +02:00
#endif
2017-02-06 14:09:08 +01:00
}
2018-09-24 19:38:07 +02:00
int32_t Backend::process (int16_t *v, int16_t cnt) {
(void)cnt;
2024-02-25 14:18:27 +01:00
#ifdef __THREADED_BACKEND__
2017-07-12 12:02:20 +02:00
while (!freeSlots. tryAcquire (1, 200))
2017-02-06 14:09:08 +01:00
if (!running)
return 0;
2019-07-08 23:30:45 +02:00
memcpy (theData [nextIn]. data(), v, fragmentSize * sizeof (int16_t));
nextIn = (nextIn + 1) % NUMBER_SLOTS;
usedSlots. release (1);
2017-10-01 19:07:22 +02:00
#else
processSegment (v);
#endif
2017-07-12 12:02:20 +02:00
return 1;
2017-02-06 14:09:08 +01:00
}
const int16_t interleaveMap [] = {0,8,4,12,2,10,6,14,1,9,5,13,3,11,7,15};
2018-09-24 19:38:07 +02:00
void Backend::processSegment (int16_t *Data) {
int16_t i;
2017-02-06 14:09:08 +01:00
2017-10-01 19:07:22 +02:00
for (i = 0; i < fragmentSize; i ++) {
tempX [i] = interleaveData [(interleaverIndex +
interleaveMap [i & 017]) & 017][i];
interleaveData [interleaverIndex][i] = Data [i];
}
2017-07-30 20:19:26 +02:00
2017-10-01 19:07:22 +02:00
interleaverIndex = (interleaverIndex + 1) & 0x0F;
2024-02-25 14:18:27 +01:00
#ifdef __THREADED_BACKEND__
nextOut = (nextOut + 1) % NUMBER_SLOTS;
freeSlots. release (1);
2017-10-01 19:07:22 +02:00
#endif
2017-07-12 12:02:20 +02:00
2017-02-06 14:09:08 +01:00
// only continue when de-interleaver is filled
2017-10-01 19:07:22 +02:00
if (countforInterleaver <= 15) {
countforInterleaver ++;
return;
}
2019-07-08 23:30:45 +02:00
deconvolver. deconvolve (tempX. data(), fragmentSize, outV. data());
2018-02-09 20:21:44 +01:00
// and the energy dispersal
for (i = 0; i < bitRate * 24; i ++)
outV [i] ^= disperseVector [i];
2017-10-01 19:07:22 +02:00
2018-09-24 19:38:07 +02:00
driver. addtoFrame (outV);
2017-02-06 14:09:08 +01:00
}
2017-10-01 19:07:22 +02:00
2024-02-25 14:18:27 +01:00
#ifdef __THREADED_BACKEND__
2019-07-08 23:30:45 +02:00
void Backend::run() {
2017-10-01 19:07:22 +02:00
2019-07-08 23:30:45 +02:00
while (running. load()) {
2017-10-01 19:07:22 +02:00
while (!usedSlots. tryAcquire (1, 200))
if (!running)
return;
2019-07-08 23:30:45 +02:00
processSegment (theData [nextOut]. data());
2017-10-01 19:07:22 +02:00
}
}
#endif
2017-02-06 14:09:08 +01:00
// It might take a msec for the task to stop
2019-07-08 23:30:45 +02:00
void Backend::stopRunning() {
2024-02-25 14:18:27 +01:00
#ifdef __THREADED_BACKEND__
2017-02-06 14:09:08 +01:00
running = false;
2019-07-08 23:30:45 +02:00
while (this -> isRunning())
2017-02-06 14:09:08 +01:00
usleep (1);
2019-07-08 23:30:45 +02:00
// myAudioSink -> stop();
2017-10-01 19:07:22 +02:00
#endif
2017-02-06 14:09:08 +01:00
}