opus-tools icon indicating copy to clipboard operation
opus-tools copied to clipboard

Force channel decoupling

Open Worldexe opened this issue 9 years ago • 14 comments

I am using telephony system that can record calls into 8kHz stereo WAV files; it uses left channel for one participant and right channel for another. To be able to process files later, I need strong channel separation (I mean, they should be encoded naturally as separate channels).

Opus seems to be perfect for my application; but still, there is no channel-independent encoding option in opusenc tool. I am quite new in audio and stuff; but have I checked Opus RFC and Opus codebase - it seems it actually supports channel separation via 'coupling' concept. I think, it would be enough to add some option to set header.channel_mapping=255 around line 695 in opusenc.c. I checked if that would work - it worked like intended.

I can make pull-request here, if you think its acceptable to add option like 'no-coupling' that will explicitly set header.channel_mapping=255 somewhere after that line.

Worldexe avatar Apr 11 '16 20:04 Worldexe

Maybe a --discrete switch would make sense for this? Note that setting the channel mapping to 255 we prevent audio playback in most software; it's intended for use by DAW software which is used to handling unmixed audio.

I would expect a --no-coupling option to still use stereo, but prevent any coupling between them, so normal playback would work. Looks like this could call opus_multistream_encoder_create() instead of opus_multistream_surround_encoder_create() for the necessary control.

rillian avatar Apr 11 '16 20:04 rillian

Well, we can choose --discrete flag, but imo --no-coupling a bit more descriptive one.

Hmm, should I call opus_multistream_encoder_create with channels=2,streams=1,coupled=0? This seems wrong as it will lead to validate_layout() error.

Calling that with channels=2,streams=2,coupled=0 seems the same as calling opus_multistream_surround_encoder_create() with header.channel_mapping=255. I also do not see any difference in mediainfo output or player (AIMP) behaviour. Am I wrong?

Sorry, I am not familiar with Opus codebase, so maybe my questions are a bit stupid =)

Worldexe avatar Apr 12 '16 15:04 Worldexe

You're right. I was thinking opus_multistream_surround_encoder_create() would propagate the mapping_family argument into the file header, but it only uses it to set up channel coupling and tweak some encoder parameters, then discards the value. It's opusenc.c that writes the actual value into the file header. So passing channels = 2, streams = 2, 'coupled = 0or creating a surround encoder with that andchannel_mapping = 255` should be the same.

What I was trying to get out is that there's a semantic difference between opusenc actually writing header.channel_mapping = 255 into the .opus file header (discrete channels) and writing header.channel_mapping = 2 (left/right stereo, which just happen to be uncoupled).

rillian avatar Apr 12 '16 17:04 rillian

According to https://wiki.xiph.org/OggOpus, there is no reason to set header's channel mapping to 2, as

The remaining channel mapping families (2...254) are reserved. A decoder encountering a reserved mapping byte should act as though the mapping byte is 255.

Well, we can set it to 2, but who knows how it will be used in future.

Worldexe avatar Apr 17 '16 14:04 Worldexe

Sorry, I meant header.channel_mapping = 1, which is different from 255, unlike 2.

rillian avatar Apr 19 '16 00:04 rillian

Sorry, I did not forgot about this, just didnt have enough time :(

Worldexe avatar Jun 04 '16 19:06 Worldexe

Sorry to resurrect an old issue, but the proposed flag would be perfect for my use case of encoding multiple independent channels from a DAW for playback in a custom player. I exported the multichannel mix as WAV but opusenc offers no control over the channel mapping via command line switches. I see the codec is designed to handle my scenario but the tools are not.

I'm frankly getting tired of audio tools presuming surround configurations based solely on the channel counts. Just because I'm exporting an 8 channel WAV does not mean it's a 7.1 surround mix. Sure it can be default as that's low friction for most users but at least allow for customization and control.

I plan to make a PR of my own to address the issue but it will take some time.

JamesDunne avatar Nov 01 '17 00:11 JamesDunne

Sorry for not doing this myself :( Here is what I use; adapted just now to current HEAD, hope it still works; Maybe, this will be useful for your PR.

From 9993b87e9c9635588796d3a2753c693c8d27e4f5 Mon Sep 17 00:00:00 2001
From: "World.exe" <>
Date: Wed, 1 Nov 2017 19:12:35 +0300
Subject: [PATCH] Adding force channel decoupling.

---
 src/opusenc.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/src/opusenc.c b/src/opusenc.c
index 129dcdb..2901298 100644
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -136,6 +136,7 @@ void usage(void)
   printf(" --framesize n      Set maximum frame size in milliseconds\n");
   printf("                      (2.5, 5, 10, 20, 40, 60, default: 20)\n");
   printf(" --expect-loss      Set expected packet loss in percent (default: 0)\n");
+  printf(" --no-coupling      Force disable channel coupling\n");
   printf(" --downmix-mono     Downmix to mono\n");
   printf(" --downmix-stereo   Downmix to stereo (if >2 channels)\n");
   printf(" --max-delay n      Set maximum container delay in milliseconds\n");
@@ -345,6 +346,8 @@ int main(int argc, char **argv)
   int                comment_padding=512;
   int                serialno;
   opus_int32         lookahead=0;
+  int                no_coupling=0;
+
 #ifdef WIN_UNICODE
    int argc_utf8;
    char **argv_utf8;
@@ -577,6 +580,8 @@ int main(int argc, char **argv)
           inopt.copy_pictures=0;
         } else if(strcmp(long_options[option_index].name,"discard-pictures")==0){
           inopt.copy_pictures=0;
+        } else if(strcmp(long_options[option_index].name,"no-coupling")==0){
+          no_coupling=1;
         }
         /*Commands whose arguments would leak file paths or just end up as metadata
            should have save_cmd=0; to prevent them from being saved in the
@@ -698,6 +703,10 @@ int main(int argc, char **argv)
   /*Initialize Opus encoder*/
   /*Frame sizes <10ms can only use the MDCT modes, so we switch on RESTRICTED_LOWDELAY
     to save the extra 4ms of codec lookahead when we'll be using only small frames.*/
+  if(no_coupling){ 
+    header.channel_mapping=255;
+  }
+
   st=opus_multistream_surround_encoder_create(coding_rate, chan, header.channel_mapping, &header.nb_streams, &header.nb_coupled,
      header.stream_map, frame_size<480/(48000/coding_rate)?OPUS_APPLICATION_RESTRICTED_LOWDELAY:OPUS_APPLICATION_AUDIO, &ret);
   if(ret!=OPUS_OK){
-- 
1.9.5.msysgit.1

Worldexe avatar Nov 01 '17 16:11 Worldexe

@Worldexe I've just completed something similar albeit more flexible on my fork.

--no-surround      Disable surround sound encoding
--coupled 1,4      Specify coupled input channels (e.g. 1/2, 4/5)

However, the encoder still remaps channels according to vorbis channel ordering which is not what I want. I want the input channels to be mapped 1:1 to the output channels.

I'm currently looking for a way to completely disable the vorbis channel remapping but I don't see any obvious avenue to do so in the code here. It'd be great if I could specify my own output channel mapping to vorbis so that the coupled channels found in the original input audio file are kept in their original places in the output file with the added benefit of being coupled together for stereo processing.

JamesDunne avatar Nov 01 '17 16:11 JamesDunne

Nevermind what I wrote last. I'm using Reaper as my DAW and it is decoding as surround and remapping the channels on decode. opusenc and opusdec with my --no-surround option correctly encode and decode the channels without reordering.

JamesDunne avatar Nov 01 '17 16:11 JamesDunne

Got a working PR to resolve this: https://github.com/xiph/opus-tools/pull/19

Tested it out locally on my 8-channel non-surround mix and it works!

JamesDunne avatar Nov 01 '17 19:11 JamesDunne

After 6 years.. sorry. What is the right way to encode 2 channels uncoupled using opus_multistream_encoder_create? Any changes in validate_layout?

MT00x avatar Feb 15 '23 22:02 MT00x

This https://github.com/xiph/opus-tools/pull/80 should also fix this issue

chris-hld avatar Nov 03 '23 11:11 chris-hld

@ePirat @mark4o Probably this issue can be closed now, for https://github.com/xiph/opus-tools/pull/80 was merged in master

vadimkantorov avatar Dec 06 '23 23:12 vadimkantorov