-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathevrc_codec.cpp
More file actions
93 lines (80 loc) · 2.74 KB
/
evrc_codec.cpp
File metadata and controls
93 lines (80 loc) · 2.74 KB
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
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include "data/dec_raw.h"
#include "data/enc_evrc.h"
#include "data/input.h"
#define INPUT_LEN (sizeof(input))
#define ENC_EVRC_LEN (sizeof(enc_evrc))
#define DEC_RAW_LEN (sizeof(dec_raw))
uint8_t enc_result[ENC_EVRC_LEN] = {0};
uint8_t dec_result[DEC_RAW_LEN] = {0};
constexpr int sample_rate = 8000;
constexpr double total_sec = INPUT_LEN * 1.0 / sizeof(short) / sample_rate;
#include "evrcc.h"
#include "nmsis_bench.h"
BENCH_DECLARE_VAR();
using namespace std;
int verify_result(const uint8_t *ref, const uint8_t *res, int len,
int threshold) {
for (int i = 0; i < len; i++) {
if (abs(ref[i] - res[i]) > threshold) {
printf(
"Result mismatch at byte %d!, expected 0x%02x, got 0x%02x\r\n",
i, ref[i], res[i]);
return EXIT_FAILURE;
}
}
printf("Result matches!\r\n");
return EXIT_SUCCESS;
}
int encodefile() {
size_t nFrame = INPUT_LEN / 320;
int16_t *pcm = (int16_t *)input;
void *ct = evrc_encoder_init(4, 4, 1);
BENCH_START(evrc_encode);
int r = evrc_encoder_encode_to_stream(ct, (short *)&(pcm[0]),
INPUT_LEN / sizeof(short), enc_result,
1024 * 100);
BENCH_SAMPLE(evrc_encode);
/* NOTE: There is an overflow risk */
unsigned long used_cycle = BENCH_GET_USECYC();
double mcps = used_cycle * 1.0 / 1000000 / total_sec;
printf("encode %d frames for %d ms, use %lu cycles\r\n", nFrame,
nFrame * 20, used_cycle);
printf("CSV, evrc_encode, %.02f\r\n", mcps);
evrc_encoder_uninit(ct);
return 0;
}
int decodefile() {
void *ct = evrc_decoder_init();
const int words = evrc_decoder_stream_max_sample(enc_evrc, ENC_EVRC_LEN);
short *pcm_buf = (short *)dec_result;
BENCH_START(evrc_decode);
int bytes = evrc_decoder_decode_from_stream(ct, enc_evrc, ENC_EVRC_LEN,
pcm_buf, words);
BENCH_SAMPLE(evrc_decode);
unsigned long used_cycle = BENCH_GET_USECYC();
if (bytes > 0) {
double mcps = used_cycle * 1.0 / 1000000 / total_sec;
printf("decode %d frames\r\n", bytes / 320);
printf("CSV, evrc_decode, %.02f\r\n", mcps);
}
evrc_decoder_uninit(ct);
return 0;
}
int main(int argc, char *argv[]) {
encodefile();
if (verify_result(enc_evrc, enc_result, ENC_EVRC_LEN, 0) != EXIT_SUCCESS) {
printf("FAIL\r\n");
return EXIT_FAILURE;
}
decodefile();
if (verify_result(dec_raw, dec_result, DEC_RAW_LEN, 0) != EXIT_SUCCESS) {
printf("FAIL\r\n");
return EXIT_FAILURE;
}
printf("PASS\r\n");
return EXIT_SUCCESS;
}