mirror of https://github.com/pvnis/srsRAN_4G.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
334 lines
7.8 KiB
C
334 lines
7.8 KiB
C
11 years ago
|
/**
|
||
|
*
|
||
|
* \section COPYRIGHT
|
||
|
*
|
||
9 years ago
|
* Copyright 2013-2015 Software Radio Systems Limited
|
||
11 years ago
|
*
|
||
|
* \section LICENSE
|
||
|
*
|
||
10 years ago
|
* This file is part of the srsLTE library.
|
||
11 years ago
|
*
|
||
10 years ago
|
* srsLTE is free software: you can redistribute it and/or modify
|
||
10 years ago
|
* it under the terms of the GNU Affero General Public License as
|
||
11 years ago
|
* published by the Free Software Foundation, either version 3 of
|
||
|
* the License, or (at your option) any later version.
|
||
|
*
|
||
10 years ago
|
* srsLTE is distributed in the hope that it will be useful,
|
||
11 years ago
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
* GNU Affero General Public License for more details.
|
||
11 years ago
|
*
|
||
10 years ago
|
* A copy of the GNU Affero General Public License can be found in
|
||
11 years ago
|
* the LICENSE file in the top-level directory of this distribution
|
||
|
* and at http://www.gnu.org/licenses/.
|
||
|
*
|
||
|
*/
|
||
|
|
||
|
|
||
11 years ago
|
#include <stdio.h>
|
||
10 years ago
|
#include <stdint.h>
|
||
9 years ago
|
#include <string.h>
|
||
|
#include <stdlib.h>
|
||
10 years ago
|
|
||
9 years ago
|
#include "srslte/fec/cbsegm.h"
|
||
10 years ago
|
#include "srslte/fec/turbocoder.h"
|
||
9 years ago
|
#include "srslte/utils/bit.h"
|
||
|
#include "srslte/utils/vector.h"
|
||
11 years ago
|
|
||
11 years ago
|
#define NOF_REGS 3
|
||
|
|
||
10 years ago
|
#define RATE 3
|
||
|
#define TOTALTAIL 12
|
||
11 years ago
|
|
||
9 years ago
|
uint8_t tcod_lut_next_state[188][8][256];
|
||
|
uint8_t tcod_lut_output[188][8][256];
|
||
9 years ago
|
uint16_t tcod_per_fw[188][6144];
|
||
9 years ago
|
|
||
|
static bool table_initiated = false;
|
||
10 years ago
|
|
||
|
int srslte_tcod_init(srslte_tcod_t *h, uint32_t max_long_cb) {
|
||
|
|
||
11 years ago
|
h->max_long_cb = max_long_cb;
|
||
9 years ago
|
h->temp = srslte_vec_malloc(max_long_cb/8);
|
||
9 years ago
|
|
||
|
if (!table_initiated) {
|
||
|
table_initiated = true;
|
||
|
srslte_tcod_gentable();
|
||
|
}
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
10 years ago
|
void srslte_tcod_free(srslte_tcod_t *h) {
|
||
11 years ago
|
h->max_long_cb = 0;
|
||
9 years ago
|
if (h->temp) {
|
||
|
free(h->temp);
|
||
|
}
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
/* Expects bits (1 byte = 1 bit) and produces bits. The systematic and parity bits are interlaced in the output */
|
||
|
int srslte_tcod_encode(srslte_tcod_t *h, uint8_t *input, uint8_t *output, uint32_t long_cb)
|
||
|
{
|
||
11 years ago
|
|
||
10 years ago
|
uint8_t reg1_0, reg1_1, reg1_2, reg2_0, reg2_1, reg2_2;
|
||
10 years ago
|
uint32_t i, k = 0, j;
|
||
10 years ago
|
uint8_t bit;
|
||
|
uint8_t in, out;
|
||
9 years ago
|
uint16_t *per;
|
||
11 years ago
|
|
||
11 years ago
|
if (long_cb > h->max_long_cb) {
|
||
|
fprintf(stderr, "Turbo coder initiated for max_long_cb=%d\n",
|
||
|
h->max_long_cb);
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
9 years ago
|
int longcb_idx = srslte_cbsegm_cbindex(long_cb);
|
||
|
if (longcb_idx < 0) {
|
||
|
fprintf(stderr, "Invalid CB size %d\n", long_cb);
|
||
11 years ago
|
return -1;
|
||
|
}
|
||
9 years ago
|
|
||
|
per = tcod_per_fw[longcb_idx];
|
||
11 years ago
|
|
||
|
reg1_0 = 0;
|
||
|
reg1_1 = 0;
|
||
|
reg1_2 = 0;
|
||
|
|
||
|
reg2_0 = 0;
|
||
|
reg2_1 = 0;
|
||
|
reg2_2 = 0;
|
||
|
|
||
|
k = 0;
|
||
|
for (i = 0; i < long_cb; i++) {
|
||
10 years ago
|
if (input[i] == SRSLTE_TX_NULL) {
|
||
10 years ago
|
bit = 0;
|
||
|
} else {
|
||
|
bit = input[i];
|
||
|
}
|
||
|
output[k] = input[i];
|
||
|
|
||
11 years ago
|
k++;
|
||
11 years ago
|
|
||
|
in = bit ^ (reg1_2 ^ reg1_1);
|
||
|
out = reg1_2 ^ (reg1_0 ^ in);
|
||
|
|
||
|
reg1_2 = reg1_1;
|
||
|
reg1_1 = reg1_0;
|
||
|
reg1_0 = in;
|
||
9 years ago
|
|
||
10 years ago
|
if (input[i] == SRSLTE_TX_NULL) {
|
||
|
output[k] = SRSLTE_TX_NULL;
|
||
10 years ago
|
} else {
|
||
|
output[k] = out;
|
||
|
}
|
||
11 years ago
|
k++;
|
||
11 years ago
|
|
||
|
bit = input[per[i]];
|
||
10 years ago
|
if (bit == SRSLTE_TX_NULL) {
|
||
10 years ago
|
bit = 0;
|
||
|
}
|
||
11 years ago
|
|
||
|
in = bit ^ (reg2_2 ^ reg2_1);
|
||
|
out = reg2_2 ^ (reg2_0 ^ in);
|
||
|
|
||
|
reg2_2 = reg2_1;
|
||
|
reg2_1 = reg2_0;
|
||
|
reg2_0 = in;
|
||
|
|
||
|
output[k] = out;
|
||
11 years ago
|
k++;
|
||
10 years ago
|
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
|
k = 3 * long_cb;
|
||
|
|
||
11 years ago
|
/* TAILING CODER #1 */
|
||
11 years ago
|
for (j = 0; j < NOF_REGS; j++) {
|
||
|
bit = reg1_2 ^ reg1_1;
|
||
|
|
||
|
output[k] = bit;
|
||
11 years ago
|
k++;
|
||
11 years ago
|
|
||
|
in = bit ^ (reg1_2 ^ reg1_1);
|
||
|
out = reg1_2 ^ (reg1_0 ^ in);
|
||
|
|
||
|
reg1_2 = reg1_1;
|
||
|
reg1_1 = reg1_0;
|
||
|
reg1_0 = in;
|
||
|
|
||
|
output[k] = out;
|
||
11 years ago
|
k++;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
/* TAILING CODER #2 */
|
||
11 years ago
|
for (j = 0; j < NOF_REGS; j++) {
|
||
|
bit = reg2_2 ^ reg2_1;
|
||
|
|
||
|
output[k] = bit;
|
||
11 years ago
|
k++;
|
||
11 years ago
|
|
||
|
in = bit ^ (reg2_2 ^ reg2_1);
|
||
|
out = reg2_2 ^ (reg2_0 ^ in);
|
||
|
|
||
|
reg2_2 = reg2_1;
|
||
|
reg2_1 = reg2_0;
|
||
|
reg2_0 = in;
|
||
|
|
||
|
output[k] = out;
|
||
11 years ago
|
k++;
|
||
|
}
|
||
11 years ago
|
return 0;
|
||
11 years ago
|
}
|
||
|
|
||
9 years ago
|
/* Expects bytes and produces bytes. The systematic and parity bits are interlaced in the output */
|
||
9 years ago
|
int srslte_tcod_encode_lut(srslte_tcod_t *h, uint8_t *input, uint8_t *parity, uint32_t cblen_idx)
|
||
9 years ago
|
{
|
||
9 years ago
|
if (cblen_idx < 188) {
|
||
|
uint32_t long_cb = srslte_cbsegm_cbsize(cblen_idx);
|
||
9 years ago
|
|
||
9 years ago
|
if (long_cb % 8) {
|
||
|
fprintf(stderr, "Turbo coder LUT implementation long_cb must be multiple of 8\n");
|
||
|
return -1;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
/* Parity bits for the 1st constituent encoders */
|
||
|
uint8_t state0 = 0;
|
||
|
for (uint32_t i=0;i<long_cb/8;i++) {
|
||
|
parity[i] = tcod_lut_output[cblen_idx][state0][input[i]];
|
||
|
state0 = tcod_lut_next_state[cblen_idx][state0][input[i]] % 8;
|
||
|
}
|
||
9 years ago
|
parity[long_cb/8] = 0; // will put tail here later
|
||
|
|
||
9 years ago
|
/* Interleave input */
|
||
|
srslte_bit_interleave(input, h->temp, tcod_per_fw[cblen_idx], long_cb);
|
||
9 years ago
|
|
||
9 years ago
|
/* Parity bits for the 2nd constituent encoders */
|
||
|
uint8_t state1 = 0;
|
||
|
for (uint32_t i=0;i<long_cb/8;i++) {
|
||
9 years ago
|
uint8_t out = tcod_lut_output[cblen_idx][state1][h->temp[i]];
|
||
|
parity[long_cb/8+i] |= (out&0xf0)>>4;
|
||
|
parity[long_cb/8+i+1] = (out&0xf)<<4;
|
||
9 years ago
|
state1 = tcod_lut_next_state[cblen_idx][state1][h->temp[i]] % 8;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
/* Tail bits */
|
||
|
uint8_t reg1_0, reg1_1, reg1_2, reg2_0, reg2_1, reg2_2;
|
||
|
uint8_t bit, in, out;
|
||
|
uint8_t k=0;
|
||
|
uint8_t tail[12];
|
||
|
|
||
|
reg2_0 = (state1&4)>>2;
|
||
|
reg2_1 = (state1&2)>>1;
|
||
|
reg2_2 = state1&1;
|
||
|
|
||
|
reg1_0 = (state0&4)>>2;
|
||
|
reg1_1 = (state0&2)>>1;
|
||
|
reg1_2 = state0&1;
|
||
|
|
||
|
/* TAILING CODER #1 */
|
||
|
for (uint32_t j = 0; j < NOF_REGS; j++) {
|
||
|
bit = reg1_2 ^ reg1_1;
|
||
|
|
||
|
tail[k] = bit;
|
||
|
k++;
|
||
|
|
||
|
in = bit ^ (reg1_2 ^ reg1_1);
|
||
|
out = reg1_2 ^ (reg1_0 ^ in);
|
||
|
|
||
|
reg1_2 = reg1_1;
|
||
|
reg1_1 = reg1_0;
|
||
|
reg1_0 = in;
|
||
|
|
||
|
tail[k] = out;
|
||
|
k++;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
/* TAILING CODER #2 */
|
||
|
for (uint32_t j = 0; j < NOF_REGS; j++) {
|
||
|
bit = reg2_2 ^ reg2_1;
|
||
9 years ago
|
|
||
9 years ago
|
tail[k] = bit;
|
||
|
k++;
|
||
9 years ago
|
|
||
9 years ago
|
in = bit ^ (reg2_2 ^ reg2_1);
|
||
|
out = reg2_2 ^ (reg2_0 ^ in);
|
||
9 years ago
|
|
||
9 years ago
|
reg2_2 = reg2_1;
|
||
|
reg2_1 = reg2_0;
|
||
|
reg2_0 = in;
|
||
9 years ago
|
|
||
9 years ago
|
tail[k] = out;
|
||
|
k++;
|
||
|
}
|
||
9 years ago
|
|
||
9 years ago
|
uint8_t tailv[3][4];
|
||
|
for (int i=0;i<4;i++) {
|
||
|
for (int j=0;j<3;j++) {
|
||
|
tailv[j][i] = tail[3*i+j];
|
||
|
}
|
||
|
}
|
||
|
uint8_t *x = tailv[0];
|
||
9 years ago
|
input[long_cb/8] = (srslte_bit_pack(&x, 4)<<4);
|
||
9 years ago
|
x = tailv[1];
|
||
9 years ago
|
parity[long_cb/8] |= (srslte_bit_pack(&x, 4)<<4);
|
||
9 years ago
|
x = tailv[2];
|
||
9 years ago
|
parity[2*long_cb/8] |= (srslte_bit_pack(&x, 4)&0xf);
|
||
9 years ago
|
|
||
|
return 3*long_cb+TOTALTAIL;
|
||
|
} else {
|
||
|
return -1;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
|
||
9 years ago
|
void srslte_tcod_gentable() {
|
||
|
srslte_tc_interl_t interl;
|
||
|
|
||
|
if (srslte_tc_interl_init(&interl, 6144)) {
|
||
|
fprintf(stderr, "Error initiating interleave\n");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
for (uint32_t len=0;len<188;len++) {
|
||
|
uint32_t long_cb = srslte_cbsegm_cbsize(len);
|
||
|
if (srslte_tc_interl_LTE_gen(&interl, long_cb)) {
|
||
|
fprintf(stderr, "Error initiating TC interleaver for long_cb=%d\n", long_cb);
|
||
|
return;
|
||
|
}
|
||
|
// Save fw/bw permutation tables
|
||
|
for (uint32_t i=0;i<long_cb;i++) {
|
||
|
tcod_per_fw[len][i] = interl.forward[i];
|
||
|
}
|
||
|
for (uint32_t i=long_cb;i<6144;i++) {
|
||
|
tcod_per_fw[len][i] = 0;
|
||
|
}
|
||
|
// Compute state transitions
|
||
|
for (uint32_t state=0;state<8;state++) {
|
||
|
for (uint32_t data=0;data<256;data++) {
|
||
|
|
||
|
uint8_t reg_0, reg_1, reg_2;
|
||
|
reg_0 = (state&4)>>2;
|
||
|
reg_1 = (state&2)>>1;
|
||
|
reg_2 = state&1;
|
||
|
|
||
|
tcod_lut_output[len][state][data] = 0;
|
||
|
uint8_t bit, in, out;
|
||
|
for (uint32_t i = 0; i < 8; i++) {
|
||
|
bit = (data&(1<<(7-i)))?1:0;
|
||
|
|
||
|
in = bit ^ (reg_2 ^ reg_1);
|
||
|
out = reg_2 ^ (reg_0 ^ in);
|
||
|
|
||
|
reg_2 = reg_1;
|
||
|
reg_1 = reg_0;
|
||
|
reg_0 = in;
|
||
|
|
||
|
tcod_lut_output[len][state][data] |= out<<(7-i);
|
||
|
|
||
|
}
|
||
9 years ago
|
tcod_lut_next_state[len][state][data] = reg_0<<2 | reg_1<<1 | reg_2;
|
||
9 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
srslte_tc_interl_free(&interl);
|
||
|
}
|