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
32 int max_buffer_size = OPUS_DEC_MAX_BUFFER_SIZE;
33 int max_buffer_write_size = 512;
71 max_buffer_size = OPUS_ENC_MAX_BUFFER_SIZE;
141 bool begin(OpusSettings settings) {
145 notifyAudioChange(cfg);
149 bool begin()
override {
152 LOGE(
"Sample rate not supported: %d", cfg.
sample_rate);
155 outbuf.resize(cfg.max_buffer_size);
156 assert(outbuf.data() !=
nullptr);
161 size_t size = opus_decoder_get_size(cfg.
channels);
163 assert(decbuf.data() !=
nullptr);
164 dec = (OpusDecoder*)decbuf.data();
168 if (err != OPUS_OK) {
169 LOGE(
"opus_decoder_create: %s for sample_rate: %d, channels:%d",
177 void end()
override {
193 size_t write(
const uint8_t *data,
size_t len)
override {
194 if (!active || p_print ==
nullptr)
return 0;
196 LOGD(
"OpusAudioDecoder::write: %d", (
int)len);
197 int in_band_forward_error_correction = 0;
198 int frame_count = cfg.max_buffer_size / cfg.
channels /
sizeof(opus_int16);
199 int out_samples = opus_decode(
200 dec, (uint8_t *)data, len, (opus_int16 *)outbuf.data(),
201 frame_count, in_band_forward_error_correction);
202 if (out_samples < 0) {
203 LOGW(
"opus-decode: %s", opus_strerror(out_samples));
204 }
else if (out_samples > 0) {
206 int out_bytes = out_samples * cfg.
channels *
sizeof(int16_t);
207 LOGD(
"opus-decode: %d", out_bytes);
208 int open = out_bytes;
211 int to_write = std::min(open, cfg.max_buffer_write_size);
212 int written = p_print->write(outbuf.data()+processed, to_write);
214 processed += written;
220 operator bool()
override {
return active; }
223 Print *p_print =
nullptr;
224 OpusDecoder *dec =
nullptr;
227 Vector<uint8_t> outbuf{0};
228 Vector<uint8_t> decbuf{0};
229 const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
231 bool isValidRate(
int rate){
232 for (
auto &valid : valid_rates){
233 if (valid==rate)
return true;
258 const char *
mime()
override {
return "audio/opus"; }
273 assert(frame.data() !=
nullptr);
274 enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application, &err);
275 if (err != OPUS_OK) {
276 LOGE(
"opus_encoder_create: %s for sample_rate: %d, channels:%d",
277 opus_strerror(err), cfg.sample_rate, cfg.channels);
280 is_open = settings();
289 bool begin(OpusEncoderSettings settings) {
299 opus_encoder_destroy(enc);
304 size_t write(
const uint8_t *data,
size_t len)
override {
305 if (!is_open || p_print ==
nullptr)
return 0;
306 LOGD(
"OpusAudioEncoder::write: %d", (
int)len);
309 for (
int j = 0; j < len; j++) {
304 size_t write(
const uint8_t *data,
size_t len)
override {
…}
315 operator bool()
override {
return is_open; }
317 bool isOpen() {
return is_open; }
320 Print *p_print =
nullptr;
321 OpusEncoder *enc =
nullptr;
322 OpusEncoderSettings cfg;
323 bool is_open =
false;
324 Vector<uint8_t> frame{0};
327 void encodeByte(uint8_t data) {
329 frame[frame_pos++] = data;
332 if (frame_pos >= frame.size()) {
339 if (frame.size() > 0) {
341 int packet_len = OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
342 uint8_t packet[packet_len];
344 int frames = frame.size() / cfg.channels /
sizeof(int16_t);
345 LOGD(
"opus_encode - frame_size: %d", frames);
346 int len = opus_encode(enc, (opus_int16 *)frame.data(), frames,
349 LOGE(
"opus_encode: %s", opus_strerror(len));
350 }
else if (len > 0) {
351 LOGD(
"opus-encode: %d", len);
352 int eff = p_print->write(packet, len);
354 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
362 switch (cfg.frame_sizes_ms_x2) {
363 case OPUS_FRAMESIZE_2_5_MS:
364 return sampling_rate / 400;
365 case OPUS_FRAMESIZE_5_MS:
366 return sampling_rate / 200;
367 case OPUS_FRAMESIZE_10_MS:
368 return sampling_rate / 100;
369 case OPUS_FRAMESIZE_20_MS:
370 return sampling_rate / 50;
371 case OPUS_FRAMESIZE_40_MS:
372 return sampling_rate / 25;
373 case OPUS_FRAMESIZE_60_MS:
374 return 3 * sampling_rate / 50;
375 case OPUS_FRAMESIZE_80_MS:
376 return 4 * sampling_rate / 50;
377 case OPUS_FRAMESIZE_100_MS:
378 return 5 * sampling_rate / 50;
379 case OPUS_FRAMESIZE_120_MS:
380 return 6 * sampling_rate / 50;
382 return sampling_rate / 100;
387 if (cfg.bitrate >= 0 &&
388 opus_encoder_ctl(enc, OPUS_SET_BITRATE(cfg.bitrate)) != OPUS_OK) {
389 LOGE(
"invalid bitrate: %d", cfg.bitrate);
392 if (cfg.force_channel >= 0 &&
393 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(cfg.force_channel)) !=
395 LOGE(
"invalid force_channel: %d", cfg.force_channel);
399 opus_encoder_ctl(enc, OPUS_SET_VBR(cfg.vbr)) != OPUS_OK) {
400 LOGE(
"invalid vbr: %d", cfg.vbr);
403 if (cfg.vbr_constraint >= 0 &&
404 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cfg.vbr_constraint)) !=
406 LOGE(
"invalid vbr_constraint: %d", cfg.vbr_constraint);
409 if (cfg.complexity >= 0 &&
410 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(cfg.complexity)) != OPUS_OK) {
411 LOGE(
"invalid complexity: %d", cfg.complexity);
414 if (cfg.max_bandwidth >= 0 &&
415 opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(cfg.max_bandwidth)) !=
417 LOGE(
"invalid max_bandwidth: %d", cfg.max_bandwidth);
420 if (cfg.signal >= 0 &&
421 opus_encoder_ctl(enc, OPUS_SET_SIGNAL(cfg.signal)) != OPUS_OK) {
422 LOGE(
"invalid signal: %d", cfg.signal);
425 if (cfg.inband_fec >= 0 &&
426 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(cfg.inband_fec)) != OPUS_OK) {
427 LOGE(
"invalid inband_fec: %d", cfg.inband_fec);
430 if (cfg.packet_loss_perc >= 0 &&
432 enc, OPUS_SET_PACKET_LOSS_PERC(cfg.packet_loss_perc)) != OPUS_OK) {
433 LOGE(
"invalid pkt_loss: %d", cfg.packet_loss_perc);
436 if (cfg.lsb_depth >= 0 &&
437 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(cfg.lsb_depth)) != OPUS_OK) {
438 LOGE(
"invalid lsb_depth: %d", cfg.lsb_depth);
441 if (cfg.prediction_disabled >= 0 &&
442 opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(
443 cfg.prediction_disabled)) != OPUS_OK) {
444 LOGE(
"invalid pred_disabled: %d", cfg.prediction_disabled);
447 if (cfg.use_dtx >= 0 &&
448 opus_encoder_ctl(enc, OPUS_SET_DTX(cfg.use_dtx)) != OPUS_OK) {
449 LOGE(
"invalid use_dtx: %d", cfg.use_dtx);
452 if (cfg.frame_sizes_ms_x2 > 0 &&
453 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(
454 cfg.frame_sizes_ms_x2)) != OPUS_OK) {
455 LOGE(
"invalid frame_sizes_ms_x2: %d", cfg.frame_sizes_ms_x2);