blob: 8b344f4d38cd25451e0f91b7b2bb5ebbc6896c44 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file conv.h
2 * Osmocom convolutional encoder and decoder. */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +02003/*
Sylvain Munaut19dc5c92011-04-23 16:09:19 +02004 * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com>
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
Harald Welteba6988b2011-08-17 12:46:48 +020023/*! \defgroup conv Convolutional encoding and decoding routines
24 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020025 * \file conv.h */
Harald Welteba6988b2011-08-17 12:46:48 +020026
Sylvain Munaut12ba7782014-06-16 10:13:40 +020027#pragma once
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020028
29#include <stdint.h>
30
31#include <osmocom/core/bits.h>
32
Neels Hofmeyr87e45502017-06-20 00:17:59 +020033/*! possibe termination types
Sylvain Munaut297d13f2011-11-24 17:46:58 +010034 *
35 * The termination type will determine which state the encoder/decoder
36 * can start/end with. This is mostly taken care of in the high level API
37 * call. So if you use the low level API, you must take care of making the
38 * proper calls yourself.
39 */
40enum osmo_conv_term {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020041 CONV_TERM_FLUSH = 0, /*!< Flush encoder state */
42 CONV_TERM_TRUNCATION, /*!< Direct truncation */
43 CONV_TERM_TAIL_BITING, /*!< Tail biting */
Sylvain Munaut297d13f2011-11-24 17:46:58 +010044};
45
Neels Hofmeyr87e45502017-06-20 00:17:59 +020046/*! structure describing a given convolutional code
Sylvain Munaut03d2c892011-11-24 11:53:49 +010047 *
48 * The only required fields are N,K and the next_output/next_state arrays. The
49 * other can be left to default value of zero depending on what the code does.
50 * If 'len' is left at 0 then only the low level API can be used.
51 */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020052struct osmo_conv_code {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053 int N; /*!< Inverse of code rate */
54 int K; /*!< Constraint length */
55 int len; /*!< # of data bits */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020056
Neels Hofmeyr87e45502017-06-20 00:17:59 +020057 enum osmo_conv_term term; /*!< Termination type */
Sylvain Munaut297d13f2011-11-24 17:46:58 +010058
Neels Hofmeyr87e45502017-06-20 00:17:59 +020059 const uint8_t (*next_output)[2];/*!< Next output array */
60 const uint8_t (*next_state)[2]; /*!< Next state array */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020061
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062 const uint8_t *next_term_output;/*!< Flush termination output */
63 const uint8_t *next_term_state; /*!< Flush termination state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020064
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065 const int *puncture; /*!< Punctured bits indexes */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020066};
67
68
Sylvain Munautae8dbb42011-11-24 17:47:32 +010069/* Common */
70
71int osmo_conv_get_input_length(const struct osmo_conv_code *code, int len);
72int osmo_conv_get_output_length(const struct osmo_conv_code *code, int len);
73
74
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020075/* Encoding */
76
77 /* Low level API */
Sylvain Munaut03d2c892011-11-24 11:53:49 +010078
Neels Hofmeyr87e45502017-06-20 00:17:59 +020079/*! convolutional encoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020080struct osmo_conv_encoder {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020081 const struct osmo_conv_code *code; /*!< for which code? */
82 int i_idx; /*!< Next input bit index */
83 int p_idx; /*!< Current puncture index */
84 uint8_t state; /*!< Current state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020085};
86
87void osmo_conv_encode_init(struct osmo_conv_encoder *encoder,
88 const struct osmo_conv_code *code);
Sylvain Munaut297d13f2011-11-24 17:46:58 +010089void osmo_conv_encode_load_state(struct osmo_conv_encoder *encoder,
90 const ubit_t *input);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020091int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder,
92 const ubit_t *input, ubit_t *output, int n);
Sylvain Munaut297d13f2011-11-24 17:46:58 +010093int osmo_conv_encode_flush(struct osmo_conv_encoder *encoder, ubit_t *output);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020094
95 /* All-in-one */
96int osmo_conv_encode(const struct osmo_conv_code *code,
97 const ubit_t *input, ubit_t *output);
98
99
100/* Decoding */
101
102 /* Low level API */
Sylvain Munaut03d2c892011-11-24 11:53:49 +0100103
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200104/*! convolutional decoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200105struct osmo_conv_decoder {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106 const struct osmo_conv_code *code; /*!< for which code? */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108 int n_states; /*!< number of states */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200109
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200110 int len; /*!< Max o_idx (excl. termination) */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200111
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200112 int o_idx; /*!< output index */
113 int p_idx; /*!< puncture index */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200114
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200115 unsigned int *ae; /*!< accumulated error */
116 unsigned int *ae_next; /*!< next accumulated error (tmp in scan) */
117 uint8_t *state_history; /*!< state history [len][n_states] */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200118};
119
120void osmo_conv_decode_init(struct osmo_conv_decoder *decoder,
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100121 const struct osmo_conv_code *code,
122 int len, int start_state);
123void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder, int start_state);
124void osmo_conv_decode_rewind(struct osmo_conv_decoder *decoder);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200125void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder);
126
127int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder,
128 const sbit_t *input, int n);
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100129int osmo_conv_decode_flush(struct osmo_conv_decoder *decoder,
130 const sbit_t *input);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200131int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder,
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100132 ubit_t *output, int has_flush, int end_state);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200133
134 /* All-in-one */
135int osmo_conv_decode(const struct osmo_conv_code *code,
136 const sbit_t *input, ubit_t *output);
137
138
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200139/*! @} */