summaryrefslogtreecommitdiffstats
path: root/src/TimeInterleaver.cpp
blob: 2bd4b6c8379c0befa14f9b2c40f60a031a76000c (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
/*
   Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011 Her Majesty
   the Queen in Right of Canada (Communications Research Center Canada)
 */
/*
   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 "TimeInterleaver.h"
#include "PcDebug.h"

#include <vector>
#include <stdint.h>


TimeInterleaver::TimeInterleaver(size_t framesize)
    throw (std::invalid_argument) :
        ModCodec(ModFormat(framesize), ModFormat(framesize)),
        d_framesize(framesize)
{
    PDEBUG("TimeInterleaver::TimeInterleaver(%zu) @ %p\n", framesize, this);

    if (framesize & 1) {
        throw std::invalid_argument("framesize must be 16 bits multiple");
    }
    for (int i = 0; i < 16; ++i) {
        std::vector<unsigned char> data(framesize, 0);
        d_history.push_back(data);
    }
}


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


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

    dataOut->setLength(dataIn->getLength());
    const unsigned char* in = reinterpret_cast<const unsigned char*>(dataIn->getData());
    unsigned char* out = reinterpret_cast<unsigned char*>(dataOut->getData());

    
    for (size_t i = 0; i < dataOut->getLength();) {
        d_history.push_front(d_history.back());
        d_history.pop_back();
        for (uint_fast16_t j = 0; j < d_framesize;) {
            d_history[0][j] = in[i];
            out[i]  = d_history[0] [j] & 0x80;
            out[i] |= d_history[8] [j] & 0x40;
            out[i] |= d_history[4] [j] & 0x20;
            out[i] |= d_history[12][j] & 0x10;
            out[i] |= d_history[2] [j] & 0x08;
            out[i] |= d_history[10][j] & 0x04;
            out[i] |= d_history[6] [j] & 0x02;
            out[i] |= d_history[14][j] & 0x01;
            ++i;
            ++j;
            d_history[0][j] = in[i];
            out[i]  = d_history[1] [j] & 0x80;
            out[i] |= d_history[9] [j] & 0x40;
            out[i] |= d_history[5] [j] & 0x20;
            out[i] |= d_history[13][j] & 0x10;
            out[i] |= d_history[3] [j] & 0x08;
            out[i] |= d_history[11][j] & 0x04;
            out[i] |= d_history[7] [j] & 0x02;
            out[i] |= d_history[15][j] & 0x01;
            ++i;
            ++j;
        }
    }
    
    return dataOut->getLength();
}