Commit 3949cd7d authored by Stefan Westerfeld's avatar Stefan Westerfeld

Avoid global state for sync / data frame positions.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 7a6da198
......@@ -38,6 +38,7 @@ SyncFinder::init_up_down (const Key& key, const WavData& wav_data, Mode mode)
size_t n_bands = Params::max_band - Params::min_band + 1;
UpDownGen up_down_gen (key, Random::Stream::sync_up_down);
BitPosGen bit_pos_gen (key);
for (int bit = 0; bit < Params::sync_bits; bit++)
{
vector<FrameBit> frame_bits;
......@@ -49,7 +50,7 @@ SyncFinder::init_up_down (const Key& key, const WavData& wav_data, Mode mode)
for (int block = 0; block < block_count; block++)
{
FrameBit frame_bit;
frame_bit.frame = sync_frame_pos (key, f + bit * Params::sync_frames_per_bit) + block * first_block_end;
frame_bit.frame = bit_pos_gen.sync_frame (f + bit * Params::sync_frames_per_bit) + block * first_block_end;
for (int ch = 0; ch < wav_data.n_channels(); ch++)
{
if (block == 0)
......@@ -259,6 +260,7 @@ SyncFinder::search_refine (const Key& key, const WavData& wav_data, Mode mode, v
vector<float> fft_db;
vector<char> have_frames;
vector<Score> result_scores;
BitPosGen bit_pos_gen (key);
int total_frame_count = mark_sync_frame_count() + mark_data_frame_count();
const int first_block_end = total_frame_count;
......@@ -268,9 +270,9 @@ SyncFinder::search_refine (const Key& key, const WavData& wav_data, Mode mode, v
vector<char> want_frames (total_frame_count);
for (size_t f = 0; f < mark_sync_frame_count(); f++)
{
want_frames[sync_frame_pos (key, f)] = 1;
want_frames[bit_pos_gen.sync_frame (f)] = 1;
if (mode == Mode::CLIP)
want_frames[first_block_end + sync_frame_pos (key, f)] = 1;
want_frames[first_block_end + bit_pos_gen.sync_frame (f)] = 1;
}
for (const auto& score : sync_scores)
......
......@@ -114,10 +114,11 @@ mark_data (const Key& key, vector<vector<FrameMod>>& frame_mod, const vector<int
else
{
UpDownGen up_down_gen (key, Random::Stream::data_up_down);
BitPosGen bit_pos_gen (key);
for (int f = 0; f < frame_count; f++)
{
size_t index = data_frame_pos (key, f);
size_t index = bit_pos_gen.data_frame (f);
prepare_frame_mod (up_down_gen, f, frame_mod[index], bitvec[f / Params::frames_per_bit]);
}
......@@ -131,11 +132,12 @@ mark_sync (const Key& key, vector<vector<FrameMod>>& frame_mod, int ab)
assert (frame_mod.size() >= mark_sync_frame_count());
UpDownGen up_down_gen (key, Random::Stream::sync_up_down);
BitPosGen bit_pos_gen (key);
// sync block always written in linear order (no mix)
for (int f = 0; f < frame_count; f++)
{
size_t index = sync_frame_pos (key, f);
size_t index = bit_pos_gen.sync_frame (f);
int data_bit = (f / Params::sync_frames_per_bit + ab) & 1; /* write 010101 for a block, 101010 for b block */
prepare_frame_mod (up_down_gen, f, frame_mod[index], data_bit);
......
......@@ -139,44 +139,28 @@ FFTAnalyzer::fft_range (const vector<float>& samples, size_t start_index, size_t
return fft_out;
}
int
frame_pos (const Key& key, int f, bool sync)
BitPosGen::BitPosGen (const Key& key)
{
static vector<int> pos_vec;
if (pos_vec.empty())
{
int frame_count = mark_data_frame_count() + mark_sync_frame_count();
for (int i = 0; i < frame_count; i++)
pos_vec.push_back (i);
Random random (key, 0, Random::Stream::frame_position);
random.shuffle (pos_vec);
}
if (sync)
{
assert (f >= 0 && size_t (f) < mark_sync_frame_count());
int frame_count = mark_data_frame_count() + mark_sync_frame_count();
for (int i = 0; i < frame_count; i++)
pos_vec.push_back (i);
return pos_vec[f];
}
else
{
assert (f >= 0 && size_t (f) < mark_data_frame_count());
return pos_vec[f + mark_sync_frame_count()];
}
Random random (key, 0, Random::Stream::frame_position);
random.shuffle (pos_vec);
}
int
sync_frame_pos (const Key& key, int f)
BitPosGen::sync_frame (int f)
{
return frame_pos (key, f, true);
assert (f >= 0 && size_t (f) < mark_sync_frame_count());
return pos_vec[f];
}
int
data_frame_pos (const Key& key, int f)
BitPosGen::data_frame (int f)
{
return frame_pos (key, f, false);
assert (f >= 0 && size_t (f) < mark_data_frame_count());
return pos_vec[f + mark_sync_frame_count()];
}
size_t
......@@ -198,10 +182,11 @@ gen_mix_entries (const Key& key)
vector<MixEntry> mix_entries (frame_count * Params::bands_per_frame);
UpDownGen up_down_gen (key, Random::Stream::data_up_down);
BitPosGen bit_pos_gen (key);
int entry = 0;
for (int f = 0; f < frame_count; f++)
{
const int index = data_frame_pos (key, f);
const int index = bit_pos_gen.data_frame (f);
UpDownArray up, down;
up_down_gen.get (f, up, down);
......
......@@ -119,6 +119,16 @@ public:
}
};
class BitPosGen
{
std::vector<int> pos_vec;
public:
BitPosGen (const Key& key);
int sync_frame (int f);
int data_frame (int f);
};
class FFTAnalyzer
{
int m_n_channels = 0;
......@@ -147,9 +157,6 @@ size_t mark_sync_frame_count();
int frame_count (const WavData& wav_data);
int sync_frame_pos (const Key& key, int f);
int data_frame_pos (const Key& key, int f);
std::vector<int> parse_payload (const std::string& str);
template<class T> std::vector<T>
......
......@@ -101,6 +101,7 @@ static vector<float>
linear_decode (const Key& key, vector<vector<complex<float>>>& fft_out, int n_channels)
{
UpDownGen up_down_gen (key, Random::Stream::data_up_down);
BitPosGen bit_pos_gen (key);
vector<float> raw_bit_vec;
const int frame_count = mark_data_frame_count();
......@@ -110,7 +111,7 @@ linear_decode (const Key& key, vector<vector<complex<float>>>& fft_out, int n_ch
{
for (int ch = 0; ch < n_channels; ch++)
{
const size_t index = data_frame_pos (key, f) * n_channels + ch;
const size_t index = bit_pos_gen.data_frame (f) * n_channels + ch;
UpDownArray up, down;
up_down_gen.get (f, up, down);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment