Imported Upstream version 3.2.2
[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 3, 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 #include <string.h>
30 #include <cstdio>
31
32 #define VERBOSE 0
33
34 gr_stream_mux_sptr
35 gr_make_stream_mux (size_t itemsize, const std::vector<int> &lengths)
36 {
37   return gr_stream_mux_sptr (new gr_stream_mux (itemsize, lengths));
38 }
39
40 gr_stream_mux::gr_stream_mux (size_t itemsize, const std::vector<int> &lengths)
41   : gr_block ("stream_mux",
42               gr_make_io_signature (1, -1, itemsize),
43               gr_make_io_signature (1, 1, itemsize)),
44     d_itemsize(itemsize),
45     d_stream(0),
46     d_residual(0),
47     d_lengths(lengths)
48 {
49   if(d_lengths[d_stream] == 0) {
50     increment_stream();
51   }
52   d_residual = d_lengths[d_stream];
53 }    
54
55 gr_stream_mux::~gr_stream_mux(void)
56 {
57 }
58
59 void 
60 gr_stream_mux::forecast (int noutput_items, gr_vector_int &ninput_items_required)
61 {
62   unsigned ninputs = ninput_items_required.size ();
63   for (unsigned i = 0; i < ninputs; i++)
64     ninput_items_required[i] = (d_lengths[i] == 0 ? 0 : 1);
65 }
66
67 void gr_stream_mux::increment_stream()
68 {
69   do {
70     d_stream = (d_stream+1) % d_lengths.size();
71   } while(d_lengths[d_stream] == 0);
72   
73   d_residual = d_lengths[d_stream];
74 }
75
76 int
77 gr_stream_mux::general_work(int noutput_items,
78                             gr_vector_int &ninput_items,
79                             gr_vector_const_void_star &input_items,
80                             gr_vector_void_star &output_items)
81 {
82     char *out = (char *) output_items[0];
83     const char *in;
84     int out_index = 0;
85     std::vector<int> input_index(d_lengths.size(), 0);
86
87     if(VERBOSE) {
88       printf("mux: nouput_items: %d   d_stream: %d\n", noutput_items, d_stream);
89       for(size_t i = 0; i < d_lengths.size(); i++)
90         printf("\tninput_items[%zu]: %d\n", i, ninput_items[i]);
91     }
92
93     while (1) {
94       int r = std::min(noutput_items - out_index,
95                        std::min(d_residual,
96                                 ninput_items[d_stream] - input_index[d_stream]));
97       if(VERBOSE) {
98         printf("mux: r=%d\n", r);
99         printf("\tnoutput_items - out_index: %d\n", 
100                noutput_items - out_index);
101         printf("\td_residual: %d\n", 
102                d_residual);
103         printf("\tninput_items[d_stream] - input_index[d_stream]: %d\n", 
104                ninput_items[d_stream] - input_index[d_stream]);
105       }
106
107       if(r <= 0) {
108         return out_index;
109       }
110
111       in = (const char *) input_items[d_stream] + input_index[d_stream]*d_itemsize;
112       
113       memcpy(&out[out_index*d_itemsize], in, r*d_itemsize);
114       out_index += r;
115       input_index[d_stream] += r;
116       d_residual -= r;
117
118       consume(d_stream, r);
119       
120       if(d_residual == 0) {
121         increment_stream();
122       }
123     }
124 }