goldfish-codecs: Resend cds0 and cds1 upon portFlush

When port flushes, the host side decoder is re-created
and they need the initial cds0 and cds1 to work properly

BUG: 124388359

Test:
android.media.cts.DecoderTest#testCodecResetsH264WithSurface

Change-Id: I07db146b5e8364f2e66a54cdbda6fa35cf5b5e6b
diff --git a/system/codecs/omx/avcdec/GoldfishAVCDec.cpp b/system/codecs/omx/avcdec/GoldfishAVCDec.cpp
index d6f7f90..31470a5 100644
--- a/system/codecs/omx/avcdec/GoldfishAVCDec.cpp
+++ b/system/codecs/omx/avcdec/GoldfishAVCDec.cpp
@@ -207,10 +207,30 @@
     return true;
 }
 
+void GoldfishAVCDec::readAndDiscardAllHostBuffers() {
+    while (mContext) {
+        h264_image_t img = mContext->getImage();
+        if (img.data != nullptr) {
+            ALOGD("img pts %lld is discarded", (long long)img.pts);
+        } else {
+            return;
+        }
+    }
+}
+
 void GoldfishAVCDec::onPortFlushCompleted(OMX_U32 portIndex) {
     /* Once the output buffers are flushed, ignore any buffers that are held in decoder */
     if (kOutputPortIndex == portIndex) {
         setFlushMode();
+        ALOGD("%s %d", __func__, __LINE__);
+        readAndDiscardAllHostBuffers();
+        mContext->resetH264Context(mWidth, mHeight, mWidth, mHeight, MediaH264Decoder::PixelFormat::YUV420P);
+        if (!mCsd0.empty() && !mCsd1.empty()) {
+            mContext->decodeFrame(&(mCsd0[0]), mCsd0.size(), 0);
+            mContext->getImage();
+            mContext->decodeFrame(&(mCsd1[0]), mCsd1.size(), 0);
+            mContext->getImage();
+        }
         resetPlugin();
     } else {
         mInputOffset = 0;
@@ -308,6 +328,15 @@
             // TODO: We also need to send the timestamp
             h264_result_t h264Res = {(int)MediaH264Decoder::Err::NoErr, 0};
             if (inHeader != nullptr) {
+                if (inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) { 
+                    unsigned long mysize = (inHeader->nFilledLen - mInputOffset);
+                    uint8_t* mydata = mInPBuffer;
+                    if (mCsd0.empty()) {
+                        mCsd0.assign(mydata, mydata + mysize);
+                    } else if (mCsd1.empty()) {
+                        mCsd1.assign(mydata, mydata + mysize);
+                    }
+                }
                 ALOGD("Decoding frame(sz=%lu)", (unsigned long)(inHeader->nFilledLen - mInputOffset));
                 h264Res = mContext->decodeFrame(mInPBuffer,
                                                 inHeader->nFilledLen - mInputOffset,
diff --git a/system/codecs/omx/avcdec/GoldfishAVCDec.h b/system/codecs/omx/avcdec/GoldfishAVCDec.h
index d3da30d..3b25630 100644
--- a/system/codecs/omx/avcdec/GoldfishAVCDec.h
+++ b/system/codecs/omx/avcdec/GoldfishAVCDec.h
@@ -89,6 +89,8 @@
     status_t resetPlugin();
 
 
+    void readAndDiscardAllHostBuffers();
+
     bool setDecodeArgs(
             OMX_BUFFERHEADERTYPE *inHeader,
             OMX_BUFFERHEADERTYPE *outHeader);
@@ -96,6 +98,8 @@
     bool getVUIParams(h264_image_t& img);
 
     std::unique_ptr<MediaH264Decoder> mContext;
+    std::vector<uint8_t> mCsd0;
+    std::vector<uint8_t> mCsd1;
     uint64_t mConsumedBytes = 0;
     uint8_t* mInPBuffer = nullptr;
     uint8_t* mOutHeaderBuf = nullptr;
diff --git a/system/codecs/omx/avcdec/MediaH264Decoder.cpp b/system/codecs/omx/avcdec/MediaH264Decoder.cpp
index 05111cc..3ecc489 100644
--- a/system/codecs/omx/avcdec/MediaH264Decoder.cpp
+++ b/system/codecs/omx/avcdec/MediaH264Decoder.cpp
@@ -57,7 +57,7 @@
                                        PixelFormat pixFmt) {
     auto transport = GoldfishMediaTransport::getInstance();
     if (!mHasAddressSpaceMemory) {
-        ALOGE("%s no address space memory");
+        ALOGE("%s no address space memory", __func__);
         return;
     }
     transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
@@ -74,6 +74,7 @@
 
 void MediaH264Decoder::destroyH264Context() {
 
+    ALOGD("return memory lot %d addrr %x", (int)(mAddressOffSet >> 23), mAddressOffSet);
     auto transport = GoldfishMediaTransport::getInstance();
     transport->writeParam((uint64_t)mHostHandle, 0, mAddressOffSet);
     transport->sendOperation(MediaCodecType::H264Codec,
@@ -86,7 +87,7 @@
     ALOGD("decode frame: use handle to host %lld", mHostHandle);
     h264_result_t res = {0, 0};
     if (!mHasAddressSpaceMemory) {
-        ALOGE("%s no address space memory");
+        ALOGE("%s no address space memory", __func__);
         return res;
     }
     auto transport = GoldfishMediaTransport::getInstance();
@@ -111,7 +112,7 @@
 
 void MediaH264Decoder::flush() {
     if (!mHasAddressSpaceMemory) {
-        ALOGE("%s no address space memory");
+        ALOGE("%s no address space memory", __func__);
         return;
     }
     ALOGD("flush: use handle to host %lld", mHostHandle);
@@ -125,7 +126,7 @@
     ALOGD("getImage: use handle to host %lld", mHostHandle);
     h264_image_t res { };
     if (!mHasAddressSpaceMemory) {
-        ALOGE("%s no address space memory");
+        ALOGE("%s no address space memory", __func__);
         return res;
     }
     auto transport = GoldfishMediaTransport::getInstance();