blob: 21c6a578bb23069f9b16d2bfbfd8f2c11bbe12f1 [file] [log] [blame]
Tom Tsou35536802016-11-24 19:24:32 +07001/*
2 * Viterbi decoder
3 *
4 * Copyright (C) 2013, 2014 Thomas Tsou <tom@tsou.cc>
5 *
6 * All Rights Reserved
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23#include <stdlib.h>
24#include <string.h>
25#include <errno.h>
26
27#include <osmocom/core/conv.h>
28#include "config.h"
29
30#define BIT2NRZ(REG,N) (((REG >> N) & 0x01) * 2 - 1) * -1
31#define NUM_STATES(K) (K == 7 ? 64 : 16)
Tom Tsou35536802016-11-24 19:24:32 +070032
33/* Forward Metric Units */
34void osmo_conv_gen_metrics_k5_n2(const int8_t *seq, const int16_t *out,
35 int16_t *sums, int16_t *paths, int norm);
36void osmo_conv_gen_metrics_k5_n3(const int8_t *seq, const int16_t *out,
37 int16_t *sums, int16_t *paths, int norm);
38void osmo_conv_gen_metrics_k5_n4(const int8_t *seq, const int16_t *out,
39 int16_t *sums, int16_t *paths, int norm);
40void osmo_conv_gen_metrics_k7_n2(const int8_t *seq, const int16_t *out,
41 int16_t *sums, int16_t *paths, int norm);
42void osmo_conv_gen_metrics_k7_n3(const int8_t *seq, const int16_t *out,
43 int16_t *sums, int16_t *paths, int norm);
44void osmo_conv_gen_metrics_k7_n4(const int8_t *seq, const int16_t *out,
45 int16_t *sums, int16_t *paths, int norm);
46
47/* Trellis State
48 * state - Internal lshift register value
49 * prev - Register values of previous 0 and 1 states
50 */
51struct vstate {
52 unsigned state;
53 unsigned prev[2];
54};
55
56/* Trellis Object
57 * num_states - Number of states in the trellis
58 * sums - Accumulated path metrics
59 * outputs - Trellis output values
60 * vals - Input value that led to each state
61 */
62struct vtrellis {
63 int num_states;
64 int16_t *sums;
65 int16_t *outputs;
66 uint8_t *vals;
67};
68
69/* Viterbi Decoder
70 * n - Code order
71 * k - Constraint length
72 * len - Horizontal length of trellis
73 * recursive - Set to '1' if the code is recursive
74 * intrvl - Normalization interval
75 * trellis - Trellis object
76 * punc - Puncturing sequence
77 * paths - Trellis paths
78 */
79struct vdecoder {
80 int n;
81 int k;
82 int len;
83 int recursive;
84 int intrvl;
85 struct vtrellis *trellis;
86 int *punc;
87 int16_t **paths;
88
89 void (*metric_func)(const int8_t *, const int16_t *,
90 int16_t *, int16_t *, int);
91};
92
Vadim Yanitskiye604ee32017-05-01 16:44:02 +070093/* Non-aligned Memory Allocator */
Tom Tsou35536802016-11-24 19:24:32 +070094static int16_t *vdec_malloc(size_t n)
95{
Tom Tsou35536802016-11-24 19:24:32 +070096 return (int16_t *) malloc(sizeof(int16_t) * n);
Tom Tsou35536802016-11-24 19:24:32 +070097}
98
99/* Accessor calls */
100static inline int conv_code_recursive(const struct osmo_conv_code *code)
101{
102 return code->next_term_output ? 1 : 0;
103}
104
105/* Left shift and mask for finding the previous state */
106static unsigned vstate_lshift(unsigned reg, int k, int val)
107{
108 unsigned mask;
109
110 if (k == 5)
111 mask = 0x0e;
112 else if (k == 7)
113 mask = 0x3e;
114 else
115 mask = 0;
116
117 return ((reg << 1) & mask) | val;
118}
119
120/* Bit endian manipulators */
121static inline unsigned bitswap2(unsigned v)
122{
123 return ((v & 0x02) >> 1) | ((v & 0x01) << 1);
124}
125
126static inline unsigned bitswap3(unsigned v)
127{
128 return ((v & 0x04) >> 2) | ((v & 0x02) >> 0) |
129 ((v & 0x01) << 2);
130}
131
132static inline unsigned bitswap4(unsigned v)
133{
134 return ((v & 0x08) >> 3) | ((v & 0x04) >> 1) |
135 ((v & 0x02) << 1) | ((v & 0x01) << 3);
136}
137
138static inline unsigned bitswap5(unsigned v)
139{
140 return ((v & 0x10) >> 4) | ((v & 0x08) >> 2) | ((v & 0x04) >> 0) |
141 ((v & 0x02) << 2) | ((v & 0x01) << 4);
142}
143
144static inline unsigned bitswap6(unsigned v)
145{
146 return ((v & 0x20) >> 5) | ((v & 0x10) >> 3) | ((v & 0x08) >> 1) |
147 ((v & 0x04) << 1) | ((v & 0x02) << 3) | ((v & 0x01) << 5);
148}
149
150static unsigned bitswap(unsigned v, unsigned n)
151{
152 switch (n) {
153 case 1:
154 return v;
155 case 2:
156 return bitswap2(v);
157 case 3:
158 return bitswap3(v);
159 case 4:
160 return bitswap4(v);
161 case 5:
162 return bitswap5(v);
163 case 6:
164 return bitswap6(v);
165 default:
166 return 0;
167 }
168}
169
170/* Generate non-recursive state output from generator state table
171 * Note that the shift register moves right (i.e. the most recent bit is
172 * shifted into the register at k-1 bit of the register), which is typical
173 * textbook representation. The API transition table expects the most recent
174 * bit in the low order bit, or left shift. A bitswap operation is required
175 * to accommodate the difference.
176 */
177static unsigned gen_output(struct vstate *state, int val,
178 const struct osmo_conv_code *code)
179{
180 unsigned out, prev;
181
182 prev = bitswap(state->prev[0], code->K - 1);
183 out = code->next_output[prev][val];
184 out = bitswap(out, code->N);
185
186 return out;
187}
188
189/* Populate non-recursive trellis state
190 * For a given state defined by the k-1 length shift register, find the
191 * value of the input bit that drove the trellis to that state. Also
192 * generate the N outputs of the generator polynomial at that state.
193 */
194static int gen_state_info(uint8_t *val, unsigned reg,
195 int16_t *output, const struct osmo_conv_code *code)
196{
197 int i;
198 unsigned out;
199 struct vstate state;
200
201 /* Previous '0' state */
202 state.state = reg;
203 state.prev[0] = vstate_lshift(reg, code->K, 0);
204 state.prev[1] = vstate_lshift(reg, code->K, 1);
205
206 *val = (reg >> (code->K - 2)) & 0x01;
207
208 /* Transition output */
209 out = gen_output(&state, *val, code);
210
211 /* Unpack to NRZ */
212 for (i = 0; i < code->N; i++)
213 output[i] = BIT2NRZ(out, i);
214
215 return 0;
216}
217
218/* Generate recursive state output from generator state table */
219static unsigned gen_recursive_output(struct vstate *state,
220 uint8_t *val, unsigned reg,
221 const struct osmo_conv_code *code, int pos)
222{
223 int val0, val1;
224 unsigned out, prev;
225
226 /* Previous '0' state */
227 prev = vstate_lshift(reg, code->K, 0);
228 prev = bitswap(prev, code->K - 1);
229
230 /* Input value */
231 val0 = (reg >> (code->K - 2)) & 0x01;
232 val1 = (code->next_term_output[prev] >> pos) & 0x01;
233 *val = val0 == val1 ? 0 : 1;
234
235 /* Wrapper for osmocom state access */
236 prev = bitswap(state->prev[0], code->K - 1);
237
238 /* Compute the transition output */
239 out = code->next_output[prev][*val];
240 out = bitswap(out, code->N);
241
242 return out;
243}
244
245/* Populate recursive trellis state
246 * The bit position of the systematic bit is not explicitly marked by the
247 * API, so it must be extracted from the generator table. Otherwise,
248 * populate the trellis similar to the non-recursive version.
249 * Non-systematic recursive codes are not supported.
250 */
251static int gen_recursive_state_info(uint8_t *val,
252 unsigned reg, int16_t *output, const struct osmo_conv_code *code)
253{
254 int i, j, pos = -1;
255 int ns = NUM_STATES(code->K);
256 unsigned out;
257 struct vstate state;
258
259 /* Previous '0' and '1' states */
260 state.state = reg;
261 state.prev[0] = vstate_lshift(reg, code->K, 0);
262 state.prev[1] = vstate_lshift(reg, code->K, 1);
263
264 /* Find recursive bit location */
265 for (i = 0; i < code->N; i++) {
266 for (j = 0; j < ns; j++) {
267 if ((code->next_output[j][0] >> i) & 0x01)
268 break;
269 }
270
271 if (j == ns) {
272 pos = i;
273 break;
274 }
275 }
276
277 /* Non-systematic recursive code not supported */
278 if (pos < 0)
279 return -EPROTO;
280
281 /* Transition output */
282 out = gen_recursive_output(&state, val, reg, code, pos);
283
284 /* Unpack to NRZ */
285 for (i = 0; i < code->N; i++)
286 output[i] = BIT2NRZ(out, i);
287
288 return 0;
289}
290
291/* Release the trellis */
292static void free_trellis(struct vtrellis *trellis)
293{
294 if (!trellis)
295 return;
296
297 free(trellis->vals);
298 free(trellis->outputs);
299 free(trellis->sums);
300 free(trellis);
301}
302
303/* Allocate and initialize the trellis object
304 * Initialization consists of generating the outputs and output value of a
305 * given state. Due to trellis symmetry and anti-symmetry, only one of the
306 * transition paths is utilized by the butterfly operation in the forward
307 * recursion, so only one set of N outputs is required per state variable.
308 */
309static struct vtrellis *generate_trellis(const struct osmo_conv_code *code)
310{
311 int i, rc = -1;
312 struct vtrellis *trellis;
313 int16_t *outputs;
314
315 int ns = NUM_STATES(code->K);
316 int recursive = conv_code_recursive(code);
317 int olen = (code->N == 2) ? 2 : 4;
318
319 trellis = (struct vtrellis *) calloc(1, sizeof(struct vtrellis));
320 trellis->num_states = ns;
321 trellis->sums = vdec_malloc(ns);
322 trellis->outputs = vdec_malloc(ns * olen);
323 trellis->vals = (uint8_t *) malloc(ns * sizeof(uint8_t));
324
325 if (!trellis->sums || !trellis->outputs)
326 goto fail;
327
328 /* Populate the trellis state objects */
329 for (i = 0; i < ns; i++) {
330 outputs = &trellis->outputs[olen * i];
331 if (recursive) {
332 rc = gen_recursive_state_info(&trellis->vals[i],
333 i, outputs, code);
334 } else {
335 rc = gen_state_info(&trellis->vals[i],
336 i, outputs, code);
337 }
338 }
339
340 if (rc < 0)
341 goto fail;
342
343 return trellis;
344
345fail:
346 free_trellis(trellis);
347 return NULL;
348}
349
350/* Reset decoder
351 * Set accumulated path metrics to zero. For termination other than
352 * tail-biting, initialize the zero state as the encoder starting state.
353 * Initialize with the maximum accumulated sum at length equal to the
354 * constraint length.
355 */
356static void reset_decoder(struct vdecoder *dec, int term)
357{
358 int ns = dec->trellis->num_states;
359
360 memset(dec->trellis->sums, 0, sizeof(int16_t) * ns);
361
362 if (term != CONV_TERM_TAIL_BITING)
363 dec->trellis->sums[0] = INT8_MAX * dec->n * dec->k;
364}
365
366static void _traceback(struct vdecoder *dec,
367 unsigned state, uint8_t *out, int len)
368{
369 int i;
370 unsigned path;
371
372 for (i = len - 1; i >= 0; i--) {
373 path = dec->paths[i][state] + 1;
374 out[i] = dec->trellis->vals[state];
375 state = vstate_lshift(state, dec->k, path);
376 }
377}
378
379static void _traceback_rec(struct vdecoder *dec,
380 unsigned state, uint8_t *out, int len)
381{
382 int i;
383 unsigned path;
384
385 for (i = len - 1; i >= 0; i--) {
386 path = dec->paths[i][state] + 1;
387 out[i] = path ^ dec->trellis->vals[state];
388 state = vstate_lshift(state, dec->k, path);
389 }
390}
391
392/* Traceback and generate decoded output
393 * Find the largest accumulated path metric at the final state except for
394 * the zero terminated case, where we assume the final state is always zero.
395 */
396static int traceback(struct vdecoder *dec, uint8_t *out, int term, int len)
397{
398 int i, sum, max = -1;
399 unsigned path, state = 0;
400
401 if (term != CONV_TERM_FLUSH) {
402 for (i = 0; i < dec->trellis->num_states; i++) {
403 sum = dec->trellis->sums[i];
404 if (sum > max) {
405 max = sum;
406 state = i;
407 }
408 }
409
410 if (max < 0)
411 return -EPROTO;
412 }
413
414 for (i = dec->len - 1; i >= len; i--) {
415 path = dec->paths[i][state] + 1;
416 state = vstate_lshift(state, dec->k, path);
417 }
418
419 if (dec->recursive)
420 _traceback_rec(dec, state, out, len);
421 else
422 _traceback(dec, state, out, len);
423
424 return 0;
425}
426
427/* Release decoder object */
428static void free_vdec(struct vdecoder *dec)
429{
430 if (!dec)
431 return;
432
433 free(dec->paths[0]);
434 free(dec->paths);
435 free_trellis(dec->trellis);
436 free(dec);
437}
438
439/* Allocate decoder object
440 * Subtract the constraint length K on the normalization interval to
441 * accommodate the initialization path metric at state zero.
442 */
443static struct vdecoder *alloc_vdec(const struct osmo_conv_code *code)
444{
445 int i, ns;
446 struct vdecoder *dec;
447
448 ns = NUM_STATES(code->K);
449
450 dec = (struct vdecoder *) calloc(1, sizeof(struct vdecoder));
451 dec->n = code->N;
452 dec->k = code->K;
453 dec->recursive = conv_code_recursive(code);
454 dec->intrvl = INT16_MAX / (dec->n * INT8_MAX) - dec->k;
455
456 if (dec->k == 5) {
457 switch (dec->n) {
458 case 2:
459 dec->metric_func = osmo_conv_gen_metrics_k5_n2;
460 break;
461 case 3:
462 dec->metric_func = osmo_conv_gen_metrics_k5_n3;
463 break;
464 case 4:
465 dec->metric_func = osmo_conv_gen_metrics_k5_n4;
466 break;
467 default:
468 goto fail;
469 }
470 } else if (dec->k == 7) {
471 switch (dec->n) {
472 case 2:
473 dec->metric_func = osmo_conv_gen_metrics_k7_n2;
474 break;
475 case 3:
476 dec->metric_func = osmo_conv_gen_metrics_k7_n3;
477 break;
478 case 4:
479 dec->metric_func = osmo_conv_gen_metrics_k7_n4;
480 break;
481 default:
482 goto fail;
483 }
484 } else {
485 goto fail;
486 }
487
488 if (code->term == CONV_TERM_FLUSH)
489 dec->len = code->len + code->K - 1;
490 else
491 dec->len = code->len;
492
493 dec->trellis = generate_trellis(code);
494 if (!dec->trellis)
495 goto fail;
496
497 dec->paths = (int16_t **) malloc(sizeof(int16_t *) * dec->len);
498 dec->paths[0] = vdec_malloc(ns * dec->len);
499 for (i = 1; i < dec->len; i++)
500 dec->paths[i] = &dec->paths[0][i * ns];
501
502 return dec;
503
504fail:
505 free_vdec(dec);
506 return NULL;
507}
508
509/* Depuncture sequence with nagative value terminated puncturing matrix */
510static int depuncture(const int8_t *in, const int *punc, int8_t *out, int len)
511{
512 int i, n = 0, m = 0;
513
514 for (i = 0; i < len; i++) {
515 if (i == punc[n]) {
516 out[i] = 0;
517 n++;
518 continue;
519 }
520
521 out[i] = in[m++];
522 }
523
524 return 0;
525}
526
527/* Forward trellis recursion
528 * Generate branch metrics and path metrics with a combined function. Only
529 * accumulated path metric sums and path selections are stored. Normalize on
530 * the interval specified by the decoder.
531 */
532static void forward_traverse(struct vdecoder *dec, const int8_t *seq)
533{
534 struct vtrellis *trellis = dec->trellis;
535 int i;
536
537 for (i = 0; i < dec->len; i++) {
538 dec->metric_func(&seq[dec->n * i],
539 trellis->outputs,
540 trellis->sums,
541 dec->paths[i],
542 !(i % dec->intrvl));
543 }
544}
545
546/* Convolutional decode with a decoder object
547 * Initial puncturing run if necessary followed by the forward recursion.
548 * For tail-biting perform a second pass before running the backward
549 * traceback operation.
550 */
551static int conv_decode(struct vdecoder *dec, const int8_t *seq,
552 const int *punc, uint8_t *out, int len, int term)
553{
554 int8_t depunc[dec->len * dec->n];
555
556 reset_decoder(dec, term);
557
558 if (punc) {
559 depuncture(seq, punc, depunc, dec->len * dec->n);
560 seq = depunc;
561 }
562
563 /* Propagate through the trellis with interval normalization */
564 forward_traverse(dec, seq);
565
566 if (term == CONV_TERM_TAIL_BITING)
567 forward_traverse(dec, seq);
568
569 return traceback(dec, out, term, len);
570}
571
572/* All-in-one Viterbi decoding */
573int osmo_conv_decode_acc(const struct osmo_conv_code *code,
574 const sbit_t *input, ubit_t *output)
575{
576 int rc;
577 struct vdecoder *vdec;
578
579 if ((code->N < 2) || (code->N > 4) || (code->len < 1) ||
580 ((code->K != 5) && (code->K != 7)))
581 return -EINVAL;
582
583 vdec = alloc_vdec(code);
584 if (!vdec)
585 return -EFAULT;
586
587 rc = conv_decode(vdec, input, code->puncture,
588 output, code->len, code->term);
589
590 free_vdec(vdec);
591
592 return rc;
593}