Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file conv.h |
| 2 | * Osmocom convolutional encoder and decoder. */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 3 | /* |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 4 | * 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 Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 19 | /*! \defgroup conv Convolutional encoding and decoding routines |
| 20 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 21 | * \file conv.h */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 22 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 23 | #pragma once |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 24 | |
| 25 | #include <stdint.h> |
| 26 | |
| 27 | #include <osmocom/core/bits.h> |
| 28 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 29 | /*! possibe termination types |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 30 | * |
| 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 | */ |
| 36 | enum osmo_conv_term { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 37 | CONV_TERM_FLUSH = 0, /*!< Flush encoder state */ |
| 38 | CONV_TERM_TRUNCATION, /*!< Direct truncation */ |
| 39 | CONV_TERM_TAIL_BITING, /*!< Tail biting */ |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 40 | }; |
| 41 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 42 | /*! structure describing a given convolutional code |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame] | 43 | * |
| 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 Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 48 | struct osmo_conv_code { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 49 | int N; /*!< Inverse of code rate */ |
| 50 | int K; /*!< Constraint length */ |
| 51 | int len; /*!< # of data bits */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 52 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 53 | enum osmo_conv_term term; /*!< Termination type */ |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 54 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 55 | const uint8_t (*next_output)[2];/*!< Next output array */ |
| 56 | const uint8_t (*next_state)[2]; /*!< Next state array */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 57 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 58 | const uint8_t *next_term_output;/*!< Flush termination output */ |
| 59 | const uint8_t *next_term_state; /*!< Flush termination state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 60 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 61 | const int *puncture; /*!< Punctured bits indexes */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | |
Sylvain Munaut | ae8dbb4 | 2011-11-24 17:47:32 +0100 | [diff] [blame] | 65 | /* Common */ |
| 66 | |
| 67 | int osmo_conv_get_input_length(const struct osmo_conv_code *code, int len); |
| 68 | int osmo_conv_get_output_length(const struct osmo_conv_code *code, int len); |
| 69 | |
| 70 | |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 71 | /* Encoding */ |
| 72 | |
| 73 | /* Low level API */ |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame] | 74 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 75 | /*! convolutional encoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 76 | struct osmo_conv_encoder { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 77 | 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 Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | void osmo_conv_encode_init(struct osmo_conv_encoder *encoder, |
| 84 | const struct osmo_conv_code *code); |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 85 | void osmo_conv_encode_load_state(struct osmo_conv_encoder *encoder, |
| 86 | const ubit_t *input); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 87 | int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder, |
| 88 | const ubit_t *input, ubit_t *output, int n); |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 89 | int osmo_conv_encode_flush(struct osmo_conv_encoder *encoder, ubit_t *output); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 90 | |
| 91 | /* All-in-one */ |
| 92 | int 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 Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame] | 99 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 100 | /*! convolutional decoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 101 | struct osmo_conv_decoder { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 102 | const struct osmo_conv_code *code; /*!< for which code? */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 103 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 104 | int n_states; /*!< number of states */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 105 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 106 | int len; /*!< Max o_idx (excl. termination) */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 107 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 108 | int o_idx; /*!< output index */ |
| 109 | int p_idx; /*!< puncture index */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 110 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 111 | 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 Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 114 | }; |
| 115 | |
| 116 | void osmo_conv_decode_init(struct osmo_conv_decoder *decoder, |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 117 | const struct osmo_conv_code *code, |
| 118 | int len, int start_state); |
| 119 | void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder, int start_state); |
| 120 | void osmo_conv_decode_rewind(struct osmo_conv_decoder *decoder); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 121 | void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder); |
| 122 | |
| 123 | int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder, |
| 124 | const sbit_t *input, int n); |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 125 | int osmo_conv_decode_flush(struct osmo_conv_decoder *decoder, |
| 126 | const sbit_t *input); |
Sylvain Munaut | d5974e9 | 2021-12-21 19:45:34 +0100 | [diff] [blame] | 127 | int osmo_conv_decode_get_best_end_state(struct osmo_conv_decoder *decoder); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 128 | int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder, |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 129 | ubit_t *output, int has_flush, int end_state); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 130 | |
| 131 | /* All-in-one */ |
| 132 | int osmo_conv_decode(const struct osmo_conv_code *code, |
| 133 | const sbit_t *input, ubit_t *output); |
| 134 | |
| 135 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 136 | /*! @} */ |