blob: 84ef0f858bd0eed659654edcb9d69035c9bdc64e [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.
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020017 */
18
Harald Welteba6988b2011-08-17 12:46:48 +020019/*! \defgroup conv Convolutional encoding and decoding routines
20 * @{
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020021 * \file conv.h */
Harald Welteba6988b2011-08-17 12:46:48 +020022
Sylvain Munaut12ba7782014-06-16 10:13:40 +020023#pragma once
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020024
25#include <stdint.h>
26
27#include <osmocom/core/bits.h>
28
Neels Hofmeyr87e45502017-06-20 00:17:59 +020029/*! possibe termination types
Sylvain Munaut297d13f2011-11-24 17:46:58 +010030 *
31 * The termination type will determine which state the encoder/decoder
32 * can start/end with. This is mostly taken care of in the high level API
33 * call. So if you use the low level API, you must take care of making the
34 * proper calls yourself.
35 */
36enum osmo_conv_term {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020037 CONV_TERM_FLUSH = 0, /*!< Flush encoder state */
38 CONV_TERM_TRUNCATION, /*!< Direct truncation */
39 CONV_TERM_TAIL_BITING, /*!< Tail biting */
Sylvain Munaut297d13f2011-11-24 17:46:58 +010040};
41
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042/*! structure describing a given convolutional code
Sylvain Munaut03d2c892011-11-24 11:53:49 +010043 *
44 * The only required fields are N,K and the next_output/next_state arrays. The
45 * other can be left to default value of zero depending on what the code does.
46 * If 'len' is left at 0 then only the low level API can be used.
47 */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020048struct osmo_conv_code {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020049 int N; /*!< Inverse of code rate */
50 int K; /*!< Constraint length */
51 int len; /*!< # of data bits */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020052
Neels Hofmeyr87e45502017-06-20 00:17:59 +020053 enum osmo_conv_term term; /*!< Termination type */
Sylvain Munaut297d13f2011-11-24 17:46:58 +010054
Neels Hofmeyr87e45502017-06-20 00:17:59 +020055 const uint8_t (*next_output)[2];/*!< Next output array */
56 const uint8_t (*next_state)[2]; /*!< Next state array */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020057
Neels Hofmeyr87e45502017-06-20 00:17:59 +020058 const uint8_t *next_term_output;/*!< Flush termination output */
59 const uint8_t *next_term_state; /*!< Flush termination state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020060
Neels Hofmeyr87e45502017-06-20 00:17:59 +020061 const int *puncture; /*!< Punctured bits indexes */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020062};
63
64
Sylvain Munautae8dbb42011-11-24 17:47:32 +010065/* Common */
66
67int osmo_conv_get_input_length(const struct osmo_conv_code *code, int len);
68int osmo_conv_get_output_length(const struct osmo_conv_code *code, int len);
69
70
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020071/* Encoding */
72
73 /* Low level API */
Sylvain Munaut03d2c892011-11-24 11:53:49 +010074
Neels Hofmeyr87e45502017-06-20 00:17:59 +020075/*! convolutional encoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020076struct osmo_conv_encoder {
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077 const struct osmo_conv_code *code; /*!< for which code? */
78 int i_idx; /*!< Next input bit index */
79 int p_idx; /*!< Current puncture index */
80 uint8_t state; /*!< Current state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020081};
82
83void osmo_conv_encode_init(struct osmo_conv_encoder *encoder,
84 const struct osmo_conv_code *code);
Sylvain Munaut297d13f2011-11-24 17:46:58 +010085void osmo_conv_encode_load_state(struct osmo_conv_encoder *encoder,
86 const ubit_t *input);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020087int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder,
88 const ubit_t *input, ubit_t *output, int n);
Sylvain Munaut297d13f2011-11-24 17:46:58 +010089int osmo_conv_encode_flush(struct osmo_conv_encoder *encoder, ubit_t *output);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020090
91 /* All-in-one */
92int osmo_conv_encode(const struct osmo_conv_code *code,
93 const ubit_t *input, ubit_t *output);
94
95
96/* Decoding */
97
98 /* Low level API */
Sylvain Munaut03d2c892011-11-24 11:53:49 +010099
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200100/*! convolutional decoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200101struct osmo_conv_decoder {
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200102 const struct osmo_conv_code *code; /*!< for which code? */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200103
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200104 int n_states; /*!< number of states */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200105
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200106 int len; /*!< Max o_idx (excl. termination) */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108 int o_idx; /*!< output index */
109 int p_idx; /*!< puncture index */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200110
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200111 unsigned int *ae; /*!< accumulated error */
112 unsigned int *ae_next; /*!< next accumulated error (tmp in scan) */
113 uint8_t *state_history; /*!< state history [len][n_states] */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200114};
115
116void osmo_conv_decode_init(struct osmo_conv_decoder *decoder,
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100117 const struct osmo_conv_code *code,
118 int len, int start_state);
119void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder, int start_state);
120void osmo_conv_decode_rewind(struct osmo_conv_decoder *decoder);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200121void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder);
122
123int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder,
124 const sbit_t *input, int n);
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100125int osmo_conv_decode_flush(struct osmo_conv_decoder *decoder,
126 const sbit_t *input);
Sylvain Munautd5974e92021-12-21 19:45:34 +0100127int osmo_conv_decode_get_best_end_state(struct osmo_conv_decoder *decoder);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200128int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder,
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100129 ubit_t *output, int has_flush, int end_state);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200130
131 /* All-in-one */
132int osmo_conv_decode(const struct osmo_conv_code *code,
133 const sbit_t *input, ubit_t *output);
134
135
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200136/*! @} */