Skip to content

Commit

Permalink
Fix and compile with warnings across clang/gcc/msvc_cl
Browse files Browse the repository at this point in the history
  • Loading branch information
williamyang98 committed Feb 15, 2024
1 parent cfb081a commit e17cf0c
Show file tree
Hide file tree
Showing 35 changed files with 225 additions and 179 deletions.
22 changes: 21 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,24 @@ set(BASIC_RADIO_USE_EASYLOGGING ON CACHE BOOL "Use easylogging for basic_radio"
set(BASIC_SCRAPER_USE_EASYLOGGING ON CACHE BOOL "Use easylogging for basic_scraper" FORCE)

add_subdirectory(${CMAKE_SOURCE_DIR}/src)
add_subdirectory(${CMAKE_SOURCE_DIR}/examples)
add_subdirectory(${CMAKE_SOURCE_DIR}/examples)

# private compiler flags from CMakePresets.json
function(add_project_target_flags target)
separate_arguments(PRIVATE_ARGS NATIVE_COMMAND "${PROJECT_TARGET_PRIVATE_COMPILER_FLAGS}")
target_compile_options(${target} PRIVATE ${PRIVATE_ARGS})
endfunction()
# src/
add_project_target_flags(ofdm_core)
add_project_target_flags(dab_core)
add_project_target_flags(basic_radio)
add_project_target_flags(basic_scraper)
# examples/
add_project_target_flags(radio_app)
add_project_target_flags(basic_radio_app)
add_project_target_flags(basic_radio_app_cli)
add_project_target_flags(rtl_sdr)
add_project_target_flags(simulate_transmitter)
add_project_target_flags(convert_viterbi)
add_project_target_flags(apply_frequency_shift)
add_project_target_flags(read_wav)
42 changes: 41 additions & 1 deletion CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,44 @@
"name": "windows-msvc",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_C_COMPILER": "cl",
"CMAKE_CXX_COMPILER": "cl",
"CMAKE_CXX_FLAGS_INIT": "/MP /fp:fast /arch:AVX2 /D_CRT_SECURE_NO_WARNINGS /D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING",
"CMAKE_C_FLAGS_INIT": "/MP /fp:fast /arch:AVX2 /D_CRT_SECURE_NO_WARNINGS /D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING"
}
},
{
"name": "windows-msvc-warnings",
"inherits": ["windows-msvc"],
"cacheVariables": {
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "/W3 /WX"
}
},
{
"name": "windows-msvc-sanitize",
"generator": "Ninja",
"inherits": ["windows-msvc-warnings"],
"cacheVariables": {
"CMAKE_CXX_FLAGS_INIT": "/MP /fp:fast /arch:AVX2 /fsanitize=address /D_CRT_SECURE_NO_WARNINGS /D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION",
"CMAKE_C_FLAGS_INIT": "/MP /fp:fast /arch:AVX2 /fsanitize=address /D_CRT_SECURE_NO_WARNINGS /D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING /D_DISABLE_VECTOR_ANNOTATION /D_DISABLE_STRING_ANNOTATION"
}
},
{
"name": "windows-clang",
"generator": "Ninja",
"cacheVariables": {
"CMAKE_C_COMPILER": "clang",
"CMAKE_CXX_COMPILER": "clang++",
"CMAKE_CXX_FLAGS_INIT": "-ffast-math -march=native -D_CRT_SECURE_NO_WARNINGS -D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING",
"CMAKE_C_FLAGS_INIT": "-ffast-math -march=native -D_CRT_SECURE_NO_WARNINGS -D_SILENCE_NONFLOATING_COMPLEX_DEPRECATION_WARNING"
}
},
{
"name": "windows-clang-warnings",
"inherits": ["windows-clang"],
"cacheVariables": {
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter"
}
},
{
"name": "gcc",
"generator": "Ninja",
Expand All @@ -32,6 +58,13 @@
"CMAKE_C_FLAGS_INIT": "-ffast-math -march=native"
}
},
{
"name": "gcc-warnings",
"inherits": ["gcc"],
"cacheVariables": {
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter"
}
},
{
"name": "clang",
"generator": "Ninja",
Expand All @@ -42,6 +75,13 @@
"CMAKE_C_FLAGS_INIT": "-ffast-math -march=native"
}
},
{
"name": "clang-warnings",
"inherits": ["clang"],
"cacheVariables": {
"PROJECT_TARGET_PRIVATE_COMPILER_FLAGS": "-Wall -Wextra -Werror -Wno-unused-function -Wno-unused-parameter"
}
},
{
"name": "clang-arm",
"generator": "Ninja",
Expand Down
2 changes: 1 addition & 1 deletion examples/device/device.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class Device
void Close();
bool IsRunning() const { return is_running; }
const auto& GetDescriptor() { return descriptor; }
const int GetBlockSize(void) { return block_size; }
int GetBlockSize(void) { return block_size; }
const auto& GetGainList(void) { return gain_list; }
bool GetIsGainManual(void) { return is_gain_manual; }
float GetSelectedGain(void) { return selected_gain; }
Expand Down
5 changes: 3 additions & 2 deletions examples/read_wav.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ int main(int argc, char** argv) {
if (is_16_bit) {
static auto convert_buf = std::vector<int16_t>(N);
nb_read = fread(convert_buf.data(), sizeof(int16_t), N, fp_in);
for (int i = 0; i < nb_read; i++) {
for (size_t i = 0; i < nb_read; i++) {
const int16_t v0 = convert_buf[i];
block[i] = (uint8_t)(v0/256 + 127);
}
Expand Down Expand Up @@ -213,6 +213,7 @@ bool validate_wav_header(WavHeader& header) {
fprintf(stderr, "[WARN] Expected 8bits per sample for an 8bit IQ stream but got %d bits\n", header.BitsPerSample);
is_warning = true;
}


(void)is_warning;
return !is_error;
}
2 changes: 0 additions & 2 deletions examples/rtl_sdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,6 @@ static void list_channels() {
}

int main(int argc, char **argv) {
const char* PROGRAM_NAME = "rtl_sdr";
const char* PROGRAM_VERSION_NAME = "0.1.0";
auto parser = argparse::ArgumentParser("rtl_sdr", "0.1.0");
parser.add_description("An I/Q recorder for RTL2832 based DVB-T receivers");
init_parser(parser);
Expand Down
5 changes: 2 additions & 3 deletions examples/simulate_transmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,9 @@ int main(int argc, char** argv) {

// generate random digital data
auto frame_bytes_buf = std::vector<uint8_t>(nb_frame_bytes);
uint16_t scrambler_code_word = 0b0000000010101001;
auto scrambler = Scrambler();
scrambler.Reset();
for (int i = 0; i < nb_frame_bytes; i++) {
for (size_t i = 0; i < nb_frame_bytes; i++) {
frame_bytes_buf[i] = scrambler.Process();
}

Expand All @@ -148,7 +147,7 @@ int main(int argc, char** argv) {
apply_pll_auto(frame_out_buf, frame_out_buf, frequency_norm);
}

for (int i = 0; i < frame_size; i++) {
for (size_t i = 0; i < frame_size; i++) {
const float I = frame_out_buf[i].real();
const float Q = frame_out_buf[i].imag();
const float A = 1.0f/(float)params.nb_data_carriers * 200.0f * 2.0f;
Expand Down
2 changes: 1 addition & 1 deletion src/basic_radio/basic_radio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#define LOG_ERROR(...) BASIC_RADIO_LOG_ERROR(fmt::format(__VA_ARGS__))

BasicRadio::BasicRadio(const DAB_Parameters& _params, const size_t nb_threads)
: params(_params), fic_runner(_params), thread_pool(nb_threads)
: params(_params), thread_pool(nb_threads), fic_runner(_params)
{
dab_misc_info = std::make_unique<DAB_Misc_Info>();
dab_database = std::make_unique<DAB_Database>();
Expand Down
3 changes: 2 additions & 1 deletion src/basic_radio/basic_thread_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <thread>
#include <queue>
#include <vector>
#include <stddef.h>

// simple thread pool to decode FIC and MSC channels across all cores
class BasicThreadPool
Expand All @@ -32,7 +33,7 @@ class BasicThreadPool
nb_threads = _nb_threads ? _nb_threads : std::thread::hardware_concurrency();

task_threads.reserve(nb_threads);
for (int i = 0; i < nb_threads; i++) {
for (size_t i = 0; i < nb_threads; i++) {
task_threads.emplace_back(&BasicThreadPool::RunnerThread, this);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/basic_scraper/basic_scraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@ FILE* BasicAudioScraper::CreateWavFile(BasicAudioParams params) {
const int32_t BitsPerSample = params.bytes_per_sample * 8;
const int32_t SampleRate = static_cast<int32_t>(params.frequency);

strncpy(header.ChunkID, "RIFF", 4);
strncpy(header.Format, "WAVE", 4);
strncpy(header.Subchunk1ID, "fmt ", 4);
strncpy(header.Subchunk2ID, "data", 4);
memcpy(header.ChunkID, "RIFF", 4);
memcpy(header.Format, "WAVE", 4);
memcpy(header.Subchunk1ID, "fmt ", 4);
memcpy(header.Subchunk2ID, "data", 4);

header.Subchunk1Size = 16; // size of PCM format fields
header.AudioFormat = 1; // Linear quantisation
Expand Down
6 changes: 3 additions & 3 deletions src/dab/algorithms/crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class CRC_Calculator {
T crc = initial_value;
const size_t shift = (sizeof(T)-1)*8;
const size_t N = x.size();
for (int i = 0; i < N; i++) {
for (size_t i = 0; i < N; i++) {
crc = crc ^ ((T)(x[i]) << shift);
uint8_t lut_idx = (crc >> shift) & 0xFF;
crc = (crc << 8) ^ lut[lut_idx];
Expand All @@ -48,9 +48,9 @@ class CRC_Calculator {
const T bitcheck = 1u << (sizeof(T)*8 - 1);
const int shift = (sizeof(T)-1)*8;

const int N = 256;
const size_t N = 256;
auto* lut = new T[N];
for (int i = 0; i < N; i++) {
for (size_t i = 0; i < N; i++) {
T crc = static_cast<uint8_t>(i) << shift;
for (int j = 0; j < 8; j++) {
if ((crc & bitcheck) != 0) {
Expand Down
4 changes: 2 additions & 2 deletions src/dab/audio/aac_data_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ bool AAC_Data_Decoder::ProcessDataElement(tcb::span<const uint8_t> data) {
// Relevant functions are raw_data_block(...), and data_stream_element(...)
const uint8_t header = data[0];
const uint8_t data_type = (header & 0b11100000) >> 5;
const uint8_t instance_tag = (header & 0b00011110) >> 1;
const uint8_t is_byte_align = (header & 0b00000001) >> 0;
// const uint8_t instance_tag = (header & 0b00011110) >> 1;
// const uint8_t is_byte_align = (header & 0b00000001) >> 0;

if (data_type == 0) {
// LOG_ERROR("Got null data type");
Expand Down
9 changes: 5 additions & 4 deletions src/dab/audio/aac_frame_processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,9 @@ bool AAC_Frame_Processor::CalculateFirecode(tcb::span<const uint8_t> buf) {
}

void AAC_Frame_Processor::AccumulateFrame(tcb::span<const uint8_t> buf) {
const auto N = buf.size();
const size_t N = buf.size();
auto dst_buf = tcb::span(super_frame_buf).subspan(curr_dab_frame*N, N);
for (int i = 0; i < N; i++) {
for (size_t i = 0; i < N; i++) {
dst_buf[i] = buf[i];
}
}
Expand Down Expand Up @@ -217,9 +217,10 @@ void AAC_Frame_Processor::ProcessSuperFrame(const int nb_dab_frame_bytes) {
// Table 2: Syntax of he_aac_super_frame_header()
auto& buf = super_frame_buf;
int curr_byte = 0;
const uint16_t firecode = (buf[0] << 8) | (buf[1]);
// TODO: We can fix firecode using ECC properties
// const uint16_t firecode = (buf[0] << 8) | (buf[1]);
const uint8_t descriptor = buf[2];
const uint8_t rfa = (descriptor & 0b10000000) >> 7;
// const uint8_t rfa = (descriptor & 0b10000000) >> 7;
const uint8_t dac_rate = (descriptor & 0b01000000) >> 6;
const uint8_t sbr_flag = (descriptor & 0b00100000) >> 5;
const uint8_t aac_channel_mode = (descriptor & 0b00010000) >> 4;
Expand Down
2 changes: 1 addition & 1 deletion src/dab/database/dab_database_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class DatabaseEntityUpdater
DatabaseUpdaterGlobalStatistics& stats;
public:
DatabaseEntityUpdater(DatabaseUpdaterGlobalStatistics& _stats): stats(_stats) {}
~DatabaseEntityUpdater() {}
virtual ~DatabaseEntityUpdater() {}
virtual bool IsComplete() = 0;
void OnCreate() {
stats.nb_total++;
Expand Down
35 changes: 16 additions & 19 deletions src/dab/fic/fic_decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
#include "../algorithms/crc.h"
#include "../constants/puncture_codes.h"
#include <fmt/core.h>
#include <assert.h>

#include "../dab_logging.h"
#define TAG "fic-decoder"
static auto _logger = DAB_LOG_REGISTER(TAG);
#define LOG_MESSAGE(...) DAB_LOG_MESSAGE(TAG, fmt::format(__VA_ARGS__))
#define LOG_ERROR(...) DAB_LOG_ERROR(TAG, fmt::format(__VA_ARGS__))

static const auto Generate_CRC_Calc() {
static auto Generate_CRC_Calc() {
// DOC: ETSI EN 300 401
// Clause 5.2.1 - Fast Information Block (FIB)
// CRC16 Polynomial is given by:
Expand All @@ -28,13 +29,13 @@ static const auto Generate_CRC_Calc() {

static auto CRC16_CALC = Generate_CRC_Calc();

FIC_Decoder::FIC_Decoder(const int _nb_encoded_bits, const int _nb_fibs_per_group)
FIC_Decoder::FIC_Decoder(const size_t _nb_encoded_bits, const size_t _nb_fibs_per_group)
// NOTE: 1/3 coding rate after puncturing and 1/4 code
// For all transmission modes these parameters are constant
: nb_encoded_bits(_nb_encoded_bits),
nb_decoded_bits(_nb_encoded_bits/3),
: nb_fibs_per_group(_nb_fibs_per_group),
nb_encoded_bits(_nb_encoded_bits),
nb_decoded_bytes(_nb_encoded_bits/(8*3)),
nb_fibs_per_group(_nb_fibs_per_group)
nb_decoded_bits(_nb_encoded_bits/3)
{
vitdec = std::make_unique<DAB_Viterbi_Decoder>();
vitdec->set_traceback_length(nb_decoded_bits);
Expand All @@ -46,12 +47,8 @@ FIC_Decoder::FIC_Decoder(const int _nb_encoded_bits, const int _nb_fibs_per_grou
FIC_Decoder::~FIC_Decoder() = default;

// Each group contains 3 fibs (fast information blocks) in mode I
void FIC_Decoder::DecodeFIBGroup(tcb::span<const viterbi_bit_t> encoded_bits, const int cif_index) {
// viterbi decoding
int curr_encoded_bit = 0;
int curr_puncture_bit = 0;
int curr_decoded_bit = 0;

void FIC_Decoder::DecodeFIBGroup(tcb::span<const viterbi_bit_t> encoded_bits, const size_t cif_index) {
assert(encoded_bits.size() >= nb_encoded_bits);
// DOC: ETSI EN 300 401
// Clause 11.2 - Coding in the fast information channel
// PI_16, PI_15 and PI_X are used
Expand All @@ -63,8 +60,8 @@ void FIC_Decoder::DecodeFIBGroup(tcb::span<const viterbi_bit_t> encoded_bits, co
// Perhaps these other modes also use the same puncture codes???
// Refer to DOC: docs/DAB_parameters.pdf, Clause A1.1: System parameters
// for the number of bits per fib group for each transmission mode
const int nb_tail_bits = 6;
const int nb_decoded_bits_mode_I = (128*21 + 128*3 + 24)/int(DAB_Viterbi_Decoder::code_rate) - nb_tail_bits;
const size_t nb_tail_bits = 6;
const size_t nb_decoded_bits_mode_I = (128*21 + 128*3 + 24)/DAB_Viterbi_Decoder::code_rate - nb_tail_bits;
if (nb_decoded_bits != nb_decoded_bits_mode_I) {
LOG_ERROR("Expected {} encoded bits but got {}", nb_decoded_bits_mode_I, nb_decoded_bits);
LOG_ERROR("ETSI EN 300 401 standard only gives the puncture codes used in transmission mode I");
Expand All @@ -89,17 +86,18 @@ void FIC_Decoder::DecodeFIBGroup(tcb::span<const viterbi_bit_t> encoded_bits, co

// descrambler
scrambler->Reset();
for (int i = 0; i < nb_decoded_bytes; i++) {
for (size_t i = 0; i < nb_decoded_bytes; i++) {
uint8_t b = scrambler->Process();
decoded_bytes[i] ^= b;
}

// crc16 check
const int nb_fib_bytes = nb_decoded_bytes/nb_fibs_per_group;
const int nb_crc16_bytes = 2;
const int nb_data_bytes = nb_fib_bytes-nb_crc16_bytes;
const size_t nb_fib_bytes = nb_decoded_bytes/nb_fibs_per_group;
const size_t nb_crc16_bytes = 2;
assert(nb_fib_bytes >= nb_crc16_bytes);
const size_t nb_data_bytes = nb_fib_bytes-nb_crc16_bytes;

for (int i = 0; i < nb_fibs_per_group; i++) {
for (size_t i = 0; i < nb_fibs_per_group; i++) {
auto fib_buf = tcb::span(decoded_bytes).subspan(i*nb_fib_bytes, nb_fib_bytes);
auto data_buf = fib_buf.first(nb_data_bytes);
auto crc_buf = fib_buf.last(nb_crc16_bytes);
Expand All @@ -109,7 +107,6 @@ void FIC_Decoder::DecodeFIBGroup(tcb::span<const viterbi_bit_t> encoded_bits, co
const bool is_valid = crc16_rx == crc16_pred;
LOG_MESSAGE("[crc16] fib={}/{} is_match={} pred={:04X} got={:04X}",
i, nb_fibs_per_group, is_valid, crc16_pred, crc16_rx);

if (is_valid) {
obs_on_fib.Notify(data_buf);
}
Expand Down
13 changes: 7 additions & 6 deletions src/dab/fic/fic_decoder.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <stdint.h>
#include <stddef.h>
#include <memory>
#include <vector>
#include "utility/observable.h"
Expand All @@ -18,17 +19,17 @@ class FIC_Decoder
std::unique_ptr<AdditiveScrambler> scrambler;
std::vector<uint8_t> decoded_bytes;

const int nb_fibs_per_group;
const int nb_encoded_bits;
const int nb_decoded_bytes;
const int nb_decoded_bits;
const size_t nb_fibs_per_group;
const size_t nb_encoded_bits;
const size_t nb_decoded_bytes;
const size_t nb_decoded_bits;

// fib buffer
Observable<tcb::span<const uint8_t>> obs_on_fib;
public:
// number of bits in FIB (fast information block) group per CIF (common interleaved frame)
FIC_Decoder(const int _nb_encoded_bits, const int _nb_fibs_per_group);
FIC_Decoder(const size_t _nb_encoded_bits, const size_t _nb_fibs_per_group);
~FIC_Decoder();
void DecodeFIBGroup(tcb::span<const viterbi_bit_t> encoded_bits, const int cif_index);
void DecodeFIBGroup(tcb::span<const viterbi_bit_t> encoded_bits, const size_t cif_index);
auto& OnFIB(void) { return obs_on_fib; }
};
Loading

0 comments on commit e17cf0c

Please sign in to comment.