3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
30 #define DO_PRINT_DEBUG 0
33 * encode a certain number of output bits
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!
38 * n_bits_to_output: the number of bits per output stream to encode
40 * returns the actual number of bits used per input stream
45 (const code_input_ptr in_buf,
46 code_output_ptr out_buf,
47 size_t n_bits_to_output)
50 std::cerr << "encoder::encode{output}: Error: "
51 "input buffer is NULL.\n";
55 std::cerr << "encoder::encode{output}: Error: "
56 "output buffer is NULL.\n";
59 if (n_bits_to_output == 0) {
60 std::cerr << "encoder::encode{output}: Warning: "
61 "no output bits requested.\n";
65 // set the class-internal number of input bits and
66 // output bits left to encode
71 // check that there are enough output buffer items
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();
81 // check that there are enough input buffer items
83 size_t n_bits_to_input = compute_n_input_bits (n_bits_to_output);
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);
94 // set the correct number of I/O bits
96 d_n_bits_to_input = n_bits_to_input;
97 d_n_bits_to_output = n_bits_to_output;
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";
108 // call the private encode function
112 if (DO_PRINT_DEBUG) {
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";
121 // clear these buffers, just in case
126 // return the actual number of input bits used
128 return (n_bits_to_input - d_n_bits_to_input);
132 * encode a certain number of input bits
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!
137 * n_bits_to_input: the number of bits per input stream to encode
139 * returns the actual number of bits written per output stream
144 (const code_input_ptr in_buf,
145 size_t n_bits_to_input,
146 code_output_ptr out_buf)
149 std::cerr << "encoder::encode{input}: Error: input buffer is NULL.\n";
153 std::cerr << "encoder::encode{input}: Error: output buffer is NULL.\n";
156 if (n_bits_to_input == 0) {
157 std::cerr << "encoder::encode{input}: Warning: no input bits requested.\n";
161 // set the class-internal number of input and
162 // output bits left to encode
167 // check that there are enough input buffer items
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();
177 // check that there are enough output buffer items
179 size_t n_bits_to_output = compute_n_output_bits (n_bits_to_input);
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);
190 // set the correct number of I/O bits
192 d_n_bits_to_input = n_bits_to_input;
193 d_n_bits_to_output = n_bits_to_output;
195 if (DO_PRINT_DEBUG) {
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";
204 // call the private encode function
208 if (DO_PRINT_DEBUG) {
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";
217 // clear these buffers, just in case
222 // return the actual number of output bits written
224 return (n_bits_to_output - d_n_bits_to_output);