Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * conv.c |
| 3 | * |
| 4 | * Generic convolutional encoding / decoding |
| 5 | * |
| 6 | * Copyright (C) 2011 Sylvain Munaut <tnt@246tNt.com> |
| 7 | * |
| 8 | * All Rights Reserved |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | */ |
| 24 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 25 | /*! \addtogroup conv |
| 26 | * @{ |
| 27 | */ |
| 28 | |
| 29 | /*! \file conv.c |
| 30 | * \file Osmocom convolutional encoder and decoder |
| 31 | */ |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 32 | #include "config.h" |
| 33 | #ifdef HAVE_ALLOCA_H |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 34 | #include <alloca.h> |
Holger Hans Peter Freyther | 4772348 | 2011-11-09 11:26:15 +0100 | [diff] [blame] | 35 | #endif |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 36 | #include <stdint.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <string.h> |
| 39 | |
| 40 | #include <osmocom/core/bits.h> |
| 41 | #include <osmocom/core/conv.h> |
| 42 | |
| 43 | |
| 44 | /* ------------------------------------------------------------------------ */ |
| 45 | /* Encoding */ |
| 46 | /* ------------------------------------------------------------------------ */ |
| 47 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 48 | /*! \brief Initialize a convolutional encoder |
| 49 | * \param[in,out] encoder Encoder state to initialize |
| 50 | * \param[in] code Description of convolutional code |
| 51 | */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 52 | void |
| 53 | osmo_conv_encode_init(struct osmo_conv_encoder *encoder, |
| 54 | const struct osmo_conv_code *code) |
| 55 | { |
| 56 | memset(encoder, 0x00, sizeof(struct osmo_conv_encoder)); |
| 57 | encoder->code = code; |
| 58 | } |
| 59 | |
| 60 | static inline int |
| 61 | _conv_encode_do_output(struct osmo_conv_encoder *encoder, |
| 62 | uint8_t out, ubit_t *output) |
| 63 | { |
| 64 | const struct osmo_conv_code *code = encoder->code; |
| 65 | int o_idx = 0; |
| 66 | int j; |
| 67 | |
| 68 | if (code->puncture) { |
| 69 | for (j=0; j<code->N; j++) |
| 70 | { |
| 71 | int bit_no = code->N - j - 1; |
| 72 | int r_idx = encoder->i_idx * code->N + j; |
| 73 | |
| 74 | if (code->puncture[encoder->p_idx] == r_idx) |
| 75 | encoder->p_idx++; |
| 76 | else |
| 77 | output[o_idx++] = (out >> bit_no) & 1; |
| 78 | } |
| 79 | } else { |
| 80 | for (j=0; j<code->N; j++) |
| 81 | { |
| 82 | int bit_no = code->N - j - 1; |
| 83 | output[o_idx++] = (out >> bit_no) & 1; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return o_idx; |
| 88 | } |
| 89 | |
| 90 | int |
| 91 | osmo_conv_encode_raw(struct osmo_conv_encoder *encoder, |
| 92 | const ubit_t *input, ubit_t *output, int n) |
| 93 | { |
| 94 | const struct osmo_conv_code *code = encoder->code; |
| 95 | uint8_t state; |
| 96 | int i; |
| 97 | int o_idx; |
| 98 | |
| 99 | o_idx = 0; |
| 100 | state = encoder->state; |
| 101 | |
| 102 | for (i=0; i<n; i++) { |
| 103 | int bit = input[i]; |
| 104 | uint8_t out; |
| 105 | |
| 106 | out = code->next_output[state][bit]; |
| 107 | state = code->next_state[state][bit]; |
| 108 | |
| 109 | o_idx += _conv_encode_do_output(encoder, out, &output[o_idx]); |
| 110 | |
| 111 | encoder->i_idx++; |
| 112 | } |
| 113 | |
| 114 | encoder->state = state; |
| 115 | |
| 116 | return o_idx; |
| 117 | } |
| 118 | |
| 119 | int |
| 120 | osmo_conv_encode_finish(struct osmo_conv_encoder *encoder, |
| 121 | ubit_t *output) |
| 122 | { |
| 123 | const struct osmo_conv_code *code = encoder->code; |
| 124 | uint8_t state; |
| 125 | int n; |
| 126 | int i; |
| 127 | int o_idx; |
| 128 | |
| 129 | n = code->K - 1; |
| 130 | |
| 131 | o_idx = 0; |
| 132 | state = encoder->state; |
| 133 | |
| 134 | for (i=0; i<n; i++) { |
| 135 | uint8_t out; |
| 136 | |
| 137 | if (code->next_term_output) { |
| 138 | out = code->next_term_output[state]; |
| 139 | state = code->next_term_state[state]; |
| 140 | } else { |
| 141 | out = code->next_output[state][0]; |
| 142 | state = code->next_state[state][0]; |
| 143 | } |
| 144 | |
| 145 | o_idx += _conv_encode_do_output(encoder, out, &output[o_idx]); |
| 146 | |
| 147 | encoder->i_idx++; |
| 148 | } |
| 149 | |
| 150 | encoder->state = state; |
| 151 | |
| 152 | return o_idx; |
| 153 | } |
| 154 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 155 | /*! \brief All-in-one convolutional encoding function |
| 156 | * \param[in] code description of convolutional code to be used |
| 157 | * \param[in] input array of unpacked bits (uncoded) |
| 158 | * \param[out] output array of unpacked bits (encoded) |
| 159 | * |
| 160 | * This is an all-in-one function, taking care of |
| 161 | * \ref osmo_conv_init, \ref osmo_conv_encode_raw and |
| 162 | * \ref osmo_conv_encode_finish. |
| 163 | */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 164 | int |
| 165 | osmo_conv_encode(const struct osmo_conv_code *code, |
| 166 | const ubit_t *input, ubit_t *output) |
| 167 | { |
| 168 | struct osmo_conv_encoder encoder; |
| 169 | int l; |
| 170 | |
| 171 | osmo_conv_encode_init(&encoder, code); |
| 172 | l = osmo_conv_encode_raw(&encoder, input, output, code->len); |
| 173 | l += osmo_conv_encode_finish(&encoder, &output[l]); |
| 174 | |
| 175 | return l; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | /* ------------------------------------------------------------------------ */ |
| 180 | /* Decoding (viterbi) */ |
| 181 | /* ------------------------------------------------------------------------ */ |
| 182 | |
| 183 | #define MAX_AE 0x00ffffff |
| 184 | |
| 185 | void |
| 186 | osmo_conv_decode_init(struct osmo_conv_decoder *decoder, |
| 187 | const struct osmo_conv_code *code, int len) |
| 188 | { |
| 189 | int n_states; |
| 190 | |
| 191 | /* Init */ |
| 192 | if (len <= 0) |
| 193 | len = code->len; |
| 194 | |
| 195 | n_states = 1 << (code->K - 1); |
| 196 | |
| 197 | memset(decoder, 0x00, sizeof(struct osmo_conv_decoder)); |
| 198 | |
| 199 | decoder->code = code; |
| 200 | decoder->n_states = n_states; |
| 201 | decoder->len = len; |
| 202 | |
| 203 | /* Allocate arrays */ |
| 204 | decoder->ae = malloc(sizeof(unsigned int) * n_states); |
| 205 | decoder->ae_next = malloc(sizeof(unsigned int) * n_states); |
| 206 | |
| 207 | decoder->state_history = malloc(sizeof(uint8_t) * n_states * (len + decoder->code->K - 1)); |
| 208 | |
| 209 | /* Classic reset */ |
| 210 | osmo_conv_decode_reset(decoder); |
| 211 | } |
| 212 | |
| 213 | void |
| 214 | osmo_conv_decode_reset(struct osmo_conv_decoder *decoder) |
| 215 | { |
| 216 | int i; |
| 217 | |
| 218 | /* Reset indexes */ |
| 219 | decoder->o_idx = 0; |
| 220 | decoder->p_idx = 0; |
| 221 | |
| 222 | /* Initial error (only state 0 is valid) */ |
| 223 | decoder->ae[0] = 0; |
| 224 | for (i=1; i<decoder->n_states; i++) { |
| 225 | decoder->ae[i] = MAX_AE; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | osmo_conv_decode_deinit(struct osmo_conv_decoder *decoder) |
| 231 | { |
| 232 | free(decoder->ae); |
| 233 | free(decoder->ae_next); |
| 234 | free(decoder->state_history); |
| 235 | |
| 236 | memset(decoder, 0x00, sizeof(struct osmo_conv_decoder)); |
| 237 | } |
| 238 | |
| 239 | int |
| 240 | osmo_conv_decode_scan(struct osmo_conv_decoder *decoder, |
| 241 | const sbit_t *input, int n) |
| 242 | { |
| 243 | const struct osmo_conv_code *code = decoder->code; |
| 244 | |
| 245 | int i, s, b, j; |
| 246 | |
| 247 | int n_states; |
| 248 | unsigned int *ae; |
| 249 | unsigned int *ae_next; |
| 250 | uint8_t *state_history; |
| 251 | sbit_t *in_sym; |
| 252 | |
| 253 | int i_idx, p_idx; |
| 254 | |
| 255 | /* Prepare */ |
| 256 | n_states = decoder->n_states; |
| 257 | |
| 258 | ae = decoder->ae; |
| 259 | ae_next = decoder->ae_next; |
| 260 | state_history = &decoder->state_history[n_states * decoder->o_idx]; |
| 261 | |
| 262 | in_sym = alloca(sizeof(sbit_t) * code->N); |
| 263 | |
| 264 | i_idx = 0; |
| 265 | p_idx = decoder->p_idx; |
| 266 | |
| 267 | /* Scan the treillis */ |
| 268 | for (i=0; i<n; i++) |
| 269 | { |
| 270 | /* Reset next accumulated error */ |
| 271 | for (s=0; s<n_states; s++) { |
| 272 | ae_next[s] = MAX_AE; |
| 273 | } |
| 274 | |
| 275 | /* Get input */ |
| 276 | if (code->puncture) { |
| 277 | /* Hard way ... */ |
| 278 | for (j=0; j<code->N; j++) { |
| 279 | int idx = ((decoder->o_idx + i) * code->N) + j; |
| 280 | if (idx == code->puncture[p_idx]) { |
| 281 | in_sym[j] = 0; /* Undefined */ |
| 282 | p_idx++; |
| 283 | } else { |
| 284 | in_sym[j] = input[i_idx]; |
| 285 | i_idx++; |
| 286 | } |
| 287 | } |
| 288 | } else { |
| 289 | /* Easy, just copy N bits */ |
| 290 | memcpy(in_sym, &input[i_idx], code->N); |
| 291 | i_idx += code->N; |
| 292 | } |
| 293 | |
| 294 | /* Scan all state */ |
| 295 | for (s=0; s<n_states; s++) |
| 296 | { |
| 297 | /* Scan possible input bits */ |
| 298 | for (b=0; b<2; b++) |
| 299 | { |
| 300 | int nae, ov, e; |
| 301 | uint8_t m; |
| 302 | |
| 303 | /* Next output and state */ |
| 304 | uint8_t out = code->next_output[s][b]; |
| 305 | uint8_t state = code->next_state[s][b]; |
| 306 | |
| 307 | /* New error for this path */ |
| 308 | nae = ae[s]; /* start from last error */ |
| 309 | m = 1 << (code->N - 1); /* mask for 'out' bit selection */ |
| 310 | |
| 311 | for (j=0; j<code->N; j++) { |
| 312 | ov = (out & m) ? -127 : 127; /* sbit_t value for it */ |
| 313 | e = ((int)in_sym[j]) - ov; /* raw error for this bit */ |
| 314 | nae += (e * e) >> 9; /* acc the squared/scaled value */ |
| 315 | m >>= 1; /* next mask bit */ |
| 316 | } |
| 317 | |
| 318 | /* Is it survivor ? */ |
| 319 | if (ae_next[state] > nae) { |
| 320 | ae_next[state] = nae; |
| 321 | state_history[(n_states * i) + state] = s; |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | /* Copy accumulated error */ |
| 327 | memcpy(ae, ae_next, sizeof(unsigned int) * n_states); |
| 328 | } |
| 329 | |
| 330 | /* Update decoder state */ |
| 331 | decoder->p_idx = p_idx; |
| 332 | decoder->o_idx += n; |
| 333 | |
| 334 | return i_idx; |
| 335 | } |
| 336 | |
| 337 | int |
| 338 | osmo_conv_decode_finish(struct osmo_conv_decoder *decoder, |
| 339 | const sbit_t *input) |
| 340 | { |
| 341 | const struct osmo_conv_code *code = decoder->code; |
| 342 | |
| 343 | int i, s, j; |
| 344 | |
| 345 | int n_states; |
| 346 | unsigned int *ae; |
| 347 | unsigned int *ae_next; |
| 348 | uint8_t *state_history; |
| 349 | sbit_t *in_sym; |
| 350 | |
| 351 | int i_idx, p_idx; |
| 352 | |
| 353 | /* Prepare */ |
| 354 | n_states = decoder->n_states; |
| 355 | |
| 356 | ae = decoder->ae; |
| 357 | ae_next = decoder->ae_next; |
| 358 | state_history = &decoder->state_history[n_states * decoder->o_idx]; |
| 359 | |
| 360 | in_sym = alloca(sizeof(sbit_t) * code->N); |
| 361 | |
| 362 | i_idx = 0; |
| 363 | p_idx = decoder->p_idx; |
| 364 | |
| 365 | /* Scan the treillis */ |
| 366 | for (i=0; i<code->K-1; i++) |
| 367 | { |
| 368 | /* Reset next accumulated error */ |
| 369 | for (s=0; s<n_states; s++) { |
| 370 | ae_next[s] = MAX_AE; |
| 371 | } |
| 372 | |
| 373 | /* Get input */ |
| 374 | if (code->puncture) { |
| 375 | /* Hard way ... */ |
| 376 | for (j=0; j<code->N; j++) { |
| 377 | int idx = ((decoder->o_idx + i) * code->N) + j; |
| 378 | if (idx == code->puncture[p_idx]) { |
| 379 | in_sym[j] = 0; /* Undefined */ |
| 380 | p_idx++; |
| 381 | } else { |
| 382 | in_sym[j] = input[i_idx]; |
| 383 | i_idx++; |
| 384 | } |
| 385 | } |
| 386 | } else { |
| 387 | /* Easy, just copy N bits */ |
| 388 | memcpy(in_sym, &input[i_idx], code->N); |
| 389 | i_idx += code->N; |
| 390 | } |
| 391 | |
| 392 | /* Scan all state */ |
| 393 | for (s=0; s<n_states; s++) |
| 394 | { |
| 395 | int nae, ov, e; |
| 396 | uint8_t m; |
| 397 | |
| 398 | /* Next output and state */ |
| 399 | uint8_t out; |
| 400 | uint8_t state; |
| 401 | |
| 402 | if (code->next_term_output) { |
| 403 | out = code->next_term_output[s]; |
| 404 | state = code->next_term_state[s]; |
| 405 | } else { |
| 406 | out = code->next_output[s][0]; |
| 407 | state = code->next_state[s][0]; |
| 408 | } |
| 409 | |
| 410 | /* New error for this path */ |
| 411 | nae = ae[s]; /* start from last error */ |
| 412 | m = 1 << (code->N - 1); /* mask for 'out' bit selection */ |
| 413 | |
| 414 | for (j=0; j<code->N; j++) { |
Sylvain Munaut | 1dd7c84 | 2011-04-28 22:30:30 +0200 | [diff] [blame] | 415 | int is = (int)in_sym[j]; |
| 416 | if (is) { |
| 417 | ov = (out & m) ? -127 : 127; /* sbit_t value for it */ |
| 418 | e = is - ov; /* raw error for this bit */ |
| 419 | nae += (e * e) >> 9; /* acc the squared/scaled value */ |
| 420 | } |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 421 | m >>= 1; /* next mask bit */ |
| 422 | } |
| 423 | |
| 424 | /* Is it survivor ? */ |
| 425 | if (ae_next[state] > nae) { |
| 426 | ae_next[state] = nae; |
| 427 | state_history[(n_states * i) + state] = s; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | /* Copy accumulated error */ |
| 432 | memcpy(ae, ae_next, sizeof(unsigned int) * n_states); |
| 433 | } |
| 434 | |
| 435 | /* Update decoder state */ |
| 436 | decoder->p_idx = p_idx; |
| 437 | decoder->o_idx += code->K - 1; |
| 438 | |
| 439 | return i_idx; |
| 440 | } |
| 441 | |
| 442 | int |
| 443 | osmo_conv_decode_get_output(struct osmo_conv_decoder *decoder, |
| 444 | ubit_t *output, int has_finish) |
| 445 | { |
| 446 | const struct osmo_conv_code *code = decoder->code; |
| 447 | |
| 448 | int min_ae; |
| 449 | uint8_t min_state, cur_state; |
| 450 | int i, s, n; |
| 451 | |
| 452 | uint8_t *sh_ptr; |
| 453 | |
| 454 | /* Find state with least error */ |
| 455 | min_ae = MAX_AE; |
| 456 | min_state = 0xff; |
| 457 | |
| 458 | for (s=0; s<decoder->n_states; s++) |
| 459 | { |
| 460 | if (decoder->ae[s] < min_ae) { |
| 461 | min_ae = decoder->ae[s]; |
| 462 | min_state = s; |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | if (min_state == 0xff) |
| 467 | return -1; |
| 468 | |
| 469 | /* Traceback */ |
| 470 | cur_state = min_state; |
| 471 | |
| 472 | n = decoder->o_idx; |
| 473 | |
| 474 | sh_ptr = &decoder->state_history[decoder->n_states * (n-1)]; |
| 475 | |
| 476 | /* No output for the K-1 termination input bits */ |
| 477 | if (has_finish) { |
| 478 | for (i=0; i<code->K-1; i++) { |
| 479 | cur_state = sh_ptr[cur_state]; |
| 480 | sh_ptr -= decoder->n_states; |
| 481 | } |
| 482 | n -= code->K - 1; |
| 483 | } |
| 484 | |
| 485 | /* Generate output backward */ |
| 486 | for (i=n-1; i>=0; i--) |
| 487 | { |
| 488 | min_state = cur_state; |
| 489 | cur_state = sh_ptr[cur_state]; |
| 490 | |
| 491 | sh_ptr -= decoder->n_states; |
| 492 | |
| 493 | if (code->next_state[cur_state][0] == min_state) |
| 494 | output[i] = 0; |
| 495 | else |
| 496 | output[i] = 1; |
| 497 | } |
| 498 | |
| 499 | return min_ae; |
| 500 | } |
| 501 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 502 | /*! \brief All-in-one convolutional decoding function |
| 503 | * \param[in] code description of convolutional code to be used |
| 504 | * \param[in] input array of soft bits (coded) |
| 505 | * \param[out] output array of unpacked bits (decoded) |
| 506 | * |
| 507 | * This is an all-in-one function, taking care of |
| 508 | * \ref osmo_conv_decode_init, \ref osmo_conv_decode_scan, |
| 509 | * \ref osmo_conv_decode_finish, \ref osmo_conv_decode_get_output and |
| 510 | * \ref osmo_conv_decode_deinit. |
| 511 | */ |
Sylvain Munaut | 19dc5c9 | 2011-04-23 16:09:19 +0200 | [diff] [blame] | 512 | int |
| 513 | osmo_conv_decode(const struct osmo_conv_code *code, |
| 514 | const sbit_t *input, ubit_t *output) |
| 515 | { |
| 516 | struct osmo_conv_decoder decoder; |
| 517 | int rv, l; |
| 518 | |
| 519 | osmo_conv_decode_init(&decoder, code, 0); |
| 520 | |
| 521 | l = osmo_conv_decode_scan(&decoder, input, code->len); |
| 522 | l = osmo_conv_decode_finish(&decoder, &input[l]); |
| 523 | |
| 524 | rv = osmo_conv_decode_get_output(&decoder, output, 1); |
| 525 | |
| 526 | osmo_conv_decode_deinit(&decoder); |
| 527 | |
| 528 | return rv; |
| 529 | } |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 530 | |
| 531 | /*! }@ */ |