lte_segmentation.c 5.11 KB
Newer Older
1
/*******************************************************************************
2
    OpenAirInterface
ghaddab's avatar
ghaddab committed
3
    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
    You should have received a copy of the GNU General Public License
17 18
    along with OpenAirInterface.The full GNU General Public License is
   included in this distribution in the file called "COPYING". If not,
ghaddab's avatar
ghaddab committed
19
   see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
ghaddab's avatar
ghaddab committed
22 23
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
24
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr
25

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

//#define DEBUG_SEGMENTATION

int lte_segmentation(unsigned char *input_buffer,
40 41 42 43 44 45 46 47 48
                     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)
{
49 50 51 52 53 54 55

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

  if (B<=6144) {
    L=0;
    *C=1;
    Bprime=B;
56
  } else {
57 58
    L=24;
    *C = B/(6144-L);
59

60 61
    if ((6144-L)*(*C) < B)
      *C=*C+1;
62 63

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

69 70 71 72 73 74 75 76 77 78
  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
79
  //  Bprime = Bprime_by_C>>3;
80 81 82 83

  if (Bprime_by_C <= 40) {
    *Kplus = 40;
    *Kminus = 0;
84
  } else if (Bprime_by_C<=512) { // increase by 1 byte til here
85 86
    *Kplus = (Bprime_by_C>>3)<<3;
    *Kminus = Bprime_by_C-8;
87
  } else if (Bprime_by_C <=1024) { // increase by 2 bytes til here
88
    *Kplus = (Bprime_by_C>>4)<<4;
89

90 91
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 16;
92

93
    *Kminus = (*Kplus - 16);
94
  } else if (Bprime_by_C <= 2048) { // increase by 4 bytes til here
95
    *Kplus = (Bprime_by_C>>5)<<5;
96

97 98
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 32;
99

100
    *Kminus = (*Kplus - 32);
101
  } else if (Bprime_by_C <=6144 ) { // increase by 8 bytes til here
102

103
    *Kplus = (Bprime_by_C>>6)<<6;
104 105
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus %d\n",Bprime_by_C,*Kplus);
106 107
#endif

108 109
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 64;
110

111 112 113 114
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus2 %d\n",Bprime_by_C,*Kplus);
#endif
    *Kminus = (*Kplus - 64);
115
  } else {
116 117
    msg("lte_segmentation.c: Illegal codeword size !!!\n");
    return(-1);
118
  }
119 120 121 122 123

  if (*C == 1) {
    *Cplus = *C;
    *Kminus = 0;
    *Cminus = 0;
124
  } else {
125 126 127 128 129 130 131 132 133 134 135 136 137

    //    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
138

139 140
  if ((input_buffer) && (output_buffers)) {

141
    for (k=0; k<*F>>3; k++) {
142
      output_buffers[0][k] = 0;
143 144
    }

145
    s=0;
146 147

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

      if (r<*Cminus)
150
        Kr = *Kminus;
151
      else
152
        Kr = *Kplus;
153 154

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

      if (*C > 1) { // add CRC
162 163 164 165
        crc = crc24b(output_buffers[r],Kr-24)>>8;
        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];
166
#ifdef DEBUG_SEGMENTATION
167
        printf("Segment %d : CRC %x\n",r,crc);
168 169
#endif
      }
170

171 172 173
      k=0;
    }
  }
174

175 176 177
  return(0);
}

178

179 180

#ifdef MAIN
181 182
main()
{
183 184

  unsigned int Kplus,Kminus,C,Cplus,Cminus,F,Bbytes;
185 186

  for (Bbytes=5; Bbytes<2*768; Bbytes++) {
187 188
    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",
189
           Bbytes, C, Cplus, Cminus, Kplus, Kminus, F);
190 191 192
  }
}
#endif