Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * conv.h |
| 3 | * |
| 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. |
| 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 Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 23 | /*! \defgroup conv Convolutional encoding and decoding routines |
| 24 | * @{ |
| 25 | */ |
| 26 | |
| 27 | /*! \file conv.h |
Katerina Barone-Adesi | c28c6a0 | 2013-02-15 13:27:59 +0100 | [diff] [blame] | 28 | * Osmocom convolutional encoder and decoder |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 29 | */ |
| 30 | |
Sylvain Munaut | 12ba778 | 2014-06-16 10:13:40 +0200 | [diff] [blame] | 31 | #pragma once |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 32 | |
| 33 | #include <stdint.h> |
| 34 | |
| 35 | #include <osmocom/core/bits.h> |
| 36 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 37 | /*! possibe termination types |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 38 | * |
| 39 | * The termination type will determine which state the encoder/decoder |
| 40 | * can start/end with. This is mostly taken care of in the high level API |
| 41 | * call. So if you use the low level API, you must take care of making the |
| 42 | * proper calls yourself. |
| 43 | */ |
| 44 | enum osmo_conv_term { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 45 | CONV_TERM_FLUSH = 0, /*!< Flush encoder state */ |
| 46 | CONV_TERM_TRUNCATION, /*!< Direct truncation */ |
| 47 | CONV_TERM_TAIL_BITING, /*!< Tail biting */ |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 48 | }; |
| 49 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 50 | /*! structure describing a given convolutional code |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame] | 51 | * |
| 52 | * The only required fields are N,K and the next_output/next_state arrays. The |
| 53 | * other can be left to default value of zero depending on what the code does. |
| 54 | * If 'len' is left at 0 then only the low level API can be used. |
| 55 | */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 56 | struct osmo_conv_code { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 57 | int N; /*!< Inverse of code rate */ |
| 58 | int K; /*!< Constraint length */ |
| 59 | int len; /*!< # of data bits */ |
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 | enum osmo_conv_term term; /*!< Termination type */ |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 62 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 63 | const uint8_t (*next_output)[2];/*!< Next output array */ |
| 64 | const uint8_t (*next_state)[2]; /*!< Next state array */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 65 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 66 | const uint8_t *next_term_output;/*!< Flush termination output */ |
| 67 | const uint8_t *next_term_state; /*!< Flush termination state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 68 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 69 | const int *puncture; /*!< Punctured bits indexes */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | |
Sylvain Munaut | ae8dbb4 | 2011-11-24 17:47:32 +0100 | [diff] [blame] | 73 | /* Common */ |
| 74 | |
| 75 | int osmo_conv_get_input_length(const struct osmo_conv_code *code, int len); |
| 76 | int osmo_conv_get_output_length(const struct osmo_conv_code *code, int len); |
| 77 | |
| 78 | |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 79 | /* Encoding */ |
| 80 | |
| 81 | /* Low level API */ |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame] | 82 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 83 | /*! convolutional encoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 84 | struct osmo_conv_encoder { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 85 | const struct osmo_conv_code *code; /*!< for which code? */ |
| 86 | int i_idx; /*!< Next input bit index */ |
| 87 | int p_idx; /*!< Current puncture index */ |
| 88 | uint8_t state; /*!< Current state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | void osmo_conv_encode_init(struct osmo_conv_encoder *encoder, |
| 92 | const struct osmo_conv_code *code); |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 93 | void osmo_conv_encode_load_state(struct osmo_conv_encoder *encoder, |
| 94 | const ubit_t *input); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 95 | int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder, |
| 96 | const ubit_t *input, ubit_t *output, int n); |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 97 | 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] | 98 | |
| 99 | /* All-in-one */ |
| 100 | int osmo_conv_encode(const struct osmo_conv_code *code, |
| 101 | const ubit_t *input, ubit_t *output); |
| 102 | |
| 103 | |
| 104 | /* Decoding */ |
| 105 | |
| 106 | /* Low level API */ |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame] | 107 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 108 | /*! convolutional decoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 109 | struct osmo_conv_decoder { |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 110 | const struct osmo_conv_code *code; /*!< for which code? */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 111 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 112 | int n_states; /*!< number of states */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 113 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 114 | int len; /*!< Max o_idx (excl. termination) */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 115 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 116 | int o_idx; /*!< output index */ |
| 117 | int p_idx; /*!< puncture index */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 118 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame^] | 119 | unsigned int *ae; /*!< accumulated error */ |
| 120 | unsigned int *ae_next; /*!< next accumulated error (tmp in scan) */ |
| 121 | uint8_t *state_history; /*!< state history [len][n_states] */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | void osmo_conv_decode_init(struct osmo_conv_decoder *decoder, |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 125 | const struct osmo_conv_code *code, |
| 126 | int len, int start_state); |
| 127 | void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder, int start_state); |
| 128 | void osmo_conv_decode_rewind(struct osmo_conv_decoder *decoder); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 129 | void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder); |
| 130 | |
| 131 | int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder, |
| 132 | const sbit_t *input, int n); |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 133 | int osmo_conv_decode_flush(struct osmo_conv_decoder *decoder, |
| 134 | const sbit_t *input); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 135 | int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder, |
Sylvain Munaut | 297d13f | 2011-11-24 17:46:58 +0100 | [diff] [blame] | 136 | ubit_t *output, int has_flush, int end_state); |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 137 | |
| 138 | /* All-in-one */ |
| 139 | int osmo_conv_decode(const struct osmo_conv_code *code, |
| 140 | const sbit_t *input, ubit_t *output); |
| 141 | |
| 142 | |
Sylvain Munaut | dca7d2c | 2012-04-18 21:53:23 +0200 | [diff] [blame] | 143 | /*! @} */ |