summaryrefslogtreecommitdiff
path: root/gr-dtv/lib/atsc/atsc_interleaver_impl.cc
blob: e628fbf30d7d787bb57a7eebfe73bf6cc2b54c63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/* -*- c++ -*- */
/*
 * Copyright 2015 Free Software Foundation, Inc.
 *
 * This 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.
 *
 * This software 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 this software; 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 "atsc_interleaver_impl.h"
#include "gnuradio/dtv/atsc_consts.h"
#include <stdio.h>

namespace gr {
  namespace dtv {

    atsc_interleaver::sptr
    atsc_interleaver::make()
    {
      return gnuradio::get_initial_sptr
        (new atsc_interleaver_impl());
    }

    atsc_interleaver_impl::atsc_interleaver_impl()
      : gr::sync_block("atsc_interleaver",
                       gr::io_signature::make(1, 1, sizeof(atsc_mpeg_packet_rs_encoded)),
                       gr::io_signature::make(1, 1, sizeof(atsc_mpeg_packet_rs_encoded)))
    {
      I = 52;    /* ATSC interleaver */
      J = 4;
      registers = (unsigned char *) malloc(sizeof(unsigned char) * I * ((I - 1) * J));
      if (registers == NULL) {
        fprintf(stderr, "Out of memory.\n");
        exit(1);
      }

      pointers = (int *) malloc(sizeof(int) * I);
      if (pointers == NULL) {
        fprintf(stderr, "Out of memory.\n");
        exit(1);
      }

      memset(registers, 0, sizeof(unsigned char) * I * ((I - 1) * J));
      memset(pointers, 0, sizeof(int) * I);

      commutator = 0;
    }

    atsc_interleaver_impl::~atsc_interleaver_impl()
    {
      free(pointers);
      free(registers);
    }

    int
    atsc_interleaver_impl::work(int noutput_items,
                                gr_vector_const_void_star &input_items,
                                gr_vector_void_star &output_items)
    {
      const atsc_mpeg_packet_rs_encoded *in = (const atsc_mpeg_packet_rs_encoded *) input_items[0];
      atsc_mpeg_packet_rs_encoded *out = (atsc_mpeg_packet_rs_encoded *) output_items[0];
      int p, n = ATSC_MPEG_RS_ENCODED_LENGTH;

      for (int i = 0; i < noutput_items; i++) {
        assert(in[i].pli.regular_seg_p());
        plinfo::sanity_check(in[i].pli);

        out[i].pli = in[i].pli;                 // copy pipeline info
        if (in[i].pli.first_regular_seg_p()) {  // reset commutator if required
          commutator = 0;
        }
        for (int j = 0; j < n; j++) {
          if (commutator == 0) {
            out[i].data[j] = in[i].data[j];
          }
          else {
            p = pointers[commutator];

            out[i].data[j] = registers[commutator * (I - 1) * J + p];
            registers[commutator * (I - 1) * J + p] = in[i].data[j];

            pointers[commutator] = (p + 1) % (commutator * J);
          }

          commutator = (commutator + 1) % I;
        }
      }

      // Tell runtime system how many output items we produced.
      return noutput_items;
    }

  } /* namespace dtv */
} /* namespace gr */