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 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 38 | /*! \brief structure describing a given convolutional code */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 39 | struct osmo_conv_code { |
| 40 | int N; |
| 41 | int K; |
| 42 | int len; |
| 43 | |
| 44 | const uint8_t (*next_output)[2]; |
| 45 | const uint8_t (*next_state)[2]; |
| 46 | |
| 47 | const uint8_t *next_term_output; |
| 48 | const uint8_t *next_term_state; |
| 49 | |
| 50 | const int *puncture; |
| 51 | }; |
| 52 | |
| 53 | |
| 54 | /* Encoding */ |
| 55 | |
| 56 | /* Low level API */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 57 | /*! \brief convolutional encoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 58 | struct osmo_conv_encoder { |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 59 | const struct osmo_conv_code *code; /*!< \brief for which code? */ |
| 60 | int i_idx; /*!< \brief Next input bit index */ |
| 61 | int p_idx; /*!< \brief Current puncture index */ |
| 62 | uint8_t state; /*!< \brief Current state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | void osmo_conv_encode_init(struct osmo_conv_encoder *encoder, |
| 66 | const struct osmo_conv_code *code); |
| 67 | int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder, |
| 68 | const ubit_t *input, ubit_t *output, int n); |
| 69 | int osmo_conv_encode_finish(struct osmo_conv_encoder *encoder, ubit_t *output); |
| 70 | |
| 71 | /* All-in-one */ |
| 72 | int osmo_conv_encode(const struct osmo_conv_code *code, |
| 73 | const ubit_t *input, ubit_t *output); |
| 74 | |
| 75 | |
| 76 | /* Decoding */ |
| 77 | |
| 78 | /* Low level API */ |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 79 | /*! \brief convolutional decoder state */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 80 | struct osmo_conv_decoder { |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 81 | /*! \brief description of convolutional code */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 82 | const struct osmo_conv_code *code; |
| 83 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 84 | int n_states; /*!< \brief number of states */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 85 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 86 | int len; /*!< \brief Max o_idx (excl. termination) */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 87 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 88 | int o_idx; /*!< \brief output index */ |
| 89 | int p_idx; /*!< \brief puncture index */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 90 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 91 | unsigned int *ae; /*!< \brief accumulater error */ |
| 92 | unsigned int *ae_next; /*!< \brief next accumulated error (tmp in scan) */ |
| 93 | uint8_t *state_history; /*!< \brief state history [len][n_states] */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | void osmo_conv_decode_init(struct osmo_conv_decoder *decoder, |
| 97 | const struct osmo_conv_code *code, int len); |
| 98 | void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder); |
| 99 | void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder); |
| 100 | |
| 101 | int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder, |
| 102 | const sbit_t *input, int n); |
| 103 | int osmo_conv_decode_finish(struct osmo_conv_decoder *decoder, |
| 104 | const sbit_t *input); |
| 105 | int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder, |
| 106 | ubit_t *output, int has_finish); |
| 107 | |
| 108 | /* All-in-one */ |
| 109 | int osmo_conv_decode(const struct osmo_conv_code *code, |
| 110 | const sbit_t *input, ubit_t *output); |
| 111 | |
| 112 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 113 | /*! }@ */ |
| 114 | |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 115 | #endif /* __OSMO_CONV_H__ */ |