Files
lotherk e3e4e95d07 dtchat: add the Opus encoder and decoder (T6.2)
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).
2026-11-17 07:14:51 +00:00

117 lines
2.5 KiB
C

/*
* media_audio.c -- the Opus encoder and decoder (T6.2)
*
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
*
* 48 kHz mono, 20 ms frames (960 samples), 24 kbit/s by default, in-band FEC
* and packet-loss concealment. The codec's own delay (about 6.5 ms) is not
* compensated here; the jitter buffer and the call UI handle playout.
*/
#include "dc_internal.h"
#include <stdio.h>
#include <stdlib.h>
#include <opus/opus.h>
struct DcOpusEnc {
OpusEncoder *enc;
};
struct DcOpusDec {
OpusDecoder *dec;
};
DcOpusEnc *
dc_opus_enc_new(int bitrate, char *err, size_t errlen)
{
DcOpusEnc *enc;
int error = 0;
enc = calloc(1, sizeof(*enc));
if (enc == NULL)
return NULL;
enc->enc = opus_encoder_create(DC_AUDIO_RATE, 1, OPUS_APPLICATION_VOIP,
&error);
if (enc->enc == NULL || error != OPUS_OK) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "opus encoder: %s",
opus_strerror(error));
dc_opus_enc_free(enc);
return NULL;
}
(void)opus_encoder_ctl(enc->enc, OPUS_SET_BITRATE(bitrate > 0 ?
bitrate : 24000));
(void)opus_encoder_ctl(enc->enc, OPUS_SET_INBAND_FEC(1));
(void)opus_encoder_ctl(enc->enc, OPUS_SET_PACKET_LOSS_PERC(10));
(void)opus_encoder_ctl(enc->enc, OPUS_SET_COMPLEXITY(8));
return enc;
}
int
dc_opus_enc_frame(DcOpusEnc *enc, const int16_t *pcm, unsigned char *out,
size_t outlen)
{
opus_int32 n;
if (enc == NULL || pcm == NULL || out == NULL ||
outlen > 0x7fffffff)
return -1;
n = opus_encode(enc->enc, pcm, DC_AUDIO_FRAME_SAMPLES, out,
(opus_int32)outlen);
return n < 0 ? -1 : (int)n;
}
void
dc_opus_enc_free(DcOpusEnc *enc)
{
if (enc == NULL)
return;
if (enc->enc != NULL)
opus_encoder_destroy(enc->enc);
free(enc);
}
DcOpusDec *
dc_opus_dec_new(char *err, size_t errlen)
{
DcOpusDec *dec;
int error = 0;
dec = calloc(1, sizeof(*dec));
if (dec == NULL)
return NULL;
dec->dec = opus_decoder_create(DC_AUDIO_RATE, 1, &error);
if (dec->dec == NULL || error != OPUS_OK) {
if (err != NULL && errlen > 0)
snprintf(err, errlen, "opus decoder: %s",
opus_strerror(error));
dc_opus_dec_free(dec);
return NULL;
}
return dec;
}
int
dc_opus_dec_frame(DcOpusDec *dec, const unsigned char *packet, size_t len,
int16_t *pcm)
{
int n;
if (dec == NULL || pcm == NULL)
return -1;
n = opus_decode(dec->dec, len > 0 ? packet : NULL, (opus_int32)len,
pcm, DC_AUDIO_FRAME_SAMPLES, 0);
return n < 0 ? -1 : n;
}
void
dc_opus_dec_free(DcOpusDec *dec)
{
if (dec == NULL)
return;
if (dec->dec != NULL)
opus_decoder_destroy(dec->dec);
free(dec);
}