2017-02-06 14:09:08 +01:00
|
|
|
#
|
|
|
|
/*
|
2025-07-23 20:45:54 +02:00
|
|
|
* Copyright (C) 2016 .. 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
|
|
|
*
|
2017-03-21 11:59:45 +01: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.
|
|
|
|
*
|
2017-03-21 11:59:45 +01:00
|
|
|
* 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
|
2017-03-21 11:59:45 +01:00
|
|
|
* 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,
|
2019-10-27 18:51:52 +01:00
|
|
|
RingBuffer<uint8_t> *databuffer,
|
2022-03-12 15:09:46 +01:00
|
|
|
RingBuffer<uint8_t> *frameBuffer,
|
2025-01-29 19:39:38 +01:00
|
|
|
FILE *dump, int flag,
|
|
|
|
uint8_t cpuSupport):
|
|
|
|
deconvolver (d, cpuSupport),
|
2025-07-23 20:45:54 +02:00
|
|
|
hardBits (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__
|
2019-11-22 14:45:10 +01:00
|
|
|
,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;
|
2018-02-04 20:29:45 +01:00
|
|
|
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);
|
2018-09-01 15:17:34 +02:00
|
|
|
interleaveData. resize (16);
|
2025-01-06 20:47:14 +01:00
|
|
|
for (int i = 0; i < 16; i ++) {
|
2018-09-01 15:17:34 +02:00
|
|
|
interleaveData [i]. resize (fragmentSize);
|
2025-07-23 20:45:54 +02:00
|
|
|
memset (interleaveData [i]. data (), 0,
|
2018-09-01 15:17:34 +02:00
|
|
|
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 ++)
|
2018-09-01 15:17:34 +02:00
|
|
|
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__
|
2025-09-12 18:33:52 +02:00
|
|
|
if (!running. load () &&
|
|
|
|
!this -> isRunning ())
|
|
|
|
return;
|
2017-10-01 19:07:22 +02:00
|
|
|
running. store (false);
|
2025-09-12 18:33:52 +02:00
|
|
|
driver. stop ();
|
2019-07-08 23:30:45 +02:00
|
|
|
while (this -> isRunning())
|
2018-07-03 13:03:12 +02:00
|
|
|
usleep (1000);
|
2017-10-01 19:07:22 +02:00
|
|
|
#endif
|
2017-02-06 14:09:08 +01:00
|
|
|
}
|
|
|
|
|
2025-07-23 20:45:54 +02:00
|
|
|
int32_t Backend::process (int16_t *softBits, int16_t cnt) {
|
2018-09-24 19:38:07 +02:00
|
|
|
(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))
|
2025-09-08 13:38:08 +02:00
|
|
|
if (!running. load ())
|
2017-02-06 14:09:08 +01:00
|
|
|
return 0;
|
2025-07-23 20:45:54 +02:00
|
|
|
memcpy (theData [nextIn]. data (), softBits,
|
|
|
|
fragmentSize * sizeof (int16_t));
|
2019-11-22 14:45:10 +01:00
|
|
|
nextIn = (nextIn + 1) % NUMBER_SLOTS;
|
|
|
|
usedSlots. release (1);
|
2017-10-01 19:07:22 +02:00
|
|
|
#else
|
2025-07-23 20:45:54 +02:00
|
|
|
processSegment (softBits);
|
2017-10-01 19:07:22 +02:00
|
|
|
#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};
|
2025-07-23 20:45:54 +02:00
|
|
|
void Backend::processSegment (int16_t *softBits_in) {
|
2017-02-06 14:09:08 +01:00
|
|
|
|
2025-07-23 20:45:54 +02:00
|
|
|
for (uint16_t i = 0; i < fragmentSize; i ++) {
|
2017-10-01 19:07:22 +02:00
|
|
|
tempX [i] = interleaveData [(interleaverIndex +
|
|
|
|
interleaveMap [i & 017]) & 017][i];
|
2025-07-23 20:45:54 +02:00
|
|
|
interleaveData [interleaverIndex][i] = softBits_in [i];
|
2017-10-01 19:07:22 +02:00
|
|
|
}
|
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__
|
2019-11-22 14:45:10 +01:00
|
|
|
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;
|
|
|
|
}
|
2017-03-21 11:59:45 +01:00
|
|
|
|
2025-07-23 20:45:54 +02:00
|
|
|
deconvolver. deconvolve (tempX. data(), fragmentSize, hardBits. data());
|
2018-02-09 20:21:44 +01:00
|
|
|
// and the energy dispersal
|
2025-07-23 20:45:54 +02:00
|
|
|
for (uint16_t i = 0; i < bitRate * 24; i ++)
|
|
|
|
hardBits [i] ^= disperseVector [i];
|
2017-10-01 19:07:22 +02:00
|
|
|
|
2025-09-12 18:33:52 +02:00
|
|
|
locker. lock ();
|
2025-07-23 20:45:54 +02:00
|
|
|
driver. addtoFrame (hardBits);
|
2025-09-12 18:33:52 +02:00
|
|
|
locker. unlock ();
|
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))
|
2025-09-08 13:38:08 +02:00
|
|
|
if (!running. load ()) {
|
2017-10-01 19:07:22 +02:00
|
|
|
return;
|
2025-09-08 13:38:08 +02:00
|
|
|
}
|
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__
|
2025-09-12 18:33:52 +02:00
|
|
|
locker. lock ();
|
2025-09-08 13:38:08 +02:00
|
|
|
driver. stop ();
|
2025-09-12 18:33:52 +02:00
|
|
|
locker. unlock ();
|
2025-09-08 13:38:08 +02:00
|
|
|
running. store (false);
|
2019-07-08 23:30:45 +02:00
|
|
|
while (this -> isRunning())
|
2025-09-12 18:33:52 +02:00
|
|
|
usleep (1000);
|
2017-10-01 19:07:22 +02:00
|
|
|
#endif
|
2017-02-06 14:09:08 +01:00
|
|
|
}
|
|
|
|
|