blob: e76a5c61a74f33c4e37cf2a6cf850252b100afc6 [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 Munaut297d13f2011-11-24 17:46:58 +010038/*! \brief possibe termination types
39 *
40 * The termination type will determine which state the encoder/decoder
41 * can start/end with. This is mostly taken care of in the high level API
42 * call. So if you use the low level API, you must take care of making the
43 * proper calls yourself.
44 */
45enum osmo_conv_term {
46 CONV_TERM_FLUSH = 0, /*!< \brief Flush encoder state */
47 CONV_TERM_TRUNCATION, /*!< \brief Direct truncation */
48 CONV_TERM_TAIL_BITING, /*!< \brief Tail biting */
49};
50
Sylvain Munaut03d2c892011-11-24 11:53:49 +010051/*! \brief structure describing a given convolutional code
52 *
53 * The only required fields are N,K and the next_output/next_state arrays. The
54 * other can be left to default value of zero depending on what the code does.
55 * If 'len' is left at 0 then only the low level API can be used.
56 */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020057struct osmo_conv_code {
Sylvain Munaut03d2c892011-11-24 11:53:49 +010058 int N; /*!< \brief Inverse of code rate */
59 int K; /*!< \brief Constraint length */
60 int len; /*!< \brief # of data bits */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020061
Sylvain Munaut297d13f2011-11-24 17:46:58 +010062 enum osmo_conv_term term; /*!< \brief Termination type */
63
Sylvain Munaut03d2c892011-11-24 11:53:49 +010064 const uint8_t (*next_output)[2];/*!< \brief Next output array */
65 const uint8_t (*next_state)[2]; /*!< \brief Next state array */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020066
Sylvain Munaut03d2c892011-11-24 11:53:49 +010067 const uint8_t *next_term_output;/*!< \brief Flush termination output */
68 const uint8_t *next_term_state; /*!< \brief Flush termination state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020069
Sylvain Munaut03d2c892011-11-24 11:53:49 +010070 const int *puncture; /*!< \brief Punctured bits indexes */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020071};
72
73
Sylvain Munautae8dbb42011-11-24 17:47:32 +010074/* Common */
75
76int osmo_conv_get_input_length(const struct osmo_conv_code *code, int len);
77int osmo_conv_get_output_length(const struct osmo_conv_code *code, int len);
78
79
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020080/* Encoding */
81
82 /* Low level API */
Sylvain Munaut03d2c892011-11-24 11:53:49 +010083
Harald Welteba6988b2011-08-17 12:46:48 +020084/*! \brief convolutional encoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020085struct osmo_conv_encoder {
Harald Welteba6988b2011-08-17 12:46:48 +020086 const struct osmo_conv_code *code; /*!< \brief for which code? */
87 int i_idx; /*!< \brief Next input bit index */
88 int p_idx; /*!< \brief Current puncture index */
89 uint8_t state; /*!< \brief Current state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020090};
91
92void osmo_conv_encode_init(struct osmo_conv_encoder *encoder,
93 const struct osmo_conv_code *code);
Sylvain Munaut297d13f2011-11-24 17:46:58 +010094void osmo_conv_encode_load_state(struct osmo_conv_encoder *encoder,
95 const ubit_t *input);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020096int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder,
97 const ubit_t *input, ubit_t *output, int n);
Sylvain Munaut297d13f2011-11-24 17:46:58 +010098int osmo_conv_encode_flush(struct osmo_conv_encoder *encoder, ubit_t *output);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +020099
100 /* All-in-one */
101int osmo_conv_encode(const struct osmo_conv_code *code,
102 const ubit_t *input, ubit_t *output);
103
104
105/* Decoding */
106
107 /* Low level API */
Sylvain Munaut03d2c892011-11-24 11:53:49 +0100108
Harald Welteba6988b2011-08-17 12:46:48 +0200109/*! \brief convolutional decoder state */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200110struct osmo_conv_decoder {
Sylvain Munaut03d2c892011-11-24 11:53:49 +0100111 const struct osmo_conv_code *code; /*!< \brief for which code? */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200112
Harald Welteba6988b2011-08-17 12:46:48 +0200113 int n_states; /*!< \brief number of states */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200114
Harald Welteba6988b2011-08-17 12:46:48 +0200115 int len; /*!< \brief Max o_idx (excl. termination) */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200116
Harald Welteba6988b2011-08-17 12:46:48 +0200117 int o_idx; /*!< \brief output index */
118 int p_idx; /*!< \brief puncture index */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200119
Sylvain Munaut03d2c892011-11-24 11:53:49 +0100120 unsigned int *ae; /*!< \brief accumulated error */
Harald Welteba6988b2011-08-17 12:46:48 +0200121 unsigned int *ae_next; /*!< \brief next accumulated error (tmp in scan) */
122 uint8_t *state_history; /*!< \brief state history [len][n_states] */
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200123};
124
125void osmo_conv_decode_init(struct osmo_conv_decoder *decoder,
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100126 const struct osmo_conv_code *code,
127 int len, int start_state);
128void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder, int start_state);
129void osmo_conv_decode_rewind(struct osmo_conv_decoder *decoder);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200130void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder);
131
132int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder,
133 const sbit_t *input, int n);
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100134int osmo_conv_decode_flush(struct osmo_conv_decoder *decoder,
135 const sbit_t *input);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200136int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder,
Sylvain Munaut297d13f2011-11-24 17:46:58 +0100137 ubit_t *output, int has_flush, int end_state);
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200138
139 /* All-in-one */
140int osmo_conv_decode(const struct osmo_conv_code *code,
141 const sbit_t *input, ubit_t *output);
142
143
Harald Welteba6988b2011-08-17 12:46:48 +0200144/*! }@ */
145
Sylvain Munaut19dc5c92011-04-23 16:09:19 +0200146#endif /* __OSMO_CONV_H__ */