1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
/* -*- c++ -*- */
/*
* Copyright 2006 Free Software Foundation, Inc.
*
* This file is part of GNU Radio
*
* GNU Radio is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GNU Radio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <encoder.h>
#include <iostream>
#define DO_PRINT_DEBUG 0
/*
* encode a certain number of output bits
*
* the 'in_buf' and 'out_buf' must have enough memory to handle the
* number of input and output bits; no error checking is done!
*
* n_bits_to_output: the number of bits per output stream to encode
*
* returns the actual number of bits used per input stream
*/
size_t
encoder::encode
(const code_input_ptr in_buf,
code_output_ptr out_buf,
size_t n_bits_to_output)
{
if (in_buf == 0) {
std::cerr << "encoder::encode{output}: Error: "
"input buffer is NULL.\n";
assert (0);
}
if (out_buf == 0) {
std::cerr << "encoder::encode{output}: Error: "
"output buffer is NULL.\n";
assert (0);
}
if (n_bits_to_output == 0) {
std::cerr << "encoder::encode{output}: Warning: "
"no output bits requested.\n";
return (0);
}
// set the class-internal number of input bits and
// output bits left to encode
d_in_buf = in_buf;
d_out_buf = out_buf;
// check that there are enough output buffer items
if (d_out_buf->n_items_left() < n_bits_to_output) {
std::cerr << "encoder::encode{output}: Warning: "
"output buffer size (" << d_out_buf->n_items_left() <<
") is less than the desired number of output items (" <<
n_bits_to_output << ") ... using lower number.\n";
n_bits_to_output = d_out_buf->n_items_left();
}
// check that there are enough input buffer items
size_t n_bits_to_input = compute_n_input_bits (n_bits_to_output);
if (d_in_buf->n_items_left() < n_bits_to_input) {
std::cerr << "encoder::encode{output}: Warning: input buffer size (" <<
d_in_buf->n_items_left() << ") is less than the computed number "
"of required input items (" << n_bits_to_input <<
") ... using lower number.\n";
n_bits_to_input = d_in_buf->n_items_left();
n_bits_to_output = compute_n_output_bits (n_bits_to_input);
}
// set the correct number of I/O bits
d_n_bits_to_input = n_bits_to_input;
d_n_bits_to_output = n_bits_to_output;
if (DO_PRINT_DEBUG) {
std::cout <<
"Before Encoding{output}:\n"
" # output bits = " << d_n_bits_to_output << "\n"
" # input bits = " << d_n_bits_to_input << "\n"
" # output bits used = " << d_out_buf->n_items_used() << "\n"
" # input bits used = " << d_in_buf->n_items_used() << "\n";
}
// call the private encode function
encode_private ();
if (DO_PRINT_DEBUG) {
std::cout <<
"After Encoding{output}:\n"
" # output bits = " << d_n_bits_to_output << "\n"
" # input bits = " << d_n_bits_to_input << "\n"
" # output bits used = " << d_out_buf->n_items_used() << "\n"
" # input bits used = " << d_in_buf->n_items_used() << "\n";
}
// clear these buffers, just in case
d_in_buf = 0;
d_out_buf = 0;
// return the actual number of input bits used
return (n_bits_to_input - d_n_bits_to_input);
}
/*
* encode a certain number of input bits
*
* the 'in_buf' and 'out_buf' must have enough memory to handle the
* number of input and output bits; no error checking is done!
*
* n_bits_to_input: the number of bits per input stream to encode
*
* returns the actual number of bits written per output stream
*/
size_t
encoder::encode
(const code_input_ptr in_buf,
size_t n_bits_to_input,
code_output_ptr out_buf)
{
if (in_buf == 0) {
std::cerr << "encoder::encode{input}: Error: input buffer is NULL.\n";
assert (0);
}
if (out_buf == 0) {
std::cerr << "encoder::encode{input}: Error: output buffer is NULL.\n";
assert (0);
}
if (n_bits_to_input == 0) {
std::cerr << "encoder::encode{input}: Warning: no input bits requested.\n";
return (0);
}
// set the class-internal number of input and
// output bits left to encode
d_in_buf = in_buf;
d_out_buf = out_buf;
// check that there are enough input buffer items
if (d_in_buf->n_items_left() < n_bits_to_input) {
std::cerr << "encoder::encode{input}: Warning: input buffer size (" <<
d_in_buf->n_items_left() << ") is less than the desired number "
"of input items (" << n_bits_to_input <<
") ... using lower number.\n";
n_bits_to_input = d_in_buf->n_items_left();
}
// check that there are enough output buffer items
size_t n_bits_to_output = compute_n_output_bits (n_bits_to_input);
if (d_out_buf->n_items_left() < n_bits_to_output) {
std::cerr << "encoder::encode{input}: Warning: output buffer size (" <<
d_out_buf->n_items_left() << ") is less than the computed number "
"of required output items (" << n_bits_to_output <<
") ... using lower number.\n";
n_bits_to_output = d_out_buf->n_items_left();
n_bits_to_input = compute_n_input_bits (n_bits_to_output);
}
// set the correct number of I/O bits
d_n_bits_to_input = n_bits_to_input;
d_n_bits_to_output = n_bits_to_output;
if (DO_PRINT_DEBUG) {
std::cout <<
"Before Encoding{input}:\n"
" # output bits = " << d_n_bits_to_output << "\n"
" # input bits = " << d_n_bits_to_input << "\n"
" # output bits used = " << d_out_buf->n_items_used() << "\n"
" # input bits used = " << d_in_buf->n_items_used() << "\n";
}
// call the private encode function
encode_private ();
if (DO_PRINT_DEBUG) {
std::cout <<
"After Encoding{input}:\n"
" # output bits = " << d_n_bits_to_output << "\n"
" # input bits = " << d_n_bits_to_input << "\n"
" # output bits used = " << d_out_buf->n_items_used() << "\n"
" # input bits used = " << d_in_buf->n_items_used() << "\n";
}
// clear these buffers, just in case
d_in_buf = 0;
d_out_buf = 0;
// return the actual number of output bits written
return (n_bits_to_output - d_n_bits_to_output);
}
|