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
|
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2016 Edouard Griffiths, F4EXB. //
// //
// This program 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 as version 3 of the License, or //
// //
// This program 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 V3 for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with this program. If not, see <http://www.gnu.org/licenses/>. //
///////////////////////////////////////////////////////////////////////////////////
/* descramble.h */
// Functions for processing the radio-header:
// descramble
// deinterleave
// FECdecoder
// (C) 2011 Jonathan Naylor G4KLX
/*
* This program 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; version 2 of the License.
*
* This program 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.
*/
// This code was originally written by JOnathan Naylor, G4KLX, as part
// of the "pcrepeatercontroller" project
// More info:
// http://groups.yahoo.com/group/pcrepeatercontroller
// Changes:
// Convert C++ to C
// Version 20111106: initial release
// F4EXB: Convert back to C++ !
#include <string.h>
#include "descramble.h"
namespace DSDcc
{
const unsigned char Descramble::SCRAMBLER_TABLE_BITS[] = {
0,0,0,0,1,1,1,0,1,1,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,
0,0,1,0,0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,0,
1,1,0,1,0,1,0,0,1,1,1,0,0,1,1,1,1,0,1,1,0,1,0,0,0,0,1,0,1,0,1,0,
1,1,1,1,1,0,1,0,0,1,0,1,0,0,0,1,1,0,1,1,1,0,0,0,1,1,1,1,1,1,1,0,
0,0,0,1,1,1,0,1,1,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,
0,1,0,0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,0,1,
1,0,1,0,1,0,0,1,1,1,0,0,1,1,1,1,0,1,1,0,1,0,0,0,0,1,0,1,0,1,0,1,
1,1,1,1,0,1,0,0,1,0,1,0,0,0,1,1,0,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,
0,0,1,1,1,0,1,1,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,
1,0,0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,0,1,1,
0,1,0,1,0,0,1,1,1,0,0,1,1,1,1,0,1,1,0,1,0,0,0,0,1,0,1,0,1,0,1,1,
1,1,1,0,1,0,0,1,0,1,0,0,0,1,1,0,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,0,
0,1,1,1,0,1,1,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,1,
0,0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,0,1,1,0,
1,0,1,0,0,1,1,1,0,0,1,1,1,1,0,1,1,0,1,0,0,0,0,1,0,1,0,1,0,1,1,1,
1,1,0,1,0,0,1,0,1,0,0,0,1,1,0,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,0,0,
1,1,1,0,1,1,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,
0,1,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,0,1,1,0,1,
0,1,0,0,1,1,1,0,0,1,1,1,1,0,1,1,0,1,0,0,0,0,1,0,1,0,1,0,1,1,1,1,
1,0,1,0,0,1,0,1,0,0,0,1,1,0,1,1,1,0,0,0,1,1,1,1,1,1,1,0,0,0,0,1,
1,1,0,1,1,1,1,0,0,1,0,1,1,0,0,1,0,0,1,0,0,0,0,0,0,1,0,0,0,1,0,0,
1,1,0,0,0,1,0,1,1,1,0,1,0,1,1,0,1,1,0,0,0,0,0,1,1,0,0,1,1,0,1,0,
1,0,0,1,1,1,0,0,1,1,1,1,0,1,1,0};
int Descramble::traceBack(unsigned char *out,
unsigned char * m_pathMemory0,
unsigned char * m_pathMemory1,
unsigned char * m_pathMemory2,
unsigned char * m_pathMemory3)
{
enum FEC_STATE
{
S0, S1, S2, S3
} state;
int loop;
int length = 0;
state = S0;
for (loop = 329; loop >= 0; loop--, length++)
{
switch (state)
{
case S0: // if state S0
if (m_pathMemory0[loop])
{
state = S2; // lower path
}
else
{
state = S0; // upper path
}
; // end else - if
out[loop] = 0;
break;
case S1: // if state S1
if (m_pathMemory1[loop])
{
state = S2; // lower path
}
else
{
state = S0; // upper path
}
; // end else - if
out[loop] = 1;
break;
case S2: // if state S2
if (m_pathMemory2[loop])
{
state = S3; // lower path
}
else
{
state = S1; // upper path
}
; // end else - if
out[loop] = 0;
break;
case S3: // if state S3
if (m_pathMemory3[loop])
{
state = S3; // lower path
}
else
{
state = S1; // upper path
}
; // end else - if
out[loop] = 1;
break;
}; // end switch
}; // end for
return (length);
} // end function
void Descramble::viterbiDecode(int n,
unsigned char *data,
unsigned char *m_pathMemory0,
unsigned char *m_pathMemory1,
unsigned char *m_pathMemory2,
unsigned char *m_pathMemory3,
unsigned char *m_pathMetric)
{
int tempMetric[4];
int metric[8];
int loop;
int m1;
int m2;
// Organization is not understandable:
metric[0] = (data[1] ^ 0) + (data[0] ^ 0); // 0/00 S0
metric[1] = (data[1] ^ 1) + (data[0] ^ 1); // 0/11 S0
metric[2] = (data[1] ^ 1) + (data[0] ^ 0); // 0/10 S1
metric[3] = (data[1] ^ 0) + (data[0] ^ 1); // 0/01 S1
metric[4] = (data[1] ^ 1) + (data[0] ^ 1); // 1/11 S2
metric[5] = (data[1] ^ 0) + (data[0] ^ 0); // 1/00 S2
metric[6] = (data[1] ^ 0) + (data[0] ^ 1); // 1/01 S3
metric[7] = (data[1] ^ 1) + (data[0] ^ 0); // 1/10 S3
// Pres. state = S0, Prev. state = S0 & S2
m1 = metric[0] + m_pathMetric[0];
m2 = metric[4] + m_pathMetric[2];
if (m1 < m2)
{
m_pathMemory0[n] = 0;
tempMetric[0] = m1;
}
else
{
m_pathMemory0[n] = 1;
tempMetric[0] = m2;
}; // end else - if
// Pres. state = S1, Prev. state = S0 & S2
m1 = metric[1] + m_pathMetric[0];
m2 = metric[5] + m_pathMetric[2];
if (m1 < m2)
{
m_pathMemory1[n] = 0;
tempMetric[1] = m1;
}
else
{
m_pathMemory1[n] = 1;
tempMetric[1] = m2;
}; // end else - if
// Pres. state = S2, Prev. state = S1 & S3
m1 = metric[2] + m_pathMetric[1];
m2 = metric[6] + m_pathMetric[3];
if (m1 < m2)
{
m_pathMemory2[n] = 0;
tempMetric[2] = m1;
}
else
{
m_pathMemory2[n] = 1;
tempMetric[2] = m2;
}
// Pres. state = S3, Prev. state = S1 & S3
m1 = metric[3] + m_pathMetric[1];
m2 = metric[7] + m_pathMetric[3];
if (m1 < m2)
{
m_pathMemory3[n] = 0;
tempMetric[3] = m1;
}
else
{
m_pathMemory3[n] = 1;
tempMetric[3] = m2;
}; // end else - if
for (loop = 0; loop < 4; loop++)
{
m_pathMetric[loop] = tempMetric[loop];
}; // end for
} // end function ViterbiDecode
int Descramble::FECdecoder(unsigned char *in, unsigned char *out)
{
int outLen;
unsigned char m_pathMemory0[330];
memset(m_pathMemory0, 0, 330 * sizeof(unsigned char));
unsigned char m_pathMemory1[330];
memset(m_pathMemory1, 0, 330 * sizeof(unsigned char));
unsigned char m_pathMemory2[330];
memset(m_pathMemory2, 0, 330 * sizeof(unsigned char));
unsigned char m_pathMemory3[330];
memset(m_pathMemory3, 0, 330 * sizeof(unsigned char));
unsigned char m_pathMetric[4];
int loop, loop2;
int n = 0;
for (loop = 0; loop < 4; loop++)
{
m_pathMetric[loop] = 0;
}; // end for
for (loop2 = 0; loop2 < 660; loop2 += 2, n++)
{
unsigned char data[2];
if (in[loop2])
{
data[1] = 1;
}
else
{
data[1] = 0;
}; // end else - if
if (in[loop2 + 1])
{
data[0] = 1;
}
else
{
data[0] = 0;
}; // end else - if
viterbiDecode(n, data, m_pathMemory0, m_pathMemory1, m_pathMemory2,
m_pathMemory3, m_pathMetric);
}; // end for
outLen = traceBack(out, m_pathMemory0, m_pathMemory1, m_pathMemory2,
m_pathMemory3);
// Swap endian-ness
// code removed (done converting bits into octets), done in main program
//for (loop=0;loop<330;loop+=8) {
// int temp;
// temp=out[loop];out[loop]=out[loop+7];out[loop+7]=temp;
// temp=out[loop+1];out[loop+1]=out[loop+6];out[loop+6]=temp;
// temp=out[loop+2];out[loop+2]=out[loop+5];out[loop+5]=temp;
// temp=out[loop+3];out[loop+3]=out[loop+4];out[loop+4]=temp;
//}
return (outLen);
} // end function FECdecoder
void Descramble::deinterleave(unsigned char *in, unsigned char *out)
{
int k = 0;
int loop = 0;
// function starts here
// init vars
k = 0;
for (loop = 0; loop < 660; loop++)
{
out[k] = in[loop];
k += 24;
if (k >= 672)
{
k -= 671;
}
else if (k >= 660)
{
k -= 647;
}; // end elsif - if
}; // end for
} // end function deinterleave
void Descramble::scramble (unsigned char *in, unsigned char *out)
{
int loop = 0;
int m_count = 0;
for (loop = 0; loop < 660; loop++)
{
out[loop] = in[loop] ^ SCRAMBLER_TABLE_BITS[m_count++];
if (m_count >= SCRAMBLER_TABLE_BITS_LENGTH)
{
m_count = 0U;
}; // end if
}; // end for
}
} // namespace DSDcc
|