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

Michael Graczyk mgraczyk at google.com
Mon May 23 17:27:27 UTC 2016


Hi Jean-Marc,

On Sat, May 21, 2016 at 1:58 PM, Jean-Marc Valin <jmvalin at jmvalin.ca> wrote:
> 1) I think "allocation mode" should be renamed to "mapping type" or
> something similar that to make it less confusing.
Done

> 2) After discussing with Tim, the conclusion is that we should use
> mapping family 254 in the short term. We can switch to mapping family 2
> when standardization is a bit more advanced.
Sounds good, done.

> 3) Unless it causes problems in the existing code, it seems like family
> 254 should already set mapping_type=MAPPING_TYPE_AMBISONICS. From my
> understanding, it would not change the code behaviour, but would make it
> clearer what the new type is for (or did I miss something?).
Done, I think. The patch still doesn't change behavior, but it now
sets the member OpusMSEncoder.mapping_type to MAPPING_TYPE_AMBISONICS.
-------------- next part --------------
From 75b9f240aabcc6418514831270af1ced5b11af77 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 only codes each channel independently with no
special allocation rules.
---
 configure.ac                   | 11 +++++++++
 src/opus_multistream_encoder.c | 56 ++++++++++++++++++++++++++++--------------
 2 files changed, 49 insertions(+), 18 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..3061373 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 {
+  MAPPING_TYPE_NONE = 0,
+  MAPPING_TYPE_SURROUND = 1,
+  MAPPING_TYPE_AMBISONICS = 254,
+} MappingType;
+
 struct OpusMSEncoder {
    ChannelLayout layout;
    int arch;
    int lfe_stream;
    int application;
    int variable_duration;
-   int surround;
+   MappingType mapping_type;
    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==254 && 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
+      MappingType mapping_type
 )
 {
    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 (mapping_type != MAPPING_TYPE_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 (mapping_type == MAPPING_TYPE_SURROUND)
    {
       OPUS_CLEAR(ms_get_preemph_mem(st), channels);
       OPUS_CLEAR(ms_get_window_mem(st), channels*120);
    }
-   st->surround = surround;
+   st->mapping_type = mapping_type;
    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, MAPPING_TYPE_NONE);
 }
 
 int opus_multistream_surround_encoder_init(
@@ -496,6 +505,7 @@ int opus_multistream_surround_encoder_init(
       int application
 )
 {
+   int mapping_type;
    if ((channels>255) || (channels<1))
       return OPUS_BAD_ARG;
    st->lfe_stream = -1;
@@ -523,7 +533,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==254 && ENABLE_EXPERIMENTAL_AMBISONICS))
    {
       int i;
       *streams=channels;
@@ -532,8 +543,17 @@ 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);
+
+   if (channels>2 && mapping_family==1) {
+      mapping_type = MAPPING_TYPE_SURROUND;
+   } else if (mapping_family==254 && ENABLE_EXPERIMENTAL_AMBISONICS) {
+      mapping_type = MAPPING_TYPE_AMBISONICS;
+   } else {
+      mapping_type = MAPPING_TYPE_NONE;
+   }
+   return opus_multistream_encoder_init_impl(st, Fs, channels, *streams,
+                                             *coupled_streams, mapping,
+                                             application, mapping_type);
 }
 
 OpusMSEncoder *opus_multistream_encoder_create(
@@ -730,7 +750,7 @@ static int opus_multistream_encode_native
    opus_int32 smallest_packet;
    ALLOC_STACK;
 
-   if (st->surround)
+   if (st->mapping_type == MAPPING_TYPE_SURROUND)
    {
       preemph_mem = ms_get_preemph_mem(st);
       mem = ms_get_window_mem(st);
@@ -784,7 +804,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->mapping_type == MAPPING_TYPE_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 +833,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->mapping_type == MAPPING_TYPE_SURROUND)
       {
          opus_int32 equiv_rate;
          equiv_rate = st->bitrate_bps;
@@ -859,7 +879,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->mapping_type == MAPPING_TYPE_SURROUND)
          {
             for (i=0;i<21;i++)
             {
@@ -875,7 +895,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->mapping_type == MAPPING_TYPE_SURROUND)
          {
             for (i=0;i<21;i++)
                bandLogE[i] = bandSMR[21*chan+i];
@@ -883,7 +903,7 @@ static int opus_multistream_encode_native
          c1 = chan;
          c2 = -1;
       }
-      if (st->surround)
+      if (st->mapping_type == MAPPING_TYPE_SURROUND)
          opus_encoder_ctl(enc, OPUS_SET_ENERGY_MASK(bandLogE));
       /* number of bytes left (+Toc) */
       curr_max = max_data_bytes - tot_size;
@@ -1183,7 +1203,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->mapping_type == MAPPING_TYPE_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