| 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 |
#include "asterisk.h" |
| 42 |
|
| 43 |
#include <math.h> |
| 44 |
|
| 45 |
#include "asterisk/module.h" |
| 46 |
#include "asterisk/test.h" |
| 47 |
#include "asterisk/translate.h" |
| 48 |
#include "asterisk/format.h" |
| 49 |
#include "asterisk/format_cap.h" |
| 50 |
#include "asterisk/format_cache.h" |
| 51 |
#include "asterisk/codec.h" |
| 52 |
#include "asterisk/frame.h" |
| 53 |
#include "asterisk/slin.h" |
| 54 |
#include "asterisk/logger.h" |
| 55 |
#include "asterisk/utils.h" |
| 56 |
|
| 57 |
|
| 58 |
#define TEST_DURATION_SECS 2 |
| 59 |
|
| 60 |
|
| 61 |
#define CHUNK_MS 20 |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define MIN_SNR_LOSSY_DB 15.0 |
| 66 |
|
| 67 |
|
| 68 |
/ |
| 69 |
#define MAX_SAMPLE_ERR_LOSSLESS 256 |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
#define MIN_DECODED_RATIO 0.90 |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static void generate_speech_signal(int16_t *buf, int samples, int sample_rate) |
| 88 |
{ |
| 89 |
int i; |
| 90 |
for (i = 0; i < samples; i++) { |
| 91 |
double t = (double)i / sample_rate; |
| 92 |
double sig = 0.6 * sin(2.0 * M_PI * 200.0 * t) |
| 93 |
+ 0.4 * sin(2.0 * M_PI * 800.0 * t); |
| 94 |
|
| 95 |
sig *= 0.5 * (1.0 + sin(2.0 * M_PI * 4.0 * t)); |
| 96 |
buf[i] = (int16_t)(sig * 16000.0); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
static double compute_snr(const int16_t *orig, const int16_t *roundtrip, int samples) |
| 110 |
{ |
| 111 |
double signal_power = 0.0; |
| 112 |
double noise_power = 0.0; |
| 113 |
int i; |
| 114 |
|
| 115 |
for (i = 0; i < samples; i++) { |
| 116 |
double s = (double)orig[i]; |
| 117 |
double n = (double)(orig[i] - roundtrip[i]); |
| 118 |
signal_power += s * s; |
| 119 |
noise_power += n * n; |
| 120 |
} |
| 121 |
|
| 122 |
if (signal_power < 1.0) { |
| 123 |
return -100.0; |
| 124 |
} |
| 125 |
if (noise_power < 1.0) { |
| 126 |
return 999.0; |
| 127 |
} |
| 128 |
|
| 129 |
return 10.0 * log10(signal_power / noise_power); |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
s |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
static double compute_snr_aligned(const int16_t *orig, const int16_t *decoded, |
| 154 |
int samples, int max_delay, int *delay_out) |
| 155 |
{ |
| 156 |
double best_corr = -1e300; |
| 157 |
int best_delay = 0; |
| 158 |
int d; |
| 159 |
|
| 160 |
if (delay_out) { |
| 161 |
*delay_out = 0; |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
if (samples <= max_delay || max_delay <= 0) { |
| 166 |
return compute_snr(orig, decoded, samples); |
| 167 |
} |
| 168 |
|
| 169 |
for (d = 0; d <= max_delay; d++) { |
| 170 |
int n = samples - d; |
| 171 |
double corr = 0.0; |
| 172 |
int i; |
| 173 |
for (i = 0; i < n; i++) { |
| 174 |
corr += (double)orig[i] * (double)decoded[i + d]; |
| 175 |
} |
| 176 |
if (corr > best_corr) { |
| 177 |
best_corr = corr; |
| 178 |
best_delay = d; |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
if (delay_out) { |
| 183 |
*delay_out = best_delay; |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
return compute_snr(orig, decoded + best_delay, samples - best_delay); |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
static int compute_max_error(const int16_t *orig, const int16_t *roundtrip, int samples) |
| 194 |
{ |
| 195 |
int max_err = 0; |
| 196 |
int i; |
| 197 |
|
| 198 |
for (i = 0; i < samples; i++) { |
| 199 |
int err = abs((int)orig[i] - (int)roundtrip[i]); |
| 200 |
if (err > max_err) { |
| 201 |
max_err = err; |
| 202 |
} |
| 203 |
} |
| 204 |
return max_err; |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
static enum ast_test_result_state check_codec( |
| 229 |
struct ast_test *test, |
| 230 |
struct ast_format *slin_fmt, |
| 231 |
struct ast_format *target_fmt, |
| 232 |
const int16_t *orig_buf, |
| 233 |
int total_samples, |
| 234 |
int sample_rate, |
| 235 |
int is_lossy, |
| 236 |
double min_snr_db) |
| 237 |
{ |
| 238 |
struct ast_trans_pvt *encode_path = NULL; |
| 239 |
struct ast_trans_pvt *decode_path = NULL; |
| 240 |
int16_t *decoded_buf = NULL; |
| 241 |
int total_decoded = 0; |
| 242 |
int chunk_samples = sample_rate * CHUNK_MS / 1000; |
| 243 |
int buf_capacity; |
| 244 |
int offset; |
| 245 |
enum ast_test_result_state result = AST_TEST_FAIL; |
| 246 |
const char *codec_name; |
| 247 |
|
| 248 |
codec_name = ast_format_get_name(target_fmt); |
| 249 |
|
| 250 |
|
| 251 |
encode_path = ast_translator_build_path(target_fmt, slin_fmt); |
| 252 |
if (!encode_path) { |
| 253 |
ast_test_status_update(test, |
| 254 |
"Skipping %s: no translation path from slin to %s\n", |
| 255 |
codec_name, codec_name); |
| 256 |
return AST_TEST_PASS; |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
decode_path = ast_translator_build_path(slin_fmt, target_fmt); |
| 261 |
if (!decode_path) { |
| 262 |
ast_test_status_update(test, |
| 263 |
"FAIL %s: found encoder but no decoder path back to slin\n", |
| 264 |
codec_name); |
| 265 |
goto cleanup; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
buf_capacity = total_samples + chunk_samples; |
| 270 |
decoded_buf = ast_calloc(buf_capacity, sizeof(int16_t)); |
| 271 |
if (!decoded_buf) { |
| 272 |
goto cleanup; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
for (offset = 0; offset < total_samples; offset += chunk_samples) { |
| 277 |
struct ast_frame input_frame; |
| 278 |
struct ast_frame *encoded; |
| 279 |
struct ast_frame *decoded; |
| 280 |
struct ast_frame *cur; |
| 281 |
int feed = total_samples - offset; |
| 282 |
if (feed > chunk_samples) { |
| 283 |
feed = chunk_samples; |
| 284 |
} |
| 285 |
|
| 286 |
memset(&input_frame, 0, sizeof(input_frame)); |
| 287 |
input_frame.frametype = AST_FRAME_VOICE; |
| 288 |
input_frame.subclass.format = slin_fmt; |
| 289 |
input_frame.datalen = feed * sizeof(int16_t); |
| 290 |
input_frame.samples = feed; |
| 291 |
input_frame.data.ptr = (void *)(orig_buf + offset); |
| 292 |
input_frame.mallocd = 0; |
| 293 |
input_frame.src = "test_codec_translations"; |
| 294 |
|
| 295 |
|
| 296 |
encoded = ast_translate(encode_path, &input_frame, 0); |
| 297 |
if (!encoded) { |
| 298 |
continue; |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
decoded = ast_translate(decode_path, encoded, 0); |
| 304 |
if (!decoded) { |
| 305 |
continue; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
for (cur = decoded; cur; cur = AST_LIST_NEXT(cur, frame_list)) { |
| 311 |
int copy; |
| 312 |
if (cur->frametype != AST_FRAME_VOICE || !cur->data.ptr |
| 313 |
|| cur->samples <= 0) { |
| 314 |
continue; |
| 315 |
} |
| 316 |
copy = cur->samples; |
| 317 |
if (total_decoded + copy > buf_capacity) { |
| 318 |
copy = buf_capacity - total_decoded; |
| 319 |
} |
| 320 |
memcpy(decoded_buf + total_decoded, cur->data.ptr, |
| 321 |
copy * sizeof(int16_t)); |
| 322 |
total_decoded += copy; |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
if (total_decoded < (int)(total_samples * MIN_DECODED_RATIO)) { |
| 328 |
ast_test_status_update(test, |
| 329 |
"FAIL %s: decoded only %d of %d samples (%.0f%%)\n", |
| 330 |
codec_name, total_decoded, total_samples, |
| 331 |
100.0 * total_decoded / total_samples); |
| 332 |
goto cleanup; |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
{ |
| 337 |
int cmp_samples = total_decoded < total_samples |
| 338 |
? total_decoded : total_samples; |
| 339 |
|
| 340 |
if (is_lossy) { |
| 341 |
int max_delay = sample_rate * 20 / 1000; |
| 342 |
int delay = 0; |
| 343 |
double snr = compute_snr_aligned(orig_buf, decoded_buf, |
| 344 |
cmp_samples, max_delay, &delay); |
| 345 |
|
| 346 |
ast_test_status_update(test, |
| 347 |
" %s (lossy): SNR = %.1f dB (threshold %.1f dB)" |
| 348 |
" [%d/%d samples, delay=%d/%.1fms]\n", |
| 349 |
codec_name, snr, min_snr_db, |
| 350 |
cmp_samples, total_samples, |
| 351 |
delay, 1000.0 * delay / sample_rate); |
| 352 |
|
| 353 |
if (snr < min_snr_db) { |
| 354 |
ast_test_status_update(test, |
| 355 |
"FAIL %s: SNR %.1f dB is below minimum %.1f dB\n", |
| 356 |
codec_name, snr, min_snr_db); |
| 357 |
goto cleanup; |
| 358 |
} |
| 359 |
} else { |
| 360 |
int max_err = compute_max_error(orig_buf, decoded_buf, cmp_samples); |
| 361 |
double snr = compute_snr(orig_buf, decoded_buf, cmp_samples); |
| 362 |
|
| 363 |
ast_test_status_update(test, |
| 364 |
" %s (lossless): max_err = %d (limit %d)," |
| 365 |
" SNR = %.1f dB [%d/%d samples]\n", |
| 366 |
codec_name, max_err, MAX_SAMPLE_ERR_LOSSLESS, snr, |
| 367 |
cmp_samples, total_samples); |
| 368 |
|
| 369 |
if (max_err > MAX_SAMPLE_ERR_LOSSLESS) { |
| 370 |
ast_test_status_update(test, |
| 371 |
"FAIL %s: max sample error %d exceeds limit %d\n", |
| 372 |
codec_name, max_err, MAX_SAMPLE_ERR_LOSSLESS); |
| 373 |
goto cleanup; |
| 374 |
} |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
result = AST_TEST_PASS; |
| 379 |
|
| 380 |
cleanup: |
| 381 |
ast_free(decoded_buf); |
| 382 |
if (encode_path) { |
| 383 |
ast_translator_free_path(encode_path); |
| 384 |
} |
| 385 |
if (decode_path) { |
| 386 |
ast_translator_free_path(decode_path); |
| 387 |
} |
| 388 |
|
| 389 |
return result; |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
struct codec_test_entry { |
| 396 |
const char *format_name; |
| 397 |
int is_lossy; |
| 398 |
double min_snr_db; |
| 399 |
int sample_rate; |
| 400 |
}; |
| 401 |
|
| 402 |
|
| 403 |
static const struct codec_test_entry codec_table[] = { |
| 404 |
*/ |
| 405 |
{ "ulaw", 0 }, |
| 406 |
{ "alaw", 0 }, |
| 407 |
|
| 408 |
*/ |
| 409 |
{ "adpcm", 1 }, |
| 410 |
{ "g726", 1 }, |
| 411 |
{ "g726aal2", 1 }, |
| 412 |
{ "gsm", 1 }, |
| 413 |
{ "speex", 1, 7.0 }, |
| 414 |
{ "ilbc", 1, 7.0 }, |
| 415 |
{ "codec2", 1, -2.0 }, |
| 416 |
{ "lpc10", 1, -2.0 }, |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
*/ |
| 425 |
{ "g722", 1, 0.0, 16000 }, |
| 426 |
{ "speex16", 1, 5.0, 16000 }, |
| 427 |
|
| 428 |
*/ |
| 429 |
{ "speex32", 1, 5.0, 32000 }, |
| 430 |
|
| 431 |
|
| 432 |
{ "opus", 1, 8.0, 48000 }, |
| 433 |
}; |
| 434 |
|
| 435 |
AST_TEST_DEFINE(codec_translate_roundtrip) |
| 436 |
{ |
| 437 |
int i; |
| 438 |
int tested = 0; |
| 439 |
int failed = 0; |
| 440 |
enum ast_test_result_state overall = AST_TEST_PASS; |
| 441 |
|
| 442 |
switch (cmd) { |
| 443 |
case TEST_INIT: |
| 444 |
info->name = "codec_translations"; |
| 445 |
info->category = "/main/codec/"; |
| 446 |
info->summary = "Roundtrip encode/decode test and quality check for various codecs referenced in this test and present in the installation"; |
| 447 |
info->description = |
| 448 |
"Generates a synthetic speech-like signal (200 Hz +\n" |
| 449 |
"800 Hz with 4 Hz AM envelope) at the codec's native sample\n" |
| 450 |
"rate, feeds it through the codec and back,\n" |
| 451 |
"then verifies the output quality over the full duration.\n" |
| 452 |
"For near-lossless codecs (ulaw, alaw) it checks that the\n" |
| 453 |
"maximum per-sample error is within a tight bound.\n" |
| 454 |
"For lossy codecs it checks that the SNR exceeds a per-codec\n" |
| 455 |
"minimum threshold. Vocoders use a near-zero threshold\n" |
| 456 |
"(smoke test)."; |
| 457 |
return AST_TEST_NOT_RUN; |
| 458 |
case TEST_EXECUTE: |
| 459 |
break; |
| 460 |
} |
| 461 |
|
| 462 |
ast_test_status_update(test, |
| 463 |
"Starting codec roundtrip tests (%d codecs, %d seconds of audio)\n", |
| 464 |
(int)ARRAY_LEN(codec_table), TEST_DURATION_SECS); |
| 465 |
|
| 466 |
for (i = 0; i < (int)ARRAY_LEN(codec_table); i++) { |
| 467 |
struct ast_format *slin_fmt; |
| 468 |
struct ast_format *target_fmt; |
| 469 |
int16_t *orig_buf; |
| 470 |
enum ast_test_result_state res; |
| 471 |
int rate = codec_table[i].sample_rate > 0 |
| 472 |
? codec_table[i].sample_rate : 8000; |
| 473 |
int total_samples = rate * TEST_DURATION_SECS; |
| 474 |
|
| 475 |
|
| 476 |
switch (rate) { |
| 477 |
case 16000: |
| 478 |
slin_fmt = ast_format_slin16; |
| 479 |
break; |
| 480 |
case 32000: |
| 481 |
slin_fmt = ast_format_slin32; |
| 482 |
break; |
| 483 |
case 48000: |
| 484 |
slin_fmt = ast_format_slin48; |
| 485 |
break; |
| 486 |
default: |
| 487 |
slin_fmt = ast_format_slin; |
| 488 |
break; |
| 489 |
} |
| 490 |
|
| 491 |
target_fmt = ast_format_cache_get(codec_table[i].format_name); |
| 492 |
if (!target_fmt |
| 493 |
|| ast_translate_path_steps(target_fmt, slin_fmt) == -1) { |
| 494 |
ast_test_status_update(test, |
| 495 |
" %s: no translation path available, skipping\n", |
| 496 |
codec_table[i].format_name); |
| 497 |
ao2_cleanup(target_fmt); |
| 498 |
continue; |
| 499 |
} |
| 500 |
|
| 501 |
orig_buf = ast_malloc(total_samples * sizeof(int16_t)); |
| 502 |
if (!orig_buf) { |
| 503 |
ao2_ref(target_fmt, -1); |
| 504 |
overall = AST_TEST_FAIL; |
| 505 |
break; |
| 506 |
} |
| 507 |
|
| 508 |
generate_speech_signal(orig_buf, total_samples, rate); |
| 509 |
|
| 510 |
res = check_codec( |
| 511 |
test, slin_fmt, target_fmt, orig_buf, |
| 512 |
total_samples, rate, |
| 513 |
codec_table[i].is_lossy, |
| 514 |
codec_table[i].min_snr_db != 0.0 |
| 515 |
? codec_table[i].min_snr_db : MIN_SNR_LOSSY_DB); |
| 516 |
|
| 517 |
ast_free(orig_buf); |
| 518 |
ao2_ref(target_fmt, -1); |
| 519 |
|
| 520 |
tested++; |
| 521 |
if (res == AST_TEST_FAIL) { |
| 522 |
failed++; |
| 523 |
overall = AST_TEST_FAIL; |
| 524 |
} |
| 525 |
} |
| 526 |
|
| 527 |
ast_test_status_update(test, |
| 528 |
"\nCodec roundtrip summary: %d tested, %d passed, %d failed\n", |
| 529 |
tested, tested - failed, failed); |
| 530 |
|
| 531 |
if (tested == 0) { |
| 532 |
ast_test_status_update(test, |
| 533 |
"WARNING: No codecs were available to test. " |
| 534 |
"Ensure codec modules are loaded.\n"); |
| 535 |
} |
| 536 |
|
| 537 |
return overall; |
| 538 |
} |
| 539 |
|
| 540 |
static int unload_module(void) |
| 541 |
{ |
| 542 |
AST_TEST_UNREGISTER(codec_translate_roundtrip); |
| 543 |
return 0; |
| 544 |
} |
| 545 |
|
| 546 |
static int load_module(void) |
| 547 |
{ |
| 548 |
AST_TEST_REGISTER(codec_translate_roundtrip); |
| 549 |
return AST_MODULE_LOAD_SUCCESS; |
| 550 |
} |
| 551 |
|
| 552 |
AST_MODULE_INFO_STANDARD_EXTENDED(ASTERISK_GPL_KEY, "Codec Translation Roundtrip Tests"); |