lte_segmentation.c 6.98 KB
Newer Older
1
/*******************************************************************************
ghaddab's avatar
ghaddab committed
2
3
    OpenAirInterface 
    Copyright(c) 1999 - 2014 Eurecom
4

ghaddab's avatar
ghaddab committed
5
6
7
8
    OpenAirInterface 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.
9
10


ghaddab's avatar
ghaddab committed
11
12
13
14
    OpenAirInterface 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.
15

ghaddab's avatar
ghaddab committed
16
17
18
19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is 
   included in this distribution in the file called "COPYING". If not, 
   see <http://www.gnu.org/licenses/>.
20
21

  Contact Information
ghaddab's avatar
ghaddab committed
22
23
24
25
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr
  
ghaddab's avatar
ghaddab committed
26
  Address      : Eurecom, Campus SophiaTech, 450 Route des Chappes, CS 50193 - 06904 Biot Sophia Antipolis cedex, FRANCE
27

ghaddab's avatar
ghaddab committed
28
 *******************************************************************************/
29
30
31
32
33
34
/* file: lte_segmentation.c
   purpose: Procedures for transport block segmentation for LTE (turbo-coded transport channels) 
   author: raymond.knopp@eurecom.fr
   date: 21.10.2009 
*/
#include "PHY/defs.h"
35
#include "SCHED/extern.h"
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

//#define DEBUG_SEGMENTATION

int lte_segmentation(unsigned char *input_buffer,
		      unsigned char **output_buffers,
		      unsigned int B,
		      unsigned int *C,
		      unsigned int *Cplus,
		      unsigned int *Cminus,
		      unsigned int *Kplus,
		      unsigned int *Kminus,
		      unsigned int *F) {

  unsigned int L,Bprime,Bprime_by_C,r,Kr,k,s,crc;

  if (B<=6144) {
    L=0;
    *C=1;
    Bprime=B;
  }
  else {
    L=24;
    *C = B/(6144-L);
    if ((6144-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) {
    msg("lte_segmentation.c: too many segments %d\n",*C);
    return(-1);
  }

  // Find K+
  Bprime_by_C  = Bprime/(*C);
#ifdef DEBUG_SEGMENTATION
  printf("Bprime_by_C %d\n",Bprime_by_C);
#endif
    //  Bprime = Bprime_by_C>>3;

  if (Bprime_by_C <= 40) {
    *Kplus = 40;
    *Kminus = 0;
  }
  else if (Bprime_by_C<=512) { // increase by 1 byte til here
    *Kplus = (Bprime_by_C>>3)<<3;
    *Kminus = Bprime_by_C-8;
  }
  else if (Bprime_by_C <=1024) {// increase by 2 bytes til here
    *Kplus = (Bprime_by_C>>4)<<4;
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 16;
    *Kminus = (*Kplus - 16);
  }
  else if (Bprime_by_C <= 2048) { // increase by 4 bytes til here
    *Kplus = (Bprime_by_C>>5)<<5;
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 32;
    *Kminus = (*Kplus - 32);
  }
  else if (Bprime_by_C <=6144 ) { // increase by 8 bytes til here

   *Kplus = (Bprime_by_C>>6)<<6;
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus %d\n",Bprime_by_C,*Kplus);
#endif 
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 64;
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus2 %d\n",Bprime_by_C,*Kplus);
#endif
    *Kminus = (*Kplus - 64);
  }
  else {
    msg("lte_segmentation.c: Illegal codeword size !!!\n");
    return(-1);
  }  

  if (*C == 1) {
    *Cplus = *C;
    *Kminus = 0;
    *Cminus = 0;
  }
  else {

    //    printf("More than one segment (%d), exiting \n",*C);
    //    exit(-1);
    *Cminus = ((*C)*(*Kplus) - (Bprime))/((*Kplus) - (*Kminus));
    *Cplus  = (*C) - (*Cminus);
  }



  *F = ((*Cplus)*(*Kplus) + (*Cminus)*(*Kminus) - (Bprime));
#ifdef DEBUG_SEGMENTATION
  printf("C %d, Cplus %d, Cminus %d, Kplus %d, Kminus %d, Bprime_bytes %d, Bprime %d, F %d\n",*C,*Cplus,*Cminus,*Kplus,*Kminus,Bprime>>3,Bprime,*F);
#endif
  if ((input_buffer) && (output_buffers)) {

    for (k=0;k<*F>>3;k++) {
      output_buffers[0][k] = 0;
    } 
    s=0;
    for (r=0;r<*C;r++) {

      if (r<*Cminus)
	Kr = *Kminus;
      else
	Kr = *Kplus;

      while (k<((Kr - L)>>3)) {
	output_buffers[r][k] = input_buffer[s];
	//	printf("encoding segment %d : byte %d => %d\n",r,k,input_buffer[s]);
	k++;
	s++;
      }

      if (*C > 1) { // add CRC
	crc = crc24b(output_buffers[r],Kr-24)>>8;
gauthier's avatar
gauthier committed
157
158
159
	output_buffers[r][(Kr-24)>>3] = ((uint8_t*)&crc)[2];
	output_buffers[r][1+((Kr-24)>>3)] = ((uint8_t*)&crc)[1];
	output_buffers[r][2+((Kr-24)>>3)] = ((uint8_t*)&crc)[0];
160
161
162
163
164
165
166
167
168
169
#ifdef DEBUG_SEGMENTATION
	printf("Segment %d : CRC %x\n",r,crc);
#endif
      }
      k=0;
    }
  }
  return(0);
}

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
// uint8_t eNB_id,uint8_t harq_pid, uint8_t UE_id,
int16_t estimate_ue_tx_power(uint32_t tbs, uint32_t nb_rb, uint8_t control_only, lte_prefix_type_t ncp, uint8_t use_srs){

  /// The payload + CRC size in bits, "B" 
  uint32_t B;  
  /// Number of code segments 
  uint32_t C;                         
  /// Number of "small" code segments 
  uint32_t Cminus;                    
  /// Number of "large" code segments 
  uint32_t Cplus;                     
  /// Number of bits in "small" code segments (<6144) 
  uint32_t Kminus;                    
  /// Number of bits in "large" code segments (<6144) 
  uint32_t Kplus;                     
  /// Total number of bits across all segments
  uint32_t sumKr;
  /// Number of "Filler" bits 
  uint32_t F;                         
  // num resource elements
  uint32_t num_re=0.0;
  // num symbols
  uint32_t num_symb=0.0;
  /// effective spectral efficiency of the PUSCH
  uint32_t MPR_x100=0; 
  /// beta_offset
  uint16_t beta_offset_pusch_x8=8;
  /// delta mcs
  float delta_mcs=0.0;
  /// bandwidth factor 
  float bw_factor=0.0;

  B= tbs+24;
  lte_segmentation(NULL,
		   NULL,
		   B,
		   &C,
		   &Cplus,
		   &Cminus,
		   &Kplus,
		   &Kminus,		     
		   &F);
 

  sumKr = Cminus*Kminus + Cplus*Kplus;    
  num_symb = 12-(ncp<<1)-(use_srs==0?0:1);
  num_re = num_symb * nb_rb * 12;
  
  if (num_re == 0)
    return(0);

  MPR_x100 = 100*sumKr/num_re;
  if (control_only == 1 )
    beta_offset_pusch_x8=8; // fixme
  //(beta_offset_pusch_x8=phy_vars_ue->ulsch_ue[eNB_id]->harq_processes[harq_pid]->control_only == 1) ? phy_vars_ue->ulsch_ue[eNB_id]->beta_offset_cqi_times8:8;

  // if deltamcs_enabledm
  delta_mcs = ((hundred_times_delta_TF[MPR_x100/6]+10*dB_fixed_times10((beta_offset_pusch_x8)>>3))/100.0);
  bw_factor = (hundred_times_log10_NPRB[nb_rb-1]/100.0);
#ifdef DEBUG_SEGMENTATION
 printf("estimated ue tx power %d (num_re %d, sumKr %d, mpr_x100 %d, delta_mcs %f, bw_factor %f)\n", 
	(int16_t)ceil(delta_mcs + bw_factor), num_re, sumKr, MPR_x100, delta_mcs, bw_factor);
#endif 
  return (int16_t)ceil(delta_mcs + bw_factor);
  
}
236
237
238
239
240
241
242
243
244
245
246
247
248

#ifdef MAIN
main() {

  unsigned int Kplus,Kminus,C,Cplus,Cminus,F,Bbytes;
  
  for (Bbytes=5;Bbytes<2*768;Bbytes++) {
    lte_segmentation(0,0,Bbytes<<3,&C,&Cplus,&Cminus,&Kplus,&Kminus,&F);
    printf("Bbytes %d : C %d, Cplus %d, Cminus %d, Kplus %d, Kminus %d, F %d\n",
	   Bbytes, C, Cplus, Cminus, Kplus, Kminus, F);
  }
}
#endif