2017-02-06 14:09:08 +01:00
|
|
|
#
|
|
|
|
/*
|
2019-11-10 15:46:23 +01:00
|
|
|
* Copyright (C) 2014 .. 2019
|
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
|
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"
|
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,
|
|
|
|
descriptorType *d,
|
|
|
|
RingBuffer<int16_t> *audiobuffer,
|
2019-10-27 18:51:52 +01:00
|
|
|
RingBuffer<uint8_t> *databuffer,
|
2020-04-12 19:08:56 +02:00
|
|
|
RingBuffer<uint8_t> *frameBuffer):
|
2018-09-24 19:38:07 +02:00
|
|
|
outV (d -> bitRate * 24),
|
|
|
|
driver (mr,
|
|
|
|
d,
|
|
|
|
audiobuffer,
|
|
|
|
databuffer,
|
2020-04-12 19:08:56 +02:00
|
|
|
frameBuffer),
|
2018-09-24 19:38:07 +02:00
|
|
|
deconvolver (d)
|
2018-02-04 20:29:45 +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
|
|
|
|
{
|
2017-02-06 14:09:08 +01:00
|
|
|
int32_t i, j;
|
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;
|
2018-09-01 15:17:34 +02:00
|
|
|
interleaveData. resize (16);
|
2017-02-06 14:09:08 +01:00
|
|
|
for (i = 0; i < 16; i ++) {
|
2018-09-01 15:17:34 +02:00
|
|
|
interleaveData [i]. resize (fragmentSize);
|
2019-07-08 23:30:45 +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);
|
|
|
|
for (i = 0; i < bitRate * 24; i ++) {
|
|
|
|
uint8_t b = shiftRegister [8] ^ shiftRegister [4];
|
|
|
|
for (j = 8; j > 0; j--)
|
|
|
|
shiftRegister [j] = shiftRegister [j - 1];
|
|
|
|
shiftRegister [0] = b;
|
|
|
|
disperseVector [i] = b;
|
|
|
|
}
|
2018-02-04 20:29:45 +01:00
|
|
|
#ifdef __THREADED_BACKEND
|
2017-10-01 19:07:22 +02:00
|
|
|
// for local buffering the input, we have
|
|
|
|
nextIn = 0;
|
|
|
|
nextOut = 0;
|
2019-11-22 14:45:10 +01:00
|
|
|
for (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);
|
2019-07-08 23:30:45 +02:00
|
|
|
start();
|
2017-10-01 19:07:22 +02:00
|
|
|
#endif
|
2017-02-06 14:09:08 +01:00
|
|
|
}
|
|
|
|
|
2019-07-08 23:30:45 +02:00
|
|
|
Backend::~Backend() {
|
2018-02-04 20:29:45 +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())
|
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
|
|
|
}
|
|
|
|
|
2018-09-24 19:38:07 +02:00
|
|
|
int32_t Backend::process (int16_t *v, int16_t cnt) {
|
|
|
|
(void)cnt;
|
2018-02-04 20:29:45 +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));
|
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
|
|
|
|
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;
|
2018-02-04 20:29:45 +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
|
|
|
|
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
|
|
|
|
2018-02-04 20:29:45 +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() {
|
2018-02-04 20:29:45 +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
|
|
|
}
|
|
|
|
|
2020-02-26 13:12:21 +01:00
|
|
|
|
|
|
|
|