[opus] [PATCH] Add Functions to Create Ambisonic Multistream Encoder

Michael Graczyk mgraczyk at google.com
Fri May 6 23:46:05 UTC 2016


Here is the modified patch. I added a flag to configure.ac which is set to
0 to disable ambisonics, and 1 to enable it. Right now the implementation
simply creates a surround encoder with N uncoupled streams.


Thanks,
Michael Graczyk
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.xiph.org/pipermail/opus/attachments/20160506/5ba6a372/attachment.html>
-------------- next part --------------
From b6ef739067f5bfcdae61938ac0b34764e05e518e Mon Sep 17 00:00:00 2001
From: Michael Graczyk <michael at mgraczyk.com>
Date: Mon, 2 May 2016 21:42:18 -0700
Subject: [PATCH] Add experimental support for ambisonic encoding

The implementation currently uses the surround encoder with mapping family 255.
---
 configure.ac                   | 11 +++++++++++
 src/opus_multistream_encoder.c | 43 ++++++++++++++++++++++++++----------------
 2 files changed, 38 insertions(+), 16 deletions(-)

diff --git a/configure.ac b/configure.ac
index a67aa37..b6ca573 100644
--- a/configure.ac
+++ b/configure.ac
@@ -736,6 +736,16 @@ AS_IF([test "$enable_fuzzing" = "yes"], [
   AC_DEFINE([FUZZING], [1], [Fuzzing])
 ])
 
+AC_ARG_ENABLE([ambisonics],
+    [AS_HELP_STRING([--enable-ambisonics],[enable experimental ambisonic encoding and decoding support])],,
+    [enable_ambisonics=no])
+
+AS_IF([test "$enable_ambisonics" = "yes"], [
+  AC_DEFINE([ENABLE_EXPERIMENTAL_AMBISONICS], [1], [Ambisonics Support])
+], [
+  AC_DEFINE([ENABLE_EXPERIMENTAL_AMBISONICS], [0], [Ambisonics Support])
+])
+
 AC_ARG_ENABLE([doc],
     [AS_HELP_STRING([--disable-doc], [Do not build API documentation])],,
     [enable_doc=yes])
@@ -820,6 +830,7 @@ AC_MSG_NOTICE([
       Custom modes: .................. ${enable_custom_modes}
       Assertion checking: ............ ${enable_assertions}
       Fuzzing: ....................... ${enable_fuzzing}
+      Ambisonics support: .............${enable_ambisonics}
 
       API documentation: ............. ${enable_doc}
       Extra programs: ................ ${enable_extra_programs}
diff --git a/src/opus_multistream_encoder.c b/src/opus_multistream_encoder.c
index 9e85773..f0f344b 100644
--- a/src/opus_multistream_encoder.c
+++ b/src/opus_multistream_encoder.c
@@ -70,13 +70,19 @@ typedef void (*opus_copy_channel_in_func)(
   int frame_size
 );
 
+typedef enum {
+  ALLOCATION_MODE_NONE = 0,
+  ALLOCATION_MODE_SURROUND = 1,
+  ALLOCATION_MODE_AMBISONICS = 2,
+} AllocationMode;
+
 struct OpusMSEncoder {
    ChannelLayout layout;
    int arch;
    int lfe_stream;
    int application;
    int variable_duration;
-   int surround;
+   AllocationMode allocation_mode;
    opus_int32 bitrate_bps;
    float subframe_mem[3];
    /* Encoder states go here */
@@ -242,6 +248,7 @@ void surround_analysis(const CELTMode *celt_mode, const void *pcm, opus_val16 *b
    upsample = resampling_factor(rate);
    frame_size = len*upsample;
 
+   /* LM = log2(frame_size / 120) */
    for (LM=0;LM<celt_mode->maxLM;LM++)
       if (celt_mode->shortMdctSize<<LM==frame_size)
          break;
@@ -394,7 +401,8 @@ opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_
    {
       nb_streams=vorbis_mappings[channels-1].nb_streams;
       nb_coupled_streams=vorbis_mappings[channels-1].nb_coupled_streams;
-   } else if (mapping_family==255)
+   } else if (mapping_family==255
+              || (mapping_family==2 && ENABLE_EXPERIMENTAL_AMBISONICS))
    {
       nb_streams=channels;
       nb_coupled_streams=0;
@@ -408,7 +416,6 @@ opus_int32 opus_multistream_surround_encoder_get_size(int channels, int mapping_
    return size;
 }
 
-
 static int opus_multistream_encoder_init_impl(
       OpusMSEncoder *st,
       opus_int32 Fs,
@@ -417,7 +424,7 @@ static int opus_multistream_encoder_init_impl(
       int coupled_streams,
       const unsigned char *mapping,
       int application,
-      int surround
+      AllocationMode allocation_mode
 )
 {
    int coupled_size;
@@ -434,7 +441,7 @@ static int opus_multistream_encoder_init_impl(
    st->layout.nb_streams = streams;
    st->layout.nb_coupled_streams = coupled_streams;
    st->subframe_mem[0]=st->subframe_mem[1]=st->subframe_mem[2]=0;
-   if (!surround)
+   if (allocation_mode != ALLOCATION_MODE_SURROUND)
       st->lfe_stream = -1;
    st->bitrate_bps = OPUS_AUTO;
    st->application = application;
@@ -463,12 +470,12 @@ static int opus_multistream_encoder_init_impl(
       if(ret!=OPUS_OK)return ret;
       ptr += align(mono_size);
    }
-   if (surround)
+   if (allocation_mode == ALLOCATION_MODE_SURROUND)
    {
       OPUS_CLEAR(ms_get_preemph_mem(st), channels);
       OPUS_CLEAR(ms_get_window_mem(st), channels*120);
    }
-   st->surround = surround;
+   st->allocation_mode = allocation_mode;
    return OPUS_OK;
 }
 
@@ -482,7 +489,9 @@ int opus_multistream_encoder_init(
       int application
 )
 {
-   return opus_multistream_encoder_init_impl(st, Fs, channels, streams, coupled_streams, mapping, application, 0);
+   return opus_multistream_encoder_init_impl(st, Fs, channels, streams,
+                                             coupled_streams, mapping,
+                                             application, ALLOCATION_MODE_NONE);
 }
 
 int opus_multistream_surround_encoder_init(
@@ -523,7 +532,8 @@ int opus_multistream_surround_encoder_init(
          mapping[i] = vorbis_mappings[channels-1].mapping[i];
       if (channels>=6)
          st->lfe_stream = *streams-1;
-   } else if (mapping_family==255)
+   } else if (mapping_family==255
+              || (mapping_family==2 && ENABLE_EXPERIMENTAL_AMBISONICS))
    {
       int i;
       *streams=channels;
@@ -532,6 +542,7 @@ int opus_multistream_surround_encoder_init(
          mapping[i] = i;
    } else
       return OPUS_UNIMPLEMENTED;
+
    return opus_multistream_encoder_init_impl(st, Fs, channels, *streams, *coupled_streams,
          mapping, application, channels>2&&mapping_family==1);
 }
@@ -730,7 +741,7 @@ static int opus_multistream_encode_native
    opus_int32 smallest_packet;
    ALLOC_STACK;
 
-   if (st->surround)
+   if (st->allocation_mode == ALLOCATION_MODE_SURROUND)
    {
       preemph_mem = ms_get_preemph_mem(st);
       mem = ms_get_window_mem(st);
@@ -784,7 +795,7 @@ static int opus_multistream_encode_native
    mono_size = opus_encoder_get_size(1);
 
    ALLOC(bandSMR, 21*st->layout.nb_channels, opus_val16);
-   if (st->surround)
+   if (st->allocation_mode == ALLOCATION_MODE_SURROUND)
    {
       surround_analysis(celt_mode, pcm, bandSMR, mem, preemph_mem, frame_size, 120, st->layout.nb_channels, Fs, copy_channel_in, st->arch);
    }
@@ -813,7 +824,7 @@ static int opus_multistream_encode_native
       else
          ptr += align(mono_size);
       opus_encoder_ctl(enc, OPUS_SET_BITRATE(bitrates[s]));
-      if (st->surround)
+      if (st->allocation_mode == ALLOCATION_MODE_SURROUND)
       {
          opus_int32 equiv_rate;
          equiv_rate = st->bitrate_bps;
@@ -859,7 +870,7 @@ static int opus_multistream_encode_native
          (*copy_channel_in)(buf+1, 2,
             pcm, st->layout.nb_channels, right, frame_size);
          ptr += align(coupled_size);
-         if (st->surround)
+         if (st->allocation_mode == ALLOCATION_MODE_SURROUND)
          {
             for (i=0;i<21;i++)
             {
@@ -875,7 +886,7 @@ static int opus_multistream_encode_native
          (*copy_channel_in)(buf, 1,
             pcm, st->layout.nb_channels, chan, frame_size);
          ptr += align(mono_size);
-         if (st->surround)
+         if (st->allocation_mode == ALLOCATION_MODE_SURROUND)
          {
             for (i=0;i<21;i++)
                bandLogE[i] = bandSMR[21*chan+i];
@@ -883,7 +894,7 @@ static int opus_multistream_encode_native
          c1 = chan;
          c2 = -1;
       }
-      if (st->surround)
+      if (st->allocation_mode == ALLOCATION_MODE_SURROUND)
          opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE));
       /* number of bytes left (+Toc) */
       curr_max = max_data_bytes - tot_size;
@@ -1183,7 +1194,7 @@ int opus_multistream_encoder_ctl(OpusMSEncoder *st, int request, ...)
    {
       int s;
       st->subframe_mem[0] = st->subframe_mem[1] = st->subframe_mem[2] = 0;
-      if (st->surround)
+      if (st->allocation_mode == ALLOCATION_MODE_SURROUND)
       {
          OPUS_CLEAR(ms_get_preemph_mem(st), st->layout.nb_channels);
          OPUS_CLEAR(ms_get_window_mem(st), st->layout.nb_channels*120);
-- 
2.8.0.rc3.226.g39d4020



More information about the opus mailing list