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 {
180 opus_decoder_destroy(dec);
196 size_t write(
const uint8_t *data,
size_t len)
override {
197 if (!active || p_print ==
nullptr)
return 0;
199 LOGD(
"OpusAudioDecoder::write: %d", (
int)len);
200 int in_band_forward_error_correction = 0;
201 int frame_count = cfg.max_buffer_size / cfg.
channels /
sizeof(opus_int16);
202 int out_samples = opus_decode(
203 dec, (uint8_t *)data, len, (opus_int16 *)outbuf.data(),
204 frame_count, in_band_forward_error_correction);
205 if (out_samples < 0) {
206 LOGW(
"opus-decode: %s", opus_strerror(out_samples));
207 }
else if (out_samples > 0) {
209 int out_bytes = out_samples * cfg.
channels *
sizeof(int16_t);
210 LOGD(
"opus-decode: %d", out_bytes);
211 int open = out_bytes;
214 int to_write = std::min(open, cfg.max_buffer_write_size);
215 int written = p_print->write(outbuf.data()+processed, to_write);
217 processed += written;
223 operator bool()
override {
return active; }
226 Print *p_print =
nullptr;
230 Vector<uint8_t> outbuf{0};
231 Vector<uint8_t> decbuf{0};
232 const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
234 bool isValidRate(
int rate){
235 for (
auto &valid : valid_rates){
236 if (valid==rate)
return true;
261 const char *
mime()
override {
return "audio/opus"; }
276 assert(frame.data() !=
nullptr);
277 enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application, &err);
278 if (err != OPUS_OK) {
279 LOGE(
"opus_encoder_create: %s for sample_rate: %d, channels:%d",
280 opus_strerror(err), cfg.sample_rate, cfg.channels);
283 is_open = settings();
292 bool begin(OpusEncoderSettings settings) {
302 opus_encoder_destroy(enc);
307 size_t write(
const uint8_t *data,
size_t len)
override {
308 if (!is_open || p_print ==
nullptr)
return 0;
309 LOGD(
"OpusAudioEncoder::write: %d", (
int)len);
312 for (
int j = 0; j < len; j++) {
318 operator bool()
override {
return is_open; }
320 bool isOpen() {
return is_open; }
323 Print *p_print =
nullptr;
324 OpusEncoder *enc =
nullptr;
325 OpusEncoderSettings cfg;
326 bool is_open =
false;
327 Vector<uint8_t> frame{0};
330 void encodeByte(uint8_t data) {
332 frame[frame_pos++] = data;
335 if (frame_pos >= frame.size()) {
342 if (frame.size() > 0) {
344 int packet_len = OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
345 uint8_t packet[packet_len];
347 int frames = frame.size() / cfg.channels /
sizeof(int16_t);
348 LOGD(
"opus_encode - frame_size: %d", frames);
349 int len = opus_encode(enc, (opus_int16 *)frame.data(), frames,
352 LOGE(
"opus_encode: %s", opus_strerror(len));
353 }
else if (len > 0) {
354 LOGD(
"opus-encode: %d", len);
355 int eff = p_print->write(packet, len);
357 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
365 switch (cfg.frame_sizes_ms_x2) {
366 case OPUS_FRAMESIZE_2_5_MS:
367 return sampling_rate / 400;
368 case OPUS_FRAMESIZE_5_MS:
369 return sampling_rate / 200;
370 case OPUS_FRAMESIZE_10_MS:
371 return sampling_rate / 100;
372 case OPUS_FRAMESIZE_20_MS:
373 return sampling_rate / 50;
374 case OPUS_FRAMESIZE_40_MS:
375 return sampling_rate / 25;
376 case OPUS_FRAMESIZE_60_MS:
377 return 3 * sampling_rate / 50;
378 case OPUS_FRAMESIZE_80_MS:
379 return 4 * sampling_rate / 50;
380 case OPUS_FRAMESIZE_100_MS:
381 return 5 * sampling_rate / 50;
382 case OPUS_FRAMESIZE_120_MS:
383 return 6 * sampling_rate / 50;
385 return sampling_rate / 100;
390 if (cfg.bitrate >= 0 &&
391 opus_encoder_ctl(enc, OPUS_SET_BITRATE(cfg.bitrate)) != OPUS_OK) {
392 LOGE(
"invalid bitrate: %d", cfg.bitrate);
395 if (cfg.force_channel >= 0 &&
396 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(cfg.force_channel)) !=
398 LOGE(
"invalid force_channel: %d", cfg.force_channel);
402 opus_encoder_ctl(enc, OPUS_SET_VBR(cfg.vbr)) != OPUS_OK) {
403 LOGE(
"invalid vbr: %d", cfg.vbr);
406 if (cfg.vbr_constraint >= 0 &&
407 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cfg.vbr_constraint)) !=
409 LOGE(
"invalid vbr_constraint: %d", cfg.vbr_constraint);
412 if (cfg.complexity >= 0 &&
413 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(cfg.complexity)) != OPUS_OK) {
414 LOGE(
"invalid complexity: %d", cfg.complexity);
417 if (cfg.max_bandwidth >= 0 &&
418 opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(cfg.max_bandwidth)) !=
420 LOGE(
"invalid max_bandwidth: %d", cfg.max_bandwidth);
423 if (cfg.singal >= 0 &&
424 opus_encoder_ctl(enc, OPUS_SET_SIGNAL(cfg.singal)) != OPUS_OK) {
425 LOGE(
"invalid singal: %d", cfg.singal);
428 if (cfg.inband_fec >= 0 &&
429 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(cfg.inband_fec)) != OPUS_OK) {
430 LOGE(
"invalid inband_fec: %d", cfg.inband_fec);
433 if (cfg.packet_loss_perc >= 0 &&
435 enc, OPUS_SET_PACKET_LOSS_PERC(cfg.packet_loss_perc)) != OPUS_OK) {
436 LOGE(
"invalid pkt_loss: %d", cfg.packet_loss_perc);
439 if (cfg.lsb_depth >= 0 &&
440 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(cfg.lsb_depth)) != OPUS_OK) {
441 LOGE(
"invalid lsb_depth: %d", cfg.lsb_depth);
444 if (cfg.prediction_disabled >= 0 &&
445 opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(
446 cfg.prediction_disabled)) != OPUS_OK) {
447 LOGE(
"invalid pred_disabled: %d", cfg.prediction_disabled);
450 if (cfg.use_dtx >= 0 &&
451 opus_encoder_ctl(enc, OPUS_SET_DTX(cfg.use_dtx)) != OPUS_OK) {
452 LOGE(
"invalid use_dtx: %d", cfg.use_dtx);
455 if (cfg.frame_sizes_ms_x2 > 0 &&
456 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(
457 cfg.frame_sizes_ms_x2)) != OPUS_OK) {
458 LOGE(
"invalid frame_sizes_ms_x2: %d", cfg.frame_sizes_ms_x2);