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 |
| 28 | * \file Osmocom convolutional encoder and decoder |
| 29 | */ |
| 30 | |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 31 | #ifndef __OSMO_CONV_H__ |
| 32 | #define __OSMO_CONV_H__ |
| 33 | |
| 34 | #include <stdint.h> |
| 35 | |
| 36 | #include <osmocom/core/bits.h> |
| 37 | |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 38 | /*! \brief structure describing a given convolutional code |
| 39 | * |
| 40 | * The only required fields are N,K and the next_output/next_state arrays. The |
| 41 | * other can be left to default value of zero depending on what the code does. |
| 42 | * If 'len' is left at 0 then only the low level API can be used. |
| 43 | */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 44 | struct osmo_conv_code { |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 45 | int N; /*!< \brief Inverse of code rate */ |
| 46 | int K; /*!< \brief Constraint length */ |
| 47 | int len; /*!< \brief # of data bits */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 48 | |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 49 | const uint8_t (*next_output)[2];/*!< \brief Next output array */ |
| 50 | const uint8_t (*next_state)[2]; /*!< \brief Next state array */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 51 | |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 52 | const uint8_t *next_term_output;/*!< \brief Flush termination output */ |
| 53 | const uint8_t *next_term_state; /*!< \brief Flush termination state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 54 | |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 55 | const int *puncture; /*!< \brief Punctured bits indexes */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | |
| 59 | /* Encoding */ |
| 60 | |
| 61 | /* Low level API */ |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 62 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 63 | /*! \brief convolutional encoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 64 | struct osmo_conv_encoder { |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 65 | const struct osmo_conv_code *code; /*!< \brief for which code? */ |
| 66 | int i_idx; /*!< \brief Next input bit index */ |
| 67 | int p_idx; /*!< \brief Current puncture index */ |
| 68 | uint8_t state; /*!< \brief Current state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | void osmo_conv_encode_init(struct osmo_conv_encoder *encoder, |
| 72 | const struct osmo_conv_code *code); |
| 73 | int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder, |
| 74 | const ubit_t *input, ubit_t *output, int n); |
| 75 | int osmo_conv_encode_finish(struct osmo_conv_encoder *encoder, ubit_t *output); |
| 76 | |
| 77 | /* All-in-one */ |
| 78 | int osmo_conv_encode(const struct osmo_conv_code *code, |
| 79 | const ubit_t *input, ubit_t *output); |
| 80 | |
| 81 | |
| 82 | /* Decoding */ |
| 83 | |
| 84 | /* Low level API */ |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 85 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 86 | /*! \brief convolutional decoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 87 | struct osmo_conv_decoder { |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 88 | const struct osmo_conv_code *code; /*!< \brief for which code? */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 89 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 90 | int n_states; /*!< \brief number of states */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 91 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 92 | int len; /*!< \brief Max o_idx (excl. termination) */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 93 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 94 | int o_idx; /*!< \brief output index */ |
| 95 | int p_idx; /*!< \brief puncture index */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 96 | |
Sylvain Munaut | 03d2c89 | 2011-11-24 11:53:49 +0100 | [diff] [blame^] | 97 | unsigned int *ae; /*!< \brief accumulated error */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 98 | unsigned int *ae_next; /*!< \brief next accumulated error (tmp in scan) */ |
| 99 | uint8_t *state_history; /*!< \brief state history [len][n_states] */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | void osmo_conv_decode_init(struct osmo_conv_decoder *decoder, |
| 103 | const struct osmo_conv_code *code, int len); |
| 104 | void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder); |
| 105 | void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder); |
| 106 | |
| 107 | int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder, |
| 108 | const sbit_t *input, int n); |
| 109 | int osmo_conv_decode_finish(struct osmo_conv_decoder *decoder, |
| 110 | const sbit_t *input); |
| 111 | int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder, |
| 112 | ubit_t *output, int has_finish); |
| 113 | |
| 114 | /* All-in-one */ |
| 115 | int osmo_conv_decode(const struct osmo_conv_code *code, |
| 116 | const sbit_t *input, ubit_t *output); |
| 117 | |
| 118 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 119 | /*! }@ */ |
| 120 | |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 121 | #endif /* __OSMO_CONV_H__ */ |