lte_segmentation.c 4.91 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * 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.0  (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
 */

22
/* file: lte_segmentation.c
23
   purpose: Procedures for transport block segmentation for LTE (turbo-coded transport channels)
24
   author: raymond.knopp@eurecom.fr
25
   date: 21.10.2009
26 27
*/
#include "PHY/defs.h"
28
#include "SCHED/extern.h"
29 30 31 32

//#define DEBUG_SEGMENTATION

int lte_segmentation(unsigned char *input_buffer,
33 34 35 36 37 38 39 40 41
                     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)
{
42 43 44 45 46 47 48

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

  if (B<=6144) {
    L=0;
    *C=1;
    Bprime=B;
49
  } else {
50 51
    L=24;
    *C = B/(6144-L);
52

53 54
    if ((6144-L)*(*C) < B)
      *C=*C+1;
55 56

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

62
  if ((*C)>MAX_NUM_DLSCH_SEGMENTS) {
63
      LOG_E(PHY,"lte_segmentation.c: too many segments %d, B %d, L %d, Bprime %d\n",*C,B,L,Bprime);
64 65 66 67 68 69 70 71
    return(-1);
  }

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

  if (Bprime_by_C <= 40) {
    *Kplus = 40;
    *Kminus = 0;
77
  } else if (Bprime_by_C<=512) { // increase by 1 byte til here
78 79
    *Kplus = (Bprime_by_C>>3)<<3;
    *Kminus = Bprime_by_C-8;
80
  } else if (Bprime_by_C <=1024) { // increase by 2 bytes til here
81
    *Kplus = (Bprime_by_C>>4)<<4;
82

83 84
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 16;
85

86
    *Kminus = (*Kplus - 16);
87
  } else if (Bprime_by_C <= 2048) { // increase by 4 bytes til here
88
    *Kplus = (Bprime_by_C>>5)<<5;
89

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

93
    *Kminus = (*Kplus - 32);
94
  } else if (Bprime_by_C <=6144 ) { // increase by 8 bytes til here
95

96
    *Kplus = (Bprime_by_C>>6)<<6;
97 98
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus %d\n",Bprime_by_C,*Kplus);
99 100
#endif

101 102
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 64;
103

104 105 106 107
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus2 %d\n",Bprime_by_C,*Kplus);
#endif
    *Kminus = (*Kplus - 64);
108
  } else {
109 110
    msg("lte_segmentation.c: Illegal codeword size !!!\n");
    return(-1);
111
  }
112 113 114 115 116

  if (*C == 1) {
    *Cplus = *C;
    *Kminus = 0;
    *Cminus = 0;
117
  } else {
118 119 120 121 122 123 124 125 126 127 128 129 130

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

132 133
  if ((input_buffer) && (output_buffers)) {

134
    for (k=0; k<*F>>3; k++) {
135
      output_buffers[0][k] = 0;
136 137
    }

138
    s=0;
139 140

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

      if (r<*Cminus)
143
        Kr = *Kminus;
144
      else
145
        Kr = *Kplus;
146 147

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

      if (*C > 1) { // add CRC
155 156 157 158
        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];
159
#ifdef DEBUG_SEGMENTATION
160
        printf("Segment %d : CRC %x\n",r,crc);
161 162
#endif
      }
163

164 165 166
      k=0;
    }
  }
167

168 169 170
  return(0);
}

171

172 173

#ifdef MAIN
174 175
main()
{
176 177

  unsigned int Kplus,Kminus,C,Cplus,Cminus,F,Bbytes;
178 179

  for (Bbytes=5; Bbytes<2*768; Bbytes++) {
180 181
    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",
182
           Bbytes, C, Cplus, Cminus, Kplus, Kminus, F);
183 184 185
  }
}
#endif