mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-01 10:21:46 +02:00
opus add pseudo header and configurable options
This commit is contained in:
parent
3fea89a60b
commit
6cd3b7df67
4 changed files with 155 additions and 21 deletions
|
@ -18,16 +18,21 @@
|
||||||
|
|
||||||
#include "opus_decoder.hpp"
|
#include "opus_decoder.hpp"
|
||||||
#include "common/aixlog.hpp"
|
#include "common/aixlog.hpp"
|
||||||
|
#include "common/snap_exception.hpp"
|
||||||
|
#include "common/str_compat.hpp"
|
||||||
|
|
||||||
|
#define ID_OPUS 0x4F505553
|
||||||
|
|
||||||
|
/// int: Number of samples per channel in the input signal.
|
||||||
|
/// This must be an Opus frame size for the encoder's sampling rate. For example, at 48 kHz the
|
||||||
|
/// permitted values are 120, 240, 480, 960, 1920, and 2880.
|
||||||
|
/// Passing in a duration of less than 10 ms (480 samples at 48 kHz) will prevent the encoder from using the LPC or hybrid modes.
|
||||||
|
static constexpr int const_max_frame_size = 2880;
|
||||||
|
|
||||||
|
|
||||||
OpusDecoder::OpusDecoder() : Decoder()
|
OpusDecoder::OpusDecoder() : Decoder(), dec_(nullptr)
|
||||||
{
|
{
|
||||||
int error;
|
pcm_.resize(120);
|
||||||
dec_ = opus_decoder_create(48000, 2, &error); // fixme
|
|
||||||
if (error != 0)
|
|
||||||
{
|
|
||||||
LOG(ERROR) << "Failed to initialize opus decoder: " << error << '\n' << " Rate: " << 48000 << '\n' << " Channels: " << 2 << '\n';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -40,32 +45,66 @@ OpusDecoder::~OpusDecoder()
|
||||||
|
|
||||||
bool OpusDecoder::decode(msg::PcmChunk* chunk)
|
bool OpusDecoder::decode(msg::PcmChunk* chunk)
|
||||||
{
|
{
|
||||||
size_t samples = 480;
|
int frame_size = 0;
|
||||||
// reserve space for decoded audio
|
|
||||||
if (pcm_.size() < samples * 2)
|
while ((frame_size = opus_decode(dec_, (unsigned char*)chunk->payload, chunk->payloadSize, pcm_.data(), pcm_.size() / sample_format_.channels, 0)) ==
|
||||||
pcm_.resize(samples * 2);
|
OPUS_BUFFER_TOO_SMALL)
|
||||||
|
{
|
||||||
|
if (pcm_.size() < const_max_frame_size * sample_format_.channels)
|
||||||
|
{
|
||||||
|
pcm_.resize(pcm_.size() * 2);
|
||||||
|
LOG(INFO) << "OPUS encoding buffer too small, resizing to " << pcm_.size() / sample_format_.channels << " samples per channel\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
// decode
|
|
||||||
int frame_size = opus_decode(dec_, (unsigned char*)chunk->payload, chunk->payloadSize, pcm_.data(), samples, 0);
|
|
||||||
if (frame_size < 0)
|
if (frame_size < 0)
|
||||||
{
|
{
|
||||||
LOG(ERROR) << "Failed to decode chunk: " << frame_size << '\n' << " IN size: " << chunk->payloadSize << '\n' << " OUT size: " << samples * 4 << '\n';
|
LOG(ERROR) << "Failed to decode chunk: " << frame_size << '\n' << " IN size: " << chunk->payloadSize << '\n' << " OUT size: " << pcm_.size() << '\n';
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOG(DEBUG) << "Decoded chunk: size " << chunk->payloadSize << " bytes, decoded " << frame_size << " bytes" << '\n';
|
LOG(DEBUG) << "Decoded chunk: size " << chunk->payloadSize << " bytes, decoded " << frame_size << " samples" << '\n';
|
||||||
|
|
||||||
// copy encoded data to chunk
|
// copy encoded data to chunk
|
||||||
chunk->payloadSize = samples * 4;
|
chunk->payloadSize = frame_size * sample_format_.channels * sizeof(opus_int16);
|
||||||
chunk->payload = (char*)realloc(chunk->payload, chunk->payloadSize);
|
chunk->payload = (char*)realloc(chunk->payload, chunk->payloadSize);
|
||||||
memcpy(chunk->payload, (char*)pcm_.data(), samples * 4);
|
memcpy(chunk->payload, (char*)pcm_.data(), chunk->payloadSize);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
SampleFormat OpusDecoder::setHeader(msg::CodecHeader* /*chunk*/)
|
SampleFormat OpusDecoder::setHeader(msg::CodecHeader* chunk)
|
||||||
{
|
{
|
||||||
return {48000, 16, 2};
|
// decode the opus pseudo header
|
||||||
|
if (chunk->payloadSize < 12)
|
||||||
|
throw SnapException("OPUS header too small");
|
||||||
|
|
||||||
|
// decode the "opus id" magic number, this is our constant part that must match
|
||||||
|
uint32_t id_opus;
|
||||||
|
memcpy(&id_opus, chunk->payload, sizeof(id_opus));
|
||||||
|
if (SWAP_32(id_opus) != ID_OPUS)
|
||||||
|
throw SnapException("Not an Opus pseudo header");
|
||||||
|
|
||||||
|
// decode the sampleformat
|
||||||
|
uint32_t rate;
|
||||||
|
memcpy(&rate, chunk->payload + 4, sizeof(id_opus));
|
||||||
|
uint16_t bits;
|
||||||
|
memcpy(&bits, chunk->payload + 8, sizeof(bits));
|
||||||
|
uint16_t channels;
|
||||||
|
memcpy(&channels, chunk->payload + 10, sizeof(channels));
|
||||||
|
|
||||||
|
sample_format_.setFormat(SWAP_32(rate), SWAP_16(bits), SWAP_16(channels));
|
||||||
|
LOG(INFO) << "Opus sampleformat: " << sample_format_.getFormat() << "\n";
|
||||||
|
|
||||||
|
// create the decoder
|
||||||
|
int error;
|
||||||
|
dec_ = opus_decoder_create(sample_format_.rate, sample_format_.channels, &error);
|
||||||
|
if (error != 0)
|
||||||
|
throw SnapException("Failed to initialize Opus decoder: " + cpt::to_string(error));
|
||||||
|
|
||||||
|
return sample_format_;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,4 +33,5 @@ public:
|
||||||
private:
|
private:
|
||||||
OpusDecoder* dec_;
|
OpusDecoder* dec_;
|
||||||
std::vector<opus_int16> pcm_;
|
std::vector<opus_int16> pcm_;
|
||||||
|
SampleFormat sample_format_;
|
||||||
};
|
};
|
||||||
|
|
|
@ -20,11 +20,27 @@
|
||||||
#include "common/aixlog.hpp"
|
#include "common/aixlog.hpp"
|
||||||
#include "common/snap_exception.hpp"
|
#include "common/snap_exception.hpp"
|
||||||
#include "common/str_compat.hpp"
|
#include "common/str_compat.hpp"
|
||||||
|
#include "common/utils/string_utils.hpp"
|
||||||
|
|
||||||
|
#define ID_OPUS 0x4F505553
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
template <typename T>
|
||||||
|
void assign(void* pointer, T val)
|
||||||
|
{
|
||||||
|
T* p = (T*)pointer;
|
||||||
|
*p = val;
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
// TODO:
|
||||||
|
// - handle variable chunk durations (now it's fixed to 10ms)
|
||||||
|
|
||||||
// const msg::SampleFormat& format);
|
// const msg::SampleFormat& format);
|
||||||
OpusEncoder::OpusEncoder(const std::string& codecOptions) : Encoder(codecOptions), enc_(nullptr)
|
OpusEncoder::OpusEncoder(const std::string& codecOptions) : Encoder(codecOptions), enc_(nullptr)
|
||||||
{
|
{
|
||||||
|
headerChunk_.reset(new msg::CodecHeader("opus"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -35,6 +51,17 @@ OpusEncoder::~OpusEncoder()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string OpusEncoder::getAvailableOptions() const
|
||||||
|
{
|
||||||
|
return "BR:[6 - 512|MAX|AUTO],COMPLEXITY:[1-10]";
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::string OpusEncoder::getDefaultOptions() const
|
||||||
|
{
|
||||||
|
return "BR:192,COMPLEXITY:10";
|
||||||
|
}
|
||||||
|
|
||||||
std::string OpusEncoder::name() const
|
std::string OpusEncoder::name() const
|
||||||
{
|
{
|
||||||
return "opus";
|
return "opus";
|
||||||
|
@ -43,13 +70,78 @@ std::string OpusEncoder::name() const
|
||||||
|
|
||||||
void OpusEncoder::initEncoder()
|
void OpusEncoder::initEncoder()
|
||||||
{
|
{
|
||||||
|
opus_int32 bitrate = 192000;
|
||||||
|
opus_int32 complexity = 10;
|
||||||
|
auto options = utils::string::split(codecOptions_, ',');
|
||||||
|
for (const auto& option : options)
|
||||||
|
{
|
||||||
|
auto kv = utils::string::split(option, ':');
|
||||||
|
if (kv.size() == 2)
|
||||||
|
{
|
||||||
|
if (kv.front() == "BR")
|
||||||
|
{
|
||||||
|
if (kv.back() == "MAX")
|
||||||
|
bitrate = OPUS_BITRATE_MAX;
|
||||||
|
else if (kv.back() == "AUTO")
|
||||||
|
bitrate = OPUS_AUTO;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
bitrate = cpt::stoi(kv.back());
|
||||||
|
if ((bitrate < 6) || (bitrate > 512))
|
||||||
|
throw SnapException("Opus bitrate must be between 6 and 512");
|
||||||
|
bitrate *= 1000;
|
||||||
|
}
|
||||||
|
catch (const std::invalid_argument&)
|
||||||
|
{
|
||||||
|
throw SnapException("Opus error parsing bitrate (must be between 6 and 512): " + kv.back());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if (kv.front() == "COMPLEXITY")
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
complexity = cpt::stoi(kv.back());
|
||||||
|
if ((complexity < 1) || (complexity > 10))
|
||||||
|
throw SnapException("Opus complexity must be between 1 and 10");
|
||||||
|
}
|
||||||
|
catch (const std::invalid_argument&)
|
||||||
|
{
|
||||||
|
throw SnapException("Opus error parsing complexity (must be between 1 and 10): " + kv.back());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw SnapException("Opus unknown option: " + kv.front());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
throw SnapException("Opus error parsing options: " + codecOptions_);
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG(INFO) << "Opus bitrate: " << bitrate << " bps, complexity: " << complexity << "\n";
|
||||||
|
|
||||||
int error;
|
int error;
|
||||||
enc_ = opus_encoder_create(sampleFormat_.rate, sampleFormat_.channels, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &error);
|
enc_ = opus_encoder_create(sampleFormat_.rate, sampleFormat_.channels, OPUS_APPLICATION_RESTRICTED_LOWDELAY, &error);
|
||||||
if (error != 0)
|
if (error != 0)
|
||||||
{
|
{
|
||||||
throw SnapException("Failed to initialize opus encoder: " + cpt::to_string(error));
|
throw SnapException("Failed to initialize Opus encoder: " + cpt::to_string(error));
|
||||||
}
|
}
|
||||||
headerChunk_.reset(new msg::CodecHeader("opus"));
|
|
||||||
|
opus_encoder_ctl(enc_, OPUS_SET_BITRATE(bitrate)); // max 512000, OPUS_BITRATE_MAX, OPUS_AUTO
|
||||||
|
opus_encoder_ctl(enc_, OPUS_SET_COMPLEXITY(complexity)); // max 10
|
||||||
|
|
||||||
|
if ((sampleFormat_.rate != 48000) || (sampleFormat_.bits != 16) || (sampleFormat_.channels != 2))
|
||||||
|
throw SnapException("Opus sampleformat must be 48000:16:2");
|
||||||
|
|
||||||
|
// create some opus pseudo header to let the decoder know about the sample format
|
||||||
|
headerChunk_->payloadSize = 12;
|
||||||
|
headerChunk_->payload = (char*)malloc(headerChunk_->payloadSize);
|
||||||
|
char* payload = headerChunk_->payload;
|
||||||
|
assign(payload, SWAP_32(ID_OPUS));
|
||||||
|
assign(payload + 4, SWAP_32(sampleFormat_.rate));
|
||||||
|
assign(payload + 8, SWAP_16(sampleFormat_.bits));
|
||||||
|
assign(payload + 10, SWAP_16(sampleFormat_.channels));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -29,6 +29,8 @@ public:
|
||||||
~OpusEncoder() override;
|
~OpusEncoder() override;
|
||||||
|
|
||||||
void encode(const msg::PcmChunk* chunk) override;
|
void encode(const msg::PcmChunk* chunk) override;
|
||||||
|
std::string getAvailableOptions() const override;
|
||||||
|
std::string getDefaultOptions() const override;
|
||||||
std::string name() const override;
|
std::string name() const override;
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue