summaryrefslogtreecommitdiffstats
path: root/src/DabModulator.cpp
blob: 0ead55519984e743f4c7652f540256f8baa72471 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
   Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012
   Her Majesty the Queen in Right of Canada (Communications Research
   Center Canada)

   Copyright (C) 2015
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://opendigitalradio.org
 */
/*
   This file is part of ODR-DabMod.

   ODR-DabMod 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, either version 3 of the
   License, or (at your option) any later version.

   ODR-DabMod 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 for more details.

   You should have received a copy of the GNU General Public License
   along with ODR-DabMod.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <string>

#include "DabModulator.h"
#include "PcDebug.h"

#include "FrameMultiplexer.h"
#include "PrbsGenerator.h"
#include "BlockPartitioner.h"
#include "QpskSymbolMapper.h"
#include "FrequencyInterleaver.h"
#include "PhaseReference.h"
#include "DifferentialModulator.h"
#include "NullSymbol.h"
#include "SignalMultiplexer.h"
#include "CicEqualizer.h"
#include "OfdmGenerator.h"
#include "GainControl.h"
#include "GuardIntervalInserter.h"
#include "Resampler.h"
#include "ConvEncoder.h"
#include "FIRFilter.h"
#include "TII.h"
#include "PuncturingEncoder.h"
#include "TimeInterleaver.h"
#include "TimestampDecoder.h"
#include "RemoteControl.h"
#include "Log.h"

using namespace boost;

DabModulator::DabModulator(
        struct modulator_offset_config& modconf,
        RemoteControllers* rcs,
        unsigned outputRate, unsigned clockRate,
        unsigned dabMode, GainMode gainMode,
        float digGain, float normalise,
        std::string filterTapsFilename
        ) :
    ModCodec(ModFormat(1), ModFormat(0)),
    myOutputRate(outputRate),
    myClockRate(clockRate),
    myDabMode(dabMode),
    myGainMode(gainMode),
    myDigGain(digGain),
    myNormalise(normalise),
    myEtiReader(EtiReader(modconf)),
    myFlowgraph(NULL),
    myFilterTapsFilename(filterTapsFilename),
    myRCs(rcs)
{
    PDEBUG("DabModulator::DabModulator(%u, %u, %u, %u) @ %p\n",
            outputRate, clockRate, dabMode, gainMode, this);

    if (myDabMode == 0) {
        setMode(2);
    } else {
        setMode(myDabMode);
    }
}


DabModulator::~DabModulator()
{
    PDEBUG("DabModulator::~DabModulator() @ %p\n", this);

    delete myFlowgraph;
}


void DabModulator::setMode(unsigned mode)
{
    switch (mode) {
    case 1:
        myNbSymbols = 76;
        myNbCarriers = 1536;
        mySpacing = 2048;
        myNullSize = 2656;
        mySymSize = 2552;
        myFicSizeOut = 288;
        break;
    case 2:
        myNbSymbols = 76;
        myNbCarriers = 384;
        mySpacing = 512;
        myNullSize = 664;
        mySymSize = 638;
        myFicSizeOut = 288;
        break;
    case 3:
        myNbSymbols = 153;
        myNbCarriers = 192;
        mySpacing = 256;
        myNullSize = 345;
        mySymSize = 319;
        myFicSizeOut = 384;
        break;
    case 4:
        myNbSymbols = 76;
        myNbCarriers = 768;
        mySpacing = 1024;
        myNullSize = 1328;
        mySymSize = 1276;
        myFicSizeOut = 288;
        break;
    default:
        throw std::runtime_error("DabModulator::setMode invalid mode size");
    }

    myOutputFormat.size((size_t)((myNullSize + (myNbSymbols * mySymSize))
                * sizeof(complexf) / 2048000.0 * myOutputRate));
}


int DabModulator::process(Buffer* const dataIn, Buffer* dataOut)
{
    PDEBUG("DabModulator::process(dataIn: %p, dataOut: %p)\n",
            dataIn, dataOut);

    myEtiReader.process(dataIn);
    if (myFlowgraph == NULL) {
        unsigned mode = myEtiReader.getMode();
        if (myDabMode != 0) {
            mode = myDabMode;
        } else if (mode == 0) {
            mode = 4;
        }
        setMode(mode);

        myFlowgraph = new Flowgraph();
        ////////////////////////////////////////////////////////////////
        // CIF data initialisation
        ////////////////////////////////////////////////////////////////
        shared_ptr<PrbsGenerator> cifPrbs(new PrbsGenerator(864 * 8, 0x110));
        shared_ptr<FrameMultiplexer> cifMux(
                new FrameMultiplexer(myFicSizeOut + 864 * 8,
                &myEtiReader.getSubchannels()));

        shared_ptr<BlockPartitioner> cifPart(
                new BlockPartitioner(mode, myEtiReader.getFp()));

        shared_ptr<QpskSymbolMapper> cifMap(new QpskSymbolMapper(myNbCarriers));
        shared_ptr<PhaseReference> cifRef(new PhaseReference(mode));
        shared_ptr<FrequencyInterleaver> cifFreq(new FrequencyInterleaver(mode));
        shared_ptr<DifferentialModulator> cifDiff(
                new DifferentialModulator(myNbCarriers));

        shared_ptr<NullSymbol> cifNull(new NullSymbol(myNbCarriers));
        shared_ptr<SignalMultiplexer> cifSig(new SignalMultiplexer(
                (1 + myNbSymbols) * myNbCarriers * sizeof(complexf)));

        // TODO this needs a review
        bool useCicEq = false;
        unsigned cic_ratio = 1;
        if (myClockRate) {
            cic_ratio = myClockRate / myOutputRate;
            cic_ratio /= 4; // FPGA DUC
            if (myClockRate == 400000000) { // USRP2
                if (cic_ratio & 1) { // odd
                    useCicEq = true;
                } // even, no filter
            }
            else {
                useCicEq = true;
            }
        }

        shared_ptr<CicEqualizer> cifCicEq(new CicEqualizer(myNbCarriers,
                (float)mySpacing * (float)myOutputRate / 2048000.0f,
                cic_ratio));


        shared_ptr<TII> tii(new TII(myDabMode, 3, 16));

        shared_ptr<OfdmGenerator> cifOfdm(
                new OfdmGenerator((1 + myNbSymbols), myNbCarriers, mySpacing));

        shared_ptr<GainControl> cifGain(
                new GainControl(mySpacing, myGainMode, myDigGain, myNormalise));

        cifGain->enrol_at(*myRCs);

        shared_ptr<GuardIntervalInserter> cifGuard(
                new GuardIntervalInserter(myNbSymbols, mySpacing,
                myNullSize, mySymSize));

        FIRFilter* cifFilter = NULL;
        if (myFilterTapsFilename != "") {
            cifFilter = new FIRFilter(myFilterTapsFilename);
            cifFilter->enrol_at(*myRCs);
        }
        shared_ptr<OutputMemory> myOutput(new OutputMemory(dataOut));

        Resampler* cifRes = NULL;
        if (myOutputRate != 2048000) {
            cifRes = new Resampler(2048000, myOutputRate, mySpacing);
        } else {
            fprintf(stderr, "No resampler\n");
        }

        myFlowgraph->connect(cifPrbs, cifMux);

        ////////////////////////////////////////////////////////////////
        // Processing FIC
        ////////////////////////////////////////////////////////////////
        shared_ptr<FicSource> fic(myEtiReader.getFic());
        ////////////////////////////////////////////////////////////////
        // Data initialisation
        ////////////////////////////////////////////////////////////////
        myFicSizeIn = fic->getFramesize();

        ////////////////////////////////////////////////////////////////
        // Modules configuration
        ////////////////////////////////////////////////////////////////

        // Configuring FIC channel

        PDEBUG("FIC:\n");
        PDEBUG(" Framesize: %zu\n", fic->getFramesize());

        // Configuring prbs generator
        shared_ptr<PrbsGenerator> ficPrbs(new PrbsGenerator(myFicSizeIn, 0x110));

        // Configuring convolutionnal encoder
        shared_ptr<ConvEncoder> ficConv(new ConvEncoder(myFicSizeIn));

        // Configuring puncturing encoder
        shared_ptr<PuncturingEncoder> ficPunc(new PuncturingEncoder());
        std::vector<PuncturingRule*> rules = fic->get_rules();
        std::vector<PuncturingRule*>::const_iterator rule;
        for (rule = rules.begin(); rule != rules.end(); ++rule) {
            PDEBUG(" Adding rule:\n");
            PDEBUG("  Length: %zu\n", (*rule)->length());
            PDEBUG("  Pattern: 0x%x\n", (*rule)->pattern());
            ficPunc->append_rule(*(*rule));
        }
        PDEBUG(" Adding tail\n");
        ficPunc->append_tail_rule(PuncturingRule(3, 0xcccccc));

        myFlowgraph->connect(fic, ficPrbs);
        myFlowgraph->connect(ficPrbs, ficConv);
        myFlowgraph->connect(ficConv, ficPunc);
        myFlowgraph->connect(ficPunc, cifPart);

        ////////////////////////////////////////////////////////////////
        // Configuring subchannels
        ////////////////////////////////////////////////////////////////
        std::vector<shared_ptr<SubchannelSource> > subchannels =
            myEtiReader.getSubchannels();
        std::vector<shared_ptr<SubchannelSource> >::const_iterator subchannel;
        for (subchannel = subchannels.begin();
                subchannel != subchannels.end();
                ++subchannel) {

            ////////////////////////////////////////////////////////////
            // Data initialisation
            ////////////////////////////////////////////////////////////
            size_t subchSizeIn = (*subchannel)->framesize();
            size_t subchSizeOut = (*subchannel)->framesizeCu() * 8;

            ////////////////////////////////////////////////////////////
            // Modules configuration
            ////////////////////////////////////////////////////////////

            // Configuring subchannel
            PDEBUG("Subchannel:\n");
            PDEBUG(" Start address: %zu\n",
                    (*subchannel)->startAddress());
            PDEBUG(" Framesize: %zu\n",
                    (*subchannel)->framesize());
            PDEBUG(" Bitrate: %zu\n", (*subchannel)->bitrate());
            PDEBUG(" Framesize CU: %zu\n",
                    (*subchannel)->framesizeCu());
            PDEBUG(" Protection: %zu\n",
                    (*subchannel)->protection());
            PDEBUG("  Form: %zu\n",
                    (*subchannel)->protectionForm());
            PDEBUG("  Level: %zu\n",
                    (*subchannel)->protectionLevel());
            PDEBUG("  Option: %zu\n",
                    (*subchannel)->protectionOption());

            // Configuring prbs genrerator
            shared_ptr<PrbsGenerator> subchPrbs(
                    new PrbsGenerator(subchSizeIn, 0x110));

            // Configuring convolutionnal encoder
            shared_ptr<ConvEncoder> subchConv(
                    new ConvEncoder(subchSizeIn));

            // Configuring puncturing encoder
            shared_ptr<PuncturingEncoder> subchPunc(
                    new PuncturingEncoder());

            std::vector<PuncturingRule*> rules = (*subchannel)->get_rules();
            std::vector<PuncturingRule*>::const_iterator rule;
            for (rule = rules.begin(); rule != rules.end(); ++rule) {
                PDEBUG(" Adding rule:\n");
                PDEBUG("  Length: %zu\n", (*rule)->length());
                PDEBUG("  Pattern: 0x%x\n", (*rule)->pattern());
                subchPunc->append_rule(*(*rule));
            }
            PDEBUG(" Adding tail\n");
            subchPunc->append_tail_rule(PuncturingRule(3, 0xcccccc));

            // Configuring time interleaver
            shared_ptr<TimeInterleaver> subchInterleaver(
                    new TimeInterleaver(subchSizeOut));

            myFlowgraph->connect(*subchannel, subchPrbs);
            myFlowgraph->connect(subchPrbs, subchConv);
            myFlowgraph->connect(subchConv, subchPunc);
            myFlowgraph->connect(subchPunc, subchInterleaver);
            myFlowgraph->connect(subchInterleaver, cifMux);
        }

        myFlowgraph->connect(cifMux, cifPart);
        myFlowgraph->connect(cifPart, cifMap);
        myFlowgraph->connect(cifMap, cifFreq);
        myFlowgraph->connect(cifRef, cifDiff);
        myFlowgraph->connect(cifFreq, cifDiff);
        myFlowgraph->connect(cifNull, cifSig);
        myFlowgraph->connect(cifDiff, cifSig);
        myFlowgraph->connect(tii, cifSig);
        if (useCicEq) {
            myFlowgraph->connect(cifSig, cifCicEq);
            myFlowgraph->connect(cifCicEq, cifOfdm);
        } else {
            myFlowgraph->connect(cifSig, cifOfdm);
        }
        myFlowgraph->connect(cifOfdm, cifGain);
        myFlowgraph->connect(cifGain, cifGuard);

        if (myFilterTapsFilename != "") {
            shared_ptr<FIRFilter> cifFilterptr(cifFilter);
            myFlowgraph->connect(cifGuard, cifFilterptr);
            if (cifRes != NULL) {
                shared_ptr<Resampler> res(cifRes);
                myFlowgraph->connect(cifFilterptr, res);
                myFlowgraph->connect(res, myOutput);
            } else {
                myFlowgraph->connect(cifFilterptr, myOutput);
            }
        }
        else { //no filtering
            if (cifRes != NULL) {
                shared_ptr<Resampler> res(cifRes);
                myFlowgraph->connect(cifGuard, res);
                myFlowgraph->connect(res, myOutput);
            } else {
                myFlowgraph->connect(cifGuard, myOutput);
            }

        }
    }

    ////////////////////////////////////////////////////////////////////
    // Proccessing data
    ////////////////////////////////////////////////////////////////////
    return myFlowgraph->run();
}