diff options
author | Johnathan Corgan <johnathan@corganlabs.com> | 2014-07-21 14:38:14 -0700 |
---|---|---|
committer | Johnathan Corgan <johnathan@corganlabs.com> | 2014-07-21 14:38:14 -0700 |
commit | 2ece28b3fea5b27baa6a6858e7645467d75cbd11 (patch) | |
tree | b551a463d785aa4346442cd45ea7aaca73feaaa6 /gr-dtv/lib/atsc | |
parent | dbbc0687b7323c86eb2b1c2d7524ff212bbd64d8 (diff) |
dtv: move ATSC blocks into lib/atsc
Diffstat (limited to 'gr-dtv/lib/atsc')
30 files changed, 3036 insertions, 0 deletions
diff --git a/gr-dtv/lib/atsc/atsc_deinterleaver_impl.cc b/gr-dtv/lib/atsc/atsc_deinterleaver_impl.cc new file mode 100644 index 0000000000..7b2001aacb --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_deinterleaver_impl.cc @@ -0,0 +1,97 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_deinterleaver_impl.h" +#include "gnuradio/dtv/atsc_consts.h" +#include <gnuradio/io_signature.h> + +namespace gr { + namespace dtv { + + atsc_deinterleaver::sptr + atsc_deinterleaver::make() + { + return gnuradio::get_initial_sptr + (new atsc_deinterleaver_impl()); + } + + atsc_deinterleaver_impl::atsc_deinterleaver_impl() + : gr::sync_block("atsc_deinterleaver", + io_signature::make(1, 1, sizeof(atsc_mpeg_packet_rs_encoded)), + io_signature::make(1, 1, sizeof(atsc_mpeg_packet_rs_encoded))), + alignment_fifo (156) + { + m_fifo.resize(52); + + for (int i = 0; i < 52; i++) + m_fifo[52 - 1 - i] = new interleaver_fifo<unsigned char>(i * 4); + + sync(); + } + + atsc_deinterleaver_impl::~atsc_deinterleaver_impl() + { + for (int i = 0; i < 52; i++) + delete m_fifo[i]; + } + + void atsc_deinterleaver_impl::reset() + { + sync(); + for (int i = 0; i < 52; i++) + m_fifo[i]->reset(); + } + + int + atsc_deinterleaver_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]; + + for (int i = 0; i < noutput_items; i++) { + assert (in[i].pli.regular_seg_p()); + plinfo::sanity_check(in[i].pli); + + // reset commutator if required using INPUT pipeline info + if (in[i].pli.first_regular_seg_p()) + sync(); + + // remap OUTPUT pipeline info to reflect 52 data segment end-to-end delay + plinfo::delay (out[i].pli, in[i].pli, 52); + + // now do the actual deinterleaving + for (unsigned int j = 0; j < sizeof(in[i].data); j++) { + out[i].data[j] = alignment_fifo.stuff(transform (in[i].data[j])); + } + } + + return noutput_items; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_deinterleaver_impl.h b/gr-dtv/lib/atsc/atsc_deinterleaver_impl.h new file mode 100644 index 0000000000..75eb7e877d --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_deinterleaver_impl.h @@ -0,0 +1,75 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_DEINTERLEAVER_IMPL_H +#define INCLUDED_DTV_ATSC_DEINTERLEAVER_IMPL_H + +#include <gnuradio/dtv/atsc_deinterleaver.h> +#include "atsc_types.h" +#include "interleaver_fifo.h" + +namespace gr { + namespace dtv { + + class atsc_deinterleaver_impl : public atsc_deinterleaver + { + private: + //! transform a single symbol + unsigned char transform(unsigned char input) { + unsigned char retval = m_fifo[m_commutator]->stuff(input); + m_commutator++; + if (m_commutator >= 52) + m_commutator = 0; + return retval; + } + + /*! + * Note: The use of the alignment_fifo keeps the encoder and decoder + * aligned if both are synced to a field boundary. There may be other + * ways to implement this function. This is a best guess as to how + * this should behave, as we have no test vectors for either the + * interleaver or deinterleaver. + */ + interleaver_fifo<unsigned char> alignment_fifo; + + int m_commutator; + std::vector<interleaver_fifo<unsigned char> *> m_fifo; + + public: + atsc_deinterleaver_impl(); + ~atsc_deinterleaver_impl(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + + //! reset interleaver (flushes contents and resets commutator) + void reset(); + + //! sync interleaver (resets commutator, but doesn't flush fifos) + void sync() { m_commutator = 0; } + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_DEINTERLEAVER_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_depad_impl.cc b/gr-dtv/lib/atsc/atsc_depad_impl.cc new file mode 100644 index 0000000000..e1172e630c --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_depad_impl.cc @@ -0,0 +1,66 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_depad_impl.h" +#include "gnuradio/dtv/atsc_consts.h" +#include <gnuradio/io_signature.h> + +namespace gr { + namespace dtv { + + atsc_depad::sptr + atsc_depad::make() + { + return gnuradio::get_initial_sptr + (new atsc_depad_impl()); + } + + atsc_depad_impl::atsc_depad_impl() + : gr::sync_interpolator("atsc_depad", + io_signature::make(1, 1, sizeof(atsc_mpeg_packet)), + io_signature::make(1, 1, sizeof(unsigned char)), + ATSC_MPEG_PKT_LENGTH) + { + } + + int + atsc_depad_impl::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const atsc_mpeg_packet *in = (const atsc_mpeg_packet *)input_items[0]; + unsigned char *out = (unsigned char *)output_items[0]; + + int i; + + for (i = 0; i < noutput_items/ATSC_MPEG_PKT_LENGTH; i++) + memcpy(&out[i * ATSC_MPEG_PKT_LENGTH], in[i].data, ATSC_MPEG_PKT_LENGTH); + + return i * ATSC_MPEG_PKT_LENGTH; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_depad_impl.h b/gr-dtv/lib/atsc/atsc_depad_impl.h new file mode 100644 index 0000000000..d29b1cfe9e --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_depad_impl.h @@ -0,0 +1,45 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_DEPAD_IMPL_H +#define INCLUDED_DTV_ATSC_DEPAD_IMPL_H + +#include <gnuradio/dtv/atsc_depad.h> +#include "atsc_types.h" + +namespace gr { + namespace dtv { + + class atsc_depad_impl : public atsc_depad + { + public: + atsc_depad_impl(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_DEPAD_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_derandomizer_impl.cc b/gr-dtv/lib/atsc/atsc_derandomizer_impl.cc new file mode 100644 index 0000000000..364713f7b5 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_derandomizer_impl.cc @@ -0,0 +1,79 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_derandomizer_impl.h" +#include "gnuradio/dtv/atsc_consts.h" +#include <gnuradio/io_signature.h> + +namespace gr { + namespace dtv { + + atsc_derandomizer::sptr + atsc_derandomizer::make() + { + return gnuradio::get_initial_sptr + (new atsc_derandomizer_impl()); + } + + atsc_derandomizer_impl::atsc_derandomizer_impl() + : gr::sync_block("dtv_atsc_derandomizer", + io_signature::make(1, 1, sizeof(atsc_mpeg_packet_no_sync)), + io_signature::make(1, 1, sizeof(atsc_mpeg_packet))) + { + d_rand.reset(); + } + + int + atsc_derandomizer_impl::work (int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const atsc_mpeg_packet_no_sync *in = (const atsc_mpeg_packet_no_sync *) input_items[0]; + atsc_mpeg_packet *out = (atsc_mpeg_packet *) output_items[0]; + + for (int i = 0; i < noutput_items; i++){ + + assert(in[i].pli.regular_seg_p()); + + if (in[i].pli.first_regular_seg_p()) + d_rand.reset(); + + d_rand.derandomize(out[i], in[i]); + + // Check the pipeline info for error status and and set the + // corresponding bit in transport packet header. + + if (in[i].pli.transport_error_p()) + out[i].data[1] |= MPEG_TRANSPORT_ERROR_BIT; + else + out[i].data[1] &= ~MPEG_TRANSPORT_ERROR_BIT; + } + + return noutput_items; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_derandomizer_impl.h b/gr-dtv/lib/atsc/atsc_derandomizer_impl.h new file mode 100644 index 0000000000..a307a51d86 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_derandomizer_impl.h @@ -0,0 +1,48 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_DERANDOMIZER_IMPL_H +#define INCLUDED_DTV_ATSC_DERANDOMIZER_IMPL_H + +#include <gnuradio/dtv/atsc_derandomizer.h> +#include "atsc_randomize.h" + +namespace gr { + namespace dtv { + + class atsc_derandomizer_impl : public atsc_derandomizer + { + private: + atsc_randomize d_rand; + + public: + atsc_derandomizer_impl(); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_DERANDOMIZER_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_equalizer_impl.cc b/gr-dtv/lib/atsc/atsc_equalizer_impl.cc new file mode 100644 index 0000000000..c804be6dc7 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_equalizer_impl.cc @@ -0,0 +1,183 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_equalizer_impl.h" +#include "atsc_types.h" +#include "atsc_pnXXX_impl.h" +#include <gnuradio/io_signature.h> + +namespace gr { + namespace dtv { + + atsc_equalizer::sptr + atsc_equalizer::make() + { + return gnuradio::get_initial_sptr + (new atsc_equalizer_impl()); + } + + static float + bin_map (int bit) + { + return bit ? +5 : -5; + } + + static void + init_field_sync_common (float *p, int mask) + { + int i = 0; + + p[i++] = bin_map (1); // data segment sync pulse + p[i++] = bin_map (0); + p[i++] = bin_map (0); + p[i++] = bin_map (1); + + for (int j = 0; j < 511; j++) // PN511 + p[i++] = bin_map (atsc_pn511[j]); + + for (int j = 0; j < 63; j++) // PN63 + p[i++] = bin_map (atsc_pn63[j]); + + for (int j = 0; j < 63; j++) // PN63, toggled on field 2 + p[i++] = bin_map (atsc_pn63[j] ^ mask); + + for (int j = 0; j < 63; j++) // PN63 + p[i++] = bin_map (atsc_pn63[j]); + } + + atsc_equalizer_impl::atsc_equalizer_impl() + : gr::block("dtv_atsc_equalizer", + io_signature::make(1, 1, sizeof(atsc_soft_data_segment)), + io_signature::make(1, 1, sizeof(atsc_soft_data_segment))) + { + init_field_sync_common(training_sequence1, 0); + init_field_sync_common(training_sequence2, 1); + + for (int i = 0; i < NTAPS; i++) + d_taps[i] = 0.0; + + d_buff_not_filled = true; + } + + atsc_equalizer_impl::~atsc_equalizer_impl() + { + } + + void + atsc_equalizer_impl::filterN(const float *input_samples, float *output_samples, int nsamples) + { + for (int j = 0; j < nsamples; j++) { + output_samples[j] = 0; + for(int i = 0; i < NTAPS; i++) + output_samples[j] += d_taps[i] * input_samples[j + i]; + } + } + + void + atsc_equalizer_impl::adaptN(const float *input_samples, + const float *training_pattern, + float *output_samples, + int nsamples) + { + static const double BETA = 0.00005; // FIXME figure out what this ought to be + // FIXME add gear-shifting + + for(int j = 0; j < nsamples; j++) { + output_samples[j] = 0; + for( int i = 0; i < NTAPS; i++ ) + output_samples[j] += d_taps[i] * input_samples[j + i]; + + double e = output_samples[j] - training_pattern[j]; + + // update taps... + for( int i = 0; i < NTAPS; i++ ) + d_taps[i] -= BETA * e * (double)(input_samples[j + i]); + } + } + + int + atsc_equalizer_impl::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const atsc_soft_data_segment *in = (const atsc_soft_data_segment *) input_items[0]; + atsc_soft_data_segment *out = (atsc_soft_data_segment *) output_items[0]; + + int output_produced = 0; + int i = 0; + + if(d_buff_not_filled) { + for(int j = 0; j < ATSC_DATA_SEGMENT_LENGTH; j++) + data_mem[NPRETAPS + j] = in[i].data[j]; + d_flags = in[i].pli._flags; + d_segno = in[i].pli._segno; + d_buff_not_filled = false; + i++; + } + + for (; i < noutput_items; i++) { + + for(int j = 0; j < NTAPS - NPRETAPS; j++) + data_mem[ATSC_DATA_SEGMENT_LENGTH + NPRETAPS + j] = in[i].data[j]; + + if(d_segno == -1) { + if(d_flags & 0x0010) { + adaptN(data_mem, training_sequence2, data_mem2, KNOWN_FIELD_SYNC_LENGTH); + //filterN(&data_mem[KNOWN_FIELD_SYNC_LENGTH], data_mem2, ATSC_DATA_SEGMENT_LENGTH - KNOWN_FIELD_SYNC_LENGTH); + } + else if( !(d_flags & 0x0010) ) { + adaptN( data_mem, training_sequence1, data_mem2, KNOWN_FIELD_SYNC_LENGTH ); + //filterN(&data_mem[KNOWN_FIELD_SYNC_LENGTH], data_mem2, ATSC_DATA_SEGMENT_LENGTH - KNOWN_FIELD_SYNC_LENGTH); + } + } + else { + filterN(data_mem, data_mem2, ATSC_DATA_SEGMENT_LENGTH); + + for(int j = 0; j < ATSC_DATA_SEGMENT_LENGTH; j++) + out[output_produced].data[j] = data_mem2[j]; + + out[output_produced].pli._flags = d_flags; + out[output_produced].pli._segno = d_segno; + output_produced++; + } + + for( int j = 0; j < NPRETAPS; j++ ) + data_mem[j] = data_mem[ATSC_DATA_SEGMENT_LENGTH + j]; + + for(int j = 0; j < ATSC_DATA_SEGMENT_LENGTH; j++) + data_mem[NPRETAPS + j] = in[i].data[j]; + + d_flags = in[i].pli._flags; + d_segno = in[i].pli._segno; + } + + consume_each(noutput_items); + return output_produced; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_equalizer_impl.h b/gr-dtv/lib/atsc/atsc_equalizer_impl.h new file mode 100644 index 0000000000..6ff89ca646 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_equalizer_impl.h @@ -0,0 +1,71 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_EQUALIZER_IMPL_H +#define INCLUDED_DTV_ATSC_EQUALIZER_IMPL_H + +#include <gnuradio/dtv/atsc_equalizer.h> +#include <gnuradio/dtv/atsc_consts.h> +#include "atsc_syminfo_impl.h" + +namespace gr { + namespace dtv { + + class atsc_equalizer_impl : public atsc_equalizer + { + private: + static const int NTAPS = 64; + static const int NPRETAPS = (int) (NTAPS * 0.8); // probably should be either .2 or .8 + + // the length of the field sync pattern that we know unequivocally + static const int KNOWN_FIELD_SYNC_LENGTH = 4 + 511 + 3 * 63; + + float training_sequence1[KNOWN_FIELD_SYNC_LENGTH]; + float training_sequence2[KNOWN_FIELD_SYNC_LENGTH]; + + void filterN(const float *input_samples, float *output_samples, int nsamples); + void adaptN(const float *input_samples, const float *training_pattern, + float *output_samples, int nsamples); + + float d_taps[NTAPS]; + + float data_mem[ATSC_DATA_SEGMENT_LENGTH + NTAPS]; // Buffer for previous data packet + float data_mem2[ATSC_DATA_SEGMENT_LENGTH]; + unsigned short d_flags; + short d_segno; + + int d_buff_not_filled; + + public: + atsc_equalizer_impl(); + ~atsc_equalizer_impl(); + + virtual 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 dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_EQUALIZER_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_fake_single_viterbi.cc b/gr-dtv/lib/atsc/atsc_fake_single_viterbi.cc new file mode 100644 index 0000000000..ffe410292e --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_fake_single_viterbi.cc @@ -0,0 +1,72 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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. + */ + +#include "atsc_fake_single_viterbi.h" + +namespace gr { + namespace dtv { + + void + atsc_fake_single_viterbi::reset() + { + post_coder_state = 0; + } + + atsc_fake_single_viterbi::atsc_fake_single_viterbi() + { + reset(); + } + + /* + * implement simple slicer and post coder + */ + char + atsc_fake_single_viterbi::decode(float input) + { + int y2, y1; + + if (input < -4){ + y2 = 0; + y1 = 0; + } + else if (input < 0){ + y2 = 0; + y1 = 1; + } + else if (input < 4){ + y2 = 1; + y1 = 0; + } + else { + y2 = 1; + y1 = 1; + } + + int x1 = y1; + int x2 = y2 ^ post_coder_state; + post_coder_state = y2; + + return (x2 << 1) | x1; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_fake_single_viterbi.h b/gr-dtv/lib/atsc/atsc_fake_single_viterbi.h new file mode 100644 index 0000000000..9ca546a6ab --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_fake_single_viterbi.h @@ -0,0 +1,52 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_ATSC_FAKE_SINGLE_VITERBI_H +#define INCLUDED_ATSC_FAKE_SINGLE_VITERBI_H + +namespace gr { + namespace dtv { + + class atsc_fake_single_viterbi + { + public: + atsc_fake_single_viterbi(); + + /*! + * \p INPUT ideally takes on the values +/- 1,3,5,7 + * return is decoded dibit in the range [0, 3] + */ + char decode (float input); + + void reset(); + + //! internal delay of decoder + int delay() { return 0; } + + protected: + int post_coder_state; + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_ATSC_FAKE_SINGLE_VITERBI_H */ diff --git a/gr-dtv/lib/atsc/atsc_fpll_impl.cc b/gr-dtv/lib/atsc/atsc_fpll_impl.cc new file mode 100644 index 0000000000..b211f80d6e --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_fpll_impl.cc @@ -0,0 +1,95 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_fpll_impl.h" +#include <gnuradio/io_signature.h> +#include <gnuradio/math.h> +#include <gnuradio/sincos.h> + +namespace gr { + namespace dtv { + + atsc_fpll::sptr + atsc_fpll::make(float rate) + { + return gnuradio::get_initial_sptr + (new atsc_fpll_impl(rate)); + } + + atsc_fpll_impl::atsc_fpll_impl(float rate) + : sync_block("dtv_atsc_fpll", + io_signature::make(1, 1, sizeof(gr_complex)), + io_signature::make(1, 1, sizeof(float))) + { + d_afc.set_taps(1.0-exp(-1.0/rate/5e-6)); + d_nco.set_freq((-3e6 + 0.309e6)/rate*2*M_PI); + d_nco.set_phase(0.0); + } + + atsc_fpll_impl::~atsc_fpll_impl() + { + } + + int + atsc_fpll_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const gr_complex *in = (const gr_complex *)input_items[0]; + float *out = (float *)output_items[0]; + + for (int k = 0; k < noutput_items; k++) { + float a_cos, a_sin; + + d_nco.step(); // increment phase + d_nco.sincos(&a_sin, &a_cos); // compute cos and sin + + // Mix out carrier and output I-only signal + gr_complex result = in[k]*gr_complex(a_sin, a_cos); + out[k] = result.real(); + + // Update phase/freq error + gr_complex filtered = d_afc.filter(result); + float x = gr::fast_atan2f(filtered.imag(), filtered.real()); + + // avoid slamming filter with big transitions + static const float limit = M_PI/2.0; + if (x > limit) + x = limit; + else if (x < -limit) + x = -limit; + + static const float alpha = 0.0002; + static const float beta = alpha*alpha/4.0; + d_nco.adjust_phase(alpha*x); + d_nco.adjust_freq(beta*x); + } + + return noutput_items; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_fpll_impl.h b/gr-dtv/lib/atsc/atsc_fpll_impl.h new file mode 100644 index 0000000000..e06fc09cc5 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_fpll_impl.h @@ -0,0 +1,53 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_FPLL_IMPL_H +#define INCLUDED_DTV_ATSC_FPLL_IMPL_H + +#include <gnuradio/dtv/atsc_fpll.h> +#include <gnuradio/nco.h> +#include <gnuradio/filter/single_pole_iir.h> +#include <gnuradio/analog/agc.h> +#include <stdio.h> + +namespace gr { + namespace dtv { + + class atsc_fpll_impl : public atsc_fpll + { + private: + gr::nco<float,float> d_nco; + gr::filter::single_pole_iir<gr_complex,gr_complex,float> d_afc; + + public: + atsc_fpll_impl(float rate); + ~atsc_fpll_impl(); + + virtual int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_FPLL_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_fs_checker_impl.cc b/gr-dtv/lib/atsc/atsc_fs_checker_impl.cc new file mode 100644 index 0000000000..3a39bc794b --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_fs_checker_impl.cc @@ -0,0 +1,128 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_fs_checker_impl.h" +#include "atsc_types.h" +#include "atsc_syminfo_impl.h" +#include "atsc_pnXXX_impl.h" +#include "gnuradio/dtv/atsc_consts.h" +#include <gnuradio/io_signature.h> + +static const int PN511_ERROR_LIMIT = 20; // max number of bits wrong +static const int PN63_ERROR_LIMIT = 5; + +namespace gr { + namespace dtv { + + atsc_fs_checker::sptr + atsc_fs_checker::make() + { + return gnuradio::get_initial_sptr + (new atsc_fs_checker_impl()); + } + + atsc_fs_checker_impl::atsc_fs_checker_impl() + : gr::block("dtv_atsc_fs_checker", + io_signature::make(1, 1, sizeof(atsc_soft_data_segment)), + io_signature::make(1, 1, sizeof(atsc_soft_data_segment))) + { + reset(); + } + + void + atsc_fs_checker_impl::reset() + { + d_index = 0; + memset (d_sample_sr, 0, sizeof (d_sample_sr)); + memset (d_tag_sr, 0, sizeof (d_tag_sr)); + memset (d_bit_sr, 0, sizeof (d_bit_sr)); + d_field_num = 0; + d_segment_num = 0; + } + + atsc_fs_checker_impl::~atsc_fs_checker_impl() + { + } + + int + atsc_fs_checker_impl::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const atsc_soft_data_segment *in = (const atsc_soft_data_segment *) input_items[0]; + atsc_soft_data_segment *out = (atsc_soft_data_segment *) output_items[0]; + + int output_produced = 0; + + for (int i = 0; i < noutput_items; i++) { + // check for a hit on the PN 511 pattern + int errors = 0; + + for (int j = 0; j < LENGTH_511 && errors < PN511_ERROR_LIMIT; j++) + errors += (in[i].data[j + OFFSET_511] >= 0) ^ atsc_pn511[j]; + + //std::cout << errors << std::endl; + + if (errors < PN511_ERROR_LIMIT) { // 511 pattern is good. + // determine if this is field 1 or field 2 + errors = 0; + for (int j = 0; j < LENGTH_2ND_63; j++) + errors += (in[i].data[j + OFFSET_2ND_63] >= 0) ^ atsc_pn63[j]; + + // we should have either field 1 (== PN63) or field 2 (== ~PN63) + if (errors <= PN63_ERROR_LIMIT) { + //std::cout << "Found FIELD_SYNC_1" << std::endl; + d_field_num = 1; // We are in field number 1 now + d_segment_num = -1; // This is the first segment + } + else if (errors >= (LENGTH_2ND_63 - PN63_ERROR_LIMIT)) { + //std::cout << "Found FIELD_SYNC_2" << std::endl; + d_field_num = 2; // We are in field number 2 now + d_segment_num = -1; // This is the first segment + } + else { + // should be extremely rare. + std::cerr << "!!! atsc_fs_checker: PN63 error count = " << errors << std::endl; + } + } + + if( d_field_num == 1 || d_field_num == 2 ) { // If we have sync + // So we copy out current packet data to an output packet and fill its plinfo + for( int j = 0; j < ATSC_DATA_SEGMENT_LENGTH; j++ ) + out[output_produced].data[j] = in[i].data[j]; + out[output_produced].pli.set_regular_seg((d_field_num == 2), d_segment_num); + d_segment_num++; + output_produced++; + } + } + + consume_each(noutput_items); + return output_produced; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_fs_checker_impl.h b/gr-dtv/lib/atsc/atsc_fs_checker_impl.h new file mode 100644 index 0000000000..1210ae5830 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_fs_checker_impl.h @@ -0,0 +1,67 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_FS_CHECKER_IMPL_H +#define INCLUDED_DTV_ATSC_FS_CHECKER_IMPL_H + +#include <gnuradio/dtv/atsc_fs_checker.h> +#include "atsc_syminfo_impl.h" + +namespace gr { + namespace dtv { + + class atsc_fs_checker_impl : public atsc_fs_checker + { + private: + static const int SRSIZE = 1024; // must be power of two + int d_index; // points at oldest sample + float d_sample_sr[SRSIZE]; // sample shift register + atsc::syminfo d_tag_sr[SRSIZE]; // tag shift register + unsigned char d_bit_sr[SRSIZE]; // binary decision shift register + int d_field_num; + int d_segment_num; + + static const int OFFSET_511 = 4; // offset to second PN 63 pattern + static const int LENGTH_511 = 511; // length of PN 63 pattern + static const int OFFSET_2ND_63 = 578; // offset to second PN 63 pattern + static const int LENGTH_2ND_63 = 63; // length of PN 63 pattern + + inline static int wrap (int index){ return index & (SRSIZE - 1); } + inline static int incr (int index){ return wrap (index + 1); } + inline static int decr (int index){ return wrap (index - 1); } + + public: + atsc_fs_checker_impl(); + ~atsc_fs_checker_impl(); + + void reset(); + + virtual 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 dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_FS_CHECKER_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_interleaver_fifo.h b/gr-dtv/lib/atsc/atsc_interleaver_fifo.h new file mode 100644 index 0000000000..0d40389c74 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_interleaver_fifo.h @@ -0,0 +1,86 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_INTERLEAVER_FIFO_H +#define INCLUDED_DTV_ATSC_INTERLEAVER_FIFO_H + +#include <string.h> + +namespace gr { + namespace dtv { + + template<class symbol_type> + class interleaver_fifo { + public: + + interleaver_fifo(unsigned int size); + ~interleaver_fifo(); + + //! reset interleaver (flushes contents and resets commutator) + void reset (); + + //! stuff a symbol into the fifo and return the oldest + symbol_type stuff(symbol_type input) { + if (m_size == 0) + return input; + + symbol_type retval = m_fifo[m_position]; + m_fifo[m_position] = input; + m_position++; + if (m_position >= m_size) + m_position = 0; + + return retval; + } + + protected: + unsigned int m_size; + unsigned int m_position; + symbol_type *m_fifo; + }; + + template<class symbol_type> + interleaver_fifo<symbol_type>::interleaver_fifo(unsigned int size) + { + m_size = size; + m_position = 0; + m_fifo = new symbol_type[size]; + memset (m_fifo, 0, m_size * sizeof(symbol_type)); + } + + template<class symbol_type> + interleaver_fifo<symbol_type>::~interleaver_fifo() + { + delete [] m_fifo; + } + + template<class symbol_type> void + interleaver_fifo<symbol_type>::reset() + { + m_position = 0; + memset (m_fifo, 0, m_size * sizeof (symbol_type)); + } + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_INTERLEAVER_FIFO_H */ diff --git a/gr-dtv/lib/atsc/atsc_pnXXX_impl.h b/gr-dtv/lib/atsc/atsc_pnXXX_impl.h new file mode 100644 index 0000000000..d4b86dcdbb --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_pnXXX_impl.h @@ -0,0 +1,59 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2013 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_DTV_ATSC_PNXXX_H +#define INCLUDED_DTV_ATSC_PNXXX_H + +#include <gnuradio/dtv/api.h> + +const unsigned char atsc_pn511[511] = { + 0,0,0,0, 0,0,0,1, 0,1,1,1, 1,1,1,1, 1,1,0,0, 1,0,1,0, 1,0,1,0, 1,1,1,0, + 0,1,1,0, 0,1,1,0, 1,0,0,0, 1,0,0,0, 1,0,0,1, 1,1,1,0, 0,0,0,1, 1,1,0,1, + + 0,1,1,1, 1,1,0,1, 0,0,1,1, 0,1,0,1, 0,0,1,1, 1,0,1,1, 0,0,1,1, 1,0,1,0, + 0,1,0,0, 0,1,0,1, 1,0,0,0, 1,1,1,1, 0,0,1,0, 0,0,0,1, 0,1,0,0, 0,1,1,1, + + 1,1,0,0, 1,1,1,1, 0,1,0,1, 0,0,0,1, 0,1,0,0, 1,1,0,0, 0,0,1,1, 0,0,0,1, + 0,0,0,0, 0,1,0,0, 0,0,1,1, 1,1,1,1, 0,0,0,0, 0,1,0,1, 0,1,0,0, 0,0,0,0, + + 1,1,0,0, 1,1,1,1, 1,1,1,0, 1,1,1,0, 1,0,1,0, 1,0,0,1, 0,1,1,0, 0,1,1,0, + 0,0,1,1, 0,1,1,1, 0,1,1,1, 1,0,1,1, 0,1,0,0, 1,0,1,0, 0,1,0,0, 1,1,1,0, + + 0,1,1,1, 0,0,0,1, 0,1,1,1, 0,1,0,0, 0,0,1,1, 0,1,0,0, 1,1,1,1, 1,0,1,1, + 0,0,0,1, 0,1,0,1, 1,0,1,1, 1,1,0,0, 1,1,0,1, 1,0,1,0, 1,1,1,0, 1,1,0,1, + + 1,0,0,1, 0,1,1,0, 1,1,0,1, 1,1,0,0, 1,0,0,1, 0,0,1,0, 1,1,1,0, 0,0,1,1, + 1,0,0,1, 0,1,1,1, 1,0,1,0, 0,0,1,1, 0,1,0,1, 1,0,0,0, 0,1,0,0, 1,1,0,1, + + 1,1,1,1, 0,0,0,1, 0,0,1,0, 1,0,1,1, 1,1,0,0, 0,1,1,0, 0,1,0,1, 0,0,0,0, + 1,0,0,0, 1,1,0,0, 0,0,0,1, 1,1,1,0, 1,1,1,1, 1,1,0,1, 0,1,1,0, 1,0,1,0, + + 1,1,0,0, 1,0,0,1, 1,0,0,1, 0,0,0,1, 1,1,0,1, 1,1,0,0, 0,0,1,0, 1,1,0,1, + 0,0,0,0, 0,1,1,0, 1,1,0,0, 0,0,0,0, 1,0,0,1, 0,0,0,0, 0,0,0,1, 1,1,0 +}; + +const unsigned char atsc_pn63[63] = { + 1,1,1,0, 0,1,0,0, 1,0,1,1, 0,1,1,1, 0,1,1,0, 0,1,1,0, 1,0,1,0, 1,1,1,1, + 1,1,0,0, 0,0,0,1, 0,0,0,0, 1,1,0,0, 0,1,0,1, 0,0,1,1, 1,1,0,1, 0,0,0 +}; + +#endif /* INCLUDED_DTV_ATSC_PNXXX_H */ diff --git a/gr-dtv/lib/atsc/atsc_randomize.cc b/gr-dtv/lib/atsc/atsc_randomize.cc new file mode 100644 index 0000000000..769b08587a --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_randomize.cc @@ -0,0 +1,117 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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. + */ + +#include "atsc_randomize.h" + +namespace gr { + namespace dtv { + + unsigned char atsc_randomize::s_output_map[1 << 14]; + bool atsc_randomize::s_output_map_initialized_p = false; + + atsc_randomize::atsc_randomize () + { + d_state = PRELOAD_VALUE; + + if (!s_output_map_initialized_p) + initialize_output_map (); + } + + /*! + * \brief Generate the table used in the fast_output_map function. + * + * The table has 16K byte entries, but because of how is is used, only + * 256 entries end up being resident in the cache. This seems + * like a good use of memory. We can get away with a 16K table + * because the low two bits of the state do not affect the output + * function. By shifting right those two bits we shrink the table, + * and also get better cache line utilization. + */ + void + atsc_randomize::initialize_output_map () + { + s_output_map_initialized_p = true; + + for (int i = 0; i < (1 << 14); i++) + s_output_map[i] = slow_output_map (i << 2); + } + + + void + atsc_randomize::reset () + { + d_state = PRELOAD_VALUE; + } + + void + atsc_randomize::randomize (atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet &in) + { + assert (in.data[0] == MPEG_SYNC_BYTE); // confirm it's there, then drop + + for (int i = 0; i < ATSC_MPEG_DATA_LENGTH; i++) + out.data[i] = in.data[i + 1] ^ output_and_clk (); + } + + void + atsc_randomize::derandomize (atsc_mpeg_packet &out, const atsc_mpeg_packet_no_sync &in) + { + out.data[0] = MPEG_SYNC_BYTE; // add sync byte to beginning of packet + + for (int i = 0; i < ATSC_MPEG_DATA_LENGTH; i++) + out.data[i + 1] = in.data[i] ^ output_and_clk (); + } + + + unsigned char + atsc_randomize::slow_output_map (int st) + { + int output = 0; + + if (st & 0x8000) + output |= 0x01; + + if (st & 0x2000) + output |= 0x02; + + if (st & 0x1000) + output |= 0x04; + + if (st & 0x0200) + output |= 0x08; + + if (st & 0x0020) + output |= 0x10; + + if (st & 0x0010) + output |= 0x20; + + if (st & 0x0008) + output |= 0x40; + + if (st & 0x0004) + output |= 0x80; + + return output; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_randomize.h b/gr-dtv/lib/atsc/atsc_randomize.h new file mode 100644 index 0000000000..0903240f48 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_randomize.h @@ -0,0 +1,92 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_ATSC_RANDOMIZE_H +#define INCLUDED_ATSC_RANDOMIZE_H + +#include "atsc_types.h" + +namespace gr { + namespace dtv { + + class atsc_randomize + { + public: + atsc_randomize(); + + /*! \brief reset randomizer LFSR + * + * must be called during the Data Segment Sync interval prior to the + * first data segment. I.e., the LFSR is reset prior to the first + * field of each VSB data frame. + */ + void reset (); + + //! randomize (whiten) mpeg packet and remove leading MPEG-2 sync byte + void randomize (atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet &in); + + //! derandomize (de-whiten) mpeg packet and add leading MPEG-2 sync byte + void derandomize (atsc_mpeg_packet &out, const atsc_mpeg_packet_no_sync &in); + + unsigned int state() const { return d_state; } + private: + static void initialize_output_map (); + static unsigned char slow_output_map (int st); + + static unsigned char fast_output_map (int st){ + return s_output_map[(st & 0xb23c) >> 2]; // Magic const with 8 bits set improves cache + // utilization. The bits correspond to the taps + // used in output calculation. Others may be + // safely ignored. + } + + //! return current output value + unsigned char output (){ + return fast_output_map (d_state); + } + + //! clock LFSR; advance to next state. + void clk (){ + if (d_state & 0x1) + d_state = ((d_state ^ MASK) >> 1) | 0x8000; + else + d_state = d_state >> 1; + } + + //! return current output value and advance to next state + unsigned char output_and_clk (){ + unsigned char r = output (); + clk (); + return r; + } + + unsigned int d_state; + + static const unsigned int PRELOAD_VALUE = 0x018f; /* 0xf180 bit reversed */ + static const unsigned int MASK = 0xa638; + static unsigned char s_output_map[1 << 14]; + static bool s_output_map_initialized_p; + }; + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_ATSC_RANDOMIZE_H */ diff --git a/gr-dtv/lib/atsc/atsc_rs_decoder_impl.cc b/gr-dtv/lib/atsc/atsc_rs_decoder_impl.cc new file mode 100644 index 0000000000..7fb6541872 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_rs_decoder_impl.cc @@ -0,0 +1,129 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_rs_decoder_impl.h" +#include "gnuradio/dtv/atsc_consts.h" +#include <gnuradio/io_signature.h> + +namespace gr { + namespace dtv { + + static const int rs_init_symsize = 8; + static const int rs_init_gfpoly = 0x11d; + static const int rs_init_fcr = 0; // first consecutive root + static const int rs_init_prim = 1; // primitive is 1 (alpha) + static const int rs_init_nroots = 20; + + static const int N = (1 << rs_init_symsize) - 1; // 255 + static const int K = N - rs_init_nroots; // 235 + + static const int amount_of_pad = N - ATSC_MPEG_RS_ENCODED_LENGTH; // 48 + + atsc_rs_decoder::sptr + atsc_rs_decoder::make() + { + return gnuradio::get_initial_sptr + (new atsc_rs_decoder_impl()); + } + + atsc_rs_decoder_impl::atsc_rs_decoder_impl() + : gr::sync_block("dtv_atsc_rs_decoder", + io_signature::make(1, 1, sizeof(atsc_mpeg_packet_rs_encoded)), + io_signature::make(1, 1, sizeof(atsc_mpeg_packet_no_sync))) + { + d_rs = init_rs_char(rs_init_symsize, rs_init_gfpoly, rs_init_fcr, rs_init_prim, rs_init_nroots); + assert (d_rs != 0); + nerrors_corrrected_count = 0; + bad_packet_count = 0; + total_packets = 0; + } + + int atsc_rs_decoder_impl::decode (atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet_rs_encoded &in) + { + unsigned char tmp[N]; + int ncorrections; + + assert((int)(amount_of_pad + sizeof(in.data)) == N); + + // add missing prefix zero padding to message + memset(tmp, 0, amount_of_pad); + memcpy(&tmp[amount_of_pad], in.data, sizeof(in.data)); + + // correct message... + ncorrections = decode_rs_char(d_rs, tmp, 0, 0); + + // copy corrected message to output, skipping prefix zero padding + memcpy (out.data, &tmp[amount_of_pad], sizeof (out.data)); + + return ncorrections; + } + + atsc_rs_decoder_impl::~atsc_rs_decoder_impl() + { + if (d_rs) + free_rs_char (d_rs); + d_rs = 0; + } + + int + atsc_rs_decoder_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_no_sync *out = (atsc_mpeg_packet_no_sync *)output_items[0]; + + for (int i = 0; i < noutput_items; i++) { + assert(in[i].pli.regular_seg_p()); + out[i].pli = in[i].pli; // copy pipeline info... + + int nerrors_corrrected = decode(out[i], in[i]); + out[i].pli.set_transport_error(nerrors_corrrected == -1); + if (nerrors_corrrected == -1) + bad_packet_count++; + else + nerrors_corrrected_count += nerrors_corrrected; + + total_packets++; + if (total_packets > 1000) { + // FIXME: convert to logger + std::cout << "Error rate: " + << (float)nerrors_corrrected_count/total_packets + << "\tPacket error rate: " + << (float)bad_packet_count/total_packets + << std::endl; + + nerrors_corrrected_count = 0; + bad_packet_count = 0; + total_packets = 0; + } + } + + return noutput_items; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_rs_decoder_impl.h b/gr-dtv/lib/atsc/atsc_rs_decoder_impl.h new file mode 100644 index 0000000000..57460128dc --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_rs_decoder_impl.h @@ -0,0 +1,62 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_RS_DECODER_IMPL_H +#define INCLUDED_DTV_ATSC_RS_DECODER_IMPL_H + +#include <gnuradio/dtv/atsc_rs_decoder.h> +#include "atsc_types.h" + +extern "C" { +#include <gnuradio/fec/rs.h> +} + +namespace gr { + namespace dtv { + + class atsc_rs_decoder_impl : public atsc_rs_decoder + { + private: + int nerrors_corrrected_count; + int bad_packet_count; + int total_packets; + void *d_rs; + + public: + atsc_rs_decoder_impl(); + ~atsc_rs_decoder_impl(); + + /*! + * Decode RS encoded packet. + * \returns a count of corrected symbols, or -1 if the block was uncorrectible. + */ + int decode(atsc_mpeg_packet_no_sync &out, const atsc_mpeg_packet_rs_encoded &in); + + int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_RS_DECODER_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_single_viterbi.cc b/gr-dtv/lib/atsc/atsc_single_viterbi.cc new file mode 100644 index 0000000000..385940e453 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_single_viterbi.cc @@ -0,0 +1,123 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002, 2014 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. + */ + +#include "atsc_single_viterbi.h" +#include <math.h> + +namespace gr { + namespace dtv { + + /* was_sent is a table of what symbol we get given what bit pair + was sent and what state we where in [state][pair] */ + const int atsc_single_viterbi::was_sent[4][4] = { + {0,2,4,6}, + {0,2,4,6}, + {1,3,5,7}, + {1,3,5,7}, + }; + + /* transition_table is a table of what state we were in + given current state and bit pair sent [state][pair] */ + const int atsc_single_viterbi::transition_table[4][4] = { + {0,2,0,2}, + {2,0,2,0}, + {1,3,1,3}, + {3,1,3,1}, + }; + + void + atsc_single_viterbi::reset() + { + for (unsigned int i = 0; i<2; i++) + for (unsigned int j = 0; j<4; j++) { + path_metrics[i][j] = 0; + traceback[i][j] = 0; + } + post_coder_state = 0; + phase = 0; + } + + atsc_single_viterbi::atsc_single_viterbi() + { + reset(); + } + + char + atsc_single_viterbi::decode(float input) + { + unsigned int best_state = 0; + float best_state_metric = 100000; + + /* Precompute distances from input to each possible symbol */ + float distances[8] = { (float)fabs( input + 7 ), (float)fabs( input + 5 ), + (float)fabs( input + 3 ), (float)fabs( input + 1 ), + (float)fabs( input - 1 ), (float)fabs( input - 3 ), + (float)fabs( input - 5 ), (float)fabs( input - 7 ) }; + + /* We start by iterating over all possible states */ + for (unsigned int state = 0; state < 4; state++) { + /* Next we find the most probable path from the previous + states to the state we are testing, we only need to look at + the 4 paths that can be taken given the 2-bit input */ + int min_metric_symb = 0; + float min_metric = distances[was_sent[state][0]] + path_metrics[phase][transition_table[state][0]]; + for (unsigned int symbol_sent = 1; symbol_sent < 4; symbol_sent++) + if( (distances[was_sent[state][symbol_sent]] + path_metrics[phase][transition_table[state][symbol_sent]]) < min_metric) { + min_metric = distances[was_sent[state][symbol_sent]] + path_metrics[phase][transition_table[state][symbol_sent]]; + min_metric_symb = symbol_sent; + } + + path_metrics[phase^1][state] = min_metric; + traceback[phase^1][state] = (((unsigned long long)min_metric_symb) << 62) | (traceback[phase][transition_table[state][min_metric_symb]] >> 2); + + /* If this is the most probable state so far remember it, this + only needs to be checked when we are about to output a path + so this test can be saved till later if needed, if perfomed + later it could also be optimized with SIMD instructions. + Even better this check could be eliminated as we are + outputing the tail of our traceback not the head, for any + head state path will tend towards the optimal path with a + probability approaching 1 in just 8 or so transitions + */ + if(min_metric <= best_state_metric) { + best_state_metric = min_metric; + best_state = state; + } + } + + if(best_state_metric > 10000) { + for(unsigned int state = 0; state < 4; state++) + path_metrics[phase^1][state] -= best_state_metric; + } + phase ^= 1; + + int y2 = (0x2 & traceback[phase][best_state]) >> 1; + int x2 = y2 ^ post_coder_state; + post_coder_state = y2; + + return ( x2 << 1 ) | (0x1 & traceback[phase][best_state]); + } + + } /* namespace dtv */ +} /* namespace gr */ + + diff --git a/gr-dtv/lib/atsc/atsc_single_viterbi.h b/gr-dtv/lib/atsc/atsc_single_viterbi.h new file mode 100644 index 0000000000..3c756c7690 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_single_viterbi.h @@ -0,0 +1,60 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002, 2014 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_DTV_ATSC_SINGLE_VITERBI_H +#define INCLUDED_DTV_ATSC_SINGLE_VITERBI_H + +namespace gr { + namespace dtv { + + class atsc_single_viterbi + { + public: + atsc_single_viterbi(); + + static const unsigned int TB_LEN = 32; + + /*! + * \p INPUT ideally takes on the values +/- 1,3,5,7 + * return is decoded dibit in the range [0, 3] + */ + char decode(float input); + + void reset (); + + //! internal delay of decoder + static int delay () { return TB_LEN - 1; } + + protected: + static const int transition_table[4][4]; + static const int was_sent[4][4]; + + float path_metrics [2][4]; + unsigned long long traceback [2][4]; + unsigned char phase; + int post_coder_state; + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_SINGLE_VITERBI_H */ diff --git a/gr-dtv/lib/atsc/atsc_syminfo_impl.h b/gr-dtv/lib/atsc/atsc_syminfo_impl.h new file mode 100644 index 0000000000..4a5008e5f8 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_syminfo_impl.h @@ -0,0 +1,62 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_GR_DTV_ATSC_SYMINFO_H +#define INCLUDED_GR_DTV_ATSC_SYMINFO_H + +namespace gr { + namespace dtv { + namespace atsc { + + static const unsigned int SI_SEGMENT_NUM_MASK = 0x1ff; + static const unsigned int SI_FIELD_SYNC_SEGMENT_NUM = SI_SEGMENT_NUM_MASK; // conceptually -1 + + struct syminfo { + unsigned int symbol_num : 10; // 0..831 + unsigned int segment_num : 9; // 0..311 and SI_FIELD_SYNC_SEGMENT_NUM + unsigned int field_num : 1; // 0..1 + unsigned int valid : 1; // contents are valid + }; + + + static inline bool + tag_is_start_field_sync (syminfo tag) + { + return tag.symbol_num == 0 && tag.segment_num == SI_FIELD_SYNC_SEGMENT_NUM && tag.valid; + } + + static inline bool + tag_is_start_field_sync_1 (syminfo tag) + { + return tag_is_start_field_sync (tag) && tag.field_num == 0; + } + + static inline bool + tag_is_start_field_sync_2 (syminfo tag) + { + return tag_is_start_field_sync (tag) && tag.field_num == 1; + } + + } /* namespace atsc */ + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_GR_DTV_ATSC_SYMINFO_H */ diff --git a/gr-dtv/lib/atsc/atsc_sync_impl.cc b/gr-dtv/lib/atsc/atsc_sync_impl.cc new file mode 100644 index 0000000000..ec46d6b8d1 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_sync_impl.cc @@ -0,0 +1,196 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 "atsc_sync_impl.h" +#include "atsc_types.h" +#include <gnuradio/io_signature.h> + +namespace gr { + namespace dtv { + + atsc_sync::sptr + atsc_sync::make(float rate) + { + return gnuradio::get_initial_sptr + (new atsc_sync_impl(rate)); + } + + atsc_sync_impl::atsc_sync_impl(float rate) + : gr::block("dtv_atsc_sync", + io_signature::make(1, 1, sizeof(float)), + io_signature::make(1, 1, sizeof(atsc_soft_data_segment))), + d_next_input(0), d_rx_clock_to_symbol_freq(rate/ATSC_SYMBOL_RATE), + d_si(0) + { + d_loop.set_taps(LOOP_FILTER_TAP); + reset(); + } + + void + atsc_sync_impl::reset() + { + d_w = d_rx_clock_to_symbol_freq; + d_mu = 0.5; + + for (int i = 0; i < ATSC_DATA_SEGMENT_LENGTH; i++) + sample_mem[i] = 0; + + d_timing_adjust = 0; + d_counter = 0; + d_symbol_index = 0; + d_seg_locked = false; + + d_sr = 0; + + for (int i = 0; i < ATSC_DATA_SEGMENT_LENGTH; i++) + d_integrator[i] = SSI_MIN; + } + + atsc_sync_impl::~atsc_sync_impl() + { + } + + void + atsc_sync_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] = static_cast<int>(noutput_items * d_rx_clock_to_symbol_freq * ATSC_DATA_SEGMENT_LENGTH) + 1500 - 1; + } + + int + atsc_sync_impl::general_work(int noutput_items, + gr_vector_int &ninput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const float *in = (const float *) input_items[0]; + atsc_soft_data_segment *soft_data_segment_out = (atsc_soft_data_segment *) output_items[0]; + + float interp_sample; + + // amount actually consumed + d_si = 0; + + for (output_produced = 0; output_produced < noutput_items && (d_si + (int)d_interp.ntaps()) < ninput_items[0];) { + // First we interpolate a sample from input to work with + interp_sample = d_interp.interpolate(&in[d_si], d_mu); + + // Apply our timing adjustment slowly over several samples + d_mu += ADJUSTMENT_GAIN * 1e3 * d_timing_adjust; + + double s = d_mu + d_w; + double float_incr = floor(s); + d_mu = s - float_incr; + d_incr = (int)float_incr; + + assert (d_incr >= 1 && d_incr <= 3); + d_si += d_incr; + + // Remember the sample at this count position + sample_mem[d_counter] = interp_sample; + + // Is the sample positive or negative? + int bit = (interp_sample < 0 ? 0 : 1); + + // Put the sign bit into our shift register + d_sr = ((bit & 1) << 3) | (d_sr >> 1); + + // When +,-,-,+ (0x9, 1001) samples show up we have likely found a segment + // sync, it is more likely the segment sync will show up at about the same + // spot every ATSC_DATA_SEGMENT_LENGTH samples so we add some weight + // to this spot every pass to prevent random +,-,-,+ symbols from + // confusing our synchronizer + d_integrator[d_counter] += ((d_sr == 0x9) ? +2 : -1); + if(d_integrator[d_counter] < SSI_MIN) d_integrator[d_counter] = SSI_MIN; + if(d_integrator[d_counter] > SSI_MAX) d_integrator[d_counter] = SSI_MAX; + + d_symbol_index++; + if( d_symbol_index >= ATSC_DATA_SEGMENT_LENGTH ) + d_symbol_index = 0; + + d_counter++; + if( d_counter >= ATSC_DATA_SEGMENT_LENGTH ) { // counter just wrapped... + int best_correlation_value = d_integrator[0]; + int best_correlation_index = 0; + + for(int i = 1; i < ATSC_DATA_SEGMENT_LENGTH; i++) + if (d_integrator[i] > best_correlation_value) { + best_correlation_value = d_integrator[i]; + best_correlation_index = i; + } + + d_seg_locked = best_correlation_value >= MIN_SEG_LOCK_CORRELATION_VALUE; + + // the coefficients are -1,-1,+1,+1 + //d_timing_adjust = sample_mem[best_correlation_index - 3] + + // sample_mem[best_correlation_index - 2] - + // sample_mem[best_correlation_index - 1] - + // sample_mem[best_correlation_index]; + + //printf( "d_timing_adjust = %f\n", d_timing_adjust ); + + int corr_count = best_correlation_index; + + d_timing_adjust = -sample_mem[corr_count--]; + if( corr_count < 0 ) corr_count = ATSC_DATA_SEGMENT_LENGTH - 1; + d_timing_adjust -= sample_mem[corr_count--]; + if( corr_count < 0 ) corr_count = ATSC_DATA_SEGMENT_LENGTH - 1; + d_timing_adjust += sample_mem[corr_count--]; + if( corr_count < 0 ) corr_count = ATSC_DATA_SEGMENT_LENGTH - 1; + d_timing_adjust += sample_mem[corr_count--]; + + d_symbol_index = SYMBOL_INDEX_OFFSET - 1 - best_correlation_index; + if (d_symbol_index < 0) + d_symbol_index += ATSC_DATA_SEGMENT_LENGTH; + + d_counter = 0; + } + + // If we are locked we can start filling and producing data packets + // Due to the way we lock the first data packet will almost always be + // half full, this is OK becouse the fs_checker will not let packets though + // untill a non-corrupted field packet is found + if( d_seg_locked ) { + data_mem[d_symbol_index] = interp_sample; + + if( d_symbol_index >= (ATSC_DATA_SEGMENT_LENGTH - 1) ) + { + for( int i = 0; i < ATSC_DATA_SEGMENT_LENGTH; i++ ) + soft_data_segment_out[output_produced].data[i] = data_mem[i]; + output_produced++; + } + } + } + + consume_each(d_si); + return output_produced; + + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_sync_impl.h b/gr-dtv/lib/atsc/atsc_sync_impl.h new file mode 100644 index 0000000000..eb0ad17286 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_sync_impl.h @@ -0,0 +1,83 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_SYNC_IMPL_H +#define INCLUDED_DTV_ATSC_SYNC_IMPL_H + +#include <gnuradio/dtv/atsc_consts.h> +#include <gnuradio/dtv/atsc_sync.h> +#include <gnuradio/filter/single_pole_iir.h> +#include <gnuradio/filter/mmse_fir_interpolator_ff.h> + +namespace gr { + namespace dtv { + + class atsc_sync_impl : public atsc_sync + { + private: + static const double LOOP_FILTER_TAP = 0.0005; // 0.0005 works + static const double ADJUSTMENT_GAIN = 1.0e-5 / (10 * ATSC_DATA_SEGMENT_LENGTH); + static const int SYMBOL_INDEX_OFFSET = 3; + static const int MIN_SEG_LOCK_CORRELATION_VALUE = 5; + static const int SSI_MIN = -16; + static const int SSI_MAX = 15; + + gr::filter::single_pole_iir<float,float,float> d_loop; // ``VCO'' loop filter + gr::filter::mmse_fir_interpolator_ff d_interp; + + unsigned long long d_next_input; + double d_rx_clock_to_symbol_freq; + int d_si; + double d_w; // ratio of PERIOD of Tx to Rx clocks + double d_mu; // fractional delay [0,1] + int d_incr; + + float sample_mem[ATSC_DATA_SEGMENT_LENGTH]; + float data_mem[ATSC_DATA_SEGMENT_LENGTH]; + + double d_timing_adjust; + int d_counter; // free running mod 832 counter + int d_symbol_index; + bool d_seg_locked; + int d_sr; // 4 bit shift register + signed char d_integrator[ATSC_DATA_SEGMENT_LENGTH]; + int output_produced; + + public: + atsc_sync_impl(float rate); + ~atsc_sync_impl(); + + void reset(); + + void forecast(int noutput_items, + gr_vector_int &ninput_items_required); + + virtual 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 dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_SYNC_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_types.h b/gr-dtv/lib/atsc/atsc_types.h new file mode 100644 index 0000000000..7c02d0adf2 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_types.h @@ -0,0 +1,269 @@ +/* -*- c++ -*- */ +/* + * Copyright 2001,2006 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 DTV_INCLUDED_ATSC_TYPES_H +#define DTV_INCLUDED_ATSC_TYPES_H + +#include <gnuradio/dtv/atsc_consts.h> +#include <cstring> +#include <cassert> + +namespace gr { + namespace dtv { + + /*! + * \brief pipeline info that flows with data + * + * Not all modules need all the info + */ + class plinfo { + public: + plinfo () : _flags (0), _segno (0) { } + + // accessors + + bool field_sync1_p () const { return (_flags & fl_field_sync1) != 0; } + bool field_sync2_p () const { return (_flags & fl_field_sync2) != 0; } + bool field_sync_p () const { return field_sync1_p () || field_sync2_p (); } + + bool regular_seg_p () const { return (_flags & fl_regular_seg) != 0; } + + bool in_field1_p () const { return (_flags & fl_field2) == 0; } + bool in_field2_p () const { return (_flags & fl_field2) != 0; } + + bool first_regular_seg_p () const { return (_flags & fl_first_regular_seg) != 0; } + + bool transport_error_p () const { return (_flags & fl_transport_error) != 0; } + + unsigned int segno () const { return _segno; } + unsigned int flags () const { return _flags; } + + // setters + + void set_field_sync1 () + { + _segno = 0; + _flags = fl_field_sync1; + } + + void set_field_sync2 () + { + _segno = 0; + _flags = fl_field_sync2 | fl_field2; + } + + void set_regular_seg (bool field2, int segno) + { + //assert (0 <= segno && segno < ATSC_DSEGS_PER_FIELD); + _segno = segno; + _flags = fl_regular_seg; + if (segno == 0) + _flags |= fl_first_regular_seg; + if (segno >= ATSC_DSEGS_PER_FIELD) + _flags |= fl_transport_error; + if (field2) + _flags |= fl_field2; + } + + void set_transport_error (bool error){ + if (error) + _flags |= fl_transport_error; + else + _flags &= ~fl_transport_error; + } + + // overload equality operator + bool operator== (const plinfo &other) const { + return (_flags == other._flags && _segno == other._segno); + } + + bool operator!= (const plinfo &other) const { + return !(_flags == other._flags && _segno == other._segno); + } + + /*! + * Set \p OUT such that it reflects a \p NSEGS_OF_DELAY + * pipeline delay from \p IN. + */ + static void delay (plinfo &out, const plinfo &in, int nsegs_of_delay) + { + assert (in.regular_seg_p ()); + assert (nsegs_of_delay >= 0); + + int s = in.segno (); + if (in.in_field2_p ()) + s += ATSC_DSEGS_PER_FIELD; + + s -= nsegs_of_delay; + if (s < 0) + s += 2 * ATSC_DSEGS_PER_FIELD; + + //assert (0 <= s && s < 2 * ATSC_DSEGS_PER_FIELD); + + if (s < ATSC_DSEGS_PER_FIELD) + out.set_regular_seg (false, s); // field 1 + else + out.set_regular_seg (true, s - ATSC_DSEGS_PER_FIELD); // field 2 + } + + /*! + * confirm that \p X is plausible + */ + static void sanity_check (const plinfo &in) + { + // basic sanity checks... + //assert (x.segno () >= 0); + //assert (x.segno () < (unsigned) ATSC_DSEGS_PER_FIELD); + //assert ((x.flags () & ~0x3f) == 0); + + //assert (x.regular_seg_p () ^ x.field_sync_p ()); + //assert ((x.segno () != 0) ^ x.first_regular_seg_p ()); + } + + unsigned short _flags; // bitmask + short _segno; // segment number [-1,311] -1 is the field sync segment + + protected: + // these three are mutually exclusive + // This is a regular data segment. + static const int fl_regular_seg = 0x0001; + // This is a field sync segment, for 1st half of a field. + static const int fl_field_sync1 = 0x0002; + // This is a field sync segment, for 2nd half of a field. + static const int fl_field_sync2 = 0x0004; + + // This bit is on ONLY when fl_regular_seg is set AND when this is + // the first regular data segment AFTER a field sync segment. This + // segment causes various processing modules to reset. + static const int fl_first_regular_seg = 0x0008; + + // which field are we in? + static const int fl_field2 = 0x0010; // else field 1 + + // This bit is set when Reed-Solomon decoding detects an error that it + // can't correct. Note that other error detection (e.g. Viterbi) do not + // set it, since Reed-Solomon will correct many of those. This bit is + // then copied into the final Transport Stream packet so that MPEG + // software can see that the 188-byte data segment has been corrupted. + static const int fl_transport_error = 0x0020; + }; + + + + + class atsc_mpeg_packet { + public: + static const int NPAD = 68; + unsigned char data[ATSC_MPEG_DATA_LENGTH + 1]; // first byte is sync + unsigned char _pad_[NPAD]; // pad to power of 2 (256) + + // overload equality operator + bool operator== (const atsc_mpeg_packet &other) const { + return std::memcmp (data, other.data, sizeof (data)) == 0; + }; + + bool operator!= (const atsc_mpeg_packet &other) const { + return !(std::memcmp (data, other.data, sizeof (data)) == 0); + }; + }; + + class atsc_mpeg_packet_no_sync { + public: + static const int NPAD = 65; + plinfo pli; + unsigned char data[ATSC_MPEG_DATA_LENGTH]; + unsigned char _pad_[NPAD]; // pad to power of 2 (256) + + // overload equality operator + bool operator== (const atsc_mpeg_packet_no_sync &other) const { + return std::memcmp (data, other.data, sizeof (data)) == 0; + } + + bool operator!= (const atsc_mpeg_packet_no_sync &other) const { + return !(std::memcmp (data, other.data, sizeof (data)) == 0); + } + }; + + class atsc_mpeg_packet_rs_encoded { + public: + static const int NPAD = 45; + plinfo pli; + unsigned char data[ATSC_MPEG_RS_ENCODED_LENGTH]; + unsigned char _pad_[NPAD]; // pad to power of 2 (256) + + // overload equality operator + bool operator== (const atsc_mpeg_packet_rs_encoded &other) const { + return std::memcmp (data, other.data, sizeof (data)) == 0; + } + + bool operator!= (const atsc_mpeg_packet_rs_encoded &other) const { + return !(std::memcmp (data, other.data, sizeof (data)) == 0); + } + }; + + + //! contains 832 3 bit symbols. The low 3 bits in the byte hold the symbol. + + class atsc_data_segment { + public: + static const int NPAD = 188; + plinfo pli; + unsigned char data[ATSC_DATA_SEGMENT_LENGTH]; + unsigned char _pad_[NPAD]; // pad to power of 2 (1024) + + // overload equality operator + bool operator== (const atsc_data_segment &other) const { + return std::memcmp (data, other.data, sizeof (data)) == 0; + } + + bool operator!= (const atsc_data_segment &other) const { + return !(std::memcmp (data, other.data, sizeof (data)) == 0); + } + }; + + /*! + * Contains 832 bipolar floating point symbols. + * Nominal values are +/- {1, 3, 5, 7}. + * This data type represents the input to the viterbi decoder. + */ + + class atsc_soft_data_segment { + public: + static const int NPAD = 764; + plinfo pli; + float data[ATSC_DATA_SEGMENT_LENGTH]; + unsigned char _pad_[NPAD]; // pad to power of 2 (4096) + + // overload equality operator + bool operator== (const atsc_data_segment &other) const { + return std::memcmp (data, other.data, sizeof (data)) == 0; + } + + bool operator!= (const atsc_data_segment &other) const { + return !(std::memcmp (data, other.data, sizeof (data)) == 0); + } + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* _ATSC_TYPES_H_ */ diff --git a/gr-dtv/lib/atsc/atsc_viterbi_decoder_impl.cc b/gr-dtv/lib/atsc/atsc_viterbi_decoder_impl.cc new file mode 100644 index 0000000000..8e9cbc02b7 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_viterbi_decoder_impl.cc @@ -0,0 +1,140 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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 <gnuradio/io_signature.h> +#include "atsc_viterbi_decoder_impl.h" +#include "atsc_types.h" +#include <atsc_viterbi_mux.cc> // machine generated +#include <emmintrin.h> + +namespace gr { + namespace dtv { + + atsc_viterbi_decoder::sptr + atsc_viterbi_decoder::make() + { + return gnuradio::get_initial_sptr + (new atsc_viterbi_decoder_impl()); + } + + atsc_viterbi_decoder_impl::atsc_viterbi_decoder_impl() + : sync_block("dtv_atsc_viterbi_decoder", + io_signature::make(1, 1, sizeof(atsc_soft_data_segment)), + io_signature::make(1, 1, sizeof(atsc_mpeg_packet_rs_encoded))) + { + set_output_multiple(NCODERS); + + /* + * These fifo's handle the alignment problem caused by the + * inherent decoding delay of the individual viterbi decoders. + * The net result is that this entire block has a pipeline latency + * of 12 complete segments. + * + * If anybody cares, it is possible to do it with less delay, but + * this approach is at least somewhat understandable... + */ + + // the -4 is for the 4 sync symbols + int fifo_size = ATSC_DATA_SEGMENT_LENGTH - 4 - viterbi[0].delay(); + for (int i = 0; i < NCODERS; i++) + fifo[i] = new fifo_t(fifo_size); + + reset(); + } + + atsc_viterbi_decoder_impl::~atsc_viterbi_decoder_impl() + { + for (int i = 0; i < NCODERS; i++) + delete fifo[i]; + } + + void + atsc_viterbi_decoder_impl::reset() + { + for (int i = 0; i < NCODERS; i++) + fifo[i]->reset(); + } + + int + atsc_viterbi_decoder_impl::work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items) + { + const atsc_soft_data_segment *in = (const atsc_soft_data_segment *) input_items[0]; + atsc_mpeg_packet_rs_encoded *out = (atsc_mpeg_packet_rs_encoded *) output_items[0]; + + // The way the fs_checker works ensures we start getting packets + // starting with a field sync, and out input multiple is set to + // 12, so we should always get a mod 12 numbered first packet + assert(noutput_items % NCODERS == 0); + + int dbwhere; + int dbindex; + int shift; + float symbols[NCODERS][enco_which_max]; + unsigned char dibits[NCODERS][enco_which_max]; + + unsigned char out_copy[OUTPUT_SIZE]; + + for (int i = 0; i < noutput_items; i += NCODERS) { + /* Build a continuous symbol buffer for each encoder */ + for (unsigned int encoder = 0; encoder < NCODERS; encoder++) + for (unsigned int k = 0; k < enco_which_max; k++) + symbols[encoder][k] = in[i + (enco_which_syms[encoder][k]/832)].data[enco_which_syms[encoder][k] % 832]; + + /* Now run each of the 12 Viterbi decoders over their subset of + the input symbols */ + for (unsigned int encoder = 0; encoder < NCODERS; encoder++) + for (unsigned int k = 0; k < enco_which_max; k++) + dibits[encoder][k] = viterbi[encoder].decode(symbols[encoder][k]); + + /* Move dibits into their location in the output buffer */ + for (unsigned int encoder = 0; encoder < NCODERS; encoder++) { + for (unsigned int k = 0; k < enco_which_max; k++) { + /* Store the dibit into the output data segment */ + dbwhere = enco_which_dibits[encoder][k]; + dbindex = dbwhere >> 3; + shift = dbwhere & 0x7; + out_copy[dbindex] = (out_copy[dbindex] & ~(0x03 << shift)) | (fifo[encoder]->stuff(dibits[encoder][k]) << shift); + } /* Symbols fed into one encoder */ + } /* Encoders */ + + // copy output from contiguous temp buffer into final output + for (int j = 0; j < NCODERS; j++) { + memcpy (&out[i + j].data[0], + &out_copy[j * OUTPUT_SIZE/NCODERS], + ATSC_MPEG_RS_ENCODED_LENGTH * sizeof(out_copy[0])); + + // adjust pipeline info to reflect 12 segment delay + plinfo::delay(out[i + j].pli, in[i + j].pli, NCODERS); + } + } + + return noutput_items; + } + + } /* namespace dtv */ +} /* namespace gr */ diff --git a/gr-dtv/lib/atsc/atsc_viterbi_decoder_impl.h b/gr-dtv/lib/atsc/atsc_viterbi_decoder_impl.h new file mode 100644 index 0000000000..b4fbbd1033 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_viterbi_decoder_impl.h @@ -0,0 +1,76 @@ +/* -*- c++ -*- */ +/* + * Copyright 2014 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_DTV_ATSC_VITERBI_DECODER_IMPL_H +#define INCLUDED_DTV_ATSC_VITERBI_DECODER_IMPL_H + +#include <gnuradio/dtv/atsc_viterbi_decoder.h> +#include <gnuradio/dtv/atsc_consts.h> +#include "atsc_syminfo_impl.h" +#include "atsc_interleaver_fifo.h" + +#define USE_SIMPLE_SLICER 0 +#define NCODERS 12 + +#if USE_SIMPLE_SLICER +#include "atsc_fake_single_viterbi.h" +#else +#include "atsc_single_viterbi.h" +#endif + +namespace gr { + namespace dtv { + +#if USE_SIMPLE_SLICER + typedef atsc_fake_single_viterbi single_viterbi_t; +#else + typedef atsc_single_viterbi single_viterbi_t; +#endif + + class atsc_viterbi_decoder_impl : public atsc_viterbi_decoder + { + private: + int last_start; + typedef interleaver_fifo<unsigned char> fifo_t; + + static const int SEGMENT_SIZE = ATSC_MPEG_RS_ENCODED_LENGTH; // 207 + static const int OUTPUT_SIZE = (SEGMENT_SIZE * 12); + static const int INPUT_SIZE = (ATSC_DATA_SEGMENT_LENGTH * 12); + + single_viterbi_t viterbi[NCODERS]; + fifo_t *fifo[NCODERS]; + + public: + atsc_viterbi_decoder_impl(); + ~atsc_viterbi_decoder_impl(); + + void reset(); + + virtual int work(int noutput_items, + gr_vector_const_void_star &input_items, + gr_vector_void_star &output_items); + }; + + } /* namespace dtv */ +} /* namespace gr */ + +#endif /* INCLUDED_DTV_ATSC_VITERBI_DECODER_IMPL_H */ diff --git a/gr-dtv/lib/atsc/atsc_viterbi_gen.cc b/gr-dtv/lib/atsc/atsc_viterbi_gen.cc new file mode 100644 index 0000000000..bff3c47736 --- /dev/null +++ b/gr-dtv/lib/atsc/atsc_viterbi_gen.cc @@ -0,0 +1,267 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002,2006 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. + */ + +#include <iostream> +#include <stdio.h> + +using std::cerr; + +/* + * Trellis-encode a whole pile of 12 data segments for ATSC. + * This also includes scrambling the data among twelve Trellis encoders. + * + * Input is twelve 207-byte blocks of raw data (Reed-Solomon output that's + * been scrambled up by interleaving with other blocks). + * + * Output is 12 x 208 x 4 bytes, each byte containing a 3-bit symbol. + * The first 4 bytes are the segment sync symbol. + * + Got the first version of Trellis encoder coded. Compiles, but I + didn't realize that each data segment contains an odd number of BITS! + The second data segment in a field starts by pulling bits out of the + middles of the bytes it's encoding. You actually have to read all the + entries in that table on page 59 AND 60 to get it. + + There's a 4-segment asymmetric pattern of bit accesses. + There's a 3-segment asymmetric pattern of muxing encoders. + + The result is there's a 12-segment pattern that repeats throughout + the encoding of a field. So this routine now encodes 12 segments at once. + + This encoding system was either designed by a complete idiot or by + a complete genius. It's highly complex when it could have been very + simple. Now the question is whether + this incredible complexity buys us anything subtle and important. + */ + +#define SEGMENT_SIZE 207 +#define INPUT_SIZE (SEGMENT_SIZE * 12) +#define DIBITS_PER_BYTE 4 +#define EXTRAS (4 * 12) /* FIXME, sync symbols and such */ +#define SYMBOLS_OUT ((INPUT_SIZE * DIBITS_PER_BYTE) + EXTRAS) +#define SEGOF(x) ( (x) / ((SEGMENT_SIZE+1) * DIBITS_PER_BYTE)) +#define SYMOF(x) (((x) % ((SEGMENT_SIZE+1) * DIBITS_PER_BYTE))-4) +#define ENCODERS 12 +#define ENCODER_SEG_BUMP 4 + + +/* Shift counts to bit numbers (high order, low order); 9x entries unused */ +static const int bit1[8] = {1, 99, 3, 98, 5, 97, 7, 96}; +static const int bit2[8] = {0, 99, 2, 98, 4, 97, 6, 96}; + +/* Detailed Debugging */ +int debug_dec = 0; + +/* + * Build indirect data structures to say which symbols go into which + * encoder, and then where the resulting dibits from the encoders go. + */ +int +build_decode_structures (char *fileout) +{ + int retval = 0; + int i; + int encoder; + int trellis_wheredata[ENCODERS]; + unsigned char *symp, *next_sym_seg; + unsigned char symbols[SYMBOLS_OUT]; + int chunk; + int shift; + int skip_encoder_bump; + int *enco_syms[ENCODERS]; + int *enco_dibits[ENCODERS]; + int j; + /* The data structures we'll build and then spit out... */ + int sync_symbol_indices[1000]; + int sync_symbol_indices_max; + int enco_which_syms[ENCODERS][INPUT_SIZE]; + int enco_which_dibits[ENCODERS][INPUT_SIZE]; + int enco_which_max; + #define BIT_PTR(int,shif) (((int) << 3) | ((shif) & 0x7)) + /* Running indices into them as we build 'em... */ + int *syncsyms = sync_symbol_indices; + + /* Start our running pointers at the start of our per-encoder subarrays */ + for (i = 0; i < ENCODERS; i++) { + enco_dibits[i] = enco_which_dibits[i]; + enco_syms[i] = enco_which_syms[i]; + } + + encoder = ENCODERS - ENCODER_SEG_BUMP; + skip_encoder_bump = 0; + symp = symbols; + next_sym_seg = symp; + + for (chunk = 0; + chunk < INPUT_SIZE; + chunk += ENCODERS) { + /* Associate data bytes with the Trellis encoders. + They get loaded or stored in an order that depends on where we are in the + segment sync progress (sigh). + GRR! When the chunk reload happens at the same time as the + segment boundary, we should bump the encoder NOW for the reload, + rather than LATER during the bitshift transition!!! */ + if (symp >= next_sym_seg) { + encoder = (encoder + ENCODER_SEG_BUMP) % ENCODERS; + skip_encoder_bump = 1; + } + + /* Remember where the data bytes are going to go, once we've + accumulated them from the 12 interleaved decoders */ + for (i = 0; i < ENCODERS; i++) { + trellis_wheredata[encoder] = chunk+i; + encoder++; + if (encoder >= ENCODERS) encoder = 0; + } + + for (shift = 6; shift >= 0; shift -= 2) { + + /* Segment boundaries happen to occur on some bitshift transitions. */ + if (symp >= next_sym_seg) { + /* Segment transition. Output a data segment sync symbol, and + mess with the trellis encoder mux. */ + *syncsyms++ = symp - symbols; + symp += 4; + next_sym_seg = symp + (SEGMENT_SIZE * DIBITS_PER_BYTE); + + if (!skip_encoder_bump) + encoder = (encoder + ENCODER_SEG_BUMP) % ENCODERS; + skip_encoder_bump = 0; + } + + /* Now run each of the 12 Trellis encoders to spit out 12 symbols. + Each encoder takes input from the same byte of the chunk, but the + outputs of the encoders come out in various orders. + NOPE -- this is false. The encoders take input from various + bytes of the chunk (which changes at segment sync time), AND + they also come out in various orders. You really do have to + keep separate track of: the datasegs bytes, the encoders, and + the symbol bytes -- because they're all moving with respect to + each other!!! */ + for (i = 0; i < ENCODERS; i++) { + if (debug_dec) + printf ("Seg %ld Symb %3ld Trell %2d Byte %6d Bits %d-%d = ", + (long) SEGOF(symp-symbols), (long) SYMOF(symp-symbols), + encoder, trellis_wheredata[encoder], + bit1[shift], bit2[shift]); + + /* Decoding: Grab symbol, run through decoder, slice dibit into + buffer. */ + /* This symbol goes into this encoder next */ + *(enco_syms[encoder]++) = symp - symbols; + symp++; + /* The next output from this encoder goes into these dibits */ + *(enco_dibits[encoder]++) = BIT_PTR(trellis_wheredata[encoder], shift); + + encoder++; if (encoder >= ENCODERS) encoder = 0; + } /* Encoders */ + } /* Bit shifts */ + +#if 0 + /* Now dump out the chunk of 12 data bytes that the twelve decoders have + accumulated. */ + unsigned char trellis_buffer[ENCODERS]; + unsigned char dibit; + unsigned char symbol; + int save_state; + for (i = 0; i < ENCODERS; i++) { + datasegs [trellis_wheredata[encoder]] = trellis_buffer[encoder]; + encoder++; + if (encoder >= ENCODERS) encoder = 0; + } /* Dumping encoder bytes */ +#endif + } /* Chunks */ + + /* Now print the resulting data structures in C++ */ + + if (!freopen(fileout, "w", stdout)) + return 2; + + printf ("/*\n\ + * atsc_viterbi_mux.cc\n\ + *\n\ + * Data structures for knowing which symbols are fed to which\n\ + * Viterbi decoders, and then where to put the resulting output dibits.\n\ + *\n\ + * Generated by 'atsc_viterbi_gen.cc'.\n\ + */\n\n"); + sync_symbol_indices_max = syncsyms - sync_symbol_indices; + printf ("const unsigned int sync_symbol_indices_max = %d;\n", + sync_symbol_indices_max); + printf ("const unsigned int sync_symbol_indices[%d] = {\n ", + sync_symbol_indices_max); + for (i = 0; i < sync_symbol_indices_max; i++) { + printf ("%d,%s", sync_symbol_indices[i], (7 == i%8)? "\n ": " "); + } + printf ("};\n\n"); + + enco_which_max = enco_dibits[0] - enco_which_dibits[0]; + for (i = 0; i < ENCODERS; i++) + if (enco_which_max != enco_dibits[i] - enco_which_dibits[i]) { + cerr << "Encoder " << i << " has different max_dibits " << + enco_dibits[i] - enco_which_dibits[i] << " than " << enco_which_max; + retval = 3; + } + + printf ("const unsigned int enco_which_max = %d;\n" , enco_which_max); + + printf ("const unsigned int enco_which_syms[%d][%d] = {\n", + ENCODERS, enco_which_max); + for (i = 0; i < ENCODERS; i++) { + printf (" /* %d */\n {", i); + for (j = 0; j < enco_which_max; j++) + printf ("%d,%s", enco_which_syms[i][j], (7 == j%8)? "\n ": " "); + printf ("},\n"); + } + printf ("};\n\n"); + + printf ("const unsigned int enco_which_dibits[%d][%d] = {\n", + ENCODERS, enco_which_max); + for (i = 0; i < ENCODERS; i++) { + printf (" /* %d */\n {", i); + for (j = 0; j < enco_which_max; j++) + printf ("%d,%s", enco_which_dibits[i][j], (7 == j%8)? "\n ": " "); + printf ("},\n"); + } + printf ("};\n\n"); + return retval; +} + +int +usage() +{ + cerr << "atsc_viterbi_gen: Usage:\n"; + cerr << " ./atsc_viterbi_gen -o atsc_viterbi_mux.cc\n"; + cerr << "That's all, folks!\n"; + return 1; +} + + +int +main(int argc, char **argv) +{ + if (argc != 3) return usage(); + if (argv[1][0] != '-' + || argv[1][1] != 'o' + || argv[1][2] != 0 ) return usage(); + return build_decode_structures(argv[2]); +} diff --git a/gr-dtv/lib/atsc/interleaver_fifo.h b/gr-dtv/lib/atsc/interleaver_fifo.h new file mode 100644 index 0000000000..c1e6e3a1f0 --- /dev/null +++ b/gr-dtv/lib/atsc/interleaver_fifo.h @@ -0,0 +1,84 @@ +/* -*- c++ -*- */ +/* + * Copyright 2002 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 _INTERLEAVER_FIFO_H_ +#define _INTERLEAVER_FIFO_H_ + +#include <string.h> + +/*! + * \brief template class for interleaver fifo + */ + +template<class symbol_type> +class interleaver_fifo { + public: + + interleaver_fifo (unsigned int size); + ~interleaver_fifo (); + + //! reset interleaver (flushes contents and resets commutator) + void reset (); + + //! stuff a symbol into the fifo and return the oldest + symbol_type stuff (symbol_type input){ + if (m_size == 0) + return input; + + symbol_type retval = m_fifo[m_position]; + m_fifo[m_position] = input; + m_position++; + if (m_position >= m_size) + m_position = 0; + + return retval; + } + +protected: + unsigned int m_size; + unsigned int m_position; + symbol_type *m_fifo; +}; + +template<class symbol_type> +interleaver_fifo<symbol_type>::interleaver_fifo (unsigned int size) +{ + m_size = size; + m_position = 0; + m_fifo = new symbol_type[size]; + memset (m_fifo, 0, m_size * sizeof (symbol_type)); +} + +template<class symbol_type> +interleaver_fifo<symbol_type>::~interleaver_fifo () +{ + delete [] m_fifo; +} + +template<class symbol_type> void +interleaver_fifo<symbol_type>::reset () +{ + m_position = 0; + memset (m_fifo, 0, m_size * sizeof (symbol_type)); +} + +#endif /* _INTERLEAVER_FIFO_H_ */ |