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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
|
/**
* @file opus.c Opus Audio Codec
*
* Copyright (C) 2010 Alfred E. Heggestad
*/
#include <re.h>
#include <baresip.h>
#include <opus/opus.h>
#include "opus.h"
/**
* @defgroup opus opus
*
* The OPUS audio codec
*
* Supported version: libopus 1.0.0 or later
*
* Configuration options:
*
\verbatim
opus_stereo yes # Request peer to send stereo
opus_sprop_stereo yes # Sending stereo
opus_bitrate 128000 # Average bitrate in [bps]
opus_cbr {yes,no} # Constant Bitrate (inverse of VBR)
opus_inbandfec {yes,no} # Enable inband Forward Error Correction (FEC)
opus_dtx {yes,no} # Enable Discontinuous Transmission (DTX)
opus_complexity {0-10} # Encoder's computational complexity (10 max)
opus_application {audio, voip} # Encoder's intended application
opus_packet_loss {0-100} # Expected packet loss for FEC
\endverbatim
*
* References:
*
* RFC 6716 Definition of the Opus Audio Codec
* RFC 7587 RTP Payload Format for the Opus Speech and Audio Codec
*
* http://opus-codec.org/downloads/
*/
static bool opus_mirror;
static char fmtp[256];
static char fmtp_mirror[256];
uint32_t opus_complexity = 10;
opus_int32 opus_application = OPUS_APPLICATION_AUDIO;
opus_int32 opus_packet_loss = 0;
static int opus_fmtp_enc(struct mbuf *mb, const struct sdp_format *fmt,
bool offer, void *arg)
{
bool mirror;
(void)arg;
(void)offer;
if (!mb || !fmt)
return 0;
mirror = !offer && str_isset(fmtp_mirror);
return mbuf_printf(mb, "a=fmtp:%s %s\r\n",
fmt->id, mirror ? fmtp_mirror : fmtp);
}
static struct aucodec opus = {
.name = "opus",
.srate = 48000,
.crate = 48000,
.ch = 2,
.pch = 2,
.fmtp = fmtp,
.encupdh = opus_encode_update,
.ench = opus_encode_frm,
.decupdh = opus_decode_update,
.dech = opus_decode_frm,
.plch = opus_decode_pkloss,
};
void opus_mirror_params(const char *x)
{
if (!opus_mirror)
return;
info("opus: mirror parameters: \"%s\"\n", x);
str_ncpy(fmtp_mirror, x, sizeof(fmtp_mirror));
}
static int module_init(void)
{
struct conf *conf = conf_cur();
uint32_t value;
char *p = fmtp;
bool b, stereo = true, sprop_stereo = true;
struct pl pl;
int n = 0;
fmtp[0] = '\0';
fmtp_mirror[0] = '\0';
conf_get_bool(conf, "opus_stereo", &stereo);
conf_get_bool(conf, "opus_sprop_stereo", &sprop_stereo);
if (!stereo || !sprop_stereo)
opus.ch = 1;
/* always set stereo parameter first */
n = re_snprintf(p, sizeof(fmtp) - str_len(p),
"stereo=%d;sprop-stereo=%d", stereo, sprop_stereo);
if (n <= 0)
return ENOMEM;
p += n;
if (0 == conf_get_u32(conf, "opus_bitrate", &value)) {
n = re_snprintf(p, sizeof(fmtp) - str_len(p),
";maxaveragebitrate=%d", value);
if (n <= 0)
return ENOMEM;
p += n;
}
if (0 == conf_get_u32(conf, "opus_samplerate", &value)) {
if ((value != 8000) && (value != 12000) && (value != 16000) &&
(value != 24000) && (value != 48000)) {
warning("opus: invalid samplerate: %d\n", value);
return EINVAL;
}
opus.srate = value;
}
if (0 == conf_get_bool(conf, "opus_cbr", &b)) {
n = re_snprintf(p, sizeof(fmtp) - str_len(p),
";cbr=%d", b);
if (n <= 0)
return ENOMEM;
p += n;
}
if (0 == conf_get_bool(conf, "opus_inbandfec", &b)) {
n = re_snprintf(p, sizeof(fmtp) - str_len(p),
";useinbandfec=%d", b);
if (n <= 0)
return ENOMEM;
p += n;
}
if (0 == conf_get_bool(conf, "opus_dtx", &b)) {
n = re_snprintf(p, sizeof(fmtp) - str_len(p),
";usedtx=%d", b);
if (n <= 0)
return ENOMEM;
}
(void)conf_get_bool(conf, "opus_mirror", &opus_mirror);
if (opus_mirror) {
opus.fmtp = NULL;
opus.fmtp_ench = opus_fmtp_enc;
}
(void)conf_get_u32(conf, "opus_complexity", &opus_complexity);
if (opus_complexity > 10)
opus_complexity = 10;
if (!conf_get(conf, "opus_application", &pl)) {
if (!pl_strcasecmp(&pl, "audio"))
opus_application = OPUS_APPLICATION_AUDIO;
else if (!pl_strcasecmp(&pl, "voip"))
opus_application = OPUS_APPLICATION_VOIP;
else {
warning("opus: unknown encoder application: %r\n",
&pl);
return EINVAL;
}
}
if (0 == conf_get_u32(conf, "opus_packet_loss", &value)) {
if (value > 100)
opus_packet_loss = 100;
else
opus_packet_loss = value;
}
debug("opus: fmtp=\"%s\"\n", fmtp);
aucodec_register(baresip_aucodecl(), &opus);
return 0;
}
static int module_close(void)
{
aucodec_unregister(&opus);
return 0;
}
EXPORT_SYM const struct mod_export DECL_EXPORTS(opus) = {
"opus",
"audio codec",
module_init,
module_close,
};
|