1 /* -*- c++ -*- */
2 /*
3 * Copyright 2014 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3, or (at your option)
10 * any later version.
11 *
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
21 */
22
23 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26
27 #include "ccsds_encoder_impl.h"
28 #include <gnuradio/fec/generic_encoder.h>
29 #include <cstdio>
30
31 #include <gnuradio/fec/viterbi.h>
32
33 namespace gr {
34 namespace fec {
35 namespace code {
36
make(int frame_size,int start_state,cc_mode_t mode)37 generic_encoder::sptr ccsds_encoder::make(int frame_size, int start_state, cc_mode_t mode)
38 {
39 return generic_encoder::sptr(new ccsds_encoder_impl(frame_size, start_state, mode));
40 }
41
ccsds_encoder_impl(int frame_size,int start_state,cc_mode_t mode)42 ccsds_encoder_impl::ccsds_encoder_impl(int frame_size, int start_state, cc_mode_t mode)
43 : generic_encoder("ccsds_encoder"),
44 d_start_state(static_cast<unsigned char>(start_state)),
45 d_mode(mode)
46 {
47 d_max_frame_size = frame_size;
48 set_frame_size(frame_size);
49 }
50
~ccsds_encoder_impl()51 ccsds_encoder_impl::~ccsds_encoder_impl() {}
52
get_output_size()53 int ccsds_encoder_impl::get_output_size() { return d_output_size; }
54
get_input_size()55 int ccsds_encoder_impl::get_input_size()
56 {
57 return d_frame_size / 8; // packed byte input
58 }
59
get_input_conversion()60 const char* ccsds_encoder_impl::get_input_conversion()
61 {
62 // Expects packed data input; tell wrapper to pack it.
63 return "pack";
64 }
65
set_frame_size(unsigned int frame_size)66 bool ccsds_encoder_impl::set_frame_size(unsigned int frame_size)
67 {
68 bool ret = true;
69 if (frame_size > d_max_frame_size) {
70 GR_LOG_INFO(d_logger,
71 boost::format("tried to set frame to %1%; max possible is %2%") %
72 frame_size % d_max_frame_size);
73 frame_size = d_max_frame_size;
74 ret = false;
75 }
76
77 d_frame_size = frame_size;
78
79 if (d_mode == CC_TERMINATED) {
80 d_output_size = 2 * (d_frame_size + 7 - 1);
81 } else {
82 d_output_size = 2 * d_frame_size;
83 }
84
85 return ret;
86 }
87
rate()88 double ccsds_encoder_impl::rate() { return 0.5; }
89
generic_work(void * in_buffer,void * out_buffer)90 void ccsds_encoder_impl::generic_work(void* in_buffer, void* out_buffer)
91 {
92 unsigned char* in = (unsigned char*)in_buffer;
93 unsigned char* out = (unsigned char*)out_buffer;
94
95 unsigned char my_state = d_start_state;
96
97 if (d_mode == CC_TAILBITING) {
98 // Grab K-1 (6) bits of data from the last input byte to add
99 // onto the from of the encoding stream for tailbiting mode.
100 unsigned char sym = in[d_frame_size / 8 - 1];
101 for (unsigned int i = 0; i < 7 - 1; ++i) {
102 my_state = (my_state << 1) | ((sym >> (5 - i)) & 1);
103 }
104 }
105
106 my_state = encode(out, in, d_frame_size / 8, my_state);
107
108 if (d_mode == CC_TERMINATED) {
109 // encode works on bytes, but we are only adding some number
110 // of bits to the end of the frame, so we abuse the encode
111 // function by reshifting the start state within the
112 // for-loop and only taking the last two bits out of the
113 // encoded 2-bytes.
114 unsigned char end_bits[16];
115 for (unsigned int i = 0; i < (7 - 1); ++i) {
116 my_state = (my_state << 1) | ((d_start_state >> (7 - 2 - i)) & 1);
117 encode(&end_bits[0], &my_state, 1, my_state);
118 out[(i + d_frame_size) * 2 + 0] = end_bits[14];
119 out[(i + d_frame_size) * 2 + 1] = end_bits[15];
120 }
121 }
122
123 if (d_mode == CC_TRUNCATED) {
124 my_state = d_start_state;
125 }
126
127 d_start_state = my_state;
128 }
129
130 } /* namespace code */
131 } /* namespace fec */
132 } /* namespace gr */
133