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);
165 size_t size = opus_decoder_get_size(cfg.
channels);
167 assert(decbuf.data() !=
nullptr);
168 dec = (OpusDecoder *)decbuf.data();
171 if (err != OPUS_OK) {
172 LOGE(
"opus_decoder_create: %s for sample_rate: %d, channels:%d",
180 void end()
override {
183 if (release_on_end) {
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; }
233 Print *p_print =
nullptr;
234 OpusDecoder *dec =
nullptr;
238 Vector<uint8_t> decbuf{0};
239 const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
240 bool release_on_end =
false;
242 bool isValidRate(
int rate) {
243 for (
auto &valid : valid_rates) {
244 if (valid == rate)
return true;
272 const char *
mime()
override {
return "audio/opus"; }
287 assert(frame.data() !=
nullptr);
290 if (err != OPUS_OK) {
291 LOGE(
"opus_encoder_create: %s for sample_rate: %d, channels:%d",
295 is_open = settings();
304 bool begin(OpusEncoderSettings settings) {
314 opus_encoder_destroy(enc);
319 size_t write(
const uint8_t *data,
size_t len)
override {
320 if (!is_open || p_print ==
nullptr)
return 0;
321 LOGD(
"OpusAudioEncoder::write: %d", (
int)len);
324 for (
int j = 0; j < len; j++) {
330 operator bool()
override {
return is_open; }
332 bool isOpen() {
return is_open; }
335 Print *p_print =
nullptr;
336 OpusEncoder *enc =
nullptr;
337 OpusEncoderSettings cfg;
338 bool is_open =
false;
339 Vector<uint8_t> frame{0};
342 void encodeByte(uint8_t data) {
344 frame[frame_pos++] = data;
347 if (frame_pos >= frame.size()) {
354 if (frame.size() > 0) {
357 OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
358 uint8_t packet[packet_len];
360 int frames = frame.size() / cfg.channels /
sizeof(int16_t);
361 LOGD(
"opus_encode - frame_size: %d", frames);
362 int len = opus_encode(enc, (opus_int16 *)frame.data(), frames, packet,
365 LOGE(
"opus_encode: %s", opus_strerror(len));
366 }
else if (len > 0) {
367 LOGD(
"opus-encode: %d", len);
368 int eff = p_print->write(packet, len);
370 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
378 switch (cfg.frame_sizes_ms_x2) {
379 case OPUS_FRAMESIZE_2_5_MS:
380 return sampling_rate / 400;
381 case OPUS_FRAMESIZE_5_MS:
382 return sampling_rate / 200;
383 case OPUS_FRAMESIZE_10_MS:
384 return sampling_rate / 100;
385 case OPUS_FRAMESIZE_20_MS:
386 return sampling_rate / 50;
387 case OPUS_FRAMESIZE_40_MS:
388 return sampling_rate / 25;
389 case OPUS_FRAMESIZE_60_MS:
390 return 3 * sampling_rate / 50;
391 case OPUS_FRAMESIZE_80_MS:
392 return 4 * sampling_rate / 50;
393 case OPUS_FRAMESIZE_100_MS:
394 return 5 * sampling_rate / 50;
395 case OPUS_FRAMESIZE_120_MS:
396 return 6 * sampling_rate / 50;
398 return sampling_rate / 100;
403 if (cfg.bitrate >= 0 &&
404 opus_encoder_ctl(enc, OPUS_SET_BITRATE(cfg.bitrate)) != OPUS_OK) {
405 LOGE(
"invalid bitrate: %d", cfg.bitrate);
408 if (cfg.force_channel >= 0 &&
409 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(cfg.force_channel)) !=
411 LOGE(
"invalid force_channel: %d", cfg.force_channel);
415 opus_encoder_ctl(enc, OPUS_SET_VBR(cfg.vbr)) != OPUS_OK) {
416 LOGE(
"invalid vbr: %d", cfg.vbr);
419 if (cfg.vbr_constraint >= 0 &&
420 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cfg.vbr_constraint)) !=
422 LOGE(
"invalid vbr_constraint: %d", cfg.vbr_constraint);
425 if (cfg.complexity >= 0 &&
426 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(cfg.complexity)) != OPUS_OK) {
427 LOGE(
"invalid complexity: %d", cfg.complexity);
430 if (cfg.max_bandwidth >= 0 &&
431 opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(cfg.max_bandwidth)) !=
433 LOGE(
"invalid max_bandwidth: %d", cfg.max_bandwidth);
436 if (cfg.signal >= 0 &&
437 opus_encoder_ctl(enc, OPUS_SET_SIGNAL(cfg.signal)) != OPUS_OK) {
438 LOGE(
"invalid signal: %d", cfg.signal);
441 if (cfg.inband_fec >= 0 &&
442 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(cfg.inband_fec)) != OPUS_OK) {
443 LOGE(
"invalid inband_fec: %d", cfg.inband_fec);
446 if (cfg.packet_loss_perc >= 0 &&
448 enc, OPUS_SET_PACKET_LOSS_PERC(cfg.packet_loss_perc)) != OPUS_OK) {
449 LOGE(
"invalid pkt_loss: %d", cfg.packet_loss_perc);
452 if (cfg.lsb_depth >= 0 &&
453 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(cfg.lsb_depth)) != OPUS_OK) {
454 LOGE(
"invalid lsb_depth: %d", cfg.lsb_depth);
457 if (cfg.prediction_disabled >= 0 &&
458 opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(
459 cfg.prediction_disabled)) != OPUS_OK) {
460 LOGE(
"invalid pred_disabled: %d", cfg.prediction_disabled);
463 if (cfg.use_dtx >= 0 &&
464 opus_encoder_ctl(enc, OPUS_SET_DTX(cfg.use_dtx)) != OPUS_OK) {
465 LOGE(
"invalid use_dtx: %d", cfg.use_dtx);
468 if (cfg.frame_sizes_ms_x2 > 0 &&
469 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(
470 cfg.frame_sizes_ms_x2)) != OPUS_OK) {
471 LOGE(
"invalid frame_sizes_ms_x2: %d", cfg.frame_sizes_ms_x2);