nr_segmentation.c 4.6 KB
Newer Older
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
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

/* file: nr_segmentation.c
   purpose: Procedures for transport block segmentation for NR (LDPC-coded transport channels)
   author: Hongzhi WANG (TCL)
   date: 12.09.2017
*/
27
#include "PHY/defs_nr_UE.h"
28
//#include "SCHED/extern.h"
29 30 31 32 33 34 35

//#define DEBUG_SEGMENTATION

int32_t nr_segmentation(unsigned char *input_buffer,
                     unsigned char **output_buffers,
                     unsigned int B,
                     unsigned int *C,
Hongzhi Wang's avatar
Hongzhi Wang committed
36
                     unsigned int *K,
Ahmed's avatar
Ahmed committed
37
                     unsigned int *Zout, // [hna] Zout is Zc
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
                     unsigned int *F)
{

  unsigned int L,Bprime,Bprime_by_C,Z,r,Kb,k,s,crc,Kprime;

  if (B<=8448) {
    L=0;
    *C=1;
    Bprime=B;
  } else {
    L=24;
    *C = B/(8448-L);

    if ((8448-L)*(*C) < B)
      *C=*C+1;

    Bprime = B+((*C)*L);
#ifdef DEBUG_SEGMENTATION
    printf("Bprime %d\n",Bprime);
#endif
  }

  if ((*C)>MAX_NUM_DLSCH_SEGMENTS) {
Hongzhi Wang's avatar
Hongzhi Wang committed
61
      LOG_E(PHY,"nr_segmentation.c: too many segments %d, B %d, L %d, Bprime %d\n",*C,B,L,Bprime);
62 63 64 65 66
    return(-1);
  }

  // Find K+
  Bprime_by_C = Bprime/(*C);
Hongzhi Wang's avatar
Hongzhi Wang committed
67
  if (Bprime <=192) {
68 69 70 71 72 73 74
	  Kb = 6;
  } else if (Bprime <=560) {
	  Kb = 8;
  } else if (Bprime <=640) {
	  Kb = 9;
  } else if (Bprime <=3840) {
	  Kb = 10;;
Hongzhi Wang's avatar
Hongzhi Wang committed
75
  } else {
76
	  Kb = 22;
Hongzhi Wang's avatar
Hongzhi Wang committed
77
  }
78 79 80 81 82 83 84 85 86 87 88 89


if ((Bprime_by_C%Kb) > 0)
  Z  = (Bprime_by_C/Kb)+1;
else
	Z = (Bprime_by_C/Kb);

#ifdef DEBUG_SEGMENTATION
 printf("nr segmetation B %d Bprime %d Bprime_by_C %d z %d \n", B, Bprime, Bprime_by_C, Z);
#endif
	  
  if (Z <= 2) {
Hongzhi Wang's avatar
Hongzhi Wang committed
90
    *K = 2;
91
  } else if (Z<=16) { // increase by 1 byte til here
Hongzhi Wang's avatar
Hongzhi Wang committed
92
    *K = Z;
93
  } else if (Z <=32) { // increase by 2 bytes til here
Hongzhi Wang's avatar
Hongzhi Wang committed
94
    *K = (Z>>1)<<1;
95

Hongzhi Wang's avatar
Hongzhi Wang committed
96 97
    if (*K < Z)
      *K = *K + 2;
98 99

  } else if (Z <= 64) { // increase by 4 bytes til here
Hongzhi Wang's avatar
Hongzhi Wang committed
100
    *K = (Z>>2)<<2;
101

Hongzhi Wang's avatar
Hongzhi Wang committed
102 103
    if (*K < Z)
      *K = *K + 4;
104 105 106

  } else if (Z <=128 ) { // increase by 8 bytes til here

Hongzhi Wang's avatar
Hongzhi Wang committed
107
    *K = (Z>>3)<<3;
108

Hongzhi Wang's avatar
Hongzhi Wang committed
109 110
    if (*K < Z)
      *K = *K + 8;
111 112

#ifdef DEBUG_SEGMENTATION
Hongzhi Wang's avatar
Hongzhi Wang committed
113
    printf("Z_by_C %d , K2 %d\n",Z,*K);
114 115
#endif
  } else if (Z <= 256) { // increase by 4 bytes til here
Hongzhi Wang's avatar
Hongzhi Wang committed
116
      *K = (Z>>4)<<4;
117

Hongzhi Wang's avatar
Hongzhi Wang committed
118 119
      if (*K < Z)
        *K = *K + 16;
120 121

  } else if (Z <= 384) { // increase by 4 bytes til here
Hongzhi Wang's avatar
Hongzhi Wang committed
122
      *K = (Z>>5)<<5;
123

Hongzhi Wang's avatar
Hongzhi Wang committed
124 125
      if (*K < Z)
        *K = *K + 32;
126 127 128

  } else {
    //msg("nr_segmentation.c: Illegal codeword size !!!\n");
129
    return -1;
130
  }
Hongzhi Wang's avatar
Hongzhi Wang committed
131 132
  *Zout = *K;
  *K = *K*Kb;
133

Hongzhi Wang's avatar
Hongzhi Wang committed
134
  *F = ((*K) - Bprime_by_C);
135 136

#ifdef DEBUG_SEGMENTATION
Hongzhi Wang's avatar
Hongzhi Wang committed
137 138
  printf("final nr seg output Z %d K %d F %d \n", *Zout, *K, *F);
  printf("C %d, K %d, Bprime_bytes %d, Bprime %d, F %d\n",*C,*K,Bprime>>3,Bprime,*F);
139 140 141 142
#endif

  if ((input_buffer) && (output_buffers)) {

143
    s = 0;
144 145 146

    for (r=0; r<*C; r++) {

147
      k = 0;
Hongzhi Wang's avatar
Hongzhi Wang committed
148
      Kprime = Bprime_by_C;
149 150 151

      while (k<((Kprime - L)>>3)) {
        output_buffers[r][k] = input_buffer[s];
Hongzhi Wang's avatar
Hongzhi Wang committed
152
		//printf("encoding segment %d : byte %d (%d) => %d\n",r,k,(Kprime-L)>>3,input_buffer[s]);
153 154 155 156
        k++;
        s++;
      }

157 158
      if (*F>0) {
        for (k=Kprime>>3; k<(*K)>>3; k++) {
159
          output_buffers[r][k] = 0;
Hongzhi Wang's avatar
Hongzhi Wang committed
160
          //printf("r %d filler bits [%d] = %d Kprime %d \n", r,k, output_buffers[r][k], Kprime);
161
        }
Hongzhi Wang's avatar
Hongzhi Wang committed
162 163
      }

164
      if (*C > 1) { // add CRC
Hongzhi Wang's avatar
Hongzhi Wang committed
165 166 167 168
        crc = crc24b(output_buffers[r],Kprime-L)>>8;
        output_buffers[r][(Kprime-L)>>3] = ((uint8_t*)&crc)[2];
        output_buffers[r][1+((Kprime-L)>>3)] = ((uint8_t*)&crc)[1];
        output_buffers[r][2+((Kprime-L)>>3)] = ((uint8_t*)&crc)[0];
169 170 171 172
      }
    }
  }

173
  return 0;
174 175 176 177 178 179 180 181
}



#ifdef MAIN
main()
{

Hongzhi Wang's avatar
Hongzhi Wang committed
182
  unsigned int K,C,F,Bbytes, Zout;
183 184

  for (Bbytes=5; Bbytes<8; Bbytes++) {
Hongzhi Wang's avatar
Hongzhi Wang committed
185 186 187
    nr_segmentation(0,0,Bbytes<<3,&C,&K,&Zout, &F);
    printf("Bbytes %d : C %d, K %d, F %d\n",
           Bbytes, C, K, F);
188 189 190
  }
}
#endif