From 83d2624cdfc1dd45ab1da02881448c5584ff6050 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sat, 29 Oct 2016 00:05:12 +0200 Subject: Modernize ReedSolomon, use vector instead of raw buffer --- src/ReedSolomon.cpp | 45 ++++++++++++++++++++++++++------------------- src/ReedSolomon.h | 30 +++++++++++++++++------------- 2 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/ReedSolomon.cpp b/src/ReedSolomon.cpp index 7508747..38d8ea8 100644 --- a/src/ReedSolomon.cpp +++ b/src/ReedSolomon.cpp @@ -1,6 +1,11 @@ /* Copyright (C) 2005, 2006, 2007, 2008, 2009 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) + + Copyright (C) 2016 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org */ /* This file is part of ODR-DabMux. @@ -20,6 +25,8 @@ */ #include "ReedSolomon.h" +#include +#include #include #include #include // For galois.h ... @@ -29,7 +36,6 @@ extern "C" { #include "fec/fec.h" } #include -#include #define SYMSIZE 8 @@ -38,8 +44,8 @@ ReedSolomon::ReedSolomon(int N, int K, bool reverse, int gfpoly, int firstRoot, { setReverse(reverse); - myN = N; - myK = K; + m_N = N; + m_K = K; const int symsize = SYMSIZE; const int nroots = N - K; // For EDI PFT, this must be 48 @@ -68,25 +74,25 @@ void ReedSolomon::setReverse(bool state) } -int ReedSolomon::encode(void* data, void* fec, unsigned long size) +int ReedSolomon::encode(void* data, void* fec, size_t size) { - unsigned char* input = reinterpret_cast(data); - unsigned char* output = reinterpret_cast(fec); + uint8_t* input = reinterpret_cast(data); + uint8_t* output = reinterpret_cast(fec); int ret = 0; if (reverse) { - unsigned char* buffer = new unsigned char[myN]; - memcpy(buffer, input, myK); - memcpy(&buffer[myK], output, myN - myK); + std::vector buffer(m_N); + + memcpy(&buffer[0], input, m_K); + memcpy(&buffer[m_K], output, m_N - m_K); - ret = decode_rs_char(rsData, buffer, nullptr, 0); + ret = decode_rs_char(rsData, &buffer[0], nullptr, 0); if ((ret != 0) && (ret != -1)) { - memcpy(input, buffer, myK); - memcpy(output, &buffer[myK], myN - myK); + memcpy(input, &buffer[0], m_K); + memcpy(output, &buffer[m_K], m_N - m_K); } - - delete[] buffer; - } else { + } + else { encode_rs_char(rsData, input, output); } @@ -94,15 +100,16 @@ int ReedSolomon::encode(void* data, void* fec, unsigned long size) } -int ReedSolomon::encode(void* data, unsigned long size) +int ReedSolomon::encode(void* data, size_t size) { - unsigned char* input = reinterpret_cast(data); + uint8_t* input = reinterpret_cast(data); int ret = 0; if (reverse) { ret = decode_rs_char(rsData, input, nullptr, 0); - } else { - encode_rs_char(rsData, input, &input[myK]); + } + else { + encode_rs_char(rsData, input, &input[m_K]); } return ret; diff --git a/src/ReedSolomon.h b/src/ReedSolomon.h index d96a522..abcef62 100644 --- a/src/ReedSolomon.h +++ b/src/ReedSolomon.h @@ -1,6 +1,11 @@ /* Copyright (C) 2005, 2006, 2007, 2008, 2009 Her Majesty the Queen in Right of Canada (Communications Research Center Canada) + + Copyright (C) 2016 + Matthias P. Braendli, matthias.braendli@mpb.li + + http://www.opendigitalradio.org */ /* This file is part of ODR-DabMux. @@ -19,34 +24,33 @@ along with ODR-DabMux. If not, see . */ -#ifndef _REEDSOLOMON -#define _REEDSOLOMON +#pragma once #ifdef HAVE_CONFIG_H # include #endif +#include class ReedSolomon { public: - ReedSolomon(int N, int K, bool reverse = false, + ReedSolomon(int N, int K, + bool reverse = false, int gfpoly = 0x11d, int firstRoot = 0, int primElem = 1); - ReedSolomon(const ReedSolomon& clone); - virtual ~ReedSolomon(); + ReedSolomon(const ReedSolomon& other) = delete; + ReedSolomon operator=(const ReedSolomon& other) = delete; + ~ReedSolomon(); void setReverse(bool state); - int encode(void* data, void* fec, unsigned long size); - int encode(void* data, unsigned long size); - -protected: - int myN; - int myK; + int encode(void* data, void* fec, size_t size); + int encode(void* data, size_t size); private: + int m_N; + int m_K; + void* rsData; bool reverse; }; - -#endif // _REEDSOLOMON -- cgit v1.2.3