|
Asterisk - The Open Source Telephony Project GIT-master-4c84066
|
Codec Translation Roundtrip Tests. More...
#include "asterisk.h"#include <math.h>#include "asterisk/module.h"#include "asterisk/test.h"#include "asterisk/translate.h"#include "asterisk/format.h"#include "asterisk/format_cap.h"#include "asterisk/format_cache.h"#include "asterisk/codec.h"#include "asterisk/frame.h"#include "asterisk/slin.h"#include "asterisk/logger.h"#include "asterisk/utils.h"
Go to the source code of this file.
Data Structures | |
| struct | codec_test_entry |
| Codec roundtrip entry: table of codecs to test. More... | |
Macros | |
| #define | CHUNK_MS 20 |
| #define | MAX_SAMPLE_ERR_LOSSLESS 256 |
| #define | MIN_DECODED_RATIO 0.90 |
| #define | MIN_SNR_LOSSY_DB 15.0 |
| #define | TEST_DURATION_SECS 2 |
Functions | |
| AST_MODULE_INFO_STANDARD_EXTENDED (ASTERISK_GPL_KEY, "Codec Translation Roundtrip Tests") | |
| AST_TEST_DEFINE (codec_translate_roundtrip) | |
| static enum ast_test_result_state | check_codec (struct ast_test *test, struct ast_format *slin_fmt, struct ast_format *target_fmt, const int16_t *orig_buf, int total_samples, int sample_rate, int is_lossy, double min_snr_db) |
| Attempt a roundtrip encode/decode for one codec format. | |
| static int | compute_max_error (const int16_t *orig, const int16_t *roundtrip, int samples) |
| Compute maximum absolute per-sample error. | |
| static double | compute_snr (const int16_t *orig, const int16_t *roundtrip, int samples) |
| Compute Signal-to-Noise Ratio between original and roundtripped signal. | |
| static double | compute_snr_aligned (const int16_t *orig, const int16_t *decoded, int samples, int max_delay, int *delay_out) |
| Compute SNR after aligning the decoded signal via cross-correlation. | |
| static void | generate_speech_signal (int16_t *buf, int samples, int sample_rate) |
| Generate a synthetic speech-like test signal in linear sample. | |
| static int | load_module (void) |
| static int | unload_module (void) |
Variables | |
| static const struct codec_test_entry | codec_table [] |
Codec Translation Roundtrip Tests.
Tests that encoding sample frames through a codec and decoding them back to slin produces output that is (almost) identical to the original input. This verifies that each codec translator pair (slin -> codec -> slin) does not corrupt or destroy the audio signal.
For near-lossless codecs (alaw, ulaw) the tolerance is very tight. For lossy codecs (adpcm, g726, gsm, g722, speex, ilbc, opus, g729, etc.) a Signal-to-Noise Ratio (SNR) threshold is used since some degradation is inherent to the compression algorithm. Wideband codecs are tested with a matching slin16/slin32/slin48 signal at the codec's native sample rate.
Definition in file test_codec_translations.c.
| #define CHUNK_MS 20 |
Chunk duration in milliseconds to feed chunks to the coders
Definition at line 61 of file test_codec_translations.c.
| #define MAX_SAMPLE_ERR_LOSSLESS 256 |
Maximum per-sample absolute error for near-lossless codecs (ulaw/alaw). μ-law/A-law quantisation can reach a max error of 256.
Definition at line 69 of file test_codec_translations.c.
| #define MIN_DECODED_RATIO 0.90 |
Minimum fraction of input samples that must survive the roundtrip. Codec lookahead + trailing partial frame may consume some samples; require at least 90 % to call the test valid.
Definition at line 74 of file test_codec_translations.c.
| #define MIN_SNR_LOSSY_DB 15.0 |
Minimum acceptable SNR in dB for lossy codecs. Most telephony codecs at 8 kHz should exceed ~20 dB
Definition at line 65 of file test_codec_translations.c.
| #define TEST_DURATION_SECS 2 |
Duration of the test signal in seconds
Definition at line 58 of file test_codec_translations.c.
| AST_MODULE_INFO_STANDARD_EXTENDED | ( | ASTERISK_GPL_KEY | , |
| "Codec Translation Roundtrip Tests" | |||
| ) |
| AST_TEST_DEFINE | ( | codec_translate_roundtrip | ) |
Definition at line 435 of file test_codec_translations.c.
References ao2_cleanup, ao2_ref, ARRAY_LEN, ast_format_cache_get, ast_format_slin, ast_format_slin16, ast_format_slin32, ast_format_slin48, ast_free, ast_malloc, AST_TEST_FAIL, AST_TEST_NOT_RUN, AST_TEST_PASS, ast_test_status_update, ast_translate_path_steps(), check_codec(), codec_table, generate_speech_signal(), MIN_SNR_LOSSY_DB, codec_test_entry::sample_rate, TEST_DURATION_SECS, TEST_EXECUTE, and TEST_INIT.
|
static |
Attempt a roundtrip encode/decode for one codec format.
Feeds the original slin signal through the encoder and decoder in 20 ms chunks (matching the universal VoIP frame duration), accumulates the decoded output, and compares the full result against the original. This naturally respects every codec's internal buffer sizes and frame granularity without needing per-codec sample-count overrides.
| [in] | test | Test framework handle (for status messages) |
| [in] | slin_fmt | The slin format appropriate for the codec's sample rate |
| [in] | target_fmt | The codec format to test |
| [in] | orig_buf | Original slin sample buffer |
| [in] | total_samples | Number of samples in orig_buf |
| [in] | sample_rate | Sample rate in Hz |
| [in] | is_lossy | If non-zero, use SNR-based comparison; otherwise use max-error |
| [in] | min_snr_db | Minimum acceptable SNR for lossy check (ignored when !is_lossy) |
| AST_TEST_PASS | on success |
| AST_TEST_FAIL | on failure |
Definition at line 228 of file test_codec_translations.c.
References ast_calloc, ast_format_get_name(), AST_FRAME_VOICE, ast_free, AST_LIST_NEXT, AST_TEST_FAIL, AST_TEST_PASS, ast_test_status_update, ast_translate(), ast_translator_build_path(), ast_translator_free_path(), CHUNK_MS, cleanup(), compute_max_error(), compute_snr(), compute_snr_aligned(), copy(), ast_frame::data, ast_frame::datalen, ast_frame_subclass::format, ast_frame::frametype, ast_frame::mallocd, MAX_SAMPLE_ERR_LOSSLESS, MIN_DECODED_RATIO, NULL, ast_frame::offset, ast_frame::ptr, result, ast_frame::samples, ast_frame::src, and ast_frame::subclass.
Referenced by AST_TEST_DEFINE().
|
static |
Compute maximum absolute per-sample error.
Definition at line 193 of file test_codec_translations.c.
References abs.
Referenced by check_codec().
|
static |
Compute Signal-to-Noise Ratio between original and roundtripped signal.
| [in] | orig | Original sample buffer |
| [in] | roundtrip | Decoded (roundtripped) sample buffer |
| [in] | samples | Number of samples |
Definition at line 109 of file test_codec_translations.c.
Referenced by check_codec(), and compute_snr_aligned().
|
static |
Compute SNR after aligning the decoded signal via cross-correlation.
Some codecs (e.g. speex, opus) introduce an algorithmic lookahead delay: the decoded signal is time-shifted by a fixed number of samples relative to the original. Computing SNR without compensating for this shift yields a near-zero result even when the codec works perfectly.
This function searches delays d = 0..max_delay, finds the integer shift that maximises the cross-correlation sum orig[i]·decoded[i+d], then returns the SNR computed at that optimal alignment.
| [in] | orig | Original sample buffer (must hold at least samples values) |
| [in] | decoded | Decoded sample buffer (must hold at least samples values) |
| [in] | samples | Number of samples available in each buffer |
| [in] | max_delay | Search range: delays 0..max_delay are tested |
| [out] | delay_out | Receives the best-fit delay found; may be NULL |
Definition at line 153 of file test_codec_translations.c.
References compute_snr(), and d.
Referenced by check_codec().
|
static |
Generate a synthetic speech-like test signal in linear sample.
a mix of a 200 Hz fundamental and an 800 Hz harmonic with a slow 4 Hz amplitude-modulation envelope, loosely inspired by ITU-T P.50 artificial voice.
| [out] | buf | Buffer to fill (must hold at least samples int16 values) |
| [in] | samples | Number of samples to generate |
| [in] | sample_rate | Sampling rate in Hz (used to scale time correctly) |
Definition at line 87 of file test_codec_translations.c.
Referenced by AST_TEST_DEFINE().
|
static |
Definition at line 546 of file test_codec_translations.c.
References AST_MODULE_LOAD_SUCCESS, and AST_TEST_REGISTER.
|
static |
Definition at line 540 of file test_codec_translations.c.
References AST_TEST_UNREGISTER.
|
static |
Table of codecs to roundtrip-test.
Definition at line 403 of file test_codec_translations.c.
Referenced by AST_TEST_DEFINE().