3#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
7#ifndef OPUS_ENC_MAX_BUFFER_SIZE
8#define OPUS_ENC_MAX_BUFFER_SIZE 2048
11#ifndef OPUS_DEC_MAX_BUFFER_SIZE
12#define OPUS_DEC_MAX_BUFFER_SIZE 4 * 1024
31 int max_buffer_size = OPUS_DEC_MAX_BUFFER_SIZE;
32 int max_buffer_write_size = 512;
71 max_buffer_size = OPUS_ENC_MAX_BUFFER_SIZE;
147 bool begin(OpusSettings settings) {
151 notifyAudioChange(cfg);
155 bool begin()
override {
158 LOGE(
"Sample rate not supported: %d", cfg.
sample_rate);
161 outbuf.resize(cfg.max_buffer_size);
162 assert(outbuf.data() !=
nullptr);
167 size_t size = opus_decoder_get_size(cfg.
channels);
169 assert(decbuf.data() !=
nullptr);
170 dec = (OpusDecoder *)decbuf.data();
173 if (err != OPUS_OK) {
174 LOGE(
"opus_decoder_create: %s for sample_rate: %d, channels:%d",
182 void end()
override {
199 size_t write(
const uint8_t *data,
size_t len)
override {
200 if (!active || p_print ==
nullptr)
return 0;
202 LOGD(
"OpusAudioDecoder::write: %d", (
int)len);
203 int in_band_forward_error_correction = 0;
204 int frame_count = cfg.max_buffer_size / cfg.
channels /
sizeof(opus_int16);
206 opus_decode(dec, (uint8_t *)data, len, (opus_int16 *)outbuf.data(),
207 frame_count, in_band_forward_error_correction);
208 if (out_samples < 0) {
209 LOGW(
"opus-decode: %s", opus_strerror(out_samples));
210 }
else if (out_samples > 0) {
212 int out_bytes = out_samples * cfg.
channels *
sizeof(int16_t);
213 LOGD(
"opus-decode: %d", out_bytes);
214 int open = out_bytes;
217 int to_write = std::min(open, cfg.max_buffer_write_size);
218 int written = p_print->write(outbuf.data() + processed, to_write);
220 processed += written;
226 operator bool()
override {
return active; }
229 Print *p_print =
nullptr;
230 OpusDecoder *dec =
nullptr;
233 Vector<uint8_t> outbuf{0};
234 Vector<uint8_t> decbuf{0};
235 const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
237 bool isValidRate(
int rate) {
238 for (
auto &valid : valid_rates) {
239 if (valid == rate)
return true;
267 const char *
mime()
override {
return "audio/opus"; }
282 assert(frame.data() !=
nullptr);
283 enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application,
285 if (err != OPUS_OK) {
286 LOGE(
"opus_encoder_create: %s for sample_rate: %d, channels:%d",
287 opus_strerror(err), cfg.sample_rate, cfg.channels);
290 is_open = settings();
299 bool begin(OpusEncoderSettings settings) {
309 opus_encoder_destroy(enc);
314 size_t write(
const uint8_t *data,
size_t len)
override {
315 if (!is_open || p_print ==
nullptr)
return 0;
316 LOGD(
"OpusAudioEncoder::write: %d", (
int)len);
319 for (
int j = 0; j < len; j++) {
325 operator bool()
override {
return is_open; }
327 bool isOpen() {
return is_open; }
330 Print *p_print =
nullptr;
331 OpusEncoder *enc =
nullptr;
332 OpusEncoderSettings cfg;
333 bool is_open =
false;
334 Vector<uint8_t> frame{0};
337 void encodeByte(uint8_t data) {
339 frame[frame_pos++] = data;
342 if (frame_pos >= frame.size()) {
349 if (frame.size() > 0) {
352 OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
353 uint8_t packet[packet_len];
355 int frames = frame.size() / cfg.channels /
sizeof(int16_t);
356 LOGD(
"opus_encode - frame_size: %d", frames);
357 int len = opus_encode(enc, (opus_int16 *)frame.data(), frames, packet,
360 LOGE(
"opus_encode: %s", opus_strerror(len));
361 }
else if (len > 0) {
362 LOGD(
"opus-encode: %d", len);
363 int eff = p_print->write(packet, len);
365 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
373 switch (cfg.frame_sizes_ms_x2) {
374 case OPUS_FRAMESIZE_2_5_MS:
375 return sampling_rate / 400;
376 case OPUS_FRAMESIZE_5_MS:
377 return sampling_rate / 200;
378 case OPUS_FRAMESIZE_10_MS:
379 return sampling_rate / 100;
380 case OPUS_FRAMESIZE_20_MS:
381 return sampling_rate / 50;
382 case OPUS_FRAMESIZE_40_MS:
383 return sampling_rate / 25;
384 case OPUS_FRAMESIZE_60_MS:
385 return 3 * sampling_rate / 50;
386 case OPUS_FRAMESIZE_80_MS:
387 return 4 * sampling_rate / 50;
388 case OPUS_FRAMESIZE_100_MS:
389 return 5 * sampling_rate / 50;
390 case OPUS_FRAMESIZE_120_MS:
391 return 6 * sampling_rate / 50;
393 return sampling_rate / 100;
398 if (cfg.bitrate >= 0 &&
399 opus_encoder_ctl(enc, OPUS_SET_BITRATE(cfg.bitrate)) != OPUS_OK) {
400 LOGE(
"invalid bitrate: %d", cfg.bitrate);
403 if (cfg.force_channel >= 0 &&
404 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(cfg.force_channel)) !=
406 LOGE(
"invalid force_channel: %d", cfg.force_channel);
410 opus_encoder_ctl(enc, OPUS_SET_VBR(cfg.vbr)) != OPUS_OK) {
411 LOGE(
"invalid vbr: %d", cfg.vbr);
414 if (cfg.vbr_constraint >= 0 &&
415 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cfg.vbr_constraint)) !=
417 LOGE(
"invalid vbr_constraint: %d", cfg.vbr_constraint);
420 if (cfg.complexity >= 0 &&
421 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(cfg.complexity)) != OPUS_OK) {
422 LOGE(
"invalid complexity: %d", cfg.complexity);
425 if (cfg.max_bandwidth >= 0 &&
426 opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(cfg.max_bandwidth)) !=
428 LOGE(
"invalid max_bandwidth: %d", cfg.max_bandwidth);
431 if (cfg.signal >= 0 &&
432 opus_encoder_ctl(enc, OPUS_SET_SIGNAL(cfg.signal)) != OPUS_OK) {
433 LOGE(
"invalid signal: %d", cfg.signal);
436 if (cfg.inband_fec >= 0 &&
437 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(cfg.inband_fec)) != OPUS_OK) {
438 LOGE(
"invalid inband_fec: %d", cfg.inband_fec);
441 if (cfg.packet_loss_perc >= 0 &&
443 enc, OPUS_SET_PACKET_LOSS_PERC(cfg.packet_loss_perc)) != OPUS_OK) {
444 LOGE(
"invalid pkt_loss: %d", cfg.packet_loss_perc);
447 if (cfg.lsb_depth >= 0 &&
448 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(cfg.lsb_depth)) != OPUS_OK) {
449 LOGE(
"invalid lsb_depth: %d", cfg.lsb_depth);
452 if (cfg.prediction_disabled >= 0 &&
453 opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(
454 cfg.prediction_disabled)) != OPUS_OK) {
455 LOGE(
"invalid pred_disabled: %d", cfg.prediction_disabled);
458 if (cfg.use_dtx >= 0 &&
459 opus_encoder_ctl(enc, OPUS_SET_DTX(cfg.use_dtx)) != OPUS_OK) {
460 LOGE(
"invalid use_dtx: %d", cfg.use_dtx);
463 if (cfg.frame_sizes_ms_x2 > 0 &&
464 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(
465 cfg.frame_sizes_ms_x2)) != OPUS_OK) {
466 LOGE(
"invalid frame_sizes_ms_x2: %d", cfg.frame_sizes_ms_x2);