blob: ba490b454ac77d4b8d591a7f9aaf7ee8a80c65a2 [file] [log] [blame]
Sylvain Munaut19dc5c92011-04-23 16:09:19 +02001/*
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 Welteba6988b2011-08-17 12:46:48 +020023/*! \defgroup conv Convolutional encoding and decoding routines
24 * @{
25 */
26
27/*! \file conv.h
28 * \file Osmocom convolutional encoder and decoder
29 */
30
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020031#ifndef __OSMO_CONV_H__
32#define __OSMO_CONV_H__
33
34#include <stdint.h>
35
36#include <osmocom/core/bits.h>
37
Sylvain Munaut03d2c892011-11-24 11:53:49 +010038/*! \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 Munaut19dc5c92011-04-23 16:09:19 +020044struct osmo_conv_code {
Sylvain Munaut03d2c892011-11-24 11:53:49 +010045 int N; /*!< \brief Inverse of code rate */
46 int K; /*!< \brief Constraint length */
47 int len; /*!< \brief # of data bits */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020048
Sylvain Munaut03d2c892011-11-24 11:53:49 +010049 const uint8_t (*next_output)[2];/*!< \brief Next output array */
50 const uint8_t (*next_state)[2]; /*!< \brief Next state array */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020051
Sylvain Munaut03d2c892011-11-24 11:53:49 +010052 const uint8_t *next_term_output;/*!< \brief Flush termination output */
53 const uint8_t *next_term_state; /*!< \brief Flush termination state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020054
Sylvain Munaut03d2c892011-11-24 11:53:49 +010055 const int *puncture; /*!< \brief Punctured bits indexes */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020056};
57
58
59/* Encoding */
60
61 /* Low level API */
Sylvain Munaut03d2c892011-11-24 11:53:49 +010062
Harald Welteba6988b2011-08-17 12:46:48 +020063/*! \brief convolutional encoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020064struct osmo_conv_encoder {
Harald Welteba6988b2011-08-17 12:46:48 +020065 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 Munaut19dc5c92011-04-23 16:09:19 +020069};
70
71void osmo_conv_encode_init(struct osmo_conv_encoder *encoder,
72 const struct osmo_conv_code *code);
73int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder,
74 const ubit_t *input, ubit_t *output, int n);
75int osmo_conv_encode_finish(struct osmo_conv_encoder *encoder, ubit_t *output);
76
77 /* All-in-one */
78int 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 Munaut03d2c892011-11-24 11:53:49 +010085
Harald Welteba6988b2011-08-17 12:46:48 +020086/*! \brief convolutional decoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020087struct osmo_conv_decoder {
Sylvain Munaut03d2c892011-11-24 11:53:49 +010088 const struct osmo_conv_code *code; /*!< \brief for which code? */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020089
Harald Welteba6988b2011-08-17 12:46:48 +020090 int n_states; /*!< \brief number of states */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020091
Harald Welteba6988b2011-08-17 12:46:48 +020092 int len; /*!< \brief Max o_idx (excl. termination) */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020093
Harald Welteba6988b2011-08-17 12:46:48 +020094 int o_idx; /*!< \brief output index */
95 int p_idx; /*!< \brief puncture index */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020096
Sylvain Munaut03d2c892011-11-24 11:53:49 +010097 unsigned int *ae; /*!< \brief accumulated error */
Harald Welteba6988b2011-08-17 12:46:48 +020098 unsigned int *ae_next; /*!< \brief next accumulated error (tmp in scan) */
99 uint8_t *state_history; /*!< \brief state history [len][n_states] */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200100};
101
102void osmo_conv_decode_init(struct osmo_conv_decoder *decoder,
103 const struct osmo_conv_code *code, int len);
104void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder);
105void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder);
106
107int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder,
108 const sbit_t *input, int n);
109int osmo_conv_decode_finish(struct osmo_conv_decoder *decoder,
110 const sbit_t *input);
111int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder,
112 ubit_t *output, int has_finish);
113
114 /* All-in-one */
115int osmo_conv_decode(const struct osmo_conv_code *code,
116 const sbit_t *input, ubit_t *output);
117
118
Harald Welteba6988b2011-08-17 12:46:48 +0200119/*! }@ */
120
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200121#endif /* __OSMO_CONV_H__ */