Skip to content

Opus 1.4 Corrupted audio while encode of 128 channels #397

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
NickolayGerasimenko opened this issue Apr 11, 2025 · 0 comments
Open

Opus 1.4 Corrupted audio while encode of 128 channels #397

NickolayGerasimenko opened this issue Apr 11, 2025 · 0 comments

Comments

@NickolayGerasimenko
Copy link

NickolayGerasimenko commented Apr 11, 2025

Environment

Opus 1.4 (commit: 82ac57d)
OS: Win/MacOS

Repro steps

I have very simple encoder as below,
I try to encode 128 channels where I have sine wave on every channel,
I set total bitrate as 64kbps * 128 = 8192kbps

So I instantiate Encoder as follows:

Encoder encoder(8192, 48000, 128);

Actual result:

Audio corrupted on last channels, up to total loss of audio in last channel.

Issue can be reproduced on any number of channels depending on set total bitrate.

Expected result:

Audio should be of good quality in every channel if set total bitrate as 64kbps * channels

Results on different bitrates:

total bitrate 8192kbps - no audio on 128th channel at all:

Image

total bitrate 16384kbps - it's 128kbps per channel, still 128th channel is corrupted:

Image

Workaround:

Only after setting total bitrate to 20480kbps audio looks fine.

Image
Encoder::Encoder(int bitrateKbps, int sampleRate, int numChannels)
{
    int error = 0;
    msEncoder_ = opus_multistream_encoder_create(sampleRate, numChannels, numChannels, 0, kChannelMapping.data(), OPUS_APPLICATION_AUDIO, &error);

    if (error != OPUS_OK)
        throw std::logic_error(std::string("Failed to create opus mulistream encoder, error: ") + opus_strerror(error));

    opus_multistream_encoder_ctl(msEncoder_, OPUS_SET_BITRATE(bitrateKbps * 1000));
    opus_multistream_encoder_ctl(msEncoder_, OPUS_SET_VBR(0));
    opus_multistream_encoder_ctl(msEncoder_, OPUS_SET_PACKET_LOSS_PERC(5));
    opus_multistream_encoder_ctl(msEncoder_, OPUS_SET_COMPLEXITY(5));
    opus_multistream_encoder_ctl(msEncoder_, OPUS_SET_BANDWIDTH(OPUS_AUTO));
}

Encoder::~Encoder()
{
    opus_multistream_encoder_destroy(msEncoder_);
}

bool Encoder::encode(const float* inBuf, int numInputSamples, unsigned char *outBuf, int outBufCapacity, int& encBytes)
{
    encBytes = opus_multistream_encode_float(msEncoder_, inBuf, numInputSamples, outBuf, outBufCapacity);

    if (encBytes < 0)
    {
        return false;
    }

    return true;
}
@NickolayGerasimenko NickolayGerasimenko changed the title Corrupted audio while encode of 128 channels Opus 1.4 Corrupted audio while encode of 128 channels Apr 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant