-
Notifications
You must be signed in to change notification settings - Fork 3
/
encode.cc
332 lines (316 loc) · 10.4 KB
/
encode.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
OFDM TV encoder
Copyright 2020 Ahmet Inan <[email protected]>
*/
#include <iostream>
#include <cassert>
#include <cmath>
#include "netpbm.hh"
#include "complex.hh"
#include "utils.hh"
#include "decibel.hh"
#include "fft.hh"
#include "wav.hh"
#include "pcm.hh"
#include "mls.hh"
#include "crc.hh"
#include "galois_field.hh"
#include "bose_chaudhuri_hocquenghem_encoder.hh"
template <typename value, typename cmplx, int rate>
struct Encoder
{
static const int symbol_len = (1280 * rate) / 8000;
static const int guard_len = symbol_len / 8;
static const int img_width = 320;
static const int img_height = 240;
static const int teeth_count = 16;
static const int teeth_dist = img_width / teeth_count;
static const int teeth_off = teeth_dist / 2;
static const int frame_width = 32;
static const int mls0_len = 127;
static const int mls0_poly = 0b10001001;
static const int mls1_len = img_width + teeth_count;
static const int mls1_poly = 0b1100110001;
static const int mls2_poly = 0b10001000000001011;
static const int mls3_poly = 0b10111010010000001;
static const int mls4_len = 255;
static const int mls4_poly = 0b100101011;
DSP::WritePCM<value> *pcm;
DSP::FastFourierTransform<symbol_len, cmplx, 1> bwd;
CODE::CRC<uint16_t> crc;
CODE::BoseChaudhuriHocquenghemEncoder<255, 71> bchenc;
cmplx fdom[2 * symbol_len];
cmplx tdom[symbol_len];
cmplx kern0[symbol_len], kern1[symbol_len];
cmplx guard[guard_len];
value rgb_line[2 * 3 * img_width];
cmplx papr_min, papr_max;
int mls0_off;
int mls1_off;
int mls4_off;
static int bin(int carrier)
{
return (carrier + symbol_len) % symbol_len;
}
static value nrz(bool bit)
{
return 1 - 2 * bit;
}
void rgb_to_yuv(value *yuv, const value *rgb)
{
value WR(0.299), WB(0.114), WG(1-WR-WB), UMAX(0.493), VMAX(0.877);
yuv[0] = WR * rgb[0] + WG * rgb[1] + WB * rgb[2];
yuv[1] = (UMAX/(1-WB)) * (rgb[2]-yuv[0]);
yuv[2] = (VMAX/(1-WR)) * (rgb[0]-yuv[0]);
}
void rgb_to_cmplx(cmplx *out0, cmplx *out1, const value *rgb0, const value *rgb1)
{
value yuv0[3], yuv1[3];
rgb_to_yuv(yuv0, rgb0);
rgb_to_yuv(yuv1, rgb1);
out0[0] = cmplx(yuv0[0]+(yuv0[2]+yuv1[2])/2, yuv0[0]-(yuv0[1]+yuv1[1])/2);
out1[0] = cmplx((yuv0[1]+yuv1[1])/2-yuv1[0], (yuv0[2]+yuv1[2])/2-yuv1[0]);
rgb_to_yuv(yuv0, rgb0+3);
rgb_to_yuv(yuv1, rgb1+3);
out0[1] = cmplx((yuv0[1]+yuv1[1])/2-yuv0[0], (yuv0[2]+yuv1[2])/2-yuv0[0]);
out1[1] = cmplx(yuv1[0]+(yuv0[2]+yuv1[2])/2, yuv1[0]-(yuv0[1]+yuv1[1])/2);
}
void improve_papr(const cmplx *kern)
{
for (int n = 0; n < 1000; ++n) {
int peak = 0;
for (int i = 1; i < symbol_len; ++i)
if (norm(tdom[peak]) < norm(tdom[i]))
peak = i;
cmplx orig = tdom[peak];
for (int i = 0; i < peak; ++i)
tdom[i] -= orig * kern[symbol_len-peak+i];
for (int i = peak; i < symbol_len; ++i)
tdom[i] -= orig * kern[i-peak];
}
}
void symbol(const cmplx *kern = 0)
{
bwd(tdom, fdom);
for (int i = 0; i < symbol_len; ++i)
tdom[i] /= sqrt(value(8 * symbol_len));
if (kern)
improve_papr(kern);
for (int i = 0; i < guard_len; ++i) {
value x = value(i) / value(guard_len - 1);
x = value(0.5) * (value(1) - std::cos(DSP::Const<value>::Pi() * x));
guard[i] = DSP::lerp(guard[i], tdom[i+symbol_len-guard_len], x);
}
cmplx peak, mean;
for (int i = 0; i < symbol_len; ++i) {
cmplx power(tdom[i].real() * tdom[i].real(), tdom[i].imag() * tdom[i].imag());
peak = cmplx(std::max(peak.real(), power.real()), std::max(peak.imag(), power.imag()));
mean += power;
}
if (mean.real() > 0 && mean.imag() > 0) {
cmplx papr(peak.real() / mean.real(), peak.imag() / mean.imag());
papr *= value(symbol_len);
papr_min = cmplx(std::min(papr_min.real(), papr.real()), std::min(papr_min.imag(), papr.imag()));
papr_max = cmplx(std::max(papr_max.real(), papr.real()), std::max(papr_max.imag(), papr.imag()));
}
pcm->write(reinterpret_cast<value *>(guard), guard_len, 2);
pcm->write(reinterpret_cast<value *>(tdom), symbol_len, 2);
for (int i = 0; i < guard_len; ++i)
guard[i] = tdom[i];
}
void pilot_block()
{
CODE::MLS seq1(mls1_poly);
value mls1_fac = sqrt(value(symbol_len) / value(mls1_len));
for (int i = 0; i < symbol_len; ++i)
fdom[i] = 0;
for (int i = mls1_off; i < mls1_off + mls1_len; ++i)
fdom[bin(i)] = mls1_fac * nrz(seq1());
symbol(kern1);
}
void schmidl_cox()
{
CODE::MLS seq0(mls0_poly);
value mls0_fac = sqrt(value(symbol_len) / value(mls0_len));
for (int i = 0; i < symbol_len; ++i)
fdom[i] = 0;
fdom[bin(mls0_off-2)] = mls0_fac;
for (int i = 0; i < mls0_len; ++i)
fdom[bin(2*i+mls0_off)] = nrz(seq0());
for (int i = 0; i < mls0_len; ++i)
fdom[bin(2*i+mls0_off)] *= fdom[bin(2*(i-1)+mls0_off)];
symbol();
}
void meta_data(uint64_t md)
{
uint8_t data[9] = { 0 }, parity[23] = { 0 };
for (int i = 0; i < 55; ++i)
CODE::set_be_bit(data, i, (md>>i)&1);
crc.reset();
uint16_t cs = crc(md << 9);
for (int i = 0; i < 16; ++i)
CODE::set_be_bit(data, i+55, (cs>>i)&1);
bchenc(data, parity);
CODE::MLS seq4(mls4_poly);
value mls4_fac = sqrt(value(symbol_len) / value(mls4_len));
for (int i = 0; i < symbol_len; ++i)
fdom[i] = 0;
fdom[bin(mls4_off-1)] = mls4_fac;
for (int i = 0; i < 71; ++i)
fdom[bin(i+mls4_off)] = nrz(CODE::get_be_bit(data, i));
for (int i = 71; i < mls4_len; ++i)
fdom[bin(i+mls4_off)] = nrz(CODE::get_be_bit(parity, i-71));
for (int i = 0; i < mls4_len; ++i)
fdom[bin(i+mls4_off)] *= fdom[bin(i-1+mls4_off)];
for (int i = 0; i < mls4_len; ++i)
fdom[bin(i+mls4_off)] *= nrz(seq4());
symbol(kern0);
}
Encoder(DSP::WritePCM<value> *pcm, DSP::ReadPEL<value> *pel, int freq_off, uint64_t call_sign) :
pcm(pcm), crc(0xA8F4), bchenc({
0b100011101, 0b101110111, 0b111110011, 0b101101001,
0b110111101, 0b111100111, 0b100101011, 0b111010111,
0b000010011, 0b101100101, 0b110001011, 0b101100011,
0b100011011, 0b100111111, 0b110001101, 0b100101101,
0b101011111, 0b111111001, 0b111000011, 0b100111001,
0b110101001, 0b000011111, 0b110000111, 0b110110001})
{
int offset = (freq_off * symbol_len) / rate;
mls1_off = offset - mls1_len / 2;
mls0_off = offset - mls0_len + 1;
mls4_off = offset - mls4_len / 2;
int car_min = mls1_off - frame_width;
int car_max = mls1_off+mls1_len + frame_width;
papr_min = cmplx(1000, 1000), papr_max = cmplx(-1000, -1000);
for (int i = 0; i < symbol_len; ++i)
fdom[i] = 0;
int count = 0;
for (int i = car_min; i <= car_max; ++i) {
if (i < mls4_off-1 || i >= mls4_off+mls4_len) {
fdom[bin(i)] = 1;
++count;
}
}
for (int i = 0; i < symbol_len; ++i)
fdom[i] /= value(10 * count);
bwd(kern0, fdom);
for (int i = 0; i < symbol_len; ++i)
fdom[i] = 0;
count = 0;
for (int i = car_min; i <= car_max; ++i) {
if (i < mls1_off || i >= mls1_off+mls1_len) {
fdom[bin(i)] = 1;
++count;
}
}
for (int i = 0; i < symbol_len; ++i)
fdom[i] /= value(10 * count);
bwd(kern1, fdom);
pilot_block();
schmidl_cox();
meta_data((call_sign << 8) | 1);
CODE::MLS seq0(mls0_poly), seq2(mls2_poly), seq3(mls3_poly);
value img_fac = sqrt(value(symbol_len) / value(mls1_len));
for (int i = 0; i < symbol_len; ++i)
fdom[i] = 0;
for (int j = 0; j < img_height; j += 2) {
if (j%8 == 0)
pilot_block();
pel->read(rgb_line, 2 * img_width);
for (int i = 0, l = 0; i < img_width; i += 2, l += 2) {
if (i % teeth_dist == teeth_off)
++l;
rgb_to_cmplx(fdom+bin(l+mls1_off), fdom+bin(l+mls1_off)+symbol_len, rgb_line+3*i, rgb_line+3*(img_width+i));
}
for (int k = 0; k < 2; ++k) {
for (int i = 0, l = 0; i < img_width; ++i, ++l) {
if (i % teeth_dist == teeth_off) {
fdom[bin(l+mls1_off)] = img_fac * nrz(seq0());
++l;
}
fdom[bin(l+mls1_off)] = img_fac * cmplx(
fdom[bin(l+mls1_off)+symbol_len*k].real() * nrz(seq2()),
fdom[bin(l+mls1_off)+symbol_len*k].imag() * nrz(seq3()));
}
symbol(kern1);
}
}
pilot_block();
for (int i = 0; i < symbol_len; ++i)
fdom[i] = 0;
symbol();
std::cerr << "real PAPR: " << DSP::decibel(papr_min.real()) << " .. " << DSP::decibel(papr_max.real()) << " dB" << std::endl;
if (pcm->channels() == 2)
std::cerr << "imag PAPR: " << DSP::decibel(papr_min.imag()) << " .. " << DSP::decibel(papr_max.imag()) << " dB" << std::endl;
}
};
long long int base37_encoder(const char *str)
{
long long int acc = 0;
for (char c = *str++; c; c = *str++) {
acc *= 37;
if (c >= '0' && c <= '9')
acc += c - '0' + 1;
else if (c >= 'a' && c <= 'z')
acc += c - 'a' + 11;
else if (c >= 'A' && c <= 'Z')
acc += c - 'A' + 11;
else if (c != ' ')
return -1;
}
return acc;
}
int main(int argc, char **argv)
{
if (argc < 6 || argc > 8) {
std::cerr << "usage: " << argv[0] << " OUTPUT RATE BITS CHANNELS PICTURE [OFFSET] [CALLSIGN]" << std::endl;
return 1;
}
const char *output_name = argv[1];
int output_rate = std::atoi(argv[2]);
int output_bits = std::atoi(argv[3]);
int output_chan = std::atoi(argv[4]);
const char *picture_name = argv[5];
int freq_off = output_chan == 1 ? 2000 : 0;
if (argc >= 7)
freq_off = std::atoi(argv[6]);
if ((output_chan == 1 && freq_off < 1200) || freq_off < 1200 - output_rate / 2 || freq_off > output_rate / 2 - 1200) {
std::cerr << "Unsupported frequency offset." << std::endl;
return 1;
}
long long int call_sign = base37_encoder("ANONYMOUS");
if (argc >= 8)
call_sign = base37_encoder(argv[7]);
if (call_sign <= 0 || call_sign >= 129961739795077L) {
std::cerr << "Unsupported call sign." << std::endl;
return 1;
}
typedef float value;
typedef DSP::Complex<value> cmplx;
DSP::ReadPNM<value> picture_file(picture_name);
if (picture_file.width() != 320 || picture_file.height() != 240 || picture_file.mono()) {
std::cerr << "Unsupported picture format." << std::endl;
return 1;
}
DSP::WriteWAV<value> output_file(output_name, output_rate, output_bits, output_chan);
output_file.silence(output_rate);
switch (output_rate) {
case 8000:
delete new Encoder<value, cmplx, 8000>(&output_file, &picture_file, freq_off, call_sign);
break;
case 16000:
delete new Encoder<value, cmplx, 16000>(&output_file, &picture_file, freq_off, call_sign);
break;
case 44100:
delete new Encoder<value, cmplx, 44100>(&output_file, &picture_file, freq_off, call_sign);
break;
case 48000:
delete new Encoder<value, cmplx, 48000>(&output_file, &picture_file, freq_off, call_sign);
break;
default:
std::cerr << "Unsupported sample rate." << std::endl;
return 1;
}
output_file.silence(output_rate);
return 0;
}