-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsnd_comp.cpp
643 lines (508 loc) · 18.8 KB
/
snd_comp.cpp
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
#define NOMINMAX
#include <iostream>
#include <fstream>
#include <cmath>
#include <utility>
#include <cassert>
#include <algorithm>
#include <execution>
#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#define _chsize_s(d, s) ftruncate(d,s)
#endif
#include <array>
#include <variant>
#include "myargs/myargs.hpp"
#include "the_matrix.hpp"
#include "dmc.hpp"
#include "mio.hpp"
#include "utils.hpp"
//#include "quantization.hpp"
#include "codebook.hpp"
#include "timed.hpp"
#include "bwt_mtf_rle.hpp"
#include "huffman.hpp"
#include "dkm.hpp"
// #define no_dwt2d
// #include "dwt53.hpp"
using myargs::Args;
using pack::DMC_compressor;
using pack::DMC_decompressor;
using pack::DMCModelConfig;
using pack::DMC_compressor_ref_model;
using pack::DMC_decompressor_ref_model;
using pack::DMCModel;
namespace G711 {
constexpr int BIAS = 0x84; /* Bias for linear code. */
constexpr int SEG_SHIFT = 4; /* Left shift for segment number. */
constexpr int QUANT_MASK = 0xf; /* Quantization field mask. */
constexpr int SEG_MASK = 0x70; /* Segment field mask. */
constexpr int SIGN_BIT = 0x80; /* Sign bit for a A-law byte. */
constexpr int CLIP = 8159;
int16_t ulaw2linear(uint8_t u_val)
{
/* Complement to obtain normal u-law value. */
u_val = ~u_val;
/*
* Extract and bias the quantization bits. Then
* shift up by the segment number and subtract out the bias.
*/
int16_t t = ((u_val & QUANT_MASK) << 3) + BIAS;
t <<= ((unsigned)u_val & SEG_MASK) >> SEG_SHIFT;
return ((u_val & SIGN_BIT) ? (BIAS - t) : (t - BIAS));
}
uint8_t linear2ulaw(int16_t pcm_val)
{
int16_t mask;
/* Get the sign and the magnitude of the value. */
pcm_val = pcm_val >> 2;
if (pcm_val < 0) {
pcm_val = -pcm_val;
mask = 0x7F;
} else {
mask = 0xFF;
}
if ( pcm_val > CLIP ) pcm_val = CLIP; /* clip the magnitude */
pcm_val += (BIAS >> 2);
/* Convert the scaled magnitude to segment number. */
int seg = 0;
for (auto val : {0x3F, 0x7F, 0xFF, 0x1FF,0x3FF, 0x7FF, 0xFFF, 0x1FFF}) {
if (pcm_val <= val)
break;
++seg;
}
if (seg >= 8)
return static_cast<uint8_t>(0x7F ^ mask);
/*
* Combine the sign, segment, quantization bits;
* and complement the code word.
*/
return static_cast<uint8_t>( (seg << 4) | ((pcm_val >> (seg + 1)) & 0xF) ) ^ mask;
}
}
inline float linear(float x, float width, float x0, float x1)
{
return (x1-x0) / width * x + x0;
}
void createEmptyFile(const std::string& fileName, std::streamsize fileSize) {
FILE *fp=fopen(fileName.data(), "w");
_chsize_s(fileno(fp),fileSize);
fclose(fp);
}
struct ADPCM4_codec {
int prev_sample = 0;
int step_index = 0;
/* ff_adpcm_step_table[] and ff_adpcm_index_table[] are from the ADPCM
reference source */
const int8_t ff_adpcm_index_table[16] = {
-1, -1, -1, -1, 2, 4, 6, 8,
-1, -1, -1, -1, 2, 4, 6, 8,
};
/**
* This is the step table. Note that many programs use slight deviations from
* this table, but such deviations are negligible:
*/
const int16_t ff_adpcm_step_table[89] = {
7, 8, 9, 10, 11, 12, 13, 14, 16, 17,
19, 21, 23, 25, 28, 31, 34, 37, 41, 45,
50, 55, 60, 66, 73, 80, 88, 97, 107, 118,
130, 143, 157, 173, 190, 209, 230, 253, 279, 307,
337, 371, 408, 449, 494, 544, 598, 658, 724, 796,
876, 963, 1060, 1166, 1282, 1411, 1552, 1707, 1878, 2066,
2272, 2499, 2749, 3024, 3327, 3660, 4026, 4428, 4871, 5358,
5894, 6484, 7132, 7845, 8630, 9493, 10442, 11487, 12635, 13899,
15289, 16818, 18500, 20350, 22385, 24623, 27086, 29794, 32767
};
class MSE {
public:
void update(float ideal, float aproxymated) {
float error = aproxymated - ideal;
sum_sq += (error * error);
count++;
}
float value() {
return sum_sq / count;
}
void reset() {
sum_sq = 0.0;
count = 0;
}
private:
float sum_sq = 0.0;
size_t count = 0;
};
int encoder(int sample)
{
int delta = sample - prev_sample;
int diff, step = ff_adpcm_step_table[step_index];
int nibble = 8*(delta < 0);
delta= abs(delta);
diff = delta + (step >> 3);
if (delta >= step) {
nibble |= 4;
delta -= step;
}
step >>= 1;
if (delta >= step) {
nibble |= 2;
delta -= step;
}
step >>= 1;
if (delta >= step) {
nibble |= 1;
delta -= step;
}
diff -= delta;
if (nibble & 8)
prev_sample -= diff;
else
prev_sample += diff;
prev_sample = std::clamp( prev_sample, -32767, 32767 );
step_index = std::clamp( step_index + ff_adpcm_index_table[nibble], 0, 88);
return nibble;
}
int decoder(int nibble)
{
int sign, delta, diff, step;
step = ff_adpcm_step_table[step_index];
step_index = std::clamp(step_index + ff_adpcm_index_table[(unsigned)nibble],0,88);
sign = nibble & 8;
delta = nibble & 7;
/* perform direct multiplication instead of series of jumps proposed by
* the reference ADPCM implementation since modern CPUs can do the mults
* quickly enough */
diff = step >> 3;
if (nibble & 4) diff += step;
if (nibble & 2) diff += step >> 1;
if (nibble & 1) diff += step >> 2;
int predictor = prev_sample;
if (sign) predictor -= diff;
else predictor += diff;
prev_sample = std::clamp(predictor,-32767, 32767);
return prev_sample;
}
};
class diff_codec {
public:
diff_codec()
{
}
int encode(float sample) {
auto pair = adpcm_cb.lockup_pair( round( (sample - prev_sample) * 256.0f) );
prev_sample = std::clamp( prev_sample + pair.second * float(1.0/256.0) , -1.0f, 1.0f );
//std::cout << sample << " -> " << pair.first << std::endl;
return pair.first;
}
float decode(int diff) {
return prev_sample = std::clamp( prev_sample + adpcm_cb.get_codebook_value_at(diff) * float(1.0/256.0), -1.0f, 1.0f );
}
private:
float prev_sample = 0.0f;
Codebook adpcm_cb = Codebook( std::vector<int>{
//0, 60, 195, 501, 1190, 2742, 6240, 14123, 31887
//0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 14, 17, 23, 34, 149
0,1,2,3,4,6,12,136
});
};
#if 0
int tinyint_to_int(int v)
{
if (v >= -16 && v <= 16)
return v;
int sign = v < 0 ? (v=-v,-1) : 1;
int mantissa = v & 0x7;
int exponent = (v >> 3) & 0xf;
int integer = (4 << (exponent)) + (mantissa << (exponent-1));
return sign*integer;
}
int int_to_tinyint(int v)
{
if (v >= -16 && v <= 16)
return v;
int sign = v < 0 ? (v=-v,-1) : 1;
int exponent = ilog2_32(v,0)-3;
//return (4 << exponent) + ( ( (v >> (exponent-1)) & 0x7) <<(exponent-1));
return sign*( (exponent << 3) | ( (v >> (exponent-1)) & 7 ) );
}
#endif
static const float phi = (1 + std::sqrt(5)) / 2; // Золотое сечение
static const float sqrt5 = std::sqrt(5);
static const float log_phi = std::log(phi);
// Функция, которая находит индекс числа Фибоначчи для заданного числа
inline int findFibonacciIndex(int num) {
if (num == 0)
return 0;
int sign = num < 0 ? (num=-num,-1) : 1;
// Используем формулу Бине, чтобы найти приближенный индекс числа Фибоначчи
int index = std::round(std::log(num * sqrt5) / log_phi);
return index*sign;
}
// Функция, которая возвращает число Фибоначчи по его индексу
inline int getFibonacciNumber(int index) {
if (index == 0)
return 0;
int sign = index < 0 ? (index=-index,-1) : 1;
// Используем формулу Бине, чтобы найти значение числа Фибоначчи
int fibonacci = std::roundf((std::pow(phi, index) - std::pow(1 - phi, index)) / sqrt5);
return fibonacci*sign;
}
struct DPCMQ_codec {
int prev_sample = 0;
int encoder(float samplef)
{
int sample = samplef * 0x1000000;
int32_t delta = int_to_tinyint(sample - prev_sample);
prev_sample = std::clamp( prev_sample + tinyint_to_int(delta), -0xffffff, 0x1000000 );
return delta;
}
float decoder(int delta)
{
prev_sample =std::clamp( prev_sample + tinyint_to_int(delta), -0xffffff, 0x1000000 );
return prev_sample / static_cast<float>(0x1000000);
}
};
struct DPCMQ_codec_int {
int prev_sample = 0;
int clip_min = -32768;
int clip_max = 32767;
DPCMQ_codec_int(int bits)
{
clip_min = -(1 << (bits-1));
clip_max = (1 << (bits-1));
}
int encoder(int sample)
{
int diff = findFibonacciIndex( sample - prev_sample);
prev_sample = std::clamp( prev_sample + getFibonacciNumber(diff), clip_min, clip_max );
return diff;
}
int decoder(int diff)
{
prev_sample = std::clamp( prev_sample + getFibonacciNumber( diff ), clip_min, clip_max );
return prev_sample;
}
};
int main(int argc, char**argv)
{
try
{
Args args;
args.parse(argc, argv);
std::error_code error;
// if (args.has('c')) {
// auto rw_mmap = mio::make_mmap<mio::ummap_sink>(args[1], 0, 0, error);
// if (error)
// {
// std::cout << error.message() << std::endl;
// return 1;
// }
// for (auto& rw : rw_mmap)
// {
// rw = G711::linear2ulaw( G711::ulaw2linear(rw) );
// }
// return 0;
// }
// std::ifstream input(args[1],std::ios::binary);
// if (input.fail()) {
// std::cerr << "can not open input file!!!" << std::endl << "ERROR (" << errno << ") : " << strerror(errno) << std::endl;
// return 1;
// }
mio::ummap_source mmap = mio::make_mmap<mio::ummap_source>(args[1], 0, 0, error);
if (error)
{
std::cout << error.message() << std::endl;
return 1;
}
bool trace_opt = args.has("trace");
profiling::timed execution_time;
if (!args.has('d'))
{
bool opt_mono = args.has("mono");
int channels_nb = opt_mono ? 1 : 2;
auto sample = reinterpret_cast<const float*>( mmap.data() );
size_t samples_nb = mmap.size() / 4 / channels_nb;
int discretize = args.get("discretize",1024,1 << 8,1 << 24);
std::vector<size_t> vectors(discretize);
if (args.has("stat")) {
std::cout << "stat N=" << samples_nb << " discretize=" << discretize << std::endl;
auto sample = reinterpret_cast<const float*>( mmap.data() );
float prev = 0.0f;
for (size_t n=0; n < samples_nb; ++n)
{
vectors.at( std::min DUMMY (discretize-1, int(std::abs(*sample - prev) * discretize)) )++;
prev = *sample;
sample += channels_nb;
}
std::vector<std::array<double, 2>> dataset;
for (int i = 0; i < discretize; ++i) {
size_t count = vectors.at(i);
if (count > 0 /* samples_nb/discretize/8 */ ) {
auto & array = dataset.emplace_back();
array[0] = double(i);
array[1] = double(count);
}
}
std::cout << "dataset size=" << dataset.size() << std::endl;
auto cluster_data = dkm::kmeans_lloyd(dataset,args.get("size",16,2,1024) );
std::cout << "Means:" << std::endl;
std::vector<int> values;
for (const auto& mean : std::get<0>(cluster_data)) {
std::cout << "\t(" << int(mean[0]) << "," << int( mean[1]) << ")" << std::endl;
values.push_back(mean[0]);
}
std::sort(values.begin(), values.end());
for (auto v : values)
std::cout << v << ", ";
std::cout << std::endl;
//std::cout << "\nCluster labels:" << std::endl;
// std::cout << "\tPoint:";
// for (const auto& point : dataset) {
// std::stringstream value;
// value << "(" << point[0] << "," << point[1] << ")";
// std::cout << std::setw(14) << value.str();
// }
// std::cout << std::endl;
// std::cout << "\tLabel:";
// for (const auto& label : std::get<1>(cluster_data)) {
// std::cout << std::setw(14) << label;
// }
return 0;
}
if (args.has("vec")) {
std::cout << "vec N=" << samples_nb << " discretize=" << discretize << std::endl;
auto sample = reinterpret_cast<const float*>( mmap.data() );
float prev = 0.0f;
std::vector<std::array<float, 8>> dataset;
for (size_t n=0; n < samples_nb*channels_nb-8 && dataset.size() < discretize; n+=8)
{
auto& array = dataset.emplace_back();
std::copy(sample, sample+8, std::begin(array));
sample+=8;
}
std::cout << "dataset size=" << dataset.size() << std::endl;
auto cluster_data = dkm::kmeans_lloyd(dataset,args.get("size",16,2,1024),dataset.size()*2,1.0f/1024.0f );
auto & means = std::get<0>(cluster_data);
std::cout << "Means:" << std::endl;
for (const auto& mean : means ) {
std::cout << "\t(";
for (auto v : mean)
std::cout << int(v*1024) << ",";
std::cout << "\b)" << std::endl;
}
std::cout << std::endl;
std::string output_filename = args[1] + ".vec.raw";
auto outsize = mmap.size();
createEmptyFile(output_filename,outsize);
auto rw_mmap = mio::make_mmap<mio::ummap_sink>(output_filename, 0, outsize, error);
if (error)
{
std::cout << error.message() << std::endl;
return 1;
}
auto sample_in = reinterpret_cast<const float*>( mmap.data() );
auto sample_out = reinterpret_cast<float*>( rw_mmap.data() );
for (const float* sample = sample_in; sample < sample_in + (mmap.size() / 4) -8; sample += 8, sample_out += 8)
{
std::array<float, 8> array;
std::copy(sample, sample+8, std::begin(array));
auto index = dkm::details::closest_mean( array, means);
std::copy( std::begin( means[index] ), std::end( means[index] ), sample_out );
if (trace_opt) {
for (auto v : means[index] )
std::cout << int(v*1024) << ",";
std::cout << std::endl;
}
}
//std::cout << "\nCluster labels:" << std::endl;
// std::cout << "\tPoint:";
// for (const auto& point : dataset) {
// std::stringstream value;
// value << "(" << point[0] << "," << point[1] << ")";
// std::cout << std::setw(14) << value.str();
// }
// std::cout << std::endl;
// std::cout << "\tLabel:";
// for (const auto& label : std::get<1>(cluster_data)) {
// std::cout << std::setw(14) << label;
// }
return 0;
}
DMCModelConfig config;
config.threshold = 4;
config.bigthresh = 40;
config.reset_on_overflow = false;
config.maxnodes = 1ULL << 23;
DMC_compressor enc(config);
enc.put_symbol_bits<64>(samples_nb);
int opt_bits = args.get("bits",16,16,16);
enc.put_symbol(opt_bits,0);
enc.put(opt_mono);
float float_to_int_multipler = (1 << (opt_bits-1)) - 1;
std::vector<diff_codec> codecs;
for (int i=0; i < channels_nb; ++i)
codecs.emplace_back();
std::cout << samples_nb << " samples, " <<(opt_mono ? " mono" : " stereo") << ", " << opt_bits << " bits." << std::endl;
for (size_t n=0; n < samples_nb; ++n)
{
for (auto & codec : codecs ) {
enc.put_symbol( codec.encode( *sample++) ,1);
//enc.put_symbol( codec.encode( std::clamp<float>(*sample,-1.0f, 1.0f) * float_to_int_multipler ) ,1);
//enc.put_symbol( codec.encode( std::clamp<float>(*sample,-1.0f, 1.0f) * float_to_int_multipler ) ,1);
}
}
auto encoded = enc.finish();
std::cout << std::endl;
std::cout << "nodes: " << enc.get_nodes_count() << std::endl;
std::cout << "compressed size: " << encoded.size() << std::endl;
std::string output_filename = ( args.size() > 2 ) ? args[2] : args[1] + ".sdmc2";
std::ofstream output(output_filename, std::ios::binary);
if (!output) {
std::cerr << "can not create output file!!!" << std::endl << "ERROR (" << errno << ") : " << strerror(errno) << std::endl;
return 1;
}
output.write(reinterpret_cast<const char*>(encoded.data()),encoded.size());
}
else
{
DMCModelConfig config;
config.threshold = 4;
config.bigthresh = 40;
config.reset_on_overflow = false;
config.maxnodes = 1ULL << 23;
DMC_decompressor dec(mmap.cbegin(), mmap.cend(), config);
auto samples_nb = dec.get_symbol_bits<64>();
int opt_bits = dec.get_symbol(0);
float float_to_int_multipler = 1.0f / ( (1 << (opt_bits-1))-0 );
bool opt_mono = dec.get();
int channels_nb = opt_mono ? 1 : 2;
std::cout << samples_nb << " samples" << (opt_mono ? ", mono" : ", stereo") << ", " << ( opt_bits ) << " bits." << std::endl;
std::vector<diff_codec> codecs;
for (int i = 0; i < channels_nb; ++i)
codecs.emplace_back();
std::string output_filename = ( args.size() > 2 ) ? args[2] : args[1] + ".f32.raw";
auto outsize = samples_nb*4*channels_nb;
createEmptyFile(output_filename,outsize);
auto rw_mmap = mio::make_mmap<mio::ummap_sink>(output_filename, 0, outsize, error);
if (error)
{
std::cout << error.message() << std::endl;
return 1;
}
auto sample = reinterpret_cast<float*>( rw_mmap.data() );
for (size_t n = 0; n < samples_nb; ++n)
{
for (auto & codec : codecs) {
*sample++ = codec.decode( dec.get_symbol(1) );
//*sample++ = std::clamp<float>( codec.decode( dec.get_symbol(1) ) * float_to_int_multipler, -1.0f, 1.0f);
// int a = codec.decode( dec.get_symbol(1) );
// int b = codec.decode( dec.get_symbol(1) );
// *sample++ = std::clamp<float>( ((a + b) / 2) * float_to_int_multipler, -1, 1);
}
}
}
} catch (const std::exception & e) {
std::cerr << e.what() << std::endl;
}
}