/* Encode a transport block according to 36.212 5.3.2 * */ static int encode_tb(srslte_sch_t *q, srslte_softbuffer_tx_t *soft_buffer, srslte_cbsegm_t *cb_segm, uint32_t Qm, uint32_t rv, uint32_t nof_e_bits, uint8_t *data, uint8_t *e_bits) { uint8_t parity[3] = {0, 0, 0}; uint32_t par; uint32_t i; uint32_t cb_len, rp, wp, rlen, F, n_e; int ret = SRSLTE_ERROR_INVALID_INPUTS; if (q != NULL && e_bits != NULL && cb_segm != NULL && soft_buffer != NULL) { uint32_t Gp = nof_e_bits / Qm; uint32_t gamma = Gp; if (cb_segm->C > 0) { gamma = Gp%cb_segm->C; } if (data) { /* Compute transport block CRC */ par = srslte_crc_checksum_byte(&q->crc_tb, data, cb_segm->tbs); /* parity bits will be appended later */ parity[0] = (par&(0xff<<16))>>16; parity[1] = (par&(0xff<<8))>>8; parity[2] = par&0xff; if (SRSLTE_VERBOSE_ISDEBUG()) { DEBUG("DATA: ", 0); srslte_vec_fprint_byte(stdout, data, cb_segm->tbs/8); DEBUG("PARITY: ", 0); srslte_vec_fprint_byte(stdout, parity, 3); } } wp = 0; rp = 0; for (i = 0; i < cb_segm->C; i++) { /* Get read lengths */ if (i < cb_segm->C2) { cb_len = cb_segm->K2; } else { cb_len = cb_segm->K1; } if (cb_segm->C > 1) { rlen = cb_len - 24; } else { rlen = cb_len; } if (i == 0) { F = cb_segm->F; } else { F = 0; } if (i <= cb_segm->C - gamma - 1) { n_e = Qm * (Gp/cb_segm->C); } else { n_e = Qm * ((uint32_t) ceilf((float) Gp/cb_segm->C)); } INFO("CB#%d: cb_len: %d, rlen: %d, wp: %d, rp: %d, F: %d, E: %d\n", i, cb_len, rlen - F, wp, rp, F, n_e); if (data) { /* Copy data to another buffer, making space for the Codeblock CRC */ if (i < cb_segm->C - 1) { // Copy data memcpy(&q->cb_in[F/8], &data[rp/8], (rlen - F) * sizeof(uint8_t)/8); } else { INFO("Last CB, appending parity: %d from %d and 24 to %d\n", rlen - F - 24, rp, rlen - 24); /* Append Transport Block parity bits to the last CB */ memcpy(&q->cb_in[F/8], &data[rp/8], (rlen - 24 - F) * sizeof(uint8_t)/8); memcpy(&q->cb_in[(rlen - 24)/8], parity, 3 * sizeof(uint8_t)); } /* Filler bits are treated like zeros for the CB CRC calculation */ for (int j = 0; j < F/8; j++) { q->cb_in[j] = 0; } /* Attach Codeblock CRC */ if (cb_segm->C > 1) { srslte_crc_attach_byte(&q->crc_cb, q->cb_in, rlen); } /* pack bits to temporal buffer for encoding */ srslte_bit_unpack_vector(q->cb_in, q->cb_temp, cb_len); /* Set the filler bits to <NULL> */ for (int j = 0; j < F; j++) { q->cb_temp[j] = SRSLTE_TX_NULL; } if (SRSLTE_VERBOSE_ISDEBUG()) { DEBUG("CB#%d: ", i); srslte_vec_fprint_hex(stdout, q->cb_temp, cb_len); } /* Turbo Encoding */ srslte_tcod_encode(&q->encoder, q->cb_temp, (uint8_t*) q->cb_out, cb_len); if (SRSLTE_VERBOSE_ISDEBUG()) { DEBUG("CB#%d encoded: ", i); srslte_vec_fprint_b(stdout, q->cb_out, 3*cb_len+12); } } /* Rate matching */ if (srslte_rm_turbo_tx(soft_buffer->buffer_b[i], soft_buffer->buff_size, (uint8_t*) q->cb_out, 3 * cb_len + 12, &e_bits[wp], n_e, rv)) { fprintf(stderr, "Error in rate matching\n"); return SRSLTE_ERROR; } /* Set read/write pointers */ rp += (rlen - F); wp += n_e; } INFO("END CB#%d: wp: %d, rp: %d\n", i, wp, rp); ret = SRSLTE_SUCCESS; }
int main(int argc, char **argv) { int i; uint8_t *rm_bits, *rm_bits2, *rm_bits2_bytes; short *rm_bits_s; float *rm_bits_f; parse_args(argc, argv); srslte_rm_turbo_gentables(); rm_bits_s = srslte_vec_malloc(sizeof(short) * nof_e_bits); if (!rm_bits_s) { perror("malloc"); exit(-1); } rm_bits_f = srslte_vec_malloc(sizeof(float) * nof_e_bits); if (!rm_bits_f) { perror("malloc"); exit(-1); } rm_bits = srslte_vec_malloc(sizeof(uint8_t) * nof_e_bits); if (!rm_bits) { perror("malloc"); exit(-1); } rm_bits2 = malloc(sizeof(uint8_t) * nof_e_bits); if (!rm_bits2) { perror("malloc"); exit(-1); } rm_bits2_bytes = malloc(sizeof(uint8_t) * nof_e_bits/8 + 1); if (!rm_bits2_bytes) { perror("malloc"); exit(-1); } uint32_t st=0, end=188; if (cb_idx != -1) { st=cb_idx; end=cb_idx+1; } uint32_t rv_st=0, rv_end=4; if (rv_idx != -1) { rv_st=rv_idx; rv_end=rv_idx+1; } for (cb_idx=st;cb_idx<end;cb_idx++) { for (rv_idx=rv_st;rv_idx<rv_end;rv_idx++) { uint32_t long_cb_enc = 3*srslte_cbsegm_cbsize(cb_idx)+12; printf("checking cb_idx=%3d rv_idx=%d...", cb_idx, rv_idx); for (i = 0; i < long_cb_enc; i++) { bits[i] = rand() % 2; } bzero(buff_b, BUFFSZ * sizeof(uint8_t)); srslte_rm_turbo_tx(buff_b, BUFFSZ, bits, long_cb_enc, rm_bits, nof_e_bits, 0); if (rv_idx > 0) { srslte_rm_turbo_tx(buff_b, BUFFSZ, bits, long_cb_enc, rm_bits, nof_e_bits, rv_idx); } for (int i=0;i<long_cb_enc/3;i++) { systematic[i] = bits[3*i]; parity[i] = bits[3*i+1]; parity[i+long_cb_enc/3] = bits[3*i+2]; } srslte_bit_pack_vector(systematic, systematic_bytes, long_cb_enc/3); srslte_bit_pack_vector(parity, parity_bytes, 2*long_cb_enc/3); bzero(buff_b, BUFFSZ * sizeof(uint8_t)); bzero(rm_bits2_bytes, nof_e_bits/8); srslte_rm_turbo_tx_lut(buff_b, systematic_bytes, parity_bytes, rm_bits2_bytes, cb_idx, nof_e_bits, 0, 0); if (rv_idx > 0) { bzero(rm_bits2_bytes, nof_e_bits/8); srslte_rm_turbo_tx_lut(buff_b, systematic_bytes, parity_bytes, rm_bits2_bytes, cb_idx, nof_e_bits, 0, rv_idx); } srslte_bit_unpack_vector(rm_bits2_bytes, rm_bits2, nof_e_bits); for (int i=0;i<nof_e_bits;i++) { if (rm_bits2[i] != rm_bits[i]) { printf("Error in TX bit %d\n", i); exit(-1); } } printf("OK TX..."); for (int i=0;i<nof_e_bits;i++) { rm_bits_f[i] = rand()%10-5; rm_bits_s[i] = (short) rm_bits_f[i]; } bzero(buff_f, BUFFSZ*sizeof(float)); srslte_rm_turbo_rx(buff_f, BUFFSZ, rm_bits_f, nof_e_bits, bits_f, long_cb_enc, rv_idx, 0); bzero(bits2_s, long_cb_enc*sizeof(short)); srslte_rm_turbo_rx_lut(rm_bits_s, bits2_s, nof_e_bits, cb_idx, rv_idx); for (int i=0;i<long_cb_enc;i++) { if (bits_f[i] != bits2_s[i]) { printf("error RX in bit %d %f!=%d\n", i, bits_f[i], bits2_s[i]); exit(-1); } } printf("OK RX\n"); } } free(rm_bits); free(rm_bits2); free(rm_bits2_bytes); exit(0); }