1 /* -*- c++ -*- */
2 /*
3  * Copyright 2012 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 "tagged_stream_mux_impl.h"
28 #include <gnuradio/io_signature.h>
29 
30 namespace gr {
31 namespace blocks {
32 
make(size_t itemsize,const std::string & lengthtagname,unsigned int tag_preserve_head_pos)33 tagged_stream_mux::sptr tagged_stream_mux::make(size_t itemsize,
34                                                 const std::string& lengthtagname,
35                                                 unsigned int tag_preserve_head_pos)
36 {
37     return gnuradio::get_initial_sptr(
38         new tagged_stream_mux_impl(itemsize, lengthtagname, tag_preserve_head_pos));
39 }
40 
tagged_stream_mux_impl(size_t itemsize,const std::string & lengthtagname,unsigned int tag_preserve_head_pos)41 tagged_stream_mux_impl::tagged_stream_mux_impl(size_t itemsize,
42                                                const std::string& lengthtagname,
43                                                unsigned int tag_preserve_head_pos)
44     : tagged_stream_block("tagged_stream_mux",
45                           io_signature::make(1, -1, itemsize),
46                           io_signature::make(1, 1, itemsize),
47                           lengthtagname),
48       d_itemsize(itemsize),
49       d_tag_preserve_head_pos(tag_preserve_head_pos)
50 {
51     set_tag_propagation_policy(TPP_DONT);
52 }
53 
~tagged_stream_mux_impl()54 tagged_stream_mux_impl::~tagged_stream_mux_impl() {}
55 
calculate_output_stream_length(const gr_vector_int & ninput_items)56 int tagged_stream_mux_impl::calculate_output_stream_length(
57     const gr_vector_int& ninput_items)
58 {
59     int nout = 0;
60     for (unsigned i = 0; i < ninput_items.size(); i++) {
61         nout += ninput_items[i];
62     }
63     return nout;
64 }
65 
work(int noutput_items,gr_vector_int & ninput_items,gr_vector_const_void_star & input_items,gr_vector_void_star & output_items)66 int tagged_stream_mux_impl::work(int noutput_items,
67                                  gr_vector_int& ninput_items,
68                                  gr_vector_const_void_star& input_items,
69                                  gr_vector_void_star& output_items)
70 {
71     unsigned char* out = (unsigned char*)output_items[0];
72     int n_produced = 0;
73 
74     set_relative_rate((uint64_t)ninput_items.size(), 1);
75 
76     for (unsigned int i = 0; i < input_items.size(); i++) {
77         const unsigned char* in = (const unsigned char*)input_items[i];
78 
79         std::vector<tag_t> tags;
80         get_tags_in_range(tags, i, nitems_read(i), nitems_read(i) + ninput_items[i]);
81         for (unsigned int j = 0; j < tags.size(); j++) {
82             uint64_t offset =
83                 tags[j].offset - nitems_read(i) + nitems_written(0) + n_produced;
84             if (i == d_tag_preserve_head_pos && tags[j].offset == nitems_read(i)) {
85                 offset -= n_produced;
86             }
87             add_item_tag(0, offset, tags[j].key, tags[j].value);
88         }
89         memcpy((void*)out, (const void*)in, ninput_items[i] * d_itemsize);
90         out += ninput_items[i] * d_itemsize;
91         n_produced += ninput_items[i];
92     }
93 
94     return n_produced;
95 }
96 
97 } /* namespace blocks */
98 } /* namespace gr */
99