Houston, we have a trunk.
[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., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, 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 char** in_buf,
46  char** out_buf,
47  size_t n_bits_to_output)
48 {
49   if (DO_PRINT_DEBUG) {
50     std::cout << "encode{out}(): Starting:";
51   }
52
53   // set the class-internal number of input bits and
54   // output bits left to encode
55
56   size_t saved_n_input_bits;
57   saved_n_input_bits = d_n_input_bits_left =
58     compute_n_input_bits (n_bits_to_output);
59   d_n_output_bits_left = n_bits_to_output;
60
61   if (DO_PRINT_DEBUG) {
62     std::cout <<
63       "# output bits provided = " << d_n_output_bits_left << "\n"
64       "# input bits computed  = " << d_n_input_bits_left << "\n";
65   }
66
67   // call the private encode function
68
69   encode_private (in_buf, out_buf);
70
71   if (DO_PRINT_DEBUG) {
72     std::cout <<
73       "n_input_bits_used  = " <<
74       (saved_n_input_bits - d_n_input_bits_left) << "\n"
75       "n_output_bits_used = " <<
76       (n_bits_to_output - d_n_output_bits_left) << '\n';
77   }
78
79   // return the actual number of input bits used
80
81   return (saved_n_input_bits - d_n_input_bits_left);
82 }
83
84 /*
85  * encode a certain number of input bits
86  *
87  * the 'in_buf' and 'out_buf' must have enough memory to handle the
88  *     number of input and output bits; no error checking is done!
89  *
90  * n_bits_to_input: the number of bits per input stream to encode
91  *
92  * returns the actual number of bits written per output stream
93  */
94
95 size_t
96 encoder::encode
97 (const char** in_buf,   
98  size_t n_bits_to_input,
99  char** out_buf)
100 {
101   // set the class-internal number of input and
102   // output bits left to encode
103
104   size_t saved_n_output_bits;
105   saved_n_output_bits = d_n_output_bits_left =
106     compute_n_output_bits (n_bits_to_input);
107   d_n_input_bits_left = n_bits_to_input;
108
109   // call the private encode function
110
111   encode_private (in_buf, out_buf);
112
113   if (DO_PRINT_DEBUG) {
114     std::cout << "n_input_bits_used = " <<
115       (n_bits_to_input - d_n_input_bits_left) << '\n';
116     std::cout << "n_output_bits_used = " <<
117       (saved_n_output_bits - d_n_output_bits_left) << '\n';
118   }
119
120   // return the actual number of output bits written
121
122   return (saved_n_output_bits - d_n_output_bits_left);
123 }