Merge "C2SoftOpusEnc: Fix mono encoding" into qt-dev am: 46238cd039 am: f731834fe7
am: 8773eeecff

Change-Id: Ie901eef1cdd9c4f06b5ce447c856d5b6bd380d72
diff --git a/media/codec2/components/opus/C2SoftOpusEnc.cpp b/media/codec2/components/opus/C2SoftOpusEnc.cpp
index 2d110f7..384d58b 100644
--- a/media/codec2/components/opus/C2SoftOpusEnc.cpp
+++ b/media/codec2/components/opus/C2SoftOpusEnc.cpp
@@ -38,6 +38,8 @@
 
 }  // namespace
 
+static const int kMaxNumChannelsSupported = 2;
+
 class C2SoftOpusEnc::IntfImpl : public SimpleInterface<void>::BaseParams {
 public:
     explicit IntfImpl(const std::shared_ptr<C2ReflectorHelper> &helper)
@@ -71,7 +73,7 @@
         addParameter(
                 DefineParam(mChannelCount, C2_PARAMKEY_CHANNEL_COUNT)
                 .withDefault(new C2StreamChannelCountInfo::input(0u, 1))
-                .withFields({C2F(mChannelCount, value).inRange(1, 8)})
+                .withFields({C2F(mChannelCount, value).inRange(1, kMaxNumChannelsSupported)})
                 .withSetter((Setter<decltype(*mChannelCount)>::StrictValueWithNoDeps))
                 .build());
 
@@ -128,9 +130,8 @@
 }
 
 c2_status_t C2SoftOpusEnc::configureEncoder() {
-    unsigned char mono_mapping[256] = {0};
-    unsigned char stereo_mapping[256] = {0, 1};
-    unsigned char surround_mapping[256] = {0, 1, 255};
+    static const unsigned char mono_mapping[256] = {0};
+    static const unsigned char stereo_mapping[256] = {0, 1};
     mSampleRate = mIntf->getSampleRate();
     mChannelCount = mIntf->getChannelCount();
     uint32_t bitrate = mIntf->getBitrate();
@@ -140,13 +141,14 @@
         mChannelCount * mNumSamplesPerFrame * sizeof(int16_t);
     int err = C2_OK;
 
-    unsigned char* mapping;
-    if (mChannelCount < 2) {
+    const unsigned char* mapping;
+    if (mChannelCount == 1) {
         mapping = mono_mapping;
     } else if (mChannelCount == 2) {
         mapping = stereo_mapping;
     } else {
-        mapping = surround_mapping;
+        ALOGE("Number of channels (%d) is not supported", mChannelCount);
+        return C2_BAD_VALUE;
     }
 
     if (mEncoder != nullptr) {
@@ -154,7 +156,7 @@
     }
 
     mEncoder = opus_multistream_encoder_create(mSampleRate, mChannelCount,
-        1, 1, mapping, OPUS_APPLICATION_AUDIO, &err);
+        1, mChannelCount - 1, mapping, OPUS_APPLICATION_AUDIO, &err);
     if (err) {
         ALOGE("Could not create libopus encoder. Error code: %i", err);
         return C2_CORRUPTED;