Commit b7be33aa authored by Stefan Westerfeld's avatar Stefan Westerfeld

Add binary block code required for short/checked payload.

Signed-off-by: Stefan Westerfeld's avatarStefan Westerfeld <stefan@space.twc.de>
parent 597fc3fc
......@@ -5,7 +5,7 @@ COMMON_SRC = utils.hh utils.cc convcode.hh convcode.cc random.hh random.cc wavda
audiostream.cc audiostream.hh sfinputstream.cc sfinputstream.hh stdoutwavoutputstream.cc stdoutwavoutputstream.hh \
sfoutputstream.cc sfoutputstream.hh rawinputstream.cc rawinputstream.hh rawoutputstream.cc rawoutputstream.hh \
rawconverter.cc rawconverter.hh mp3inputstream.cc mp3inputstream.hh wmcommon.cc wmcommon.hh fft.cc fft.hh \
limiter.cc limiter.hh
limiter.cc limiter.hh shortcode.cc shortcode.hh
COMMON_LIBS = $(SNDFILE_LIBS) $(FFTW_LIBS) $(LIBGCRYPT_LIBS) $(LIBMPG123_LIBS)
audiowmark_SOURCES = audiowmark.cc wmget.cc wmadd.cc $(COMMON_SRC)
......
/*
* Copyright (C) 2018-2020 Stefan Westerfeld
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 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 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 "utils.hh"
#include "shortcode.hh"
#include "wmcommon.hh"
#include <assert.h>
using std::vector;
/* codetables.de */
static vector<vector<int>> block_48_16_16 = {
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1 },
{ 0, 1, 0, 0, 0, 0, 0, 0, 0, 0,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1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1 },
};
static const auto& gen_matrix = block_56_12_22;
static constexpr int gen_in_count = 12;
static constexpr int gen_out_count = 56;
#endif
vector<int>
code_encode (ConvBlockType block_type, const vector<int>& in_bits)
{
return Params::payload_short ? short_encode (block_type, in_bits) : conv_encode (block_type, in_bits);
}
size_t
code_size (ConvBlockType block_type, size_t msg_size)
{
return Params::payload_short ? short_code_size (block_type, msg_size) : conv_code_size (block_type, msg_size);
}
vector<int>
code_decode_soft (ConvBlockType block_type, const std::vector<float>& coded_bits, float *error_out)
{
return Params::payload_short ? short_decode_soft (block_type, coded_bits, error_out) : conv_decode_soft (block_type, coded_bits, error_out);
}
vector<int>
short_encode (ConvBlockType block_type, const vector<int>& in_bits)
{
assert (gen_matrix.size() == in_bits.size());
vector<int> out_bits;
for (size_t j = 0; j < gen_out_count; j++)
{
int x = 0;
for (size_t bit = 0; bit < gen_in_count; bit++)
{
if (in_bits[bit])
{
x ^= gen_matrix[bit][j];
}
}
out_bits.push_back (x);
}
return conv_encode (block_type, out_bits);
}
size_t
short_code_size (ConvBlockType block_type, size_t msg_size)
{
assert (msg_size == gen_matrix.size());
return conv_code_size (block_type, gen_out_count);
}
vector<int>
short_decode_soft (ConvBlockType block_type, const std::vector<float>& coded_bits, float *error_out)
{
vector<int> xbits = conv_decode_soft (block_type, coded_bits, error_out);
vector<int> out_bits;
for (size_t c = 0; c < (1 << gen_in_count); c++)
{
vector<int> w;
for (size_t j = 0; j < gen_out_count; j++)
{
int x = 0;
for (size_t bit = 0; bit < gen_in_count; bit++)
{
if (c & (1 << bit))
{
x ^= gen_matrix[bit][j];
}
}
w.push_back (x);
}
if (w == xbits)
{
for (size_t bit = 0; bit < gen_in_count; bit++)
{
if (c & (1 << bit))
{
out_bits.push_back (1);
}
else
{
out_bits.push_back (0);
}
}
return out_bits;
}
}
return out_bits;
}
/*
* Copyright (C) 2018-2020 Stefan Westerfeld
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 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 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 AUDIOWMARK_SHORT_CODE_HH
#define AUDIOWMARK_SHORT_CODE_HH
#include <vector>
#include <string>
#include "convcode.hh"
size_t code_size (ConvBlockType block_type, size_t msg_size);
std::vector<int> code_encode (ConvBlockType block_type, const std::vector<int>& in_bits);
std::vector<int> code_decode_soft (ConvBlockType block_type, const std::vector<float>& coded_bits, float *error_out = nullptr);
size_t short_code_size (ConvBlockType block_type, size_t msg_size);
std::vector<int> short_encode (ConvBlockType block_type, const std::vector<int>& in_bits);
std::vector<int> short_decode_soft (ConvBlockType block_type, const std::vector<float>& coded_bits, float *error_out = nullptr);
#endif /* AUDIOWMARK_SHORT_CODE_HH */
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