RESTRICT AUTOMERGE CCodec: workaround for guessing output buffer size

Bug: 122050147
Test: bug repro steps
Change-Id: I01d0f8b566fac10a8331e6279a55a7c6deb4bf68
(cherry picked from commit d3c4c2008e80ed92600dc987d4d1d04f1f3ee95c)
diff --git a/media/sfplugin/CCodecBufferChannel.cpp b/media/sfplugin/CCodecBufferChannel.cpp
index 31f133b..c249532 100644
--- a/media/sfplugin/CCodecBufferChannel.cpp
+++ b/media/sfplugin/CCodecBufferChannel.cpp
@@ -1252,7 +1252,21 @@
 
     sp<Codec2Buffer> allocateArrayBuffer() override {
         // TODO: proper max output size
-        return new LocalLinearBuffer(mFormat, new ABuffer(kLinearBufferSize));
+        size_t capacity = kLinearBufferSize;
+        int32_t width = 0, height = 0;
+        bool video = mFormat->findInt32(KEY_MAX_WIDTH, &width)
+                  && mFormat->findInt32(KEY_MAX_HEIGHT, &height);
+        if (!video) {
+            video = mFormat->findInt32(KEY_WIDTH, &width)
+                 && mFormat->findInt32(KEY_HEIGHT, &height);
+        }
+        if (video) {
+            // Assuming data compression ratio better than 3:1.
+            capacity = std::min(std::max(capacity, (size_t)width * height / 2),
+                                kMaxLinearBufferSize);
+        }
+        ALOGD("[%s] Using linear capacity of %zu for array buffer", mName, capacity);
+        return new LocalLinearBuffer(mFormat, new ABuffer(capacity));
     }
 };
 
@@ -2178,8 +2192,7 @@
                     outputGeneration);
         }
 
-        if (oStreamFormat.value == C2BufferData::LINEAR
-                && mComponentName.find("c2.qti.") == std::string::npos) {
+        if (oStreamFormat.value == C2BufferData::LINEAR) {
             // WORKAROUND: if we're using early CSD workaround we convert to
             //             array mode, to appease apps assuming the output
             //             buffers to be of the same size.
@@ -2256,8 +2269,7 @@
                     ALOGD("[%s] buffer capacity too small for the config (%zu < %zu)",
                             mName, buffer->capacity(), config->size());
                 }
-            } else if (oStreamFormat.value == C2BufferData::LINEAR && i == 0
-                    && mComponentName.find("c2.qti.") == std::string::npos) {
+            } else if (oStreamFormat.value == C2BufferData::LINEAR && i == 0) {
                 // WORKAROUND: Some apps expect CSD available without queueing
                 //             any input. Queue an empty buffer to get the CSD.
                 buffer->setRange(0, 0);