media_audio.c wraps libopus for 48 kHz mono, 20 ms frames (960 samples), 24 kbit/s, in-band FEC and a 10 percent loss hint; dc_opus_dec_frame with len 0 conceals a lost frame. configure requires libopus (OPUSLIB). test_audio_opus encodes/decodes 2 s of a 440 Hz sine, checks the frame size, a decoded RMS above half the input, a 3-12 KB budget for 24 kbit/s and a non-silent PLC frame after a loss; standalone and ASan/UBSan clean. libopus 1.6.1 was installed on the build host together with libstrophe, avahi-client and libvpx, so T4/T5.2/T6/T7 are no longer missing libraries. In-tree build pending: a foreign release chain holds the lock (pid 141722).
111 lines
2.3 KiB
C
111 lines
2.3 KiB
C
/*
|
|
* test_audio_opus.c -- tests for the Opus wrapper (T6.2)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*/
|
|
#include "dc_internal.h"
|
|
|
|
#include <math.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#ifndef M_PI
|
|
#define M_PI 3.14159265358979323846
|
|
#endif
|
|
|
|
static int fails;
|
|
|
|
#define CHECK(cond) do {\
|
|
if (!(cond)) {\
|
|
fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__,\
|
|
#cond);\
|
|
fails++;\
|
|
}\
|
|
} while (0)
|
|
|
|
#define FRAMES 100 /* two seconds */
|
|
|
|
static double
|
|
rms(const int16_t *pcm, size_t n)
|
|
{
|
|
double sum = 0;
|
|
size_t i;
|
|
|
|
for (i = 0; i < n; i++)
|
|
sum += (double)pcm[i] * (double)pcm[i];
|
|
return n > 0 ? sqrt(sum / (double)n) : 0;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
DcOpusEnc *enc;
|
|
DcOpusDec *dec;
|
|
unsigned char packet[400];
|
|
int16_t in[DC_AUDIO_FRAME_SAMPLES];
|
|
int16_t out[DC_AUDIO_FRAME_SAMPLES];
|
|
long long bytes = 0;
|
|
double decoded_rms;
|
|
double input_rms;
|
|
int frames_ok = 0;
|
|
int i, n;
|
|
|
|
enc = dc_opus_enc_new(24000, NULL, 0);
|
|
dec = dc_opus_dec_new(NULL, 0);
|
|
CHECK(enc != NULL);
|
|
CHECK(dec != NULL);
|
|
if (enc == NULL || dec == NULL)
|
|
return 1;
|
|
|
|
for (i = 0; i < FRAMES; i++) {
|
|
int s;
|
|
int16_t *dst = in;
|
|
|
|
for (s = 0; s < DC_AUDIO_FRAME_SAMPLES; s++) {
|
|
double t = (double)(i * DC_AUDIO_FRAME_SAMPLES + s) /
|
|
(double)DC_AUDIO_RATE;
|
|
|
|
in[s] = (int16_t)(sin(2.0 * M_PI * 440.0 * t) *
|
|
9830.0);
|
|
}
|
|
n = dc_opus_enc_frame(enc, dst, packet, sizeof(packet));
|
|
CHECK(n > 0);
|
|
if (n <= 0)
|
|
break;
|
|
bytes += n;
|
|
n = dc_opus_dec_frame(dec, packet, (size_t)n, out);
|
|
CHECK(n == DC_AUDIO_FRAME_SAMPLES);
|
|
if (n == DC_AUDIO_FRAME_SAMPLES && i > 5) {
|
|
decoded_rms = rms(out, DC_AUDIO_FRAME_SAMPLES);
|
|
input_rms = rms(dst, DC_AUDIO_FRAME_SAMPLES);
|
|
if (decoded_rms > input_rms * 0.5)
|
|
frames_ok++;
|
|
}
|
|
}
|
|
CHECK(frames_ok > FRAMES - 15);
|
|
/* 24 kbit/s for 2 s is 6000 bytes; allow the codec's overhead */
|
|
CHECK(bytes > 3000 && bytes < 12000);
|
|
|
|
/* packet loss concealment: after good frames, an empty packet */
|
|
n = dc_opus_dec_frame(dec, NULL, 0, out);
|
|
CHECK(n == DC_AUDIO_FRAME_SAMPLES);
|
|
{
|
|
int non_zero = 0;
|
|
|
|
for (i = 0; i < n; i++)
|
|
if (out[i] != 0)
|
|
non_zero++;
|
|
CHECK(non_zero > 0);
|
|
}
|
|
|
|
dc_opus_enc_free(enc);
|
|
dc_opus_dec_free(dec);
|
|
if (fails != 0) {
|
|
fprintf(stderr, "%d test(s) failed\n", fails);
|
|
return 1;
|
|
}
|
|
printf("test_audio_opus: all ok\n");
|
|
return 0;
|
|
}
|