lte_segmentation.c 3.84 KB
Newer Older
1
/* file: lte_segmentation.c
2
   purpose: Procedures for transport block segmentation for LTE (turbo-coded transport channels)
3
   author: raymond.knopp@eurecom.fr
4
   date: 21.10.2009
5
6
*/
#include "PHY/defs.h"
7
#include "SCHED/extern.h"
8
9
10
11

//#define DEBUG_SEGMENTATION

int lte_segmentation(unsigned char *input_buffer,
12
13
14
15
16
17
18
19
20
                     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)
{
21
22
23
24
25
26
27

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

  if (B<=6144) {
    L=0;
    *C=1;
    Bprime=B;
28
  } else {
29
30
    L=24;
    *C = B/(6144-L);
31

32
33
    if ((6144-L)*(*C) < B)
      *C=*C+1;
34
35

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

41
42
43
44
45
46
47
48
49
50
  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
51
  //  Bprime = Bprime_by_C>>3;
52
53
54
55

  if (Bprime_by_C <= 40) {
    *Kplus = 40;
    *Kminus = 0;
56
  } else if (Bprime_by_C<=512) { // increase by 1 byte til here
57
58
    *Kplus = (Bprime_by_C>>3)<<3;
    *Kminus = Bprime_by_C-8;
59
  } else if (Bprime_by_C <=1024) { // increase by 2 bytes til here
60
    *Kplus = (Bprime_by_C>>4)<<4;
61

62
63
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 16;
64

65
    *Kminus = (*Kplus - 16);
66
  } else if (Bprime_by_C <= 2048) { // increase by 4 bytes til here
67
    *Kplus = (Bprime_by_C>>5)<<5;
68

69
70
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 32;
71

72
    *Kminus = (*Kplus - 32);
73
  } else if (Bprime_by_C <=6144 ) { // increase by 8 bytes til here
74

75
    *Kplus = (Bprime_by_C>>6)<<6;
76
77
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus %d\n",Bprime_by_C,*Kplus);
78
79
#endif

80
81
    if (*Kplus < Bprime_by_C)
      *Kplus = *Kplus + 64;
82

83
84
85
86
#ifdef DEBUG_SEGMENTATION
    printf("Bprime_by_C_by_C %d , Kplus2 %d\n",Bprime_by_C,*Kplus);
#endif
    *Kminus = (*Kplus - 64);
87
  } else {
88
89
    msg("lte_segmentation.c: Illegal codeword size !!!\n");
    return(-1);
90
  }
91
92
93
94
95

  if (*C == 1) {
    *Cplus = *C;
    *Kminus = 0;
    *Cminus = 0;
96
  } else {
97
98
99
100
101
102
103
104
105
106
107
108
109

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

111
112
  if ((input_buffer) && (output_buffers)) {

113
    for (k=0; k<*F>>3; k++) {
114
      output_buffers[0][k] = 0;
115
116
    }

117
    s=0;
118
119

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

      if (r<*Cminus)
122
        Kr = *Kminus;
123
      else
124
        Kr = *Kplus;
125
126

      while (k<((Kr - L)>>3)) {
127
        output_buffers[r][k] = input_buffer[s];
knopp's avatar
knopp committed
128
	//	printf("encoding segment %d : byte %d (%d) => %d\n",r,k,Kr>>3,input_buffer[s]);
129
130
        k++;
        s++;
131
132
133
      }

      if (*C > 1) { // add CRC
134
135
136
137
        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];
138
#ifdef DEBUG_SEGMENTATION
139
        printf("Segment %d : CRC %x\n",r,crc);
140
141
#endif
      }
142

143
144
145
      k=0;
    }
  }
146

147
148
149
  return(0);
}

150

151
152

#ifdef MAIN
153
154
main()
{
155
156

  unsigned int Kplus,Kminus,C,Cplus,Cminus,F,Bbytes;
157
158

  for (Bbytes=5; Bbytes<2*768; Bbytes++) {
159
160
    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",
161
           Bbytes, C, Cplus, Cminus, Kplus, Kminus, F);
162
163
164
  }
}
#endif