Houston, we have a trunk.
[debian/gnuradio] / gr-error-correcting-codes / src / lib / gr_streams_encode_convolutional.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 <gr_streams_encode_convolutional.h>
28 #include <gr_io_signature.h>
29 #include <assert.h>
30 #include <iostream>
31
32 gr_streams_encode_convolutional_sptr 
33 gr_make_streams_encode_convolutional
34 (int frame_size_bits,
35  int n_code_inputs,
36  int n_code_outputs,
37  const std::vector<int> &code_generator,
38  bool do_termination,
39  int start_memory_state,
40  int end_memory_state)
41 {
42   return gr_streams_encode_convolutional_sptr
43     (new gr_streams_encode_convolutional (frame_size_bits,
44                                           n_code_inputs,
45                                           n_code_outputs,
46                                           code_generator,
47                                           do_termination,
48                                           start_memory_state,
49                                           end_memory_state));
50 }
51
52 gr_streams_encode_convolutional_sptr 
53 gr_make_streams_encode_convolutional_feedback
54 (int frame_size_bits,
55  int n_code_inputs,
56  int n_code_outputs,
57  const std::vector<int> &code_generator,
58  const std::vector<int> &code_feedback,
59  bool do_termination,
60  int start_memory_state,
61  int end_memory_state)
62 {
63   return gr_streams_encode_convolutional_sptr
64     (new gr_streams_encode_convolutional (frame_size_bits,
65                                           n_code_inputs,
66                                           n_code_outputs,
67                                           code_generator,
68                                           code_feedback,
69                                           do_termination,
70                                           start_memory_state,
71                                           end_memory_state));
72 }
73
74 gr_streams_encode_convolutional::gr_streams_encode_convolutional
75 (int frame_size_bits,
76  int n_code_inputs,
77  int n_code_outputs,
78  const std::vector<int> &code_generators,
79  bool do_termination,
80  int start_memory_state,
81  int end_memory_state)
82   : gr_block ("streams_encode_convolutional",
83               gr_make_io_signature (0, 0, 0),
84               gr_make_io_signature (0, 0, 0))
85 {
86   // error checking is done by the encoder class itself;
87   // just pass items on here.
88
89   d_encoder = new encoder_convolutional_ic1_ic1 (frame_size_bits,
90                                                  n_code_inputs,
91                                                  n_code_outputs,
92                                                  code_generators,
93                                                  do_termination,
94                                                  start_memory_state,
95                                                  end_memory_state);
96
97   setup_io_signatures (n_code_inputs, n_code_outputs);
98 }
99
100 gr_streams_encode_convolutional::gr_streams_encode_convolutional
101 (int frame_size_bits,
102  int n_code_inputs,
103  int n_code_outputs,
104  const std::vector<int> &code_generators,
105  const std::vector<int> &code_feedback,
106  bool do_termination,
107  int start_memory_state,
108  int end_memory_state)
109   : gr_block ("streams_encode_convolutional_feedback",
110               gr_make_io_signature (0, 0, 0),
111               gr_make_io_signature (0, 0, 0))
112 {
113   // error checking is done by the encoder class itself;
114   // just pass items on here.
115
116   d_encoder = new encoder_convolutional_ic1_ic1 (frame_size_bits,
117                                                  n_code_inputs,
118                                                  n_code_outputs,
119                                                  code_generators,
120                                                  code_feedback,
121                                                  do_termination,
122                                                  start_memory_state,
123                                                  end_memory_state);
124
125   setup_io_signatures (n_code_inputs, n_code_outputs);
126 }
127
128 void
129 gr_streams_encode_convolutional::setup_io_signatures
130 (int n_code_inputs,
131  int n_code_outputs)
132 {
133   fprintf (stderr, "gr_s_e_c: d_encoder == %X\n", (unsigned int)d_encoder);
134
135   // create the correct input signature; 1 bit per input char
136
137   set_input_signature (gr_make_io_signature (n_code_inputs,
138                                              n_code_inputs,
139                                              sizeof (char)));
140
141   // create the correct output signature; 1 bit per output char
142
143   set_output_signature (gr_make_io_signature (n_code_outputs,
144                                               n_code_outputs,
145                                               sizeof (char)));
146
147   // no need to set the output multiple (to 1 item), since 1 is the
148   // default, and the encoder class handles the rest internally
149
150   // no need to set the relative rate (# out / # in) because for each
151   // input bit per input stream there is 1 output bit per output
152   // stream ... with a little added for termination, but ignore those.
153 }
154
155 gr_streams_encode_convolutional::~gr_streams_encode_convolutional
156 ()
157 {
158   delete d_encoder;
159   d_encoder = 0;
160 }
161
162 /*
163  * Compute the number of input items needed to produce 'n_output'
164  * bits.  Handled internally by the encoder.
165  */
166
167 void gr_streams_encode_convolutional::forecast
168 (int noutput_items,
169  gr_vector_int &ninput_items_required)
170 {
171   fprintf (stderr, "gr_s_e_c::forecast: #out = %d\n",
172           noutput_items);
173
174   int ninput_items = d_encoder->compute_n_input_bits (noutput_items);
175
176   fprintf (stderr, "s_e_c::forecast: #out = %d, #in = %d\n",
177           noutput_items, ninput_items);
178
179   size_t ninputs = ninput_items_required.size();
180   for (size_t n = 0; n < ninputs; n++)
181     ninput_items_required[n] = ninput_items;
182 }
183
184 int
185 gr_streams_encode_convolutional::general_work
186 (int noutput_items,
187  gr_vector_int &ninput_items,
188  gr_vector_const_void_star &input_items,
189  gr_vector_void_star &output_items)
190 {
191   fprintf (stderr, "gr_s_e_c::general_work: Starting #out = %d\n",
192            noutput_items);
193
194   // FIXME: compute the actual number of output items (1 bit char's) created.
195
196   size_t t_n_input_items = d_encoder->compute_n_input_bits (noutput_items);
197
198 #if 0
199   size_t t_n_output_items = d_encoder->compute_n_output_bits (t_n_input_items);
200
201   assert (t_n_output_items == ((size_t)noutput_items));
202 #endif
203
204   // "work" is handled by the encoder; which returns the actual number
205   // of input items (1-bit char's) used.
206
207   t_n_input_items = d_encoder->encode ((const char **)(&input_items[0]), 
208                                        (char **)(&output_items[0]),
209                                        (size_t) noutput_items);
210
211   assert (0);
212
213   // consume the number of used input items on all input streams
214
215   consume_each (t_n_input_items);
216
217   // returns number of items written to each output stream
218
219   return (noutput_items);
220 }