Merged r4518:5130 from developer branch n4hy/ofdm into trunk, passes distcheck.
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_stream_mux.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 <gr_stream_mux.h>
28 #include <gr_io_signature.h>
29
30 #define VERBOSE 0
31
32 gr_stream_mux_sptr
33 gr_make_stream_mux (size_t itemsize, const std::vector<int> &lengths)
34 {
35   return gr_stream_mux_sptr (new gr_stream_mux (itemsize, lengths));
36 }
37
38 gr_stream_mux::gr_stream_mux (size_t itemsize, const std::vector<int> &lengths)
39   : gr_block ("stream_mux",
40               gr_make_io_signature (1, -1, itemsize),
41               gr_make_io_signature (1, 1, itemsize)),
42     d_itemsize(itemsize),
43     d_stream(0),
44     d_residual(0),
45     d_lengths(lengths)
46 {
47 }    
48
49 gr_stream_mux::~gr_stream_mux(void)
50 {
51 }
52
53 void 
54 gr_stream_mux::forecast (int noutput_items, gr_vector_int &ninput_items_required)
55 {
56   unsigned ninputs = ninput_items_required.size ();
57   for (unsigned i = 0; i < ninputs; i++)
58     ninput_items_required[i] = 0;
59 }
60
61
62 int
63 gr_stream_mux::general_work(int noutput_items,
64                             gr_vector_int &ninput_items,
65                             gr_vector_const_void_star &input_items,
66                             gr_vector_void_star &output_items)
67 {
68     
69     char *out = (char *) output_items[0];
70     const char *in;
71
72     int acc = 0;
73     int N=0;
74     int M=0;
75     std::vector<int> consume_vector(d_lengths.size(), 0);
76
77     #if VERBOSE
78     printf("mux: nouput_items: %d   d_stream: %d\n", noutput_items, d_stream);
79     for(int i = 0; i < d_lengths.size(); i++)
80       printf("\tninput_items[%d]: %d\n", i, ninput_items[i]);
81     #endif
82
83     in = (const char *) input_items[d_stream];
84
85     if(d_residual) {
86       #if VERBOSE
87       printf("Cleaning up residual bytes (%d) from stream %d\n", d_residual, d_stream);
88       #endif
89       
90       // get the number of items available in input stream up to the
91       // num items required
92       N=std::min(d_residual, ninput_items[d_stream]);
93       
94       // get the number of items we can put into the output buffer
95       M=std::min(N, noutput_items);
96
97       // copy the items to the output buff
98       memcpy(out, in, M*d_itemsize);
99
100       // increment the output accumulator
101       acc += M;
102
103       // keep track of items consumed
104       consume_vector[d_stream]=M;
105       
106       // keep track if there are residual items left from the input stream
107       d_residual -= M;
108
109       #if VERBOSE
110       printf("Stream: %d (%x)  Wrote: %d bytes  Output has: %d bytes  residual: %d bytes\n", 
111              d_stream, in, M, acc, d_residual);
112       #endif
113
114       // if no residual items, we're done with this input stream for
115       // this round
116       if (!d_residual) {
117         if(d_stream == d_lengths.size() - 1) {
118           d_stream=0;  // wrap stream pointer
119         }
120         else {
121           d_stream++;  // or increment the stream pointer
122         }
123         #if VERBOSE
124         printf("Going to next stream: %d\n", d_stream);
125         #endif
126         in = ((const char *) (input_items[d_stream])) + d_itemsize*consume_vector[d_stream];
127       }
128     }
129
130     if(!d_residual) {
131       while (acc<noutput_items){
132         // get the number of items available in input stream up to the
133         // num items required
134         N=std::min(d_lengths[d_stream], ninput_items[d_stream]);
135         
136         // get the number of items we can put into the output buffer
137         M=std::min(N, noutput_items-acc);
138         
139         // copy the items to the output buff
140         memcpy(out+acc*d_itemsize,in,M*d_itemsize);
141         
142         // increment the output accumulator
143         acc += M;
144         
145         // keep track of items consumed
146         consume_vector[d_stream]+=M;
147         
148         // keep track if there are residual items left from the input stream
149         d_residual=d_lengths[d_stream] - M;
150         
151         #if VERBOSE
152         printf("Stream: %d (%x)  Wrote: %d bytes  Output has: %d bytes  residual: %d bytes\n", 
153                d_stream, in, M, acc, d_residual);
154         #endif
155
156         // if no residual items, we're done with this input stream for
157         // this round
158         if (!d_residual) {
159           if(d_stream == d_lengths.size() - 1) {
160             d_stream=0;  // wrap stream pointer
161           }
162           else {
163             d_stream++;  // or increment the stream pointer
164           }
165           #if VERBOSE
166           printf("Going to next stream: %d\n", d_stream);
167           #endif
168           
169           // get next stream pointer
170           in = ((const char *) (input_items[d_stream])) + d_itemsize*consume_vector[d_stream];
171         }
172         else{ 
173           break;
174         }   
175       }
176     }
177     
178     for (unsigned int j=0;j<d_lengths.size();j++){
179       consume(j,consume_vector[j]);
180
181       #if VERBOSE
182       printf("consuming: %d on stream: %d\n", consume_vector[j], j);
183       #endif
184     }
185
186     #if VERBOSE
187     printf("mux: returning: %d\n\n", acc);
188     #endif
189
190     return acc;
191                 
192 }