-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathheval-hd100.c
366 lines (300 loc) · 9.84 KB
/
heval-hd100.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
/*
* heval-hd100.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 "aptxHD100.h"
#include "inspect-hd100.h"
#include "inspect-utils.h"
#include "openaptx.h"
#include "../src/aptxhd100/encode.h"
#include "../src/aptxhd100/mathex.h"
#include "../src/aptxhd100/params.h"
#include "../src/aptxhd100/processor.h"
#include "../src/aptxhd100/qmf.h"
#include "../src/aptxhd100/quantizer.h"
#include "../src/aptxhd100/search.h"
#define aptxhdbtenc_init aptXHD_init
#define aptxhdbtenc_encodestereo aptXHD_encode_stereo
#include "../src/aptxhd100/main.c"
#undef aptxhdbtenc_init
#undef aptxhdbtenc_encodestereo
static const char * getSubbandName(enum aptXHD_subband sb) {
switch (sb) {
case APTXHD_SUBBAND_LL:
return "LL";
case APTXHD_SUBBAND_LH:
return "LH";
case APTXHD_SUBBAND_HL:
return "HL";
case APTXHD_SUBBAND_HH:
return "HH";
default:
return "";
}
}
static int eval_init(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
static const int param_sizes[4] = {
[APTXHD_SUBBAND_LL] = sizeof(aptXHD_q9incr24) / sizeof(*aptXHD_q9incr24),
[APTXHD_SUBBAND_LH] = sizeof(aptXHD_q6incr24) / sizeof(*aptXHD_q6incr24),
[APTXHD_SUBBAND_HL] = sizeof(aptXHD_q4incr24) / sizeof(*aptXHD_q4incr24),
[APTXHD_SUBBAND_HH] = sizeof(aptXHD_q5incr24) / sizeof(*aptXHD_q5incr24),
};
while (nloops--) {
aptXHD_encoder_100 enc_100 = { 0 };
aptXHD_encoder_100 enc_new = { 0 };
short endian = rand() > (RAND_MAX / 2);
aptxhdbtenc_init(&enc_100, endian);
aptXHD_init(&enc_new, endian);
int ret = 0;
for (size_t c = 0; c < APTXHD_CHANNELS; c++)
for (size_t b = 0; b < APTXHD_SUBBANDS; b++) {
for (size_t i = 0; i < param_sizes[b]; i++)
ret |= diffint("bit16", enc_new.encoder[c].processor[b].inverter.subband_param_bit16_sl1[i],
enc_100.encoder[c].processor[b].inverter.subband_param_bit16_sl1[i]);
for (size_t i = 0; i < param_sizes[i]; i++)
ret |= diffint("dith16", enc_new.encoder[c].processor[b].inverter.subband_param_dith16_sf1[i],
enc_100.encoder[c].processor[b].inverter.subband_param_dith16_sf1[i]);
for (size_t i = 0; i < param_sizes[i]; i++)
ret |= diffint("mLamb16", enc_new.encoder[c].quantizer[b].subband_param_mLamb16[i],
enc_100.encoder[c].quantizer[b].subband_param_mLamb16[i]);
for (size_t i = 0; i < param_sizes[i]; i++)
ret |= diffint("incr16", enc_new.encoder[c].processor[b].inverter.subband_param_incr16[i],
enc_100.encoder[c].processor[b].inverter.subband_param_incr16[i]);
}
if (aptXHD_encoder_100_cmp("\tenc", &enc_new, &enc_100) || ret) {
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] = aptXHD_QMF_outer_coeffs[i];
while (nloops--) {
int32_t out_100[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();
AsmQmfConvO(&a1[15], a2, coef, out_100);
aptXHD_QMF_conv_outer(&a1[15], a2, &out_new[0], &out_new[2]);
if (diffmem("\tout", out_new, out_100, sizeof(out_100))) {
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] = aptXHD_QMF_inner_coeffs[i];
while (nloops--) {
int32_t out_100[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_100);
aptXHD_QMF_conv_inner(&a1[15], a2, &out_new[0], &out_new[1]);
if (diffmem("\tout", out_new, out_100, sizeof(out_100))) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_Bsearch(enum aptXHD_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_100, o_new;
a = abs32(a);
clamp_int24_t(a);
switch (sb) {
case APTXHD_SUBBAND_LH:
o_100 = BsearchLH(a, x, aptXHD_dq6bit24_sl1);
o_new = aptXHD_search_LH(a, x, aptXHD_dq6bit24_sl1);
break;
default:
fprintf(stderr, "%s sub-band function not available\n", getSubbandName(sb));
return -1;
}
if (diffint("\treturn", o_new, o_100)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_quantiseDifference(enum aptXHD_subband sb, size_t nloops, bool errstop) {
fprintf(stderr, "%s%s: ", __func__, getSubbandName(sb));
aptXHD_encoder_100 enc;
aptXHD_init(&enc, 0);
aptXHD_quantizer_100 * q_100 = &enc.encoder[0].quantizer[sb];
aptXHD_quantizer_100 * q_new = &enc.encoder[1].quantizer[sb];
while (nloops--) {
int32_t diff = rand();
int32_t dither = rand();
int32_t x = rand();
switch (sb) {
case APTXHD_SUBBAND_LL:
quantiseDifferenceLL(diff, dither, x, q_100);
aptXHD_quantize_difference_LL(diff, dither, x, q_new);
break;
case APTXHD_SUBBAND_LH:
quantiseDifferenceLH(diff, dither, x, q_100);
aptXHD_quantize_difference_LH(diff, dither, x, q_new);
break;
case APTXHD_SUBBAND_HL:
quantiseDifferenceHL(diff, dither, x, q_100);
aptXHD_quantize_difference_HL(diff, dither, x, q_new);
break;
case APTXHD_SUBBAND_HH:
quantiseDifferenceHH(diff, dither, x, q_100);
aptXHD_quantize_difference_HH(diff, dither, x, q_new);
break;
default:
fprintf(stderr, "%s sub-band function not available\n", getSubbandName(sb));
return -1;
}
if (aptXHD_quantizer_100_cmp("\tquantizer", q_new, q_100)) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(q_new, q_100, sizeof(*q_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
static int eval_processSubband(enum aptXHD_subband sb, size_t nloops, bool errstop) {
fprintf(stderr, "%s%s: ", __func__, getSubbandName(sb));
aptXHD_encoder_100 enc;
aptXHD_init(&enc, 0);
aptXHD_prediction_filter_100 * f_100 = &enc.encoder[0].processor[sb].filter;
aptXHD_prediction_filter_100 * f_new = &enc.encoder[1].processor[sb].filter;
aptXHD_inverter_100 * i_100 = &enc.encoder[0].processor[sb].inverter;
aptXHD_inverter_100 * 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 APTXHD_SUBBAND_LL:
processSubbandLL(a, dither, f_100, i_100);
aptXHD_process_subband(a, dither, f_new, i_new);
break;
case APTXHD_SUBBAND_LH:
processSubband(a, dither, f_100, i_100);
aptXHD_process_subband(a, dither, f_new, i_new);
break;
case APTXHD_SUBBAND_HL:
processSubbandHL(a, dither, f_100, i_100);
aptXHD_process_subband(a, dither, f_new, i_new);
break;
case APTXHD_SUBBAND_HH:
processSubband(a, dither, f_100, i_100);
aptXHD_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 |= aptXHD_prediction_filter_100_cmp("\tfilter", f_new, f_100);
ret |= aptXHD_inverter_100_cmp("\tinverter", i_new, i_100);
if (ret) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(f_new, f_100, sizeof(*f_new));
memcpy(i_new, i_100, sizeof(*i_new));
}
}
fprintf(stderr, "OK\n");
return 0;
}
static aptXHD_encoder_100 enc_100;
static aptXHD_encoder_100 enc_new;
static int eval_aptxbtenc_encodestereo(size_t nloops, bool errstop) {
fprintf(stderr, "%s: ", __func__);
aptXHD_init(&enc_100, 1);
aptXHD_init(&enc_new, 1);
while (nloops--) {
int32_t pcmL[4] = { rand(), rand(), rand(), rand() };
int32_t pcmR[4] = { rand(), rand(), rand(), rand() };
uint32_t code_100[2] = { 0 };
uint32_t code_new[2] = { 0 };
aptxhdbtenc_encodestereo(&enc_100, pcmL, pcmR, code_100);
aptXHD_encode_stereo(&enc_new, pcmL, pcmR, code_new);
int ret = 0;
ret |= aptXHD_encoder_100_cmp("\tenc", &enc_new, &enc_100);
ret |= diffmem("\tcode", code_new, code_100, sizeof(code_new));
if (ret) {
fprintf(stderr, "Failed: TTL %zd\n", nloops);
if (errstop)
return -1;
memcpy(&enc_new, &enc_100, 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(APTXHD_SUBBAND_LH, nloops, errstop);
ret |= eval_quantiseDifference(APTXHD_SUBBAND_LL, nloops, errstop);
ret |= eval_quantiseDifference(APTXHD_SUBBAND_LH, nloops, errstop);
ret |= eval_quantiseDifference(APTXHD_SUBBAND_HL, nloops, errstop);
ret |= eval_quantiseDifference(APTXHD_SUBBAND_HH, nloops, errstop);
ret |= eval_processSubband(APTXHD_SUBBAND_LL, nloops, errstop);
ret |= eval_aptxbtenc_encodestereo(nloops, errstop);
return ret;
}