-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathheval-422.c
518 lines (417 loc) · 12.8 KB
/
heval-422.c
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
/*
* heval-422.c
* Copyright (c) 2017-2018 Arkadiusz Bokowy
*
* This file is a part of [open]aptx.
*
* This project is licensed under the terms of the MIT license.
*
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "aptx422.h"
#include "inspect-422.h"
#include "inspect-utils.h"
#include "openaptx.h"
#include "../src/aptx422/encode.h"
#include "../src/aptx422/params.h"
#include "../src/aptx422/processor.h"
#include "../src/aptx422/qmf.h"
#include "../src/aptx422/quantizer.h"
#include "../src/aptx422/search.h"
#define aptxbtenc_init aptX_init
#define aptxbtenc_encodestereo aptX_encode_stereo
#include "../src/aptx422/main.c"
#undef aptxbtenc_init
#undef aptxbtenc_encodestereo
static const char * getSubbandName(enum aptX_subband sb) {
switch (sb) {
case APTX_SUBBAND_LL:
return "LL";
case APTX_SUBBAND_LH:
return "LH";
case APTX_SUBBAND_HL:
return "HL";
case APTX_SUBBAND_HH:
return "HH";
default:
return "";
}
}
static int eval_init(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
while (nloops--) {
aptX_encoder_422 enc_422 = { 0 };
aptX_encoder_422 enc_new = { 0 };
short endian = rand() > (RAND_MAX / 2);
aptxbtenc_init(&enc_422, endian);
aptX_init(&enc_new, endian);
if (aptX_encoder_422_cmp("\tenc", &enc_new, &enc_422)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_AsmQmfConvO(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
int32_t coef[16];
for (size_t i = 0; i < sizeof(coef) / sizeof(*coef); i++)
coef[i] = aptX_QMF_outer_coeffs[i];
while (nloops--) {
int32_t out_422[4] = { 0 };
int32_t out_new[4] = { 0 };
int16_t a1[16];
int16_t a2[16];
for (size_t i = 0; i < sizeof(a1) / sizeof(*a1); i++)
a1[i] = rand();
for (size_t i = 0; i < sizeof(a2) / sizeof(*a2); i++)
a2[i] = rand();
AsmQmfConvO(&a1[15], a2, coef, out_422);
aptX_QMF_conv_outer(&a1[15], a2, &out_new[0], &out_new[2]);
if (diffmem("\tout", out_new, out_422, sizeof(out_422))) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_AsmQmfConvI(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
int32_t coef[16];
for (size_t i = 0; i < sizeof(coef) / sizeof(*coef); i++)
coef[i] = aptX_QMF_inner_coeffs[i];
while (nloops--) {
int32_t out_422[4] = { 0 };
int32_t out_new[4] = { 0 };
int32_t a1[16];
int32_t a2[16];
for (size_t i = 0; i < sizeof(a1) / sizeof(*a1); i++)
a1[i] = rand();
for (size_t i = 0; i < sizeof(a2) / sizeof(*a2); i++)
a2[i] = rand();
AsmQmfConvI(&a1[15], a2, coef, out_422);
aptX_QMF_conv_inner(&a1[15], a2, &out_new[0], &out_new[1]);
if (diffmem("\tout", out_new, out_422, sizeof(out_422))) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_Bsearch(enum aptX_subband sb, size_t nloops, bool errstop) {
fprintf(stderr, "%s%s: ", __func__, getSubbandName(sb));
while (nloops--) {
int32_t a = rand();
int32_t x = rand();
size_t o_422, o_new;
switch (sb) {
#if 0
case APTX_SUBBAND_LL:
o_422 = BsearchLL(a, x, aptX_dq7bit16_sl1);
o_new = aptX_search_LL(a, x, aptX_dq7bit16_sl1);
break;
#endif
case APTX_SUBBAND_LH:
o_422 = BsearchLH(a, x, aptX_dq4bit16_sl1);
o_new = aptX_search_LH(a, x, aptX_dq4bit16_sl1);
break;
case APTX_SUBBAND_HL:
o_422 = BsearchHL(a, x, aptX_dq2bit16_sl1);
o_new = aptX_search_HL(a, x, aptX_dq2bit16_sl1);
break;
case APTX_SUBBAND_HH:
o_422 = BsearchHH(a, x, aptX_dq3bit16_sl1);
o_new = aptX_search_HH(a, x, aptX_dq3bit16_sl1);
break;
default:
fprintf(stderr, "%s sub-band function not available\n", getSubbandName(sb));
return -1;
}
if (diffint("\treturn", o_new, o_422)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_packCodeword(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
while (nloops--) {
aptX_subband_encoder_422 e = { 0 };
e.dither_sign = rand();
for (size_t i = 0; i < APTX_SUBBANDS; i++)
e.quantizer[i].unk1 = rand();
uint16_t o_422 = packCodeword(&e);
uint16_t o_new = aptX_pack_codeword(&e);
if (diffint("\tcodeword", o_new, o_422)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_quantiseDifference(enum aptX_subband sb, size_t nloops, bool errstop) {
fprintf(stderr, "%s%s: ", __func__, getSubbandName(sb));
aptX_encoder_422 enc;
aptX_init(&enc, 0);
aptX_quantizer_422 * q_422 = &enc.encoder[0].quantizer[sb];
aptX_quantizer_422 * q_new = &enc.encoder[1].quantizer[sb];
while (nloops--) {
int32_t diff = rand();
int32_t dither = rand();
int32_t x = rand();
switch (sb) {
case APTX_SUBBAND_LL:
quantiseDifferenceLL(diff, dither, x, q_422);
aptX_quantize_difference_LL(diff, dither, x, q_new);
break;
case APTX_SUBBAND_LH:
quantiseDifferenceLH(diff, dither, x, q_422);
aptX_quantize_difference_LH(diff, dither, x, q_new);
break;
case APTX_SUBBAND_HL:
quantiseDifferenceHL(diff, dither, x, q_422);
aptX_quantize_difference_HL(diff, dither, x, q_new);
break;
case APTX_SUBBAND_HH:
quantiseDifferenceHH(diff, dither, x, q_422);
aptX_quantize_difference_HH(diff, dither, x, q_new);
break;
default:
fprintf(stderr, "%s sub-band function not available\n", getSubbandName(sb));
return -1;
}
if (aptX_quantizer_422_cmp("\tquantizer", q_new, q_422)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(q_new, q_422, sizeof(*q_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_aptxEncode(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
aptX_encoder_422 enc;
aptX_init(&enc, 0);
aptX_QMF_analyzer_422 * a_422 = &enc.analyzer[0];
aptX_QMF_analyzer_422 * a_new = &enc.analyzer[1];
aptX_subband_encoder_422 * e_422 = &enc.encoder[0];
aptX_subband_encoder_422 * e_new = &enc.encoder[1];
while (nloops--) {
int32_t pcm[4] = { rand(), rand(), rand(), rand() };
aptxEncode(pcm, a_422, e_422);
aptX_encode(pcm, a_new, e_new);
int ret = 0;
ret |= aptX_QMF_analyzer_422_cmp("\tanalyzer", a_new, a_422);
ret |= aptX_subband_encoder_422_cmp("\tencoder", e_new, e_422);
if (ret) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(a_new, a_422, sizeof(*a_new));
memcpy(e_new, e_422, sizeof(*e_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_invertQuantisation(enum aptX_subband sb, size_t nloops, bool errstop) {
fprintf(stderr, "%s%s: ", __func__, getSubbandName(sb));
aptX_encoder_422 enc;
aptX_init(&enc, 0);
aptX_inverter_422 * i_422 = &enc.encoder[0].processor[sb].inverter;
aptX_inverter_422 * i_new = &enc.encoder[1].processor[sb].inverter;
while (nloops--) {
/* modulo must match selected subband */
int32_t a = rand() % 65;
int32_t dither = rand();
switch (sb) {
case APTX_SUBBAND_LL:
invertQuantisation(a, dither, i_422);
aptX_invert_quantization(a, dither, i_new);
break;
case APTX_SUBBAND_HL:
invertQuantisationHL(a, dither, i_422);
aptX_invert_quantization(a, dither, i_new);
break;
default:
fprintf(stderr, "%s sub-band function not available\n", getSubbandName(sb));
return -1;
}
if (aptX_inverter_422_cmp("\tinverter", i_new, i_422)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(i_new, i_422, sizeof(*i_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_performPredictionFiltering(enum aptX_subband sb, size_t nloops, bool errstop) {
fprintf(stderr, "%s%s: ", __func__, getSubbandName(sb));
aptX_encoder_422 enc;
aptX_init(&enc, 0);
aptX_prediction_filter_422 * f_422 = &enc.encoder[0].processor[sb].filter;
aptX_prediction_filter_422 * f_new = &enc.encoder[1].processor[sb].filter;
while (nloops--) {
int32_t a = rand();
switch (sb) {
case APTX_SUBBAND_LL:
performPredictionFilteringLL(a, f_422);
aptX_prediction_filtering(a, f_new);
break;
case APTX_SUBBAND_HL:
performPredictionFilteringHL(a, f_422);
aptX_prediction_filtering(a, f_new);
break;
default:
fprintf(stderr, "%s sub-band function not available\n", getSubbandName(sb));
return -1;
}
if (aptX_prediction_filter_422_cmp("\tfilter", f_new, f_422)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(f_new, f_422, sizeof(*f_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_processSubband(enum aptX_subband sb, size_t nloops, bool errstop) {
fprintf(stderr, "%s%s: ", __func__, getSubbandName(sb));
aptX_encoder_422 enc;
aptX_init(&enc, 0);
aptX_prediction_filter_422 * f_422 = &enc.encoder[0].processor[sb].filter;
aptX_prediction_filter_422 * f_new = &enc.encoder[1].processor[sb].filter;
aptX_inverter_422 * i_422 = &enc.encoder[0].processor[sb].inverter;
aptX_inverter_422 * i_new = &enc.encoder[1].processor[sb].inverter;
while (nloops--) {
/* modulo must match selected subband */
int32_t a = rand() % 65;
int32_t dither = rand();
switch (sb) {
case APTX_SUBBAND_LL:
processSubbandLL(a, dither, f_422, i_422);
aptX_process_subband(a, dither, f_new, i_new);
break;
case APTX_SUBBAND_HL:
processSubbandHL(a, dither, f_422, i_422);
aptX_process_subband(a, dither, f_new, i_new);
break;
default:
fprintf(stderr, "%s sub-band function not available\n", getSubbandName(sb));
return -1;
}
int ret = 0;
ret |= aptX_prediction_filter_422_cmp("\tfilter", f_new, f_422);
ret |= aptX_inverter_422_cmp("\tinverter", i_new, i_422);
if (ret) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(f_new, f_422, sizeof(*f_new));
memcpy(i_new, i_422, sizeof(*i_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_insertSync(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
while (nloops--) {
aptX_subband_encoder_422 e1 = { 0 };
aptX_subband_encoder_422 e2 = { 0 };
int32_t o_422, o_new;
o_422 = o_new = rand();
insertSync(&e1, &e2, &o_422);
aptX_insert_sync(&e1, &e2, &o_new);
if (diffint("\tsync", o_new, o_422)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static aptX_encoder_422 enc_422;
static aptX_encoder_422 enc_new;
static int eval_aptxbtenc_encodestereo(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
aptX_init(&enc_422, 1);
aptX_init(&enc_new, 1);
while (nloops--) {
int32_t pcmL[4] = { rand(), rand(), rand(), rand() };
int32_t pcmR[4] = { rand(), rand(), rand(), rand() };
uint16_t code_422[2] = { 0 };
uint16_t code_new[2] = { 0 };
aptxbtenc_encodestereo(&enc_422, pcmL, pcmR, code_422);
aptX_encode_stereo(&enc_new, pcmL, pcmR, code_new);
int ret = 0;
ret |= aptX_encoder_422_cmp("\tenc", &enc_new, &enc_422);
ret |= diffmem("\tcode", code_new, code_422, sizeof(code_new));
if (ret) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(&enc_new, &enc_422, sizeof(enc_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
int main(int argc, char * argv[]) {
bool errstop = true;
size_t nloops = 1;
srand(time(NULL));
if (argc >= 2)
nloops = atoi(argv[1]);
if (argc >= 3)
srand(atoi(argv[2]));
if (argc >= 4)
errstop = atoi(argv[3]);
if (argc >= 5) {
fprintf(stderr, "usage: %s [COUNT] [SEED] [STOP]\n", argv[0]);
return -1;
}
fprintf(stderr, "== HEURISTIC EVALUATION ==\n");
int ret = eval_init(nloops, errstop);
ret |= eval_AsmQmfConvO(nloops, errstop);
ret |= eval_AsmQmfConvI(nloops, errstop);
ret |= eval_Bsearch(APTX_SUBBAND_LH, nloops, errstop);
ret |= eval_Bsearch(APTX_SUBBAND_HL, nloops, errstop);
ret |= eval_Bsearch(APTX_SUBBAND_HH, nloops, errstop);
ret |= eval_quantiseDifference(APTX_SUBBAND_LL, nloops, errstop);
ret |= eval_quantiseDifference(APTX_SUBBAND_LH, nloops, errstop);
ret |= eval_quantiseDifference(APTX_SUBBAND_HL, nloops, errstop);
ret |= eval_quantiseDifference(APTX_SUBBAND_HH, nloops, errstop);
ret |= eval_aptxEncode(nloops, errstop);
ret |= eval_insertSync(nloops, errstop);
ret |= eval_invertQuantisation(APTX_SUBBAND_LL, nloops, errstop);
ret |= eval_invertQuantisation(APTX_SUBBAND_HL, nloops, errstop);
ret |= eval_performPredictionFiltering(APTX_SUBBAND_LL, nloops, errstop);
ret |= eval_performPredictionFiltering(APTX_SUBBAND_HL, nloops, errstop);
ret |= eval_processSubband(APTX_SUBBAND_LL, nloops, errstop);
ret |= eval_processSubband(APTX_SUBBAND_HL, nloops, errstop);
ret |= eval_packCodeword(nloops, errstop);
ret |= eval_aptxbtenc_encodestereo(nloops, errstop);
return ret;
}