1 /* -*- c++ -*- */
2 /*
3  * Copyright 2015 Free Software Foundation, Inc.
4  *
5  * This is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published
7  * by the Free Software Foundation; either version 3, or (at your
8  * option) any later version.
9  *
10  * This software is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this software; see the file COPYING.  If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24 
25 #include "ldpc_gen_mtrx_encoder_impl.h"
26 #include <sstream>
27 
28 namespace gr {
29 namespace fec {
30 namespace code {
31 
make(const ldpc_G_matrix::sptr G_obj)32 generic_encoder::sptr ldpc_gen_mtrx_encoder::make(const ldpc_G_matrix::sptr G_obj)
33 {
34     return generic_encoder::sptr(new ldpc_gen_mtrx_encoder_impl(G_obj));
35 }
36 
ldpc_gen_mtrx_encoder_impl(const ldpc_G_matrix::sptr G_obj)37 ldpc_gen_mtrx_encoder_impl::ldpc_gen_mtrx_encoder_impl(const ldpc_G_matrix::sptr G_obj)
38     : generic_encoder("ldpc_gen_mtrx_encoder")
39 {
40     // Generator matrix to use for encoding
41     d_G = G_obj;
42 
43     d_rate = static_cast<double>(d_G->n()) / static_cast<double>(d_G->k());
44 
45     // Set frame size to k, the # of bits in the information word
46     // All buffers and settings will be based on this value.
47     set_frame_size(d_G->k());
48 }
49 
~ldpc_gen_mtrx_encoder_impl()50 ldpc_gen_mtrx_encoder_impl::~ldpc_gen_mtrx_encoder_impl() {}
51 
get_output_size()52 int ldpc_gen_mtrx_encoder_impl::get_output_size() { return d_output_size; }
53 
get_input_size()54 int ldpc_gen_mtrx_encoder_impl::get_input_size() { return d_frame_size; }
55 
set_frame_size(unsigned int frame_size)56 bool ldpc_gen_mtrx_encoder_impl::set_frame_size(unsigned int frame_size)
57 {
58     bool ret = true;
59 
60     if (frame_size % d_G->k() != 0) {
61         GR_LOG_ERROR(d_logger,
62                      boost::format("Frame size (%1% bits) must be a "
63                                    "multiple of the information word "
64                                    "size of the LDPC matrix (%2%).") %
65                          frame_size % (d_G->k()));
66         throw std::runtime_error("ldpc_gen_mtrx_encoder: cannot use frame size.");
67     }
68 
69     d_frame_size = frame_size;
70 
71     d_output_size = static_cast<int>(d_rate * d_frame_size);
72 
73     return ret;
74 }
75 
rate()76 double ldpc_gen_mtrx_encoder_impl::rate() { return d_rate; }
77 
generic_work(void * inbuffer,void * outbuffer)78 void ldpc_gen_mtrx_encoder_impl::generic_work(void* inbuffer, void* outbuffer)
79 {
80     // Populate the information word
81     const unsigned char* in = (const unsigned char*)inbuffer;
82     unsigned char* out = (unsigned char*)outbuffer;
83 
84     int j = 0;
85     for (int i = 0; i < get_input_size(); i += d_G->k()) {
86         d_G->encode(&out[j], &in[i]);
87         j += d_G->n();
88     }
89 }
90 
91 } /* namespace code */
92 } /* namespace fec */
93 } /* namespace gr */
94