aboutsummaryrefslogtreecommitdiffstats
path: root/src/l1/punct.c
blob: 3c0f7f7fa03ecc72e1ea0421ee1a59837b23b6ce (plain)
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
27
28
29
30
31
32
33
34
35
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
157
158
159
160
161
162
163
164
165
166
167
168
169
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* GMR-1 puncturing */
/* See GMR-1 05.003 (ETSI TS 101 376-5-3 V1.2.1) - Section 4.5 */

/* (C) 2011-2016 by Sylvain Munaut <tnt@246tNt.com>
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/*! \addtogroup punct
 *  @{
 */

/*! \file l1/punct.c
 *  \brief Osmocom GMR-1 puncturing implementation
 */

#include <osmocom/core/conv.h>

#include <osmocom/gmr1/l1/punct.h>

#include <errno.h>
#include <stdlib.h>


/*! \brief Generate convolutional code puncturing array for a osmo_conv_code
 *  \param[inout] code The code for which to generate the puncturing array
 *  \param[in] punct_pre The puncturing scheme for first block (can be NULL)
 *  \param[in] punct_main The puncturing scheme
 *  \param[in] punct_post The puncturing scheme for last block (can be NULL)
 *  \param[in] repeat How many time to apply main punctured (0 = auto)
 *  \return 0 for success, <0 for error codes.
 *
 * The array is allocated with malloc and must be free'd by the caller
 * when no longer required.
 */
int
gmr1_puncturer_generate(struct osmo_conv_code *code,
                        const struct gmr1_puncturer *punct_pre,
                        const struct gmr1_puncturer *punct_main,
                        const struct gmr1_puncturer *punct_post,
			int repeat)
{
	int N, d, cl, pl;
	int ip, ii, io, i;
	int *p;

	/* Safety checks */
	N = code->N;

	if (punct_pre && (punct_pre->N != N))
		return -EINVAL;

	if (punct_main->N != N)
		return -EINVAL;

	if (punct_post && (punct_post->N != N))
		return -EINVAL;

	/* Upper bound for length */
	cl = osmo_conv_get_output_length(code, 0);
	pl = 0;

	if (punct_pre) {
		cl -= punct_pre->L * N;
		pl += punct_pre->r;
	}

	if (punct_post) {
		cl -= punct_post->L * N;
		pl += punct_post->r;
	}

	d = punct_main->L * N;

	if (!repeat)
		repeat = ((cl + d - 1) / d);

	pl += repeat * punct_main->r + 1;

	/* Alloc array */
	p = malloc(pl * sizeof(int));
	if (!p)
		return -ENOMEM;

	/* Fill */
	cl = osmo_conv_get_output_length(code, 0);
	ii = io = 0;

	if (punct_pre) {
		d = punct_pre->L * N;
		for (ip=0; ii<cl && ip<d ; ii++,ip++)
			if (punct_pre->mask[ip] == 0)
				p[io++] = ii;
	}

	if (punct_post) {
		cl -= punct_post->L * N;
	}

	for (i=0; i<repeat; i++) {
		d = punct_main->L * N;
		for (ip=0; ii<cl && ip<d; ii++,ip++) {
			if (punct_main->mask[ip] == 0)
				p[io++] = ii;
		}
	}

	if (punct_post) {
		d = punct_post->L * N;
		ii = cl;
		for (ip=0; ii>0 && ip<d; ii++,ip++)
			if (punct_post->mask[ip] == 0)
				p[io++] = ii;
	}

	p[io] = -1;

	code->puncture = p;

	return 0;
}


/*! \brief GMR-1 P(1;2) puncturing code for the rate 1/2 conv coder */
const struct gmr1_puncturer gmr1_punct12_P12 = {
	.r = 1,
	.L = 2,
	.N = 2,
	.mask = {
		1, 1,
		1, 0,
	},
};

/*! \brief GMR-1 P(2;3) puncturing code for the rate 1/2 conv coder */
const struct gmr1_puncturer gmr1_punct12_P23 = {
	.r = 2,
	.L = 3,
	.N = 2,
	.mask = {
		0, 1,
		1, 0,
		1, 1,
	},
};

/*! \brief GMR-1 P(2;5) puncturing code for the rate 1/2 conv coder */
const struct gmr1_puncturer gmr1_punct12_P25 = {
	.r = 2,
	.L = 5,
	.N = 2,
	.mask = {
		1, 0,
		1, 1,
		1, 0,
		1, 1,
		1, 1,
	},
};

/*! \brief GMR-1 P*(2;5) puncturing code for the rate 1/2 conv coder */
const struct gmr1_puncturer gmr1_punct12_Ps25 = {
	.r = 2,
	.L = 5,
	.N = 2,
	.mask = {
		1, 1,
		1, 1,
		1, 0,
		1, 1,
		1, 0,
	},
};

/*! \brief GMR-1 P(3;11) puncturing code for the rate 1/2 conv coder */
const struct gmr1_puncturer gmr1_punct12_P311 = {
	.r = 3,
	.L = 11,
	.N = 2,
	.mask = {
		1, 0,
		1, 1,
		1, 0,
		1, 1,
		1, 1,
		1, 0,
		1, 1,
		1, 1,
		1, 1,
		1, 1,
		1, 1,
	},
};

/*! \brief GMR-1 P(4;12) puncturing code for the rate 1/2 conv coder */
const struct gmr1_puncturer gmr1_punct12_P412 = {
	.r = 4,
	.L = 12,
	.N = 2,
	.mask = {
		1, 0,
		1, 1,
		1, 0,
		1, 1,
		1, 0,
		1, 1,
		1, 0,
		1, 1,
		1, 1,
		1, 1,
		1, 1,
		1, 1,
	},
};

/*! \brief GMR-1 P*(4;12) puncturing code for the rate 1/2 conv coder */
const struct gmr1_puncturer gmr1_punct12_Ps412 = {
	.r = 4,
	.L = 12,
	.N = 2,
	.mask = {
		1, 1,
		1, 1,
		1, 1,
		1, 1,
		1, 1,
		1, 0,
		1, 1,
		1, 0,
		1, 1,
		1, 0,
		1, 1,
		1, 0,
	},
};

/*! \brief GMR-1 P(2;5) puncturing code for the rate 1/3 conv coder */
const struct gmr1_puncturer gmr1_punct13_P25 = {
	.r = 2,
	.L = 5,
	.N = 3,
	.mask = {
		1, 1, 1,
		1, 1, 1,
		1, 0, 1,
		1, 1, 1,
		1, 0, 1,
	},
};

/*! \brief GMR-1 P(1;5) puncturing code for the rate 1/3 conv coder */
const struct gmr1_puncturer gmr1_punct13_P15 = {
	.r = 1,
	.L = 5,
	.N = 3,
	.mask = {
		1, 0, 1,
		1, 1, 1,
		1, 1, 1,
		1, 1, 1,
		1, 1, 1,
	},
};

/*! \brief GMR-1 P*(1;5) puncturing code for the rate 1/3 conv coder */
const struct gmr1_puncturer gmr1_punct13_Ps15 = {
	.r = 1,
	.L = 5,
	.N = 3,
	.mask = {
		1, 1, 1,
		1, 1, 1,
		1, 1, 1,
		1, 1, 1,
		1, 0, 1,
	},
};

/*! \brief GMR-1 P(1;6) puncturing code for the rate 1/3 conv coder */
const struct gmr1_puncturer gmr1_punct13_P16 = {
	.r = 1,
	.L = 6,
	.N = 3,
	.mask = {
		1, 1, 0,
		1, 1, 1,
		1, 1, 1,
		1, 1, 1,
		1, 1, 1,
		1, 1, 1,
	},
};

/*! \brief GMR-1 P(2;3) puncturing code for the rate 1/5 conv coder */
const struct gmr1_puncturer gmr1_punct15_P23 = {
	.r = 2,
	.L = 3,
	.N = 5,
	.mask = {
		1, 1, 1, 1, 1,
		1, 1, 0, 1, 1,
		1, 1, 1, 1, 0,
	},
};

/*! \brief GMR-1 P(5;3) puncturing code for the rate 1/5 conv coder */
const struct gmr1_puncturer gmr1_punct15_P53 = {
	.r = 5,
	.L = 3,
	.N = 5,
	.mask = {
		1, 1, 1, 0, 1,
		1, 0, 0, 1, 1,
		1, 1, 1, 0, 0,
	},
};

/*! \brief GMR-1 P*(5;3) puncturing code for the rate 1/5 conv coder */
const struct gmr1_puncturer gmr1_punct15_Ps53 = {
	.r = 5,
	.L = 3,
	.N = 5,
	.mask = {
		1, 1, 1, 0, 0,
		1, 0, 0, 1, 1,
		1, 1, 1, 0, 1,
	},
};

/*! @} */