Updated FSF address in all files. Fixes ticket:51
[debian/gnuradio] / gr-error-correcting-codes / src / lib / libecc / decoder.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 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 2, 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 <decoder.h>
28 #include <iostream>
29
30 #define DO_PRINT_DEBUG 0
31
32 /*
33  * decode a certain number of output bits
34  *
35  * the 'in_buf' and 'out_buf' must have enough memory to handle the
36  *     number of input items and output bits; no error checking is done!
37  *
38  * n_bits_to_output: the number of bits per output stream to decode.
39  *
40  * returns the actual number of items used per input stream.
41  */
42
43 size_t
44 decoder::decode
45 (const code_input_ptr in_buf,
46  code_output_ptr out_buf,
47  size_t n_bits_to_output)
48 {
49   if (in_buf == 0) {
50     std::cerr << "decoder::decode{output}: Error: input buffer is NULL.\n";
51     assert (0);
52   }
53   if (out_buf == 0) {
54     std::cerr << "decoder::decode{output}: Error: output buffer is NULL.\n";
55     assert (0);
56   }
57   if (n_bits_to_output == 0) {
58     std::cerr << "decoder::decode{output}: Warning: "
59       "no output bits requested.\n";
60     return (0);
61   }
62
63   // set the class-internal number of input metrics
64   // and output bits left to decode
65
66   d_in_buf = in_buf;
67   d_out_buf = out_buf;
68
69   // check that there are enough output buffer items
70
71   if (d_out_buf->n_items_left() < n_bits_to_output) {
72     std::cerr << "encoder::encode{output}: Warning: output buffer size (" <<
73       d_out_buf->n_items_left() << ") is less than the desired number "
74       "of output items (" << n_bits_to_output <<
75       ") ... using lower number.\n";
76     n_bits_to_output = d_out_buf->n_items_left();
77   }
78
79   // check that there are enough input buffer items
80
81   size_t n_items_to_input = compute_n_input_items (n_bits_to_output);
82
83   if (d_in_buf->n_items_left() < n_items_to_input) {
84     std::cerr << "encoder::encode{output}: Warning: input buffer size (" <<
85       d_in_buf->n_items_left() << ") is less than the computed number "
86       "of required input items (" << n_items_to_input <<
87       ") ... using lower number.\n";
88     n_items_to_input = d_in_buf->n_items_left();
89     n_bits_to_output = compute_n_output_bits (n_items_to_input);
90   }
91
92   // set the correct number of I/O bits
93
94   d_n_items_to_input = n_items_to_input;
95   d_n_bits_to_output = n_bits_to_output;
96
97   if (DO_PRINT_DEBUG) {
98     std::cout <<
99       "Before Decoding{output}:\n"
100       "  # output bits      = " << d_n_bits_to_output << "\n"
101       "  # input items      = " << d_n_items_to_input << "\n"
102       "  # output bits used = " << d_out_buf->n_items_used() << "\n"
103       "  # input items used = " << d_in_buf->n_items_used() << "\n";
104   }
105
106   // call the private decode function
107
108   decode_private ();
109
110   if (DO_PRINT_DEBUG) {
111     std::cout <<
112       "After Encoding{output}:\n"
113       "  # output bits      = " << d_n_bits_to_output << "\n"
114       "  # input items      = " << d_n_items_to_input << "\n"
115       "  # output bits used = " << d_out_buf->n_items_used() << "\n"
116       "  # input items used = " << d_in_buf->n_items_used() << "\n";
117   }
118
119   size_t n_items_used = d_in_buf->n_items_used ();
120
121   // clear these buffers, just in case
122
123   d_in_buf = 0;
124   d_out_buf = 0;
125
126   // return the actual number of input bits used
127
128   return (n_items_used);
129 }
130
131 /*
132  * decode a certain number of input metrics
133  *
134  * the 'in_buf' and 'out_buf' must have enough memory to handle the
135  *     number of input items and output bits; no error checking is done!
136  *
137  * n_items_to_input: the number of items per input stream to decode
138  *
139  * returns the actual number of bits written per output stream
140  */
141
142 size_t
143 decoder::decode
144 (const code_input_ptr in_buf,
145  size_t n_items_to_input,
146  code_output_ptr out_buf)
147 {
148   if (in_buf == 0) {
149     std::cerr << "encoder::encode{input}: Error: input buffer is NULL.\n";
150     assert (0);
151   }
152   if (out_buf == 0) {
153     std::cerr << "encoder::encode{input}: Error: output buffer is NULL.\n";
154     assert (0);
155   }
156   if (n_items_to_input == 0) {
157     std::cerr << "encoder::encode{input}: Warning: "
158       "no input items requested.\n";
159     return (0);
160   }
161
162   // set the class-internal number of input metrics and
163   // output bits left to decode
164
165   d_in_buf = in_buf;
166   d_out_buf = out_buf;
167
168   // check that there are enough input buffer items
169
170   if (d_in_buf->n_items_left() < n_items_to_input) {
171     std::cerr << "encoder::encode{input}: Warning: input buffer size (" <<
172       d_in_buf->n_items_left() << ") is less than the desired number "
173       "of input items (" << n_items_to_input <<
174       ") ... using lower number.\n";
175     n_items_to_input = d_in_buf->n_items_left();
176   }
177
178   // check that there are enough output buffer items
179
180   size_t n_bits_to_output = compute_n_output_bits (n_items_to_input);
181
182   if (d_out_buf->n_items_left() < n_bits_to_output) {
183     std::cerr << "encoder::encode{input}: Warning: output buffer size (" <<
184       d_out_buf->n_items_left() << ") is less than the computed number "
185       "of required output items (" << n_bits_to_output <<
186       ") ... using lower number.\n";
187     n_bits_to_output = d_out_buf->n_items_left();
188     n_items_to_input = compute_n_input_items (n_bits_to_output);
189   }
190
191   // set the correct number of I/O bits
192
193   d_n_items_to_input = n_items_to_input;
194   d_n_bits_to_output = n_bits_to_output;
195
196   if (DO_PRINT_DEBUG) {
197     std::cout <<
198       "Before Decoding{output}:\n"
199       "  # output bits      = " << d_n_bits_to_output << "\n"
200       "  # input items      = " << d_n_items_to_input << "\n"
201       "  # output bits used = " << d_out_buf->n_items_used() << "\n"
202       "  # input items used = " << d_in_buf->n_items_used() << "\n";
203   }
204
205   // call the private decode function
206
207   decode_private ();
208
209   if (DO_PRINT_DEBUG) {
210     std::cout <<
211       "After Encoding{output}:\n"
212       "  # output bits      = " << d_n_bits_to_output << "\n"
213       "  # input items      = " << d_n_items_to_input << "\n"
214       "  # output bits used = " << d_out_buf->n_items_used() << "\n"
215       "  # input items used = " << d_in_buf->n_items_used() << "\n";
216   }
217
218   size_t n_items_used = d_out_buf->n_items_used();
219
220   // clear these buffers, just in case
221
222   d_in_buf = 0;
223   d_out_buf = 0;
224
225   // return the actual number of output bits written
226
227   return (n_items_used);
228 }