diff --git a/DMRSlot.cpp b/DMRSlot.cpp index 62c4301..a719ec1 100644 --- a/DMRSlot.cpp +++ b/DMRSlot.cpp @@ -15,6 +15,7 @@ #include "DMRShortLC.h" #include "DMRFullLC.h" #include "BPTC19696.h" +#include "Trellis.h" #include "DMRSlot.h" #include "DMRCSBK.h" #include "Utils.h" @@ -313,12 +314,26 @@ void CDMRSlot::writeModem(unsigned char *data) if (m_rfState != RS_RF_DATA) return; - // Regenerate the payload if possible + // Regenerate and display the payload if (dataType == DT_RATE_12_DATA) { CBPTC19696 bptc; unsigned char payload[12U]; bptc.decode(data + 2U, payload); + LogDebug("DMR Slot %u, Rate 1/2 Data", m_slotNo); + CUtils::dump(1U, "Payload", payload, 12U); bptc.encode(payload, data + 2U); + } else if (dataType == DT_RATE_34_DATA) { + CTrellis trellis; + unsigned char payload[12U]; + trellis.decode(data + 2U, payload); + LogDebug("DMR Slot %u, Rate 3/4 Data", m_slotNo); + CUtils::dump(1U, "Payload", payload, 12U); + } else { + unsigned char payload[24U]; + ::memcpy(payload + 0U, data + 2U + 0U, 12U); + ::memcpy(payload + 12U, data + 2U + 21U, 12U); + LogDebug("DMR Slot %u, Rate 1 Data", m_slotNo); + CUtils::dump(1U, "Payload", payload, 24U); } // Regenerate the Slot Type @@ -958,12 +973,26 @@ void CDMRSlot::writeNetwork(const CDMRData& dmrData) if (m_netState != RS_NET_DATA) return; - // Regenerate the payload if possible + // Regenerate and display the payload if (dataType == DT_RATE_12_DATA) { CBPTC19696 bptc; unsigned char payload[12U]; bptc.decode(data + 2U, payload); + LogDebug("DMR Slot %u, Rate 1/2 Data", m_slotNo); + CUtils::dump(1U, "Payload", payload, 12U); bptc.encode(payload, data + 2U); + } else if (dataType == DT_RATE_34_DATA) { + CTrellis trellis; + unsigned char payload[12U]; + trellis.decode(data + 2U, payload); + LogDebug("DMR Slot %u, Rate 3/4 Data", m_slotNo); + CUtils::dump(1U, "Payload", payload, 12U); + } else { + unsigned char payload[24U]; + ::memcpy(payload + 0U, data + 2U + 0U, 12U); + ::memcpy(payload + 12U, data + 2U + 21U, 12U); + LogDebug("DMR Slot %u, Rate 1 Data", m_slotNo); + CUtils::dump(1U, "Payload", payload, 24U); } // Regenerate the Slot Type diff --git a/MMDVMHost.vcxproj b/MMDVMHost.vcxproj index 4dbaa5d..66a0715 100644 --- a/MMDVMHost.vcxproj +++ b/MMDVMHost.vcxproj @@ -186,6 +186,7 @@ + @@ -233,6 +234,7 @@ + diff --git a/MMDVMHost.vcxproj.filters b/MMDVMHost.vcxproj.filters index 2ae5e9f..2f865e0 100644 --- a/MMDVMHost.vcxproj.filters +++ b/MMDVMHost.vcxproj.filters @@ -158,6 +158,9 @@ Header Files + + Header Files + @@ -289,5 +292,8 @@ Source Files + + Source Files + \ No newline at end of file diff --git a/Makefile b/Makefile index 545a998..7331e31 100644 --- a/Makefile +++ b/Makefile @@ -7,8 +7,8 @@ LDFLAGS = -g OBJECTS = \ AMBEFEC.o BPTC19696.o Conf.o CRC.o Display.o DMRControl.o DMRCSBK.o DMRData.o DMRDataHeader.o DMREMB.o DMREmbeddedLC.o DMRFullLC.o DMRIPSC.o DMRLC.o DMRShortLC.o \ DMRSlot.o DMRSlotType.o DStarControl.o DStarHeader.o DStarNetwork.o DStarSlowData.o Golay2087.o Golay24128.o Hamming.o Log.o MMDVMHost.o Modem.o NullDisplay.o \ - QR1676.o RS129.o SerialController.o SHA256.o StopWatch.o Sync.o TFTSerial.o Timer.o UDPSocket.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o YSFParrot.o \ - YSFPayload.o + QR1676.o RS129.o SerialController.o SHA256.o StopWatch.o Sync.o TFTSerial.o Timer.o Trellis.o UDPSocket.o Utils.o YSFControl.o YSFConvolution.o YSFFICH.o \ + YSFParrot.o YSFPayload.o all: MMDVMHost diff --git a/Trellis.cpp b/Trellis.cpp new file mode 100644 index 0000000..7719d95 --- /dev/null +++ b/Trellis.cpp @@ -0,0 +1,162 @@ +/* +* Copyright (C) 2016 Jonathan Naylor, G4KLX +* Copyright (C) 2012 Ian Wraith +* +* 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; version 2 of the License. +* +* This program is distributed in the hope that it will be usefulU, +* 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. +*/ + +#include "Trellis.h" + +#include +#include +#include + +const unsigned int INTERLEAVE[] = { + 0U, 1U, 8U, 9U, 16U, 17U, 24U, 25U, 32U, 33U, 40U, 41U, 48U, 49U, 56U, 57U, 64U, 65U, 72U, 73U, 80U, 81U, 88U, 89U, 96U, 97U, + 2U, 3U, 10U, 11U, 18U, 19U, 26U, 27U, 34U, 35U, 42U, 43U, 50U, 51U, 58U, 59U, 66U, 67U, 74U, 75U, 82U, 83U, 90U, 91U, + 4U, 5U, 12U, 13U, 20U, 21U, 28U, 29U, 36U, 37U, 44U, 45U, 52U, 53U, 60U, 61U, 68U, 69U, 76U, 77U, 84U, 85U, 92U, 93U, + 6U, 7U, 14U, 15U, 22U, 23U, 30U, 31U, 38U, 39U, 46U, 47U, 54U, 55U, 62U, 63U, 70U, 71U, 78U, 79U, 86U, 87U, 94U, 95U}; + +const unsigned int STATETABLE[] = { + 0U, 8U, 4U, 12U, 2U, 10U, 6U, 14U, + 4U, 12U, 2U, 10U, 6U, 14U, 0U, 8U, + 1U, 9U, 5U, 13U, 3U, 11U, 7U, 15U, + 5U, 13U, 3U, 11U, 7U, 15U, 1U, 9U, + 3U, 11U, 7U, 15U, 1U, 9U, 5U, 13U, + 7U, 15U, 1U, 9U, 5U, 13U, 3U, 11U, + 2U, 10U, 6U, 14U, 0U, 8U, 4U, 12U, + 6U, 14U, 0U, 8U, 4U, 12U, 2U, 10U}; + +const unsigned char BIT_MASK_TABLE[] = { 0x80U, 0x40U, 0x20U, 0x10U, 0x08U, 0x04U, 0x02U, 0x01U }; + +#define WRITE_BIT(p,i,b) p[(i)>>3] = (b) ? (p[(i)>>3] | BIT_MASK_TABLE[(i)&7]) : (p[(i)>>3] & ~BIT_MASK_TABLE[(i)&7]) +#define READ_BIT(p,i) (p[(i)>>3] & BIT_MASK_TABLE[(i)&7]) + +CTrellis::CTrellis() +{ +} + +CTrellis::~CTrellis() +{ +} + +// Converts the 3/4 rate trellis encoded bits to plain binary +void CTrellis::decode(const unsigned char* in, unsigned char* out) +{ + assert(in != NULL); + assert(out != NULL); + + int8_t dibits[98U]; + extractDiBits(in, dibits); + + uint8_t cons[49U]; + constellationOut(dibits, cons); + + uint16_t tris[49U]; + bool ret = tribitExtract(cons, tris); + // If the output of tribitExtract() is false then we have an error so return + if (!ret) + return; + + binaryConvert(tris, out); +} + +// Extract and deinterleave the dibits +void CTrellis::extractDiBits(const unsigned char* in, int8_t* dibits) const +{ + for (unsigned int index = 0U; index < 98U; index++) { + unsigned int a = index * 2U; + if (a >= 98U) + a += 68U; + + bool b0 = READ_BIT(in, a) != 0x00U; + a++; + bool b1 = READ_BIT(in, a) != 0x00U; + + // Set the dibits + // 01 = +3 + // 00 = +1 + // 10 = -1 + // 11 = -3 + uint8_t dibit = 0U; + if (!b0 && b1) dibit = +3; + else if (!b0 && !b1) dibit = +1; + else if (b0 && !b1) dibit = -1; + else if (b0 && b1) dibit = -3; + // Deinterleave + unsigned int deinterleave = INTERLEAVE[index]; + dibits[deinterleave] = dibit; + } +} + +// Extract the constellation points +void CTrellis::constellationOut(const int8_t* dibits, uint8_t* cons) const +{ + unsigned int index = 0U; + for (unsigned int a = 0U; a < 98U; a += 2U, index++) { + if ((dibits[a] == +1) && (dibits[a + 1] == -1)) cons[index] = 0; + else if ((dibits[a] == -1) && (dibits[a + 1] == -1)) cons[index] = 1; + else if ((dibits[a] == +3) && (dibits[a + 1] == -3)) cons[index] = 2; + else if ((dibits[a] == -3) && (dibits[a + 1] == -3)) cons[index] = 3; + else if ((dibits[a] == -3) && (dibits[a + 1] == -1)) cons[index] = 4; + else if ((dibits[a] == +3) && (dibits[a + 1] == -1)) cons[index] = 5; + else if ((dibits[a] == -1) && (dibits[a + 1] == -3)) cons[index] = 6; + else if ((dibits[a] == +1) && (dibits[a + 1] == -3)) cons[index] = 7; + else if ((dibits[a] == -3) && (dibits[a + 1] == +3)) cons[index] = 8; + else if ((dibits[a] == +3) && (dibits[a + 1] == +3)) cons[index] = 9; + else if ((dibits[a] == -1) && (dibits[a + 1] == +1)) cons[index] = 10; + else if ((dibits[a] == +1) && (dibits[a + 1] == +1)) cons[index] = 11; + else if ((dibits[a] == +1) && (dibits[a + 1] == +3)) cons[index] = 12; + else if ((dibits[a] == -1) && (dibits[a + 1] == +3)) cons[index] = 13; + else if ((dibits[a] == +3) && (dibits[a + 1] == +1)) cons[index] = 14; + else if ((dibits[a] == -3) && (dibits[a + 1] == +1)) cons[index] = 15; + } +} + +// Extract tribits (as ints) from the constellation points +bool CTrellis::tribitExtract(const uint8_t* cons, uint16_t* tris) const +{ + unsigned int lastState = 0U; + for (unsigned int a = 0U; a < 49U; a++) { + // The lastState variable decides which row of STATETABLE we should use + unsigned int rowStart = lastState * 8; + bool match = false; + for (unsigned int b = rowStart; b < (rowStart + 8U); b++) { + // Check if this constellation point matches an element of this row of STATETABLE + if (cons[a] == STATETABLE[b]) { + // Yes it does + match = true; + lastState = b - rowStart; + tris[a] = lastState; + } + } + + // If no match found then we have a problem + if (!match) + return false; + } + + return true; +} + +// Extract the 144 binary bits from the dibits +void CTrellis::binaryConvert(const uint16_t* tris, unsigned char* out) const +{ + unsigned int a = 0U; + for (unsigned int b = 0U; b < 48U; b++) { + // Convert three bits at a time + WRITE_BIT(out, a, (tris[b] & 0x04U)); + a++; + WRITE_BIT(out, a, (tris[b] & 0x02U)); + a++; + WRITE_BIT(out, a, (tris[b] & 0x01U)); + a++; + } +} diff --git a/Trellis.h b/Trellis.h new file mode 100644 index 0000000..736b77e --- /dev/null +++ b/Trellis.h @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2016 by Jonathan Naylor G4KLX + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ + +#if !defined(Trellis_H) +#define Trellis_H + +#include + +class CTrellis +{ +public: + CTrellis(); + ~CTrellis(); + + void decode(const unsigned char* in, unsigned char* out); + +private: + void extractDiBits(const unsigned char* in, int8_t* dibits) const; + void constellationOut(const int8_t* dibits, uint8_t* cons) const; + bool tribitExtract(const uint8_t* cons, uint16_t* tris) const; + void binaryConvert(const uint16_t* tris, unsigned char* out) const; +}; + +#endif