Browse Source

Merge pull request #3003 from srcejon/dvb

Add DVB demodulator
chirpmodem-v8.0.0
Edouard Griffiths 1 month ago committed by GitHub
parent
commit
2e87b1a85f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 2
      .github/workflows/coverity.yml
  2. 2
      .github/workflows/linux-qt6.yml
  3. 2
      .github/workflows/linux.yml
  4. 2
      .github/workflows/sdrangel.yml
  5. 1
      .travis.yml
  6. 5
      CMakeLists.txt
  7. 1
      debian/control
  8. BIN
      doc/img/DVBDemod_charts.png
  9. BIN
      doc/img/DVBDemod_dialog_epg.png
  10. BIN
      doc/img/DVBDemod_dvbs.png
  11. BIN
      doc/img/DVBDemod_dvbt.png
  12. BIN
      doc/img/DVBDemod_history.png
  13. BIN
      doc/img/DVBDemod_nowandnext.png
  14. BIN
      doc/img/DVBDemod_onscreen_epg.png
  15. BIN
      doc/img/DVBDemod_plugin.png
  16. BIN
      doc/img/DVBDemod_table.png
  17. BIN
      doc/img/DVBDemod_video.png
  18. 12
      exports/export.h
  19. 2
      external/CMakeLists.txt
  20. 145
      modemdvb/CMakeLists.txt
  21. 169
      modemdvb/cpufeatures.cpp
  22. 58
      modemdvb/cpufeatures.h
  23. 398
      modemdvb/cwcanceller.cpp
  24. 125
      modemdvb/cwcanceller.h
  25. 256
      modemdvb/dvbs.cpp
  26. 97
      modemdvb/dvbs.h
  27. 1590
      modemdvb/dvbs2.cpp
  28. 331
      modemdvb/dvbs2.h
  29. 772
      modemdvb/dvbs2ldpctables.cpp
  30. 367
      modemdvb/dvbs2modulator.cpp
  31. 126
      modemdvb/dvbs2modulator.h
  32. 3340
      modemdvb/dvbs2pl.cpp
  33. 472
      modemdvb/dvbs2pl.h
  34. 1247
      modemdvb/dvbs2receiver.cpp
  35. 318
      modemdvb/dvbs2receiver.h
  36. 3015
      modemdvb/dvbs2xldpctables.cpp
  37. 520
      modemdvb/dvbsfec.cpp
  38. 158
      modemdvb/dvbsfec.h
  39. 950
      modemdvb/dvbsfrontend.cpp
  40. 202
      modemdvb/dvbsfrontend.h
  41. 210
      modemdvb/dvbsmodulator.cpp
  42. 80
      modemdvb/dvbsmodulator.h
  43. 846
      modemdvb/dvbsreceiver.cpp
  44. 260
      modemdvb/dvbsreceiver.h
  45. 460
      modemdvb/dvbssharedfrontend.cpp
  46. 118
      modemdvb/dvbssharedfrontend.h
  47. 419
      modemdvb/dvbssymbolrate.cpp
  48. 113
      modemdvb/dvbssymbolrate.h
  49. 351
      modemdvb/dvbt.cpp
  50. 120
      modemdvb/dvbt.h
  51. 1028
      modemdvb/dvbt2.cpp
  52. 307
      modemdvb/dvbt2.h
  53. 277
      modemdvb/dvbt2demap.cpp
  54. 45
      modemdvb/dvbt2demap.h
  55. 1711
      modemdvb/dvbt2fec.cpp
  56. 376
      modemdvb/dvbt2fec.h
  57. 70
      modemdvb/dvbt2ldpc.h
  58. 54
      modemdvb/dvbt2ldpc_avx2.cpp
  59. 85
      modemdvb/dvbt2ldpc_generic.cpp
  60. 83
      modemdvb/dvbt2ldpc_neon.cpp
  61. 54
      modemdvb/dvbt2ldpc_sse41.cpp
  62. 483
      modemdvb/dvbt2ldpckernel.h
  63. 1105
      modemdvb/dvbt2ldpctables.cpp
  64. 103
      modemdvb/dvbt2ldpctables.h
  65. 554
      modemdvb/dvbt2modulator.cpp
  66. 123
      modemdvb/dvbt2modulator.h
  67. 3063
      modemdvb/dvbt2ofdm.cpp
  68. 357
      modemdvb/dvbt2ofdm.h
  69. 1434
      modemdvb/dvbt2receiver.cpp
  70. 313
      modemdvb/dvbt2receiver.h
  71. 278
      modemdvb/dvbt2tables.cpp
  72. 43
      modemdvb/dvbt2tables.h
  73. 489
      modemdvb/dvbtfec.cpp
  74. 178
      modemdvb/dvbtfec.h
  75. 374
      modemdvb/dvbtinner.cpp
  76. 94
      modemdvb/dvbtinner.h
  77. 250
      modemdvb/dvbtmodulator.cpp
  78. 90
      modemdvb/dvbtmodulator.h
  79. 2380
      modemdvb/dvbtofdm.cpp
  80. 332
      modemdvb/dvbtofdm.h
  81. 400
      modemdvb/dvbtofdm_avx2.cpp
  82. 58
      modemdvb/dvbtofdmkernel.h
  83. 386
      modemdvb/dvbtouter.cpp
  84. 120
      modemdvb/dvbtouter.h
  85. 703
      modemdvb/dvbtreceiver.cpp
  86. 224
      modemdvb/dvbtreceiver.h
  87. 224
      modemdvb/ldpcpool.cpp
  88. 113
      modemdvb/ldpcpool.h
  89. 286
      modemdvb/resampler.cpp
  90. 87
      modemdvb/resampler.h
  91. 127
      modemdvb/resampler_avx2.cpp
  92. 83
      modemdvb/resamplerkernel.h
  93. 204
      modemdvb/samplethread.h
  94. 322
      modemdvb/viterbi27.cpp
  95. 75
      modemdvb/viterbi27.h
  96. 101
      modemdvb/viterbi27_avx2.cpp
  97. 122
      modemdvb/viterbi27_sse41.cpp
  98. 42
      modemdvb/viterbi27kernel.h
  99. 7
      plugins/channelrx/CMakeLists.txt
  100. 138
      plugins/channelrx/demoddvb/CMakeLists.txt
  101. Some files were not shown because too many files have changed in this diff Show More

2
.github/workflows/coverity.yml

@ -42,7 +42,7 @@ jobs: @@ -42,7 +42,7 @@ jobs:
libqt5serialport5-dev qtdeclarative5-dev qtpositioning5-dev qtlocation5-dev \
libqt5charts5-dev libqt5texttospeech5-dev libqt5gamepad5-dev libqt5svg5-dev libfaad-dev libflac-dev zlib1g-dev \
libusb-1.0-0-dev libhidapi-dev libboost-all-dev libasound2-dev libopencv-dev libopencv-imgcodecs-dev \
libxml2-dev bison flex ffmpeg libpostproc-dev libavcodec-dev libavformat-dev \
libxml2-dev bison flex ffmpeg libpostproc-dev libavcodec-dev libavformat-dev libavfilter-dev \
libopus-dev libcodec2-dev libairspy-dev libhackrf-dev \
libbladerf-dev libsoapysdr-dev libiio-dev libuhd-dev libhamlib-dev libunwind-dev \
libavahi-client-dev libavahi-common-dev libaio-dev \

2
.github/workflows/linux-qt6.yml

@ -45,7 +45,7 @@ jobs: @@ -45,7 +45,7 @@ jobs:
qml6-module-qt5compat-graphicaleffects qt6-speech-speechd-plugin \
libfaad-dev libflac-dev zlib1g-dev \
libusb-1.0-0-dev libhidapi-dev libboost-all-dev libasound2-dev libopencv-dev libopencv-imgcodecs-dev \
libxml2-dev bison flex ffmpeg libavcodec-dev libavformat-dev \
libxml2-dev bison flex ffmpeg libavcodec-dev libavformat-dev libavfilter-dev \
libopus-dev libcodec2-dev libairspy-dev libhackrf-dev \
libbladerf-dev libsoapysdr-dev libiio-dev libuhd-dev libhamlib-dev libunwind-dev \
libavahi-client-dev libavahi-common-dev libaio-dev \

2
.github/workflows/linux.yml

@ -36,7 +36,7 @@ jobs: @@ -36,7 +36,7 @@ jobs:
libqt5serialport5-dev qtdeclarative5-dev qtpositioning5-dev qtlocation5-dev \
libqt5charts5-dev libqt5texttospeech5-dev libqt5gamepad5-dev libqt5svg5-dev libfaad-dev libflac-dev zlib1g-dev \
libusb-1.0-0-dev libhidapi-dev libboost-all-dev libasound2-dev libopencv-dev libopencv-imgcodecs-dev \
libxml2-dev bison flex ffmpeg libpostproc-dev libavcodec-dev libavformat-dev \
libxml2-dev bison flex ffmpeg libpostproc-dev libavcodec-dev libavformat-dev libavfilter-dev \
libopus-dev libcodec2-dev libairspy-dev libhackrf-dev \
libbladerf-dev libsoapysdr-dev libiio-dev libuhd-dev libhamlib-dev libunwind-dev \
libavahi-client-dev libavahi-common-dev libaio-dev \

2
.github/workflows/sdrangel.yml

@ -163,7 +163,7 @@ jobs: @@ -163,7 +163,7 @@ jobs:
libqt5serialport5-dev qtdeclarative5-dev qtpositioning5-dev qtlocation5-dev \
libqt5charts5-dev libqt5texttospeech5-dev libqt5gamepad5-dev libqt5svg5-dev libfaad-dev libflac-dev zlib1g-dev \
libusb-1.0-0-dev libhidapi-dev libboost-all-dev libasound2-dev libopencv-dev libopencv-imgcodecs-dev \
libxml2-dev bison flex ffmpeg libpostproc-dev libavcodec-dev libavformat-dev \
libxml2-dev bison flex ffmpeg libpostproc-dev libavcodec-dev libavformat-dev libavfilter-dev \
libopus-dev libcodec2-dev libairspy-dev libhackrf-dev \
libbladerf-dev libsoapysdr-dev libiio-dev libuhd-dev libhamlib-dev libunwind-dev \
libavahi-client-dev libavahi-common-dev libaio-dev \

1
.travis.yml

@ -54,6 +54,7 @@ addons: @@ -54,6 +54,7 @@ addons:
- ffmpeg
- libavcodec-dev
- libavformat-dev
- libavfilter-dev
- libopus-dev
- libcodec2-dev
- libairspy-dev

5
CMakeLists.txt

@ -137,6 +137,7 @@ option(ENABLE_CHANNELRX "Enable channelrx plugins" ON) @@ -137,6 +137,7 @@ option(ENABLE_CHANNELRX "Enable channelrx plugins" ON)
option(ENABLE_CHANNELRX_DEMODADSB "Enable channelrx demodadsb plugin" ON)
option(ENABLE_CHANNELRX_DEMODAM "Enable channelrx demodam plugin" ON)
option(ENABLE_CHANNELRX_DEMODDATV "Enable channelrx demoddatv plugin" ON)
option(ENABLE_CHANNELRX_DEMODDVB "Enable channelrx demoddvb plugin" ON)
option(ENABLE_CHANNELRX_RADIOCLOCK "Enable channelrx radioclock plugin" ON)
option(ENABLE_CHANNELRX_RADIOASTRONOMY "Enable channelrx radioastronomy plugin" ON)
option(ENABLE_CHANNELRX_DEMODRADIOSONDE "Enable channelrx demodradiosonde plugin" ON)
@ -492,6 +493,8 @@ elseif (WIN32) @@ -492,6 +493,8 @@ elseif (WIN32)
set(SWRESAMPLE_LIBRARIES "${EXTERNAL_LIBRARY_FOLDER}/ffmpeg/lib/swresample.lib" CACHE INTERNAL "")
set(SWSCALE_INCLUDE_DIRS "${EXTERNAL_LIBRARY_FOLDER}/ffmpeg/include" CACHE INTERNAL "")
set(SWSCALE_LIBRARIES "${EXTERNAL_LIBRARY_FOLDER}/ffmpeg/lib/swscale.lib" CACHE INTERNAL "")
set(AVFILTER_INCLUDE_DIRS "${EXTERNAL_LIBRARY_FOLDER}/ffmpeg/include" CACHE INTERNAL "")
set(AVFILTER_LIBRARIES "${EXTERNAL_LIBRARY_FOLDER}/ffmpeg/lib/avfilter.lib" CACHE INTERNAL "")
# LimeSuite FX3 support
set(FX3SDK_DIR "${EXTERNAL_LIBRARY_FOLDER}/FX3SDK" CACHE INTERNAL "")
@ -718,6 +721,7 @@ elseif(ANDROID) @@ -718,6 +721,7 @@ elseif(ANDROID)
${CMAKE_BINARY_DIR}/lib/libmetisdevice.so
${CMAKE_BINARY_DIR}/lib/libmodemm17.so
${CMAKE_BINARY_DIR}/lib/libmodemmeshtastic.so
${CMAKE_BINARY_DIR}/lib/libmodemdvb.so
${CMAKE_BINARY_DIR}/lib/libwdsp.so
CACHE INTERNAL ""
)
@ -1100,6 +1104,7 @@ add_subdirectory(devices) @@ -1100,6 +1104,7 @@ add_subdirectory(devices)
add_subdirectory(modemm17)
add_subdirectory(modemmeshtastic)
add_subdirectory(modemmeshcore)
add_subdirectory(modemdvb)
if (FFTW3F_FOUND)
add_subdirectory(ft8)

1
debian/control vendored

@ -50,6 +50,7 @@ Build-Depends: debhelper (>= 9), @@ -50,6 +50,7 @@ Build-Depends: debhelper (>= 9),
libflac-dev,
libavcodec-dev,
libavformat-dev,
libavfilter-dev,
libopus-dev,
libairspy-dev,
libhackrf-dev,

BIN
doc/img/DVBDemod_charts.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 44 KiB

BIN
doc/img/DVBDemod_dialog_epg.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

BIN
doc/img/DVBDemod_dvbs.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 24 KiB

BIN
doc/img/DVBDemod_dvbt.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

BIN
doc/img/DVBDemod_history.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.4 KiB

BIN
doc/img/DVBDemod_nowandnext.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 78 KiB

BIN
doc/img/DVBDemod_onscreen_epg.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 437 KiB

BIN
doc/img/DVBDemod_plugin.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 742 KiB

BIN
doc/img/DVBDemod_table.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

BIN
doc/img/DVBDemod_video.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

12
exports/export.h

@ -167,6 +167,18 @@ @@ -167,6 +167,18 @@
# define MODEMMESHTASTIC_API
#endif
/* the 'MODEMDVB_API' controls the import/export of 'modemdvb' symbols
*/
#if !defined(sdrangel_STATIC)
# ifdef modemdvb_EXPORTS
# define MODEMDVB_API __SDR_EXPORT
# else
# define MODEMDVB_API __SDR_IMPORT
# endif
#else
# define MODEMDVB_API
#endif
/* the 'MODEMMESHCORE_API' controls the import/export of 'modemmeshcore' symbols
*/
#if !defined(sdrangel_STATIC)

2
external/CMakeLists.txt vendored

@ -558,6 +558,8 @@ if (NOT FFMPEG_FOUND AND NOT USE_PRECOMPILED_LIBS) @@ -558,6 +558,8 @@ if (NOT FFMPEG_FOUND AND NOT USE_PRECOMPILED_LIBS)
set(AVUTIL_LIBRARIES "${install_dir}/lib/libavutil${CMAKE_SHARED_LIBRARY_SUFFIX}" CACHE INTERNAL "")
set(SWRESAMPLE_LIBRARIES "${install_dir}/lib/libswresample${CMAKE_SHARED_LIBRARY_SUFFIX}" CACHE INTERNAL "")
set(SWSCALE_LIBRARIES "${install_dir}/lib/libswscale${CMAKE_SHARED_LIBRARY_SUFFIX}" CACHE INTERNAL "")
set(AVFILTER_INCLUDE_DIRS "${install_dir}/include" CACHE INTERNAL "")
set(AVFILTER_LIBRARIES "${install_dir}/lib/libavfilter${CMAKE_SHARED_LIBRARY_SUFFIX}" CACHE INTERNAL "")
if (APPLE)
set(MACOS_EXTERNAL_LIBS_FIXUP "${MACOS_EXTERNAL_LIBS_FIXUP};${install_dir}/lib")
endif ()

145
modemdvb/CMakeLists.txt

@ -0,0 +1,145 @@ @@ -0,0 +1,145 @@
project(modemdvb)
set(modemdvb_SOURCES
dvbt.cpp
cpufeatures.cpp
dvbt2.cpp
dvbt2demap.cpp
dvbt2fec.cpp
dvbt2ldpc_generic.cpp
dvbt2ldpctables.cpp
dvbt2modulator.cpp
dvbt2ofdm.cpp
dvbt2receiver.cpp
dvbt2tables.cpp
dvbs2ldpctables.cpp
dvbs2xldpctables.cpp
dvbs.cpp
dvbs2.cpp
dvbs2modulator.cpp
dvbs2pl.cpp
dvbs2receiver.cpp
ldpcpool.cpp
dvbsfec.cpp
dvbsfrontend.cpp
dvbssharedfrontend.cpp
dvbssymbolrate.cpp
dvbsmodulator.cpp
dvbsreceiver.cpp
dvbtfec.cpp
dvbtinner.cpp
dvbtmodulator.cpp
dvbtofdm.cpp
cwcanceller.cpp
dvbtouter.cpp
dvbtreceiver.cpp
viterbi27.cpp
resampler.cpp
)
set(modemdvb_HEADERS
dvbs.h
dvbs2.h
dvbs2modulator.h
dvbs2pl.h
dvbs2receiver.h
ldpcpool.h
dvbsfec.h
dvbsfrontend.h
dvbssharedfrontend.h
dvbssymbolrate.h
dvbsmodulator.h
dvbsreceiver.h
dvbt.h
cpufeatures.h
dvbt2.h
dvbt2demap.h
dvbt2fec.h
dvbt2modulator.h
dvbt2ofdm.h
dvbt2receiver.h
dvbt2tables.h
dvbt2ldpc.h
dvbt2ldpckernel.h
dvbt2ldpctables.h
dvbtfec.h
dvbtinner.h
dvbtmodulator.h
dvbtofdm.h
cwcanceller.h
dvbtouter.h
dvbtreceiver.h
viterbi27.h
viterbi27kernel.h
resamplerkernel.h
samplethread.h
resampler.h
)
include_directories(
${CMAKE_SOURCE_DIR}/exports
${CMAKE_SOURCE_DIR}/sdrbase
)
# The DVB-T2 LDPC decoder, built for SSE4.1 and AVX2, chosen at run time for the CPU, or for NEON on ARM (the file
# is empty if the compiler isn't targeting NEON). dvbt2ldpc_generic.cpp is for other CPUs. Likewise the resampler
# and Viterbi kernels (NEON in resampler.cpp and viterbi27.cpp). The architecture is the target's, as the compiler
# says (DetectArchitecture.cmake), not the host's (CMAKE_SYSTEM_PROCESSOR), which differs when cross compiling, as
# with Visual Studio's -A ARM64 on x64
if(ARCHITECTURE_x86_64 OR ARCHITECTURE_x86)
set(modemdvb_SSE41_SOURCES dvbt2ldpc_sse41.cpp viterbi27_sse41.cpp)
set(modemdvb_AVX2_SOURCES dvbt2ldpc_avx2.cpp viterbi27_avx2.cpp)
set(modemdvb_AVX2_FMA_SOURCES resampler_avx2.cpp dvbtofdm_avx2.cpp)
list(APPEND modemdvb_SOURCES ${modemdvb_SSE41_SOURCES} ${modemdvb_AVX2_SOURCES} ${modemdvb_AVX2_FMA_SOURCES})
# These files are compiled for instruction sets the CPU mightn't have, so only use raw buffers and their own
# (internal) functions: any library code they instantiated (std::vector's...) would be compiled for them too, and
# the linker keeps one copy of each for every caller, so code meant to run anywhere could crash
if(MSVC AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# MSVC compiles the AVX2 and FMA intrinsics without /arch:AVX2. Without it, PSB1's decoding is bit-identical
# and the kernels within a few percent
else()
if(MSVC)
# clang-cl: its intrinsics need the instruction sets enabled, with the GCC style options
set(modemdvb_SSE41_OPTIONS "/clang:-msse4.1")
set(modemdvb_AVX2_OPTIONS "/clang:-mavx2")
set(modemdvb_AVX2_FMA_OPTIONS "/clang:-mavx2;/clang:-mfma")
else()
set(modemdvb_SSE41_OPTIONS "-msse4.1")
set(modemdvb_AVX2_OPTIONS "-mavx2")
set(modemdvb_AVX2_FMA_OPTIONS "-mavx2;-mfma")
if(MINGW AND CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
# MinGW's GCC assumes the stack is 32-byte aligned for AVX spills, which Windows doesn't guarantee
# (GCC bug 54412): the assembler can make its aligned moves unaligned ones (binutils 2.38)
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag("-Wa,-muse-unaligned-vector-move" MODEMDVB_UNALIGNED_VECTOR_MOVE)
if(MODEMDVB_UNALIGNED_VECTOR_MOVE)
list(APPEND modemdvb_AVX2_OPTIONS "-Wa,-muse-unaligned-vector-move")
list(APPEND modemdvb_AVX2_FMA_OPTIONS "-Wa,-muse-unaligned-vector-move")
else()
message(WARNING "modemdvb: the assembler lacks -muse-unaligned-vector-move, so the AVX2 code may crash (GCC bug 54412)")
endif()
endif()
endif()
set_source_files_properties(${modemdvb_SSE41_SOURCES} PROPERTIES COMPILE_OPTIONS "${modemdvb_SSE41_OPTIONS}")
set_source_files_properties(${modemdvb_AVX2_SOURCES} PROPERTIES COMPILE_OPTIONS "${modemdvb_AVX2_OPTIONS}")
set_source_files_properties(${modemdvb_AVX2_FMA_SOURCES} PROPERTIES COMPILE_OPTIONS "${modemdvb_AVX2_FMA_OPTIONS}")
endif()
set(modemdvb_LDPC_X86 ON)
elseif(ARCHITECTURE_ARM64 OR ARCHITECTURE_ARM)
list(APPEND modemdvb_SOURCES dvbt2ldpc_neon.cpp)
endif()
add_library(modemdvb
${modemdvb_SOURCES}
)
if(modemdvb_LDPC_X86)
target_compile_definitions(modemdvb PRIVATE DVBT2_LDPC_X86 MODEMDVB_X86)
endif()
target_link_libraries(modemdvb
Qt::Core
sdrbase
)
install(TARGETS modemdvb DESTINATION ${INSTALL_LIB_DIR})

169
modemdvb/cpufeatures.cpp

@ -0,0 +1,169 @@ @@ -0,0 +1,169 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <cstdlib>
#include "cpufeatures.h"
#if defined(MODEMDVB_X86) && defined(_MSC_VER)
#include <intrin.h>
#elif defined(MODEMDVB_X86)
#include <cpuid.h>
#endif
namespace modemdvb {
#if defined(MODEMDVB_X86)
#if defined(_MSC_VER)
// clang-cl's _xgetbv needs the XSAVE instructions enabled, but only for this, so not for the whole file
#if defined(__clang__)
__attribute__((target("xsave")))
#endif
unsigned long long CPUFeatures::xcr0()
{
return _xgetbv(0);
}
#endif
bool CPUFeatures::detectAVX2()
{
if (getenv("DVB_NO_AVX2")) {
return false;
}
#if defined(_MSC_VER)
int r[4];
__cpuid(r, 0);
if (r[0] < 7) {
return false;
}
__cpuid(r, 1);
const bool fma = (r[2] & (1 << 12)) != 0;
const bool osxsave = (r[2] & (1 << 27)) != 0;
const bool avx = (r[2] & (1 << 28)) != 0;
if (!fma || !osxsave || !avx || ((xcr0() & 6) != 6)) { // OS saves the YMM registers
return false;
}
__cpuidex(r, 7, 0);
return (r[1] & (1 << 5)) != 0;
#else
return __builtin_cpu_supports("avx2") && __builtin_cpu_supports("fma");
#endif
}
bool CPUFeatures::detectSSE41()
{
#if defined(_MSC_VER)
int r[4];
__cpuid(r, 1);
return (r[2] & (1 << 19)) != 0;
#else
return __builtin_cpu_supports("sse4.1");
#endif
}
bool cpuHasAVX2()
{
static const bool avx2 = CPUFeatures::detectAVX2();
return avx2;
}
bool cpuHasSSE41()
{
static const bool sse41 = CPUFeatures::detectSSE41();
return sse41;
}
void CPUFeatures::cpuid(int leaf, int sub, unsigned int r[4])
{
#if defined(_MSC_VER)
int v[4];
__cpuidex(v, leaf, sub);
for (int i = 0; i < 4; i++) {
r[i] = (unsigned int) v[i];
}
#else
__cpuid_count(leaf, sub, r[0], r[1], r[2], r[3]);
#endif
}
// CPUID.07H:EDX bit 15, and leaf 1AH available
bool CPUFeatures::detectHybrid()
{
unsigned int r[4];
cpuid(0, 0, r);
if (r[0] < 0x1a) {
return false;
}
cpuid(7, 0, r);
return (r[3] & (1u << 15)) != 0;
}
bool onEfficiencyCore()
{
static const bool hybrid = CPUFeatures::detectHybrid();
if (!hybrid) {
return false;
}
// CPUID can be slow (in a VM, the hypervisor handles it), and this is asked for each block, so the thread's last
// answer is kept for a few calls. Should the OS move the thread meanwhile, it only runs slower code till then
static const unsigned int refresh = 32;
thread_local unsigned int calls = 0;
thread_local bool efficiency = false;
if (calls++ % refresh == 0)
{
// CPUID.1AH:EAX bits 31..24: the type of the core it runs on, 0x20 for an efficiency (Atom) core
unsigned int r[4];
CPUFeatures::cpuid(0x1a, 0, r);
efficiency = (r[0] >> 24) == 0x20;
}
return efficiency;
}
#else
bool cpuHasAVX2()
{
return false;
}
bool cpuHasSSE41()
{
return false;
}
bool onEfficiencyCore()
{
return false;
}
#endif
} // namespace modemdvb

58
modemdvb/cpufeatures.h

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_CPUFEATURES_H
#define MODEMDVB_CPUFEATURES_H
// Internal to modemdvb: which of the instruction sets code is built for at run time can be used. Setting DVB_NO_AVX2
// in the environment disables AVX2, to compare it with the SSE code it replaces
namespace modemdvb {
bool cpuHasAVX2(); //!< And FMA, which every AVX2 CPU also has
bool cpuHasSSE41();
//! On a hybrid CPU (Intel's performance and efficiency cores), if the calling thread is on an efficiency core now.
//! Their 128-bit vector units split 256-bit instructions and are slow at moving data across their halves, so some
//! AVX2 code is slower there than the SSE code. False if the CPU isn't hybrid
bool onEfficiencyCore();
//! The detection behind those
class CPUFeatures
{
private:
friend bool cpuHasAVX2();
friend bool cpuHasSSE41();
friend bool onEfficiencyCore();
#if defined(MODEMDVB_X86)
#if defined(_MSC_VER)
#if defined(__clang__)
__attribute__((target("xsave"))) // As its definition
#endif
static unsigned long long xcr0();
#endif
static bool detectAVX2();
static bool detectSSE41();
static void cpuid(int leaf, int sub, unsigned int r[4]);
static bool detectHybrid();
#endif
};
} // namespace modemdvb
#endif // MODEMDVB_CPUFEATURES_H

398
modemdvb/cwcanceller.cpp

@ -0,0 +1,398 @@ @@ -0,0 +1,398 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include "cwcanceller.h"
namespace dvbt {
const double CWCanceller::m_twoPi = 6.283185307179586;
const double CWCanceller::m_thresholdDb = 12.0;
const double CWCanceller::m_poleRadius = 0.999;
CWCanceller::CWCanceller() :
m_enabled(true),
m_sampleRate(1.0),
m_bandwidth(1.0)
{
configure(9142857.0, 7.61e6);
}
void CWCanceller::configure(double sampleRate, double bandwidth)
{
const int n = m_fftSize;
m_sampleRate = sampleRate;
m_bandwidth = bandwidth;
// Two blocks back to back (their phase difference refines the frequency) every 20 ms
m_interval = std::max(4 * n, (int) (sampleRate * 0.02));
m_window.resize(n);
for (int i = 0; i < n; i++) {
m_window[i] = 0.5 - 0.5 * std::cos(m_twoPi * (i + 0.5) / n);
}
m_twiddles.resize(n / 2);
for (int i = 0; i < n / 2; i++) {
m_twiddles[i] = std::polar(1.0, -m_twoPi * i / n);
}
m_bitReverse.resize(n);
int bits = 0;
while ((1 << bits) < n) {
bits++;
}
for (int i = 0; i < n; i++)
{
int r = 0;
for (int b = 0; b < bits; b++) {
r |= ((i >> b) & 1) << (bits - 1 - b);
}
m_bitReverse[i] = r;
}
// The bins within the band
m_band.clear();
for (int k = 0; k < n; k++)
{
const int signedK = k < n / 2 ? k : k - n;
if (std::abs(signedK * m_sampleRate / n) < m_bandwidth / 2.0) {
m_band.push_back(k);
}
}
reset();
}
void CWCanceller::reset()
{
m_untilNext = 0;
m_block.assign(2 * m_fftSize, Cf(0.0f, 0.0f));
m_blockFill = 0;
m_power.assign(m_fftSize, 0.0);
m_blockCount = 0;
m_candidates.clear();
m_notches.clear();
}
void CWCanceller::setEnabled(bool enabled)
{
m_enabled = enabled;
if (!enabled) {
reset();
}
}
const CWCanceller::Cf *CWCanceller::process(const Cf *samples, int nbSamples, std::vector<Cf>& out)
{
if (!m_enabled) {
return samples;
}
// Measure the input (not what's been notched, so a notch stays while its interferer does)
const int pair = 2 * m_fftSize;
int i = 0;
while (i < nbSamples)
{
if ((m_blockFill > 0) || (m_untilNext <= 0))
{
const int n = std::min(pair - m_blockFill, nbSamples - i);
std::copy(samples + i, samples + i + n, m_block.begin() + m_blockFill);
m_blockFill += n;
i += n;
if (m_blockFill == pair)
{
measure(m_block.data());
m_blockFill = 0;
m_untilNext = m_interval - pair;
}
}
else
{
const int skip = std::min(m_untilNext, nbSamples - i);
m_untilNext -= skip;
i += skip;
}
}
if (m_notches.empty()) {
return samples;
}
// Each notch: w = x + p w', y = w - z w'
out.assign(samples, samples + nbSamples);
float *y = reinterpret_cast<float *>(out.data());
for (Notch& notch : m_notches)
{
const float zr = notch.m_zr, zi = notch.m_zi, pr = notch.m_pr, pi = notch.m_pi;
float wr = notch.m_wr, wi = notch.m_wi;
for (int k = 0; k < nbSamples; k++)
{
const float xr = y[2 * k], xi = y[2 * k + 1];
const float nr = xr + pr * wr - pi * wi;
const float ni = xi + pr * wi + pi * wr;
y[2 * k] = nr - (zr * wr - zi * wi);
y[2 * k + 1] = ni - (zr * wi + zi * wr);
wr = nr;
wi = ni;
}
notch.m_wr = wr;
notch.m_wi = wi;
}
return out.data();
}
// The power spectrum of both blocks, averaged until there are enough, and the second block's spectrum relative to
// the first's, for the frequencies of the peaks
void CWCanceller::measure(const Cf *block)
{
const int n = m_fftSize;
std::vector<std::complex<double>> *spectra = m_spectra;
for (int b = 0; b < 2; b++)
{
std::vector<std::complex<double>>& fft = spectra[b];
fft.resize(n);
for (int i = 0; i < n; i++) {
fft[m_bitReverse[i]] = std::complex<double>(block[b * n + i]) * m_window[i];
}
for (int size = 2; size <= n; size *= 2)
{
const int half = size / 2;
const int stride = n / size;
for (int start = 0; start < n; start += size)
{
for (int j = 0; j < half; j++)
{
const std::complex<double> t = fft[start + j + half] * m_twiddles[j * stride];
fft[start + j + half] = fft[start + j] - t;
fft[start + j] += t;
}
}
}
for (int k = 0; k < n; k++) {
m_power[k] += std::norm(fft[k]);
}
}
m_blockCount += 2;
// Each pair until there's a notch, so one is soon in place (within about 40 ms), before the receiver locks, as
// starting one afterwards changes the channel the receiver is following
if ((m_blockCount < m_blocks) && !m_notches.empty()) {
return;
}
// Peaks well above the signal's median level in the band, strongest first, each refined: roughly by the shape of
// the window's main lobe, then from the phase advance of a block (a block's length at the frequency)
const std::vector<int>& band = m_band;
std::vector<double>& levels = m_levels;
levels.clear();
for (int k : band) {
levels.push_back(m_power[k]);
}
std::nth_element(levels.begin(), levels.begin() + levels.size() / 2, levels.end());
const double median = levels.empty() ? 0.0 : levels[levels.size() / 2];
const double threshold = median * std::pow(10.0, m_thresholdDb / 10.0);
std::vector<std::pair<double, int>> peaks; // Power, bin
for (int k : band)
{
const double p = m_power[k];
if ((median > 0.0) && (p > threshold) && (p >= m_power[(k + n - 1) % n]) && (p >= m_power[(k + 1) % n])) {
peaks.push_back({p, k});
}
}
std::sort(peaks.begin(), peaks.end(), [](const std::pair<double, int>& a, const std::pair<double, int>& b) { return a.first > b.first; });
std::vector<std::pair<double, double>> detected; // Frequency (cycles per sample), level (dB)
for (const auto& peak : peaks)
{
const int k = peak.second;
const int signedK = k < n / 2 ? k : k - n;
bool near = false;
for (const auto& d : detected)
{
if (std::abs(d.first * n - signedK) < 2 * m_matchBins) {
near = true; // A sidelobe of a stronger one
}
}
if (near || ((int) detected.size() >= m_maxNotches)) {
continue;
}
const double lm = std::log(m_power[(k + n - 1) % n]), l0 = std::log(m_power[k]), lp = std::log(m_power[(k + 1) % n]);
const double denominator = lm - 2.0 * l0 + lp;
const double delta = denominator != 0.0 ? 0.5 * (lm - lp) / denominator : 0.0;
const double coarse = signedK + std::max(-0.5, std::min(0.5, delta)); // Bins
// The second block is n samples on, so the tone has advanced by 2 pi f n: whole cycles from the coarse
// estimate, and the fraction from the phase
const double advance = std::arg(spectra[1][k] * std::conj(spectra[0][k])) / m_twoPi;
const double fine = advance + std::round(coarse - advance);
detected.push_back({fine / n, 10.0 * std::log10(peak.first / median)});
}
// Follow the notches, start new ones for interferers seen twice in a row, and stop those that have gone
std::vector<bool> used(detected.size(), false);
for (Notch& notch : m_notches)
{
notch.m_missed++;
for (size_t d = 0; d < detected.size(); d++)
{
if (!used[d] && (std::abs(detected[d].first - notch.m_frequency) * n < m_matchBins))
{
used[d] = true;
notch.m_missed = 0;
notch.m_level = detected[d].second;
setFrequency(notch, detected[d].first);
}
}
}
m_notches.erase(std::remove_if(m_notches.begin(), m_notches.end(), [](const Notch& notch) {
return notch.m_missed >= m_missesToRemove;
}), m_notches.end());
std::vector<double> candidates;
for (size_t d = 0; d < detected.size(); d++)
{
if (used[d]) {
continue;
}
const bool seen = std::any_of(m_candidates.begin(), m_candidates.end(), [&](double f) {
return std::abs(f - detected[d].first) * n < m_matchBins;
});
if (seen && ((int) m_notches.size() < m_maxNotches))
{
Notch notch{};
notch.m_level = detected[d].second;
setFrequency(notch, detected[d].first);
m_notches.push_back(notch);
}
else
{
candidates.push_back(detected[d].first);
}
}
m_candidates = candidates;
std::fill(m_power.begin(), m_power.end(), 0.0);
m_blockCount = 0;
}
void NotchedCarriers::setNotches(const std::vector<double>& frequencies, double inputRate)
{
m_notches = frequencies;
m_inputRate = inputRate;
if (m_notches.empty())
{
m_carriers.clear();
m_factors.clear();
m_erased.clear();
}
}
void NotchedCarriers::prepare(int kMax, double spacing, double offset)
{
for (int k : m_carriers)
{
if (k < (int) m_erased.size()) {
m_erased[k] = 0;
}
}
m_carriers.clear();
m_factors.clear();
m_erased.resize(kMax + 1, 0);
const int width = (int) std::ceil(50e3 / spacing);
std::vector<std::complex<double>> response;
for (int k = 0; k <= kMax; k++)
{
const double f = (k - kMax / 2) * spacing + offset;
std::complex<double> h(1.0, 0.0);
bool near = false;
for (double notch : m_notches)
{
if (std::abs(f - notch) <= width * spacing)
{
// H = (1 - z e^-jw) / (1 - r z e^-jw), with z the zero, at the carrier's frequency w
const std::complex<double> e = std::polar(1.0, CWCanceller::m_twoPi * (notch - f) / m_inputRate);
h *= (1.0 - e) / (1.0 - CWCanceller::m_poleRadius * e);
near = true;
}
}
if (near)
{
const double h2 = std::norm(h);
m_carriers.push_back(k);
m_factors.push_back(Cf(std::conj(h) / std::max(h2, 0.09)));
m_erased[k] = h2 < 0.25;
}
}
}
void NotchedCarriers::compensate(Cf *y) const
{
for (size_t i = 0; i < m_carriers.size(); i++) {
y[m_carriers[i]] *= m_factors[i];
}
}
void CWCanceller::setFrequency(Notch& notch, double frequency)
{
notch.m_frequency = frequency;
notch.m_zr = (float) std::cos(m_twoPi * frequency);
notch.m_zi = (float) std::sin(m_twoPi * frequency);
notch.m_pr = (float) (m_poleRadius * notch.m_zr);
notch.m_pi = (float) (m_poleRadius * notch.m_zi);
}
} // namespace dvbt

125
modemdvb/cwcanceller.h

@ -0,0 +1,125 @@ @@ -0,0 +1,125 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_CWCANCELLER_H
#define MODEMDVB_CWCANCELLER_H
#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdint>
#include <vector>
#include "export.h"
namespace dvbt {
// Notches out strong narrowband interferers (CW), before the FFT. An OFDM receiver's FFT leaks a tone into
// carriers far from it (falling only 20 dB a decade), so one much stronger than the signal spoils most of the
// carriers, which weighting them by their noise can't help. The input's spectrum is measured now and then (a
// windowed FFT, averaged over a few blocks), and a narrow peak well above the signal's level gets a complex IIR
// notch (a zero on the unit circle and a pole just inside it), a few kHz wide, which follows its frequency
class MODEMDVB_API CWCanceller
{
public:
typedef std::complex<float> Cf;
struct Notch
{
double m_frequency; //!< Cycles per sample
double m_level; //!< dB above the signal's level, in the measurement
int m_missed; //!< Measurements since it was last seen
float m_zr, m_zi; //!< Zero, e^jw
float m_pr, m_pi; //!< Pole, r e^jw
float m_wr, m_wi; //!< State
};
CWCanceller();
//! Look within +/- bandwidth/2 of the centre, of samples at sampleRate
void configure(double sampleRate, double bandwidth);
void reset();
void setEnabled(bool enabled);
//! The samples to use: samples itself if no notch is active, else out's data (out is resized to fit)
const Cf *process(const Cf *samples, int nbSamples, std::vector<Cf>& out);
const std::vector<Notch>& getNotches() const { return m_notches; }
private:
friend class NotchedCarriers; //!< For m_twoPi and m_poleRadius
static constexpr int m_fftSize = 4096;
static constexpr int m_blocks = 8; //!< Averaged in each measurement
static constexpr int m_maxNotches = 4;
static const double m_twoPi;
static const double m_thresholdDb; //!< Above the signal's median level in a bin, to notch (a CW at -23 dBc)
static const double m_poleRadius; //!< Notch about 0.0003 of the sample rate wide (3 kHz at 9.14 MS/s)
static constexpr int m_matchBins = 3; //!< A peak this close to a notch, or a candidate, is the same one
static constexpr int m_missesToRemove = 3; //!< Measurements without the interferer
void measure(const Cf *block);
void decide();
void setFrequency(Notch& notch, double frequency);
bool m_enabled;
double m_sampleRate;
double m_bandwidth;
int m_interval; //!< Samples between blocks measured
int m_untilNext; //!< Samples until the next block is measured
std::vector<Cf> m_block; //!< Being gathered, as a block may span calls
int m_blockFill;
std::vector<double> m_window;
std::vector<double> m_power; //!< Averaged over m_blockCount blocks
int m_blockCount;
std::vector<std::complex<double>> m_spectra[2]; //!< Of the two blocks measured
std::vector<int> m_band; //!< Bins within the bandwidth
std::vector<double> m_levels;
std::vector<std::complex<double>> m_twiddles;
std::vector<int> m_bitReverse;
std::vector<double> m_candidates; //!< Frequencies seen in the last measurement, not yet notched
std::vector<Notch> m_notches;
};
// The notches' effect on the carriers near them, undone: each carrier within 50 kHz of a notch is divided by the
// notch's response at its frequency (known exactly), as the notch's phase changes too fast across them for the
// channel estimate to follow (26 degrees 3 kHz away, still 10 at 8 kHz), and those where the response is below half
// are erased (a carrier or two, or a few in 32K)
class MODEMDVB_API NotchedCarriers
{
public:
typedef std::complex<float> Cf;
//! Hz from the input's centre, of the input at inputRate
void setNotches(const std::vector<double>& frequencies, double inputRate);
bool empty() const { return m_notches.empty(); }
//! For carriers 0..kMax, carrier k being at (k - kMax / 2) spacing + offset Hz
void prepare(int kMax, double spacing, double offset);
//! Divide the carriers near the notches by its response
void compensate(Cf *y) const;
bool erased(int k) const { return (k < (int) m_erased.size()) && m_erased[k]; }
private:
std::vector<double> m_notches;
double m_inputRate = 1.0;
std::vector<int> m_carriers; //!< Near notches
std::vector<Cf> m_factors; //!< For each of them
std::vector<uint8_t> m_erased; //!< Per carrier
};
} // namespace dvbt
#endif // MODEMDVB_CWCANCELLER_H

256
modemdvb/dvbs.cpp

@ -0,0 +1,256 @@ @@ -0,0 +1,256 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include <cstring>
#include "dvbs.h"
#if defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
#include <emmintrin.h>
#define DVBS_SSE2
#endif
namespace dvbs {
double rollOffValue(RollOff rollOff)
{
switch (rollOff)
{
case RollOff::R0_25: return 0.25;
case RollOff::R0_20: return 0.20;
default: return 0.35;
}
}
const char *toString(RollOff rollOff)
{
switch (rollOff)
{
case RollOff::R0_25: return "0.25";
case RollOff::R0_20: return "0.20";
default: return "0.35";
}
}
void puncturePattern(CodeRate rate, std::vector<PunctureEntry>& pattern, int& period)
{
switch (rate)
{
case CodeRate::R1_2:
pattern = {{0, 0}, {0, 1}};
period = 1;
break;
case CodeRate::R2_3: // X: 10, Y: 11
pattern = {{0, 0}, {0, 1}, {1, 1}};
period = 2;
break;
case CodeRate::R3_4: // X: 101, Y: 110
pattern = {{0, 0}, {0, 1}, {1, 1}, {2, 0}};
period = 3;
break;
case CodeRate::R5_6: // X: 10101, Y: 11010
pattern = {{0, 0}, {0, 1}, {1, 1}, {2, 0}, {3, 1}, {4, 0}};
period = 5;
break;
case CodeRate::R7_8: // X: 1000101, Y: 1111010
default:
pattern = {{0, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 0}, {5, 1}, {6, 0}};
period = 7;
break;
}
}
int punctureLength(CodeRate rate)
{
std::vector<PunctureEntry> pattern;
int period;
puncturePattern(rate, pattern, period);
return (int) pattern.size();
}
double usefulBitRate(double symbolRate, CodeRate rate)
{
int num, den;
dvbt::codeRateFraction(rate, num, den);
return 2.0 * symbolRate * num / den * 188.0 / 204.0;
}
double rootRaisedCosine(double t, double a)
{
static const double pi = 3.141592653589793;
if (std::fabs(t) < 1e-9) {
return 1.0 - a + 4.0 * a / pi;
}
if ((a > 0.0) && (std::fabs(std::fabs(t) - 1.0 / (4.0 * a)) < 1e-9)) {
return a / std::sqrt(2.0) * ((1.0 + 2.0 / pi) * std::sin(pi / (4.0 * a)) + (1.0 - 2.0 / pi) * std::cos(pi / (4.0 * a)));
}
const double x = 4.0 * a * t;
return (std::sin(pi * t * (1.0 - a)) + 4.0 * a * t * std::cos(pi * t * (1.0 + a))) / (pi * t * (1.0 - x * x));
}
void softBits(const Cf *symbols, int nbSymbols, float scale, int8_t *out)
{
const float *in = reinterpret_cast<const float *>(symbols);
const int n = 2 * nbSymbols;
int i = 0;
#if defined(DVBS_SSE2)
const __m128 vScale = _mm_set1_ps(scale);
const __m128 vMax = _mm_set1_ps(127.0f);
const __m128 vMin = _mm_set1_ps(-127.0f);
for (; i + 16 <= n; i += 16)
{
__m128i q[4];
for (int j = 0; j < 4; j++)
{
__m128 v = _mm_mul_ps(_mm_loadu_ps(in + i + 4 * j), vScale);
q[j] = _mm_cvtps_epi32(_mm_max_ps(vMin, _mm_min_ps(vMax, v)));
}
_mm_storeu_si128(reinterpret_cast<__m128i *>(out + i), _mm_packs_epi16(_mm_packs_epi32(q[0], q[1]), _mm_packs_epi32(q[2], q[3])));
}
#endif
for (; i < n; i++) {
out[i] = (int8_t) std::lrint(std::max(-127.0f, std::min(127.0f, in[i] * scale)));
}
}
void fft(std::vector<std::complex<double>>& x, bool inverse)
{
static const double pi = 3.141592653589793;
const int n = (int) x.size();
for (int i = 1, j = 0; i < n; i++)
{
int bit = n >> 1;
for (; j & bit; bit >>= 1) {
j ^= bit;
}
j ^= bit;
if (i < j) {
std::swap(x[i], x[j]);
}
}
for (int len = 2; len <= n; len <<= 1)
{
const double angle = (inverse ? 2.0 : -2.0) * pi / len;
const std::complex<double> step(std::cos(angle), std::sin(angle));
for (int i = 0; i < n; i += len)
{
std::complex<double> w(1.0, 0.0);
for (int j = 0; j < len / 2; j++)
{
const std::complex<double> t = x[i + j + len / 2] * w;
x[i + j + len / 2] = x[i + j] - t;
x[i + j] += t;
w *= step;
}
}
}
}
void FFT::transform(std::vector<std::complex<double>>& x, bool inverse)
{
static const double pi = 3.141592653589793;
const int n = (int) x.size();
if ((int) m_twiddles.size() < n / 2)
{
m_twiddles.resize(n / 2);
for (int k = 0; k < n / 2; k++) {
m_twiddles[k] = std::polar(1.0, -2.0 * pi * k / n);
}
}
const int size = 2 * (int) m_twiddles.size();
const double sign = inverse ? -1.0 : 1.0; // Of the twiddles' imaginary parts
for (int i = 1, j = 0; i < n; i++)
{
int bit = n >> 1;
for (; j & bit; bit >>= 1) {
j ^= bit;
}
j ^= bit;
if (i < j) {
std::swap(x[i], x[j]);
}
}
// The products written out (as std::complex's may check for infinities at each)
for (int len = 2; len <= n; len <<= 1)
{
const int half = len / 2;
const int stride = size / len;
for (int i = 0; i < n; i += len)
{
for (int j = 0; j < half; j++)
{
const std::complex<double>& w = m_twiddles[(size_t) j * stride];
const double wr = w.real(), wi = sign * w.imag();
const double xr = x[i + j + half].real(), xi = x[i + j + half].imag();
const std::complex<double> t(xr * wr - xi * wi, xr * wi + xi * wr);
x[i + j + half] = x[i + j] - t;
x[i + j] += t;
}
}
}
}
double peakFrequency(const std::vector<std::complex<double>>& x)
{
const int n = (int) x.size();
int best = 0;
for (int i = 1; i < n; i++)
{
if (std::norm(x[i]) > std::norm(x[best])) {
best = i;
}
}
const double a = std::abs(x[(best + n - 1) % n]), b = std::abs(x[best]), c = std::abs(x[(best + 1) % n]);
const double denom = a - 2.0 * b + c;
double offset = denom < 0.0 ? std::max(-0.5, std::min(0.5, 0.5 * (a - c) / denom)) : 0.0;
double bin = best + offset;
if (bin >= n / 2.0) {
bin -= n;
}
return bin / n;
}
} // namespace dvbs

97
modemdvb/dvbs.h

@ -0,0 +1,97 @@ @@ -0,0 +1,97 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBS_H
#define MODEMDVB_DVBS_H
#include <complex>
#include <cstdint>
#include <cstring>
#include <vector>
#include "export.h"
#include "dvbt.h"
// DVB-S (EN 300 421): what the DVB-S and DVB-S2 receivers share
namespace dvbs {
typedef std::complex<float> Cf;
//! DVB-S's code rates are DVB-T's: 1/2, 2/3, 3/4, 5/6 and 7/8, punctured from the same K = 7 code the same way
typedef dvbt::CodeRate CodeRate;
//! Roll-off of the root raised cosine pulse: DVB-S's is 0.35, DVB-S2 also has 0.25 and 0.20
enum class RollOff { R0_35 = 0, R0_25 = 1, R0_20 = 2 };
MODEMDVB_API double rollOffValue(RollOff rollOff);
MODEMDVB_API const char *toString(RollOff rollOff);
//! One transmitted bit of a puncturing period: the information bit within the period, and X (0) or Y (1)
struct PunctureEntry
{
int m_bit;
int m_y;
};
//! EN 300 421 table 2 (the same as EN 300 744's): the transmitted bits of a period of period information bits, in
//! order. Consecutive pairs are a symbol's I and Q
MODEMDVB_API void puncturePattern(CodeRate rate, std::vector<PunctureEntry>& pattern, int& period);
//! Bits transmitted in a puncturing period
MODEMDVB_API int punctureLength(CodeRate rate);
//! Useful (transport stream) bit rate at a symbol rate: 2 Rs x rate x 188 / 204
MODEMDVB_API double usefulBitRate(double symbolRate, CodeRate rate);
//! Root raised cosine impulse response at t symbols from its centre, peak 1 - a + 4a / pi
MODEMDVB_API double rootRaisedCosine(double t, double rollOff);
//! magnitude with the sign of x, without a branch (decisions on noisy symbols would mispredict half the time)
inline float withSignOf(float magnitude, float x)
{
uint32_t m, s;
std::memcpy(&m, &magnitude, 4);
std::memcpy(&s, &x, 4);
m = (m & 0x7fffffffu) | (s & 0x80000000u);
float r;
std::memcpy(&r, &m, 4);
return r;
}
//! Soft bits of symbols: I and Q of each times scale, rounded and limited to +/-127 (SSE2 where there is it)
MODEMDVB_API void softBits(const Cf *symbols, int nbSymbols, float scale, int8_t *out);
//! In place radix-2 FFT, of a power of 2 points (forward: e^-j). For occasional use: twiddles computed each time
MODEMDVB_API void fft(std::vector<std::complex<double>>& x, bool inverse = false);
//! fft() for repeated use: its twiddles computed once (exactly, rather than by a recurrence), for the largest size it's
//! been used at, smaller sizes taking every so many
class MODEMDVB_API FFT
{
public:
void transform(std::vector<std::complex<double>>& x, bool inverse = false);
private:
std::vector<std::complex<double>> m_twiddles; //!< e^-j2 pi k / N, for k < N / 2
};
//! The peak of |x|^2 (from fft()), interpolated between bins, as a frequency in cycles a sample, -0.5 to 0.5
MODEMDVB_API double peakFrequency(const std::vector<std::complex<double>>& x);
} // namespace dvbs
#endif // MODEMDVB_DVBS_H

1590
modemdvb/dvbs2.cpp

File diff suppressed because it is too large Load Diff

331
modemdvb/dvbs2.h

@ -0,0 +1,331 @@ @@ -0,0 +1,331 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBS2_H
#define MODEMDVB_DVBS2_H
#include <algorithm>
#include <array>
#include <complex>
#include <cstdint>
#include <initializer_list>
#include <vector>
#include "export.h"
#include "dvbt2.h"
// DVB-S2 (EN 302 307 V1.2.1) and S2X (EN 302 307-2 V1.3.1, its broadcast MODCODs and VL-SNR frames: not superframes
// or channel bonding): MODCODs, the physical layer frame's structure and signalling, scrambling, constellations, bit interleaving
// and demapping. The LDPC and BCH codes are DVB-T2's (dvbt2fec.h), as CodeFamily::S2
//
// A MODCOD's id is S2's MODCOD (0 to 31) or, for S2X's (the PLS code's b0 = 1), 32 + its b1..b6 (32 to 95). An S2X
// MODCOD is for one frame size, its TYPE's MSB set from it; its b7 is the pilots, the TYPE's LSB as S2's
namespace dvbs2 {
typedef std::complex<float> Cf;
using dvbt2::CodeFamily;
using dvbt2::CodeRate;
using dvbt2::FECType;
//! The constellation's size and kind. S2X's 16 and 32APSK are S2's (4+12, 4+12+16) or others (8+8, 4+12+16rb,
//! 4+8+4+16) by code rate, its 64APSK one of three (16+16+16+16, 8+16+20+20, 4+12+20+28) and 8APSK is 2+4+2.
//! BPSK is pi/2 BPSK, VL-SNR's alone
enum class Modulation { QPSK = 0, PSK8 = 1, APSK16 = 2, APSK32 = 3, APSK8 = 4, APSK64 = 5, APSK128 = 6, APSK256 = 7, BPSK = 8 };
static const int slotSymbols = 90; //!< M (5.5)
static const int headerSymbols = 90; //!< The PLHEADER, one slot
static const int timeSlicingHeaderSymbols = 180; //!< EN 302 307-1 Annex M's (time slicing), two slots
static const int timeSlicingCodeBits = 77; //!< Its (77, 16) convolutional code's, each sent twice
static const int sofSymbols = 26;
static const int pilotSymbols = 36; //!< P, a pilot block
static const int pilotInterval = 16; //!< Slots between pilot blocks
static const int dummySlots = 36; //!< A dummy PLFRAME's
static const int dummyModcod = 0;
static const int s2xModcodBase = 32; //!< S2X MODCODs' ids: this plus b1..b6
static const int nbModcods = 96; //!< Ids 0 to 95
static const int maxPoints = 256; //!< In a constellation
static const int maxBitsPerSymbol = 8;
MODEMDVB_API const char *toString(Modulation modulation);
MODEMDVB_API int bitsPerSymbol(Modulation modulation);
//! Table 12 (S2's MODCOD 1 to 28) and EN 302 307-2's table 17a (S2X's): the modulation and code rate (the LDPC code's,
//! as the implementation's name has it). Returns false for 0 (dummy), S2's reserved 29 to 31 and S2X's reserved ones
MODEMDVB_API bool modcodInfo(int modcod, Modulation& modulation, CodeRate& rate);
//! The MODCOD of a modulation, code rate and frame size, 0 if there isn't one
MODEMDVB_API int modcod(Modulation modulation, CodeRate rate, FECType type = FECType::Normal);
//! "8PSK 2/3", or S2X's canonical name, as "16APSK 2/3-L"
MODEMDVB_API const char *modcodString(int modcod);
inline bool isS2X(int modcod) { return modcod >= s2xModcodBase; }
//! An S2X MODCOD's frame size, its code's. False for S2's, whose TYPE says
MODEMDVB_API bool fixedFECType(int modcod, FECType& type);
//! The frame size a MODCOD is sent with: an S2X one's own, else the one given
MODEMDVB_API FECType modcodFECType(int modcod, FECType type);
//! A MODCOD that can be decoded at that frame size: S2's 1 to 28 at either (but 9/10 only normal), S2X's at its own
MODEMDVB_API bool validModcod(int modcod, FECType type);
//! The PLS code's 8 bits (b0 the MSB) for a MODCOD and TYPE
MODEMDVB_API int plsCode(int modcod, int type);
//! S, the slots of an XFECFRAME (table 11; 128APSK's padded to 103)
MODEMDVB_API int slotCount(Modulation modulation, FECType type);
//! Pilot blocks in a PLFRAME of that many slots: one after every 16, but not at the end
MODEMDVB_API int pilotBlocks(int nbSlots, bool pilots);
//! Symbols in a PLFRAME, PLHEADER included: 90 (S + 1) + 36 int((S - 1) / 16) with pilots; a dummy's; that of an S2X
//! reserved PLS code (EN 302 307-2 table 17b: a receiver keeps lock through it); a VL-SNR set's. 0 if there's no length
//! (S2's reserved MODCODs, a MODCOD at a frame size it isn't for)
MODEMDVB_API int plFrameSymbols(int modcod, FECType type, bool pilots);
//! A PLFRAME's TYPE field: the frame size in its MSB, pilots in its LSB
inline int frameType(FECType type, bool pilots) { return (type == FECType::Short ? 2 : 0) | (pilots ? 1 : 0); }
//! Ideal Es/N0 at quasi error free (table 13, 64800 frames; EN 302 307-2's tables 20a and 20b), dB
MODEMDVB_API double idealEsN0(int modcod);
//! Useful bit rate: kbch less the BBHEADER of each frame, at a symbol rate
MODEMDVB_API double usefulBitRate(double symbolRate, int modcod, FECType type, bool pilots);
//! The PLHEADER's bits (5.5.2): the SOF then the scrambled PLS code of the MODCOD and TYPE, one per byte
MODEMDVB_API void plHeaderBits(int modcod, int type, uint8_t bits[headerSymbols]);
//! The PLHEADER's pi/2 BPSK symbols. S2X's PLS code's are turned by pi/2 (multiplied by j) after the SOF
MODEMDVB_API void plHeaderSymbols(int modcod, int type, Cf symbols[headerSymbols]);
//! The pi/2 BPSK symbol for a 0 at header position n (counting from 0): e^j pi/4, then e^j 3pi/4, alternately
MODEMDVB_API Cf headerSymbolZero(int n);
//! The SOF's bits
MODEMDVB_API const uint8_t *sofBits();
//! The PLS code's scrambling sequence, 64 bits
MODEMDVB_API const uint8_t *plsScrambling();
//! The 32 bits of the (32, 7) code for b0..b6 (b0 the MSB of value), each 0 or 1 (S2's figure 13b, with S2X's first
//! row, EN 302 307-2 figure 20). With b0 = 0, S2's (32, 6) code for b1..b6
MODEMDVB_API void plsCodeword(int value, uint8_t bits[32]);
//! The (32, 7) code's row for b0
static const uint32_t plsB0Row = 0x90AC2DDDu;
// EN 302 307-1 Annex M, time slicing: the PLHEADER's two slots, the SOF then the PLS code's 16 bits u0..u15 (the PLS code's
// b0..b7, as plsCode's, then the 8 bit time slice number, TSN) in a k = 5 rate 1/5 tail biting convolutional code, p4
// punctured after u3, u8 and u13 (77 bits), each bit sent twice and scrambled: 154 pi/2 BPSK symbols, without S2X's
// quarter turn
//! The code's five bits (p0 in bit 4) for an input bit from a state (S0 in bit 3 .. S3 in bit 0, S0 the last input).
//! The next state is (input << 3) | (state >> 1)
MODEMDVB_API int timeSlicingParity(int input, int state);
//! Whether the code's input bit j has its p4 (it's punctured for u3, u8 and u13)
inline bool timeSlicingHasP4(int j) { return (j != 3) && (j != 8) && (j != 13); }
//! The 77 bits of the code for a 16 bit word (u0 its MSB), each 0 or 1
MODEMDVB_API void timeSlicingCodeword(int word, uint8_t bits[timeSlicingCodeBits]);
//! The PLS code's scrambling sequence, 154 bits
MODEMDVB_API const uint8_t *timeSlicingScrambling();
//! The PLHEADER's bits: the SOF, then the code of the MODCOD's and TYPE's PLS code and the TSN, repeated and scrambled
MODEMDVB_API void timeSlicingHeaderBits(int modcod, int type, int tsn, uint8_t bits[timeSlicingHeaderSymbols]);
//! The PLHEADER's pi/2 BPSK symbols
MODEMDVB_API void timeSlicingHeader(int modcod, int type, int tsn, Cf symbols[timeSlicingHeaderSymbols]);
// EN 302 307-2 5.5.2, VL-SNR frames: a PLHEADER of PLS code 129 (set 1, MODCOD id 32 with pilots) or 131 (set 2, 33),
// then a 900 symbol pi/2 BPSK VL-SNR header whose Walsh-Hadamard sequence (index 0 to 15) says the MODCOD, then the
// XFECFRAME (shortened and punctured to fit) with extra pilot blocks between S2's: QPSK normal's length with pilots
// (set 1) or 16APSK normal's (set 2), so legacy receivers skip them. The headers aren't scrambled; the scrambling starts
// after the VL-SNR header, pi/2 BPSK data's by (-1)^R
static const int vlsnrHeaderSymbols = 900;
static const int vlsnrHeaderLength = headerSymbols + vlsnrHeaderSymbols; //!< Before the scrambling starts
static const int vlsnrIndexes = 16;
static const int vlsnrDummy = 12; //!< The VL-SNR dummy frame's index (set 2)
struct VLSNRModcod
{
bool m_valid; //!< Assigned (the dummy's not)
int m_set; //!< 1 or 2
Modulation m_modulation; //!< QPSK or BPSK (pi/2)
CodeRate m_rate; //!< The LDPC code's
FECType m_type;
int m_spreading; //!< Each bit sent this many times
int m_xs; //!< Information bits shortened (zeros, not sent)
int m_p; //!< Every P'th parity bit from the first punctured,
int m_xp; //!< this many
double m_esn0; //!< Ideal Es/N0 (EN 302 307-2 tables 20a to 20c)
const char *m_name; //!< Canonical (table 18a)
};
//! Table 18b's MODCOD for a VL-SNR header's index (0 to 15)
MODEMDVB_API const VLSNRModcod& vlsnrModcod(int index);
//! A PLHEADER's MODCOD id and TYPE are VL-SNR's: its set (1 or 2), else 0
inline int vlsnrSet(int modcod, int type) { return (type & 1) && ((modcod == s2xModcodBase) || (modcod == s2xModcodBase + 1)) ? modcod - s2xModcodBase + 1 : 0; }
//! The PLHEADER's MODCOD id for a set (with pilots)
inline int vlsnrSetModcod(int set) { return s2xModcodBase + set - 1; }
//! Symbols in a set's PLFRAME, headers included: 33 282 or 16 686
MODEMDVB_API int vlsnrFrameSymbols(int set);
//! Its data symbols: 30 780 or 14 976
MODEMDVB_API int vlsnrDataSymbols(int set);
//! For each symbol after the VL-SNR header, 1 if it's a pilot (figures 17 and 18): S2's blocks of 36 after every 16
//! slots (from the PLHEADER), and between them blocks of 34 (set 1's first 18 groups), 32 (set 2's first 9) or 36
MODEMDVB_API const std::vector<uint8_t>& vlsnrPilots(int set);
//! The header's 900 bits for an index: 00, the 896 bit pattern's 16 rows each inverted by the sequence's minus, 00
MODEMDVB_API void vlsnrHeaderBits(int index, uint8_t bits[vlsnrHeaderSymbols]);
//! Its pi/2 BPSK symbols
MODEMDVB_API void vlsnrHeader(int index, Cf symbols[vlsnrHeaderSymbols]);
//! The BBFRAME's bits, Kbch less the shortening (not whole bytes for the medium codes: the BBFRAME's last bits padding)
MODEMDVB_API int vlsnrKbch(int index);
//! Whether parity bit j of the index's code is punctured
inline bool vlsnrPunctured(const VLSNRModcod& m, int j) { return (j % m.m_p == 0) && (j / m.m_p < m.m_xp); }
//! The physical layer scrambling sequence (5.5.4) of Gold code n: R_n(i), 0 to 3, the symbol multiplied by j^R
class MODEMDVB_API Scrambler
{
public:
explicit Scrambler(int n = 0);
void setCode(int n);
int code() const { return m_n; }
//! R_n(i), for i from 0 at the first symbol after the PLHEADER
const std::vector<uint8_t>& sequence() const { return m_sequence; }
static constexpr int m_length = 66420; //!< As the standard defines it
private:
int m_n;
std::vector<uint8_t> m_sequence;
};
//! The constellation's points (5.4), indexed by the bits MSB first, at unit average power. APSK's ring ratios depend on
//! the code rate (tables 9 and 10), and for S2X, which constellation it is (EN 302 307-2 5.4), and for 4+12+16rbAPSK
//! 2/3, the frame size
MODEMDVB_API void constellation(Modulation modulation, CodeRate rate, std::vector<Cf>& points, FECType type = FECType::Normal);
//! The bit interleaver (5.3.3) and the mapping's serial to parallel conversion: map[symbol * eta + bit] is the codeword
//! bit that bit (MSB first) of the symbol carries. No interleaving for QPSK; columns read in reverse for 8PSK 3/5, and
//! for S2X, in the order of EN 302 307-2's tables 9a and 9b. 128APSK's padding: mapZero (the 6 zeros after the
//! codeword, interleaved) and mapOne (the 84 ones after the interleaver)
MODEMDVB_API void bitMap(FECType type, Modulation modulation, CodeRate rate, std::vector<int>& map);
static const int mapZero = -1;
static const int mapOne = -2;
//! The nearest of a constellation's points to a symbol, from a grid over the plane: in each cell, the points nearest
//! anywhere in it (those nearest its corners and centre: as the points' regions are far larger than a cell, those
//! are all of them but where three regions meet in it). The nearest of a cell's few (one, but by a boundary), rather
//! than of up to 256
class MODEMDVB_API NearestTable
{
public:
void build(const std::vector<Cf>& points);
bool empty() const { return m_table.empty(); }
int nearest(const Cf& c, const std::vector<Cf>& points) const
{
const int gx = std::min(m_grid - 1, std::max(0, (int) ((c.real() + m_range) * m_cellsPerUnit)));
const int gy = std::min(m_grid - 1, std::max(0, (int) ((c.imag() + m_range) * m_cellsPerUnit)));
const uint16_t *cell = &m_table[(gy * m_grid + gx) * m_slots];
int nearest = cell[0];
if (cell[1] != m_unused)
{
float best = std::norm(c - points[nearest]);
for (int k = 1; (k < m_slots) && (cell[k] != m_unused); k++)
{
const float e = std::norm(c - points[cell[k]]);
if (e < best)
{
best = e;
nearest = cell[k];
}
}
}
return nearest;
}
static constexpr int m_grid = 256; //!< Cells a side
static constexpr float m_range = 2.0f; //!< It covers +/- this, at unit energy (beyond, the nearest edge cell's)
static constexpr float m_cellsPerUnit = m_grid / (2.0f * m_range);
static constexpr int m_slots = 4; //!< Points a cell can list
static constexpr uint16_t m_unused = 0xffff;
private:
std::vector<uint16_t> m_table;
};
//! For the largest constellations (128 and 256 points), max-log demapping from a few of the points rather than all:
//! the symbol's nearest, and for each bit, those nearest that point with the bit the other way. Near enough what all
//! the points give (where the nearest with a bit the other way is further, the LLR's at its limit anyway), for a tenth
//! of the work
class MODEMDVB_API DemapTable
{
public:
void build(const std::vector<Cf>& points, int eta);
bool empty() const { return m_offsets.empty(); }
const NearestTable& nearestTable() const { return m_nearest; }
//! The candidates for a symbol whose nearest point is this one
const uint8_t *candidates(int point, int& count) const
{
count = m_offsets[point + 1] - m_offsets[point];
return &m_candidates[m_offsets[point]];
}
static constexpr int m_minPoints = 128; //!< Constellations this size or more have one
static constexpr int m_perBit = 4; //!< Nearest a point with each bit the other way
private:
NearestTable m_nearest;
std::vector<int> m_offsets;
std::vector<uint8_t> m_candidates;
};
//! Max-log demapping of normalised symbols (unit average power, the constellation's points) to int8 LLRs, positive for
//! 0, twice the actual LLR and limited to dvbt2::llrLimit, as dvbt2::demapCells: llrs[map[cell * eta + bit]]. scales:
//! each cell's 4 / noise variance per dimension (times its reliability); if outlierThreshold > 0, a cell whose error
//! power to the nearest point is more than outlierThreshold times the noise per dimension has its scale reduced in
//! proportion. errors: if not null, each cell's error power to the nearest point. Map entries below 0 (padding) are
//! skipped. table: for a constellation of 128 points or more, its candidates (else every point's tried). oneScale:
//! scales[0] is every cell's
MODEMDVB_API void demap(const Cf *cells, const float *scales, int nbCells, const std::vector<Cf>& points, int eta,
float outlierThreshold, const int *map, int8_t *llrs, float *errors = nullptr, const DemapTable *table = nullptr,
bool oneScale = false);
struct ModcodEntry;
struct S2XModcodEntry;
//! The tables and helpers of dvbs2.cpp's free functions
class S2Tables
{
private:
static constexpr double m_pi = 3.141592653589793;
static const ModcodEntry m_modcods[28];
static const S2XModcodEntry m_s2xModcods[];
static const float m_apsk16_18_30[16][2];
static const float m_apsk16_20_30[16][2];
static const float m_apsk256_20_30[256][2];
static const float m_apsk256_22_30[256][2];
static const VLSNRModcod m_vlsnrModcods[vlsnrIndexes];
static const S2XModcodEntry *s2xEntry(int modcod);
static std::vector<uint8_t> vlsnrPilotMask(std::initializer_list<std::array<int, 4>> groups, int last);
static void normalise(std::vector<Cf>& points);
static double tableAngle(double a, int p, int q);
static const char *interleaverPattern(FECType type, Modulation modulation, CodeRate rate);
static inline int8_t quantise(float v);
friend MODEMDVB_API bool modcodInfo(int modcod, Modulation& modulation, CodeRate& rate);
friend MODEMDVB_API int modcod(Modulation modulation, CodeRate rate, FECType type);
friend MODEMDVB_API const char *modcodString(int modcod);
friend MODEMDVB_API double idealEsN0(int modcod);
friend MODEMDVB_API const VLSNRModcod& vlsnrModcod(int index);
friend MODEMDVB_API const std::vector<uint8_t>& vlsnrPilots(int set);
friend MODEMDVB_API void constellation(Modulation modulation, CodeRate rate, std::vector<Cf>& points, FECType type);
friend MODEMDVB_API void bitMap(FECType type, Modulation modulation, CodeRate rate, std::vector<int>& map);
friend MODEMDVB_API void demap(const Cf *cells, const float *scales, int nbCells, const std::vector<Cf>& points, int eta,
float outlierThreshold, const int *map, int8_t *llrs, float *errors, const DemapTable *table, bool oneScale);
};
} // namespace dvbs2
#endif // MODEMDVB_DVBS2_H

772
modemdvb/dvbs2ldpctables.cpp

@ -0,0 +1,772 @@ @@ -0,0 +1,772 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// Addresses of parity bit accumulators of the DVB-S2 LDPC codes that DVB-T2 doesn't have, EN 302 307 V1.2.1
// annex B (Nldpc = 64 800) and annex C (Nldpc = 16 200). Generated from the text of the standard (and checked against
// ldpctool's copy). The other S2 codes are T2's (EN 302 755 annexes A and B): S2's 64K 2/3 and 16K 3/5 are not, so
// they're here. Each row, for a group of 360 information bits, is its number of addresses followed by the addresses
// for the group's first bit
#include "dvbt2fec.h"
#include "dvbt2ldpctables.h"
namespace dvbt2 {
// Table B.1: rate 1/4, Nldpc = 64800
const uint16_t LDPCTables::m_s2TableB1[] = {
12, 23606, 36098, 1140, 28859, 18148, 18510, 6226, 540, 42014, 20879, 23802, 47088,
12, 16419, 24928, 16609, 17248, 7693, 24997, 42587, 16858, 34921, 21042, 37024, 20692,
12, 1874, 40094, 18704, 14474, 14004, 11519, 13106, 28826, 38669, 22363, 30255, 31105,
12, 22254, 40564, 22645, 22532, 6134, 9176, 39998, 23892, 8937, 15608, 16854, 31009,
12, 8037, 40401, 13550, 19526, 41902, 28782, 13304, 32796, 24679, 27140, 45980, 10021,
12, 40540, 44498, 13911, 22435, 32701, 18405, 39929, 25521, 12497, 9851, 39223, 34823,
12, 15233, 45333, 5041, 44979, 45710, 42150, 19416, 1892, 23121, 15860, 8832, 10308,
12, 10468, 44296, 3611, 1480, 37581, 32254, 13817, 6883, 32892, 40258, 46538, 11940,
12, 6705, 21634, 28150, 43757, 895, 6547, 20970, 28914, 30117, 25736, 41734, 11392,
12, 22002, 5739, 27210, 27828, 34192, 37992, 10915, 6998, 3824, 42130, 4494, 35739,
12, 8515, 1191, 13642, 30950, 25943, 12673, 16726, 34261, 31828, 3340, 8747, 39225,
12, 18979, 17058, 43130, 4246, 4793, 44030, 19454, 29511, 47929, 15174, 24333, 19354,
12, 16694, 8381, 29642, 46516, 32224, 26344, 9405, 18292, 12437, 27316, 35466, 41992,
12, 15642, 5871, 46489, 26723, 23396, 7257, 8974, 3156, 37420, 44823, 35423, 13541,
12, 42858, 32008, 41282, 38773, 26570, 2702, 27260, 46974, 1469, 20887, 27426, 38553,
3, 22152, 24261, 8297,
3, 19347, 9978, 27802,
3, 34991, 6354, 33561,
3, 29782, 30875, 29523,
3, 9278, 48512, 14349,
3, 38061, 4165, 43878,
3, 8548, 33172, 34410,
3, 22535, 28811, 23950,
3, 20439, 4027, 24186,
3, 38618, 8187, 30947,
3, 35538, 43880, 21459,
3, 7091, 45616, 15063,
3, 5505, 9315, 21908,
3, 36046, 32914, 11836,
3, 7304, 39782, 33721,
3, 16905, 29962, 12980,
3, 11171, 23709, 22460,
3, 34541, 9937, 44500,
3, 14035, 47316, 8815,
3, 15057, 45482, 24461,
3, 30518, 36877, 879,
3, 7583, 13364, 24332,
3, 448, 27056, 4682,
3, 12083, 31378, 21670,
3, 1159, 18031, 2221,
3, 17028, 38715, 9350,
3, 17343, 24530, 29574,
3, 46128, 31039, 32818,
3, 20373, 36967, 18345,
3, 46685, 20622, 32806,
};
// Table B.2: rate 1/3, Nldpc = 64800
const uint16_t LDPCTables::m_s2TableB2[] = {
12, 34903, 20927, 32093, 1052, 25611, 16093, 16454, 5520, 506, 37399, 18518, 21120,
12, 11636, 14594, 22158, 14763, 15333, 6838, 22222, 37856, 14985, 31041, 18704, 32910,
12, 17449, 1665, 35639, 16624, 12867, 12449, 10241, 11650, 25622, 34372, 19878, 26894,
12, 29235, 19780, 36056, 20129, 20029, 5457, 8157, 35554, 21237, 7943, 13873, 14980,
12, 9912, 7143, 35911, 12043, 17360, 37253, 25588, 11827, 29152, 21936, 24125, 40870,
12, 40701, 36035, 39556, 12366, 19946, 29072, 16365, 35495, 22686, 11106, 8756, 34863,
12, 19165, 15702, 13536, 40238, 4465, 40034, 40590, 37540, 17162, 1712, 20577, 14138,
12, 31338, 19342, 9301, 39375, 3211, 1316, 33409, 28670, 12282, 6118, 29236, 35787,
12, 11504, 30506, 19558, 5100, 24188, 24738, 30397, 33775, 9699, 6215, 3397, 37451,
12, 34689, 23126, 7571, 1058, 12127, 27518, 23064, 11265, 14867, 30451, 28289, 2966,
12, 11660, 15334, 16867, 15160, 38343, 3778, 4265, 39139, 17293, 26229, 42604, 13486,
12, 31497, 1365, 14828, 7453, 26350, 41346, 28643, 23421, 8354, 16255, 11055, 24279,
12, 15687, 12467, 13906, 5215, 41328, 23755, 20800, 6447, 7970, 2803, 33262, 39843,
12, 5363, 22469, 38091, 28457, 36696, 34471, 23619, 2404, 24229, 41754, 1297, 18563,
12, 3673, 39070, 14480, 30279, 37483, 7580, 29519, 30519, 39831, 20252, 18132, 20010,
12, 34386, 7252, 27526, 12950, 6875, 43020, 31566, 39069, 18985, 15541, 40020, 16715,
12, 1721, 37332, 39953, 17430, 32134, 29162, 10490, 12971, 28581, 29331, 6489, 35383,
12, 736, 7022, 42349, 8783, 6767, 11871, 21675, 10325, 11548, 25978, 431, 24085,
12, 1925, 10602, 28585, 12170, 15156, 34404, 8351, 13273, 20208, 5800, 15367, 21764,
12, 16279, 37832, 34792, 21250, 34192, 7406, 41488, 18346, 29227, 26127, 25493, 7048,
3, 39948, 28229, 24899,
3, 17408, 14274, 38993,
3, 38774, 15968, 28459,
3, 41404, 27249, 27425,
3, 41229, 6082, 43114,
3, 13957, 4979, 40654,
3, 3093, 3438, 34992,
3, 34082, 6172, 28760,
3, 42210, 34141, 41021,
3, 14705, 17783, 10134,
3, 41755, 39884, 22773,
3, 14615, 15593, 1642,
3, 29111, 37061, 39860,
3, 9579, 33552, 633,
3, 12951, 21137, 39608,
3, 38244, 27361, 29417,
3, 2939, 10172, 36479,
3, 29094, 5357, 19224,
3, 9562, 24436, 28637,
3, 40177, 2326, 13504,
3, 6834, 21583, 42516,
3, 40651, 42810, 25709,
3, 31557, 32138, 38142,
3, 18624, 41867, 39296,
3, 37560, 14295, 16245,
3, 6821, 21679, 31570,
3, 25339, 25083, 22081,
3, 8047, 697, 35268,
3, 9884, 17073, 19995,
3, 26848, 35245, 8390,
3, 18658, 16134, 14807,
3, 12201, 32944, 5035,
3, 25236, 1216, 38986,
3, 42994, 24782, 8681,
3, 28321, 4932, 34249,
3, 4107, 29382, 32124,
3, 22157, 2624, 14468,
3, 38788, 27081, 7936,
3, 4368, 26148, 10578,
3, 25353, 4122, 39751,
};
// Table B.3: rate 2/5, Nldpc = 64800
const uint16_t LDPCTables::m_s2TableB3[] = {
12, 31413, 18834, 28884, 947, 23050, 14484, 14809, 4968, 455, 33659, 16666, 19008,
12, 13172, 19939, 13354, 13719, 6132, 20086, 34040, 13442, 27958, 16813, 29619, 16553,
12, 1499, 32075, 14962, 11578, 11204, 9217, 10485, 23062, 30936, 17892, 24204, 24885,
12, 32490, 18086, 18007, 4957, 7285, 32073, 19038, 7152, 12486, 13483, 24808, 21759,
12, 32321, 10839, 15620, 33521, 23030, 10646, 26236, 19744, 21713, 36784, 8016, 12869,
12, 35597, 11129, 17948, 26160, 14729, 31943, 20416, 10000, 7882, 31380, 27858, 33356,
12, 14125, 12131, 36199, 4058, 35992, 36594, 33698, 15475, 1566, 18498, 12725, 7067,
12, 17406, 8372, 35437, 2888, 1184, 30068, 25802, 11056, 5507, 26313, 32205, 37232,
12, 15254, 5365, 17308, 22519, 35009, 718, 5240, 16778, 23131, 24092, 20587, 33385,
12, 27455, 17602, 4590, 21767, 22266, 27357, 30400, 8732, 5596, 3060, 33703, 3596,
12, 6882, 873, 10997, 24738, 20770, 10067, 13379, 27409, 25463, 2673, 6998, 31378,
12, 15181, 13645, 34501, 3393, 3840, 35227, 15562, 23615, 38342, 12139, 19471, 15483,
12, 13350, 6707, 23709, 37204, 25778, 21082, 7511, 14588, 10010, 21854, 28375, 33591,
12, 12514, 4695, 37190, 21379, 18723, 5802, 7182, 2529, 29936, 35860, 28338, 10835,
12, 34283, 25610, 33026, 31017, 21259, 2165, 21807, 37578, 1175, 16710, 21939, 30841,
12, 27292, 33730, 6836, 26476, 27539, 35784, 18245, 16394, 17939, 23094, 19216, 17432,
12, 11655, 6183, 38708, 28408, 35157, 17089, 13998, 36029, 15052, 16617, 5638, 36464,
12, 15693, 28923, 26245, 9432, 11675, 25720, 26405, 5838, 31851, 26898, 8090, 37037,
12, 24418, 27583, 7959, 35562, 37771, 17784, 11382, 11156, 37855, 7073, 21685, 34515,
12, 10977, 13633, 30969, 7516, 11943, 18199, 5231, 13825, 19589, 23661, 11150, 35602,
12, 19124, 30774, 6670, 37344, 16510, 26317, 23518, 22957, 6348, 34069, 8845, 20175,
12, 34985, 14441, 25668, 4116, 3019, 21049, 37308, 24551, 24727, 20104, 24850, 12114,
12, 38187, 28527, 13108, 13985, 1425, 21477, 30807, 8613, 26241, 33368, 35913, 32477,
12, 5903, 34390, 24641, 26556, 23007, 27305, 38247, 2621, 9122, 32806, 21554, 18685,
3, 17287, 27292, 19033,
3, 25796, 31795, 12152,
3, 12184, 35088, 31226,
3, 38263, 33386, 24892,
3, 23114, 37995, 29796,
3, 34336, 10551, 36245,
3, 35407, 175, 7203,
3, 14654, 38201, 22605,
3, 28404, 6595, 1018,
3, 19932, 3524, 29305,
3, 31749, 20247, 8128,
3, 18026, 36357, 26735,
3, 7543, 29767, 13588,
3, 13333, 25965, 8463,
3, 14504, 36796, 19710,
3, 4528, 25299, 7318,
3, 35091, 25550, 14798,
3, 7824, 215, 1248,
3, 30848, 5362, 17291,
3, 28932, 30249, 27073,
3, 13062, 2103, 16206,
3, 7129, 32062, 19612,
3, 9512, 21936, 38833,
3, 35849, 33754, 23450,
3, 18705, 28656, 18111,
3, 22749, 27456, 32187,
3, 28229, 31684, 30160,
3, 15293, 8483, 28002,
3, 14880, 13334, 12584,
3, 28646, 2558, 19687,
3, 6259, 4499, 26336,
3, 11952, 28386, 8405,
3, 10609, 961, 7582,
3, 10423, 13191, 26818,
3, 15922, 36654, 21450,
3, 10492, 1532, 1205,
3, 30551, 36482, 22153,
3, 5156, 11330, 34243,
3, 28616, 35369, 13322,
3, 8962, 1485, 21186,
3, 23541, 17445, 35561,
3, 33133, 11593, 19895,
3, 33917, 7863, 33651,
3, 20063, 28331, 10702,
3, 13195, 21107, 21859,
3, 4364, 31137, 4804,
3, 5585, 2037, 4830,
3, 30672, 16927, 14800,
};
// Table B.6: rate 2/3, Nldpc = 64800
const uint16_t LDPCTables::m_s2TableB6[] = {
13, 0, 10491, 16043, 506, 12826, 8065, 8226, 2767, 240, 18673, 9279, 10579, 20928,
13, 1, 17819, 8313, 6433, 6224, 5120, 5824, 12812, 17187, 9940, 13447, 13825, 18483,
13, 2, 17957, 6024, 8681, 18628, 12794, 5915, 14576, 10970, 12064, 20437, 4455, 7151,
13, 3, 19777, 6183, 9972, 14536, 8182, 17749, 11341, 5556, 4379, 17434, 15477, 18532,
13, 4, 4651, 19689, 1608, 659, 16707, 14335, 6143, 3058, 14618, 17894, 20684, 5306,
13, 5, 9778, 2552, 12096, 12369, 15198, 16890, 4851, 3109, 1700, 18725, 1997, 15882,
13, 6, 486, 6111, 13743, 11537, 5591, 7433, 15227, 14145, 1483, 3887, 17431, 12430,
13, 7, 20647, 14311, 11734, 4180, 8110, 5525, 12141, 15761, 18661, 18441, 10569, 8192,
13, 8, 3791, 14759, 15264, 19918, 10132, 9062, 10010, 12786, 10675, 9682, 19246, 5454,
13, 9, 19525, 9485, 7777, 19999, 8378, 9209, 3163, 20232, 6690, 16518, 716, 7353,
13, 10, 4588, 6709, 20202, 10905, 915, 4317, 11073, 13576, 16433, 368, 3508, 21171,
13, 11, 14072, 4033, 19959, 12608, 631, 19494, 14160, 8249, 10223, 21504, 12395, 4322,
3, 12, 13800, 14161,
3, 13, 2948, 9647,
3, 14, 14693, 16027,
3, 15, 20506, 11082,
3, 16, 1143, 9020,
3, 17, 13501, 4014,
3, 18, 1548, 2190,
3, 19, 12216, 21556,
3, 20, 2095, 19897,
3, 21, 4189, 7958,
3, 22, 15940, 10048,
3, 23, 515, 12614,
3, 24, 8501, 8450,
3, 25, 17595, 16784,
3, 26, 5913, 8495,
3, 27, 16394, 10423,
3, 28, 7409, 6981,
3, 29, 6678, 15939,
3, 30, 20344, 12987,
3, 31, 2510, 14588,
3, 32, 17918, 6655,
3, 33, 6703, 19451,
3, 34, 496, 4217,
3, 35, 7290, 5766,
3, 36, 10521, 8925,
3, 37, 20379, 11905,
3, 38, 4090, 5838,
3, 39, 19082, 17040,
3, 40, 20233, 12352,
3, 41, 19365, 19546,
3, 42, 6249, 19030,
3, 43, 11037, 19193,
3, 44, 19760, 11772,
3, 45, 19644, 7428,
3, 46, 16076, 3521,
3, 47, 11779, 21062,
3, 48, 13062, 9682,
3, 49, 8934, 5217,
3, 50, 11087, 3319,
3, 51, 18892, 4356,
3, 52, 7894, 3898,
3, 53, 5963, 4360,
3, 54, 7346, 11726,
3, 55, 5182, 5609,
3, 56, 2412, 17295,
3, 57, 9845, 20494,
3, 58, 6687, 1864,
3, 59, 20564, 5216,
3, 0, 18226, 17207,
3, 1, 9380, 8266,
3, 2, 7073, 3065,
3, 3, 18252, 13437,
3, 4, 9161, 15642,
3, 5, 10714, 10153,
3, 6, 11585, 9078,
3, 7, 5359, 9418,
3, 8, 9024, 9515,
3, 9, 1206, 16354,
3, 10, 14994, 1102,
3, 11, 9375, 20796,
3, 12, 15964, 6027,
3, 13, 14789, 6452,
3, 14, 8002, 18591,
3, 15, 14742, 14089,
3, 16, 253, 3045,
3, 17, 1274, 19286,
3, 18, 14777, 2044,
3, 19, 13920, 9900,
3, 20, 452, 7374,
3, 21, 18206, 9921,
3, 22, 6131, 5414,
3, 23, 10077, 9726,
3, 24, 12045, 5479,
3, 25, 4322, 7990,
3, 26, 15616, 5550,
3, 27, 15561, 10661,
3, 28, 20718, 7387,
3, 29, 2518, 18804,
3, 30, 8984, 2600,
3, 31, 6516, 17909,
3, 32, 11148, 98,
3, 33, 20559, 3704,
3, 34, 7510, 1569,
3, 35, 16000, 11692,
3, 36, 9147, 10303,
3, 37, 16650, 191,
3, 38, 15577, 18685,
3, 39, 17167, 20917,
3, 40, 4256, 3391,
3, 41, 20092, 17219,
3, 42, 9218, 5056,
3, 43, 18429, 8472,
3, 44, 12093, 20753,
3, 45, 16345, 12748,
3, 46, 16023, 11095,
3, 47, 5048, 17595,
3, 48, 18995, 4817,
3, 49, 16483, 3536,
3, 50, 1439, 16148,
3, 51, 3661, 3039,
3, 52, 19010, 18121,
3, 53, 8968, 11793,
3, 54, 13427, 18003,
3, 55, 5303, 3083,
3, 56, 531, 16668,
3, 57, 4771, 6722,
3, 58, 5695, 7960,
3, 59, 3589, 14630,
};
// Table B.10: rate 8/9, Nldpc = 64800
const uint16_t LDPCTables::m_s2TableB10[] = {
4, 0, 6235, 2848, 3222,
4, 1, 5800, 3492, 5348,
4, 2, 2757, 927, 90,
4, 3, 6961, 4516, 4739,
4, 4, 1172, 3237, 6264,
4, 5, 1927, 2425, 3683,
4, 6, 3714, 6309, 2495,
4, 7, 3070, 6342, 7154,
4, 8, 2428, 613, 3761,
4, 9, 2906, 264, 5927,
4, 10, 1716, 1950, 4273,
4, 11, 4613, 6179, 3491,
4, 12, 4865, 3286, 6005,
4, 13, 1343, 5923, 3529,
4, 14, 4589, 4035, 2132,
4, 15, 1579, 3920, 6737,
4, 16, 1644, 1191, 5998,
4, 17, 1482, 2381, 4620,
4, 18, 6791, 6014, 6596,
4, 19, 2738, 5918, 3786,
3, 0, 5156, 6166,
3, 1, 1504, 4356,
3, 2, 130, 1904,
3, 3, 6027, 3187,
3, 4, 6718, 759,
3, 5, 6240, 2870,
3, 6, 2343, 1311,
3, 7, 1039, 5465,
3, 8, 6617, 2513,
3, 9, 1588, 5222,
3, 10, 6561, 535,
3, 11, 4765, 2054,
3, 12, 5966, 6892,
3, 13, 1969, 3869,
3, 14, 3571, 2420,
3, 15, 4632, 981,
3, 16, 3215, 4163,
3, 17, 973, 3117,
3, 18, 3802, 6198,
3, 19, 3794, 3948,
3, 0, 3196, 6126,
3, 1, 573, 1909,
3, 2, 850, 4034,
3, 3, 5622, 1601,
3, 4, 6005, 524,
3, 5, 5251, 5783,
3, 6, 172, 2032,
3, 7, 1875, 2475,
3, 8, 497, 1291,
3, 9, 2566, 3430,
3, 10, 1249, 740,
3, 11, 2944, 1948,
3, 12, 6528, 2899,
3, 13, 2243, 3616,
3, 14, 867, 3733,
3, 15, 1374, 4702,
3, 16, 4698, 2285,
3, 17, 4760, 3917,
3, 18, 1859, 4058,
3, 19, 6141, 3527,
3, 0, 2148, 5066,
3, 1, 1306, 145,
3, 2, 2319, 871,
3, 3, 3463, 1061,
3, 4, 5554, 6647,
3, 5, 5837, 339,
3, 6, 5821, 4932,
3, 7, 6356, 4756,
3, 8, 3930, 418,
3, 9, 211, 3094,
3, 10, 1007, 4928,
3, 11, 3584, 1235,
3, 12, 6982, 2869,
3, 13, 1612, 1013,
3, 14, 953, 4964,
3, 15, 4555, 4410,
3, 16, 4925, 4842,
3, 17, 5778, 600,
3, 18, 6509, 2417,
3, 19, 1260, 4903,
3, 0, 3369, 3031,
3, 1, 3557, 3224,
3, 2, 3028, 583,
3, 3, 3258, 440,
3, 4, 6226, 6655,
3, 5, 4895, 1094,
3, 6, 1481, 6847,
3, 7, 4433, 1932,
3, 8, 2107, 1649,
3, 9, 2119, 2065,
3, 10, 4003, 6388,
3, 11, 6720, 3622,
3, 12, 3694, 4521,
3, 13, 1164, 7050,
3, 14, 1965, 3613,
3, 15, 4331, 66,
3, 16, 2970, 1796,
3, 17, 4652, 3218,
3, 18, 1762, 4777,
3, 19, 5736, 1399,
3, 0, 970, 2572,
3, 1, 2062, 6599,
3, 2, 4597, 4870,
3, 3, 1228, 6913,
3, 4, 4159, 1037,
3, 5, 2916, 2362,
3, 6, 395, 1226,
3, 7, 6911, 4548,
3, 8, 4618, 2241,
3, 9, 4120, 4280,
3, 10, 5825, 474,
3, 11, 2154, 5558,
3, 12, 3793, 5471,
3, 13, 5707, 1595,
3, 14, 1403, 325,
3, 15, 6601, 5183,
3, 16, 6369, 4569,
3, 17, 4846, 896,
3, 18, 7092, 6184,
3, 19, 6764, 7127,
3, 0, 6358, 1951,
3, 1, 3117, 6960,
3, 2, 2710, 7062,
3, 3, 1133, 3604,
3, 4, 3694, 657,
3, 5, 1355, 110,
3, 6, 3329, 6736,
3, 7, 2505, 3407,
3, 8, 2462, 4806,
3, 9, 4216, 214,
3, 10, 5348, 5619,
3, 11, 6627, 6243,
3, 12, 2644, 5073,
3, 13, 4212, 5088,
3, 14, 3463, 3889,
3, 15, 5306, 478,
3, 16, 4320, 6121,
3, 17, 3961, 1125,
3, 18, 5699, 1195,
3, 19, 6511, 792,
3, 0, 3934, 2778,
3, 1, 3238, 6587,
3, 2, 1111, 6596,
3, 3, 1457, 6226,
3, 4, 1446, 3885,
3, 5, 3907, 4043,
3, 6, 6839, 2873,
3, 7, 1733, 5615,
3, 8, 5202, 4269,
3, 9, 3024, 4722,
3, 10, 5445, 6372,
3, 11, 370, 1828,
3, 12, 4695, 1600,
3, 13, 680, 2074,
3, 14, 1801, 6690,
3, 15, 2669, 1377,
3, 16, 2463, 1681,
3, 17, 5972, 5171,
3, 18, 5728, 4284,
3, 19, 1696, 1459,
};
// Table B.11: rate 9/10, Nldpc = 64800
const uint16_t LDPCTables::m_s2TableB11[] = {
4, 0, 5611, 2563, 2900,
4, 1, 5220, 3143, 4813,
4, 2, 2481, 834, 81,
4, 3, 6265, 4064, 4265,
4, 4, 1055, 2914, 5638,
4, 5, 1734, 2182, 3315,
4, 6, 3342, 5678, 2246,
4, 7, 2185, 552, 3385,
4, 8, 2615, 236, 5334,
4, 9, 1546, 1755, 3846,
4, 10, 4154, 5561, 3142,
4, 11, 4382, 2957, 5400,
4, 12, 1209, 5329, 3179,
4, 13, 1421, 3528, 6063,
4, 14, 1480, 1072, 5398,
4, 15, 3843, 1777, 4369,
4, 16, 1334, 2145, 4163,
4, 17, 2368, 5055, 260,
3, 0, 6118, 5405,
3, 1, 2994, 4370,
3, 2, 3405, 1669,
3, 3, 4640, 5550,
3, 4, 1354, 3921,
3, 5, 117, 1713,
3, 6, 5425, 2866,
3, 7, 6047, 683,
3, 8, 5616, 2582,
3, 9, 2108, 1179,
3, 10, 933, 4921,
3, 11, 5953, 2261,
3, 12, 1430, 4699,
3, 13, 5905, 480,
3, 14, 4289, 1846,
3, 15, 5374, 6208,
3, 16, 1775, 3476,
3, 17, 3216, 2178,
3, 0, 4165, 884,
3, 1, 2896, 3744,
3, 2, 874, 2801,
3, 3, 3423, 5579,
3, 4, 3404, 3552,
3, 5, 2876, 5515,
3, 6, 516, 1719,
3, 7, 765, 3631,
3, 8, 5059, 1441,
3, 9, 5629, 598,
3, 10, 5405, 473,
3, 11, 4724, 5210,
3, 12, 155, 1832,
3, 13, 1689, 2229,
3, 14, 449, 1164,
3, 15, 2308, 3088,
3, 16, 1122, 669,
3, 17, 2268, 5758,
3, 0, 5878, 2609,
3, 1, 782, 3359,
3, 2, 1231, 4231,
3, 3, 4225, 2052,
3, 4, 4286, 3517,
3, 5, 5531, 3184,
3, 6, 1935, 4560,
3, 7, 1174, 131,
3, 8, 3115, 956,
3, 9, 3129, 1088,
3, 10, 5238, 4440,
3, 11, 5722, 4280,
3, 12, 3540, 375,
3, 13, 191, 2782,
3, 14, 906, 4432,
3, 15, 3225, 1111,
3, 16, 6296, 2583,
3, 17, 1457, 903,
3, 0, 855, 4475,
3, 1, 4097, 3970,
3, 2, 4433, 4361,
3, 3, 5198, 541,
3, 4, 1146, 4426,
3, 5, 3202, 2902,
3, 6, 2724, 525,
3, 7, 1083, 4124,
3, 8, 2326, 6003,
3, 9, 5605, 5990,
3, 10, 4376, 1579,
3, 11, 4407, 984,
3, 12, 1332, 6163,
3, 13, 5359, 3975,
3, 14, 1907, 1854,
3, 15, 3601, 5748,
3, 16, 6056, 3266,
3, 17, 3322, 4085,
3, 0, 1768, 3244,
3, 1, 2149, 144,
3, 2, 1589, 4291,
3, 3, 5154, 1252,
3, 4, 1855, 5939,
3, 5, 4820, 2706,
3, 6, 1475, 3360,
3, 7, 4266, 693,
3, 8, 4156, 2018,
3, 9, 2103, 752,
3, 10, 3710, 3853,
3, 11, 5123, 931,
3, 12, 6146, 3323,
3, 13, 1939, 5002,
3, 14, 5140, 1437,
3, 15, 1263, 293,
3, 16, 5949, 4665,
3, 17, 4548, 6380,
3, 0, 3171, 4690,
3, 1, 5204, 2114,
3, 2, 6384, 5565,
3, 3, 5722, 1757,
3, 4, 2805, 6264,
3, 5, 1202, 2616,
3, 6, 1018, 3244,
3, 7, 4018, 5289,
3, 8, 2257, 3067,
3, 9, 2483, 3073,
3, 10, 1196, 5329,
3, 11, 649, 3918,
3, 12, 3791, 4581,
3, 13, 5028, 3803,
3, 14, 3119, 3506,
3, 15, 4779, 431,
3, 16, 3888, 5510,
3, 17, 4387, 4084,
3, 0, 5836, 1692,
3, 1, 5126, 1078,
3, 2, 5721, 6165,
3, 3, 3540, 2499,
3, 4, 2225, 6348,
3, 5, 1044, 1484,
3, 6, 6323, 4042,
3, 7, 1313, 5603,
3, 8, 1303, 3496,
3, 9, 3516, 3639,
3, 10, 5161, 2293,
3, 11, 4682, 3845,
3, 12, 3045, 643,
3, 13, 2818, 2616,
3, 14, 3267, 649,
3, 15, 6236, 593,
3, 16, 646, 2948,
3, 17, 4213, 1442,
3, 0, 5779, 1596,
3, 1, 2403, 1237,
3, 2, 2217, 1514,
3, 3, 5609, 716,
3, 4, 5155, 3858,
3, 5, 1517, 1312,
3, 6, 2554, 3158,
3, 7, 5280, 2643,
3, 8, 4990, 1353,
3, 9, 5648, 1170,
3, 10, 1152, 4366,
3, 11, 3561, 5368,
3, 12, 3581, 1411,
3, 13, 5647, 4661,
3, 14, 1542, 5401,
3, 15, 5078, 2687,
3, 16, 316, 1755,
3, 17, 3392, 1991,
};
// Table C.5: rate 3/5, Nldpc = 16200
const uint16_t LDPCTables::m_s2TableC5[] = {
12, 2765, 5713, 6426, 3596, 1374, 4811, 2182, 544, 3394, 2840, 4310, 771,
12, 4951, 211, 2208, 723, 1246, 2928, 398, 5739, 265, 5601, 5993, 2615,
12, 210, 4730, 5777, 3096, 4282, 6238, 4939, 1119, 6463, 5298, 6320, 4016,
12, 4167, 2063, 4757, 3157, 5664, 3956, 6045, 563, 4284, 2441, 3412, 6334,
12, 4201, 2428, 4474, 59, 1721, 736, 2997, 428, 3807, 1513, 4732, 6195,
12, 2670, 3081, 5139, 3736, 1999, 5889, 4362, 3806, 4534, 5409, 6384, 5809,
12, 5516, 1622, 2906, 3285, 1257, 5797, 3816, 817, 875, 2311, 3543, 1205,
12, 4244, 2184, 5415, 1705, 5642, 4886, 2333, 287, 1848, 1121, 3595, 6022,
12, 2142, 2830, 4069, 5654, 1295, 2951, 3919, 1356, 884, 1786, 396, 4738,
3, 0, 2161, 2653,
3, 1, 1380, 1461,
3, 2, 2502, 3707,
3, 3, 3971, 1057,
3, 4, 5985, 6062,
3, 5, 1733, 6028,
3, 6, 3786, 1936,
3, 7, 4292, 956,
3, 8, 5692, 3417,
3, 9, 266, 4878,
3, 10, 4913, 3247,
3, 11, 4763, 3937,
3, 12, 3590, 2903,
3, 13, 2566, 4215,
3, 14, 5208, 4707,
3, 15, 3940, 3388,
3, 16, 5109, 4556,
3, 17, 4908, 4177,
};
// Table C.10: rate 8/9, Nldpc = 16200
const uint16_t LDPCTables::m_s2TableC10[] = {
4, 0, 1558, 712, 805,
4, 1, 1450, 873, 1337,
4, 2, 1741, 1129, 1184,
4, 3, 294, 806, 1566,
4, 4, 482, 605, 923,
3, 0, 926, 1578,
3, 1, 777, 1374,
3, 2, 608, 151,
3, 3, 1195, 210,
3, 4, 1484, 692,
3, 0, 427, 488,
3, 1, 828, 1124,
3, 2, 874, 1366,
3, 3, 1500, 835,
3, 4, 1496, 502,
3, 0, 1006, 1701,
3, 1, 1155, 97,
3, 2, 657, 1403,
3, 3, 1453, 624,
3, 4, 429, 1495,
3, 0, 809, 385,
3, 1, 367, 151,
3, 2, 1323, 202,
3, 3, 960, 318,
3, 4, 1451, 1039,
3, 0, 1098, 1722,
3, 1, 1015, 1428,
3, 2, 1261, 1564,
3, 3, 544, 1190,
3, 4, 1472, 1246,
3, 0, 508, 630,
3, 1, 421, 1704,
3, 2, 284, 898,
3, 3, 392, 577,
3, 4, 1155, 556,
3, 0, 631, 1000,
3, 1, 732, 1368,
3, 2, 1328, 329,
3, 3, 1515, 506,
3, 4, 1104, 1172,
};
//! The S2 codes not shared with T2, or null
const LDPCTable *ldpcTableS2(FECType type, CodeRate rate)
{
static const LDPCTable tables[] = {
{FECType::Normal, CodeRate::R1_4, 64800, 16200, LDPCTables::m_s2TableB1},
{FECType::Normal, CodeRate::R1_3, 64800, 21600, LDPCTables::m_s2TableB2},
{FECType::Normal, CodeRate::R2_5, 64800, 25920, LDPCTables::m_s2TableB3},
{FECType::Normal, CodeRate::R2_3, 64800, 43200, LDPCTables::m_s2TableB6},
{FECType::Normal, CodeRate::R8_9, 64800, 57600, LDPCTables::m_s2TableB10},
{FECType::Normal, CodeRate::R9_10, 64800, 58320, LDPCTables::m_s2TableB11},
{FECType::Short, CodeRate::R3_5, 16200, 9720, LDPCTables::m_s2TableC5},
{FECType::Short, CodeRate::R8_9, 16200, 14400, LDPCTables::m_s2TableC10},
};
for (const LDPCTable& table : tables)
{
if ((table.m_type == type) && (table.m_rate == rate)) {
return &table;
}
}
return nullptr;
}
} // namespace dvbt2

367
modemdvb/dvbs2modulator.cpp

@ -0,0 +1,367 @@ @@ -0,0 +1,367 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include "dvbs2modulator.h"
namespace dvbs2 {
const Cf Modulator::m_turns[4] = {Cf(1.0f, 0.0f), Cf(0.0f, 1.0f), Cf(-1.0f, 0.0f), Cf(0.0f, -1.0f)};
Modulator::Modulator(const Config& config) :
m_config(config),
m_scrambler(config.m_scramblingCode),
m_frameCount(0)
{
for (int s = 0; s < 2; s++) {
m_streams[s].m_isi = config.m_isi[s];
}
}
Modulator::~Modulator()
{
}
Modulator::Code& Modulator::code(int modcod)
{
auto it = m_codes.find(modcod);
if (it != m_codes.end()) {
return *it->second;
}
std::unique_ptr<Code> c(new Code());
FECType type;
if (isVLSNR(modcod))
{
const VLSNRModcod& m = vlsnrModcod(vlsnrIndex(modcod));
c->m_modulation = m.m_modulation;
c->m_rate = m.m_rate;
type = m.m_type;
c->m_kbch = vlsnrKbch(vlsnrIndex(modcod));
}
else
{
modcodInfo(modcod, c->m_modulation, c->m_rate);
type = modcodFECType(modcod, m_config.m_fecType);
c->m_kbch = dvbt2::kbch(type, c->m_rate, CodeFamily::S2);
}
c->m_type = type;
c->m_kldpc = dvbt2::kldpc(type, c->m_rate, CodeFamily::S2);
c->m_bch.reset(new dvbt2::BCH(type, dvbt2::bchT(type, c->m_rate, CodeFamily::S2)));
c->m_ldpc.reset(new dvbt2::LDPC(type, c->m_rate, CodeFamily::S2));
bitMap(type, c->m_modulation, c->m_rate, c->m_map);
constellation(c->m_modulation, c->m_rate, c->m_points, type);
if (m_config.m_unitOuterRing && (c->m_modulation != Modulation::QPSK) && (c->m_modulation != Modulation::PSK8))
{
float outer = 0.0f;
for (const Cf& p : c->m_points) {
outer = std::max(outer, std::abs(p));
}
for (Cf& p : c->m_points) {
p /= outer;
}
}
Code& result = *c;
m_codes[modcod] = std::move(c);
return result;
}
double Modulator::packetsPerFrame() const
{
return dataBytes(m_config.m_vcm.empty() ? m_config.m_modcod : m_config.m_vcm[0]) / 188.0;
}
int Modulator::dataBytes(int modcod) const
{
if (isVLSNR(modcod)) {
return (vlsnrKbch(vlsnrIndex(modcod)) - 80) / 8;
}
Modulation modulation;
CodeRate rate;
modcodInfo(modcod, modulation, rate);
return (dvbt2::kbch(modcodFECType(modcod, m_config.m_fecType), rate, CodeFamily::S2) - 80) / 8;
}
int Modulator::nextModcod() const
{
return m_config.m_vcm.empty() ? m_config.m_modcod : m_config.m_vcm[m_frameCount % m_config.m_vcm.size()];
}
void Modulator::addPacket(Stream& stream, const uint8_t *packet)
{
stream.m_data.push_back(stream.m_crc);
stream.m_data.insert(stream.m_data.end(), packet + 1, packet + 188);
stream.m_crc = dvbt2::crc8(packet + 1, 187);
}
void Modulator::modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& symbols)
{
uint8_t null[188] = {0x47, 0x1f, 0xff, 0x10};
for (int p = 0; p < nbPackets; p++)
{
addPacket(m_streams[0], packets + 188 * p);
if (m_config.m_streams > 1) {
addPacket(m_streams[1], null);
}
while (true)
{
Stream& stream = m_streams[m_config.m_streams > 1 ? m_frameCount % 2 : 0];
const int modcod = nextModcod();
if (!frameReady(stream, modcod)) {
break;
}
if (isVLSNR(modcod)) {
vlsnrFrame(stream, vlsnrIndex(modcod), symbols);
} else {
frame(stream, modcod, symbols);
}
}
}
}
void Modulator::bbFrame(Stream& stream, int kbch, std::vector<uint8_t>& bb)
{
const int dataBytes = (kbch - 80) / 8;
bb.assign((kbch + 7) / 8, 0);
// Time slices: each its own single stream, told apart by its TSN alone
const bool timeSlicing = !m_config.m_tsns.empty();
const bool single = (m_config.m_streams <= 1) || timeSlicing;
// TS/GS: transport stream, or generic continuous (no user packets: UPL, SYNC and SYNCD 0)
const bool generic = m_config.m_generic;
const int ro = m_config.m_rollOff < 4 ? (m_config.m_rollOff & 3) : (m_frameCount & 1) ? (m_config.m_rollOff & 3) : 3;
bb[0] = (uint8_t) ((generic ? 0x40 : 0xc0) | (single ? 0x20 : 0) | (m_config.m_vcm.empty() ? 0x10 : 0) | ro);
bb[1] = (uint8_t) (single ? 0 : stream.m_isi);
bb[2] = (uint8_t) (generic ? 0 : (188 * 8) >> 8);
bb[3] = (uint8_t) (generic ? 0 : 188 * 8);
bb[4] = (uint8_t) ((dataBytes * 8) >> 8);
bb[5] = (uint8_t) (dataBytes * 8);
bb[6] = generic ? 0 : 0x47;
const int syncd = generic ? 0 : stream.m_syncd < dataBytes ? stream.m_syncd * 8 : 65535;
bb[7] = (uint8_t) (syncd >> 8);
bb[8] = (uint8_t) syncd;
bb[9] = dvbt2::crc8(bb.data(), 9);
std::copy(stream.m_data.begin(), stream.m_data.begin() + dataBytes, bb.begin() + 10);
stream.m_data.erase(stream.m_data.begin(), stream.m_data.begin() + dataBytes);
const int consumed = dataBytes - stream.m_syncd;
stream.m_syncd = (188 - (consumed % 188)) % 188;
dvbt2::scrambleBytes(bb.data(), (int) bb.size());
}
void Modulator::frame(Stream& stream, int modcod, std::vector<Cf>& symbols)
{
Code& c = code(modcod);
const bool timeSlicing = !m_config.m_tsns.empty();
const int streamIndex = &stream == &m_streams[0] ? 0 : 1;
std::vector<uint8_t> bb;
bbFrame(stream, c.m_kbch, bb);
// BCH, then LDPC, of the bits MSB first
std::vector<uint8_t> parity((c.m_bch->parityBits() + 7) / 8);
c.m_bch->encode(bb.data(), (int) bb.size(), parity.data());
const int n = c.m_ldpc->n();
m_bits.assign(n, 0);
for (int i = 0; i < c.m_kbch; i++) {
m_bits[i] = (bb[i / 8] >> (7 - i % 8)) & 1;
}
for (int i = 0; i < c.m_bch->parityBits(); i++) {
m_bits[c.m_kbch + i] = (parity[i / 8] >> (7 - i % 8)) & 1;
}
c.m_ldpc->encode(m_bits.data(), m_bits.data() + c.m_kldpc);
// Interleaved and mapped (with 128APSK's padding)
const int eta = bitsPerSymbol(c.m_modulation);
const int nbCells = (int) c.m_map.size() / eta;
m_cells.resize(nbCells);
for (int i = 0; i < nbCells; i++)
{
int index = 0;
for (int b = 0; b < eta; b++)
{
const int bit = c.m_map[i * eta + b];
index = (index << 1) | (bit >= 0 ? m_bits[bit] : bit == mapOne ? 1 : 0);
}
m_cells[i] = c.m_points[index];
}
// The PLFRAME: header (time slicing's two slots), slots with a pilot block after every 16 (not at the end), all
// but the header scrambled
const int type = frameType(c.m_type, m_config.m_pilots);
Cf header[timeSlicingHeaderSymbols];
const int length = timeSlicing ? timeSlicingHeaderSymbols : headerSymbols;
if (timeSlicing) {
timeSlicingHeader(modcod, type, m_config.m_tsns[streamIndex % m_config.m_tsns.size()], header);
} else {
plHeaderSymbols(modcod, type, header);
}
symbols.insert(symbols.end(), header, header + length);
const int s = nbCells / slotSymbols;
const std::vector<uint8_t>& r = m_scrambler.sequence();
int k = 0;
static const Cf pilot(0.70710678f, 0.70710678f);
for (int slot = 0; slot < s; slot++)
{
for (int i = 0; i < slotSymbols; i++, k++) {
symbols.push_back(m_cells[slot * slotSymbols + i] * m_turns[r[k]]);
}
if (m_config.m_pilots && ((slot + 1) % pilotInterval == 0) && (slot + 1 < s))
{
for (int i = 0; i < pilotSymbols; i++, k++) {
symbols.push_back(pilot * m_turns[r[k]]);
}
}
}
m_frameCount++;
}
// EN 302 307-2 5.5.2: the PLHEADER (PLS code 129 or 131), the VL-SNR header, then the XFECFRAME, its first Xs
// information bits (zeros) and Xp of its parity bits not sent, each bit sent spreading times; with the pilots of
// figures 17 and 18, the scrambling restarted after the VL-SNR header
void Modulator::vlsnrFrame(Stream& stream, int index, std::vector<Cf>& symbols)
{
const VLSNRModcod& m = vlsnrModcod(index);
Code& c = code(vlsnr(index));
std::vector<uint8_t> bb;
bbFrame(stream, c.m_kbch, bb);
std::vector<uint8_t> parity((c.m_bch->parityBits() + 7) / 8);
c.m_bch->encodeBits(bb.data(), c.m_kbch, parity.data());
const int n = c.m_ldpc->n();
m_bits.assign(n, 0);
for (int i = 0; i < c.m_kbch; i++) {
m_bits[m.m_xs + i] = (bb[i / 8] >> (7 - i % 8)) & 1;
}
for (int i = 0; i < c.m_bch->parityBits(); i++) {
m_bits[m.m_xs + c.m_kbch + i] = (parity[i / 8] >> (7 - i % 8)) & 1;
}
c.m_ldpc->encode(m_bits.data(), m_bits.data() + c.m_kldpc);
// The bits sent, each spreading times
std::vector<uint8_t> sent;
sent.reserve((size_t) n * m.m_spreading);
for (int i = m.m_xs; i < n; i++)
{
if ((i >= c.m_kldpc) && vlsnrPunctured(m, i - c.m_kldpc)) {
continue;
}
sent.insert(sent.end(), (size_t) m.m_spreading, m_bits[i]);
}
const int set = m.m_set;
Cf header[headerSymbols];
plHeaderSymbols(vlsnrSetModcod(set), 1, header);
symbols.insert(symbols.end(), header, header + headerSymbols);
Cf vlHeader[vlsnrHeaderSymbols];
vlsnrHeader(index, vlHeader);
symbols.insert(symbols.end(), vlHeader, vlHeader + vlsnrHeaderSymbols);
const std::vector<uint8_t>& pilots = vlsnrPilots(set);
const std::vector<uint8_t>& r = m_scrambler.sequence();
static const Cf pilot(0.70710678f, 0.70710678f);
const bool qpsk = m.m_modulation == Modulation::QPSK;
size_t d = 0; // Data symbols
for (size_t k = 0; k < pilots.size(); k++)
{
if (pilots[k])
{
symbols.push_back(pilot * m_turns[r[k]]);
}
else if (qpsk)
{
symbols.push_back(c.m_points[(sent[2 * d] << 1) | sent[2 * d + 1]] * m_turns[r[k]]);
d++;
}
else
{
// pi/2 BPSK, scrambled by (-1)^R
const float sign = (sent[d] ? -1.0f : 1.0f) * ((r[k] & 1) ? -1.0f : 1.0f);
symbols.push_back(headerSymbolZero((int) d) * sign);
d++;
}
}
m_frameCount++;
}
void Modulator::vlsnrDummyFrame(std::vector<Cf>& symbols)
{
Cf header[headerSymbols];
plHeaderSymbols(vlsnrSetModcod(2), 1, header);
symbols.insert(symbols.end(), header, header + headerSymbols);
Cf vlHeader[vlsnrHeaderSymbols];
vlsnrHeader(vlsnrDummy, vlHeader);
symbols.insert(symbols.end(), vlHeader, vlHeader + vlsnrHeaderSymbols);
const std::vector<uint8_t>& r = m_scrambler.sequence();
static const Cf unmodulated(0.70710678f, 0.70710678f);
for (int k = 0; k < vlsnrFrameSymbols(2) - vlsnrHeaderLength; k++) {
symbols.push_back(unmodulated * m_turns[r[k]]);
}
}
void Modulator::dummyFrame(std::vector<Cf>& symbols)
{
Cf header[timeSlicingHeaderSymbols];
const bool timeSlicing = !m_config.m_tsns.empty();
if (timeSlicing) {
timeSlicingHeader(dummyModcod, 0, m_config.m_tsns[0], header);
} else {
plHeaderSymbols(dummyModcod, 0, header);
}
symbols.insert(symbols.end(), header, header + (timeSlicing ? timeSlicingHeaderSymbols : headerSymbols));
const std::vector<uint8_t>& r = m_scrambler.sequence();
static const Cf unmodulated(0.70710678f, 0.70710678f);
for (int k = 0; k < dummySlots * slotSymbols; k++) {
symbols.push_back(unmodulated * m_turns[r[k]]);
}
}
} // namespace dvbs2

126
modemdvb/dvbs2modulator.h

@ -0,0 +1,126 @@ @@ -0,0 +1,126 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBS2MODULATOR_H
#define MODEMDVB_DVBS2MODULATOR_H
#include <cstdint>
#include <map>
#include <memory>
#include <vector>
#include "dvbs2.h"
#include "dvbt2fec.h"
namespace dvbs2 {
//! DVB-S2 transmitter, for testing the receiver: a transport stream (or two, as a multiple input stream) to PLFRAMEs of
//! symbols. CCM, or VCM with a sequence of MODCODs, a frame each in turn. A MODCOD may be a VL-SNR frame's
//! (EN 302 307-2 5.5.2), as vlsnr(index) gives: negative
class MODEMDVB_API Modulator
{
public:
//! The MODCOD for a VL-SNR frame of the VL-SNR header's index (0 to 11, not the dummy's: vlsnrDummyFrame)
static int vlsnr(int index) { return -1 - index; }
static bool isVLSNR(int modcod) { return modcod < 0; }
static int vlsnrIndex(int modcod) { return -1 - modcod; }
struct Config
{
int m_modcod = 13; //!< 8PSK 2/3
FECType m_fecType = FECType::Normal;
bool m_pilots = true;
int m_scramblingCode = 0;
//! MATYPE's RO: 0 0.35, 1 0.25, 2 0.20; S2X's 4 0.15, 5 0.10, 6 0.05 (RO alternating between 11 and the
//! code less 4)
int m_rollOff = 0;
std::vector<int> m_vcm; //!< MODCODs, a frame each in turn, if not empty
int m_streams = 1; //!< 2 for a multiple input stream: the packets given in ISI m_isi[0], null packets in m_isi[1]
int m_isi[2] = {5, 9};
//! The outer ring of APSK at the pilots' amplitude (the standard's other choice), rather than unit energy
bool m_unitOuterRing = false;
//! A generic continuous stream (as GSE is carried) of the packets' bytes, rather than a transport stream
bool m_generic = false;
//! Time slicing (EN 302 307-1 Annex M), if not empty: each stream's frames in a time slice, its TSN this (the
//! first stream's the first), their BBHEADERs a single stream's
std::vector<int> m_tsns;
};
explicit Modulator(const Config& config);
~Modulator();
Modulator(const Modulator&) = delete;
Modulator& operator=(const Modulator&) = delete;
//! 188-byte packets starting 0x47. Appends the PLFRAMEs they complete
void modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& symbols);
//! A dummy PLFRAME
void dummyFrame(std::vector<Cf>& symbols);
//! A VL-SNR dummy PLFRAME (set 2's length)
void vlsnrDummyFrame(std::vector<Cf>& symbols);
//! Packets a frame of the first MODCOD takes, on average
double packetsPerFrame() const;
const Config& config() const { return m_config; }
private:
//! A stream's user packets, as they go into BBFRAMEs: normal mode, each packet's CRC-8 in place of the next's sync
struct Stream
{
std::vector<uint8_t> m_data;
int m_syncd = 0; //!< Bytes from m_data's start to the first packet's
uint8_t m_crc = 0;
int m_isi = 0;
};
struct Code
{
Modulation m_modulation;
CodeRate m_rate;
FECType m_type; //!< The configured, but an S2X MODCOD's own (a VL-SNR frame's code's)
int m_kbch; //!< A VL-SNR frame's less its shortening
int m_kldpc;
std::unique_ptr<dvbt2::BCH> m_bch;
std::unique_ptr<dvbt2::LDPC> m_ldpc;
std::vector<int> m_map;
std::vector<Cf> m_points;
};
void addPacket(Stream& stream, const uint8_t *packet);
//! The data field's bytes in a BBFRAME of the MODCOD
int dataBytes(int modcod) const;
bool frameReady(const Stream& stream, int modcod) const { return (int) stream.m_data.size() >= dataBytes(modcod); }
//! The next BBFRAME of kbch bits (its last byte's padded), scrambled, its data taken from the stream
void bbFrame(Stream& stream, int kbch, std::vector<uint8_t>& bb);
void frame(Stream& stream, int modcod, std::vector<Cf>& symbols);
void vlsnrFrame(Stream& stream, int index, std::vector<Cf>& symbols);
Code& code(int modcod);
int nextModcod() const;
static const Cf m_turns[4];
Config m_config;
Scrambler m_scrambler;
std::map<int, std::unique_ptr<Code>> m_codes;
Stream m_streams[2];
int m_frameCount;
std::vector<uint8_t> m_bits;
std::vector<Cf> m_cells;
};
} // namespace dvbs2
#endif // MODEMDVB_DVBS2MODULATOR_H

3340
modemdvb/dvbs2pl.cpp

File diff suppressed because it is too large Load Diff

472
modemdvb/dvbs2pl.h

@ -0,0 +1,472 @@ @@ -0,0 +1,472 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBS2PL_H
#define MODEMDVB_DVBS2PL_H
#include <cmath>
#include <cstdint>
#include <deque>
#include <functional>
#include <utility>
#include <vector>
#include "dvbs2.h"
namespace dvbs2 {
//! The data symbols of a PLFRAME, ready to demap
struct MODEMDVB_API XFECFrame
{
int m_modcod = 0;
int m_type = 0; //!< TYPE: short frame in bit 1, pilots in bit 0
Modulation m_modulation = Modulation::QPSK;
CodeRate m_rate = CodeRate::R1_2;
FECType m_fecType = FECType::Normal;
uint64_t m_sequence = 0; //!< PLFRAMEs (dummies included) since the decoder's reset
bool m_gap = false; //!< Frames were lost before it
bool m_suspect = false; //!< Its header or the next one's wasn't found: probably cut by lost samples
bool m_timeSlicing = false; //!< Of EN 302 307-1 Annex M's time slicing (its PLHEADER two slots)
int m_tsn = -1; //!< Time slicing, its time slice number: -1 if its header wasn't found
//! A VL-SNR frame's header's index (m_modulation, m_rate and m_fecType its code's), else -1. Its cells are those
//! sent: the shortened and punctured bits not, each spread bit's every time; pi/2 BPSK's turned to the real axis
int m_vlsnr = -1;
std::vector<Cf> m_cells; //!< Carrier corrected, normalised to the constellation and descrambled
float m_noise = 1.0f; //!< Variance per dimension of the cells' noise
};
//! DVB-S2 physical layer (EN 302 307 5.5): symbols at one a symbol (from the front end) in, XFECFRAMEs out.
//!
//! - Frame sync: the PLHEADER found by differential correlation (each symbol times the last one's conjugate, which a
//! frequency offset only turns) with the SOF's 25 known differences and the PLS code's 32 pairs (each pair's
//! difference is known but for b7's sign), against the metric's own statistics; then confirmed by the next header,
//! where the decoded PLS code says it will be. Locked, every frame's header is checked, and three missing in a row
//! start the search again.
//! - PLS code: coherently, after the SOF, soft correlated with the (32, 7) code's codewords for both values of b7, and
//! of b0: S2X's (b0 = 1) are a quarter turn on from the SOF, so its PLS codes are decoded from the quadrature.
//! - Carrier: each frame's known symbols (the header, the pilot blocks, the next header) with the modulation taken off
//! give the frequency by Luise and Reggiannini's estimator (to a few percent of the symbol rate), then the phase steps
//! from block to block refine it. Each data symbol's phase is interpolated between the blocks either side. Gain and
//! noise come from the known symbols, and the APSK data's scale from its power (the standard allows the outer ring at
//! the pilots' amplitude, as well as unit energy).
//! - Without pilots: a decision directed loop runs from the header. The frequency's tracked frame to frame from the
//! headers' phase steps, which give it only to within a turn a frame: a new lock's first frames try the rates they
//! allow, whole turns apart, and take the one the loop leaves the data nearest its points at; short frames' headers'
//! own estimates, averaged over frames, correct it later (and a lock whose loop keeps ending far from the next
//! header's phase is dropped, as an alias). The phase noise comes from the headers' steps too, and sets the
//! loop's bandwidth: narrow in white noise, wide enough to follow an LNB's.
class MODEMDVB_API PLDecoder
{
public:
enum class State { Searching = 0, Locked = 2 };
struct Status
{
State m_state = State::Searching;
int m_modcod = -1; //!< Of the last frame
int m_type = 0;
uint64_t m_frames = 0; //!< PLFRAMEs decoded (not dummies)
uint64_t m_dummyFrames = 0;
uint64_t m_headerMisses = 0; //!< Headers expected but not found
uint64_t m_syncLosses = 0;
uint64_t m_falseLocks = 0; //!< New locks dropped at once, their known symbols too noisy for any MODCOD
double m_frequency = 0.0; //!< Cycles a symbol, of the carrier left after the front end
double m_esn0 = 0.0; //!< dB, from the known symbols
double m_mer = 0.0; //!< dB, of the data symbols, from the nearest points
double m_phaseNoise = 0.0; //!< Radians rms a symbol, of the phase's random walk, from the pilots
uint64_t m_reservedFrames = 0; //!< Of S2X's reserved PLS codes (their length known): skipped
uint64_t m_modcodsSeen[2] = {0, 0}; //!< Bit for each MODCOD received (S2X's from 32)
//! The spectrum's inverted (I and Q swapped): the symbols are conjugated, m_frequency is of them
bool m_inverted = false;
bool m_timeSlicing = false; //!< The lock's frames are time slicing's (EN 302 307-1 Annex M)
int m_tsn = -1; //!< Of the last frame, time slicing
int m_vlsnr = -1; //!< Of the last frame, a VL-SNR frame's header's index
uint32_t m_vlsnrSeen = 0; //!< Bit for each VL-SNR header's index received
uint64_t m_vlsnrFrames = 0; //!< VL-SNR frames decoded (in m_frames)
//! VL-SNR's scrambling found to start after the PLHEADER, through the VL-SNR header (as the standard can be read),
//! rather than after it
bool m_vlsnrScramblingThroughHeader = false;
};
typedef std::function<void(XFECFrame& frame)> FrameCallback;
PLDecoder();
void setFrameCallback(FrameCallback callback) { m_callback = callback; }
//! The Gold code n of the physical layer scrambling, 0 by default
void setScramblingCode(int n);
void reset();
//! Symbols were skipped (searching): the search goes on from the next, with what it's learnt of the noise
void discontinuity();
void process(const Cf *symbols, int nbSymbols);
const Status& getStatus() const { return m_status; }
//! A large frequency offset (over 5 % of the symbol rate) for the front end's NCO to take out, in cycles a symbol
//! of its input (so negated if the spectrum's inverted), else 0. Taking it starts the search again, as the symbols
//! already received are at the old frequency
double takeFrequencyCorrection();
//! The symbol clock's error, from VL-SNR frames' headers slipping, for the front end's timing loop: ppm to add to
//! its clock (positive: the signal's faster), else 0. At VL-SNR's lowest SNRs, the loop's too narrow to pull in a
//! clock offset itself (and wider, it wanders too far). Taken, no more are measured till clockCorrected()
double takeClockCorrection();
//! The correction's made, for the symbols from the next given: slips are measured again from them (those before,
//! perhaps many queued for a shared front end's DVB-S2 thread, would ask for it again)
void clockCorrected();
//! Symbols the search can take to lock, without a header missed: VL-SNR's search starts only after a while, and
//! time slicing's metric after longer, then a header and the next are found. A restart sooner never locks
static int64_t acquisitionSymbols();
//! A header VL-SNR's search found is waiting for its next (a frame later) to confirm it
bool vlsnrCandidatePending() const;
//! Up to maxCells of the last frame's data cells, for display
void getConstellation(std::vector<Cf>& cells, int maxCells) const;
private:
struct Header
{
bool m_valid = false;
int m_modcod = 0;
int m_type = 0;
double m_margin = 0.0; //!< Of the best codeword's correlation over the next best, 0 to 1
double m_score = 0.0; //!< The SOF's and the best codeword's coherent correlations, for comparing frequencies
double m_frequency = 0.0; //!< It was decoded at, cycles a symbol
bool m_timeSlicing = false; //!< EN 302 307-1 Annex M's: two slots, with the TSN
int m_tsn = -1;
};
void append(const Cf *symbols, int nbSymbols);
void trim(int64_t keepFrom);
const Cf& at(int64_t index) const { return m_buffer[(size_t) (index - m_base)]; }
//! length symbols from index turned by a phasor (in double), stepped a symbol at a time, and if known's given, its
//! symbols' modulation taken off: the phasor after them
std::complex<double> turnBack(int64_t index, int length, std::complex<double> phasor, const std::complex<double>& step,
const Cf *known, Cf *out) const;
//! The differential correlations at a header starting at index: SOF's and the PLS code's (S2's 32 pairs, or 32 of
//! time slicing's repeated bits')
void correlate(int64_t index, bool timeSlicing, Cf& sof, Cf& pls) const;
//! The same at the 4 positions from index, at once (each the same as correlate()'s)
void correlate4(int64_t index, bool timeSlicing, Cf sof[4], Cf pls[4]) const;
//! |c| as std::abs's (correctly rounded), without its call to hypot: the search's metric is per symbol
static float modulus(const Cf& c)
{
return (float) std::sqrt((double) c.real() * c.real() + (double) c.imag() * c.imag());
}
static double metric(const Cf& sof, const Cf& pls) { return (modulus(sof) + modulus(pls)) / 57.0; }
//! Of the metric (of the kind) where there's no header, at least m_minMetricSigma
double metricSigma(bool timeSlicing) const;
//! The metric's threshold: sigmas over its mean where there's no header
double metricThreshold(bool timeSlicing, double sigmas) const { return m_metricMean[timeSlicing] + sigmas * metricSigma(timeSlicing); }
//! The kinds of header a decode tries
enum class HeaderKind { Any, S2, TimeSlicing };
//! Decode the PLS code of the header at index, the carrier at frequency (cycles a symbol): S2's (or S2X's), or time
//! slicing's, or either (S2's, unless it's not clear and time slicing's correlates better). Searching, either;
//! locked or confirming a candidate, the kind it's of (a weak header of one kind decodes now and then as the other,
//! whose frame length would lose the lock)
Header decodeHeader(int64_t index, double frequency, HeaderKind kind = HeaderKind::Any) const;
static HeaderKind kindOf(const Header& header) { return header.m_timeSlicing ? HeaderKind::TimeSlicing : HeaderKind::S2; }
//! Time slicing's PLS code from the header's symbols in phase with the SOF after it (154, pi/2 BPSK taken off):
//! the best 16 bit word and its correlation, and the next best's, by a tail biting list Viterbi decoder
void decodeTimeSlicing(const float *r, int& word, float& best, float& second) const;
static int headerLength(const Header& header) { return header.m_timeSlicing ? timeSlicingHeaderSymbols : headerSymbols; }
//! The PLFRAME's symbols, its header's included: 0 if its length isn't known
static int frameLength(const Header& header);
//! The header's symbols (headerLength of them)
static void knownHeader(const Header& header, Cf *symbols);
//! The same at frequencies either side of that given, the best: at low SNR a header's differences give the frequency
//! only to a few percent of the symbol rate, which would turn the phase several radians across the PLS code
Header searchHeader(int64_t index, double frequency) const;
//! The differential correlation, the PLS code's sign aligned with the SOF's: its phase is the frequency
Cf aligned(const Cf& sof, const Cf& pls) const;
double localFrequency(const Cf& sof, const Cf& pls) const;
void processFrame(int64_t start, const Header& header, int64_t next, const Header& nextHeader);
//! A VL-SNR frame (EN 302 307-2 5.5.2): the carrier from the PLHEADER, the pilot blocks and the next header, then
//! the VL-SNR header's index from its symbols, then the data
void processVLSNR(int64_t start, const Header& header, int64_t next, const Header& nextHeader);
//! A VL-SNR frame's headers at index, correlated coherently at the frequency: the PLHEADER of each set, and the
//! VL-SNR header's rows, each with its sign in the index's sequence. The best's correlation in standard deviations
//! of the noise's, its set and index, and the set whose PLHEADER alone correlates better
double vlsnrHeaderScore(int64_t index, double frequency, int& set, int& vlIndex, int& plhSet) const;
//! The next VL-SNR frame's headers at next, or a symbol or two either side (the symbol timing can slip in a frame
//! at VL-SNR's lowest SNRs), after a frame of currentSet: their header (next moved to them), else false. A slip in
//! the headers can make them look like another index's (the rows after it correlating as if inverted): another
//! set's (another frame length) is taken only if its PLHEADER alone says so too
bool findVLSNRHeader(int64_t& next, double frequency, int currentSet, Header& header) const;
//! VL-SNR's search, a block of positions from m_vlsnrScan (see process())
void searchVLSNR();
//! A VL-SNR frame's headers at index, at any frequency: each set's and index's correlated coherently at every
//! frequency (by FFT). The best's header (its frequency), if its score's over m_vlsnrPresentScore
bool verifyVLSNR(int64_t index, Header& header, double& score);
//! The phase left after the pilots' interpolation, slot by slot, from the decisions, taken off
void refineSlotPhases(Cf *cells, int slots, int modcod, Modulation modulation);
//! The phase left after the pilots' interpolation (phase noise), symbol by symbol, taken off the data cells: from
//! the frame in m_trackValue (known symbols and data, in order). noise is the known symbols', q the phase noise's
//! variance a symbol
void smoothPhase(Cf *cells, int modcod, double noise, double q);
//! The phase noise from the known blocks' phases (centres in symbols, phases unwrapped), step the frequency left
//! (radians a symbol)
void measurePhaseNoise(const double *centres, const double *phases, const int *lengths, int nbBlocks, double step,
double noise);
//! The carrier's frequency from a dummy frame, all of whose symbols are known
void trackDummy(int64_t start, const Header& header, const Header& nextHeader);
//! A known block's symbols (from start, the frame's first symbol's index) at frequency, their modulation off
void modulationOff(int64_t frameStart, int64_t blockStart, int length, const Cf *known, double frequency,
std::vector<Cf>& u) const;
const NearestTable& nearestPoints(int modcod);
//! Lock lost (falseLock: dropped as implausible, not counted as a loss)
void lost(bool falseLock = false);
//! The spectrum taken the other way up: the symbols held conjugated, and those to come
void invert();
//! A dummy frame: false if its known symbols say the lock's false
bool checkDummy(int64_t start, const Header& header);
static inline double wrap(double a);
static double outerRingStep(const std::vector<Cf>& points);
static inline Cf mul(const Cf& a, const Cf& b);
static inline Cf nearestQPSK(const Cf& v);
static inline Cf nearestPSK8(const Cf& v);
template <bool psk8>
static Cf pskErrorSum(const Cf *c, int n, const Cf& p);
static void turnValues(const Cf *in, Cf *out, int n, Cf rot, const Cf& step, const uint8_t *scrambling = nullptr);
static constexpr double m_pi = 3.141592653589793;
static constexpr int m_maxMisses = 3; //!< Headers missing in a row before searching again
static constexpr double m_candidateSigmas = 3.5; //!< Of the metric over its mean, for a header in the search
static constexpr size_t m_maxCandidates = 64; //!< Waiting to be confirmed
static constexpr double m_verifySigmas = 3.0; //!< At the candidate's next header
static constexpr double m_confirmSigmas = 2.0; //!< At a header's expected place, locked
static constexpr double m_minMetricSigma = 0.02; //!< The metric's spread is never taken as less (noise's is about 0.06)
static constexpr double m_minMargin = 0.05; //!< Of the PLS code's best correlation over the next, locked
static constexpr double m_searchMargin = 0.15; //!< The same, searching (noise's is rarely as much)
static constexpr double m_knownTolerance = 0.005; //!< Cycles a symbol: a header's own frequency this far from the one known
//!< is searched around too (the known one may be a false lock's)
static constexpr double m_strongerCandidate = 1.5; //!< A candidate waiting with this times the metric goes first: when frames
//!< repeat (a still picture, padding), data can look like a header in each
static constexpr double m_minLockEsN0 = -6.5; //!< dB: a new lock's frames' known symbols below this, it's false (the
//!< lowest MODCOD's threshold is -2.35, the estimate's spread well within)
static constexpr uint64_t m_trustedFrames = 4; //!< Locked this long, the lock's real (a false one doesn't last)
static constexpr double m_searchStep = 0.0025; //!< Cycles a symbol, of the frequencies a header's decoded at in the search
static constexpr int m_searchSteps = 20; //!< Either side: +/- 0.05
static constexpr int m_lrLags = 24; //!< Of Luise and Reggiannini's estimator: range +/- 1/25 cycles a symbol
static constexpr double m_coarseLimit = 2.5e-3; //!< Cycles a symbol: settled, a frame's estimate used only beyond this (it's
//!< too noisy at low SNR for the pilots' phase steps, which wrap at 3.4e-4)
static constexpr double m_pilotPeriod = 1476.0; //!< Symbols from pilot block to pilot block: their steps alias at 1 / this
static constexpr double m_vlsnrPilotPeriod = 738.0; //!< VL-SNR frames' (737 and 739 apart in set 1, 736 and 740 in set 2)
static constexpr uint64_t m_settledFrames = 1; //!< Locked this long, the frequency's tracked from the pilots
static constexpr double m_maxFrequency = 0.05; //!< Cycles a symbol, beyond which the front end's NCO takes it
static constexpr double m_invertedFrequency = 0.25; //!< Cycles a symbol: a valid header this far out's of an inverted spectrum
static constexpr double m_timeSlicingTest = 0.75; //!< An S2 PLS code correlating under this times the SOF, time slicing's tried
static constexpr double m_timeSlicingNoise = 0.55; //!< Time slicing's code correlating under this times its symbols' rms: noise
static constexpr double m_timeSlicingPairs = 0.3; //!< Its repeated bits' agreement under this (of half their power): not tried
static constexpr int m_sameTsnFrames = 8; //!< Frames in a row of one TSN, after which a frame without its header's of it
static constexpr int64_t m_modeSymbols = 4 * 33372; //!< Searching this long without a lock, the other kind of header's metric
//!< (S2's or time slicing's): four of the longest frames (with two, S2's
//!< longest, near threshold, were cut off before a header was confirmed)
static constexpr int m_ddBlockSymbols = 8; //!< Without pilots, the decision directed loop's update interval
static constexpr double m_ddMinBandwidth = 0.0004; //!< Of the decision directed loop, without pilots (white noise)
static constexpr double m_ddMaxBandwidth = 0.02; //!< (Heavy phase noise)
static constexpr double m_ddBandwidthGain = 0.25; //!< Of the Kalman gain, for the loop's bandwidth
static constexpr double m_ddPhaseNoiseWeight = 0.1; //!< Of each frame, in the phase noise's average
static constexpr double m_ddFrequencyGain = 0.5; //!< Of each frame's header to header frequency, without pilots
static constexpr double m_ddDriftGain = 0.15; //!< Of it, for its drift a frame
static constexpr double m_ddTrustedEnd = 1.0; //!< Radians: the loop's end from the next header's phase, within which the
//!< loop's advance is taken (else the headers', within half a turn)
static constexpr uint64_t m_aliasSearchFrames = 2; //!< Without pilots, a new lock's frames whose frequency alias is searched
static constexpr double m_aliasSearchUncertainty = 0.75 * m_searchStep; //!< Cycles a symbol either side tried at lock
static constexpr int m_aliasConfirmTurns = 4; //!< Turns a frame either side tried next
static constexpr double m_aliasErrorWeight = 0.1; //!< Of each frame, in the average of the loop's end from the next header's
static constexpr uint64_t m_aliasErrorFrames = 10; //!< After the search, before the average's taken as the lock's health
static constexpr double m_aliasLostError = 1.0; //!< Radians: the average over which the lock's dropped
static constexpr uint64_t m_coarseAliasFrames = 16; //!< Without pilots, frames of the headers' estimates averaged, at least
static constexpr double m_coarseAliasTurns = 0.9; //!< Turns a frame their average must be out
static constexpr double m_coarseAliasSigmas = 5.0; //!< And standard errors
static constexpr double m_smoothedPhaseNoise = 0.004; //!< Radians a symbol: more, the phase is tracked symbol by symbol, less
//!< (cheaper), slot by slot
static constexpr double m_vlsnrPresentScore = 6.0; //!< Standard deviations: a VL-SNR frame's headers there, locked (noise's
//!< best of the 32 hypotheses is rarely over 3; at -10 dB, a header's ~10)
static constexpr int m_maxSlip = 2; //!< Symbols either side a VL-SNR frame's next header's looked for
static constexpr int m_slipCorrection = 2; //!< Slips one way, for the clock to be corrected
static constexpr int m_maxSlipFrames = 64; //!< Frames, after which slips fewer are forgotten
static constexpr int64_t m_vlsnrSearchDelay = 2 * 33282; //!< Searching this long without a lock, VL-SNR's search too: S2's
//!< finds VL-SNR frames above about -3 dB, and at 27.5 MS/s
//!< VL-SNR's would take much of the search's CPU
static constexpr int m_vlsnrSearchFFT = 4096; //!< VL-SNR's search's FFT: its headers' 990 symbols' differences, and
static constexpr int m_vlsnrSearchBlock = 3072; //!< the positions searched with each (at most its size less theirs)
static constexpr double m_vlsnrSearchSigmas = 3.5; //!< Of the search's correlation, for a peak to verify: noise's over it
//!< e^-12 of the time (a few a second at 1 MS/s, each 10 FFTs to verify);
//!< a header's about 6 at -6 dB, 4 at -8 dB
static const Cf m_pilotValue; //!< A pilot symbol (and a dummy frame's data), unscrambled
static const Cf m_quarterTurns[4]; //!< j^R, the scrambling's
FrameCallback m_callback;
Scrambler m_scrambler;
Status m_status;
std::vector<Cf> m_buffer; //!< Symbols from m_base
std::vector<Cf> m_diff; //!< Each times the last's conjugate
int64_t m_base;
int64_t m_end; //!< Index after the last symbol
// Known patterns: the SOF's 25 differences, the PLS code's 32 pairs' differences (b7 = 0), time slicing's 77 pairs',
// the header symbols for 0
Cf m_sofDiff[sofSymbols];
Cf m_plsDiff[32];
Cf m_timeSlicingDiff[timeSlicingCodeBits];
Cf m_zero[timeSlicingHeaderSymbols];
Cf m_vlsnrKnown[2][vlsnrHeaderLength]; //!< Each VL-SNR set's PLHEADER, then the VL-SNR header of index 0
float m_vlsnrSigns[vlsnrIndexes][16]; //!< Each index's sequence: each row's sign against index 0's
//! VL-SNR's search: the next position, the conjugate spectrum of its headers' known differences, how many there are,
//! and each assigned index's (and the dummy's) headers, with its set's PLHEADER
int64_t m_vlsnrScan;
std::vector<std::complex<double>> m_vlsnrDiffSpectrum;
int m_vlsnrPairs;
struct VLSNRHypothesis
{
int m_set;
int m_index;
std::vector<Cf> m_known;
};
std::vector<VLSNRHypothesis> m_vlsnrHypotheses;
std::vector<std::complex<double>> m_vlsnrFFT; //!< The search's block's correlation
std::vector<std::complex<double>> m_vlsnrVerify; //!< A verify's (in the search's block: not the same)
std::vector<double> m_vlsnrPower;
std::vector<std::vector<float>> m_codewords; //!< The (32, 6) code's (b1..b6), as +/-1
float m_b0Signs[32]; //!< S2X's row (b0), as +/-1: added to a codeword, its signs multiply
// Each codeword as a Walsh function and a sign (the code is first order Reed-Muller), so a 32-point fast
// Walsh-Hadamard transform correlates with them all; -1 if one isn't (when they're correlated directly)
int m_walsh[64];
float m_walshSign[64];
// Search
int64_t m_scan; //!< Next header position to try
// Of the metric where there's no header, for the thresholds: S2's, then time slicing's
double m_metricMean[2];
double m_metricVar[2];
uint64_t m_metricCount[2];
//! A header found in the search, waiting for its next to confirm it (the search goes on meanwhile, so a false one
//! doesn't hold it up for a frame)
struct Candidate
{
int64_t m_index;
int64_t m_next;
Header m_header;
double m_frequency;
double m_metric; //!< The search's, at the header
bool m_timeSlicing; //!< The search's metric's kind, for the next header's
bool m_vlsnr; //!< Found by VL-SNR's search: its metric's verifyVLSNR's score
};
std::deque<Candidate> m_candidates;
// Locked
int64_t m_frameStart;
int64_t m_lockStart; //!< The first frame's, of the lock
bool m_implausible; //!< The last frame's known symbols said the lock's false
Header m_frameHeader;
int m_misses;
bool m_suspect; //!< The frame being processed: its header or the next one's missing
bool m_frequencyKnown;
double m_frequency; //!< Cycles a symbol
double m_frequencyBeforeLock; //!< Restored if the lock turns out false (lost within a few frames), with these
bool m_frequencyKnownBeforeLock;
double m_phaseNoiseBeforeLock;
uint64_t m_phaseNoiseFramesBeforeLock;
double m_coarseAverage; //!< Of the frames' Luise and Reggiannini estimates, settled, for aliases
double m_vlsnrCoarseAverage; //!< The same of VL-SNR frames', whose pilot blocks are half as far apart
uint64_t m_vlsnrCoarseFrames;
double m_ddEndError; //!< Without pilots, average of the loop's phase at a frame's end from the next header's
double m_ddPhaseNoise; //!< Without pilots, the phase noise's variance a symbol, from the headers
double m_ddPhaseNoiseBeforeLock;
double m_ddDrift; //!< Without pilots, the frequency's drift a frame, cycles a symbol
double m_ddCoarseSum; //!< Without pilots, of the headers' Luise and Reggiannini estimates since the search
double m_ddCoarseSquares;
uint64_t m_ddCoarseFrames;
double m_ddResidual; //!< The last frame's header to header phase step, less the frequency's
bool m_ddResidualValid;
uint64_t m_lockedFrames; //!< Since lock
uint64_t m_sequence;
bool m_gap;
bool m_inverted; //!< The symbols are conjugated as they come, the spectrum being inverted
//! The last lock was time slicing's, or searching long without a lock, the search tries it: the metric's then of its
//! header's pairs (a header's decoded either way)
bool m_timeSlicing;
int64_t m_modeStart; //!< Searching, where the metric last changed kind (S2's or time slicing's)
int64_t m_searchStart; //!< Where the search started (VL-SNR's runs only after a while)
//! Locked to VL-SNR frames, their headers' slips (symbols) since the last clock correction, over these symbols and
//! frames; the correction waiting
int m_slipSum;
int64_t m_slipSymbols;
int m_slipFrames;
double m_clockCorrection;
bool m_clockHold; //!< A correction taken, not yet made
int64_t m_slipFrom; //!< Slips are measured in frames from here (after the last correction)
int m_tsnRepeats; //!< Time slicing, frames in a row of the same TSN: a frame's whose header's missed, then
double m_esn0Average;
std::vector<double> m_dataPower; //!< Per MODCOD, of the APSK data relative to the pilots', 0 until measured
std::vector<Cf> m_points[nbModcods]; //!< Constellation of each MODCOD
double m_slipAngle[nbModcods]; //!< Each's outer ring's angle between neighbouring points: a decision's slip
NearestTable m_nearestTables[nbModcods]; //!< For the decisions, made when wanted
//! The MODCOD whose table each uses (one made for the same points: S2's QPSK and 8PSK at every rate), -1 till wanted
int m_nearestOf[nbModcods];
std::vector<Cf> m_dummy; //!< A dummy frame's symbols, its known values taken off
double m_phaseNoise; //!< Variance a symbol of the phase's random walk, from the pilots
uint64_t m_phaseNoiseFrames;
std::vector<double> m_slotUnwrapped; //!< Each slot's phase from its decisions, unwrapped
std::vector<double> m_slotPhases;
std::vector<Cf> m_blockSymbols; //!< A known block's, modulation off
//! A frame's known blocks (the header, pilot blocks and next header): where, their symbols, phasor and phase
struct KnownBlock
{
int64_t m_start;
int m_length;
Cf m_known[timeSlicingHeaderSymbols];
Cf m_phasor;
double m_phase;
};
std::vector<KnownBlock> m_knownBlocks; //!< processFrame's, kept (made each frame, they were 35 KB to allocate and zero)
//! A VL-SNR frame's, its symbols elsewhere (its VL-SNR header's 900)
struct VLSNRBlock
{
int64_t m_start;
int m_length;
const Cf *m_known;
Cf m_phasor;
double m_phase;
};
std::vector<VLSNRBlock> m_vlsnrBlocks; //!< processVLSNR's, and the trials of its scrambling's start
std::vector<VLSNRBlock> m_vlsnrTrial;
std::vector<Cf> m_vlsnrPilotKnown[2]; //!< The pilot blocks' symbols, in order, for each start
Cf m_vlsnrFirstKnown[timeSlicingHeaderSymbols]; //!< Its header's, the next's, and its VL-SNR header's
Cf m_vlsnrLastKnown[timeSlicingHeaderSymbols];
Cf m_vlsnrHeaderKnown[vlsnrHeaderSymbols];
std::vector<Cf> m_vlsnrSmoothed; //!< The blocks' phasors, smoothed
std::vector<double> m_blockCentres; //!< The known blocks', for the phase noise
std::vector<double> m_blockPhases;
std::vector<int> m_blockLengths;
std::vector<Cf> m_trackValue; //!< The frame for the smoother: known symbols (modulation off) and data
std::vector<uint8_t> m_trackKnown;
std::vector<float> m_trackForward; //!< The smoother's estimates each way, and their variances
std::vector<float> m_trackForwardVariance;
std::vector<Cf> m_trackForwardRot; //!< e^-j the forward's phase
std::vector<float> m_trackBackward;
std::vector<float> m_trackBackwardVariance;
std::vector<float> m_pointInvPower; //!< The smoother's: each point's inverse power, and decision variance
std::vector<float> m_pointNoise;
std::vector<std::pair<int, int>> m_segmentSlips; //!< Each run of data's forward and backward slips, in points
std::vector<Cf> m_display;
XFECFrame m_frame;
};
} // namespace dvbs2
#endif // MODEMDVB_DVBS2PL_H

1247
modemdvb/dvbs2receiver.cpp

File diff suppressed because it is too large Load Diff

318
modemdvb/dvbs2receiver.h

@ -0,0 +1,318 @@ @@ -0,0 +1,318 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBS2RECEIVER_H
#define MODEMDVB_DVBS2RECEIVER_H
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <functional>
#include <map>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "dvbs2.h"
#include "dvbs2pl.h"
#include "dvbsfrontend.h"
#include "dvbt2fec.h"
#include "ldpcpool.h"
class SampleThread;
namespace dvbs2 {
//! DVB-S2 receiver (EN 302 307): samples at the channel rate in, transport stream packets of one input stream out.
//! If threaded, the front end and physical layer decoding run in a thread of their own, and the XFECFRAMEs are queued
//! for error correction in another: it takes the frames waiting, in batches of as many as the LDPC decoder decodes at
//! once (with the same code), each demapped, LDPC and BCH decoded by one of the decoder threads, then the BBFRAMEs in
//! order. So at low symbol rates each frame's decoded on its own, as soon as it arrives, and at high ones the batches
//! fill. Packets are delivered in the thread calling process()
class MODEMDVB_API Receiver
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback;
enum class State { Spectrum = 0, Searching = 1, Frames = 2, Locked = 3 };
struct Status
{
State m_state = State::Spectrum;
dvbs::FrontEnd::Status m_frontEnd;
PLDecoder::Status m_pl;
double m_symbolRate = 0.0;
double m_frequencyOffset = 0.0; //!< Hz, of the carrier from the channel's centre
double m_clockOffset = 0.0; //!< ppm
double m_rollOff = 0.35; //!< The matched filter's
//! From the BBHEADERs: 0 0.35, 1 0.25, 2 0.20, S2X's 4 0.15, 5 0.10, 6 0.05, -1 not yet
int m_matypeRollOff = -1;
bool m_multipleStreams = false;
bool m_acm = false; //!< VCM or ACM, from the BBHEADER
int m_isi = -1; //!< Input stream being decoded (time slicing's: the time slice's TSN)
uint64_t m_streamPackets = 0; //!< Packets of it, since it was chosen: Locked needs some (m_bb's are all time's)
int m_isiCount = 0; //!< Input streams seen
std::vector<int> m_isis; //!< Their identifiers (or TSNs), in order
uint64_t m_fecFrames = 0; //!< Decoded
uint64_t m_ldpcFailures = 0; //!< Frames LDPC didn't converge on (BCH may still correct them)
uint64_t m_fecErrors = 0; //!< Decodes that failed (an exception, as running out of memory): their frames lost
uint64_t m_bchFailures = 0;
uint64_t m_bchCorrected = 0; //!< Bits
double m_ldpcIterations = 0.0; //!< Recent average
uint64_t m_otherStreams = 0; //!< BBFRAMEs of other input streams, skipped
dvbt2::BBFrameDecoder::Stats m_bb;
uint64_t m_queueWaits = 0;
uint64_t m_restarts = 0; //!< Acquisitions started again (the spectrum measured again), without lock
uint64_t m_searchedSymbols = 0; //!< Searched for headers, not locked
uint64_t m_skippedSymbols = 0; //!< Not searched, to keep to the search's budget
double m_seconds[4] = {0.0, 0.0, 0.0, 0.0}; //!< Front end, physical layer, demapping with LDPC and BCH, BBFRAMEs
};
//! decoderThreads: for LDPC decoding in all, 0 for all but two of the cores, up to 4
explicit Receiver(bool threaded = true, int decoderThreads = 0);
~Receiver();
Receiver(const Receiver&) = delete;
Receiver& operator=(const Receiver&) = delete;
//! The channel's sample rate, from 1.5 (1 + roll-off, for the matched filter) to 64 times the symbol rate (twice
//! or more is best). Acquires again
void configure(double inputRate, double symbolRate);
//! The matched filter's roll-off, or 0 to follow the BBHEADER's (starting at 0.35). From any thread
void setRollOff(double rollOff) { m_rollOffSetting = rollOff; }
//! Input stream to decode, in a multiple input stream (or time slice, by its TSN), or -1 for the first. From any thread
void setISI(int isi) { m_isiSetting = isi; }
//! Gold code n of the physical layer scrambling. Acquires again
void setScramblingCode(int n);
void setMaxIterations(int iterations) { m_maxIterations = iterations; }
void setPacketCallback(PacketCallback callback) { m_packetCallback = callback; }
void reset();
void process(const Cf *samples, int nbSamples);
//! The front end shared with the DVB-S receiver (by dvbs::SharedFrontEnd, which runs it and gives this its symbols
//! by processSymbols()), or this receiver's own again (nullptr). With neither demodulating
void setSharedFrontEnd(dvbs::FrontEnd *frontEnd);
//! The shared front end's symbols, in this receiver's thread (the front end's own, or one of its own beside it):
//! first, the front end's index of the first of them (of all it's made); frontEnd, its status as it made them;
//! corrections, how many of the frequency corrections this asked for it had made by then, the last from symbol
//! boundary; otherLoad, the cores the rest of this thread uses (the front end and DVB-S, if in it), so the search's
//! budget is what's left
void processSymbols(const Cf *symbols, int nbSymbols, uint64_t first, const dvbs::FrontEnd::Status& frontEnd,
uint64_t corrections, uint64_t boundary, double otherLoad);
//! Shared: whether this has asked for the spectrum to be measured again (taken), and has a lock it'd lose. From
//! any thread
bool takeFrontEndRestart() { return m_frontEndRestart.exchange(false); }
bool frontEndLocked() const { return m_plLocked; }
double frontEndEsN0() const { return m_esn0; }
//! The timing loop's bandwidth while searching: wide to acquire, but below -3 dB Es/N0 (from the spectrum), as it
//! would track at (at VL-SNR's lowest SNRs, the wide loop wanders thousands of ppm, too far for any header to be found)
static double searchTimingBandwidth(double spectralEsN0);
//! Seconds without a lock after which the receiver starts again (the spectrum measured again): 3, or at low symbol
//! rates as long as the slowest acquisitions take (VL-SNR's, time slicing's near threshold). A restart sooner,
//! here or by the receiver's owner, never locks to them
static double relockSeconds(double symbolRate);
//! Shared, for the front end's owner to make, from its thread: the matched filter's roll-off wanted (0 for none
//! yet), and the large frequency corrections asked for (how many, and their sum, Hz: both only grow)
double frontEndRollOff() const { return m_frontEndRollOff; }
uint64_t frequencyRequests() const { return m_frequencyRequests; }
double frequencyRequested() const { return m_frequencyRequested; }
//! A shared front end's owner: the clock correction asked for (ppm), taken
double takeClockRequest() { return m_clockRequested.exchange(0.0); }
//! Shared, in its thread: the symbols from here don't follow on from the last (samples were dropped)
void discontinuity();
//! Its own front end's equaliser on (the default) or off (a shared front end's is set there)
void setEqualiser(bool enabled) { m_frontEnd.setEqualiser(enabled); }
//! The packets decoded, to the callback, in the caller's thread (process() does it unshared)
void deliverPackets();
//! Wait for everything queued to be decoded, and deliver the packets
void flush();
Status getStatus() const;
//! Up to maxCells of the last frame's data cells, and the input's spectrum (dB, points from -rate/2 to +rate/2)
void getDisplayData(std::vector<Cf>& cells, int maxCells, std::vector<float>& spectrumDb, int points) const;
private:
struct FrameSlot
{
bool m_reset = false; //!< Not a frame: decoding starts again
bool m_skip = false; //!< Not decodable
uint64_t m_epoch = 0;
XFECFrame m_frame;
std::vector<int8_t> m_llrs;
std::vector<uint8_t> m_packed; //!< BCH codeword, hard decisions, corrected
int m_corrected = 0; //!< By BCH, -1 if it couldn't
};
//! What a code's frames need, shared by the decoder threads
struct Code
{
std::vector<int> m_map;
std::vector<Cf> m_points;
DemapTable m_demapTable; //!< For 128 and 256APSK
int m_eta = 2;
std::unique_ptr<dvbt2::BCH> m_bch;
int m_kldpc = 0;
int m_kbch = 0; //!< A VL-SNR frame's less its shortening
int m_vlsnr = -1; //!< A VL-SNR frame's header's index
};
struct Scratch : public dvbt2::LDPCPool::Scratch
{
std::vector<int8_t> m_cellLLRs; //!< A VL-SNR QPSK frame's, of its bits sent
};
void demodulate(const Cf *samples, int nbSamples);
//! The front end's symbols in m_symbols: the physical layer, and the frames to the error correction
void demodulateSymbols(double start, double mid);
void restartFrontEnd();
void onFrame(XFECFrame& frame);
void decodeFrames(std::vector<FrameSlot*>& frames);
//! decodeFrames, an exception (LDPCPool::run rethrows a decoder thread's, as bad_alloc) caught: the frames lost,
//! the BBFRAMEs discontinuous, a reset among them made, counted (m_fecErrors)
void decodeFramesCaught(std::vector<FrameSlot*>& frames);
//! The error correction's state reset, where a reset slot is reached
void resetDecoding(const FrameSlot& slot);
void decodeBatch(dvbt2::LDPC& ldpc, dvbt2::LDPCPool::Context& context, std::vector<FrameSlot*>& frames, size_t first,
size_t count, const Code& code);
const Code& code(const XFECFrame& frame);
//! A VL-SNR frame's LLRs: its shortened bits known zeros, its punctured ones unknown, its spread ones summed
void vlsnrLLRs(const XFECFrame& frame, const Code& code, int8_t *llrs, int n, Scratch& scratch);
void deframe(FrameSlot& slot);
//! No input stream chosen, none seen (after a reset)
void clearStreams();
void runFEC();
bool idle() const;
FrameSlot *freeSlot();
static double now();
static double rollOffValue(int ro);
static constexpr int m_defaultMaxIterations = 50;
static constexpr int m_vlsnrIterations = 75;
static constexpr float m_outlierThreshold = 16.0f; //!< As DVB-T2's
static constexpr double m_timingAcquire = 2e-3;
static constexpr double m_timingTrack = 5e-4;
static constexpr double m_minRelockSeconds = 3.0; //!< Without frames, before the spectrum's measured again (at least)
static constexpr int m_maxFailedFrames = 32; //!< Locked, but frames failing in a row: perhaps a false lock, if
static constexpr double m_searchBudget = 0.4; //!< Of a core, at most, for the physical layer's search (symbols skipped)
static constexpr double m_threadBudget = 0.6; //!< Of a core, for the thread as a whole: the search has what's left,
//!< after the front end (and in Auto, the DVB-S receiver)
static constexpr double m_minSearchBudget = 0.1; //!< Even so
static constexpr double m_steadyLockSeconds = 2.0; //!< Locked this long, the search's budget starts again from nothing
static constexpr double m_maxSkipSeconds = 0.25; //!< Of symbols the budget skips after a window, at most: one that cost
//!< far too much (a fault) mustn't stop the search for long
static constexpr int64_t m_searchWindow = 131072; //!< Symbols searched at a time: a few of the longest frames, so a header
//!< and the next are found in it
static constexpr int m_lowRollOffFrames = 16; //!< Trusted frames without an RO of 11, after which S2X's low roll-offs have ended
static constexpr double m_failingSeconds = 1.0; //!< Failing for this long too
static constexpr uint64_t m_nullOnlyPackets = 1000; //!< Of an input stream chosen automatically, all null: another's chosen
static constexpr uint64_t m_missingStreamFrames = 2000; //!< Of other streams, without the one chosen automatically: it's
//!< gone (the transmitter's changed its streams), another's chosen
static constexpr size_t m_minTakeBatches = 4; //!< The FEC thread takes at most whole rounds of its decoders' batches,
//!< at least this many: more, and the last round leaves decoders idle
static constexpr size_t m_maxLanes = 32; //!< The LDPC decoder's widest batch (AVX2's), for the slots made at first
bool m_threaded;
double m_inputRate;
double m_symbolRate;
std::atomic<double> m_rollOffSetting;
std::atomic<int> m_isiSetting;
std::atomic<int> m_maxIterations;
std::atomic<int> m_matypeRollOff; //!< From the BBHEADERs, for the front end
// The FEC thread's (reset where it reaches a reset, not by the caller of reset())
bool m_lowRollOffs; //!< A BBHEADER's RO was 11: S2X's low roll-offs, the others' codes theirs
int m_sinceLowRollOffs; //!< Trusted frames since an RO of 11: many, the low roll-offs have ended
PacketCallback m_packetCallback;
// Demodulation, in its own thread if threaded
dvbs::FrontEnd m_frontEnd;
dvbs::FrontEnd *m_fe; //!< m_frontEnd, or a shared one
std::atomic<bool> m_frontEndRestart; //!< Shared: asked for
std::atomic<bool> m_plLocked;
std::atomic<double> m_esn0{0.0}; //!< The physical layer's, for the shared front end's equaliser
// Shared: the front end's status with the symbols, and what's asked of its owner (the front end's in another thread)
dvbs::FrontEnd::Status m_feStatus;
std::atomic<double> m_frontEndRollOff{0.0};
std::atomic<uint64_t> m_frequencyRequests{0};
std::atomic<double> m_frequencyRequested{0.0};
std::atomic<double> m_clockRequested{0.0}; //!< Of a shared front end, by its owner
uint64_t m_awaitClock = 0; //!< A shared front end's clock adjustment count that says it's made (0: none)
uint64_t m_awaitCorrection = 0; //!< The frequency request whose corrected symbols are awaited, 0 for none
double m_pendingCorrection = 0.0; //!< Cycles a symbol: turned here, till then
double m_pendingPhase = 0.0; //!< Radians, of that turning
bool shared() const { return m_fe != &m_frontEnd; }
const dvbs::FrontEnd::Status& frontEndStatus() const { return shared() ? m_feStatus : m_frontEnd.getStatus(); }
PLDecoder m_pl;
std::vector<Cf> m_symbols;
uint64_t m_unlockedSymbols; //!< Since the physical layer was last locked
// The search's CPU budget: its time and symbols over a window, and the symbols still to skip
double m_searchSeconds = 0.0;
double m_otherLoad = 0.0; //!< Cores the rest of the thread uses (its own front end's, or as set)
int64_t m_searchSymbols = 0;
int64_t m_skipSymbols = 0;
int64_t m_lockedSymbols = 0; //!< Since the search last ran, for its budget to start again after a steady lock
std::atomic<int> m_failedFrames; //!< FECFRAMEs in a row that BCH or the BBHEADER's CRC failed (FEC thread)
uint64_t m_failingSymbols; //!< Locked, since that reached m_maxFailedFrames
uint64_t m_restarts; //!< Acquisitions started again (demodulation thread)
std::unique_ptr<SampleThread> m_demodThread;
mutable std::mutex m_demodMutex; //!< For these
Status m_demodStatus;
mutable std::atomic<bool> m_displayRequested;
mutable int m_displayMaxCells;
mutable int m_displayPoints;
std::vector<Cf> m_displayCells;
std::vector<float> m_displaySpectrum;
// Error correction
std::unique_ptr<dvbt2::LDPCPool> m_pool;
dvbt2::LDPCPool::Context m_context; //!< The worker's
std::map<int, std::unique_ptr<Code>> m_codes; //!< By MODCOD and frame size
std::thread m_fecThread;
mutable std::mutex m_mutex; //!< For the queue, packets and status below
std::condition_variable m_available;
std::condition_variable m_space;
std::condition_variable m_idle;
std::deque<FrameSlot*> m_queue;
std::vector<std::unique_ptr<FrameSlot>> m_slots;
std::vector<FrameSlot*> m_free;
std::vector<FrameSlot*> m_pending; //!< Unthreaded: frames from this call
bool m_busy;
bool m_stop;
bool m_draining; //!< Flushing: what's queued decoded without waiting for more
std::atomic<uint64_t> m_resetEpoch; //!< Set with m_mutex, read without it by the FEC thread too
uint64_t m_decodeEpoch;
dvbt2::BBFrameDecoder m_bbDecoder;
std::atomic<int> m_isi; //!< Being decoded, -1 until one's chosen (written in the error correction thread)
std::vector<bool> m_isisSeen;
std::vector<bool> m_isisNullOnly; //!< Chosen automatically, and found to carry only null packets
uint64_t m_streamPackets; //!< Of the stream being decoded, since it was chosen (FEC thread)
uint64_t m_isiPackets; //!< Of the stream chosen automatically, since chosen, and of them null
uint64_t m_isiNulls;
uint64_t m_isiMissing; //!< Trusted frames of other streams since the one chosen automatically was seen
std::vector<uint8_t> m_bbPackets;
std::vector<uint8_t> m_packets; //!< For the caller's thread
std::vector<uint8_t> m_delivering;
Status m_fecStatus;
uint64_t m_queueWaits;
static constexpr size_t m_maxQueued = 160; //!< 5 batches of 32 lanes: across the decoder threads and this one when the frames are coming faster than they're decoded (3 left a laptop's fourth decoder thread idle, and DVB-S2 waiting a quarter of its time)
size_t m_takeBatches = 1; //!< The most the FEC thread takes from the queue at once, in batches
static constexpr int m_batchWaitMs = 100; //!< The longest a frame waits for a batch to fill
};
} // namespace dvbs2
#endif // MODEMDVB_DVBS2RECEIVER_H

3015
modemdvb/dvbs2xldpctables.cpp

File diff suppressed because it is too large Load Diff

520
modemdvb/dvbsfec.cpp

@ -0,0 +1,520 @@ @@ -0,0 +1,520 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <chrono>
#include "dvbsfec.h"
namespace dvbs {
double FECDecoder::now()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
FECDecoder::FECDecoder() :
m_codeRateSetting(-1),
m_conjugateFirst(false),
m_period(1),
m_periodFill(0),
m_encoderState(0),
m_unsyncedBits(0),
m_failedRun(0),
m_berErrors(0),
m_berTotal(0)
{
reset();
}
void FECDecoder::setCodeRate(int rate)
{
// Out of range: found (rather than taken as the nearest)
if ((rate < (int) CodeRate::R1_2) || (rate > (int) CodeRate::R7_8)) {
rate = -1;
}
if (rate != m_codeRateSetting)
{
m_codeRateSetting = rate;
reset();
}
}
void FECDecoder::reset()
{
const uint64_t searches = m_status.m_searches;
const uint64_t locks = m_status.m_locks;
double seconds[3];
std::copy(m_status.m_seconds, m_status.m_seconds + 3, seconds);
m_status = Status();
m_status.m_searches = searches;
m_status.m_locks = locks;
std::copy(seconds, seconds + 3, m_status.m_seconds);
m_search.clear();
m_history.clear();
// m_skipSymbols isn't: the receiver resets the FEC at each restart, hundreds of times a second without a signal
m_outer.reset();
m_viterbi.reset();
m_hard.clear();
m_periodFill = 0;
m_encoderState = 0;
m_unsyncedBits = 0;
m_failedRun = 0;
m_berErrors = 0;
m_berTotal = 0;
m_berOuter = m_outer.getStats();
// The outer decoder's counts go on from reset to reset (as the searches and locks do): what's new is measured from
// them, so a lock doesn't take the totals for new packets (and unlock at once, after packets that failed before)
m_status.m_outer = m_outer.getStats();
}
void FECDecoder::rotate(const int8_t *iq, int nbSymbols, int rotation, bool conjugate, int8_t *out)
{
// Undoing a quarter turn: (I + jQ)(-j) = Q - jI. The soft values are positive for 0, so for +1. Conjugated first:
// Q and -Q swapped
for (int i = 0; i < nbSymbols; i++)
{
const int8_t a = iq[2 * i], q = iq[2 * i + 1];
const int8_t na = (int8_t) (a == -128 ? 127 : -a), nq = (int8_t) (q == -128 ? 127 : -q);
const int8_t b = conjugate ? nq : q, nb = conjugate ? q : nq;
switch (rotation & 3)
{
case 0: out[2 * i] = a; out[2 * i + 1] = b; break;
case 1: out[2 * i] = b; out[2 * i + 1] = na; break;
case 2: out[2 * i] = na; out[2 * i + 1] = nb; break;
default: out[2 * i] = nb; out[2 * i + 1] = a; break;
}
}
}
void FECDecoder::decode(const int8_t *iq, int nbSymbols)
{
if (nbSymbols <= 0) {
return;
}
// A symbol rate set since: the skip measured at the last isn't this one's
const double rate = m_requestedSymbolRate;
if (rate != m_symbolRate)
{
m_symbolRate = rate;
m_skipSymbols = 0;
}
if (m_status.m_state == State::Searching)
{
// Skipped, to keep to the search's budget
if (m_skipSymbols > 0)
{
const int skip = (int) std::min<int64_t>(m_skipSymbols, nbSymbols);
m_skipSymbols -= skip;
iq += 2 * skip;
nbSymbols -= skip;
}
m_search.insert(m_search.end(), iq, iq + 2 * nbSymbols);
while ((m_status.m_state == State::Searching) && ((int) m_search.size() >= 2 * m_searchSymbols)) {
search();
}
return;
}
// The last block's soft bits, to search again from at once
m_history.insert(m_history.end(), iq, iq + 2 * nbSymbols);
if ((int) m_history.size() > 2 * m_searchSymbols) {
m_history.erase(m_history.begin(), m_history.end() - 2 * m_searchSymbols);
}
m_rotated.resize(2 * nbSymbols);
rotate(iq, nbSymbols, m_hypothesis.m_rotation, m_hypothesis.m_conjugate, m_rotated.data());
decodeSerial(m_rotated.data(), 2 * nbSymbols);
if (m_status.m_state == State::Searching)
{
// Lost: search the recent symbols
m_search.swap(m_history);
m_history.clear();
while ((m_status.m_state == State::Searching) && ((int) m_search.size() >= 2 * m_searchSymbols)) {
search();
}
}
}
double FECDecoder::test(const int8_t *serial, int nbBits, CodeRate rate, int offset, std::vector<uint8_t> *bits)
{
std::vector<PunctureEntry> pattern;
int period;
puncturePattern(rate, pattern, period);
const int length = (int) pattern.size();
const int periods = (nbBits - offset) / length;
const int steps = periods * period;
m_xy.assign(2 * steps, 0);
for (int g = 0; g < periods; g++)
{
const int8_t *s = serial + offset + g * length;
int8_t *xy = m_xy.data() + 2 * g * period;
for (int j = 0; j < length; j++) {
xy[2 * pattern[j].m_bit + pattern[j].m_y] = s[j];
}
}
m_testViterbi.reset();
m_bits.clear();
m_testViterbi.decode(m_xy.data(), steps, m_bits);
const int decoded = (int) m_bits.size();
uint8_t state = 0;
m_encoded.clear();
dvbt::Viterbi27::encode(m_bits.data(), decoded, state, m_encoded);
int matches = 0, total = 0;
for (int i = m_skipSteps; i < decoded; i++)
{
for (int y = 0; y < 2; y++)
{
const int8_t soft = m_xy[2 * i + y];
if (soft != 0)
{
total++;
matches += (soft < 0 ? 1 : 0) == m_encoded[2 * i + y];
}
}
}
if (bits) {
*bits = m_bits;
}
return total > 0 ? (double) matches / total : 0.0;
}
int FECDecoder::syncBytes(const std::vector<uint8_t>& bits, bool& inverted, int& positions)
{
const int packetBits = dvbt::rsPacketSize * 8;
const int n = (int) bits.size();
int best = 0;
inverted = false;
positions = 0;
for (int offset = 0; offset < packetBits; offset++)
{
int c47 = 0, cB8 = 0, count = 0;
for (int k = offset; k + 8 <= n; k += packetBits, count++)
{
uint8_t byte = 0;
for (int b = 0; b < 8; b++) {
byte = (uint8_t) ((byte << 1) | (bits[k + b] & 1));
}
c47 += byte == 0x47;
cB8 += byte == 0xB8;
}
if (c47 + cB8 > best)
{
best = c47 + cB8;
inverted = cB8 > c47;
positions = count;
}
}
return best;
}
void FECDecoder::search()
{
const double start = now();
m_status.m_searches++;
const int nbSymbols = m_searchSymbols;
const int nbBits = 2 * nbSymbols;
std::vector<CodeRate> rates;
if (m_codeRateSetting >= 0) {
rates.push_back((CodeRate) m_codeRateSetting);
} else {
rates = {CodeRate::R1_2, CodeRate::R2_3, CodeRate::R3_4, CodeRate::R5_6, CodeRate::R7_8};
}
// Each hypothesis decoded, and the agreement of what it decoded with what was received (for the status, and to skip
// the hopeless). Agreement can't choose between code rates: at low SNR a heavily punctured code, whose codewords are
// dense, can match a rate 1/2 stream more closely than its own code does. So the decoded bits' sync bytes decide:
// 0x47 (or 0xB8) every 204 bytes, which only the right hypothesis decodes
Hypothesis best;
double bestFraction = 0.0;
double bestAgreement = 0.0;
bool bestInverted = false;
std::vector<int8_t> rotated(nbBits);
std::vector<uint8_t> bits;
m_status.m_lastSyncs = 0;
m_status.m_lastPositions = 0;
// The spectrum conjugated only if nothing's found the other way (the way last locked first)
for (int pass = 0; (pass < 2) && (bestFraction == 0.0); pass++)
{
const bool conjugate = pass == 0 ? m_conjugateFirst : !m_conjugateFirst;
for (int rotation = 0; rotation < 2; rotation++)
{
rotate(m_search.data(), nbSymbols, rotation, conjugate, rotated.data());
for (CodeRate rate : rates)
{
const int length = punctureLength(rate);
for (int offset = 0; offset < length; offset++)
{
const double agreement = test(rotated.data(), nbBits, rate, offset, &bits);
bestAgreement = std::max(bestAgreement, agreement);
if (agreement < m_minAgreement) {
continue;
}
bool inverted;
int positions;
const int syncs = syncBytes(bits, inverted, positions);
const double fraction = positions > 0 ? (double) syncs / positions : 0.0;
if ((syncs >= 3) && (fraction >= 0.6)
&& ((fraction > bestFraction) || ((fraction == bestFraction) && (agreement > best.m_agreement))))
{
best.m_rotation = rotation;
best.m_conjugate = conjugate;
best.m_rate = rate;
best.m_offset = offset;
best.m_agreement = agreement;
bestFraction = fraction;
bestInverted = inverted;
m_status.m_lastSyncs = syncs;
m_status.m_lastPositions = positions;
}
}
}
}
}
m_status.m_lastAgreement = bestAgreement;
const bool found = bestFraction > 0.0;
if (found && bestInverted) {
best.m_rotation += 2;
}
m_status.m_seconds[0] += now() - start;
if (found)
{
lock(best);
// From the start of the block, rotated as found, from the start of its first whole period
std::vector<int8_t> serial(m_search.size());
rotate(m_search.data(), (int) m_search.size() / 2, best.m_rotation, best.m_conjugate, serial.data());
m_search.clear();
decodeSerial(serial.data() + best.m_offset, (int) serial.size() - best.m_offset);
}
else
{
// Try again with half of the block, and as many new symbols. Unless searching would take more than its share of
// real time at that rate (at high symbol rates): then with symbols further on, those between skipped
const double rate = m_symbolRate;
const double seconds = now() - start;
const int64_t wanted = rate > 0.0 ? (int64_t) (seconds * rate / m_searchBudget) : 0;
const int64_t advance = std::max<int64_t>(nbSymbols / 2, wanted);
const int64_t held = (int64_t) m_search.size() / 2;
if (advance < held)
{
m_search.erase(m_search.begin(), m_search.begin() + 2 * advance);
}
else
{
m_search.clear();
m_skipSymbols = advance - held;
}
}
}
void FECDecoder::lock(const Hypothesis& hypothesis)
{
m_hypothesis = hypothesis;
m_conjugateFirst = hypothesis.m_conjugate;
puncturePattern(hypothesis.m_rate, m_pattern, m_period);
m_periodBits.assign(m_pattern.size(), 0);
m_periodFill = 0;
m_viterbi.reset();
m_outer.reset();
m_hard.clear();
m_encoderState = 0;
m_unsyncedBits = 0;
m_failedRun = 0;
m_berErrors = 0;
m_berTotal = 0;
m_berOuter = m_outer.getStats();
m_status.m_state = State::Locked;
m_status.m_codeRateKnown = true;
m_status.m_codeRate = hypothesis.m_rate;
m_status.m_rotation = hypothesis.m_rotation;
m_status.m_inverted = hypothesis.m_conjugate;
m_status.m_agreement = hypothesis.m_agreement;
m_status.m_locks++;
}
void FECDecoder::decodeSerial(const int8_t *serial, int nbBits)
{
const int length = (int) m_pattern.size();
// The periods completed, sized at once (the punctured bits left 0)
const int periods = (m_periodFill + std::max(0, nbBits)) / length;
m_xy.assign(2 * (size_t) periods * m_period, 0);
int8_t *xy = m_xy.data();
const PunctureEntry *pattern = m_pattern.data();
for (int i = 0; i < nbBits; i++)
{
m_periodBits[m_periodFill++] = serial[i];
if (m_periodFill == length)
{
for (int j = 0; j < length; j++) {
xy[2 * pattern[j].m_bit + pattern[j].m_y] = m_periodBits[j];
}
xy += 2 * m_period;
m_periodFill = 0;
}
}
const int steps = (int) m_xy.size() / 2;
if (steps == 0) {
return;
}
// Hard decisions, for the BER before Viterbi
for (int i = 0; i < steps; i++)
{
const int8_t x = m_xy[2 * i], y = m_xy[2 * i + 1];
m_hard.push_back((uint8_t) ((x < 0 ? 1 : 0) | (y < 0 ? 2 : 0) | (x != 0 ? 4 : 0) | (y != 0 ? 8 : 0)));
}
double start = now();
m_bits.clear();
m_viterbi.decode(m_xy.data(), steps, m_bits);
measureBER(m_bits);
m_status.m_viterbiBits += m_bits.size();
double mid = now();
m_status.m_seconds[1] += mid - start;
const uint64_t packetsBefore = m_status.m_outer.m_packets, failedBefore = m_status.m_outer.m_rsFailed;
m_outer.decode(m_bits.data(), (int) m_bits.size());
m_status.m_seconds[2] += now() - mid;
m_status.m_outer = m_outer.getStats();
m_status.m_tsSync = m_outer.synced();
const uint64_t newPackets = m_status.m_outer.m_packets - packetsBefore, newFailed = m_status.m_outer.m_rsFailed - failedBefore;
// RS failures are counted for packets dropped before the outer decoder's synchronised too, which aren't counted
// as packets: as many failures as packets, or more, is a run of them
if (newPackets > 0) {
m_failedRun = newFailed >= newPackets ? m_failedRun + (int) newPackets : 0;
}
// Searching again if the outer decoder can't synchronise, or RS fails packet after packet
if (m_outer.synced()) {
m_unsyncedBits = 0;
} else {
m_unsyncedBits += m_bits.size();
}
if ((m_unsyncedBits > m_maxUnsyncedPackets * dvbt::rsPacketSize * 8) || (m_failedRun >= m_maxFailedRun))
{
m_status.m_state = State::Searching;
m_status.m_tsSync = false;
m_search.clear();
m_unsyncedBits = 0;
m_failedRun = 0;
}
// BER after Viterbi, from the bits Reed-Solomon corrected, over the packets since the window started
const dvbt::OuterDecoder::Stats& stats = m_status.m_outer;
const uint64_t packets = (stats.m_packets - m_berOuter.m_packets);
if (packets >= 1000)
{
const uint64_t bits = stats.m_rsBitsCorrected - m_berOuter.m_rsBitsCorrected;
const uint64_t failed = stats.m_rsFailed - m_berOuter.m_rsFailed;
// A packet RS couldn't correct had more than 8 bytes wrong: count it as 9 bytes of 4 bits
m_status.m_postViterbiBER = (bits + failed * 36.0) / (packets * dvbt::rsPacketSize * 8.0);
m_berOuter = stats;
}
}
// Re-encode the decoded bits and compare them with the hard decisions of what was received
void FECDecoder::measureBER(const std::vector<uint8_t>& bits)
{
const int n = std::min((int) bits.size(), (int) m_hard.size());
if (n <= 0) {
return;
}
m_encoded.clear();
dvbt::Viterbi27::encode(bits.data(), n, m_encoderState, m_encoded);
uint64_t errors = 0, total = 0;
for (int i = 0; i < n; i++)
{
const uint8_t h = m_hard[i];
if (h & 4)
{
total++;
errors += (h & 1) != m_encoded[2 * i];
}
if (h & 8)
{
total++;
errors += ((h >> 1) & 1) != m_encoded[2 * i + 1];
}
}
m_hard.erase(m_hard.begin(), m_hard.begin() + n);
m_berErrors += errors;
m_berTotal += total;
if (m_berTotal >= m_berWindow)
{
m_status.m_preViterbiBER = (double) m_berErrors / m_berTotal;
m_berErrors = 0;
m_berTotal = 0;
}
else if (m_status.m_preViterbiBER == 0.0 && m_berTotal >= 20000)
{
m_status.m_preViterbiBER = (double) m_berErrors / m_berTotal;
}
}
} // namespace dvbs

158
modemdvb/dvbsfec.h

@ -0,0 +1,158 @@ @@ -0,0 +1,158 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBSFEC_H
#define MODEMDVB_DVBSFEC_H
#include <atomic>
#include <cstdint>
#include <functional>
#include <vector>
#include "dvbs.h"
#include "dvbtouter.h"
#include "viterbi27.h"
namespace dvbs {
//! DVB-S error correction (EN 300 421 4.4): from the soft bits of the QPSK symbols, as demodulated, to transport stream
//! packets. Depuncturing, K = 7 Viterbi decoding, sync byte search, Forney de-interleaving, RS(204,188) and
//! descrambling, the last four DVB-T's (dvbt::OuterDecoder).
//!
//! The carrier loop can lock at any of four phases, and the code rate and where its puncturing periods start aren't
//! known. A rotation of 180 degrees inverts every bit, which the code is transparent to (its generators have odd
//! weight): the decoded stream is inverted, its sync bytes 0xB8 and every eighth 0x47. A rotation of 90 degrees swaps
//! X and Y and inverts one, which nothing decodes. So 2 rotations, each code rate (or the one set) and each start of its
//! period are tried on a block of symbols: each is depunctured, decoded, re-encoded and compared with what was
//! received. The right one agrees with all but the channel's errors, the others about half. Then the sync bytes of what
//! it decoded say whether it's inverted.
//!
//! The spectrum may be inverted too (a high side LO, I and Q swapped): the symbols are conjugated, (I, -Q), which isn't
//! a rotation, though the carrier loop locks to it as well (QPSK conjugated is QPSK). So if nothing's found, the block's
//! tried again conjugated (before the rotation), costing a second pass only of searches that fail. The way that last
//! locked is tried first.
class MODEMDVB_API FECDecoder
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback;
enum class State { Searching = 0, Locked = 1 };
struct Status
{
State m_state = State::Searching;
bool m_tsSync = false;
bool m_codeRateKnown = false;
CodeRate m_codeRate = CodeRate::R1_2;
int m_rotation = 0; //!< Quarter turns undone (after any conjugation)
bool m_inverted = false; //!< The spectrum's inverted: the symbols are conjugated
double m_agreement = 0.0; //!< Of the hypothesis found, with what was received
double m_lastAgreement = 0.0; //!< Of the best hypothesis of the last search
int m_lastSyncs = 0; //!< Sync bytes it decoded, of
int m_lastPositions = 0;
dvbt::OuterDecoder::Stats m_outer;
double m_preViterbiBER = 0.0; //!< Recent average, from re-encoding the decoded bits
double m_postViterbiBER = 0.0; //!< Recent average, from what Reed-Solomon corrects
uint64_t m_viterbiBits = 0;
uint64_t m_searches = 0;
uint64_t m_locks = 0;
double m_seconds[3] = {0.0, 0.0, 0.0}; //!< Searching, Viterbi (and BER), outer decoding
};
FECDecoder();
void setPacketCallback(PacketCallback callback) { m_outer.setPacketCallback(callback); }
//! For the search's CPU budget: symbols are skipped between searches that find nothing, so it uses a fixed share
//! of real time. 0 (the default) for no skipping
//! The search's skip is in symbols at the rate it was measured at: none at another. From any thread: taken (and the
//! skip cleared) by the thread decoding, which alone uses the skip
void setSymbolRate(double rate) { m_requestedSymbolRate = rate; }
//! The code rate, or -1 to find it (as is any other value out of range). Searches again
void setCodeRate(int rate);
void reset();
//! The soft I and Q of each symbol (int8, positive for 0, 0 for nothing known), as demodulated: whichever rotation
//! the carrier locked at
void decode(const int8_t *iq, int nbSymbols);
const Status& getStatus() const { return m_status; }
static constexpr int m_searchSymbols = 8192; //!< Tried at a time
private:
struct Hypothesis
{
int m_rotation = 0;
bool m_conjugate = false; //!< Before the rotation
CodeRate m_rate = CodeRate::R1_2;
int m_offset = 0; //!< Of the first bit of a puncturing period in the block's serial bits
double m_agreement = 0.0;
};
static constexpr double m_minAgreement = 0.75; //!< Of a hypothesis with what was received, to look for its sync bytes
static constexpr int m_skipSteps = 32; //!< At the start of a test, while the decoder's metrics settle
static constexpr uint64_t m_maxUnsyncedPackets = 24; //!< Decoded without the outer decoder's sync, before searching again
static constexpr int m_maxFailedRun = 6; //!< Consecutive packets RS can't correct (as after a cycle slip), the same
static constexpr double m_searchBudget = 0.25; //!< Of a core, at most, searching without finding (symbols are skipped)
static constexpr uint64_t m_berWindow = 2000000; //!< Bits compared for the BER before Viterbi
static double now();
void search();
//! Depuncture serial soft bits from offset, decode, re-encode and compare. bits: the decoded bits, if not null
double test(const int8_t *serial, int nbBits, CodeRate rate, int offset, std::vector<uint8_t> *bits);
//! The most sync bytes (0x47 or 0xB8) at one alignment, and whether 0xB8 were the most of them
static int syncBytes(const std::vector<uint8_t>& bits, bool& inverted, int& positions);
//! Conjugated (if conjugate), then quarter turns undone
static void rotate(const int8_t *iq, int nbSymbols, int rotation, bool conjugate, int8_t *out);
void lock(const Hypothesis& hypothesis);
void decodeSerial(const int8_t *serial, int nbBits);
void measureBER(const std::vector<uint8_t>& bits);
dvbt::Viterbi27 m_viterbi;
dvbt::Viterbi27 m_testViterbi; //!< For the search's tests
dvbt::OuterDecoder m_outer;
std::atomic<double> m_requestedSymbolRate{0.0}; //!< Set from any thread
double m_symbolRate = 0.0; //!< The decoding thread's
int64_t m_skipSymbols = 0; //!< Still to skip before the next search (the decoding thread's)
int m_codeRateSetting; //!< -1 to find it
bool m_conjugateFirst; //!< Searched first: as last locked (kept through reset(), as the signal likely is)
Status m_status;
std::vector<int8_t> m_search; //!< Serial soft bits (I, Q of each symbol) being searched, unrotated
std::vector<int8_t> m_history; //!< The last block's, when locked, to search again at once if lock's lost
std::vector<int8_t> m_rotated;
std::vector<int8_t> m_xy; //!< Depunctured, X and Y of each step
std::vector<uint8_t> m_bits;
std::vector<uint8_t> m_encoded;
// Locked
Hypothesis m_hypothesis;
std::vector<PunctureEntry> m_pattern;
int m_period;
std::vector<int8_t> m_periodBits; //!< Of the period being received
int m_periodFill;
std::vector<uint8_t> m_hard; //!< Of each step not yet decoded: X, Y, X known, Y known in bits 0..3
uint8_t m_encoderState;
uint64_t m_unsyncedBits; //!< Decoded since the outer decoder was last synchronised
int m_failedRun; //!< Consecutive packets RS couldn't correct
uint64_t m_berErrors;
uint64_t m_berTotal;
dvbt::OuterDecoder::Stats m_berOuter; //!< The outer decoder's counts at the start of the BER window
};
} // namespace dvbs
#endif // MODEMDVB_DVBSFEC_H

950
modemdvb/dvbsfrontend.cpp

@ -0,0 +1,950 @@ @@ -0,0 +1,950 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include "dvbs.h"
#include "dvbsfrontend.h"
#include "resampler.h"
namespace dvbs {
// Raised cosine pulse (the transmit and receive filters together)
double FrontEnd::raisedCosine(double t, double a)
{
const double x = 2.0 * a * t;
const double sinc = std::fabs(t) < 1e-9 ? 1.0 : std::sin(m_pi * t) / (m_pi * t);
if (std::fabs(std::fabs(x) - 1.0) < 1e-9) {
return m_pi / 4.0 * (a > 0.0 ? std::sin(m_pi / (2.0 * a)) / (m_pi / (2.0 * a)) : 1.0);
}
return sinc * std::cos(m_pi * a * t) / (1.0 - x * x);
}
double FrontEnd::besselI0(double x)
{
double sum = 1.0, term = 1.0;
for (int k = 1; k < 50; k++)
{
term *= (x / (2.0 * k)) * (x / (2.0 * k));
sum += term;
if (term < sum * 1e-12) {
break;
}
}
return sum;
}
FrontEnd::FrontEnd() :
m_inputRate(2e6),
m_symbolRate(1e6),
m_rollOff(0.35),
m_samplesPerSymbol(2.0),
m_taps(0),
m_ncoFrequency(0.0),
m_ncoPhase(0.0),
m_frequencyKnown(false),
m_spectrumFill(0),
m_estimateBlocks(0),
m_untilDisplayBlock(0),
m_time(0.0),
m_step(1.0),
m_onSymbol(true),
m_haveLast(false),
m_integrator(0.0),
m_kp(0.0),
m_ki(0.0),
m_bandwidth(2e-3),
m_detectorGain(1.0),
m_power(1.0),
m_powerValid(false),
m_inputPowerSum(0.0),
m_inputPowerCount(0),
m_eqEnabled(true),
m_eqLocked(false),
m_eqEsN0(10.0),
m_eqBlocks(0)
{
m_window.resize(m_fftSize);
m_twiddles.resize(m_fftSize / 2);
m_bitReverse.resize(m_fftSize);
int bits = 0;
while ((1 << bits) < m_fftSize) {
bits++;
}
for (int i = 0; i < m_fftSize; i++)
{
m_window[i] = 0.5 - 0.5 * std::cos(2.0 * m_pi * i / m_fftSize);
int r = 0;
for (int b = 0; b < bits; b++) {
r |= ((i >> b) & 1) << (bits - 1 - b);
}
m_bitReverse[i] = r;
}
for (int i = 0; i < m_fftSize / 2; i++) {
m_twiddles[i] = std::polar(1.0, -2.0 * m_pi * i / m_fftSize);
}
configure(m_inputRate, m_symbolRate, m_rollOff);
}
void FrontEnd::configure(double inputRate, double symbolRate, double rollOff)
{
m_inputRate = inputRate;
m_symbolRate = symbolRate;
m_rollOff = rollOff;
m_samplesPerSymbol = inputRate / symbolRate;
m_step = m_samplesPerSymbol / 2.0;
m_taps = 0;
m_eqGainDb.clear();
makeFilter();
computeGains();
reset();
}
void FrontEnd::setRollOff(double rollOff)
{
if (rollOff == m_rollOff) {
return;
}
// The timing and carrier carry on: only the filter changes
m_rollOff = rollOff;
makeFilter();
computeGains();
}
void FrontEnd::makeFilter()
{
// A low roll-off's pulse falls off over about 1 / roll-off symbols: spanned twice that (at 0.05 and 0.10, MER 19.7
// to 19.8 dB at Es/N0 20, against 19.9 at 0.35; once that, 18.7 to 19.0), at the cost of as many more taps
const double span = std::max(m_filterSpan, 2.0 / std::max(0.01, m_rollOff));
int taps = (int) std::ceil(span * m_samplesPerSymbol);
taps = std::max(8, std::min(1024, (taps + 3) & ~3));
const int oldTaps = m_taps;
m_taps = taps;
const double half = m_taps / 2.0;
const double beta = 5.0;
const double i0Beta = besselI0(beta);
const double sps = m_samplesPerSymbol;
const double a = m_rollOff;
if (m_eqGainDb.empty())
{
dvbt::makePolyphaseFilter(m_taps, m_phases, [half, beta, i0Beta, sps, a](double x) {
const double arg = x / half;
const double window = std::fabs(arg) < 1.0 ? besselI0(beta * std::sqrt(1.0 - arg * arg)) / i0Beta : 0.0;
return rootRaisedCosine(x / sps, a) * window;
}, m_pairs);
}
else
{
// Equalised: the root raised cosine's frequency response times the correction (both even, so the impulse is
// real), its impulse the integral of that by cosines, from points across the signal's band
const double outer = (1.0 + a) / 2.0; // Of the symbol rate
const double binRate = m_inputRate / m_fftSize / m_symbolRate;
const double spacing = outer / m_eqFrequencies / sps; // Of the points, cycles an input sample
std::vector<double> response(m_eqFrequencies);
for (int k = 0; k < m_eqFrequencies; k++)
{
const double f = (k + 0.5) * outer / m_eqFrequencies; // Of the symbol rate
const double inner = (1.0 - a) / 2.0;
const double rc = f <= inner ? 1.0 : f >= outer ? 0.0 : 0.5 * (1.0 + std::cos(m_pi * (f - inner) / a));
const int bin = std::min((int) m_eqGainDb.size() - 1, (int) std::lround(f / binRate));
response[k] = std::sqrt(rc) * std::pow(10.0, m_eqGainDb[bin] / 20.0);
}
// Point k's at (k + 0.5) spacing: its cosine the real part of a phasor turned by 2 pi spacing x a point (in
// double, to about 1e-14 over the 128), rather than a cosine each, taps x phases x 128 of them
dvbt::makePolyphaseFilter(m_taps, m_phases, [&](double x) {
const double arg = x / half;
if (std::fabs(arg) >= 1.0) {
return 0.0;
}
const double window = besselI0(beta * std::sqrt(1.0 - arg * arg)) / i0Beta;
const double stepRe = std::cos(2.0 * m_pi * spacing * x), stepIm = std::sin(2.0 * m_pi * spacing * x);
double re = std::cos(m_pi * spacing * x), im = std::sin(m_pi * spacing * x);
double sum = 0.0;
for (int k = 0; k < m_eqFrequencies; k++)
{
sum += response[k] * re;
const double t = re * stepRe - im * stepIm;
im = re * stepIm + im * stepRe;
re = t;
}
return sum * window;
}, m_pairs);
}
// The output's time is its centre's (the filter's window starts taps / 2 - 1 before it, whatever its length), kept
// so the timing doesn't jump. A longer filter's first taps reach before the history kept: zeros there (the pulse's
// tails, a small transient), and the time moved with the samples (left, the symbols jumped 5 at 2.5 samples a
// symbol as a low roll-off's longer filter came in, and the lock was lost). A shorter one's are within it
const int grow = (m_taps / 2 - 1) - (oldTaps / 2 - 1);
if (oldTaps && (grow > 0) && !m_buffer.empty())
{
m_buffer.insert(m_buffer.begin(), grow, Cf(0.0f, 0.0f));
m_time += grow;
}
}
// The normalised Gardner detector's gain, from the raised cosine pulse, and the loop's gains from it
void FrontEnd::computeGains()
{
auto detector = [this](double tau) {
double e = 0.0, p = 0.0;
for (int m = -40; m <= 40; m++)
{
e += raisedCosine(m - 0.5 + tau, m_rollOff) * (raisedCosine(m + tau, m_rollOff) - raisedCosine(m - 1 + tau, m_rollOff));
p += raisedCosine(m + tau, m_rollOff) * raisedCosine(m + tau, m_rollOff);
}
return e / p;
};
m_detectorGain = (detector(0.01) - detector(-0.01)) / 0.02;
const double zeta = 0.707;
const double theta = m_bandwidth / (zeta + 1.0 / (4.0 * zeta));
const double d = 1.0 + 2.0 * zeta * theta + theta * theta;
m_kp = 4.0 * zeta * theta / d / m_detectorGain;
m_ki = 4.0 * theta * theta / d / m_detectorGain;
}
void FrontEnd::adjustClock(double ppm)
{
m_integrator = std::max(-m_maxClockOffset, std::min(m_maxClockOffset, m_integrator + ppm * 1e-6));
m_status.m_clockAdjustments++;
}
void FrontEnd::setTimingBandwidth(double bnT)
{
if (bnT != m_bandwidth)
{
m_bandwidth = bnT;
computeGains();
}
}
void FrontEnd::reset()
{
m_buffer.assign(m_taps, Cf(0.0f, 0.0f));
m_time = m_taps - 1;
m_onSymbol = true;
m_haveLast = false;
m_lastSymbol = Cf(0.0f, 0.0f);
m_lastMid = Cf(0.0f, 0.0f);
m_integrator = 0.0;
m_power = 1.0;
m_powerValid = false;
m_spectrumFill = 0;
m_spectrumBlock.assign(m_fftSize, Cf(0.0f, 0.0f));
m_estimatePower.assign(m_fftSize, 0.0);
m_estimateBlocks = 0;
m_untilDisplayBlock = 0;
m_ncoPhase = 0.0;
m_inputPowerSum = 0.0;
m_inputPowerCount = 0;
const double power = m_status.m_power;
// The clock adjustments made count on (a receiver may be waiting for one)
const uint64_t clockAdjustments = m_status.m_clockAdjustments;
m_status = Status();
m_status.m_clockAdjustments = clockAdjustments;
m_status.m_power = power;
clearEqualiser();
if (m_frequencyKnown)
{
m_status.m_estimating = false;
m_frequencyKnown = false;
}
else
{
m_ncoFrequency = 0.0;
}
m_status.m_frequencyOffset = m_ncoFrequency;
}
void FrontEnd::setFrequency(double hz)
{
m_ncoFrequency = hz;
m_frequencyKnown = true;
m_status.m_frequencyOffset = hz;
}
int FrontEnd::heldSymbols() const
{
// The next output's at m_time in the buffer
return m_samplesPerSymbol > 0.0 ? std::max(0, (int) std::lround(((double) m_buffer.size() - m_time) / m_samplesPerSymbol)) : 0;
}
void FrontEnd::adjustFrequency(double hz)
{
m_ncoFrequency += hz;
m_status.m_frequencyOffset = m_ncoFrequency;
}
void FrontEnd::fft(std::vector<std::complex<double>>& x) const
{
const int n = m_fftSize;
for (int i = 0; i < n; i++)
{
if (i < m_bitReverse[i]) {
std::swap(x[i], x[m_bitReverse[i]]);
}
}
for (int len = 2; len <= n; len <<= 1)
{
const int half = len / 2;
const int stride = n / len;
for (int i = 0; i < n; i += len)
{
for (int j = 0; j < half; j++)
{
const std::complex<double> t = x[i + j + half] * m_twiddles[j * stride];
x[i + j + half] = x[i + j] - t;
x[i + j] += t;
}
}
}
}
void FrontEnd::setEqualiserReference(bool locked, double esn0)
{
m_eqLocked = locked;
if (locked) {
m_eqEsN0 = esn0;
}
if (!m_eqEnabled && !m_eqGainDb.empty()) {
clearEqualiser();
}
}
void FrontEnd::clearEqualiser()
{
m_eqPower.clear();
m_eqBlocks = 0;
m_status.m_equalising = false;
m_status.m_equaliserEdgeDb = 0.0;
if (!m_eqGainDb.empty())
{
m_eqGainDb.clear();
makeFilter();
}
}
// A spectrum block (of the input, taken for the display), averaged while a receiver's locked, the equaliser updated from
// each m_eqUpdateBlocks
void FrontEnd::accumulateEqualiser(const std::vector<double>& power)
{
if (!m_eqEnabled || !m_eqLocked || (m_eqEsN0 < m_eqMinEsN0) || (m_samplesPerSymbol > m_eqMaxSamplesPerSymbol))
{
m_eqPower.clear();
m_eqBlocks = 0;
return;
}
if (m_eqPower.size() != power.size()) {
m_eqPower.assign(power.size(), 0.0);
}
for (size_t k = 0; k < power.size(); k++) {
m_eqPower[k] += power[k];
}
if (++m_eqBlocks >= m_eqUpdateBlocks)
{
computeEqualiser();
m_eqPower.assign(power.size(), 0.0);
m_eqBlocks = 0;
}
}
// The receive chain's response: the averaged spectrum over what it'd be (the raised cosine at the Es/N0 measured, over
// flat noise), relative to the middle of the band, smoothed, made symmetric (the matched filter's real), and the
// correction its inverse, limited. Averaged with the last, so it changes gradually
void FrontEnd::computeEqualiser()
{
const int n = m_fftSize;
const double binHz = m_inputRate / n;
const double snr = std::pow(10.0, std::min(30.0, m_eqEsN0) / 10.0);
const double inner = (1.0 - m_rollOff) * m_symbolRate / 2.0;
const double outer = (1.0 + m_rollOff) * m_symbolRate / 2.0;
const int maxBin = (int) std::ceil(outer / binHz) + 1;
// The spectrum's in order from -inputRate/2, of the input before the NCO: the signal's centre is at its frequency
const double centreBin = n / 2 + m_ncoFrequency / binHz;
std::vector<double> ratio(2 * maxBin + 1, 0.0);
std::vector<double> core;
for (int d = -maxBin; d <= maxBin; d++)
{
const double fb = centreBin + d;
const int i0 = (int) std::floor(fb);
const double t = fb - i0;
const double p = (i0 >= 0) && (i0 + 1 < n) ? (1.0 - t) * m_eqPower[i0] + t * m_eqPower[i0 + 1] : 0.0;
const double f = std::fabs(d * binHz);
const double rc = f <= inner ? 1.0 : f >= outer ? 0.0 : 0.5 * (1.0 + std::cos(m_pi * (f - inner) / (m_rollOff * m_symbolRate)));
ratio[d + maxBin] = p / (rc * snr + 1.0);
if (f < 0.3 * m_symbolRate) {
core.push_back(ratio[d + maxBin]);
}
}
if (core.empty()) {
return;
}
std::sort(core.begin(), core.end());
const double reference = core[core.size() / 2];
if (reference <= 0.0) {
return;
}
// Smoothed, and the two sides averaged (in dB)
const int smooth = std::max(1, (int) std::lround(m_eqSmoothing * m_symbolRate / binHz));
std::vector<double> responseDb(2 * maxBin + 1);
for (int d = -maxBin; d <= maxBin; d++)
{
double sum = 0.0;
int count = 0;
for (int j = std::max(-maxBin, d - smooth); j <= std::min(maxBin, d + smooth); j++, count++) {
sum += ratio[j + maxBin];
}
responseDb[d + maxBin] = 10.0 * std::log10(std::max(1e-6, sum / count / reference));
}
std::vector<double> gainDb(maxBin + 1);
for (int d = 0; d <= maxBin; d++)
{
const double response = 0.5 * (responseDb[maxBin + d] + responseDb[maxBin - d]);
gainDb[d] = std::max(m_eqMinGainDb, std::min(m_eqMaxGainDb, -response));
}
if (m_eqGainDb.size() == gainDb.size())
{
double change = 0.0;
for (size_t k = 0; k < gainDb.size(); k++)
{
gainDb[k] = 0.5 * (gainDb[k] + m_eqGainDb[k]);
change = std::max(change, std::fabs(gainDb[k] - m_eqGainDb[k]));
}
// Too little to matter: the filter (made in this, the front end's, thread) kept, and the correction it's of
if (change < m_eqMinChangeDb) {
return;
}
}
m_eqGainDb = gainDb;
makeFilter();
m_status.m_equalising = true;
m_status.m_equaliserEdgeDb = m_eqGainDb[std::min(maxBin, (int) std::lround(m_symbolRate / 2.0 / binHz))];
}
// Windowed power spectrum of a block, added to power, in order from -inputRate/2
void FrontEnd::spectrumBlock(const Cf *block, std::vector<double>& power)
{
std::vector<std::complex<double>> x(m_fftSize);
for (int i = 0; i < m_fftSize; i++) {
x[i] = std::complex<double>(block[i].real(), block[i].imag()) * m_window[i];
}
fft(x);
for (int i = 0; i < m_fftSize; i++) {
power[(i + m_fftSize / 2) % m_fftSize] += std::norm(x[i]);
}
}
// The signal's centre, from the averaged spectrum: where it correlates best with the raised cosine's power spectrum
// (less the noise floor, so the shape's what counts), interpolated between bins. And its half power width, which is
// the symbol rate
void FrontEnd::estimateCarrier()
{
const int n = m_fftSize;
const double binHz = m_inputRate / n;
std::vector<double> smooth(n, 0.0);
for (int i = 0; i < n; i++)
{
double sum = 0.0;
int count = 0;
for (int j = std::max(0, i - 2); j <= std::min(n - 1, i + 2); j++, count++) {
sum += m_estimatePower[j];
}
smooth[i] = sum / count;
}
std::vector<double> sorted(smooth);
std::sort(sorted.begin(), sorted.end());
const double floor = sorted[n / 5];
std::vector<double> excess(n);
for (int i = 0; i < n; i++) {
excess[i] = std::max(0.0, smooth[i] - floor);
}
const double inner = (1.0 - m_rollOff) * m_symbolRate / 2.0;
const double outer = (1.0 + m_rollOff) * m_symbolRate / 2.0;
const int width = (int) std::ceil(outer / binHz);
std::vector<double> shape(2 * width + 1);
for (int d = -width; d <= width; d++)
{
const double f = std::fabs(d * binHz);
shape[d + width] = f <= inner ? 1.0 : f >= outer ? 0.0 : 0.5 * (1.0 + std::cos(m_pi * (f - inner) / (m_rollOff * m_symbolRate)));
}
const int maxOffset = std::max(0, (int) ((m_inputRate / 2.0 - outer * 0.9) / binHz));
std::vector<double> scores(2 * maxOffset + 1, 0.0);
int best = 0;
for (int c = -maxOffset; c <= maxOffset; c++)
{
double score = 0.0;
for (int d = -width; d <= width; d++)
{
const int k = n / 2 + c + d;
if ((k >= 0) && (k < n)) {
score += shape[d + width] * excess[k];
}
}
scores[c + maxOffset] = score;
if (score > scores[best + maxOffset]) {
best = c;
}
}
double offset = best;
if ((best > -maxOffset) && (best < maxOffset))
{
const double a = scores[best + maxOffset - 1], b = scores[best + maxOffset], c = scores[best + maxOffset + 1];
const double denom = a - 2.0 * b + c;
if (denom < 0.0) {
offset += std::max(-0.5, std::min(0.5, 0.5 * (a - c) / denom));
}
}
if (scores[best + maxOffset] > 0.0) {
m_ncoFrequency = offset * binHz;
}
// Half power width: the in-band level, then outwards from the centre to where it's half way to the floor
const int centre = n / 2 + best;
const int core = std::max(1, (int) (0.6 * inner / binHz));
std::vector<double> band;
for (int k = centre - core; k <= centre + core; k++)
{
if ((k >= 0) && (k < n)) {
band.push_back(smooth[k]);
}
}
m_status.m_measuredSymbolRate = 0.0;
m_status.m_spectralEsN0 = 99.0;
if (!band.empty())
{
std::sort(band.begin(), band.end());
const double level = band[band.size() / 2];
// The raised cosine's power spectrum integrates to the symbol rate times its in-band level, which noise in the
// estimate doesn't bias as it does the half power points. Only if a quarter of the input's band is outside the
// signal, so the floor (its 20th percentile) is noise: at 1.25 or 1.5 times the symbol rate, it was the signal's
// edges, and the rate measured 75 to 90 % of the true one
if ((level > 2.0 * floor) && (outer <= 0.375 * m_inputRate))
{
double area = 0.0;
const int extent = (int) std::ceil(1.25 * outer / binHz);
// Limited to twice the level, so a line (an unmodulated carrier, or the transmitter's) doesn't count much
for (int k = std::max(0, centre - extent); k <= std::min(n - 1, centre + extent); k++) {
area += std::min(smooth[k] - floor, 2.0 * (level - floor));
}
m_status.m_measuredSymbolRate = area / (level - floor) * binHz;
}
// A raised cosine's in-band level is its Es: over the noise's, Es/N0
if ((outer <= 0.375 * m_inputRate) && (floor > 0.0)) {
m_status.m_spectralEsN0 = 10.0 * std::log10(std::max(1e-3, (level - floor) / floor));
}
}
std::lock_guard<std::mutex> lock(m_displayMutex);
m_displayPower = m_estimatePower;
for (double& p : m_displayPower) {
p /= m_spectrumBlocks;
}
}
void FrontEnd::getSpectrum(std::vector<float>& db, int points) const
{
std::lock_guard<std::mutex> lock(m_displayMutex);
db.clear();
if (m_displayPower.empty() || (points <= 0)) {
return;
}
db.resize(points);
const int per = std::max(1, m_fftSize / points);
for (int p = 0; p < points; p++)
{
double sum = 0.0;
const int first = (int) ((int64_t) p * m_fftSize / points);
int count = 0;
for (int i = first; i < std::min(m_fftSize, first + per); i++, count++) {
sum += m_displayPower[i];
}
db[p] = (float) (10.0 * std::log10(std::max(1e-20, sum / std::max(1, count))));
}
}
// A block of the input for display (and the equaliser), the next a tenth of a second on
void FrontEnd::displayBlock(const Cf *block)
{
std::vector<double> power(m_fftSize, 0.0);
spectrumBlock(block, power);
{
std::lock_guard<std::mutex> lock(m_displayMutex);
if (m_displayPower.size() != power.size()) {
m_displayPower = power;
}
for (int k = 0; k < m_fftSize; k++) {
m_displayPower[k] = 0.8 * m_displayPower[k] + 0.2 * power[k];
}
}
accumulateEqualiser(power);
m_untilDisplayBlock = (uint64_t) (m_inputRate / 10.0);
}
void FrontEnd::process(const Cf *in, int nbIn, std::vector<Cf>& symbols)
{
if (nbIn <= 0) {
return;
}
// The input's power, for the status, from up to 4096 samples of each block, 8 partial sums at a time (as the
// compiler won't reorder a float sum to vectorise it)
{
const int n = std::min(nbIn, 4096) & ~3;
float sums[8] = {0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f};
const float *f = reinterpret_cast<const float *>(in);
for (int i = 0; i < 2 * n; i += 8)
{
for (int j = 0; j < 8; j++) {
sums[j] += f[i + j] * f[i + j];
}
}
m_inputPowerSum += (sums[0] + sums[1]) + (sums[2] + sums[3]) + (sums[4] + sums[5]) + (sums[6] + sums[7]);
m_inputPowerCount += n;
}
if (m_inputPowerCount >= (uint64_t) (m_inputRate / 100.0))
{
m_status.m_power = 10.0 * std::log10(std::max(1e-20, m_inputPowerSum / m_inputPowerCount));
m_inputPowerSum = 0.0;
m_inputPowerCount = 0;
}
// The spectrum, of the raw input: for the carrier's estimate, all the blocks, then for display a block now and then
if (m_status.m_estimating)
{
int i = 0;
while (i < nbIn)
{
const int n = std::min(nbIn - i, m_fftSize - m_spectrumFill);
std::copy(in + i, in + i + n, m_spectrumBlock.begin() + m_spectrumFill);
m_spectrumFill += n;
i += n;
if (m_spectrumFill == m_fftSize)
{
spectrumBlock(m_spectrumBlock.data(), m_estimatePower);
m_spectrumFill = 0;
if (++m_estimateBlocks == m_spectrumBlocks)
{
estimateCarrier();
m_status.m_estimating = false;
m_status.m_frequencyOffset = m_ncoFrequency;
m_untilDisplayBlock = (uint64_t) (m_inputRate / 10.0);
break;
}
}
}
if (m_status.m_estimating) {
return;
}
// The rest of the samples are demodulated
in += i;
nbIn -= i;
}
else if ((m_spectrumFill > 0) || (m_untilDisplayBlock <= (uint64_t) nbIn))
{
if ((m_spectrumFill == 0) && (nbIn >= m_fftSize))
{
displayBlock(in);
}
else
{
// Blocks shorter than the FFT gathered (the estimate's buffer's free once it's made), from the one due
const int n = std::min(nbIn, m_fftSize - m_spectrumFill);
std::copy(in, in + n, m_spectrumBlock.begin() + m_spectrumFill);
m_spectrumFill += n;
if (m_spectrumFill == m_fftSize)
{
m_spectrumFill = 0;
displayBlock(m_spectrumBlock.data());
}
}
}
else
{
m_untilDisplayBlock -= nbIn;
}
// NCO, in float, from the exact phase every block, into the buffer grown for it (its capacity doubled when short,
// so it's seldom reallocated)
const size_t base = m_buffer.size();
if (m_buffer.capacity() < base + nbIn) {
m_buffer.reserve(2 * (base + nbIn));
}
m_buffer.resize(base + nbIn);
const double stepAngle = -2.0 * m_pi * m_ncoFrequency / m_inputRate;
const Cf stepRot((float) std::cos(stepAngle), (float) std::sin(stepAngle));
if (m_ncoFrequency == 0.0)
{
std::copy(in, in + nbIn, m_buffer.begin() + base);
}
else
{
// A block of 64 phasors from 64 steps, then each block's start from the exact phase
float stepRe[64], stepIm[64];
Cf p(1.0f, 0.0f);
for (int k = 0; k < 64; k++)
{
stepRe[k] = p.real();
stepIm[k] = p.imag();
p *= stepRot;
}
const float *src = reinterpret_cast<const float *>(in);
float *dst = reinterpret_cast<float *>(m_buffer.data() + base);
for (int j = 0; j < nbIn; j += 64)
{
const int n = std::min(64, nbIn - j);
const double phase = m_ncoPhase + j * stepAngle;
const float rr = (float) std::cos(phase), ri = (float) std::sin(phase);
for (int k = 0; k < n; k++)
{
const float pr = rr * stepRe[k] - ri * stepIm[k], pi_ = rr * stepIm[k] + ri * stepRe[k];
const float xr = src[2 * (j + k)], xi = src[2 * (j + k) + 1];
dst[2 * (j + k)] = xr * pr - xi * pi_;
dst[2 * (j + k) + 1] = xr * pi_ + xi * pr;
}
}
m_ncoPhase = std::fmod(m_ncoPhase + nbIn * stepAngle, 2.0 * m_pi);
}
// Matched filter and interpolation, a block of symbols at a time, the timing loop updated after each
const int maxOut = 2 * m_symbolsPerUpdate;
m_outputs.resize(maxOut);
while (true)
{
const double step = m_step * (1.0 - m_integrator);
const int made = dvbt::resampleFilter(m_buffer.data(), 0, (int) m_buffer.size(), m_pairs.data(), m_taps, m_phases,
m_time, step, m_outputs.data(), maxOut);
float error = 0.0f, power = 0.0f;
int count = 0;
int k = 0;
bool endedOnSymbol = false; // The last output a symbol, so the next's a half way sample
const size_t blockFirst = symbols.size(); // This block's symbols, for the AGC: the one held from the last too
if (!m_onSymbol && (made > 0))
{
m_lastMid = m_outputs[0];
m_onSymbol = true;
k = 1;
}
if (!m_haveLast && (k < made))
{
m_lastSymbol = m_outputs[k];
symbols.push_back(m_lastSymbol);
m_haveLast = true;
m_onSymbol = false;
if (++k < made)
{
m_lastMid = m_outputs[k++];
m_onSymbol = true;
}
else
{
endedOnSymbol = true;
}
}
// Pairs of a symbol and the half way sample after it, after any symbol just output. Gardner: positive when late,
// the half way sample past the zero crossing of a transition
const size_t first = symbols.size();
symbols.resize(first + (made - k + 1) / 2);
Cf *out = symbols.data() + first;
const Cf *outputs = m_outputs.data();
float lr = m_lastSymbol.real(), li = m_lastSymbol.imag(), mr = m_lastMid.real(), mi = m_lastMid.imag();
int s = 0;
for (; k + 1 < made; k += 2)
{
const float yr = outputs[k].real(), yi = outputs[k].imag();
error += mr * (yr - lr) + mi * (yi - li);
power += yr * yr + yi * yi;
out[s++] = outputs[k];
lr = yr;
li = yi;
mr = outputs[k + 1].real();
mi = outputs[k + 1].imag();
}
count = s;
if (k < made)
{
// A symbol whose half way sample is in the next block
const float yr = m_outputs[k].real(), yi = m_outputs[k].imag();
error += mr * (yr - lr) + mi * (yi - li);
power += yr * yr + yi * yi;
out[s++] = m_outputs[k];
lr = yr;
li = yi;
count++;
m_onSymbol = false;
}
else if ((made > 0) && !endedOnSymbol)
{
m_onSymbol = true;
}
symbols.resize(first + s);
m_lastSymbol = Cf(lr, li);
m_lastMid = Cf(mr, mi);
if ((count > 0) && (power > 0.0))
{
const double e = error / power;
m_integrator = std::max(-m_maxClockOffset, std::min(m_maxClockOffset, m_integrator + m_ki * e * count));
const double move = std::max(-m_maxPhaseStep, std::min(m_maxPhaseStep, m_kp * e * count));
m_time = std::max((double) (m_taps / 2 - 1), m_time - move * m_samplesPerSymbol);
const double mean = power / count;
m_power = m_powerValid ? 0.9 * m_power + 0.1 * mean : mean;
m_powerValid = true;
}
if (m_powerValid)
{
const float gain = (float) (1.0 / std::sqrt(std::max(1e-30, m_power)));
for (size_t s = blockFirst; s < symbols.size(); s++) {
symbols[s] *= gain;
}
}
m_status.m_symbols += symbols.size() - blockFirst;
if (made < maxOut) {
break;
}
}
m_status.m_clockOffset = (1.0 / (1.0 - m_integrator) - 1.0) * 1e6;
// Keep the input the next outputs need
const int consumed = std::min((int) m_buffer.size(), std::max(0, (int) m_time - (m_taps / 2 - 1)));
if (consumed > 0)
{
m_buffer.erase(m_buffer.begin(), m_buffer.begin() + consumed);
m_time -= consumed;
}
}
} // namespace dvbs

202
modemdvb/dvbsfrontend.h

@ -0,0 +1,202 @@ @@ -0,0 +1,202 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBSFRONTEND_H
#define MODEMDVB_DVBSFRONTEND_H
#include <algorithm>
#include <atomic>
#include <cmath>
#include <complex>
#include <cstdint>
#include <mutex>
#include <vector>
#include "export.h"
namespace dvbs {
//! The single carrier receivers' front end: samples at the channel rate in, symbols at one a symbol out, with the
//! carrier coarsely centred and the symbol timing recovered.
//!
//! - The carrier: before anything else, the spectrum of the first samples is measured, and the signal's centre found by
//! correlating it with the raised cosine's shape, to a fraction of a percent of the symbol rate. An NCO moves it to 0
//! Hz; the receivers' carrier recovery does the rest, and moves the NCO (adjustFrequency()) if what's left gets large.
//! - The matched filter: a root raised cosine, made a polyphase filter at the input rate (makePolyphaseFilter()), so one
//! pass of the resampler's kernel both filters and interpolates, at two outputs a symbol: on the symbol, and half way.
//! - Timing: a Gardner detector on those outputs, averaged over blocks of symbols, steering a second order loop whose
//! output moves the interpolation's time and step, so the symbol clock's offset is measured, as the OFDM receivers'
//! is (in ppm).
//! - AGC: the symbols are scaled to unit average power.
//! - Equaliser: once a receiver's locked (setEqualiserReference()), the input's spectrum is averaged and compared with the
//! raised cosine's, plus noise at the Es/N0 measured: the difference is the receive chain's response (the device's
//! filter, the transponder's, cable tilt), which cut the signal's edges by 4 to 7 dB at 27.5 MS/s on Astra 2 and cost
//! 1 to 2 dB of Es/N0. It's undone in the matched filter, symmetrically (the filter's real), by up to 10 dB, so it
//! costs nothing a sample. For signals of at most 8 samples a symbol (the device's filter is flat for narrower)
class MODEMDVB_API FrontEnd
{
public:
typedef std::complex<float> Cf;
struct Status
{
bool m_estimating = true; //!< Measuring the spectrum, so no symbols yet
double m_frequencyOffset = 0.0; //!< Hz, of the NCO: where the signal was found, plus adjustments
double m_clockOffset = 0.0; //!< ppm, of the symbol rate from that set (positive if faster)
double m_measuredSymbolRate = 0.0; //!< From the spectrum's half power width, 0 if not measured
//! dB, from the spectrum: its in-band level over the noise floor outside the signal, when there's enough
//! outside it (as for m_measuredSymbolRate), else a high value (taken as a strong signal)
double m_spectralEsN0 = 99.0;
uint64_t m_clockAdjustments = 0; //!< adjustClock()s made, so a receiver knows when its has been
double m_power = 0.0; //!< dB, of the input
uint64_t m_symbols = 0; //!< Output since the reset
bool m_equalising = false; //!< The matched filter undoes the receive chain's response
double m_equaliserEdgeDb = 0.0; //!< The power gain it applies half the symbol rate from the centre
};
FrontEnd();
//! Input at inputRate, at least 2 symbolRate for the matched filter not to alias (a little less works)
void configure(double inputRate, double symbolRate, double rollOff);
void setRollOff(double rollOff);
double rollOff() const { return m_rollOff; }
double inputRate() const { return m_inputRate; }
double symbolRate() const { return m_symbolRate; }
//! Measure the spectrum again for the carrier, and start the timing loop afresh
void reset();
//! Keep the carrier's frequency (from adjustFrequency()) through a reset rather than measuring it again
void setFrequency(double hz);
//! Appends the symbols the samples complete (none while estimating)
void process(const Cf *in, int nbIn, std::vector<Cf>& symbols);
//! Move the carrier the NCO takes out by hz, from the next sample processed
void adjustFrequency(double hz);
//! Move the timing loop's clock (its integrator) by ppm: a clock error measured elsewhere (from frames' slips)
void adjustClock(double ppm);
//! Symbols still to come of the samples it holds (turned by the NCO as it was then)
int heldSymbols() const;
//! Loop noise bandwidth times the symbol period: wide to acquire, narrow to track
void setTimingBandwidth(double bnT);
//! The tracking bandwidth for a lock at an Es/N0 (dB): bnT down to -2 dB (DVB-S2's lowest MODCOD's threshold), then
//! less with the SNR, a tenth at -7 dB. The Gardner detector's noise grows as the SNR falls (its noise by noise
//! term), and at DVB-S2X VL-SNR's -6 to -10 dB, the loop at bnT wandered tens of ppm, half a symbol across a frame,
//! losing the next header
static double trackingBandwidth(double bnT, double esn0)
{
return esn0 >= -2.0 ? bnT : std::max(0.04 * bnT, bnT * std::pow(10.0, (esn0 + 2.0) / 5.0));
}
//! The equaliser on (the default) or off: off, the matched filter's the plain root raised cosine again
void setEqualiser(bool enabled) { m_eqEnabled = enabled; }
//! From the receiver, each block: whether it's locked (so the spectrum's the signal's), and its Es/N0 (dB)
void setEqualiserReference(bool locked, double esn0);
const Status& getStatus() const { return m_status; }
//! The input's power spectrum, dB, points bins from -inputRate/2 to +inputRate/2. Empty if not measured yet. From
//! any thread
void getSpectrum(std::vector<float>& db, int points) const;
static constexpr int m_fftSize = 1024;
private:
static constexpr double m_pi = 3.141592653589793;
static constexpr int m_spectrumBlocks = 48; //!< Averaged for the carrier's estimate
static constexpr int m_symbolsPerUpdate = 32; //!< Of the timing loop
static constexpr double m_filterSpan = 10.0; //!< Symbols, of the matched filter
static constexpr double m_maxClockOffset = 0.005; //!< Of the timing loop's integrator: 5000 ppm
static constexpr double m_maxPhaseStep = 0.1; //!< Symbols, the timing loop may move a block
static constexpr int m_eqUpdateBlocks = 40; //!< Spectrum blocks (one each 0.1 s) averaged for each equaliser update
static constexpr double m_eqMaxSamplesPerSymbol = 8.0; //!< Wider signals only
static constexpr double m_eqMaxGainDb = 10.0; //!< Of the correction, of power
static constexpr double m_eqMinGainDb = -3.0;
static constexpr double m_eqSmoothing = 0.02; //!< Of the symbol rate either side, the response is averaged over
static constexpr int m_eqFrequencies = 128; //!< Points of the filter's frequency response, for its impulse
static constexpr double m_eqMinChangeDb = 0.1; //!< Of the correction at any frequency, for the filter to be made again
static constexpr double m_eqMinEsN0 = 3.0; //!< dB, for the response to be measured: below, the model of the spectrum
//!< (the raised cosine over noise) needs the Es/N0 too exactly, and the
//!< noise swamps the interference undoing the response would remove
static double raisedCosine(double t, double a);
static double besselI0(double x);
void makeFilter();
void computeGains();
void accumulateEqualiser(const std::vector<double>& power);
void computeEqualiser();
void clearEqualiser();
void spectrumBlock(const Cf *block, std::vector<double>& power);
void displayBlock(const Cf *block);
void estimateCarrier();
void fft(std::vector<std::complex<double>>& x) const;
double m_inputRate;
double m_symbolRate;
double m_rollOff;
double m_samplesPerSymbol; //!< Input samples
int m_taps;
std::vector<float> m_pairs; //!< The matched filter's phases
static constexpr int m_phases = 256;
// NCO
double m_ncoFrequency; //!< Hz taken out
double m_ncoPhase; //!< Radians, at the next sample
bool m_frequencyKnown; //!< setFrequency() was called, so no estimate after the reset
// Spectrum: the carrier estimate, then for display
std::vector<double> m_window;
std::vector<std::complex<double>> m_twiddles;
std::vector<int> m_bitReverse;
std::vector<Cf> m_spectrumBlock; //!< Being gathered (for the estimate, then for display from short blocks)
int m_spectrumFill;
std::vector<double> m_estimatePower;
int m_estimateBlocks;
std::vector<double> m_displayPower;
mutable std::mutex m_displayMutex; //!< For m_displayPower
uint64_t m_untilDisplayBlock; //!< Input samples
// Matched filter and timing
std::vector<Cf> m_buffer; //!< NCO'd input not yet used
double m_time; //!< Of the next output, input samples from m_buffer[0]
double m_step; //!< Nominal, input samples between outputs (half a symbol)
bool m_onSymbol; //!< The next output is on a symbol (else half way)
Cf m_lastSymbol;
Cf m_lastMid;
bool m_haveLast;
double m_integrator; //!< Symbols per symbol: the clock's offset
double m_kp;
double m_ki;
double m_bandwidth;
double m_detectorGain; //!< Of the normalised Gardner detector, per symbol of timing error
double m_power; //!< Of the symbols, for the AGC
bool m_powerValid;
std::vector<Cf> m_outputs; //!< Scratch
double m_inputPowerSum;
uint64_t m_inputPowerCount;
// Equaliser: the spectrum averaged while locked, and the power gain (dB) applied at each bin's frequency from the
// signal's centre (empty for none)
std::atomic<bool> m_eqEnabled;
bool m_eqLocked;
double m_eqEsN0;
std::vector<double> m_eqPower;
int m_eqBlocks;
std::vector<double> m_eqGainDb;
Status m_status;
};
} // namespace dvbs
#endif // MODEMDVB_DVBSFRONTEND_H

210
modemdvb/dvbsmodulator.cpp

@ -0,0 +1,210 @@ @@ -0,0 +1,210 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include <cstring>
#include "dvbsmodulator.h"
#include "dvbtouter.h"
#include "viterbi27.h"
namespace dvbs {
PulseShaper::PulseShaper() :
m_sampleRate(2.0),
m_symbolRate(1.0),
m_span(16),
m_time(0.0),
m_step(0.5),
m_gain(1.0f)
{
configure(2.0, 1.0, 0.35);
}
void PulseShaper::configure(double sampleRate, double symbolRate, double rollOff, int span)
{
m_sampleRate = sampleRate;
m_symbolRate = symbolRate;
// A low roll-off's pulse falls off over about 1 / roll-off symbols: spanned 3 times that, else truncated it's ISI
m_span = std::max(span, 2 * (int) std::ceil(1.5 / std::max(0.01, rollOff)));
m_step = symbolRate / sampleRate;
const int points = m_span * m_resolution + 1;
m_table.resize(points);
double energy = 0.0;
for (int i = 0; i < points; i++)
{
const double t = (double) i / m_resolution - m_span / 2.0;
m_table[i] = (float) rootRaisedCosine(t, rollOff);
}
// Unit power out for unit power symbols: the average power is the pulse's energy (the integral of h^2 over t, in
// symbols) a symbol, whatever the sample rate
for (int i = 0; i < points; i += 1) {
energy += (double) m_table[i] * m_table[i];
}
energy /= m_resolution;
m_gain = (float) std::sqrt(1.0 / energy);
m_history.assign(m_span, Cf(0.0f, 0.0f));
m_time = m_span / 2.0;
}
void PulseShaper::setClockOffset(double ppm)
{
m_step = m_symbolRate * (1.0 + ppm * 1e-6) / m_sampleRate;
}
void PulseShaper::process(const Cf *symbols, int nbSymbols, std::vector<Cf>& samples)
{
m_history.insert(m_history.end(), symbols, symbols + nbSymbols);
const int n = (int) m_history.size();
const int half = m_span / 2;
// Output at m_time needs symbols from m_time - half to m_time + half
while (m_time + half + 1 < n)
{
const int centre = (int) std::floor(m_time);
float re = 0.0f, im = 0.0f;
for (int k = centre - half + 1; k <= centre + half; k++)
{
const double t = m_time - k + m_span / 2.0; // From the table's start
const int index = (int) std::lround(t * m_resolution);
if ((index >= 0) && (index < (int) m_table.size()))
{
const float h = m_table[index];
re += m_history[k].real() * h;
im += m_history[k].imag() * h;
}
}
samples.emplace_back(re * m_gain, im * m_gain);
m_time += m_step;
}
const int consumed = std::max(0, std::min(n, (int) std::floor(m_time) - half));
m_history.erase(m_history.begin(), m_history.begin() + consumed);
m_time -= consumed;
}
Modulator::Modulator(CodeRate rate) :
m_period(1),
m_encoderState(0)
{
puncturePattern(rate, m_pattern, m_period);
// The interleaver starts full of random bytes, as if the transmitter had been running: zeros would make the first
// packets a run of the same symbol, an unmodulated carrier
uint32_t lcg = 12345;
for (int j = 0; j < 12; j++)
{
m_fifo[j].resize(17 * j);
for (uint8_t& byte : m_fifo[j])
{
lcg = lcg * 1664525u + 1013904223u;
byte = (uint8_t) (lcg >> 24);
}
m_fifoPos[j] = 0;
}
}
double Modulator::symbolsPerPacket() const
{
return dvbt::rsPacketSize * 8.0 * m_pattern.size() / m_period / 2.0;
}
void Modulator::modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& symbols)
{
static const float a = 0.70710678f;
for (int p = 0; p < nbPackets; p++)
{
m_pending.insert(m_pending.end(), packets + p * dvbt::tsPacketSize, packets + (p + 1) * dvbt::tsPacketSize);
if (m_pending.size() == 8 * dvbt::tsPacketSize)
{
encodeGroup();
m_pending.clear();
}
}
// Puncture whole periods, then a symbol for each pair: I = 1 - 2 x, Q = 1 - 2 y
const int periods = (int) m_bits.size() / m_period;
std::vector<uint8_t> xy;
dvbt::Viterbi27::encode(m_bits.data(), periods * m_period, m_encoderState, xy);
m_bits.erase(m_bits.begin(), m_bits.begin() + periods * m_period);
for (int g = 0; g < periods; g++)
{
for (const PunctureEntry& e : m_pattern) {
m_serial.push_back(xy[2 * (g * m_period + e.m_bit) + e.m_y]);
}
}
size_t i = 0;
for (; i + 2 <= m_serial.size(); i += 2) {
symbols.emplace_back(m_serial[i] ? -a : a, m_serial[i + 1] ? -a : a);
}
m_serial.erase(m_serial.begin(), m_serial.begin() + i);
}
// Randomisation of 8 packets, RS encoding, outer interleaving, and their bits for the convolutional code
void Modulator::encodeGroup()
{
uint8_t group[8][dvbt::tsPacketSize];
for (int k = 0; k < 8; k++) {
std::memcpy(group[k], &m_pending[k * dvbt::tsPacketSize], dvbt::tsPacketSize);
}
dvbt::OuterDecoder::randomize(group);
for (int k = 0; k < 8; k++)
{
uint8_t rs[dvbt::rsPacketSize];
dvbt::OuterDecoder::rsEncode(group[k], rs);
for (int b = 0; b < dvbt::rsPacketSize; b++)
{
const int j = b % 12;
uint8_t out = rs[b];
if (j > 0)
{
std::vector<uint8_t>& fifo = m_fifo[j];
out = fifo[m_fifoPos[j]];
fifo[m_fifoPos[j]] = rs[b];
m_fifoPos[j] = (m_fifoPos[j] + 1) % (int) fifo.size();
}
for (int bit = 7; bit >= 0; bit--) {
m_bits.push_back((out >> bit) & 1);
}
}
}
}
} // namespace dvbs

80
modemdvb/dvbsmodulator.h

@ -0,0 +1,80 @@ @@ -0,0 +1,80 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBSMODULATOR_H
#define MODEMDVB_DVBSMODULATOR_H
#include <cstdint>
#include <vector>
#include "dvbs.h"
namespace dvbs {
//! Root raised cosine pulse shaping of symbols to samples at any rate, for the test transmitters: each output the sum
//! of the symbols around it, weighted by the pulse at its distance from each (from a finely sampled table)
class MODEMDVB_API PulseShaper
{
public:
PulseShaper();
void configure(double sampleRate, double symbolRate, double rollOff, int span = 16);
//! Appends the samples the symbols complete. Unit average power for unit power symbols
void process(const Cf *symbols, int nbSymbols, std::vector<Cf>& samples);
//! Changes the symbol rate by ppm (a transmitter's clock error)
void setClockOffset(double ppm);
private:
double m_sampleRate;
double m_symbolRate;
int m_span;
static constexpr int m_resolution = 256; //!< Table points a symbol
std::vector<float> m_table;
std::vector<Cf> m_history; //!< The symbols the next outputs need
double m_time; //!< Of the next output, in symbols from m_history[0]
double m_step; //!< Symbols an output
float m_gain;
};
//! DVB-S transmitter (EN 300 421 4.4, 4.5), for testing the receiver: transport stream packets to QPSK symbols
class MODEMDVB_API Modulator
{
public:
explicit Modulator(CodeRate rate);
//! 188-byte packets starting 0x47. Appends the QPSK symbols (unit power) they complete: packets are randomised a
//! group of 8 at a time, so the last of a call may wait for the next
void modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& symbols);
//! Symbols a packet takes, on average
double symbolsPerPacket() const;
private:
void encodeGroup();
std::vector<PunctureEntry> m_pattern;
int m_period;
std::vector<uint8_t> m_pending; //!< Packets for the next group of 8
std::vector<uint8_t> m_fifo[12]; //!< Outer interleaver, branch j delays 17 j bytes
int m_fifoPos[12];
uint8_t m_encoderState;
std::vector<uint8_t> m_bits; //!< Information bits waiting for a whole puncturing period
std::vector<uint8_t> m_serial; //!< Punctured bits waiting for a symbol's pair
};
} // namespace dvbs
#endif // MODEMDVB_DVBSMODULATOR_H

846
modemdvb/dvbsreceiver.cpp

@ -0,0 +1,846 @@ @@ -0,0 +1,846 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <chrono>
#include <cmath>
#include <exception>
#include "util/profiler.h"
#include "dvbsreceiver.h"
#include "samplethread.h"
namespace dvbs {
// e^j(angle): for the small angles a loop turns by, sine and cosine to the fifth and fourth power (to 1e-6 within 0.3
// radians, and the rotator's renormalised each group), else exactly
inline std::complex<float> QPSKCarrier::rotation(double angle)
{
if (std::fabs(angle) > 0.3) {
return std::complex<float>((float) std::cos(angle), (float) std::sin(angle));
}
const float x = (float) angle, x2 = x * x;
return std::complex<float>(1.0f - x2 * (0.5f - x2 * (1.0f / 24.0f)), x * (1.0f - x2 * ((1.0f / 6.0f) - x2 * (1.0f / 120.0f))));
}
double Receiver::now()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
QPSKCarrier::QPSKCarrier() :
m_tracking(false),
m_rotator(1.0f, 0.0f),
m_frequency(0.0),
m_kp(0.0),
m_ki(0.0),
m_bandwidth(m_trackBandwidth),
m_lock(0.0),
m_errorPower(1.0),
m_averaged(false),
m_m2(0.0),
m_m4(0.0),
m_tracked(0),
m_unlocked(0),
m_acquisitions(0)
{
reset();
}
void QPSKCarrier::reset()
{
m_tracking = false;
m_acquire.clear();
m_acquire.reserve(m_acquireSymbols);
m_rotator = Cf(1.0f, 0.0f);
m_frequency = 0.0;
m_pendingShift = 0.0;
m_pendingSymbols = 0;
m_lock = 0.0;
m_errorPower = 1.0;
m_averaged = false;
m_m2 = 0.0;
m_m4 = 0.0;
m_tracked = 0;
m_unlocked = 0;
}
void QPSKCarrier::shiftFrequency(double cyclesPerSymbol, int afterSymbols)
{
if (afterSymbols <= 0)
{
m_frequency -= cyclesPerSymbol;
}
else
{
m_pendingShift += cyclesPerSymbol;
m_pendingSymbols = afterSymbols;
}
}
void QPSKCarrier::setBandwidth(double bnT)
{
if (bnT != m_bandwidth)
{
m_bandwidth = bnT;
if (m_tracking && (m_tracked >= m_acquireTrackSymbols)) {
computeGains(bnT);
}
}
}
void QPSKCarrier::computeGains(double bnT)
{
// Phase detector Im(w conj(d)) for unit power symbols: sin of the error, so a gain of 1 (cycles: 2 pi)
const double zeta = 0.707;
const double theta = bnT / (zeta + 1.0 / (4.0 * zeta));
const double d = 1.0 + 2.0 * zeta * theta + theta * theta;
m_kp = 4.0 * zeta * theta / d / (2.0 * m_pi);
m_ki = 4.0 * theta * theta / d / (2.0 * m_pi);
}
int QPSKCarrier::process(const Cf *z, int nbSymbols, Cf *w, bool& restarted, int& restartAt)
{
static const float a = 0.70710678f;
static const int group = 8;
int i = 0, out = 0;
while (i < nbSymbols)
{
if (!m_tracking)
{
for (; (i < nbSymbols) && ((int) m_acquire.size() < m_acquireSymbols); i++)
{
const std::complex<double> zd(z[i].real(), z[i].imag());
const std::complex<double> z2 = zd * zd;
m_acquire.push_back(z2 * z2);
}
if ((int) m_acquire.size() < m_acquireSymbols) {
break;
}
// The fourth power's line, at 4 times the offset
fft(m_acquire);
m_frequency = peakFrequency(m_acquire) / 4.0;
m_pendingShift = 0.0;
m_pendingSymbols = 0;
m_acquire.clear();
m_rotator = Cf(1.0f, 0.0f);
m_tracking = true;
m_tracked = 0;
m_unlocked = 0;
m_lock = 0.0;
m_errorPower = 1.0;
m_averaged = false;
m_m2 = 0.0;
m_m4 = 0.0;
m_acquisitions++;
computeGains(m_acquireBandwidth);
continue;
}
const int first = i;
double lockNumerator = 0.0, lockDenominator = 0.0, errorPower = 0.0, powerSum = 0.0;
for (int n = 0; (i < nbSymbols) && m_tracking; i += n)
{
// A group ends where a shift of the frequency is due
n = std::min(m_pendingSymbols > 0 ? std::min(group, m_pendingSymbols) : group, nbSymbols - i);
// This group turned at the loop's frequency from the rotator
const Cf step = rotation(-2.0 * m_pi * m_frequency);
Cf rot = m_rotator;
float error = 0.0f, num = 0.0f, den = 0.0f, err = 0.0f, pw = 0.0f;
float rr = rot.real(), ri = rot.imag();
const float sr = step.real(), si = step.imag();
for (int k = 0; k < n; k++)
{
const float zr = z[i + k].real(), zi = z[i + k].imag();
const Cf v(zr * rr - zi * ri, zr * ri + zi * rr);
const float t = rr * sr - ri * si;
ri = rr * si + ri * sr;
rr = t;
w[out + k] = v;
const float dr = withSignOf(a, v.real()), di = withSignOf(a, v.imag());
error += v.imag() * dr - v.real() * di; // Im(v conj(d)): the sine of the error, at unit power
const float er = v.real() - dr, ei = v.imag() - di;
err += er * er + ei * ei;
const float p = v.real() * v.real() + v.imag() * v.imag();
const float re2 = v.real() * v.real() - v.imag() * v.imag(), im2 = 2.0f * v.real() * v.imag();
num -= re2 * re2 - im2 * im2; // -Re(v^4): QPSK's points' fourth power is -1
den += p * p;
pw += p;
}
out += n;
lockNumerator += num;
lockDenominator += den;
powerSum += pw;
errorPower += err;
// The loop, as a symbol by symbol one would have moved over the group
m_frequency += m_ki * error;
const double turn = -2.0 * m_pi * m_kp * error;
rot = Cf(rr, ri);
m_rotator = rot * rotation(turn);
// Back to unit magnitude, by a Newton step for 1 / sqrt (it drifts by rounding only)
m_rotator *= 1.5f - 0.5f * std::norm(m_rotator);
const uint64_t before = m_tracked;
m_tracked += n;
if ((m_pendingSymbols > 0) && ((m_pendingSymbols -= n) == 0))
{
m_frequency -= m_pendingShift;
m_pendingShift = 0.0;
}
if ((before < m_acquireTrackSymbols) && (m_tracked >= m_acquireTrackSymbols)) {
computeGains(m_bandwidth);
}
}
// Lock (the fourth power's coherence) and error power, averaged over a couple of thousand symbols
const int count = i - first;
if ((count > 0) && (lockDenominator > 0.0))
{
const double weight = std::min(1.0, count / 2048.0);
m_lock += (lockNumerator / lockDenominator - m_lock) * weight;
// The first update whole (from 0, the moments' SNR would read noise alone until they'd averaged up)
const double average = m_averaged ? std::min(1.0, count / 4096.0) : 1.0;
m_errorPower += (errorPower / count - m_errorPower) * average;
m_m2 += (powerSum / count - m_m2) * average;
m_m4 += (lockDenominator / count - m_m4) * average;
m_averaged = true;
}
// Unlocked for a while: acquire again
if ((m_tracked > (uint64_t) m_acquireSymbols) && (m_lock < m_lockThreshold / 2.0))
{
m_unlocked += count;
if (m_unlocked > m_unlockedLimit)
{
m_tracking = false;
restarted = true;
restartAt = out;
}
}
else
{
m_unlocked = 0;
}
}
return out;
}
double QPSKCarrier::mer() const
{
return -10.0 * std::log10(std::max(1e-10, m_errorPower));
}
// For a constant envelope (QPSK) in complex Gaussian noise, E|y|^2 = S + N and E|y|^4 = S^2 + 4SN + 2N^2, so
// S = sqrt(2 M2^2 - M4) and N = M2 - S
double QPSKCarrier::snr() const
{
const double s2 = 2.0 * m_m2 * m_m2 - m_m4;
if ((m_m2 <= 0.0) || (s2 <= 0.0)) {
return -10.0; // Noise alone, as far as can be told
}
const double s = std::sqrt(s2);
const double n = m_m2 - s;
return n > 0.0 ? std::min(50.0, 10.0 * std::log10(s / n)) : 50.0;
}
Receiver::Receiver(bool threaded) :
m_threaded(threaded),
m_inputRate(2e6),
m_symbolRate(1e6),
m_codeRate(-1),
m_carrierBandwidth(0.0),
m_fecLocked(false),
m_displayRequested(true),
m_displayMaxSymbols(2000),
m_displayPoints(512),
m_busy(false),
m_stop(false),
m_resetEpoch(0),
m_unlockedSymbols(0),
m_fecProgress(false),
m_lastGoodPackets(0),
m_restarts(0),
m_decodeEpoch(0),
m_fecCodeRate(-1),
m_queueWaits(0),
m_fecSeconds(0.0)
{
m_fe = &m_frontEnd;
m_frontEndRestart = false;
m_fec.setPacketCallback([this](const uint8_t *packet) {
m_fecPackets.insert(m_fecPackets.end(), packet, packet + dvbt::tsPacketSize);
});
m_frontEnd.configure(m_inputRate, m_symbolRate, 0.35);
m_fec.setSymbolRate(m_symbolRate);
m_frontEnd.setTimingBandwidth(m_timingAcquire);
m_recentSymbols.assign(m_nbDisplaySymbols, Cf(0.0f, 0.0f));
if (m_threaded)
{
m_fecThread = std::thread(&Receiver::runFEC, this);
m_demodThread.reset(new SampleThread([this](const Cf *samples, int nbSamples) {
demodulate(samples, nbSamples);
}));
}
}
Receiver::~Receiver()
{
m_demodThread.reset();
if (m_threaded)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_stop = true;
}
m_available.notify_all();
m_space.notify_all();
m_fecThread.join();
}
}
void Receiver::configure(double inputRate, double symbolRate)
{
if (m_demodThread) {
m_demodThread->discard();
}
m_inputRate = inputRate;
m_symbolRate = symbolRate;
m_frontEnd.configure(inputRate, symbolRate, 0.35);
m_fec.setSymbolRate(symbolRate);
reset();
}
void Receiver::setSharedFrontEnd(FrontEnd *frontEnd)
{
m_fe = frontEnd ? frontEnd : &m_frontEnd;
m_frontEndRestart = false;
}
void Receiver::reset()
{
if (m_demodThread) {
m_demodThread->discard();
}
// A shared front end's reset by its owner
if (m_fe == &m_frontEnd)
{
m_frontEnd.reset();
m_frontEnd.setTimingBandwidth(m_timingAcquire);
}
m_frontEndRestart = false;
m_carrier.reset();
m_fecLocked = false;
m_unlockedSymbols = 0;
m_fecProgress = false;
{
std::lock_guard<std::mutex> lock(m_demodMutex);
m_demodStatus = Status();
m_demodStatus.m_symbolRate = m_symbolRate;
m_displaySymbols.clear();
std::fill(m_recentSymbols.begin(), m_recentSymbols.end(), Cf(0.0f, 0.0f));
}
if (m_threaded)
{
std::lock_guard<std::mutex> lock(m_mutex);
while (!m_queue.empty())
{
if (!m_queue.front().m_reset && (m_free.size() < m_maxQueued + 2))
{
m_queue.front().m_soft.clear();
m_free.push_back(std::move(m_queue.front()));
}
m_queue.pop_front();
}
SoftBlock block;
block.m_reset = true;
block.m_epoch = ++m_resetEpoch;
m_queue.push_back(std::move(block));
m_packets.clear();
// Again with the new epoch: the error correction thread may have published a block's lock since. The status
// is the last block's until the reset's decoded: not its lock
m_fecLocked = false;
m_fecProgress = false;
m_fecStatus.m_state = FECDecoder::State::Searching;
m_fecStatus.m_tsSync = false;
m_space.notify_all();
m_available.notify_one();
}
else
{
m_fec.reset();
m_fecPackets.clear();
std::lock_guard<std::mutex> lock(m_mutex);
m_packets.clear();
m_fecStatus = m_fec.getStatus();
}
}
void Receiver::process(const Cf *samples, int nbSamples)
{
if (m_demodThread) {
m_demodThread->push(samples, nbSamples);
} else {
demodulate(samples, nbSamples);
}
deliverPackets();
}
void Receiver::demodulate(const Cf *samples, int nbSamples)
{
const double start = now();
m_symbols.clear();
m_frontEnd.process(samples, nbSamples, m_symbols);
demodulateSymbols(start, now());
}
void Receiver::processSymbols(const Cf *symbols, int nbSymbols)
{
const double start = now();
m_symbols.assign(symbols, symbols + nbSymbols);
demodulateSymbols(start, start);
}
// The error correction's sync (Viterbi, deinterleaver, packets) would be lost in the gap, its packets failing RS until
// it found it was: it searches again from the symbols after (the carrier loop keeps its frequency, and follows the
// phase again). As after a restart, the packets aren't dropped: the transport stream goes on after the gap
void Receiver::discontinuity()
{
deliverReset();
}
void Receiver::frontEndShifted(double hz, int afterSymbols)
{
// Acquiring, the frequency's measured afresh
if (m_carrier.tracking() && (m_symbolRate > 0.0)) {
m_carrier.shiftFrequency(hz / m_symbolRate, afterSymbols);
}
}
// As a restart of this receiver's own, without asking for the front end's: the carrier acquired again from the symbols
// after, and the error correction searching again
void Receiver::frontEndReset()
{
m_carrier.reset();
m_unlockedSymbols = 0;
deliverReset();
}
void Receiver::demodulateSymbols(double start, double frontEndEnd)
{
const double bandwidth = m_carrierBandwidth;
if (bandwidth > 0.0)
{
m_carrier.setBandwidth(bandwidth);
}
else if (m_carrier.tracking())
{
// Narrow at low SNR, where a wide loop slips, wide at high SNR, to follow phase noise
const double mer = m_carrier.mer();
m_carrier.setBandwidth(std::max(0.005, std::min(0.02, 0.005 + (mer - 5.0) * 0.0025)));
}
SoftBlock& block = m_filling;
block.m_reset = false;
bool restart = false;
int restartAt = 0;
m_tracked.resize(m_symbols.size());
const int n = m_carrier.process(m_symbols.data(), (int) m_symbols.size(), m_tracked.data(), restart, restartAt);
block.m_soft.resize(2 * n);
softBits(m_tracked.data(), n, m_softScale, block.m_soft.data());
// The last symbols, for display
if (n > 0)
{
const int keep = std::min(n, (int) m_recentSymbols.size());
m_recentSymbols.erase(m_recentSymbols.begin(), m_recentSymbols.begin() + keep);
m_recentSymbols.insert(m_recentSymbols.end(), m_tracked.begin() + (n - keep), m_tracked.begin() + n);
}
// A large frequency offset is moved to the front end's NCO, so the matched filter stays centred on the signal (only
// when tracking: an unlocked loop's frequency is nothing to move, and would mix two into the next acquisition's).
// A shared front end's only with the FEC locked: the carrier loop can track a DVB-S2 signal for a while, and move
// it from under the DVB-S2 receiver searching
if (m_carrier.tracking() && ((m_fe == &m_frontEnd) || m_fecLocked) && (std::fabs(m_carrier.frequency()) > m_foldBack))
{
const double f = m_carrier.frequency();
const int held = m_fe->heldSymbols();
m_fe->adjustFrequency(f * m_symbolRate);
m_carrier.shiftFrequency(f, held);
}
// No packet through RS for a while: the carrier may have been found wrong, a quarter of the symbol rate out (as QPSK
// turned 90 degrees a symbol looks right to the carrier loop, and the code search can even find sync bytes now and
// then), from a spectrum measured as the signal changed, so it's measured again
if (m_fecProgress.exchange(false) || m_fe->getStatus().m_estimating)
{
m_unlockedSymbols = 0;
}
else if ((m_unlockedSymbols += m_symbols.size()) > std::max(m_relockSeconds * m_symbolRate, m_relockMinSymbols))
{
// A shared front end's restarted by its owner, if the DVB-S2 receiver hasn't a lock it'd lose
if (m_fe == &m_frontEnd)
{
m_frontEnd.reset();
m_frontEnd.setTimingBandwidth(m_timingAcquire);
}
else
{
m_frontEndRestart = true;
}
m_carrier.reset();
m_unlockedSymbols = 0;
m_restarts++;
restart = true;
restartAt = n; // This block's symbols are the lock's before: before the reset
}
// A shared one's owner sets them, from both receivers, after each block
if (m_fe == &m_frontEnd)
{
m_frontEnd.setTimingBandwidth(m_fecLocked ? m_timingTrack : m_timingAcquire);
m_frontEnd.setEqualiserReference(m_fecLocked, frontEndEsN0());
}
const double end = now();
{
std::lock_guard<std::mutex> lock(m_demodMutex);
Status& s = m_demodStatus;
s.m_frontEnd = m_fe->getStatus();
s.m_symbolRate = m_symbolRate;
s.m_carrierLocked = m_carrier.locked();
s.m_carrierLock = m_carrier.lockMetric();
s.m_mer = m_carrier.tracking() ? m_carrier.mer() : 0.0;
s.m_snr = m_carrier.tracking() ? m_carrier.snr() : 0.0;
s.m_carrierAcquisitions = m_carrier.acquisitions();
s.m_restarts = m_restarts;
s.m_frequencyOffset = s.m_frontEnd.m_frequencyOffset + (m_carrier.tracking() ? m_carrier.frequency() * m_symbolRate : 0.0);
s.m_clockOffset = s.m_frontEnd.m_clockOffset;
s.m_seconds[0] += frontEndEnd - start;
s.m_seconds[1] += end - frontEndEnd;
if (m_displayRequested)
{
const int count = std::min(m_displayMaxSymbols, (int) m_recentSymbols.size());
m_displaySymbols.assign(m_recentSymbols.end() - count, m_recentSymbols.end());
m_fe->getSpectrum(m_displaySpectrum, m_displayPoints);
m_displayRequested = false;
}
}
// After a restart, the symbols of the lock before it go to the error correction first, then the reset
if (restart)
{
if (restartAt > 0)
{
SoftBlock before;
before.m_soft.assign(block.m_soft.begin(), block.m_soft.begin() + 2 * restartAt);
block.m_soft.erase(block.m_soft.begin(), block.m_soft.begin() + 2 * restartAt);
deliver(before);
}
deliverReset();
}
deliver(block);
}
void Receiver::deliverReset()
{
SoftBlock resetBlock;
resetBlock.m_reset = true;
if (m_threaded)
{
std::unique_lock<std::mutex> lock(m_mutex);
resetBlock.m_epoch = m_resetEpoch; // Not a reset of the receiver: packets are still wanted
m_queue.push_back(std::move(resetBlock));
m_available.notify_one();
}
else
{
resetBlock.m_epoch = m_resetEpoch;
decodeBlock(resetBlock);
}
}
void Receiver::deliver(SoftBlock& block)
{
if (block.m_soft.empty()) {
return;
}
if (!m_threaded)
{
decodeBlock(block);
std::lock_guard<std::mutex> lock(m_mutex);
m_packets.insert(m_packets.end(), m_fecPackets.begin(), m_fecPackets.end());
m_fecPackets.clear();
return;
}
std::unique_lock<std::mutex> lock(m_mutex);
if (m_queue.size() >= m_maxQueued)
{
m_queueWaits++;
PROFILER_START()
m_space.wait(lock, [this]() { return m_stop || (m_queue.size() < m_maxQueued); });
PROFILER_STOP("DVB-S waiting for its FEC thread")
}
SoftBlock queued;
if (!m_free.empty())
{
queued = std::move(m_free.back());
m_free.pop_back();
}
queued.m_reset = false;
queued.m_soft.swap(block.m_soft);
m_queue.push_back(std::move(queued));
m_available.notify_one();
}
// In the error correction thread (or the caller's, unthreaded). Returns whether packets passed RS
bool Receiver::decodeBlock(SoftBlock& block)
{
const int rate = m_codeRate;
if (rate != m_fecCodeRate)
{
m_fecCodeRate = rate;
m_fec.setCodeRate(rate);
}
const double start = now();
if (block.m_reset) {
m_fec.reset();
} else {
m_fec.decode(block.m_soft.data(), (int) block.m_soft.size() / 2);
}
m_fecSeconds = m_fecSeconds + (now() - start); // Only this thread writes it
PROFILER_ADD("DVB-S FEC thread (search and decoding)", (qint64) ((now() - start) * 1e9))
const FECDecoder::Status& fecStatus = m_fec.getStatus();
// RS failures include packets dropped before the outer decoder's synchronised, not counted as packets
const uint64_t good = fecStatus.m_outer.m_packets - std::min(fecStatus.m_outer.m_packets, fecStatus.m_outer.m_rsFailed);
// Packets through RS with the transport stream in sync: a false lock's garbage may now and then pass RS
const bool progress = (good > m_lastGoodPackets) && fecStatus.m_tsSync;
m_lastGoodPackets = good;
// Threaded, runFEC() publishes them, if the block's not from before a reset
if (!m_threaded)
{
m_fecLocked = fecStatus.m_state == FECDecoder::State::Locked;
if (progress) {
m_fecProgress = true;
}
std::lock_guard<std::mutex> lock(m_mutex); // getStatus() may be called from another thread
m_fecStatus = fecStatus;
}
return progress;
}
void Receiver::runFEC()
{
std::unique_lock<std::mutex> lock(m_mutex);
while (true)
{
m_available.wait(lock, [this]() { return m_stop || !m_queue.empty(); });
if (m_stop) {
break;
}
SoftBlock block = std::move(m_queue.front());
m_queue.pop_front();
m_busy = true;
lock.unlock();
m_space.notify_one();
bool progress = false;
// An exception (out of memory) loses the block, the decoder searching again, rather than ending the process
try {
progress = decodeBlock(block);
} catch (const std::exception&) {
m_fec.reset();
}
const FECDecoder::Status status = m_fec.getStatus();
lock.lock();
if (block.m_reset && (block.m_epoch == m_resetEpoch)) {
m_decodeEpoch = block.m_epoch;
}
// A block of the signal before a reset of the receiver: its lock and progress aren't this one's. Published with
// the epoch checked under the lock, so reset() can't be followed by an old block's lock
const bool current = m_decodeEpoch == m_resetEpoch;
m_fecLocked = current && (status.m_state == FECDecoder::State::Locked);
if (current && progress) {
m_fecProgress = true;
}
// Packets from before a reset aren't wanted
if (!m_fecPackets.empty())
{
if (m_decodeEpoch == m_resetEpoch) {
m_packets.insert(m_packets.end(), m_fecPackets.begin(), m_fecPackets.end());
}
m_fecPackets.clear();
}
if (m_decodeEpoch == m_resetEpoch) {
m_fecStatus = status;
}
// Blocks are reused for their buffers: not reset blocks, which have none (one is queued at each restart, many
// a second without a signal), and no more than can be queued
if (!block.m_reset && (m_free.size() < m_maxQueued + 2))
{
block.m_soft.clear();
m_free.push_back(std::move(block));
}
m_busy = false;
if (idle()) {
m_idle.notify_all();
}
}
}
// With m_mutex locked
bool Receiver::idle() const
{
return m_queue.empty() && !m_busy;
}
void Receiver::deliverPackets()
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_delivering.swap(m_packets);
}
if (m_packetCallback)
{
for (size_t i = 0; i + dvbt::tsPacketSize <= m_delivering.size(); i += dvbt::tsPacketSize) {
m_packetCallback(&m_delivering[i]);
}
}
m_delivering.clear();
}
void Receiver::flush()
{
if (m_demodThread) {
m_demodThread->drain();
}
if (m_threaded)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_idle.wait(lock, [this] { return idle(); });
}
deliverPackets();
}
Receiver::Status Receiver::getStatus() const
{
Status status;
{
std::lock_guard<std::mutex> lock(m_demodMutex);
status = m_demodStatus;
}
{
std::lock_guard<std::mutex> lock(m_mutex);
status.m_fec = m_fecStatus;
status.m_queueWaits = m_queueWaits;
status.m_seconds[2] = m_fecSeconds;
}
if (status.m_frontEnd.m_estimating) {
status.m_state = State::Spectrum;
} else if (!status.m_carrierLocked && (status.m_fec.m_state != FECDecoder::State::Locked)) {
status.m_state = State::Carrier;
} else if (status.m_fec.m_state != FECDecoder::State::Locked) {
status.m_state = State::Code;
} else if (!status.m_fec.m_tsSync) {
status.m_state = State::Sync;
} else {
status.m_state = State::Locked;
}
return status;
}
void Receiver::getDisplayData(std::vector<Cf>& symbols, int maxSymbols, std::vector<float>& spectrumDb, int points) const
{
std::lock_guard<std::mutex> lock(m_demodMutex);
symbols = m_displaySymbols;
spectrumDb = m_displaySpectrum;
m_displayMaxSymbols = std::min(maxSymbols, m_nbDisplaySymbols);
m_displayPoints = points;
m_displayRequested = true;
}
} // namespace dvbs

260
modemdvb/dvbsreceiver.h

@ -0,0 +1,260 @@ @@ -0,0 +1,260 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBSRECEIVER_H
#define MODEMDVB_DVBSRECEIVER_H
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "dvbs.h"
#include "dvbsfec.h"
#include "dvbsfrontend.h"
class SampleThread;
namespace dvbs {
//! QPSK carrier recovery: the frequency from the spectrum of the symbols' fourth power (which takes the modulation off,
//! leaving a line at four times the offset, unambiguous to an eighth of the symbol rate), then a second order decision
//! directed phase locked loop. The loop is updated every 8 symbols, each group turned by the phase and frequency as they
//! were at its start, so the symbols of a group are independent (the loop's bandwidth is far below an eighth of the
//! symbol rate). Lock is judged from the fourth power's coherence
class MODEMDVB_API QPSKCarrier
{
public:
typedef std::complex<float> Cf;
QPSKCarrier();
void reset();
//! Noise bandwidth times the symbol period, once tracking
void setBandwidth(double bnT);
//! The symbols, with the carrier taken off, into w, from when tracking (none while acquiring). Returns how many.
//! restarted: set if it lost lock and acquired again, so what follows doesn't follow what went before, with
//! restartAt the index in w of the first symbol after (those before are of the lock before)
int process(const Cf *z, int nbSymbols, Cf *w, bool& restarted, int& restartAt);
bool tracking() const { return m_tracking; }
bool locked() const { return m_tracking && (m_lock > m_lockThreshold); }
double lockMetric() const { return m_lock; }
double frequency() const { return m_frequency - m_pendingShift; } //!< Cycles a symbol, with any shift to come
//! The frequency is moved elsewhere (the front end's NCO): taken off the loop's after the symbols the front end
//! already holds, which it turned at the old frequency (else they'd be turned a few symbols' worth of it too little:
//! a step in phase of tens of degrees)
void shiftFrequency(double cyclesPerSymbol, int afterSymbols);
double mer() const; //!< dB, from the decisions
//! dB, Es/N0 from the symbols' second and fourth moments (M2M4): unlike the MER, not raised by decision errors at
//! low SNR, and needing no decisions or phase
double snr() const;
uint64_t acquisitions() const { return m_acquisitions; }
static constexpr int m_acquireSymbols = 4096;
private:
static constexpr double m_pi = 3.141592653589793;
static constexpr double m_acquireBandwidth = 0.02; //!< Of the carrier loop, for its first symbols
static constexpr double m_trackBandwidth = 0.005;
static constexpr uint64_t m_acquireTrackSymbols = 2000; //!< At the acquisition bandwidth
static constexpr uint64_t m_unlockedLimit = 30000; //!< Symbols judged unlocked before acquiring again
static inline std::complex<float> rotation(double angle); //!< e^j(angle)
void computeGains(double bnT);
bool m_tracking;
std::vector<std::complex<double>> m_acquire; //!< Fourth powers
Cf m_rotator; //!< e^-j(phase)
double m_frequency; //!< Cycles a symbol
double m_pendingShift; //!< To take off the frequency after m_pendingSymbols
int m_pendingSymbols;
double m_kp, m_ki;
double m_bandwidth;
double m_lock; //!< Average of the fourth power's coherence: 1 without noise, 0 unlocked
double m_errorPower;
bool m_averaged; //!< The error power and moments have had an update since tracking started
double m_m2; //!< Averages of the symbols' power and its square, for snr()
double m_m4;
uint64_t m_tracked; //!< Symbols since tracking started
uint64_t m_unlocked; //!< Consecutive symbols judged unlocked
uint64_t m_acquisitions;
static constexpr double m_lockThreshold = 0.12;
};
//! DVB-S receiver (EN 300 421): samples at the channel rate in, transport stream packets out. If threaded, the front
//! end and carrier recovery run in a thread of their own, error correction in another, and packets are delivered in
//! the thread calling process(). Otherwise all in the caller's thread
class MODEMDVB_API Receiver
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback;
typedef std::complex<float> Cf;
enum class State { Spectrum = 0, Carrier = 1, Code = 2, Sync = 3, Locked = 4 };
struct Status
{
State m_state = State::Spectrum;
FrontEnd::Status m_frontEnd;
double m_symbolRate = 0.0; //!< Set
double m_frequencyOffset = 0.0; //!< Hz, of the carrier from the channel's centre
double m_clockOffset = 0.0; //!< ppm
bool m_carrierLocked = false;
double m_carrierLock = 0.0; //!< Lock metric, 0 to 1
double m_mer = 0.0; //!< dB
double m_snr = 0.0; //!< dB, Es/N0 from the symbols' moments; 0 until tracking
//! These two count on through reset() and configure() (as the error correction's searches and locks do), so
//! a change is seen whenever the status is read
uint64_t m_carrierAcquisitions = 0;
uint64_t m_restarts = 0; //!< Acquisitions started again (the spectrum measured again), without lock
FECDecoder::Status m_fec;
uint64_t m_queueWaits = 0;
double m_seconds[3] = {0.0, 0.0, 0.0}; //!< Front end, carrier recovery, error correction
};
explicit Receiver(bool threaded = true);
~Receiver();
//! The channel's sample rate, from 1.5 (1 + roll-off, for the matched filter) to 64 times the symbol rate (twice
//! or more is best). Acquires again
void configure(double inputRate, double symbolRate);
//! -1 to find the code rate, else its index (dvbt::CodeRate). From any thread
void setCodeRate(int rate) { m_codeRate = rate; }
//! The carrier loop's noise bandwidth times the symbol period, once tracking: wider follows more phase noise (as
//! a Ku band LNB's at low symbol rates), narrower slips less at low SNR. 0 (the default) for automatic: from 0.005
//! at an MER of 5 dB to 0.02 from 11 dB. From any thread
void setCarrierBandwidth(double bnT) { m_carrierBandwidth = bnT; }
void setPacketCallback(PacketCallback callback) { m_packetCallback = callback; }
void reset();
void process(const Cf *samples, int nbSamples);
//! Wait for everything queued to be demodulated and decoded, and deliver the packets
void flush();
//! The front end shared with the DVB-S2 receiver (by SharedFrontEnd, which runs it and gives this its symbols by
//! processSymbols()), or this receiver's own again (nullptr). With neither demodulating
void setSharedFrontEnd(FrontEnd *frontEnd);
//! The shared front end's symbols, in the thread running it
void processSymbols(const Cf *symbols, int nbSymbols);
//! Shared, in its thread: the symbols from here don't follow on from the last (samples were dropped)
void discontinuity();
//! Shared, in its thread: the front end's NCO moved by hz for the other receiver, from afterSymbols (those it held
//! were turned as before), so the carrier loop's frequency is moved to match
void frontEndShifted(double hz, int afterSymbols);
//! Shared, in its thread: the front end was reset for the other receiver (the spectrum measured again), so the
//! carrier's acquired again and the error correction searches again
void frontEndReset();
//! Shared: whether this has asked for the spectrum to be measured again (taken), and has a lock it'd lose
bool takeFrontEndRestart() { return m_frontEndRestart.exchange(false); }
bool frontEndLocked() const { return m_fecLocked; }
double frontEndEsN0() const { return m_carrier.tracking() ? m_carrier.snr() : 0.0; }
//! Its own front end's equaliser on (the default) or off (a shared front end's is set there)
void setEqualiser(bool enabled) { m_frontEnd.setEqualiser(enabled); }
//! The packets decoded, to the callback, in the caller's thread (process() does it unshared)
void deliverPackets();
Status getStatus() const;
//! Up to maxSymbols of the demodulated symbols, and the input's spectrum (dB, points from -rate/2 to +rate/2). If
//! threaded, from a moment ago (none the first time)
void getDisplayData(std::vector<Cf>& symbols, int maxSymbols, std::vector<float>& spectrumDb, int points) const;
private:
struct SoftBlock
{
bool m_reset = false;
uint64_t m_epoch = 0;
std::vector<int8_t> m_soft; //!< I and Q of each symbol
};
static constexpr float m_softScale = 16.0f * 1.41421356f; //!< As DVB-T's: 16 for each unit of an unnormalised QPSK point
static constexpr double m_foldBack = 0.02; //!< Cycles a symbol, of the loop's frequency, moved to the NCO
static constexpr double m_timingAcquire = 2e-3;
static constexpr double m_timingTrack = 5e-4;
static constexpr double m_relockSeconds = 4.0; //!< Without the FEC locked, before the spectrum's measured again
static constexpr double m_relockMinSymbols = 600000.0; //!< (and at least these symbols, for the search at low rates)
static constexpr int m_nbDisplaySymbols = 4096;
static double now();
void demodulate(const Cf *samples, int nbSamples);
//! The front end's symbols in m_symbols: the carrier, and the error correction. start and frontEndEnd for timing
void demodulateSymbols(double start, double frontEndEnd);
bool decodeBlock(SoftBlock& block);
//! To the error correction: a block of soft symbols (its buffer swapped for a free one), or a reset after a restart
void deliver(SoftBlock& block);
void deliverReset();
void runFEC();
bool idle() const;
bool m_threaded;
double m_inputRate;
double m_symbolRate;
std::atomic<int> m_codeRate;
std::atomic<double> m_carrierBandwidth;
PacketCallback m_packetCallback;
// Demodulation, in its own thread if threaded
FrontEnd m_frontEnd;
FrontEnd *m_fe; //!< m_frontEnd, or a shared one
std::atomic<bool> m_frontEndRestart; //!< Shared: asked for
QPSKCarrier m_carrier;
std::vector<Cf> m_symbols;
std::vector<Cf> m_tracked; //!< With the carrier taken off
std::atomic<bool> m_fecLocked;
std::unique_ptr<SampleThread> m_demodThread;
mutable std::mutex m_demodMutex; //!< For these
Status m_demodStatus;
mutable std::atomic<bool> m_displayRequested;
mutable int m_displayMaxSymbols;
mutable int m_displayPoints;
std::vector<Cf> m_displaySymbols;
std::vector<float> m_displaySpectrum;
std::vector<Cf> m_recentSymbols; //!< The last demodulated, for display
// Error correction, in its own thread if threaded
FECDecoder m_fec;
std::thread m_fecThread;
mutable std::mutex m_mutex; //!< For the queue, packets and status below
std::condition_variable m_available;
std::condition_variable m_space;
std::condition_variable m_idle;
std::deque<SoftBlock> m_queue;
std::vector<SoftBlock> m_free;
SoftBlock m_filling;
bool m_busy;
bool m_stop;
uint64_t m_resetEpoch;
uint64_t m_unlockedSymbols; //!< Since a packet last passed RS (demodulation thread)
std::atomic<bool> m_fecProgress; //!< A packet has passed RS (set by the FEC thread, taken by demodulation)
uint64_t m_lastGoodPackets; //!< Packets that passed RS, as last counted (FEC thread)
uint64_t m_restarts; //!< Acquisitions started again, without lock (demodulation thread)
uint64_t m_decodeEpoch;
int m_fecCodeRate; //!< As the decoder has it
std::vector<uint8_t> m_fecPackets; //!< From one block
std::vector<uint8_t> m_packets; //!< For the caller's thread
std::vector<uint8_t> m_delivering;
FECDecoder::Status m_fecStatus;
uint64_t m_queueWaits;
std::atomic<double> m_fecSeconds; //!< Written in the error correction thread
static constexpr size_t m_maxQueued = 64;
};
} // namespace dvbs
#endif // MODEMDVB_DVBSRECEIVER_H

460
modemdvb/dvbssharedfrontend.cpp

@ -0,0 +1,460 @@ @@ -0,0 +1,460 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <chrono>
#include <condition_variable>
#include <deque>
#include <functional>
#include <thread>
#include "util/profiler.h"
#include "dvbssharedfrontend.h"
#include "dvbsreceiver.h"
#include "dvbs2receiver.h"
#include "samplethread.h"
namespace dvbs {
// A block of the front end's symbols for DVB-S2, with what it needs of the front end then
struct SharedFrontEnd::S2Block
{
std::vector<Cf> m_symbols;
uint64_t m_first = 0; //!< Index of the first
FrontEnd::Status m_frontEnd;
uint64_t m_corrections = 0; //!< DVB-S2's frequency corrections made
uint64_t m_boundary = 0; //!< The first symbol after the last
double m_otherLoad = 0.0; //!< Cores the rest of DVB-S2's thread uses
bool m_feed = false; //!< The symbols to DVB-S2 (else only the reset or discontinuity)
bool m_reset = false;
bool m_discontinuity = false;
};
// Runs DVB-S2 on the blocks, in order, in a thread of its own. The caller waits if too many symbols are queued
class SharedFrontEnd::S2Thread
{
public:
explicit S2Thread(std::function<void(S2Block&)> process) :
m_process(process)
{
m_thread = std::thread(&S2Thread::run, this);
}
~S2Thread()
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_stop = true;
}
m_available.notify_all();
m_space.notify_all();
m_thread.join();
}
//! The block's taken, its symbols given a buffer to reuse
void push(S2Block& block)
{
S2Block queued;
queued.m_symbols.swap(block.m_symbols);
queued.m_first = block.m_first;
queued.m_frontEnd = block.m_frontEnd;
queued.m_corrections = block.m_corrections;
queued.m_boundary = block.m_boundary;
queued.m_otherLoad = block.m_otherLoad;
queued.m_feed = block.m_feed;
queued.m_reset = block.m_reset;
queued.m_discontinuity = block.m_discontinuity;
const size_t n = queued.m_symbols.size();
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_free.empty())
{
block.m_symbols.swap(m_free.back());
m_free.pop_back();
}
if (m_queued >= m_maxQueuedS2)
{
PROFILER_START()
m_space.wait(lock, [this]() { return m_stop || (m_queued < m_maxQueuedS2); });
PROFILER_STOP("DVB-S/S2 front end thread waiting for DVB-S2's thread")
}
m_queued += n;
m_queue.push_back(std::move(queued));
lock.unlock();
m_available.notify_one();
}
void drain()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_idle.wait(lock, [this]() { return m_queue.empty() && !m_busy; });
}
//! What's queued dropped, and waits for the block being processed
void discard()
{
std::unique_lock<std::mutex> lock(m_mutex);
while (!m_queue.empty())
{
recycle(m_queue.front());
m_queue.pop_front();
}
m_queued = 0;
m_space.notify_all();
m_idle.wait(lock, [this]() { return !m_busy; });
}
private:
void run()
{
while (true)
{
S2Block block;
{
std::unique_lock<std::mutex> lock(m_mutex);
m_available.wait(lock, [this]() { return m_stop || !m_queue.empty(); });
if (m_stop) {
return;
}
block = std::move(m_queue.front());
m_queue.pop_front();
m_queued -= block.m_symbols.size();
m_busy = true;
}
m_space.notify_one();
try {
m_process(block);
} catch (const std::exception&) {
}
{
std::lock_guard<std::mutex> lock(m_mutex);
m_busy = false;
recycle(block);
}
m_idle.notify_all();
}
}
// With m_mutex
void recycle(S2Block& block)
{
if ((m_free.size() < 16) && (block.m_symbols.capacity() > 0)) {
m_free.push_back(std::move(block.m_symbols));
}
}
std::function<void(S2Block&)> m_process;
size_t m_queued = 0;
bool m_busy = false;
bool m_stop = false;
std::thread m_thread;
std::mutex m_mutex;
std::condition_variable m_available;
std::condition_variable m_space;
std::condition_variable m_idle;
std::deque<S2Block> m_queue;
std::vector<std::vector<Cf>> m_free;
};
SharedFrontEnd::SharedFrontEnd(Receiver *receiverS, dvbs2::Receiver *receiverS2, bool threaded) :
m_receiverS(receiverS),
m_receiverS2(receiverS2),
m_feedS(true),
m_feedS2(true),
m_resetS(false),
m_resetS2(false)
{
m_frontEnd.setTimingBandwidth(m_timingAcquire);
m_receiverS->setSharedFrontEnd(&m_frontEnd);
m_receiverS2->setSharedFrontEnd(&m_frontEnd);
if (threaded)
{
m_threadS2.reset(new S2Thread([this](S2Block& block) {
demodulateS2(block);
}));
m_thread.reset(new SampleThread([this](const Cf *samples, int nbSamples) {
demodulate(samples, nbSamples);
}));
}
}
SharedFrontEnd::~SharedFrontEnd()
{
m_thread.reset();
m_threadS2.reset();
m_receiverS->setSharedFrontEnd(nullptr);
m_receiverS2->setSharedFrontEnd(nullptr);
}
void SharedFrontEnd::configure(double inputRate, double symbolRate)
{
discard();
m_frontEnd.configure(inputRate, symbolRate, 0.35);
m_frontEnd.setTimingBandwidth(m_timingAcquire);
m_receiverS->configure(inputRate, symbolRate);
m_receiverS2->configure(inputRate, symbolRate);
m_resetS = false;
m_resetS2 = false;
forgetRequestsS2();
}
void SharedFrontEnd::reset()
{
discard();
m_frontEnd.reset();
m_frontEnd.setTimingBandwidth(m_timingAcquire);
m_receiverS->reset();
m_receiverS2->reset();
m_resetS = false;
m_resetS2 = false;
forgetRequestsS2();
}
// The front end's thread first: it queues for DVB-S2's
void SharedFrontEnd::discard()
{
if (m_thread) {
m_thread->discard();
}
if (m_threadS2) {
m_threadS2->discard();
}
}
void SharedFrontEnd::setFeeding(bool s, bool s2)
{
m_feedS = s;
m_feedS2 = s2;
}
void SharedFrontEnd::process(const Cf *samples, int nbSamples)
{
if (m_thread) {
m_thread->push(samples, nbSamples);
} else {
demodulate(samples, nbSamples);
}
}
void SharedFrontEnd::discontinuity()
{
discard();
m_discontinuity = true;
}
void SharedFrontEnd::flush()
{
if (m_thread) {
m_thread->drain();
}
if (m_threadS2) {
m_threadS2->drain();
}
}
void SharedFrontEnd::forgetRequestsS2()
{
m_requestsS2 = m_receiverS2->frequencyRequests();
m_requestedS2 = m_receiverS2->frequencyRequested();
m_boundaryS2 = m_symbolIndex;
}
void SharedFrontEnd::takeRequestsS2()
{
const double rollOff = m_receiverS2->frontEndRollOff();
if (rollOff > 0.0) {
m_frontEnd.setRollOff(rollOff);
}
// The count first: the sum's then at least those
const uint64_t requests = m_receiverS2->frequencyRequests();
if (requests != m_requestsS2)
{
const double requested = m_receiverS2->frequencyRequested();
const int held = m_frontEnd.heldSymbols();
m_frontEnd.adjustFrequency(requested - m_requestedS2);
// The symbols of the samples it holds were turned by the NCO as it was. DVB-S's carrier loop, fed too, tracks
// what's left after the NCO: moved to match
m_boundaryS2 = m_symbolIndex + held;
if (m_feedS) {
m_receiverS->frontEndShifted(requested - m_requestedS2, held);
}
m_requestsS2 = requests;
m_requestedS2 = requested;
}
}
void SharedFrontEnd::demodulateS2(S2Block& block)
{
if (block.m_reset) {
m_receiverS2->reset();
}
if (block.m_discontinuity) {
m_receiverS2->discontinuity();
}
if (block.m_feed)
{
PROFILER_START()
m_receiverS2->processSymbols(block.m_symbols.data(), (int) block.m_symbols.size(), block.m_first, block.m_frontEnd,
block.m_corrections, block.m_boundary, block.m_otherLoad);
PROFILER_STOP(m_threadS2 ? "DVB-S2 thread: physical layer" : "DVB-S/S2 shared thread: DVB-S2")
}
}
FrontEnd::Status SharedFrontEnd::getStatus() const
{
std::lock_guard<std::mutex> lock(m_statusMutex);
return m_status;
}
void SharedFrontEnd::demodulate(const Cf *samples, int nbSamples)
{
if (m_resetS.exchange(false)) {
m_receiverS->reset();
}
// DVB-S2's in its thread, before the block's symbols
const bool resetS2 = m_resetS2.exchange(false);
const bool discontinuity = m_discontinuity.exchange(false);
if (discontinuity) {
m_receiverS->discontinuity();
}
const bool feedS = m_feedS, feedS2 = m_feedS2;
// DVB-S2's roll-off while it's fed (DVB-S can be received through a lower one's filter, if not as well), else
// DVB-S's
if (feedS2) {
takeRequestsS2();
} else if (feedS) {
m_frontEnd.setRollOff(rollOffValue(RollOff::R0_35));
}
const auto start = std::chrono::steady_clock::now();
PROFILER_START()
m_symbols.clear();
m_frontEnd.process(samples, nbSamples, m_symbols);
PROFILER_STOP(m_threadS2 ? "DVB-S/S2 front end thread: front end" : "DVB-S/S2 shared thread: front end")
const uint64_t first = m_symbolIndex;
m_symbolIndex += m_symbols.size();
// Each fed even without symbols (while the spectrum's measured), so its status and restarts go on
if (feedS)
{
PROFILER_RESTART()
m_receiverS->processSymbols(m_symbols.data(), (int) m_symbols.size());
PROFILER_STOP(m_threadS2 ? "DVB-S/S2 front end thread: DVB-S" : "DVB-S/S2 shared thread: DVB-S")
}
// The front end's and DVB-S's load (cores), so the DVB-S2 search, if in this thread too, has what's left
const double inputRate = m_frontEnd.inputRate();
if ((nbSamples > 0) && (inputRate > 0.0))
{
const double seconds = std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
m_otherLoad += 0.05 * (seconds * inputRate / nbSamples - m_otherLoad);
}
if (feedS2 || resetS2 || discontinuity)
{
S2Block block;
block.m_first = first;
block.m_frontEnd = m_frontEnd.getStatus();
block.m_corrections = m_requestsS2;
block.m_boundary = m_boundaryS2;
block.m_otherLoad = m_threadS2 ? 0.0 : m_otherLoad;
block.m_feed = feedS2;
block.m_reset = resetS2;
block.m_discontinuity = discontinuity;
if (m_threadS2)
{
if (feedS2)
{
m_symbolsS2.assign(m_symbols.begin(), m_symbols.end());
block.m_symbols.swap(m_symbolsS2);
}
m_threadS2->push(block);
m_symbolsS2.swap(block.m_symbols);
}
else
{
block.m_symbols.swap(m_symbols);
demodulateS2(block);
m_symbols.swap(block.m_symbols);
}
}
// The spectrum measured again if a receiver's asked (it's restarted its own part), unless the other has a lock
// that would lose. The timing loop tracking if either's locked
const bool restartS = feedS && m_receiverS->takeFrontEndRestart();
const bool restartS2 = feedS2 && m_receiverS2->takeFrontEndRestart();
const bool lockedS = feedS && m_receiverS->frontEndLocked();
const bool lockedS2 = feedS2 && m_receiverS2->frontEndLocked();
if ((restartS && !lockedS2) || (restartS2 && !lockedS))
{
m_frontEnd.reset();
forgetRequestsS2();
// DVB-S's carrier was of the front end before (unless it restarted itself, asking for this)
if (!restartS) {
m_receiverS->frontEndReset();
}
}
if (feedS2)
{
const double ppm = m_receiverS2->takeClockRequest();
if (ppm != 0.0) {
m_frontEnd.adjustClock(ppm);
}
}
// DVB-S2's lock, at its SNR (VL-SNR's can be far below DVB-S's)
m_frontEnd.setTimingBandwidth(lockedS2 ? FrontEnd::trackingBandwidth(m_timingTrack, m_receiverS2->frontEndEsN0())
: lockedS ? m_timingTrack : feedS2 ? dvbs2::Receiver::searchTimingBandwidth(m_frontEnd.getStatus().m_spectralEsN0) : m_timingAcquire);
m_frontEnd.setEqualiserReference(lockedS || lockedS2, lockedS2 ? m_receiverS2->frontEndEsN0() : m_receiverS->frontEndEsN0());
std::lock_guard<std::mutex> lock(m_statusMutex);
m_status = m_frontEnd.getStatus();
}
} // namespace dvbs

118
modemdvb/dvbssharedfrontend.h

@ -0,0 +1,118 @@ @@ -0,0 +1,118 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBSSHAREDFRONTEND_H
#define MODEMDVB_DVBSSHAREDFRONTEND_H
#include <atomic>
#include <memory>
#include <mutex>
#include <vector>
#include "export.h"
#include "dvbs.h"
#include "dvbsfrontend.h"
class SampleThread;
namespace dvbs2 {
class Receiver;
}
namespace dvbs {
class Receiver;
//! One front end (the spectrum's carrier estimate, the matched filter and timing) for a DVB-S and a DVB-S2 receiver,
//! in its own thread, its symbols to whichever are fed, rather than each running its own on the same samples (as
//! while Auto searches for either: half the front end's CPU, a fifth to a third of a core at broadcast rates). Each
//! DVB-S receiver's carrier runs in this thread too, so it moves the NCO in step with the symbols, as it would its own.
//! The DVB-S2 receiver's physical layer runs in a thread of its own beside it, its symbols queued (together the front
//! end and it took more than a core at 27.5 MS/s on a laptop): what it asks of the front end (the matched filter's
//! roll-off, large frequency corrections) is made by this thread between blocks. Its restarts (the spectrum measured
//! again) are those a receiver asks for, unless the other's locked; its timing loop tracks if either is locked
class MODEMDVB_API SharedFrontEnd
{
public:
//! The receivers use this front end until it's destroyed
SharedFrontEnd(Receiver *receiverS, dvbs2::Receiver *receiverS2, bool threaded = true);
~SharedFrontEnd();
//! The receivers' too, with nothing being demodulated
void configure(double inputRate, double symbolRate);
//! The front end and both receivers, the samples queued dropped (as on a retune)
void reset();
//! Waits for the block being demodulated, the samples queued dropped: the receivers can then be changed (as their
//! scrambling code) from the caller's thread, until the next process()
void discard();
//! Which receivers the symbols go to
void setFeeding(bool s, bool s2);
//! A receiver reset in this thread, before the next block (as when Auto finds the other, its lock not to be
//! interrupted by dropping samples)
void resetReceiverS() { m_resetS = true; }
void resetReceiverS2() { m_resetS2 = true; }
void process(const Cf *samples, int nbSamples);
//! Waits for everything queued to be demodulated (the receivers' error correction may still be decoding)
void flush();
//! Samples were dropped before the next: those queued are too, and the receivers start again from them (DVB-S2's
//! search, DVB-S's error correction)
void discontinuity();
FrontEnd::Status getStatus() const;
//! The equaliser on (the default) or off
void setEqualiser(bool enabled) { m_frontEnd.setEqualiser(enabled); }
private:
struct S2Block;
class S2Thread;
static constexpr double m_timingAcquire = 2e-3; //!< As the receivers'
static constexpr double m_timingTrack = 5e-4;
static constexpr size_t m_maxQueuedS2 = 1 << 20; //!< Symbols queued for DVB-S2, before the front end's thread waits
void demodulate(const Cf *samples, int nbSamples);
//! In DVB-S2's thread (or this one, unthreaded)
void demodulateS2(S2Block& block);
//! What the DVB-S2 receiver's asked of the front end, made between blocks
void takeRequestsS2();
//! Its frequency corrections asked for so far taken as made (the front end's starting again, or configured)
void forgetRequestsS2();
Receiver *m_receiverS;
dvbs2::Receiver *m_receiverS2;
FrontEnd m_frontEnd;
std::vector<Cf> m_symbols;
double m_otherLoad = 0.0; //!< Cores the front end and DVB-S receiver use, averaged
std::atomic<bool> m_discontinuity{false};
std::unique_ptr<SampleThread> m_thread;
std::unique_ptr<S2Thread> m_threadS2;
std::vector<Cf> m_symbolsS2; //!< A buffer to queue DVB-S2's symbols in
uint64_t m_symbolIndex = 0; //!< Symbols made, of all blocks
uint64_t m_requestsS2 = 0; //!< DVB-S2's frequency corrections made
double m_requestedS2 = 0.0; //!< Their sum, Hz
uint64_t m_boundaryS2 = 0; //!< The first symbol after the last
std::atomic<bool> m_feedS;
std::atomic<bool> m_feedS2;
std::atomic<bool> m_resetS;
std::atomic<bool> m_resetS2;
mutable std::mutex m_statusMutex;
FrontEnd::Status m_status;
};
} // namespace dvbs
#endif // MODEMDVB_DVBSSHAREDFRONTEND_H

419
modemdvb/dvbssymbolrate.cpp

@ -0,0 +1,419 @@ @@ -0,0 +1,419 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include "dvbssymbolrate.h"
namespace dvbs {
// Rates in common use (broadcast, and amateur DATV), which a measurement this close to is taken as
const double SymbolRateSearch::m_commonRates[20] = {
25000, 33000, 35000, 66000, 125000, 250000, 333000, 500000, 1000000, 1500000, 2000000, 4000000,
22000000, 22500000, 23000000, 27500000, 28000000, 29500000, 29900000, 30000000
};
void SymbolRateSearch::configure(double inputRate, double minRate, double maxRate)
{
m_inputRate = inputRate;
m_minRate = minRate;
m_maxRate = maxRate;
reset();
}
void SymbolRateSearch::setBand(double frequency, double width)
{
m_bandFrequency = frequency;
m_bandWidth = std::max(0.0, width);
// What's left of a holdoff kept (as reset(seconds)'s after a retune, which the band's set just after)
const int64_t holdoff = m_holdoff;
reset();
m_holdoff = holdoff;
}
void SymbolRateSearch::reset(double holdoffSeconds)
{
m_buffer.clear();
m_buffer.reserve(searchSamples);
m_result = Result();
m_holdoff = (int64_t) (std::max(0.0, holdoffSeconds) * m_inputRate);
}
bool SymbolRateSearch::process(const Cf *samples, int nbSamples)
{
bool ended = false;
while (nbSamples > 0)
{
if (m_holdoff > 0)
{
const int skip = (int) std::min((int64_t) nbSamples, m_holdoff);
m_holdoff -= skip;
samples += skip;
nbSamples -= skip;
continue;
}
const int n = std::min(nbSamples, searchSamples - (int) m_buffer.size());
m_buffer.insert(m_buffer.end(), samples, samples + n);
samples += n;
nbSamples -= n;
if ((int) m_buffer.size() == searchSamples)
{
// Not with zeros, as a device gives while it retunes (more than a run of them can be by chance): measured,
// they gave lines at 0 Hz hundreds of dB up, and the spectrum a dip that made its neighbours look like signals
if (zeroRunSamples() > searchSamples / 100)
{
m_buffer.clear();
continue;
}
search();
m_buffer.clear();
m_holdoff = m_result.m_found ? 0 : (int64_t) (m_retrySeconds * m_inputRate);
ended = true;
}
}
return ended;
}
// Samples in runs of zeros (exactly) 64 or longer
int SymbolRateSearch::zeroRunSamples() const
{
int total = 0, run = 0;
for (const Cf& s : m_buffer)
{
if ((s.real() == 0.0f) && (s.imag() == 0.0f))
{
run++;
}
else
{
total += run >= 64 ? run : 0;
run = 0;
}
}
return total + (run >= 64 ? run : 0);
}
std::vector<SymbolRateSearch::Signal> SymbolRateSearch::findSignals(const std::vector<double>& smooth, double floor,
double binHz, double origin, double minWidth, double over)
{
std::vector<Signal> signals;
const int n = (int) smooth.size();
// Every signal: each run over the threshold, its in-band level the median of its middle half, its half power
// points where it's half way from that to the floor (interpolated). Neighbours with no gap between run together
for (int i = 0; i < n; )
{
if (smooth[i] <= over * floor)
{
i++;
continue;
}
const int a = i;
while ((i < n) && (smooth[i] > over * floor)) {
i++;
}
const int b = i - 1;
const int inner = (b - a) / 4;
std::vector<double> mid(smooth.begin() + a + inner, smooth.begin() + b - inner + 1);
std::sort(mid.begin(), mid.end());
const double runLevel = mid[mid.size() / 2];
const double half = floor + 0.5 * (runLevel - floor);
// From the run's ends (3 dB up, which may be below half way), in or out to the first bins over half way
int lo = a, hi = b;
while ((lo < b) && (smooth[lo] <= half)) {
lo++;
}
while ((lo > 0) && (smooth[lo - 1] > half)) {
lo--;
}
while ((hi > lo) && (smooth[hi] <= half)) {
hi--;
}
while ((hi < n - 1) && (smooth[hi + 1] > half)) {
hi++;
}
const double low = lo > 0 ? lo - (smooth[lo] - half) / std::max(1e-30, smooth[lo] - smooth[lo - 1]) : lo;
const double high = hi < n - 1 ? hi + (smooth[hi] - half) / std::max(1e-30, smooth[hi] - smooth[hi + 1]) : hi;
Signal signal;
signal.m_frequency = origin + 0.5 * (low + high) * binHz;
signal.m_width = (high - low) * binHz;
signal.m_levelDb = 10.0 * std::log10(std::max(1e-30, runLevel - floor) / floor);
if (signal.m_width >= minWidth) {
signals.push_back(signal);
}
}
return signals;
}
// A Hann window of n points, in window (made again only if its size changes)
const std::vector<double>& SymbolRateSearch::hann(std::vector<double>& window, int n)
{
if ((int) window.size() != n)
{
window.resize(n);
for (int i = 0; i < n; i++) {
window[i] = 0.5 - 0.5 * std::cos(2.0 * m_pi * i / n);
}
}
return window;
}
void SymbolRateSearch::search()
{
m_result = Result();
if (m_inputRate <= 0.0) {
return;
}
// The averaged spectrum, centred
const int n = m_spectrumSize;
const double binHz = m_inputRate / n;
std::vector<double> power(n, 0.0);
const std::vector<double>& window = hann(m_spectrumWindow, n);
std::vector<std::complex<double>> x(n);
for (int b = 0; b + n <= searchSamples; b += n / 2)
{
for (int i = 0; i < n; i++) {
x[i] = std::complex<double>(m_buffer[b + i].real(), m_buffer[b + i].imag()) * window[i];
}
m_fft.transform(x);
for (int i = 0; i < n; i++) {
power[(i + n / 2) % n] += std::norm(x[i]);
}
}
// Smoothed with a median over half the lowest rate looked for (a quarter either side): that removes anything
// narrower than a quarter of it (spurs, as an SDR's LO leaking, offset from its centre, which can peak above the
// signal, and would be taken for it), while a signal's flat top and edges (monotonic) are kept
const int halfWindow = std::max(3, (int) std::lround(0.25 * m_minRate / binHz));
std::vector<double> smooth(n), window2;
for (int i = 0; i < n; i++)
{
const int from = std::max(0, i - halfWindow), to = std::min(n - 1, i + halfWindow);
window2.assign(power.begin() + from, power.begin() + to + 1);
std::nth_element(window2.begin(), window2.begin() + window2.size() / 2, window2.end());
smooth[i] = window2[window2.size() / 2];
}
std::vector<double> sorted(smooth);
std::sort(sorted.begin(), sorted.end());
const double floor = sorted[n / 5];
m_result.m_minWidth = 0.5 * m_minRate;
m_result.m_signals = findSignals(smooth, floor, binHz, -(n / 2) * binHz, m_result.m_minWidth);
m_result.m_binHz = binHz;
m_result.m_spectrum.resize(n);
for (int i = 0; i < n; i++) {
m_result.m_spectrum[i] = (float) smooth[i];
}
if (m_spectrumOnly) {
return;
}
// In a band, only its bins
int first = 0, last = n - 1;
if (m_bandWidth > 0.0)
{
first = std::max(0, (int) std::floor((m_bandFrequency - m_bandWidth / 2.0) / binHz) + n / 2);
last = std::min(n - 1, (int) std::ceil((m_bandFrequency + m_bandWidth / 2.0) / binHz) + n / 2);
if (last <= first) {
return;
}
}
const int peak = (int) (std::max_element(smooth.begin() + first, smooth.begin() + last + 1) - smooth.begin());
// The strongest signal's half power points: from its peak, then from its in-band level (the median of the middle
// of the band, as the peak's raised by noise), outwards to where it's half way to the floor, interpolated
double level = smooth[peak];
double lowEdge = peak, highEdge = peak;
for (int pass = 0; pass < 3; pass++)
{
const double half = floor + 0.5 * (level - floor);
int a = peak, b = peak;
while ((a > first) && (smooth[a - 1] > half)) {
a--;
}
while ((b < last) && (smooth[b + 1] > half)) {
b++;
}
lowEdge = (a > first) ? a - (smooth[a] - half) / std::max(1e-30, smooth[a] - smooth[a - 1]) : a;
highEdge = (b < last) ? b + (smooth[b] - half) / std::max(1e-30, smooth[b] - smooth[b + 1]) : b;
const int inner = std::max(1, (b - a) / 4);
const int from = std::min(a + inner, b), to = std::max(b - inner, from);
std::vector<double> band(smooth.begin() + from, smooth.begin() + to + 1);
std::sort(band.begin(), band.end());
level = band[band.size() / 2];
}
if (level < m_minLevel * floor) {
return;
}
m_result.m_width = (highEdge - lowEdge) * binHz;
m_result.m_frequency = (0.5 * (lowEdge + highEdge) - n / 2) * binHz;
// The squared magnitude's spectrum, over the whole buffer (its resolution sets the precision), near the width. Its
// line can be near half the sample rate (as at 2 samples a symbol), so then the samples are interpolated to twice
// the rate first (zero padding their spectrum)
const bool upsample = m_searchHigh * m_result.m_width > 0.4 * m_inputRate;
const int m = upsample ? 2 * searchSamples : searchSamples;
std::vector<std::complex<double>> z(searchSamples);
for (int i = 0; i < searchSamples; i++) {
z[i] = std::complex<double>(m_buffer[i].real(), m_buffer[i].imag());
}
// In a band, the input filtered to it first (the others' lines, and their mixing with it, gone)
if (m_bandWidth > 0.0)
{
m_fft.transform(z);
const double fullHz = m_inputRate / searchSamples;
for (int k = 0; k < searchSamples; k++)
{
const double f = (k < searchSamples / 2 ? k : k - searchSamples) * fullHz;
if (std::fabs(f - m_bandFrequency) > m_bandWidth / 2.0) {
z[k] = 0.0;
}
}
if (!upsample) {
m_fft.transform(z, true);
}
}
if (upsample)
{
if (m_bandWidth <= 0.0) {
m_fft.transform(z);
}
std::vector<std::complex<double>> padded(m, std::complex<double>(0.0, 0.0));
const int h = searchSamples / 2;
for (int i = 0; i < h; i++)
{
padded[i] = z[i];
padded[m - h + i] = z[searchSamples - h + i];
}
m_fft.transform(padded, true);
z.swap(padded);
}
std::vector<std::complex<double>> y(m);
double mean = 0.0;
for (int i = 0; i < m; i++) {
mean += std::norm(z[i]);
}
mean /= m;
const std::vector<double>& lineWindow = hann(m_lineWindow, m);
for (int i = 0; i < m; i++) {
y[i] = (std::norm(z[i]) - mean) * lineWindow[i];
}
m_fft.transform(y);
const double lineHz = (upsample ? 2.0 * m_inputRate : m_inputRate) / m;
// A little beyond the limits, so a rate at one is found (not taken for the edge of the range), then checked
const int lo = std::max(1, (int) std::floor(std::max(0.95 * m_minRate, m_searchLow * m_result.m_width) / lineHz));
const int hi = std::min(m / 2 - 2, (int) std::ceil(std::min(1.05 * m_maxRate, m_searchHigh * m_result.m_width) / lineHz));
if (hi <= lo + 4) {
return;
}
std::vector<double> mag(hi - lo + 1);
int best = lo;
for (int k = lo; k <= hi; k++)
{
mag[k - lo] = std::norm(y[k]);
if (mag[k - lo] > mag[best - lo]) {
best = k;
}
}
std::vector<double> magSorted(mag);
std::sort(magSorted.begin(), magSorted.end());
const double median = std::max(1e-30, magSorted[magSorted.size() / 2]);
m_result.m_lineSnr = 10.0 * std::log10(mag[best - lo] / median);
if ((m_result.m_lineSnr < m_minLineSnr) || (best <= lo) || (best >= hi)) {
return;
}
// Interpolated on the log magnitude (Gaussian), which for the Hann window's main lobe is close
const double a = std::log(std::norm(y[best - 1]) + 1e-30), b = std::log(std::norm(y[best]) + 1e-30);
const double c = std::log(std::norm(y[best + 1]) + 1e-30);
const double denom = a - 2.0 * b + c;
const double offset = denom < 0.0 ? std::max(-0.5, std::min(0.5, 0.5 * (a - c) / denom)) : 0.0;
double rate = (best + offset) * lineHz;
for (double common : m_commonRates)
{
if (std::fabs(rate - common) <= m_snapTolerance * common)
{
rate = common;
break;
}
}
m_result.m_symbolRate = rate;
m_result.m_found = (rate >= m_minRate * (1.0 - m_snapTolerance)) && (rate <= m_maxRate * (1.0 + m_snapTolerance));
}
} // namespace dvbs

113
modemdvb/dvbssymbolrate.h

@ -0,0 +1,113 @@ @@ -0,0 +1,113 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBSSYMBOLRATE_H
#define MODEMDVB_DVBSSYMBOLRATE_H
#include <cstdint>
#include <vector>
#include "export.h"
#include "dvbs.h"
namespace dvbs {
//! Finds a DVB-S or S2 signal's symbol rate without knowing it. The averaged spectrum gives the strongest signal's
//! half power width, which for a root raised cosine pulse is the symbol rate, to a few percent; then the spectrum of
//! the squared magnitude has a line at the symbol rate (for any roll-off above 0, whatever the carrier's frequency),
//! found near that, to tens of ppm: well within what the receivers' timing loops pull in
class MODEMDVB_API SymbolRateSearch
{
public:
//! A signal in the spectrum: where, and how wide (Hz, from the input's centre), and its level over the noise
struct Signal
{
double m_frequency = 0.0;
double m_width = 0.0; //!< Half power
double m_levelDb = 0.0;
};
struct Result
{
bool m_found = false;
double m_symbolRate = 0.0; //!< S/s
double m_frequency = 0.0; //!< Hz, of the signal's centre from the channel's
double m_width = 0.0; //!< Hz, half power
double m_lineSnr = 0.0; //!< dB, of the line over the squared magnitude's spectrum near it
std::vector<Signal> m_signals; //!< All those in the spectrum (3 dB or more over the noise), in frequency order
//! The averaged power spectrum, smoothed (spurs gone), centred: bin i at (i - size / 2) * m_binHz
std::vector<float> m_spectrum;
double m_binHz = 0.0;
double m_minWidth = 0.0; //!< Hz: signals narrower aren't taken
};
//! Symbol rates from minRate to maxRate are looked for
void configure(double inputRate, double minRate, double maxRate);
//! Only the signal in this band (Hz from the input's centre) is measured: the strongest there, its line from the
//! input filtered to the band. A width of 0 for the whole input (the default). Kept through reset() and configure().
//! Searches again, after any holdoff left (as a retune's)
void setBand(double frequency, double width);
//! Only the spectrum and its signals, no rate (as for a scan's sweep, which then costs little). Kept as setBand()
void setSpectrumOnly(bool spectrumOnly) { m_spectrumOnly = spectrumOnly; }
//! Searching again, after holdoffSeconds of samples (as after a retune, while the samples before it still come)
void reset(double holdoffSeconds = 0.0);
//! True when a search ends (each searchSamples samples), found or not
bool process(const Cf *samples, int nbSamples);
const Result& result() const { return m_result; }
//! The signals in a smoothed power spectrum (as m_spectrum, or several stitched together): each run over times
//! floor (3 dB by default), minWidth or wider at half power, in frequency order. Bin i at origin + i * binHz
static std::vector<Signal> findSignals(const std::vector<double>& smooth, double floor, double binHz, double origin,
double minWidth, double over = 2.0);
static const int searchSamples = 1 << 18;
private:
static constexpr double m_pi = 3.141592653589793;
static constexpr int m_spectrumSize = 4096; //!< FFT of the averaged spectrum
static constexpr double m_minLevel = 1.25; //!< In-band level over the floor, for a signal to be measured
static constexpr double m_minLineSnr = 10.0; //!< dB, for the line to be believed
static constexpr double m_searchLow = 0.8; //!< The line's looked for from this times the half power width
static constexpr double m_searchHigh = 1.5; //!< to this: the width's narrowed when the device's filter cuts
//!< the edges, as at 1.25 times the symbol rate (27.5 MS/s 0.76 of it)
static constexpr double m_snapTolerance = 2e-4; //!< A common rate this close is taken as the rate
static constexpr double m_retrySeconds = 0.2; //!< Between searches that find nothing, so one taking longer than its
//!< samples take to arrive (at high sample rates) doesn't hold up the
//!< caller for long
static const double m_commonRates[20]; //!< Symbol rates in common use
void search();
int zeroRunSamples() const;
static const std::vector<double>& hann(std::vector<double>& window, int n);
double m_inputRate = 0.0;
double m_minRate = 0.0;
double m_maxRate = 0.0;
double m_bandFrequency = 0.0;
double m_bandWidth = 0.0;
bool m_spectrumOnly = false;
std::vector<Cf> m_buffer;
Result m_result;
int64_t m_holdoff = 0; //!< Samples skipped before the next search
FFT m_fft; //!< Its twiddles kept from search to search (on the caller's thread, at up to 2^19 points)
std::vector<double> m_spectrumWindow;
std::vector<double> m_lineWindow; //!< Of the squared magnitude, searchSamples or twice that
};
} // namespace dvbs
#endif // MODEMDVB_DVBSSYMBOLRATE_H

351
modemdvb/dvbt.cpp

@ -0,0 +1,351 @@ @@ -0,0 +1,351 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include "dvbt.h"
namespace dvbt {
int fftSize(Mode mode)
{
return mode == Mode::Mode2K ? 2048 : 8192;
}
int kMax(Mode mode)
{
return mode == Mode::Mode2K ? 1704 : 6816;
}
int dataCarriers(Mode mode)
{
return mode == Mode::Mode2K ? 1512 : 6048;
}
int guardSamples(Mode mode, GuardInterval guard)
{
return fftSize(mode) / (32 >> (int) guard);
}
int bitsPerCell(Constellation constellation)
{
return 2 * ((int) constellation + 1);
}
void codeRateFraction(CodeRate rate, int& num, int& den)
{
static const int nums[] = {1, 2, 3, 5, 7};
num = nums[(int) rate];
den = num + 1;
}
double sampleRate(int bandwidthHz)
{
return 8.0 * bandwidthHz / 7.0; // Elementary period T = 7/64 us for 8 MHz
}
const char *toString(Mode mode)
{
return mode == Mode::Mode2K ? "2K" : "8K";
}
const char *toString(Constellation constellation)
{
static const char *names[] = {"QPSK", "16-QAM", "64-QAM"};
return names[(int) constellation];
}
const char *toString(Hierarchy hierarchy)
{
static const char *names[] = {"None", "a=1", "a=2", "a=4"};
return names[(int) hierarchy];
}
const char *toString(CodeRate rate)
{
static const char *names[] = {"1/2", "2/3", "3/4", "5/6", "7/8"};
return names[(int) rate];
}
const char *toString(GuardInterval guard)
{
static const char *names[] = {"1/32", "1/16", "1/8", "1/4"};
return names[(int) guard];
}
double netBitRate(int bandwidthHz, Mode mode, Constellation constellation, CodeRate rate, GuardInterval guard)
{
int num, den;
codeRateFraction(rate, num, den);
double symbolDuration = (fftSize(mode) + guardSamples(mode, guard)) / sampleRate(bandwidthHz);
double bits = dataCarriers(mode) * bitsPerCell(constellation) * (double) num / den * tsPacketSize / rsPacketSize;
return bits / symbolDuration;
}
// TPS
TPS::TPS() :
m_frameNumber(0),
m_constellation(Constellation::QAM64),
m_hierarchy(Hierarchy::None),
m_hpCodeRate(CodeRate::R2_3),
m_lpCodeRate(CodeRate::R1_2),
m_guard(GuardInterval::G1_32),
m_mode(Mode::Mode8K),
m_lengthIndicator(23),
m_cellIdByte(0),
m_inDepth(false),
m_bchOK(false)
{
}
bool TPS::sameParameters(const TPS& other) const
{
return (m_constellation == other.m_constellation)
&& (m_hierarchy == other.m_hierarchy)
&& (m_hpCodeRate == other.m_hpCodeRate)
&& (m_lpCodeRate == other.m_lpCodeRate)
&& (m_guard == other.m_guard)
&& (m_mode == other.m_mode)
&& (m_inDepth == other.m_inDepth);
}
int TPS::getBits(const uint8_t *bits, int start, int count)
{
int v = 0;
for (int i = 0; i < count; i++) {
v = (v << 1) | (bits[start + i] & 1);
}
return v;
}
void TPS::setBits(uint8_t *bits, int start, int count, int value)
{
for (int i = 0; i < count; i++) {
bits[start + i] = (value >> (count - 1 - i)) & 1;
}
}
// BCH(67,53, t=2), shortened from BCH(127,113): g(x) = x^14 + x^9 + x^8 + x^6 + x^5 + x^4 + x^2 + x + 1 (4.6.2.11)
// Remainder of the polynomial with coefficients bits[start..start+count-1] (highest degree first) times x^14
uint16_t TPS::bchRemainder(const uint8_t *bits, int start, int count)
{
const uint16_t g = 0x0377; // g(x) without x^14
uint16_t rem = 0;
for (int i = 0; i < count; i++)
{
int feedback = ((rem >> 13) & 1) ^ (bits[start + i] & 1);
rem = (rem << 1) & 0x3fff;
if (feedback) {
rem ^= g;
}
}
return rem;
}
bool TPS::checkBCH(const uint8_t bits[symbolsPerFrame])
{
return bchRemainder(bits, 1, 67) == 0; // s1..s67
}
void TPS::addBCH(uint8_t bits[symbolsPerFrame])
{
setBits(bits, 54, 14, bchRemainder(bits, 1, 53)); // Parity of s1..s53 into s54..s67
}
bool TPS::decode(const uint8_t bits[symbolsPerFrame])
{
int sync = getBits(bits, 1, 16);
int frameNumber = getBits(bits, 23, 2);
bool oddFrame = (frameNumber & 1) == 0; // Frames 1 and 3 have the non-inverted sync word
if (sync != (oddFrame ? m_syncWord : (~m_syncWord & 0xffff))) {
return false;
}
int constellation = getBits(bits, 25, 2);
int hierarchy = getBits(bits, 28, 2); // s27 is the in-depth interleaver flag (DVB-H, F.3.4.2.2)
int hpCodeRate = getBits(bits, 30, 3);
int lpCodeRate = getBits(bits, 33, 3);
int mode = getBits(bits, 38, 2);
if ((constellation > 2) || (hpCodeRate > 4) || (lpCodeRate > 4) || (mode > 1)) {
return false;
}
m_frameNumber = frameNumber;
m_lengthIndicator = getBits(bits, 17, 6);
m_constellation = (Constellation) constellation;
m_hierarchy = (Hierarchy) hierarchy;
m_inDepth = (bits[27] & 1) != 0;
m_hpCodeRate = (CodeRate) hpCodeRate;
m_lpCodeRate = (CodeRate) lpCodeRate;
m_guard = (GuardInterval) getBits(bits, 36, 2);
m_mode = (Mode) mode;
m_cellIdByte = getBits(bits, 40, 8);
m_bchOK = checkBCH(bits);
return true;
}
void TPS::encode(uint8_t bits[symbolsPerFrame]) const
{
bool oddFrame = (m_frameNumber & 1) == 0;
bits[0] = 0;
setBits(bits, 1, 16, oddFrame ? m_syncWord : (~m_syncWord & 0xffff));
setBits(bits, 17, 6, m_lengthIndicator);
setBits(bits, 23, 2, m_frameNumber);
setBits(bits, 25, 2, (int) m_constellation);
setBits(bits, 27, 3, (m_inDepth ? 4 : 0) | (int) m_hierarchy);
setBits(bits, 30, 3, (int) m_hpCodeRate);
setBits(bits, 33, 3, (int) m_lpCodeRate);
setBits(bits, 36, 2, (int) m_guard);
setBits(bits, 38, 2, (int) m_mode);
setBits(bits, 40, 8, m_cellIdByte);
setBits(bits, 48, 6, 0);
addBCH(bits);
}
// Tables
// Continual pilot carrier indices for 2K mode (4.5.3, table 7). The 8K positions are these repeated
// every 1704 carriers, plus 6816
const int Tables::m_continualPilots2K[] = {
0, 48, 54, 87, 141, 156, 192, 201, 255, 279, 282, 333, 432, 450, 483, 525, 531, 618, 636, 714, 759, 765,
780, 804, 873, 888, 918, 939, 942, 969, 984, 1050, 1101, 1107, 1110, 1137, 1140, 1146, 1206, 1269, 1323,
1377, 1491, 1683, 1704
};
// TPS carrier indices for 2K mode (4.6, table 8). The 8K positions are these repeated every 1704 carriers
const int Tables::m_tpsCarriers2K[] = {
34, 50, 209, 346, 413, 569, 595, 688, 790, 901, 1073, 1219, 1262, 1286, 1469, 1594, 1687
};
const Tables& Tables::get(Mode mode)
{
static const Tables tables2K(Mode::Mode2K);
static const Tables tables8K(Mode::Mode8K);
return mode == Mode::Mode2K ? tables2K : tables8K;
}
Tables::Tables(Mode mode) :
m_mode(mode),
m_fftSize(fftSize(mode)),
m_kMax(kMax(mode)),
m_nbData(dataCarriers(mode))
{
const int nbCPs2K = sizeof(m_continualPilots2K) / sizeof(m_continualPilots2K[0]);
const int nbTPS2K = sizeof(m_tpsCarriers2K) / sizeof(m_tpsCarriers2K[0]);
const int repeats = (m_kMax + 1) / 1704;
for (int r = 0; r < repeats; r++)
{
for (int i = 0; i < nbCPs2K - 1; i++) {
m_continualPilots.push_back(m_continualPilots2K[i] + 1704 * r);
}
for (int i = 0; i < nbTPS2K; i++) {
m_tpsCarriers.push_back(m_tpsCarriers2K[i] + 1704 * r);
}
}
m_continualPilots.push_back(m_kMax);
// Pilot reference sequence: x^11 + x^2 + 1, register initialised to all ones (4.5.2)
m_prbs.resize(m_kMax + 1);
unsigned int reg = 0x7ff;
for (int k = 0; k <= m_kMax; k++)
{
m_prbs[k] = reg & 1;
unsigned int bit = (reg ^ (reg >> 2)) & 1;
reg = (reg >> 1) | (bit << 10);
}
// Carrier types for each of the four scattered pilot phases (4.5.3)
for (int p = 0; p < 4; p++)
{
m_carrierType[p].assign(m_kMax + 1, 0);
for (int k = 3 * p; k <= m_kMax; k += 12) {
m_carrierType[p][k] = 1;
}
for (int k : m_continualPilots) {
m_carrierType[p][k] = 1;
}
for (int k : m_tpsCarriers) {
m_carrierType[p][k] = 2;
}
for (int k = 0; k <= m_kMax; k++)
{
if (m_carrierType[p][k] == 0) {
m_dataCarriers[p].push_back(k);
}
}
}
// Symbol interleaver permutation H(q) (4.3.4.2)
const int nr = mode == Mode::Mode2K ? 11 : 13;
const int mMax = 1 << nr;
static const int perm2K[] = {4, 3, 9, 6, 2, 8, 1, 5, 7, 0}; // R' bit j goes to R bit perm[j]
static const int perm8K[] = {7, 1, 4, 2, 9, 6, 8, 10, 0, 3, 11, 5};
const int *perm = mode == Mode::Mode2K ? perm2K : perm8K;
unsigned int rp = 0; // R'_i
m_symbolInterleaver.resize(m_nbData);
for (int i = 0, q = 0; i < mMax; i++)
{
if (i < 2) {
rp = 0;
} else if (i == 2) {
rp = 1;
}
else
{
unsigned int bit = mode == Mode::Mode2K
? ((rp >> 0) ^ (rp >> 3)) & 1
: ((rp >> 0) ^ (rp >> 1) ^ (rp >> 4) ^ (rp >> 6)) & 1;
rp = (rp >> 1) | (bit << (nr - 2));
}
unsigned int r = 0;
for (int j = 0; j < nr - 1; j++) {
r |= ((rp >> j) & 1) << perm[j];
}
int h = (i % 2) * (1 << (nr - 1)) + r;
if (h < m_nbData) {
m_symbolInterleaver[q++] = h;
}
}
}
} // namespace dvbt

120
modemdvb/dvbt.h

@ -0,0 +1,120 @@ @@ -0,0 +1,120 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT_H
#define MODEMDVB_DVBT_H
#include <cstdint>
#include <complex>
#include <vector>
#include "export.h"
// DVB-T as defined in ETSI EN 300 744. Section references in this library are to that document
namespace dvbt {
typedef std::complex<float> Cf;
// Values are those used in the TPS (EN 300 744 4.6.2)
enum class Mode { Mode2K = 0, Mode8K = 1 };
enum class Constellation { QPSK = 0, QAM16 = 1, QAM64 = 2 };
enum class Hierarchy { None = 0, Alpha1 = 1, Alpha2 = 2, Alpha4 = 3 };
enum class CodeRate { R1_2 = 0, R2_3 = 1, R3_4 = 2, R5_6 = 3, R7_8 = 4 };
enum class GuardInterval { G1_32 = 0, G1_16 = 1, G1_8 = 2, G1_4 = 3 };
static const int symbolsPerFrame = 68;
static const int framesPerSuperFrame = 4;
static const int tsPacketSize = 188;
static const int rsPacketSize = 204;
MODEMDVB_API int fftSize(Mode mode);
MODEMDVB_API int kMax(Mode mode); //!< Index of highest carrier (Kmin is 0)
MODEMDVB_API int dataCarriers(Mode mode); //!< Data cells per OFDM symbol
MODEMDVB_API int guardSamples(Mode mode, GuardInterval guard);
MODEMDVB_API int bitsPerCell(Constellation constellation);
MODEMDVB_API void codeRateFraction(CodeRate rate, int& num, int& den);
MODEMDVB_API double sampleRate(int bandwidthHz); //!< 64/7 MS/s for 8 MHz channels
MODEMDVB_API const char *toString(Mode mode);
MODEMDVB_API const char *toString(Constellation constellation);
MODEMDVB_API const char *toString(Hierarchy hierarchy);
MODEMDVB_API const char *toString(CodeRate rate);
MODEMDVB_API const char *toString(GuardInterval guard);
MODEMDVB_API double netBitRate(int bandwidthHz, Mode mode, Constellation constellation, CodeRate rate, GuardInterval guard);
// Transmission Parameter Signalling (4.6)
struct MODEMDVB_API TPS
{
int m_frameNumber; //!< 0..3 within the super-frame
Constellation m_constellation;
Hierarchy m_hierarchy;
CodeRate m_hpCodeRate;
CodeRate m_lpCodeRate;
GuardInterval m_guard;
Mode m_mode;
int m_lengthIndicator; //!< 23, or 31 if the cell id is signalled (33 with DVB-H signalling)
int m_cellIdByte; //!< Half of the cell id carried in this frame: high byte in frames 1 and 3
bool m_inDepth; //!< DVB-H's in-depth inner interleaver (s27, annex F), for 2K (and 4K) only
bool m_bchOK;
TPS();
bool sameParameters(const TPS& other) const; //!< Compare the fields that set up the receiver
//! Decode a 68-bit TPS block, s0..s67. Returns false if the sync word or a field is invalid
bool decode(const uint8_t bits[symbolsPerFrame]);
//! Encode into a 68-bit TPS block (s0 is set to 0)
void encode(uint8_t bits[symbolsPerFrame]) const;
static constexpr uint16_t m_syncWord = 0x35ee; //!< 0011010111101110, frames 1 and 3
static bool checkBCH(const uint8_t bits[symbolsPerFrame]);
static void addBCH(uint8_t bits[symbolsPerFrame]);
private:
static int getBits(const uint8_t *bits, int start, int count);
static void setBits(uint8_t *bits, int start, int count, int value);
static uint16_t bchRemainder(const uint8_t *bits, int start, int count);
};
// Per-mode carrier tables (4.5)
class MODEMDVB_API Tables
{
public:
static const Tables& get(Mode mode);
Mode m_mode;
int m_fftSize;
int m_kMax;
int m_nbData;
std::vector<int> m_continualPilots; //!< Carrier indices
std::vector<int> m_tpsCarriers; //!< Carrier indices
std::vector<uint8_t> m_prbs; //!< w_k for k = 0..Kmax (4.5.2)
//! Per scattered pilot phase (l mod 4): carrier type, 0 = data, 1 = scattered/continual pilot, 2 = TPS
std::vector<uint8_t> m_carrierType[4];
//! Per scattered pilot phase: indices of the data carriers, in increasing order
std::vector<int> m_dataCarriers[4];
std::vector<int> m_symbolInterleaver; //!< H(q), 4.3.4.2
private:
explicit Tables(Mode mode);
static const int m_continualPilots2K[]; //!< 2K mode (4.5.3, table 7)
static const int m_tpsCarriers2K[]; //!< 2K mode (4.6, table 8)
};
} // namespace dvbt
#endif // MODEMDVB_DVBT_H

1028
modemdvb/dvbt2.cpp

File diff suppressed because it is too large Load Diff

307
modemdvb/dvbt2.h

@ -0,0 +1,307 @@ @@ -0,0 +1,307 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2_H
#define MODEMDVB_DVBT2_H
#include <algorithm>
#include <complex>
#include <cstdint>
#include <vector>
#include "export.h"
// DVB-T2 (ETSI EN 302 755 V1.4.1) parameters, L1 signalling and carrier tables. T2-base profile, SISO
namespace dvbt2 {
typedef std::complex<float> Cf;
//! S1 field of P1 (table 18)
enum class PreambleType { T2SISO = 0, T2MISO = 1, NonT2 = 2, T2LiteSISO = 3, T2LiteMISO = 4 };
//! GUARD_INTERVAL field of L1-pre (table 22)
enum class GuardInterval { G1_32 = 0, G1_16 = 1, G1_8 = 2, G1_4 = 3, G1_128 = 4, G19_128 = 5, G19_256 = 6 };
//! PILOT_PATTERN field (table 27)
enum class PilotPattern { PP1 = 0, PP2, PP3, PP4, PP5, PP6, PP7, PP8 };
//! PLP_MOD field (table 33)
enum class Constellation { QPSK = 0, QAM16 = 1, QAM64 = 2, QAM256 = 3 };
//! L1_MOD field (table 24)
enum class L1Constellation { BPSK = 0, QPSK = 1, QAM16 = 2, QAM64 = 3 };
//! PLP_COD field (table 32). 1/3 and 2/5 are T2-Lite only. 1/4 is only for L1-pre
//! LDPC code rates. 8/9 and 9/10 are DVB-S2's alone
//! T2's, then S2's (from R1_3), then S2X's (EN 302 307-2, from R13_45: its LDPC code identifiers; 26/45 and 32/45 are
//! codes in both frame sizes, the others in one)
enum class CodeRate { R1_2 = 0, R3_5 = 1, R2_3 = 2, R3_4 = 3, R4_5 = 4, R5_6 = 5, R1_3 = 6, R2_5 = 7, R1_4 = 8, R8_9 = 9, R9_10 = 10,
R13_45, R9_20, R11_20, R26_45, R28_45, R23_36, R25_36, R13_18, R7_9, R90_180, R96_180, R100_180, R104_180,
R116_180, R124_180, R128_180, R132_180, R135_180, R140_180, R154_180, R18_30, R20_30, R22_30,
R11_45, R4_15, R14_45, R7_15, R8_15, R32_45, R2_9, R1_5 }; // 2/9 normal and 1/5 medium: S2X's VL-SNR codes
static const int nbCodeRates = (int) CodeRate::R1_5 + 1;
//! One of S2X's
inline bool isS2X(CodeRate rate) { return (int) rate >= (int) CodeRate::R13_45; }
//! PLP_FEC_TYPE field (table 34)
//! 16K and 64K LDPC. Medium (32 400 bits) is only S2X's, for VL-SNR (EN 302 307-2 table 5)
enum class FECType { Short = 0, Normal = 1, Medium = 2 };
//! Whose LDPC and BCH codes: DVB-T2's (EN 302 755) or DVB-S2's (EN 302 307), which share most, but S2's 64K 2/3 and 16K
//! 3/5 codes are different, it has rates T2 hasn't (64K 1/4, 1/3, 2/5, 8/9, 9/10 and 16K 8/9), and its 8/9 and 9/10
//! have t = 8 BCH
enum class CodeFamily { T2 = 0, S2 = 1 };
MODEMDVB_API const char *toString(GuardInterval guard);
MODEMDVB_API const char *toString(PilotPattern pattern);
MODEMDVB_API const char *toString(Constellation constellation);
MODEMDVB_API const char *toString(L1Constellation constellation);
MODEMDVB_API const char *toString(CodeRate rate);
MODEMDVB_API const char *fftSizeString(int fftSize);
MODEMDVB_API double sampleRate(int bandwidthHz); //!< 1/T (table 65)
MODEMDVB_API int guardSamples(int fftSize, GuardInterval guard);
MODEMDVB_API bool guardAllowed(int fftSize, GuardInterval guard); //!< Table 67
MODEMDVB_API int numP2(int fftSize); //!< Table 51
MODEMDVB_API int kMaxNormal(int fftSize); //!< Table 66
MODEMDVB_API int kExt(int fftSize);
MODEMDVB_API int dx(PilotPattern pattern); //!< Table 57
MODEMDVB_API int dy(PilotPattern pattern);
//! Table 64 for SISO. In MISO, always but with PP8 (9.2.7)
MODEMDVB_API bool frameClosingSymbol(int fftSize, GuardInterval guard, PilotPattern pattern, bool miso = false);
MODEMDVB_API int bitsPerCell(Constellation constellation);
MODEMDVB_API int bitsPerCell(L1Constellation constellation);
MODEMDVB_API int nldpc(FECType type);
//! Table 6 (EN 302 307 tables 5a and 5b for S2). 0 if the family hasn't the code
MODEMDVB_API int kldpc(FECType type, CodeRate rate, CodeFamily family = CodeFamily::T2);
MODEMDVB_API int kbch(FECType type, CodeRate rate, CodeFamily family = CodeFamily::T2);
//! Error correction capability
MODEMDVB_API int bchT(FECType type, CodeRate rate, CodeFamily family = CodeFamily::T2);
MODEMDVB_API int cellsPerFECBlock(FECType type, Constellation constellation); //!< Table 17
//! FFT size from S2 field 1 (table 19(a)) of T2-base or T2-Lite, or 0 if reserved, and whether the guard interval is one
//! of 1/128, 19/256, 19/128
MODEMDVB_API int fftSizeFromS2(int s2, bool& newGuards, bool lite = false);
//! Which T2 profile to receive: Auto is T2-base, or T2-Lite when it's alone (its P1s don't signal other preambles)
enum class Profile { Auto = 0, Base = 1, Lite = 2 };
//! The P1 signalling
struct MODEMDVB_API P1Signalling
{
int m_s1 = 0;
int m_s2 = 0; //!< Field 1 in bits 3-1, mixed flag in bit 0
int fftSize() const; //!< 0 if not T2-base or T2-Lite, or reserved
bool isT2Base() const { return (m_s1 == (int) PreambleType::T2SISO) || (m_s1 == (int) PreambleType::T2MISO); }
bool isT2Lite() const { return (m_s1 == (int) PreambleType::T2LiteSISO) || (m_s1 == (int) PreambleType::T2LiteMISO); }
bool isT2() const { return isT2Base() || isT2Lite(); }
bool isMISO() const { return (m_s1 == (int) PreambleType::T2MISO) || (m_s1 == (int) PreambleType::T2LiteMISO); }
bool hasFEF() const { return (m_s2 & 1) != 0; }
};
//! L1-pre's BPSK cells: the first l1PreCells / NP2 of each P2 symbol (8.3.5)
static const int l1PreCells = 1840;
//! L1-pre signalling (7.2.2), 200 bits including the CRC
struct MODEMDVB_API L1Pre
{
static const int size = 200;
int m_type = 0;
bool m_bwtExt = false;
int m_s1 = 0;
int m_s2 = 0;
bool m_l1RepetitionFlag = false;
GuardInterval m_guard = GuardInterval::G1_32;
int m_papr = 0;
L1Constellation m_l1Mod = L1Constellation::BPSK;
int m_l1Cod = 0;
int m_l1FecType = 0;
int m_l1PostSize = 0; //!< In cells
int m_l1PostInfoSize = 0; //!< In bits, excluding the CRC
PilotPattern m_pilotPattern = PilotPattern::PP1;
int m_txIdAvailability = 0;
int m_cellId = 0;
int m_networkId = 0;
int m_t2SystemId = 0;
int m_numT2Frames = 0;
int m_numDataSymbols = 0;
int m_regenFlag = 0;
bool m_l1PostExtension = false;
int m_numRf = 1;
int m_currentRfIdx = 0;
int m_t2Version = 0;
bool m_l1PostScrambled = false;
bool m_t2BaseLite = false;
//! bits: one per byte, MSB first. Returns false if the CRC is wrong
bool decode(const uint8_t *bits);
void encode(uint8_t *bits) const;
int fftSize() const;
//! Tone reservation in the data symbols (table 23)
bool toneReservation() const { return (m_papr & 2) != 0; }
private:
friend struct L1Post;
//! Bit fields, MSB first, one bit per byte
static uint32_t getBits(const uint8_t *bits, int& offset, int nb);
static void putBits(std::vector<uint8_t>& bits, uint32_t value, int nb);
};
//! A PLP, from the configurable and dynamic L1-post signalling
struct MODEMDVB_API PLP
{
int m_id = 0;
int m_type = 1; //!< 0 common, 1 data type 1, 2 data type 2
int m_payloadType = 3; //!< 3 TS
bool m_ffFlag = false;
int m_firstRfIdx = 0;
int m_firstFrameIdx = 0;
int m_groupId = 0;
CodeRate m_codeRate = CodeRate::R2_3;
Constellation m_constellation = Constellation::QAM256;
bool m_rotation = false;
FECType m_fecType = FECType::Normal;
int m_numBlocksMax = 0;
int m_frameInterval = 1;
int m_timeIlLength = 0;
bool m_timeIlType = false;
bool m_inBandA = false;
bool m_inBandB = false;
int m_reserved1 = 0;
int m_plpMode = 0;
bool m_staticFlag = false;
bool m_staticPaddingFlag = false;
bool m_valid = true; //!< Its code rate, modulation and FEC type aren't reserved for the profile, so it can be decoded
// Dynamic
int m_start = 0; //!< Cell address
int m_numBlocks = 0; //!< FEC blocks in the interleaving frame
int interleavingFrames() const { return m_timeIlType ? m_timeIlLength : 1; } //!< PI, T2-frames per interleaving frame
int tiBlocks() const { return m_timeIlType ? 1 : std::max(1, m_timeIlLength); } //!< NTI
};
//! L1-post signalling (7.2.3): configurable and dynamic parts
struct MODEMDVB_API L1Post
{
int m_subSlicesPerFrame = 1;
int m_numAux = 0;
int m_auxConfigRfu = 0;
std::vector<uint32_t> m_frequencies; //!< Indexed by RF_IDX
int m_fefType = 0;
int m_fefLength = 0;
int m_fefInterval = 0;
std::vector<PLP> m_plps;
int m_fefLengthMsb = 0;
// Dynamic
int m_frameIdx = 0;
int m_subSliceInterval = 0;
int m_type2Start = 0;
int m_l1ChangeCounter = 0;
int m_startRfIdx = 0;
//! Parse L1_POST_INFO_SIZE + 32 bits (one per byte), checking the CRC
bool decode(const uint8_t *bits, const L1Pre& pre);
//! Configurable, dynamic and CRC bits. Sets the info size in pre
void encode(std::vector<uint8_t>& bits, L1Pre& pre) const;
};
MODEMDVB_API uint32_t crc32(const uint8_t *bits, int nbBits); //!< Annex F, one bit per byte
MODEMDVB_API uint8_t crc8(const uint8_t *bytes, int nbBytes); //!< Annex F
//! Energy dispersal (5.2.4) of nbBits bits, one per byte, starting from the initial state
MODEMDVB_API void scramble(uint8_t *bits, int nbBits);
//! The same, on bytes, MSB first
MODEMDVB_API void scrambleBytes(uint8_t *bytes, int nbBytes);
//! Carrier types and frequency interleaver for one combination of FFT size, carrier mode, pilot pattern and
//! tone reservation. Carriers are indexed from 0 to kMax (extended mode includes Kext each side)
class MODEMDVB_API Tables
{
public:
struct Pilot
{
int m_k;
float m_amplitude;
};
static const Tables& get(int fftSize, bool extended, PilotPattern pattern, bool toneReservation, bool miso = false);
int m_fftSize;
bool m_extended;
PilotPattern m_pattern;
bool m_toneReservation;
bool m_miso; //!< P2 pilots for MISO
int m_kMax;
int m_kExt; //!< Used for the extended mode offsets, 0 in normal mode
int m_dx;
int m_dy;
std::vector<uint8_t> m_w; //!< Symbol level PRBS for each carrier (9.2.2.1)
// P2 symbols
std::vector<int> m_p2Data;
std::vector<Pilot> m_p2Pilots;
// Normal data symbols, for each value of l mod Dy (l counting from the first P2 symbol)
std::vector<int> m_data[16];
std::vector<Pilot> m_pilots[16];
std::vector<Pilot> m_scattered[16]; //!< Of m_pilots, those on the scattered pilots' grid (Dx Dy carriers apart)
// Frame closing symbol: NFC data cells, the last NFC-CFC of which are unmodulated
std::vector<int> m_fcData;
std::vector<Pilot> m_fcPilots;
int m_cfc;
std::vector<int> m_continual; //!< Continual pilot carriers, for frequency synchronisation
//! Frequency interleaver (8.5): data cell x[p] is carrier data[read[p]]. For symbol l of the frame
const std::vector<int>& p2Deinterleave(int l) const { return m_p2Read[l & 1]; }
const std::vector<int>& dataDeinterleave(int l) const { return m_dataRead[l & 1]; }
const std::vector<int>& fcDeinterleave(int l) const { return m_fcRead[l & 1]; }
private:
Tables(int fftSize, bool extended, PilotPattern pattern, bool toneReservation, bool miso);
static int fftIndex(int fftSize);
//! Table 50: active cells in the frame closing symbol, without tone reservation
static int frameClosingActiveCells(int fftSize, bool extended, PilotPattern pattern);
void interleaver(int ndata, std::vector<int> read[2]) const;
std::vector<int> m_p2Read[2];
std::vector<int> m_dataRead[2];
std::vector<int> m_fcRead[2];
};
//! Frame level PN sequence bit for symbol l (9.2.2.2), 0 past its end
MODEMDVB_API int framePN(int l);
static const int framePNLength = 2624; //!< Table 56: the most symbols a T2-frame can have (10 MHz)
//! P1 tables (9.8)
struct MODEMDVB_API P1Tables
{
static const P1Tables& get();
int m_carriers[384];
uint8_t m_s1[8][64];
uint8_t m_s2[16][256];
uint8_t m_scrambling[384];
//! Modulation of the active carriers for given S1 and S2, before scrambling removed: +/-1
void modulation(int s1, int s2, float *values) const;
private:
P1Tables();
};
} // namespace dvbt2
#endif // MODEMDVB_DVBT2_H

277
modemdvb/dvbt2demap.cpp

@ -0,0 +1,277 @@ @@ -0,0 +1,277 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// Demapping of many cells at once, 4 at a time with SSE2 or NEON (AArch64). The same arithmetic as demapCell()
// and the receiver's outlier limiting, cell by cell, so the same LLRs
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <limits>
#include "dvbt2fec.h"
#include "dvbt2demap.h"
#if defined(__aarch64__) || defined(_M_ARM64)
#include <arm_neon.h>
#define DEMAP_NEON
#elif defined(__SSE2__) || defined(_M_X64) || (defined(_M_IX86_FP) && (_M_IX86_FP >= 2))
#include <emmintrin.h>
#define DEMAP_SSE2
#endif
namespace dvbt2 {
float CellDemapper::normalisation(int eta)
{
static const float values[] = {1.0f, 1.0f, std::sqrt(2.0f), 1.0f, std::sqrt(10.0f), 1.0f, std::sqrt(42.0f), 1.0f, std::sqrt(170.0f)};
return values[eta];
}
#if defined(DEMAP_SSE2)
struct F4
{
typedef __m128 T;
static T load(const float *p) { return _mm_loadu_ps(p); }
static void store(float *p, T a) { _mm_storeu_ps(p, a); }
static T set1(float x) { return _mm_set1_ps(x); }
static T add(T a, T b) { return _mm_add_ps(a, b); }
static T sub(T a, T b) { return _mm_sub_ps(a, b); }
static T mul(T a, T b) { return _mm_mul_ps(a, b); }
static T div(T a, T b) { return _mm_div_ps(a, b); }
static T min(T a, T b) { return _mm_min_ps(a, b); }
static T max(T a, T b) { return _mm_max_ps(a, b); }
static T abs(T a) { return _mm_andnot_ps(_mm_set1_ps(-0.0f), a); }
static T greater(T a, T b) { return _mm_cmpgt_ps(a, b); }
static T select(T mask, T a, T b) { return _mm_or_ps(_mm_and_ps(mask, a), _mm_andnot_ps(mask, b)); }
// Exact for |a| < 2^31: truncate, then one less where that rounded up
static T floor(T a)
{
T t = _mm_cvtepi32_ps(_mm_cvttps_epi32(a));
return _mm_sub_ps(t, _mm_and_ps(_mm_cmpgt_ps(t, a), _mm_set1_ps(1.0f)));
}
// Round to nearest, ties to even (the default rounding mode, as std::nearbyint), clamped to +/-llrLimit
static void toInt8(T a, int8_t out[4])
{
a = _mm_min_ps(_mm_max_ps(a, _mm_set1_ps(-llrLimit)), _mm_set1_ps(llrLimit));
__m128i i = _mm_cvtps_epi32(a);
i = _mm_packs_epi32(i, i);
i = _mm_packs_epi16(i, i);
int32_t bytes = _mm_cvtsi128_si32(i);
std::memcpy(out, &bytes, 4);
}
};
#elif defined(DEMAP_NEON)
struct F4
{
typedef float32x4_t T;
static T load(const float *p) { return vld1q_f32(p); }
static void store(float *p, T a) { vst1q_f32(p, a); }
static T set1(float x) { return vdupq_n_f32(x); }
static T add(T a, T b) { return vaddq_f32(a, b); }
static T sub(T a, T b) { return vsubq_f32(a, b); }
static T mul(T a, T b) { return vmulq_f32(a, b); }
static T div(T a, T b) { return vdivq_f32(a, b); }
static T min(T a, T b) { return vminq_f32(a, b); }
static T max(T a, T b) { return vmaxq_f32(a, b); }
static T abs(T a) { return vabsq_f32(a); }
static T greater(T a, T b) { return vreinterpretq_f32_u32(vcgtq_f32(a, b)); }
static T select(T mask, T a, T b) { return vbslq_f32(vreinterpretq_u32_f32(mask), a, b); }
static T floor(T a) { return vrndmq_f32(a); }
static void toInt8(T a, int8_t out[4])
{
a = vminq_f32(vmaxq_f32(a, vdupq_n_f32(-llrLimit)), vdupq_n_f32(llrLimit));
int32x4_t i = vcvtnq_s32_f32(a);
int16x4_t h = vmovn_s32(i);
int8x8_t b = vmovn_s16(vcombine_s16(h, h));
int32_t bytes = vget_lane_s32(vreinterpret_s32_s8(b), 0);
std::memcpy(out, &bytes, 4);
}
};
#else
// One lane, for other processors
struct F4
{
struct T { float v[4]; };
static T load(const float *p) { T r; for (int i = 0; i < 4; i++) r.v[i] = p[i]; return r; }
static void store(float *p, T a) { for (int i = 0; i < 4; i++) p[i] = a.v[i]; }
static T set1(float x) { T r; for (int i = 0; i < 4; i++) r.v[i] = x; return r; }
static T add(T a, T b) { for (int i = 0; i < 4; i++) a.v[i] += b.v[i]; return a; }
static T sub(T a, T b) { for (int i = 0; i < 4; i++) a.v[i] -= b.v[i]; return a; }
static T mul(T a, T b) { for (int i = 0; i < 4; i++) a.v[i] *= b.v[i]; return a; }
static T div(T a, T b) { for (int i = 0; i < 4; i++) a.v[i] /= b.v[i]; return a; }
static T min(T a, T b) { for (int i = 0; i < 4; i++) a.v[i] = std::min(a.v[i], b.v[i]); return a; }
static T max(T a, T b) { for (int i = 0; i < 4; i++) a.v[i] = std::max(a.v[i], b.v[i]); return a; }
static T abs(T a) { for (int i = 0; i < 4; i++) a.v[i] = std::fabs(a.v[i]); return a; }
static T greater(T a, T b) { T r; for (int i = 0; i < 4; i++) r.v[i] = a.v[i] > b.v[i] ? 1.0f : 0.0f; return r; }
static T select(T mask, T a, T b) { for (int i = 0; i < 4; i++) a.v[i] = mask.v[i] != 0.0f ? a.v[i] : b.v[i]; return a; }
static T floor(T a) { for (int i = 0; i < 4; i++) a.v[i] = std::floor(a.v[i]); return a; }
static void toInt8(T a, int8_t out[4])
{
for (int i = 0; i < 4; i++) {
out[i] = (int8_t) std::max(-llrLimit, std::min(llrLimit, std::nearbyint(a.v[i])));
}
}
};
#endif
template <bool axes>
void CellDemapper::demap(const float *re, const float *im, float *scales, float *scalesQ, int nbCells, int eta, float outlierThreshold,
const int *bitMap, int8_t *llrs, float *errors)
{
const int m = eta / 2;
const float norm = normalisation(eta);
const float norm2 = norm * norm;
const float maxLevel = (float) ((1 << m) - 1);
const F4::T vNorm = F4::set1(norm);
const F4::T vNorm2 = F4::set1(norm2);
const F4::T vMax = F4::set1(maxLevel);
const F4::T vMin = F4::set1(-maxLevel);
const F4::T vHalf = F4::set1(0.5f);
const F4::T vTwo = F4::set1(2.0f);
const F4::T vOne = F4::set1(1.0f);
const F4::T vQuarter = F4::set1(0.25f);
const F4::T vThreshold = F4::set1(outlierThreshold);
const F4::T vInfinity = F4::set1(std::numeric_limits<float>::infinity());
const F4::T vZero = F4::set1(0.0f);
int8_t bytes[4];
int c = 0;
// Whole groups of 4 cells, then the rest as a padded group
for (; c < nbCells; c += 4)
{
const int n = std::min(4, nbCells - c);
float r4[4] = {0.0f, 0.0f, 0.0f, 0.0f}, i4[4] = {0.0f, 0.0f, 0.0f, 0.0f}, s4[4] = {0.0f, 0.0f, 0.0f, 0.0f};
float q4[4] = {0.0f, 0.0f, 0.0f, 0.0f};
std::copy(re + c, re + c + n, r4);
std::copy(im + c, im + c + n, i4);
std::copy(scales + c, scales + c + n, s4);
if (axes) {
std::copy(scalesQ + c, scalesQ + c + n, q4);
}
const F4::T x = F4::mul(F4::load(r4), vNorm); // Unnormalised units
const F4::T y = F4::mul(F4::load(i4), vNorm);
// A scale that isn't finite (NaN, or infinite, from no noise at all) is no information: as the clamps of NaN
// differ between processors, its LLRs would too
F4::T scale = F4::load(s4);
scale = F4::select(F4::greater(vInfinity, F4::abs(scale)), scale, vZero);
F4::T scaleQ = scale;
if (axes)
{
scaleQ = F4::load(q4);
scaleQ = F4::select(F4::greater(vInfinity, F4::abs(scaleQ)), scaleQ, vZero);
}
// Error power to the nearest point, normalised. Clamping before the floor makes no difference to the level,
// and keeps the floor exact however far out the cell is
auto axisError = [&](F4::T v) {
F4::T half = F4::max(vMin, F4::min(vMax, F4::mul(v, vHalf)));
F4::T level = F4::add(F4::mul(vTwo, F4::floor(half)), vOne);
level = F4::max(vMin, F4::min(vMax, level));
return F4::sub(v, level);
};
const F4::T ei = axisError(x), eq = axisError(y);
const F4::T ei2 = F4::mul(ei, ei), eq2 = F4::mul(eq, eq);
const F4::T error = F4::div(F4::add(ei2, eq2), vNorm2);
float e4[4];
F4::store(e4, error);
std::copy(e4, e4 + n, errors + c);
// A cell much further from the nearest point than noise would put it is less reliable than it appears. With
// a scale for each axis, each axis's error is relative to its own noise, and both are reduced alike
if (outlierThreshold > 0.0f)
{
if (axes)
{
const F4::T relative = F4::mul(F4::div(F4::add(F4::mul(ei2, scale), F4::mul(eq2, scaleQ)), vNorm2), vQuarter);
const F4::T over = F4::greater(relative, vThreshold);
const F4::T factor = F4::div(vThreshold, relative);
scale = F4::select(over, F4::mul(scale, factor), scale);
scaleQ = F4::select(over, F4::mul(scaleQ, factor), scaleQ);
F4::store(q4, scaleQ);
std::copy(q4, q4 + n, scalesQ + c);
}
else
{
const F4::T relative = F4::mul(F4::mul(error, scale), vQuarter); // Over the noise per dimension
const F4::T limited = F4::mul(scale, F4::div(vThreshold, relative));
scale = F4::select(F4::greater(relative, vThreshold), limited, scale);
}
F4::store(s4, scale);
std::copy(s4, s4 + n, scales + c);
}
// LLRs of each axis's bits: the sign, then |d - threshold| recursively (table 14's Gray mapping)
const F4::T sI = F4::div(scale, vNorm2);
const F4::T sQ = axes ? F4::div(scaleQ, vNorm2) : sI;
for (int axis = 0; axis < 2; axis++)
{
const F4::T s = axis == 0 ? sI : sQ;
F4::T v = axis == 0 ? x : y;
F4::T d = F4::abs(v);
float threshold = (float) (1 << (m - 1));
F4::toInt8(F4::mul(s, v), bytes);
for (int j = 0; j < n; j++) {
llrs[bitMap[(c + j) * eta + axis]] = bytes[j];
}
for (int k = 1; k < m; k++)
{
const F4::T t = F4::set1(threshold);
F4::toInt8(F4::mul(s, F4::sub(d, t)), bytes);
for (int j = 0; j < n; j++) {
llrs[bitMap[(c + j) * eta + 2 * k + axis]] = bytes[j];
}
d = F4::abs(F4::sub(d, t));
threshold *= 0.5f;
}
}
}
}
void demapCells(const float *re, const float *im, float *scales, int nbCells, int eta, float outlierThreshold,
const int *bitMap, int8_t *llrs, float *errors)
{
CellDemapper::demap<false>(re, im, scales, nullptr, nbCells, eta, outlierThreshold, bitMap, llrs, errors);
}
void demapCellsAxes(const float *re, const float *im, float *scalesI, float *scalesQ, int nbCells, int eta,
float outlierThreshold, const int *bitMap, int8_t *llrs, float *errors)
{
CellDemapper::demap<true>(re, im, scalesI, scalesQ, nbCells, eta, outlierThreshold, bitMap, llrs, errors);
}
} // namespace dvbt2

45
modemdvb/dvbt2demap.h

@ -0,0 +1,45 @@ @@ -0,0 +1,45 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2DEMAP_H
#define MODEMDVB_DVBT2DEMAP_H
#include <cstdint>
#include "dvbt2fec.h"
namespace dvbt2 {
//! The helpers of demapCells() and demapCellsAxes() (declared in dvbt2fec.h)
class CellDemapper
{
private:
static float normalisation(int eta);
//! With axes, each axis has its own scale (scalesQ for the imaginary part's), else scales is for both
template <bool axes>
static void demap(const float *re, const float *im, float *scales, float *scalesQ, int nbCells, int eta, float outlierThreshold,
const int *bitMap, int8_t *llrs, float *errors);
friend void demapCells(const float *re, const float *im, float *scales, int nbCells, int eta, float outlierThreshold,
const int *bitMap, int8_t *llrs, float *errors);
friend void demapCellsAxes(const float *re, const float *im, float *scalesI, float *scalesQ, int nbCells, int eta,
float outlierThreshold, const int *bitMap, int8_t *llrs, float *errors);
};
} // namespace dvbt2
#endif // MODEMDVB_DVBT2DEMAP_H

1711
modemdvb/dvbt2fec.cpp

File diff suppressed because it is too large Load Diff

376
modemdvb/dvbt2fec.h

@ -0,0 +1,376 @@ @@ -0,0 +1,376 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2FEC_H
#define MODEMDVB_DVBT2FEC_H
#include <cstdint>
#include <functional>
#include <memory>
#include <vector>
#include "dvbt2.h"
namespace dvbt2 {
// Defined in dvbt2fec.cpp and dvbt2ldpc.h (internal)
struct LDPCKernel;
struct LDPCGraph;
struct PostLayout;
//! BCH outer code (6.1.1), on bytes packed MSB first
class MODEMDVB_API BCH
{
public:
BCH(FECType type, int t);
int parityBits() const { return m_parityBits; }
//! msg: kbch / 8 bytes. parity: parityBits / 8 bytes (rounded up: the medium frame's 180 bits, left aligned)
void encode(const uint8_t *msg, int msgBytes, uint8_t *parity) const;
//! The same for a message of any number of bits (the medium frame's VL-SNR codes' aren't whole bytes), MSB first
void encodeBits(const uint8_t *msg, int msgBits, uint8_t *parity) const;
//! codeword: (kbch + parityBits) / 8 bytes, corrected in place. Returns the number of bits corrected, -1 if it can't
int decode(uint8_t *codeword, int codewordBytes) const;
private:
void remainder(const uint8_t *data, int nbBytes, uint8_t *rem) const;
int mul(int a, int b) const { return (a && b) ? m_exp[m_log[a] + m_log[b]] : 0; }
static inline uint64_t loadBigEndian64(const uint8_t *p);
static constexpr int m_remWords = 3; //!< The remainder's 64-bit words: up to 192 parity bits (t = 12 of 16 bits)
static constexpr int m_maxT = 12;
//! Generator polynomial factors of each frame size (table 7), as the powers of x with non-zero coefficients
static const std::vector<std::vector<int>> m_bchNormalPolys;
static const std::vector<std::vector<int>> m_bchMediumPolys;
static const std::vector<std::vector<int>> m_bchShortPolys;
int m_m; //!< GF(2^m)
int m_t;
int m_parityBits;
int m_parityBytes;
//! For division 8 bytes at a time: [byte k of 8][value][word], the remainder of the value followed by 7 - k zero
//! bytes, left aligned in the words (the coefficient of x^(P-1) the MSB of the first)
std::vector<uint64_t> m_slices;
std::vector<int> m_exp; //!< Doubled, so the sum of two logs can be looked up
std::vector<int> m_log;
};
//! Addresses of parity bit accumulators (annexes A and B). rows: for each group of 360 information bits, the
//! number of addresses, then the addresses for the group's first bit
struct LDPCTable
{
FECType m_type;
CodeRate m_rate;
int m_n;
int m_k;
const uint16_t *m_rows;
};
//! Null for a combination that doesn't exist in the family
MODEMDVB_API const LDPCTable *ldpcTable(FECType type, CodeRate rate, CodeFamily family = CodeFamily::T2);
//! The S2 codes not shared with T2 (dvbs2ldpctables.cpp), or null
MODEMDVB_API const LDPCTable *ldpcTableS2(FECType type, CodeRate rate);
//! The S2X codes (dvbs2xldpctables.cpp), or null
MODEMDVB_API const LDPCTable *ldpcTableS2X(FECType type, CodeRate rate);
//! LDPC inner code (6.1.2). LLRs are int8, positive for 0, twice the actual LLR. Decoded with layered min-sum
//! (dvbt2ldpckernel.h): AVX2, SSE4.1 or NEON if the CPU has it, else portable C++
class MODEMDVB_API LDPC
{
public:
//! A rate the family hasn't for the frame size is given its rate 1/2 (std::invalid_argument thrown if it has none)
LDPC(FECType type, CodeRate rate, CodeFamily family = CodeFamily::T2);
~LDPC();
int n() const { return m_n; }
int k() const { return m_k; }
//! Blocks decoded in parallel
int lanes() const;
//! About the memory its decoder holds: the code's graph, about 1 MB for a normal frame's, shared with the other
//! decoders of the code. The messages (a vector of lanes() for each of the graph's edges and variables, a normal
//! frame's about 10 MB with AVX2) are each decoding thread's, for whatever code it decodes
size_t memoryBytes() const;
//! bits: k info bits, one per byte. parity: n - k bits
void encode(const uint8_t *bits, uint8_t *parity) const;
//! Decode up to lanes() blocks of n LLRs in place, with the calling thread's buffers (so threads can decode with
//! the same LDPC at once). Returns the iterations used (maxIterations + 1 if not converged).
//! unconverged, if not null: the number of blocks that didn't converge. quickBlocks: a bit for each block not worth
//! more than a few iterations (probably damaged), given up then, so it doesn't hold up the others
int decode(int8_t *const *blocks, int nbBlocks, int maxIterations = 25, int *unconverged = nullptr,
uint64_t quickBlocks = 0);
private:
static const LDPCKernel& chooseLDPCKernel();
template <class F>
static void forEachInfoBit(const LDPCTable& table, F f);
static void buildGraph(const LDPCTable& table, LDPCGraph& graph);
static std::shared_ptr<const LDPCGraph> sharedGraph(const LDPCTable& table);
static size_t align64(size_t bytes);
struct Private;
std::unique_ptr<Private> m_private;
const LDPCTable *m_table;
int m_n;
int m_k;
};
//! Bit interleaver (6.1.3) and demultiplexer (6.2.1): codeword bit index for each bit of each cell
//! (cell q, bit e at q * eta + e). Also used for L1-post with no parity interleaving or twist
MODEMDVB_API void cellBitMap(FECType type, CodeRate rate, Constellation constellation, std::vector<int>& map);
//! Constellation mapping (6.2.2) of eta bits, one per byte, to a normalised cell
MODEMDVB_API Cf mapCell(const uint8_t *bits, int eta);
// The channel LLRs (in nats) given to LDPC are limited to this. Its messages are limited to 31 and the variables'
// sums of them to 127, so a variable in 3 checks can still overturn its channel LLR, and its sum doesn't saturate. At
// 127, the cells of the heavy tail of real reception's errors (phase noise, interference) came in confidently wrong,
// beyond what 2 or 3 checks could overturn, and saturated the sums: at 25 to 30 dB, LDPC was at its iteration limit,
// with 10 to 20 % of blocks not converging (B210, HackRF), left for BCH. At 32, a B210's and an RSPduo's recordings
// converge in 2 to 3 iterations, every block, and the thresholds in AWGN (QPSK to 256-QAM) are unchanged
static constexpr float llrLimit = 32.0f;
//! Max-log soft demapping of a normalised cell to eta int8 LLRs (positive for 0, as LDPC wants).
//! llrScale: 2 * 2 / noise variance per dimension of the normalised cell, times its reliability
MODEMDVB_API void demapCell(Cf cell, int eta, float llrScale, int8_t *llrs);
//! Separate demapping of the two axes, for rotated constellations, where they have different reliabilities
MODEMDVB_API void demapCellAxes(Cf cell, int eta, float scaleI, float scaleQ, int8_t *llrs);
//! Demapping of many normalised cells (eta 2 to 8), as demapCell(), with SIMD: llrs[bitMap[cell * eta + bit]].
//! errors: each cell's error power to the nearest constellation point. If outlierThreshold > 0, scales of cells
//! with error power times scale / 4 (relative to the noise per dimension) over it are reduced in proportion
MODEMDVB_API void demapCells(const float *re, const float *im, float *scales, int nbCells, int eta, float outlierThreshold,
const int *bitMap, int8_t *llrs, float *errors);
//! The same with a scale for each axis, as demapCellAxes(). An outlier's error power is relative to each axis's noise,
//! and both its scales are reduced alike
MODEMDVB_API void demapCellsAxes(const float *re, const float *im, float *scalesI, float *scalesQ, int nbCells, int eta,
float outlierThreshold, const int *bitMap, int8_t *llrs, float *errors);
//! Constellation rotation angle (table 16), radians
MODEMDVB_API float rotationAngle(Constellation constellation);
//! For cellBitMap(), mapCell(), demapCell() and demapCellAxes(), and L1Codec: the demultiplexer's tables, and the
//! constellations' levels and LLRs
class CellMapper
{
private:
friend MODEMDVB_API void cellBitMap(FECType type, CodeRate rate, Constellation constellation, std::vector<int>& map);
friend MODEMDVB_API Cf mapCell(const uint8_t *bits, int eta);
friend MODEMDVB_API void demapCell(Cf cell, int eta, float llrScale, int8_t *llrs);
friend MODEMDVB_API void demapCellAxes(Cf cell, int eta, float scaleI, float scaleQ, int8_t *llrs);
friend class L1Codec;
static const int *demuxTable(CodeRate rate, Constellation constellation, FECType type);
static float normalisation(int eta);
static int axisLevel(const uint8_t *bits, int stride, int m);
static inline int8_t quantise(float value);
static inline void demapAxis(float value, int m, float scale, int8_t *llrs, int stride);
};
//! Cell interleaver (6.4): d[L(r, q)] = g[q]
class MODEMDVB_API CellInterleaver
{
public:
explicit CellInterleaver(int ncells);
int address(int r, int q) const { return (m_l0[q] + shift(r)) % m_ncells; }
//! Block r's shift. Doesn't change anything (those of a TI-block's blocks are worked out when it's made), so
//! threads can ask at once
int shift(int r) const;
//! The addresses of block 0: address(r, q) is (l0()[q] + shift(r)) mod ncells, each term less than ncells
const int *l0() const { return m_l0.data(); }
private:
int nextShift(int& k) const;
static constexpr int m_maxBlocks = 1024; //!< A TI-block's FEC blocks: PLP_NUM_BLOCKS_MAX's 10 bits
int m_ncells;
int m_nd;
std::vector<int> m_l0;
std::vector<int> m_shifts;
int m_nextK; //!< The bit reversed counter after the last of m_shifts
};
//! Time interleaver (6.5.3): the order the cells of a TI-block are transmitted in. read[i] is the index into the
//! cells of the FEC blocks (r * ncells + q) of transmitted cell i
MODEMDVB_API void timeInterleaver(int ncells, int nfec, std::vector<int>& read);
//! L1 signalling FEC (7.3): shortened and punctured 16K LDPC with BCH, and modulation
class MODEMDVB_API L1Codec
{
public:
L1Codec();
~L1Codec();
//! 200 L1-pre bits to 1840 BPSK cells
void encodePre(const uint8_t *bits, std::vector<Cf>& cells);
//! Decode 1840 cells, with per-cell LLR scales. Returns false if the FEC or CRC fails
bool decodePre(const Cf *cells, const float *llrScales, L1Pre& pre);
//! L1-post bits (including the CRC), encoded and modulated as signalled in pre. Sets L1_POST_SIZE
void encodePost(const std::vector<uint8_t>& bits, L1Pre& pre, std::vector<Cf>& cells);
//! Decode the L1_POST_SIZE cells signalled in pre
bool decodePost(const Cf *cells, const float *llrScales, const L1Pre& pre, L1Post& post);
//! The number of L1-post cells (L1_POST_SIZE) for this many information bits (L1_POST_INFO_SIZE)
static int postCells(int infoBits, L1Constellation constellation, int fftSize);
private:
struct Block
{
int m_ksig;
int m_npunc;
std::vector<int> m_info; //!< LDPC codeword positions of the info bits
std::vector<int> m_transmitted; //!< LDPC codeword positions of the transmitted bits, in order
};
static void layout(int ksig, int kbchL1, int kldpcL1, int nparity, int npunc, const int *groupOrder,
const int *parityOrder, Block& block);
bool decodeBlock(const Block& block, LDPC& ldpc, const BCH& bch, int kbchL1, const int8_t *llrs, std::vector<uint8_t>& bits);
static int postTableIndex(L1Constellation constellation);
static void packBits(const uint8_t *bits, int nbBits, uint8_t *bytes);
static void encodeL1Block(const uint8_t *info, int ksig, const std::vector<int>& infoPositions, int kbchL1, int kldpcL1,
const BCH& bch, const LDPC& ldpc, std::vector<uint8_t>& codeword);
static PostLayout postLayout(int infoBits, L1Constellation constellation, int fftSize);
static void postCellBitMap(L1Constellation constellation, int npost, std::vector<int>& map);
// L1 signalling (7.3)
static constexpr int m_preKbch = 3072;
static constexpr int m_preKldpc = 3240;
static constexpr int m_preKsig = 200;
static constexpr int m_preNpunc = 11488;
static constexpr int m_postKbch = 7032;
static constexpr int m_postKldpc = 7200;
static constexpr int m_l1BchParity = 168;
static const int m_preGroups[9];
static const int m_preParity[36];
static const int m_postGroups[3][20];
static const int m_postParity[3][25];
LDPC m_preLDPC;
LDPC m_postLDPC;
BCH m_bch;
Block m_preBlock; //!< L1-pre's layout, always the same
// L1-post's layout and bit map, for the parameters they were last made for
Block m_postBlock;
std::vector<int> m_postMap;
int m_postKey[4] = {-1, -1, -1, -1}; //!< KSIG, NPUNC, NPOST, L1 constellation
};
//! BBFRAMEs (5.1, 5.2) from a transport stream: normal or high efficiency mode, optionally with null packet deletion and
//! (normal mode) ISSY bytes. Also DVB-S2's (EN 302 307 5.1.6), the same BBHEADER, with the roll-off and input stream in
//! MATYPE
class MODEMDVB_API BBFrameEncoder
{
public:
BBFrameEncoder(FECType type, CodeRate rate, bool highEfficiency = false, CodeFamily family = CodeFamily::T2);
int kbch() const { return m_kbch; }
//! MATYPE: by default 0xf0 (TS, single input stream, CCM, no ISSY or null packet deletion, roll-off 0.35) and 0
void setMatype(uint8_t matype1, uint8_t matype2) { m_matype1 = matype1; m_matype2 = matype2; }
//! Null packet deletion (5.1.5), and ISSY bytes after each packet (0, 2 or 3; normal mode only; a count, not a real
//! timestamp), signalled in MATYPE. Before the first packet
void setOptions(bool nullPacketDeletion, int issyBytes);
//! Add a TS packet. Returns true when a BBFRAME (kbch / 8 bytes, scrambled) is ready
bool addPacket(const uint8_t *packet, std::vector<uint8_t>& frame);
private:
void start();
int m_kbch;
bool m_hem;
uint8_t m_matype1 = 0xf0;
uint8_t m_matype2 = 0;
std::vector<uint8_t> m_data; //!< Data field
int m_syncd;
uint8_t m_crc; //!< CRC-8 of the previous packet
bool m_npd = false;
int m_issyBytes = 0;
int m_dnp = 0; //!< Null packets deleted since the last packet sent
uint32_t m_issyCount = 0;
};
//! Transport stream from BBFRAMEs (5.1): normal and high efficiency modes, null packet deletion, ISSY
class MODEMDVB_API BBFrameDecoder
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback;
//! A BBHEADER's fields (5.1.6), from a descrambled BBFRAME
struct Header
{
uint8_t m_matype1 = 0;
uint8_t m_matype2 = 0; //!< The input stream identifier, in a multiple input stream
int m_upl = 0;
int m_dfl = 0;
uint8_t m_sync = 0;
int m_syncd = 0;
int m_mode = 0; //!< CRC-8 xor MODE: 0 normal, 1 high efficiency (T2), else the CRC failed
bool transportStream() const { return (m_matype1 >> 6) == 3; }
bool singleStream() const { return (m_matype1 & 0x20) != 0; }
int rollOff() const { return m_matype1 & 3; } //!< 0 0.35, 1 0.25, 2 0.20 (S2)
};
//! The header of a descrambled BBFRAME. Returns false if its CRC fails (mode neither 0 nor 1)
static bool parseHeader(const uint8_t *frame, Header& header);
struct Stats
{
uint64_t m_frames = 0;
uint64_t m_headerErrors = 0; //!< BBHEADER CRC failures
uint64_t m_packets = 0;
uint64_t m_nullPackets = 0; //!< Reinserted
uint64_t m_crcErrors = 0; //!< Packet CRC-8 failures, in normal mode (those packets are flagged)
uint64_t m_genericFrames = 0; //!< Of a generic stream (headers good): not decoded
int m_streamType = -1; //!< MATYPE-1's TS/GS field of the last good header, -1 none yet
};
//! MATYPE-1's TS/GS field: 0 generic packetized, 1 generic continuous (in DVB-S2, as GSE is carried), 2 GSE (T2,
//! and S2X's GSE-HEM), 3 transport stream
static const char *streamTypeName(int type);
void setPacketCallback(PacketCallback callback) { m_callback = callback; }
//! DVB-S2 has no high efficiency mode: a CRC-8 that's 1 out is an error
void setHighEfficiencyAllowed(bool allowed) { m_hemAllowed = allowed; }
//! A descrambled BBFRAME of kbch / 8 bytes. bad: BCH failed, so the packets are flagged as errored
void process(const uint8_t *frame, int kbch, bool bad);
//! BBFRAMEs were lost before the next: the packet being assembled can't be completed from it
void discontinuity();
void reset();
//! The end of the stream: the packet held for the next's CRC-8 is output as it is
void flush();
const Stats& stats() const { return m_stats; }
private:
void outputPacket(bool bad);
void releaseHeld();
PacketCallback m_callback;
bool m_hemAllowed = true;
std::vector<uint8_t> m_packet; //!< Current user packet being assembled
int m_packetBits = 0; //!< Bits of it received
bool m_synced = false;
bool m_packetBad = false;
int m_upl = 0;
bool m_hem = false;
bool m_npd = false;
int m_issyBytes = 0;
uint8_t m_lastCrc = 0;
bool m_lastCrcValid = false; //!< m_lastCrc is of the packet before the one being assembled
bool m_firstByteBad = false; //!< The packet being assembled began in a BBFRAME that BCH didn't correct
//! In normal mode, the last packet, until the next's CRC-8 of it says whether it's errored
uint8_t m_held[188];
bool m_heldValid = false;
Stats m_stats;
};
} // namespace dvbt2
#endif // MODEMDVB_DVBT2FEC_H

70
modemdvb/dvbt2ldpc.h

@ -0,0 +1,70 @@ @@ -0,0 +1,70 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2LDPC_H
#define MODEMDVB_DVBT2LDPC_H
#include <cstdint>
// Internal to modemdvb: vectorised LDPC decoders, each lane of a vector decoding a different codeword. Included by
// files compiled for instruction sets the CPU mightn't have, so only raw types here, no library code (see
// dvbt2ldpckernel.h)
namespace dvbt2 {
//! A code's graph and the buffers to decode it with, made by LDPC (dvbt2fec.cpp). The parity check matrix: the
//! variables (codeword bits) of each check, information bits first, then parity. Vectors are of the kernel's lanes
//! bytes, aligned to 64
struct LDPCKernelData
{
int m_n; //!< Variables
int m_nbChecks;
const int *m_checkStart; //!< Index into m_vars of each check's variables, and one past the last
const int *m_vars;
const int8_t *m_filler; //!< m_n of 127: a codeword of zeros, for lanes without a block
void *m_app; //!< m_n vectors: each variable's LLR
void *m_c2v; //!< A vector for each of m_vars: each edge's message from its check. Needn't be set
void *m_t; //!< The largest check degree's vectors, for the check being updated
void *m_prefix; //!< The same
};
//! A decoder for an instruction set
struct LDPCKernel
{
int m_lanes; //!< Codewords decoded at once, and the bytes of a vector
//! LLRs are int8, positive for 0, decoded in place. Returns the iterations used, or maxIterations + 1 if not all
//! the codewords satisfy the parity checks. Stops early if those that don't seem stuck, with a few errors.
//! unconverged, if not null: the number of codewords that don't. quickBlocks: a bit for each block not worth more
//! than a few iterations (probably damaged), given up then
int (*m_decode)(const LDPCKernelData& data, int8_t *const *blocks, int nbBlocks, int maxIterations,
int *unconverged, uint64_t quickBlocks);
};
extern const LDPCKernel ldpcKernelGeneric;
#if defined(DVBT2_LDPC_X86)
extern const LDPCKernel ldpcKernelSSE41;
extern const LDPCKernel ldpcKernelAVX2;
#elif defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(_M_ARM64)
#define DVBT2_LDPC_NEON
extern const LDPCKernel ldpcKernelNEON;
#endif
} // namespace dvbt2
#endif // MODEMDVB_DVBT2LDPC_H

54
modemdvb/dvbt2ldpc_avx2.cpp

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// LDPC decoder for 32 codewords at a time, with AVX2. Compiled with AVX2 enabled, so only used if the CPU has it
#include <immintrin.h>
#include "dvbt2ldpckernel.h"
namespace dvbt2 {
namespace {
struct VectorAVX2
{
typedef __m256i T;
static const int W = 32;
static T zero() { return _mm256_setzero_si256(); }
static T set1(char x) { return _mm256_set1_epi8(x); }
static T adds(T a, T b) { return _mm256_adds_epi8(a, b); }
static T subs(T a, T b) { return _mm256_subs_epi8(a, b); }
static T addsu(T a, T b) { return _mm256_adds_epu8(a, b); }
static T abs(T a) { return _mm256_abs_epi8(a); }
static T min(T a, T b) { return _mm256_min_epi8(a, b); }
static T max(T a, T b) { return _mm256_max_epi8(a, b); }
static T cmpgt(T a, T b) { return _mm256_cmpgt_epi8(a, b); }
static T sign(T a, T b) { return _mm256_sign_epi8(a, b); }
static T xor_(T a, T b) { return _mm256_xor_si256(a, b); }
static T and_(T a, T b) { return _mm256_and_si256(a, b); }
static T or_(T a, T b) { return _mm256_or_si256(a, b); }
static void store(uint8_t *p, T a) { _mm256_storeu_si256(reinterpret_cast<T *>(p), a); }
static uint64_t movemask(T a) { return (uint32_t) _mm256_movemask_epi8(a); }
};
} // namespace
const LDPCKernel ldpcKernelAVX2 = {VectorAVX2::W, decodeLDPC<VectorAVX2>};
} // namespace dvbt2

85
modemdvb/dvbt2ldpc_generic.cpp

@ -0,0 +1,85 @@ @@ -0,0 +1,85 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// LDPC decoder for 16 codewords at a time, in portable C++, for CPUs without SSE4.1 or NEON. The same arithmetic
// as the SIMD versions (saturating int8), so it gives the same results, but is several times slower. Compilers may
// vectorise the loops
#include <algorithm>
#include "dvbt2ldpc.h"
// Load and store without SSE2's transposes, as this may be built for any CPU
#undef DVBT2_LDPC_X86
#include "dvbt2ldpckernel.h"
namespace dvbt2 {
namespace {
// Plain loops over the lanes, which compilers can vectorise
#define DVBT2_LANES(expr) T r; for (int i = 0; i < W; i++) { r.v[i] = (int8_t) (expr); } return r;
struct VectorGeneric
{
static const int W = 16;
struct T { int8_t v[W]; };
static int clamp(int x) { return x > 127 ? 127 : x < -128 ? -128 : x; }
static T set1(char x) { DVBT2_LANES(x) }
static T zero() { DVBT2_LANES(0) }
static T adds(const T& a, const T& b) { DVBT2_LANES(clamp(a.v[i] + b.v[i])) }
static T subs(const T& a, const T& b) { DVBT2_LANES(clamp(a.v[i] - b.v[i])) }
static T addsu(const T& a, const T& b) { DVBT2_LANES(std::min((int) (uint8_t) a.v[i] + (uint8_t) b.v[i], 255)) }
// As _mm_abs_epi8: -128 stays -128
static T abs(const T& a) { DVBT2_LANES(a.v[i] < 0 ? -a.v[i] : a.v[i]) }
static T min(const T& a, const T& b) { DVBT2_LANES(a.v[i] < b.v[i] ? a.v[i] : b.v[i]) }
static T max(const T& a, const T& b) { DVBT2_LANES(a.v[i] > b.v[i] ? a.v[i] : b.v[i]) }
static T cmpgt(const T& a, const T& b) { DVBT2_LANES(a.v[i] > b.v[i] ? -1 : 0) }
// As _mm_sign_epi8: a, negated where b is negative, zero where b is zero
static T sign(const T& a, const T& b) { DVBT2_LANES(b.v[i] < 0 ? -a.v[i] : b.v[i] == 0 ? 0 : a.v[i]) }
static T xor_(const T& a, const T& b) { DVBT2_LANES(a.v[i] ^ b.v[i]) }
static T and_(const T& a, const T& b) { DVBT2_LANES(a.v[i] & b.v[i]) }
static T or_(const T& a, const T& b) { DVBT2_LANES(a.v[i] | b.v[i]) }
static void store(uint8_t *p, const T& a)
{
for (int i = 0; i < W; i++) {
p[i] = (uint8_t) a.v[i];
}
}
static uint64_t movemask(const T& a)
{
uint64_t mask = 0;
for (int i = 0; i < W; i++) {
mask |= (uint64_t) (a.v[i] < 0) << i;
}
return mask;
}
};
#undef DVBT2_LANES
} // namespace
const LDPCKernel ldpcKernelGeneric = {VectorGeneric::W, decodeLDPC<VectorGeneric>};
} // namespace dvbt2

83
modemdvb/dvbt2ldpc_neon.cpp

@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// LDPC decoder for 16 codewords at a time, with ARM NEON (always present on AArch64; on 32-bit ARM, when the
// compiler targets it)
#include "dvbt2ldpc.h"
#if defined(DVBT2_LDPC_NEON)
#include <arm_neon.h>
#include "dvbt2ldpckernel.h"
namespace dvbt2 {
namespace {
struct VectorNEON
{
typedef int8x16_t T;
static const int W = 16;
static T zero() { return vdupq_n_s8(0); }
static T set1(char x) { return vdupq_n_s8((int8_t) x); }
static T adds(T a, T b) { return vqaddq_s8(a, b); }
static T subs(T a, T b) { return vqsubq_s8(a, b); }
static T addsu(T a, T b) { return vreinterpretq_s8_u8(vqaddq_u8(vreinterpretq_u8_s8(a), vreinterpretq_u8_s8(b))); }
static T abs(T a) { return vabsq_s8(a); }
static T min(T a, T b) { return vminq_s8(a, b); }
static T max(T a, T b) { return vmaxq_s8(a, b); }
static T cmpgt(T a, T b) { return vreinterpretq_s8_u8(vcgtq_s8(a, b)); }
// As SSSE3's _mm_sign_epi8: a, negated where b is negative, zero where b is zero
static T sign(T a, T b)
{
const uint8x16_t negative = vcltq_s8(b, vdupq_n_s8(0));
const uint8x16_t isZero = vceqq_s8(b, vdupq_n_s8(0));
const T r = vbslq_s8(negative, vnegq_s8(a), a);
return vbicq_s8(r, vreinterpretq_s8_u8(isZero));
}
static T xor_(T a, T b) { return veorq_s8(a, b); }
static T and_(T a, T b) { return vandq_s8(a, b); }
static T or_(T a, T b) { return vorrq_s8(a, b); }
static void store(uint8_t *p, T a) { vst1q_u8(p, vreinterpretq_u8_s8(a)); }
// The sign bit of each lane, as SSE2's _mm_movemask_epi8. NEON has no equivalent, but this is only used now and
// then, to see if decoding has finished
static uint64_t movemask(T a)
{
uint8_t bits[16];
vst1q_u8(bits, vshrq_n_u8(vreinterpretq_u8_s8(a), 7));
uint64_t mask = 0;
for (int i = 0; i < 16; i++) {
mask |= (uint64_t) bits[i] << i;
}
return mask;
}
};
} // namespace
const LDPCKernel ldpcKernelNEON = {VectorNEON::W, decodeLDPC<VectorNEON>};
} // namespace dvbt2
#endif // DVBT2_LDPC_NEON

54
modemdvb/dvbt2ldpc_sse41.cpp

@ -0,0 +1,54 @@ @@ -0,0 +1,54 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// LDPC decoder for 16 codewords at a time, with SSE4.1. Compiled with SSE4.1 enabled
#include <smmintrin.h>
#include "dvbt2ldpckernel.h"
namespace dvbt2 {
namespace {
struct VectorSSE41
{
typedef __m128i T;
static const int W = 16;
static T zero() { return _mm_setzero_si128(); }
static T set1(char x) { return _mm_set1_epi8(x); }
static T adds(T a, T b) { return _mm_adds_epi8(a, b); }
static T subs(T a, T b) { return _mm_subs_epi8(a, b); }
static T addsu(T a, T b) { return _mm_adds_epu8(a, b); }
static T abs(T a) { return _mm_abs_epi8(a); }
static T min(T a, T b) { return _mm_min_epi8(a, b); }
static T max(T a, T b) { return _mm_max_epi8(a, b); }
static T cmpgt(T a, T b) { return _mm_cmpgt_epi8(a, b); }
static T sign(T a, T b) { return _mm_sign_epi8(a, b); }
static T xor_(T a, T b) { return _mm_xor_si128(a, b); }
static T and_(T a, T b) { return _mm_and_si128(a, b); }
static T or_(T a, T b) { return _mm_or_si128(a, b); }
static void store(uint8_t *p, T a) { _mm_storeu_si128(reinterpret_cast<T *>(p), a); }
static uint64_t movemask(T a) { return (uint32_t) _mm_movemask_epi8(a); }
};
} // namespace
const LDPCKernel ldpcKernelSSE41 = {VectorSSE41::W, decodeLDPC<VectorSSE41>};
} // namespace dvbt2

483
modemdvb/dvbt2ldpckernel.h

@ -0,0 +1,483 @@ @@ -0,0 +1,483 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2LDPCKERNEL_H
#define MODEMDVB_DVBT2LDPCKERNEL_H
// The LDPC decoder, for a vector type V (see dvbt2ldpc_sse41.cpp, dvbt2ldpc_avx2.cpp, dvbt2ldpc_neon.cpp and
// dvbt2ldpc_generic.cpp, compiled for each instruction set). Included only by those files. As some are compiled for
// instruction sets the CPU mightn't have, only raw buffers here, made by LDPC (dvbt2fec.cpp): library code they
// instantiated (std::vector's, std::fill...) would be compiled for that instruction set too, and the linker could keep
// that copy for every caller. And all in an unnamed namespace, so each file's is its own
#include <cstddef>
#include <cstdint>
#include "dvbt2ldpc.h"
#if defined(DVBT2_LDPC_X86)
#include <emmintrin.h>
#endif
namespace dvbt2 {
namespace {
inline int minInt(int a, int b) { return a < b ? a : b; }
inline int maxInt(int a, int b) { return a > b ? a : b; }
#if defined(DVBT2_LDPC_X86)
// 16x16 byte transpose: r[i] holds row i, and on return column i
static inline void transpose16x16(__m128i r[16])
{
__m128i b[16], c[16], d[16];
for (int i = 0; i < 8; i++)
{
b[2 * i] = _mm_unpacklo_epi8(r[2 * i], r[2 * i + 1]);
b[2 * i + 1] = _mm_unpackhi_epi8(r[2 * i], r[2 * i + 1]);
}
for (int i = 0; i < 4; i++)
{
c[4 * i] = _mm_unpacklo_epi16(b[4 * i], b[4 * i + 2]);
c[4 * i + 1] = _mm_unpackhi_epi16(b[4 * i], b[4 * i + 2]);
c[4 * i + 2] = _mm_unpacklo_epi16(b[4 * i + 1], b[4 * i + 3]);
c[4 * i + 3] = _mm_unpackhi_epi16(b[4 * i + 1], b[4 * i + 3]);
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 4; j++)
{
d[8 * i + 2 * j] = _mm_unpacklo_epi32(c[8 * i + j], c[8 * i + j + 4]);
d[8 * i + 2 * j + 1] = _mm_unpackhi_epi32(c[8 * i + j], c[8 * i + j + 4]);
}
}
for (int j = 0; j < 8; j++)
{
r[2 * j] = _mm_unpacklo_epi64(d[j], d[j + 8]);
r[2 * j + 1] = _mm_unpackhi_epi64(d[j], d[j + 8]);
}
}
#endif
// Layered decoding: each check's messages to its variables are stored, so the variables' LLRs can be updated in place,
// check by check. The vectors hold the same bit of W codewords. The check update is the sum-product's, with pairwise
// minimums corrected towards the exact box-plus, as ldpctool's MinSumC, which offset min-sum is 0.2 dB worse than
template <class V>
class LDPCKernelImpl
{
public:
typedef typename V::T T;
explicit LDPCKernelImpl(const LDPCKernelData& data) :
m_n(data.m_n),
m_nbChecks(data.m_nbChecks),
m_start(data.m_checkStart),
m_vars(data.m_vars),
m_filler(data.m_filler),
m_app(static_cast<T *>(data.m_app)),
m_c2v(static_cast<T *>(data.m_c2v)),
m_t(static_cast<T *>(data.m_t)),
m_prefix(static_cast<T *>(data.m_prefix))
{
}
int decode(int8_t *const *blocks, int nbBlocks, int maxIterations, int *unconverged, uint64_t quickBlocks)
{
const int nbChecks = m_nbChecks;
const int *start = m_start;
const int *vars = m_vars;
const uint64_t laneMask = nbBlocks >= 64 ? ~0ULL : ((1ULL << nbBlocks) - 1);
const uint64_t quick = quickBlocks & laneMask;
const uint64_t others = laneMask & ~quick;
load(blocks, nbBlocks);
T *app = m_app;
T *c2v = m_c2v;
int iteration = 0;
bool stuck = false;
uint8_t counts[V::W], previous[V::W];
int stable[V::W];
for (int l = 0; l < V::W; l++)
{
previous[l] = 255;
stable[l] = 0;
}
// The iterations the blocks not quick took (converged or stuck), -1 till they have: the quick ones are given a
// few times as many (at threshold, many, as they can need)
int othersDone = ((quick != 0) && (others == 0)) ? 0 : -1;
for (; iteration < maxIterations; iteration++)
{
// A codeword with a few unsatisfied checks, the same number for a few iterations, is usually stuck
// there, with errors BCH can correct, so continuing would only delay the others of the batch. Most
// converge within a few iterations, so that's when to start checking
if (iteration >= stuckStart)
{
unsatisfied(counts);
bool converged = true;
bool othersFinished = true;
for (int l = 0; l < minInt(nbBlocks, V::W); l++)
{
if (counts[l] == 0) {
continue;
}
converged = false;
stable[l] = (counts[l] <= stuckChecks) && (counts[l] == previous[l]) ? stable[l] + 1 : 0;
previous[l] = counts[l];
othersFinished = othersFinished && ((((quick >> l) & 1) != 0) || (stable[l] >= stuckIterations));
}
if (othersFinished && (othersDone < 0)) {
othersDone = iteration;
}
const bool giveUp = (othersDone >= 0) && (iteration >= maxInt(quickIterations, quickFactor * othersDone));
stuck = !converged;
for (int l = 0; l < minInt(nbBlocks, V::W); l++)
{
if (counts[l] != 0) {
stuck = stuck && ((giveUp && (((quick >> l) & 1) != 0)) || (stable[l] >= stuckIterations));
}
}
if (converged || stuck) {
break;
}
}
else if (satisfied(laneMask))
{
break;
}
else if ((othersDone < 0) && (quick != 0) && satisfied(others))
{
othersDone = iteration;
}
// The first iteration's messages from the checks are all 0, so aren't cleared first: each check's are
// only read by its own update, before it writes them
if (iteration == 0)
{
for (int c = 0; c < nbChecks; c++) {
updateCheck<true>(app, c2v + start[c], vars + start[c], start[c + 1] - start[c]);
}
}
else
{
for (int c = 0; c < nbChecks; c++) {
updateCheck<false>(app, c2v + start[c], vars + start[c], start[c + 1] - start[c]);
}
}
}
// Codewords not satisfying the parity checks: those counted when found stuck, or at the limit, counted now
int failed = 0;
if (!stuck && (iteration == maxIterations)) {
unsatisfied(counts);
}
if (stuck || (iteration == maxIterations))
{
for (int l = 0; l < minInt(nbBlocks, V::W); l++) {
failed += counts[l] != 0;
}
}
if (failed > 0) {
iteration = maxIterations + 1;
}
if (unconverged) {
*unconverged = failed;
}
store(blocks, nbBlocks);
return iteration;
}
private:
// Variable to check messages: the variables' LLRs without this check's last messages (none in the first iteration:
// the same as subtracting 0)
template <bool first>
inline void extrinsic(const T *app, const T *c2v, const int *vars, int degree, T *t) const
{
const T floor = V::set1(-127);
for (int j = 0; j < degree; j++)
{
if constexpr (first) {
(void) c2v;
t[j] = V::max(app[vars[j]], floor);
} else {
t[j] = V::max(V::subs(app[vars[j]], c2v[j]), floor);
}
}
}
// a box-plus b, approximately: the smaller magnitude, with the sign of the product, corrected by
// log(1 + exp(-|a + b|)) - log(1 + exp(-|a - b|)) rounded to a unit
static inline T boxplus(T a, T b)
{
const T floor = V::set1(-127);
const T four = V::set1(4);
const T one = V::set1(1);
const T minusOne = V::set1(-1);
T m = V::min(V::abs(a), V::abs(b));
T x = V::sign(V::sign(m, a), b);
T apb = V::abs(V::max(V::adds(a, b), floor));
T amb = V::abs(V::max(V::subs(a, b), floor));
T pc = V::and_(V::and_(V::cmpgt(four, apb), V::cmpgt(amb, V::adds(apb, apb))), one);
T nc = V::and_(V::and_(V::cmpgt(four, amb), V::cmpgt(apb, V::adds(amb, amb))), minusOne);
return V::adds(x, V::or_(pc, nc));
}
// Each variable's message is the box-plus of the others', from a prefix and a suffix. The messages are limited,
// so the sums of them in the variables rarely saturate: without that, many codewords diverge
template <bool first>
inline void updateCheck(T *app, T *c2v, const int *vars, int degree)
{
const T floor = V::set1(-127);
const T limit = V::set1((char) messageLimit);
const T minusLimit = V::set1((char) -messageLimit);
T *t = m_t;
T *prefix = m_prefix;
auto out = [&](int j, T message) {
message = V::max(V::min(message, limit), minusLimit);
c2v[j] = message;
app[vars[j]] = V::max(V::adds(t[j], message), floor);
};
extrinsic<first>(app, c2v, vars, degree, t);
T pre = t[0];
for (int j = 1; j < degree - 1; j++)
{
prefix[j] = pre;
pre = boxplus(pre, t[j]);
}
T suf = t[degree - 1];
out(degree - 1, pre);
for (int j = degree - 2; j > 0; j--)
{
T message = boxplus(prefix[j], suf);
suf = boxplus(suf, t[j]);
out(j, message);
}
out(0, suf);
}
// Number of unsatisfied parity checks of each lane, up to 255
void unsatisfied(uint8_t *counts) const
{
const int nbChecks = m_nbChecks;
const int *start = m_start;
const int *vars = m_vars;
const T *app = m_app;
const T zero = V::zero();
const T one = V::set1(1);
T count = V::zero();
for (int c = 0; c < nbChecks; c++)
{
T x = V::zero();
for (int j = start[c]; j < start[c + 1]; j++) {
x = V::xor_(x, app[vars[j]]);
}
count = V::addsu(count, V::and_(V::cmpgt(zero, x), one));
}
V::store(counts, count);
}
// All parity checks satisfied by the hard decisions, in the lanes used
bool satisfied(uint64_t laneMask) const
{
const int nbChecks = m_nbChecks;
const int *start = m_start;
const int *vars = m_vars;
const T *app = m_app;
T failed = V::zero();
for (int c = 0; c < nbChecks; c++)
{
T x = V::zero();
for (int j = start[c]; j < start[c + 1]; j++) {
x = V::xor_(x, app[vars[j]]);
}
failed = V::or_(failed, x);
if (((c & 255) == 255) && (V::movemask(failed) & laneMask)) {
return false;
}
}
return (V::movemask(failed) & laneMask) == 0;
}
#if defined(DVBT2_LDPC_X86)
// Interleave the blocks' LLRs, 16 lanes and 16 bits at a time
void load(int8_t *const *blocks, int nbBlocks)
{
int8_t *app8 = reinterpret_cast<int8_t *>(m_app);
const int n = m_n;
__m128i r[16];
for (int base = 0; base < V::W; base += 16)
{
const int8_t *rows[16];
for (int l = 0; l < 16; l++) {
rows[l] = base + l < nbBlocks ? blocks[base + l] : m_filler;
}
int v = 0;
for (; v + 16 <= n; v += 16)
{
for (int l = 0; l < 16; l++) {
r[l] = _mm_loadu_si128(reinterpret_cast<const __m128i *>(rows[l] + v));
}
transpose16x16(r);
for (int k = 0; k < 16; k++) {
_mm_storeu_si128(reinterpret_cast<__m128i *>(app8 + (size_t) (v + k) * V::W + base), r[k]);
}
}
for (; v < n; v++)
{
for (int l = 0; l < 16; l++) {
app8[(size_t) v * V::W + base + l] = rows[l][v];
}
}
}
}
void store(int8_t *const *blocks, int nbBlocks) const
{
const int8_t *app8 = reinterpret_cast<const int8_t *>(m_app);
const int n = m_n;
__m128i r[16];
for (int base = 0; base < minInt(nbBlocks, V::W); base += 16)
{
const int nbLanes = minInt(16, nbBlocks - base);
int v = 0;
for (; v + 16 <= n; v += 16)
{
for (int k = 0; k < 16; k++) {
r[k] = _mm_loadu_si128(reinterpret_cast<const __m128i *>(app8 + (size_t) (v + k) * V::W + base));
}
transpose16x16(r);
for (int l = 0; l < nbLanes; l++) {
_mm_storeu_si128(reinterpret_cast<__m128i *>(blocks[base + l] + v), r[l]);
}
}
for (; v < n; v++)
{
for (int l = 0; l < nbLanes; l++) {
blocks[base + l][v] = app8[(size_t) v * V::W + base + l];
}
}
}
}
#else
// Interleave the blocks' LLRs
void load(int8_t *const *blocks, int nbBlocks)
{
int8_t *app8 = reinterpret_cast<int8_t *>(m_app);
const int n = m_n;
for (int l = 0; l < V::W; l++)
{
const int8_t *row = l < nbBlocks ? blocks[l] : m_filler;
for (int v = 0; v < n; v++) {
app8[(size_t) v * V::W + l] = row[v];
}
}
}
void store(int8_t *const *blocks, int nbBlocks) const
{
const int8_t *app8 = reinterpret_cast<const int8_t *>(m_app);
const int n = m_n;
for (int l = 0; l < minInt(nbBlocks, V::W); l++)
{
for (int v = 0; v < n; v++) {
blocks[l][v] = app8[(size_t) v * V::W + l];
}
}
}
#endif
static const int messageLimit = 31;
static const int stuckStart = 8; //!< Iterations before looking for stuck codewords
static const int stuckChecks = 24; //!< A codeword with up to this many unsatisfied checks...
static const int stuckIterations = 4; //!< ...unchanged for this many iterations is stuck
static const int quickIterations = 10; //!< A quick block not converged by then...
static const int quickFactor = 3; //!< ...nor this many times the iterations the others took is given up
const int m_n;
const int m_nbChecks;
const int *const m_start;
const int *const m_vars;
const int8_t *const m_filler; //!< For lanes without a block: a codeword of zeros
T *const m_app; //!< Each variable's LLR
T *const m_c2v; //!< Each edge's message from its check
T *const m_t; //!< Variable to check messages of the check being updated
T *const m_prefix;
};
//! For LDPCKernel::m_decode
template <class V>
int decodeLDPC(const LDPCKernelData& data, int8_t *const *blocks, int nbBlocks, int maxIterations, int *unconverged,
uint64_t quickBlocks)
{
LDPCKernelImpl<V> kernel(data);
return kernel.decode(blocks, nbBlocks, maxIterations, unconverged, quickBlocks);
}
} // namespace
} // namespace dvbt2
#endif // MODEMDVB_DVBT2LDPCKERNEL_H

1105
modemdvb/dvbt2ldpctables.cpp

File diff suppressed because it is too large Load Diff

103
modemdvb/dvbt2ldpctables.h

@ -0,0 +1,103 @@ @@ -0,0 +1,103 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2LDPCTABLES_H
#define MODEMDVB_DVBT2LDPCTABLES_H
#include <cstdint>
#include "dvbt2fec.h"
// Internal to modemdvb: the LDPC codes' tables of parity bit accumulator addresses, for ldpcTable() (T2's,
// dvbt2ldpctables.cpp), ldpcTableS2() (dvbs2ldpctables.cpp) and ldpcTableS2X() (dvbs2xldpctables.cpp)
namespace dvbt2 {
class LDPCTables
{
private:
friend MODEMDVB_API const LDPCTable *ldpcTable(FECType type, CodeRate rate, CodeFamily family);
friend MODEMDVB_API const LDPCTable *ldpcTableS2(FECType type, CodeRate rate);
friend MODEMDVB_API const LDPCTable *ldpcTableS2X(FECType type, CodeRate rate);
// EN 302 755 annex A (Nldpc = 64 800) and annex B (Nldpc = 16 200)
static const uint16_t m_tableA1[];
static const uint16_t m_tableA2[];
static const uint16_t m_tableA3[];
static const uint16_t m_tableA4[];
static const uint16_t m_tableA5[];
static const uint16_t m_tableA6[];
static const uint16_t m_tableB1[];
static const uint16_t m_tableB2[];
static const uint16_t m_tableB3[];
static const uint16_t m_tableB4[];
static const uint16_t m_tableB5[];
static const uint16_t m_tableB6[];
static const uint16_t m_tableB7[];
static const uint16_t m_tableB8[];
static const uint16_t m_tableB9[];
// EN 302 307 annexes B and C: S2's not shared with T2
static const uint16_t m_s2TableB1[];
static const uint16_t m_s2TableB2[];
static const uint16_t m_s2TableB3[];
static const uint16_t m_s2TableB6[];
static const uint16_t m_s2TableB10[];
static const uint16_t m_s2TableB11[];
static const uint16_t m_s2TableC5[];
static const uint16_t m_s2TableC10[];
// EN 302 307-2 annexes B and C: S2X's
static const uint16_t m_s2xTableB2[];
static const uint16_t m_s2xTableB3[];
static const uint16_t m_s2xTableB4[];
static const uint16_t m_s2xTableB5[];
static const uint16_t m_s2xTableB6[];
static const uint16_t m_s2xTableB7[];
static const uint16_t m_s2xTableB8[];
static const uint16_t m_s2xTableB9[];
static const uint16_t m_s2xTableB10[];
static const uint16_t m_s2xTableB11[];
static const uint16_t m_s2xTableB12[];
static const uint16_t m_s2xTableB13[];
static const uint16_t m_s2xTableB14[];
static const uint16_t m_s2xTableB15[];
static const uint16_t m_s2xTableB16[];
static const uint16_t m_s2xTableB17[];
static const uint16_t m_s2xTableB18[];
static const uint16_t m_s2xTableB19[];
static const uint16_t m_s2xTableB20[];
static const uint16_t m_s2xTableB21[];
static const uint16_t m_s2xTableB22[];
static const uint16_t m_s2xTableB23[];
static const uint16_t m_s2xTableB24[];
static const uint16_t m_s2xTableC1[];
static const uint16_t m_s2xTableC2[];
static const uint16_t m_s2xTableC3[];
static const uint16_t m_s2xTableC4[];
static const uint16_t m_s2xTableC5[];
static const uint16_t m_s2xTableC6[];
static const uint16_t m_s2xTableC7[];
// VL-SNR's
static const uint16_t m_tableS2X_B1_2_9[];
static const uint16_t m_tableS2X_C8_1_5[];
static const uint16_t m_tableS2X_C9_11_45[];
static const uint16_t m_tableS2X_C10_1_3[];
};
} // namespace dvbt2
#endif // MODEMDVB_DVBT2LDPCTABLES_H

554
modemdvb/dvbt2modulator.cpp

@ -0,0 +1,554 @@ @@ -0,0 +1,554 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <cmath>
#include <QString>
#include "dsp/fftengine.h"
#include "dvbt2modulator.h"
namespace dvbt2 {
Modulator::Modulator(const Config& config) :
m_config(config),
m_frameIdx(0)
{
m_fft = FFTEngine::create(QString());
setup();
}
Modulator::~Modulator()
{
delete m_fft;
}
// S2 field 1 (table 19(a))
int Modulator::s2Field1(int fftSize, GuardInterval guard, bool lite)
{
bool newGuard = (guard == GuardInterval::G1_128) || (guard == GuardInterval::G19_128) || (guard == GuardInterval::G19_256);
switch (fftSize)
{
case 1024: return 3;
case 2048: return 0;
case 4096: return 2;
case 8192: return newGuard ? 6 : 1;
case 16384: return lite && newGuard ? 3 : 4;
default: return newGuard ? 7 : 5;
}
}
void Modulator::setup()
{
const Config& c = m_config;
const int n = c.m_fftSize;
const int ng = guardSamples(n, c.m_guard);
const int np2 = numP2(n);
m_tables = &Tables::get(n, c.m_extended, c.m_pattern, c.m_toneReservation, c.m_miso);
m_frameClosing = frameClosingSymbol(n, c.m_guard, c.m_pattern, c.m_miso);
// Frame length: at most 250 ms. 32K frames have an even number of symbols
int lf = c.m_dataSymbols > 0 ? c.m_dataSymbols + np2 : (int) ((0.25 * sampleRate(c.m_bandwidth) - 2048) / (n + ng));
if ((n == 32768) && (lf % 2)) {
lf--;
}
m_lf = lf;
const int ldata = lf - np2;
m_pre = L1Pre();
m_pre.m_type = 0;
m_pre.m_bwtExt = m_tables->m_extended;
m_pre.m_s1 = c.m_lite ? (int) (c.m_miso ? PreambleType::T2LiteMISO : PreambleType::T2LiteSISO)
: (int) (c.m_miso ? PreambleType::T2MISO : PreambleType::T2SISO);
m_pre.m_s2 = (s2Field1(n, c.m_guard, c.m_lite) << 1) | (c.m_fefLength > 0 ? 1 : 0);
m_pre.m_guard = c.m_guard;
m_pre.m_papr = c.m_toneReservation ? 2 : 0;
m_pre.m_l1Mod = c.m_l1Mod;
m_pre.m_pilotPattern = c.m_pattern;
m_pre.m_cellId = c.m_cellId;
m_pre.m_networkId = 0x3001;
m_pre.m_t2SystemId = 0x8001;
m_pre.m_numT2Frames = c.m_frames;
m_pre.m_numDataSymbols = ldata;
m_pre.m_numRf = 1;
m_pre.m_t2Version = 2;
m_ncells = cellsPerFECBlock(c.m_fecType, c.m_constellation);
PLP plp;
plp.m_id = 0;
plp.m_type = 1;
plp.m_payloadType = 3;
plp.m_codeRate = c.m_codeRate;
plp.m_constellation = c.m_constellation;
plp.m_rotation = c.m_rotation;
plp.m_fecType = c.m_fecType;
plp.m_frameInterval = 1;
plp.m_plpMode = c.m_highEfficiency ? 2 : 1;
m_post = L1Post();
m_post.m_frequencies.push_back(0);
m_post.m_plps.push_back(plp);
if (c.m_fefLength > 0)
{
m_post.m_fefType = 0;
m_post.m_fefLength = c.m_fefLength & 0x3fffff;
m_post.m_fefLengthMsb = c.m_fefLength >> 22;
m_post.m_fefInterval = std::max(1, c.m_fefInterval);
}
// Capacity: L1 signalling first, then the PLP, then dummy cells
std::vector<uint8_t> postBits;
m_post.encode(postBits, m_pre);
m_pre.m_l1PostSize = L1Codec::postCells(m_pre.m_l1PostInfoSize, c.m_l1Mod, n);
const int dl1 = l1PreCells + m_pre.m_l1PostSize;
const int cdata = (int) m_tables->m_data[0].size();
const int ctot = np2 * (int) m_tables->m_p2Data.size() + (ldata - 1) * cdata + (m_frameClosing ? m_tables->m_cfc : cdata);
m_blocksPerFrame = (ctot - dl1) / m_ncells;
const int pi = std::max(1, c.m_interleavingFrames);
int nti = c.m_tiBlocks;
if (pi > 1)
{
// PI of an Interleaving Frame's cells must be an integer (8.3.6.3.1)
m_blocksPerFrame = std::min(pi * m_blocksPerFrame, m_maxTICells / m_ncells);
m_post.m_plps[0].m_timeIlType = true;
m_post.m_plps[0].m_timeIlLength = pi;
}
else
{
if (nti > 0)
{
while (((m_blocksPerFrame + nti - 1) / nti) * m_ncells > m_maxTICells) {
nti++;
}
}
m_post.m_plps[0].m_timeIlLength = nti;
}
m_post.m_plps[0].m_frameInterval = std::max(1, c.m_frameInterval);
m_post.m_plps[0].m_numBlocksMax = m_blocksPerFrame;
m_post.m_plps[0].m_numBlocks = m_blocksPerFrame;
m_post.m_plps[0].m_start = 0;
m_post.m_type2Start = m_blocksPerFrame * m_ncells / pi;
m_ldpc.reset(new LDPC(c.m_fecType, c.m_codeRate));
m_bch.reset(new BCH(c.m_fecType, bchT(c.m_fecType, c.m_codeRate)));
m_bbEncoder.reset(new BBFrameEncoder(c.m_fecType, c.m_codeRate, c.m_highEfficiency));
m_cellInterleaver.reset(new CellInterleaver(m_ncells));
cellBitMap(c.m_fecType, c.m_codeRate, c.m_constellation, m_bitMap);
m_packetsPerFrame = (int) std::ceil(m_blocksPerFrame * (double) (kbch(c.m_fecType, c.m_codeRate) - 80) / (188 * 8)
/ (pi * std::max(1, c.m_frameInterval)));
makeP1(m_pre.m_s1, m_pre.m_s2, m_p1);
if (c.m_fefLength > 0) {
makeP1(c.m_fefS1, 0, m_fefP1);
}
m_fft->configure(n, true);
}
// P1 (9.8): the A part from the active carriers, then C and B, frequency shifted copies of its start and end
void Modulator::makeP1(int s1, int s2, std::vector<Cf>& p1Samples)
{
float values[384];
const P1Tables& p1 = P1Tables::get();
p1.modulation(s1, s2, values);
m_fft->configure(1024, true);
Cf *in = m_fft->in();
std::fill(in, in + 1024, Cf(0.0f, 0.0f));
for (int i = 0; i < 384; i++) {
in[(p1.m_carriers[i] - 426 + 1024) % 1024] = Cf(values[i] / std::sqrt(384.0f), 0.0f);
}
m_fft->transform();
const Cf *a = m_fft->out();
p1Samples.resize(2048);
for (int i = 0; i < 2048; i++)
{
Cf shift = std::polar(1.0f, (float) (2.0 * M_PI * i / 1024.0));
if (i < 542) {
p1Samples[i] = a[i] * shift;
} else if (i < 1566) {
p1Samples[i] = a[i - 542];
} else {
p1Samples[i] = a[i - 1024] * shift;
}
}
}
// A FEF part (8.4): a P1, then anything, here noise at the T2 signal's power
void Modulator::addFEF(std::vector<Cf>& samples)
{
const size_t start = samples.size();
samples.insert(samples.end(), m_fefP1.begin(), m_fefP1.end());
std::normal_distribution<float> noise(0.0f, (float) std::sqrt(0.5));
for (int i = (int) m_fefP1.size(); i < m_config.m_fefLength; i++) {
samples.push_back(Cf(noise(m_fefRng), noise(m_fefRng)));
}
if (m_tx2) {
m_tx2->insert(m_tx2->end(), samples.begin() + start, samples.end());
}
}
int Modulator::samplesPerFrame() const
{
return 2048 + m_lf * (m_config.m_fftSize + guardSamples(m_config.m_fftSize, m_config.m_guard));
}
// BCH, LDPC, bit interleaving and mapping (6.1, 6.2), then rotation (6.3)
void Modulator::encodeFECBlock(const std::vector<uint8_t>& bbframe, std::vector<Cf>& cells)
{
const int kbchBytes = (int) bbframe.size();
const int k = m_ldpc->k();
const int n = m_ldpc->n();
std::vector<uint8_t> packed(k / 8);
std::copy(bbframe.begin(), bbframe.end(), packed.begin());
m_bch->encode(packed.data(), kbchBytes, packed.data() + kbchBytes);
std::vector<uint8_t> codeword(n);
for (int i = 0; i < k; i++) {
codeword[i] = (packed[i / 8] >> (7 - (i % 8))) & 1;
}
m_ldpc->encode(codeword.data(), codeword.data() + k);
const int eta = bitsPerCell(m_config.m_constellation);
std::vector<Cf> f(m_ncells);
uint8_t bits[8];
for (int q = 0; q < m_ncells; q++)
{
for (int e = 0; e < eta; e++) {
bits[e] = codeword[m_bitMap[q * eta + e]];
}
f[q] = mapCell(bits, eta);
}
cells.resize(m_ncells);
if (m_config.m_rotation)
{
Cf r = std::polar(1.0f, rotationAngle(m_config.m_constellation));
for (int q = 0; q < m_ncells; q++)
{
Cf a = r * f[q];
Cf b = r * f[(q + m_ncells - 1) % m_ncells];
cells[q] = Cf(a.real(), b.imag());
}
}
else
{
cells = f;
}
}
void Modulator::modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& samples, std::vector<Cf> *samples2)
{
m_tx2 = m_config.m_miso ? samples2 : nullptr;
std::vector<uint8_t> bbframe;
for (int p = 0; p < nbPackets; p++)
{
if (m_bbEncoder->addPacket(packets + 188 * p, bbframe)) {
m_bbframes.push_back(bbframe);
}
while ((int) m_bbframes.size() >= blocksNeeded()) {
buildFrame(samples);
}
}
}
// Blocks the next frame takes: an Interleaving Frame's, if it's the first it's in
int Modulator::blocksNeeded() const
{
const int ijump = std::max(1, m_config.m_frameInterval);
const int pi = std::max(1, m_config.m_interleavingFrames);
return ((m_frameIdx % ijump) == 0) && (((m_frameIdx / ijump) % pi) == 0) ? m_blocksPerFrame : 0;
}
void Modulator::buildFrame(std::vector<Cf>& samples)
{
const Config& c = m_config;
const int ijump = std::max(1, c.m_frameInterval);
const int pi = std::max(1, c.m_interleavingFrames);
const bool present = (m_frameIdx % ijump) == 0;
const int part = (m_frameIdx / ijump) % pi;
// The PLP's cells in this frame: part of the Interleaving Frame (8.3.6.3.1)
std::vector<Cf> plpCells;
if (present)
{
if (part == 0) {
encodeInterleavingFrame();
}
const size_t d = m_ifCells.size() / pi;
plpCells.assign(m_ifCells.begin() + part * d, m_ifCells.begin() + (part + 1) * d);
}
m_post.m_plps[0].m_start = 0;
m_post.m_type2Start = (int) plpCells.size();
buildFrameCells(plpCells, samples);
}
// The PLP's cells: cell and time interleaving of the FEC blocks (6.4, 6.5)
void Modulator::encodeInterleavingFrame()
{
const PLP& plp = m_post.m_plps[0];
std::vector<Cf>& plpCells = m_ifCells;
plpCells.clear();
plpCells.reserve((size_t) m_blocksPerFrame * m_ncells);
std::vector<std::vector<Cf>> blocks(m_blocksPerFrame);
for (int b = 0; b < m_blocksPerFrame; b++) {
encodeFECBlock(m_bbframes[b], blocks[b]);
}
m_bbframes.erase(m_bbframes.begin(), m_bbframes.begin() + m_blocksPerFrame);
const int nti = plp.m_timeIlType ? 1 : plp.m_timeIlLength;
if (nti == 0)
{
for (int b = 0; b < m_blocksPerFrame; b++)
{
std::vector<Cf> d(m_ncells);
for (int q = 0; q < m_ncells; q++) {
d[m_cellInterleaver->address(0, q)] = blocks[b][q];
}
plpCells.insert(plpCells.end(), d.begin(), d.end());
}
}
else
{
int b = 0;
for (int s = 0; s < nti; s++)
{
int nfec = m_blocksPerFrame / nti + (s >= nti - (m_blocksPerFrame % nti) ? 1 : 0);
std::vector<Cf> d((size_t) nfec * m_ncells);
for (int r = 0; r < nfec; r++, b++)
{
for (int q = 0; q < m_ncells; q++) {
d[(size_t) r * m_ncells + m_cellInterleaver->address(r, q)] = blocks[b][q];
}
}
std::vector<int> read;
timeInterleaver(m_ncells, nfec, read);
for (size_t i = 0; i < read.size(); i++) {
plpCells.push_back(d[read[i]]);
}
}
}
}
// L1, the PLP's cells and dummy cells into the frame's symbols
void Modulator::buildFrameCells(std::vector<Cf>& plpCells, std::vector<Cf>& samples)
{
const Config& c = m_config;
const int np2 = numP2(c.m_fftSize);
// L1 signalling for this frame
m_post.m_frameIdx = m_frameIdx;
std::vector<uint8_t> postBits;
m_post.encode(postBits, m_pre);
std::vector<Cf> postCells, preCells;
m_l1Codec.encodePost(postBits, m_pre, postCells);
uint8_t preBits[L1Pre::size];
m_pre.encode(preBits);
m_l1Codec.encodePre(preBits, preCells);
// Dummy cells (8.3.8): the BB scrambling sequence, BPSK
const int cp2 = (int) m_tables->m_p2Data.size();
const int cdata = (int) m_tables->m_data[0].size();
const int dl1 = (int) (preCells.size() + postCells.size());
const int ctot = np2 * cp2 + (m_lf - np2 - 1) * cdata + (m_frameClosing ? m_tables->m_cfc : cdata);
const int ndummy = ctot - dl1 - (int) plpCells.size();
std::vector<uint8_t> dummyBits(ndummy, 0);
scramble(dummyBits.data(), ndummy);
for (int i = 0; i < ndummy; i++) {
plpCells.push_back(Cf(dummyBits[i] ? -1.0f : 1.0f, 0.0f));
}
// Map to the symbols (8.3.5, 8.3.6.2): L1 zig-zag across the P2 symbols, then the cells in address order
addP1(samples);
size_t address = 0;
const int preRows = (int) preCells.size() / np2;
const int postRows = (int) postCells.size() / np2;
for (int l = 0; l < m_lf; l++)
{
std::vector<Cf> cells;
if (l < np2)
{
cells.resize(cp2);
for (int p = 0; p < preRows; p++) {
cells[p] = preCells[np2 * p + l];
}
for (int p = 0; p < postRows; p++) {
cells[preRows + p] = postCells[np2 * p + l];
}
for (int p = preRows + postRows; p < cp2; p++) {
cells[p] = plpCells[address++];
}
}
else if (m_frameClosing && (l == m_lf - 1))
{
cells.assign(m_tables->m_fcData.size(), Cf(0.0f, 0.0f));
for (int p = 0; p < m_tables->m_cfc; p++) {
cells[p] = plpCells[address++];
}
}
else
{
cells.resize(cdata);
for (int p = 0; p < cdata; p++) {
cells[p] = plpCells[address++];
}
}
addSymbol(l, cells, samples);
}
if ((c.m_fefLength > 0) && ((m_frameIdx + 1) % std::max(1, c.m_fefInterval) == 0)) {
addFEF(samples);
}
m_frameIdx = (m_frameIdx + 1) % c.m_frames;
}
void Modulator::addP1(std::vector<Cf>& samples)
{
samples.insert(samples.end(), m_p1.begin(), m_p1.end());
if (m_tx2) {
m_tx2->insert(m_tx2->end(), m_p1.begin(), m_p1.end());
}
}
// The second group of transmitters' pilot sign (9.2.8)
float Modulator::misoSign(int k, int l) const
{
const Tables& t = *m_tables;
if (l < numP2(m_config.m_fftSize)) {
return (k % 6 == 3) ? -1.0f : 1.0f;
}
if ((k == 0) || (k == t.m_kMax)) {
return (l & 1) ? -1.0f : 1.0f;
}
if (k % t.m_dx == 0) {
return ((k / t.m_dx) & 1) ? -1.0f : 1.0f;
}
return 1.0f;
}
// Frequency interleaving, pilots, IFFT and guard interval (8.5, 9.2, 9.5, 9.7)
void Modulator::addSymbol(int l, const std::vector<Cf>& cells, std::vector<Cf>& samples)
{
const int n = m_config.m_fftSize;
const int np2 = numP2(n);
const Tables& t = *m_tables;
const bool p2 = l < np2;
const bool fc = !p2 && m_frameClosing && (l == m_lf - 1);
const std::vector<int>& data = p2 ? t.m_p2Data : fc ? t.m_fcData : t.m_data[l % t.m_dy];
const std::vector<Tables::Pilot>& pilots = p2 ? t.m_p2Pilots : fc ? t.m_fcPilots : t.m_pilots[l % t.m_dy];
const std::vector<int>& read = p2 ? t.p2Deinterleave(l) : fc ? t.fcDeinterleave(l) : t.dataDeinterleave(l);
const int pn = framePN(l);
const float scale = 5.0f / std::sqrt(27.0f * (t.m_kMax + 1));
const int ng = guardSamples(n, m_config.m_guard);
auto bin = [n, &t](int k) { return (k - t.m_kMax / 2 + n) % n; };
// The cells in carrier order, the frequency interleaver's output
m_carrierCells.assign(data.size(), Cf(0.0f, 0.0f));
for (size_t p = 0; p < cells.size(); p++) {
m_carrierCells[read[p]] = cells[p];
}
// The first group of transmitters, or the only one, then in MISO the second: each pair of cells a0, a1 as
// -a1*, a0* (9.1), and some pilots inverted
for (int group = 0; group < (m_tx2 ? 2 : 1); group++)
{
Cf *in = m_fft->in();
std::fill(in, in + n, Cf(0.0f, 0.0f));
for (size_t i = 0; i < data.size(); i++)
{
if (group == 0) {
in[bin(data[i])] = m_carrierCells[i];
} else if (i & 1) {
in[bin(data[i])] = std::conj(m_carrierCells[i - 1]);
} else if (i + 1 < data.size()) {
in[bin(data[i])] = -std::conj(m_carrierCells[i + 1]);
}
}
for (const Tables::Pilot& pilot : pilots)
{
int r = t.m_w[pilot.m_k] ^ pn;
const float sign = group == 1 ? misoSign(pilot.m_k, l) : 1.0f;
in[bin(pilot.m_k)] = Cf(sign * (r ? -pilot.m_amplitude : pilot.m_amplitude), 0.0f);
}
m_fft->transform();
const Cf *out = m_fft->out();
std::vector<Cf>& to = group == 0 ? samples : *m_tx2;
for (int i = n - ng; i < n; i++) {
to.push_back(out[i] * scale);
}
for (int i = 0; i < n; i++) {
to.push_back(out[i] * scale);
}
}
}
} // namespace dvbt2

123
modemdvb/dvbt2modulator.h

@ -0,0 +1,123 @@ @@ -0,0 +1,123 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2MODULATOR_H
#define MODEMDVB_DVBT2MODULATOR_H
#include <memory>
#include <random>
#include <vector>
#include "dvbt2.h"
#include "dvbt2fec.h"
class FFTEngine;
namespace dvbt2 {
//! DVB-T2 transmitter, for testing the receiver: one type 1 PLP carrying a transport stream, SISO or MISO (both
//! groups of transmitters' signals), no PAPR reduction (but reserved carriers may be signalled). FEF parts, if any,
//! are a P1 then noise
class MODEMDVB_API Modulator
{
public:
struct Config
{
int m_fftSize = 32768;
bool m_extended = true;
GuardInterval m_guard = GuardInterval::G1_128;
PilotPattern m_pattern = PilotPattern::PP7;
bool m_toneReservation = false;
L1Constellation m_l1Mod = L1Constellation::QAM64;
Constellation m_constellation = Constellation::QAM256;
CodeRate m_codeRate = CodeRate::R2_3;
FECType m_fecType = FECType::Normal;
bool m_rotation = true;
int m_dataSymbols = 0; //!< 0 for the most that fit in 250 ms
int m_frames = 2; //!< T2-frames per super-frame
int m_tiBlocks = 1; //!< 0 for no time interleaving
int m_interleavingFrames = 1; //!< PI: T2-frames an Interleaving Frame is spread over (then one TI-block)
int m_frameInterval = 1; //!< IJUMP: the PLP is in every IJUMP-th T2-frame (m_frames a multiple of PI x IJUMP)
bool m_highEfficiency = false;
int m_cellId = 0;
int m_bandwidth = 8000000;
int m_fefLength = 0; //!< Samples in each FEF part, from its P1 to the next T2-frame's, 0 for none
int m_fefInterval = 1; //!< T2-frames between FEF parts (m_frames must be a multiple of it)
int m_fefS1 = 2; //!< Of the FEF parts' P1s: 2 non-T2, 3 T2-Lite SISO
bool m_lite = false; //!< T2-Lite profile (16K LDPC only, with 1/3 and 2/5 too)
bool m_miso = false; //!< MISO: modulate() gives the second group of transmitters' signal too
};
explicit Modulator(const Config& config);
~Modulator();
//! TS packets each frame takes, on average
int packetsPerFrame() const { return m_packetsPerFrame; }
//! Samples in a frame
int samplesPerFrame() const;
const L1Pre& l1Pre() const { return m_pre; }
const Config& config() const { return m_config; }
//! Modulate whole frames from the packets (packetsPerFrame() each), appending to samples. Any remainder is kept
//! In MISO, appends the second group of transmitters' samples to samples2, if given
void modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& samples, std::vector<Cf> *samples2 = nullptr);
private:
static constexpr int m_maxTICells = (1 << 19) + (1 << 15); //!< MTI (6.5.2)
static int s2Field1(int fftSize, GuardInterval guard, bool lite); //!< S2 field 1 (table 19(a))
void setup();
void encodeFECBlock(const std::vector<uint8_t>& bbframe, std::vector<Cf>& cells);
void encodeInterleavingFrame();
void buildFrameCells(std::vector<Cf>& plpCells, std::vector<Cf>& samples);
int blocksNeeded() const; //!< For the next frame
void buildFrame(std::vector<Cf>& samples);
void addP1(std::vector<Cf>& samples);
void makeP1(int s1, int s2, std::vector<Cf>& p1);
void addFEF(std::vector<Cf>& samples);
void addSymbol(int l, const std::vector<Cf>& cells, std::vector<Cf>& samples);
Config m_config;
const Tables *m_tables;
L1Pre m_pre;
L1Post m_post;
L1Codec m_l1Codec;
std::unique_ptr<LDPC> m_ldpc;
std::unique_ptr<BCH> m_bch;
std::unique_ptr<BBFrameEncoder> m_bbEncoder;
std::unique_ptr<CellInterleaver> m_cellInterleaver;
std::vector<int> m_bitMap;
int m_ncells;
int m_blocksPerFrame; //!< In an Interleaving Frame
std::vector<Cf> m_ifCells; //!< Of the Interleaving Frame being sent
int m_packetsPerFrame;
int m_lf; //!< Symbols per frame, excluding P1
bool m_frameClosing;
int m_frameIdx;
std::vector<std::vector<uint8_t>> m_bbframes;
FFTEngine *m_fft;
std::vector<Cf> m_p1;
std::vector<Cf> *m_tx2 = nullptr; //!< The second group's samples, during modulate()
std::vector<Cf> m_carrierCells;
float misoSign(int k, int l) const;
std::vector<Cf> m_fefP1;
std::mt19937 m_fefRng;
};
} // namespace dvbt2
#endif // MODEMDVB_DVBT2MODULATOR_H

3063
modemdvb/dvbt2ofdm.cpp

File diff suppressed because it is too large Load Diff

357
modemdvb/dvbt2ofdm.h

@ -0,0 +1,357 @@ @@ -0,0 +1,357 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2OFDM_H
#define MODEMDVB_DVBT2OFDM_H
#include <cstdint>
#include <functional>
#include <array>
#include <map>
#include <vector>
#include "cwcanceller.h"
#include "dvbt2.h"
class FFTFactory;
class FFTEngine;
namespace dvbt2 {
//! The channel's impulse response, for display: power in dB relative to the strongest path, against delay from the
//! FFT window's start, bin b being b * m_resolution samples, wrapping round after m_powerDb.size() bins
struct ImpulseResponse
{
std::vector<float> m_powerDb;
double m_resolution = 0.0; //!< Samples a bin
int m_guard = 0; //!< Guard interval, samples
};
//! DVB-T2 OFDM demodulation: P1 detection, synchronisation, channel estimation and frequency de-interleaving.
//! Outputs the data cells of each P2 and data symbol, in frame builder order, with their reliabilities
class MODEMDVB_API OFDMDemod
{
public:
enum State
{
SearchP1, //!< Looking for a P1 symbol
FindGuard, //!< Found a P1, finding the guard interval from the next symbols
Symbols, //!< Demodulating the symbols of a frame
NextP1 //!< Expecting the P1 of the next frame
};
struct Symbol
{
int m_frame; //!< Frames since acquisition
int m_l; //!< Symbol in the frame, from 0 for the first P2
bool m_p2;
bool m_frameClosing;
const Cf *m_cells; //!< Equalised and frequency de-interleaved
const float *m_llrScales; //!< 4 / noise variance per dimension of each equalised cell
int m_nbCells;
};
typedef std::function<void(const Symbol& symbol)> SymbolCallback;
typedef std::function<void(const char *message)> LogCallback;
struct Status
{
State m_state = SearchP1;
P1Signalling m_p1;
int m_fftSize = 0;
GuardInterval m_guard = GuardInterval::G1_32;
bool m_frameKnown = false; //!< L1-pre decoded for the current frame
double m_frequencyOffset = 0.0; //!< Hz
double m_clockOffset = 0.0; //!< ppm, from the continual pilots, or the P1 interval
bool m_clockFromP1s = false; //!< Only from the spacing of P1s found by searching, as L1-pre isn't decoded
uint64_t m_fefParts = 0; //!< Future Extension Frame parts skipped
double m_p1Correlation = 0.0;
double m_snr = 0.0; //!< dB, from the continual pilots
uint64_t m_frames = 0;
uint64_t m_acquisitions = 0;
};
explicit OFDMDemod(int bandwidthHz = 8000000, FFTFactory *fftFactory = nullptr);
~OFDMDemod();
void setCallback(SymbolCallback callback) { m_callback = callback; }
void setLogCallback(LogCallback callback) { m_log = callback; }
void reset();
void process(const Cf *samples, int nbSamples);
//! Call from the symbol callback with the L1-pre of the frame, once decoded from the P2 symbols, so the data
//! symbols can be demodulated. If it isn't called by the last P2 symbol, the receiver searches for the next P1
void setFrameParameters(const L1Pre& pre);
//! Samples in each FEF part (from its P1 to the next T2-frame's), from L1-post, 0 if none or not known
void setFEFLength(int64_t samples) { m_fefLength = samples; }
//! The profile to receive. The other's frames are FEF parts. Takes effect from the next search for a P1
void setProfile(Profile profile) { m_profile = profile; }
Status getStatus() const { return m_status; }
//! The clock offset of the samples from sample (from the first given since reset()) on has changed by ppm, as
//! when they're resampled to correct it. Symbols before it keep the old offset, and those after the new, so
//! the frame's timing stays continuous
void adjustClockOffset(double ppm, int64_t sample);
//! Narrowband interferers notched out before the FFT, Hz from the input's centre, so their carriers are erased
void setNotches(const std::vector<double>& frequencies, double inputRate) { m_notched.setNotches(frequencies, inputRate); }
//! Equalised cells and the channel's magnitude response (dB), for display. Call from the processing thread
void getDisplayData(std::vector<Cf>& cells, int maxCells, std::vector<float>& responseDb, int points) const;
//! The latest impulse response, in at most points bins, empty if none. Call from the processing thread
void getImpulseResponse(ImpulseResponse& response, int points) const;
//! For analysis: the error power (relative to the signal) and count of the continual pilots of each carrier
void getPilotErrors(std::vector<double>& error, std::vector<double>& count) const { error = m_pilotError; count = m_pilotCount; }
//! For analysis: SNR of each data symbol from its pilots, dB
const std::vector<float>& symbolSNRs() const { return m_symbolSNRs; }
private:
struct FFT
{
FFTFactory *m_factory = nullptr;
FFTEngine *m_engine = nullptr;
unsigned int m_sequence = 0;
int m_size = 0;
bool m_inverse = false;
void configure(int size, bool inverse);
void release();
};
static constexpr double m_twoPi = 6.283185307179586;
static constexpr double m_p1Threshold = 0.4; //!< Normalised P1 correlation
static constexpr int m_p1Length = 2048; //!< TP1 in samples
static constexpr int m_nextP1Range = 48; //!< Samples either side of the expected P1 position to search
static constexpr double m_frequencyLoopGain = 0.2;
static constexpr int m_interpolationHalfLength = 6; //!< Pilots each side, for frequency interpolation
//! The P1's frequency shift removed: sample n times shift[n mod 1024], one 1K carrier down
static const std::complex<double> *p1Shifts();
//! The impulse response's power, reduced to at most points bins (by maximum, which keeps narrow paths), in dB
//! relative to its peak, down to -60 dB
static void impulseResponseDb(const std::vector<float>& power, double resolution, int points, std::vector<float>& powerDb,
double& binResolution);
bool searchP1();
//! The measurements of the frames before (of timing, clock and channel) are stale: a P1 has been found by searching
void startAcquisition();
bool decodeP1(int64_t start, double fraction, P1Signalling& p1, double& frequency);
double p1Metric(int64_t start, std::complex<double>& c1, std::complex<double>& c2) const;
bool findGuard();
bool processSymbol();
bool nextP1();
//! Sample index of a position in the frame, nominally offset samples after the P1 start, allowing for the clock offset
int64_t framePosition(int64_t offset) const;
int64_t frameLength() const; //!< Nominal samples from the frame's (or FEF part's) P1 to the next P1
double m_exactDrift = 0.0; //!< Of this symbol from its nominal position, samples, from the clock offset
//! Of this symbol: its exact position (from m_exactDrift) less the sample framePosition() put it at. Usually a
//! fraction of a sample, but several when the frame's clock measurement differs from the estimate it started with
double m_timingFraction = 0.0;
double m_previousTimingFraction = 0.0;
bool m_repeatSymbol = false; //!< The symbol is being demodulated again, after measuring its impulse response
//! Of the window from start, with the frequency corrected, into the FFT's output, which extractCarriers() reads
void fft(int64_t start);
void estimateChannel(const std::vector<Tables::Pilot>& pilots, int spacing, int offset, int l, bool hold, float amplitude);
void interpolate(int spacing, int offset, bool hold);
void interpolateGrid(const Cf *source, Cf *dest, int spacing, int offset, double delay);
// MISO (9.1, 9.2.8): the second group of transmitters sends each pair of data cells Alamouti coded, and its pilots
// inverted on alternate pilot-bearing carriers, so the pilots on a grid are H1 + (-1)^j H2: the two channels are
// the grid, and it alternated, half-band filtered
static void edgeFilter(double pass, double stop, int left, int right, std::vector<float>& taps);
void separateMISO(const Cf *source, int spacing, int offset, int kMax, bool useCentres);
double meanDelay(const Cf *grid, int spacing, int offset, int nbPilots) const;
float misoSign(int k, int l) const; //!< Of the second group's pilot on carrier k of symbol l
void misoImpulseResponse(int spacing, int offset, int windowOffset, int l);
//! Delays of the clusters of paths in the last impulse response measured, strongest first, of those with at least
//! a thirtieth of its power
void cirClusters(int kMax, int spacing, int offset, int windowOffset, std::vector<double>& delays) const;
void equalise(const std::vector<int>& data, const std::vector<int>& read, const std::vector<Tables::Pilot>& pilots, int l);
//! What equalise() changes of the noise estimates, so trials of the channel estimate at acquisition can be undone
struct NoiseState
{
double m_noise;
double m_noiseRatio;
double m_snr;
std::vector<float> m_carrierNoise;
std::vector<float> m_carrierWeight;
};
void saveNoise(NoiseState& state) const;
void restoreNoise(NoiseState& state);
//! Of the P2 symbol just equalised: its first nbCells (L1-pre's, BPSK) squared distance from their points
double l1PreError(int nbCells) const;
void trackFrequency(int64_t start);
void measureClock(int64_t window);
const Cf *bufferAt(int64_t index) const { return &m_buffer[index - m_bufferOffset]; }
int64_t bufferEnd() const { return m_bufferOffset + (int64_t) m_buffer.size(); }
void log(const char *format, ...);
double m_sampleRate;
SymbolCallback m_callback;
LogCallback m_log;
Status m_status;
FFT m_fft;
FFT m_p1Fft;
FFT m_cirFft; //!< Impulse response from the pilots
// FFT window position: the paths' first delay and spread, measured over a frame, place the window for the next,
// so they all fall within the guard interval
//! A reading of the impulse response: delays from the window start
struct CIRReading
{
double m_delay; //!< Of the first path
double m_spread;
};
//! Accumulates the measurement for the window placement, or if readings is given, returns those the guard interval
//! could hold (most likely first), for the caller to choose
void measureImpulseResponse(const Cf *estimates, int kMax, int spacing, int offset, int windowOffset, int l,
bool resolveAmbiguity, std::vector<CIRReading> *readings = nullptr);
void useImpulseResponse(const CIRReading& reading, int windowOffset);
int windowOffsetFor(double firstPath, double spread) const;
void extractCarriers(int kMax);
std::vector<CIRReading> m_cirReadings;
std::vector<double> m_p1Metrics; //!< Over the next P1's search range
std::vector<std::complex<double>> m_p1C1, m_p1C2;
double continualPilotFit(const Cf *cir, int size, double resolution, int first, double firstDelay, int offset, int l) const;
void updateWindowOffset();
int windowOffset() const;
int m_windowOffset = -1; //!< Window start after the nominal symbol start, -1 for the default
int m_windowGuard = 0; //!< Guard interval it is for
double m_firstPathSum = 0.0; //!< Over the frame: first path relative to the nominal symbol start, samples
double m_spreadSum = 0.0;
int m_cirCount = 0;
double m_spread = 0.0; //!< Delay spread over the last frame, samples
bool m_timeInterpolation = false; //!< Use the scattered pilots of earlier symbols too, for a long spread
double m_centre = 0.0; //!< Delay of the middle of the paths from the window start, from the latest measurement
bool m_centreValid = false;
std::vector<float> m_cirPower;
double m_cirResolution = 0.0; //!< Samples a bin of m_cirPower, 0 until measured
std::vector<float> m_cirSorted;
std::vector<int> m_cirBins;
std::vector<Cf> m_buffer;
int64_t m_bufferOffset;
int64_t m_searchPos; //!< Next P1 start to test
double m_frequency; //!< Correction, cycles per sample
double m_ncoPhase; //!< Cycles, at m_ncoIndex, so changes of frequency don't make the phase jump
int64_t m_ncoIndex;
// Frame
int m_fftSize;
int m_guardSamples;
int m_np2;
int64_t m_frameStart; //!< P1 start
int64_t m_previousFrameStart;
int m_l; //!< Next symbol
int m_lf; //!< Symbols in the frame, once known
bool m_frameClosing;
bool m_frameKnown;
std::vector<GuardInterval> m_guardCandidates;
const Tables *m_p2Tables;
const Tables *m_tables;
L1Pre m_pre;
int m_frameCount;
// Symbol
std::vector<Cf> m_y; //!< Carriers 0..Kmax
std::vector<Cf> m_h; //!< Channel estimate (of the first group of transmitters, in MISO)
std::vector<Cf> m_h2; //!< Of the second group, in MISO
std::vector<Cf> m_misoGrid[2]; //!< The two groups' pilots, separated
std::vector<Cf> m_misoPilots; //!< separateMISO()'s, one group's at a time, with its delay removed
std::vector<Cf> m_p2Hold; //!< P2 pilots in MISO, whose signs differ from the data symbols' on the same carriers
std::vector<Cf> m_p2Channels[2]; //!< The last P2 symbol's two channels, in MISO
std::vector<Cf> m_misoCells; //!< Alamouti decoded, in carrier order
std::vector<float> m_misoGain;
std::vector<float> m_misoTaps[2]; //!< Each group's low-pass filter
std::vector<std::complex<double>> m_cpeTerms; //!< Each pilot's part of the common phase's measurement
//! Filters at the edges, where the pilots either side are fewer: by their pass and stop bands (in 1/200 cycles a
//! pilot) and the pilots either side, kept for P2's and the data symbols' spacings
std::map<std::array<int, 4>, std::vector<float>> m_misoEdgeTaps;
double m_misoCutoff[2] = {0.25, 0.25}; //!< Of each group's filter, cycles a pilot
float m_misoTapSum[2] = {1.0f, 1.0f};
double m_misoSpread[2] = {-1.0, -1.0}; //!< Of each group's paths, from its impulse response, -1 if not known
bool m_miso = false;
double m_misoCentre[2] = {0.0, 0.0}; //!< Middle of each group's paths, from their impulse responses
bool m_misoCentreValid[2] = {false, false};
bool m_misoPresent[2] = {true, true}; //!< Not far weaker than the other group, as last separated
double m_misoDelay[2] = {0.0, 0.0}; //!< Each group's channel is centred on, as separated
std::vector<Cf> m_hold; //!< Latest pilot estimate on every pilot carrier, for time interpolation
std::vector<float> m_holdAmplitude; //!< Of the pilot each estimate is from, 0 for none yet
double m_holdNoise = 0.0; //!< This symbol's pilots' change from the estimates held from earlier ones
double m_holdWeight = 0.0;
int m_holdCount = 0;
std::vector<double> m_pilotError;
std::vector<float> m_symbolSNRs;
std::vector<double> m_pilotCount;
std::vector<Cf> m_cells;
std::vector<float> m_llrScales;
std::vector<Cf> m_eqCells; //!< Equalised, in carrier order (SISO)
std::vector<float> m_eqScales;
std::vector<Cf> m_previousCP; //!< Continual pilots of the previous data symbol
int64_t m_previousWindow; //!< -1 if there wasn't one
double m_clockSum; //!< Of the measurements in this frame, ppm
int m_clockFrames; //!< Frames the clock offset has been measured over
int64_t m_lastSearchedP1; //!< Start of the last P1 found by searching, -1 if none
int64_t m_lastP1Symbols; //!< Symbols between the last two P1s found by searching, -1 if not known
int64_t m_fefLength = 0; //!< Samples in a FEF part, 0 if none or not known
Profile m_profile = Profile::Auto;
bool m_lite = false; //!< Receiving T2-Lite
bool profileWanted(const P1Signalling& p1) const;
bool m_inFEF = false; //!< m_frameStart is the P1 of a FEF part, not of a T2-frame
// The first P2 symbol after acquisition gives the impulse response, from its dense pilots, and is demodulated
// again with the window and channel estimation for it, so a long echo doesn't stop L1 being decoded
bool m_p2Measured = true;
int m_noiseSettling = 0; //!< Data symbols left whose own noise estimate is used, rather than an average
std::vector<Cf> m_p2Estimates;
dvbt::NotchedCarriers m_notched;
// Timing jumps (samples lost): the guard interval correlation at each symbol, which collapses
double m_giAverage; //!< 0 until measured
int m_giLow; //!< Consecutive symbols with too little
double guardCorrelation(int64_t start) const;
bool guardMoved(int64_t start, double threshold) const;
// A change of the clock offset part way through a frame: m_status.m_clockOffset is that of the samples from
// m_clockChangeSample on, and before them it's m_clockChangeDelta less. Cleared when a frame starts after it
int64_t m_clockChangeSample;
double m_clockChangeDelta; //!< ppm, 0 for none
int p1Range() const; //!< Samples either side of the next P1's expected position to search
int m_clockCount;
double m_noise; //!< Noise variance per dimension of the received cells, relative to the mean |h|^2
double m_noiseRatio = -1.0; //!< The noise's part proportional to |h|^2, over the rest, -1 until measured
double m_delay; //!< Of the channel estimate, samples
std::vector<std::vector<float>> m_interpolationTaps;
std::vector<float> m_tapPower; //!< Sum of the squares of the taps, for each position between pilots
std::vector<float> m_carrierTapPower; //!< m_tapPower of each carrier's position
// Noise on each carrier (from its pilots), so narrowband interference (spurs, leakage, what's left near a notch)
// makes its cells less confident
std::vector<float> m_carrierNoise; //!< Variance, -1 until measured
std::vector<float> m_carrierWeight; //!< Of each carrier's cells, the median noise over its smoothed noise
int m_carrierWeightAge = 0; //!< Symbols since m_carrierWeight was worked out
std::vector<float> m_measuredNoise; //!< updateCarrierWeights()'s, kept so it isn't allocated each time
void carrierNoise(int k, float variance);
//! If the carrier's noise is much more than the others', so it's left out of the overall estimate
bool noisyCarrier(int k) const { return (k < (int) m_carrierWeight.size()) && (m_carrierWeight[k] < 1.0f); }
void updateCarrierWeights(int kMax);
std::vector<Cf> m_padded; //!< Pilots with the mean delay removed, the first and last repeated at each end
std::vector<Cf> m_phaseOut; //!< Interpolated, for one position between pilots
std::vector<Cf> m_pilotRamp; //!< To restore the delay: at each pilot, and at each position after a pilot
std::vector<Cf> m_phaseRamp;
int m_interpolationSpacing;
int m_gridSpacing; //!< Of the pilots the channel estimate is from
int m_gridOffset;
float m_gridAmplitude;
};
} // namespace dvbt2
#endif // MODEMDVB_DVBT2OFDM_H

1434
modemdvb/dvbt2receiver.cpp

File diff suppressed because it is too large Load Diff

313
modemdvb/dvbt2receiver.h

@ -0,0 +1,313 @@ @@ -0,0 +1,313 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2RECEIVER_H
#define MODEMDVB_DVBT2RECEIVER_H
#include <atomic>
#include <condition_variable>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "dvbt2.h"
#include "dvbt2fec.h"
#include "dvbt2ofdm.h"
#include "cwcanceller.h"
#include "ldpcpool.h"
#include "resampler.h"
class SampleThread;
namespace dvbt2 {
//! DVB-T2 receiver: samples at 1/T in, transport stream packets of one PLP out.
//! If threaded, resampling runs in the thread calling process(), and OFDM demodulation (with the frames' assembly
//! and L1 decoding) in a thread of its own. De-interleaving and FEC decoding run in worker threads, each decoding an
//! interleaving frame, its batches of FEC blocks shared with decoder threads common to the workers, so several frames
//! are decoded at once
class MODEMDVB_API Receiver
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback;
struct Status
{
OFDMDemod::Status m_ofdm;
bool m_l1Valid = false;
L1Pre m_pre;
L1Post m_post;
int m_plpId = -1; //!< PLP being decoded
uint64_t m_l1Errors = 0;
uint64_t m_fecBlocks = 0;
uint64_t m_ldpcFailures = 0; //!< Blocks LDPC didn't converge on (BCH may still correct them)
uint64_t m_bchFailures = 0;
uint64_t m_bchCorrected = 0; //!< Bits
uint64_t m_bchCorrectedBlocks = 0;
double m_ldpcIterations = 0.0; //!< Average
BBFrameDecoder::Stats m_bb;
double m_mer = 0.0; //!< dB, of the PLP's cells (unrotated constellations)
uint64_t m_queueWaits = 0;
std::vector<double> m_notches; //!< Frequencies of narrowband interferers notched out, Hz from the centre
double m_seconds[4] = {0.0, 0.0, 0.0, 0.0}; //!< Demodulation, time de-interleaving, demapping, LDPC and BCH, BBFRAMEs
// Analysis, of the cells of blocks that decoded: error relative to what was sent, scaled by the noise the
// LLRs assume, so each dimension should have unit variance. Separately for the real and imaginary parts
uint64_t m_analysedCells = 0;
double m_normalisedError[2] = {0.0, 0.0};
uint64_t m_outliers[3] = {0, 0, 0}; //!< Normalised error power per dimension over 9, 16, 25
double m_errorByScale[8] = {0.0}; //!< Normalised error power, by LLR scale octave relative to the median
uint64_t m_cellsByScale[8] = {0};
};
//! decoderThreads: for LDPC decoding in all, 0 for all but two of the cores, up to 8
explicit Receiver(int bandwidthHz = 8000000, FFTFactory *fftFactory = nullptr, bool threaded = true, int decoderThreads = 0);
//! Notching out strong narrowband interferers, on by default. From the thread that calls process()
void setNotchEnabled(bool enabled) { m_canceller.setEnabled(enabled); }
~Receiver();
void setPacketCallback(PacketCallback callback) { m_packetCallback = callback; }
void setLogCallback(OFDMDemod::LogCallback callback);
//! PLP to decode, or -1 for the first data PLP (also used if the one selected isn't there)
void selectPLP(int id) { m_selectedPLP = id; }
//! T2-base or T2-Lite. From the thread that calls process(): when it changes, the signal is acquired again
void setProfile(Profile profile);
//! LDPC iterations limit. BCH corrects the few errors LDPC leaves near the threshold, so this needn't be high
void setMaxIterations(int iterations) { m_maxIterations = iterations; }
//! Reduce the reliability of cells much further from the constellation than noise would put them. On by default
void setOutlierLimiting(bool enable) { m_outlierLimiting = enable; }
void setAnalysis(bool enable) { m_analysis = enable; }
void reset();
//! Input sample rate, if not 1/T (64/7 MS/s for 8 MHz): resampled, from about 0.5 to 2 times
void setInputSampleRate(double sampleRate);
void process(const Cf *samples, int nbSamples);
//! Wait for the error correction of everything received to finish
void flush();
Status getStatus();
//! For display: up to maxCells of the PLP's cells, with any constellation rotation undone, and the channel
//! frequency response magnitude in dB relative to its mean. The response is from the demodulator a moment ago
//! (none the first time), as it gives it between calls to process(), in that thread
void getDisplayData(std::vector<Cf>& cells, int maxCells, std::vector<float>& responseDb, int points) const;
//! As OFDMDemod::getImpulseResponse(), from when getDisplayData() was last satisfied
void getImpulseResponse(ImpulseResponse& response) const;
//! Only to be used after flush()
const OFDMDemod& ofdm() const { return m_ofdm; }
private:
//! The cells of one interleaving frame of the PLP, for the worker
struct InterleavingFrame
{
uint64_t m_sequence; //!< To finish in order
bool m_gap = false; //!< Not following the last decoded, so a packet across them can't be completed
uint64_t m_acquisition = 0; //!< OFDM acquisitions, and frame count, at its first T2-frame
int m_firstFrame = 0;
PLP m_plp;
std::vector<Cf> m_cells;
std::vector<float> m_scales;
};
static constexpr size_t m_maxQueuedFrames = 4;
static constexpr int m_defaultMaxIterations = 50; //!< LDPC. 0.2 dB better than 25 near the threshold, 100 only 0.05 dB more
static constexpr float m_outlierThreshold = 16.0f; //!< Error power of a cell, relative to the noise per dimension, beyond which it's limited
static constexpr double m_clockCorrectionThreshold = 1.0; //!< ppm, to start resampling. 32K symbols are long enough for 3 ppm to matter
static constexpr uint64_t m_clockCorrectionFrames = 2;
//! MTI (6.5.2): an Interleaving Frame over several T2-frames has at most this
static constexpr size_t m_maxTICells = (1 << 19) + (1 << 15);
static double now();
void onSymbol(const OFDMDemod::Symbol& symbol);
void decodeL1();
void addCells(const Cf *cells, const float *scales, int nbCells);
void frameComplete();
struct FEC;
//! An interleaving frame's turn to finish (its BBFRAMEs, statistics and display, in the order the frames were
//! received): taken when needed, and passed on to the next however the frame's decoding ends, so an exception (out
//! of memory) doesn't leave the other worker, reset() and flush() waiting for it forever
class Turn
{
public:
Turn(Receiver& receiver, uint64_t sequence) : m_receiver(receiver), m_sequence(sequence) {}
~Turn();
void take();
private:
Receiver& m_receiver;
uint64_t m_sequence;
bool m_taken = false;
};
void decodeInterleavingFrame(InterleavingFrame& frame, FEC& fec);
void decodeFrame(InterleavingFrame& frame, FEC& fec, Turn& turn);
struct DemapScratch : public LDPCPool::Scratch
{
std::vector<Cf> m_g;
std::vector<float> m_gs, m_re, m_im, m_scales, m_scalesQ, m_errors; //!< m_scalesQ: of rotated cells' imaginary parts
std::vector<float> m_merScales; //!< Each 16th cell's reliability before outlier limiting, for the MER
std::vector<int8_t *> m_blocks; //!< A batch's LLRs, for the LDPC decoder
};
int decodeLDPC(FEC& fec, LDPC& ldpc, int batch, int nbBlocks, DemapScratch& scratch);
void demapBlock(FEC& fec, const PLP& plp, int block, float merThreshold, int displayStep, DemapScratch& scratch,
double& errorPower, int& errorCount);
void decodeBCH(FEC& fec, int first, int nbBlocks);
void decodeBlocks(FEC& fec, int nbBlocks);
void analyseBlock(FEC& fec, int block, const uint8_t *packed);
void worker(int index);
void deliverPackets();
void correctClock();
OFDMDemod m_ofdm;
double m_outputRate;
double m_inputRate;
bool m_resampling;
dvbt::Resampler m_resampler;
std::atomic<double> m_ratioCorrection{1.0}; //!< The resampler's, for getStatus() in another thread
dvbt::CWCanceller m_canceller;
std::vector<Cf> m_notched;
std::vector<double> m_notchFrequencies; //!< With m_ofdmMutex
struct NotchChange
{
int64_t m_sample; //!< The first sample notched with them
std::vector<double> m_frequencies;
};
std::deque<NotchChange> m_notchChanges; //!< With m_ofdmMutex
std::vector<double> m_notchesQueued; //!< The last in m_notchChanges, or given the demodulator
std::vector<Cf> m_resampled;
std::vector<Cf> m_lastInput; //!< To start resampling without a gap
uint64_t m_clockFrames; //!< Frames when the clock was last corrected
bool m_clockMeasured; //!< Corrected by a measurement since the reset, so later ones are tracked
L1Codec m_l1Codec;
PacketCallback m_packetCallback;
OFDMDemod::LogCallback m_log;
std::atomic<int> m_selectedPLP; //!< Read by the demodulator thread
Profile m_profile = Profile::Auto;
bool m_threaded;
// Current frame
std::vector<std::vector<Cf>> m_p2Cells;
std::vector<std::vector<float>> m_p2Scales;
bool m_frameValid;
L1Pre m_pre;
L1Post m_post;
PLP m_plp;
int m_address; //!< Of the next cell of the frame
std::vector<std::pair<int, int>> m_ranges; //!< Cell address ranges [start, end) of the PLP in this frame
size_t m_rangeCursor = 0; //!< The first of them not yet passed
InterleavingFrame m_current;
// An Interleaving Frame over PI T2-frames (TIME_IL_TYPE 1) is collected from consecutive ones carrying the PLP
int m_symbolFrame = 0; //!< OFDM frame count of the T2-frame being received
int m_currentParts = 0; //!< Of m_current's PI, received
int m_currentFrame = 0; //!< OFDM frame count of the last
uint64_t m_lastAcquisition = ~0ULL; //!< Of the first T2-frame of the last Interleaving Frame decoded
int m_lastFirstFrame = 0;
int m_lastPLPId = -1;
// Error correction, one for each worker
struct FEC
{
FEC() : m_type(FECType::Normal), m_rate(CodeRate::R1_2), m_constellation(Constellation::QPSK), m_analysisRotation(false) {}
FECType m_type;
CodeRate m_rate;
Constellation m_constellation;
LDPCPool::Context m_context; //!< The worker's own decoders and scratch, for the pool
LDPC *m_ldpc = nullptr; //!< Its decoder for the interleaving frame's code
std::unique_ptr<BCH> m_bch;
std::unique_ptr<CellInterleaver> m_cellInterleaver;
std::vector<int> m_bitMap;
std::vector<std::vector<int8_t>> m_llrs; //!< One per FEC block of the interleaving frame
std::vector<Cf> m_cells; //!< Of each FEC block in turn, time de-interleaved
std::vector<float> m_scales;
std::vector<int> m_blockR; //!< Each block's index in its TI block, for the cell interleaver
std::vector<std::vector<Cf>> m_blockDisplay; //!< Cells of each block to display
std::vector<std::vector<uint8_t>> m_packed; //!< Hard decisions of each block's BCH codeword, corrected
std::vector<int> m_corrected; //!< Bits BCH corrected in each block, -1 if it couldn't
std::vector<int> m_iterations; //!< Per batch
std::vector<int> m_unconverged; //!< Per batch, the blocks LDPC didn't converge on
// The settings for the interleaving frame being decoded, as they were when it started
int m_maxIterations = 50;
bool m_outlierLimiting = true;
bool m_analysis = false;
bool m_analysisRotation;
std::vector<std::vector<Cf>> m_analysisCells; //!< Of each block, de-interleaved, before demapping
std::vector<std::vector<float>> m_analysisScales;
std::vector<Cf> m_displayCells; //!< Some of the PLP's cells, ready to demap
};
std::vector<std::unique_ptr<FEC>> m_fecs;
int m_workers;
// Decoder threads, shared by the workers: each worker's batches of FEC blocks, each demapped, LDPC and BCH decoded
// by one thread, oldest first, a batch at a time as they finish one, so a batch that takes longer (more iterations)
// doesn't hold up the rest, and while a worker de-interleaves or delivers a frame, the other's batches use them all.
// The workers decode their own batches too
std::unique_ptr<LDPCPool> m_pool;
// Settings, which may be changed from other threads. Each interleaving frame's FEC uses them as they were at its start
std::atomic<int> m_maxIterations;
std::atomic<bool> m_analysis;
std::atomic<bool> m_outlierLimiting;
BBFrameDecoder m_bbDecoder; //!< Used by one worker at a time, in frame order
std::vector<uint8_t> m_packets; //!< Decoded, for the caller's thread
std::vector<uint8_t> m_bbPackets; //!< From m_bbDecoder, added to m_packets a frame at a time
std::vector<uint8_t> m_delivered; //!< Being delivered, in the caller's thread
// Workers
std::vector<std::thread> m_threads;
std::mutex m_mutex;
std::condition_variable m_frameAvailable;
std::condition_variable m_idle;
std::condition_variable m_turn;
std::deque<InterleavingFrame> m_queue;
std::vector<InterleavingFrame> m_freeFrames; //!< Decoded, so their buffers can be reused without page faults
int m_busy; //!< Workers decoding
uint64_t m_nextSequence; //!< Of the next interleaving frame queued
uint64_t m_finishSequence; //!< Of the next to finish
bool m_stop;
std::mutex m_statusMutex;
Status m_status;
mutable std::mutex m_displayMutex;
std::vector<Cf> m_ofdmCells; //!< From the demodulator, for display until the PLP's cells are known
std::vector<float> m_ofdmResponse;
ImpulseResponse m_ofdmImpulse;
mutable bool m_displayRequested = false;
mutable int m_displayMaxCells = 0;
mutable int m_displayPoints = 0;
// Demodulation, in its own thread if threaded, with what's needed of it from other threads
void demodulate(const Cf *samples, int nbSamples);
OFDMDemod::Status ofdmStatus() const;
void resetDemodulator();
std::unique_ptr<SampleThread> m_demodThread;
mutable std::mutex m_ofdmMutex;
OFDMDemod::Status m_ofdmStatus;
// Clock corrections reach the demodulator with the samples they apply to, so its estimate follows them
struct ClockAdjustment
{
int64_t m_sample; //!< From this sample pushed to the demodulator
double m_ppm;
};
int64_t m_samplesPushed = 0;
int64_t m_samplesDemodulated = 0;
std::deque<ClockAdjustment> m_clockAdjustments; //!< With m_ofdmMutex
uint64_t m_clockLandedFrames = 0; //!< OFDM frames when the last of them reached the demodulator. With m_ofdmMutex
void setRatioCorrection(double ratio);
std::vector<Cf> m_displayCells; //!< From the last interleaving frame decoded
};
} // namespace dvbt2
#endif // MODEMDVB_DVBT2RECEIVER_H

278
modemdvb/dvbt2tables.cpp

@ -0,0 +1,278 @@ @@ -0,0 +1,278 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// Tables from ETSI EN 302 755 V1.4.1. Generated: continual pilots from annex G, reserved carriers from annex H,
// the frame level PN sequence from table 56 and the P1 carriers and modulation patterns from tables 68 and 69
#include "dvbt2tables.h"
namespace dvbt2 {
namespace tables {
const int continualPilots[1148] = {
116, 255, 285, 430, 518, 546, 601, 646, 744, 1662, 1893, 1995, 2322, 3309, 3351, 3567,
3813, 4032, 5568, 5706, 116, 318, 390, 430, 474, 518, 601, 646, 708, 726, 1752, 1758,
1944, 2100, 2208, 2466, 3792, 5322, 5454, 5640, 116, 318, 342, 426, 430, 518, 582, 601,
646, 816, 1758, 1764, 2400, 3450, 3504, 3888, 4020, 4932, 5154, 5250, 5292, 5334, 108, 116,
144, 264, 288, 430, 518, 564, 636, 646, 828, 2184, 3360, 3396, 3912, 4032, 4932, 5220,
5676, 5688, 108, 116, 228, 430, 518, 601, 646, 804, 1644, 1680, 1752, 1800, 1836, 3288,
3660, 4080, 4932, 4968, 5472, 264, 360, 1848, 2088, 2112, 2160, 2256, 2280, 3936, 3960, 3984,
5016, 5136, 5208, 5664, 1022, 1224, 1302, 1371, 1495, 2261, 2551, 2583, 2649, 2833, 2925, 3192,
4266, 5395, 5710, 5881, 8164, 10568, 11069, 11560, 12631, 12946, 13954, 16745, 21494, 1022, 1092, 1369,
1416, 1446, 1495, 2598, 2833, 2928, 3144, 4410, 4800, 5710, 5881, 6018, 6126, 10568, 11515, 12946,
13954, 15559, 16681, 1022, 1495, 2261, 2551, 2802, 2820, 2833, 2922, 4422, 4752, 4884, 5710, 8164,
10568, 11069, 11560, 12631, 12946, 16745, 21494, 601, 1022, 1092, 1164, 1369, 1392, 1452, 1495, 2261,
2580, 2833, 3072, 4320, 4452, 5710, 5881, 6048, 10568, 11515, 12946, 13954, 15559, 16681, 852, 1022,
1495, 2508, 2551, 2604, 2664, 2736, 2833, 3120, 4248, 4512, 4836, 5710, 5940, 6108, 8164, 10568,
11069, 11560, 12946, 13954, 21494, 116, 430, 518, 601, 646, 1022, 1296, 1368, 1369, 1495, 2833,
3024, 4416, 4608, 4776, 5710, 5881, 6168, 7013, 8164, 10568, 10709, 11515, 12946, 15559, 23239, 24934,
25879, 26308, 26674, 2261, 8164, 13954, 8164, 648, 4644, 16745, 456, 480, 2261, 6072, 17500, 10709,
19930, 10709, 19930, 12631, 1008, 6120, 13954, 116, 132, 180, 430, 518, 601, 646, 1022, 1266,
1369, 1495, 2261, 2490, 2551, 2712, 2833, 3372, 3438, 4086, 4098, 4368, 4572, 4614, 4746, 4830,
4968, 5395, 5710, 5881, 7649, 8164, 10568, 11069, 11560, 12631, 12946, 13954, 15760, 16612, 16745, 17500,
19078, 19930, 21494, 22867, 25879, 26308, 1369, 7013, 7215, 7284, 7649, 7818, 8025, 8382, 8733, 8880,
9249, 9432, 9771, 10107, 10110, 10398, 10659, 10709, 10785, 10872, 11115, 11373, 11515, 11649, 11652, 12594,
12627, 12822, 12984, 15760, 16612, 17500, 18358, 19078, 19930, 20261, 20422, 22124, 22867, 23239, 24934, 25879,
26308, 26674, 6744, 7013, 7020, 7122, 7308, 7649, 7674, 7752, 7764, 8154, 8190, 8856, 8922, 9504,
9702, 9882, 9924, 10032, 10092, 10266, 10302, 10494, 10530, 10716, 11016, 11076, 11160, 11286, 11436, 11586,
12582, 13002, 17500, 18358, 19078, 22124, 23239, 24073, 24934, 25879, 26308, 1369, 5395, 5881, 6564, 6684,
7013, 7649, 8376, 8544, 8718, 8856, 9024, 9132, 9498, 9774, 9840, 10302, 10512, 10566, 10770, 10914,
11340, 11418, 11730, 11742, 12180, 12276, 12474, 12486, 15760, 16612, 17500, 18358, 19078, 19930, 20261, 20422,
22124, 22867, 23239, 24934, 25879, 26308, 26674, 6612, 6708, 7013, 7068, 7164, 7224, 7308, 7464, 7649,
7656, 7716, 7752, 7812, 7860, 8568, 8808, 8880, 9072, 9228, 9516, 9696, 9996, 10560, 10608, 10728,
11148, 11232, 11244, 11496, 11520, 11664, 11676, 11724, 11916, 17500, 18358, 19078, 21284, 22124, 23239, 24073,
24934, 25879, 26308, 1369, 2261, 5395, 5881, 6552, 6636, 6744, 6900, 7032, 7296, 7344, 7464, 7644,
7649, 7668, 7956, 8124, 8244, 8904, 8940, 8976, 9216, 9672, 9780, 10224, 10332, 10709, 10776, 10944,
11100, 11292, 11364, 11496, 11532, 11904, 12228, 12372, 12816, 15760, 16612, 17500, 19078, 22867, 25879, 116,
384, 408, 518, 601, 646, 672, 960, 1022, 1272, 1344, 1369, 1495, 1800, 2040, 2261, 2833,
3192, 3240, 3768, 3864, 3984, 4104, 4632, 4728, 4752, 4944, 5184, 5232, 5256, 5376, 5592, 5616,
5710, 5808, 5881, 6360, 6792, 6960, 7013, 7272, 7344, 7392, 7536, 7649, 7680, 7800, 8064, 8160,
8164, 8184, 8400, 8808, 8832, 9144, 9648, 9696, 9912, 10008, 10200, 10488, 10568, 10656, 10709, 11088,
11160, 11515, 11592, 12048, 12264, 12288, 12312, 12552, 12672, 12946, 13954, 15559, 16681, 17500, 19078, 20422,
21284, 22124, 23239, 24934, 25879, 26308, 26674, 6984, 7032, 7056, 7080, 7152, 7320, 7392, 7536, 7649,
7704, 7728, 7752, 8088, 8952, 9240, 9288, 9312, 9480, 9504, 9840, 9960, 10320, 10368, 10728, 10752,
11448, 11640, 11688, 11808, 12192, 12240, 12480, 12816, 16681, 22124, 6720, 6954, 7013, 7026, 7092, 7512,
7536, 7596, 7746, 7758, 7818, 7986, 8160, 8628, 9054, 9096, 9852, 9924, 10146, 10254, 10428, 10704,
11418, 11436, 11496, 11550, 11766, 11862, 12006, 12132, 12216, 12486, 12762, 18358, 20261, 20422, 22124, 23239,
24934, 13164, 13206, 13476, 13530, 13536, 13764, 13848, 13938, 13968, 14028, 14190, 14316, 14526, 14556, 14562,
14658, 14910, 14946, 15048, 15186, 15252, 15468, 15540, 15576, 15630, 15738, 15840, 16350, 16572, 16806, 17028,
17064, 17250, 17472, 17784, 17838, 18180, 18246, 18480, 18900, 18960, 19254, 19482, 19638, 19680, 20082, 20310,
20422, 20454, 20682, 20874, 21240, 21284, 21444, 21450, 21522, 21594, 21648, 21696, 21738, 22416, 22824, 23016,
23124, 23196, 23238, 23316, 23418, 23922, 23940, 24090, 24168, 24222, 24324, 24342, 24378, 24384, 24540, 24744,
24894, 24990, 25002, 25194, 25218, 25260, 25566, 26674, 26944, 13080, 13152, 13260, 13380, 13428, 13572, 13884,
13956, 14004, 14016, 14088, 14232, 14304, 14532, 14568, 14760, 14940, 15168, 15288, 15612, 15684, 15888, 16236,
16320, 16428, 16680, 16812, 16908, 17184, 17472, 17508, 17580, 17892, 17988, 18000, 18336, 18480, 18516, 19020,
19176, 19188, 19320, 19776, 19848, 20112, 20124, 20184, 20388, 20532, 20556, 20676, 20772, 21156, 21240, 21276,
21336, 21384, 21816, 21888, 22068, 22092, 22512, 22680, 22740, 22800, 22836, 22884, 23304, 23496, 23568, 23640,
24120, 24168, 24420, 24444, 24456, 24492, 24708, 24864, 25332, 25536, 25764, 25992, 26004, 26674, 26944, 13080,
13368, 13464, 13536, 13656, 13728, 13824, 14112, 14232, 14448, 14472, 14712, 14808, 14952, 15000, 15336, 15360,
15408, 15600, 15624, 15648, 16128, 16296, 16320, 16416, 16536, 16632, 16824, 16848, 17184, 17208, 17280, 17352,
17520, 17664, 17736, 17784, 18048, 18768, 18816, 18840, 19296, 19392, 19584, 19728, 19752, 19776, 20136, 20184,
20208, 20256, 21096, 21216, 21360, 21408, 21744, 21768, 22200, 22224, 22320, 22344, 22416, 22848, 22968, 23016,
23040, 23496, 23688, 23904, 24048, 24168, 24360, 24408, 24984, 25152, 25176, 25224, 25272, 25344, 25416, 25488,
25512, 25536, 25656, 25680, 25752, 25992, 26016, 13416, 13440, 13536, 13608, 13704, 13752, 14016, 14040, 14112,
14208, 14304, 14376, 14448, 14616, 14712, 14760, 14832, 14976, 15096, 15312, 15336, 15552, 15816, 15984, 16224,
16464, 16560, 17088, 17136, 17256, 17352, 17400, 17448, 17544, 17928, 18048, 18336, 18456, 18576, 18864, 19032,
19078, 19104, 19320, 19344, 19416, 19488, 19920, 19930, 19992, 20424, 20664, 20808, 21168, 21284, 21360, 21456,
21816, 22128, 22200, 22584, 22608, 22824, 22848, 22944, 22992, 23016, 23064, 23424, 23448, 23472, 23592, 24192,
24312, 24360, 24504, 24552, 24624, 24648, 24672, 24768, 24792, 25080, 25176, 25224, 25320, 25344, 25584, 25680,
25824, 26064, 26944, 10709, 11515, 13254, 13440, 13614, 13818, 14166, 14274, 14304, 14364, 14586, 14664, 15030,
15300, 15468, 15474, 15559, 15732, 15774, 16272, 16302, 16428, 16500, 16662, 16681, 16872, 17112, 17208, 17862,
18036, 18282, 18342, 18396, 18420, 18426, 18732, 19050, 19296, 19434, 19602, 19668, 19686, 19728, 19938, 20034,
21042, 21120, 21168, 21258, 21284, 21528, 21594, 21678, 21930, 21936, 21990, 22290, 22632, 22788, 23052, 23358,
23448, 23454, 23706, 23772, 24048, 24072, 24073, 24222, 24384, 24402, 24444, 24462, 24600, 24738, 24804, 24840,
24918, 24996, 25038, 25164, 25314, 25380, 25470, 25974, 26076, 26674, 26753, 26944,
};
// Offset and count into continualPilots, for group 1-6 (row) and PP1-PP8 (column)
const int continualPilotIndex[6][8][2] = {
{ {0, 20}, {20, 20}, {40, 22}, {62, 20}, {82, 19}, {101, 0}, {101, 15}, {116, 0} },
{ {116, 25}, {141, 22}, {163, 20}, {183, 23}, {206, 23}, {229, 0}, {229, 30}, {259, 0} },
{ {259, 0}, {259, 2}, {261, 1}, {262, 1}, {263, 3}, {266, 0}, {266, 5}, {271, 0} },
{ {271, 0}, {271, 2}, {273, 0}, {273, 2}, {275, 1}, {276, 0}, {276, 3}, {279, 47} },
{ {326, 44}, {370, 41}, {411, 44}, {455, 44}, {499, 44}, {543, 88}, {631, 35}, {666, 39} },
{ {705, 0}, {705, 88}, {793, 0}, {793, 86}, {879, 0}, {879, 88}, {967, 92}, {1059, 89} },
};
const int extendedContinualPilots[54] = {
6820, 6847, 6869, 6898, 6820, 6869, 6820, 6869, 6820, 6833, 6869, 6887, 6898, 6820, 6833, 6869,
6887, 6898, 13636, 13724, 13790, 13879, 13636, 13790, 13636, 13790, 13636, 13790, 13636, 13790, 13636, 13790,
13636, 13724, 13879, 13636, 13724, 13879, 27268, 27688, 27268, 27688, 27268, 27448, 27688, 27758, 27268, 27688,
27268, 27368, 27448, 27580, 27688, 27758,
};
// Offset and count into extendedContinualPilots, for 8K, 16K and 32K (row) and PP1-PP8 (column)
const int extendedContinualPilotIndex[3][8][2] = {
{ {0, 0}, {0, 4}, {4, 2}, {6, 2}, {8, 0}, {8, 0}, {8, 5}, {13, 5} },
{ {18, 4}, {22, 2}, {24, 2}, {26, 2}, {28, 2}, {30, 2}, {32, 3}, {35, 3} },
{ {38, 0}, {38, 2}, {40, 0}, {40, 2}, {42, 0}, {42, 4}, {46, 2}, {48, 6} },
};
const int p2ReservedCarriers[568] = {
116, 130, 134, 157, 182, 256, 346, 478, 479, 532, 113, 124, 262, 467, 479, 727,
803, 862, 910, 946, 980, 1201, 1322, 1342, 1396, 1397, 1562, 1565, 104, 116, 119, 163,
170, 173, 664, 886, 1064, 1151, 1196, 1264, 1531, 1736, 1951, 1960, 2069, 2098, 2311, 2366,
2473, 2552, 2584, 2585, 2645, 2774, 2846, 2882, 3004, 3034, 3107, 3127, 3148, 3191, 3283, 3289,
106, 109, 110, 112, 115, 118, 133, 142, 163, 184, 206, 247, 445, 461, 503, 565,
602, 656, 766, 800, 922, 1094, 1108, 1199, 1258, 1726, 1793, 1939, 2128, 2714, 3185, 3365,
3541, 3655, 3770, 3863, 4066, 4190, 4282, 4565, 4628, 4727, 4882, 4885, 5143, 5192, 5210, 5257,
5261, 5459, 5651, 5809, 5830, 5986, 6020, 6076, 6253, 6269, 6410, 6436, 6467, 6475, 6509, 6556,
6611, 6674, 6685, 6689, 6691, 6695, 6698, 6701, 104, 106, 107, 109, 110, 112, 113, 115,
116, 118, 119, 121, 122, 125, 128, 131, 134, 137, 140, 143, 161, 223, 230, 398,
482, 497, 733, 809, 850, 922, 962, 1196, 1256, 1262, 1559, 1691, 1801, 1819, 1937, 2005,
2095, 2308, 2383, 2408, 2425, 2428, 2479, 2579, 2893, 2902, 3086, 3554, 4085, 4127, 4139, 4151,
4163, 4373, 4400, 4576, 4609, 4952, 4961, 5444, 5756, 5800, 6094, 6208, 6658, 6673, 6799, 7208,
7682, 8101, 8135, 8230, 8692, 8788, 8933, 9323, 9449, 9478, 9868, 10192, 10261, 10430, 10630, 10685,
10828, 10915, 10930, 10942, 11053, 11185, 11324, 11369, 11468, 11507, 11542, 11561, 11794, 11912, 11974, 11978,
12085, 12179, 12193, 12269, 12311, 12758, 12767, 12866, 12938, 12962, 12971, 13099, 13102, 13105, 13120, 13150,
13280, 13282, 13309, 13312, 13321, 13381, 13402, 13448, 13456, 13462, 13463, 13466, 13478, 13492, 13495, 13498,
13501, 13502, 13504, 13507, 13510, 13513, 13514, 13516, 104, 106, 107, 109, 110, 112, 113, 115,
118, 121, 124, 127, 130, 133, 136, 139, 142, 145, 148, 151, 154, 157, 160, 163,
166, 169, 172, 175, 178, 181, 184, 187, 190, 193, 196, 199, 202, 205, 208, 211,
404, 452, 455, 467, 509, 539, 568, 650, 749, 1001, 1087, 1286, 1637, 1823, 1835, 1841,
1889, 1898, 1901, 2111, 2225, 2252, 2279, 2309, 2315, 2428, 2452, 2497, 2519, 3109, 3154, 3160,
3170, 3193, 3214, 3298, 3331, 3346, 3388, 3397, 3404, 3416, 3466, 3491, 3500, 3572, 4181, 4411,
4594, 4970, 5042, 5069, 5081, 5086, 5095, 5104, 5320, 5465, 5491, 6193, 6541, 6778, 6853, 6928,
6934, 7030, 7198, 7351, 7712, 7826, 7922, 8194, 8347, 8350, 8435, 8518, 8671, 8861, 8887, 9199,
9980, 10031, 10240, 10519, 10537, 10573, 10589, 11078, 11278, 11324, 11489, 11642, 12034, 12107, 12184, 12295,
12635, 12643, 12941, 12995, 13001, 13133, 13172, 13246, 13514, 13522, 13939, 14362, 14720, 14926, 15338, 15524,
15565, 15662, 15775, 16358, 16613, 16688, 16760, 17003, 17267, 17596, 17705, 18157, 18272, 18715, 18994, 19249,
19348, 20221, 20855, 21400, 21412, 21418, 21430, 21478, 21559, 21983, 21986, 22331, 22367, 22370, 22402, 22447,
22535, 22567, 22571, 22660, 22780, 22802, 22844, 22888, 22907, 23021, 23057, 23086, 23213, 23240, 23263, 23333,
23369, 23453, 23594, 24143, 24176, 24319, 24325, 24565, 24587, 24641, 24965, 25067, 25094, 25142, 25331, 25379,
25465, 25553, 25589, 25594, 25655, 25664, 25807, 25823, 25873, 25925, 25948, 26002, 26008, 26102, 26138, 26141,
26377, 26468, 26498, 26510, 26512, 26578, 26579, 26588, 26594, 26597, 26608, 26627, 26642, 26767, 26776, 26800,
26876, 26882, 26900, 26917, 26927, 26951, 26957, 26960, 26974, 26986, 27010, 27013, 27038, 27044, 27053, 27059,
27061, 27074, 27076, 27083, 27086, 27092, 27094, 27098, 27103, 27110, 27115, 27118, 27119, 27125, 27128, 27130,
27133, 27134, 27140, 27143, 27145, 27146, 27148, 27149,
};
// Offset and count into p2ReservedCarriers, for 1K to 32K
const int p2ReservedCarriersIndex[6][2] = { {0, 10}, {10, 18}, {28, 36}, {64, 72}, {136, 144}, {280, 288} };
const int reservedCarriers[568] = {
109, 117, 122, 129, 139, 321, 350, 403, 459, 465, 250, 404, 638, 677, 700, 712,
755, 952, 1125, 1145, 1190, 1276, 1325, 1335, 1406, 1431, 1472, 1481, 170, 219, 405, 501,
597, 654, 661, 745, 995, 1025, 1319, 1361, 1394, 1623, 1658, 1913, 1961, 1971, 2106, 2117,
2222, 2228, 2246, 2254, 2361, 2468, 2469, 2482, 2637, 2679, 2708, 2825, 2915, 2996, 3033, 3119,
111, 115, 123, 215, 229, 392, 613, 658, 831, 842, 997, 1503, 1626, 1916, 1924, 1961,
2233, 2246, 2302, 2331, 2778, 2822, 2913, 2927, 2963, 2994, 3087, 3162, 3226, 3270, 3503, 3585,
3711, 3738, 3874, 3902, 4013, 4017, 4186, 4253, 4292, 4339, 4412, 4453, 4669, 4910, 5015, 5030,
5061, 5170, 5263, 5313, 5360, 5384, 5394, 5493, 5550, 5847, 5901, 5999, 6020, 6165, 6174, 6227,
6245, 6314, 6316, 6327, 6503, 6507, 6545, 6565, 109, 122, 139, 171, 213, 214, 251, 585,
763, 1012, 1021, 1077, 1148, 1472, 1792, 1883, 1889, 1895, 1900, 2013, 2311, 2582, 2860, 2980,
3011, 3099, 3143, 3171, 3197, 3243, 3257, 3270, 3315, 3436, 3470, 3582, 3681, 3712, 3767, 3802,
3979, 4045, 4112, 4197, 4409, 4462, 4756, 5003, 5007, 5036, 5246, 5483, 5535, 5584, 5787, 5789,
6047, 6349, 6392, 6498, 6526, 6542, 6591, 6680, 6688, 6785, 6860, 7134, 7286, 7387, 7415, 7417,
7505, 7526, 7541, 7551, 7556, 7747, 7814, 7861, 7880, 8045, 8179, 8374, 8451, 8514, 8684, 8698,
8804, 8924, 9027, 9113, 9211, 9330, 9479, 9482, 9487, 9619, 9829, 10326, 10394, 10407, 10450, 10528,
10671, 10746, 10774, 10799, 10801, 10912, 11113, 11128, 11205, 11379, 11459, 11468, 11658, 11776, 11791, 11953,
11959, 12021, 12028, 12135, 12233, 12407, 12441, 12448, 12470, 12501, 12548, 12642, 12679, 12770, 12788, 12899,
12923, 12939, 13050, 13103, 13147, 13256, 13339, 13409, 164, 320, 350, 521, 527, 578, 590, 619,
635, 651, 662, 664, 676, 691, 723, 940, 1280, 1326, 1509, 1520, 1638, 1682, 1805, 1833,
1861, 1891, 1900, 1902, 1949, 1967, 1978, 1998, 2006, 2087, 2134, 2165, 2212, 2427, 2475, 2555,
2874, 3067, 3091, 3101, 3146, 3188, 3322, 3353, 3383, 3503, 3523, 3654, 3856, 4150, 4158, 4159,
4174, 4206, 4318, 4417, 4629, 4631, 4875, 5104, 5106, 5111, 5131, 5145, 5146, 5177, 5181, 5246,
5269, 5458, 5474, 5500, 5509, 5579, 5810, 5823, 6058, 6066, 6098, 6411, 6741, 6775, 6932, 7103,
7258, 7303, 7413, 7586, 7591, 7634, 7636, 7655, 7671, 7675, 7756, 7760, 7826, 7931, 7937, 7951,
8017, 8061, 8071, 8117, 8317, 8321, 8353, 8806, 9010, 9237, 9427, 9453, 9469, 9525, 9558, 9574,
9584, 9820, 9973, 10011, 10043, 10064, 10066, 10081, 10136, 10193, 10249, 10511, 10537, 11083, 11350, 11369,
11428, 11622, 11720, 11924, 11974, 11979, 12944, 12945, 13009, 13070, 13110, 13257, 13364, 13370, 13449, 13503,
13514, 13520, 13583, 13593, 13708, 13925, 14192, 14228, 14235, 14279, 14284, 14370, 14393, 14407, 14422, 14471,
14494, 14536, 14617, 14829, 14915, 15094, 15138, 15155, 15170, 15260, 15283, 15435, 15594, 15634, 15810, 16178,
16192, 16196, 16297, 16366, 16498, 16501, 16861, 16966, 17039, 17057, 17240, 17523, 17767, 18094, 18130, 18218,
18344, 18374, 18657, 18679, 18746, 18772, 18779, 18786, 18874, 18884, 18955, 19143, 19497, 19534, 19679, 19729,
19738, 19751, 19910, 19913, 20144, 20188, 20194, 20359, 20490, 20500, 20555, 20594, 20633, 20656, 21099, 21115,
21597, 22139, 22208, 22244, 22530, 22547, 22562, 22567, 22696, 22757, 22798, 22854, 22877, 23068, 23102, 23141,
23154, 23170, 23202, 23368, 23864, 24057, 24215, 24219, 24257, 24271, 24325, 24447, 25137, 25590, 25702, 25706,
25744, 25763, 25811, 25842, 25853, 25954, 26079, 26158, 26285, 26346, 26488, 26598, 26812, 26845, 26852, 26869,
26898, 26909, 26927, 26931, 26946, 26975, 26991, 27039,
};
// Offset and count into reservedCarriers, for 1K to 32K
const int reservedCarriersIndex[6][2] = { {0, 10}, {10, 18}, {28, 36}, {64, 72}, {136, 144}, {280, 288} };
const char frameLevelPN[] =
"4DC2AF7BD8C3C9A1E76C9A090AF1C3114F07FCA2808E9462E9AD7B712D6F4AC8A59BB069CC50BF1149927E6BB1C9FC8C"
"18BB949B30CD09DDD749E704F57B41DEC7E7B176E12C5657432B51B0B812DF0E14887E24D80C97F09374AD76270E58FE"
"1774B2781D8D3821E393F2EA0FFD4D24DE20C05D0BA1703D10E52D61E013D837AA62D007CC2FD76D23A3E125BDE8A9A7"
"C02A98B70251C556F6341EBDECB801AAD5D9FB8CBEA80BB619096527A8C475B3D8DB28AF8543A00EC3480DFF1E2CDA9F"
"985B523B879007AA5D0CE58D21B18631006617F6F769EB947F924EA5161EC2C0488B63ED7993BA8EF4E552FA32FC3F1B"
"DB19923902BCBBE5DDABB824126E08459CA6CFA0267E5294A98C632569791E60EF659AEE9518CDF08D87833690C1B791"
"83ED127E53360CD86514859A28B5494F51AA4882419A25A2D01A5F47AA27301E79A5370CCB3E197F"
;
const int p1Carriers[384] = {
44, 45, 47, 51, 54, 59, 62, 64, 65, 66, 70, 75, 78, 80, 81, 82,
84, 85, 87, 88, 89, 90, 94, 96, 97, 98, 102, 107, 110, 112, 113, 114,
116, 117, 119, 120, 121, 122, 124, 125, 127, 131, 132, 133, 135, 136, 137, 138,
142, 144, 145, 146, 148, 149, 151, 152, 153, 154, 158, 160, 161, 162, 166, 171,
172, 173, 175, 179, 182, 187, 190, 192, 193, 194, 198, 203, 206, 208, 209, 210,
212, 213, 215, 216, 217, 218, 222, 224, 225, 226, 230, 235, 238, 240, 241, 242,
244, 245, 247, 248, 249, 250, 252, 253, 255, 259, 260, 261, 263, 264, 265, 266,
270, 272, 273, 274, 276, 277, 279, 280, 281, 282, 286, 288, 289, 290, 294, 299,
300, 301, 303, 307, 310, 315, 318, 320, 321, 322, 326, 331, 334, 336, 337, 338,
340, 341, 343, 344, 345, 346, 350, 352, 353, 354, 358, 363, 364, 365, 367, 371,
374, 379, 382, 384, 385, 386, 390, 395, 396, 397, 399, 403, 406, 411, 412, 413,
415, 419, 420, 421, 423, 424, 425, 426, 428, 429, 431, 435, 438, 443, 446, 448,
449, 450, 454, 459, 462, 464, 465, 466, 468, 469, 471, 472, 473, 474, 478, 480,
481, 482, 486, 491, 494, 496, 497, 498, 500, 501, 503, 504, 505, 506, 508, 509,
511, 515, 516, 517, 519, 520, 521, 522, 526, 528, 529, 530, 532, 533, 535, 536,
537, 538, 542, 544, 545, 546, 550, 555, 558, 560, 561, 562, 564, 565, 567, 568,
569, 570, 572, 573, 575, 579, 580, 581, 583, 584, 585, 586, 588, 589, 591, 595,
598, 603, 604, 605, 607, 611, 612, 613, 615, 616, 617, 618, 622, 624, 625, 626,
628, 629, 631, 632, 633, 634, 636, 637, 639, 643, 644, 645, 647, 648, 649, 650,
654, 656, 657, 658, 660, 661, 663, 664, 665, 666, 670, 672, 673, 674, 678, 683,
684, 689, 692, 696, 698, 699, 701, 702, 703, 704, 706, 707, 708, 712, 714, 715,
717, 718, 719, 720, 722, 723, 725, 726, 727, 729, 733, 734, 735, 736, 738, 739,
740, 744, 746, 747, 748, 753, 756, 760, 762, 763, 765, 766, 767, 768, 770, 771,
772, 776, 778, 779, 780, 785, 788, 792, 794, 795, 796, 801, 805, 806, 807, 809,
};
const char *const p1S1[8] = {
"124721741D482E7B",
"47127421481D7B2E",
"217412472E7B1D48",
"742147127B2E481D",
"1D482E7B12472174",
"481D7B2E47127421",
"2E7B1D4821741247",
"7B2E481D74214712",
};
const char *const p1S2[16] = {
"121D4748212E747B1D1248472E217B7412E247B721D174841DED48B82EDE7B8B",
"4748121D747B212E48471D127B742E2147B712E2748421D148B81DED7B8B2EDE",
"212E747B121D47482E217B741D12484721D1748412E247B72EDE7B8B1DED48B8",
"747B212E4748121D7B742E2148471D12748421D147B712E27B8B2EDE48B81DED",
"1D1248472E217B74121D4748212E747B1DED48B82EDE7B8B12E247B721D17484",
"48471D127B742E214748121D747B212E48B81DED7B8B2EDE47B712E2748421D1",
"2E217B741D124847212E747B121D47482EDE7B8B1DED48B821D1748412E247B7",
"7B742E2148471D12747B212E4748121D7B8B2EDE48B81DED748421D147B712E2",
"12E247B721D174841DED48B82EDE7B8B121D4748212E747B1D1248472E217B74",
"47B712E2748421D148B81DED7B8B2EDE4748121D747B212E48471D127B742E21",
"21D1748412E247B72EDE7B8B1DED48B8212E747B121D47482E217B741D124847",
"748421D147B712E27B8B2EDE48B81DED747B212E4748121D7B742E2148471D12",
"1DED48B82EDE7B8B12E247B721D174841D1248472E217B74121D4748212E747B",
"48B81DED7B8B2EDE47B712E2748421D148471D127B742E214748121D747B212E",
"2EDE7B8B1DED48B821D1748412E247B72E217B741D124847212E747B121D4748",
"7B8B2EDE48B81DED748421D147B712E27B742E2148471D12747B212E4748121D",
};
} // namespace tables
} // namespace dvbt2

43
modemdvb/dvbt2tables.h

@ -0,0 +1,43 @@ @@ -0,0 +1,43 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBT2TABLES_H
#define MODEMDVB_DVBT2TABLES_H
// Raw tables from EN 302 755 V1.4.1, used to build dvbt2::Tables
namespace dvbt2 {
namespace tables {
extern const int continualPilots[]; //!< Annex G table G.1, 32K carrier indices
extern const int continualPilotIndex[6][8][2]; //!< [CP group 1-6][PP1-PP8] = {offset, count}
extern const int extendedContinualPilots[]; //!< Annex G table G.2
extern const int extendedContinualPilotIndex[3][8][2]; //!< [8K, 16K, 32K][PP1-PP8] = {offset, count}
extern const int p2ReservedCarriers[]; //!< Annex H table H.1
extern const int p2ReservedCarriersIndex[6][2]; //!< [1K-32K] = {offset, count}
extern const int reservedCarriers[]; //!< Annex H table H.2
extern const int reservedCarriersIndex[6][2]; //!< [1K-32K] = {offset, count}
extern const char frameLevelPN[]; //!< Table 56, hexadecimal, MSB first
extern const int p1Carriers[384]; //!< Table 68
extern const char *const p1S1[8]; //!< Table 69, hexadecimal, MSB first
extern const char *const p1S2[16];
} // namespace tables
} // namespace dvbt2
#endif // MODEMDVB_DVBT2TABLES_H

489
modemdvb/dvbtfec.cpp

@ -0,0 +1,489 @@ @@ -0,0 +1,489 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <chrono>
#include "dvbtfec.h"
namespace dvbt {
double FECDecoder::now()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
FECDecoder::FECDecoder() :
m_configured(false),
m_demapSupported(true),
m_supported(true),
m_mer(0.0),
m_encoderState(0),
m_ber(0.0),
m_berValid(false),
m_berErrors(0),
m_berTotal(0),
m_berCalls(0),
m_postBER(0.0),
m_postBERPackets(0),
m_postBERBits(0),
m_viterbiBits(0),
m_seconds{0.0, 0.0, 0.0, 0.0}
{
}
FECDecoder::~FECDecoder()
{
stopViterbiThreads();
}
void FECDecoder::reset()
{
resetDemap();
resetDecode();
}
void FECDecoder::resetDemap()
{
m_configured = false;
m_demapSupported = true;
}
void FECDecoder::resetDecode()
{
m_viterbi.reset();
m_stream.clear();
m_streamStart = 0;
m_streamDecoded = 0;
m_outer.reset();
m_supported = true;
m_hardDecisions.clear();
m_encoderState = 0;
m_ber = 0.0;
m_berValid = false;
m_berErrors = 0;
m_berTotal = 0;
m_postBER = 0.0;
m_postBERPackets = m_outer.getStats().m_packets;
m_postBERBits = m_outer.getStats().m_rsBitsCorrected;
}
void FECDecoder::decode(int symbolIndex, const TPS& tps, const Cf *cells, const float *csi, int nbCells)
{
demap(symbolIndex, tps, cells, csi, nbCells, m_demapped);
decodeDemapped(m_demapped);
}
void FECDecoder::demap(int symbolIndex, const TPS& tps, const Cf *cells, const float *csi, int nbCells, Demapped& out)
{
out.m_reconfigured = false;
// Cells not for the mode signalled (as before the demodulator acquires a new mode): skipped, with no soft bits,
// and the next symbol that has the right number starts decoding again
if (nbCells != dataCarriers(tps.m_mode))
{
m_configured = false;
out.m_supported = m_demapSupported;
out.m_llrs.clear();
out.m_seconds = 0.0;
return;
}
const bool lowPriority = m_lowPriority;
if (!m_configured || !tps.sameParameters(m_tps) || (lowPriority != m_configuredLowPriority))
{
// In hierarchical modes, the stream chosen, with its code rate
const bool hierarchical = (tps.m_hierarchy != Hierarchy::None) && (tps.m_constellation != Constellation::QPSK);
const bool lp = hierarchical && lowPriority;
m_tps = tps;
m_configured = true;
m_configuredLowPriority = lowPriority;
// DVB-H's in-depth interleaver isn't, in 2K. In 8K only the native one is allowed (F.3.4.2.2), so the flag
// is ignored
m_demapSupported = !(tps.m_inDepth && (tps.m_mode == Mode::Mode2K));
m_inner.configure(tps.m_mode, tps.m_constellation, lp ? tps.m_lpCodeRate : tps.m_hpCodeRate, tps.m_hierarchy, lp);
out.m_reconfigured = true;
}
out.m_supported = m_demapSupported;
out.m_llrs.clear();
out.m_seconds = 0.0;
if (!m_demapSupported) {
return;
}
double t0 = now();
m_inner.decode(symbolIndex, cells, csi, out.m_llrs);
out.m_mer = m_inner.getMER();
out.m_seconds = now() - t0;
}
void FECDecoder::decodeDemapped(const Demapped& in)
{
if (in.m_reconfigured)
{
m_viterbi.reset();
m_stream.clear();
m_streamStart = 0;
m_streamDecoded = 0;
m_outer.reset();
m_postBERPackets = m_outer.getStats().m_packets;
m_postBERBits = m_outer.getStats().m_rsBitsCorrected;
m_hardDecisions.clear();
m_encoderState = 0;
}
m_supported = in.m_supported;
if (!m_supported) {
return;
}
m_mer = in.m_mer;
double t1 = now();
m_bits.clear();
int nbInfoBits = (int) in.m_llrs.size() / 2;
const int threads = m_viterbiThreadsWanted;
if (threads != m_viterbiThreads)
{
// Seamlessly: the stream's last steps are kept either way, so the chunks continue from what the single
// decoder output, or it starts from 192 steps before what they output (bits it outputs again)
const bool toSingle = (threads <= 0) && (m_viterbiThreads > 0);
stopViterbiThreads();
startViterbiThreads(threads);
if (toSingle)
{
static const int roll = Viterbi27::m_tracebackLength;
const int64_t from = std::max(m_streamStart, m_streamDecoded - roll);
const int64_t end = m_streamStart + (int64_t) m_stream.size() / 2;
std::vector<uint8_t> again;
m_viterbi.reset();
m_viterbi.decode(m_stream.data() + 2 * (from - m_streamStart), (int) (end - from), again);
}
}
if (m_viterbiThreads > 0)
{
viterbiParallel(in.m_llrs.data(), nbInfoBits, m_bits);
}
else
{
m_viterbi.decode(in.m_llrs.data(), nbInfoBits, m_bits);
// Its last steps, in case Viterbi threads are added: those from keep, normally all in this call's soft bits,
// so only they are copied (not the whole symbol's, 48 kB in 8K)
static const int roll = Viterbi27::m_tracebackLength;
const int64_t inputStart = m_streamStart + (int64_t) m_stream.size() / 2;
const int64_t end = inputStart + nbInfoBits;
m_streamDecoded += (int64_t) m_bits.size();
const int64_t keep = std::max(m_streamStart, std::min(m_streamDecoded - roll, end));
if (keep >= inputStart)
{
m_stream.assign(in.m_llrs.begin() + 2 * (keep - inputStart), in.m_llrs.begin() + 2 * nbInfoBits);
}
else
{
m_stream.erase(m_stream.begin(), m_stream.begin() + 2 * (keep - m_streamStart));
m_stream.insert(m_stream.end(), in.m_llrs.begin(), in.m_llrs.begin() + 2 * nbInfoBits);
}
m_streamStart = keep;
}
m_viterbiBits += m_bits.size();
double t2 = now();
measureBER(in.m_llrs.data(), nbInfoBits, m_bits.data(), (int) m_bits.size());
double t3 = now();
m_outer.decode(m_bits.data(), (int) m_bits.size());
measurePostViterbiBER();
double t4 = now();
m_seconds[0] += in.m_seconds;
m_seconds[1] += t2 - t1;
m_seconds[2] += t3 - t2;
m_seconds[3] += t4 - t3;
}
void FECDecoder::startViterbiThreads(int threads)
{
m_viterbiThreads = std::max(0, std::min(threads, 7));
m_workStop = false;
m_chunks.clear();
for (int i = 0; i <= m_viterbiThreads; i++) {
m_chunks.emplace_back(new ViterbiChunk());
}
// Each from the current round, so it waits for the next rather than running one of earlier workers'
uint64_t generation;
{
std::lock_guard<std::mutex> lock(m_workMutex);
generation = m_workGeneration;
}
for (int i = 1; i <= m_viterbiThreads; i++) {
m_workers.emplace_back(&FECDecoder::viterbiWorker, this, i, generation);
}
}
void FECDecoder::stopViterbiThreads()
{
{
std::lock_guard<std::mutex> lock(m_workMutex);
m_workStop = true;
}
m_workAvailable.notify_all();
for (std::thread& worker : m_workers) {
worker.join();
}
m_workers.clear();
m_viterbiThreads = 0;
}
void FECDecoder::decodeChunk(Viterbi27& viterbi, const int8_t *llrs, int steps, int skip, int count, std::vector<uint8_t>& out)
{
out.clear();
viterbi.reset();
viterbi.decode(llrs, steps, out);
out.erase(out.begin(), out.begin() + std::min((int) out.size(), skip));
out.resize(std::min((int) out.size(), count));
}
void FECDecoder::viterbiWorker(int index, uint64_t generation)
{
while (true)
{
{
std::unique_lock<std::mutex> lock(m_workMutex);
m_workAvailable.wait(lock, [&] { return m_workStop || (m_workGeneration != generation); });
if (m_workStop) {
return;
}
generation = m_workGeneration;
}
ViterbiChunk& chunk = *m_chunks[index];
if (chunk.m_count > 0) {
decodeChunk(chunk.m_viterbi, chunk.m_llrs, chunk.m_steps, chunk.m_skip, chunk.m_count, chunk.m_out);
}
{
std::lock_guard<std::mutex> lock(m_workMutex);
m_workPending--;
}
m_workDone.notify_one();
}
}
// The steps that can be decoded (all but the last traceback length, as the single decoder holds back), in a chunk
// for each thread, each decoder starting 192 steps before its chunk (from equal metrics, which converge well within
// that) and tracing back from 192 steps after it
void FECDecoder::viterbiParallel(const int8_t *llrs, int nbInfoBits, std::vector<uint8_t>& out)
{
static const int roll = Viterbi27::m_tracebackLength;
m_stream.insert(m_stream.end(), llrs, llrs + 2 * nbInfoBits);
const int64_t end = m_streamStart + (int64_t) m_stream.size() / 2;
const int64_t decodable = end - roll - m_streamDecoded;
if (decodable <= 0) {
return;
}
// Not so small a chunk that the extra steps cost more than they save
const int nbChunks = (int) std::max<int64_t>(1, std::min<int64_t>(m_viterbiThreads + 1, decodable / 4096));
const int64_t first = m_streamDecoded;
for (int c = 0; c <= m_viterbiThreads; c++)
{
ViterbiChunk& chunk = *m_chunks[c];
const int64_t a = first + decodable * c / nbChunks;
const int64_t b = first + decodable * (c + 1) / nbChunks;
if (c >= nbChunks)
{
chunk.m_count = 0;
continue;
}
const int64_t from = std::max(m_streamStart, a - roll);
chunk.m_llrs = m_stream.data() + 2 * (from - m_streamStart);
chunk.m_steps = (int) (b + roll - from);
chunk.m_skip = (int) (a - from);
chunk.m_count = (int) (b - a);
}
{
std::lock_guard<std::mutex> lock(m_workMutex);
m_workPending = m_viterbiThreads;
m_workGeneration++;
}
m_workAvailable.notify_all();
ViterbiChunk& own = *m_chunks[0];
decodeChunk(own.m_viterbi, own.m_llrs, own.m_steps, own.m_skip, own.m_count, own.m_out);
{
std::unique_lock<std::mutex> lock(m_workMutex);
m_workDone.wait(lock, [this] { return m_workPending == 0; });
}
for (int c = 0; c < nbChunks; c++) {
out.insert(out.end(), m_chunks[c]->m_out.begin(), m_chunks[c]->m_out.end());
}
m_streamDecoded = first + decodable;
// Keep what the next chunks start from
const int64_t keep = std::max(m_streamStart, m_streamDecoded - roll);
m_stream.erase(m_stream.begin(), m_stream.begin() + 2 * (keep - m_streamStart));
m_streamStart = keep;
}
void FECDecoder::appendHardDecisions(const int8_t *llrs, int nbInfoBits, std::vector<uint8_t>& hardDecisions)
{
size_t base = hardDecisions.size();
hardDecisions.resize(base + nbInfoBits);
uint8_t *hard = &hardDecisions[base];
for (int i = 0; i < nbInfoBits; i++)
{
int8_t x = llrs[2 * i];
int8_t y = llrs[2 * i + 1];
hard[i] = (x < 0) | ((y < 0) << 1) | ((x != 0) << 2) | ((y != 0) << 3);
}
}
// Re-encode the decoded bits and compare with hard decisions on the unpunctured received bits.
// Decoded bits lag the soft input (by the Viterbi decoder's traceback), so the hard decisions for the bits not yet
// decoded are kept for the next call. Re-encoding takes as long as an eighth of the error correction, so only one
// call in m_berInterval is measured, and the others only make hard decisions for the bits they leave undecoded
void FECDecoder::measureBER(const int8_t *llrs, int nbInfoBits, const uint8_t *bits, int nbBits)
{
const int pending = (int) m_hardDecisions.size();
if ((m_berCalls++ % m_berInterval) != 0)
{
// Not measured: just keep the encoder's state, which is the last 6 bits
for (int i = std::max(0, nbBits - 6); i < nbBits; i++) {
m_encoderState = (uint8_t) (((m_encoderState << 1) | (bits[i] & 1)) & 63);
}
if (nbBits >= pending)
{
int skip = std::min(nbBits - pending, nbInfoBits);
m_hardDecisions.clear();
appendHardDecisions(llrs + 2 * skip, nbInfoBits - skip, m_hardDecisions);
}
else
{
m_hardDecisions.erase(m_hardDecisions.begin(), m_hardDecisions.begin() + nbBits);
appendHardDecisions(llrs, nbInfoBits, m_hardDecisions);
}
return;
}
appendHardDecisions(llrs, nbInfoBits, m_hardDecisions);
static const uint8_t bitCount[4] = {0, 1, 1, 2};
int errors = 0;
int total = 0;
m_xy.clear();
Viterbi27::encode(bits, nbBits, m_encoderState, m_xy);
int n = std::min(nbBits, (int) m_hardDecisions.size());
const uint8_t *hard = m_hardDecisions.data();
for (int i = 0; i < n; i++)
{
int valid = hard[i] >> 2;
int sent = m_xy[2 * i] | (m_xy[2 * i + 1] << 1);
errors += bitCount[(hard[i] ^ sent) & valid & 3];
total += bitCount[valid];
}
m_hardDecisions.erase(m_hardDecisions.begin(), m_hardDecisions.begin() + n);
// BER over windows of m_berWindow bits, the first estimate as soon as there are some
m_berErrors += errors;
m_berTotal += total;
if ((m_berTotal >= m_berWindow) || !m_berValid)
{
m_ber = m_berTotal > 0 ? (double) m_berErrors / m_berTotal : 0.0;
m_berValid = m_berTotal > 0;
if (m_berTotal >= m_berWindow)
{
m_berErrors = 0;
m_berTotal = 0;
}
}
}
void FECDecoder::measurePostViterbiBER()
{
const OuterDecoder::Stats& stats = m_outer.getStats();
if (stats.m_packets < m_postBERPackets) // Stats were reset
{
m_postBERPackets = stats.m_packets;
m_postBERBits = stats.m_rsBitsCorrected;
}
uint64_t bits = (stats.m_packets - m_postBERPackets) * rsPacketSize * 8;
if (bits >= m_berWindow)
{
m_postBER = (double) (stats.m_rsBitsCorrected - m_postBERBits) / bits;
m_postBERPackets = stats.m_packets;
m_postBERBits = stats.m_rsBitsCorrected;
}
}
FECDecoder::Status FECDecoder::getStatus() const
{
Status status;
status.m_outer = m_outer.getStats();
status.m_tsSync = m_outer.synced();
status.m_supported = m_supported;
status.m_lowPriority = m_lowPriority;
status.m_mer = m_mer;
status.m_preViterbiBER = m_ber;
status.m_postViterbiBER = m_postBER;
status.m_viterbiBits = m_viterbiBits;
std::copy(m_seconds, m_seconds + 4, status.m_seconds);
return status;
}
} // namespace dvbt

178
modemdvb/dvbtfec.h

@ -0,0 +1,178 @@ @@ -0,0 +1,178 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBTFEC_H
#define MODEMDVB_DVBTFEC_H
#include <algorithm>
#include <atomic>
#include <condition_variable>
#include <cstdint>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "dvbt.h"
#include "dvbtinner.h"
#include "dvbtouter.h"
#include "viterbi27.h"
namespace dvbt {
// Everything after OFDM demodulation: de-mapping and inner de-interleaving, Viterbi decoding and outer decoding,
// from the equalised data cells of each symbol to transport stream packets. Also measures the BER. In two stages,
// which can run in two threads: demap() (inner de-interleaving and de-mapping, to soft bits), then decodeDemapped()
// (Viterbi, BER and outer decoding), each always called from the same thread, with demap()'s output in order
class MODEMDVB_API FECDecoder
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback; //!< 188-byte TS packet
struct Status
{
OuterDecoder::Stats m_outer;
bool m_tsSync;
bool m_supported; //!< False for a mode that can't be decoded (DVB-H's 2K in-depth interleaver)
bool m_lowPriority = false; //!< Decoding a hierarchical mode's low priority stream
double m_mer; //!< dB
double m_preViterbiBER; //!< Recent average, from re-encoding the decoded bits
double m_postViterbiBER; //!< Recent average, from the bits Reed-Solomon corrects. QEF needs < 2e-4
uint64_t m_viterbiBits;
double m_seconds[4]; //!< Processing time: inner, Viterbi, BER, outer
Status() :
m_tsSync(false),
m_supported(true),
m_mer(0.0),
m_preViterbiBER(0.0),
m_postViterbiBER(0.0),
m_viterbiBits(0),
m_seconds{0.0, 0.0, 0.0, 0.0}
{
}
};
//! A symbol's soft bits, from demap()
struct Demapped
{
bool m_reconfigured = false; //!< The parameters changed, so decoding starts again
bool m_supported = true;
double m_mer = 0.0;
double m_seconds = 0.0; //!< To demap
std::vector<int8_t> m_llrs; //!< X, Y per information bit, positive for 0, 0 for erased
};
FECDecoder();
~FECDecoder();
void setPacketCallback(PacketCallback callback) { m_outer.setPacketCallback(callback); }
void reset(); //!< Both stages
void resetDemap();
void resetDecode();
//! Both stages. A symbol without dataCarriers(tps.m_mode) cells is skipped, and decoding starts again after it
void decode(int symbolIndex, const TPS& tps, const Cf *cells, const float *csi, int nbCells);
void demap(int symbolIndex, const TPS& tps, const Cf *cells, const float *csi, int nbCells, Demapped& out);
void decodeDemapped(const Demapped& in);
//! In hierarchical modes, decode the low priority stream rather than the high priority one. From any thread
void setLowPriority(bool lowPriority) { m_lowPriority = lowPriority; }
//! Threads, besides the one calling decodeDemapped(), to share Viterbi decoding, for slow CPUs. From any thread
//! Clamped to what startViterbiThreads() starts, so a request beyond doesn't restart them every symbol
void setViterbiThreads(int threads) { m_viterbiThreadsWanted = std::max(0, std::min(threads, 7)); }
//! From the thread that calls decodeDemapped()
Status getStatus() const;
private:
static double now();
static void decodeChunk(Viterbi27& viterbi, const int8_t *llrs, int steps, int skip, int count, std::vector<uint8_t>& out);
static void appendHardDecisions(const int8_t *llrs, int nbInfoBits, std::vector<uint8_t>& hardDecisions);
void measureBER(const int8_t *llrs, int nbInfoBits, const uint8_t *bits, int nbBits);
void measurePostViterbiBER();
void viterbiParallel(const int8_t *llrs, int nbInfoBits, std::vector<uint8_t>& out);
void startViterbiThreads(int threads);
void stopViterbiThreads();
void viterbiWorker(int index, uint64_t generation);
// Viterbi decoding shared between threads: the stream's undecoded steps (and those before them that start each
// chunk's decoder) are split into chunks, each decoded on its own from 192 steps before it to 192 after, so the
// result is as the single decoder's, with the same lag
struct ViterbiChunk
{
Viterbi27 m_viterbi;
const int8_t *m_llrs = nullptr;
int m_steps = 0; //!< To decode, from m_llrs
int m_skip = 0; //!< Of the decoded bits, the first not wanted
int m_count = 0; //!< Wanted
std::vector<uint8_t> m_out;
};
std::atomic<int> m_viterbiThreadsWanted{0};
int m_viterbiThreads = 0;
std::vector<std::thread> m_workers;
std::vector<std::unique_ptr<ViterbiChunk>> m_chunks; //!< 0 for this thread, then one per worker
std::mutex m_workMutex;
std::condition_variable m_workAvailable;
std::condition_variable m_workDone;
uint64_t m_workGeneration = 0;
int m_workPending = 0;
bool m_workStop = false;
std::vector<int8_t> m_stream; //!< Soft bits from step m_streamStart
int64_t m_streamStart = 0;
int64_t m_streamDecoded = 0; //!< Steps output
// Demapping
InnerDecoder m_inner;
bool m_configured;
bool m_demapSupported;
std::atomic<bool> m_lowPriority{false};
bool m_configuredLowPriority = false;
TPS m_tps;
Demapped m_demapped; //!< For decode()
// Decoding
Viterbi27 m_viterbi;
OuterDecoder m_outer;
bool m_supported;
double m_mer;
std::vector<uint8_t> m_bits;
// Pre-Viterbi BER
std::vector<uint8_t> m_hardDecisions; //!< Per information bit not yet decoded: X, Y, X valid, Y valid in bits 0..3
std::vector<uint8_t> m_xy;
uint8_t m_encoderState;
double m_ber;
bool m_berValid;
uint64_t m_berErrors;
uint64_t m_berTotal;
unsigned int m_berCalls;
static constexpr uint64_t m_berWindow = 2000000; //!< Bits compared
static const unsigned int m_berInterval = 8; //!< Calls, of which one is measured, to save time
// Post-Viterbi BER
double m_postBER;
uint64_t m_postBERPackets; //!< Outer decoder counts at the start of the window
uint64_t m_postBERBits;
uint64_t m_viterbiBits;
double m_seconds[4];
};
} // namespace dvbt
#endif // MODEMDVB_DVBTFEC_H

374
modemdvb/dvbtinner.cpp

@ -0,0 +1,374 @@ @@ -0,0 +1,374 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include "dvbtinner.h"
#if defined(USE_SSE2) || defined(__SSE2__) || defined(_M_X64)
#include <emmintrin.h>
#define INNER_SSE2
#endif
namespace dvbt {
const float InnerDecoder::m_llrScale = 16.0f;
InnerDecoder::InnerDecoder() :
m_mode(Mode::Mode8K),
m_constellation(Constellation::QAM64),
m_codeRate(CodeRate::R2_3),
m_hierarchy(Hierarchy::None),
m_lowPriority(false),
m_nbData(0),
m_bitsPerCell(0),
m_infoBitsPerSymbol(0),
m_puncturePeriod(1),
m_mer(0.0)
{
}
void InnerDecoder::getPuncturePattern(CodeRate codeRate, std::vector<PunctureEntry>& pattern, int& period)
{
// Transmission order of the punctured X and Y bits (4.3.3, table 2)
switch (codeRate)
{
case CodeRate::R1_2:
pattern = {{0, 0}, {0, 1}};
period = 1;
break;
case CodeRate::R2_3:
pattern = {{0, 0}, {0, 1}, {1, 1}};
period = 2;
break;
case CodeRate::R3_4:
pattern = {{0, 0}, {0, 1}, {1, 1}, {2, 0}};
period = 3;
break;
case CodeRate::R5_6:
pattern = {{0, 0}, {0, 1}, {1, 1}, {2, 0}, {3, 1}, {4, 0}};
period = 5;
break;
case CodeRate::R7_8:
default:
pattern = {{0, 0}, {0, 1}, {1, 1}, {2, 1}, {3, 1}, {4, 0}, {5, 1}, {6, 0}};
period = 7;
break;
}
}
float InnerDecoder::normalisation(Constellation constellation, Hierarchy hierarchy)
{
const int a = alpha(hierarchy);
if (constellation == Constellation::QPSK) {
return std::sqrt(2.0f);
} else if (constellation == Constellation::QAM16) {
return std::sqrt(a == 1 ? 10.0f : a == 2 ? 20.0f : 52.0f);
} else {
return std::sqrt(a == 1 ? 42.0f : a == 2 ? 60.0f : 108.0f);
}
}
void InnerDecoder::configure(Mode mode, Constellation constellation, CodeRate codeRate, Hierarchy hierarchy, bool lowPriority)
{
const Tables& tables = Tables::get(mode);
m_mode = mode;
m_constellation = constellation;
m_codeRate = codeRate;
m_hierarchy = constellation == Constellation::QPSK ? Hierarchy::None : hierarchy; // QPSK can't be hierarchical
m_lowPriority = (m_hierarchy != Hierarchy::None) && lowPriority;
m_nbData = tables.m_nbData;
m_bitsPerCell = bitsPerCell(constellation);
getPuncturePattern(codeRate, m_puncture, m_puncturePeriod);
const int streamBits = m_lowPriority ? lpBitsPerSymbol() : hpBitsPerSymbol();
m_infoBitsPerSymbol = streamBits / (int) m_puncture.size() * m_puncturePeriod;
m_softBits.resize(m_nbData * m_bitsPerCell);
m_quantized.resize(m_nbData * m_bitsPerCell);
m_mer = 0.0;
// Symbol interleaver: even symbols carry word q on cell H(q), odd symbols carry word H(q) on cell q (4.3.4.2)
const std::vector<int>& h = tables.m_symbolInterleaver;
std::vector<int> hInv(m_nbData);
for (int q = 0; q < m_nbData; q++) {
hInv[h[q]] = q;
}
// Bit interleavers H_e(w) = (w + offset_e) mod 126 (4.3.4.1)
static const int bitInterleaverOffsets[6] = {0, 63, 105, 42, 21, 84};
const int v = m_bitsPerCell;
const bool hierarchical = m_hierarchy != Hierarchy::None;
for (int parity = 0; parity < 2; parity++)
{
m_permutation[parity].resize(m_nbData * v);
m_softIndex[parity].clear();
m_llrIndex[parity].clear();
for (int c = 0; c < m_nbData; c++)
{
int w = parity == 0 ? hInv[c] : h[c];
int block = w / 126;
int j = w % 126;
for (int e = 0; e < v; e++)
{
// Bit e of the word comes from sub-stream e. Non-hierarchical, it takes input bits in the order
// 0, 2, 4, 1, 3, 5 of each group of v. Hierarchical, sub-streams 0 and 1 take the high priority
// stream's bits alternately, and 2 to v - 1 the low priority stream's, in the order 2, 4, 3, 5 of each
// group of v - 2 (x''di to b[di mod (v-2)] div ((v-2)/2) + 2 [di mod ((v-2)/2)] + 2, 4.3.4.1)
int n = 126 * block + (j + bitInterleaverOffsets[e]) % 126;
int x;
bool lp = false;
if (!hierarchical)
{
int order = (e % 2 == 0) ? e / 2 : v / 2 + e / 2;
x = v * n + order;
}
else if (e < 2)
{
x = 2 * n + e;
}
else
{
const int lpBits = v - 2;
const int half = lpBits / 2;
int r = 0;
for (int rr = 0; rr < lpBits; rr++)
{
if ((rr / half) + 2 * (rr % half) + 2 == e) {
r = rr;
}
}
x = lpBits * n + r;
lp = true;
}
m_permutation[parity][c * v + e] = x;
// The stream decoded: position of punctured bit x in the X/Y pairs
if (lp == m_lowPriority)
{
const PunctureEntry& entry = m_puncture[x % m_puncture.size()];
m_softIndex[parity].push_back(c * v + e);
m_llrIndex[parity].push_back(2 * ((int) (x / m_puncture.size()) * m_puncturePeriod + entry.m_bit) + entry.m_y);
}
}
}
}
}
// Nearest odd integer, limited to +/-maxPoint
inline float InnerDecoder::nearestPoint(float x, float maxPoint)
{
x = std::max(-maxPoint, std::min(maxPoint, x));
float h = x * 0.5f + 8.0f; // Positive, so truncation is floor
return 2.0f * (float) ((int) h - 8) + 1.0f;
}
// Scale, round and saturate to +/-127
void InnerDecoder::quantize(const float *in, int8_t *out, int n)
{
int i = 0;
#ifdef INNER_SSE2
const __m128 scale = _mm_set1_ps(m_llrScale);
const __m128 maxValue = _mm_set1_ps(127.0f);
const __m128 minValue = _mm_set1_ps(-127.0f);
for (; i + 16 <= n; i += 16)
{
__m128i q[4];
for (int j = 0; j < 4; j++)
{
__m128 v = _mm_mul_ps(_mm_loadu_ps(in + i + 4 * j), scale);
v = _mm_max_ps(minValue, _mm_min_ps(maxValue, v));
q[j] = _mm_cvtps_epi32(v);
}
__m128i lo = _mm_packs_epi32(q[0], q[1]);
__m128i hi = _mm_packs_epi32(q[2], q[3]);
_mm_storeu_si128((__m128i *) (out + i), _mm_packs_epi16(lo, hi));
}
#endif
for (; i < n; i++)
{
float v = std::max(-127.0f, std::min(127.0f, in[i] * m_llrScale));
out[i] = (int8_t) (v >= 0.0f ? v + 0.5f : v - 0.5f);
}
}
void InnerDecoder::decode(int symbolIndex, const Cf *cells, const float *csi, std::vector<int8_t>& llrs)
{
const int v = m_bitsPerCell;
const int nbBits = m_nbData * v;
const float scale = normalisation(m_constellation, m_hierarchy);
const float maxPoint = (float) ((1 << (v / 2)) - 1);
// Non-uniform constellations' points are alpha - 1 further from the axes (4.3.5)
const float offset = (float) (alpha(m_hierarchy) - 1);
float *soft = m_softBits.data();
// Soft bits, in cell order. Gray mapping (4.3.5): y0/y1 are the signs of I/Q (1 = negative),
// y2/y3 and y4/y5 the magnitudes
if (m_constellation == Constellation::QPSK)
{
for (int c = 0; c < m_nbData; c++, soft += 2)
{
float w = csi[c] * scale;
soft[0] = cells[c].real() * w;
soft[1] = cells[c].imag() * w;
}
}
else if (m_constellation == Constellation::QAM16)
{
for (int c = 0; c < m_nbData; c++, soft += 4)
{
float xi = cells[c].real() * scale;
float xq = cells[c].imag() * scale;
float w = csi[c];
soft[0] = xi * w;
soft[1] = xq * w;
soft[2] = (std::fabs(xi) - 2.0f - offset) * w;
soft[3] = (std::fabs(xq) - 2.0f - offset) * w;
}
}
else
{
for (int c = 0; c < m_nbData; c++, soft += 6)
{
float xi = cells[c].real() * scale;
float xq = cells[c].imag() * scale;
float w = csi[c];
float ai = std::fabs(xi) - 4.0f - offset;
float aq = std::fabs(xq) - 4.0f - offset;
soft[0] = xi * w;
soft[1] = xq * w;
soft[2] = ai * w;
soft[3] = aq * w;
soft[4] = (std::fabs(ai) - 2.0f) * w;
soft[5] = (std::fabs(aq) - 2.0f) * w;
}
}
// MER, from a quarter of the cells, of those received (not erased, with CSI 0)
double errorPower = 0.0;
int measured = 0;
for (int c = 0; c < m_nbData; c += 4)
{
if (csi[c] <= 0.0f) {
continue;
}
measured++;
// Error from the nearest point, of the magnitudes less the offset (an odd integer, at least 1)
float ai = std::fabs(cells[c].real() * scale) - offset;
float aq = std::fabs(cells[c].imag() * scale) - offset;
float ei = ai - std::max(1.0f, nearestPoint(ai, maxPoint));
float eq = aq - std::max(1.0f, nearestPoint(aq, maxPoint));
errorPower += ei * ei + eq * eq;
}
if (measured > 0)
{
double mer = 10.0 * std::log10((scale * scale) * measured / std::max(errorPower, 1e-9));
m_mer = m_mer == 0.0 ? mer : 0.9 * m_mer + 0.1 * mer;
}
quantize(m_softBits.data(), m_quantized.data(), nbBits);
// De-interleave and de-puncture into X/Y pairs, erased bits being 0. Of the stream decoded
const std::vector<int>& softIndex = m_softIndex[symbolIndex & 1];
const std::vector<int>& llrIndex = m_llrIndex[symbolIndex & 1];
size_t base = llrs.size();
llrs.resize(base + 2 * m_infoBitsPerSymbol, 0);
int8_t *out = &llrs[base];
const int8_t *q = m_quantized.data();
const int nbStream = (int) llrIndex.size();
if (nbStream == nbBits)
{
for (int i = 0; i < nbBits; i++) {
out[llrIndex[i]] = q[i];
}
}
else
{
for (int i = 0; i < nbStream; i++) {
out[llrIndex[i]] = q[softIndex[i]];
}
}
}
void InnerDecoder::encode(int symbolIndex, const uint8_t *bits, Cf *cells, const uint8_t *lpBits) const
{
const std::vector<int>& perm = m_permutation[symbolIndex & 1];
const int v = m_bitsPerCell;
const float scale = normalisation(m_constellation, m_hierarchy);
const float offset = (float) (alpha(m_hierarchy) - 1);
const bool hierarchical = m_hierarchy != Hierarchy::None;
for (int c = 0; c < m_nbData; c++)
{
const int *p = &perm[c * v];
int y[6] = {0, 0, 0, 0, 0, 0};
for (int e = 0; e < v; e++) {
y[e] = (hierarchical && (e >= 2) ? lpBits : bits)[p[e]];
}
float mi = 1.0f, mq = 1.0f;
if (m_constellation == Constellation::QAM16)
{
mi = (y[2] ? 1.0f : 3.0f) + offset;
mq = (y[3] ? 1.0f : 3.0f) + offset;
}
else if (m_constellation == Constellation::QAM64)
{
static const float mag[4] = {7.0f, 5.0f, 1.0f, 3.0f}; // Index (y2 << 1) | y4
mi = mag[(y[2] << 1) | y[4]] + offset;
mq = mag[(y[3] << 1) | y[5]] + offset;
}
cells[c] = Cf(y[0] ? -mi : mi, y[1] ? -mq : mq) / scale;
}
}
void InnerDecoder::puncture(CodeRate codeRate, const uint8_t *xy, int nbInfoBits, std::vector<uint8_t>& out)
{
std::vector<PunctureEntry> pattern;
int period;
getPuncturePattern(codeRate, pattern, period);
for (int i = 0; i + period <= nbInfoBits; i += period)
{
for (const PunctureEntry& entry : pattern) {
out.push_back(xy[2 * (i + entry.m_bit) + entry.m_y]);
}
}
}
} // namespace dvbt

94
modemdvb/dvbtinner.h

@ -0,0 +1,94 @@ @@ -0,0 +1,94 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBTINNER_H
#define MODEMDVB_DVBTINNER_H
#include <cstdint>
#include <vector>
#include "dvbt.h"
namespace dvbt {
// Inner de-interleaving and de-mapping (4.3.4, 4.3.5) and de-puncturing (4.3.3). Turns the equalized data cells of one
// OFDM symbol into soft X/Y pairs for the Viterbi decoder. In hierarchical modes, of one of the two streams: the high
// priority one, in the first two bits of each cell (the quadrant), or the low priority one, in the others, each with its
// own code rate, and with alpha 2 or 4 the constellation's points are further from the axes (non-uniform)
class MODEMDVB_API InnerDecoder
{
public:
InnerDecoder();
//! codeRate is that of the stream decoded: the low priority one if lowPriority, in a hierarchical mode
void configure(Mode mode, Constellation constellation, CodeRate codeRate, Hierarchy hierarchy = Hierarchy::None,
bool lowPriority = false);
int infoBitsPerSymbol() const { return m_infoBitsPerSymbol; }
//! cells are equalized and normalised to unit average power, csi the per-cell reliability (1 = average).
//! Appends infoBitsPerSymbol() X/Y pairs to llrs
void decode(int symbolIndex, const Cf *cells, const float *csi, std::vector<int8_t>& llrs);
double getMER() const { return m_mer; } //!< dB, averaged over recent symbols
// Transmitter side, for testing: punctured bits of one symbol into cells, in carrier order. In hierarchical modes,
// bits are the high priority stream's and lpBits the low priority one's
void encode(int symbolIndex, const uint8_t *bits, Cf *cells, const uint8_t *lpBits = nullptr) const;
//! Punctured bits a symbol carries of the high (or the only) and the low priority streams
int hpBitsPerSymbol() const { return m_nbData * (m_hierarchy == Hierarchy::None ? m_bitsPerCell : 2); }
int lpBitsPerSymbol() const { return m_hierarchy == Hierarchy::None ? 0 : m_nbData * (m_bitsPerCell - 2); }
static int alpha(Hierarchy hierarchy) { return hierarchy == Hierarchy::Alpha2 ? 2 : hierarchy == Hierarchy::Alpha4 ? 4 : 1; }
//! What normalises the constellation to unit average power (its points being odd integers, plus alpha - 1)
static float normalisation(Constellation constellation, Hierarchy hierarchy);
static void puncture(CodeRate codeRate, const uint8_t *xy, int nbInfoBits, std::vector<uint8_t>& out);
private:
struct PunctureEntry
{
int m_bit; //!< Information bit within the period
int m_y; //!< 0 for X, 1 for Y
};
static void getPuncturePattern(CodeRate codeRate, std::vector<PunctureEntry>& pattern, int& period);
static inline float nearestPoint(float x, float maxPoint);
static void quantize(const float *in, int8_t *out, int n);
//! Soft bit scale: an LLR of m_llrScale for a point 1 away from the decision boundary (points are 2 apart)
static const float m_llrScale;
Mode m_mode;
Constellation m_constellation;
CodeRate m_codeRate;
Hierarchy m_hierarchy;
bool m_lowPriority;
int m_nbData;
int m_bitsPerCell;
int m_infoBitsPerSymbol;
std::vector<int> m_permutation[2]; //!< [symbol parity][cell * bits + bit] -> punctured bit index, in its stream
std::vector<int> m_softIndex[2]; //!< [symbol parity][i] -> cell * bits + bit, of the stream decoded
std::vector<int> m_llrIndex[2]; //!< [symbol parity][i] -> index in X/Y pairs
std::vector<PunctureEntry> m_puncture;
int m_puncturePeriod;
std::vector<float> m_softBits; //!< Soft bits of one symbol, in cell order
std::vector<int8_t> m_quantized;
double m_mer;
};
} // namespace dvbt
#endif // MODEMDVB_DVBTINNER_H

250
modemdvb/dvbtmodulator.cpp

@ -0,0 +1,250 @@ @@ -0,0 +1,250 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <cmath>
#include <cstring>
#include <QString>
#include "dsp/fftengine.h"
#include "dvbtmodulator.h"
#include "dvbtouter.h"
#include "viterbi27.h"
namespace dvbt {
Modulator::Modulator(Mode mode, Constellation constellation, CodeRate codeRate, GuardInterval guard, int cellId,
Hierarchy hierarchy, CodeRate lpCodeRate) :
m_mode(mode),
m_constellation(constellation),
m_codeRate(codeRate),
m_guard(guard),
m_cellId(cellId),
m_tables(Tables::get(mode)),
m_hierarchy(constellation == Constellation::QPSK ? Hierarchy::None : hierarchy),
m_lpCodeRate(lpCodeRate),
m_symbolIndex(0),
m_frameNumber(0)
{
m_inner.configure(mode, constellation, codeRate, m_hierarchy, false);
m_fft = FFTEngine::create(QString());
m_fft->configure(m_tables.m_fftSize, true);
m_cells.resize(m_tables.m_nbData);
m_tpsValue.resize(m_tables.m_tpsCarriers.size());
m_streams[0].m_rate = codeRate;
m_streams[0].m_infoBits = m_inner.infoBitsPerSymbol();
if (m_hierarchy != Hierarchy::None)
{
InnerDecoder lp;
lp.configure(mode, constellation, lpCodeRate, m_hierarchy, true);
m_streams[1].m_rate = lpCodeRate;
m_streams[1].m_infoBits = lp.infoBitsPerSymbol();
}
for (Stream& stream : m_streams)
{
for (int j = 0; j < 12; j++) {
stream.m_fifo[j].assign(j * 17, 0);
}
}
}
Modulator::~Modulator()
{
delete m_fft;
}
void Modulator::Stream::encode(const uint8_t *packets, int nbPackets)
{
for (int g = 0; g + 8 <= nbPackets; g += 8)
{
uint8_t group[8][tsPacketSize];
memcpy(group, packets + g * tsPacketSize, sizeof(group));
OuterDecoder::randomize(group);
for (int p = 0; p < 8; p++)
{
uint8_t rsPacket[rsPacketSize];
OuterDecoder::rsEncode(group[p], rsPacket);
uint8_t bits[rsPacketSize * 8];
for (int i = 0; i < rsPacketSize; i++)
{
// Outer interleave
int j = m_bytePos % 12;
uint8_t byte = rsPacket[i];
if (j > 0)
{
uint8_t out = m_fifo[j][m_fifoPos[j]];
m_fifo[j][m_fifoPos[j]] = byte;
m_fifoPos[j] = (m_fifoPos[j] + 1) % (int) m_fifo[j].size();
byte = out;
}
m_bytePos = (m_bytePos + 1) % rsPacketSize;
for (int b = 0; b < 8; b++) {
bits[i * 8 + b] = (byte >> (7 - b)) & 1;
}
}
Viterbi27::encode(bits, rsPacketSize * 8, m_encoderState, m_xy);
}
}
}
void Modulator::modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& samples, const uint8_t *lpPackets, int nbLpPackets)
{
m_streams[0].encode(packets, nbPackets);
const bool hierarchical = m_hierarchy != Hierarchy::None;
const bool lpGiven = lpPackets && (nbLpPackets > 0);
std::vector<uint8_t> nullPackets;
if (hierarchical && lpGiven) {
m_streams[1].encode(lpPackets, nbLpPackets);
}
if (hierarchical && !lpGiven)
{
// Null packets (PID 0x1fff) for the low priority stream, so the high priority one's are still sent
nullPackets.assign(8 * tsPacketSize, (uint8_t) 0xff);
for (int p = 0; p < 8; p++)
{
uint8_t *packet = &nullPackets[p * tsPacketSize];
packet[0] = 0x47;
packet[1] = 0x1f;
packet[2] = 0xff;
packet[3] = 0x10;
}
}
// Symbols, while each stream has the bits for one
while (true)
{
if (!nullPackets.empty() && ((int) m_streams[0].m_xy.size() >= 2 * m_streams[0].m_infoBits))
{
while ((int) m_streams[1].m_xy.size() < 2 * m_streams[1].m_infoBits) {
m_streams[1].encode(nullPackets.data(), 8);
}
}
for (int s = 0; s < (hierarchical ? 2 : 1); s++)
{
if ((int) m_streams[s].m_xy.size() < 2 * m_streams[s].m_infoBits) {
return;
}
}
for (int s = 0; s < (hierarchical ? 2 : 1); s++)
{
Stream& stream = m_streams[s];
stream.m_punctured.clear();
InnerDecoder::puncture(stream.m_rate, stream.m_xy.data(), stream.m_infoBits, stream.m_punctured);
stream.m_xy.erase(stream.m_xy.begin(), stream.m_xy.begin() + 2 * stream.m_infoBits);
}
modulateSymbol(samples);
}
}
void Modulator::modulateSymbol(std::vector<Cf>& samples)
{
const int n = m_tables.m_fftSize;
const int kMax = m_tables.m_kMax;
const int phase = m_symbolIndex % 4;
const float boost = 4.0f / 3.0f;
if (m_symbolIndex == 0)
{
TPS tps;
tps.m_frameNumber = m_frameNumber;
tps.m_constellation = m_constellation;
tps.m_hierarchy = m_hierarchy;
tps.m_hpCodeRate = m_codeRate;
tps.m_lpCodeRate = m_hierarchy != Hierarchy::None ? m_lpCodeRate : CodeRate::R1_2; // 000 (4.6.2.7)
tps.m_guard = m_guard;
tps.m_mode = m_mode;
if (m_cellId >= 0)
{
tps.m_lengthIndicator = 31;
tps.m_cellIdByte = (m_frameNumber & 1) ? (m_cellId & 0xff) : (m_cellId >> 8);
}
tps.encode(m_tpsBits);
}
m_inner.encode(m_symbolIndex, m_streams[0].m_punctured.data(), m_cells.data(),
m_hierarchy != Hierarchy::None ? m_streams[1].m_punctured.data() : nullptr);
Cf *in = m_fft->in();
std::fill(in, in + n, Cf(0.0f, 0.0f));
const std::vector<uint8_t>& type = m_tables.m_carrierType[phase];
const std::vector<int>& data = m_tables.m_dataCarriers[phase];
auto bin = [n, kMax](int k) { int i = k - kMax / 2; return i < 0 ? i + n : i; };
for (int k = 0; k <= kMax; k++)
{
if (type[k] == 1) {
in[bin(k)] = Cf(m_tables.m_prbs[k] ? -boost : boost, 0.0f);
}
}
for (size_t i = 0; i < data.size(); i++) {
in[bin(data[i])] = m_cells[i];
}
// TPS: absolute in symbol 0, then DBPSK (4.6)
for (size_t i = 0; i < m_tables.m_tpsCarriers.size(); i++)
{
int k = m_tables.m_tpsCarriers[i];
if (m_symbolIndex == 0) {
m_tpsValue[i] = m_tables.m_prbs[k] ? -1.0f : 1.0f;
} else if (m_tpsBits[m_symbolIndex]) {
m_tpsValue[i] = -m_tpsValue[i];
}
in[bin(k)] = Cf(m_tpsValue[i], 0.0f);
}
m_fft->transform();
const Cf *out = m_fft->out();
const int ng = guardSamples(m_mode, m_guard);
const float scale = 1.0f / std::sqrt((float) (kMax + 1));
for (int i = n - ng; i < n; i++) {
samples.push_back(out[i] * scale);
}
for (int i = 0; i < n; i++) {
samples.push_back(out[i] * scale);
}
if (++m_symbolIndex == symbolsPerFrame)
{
m_symbolIndex = 0;
m_frameNumber = (m_frameNumber + 1) % framesPerSuperFrame;
}
}
} // namespace dvbt

90
modemdvb/dvbtmodulator.h

@ -0,0 +1,90 @@ @@ -0,0 +1,90 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBTMODULATOR_H
#define MODEMDVB_DVBTMODULATOR_H
#include <cstdint>
#include <vector>
#include "dvbt.h"
#include "dvbtinner.h"
class FFTEngine;
namespace dvbt {
// DVB-T transmitter, for testing the receiver. Hierarchical modes have a high priority stream (codeRate) and a low
// priority one (lpCodeRate), each with its own outer and inner coding
class MODEMDVB_API Modulator
{
public:
//! cellId is signalled in the TPS if not negative
Modulator(Mode mode, Constellation constellation, CodeRate codeRate, GuardInterval guard, int cellId = -1,
Hierarchy hierarchy = Hierarchy::None, CodeRate lpCodeRate = CodeRate::R1_2);
~Modulator();
//! Packets are 188 bytes, starting 0x47, a multiple of 8 of each stream (lpPackets for the low priority stream
//! of a hierarchical mode, which carries null packets if there are none). Appends samples, with unit average
//! power, for each OFDM symbol completed, which takes the bits of both streams
void modulate(const uint8_t *packets, int nbPackets, std::vector<Cf>& samples, const uint8_t *lpPackets = nullptr,
int nbLpPackets = 0);
//! Packets a symbol carries, on average, of the high (or the only) and the low priority streams
double hpPacketsPerSymbol() const { return m_streams[0].m_infoBits / (rsPacketSize * 8.0); }
double lpPacketsPerSymbol() const { return m_hierarchy == Hierarchy::None ? 0.0 : m_streams[1].m_infoBits / (rsPacketSize * 8.0); }
private:
// Outer coding, interleaving and convolutional coding of one stream
struct Stream
{
CodeRate m_rate = CodeRate::R1_2;
int m_infoBits = 0; //!< A symbol takes
std::vector<uint8_t> m_fifo[12]; //!< Outer interleaver, branch j delays j * 17 bytes
int m_fifoPos[12] = {};
int m_bytePos = 0;
uint8_t m_encoderState = 0;
std::vector<uint8_t> m_xy; //!< Encoded X/Y pairs waiting for a symbol
std::vector<uint8_t> m_punctured;
void encode(const uint8_t *packets, int nbPackets);
};
void modulateSymbol(std::vector<Cf>& samples);
Mode m_mode;
Constellation m_constellation;
CodeRate m_codeRate;
GuardInterval m_guard;
int m_cellId;
const Tables& m_tables;
Hierarchy m_hierarchy;
CodeRate m_lpCodeRate;
InnerDecoder m_inner;
FFTEngine *m_fft;
Stream m_streams[2]; //!< High (or the only) and low priority
std::vector<Cf> m_cells;
std::vector<float> m_tpsValue; //!< Current TPS carrier values
uint8_t m_tpsBits[symbolsPerFrame];
int m_symbolIndex;
int m_frameNumber;
};
} // namespace dvbt
#endif // MODEMDVB_DVBTMODULATOR_H

2380
modemdvb/dvbtofdm.cpp

File diff suppressed because it is too large Load Diff

332
modemdvb/dvbtofdm.h

@ -0,0 +1,332 @@ @@ -0,0 +1,332 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBTOFDM_H
#define MODEMDVB_DVBTOFDM_H
#include <cstdint>
#include <functional>
#include <vector>
#include "cwcanceller.h"
#include "dvbt.h"
class FFTEngine;
class FFTFactory;
namespace dvbt {
//! The channel's impulse response, for display: power in dB relative to the strongest path, against delay from the
//! FFT window's start, bin b being b * m_resolution samples, wrapping round after m_powerDb.size() bins
struct ImpulseResponse
{
std::vector<float> m_powerDb;
double m_resolution = 0.0; //!< Samples a bin
int m_guard = 0; //!< Guard interval, samples
};
// OFDM demodulation: acquisition, synchronisation, channel estimation, equalisation and TPS decoding.
// Input must be at sampleRate(bandwidth), e.g. 64/7 MS/s for 8 MHz channels.
// Clock error is followed by moving the FFT window a sample at a time
class MODEMDVB_API OFDMDemod
{
public:
enum State {
Acquire, //!< Looking for guard interval correlation
IntegerCFO, //!< Finding the frequency offset in whole carriers
PilotSync, //!< Finding the scattered pilot phase
TPSSync, //!< Looking for the TPS sync word
Locked //!< Outputting data cells
};
struct Symbol
{
int m_index; //!< In the frame, 0..67
int m_frame; //!< In the super-frame, 0..3
const TPS *m_tps;
const Cf *m_cells; //!< Equalised data cells, in carrier order, unit average power
const float *m_csi; //!< Per-cell reliability, 1 = average
int m_nbCells;
};
typedef std::function<void(const Symbol& symbol)> SymbolCallback;
typedef std::function<void(const char *message)> LogCallback;
struct Status
{
State m_state = Acquire;
Mode m_mode = Mode::Mode8K;
GuardInterval m_guard = GuardInterval::G1_32;
double m_frequencyOffset = 0.0; //!< Hz
double m_clockOffset = 0.0; //!< ppm
int m_clockMeasurements = 0; //!< Symbols it's averaged over, up to 100
double m_gaCorrelation = 0.0; //!< Guard interval correlation at acquisition, 0..1
double m_delay = 0.0; //!< Delay of the first path from the FFT window start, samples
double m_spread = 0.0; //!< Delay between the first and last significant paths, samples
double m_snr = 0.0; //!< dB, of the equalised cells, from the continual pilots' error; 0 if not known
bool m_tpsValid = false;
TPS m_tps;
int m_cellId = -1; //!< -1 if not signalled or not yet received
uint64_t m_symbols = 0; //!< Symbols demodulated
uint64_t m_windowSteps = 0; //!< Timing corrections
uint64_t m_acquisitions = 0;
uint64_t m_resyncs = 0; //!< Timing jumps recovered from, without acquiring again
};
//! If fftFactory is null, FFT engines are created and planned here
explicit OFDMDemod(int bandwidthHz = 8000000, FFTFactory *fftFactory = nullptr);
~OFDMDemod();
void setSymbolCallback(SymbolCallback callback) { m_callback = callback; }
void setLogCallback(LogCallback callback) { m_log = callback; } //!< For state changes and their measurements
void reset();
void process(const Cf *samples, int nbSamples);
//! The clock offset of the samples from now has changed by ppm, as when they're resampled to correct it
void adjustClockOffset(double ppm) { m_clockOffset += ppm; }
//! Narrowband interferers notched out before the FFT, Hz from the input's centre, so their carriers are erased
void setNotches(const std::vector<double>& frequencies, double inputRate);
Status getStatus() const;
//! For display, from the most recent symbol: up to maxCells equalised data cells, and the channel
//! frequency response magnitude in dB, relative to its mean, at points evenly spread over the carriers.
//! Not thread safe: call from the thread that calls process()
void getDisplayData(std::vector<Cf>& cells, int maxCells, std::vector<float>& responseDb, int points) const;
//! The latest impulse response, in at most points bins, empty if none. Not thread safe, as getDisplayData()
void getImpulseResponse(ImpulseResponse& response, int points) const;
private:
struct RingSymbol
{
std::vector<Cf> m_y; //!< FFT output, carriers 0..Kmax
int m_spPhase; //!< Scattered pilot phase, l mod 4
double m_rotation; //!< Phase removed from the FFT output, to align it with the previous symbol
bool m_coherent = true; //!< False if its continual pilots didn't correlate with the reference symbol's
double m_drift = 0.0; //!< Timing drift from the clock offset, samples, since an arbitrary symbol
bool m_valid = false; //!< False if lost or misaligned, as when the timing jumps
};
static constexpr int m_ringSize = 7; //!< Channel estimation interpolates over +/-3 symbols
//! m_seq is kept below twice this, so it doesn't overflow. A multiple of the ring size and the scattered pilot
//! period, so subtracting it leaves the ring positions and pilot phases as they were
static constexpr int m_seqPeriod = 4 * m_ringSize * (1 << 20);
static const double m_giLossFraction; //!< Of the usual guard interval correlation, below which the timing is lost
static const double m_giAveraging; //!< Of the guard interval correlation, per symbol
static const double m_twoPi;
static constexpr int m_acquireSymbols = 12; //!< Symbols of the longest mode analysed per acquisition attempt
static const double m_acquireThreshold; //!< Minimum normalised guard interval correlation
static const double m_intCFOThreshold; //!< Minimum normalised continual pilot correlation
static const double m_frequencyLoopGain;
static const double m_delaySmoothing;
//! Symbols whose significant paths all spread over more than the guard interval (net of those that didn't) before
//! the least spread is taken, rather than the last measurement kept, so bursts of noise don't count
static constexpr int m_cirRejectSymbols = 8;
static const float m_pilotBoost; //!< Scattered and continual pilot amplitude (4.5.3)
static const float m_noiseAlpha; //!< Per symbol, for the noise on each carrier
static constexpr int m_noiseSettleSymbols = 64; //!< Before the per-carrier noise is used
static const float m_noiseFloor; //!< Least noise assumed on a carrier, relative to the mean
//! The carriers near notches are worked out again when the frequency offset has moved this far, as the frequency
//! loop moves it a little every symbol. Their compensation's phase changes by at most 0.03 degrees a Hz
static const double m_notchOffsetTolerance; //!< Hz
//! Samples used are discarded from the buffer once there are this many, keeping it small enough to stay in cache
static constexpr int64_t m_bufferDiscard = 1 << 16;
static inline Cf complexMultiply(Cf a, Cf b);
static void rotate(const Cf *x, Cf *y, int n, double phase, double step);
static void scale(Cf *y, int n, Cf c);
static void multiply(Cf *y, const Cf *c, int n);
static void equalise(const Cf *y, const Cf *h, Cf derotate, int n, Cf *e, float *power);
static inline std::complex<double> unit(const Cf& v);
static void convolveCentred(const Cf *x, int n, const float *w, int half, Cf *y, bool symmetric, int done = -1);
static void impulseResponseDb(const std::vector<float>& power, double resolution, int points, std::vector<float>& powerDb,
double& binResolution);
void configureMode(Mode mode, GuardInterval guard);
bool acquire();
void processSymbol();
void findIntegerCFO(const Cf *bins);
void findPilotPhase();
void estimateChannel(int seq);
void designSmoothingFilter(double cutoff);
void designInterpolationFilter();
bool measureImpulseResponse(int t, double& firstPath, double& spread, double& strongest);
void prepareHypotheses();
double hypothesisMetric(int t, int startIndex, int alias);
void smoothChannel();
void erasePilotOutliers();
void updateNoise(int phase);
void decodeTPSBit(int bit, bool known);
void advance(bool valid);
void outputSymbol(int t);
void skipSymbol();
double guardCorrelation(int64_t start) const;
int scatteredPilotPhase(const std::vector<Cf>& y) const;
void resync();
void trackFrequency();
void trackTiming();
void applyWindowStep(int step);
void startAcquire();
RingSymbol& ring(int seq) { return m_ring[(seq % m_ringSize + m_ringSize) % m_ringSize]; }
const Cf *bufferAt(int64_t index) const { return &m_buffer[index - m_bufferOffset]; }
void log(const char *format, ...);
double m_sampleRate;
SymbolCallback m_callback;
LogCallback m_log;
State m_state;
// Mode
Mode m_mode;
GuardInterval m_guard;
const Tables *m_tables;
int m_fftSize;
int m_guardSize;
int m_symbolSize;
int m_kMax;
int m_backoff; //!< FFT window starts this many samples before the end of the guard interval
// An FFT engine, from the factory if there is one
struct FFT
{
FFTFactory *m_factory = nullptr;
FFTEngine *m_engine = nullptr;
unsigned int m_sequence = 0;
int m_size = 0;
bool m_inverse = false;
void configure(int size, bool inverse);
void release();
};
FFT m_fft;
FFT m_cirFft; //!< For the channel impulse response, from the pilots
std::vector<float> m_cirWindow;
std::vector<float> m_cirPower;
double m_cirResolution = 0.0; //!< Samples a bin of m_cirPower, 0 until measured
std::vector<int> m_cirBins; //!< Significant paths
std::vector<int> m_cirAbove; //!< For findAboveAVX2's output, a CIR size
std::vector<Cf> m_cirPhasors; //!< exp(-j 2 pi n / (3 * CIR size))
std::vector<Cf> m_cirModel; //!< Channel at the TPS carriers, from a hypothesis
std::vector<Cf> m_cirPrefix; //!< For each TPS carrier, prefix sums over the paths of their contributions
std::vector<Cf> m_cirWrap; //!< For each TPS carrier, the phasor for a path a CIR period later
std::vector<Cf> m_cirAlias; //!< For each TPS carrier, phasors for an alias of -1, 0 and 1
int m_aliasCount; //!< Consecutive symbols the first path appears to have jumped
int m_cirRejected; //!< Symbols whose paths spread over the guard interval, net of others
// Samples
std::vector<Cf> m_buffer;
int64_t m_bufferOffset; //!< Absolute index of m_buffer[0]
int64_t m_symbolStart; //!< Absolute index of the start of the next symbol's guard interval
int m_windowStep; //!< Applied to the next symbol start
// Frequency correction
double m_ncoFreq; //!< Radians per sample, applied to the input
double m_ncoPhase;
int64_t m_ncoIndex; //!< Absolute sample index at which m_ncoPhase applies
// Symbol ring and channel estimate
std::vector<int> m_carrierBin; //!< FFT bin of each carrier
RingSymbol m_ring[m_ringSize];
int m_seq; //!< Symbols since pilot sync, less multiples of m_seqPeriod
int m_spPhase0; //!< Scattered pilot phase of m_seq 0
std::vector<Cf> m_y;
std::vector<Cf> m_prevBins;
std::vector<Cf> m_hp; //!< Channel estimate on every third carrier
std::vector<float> m_pilotSign; //!< Of each scattered pilot (on every third carrier), divided by its boost
std::vector<float> m_pilotPower;
std::vector<float> m_pilotSorted;
std::vector<uint8_t> m_pilotErased;
std::vector<Cf> m_hs; //!< m_hp smoothed across frequency
std::vector<Cf> m_hi[2]; //!< Interpolated 1 and 2 carriers after each pilot
std::vector<float> m_smoothingTaps; //!< Low pass filter matched to the delay spread, empty for none
double m_smoothingCutoff; //!< Cycles per pilot
static constexpr int m_smoothingHalfLength = 40;
std::vector<float> m_interpolationTaps[3]; //!< Interpolate from every third carrier, per carrier index mod 3
static constexpr int m_interpolationHalfLength = 8; //!< Pilots each side
std::vector<Cf> m_h; //!< Channel estimate of the symbol being output
std::vector<Cf> m_equalised; //!< Every carrier of the symbol being output
std::vector<float> m_power; //!< |m_h|^2
std::vector<float> m_dataMask[4]; //!< 1 for data carriers, 0 for others, per scattered pilot phase
std::vector<Cf> m_cells;
std::vector<float> m_csi;
std::vector<float> m_noise; //!< Noise power per carrier (from index 2), before equalisation, -1 until measured
std::vector<float> m_noiseSmoothed; //!< Smoothed across carriers and limited, for the CSI
std::vector<float> m_noiseInverse; //!< 1 / m_noiseSmoothed
NotchedCarriers m_notched;
std::vector<double> m_notchFrequencies; //!< As last set
double m_notchInputRate = 0.0;
bool m_notchesChanged = true; //!< So the carriers near them are to be found again
double m_notchOffset = 0.0; //!< Frequency offset they were found for, Hz
int m_noiseSymbols; //!< Symbols that have contributed to m_noise
double m_pilotNoise = -1.0; //!< Mean error power of the equalised continual pilots, -1 if not measured
Constellation m_noiseConstellation; //!< Of the decisions m_noise is from
std::vector<Cf> m_stepPhasors; //!< For window steps
std::vector<Cf> m_prevTPS; //!< Equalised TPS carriers of the previous output symbol
bool m_prevTPSValid;
double m_delay; //!< Smoothed delay of the first path from the window start, samples
double m_spread; //!< Smoothed spread of the paths, samples
double m_strongest; //!< Smoothed delay of the strongest path from the first, samples
// Acquisition sub-states
int m_stateSymbols; //!< Symbols processed in the current state
int m_intCFOCandidate;
int m_lastSPPhase;
int m_spPhaseRun; //!< Consecutive symbols with consistent scattered pilot phase
// TPS
uint16_t m_tpsSync;
int m_symbolIndex; //!< Of the symbol being output, -1 if not known
int m_frameNumber;
uint8_t m_tpsBits[symbolsPerFrame];
TPS m_tps;
bool m_tpsValid;
TPS m_tpsCandidate;
bool m_tpsCandidateValid;
TPS m_tpsPending; //!< Changed parameters, for the next super-frame
bool m_tpsPendingValid;
int m_tpsFailures;
bool m_tpsSyncFailed; //!< The current frame's sync word was wrong, which m_tpsFailures has counted
int m_incoherentSymbols; //!< Consecutive symbols where the continual pilots don't correlate
bool m_tpsBitsLost; //!< Some of the current frame's TPS bits weren't received
uint16_t m_tpsHistory; //!< The last 16 TPS bits, to find the sync word in
int m_tpsHistoryBits; //!< Consecutive bits in it that were received
int m_tpsRealign; //!< Symbols left to find the frame position in after a resync, 0 if not looking
// Timing jumps: the guard interval correlation at the window collapses when samples are lost or repeated (as
// by an SDR that can't keep up, or a recording of one). The timing is found again, keeping the frequency,
// and the scattered pilot phase says how many symbols were lost, so the frame and the FEC stay aligned
double m_giAverage; //!< Guard interval correlation at the window, 0..1
int m_giLow; //!< Consecutive symbols with too little
bool m_resyncPending; //!< Find the timing again, once there are enough samples
bool m_resyncPhase; //!< Check the scattered pilot phase of the next symbol
bool m_outputErased; //!< Output m_erasedCells, for a symbol that wasn't received
std::vector<Cf> m_erasedCells;
std::vector<float> m_erasedCSI;
uint64_t m_resyncs;
int m_cellIdBytes[2]; //!< High and low bytes of the cell id, -1 until received
// Status
double m_gaCorrelation;
double m_clockOffset;
int m_clockMeasurements; //!< Averaged into m_clockOffset, up to 100
uint64_t m_symbolCount;
uint64_t m_windowSteps;
uint64_t m_acquisitions;
int m_acquireFailures;
};
} // namespace dvbt
#endif // MODEMDVB_DVBTOFDM_H

400
modemdvb/dvbtofdm_avx2.cpp

@ -0,0 +1,400 @@ @@ -0,0 +1,400 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// DVB-T OFDM demodulation's kernels, with AVX2 and FMA. Compiled with them enabled, so only used if the CPU has
// them. The convolutions work 32 floats (16 carriers) a block, in four or eight accumulators, enough to keep the
// FMAs busy. No library templates (std::complex, std::min) are used: code instantiated here, compiled for AVX2,
// could be shared with other files that run on any CPU
#include <cmath>
#include <immintrin.h>
#if defined(_MSC_VER)
#include <intrin.h>
#endif
#include "dvbtofdmkernel.h"
namespace dvbt {
int convolveAVX2(const float *x, const float *wc, int half, bool symmetric, int f, int end, float *y)
{
for (; f + 32 <= end; f += 32)
{
const float *xc = x + f;
const __m256 w0 = _mm256_set1_ps(wc[0]);
__m256 a0 = _mm256_mul_ps(w0, _mm256_loadu_ps(xc));
__m256 a1 = _mm256_mul_ps(w0, _mm256_loadu_ps(xc + 8));
__m256 a2 = _mm256_mul_ps(w0, _mm256_loadu_ps(xc + 16));
__m256 a3 = _mm256_mul_ps(w0, _mm256_loadu_ps(xc + 24));
if (symmetric)
{
for (int i = 1; i <= half; i++)
{
const __m256 w = _mm256_broadcast_ss(&wc[i]);
const float *lo = xc - 2 * i;
const float *hi = xc + 2 * i;
a0 = _mm256_fmadd_ps(w, _mm256_add_ps(_mm256_loadu_ps(lo), _mm256_loadu_ps(hi)), a0);
a1 = _mm256_fmadd_ps(w, _mm256_add_ps(_mm256_loadu_ps(lo + 8), _mm256_loadu_ps(hi + 8)), a1);
a2 = _mm256_fmadd_ps(w, _mm256_add_ps(_mm256_loadu_ps(lo + 16), _mm256_loadu_ps(hi + 16)), a2);
a3 = _mm256_fmadd_ps(w, _mm256_add_ps(_mm256_loadu_ps(lo + 24), _mm256_loadu_ps(hi + 24)), a3);
}
}
else
{
for (int i = -half; i <= half; i++)
{
if (i == 0) {
continue;
}
const __m256 w = _mm256_broadcast_ss(&wc[i]);
const float *xi = xc + 2 * i;
a0 = _mm256_fmadd_ps(w, _mm256_loadu_ps(xi), a0);
a1 = _mm256_fmadd_ps(w, _mm256_loadu_ps(xi + 8), a1);
a2 = _mm256_fmadd_ps(w, _mm256_loadu_ps(xi + 16), a2);
a3 = _mm256_fmadd_ps(w, _mm256_loadu_ps(xi + 24), a3);
}
}
_mm256_storeu_ps(y + f, a0);
_mm256_storeu_ps(y + f + 8, a1);
_mm256_storeu_ps(y + f + 16, a2);
_mm256_storeu_ps(y + f + 24, a3);
}
return f;
}
int convolve2AVX2(const float *x, const float *wc1, const float *wc2, int half, int f, int end, float *y1, float *y2)
{
for (; f + 32 <= end; f += 32)
{
const float *xc = x + f;
__m256 a0 = _mm256_setzero_ps(), a1 = a0, a2 = a0, a3 = a0;
__m256 b0 = a0, b1 = a0, b2 = a0, b3 = a0;
for (int i = -half; i <= half; i++)
{
const __m256 w1 = _mm256_broadcast_ss(&wc1[i]);
const __m256 w2 = _mm256_broadcast_ss(&wc2[i]);
const float *xi = xc + 2 * i;
const __m256 x0 = _mm256_loadu_ps(xi);
const __m256 x1 = _mm256_loadu_ps(xi + 8);
const __m256 x2 = _mm256_loadu_ps(xi + 16);
const __m256 x3 = _mm256_loadu_ps(xi + 24);
a0 = _mm256_fmadd_ps(w1, x0, a0);
a1 = _mm256_fmadd_ps(w1, x1, a1);
a2 = _mm256_fmadd_ps(w1, x2, a2);
a3 = _mm256_fmadd_ps(w1, x3, a3);
b0 = _mm256_fmadd_ps(w2, x0, b0);
b1 = _mm256_fmadd_ps(w2, x1, b1);
b2 = _mm256_fmadd_ps(w2, x2, b2);
b3 = _mm256_fmadd_ps(w2, x3, b3);
}
_mm256_storeu_ps(y1 + f, a0);
_mm256_storeu_ps(y1 + f + 8, a1);
_mm256_storeu_ps(y1 + f + 16, a2);
_mm256_storeu_ps(y1 + f + 24, a3);
_mm256_storeu_ps(y2 + f, b0);
_mm256_storeu_ps(y2 + f + 8, b1);
_mm256_storeu_ps(y2 + f + 16, b2);
_mm256_storeu_ps(y2 + f + 24, b3);
}
return f;
}
// Four complex products at once
static inline __m256 complexMultiply(__m256 a, __m256 b)
{
const __m256 swapped = _mm256_permute_ps(a, 0xb1); // Imaginary, real
return _mm256_fmaddsub_ps(a, _mm256_moveldup_ps(b), _mm256_mul_ps(swapped, _mm256_movehdup_ps(b)));
}
// One, (ar + j ai)(br + j bi), as std::complex<float> would
static inline void complexMultiply(float ar, float ai, float br, float bi, float& yr, float& yi)
{
yr = ar * br - ai * bi;
yi = ar * bi + ai * br;
}
// Eight carriers (interleaved complex) are loaded as carriers 0, 1 | 4, 5 and 2, 3 | 6, 7, so a horizontal add of
// the two gives a value per carrier in order, without moving data across the 128-bit halves, which is slow on
// efficiency cores
static inline __m256 load2(const float *lo, const float *hi)
{
return _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_loadu_ps(lo)), _mm_loadu_ps(hi), 1);
}
static inline void store2(float *lo, float *hi, __m256 v)
{
_mm_storeu_ps(lo, _mm256_castps256_ps128(v));
_mm_storeu_ps(hi, _mm256_extractf128_ps(v, 1));
}
static inline __m256 select(__m256 mask, __m256 a, __m256 b) // a where mask is set, else b
{
return _mm256_or_ps(_mm256_and_ps(mask, a), _mm256_andnot_ps(mask, b));
}
void rotateAVX2(const float *x, float *y, int n, const float *table, double phase, double blockStepRe,
double blockStepIm)
{
static const int block = 64;
double baseRe = std::cos(phase), baseIm = std::sin(phase); // As std::polar(1.0, phase)
for (int b = 0; b < n; b += block)
{
const int m = n - b < block ? n - b : block;
const float br = (float) baseRe, bi = (float) baseIm;
const __m256 bv = _mm256_setr_ps(br, bi, br, bi, br, bi, br, bi);
int i = 0;
for (; i + 4 <= m; i += 4)
{
const __m256 phasors = complexMultiply(_mm256_loadu_ps(table + 2 * i), bv);
_mm256_storeu_ps(y + 2 * (b + i), complexMultiply(_mm256_loadu_ps(x + 2 * (b + i)), phasors));
}
for (; i < m; i++)
{
float pr, pi;
complexMultiply(table[2 * i], table[2 * i + 1], br, bi, pr, pi);
complexMultiply(x[2 * (b + i)], x[2 * (b + i) + 1], pr, pi, y[2 * (b + i)], y[2 * (b + i) + 1]);
}
// base *= blockStep, as std::complex<double>
const double re = baseRe * blockStepRe - baseIm * blockStepIm;
baseIm = baseRe * blockStepIm + baseIm * blockStepRe;
baseRe = re;
}
}
void equaliseAVX2(const float *y, const float *h, float derotateRe, float derotateIm, int n, float *e, float *power)
{
const __m256 dv = _mm256_setr_ps(derotateRe, derotateIm, derotateRe, derotateIm, derotateRe, derotateIm,
derotateRe, derotateIm);
const __m256 conjugate = _mm256_castsi256_ps(_mm256_setr_epi32(0, (int) 0x80000000, 0, (int) 0x80000000, 0,
(int) 0x80000000, 0, (int) 0x80000000));
const __m256 one = _mm256_set1_ps(1.0f);
const __m256 zero = _mm256_setzero_ps();
int i = 0;
for (; i + 8 <= n; i += 8)
{
const float *yi = y + 2 * i;
const float *hi = h + 2 * i;
const __m256 h0 = load2(hi, hi + 8); // Carriers i, i+1 | i+4, i+5
const __m256 h1 = load2(hi + 4, hi + 12); // i+2, i+3 | i+6, i+7
const __m256 v0 = complexMultiply(complexMultiply(load2(yi, yi + 8), _mm256_xor_ps(h0, conjugate)), dv);
const __m256 v1 = complexMultiply(complexMultiply(load2(yi + 4, yi + 12), _mm256_xor_ps(h1, conjugate)), dv);
// |h|^2 in order. Duplicating each, low then high, gives the layout of v0 and v1
const __m256 p = _mm256_hadd_ps(_mm256_mul_ps(h0, h0), _mm256_mul_ps(h1, h1));
const __m256 inverse = _mm256_and_ps(_mm256_div_ps(one, p), _mm256_cmp_ps(p, zero, _CMP_GT_OQ));
float *ei = e + 2 * i;
store2(ei, ei + 8, _mm256_mul_ps(v0, _mm256_unpacklo_ps(inverse, inverse)));
store2(ei + 4, ei + 12, _mm256_mul_ps(v1, _mm256_unpackhi_ps(inverse, inverse)));
_mm256_storeu_ps(power + i, p);
}
for (; i < n; i++)
{
const float p = h[2 * i] * h[2 * i] + h[2 * i + 1] * h[2 * i + 1];
float ur, ui, vr, vi;
complexMultiply(y[2 * i], y[2 * i + 1], h[2 * i], -h[2 * i + 1], ur, ui);
complexMultiply(ur, ui, derotateRe, derotateIm, vr, vi);
const float inverse = p > 0.0f ? 1.0f / p : 0.0f;
e[2 * i] = vr * inverse;
e[2 * i + 1] = vi * inverse;
power[i] = p;
}
}
int updateNoiseAVX2(const float *e, const float *power, const float *mask, int n, float scale, float maxPoint,
float invScale2, float alpha, float *noise, float& total, float& count)
{
const __m256 sv = _mm256_set1_ps(scale);
const __m256 maxv = _mm256_set1_ps(maxPoint);
const __m256 minv = _mm256_set1_ps(-maxPoint);
const __m256 half = _mm256_set1_ps(0.5f);
const __m256 four = _mm256_set1_ps(4.0f);
const __m256i fouri = _mm256_set1_epi32(4);
const __m256 two = _mm256_set1_ps(2.0f);
const __m256 one = _mm256_set1_ps(1.0f);
const __m256 zero = _mm256_setzero_ps();
const __m256 av = _mm256_set1_ps(alpha);
const __m256 is2 = _mm256_set1_ps(invScale2);
__m256 totals = zero, counts = zero;
// The nearest point, 2 floor(x / 2) + 1, clamped. Adding 4 makes it positive, so truncation floors
auto squaredError = [&](__m256 x) {
x = _mm256_mul_ps(x, sv);
const __m256 c = _mm256_max_ps(minv, _mm256_min_ps(maxv, x));
const __m256i f = _mm256_sub_epi32(_mm256_cvttps_epi32(_mm256_fmadd_ps(c, half, four)), fouri);
const __m256 err = _mm256_sub_ps(x, _mm256_fmadd_ps(two, _mm256_cvtepi32_ps(f), one));
return _mm256_mul_ps(err, err);
};
int k = 0;
for (; k + 8 <= n; k += 8)
{
const float *ek = e + 2 * k;
const __m256 a = squaredError(load2(ek, ek + 8));
const __m256 b = squaredError(load2(ek + 4, ek + 12));
const __m256 measured = _mm256_mul_ps(_mm256_mul_ps(_mm256_hadd_ps(a, b), is2), _mm256_loadu_ps(power + k));
const __m256 old = _mm256_loadu_ps(noise + k);
const __m256 averaged = _mm256_fmadd_ps(av, _mm256_sub_ps(measured, old), old);
const __m256 updated = select(_mm256_cmp_ps(old, zero, _CMP_LT_OQ), measured, averaged);
const __m256 result = select(_mm256_cmp_ps(_mm256_loadu_ps(mask + k), zero, _CMP_GT_OQ), updated, old);
_mm256_storeu_ps(noise + k, result);
totals = _mm256_add_ps(totals, _mm256_max_ps(result, zero));
counts = _mm256_add_ps(counts, _mm256_and_ps(_mm256_cmp_ps(result, zero, _CMP_GE_OQ), one));
}
alignas(32) float t[8], c[8];
_mm256_store_ps(t, totals);
_mm256_store_ps(c, counts);
for (int i = 0; i < 8; i++)
{
total += t[i];
count += c[i];
}
return k;
}
int smoothNoiseAVX2(const float *noise, int n, float floor, float *smoothed, float *inverse)
{
const __m256 floorv = _mm256_set1_ps(floor);
const __m256 zero = _mm256_setzero_ps();
const __m256 one = _mm256_set1_ps(1.0f);
int j = 0;
for (; j + 8 <= n; j += 8)
{
__m256 s = zero, c = zero;
for (int d = -2; d <= 2; d++)
{
const __m256 x = _mm256_loadu_ps(noise + j + d);
s = _mm256_add_ps(s, _mm256_max_ps(x, zero));
c = _mm256_add_ps(c, _mm256_and_ps(_mm256_cmp_ps(x, zero, _CMP_GE_OQ), one));
}
// None measured gives the floor
const __m256 sm = _mm256_max_ps(floorv, _mm256_div_ps(s, _mm256_max_ps(c, one)));
_mm256_storeu_ps(smoothed + j, sm);
_mm256_storeu_ps(inverse + j, _mm256_div_ps(one, sm));
}
return j;
}
void windowAVX2(const float *x, const float *window, int n, float *out)
{
int j = 0;
for (; j + 4 <= n; j += 4)
{
const __m128 w = _mm_loadu_ps(window + j); // Each duplicated, for the real and imaginary parts
const __m256 wv = _mm256_insertf128_ps(_mm256_castps128_ps256(_mm_unpacklo_ps(w, w)), _mm_unpackhi_ps(w, w), 1);
_mm256_storeu_ps(out + 2 * j, _mm256_mul_ps(_mm256_loadu_ps(x + 2 * j), wv));
}
for (; j < n; j++)
{
out[2 * j] = x[2 * j] * window[j];
out[2 * j + 1] = x[2 * j + 1] * window[j];
}
}
float powerAVX2(const float *x, int n, float *power)
{
__m256 peaks = _mm256_setzero_ps();
int b = 0;
for (; b + 8 <= n; b += 8)
{
const float *xb = x + 2 * b;
const __m256 a = load2(xb, xb + 8);
const __m256 c = load2(xb + 4, xb + 12);
const __m256 p = _mm256_hadd_ps(_mm256_mul_ps(a, a), _mm256_mul_ps(c, c));
_mm256_storeu_ps(power + b, p);
peaks = _mm256_max_ps(peaks, p);
}
alignas(32) float m[8];
_mm256_store_ps(m, peaks);
float peak = 0.0f;
for (int i = 0; i < 8; i++) {
peak = m[i] > peak ? m[i] : peak;
}
for (; b < n; b++)
{
power[b] = x[2 * b] * x[2 * b] + x[2 * b + 1] * x[2 * b + 1];
peak = power[b] > peak ? power[b] : peak;
}
return peak;
}
static inline int lowestBit(unsigned int x)
{
#if defined(_MSC_VER)
unsigned long index;
_BitScanForward(&index, x);
return (int) index;
#else
return __builtin_ctz(x);
#endif
}
int findAboveAVX2(const float *values, int n, float threshold, int *indices)
{
const __m256 tv = _mm256_set1_ps(threshold);
int count = 0;
int b = 0;
for (; b + 8 <= n; b += 8)
{
unsigned int mask = (unsigned int) _mm256_movemask_ps(_mm256_cmp_ps(_mm256_loadu_ps(values + b), tv, _CMP_GE_OQ));
while (mask)
{
indices[count++] = b + lowestBit(mask);
mask &= mask - 1;
}
}
for (; b < n; b++)
{
if (values[b] >= threshold) {
indices[count++] = b;
}
}
return count;
}
} // namespace dvbt

58
modemdvb/dvbtofdmkernel.h

@ -0,0 +1,58 @@ @@ -0,0 +1,58 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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 MODEMDVB_DVBTOFDMKERNEL_H
#define MODEMDVB_DVBTOFDMKERNEL_H
// Internal to modemdvb: DVB-T OFDM demodulation's kernels, for instruction sets chosen at run time
namespace dvbt {
#if defined(MODEMDVB_X86)
//! Interior outputs of a centred convolution of complex values (as interleaved floats) with real taps: floats
//! [f, end) of y, from x, with taps wc[-half..half]. Symmetric taps (wc[-i] == wc[i]) are folded. Returns where it
//! stopped, as it works a block at a time, leaving the rest to the caller
int convolveAVX2(const float *x, const float *wc, int half, bool symmetric, int f, int end, float *y);
//! As convolveAVX2, with two sets of taps at once, into y1 and y2, sharing the loads
int convolve2AVX2(const float *x, const float *wc1, const float *wc2, int half, int f, int end, float *y1, float *y2);
//! y[i] = x[i] exp(j (phase + step i)) for n complex values (as interleaved floats), with table[i] = exp(j step i)
//! for i < 64, and blockStep exp(j 64 step)
void rotateAVX2(const float *x, float *y, int n, const float *table, double phase, double blockStepRe,
double blockStepIm);
//! Equalise n carriers: e = y conj(h) derotate / |h|^2 (0 where h is), power = |h|^2
void equaliseAVX2(const float *y, const float *h, float derotateRe, float derotateIm, int n, float *e, float *power);
//! The noise estimate's update, for carriers [0, n) of e (equalised, interleaved) with power |h|^2: where mask > 0,
//! noise (-1 if not yet measured) moves by alpha towards the squared distance to the nearest point (of a square
//! constellation, after scaling by scale, points up to +/-maxPoint) times invScale2 * power. Adds the sum and number
//! of the measured noise values (>= 0) to total and count. Returns the carriers done, a multiple of 8
int updateNoiseAVX2(const float *e, const float *power, const float *mask, int n, float scale, float maxPoint,
float invScale2, float alpha, float *noise, float& total, float& count);
//! The noise smoothed over carriers k - 2..k + 2 of those measured (noise[-2..n + 1] readable), at least floor, and
//! its inverse. Returns the carriers done, a multiple of 8
int smoothNoiseAVX2(const float *noise, int n, float floor, float *smoothed, float *inverse);
//! out = x (complex, interleaved) * window, for n values
void windowAVX2(const float *x, const float *window, int n, float *out);
//! power = |x|^2 for n complex values, returning the largest
float powerAVX2(const float *x, int n, float *power);
//! The indices of values >= threshold, in order, into indices (room for n), returning how many. No library
//! containers, as code instantiated here (compiled for AVX2) could be shared with other files
int findAboveAVX2(const float *values, int n, float threshold, int *indices);
#endif
} // namespace dvbt
#endif // MODEMDVB_DVBTOFDMKERNEL_H

386
modemdvb/dvbtouter.cpp

@ -0,0 +1,386 @@ @@ -0,0 +1,386 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <cstring>
#include "util/reedsolomon.h"
#include "dvbtouter.h"
namespace dvbt {
// RS(204,188, t=8) shortened from RS(255,239): p(x) = x^8 + x^4 + x^3 + x^2 + 1, roots alpha^0..alpha^15 (4.3.2)
const ReedSolomon::RS<255, 239> OuterDecoder::m_rs;
// Most packets have no errors, which the syndromes, the received polynomial at the roots, being 0 shows. They're
// found by Horner's rule with a table for multiplying by each root, 16 independent chains, so quickly; the full
// decoder (which finds them again) is only needed if one isn't 0
struct SyndromeTables
{
uint8_t m_multiply[16][256]; //!< By alpha^j
SyndromeTables()
{
auto multiply = [](int a, int b) {
int product = 0;
while (b)
{
if (b & 1) {
product ^= a;
}
a <<= 1;
if (a & 0x100) {
a ^= 0x11d;
}
b >>= 1;
}
return product;
};
int root = 1;
for (int j = 0; j < 16; j++)
{
for (int x = 0; x < 256; x++) {
m_multiply[j][x] = (uint8_t) multiply(x, root);
}
root = multiply(root, 2);
}
}
};
const SyndromeTables OuterDecoder::m_syndromeTables;
bool OuterDecoder::hasErrors(const uint8_t *packet)
{
uint8_t s[16] = {};
for (int i = 0; i < rsPacketSize; i++)
{
const uint8_t r = packet[i];
for (int j = 0; j < 16; j++) {
s[j] = m_syndromeTables.m_multiply[j][s[j]] ^ r;
}
}
uint8_t any = 0;
for (int j = 0; j < 16; j++) {
any |= s[j];
}
return any != 0;
}
// Energy dispersal PRBS 1 + x^14 + x^15, initialised to 100101010000000 at each inverted sync byte (4.3.1)
std::vector<uint8_t> OuterDecoder::makePRBS()
{
std::vector<uint8_t> prbs(8 * tsPacketSize);
unsigned int st = 0251; // Initialisation sequence, bit-reversed
for (int i = 1; i < 8 * tsPacketSize; i++)
{
uint8_t out = 0;
for (int n = 0; n < 8; n++)
{
unsigned int bit = ((st >> 13) ^ (st >> 14)) & 1;
out = (out << 1) | bit;
st = (st << 1) | bit;
}
prbs[i] = (i % tsPacketSize) ? out : 0; // Sync bytes are not randomized, but the PRBS keeps running
}
return prbs;
}
OuterDecoder::OuterDecoder() :
m_prbs(makePRBS())
{
for (int j = 0; j < m_interleaverI; j++) {
m_fifo[j].resize((m_interleaverI - 1 - j) * m_interleaverM);
}
reset();
}
void OuterDecoder::reset()
{
m_bits.clear();
m_synced = false;
m_bitCount = 0;
m_byte = 0;
m_bytePos = 0;
m_syncMisses = 0;
m_packetPos = 0;
m_packetsInDeinterleaver = 0;
m_prbsPacket = 0;
m_prbsSynced = false;
for (int j = 0; j < m_interleaverI; j++)
{
std::fill(m_fifo[j].begin(), m_fifo[j].end(), 0);
m_fifoPos[j] = 0;
}
}
void OuterDecoder::decode(const uint8_t *bits, int nbBits)
{
int i = 0;
while (i < nbBits)
{
if (!m_synced)
{
m_bits.insert(m_bits.end(), bits + i, bits + nbBits);
i = nbBits;
search();
}
else if ((m_bitCount == 0) && (nbBits - i >= 8))
{
// Whole bytes, packed with a multiply: bit k of the eight (one per byte, little endian) moves to bit 63 - k
while ((nbBits - i >= 8) && m_synced)
{
uint64_t x;
memcpy(&x, bits + i, 8);
i += 8;
processByte((uint8_t) (((x & 0x0101010101010101ULL) * 0x8040201008040201ULL) >> 56));
}
// Lost sync: search the remaining bits
if (!m_synced)
{
m_bits.assign(bits + i, bits + nbBits);
i = nbBits;
search();
}
}
else
{
m_byte = (m_byte << 1) | (bits[i++] & 1);
if (++m_bitCount == 8)
{
m_bitCount = 0;
processByte(m_byte);
// Lost sync: search the remaining bits
if (!m_synced)
{
m_bits.assign(bits + i, bits + nbBits);
i = nbBits;
search();
}
}
}
}
}
uint8_t OuterDecoder::getByte(const uint8_t *bits)
{
uint8_t byte = 0;
for (int i = 0; i < 8; i++) {
byte = (byte << 1) | bits[i];
}
return byte;
}
// Sync bytes are carried by interleaver branch 0, which has no delay in the transmitter,
// so they appear every 204 bytes in the received stream
void OuterDecoder::search()
{
const int packetBits = rsPacketSize * 8;
const int window = packetBits * (m_syncPackets - 1) + 8;
int start = 0; // Bits before it have been searched, and are discarded once at the end, not a packet at a time
while ((int) m_bits.size() - start >= window + packetBits)
{
const uint8_t *bits = m_bits.data() + start;
for (int offset = 0; offset < packetBits; offset++)
{
int p;
for (p = 0; p < m_syncPackets; p++)
{
uint8_t byte = getByte(&bits[offset + p * packetBits]);
if ((byte != m_syncByte) && (byte != m_invertedSyncByte)) {
break;
}
}
if (p == m_syncPackets)
{
std::vector<uint8_t> remaining(m_bits.begin() + start + offset, m_bits.end());
m_bits.clear();
m_synced = true;
m_bitCount = 0;
m_bytePos = 0;
m_syncMisses = 0;
m_packetPos = 0;
m_packetsInDeinterleaver = 0;
m_prbsSynced = false;
decode(remaining.data(), (int) remaining.size());
return;
}
}
start += packetBits;
}
m_bits.erase(m_bits.begin(), m_bits.begin() + start);
}
// Inline, as it's called for every byte (builds that only inline functions marked so wouldn't)
inline void OuterDecoder::processByte(uint8_t byte)
{
if (m_bytePos == 0)
{
if ((byte == m_syncByte) || (byte == m_invertedSyncByte))
{
m_syncMisses = 0;
}
else if (++m_syncMisses >= m_maxSyncMisses)
{
m_synced = false;
m_stats.m_syncLosses++;
return;
}
}
// De-interleave: branch j has delay (11 - j) * 17 bytes
int j = m_bytePos % m_interleaverI;
uint8_t out;
if (m_fifo[j].empty())
{
out = byte;
}
else
{
out = m_fifo[j][m_fifoPos[j]];
m_fifo[j][m_fifoPos[j]] = byte;
if (++m_fifoPos[j] == (int) m_fifo[j].size()) {
m_fifoPos[j] = 0;
}
}
m_packet[m_bytePos] = out;
if (++m_bytePos == rsPacketSize)
{
m_bytePos = 0;
if (m_packetsInDeinterleaver < m_interleaverI - 1) {
m_packetsInDeinterleaver++;
} else {
processPacket(m_packet);
}
}
}
void OuterDecoder::processPacket(uint8_t *packet)
{
uint8_t received[rsPacketSize];
int corrected = 0;
if (hasErrors(packet))
{
memcpy(received, packet, rsPacketSize);
corrected = m_rs.decode(packet, tsPacketSize, packet + tsPacketSize);
}
if (corrected < 0) {
m_stats.m_rsFailed++;
}
else if (corrected > 0)
{
m_stats.m_rsCorrected++;
m_stats.m_rsBytesCorrected += corrected;
for (int i = 0; i < rsPacketSize; i++)
{
for (uint8_t diff = received[i] ^ packet[i]; diff; diff &= diff - 1) {
m_stats.m_rsBitsCorrected++;
}
}
}
if (packet[0] == m_invertedSyncByte)
{
m_prbsPacket = 0;
m_prbsSynced = true;
}
else if (!m_prbsSynced)
{
return; // Can't de-randomize until the start of a group of 8 packets is known
}
packet[0] = m_syncByte;
const uint8_t *prbs = &m_prbs[m_prbsPacket * tsPacketSize];
for (int i = 1; i < tsPacketSize; i++) {
packet[i] ^= prbs[i];
}
m_prbsPacket = (m_prbsPacket + 1) % 8;
if (corrected < 0) {
packet[1] |= 0x80; // transport_error_indicator
}
m_stats.m_packets++;
if (m_callback) {
m_callback(packet);
}
}
void OuterDecoder::randomize(uint8_t packets[8][tsPacketSize])
{
static const std::vector<uint8_t> prbs = makePRBS();
for (int p = 0; p < 8; p++)
{
packets[p][0] = p == 0 ? m_invertedSyncByte : m_syncByte;
for (int i = 1; i < tsPacketSize; i++) {
packets[p][i] ^= prbs[p * tsPacketSize + i];
}
}
}
void OuterDecoder::rsEncode(const uint8_t *packet, uint8_t *rsPacket)
{
memcpy(rsPacket, packet, tsPacketSize);
m_rs.encode(packet, tsPacketSize, rsPacket + tsPacketSize);
}
} // namespace dvbt

120
modemdvb/dvbtouter.h

@ -0,0 +1,120 @@ @@ -0,0 +1,120 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBTOUTER_H
#define MODEMDVB_DVBTOUTER_H
#include <cstddef>
#include <cstdint>
#include <functional>
#include <vector>
#include "dvbt.h"
namespace ReedSolomon {
template <std::size_t SYMBOLS, std::size_t PAYLOAD>
struct RS;
}
namespace dvbt {
struct SyndromeTables;
// Outer decoding (4.3.1 and 4.3.2), from the bit stream out of the Viterbi decoder:
// find the packet sync bytes, outer (Forney) de-interleave, RS(204,188) decode, de-randomize
class MODEMDVB_API OuterDecoder
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback; //!< 188-byte TS packet
struct Stats
{
uint64_t m_packets; //!< TS packets output
uint64_t m_rsCorrected; //!< Packets that RS corrected
uint64_t m_rsBytesCorrected;
uint64_t m_rsBitsCorrected; //!< In packets RS corrected, so BER after Viterbi is about this / (m_packets * 204 * 8)
uint64_t m_rsFailed; //!< Packets RS could not correct, output with transport_error_indicator set
uint64_t m_syncLosses;
Stats() { clear(); }
void clear()
{
m_packets = 0;
m_rsCorrected = 0;
m_rsBytesCorrected = 0;
m_rsBitsCorrected = 0;
m_rsFailed = 0;
m_syncLosses = 0;
}
};
OuterDecoder();
void setPacketCallback(PacketCallback callback) { m_callback = callback; }
void reset();
void decode(const uint8_t *bits, int nbBits); //!< One bit per byte, in transmission order
bool synced() const { return m_synced; }
const Stats& getStats() const { return m_stats; }
// For the test transmitter
static void randomize(uint8_t packets[8][tsPacketSize]); //!< Energy dispersal on a group of 8 packets
static void rsEncode(const uint8_t *packet, uint8_t *rsPacket);
private:
void search();
void processByte(uint8_t byte);
void processPacket(uint8_t *packet);
static constexpr int m_syncPackets = 4; //!< Sync bytes required to sync
static constexpr int m_maxSyncMisses = 8; //!< Consecutive packets without a sync byte before losing sync
static constexpr int m_interleaverI = 12;
static constexpr int m_interleaverM = 17;
static constexpr uint8_t m_syncByte = 0x47;
static constexpr uint8_t m_invertedSyncByte = 0xb8;
static const ReedSolomon::RS<255, 239> m_rs;
static const SyndromeTables m_syndromeTables;
static bool hasErrors(const uint8_t *packet);
static std::vector<uint8_t> makePRBS();
static uint8_t getByte(const uint8_t *bits);
PacketCallback m_callback;
Stats m_stats;
std::vector<uint8_t> m_bits; //!< Bits being searched for sync
bool m_synced;
int m_bitCount;
uint8_t m_byte;
int m_bytePos; //!< Byte position in the 204-byte packet
int m_syncMisses;
std::vector<uint8_t> m_fifo[m_interleaverI];
int m_fifoPos[m_interleaverI];
int m_packetsInDeinterleaver; //!< Until it has filled, its output is meaningless
uint8_t m_packet[rsPacketSize];
int m_packetPos;
std::vector<uint8_t> m_prbs; //!< 8 * 188 bytes, 0 at sync byte positions
int m_prbsPacket; //!< Packet within the group of 8
bool m_prbsSynced; //!< An inverted sync byte has been seen
};
} // namespace dvbt
#endif // MODEMDVB_DVBTOUTER_H

703
modemdvb/dvbtreceiver.cpp

@ -0,0 +1,703 @@ @@ -0,0 +1,703 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <chrono>
#include <cmath>
#include <iterator>
#include "util/profiler.h"
#include "dvbtreceiver.h"
#include "samplethread.h"
namespace dvbt {
const double Receiver::m_clockCorrectionThreshold = 3.0;
const double Receiver::m_viterbiAddLoad = 0.85;
const double Receiver::m_viterbiRemoveLoad = 0.6;
double Receiver::now()
{
return std::chrono::duration<double>(std::chrono::steady_clock::now().time_since_epoch()).count();
}
Receiver::Receiver(int bandwidthHz, FFTFactory *fftFactory, bool threaded) :
m_outputRate(sampleRate(bandwidthHz)),
m_inputRate(sampleRate(bandwidthHz)),
m_resampling(false),
m_samplesPushed(0),
m_samplesDemodulated(0),
m_clockUpdateSymbols(0),
m_ofdm(bandwidthHz, fftFactory),
m_ofdmSeconds(0.0),
m_displayRequested(false),
m_displayMaxCells(0),
m_displayPoints(0),
m_threaded(threaded),
m_innerBusy(false),
m_busy(false),
m_stop(false),
m_queueWaits(0)
{
m_ofdm.setSymbolCallback([this](const OFDMDemod::Symbol& symbol) {
onSymbol(symbol);
});
m_fec.setPacketCallback([this](const uint8_t *packet) {
onPacket(packet);
});
m_ofdmStatus = m_ofdm.getStatus();
m_canceller.configure(m_inputRate, 0.83 * m_outputRate); // The occupied bandwidth, 7.61 MHz in 8
if (m_threaded)
{
m_thread = std::thread(&Receiver::runFEC, this);
m_innerThread = std::thread(&Receiver::runInner, this);
m_demodThread.reset(new SampleThread([this](const Cf *samples, int nbSamples) {
demodulate(samples, nbSamples);
}));
}
}
Receiver::~Receiver()
{
m_demodThread.reset(); // Before error correction, which it feeds
if (m_threaded)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_stop = true;
}
m_symbolAvailable.notify_all();
m_spaceAvailable.notify_all();
m_demappedAvailable.notify_all();
m_demappedSpace.notify_all();
m_innerThread.join();
m_thread.join();
}
}
void Receiver::reset()
{
if (m_demodThread) {
m_demodThread->discard();
}
m_ofdm.reset();
m_clockUpdateSymbols = 0;
{
std::lock_guard<std::mutex> lock(m_ofdmMutex);
m_ofdmStatus = m_ofdm.getStatus();
m_displayCells.clear();
m_displayResponse.clear();
m_clockAdjustments.clear();
m_samplesPushed = 0;
m_samplesDemodulated = 0;
// The notches the samples from now on are notched with (the canceller keeps them)
m_notchChanges.clear();
m_notchFrequencies.clear();
for (const auto& notch : m_canceller.getNotches()) {
m_notchFrequencies.push_back(notch.m_frequency * m_inputRate);
}
m_notchesQueued = m_notchFrequencies;
}
m_ofdm.setNotches(m_notchesQueued, m_inputRate); // The demodulator thread isn't running
if (m_threaded)
{
// Discard what is queued, and have the worker reset error correction before anything else
std::lock_guard<std::mutex> lock(m_mutex);
while (!m_queue.empty())
{
m_free.push_back(std::move(m_queue.front()));
m_queue.pop_front();
}
while (!m_demappedQueue.empty())
{
m_demappedFree.push_back(std::move(m_demappedQueue.front()));
m_demappedQueue.pop_front();
}
m_demappedSpace.notify_all();
// Packets from symbols already being decoded are dropped until this reset has passed through decoding
QueuedSymbol resetSymbol;
resetSymbol.m_reset = true;
resetSymbol.m_index = 0;
resetSymbol.m_epoch = ++m_resetEpoch;
m_queue.push_back(std::move(resetSymbol));
m_packets.clear();
m_symbolAvailable.notify_one();
}
else
{
m_fec.reset();
}
}
void Receiver::setInputSampleRate(double sampleRate)
{
{
std::lock_guard<std::mutex> lock(m_ofdmMutex); // The demodulator thread reads it
m_inputRate = sampleRate;
}
m_canceller.configure(sampleRate, 0.83 * m_outputRate);
m_resampling = std::abs(sampleRate / m_outputRate - 1.0) > 1e-6;
m_resampler.setRatioCorrection(1.0);
if (m_resampling) {
m_resampler.configure(m_inputRate, m_outputRate, 0.4165 * m_outputRate); // Half the occupied bandwidth
}
reset();
}
void Receiver::process(const Cf *samples, int nbSamples)
{
PROFILER_START()
double start = now();
// Strong narrowband interferers notched out first
samples = m_canceller.process(samples, nbSamples, m_notched);
const Cf *input = samples;
int nbInput = nbSamples;
if (m_resampling)
{
m_resampled.clear();
m_resampler.process(samples, nbSamples, m_resampled);
input = m_resampled.data();
nbInput = (int) m_resampled.size();
}
else
{
int keep = std::min(nbSamples, 64);
m_lastInput.assign(samples + nbSamples - keep, samples + nbSamples);
}
{
std::lock_guard<std::mutex> lock(m_ofdmMutex);
m_ofdmSeconds += now() - start;
m_notchFrequencies.clear();
for (const auto& notch : m_canceller.getNotches()) {
m_notchFrequencies.push_back(notch.m_frequency * m_inputRate);
}
// The canceller notches all of this call's samples with them, so they apply from its first
if (m_notchFrequencies != m_notchesQueued)
{
m_notchesQueued = m_notchFrequencies;
m_notchChanges.push_back({m_samplesPushed, m_notchFrequencies});
}
}
PROFILER_STOP("DVB-T resampling")
if (m_demodThread) {
m_demodThread->push(input, nbInput);
} else {
demodulate(input, nbInput);
}
m_samplesPushed += nbInput;
correctClock();
PROFILER_RESTART()
deliverPackets();
PROFILER_STOP("DVB-T packet delivery")
}
// OFDM demodulation, and what other threads need of it
void Receiver::demodulate(const Cf *samples, int nbSamples)
{
PROFILER_START()
double start = now();
// Clock corrections and notches from these samples on
{
std::lock_guard<std::mutex> lock(m_ofdmMutex);
while (!m_clockAdjustments.empty() && (m_clockAdjustments.front().m_sample <= m_samplesDemodulated))
{
m_ofdm.adjustClockOffset(m_clockAdjustments.front().m_ppm);
m_clockAdjustments.pop_front();
}
while (!m_notchChanges.empty() && (m_notchChanges.front().m_sample <= m_samplesDemodulated))
{
m_ofdm.setNotches(m_notchChanges.front().m_frequencies, m_inputRate);
m_notchChanges.pop_front();
}
}
m_samplesDemodulated += nbSamples;
m_ofdm.process(samples, nbSamples);
OFDMDemod::Status status = m_ofdm.getStatus();
{
std::lock_guard<std::mutex> lock(m_ofdmMutex);
m_ofdmStatus = status;
m_ofdmSeconds += now() - start;
if (m_displayRequested)
{
m_ofdm.getDisplayData(m_displayCells, m_displayMaxCells, m_displayResponse, m_displayPoints);
m_ofdm.getImpulseResponse(m_displayImpulse, m_displayPoints);
m_displayRequested = false;
}
}
PROFILER_STOP("DVB-T demodulation")
}
OFDMDemod::Status Receiver::ofdmStatus() const
{
std::lock_guard<std::mutex> lock(m_ofdmMutex);
return m_demodThread ? m_ofdmStatus : m_ofdm.getStatus();
}
void Receiver::getDisplayData(std::vector<Cf>& cells, int maxCells, std::vector<float>& responseDb, int points) const
{
if (!m_demodThread)
{
m_displayPoints = points; // For getImpulseResponse()
m_ofdm.getDisplayData(cells, maxCells, responseDb, points);
return;
}
std::lock_guard<std::mutex> lock(m_ofdmMutex);
cells = m_displayCells;
responseDb = m_displayResponse;
m_displayRequested = true;
m_displayMaxCells = maxCells;
m_displayPoints = points;
}
void Receiver::getImpulseResponse(ImpulseResponse& response) const
{
if (!m_demodThread)
{
m_ofdm.getImpulseResponse(response, std::max(1, m_displayPoints));
return;
}
std::lock_guard<std::mutex> lock(m_ofdmMutex);
response = m_displayImpulse;
}
// Trim the resampling ratio by the measured clock offset, starting resampling if needed
void Receiver::correctClock()
{
OFDMDemod::Status status = ofdmStatus();
// First once the offset has been averaged over a few symbols, as large offsets spoil reception until corrected
// (from 40 ppm in 8K, by interference between the carriers). Then every so often
if ((status.m_state < OFDMDemod::TPSSync) || (status.m_clockMeasurements < m_clockFirstMeasurements)
|| ((m_clockUpdateSymbols > 0) && (status.m_symbols < m_clockUpdateSymbols + m_clockCorrectionPeriod))) {
return;
}
const bool first = m_clockUpdateSymbols == 0;
m_clockUpdateSymbols = status.m_symbols;
double ppm = status.m_clockOffset;
if (!m_resampling)
{
if (std::abs(ppm) < m_clockCorrectionThreshold) {
return;
}
m_resampling = true;
m_resampler.setRatioCorrection(1.0);
m_resampler.configure(m_inputRate, m_outputRate, 0.4165 * m_outputRate);
m_resampler.prime(m_lastInput);
setRatioCorrection(1.0 - ppm * 1e-6); // The measurement is accurate, so start with all of it
return;
}
// A positive offset means each sample spans more time than it should, so take fewer input samples per output
// All of the first measurement, as when resampling starts, also when the input was resampled from another rate
setRatioCorrection(m_resampler.getRatioCorrection() * (1.0 - (first ? 1.0 : 0.5) * ppm * 1e-6));
}
// And the demodulator's clock offset changes with it, from the samples resampled with it
void Receiver::setRatioCorrection(double ratio)
{
const double change = (ratio - m_resampler.getRatioCorrection()) * 1e6;
m_resampler.setRatioCorrection(ratio);
std::lock_guard<std::mutex> lock(m_ofdmMutex);
m_clockAdjustments.push_back({m_samplesPushed, change});
}
void Receiver::onSymbol(const OFDMDemod::Symbol& symbol)
{
if (m_symbolObserver) {
m_symbolObserver(symbol);
}
if (!m_threaded)
{
m_fec.decode(symbol.m_index, *symbol.m_tps, symbol.m_cells, symbol.m_csi, symbol.m_nbCells);
return;
}
// Copied (about 72 kB in 8K) before taking the lock, which the other threads take too, into a buffer they've
// finished with
m_filling.m_reset = false;
m_filling.m_index = symbol.m_index;
m_filling.m_tps = *symbol.m_tps;
m_filling.m_cells.assign(symbol.m_cells, symbol.m_cells + symbol.m_nbCells);
m_filling.m_csi.assign(symbol.m_csi, symbol.m_csi + symbol.m_nbCells);
std::unique_lock<std::mutex> lock(m_mutex);
// Wait rather than drop symbols, so decoding a recording is lossless. In real time, error correction normally
// keeps up easily, so this only happens on overload
if (m_queue.size() >= m_maxQueuedSymbols)
{
m_queueWaits++;
PROFILER_START()
m_spaceAvailable.wait(lock, [this] { return m_stop || (m_queue.size() < m_maxQueuedSymbols / 2); });
PROFILER_STOP("DVB-T demodulation waiting for FEC")
}
m_queue.push_back(std::move(m_filling));
if (!m_free.empty())
{
m_filling = std::move(m_free.back());
m_free.pop_back();
}
else
{
m_filling = QueuedSymbol();
}
lock.unlock();
m_symbolAvailable.notify_one();
}
// In the worker thread, if threaded
void Receiver::onPacket(const uint8_t *packet)
{
if (!m_threaded)
{
if (m_packetCallback) {
m_packetCallback(packet);
}
return;
}
// Gathered without the lock, and added to m_packets after the call that decoded them
m_fecPackets.insert(m_fecPackets.end(), packet, packet + tsPacketSize);
}
// Inner de-interleaving and de-mapping, in its own thread, as the first stage of error correction
void Receiver::runInner()
{
std::unique_lock<std::mutex> lock(m_mutex);
while (true)
{
m_symbolAvailable.wait(lock, [this] { return m_stop || !m_queue.empty(); });
if (m_stop) {
break;
}
QueuedSymbol symbol = std::move(m_queue.front());
m_queue.pop_front();
QueuedDemapped demapped;
if (!m_demappedFree.empty())
{
demapped = std::move(m_demappedFree.back());
m_demappedFree.pop_back();
}
m_innerBusy = true;
lock.unlock();
m_spaceAvailable.notify_one();
PROFILER_START()
demapped.m_reset = symbol.m_reset;
demapped.m_epoch = symbol.m_epoch;
if (symbol.m_reset)
{
m_fec.resetDemap();
}
else
{
// An exception (out of memory) loses the symbol, not the process
try
{
m_fec.demap(symbol.m_index, symbol.m_tps, symbol.m_cells.data(), symbol.m_csi.data(),
(int) symbol.m_cells.size(), demapped.m_demapped);
}
catch (const std::exception&)
{
// Decoding starts again after the gap
demapped.m_demapped.m_llrs.clear();
demapped.m_demapped.m_reconfigured = true;
}
}
PROFILER_STOP("DVB-T inner")
lock.lock();
m_free.push_back(std::move(symbol));
// The buffers an overload queued up (up to 37 MB) aren't all kept once it's over
if (m_queue.empty() && (m_free.size() > m_maxFreeSymbols))
{
std::vector<QueuedSymbol> excess(std::make_move_iterator(m_free.begin() + m_maxFreeSymbols),
std::make_move_iterator(m_free.end()));
m_free.resize(m_maxFreeSymbols);
lock.unlock();
excess.clear(); // Freed without the lock
lock.lock();
}
if (m_demappedQueue.size() >= m_maxQueuedDemapped)
{
m_queueWaits++;
PROFILER_START()
m_demappedSpace.wait(lock, [this] { return m_stop || (m_demappedQueue.size() < m_maxQueuedDemapped / 2); });
PROFILER_STOP("DVB-T inner waiting for FEC")
if (m_stop) {
break;
}
}
m_demappedQueue.push_back(std::move(demapped));
m_innerBusy = false;
m_demappedAvailable.notify_one();
}
}
void Receiver::setViterbiThreads(int threads)
{
std::lock_guard<std::mutex> lock(m_mutex); // Not to cross with runFEC() changing them
m_autoViterbi = false;
m_fec.setViterbiThreads(threads);
}
// Viterbi and outer decoding, in its own thread, after de-mapping
void Receiver::runFEC()
{
std::unique_lock<std::mutex> lock(m_mutex);
m_fecWindowStart = std::chrono::steady_clock::now();
// The OFDM, inner and this thread each take a core
const int maxViterbiThreads = std::min(3, std::max(0, (int) std::thread::hardware_concurrency() - 3));
while (true)
{
m_demappedAvailable.wait(lock, [this] { return m_stop || !m_demappedQueue.empty(); });
if (m_stop) {
break;
}
QueuedDemapped demapped = std::move(m_demappedQueue.front());
m_demappedQueue.pop_front();
m_busy = true;
lock.unlock();
m_demappedSpace.notify_one();
PROFILER_START()
const auto start = std::chrono::steady_clock::now();
if (demapped.m_reset)
{
m_fec.resetDecode();
lock.lock();
m_decodeEpoch = demapped.m_epoch; // Packets are wanted again, if there's been no reset since
lock.unlock();
}
else
{
try {
m_fec.decodeDemapped(demapped.m_demapped);
} catch (const std::exception&) {
// Lost, not the process
}
}
PROFILER_STOP("DVB-T FEC")
const auto end = std::chrono::steady_clock::now();
m_fecBusy += std::chrono::duration<double>(end - start).count();
FECDecoder::Status status = m_fec.getStatus();
lock.lock();
// The packets from this call, unless there's been a reset since
if (!m_fecPackets.empty())
{
if (m_decodeEpoch == m_resetEpoch) {
m_packets.insert(m_packets.end(), m_fecPackets.begin(), m_fecPackets.end());
}
m_fecPackets.clear();
}
// On a slow CPU, this thread may not keep up: busy nearly all the time, with symbols waiting for it. Then
// Viterbi decoding is shared with more threads, one at a time. When it would be comfortably under that with
// one fewer, for a while (as after a start-up hiccup), one goes. Either way the output is the same
const double window = std::chrono::duration<double>(end - m_fecWindowStart).count();
if (window >= 2.0)
{
const double viterbi = std::max(0.0, status.m_seconds[1] - m_fecViterbiSeconds);
m_fecViterbiSeconds = status.m_seconds[1];
if (m_autoViterbi)
{
if ((m_fecBusy > m_viterbiAddLoad * window) && (m_demappedQueue.size() >= m_maxQueuedDemapped / 2)
&& (m_viterbiThreads < maxViterbiThreads))
{
m_fec.setViterbiThreads(++m_viterbiThreads);
m_fecQuietWindows = 0;
}
else if (m_viterbiThreads > 0)
{
// With n threads besides this one, each decodes about 1 / (n + 1) of the steps, so with one fewer
// the Viterbi time here would be about (n + 1) / n times as long
const double fewer = m_fecBusy + viterbi / m_viterbiThreads;
if (fewer >= m_viterbiRemoveLoad * window) {
m_fecQuietWindows = 0;
} else if (++m_fecQuietWindows >= m_viterbiRemoveWindows) {
m_fec.setViterbiThreads(--m_viterbiThreads);
m_fecQuietWindows = 0;
}
}
}
m_fecBusy = 0.0;
m_fecWindowStart = end;
}
m_fecStatus = status;
m_demappedFree.push_back(std::move(demapped));
m_busy = false;
if (idle()) {
m_idle.notify_all();
}
}
}
// With m_mutex locked
bool Receiver::idle() const
{
return m_queue.empty() && !m_innerBusy && m_demappedQueue.empty() && !m_busy;
}
void Receiver::deliverPackets()
{
if (!m_threaded) {
return;
}
{
std::lock_guard<std::mutex> lock(m_mutex);
m_delivering.swap(m_packets);
}
if (m_packetCallback)
{
for (size_t i = 0; i + tsPacketSize <= m_delivering.size(); i += tsPacketSize) {
m_packetCallback(&m_delivering[i]);
}
}
m_delivering.clear();
}
void Receiver::flush()
{
if (m_demodThread) {
m_demodThread->drain();
}
if (m_threaded)
{
std::unique_lock<std::mutex> lock(m_mutex);
m_idle.wait(lock, [this] { return idle(); });
}
deliverPackets();
}
Receiver::Status Receiver::getStatus() const
{
Status status;
status.m_ofdm = ofdmStatus();
status.m_ofdm.m_clockOffset += (1.0 - m_resampler.getRatioCorrection()) * 1e6; // Include the correction applied
FECDecoder::Status fec;
if (m_threaded)
{
std::lock_guard<std::mutex> lock(m_mutex);
// Until a reset's passed through decoding, the FEC's status is the signal's before (in Auto, its TS sync would
// be taken for this one's)
if (m_decodeEpoch == m_resetEpoch) {
fec = m_fecStatus;
}
status.m_queueWaits = m_queueWaits;
}
else
{
fec = m_fec.getStatus();
status.m_queueWaits = 0;
}
status.m_outer = fec.m_outer;
status.m_tsSync = fec.m_tsSync;
status.m_supported = fec.m_supported;
status.m_lowPriority = fec.m_lowPriority;
status.m_mer = fec.m_mer;
status.m_preViterbiBER = fec.m_preViterbiBER;
status.m_postViterbiBER = fec.m_postViterbiBER;
status.m_viterbiBits = fec.m_viterbiBits;
{
std::lock_guard<std::mutex> lock(m_ofdmMutex);
status.m_seconds[0] = m_ofdmSeconds;
status.m_notches = m_notchFrequencies;
}
std::copy(fec.m_seconds, fec.m_seconds + 4, status.m_seconds + 1);
return status;
}
} // namespace dvbt

224
modemdvb/dvbtreceiver.h

@ -0,0 +1,224 @@ @@ -0,0 +1,224 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_DVBTRECEIVER_H
#define MODEMDVB_DVBTRECEIVER_H
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstdint>
#include <deque>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "dvbt.h"
#include "dvbtofdm.h"
#include "dvbtfec.h"
#include "cwcanceller.h"
#include "resampler.h"
class SampleThread;
namespace dvbt {
// Complete DVB-T receiver: baseband samples in, MPEG transport stream packets out.
// If threaded, resampling runs in the caller's thread, OFDM demodulation in a thread of its own, inner
// de-interleaving and de-mapping in another, and the rest of error correction (Viterbi and outer decoding) in a third,
// with packets handed back to the caller's thread at the end of each call to process(). Otherwise all in the caller's
// thread
class MODEMDVB_API Receiver
{
public:
typedef std::function<void(const uint8_t *packet)> PacketCallback; //!< 188-byte TS packet
struct Status
{
OFDMDemod::Status m_ofdm;
OuterDecoder::Stats m_outer;
bool m_tsSync = false;
bool m_supported = true; //!< False for an invalid mode
bool m_lowPriority = false; //!< Decoding a hierarchical mode's low priority stream
double m_mer = 0.0; //!< dB
double m_preViterbiBER = 0.0; //!< Recent average, from re-encoding the decoded bits
double m_postViterbiBER = 0.0; //!< Recent average, from the bits Reed-Solomon corrects. QEF needs < 2e-4
uint64_t m_viterbiBits = 0;
uint64_t m_queueWaits = 0; //!< Times demodulation or de-mapping waited for the next stage to catch up
std::vector<double> m_notches; //!< Frequencies of narrowband interferers notched out, Hz from the centre
double m_seconds[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; //!< Processing time: OFDM (and resampling), inner, Viterbi, BER, outer
};
//! FFT engines come from fftFactory if not null
explicit Receiver(int bandwidthHz = 8000000, FFTFactory *fftFactory = nullptr, bool threaded = true);
~Receiver();
void setPacketCallback(PacketCallback callback) { m_packetCallback = callback; }
void setLogCallback(OFDMDemod::LogCallback callback) { m_ofdm.setLogCallback(callback); }
//! Also called with each demodulated symbol, from the thread that calls process(). For analysis
void setSymbolObserver(OFDMDemod::SymbolCallback callback) { m_symbolObserver = callback; }
void reset();
//! Input sample rate, if not sampleRate(bandwidth). Between about 0.8 and 2.2 times that
void setInputSampleRate(double sampleRate);
//! Notching out strong narrowband interferers, on by default. From the thread that calls process()
void setNotchEnabled(bool enabled) { m_canceller.setEnabled(enabled); }
//! In hierarchical modes, decode the low priority stream rather than the high priority one
void setLowPriority(bool lowPriority) { m_fec.setLowPriority(lowPriority); }
//! Threads sharing Viterbi decoding with the error correction thread, for slow CPUs. By default, they're added
//! while that thread is busy over 85 % of the time (up to 3, and the cores the other threads leave). From
//! any thread, which stops them being added
void setViterbiThreads(int threads);
void process(const Cf *samples, int nbSamples);
void flush(); //!< Wait for demodulation and error correction to finish what is queued, and output the packets
Status getStatus() const;
//! Only to be used after flush()
const OFDMDemod& getOFDMDemod() const { return m_ofdm; }
//! As OFDMDemod::getDisplayData(). If threaded, from the demodulator a moment ago (none the first time)
void getDisplayData(std::vector<Cf>& cells, int maxCells, std::vector<float>& responseDb, int points) const;
//! As OFDMDemod::getImpulseResponse(), from when getDisplayData() was last satisfied
void getImpulseResponse(ImpulseResponse& response) const;
private:
struct QueuedSymbol
{
bool m_reset; //!< Reset error correction, rather than decode
uint64_t m_epoch = 0; //!< Of the reset
int m_index;
TPS m_tps;
std::vector<Cf> m_cells;
std::vector<float> m_csi;
};
struct QueuedDemapped
{
bool m_reset; //!< Reset decoding, rather than decode
uint64_t m_epoch = 0; //!< Of the reset
FECDecoder::Demapped m_demapped;
};
void demodulate(const Cf *samples, int nbSamples);
OFDMDemod::Status ofdmStatus() const;
void onSymbol(const OFDMDemod::Symbol& symbol);
void onPacket(const uint8_t *packet);
void runInner();
void runFEC();
bool idle() const;
void deliverPackets();
void correctClock();
static constexpr size_t m_maxQueuedSymbols = 512;
static constexpr size_t m_maxFreeSymbols = 16; //!< Kept for reuse once the queue empties (about 72 kB each)
static constexpr size_t m_maxQueuedDemapped = 32; //!< About 48 kB each
// Clock offsets beyond this are corrected by resampling, rather than just by moving the FFT window,
// as they reduce the MER (by a few dB at 20 ppm in 8K mode)
static const double m_clockCorrectionThreshold; //!< ppm
static constexpr uint64_t m_clockCorrectionPeriod = 256; //!< Symbols
static constexpr int m_clockFirstMeasurements = 20; //!< Symbols the clock offset is averaged over, to correct it first
// Viterbi threads are added while the error correction thread is busy over this fraction of the time, with symbols
// waiting for it, and removed when it would have been under the lower fraction with one fewer, for a few windows
static const double m_viterbiAddLoad;
static const double m_viterbiRemoveLoad;
static constexpr int m_viterbiRemoveWindows = 3; //!< Of 2 s
static double now();
PacketCallback m_packetCallback;
OFDMDemod::SymbolCallback m_symbolObserver;
double m_outputRate; //!< sampleRate(bandwidth)
double m_inputRate;
bool m_resampling;
Resampler m_resampler;
CWCanceller m_canceller;
std::vector<Cf> m_notched;
std::vector<double> m_notchFrequencies; //!< With m_ofdmMutex
// Notches reach the demodulator with the samples they apply to, as clock corrections do, as the canceller's
// output is notched with them from the start of the call to process() that changed them
struct NotchChange
{
uint64_t m_sample; //!< From this sample pushed to the demodulator
std::vector<double> m_frequencies;
};
std::deque<NotchChange> m_notchChanges; //!< With m_ofdmMutex
std::vector<double> m_notchesQueued; //!< The last in m_notchChanges, or given the demodulator
std::vector<Cf> m_resampled;
std::vector<Cf> m_lastInput; //!< Recent input, to start resampling seamlessly
// Clock corrections reach the demodulator with the samples they apply to, so its estimate follows them
struct ClockAdjustment
{
uint64_t m_sample; //!< From this sample pushed to the demodulator
double m_ppm;
};
uint64_t m_samplesPushed;
uint64_t m_samplesDemodulated;
std::deque<ClockAdjustment> m_clockAdjustments; //!< With m_ofdmMutex
void setRatioCorrection(double ratio);
uint64_t m_clockUpdateSymbols; //!< When the clock correction was last updated
OFDMDemod m_ofdm;
double m_ofdmSeconds;
// Demodulation, in its own thread if threaded, with what's needed of it from other threads
std::unique_ptr<SampleThread> m_demodThread;
mutable std::mutex m_ofdmMutex; //!< For these
OFDMDemod::Status m_ofdmStatus;
mutable bool m_displayRequested;
mutable int m_displayMaxCells;
mutable int m_displayPoints;
std::vector<Cf> m_displayCells;
std::vector<float> m_displayResponse;
ImpulseResponse m_displayImpulse;
// Error correction, in two worker threads if threaded: de-mapping, then decoding
bool m_threaded;
FECDecoder m_fec;
std::thread m_innerThread;
std::thread m_thread;
mutable std::mutex m_mutex; //!< For the queues and status below
std::condition_variable m_symbolAvailable;
std::condition_variable m_idle;
std::condition_variable m_spaceAvailable;
std::condition_variable m_demappedAvailable;
std::condition_variable m_demappedSpace;
std::deque<QueuedSymbol> m_queue;
std::vector<QueuedSymbol> m_free;
QueuedSymbol m_filling; //!< The demodulator thread's next symbol for the queue, filled without m_mutex
std::deque<QueuedDemapped> m_demappedQueue;
std::vector<QueuedDemapped> m_demappedFree;
bool m_innerBusy;
bool m_busy;
// Viterbi threads added as the error correction thread's load needs them
std::atomic<bool> m_autoViterbi{true};
int m_viterbiThreads = 0;
double m_fecBusy = 0.0; //!< Seconds decoding since m_fecWindowStart
std::chrono::steady_clock::time_point m_fecWindowStart;
double m_fecViterbiSeconds = 0.0; //!< FECDecoder's Viterbi time at m_fecWindowStart
int m_fecQuietWindows = 0; //!< Consecutive windows it would have been comfortable with a thread fewer
std::vector<uint8_t> m_fecPackets; //!< Decoded by one call, added to m_packets together
bool m_stop;
std::vector<uint8_t> m_packets; //!< Decoded, waiting to be delivered
uint64_t m_resetEpoch = 0; //!< Resets queued
uint64_t m_decodeEpoch = 0; //!< Of the last reset decoding has reached: packets are dropped until it's m_resetEpoch
std::vector<uint8_t> m_delivering;
FECDecoder::Status m_fecStatus;
uint64_t m_queueWaits;
};
} // namespace dvbt
#endif // MODEMDVB_DVBTRECEIVER_H

224
modemdvb/ldpcpool.cpp

@ -0,0 +1,224 @@ @@ -0,0 +1,224 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include "ldpcpool.h"
namespace dvbt2 {
LDPC& LDPCPool::Context::decoder(CodeFamily family, FECType type, CodeRate rate)
{
for (auto it = m_decoders.begin(); it != m_decoders.end(); ++it)
{
if ((it->m_family == family) && (it->m_type == type) && (it->m_rate == rate))
{
if (it != m_decoders.begin())
{
Entry entry = std::move(*it);
m_decoders.erase(it);
m_decoders.push_front(std::move(entry));
}
return *m_decoders.front().m_ldpc;
}
}
Entry entry;
entry.m_family = family;
entry.m_type = type;
entry.m_rate = rate;
entry.m_ldpc.reset(new LDPC(type, rate, family));
m_decoders.push_front(std::move(entry));
// The least recently used dropped, over the budget (the new one's always kept)
size_t bytes = 0;
for (const Entry& e : m_decoders) {
bytes += e.m_ldpc->memoryBytes();
}
while ((m_decoders.size() > 1) && (bytes > m_maxBytes))
{
bytes -= m_decoders.back().m_ldpc->memoryBytes();
m_decoders.pop_back();
}
return *m_decoders.front().m_ldpc;
}
LDPCPool::LDPCPool(int threads) :
m_stop(false)
{
for (int t = 0; t < threads; t++) {
m_threads.emplace_back(&LDPCPool::thread, this);
}
}
LDPCPool::~LDPCPool()
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_stop = true;
}
m_available.notify_all();
for (std::thread& thread : m_threads) {
thread.join();
}
}
void LDPCPool::run(Job& job, Context& context)
{
if (job.m_batches <= 0) {
return;
}
if (!m_threads.empty() && (job.m_batches > 1))
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_jobs.push_back(&job);
}
// Only as many threads as there are batches besides this thread's first: the rest would find none. Those
// woken take more as they finish, and this thread takes any left, so none is missed
const int wake = std::min((int) m_threads.size(), job.m_batches - 1);
for (int i = 0; i < wake; i++) {
m_available.notify_one();
}
}
while (true)
{
int batch;
{
std::lock_guard<std::mutex> lock(m_mutex);
if (job.m_next >= job.m_batches) {
break;
}
batch = job.m_next++;
if (job.m_next == job.m_batches)
{
auto it = std::find(m_jobs.begin(), m_jobs.end(), &job);
if (it != m_jobs.end()) {
m_jobs.erase(it);
}
}
}
try
{
job.m_decode(context.decoder(job.m_family, job.m_type, job.m_rate), context, batch);
}
catch (...)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!job.m_error) {
job.m_error = std::current_exception();
}
cancel(job);
}
std::lock_guard<std::mutex> lock(m_mutex);
job.m_done++;
}
std::unique_lock<std::mutex> lock(m_mutex);
m_done.wait(lock, [&job]() { return job.m_done == job.m_batches; });
if (job.m_error) {
std::rethrow_exception(job.m_error);
}
}
void LDPCPool::cancel(Job& job)
{
job.m_done += job.m_batches - job.m_next;
job.m_next = job.m_batches;
auto it = std::find(m_jobs.begin(), m_jobs.end(), &job);
if (it != m_jobs.end()) {
m_jobs.erase(it);
}
}
void LDPCPool::thread()
{
Context context;
while (true)
{
Job *job;
int batch;
{
std::unique_lock<std::mutex> lock(m_mutex);
m_available.wait(lock, [this]() { return m_stop || !m_jobs.empty(); });
if (m_stop) {
return;
}
job = m_jobs.front();
batch = job->m_next++;
if (job->m_next == job->m_batches) {
m_jobs.pop_front();
}
}
bool finished;
try
{
job->m_decode(context.decoder(job->m_family, job->m_type, job->m_rate), context, batch);
}
catch (...)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (!job->m_error) {
job->m_error = std::current_exception();
}
cancel(*job);
}
{
std::lock_guard<std::mutex> lock(m_mutex);
finished = ++job->m_done == job->m_batches;
}
// The job may be gone once its runner sees this
if (finished) {
m_done.notify_all();
}
}
}
} // namespace dvbt2

113
modemdvb/ldpcpool.h

@ -0,0 +1,113 @@ @@ -0,0 +1,113 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_LDPCPOOL_H
#define MODEMDVB_LDPCPOOL_H
#include <condition_variable>
#include <deque>
#include <exception>
#include <functional>
#include <memory>
#include <mutex>
#include <thread>
#include <vector>
#include "dvbt2fec.h"
namespace dvbt2 {
//! Threads decoding batches of LDPC codewords (with whatever goes with them: demapping before, BCH after), for the
//! DVB-T2 and DVB-S2 receivers. A job's batches are taken, oldest job first, a batch at a time as each thread is free,
//! so a batch that takes longer (more iterations) doesn't hold up the rest. The thread that runs a job decodes its
//! batches too. Each thread keeps decoders for the codes it's been given (an LDPC decoder holds megabytes of messages,
//! so a few threads are plenty: more overflow the cache)
class MODEMDVB_API LDPCPool
{
public:
//! Whatever a thread needs besides its decoder, made by the job when it's null, kept between jobs
struct Scratch
{
virtual ~Scratch() {}
};
//! A thread's decoders, by code, and its scratch. The caller of run() has its own
class Context
{
public:
LDPC& decoder(CodeFamily family, FECType type, CodeRate rate);
std::unique_ptr<Scratch> m_scratch;
private:
struct Entry
{
CodeFamily m_family;
FECType m_type;
CodeRate m_rate;
std::unique_ptr<LDPC> m_ldpc;
};
std::deque<Entry> m_decoders; //!< Most recently used first
//! Bytes of decoders kept, about: their graphs (shared with the other threads' decoders of the code; the
//! messages are each thread's, for any code), a dozen normal frames' (more short ones), so VCM and ACM's
//! MODCODs don't each make their decoder again and again (with two kept, three MODCODs in turn did, the
//! demapping and LDPC taking five times as long), while threads that have seen many codes don't hold them all
static constexpr size_t m_maxBytes = 16u << 20;
};
struct Job
{
CodeFamily m_family = CodeFamily::T2;
FECType m_type = FECType::Normal;
CodeRate m_rate = CodeRate::R1_2;
int m_batches = 0;
//! Decode a batch, with the thread's decoder for the job's code, and its context (for its scratch)
std::function<void(LDPC& ldpc, Context& context, int batch)> m_decode;
// For the pool, with its mutex
int m_next = 0;
int m_done = 0;
std::exception_ptr m_error; //!< The first a batch threw: run() throws it, once the batches running end
};
//! threads: besides those calling run()
explicit LDPCPool(int threads);
~LDPCPool();
LDPCPool(const LDPCPool&) = delete;
LDPCPool& operator=(const LDPCPool&) = delete;
int threads() const { return (int) m_threads.size(); }
//! Decode the job's batches, by this thread (with its context) and the pool's as they're free. Returns when all are
//! done; if one threw (as on running out of memory), the rest aren't started, and it's thrown when those running
//! have ended (so none is left using the job)
void run(Job& job, Context& context);
private:
void thread();
//! With the mutex: no more of the job's batches are started, those not started counted as done
void cancel(Job& job);
std::vector<std::thread> m_threads;
std::mutex m_mutex;
std::condition_variable m_available;
std::condition_variable m_done;
std::deque<Job*> m_jobs; //!< With batches not yet started
bool m_stop;
};
} // namespace dvbt2
#endif // MODEMDVB_LDPCPOOL_H

286
modemdvb/resampler.cpp

@ -0,0 +1,286 @@ @@ -0,0 +1,286 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cmath>
#include "cpufeatures.h"
#include "resampler.h"
#include "resamplerkernel.h"
#if defined(__ARM_NEON) || defined(__ARM_NEON__) || defined(_M_ARM64)
#include <arm_neon.h>
#define RESAMPLER_NEON
#elif defined(USE_SSE2) || defined(__SSE2__) || defined(_M_X64)
#include <emmintrin.h>
#define RESAMPLER_SSE2
#endif
namespace dvbt {
const double Resampler::m_pi = 3.141592653589793;
const double Resampler::m_attenuation = 70.0;
const double Resampler::m_kaiserBeta = 0.1102 * (m_attenuation - 8.7);
double Resampler::besselI0(double x)
{
double sum = 1.0, term = 1.0;
for (int k = 1; k < 50; k++)
{
term *= (x / (2.0 * k)) * (x / (2.0 * k));
sum += term;
if (term < sum * 1e-12) {
break;
}
}
return sum;
}
// The filter for one output sample: a dot product of the input with a phase of the filter, 2 complex taps at a time
struct DotBase
{
#if defined(RESAMPLER_SSE2)
static void dot(const float *x, const float *h, int taps, float *out)
{
__m128 acc = _mm_setzero_ps();
for (int n = 0; n < 2 * taps; n += 4) {
acc = _mm_add_ps(acc, _mm_mul_ps(_mm_loadu_ps(x + n), _mm_loadu_ps(h + n)));
}
// acc holds re, im of even taps and re, im of odd taps
acc = _mm_add_ps(acc, _mm_movehl_ps(acc, acc));
_mm_storel_pi(reinterpret_cast<__m64 *>(out), acc);
}
#elif defined(RESAMPLER_NEON)
static void dot(const float *x, const float *h, int taps, float *out)
{
float32x4_t acc = vdupq_n_f32(0.0f);
for (int n = 0; n < 2 * taps; n += 4) {
acc = vmlaq_f32(acc, vld1q_f32(x + n), vld1q_f32(h + n));
}
// acc holds re, im of even taps and re, im of odd taps
vst1_f32(out, vadd_f32(vget_low_f32(acc), vget_high_f32(acc)));
}
#else
static void dot(const float *x, const float *h, int taps, float *out)
{
float re = 0.0f, im = 0.0f;
for (int n = 0; n < 2 * taps; n += 2)
{
re += x[n] * h[n];
im += x[n + 1] * h[n + 1];
}
out[0] = re;
out[1] = im;
}
#endif
};
Resampler::Resampler() :
m_taps(0),
m_avx2(modemdvb::cpuHasAVX2()),
m_step(1.0),
m_nominalStep(1.0),
m_correction(1.0),
m_time(0.0)
{
}
void Resampler::configure(double inputRate, double outputRate, double passband)
{
// Content above the stopband would alias onto the passband, at whichever rate is lower
double stopband = std::min(inputRate, outputRate) - passband;
double cutoff = (passband + stopband) / 2.0 / inputRate; // Cycles per input sample
double transition = std::max(0.02, (stopband - passband) / inputRate);
int length = (int) std::ceil((m_attenuation - 7.95) / (14.36 * transition)); // Kaiser estimate, in input samples
m_taps = std::max(4, std::min(64, length + 1));
m_taps = (m_taps + 3) & ~3; // A multiple of 4, for SIMD
m_filter.assign(m_phases * m_taps, 0.0f);
// Phase p delays by p / m_phases of an input sample. Tap n multiplies input sample i0 - m_taps/2 + 1 + n
for (int p = 0; p < m_phases; p++)
{
double frac = (double) p / m_phases;
double sum = 0.0;
for (int n = 0; n < m_taps; n++)
{
double x = (n - m_taps / 2 + 1) - frac; // Distance from the output time, in input samples
double arg = 2.0 * x / m_taps;
double window = std::abs(arg) < 1.0 ? besselI0(m_kaiserBeta * std::sqrt(1.0 - arg * arg)) / besselI0(m_kaiserBeta) : 0.0;
double y = 2.0 * m_pi * cutoff * x;
double sinc = std::abs(y) < 1e-12 ? 1.0 : std::sin(y) / y;
double h = 2.0 * cutoff * sinc * window;
m_filter[p * m_taps + n] = (float) h;
sum += h;
}
for (int n = 0; n < m_taps; n++) {
m_filter[p * m_taps + n] /= (float) sum; // Unity gain at DC for every phase
}
}
m_filterPairs.resize(2 * m_filter.size());
for (size_t i = 0; i < m_filter.size(); i++)
{
m_filterPairs[2 * i] = m_filter[i];
m_filterPairs[2 * i + 1] = m_filter[i];
}
m_nominalStep = inputRate / outputRate;
m_step = m_nominalStep * m_correction;
reset();
}
void Resampler::setRatioCorrection(double correction)
{
// Clock offsets are a few ppm, so anything far beyond is an error, which mustn't stop the resampler working
if (!std::isfinite(correction)) {
correction = 1.0;
}
m_correction = std::max(0.99, std::min(1.01, correction));
m_step = m_nominalStep * m_correction;
}
void Resampler::reset()
{
m_history.assign(m_taps, Cf(0.0f, 0.0f));
m_time = m_taps / 2 - 1;
}
void Resampler::prime(const std::vector<Cf>& previous)
{
reset();
int n = std::min((int) previous.size(), m_taps);
std::copy(previous.end() - n, previous.end(), m_history.end() - n);
m_time = m_taps; // The first new sample
}
void Resampler::process(const Cf *in, int nbIn, std::vector<Cf>& out)
{
const size_t n = out.size();
const int maxOut = maxOutputs(nbIn);
out.resize(n + maxOut);
const int made = process(in, nbIn, out.data() + n, maxOut);
out.resize(n + made);
}
int Resampler::maxOutputs(int nbIn) const
{
return resampleMaxOutputs((int) m_history.size() + nbIn, m_time, m_step);
}
void makePolyphaseFilter(int taps, int phases, const std::function<double(double)>& impulse, std::vector<float>& pairs)
{
pairs.assign(2 * (size_t) phases * taps, 0.0f);
std::vector<double> h(taps);
// As configure(): phase p delays by p / phases of an input sample, tap n multiplies input sample i0 - taps/2 + 1 + n
for (int p = 0; p < phases; p++)
{
const double frac = (double) p / phases;
double sum = 0.0;
for (int n = 0; n < taps; n++)
{
h[n] = impulse((n - taps / 2 + 1) - frac);
sum += h[n];
}
for (int n = 0; n < taps; n++)
{
const float v = (float) (sum != 0.0 ? h[n] / sum : h[n]);
pairs[2 * ((size_t) p * taps + n)] = v;
pairs[2 * ((size_t) p * taps + n) + 1] = v;
}
}
}
int resampleFilter(const std::complex<float> *samples, int offset, int available, const float *pairs, int taps,
int phases, double& time, double step, std::complex<float> *out, int maxOut)
{
#if defined(MODEMDVB_X86)
static const bool avx2 = modemdvb::cpuHasAVX2();
if (avx2) {
return resampleAVX2(samples, offset, available, pairs, taps, phases, time, step, out, maxOut);
}
#endif
return resampleRun<DotBase>(samples, offset, available, pairs, taps, phases, time, step, out, maxOut);
}
// Input sample j (from m_history[0], which m_time is from) at samples[j - offset], to available
int Resampler::run(const Cf *samples, int offset, int available, Cf *out, int maxOut)
{
#if defined(MODEMDVB_X86)
if (m_avx2) {
return resampleAVX2(samples, offset, available, m_filterPairs.data(), m_taps, m_phases, m_time, m_step, out, maxOut);
}
#endif
return resampleRun<DotBase>(samples, offset, available, m_filterPairs.data(), m_taps, m_phases, m_time, m_step, out, maxOut);
}
// The input follows the history. The outputs whose taps include the history are made from a copy of it with the start
// of the input, and the rest from the input itself, so it isn't copied. Either way, m_time is from m_history[0], so
// the outputs are the same
int Resampler::process(const Cf *in, int nbIn, Cf *out, int maxOut)
{
const int h = (int) m_history.size();
const int half = m_taps / 2 - 1;
const int available = h + nbIn;
// Those that stop for want of the rest of the head have their first tap past the history
const int head = std::min(nbIn, m_taps);
m_staging.assign(m_history.begin(), m_history.end());
m_staging.insert(m_staging.end(), in, in + head);
int made = run(m_staging.data(), 0, h + head, out, maxOut);
if (head < nbIn) {
made += run(in, h, available, out + made, maxOut - made);
}
// Keep what is still needed
int consumed = std::max(0, (int) m_time - half);
consumed = std::min(consumed, available);
if (consumed >= h)
{
m_history.assign(in + (consumed - h), in + nbIn);
}
else
{
m_history.erase(m_history.begin(), m_history.begin() + consumed);
m_history.insert(m_history.end(), in, in + nbIn);
}
m_time -= consumed;
return made;
}
} // namespace dvbt

87
modemdvb/resampler.h

@ -0,0 +1,87 @@ @@ -0,0 +1,87 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_RESAMPLER_H
#define MODEMDVB_RESAMPLER_H
#include <complex>
#include <functional>
#include <vector>
#include "export.h"
namespace dvbt {
// Polyphase windowed sinc resampler with an adjustable ratio, for any ratio between about 0.5 and 2.
// The filter passes up to passband Hz and rejects what would alias onto it
class MODEMDVB_API Resampler
{
public:
typedef std::complex<float> Cf;
Resampler();
void configure(double inputRate, double outputRate, double passband);
void setRatioCorrection(double correction); //!< Multiplies the input samples per output sample
double getRatioCorrection() const { return m_correction; }
void reset();
//! Start with the given previous input, so the first output is at the next input sample, without a gap or jump
void prime(const std::vector<Cf>& previous);
int getTaps() const { return m_taps; }
void process(const Cf *in, int nbIn, std::vector<Cf>& out); //!< Appends to out
//! Room for the outputs of nbIn more input samples
int maxOutputs(int nbIn) const;
//! Into out, room for maxOut (maxOutputs(nbIn)). Returns the number made
int process(const Cf *in, int nbIn, Cf *out, int maxOut);
private:
static constexpr int m_phases = 512;
static const double m_pi;
static const double m_attenuation; //!< dB
static const double m_kaiserBeta;
static double besselI0(double x);
int m_taps; //!< Per phase
std::vector<float> m_filter; //!< [phase][tap]
bool m_avx2; //!< Use the AVX2 filter
std::vector<float> m_filterPairs; //!< [phase][tap][2], each tap twice, to multiply interleaved I/Q
std::vector<Cf> m_history; //!< Input samples not yet used
std::vector<Cf> m_staging; //!< The history and the start of the input, for the outputs that need both
double m_step; //!< Input samples per output sample
double m_nominalStep;
double m_correction;
double m_time; //!< Of the next output sample, in input samples from m_history[0]
int run(const Cf *samples, int offset, int available, Cf *out, int maxOut);
};
//! A polyphase filter for resampleFilter(): phases phases of taps taps (a multiple of 4), from impulse(x), x the
//! distance in input samples from the output's time. Each phase normalised to unity gain at DC. pairs: [phase][tap][2]
MODEMDVB_API void makePolyphaseFilter(int taps, int phases, const std::function<double(double)>& impulse, std::vector<float>& pairs);
//! The resampler's loop over outputs, with the best kernel for the CPU, for a caller with its own buffer and filter
//! (from makePolyphaseFilter()). Input sample j (counting from the one time is from) is samples[j - offset], for j from
//! offset to available. time: of the next output, in input samples, advanced by step for each. Makes up to maxOut,
//! returning how many: it stops when the next needs input sample available or beyond
MODEMDVB_API int resampleFilter(const std::complex<float> *samples, int offset, int available, const float *pairs, int taps,
int phases, double& time, double step, std::complex<float> *out, int maxOut);
} // namespace dvbt
#endif // MODEMDVB_RESAMPLER_H

127
modemdvb/resampler_avx2.cpp

@ -0,0 +1,127 @@ @@ -0,0 +1,127 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// The resampler's filter, with AVX2 and FMA. Compiled with them enabled, so only used if the CPU has them
#include <immintrin.h>
#include "resamplerkernel.h"
namespace dvbt {
// acc holds re, im of 4 taps: their sums, re and im in the low two floats
static inline __m128 sumTaps(__m256 acc)
{
__m128 sum = _mm_add_ps(_mm256_castps256_ps128(acc), _mm256_extractf128_ps(acc, 1));
return _mm_add_ps(sum, _mm_movehl_ps(sum, sum));
}
// In an unnamed namespace, as is resampleRun<DotAVX2>, so neither can be confused with another file's
namespace {
struct DotAVX2
{
// 4 complex taps at a time
static void dot(const float *x, const float *h, int taps, float *out)
{
__m256 acc = _mm256_setzero_ps();
for (int n = 0; n < 2 * taps; n += 8) {
acc = _mm256_fmadd_ps(_mm256_loadu_ps(x + n), _mm256_loadu_ps(h + n), acc);
}
_mm_storel_pi(reinterpret_cast<__m64 *>(out), sumTaps(acc));
}
};
} // namespace
// The first input sample and the filter phase of the output at time, as resampleRun()
static inline void position(double time, int phases, int half, int& first, int& phase)
{
int i0 = (int) time;
phase = (int) ((time - i0) * phases + 0.5);
if (phase == phases)
{
i0++;
phase = 0;
}
first = i0 - half;
}
// Four outputs at a time, each summed as DotAVX2::dot does, so the same, but with four chains of multiply-adds in
// flight rather than one, which would wait for each other
int resampleAVX2(const ResamplerCf *samples, int offset, int available, const float *pairs, int taps, int phases,
double& time, double step, ResamplerCf *out, int maxOut)
{
const int half = taps / 2 - 1;
int n = 0;
double t = time;
while (n + 4 <= maxOut)
{
int first0, first1, first2, first3, phase0, phase1, phase2, phase3;
double next = t;
position(next, phases, half, first0, phase0);
next += step;
position(next, phases, half, first1, phase1);
next += step;
position(next, phases, half, first2, phase2);
next += step;
position(next, phases, half, first3, phase3);
next += step;
// The last's first tap is the furthest on
if (first3 + taps > available) {
break;
}
const float *x0 = reinterpret_cast<const float *>(samples + (first0 - offset));
const float *x1 = reinterpret_cast<const float *>(samples + (first1 - offset));
const float *x2 = reinterpret_cast<const float *>(samples + (first2 - offset));
const float *x3 = reinterpret_cast<const float *>(samples + (first3 - offset));
const float *h0 = pairs + 2 * phase0 * taps;
const float *h1 = pairs + 2 * phase1 * taps;
const float *h2 = pairs + 2 * phase2 * taps;
const float *h3 = pairs + 2 * phase3 * taps;
__m256 acc0 = _mm256_setzero_ps(), acc1 = _mm256_setzero_ps(), acc2 = _mm256_setzero_ps(), acc3 = _mm256_setzero_ps();
for (int i = 0; i < 2 * taps; i += 8)
{
acc0 = _mm256_fmadd_ps(_mm256_loadu_ps(x0 + i), _mm256_loadu_ps(h0 + i), acc0);
acc1 = _mm256_fmadd_ps(_mm256_loadu_ps(x1 + i), _mm256_loadu_ps(h1 + i), acc1);
acc2 = _mm256_fmadd_ps(_mm256_loadu_ps(x2 + i), _mm256_loadu_ps(h2 + i), acc2);
acc3 = _mm256_fmadd_ps(_mm256_loadu_ps(x3 + i), _mm256_loadu_ps(h3 + i), acc3);
}
float *o = reinterpret_cast<float *>(out + n);
_mm_storel_pi(reinterpret_cast<__m64 *>(o), sumTaps(acc0));
_mm_storel_pi(reinterpret_cast<__m64 *>(o + 2), sumTaps(acc1));
_mm_storel_pi(reinterpret_cast<__m64 *>(o + 4), sumTaps(acc2));
_mm_storel_pi(reinterpret_cast<__m64 *>(o + 6), sumTaps(acc3));
n += 4;
t = next;
}
// The rest, one at a time
time = t;
return n + resampleRun<DotAVX2>(samples, offset, available, pairs, taps, phases, time, step, out + n, maxOut - n);
}
} // namespace dvbt

83
modemdvb/resamplerkernel.h

@ -0,0 +1,83 @@ @@ -0,0 +1,83 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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 MODEMDVB_RESAMPLERKERNEL_H
#define MODEMDVB_RESAMPLERKERNEL_H
// Internal to modemdvb: the resampler's loop over output samples, for a dot product of the input with a phase of
// the filter (Dot::dot), compiled for each instruction set. Only raw buffers here: a file compiled for AVX2 mustn't
// instantiate library code (as std::vector's, or even std::complex's constructors) that the linker could then use for
// every caller. Samples are std::complex only as pointers, reinterpreted as float pairs, as std::complex allows
#include <complex>
namespace dvbt {
typedef std::complex<float> ResamplerCf;
//! Outputs that can be made from what's available for them, up to maxOut (resampleMaxOutputs) into out. Input sample
//! j (from the one time is from) is samples[j - offset], for j from offset (the first output's first tap must be at
//! or after it) to available. pairs: the filter, [phase][tap][2], each tap twice to multiply interleaved I/Q. taps is
//! a multiple of 4. time: of the next output, in input samples from sample 0, advanced. Returns the number made.
//! Dot::dot(x, h, taps, out): the output's real and imaginary parts to out[0] and out[1]
template <class Dot>
int resampleRun(const ResamplerCf *samples, int offset, int available, const float *pairs, int taps, int phases,
double& time, double step, ResamplerCf *out, int maxOut)
{
const int half = taps / 2 - 1;
int n = 0;
while (n < maxOut)
{
int i0 = (int) time;
int phase = (int) ((time - i0) * phases + 0.5);
if (phase == phases)
{
i0++;
phase = 0;
}
const int first = i0 - half;
if (first + taps > available) {
break;
}
Dot::dot(reinterpret_cast<const float *>(samples + (first - offset)), pairs + 2 * phase * taps, taps,
reinterpret_cast<float *>(out + n));
n++;
time += step;
}
return n;
}
//! Room for every output resampleRun can make from available input samples
inline int resampleMaxOutputs(int available, double time, double step)
{
return available > time ? (int) ((available - time) / step) + 2 : 2;
}
#if defined(MODEMDVB_X86)
int resampleAVX2(const ResamplerCf *samples, int offset, int available, const float *pairs, int taps, int phases,
double& time, double step, ResamplerCf *out, int maxOut);
#endif
} // namespace dvbt
#endif // MODEMDVB_RESAMPLERKERNEL_H

204
modemdvb/samplethread.h

@ -0,0 +1,204 @@ @@ -0,0 +1,204 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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 MODEMDVB_SAMPLETHREAD_H
#define MODEMDVB_SAMPLETHREAD_H
#include <cassert>
#include <complex>
#include <condition_variable>
#include <deque>
#include <exception>
#include <functional>
#include <mutex>
#include <thread>
#include <vector>
// Internal to modemdvb: runs a function on blocks of samples, in the order given, in a thread of its own, so the
// caller can get on with something else. The caller waits if too many samples are queued. Destroying it waits for the
// block being processed, but drops those queued: drain() first to have them processed
class SampleThread
{
public:
typedef std::complex<float> Cf;
typedef std::function<void(const Cf *samples, int nbSamples)> Process;
explicit SampleThread(Process process, size_t maxQueued = 1 << 20) :
m_process(process),
m_maxQueued(maxQueued),
m_queued(0),
m_busy(false),
m_stop(false)
{
m_thread = std::thread(&SampleThread::run, this);
}
~SampleThread()
{
{
std::lock_guard<std::mutex> lock(m_mutex);
m_stop = true;
}
m_available.notify_all();
m_space.notify_all();
m_thread.join();
}
void push(const Cf *samples, int nbSamples)
{
if (nbSamples <= 0) {
return;
}
std::vector<Cf> block;
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_free.empty())
{
block = std::move(m_free.back());
m_free.pop_back();
}
lock.unlock();
block.assign(samples, samples + nbSamples);
lock.lock();
m_space.wait(lock, [this]() { return m_stop || (m_queued < m_maxQueued); });
m_queued += nbSamples;
m_queue.push_back(std::move(block));
lock.unlock();
m_available.notify_one();
}
//! The same, without copying: block's samples are taken, and it's given a buffer to reuse, of any size (not
//! cleared, so resizing it to about the size it was doesn't have to fill it)
void push(std::vector<Cf>& block)
{
if (block.empty()) {
return;
}
std::vector<Cf> queued;
queued.swap(block);
const size_t nbSamples = queued.size();
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_free.empty())
{
block.swap(m_free.back());
m_free.pop_back();
}
m_space.wait(lock, [this]() { return m_stop || (m_queued < m_maxQueued); });
m_queued += nbSamples;
m_queue.push_back(std::move(queued));
lock.unlock();
m_available.notify_one();
}
//! Wait until everything queued has been processed. Not from the function (it would wait for itself)
void drain()
{
const bool own = std::this_thread::get_id() == m_thread.get_id();
assert(!own);
if (own) {
return;
}
std::unique_lock<std::mutex> lock(m_mutex);
m_idle.wait(lock, [this]() { return m_queue.empty() && !m_busy; });
}
//! Discard what is queued, and wait for what is being processed. The function isn't called again until more
//! samples are pushed, so the caller can then change what it uses. From the function, only discards (what's being
//! processed is the caller's)
void discard()
{
std::unique_lock<std::mutex> lock(m_mutex);
while (!m_queue.empty())
{
m_free.push_back(std::move(m_queue.front()));
m_queue.pop_front();
}
m_queued = 0;
m_space.notify_all();
if (std::this_thread::get_id() != m_thread.get_id()) {
m_idle.wait(lock, [this]() { return !m_busy; });
}
}
private:
void run()
{
while (true)
{
std::vector<Cf> block;
{
std::unique_lock<std::mutex> lock(m_mutex);
m_available.wait(lock, [this]() { return m_stop || !m_queue.empty(); });
if (m_stop) {
return;
}
block = std::move(m_queue.front());
m_queue.pop_front();
m_queued -= block.size();
m_busy = true;
}
m_space.notify_one();
// An exception (out of memory) loses the block, rather than ending the process at the thread's end
try {
m_process(block.data(), (int) block.size());
} catch (...) {
}
{
std::lock_guard<std::mutex> lock(m_mutex);
m_busy = false;
if (m_free.size() < 16) {
m_free.push_back(std::move(block));
}
}
m_idle.notify_all();
}
}
Process m_process;
size_t m_maxQueued; //!< Samples
size_t m_queued;
bool m_busy;
bool m_stop;
std::thread m_thread;
std::mutex m_mutex;
std::condition_variable m_available;
std::condition_variable m_space;
std::condition_variable m_idle;
std::deque<std::vector<Cf>> m_queue;
std::vector<std::vector<Cf>> m_free;
};
#endif // MODEMDVB_SAMPLETHREAD_H

322
modemdvb/viterbi27.cpp

@ -0,0 +1,322 @@ @@ -0,0 +1,322 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 <algorithm>
#include <cstdlib>
#include "cpufeatures.h"
#include "viterbi27.h"
#include "viterbi27kernel.h"
#if defined(__aarch64__) || defined(_M_ARM64)
#include <arm_neon.h>
#define VITERBI27_NEON
#endif
namespace dvbt {
int Viterbi27::parity(int v)
{
v ^= v >> 4;
v ^= v >> 2;
v ^= v >> 1;
return v & 1;
}
// Encoder X (bit 0) and Y (bit 1) outputs for each state, with input 0. From state j + 32 they're those from j
// inverted (s5 is in both polynomials), as are those with input 1
struct EncoderOutputs
{
alignas(32) uint8_t m_xy[64];
EncoderOutputs()
{
for (int s = 0; s < 64; s++) {
m_xy[s] = Viterbi27::parity(s & Viterbi27::m_maskX) | (Viterbi27::parity(s & Viterbi27::m_maskY) << 1);
}
}
};
const EncoderOutputs Viterbi27::m_encoderOutputs;
// Branch costs for 8-bit path metrics: a soft bit costs |llr| / 2 (up to 63) when the bit sent differs from its sign,
// so a branch costs up to 126. A bit's costs for encoder outputs 0..3 (X = bit 0, Y = bit 1) are in bytes 0..3, so a
// step's costs are the sum of X's and Y's words. Halving the LLRs cost 0.01 dB against 16-bit metrics at 64-QAM 2/3
// (quartering them, 0.025 dB)
struct BranchCosts
{
uint32_t m_x[256];
uint32_t m_y[256];
BranchCosts()
{
for (int v = -128; v < 128; v++)
{
uint32_t q = std::min(63, (std::abs(v) + 1) >> 1);
uint32_t if0 = v < 0 ? q : 0; // Cost if 0 was sent
uint32_t if1 = v > 0 ? q : 0;
m_x[v & 255] = if0 | (if1 << 8) | (if0 << 16) | (if1 << 24);
m_y[v & 255] = if0 | (if0 << 8) | (if1 << 16) | (if1 << 24);
}
}
};
const BranchCosts Viterbi27::m_branchCosts;
Viterbi27::Viterbi27()
{
reset();
}
void Viterbi27::reset()
{
std::fill(m_metrics, m_metrics + 64, 0);
m_nbDecisions = 0;
}
void Viterbi27::encode(const uint8_t *bits, int nbBits, uint8_t& state, std::vector<uint8_t>& out)
{
size_t base = out.size();
out.resize(base + 2 * nbBits);
uint8_t *o = out.data() + base;
unsigned int s = state; // A local, as the output (bytes) could alias the state
for (int i = 0; i < nbBits; i++)
{
int u = bits[i] & 1;
int xy = m_encoderOutputs.m_xy[s] ^ (u ? 3 : 0);
o[2 * i] = xy & 1;
o[2 * i + 1] = xy >> 1;
s = ((s << 1) | u) & 63;
}
state = (uint8_t) s;
}
#ifdef VITERBI27_NEON
// As the SSE4.1 kernel (viterbi27_sse41.cpp), with table lookups for its byte shuffles, zips for its unpacks, a
// sum of weighted bits for its movemask, and a horizontal minimum. File-local, not Viterbi27's members: their
// signatures have NEON types, which viterbi27.h would need arm_neon.h for
static inline uint64_t movemask(uint8x16_t x, uint8x16_t weights)
{
const uint8x16_t bits = vandq_u8(x, weights);
return vaddv_u8(vget_low_u8(bits)) | ((uint64_t) vaddv_u8(vget_high_u8(bits)) << 8);
}
static inline void step8NEON(uint8x16_t m[4], uint32_t cost, const uint8x16_t index[2],
const uint8x16_t indexInverse[2], uint8x16_t weights, uint64_t *decisions)
{
const uint8x16_t w = vreinterpretq_u8_u32(vdupq_n_u32(cost));
uint64_t dec = 0;
uint8x16_t nm[4];
for (int g = 0; g < 2; g++)
{
const uint8x16_t c = vqtbl1q_u8(w, index[g]); // From j, input 0, and from j + 32, input 1
const uint8x16_t cInverse = vqtbl1q_u8(w, indexInverse[g]);
const uint8x16_t a = m[g];
const uint8x16_t b = m[g + 2];
const uint8x16_t e0 = vqaddq_u8(a, c);
const uint8x16_t e1 = vqaddq_u8(b, cInverse);
const uint8x16_t o0 = vqaddq_u8(a, cInverse);
const uint8x16_t o1 = vqaddq_u8(b, c);
const uint8x16_t even = vminq_u8(e0, e1);
const uint8x16_t odd = vminq_u8(o0, o1);
const uint8x16_t de = vceqq_u8(even, e0);
const uint8x16_t dd = vceqq_u8(odd, o0);
const uint64_t dlo = movemask(vzip1q_u8(de, dd), weights);
const uint64_t dhi = movemask(vzip2q_u8(de, dd), weights);
dec |= (dlo | (dhi << 16)) << (32 * g);
// New states 32g..32g+31
nm[2 * g] = vzip1q_u8(even, odd);
nm[2 * g + 1] = vzip2q_u8(even, odd);
}
for (int g = 0; g < 4; g++) {
m[g] = nm[g];
}
*decisions = ~dec;
}
static void forwardNEON(uint8_t *metrics, const uint8_t *xy, const uint32_t *costs, int nbBits, uint64_t *decisions)
{
static const uint8_t weightBytes[16] = {1, 2, 4, 8, 16, 32, 64, 128, 1, 2, 4, 8, 16, 32, 64, 128};
const uint8x16_t weights = vld1q_u8(weightBytes);
uint8x16_t m[4], index[2], indexInverse[2];
for (int g = 0; g < 4; g++) {
m[g] = vld1q_u8(metrics + 16 * g);
}
for (int g = 0; g < 2; g++)
{
index[g] = vld1q_u8(xy + 16 * g);
indexInverse[g] = veorq_u8(index[g], vdupq_n_u8(3));
}
int i = 0;
for (; i + 1 < nbBits; i += 2)
{
const uint8x16_t norm = vdupq_n_u8(vminvq_u8(vminq_u8(vminq_u8(m[0], m[1]), vminq_u8(m[2], m[3]))));
step8NEON(m, costs[i], index, indexInverse, weights, &decisions[i]);
step8NEON(m, costs[i + 1], index, indexInverse, weights, &decisions[i + 1]);
for (int g = 0; g < 4; g++) {
m[g] = vqsubq_u8(m[g], norm);
}
}
// A last step on its own is normalised too, so the next call starts within the bound
if (i < nbBits)
{
const uint8x16_t norm = vdupq_n_u8(vminvq_u8(vminq_u8(vminq_u8(m[0], m[1]), vminq_u8(m[2], m[3]))));
step8NEON(m, costs[i], index, indexInverse, weights, &decisions[i]);
for (int g = 0; g < 4; g++) {
m[g] = vqsubq_u8(m[g], norm);
}
}
for (int g = 0; g < 4; g++) {
vst1q_u8(metrics + 16 * g, m[g]);
}
}
#endif
// 8-bit path metrics, smallest best: 32 states a vector with AVX2, 16 with SSE4.1 or NEON, twice as many as 16-bit
// ones
void Viterbi27::forward(const int8_t *llrs, int nbBits)
{
if (m_costs.size() < (size_t) nbBits) {
m_costs.resize(nbBits);
}
for (int i = 0; i < nbBits; i++) {
m_costs[i] = m_branchCosts.m_x[(uint8_t) llrs[2 * i]] + m_branchCosts.m_y[(uint8_t) llrs[2 * i + 1]];
}
const size_t start = m_nbDecisions;
if (m_decisions.size() < start + nbBits) {
m_decisions.resize(start + nbBits);
}
uint64_t *decisions = m_decisions.data() + start;
m_nbDecisions += nbBits;
#if defined(VITERBI27_NEON)
forwardNEON(m_metrics, m_encoderOutputs.m_xy, m_costs.data(), nbBits, decisions);
#else
#if defined(MODEMDVB_X86)
// The SSE4.1 kernel is faster on efficiency cores: the AVX2 one's permutes across the halves of its registers
// are slow there
if (modemdvb::cpuHasAVX2() && !modemdvb::onEfficiencyCore())
{
viterbi27Forward8AVX2(m_metrics, m_encoderOutputs.m_xy, m_costs.data(), nbBits, decisions);
return;
}
if (modemdvb::cpuHasSSE41())
{
viterbi27Forward8SSE41(m_metrics, m_encoderOutputs.m_xy, m_costs.data(), nbBits, decisions);
return;
}
#endif
forwardScalar(nbBits, decisions);
#endif
}
// The 8-bit kernels' arithmetic, for CPUs without them: saturating adds, and the smallest metric at the start of
// each pair of steps (or of a last step on its own) subtracted at its end
void Viterbi27::forwardScalar(int nbBits, uint64_t *decisions)
{
uint8_t newMetrics[64];
int norm = 0;
for (int i = 0; i < nbBits; i++)
{
const uint32_t w = m_costs[i];
uint64_t dec = 0;
if ((i & 1) == 0) {
norm = *std::min_element(m_metrics, m_metrics + 64);
}
const int subtract = ((i & 1) || (i == nbBits - 1)) ? norm : 0;
for (int j = 0; j < 32; j++)
{
const int k = m_encoderOutputs.m_xy[j];
const int c = (w >> (8 * k)) & 255;
const int cInverse = (w >> (8 * (k ^ 3))) & 255;
const int e0 = std::min(255, m_metrics[j] + c);
const int e1 = std::min(255, m_metrics[j + 32] + cInverse);
const int o0 = std::min(255, m_metrics[j] + cInverse);
const int o1 = std::min(255, m_metrics[j + 32] + c);
newMetrics[2 * j] = (uint8_t) (std::min(e0, e1) - subtract);
newMetrics[2 * j + 1] = (uint8_t) (std::min(o0, o1) - subtract);
dec |= (uint64_t) (e1 < e0) << (2 * j);
dec |= (uint64_t) (o1 < o0) << (2 * j + 1);
}
std::copy(newMetrics, newMetrics + 64, m_metrics);
decisions[i] = dec;
}
}
void Viterbi27::decode(const int8_t *llrs, int nbBits, std::vector<uint8_t>& out)
{
forward(llrs, nbBits);
int pending = m_nbDecisions;
int nbOut = pending - m_tracebackLength;
if (nbOut <= 0) {
return;
}
uint32_t state = (uint32_t) (std::min_element(m_metrics, m_metrics + 64) - m_metrics);
const uint64_t *decisions = m_decisions.data();
for (int t = pending - 1; t >= nbOut; t--) {
state = (state >> 1) | ((uint32_t) (decisions[t] >> state) & 1) << 5;
}
size_t base = out.size();
out.resize(base + nbOut);
uint8_t *bits = out.data() + base;
for (int t = nbOut - 1; t >= 0; t--)
{
bits[t] = (uint8_t) (state & 1);
state = (state >> 1) | ((uint32_t) (decisions[t] >> state) & 1) << 5;
}
// Keep the steps not yet output, at the start
std::copy(m_decisions.begin() + nbOut, m_decisions.begin() + pending, m_decisions.begin());
m_nbDecisions = pending - nbOut;
}
} // namespace dvbt

75
modemdvb/viterbi27.h

@ -0,0 +1,75 @@ @@ -0,0 +1,75 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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 MODEMDVB_VITERBI27_H
#define MODEMDVB_VITERBI27_H
#include <cstdint>
#include <vector>
#include "export.h"
namespace dvbt {
struct EncoderOutputs;
struct BranchCosts;
// Convolutional code, rate 1/2, K = 7, G1 = 171 (X), G2 = 133 (Y) octal, as used by DVB-S and DVB-T
// The encoder is never terminated, so the decoder runs continuously, tracing back over each block
// it is given and holding back the most recent bits until the following block has arrived
class MODEMDVB_API Viterbi27
{
public:
Viterbi27();
void reset();
//! Decode nbBits information bits from soft symbols llrs[2*i] (X) and llrs[2*i+1] (Y),
//! positive meaning 0 and 0 meaning erased. Appends decoded bits (one per byte) to out.
//! Output lags input by m_tracebackLength bits
void decode(const int8_t *llrs, int nbBits, std::vector<uint8_t>& out);
//! Encode bits (one per byte) into X, Y pairs (one bit per byte). Used for testing
static void encode(const uint8_t *bits, int nbBits, uint8_t& state, std::vector<uint8_t>& out);
static constexpr int m_tracebackLength = 192;
private:
friend struct EncoderOutputs;
void forward(const int8_t *llrs, int nbBits);
void forwardScalar(int nbBits, uint64_t *decisions);
static int parity(int v);
// State s holds the previous six input bits, the most recent in bit 0. Input u moves state s to
// ((s << 1) | u) & 63, so new states 2j and 2j+1 both have predecessors j and j+32 (a butterfly)
// X = u ^ s0 ^ s1 ^ s2 ^ s5 (171 octal), Y = u ^ s1 ^ s2 ^ s4 ^ s5 (133 octal)
static constexpr int m_maskX = 0x27;
static constexpr int m_maskY = 0x36;
static const EncoderOutputs m_encoderOutputs;
static const BranchCosts m_branchCosts;
alignas(32) uint8_t m_metrics[64]; //!< 8-bit path metrics, smallest best
//! One word per step not yet output (the first m_nbDecisions), bit s set when state s came from s/2 + 32. Only
//! grows, so its words aren't zeroed each call before the kernels write them (190 kB a symbol in 8K)
std::vector<uint64_t> m_decisions;
int m_nbDecisions = 0;
std::vector<uint32_t> m_costs; //!< Per step, the branch costs for encoder outputs 0..3 in bytes 0..3
};
} // namespace dvbt
#endif // MODEMDVB_VITERBI27_H

101
modemdvb/viterbi27_avx2.cpp

@ -0,0 +1,101 @@ @@ -0,0 +1,101 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// The Viterbi decoder's add-compare-select with 8-bit path metrics and AVX2: 32 states a vector. Compiled with AVX2
// enabled, so only used if the CPU has it
#include <immintrin.h>
#include "viterbi27kernel.h"
namespace dvbt {
// All 64 states in two vectors: states 0..31 (a) and their butterfly partners 32..63 (b)
static inline void step8(__m256i& a, __m256i& b, uint32_t cost, const __m256i index, const __m256i indexInverse,
uint64_t *decisions)
{
const __m256i w = _mm256_set1_epi32((int) cost);
const __m256i c = _mm256_shuffle_epi8(w, index); // From j, input 0, and from j + 32, input 1
const __m256i cInverse = _mm256_shuffle_epi8(w, indexInverse);
const __m256i e0 = _mm256_adds_epu8(a, c);
const __m256i e1 = _mm256_adds_epu8(b, cInverse);
const __m256i o0 = _mm256_adds_epu8(a, cInverse);
const __m256i o1 = _mm256_adds_epu8(b, c);
const __m256i even = _mm256_min_epu8(e0, e1);
const __m256i odd = _mm256_min_epu8(o0, o1);
const __m256i de = _mm256_cmpeq_epi8(even, e0);
const __m256i dd = _mm256_cmpeq_epi8(odd, o0);
// States 2j and 2j + 1 interleaved, within each 128-bit half: [0..15 | 32..47] and [16..31 | 48..63]
const __m256i lo = _mm256_unpacklo_epi8(even, odd);
const __m256i hi = _mm256_unpackhi_epi8(even, odd);
a = _mm256_permute2x128_si256(lo, hi, 0x20);
b = _mm256_permute2x128_si256(lo, hi, 0x31);
// Decisions likewise (set where the state came from j), then the middle 16-bit quarters swapped
const uint64_t dlo = (uint32_t) _mm256_movemask_epi8(_mm256_unpacklo_epi8(de, dd));
const uint64_t dhi = (uint32_t) _mm256_movemask_epi8(_mm256_unpackhi_epi8(de, dd));
const uint64_t x = ~(dlo | (dhi << 32));
*decisions = (x & 0xffff00000000ffffULL) | ((x >> 16) & 0x00000000ffff0000ULL) | ((x << 16) & 0x0000ffff00000000ULL);
}
// The smallest metric, in every byte
static inline __m256i smallest8(const __m256i a, const __m256i b)
{
const __m256i ab = _mm256_min_epu8(a, b);
__m128i smallest = _mm_min_epu8(_mm256_castsi256_si128(ab), _mm256_extracti128_si256(ab, 1));
smallest = _mm_min_epu8(smallest, _mm_srli_si128(smallest, 8));
smallest = _mm_minpos_epu16(_mm_cvtepu8_epi16(smallest));
return _mm256_broadcastb_epi8(smallest);
}
// The smallest metric at the start of each pair of steps is subtracted at the end, as the metrics can't get smaller,
// so finding it overlaps the steps
void viterbi27Forward8AVX2(uint8_t *metrics, const uint8_t *outputs, const uint32_t *costs, int nbBits,
uint64_t *decisions)
{
__m256i a = _mm256_loadu_si256((const __m256i *) metrics);
__m256i b = _mm256_loadu_si256((const __m256i *) (metrics + 32));
const __m256i index = _mm256_loadu_si256((const __m256i *) outputs);
const __m256i indexInverse = _mm256_xor_si256(index, _mm256_set1_epi8(3));
int i = 0;
for (; i + 1 < nbBits; i += 2)
{
const __m256i norm = smallest8(a, b);
step8(a, b, costs[i], index, indexInverse, &decisions[i]);
step8(a, b, costs[i + 1], index, indexInverse, &decisions[i + 1]);
a = _mm256_subs_epu8(a, norm);
b = _mm256_subs_epu8(b, norm);
}
// A last step on its own is normalised too, so the next call starts within the bound
if (i < nbBits)
{
const __m256i norm = smallest8(a, b);
step8(a, b, costs[i], index, indexInverse, &decisions[i]);
a = _mm256_subs_epu8(a, norm);
b = _mm256_subs_epu8(b, norm);
}
_mm256_storeu_si256((__m256i *) metrics, a);
_mm256_storeu_si256((__m256i *) (metrics + 32), b);
}
} // namespace dvbt

122
modemdvb/viterbi27_sse41.cpp

@ -0,0 +1,122 @@ @@ -0,0 +1,122 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// Some code by AI //
// //
// 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 as 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 V3 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/>. //
///////////////////////////////////////////////////////////////////////////////////
// The Viterbi decoder's add-compare-select with 8-bit path metrics and SSE4.1 (for its byte shuffle, unsigned byte
// minimum and minpos): 16 states a vector. Compiled with SSE4.1 enabled, so only used if the CPU has it
#include <smmintrin.h>
#include "viterbi27kernel.h"
namespace dvbt {
// States 0..31 (a) in m[0..1], their butterfly partners 32..63 (b) in m[2..3]
static inline void step8(__m128i m[4], uint32_t cost, const __m128i index[2], const __m128i indexInverse[2],
uint64_t *decisions)
{
const __m128i w = _mm_set1_epi32((int) cost);
uint64_t dec = 0;
__m128i nm[4];
for (int g = 0; g < 2; g++)
{
const __m128i c = _mm_shuffle_epi8(w, index[g]); // From j, input 0, and from j + 32, input 1
const __m128i cInverse = _mm_shuffle_epi8(w, indexInverse[g]);
const __m128i a = m[g];
const __m128i b = m[g + 2];
const __m128i e0 = _mm_adds_epu8(a, c);
const __m128i e1 = _mm_adds_epu8(b, cInverse);
const __m128i o0 = _mm_adds_epu8(a, cInverse);
const __m128i o1 = _mm_adds_epu8(b, c);
const __m128i even = _mm_min_epu8(e0, e1);
const __m128i odd = _mm_min_epu8(o0, o1);
const __m128i de = _mm_cmpeq_epi8(even, e0);
const __m128i dd = _mm_cmpeq_epi8(odd, o0);
const uint64_t dlo = (uint32_t) _mm_movemask_epi8(_mm_unpacklo_epi8(de, dd));
const uint64_t dhi = (uint32_t) _mm_movemask_epi8(_mm_unpackhi_epi8(de, dd));
dec |= (dlo | (dhi << 16)) << (32 * g);
// New states 32g..32g+31
nm[2 * g] = _mm_unpacklo_epi8(even, odd);
nm[2 * g + 1] = _mm_unpackhi_epi8(even, odd);
}
for (int g = 0; g < 4; g++) {
m[g] = nm[g];
}
*decisions = ~dec;
}
// The smallest metric, in every byte
static inline __m128i smallest8(const __m128i m[4])
{
__m128i smallest = _mm_min_epu8(_mm_min_epu8(m[0], m[1]), _mm_min_epu8(m[2], m[3]));
smallest = _mm_min_epu8(smallest, _mm_srli_si128(smallest, 8));
smallest = _mm_minpos_epu16(_mm_cvtepu8_epi16(smallest));
return _mm_shuffle_epi8(smallest, _mm_setzero_si128());
}
// The smallest metric at the start of each pair of steps is subtracted at the end, as the metrics can't get smaller,
// so finding it overlaps the steps
void viterbi27Forward8SSE41(uint8_t *metrics, const uint8_t *outputs, const uint32_t *costs, int nbBits,
uint64_t *decisions)
{
__m128i m[4], index[2], indexInverse[2];
for (int g = 0; g < 4; g++) {
m[g] = _mm_loadu_si128((const __m128i *) (metrics + 16 * g));
}
for (int g = 0; g < 2; g++)
{
index[g] = _mm_loadu_si128((const __m128i *) (outputs + 16 * g));
indexInverse[g] = _mm_xor_si128(index[g], _mm_set1_epi8(3));
}
int i = 0;
for (; i + 1 < nbBits; i += 2)
{
const __m128i norm = smallest8(m);
step8(m, costs[i], index, indexInverse, &decisions[i]);
step8(m, costs[i + 1], index, indexInverse, &decisions[i + 1]);
for (int g = 0; g < 4; g++) {
m[g] = _mm_subs_epu8(m[g], norm);
}
}
// A last step on its own is normalised too, so the next call starts within the bound
if (i < nbBits)
{
const __m128i norm = smallest8(m);
step8(m, costs[i], index, indexInverse, &decisions[i]);
for (int g = 0; g < 4; g++) {
m[g] = _mm_subs_epu8(m[g], norm);
}
}
for (int g = 0; g < 4; g++) {
_mm_storeu_si128((__m128i *) (metrics + 16 * g), m[g]);
}
}
} // namespace dvbt

42
modemdvb/viterbi27kernel.h

@ -0,0 +1,42 @@ @@ -0,0 +1,42 @@
///////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2026 Jon Beniston, M7RCE <jon@beniston.com> //
// //
// 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 as 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 V3 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 MODEMDVB_VITERBI27KERNEL_H
#define MODEMDVB_VITERBI27KERNEL_H
// Internal to modemdvb: the Viterbi decoder's add-compare-select for instruction sets chosen at run time
#include <cstdint>
namespace dvbt {
#if defined(MODEMDVB_X86)
//! With 8-bit path metrics, smallest best. metrics: 64, updated. outputs: the encoder's outputs (X | Y << 1) from
//! states 0..31 with input 0. costs: per step, the branch costs for outputs 0..3 in bytes 0..3. decisions: nbBits
//! words, bit s set when state s came from s/2 + 32.
//! The smallest metric at the start of each pair of steps (or of a last step on its own, for an odd nbBits) is
//! subtracted at its end (metrics can't get smaller), so finding it overlaps the steps. From the best state one branch costs at most half the step's costs (63), so the
//! smallest metric is at most 126 after subtracting and 252 before: the best paths never saturate at 255
void viterbi27Forward8AVX2(uint8_t *metrics, const uint8_t *outputs, const uint32_t *costs, int nbBits,
uint64_t *decisions);
void viterbi27Forward8SSE41(uint8_t *metrics, const uint8_t *outputs, const uint32_t *costs, int nbBits,
uint64_t *decisions);
#endif
} // namespace dvbt
#endif // MODEMDVB_VITERBI27KERNEL_H

7
plugins/channelrx/CMakeLists.txt

@ -262,6 +262,13 @@ else() @@ -262,6 +262,13 @@ else()
message(STATUS "Not building demoddatv (ENABLE_CHANNELRX_DEMODDATV=${ENABLE_CHANNELRX_DEMODDATV} FFMPEG_FOUND=${FFMPEG_FOUND})")
endif()
# The GUI needs FFmpeg to play video. The server only demodulates, sending the transport stream over UDP
if (ENABLE_CHANNELRX_DEMODDVB AND (FFMPEG_FOUND OR SERVER_MODE))
add_subdirectory(demoddvb)
else()
message(STATUS "Not building demoddvb (ENABLE_CHANNELRX_DEMODDVB=${ENABLE_CHANNELRX_DEMODDVB} FFMPEG_FOUND=${FFMPEG_FOUND})")
endif()
if(NOT SERVER_MODE)
if (ENABLE_CHANNELRX_HEATMAP)
add_subdirectory(heatmap)

138
plugins/channelrx/demoddvb/CMakeLists.txt

@ -0,0 +1,138 @@ @@ -0,0 +1,138 @@
project(demoddvb)
set(demoddvb_SOURCES
dvbdemod.cpp
dvbdemodsettings.cpp
dvbdemodbaseband.cpp
dvbdemodsink.cpp
dvbdemodplugin.cpp
dvbdemodtsfifo.cpp
dvbdemodpsi.cpp
dvbdemodsections.cpp
dvbdemodfreesat.cpp
dvbdemodfreesattables.cpp
dvbdemodtext.cpp
dvbdemodepg.cpp
dvbdemodrecorder.cpp
dvbdemodudp.cpp
dvbdemodwebapiadapter.cpp
)
set(demoddvb_HEADERS
dvbdemod.h
dvbdemodsettings.h
dvbdemodbaseband.h
dvbdemodsink.h
dvbdemodplugin.h
dvbdemodtsfifo.h
dvbdemodpsi.h
dvbdemodsections.h
dvbdemodfreesat.h
dvbdemodtext.h
dvbdemodepg.h
dvbdemodrecorder.h
dvbdemodudp.h
dvbdemodwebapiadapter.h
)
include_directories(
${CMAKE_SOURCE_DIR}/swagger/sdrangel/code/qt5/client
${CMAKE_SOURCE_DIR}
)
if(NOT SERVER_MODE)
# The service is decoded and displayed with FFmpeg. Without a GUI, the transport stream can be sent over UDP
set(demoddvb_SOURCES
${demoddvb_SOURCES}
dvbdemodgui.cpp
dvbdemodgui.ui
dvbdemodvideoplayer.cpp
dvbdemodscope.cpp
dvbdemodepgdialog.cpp
dvbdemodepggrid.cpp
dvbdemodguideoverlay.cpp
dvbdemod.qrc
)
set(demoddvb_HEADERS
${demoddvb_HEADERS}
dvbdemodgui.h
dvbdemodvideoplayer.h
dvbdemodscope.h
dvbdemodepgdialog.h
dvbdemodepggrid.h
dvbdemodguideoverlay.h
)
include_directories(
${AVCODEC_INCLUDE_DIRS}
${AVFORMAT_INCLUDE_DIRS}
${AVUTIL_INCLUDE_DIRS}
${SWSCALE_INCLUDE_DIRS}
${SWRESAMPLE_INCLUDE_DIRS}
)
# Deinterlacing uses libavfilter, if available
if(NOT AVFILTER_LIBRARIES)
find_library(DVBDEMOD_AVFILTER_LIBRARY NAMES avfilter)
find_path(DVBDEMOD_AVFILTER_INCLUDE_DIR libavfilter/avfilter.h HINTS ${AVCODEC_INCLUDE_DIRS})
if(DVBDEMOD_AVFILTER_LIBRARY AND DVBDEMOD_AVFILTER_INCLUDE_DIR)
set(AVFILTER_LIBRARIES ${DVBDEMOD_AVFILTER_LIBRARY})
set(AVFILTER_INCLUDE_DIRS ${DVBDEMOD_AVFILTER_INCLUDE_DIR})
endif()
endif()
if(AVFILTER_LIBRARIES)
include_directories(${AVFILTER_INCLUDE_DIRS})
add_definitions(-DDVBDEMOD_DEINTERLACE)
else()
message(STATUS "demoddvb: libavfilter not found, so no deinterlacing")
endif()
set(TARGET_NAME ${PLUGINS_PREFIX}demoddvb)
set(TARGET_LIB Qt::Widgets ${AVCODEC_LIBRARIES} ${AVFORMAT_LIBRARIES} ${AVUTIL_LIBRARIES} ${SWSCALE_LIBRARIES} ${SWRESAMPLE_LIBRARIES} ${AVFILTER_LIBRARIES})
set(TARGET_LIB_GUI "sdrgui")
set(INSTALL_FOLDER ${INSTALL_PLUGINS_DIR})
else()
set(TARGET_NAME ${PLUGINSSRV_PREFIX}demoddvbsrv)
set(TARGET_LIB "")
set(TARGET_LIB_GUI "")
set(INSTALL_FOLDER ${INSTALL_PLUGINSSRV_DIR})
endif()
if(NOT Qt6_FOUND)
add_library(${TARGET_NAME} ${demoddvb_SOURCES})
else()
qt_add_plugin(${TARGET_NAME} CLASS_NAME DVBDemodPlugin)
target_sources(${TARGET_NAME} PRIVATE ${demoddvb_SOURCES})
endif()
if(NOT BUILD_SHARED_LIBS)
set_property(GLOBAL APPEND PROPERTY STATIC_PLUGINS_PROPERTY ${TARGET_NAME})
endif()
target_link_libraries(${TARGET_NAME} PRIVATE
Qt::Core
Qt::Network
${TARGET_LIB}
sdrbase
${TARGET_LIB_GUI}
modemdvb
)
if(WIN32)
target_link_libraries(${TARGET_NAME} PRIVATE winmm) # The video player sets the timer resolution
endif()
if(APPLE AND NOT SERVER_MODE)
target_link_libraries(${TARGET_NAME} PRIVATE "-framework CoreVideo") # VideoToolbox frames' IOSurfaces
endif()
if(DEFINED FFMPEG_DEPENDS)
add_dependencies(${TARGET_NAME} ${FFMPEG_DEPENDS})
endif()
install(TARGETS ${TARGET_NAME} DESTINATION ${INSTALL_FOLDER})
# Install debug symbols
if (WIN32)
install(FILES $<TARGET_PROPERTY:${TARGET_NAME},RUNTIME_OUTPUT_DIRECTORY>/${TARGET_NAME}stripped.pdb CONFIGURATIONS Release DESTINATION ${INSTALL_FOLDER} RENAME ${TARGET_NAME}.pdb )
install(FILES $<TARGET_PDB_FILE:${TARGET_NAME}> CONFIGURATIONS Debug RelWithDebInfo DESTINATION ${INSTALL_FOLDER} )
endif()

Some files were not shown because too many files have changed in this diff Show More

Loading…
Cancel
Save