diff options
author | Johnathan Corgan | 2012-08-29 09:29:50 -0700 |
---|---|---|
committer | Johnathan Corgan | 2012-08-30 13:56:24 -0700 |
commit | 0a5bb996ca4f4bb350cf91b00b4c3db5a5c4eb75 (patch) | |
tree | d702945cbcf0739a4044fc91c6c334a6e7c762b4 /gr-blocks/lib | |
parent | 842c1cd3da19042a134cea722ce1d73f0a21e594 (diff) | |
download | gnuradio-0a5bb996ca4f4bb350cf91b00b4c3db5a5c4eb75.tar.gz gnuradio-0a5bb996ca4f4bb350cf91b00b4c3db5a5c4eb75.tar.bz2 gnuradio-0a5bb996ca4f4bb350cf91b00b4c3db5a5c4eb75.zip |
blocks: added gr::blocks::stream_mux
Diffstat (limited to 'gr-blocks/lib')
-rw-r--r-- | gr-blocks/lib/CMakeLists.txt | 1 | ||||
-rw-r--r-- | gr-blocks/lib/stream_mux_impl.cc | 126 | ||||
-rw-r--r-- | gr-blocks/lib/stream_mux_impl.h | 55 |
3 files changed, 182 insertions, 0 deletions
diff --git a/gr-blocks/lib/CMakeLists.txt b/gr-blocks/lib/CMakeLists.txt index 9b0043d4b..72e09d5f9 100644 --- a/gr-blocks/lib/CMakeLists.txt +++ b/gr-blocks/lib/CMakeLists.txt @@ -157,6 +157,7 @@ list(APPEND gr_blocks_sources repeat_impl.cc short_to_char_impl.cc short_to_float_impl.cc + stream_mux_impl.cc uchar_array_to_float.cc uchar_to_float_impl.cc ) diff --git a/gr-blocks/lib/stream_mux_impl.cc b/gr-blocks/lib/stream_mux_impl.cc new file mode 100644 index 000000000..214734c4b --- /dev/null +++ b/gr-blocks/lib/stream_mux_impl.cc @@ -0,0 +1,126 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 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 3, 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., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "stream_mux_impl.h" +#include <gr_io_signature.h> +#include <string.h> +#include <cstdio> + +#define VERBOSE 0 + +namespace gr { + namespace blocks { + + stream_mux::sptr stream_mux::make(size_t itemsize, const std::vector<int> &lengths) + { + return gnuradio::get_initial_sptr(new stream_mux_impl(itemsize, lengths)); + } + + stream_mux_impl::stream_mux_impl(size_t itemsize, const std::vector<int> &lengths) + : gr_block("stream_mux", + gr_make_io_signature (1, -1, itemsize), + gr_make_io_signature (1, 1, itemsize)), + d_itemsize(itemsize), + d_stream(0), + d_residual(0), + d_lengths(lengths) + { + if(d_lengths[d_stream] == 0) { + increment_stream(); + } + d_residual = d_lengths[d_stream]; + } + + void + stream_mux_impl::forecast(int noutput_items, gr_vector_int &ninput_items_required) + { + unsigned ninputs = ninput_items_required.size (); + for (unsigned i = 0; i < ninputs; i++) + ninput_items_required[i] = (d_lengths[i] == 0 ? 0 : 1); + } + + void + stream_mux_impl::increment_stream() + { + do { + d_stream = (d_stream+1) % d_lengths.size(); + } while(d_lengths[d_stream] == 0); + + d_residual = d_lengths[d_stream]; + } + + + int + stream_mux_impl::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + char *out = (char *) output_items[0]; + const char *in; + int out_index = 0; + std::vector<int> input_index(d_lengths.size(), 0); + + if(VERBOSE) { + printf("mux: nouput_items: %d d_stream: %d\n", noutput_items, d_stream); + for(size_t i = 0; i < d_lengths.size(); i++) + printf("\tninput_items[%zu]: %d\n", i, ninput_items[i]); + } + + while (1) { + int r = std::min(noutput_items - out_index, + std::min(d_residual, + ninput_items[d_stream] - input_index[d_stream])); + if(VERBOSE) { + printf("mux: r=%d\n", r); + printf("\tnoutput_items - out_index: %d\n", + noutput_items - out_index); + printf("\td_residual: %d\n", + d_residual); + printf("\tninput_items[d_stream] - input_index[d_stream]: %d\n", + ninput_items[d_stream] - input_index[d_stream]); + } + + if(r <= 0) { + return out_index; + } + + in = (const char *) input_items[d_stream] + input_index[d_stream]*d_itemsize; + + memcpy(&out[out_index*d_itemsize], in, r*d_itemsize); + out_index += r; + input_index[d_stream] += r; + d_residual -= r; + + consume(d_stream, r); + + if(d_residual == 0) { + increment_stream(); + } + } + } + } /* namespace blocks */ +} /* namespace gr */ diff --git a/gr-blocks/lib/stream_mux_impl.h b/gr-blocks/lib/stream_mux_impl.h new file mode 100644 index 000000000..7b2dac95c --- /dev/null +++ b/gr-blocks/lib/stream_mux_impl.h @@ -0,0 +1,55 @@ +/* -*- c++ -*- */ +/* + * Copyright 2012 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 3, 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., 51 Franklin Street, + * Boston, MA 02110-1301, USA. + */ + +#ifndef INCLUDED_STREAM_MUX_IMPL_H +#define INCLUDED_STREAM_MUX_IMPL_H + +#include <blocks/stream_mux.h> + +namespace gr { + namespace blocks { + + class BLOCKS_API stream_mux_impl : public stream_mux + { + private: + size_t d_itemsize; + unsigned int d_stream; // index of currently selected stream + int d_residual; // number if items left to put into current stream + gr_vector_int d_lengths; // number if items to pack per stream + + void increment_stream(); + + void forecast(int noutput_items, gr_vector_int &ninput_items_required); + + public: + stream_mux_impl(size_t itemsize, const std::vector<int> &lengths); + + int general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace blocks */ +} /* namespace gr */ + +#endif /* INCLUDED_STREAM_MUX_IMPL_H */ |