2017-06-20 02:35:06 +00:00
|
|
|
/*! \file conv.h
|
|
|
|
* Osmocom convolutional encoder and decoder. */
|
2011-04-23 14:09:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com>
|
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2011-08-17 10:46:48 +00:00
|
|
|
/*! \defgroup conv Convolutional encoding and decoding routines
|
|
|
|
* @{
|
2017-06-20 02:35:06 +00:00
|
|
|
* \file conv.h */
|
2011-08-17 10:46:48 +00:00
|
|
|
|
2014-06-16 08:13:40 +00:00
|
|
|
#pragma once
|
2011-04-23 14:09:19 +00:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include <osmocom/core/bits.h>
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! possibe termination types
|
2011-11-24 16:46:58 +00:00
|
|
|
*
|
|
|
|
* The termination type will determine which state the encoder/decoder
|
|
|
|
* can start/end with. This is mostly taken care of in the high level API
|
|
|
|
* call. So if you use the low level API, you must take care of making the
|
|
|
|
* proper calls yourself.
|
|
|
|
*/
|
|
|
|
enum osmo_conv_term {
|
2017-06-19 22:17:59 +00:00
|
|
|
CONV_TERM_FLUSH = 0, /*!< Flush encoder state */
|
|
|
|
CONV_TERM_TRUNCATION, /*!< Direct truncation */
|
|
|
|
CONV_TERM_TAIL_BITING, /*!< Tail biting */
|
2011-11-24 16:46:58 +00:00
|
|
|
};
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! structure describing a given convolutional code
|
2011-11-24 10:53:49 +00:00
|
|
|
*
|
|
|
|
* The only required fields are N,K and the next_output/next_state arrays. The
|
|
|
|
* other can be left to default value of zero depending on what the code does.
|
|
|
|
* If 'len' is left at 0 then only the low level API can be used.
|
|
|
|
*/
|
2011-04-23 14:09:19 +00:00
|
|
|
struct osmo_conv_code {
|
2017-06-19 22:17:59 +00:00
|
|
|
int N; /*!< Inverse of code rate */
|
|
|
|
int K; /*!< Constraint length */
|
|
|
|
int len; /*!< # of data bits */
|
2011-04-23 14:09:19 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
enum osmo_conv_term term; /*!< Termination type */
|
2011-11-24 16:46:58 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
const uint8_t (*next_output)[2];/*!< Next output array */
|
|
|
|
const uint8_t (*next_state)[2]; /*!< Next state array */
|
2011-04-23 14:09:19 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
const uint8_t *next_term_output;/*!< Flush termination output */
|
|
|
|
const uint8_t *next_term_state; /*!< Flush termination state */
|
2011-04-23 14:09:19 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
const int *puncture; /*!< Punctured bits indexes */
|
2011-04-23 14:09:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-11-24 16:47:32 +00:00
|
|
|
/* Common */
|
|
|
|
|
|
|
|
int osmo_conv_get_input_length(const struct osmo_conv_code *code, int len);
|
|
|
|
int osmo_conv_get_output_length(const struct osmo_conv_code *code, int len);
|
|
|
|
|
|
|
|
|
2011-04-23 14:09:19 +00:00
|
|
|
/* Encoding */
|
|
|
|
|
|
|
|
/* Low level API */
|
2011-11-24 10:53:49 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! convolutional encoder state */
|
2011-04-23 14:09:19 +00:00
|
|
|
struct osmo_conv_encoder {
|
2017-06-19 22:17:59 +00:00
|
|
|
const struct osmo_conv_code *code; /*!< for which code? */
|
|
|
|
int i_idx; /*!< Next input bit index */
|
|
|
|
int p_idx; /*!< Current puncture index */
|
|
|
|
uint8_t state; /*!< Current state */
|
2011-04-23 14:09:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void osmo_conv_encode_init(struct osmo_conv_encoder *encoder,
|
|
|
|
const struct osmo_conv_code *code);
|
2011-11-24 16:46:58 +00:00
|
|
|
void osmo_conv_encode_load_state(struct osmo_conv_encoder *encoder,
|
|
|
|
const ubit_t *input);
|
2011-04-23 14:09:19 +00:00
|
|
|
int osmo_conv_encode_raw(struct osmo_conv_encoder *encoder,
|
|
|
|
const ubit_t *input, ubit_t *output, int n);
|
2011-11-24 16:46:58 +00:00
|
|
|
int osmo_conv_encode_flush(struct osmo_conv_encoder *encoder, ubit_t *output);
|
2011-04-23 14:09:19 +00:00
|
|
|
|
|
|
|
/* All-in-one */
|
|
|
|
int osmo_conv_encode(const struct osmo_conv_code *code,
|
|
|
|
const ubit_t *input, ubit_t *output);
|
|
|
|
|
|
|
|
|
|
|
|
/* Decoding */
|
|
|
|
|
|
|
|
/* Low level API */
|
2011-11-24 10:53:49 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! convolutional decoder state */
|
2011-04-23 14:09:19 +00:00
|
|
|
struct osmo_conv_decoder {
|
2017-06-19 22:17:59 +00:00
|
|
|
const struct osmo_conv_code *code; /*!< for which code? */
|
2011-04-23 14:09:19 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
int n_states; /*!< number of states */
|
2011-04-23 14:09:19 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
int len; /*!< Max o_idx (excl. termination) */
|
2011-04-23 14:09:19 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
int o_idx; /*!< output index */
|
|
|
|
int p_idx; /*!< puncture index */
|
2011-04-23 14:09:19 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
unsigned int *ae; /*!< accumulated error */
|
|
|
|
unsigned int *ae_next; /*!< next accumulated error (tmp in scan) */
|
|
|
|
uint8_t *state_history; /*!< state history [len][n_states] */
|
2011-04-23 14:09:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void osmo_conv_decode_init(struct osmo_conv_decoder *decoder,
|
2011-11-24 16:46:58 +00:00
|
|
|
const struct osmo_conv_code *code,
|
|
|
|
int len, int start_state);
|
|
|
|
void osmo_conv_decode_reset(struct osmo_conv_decoder *decoder, int start_state);
|
|
|
|
void osmo_conv_decode_rewind(struct osmo_conv_decoder *decoder);
|
2011-04-23 14:09:19 +00:00
|
|
|
void osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder);
|
|
|
|
|
|
|
|
int osmo_conv_decode_scan(struct osmo_conv_decoder *decoder,
|
|
|
|
const sbit_t *input, int n);
|
2011-11-24 16:46:58 +00:00
|
|
|
int osmo_conv_decode_flush(struct osmo_conv_decoder *decoder,
|
|
|
|
const sbit_t *input);
|
2011-04-23 14:09:19 +00:00
|
|
|
int osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder,
|
2011-11-24 16:46:58 +00:00
|
|
|
ubit_t *output, int has_flush, int end_state);
|
2011-04-23 14:09:19 +00:00
|
|
|
|
|
|
|
/* All-in-one */
|
|
|
|
int osmo_conv_decode(const struct osmo_conv_code *code,
|
|
|
|
const sbit_t *input, ubit_t *output);
|
|
|
|
|
|
|
|
|
2012-04-18 19:53:23 +00:00
|
|
|
/*! @} */
|