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