Make sure not to ask for more buffers when we know that there won't be any, added a quirk for the aac decoder.
diff --git a/include/media/stagefright/OMXCodec.h b/include/media/stagefright/OMXCodec.h
index 0b94118..e4f6a4e 100644
--- a/include/media/stagefright/OMXCodec.h
+++ b/include/media/stagefright/OMXCodec.h
@@ -82,6 +82,7 @@
         kWantsNALFragments                   = 2,
         kRequiresLoadedToIdleAfterAllocation = 4,
         kRequiresAllocateBufferOnInputPorts  = 8,
+        kRequiresFlushCompleteEmulation      = 16,
     };
 
     struct BufferInfo {
@@ -165,7 +166,13 @@
     void drainInputBuffers();
     void fillOutputBuffers();
 
-    void flushPortAsync(OMX_U32 portIndex);
+    // Returns true iff a flush was initiated and a completion event is
+    // upcoming, false otherwise (A flush was not necessary as we own all
+    // the buffers on that port).
+    // This method will ONLY ever return false for a component with quirk
+    // "kRequiresFlushCompleteEmulation".
+    bool flushPortAsync(OMX_U32 portIndex);
+
     void disablePortAsync(OMX_U32 portIndex);
     void enablePortAsync(OMX_U32 portIndex);
 
diff --git a/media/libstagefright/OMXCodec.cpp b/media/libstagefright/OMXCodec.cpp
index ec9f6d3..fec6cfb 100644
--- a/media/libstagefright/OMXCodec.cpp
+++ b/media/libstagefright/OMXCodec.cpp
@@ -193,6 +193,7 @@
     }
     if (!strcmp(componentName, "OMX.TI.AAC.decode")) {
         quirks |= kNeedsFlushBeforeDisable;
+        quirks |= kRequiresFlushCompleteEmulation;
     }
     if (!strncmp(componentName, "OMX.qcom.video.encoder.", 23)) {
         quirks |= kRequiresLoadedToIdleAfterAllocation;
@@ -1163,21 +1164,38 @@
     setState(RECONFIGURING);
 
     if (mQuirks & kNeedsFlushBeforeDisable) {
-        flushPortAsync(portIndex);
+        if (!flushPortAsync(portIndex)) {
+            onCmdComplete(OMX_CommandFlush, portIndex);
+        }
     } else {
         disablePortAsync(portIndex);
     }
 }
 
-void OMXCodec::flushPortAsync(OMX_U32 portIndex) {
+bool OMXCodec::flushPortAsync(OMX_U32 portIndex) {
     CHECK(mState == EXECUTING || mState == RECONFIGURING);
 
+    LOGV("flushPortAsync(%ld): we own %d out of %d buffers already.",
+         portIndex, countBuffersWeOwn(mPortBuffers[portIndex]),
+         mPortBuffers[portIndex].size());
+
     CHECK_EQ(mPortStatus[portIndex], ENABLED);
     mPortStatus[portIndex] = SHUTTING_DOWN;
 
+    if ((mQuirks & kRequiresFlushCompleteEmulation)
+        && countBuffersWeOwn(mPortBuffers[portIndex])
+                == mPortBuffers[portIndex].size()) {
+        // No flush is necessary and this component fails to send a
+        // flush-complete event in this case.
+
+        return false;
+    }
+
     status_t err =
         mOMX->send_command(mNode, OMX_CommandFlush, portIndex);
     CHECK_EQ(err, OK);
+
+    return true;
 }
 
 void OMXCodec::disablePortAsync(OMX_U32 portIndex) {
@@ -1323,6 +1341,12 @@
 void OMXCodec::fillOutputBuffer(BufferInfo *info) {
     CHECK_EQ(info->mOwnedByComponent, false);
 
+    if (mNoMoreOutputData) {
+        LOGV("There is no more output data available, not "
+             "calling fillOutputBuffer");
+        return;
+    }
+
     LOGV("Calling fill_buffer on buffer %p", info->mBuffer);
     mOMX->fill_buffer(mNode, info->mBuffer);
 
@@ -1648,8 +1672,16 @@
 
         CHECK_EQ(mState, EXECUTING);
 
-        flushPortAsync(kPortIndexInput);
-        flushPortAsync(kPortIndexOutput);
+        bool emulateInputFlushCompletion = !flushPortAsync(kPortIndexInput);
+        bool emulateOutputFlushCompletion = !flushPortAsync(kPortIndexOutput);
+
+        if (emulateInputFlushCompletion) {
+            onCmdComplete(OMX_CommandFlush, kPortIndexInput);
+        }
+
+        if (emulateOutputFlushCompletion) {
+            onCmdComplete(OMX_CommandFlush, kPortIndexOutput);
+        }
     }
 
     while (mState != ERROR && !mNoMoreOutputData && mFilledBuffers.empty()) {