Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Forcing L/R stereo

Tags:

webrtc

opus

I'm trying to produce low bitrate opus files with L/R stereo. What decides if opusenc will use L/R stereo instead of joint stereo? Is there are flag I can pass? Is it related to bitrate?

opusenc input.wav output.opus //produces L/R stereo
opusenc input.wav output.opus --bitrate 8 //produces joint stereo
like image 560
Colin Pickard Avatar asked Oct 20 '22 10:10

Colin Pickard


2 Answers

It looks like it is determined here:

    if (st->force_channels!=OPUS_AUTO && st->channels == 2)
    {
        st->stream_channels = st->force_channels;
    } else {
#ifdef FUZZING
       /* Random mono/stereo decision */
       if (st->channels == 2 && (rand()&0x1F)==0)
          st->stream_channels = 3-st->stream_channels;
#else
       /* Rate-dependent mono-stereo decision */
       if (st->channels == 2)
       {
          opus_int32 stereo_threshold;
          stereo_threshold = stereo_music_threshold + ((voice_est*voice_est*(stereo_voice_threshold-stereo_music_threshold))>>14);
          if (st->stream_channels == 2)
             stereo_threshold -= 4000;
          else
             stereo_threshold += 4000;
          st->stream_channels = (equiv_rate > stereo_threshold) ? 2 : 1;
       } else {
          st->stream_channels = st->channels;
       }
#endif
    }

Just breifly reading through the opusenc source code, it looks like setting force_channels to 2 on the struct OpusEncoder will make it work. However, looking through the opusenc.c source code, no where is that field set. You could easily modify the source however to always force channels to be two. For the future, it looks like opus calls it "dual stereo" rather than "L/R stereo".

like image 75
Carl Mastrangelo Avatar answered Oct 27 '22 11:10

Carl Mastrangelo


Opus by default attempts to make the best decision possible based on the current bitrate. The decision is made on the following table (20 ms frame size):

  • 8-12 kbit/s for NB speech,
  • 16-20 kbit/s for WB speech,
  • 28-40 kbit/s for FB speech,
  • 48-64 kbit/s for FB mono music, and
  • 64-128 kbit/s for FB stereo music.

This is because opus assume that, if the bitrate is too low, it cannot encode stereo with sufficient quality.

Actually the documentation says that it is possible to change the number of channels BUT it doesn't explain how. I'll take a look later anyway on how to do this.

You can find these informations on rfc6716

like image 34
Antonio E. Avatar answered Oct 27 '22 10:10

Antonio E.