aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPiotr Krysik <ptrkrysik@users.noreply.github.com>2015-05-20 14:21:52 +0200
committerPiotr Krysik <ptrkrysik@users.noreply.github.com>2015-05-20 14:21:52 +0200
commit07e577fee1bf496f7691ddf7cc90409c814742bd (patch)
treebae37ad4e2c96547cc176ff39bf6bd50f10c019e
parente55cba498f47e09fe97d44410c1faaa932a04001 (diff)
parent059bab9c0bf3a026c5b836931158587fa165dbf8 (diff)
Merge pull request #53 from romankh/master
TCH/F decoder implementation
-rw-r--r--grc/decoding/CMakeLists.txt3
-rw-r--r--grc/decoding/gsm_tch_f_decoder.xml37
-rw-r--r--grc/gsm_block_tree.xml1
-rw-r--r--include/grgsm/decoding/CMakeLists.txt3
-rw-r--r--include/grgsm/decoding/tch_f_decoder.h65
-rw-r--r--lib/CMakeLists.txt5
-rw-r--r--lib/decoding/BitVector.cpp513
-rw-r--r--lib/decoding/BitVector.h427
-rw-r--r--lib/decoding/GSM610Tables.cpp492
-rw-r--r--lib/decoding/GSM610Tables.h37
-rw-r--r--lib/decoding/GSM660Tables.cpp244
-rw-r--r--lib/decoding/GSM660Tables.h31
-rw-r--r--lib/decoding/GSM690Tables.cpp49
-rw-r--r--lib/decoding/GSM690Tables.h31
-rw-r--r--lib/decoding/Vector.h256
-rw-r--r--lib/decoding/VocoderFrame.h43
-rw-r--r--lib/decoding/tch_f_decoder_impl.cc242
-rw-r--r--lib/decoding/tch_f_decoder_impl.h65
-rw-r--r--lib/demapping/tch_f_chans_demapper_impl.cc88
-rw-r--r--swig/grgsm_swig.i3
20 files changed, 2569 insertions, 66 deletions
diff --git a/grc/decoding/CMakeLists.txt b/grc/decoding/CMakeLists.txt
index 36820f3..2f6eada 100644
--- a/grc/decoding/CMakeLists.txt
+++ b/grc/decoding/CMakeLists.txt
@@ -18,5 +18,6 @@
# Boston, MA 02110-1301, USA.
install(FILES
- gsm_control_channels_decoder.xml DESTINATION share/gnuradio/grc/blocks
+ gsm_control_channels_decoder.xml
+ gsm_tch_f_decoder.xml DESTINATION share/gnuradio/grc/blocks
)
diff --git a/grc/decoding/gsm_tch_f_decoder.xml b/grc/decoding/gsm_tch_f_decoder.xml
new file mode 100644
index 0000000..b8d6dbf
--- /dev/null
+++ b/grc/decoding/gsm_tch_f_decoder.xml
@@ -0,0 +1,37 @@
+<?xml version="1.0"?>
+<block>
+ <name>TCH/F decoder</name>
+ <key>gsm_tch_f_decoder</key>
+ <import>import grgsm</import>
+ <make>grgsm.tch_f_decoder($mode, $file)</make>
+
+ <param>
+ <name>TCH coding mode</name>
+ <key>mode</key>
+ <type>enum</type>
+ <option>
+ <name>GSM-FR</name>
+ <key>grgsm.MODE_SPEECH_FR</key>
+ </option>
+ <option>
+ <name>GSM-EFR</name>
+ <key>grgsm.MODE_SPEECH_EFR</key>
+ </option>
+ </param>
+ <param>
+ <name>destination file</name>
+ <key>file</key>
+ <value>/tmp/speech.gsm</value>
+ <type>file_open</type>
+ </param>
+
+ <sink>
+ <name>bursts</name>
+ <type>message</type>
+ </sink>
+ <source>
+ <name>msgs</name>
+ <type>message</type>
+ <optional>1</optional>
+ </source>
+</block>
diff --git a/grc/gsm_block_tree.xml b/grc/gsm_block_tree.xml
index 46f96ff..e32121c 100644
--- a/grc/gsm_block_tree.xml
+++ b/grc/gsm_block_tree.xml
@@ -31,6 +31,7 @@
<cat>
<name>Decoding</name>
<block>gsm_control_channels_decoder</block>
+ <block>gsm_tch_f_decoder</block>
</cat>
<cat>
<name>Utilities</name>
diff --git a/include/grgsm/decoding/CMakeLists.txt b/include/grgsm/decoding/CMakeLists.txt
index 3fa27ac..64bbb16 100644
--- a/include/grgsm/decoding/CMakeLists.txt
+++ b/include/grgsm/decoding/CMakeLists.txt
@@ -21,5 +21,6 @@
# Install public header files
########################################################################
install(FILES
- control_channels_decoder.h DESTINATION include/grgsm/decoding
+ control_channels_decoder.h
+ tch_f_decoder.h DESTINATION include/grgsm/decoding
)
diff --git a/include/grgsm/decoding/tch_f_decoder.h b/include/grgsm/decoding/tch_f_decoder.h
new file mode 100644
index 0000000..3c88dbe
--- /dev/null
+++ b/include/grgsm/decoding/tch_f_decoder.h
@@ -0,0 +1,65 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author Piotr Krysik <ptrkrysik@gmail.com>
+ * @section LICENSE
+ *
+ * Gr-gsm 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.
+ *
+ * Gr-gsm 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 gr-gsm; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+
+#ifndef INCLUDED_GSM_TCH_F_DECODER_H
+#define INCLUDED_GSM_TCH_F_DECODER_H
+
+#include <grgsm/api.h>
+#include <gnuradio/block.h>
+
+namespace gr {
+ namespace gsm {
+
+ enum tch_mode
+ {
+ MODE_SPEECH_FR,
+ MODE_SPEECH_EFR,
+ };
+
+ /*!
+ * \brief <+description of block+>
+ * \ingroup gsm
+ *
+ */
+ class GSM_API tch_f_decoder : virtual public gr::block
+ {
+ public:
+ typedef boost::shared_ptr<tch_f_decoder> sptr;
+
+ /*!
+ * \brief Return a shared_ptr to a new instance of gsm::tch_f_decoder.
+ *
+ * To avoid accidental use of raw pointers, gsm::tch_f_decoder's
+ * constructor is in a private implementation
+ * class. gsm::tch_f_decoder::make is the public interface for
+ * creating new instances.
+ */
+ static sptr make(tch_mode mode, const std::string &file);
+
+ };
+
+ } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_TCH_F_DECODER_H */
+
diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt
index 12bc893..028660f 100644
--- a/lib/CMakeLists.txt
+++ b/lib/CMakeLists.txt
@@ -37,6 +37,11 @@ list(APPEND grgsm_sources
decoding/control_channels_decoder_impl.cc
decoding/cch.c
decoding/fire_crc.c
+ decoding/tch_f_decoder_impl.cc
+ decoding/BitVector.cpp
+ decoding/GSM610Tables.cpp
+ decoding/GSM660Tables.cpp
+ decoding/GSM690Tables.cpp
misc_utils/controlled_rotator_cc_impl.cc
misc_utils/controlled_const_source_f_impl.cc
misc_utils/message_printer_impl.cc
diff --git a/lib/decoding/BitVector.cpp b/lib/decoding/BitVector.cpp
new file mode 100644
index 0000000..89d8d19
--- /dev/null
+++ b/lib/decoding/BitVector.cpp
@@ -0,0 +1,513 @@
+/*
+* Copyright 2008 Free Software Foundation, Inc.
+*
+* This software is distributed under the terms of the GNU Public License.
+* See the COPYING file in the main directory for details.
+
+ This program 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 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+
+
+#include "BitVector.h"
+#include <iostream>
+
+using namespace std;
+
+
+/**
+ Apply a Galois polymonial to a binary seqeunce.
+ @param val The input sequence.
+ @param poly The polynomial.
+ @param order The order of the polynomial.
+ @return Single-bit result.
+*/
+unsigned applyPoly(uint64_t val, uint64_t poly, unsigned order)
+{
+ uint64_t prod = val & poly;
+ unsigned sum = prod;
+ for (unsigned i=1; i<order; i++) sum ^= prod>>i;
+ return sum & 0x01;
+}
+
+
+
+
+
+
+BitVector::BitVector(const char *valString)
+ :Vector<char>(strlen(valString))
+{
+ uint32_t accum = 0;
+ for (size_t i=0; i<size(); i++) {
+ accum <<= 1;
+ if (valString[i]=='1') accum |= 0x01;
+ mStart[i] = accum;
+ }
+}
+
+
+
+
+
+uint64_t BitVector::peekField(size_t readIndex, unsigned length) const
+{
+ uint64_t accum = 0;
+ char *dp = mStart + readIndex;
+ assert(dp+length <= mEnd);
+ for (unsigned i=0; i<length; i++) {
+ accum = (accum<<1) | ((*dp++) & 0x01);
+ }
+ return accum;
+}
+
+
+uint64_t BitVector::readField(size_t& readIndex, unsigned length) const
+{
+ const uint64_t retVal = peekField(readIndex,length);
+ readIndex += length;
+ return retVal;
+}
+
+
+void BitVector::fillField(size_t writeIndex, uint64_t value, unsigned length)
+{
+ char *dpBase = mStart + writeIndex;
+ char *dp = dpBase + length - 1;
+ assert(dp < mEnd);
+ while (dp>=dpBase) {
+ *dp-- = value & 0x01;
+ value >>= 1;
+ }
+}
+
+void BitVector::writeField(size_t& writeIndex, uint64_t value, unsigned length)
+{
+ fillField(writeIndex,value,length);
+ writeIndex += length;
+}
+
+
+void BitVector::invert()
+{
+ for (size_t i=0; i<size(); i++) {
+ mStart[i] = ~mStart[i];
+ }
+}
+
+
+
+
+void BitVector::reverse8()
+{
+ assert(size()>=8);
+
+ char tmp0 = mStart[0];
+ mStart[0] = mStart[7];
+ mStart[7] = tmp0;
+
+ char tmp1 = mStart[1];
+ mStart[1] = mStart[6];
+ mStart[6] = tmp1;
+
+ char tmp2 = mStart[2];
+ mStart[2] = mStart[5];
+ mStart[5] = tmp2;
+
+ char tmp3 = mStart[3];
+ mStart[3] = mStart[4];
+ mStart[4] = tmp3;
+}
+
+
+
+void BitVector::LSB8MSB()
+{
+ size_t size8 = 8*(size()/8);
+ size_t iTop = size8 - 8;
+ for (size_t i=0; i<=iTop; i+=8) segment(i,8).reverse8();
+}
+
+
+
+uint64_t BitVector::syndrome(Generator& gen) const
+{
+ gen.clear();
+ const char *dp = mStart;
+ while (dp<mEnd) gen.syndromeShift(*dp++);
+ return gen.state();
+}
+
+
+uint64_t BitVector::parity(Generator& gen) const
+{
+ gen.clear();
+ const char *dp = mStart;
+ while (dp<mEnd) gen.encoderShift(*dp++);
+ return gen.state();
+}
+
+
+void BitVector::encode(const ViterbiR2O4& coder, BitVector& target)
+{
+ size_t sz = size();
+ assert(sz*coder.iRate() == target.size());
+
+ // Build a "history" array where each element contains the full history.
+ uint32_t history[sz];
+ uint32_t accum = 0;
+ for (size_t i=0; i<sz; i++) {
+ accum = (accum<<1) | bit(i);
+ history[i] = accum;
+ }
+
+ // Look up histories in the pre-generated state table.
+ char *op = target.begin();
+ for (size_t i=0; i<sz; i++) {
+ unsigned index = coder.cMask() & history[i];
+ for (unsigned g=0; g<coder.iRate(); g++) {
+ *op++ = coder.stateTable(g,index);
+ }
+ }
+}
+
+
+
+unsigned BitVector::sum() const
+{
+ unsigned sum = 0;
+ for (size_t i=0; i<size(); i++) sum += mStart[i] & 0x01;
+ return sum;
+}
+
+
+
+
+void BitVector::map(const unsigned *map, size_t mapSize, BitVector& dest) const
+{
+ for (unsigned i=0; i<mapSize; i++) {
+ dest.mStart[i] = mStart[map[i]];
+ }
+}
+
+
+
+
+void BitVector::unmap(const unsigned *map, size_t mapSize, BitVector& dest) const
+{
+ for (unsigned i=0; i<mapSize; i++) {
+ dest.mStart[map[i]] = mStart[i];
+ }
+}
+
+
+
+
+
+
+
+
+
+
+ostream& operator<<(ostream& os, const BitVector& hv)
+{
+ for (size_t i=0; i<hv.size(); i++) {
+ if (hv.bit(i)) os << '1';
+ else os << '0';
+ }
+ return os;
+}
+
+
+
+
+ViterbiR2O4::ViterbiR2O4()
+{
+ assert(mDeferral < 32);
+ mCoeffs[0] = 0x019;
+ mCoeffs[1] = 0x01b;
+ computeStateTables(0);
+ computeStateTables(1);
+ computeGeneratorTable();
+}
+
+
+
+
+void ViterbiR2O4::initializeStates()
+{
+ for (unsigned i=0; i<mIStates; i++) clear(mSurvivors[i]);
+ for (unsigned i=0; i<mNumCands; i++) clear(mCandidates[i]);
+}
+
+
+
+void ViterbiR2O4::computeStateTables(unsigned g)
+{
+ assert(g<mIRate);
+ for (unsigned state=0; state<mIStates; state++) {
+ // 0 input
+ uint32_t inputVal = state<<1;
+ mStateTable[g][inputVal] = applyPoly(inputVal, mCoeffs[g], mOrder+1);
+ // 1 input
+ inputVal |= 1;
+ mStateTable[g][inputVal] = applyPoly(inputVal, mCoeffs[g], mOrder+1);
+ }
+}
+
+void ViterbiR2O4::computeGeneratorTable()
+{
+ for (unsigned index=0; index<mIStates*2; index++) {
+ mGeneratorTable[index] = (mStateTable[0][index]<<1) | mStateTable[1][index];
+ }
+}
+
+
+
+
+
+
+void ViterbiR2O4::branchCandidates()
+{
+ // Branch to generate new input states.
+ const vCand *sp = mSurvivors;
+ for (unsigned i=0; i<mNumCands; i+=2) {
+ // extend and suffix
+ const uint32_t iState0 = (sp->iState) << 1; // input state for 0
+ const uint32_t iState1 = iState0 | 0x01; // input state for 1
+ const uint32_t oStateShifted = (sp->oState) << mIRate; // shifted output
+ const float cost = sp->cost;
+ sp++;
+ // 0 input extension
+ mCandidates[i].cost = cost;
+ mCandidates[i].oState = oStateShifted | mGeneratorTable[iState0 & mCMask];
+ mCandidates[i].iState = iState0;
+ // 1 input extension
+ mCandidates[i+1].cost = cost;
+ mCandidates[i+1].oState = oStateShifted | mGeneratorTable[iState1 & mCMask];
+ mCandidates[i+1].iState = iState1;
+ }
+}
+
+
+void ViterbiR2O4::getSoftCostMetrics(const uint32_t inSample, const float *matchCost, const float *mismatchCost)
+{
+ const float *cTab[2] = {matchCost,mismatchCost};
+ for (unsigned i=0; i<mNumCands; i++) {
+ vCand& thisCand = mCandidates[i];
+ // We examine input bits 2 at a time for a rate 1/2 coder.
+ const unsigned mismatched = inSample ^ (thisCand.oState);
+ thisCand.cost += cTab[mismatched&0x01][1] + cTab[(mismatched>>1)&0x01][0];
+ }
+}
+
+
+void ViterbiR2O4::pruneCandidates()
+{
+ const vCand* c1 = mCandidates; // 0-prefix
+ const vCand* c2 = mCandidates + mIStates; // 1-prefix
+ for (unsigned i=0; i<mIStates; i++) {
+ if (c1[i].cost < c2[i].cost) mSurvivors[i] = c1[i];
+ else mSurvivors[i] = c2[i];
+ }
+}
+
+
+const ViterbiR2O4::vCand& ViterbiR2O4::minCost() const
+{
+ int minIndex = 0;
+ float minCost = mSurvivors[0].cost;
+ for (unsigned i=1; i<mIStates; i++) {
+ const float thisCost = mSurvivors[i].cost;
+ if (thisCost>=minCost) continue;
+ minCost = thisCost;
+ minIndex=i;
+ }
+ return mSurvivors[minIndex];
+}
+
+
+const ViterbiR2O4::vCand& ViterbiR2O4::step(uint32_t inSample, const float *probs, const float *iprobs)
+{
+ branchCandidates();
+ getSoftCostMetrics(inSample,probs,iprobs);
+ pruneCandidates();
+ return minCost();
+}
+
+
+uint64_t Parity::syndrome(const BitVector& receivedCodeword)
+{
+ return receivedCodeword.syndrome(*this);
+}
+
+
+void Parity::writeParityWord(const BitVector& data, BitVector& parityTarget, bool invert)
+{
+ uint64_t pWord = data.parity(*this);
+ if (invert) pWord = ~pWord;
+ parityTarget.fillField(0,pWord,size());
+}
+
+
+
+
+
+
+
+
+
+SoftVector::SoftVector(const BitVector& source)
+{
+ resize(source.size());
+ for (size_t i=0; i<size(); i++) {
+ if (source.bit(i)) mStart[i]=1.0F;
+ else mStart[i]=0.0F;
+ }
+}
+
+
+BitVector SoftVector::sliced() const
+{
+ size_t sz = size();
+ BitVector newSig(sz);
+ for (size_t i=0; i<sz; i++) {
+ if (mStart[i]>0.5F) newSig[i]=1;
+ else newSig[i] = 0;
+ }
+ return newSig;
+}
+
+
+
+void SoftVector::decode(ViterbiR2O4 &decoder, BitVector& target) const
+{
+ const size_t sz = size();
+ const unsigned deferral = decoder.deferral();
+ const size_t ctsz = sz + deferral;
+ assert(sz <= decoder.iRate()*target.size());
+
+ // Build a "history" array where each element contains the full history.
+ uint32_t history[ctsz];
+ {
+ BitVector bits = sliced();
+ uint32_t accum = 0;
+ for (size_t i=0; i<sz; i++) {
+ accum = (accum<<1) | bits.bit(i);
+ history[i] = accum;
+ }
+ // Repeat last bit at the end.
+ for (size_t i=sz; i<ctsz; i++) {
+ accum = (accum<<1) | (accum & 0x01);
+ history[i] = accum;
+ }
+ }
+
+ // Precompute metric tables.
+ float matchCostTable[ctsz];
+ float mismatchCostTable[ctsz];
+ {
+ const float *dp = mStart;
+ for (size_t i=0; i<sz; i++) {
+ // pVal is the probability that a bit is correct.
+ // ipVal is the probability that a bit is correct.
+ float pVal = dp[i];
+ if (pVal>0.5F) pVal = 1.0F-pVal;
+ float ipVal = 1.0F-pVal;
+ // This is a cheap approximation to an ideal cost function.
+ if (pVal<0.01F) pVal = 0.01;
+ if (ipVal<0.01F) ipVal = 0.01;
+ matchCostTable[i] = 0.25F/ipVal;
+ mismatchCostTable[i] = 0.25F/pVal;
+ }
+
+ // pad end of table with unknowns
+ for (size_t i=sz; i<ctsz; i++) {
+ matchCostTable[i] = 0.5F;
+ mismatchCostTable[i] = 0.5F;
+ }
+ }
+
+ {
+ decoder.initializeStates();
+ // Each sample of history[] carries its history.
+ // So we only have to process every iRate-th sample.
+ const unsigned step = decoder.iRate();
+ // input pointer
+ const uint32_t *ip = history + step - 1;
+ // output pointers
+ char *op = target.begin();
+ const char *const opt = target.end();
+ // table pointers
+ const float* match = matchCostTable;
+ const float* mismatch = mismatchCostTable;
+ size_t oCount = 0;
+ while (op<opt) {
+ // Viterbi algorithm
+ const ViterbiR2O4::vCand &minCost = decoder.step(*ip, match, mismatch);
+ ip += step;
+ match += step;
+ mismatch += step;
+ // output
+ if (oCount>=deferral) *op++ = (minCost.iState >> deferral);
+ oCount++;
+ }
+ }
+}
+
+
+
+
+ostream& operator<<(ostream& os, const SoftVector& sv)
+{
+ for (size_t i=0; i<sv.size(); i++) {
+ if (sv[i]<0.25) os << "0";
+ else if (sv[i]>0.75) os << "1";
+ else os << "-";
+ }
+ return os;
+}
+
+
+
+void BitVector::pack(unsigned char* targ) const
+{
+ // Assumes MSB-first packing.
+ unsigned bytes = size()/8;
+ for (unsigned i=0; i<bytes; i++) {
+ targ[i] = peekField(i*8,8);
+ }
+ unsigned whole = bytes*8;
+ unsigned rem = size() - whole;
+ if (rem==0) return;
+ targ[bytes] = peekField(whole,rem) << (8-rem);
+}
+
+
+void BitVector::unpack(const unsigned char* src)
+{
+ // Assumes MSB-first packing.
+ unsigned bytes = size()/8;
+ for (unsigned i=0; i<bytes; i++) {
+ fillField(i*8,src[i],8);
+ }
+ unsigned whole = bytes*8;
+ unsigned rem = size() - whole;
+ if (rem==0) return;
+ fillField(whole,src[bytes],rem);
+}
+
+// vim: ts=4 sw=4
diff --git a/lib/decoding/BitVector.h b/lib/decoding/BitVector.h
new file mode 100644
index 0000000..3019c2c
--- /dev/null
+++ b/lib/decoding/BitVector.h
@@ -0,0 +1,427 @@
+/*
+* Copyright 2008 Free Software Foundation, Inc.
+*
+* This software is distributed under the terms of the GNU Public License.
+* See the COPYING file in the main directory for details.
+
+ This program 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 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+#ifndef FECVECTORS_H
+#define FECVECTORS_H
+
+#include "Vector.h"
+#include <stdint.h>
+
+
+class BitVector;
+class SoftVector;
+
+
+
+/** Shift-register (LFSR) generator. */
+class Generator {
+
+ private:
+
+ uint64_t mCoeff; ///< polynomial coefficients. LSB is zero exponent.
+ uint64_t mState; ///< shift register state. LSB is most recent.
+ uint64_t mMask; ///< mask for reading state
+ unsigned mLen; ///< number of bits used in shift register
+ unsigned mLen_1; ///< mLen - 1
+
+ public:
+
+ Generator(uint64_t wCoeff, unsigned wLen)
+ :mCoeff(wCoeff),mState(0),
+ mMask((1ULL<<wLen)-1),
+ mLen(wLen),mLen_1(wLen-1)
+ { assert(wLen<64); }
+
+ void clear() { mState=0; }
+
+ /**@name Accessors */
+ //@{
+ uint64_t state() const { return mState & mMask; }
+ unsigned size() const { return mLen; }
+ //@}
+
+ /**
+ Calculate one bit of a syndrome.
+ This is in the .h for inlining.
+ */
+ void syndromeShift(unsigned inBit)
+ {
+ const unsigned fb = (mState>>(mLen_1)) & 0x01;
+ mState = (mState<<1) ^ (inBit & 0x01);
+ if (fb) mState ^= mCoeff;
+ }
+
+ /**
+ Update the generator state by one cycle.
+ This is in the .h for inlining.
+ */
+ void encoderShift(unsigned inBit)
+ {
+ const unsigned fb = ((mState>>(mLen_1)) ^ inBit) & 0x01;
+ mState <<= 1;
+ if (fb) mState ^= mCoeff;
+ }
+
+
+};
+
+
+
+
+/** Parity (CRC-type) generator and checker based on a Generator. */
+class Parity : public Generator {
+
+ protected:
+
+ unsigned mCodewordSize;
+
+ public:
+
+ Parity(uint64_t wCoefficients, unsigned wParitySize, unsigned wCodewordSize)
+ :Generator(wCoefficients, wParitySize),
+ mCodewordSize(wCodewordSize)
+ { }
+
+ /** Compute the parity word and write it into the target segment. */
+ void writeParityWord(const BitVector& data, BitVector& parityWordTarget, bool invert=true);
+
+ /** Compute the syndrome of a received sequence. */
+ uint64_t syndrome(const BitVector& receivedCodeword);
+};
+
+
+
+
+/**
+ Class to represent convolutional coders/decoders of rate 1/2, memory length 4.
+ This is the "workhorse" coder for most GSM channels.
+*/
+class ViterbiR2O4 {
+
+ private:
+ /**name Lots of precomputed elements so the compiler can optimize like hell. */
+ //@{
+ /**@name Core values. */
+ //@{
+ static const unsigned mIRate = 2; ///< reciprocal of rate
+ static const unsigned mOrder = 4; ///< memory length of generators
+ //@}
+ /**@name Derived values. */
+ //@{
+ static const unsigned mIStates = 0x01 << mOrder; ///< number of states, number of survivors
+ static const uint32_t mSMask = mIStates-1; ///< survivor mask
+ static const uint32_t mCMask = (mSMask<<1) | 0x01; ///< candidate mask
+ static const uint32_t mOMask = (0x01<<mIRate)-1; ///< ouput mask, all iRate low bits set
+ static const unsigned mNumCands = mIStates*2; ///< number of candidates to generate during branching
+ static const unsigned mDeferral = 6*mOrder; ///< deferral to be used
+ //@}
+ //@}
+
+ /** Precomputed tables. */
+ //@{
+ uint32_t mCoeffs[mIRate]; ///< polynomial for each generator
+ uint32_t mStateTable[mIRate][2*mIStates]; ///< precomputed generator output tables
+ uint32_t mGeneratorTable[2*mIStates]; ///< precomputed coder output table
+ //@}
+
+ public:
+
+ /**
+ A candidate sequence in a Viterbi decoder.
+ The 32-bit state register can support a deferral of 6 with a 4th-order coder.
+ */
+ typedef struct candStruct {
+ uint32_t iState; ///< encoder input associated with this candidate
+ uint32_t oState; ///< encoder output associated with this candidate
+ float cost; ///< cost (metric value), float to support soft inputs
+ } vCand;
+
+ /** Clear a structure. */
+ void clear(vCand& v)
+ {
+ v.iState=0;
+ v.oState=0;
+ v.cost=0;
+ }
+
+
+ private:
+
+ /**@name Survivors and candidates. */
+ //@{
+ vCand mSurvivors[mIStates]; ///< current survivor pool
+ vCand mCandidates[2*mIStates]; ///< current candidate pool
+ //@}
+
+ public:
+
+ unsigned iRate() const { return mIRate; }
+ uint32_t cMask() const { return mCMask; }
+ uint32_t stateTable(unsigned g, unsigned i) const { return mStateTable[g][i]; }
+ unsigned deferral() const { return mDeferral; }
+
+
+ ViterbiR2O4();
+
+ /** Set all cost metrics to zero. */
+ void initializeStates();
+
+ /**
+ Full cycle of the Viterbi algorithm: branch, metrics, prune, select.
+ @return reference to minimum-cost candidate.
+ */
+ const vCand& step(uint32_t inSample, const float *probs, const float *iprobs);
+
+ private:
+
+ /** Branch survivors into new candidates. */
+ void branchCandidates();
+
+ /** Compute cost metrics for soft-inputs. */
+ void getSoftCostMetrics(uint32_t inSample, const float *probs, const float *iprobs);
+
+ /** Select survivors from the candidate set. */
+ void pruneCandidates();
+
+ /** Find the minimum cost survivor. */
+ const vCand& minCost() const;
+
+ /**
+ Precompute the state tables.
+ @param g Generator index 0..((1/rate)-1)
+ */
+ void computeStateTables(unsigned g);
+
+ /**
+ Precompute the generator outputs.
+ mCoeffs must be defined first.
+ */
+ void computeGeneratorTable();
+
+};
+
+
+
+
+class BitVector : public Vector<char> {
+
+
+ public:
+
+ /**@name Constructors. */
+ //@{
+
+ /**@name Casts of Vector constructors. */
+ //@{
+ BitVector(char* wData, char* wStart, char* wEnd)
+ :Vector<char>(wData,wStart,wEnd)
+ { }
+ BitVector(size_t len=0):Vector<char>(len) {}
+ BitVector(const Vector<char>& source):Vector<char>(source) {}
+ BitVector(Vector<char>& source):Vector<char>(source) {}
+ BitVector(const Vector<char>& source1, const Vector<char> source2):Vector<char>(source1,source2) {}
+ //@}
+
+ /** Construct from a string of "0" and "1". */
+ BitVector(const char* valString);
+ //@}
+
+ /** Index a single bit. */
+ bool bit(size_t index) const
+ {
+ // We put this code in .h for fast inlining.
+ const char *dp = mStart+index;
+ assert(dp<mEnd);
+ return (*dp) & 0x01;
+ }
+
+ /**@name Casts and overrides of Vector operators. */
+ //@{
+ BitVector segment(size_t start, size_t span)
+ {
+ char* wStart = mStart + start;
+ char* wEnd = wStart + span;
+ assert(wEnd<=mEnd);
+ return BitVector(NULL,wStart,wEnd);
+ }
+
+ BitVector alias()
+ { return segment(0,size()); }
+
+ const BitVector segment(size_t start, size_t span) const
+ { return (BitVector)(Vector<char>::segment(start,span)); }
+
+ BitVector head(size_t span) { return segment(0,span); }
+ const BitVector head(size_t span) const { return segment(0,span); }
+ BitVector tail(size_t start) { return segment(start,size()-start); }
+ const BitVector tail(size_t start) const { return segment(start,size()-start); }
+ //@}
+
+
+ void zero() { fill(0); }
+
+ /**@name FEC operations. */
+ //@{
+ /** Calculate the syndrome of the vector with the given Generator. */
+ uint64_t syndrome(Generator& gen) const;
+ /** Calculate the parity word for the vector with the given Generator. */
+ uint64_t parity(Generator& gen) const;
+ /** Encode the signal with the GSM rate 1/2 convolutional encoder. */
+ void encode(const ViterbiR2O4& encoder, BitVector& target);
+ //@}
+
+
+ /** Invert 0<->1. */
+ void invert();
+
+ /**@name Byte-wise operations. */
+ //@{
+ /** Reverse an 8-bit vector. */
+ void reverse8();
+ /** Reverse groups of 8 within the vector (byte reversal). */
+ void LSB8MSB();
+ //@}
+
+ /**@name Serialization and deserialization. */
+ //@{
+ uint64_t peekField(size_t readIndex, unsigned length) const;
+ uint64_t readField(size_t& readIndex, unsigned length) const;
+ void fillField(size_t writeIndex, uint64_t value, unsigned length);
+ void writeField(size_t& writeIndex, uint64_t value, unsigned length);
+ //@}
+
+ /** Sum of bits. */
+ unsigned sum() const;
+
+ /** Reorder bits, dest[i] = this[map[i]]. */
+ void map(const unsigned *map, size_t mapSize, BitVector& dest) const;
+
+ /** Reorder bits, dest[map[i]] = this[i]. */
+ void unmap(const unsigned *map, size_t mapSize, BitVector& dest) const;
+
+ /** Pack into a char array. */
+ void pack(unsigned char*) const;
+
+ /** Unopack from a char array. */
+ void unpack(const unsigned char*);
+
+};
+
+
+
+std::ostream& operator<<(std::ostream&, const BitVector&);
+
+
+
+
+
+
+/**
+ The SoftVector class is used to represent a soft-decision signal.
+ Values 0..1 represent probabilities that a bit is "true".
+ */
+class SoftVector: public Vector<float> {
+
+ public:
+
+ /** Build a SoftVector of a given length. */
+ SoftVector(size_t wSize=0):Vector<float>(wSize) {}
+
+ /** Construct a SoftVector from a C string of "0", "1", and "X". */
+ SoftVector(const char* valString);
+
+ /** Construct a SoftVector from a BitVector. */
+ SoftVector(const BitVector& source);
+
+ /**
+ Wrap a SoftVector around a block of floats.
+ The block will be delete[]ed upon desctuction.
+ */
+ SoftVector(float *wData, unsigned length)
+ :Vector<float>(wData,length)
+ {}
+
+ SoftVector(float* wData, float* wStart, float* wEnd)
+ :Vector<float>(wData,wStart,wEnd)
+ { }
+
+ /**
+ Casting from a Vector<float>.
+ Note that this is NOT pass-by-reference.
+ */
+ SoftVector(Vector<float> source)
+ :Vector<float>(source)
+ {}
+
+
+ /**@name Casts and overrides of Vector operators. */
+ //@{
+ SoftVector segment(size_t start, size_t span)
+ {
+ float* wStart = mStart + start;
+ float* wEnd = wStart + span;
+ assert(wEnd<=mEnd);
+ return SoftVector(NULL,wStart,wEnd);
+ }
+
+ SoftVector alias()
+ { return segment(0,size()); }
+
+ const SoftVector segment(size_t start, size_t span) const
+ { return (SoftVector)(Vector<float>::segment(start,span)); }
+
+ SoftVector head(size_t span) { return segment(0,span); }
+ const SoftVector head(size_t span) const { return segment(0,span); }
+ SoftVector tail(size_t start) { return segment(start,size()-start); }
+ const SoftVector tail(size_t start) const { return segment(start,size()-start); }
+ //@}
+
+ /** Decode soft symbols with the GSM rate-1/2 Viterbi decoder. */
+ void decode(ViterbiR2O4 &decoder, BitVector& target) const;
+
+ /** Fill with "unknown" values. */
+ void unknown() { fill(0.5F); }
+
+ /** Return a hard bit value from a given index by slicing. */
+ bool bit(size_t index) const
+ {
+ const float *dp = mStart+index;
+ assert(dp<mEnd);
+ return (*dp)>0.5F;
+ }
+
+ /** Slice the whole signal into bits. */
+ BitVector sliced() const;
+
+};
+
+
+
+std::ostream& operator<<(std::ostream&, const SoftVector&);
+
+
+
+
+
+
+#endif
+// vim: ts=4 sw=4
diff --git a/lib/decoding/GSM610Tables.cpp b/lib/decoding/GSM610Tables.cpp
new file mode 100644
index 0000000..38f643f
--- /dev/null
+++ b/lib/decoding/GSM610Tables.cpp
@@ -0,0 +1,492 @@
+/*
+* Copyright 2008 Free Software Foundation, Inc.
+*
+* This software is distributed under the terms of the GNU Public License.
+* See the COPYING file in the main directory for details.
+
+ This program 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 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+
+#include "GSM610Tables.h"
+
+
+/*
+RFC 3551 RTP A/V Profile July 2003
+
+
+ Octet Bit 0 Bit 1 Bit 2 Bit 3 Bit 4 Bit 5 Bit 6 Bit 7
+ _____________________________________________________________________
+ 0 1 1 0 1 LARc0.0 LARc0.1 LARc0.2 LARc0.3
+ 1 LARc0.4 LARc0.5 LARc1.0 LARc1.1 LARc1.2 LARc1.3 LARc1.4 LARc1.5
+ 2 LARc2.0 LARc2.1 LARc2.2 LARc2.3 LARc2.4 LARc3.0 LARc3.1 LARc3.2
+ 3 LARc3.3 LARc3.4 LARc4.0 LARc4.1 LARc4.2 LARc4.3 LARc5.0 LARc5.1
+ 4 LARc5.2 LARc5.3 LARc6.0 LARc6.1 LARc6.2 LARc7.0 LARc7.1 LARc7.2
+ 5 Nc0.0 Nc0.1 Nc0.2 Nc0.3 Nc0.4 Nc0.5 Nc0.6 bc0.0
+ 6 bc0.1 Mc0.0 Mc0.1 xmaxc00 xmaxc01 xmaxc02 xmaxc03 xmaxc04
+ 7 xmaxc05 xmc0.0 xmc0.1 xmc0.2 xmc1.0 xmc1.1 xmc1.2 xmc2.0
+ 8 xmc2.1 xmc2.2 xmc3.0 xmc3.1 xmc3.2 xmc4.0 xmc4.1 xmc4.2
+ 9 xmc5.0 xmc5.1 xmc5.2 xmc6.0 xmc6.1 xmc6.2 xmc7.0 xmc7.1
+ 10 xmc7.2 xmc8.0 xmc8.1 xmc8.2 xmc9.0 xmc9.1 xmc9.2 xmc10.0
+ 11 xmc10.1 xmc10.2 xmc11.0 xmc11.1 xmc11.2 xmc12.0 xmc12.1 xcm12.2
+ 12 Nc1.0 Nc1.1 Nc1.2 Nc1.3 Nc1.4 Nc1.5 Nc1.6 bc1.0
+ 13 bc1.1 Mc1.0 Mc1.1 xmaxc10 xmaxc11 xmaxc12 xmaxc13 xmaxc14
+ 14 xmax15 xmc13.0 xmc13.1 xmc13.2 xmc14.0 xmc14.1 xmc14.2 xmc15.0
+ 15 xmc15.1 xmc15.2 xmc16.0 xmc16.1 xmc16.2 xmc17.0 xmc17.1 xmc17.2
+ 16 xmc18.0 xmc18.1 xmc18.2 xmc19.0 xmc19.1 xmc19.2 xmc20.0 xmc20.1
+ 17 xmc20.2 xmc21.0 xmc21.1 xmc21.2 xmc22.0 xmc22.1 xmc22.2 xmc23.0
+ 18 xmc23.1 xmc23.2 xmc24.0 xmc24.1 xmc24.2 xmc25.0 xmc25.1 xmc25.2
+ 19 Nc2.0 Nc2.1 Nc2.2 Nc2.3 Nc2.4 Nc2.5 Nc2.6 bc2.0
+ 20 bc2.1 Mc2.0 Mc2.1 xmaxc20 xmaxc21 xmaxc22 xmaxc23 xmaxc24
+ 21 xmaxc25 xmc26.0 xmc26.1 xmc26.2 xmc27.0 xmc27.1 xmc27.2 xmc28.0
+ 22 xmc28.1 xmc28.2 xmc29.0 xmc29.1 xmc29.2 xmc30.0 xmc30.1 xmc30.2
+ 23 xmc31.0 xmc31.1 xmc31.2 xmc32.0 xmc32.1 xmc32.2 xmc33.0 xmc33.1
+ 24 xmc33.2 xmc34.0 xmc34.1 xmc34.2 xmc35.0 xmc35.1 xmc35.2 xmc36.0
+ 25 Xmc36.1 xmc36.2 xmc37.0 xmc37.1 xmc37.2 xmc38.0 xmc38.1 xmc38.2
+ 26 Nc3.0 Nc3.1 Nc3.2 Nc3.3 Nc3.4 Nc3.5 Nc3.6 bc3.0
+ 27 bc3.1 Mc3.0 Mc3.1 xmaxc30 xmaxc31 xmaxc32 xmaxc33 xmaxc34
+ 28 xmaxc35 xmc39.0 xmc39.1 xmc39.2 xmc40.0 xmc40.1 xmc40.2 xmc41.0
+ 29 xmc41.1 xmc41.2 xmc42.0 xmc42.1 xmc42.2 xmc43.0 xmc43.1 xmc43.2
+ 30 xmc44.0 xmc44.1 xmc44.2 xmc45.0 xmc45.1 xmc45.2 xmc46.0 xmc46.1
+ 31 xmc46.2 xmc47.0 xmc47.1 xmc47.2 xmc48.0 xmc48.1 xmc48.2 xmc49.0
+ 32 xmc49.1 xmc49.2 xmc50.0 xmc50.1 xmc50.2 xmc51.0 xmc51.1 xmc51.2
+
+ Table 3: GSM payload format
+*/
+
+
+/*
+ This file encodes a mapping between
+ GSM 05.03 Table 2 and RFC-3551 Table 3.
+*/
+
+/*
+ Naming convention:
+ xxx_p position (bit index)
+ xxx_l length (bit field length)
+ LAR log area ratio
+ N LTP lag
+ b LTP gain
+ M grid
+ Xmax block amplitude
+ x RPE pulses
+*/
+
+
+/**@name Lengths of GSM 06.10 fields */
+//@{
+const unsigned int LAR1_l=6; ///< log area ratio
+const unsigned int LAR2_l=6; ///< log area ratio
+const unsigned int LAR3_l=5; ///< log area ratio
+const unsigned int LAR4_l=5; ///< log area ratio
+const unsigned int LAR5_l=4; ///< log area ratio
+const unsigned int LAR6_l=4; ///< log area ratio
+const unsigned int LAR7_l=3; ///< log area ratio
+const unsigned int LAR8_l=3; ///< log area ratio
+const unsigned int N_l=7; ///< LTP lag
+const unsigned int b_l=2; ///< LTP gain
+const unsigned int M_l=2; ///< grid position
+const unsigned int Xmax_l=6; ///< block amplitude
+const unsigned int x_l=3; ///< RPE pulses
+//@}
+
+
+
+/*@name Indecies of GSM 06.10 fields as they appear in RFC-3551 Table 3. */
+//@{
+
+/**@name Log area ratios, apply to whole frame. */
+//@{
+const unsigned int LAR1_p = 0;
+const unsigned int LAR2_p = LAR1_p + LAR1_l;
+const unsigned int LAR3_p = LAR2_p + LAR2_l;
+const unsigned int LAR4_p = LAR3_p + LAR3_l;
+const unsigned int LAR5_p = LAR4_p + LAR4_l;
+const unsigned int LAR6_p = LAR5_p + LAR5_l;
+const unsigned int LAR7_p = LAR6_p + LAR6_l;
+const unsigned int LAR8_p = LAR7_p + LAR7_l;
+//@}
+/**@name Subframe 1 */
+//@{
+const unsigned int N1_p = LAR8_p + LAR8_l;
+const unsigned int b1_p = N1_p + N_l;
+const unsigned int M1_p = b1_p + b_l;
+const unsigned int Xmax1_p = M1_p + M_l;
+const unsigned int x1_0_p = Xmax1_p + Xmax_l;
+const unsigned int x1_1_p = x1_0_p + x_l;
+const unsigned int x1_2_p = x1_1_p + x_l;
+const unsigned int x1_3_p = x1_2_p + x_l;
+const unsigned int x1_4_p = x1_3_p + x_l;
+const unsigned int x1_5_p = x1_4_p + x_l;
+const unsigned int x1_6_p = x1_5_p + x_l;
+const unsigned int x1_7_p = x1_6_p + x_l;
+const unsigned int x1_8_p = x1_7_p + x_l;
+const unsigned int x1_9_p = x1_8_p + x_l;
+const unsigned int x1_10_p = x1_9_p + x_l;
+const unsigned int x1_11_p = x1_10_p + x_l;
+const unsigned int x1_12_p = x1_11_p + x_l;
+//@}
+/**@name Subframe 2 */
+//@{
+const unsigned int N2_p = x1_12_p + x_l;
+const unsigned int b2_p = N2_p + N_l;
+const unsigned int M2_p = b2_p + b_l;
+const unsigned int Xmax2_p = M2_p + M_l;
+const unsigned int x2_0_p = Xmax2_p + Xmax_l;
+const unsigned int x2_1_p = x2_0_p + x_l;
+const unsigned int x2_2_p = x2_1_p + x_l;
+const unsigned int x2_3_p = x2_2_p + x_l;
+const unsigned int x2_4_p = x2_3_p + x_l;
+const unsigned int x2_5_p = x2_4_p + x_l;
+const unsigned int x2_6_p = x2_5_p + x_l;
+const unsigned int x2_7_p = x2_6_p + x_l;
+const unsigned int x2_8_p = x2_7_p + x_l;
+const unsigned int x2_9_p = x2_8_p + x_l;
+const unsigned int x2_10_p = x2_9_p + x_l;
+const unsigned int x2_11_p = x2_10_p + x_l;
+const unsigned int x2_12_p = x2_11_p + x_l;
+//@}
+/**@mame Subframe 3 */
+//@{
+const unsigned int N3_p = x2_12_p + x_l;
+const unsigned int b3_p = N3_p + N_l;
+const unsigned int M3_p = b3_p + b_l;
+const unsigned int Xmax3_p = M3_p + M_l;
+const unsigned int x3_0_p = Xmax3_p + Xmax_l;
+const unsigned int x3_1_p = x3_0_p + x_l;
+const unsigned int x3_2_p = x3_1_p + x_l;
+const unsigned int x3_3_p = x3_2_p + x_l;
+const unsigned int x3_4_p = x3_3_p + x_l;
+const unsigned int x3_5_p = x3_4_p + x_l;
+const unsigned int x3_6_p = x3_5_p + x_l;
+const unsigned int x3_7_p = x3_6_p + x_l;
+const unsigned int x3_8_p = x3_7_p + x_l;
+const unsigned int x3_9_p = x3_8_p + x_l;
+const unsigned int x3_10_p = x3_9_p + x_l;
+const unsigned int x3_11_p = x3_10_p + x_l;
+const unsigned int x3_12_p = x3_11_p + x_l;
+//@}
+/**@name Subframe 4 */
+//@{
+const unsigned int N4_p = x3_12_p + x_l;
+const unsigned int b4_p = N4_p + N_l;
+const unsigned int M4_p = b4_p + b_l;
+const unsigned int Xmax4_p = M4_p + M_l;
+const unsigned int x4_0_p = Xmax4_p + Xmax_l;
+const unsigned int x4_1_p = x4_0_p + x_l;
+const unsigned int x4_2_p = x4_1_p + x_l;
+const unsigned int x4_3_p = x4_2_p + x_l;
+const unsigned int x4_4_p = x4_3_p + x_l;
+const unsigned int x4_5_p = x4_4_p + x_l;
+const unsigned int x4_6_p = x4_5_p + x_l;
+const unsigned int x4_7_p = x4_6_p + x_l;
+const unsigned int x4_8_p = x4_7_p + x_l;
+const unsigned int x4_9_p = x4_8_p + x_l;
+const unsigned int x4_10_p = x4_9_p + x_l;
+const unsigned int x4_11_p = x4_10_p + x_l;
+const unsigned int x4_12_p = x4_11_p + x_l;
+//@}
+//@}
+
+
+/*
+ This array encodes GSM 05.03 Table 2.
+ It's also GSM 06.10 Table A2.1a.
+ This is the order of bits as they appear in
+ the d[] bits of the GSM TCH/F.
+ RTP[4+g610BitOrder[i]] <=> GSM[i]
+*/
+unsigned int GSM::g610BitOrder[260] = {
+/**@name importance class 1 */
+//@{
+/** LAR1:5 */ LAR1_p+LAR1_l-1-5, /* bit 0 */
+/** Xmax1:5 */ Xmax1_p+Xmax_l-1-5,
+/** Xmax2:5 */ Xmax2_p+Xmax_l-1-5,
+/** Xmax3:5 */ Xmax3_p+Xmax_l-1-5,
+/** Xmax4:5 */ Xmax4_p+Xmax_l-1-5,
+//@}
+/**@name importance class 2 */
+//@{
+/** LAR1:4 */ LAR1_p+LAR1_l-1-4,
+/** LAR2:5 */ LAR2_p+LAR2_l-1-5,
+/** LAR3:4 */ LAR3_p+LAR3_l-1-4,
+//@}
+/**@name importance class 3 */
+//@{
+/** LAR1:3 */ LAR1_p+LAR1_l-1-3,
+/** LAR2:4 */ LAR2_p+LAR2_l-1-4,
+/** LAR3:3 */ LAR3_p+LAR3_l-1-3, /* bit 10 */
+/** LAR4:4 */ LAR4_p+LAR4_l-1-4,
+/** N1:6 */ N1_p+N_l-1-6,
+/** N2:6 */ N2_p+N_l-1-6,
+/** N3:6 */ N3_p+N_l-1-6,
+/** N4:6 */ N4_p+N_l-1-6,
+/** Xmax1:4 */ Xmax1_p+Xmax_l-1-4,
+/** Xmax2:4 */ Xmax2_p+Xmax_l-1-4,
+/** Xmax3:4 */ Xmax3_p+Xmax_l-1-4,
+/** Xmax4:4 */ Xmax4_p+Xmax_l-1-4,
+/** LAR2:3 */ LAR2_p+LAR2_l-1-3, /* bit 20 */
+/** LAR5:3 */ LAR5_p+LAR5_l-1-3,
+/** LAR6:3 */ LAR6_p+LAR6_l-1-3,
+/** N1:5 */ N1_p+N_l-1-5,
+/** N2:5 */ N2_p+N_l-1-5,
+/** N3:5 */ N3_p+N_l-1-5,
+/** N4:5 */ N4_p+N_l-1-5,
+/** N1:4 */ N1_p+N_l-1-4,
+/** N2:4 */ N2_p+N_l-1-4,
+/** N3:4 */ N3_p+N_l-1-4,
+/** N4:4 */ N4_p+N_l-1-4, /* bit 30 */
+/** N1:3 */ N1_p+N_l-1-3,
+/** N2:3 */ N2_p+N_l-1-3,
+/** N3:3 */ N3_p+N_l-1-3,
+/** N4:3 */ N4_p+N_l-1-3,
+/** N1:2 */ N1_p+N_l-1-2,
+/** N2:2 */ N2_p+N_l-1-2,
+/** N3:2 */ N3_p+N_l-1-2,
+/** N4:2 */ N4_p+N_l-1-2,
+//@}
+/**@name importance class 4 */
+//@{
+/** Xmax1:3 */ Xmax1_p+Xmax_l-1-3,
+/** Xmax2:3 */ Xmax2_p+Xmax_l-1-3, /* bit 40 */
+/** Xmax3:3 */ Xmax3_p+Xmax_l-1-3,
+/** Xmax4:3 */ Xmax4_p+Xmax_l-1-3,
+/** LAR1:2 */ LAR1_p+LAR1_l-1-2,
+/** LAR4:3 */ LAR4_p+LAR4_l-1-3,
+/** LAR7:2 */ LAR7_p+LAR7_l-1-2,
+/** N1:1 */ N1_p+N_l-1-1,
+/** N2:1 */ N2_p+N_l-1-1,
+/** N3:1 */ N3_p+N_l-1-1,
+/** N4:1 */ N4_p+N_l-1-1,
+/** LAR5:2 */ LAR5_p+LAR5_l-1-2, /* bit 50 */
+/** LAR6:2 */ LAR6_p+LAR6_l-1-2,
+/** b1:1 */ b1_p+b_l-1-1,
+/** b2:1 */ b2_p+b_l-1-1,
+/** b3:1 */ b3_p+b_l-1-1,
+/** b4:1 */ b4_p+b_l-1-1,
+/** N1:0 */ N1_p+N_l-1-0,
+/** N2:0 */ N2_p+N_l-1-0,
+/** N3:0 */ N3_p+N_l-1-0,
+/** N4:0 */ N4_p+N_l-1-0,
+/** M1:1 */ M1_p+M_l-1-1, /* bit 60 */
+/** M2:1 */ M2_p+M_l-1-1,
+/** M3:1 */ M3_p+M_l-1-1,
+/** M4:1 */ M4_p+M_l-1-1,
+//@}
+/**@name importance class 5 */
+//@{
+/** LAR1:1 */ LAR1_p+LAR1_l-1-1,
+/** LAR2:2 */ LAR2_p+LAR2_l-1-2,
+/** LAR3:2 */ LAR3_p+LAR3_l-1-2,
+/** LAR8:2 */ LAR8_p+LAR8_l-1-2,
+/** LAR4:2 */ LAR4_p+LAR4_l-1-2,
+/** LAR5:1 */ LAR5_p+LAR5_l-1-1,
+/** LAR7:1 */ LAR7_p+LAR7_l-1-1, /* bit 70 */
+/** b1:0 */ b1_p+b_l-1-0,
+/** b2:0 */ b2_p+b_l-1-0,
+/** b3:0 */ b3_p+b_l-1-0,
+/** b4:0 */ b4_p+b_l-1-0,
+/** Xmax1:2 */ Xmax1_p+Xmax_l-1-2,
+/** Xmax2:2 */ Xmax2_p+Xmax_l-1-2,
+/** Xmax3:2 */ Xmax3_p+Xmax_l-1-2,
+/** Xmax4:2 */ Xmax4_p+Xmax_l-1-2,
+/** x1_0:2 */ x1_0_p+x_l-1-2,
+/** x1_1:2 */ x1_1_p+x_l-1-2, /* bit 80 */
+/** x1_2:2 */ x1_2_p+x_l-1-2,
+/** x1_3:2 */ x1_3_p+x_l-1-2,
+/** x1_4:2 */ x1_4_p+x_l-1-2,
+/** x1_5:2 */ x1_5_p+x_l-1-2,
+/** x1_6:2 */ x1_6_p+x_l-1-2,
+/** x1_7:2 */ x1_7_p+x_l-1-2,
+/** x1_8:2 */ x1_8_p+x_l-1-2,
+/** x1_9:2 */ x1_9_p+x_l-1-2,
+/** x1_10:2 */ x1_10_p+x_l-1-2,
+/** x1_11:2 */ x1_11_p+x_l-1-2, /* bit 90 */
+/** x1_12:2 */ x1_12_p+x_l-1-2,
+/** x2_0:2 */ x2_0_p+x_l-1-2,
+/** x2_1:2 */ x2_1_p+x_l-1-2,
+/** x2_2:2 */ x2_2_p+x_l-1-2,
+/** x2_3:2 */ x2_3_p+x_l-1-2,
+/** x2_4:2 */ x2_4_p+x_l-1-2,
+/** x2_5:2 */ x2_5_p+x_l-1-2,
+/** x2_6:2 */ x2_6_p+x_l-1-2,
+/** x2_7:2 */ x2_7_p+x_l-1-2,
+/** x2_8:2 */ x2_8_p+x_l-1-2, /* bit 100 */
+/** x2_9:2 */ x2_9_p+x_l-1-2,
+/** x2_10:2 */ x2_10_p+x_l-1-2,
+/** x2_11:2 */ x2_11_p+x_l-1-2,
+/** x2_12:2 */ x2_12_p+x_l-1-2,
+/** x3_0:2 */ x3_0_p+x_l-1-2,
+/** x3_1:2 */ x3_1_p+x_l-1-2,
+/** x3_2:2 */ x3_2_p+x_l-1-2,
+/** x3_3:2 */ x3_3_p+x_l-1-2,
+/** x3_4:2 */ x3_4_p+x_l-1-2,
+/** x3_5:2 */ x3_5_p+x_l-1-2, /* bit 110 */
+/** x3_6:2 */ x3_6_p+x_l-1-2,
+/** x3_7:2 */ x3_7_p+x_l-1-2,
+/** x3_8:2 */ x3_8_p+x_l-1-2,
+/** x3_9:2 */ x3_9_p+x_l-1-2,
+/** x3_10:2 */ x3_10_p+x_l-1-2,
+/** x3_11:2 */ x3_11_p+x_l-1-2,
+/** x3_12:2 */ x3_12_p+x_l-1-2,
+/** x4_0:2 */ x4_0_p+x_l-1-2,
+/** x4_1:2 */ x4_1_p+x_l-1-2,
+/** x4_2:2 */ x4_2_p+x_l-1-2, /* bit 120 */
+/** x4_3:2 */ x4_3_p+x_l-1-2,
+/** x4_4:2 */ x4_4_p+x_l-1-2,
+/** x4_5:2 */ x4_5_p+x_l-1-2,
+/** x4_6:2 */ x4_6_p+x_l-1-2,
+/** x4_7:2 */ x4_7_p+x_l-1-2,
+/** x4_8:2 */ x4_8_p+x_l-1-2,
+/** x4_9:2 */ x4_9_p+x_l-1-2,
+/** x4_10:2 */ x4_10_p+x_l-1-2,
+/** x4_11:2 */ x4_11_p+x_l-1-2,
+/** x4_12:2 */ x4_12_p+x_l-1-2, /* bit 130 */
+/** M1:0 */ M1_p+M_l-1-0,
+/** M2:0 */ M2_p+M_l-1-0,
+/** M3:0 */ M3_p+M_l-1-0,
+/** M4:0 */ M4_p+M_l-1-0,
+/** Xmax1:1 */ Xmax1_p+Xmax_l-1-1,
+/** Xmax2:1 */ Xmax2_p+Xmax_l-1-1,
+/** Xmax3:1 */ Xmax3_p+Xmax_l-1-1,
+/** Xmax4:1 */ Xmax4_p+Xmax_l-1-1,
+/** x1_0:1 */ x1_0_p+x_l-1-1,
+/** x1_1:1 */ x1_1_p+x_l-1-1, /* bit 140 */
+/** x1_2:1 */ x1_2_p+x_l-1-1,
+/** x1_3:1 */ x1_3_p+x_l-1-1,
+/** x1_4:1 */ x1_4_p+x_l-1-1,
+/** x1_5:1 */ x1_5_p+x_l-1-1,
+/** x1_6:1 */ x1_6_p+x_l-1-1,
+/** x1_7:1 */ x1_7_p+x_l-1-1,
+/** x1_8:1 */ x1_8_p+x_l-1-1,
+/** x1_9:1 */ x1_9_p+x_l-1-1,
+/** x1_10:1 */ x1_10_p+x_l-1-1,
+/** x1_11:1 */ x1_11_p+x_l-1-1, /* bit 150 */
+/** x1_12:1 */ x1_12_p+x_l-1-1,
+/** x2_0:1 */ x2_0_p+x_l-1-1,
+/** x2_1:1 */ x2_1_p+x_l-1-1,
+/** x2_2:1 */ x2_2_p+x_l-1-1,
+/** x2_3:1 */ x2_3_p+x_l-1-1,
+/** x2_4:1 */ x2_4_p+x_l-1-1,
+/** x2_5:1 */ x2_5_p+x_l-1-1,
+/** x2_6:1 */ x2_6_p+x_l-1-1,
+/** x2_7:1 */ x2_7_p+x_l-1-1,
+/** x2_8:1 */ x2_8_p+x_l-1-1, /* bit 160 */
+/** x2_9:1 */ x2_9_p+x_l-1-1,
+/** x2_10:1 */ x2_10_p+x_l-1-1,
+/** x2_11:1 */ x2_11_p+x_l-1-1,
+/** x2_12:1 */ x2_12_p+x_l-1-1,
+/** x3_0:1 */ x3_0_p+x_l-1-1,
+/** x3_1:1 */ x3_1_p+x_l-1-1,
+/** x3_2:1 */ x3_2_p+x_l-1-1,
+/** x3_3:1 */ x3_3_p+x_l-1-1,
+/** x3_4:1 */ x3_4_p+x_l-1-1,
+/** x3_5:1 */ x3_5_p+x_l-1-1, /* bit 170 */
+/** x3_6:1 */ x3_6_p+x_l-1-1,
+/** x3_7:1 */ x3_7_p+x_l-1-1,
+/** x3_8:1 */ x3_8_p+x_l-1-1,
+/** x3_9:1 */ x3_9_p+x_l-1-1,
+/** x3_10:1 */ x3_10_p+x_l-1-1,
+/** x3_11:1 */ x3_11_p+x_l-1-1,
+/** x3_12:1 */ x3_12_p+x_l-1-1,
+/** x4_0:1 */ x4_0_p+x_l-1-1,
+/** x4_1:1 */ x4_1_p+x_l-1-1,
+/** x4_2:1 */ x4_2_p+x_l-1-1, /* bit 180 */
+/** x4_3:1 */ x4_3_p+x_l-1-1,
+//@}
+/**@name importance class 6 */
+//@{
+/** x4_4:1 */ x4_4_p+x_l-1-1,
+/** x4_5:1 */ x4_5_p+x_l-1-1,
+/** x4_6:1 */ x4_6_p+x_l-1-1,
+/** x4_7:1 */ x4_7_p+x_l-1-1,
+/** x4_8:1 */ x4_8_p+x_l-1-1,
+/** x4_9:1 */ x4_9_p+x_l-1-1,
+/** x4_10:1 */ x4_10_p+x_l-1-1,
+/** x4_11:1 */ x4_11_p+x_l-1-1,
+/** x4_12:1 */ x4_12_p+x_l-1-1, /* bit 190 */
+/** LAR1:0 */ LAR1_p+LAR1_l-1-0,
+/** LAR2:1 */ LAR2_p+LAR2_l-1-1,
+/** LAR3:1 */ LAR3_p+LAR3_l-1-1,
+/** LAR6:1 */ LAR6_p+LAR6_l-1-1,
+/** LAR7:0 */ LAR7_p+LAR7_l-1-0,
+/** LAR8:1 */ LAR8_p+LAR8_l-1-1,
+/** LAR8:0 */ LAR8_p+LAR8_l-1-0,
+/** LAR3:0 */ LAR3_p+LAR3_l-1-0,
+/** LAR4:1 */ LAR4_p+LAR4_l-1-1,
+/** LAR4:0 */ LAR4_p+LAR4_l-1-0,
+/** LAR5:0 */ LAR5_p+LAR5_l-1-0,
+/** Xmax1:0 */ Xmax1_p+Xmax_l-1-0,
+/** Xmax2:0 */ Xmax2_p+Xmax_l-1-0,
+/** Xmax3:0 */ Xmax3_p+Xmax_l-1-0,
+/** Xmax4:0 */ Xmax4_p+Xmax_l-1-0,
+/** x1_0:0 */ x1_0_p+x_l-1-0,
+/** x1_1:0 */ x1_1_p+x_l-1-0,
+/** x1_2:0 */ x1_2_p+x_l-1-0,
+/** x1_3:0 */ x1_3_p+x_l-1-0,
+/** x1_4:0 */ x1_4_p+x_l-1-0,
+/** x1_5:0 */ x1_5_p+x_l-1-0,
+/** x1_6:0 */ x1_6_p+x_l-1-0,
+/** x1_7:0 */ x1_7_p+x_l-1-0,
+/** x1_8:0 */ x1_8_p+x_l-1-0,
+/** x1_9:0 */ x1_9_p+x_l-1-0,
+/** x1_10:0 */ x1_10_p+x_l-1-0,
+/** x1_11:0 */ x1_11_p+x_l-1-0,
+/** x1_12:0 */ x1_12_p+x_l-1-0,
+/** x2_0:0 */ x2_0_p+x_l-1-0,
+/** x2_1:0 */ x2_1_p+x_l-1-0,
+/** x2_2:0 */ x2_2_p+x_l-1-0,
+/** x2_3:0 */ x2_3_p+x_l-1-0,
+/** x2_4:0 */ x2_4_p+x_l-1-0,
+/** x2_5:0 */ x2_5_p+x_l-1-0,
+/** x2_6:0 */ x2_6_p+x_l-1-0,
+/** x2_7:0 */ x2_7_p+x_l-1-0,
+/** x2_8:0 */ x2_8_p+x_l-1-0,
+/** x2_9:0 */ x2_9_p+x_l-1-0,
+/** x2_10:0 */ x2_10_p+x_l-1-0,
+/** x2_11:0 */ x2_11_p+x_l-1-0,
+/** x2_12:0 */ x2_12_p+x_l-1-0,
+/** x3_0:0 */ x3_0_p+x_l-1-0,
+/** x3_1:0 */ x3_1_p+x_l-1-0,
+/** x3_2:0 */ x3_2_p+x_l-1-0,
+/** x3_3:0 */ x3_3_p+x_l-1-0,
+/** x3_4:0 */ x3_4_p+x_l-1-0,
+/** x3_5:0 */ x3_5_p+x_l-1-0,
+/** x3_6:0 */ x3_6_p+x_l-1-0,
+/** x3_7:0 */ x3_7_p+x_l-1-0,
+/** x3_8:0 */ x3_8_p+x_l-1-0,
+/** x3_9:0 */ x3_9_p+x_l-1-0,
+/** x3_10:0 */ x3_10_p+x_l-1-0,
+/** x3_11:0 */ x3_11_p+x_l-1-0,
+/** x3_12:0 */ x3_12_p+x_l-1-0,
+/** x4_0:0 */ x4_0_p+x_l-1-0,
+/** x4_1:0 */ x4_1_p+x_l-1-0,
+/** x4_2:0 */ x4_2_p+x_l-1-0,
+/** x4_3:0 */ x4_3_p+x_l-1-0,
+/** x4_4:0 */ x4_4_p+x_l-1-0,
+/** x4_5:0 */ x4_5_p+x_l-1-0,
+/** x4_6:0 */ x4_6_p+x_l-1-0,
+/** x4_7:0 */ x4_7_p+x_l-1-0,
+/** x4_8:0 */ x4_8_p+x_l-1-0,
+/** x4_9:0 */ x4_9_p+x_l-1-0,
+/** x4_10:0 */ x4_10_p+x_l-1-0,
+/** x4_11:0 */ x4_11_p+x_l-1-0,
+/** x4_12:0 */ x4_12_p+x_l-1-0,
+/** LAR2:0 */ LAR2_p+LAR2_l-1-0,
+/** LAR6:0 */ LAR6_p+LAR6_l-1-0
+//@}
+};
+
diff --git a/lib/decoding/GSM610Tables.h b/lib/decoding/GSM610Tables.h
new file mode 100644
index 0000000..53a8e1c
--- /dev/null
+++ b/lib/decoding/GSM610Tables.h
@@ -0,0 +1,37 @@
+/*
+* Copyright 2008 Free Software Foundation, Inc.
+*
+* This software is distributed under the terms of the GNU Public License.
+* See the COPYING file in the main directory for details.
+
+ This program 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 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+
+#ifndef GSM610TABLES_H
+#define GSM610TABLES_H
+
+
+
+namespace GSM {
+
+/** Table #2 from GSM 05.03 */
+extern unsigned int g610BitOrder[260];
+
+}
+
+
+#endif
diff --git a/lib/decoding/GSM660Tables.cpp b/lib/decoding/GSM660Tables.cpp
new file mode 100644
index 0000000..520a6c1
--- /dev/null
+++ b/lib/decoding/GSM660Tables.cpp
@@ -0,0 +1,244 @@
+/* EFR (GSM 06.60) importance bit ordering */
+
+/*
+ * Copyright 2010 Sylvain Munaut <tnt@246tNt.com>
+ * All Rights Reserved
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "GSM660Tables.h"
+
+unsigned int GSM::g660BitOrder[260] = {
+ 38, 39, 40, 41, 42, 43, /* 0 -> LTP-LAG 1: b8..b3 */
+ 145, 146, 147, 148, 149, 150, /* 6 -> LTP-LAG 3: b8..b3 */
+ 93, 94, /* 12 -> LTP-LAG 2: b5..b4 */
+ 200, 201, /* 14 -> LTP-LAG 4: b5..b4 */
+ 47, /* 16 -> LTP-GAIN 1: b3 */
+ 88, /* 17 -> FCB-GAIN 1: b4 */
+ 99, /* 18 -> LTP-GAIN 2: b3 */
+ 140, /* 19 -> FCB-GAIN 2: b4 */
+ 44, /* 20 -> LTP-LAG 1: b2 */
+ 151, /* 21 -> LTP-LAG 3: b2 */
+ 95, /* 22 -> LTP-LAG 2: b3 */
+ 202, /* 23 -> LTP-LAG 4: b3 */
+ 1, 2, /* 24 -> LPC 1: b5..b4 */
+ 7, /* 26 -> LPC 2: b7 */
+ 9, /* 27 -> LPC 2: b5 */
+ 17, 18, /* 28 -> LPC 3: b6..b5 */
+ 23, /* 30 -> LPC 3: b0 */
+ 45, 46, /* 31 -> LTP-LAG 1: b1..b0 */
+ 152, 153, /* 33 -> LTP-LAG 3: b1..b0 */
+ 96, /* 35 -> LTP-LAG 2: b2 */
+ 203, /* 36 -> LTP-LAG 4: b2 */
+ 3, 4, /* 37 -> LPC 1: b3..b2 */
+ 10, 11, /* 39 -> LPC 2: b4..b3 */
+ 15, /* 41 -> LPC 3: b8 */
+ 8, /* 42 -> LPC 2: b6 */
+ 5, 6, /* 43 -> LPC 1: b1..b0 */
+ 12, /* 45 -> LPC 2: b2 */
+ 16, /* 46 -> LPC 3: b7 */
+ 19, /* 47 -> LPC 3: b4 */
+ 97, /* 48 -> LTP-LAG 2: b1 */
+ 204, /* 49 -> LTP-LAG 4: b1 */
+ 0, /* 50 -> LPC 1: b6 */
+ 13, 14, /* 51 -> LPC 2: b1..b0 */
+ 20, /* 53 -> LPC 3: b3 */
+ 24, 25, /* 54 -> LPC 4: b7..b6 */
+ 27, /* 56 -> LPC 4: b4 */
+ 154, /* 57 -> LTP-GAIN 3: b3 */
+ 206, /* 58 -> LTP-GAIN 4: b3 */
+ 195, /* 59 -> FCB-GAIN 3: b4 */
+ 247, /* 60 -> FCB-GAIN 4: b4 */
+ 89, /* 61 -> FCB-GAIN 1: b3 */
+ 141, /* 62 -> FCB-GAIN 2: b3 */
+ 196, /* 63 -> FCB-GAIN 3: b3 */
+ 248, /* 64 -> FCB-GAIN 4: b3 */
+ 252, 253, 254, 255, 256, 257, 258, 259, /* 65 -> CRC-POLY: b7..b0 */
+ 48, /* 73 -> LTP-GAIN 1: b2 */
+ 100, /* 74 -> LTP-GAIN 2: b2 */
+ 155, /* 75 -> LTP-GAIN 3: b2 */
+ 207, /* 76 -> LTP-GAIN 4: b2 */
+ 21, 22, /* 77 -> LPC 3: b2..b1 */
+ 26, /* 79 -> LPC 4: b5 */
+ 28, /* 80 -> LPC 4: b3 */
+ 51, /* 81 -> PULSE 1_1: b3 */
+ 55, /* 82 -> PULSE 1_2: b3 */
+ 59, /* 83 -> PULSE 1_3: b3 */
+ 63, /* 84 -> PULSE 1_4: b3 */
+ 67, /* 85 -> PULSE 1_5: b3 */
+ 103, /* 86 -> PULSE 2_1: b3 */
+ 107, /* 87 -> PULSE 2_2: b3 */
+ 111, /* 88 -> PULSE 2_3: b3 */
+ 115, /* 89 -> PULSE 2_4: b3 */
+ 119, /* 90 -> PULSE 2_5: b3 */
+ 158, /* 91 -> PULSE 3_1: b3 */
+ 162, /* 92 -> PULSE 3_2: b3 */
+ 166, /* 93 -> PULSE 3_3: b3 */
+ 170, /* 94 -> PULSE 3_4: b3 */
+ 174, /* 95 -> PULSE 3_5: b3 */
+ 210, /* 96 -> PULSE 4_1: b3 */
+ 214, /* 97 -> PULSE 4_2: b3 */
+ 218, /* 98 -> PULSE 4_3: b3 */
+ 222, /* 99 -> PULSE 4_4: b3 */
+ 226, /* 100 -> PULSE 4_5: b3 */
+ 90, /* 101 -> FCB-GAIN 1: b2 */
+ 142, /* 102 -> FCB-GAIN 2: b2 */
+ 197, /* 103 -> FCB-GAIN 3: b2 */
+ 249, /* 104 -> FCB-GAIN 4: b2 */
+ 49, /* 105 -> LTP-GAIN 1: b1 */
+ 101, /* 106 -> LTP-GAIN 2: b1 */
+ 156, /* 107 -> LTP-GAIN 3: b1 */
+ 208, /* 108 -> LTP-GAIN 4: b1 */
+ 29, 30, 31, /* 109 -> LPC 4: b2..b0 */
+ 32, 33, 34, 35, /* 112 -> LPC 5: b5..b2 */
+ 98, /* 116 -> LTP-LAG 2: b0 */
+ 205, /* 117 -> LTP-LAG 4: b0 */
+ 52, /* 118 -> PULSE 1_1: b2 */
+ 56, /* 119 -> PULSE 1_2: b2 */
+ 60, /* 120 -> PULSE 1_3: b2 */
+ 64, /* 121 -> PULSE 1_4: b2 */
+ 68, /* 122 -> PULSE 1_5: b2 */
+ 104, /* 123 -> PULSE 2_1: b2 */
+ 108, /* 124 -> PULSE 2_2: b2 */
+ 112, /* 125 -> PULSE 2_3: b2 */
+ 116, /* 126 -> PULSE 2_4: b2 */
+ 120, /* 127 -> PULSE 2_5: b2 */
+ 159, /* 128 -> PULSE 3_1: b2 */
+ 163, /* 129 -> PULSE 3_2: b2 */
+ 167, /* 130 -> PULSE 3_3: b2 */
+ 171, /* 131 -> PULSE 3_4: b2 */
+ 175, /* 132 -> PULSE 3_5: b2 */
+ 211, /* 133 -> PULSE 4_1: b2 */
+ 215, /* 134 -> PULSE 4_2: b2 */
+ 219, /* 135 -> PULSE 4_3: b2 */
+ 223, /* 136 -> PULSE 4_4: b2 */
+ 227, /* 137 -> PULSE 4_5: b2 */
+ 53, /* 138 -> PULSE 1_1: b1 */
+ 57, /* 139 -> PULSE 1_2: b1 */
+ 61, /* 140 -> PULSE 1_3: b1 */
+ 65, /* 141 -> PULSE 1_4: b1 */
+ 105, /* 142 -> PULSE 2_1: b1 */
+ 109, /* 143 -> PULSE 2_2: b1 */
+ 113, /* 144 -> PULSE 2_3: b1 */
+ 117, /* 145 -> PULSE 2_4: b1 */
+ 160, /* 146 -> PULSE 3_1: b1 */
+ 164, /* 147 -> PULSE 3_2: b1 */
+ 168, /* 148 -> PULSE 3_3: b1 */
+ 172, /* 149 -> PULSE 3_4: b1 */
+ 212, /* 150 -> PULSE 4_1: b1 */
+ 220, /* 151 -> PULSE 4_3: b1 */
+ 224, /* 152 -> PULSE 4_4: b1 */
+ 91, /* 153 -> FCB-GAIN 1: b1 */
+ 143, /* 154 -> FCB-GAIN 2: b1 */
+ 198, /* 155 -> FCB-GAIN 3: b1 */
+ 250, /* 156 -> FCB-GAIN 4: b1 */
+ 50, /* 157 -> LTP-GAIN 1: b0 */
+ 102, /* 158 -> LTP-GAIN 2: b0 */
+ 157, /* 159 -> LTP-GAIN 3: b0 */
+ 209, /* 160 -> LTP-GAIN 4: b0 */
+ 92, /* 161 -> FCB-GAIN 1: b0 */
+ 144, /* 162 -> FCB-GAIN 2: b0 */
+ 199, /* 163 -> FCB-GAIN 3: b0 */
+ 251, /* 164 -> FCB-GAIN 4: b0 */
+ 54, /* 165 -> PULSE 1_1: b0 */
+ 58, /* 166 -> PULSE 1_2: b0 */
+ 62, /* 167 -> PULSE 1_3: b0 */
+ 66, /* 168 -> PULSE 1_4: b0 */
+ 106, /* 169 -> PULSE 2_1: b0 */
+ 110, /* 170 -> PULSE 2_2: b0 */
+ 114, /* 171 -> PULSE 2_3: b0 */
+ 118, /* 172 -> PULSE 2_4: b0 */
+ 161, /* 173 -> PULSE 3_1: b0 */
+ 165, /* 174 -> PULSE 3_2: b0 */
+ 169, /* 175 -> PULSE 3_3: b0 */
+ 173, /* 176 -> PULSE 3_4: b0 */
+ 213, /* 177 -> PULSE 4_1: b0 */
+ 221, /* 178 -> PULSE 4_3: b0 */
+ 225, /* 179 -> PULSE 4_4: b0 */
+ 36, 37, /* 180 -> LPC 5: b1..b0 */
+ 69, /* 182 -> PULSE 1_5: b1 */
+ 71, 72, /* 183 -> PULSE 1_5: b1..b1 */
+ 121, /* 185 -> PULSE 2_5: b1 */
+ 123, 124, /* 186 -> PULSE 2_5: b1..b1 */
+ 176, /* 188 -> PULSE 3_5: b1 */
+ 178, 179, /* 189 -> PULSE 3_5: b1..b1 */
+ 228, /* 191 -> PULSE 4_5: b1 */
+ 230, 231, /* 192 -> PULSE 4_5: b1..b1 */
+ 216, 217, /* 194 -> PULSE 4_2: b1..b0 */
+ 70, /* 196 -> PULSE 1_5: b0 */
+ 122, /* 197 -> PULSE 2_5: b0 */
+ 177, /* 198 -> PULSE 3_5: b0 */
+ 229, /* 199 -> PULSE 4_5: b0 */
+ 73, /* 200 -> PULSE 1_6: b2 */
+ 76, /* 201 -> PULSE 1_7: b2 */
+ 79, /* 202 -> PULSE 1_8: b2 */
+ 82, /* 203 -> PULSE 1_9: b2 */
+ 85, /* 204 -> PULSE 1_10: b2 */
+ 125, /* 205 -> PULSE 2_6: b2 */
+ 128, /* 206 -> PULSE 2_7: b2 */
+ 131, /* 207 -> PULSE 2_8: b2 */
+ 134, /* 208 -> PULSE 2_9: b2 */
+ 137, /* 209 -> PULSE 2_10: b2 */
+ 180, /* 210 -> PULSE 3_6: b2 */
+ 183, /* 211 -> PULSE 3_7: b2 */
+ 186, /* 212 -> PULSE 3_8: b2 */
+ 189, /* 213 -> PULSE 3_9: b2 */
+ 192, /* 214 -> PULSE 3_10: b2 */
+ 232, /* 215 -> PULSE 4_6: b2 */
+ 235, /* 216 -> PULSE 4_7: b2 */
+ 238, /* 217 -> PULSE 4_8: b2 */
+ 241, /* 218 -> PULSE 4_9: b2 */
+ 244, /* 219 -> PULSE 4_10: b2 */
+ 74, /* 220 -> PULSE 1_6: b1 */
+ 77, /* 221 -> PULSE 1_7: b1 */
+ 80, /* 222 -> PULSE 1_8: b1 */
+ 83, /* 223 -> PULSE 1_9: b1 */
+ 86, /* 224 -> PULSE 1_10: b1 */
+ 126, /* 225 -> PULSE 2_6: b1 */
+ 129, /* 226 -> PULSE 2_7: b1 */
+ 132, /* 227 -> PULSE 2_8: b1 */
+ 135, /* 228 -> PULSE 2_9: b1 */
+ 138, /* 229 -> PULSE 2_10: b1 */
+ 181, /* 230 -> PULSE 3_6: b1 */
+ 184, /* 231 -> PULSE 3_7: b1 */
+ 187, /* 232 -> PULSE 3_8: b1 */
+ 190, /* 233 -> PULSE 3_9: b1 */
+ 193, /* 234 -> PULSE 3_10: b1 */
+ 233, /* 235 -> PULSE 4_6: b1 */
+ 236, /* 236 -> PULSE 4_7: b1 */
+ 239, /* 237 -> PULSE 4_8: b1 */
+ 242, /* 238 -> PULSE 4_9: b1 */
+ 245, /* 239 -> PULSE 4_10: b1 */
+ 75, /* 240 -> PULSE 1_6: b0 */
+ 78, /* 241 -> PULSE 1_7: b0 */
+ 81, /* 242 -> PULSE 1_8: b0 */
+ 84, /* 243 -> PULSE 1_9: b0 */
+ 87, /* 244 -> PULSE 1_10: b0 */
+ 127, /* 245 -> PULSE 2_6: b0 */
+ 130, /* 246 -> PULSE 2_7: b0 */
+ 133, /* 247 -> PULSE 2_8: b0 */
+ 136, /* 248 -> PULSE 2_9: b0 */
+ 139, /* 249 -> PULSE 2_10: b0 */
+ 182, /* 250 -> PULSE 3_6: b0 */
+ 185, /* 251 -> PULSE 3_7: b0 */
+ 188, /* 252 -> PULSE 3_8: b0 */
+ 191, /* 253 -> PULSE 3_9: b0 */
+ 194, /* 254 -> PULSE 3_10: b0 */
+ 234, /* 255 -> PULSE 4_6: b0 */
+ 237, /* 256 -> PULSE 4_7: b0 */
+ 240, /* 257 -> PULSE 4_8: b0 */
+ 243, /* 258 -> PULSE 4_9: b0 */
+ 246, /* 259 -> PULSE 4_10: b0 */
+};
diff --git a/lib/decoding/GSM660Tables.h b/lib/decoding/GSM660Tables.h
new file mode 100644
index 0000000..9d639fc
--- /dev/null
+++ b/lib/decoding/GSM660Tables.h
@@ -0,0 +1,31 @@
+/* EFR (GSM 06.60) importance bit ordering */
+
+/*
+ * Copyright 2010 Sylvain Munaut <tnt@246tNt.com>
+ * All Rights Reserved
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GSM660TABLES_H
+#define GSM660TABLES_H
+
+namespace GSM {
+
+/** Table #6 from GSM 05.03 */
+extern unsigned int g660BitOrder[260];
+
+}
+
+#endif /* GSM660TABLES_H */
diff --git a/lib/decoding/GSM690Tables.cpp b/lib/decoding/GSM690Tables.cpp
new file mode 100644
index 0000000..7c40a9a
--- /dev/null
+++ b/lib/decoding/GSM690Tables.cpp
@@ -0,0 +1,49 @@
+/* AMR (GSM 06.90) importance bit ordering */
+
+/*
+ * Copyright 2010 Sylvain Munaut <tnt@246tNt.com>
+ * All Rights Reserved
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "GSM690Tables.h"
+
+unsigned int GSM::g690_12_2_BitOrder[244] = {
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
+ 10, 11, 12, 13, 14, 23, 15, 16, 17, 18,
+ 19, 20, 21, 22, 24, 25, 26, 27, 28, 38,
+ 141, 39, 142, 40, 143, 41, 144, 42, 145, 43,
+ 146, 44, 147, 45, 148, 46, 149, 47, 97, 150,
+ 200, 48, 98, 151, 201, 49, 99, 152, 202, 86,
+ 136, 189, 239, 87, 137, 190, 240, 88, 138, 191,
+ 241, 91, 194, 92, 195, 93, 196, 94, 197, 95,
+ 198, 29, 30, 31, 32, 33, 34, 35, 50, 100,
+ 153, 203, 89, 139, 192, 242, 51, 101, 154, 204,
+ 55, 105, 158, 208, 90, 140, 193, 243, 59, 109,
+ 162, 212, 63, 113, 166, 216, 67, 117, 170, 220,
+ 36, 37, 54, 53, 52, 58, 57, 56, 62, 61,
+ 60, 66, 65, 64, 70, 69, 68, 104, 103, 102,
+ 108, 107, 106, 112, 111, 110, 116, 115, 114, 120,
+ 119, 118, 157, 156, 155, 161, 160, 159, 165, 164,
+ 163, 169, 168, 167, 173, 172, 171, 207, 206, 205,
+ 211, 210, 209, 215, 214, 213, 219, 218, 217, 223,
+ 222, 221, 73, 72, 71, 76, 75, 74, 79, 78,
+ 77, 82, 81, 80, 85, 84, 83, 123, 122, 121,
+ 126, 125, 124, 129, 128, 127, 132, 131, 130, 135,
+ 134, 133, 176, 175, 174, 179, 178, 177, 182, 181,
+ 180, 185, 184, 183, 188, 187, 186, 226, 225, 224,
+ 229, 228, 227, 232, 231, 230, 235, 234, 233, 238,
+ 237, 236, 96, 199,
+};
diff --git a/lib/decoding/GSM690Tables.h b/lib/decoding/GSM690Tables.h
new file mode 100644
index 0000000..6b9bb36
--- /dev/null
+++ b/lib/decoding/GSM690Tables.h
@@ -0,0 +1,31 @@
+/* AMR (GSM 06.90) importance bit ordering */
+
+/*
+ * Copyright 2010 Sylvain Munaut <tnt@246tNt.com>
+ * All Rights Reserved
+ *
+ * This program 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 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef GSM690TABLES_H
+#define GSM690TABLES_H
+
+namespace GSM {
+
+/** Table #7 from GSM 05.03 */
+extern unsigned int g690_12_2_BitOrder[244];
+
+}
+
+#endif /* GSM690TABLES_H */
diff --git a/lib/decoding/Vector.h b/lib/decoding/Vector.h
new file mode 100644
index 0000000..88ab654
--- /dev/null
+++ b/lib/decoding/Vector.h
@@ -0,0 +1,256 @@
+/**@file Simplified Vector template with aliases. */
+/*
+* Copyright 2008 Free Software Foundation, Inc.
+*
+* This software is distributed under the terms of the GNU Public License.
+* See the COPYING file in the main directory for details.
+
+ This program 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 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+*/
+
+
+
+
+#ifndef VECTOR_H
+#define VECTOR_H
+
+#include <string.h>
+#include <iostream>
+#include <assert.h>
+
+/**
+ A simplified Vector template with aliases.
+ Unlike std::vector, this class does not support dynamic resizing.
+ Unlike std::vector, this class does support "aliases" and subvectors.
+*/
+template <class T> class Vector {
+
+ // TODO -- Replace memcpy calls with for-loops.
+
+ public:
+
+ /**@name Iterator types. */
+ //@{
+ typedef T* iterator;
+ typedef const T* const_iterator;
+ //@}
+
+ protected:
+
+ T* mData; ///< allocated data block, if any
+ T* mStart; ///< start of useful data
+ T* mEnd; ///< end of useful data + 1
+
+ public:
+
+ /** Return the size of the Vector. */
+ size_t size() const
+ {
+ assert(mStart>=mData);
+ assert(mEnd>=mStart);
+ return mEnd - mStart;
+ }
+
+ /** Return size in bytes. */
+ size_t bytes() const { return size()*sizeof(T); }
+
+ /** Change the size of the Vector, discarding content. */
+ void resize(size_t newSize)
+ {
+ if (mData!=NULL) delete[] mData;
+ if (newSize==0) mData=NULL;
+ else mData = new T[newSize];
+ mStart = mData;
+ mEnd = mStart + newSize;
+ }
+
+ /** Release memory and clear pointers. */
+ void clear() { resize(0); }
+
+
+ /** Copy data from another vector. */
+ void clone(const Vector<T>& other)
+ {
+ resize(other.size());
+ memcpy(mData,other.mStart,other.bytes());
+ }
+
+
+
+
+ //@{
+
+ /** Build an empty Vector of a given size. */
+ Vector(size_t wSize=0):mData(NULL) { resize(wSize); }
+
+ /** Build a Vector by shifting the data block. */
+ Vector(Vector<T>& other)
+ :mData(other.mData),mStart(other.mStart),mEnd(other.mEnd)
+ { other.mData=NULL; }
+
+ /** Build a Vector by copying another. */
+ Vector(const Vector<T>& other):mData(NULL) { clone(other); }
+
+ /** Build a Vector with explicit values. */
+ Vector(T* wData, T* wStart, T* wEnd)
+ :mData(wData),mStart(wStart),mEnd(wEnd)
+ { }
+
+ /** Build a vector from an existing block, NOT to be deleted upon destruction. */
+ Vector(T* wStart, size_t span)
+ :mData(NULL),mStart(wStart),mEnd(wStart+span)
+ { }
+
+ /** Build a Vector by concatenation. */
+ Vector(const Vector<T>& other1, const Vector<T>& other2)
+ :mData(NULL)
+ {
+ resize(other1.size()+other2.size());
+ memcpy(mStart, other1.mStart, other1.bytes());
+ memcpy(mStart+other1.size(), other2.mStart, other2.bytes());
+ }
+
+ //@}
+
+ /** Destroy a Vector, deleting held memory. */
+ ~Vector() { clear(); }
+
+
+
+
+ //@{
+
+ /** Assign from another Vector, shifting ownership. */
+ void operator=(Vector<T>& other)
+ {
+ clear();
+ mData=other.mData;
+ mStart=other.mStart;
+ mEnd=other.mEnd;
+ other.mData=NULL;
+ }
+
+ /** Assign from another Vector, copying. */
+ void operator=(const Vector<T>& other) { clone(other); }
+
+ //@}
+
+
+ //@{
+
+ /** Return an alias to a segment of this Vector. */
+ Vector<T> segment(size_t start, size_t span)
+ {
+ T* wStart = mStart + start;
+ T* wEnd = wStart + span;
+ assert(wEnd<=mEnd);
+ return Vector<T>(NULL,wStart,wEnd);
+ }
+
+ /** Return an alias to a segment of this Vector. */
+ const Vector<T> segment(size_t start, size_t span) const
+ {
+ T* wStart = mStart + start;
+ T* wEnd = wStart + span;
+ assert(wEnd<=mEnd);
+ return Vector<T>(NULL,wStart,wEnd);
+ }
+
+ Vector<T> head(size_t span) { return segment(0,span); }
+ const Vector<T> head(size_t span) const { return segment(0,span); }
+ Vector<T> tail(size_t start) { return segment(start,size()-start); }
+ const Vector<T> tail(size_t start) const { return segment(start,size()-start); }
+
+ /**
+ Copy part of this Vector to a segment of another Vector.
+ @param other The other vector.
+ @param start The start point in the other vector.
+ @param span The number of elements to copy.
+ */
+ void copyToSegment(Vector<T>& other, size_t start, size_t span) const
+ {
+ T* base = other.mStart + start;
+ assert(base+span<=other.mEnd);
+ assert(mStart+span<=mEnd);
+ memcpy(base,mStart,span*sizeof(T));
+ }
+
+ /** Copy all of this Vector to a segment of another Vector. */
+ void copyToSegment(Vector<T>& other, size_t start=0) const { copyToSegment(other,start,size()); }
+
+ void copyTo(Vector<T>& other) const { copyToSegment(other,0,size()); }
+
+ /**
+ Copy a segment of this vector into another.
+ @param other The other vector (to copt into starting at 0.)
+ @param start The start point in this vector.
+ @param span The number of elements to copy.
+ */
+ void segmentCopyTo(Vector<T>& other, size_t start, size_t span)
+ {
+ T* base = mStart + start;
+ assert(base+span<=mEnd);
+ assert(other.mStart+span<=other.mEnd);
+ memcpy(other.mStart,base,span*sizeof(T));
+ }
+
+ void fill(const T& val)
+ {
+ T* dp=mStart;
+ while (dp<mEnd) *dp++=val;
+ }
+
+
+ //@}
+
+
+ //@{
+
+ T& operator[](size_t index)
+ {
+ assert(mStart+index<mEnd);
+ return mStart[index];
+ }
+
+ const T& operator[](size_t index) const
+ {
+ assert(mStart+index<mEnd);
+ return mStart[index];
+ }
+
+ const T* begin() const { return mStart; }
+ T* begin() { return mStart; }
+ const T* end() const { return mEnd; }
+ T* end() { return mEnd; }
+ //@}
+
+
+};
+
+
+
+
+/** Basic print operator for Vector objects. */
+template <class T>
+std::ostream& operator<<(std::ostream& os, const Vector<T>& v)
+{
+ for (unsigned i=0; i<v.size(); i++) os << v[i] << " ";
+ return os;
+}
+
+
+
+#endif
+// vim: ts=4 sw=4
diff --git a/lib/decoding/VocoderFrame.h b/lib/decoding/VocoderFrame.h
new file mode 100644
index 0000000..0c80973
--- /dev/null
+++ b/lib/decoding/VocoderFrame.h
@@ -0,0 +1,43 @@
+#ifndef _VOCODERFRAME_H
+#define _VOCODERFRAME_H
+
+#include "BitVector.h"
+//#include "GSMCommon.h"
+
+class VocoderFrame : public BitVector {
+
+ public:
+
+ VocoderFrame()
+ :BitVector(264)
+ { fillField(0,0x0d,4); }
+
+ /** Construct by unpacking a char[33]. */
+ VocoderFrame(const unsigned char *src)
+ :BitVector(264)
+ { unpack(src); }
+
+ BitVector payload() { return tail(4); }
+ const BitVector payload() const { return tail(4); }
+
+};
+
+class VocoderAMRFrame : public BitVector {
+
+ public:
+
+ VocoderAMRFrame()
+ :BitVector(244+8)
+ { fillField(0,0x3c,8); /* AMR-NB 12.2 */ }
+
+ /** Construct by unpacking a char[32]. */
+ VocoderAMRFrame(const unsigned char *src)
+ :BitVector(244+8)
+ { unpack(src); }
+
+ BitVector payload() { return tail(8); }
+ const BitVector payload() const { return tail(8); }
+
+};
+
+#endif
diff --git a/lib/decoding/tch_f_decoder_impl.cc b/lib/decoding/tch_f_decoder_impl.cc
new file mode 100644
index 0000000..75d2402
--- /dev/null
+++ b/lib/decoding/tch_f_decoder_impl.cc
@@ -0,0 +1,242 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author Piotr Krysik <ptrkrysik@gmail.com>
+ * @section LICENSE
+ *
+ * Gr-gsm 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.
+ *
+ * Gr-gsm 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 gr-gsm; 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 <grgsm/gsmtap.h>
+#include "stdio.h"
+#include "tch_f_decoder_impl.h"
+
+#define DATA_BYTES 23
+
+namespace gr {
+ namespace gsm {
+
+ tch_f_decoder::sptr
+ tch_f_decoder::make(tch_mode mode, const std::string &file)
+ {
+ return gnuradio::get_initial_sptr
+ (new tch_f_decoder_impl(mode, file));
+ }
+
+ /*
+ * Constructor
+ */
+ tch_f_decoder_impl::tch_f_decoder_impl(tch_mode mode, const std::string &file)
+ : gr::block("tch_f_decoder",
+ gr::io_signature::make(0, 0, 0),
+ gr::io_signature::make(0, 0, 0)),
+ d_tch_mode(mode),
+ d_collected_bursts_num(0)
+ {
+ d_speech_file = fopen( file.c_str(), "wb" );
+ if (d_speech_file == NULL)
+ {
+ throw std::runtime_error("TCH/F Decoder: can't open file\n");
+ }
+
+ int j, k, B;
+ for (k = 0; k < CONV_SIZE; k++)
+ {
+ B = k % 8;
+ j = 2 * ((49 * k) % 57) + ((k % 8) / 4);
+ interleave_trans[k] = B * 114 + j;
+ }
+
+ //setup input/output ports
+ message_port_register_in(pmt::mp("bursts"));
+ set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_decoder_impl::decode, this, _1));
+ message_port_register_out(pmt::mp("msgs"));
+ }
+
+ tch_f_decoder_impl::~tch_f_decoder_impl()
+ {
+ }
+
+ void tch_f_decoder_impl::decode(pmt::pmt_t msg)
+ {
+ d_bursts[d_collected_bursts_num] = msg;
+ d_collected_bursts_num++;
+
+ bool stolen = false;
+
+ if (d_collected_bursts_num == 8)
+ {
+ unsigned char iBLOCK[2*BLOCKS*iBLOCK_SIZE];
+ SoftVector mC(CONV_SIZE);
+ SoftVector mClass1_c(mC.head(378));
+ SoftVector mClass2_c(mC.segment(378, 78));
+ BitVector mTCHU(189);
+ BitVector mTCHD(260);
+ BitVector mClass1A_d(mTCHD.head(50));
+ ViterbiR2O4 mVCoder;
+
+ d_collected_bursts_num = 0;
+
+ // reorganize data
+ for (int ii = 0; ii < 8; ii++)
+ {
+ pmt::pmt_t header_plus_burst = pmt::cdr(d_bursts[ii]);
+ int8_t * burst_bits = (int8_t *)(pmt::blob_data(header_plus_burst))+sizeof(gsmtap_hdr);
+
+ for (int jj = 0; jj < 57; jj++)
+ {
+ iBLOCK[ii*114+jj] = burst_bits[jj + 3];
+ iBLOCK[ii*114+jj+57] = burst_bits[jj + 88]; //88 = 3+57+1+26+1
+ }
+
+ if ((ii <= 3 && static_cast<int>(burst_bits[87]) == 1) || (ii >= 4 && static_cast<int>(burst_bits[60]) == 1))
+ {
+ stolen = true;
+ }
+ }
+
+ // deinterleave
+ for (int k = 0; k < CONV_SIZE; k++)
+ {
+ mC[k] = iBLOCK[interleave_trans[k]];
+ }
+
+ // Decode stolen frames as FACCH/F
+ if (stolen)
+ {
+ BitVector mU(228);
+ BitVector mP(mU.segment(184,40));
+ BitVector mD(mU.head(184));
+ BitVector mDP(mU.head(224));
+ Parity mBlockCoder(0x10004820009ULL, 40, 224);
+
+ mC.decode(mVCoder, mU);
+ mP.invert();
+
+ unsigned syndrome = mBlockCoder.syndrome(mDP);
+
+ if (syndrome == 0)
+ {
+ unsigned char outmsg[27];
+ unsigned char sbuf_len=224;
+ int i, j, c, pos=0;
+ for(i = 0; i < sbuf_len; i += 8) {
+ for(j = 0, c = 0; (j < 8) && (i + j < sbuf_len); j++){
+ c |= (!!mU.bit(i + j)) << j;
+ }
+ outmsg[pos++] = c & 0xff;
+ }
+
+ pmt::pmt_t first_header_plus_burst = pmt::cdr(d_bursts[0]);
+ gsmtap_hdr * header = (gsmtap_hdr *)pmt::blob_data(first_header_plus_burst);
+ header->type = GSMTAP_TYPE_UM;
+ int8_t * header_content = (int8_t *)header;
+ int8_t header_plus_data[sizeof(gsmtap_hdr)+DATA_BYTES];
+ memcpy(header_plus_data, header_content, sizeof(gsmtap_hdr));
+ memcpy(header_plus_data+sizeof(gsmtap_hdr), outmsg, DATA_BYTES);
+
+ pmt::pmt_t msg_binary_blob = pmt::make_blob(header_plus_data,DATA_BYTES+sizeof(gsmtap_hdr));
+ pmt::pmt_t msg_out = pmt::cons(pmt::PMT_NIL, msg_binary_blob);
+
+ message_port_pub(pmt::mp("msgs"), msg_out);
+ }
+ }
+
+ mClass1_c.decode(mVCoder, mTCHU);
+ mClass2_c.sliced().copyToSegment(mTCHD, 182);
+
+ // 3.1.2.1
+ // copy class 1 bits u[] to d[]
+ for (unsigned k = 0; k <= 90; k++) {
+ mTCHD[2*k] = mTCHU[k];
+ mTCHD[2*k+1] = mTCHU[184-k];
+ }
+
+ Parity mTCHParity(0x0b, 3, 50);
+
+ // 3.1.2.1
+ // check parity of class 1A
+ unsigned sentParity = (~mTCHU.peekField(91, 3)) & 0x07;
+ //unsigned calcParity = mTCHD.head(50).parity(mTCHParity) & 0x07;
+ unsigned calcParity = mClass1A_d.parity(mTCHParity) & 0x07;
+
+ // 3.1.2.2
+ // Check the tail bits, too.
+ unsigned tail = mTCHU.peekField(185, 4);
+
+ bool good = (sentParity == calcParity) && (tail == 0);
+
+ if (good)
+ {
+ unsigned char mTCHFrame[33];
+ unsigned int mTCHFrameLength;
+
+ if (d_tch_mode == MODE_SPEECH_FR) // GSM-FR
+ {
+ // Undo Um's importance-sorted bit ordering.
+ // See GSM 05.03 3.1 and Tablee 2.
+ VocoderFrame mVFrame;
+
+ BitVector payload = mVFrame.payload();
+ mTCHD.unmap(GSM::g610BitOrder, 260, payload);
+ mVFrame.pack(mTCHFrame);
+ mTCHFrameLength = 33;
+ }
+ else if (d_tch_mode == MODE_SPEECH_EFR) // GSM-EFR / AMR 12.2
+ {
+ VocoderAMRFrame mVFrameAMR;
+
+ BitVector payload = mVFrameAMR.payload();
+ BitVector TCHW(260), EFRBits(244);
+
+ // Undo Um's EFR bit ordering.
+ mTCHD.unmap(GSM::g660BitOrder, 260, TCHW);
+
+ // Remove repeating bits and CRC to get raw EFR frame (244 bits)
+ for (unsigned k=0; k<71; k++)
+ EFRBits[k] = TCHW[k] & 1;
+
+ for (unsigned k=73; k<123; k++)
+ EFRBits[k-2] = TCHW[k] & 1;
+
+ for (unsigned k=125; k<178; k++)
+ EFRBits[k-4] = TCHW[k] & 1;
+
+ for (unsigned k=180; k<230; k++)
+ EFRBits[k-6] = TCHW[k] & 1;
+
+ for (unsigned k=232; k<252; k++)
+ EFRBits[k-8] = TCHW[k] & 1;
+
+ // Map bits as AMR 12.2k
+ EFRBits.map(GSM::g690_12_2_BitOrder, 244, payload);
+
+ // Put the whole frame (hdr + payload)
+ mVFrameAMR.pack(mTCHFrame);
+ mTCHFrameLength = 32;
+ }
+ fwrite(mTCHFrame, 1 , mTCHFrameLength, d_speech_file);
+ }
+ }
+ }
+ } /* namespace gsm */
+} /* namespace gr */
+
diff --git a/lib/decoding/tch_f_decoder_impl.h b/lib/decoding/tch_f_decoder_impl.h
new file mode 100644
index 0000000..7caa30f
--- /dev/null
+++ b/lib/decoding/tch_f_decoder_impl.h
@@ -0,0 +1,65 @@
+/* -*- c++ -*- */
+/*
+ * @file
+ * @author Piotr Krysik <ptrkrysik@gmail.com>
+ * @section LICENSE
+ *
+ * Gr-gsm 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.
+ *
+ * Gr-gsm 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 gr-gsm; see the file COPYING. If not, write to
+ * the Free Software Foundation, Inc., 51 Franklin Street,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifndef INCLUDED_GSM_TCH_F_DECODER_IMPL_H
+#define INCLUDED_GSM_TCH_F_DECODER_IMPL_H
+
+#include "VocoderFrame.h"
+#include "GSM610Tables.h"
+#include "GSM660Tables.h"
+#include "GSM690Tables.h"
+#include <grgsm/decoding/tch_f_decoder.h>
+
+
+#define DATA_BLOCK_SIZE 184
+#define PARITY_SIZE 40
+#define FLUSH_BITS_SIZE 4
+#define PARITY_OUTPUT_SIZE (DATA_BLOCK_SIZE + PARITY_SIZE + FLUSH_BITS_SIZE)
+
+#define CONV_INPUT_SIZE PARITY_OUTPUT_SIZE
+#define CONV_SIZE (2 * CONV_INPUT_SIZE)
+
+#define BLOCKS 8
+#define iBLOCK_SIZE (CONV_SIZE / BLOCKS)
+
+namespace gr {
+ namespace gsm {
+
+ class tch_f_decoder_impl : public tch_f_decoder
+ {
+ private:
+ unsigned int d_collected_bursts_num;
+ unsigned short interleave_trans[CONV_SIZE];
+ pmt::pmt_t d_bursts[8];
+ FILE * d_speech_file;
+ enum tch_mode d_tch_mode;
+ void decode(pmt::pmt_t msg);
+ public:
+ tch_f_decoder_impl(tch_mode mode, const std::string &file);
+ ~tch_f_decoder_impl();
+ };
+
+ } // namespace gsm
+} // namespace gr
+
+#endif /* INCLUDED_GSM_TCH_F_DECODER_IMPL_H */
+
diff --git a/lib/demapping/tch_f_chans_demapper_impl.cc b/lib/demapping/tch_f_chans_demapper_impl.cc
index 719429e..030a3f3 100644
--- a/lib/demapping/tch_f_chans_demapper_impl.cc
+++ b/lib/demapping/tch_f_chans_demapper_impl.cc
@@ -41,20 +41,20 @@ namespace gr {
/*
* The private constructor
- *
+ *
*/
tch_f_chans_demapper_impl::tch_f_chans_demapper_impl(unsigned int timeslot_nr)
: gr::block("tch_f_chans_demapper",
gr::io_signature::make(0, 0, 0),
gr::io_signature::make(0, 0, 0)),
d_timeslot(timeslot_nr)
-
- {
- for (int ii=0; ii<3; ii++)
+
+ {
+ for (int ii=0; ii<3; ii++)
{
d_bursts_stolen[ii] = false;
}
-
+
message_port_register_in(pmt::mp("bursts"));
set_msg_handler(pmt::mp("bursts"), boost::bind(&tch_f_chans_demapper_impl::filter_tch_chans, this, _1));
message_port_register_out(pmt::mp("tch_bursts"));
@@ -67,7 +67,7 @@ namespace gr {
tch_f_chans_demapper_impl::~tch_f_chans_demapper_impl()
{
}
-
+
void tch_f_chans_demapper_impl::filter_tch_chans(pmt::pmt_t msg)
{
pmt::pmt_t header_plus_burst = pmt::cdr(msg);
@@ -81,33 +81,33 @@ namespace gr {
if(header->timeslot == d_timeslot){
header->sub_type = GSMTAP_CHANNEL_TCH_F;
-
+
if (fn_mod13 == 12)
{
header->sub_type = GSMTAP_CHANNEL_ACCH|GSMTAP_CHANNEL_TCH_F;
-
+
// position of SACCH burst based on timeslot
// see specification gsm 05.02
uint32_t index;
bool is_sacch = false;
-
+
if (d_timeslot % 2 == 0 && fn_mod26 == 12)
{
index = (((frame_nr - 12) / 26) - (d_timeslot / 2)) % 4;
is_sacch = true;
}
- else if (d_timeslot % 2 == 1 && fn_mod26 == 25)
+ else if (d_timeslot % 2 == 1 && fn_mod26 == 25)
{
index = (((frame_nr - 25) / 26) - ((d_timeslot - 1) / 2)) % 4;
is_sacch = true;
}
-
+
if (is_sacch)
{
d_bursts_sacch[index] = msg;
d_frame_numbers_sacch[index] = frame_nr;
-
- if (index == 3)
+
+ if (index == 3)
{
//check for a situation where some bursts were lost
//in this situation frame numbers won't be consecutive
@@ -132,76 +132,43 @@ namespace gr {
}
else
{
- if (fn_mod13 <= 3)
+ if (fn_mod13 <= 3)
{
// add to b1 and b3
d_bursts[0][fn_mod13] = msg;
d_bursts[2][fn_mod13 + 4] = msg;
-
+
// set framenumber
d_frame_numbers[0][fn_mod13] = frame_nr;
d_frame_numbers[2][fn_mod13 + 4] = frame_nr;
-
- // check if stealing bits are set
- // see 4.2.5 in gsm 05.03
- if (static_cast<int>(burst_bits[60]) == 1)
- {
- d_bursts_stolen[2] = true;
- }
- if (static_cast<int>(burst_bits[87]) == 1)
- {
- d_bursts_stolen[0] = true;
- }
}
else if (fn_mod13 >= 4 && fn_mod13 <= 7)
{
// add to b1 and b2
d_bursts[0][fn_mod13] = msg;
d_bursts[1][fn_mod13 - 4] = msg;
-
+
// set framenumber
d_frame_numbers[0][fn_mod13] = frame_nr;
d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
-
- // check if stealing bits are set
- // see 4.2.5 in gsm 05.03
- if (static_cast<int>(burst_bits[60]) == 1)
- {
- d_bursts_stolen[0] = true;
- }
- if (static_cast<int>(burst_bits[87]) == 1)
- {
- d_bursts_stolen[1] = true;
- }
}
else if (fn_mod13 >= 8 && fn_mod13 <= 11)
{
// add to b2 and b3
d_bursts[1][fn_mod13 - 4] = msg;
d_bursts[2][fn_mod13 - 8] = msg;
-
+
// set framenumber
d_frame_numbers[1][fn_mod13 - 4] = frame_nr;
d_frame_numbers[2][fn_mod13 - 8] = frame_nr;
-
- // check if stealing bits are set
- // see 4.2.5 in gsm 05.03
- if (static_cast<int>(burst_bits[60]) == 1)
- {
- d_bursts_stolen[1] = true;
- }
- if (static_cast<int>(burst_bits[87]) == 1)
- {
- d_bursts_stolen[2] = true;
- }
}
// send burst 1 or burst 2 to output
if (fn_mod13 == 3 || fn_mod13 == 7 || fn_mod13 == 11)
{
int tch_burst_nr = 0;
-
- if (fn_mod13 == 11)
+
+ if (fn_mod13 == 11)
{
tch_burst_nr = 1;
}
@@ -213,34 +180,27 @@ namespace gr {
//check for a situation where some bursts were lost
//in this situation frame numbers won't be consecutive
frames_are_consecutive = true;
-
+
for(int jj=1; jj<8; jj++)
{
if((d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj-1]) != 1)
{
frames_are_consecutive = false;
// burst 3 has 1 sacch burst in between
- if (tch_burst_nr == 2 && jj == 4
- && d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj - 1] == 2)
+ if (tch_burst_nr == 2 && jj == 4
+ && d_frame_numbers[tch_burst_nr][jj] - d_frame_numbers[tch_burst_nr][jj - 1] == 2)
{
frames_are_consecutive = true;
}
}
}
-
+
if(frames_are_consecutive)
{
//send bursts to the output
for(int jj=0; jj<8; jj++)
{
- if (d_bursts_stolen[tch_burst_nr])
- {
- message_port_pub(pmt::mp("acch_bursts"), d_bursts[tch_burst_nr][jj]);
- }
- else
- {
- message_port_pub(pmt::mp("tch_bursts"), d_bursts[tch_burst_nr][jj]);
- }
+ message_port_pub(pmt::mp("tch_bursts"), d_bursts[tch_burst_nr][jj]);
}
d_bursts_stolen[tch_burst_nr] = false;
}
diff --git a/swig/grgsm_swig.i b/swig/grgsm_swig.i
index 4944c68..93a7481 100644
--- a/swig/grgsm_swig.i
+++ b/swig/grgsm_swig.i
@@ -11,6 +11,7 @@
#include "grgsm/receiver/receiver.h"
#include "grgsm/receiver/clock_offset_control.h"
#include "grgsm/decoding/control_channels_decoder.h"
+#include "grgsm/decoding/tch_f_decoder.h"
#include "grgsm/decryption/decryption.h"
#include "grgsm/demapping/universal_ctrl_chans_demapper.h"
#include "grgsm/demapping/tch_f_chans_demapper.h"
@@ -31,6 +32,8 @@ GR_SWIG_BLOCK_MAGIC2(gsm, clock_offset_control);
%include "grgsm/decoding/control_channels_decoder.h"
GR_SWIG_BLOCK_MAGIC2(gsm, control_channels_decoder);
+%include "grgsm/decoding/tch_f_decoder.h"
+GR_SWIG_BLOCK_MAGIC2(gsm, tch_f_decoder);
%include "grgsm/decryption/decryption.h"
GR_SWIG_BLOCK_MAGIC2(gsm, decryption);